blob: efd8a9ef200d9c9f9a6f54a8d7ed886d250b888f [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
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020024uint32_t sched_poll(uint8_t trx, uint8_t ts, uint32_t fn, uint8_t block_nr,
25 struct gprs_rlcmac_tbf **poll_tbf,
26 struct gprs_rlcmac_tbf **ul_ack_tbf,
27 struct gprs_rlcmac_tbf **dl_ass_tbf,
28 struct gprs_rlcmac_tbf **ul_ass_tbf)
Andreas Eversberg39621c42012-06-27 15:36:27 +020029{
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020030 struct gprs_rlcmac_tbf *tbf;
Andreas Eversberg39621c42012-06-27 15:36:27 +020031 uint32_t poll_fn;
Andreas Eversberge6228b32012-07-03 13:36:03 +020032
Andreas Eversberg592e04a2012-07-15 06:25:37 +020033 /* check special TBF for events */
Andreas Eversberg39621c42012-06-27 15:36:27 +020034 poll_fn = fn + 4;
35 if ((block_nr % 3) == 2)
36 poll_fn ++;
37 poll_fn = poll_fn % 2715648;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020038 llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
39 /* this trx, this ts */
40 if (tbf->trx != trx || tbf->control_ts != ts)
41 continue;
42 /* polling for next uplink block */
43 if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED
44 && tbf->poll_fn == poll_fn)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020045 *poll_tbf = tbf;
Andreas Eversbergee31b782012-07-15 16:27:01 +020046 if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020047 *ul_ack_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020048 if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020049 *dl_ass_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020050 if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020051 *ul_ass_tbf = tbf;
Andreas Eversberg39621c42012-06-27 15:36:27 +020052 }
Andreas Eversberg592e04a2012-07-15 06:25:37 +020053 llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
54 /* this trx, this ts */
55 if (tbf->trx != trx || tbf->control_ts != ts)
56 continue;
57 /* polling for next uplink block */
58 if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED
59 && tbf->poll_fn == poll_fn)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020060 *poll_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020061 if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020062 *dl_ass_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020063 if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020064 *ul_ass_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020065 }
66
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020067 return poll_fn;
68}
69
70uint8_t sched_select_uplink(uint8_t trx, uint8_t ts, uint32_t fn,
71 uint8_t block_nr, struct gprs_rlcmac_pdch *pdch)
72{
73 struct gprs_rlcmac_tbf *tbf;
74 uint8_t usf = 0x07;
75 uint8_t i, tfi;
76
77 /* select uplink ressource */
78 for (i = 0, tfi = pdch->next_ul_tfi; i < 32;
79 i++, tfi = (tfi + 1) & 31) {
80 tbf = pdch->ul_tbf[tfi];
81 /* no TBF for this tfi, go next */
82 if (!tbf)
83 continue;
84 /* no UL ressources needed, go next */
85 /* we don't need to give ressources in FINISHED state,
86 * because we have received all blocks and only poll
87 * for packet control ack. */
88 if (tbf->state != GPRS_RLCMAC_FLOW)
89 continue;
90
91 /* use this USF */
92 usf = tbf->dir.ul.usf[ts];
Andreas Eversberg39621c42012-06-27 15:36:27 +020093 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d "
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020094 "TS=%d FN=%d block_nr=%d scheduling USF=%d for "
95 "required uplink ressource of UL TBF=%d\n", trx, ts, fn,
96 block_nr, usf, tfi);
97 /* next TBF to handle ressource is the next one */
98 pdch->next_ul_tfi = (tfi + 1) & 31;
99 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200100 }
101
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200102 return usf;
103}
104
105struct msgb *sched_select_ctrl_msg(uint8_t trx, uint8_t ts, uint32_t fn,
106 uint8_t block_nr, struct gprs_rlcmac_pdch *pdch,
107 struct gprs_rlcmac_tbf *ul_ack_tbf,
108 struct gprs_rlcmac_tbf *dl_ass_tbf,
109 struct gprs_rlcmac_tbf *ul_ass_tbf)
110{
111 struct msgb *msg = NULL;
112 struct gprs_rlcmac_tbf *tbf = NULL;
113
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200114 /* schedule PACKET DOWNLINK ASSIGNMENT */
115 if (dl_ass_tbf) {
116 tbf = dl_ass_tbf;
117 msg = gprs_rlcmac_send_packet_downlink_assignment(tbf, fn);
Andreas Eversbergee31b782012-07-15 16:27:01 +0200118 }
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200119 /* schedule PACKET UPLINK ASSIGNMENT */
Andreas Eversbergee31b782012-07-15 16:27:01 +0200120 if (!msg && ul_ass_tbf) {
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200121 tbf = ul_ass_tbf;
122 msg = gprs_rlcmac_send_packet_uplink_assignment(tbf, fn);
Andreas Eversbergee31b782012-07-15 16:27:01 +0200123 }
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200124 /* schedule PACKET UPLINK ACK */
Andreas Eversbergee31b782012-07-15 16:27:01 +0200125 if (!msg && ul_ack_tbf) {
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200126 tbf = ul_ack_tbf;
127 msg = gprs_rlcmac_send_uplink_ack(tbf, fn);
128 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200129 /* any message */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200130 if (msg) {
131 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling control "
132 "message at RTS for %s TBF=%d (TRX=%d, TS=%d)\n",
133 (tbf->direction == GPRS_RLCMAC_UL_TBF)
134 ? "UL" : "DL", tbf->tfi, trx, ts);
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200135 return msg;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200136 }
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200137 /* schedule PACKET PAGING REQUEST */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200138 if (llist_empty(&pdch->paging_list))
139 return NULL;
140 msg = gprs_rlcmac_send_packet_paging_request(pdch);
141 if (msg)
142 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling paging request "
143 "message at RTS for (TRX=%d, TS=%d)\n", trx, ts);
144
145 return msg;
146}
147
148struct msgb *sched_select_downlink(uint8_t trx, uint8_t ts, uint32_t fn,
149 uint8_t block_nr, struct gprs_rlcmac_pdch *pdch)
150{
151 struct msgb *msg = NULL;
152 struct gprs_rlcmac_tbf *tbf = NULL;
153 uint8_t i, tfi;
154
155 /* select downlink ressource */
156 for (i = 0, tfi = pdch->next_dl_tfi; i < 32;
157 i++, tfi = (tfi + 1) & 31) {
158 tbf = pdch->dl_tbf[tfi];
159 /* no TBF for this tfi, go next */
160 if (!tbf)
161 continue;
162 /* no DL TBF, go next */
163 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
164 continue;
165 /* no DL ressources needed, go next */
166 if (tbf->state != GPRS_RLCMAC_FLOW
167 && tbf->state != GPRS_RLCMAC_FINISHED)
168 continue;
169
170 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling data message at "
171 "RTS for DL TBF=%d (TRX=%d, TS=%d)\n", tfi, trx, ts);
172 /* next TBF to handle ressource is the next one */
173 pdch->next_dl_tfi = (tfi + 1) & 31;
174 /* generate DL data block */
175 msg = gprs_rlcmac_send_data_block_acknowledged(tbf, fn,
176 ts);
177 break;
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200178 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200179
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200180 return msg;
181}
182static uint8_t rlcmac_dl_idle[23] = {
183 0x47, /* control without optional header octets, no polling, USF=111 */
184 0x94, /* dummy downlink control message, paging mode 00 */
185 0x2b, /* no persistance level, 7 bits spare pattern */
186 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b,
187 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b
188};
189
190struct msgb *sched_dummy(void)
191{
192 struct msgb *msg;
193
194 msg = msgb_alloc(23, "rlcmac_dl_idle");
195 if (!msg)
196 return NULL;
197 memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23);
198
199 return msg;
200}
201
202int gprs_rlcmac_rcv_rts_block(uint8_t trx, uint8_t ts, uint16_t arfcn,
203 uint32_t fn, uint8_t block_nr)
204{
205 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
206 struct gprs_rlcmac_pdch *pdch;
207 struct gprs_rlcmac_tbf *poll_tbf = NULL, *dl_ass_tbf = NULL,
208 *ul_ass_tbf = NULL, *ul_ack_tbf = NULL;
209 uint8_t usf = 0x7;
210 struct msgb *msg = NULL;
211 uint32_t poll_fn;
212
213 if (trx >= 8 || ts >= 8)
214 return -EINVAL;
215 pdch = &bts->trx[trx].pdch[ts];
216
217 if (!pdch->enable) {
218 LOGP(DRLCMACSCHED, LOGL_ERROR, "Received RTS on disabled PDCH: "
219 "TRX=%d TS=%d\n", trx, ts);
220 return -EIO;
221 }
222
223 /* store last frame number of RTS */
224 pdch->last_rts_fn = fn;
225
226 poll_fn = sched_poll(trx, ts, fn, block_nr, &poll_tbf, &ul_ack_tbf,
227 &dl_ass_tbf, &ul_ass_tbf);
228 /* check uplink ressource for polling */
229 if (poll_tbf)
230 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d "
231 "TS=%d FN=%d block_nr=%d scheduling free USF for "
232 "polling at FN=%d of %s TFI=%d\n", trx, ts, fn,
233 block_nr, poll_fn,
234 (poll_tbf->direction == GPRS_RLCMAC_UL_TBF)
235 ? "UL" : "DL", poll_tbf->tfi);
236 /* use free USF */
237 /* else, we search for uplink ressource */
238 else
239 usf = sched_select_uplink(trx, ts, fn, block_nr, pdch);
240
241 /* Prio 1: select control message */
242 msg = sched_select_ctrl_msg(trx, ts, fn, block_nr, pdch, ul_ack_tbf,
243 dl_ass_tbf, ul_ass_tbf);
244
Andreas Eversberg39621c42012-06-27 15:36:27 +0200245 /* Prio 2: select data message for downlink */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200246 if (!msg)
247 msg = sched_select_downlink(trx, ts, fn, block_nr, pdch);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200248
249 /* Prio 3: send dummy contol message */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200250 if (!msg)
251 msg = sched_dummy();
252
253 if (!msg)
254 return -ENOMEM;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200255 /* msg is now available */
256
257 /* set USF */
258 msg->data[0] = (msg->data[0] & 0xf8) | usf;
259
Andreas Eversberg39621c42012-06-27 15:36:27 +0200260 /* send PDTCH/PACCH to L1 */
261 pcu_l1if_tx_pdtch(msg, trx, ts, arfcn, fn, block_nr);
262
263 return 0;
264}