blob: c1b73398a5c8e811240f0469a15d353691945397 [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>
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020026#include <gprs_ms.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020027
28#include <errno.h>
Jacob Erlbeckec478752015-06-19 16:35:38 +020029#include <values.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020030
31/* 3GPP TS 05.02 Annex B.1 */
32
33#define MS_NA 255 /* N/A */
34#define MS_A 254 /* 1 with hopping, 0 without */
35#define MS_B 253 /* 1 with hopping, 0 without (change Rx to Tx)*/
36#define MS_C 252 /* 1 with hopping, 0 without (change Tx to Rx)*/
37
38struct gprs_ms_multislot_class {
39 uint8_t rx, tx, sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
40 uint8_t ta, tb, ra, rb; /* Minimum Number of Slots */
41 uint8_t type; /* Type of Mobile */
42};
43
44static const struct gprs_ms_multislot_class gprs_ms_multislot_class[32] = {
45/* M-S Class Rx Tx Sum Tta Ttb Tra Trb Type */
46/* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
47/* 1 */ { 1, 1, 2, 3, 2, 4, 2, 1 },
48/* 2 */ { 2, 1, 3, 3, 2, 3, 1, 1 },
49/* 3 */ { 2, 2, 3, 3, 2, 3, 1, 1 },
50/* 4 */ { 3, 1, 4, 3, 1, 3, 1, 1 },
51/* 5 */ { 2, 2, 4, 3, 1, 3, 1, 1 },
52/* 6 */ { 3, 2, 4, 3, 1, 3, 1, 1 },
53/* 7 */ { 3, 3, 4, 3, 1, 3, 1, 1 },
54/* 8 */ { 4, 1, 5, 3, 1, 2, 1, 1 },
55/* 9 */ { 3, 2, 5, 3, 1, 2, 1, 1 },
56/* 10 */ { 4, 2, 5, 3, 1, 2, 1, 1 },
57/* 11 */ { 4, 3, 5, 3, 1, 2, 1, 1 },
58/* 12 */ { 4, 4, 5, 2, 1, 2, 1, 1 },
59/* 13 */ { 3, 3, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
60/* 14 */ { 4, 4, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
61/* 15 */ { 5, 5, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
62/* 16 */ { 6, 6, MS_NA, MS_NA, MS_A, 2, MS_A, 2 },
63/* 17 */ { 7, 7, MS_NA, MS_NA, MS_A, 1, 0, 2 },
64/* 18 */ { 8, 8, MS_NA, MS_NA, 0, 0, 0, 2 },
65/* 19 */ { 6, 2, MS_NA, 3, MS_B, 2, MS_C, 1 },
66/* 20 */ { 6, 3, MS_NA, 3, MS_B, 2, MS_C, 1 },
67/* 21 */ { 6, 4, MS_NA, 3, MS_B, 2, MS_C, 1 },
68/* 22 */ { 6, 4, MS_NA, 2, MS_B, 2, MS_C, 1 },
69/* 23 */ { 6, 6, MS_NA, 2, MS_B, 2, MS_C, 1 },
70/* 24 */ { 8, 2, MS_NA, 3, MS_B, 2, MS_C, 1 },
71/* 25 */ { 8, 3, MS_NA, 3, MS_B, 2, MS_C, 1 },
72/* 26 */ { 8, 4, MS_NA, 3, MS_B, 2, MS_C, 1 },
73/* 27 */ { 8, 4, MS_NA, 2, MS_B, 2, MS_C, 1 },
74/* 28 */ { 8, 6, MS_NA, 2, MS_B, 2, MS_C, 1 },
75/* 29 */ { 8, 8, MS_NA, 2, MS_B, 2, MS_C, 1 },
76/* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
77/* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
78};
79
Jacob Erlbeckea65c722015-06-22 16:14:23 +020080static unsigned lsb(unsigned x)
81{
82 return x & -x;
83}
84
85static unsigned bitcount(unsigned x)
86{
87 unsigned count = 0;
88 for (count = 0; x; count += 1)
89 x &= x - 1;
90
91 return count;
92}
93
94static char *set_flag_chars(char *buf, uint8_t val, char set_char, char unset_char = 0)
95{
96 int i;
97
98 for (i = 0; i < 8; i += 1, val = val >> 1) {
99 if (val & 1)
100 buf[i] = set_char;
101 else if (unset_char)
102 buf[i] = unset_char;
103 }
104
105 return buf;
106}
107
108static bool test_and_set_bit(uint32_t *bits, size_t elem)
109{
110 bool was_set = bits[elem/32] & (1 << (elem % 32));
111 bits[elem/32] |= (1 << (elem % 32));
112
113 return was_set;
114}
115
Holger Hans Peter Freyther6796ed22013-10-20 16:45:10 +0200116static inline int8_t find_free_usf(struct gprs_rlcmac_pdch *pdch)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200117{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200118 uint8_t usf_map = 0;
Jacob Erlbeck20b7ba72015-06-30 14:34:24 +0200119 uint8_t usf;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200120
Jacob Erlbeck20b7ba72015-06-30 14:34:24 +0200121 usf_map = pdch->assigned_usf();
122 if (usf_map == (1 << 7) - 1)
123 return -1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200124
125 /* look for USF, don't use USF=7 */
126 for (usf = 0; usf < 7; usf++) {
127 if (!(usf_map & (1 << usf)))
128 return usf;
129 }
130
131 return -1;
132}
133
Jacob Erlbeckec478752015-06-19 16:35:38 +0200134static int find_possible_pdchs(struct gprs_rlcmac_trx *trx,
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200135 size_t max_slots,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200136 uint8_t mask, const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200137{
Jacob Erlbeckec478752015-06-19 16:35:38 +0200138 unsigned ts;
139 int valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200140 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +0200141
142 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200143 struct gprs_rlcmac_pdch *pdch;
144
145 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200146 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200147 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
148 "not enabled\n", ts);
149 continue;
150 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200151
152 if (((1 << ts) & mask) == 0) {
153 if (mask_reason)
154 LOGP(DRLCMAC, LOGL_DEBUG,
155 "- Skipping TS %d, because %s\n",
156 ts, mask_reason);
157 continue;
158 }
159
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200160 if (max_slots > 1) {
161 /* check if TSC changes, see TS 45.002, 6.4.2 */
162 if (last_tsc < 0)
163 last_tsc = pdch->tsc;
164 else if (last_tsc != pdch->tsc) {
165 LOGP(DRLCMAC, LOGL_ERROR,
166 "Skipping TS %d of TRX=%d, because it "
167 "has different TSC than lower TS of TRX. "
168 "In order to allow multislot, all "
169 "slots must be configured with the same "
170 "TSC!\n", ts, trx->trx_no);
171 continue;
172 }
173 }
174
Jacob Erlbeckec478752015-06-19 16:35:38 +0200175 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200176 }
177
Jacob Erlbeckec478752015-06-19 16:35:38 +0200178 return valid_ts_set;
179}
180
181static int find_least_busy_pdch(struct gprs_rlcmac_trx *trx,
182 enum gprs_rlcmac_tbf_direction dir,
183 uint8_t mask,
184 int *free_usf = 0)
185{
186 unsigned ts;
187 int min_used = INT_MAX;
188 int min_ts = -1;
189 int min_usf = -1;
190
191 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
192 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
193 int num_tbfs;
194 int usf = -1; /* must be signed */
195
196 if (((1 << ts) & mask) == 0)
197 continue;
198
199 num_tbfs = pdch->num_tbfs(dir);
200 if (num_tbfs < min_used) {
201 /* We have found a candidate */
202 /* Make sure that an USF is available */
203 if (dir == GPRS_RLCMAC_UL_TBF) {
204 usf = find_free_usf(pdch);
205 if (usf < 0) {
206 LOGP(DRLCMAC, LOGL_DEBUG,
207 "- Skipping TS %d, because "
208 "no USF available\n", ts);
209 continue;
210 }
211 }
212 if (min_ts >= 0)
213 LOGP(DRLCMAC, LOGL_DEBUG,
214 "- Skipping TS %d, because "
215 "num TBFs %d > %d\n",
216 min_ts, min_used, num_tbfs);
217 min_used = num_tbfs;
218 min_ts = ts;
219 min_usf = usf;
220 } else {
221 LOGP(DRLCMAC, LOGL_DEBUG,
222 "- Skipping TS %d, because "
223 "num TBFs %d >= %d\n",
224 ts, num_tbfs, min_used);
225 }
226 }
227
228 if (min_ts < 0)
229 return -1;
230
231 if (free_usf)
232 *free_usf = min_usf;
233
234 return min_ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200235}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200236
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200237static int find_least_reserved_pdch(struct gprs_rlcmac_trx *trx,
238 enum gprs_rlcmac_tbf_direction dir,
239 uint8_t mask,
240 int *free_usf = 0)
241{
242 unsigned ts;
243 int min_used = INT_MAX;
244 int min_ts = -1;
245 int min_usf = -1;
246
247 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
248 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
249 int num_tbfs;
250 int usf = -1; /* must be signed */
251
252 if (((1 << ts) & mask) == 0)
253 continue;
254
255 num_tbfs =
256 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
257 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
258
259 if (num_tbfs < min_used) {
260 /* We have found a candidate */
261 /* Make sure that an USF is available */
262 if (dir == GPRS_RLCMAC_UL_TBF) {
263 usf = find_free_usf(pdch);
264 if (usf < 0) {
265 LOGP(DRLCMAC, LOGL_DEBUG,
266 "- Skipping TS %d, because "
267 "no USF available\n", ts);
268 continue;
269 }
270 }
271 if (min_ts >= 0)
272 LOGP(DRLCMAC, LOGL_DEBUG,
273 "- Skipping TS %d, because "
274 "num TBFs %d > %d\n",
275 min_ts, min_used, num_tbfs);
276 min_used = num_tbfs;
277 min_ts = ts;
278 min_usf = usf;
279 } else {
280 LOGP(DRLCMAC, LOGL_DEBUG,
281 "- Skipping TS %d, because "
282 "num TBFs %d >= %d\n",
283 ts, num_tbfs, min_used);
284 }
285 }
286
287 if (min_ts < 0)
288 return -1;
289
290 if (free_usf)
291 *free_usf = min_usf;
292
293 return min_ts;
294}
295
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200296static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
297 struct gprs_rlcmac_tbf *tbf)
298{
299 if (tbf->pdch[pdch->ts_no])
300 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
301
302 tbf->pdch[pdch->ts_no] = pdch;
303 pdch->attach_tbf(tbf);
304}
305
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200306static void assign_uplink_tbf_usf(
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200307 struct gprs_rlcmac_pdch *pdch,
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200308 struct gprs_rlcmac_ul_tbf *tbf, int8_t usf)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200309{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100310 tbf->trx->ul_tbf[tbf->tfi()] = tbf;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200311 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200312 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200313}
314
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200315static void assign_dlink_tbf(
316 struct gprs_rlcmac_pdch *pdch,
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200317 struct gprs_rlcmac_dl_tbf *tbf)
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200318{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100319 tbf->trx->dl_tbf[tbf->tfi()] = tbf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200320 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200321}
322
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200323
324/* Slot Allocation: Algorithm A
325 *
326 * Assign single slot for uplink and downlink
327 */
328int alloc_algorithm_a(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200329 GprsMs *ms,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200330 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
331{
332 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200333 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200334 uint8_t ul_slots, dl_slots;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200335 int usf = -1;
336 int mask = 0xff;
337 const char *mask_reason = NULL;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200338
339 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200340 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200341
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200342 dl_slots = ms->reserved_dl_slots();
343 ul_slots = ms->reserved_ul_slots();
344
345 ts = ms->first_common_ts();
346
347 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200348 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200349 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200350 } else if (dl_slots || ul_slots) {
351 mask_reason = "need to use a reserved common TS";
352 mask = dl_slots & ul_slots;
353 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200354
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200355 mask = find_possible_pdchs(tbf->trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200356 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200357 return -EINVAL;
358
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200359 ts = find_least_reserved_pdch(tbf->trx, tbf->direction, mask, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200360
361 if (ts < 0) {
362 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
363 "to allocate a TS, no USF available\n");
364 return -EBUSY;
365 }
366
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200367 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200368 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200369 struct gprs_rlcmac_ul_tbf *ul_tbf = static_cast<gprs_rlcmac_ul_tbf *>(tbf);
Holger Hans Peter Freyther948a3d62013-09-30 14:10:23 +0200370
Jacob Erlbeckec478752015-06-19 16:35:38 +0200371 if (usf < 0)
372 usf = find_free_usf(pdch);
373
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200374 if (usf < 0) {
375 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
376 "allocating TS=%d, no USF available\n", ts);
377 return -EBUSY;
378 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200379
380 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d USF=%d\n",
381 ts, usf);
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200382 assign_uplink_tbf_usf(pdch, ul_tbf, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200383 } else {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200384 struct gprs_rlcmac_dl_tbf *dl_tbf = static_cast<gprs_rlcmac_dl_tbf *>(tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200385 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d\n", ts);
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200386 assign_dlink_tbf(pdch, dl_tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200387 }
388 /* the only one TS is the common TS */
389 tbf->first_ts = tbf->first_common_ts = ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200390 ms->set_reserved_slots(tbf->trx, 1 << ts, 1 << ts);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200391
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200392 tbf->upgrade_to_multislot = 0;
393
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200394 return 0;
395}
396
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200397static int find_multi_slots(struct gprs_rlcmac_bts *bts,
398 struct gprs_rlcmac_trx *trx,
399 GprsMs *ms, uint8_t *ul_slots, uint8_t *dl_slots)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200400{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200401 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100402 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200403 uint8_t Tta, Ttb, Tra, Trb; /* Minimum Number of Slots */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200404 uint8_t Type; /* Type of Mobile */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200405 int rx_window, tx_window, pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200406 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200407 char slot_info[9] = {0};
408 int max_capacity;
409 uint8_t max_ul_slots;
410 uint8_t max_dl_slots;
411 unsigned max_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200412
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200413 unsigned ul_ts, dl_ts;
414 unsigned num_tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200415
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200416 uint32_t checked_tx[256/32] = {0};
417
418 if (ms->ms_class() >= 32) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200419 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200420 ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200421 return -EINVAL;
422 }
423
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200424 if (ms->ms_class()) {
425 ms_class = &gprs_ms_multislot_class[ms->ms_class()];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200426 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200427 "class %d\n", ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200428 } else {
429 ms_class = &gprs_ms_multislot_class[12];
430 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200431 "unknown class (assuming 12)\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200432 }
433
434 if (ms_class->tx == MS_NA) {
435 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200436 "applicable.\n", ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200437 return -EINVAL;
438 }
439
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200440 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200441 Sum = ms_class->sum;
442 Tta = ms_class->ta;
443 Ttb = ms_class->tb;
444 Tra = ms_class->ra;
445 Trb = ms_class->rb;
446 Type = ms_class->type;
447
448 /* Tta and Ttb may depend on hopping or frequency change */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200449 /* TODO: Set them to 1 */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100450 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200451 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100452 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200453 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200454
455 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 +0100456 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200457 (Sum == MS_NA) ? "N/A" : digit[Sum],
458 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
459
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200460 max_slots = OSMO_MAX(ms_class->rx, ms_class->tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200461
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200462 if (*dl_slots == 0)
463 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200464
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200465 if (*ul_slots == 0)
466 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200467
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200468 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200469
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200470 *dl_slots &= pdch_slots;
471 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200472
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200473 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
474 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
475 *dl_slots, 'D', '.'),
476 *ul_slots, 'U'),
477 *ul_slots & *dl_slots, 'C'));
478
479 /* Check for each UL (TX) slot */
480
481 max_capacity = -1;
482 max_ul_slots = 0;
483 max_dl_slots = 0;
484
485 /* Iterate through possible numbers of TX slots */
486 for (num_tx = 1; num_tx <= ms_class->tx; num_tx += 1) {
487 uint16_t tx_valid_win = (1 << num_tx) - 1;
488
489 uint8_t rx_mask[2]; /* 0: Tt*, 1: Tr* */
490 rx_mask[0] = (0x100 >> OSMO_MAX(Ttb, Tta)) - 1;
491 rx_mask[0] &= ~((1 << (Trb + num_tx)) - 1);
492 rx_mask[0] = rx_mask[0] << 3 | rx_mask[0] >> 5;
493 rx_mask[1] = (0x100 >> Ttb) - 1;
494 rx_mask[1] &= ~((1 << (OSMO_MAX(Trb, Tra) + num_tx)) - 1);
495 rx_mask[1] = rx_mask[1] << 3 | rx_mask[1] >> 5;
496
497 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
498 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
499 unsigned tx_slot_count;
500 int max_rx;
501 uint16_t rx_valid_win;
502 uint32_t checked_rx[256/32] = {0};
503
504 /* Wrap valid window */
505 tx_valid_win = (tx_valid_win | tx_valid_win >> 8) & 0xff;
506
507 tx_window = tx_valid_win;
508
509 /* Filter out unavailable slots */
510 tx_window &= *ul_slots;
511
512 /* Avoid repeated TX combination check */
513 if (test_and_set_bit(checked_tx, tx_window))
514 continue;
515
516 if (!tx_window)
517 continue;
518
519 tx_slot_count = bitcount(tx_window);
520
521 max_rx = OSMO_MIN(ms_class->rx, ms_class->sum - num_tx);
522 rx_valid_win = (1 << max_rx) - 1;
523
524 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
525 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
526 /* Wrap valid window */
527 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
528
529 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
530 for (unsigned m_idx = 0; m_idx < ARRAY_SIZE(rx_mask); m_idx += 1) {
531 unsigned common_slot_count;
532 unsigned req_common_slots;
533 unsigned rx_slot_count;
534 uint16_t rx_bad;
535 uint8_t rx_good;
536 unsigned ts;
537 int capacity;
538
539 /* Filter out bad slots */
540 rx_bad = (uint16_t)(0xff & ~rx_mask[m_idx]) << ul_ts;
541 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xff;
542 rx_good = *dl_slots & ~rx_bad;
543
544 /* TODO: CHECK this calculation -> separate function for unit
545 * testing */
546
547 rx_window = rx_good & rx_valid_win;
548
549 /* Avoid repeated RX combination check */
550 if (test_and_set_bit(checked_rx, rx_window))
551 continue;
552
553 rx_slot_count = bitcount(rx_window);
554
555#if 0
556 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, "
557 "tx=%02x, rx=%02x, mask=%02x, bad=%02x, good=%02x, ul=%02x, dl=%02x\n",
558 tx_slot_count, rx_slot_count,
559 tx_window, rx_window, rx_mask[m_idx], rx_bad, rx_good, *ul_slots, *dl_slots);
560#endif
561
562 if (!rx_good) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200563#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200564 LOGP(DRLCMAC, LOGL_DEBUG,
565 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
566 "no DL slots available\n",
567 set_flag_chars(set_flag_chars(slot_info,
568 rx_bad, 'x', '.'),
569 tx_window, 'U'));
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200570#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200571 continue;
572 }
573
574 if (!rx_window)
575 continue;
576
577 /* Check number of common slots according to TS 54.002, 6.4.2.2 */
578 common_slot_count = bitcount(tx_window & rx_window);
579 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
580 if (ms_class->type == 1)
581 req_common_slots = OSMO_MIN(req_common_slots, 2);
582
583 if (req_common_slots != common_slot_count) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200584#ifdef ENABLE_TS_ALLOC_DEBUG
585 LOGP(DRLCMAC, LOGL_DEBUG,
586 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
587 "invalid number of common TS: %d (expected %d)\n",
588 set_flag_chars(set_flag_chars(set_flag_chars(
589 slot_info,
590 rx_bad, 'x', '.'),
591 rx_window, 'D'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200592 tx_window, 'U'),
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200593 common_slot_count,
594 req_common_slots);
595#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200596 continue;
597 }
598
599 /* Compute capacity */
600 capacity = 0;
601
602 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
603 int c;
604 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
605 if (rx_window & (1 << ts)) {
606 c = 32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF);
Jacob Erlbeck9ae28232015-07-01 12:27:30 +0200607 c = OSMO_MAX(c, 1);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200608 capacity += c;
609 }
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200610 /* Only consider common slots for UL */
611 if (tx_window & rx_window & (1 << ts)) {
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200612 if (find_free_usf(pdch) >= 0) {
613 c = 32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
614 c = OSMO_MAX(c, 1);
615 capacity += c;
616 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200617 }
618 }
619
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200620#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200621 LOGP(DRLCMAC, LOGL_DEBUG,
622 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
623 "capacity = %d\n",
624 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
625 slot_info,
626 rx_bad, 'x', '.'),
627 rx_window, 'D'),
628 tx_window, 'U'),
629 rx_window & tx_window, 'C'),
630 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200631#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200632
633 if (capacity <= max_capacity)
634 continue;
635
636 max_capacity = capacity;
637 max_ul_slots = tx_window;
638 max_dl_slots = rx_window;
639 }}}}
640
641 if (!max_ul_slots || !max_dl_slots) {
642 LOGP(DRLCMAC, LOGL_NOTICE,
643 "No valid UL/DL slot combination found\n");
644 return -EINVAL;
645 }
646
647 *ul_slots = max_ul_slots;
648 *dl_slots = max_dl_slots;
649
650 return 0;
651}
652
653/* Slot Allocation: Algorithm B
654 *
655 * Assign as many downlink slots as possible.
656 * Assign one uplink slot. (With free USF)
657 *
658 */
659int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
660 GprsMs *ms,
661 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
662{
663 uint8_t dl_slots = 0;
664 uint8_t ul_slots = 0;
665 int8_t first_common_ts;
666 uint8_t slotcount = 0;
667 uint8_t avail_count = 0;
668 char slot_info[9] = {0};
669 int ts;
670 int rc;
671
672 if (!ms) {
673 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
674 return -EINVAL;
675 }
676
677 dl_slots = ms->reserved_dl_slots();
678 ul_slots = ms->reserved_ul_slots();
679
680 if (!dl_slots || !ul_slots) {
681 rc = find_multi_slots(bts, tbf->trx, ms, &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100682 if (rc < 0)
683 return rc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200684
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200685 ms->set_reserved_slots(tbf->trx, ul_slots, dl_slots);
686
687 LOGP(DRLCMAC, LOGL_DEBUG,
688 "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
689 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
690 dl_slots, 'D', '.'),
691 ul_slots, 'U'),
692 ul_slots & dl_slots, 'C'));
693 }
694
695 first_common_ts = ms->first_common_ts();
696
697 if (single) {
698 /* Make sure to consider the first common slot only */
699 ul_slots = dl_slots = dl_slots & ul_slots;
700
701 ts = first_common_ts;
702
703 if (ts < 0)
704 ts = find_least_busy_pdch(tbf->trx, tbf->direction,
705 dl_slots & ul_slots, NULL);
706 if (ts < 0)
707 ul_slots = dl_slots = lsb(dl_slots & ul_slots);
708 else
709 ul_slots = dl_slots = (dl_slots & ul_slots) & (1<<ts);
710 } else if (first_common_ts > 0) {
711 /* Make sure to keep the common TBF */
712 uint8_t disable_dl_slots;
713
714 /* Mark all slots below the common TBF, e.g. cTS=4 -> xxx----- */
715 disable_dl_slots = (1 << (first_common_ts - 1)) - 1;
716
717 /* Only disable common slots in that set */
718 disable_dl_slots &= (dl_slots & ul_slots);
719
720 /* Remove them from the uplink set */
721 ul_slots &= ~disable_dl_slots;
722
723 /* The disabled UL slots will not be used again for subsequent
724 * TBF, do not reserve them anymore */
725 if (disable_dl_slots)
726 ms->set_reserved_slots(tbf->trx, ul_slots, dl_slots);
727 }
728
729 if (dl_slots == 0) {
730 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots available\n");
731 return -EINVAL;
732 }
733
734 if (ul_slots == 0) {
735 LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots available\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200736 return -EINVAL;
737 }
738
739 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200740 struct gprs_rlcmac_dl_tbf *dl_tbf = static_cast<gprs_rlcmac_dl_tbf *>(tbf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200741
742 LOGP(DRLCMAC, LOGL_DEBUG,
743 "- Selected DL slots: (TS=0)\"%s\"(TS=7)%s\n",
744 set_flag_chars(set_flag_chars(slot_info,
745 ms->reserved_dl_slots(), 'd', '.'),
746 dl_slots, 'D'),
747 single ? ", single" : "");
748
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200749 /* assign downlink */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200750 if (dl_slots == 0) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200751 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
752 "available\n");
753 return -EINVAL;
754 }
755 for (ts = 0; ts < 8; ts++) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200756 if (!(dl_slots & (1 << ts)))
757 continue;
758
759 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
760 "%d\n", ts);
761 assign_dlink_tbf(&tbf->trx->pdch[ts], dl_tbf);
762 slotcount++;
763 if (slotcount == 1)
764 dl_tbf->first_ts = ts;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200765 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200766 avail_count = bitcount(ms->reserved_dl_slots());
767
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200768 } else {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200769 struct gprs_rlcmac_ul_tbf *ul_tbf = static_cast<gprs_rlcmac_ul_tbf *>(tbf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200770 int free_usf = -1;
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200771
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200772 if (first_common_ts >= 0)
773 ul_slots = 1 << first_common_ts;
774 else
775 ul_slots = ul_slots & dl_slots;
776
777 ts = find_least_busy_pdch(tbf->trx, GPRS_RLCMAC_UL_TBF,
778 ul_slots, &free_usf);
779
780 if (free_usf < 0) {
781 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
782 return -EBUSY;
783 }
784 ul_slots = 1 << ts;
785
786 LOGP(DRLCMAC, LOGL_DEBUG,
787 "- Selected UL slots: (TS=0)\"%s\"(TS=7)%s\n",
788 set_flag_chars(set_flag_chars(slot_info,
789 ms->reserved_ul_slots(), 'u', '.'),
790 ul_slots, 'U'),
791 single ? ", single" : "");
792
793 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], ul_tbf, free_usf);
794 slotcount++;
795 ul_tbf->first_ts = ts;
796
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200797 /* We will stick to that single UL slot, unreserve the others */
798 if (ul_slots != ms->reserved_ul_slots())
799 ms->set_reserved_slots(tbf->trx,
800 ul_slots, ms->reserved_dl_slots());
801
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200802 avail_count = bitcount(ms->reserved_ul_slots());
803#if 0 /* This code assigns multiple slots for UL (and wastes USFs that way) */
804 for (ts = 0; ts < 8; ts++) {
805 if (!(ul_slots & (1 << ts)))
806 continue;
807
808 free_usf = find_free_usf(&tbf->trx->pdch[ts]);
809 if (free_usf < 0) {
810 LOGP(DRLCMAC, LOGL_DEBUG,
811 "- Skipping TS %d, because "
812 "no USF available\n", ts);
813 continue;
814 }
815
816 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
817 "%d\n", ts);
818 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], ul_tbf, free_usf);
819 slotcount++;
820 if (slotcount == 1)
821 ul_tbf->first_ts = ts;
822 }
823#endif
824 }
825
826 if (single && slotcount) {
827 tbf->upgrade_to_multislot = (avail_count > slotcount);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200828 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
829 tbf->first_ts,
830 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
831 } else {
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200832 tbf->upgrade_to_multislot = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200833 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
834 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
835 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200836
837 first_common_ts = ffs(dl_slots & ul_slots) - 1;
838
839 if (first_common_ts < 0) {
840 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
841 return -EINVAL;
842 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200843
844 tbf->first_common_ts = first_common_ts;
845
846 return 0;
847}