blob: 7d9a156f887e343b65e059a5ad1da87228416221 [file] [log] [blame]
Andreas Eversberg39621c42012-06-27 15:36:27 +02001/* PDCH scheduler
2 *
3 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include <gprs_bssgp_pcu.h>
21#include <gprs_rlcmac.h>
22#include <pcu_l1_if.h>
23
24extern struct llist_head block_queue;
25
26static uint8_t rlcmac_dl_idle[23] = {
27 0x47, /* control without optional header octets, no polling, USF=111 */
28 0x94, /* dummy downlink control message, paging mode 00 */
29 0x2b, /* no persistance level, 7 bits spare pattern */
30 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b,
31 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b
32};
33
34int gprs_rlcmac_rcv_rts_block(uint8_t trx, uint8_t ts, uint16_t arfcn,
35 uint32_t fn, uint8_t block_nr)
36{
37 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
38 struct gprs_rlcmac_pdch *pdch;
39 struct gprs_rlcmac_tbf *tbf;
40 uint8_t usf = 0x7;
41 struct msgb *msg = NULL;
42 uint32_t poll_fn;
43 uint8_t i, tfi;
44
45 if (trx >= 8 || ts >= 8)
46 return -EINVAL;
47 pdch = &bts->trx[trx].pdch[ts];
48
49 if (!pdch->enable) {
50 LOGP(DRLCMACSCHED, LOGL_ERROR, "Received RTS on disabled PDCH: "
51 "TRX=%d TS=%d\n", trx, ts);
52 return -EIO;
53 }
54
Andreas Eversberge6228b32012-07-03 13:36:03 +020055 /* store last frame number of RTS */
56 pdch->last_rts_fn = fn;
57
Andreas Eversberg39621c42012-06-27 15:36:27 +020058 /* check uplink ressource for polling */
59 poll_fn = fn + 4;
60 if ((block_nr % 3) == 2)
61 poll_fn ++;
62 poll_fn = poll_fn % 2715648;
63 for (tfi = 0; tfi < 32; tfi++) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020064 tbf = pdch->ul_tbf[tfi];
65 if (tbf) {
66 /* no polling */
67 if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED)
68 continue;
69 /* polling for next uplink block */
70 if (tbf->poll_fn == poll_fn)
71 break;
72 }
73 tbf = pdch->dl_tbf[tfi];
74 if (tbf) {
75 /* no polling */
76 if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED)
77 continue;
78 /* polling for next uplink block */
79 if (tbf->poll_fn == poll_fn)
80 break;
81 }
Andreas Eversberg39621c42012-06-27 15:36:27 +020082 }
83 /* found uplink where a block is polled */
84 if (tfi < 32) {
85 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d "
86 "TS=%d FN=%d block_nr=%d scheduling free USF for "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020087 "polling at FN=%d of %s TFI=%d\n", trx, ts, fn,
88 block_nr, poll_fn,
89 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
90 tfi);
Andreas Eversberg39621c42012-06-27 15:36:27 +020091 /* use free USF */
92 /* else, we search for uplink ressource */
93 } else {
94 /* select uplink ressource */
95 for (i = 0, tfi = pdch->next_ul_tfi; i < 32;
96 i++, tfi = (tfi + 1) & 31) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020097 tbf = pdch->ul_tbf[tfi];
Andreas Eversberg39621c42012-06-27 15:36:27 +020098 /* no TBF for this tfi, go next */
99 if (!tbf)
100 continue;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200101 /* no UL ressources needed, go next */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200102 /* we don't need to give ressources in FINISHED state,
103 * because we have received all blocks and only poll
104 * for packet control ack. */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200105 if (tbf->state != GPRS_RLCMAC_FLOW)
106 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200107
Andreas Eversberg39621c42012-06-27 15:36:27 +0200108 /* use this USF */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200109 usf = tbf->dir.ul.usf[ts];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200110 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: "
111 "TRX=%d TS=%d FN=%d block_nr=%d scheduling "
112 "USF=%d for required uplink ressource of "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200113 "UL TBF=%d\n", trx, ts, fn, block_nr, usf, tfi);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200114 /* next TBF to handle ressource is the next one */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200115 pdch->next_ul_tfi = (tfi + 1) & 31;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200116 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200117 }
118 }
119
120 /* Prio 1: select control message */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200121 for (i = 0; i < 64; i++) {
122 if (i < 32)
123 tbf = pdch->ul_tbf[i];
124 else
125 tbf = pdch->dl_tbf[i & 31];
Andreas Eversberge6228b32012-07-03 13:36:03 +0200126 /* no TBF for this tfi, go next */
127 if (!tbf)
128 continue;
129 /* schedule PACKET DOWNLINK ASSIGNMENT */
130 if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS)
131 msg = gprs_rlcmac_send_packet_downlink_assignment(tbf,
132 fn);
133 else
134 /* schedule PACKET UPLINK ASSIGNMENT */
135 if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS)
136 msg = gprs_rlcmac_send_packet_uplink_assignment(tbf,
137 fn);
138 else
139 /* schedule PACKET UPLINK ACK */
140 if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK)
Andreas Eversberg39621c42012-06-27 15:36:27 +0200141 msg = gprs_rlcmac_send_uplink_ack(tbf, fn);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200142 if (msg) {
143 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling control "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200144 "message at RTS for %s TBF=%d\n",
145 (tbf->direction == GPRS_RLCMAC_UL_TBF)
146 ? "UL" : "DL", tbf->tfi);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200147 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200148 }
149 }
150
151 /* Prio 2: select data message for downlink */
152 if (!msg) {
153 /* select downlink ressource */
154 for (i = 0, tfi = pdch->next_dl_tfi; i < 32;
155 i++, tfi = (tfi + 1) & 31) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200156 tbf = pdch->dl_tbf[tfi];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200157 /* no TBF for this tfi, go next */
158 if (!tbf)
159 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200160 /* no DL TBF, go next */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200161 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
162 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200163 /* no DL ressources needed, go next */
164 if (tbf->state != GPRS_RLCMAC_FLOW
165 && tbf->state != GPRS_RLCMAC_FINISHED)
166 continue;
167
Andreas Eversberg39621c42012-06-27 15:36:27 +0200168 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling data "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200169 "message at RTS for DL TBF=%d\n", tfi);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200170 /* next TBF to handle ressource is the next one */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200171 pdch->next_dl_tfi = (tfi + 1) & 31;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200172 /* generate DL data block */
173 msg = gprs_rlcmac_send_data_block_acknowledged(tbf, fn);
174 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200175 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200176 }
177
178 /* Prio 3: send dummy contol message */
179 if (!msg) {
180 msg = msgb_alloc(23, "rlcmac_dl_idle");
181 if (!msg)
182 return -ENOMEM;
183 memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23);
184 }
185 /* msg is now available */
186
187 /* set USF */
188 msg->data[0] = (msg->data[0] & 0xf8) | usf;
189
190// printf("len=%d, date=%s\n", msg->len, osmo_hexdump(msg->data, msg->len));
191
192 /* send PDTCH/PACCH to L1 */
193 pcu_l1if_tx_pdtch(msg, trx, ts, arfcn, fn, block_nr);
194
195 return 0;
196}