blob: 338362c6b61d12517311ca9aeec8b26c304913c9 [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{
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200118 struct gprs_rlcmac_ul_tbf *tbf;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200119 uint8_t usf_map = 0;
120 uint8_t tfi, usf;
121
122 /* make map of used USF */
123 for (tfi = 0; tfi < 32; tfi++) {
Daniel Willmann17a1d5e2014-05-30 20:21:30 +0200124 tbf = pdch->ul_tbf_by_tfi(tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200125 if (!tbf)
126 continue;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200127 usf_map |= (1 << tbf->m_usf[pdch->ts_no]);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200128 }
129
130 /* look for USF, don't use USF=7 */
131 for (usf = 0; usf < 7; usf++) {
132 if (!(usf_map & (1 << usf)))
133 return usf;
134 }
135
136 return -1;
137}
138
Jacob Erlbeckec478752015-06-19 16:35:38 +0200139static int find_possible_pdchs(struct gprs_rlcmac_trx *trx,
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200140 size_t max_slots,
Jacob Erlbeckec478752015-06-19 16:35:38 +0200141 uint8_t mask, const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200142{
Jacob Erlbeckec478752015-06-19 16:35:38 +0200143 unsigned ts;
144 int valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200145 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +0200146
147 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200148 struct gprs_rlcmac_pdch *pdch;
149
150 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200151 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200152 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
153 "not enabled\n", ts);
154 continue;
155 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200156
157 if (((1 << ts) & mask) == 0) {
158 if (mask_reason)
159 LOGP(DRLCMAC, LOGL_DEBUG,
160 "- Skipping TS %d, because %s\n",
161 ts, mask_reason);
162 continue;
163 }
164
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200165 if (max_slots > 1) {
166 /* check if TSC changes, see TS 45.002, 6.4.2 */
167 if (last_tsc < 0)
168 last_tsc = pdch->tsc;
169 else if (last_tsc != pdch->tsc) {
170 LOGP(DRLCMAC, LOGL_ERROR,
171 "Skipping TS %d of TRX=%d, because it "
172 "has different TSC than lower TS of TRX. "
173 "In order to allow multislot, all "
174 "slots must be configured with the same "
175 "TSC!\n", ts, trx->trx_no);
176 continue;
177 }
178 }
179
Jacob Erlbeckec478752015-06-19 16:35:38 +0200180 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200181 }
182
Jacob Erlbeckec478752015-06-19 16:35:38 +0200183 return valid_ts_set;
184}
185
186static int find_least_busy_pdch(struct gprs_rlcmac_trx *trx,
187 enum gprs_rlcmac_tbf_direction dir,
188 uint8_t mask,
189 int *free_usf = 0)
190{
191 unsigned ts;
192 int min_used = INT_MAX;
193 int min_ts = -1;
194 int min_usf = -1;
195
196 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
197 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
198 int num_tbfs;
199 int usf = -1; /* must be signed */
200
201 if (((1 << ts) & mask) == 0)
202 continue;
203
204 num_tbfs = pdch->num_tbfs(dir);
205 if (num_tbfs < min_used) {
206 /* We have found a candidate */
207 /* Make sure that an USF is available */
208 if (dir == GPRS_RLCMAC_UL_TBF) {
209 usf = find_free_usf(pdch);
210 if (usf < 0) {
211 LOGP(DRLCMAC, LOGL_DEBUG,
212 "- Skipping TS %d, because "
213 "no USF available\n", ts);
214 continue;
215 }
216 }
217 if (min_ts >= 0)
218 LOGP(DRLCMAC, LOGL_DEBUG,
219 "- Skipping TS %d, because "
220 "num TBFs %d > %d\n",
221 min_ts, min_used, num_tbfs);
222 min_used = num_tbfs;
223 min_ts = ts;
224 min_usf = usf;
225 } else {
226 LOGP(DRLCMAC, LOGL_DEBUG,
227 "- Skipping TS %d, because "
228 "num TBFs %d >= %d\n",
229 ts, num_tbfs, min_used);
230 }
231 }
232
233 if (min_ts < 0)
234 return -1;
235
236 if (free_usf)
237 *free_usf = min_usf;
238
239 return min_ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200240}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200241
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200242static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
243 struct gprs_rlcmac_tbf *tbf)
244{
245 if (tbf->pdch[pdch->ts_no])
246 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
247
248 tbf->pdch[pdch->ts_no] = pdch;
249 pdch->attach_tbf(tbf);
250}
251
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200252static void assign_uplink_tbf_usf(
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200253 struct gprs_rlcmac_pdch *pdch,
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200254 struct gprs_rlcmac_ul_tbf *tbf, int8_t usf)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200255{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100256 tbf->trx->ul_tbf[tbf->tfi()] = tbf;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200257 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200258 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200259}
260
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200261static void assign_dlink_tbf(
262 struct gprs_rlcmac_pdch *pdch,
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200263 struct gprs_rlcmac_dl_tbf *tbf)
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200264{
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100265 tbf->trx->dl_tbf[tbf->tfi()] = tbf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200266 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200267}
268
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200269
270/* Slot Allocation: Algorithm A
271 *
272 * Assign single slot for uplink and downlink
273 */
274int alloc_algorithm_a(struct gprs_rlcmac_bts *bts,
Jacob Erlbecke2e004e2015-06-18 17:16:26 +0200275 GprsMs *ms,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200276 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
277{
278 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200279 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200280 uint8_t ul_slots, dl_slots;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200281 int usf = -1;
282 int mask = 0xff;
283 const char *mask_reason = NULL;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200284
285 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200286 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200287
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200288 dl_slots = ms->reserved_dl_slots();
289 ul_slots = ms->reserved_ul_slots();
290
291 ts = ms->first_common_ts();
292
293 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200294 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200295 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200296 } else if (dl_slots || ul_slots) {
297 mask_reason = "need to use a reserved common TS";
298 mask = dl_slots & ul_slots;
299 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200300
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200301 mask = find_possible_pdchs(tbf->trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200302 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200303 return -EINVAL;
304
Jacob Erlbeckec478752015-06-19 16:35:38 +0200305 ts = find_least_busy_pdch(tbf->trx, tbf->direction, mask, &usf);
306
307 if (ts < 0) {
308 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
309 "to allocate a TS, no USF available\n");
310 return -EBUSY;
311 }
312
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200313 pdch = &tbf->trx->pdch[ts];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200314 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200315 struct gprs_rlcmac_ul_tbf *ul_tbf = static_cast<gprs_rlcmac_ul_tbf *>(tbf);
Holger Hans Peter Freyther948a3d62013-09-30 14:10:23 +0200316
Jacob Erlbeckec478752015-06-19 16:35:38 +0200317 if (usf < 0)
318 usf = find_free_usf(pdch);
319
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200320 if (usf < 0) {
321 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
322 "allocating TS=%d, no USF available\n", ts);
323 return -EBUSY;
324 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200325
326 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d USF=%d\n",
327 ts, usf);
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200328 assign_uplink_tbf_usf(pdch, ul_tbf, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200329 } else {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200330 struct gprs_rlcmac_dl_tbf *dl_tbf = static_cast<gprs_rlcmac_dl_tbf *>(tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200331 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d\n", ts);
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200332 assign_dlink_tbf(pdch, dl_tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200333 }
334 /* the only one TS is the common TS */
335 tbf->first_ts = tbf->first_common_ts = ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200336 ms->set_reserved_slots(tbf->trx, 1 << ts, 1 << ts);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200337
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200338 tbf->upgrade_to_multislot = 0;
339
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200340 return 0;
341}
342
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200343static int find_multi_slots(struct gprs_rlcmac_bts *bts,
344 struct gprs_rlcmac_trx *trx,
345 GprsMs *ms, uint8_t *ul_slots, uint8_t *dl_slots)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200346{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200347 const struct gprs_ms_multislot_class *ms_class;
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100348 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200349 uint8_t Tta, Ttb, Tra, Trb; /* Minimum Number of Slots */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200350 uint8_t Type; /* Type of Mobile */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200351 int rx_window, tx_window, pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200352 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200353 char slot_info[9] = {0};
354 int max_capacity;
355 uint8_t max_ul_slots;
356 uint8_t max_dl_slots;
357 unsigned max_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200358
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200359 unsigned ul_ts, dl_ts;
360 unsigned num_tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200361
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200362 uint32_t checked_tx[256/32] = {0};
363
364 if (ms->ms_class() >= 32) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200365 LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n",
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200366 ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200367 return -EINVAL;
368 }
369
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200370 if (ms->ms_class()) {
371 ms_class = &gprs_ms_multislot_class[ms->ms_class()];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200372 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200373 "class %d\n", ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200374 } else {
375 ms_class = &gprs_ms_multislot_class[12];
376 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200377 "unknown class (assuming 12)\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200378 }
379
380 if (ms_class->tx == MS_NA) {
381 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not "
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200382 "applicable.\n", ms->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200383 return -EINVAL;
384 }
385
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200386 Tx = ms_class->tx;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200387 Sum = ms_class->sum;
388 Tta = ms_class->ta;
389 Ttb = ms_class->tb;
390 Tra = ms_class->ra;
391 Trb = ms_class->rb;
392 Type = ms_class->type;
393
394 /* Tta and Ttb may depend on hopping or frequency change */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200395 /* TODO: Set them to 1 */
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100396 if (Ttb == MS_A || Ttb == MS_B)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200397 Ttb = 0;
Holger Hans Peter Freytherf34f3442013-12-25 20:33:37 +0100398 if (Trb == MS_A || Trb == MS_C)
Holger Hans Peter Freyther11a74892013-09-29 08:13:42 +0200399 Trb = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200400
401 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 +0100402 " Tra=%d Trb=%d Type=%d\n", ms_class->rx, Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200403 (Sum == MS_NA) ? "N/A" : digit[Sum],
404 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
405
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200406 max_slots = OSMO_MAX(ms_class->rx, ms_class->tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200407
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200408 if (*dl_slots == 0)
409 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200410
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200411 if (*ul_slots == 0)
412 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200413
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200414 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200415
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200416 *dl_slots &= pdch_slots;
417 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200418
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200419 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
420 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
421 *dl_slots, 'D', '.'),
422 *ul_slots, 'U'),
423 *ul_slots & *dl_slots, 'C'));
424
425 /* Check for each UL (TX) slot */
426
427 max_capacity = -1;
428 max_ul_slots = 0;
429 max_dl_slots = 0;
430
431 /* Iterate through possible numbers of TX slots */
432 for (num_tx = 1; num_tx <= ms_class->tx; num_tx += 1) {
433 uint16_t tx_valid_win = (1 << num_tx) - 1;
434
435 uint8_t rx_mask[2]; /* 0: Tt*, 1: Tr* */
436 rx_mask[0] = (0x100 >> OSMO_MAX(Ttb, Tta)) - 1;
437 rx_mask[0] &= ~((1 << (Trb + num_tx)) - 1);
438 rx_mask[0] = rx_mask[0] << 3 | rx_mask[0] >> 5;
439 rx_mask[1] = (0x100 >> Ttb) - 1;
440 rx_mask[1] &= ~((1 << (OSMO_MAX(Trb, Tra) + num_tx)) - 1);
441 rx_mask[1] = rx_mask[1] << 3 | rx_mask[1] >> 5;
442
443 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
444 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
445 unsigned tx_slot_count;
446 int max_rx;
447 uint16_t rx_valid_win;
448 uint32_t checked_rx[256/32] = {0};
449
450 /* Wrap valid window */
451 tx_valid_win = (tx_valid_win | tx_valid_win >> 8) & 0xff;
452
453 tx_window = tx_valid_win;
454
455 /* Filter out unavailable slots */
456 tx_window &= *ul_slots;
457
458 /* Avoid repeated TX combination check */
459 if (test_and_set_bit(checked_tx, tx_window))
460 continue;
461
462 if (!tx_window)
463 continue;
464
465 tx_slot_count = bitcount(tx_window);
466
467 max_rx = OSMO_MIN(ms_class->rx, ms_class->sum - num_tx);
468 rx_valid_win = (1 << max_rx) - 1;
469
470 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
471 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
472 /* Wrap valid window */
473 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
474
475 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
476 for (unsigned m_idx = 0; m_idx < ARRAY_SIZE(rx_mask); m_idx += 1) {
477 unsigned common_slot_count;
478 unsigned req_common_slots;
479 unsigned rx_slot_count;
480 uint16_t rx_bad;
481 uint8_t rx_good;
482 unsigned ts;
483 int capacity;
484
485 /* Filter out bad slots */
486 rx_bad = (uint16_t)(0xff & ~rx_mask[m_idx]) << ul_ts;
487 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xff;
488 rx_good = *dl_slots & ~rx_bad;
489
490 /* TODO: CHECK this calculation -> separate function for unit
491 * testing */
492
493 rx_window = rx_good & rx_valid_win;
494
495 /* Avoid repeated RX combination check */
496 if (test_and_set_bit(checked_rx, rx_window))
497 continue;
498
499 rx_slot_count = bitcount(rx_window);
500
501#if 0
502 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, "
503 "tx=%02x, rx=%02x, mask=%02x, bad=%02x, good=%02x, ul=%02x, dl=%02x\n",
504 tx_slot_count, rx_slot_count,
505 tx_window, rx_window, rx_mask[m_idx], rx_bad, rx_good, *ul_slots, *dl_slots);
506#endif
507
508 if (!rx_good) {
509 LOGP(DRLCMAC, LOGL_DEBUG,
510 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
511 "no DL slots available\n",
512 set_flag_chars(set_flag_chars(slot_info,
513 rx_bad, 'x', '.'),
514 tx_window, 'U'));
515 continue;
516 }
517
518 if (!rx_window)
519 continue;
520
521 /* Check number of common slots according to TS 54.002, 6.4.2.2 */
522 common_slot_count = bitcount(tx_window & rx_window);
523 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
524 if (ms_class->type == 1)
525 req_common_slots = OSMO_MIN(req_common_slots, 2);
526
527 if (req_common_slots != common_slot_count) {
528 LOGP(DRLCMAC, LOGL_DEBUG,
529 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
530 "invalid number of common TS: %d (expected %d)\n",
531 set_flag_chars(set_flag_chars(set_flag_chars(
532 slot_info,
533 rx_bad, 'x', '.'),
534 rx_window, 'D'),
535 tx_window, 'U'),
536 common_slot_count,
537 req_common_slots);
538 continue;
539 }
540
541 /* Compute capacity */
542 capacity = 0;
543
544 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
545 int c;
546 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
547 if (rx_window & (1 << ts)) {
548 c = 32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF);
Jacob Erlbeck9ae28232015-07-01 12:27:30 +0200549 c = OSMO_MAX(c, 1);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200550 capacity += c;
551 }
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200552 /* Only consider common slots for UL */
553 if (tx_window & rx_window & (1 << ts)) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200554 c = 32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
Jacob Erlbeck9ae28232015-07-01 12:27:30 +0200555 c = OSMO_MAX(c, 1);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200556 capacity += c;
557 }
558 }
559
560 LOGP(DRLCMAC, LOGL_DEBUG,
561 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
562 "capacity = %d\n",
563 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
564 slot_info,
565 rx_bad, 'x', '.'),
566 rx_window, 'D'),
567 tx_window, 'U'),
568 rx_window & tx_window, 'C'),
569 capacity);
570
571 if (capacity <= max_capacity)
572 continue;
573
574 max_capacity = capacity;
575 max_ul_slots = tx_window;
576 max_dl_slots = rx_window;
577 }}}}
578
579 if (!max_ul_slots || !max_dl_slots) {
580 LOGP(DRLCMAC, LOGL_NOTICE,
581 "No valid UL/DL slot combination found\n");
582 return -EINVAL;
583 }
584
585 *ul_slots = max_ul_slots;
586 *dl_slots = max_dl_slots;
587
588 return 0;
589}
590
591/* Slot Allocation: Algorithm B
592 *
593 * Assign as many downlink slots as possible.
594 * Assign one uplink slot. (With free USF)
595 *
596 */
597int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
598 GprsMs *ms,
599 struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single)
600{
601 uint8_t dl_slots = 0;
602 uint8_t ul_slots = 0;
603 int8_t first_common_ts;
604 uint8_t slotcount = 0;
605 uint8_t avail_count = 0;
606 char slot_info[9] = {0};
607 int ts;
608 int rc;
609
610 if (!ms) {
611 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
612 return -EINVAL;
613 }
614
615 dl_slots = ms->reserved_dl_slots();
616 ul_slots = ms->reserved_ul_slots();
617
618 if (!dl_slots || !ul_slots) {
619 rc = find_multi_slots(bts, tbf->trx, ms, &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100620 if (rc < 0)
621 return rc;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200622
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200623 ms->set_reserved_slots(tbf->trx, ul_slots, dl_slots);
624
625 LOGP(DRLCMAC, LOGL_DEBUG,
626 "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
627 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
628 dl_slots, 'D', '.'),
629 ul_slots, 'U'),
630 ul_slots & dl_slots, 'C'));
631 }
632
633 first_common_ts = ms->first_common_ts();
634
635 if (single) {
636 /* Make sure to consider the first common slot only */
637 ul_slots = dl_slots = dl_slots & ul_slots;
638
639 ts = first_common_ts;
640
641 if (ts < 0)
642 ts = find_least_busy_pdch(tbf->trx, tbf->direction,
643 dl_slots & ul_slots, NULL);
644 if (ts < 0)
645 ul_slots = dl_slots = lsb(dl_slots & ul_slots);
646 else
647 ul_slots = dl_slots = (dl_slots & ul_slots) & (1<<ts);
648 } else if (first_common_ts > 0) {
649 /* Make sure to keep the common TBF */
650 uint8_t disable_dl_slots;
651
652 /* Mark all slots below the common TBF, e.g. cTS=4 -> xxx----- */
653 disable_dl_slots = (1 << (first_common_ts - 1)) - 1;
654
655 /* Only disable common slots in that set */
656 disable_dl_slots &= (dl_slots & ul_slots);
657
658 /* Remove them from the uplink set */
659 ul_slots &= ~disable_dl_slots;
660
661 /* The disabled UL slots will not be used again for subsequent
662 * TBF, do not reserve them anymore */
663 if (disable_dl_slots)
664 ms->set_reserved_slots(tbf->trx, ul_slots, dl_slots);
665 }
666
667 if (dl_slots == 0) {
668 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots available\n");
669 return -EINVAL;
670 }
671
672 if (ul_slots == 0) {
673 LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots available\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200674 return -EINVAL;
675 }
676
677 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200678 struct gprs_rlcmac_dl_tbf *dl_tbf = static_cast<gprs_rlcmac_dl_tbf *>(tbf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200679
680 LOGP(DRLCMAC, LOGL_DEBUG,
681 "- Selected DL slots: (TS=0)\"%s\"(TS=7)%s\n",
682 set_flag_chars(set_flag_chars(slot_info,
683 ms->reserved_dl_slots(), 'd', '.'),
684 dl_slots, 'D'),
685 single ? ", single" : "");
686
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200687 /* assign downlink */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200688 if (dl_slots == 0) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200689 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
690 "available\n");
691 return -EINVAL;
692 }
693 for (ts = 0; ts < 8; ts++) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200694 if (!(dl_slots & (1 << ts)))
695 continue;
696
697 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
698 "%d\n", ts);
699 assign_dlink_tbf(&tbf->trx->pdch[ts], dl_tbf);
700 slotcount++;
701 if (slotcount == 1)
702 dl_tbf->first_ts = ts;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200703 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200704 avail_count = bitcount(ms->reserved_dl_slots());
705
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200706 } else {
Daniel Willmanncd44ec42014-08-07 15:04:57 +0200707 struct gprs_rlcmac_ul_tbf *ul_tbf = static_cast<gprs_rlcmac_ul_tbf *>(tbf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200708 int free_usf = -1;
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200709
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200710 if (first_common_ts >= 0)
711 ul_slots = 1 << first_common_ts;
712 else
713 ul_slots = ul_slots & dl_slots;
714
715 ts = find_least_busy_pdch(tbf->trx, GPRS_RLCMAC_UL_TBF,
716 ul_slots, &free_usf);
717
718 if (free_usf < 0) {
719 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
720 return -EBUSY;
721 }
722 ul_slots = 1 << ts;
723
724 LOGP(DRLCMAC, LOGL_DEBUG,
725 "- Selected UL slots: (TS=0)\"%s\"(TS=7)%s\n",
726 set_flag_chars(set_flag_chars(slot_info,
727 ms->reserved_ul_slots(), 'u', '.'),
728 ul_slots, 'U'),
729 single ? ", single" : "");
730
731 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], ul_tbf, free_usf);
732 slotcount++;
733 ul_tbf->first_ts = ts;
734
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200735 /* We will stick to that single UL slot, unreserve the others */
736 if (ul_slots != ms->reserved_ul_slots())
737 ms->set_reserved_slots(tbf->trx,
738 ul_slots, ms->reserved_dl_slots());
739
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200740 avail_count = bitcount(ms->reserved_ul_slots());
741#if 0 /* This code assigns multiple slots for UL (and wastes USFs that way) */
742 for (ts = 0; ts < 8; ts++) {
743 if (!(ul_slots & (1 << ts)))
744 continue;
745
746 free_usf = find_free_usf(&tbf->trx->pdch[ts]);
747 if (free_usf < 0) {
748 LOGP(DRLCMAC, LOGL_DEBUG,
749 "- Skipping TS %d, because "
750 "no USF available\n", ts);
751 continue;
752 }
753
754 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
755 "%d\n", ts);
756 assign_uplink_tbf_usf(&tbf->trx->pdch[ts], ul_tbf, free_usf);
757 slotcount++;
758 if (slotcount == 1)
759 ul_tbf->first_ts = ts;
760 }
761#endif
762 }
763
764 if (single && slotcount) {
765 tbf->upgrade_to_multislot = (avail_count > slotcount);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200766 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
767 tbf->first_ts,
768 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
769 } else {
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200770 tbf->upgrade_to_multislot = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200771 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
772 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
773 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200774
775 first_common_ts = ffs(dl_slots & ul_slots) - 1;
776
777 if (first_common_ts < 0) {
778 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
779 return -EINVAL;
780 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200781
782 tbf->first_common_ts = first_common_ts;
783
784 return 0;
785}