blob: c612f3e6ee0983e649809e700949c851e4ed17a6 [file] [log] [blame]
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +02001/* gprs_rlcmac.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include <gprs_rlcmac.h>
23#include <gprs_debug.h>
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020024#include <bts.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020025#include <tbf.h>
26
27#include <errno.h>
28
29/* 3GPP TS 05.02 Annex B.1 */
30
31#define MS_NA 255 /* N/A */
32#define MS_A 254 /* 1 with hopping, 0 without */
33#define MS_B 253 /* 1 with hopping, 0 without (change Rx to Tx)*/
34#define MS_C 252 /* 1 with hopping, 0 without (change Tx to Rx)*/
35
36struct gprs_ms_multislot_class {
37 uint8_t rx, tx, sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
38 uint8_t ta, tb, ra, rb; /* Minimum Number of Slots */
39 uint8_t type; /* Type of Mobile */
40};
41
42static const struct gprs_ms_multislot_class gprs_ms_multislot_class[32] = {
43/* M-S Class Rx Tx Sum Tta Ttb Tra Trb Type */
44/* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
45/* 1 */ { 1, 1, 2, 3, 2, 4, 2, 1 },
46/* 2 */ { 2, 1, 3, 3, 2, 3, 1, 1 },
47/* 3 */ { 2, 2, 3, 3, 2, 3, 1, 1 },
48/* 4 */ { 3, 1, 4, 3, 1, 3, 1, 1 },
49/* 5 */ { 2, 2, 4, 3, 1, 3, 1, 1 },
50/* 6 */ { 3, 2, 4, 3, 1, 3, 1, 1 },
51/* 7 */ { 3, 3, 4, 3, 1, 3, 1, 1 },
52/* 8 */ { 4, 1, 5, 3, 1, 2, 1, 1 },
53/* 9 */ { 3, 2, 5, 3, 1, 2, 1, 1 },
54/* 10 */ { 4, 2, 5, 3, 1, 2, 1, 1 },
55/* 11 */ { 4, 3, 5, 3, 1, 2, 1, 1 },
56/* 12 */ { 4, 4, 5, 2, 1, 2, 1, 1 },
57/* 13 */ { 3, 3, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
58/* 14 */ { 4, 4, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
59/* 15 */ { 5, 5, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
60/* 16 */ { 6, 6, MS_NA, MS_NA, MS_A, 2, MS_A, 2 },
61/* 17 */ { 7, 7, MS_NA, MS_NA, MS_A, 1, 0, 2 },
62/* 18 */ { 8, 8, MS_NA, MS_NA, 0, 0, 0, 2 },
63/* 19 */ { 6, 2, MS_NA, 3, MS_B, 2, MS_C, 1 },
64/* 20 */ { 6, 3, MS_NA, 3, MS_B, 2, MS_C, 1 },
65/* 21 */ { 6, 4, MS_NA, 3, MS_B, 2, MS_C, 1 },
66/* 22 */ { 6, 4, MS_NA, 2, MS_B, 2, MS_C, 1 },
67/* 23 */ { 6, 6, MS_NA, 2, MS_B, 2, MS_C, 1 },
68/* 24 */ { 8, 2, MS_NA, 3, MS_B, 2, MS_C, 1 },
69/* 25 */ { 8, 3, MS_NA, 3, MS_B, 2, MS_C, 1 },
70/* 26 */ { 8, 4, MS_NA, 3, MS_B, 2, MS_C, 1 },
71/* 27 */ { 8, 4, MS_NA, 2, MS_B, 2, MS_C, 1 },
72/* 28 */ { 8, 6, MS_NA, 2, MS_B, 2, MS_C, 1 },
73/* 29 */ { 8, 8, MS_NA, 2, MS_B, 2, MS_C, 1 },
74/* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
75/* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
76};
77
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +020078static inline int8_t find_free_usf(struct gprs_rlcmac_pdch *pdch)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020079{
80 struct gprs_rlcmac_tbf *tbf;
81 uint8_t usf_map = 0;
82 uint8_t tfi, usf;
83
84 /* make map of used USF */
85 for (tfi = 0; tfi < 32; tfi++) {
86 tbf = pdch->ul_tbf[tfi];
87 if (!tbf)
88 continue;
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +020089 usf_map |= (1 << tbf->dir.ul.usf[pdch->ts_no]);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020090 }
91
92 /* look for USF, don't use USF=7 */
93 for (usf = 0; usf < 7; usf++) {
94 if (!(usf_map & (1 << usf)))
95 return usf;
96 }
97
98 return -1;
99}
100
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200101static int find_enabled_pdch(struct gprs_rlcmac_trx *trx, const uint8_t start_ts)
102{
103 int ts;
104 for (ts = start_ts; ts < 8; ts++) {
105 struct gprs_rlcmac_pdch *pdch;
106
107 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200108 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200109 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
110 "not enabled\n", ts);
111 continue;
112 }
113 return ts;
114 }
115
116 return 8;
117}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200118
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200119static void assign_uplink_tbf_usf(
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200120 struct gprs_rlcmac_pdch *pdch,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200121 struct gprs_rlcmac_tbf *tbf, int8_t usf)
122{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100123 tbf->trx->ul_tbf[tbf->tfi()] = tbf;
124 pdch->ul_tbf[tbf->tfi()] = tbf;
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200125 tbf->pdch[pdch->ts_no] = pdch;
126 tbf->dir.ul.usf[pdch->ts_no] = usf;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200127}
128
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200129static void assign_dlink_tbf(
130 struct gprs_rlcmac_pdch *pdch,
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200131 struct gprs_rlcmac_tbf *tbf)
132{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100133 tbf->trx->dl_tbf[tbf->tfi()] = tbf;
134 pdch->dl_tbf[tbf->tfi()] = tbf;
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200135 tbf->pdch[pdch->ts_no] = pdch;
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200136}
137
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200138
139/* Slot Allocation: Algorithm A
140 *
141 * Assign single slot for uplink and downlink
142 */
143int alloc_algorithm_a(struct gprs_rlcmac_bts *bts,
144 struct gprs_rlcmac_tbf *old_tbf,
145 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
146{
147 struct gprs_rlcmac_pdch *pdch;
148 uint8_t ts;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200149
150 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
151 "%d\n", tbf->ms_class);
152
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200153 ts = find_enabled_pdch(tbf->trx, 0);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200154 if (ts == 8)
155 return -EINVAL;
156
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200157 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200158 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther948a3d62013-09-30 14:10:23 +0200159 int8_t usf; /* must be signed */
160
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200161 /* if USF available */
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200162 usf = find_free_usf(pdch);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200163 if (usf < 0) {
164 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
165 "allocating TS=%d, no USF available\n", ts);
166 return -EBUSY;
167 }
168 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink "
169 "TS=%d USF=%d\n", ts, usf);
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200170 assign_uplink_tbf_usf(pdch, tbf, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200171 } else {
172 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d\n", ts);
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200173 assign_dlink_tbf(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200174 }
175 /* the only one TS is the common TS */
176 tbf->first_ts = tbf->first_common_ts = ts;
177
178 return 0;
179}
180
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100181/*
182 * Select a window of Rx slots if available.
183 * The maximum allowed slots depend on RX or the window of available
184 * slots. This must be done for uplink TBF also, because it is the basis
185 * for calculating control slot and uplink slot(s).
186 */
187static uint8_t select_dl_slots(struct gprs_rlcmac_trx *trx,
188 const int ms_type, const int ms_max_rxslots,
189 uint8_t *out_rx_win_min, uint8_t *out_rx_win_max)
190
191{
192 uint8_t rx_window = 0;
193 int rx_window_size = 0;
194 int8_t last_tsc = -1; /* must be signed */
195 uint8_t rx_win_min = 0, rx_win_max = 0;
196
197 for (int ts_no = 0; ts_no < 8; ts_no++) {
198 struct gprs_rlcmac_pdch *pdch;
199 pdch = &trx->pdch[ts_no];
200
201 /* check if enabled */
202 if (!pdch->is_enabled()) {
203 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
204 "not enabled\n", ts_no);
205 if (ms_type == 1 && rx_window)
206 rx_window_size += 1;
207 continue;
208 }
209 /* check if TSC changes */
210 if (last_tsc < 0)
211 last_tsc = pdch->tsc;
212 else if (last_tsc != pdch->tsc) {
213 LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of TRX=%d, "
214 "because it has different TSC than lower TS "
215 "of TRX. In order to allow multislot, all "
216 "slots must be configured with the same "
217 "TSC!\n", ts_no, trx->trx_no);
218 if (ms_type == 1 && rx_window)
219 rx_window_size += 1;
220 continue;
221 }
222
223 if (!rx_window)
224 rx_win_min = ts_no;
225
226 rx_window |= (1 << ts_no);
227 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected DL TS %d\n", ts_no);
228
229 /* range of window (required for Type 1) */
230 rx_win_max = ts_no;
231
232 if (++rx_window_size == ms_max_rxslots) {
233 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because slots / "
234 "window reached maximum alowed Rx size\n");
235 break;
236 }
237 }
238
239 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected slots for RX: "
240 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
241 ((rx_window & 0x01)) ? 'D' : '.',
242 ((rx_window & 0x02)) ? 'D' : '.',
243 ((rx_window & 0x04)) ? 'D' : '.',
244 ((rx_window & 0x08)) ? 'D' : '.',
245 ((rx_window & 0x10)) ? 'D' : '.',
246 ((rx_window & 0x20)) ? 'D' : '.',
247 ((rx_window & 0x40)) ? 'D' : '.',
248 ((rx_window & 0x80)) ? 'D' : '.');
249
250 *out_rx_win_min = rx_win_min;
251 *out_rx_win_max = rx_win_max;
252 return rx_window;
253}
254
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100255static int reduce_rx_window(const int ms_type, const struct gprs_rlcmac_tbf *old_tbf,
256 const int Tt, const int Tr,
257 int *rx_window,
258 uint8_t *rx_win_min, uint8_t *rx_win_max)
259{
260 if (ms_type != 1)
261 return 0;
262 if (!old_tbf)
263 return 0;
264 if (old_tbf->direction != GPRS_RLCMAC_UL_TBF)
265 return 0;
266
267 uint8_t collide = 0, ul_usage = 0;
268
269 /* calculate mask of colliding slots */
270 for (uint8_t ts_no = 0; ts_no < 8; ts_no++) {
271 int j;
272 if (!old_tbf->pdch[ts_no])
273 continue;
274
275 ul_usage |= (1 << ts_no);
276 /* mark bits from TS-t .. TS+r */
277 for (j = ts_no - Tt; j != ((ts_no + Tr + 1) & 7); j = (j + 1) & 7)
278 collide |= (1 << j);
279 }
280
281 LOGP(DRLCMAC, LOGL_DEBUG, "- Not allowed slots due to existing "
282 "UL allocation: (TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7) "
283 " D=downlink x=not usable\n",
284 ((ul_usage & 0x01)) ? 'D' : ((collide & 0x01))?'x':'.',
285 ((ul_usage & 0x02)) ? 'D' : ((collide & 0x02))?'x':'.',
286 ((ul_usage & 0x04)) ? 'D' : ((collide & 0x04))?'x':'.',
287 ((ul_usage & 0x08)) ? 'D' : ((collide & 0x08))?'x':'.',
288 ((ul_usage & 0x10)) ? 'D' : ((collide & 0x10))?'x':'.',
289 ((ul_usage & 0x20)) ? 'D' : ((collide & 0x20))?'x':'.',
290 ((ul_usage & 0x40)) ? 'D' : ((collide & 0x40))?'x':'.',
291 ((ul_usage & 0x80)) ? 'D' : ((collide & 0x80))?'x':'.');
292
293 /*
294 * Uplink/Downlink in GSM is shifted by three timeslots. Make
295 * sure they don't collide.
296 */
297 *rx_window &= ~(collide << 3);
298 *rx_window &= ~(collide >> 5);
299 LOGP(DRLCMAC, LOGL_DEBUG, "- Remaining slots for RX: "
300 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
301 ((*rx_window & 0x01)) ? 'D' : '.',
302 ((*rx_window & 0x02)) ? 'D' : '.',
303 ((*rx_window & 0x04)) ? 'D' : '.',
304 ((*rx_window & 0x08)) ? 'D' : '.',
305 ((*rx_window & 0x10)) ? 'D' : '.',
306 ((*rx_window & 0x20)) ? 'D' : '.',
307 ((*rx_window & 0x40)) ? 'D' : '.',
308 ((*rx_window & 0x80)) ? 'D' : '.');
309
310 if (!*rx_window) {
311 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable downlink slots "
312 "available with current uplink assignment\n");
313 return -EBUSY;
314 }
315
316 /* calculate new min/max */
317 for (uint8_t ts_no = *rx_win_min; ts_no <= *rx_win_max; ts_no++) {
318 if ((*rx_window & (1 << ts_no)))
319 break;
320 *rx_win_min = ts_no + 1;
321 LOGP(DRLCMAC, LOGL_DEBUG, "- TS has been deleted, so "
322 "raising start of DL window to %d\n",
323 *rx_win_min);
324 }
325 for (uint8_t ts_no = *rx_win_max; ts_no >= *rx_win_min; ts_no--) {
326 if ((*rx_window & (1 << ts_no)))
327 break;
328 *rx_win_max = ts_no - 1;
329 LOGP(DRLCMAC, LOGL_DEBUG, "- TS has been deleted, so "
330 "lowering end of DL window to %d\n",
331 *rx_win_max);
332 }
333
334 return 0;
335}
336
Holger Hans Peter Freytherdd4af802013-12-25 21:03:42 +0100337/*
338 * reduce window, to allow at least one uplink TX slot
339 * this is only required for Type 1
340 */
341static uint8_t update_rx_win_max(const int ms_type, const int Tt,
342 const int Tr, uint8_t rx_win_min, uint8_t rx_win_max)
343{
344 if (ms_type != 1)
345 return rx_win_max;
346
347 if (rx_win_max - rx_win_min + 1 + Tt + 1 + Tr > 8) {
348 rx_win_max = rx_win_min + 7 - Tt - 1 - Tr;
349 LOGP(DRLCMAC, LOGL_DEBUG, "- Reduce RX window due to time "
350 "contraints to %d slots\n", rx_win_max - rx_win_min + 1);
351 }
352
353 return rx_win_max;
354}
355
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100356
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200357/* Slot Allocation: Algorithm B
358 *
359 * Assign as many downlink slots as possible.
360 * Assign one uplink slot. (With free USF)
361 *
362 */
363int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
364 struct gprs_rlcmac_tbf *old_tbf,
365 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
366{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200367 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100368 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200369 uint8_t Tta, Ttb, Tra, Trb, Tt, Tr; /* Minimum Number of Slots */
370 uint8_t Type; /* Type of Mobile */
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100371 int rx_window;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200372 uint8_t tx_win_min, tx_win_max, tx_range;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100373 uint8_t tx_window = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200374 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
375 int8_t usf[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* must be signed */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200376 int8_t first_common_ts = -1;
377 uint8_t i, ts;
378 uint8_t slotcount = 0;
379
380
381 if (tbf->ms_class >= 32) {
382 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
383 tbf->ms_class);
384 return -EINVAL;
385 }
386
387 if (tbf->ms_class) {
388 ms_class = &gprs_ms_multislot_class[tbf->ms_class];
389 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
390 "class %d\n", tbf->ms_class);
391 } else {
392 ms_class = &gprs_ms_multislot_class[12];
393 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
394 "unknow class (assuming 12)\n");
395 }
396
397 if (ms_class->tx == MS_NA) {
398 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
399 "applicable.\n", tbf->ms_class);
400 return -EINVAL;
401 }
402
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200403 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200404 Sum = ms_class->sum;
405 Tta = ms_class->ta;
406 Ttb = ms_class->tb;
407 Tra = ms_class->ra;
408 Trb = ms_class->rb;
409 Type = ms_class->type;
410
411 /* Tta and Ttb may depend on hopping or frequency change */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100412 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200413 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100414 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200415 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200416
417 LOGP(DRLCMAC, LOGL_DEBUG, "- Rx=%d Tx=%d Sum Rx+Tx=%s Tta=%s Ttb=%d "
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100418 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200419 (Sum == MS_NA) ? "N/A" : digit[Sum],
420 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
421
422 /* select the values for time contraints */
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200423 /* applicable to type 1 and type 2 */
424 Tt = Ttb;
425 Tr = Trb;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200426
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100427 uint8_t rx_win_min, rx_win_max;
428 rx_window = select_dl_slots(tbf->trx, ms_class->type, ms_class->rx,
429 &rx_win_min, &rx_win_max);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200430
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200431
432 /* reduce window, if existing uplink slots collide RX window */
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100433 int rc = reduce_rx_window(ms_class->type, old_tbf, Tt, Tr,
434 &rx_window, &rx_win_min, &rx_win_max);
435 if (rc < 0)
436 return rc;
Holger Hans Peter Freytherdd4af802013-12-25 21:03:42 +0100437 rx_win_max = update_rx_win_max(ms_class->type, Tt, Tr,
438 rx_win_min, rx_win_max);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200439 LOGP(DRLCMAC, LOGL_DEBUG, "- RX-Window is: %d..%d\n", rx_win_min,
440 rx_win_max);
441
442 /* calculate TX window */
443 if (Type == 1) {
444 /* calculate TX window (shifted by 3 timeslots)
445 * it uses the space between tx_win_max and tx_win_min */
446 tx_win_min = (rx_win_max - 2 + Tt) & 7;
447 tx_win_max = (rx_win_min + 4 - Tr) & 7;
448 /* calculate the TX window size (might be larger than Tx) */
449 tx_range = (tx_win_max - tx_win_min + 1) & 7;
450 } else {
451 /* TX and RX simultaniously */
452 tx_win_min = rx_win_min;
453 tx_win_max = 7;
454 /* TX window size (might be larger than Tx) */
455 tx_range = tx_win_max - tx_win_min + 1;
456 }
457
458 LOGP(DRLCMAC, LOGL_DEBUG, "- TX-Window is: %d..%d\n", tx_win_min,
459 tx_win_max);
460
461 /* select a window of Tx slots if available
462 * The maximum allowed slots depend on TX or the window of available
463 * slots.
464 *
465 * also assign the first common ts, which is used for control or single
466 * slot. */
467 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100468 int tsc = -1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200469 for (ts = tx_win_min, i = 0; i < tx_range; ts = (ts + 1) & 7) {
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100470 struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200471 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200472 /* check if enabled */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200473 if (!pdch->is_enabled()) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200474 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
475 "because not enabled\n", ts);
Holger Hans Peter Freytherdf022f62013-12-25 20:24:16 +0100476 #warning "Why isn't it needed to increase the window?"
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200477 continue;
478 }
479 /* check if TSC changes */
480 if (tsc < 0)
Holger Hans Peter Freyther8f399de2013-12-25 20:22:35 +0100481 tsc = pdch->tsc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200482 else if (tsc != pdch->tsc) {
483 LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of "
484 "TRX=%d, because it has different TSC "
485 "than lower TS of TRX. In order to "
486 "allow multislot, all slots must be "
487 "configured with the same TSC!\n",
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100488 ts, tbf->trx->trx_no);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200489 /* increase window for Type 1 */
Holger Hans Peter Freytherdf022f62013-12-25 20:24:16 +0100490 #warning "Why isn't it needed to check for tx_window"
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200491 if (Type == 1)
492 i++;
493 continue;
494 }
495 /* check for free usf */
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200496 usf[ts] = find_free_usf(pdch);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200497 if (usf[ts] < 0) {
498 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
499 "because no USF available\n", ts);
500 /* increase window for Type 1 */
501 if (Type == 1)
502 i++;
503 continue;
504 }
505
506 if (!tx_window)
507 first_common_ts = ts;
508
509 tx_window |= (1 << ts);
510 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected UL TS %d\n", ts);
511
512 if (1 && Type == 1) { /* FIXME: multislot UL assignment */
513 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
514 "1 slot assigned\n");
515 break;
516 }
517 if (++i == Tx) {
518 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
519 "slots / window reached maximum "
520 "allowed Tx size\n");
521 break;
522 }
523 }
524
525 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected TX window: "
526 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
527 ((tx_window & 0x01)) ? 'U' : '.',
528 ((tx_window & 0x02)) ? 'U' : '.',
529 ((tx_window & 0x04)) ? 'U' : '.',
530 ((tx_window & 0x08)) ? 'U' : '.',
531 ((tx_window & 0x10)) ? 'U' : '.',
532 ((tx_window & 0x20)) ? 'U' : '.',
533 ((tx_window & 0x40)) ? 'U' : '.',
534 ((tx_window & 0x80)) ? 'U' : '.');
535
536 if (!tx_window) {
537 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable uplink slots "
538 "available\n");
539 return -EBUSY;
540 }
541 } else {
542 /* assign the first common ts, which is used for control or
543 * single slot. */
544 for (ts = tx_win_min, i = 0; i < tx_range; ts = (ts + 1) & 7) {
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100545 struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200546 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200547 /* check if enabled */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200548 if (!pdch->is_enabled()) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200549 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
550 "because not enabled\n", ts);
551 continue;
552 }
553 first_common_ts = ts;
554 break;
555 }
556 }
557
558 if (first_common_ts < 0) {
559 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
560 return -EINVAL;
561 }
562
563 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
564 /* assign downlink */
565 if (rx_window == 0) {
566 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
567 "available\n");
568 return -EINVAL;
569 }
570 for (ts = 0; ts < 8; ts++) {
571 if ((rx_window & (1 << ts))) {
572 /* be sure to select a single downlink slots
573 * that can be used for uplink, if multiple
574 * slots are assigned later. */
575 if (single && first_common_ts != ts)
576 continue;
577 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
578 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100579 assign_dlink_tbf(&tbf->trx->pdch[ts], tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200580 slotcount++;
581 if (slotcount == 1)
582 tbf->first_ts = ts;
583 if (single)
584 break;
585 }
586 }
587 } else {
588 /* assign uplink */
589 if (tx_window == 0) {
590 LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots "
591 "available\n");
592 return -EINVAL;
593 }
594 for (ts = 0; ts < 8; ts++) {
595 if ((tx_window & (1 << ts))) {
596 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
597 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100598 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], tbf, usf[ts]);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200599 slotcount++;
600 if (slotcount == 1)
601 tbf->first_ts = ts;
602 if (single)
603 break;
604 }
605 }
606 }
607 if (single && slotcount) {
608 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
609 tbf->first_ts,
610 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
611 } else {
612 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
613 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
614 }
615 if (slotcount == 0)
616 return -EBUSY;
617
618 tbf->first_common_ts = first_common_ts;
619
620 return 0;
621}