blob: 6791b03776fcbb2f8892432d6ab3c0208003469a [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 int find_possible_pdchs(const struct gprs_rlcmac_trx *trx, size_t max_slots, uint8_t mask,
66 const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020067{
Jacob Erlbeckec478752015-06-19 16:35:38 +020068 unsigned ts;
69 int valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +020070 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +020071
72 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +010073 const struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020074
75 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020076 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020077 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
78 "not enabled\n", ts);
79 continue;
80 }
Jacob Erlbeckec478752015-06-19 16:35:38 +020081
82 if (((1 << ts) & mask) == 0) {
83 if (mask_reason)
84 LOGP(DRLCMAC, LOGL_DEBUG,
85 "- Skipping TS %d, because %s\n",
86 ts, mask_reason);
87 continue;
88 }
89
Jacob Erlbeck83426b22015-06-30 09:44:05 +020090 if (max_slots > 1) {
91 /* check if TSC changes, see TS 45.002, 6.4.2 */
92 if (last_tsc < 0)
93 last_tsc = pdch->tsc;
94 else if (last_tsc != pdch->tsc) {
95 LOGP(DRLCMAC, LOGL_ERROR,
96 "Skipping TS %d of TRX=%d, because it "
97 "has different TSC than lower TS of TRX. "
98 "In order to allow multislot, all "
99 "slots must be configured with the same "
100 "TSC!\n", ts, trx->trx_no);
101 continue;
102 }
103 }
104
Jacob Erlbeckec478752015-06-19 16:35:38 +0200105 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200106 }
107
Jacob Erlbeckec478752015-06-19 16:35:38 +0200108 return valid_ts_set;
109}
110
Maxa76a7d02018-01-26 11:09:16 +0100111static 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 +0200112{
113 return pdch->num_tbfs(dir);
114}
115
Maxa76a7d02018-01-26 11:09:16 +0100116static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200117{
118 return
119 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
120 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
121}
122
Maxa76a7d02018-01-26 11:09:16 +0100123static 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 +0200124{
125 int usage =
126 pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) +
127 pdch->num_tbfs(GPRS_RLCMAC_UL_TBF) +
128 compute_usage_by_reservation(pdch, dir);
129
Maxd000d802017-09-20 17:55:28 +0200130 if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200131 /* No TFI in the opposite direction, avoid it */
132 usage += 32;
133
134 return usage;
135
136}
137
Maxa76a7d02018-01-26 11:09:16 +0100138/*! Return the TS which corresponds to least busy PDCH
139 *
140 * \param[in] trx Pointer to TRX object
141 * \param[in] dir TBF direction
142 * \param[in] mask set of available timeslots
143 * \param[in] fn Function pointer to function which computes number of associated TBFs
144 * \param[out] free_tfi Free TFI
145 * \param[out] free_usf Free USF
146 * \returns TS number or -1 if unable to find
147 */
148static int find_least_busy_pdch(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t mask,
149 int (*fn)(const struct gprs_rlcmac_pdch *, enum gprs_rlcmac_tbf_direction dir),
150 int *free_tfi = 0, int *free_usf = 0)
Jacob Erlbeckec478752015-06-19 16:35:38 +0200151{
152 unsigned ts;
153 int min_used = INT_MAX;
154 int min_ts = -1;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200155 int min_tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200156 int min_usf = -1;
157
158 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100159 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckec478752015-06-19 16:35:38 +0200160 int num_tbfs;
161 int usf = -1; /* must be signed */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200162 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200163
164 if (((1 << ts) & mask) == 0)
165 continue;
166
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200167 num_tbfs = fn(pdch, dir);
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200168
169 if (num_tbfs < min_used) {
170 /* We have found a candidate */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200171 /* Make sure that a TFI is available */
172 if (free_tfi) {
Maxc5407c72018-02-05 16:11:36 +0100173 tfi = find_free_tfi(pdch->assigned_tfi(dir));
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200174 if (tfi < 0) {
175 LOGP(DRLCMAC, LOGL_DEBUG,
176 "- Skipping TS %d, because "
177 "no TFI available\n", ts);
178 continue;
179 }
180 }
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200181 /* Make sure that an USF is available */
182 if (dir == GPRS_RLCMAC_UL_TBF) {
Maxadca67b2018-01-31 15:22:36 +0100183 usf = find_free_usf(pdch->assigned_usf());
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200184 if (usf < 0) {
185 LOGP(DRLCMAC, LOGL_DEBUG,
186 "- Skipping TS %d, because "
187 "no USF available\n", ts);
188 continue;
189 }
190 }
191 if (min_ts >= 0)
192 LOGP(DRLCMAC, LOGL_DEBUG,
193 "- Skipping TS %d, because "
194 "num TBFs %d > %d\n",
195 min_ts, min_used, num_tbfs);
196 min_used = num_tbfs;
197 min_ts = ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200198 min_tfi = tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200199 min_usf = usf;
200 } else {
201 LOGP(DRLCMAC, LOGL_DEBUG,
202 "- Skipping TS %d, because "
203 "num TBFs %d >= %d\n",
204 ts, num_tbfs, min_used);
205 }
206 }
207
208 if (min_ts < 0)
209 return -1;
210
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200211 if (free_tfi)
212 *free_tfi = min_tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200213 if (free_usf)
214 *free_usf = min_usf;
215
216 return min_ts;
217}
218
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200219static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
220 struct gprs_rlcmac_tbf *tbf)
221{
222 if (tbf->pdch[pdch->ts_no])
223 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
224
225 tbf->pdch[pdch->ts_no] = pdch;
226 pdch->attach_tbf(tbf);
227}
228
Maxa76a7d02018-01-26 11:09:16 +0100229static 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 +0200230{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200231 tbf->m_tfi = tfi;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200232 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200233 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200234}
235
Maxa76a7d02018-01-26 11:09:16 +0100236static 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 +0200237{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200238 tbf->m_tfi = tfi;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200239 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200240}
241
Maxa76a7d02018-01-26 11:09:16 +0100242static int find_trx(const struct gprs_rlcmac_bts *bts_data, const GprsMs *ms, int8_t use_trx)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200243{
244 unsigned trx_no;
245 unsigned ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200246
247 /* We must use the TRX currently actively used by an MS */
248 if (ms && ms->current_trx())
249 return ms->current_trx()->trx_no;
250
251 if (use_trx >= 0 && use_trx < 8)
252 return use_trx;
253
254 /* Find the first TRX that has a PDCH with a free UL and DL TFI */
255 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100256 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200257 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100258 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200259 if (!pdch->is_enabled())
260 continue;
261
Maxd000d802017-09-20 17:55:28 +0200262 if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200263 continue;
264
Maxd000d802017-09-20 17:55:28 +0200265 if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200266 continue;
267
268 return trx_no;
269 }
270 }
271
272 return -EBUSY;
273}
274
Maxa76a7d02018-01-26 11:09:16 +0100275static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts_data)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200276{
277 unsigned trx_no;
278 unsigned ts;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200279
280 /* Find the first PDCH with an unused DL TS */
281 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100282 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200283 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100284 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200285 if (!pdch->is_enabled())
286 continue;
287
288 if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH)
289 continue;
290
Maxa76a7d02018-01-26 11:09:16 +0100291 return true;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200292 }
293 }
294
Maxa76a7d02018-01-26 11:09:16 +0100295 return false;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200296}
297
Maxa76a7d02018-01-26 11:09:16 +0100298/*! Return free TFI
299 *
300 * \param[in] bts Pointer to BTS struct
Max7e4921d2017-09-28 16:41:24 +0200301 * \param[in] trx Optional pointer to TRX struct
Maxa76a7d02018-01-26 11:09:16 +0100302 * \param[in] ms Pointer to MS object
303 * \param[in] dir DL or UL direction
304 * \param[in] use_trx which TRX to use or -1 if it should be selected based on what MS uses
305 * \param[out] trx_no_ TRX number on which TFI was found
306 * \returns negative error code or 0 on success
307 */
308static int tfi_find_free(const BTS *bts, const gprs_rlcmac_trx *trx, const GprsMs *ms,
309 enum gprs_rlcmac_tbf_direction dir, int8_t use_trx, uint8_t *trx_no_)
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200310{
311 int tfi;
312 uint8_t trx_no;
313
Max7e4921d2017-09-28 16:41:24 +0200314 if (trx) {
315 if (use_trx >= 0 && use_trx != trx->trx_no) {
316 LOGP(DRLCMAC, LOGL_ERROR, "- Requested incompatible TRX %d (current is %d)\n",
317 use_trx, trx->trx_no);
318 return -EINVAL;
319 }
320 use_trx = trx->trx_no;
321 }
322
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200323 if (use_trx == -1 && ms->current_trx())
324 use_trx = ms->current_trx()->trx_no;
325
326 tfi = bts->tfi_find_free(dir, &trx_no, use_trx);
327 if (tfi < 0)
328 return -EBUSY;
329
330 if (trx_no_)
331 *trx_no_ = trx_no;
332
333 return tfi;
334}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200335
Maxe9fe0e32017-09-28 15:56:05 +0200336/*! Slot Allocation: Algorithm A
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200337 *
338 * Assign single slot for uplink and downlink
Maxe9fe0e32017-09-28 15:56:05 +0200339 *
340 * \param[in,out] bts Pointer to BTS struct
341 * \param[in,out] ms_ Pointer to MS object
342 * \param[in,out] tbf_ Pointer to TBF struct
343 * \param[in] single flag indicating if we should force single-slot allocation
344 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
345 * \returns negative error code or 0 on success
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200346 */
Maxe9fe0e32017-09-28 15:56:05 +0200347int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
348 int8_t use_trx)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200349{
350 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200351 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200352 uint8_t ul_slots, dl_slots;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200353 int trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200354 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200355 int usf = -1;
356 int mask = 0xff;
357 const char *mask_reason = NULL;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200358 const GprsMs *ms = ms_;
359 const gprs_rlcmac_tbf *tbf = tbf_;
360 gprs_rlcmac_trx *trx = ms->current_trx();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200361
362 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200363 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200364
Maxa76a7d02018-01-26 11:09:16 +0100365 trx_no = find_trx(bts, ms, use_trx);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200366 if (trx_no < 0) {
367 LOGP(DRLCMAC, LOGL_NOTICE,
368 "- Failed to find a usable TRX (TFI exhausted)\n");
369 return trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200370 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200371 if (!trx)
372 trx = &bts->trx[trx_no];
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200373
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200374 dl_slots = ms->reserved_dl_slots();
375 ul_slots = ms->reserved_ul_slots();
376
377 ts = ms->first_common_ts();
378
379 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200380 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200381 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200382 } else if (dl_slots || ul_slots) {
383 mask_reason = "need to use a reserved common TS";
384 mask = dl_slots & ul_slots;
385 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200386
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200387 mask = find_possible_pdchs(trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200388 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200389 return -EINVAL;
390
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200391 ts = find_least_busy_pdch(trx, tbf->direction, mask,
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200392 compute_usage_for_algo_a,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200393 &tfi, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200394
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200395 if (tbf->direction == GPRS_RLCMAC_UL_TBF && usf < 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200396 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200397 "to allocate a TS, no USF available\n");
Jacob Erlbeckec478752015-06-19 16:35:38 +0200398 return -EBUSY;
399 }
400
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200401 if (ts < 0) {
402 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
403 "to allocate a TS, no TFI available\n");
404 return -EBUSY;
405 }
406
407 pdch = &trx->pdch[ts];
408
409 /* The allocation will be successful, so the system state and tbf_/ms_
410 * may be modified from now on. */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200411 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100412 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200413 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d TFI=%d USF=%d\n",
414 ts, tfi, usf);
415 assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200416 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100417 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200418 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d TFI=%d\n",
419 ts, tfi);
420 assign_dlink_tbf(pdch, dl_tbf, tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200421 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200422
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200423 tbf_->trx = trx;
424 /* the only one TS is the common TS */
425 tbf_->first_ts = tbf_->first_common_ts = ts;
426 ms_->set_reserved_slots(trx, 1 << ts, 1 << ts);
427
428 tbf_->upgrade_to_multislot = 0;
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200429 bts->bts->tbf_alloc_algo_a();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200430 return 0;
431}
432
Maxadca67b2018-01-31 15:22:36 +0100433/*! Compute capacity of a given TRX
434 *
435 * \param[in] trx Pointer to TRX object
436 * \param[in] rx_window Receive window
437 * \param[in] tx_window Transmit window
438 * \returns non-negative capacity
439 */
440static inline unsigned compute_capacity(const struct gprs_rlcmac_trx *trx, int rx_window, int tx_window)
441{
442 const struct gprs_rlcmac_pdch *pdch;
443 unsigned ts, capacity = 0;
444
445 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
446 pdch = &trx->pdch[ts];
447 if (rx_window & (1 << ts))
448 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF), 1);
449
450 /* Only consider common slots for UL */
451 if (tx_window & rx_window & (1 << ts)) {
452 if (find_free_usf(pdch->assigned_usf()) >= 0)
453 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF), 1);
454 }
455 }
456
457 return capacity;
458}
459
Maxa76a7d02018-01-26 11:09:16 +0100460/*! Find set of slots available for allocation while taking MS class into account
461 *
462 * \param[in] trx Pointer to TRX object
463 * \param[in] mslot_class The multislot class
464 * \param[in,out] ul_slots set of UL timeslots
465 * \param[in,out] dl_slots set of DL timeslots
466 * \returns negative error code or 0 on success
467 */
Max46fbfce2017-11-01 19:22:25 +0100468int 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 +0200469{
Maxf633b8d2018-01-31 15:28:53 +0100470 uint8_t Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */
471 Sum = mslot_class_get_sum(mslot_class); /* Max number of Tx + Rx slots */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200472 int rx_window, tx_window, pdch_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200473 char slot_info[9] = {0};
Maxf633b8d2018-01-31 15:28:53 +0100474 int max_capacity = -1;
475 uint8_t max_ul_slots = 0, max_dl_slots = 0;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200476 unsigned max_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200477
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200478 unsigned ul_ts, dl_ts;
479 unsigned num_tx;
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200480 unsigned mask_sel;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200481
Max842d7812017-11-01 18:11:24 +0100482 if (mslot_class)
483 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n",
484 mslot_class);
485
Max842d7812017-11-01 18:11:24 +0100486 if (Tx == MS_NA) {
487 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n",
488 mslot_class);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200489 return -EINVAL;
490 }
491
Max842d7812017-11-01 18:11:24 +0100492 max_slots = OSMO_MAX(mslot_class_get_rx(mslot_class), Tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200493
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200494 if (*dl_slots == 0)
495 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200496
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200497 if (*ul_slots == 0)
498 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200499
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200500 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200501
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200502 *dl_slots &= pdch_slots;
503 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200504
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200505 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
506 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
507 *dl_slots, 'D', '.'),
508 *ul_slots, 'U'),
509 *ul_slots & *dl_slots, 'C'));
510
511 /* Check for each UL (TX) slot */
512
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200513 /* Iterate through possible numbers of TX slots */
Max842d7812017-11-01 18:11:24 +0100514 for (num_tx = 1; num_tx <= mslot_class_get_tx(mslot_class); num_tx += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200515 uint16_t tx_valid_win = (1 << num_tx) - 1;
Maxf633b8d2018-01-31 15:28:53 +0100516 uint8_t rx_mask[MASK_TR + 1];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200517
Maxf633b8d2018-01-31 15:28:53 +0100518 mslot_fill_rx_mask(mslot_class, num_tx, rx_mask);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200519
520 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
521 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
522 unsigned tx_slot_count;
523 int max_rx;
524 uint16_t rx_valid_win;
525 uint32_t checked_rx[256/32] = {0};
526
527 /* Wrap valid window */
528 tx_valid_win = (tx_valid_win | tx_valid_win >> 8) & 0xff;
529
530 tx_window = tx_valid_win;
531
532 /* Filter out unavailable slots */
533 tx_window &= *ul_slots;
534
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200535 /* Skip if the the first TS (ul_ts) is not in the set */
536 if ((tx_window & (1 << ul_ts)) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200537 continue;
538
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200539 /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */
540 if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200541 continue;
542
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100543 tx_slot_count = pcu_bitcount(tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200544
Max842d7812017-11-01 18:11:24 +0100545 max_rx = OSMO_MIN(mslot_class_get_rx(mslot_class), Sum - num_tx);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200546 rx_valid_win = (1 << max_rx) - 1;
547
548 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
549 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
550 /* Wrap valid window */
551 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
552
553 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200554 for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200555 unsigned common_slot_count;
556 unsigned req_common_slots;
557 unsigned rx_slot_count;
558 uint16_t rx_bad;
559 uint8_t rx_good;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200560 int capacity;
561
562 /* Filter out bad slots */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200563 rx_bad = (uint16_t)(0xff & ~rx_mask[mask_sel]) << ul_ts;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200564 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xff;
565 rx_good = *dl_slots & ~rx_bad;
566
567 /* TODO: CHECK this calculation -> separate function for unit
568 * testing */
569
570 rx_window = rx_good & rx_valid_win;
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100571 rx_slot_count = pcu_bitcount(rx_window);
Jacob Erlbeckbae33a72015-07-06 14:55:13 +0200572
573#if 0
574 LOGP(DRLCMAC, LOGL_DEBUG, "n_tx=%d, n_rx=%d, mask_sel=%d, "
575 "tx=%02x, rx=%02x, mask=%02x, bad=%02x, good=%02x, "
576 "ul=%02x, dl=%02x\n",
577 tx_slot_count, rx_slot_count, mask_sel,
578 tx_window, rx_window, rx_mask[mask_sel], rx_bad, rx_good,
579 *ul_slots, *dl_slots);
580#endif
581
582 /* Check compliance with TS 45.002, table 6.4.2.2.1 */
583 /* Whether to skip this round doesn not only depend on the bit
584 * sets but also on mask_sel. Therefore this check must be done
585 * before doing the test_and_set_bit shortcut. */
Maxf633b8d2018-01-31 15:28:53 +0100586 if (mslot_class_get_type(mslot_class) == 1) {
Jacob Erlbeckbae33a72015-07-06 14:55:13 +0200587 unsigned slot_sum = rx_slot_count + tx_slot_count;
588 /* Assume down+up/dynamic.
589 * TODO: For ext-dynamic, down only, up only add more
590 * cases.
591 */
592 if (slot_sum <= 6 && tx_slot_count < 3) {
593 if (mask_sel != MASK_TR)
594 /* Skip Tta */
595 continue;
596 } else if (slot_sum > 6 && tx_slot_count < 3) {
597 if (mask_sel != MASK_TT)
598 /* Skip Tra */
599 continue;
600 } else {
601 /* No supported row in table 6.4.2.2.1. */
602#ifdef ENABLE_TS_ALLOC_DEBUG
603 LOGP(DRLCMAC, LOGL_DEBUG,
604 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
605 "combination not supported\n",
606 set_flag_chars(set_flag_chars(set_flag_chars(
607 slot_info,
608 rx_bad, 'x', '.'),
609 rx_window, 'D'),
610 tx_window, 'U'));
611#endif
612 continue;
613 }
614 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200615
616 /* Avoid repeated RX combination check */
617 if (test_and_set_bit(checked_rx, rx_window))
618 continue;
619
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200620 if (!rx_good) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200621#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200622 LOGP(DRLCMAC, LOGL_DEBUG,
623 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
624 "no DL slots available\n",
625 set_flag_chars(set_flag_chars(slot_info,
626 rx_bad, 'x', '.'),
627 tx_window, 'U'));
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200628#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200629 continue;
630 }
631
632 if (!rx_window)
633 continue;
634
635 /* Check number of common slots according to TS 54.002, 6.4.2.2 */
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +0100636 common_slot_count = pcu_bitcount(tx_window & rx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200637 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
Maxf633b8d2018-01-31 15:28:53 +0100638 if (mslot_class_get_type(mslot_class) == 1)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200639 req_common_slots = OSMO_MIN(req_common_slots, 2);
640
641 if (req_common_slots != common_slot_count) {
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200642#ifdef ENABLE_TS_ALLOC_DEBUG
643 LOGP(DRLCMAC, LOGL_DEBUG,
644 "- Skipping DL/UL slots: (TS=0)\"%s\"(TS=7), "
645 "invalid number of common TS: %d (expected %d)\n",
646 set_flag_chars(set_flag_chars(set_flag_chars(
647 slot_info,
648 rx_bad, 'x', '.'),
649 rx_window, 'D'),
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200650 tx_window, 'U'),
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200651 common_slot_count,
652 req_common_slots);
653#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200654 continue;
655 }
656
657 /* Compute capacity */
Maxadca67b2018-01-31 15:22:36 +0100658 capacity = compute_capacity(trx, rx_window, tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200659
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200660#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200661 LOGP(DRLCMAC, LOGL_DEBUG,
662 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
663 "capacity = %d\n",
664 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
665 slot_info,
666 rx_bad, 'x', '.'),
667 rx_window, 'D'),
668 tx_window, 'U'),
669 rx_window & tx_window, 'C'),
670 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200671#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200672
673 if (capacity <= max_capacity)
674 continue;
675
676 max_capacity = capacity;
677 max_ul_slots = tx_window;
678 max_dl_slots = rx_window;
Maxadca67b2018-01-31 15:22:36 +0100679 }
680 }
681 }
682 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200683
684 if (!max_ul_slots || !max_dl_slots) {
685 LOGP(DRLCMAC, LOGL_NOTICE,
686 "No valid UL/DL slot combination found\n");
687 return -EINVAL;
688 }
689
690 *ul_slots = max_ul_slots;
691 *dl_slots = max_dl_slots;
692
693 return 0;
694}
695
Max0cc72122018-01-31 17:00:06 +0100696/*! Count used bits in slots and reserved_slots bitmasks
697 *
698 * \param[in] slots Timeslots in use
699 * \param[in] reserved_slots Reserved timeslots
700 * \param[out] slotcount Number of TS in use
701 * \param[out] avail_count Number of reserved TS
702 */
703static void update_slot_counters(uint8_t slots, uint8_t reserved_slots, uint8_t *slotcount, uint8_t *avail_count)
704{
705 (*slotcount) = pcu_bitcount(slots);
706 (*avail_count) = pcu_bitcount(reserved_slots);
707}
708
709/*! Return slot mask with single TS from a given UL/DL set according to TBF's direction, ts pointer is set to that TS
710 * number or to negative value on error
711 *
712 * \param[in] trx Pointer to TRX object
713 * \param[in] tbf Pointer to TBF object
714 * \param[in] dl_slots set of DL timeslots
715 * \param[in] ul_slots set of UL timeslots
716 * \param[in] ts corresponding TS or -1 for autoselection
717 * \returns slot mask with single UL or DL timeslot number if possible
718 */
719static uint8_t get_single_ts(const gprs_rlcmac_trx *trx, const gprs_rlcmac_tbf *tbf, uint8_t dl_slots, uint8_t ul_slots,
720 int ts)
721{
722 uint8_t ret = dl_slots & ul_slots; /* Make sure to consider the first common slot only */
723
724 if (ts < 0)
725 ts = find_least_busy_pdch(trx, tbf->direction, ret, compute_usage_by_num_tbfs, NULL, NULL);
726
727 if (ts < 0)
728 return ffs(ret);
729
730 return ret & (1 << ts);
731}
732
733/*! Find set of timeslots available for allocation
734 *
735 * \param[in] trx Pointer to TRX object
736 * \param[in] tbf Pointer to TBF object
737 * \param[in] single Flag to force the single TS allocation
738 * \param[in] ul_slots set of UL timeslots
739 * \param[in] dl_slots set of DL timeslots
740 * \param[in] reserved_ul_slots set of reserved UL timeslots
741 * \param[in] reserved_dl_slots set of reserved DL timeslots
742 * \param[in] first_common_ts First TS common for both UL and DL or -1 if unknown
743 * \returns negative error code or selected TS on success
744 */
745static int tbf_select_slot_set(const gprs_rlcmac_tbf *tbf, const gprs_rlcmac_trx *trx, bool single,
746 uint8_t ul_slots, uint8_t dl_slots,
747 uint8_t reserved_ul_slots, uint8_t reserved_dl_slots,
748 int8_t first_common_ts)
749{
750 uint8_t sl = tbf->direction != GPRS_RLCMAC_DL_TBF ? ul_slots : dl_slots;
751 char slot_info[9] = { 0 };
752
753 if (single)
754 sl = get_single_ts(trx, tbf, dl_slots, ul_slots, first_common_ts);
755
756 if (!sl) {
757 LOGP(DRLCMAC, LOGL_NOTICE, "No %s slots available\n",
758 tbf->direction != GPRS_RLCMAC_DL_TBF ? "uplink" : "downlink");
759 return -EINVAL;
760 }
761
762 if (tbf->direction != GPRS_RLCMAC_DL_TBF) {
763 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_ul_slots, 'u'));
764 masked_override_with(slot_info, sl, 'U');
765 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected UL");
766 } else {
767 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_dl_slots, 'd'));
768 masked_override_with(slot_info, sl, 'D');
769 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected DL");
770 }
771
772 LOGPC(DRLCMAC, LOGL_DEBUG, " slots: (TS=0)\"%s\"(TS=7)%s\n", slot_info, single ? ", single" : "");
773
774 return sl;
775}
776
Max2afec6d2018-01-31 17:21:21 +0100777/*! Allocate USF according to a given UL TS mapping
778 *
779 * N. B: this is legacy implementation which ignores given selected_ul_slots
780 * \param[in] trx Pointer to TRX object
781 * \param[in] tbf Pointer to TBF object
782 * \param[in] first_common_ts First TS which is common to both UL and DL
783 * \param[in] selected_ul_slots set of UL timeslots selected for allocation
784 * \param[in] dl_slots set of DL timeslots
785 * \param[out] usf array for allocated USF
786 * \returns updated UL TS or negative on error
787 */
788static int allocate_usf(const gprs_rlcmac_trx *trx, int8_t first_common_ts, uint8_t selected_ul_slots, uint8_t dl_slots,
789 int *usf)
790{
791 int free_usf = -1, ts;
792 uint8_t ul_slots = selected_ul_slots;
793
794 if (first_common_ts >= 0)
795 ul_slots = 1 << first_common_ts;
796 else
797 ul_slots = ul_slots & dl_slots;
798
799 ts = find_least_busy_pdch(trx, GPRS_RLCMAC_UL_TBF, ul_slots, compute_usage_by_num_tbfs, NULL, &free_usf);
800
801 if (free_usf < 0 || ts < 0) {
802 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
803 return -EBUSY;
804 }
805
806 OSMO_ASSERT(ts >= 0 && ts <= 8);
807
808 /* We will stick to that single UL slot, unreserve the others */
809 ul_slots = 1 << ts;
810 usf[ts] = free_usf;
811
812 return ul_slots;
813}
814
Maxe9fe0e32017-09-28 15:56:05 +0200815/*! Slot Allocation: Algorithm B
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200816 *
817 * Assign as many downlink slots as possible.
818 * Assign one uplink slot. (With free USF)
819 *
Maxe9fe0e32017-09-28 15:56:05 +0200820 * \param[in,out] bts Pointer to BTS struct
821 * \param[in,out] ms_ Pointer to MS object
822 * \param[in,out] tbf_ Pointer to TBF struct
823 * \param[in] single flag indicating if we should force single-slot allocation
824 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
825 * \returns negative error code or 0 on success
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200826 */
Maxe9fe0e32017-09-28 15:56:05 +0200827int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
828 int8_t use_trx)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200829{
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200830 uint8_t dl_slots;
831 uint8_t ul_slots;
832 uint8_t reserved_dl_slots;
833 uint8_t reserved_ul_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200834 int8_t first_common_ts;
835 uint8_t slotcount = 0;
Maxa76a7d02018-01-26 11:09:16 +0100836 uint8_t avail_count = 0, trx_no;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200837 char slot_info[9] = {0};
838 int ts;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200839 int first_ts = -1;
840 int usf[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200841 int rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200842 int tfi;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200843 const GprsMs *ms = ms_;
844 const gprs_rlcmac_tbf *tbf = tbf_;
845 gprs_rlcmac_trx *trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200846
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200847 /* Step 1: Get current state from the MS object */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200848
849 if (!ms) {
850 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
851 return -EINVAL;
852 }
853
Max92e9c172017-09-28 16:25:25 +0200854 dl_slots = ms->reserved_dl_slots();
855 ul_slots = ms->reserved_ul_slots();
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200856 first_common_ts = ms->first_common_ts();
857 trx = ms->current_trx();
858
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200859 /* Step 2a: Find usable TRX and TFI */
Maxa76a7d02018-01-26 11:09:16 +0100860 tfi = tfi_find_free(bts->bts, trx, ms, tbf->direction, use_trx, &trx_no);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200861 if (tfi < 0) {
862 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed to allocate a TFI\n");
863 return tfi;
864 }
865
866 /* Step 2b: Reserve slots on the TRX for the MS */
867 if (!trx)
868 trx = &bts->trx[trx_no];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200869
870 if (!dl_slots || !ul_slots) {
Max842d7812017-11-01 18:11:24 +0100871 rc = find_multi_slots(trx, ms->ms_class(), &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100872 if (rc < 0)
873 return rc;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200874 }
875
Max92e9c172017-09-28 16:25:25 +0200876 reserved_dl_slots = dl_slots;
877 reserved_ul_slots = ul_slots;
878
Max0cc72122018-01-31 17:00:06 +0100879 /* Step 3a: Derive the slot set for the current TBF */
880 rc = tbf_select_slot_set(tbf, trx, single, ul_slots, dl_slots, reserved_ul_slots, reserved_dl_slots,
881 first_common_ts);
882 if (rc < 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200883 return -EINVAL;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200884
Max0cc72122018-01-31 17:00:06 +0100885 first_ts = ffs(rc) - 1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200886
Max0cc72122018-01-31 17:00:06 +0100887 /* Step 3b: Derive the slot set for a given direction */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200888 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Max0cc72122018-01-31 17:00:06 +0100889 dl_slots = rc;
890 update_slot_counters(dl_slots, reserved_dl_slots, &slotcount, &avail_count);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200891 } else {
Max2afec6d2018-01-31 17:21:21 +0100892 rc = allocate_usf(trx, first_common_ts, rc, dl_slots, usf);
893 if (rc < 0)
894 return rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200895
Max0cc72122018-01-31 17:00:06 +0100896 /* We will stick to that single UL slot, unreserve the others */
Max2afec6d2018-01-31 17:21:21 +0100897 ul_slots = rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200898 reserved_ul_slots = ul_slots;
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200899
Max0cc72122018-01-31 17:00:06 +0100900 update_slot_counters(ul_slots, reserved_ul_slots, &slotcount, &avail_count);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200901 }
902
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200903 first_common_ts = ffs(dl_slots & ul_slots) - 1;
904
905 if (first_common_ts < 0) {
906 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
907 return -EINVAL;
908 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200909 if (first_ts < 0) {
910 LOGP(DRLCMAC, LOGL_NOTICE, "No first slot available\n");
911 return -EINVAL;
912 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200913
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200914 if (single && slotcount) {
915 tbf_->upgrade_to_multislot = (avail_count > slotcount);
916 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
917 first_ts,
918 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
919 } else {
920 tbf_->upgrade_to_multislot = 0;
921 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
922 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
923 }
924
925 /* The allocation will be successful, so the system state and tbf_/ms_
926 * may be modified from now on. */
927
928 /* Step 4: Update MS and TBF and really allocate the resources */
929
930 /* The reserved slots have changed, update the MS */
931 if (reserved_ul_slots != ms->reserved_ul_slots() ||
932 reserved_dl_slots != ms->reserved_dl_slots())
933 {
934 ms_->set_reserved_slots(trx,
935 reserved_ul_slots, reserved_dl_slots);
936
937 LOGP(DRLCMAC, LOGL_DEBUG,
938 "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
939 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
940 dl_slots, 'D', '.'),
941 ul_slots, 'U'),
942 ul_slots & dl_slots, 'C'));
943 }
944
945 tbf_->trx = trx;
946 tbf_->first_common_ts = first_common_ts;
947 tbf_->first_ts = first_ts;
948
949 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100950 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200951 for (ts = 0; ts < 8; ts++) {
952 if (!(dl_slots & (1 << ts)))
953 continue;
954
955 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS "
956 "%d\n", ts);
957 assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi);
958 }
959 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100960 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200961
962 for (ts = 0; ts < 8; ts++) {
963 if (!(ul_slots & (1 << ts)))
964 continue;
965
966 OSMO_ASSERT(usf[ts] >= 0);
967
968 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS "
969 "%d\n", ts);
970 assign_uplink_tbf_usf(&trx->pdch[ts], ul_tbf,
971 tfi, usf[ts]);
972 }
973 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200974
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200975 bts->bts->tbf_alloc_algo_b();
976
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200977 return 0;
978}
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200979
Maxe9fe0e32017-09-28 15:56:05 +0200980/*! Slot Allocation: Algorithm dynamic
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200981 *
982 * This meta algorithm automatically selects on of the other algorithms based
983 * on the current system state.
984 *
985 * The goal is to support as many MS and TBF as possible. On low usage, the
986 * goal is to provide the highest possible bandwidth per MS.
987 *
Maxe9fe0e32017-09-28 15:56:05 +0200988 * \param[in,out] bts Pointer to BTS struct
989 * \param[in,out] ms_ Pointer to MS object
990 * \param[in,out] tbf_ Pointer to TBF struct
991 * \param[in] single flag indicating if we should force single-slot allocation
992 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
993 * \returns negative error code or 0 on success
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200994 */
Maxe9fe0e32017-09-28 15:56:05 +0200995int alloc_algorithm_dynamic(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
996 int8_t use_trx)
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200997{
998 int rc;
999
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001000 /* Reset load_is_high if there is at least one idle PDCH */
1001 if (bts->multislot_disabled) {
Maxa76a7d02018-01-26 11:09:16 +01001002 bts->multislot_disabled = !idle_pdch_avail(bts);
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001003 if (!bts->multislot_disabled)
1004 LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n");
1005 }
1006
1007 if (!bts->multislot_disabled) {
Maxe9fe0e32017-09-28 15:56:05 +02001008 rc = alloc_algorithm_b(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck77da3552015-07-16 18:33:46 +02001009 if (rc >= 0)
1010 return rc;
1011
1012 if (!bts->multislot_disabled)
1013 LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n");
1014 bts->multislot_disabled = 1;
1015 }
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001016
Maxe9fe0e32017-09-28 15:56:05 +02001017 return alloc_algorithm_a(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck400ec022015-07-14 13:31:48 +02001018}
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001019
1020int gprs_alloc_max_dl_slots_per_ms(struct gprs_rlcmac_bts *bts, uint8_t ms_class)
1021{
Max842d7812017-11-01 18:11:24 +01001022 int rx = mslot_class_get_rx(ms_class);
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001023
1024 if (rx == MS_NA)
1025 rx = 4;
1026
1027 if (bts->alloc_algorithm == alloc_algorithm_a)
1028 return 1;
1029
1030 if (bts->multislot_disabled)
1031 return 1;
1032
1033 return rx;
1034}