blob: 29e41f5dd97d88c69e6fec2144fdef62f4e93e1c [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>
Max6dc90b82018-02-19 17:17:28 +010026#include <pdch.h>
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020027#include <gprs_ms.h>
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010028#include <pcu_utils.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020029
30#include <errno.h>
Jacob Erlbeckec478752015-06-19 16:35:38 +020031#include <values.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020032
Max842d7812017-11-01 18:11:24 +010033extern "C" {
34#include "mslot_class.h"
Max1187a772018-01-26 13:31:42 +010035#include <osmocom/core/linuxlist.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/core/utils.h>
Max842d7812017-11-01 18:11:24 +010038}
39
Jacob Erlbeck77da3552015-07-16 18:33:46 +020040/* Consider a PDCH as idle if has at most this number of TBFs assigned to it */
41#define PDCH_IDLE_TBF_THRESH 1
42
Jacob Erlbeckea65c722015-06-22 16:14:23 +020043static char *set_flag_chars(char *buf, uint8_t val, char set_char, char unset_char = 0)
44{
45 int i;
46
47 for (i = 0; i < 8; i += 1, val = val >> 1) {
48 if (val & 1)
49 buf[i] = set_char;
50 else if (unset_char)
51 buf[i] = unset_char;
52 }
53
54 return buf;
55}
56
57static bool test_and_set_bit(uint32_t *bits, size_t elem)
58{
59 bool was_set = bits[elem/32] & (1 << (elem % 32));
60 bits[elem/32] |= (1 << (elem % 32));
61
62 return was_set;
63}
64
Maxa76a7d02018-01-26 11:09:16 +010065static 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 +020066{
Maxa76a7d02018-01-26 11:09:16 +010067 uint32_t tfi_map = pdch->assigned_tfi(dir);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +020068 int8_t tfi;
69
Maxd000d802017-09-20 17:55:28 +020070 if (tfi_map == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +020071 return -1;
72
73 /* look for USF, don't use USF=7 */
74 for (tfi = 0; tfi < 32; tfi++) {
75 if (!(tfi_map & (1 << tfi)))
76 return tfi;
77 }
78
79 return -1;
80}
81
Maxa76a7d02018-01-26 11:09:16 +010082static int find_possible_pdchs(const struct gprs_rlcmac_trx *trx, size_t max_slots, uint8_t mask,
83 const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020084{
Jacob Erlbeckec478752015-06-19 16:35:38 +020085 unsigned ts;
86 int valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +020087 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +020088
89 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +010090 const struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020091
92 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020093 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020094 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
95 "not enabled\n", ts);
96 continue;
97 }
Jacob Erlbeckec478752015-06-19 16:35:38 +020098
99 if (((1 << ts) & mask) == 0) {
100 if (mask_reason)
101 LOGP(DRLCMAC, LOGL_DEBUG,
102 "- Skipping TS %d, because %s\n",
103 ts, mask_reason);
104 continue;
105 }
106
Jacob Erlbeck83426b22015-06-30 09:44:05 +0200107 if (max_slots > 1) {
108 /* check if TSC changes, see TS 45.002, 6.4.2 */
109 if (last_tsc < 0)
110 last_tsc = pdch->tsc;
111 else if (last_tsc != pdch->tsc) {
112 LOGP(DRLCMAC, LOGL_ERROR,
113 "Skipping TS %d of TRX=%d, because it "
114 "has different TSC than lower TS of TRX. "
115 "In order to allow multislot, all "
116 "slots must be configured with the same "
117 "TSC!\n", ts, trx->trx_no);
118 continue;
119 }
120 }
121
Jacob Erlbeckec478752015-06-19 16:35:38 +0200122 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200123 }
124
Jacob Erlbeckec478752015-06-19 16:35:38 +0200125 return valid_ts_set;
126}
127
Maxa76a7d02018-01-26 11:09:16 +0100128static 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 +0200129{
130 return pdch->num_tbfs(dir);
131}
132
Maxa76a7d02018-01-26 11:09:16 +0100133static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200134{
135 return
136 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
137 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
138}
139
Maxa76a7d02018-01-26 11:09:16 +0100140static 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 +0200141{
142 int usage =
143 pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) +
144 pdch->num_tbfs(GPRS_RLCMAC_UL_TBF) +
145 compute_usage_by_reservation(pdch, dir);
146
Maxd000d802017-09-20 17:55:28 +0200147 if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200148 /* No TFI in the opposite direction, avoid it */
149 usage += 32;
150
151 return usage;
152
153}
154
Maxa76a7d02018-01-26 11:09:16 +0100155/*! Return the TS which corresponds to least busy PDCH
156 *
157 * \param[in] trx Pointer to TRX object
158 * \param[in] dir TBF direction
159 * \param[in] mask set of available timeslots
160 * \param[in] fn Function pointer to function which computes number of associated TBFs
161 * \param[out] free_tfi Free TFI
162 * \param[out] free_usf Free USF
163 * \returns TS number or -1 if unable to find
164 */
165static int find_least_busy_pdch(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t mask,
166 int (*fn)(const struct gprs_rlcmac_pdch *, enum gprs_rlcmac_tbf_direction dir),
167 int *free_tfi = 0, int *free_usf = 0)
Jacob Erlbeckec478752015-06-19 16:35:38 +0200168{
169 unsigned ts;
170 int min_used = INT_MAX;
171 int min_ts = -1;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200172 int min_tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200173 int min_usf = -1;
174
175 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100176 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckec478752015-06-19 16:35:38 +0200177 int num_tbfs;
178 int usf = -1; /* must be signed */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200179 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200180
181 if (((1 << ts) & mask) == 0)
182 continue;
183
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200184 num_tbfs = fn(pdch, dir);
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200185
186 if (num_tbfs < min_used) {
187 /* We have found a candidate */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200188 /* Make sure that a TFI is available */
189 if (free_tfi) {
190 tfi = find_free_tfi(pdch, dir);
191 if (tfi < 0) {
192 LOGP(DRLCMAC, LOGL_DEBUG,
193 "- Skipping TS %d, because "
194 "no TFI available\n", ts);
195 continue;
196 }
197 }
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200198 /* Make sure that an USF is available */
199 if (dir == GPRS_RLCMAC_UL_TBF) {
Maxadca67b2018-01-31 15:22:36 +0100200 usf = find_free_usf(pdch->assigned_usf());
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200201 if (usf < 0) {
202 LOGP(DRLCMAC, LOGL_DEBUG,
203 "- Skipping TS %d, because "
204 "no USF available\n", ts);
205 continue;
206 }
207 }
208 if (min_ts >= 0)
209 LOGP(DRLCMAC, LOGL_DEBUG,
210 "- Skipping TS %d, because "
211 "num TBFs %d > %d\n",
212 min_ts, min_used, num_tbfs);
213 min_used = num_tbfs;
214 min_ts = ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200215 min_tfi = tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200216 min_usf = usf;
217 } else {
218 LOGP(DRLCMAC, LOGL_DEBUG,
219 "- Skipping TS %d, because "
220 "num TBFs %d >= %d\n",
221 ts, num_tbfs, min_used);
222 }
223 }
224
225 if (min_ts < 0)
226 return -1;
227
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200228 if (free_tfi)
229 *free_tfi = min_tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200230 if (free_usf)
231 *free_usf = min_usf;
232
233 return min_ts;
234}
235
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200236static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
237 struct gprs_rlcmac_tbf *tbf)
238{
239 if (tbf->pdch[pdch->ts_no])
240 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
241
242 tbf->pdch[pdch->ts_no] = pdch;
243 pdch->attach_tbf(tbf);
244}
245
Maxa76a7d02018-01-26 11:09:16 +0100246static 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 +0200247{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200248 tbf->m_tfi = tfi;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200249 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200250 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200251}
252
Maxa76a7d02018-01-26 11:09:16 +0100253static 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 +0200254{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200255 tbf->m_tfi = tfi;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200256 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200257}
258
Maxa76a7d02018-01-26 11:09:16 +0100259static int find_trx(const struct gprs_rlcmac_bts *bts_data, const GprsMs *ms, int8_t use_trx)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200260{
261 unsigned trx_no;
262 unsigned ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200263
264 /* We must use the TRX currently actively used by an MS */
265 if (ms && ms->current_trx())
266 return ms->current_trx()->trx_no;
267
268 if (use_trx >= 0 && use_trx < 8)
269 return use_trx;
270
271 /* Find the first TRX that has a PDCH with a free UL and DL TFI */
272 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100273 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200274 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100275 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200276 if (!pdch->is_enabled())
277 continue;
278
Maxd000d802017-09-20 17:55:28 +0200279 if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200280 continue;
281
Maxd000d802017-09-20 17:55:28 +0200282 if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200283 continue;
284
285 return trx_no;
286 }
287 }
288
289 return -EBUSY;
290}
291
Maxa76a7d02018-01-26 11:09:16 +0100292static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts_data)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200293{
294 unsigned trx_no;
295 unsigned ts;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200296
297 /* Find the first PDCH with an unused DL TS */
298 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100299 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200300 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100301 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200302 if (!pdch->is_enabled())
303 continue;
304
305 if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH)
306 continue;
307
Maxa76a7d02018-01-26 11:09:16 +0100308 return true;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200309 }
310 }
311
Maxa76a7d02018-01-26 11:09:16 +0100312 return false;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200313}
314
Maxa76a7d02018-01-26 11:09:16 +0100315/*! Return free TFI
316 *
317 * \param[in] bts Pointer to BTS struct
Max7e4921d2017-09-28 16:41:24 +0200318 * \param[in] trx Optional pointer to TRX struct
Maxa76a7d02018-01-26 11:09:16 +0100319 * \param[in] ms Pointer to MS object
320 * \param[in] dir DL or UL direction
321 * \param[in] use_trx which TRX to use or -1 if it should be selected based on what MS uses
322 * \param[out] trx_no_ TRX number on which TFI was found
323 * \returns negative error code or 0 on success
324 */
325static int tfi_find_free(const BTS *bts, const gprs_rlcmac_trx *trx, const GprsMs *ms,
326 enum gprs_rlcmac_tbf_direction dir, int8_t use_trx, uint8_t *trx_no_)
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200327{
328 int tfi;
329 uint8_t trx_no;
330
Max7e4921d2017-09-28 16:41:24 +0200331 if (trx) {
332 if (use_trx >= 0 && use_trx != trx->trx_no) {
333 LOGP(DRLCMAC, LOGL_ERROR, "- Requested incompatible TRX %d (current is %d)\n",
334 use_trx, trx->trx_no);
335 return -EINVAL;
336 }
337 use_trx = trx->trx_no;
338 }
339
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200340 if (use_trx == -1 && ms->current_trx())
341 use_trx = ms->current_trx()->trx_no;
342
343 tfi = bts->tfi_find_free(dir, &trx_no, use_trx);
344 if (tfi < 0)
345 return -EBUSY;
346
347 if (trx_no_)
348 *trx_no_ = trx_no;
349
350 return tfi;
351}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200352
Maxe9fe0e32017-09-28 15:56:05 +0200353/*! Slot Allocation: Algorithm A
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200354 *
355 * Assign single slot for uplink and downlink
Maxe9fe0e32017-09-28 15:56:05 +0200356 *
357 * \param[in,out] bts Pointer to BTS struct
358 * \param[in,out] ms_ Pointer to MS object
359 * \param[in,out] tbf_ Pointer to TBF struct
360 * \param[in] single flag indicating if we should force single-slot allocation
361 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
362 * \returns negative error code or 0 on success
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200363 */
Maxe9fe0e32017-09-28 15:56:05 +0200364int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
365 int8_t use_trx)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200366{
367 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200368 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200369 uint8_t ul_slots, dl_slots;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200370 int trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200371 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200372 int usf = -1;
373 int mask = 0xff;
374 const char *mask_reason = NULL;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200375 const GprsMs *ms = ms_;
376 const gprs_rlcmac_tbf *tbf = tbf_;
377 gprs_rlcmac_trx *trx = ms->current_trx();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200378
379 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200380 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200381
Maxa76a7d02018-01-26 11:09:16 +0100382 trx_no = find_trx(bts, ms, use_trx);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200383 if (trx_no < 0) {
384 LOGP(DRLCMAC, LOGL_NOTICE,
385 "- Failed to find a usable TRX (TFI exhausted)\n");
386 return trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200387 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200388 if (!trx)
389 trx = &bts->trx[trx_no];
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200390
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200391 dl_slots = ms->reserved_dl_slots();
392 ul_slots = ms->reserved_ul_slots();
393
394 ts = ms->first_common_ts();
395
396 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200397 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200398 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200399 } else if (dl_slots || ul_slots) {
400 mask_reason = "need to use a reserved common TS";
401 mask = dl_slots & ul_slots;
402 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200403
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200404 mask = find_possible_pdchs(trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200405 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200406 return -EINVAL;
407
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200408 ts = find_least_busy_pdch(trx, tbf->direction, mask,
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200409 compute_usage_for_algo_a,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200410 &tfi, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200411
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200412 if (tbf->direction == GPRS_RLCMAC_UL_TBF && usf < 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200413 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200414 "to allocate a TS, no USF available\n");
Jacob Erlbeckec478752015-06-19 16:35:38 +0200415 return -EBUSY;
416 }
417
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200418 if (ts < 0) {
419 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
420 "to allocate a TS, no TFI available\n");
421 return -EBUSY;
422 }
423
424 pdch = &trx->pdch[ts];
425
426 /* The allocation will be successful, so the system state and tbf_/ms_
427 * may be modified from now on. */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200428 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100429 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200430 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d TFI=%d USF=%d\n",
431 ts, tfi, usf);
432 assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200433 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100434 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200435 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d TFI=%d\n",
436 ts, tfi);
437 assign_dlink_tbf(pdch, dl_tbf, tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200438 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200439
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200440 tbf_->trx = trx;
441 /* the only one TS is the common TS */
442 tbf_->first_ts = tbf_->first_common_ts = ts;
443 ms_->set_reserved_slots(trx, 1 << ts, 1 << ts);
444
445 tbf_->upgrade_to_multislot = 0;
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200446 bts->bts->tbf_alloc_algo_a();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200447 return 0;
448}
449
Maxadca67b2018-01-31 15:22:36 +0100450/*! Compute capacity of a given TRX
451 *
452 * \param[in] trx Pointer to TRX object
453 * \param[in] rx_window Receive window
454 * \param[in] tx_window Transmit window
455 * \returns non-negative capacity
456 */
457static inline unsigned compute_capacity(const struct gprs_rlcmac_trx *trx, int rx_window, int tx_window)
458{
459 const struct gprs_rlcmac_pdch *pdch;
460 unsigned ts, capacity = 0;
461
462 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
463 pdch = &trx->pdch[ts];
464 if (rx_window & (1 << ts))
465 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF), 1);
466
467 /* Only consider common slots for UL */
468 if (tx_window & rx_window & (1 << ts)) {
469 if (find_free_usf(pdch->assigned_usf()) >= 0)
470 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF), 1);
471 }
472 }
473
474 return capacity;
475}
476
Maxa76a7d02018-01-26 11:09:16 +0100477/*! Find set of slots available for allocation while taking MS class into account
478 *
479 * \param[in] trx Pointer to TRX object
480 * \param[in] mslot_class The multislot class
481 * \param[in,out] ul_slots set of UL timeslots
482 * \param[in,out] dl_slots set of DL timeslots
483 * \returns negative error code or 0 on success
484 */
Max46fbfce2017-11-01 19:22:25 +0100485int 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 +0200486{
Maxf633b8d2018-01-31 15:28:53 +0100487 uint8_t Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */
488 Sum = mslot_class_get_sum(mslot_class); /* Max number of Tx + Rx slots */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200489 int rx_window, tx_window, pdch_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200490 char slot_info[9] = {0};
Maxf633b8d2018-01-31 15:28:53 +0100491 int max_capacity = -1;
492 uint8_t max_ul_slots = 0, max_dl_slots = 0;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200493 unsigned max_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200494
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200495 unsigned ul_ts, dl_ts;
496 unsigned num_tx;
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200497 unsigned mask_sel;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200498
Max842d7812017-11-01 18:11:24 +0100499 if (mslot_class)
500 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n",
501 mslot_class);
502
Max842d7812017-11-01 18:11:24 +0100503 if (Tx == MS_NA) {
504 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n",
505 mslot_class);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200506 return -EINVAL;
507 }
508
Max842d7812017-11-01 18:11:24 +0100509 max_slots = OSMO_MAX(mslot_class_get_rx(mslot_class), Tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200510
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200511 if (*dl_slots == 0)
512 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200513
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200514 if (*ul_slots == 0)
515 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200516
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200517 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200518
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200519 *dl_slots &= pdch_slots;
520 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200521
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200522 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
523 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
524 *dl_slots, 'D', '.'),
525 *ul_slots, 'U'),
526 *ul_slots & *dl_slots, 'C'));
527
528 /* Check for each UL (TX) slot */
529
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200530 /* Iterate through possible numbers of TX slots */
Max842d7812017-11-01 18:11:24 +0100531 for (num_tx = 1; num_tx <= mslot_class_get_tx(mslot_class); num_tx += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200532 uint16_t tx_valid_win = (1 << num_tx) - 1;
Maxf633b8d2018-01-31 15:28:53 +0100533 uint8_t rx_mask[MASK_TR + 1];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200534
Maxf633b8d2018-01-31 15:28:53 +0100535 mslot_fill_rx_mask(mslot_class, num_tx, rx_mask);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200536
537 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
538 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
539 unsigned tx_slot_count;
540 int max_rx;
541 uint16_t rx_valid_win;
542 uint32_t checked_rx[256/32] = {0};
543
544 /* Wrap valid window */
545 tx_valid_win = (tx_valid_win | tx_valid_win >> 8) & 0xff;
546
547 tx_window = tx_valid_win;
548
549 /* Filter out unavailable slots */
550 tx_window &= *ul_slots;
551
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200552 /* Skip if the the first TS (ul_ts) is not in the set */
553 if ((tx_window & (1 << ul_ts)) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200554 continue;
555
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200556 /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */
557 if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200558 continue;
559
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100560 tx_slot_count = pcu_bitcount(tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200561
Max842d7812017-11-01 18:11:24 +0100562 max_rx = OSMO_MIN(mslot_class_get_rx(mslot_class), Sum - num_tx);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200563 rx_valid_win = (1 << max_rx) - 1;
564
565 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
566 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
567 /* Wrap valid window */
568 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
569
570 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200571 for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200572 unsigned common_slot_count;
573 unsigned req_common_slots;
574 unsigned rx_slot_count;
575 uint16_t rx_bad;
576 uint8_t rx_good;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200577 int capacity;
578
579 /* Filter out bad slots */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200580 rx_bad = (uint16_t)(0xff & ~rx_mask[mask_sel]) << ul_ts;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200581 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xff;
582 rx_good = *dl_slots & ~rx_bad;
583
584 /* TODO: CHECK this calculation -> separate function for unit
585 * testing */
586
587 rx_window = rx_good & rx_valid_win;
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100588 rx_slot_count = pcu_bitcount(rx_window);
Jacob Erlbeckbae33a72015-07-06 14:55:13 +0200589
590#if 0
591 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, mask_sel=%d, "
592 "tx=%02x, rx=%02x, mask=%02x, bad=%02x, good=%02x, "
593 "ul=%02x, dl=%02x\n",
594 tx_slot_count, rx_slot_count, mask_sel,
595 tx_window, rx_window, rx_mask[mask_sel], rx_bad, rx_good,
596 *ul_slots, *dl_slots);
597#endif
598
599 /* Check compliance with TS 45.002, table 6.4.2.2.1 */
600 /* Whether to skip this round doesn not only depend on the bit
601 * sets but also on mask_sel. Therefore this check must be done
602 * before doing the test_and_set_bit shortcut. */
Maxf633b8d2018-01-31 15:28:53 +0100603 if (mslot_class_get_type(mslot_class) == 1) {
Jacob Erlbeckbae33a72015-07-06 14:55:13 +0200604 unsigned slot_sum = rx_slot_count + tx_slot_count;
605 /* Assume down+up/dynamic.
606 * TODO: For ext-dynamic, down only, up only add more
607 * cases.
608 */
609 if (slot_sum <= 6 && tx_slot_count < 3) {
610 if (mask_sel != MASK_TR)
611 /* Skip Tta */
612 continue;
613 } else if (slot_sum > 6 && tx_slot_count < 3) {
614 if (mask_sel != MASK_TT)
615 /* Skip Tra */
616 continue;
617 } else {
618 /* No supported row in table 6.4.2.2.1. */
619#ifdef ENABLE_TS_ALLOC_DEBUG
620 LOGP(DRLCMAC, LOGL_DEBUG,
621 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
622 "combination not supported\n",
623 set_flag_chars(set_flag_chars(set_flag_chars(
624 slot_info,
625 rx_bad, 'x', '.'),
626 rx_window, 'D'),
627 tx_window, 'U'));
628#endif
629 continue;
630 }
631 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200632
633 /* Avoid repeated RX combination check */
634 if (test_and_set_bit(checked_rx, rx_window))
635 continue;
636
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200637 if (!rx_good) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200638#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200639 LOGP(DRLCMAC, LOGL_DEBUG,
640 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
641 "no DL slots available\n",
642 set_flag_chars(set_flag_chars(slot_info,
643 rx_bad, 'x', '.'),
644 tx_window, 'U'));
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200645#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200646 continue;
647 }
648
649 if (!rx_window)
650 continue;
651
652 /* Check number of common slots according to TS 54.002, 6.4.2.2 */
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100653 common_slot_count = pcu_bitcount(tx_window & rx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200654 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
Maxf633b8d2018-01-31 15:28:53 +0100655 if (mslot_class_get_type(mslot_class) == 1)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200656 req_common_slots = OSMO_MIN(req_common_slots, 2);
657
658 if (req_common_slots != common_slot_count) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200659#ifdef ENABLE_TS_ALLOC_DEBUG
660 LOGP(DRLCMAC, LOGL_DEBUG,
661 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
662 "invalid number of common TS: %d (expected %d)\n",
663 set_flag_chars(set_flag_chars(set_flag_chars(
664 slot_info,
665 rx_bad, 'x', '.'),
666 rx_window, 'D'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200667 tx_window, 'U'),
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200668 common_slot_count,
669 req_common_slots);
670#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200671 continue;
672 }
673
674 /* Compute capacity */
Maxadca67b2018-01-31 15:22:36 +0100675 capacity = compute_capacity(trx, rx_window, tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200676
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200677#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200678 LOGP(DRLCMAC, LOGL_DEBUG,
679 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
680 "capacity = %d\n",
681 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
682 slot_info,
683 rx_bad, 'x', '.'),
684 rx_window, 'D'),
685 tx_window, 'U'),
686 rx_window & tx_window, 'C'),
687 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200688#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200689
690 if (capacity <= max_capacity)
691 continue;
692
693 max_capacity = capacity;
694 max_ul_slots = tx_window;
695 max_dl_slots = rx_window;
Maxadca67b2018-01-31 15:22:36 +0100696 }
697 }
698 }
699 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200700
701 if (!max_ul_slots || !max_dl_slots) {
702 LOGP(DRLCMAC, LOGL_NOTICE,
703 "No valid UL/DL slot combination found\n");
704 return -EINVAL;
705 }
706
707 *ul_slots = max_ul_slots;
708 *dl_slots = max_dl_slots;
709
710 return 0;
711}
712
Max0cc72122018-01-31 17:00:06 +0100713/*! Count used bits in slots and reserved_slots bitmasks
714 *
715 * \param[in] slots Timeslots in use
716 * \param[in] reserved_slots Reserved timeslots
717 * \param[out] slotcount Number of TS in use
718 * \param[out] avail_count Number of reserved TS
719 */
720static void update_slot_counters(uint8_t slots, uint8_t reserved_slots, uint8_t *slotcount, uint8_t *avail_count)
721{
722 (*slotcount) = pcu_bitcount(slots);
723 (*avail_count) = pcu_bitcount(reserved_slots);
724}
725
726/*! Return slot mask with single TS from a given UL/DL set according to TBF's direction, ts pointer is set to that TS
727 * number or to negative value on error
728 *
729 * \param[in] trx Pointer to TRX object
730 * \param[in] tbf Pointer to TBF object
731 * \param[in] dl_slots set of DL timeslots
732 * \param[in] ul_slots set of UL timeslots
733 * \param[in] ts corresponding TS or -1 for autoselection
734 * \returns slot mask with single UL or DL timeslot number if possible
735 */
736static uint8_t get_single_ts(const gprs_rlcmac_trx *trx, const gprs_rlcmac_tbf *tbf, uint8_t dl_slots, uint8_t ul_slots,
737 int ts)
738{
739 uint8_t ret = dl_slots & ul_slots; /* Make sure to consider the first common slot only */
740
741 if (ts < 0)
742 ts = find_least_busy_pdch(trx, tbf->direction, ret, compute_usage_by_num_tbfs, NULL, NULL);
743
744 if (ts < 0)
745 return ffs(ret);
746
747 return ret & (1 << ts);
748}
749
750/*! Find set of timeslots available for allocation
751 *
752 * \param[in] trx Pointer to TRX object
753 * \param[in] tbf Pointer to TBF object
754 * \param[in] single Flag to force the single TS allocation
755 * \param[in] ul_slots set of UL timeslots
756 * \param[in] dl_slots set of DL timeslots
757 * \param[in] reserved_ul_slots set of reserved UL timeslots
758 * \param[in] reserved_dl_slots set of reserved DL timeslots
759 * \param[in] first_common_ts First TS common for both UL and DL or -1 if unknown
760 * \returns negative error code or selected TS on success
761 */
762static int tbf_select_slot_set(const gprs_rlcmac_tbf *tbf, const gprs_rlcmac_trx *trx, bool single,
763 uint8_t ul_slots, uint8_t dl_slots,
764 uint8_t reserved_ul_slots, uint8_t reserved_dl_slots,
765 int8_t first_common_ts)
766{
767 uint8_t sl = tbf->direction != GPRS_RLCMAC_DL_TBF ? ul_slots : dl_slots;
768 char slot_info[9] = { 0 };
769
770 if (single)
771 sl = get_single_ts(trx, tbf, dl_slots, ul_slots, first_common_ts);
772
773 if (!sl) {
774 LOGP(DRLCMAC, LOGL_NOTICE, "No %s slots available\n",
775 tbf->direction != GPRS_RLCMAC_DL_TBF ? "uplink" : "downlink");
776 return -EINVAL;
777 }
778
779 if (tbf->direction != GPRS_RLCMAC_DL_TBF) {
780 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_ul_slots, 'u'));
781 masked_override_with(slot_info, sl, 'U');
782 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected UL");
783 } else {
784 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_dl_slots, 'd'));
785 masked_override_with(slot_info, sl, 'D');
786 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected DL");
787 }
788
789 LOGPC(DRLCMAC, LOGL_DEBUG, " slots: (TS=0)\"%s\"(TS=7)%s\n", slot_info, single ? ", single" : "");
790
791 return sl;
792}
793
Max2afec6d2018-01-31 17:21:21 +0100794/*! Allocate USF according to a given UL TS mapping
795 *
796 * N. B: this is legacy implementation which ignores given selected_ul_slots
797 * \param[in] trx Pointer to TRX object
798 * \param[in] tbf Pointer to TBF object
799 * \param[in] first_common_ts First TS which is common to both UL and DL
800 * \param[in] selected_ul_slots set of UL timeslots selected for allocation
801 * \param[in] dl_slots set of DL timeslots
802 * \param[out] usf array for allocated USF
803 * \returns updated UL TS or negative on error
804 */
805static int allocate_usf(const gprs_rlcmac_trx *trx, int8_t first_common_ts, uint8_t selected_ul_slots, uint8_t dl_slots,
806 int *usf)
807{
808 int free_usf = -1, ts;
809 uint8_t ul_slots = selected_ul_slots;
810
811 if (first_common_ts >= 0)
812 ul_slots = 1 << first_common_ts;
813 else
814 ul_slots = ul_slots & dl_slots;
815
816 ts = find_least_busy_pdch(trx, GPRS_RLCMAC_UL_TBF, ul_slots, compute_usage_by_num_tbfs, NULL, &free_usf);
817
818 if (free_usf < 0 || ts < 0) {
819 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
820 return -EBUSY;
821 }
822
823 OSMO_ASSERT(ts >= 0 && ts <= 8);
824
825 /* We will stick to that single UL slot, unreserve the others */
826 ul_slots = 1 << ts;
827 usf[ts] = free_usf;
828
829 return ul_slots;
830}
831
Maxe9fe0e32017-09-28 15:56:05 +0200832/*! Slot Allocation: Algorithm B
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200833 *
834 * Assign as many downlink slots as possible.
835 * Assign one uplink slot. (With free USF)
836 *
Maxe9fe0e32017-09-28 15:56:05 +0200837 * \param[in,out] bts Pointer to BTS struct
838 * \param[in,out] ms_ Pointer to MS object
839 * \param[in,out] tbf_ Pointer to TBF struct
840 * \param[in] single flag indicating if we should force single-slot allocation
841 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
842 * \returns negative error code or 0 on success
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200843 */
Maxe9fe0e32017-09-28 15:56:05 +0200844int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
845 int8_t use_trx)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200846{
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200847 uint8_t dl_slots;
848 uint8_t ul_slots;
849 uint8_t reserved_dl_slots;
850 uint8_t reserved_ul_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200851 int8_t first_common_ts;
852 uint8_t slotcount = 0;
Maxa76a7d02018-01-26 11:09:16 +0100853 uint8_t avail_count = 0, trx_no;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200854 char slot_info[9] = {0};
855 int ts;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200856 int first_ts = -1;
857 int usf[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200858 int rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200859 int tfi;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200860 const GprsMs *ms = ms_;
861 const gprs_rlcmac_tbf *tbf = tbf_;
862 gprs_rlcmac_trx *trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200863
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200864 /* Step 1: Get current state from the MS object */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200865
866 if (!ms) {
867 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
868 return -EINVAL;
869 }
870
Max92e9c172017-09-28 16:25:25 +0200871 dl_slots = ms->reserved_dl_slots();
872 ul_slots = ms->reserved_ul_slots();
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200873 first_common_ts = ms->first_common_ts();
874 trx = ms->current_trx();
875
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200876 /* Step 2a: Find usable TRX and TFI */
Maxa76a7d02018-01-26 11:09:16 +0100877 tfi = tfi_find_free(bts->bts, trx, ms, tbf->direction, use_trx, &trx_no);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200878 if (tfi < 0) {
879 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed to allocate a TFI\n");
880 return tfi;
881 }
882
883 /* Step 2b: Reserve slots on the TRX for the MS */
884 if (!trx)
885 trx = &bts->trx[trx_no];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200886
887 if (!dl_slots || !ul_slots) {
Max842d7812017-11-01 18:11:24 +0100888 rc = find_multi_slots(trx, ms->ms_class(), &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100889 if (rc < 0)
890 return rc;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200891 }
892
Max92e9c172017-09-28 16:25:25 +0200893 reserved_dl_slots = dl_slots;
894 reserved_ul_slots = ul_slots;
895
Max0cc72122018-01-31 17:00:06 +0100896 /* Step 3a: Derive the slot set for the current TBF */
897 rc = tbf_select_slot_set(tbf, trx, single, ul_slots, dl_slots, reserved_ul_slots, reserved_dl_slots,
898 first_common_ts);
899 if (rc < 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200900 return -EINVAL;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200901
Max0cc72122018-01-31 17:00:06 +0100902 first_ts = ffs(rc) - 1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200903
Max0cc72122018-01-31 17:00:06 +0100904 /* Step 3b: Derive the slot set for a given direction */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200905 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Max0cc72122018-01-31 17:00:06 +0100906 dl_slots = rc;
907 update_slot_counters(dl_slots, reserved_dl_slots, &slotcount, &avail_count);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200908 } else {
Max2afec6d2018-01-31 17:21:21 +0100909 rc = allocate_usf(trx, first_common_ts, rc, dl_slots, usf);
910 if (rc < 0)
911 return rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200912
Max0cc72122018-01-31 17:00:06 +0100913 /* We will stick to that single UL slot, unreserve the others */
Max2afec6d2018-01-31 17:21:21 +0100914 ul_slots = rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200915 reserved_ul_slots = ul_slots;
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200916
Max0cc72122018-01-31 17:00:06 +0100917 update_slot_counters(ul_slots, reserved_ul_slots, &slotcount, &avail_count);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200918 }
919
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200920 first_common_ts = ffs(dl_slots & ul_slots) - 1;
921
922 if (first_common_ts < 0) {
923 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
924 return -EINVAL;
925 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200926 if (first_ts < 0) {
927 LOGP(DRLCMAC, LOGL_NOTICE, "No first slot available\n");
928 return -EINVAL;
929 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200930
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200931 if (single && slotcount) {
932 tbf_->upgrade_to_multislot = (avail_count > slotcount);
933 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
934 first_ts,
935 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
936 } else {
937 tbf_->upgrade_to_multislot = 0;
938 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
939 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
940 }
941
942 /* The allocation will be successful, so the system state and tbf_/ms_
943 * may be modified from now on. */
944
945 /* Step 4: Update MS and TBF and really allocate the resources */
946
947 /* The reserved slots have changed, update the MS */
948 if (reserved_ul_slots != ms->reserved_ul_slots() ||
949 reserved_dl_slots != ms->reserved_dl_slots())
950 {
951 ms_->set_reserved_slots(trx,
952 reserved_ul_slots, reserved_dl_slots);
953
954 LOGP(DRLCMAC, LOGL_DEBUG,
955 "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
956 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
957 dl_slots, 'D', '.'),
958 ul_slots, 'U'),
959 ul_slots & dl_slots, 'C'));
960 }
961
962 tbf_->trx = trx;
963 tbf_->first_common_ts = first_common_ts;
964 tbf_->first_ts = first_ts;
965
966 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100967 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200968 for (ts = 0; ts < 8; ts++) {
969 if (!(dl_slots & (1 << ts)))
970 continue;
971
972 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
973 "%d\n", ts);
974 assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi);
975 }
976 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100977 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200978
979 for (ts = 0; ts < 8; ts++) {
980 if (!(ul_slots & (1 << ts)))
981 continue;
982
983 OSMO_ASSERT(usf[ts] >= 0);
984
985 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
986 "%d\n", ts);
987 assign_uplink_tbf_usf(&trx->pdch[ts], ul_tbf,
988 tfi, usf[ts]);
989 }
990 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200991
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200992 bts->bts->tbf_alloc_algo_b();
993
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200994 return 0;
995}
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200996
Maxe9fe0e32017-09-28 15:56:05 +0200997/*! Slot Allocation: Algorithm dynamic
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200998 *
999 * This meta algorithm automatically selects on of the other algorithms based
1000 * on the current system state.
1001 *
1002 * The goal is to support as many MS and TBF as possible. On low usage, the
1003 * goal is to provide the highest possible bandwidth per MS.
1004 *
Maxe9fe0e32017-09-28 15:56:05 +02001005 * \param[in,out] bts Pointer to BTS struct
1006 * \param[in,out] ms_ Pointer to MS object
1007 * \param[in,out] tbf_ Pointer to TBF struct
1008 * \param[in] single flag indicating if we should force single-slot allocation
1009 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
1010 * \returns negative error code or 0 on success
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001011 */
Maxe9fe0e32017-09-28 15:56:05 +02001012int alloc_algorithm_dynamic(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
1013 int8_t use_trx)
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001014{
1015 int rc;
1016
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001017 /* Reset load_is_high if there is at least one idle PDCH */
1018 if (bts->multislot_disabled) {
Maxa76a7d02018-01-26 11:09:16 +01001019 bts->multislot_disabled = !idle_pdch_avail(bts);
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001020 if (!bts->multislot_disabled)
1021 LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n");
1022 }
1023
1024 if (!bts->multislot_disabled) {
Maxe9fe0e32017-09-28 15:56:05 +02001025 rc = alloc_algorithm_b(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001026 if (rc >= 0)
1027 return rc;
1028
1029 if (!bts->multislot_disabled)
1030 LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n");
1031 bts->multislot_disabled = 1;
1032 }
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001033
Maxe9fe0e32017-09-28 15:56:05 +02001034 return alloc_algorithm_a(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001035}
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001036
1037int gprs_alloc_max_dl_slots_per_ms(struct gprs_rlcmac_bts *bts, uint8_t ms_class)
1038{
Max842d7812017-11-01 18:11:24 +01001039 int rx = mslot_class_get_rx(ms_class);
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001040
1041 if (rx == MS_NA)
1042 rx = 4;
1043
1044 if (bts->alloc_algorithm == alloc_algorithm_a)
1045 return 1;
1046
1047 if (bts->multislot_disabled)
1048 return 1;
1049
1050 return rx;
1051}