blob: 171c2041f44a60e32618e2725a1a1f2fb2f8dcde [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 Freyther3fd2ddf2013-12-25 21:11:20 +0100356static void tx_win_from_rx(const int ms_type,
357 uint8_t rx_win_min, uint8_t rx_win_max,
358 int Tt, int Tr,
359 uint8_t *tx_win_min, uint8_t *tx_win_max,
360 uint8_t *tx_range)
361{
362 if (ms_type == 1) {
363 /* calculate TX window (shifted by 3 timeslots)
364 * it uses the space between tx_win_max and tx_win_min */
365 *tx_win_min = (rx_win_max - 2 + Tt) & 7;
366 *tx_win_max = (rx_win_min + 4 - Tr) & 7;
367 } else {
368 /* TX and RX simultaniously */
369 *tx_win_min = rx_win_min;
370 *tx_win_max = 7;
371 }
372
373 *tx_range = (*tx_win_max - *tx_win_min + 1) & 7;
374 LOGP(DRLCMAC, LOGL_DEBUG, "- TX-Window is: %d..%d\n", *tx_win_min,
375 *tx_win_max);
376}
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100377
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100378/*
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100379 * Select a window of Tx slots if available.
380 * The maximum allowed slots depend on TX or the window of available
381 * slots.
382 */
383static int select_ul_slots(gprs_rlcmac_trx *trx,
384 const int ms_type, const int ms_max_txslots,
385 uint8_t tx_win_min, uint8_t tx_range,
386 int8_t *usf, int8_t *first_common_ts)
387{
388 int tsc = -1;
389 uint8_t tx_window = 0;
390 int i;
391 uint8_t ts_no;
392
393 for (ts_no = tx_win_min, i = 0; i < tx_range; ts_no = (ts_no + 1) & 7) {
394 gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
395
396 /* check if enabled */
397 if (!pdch->is_enabled()) {
398 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
399 "because not enabled\n", ts_no);
400 #warning "Why isn't it needed to increase the window?"
401 continue;
402 }
403 /* check if TSC changes */
404 if (tsc < 0)
405 tsc = pdch->tsc;
406 else if (tsc != pdch->tsc) {
407 LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of "
408 "TRX=%d, because it has different TSC "
409 "than lower TS of TRX. In order to "
410 "allow multislot, all slots must be "
411 "configured with the same TSC!\n",
412 ts_no, trx->trx_no);
413 /* increase window for Type 1 */
414 #warning "Why isn't it needed to check for tx_window"
415 if (ms_type == 1)
416 i++;
417 continue;
418 }
419 /* check for free usf */
420 usf[ts_no] = find_free_usf(pdch);
421 if (usf[ts_no] < 0) {
422 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
423 "because no USF available\n", ts_no);
424 /* increase window for Type 1 */
425 if (ms_type == 1)
426 i++;
427 continue;
428 }
429
430 if (!tx_window)
431 *first_common_ts = ts_no;
432
433 tx_window |= (1 << ts_no);
434 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected UL TS %d\n", ts_no);
435
436 if (1 && ms_type == 1) { /* FIXME: multislot UL assignment */
437 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
438 "1 slot assigned\n");
439 break;
440 }
441 if (++i == ms_max_txslots) {
442 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
443 "slots / window reached maximum "
444 "allowed Tx size\n");
445 break;
446 }
447 }
448
449 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected TX window: "
450 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
451 ((tx_window & 0x01)) ? 'U' : '.',
452 ((tx_window & 0x02)) ? 'U' : '.',
453 ((tx_window & 0x04)) ? 'U' : '.',
454 ((tx_window & 0x08)) ? 'U' : '.',
455 ((tx_window & 0x10)) ? 'U' : '.',
456 ((tx_window & 0x20)) ? 'U' : '.',
457 ((tx_window & 0x40)) ? 'U' : '.',
458 ((tx_window & 0x80)) ? 'U' : '.');
459
460 if (!tx_window) {
461 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable uplink slots "
462 "available\n");
463 return -EBUSY;
464 }
465
466 return tx_window;
467}
468
469/*
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100470 * Assign the first common ts, which is used for control or
471 * single slot.
472 */
473static int select_first_ts(gprs_rlcmac_trx *trx, uint8_t tx_win_min, uint8_t tx_range)
474{
475 uint8_t ts_no;
476 int i;
477 for (ts_no = tx_win_min, i = 0; i < tx_range; ts_no = (ts_no + 1) & 7) {
478 gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
479 /* check if enabled */
480 if (!pdch->is_enabled()) {
481 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
482 "because not enabled\n", ts_no);
483 continue;
484 }
485 return ts_no;
486 }
487
488 return -1;
489}
490
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200491/* Slot Allocation: Algorithm B
492 *
493 * Assign as many downlink slots as possible.
494 * Assign one uplink slot. (With free USF)
495 *
496 */
497int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
498 struct gprs_rlcmac_tbf *old_tbf,
499 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
500{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200501 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100502 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200503 uint8_t Tta, Ttb, Tra, Trb, Tt, Tr; /* Minimum Number of Slots */
504 uint8_t Type; /* Type of Mobile */
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100505 int rx_window;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200506 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
507 int8_t usf[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* must be signed */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200508 int8_t first_common_ts = -1;
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100509 uint8_t ts;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200510 uint8_t slotcount = 0;
511
512
513 if (tbf->ms_class >= 32) {
514 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
515 tbf->ms_class);
516 return -EINVAL;
517 }
518
519 if (tbf->ms_class) {
520 ms_class = &gprs_ms_multislot_class[tbf->ms_class];
521 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
522 "class %d\n", tbf->ms_class);
523 } else {
524 ms_class = &gprs_ms_multislot_class[12];
525 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
526 "unknow class (assuming 12)\n");
527 }
528
529 if (ms_class->tx == MS_NA) {
530 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
531 "applicable.\n", tbf->ms_class);
532 return -EINVAL;
533 }
534
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200535 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200536 Sum = ms_class->sum;
537 Tta = ms_class->ta;
538 Ttb = ms_class->tb;
539 Tra = ms_class->ra;
540 Trb = ms_class->rb;
541 Type = ms_class->type;
542
543 /* Tta and Ttb may depend on hopping or frequency change */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100544 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200545 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100546 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200547 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200548
549 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 +0100550 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200551 (Sum == MS_NA) ? "N/A" : digit[Sum],
552 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
553
554 /* select the values for time contraints */
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200555 /* applicable to type 1 and type 2 */
556 Tt = Ttb;
557 Tr = Trb;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200558
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100559 uint8_t rx_win_min, rx_win_max;
560 rx_window = select_dl_slots(tbf->trx, ms_class->type, ms_class->rx,
561 &rx_win_min, &rx_win_max);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200562
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200563
564 /* reduce window, if existing uplink slots collide RX window */
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100565 int rc = reduce_rx_window(ms_class->type, old_tbf, Tt, Tr,
566 &rx_window, &rx_win_min, &rx_win_max);
567 if (rc < 0)
568 return rc;
Holger Hans Peter Freytherdd4af802013-12-25 21:03:42 +0100569 rx_win_max = update_rx_win_max(ms_class->type, Tt, Tr,
570 rx_win_min, rx_win_max);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200571 LOGP(DRLCMAC, LOGL_DEBUG, "- RX-Window is: %d..%d\n", rx_win_min,
572 rx_win_max);
573
574 /* calculate TX window */
Holger Hans Peter Freyther3fd2ddf2013-12-25 21:11:20 +0100575 uint8_t tx_win_min, tx_win_max, tx_range;
576 tx_win_from_rx(ms_class->type, rx_win_min, rx_win_max, Tt, Tr,
577 &tx_win_min, &tx_win_max, &tx_range);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200578
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100579 /* select UL slots but in both cases assign first_common_ts */
580 uint8_t tx_window = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200581 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100582 rc = select_ul_slots(tbf->trx, ms_class->type, ms_class->tx,
583 tx_win_min, tx_range, usf, &first_common_ts);
584 if (rc < 0)
585 return rc;
586 tx_window = rc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200587 } else {
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100588 first_common_ts = select_first_ts(tbf->trx, tx_win_min, tx_range);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200589 }
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100590 #warning "first_common_ts might be different if there was no free USF for the new uplink assignment"
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200591
592 if (first_common_ts < 0) {
593 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
594 return -EINVAL;
595 }
596
597 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
598 /* assign downlink */
599 if (rx_window == 0) {
600 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
601 "available\n");
602 return -EINVAL;
603 }
604 for (ts = 0; ts < 8; ts++) {
605 if ((rx_window & (1 << ts))) {
606 /* be sure to select a single downlink slots
607 * that can be used for uplink, if multiple
608 * slots are assigned later. */
609 if (single && first_common_ts != ts)
610 continue;
611 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
612 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100613 assign_dlink_tbf(&tbf->trx->pdch[ts], tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200614 slotcount++;
615 if (slotcount == 1)
616 tbf->first_ts = ts;
617 if (single)
618 break;
619 }
620 }
621 } else {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200622 for (ts = 0; ts < 8; ts++) {
623 if ((tx_window & (1 << ts))) {
624 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
625 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100626 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], tbf, usf[ts]);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200627 slotcount++;
628 if (slotcount == 1)
629 tbf->first_ts = ts;
630 if (single)
631 break;
632 }
633 }
634 }
635 if (single && slotcount) {
636 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
637 tbf->first_ts,
638 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
639 } else {
640 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
641 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
642 }
643 if (slotcount == 0)
644 return -EBUSY;
645
646 tbf->first_common_ts = first_common_ts;
647
648 return 0;
649}