blob: eb7f0ecbb09457fe939778a30e898cc8aa8eab12 [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 Freyther02ab4a82013-09-29 07:37:40 +0200255/* Slot Allocation: Algorithm B
256 *
257 * Assign as many downlink slots as possible.
258 * Assign one uplink slot. (With free USF)
259 *
260 */
261int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
262 struct gprs_rlcmac_tbf *old_tbf,
263 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
264{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200265 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100266 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200267 uint8_t Tta, Ttb, Tra, Trb, Tt, Tr; /* Minimum Number of Slots */
268 uint8_t Type; /* Type of Mobile */
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100269 uint8_t rx_window;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200270 uint8_t tx_win_min, tx_win_max, tx_range;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100271 uint8_t tx_window = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200272 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
273 int8_t usf[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* must be signed */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200274 int8_t first_common_ts = -1;
275 uint8_t i, ts;
276 uint8_t slotcount = 0;
277
278
279 if (tbf->ms_class >= 32) {
280 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
281 tbf->ms_class);
282 return -EINVAL;
283 }
284
285 if (tbf->ms_class) {
286 ms_class = &gprs_ms_multislot_class[tbf->ms_class];
287 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
288 "class %d\n", tbf->ms_class);
289 } else {
290 ms_class = &gprs_ms_multislot_class[12];
291 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
292 "unknow class (assuming 12)\n");
293 }
294
295 if (ms_class->tx == MS_NA) {
296 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
297 "applicable.\n", tbf->ms_class);
298 return -EINVAL;
299 }
300
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200301 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200302 Sum = ms_class->sum;
303 Tta = ms_class->ta;
304 Ttb = ms_class->tb;
305 Tra = ms_class->ra;
306 Trb = ms_class->rb;
307 Type = ms_class->type;
308
309 /* Tta and Ttb may depend on hopping or frequency change */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100310 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200311 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100312 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200313 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200314
315 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 +0100316 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200317 (Sum == MS_NA) ? "N/A" : digit[Sum],
318 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
319
320 /* select the values for time contraints */
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200321 /* applicable to type 1 and type 2 */
322 Tt = Ttb;
323 Tr = Trb;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200324
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100325 uint8_t rx_win_min, rx_win_max;
326 rx_window = select_dl_slots(tbf->trx, ms_class->type, ms_class->rx,
327 &rx_win_min, &rx_win_max);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200328
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200329
330 /* reduce window, if existing uplink slots collide RX window */
331 if (Type == 1 && old_tbf && old_tbf->direction == GPRS_RLCMAC_UL_TBF) {
332 uint8_t collide = 0, ul_usage = 0;
333 int j;
334
335 /* calculate mask of colliding slots */
336 for (ts = 0; ts < 8; ts++) {
337 if (old_tbf->pdch[ts]) {
338 ul_usage |= (1 << ts);
339 /* mark bits from TS-t .. TS+r */
340 for (j = ts - Tt; j != ((ts + Tr + 1) & 7);
341 j = (j + 1) & 7)
342 collide |= (1 << j);
343 }
344 }
345 LOGP(DRLCMAC, LOGL_DEBUG, "- Not allowed slots due to existing "
346 "UL allocation: (TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7) "
347 " D=downlink x=not usable\n",
348 ((ul_usage & 0x01)) ? 'D' : ((collide & 0x01))?'x':'.',
349 ((ul_usage & 0x02)) ? 'D' : ((collide & 0x02))?'x':'.',
350 ((ul_usage & 0x04)) ? 'D' : ((collide & 0x04))?'x':'.',
351 ((ul_usage & 0x08)) ? 'D' : ((collide & 0x08))?'x':'.',
352 ((ul_usage & 0x10)) ? 'D' : ((collide & 0x10))?'x':'.',
353 ((ul_usage & 0x20)) ? 'D' : ((collide & 0x20))?'x':'.',
354 ((ul_usage & 0x40)) ? 'D' : ((collide & 0x40))?'x':'.',
355 ((ul_usage & 0x80)) ? 'D' : ((collide & 0x80))?'x':'.');
356
357 /* apply mask to reduce tx_window (shifted by 3 slots) */
358 rx_window &= ~(collide << 3);
359 rx_window &= ~(collide >> 5);
360 LOGP(DRLCMAC, LOGL_DEBUG, "- Remaining slots for RX: "
361 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
362 ((rx_window & 0x01)) ? 'D' : '.',
363 ((rx_window & 0x02)) ? 'D' : '.',
364 ((rx_window & 0x04)) ? 'D' : '.',
365 ((rx_window & 0x08)) ? 'D' : '.',
366 ((rx_window & 0x10)) ? 'D' : '.',
367 ((rx_window & 0x20)) ? 'D' : '.',
368 ((rx_window & 0x40)) ? 'D' : '.',
369 ((rx_window & 0x80)) ? 'D' : '.');
370 if (!rx_window) {
371 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable downlink slots "
372 "available with current uplink assignment\n");
373 return -EBUSY;
374 }
375
376 /* calculate new min/max */
377 for (ts = rx_win_min; ts <= rx_win_max; ts++) {
378 if ((rx_window & (1 << ts)))
379 break;
380 rx_win_min = ts + 1;
381 LOGP(DRLCMAC, LOGL_DEBUG, "- TS has been deleted, so "
382 "raising start of DL window to %d\n",
383 rx_win_min);
384 }
385 for (ts = rx_win_max; ts >= rx_win_min; ts--) {
386 if ((rx_window & (1 << ts)))
387 break;
388 rx_win_max = ts - 1;
389 LOGP(DRLCMAC, LOGL_DEBUG, "- TS has been deleted, so "
390 "lowering end of DL window to %d\n",
391 rx_win_max);
392 }
393 }
394
395 /* reduce window, to allow at least one uplink TX slot
396 * this is only required for Type 1 */
397 if (Type == 1 && rx_win_max - rx_win_min + 1 + Tt + 1 + Tr > 8) {
398 rx_win_max = rx_win_min + 7 - Tt - 1 - Tr;
399 LOGP(DRLCMAC, LOGL_DEBUG, "- Reduce RX window due to time "
400 "contraints to %d slots\n",
401 rx_win_max - rx_win_min + 1);
402 }
403
404 LOGP(DRLCMAC, LOGL_DEBUG, "- RX-Window is: %d..%d\n", rx_win_min,
405 rx_win_max);
406
407 /* calculate TX window */
408 if (Type == 1) {
409 /* calculate TX window (shifted by 3 timeslots)
410 * it uses the space between tx_win_max and tx_win_min */
411 tx_win_min = (rx_win_max - 2 + Tt) & 7;
412 tx_win_max = (rx_win_min + 4 - Tr) & 7;
413 /* calculate the TX window size (might be larger than Tx) */
414 tx_range = (tx_win_max - tx_win_min + 1) & 7;
415 } else {
416 /* TX and RX simultaniously */
417 tx_win_min = rx_win_min;
418 tx_win_max = 7;
419 /* TX window size (might be larger than Tx) */
420 tx_range = tx_win_max - tx_win_min + 1;
421 }
422
423 LOGP(DRLCMAC, LOGL_DEBUG, "- TX-Window is: %d..%d\n", tx_win_min,
424 tx_win_max);
425
426 /* select a window of Tx slots if available
427 * The maximum allowed slots depend on TX or the window of available
428 * slots.
429 *
430 * also assign the first common ts, which is used for control or single
431 * slot. */
432 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100433 int tsc = -1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200434 for (ts = tx_win_min, i = 0; i < tx_range; ts = (ts + 1) & 7) {
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100435 struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200436 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200437 /* check if enabled */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200438 if (!pdch->is_enabled()) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200439 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
440 "because not enabled\n", ts);
Holger Hans Peter Freytherdf022f62013-12-25 20:24:16 +0100441 #warning "Why isn't it needed to increase the window?"
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200442 continue;
443 }
444 /* check if TSC changes */
445 if (tsc < 0)
Holger Hans Peter Freyther8f399de2013-12-25 20:22:35 +0100446 tsc = pdch->tsc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200447 else if (tsc != pdch->tsc) {
448 LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of "
449 "TRX=%d, because it has different TSC "
450 "than lower TS of TRX. In order to "
451 "allow multislot, all slots must be "
452 "configured with the same TSC!\n",
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100453 ts, tbf->trx->trx_no);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200454 /* increase window for Type 1 */
Holger Hans Peter Freytherdf022f62013-12-25 20:24:16 +0100455 #warning "Why isn't it needed to check for tx_window"
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200456 if (Type == 1)
457 i++;
458 continue;
459 }
460 /* check for free usf */
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200461 usf[ts] = find_free_usf(pdch);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200462 if (usf[ts] < 0) {
463 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
464 "because no USF available\n", ts);
465 /* increase window for Type 1 */
466 if (Type == 1)
467 i++;
468 continue;
469 }
470
471 if (!tx_window)
472 first_common_ts = ts;
473
474 tx_window |= (1 << ts);
475 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected UL TS %d\n", ts);
476
477 if (1 && Type == 1) { /* FIXME: multislot UL assignment */
478 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
479 "1 slot assigned\n");
480 break;
481 }
482 if (++i == Tx) {
483 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
484 "slots / window reached maximum "
485 "allowed Tx size\n");
486 break;
487 }
488 }
489
490 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected TX window: "
491 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
492 ((tx_window & 0x01)) ? 'U' : '.',
493 ((tx_window & 0x02)) ? 'U' : '.',
494 ((tx_window & 0x04)) ? 'U' : '.',
495 ((tx_window & 0x08)) ? 'U' : '.',
496 ((tx_window & 0x10)) ? 'U' : '.',
497 ((tx_window & 0x20)) ? 'U' : '.',
498 ((tx_window & 0x40)) ? 'U' : '.',
499 ((tx_window & 0x80)) ? 'U' : '.');
500
501 if (!tx_window) {
502 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable uplink slots "
503 "available\n");
504 return -EBUSY;
505 }
506 } else {
507 /* assign the first common ts, which is used for control or
508 * single slot. */
509 for (ts = tx_win_min, i = 0; i < tx_range; ts = (ts + 1) & 7) {
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100510 struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200511 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200512 /* check if enabled */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200513 if (!pdch->is_enabled()) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200514 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
515 "because not enabled\n", ts);
516 continue;
517 }
518 first_common_ts = ts;
519 break;
520 }
521 }
522
523 if (first_common_ts < 0) {
524 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
525 return -EINVAL;
526 }
527
528 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
529 /* assign downlink */
530 if (rx_window == 0) {
531 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
532 "available\n");
533 return -EINVAL;
534 }
535 for (ts = 0; ts < 8; ts++) {
536 if ((rx_window & (1 << ts))) {
537 /* be sure to select a single downlink slots
538 * that can be used for uplink, if multiple
539 * slots are assigned later. */
540 if (single && first_common_ts != ts)
541 continue;
542 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
543 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100544 assign_dlink_tbf(&tbf->trx->pdch[ts], tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200545 slotcount++;
546 if (slotcount == 1)
547 tbf->first_ts = ts;
548 if (single)
549 break;
550 }
551 }
552 } else {
553 /* assign uplink */
554 if (tx_window == 0) {
555 LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots "
556 "available\n");
557 return -EINVAL;
558 }
559 for (ts = 0; ts < 8; ts++) {
560 if ((tx_window & (1 << ts))) {
561 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
562 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100563 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], tbf, usf[ts]);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200564 slotcount++;
565 if (slotcount == 1)
566 tbf->first_ts = ts;
567 if (single)
568 break;
569 }
570 }
571 }
572 if (single && slotcount) {
573 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
574 tbf->first_ts,
575 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
576 } else {
577 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
578 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
579 }
580 if (slotcount == 0)
581 return -EBUSY;
582
583 tbf->first_common_ts = first_common_ts;
584
585 return 0;
586}