blob: 8c42bb19ea2b09b145a0caaab06d81c24738a7d8 [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 Erlbeckccc34e42015-06-29 13:45:05 +0200237static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
238 struct gprs_rlcmac_tbf *tbf)
239{
240 if (tbf->pdch[pdch->ts_no])
241 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
242
243 tbf->pdch[pdch->ts_no] = pdch;
244 pdch->attach_tbf(tbf);
245}
246
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200247static void assign_uplink_tbf_usf(
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200248 struct gprs_rlcmac_pdch *pdch,
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200249 struct gprs_rlcmac_ul_tbf *tbf, int8_t usf)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200250{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100251 tbf->trx->ul_tbf[tbf->tfi()] = tbf;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200252 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200253 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200254}
255
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200256static void assign_dlink_tbf(
257 struct gprs_rlcmac_pdch *pdch,
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200258 struct gprs_rlcmac_dl_tbf *tbf)
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200259{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100260 tbf->trx->dl_tbf[tbf->tfi()] = tbf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200261 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200262}
263
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200264
265/* Slot Allocation: Algorithm A
266 *
267 * Assign single slot for uplink and downlink
268 */
269int alloc_algorithm_a(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200270 GprsMs *ms,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200271 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
272{
273 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200274 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200275 uint8_t ul_slots, dl_slots;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200276 int usf = -1;
277 int mask = 0xff;
278 const char *mask_reason = NULL;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200279
280 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200281 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200282
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200283 dl_slots = ms->reserved_dl_slots();
284 ul_slots = ms->reserved_ul_slots();
285
286 ts = ms->first_common_ts();
287
288 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200289 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200290 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200291 } else if (dl_slots || ul_slots) {
292 mask_reason = "need to use a reserved common TS";
293 mask = dl_slots & ul_slots;
294 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200295
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200296 mask = find_possible_pdchs(tbf->trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200297 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200298 return -EINVAL;
299
Jacob Erlbeckec478752015-06-19 16:35:38 +0200300 ts = find_least_busy_pdch(tbf->trx, tbf->direction, mask, &usf);
301
302 if (ts < 0) {
303 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
304 "to allocate a TS, no USF available\n");
305 return -EBUSY;
306 }
307
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200308 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200309 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200310 struct gprs_rlcmac_ul_tbf *ul_tbf = static_cast<gprs_rlcmac_ul_tbf *>(tbf);
Holger Hans Peter Freyther948a3d62013-09-30 14:10:23 +0200311
Jacob Erlbeckec478752015-06-19 16:35:38 +0200312 if (usf < 0)
313 usf = find_free_usf(pdch);
314
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200315 if (usf < 0) {
316 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
317 "allocating TS=%d, no USF available\n", ts);
318 return -EBUSY;
319 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200320
321 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d USF=%d\n",
322 ts, usf);
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200323 assign_uplink_tbf_usf(pdch, ul_tbf, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200324 } else {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200325 struct gprs_rlcmac_dl_tbf *dl_tbf = static_cast<gprs_rlcmac_dl_tbf *>(tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200326 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d\n", ts);
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200327 assign_dlink_tbf(pdch, dl_tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200328 }
329 /* the only one TS is the common TS */
330 tbf->first_ts = tbf->first_common_ts = ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200331 ms->set_reserved_slots(tbf->trx, 1 << ts, 1 << ts);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200332
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200333 tbf->upgrade_to_multislot = 0;
334
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200335 return 0;
336}
337
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200338static int find_multi_slots(struct gprs_rlcmac_bts *bts,
339 struct gprs_rlcmac_trx *trx,
340 GprsMs *ms, uint8_t *ul_slots, uint8_t *dl_slots)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200341{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200342 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100343 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200344 uint8_t Tta, Ttb, Tra, Trb; /* Minimum Number of Slots */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200345 uint8_t Type; /* Type of Mobile */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200346 int rx_window, tx_window, pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200347 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200348 char slot_info[9] = {0};
349 int max_capacity;
350 uint8_t max_ul_slots;
351 uint8_t max_dl_slots;
352 unsigned max_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200353
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200354 unsigned ul_ts, dl_ts;
355 unsigned num_tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200356
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200357 uint32_t checked_tx[256/32] = {0};
358
359 if (ms->ms_class() >= 32) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200360 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200361 ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200362 return -EINVAL;
363 }
364
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200365 if (ms->ms_class()) {
366 ms_class = &gprs_ms_multislot_class[ms->ms_class()];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200367 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200368 "class %d\n", ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200369 } else {
370 ms_class = &gprs_ms_multislot_class[12];
371 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200372 "unknown class (assuming 12)\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200373 }
374
375 if (ms_class->tx == MS_NA) {
376 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200377 "applicable.\n", ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200378 return -EINVAL;
379 }
380
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200381 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200382 Sum = ms_class->sum;
383 Tta = ms_class->ta;
384 Ttb = ms_class->tb;
385 Tra = ms_class->ra;
386 Trb = ms_class->rb;
387 Type = ms_class->type;
388
389 /* Tta and Ttb may depend on hopping or frequency change */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200390 /* TODO: Set them to 1 */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100391 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200392 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100393 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200394 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200395
396 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 +0100397 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200398 (Sum == MS_NA) ? "N/A" : digit[Sum],
399 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
400
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200401 max_slots = OSMO_MAX(ms_class->rx, ms_class->tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200402
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200403 if (*dl_slots == 0)
404 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200405
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200406 if (*ul_slots == 0)
407 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200408
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200409 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200410
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200411 *dl_slots &= pdch_slots;
412 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200413
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200414 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
415 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
416 *dl_slots, 'D', '.'),
417 *ul_slots, 'U'),
418 *ul_slots & *dl_slots, 'C'));
419
420 /* Check for each UL (TX) slot */
421
422 max_capacity = -1;
423 max_ul_slots = 0;
424 max_dl_slots = 0;
425
426 /* Iterate through possible numbers of TX slots */
427 for (num_tx = 1; num_tx <= ms_class->tx; num_tx += 1) {
428 uint16_t tx_valid_win = (1 << num_tx) - 1;
429
430 uint8_t rx_mask[2]; /* 0: Tt*, 1: Tr* */
431 rx_mask[0] = (0x100 >> OSMO_MAX(Ttb, Tta)) - 1;
432 rx_mask[0] &= ~((1 << (Trb + num_tx)) - 1);
433 rx_mask[0] = rx_mask[0] << 3 | rx_mask[0] >> 5;
434 rx_mask[1] = (0x100 >> Ttb) - 1;
435 rx_mask[1] &= ~((1 << (OSMO_MAX(Trb, Tra) + num_tx)) - 1);
436 rx_mask[1] = rx_mask[1] << 3 | rx_mask[1] >> 5;
437
438 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
439 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
440 unsigned tx_slot_count;
441 int max_rx;
442 uint16_t rx_valid_win;
443 uint32_t checked_rx[256/32] = {0};
444
445 /* Wrap valid window */
446 tx_valid_win = (tx_valid_win | tx_valid_win >> 8) & 0xff;
447
448 tx_window = tx_valid_win;
449
450 /* Filter out unavailable slots */
451 tx_window &= *ul_slots;
452
453 /* Avoid repeated TX combination check */
454 if (test_and_set_bit(checked_tx, tx_window))
455 continue;
456
457 if (!tx_window)
458 continue;
459
460 tx_slot_count = bitcount(tx_window);
461
462 max_rx = OSMO_MIN(ms_class->rx, ms_class->sum - num_tx);
463 rx_valid_win = (1 << max_rx) - 1;
464
465 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
466 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
467 /* Wrap valid window */
468 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
469
470 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
471 for (unsigned m_idx = 0; m_idx < ARRAY_SIZE(rx_mask); m_idx += 1) {
472 unsigned common_slot_count;
473 unsigned req_common_slots;
474 unsigned rx_slot_count;
475 uint16_t rx_bad;
476 uint8_t rx_good;
477 unsigned ts;
478 int capacity;
479
480 /* Filter out bad slots */
481 rx_bad = (uint16_t)(0xff & ~rx_mask[m_idx]) << ul_ts;
482 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xff;
483 rx_good = *dl_slots & ~rx_bad;
484
485 /* TODO: CHECK this calculation -> separate function for unit
486 * testing */
487
488 rx_window = rx_good & rx_valid_win;
489
490 /* Avoid repeated RX combination check */
491 if (test_and_set_bit(checked_rx, rx_window))
492 continue;
493
494 rx_slot_count = bitcount(rx_window);
495
496#if 0
497 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, "
498 "tx=%02x, rx=%02x, mask=%02x, bad=%02x, good=%02x, ul=%02x, dl=%02x\n",
499 tx_slot_count, rx_slot_count,
500 tx_window, rx_window, rx_mask[m_idx], rx_bad, rx_good, *ul_slots, *dl_slots);
501#endif
502
503 if (!rx_good) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200504#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200505 LOGP(DRLCMAC, LOGL_DEBUG,
506 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
507 "no DL slots available\n",
508 set_flag_chars(set_flag_chars(slot_info,
509 rx_bad, 'x', '.'),
510 tx_window, 'U'));
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200511#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200512 continue;
513 }
514
515 if (!rx_window)
516 continue;
517
518 /* Check number of common slots according to TS 54.002, 6.4.2.2 */
519 common_slot_count = bitcount(tx_window & rx_window);
520 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
521 if (ms_class->type == 1)
522 req_common_slots = OSMO_MIN(req_common_slots, 2);
523
524 if (req_common_slots != common_slot_count) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200525#ifdef ENABLE_TS_ALLOC_DEBUG
526 LOGP(DRLCMAC, LOGL_DEBUG,
527 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
528 "invalid number of common TS: %d (expected %d)\n",
529 set_flag_chars(set_flag_chars(set_flag_chars(
530 slot_info,
531 rx_bad, 'x', '.'),
532 rx_window, 'D'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200533 tx_window, 'U'),
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200534 common_slot_count,
535 req_common_slots);
536#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200537 continue;
538 }
539
540 /* Compute capacity */
541 capacity = 0;
542
543 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
544 int c;
545 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
546 if (rx_window & (1 << ts)) {
547 c = 32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF);
Jacob Erlbeck9ae28232015-07-01 12:27:30 +0200548 c = OSMO_MAX(c, 1);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200549 capacity += c;
550 }
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200551 /* Only consider common slots for UL */
552 if (tx_window & rx_window & (1 << ts)) {
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200553 if (find_free_usf(pdch) >= 0) {
554 c = 32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
555 c = OSMO_MAX(c, 1);
556 capacity += c;
557 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200558 }
559 }
560
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200561#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200562 LOGP(DRLCMAC, LOGL_DEBUG,
563 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
564 "capacity = %d\n",
565 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
566 slot_info,
567 rx_bad, 'x', '.'),
568 rx_window, 'D'),
569 tx_window, 'U'),
570 rx_window & tx_window, 'C'),
571 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200572#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200573
574 if (capacity <= max_capacity)
575 continue;
576
577 max_capacity = capacity;
578 max_ul_slots = tx_window;
579 max_dl_slots = rx_window;
580 }}}}
581
582 if (!max_ul_slots || !max_dl_slots) {
583 LOGP(DRLCMAC, LOGL_NOTICE,
584 "No valid UL/DL slot combination found\n");
585 return -EINVAL;
586 }
587
588 *ul_slots = max_ul_slots;
589 *dl_slots = max_dl_slots;
590
591 return 0;
592}
593
594/* Slot Allocation: Algorithm B
595 *
596 * Assign as many downlink slots as possible.
597 * Assign one uplink slot. (With free USF)
598 *
599 */
600int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
601 GprsMs *ms,
602 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
603{
604 uint8_t dl_slots = 0;
605 uint8_t ul_slots = 0;
606 int8_t first_common_ts;
607 uint8_t slotcount = 0;
608 uint8_t avail_count = 0;
609 char slot_info[9] = {0};
610 int ts;
611 int rc;
612
613 if (!ms) {
614 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
615 return -EINVAL;
616 }
617
618 dl_slots = ms->reserved_dl_slots();
619 ul_slots = ms->reserved_ul_slots();
620
621 if (!dl_slots || !ul_slots) {
622 rc = find_multi_slots(bts, tbf->trx, ms, &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100623 if (rc < 0)
624 return rc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200625
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200626 ms->set_reserved_slots(tbf->trx, ul_slots, dl_slots);
627
628 LOGP(DRLCMAC, LOGL_DEBUG,
629 "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
630 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
631 dl_slots, 'D', '.'),
632 ul_slots, 'U'),
633 ul_slots & dl_slots, 'C'));
634 }
635
636 first_common_ts = ms->first_common_ts();
637
638 if (single) {
639 /* Make sure to consider the first common slot only */
640 ul_slots = dl_slots = dl_slots & ul_slots;
641
642 ts = first_common_ts;
643
644 if (ts < 0)
645 ts = find_least_busy_pdch(tbf->trx, tbf->direction,
646 dl_slots & ul_slots, NULL);
647 if (ts < 0)
648 ul_slots = dl_slots = lsb(dl_slots & ul_slots);
649 else
650 ul_slots = dl_slots = (dl_slots & ul_slots) & (1<<ts);
651 } else if (first_common_ts > 0) {
652 /* Make sure to keep the common TBF */
653 uint8_t disable_dl_slots;
654
655 /* Mark all slots below the common TBF, e.g. cTS=4 -> xxx----- */
656 disable_dl_slots = (1 << (first_common_ts - 1)) - 1;
657
658 /* Only disable common slots in that set */
659 disable_dl_slots &= (dl_slots & ul_slots);
660
661 /* Remove them from the uplink set */
662 ul_slots &= ~disable_dl_slots;
663
664 /* The disabled UL slots will not be used again for subsequent
665 * TBF, do not reserve them anymore */
666 if (disable_dl_slots)
667 ms->set_reserved_slots(tbf->trx, ul_slots, dl_slots);
668 }
669
670 if (dl_slots == 0) {
671 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots available\n");
672 return -EINVAL;
673 }
674
675 if (ul_slots == 0) {
676 LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots available\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200677 return -EINVAL;
678 }
679
680 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200681 struct gprs_rlcmac_dl_tbf *dl_tbf = static_cast<gprs_rlcmac_dl_tbf *>(tbf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200682
683 LOGP(DRLCMAC, LOGL_DEBUG,
684 "- Selected DL slots: (TS=0)\"%s\"(TS=7)%s\n",
685 set_flag_chars(set_flag_chars(slot_info,
686 ms->reserved_dl_slots(), 'd', '.'),
687 dl_slots, 'D'),
688 single ? ", single" : "");
689
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200690 /* assign downlink */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200691 if (dl_slots == 0) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200692 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
693 "available\n");
694 return -EINVAL;
695 }
696 for (ts = 0; ts < 8; ts++) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200697 if (!(dl_slots & (1 << ts)))
698 continue;
699
700 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
701 "%d\n", ts);
702 assign_dlink_tbf(&tbf->trx->pdch[ts], dl_tbf);
703 slotcount++;
704 if (slotcount == 1)
705 dl_tbf->first_ts = ts;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200706 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200707 avail_count = bitcount(ms->reserved_dl_slots());
708
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200709 } else {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200710 struct gprs_rlcmac_ul_tbf *ul_tbf = static_cast<gprs_rlcmac_ul_tbf *>(tbf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200711 int free_usf = -1;
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200712
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200713 if (first_common_ts >= 0)
714 ul_slots = 1 << first_common_ts;
715 else
716 ul_slots = ul_slots & dl_slots;
717
718 ts = find_least_busy_pdch(tbf->trx, GPRS_RLCMAC_UL_TBF,
719 ul_slots, &free_usf);
720
721 if (free_usf < 0) {
722 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
723 return -EBUSY;
724 }
725 ul_slots = 1 << ts;
726
727 LOGP(DRLCMAC, LOGL_DEBUG,
728 "- Selected UL slots: (TS=0)\"%s\"(TS=7)%s\n",
729 set_flag_chars(set_flag_chars(slot_info,
730 ms->reserved_ul_slots(), 'u', '.'),
731 ul_slots, 'U'),
732 single ? ", single" : "");
733
734 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], ul_tbf, free_usf);
735 slotcount++;
736 ul_tbf->first_ts = ts;
737
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200738 /* We will stick to that single UL slot, unreserve the others */
739 if (ul_slots != ms->reserved_ul_slots())
740 ms->set_reserved_slots(tbf->trx,
741 ul_slots, ms->reserved_dl_slots());
742
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200743 avail_count = bitcount(ms->reserved_ul_slots());
744#if 0 /* This code assigns multiple slots for UL (and wastes USFs that way) */
745 for (ts = 0; ts < 8; ts++) {
746 if (!(ul_slots & (1 << ts)))
747 continue;
748
749 free_usf = find_free_usf(&tbf->trx->pdch[ts]);
750 if (free_usf < 0) {
751 LOGP(DRLCMAC, LOGL_DEBUG,
752 "- Skipping TS %d, because "
753 "no USF available\n", ts);
754 continue;
755 }
756
757 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
758 "%d\n", ts);
759 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], ul_tbf, free_usf);
760 slotcount++;
761 if (slotcount == 1)
762 ul_tbf->first_ts = ts;
763 }
764#endif
765 }
766
767 if (single && slotcount) {
768 tbf->upgrade_to_multislot = (avail_count > slotcount);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200769 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
770 tbf->first_ts,
771 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
772 } else {
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200773 tbf->upgrade_to_multislot = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200774 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
775 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
776 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200777
778 first_common_ts = ffs(dl_slots & ul_slots) - 1;
779
780 if (first_common_ts < 0) {
781 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
782 return -EINVAL;
783 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200784
785 tbf->first_common_ts = first_common_ts;
786
787 return 0;
788}