blob: 366732c21ebe958a2d804bc518ae4c7f71f94c29 [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)
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100206 goto inc_window;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100207 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)
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100219 goto inc_window;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100220 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
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100232inc_window:
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100233 if (++rx_window_size == ms_max_rxslots) {
234 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because slots / "
235 "window reached maximum alowed Rx size\n");
236 break;
237 }
Andreas Eversbergccde4c42014-01-04 15:17:22 +0100238 if (ms_type == 1 && rx_window_size == 5) {
239 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because slots / "
240 "window reached maximum supported Rx size of "
241 "this algorithm\n");
242 break;
243 }
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100244 }
245
246 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected slots for RX: "
247 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
248 ((rx_window & 0x01)) ? 'D' : '.',
249 ((rx_window & 0x02)) ? 'D' : '.',
250 ((rx_window & 0x04)) ? 'D' : '.',
251 ((rx_window & 0x08)) ? 'D' : '.',
252 ((rx_window & 0x10)) ? 'D' : '.',
253 ((rx_window & 0x20)) ? 'D' : '.',
254 ((rx_window & 0x40)) ? 'D' : '.',
255 ((rx_window & 0x80)) ? 'D' : '.');
256
257 *out_rx_win_min = rx_win_min;
258 *out_rx_win_max = rx_win_max;
259 return rx_window;
260}
261
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100262static int reduce_rx_window(const int ms_type, const struct gprs_rlcmac_tbf *old_tbf,
263 const int Tt, const int Tr,
264 int *rx_window,
265 uint8_t *rx_win_min, uint8_t *rx_win_max)
266{
267 if (ms_type != 1)
268 return 0;
269 if (!old_tbf)
270 return 0;
271 if (old_tbf->direction != GPRS_RLCMAC_UL_TBF)
272 return 0;
273
274 uint8_t collide = 0, ul_usage = 0;
275
276 /* calculate mask of colliding slots */
277 for (uint8_t ts_no = 0; ts_no < 8; ts_no++) {
278 int j;
279 if (!old_tbf->pdch[ts_no])
280 continue;
281
282 ul_usage |= (1 << ts_no);
283 /* mark bits from TS-t .. TS+r */
284 for (j = ts_no - Tt; j != ((ts_no + Tr + 1) & 7); j = (j + 1) & 7)
285 collide |= (1 << j);
286 }
287
288 LOGP(DRLCMAC, LOGL_DEBUG, "- Not allowed slots due to existing "
289 "UL allocation: (TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7) "
290 " D=downlink x=not usable\n",
291 ((ul_usage & 0x01)) ? 'D' : ((collide & 0x01))?'x':'.',
292 ((ul_usage & 0x02)) ? 'D' : ((collide & 0x02))?'x':'.',
293 ((ul_usage & 0x04)) ? 'D' : ((collide & 0x04))?'x':'.',
294 ((ul_usage & 0x08)) ? 'D' : ((collide & 0x08))?'x':'.',
295 ((ul_usage & 0x10)) ? 'D' : ((collide & 0x10))?'x':'.',
296 ((ul_usage & 0x20)) ? 'D' : ((collide & 0x20))?'x':'.',
297 ((ul_usage & 0x40)) ? 'D' : ((collide & 0x40))?'x':'.',
298 ((ul_usage & 0x80)) ? 'D' : ((collide & 0x80))?'x':'.');
299
300 /*
301 * Uplink/Downlink in GSM is shifted by three timeslots. Make
302 * sure they don't collide.
303 */
304 *rx_window &= ~(collide << 3);
305 *rx_window &= ~(collide >> 5);
306 LOGP(DRLCMAC, LOGL_DEBUG, "- Remaining slots for RX: "
307 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
308 ((*rx_window & 0x01)) ? 'D' : '.',
309 ((*rx_window & 0x02)) ? 'D' : '.',
310 ((*rx_window & 0x04)) ? 'D' : '.',
311 ((*rx_window & 0x08)) ? 'D' : '.',
312 ((*rx_window & 0x10)) ? 'D' : '.',
313 ((*rx_window & 0x20)) ? 'D' : '.',
314 ((*rx_window & 0x40)) ? 'D' : '.',
315 ((*rx_window & 0x80)) ? 'D' : '.');
316
317 if (!*rx_window) {
318 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable downlink slots "
319 "available with current uplink assignment\n");
320 return -EBUSY;
321 }
322
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100323 return 0;
324}
325
326/* shrink range of rx_win_min and rx_win_max */
327static void shrink_rx_window(uint8_t *rx_win_min, uint8_t *rx_win_max, int rx_window)
328{
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100329 /* calculate new min/max */
330 for (uint8_t ts_no = *rx_win_min; ts_no <= *rx_win_max; ts_no++) {
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100331 if ((rx_window & (1 << ts_no)))
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100332 break;
333 *rx_win_min = ts_no + 1;
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100334 LOGP(DRLCMAC, LOGL_DEBUG, "- TS is unused, so "
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100335 "raising start of DL window to %d\n",
336 *rx_win_min);
337 }
338 for (uint8_t ts_no = *rx_win_max; ts_no >= *rx_win_min; ts_no--) {
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100339 if ((rx_window & (1 << ts_no)))
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100340 break;
341 *rx_win_max = ts_no - 1;
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100342 LOGP(DRLCMAC, LOGL_DEBUG, "- TS is unused, so "
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100343 "lowering end of DL window to %d\n",
344 *rx_win_max);
345 }
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100346}
347
Holger Hans Peter Freytherdd4af802013-12-25 21:03:42 +0100348/*
349 * reduce window, to allow at least one uplink TX slot
350 * this is only required for Type 1
351 */
352static uint8_t update_rx_win_max(const int ms_type, const int Tt,
353 const int Tr, uint8_t rx_win_min, uint8_t rx_win_max)
354{
355 if (ms_type != 1)
356 return rx_win_max;
357
358 if (rx_win_max - rx_win_min + 1 + Tt + 1 + Tr > 8) {
359 rx_win_max = rx_win_min + 7 - Tt - 1 - Tr;
360 LOGP(DRLCMAC, LOGL_DEBUG, "- Reduce RX window due to time "
361 "contraints to %d slots\n", rx_win_max - rx_win_min + 1);
362 }
363
364 return rx_win_max;
365}
366
Holger Hans Peter Freyther3fd2ddf2013-12-25 21:11:20 +0100367static void tx_win_from_rx(const int ms_type,
368 uint8_t rx_win_min, uint8_t rx_win_max,
369 int Tt, int Tr,
370 uint8_t *tx_win_min, uint8_t *tx_win_max,
371 uint8_t *tx_range)
372{
373 if (ms_type == 1) {
374 /* calculate TX window (shifted by 3 timeslots)
375 * it uses the space between tx_win_max and tx_win_min */
376 *tx_win_min = (rx_win_max - 2 + Tt) & 7;
377 *tx_win_max = (rx_win_min + 4 - Tr) & 7;
378 } else {
379 /* TX and RX simultaniously */
380 *tx_win_min = rx_win_min;
381 *tx_win_max = 7;
382 }
383
384 *tx_range = (*tx_win_max - *tx_win_min + 1) & 7;
Andreas Eversberg7a16d462014-01-04 15:25:18 +0100385 /* if TX window fills complete range */
386 if (*tx_range == 0)
387 *tx_range = 8;
Holger Hans Peter Freyther3fd2ddf2013-12-25 21:11:20 +0100388 LOGP(DRLCMAC, LOGL_DEBUG, "- TX-Window is: %d..%d\n", *tx_win_min,
389 *tx_win_max);
390}
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100391
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100392/*
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100393 * Select a window of Tx slots if available.
394 * The maximum allowed slots depend on TX or the window of available
395 * slots.
396 */
397static int select_ul_slots(gprs_rlcmac_trx *trx,
398 const int ms_type, const int ms_max_txslots,
399 uint8_t tx_win_min, uint8_t tx_range,
Andreas Eversberg765736d2014-01-04 15:27:31 +0100400 int8_t *usf, int8_t *first_common_ts, uint8_t rx_window)
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100401{
402 int tsc = -1;
403 uint8_t tx_window = 0;
404 int i;
405 uint8_t ts_no;
406
Andreas Eversbergfe2dcc82014-01-04 15:39:54 +0100407 for (ts_no = tx_win_min, i = 0; i < tx_range; ts_no = (ts_no + 1) & 7, i++) {
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100408 gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
409
410 /* check if enabled */
411 if (!pdch->is_enabled()) {
412 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
413 "because not enabled\n", ts_no);
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100414 if (ms_type == 1 && tx_window)
415 goto inc_window;
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100416 continue;
417 }
Andreas Eversberg765736d2014-01-04 15:27:31 +0100418 /* check if used as downlink */
419 if (!(rx_window & (1 << ts_no))) {
420 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
421 "because not a downlink slot\n", ts_no);
422 if (ms_type == 1 && tx_window)
423 goto inc_window;
424 continue;
425 }
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100426 /* check if TSC changes */
427 if (tsc < 0)
428 tsc = pdch->tsc;
429 else if (tsc != pdch->tsc) {
430 LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of "
431 "TRX=%d, because it has different TSC "
432 "than lower TS of TRX. In order to "
433 "allow multislot, all slots must be "
434 "configured with the same TSC!\n",
435 ts_no, trx->trx_no);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100436 if (ms_type == 1)
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100437 goto inc_window;
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100438 continue;
439 }
440 /* check for free usf */
441 usf[ts_no] = find_free_usf(pdch);
442 if (usf[ts_no] < 0) {
443 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
444 "because no USF available\n", ts_no);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100445 if (ms_type == 1)
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100446 goto inc_window;
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100447 continue;
448 }
449
450 if (!tx_window)
451 *first_common_ts = ts_no;
452
453 tx_window |= (1 << ts_no);
454 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected UL TS %d\n", ts_no);
455
Andreas Eversbergb03d4272014-01-04 15:14:19 +0100456inc_window:
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100457 if (1 && ms_type == 1) { /* FIXME: multislot UL assignment */
458 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
459 "1 slot assigned\n");
460 break;
461 }
Andreas Eversberg0a940082014-01-15 13:53:43 +0100462 if (i+1 == ms_max_txslots) {
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100463 LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because "
464 "slots / window reached maximum "
465 "allowed Tx size\n");
466 break;
467 }
468 }
469
470 LOGP(DRLCMAC, LOGL_DEBUG, "- Selected TX window: "
471 "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n",
472 ((tx_window & 0x01)) ? 'U' : '.',
473 ((tx_window & 0x02)) ? 'U' : '.',
474 ((tx_window & 0x04)) ? 'U' : '.',
475 ((tx_window & 0x08)) ? 'U' : '.',
476 ((tx_window & 0x10)) ? 'U' : '.',
477 ((tx_window & 0x20)) ? 'U' : '.',
478 ((tx_window & 0x40)) ? 'U' : '.',
479 ((tx_window & 0x80)) ? 'U' : '.');
480
481 if (!tx_window) {
482 LOGP(DRLCMAC, LOGL_NOTICE, "No suitable uplink slots "
483 "available\n");
484 return -EBUSY;
485 }
486
487 return tx_window;
488}
489
490/*
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100491 * Assign the first common ts, which is used for control or
492 * single slot.
493 */
Andreas Eversberg765736d2014-01-04 15:27:31 +0100494static int select_first_ts(gprs_rlcmac_trx *trx, uint8_t tx_win_min,
495 uint8_t tx_range, uint8_t rx_window)
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100496{
497 uint8_t ts_no;
498 int i;
Andreas Eversbergfe2dcc82014-01-04 15:39:54 +0100499 for (ts_no = tx_win_min, i = 0; i < tx_range; ts_no = (ts_no + 1) & 7, i++) {
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100500 gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
501 /* check if enabled */
502 if (!pdch->is_enabled()) {
503 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
504 "because not enabled\n", ts_no);
505 continue;
506 }
Andreas Eversberg765736d2014-01-04 15:27:31 +0100507 /* check if used as downlink */
508 if (!(rx_window & (1 << ts_no))) {
509 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, "
510 "because not a downlink slot\n", ts_no);
511 continue;
512 }
Holger Hans Peter Freyther1fe69322013-12-26 09:31:31 +0100513 return ts_no;
514 }
515
516 return -1;
517}
518
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200519/* Slot Allocation: Algorithm B
520 *
521 * Assign as many downlink slots as possible.
522 * Assign one uplink slot. (With free USF)
523 *
524 */
525int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
526 struct gprs_rlcmac_tbf *old_tbf,
527 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
528{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200529 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100530 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200531 uint8_t Tta, Ttb, Tra, Trb, Tt, Tr; /* Minimum Number of Slots */
532 uint8_t Type; /* Type of Mobile */
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100533 int rx_window;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200534 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
535 int8_t usf[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* must be signed */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200536 int8_t first_common_ts = -1;
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100537 uint8_t ts;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200538 uint8_t slotcount = 0;
539
540
541 if (tbf->ms_class >= 32) {
542 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
543 tbf->ms_class);
544 return -EINVAL;
545 }
546
547 if (tbf->ms_class) {
548 ms_class = &gprs_ms_multislot_class[tbf->ms_class];
549 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
550 "class %d\n", tbf->ms_class);
551 } else {
552 ms_class = &gprs_ms_multislot_class[12];
553 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
554 "unknow class (assuming 12)\n");
555 }
556
557 if (ms_class->tx == MS_NA) {
558 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
559 "applicable.\n", tbf->ms_class);
560 return -EINVAL;
561 }
562
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200563 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200564 Sum = ms_class->sum;
565 Tta = ms_class->ta;
566 Ttb = ms_class->tb;
567 Tra = ms_class->ra;
568 Trb = ms_class->rb;
569 Type = ms_class->type;
570
571 /* Tta and Ttb may depend on hopping or frequency change */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100572 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200573 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100574 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200575 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200576
577 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 +0100578 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200579 (Sum == MS_NA) ? "N/A" : digit[Sum],
580 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
581
582 /* select the values for time contraints */
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200583 /* applicable to type 1 and type 2 */
584 Tt = Ttb;
585 Tr = Trb;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200586
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100587 uint8_t rx_win_min, rx_win_max;
588 rx_window = select_dl_slots(tbf->trx, ms_class->type, ms_class->rx,
589 &rx_win_min, &rx_win_max);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200590
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200591
592 /* reduce window, if existing uplink slots collide RX window */
Holger Hans Peter Freythere45c19b2013-12-25 20:53:53 +0100593 int rc = reduce_rx_window(ms_class->type, old_tbf, Tt, Tr,
594 &rx_window, &rx_win_min, &rx_win_max);
595 if (rc < 0)
596 return rc;
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100597 shrink_rx_window(&rx_win_min, &rx_win_max, rx_window);
Holger Hans Peter Freytherdd4af802013-12-25 21:03:42 +0100598 rx_win_max = update_rx_win_max(ms_class->type, Tt, Tr,
599 rx_win_min, rx_win_max);
Andreas Eversberg1cd9d882014-01-04 15:42:38 +0100600 shrink_rx_window(&rx_win_min, &rx_win_max, rx_window);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200601 LOGP(DRLCMAC, LOGL_DEBUG, "- RX-Window is: %d..%d\n", rx_win_min,
602 rx_win_max);
603
604 /* calculate TX window */
Holger Hans Peter Freyther3fd2ddf2013-12-25 21:11:20 +0100605 uint8_t tx_win_min, tx_win_max, tx_range;
606 tx_win_from_rx(ms_class->type, rx_win_min, rx_win_max, Tt, Tr,
607 &tx_win_min, &tx_win_max, &tx_range);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200608
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100609 /* select UL slots but in both cases assign first_common_ts */
610 uint8_t tx_window = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200611 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100612 rc = select_ul_slots(tbf->trx, ms_class->type, ms_class->tx,
Andreas Eversberg765736d2014-01-04 15:27:31 +0100613 tx_win_min, tx_range, usf,
614 &first_common_ts, rx_window);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100615 if (rc < 0)
616 return rc;
617 tx_window = rc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200618 } else {
Andreas Eversberg765736d2014-01-04 15:27:31 +0100619 first_common_ts = select_first_ts(tbf->trx, tx_win_min,
620 tx_range, rx_window);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200621 }
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100622 #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 +0200623
624 if (first_common_ts < 0) {
625 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
626 return -EINVAL;
627 }
628
629 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
630 /* assign downlink */
631 if (rx_window == 0) {
632 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
633 "available\n");
634 return -EINVAL;
635 }
636 for (ts = 0; ts < 8; ts++) {
637 if ((rx_window & (1 << ts))) {
638 /* be sure to select a single downlink slots
639 * that can be used for uplink, if multiple
640 * slots are assigned later. */
641 if (single && first_common_ts != ts)
642 continue;
643 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
644 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100645 assign_dlink_tbf(&tbf->trx->pdch[ts], tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200646 slotcount++;
647 if (slotcount == 1)
648 tbf->first_ts = ts;
649 if (single)
650 break;
651 }
652 }
653 } else {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200654 for (ts = 0; ts < 8; ts++) {
655 if ((tx_window & (1 << ts))) {
656 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
657 "%d\n", ts);
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100658 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], tbf, usf[ts]);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200659 slotcount++;
660 if (slotcount == 1)
661 tbf->first_ts = ts;
662 if (single)
663 break;
664 }
665 }
666 }
667 if (single && slotcount) {
668 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
669 tbf->first_ts,
670 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
671 } else {
672 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
673 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
674 }
675 if (slotcount == 0)
676 return -EBUSY;
677
678 tbf->first_common_ts = first_common_ts;
679
680 return 0;
681}