blob: 0476d6db85c79844c36807598a3a7901008531d1 [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
Max0e6ac792018-02-19 18:43:01 +010043#define LOGPSL(tbf, level, fmt, args...) LOGP(DRLCMAC, level, "[%s] " fmt, \
44 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL", ## args)
45
46#define LOGPAL(tbf, kind, single, trx_n, level, fmt, args...) LOGPSL(tbf, level, \
47 "algo %s <%s> (suggested TRX: %d): " fmt, \
48 kind, single ? "single" : "multi", trx_n, ## args)
49
Jacob Erlbeckea65c722015-06-22 16:14:23 +020050static char *set_flag_chars(char *buf, uint8_t val, char set_char, char unset_char = 0)
51{
52 int i;
53
54 for (i = 0; i < 8; i += 1, val = val >> 1) {
55 if (val & 1)
56 buf[i] = set_char;
57 else if (unset_char)
58 buf[i] = unset_char;
59 }
60
61 return buf;
62}
63
Max731e2bb2018-02-05 16:15:30 +010064static uint8_t find_possible_pdchs(const struct gprs_rlcmac_trx *trx, uint8_t max_slots, uint8_t mask,
65 const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020066{
Jacob Erlbeckec478752015-06-19 16:35:38 +020067 unsigned ts;
Max731e2bb2018-02-05 16:15:30 +010068 uint8_t valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +020069 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +020070
71 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +010072 const struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020073
74 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020075 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020076 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
77 "not enabled\n", ts);
78 continue;
79 }
Jacob Erlbeckec478752015-06-19 16:35:38 +020080
81 if (((1 << ts) & mask) == 0) {
82 if (mask_reason)
83 LOGP(DRLCMAC, LOGL_DEBUG,
84 "- Skipping TS %d, because %s\n",
85 ts, mask_reason);
86 continue;
87 }
88
Jacob Erlbeck83426b22015-06-30 09:44:05 +020089 if (max_slots > 1) {
90 /* check if TSC changes, see TS 45.002, 6.4.2 */
91 if (last_tsc < 0)
92 last_tsc = pdch->tsc;
93 else if (last_tsc != pdch->tsc) {
94 LOGP(DRLCMAC, LOGL_ERROR,
95 "Skipping TS %d of TRX=%d, because it "
96 "has different TSC than lower TS of TRX. "
97 "In order to allow multislot, all "
98 "slots must be configured with the same "
99 "TSC!\n", ts, trx->trx_no);
100 continue;
101 }
102 }
103
Jacob Erlbeckec478752015-06-19 16:35:38 +0200104 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200105 }
106
Jacob Erlbeckec478752015-06-19 16:35:38 +0200107 return valid_ts_set;
108}
109
Maxa76a7d02018-01-26 11:09:16 +0100110static 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 +0200111{
112 return pdch->num_tbfs(dir);
113}
114
Maxa76a7d02018-01-26 11:09:16 +0100115static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200116{
117 return
118 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
119 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
120}
121
Maxa76a7d02018-01-26 11:09:16 +0100122static 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 +0200123{
124 int usage =
125 pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) +
126 pdch->num_tbfs(GPRS_RLCMAC_UL_TBF) +
127 compute_usage_by_reservation(pdch, dir);
128
Maxd000d802017-09-20 17:55:28 +0200129 if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200130 /* No TFI in the opposite direction, avoid it */
131 usage += 32;
132
133 return usage;
134
135}
136
Maxa76a7d02018-01-26 11:09:16 +0100137/*! Return the TS which corresponds to least busy PDCH
138 *
139 * \param[in] trx Pointer to TRX object
140 * \param[in] dir TBF direction
141 * \param[in] mask set of available timeslots
142 * \param[in] fn Function pointer to function which computes number of associated TBFs
143 * \param[out] free_tfi Free TFI
144 * \param[out] free_usf Free USF
145 * \returns TS number or -1 if unable to find
146 */
147static int find_least_busy_pdch(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t mask,
148 int (*fn)(const struct gprs_rlcmac_pdch *, enum gprs_rlcmac_tbf_direction dir),
149 int *free_tfi = 0, int *free_usf = 0)
Jacob Erlbeckec478752015-06-19 16:35:38 +0200150{
151 unsigned ts;
152 int min_used = INT_MAX;
153 int min_ts = -1;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200154 int min_tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200155 int min_usf = -1;
156
157 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100158 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckec478752015-06-19 16:35:38 +0200159 int num_tbfs;
160 int usf = -1; /* must be signed */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200161 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200162
163 if (((1 << ts) & mask) == 0)
164 continue;
165
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200166 num_tbfs = fn(pdch, dir);
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200167
168 if (num_tbfs < min_used) {
169 /* We have found a candidate */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200170 /* Make sure that a TFI is available */
171 if (free_tfi) {
Maxc5407c72018-02-05 16:11:36 +0100172 tfi = find_free_tfi(pdch->assigned_tfi(dir));
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200173 if (tfi < 0) {
174 LOGP(DRLCMAC, LOGL_DEBUG,
175 "- Skipping TS %d, because "
176 "no TFI available\n", ts);
177 continue;
178 }
179 }
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200180 /* Make sure that an USF is available */
181 if (dir == GPRS_RLCMAC_UL_TBF) {
Maxadca67b2018-01-31 15:22:36 +0100182 usf = find_free_usf(pdch->assigned_usf());
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200183 if (usf < 0) {
184 LOGP(DRLCMAC, LOGL_DEBUG,
185 "- Skipping TS %d, because "
186 "no USF available\n", ts);
187 continue;
188 }
189 }
190 if (min_ts >= 0)
191 LOGP(DRLCMAC, LOGL_DEBUG,
192 "- Skipping TS %d, because "
193 "num TBFs %d > %d\n",
194 min_ts, min_used, num_tbfs);
195 min_used = num_tbfs;
196 min_ts = ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200197 min_tfi = tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200198 min_usf = usf;
199 } else {
200 LOGP(DRLCMAC, LOGL_DEBUG,
201 "- Skipping TS %d, because "
202 "num TBFs %d >= %d\n",
203 ts, num_tbfs, min_used);
204 }
205 }
206
207 if (min_ts < 0)
208 return -1;
209
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200210 if (free_tfi)
211 *free_tfi = min_tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200212 if (free_usf)
213 *free_usf = min_usf;
214
215 return min_ts;
216}
217
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200218static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
219 struct gprs_rlcmac_tbf *tbf)
220{
221 if (tbf->pdch[pdch->ts_no])
222 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
223
224 tbf->pdch[pdch->ts_no] = pdch;
225 pdch->attach_tbf(tbf);
226}
227
Maxa76a7d02018-01-26 11:09:16 +0100228static 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 +0200229{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200230 tbf->m_tfi = tfi;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200231 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200232 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200233}
234
Maxa76a7d02018-01-26 11:09:16 +0100235static 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 +0200236{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200237 tbf->m_tfi = tfi;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200238 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200239}
240
Maxa76a7d02018-01-26 11:09:16 +0100241static int find_trx(const struct gprs_rlcmac_bts *bts_data, const GprsMs *ms, int8_t use_trx)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200242{
243 unsigned trx_no;
244 unsigned ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200245
246 /* We must use the TRX currently actively used by an MS */
247 if (ms && ms->current_trx())
248 return ms->current_trx()->trx_no;
249
250 if (use_trx >= 0 && use_trx < 8)
251 return use_trx;
252
253 /* Find the first TRX that has a PDCH with a free UL and DL TFI */
254 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100255 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200256 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100257 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200258 if (!pdch->is_enabled())
259 continue;
260
Maxd000d802017-09-20 17:55:28 +0200261 if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200262 continue;
263
Maxd000d802017-09-20 17:55:28 +0200264 if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200265 continue;
266
267 return trx_no;
268 }
269 }
270
271 return -EBUSY;
272}
273
Maxa76a7d02018-01-26 11:09:16 +0100274static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts_data)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200275{
276 unsigned trx_no;
277 unsigned ts;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200278
279 /* Find the first PDCH with an unused DL TS */
280 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100281 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200282 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100283 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200284 if (!pdch->is_enabled())
285 continue;
286
287 if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH)
288 continue;
289
Maxa76a7d02018-01-26 11:09:16 +0100290 return true;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200291 }
292 }
293
Maxa76a7d02018-01-26 11:09:16 +0100294 return false;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200295}
296
Maxa76a7d02018-01-26 11:09:16 +0100297/*! Return free TFI
298 *
299 * \param[in] bts Pointer to BTS struct
Max7e4921d2017-09-28 16:41:24 +0200300 * \param[in] trx Optional pointer to TRX struct
Maxa76a7d02018-01-26 11:09:16 +0100301 * \param[in] ms Pointer to MS object
302 * \param[in] dir DL or UL direction
303 * \param[in] use_trx which TRX to use or -1 if it should be selected based on what MS uses
304 * \param[out] trx_no_ TRX number on which TFI was found
305 * \returns negative error code or 0 on success
306 */
307static int tfi_find_free(const BTS *bts, const gprs_rlcmac_trx *trx, const GprsMs *ms,
308 enum gprs_rlcmac_tbf_direction dir, int8_t use_trx, uint8_t *trx_no_)
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200309{
310 int tfi;
311 uint8_t trx_no;
312
Max7e4921d2017-09-28 16:41:24 +0200313 if (trx) {
314 if (use_trx >= 0 && use_trx != trx->trx_no) {
315 LOGP(DRLCMAC, LOGL_ERROR, "- Requested incompatible TRX %d (current is %d)\n",
316 use_trx, trx->trx_no);
317 return -EINVAL;
318 }
319 use_trx = trx->trx_no;
320 }
321
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200322 if (use_trx == -1 && ms->current_trx())
323 use_trx = ms->current_trx()->trx_no;
324
325 tfi = bts->tfi_find_free(dir, &trx_no, use_trx);
326 if (tfi < 0)
327 return -EBUSY;
328
329 if (trx_no_)
330 *trx_no_ = trx_no;
331
332 return tfi;
333}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200334
Maxe9fe0e32017-09-28 15:56:05 +0200335/*! Slot Allocation: Algorithm A
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200336 *
337 * Assign single slot for uplink and downlink
Maxe9fe0e32017-09-28 15:56:05 +0200338 *
339 * \param[in,out] bts Pointer to BTS struct
340 * \param[in,out] ms_ Pointer to MS object
341 * \param[in,out] tbf_ Pointer to TBF struct
342 * \param[in] single flag indicating if we should force single-slot allocation
343 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
344 * \returns negative error code or 0 on success
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200345 */
Maxe9fe0e32017-09-28 15:56:05 +0200346int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
347 int8_t use_trx)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200348{
349 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200350 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200351 uint8_t ul_slots, dl_slots;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200352 int trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200353 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200354 int usf = -1;
Max731e2bb2018-02-05 16:15:30 +0100355 uint8_t mask = 0xff;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200356 const char *mask_reason = NULL;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200357 const GprsMs *ms = ms_;
358 const gprs_rlcmac_tbf *tbf = tbf_;
359 gprs_rlcmac_trx *trx = ms->current_trx();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200360
Max0e6ac792018-02-19 18:43:01 +0100361 LOGPSL(tbf, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class %d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200362
Maxa76a7d02018-01-26 11:09:16 +0100363 trx_no = find_trx(bts, ms, use_trx);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200364 if (trx_no < 0) {
Max0e6ac792018-02-19 18:43:01 +0100365 LOGPAL(tbf, "A", single ? "single" : "multi", use_trx, LOGL_NOTICE,
366 "failed to find a usable TRX (TFI exhausted)\n");
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200367 return trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200368 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200369 if (!trx)
370 trx = &bts->trx[trx_no];
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200371
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200372 dl_slots = ms->reserved_dl_slots();
373 ul_slots = ms->reserved_ul_slots();
374
375 ts = ms->first_common_ts();
376
377 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200378 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200379 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200380 } else if (dl_slots || ul_slots) {
381 mask_reason = "need to use a reserved common TS";
382 mask = dl_slots & ul_slots;
383 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200384
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200385 mask = find_possible_pdchs(trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200386 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200387 return -EINVAL;
388
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200389 ts = find_least_busy_pdch(trx, tbf->direction, mask,
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200390 compute_usage_for_algo_a,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200391 &tfi, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200392
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200393 if (tbf->direction == GPRS_RLCMAC_UL_TBF && usf < 0) {
Max0e6ac792018-02-19 18:43:01 +0100394 LOGPAL(tbf, "A", single ? "single" : "multi", use_trx, LOGL_NOTICE,
395 "failed to allocate a TS, no USF available\n");
Jacob Erlbeckec478752015-06-19 16:35:38 +0200396 return -EBUSY;
397 }
398
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200399 if (ts < 0) {
Max0e6ac792018-02-19 18:43:01 +0100400 LOGPAL(tbf, "A", single ? "single" : "multi", use_trx, LOGL_NOTICE,
401 "failed to allocate a TS, no TFI available\n");
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200402 return -EBUSY;
403 }
404
405 pdch = &trx->pdch[ts];
406
407 /* The allocation will be successful, so the system state and tbf_/ms_
408 * may be modified from now on. */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200409 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100410 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Max0e6ac792018-02-19 18:43:01 +0100411 LOGPSL(tbf, LOGL_DEBUG, "Assign uplink TS=%d TFI=%d USF=%d\n", ts, tfi, usf);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200412 assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200413 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100414 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Max0e6ac792018-02-19 18:43:01 +0100415 LOGPSL(tbf, LOGL_DEBUG, "Assign downlink TS=%d TFI=%d\n", ts, tfi);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200416 assign_dlink_tbf(pdch, dl_tbf, tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200417 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200418
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200419 tbf_->trx = trx;
420 /* the only one TS is the common TS */
421 tbf_->first_ts = tbf_->first_common_ts = ts;
422 ms_->set_reserved_slots(trx, 1 << ts, 1 << ts);
423
424 tbf_->upgrade_to_multislot = 0;
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200425 bts->bts->tbf_alloc_algo_a();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200426 return 0;
427}
428
Maxadca67b2018-01-31 15:22:36 +0100429/*! Compute capacity of a given TRX
430 *
431 * \param[in] trx Pointer to TRX object
432 * \param[in] rx_window Receive window
433 * \param[in] tx_window Transmit window
434 * \returns non-negative capacity
435 */
436static inline unsigned compute_capacity(const struct gprs_rlcmac_trx *trx, int rx_window, int tx_window)
437{
438 const struct gprs_rlcmac_pdch *pdch;
439 unsigned ts, capacity = 0;
440
441 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
442 pdch = &trx->pdch[ts];
443 if (rx_window & (1 << ts))
444 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF), 1);
445
446 /* Only consider common slots for UL */
447 if (tx_window & rx_window & (1 << ts)) {
448 if (find_free_usf(pdch->assigned_usf()) >= 0)
449 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF), 1);
450 }
451 }
452
453 return capacity;
454}
455
Max731e2bb2018-02-05 16:15:30 +0100456/*! Decide if a given slot should be skipped by multislot allocator
457 *
458 * \param[in] ms_class Pointer to MS Class object
459 * \param[in] check_tr Flag indicating whether we should check for Tra or Tta parameters for a given MS class
460 * \param[in] rx_window Receive window
461 * \param[in] tx_window Transmit window
462 * \param[in,out] checked_rx array with already checked RX timeslots
463 * \returns true if the slot should be skipped, false otherwise
464 */
465static bool skip_slot(uint8_t mslot_class, bool check_tr,
466 int16_t rx_window, int16_t tx_window,
467 uint32_t *checked_rx)
468{
469 uint8_t common_slot_count, req_common_slots,
470 rx_slot_count = pcu_bitcount(rx_window),
471 tx_slot_count = pcu_bitcount(tx_window);
472
473 /* Check compliance with TS 45.002, table 6.4.2.2.1 */
474 /* Whether to skip this round doesn not only depend on the bit
475 * sets but also on check_tr. Therefore this check must be done
476 * before doing the mslot_test_and_set_bit shortcut. */
477 if (mslot_class_get_type(mslot_class) == 1) {
478 uint16_t slot_sum = rx_slot_count + tx_slot_count;
479 /* Assume down + up / dynamic.
480 * TODO: For ext-dynamic, down only, up only add more cases.
481 */
482 if (slot_sum <= 6 && tx_slot_count < 3) {
483 if (!check_tr)
484 return true; /* Skip Tta */
485 } else if (slot_sum > 6 && tx_slot_count < 3) {
486 if (check_tr)
487 return true; /* Skip Tra */
488 } else
489 return true; /* No supported row in TS 45.002, table 6.4.2.2.1. */
490 }
491
492 /* Avoid repeated RX combination check */
493 if (mslot_test_and_set_bit(checked_rx, rx_window))
494 return true;
495
496 /* Check number of common slots according to TS 45.002, §6.4.2.2 */
497 common_slot_count = pcu_bitcount(tx_window & rx_window);
498 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
499 if (mslot_class_get_type(mslot_class) == 1)
500 req_common_slots = OSMO_MIN(req_common_slots, 2);
501
502 if (req_common_slots != common_slot_count)
503 return true;
504
505 return false;
506}
507
Maxa76a7d02018-01-26 11:09:16 +0100508/*! Find set of slots available for allocation while taking MS class into account
509 *
510 * \param[in] trx Pointer to TRX object
511 * \param[in] mslot_class The multislot class
512 * \param[in,out] ul_slots set of UL timeslots
513 * \param[in,out] dl_slots set of DL timeslots
514 * \returns negative error code or 0 on success
515 */
Max46fbfce2017-11-01 19:22:25 +0100516int 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 +0200517{
Maxf633b8d2018-01-31 15:28:53 +0100518 uint8_t Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */
Max731e2bb2018-02-05 16:15:30 +0100519 Sum = mslot_class_get_sum(mslot_class), /* Max number of Tx + Rx slots */
520 max_slots, num_tx, mask_sel, pdch_slots, ul_ts, dl_ts;
521 int16_t rx_window, tx_window;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200522 char slot_info[9] = {0};
Maxf633b8d2018-01-31 15:28:53 +0100523 int max_capacity = -1;
524 uint8_t max_ul_slots = 0, max_dl_slots = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200525
Max842d7812017-11-01 18:11:24 +0100526 if (mslot_class)
527 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n",
528 mslot_class);
529
Max842d7812017-11-01 18:11:24 +0100530 if (Tx == MS_NA) {
531 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n",
532 mslot_class);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200533 return -EINVAL;
534 }
535
Max842d7812017-11-01 18:11:24 +0100536 max_slots = OSMO_MAX(mslot_class_get_rx(mslot_class), Tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200537
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200538 if (*dl_slots == 0)
539 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200540
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200541 if (*ul_slots == 0)
542 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200543
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200544 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200545
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200546 *dl_slots &= pdch_slots;
547 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200548
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200549 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
550 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
551 *dl_slots, 'D', '.'),
552 *ul_slots, 'U'),
553 *ul_slots & *dl_slots, 'C'));
554
555 /* Check for each UL (TX) slot */
556
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200557 /* Iterate through possible numbers of TX slots */
Max842d7812017-11-01 18:11:24 +0100558 for (num_tx = 1; num_tx <= mslot_class_get_tx(mslot_class); num_tx += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200559 uint16_t tx_valid_win = (1 << num_tx) - 1;
Maxf633b8d2018-01-31 15:28:53 +0100560 uint8_t rx_mask[MASK_TR + 1];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200561
Maxf633b8d2018-01-31 15:28:53 +0100562 mslot_fill_rx_mask(mslot_class, num_tx, rx_mask);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200563
564 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
565 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200566 uint16_t rx_valid_win;
567 uint32_t checked_rx[256/32] = {0};
568
569 /* Wrap valid window */
Max731e2bb2018-02-05 16:15:30 +0100570 tx_valid_win = mslot_wrap_window(tx_valid_win);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200571
572 tx_window = tx_valid_win;
573
574 /* Filter out unavailable slots */
575 tx_window &= *ul_slots;
576
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200577 /* Skip if the the first TS (ul_ts) is not in the set */
578 if ((tx_window & (1 << ul_ts)) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200579 continue;
580
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200581 /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */
582 if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200583 continue;
584
Max731e2bb2018-02-05 16:15:30 +0100585 rx_valid_win = (1 << OSMO_MIN(mslot_class_get_rx(mslot_class), Sum - num_tx)) - 1;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200586
587 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
588 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
589 /* Wrap valid window */
590 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
591
592 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200593 for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200594 int capacity;
595
Max731e2bb2018-02-05 16:15:30 +0100596 rx_window = mslot_filter_bad(rx_mask[mask_sel], ul_ts, *dl_slots, rx_valid_win);
597 if (rx_window < 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200598 continue;
599
Max731e2bb2018-02-05 16:15:30 +0100600 if (skip_slot(mslot_class, mask_sel != MASK_TT, rx_window, tx_window, checked_rx))
601 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200602
603 /* Compute capacity */
Maxadca67b2018-01-31 15:22:36 +0100604 capacity = compute_capacity(trx, rx_window, tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200605
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200606#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200607 LOGP(DRLCMAC, LOGL_DEBUG,
608 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
609 "capacity = %d\n",
610 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
611 slot_info,
612 rx_bad, 'x', '.'),
613 rx_window, 'D'),
614 tx_window, 'U'),
615 rx_window & tx_window, 'C'),
616 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200617#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200618
619 if (capacity <= max_capacity)
620 continue;
621
622 max_capacity = capacity;
623 max_ul_slots = tx_window;
624 max_dl_slots = rx_window;
Maxadca67b2018-01-31 15:22:36 +0100625 }
626 }
627 }
628 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200629
630 if (!max_ul_slots || !max_dl_slots) {
631 LOGP(DRLCMAC, LOGL_NOTICE,
632 "No valid UL/DL slot combination found\n");
633 return -EINVAL;
634 }
635
636 *ul_slots = max_ul_slots;
637 *dl_slots = max_dl_slots;
638
639 return 0;
640}
641
Max0cc72122018-01-31 17:00:06 +0100642/*! Count used bits in slots and reserved_slots bitmasks
643 *
644 * \param[in] slots Timeslots in use
645 * \param[in] reserved_slots Reserved timeslots
646 * \param[out] slotcount Number of TS in use
647 * \param[out] avail_count Number of reserved TS
648 */
649static void update_slot_counters(uint8_t slots, uint8_t reserved_slots, uint8_t *slotcount, uint8_t *avail_count)
650{
651 (*slotcount) = pcu_bitcount(slots);
652 (*avail_count) = pcu_bitcount(reserved_slots);
653}
654
655/*! Return slot mask with single TS from a given UL/DL set according to TBF's direction, ts pointer is set to that TS
656 * number or to negative value on error
657 *
658 * \param[in] trx Pointer to TRX object
659 * \param[in] tbf Pointer to TBF object
660 * \param[in] dl_slots set of DL timeslots
661 * \param[in] ul_slots set of UL timeslots
662 * \param[in] ts corresponding TS or -1 for autoselection
663 * \returns slot mask with single UL or DL timeslot number if possible
664 */
665static uint8_t get_single_ts(const gprs_rlcmac_trx *trx, const gprs_rlcmac_tbf *tbf, uint8_t dl_slots, uint8_t ul_slots,
666 int ts)
667{
668 uint8_t ret = dl_slots & ul_slots; /* Make sure to consider the first common slot only */
669
670 if (ts < 0)
671 ts = find_least_busy_pdch(trx, tbf->direction, ret, compute_usage_by_num_tbfs, NULL, NULL);
672
673 if (ts < 0)
674 return ffs(ret);
675
676 return ret & (1 << ts);
677}
678
679/*! Find set of timeslots available for allocation
680 *
681 * \param[in] trx Pointer to TRX object
682 * \param[in] tbf Pointer to TBF object
683 * \param[in] single Flag to force the single TS allocation
684 * \param[in] ul_slots set of UL timeslots
685 * \param[in] dl_slots set of DL timeslots
686 * \param[in] reserved_ul_slots set of reserved UL timeslots
687 * \param[in] reserved_dl_slots set of reserved DL timeslots
688 * \param[in] first_common_ts First TS common for both UL and DL or -1 if unknown
689 * \returns negative error code or selected TS on success
690 */
691static int tbf_select_slot_set(const gprs_rlcmac_tbf *tbf, const gprs_rlcmac_trx *trx, bool single,
692 uint8_t ul_slots, uint8_t dl_slots,
693 uint8_t reserved_ul_slots, uint8_t reserved_dl_slots,
694 int8_t first_common_ts)
695{
696 uint8_t sl = tbf->direction != GPRS_RLCMAC_DL_TBF ? ul_slots : dl_slots;
697 char slot_info[9] = { 0 };
698
699 if (single)
700 sl = get_single_ts(trx, tbf, dl_slots, ul_slots, first_common_ts);
701
702 if (!sl) {
703 LOGP(DRLCMAC, LOGL_NOTICE, "No %s slots available\n",
704 tbf->direction != GPRS_RLCMAC_DL_TBF ? "uplink" : "downlink");
705 return -EINVAL;
706 }
707
708 if (tbf->direction != GPRS_RLCMAC_DL_TBF) {
709 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_ul_slots, 'u'));
710 masked_override_with(slot_info, sl, 'U');
711 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected UL");
712 } else {
713 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_dl_slots, 'd'));
714 masked_override_with(slot_info, sl, 'D');
715 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected DL");
716 }
717
Max0e6ac792018-02-19 18:43:01 +0100718 LOGPC(DRLCMAC, LOGL_DEBUG, " slots: (TS=0)\"%s\"(TS=7), %s\n", slot_info, single ? "single" : "multi");
Max0cc72122018-01-31 17:00:06 +0100719
720 return sl;
721}
722
Max2afec6d2018-01-31 17:21:21 +0100723/*! Allocate USF according to a given UL TS mapping
724 *
725 * N. B: this is legacy implementation which ignores given selected_ul_slots
726 * \param[in] trx Pointer to TRX object
727 * \param[in] tbf Pointer to TBF object
728 * \param[in] first_common_ts First TS which is common to both UL and DL
729 * \param[in] selected_ul_slots set of UL timeslots selected for allocation
730 * \param[in] dl_slots set of DL timeslots
731 * \param[out] usf array for allocated USF
732 * \returns updated UL TS or negative on error
733 */
734static int allocate_usf(const gprs_rlcmac_trx *trx, int8_t first_common_ts, uint8_t selected_ul_slots, uint8_t dl_slots,
735 int *usf)
736{
737 int free_usf = -1, ts;
738 uint8_t ul_slots = selected_ul_slots;
739
740 if (first_common_ts >= 0)
741 ul_slots = 1 << first_common_ts;
742 else
743 ul_slots = ul_slots & dl_slots;
744
745 ts = find_least_busy_pdch(trx, GPRS_RLCMAC_UL_TBF, ul_slots, compute_usage_by_num_tbfs, NULL, &free_usf);
746
747 if (free_usf < 0 || ts < 0) {
748 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
749 return -EBUSY;
750 }
751
752 OSMO_ASSERT(ts >= 0 && ts <= 8);
753
754 /* We will stick to that single UL slot, unreserve the others */
755 ul_slots = 1 << ts;
756 usf[ts] = free_usf;
757
758 return ul_slots;
759}
760
Max77988d42018-02-19 18:00:38 +0100761/*! Update MS' reserved timeslots
762 *
763 * \param[in,out] trx Pointer to TRX struct
764 * \param[in,out] ms_ Pointer to MS object
765 * \param[in] tbf_ Pointer to TBF struct
766 * \param[in] res_ul_slots Newly reserved UL slots
767 * \param[in] res_dl_slots Newly reserved DL slots
768 * \param[in] ul_slots available UL slots (for logging only)
769 * \param[in] dl_slots available DL slots (for logging only)
770 */
771static void update_ms_reserved_slots(gprs_rlcmac_trx *trx, GprsMs *ms, uint8_t res_ul_slots, uint8_t res_dl_slots,
772 uint8_t ul_slots, uint8_t dl_slots)
773{
774 char slot_info[9] = { 0 };
775
776 if (res_ul_slots == ms->reserved_ul_slots() && res_dl_slots == ms->reserved_dl_slots())
777 return;
778
779 /* The reserved slots have changed, update the MS */
780 ms->set_reserved_slots(trx, res_ul_slots, res_dl_slots);
781
782 ts_format(slot_info, dl_slots, ul_slots);
783 LOGP(DRLCMAC, LOGL_DEBUG, "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n", slot_info);
784}
785
786/*! Assign given UL timeslots to UL TBF
787 *
788 * \param[in,out] ul_tbf Pointer to UL TBF struct
789 * \param[in,out] trx Pointer to TRX object
790 * \param[in] ul_slots Set of slots to be assigned
791 * \param[in] tfi selected TFI
792 * \param[in] usf selected USF
793 */
794static void assign_ul_tbf_slots(struct gprs_rlcmac_ul_tbf *ul_tbf, gprs_rlcmac_trx *trx, uint8_t ul_slots, int tfi,
795 int *usf)
796{
797 uint8_t ts;
798
799 for (ts = 0; ts < 8; ts++) {
800 if (!(ul_slots & (1 << ts)))
801 continue;
802
803 OSMO_ASSERT(usf[ts] >= 0);
804
805 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS %u\n", ts);
806 assign_uplink_tbf_usf(&trx->pdch[ts], ul_tbf, tfi, usf[ts]);
807 }
808}
809
810/*! Assign given DL timeslots to DL TBF
811 *
812 * \param[in,out] dl_tbf Pointer to DL TBF struct
813 * \param[in,out] trx Pointer to TRX object
814 * \param[in] ul_slots Set of slots to be assigned
815 * \param[in] tfi selected TFI
816 */
817static void assign_dl_tbf_slots(struct gprs_rlcmac_dl_tbf *dl_tbf, gprs_rlcmac_trx *trx, uint8_t dl_slots, int tfi)
818{
819 uint8_t ts;
820
821 for (ts = 0; ts < 8; ts++) {
822 if (!(dl_slots & (1 << ts)))
823 continue;
824
825 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS %u\n", ts);
826 assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi);
827 }
828}
829
Maxe9fe0e32017-09-28 15:56:05 +0200830/*! Slot Allocation: Algorithm B
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200831 *
832 * Assign as many downlink slots as possible.
833 * Assign one uplink slot. (With free USF)
834 *
Maxe9fe0e32017-09-28 15:56:05 +0200835 * \param[in,out] bts Pointer to BTS struct
836 * \param[in,out] ms_ Pointer to MS object
837 * \param[in,out] tbf_ Pointer to TBF struct
838 * \param[in] single flag indicating if we should force single-slot allocation
839 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
840 * \returns negative error code or 0 on success
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200841 */
Maxe9fe0e32017-09-28 15:56:05 +0200842int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
843 int8_t use_trx)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200844{
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200845 uint8_t dl_slots;
846 uint8_t ul_slots;
847 uint8_t reserved_dl_slots;
848 uint8_t reserved_ul_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200849 int8_t first_common_ts;
850 uint8_t slotcount = 0;
Maxa76a7d02018-01-26 11:09:16 +0100851 uint8_t avail_count = 0, trx_no;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200852 int first_ts = -1;
853 int usf[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200854 int rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200855 int tfi;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200856 const GprsMs *ms = ms_;
857 const gprs_rlcmac_tbf *tbf = tbf_;
858 gprs_rlcmac_trx *trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200859
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200860 /* Step 1: Get current state from the MS object */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200861
862 if (!ms) {
863 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
864 return -EINVAL;
865 }
866
Max92e9c172017-09-28 16:25:25 +0200867 dl_slots = ms->reserved_dl_slots();
868 ul_slots = ms->reserved_ul_slots();
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200869 first_common_ts = ms->first_common_ts();
870 trx = ms->current_trx();
871
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200872 /* Step 2a: Find usable TRX and TFI */
Maxa76a7d02018-01-26 11:09:16 +0100873 tfi = tfi_find_free(bts->bts, trx, ms, tbf->direction, use_trx, &trx_no);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200874 if (tfi < 0) {
Max0e6ac792018-02-19 18:43:01 +0100875 LOGPAL(tbf, "B", single, use_trx, LOGL_NOTICE, "failed to allocate a TFI\n");
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200876 return tfi;
877 }
878
879 /* Step 2b: Reserve slots on the TRX for the MS */
880 if (!trx)
881 trx = &bts->trx[trx_no];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200882
883 if (!dl_slots || !ul_slots) {
Max842d7812017-11-01 18:11:24 +0100884 rc = find_multi_slots(trx, ms->ms_class(), &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100885 if (rc < 0)
886 return rc;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200887 }
888
Max92e9c172017-09-28 16:25:25 +0200889 reserved_dl_slots = dl_slots;
890 reserved_ul_slots = ul_slots;
891
Max0cc72122018-01-31 17:00:06 +0100892 /* Step 3a: Derive the slot set for the current TBF */
893 rc = tbf_select_slot_set(tbf, trx, single, ul_slots, dl_slots, reserved_ul_slots, reserved_dl_slots,
894 first_common_ts);
895 if (rc < 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200896 return -EINVAL;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200897
Max0cc72122018-01-31 17:00:06 +0100898 first_ts = ffs(rc) - 1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200899
Max0cc72122018-01-31 17:00:06 +0100900 /* Step 3b: Derive the slot set for a given direction */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200901 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Max0cc72122018-01-31 17:00:06 +0100902 dl_slots = rc;
903 update_slot_counters(dl_slots, reserved_dl_slots, &slotcount, &avail_count);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200904 } else {
Max2afec6d2018-01-31 17:21:21 +0100905 rc = allocate_usf(trx, first_common_ts, rc, dl_slots, usf);
906 if (rc < 0)
907 return rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200908
Max0cc72122018-01-31 17:00:06 +0100909 /* We will stick to that single UL slot, unreserve the others */
Max2afec6d2018-01-31 17:21:21 +0100910 ul_slots = rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200911 reserved_ul_slots = ul_slots;
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200912
Max0cc72122018-01-31 17:00:06 +0100913 update_slot_counters(ul_slots, reserved_ul_slots, &slotcount, &avail_count);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200914 }
915
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200916 first_common_ts = ffs(dl_slots & ul_slots) - 1;
917
918 if (first_common_ts < 0) {
Max0e6ac792018-02-19 18:43:01 +0100919 LOGPAL(tbf, "B", single, use_trx, LOGL_NOTICE, "first common slot unavailable\n");
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200920 return -EINVAL;
921 }
Max0e6ac792018-02-19 18:43:01 +0100922
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200923 if (first_ts < 0) {
Max0e6ac792018-02-19 18:43:01 +0100924 LOGPAL(tbf, "B", single, use_trx, LOGL_NOTICE, "first slot unavailable\n");
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200925 return -EINVAL;
926 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200927
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200928 if (single && slotcount) {
929 tbf_->upgrade_to_multislot = (avail_count > slotcount);
Max0e6ac792018-02-19 18:43:01 +0100930 LOGPAL(tbf, "B", single, use_trx, LOGL_INFO, "using single slot at TS %d\n", first_ts);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200931 } else {
932 tbf_->upgrade_to_multislot = 0;
Max0e6ac792018-02-19 18:43:01 +0100933 LOGPAL(tbf, "B", single, use_trx, LOGL_INFO, "using %d slots\n", slotcount);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200934 }
935
936 /* The allocation will be successful, so the system state and tbf_/ms_
937 * may be modified from now on. */
938
939 /* Step 4: Update MS and TBF and really allocate the resources */
940
Max77988d42018-02-19 18:00:38 +0100941 update_ms_reserved_slots(trx, ms_, reserved_ul_slots, reserved_dl_slots, ul_slots, dl_slots);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200942
943 tbf_->trx = trx;
944 tbf_->first_common_ts = first_common_ts;
945 tbf_->first_ts = first_ts;
946
Max77988d42018-02-19 18:00:38 +0100947 if (tbf->direction == GPRS_RLCMAC_DL_TBF)
948 assign_dl_tbf_slots(as_dl_tbf(tbf_), trx, dl_slots, tfi);
949 else
950 assign_ul_tbf_slots(as_ul_tbf(tbf_), trx, ul_slots, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200951
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200952 bts->bts->tbf_alloc_algo_b();
953
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200954 return 0;
955}
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200956
Maxe9fe0e32017-09-28 15:56:05 +0200957/*! Slot Allocation: Algorithm dynamic
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200958 *
959 * This meta algorithm automatically selects on of the other algorithms based
960 * on the current system state.
961 *
962 * The goal is to support as many MS and TBF as possible. On low usage, the
963 * goal is to provide the highest possible bandwidth per MS.
964 *
Maxe9fe0e32017-09-28 15:56:05 +0200965 * \param[in,out] bts Pointer to BTS struct
966 * \param[in,out] ms_ Pointer to MS object
967 * \param[in,out] tbf_ Pointer to TBF struct
968 * \param[in] single flag indicating if we should force single-slot allocation
969 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
970 * \returns negative error code or 0 on success
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200971 */
Maxe9fe0e32017-09-28 15:56:05 +0200972int alloc_algorithm_dynamic(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
973 int8_t use_trx)
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200974{
975 int rc;
976
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200977 /* Reset load_is_high if there is at least one idle PDCH */
978 if (bts->multislot_disabled) {
Maxa76a7d02018-01-26 11:09:16 +0100979 bts->multislot_disabled = !idle_pdch_avail(bts);
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200980 if (!bts->multislot_disabled)
981 LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n");
982 }
983
984 if (!bts->multislot_disabled) {
Maxe9fe0e32017-09-28 15:56:05 +0200985 rc = alloc_algorithm_b(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200986 if (rc >= 0)
987 return rc;
988
989 if (!bts->multislot_disabled)
990 LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n");
991 bts->multislot_disabled = 1;
992 }
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200993
Maxe9fe0e32017-09-28 15:56:05 +0200994 return alloc_algorithm_a(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200995}
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200996
Max4da38592018-01-31 18:03:49 +0100997int gprs_alloc_max_dl_slots_per_ms(const struct gprs_rlcmac_bts *bts, uint8_t ms_class)
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200998{
Max842d7812017-11-01 18:11:24 +0100999 int rx = mslot_class_get_rx(ms_class);
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001000
1001 if (rx == MS_NA)
1002 rx = 4;
1003
1004 if (bts->alloc_algorithm == alloc_algorithm_a)
1005 return 1;
1006
1007 if (bts->multislot_disabled)
1008 return 1;
1009
1010 return rx;
1011}