blob: e394a6e407570d2ff294983191b872e6caa724b3 [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>
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010027#include <pcu_utils.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020028
29#include <errno.h>
Jacob Erlbeckec478752015-06-19 16:35:38 +020030#include <values.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020031
Max842d7812017-11-01 18:11:24 +010032extern "C" {
33#include "mslot_class.h"
34}
35
Jacob Erlbeck77da3552015-07-16 18:33:46 +020036/* Consider a PDCH as idle if has at most this number of TBFs assigned to it */
37#define PDCH_IDLE_TBF_THRESH 1
38
Jacob Erlbeckea65c722015-06-22 16:14:23 +020039static char *set_flag_chars(char *buf, uint8_t val, char set_char, char unset_char = 0)
40{
41 int i;
42
43 for (i = 0; i < 8; i += 1, val = val >> 1) {
44 if (val & 1)
45 buf[i] = set_char;
46 else if (unset_char)
47 buf[i] = unset_char;
48 }
49
50 return buf;
51}
52
53static bool test_and_set_bit(uint32_t *bits, size_t elem)
54{
55 bool was_set = bits[elem/32] & (1 << (elem % 32));
56 bits[elem/32] |= (1 << (elem % 32));
57
58 return was_set;
59}
60
Maxa76a7d02018-01-26 11:09:16 +010061static inline int8_t find_free_usf(const struct gprs_rlcmac_pdch *pdch)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020062{
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020063 uint8_t usf_map = 0;
Jacob Erlbeck20b7ba72015-06-30 14:34:24 +020064 uint8_t usf;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020065
Jacob Erlbeck20b7ba72015-06-30 14:34:24 +020066 usf_map = pdch->assigned_usf();
67 if (usf_map == (1 << 7) - 1)
68 return -1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020069
70 /* look for USF, don't use USF=7 */
71 for (usf = 0; usf < 7; usf++) {
72 if (!(usf_map & (1 << usf)))
73 return usf;
74 }
75
76 return -1;
77}
78
Maxa76a7d02018-01-26 11:09:16 +010079static inline int8_t find_free_tfi(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction dir)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +020080{
Maxa76a7d02018-01-26 11:09:16 +010081 uint32_t tfi_map = pdch->assigned_tfi(dir);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +020082 int8_t tfi;
83
Maxd000d802017-09-20 17:55:28 +020084 if (tfi_map == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +020085 return -1;
86
87 /* look for USF, don't use USF=7 */
88 for (tfi = 0; tfi < 32; tfi++) {
89 if (!(tfi_map & (1 << tfi)))
90 return tfi;
91 }
92
93 return -1;
94}
95
Maxa76a7d02018-01-26 11:09:16 +010096static int find_possible_pdchs(const struct gprs_rlcmac_trx *trx, size_t max_slots, uint8_t mask,
97 const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020098{
Jacob Erlbeckec478752015-06-19 16:35:38 +020099 unsigned ts;
100 int valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200101 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +0200102
103 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100104 const struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200105
106 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200107 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200108 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
109 "not enabled\n", ts);
110 continue;
111 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200112
113 if (((1 << ts) & mask) == 0) {
114 if (mask_reason)
115 LOGP(DRLCMAC, LOGL_DEBUG,
116 "- Skipping TS %d, because %s\n",
117 ts, mask_reason);
118 continue;
119 }
120
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200121 if (max_slots > 1) {
122 /* check if TSC changes, see TS 45.002, 6.4.2 */
123 if (last_tsc < 0)
124 last_tsc = pdch->tsc;
125 else if (last_tsc != pdch->tsc) {
126 LOGP(DRLCMAC, LOGL_ERROR,
127 "Skipping TS %d of TRX=%d, because it "
128 "has different TSC than lower TS of TRX. "
129 "In order to allow multislot, all "
130 "slots must be configured with the same "
131 "TSC!\n", ts, trx->trx_no);
132 continue;
133 }
134 }
135
Jacob Erlbeckec478752015-06-19 16:35:38 +0200136 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200137 }
138
Jacob Erlbeckec478752015-06-19 16:35:38 +0200139 return valid_ts_set;
140}
141
Maxa76a7d02018-01-26 11:09:16 +0100142static int compute_usage_by_num_tbfs(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction dir)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200143{
144 return pdch->num_tbfs(dir);
145}
146
Maxa76a7d02018-01-26 11:09:16 +0100147static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200148{
149 return
150 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
151 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
152}
153
Maxa76a7d02018-01-26 11:09:16 +0100154static int compute_usage_for_algo_a(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction dir)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200155{
156 int usage =
157 pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) +
158 pdch->num_tbfs(GPRS_RLCMAC_UL_TBF) +
159 compute_usage_by_reservation(pdch, dir);
160
Maxd000d802017-09-20 17:55:28 +0200161 if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200162 /* No TFI in the opposite direction, avoid it */
163 usage += 32;
164
165 return usage;
166
167}
168
Maxa76a7d02018-01-26 11:09:16 +0100169/*! Return the TS which corresponds to least busy PDCH
170 *
171 * \param[in] trx Pointer to TRX object
172 * \param[in] dir TBF direction
173 * \param[in] mask set of available timeslots
174 * \param[in] fn Function pointer to function which computes number of associated TBFs
175 * \param[out] free_tfi Free TFI
176 * \param[out] free_usf Free USF
177 * \returns TS number or -1 if unable to find
178 */
179static int find_least_busy_pdch(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t mask,
180 int (*fn)(const struct gprs_rlcmac_pdch *, enum gprs_rlcmac_tbf_direction dir),
181 int *free_tfi = 0, int *free_usf = 0)
Jacob Erlbeckec478752015-06-19 16:35:38 +0200182{
183 unsigned ts;
184 int min_used = INT_MAX;
185 int min_ts = -1;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200186 int min_tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200187 int min_usf = -1;
188
189 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100190 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckec478752015-06-19 16:35:38 +0200191 int num_tbfs;
192 int usf = -1; /* must be signed */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200193 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200194
195 if (((1 << ts) & mask) == 0)
196 continue;
197
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200198 num_tbfs = fn(pdch, dir);
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200199
200 if (num_tbfs < min_used) {
201 /* We have found a candidate */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200202 /* Make sure that a TFI is available */
203 if (free_tfi) {
204 tfi = find_free_tfi(pdch, dir);
205 if (tfi < 0) {
206 LOGP(DRLCMAC, LOGL_DEBUG,
207 "- Skipping TS %d, because "
208 "no TFI available\n", ts);
209 continue;
210 }
211 }
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200212 /* Make sure that an USF is available */
213 if (dir == GPRS_RLCMAC_UL_TBF) {
214 usf = find_free_usf(pdch);
215 if (usf < 0) {
216 LOGP(DRLCMAC, LOGL_DEBUG,
217 "- Skipping TS %d, because "
218 "no USF available\n", ts);
219 continue;
220 }
221 }
222 if (min_ts >= 0)
223 LOGP(DRLCMAC, LOGL_DEBUG,
224 "- Skipping TS %d, because "
225 "num TBFs %d > %d\n",
226 min_ts, min_used, num_tbfs);
227 min_used = num_tbfs;
228 min_ts = ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200229 min_tfi = tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200230 min_usf = usf;
231 } else {
232 LOGP(DRLCMAC, LOGL_DEBUG,
233 "- Skipping TS %d, because "
234 "num TBFs %d >= %d\n",
235 ts, num_tbfs, min_used);
236 }
237 }
238
239 if (min_ts < 0)
240 return -1;
241
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200242 if (free_tfi)
243 *free_tfi = min_tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200244 if (free_usf)
245 *free_usf = min_usf;
246
247 return min_ts;
248}
249
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200250static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
251 struct gprs_rlcmac_tbf *tbf)
252{
253 if (tbf->pdch[pdch->ts_no])
254 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
255
256 tbf->pdch[pdch->ts_no] = pdch;
257 pdch->attach_tbf(tbf);
258}
259
Maxa76a7d02018-01-26 11:09:16 +0100260static void assign_uplink_tbf_usf(struct gprs_rlcmac_pdch *pdch, struct gprs_rlcmac_ul_tbf *tbf, uint8_t tfi, int8_t usf)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200261{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200262 tbf->m_tfi = tfi;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200263 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200264 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200265}
266
Maxa76a7d02018-01-26 11:09:16 +0100267static void assign_dlink_tbf(struct gprs_rlcmac_pdch *pdch, struct gprs_rlcmac_dl_tbf *tbf, uint8_t tfi)
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200268{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200269 tbf->m_tfi = tfi;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200270 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200271}
272
Maxa76a7d02018-01-26 11:09:16 +0100273static int find_trx(const struct gprs_rlcmac_bts *bts_data, const GprsMs *ms, int8_t use_trx)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200274{
275 unsigned trx_no;
276 unsigned ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200277
278 /* We must use the TRX currently actively used by an MS */
279 if (ms && ms->current_trx())
280 return ms->current_trx()->trx_no;
281
282 if (use_trx >= 0 && use_trx < 8)
283 return use_trx;
284
285 /* Find the first TRX that has a PDCH with a free UL and DL TFI */
286 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100287 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200288 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100289 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200290 if (!pdch->is_enabled())
291 continue;
292
Maxd000d802017-09-20 17:55:28 +0200293 if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200294 continue;
295
Maxd000d802017-09-20 17:55:28 +0200296 if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200297 continue;
298
299 return trx_no;
300 }
301 }
302
303 return -EBUSY;
304}
305
Maxa76a7d02018-01-26 11:09:16 +0100306static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts_data)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200307{
308 unsigned trx_no;
309 unsigned ts;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200310
311 /* Find the first PDCH with an unused DL TS */
312 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100313 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200314 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100315 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200316 if (!pdch->is_enabled())
317 continue;
318
319 if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH)
320 continue;
321
Maxa76a7d02018-01-26 11:09:16 +0100322 return true;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200323 }
324 }
325
Maxa76a7d02018-01-26 11:09:16 +0100326 return false;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200327}
328
Maxa76a7d02018-01-26 11:09:16 +0100329/*! Return free TFI
330 *
331 * \param[in] bts Pointer to BTS struct
332 * \param[in] trx Pointer to TRX struct
333 * \param[in] ms Pointer to MS object
334 * \param[in] dir DL or UL direction
335 * \param[in] use_trx which TRX to use or -1 if it should be selected based on what MS uses
336 * \param[out] trx_no_ TRX number on which TFI was found
337 * \returns negative error code or 0 on success
338 */
339static int tfi_find_free(const BTS *bts, const gprs_rlcmac_trx *trx, const GprsMs *ms,
340 enum gprs_rlcmac_tbf_direction dir, int8_t use_trx, uint8_t *trx_no_)
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200341{
342 int tfi;
343 uint8_t trx_no;
344
345 if (use_trx == -1 && ms->current_trx())
346 use_trx = ms->current_trx()->trx_no;
347
348 tfi = bts->tfi_find_free(dir, &trx_no, use_trx);
349 if (tfi < 0)
350 return -EBUSY;
351
352 if (trx_no_)
353 *trx_no_ = trx_no;
354
355 return tfi;
356}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200357
Maxe9fe0e32017-09-28 15:56:05 +0200358/*! Slot Allocation: Algorithm A
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200359 *
360 * Assign single slot for uplink and downlink
Maxe9fe0e32017-09-28 15:56:05 +0200361 *
362 * \param[in,out] bts Pointer to BTS struct
363 * \param[in,out] ms_ Pointer to MS object
364 * \param[in,out] tbf_ Pointer to TBF struct
365 * \param[in] single flag indicating if we should force single-slot allocation
366 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
367 * \returns negative error code or 0 on success
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200368 */
Maxe9fe0e32017-09-28 15:56:05 +0200369int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
370 int8_t use_trx)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200371{
372 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200373 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200374 uint8_t ul_slots, dl_slots;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200375 int trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200376 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200377 int usf = -1;
378 int mask = 0xff;
379 const char *mask_reason = NULL;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200380 const GprsMs *ms = ms_;
381 const gprs_rlcmac_tbf *tbf = tbf_;
382 gprs_rlcmac_trx *trx = ms->current_trx();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200383
384 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200385 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200386
Maxa76a7d02018-01-26 11:09:16 +0100387 trx_no = find_trx(bts, ms, use_trx);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200388 if (trx_no < 0) {
389 LOGP(DRLCMAC, LOGL_NOTICE,
390 "- Failed to find a usable TRX (TFI exhausted)\n");
391 return trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200392 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200393 if (!trx)
394 trx = &bts->trx[trx_no];
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200395
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200396 dl_slots = ms->reserved_dl_slots();
397 ul_slots = ms->reserved_ul_slots();
398
399 ts = ms->first_common_ts();
400
401 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200402 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200403 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200404 } else if (dl_slots || ul_slots) {
405 mask_reason = "need to use a reserved common TS";
406 mask = dl_slots & ul_slots;
407 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200408
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200409 mask = find_possible_pdchs(trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200410 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200411 return -EINVAL;
412
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200413 ts = find_least_busy_pdch(trx, tbf->direction, mask,
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200414 compute_usage_for_algo_a,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200415 &tfi, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200416
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200417 if (tbf->direction == GPRS_RLCMAC_UL_TBF && usf < 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200418 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200419 "to allocate a TS, no USF available\n");
Jacob Erlbeckec478752015-06-19 16:35:38 +0200420 return -EBUSY;
421 }
422
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200423 if (ts < 0) {
424 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
425 "to allocate a TS, no TFI available\n");
426 return -EBUSY;
427 }
428
429 pdch = &trx->pdch[ts];
430
431 /* The allocation will be successful, so the system state and tbf_/ms_
432 * may be modified from now on. */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200433 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100434 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200435 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d TFI=%d USF=%d\n",
436 ts, tfi, usf);
437 assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200438 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100439 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200440 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d TFI=%d\n",
441 ts, tfi);
442 assign_dlink_tbf(pdch, dl_tbf, tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200443 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200444
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200445 tbf_->trx = trx;
446 /* the only one TS is the common TS */
447 tbf_->first_ts = tbf_->first_common_ts = ts;
448 ms_->set_reserved_slots(trx, 1 << ts, 1 << ts);
449
450 tbf_->upgrade_to_multislot = 0;
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200451 bts->bts->tbf_alloc_algo_a();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200452 return 0;
453}
454
Maxa76a7d02018-01-26 11:09:16 +0100455/*! Find set of slots available for allocation while taking MS class into account
456 *
457 * \param[in] trx Pointer to TRX object
458 * \param[in] mslot_class The multislot class
459 * \param[in,out] ul_slots set of UL timeslots
460 * \param[in,out] dl_slots set of DL timeslots
461 * \returns negative error code or 0 on success
462 */
Max46fbfce2017-11-01 19:22:25 +0100463int find_multi_slots(struct gprs_rlcmac_trx *trx, uint8_t mslot_class, uint8_t *ul_slots, uint8_t *dl_slots)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200464{
Holger Hans Peter Freyther882fc9b2013-12-25 20:34:26 +0100465 uint8_t Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200466 uint8_t Tta, Ttb, Tra, Trb; /* Minimum Number of Slots */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200467 uint8_t Type; /* Type of Mobile */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200468 int rx_window, tx_window, pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200469 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200470 char slot_info[9] = {0};
471 int max_capacity;
472 uint8_t max_ul_slots;
473 uint8_t max_dl_slots;
474 unsigned max_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200475
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200476 unsigned ul_ts, dl_ts;
477 unsigned num_tx;
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200478 enum {MASK_TT, MASK_TR};
479 unsigned mask_sel;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200480
Max842d7812017-11-01 18:11:24 +0100481 if (mslot_class)
482 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n",
483 mslot_class);
484
485 Tx = mslot_class_get_tx(mslot_class);
486 Sum = mslot_class_get_sum(mslot_class);
487 Tta = mslot_class_get_ta(mslot_class);
488 Ttb = mslot_class_get_tb(mslot_class);
Max01bd0cc2018-01-23 20:58:49 +0100489
490 /* FIXME: use actual TA offset for computation - make sure to adjust "1 + MS_TO" accordingly
491 see also "Offset required" bit in 3GPP TS 24.008 §10.5.1.7 */
492 Tra = mslot_class_get_ra(mslot_class, 0);
493 Trb = mslot_class_get_rb(mslot_class, 0);
494
Max842d7812017-11-01 18:11:24 +0100495 Type = mslot_class_get_type(mslot_class);
496
497 if (Tx == MS_NA) {
498 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n",
499 mslot_class);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200500 return -EINVAL;
501 }
502
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200503 LOGP(DRLCMAC, LOGL_DEBUG, "- Rx=%d Tx=%d Sum Rx+Tx=%s Tta=%s Ttb=%d "
Max842d7812017-11-01 18:11:24 +0100504 " Tra=%d Trb=%d Type=%d\n", mslot_class_get_rx(mslot_class), Tx,
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200505 (Sum == MS_NA) ? "N/A" : digit[Sum],
506 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
507
Max842d7812017-11-01 18:11:24 +0100508 max_slots = OSMO_MAX(mslot_class_get_rx(mslot_class), Tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200509
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200510 if (*dl_slots == 0)
511 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200512
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200513 if (*ul_slots == 0)
514 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200515
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200516 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200517
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200518 *dl_slots &= pdch_slots;
519 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200520
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200521 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
522 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
523 *dl_slots, 'D', '.'),
524 *ul_slots, 'U'),
525 *ul_slots & *dl_slots, 'C'));
526
527 /* Check for each UL (TX) slot */
528
529 max_capacity = -1;
530 max_ul_slots = 0;
531 max_dl_slots = 0;
532
533 /* Iterate through possible numbers of TX slots */
Max842d7812017-11-01 18:11:24 +0100534 for (num_tx = 1; num_tx <= mslot_class_get_tx(mslot_class); num_tx += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200535 uint16_t tx_valid_win = (1 << num_tx) - 1;
536
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200537 uint8_t rx_mask[MASK_TR+1];
Max842d7812017-11-01 18:11:24 +0100538 if (Type == 1) {
Jacob Erlbeckdd08ac82015-07-09 12:06:56 +0200539 rx_mask[MASK_TT] = (0x100 >> OSMO_MAX(Ttb, Tta)) - 1;
540 rx_mask[MASK_TT] &= ~((1 << (Trb + num_tx)) - 1);
541 rx_mask[MASK_TR] = (0x100 >> Ttb) - 1;
542 rx_mask[MASK_TR] &=
543 ~((1 << (OSMO_MAX(Trb, Tra) + num_tx)) - 1);
544 } else {
545 /* Class type 2 MS have independant RX and TX */
546 rx_mask[MASK_TT] = 0xff;
547 rx_mask[MASK_TR] = 0xff;
548 }
549
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200550 rx_mask[MASK_TT] = (rx_mask[MASK_TT] << 3) | (rx_mask[MASK_TT] >> 5);
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200551 rx_mask[MASK_TR] = (rx_mask[MASK_TR] << 3) | (rx_mask[MASK_TR] >> 5);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200552
553 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
554 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
555 unsigned tx_slot_count;
556 int max_rx;
557 uint16_t rx_valid_win;
558 uint32_t checked_rx[256/32] = {0};
559
560 /* Wrap valid window */
561 tx_valid_win = (tx_valid_win | tx_valid_win >> 8) & 0xff;
562
563 tx_window = tx_valid_win;
564
565 /* Filter out unavailable slots */
566 tx_window &= *ul_slots;
567
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200568 /* Skip if the the first TS (ul_ts) is not in the set */
569 if ((tx_window & (1 << ul_ts)) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200570 continue;
571
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200572 /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */
573 if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200574 continue;
575
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100576 tx_slot_count = pcu_bitcount(tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200577
Max842d7812017-11-01 18:11:24 +0100578 max_rx = OSMO_MIN(mslot_class_get_rx(mslot_class), Sum - num_tx);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200579 rx_valid_win = (1 << max_rx) - 1;
580
581 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
582 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
583 /* Wrap valid window */
584 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
585
586 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200587 for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200588 unsigned common_slot_count;
589 unsigned req_common_slots;
590 unsigned rx_slot_count;
591 uint16_t rx_bad;
592 uint8_t rx_good;
593 unsigned ts;
594 int capacity;
595
596 /* Filter out bad slots */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200597 rx_bad = (uint16_t)(0xff & ~rx_mask[mask_sel]) << ul_ts;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200598 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xff;
599 rx_good = *dl_slots & ~rx_bad;
600
601 /* TODO: CHECK this calculation -> separate function for unit
602 * testing */
603
604 rx_window = rx_good & rx_valid_win;
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100605 rx_slot_count = pcu_bitcount(rx_window);
Jacob Erlbeckbae33a72015-07-06 14:55:13 +0200606
607#if 0
608 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, mask_sel=%d, "
609 "tx=%02x, rx=%02x, mask=%02x, bad=%02x, good=%02x, "
610 "ul=%02x, dl=%02x\n",
611 tx_slot_count, rx_slot_count, mask_sel,
612 tx_window, rx_window, rx_mask[mask_sel], rx_bad, rx_good,
613 *ul_slots, *dl_slots);
614#endif
615
616 /* Check compliance with TS 45.002, table 6.4.2.2.1 */
617 /* Whether to skip this round doesn not only depend on the bit
618 * sets but also on mask_sel. Therefore this check must be done
619 * before doing the test_and_set_bit shortcut. */
Max842d7812017-11-01 18:11:24 +0100620 if (Type == 1) {
Jacob Erlbeckbae33a72015-07-06 14:55:13 +0200621 unsigned slot_sum = rx_slot_count + tx_slot_count;
622 /* Assume down+up/dynamic.
623 * TODO: For ext-dynamic, down only, up only add more
624 * cases.
625 */
626 if (slot_sum <= 6 && tx_slot_count < 3) {
627 if (mask_sel != MASK_TR)
628 /* Skip Tta */
629 continue;
630 } else if (slot_sum > 6 && tx_slot_count < 3) {
631 if (mask_sel != MASK_TT)
632 /* Skip Tra */
633 continue;
634 } else {
635 /* No supported row in table 6.4.2.2.1. */
636#ifdef ENABLE_TS_ALLOC_DEBUG
637 LOGP(DRLCMAC, LOGL_DEBUG,
638 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
639 "combination not supported\n",
640 set_flag_chars(set_flag_chars(set_flag_chars(
641 slot_info,
642 rx_bad, 'x', '.'),
643 rx_window, 'D'),
644 tx_window, 'U'));
645#endif
646 continue;
647 }
648 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200649
650 /* Avoid repeated RX combination check */
651 if (test_and_set_bit(checked_rx, rx_window))
652 continue;
653
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200654 if (!rx_good) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200655#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200656 LOGP(DRLCMAC, LOGL_DEBUG,
657 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
658 "no DL slots available\n",
659 set_flag_chars(set_flag_chars(slot_info,
660 rx_bad, 'x', '.'),
661 tx_window, 'U'));
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200662#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200663 continue;
664 }
665
666 if (!rx_window)
667 continue;
668
669 /* Check number of common slots according to TS 54.002, 6.4.2.2 */
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100670 common_slot_count = pcu_bitcount(tx_window & rx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200671 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
Max842d7812017-11-01 18:11:24 +0100672 if (Type == 1)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200673 req_common_slots = OSMO_MIN(req_common_slots, 2);
674
675 if (req_common_slots != common_slot_count) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200676#ifdef ENABLE_TS_ALLOC_DEBUG
677 LOGP(DRLCMAC, LOGL_DEBUG,
678 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
679 "invalid number of common TS: %d (expected %d)\n",
680 set_flag_chars(set_flag_chars(set_flag_chars(
681 slot_info,
682 rx_bad, 'x', '.'),
683 rx_window, 'D'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200684 tx_window, 'U'),
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200685 common_slot_count,
686 req_common_slots);
687#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200688 continue;
689 }
690
691 /* Compute capacity */
692 capacity = 0;
693
694 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
695 int c;
Maxa76a7d02018-01-26 11:09:16 +0100696 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200697 if (rx_window & (1 << ts)) {
698 c = 32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF);
Jacob Erlbeck9ae28232015-07-01 12:27:30 +0200699 c = OSMO_MAX(c, 1);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200700 capacity += c;
701 }
Jacob Erlbecked46afd2015-07-01 12:19:40 +0200702 /* Only consider common slots for UL */
703 if (tx_window & rx_window & (1 << ts)) {
Jacob Erlbeck16c6ecc2015-06-30 13:40:18 +0200704 if (find_free_usf(pdch) >= 0) {
705 c = 32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
706 c = OSMO_MAX(c, 1);
707 capacity += c;
708 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200709 }
710 }
711
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200712#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200713 LOGP(DRLCMAC, LOGL_DEBUG,
714 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
715 "capacity = %d\n",
716 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
717 slot_info,
718 rx_bad, 'x', '.'),
719 rx_window, 'D'),
720 tx_window, 'U'),
721 rx_window & tx_window, 'C'),
722 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200723#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200724
725 if (capacity <= max_capacity)
726 continue;
727
728 max_capacity = capacity;
729 max_ul_slots = tx_window;
730 max_dl_slots = rx_window;
731 }}}}
732
733 if (!max_ul_slots || !max_dl_slots) {
734 LOGP(DRLCMAC, LOGL_NOTICE,
735 "No valid UL/DL slot combination found\n");
736 return -EINVAL;
737 }
738
739 *ul_slots = max_ul_slots;
740 *dl_slots = max_dl_slots;
741
742 return 0;
743}
744
Maxe9fe0e32017-09-28 15:56:05 +0200745/*! Slot Allocation: Algorithm B
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200746 *
747 * Assign as many downlink slots as possible.
748 * Assign one uplink slot. (With free USF)
749 *
Maxe9fe0e32017-09-28 15:56:05 +0200750 * \param[in,out] bts Pointer to BTS struct
751 * \param[in,out] ms_ Pointer to MS object
752 * \param[in,out] tbf_ Pointer to TBF struct
753 * \param[in] single flag indicating if we should force single-slot allocation
754 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
755 * \returns negative error code or 0 on success
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200756 */
Maxe9fe0e32017-09-28 15:56:05 +0200757int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
758 int8_t use_trx)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200759{
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200760 uint8_t dl_slots;
761 uint8_t ul_slots;
762 uint8_t reserved_dl_slots;
763 uint8_t reserved_ul_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200764 int8_t first_common_ts;
765 uint8_t slotcount = 0;
Maxa76a7d02018-01-26 11:09:16 +0100766 uint8_t avail_count = 0, trx_no;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200767 char slot_info[9] = {0};
768 int ts;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200769 int first_ts = -1;
770 int usf[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200771 int rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200772 int tfi;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200773 const GprsMs *ms = ms_;
774 const gprs_rlcmac_tbf *tbf = tbf_;
775 gprs_rlcmac_trx *trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200776
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200777 /* Step 1: Get current state from the MS object */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200778
779 if (!ms) {
780 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
781 return -EINVAL;
782 }
783
Max92e9c172017-09-28 16:25:25 +0200784 dl_slots = ms->reserved_dl_slots();
785 ul_slots = ms->reserved_ul_slots();
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200786 first_common_ts = ms->first_common_ts();
787 trx = ms->current_trx();
788
789 if (trx) {
790 if (use_trx >= 0 && use_trx != trx->trx_no) {
791 LOGP(DRLCMAC, LOGL_ERROR,
792 "- Requested incompatible TRX %d (current is %d)\n",
793 use_trx, trx->trx_no);
794 return -EINVAL;
795 }
796 use_trx = trx->trx_no;
797 }
798
799 /* Step 2a: Find usable TRX and TFI */
Maxa76a7d02018-01-26 11:09:16 +0100800 tfi = tfi_find_free(bts->bts, trx, ms, tbf->direction, use_trx, &trx_no);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200801 if (tfi < 0) {
802 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed to allocate a TFI\n");
803 return tfi;
804 }
805
806 /* Step 2b: Reserve slots on the TRX for the MS */
807 if (!trx)
808 trx = &bts->trx[trx_no];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200809
810 if (!dl_slots || !ul_slots) {
Max842d7812017-11-01 18:11:24 +0100811 rc = find_multi_slots(trx, ms->ms_class(), &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100812 if (rc < 0)
813 return rc;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200814 }
815
Max92e9c172017-09-28 16:25:25 +0200816 reserved_dl_slots = dl_slots;
817 reserved_ul_slots = ul_slots;
818
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200819 /* Step 3: Derive the slot set for the current TBF */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200820 if (single) {
821 /* Make sure to consider the first common slot only */
822 ul_slots = dl_slots = dl_slots & ul_slots;
823
824 ts = first_common_ts;
825
826 if (ts < 0)
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200827 ts = find_least_busy_pdch(trx, tbf->direction,
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200828 dl_slots & ul_slots, compute_usage_by_num_tbfs,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200829 NULL, NULL);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200830 if (ts < 0)
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100831 ul_slots = dl_slots = pcu_lsb(dl_slots & ul_slots);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200832 else
833 ul_slots = dl_slots = (dl_slots & ul_slots) & (1<<ts);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200834 }
835
836 if (dl_slots == 0) {
837 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots available\n");
838 return -EINVAL;
839 }
840
841 if (ul_slots == 0) {
842 LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots available\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200843 return -EINVAL;
844 }
845
846 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200847 LOGP(DRLCMAC, LOGL_DEBUG,
848 "- Selected DL slots: (TS=0)\"%s\"(TS=7)%s\n",
849 set_flag_chars(set_flag_chars(slot_info,
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200850 reserved_dl_slots, 'd', '.'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200851 dl_slots, 'D'),
852 single ? ", single" : "");
853
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200854 /* assign downlink */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200855 if (dl_slots == 0) {
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200856 LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots "
857 "available\n");
858 return -EINVAL;
859 }
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100860 slotcount = pcu_bitcount(dl_slots);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200861 first_ts = ffs(dl_slots) - 1;
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100862 avail_count = pcu_bitcount(reserved_dl_slots);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200863
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200864 } else {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200865 int free_usf = -1;
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200866
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200867 if (first_common_ts >= 0)
868 ul_slots = 1 << first_common_ts;
869 else
870 ul_slots = ul_slots & dl_slots;
871
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200872 ts = find_least_busy_pdch(trx, GPRS_RLCMAC_UL_TBF,
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200873 ul_slots, compute_usage_by_num_tbfs,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200874 NULL, &free_usf);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200875
876 if (free_usf < 0) {
877 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
878 return -EBUSY;
879 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200880 OSMO_ASSERT(ts >= 0 && ts <= 8);
881
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200882 ul_slots = 1 << ts;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200883 usf[ts] = free_usf;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200884
885 LOGP(DRLCMAC, LOGL_DEBUG,
886 "- Selected UL slots: (TS=0)\"%s\"(TS=7)%s\n",
887 set_flag_chars(set_flag_chars(slot_info,
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200888 reserved_ul_slots, 'u', '.'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200889 ul_slots, 'U'),
890 single ? ", single" : "");
891
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200892 slotcount++;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200893 first_ts = ts;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200894
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200895 /* We will stick to that single UL slot, unreserve the others */
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200896 reserved_ul_slots = ul_slots;
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200897
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100898 avail_count = pcu_bitcount(reserved_ul_slots);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200899 }
900
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200901 first_common_ts = ffs(dl_slots & ul_slots) - 1;
902
903 if (first_common_ts < 0) {
904 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
905 return -EINVAL;
906 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200907 if (first_ts < 0) {
908 LOGP(DRLCMAC, LOGL_NOTICE, "No first slot available\n");
909 return -EINVAL;
910 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200911
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200912 if (single && slotcount) {
913 tbf_->upgrade_to_multislot = (avail_count > slotcount);
914 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
915 first_ts,
916 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
917 } else {
918 tbf_->upgrade_to_multislot = 0;
919 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
920 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
921 }
922
923 /* The allocation will be successful, so the system state and tbf_/ms_
924 * may be modified from now on. */
925
926 /* Step 4: Update MS and TBF and really allocate the resources */
927
928 /* The reserved slots have changed, update the MS */
929 if (reserved_ul_slots != ms->reserved_ul_slots() ||
930 reserved_dl_slots != ms->reserved_dl_slots())
931 {
932 ms_->set_reserved_slots(trx,
933 reserved_ul_slots, reserved_dl_slots);
934
935 LOGP(DRLCMAC, LOGL_DEBUG,
936 "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
937 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
938 dl_slots, 'D', '.'),
939 ul_slots, 'U'),
940 ul_slots & dl_slots, 'C'));
941 }
942
943 tbf_->trx = trx;
944 tbf_->first_common_ts = first_common_ts;
945 tbf_->first_ts = first_ts;
946
947 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100948 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200949 for (ts = 0; ts < 8; ts++) {
950 if (!(dl_slots & (1 << ts)))
951 continue;
952
953 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
954 "%d\n", ts);
955 assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi);
956 }
957 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100958 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200959
960 for (ts = 0; ts < 8; ts++) {
961 if (!(ul_slots & (1 << ts)))
962 continue;
963
964 OSMO_ASSERT(usf[ts] >= 0);
965
966 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
967 "%d\n", ts);
968 assign_uplink_tbf_usf(&trx->pdch[ts], ul_tbf,
969 tfi, usf[ts]);
970 }
971 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200972
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200973 bts->bts->tbf_alloc_algo_b();
974
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200975 return 0;
976}
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200977
Maxe9fe0e32017-09-28 15:56:05 +0200978/*! Slot Allocation: Algorithm dynamic
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200979 *
980 * This meta algorithm automatically selects on of the other algorithms based
981 * on the current system state.
982 *
983 * The goal is to support as many MS and TBF as possible. On low usage, the
984 * goal is to provide the highest possible bandwidth per MS.
985 *
Maxe9fe0e32017-09-28 15:56:05 +0200986 * \param[in,out] bts Pointer to BTS struct
987 * \param[in,out] ms_ Pointer to MS object
988 * \param[in,out] tbf_ Pointer to TBF struct
989 * \param[in] single flag indicating if we should force single-slot allocation
990 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
991 * \returns negative error code or 0 on success
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200992 */
Maxe9fe0e32017-09-28 15:56:05 +0200993int alloc_algorithm_dynamic(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
994 int8_t use_trx)
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200995{
996 int rc;
997
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200998 /* Reset load_is_high if there is at least one idle PDCH */
999 if (bts->multislot_disabled) {
Maxa76a7d02018-01-26 11:09:16 +01001000 bts->multislot_disabled = !idle_pdch_avail(bts);
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001001 if (!bts->multislot_disabled)
1002 LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n");
1003 }
1004
1005 if (!bts->multislot_disabled) {
Maxe9fe0e32017-09-28 15:56:05 +02001006 rc = alloc_algorithm_b(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001007 if (rc >= 0)
1008 return rc;
1009
1010 if (!bts->multislot_disabled)
1011 LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n");
1012 bts->multislot_disabled = 1;
1013 }
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001014
Maxe9fe0e32017-09-28 15:56:05 +02001015 return alloc_algorithm_a(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001016}
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001017
1018int gprs_alloc_max_dl_slots_per_ms(struct gprs_rlcmac_bts *bts, uint8_t ms_class)
1019{
Max842d7812017-11-01 18:11:24 +01001020 int rx = mslot_class_get_rx(ms_class);
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001021
1022 if (rx == MS_NA)
1023 rx = 4;
1024
1025 if (bts->alloc_algorithm == alloc_algorithm_a)
1026 return 1;
1027
1028 if (bts->multislot_disabled)
1029 return 1;
1030
1031 return rx;
1032}