blob: 835c199b6383121f9f97dfd4aa081883ad54165a [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
Max731e2bb2018-02-05 16:15:30 +010057static uint8_t find_possible_pdchs(const struct gprs_rlcmac_trx *trx, uint8_t max_slots, uint8_t mask,
58 const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020059{
Jacob Erlbeckec478752015-06-19 16:35:38 +020060 unsigned ts;
Max731e2bb2018-02-05 16:15:30 +010061 uint8_t valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +020062 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +020063
64 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +010065 const struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020066
67 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020068 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020069 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
70 "not enabled\n", ts);
71 continue;
72 }
Jacob Erlbeckec478752015-06-19 16:35:38 +020073
74 if (((1 << ts) & mask) == 0) {
75 if (mask_reason)
76 LOGP(DRLCMAC, LOGL_DEBUG,
77 "- Skipping TS %d, because %s\n",
78 ts, mask_reason);
79 continue;
80 }
81
Jacob Erlbeck83426b22015-06-30 09:44:05 +020082 if (max_slots > 1) {
83 /* check if TSC changes, see TS 45.002, 6.4.2 */
84 if (last_tsc < 0)
85 last_tsc = pdch->tsc;
86 else if (last_tsc != pdch->tsc) {
87 LOGP(DRLCMAC, LOGL_ERROR,
88 "Skipping TS %d of TRX=%d, because it "
89 "has different TSC than lower TS of TRX. "
90 "In order to allow multislot, all "
91 "slots must be configured with the same "
92 "TSC!\n", ts, trx->trx_no);
93 continue;
94 }
95 }
96
Jacob Erlbeckec478752015-06-19 16:35:38 +020097 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020098 }
99
Jacob Erlbeckec478752015-06-19 16:35:38 +0200100 return valid_ts_set;
101}
102
Maxa76a7d02018-01-26 11:09:16 +0100103static 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 +0200104{
105 return pdch->num_tbfs(dir);
106}
107
Maxa76a7d02018-01-26 11:09:16 +0100108static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200109{
110 return
111 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
112 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
113}
114
Maxa76a7d02018-01-26 11:09:16 +0100115static 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 +0200116{
117 int usage =
118 pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) +
119 pdch->num_tbfs(GPRS_RLCMAC_UL_TBF) +
120 compute_usage_by_reservation(pdch, dir);
121
Maxd000d802017-09-20 17:55:28 +0200122 if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200123 /* No TFI in the opposite direction, avoid it */
124 usage += 32;
125
126 return usage;
127
128}
129
Maxa76a7d02018-01-26 11:09:16 +0100130/*! Return the TS which corresponds to least busy PDCH
131 *
132 * \param[in] trx Pointer to TRX object
133 * \param[in] dir TBF direction
134 * \param[in] mask set of available timeslots
135 * \param[in] fn Function pointer to function which computes number of associated TBFs
136 * \param[out] free_tfi Free TFI
137 * \param[out] free_usf Free USF
138 * \returns TS number or -1 if unable to find
139 */
140static int find_least_busy_pdch(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t mask,
141 int (*fn)(const struct gprs_rlcmac_pdch *, enum gprs_rlcmac_tbf_direction dir),
142 int *free_tfi = 0, int *free_usf = 0)
Jacob Erlbeckec478752015-06-19 16:35:38 +0200143{
144 unsigned ts;
145 int min_used = INT_MAX;
146 int min_ts = -1;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200147 int min_tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200148 int min_usf = -1;
149
150 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100151 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckec478752015-06-19 16:35:38 +0200152 int num_tbfs;
153 int usf = -1; /* must be signed */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200154 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200155
156 if (((1 << ts) & mask) == 0)
157 continue;
158
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200159 num_tbfs = fn(pdch, dir);
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200160
161 if (num_tbfs < min_used) {
162 /* We have found a candidate */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200163 /* Make sure that a TFI is available */
164 if (free_tfi) {
Maxc5407c72018-02-05 16:11:36 +0100165 tfi = find_free_tfi(pdch->assigned_tfi(dir));
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200166 if (tfi < 0) {
167 LOGP(DRLCMAC, LOGL_DEBUG,
168 "- Skipping TS %d, because "
169 "no TFI available\n", ts);
170 continue;
171 }
172 }
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200173 /* Make sure that an USF is available */
174 if (dir == GPRS_RLCMAC_UL_TBF) {
Maxadca67b2018-01-31 15:22:36 +0100175 usf = find_free_usf(pdch->assigned_usf());
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200176 if (usf < 0) {
177 LOGP(DRLCMAC, LOGL_DEBUG,
178 "- Skipping TS %d, because "
179 "no USF available\n", ts);
180 continue;
181 }
182 }
183 if (min_ts >= 0)
184 LOGP(DRLCMAC, LOGL_DEBUG,
185 "- Skipping TS %d, because "
186 "num TBFs %d > %d\n",
187 min_ts, min_used, num_tbfs);
188 min_used = num_tbfs;
189 min_ts = ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200190 min_tfi = tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200191 min_usf = usf;
192 } else {
193 LOGP(DRLCMAC, LOGL_DEBUG,
194 "- Skipping TS %d, because "
195 "num TBFs %d >= %d\n",
196 ts, num_tbfs, min_used);
197 }
198 }
199
200 if (min_ts < 0)
201 return -1;
202
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200203 if (free_tfi)
204 *free_tfi = min_tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200205 if (free_usf)
206 *free_usf = min_usf;
207
208 return min_ts;
209}
210
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200211static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
212 struct gprs_rlcmac_tbf *tbf)
213{
214 if (tbf->pdch[pdch->ts_no])
215 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
216
217 tbf->pdch[pdch->ts_no] = pdch;
218 pdch->attach_tbf(tbf);
219}
220
Maxa76a7d02018-01-26 11:09:16 +0100221static 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 +0200222{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200223 tbf->m_tfi = tfi;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200224 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200225 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200226}
227
Maxa76a7d02018-01-26 11:09:16 +0100228static 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 +0200229{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200230 tbf->m_tfi = tfi;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200231 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200232}
233
Maxa76a7d02018-01-26 11:09:16 +0100234static int find_trx(const struct gprs_rlcmac_bts *bts_data, const GprsMs *ms, int8_t use_trx)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200235{
236 unsigned trx_no;
237 unsigned ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200238
239 /* We must use the TRX currently actively used by an MS */
240 if (ms && ms->current_trx())
241 return ms->current_trx()->trx_no;
242
243 if (use_trx >= 0 && use_trx < 8)
244 return use_trx;
245
246 /* Find the first TRX that has a PDCH with a free UL and DL TFI */
247 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100248 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200249 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100250 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200251 if (!pdch->is_enabled())
252 continue;
253
Maxd000d802017-09-20 17:55:28 +0200254 if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200255 continue;
256
Maxd000d802017-09-20 17:55:28 +0200257 if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200258 continue;
259
260 return trx_no;
261 }
262 }
263
264 return -EBUSY;
265}
266
Maxa76a7d02018-01-26 11:09:16 +0100267static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts_data)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200268{
269 unsigned trx_no;
270 unsigned ts;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200271
272 /* Find the first PDCH with an unused DL TS */
273 for (trx_no = 0; trx_no < ARRAY_SIZE(bts_data->trx); trx_no += 1) {
Maxa76a7d02018-01-26 11:09:16 +0100274 const struct gprs_rlcmac_trx *trx = &bts_data->trx[trx_no];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200275 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100276 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200277 if (!pdch->is_enabled())
278 continue;
279
280 if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH)
281 continue;
282
Maxa76a7d02018-01-26 11:09:16 +0100283 return true;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200284 }
285 }
286
Maxa76a7d02018-01-26 11:09:16 +0100287 return false;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200288}
289
Maxa76a7d02018-01-26 11:09:16 +0100290/*! Return free TFI
291 *
292 * \param[in] bts Pointer to BTS struct
Max7e4921d2017-09-28 16:41:24 +0200293 * \param[in] trx Optional pointer to TRX struct
Maxa76a7d02018-01-26 11:09:16 +0100294 * \param[in] ms Pointer to MS object
295 * \param[in] dir DL or UL direction
296 * \param[in] use_trx which TRX to use or -1 if it should be selected based on what MS uses
297 * \param[out] trx_no_ TRX number on which TFI was found
298 * \returns negative error code or 0 on success
299 */
300static int tfi_find_free(const BTS *bts, const gprs_rlcmac_trx *trx, const GprsMs *ms,
301 enum gprs_rlcmac_tbf_direction dir, int8_t use_trx, uint8_t *trx_no_)
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200302{
303 int tfi;
304 uint8_t trx_no;
305
Max7e4921d2017-09-28 16:41:24 +0200306 if (trx) {
307 if (use_trx >= 0 && use_trx != trx->trx_no) {
308 LOGP(DRLCMAC, LOGL_ERROR, "- Requested incompatible TRX %d (current is %d)\n",
309 use_trx, trx->trx_no);
310 return -EINVAL;
311 }
312 use_trx = trx->trx_no;
313 }
314
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200315 if (use_trx == -1 && ms->current_trx())
316 use_trx = ms->current_trx()->trx_no;
317
318 tfi = bts->tfi_find_free(dir, &trx_no, use_trx);
319 if (tfi < 0)
320 return -EBUSY;
321
322 if (trx_no_)
323 *trx_no_ = trx_no;
324
325 return tfi;
326}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200327
Maxe9fe0e32017-09-28 15:56:05 +0200328/*! Slot Allocation: Algorithm A
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200329 *
330 * Assign single slot for uplink and downlink
Maxe9fe0e32017-09-28 15:56:05 +0200331 *
332 * \param[in,out] bts Pointer to BTS struct
333 * \param[in,out] ms_ Pointer to MS object
334 * \param[in,out] tbf_ Pointer to TBF struct
335 * \param[in] single flag indicating if we should force single-slot allocation
336 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
337 * \returns negative error code or 0 on success
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200338 */
Maxe9fe0e32017-09-28 15:56:05 +0200339int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
340 int8_t use_trx)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200341{
342 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200343 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200344 uint8_t ul_slots, dl_slots;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200345 int trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200346 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200347 int usf = -1;
Max731e2bb2018-02-05 16:15:30 +0100348 uint8_t mask = 0xff;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200349 const char *mask_reason = NULL;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200350 const GprsMs *ms = ms_;
351 const gprs_rlcmac_tbf *tbf = tbf_;
352 gprs_rlcmac_trx *trx = ms->current_trx();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200353
354 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
Jacob Erlbeckbefc7602015-06-02 12:33:30 +0200355 "%d\n", tbf->ms_class());
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200356
Maxa76a7d02018-01-26 11:09:16 +0100357 trx_no = find_trx(bts, ms, use_trx);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200358 if (trx_no < 0) {
359 LOGP(DRLCMAC, LOGL_NOTICE,
360 "- Failed to find a usable TRX (TFI exhausted)\n");
361 return trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200362 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200363 if (!trx)
364 trx = &bts->trx[trx_no];
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200365
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200366 dl_slots = ms->reserved_dl_slots();
367 ul_slots = ms->reserved_ul_slots();
368
369 ts = ms->first_common_ts();
370
371 if (ts >= 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200372 mask_reason = "need to reuse TS";
Jacob Erlbeckec478752015-06-19 16:35:38 +0200373 mask = 1 << ts;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200374 } else if (dl_slots || ul_slots) {
375 mask_reason = "need to use a reserved common TS";
376 mask = dl_slots & ul_slots;
377 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200378
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200379 mask = find_possible_pdchs(trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200380 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200381 return -EINVAL;
382
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200383 ts = find_least_busy_pdch(trx, tbf->direction, mask,
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200384 compute_usage_for_algo_a,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200385 &tfi, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200386
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200387 if (tbf->direction == GPRS_RLCMAC_UL_TBF && usf < 0) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200388 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200389 "to allocate a TS, no USF available\n");
Jacob Erlbeckec478752015-06-19 16:35:38 +0200390 return -EBUSY;
391 }
392
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200393 if (ts < 0) {
394 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed "
395 "to allocate a TS, no TFI available\n");
396 return -EBUSY;
397 }
398
399 pdch = &trx->pdch[ts];
400
401 /* The allocation will be successful, so the system state and tbf_/ms_
402 * may be modified from now on. */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200403 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100404 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200405 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink TS=%d TFI=%d USF=%d\n",
406 ts, tfi, usf);
407 assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200408 } else {
Jacob Erlbeckaa9daa12015-12-28 18:49:12 +0100409 struct gprs_rlcmac_dl_tbf *dl_tbf = as_dl_tbf(tbf_);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200410 LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d TFI=%d\n",
411 ts, tfi);
412 assign_dlink_tbf(pdch, dl_tbf, tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200413 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200414
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200415 tbf_->trx = trx;
416 /* the only one TS is the common TS */
417 tbf_->first_ts = tbf_->first_common_ts = ts;
418 ms_->set_reserved_slots(trx, 1 << ts, 1 << ts);
419
420 tbf_->upgrade_to_multislot = 0;
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200421 bts->bts->tbf_alloc_algo_a();
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200422 return 0;
423}
424
Maxadca67b2018-01-31 15:22:36 +0100425/*! Compute capacity of a given TRX
426 *
427 * \param[in] trx Pointer to TRX object
428 * \param[in] rx_window Receive window
429 * \param[in] tx_window Transmit window
430 * \returns non-negative capacity
431 */
432static inline unsigned compute_capacity(const struct gprs_rlcmac_trx *trx, int rx_window, int tx_window)
433{
434 const struct gprs_rlcmac_pdch *pdch;
435 unsigned ts, capacity = 0;
436
437 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
438 pdch = &trx->pdch[ts];
439 if (rx_window & (1 << ts))
440 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF), 1);
441
442 /* Only consider common slots for UL */
443 if (tx_window & rx_window & (1 << ts)) {
444 if (find_free_usf(pdch->assigned_usf()) >= 0)
445 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF), 1);
446 }
447 }
448
449 return capacity;
450}
451
Max731e2bb2018-02-05 16:15:30 +0100452/*! Decide if a given slot should be skipped by multislot allocator
453 *
454 * \param[in] ms_class Pointer to MS Class object
455 * \param[in] check_tr Flag indicating whether we should check for Tra or Tta parameters for a given MS class
456 * \param[in] rx_window Receive window
457 * \param[in] tx_window Transmit window
458 * \param[in,out] checked_rx array with already checked RX timeslots
459 * \returns true if the slot should be skipped, false otherwise
460 */
461static bool skip_slot(uint8_t mslot_class, bool check_tr,
462 int16_t rx_window, int16_t tx_window,
463 uint32_t *checked_rx)
464{
465 uint8_t common_slot_count, req_common_slots,
466 rx_slot_count = pcu_bitcount(rx_window),
467 tx_slot_count = pcu_bitcount(tx_window);
468
469 /* Check compliance with TS 45.002, table 6.4.2.2.1 */
470 /* Whether to skip this round doesn not only depend on the bit
471 * sets but also on check_tr. Therefore this check must be done
472 * before doing the mslot_test_and_set_bit shortcut. */
473 if (mslot_class_get_type(mslot_class) == 1) {
474 uint16_t slot_sum = rx_slot_count + tx_slot_count;
475 /* Assume down + up / dynamic.
476 * TODO: For ext-dynamic, down only, up only add more cases.
477 */
478 if (slot_sum <= 6 && tx_slot_count < 3) {
479 if (!check_tr)
480 return true; /* Skip Tta */
481 } else if (slot_sum > 6 && tx_slot_count < 3) {
482 if (check_tr)
483 return true; /* Skip Tra */
484 } else
485 return true; /* No supported row in TS 45.002, table 6.4.2.2.1. */
486 }
487
488 /* Avoid repeated RX combination check */
489 if (mslot_test_and_set_bit(checked_rx, rx_window))
490 return true;
491
492 /* Check number of common slots according to TS 45.002, §6.4.2.2 */
493 common_slot_count = pcu_bitcount(tx_window & rx_window);
494 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
495 if (mslot_class_get_type(mslot_class) == 1)
496 req_common_slots = OSMO_MIN(req_common_slots, 2);
497
498 if (req_common_slots != common_slot_count)
499 return true;
500
501 return false;
502}
503
Maxa76a7d02018-01-26 11:09:16 +0100504/*! Find set of slots available for allocation while taking MS class into account
505 *
506 * \param[in] trx Pointer to TRX object
507 * \param[in] mslot_class The multislot class
508 * \param[in,out] ul_slots set of UL timeslots
509 * \param[in,out] dl_slots set of DL timeslots
510 * \returns negative error code or 0 on success
511 */
Max46fbfce2017-11-01 19:22:25 +0100512int 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 +0200513{
Maxf633b8d2018-01-31 15:28:53 +0100514 uint8_t Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */
Max731e2bb2018-02-05 16:15:30 +0100515 Sum = mslot_class_get_sum(mslot_class), /* Max number of Tx + Rx slots */
516 max_slots, num_tx, mask_sel, pdch_slots, ul_ts, dl_ts;
517 int16_t rx_window, tx_window;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200518 char slot_info[9] = {0};
Maxf633b8d2018-01-31 15:28:53 +0100519 int max_capacity = -1;
520 uint8_t max_ul_slots = 0, max_dl_slots = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200521
Max842d7812017-11-01 18:11:24 +0100522 if (mslot_class)
523 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n",
524 mslot_class);
525
Max842d7812017-11-01 18:11:24 +0100526 if (Tx == MS_NA) {
527 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n",
528 mslot_class);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200529 return -EINVAL;
530 }
531
Max842d7812017-11-01 18:11:24 +0100532 max_slots = OSMO_MAX(mslot_class_get_rx(mslot_class), Tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200533
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200534 if (*dl_slots == 0)
535 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200536
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200537 if (*ul_slots == 0)
538 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200539
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200540 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200541
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200542 *dl_slots &= pdch_slots;
543 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200544
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200545 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
546 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
547 *dl_slots, 'D', '.'),
548 *ul_slots, 'U'),
549 *ul_slots & *dl_slots, 'C'));
550
551 /* Check for each UL (TX) slot */
552
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200553 /* Iterate through possible numbers of TX slots */
Max842d7812017-11-01 18:11:24 +0100554 for (num_tx = 1; num_tx <= mslot_class_get_tx(mslot_class); num_tx += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200555 uint16_t tx_valid_win = (1 << num_tx) - 1;
Maxf633b8d2018-01-31 15:28:53 +0100556 uint8_t rx_mask[MASK_TR + 1];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200557
Maxf633b8d2018-01-31 15:28:53 +0100558 mslot_fill_rx_mask(mslot_class, num_tx, rx_mask);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200559
560 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
561 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200562 uint16_t rx_valid_win;
563 uint32_t checked_rx[256/32] = {0};
564
565 /* Wrap valid window */
Max731e2bb2018-02-05 16:15:30 +0100566 tx_valid_win = mslot_wrap_window(tx_valid_win);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200567
568 tx_window = tx_valid_win;
569
570 /* Filter out unavailable slots */
571 tx_window &= *ul_slots;
572
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200573 /* Skip if the the first TS (ul_ts) is not in the set */
574 if ((tx_window & (1 << ul_ts)) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200575 continue;
576
Jacob Erlbecke21b79c2015-07-16 11:48:43 +0200577 /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */
578 if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200579 continue;
580
Max731e2bb2018-02-05 16:15:30 +0100581 rx_valid_win = (1 << OSMO_MIN(mslot_class_get_rx(mslot_class), Sum - num_tx)) - 1;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200582
583 /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */
584 for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) {
585 /* Wrap valid window */
586 rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff;
587
588 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
Jacob Erlbeck5e46a202015-07-09 11:48:23 +0200589 for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200590 int capacity;
591
Max731e2bb2018-02-05 16:15:30 +0100592 rx_window = mslot_filter_bad(rx_mask[mask_sel], ul_ts, *dl_slots, rx_valid_win);
593 if (rx_window < 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200594 continue;
595
Max731e2bb2018-02-05 16:15:30 +0100596 if (skip_slot(mslot_class, mask_sel != MASK_TT, rx_window, tx_window, checked_rx))
597 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200598
599 /* Compute capacity */
Maxadca67b2018-01-31 15:22:36 +0100600 capacity = compute_capacity(trx, rx_window, tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200601
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200602#ifdef ENABLE_TS_ALLOC_DEBUG
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200603 LOGP(DRLCMAC, LOGL_DEBUG,
604 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
605 "capacity = %d\n",
606 set_flag_chars(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 rx_window & tx_window, 'C'),
612 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200613#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200614
615 if (capacity <= max_capacity)
616 continue;
617
618 max_capacity = capacity;
619 max_ul_slots = tx_window;
620 max_dl_slots = rx_window;
Maxadca67b2018-01-31 15:22:36 +0100621 }
622 }
623 }
624 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200625
626 if (!max_ul_slots || !max_dl_slots) {
627 LOGP(DRLCMAC, LOGL_NOTICE,
628 "No valid UL/DL slot combination found\n");
629 return -EINVAL;
630 }
631
632 *ul_slots = max_ul_slots;
633 *dl_slots = max_dl_slots;
634
635 return 0;
636}
637
Max0cc72122018-01-31 17:00:06 +0100638/*! Count used bits in slots and reserved_slots bitmasks
639 *
640 * \param[in] slots Timeslots in use
641 * \param[in] reserved_slots Reserved timeslots
642 * \param[out] slotcount Number of TS in use
643 * \param[out] avail_count Number of reserved TS
644 */
645static void update_slot_counters(uint8_t slots, uint8_t reserved_slots, uint8_t *slotcount, uint8_t *avail_count)
646{
647 (*slotcount) = pcu_bitcount(slots);
648 (*avail_count) = pcu_bitcount(reserved_slots);
649}
650
651/*! Return slot mask with single TS from a given UL/DL set according to TBF's direction, ts pointer is set to that TS
652 * number or to negative value on error
653 *
654 * \param[in] trx Pointer to TRX object
655 * \param[in] tbf Pointer to TBF object
656 * \param[in] dl_slots set of DL timeslots
657 * \param[in] ul_slots set of UL timeslots
658 * \param[in] ts corresponding TS or -1 for autoselection
659 * \returns slot mask with single UL or DL timeslot number if possible
660 */
661static uint8_t get_single_ts(const gprs_rlcmac_trx *trx, const gprs_rlcmac_tbf *tbf, uint8_t dl_slots, uint8_t ul_slots,
662 int ts)
663{
664 uint8_t ret = dl_slots & ul_slots; /* Make sure to consider the first common slot only */
665
666 if (ts < 0)
667 ts = find_least_busy_pdch(trx, tbf->direction, ret, compute_usage_by_num_tbfs, NULL, NULL);
668
669 if (ts < 0)
670 return ffs(ret);
671
672 return ret & (1 << ts);
673}
674
675/*! Find set of timeslots available for allocation
676 *
677 * \param[in] trx Pointer to TRX object
678 * \param[in] tbf Pointer to TBF object
679 * \param[in] single Flag to force the single TS allocation
680 * \param[in] ul_slots set of UL timeslots
681 * \param[in] dl_slots set of DL timeslots
682 * \param[in] reserved_ul_slots set of reserved UL timeslots
683 * \param[in] reserved_dl_slots set of reserved DL timeslots
684 * \param[in] first_common_ts First TS common for both UL and DL or -1 if unknown
685 * \returns negative error code or selected TS on success
686 */
687static int tbf_select_slot_set(const gprs_rlcmac_tbf *tbf, const gprs_rlcmac_trx *trx, bool single,
688 uint8_t ul_slots, uint8_t dl_slots,
689 uint8_t reserved_ul_slots, uint8_t reserved_dl_slots,
690 int8_t first_common_ts)
691{
692 uint8_t sl = tbf->direction != GPRS_RLCMAC_DL_TBF ? ul_slots : dl_slots;
693 char slot_info[9] = { 0 };
694
695 if (single)
696 sl = get_single_ts(trx, tbf, dl_slots, ul_slots, first_common_ts);
697
698 if (!sl) {
699 LOGP(DRLCMAC, LOGL_NOTICE, "No %s slots available\n",
700 tbf->direction != GPRS_RLCMAC_DL_TBF ? "uplink" : "downlink");
701 return -EINVAL;
702 }
703
704 if (tbf->direction != GPRS_RLCMAC_DL_TBF) {
705 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_ul_slots, 'u'));
706 masked_override_with(slot_info, sl, 'U');
707 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected UL");
708 } else {
709 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_dl_slots, 'd'));
710 masked_override_with(slot_info, sl, 'D');
711 LOGPC(DRLCMAC, LOGL_DEBUG, "- Selected DL");
712 }
713
714 LOGPC(DRLCMAC, LOGL_DEBUG, " slots: (TS=0)\"%s\"(TS=7)%s\n", slot_info, single ? ", single" : "");
715
716 return sl;
717}
718
Max2afec6d2018-01-31 17:21:21 +0100719/*! Allocate USF according to a given UL TS mapping
720 *
721 * N. B: this is legacy implementation which ignores given selected_ul_slots
722 * \param[in] trx Pointer to TRX object
723 * \param[in] tbf Pointer to TBF object
724 * \param[in] first_common_ts First TS which is common to both UL and DL
725 * \param[in] selected_ul_slots set of UL timeslots selected for allocation
726 * \param[in] dl_slots set of DL timeslots
727 * \param[out] usf array for allocated USF
728 * \returns updated UL TS or negative on error
729 */
730static int allocate_usf(const gprs_rlcmac_trx *trx, int8_t first_common_ts, uint8_t selected_ul_slots, uint8_t dl_slots,
731 int *usf)
732{
733 int free_usf = -1, ts;
734 uint8_t ul_slots = selected_ul_slots;
735
736 if (first_common_ts >= 0)
737 ul_slots = 1 << first_common_ts;
738 else
739 ul_slots = ul_slots & dl_slots;
740
741 ts = find_least_busy_pdch(trx, GPRS_RLCMAC_UL_TBF, ul_slots, compute_usage_by_num_tbfs, NULL, &free_usf);
742
743 if (free_usf < 0 || ts < 0) {
744 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
745 return -EBUSY;
746 }
747
748 OSMO_ASSERT(ts >= 0 && ts <= 8);
749
750 /* We will stick to that single UL slot, unreserve the others */
751 ul_slots = 1 << ts;
752 usf[ts] = free_usf;
753
754 return ul_slots;
755}
756
Max77988d42018-02-19 18:00:38 +0100757/*! Update MS' reserved timeslots
758 *
759 * \param[in,out] trx Pointer to TRX struct
760 * \param[in,out] ms_ Pointer to MS object
761 * \param[in] tbf_ Pointer to TBF struct
762 * \param[in] res_ul_slots Newly reserved UL slots
763 * \param[in] res_dl_slots Newly reserved DL slots
764 * \param[in] ul_slots available UL slots (for logging only)
765 * \param[in] dl_slots available DL slots (for logging only)
766 */
767static void update_ms_reserved_slots(gprs_rlcmac_trx *trx, GprsMs *ms, uint8_t res_ul_slots, uint8_t res_dl_slots,
768 uint8_t ul_slots, uint8_t dl_slots)
769{
770 char slot_info[9] = { 0 };
771
772 if (res_ul_slots == ms->reserved_ul_slots() && res_dl_slots == ms->reserved_dl_slots())
773 return;
774
775 /* The reserved slots have changed, update the MS */
776 ms->set_reserved_slots(trx, res_ul_slots, res_dl_slots);
777
778 ts_format(slot_info, dl_slots, ul_slots);
779 LOGP(DRLCMAC, LOGL_DEBUG, "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n", slot_info);
780}
781
782/*! Assign given UL timeslots to UL TBF
783 *
784 * \param[in,out] ul_tbf Pointer to UL TBF struct
785 * \param[in,out] trx Pointer to TRX object
786 * \param[in] ul_slots Set of slots to be assigned
787 * \param[in] tfi selected TFI
788 * \param[in] usf selected USF
789 */
790static void assign_ul_tbf_slots(struct gprs_rlcmac_ul_tbf *ul_tbf, gprs_rlcmac_trx *trx, uint8_t ul_slots, int tfi,
791 int *usf)
792{
793 uint8_t ts;
794
795 for (ts = 0; ts < 8; ts++) {
796 if (!(ul_slots & (1 << ts)))
797 continue;
798
799 OSMO_ASSERT(usf[ts] >= 0);
800
801 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS %u\n", ts);
802 assign_uplink_tbf_usf(&trx->pdch[ts], ul_tbf, tfi, usf[ts]);
803 }
804}
805
806/*! Assign given DL timeslots to DL TBF
807 *
808 * \param[in,out] dl_tbf Pointer to DL TBF struct
809 * \param[in,out] trx Pointer to TRX object
810 * \param[in] ul_slots Set of slots to be assigned
811 * \param[in] tfi selected TFI
812 */
813static void assign_dl_tbf_slots(struct gprs_rlcmac_dl_tbf *dl_tbf, gprs_rlcmac_trx *trx, uint8_t dl_slots, int tfi)
814{
815 uint8_t ts;
816
817 for (ts = 0; ts < 8; ts++) {
818 if (!(dl_slots & (1 << ts)))
819 continue;
820
821 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS %u\n", ts);
822 assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi);
823 }
824}
825
Maxe9fe0e32017-09-28 15:56:05 +0200826/*! Slot Allocation: Algorithm B
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200827 *
828 * Assign as many downlink slots as possible.
829 * Assign one uplink slot. (With free USF)
830 *
Maxe9fe0e32017-09-28 15:56:05 +0200831 * \param[in,out] bts Pointer to BTS struct
832 * \param[in,out] ms_ Pointer to MS object
833 * \param[in,out] tbf_ Pointer to TBF struct
834 * \param[in] single flag indicating if we should force single-slot allocation
835 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
836 * \returns negative error code or 0 on success
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200837 */
Maxe9fe0e32017-09-28 15:56:05 +0200838int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
839 int8_t use_trx)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200840{
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200841 uint8_t dl_slots;
842 uint8_t ul_slots;
843 uint8_t reserved_dl_slots;
844 uint8_t reserved_ul_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200845 int8_t first_common_ts;
846 uint8_t slotcount = 0;
Maxa76a7d02018-01-26 11:09:16 +0100847 uint8_t avail_count = 0, trx_no;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200848 int first_ts = -1;
849 int usf[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200850 int rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200851 int tfi;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200852 const GprsMs *ms = ms_;
853 const gprs_rlcmac_tbf *tbf = tbf_;
854 gprs_rlcmac_trx *trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200855
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200856 /* Step 1: Get current state from the MS object */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200857
858 if (!ms) {
859 LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");
860 return -EINVAL;
861 }
862
Max92e9c172017-09-28 16:25:25 +0200863 dl_slots = ms->reserved_dl_slots();
864 ul_slots = ms->reserved_ul_slots();
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200865 first_common_ts = ms->first_common_ts();
866 trx = ms->current_trx();
867
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200868 /* Step 2a: Find usable TRX and TFI */
Maxa76a7d02018-01-26 11:09:16 +0100869 tfi = tfi_find_free(bts->bts, trx, ms, tbf->direction, use_trx, &trx_no);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200870 if (tfi < 0) {
871 LOGP(DRLCMAC, LOGL_NOTICE, "- Failed to allocate a TFI\n");
872 return tfi;
873 }
874
875 /* Step 2b: Reserve slots on the TRX for the MS */
876 if (!trx)
877 trx = &bts->trx[trx_no];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200878
879 if (!dl_slots || !ul_slots) {
Max842d7812017-11-01 18:11:24 +0100880 rc = find_multi_slots(trx, ms->ms_class(), &ul_slots, &dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100881 if (rc < 0)
882 return rc;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200883 }
884
Max92e9c172017-09-28 16:25:25 +0200885 reserved_dl_slots = dl_slots;
886 reserved_ul_slots = ul_slots;
887
Max0cc72122018-01-31 17:00:06 +0100888 /* Step 3a: Derive the slot set for the current TBF */
889 rc = tbf_select_slot_set(tbf, trx, single, ul_slots, dl_slots, reserved_ul_slots, reserved_dl_slots,
890 first_common_ts);
891 if (rc < 0)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200892 return -EINVAL;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200893
Max0cc72122018-01-31 17:00:06 +0100894 first_ts = ffs(rc) - 1;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200895
Max0cc72122018-01-31 17:00:06 +0100896 /* Step 3b: Derive the slot set for a given direction */
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200897 if (tbf->direction == GPRS_RLCMAC_DL_TBF) {
Max0cc72122018-01-31 17:00:06 +0100898 dl_slots = rc;
899 update_slot_counters(dl_slots, reserved_dl_slots, &slotcount, &avail_count);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200900 } else {
Max2afec6d2018-01-31 17:21:21 +0100901 rc = allocate_usf(trx, first_common_ts, rc, dl_slots, usf);
902 if (rc < 0)
903 return rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200904
Max0cc72122018-01-31 17:00:06 +0100905 /* We will stick to that single UL slot, unreserve the others */
Max2afec6d2018-01-31 17:21:21 +0100906 ul_slots = rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200907 reserved_ul_slots = ul_slots;
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200908
Max0cc72122018-01-31 17:00:06 +0100909 update_slot_counters(ul_slots, reserved_ul_slots, &slotcount, &avail_count);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200910 }
911
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200912 first_common_ts = ffs(dl_slots & ul_slots) - 1;
913
914 if (first_common_ts < 0) {
915 LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n");
916 return -EINVAL;
917 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200918 if (first_ts < 0) {
919 LOGP(DRLCMAC, LOGL_NOTICE, "No first slot available\n");
920 return -EINVAL;
921 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200922
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200923 if (single && slotcount) {
924 tbf_->upgrade_to_multislot = (avail_count > slotcount);
925 LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n",
926 first_ts,
927 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
928 } else {
929 tbf_->upgrade_to_multislot = 0;
930 LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount,
931 (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL");
932 }
933
934 /* The allocation will be successful, so the system state and tbf_/ms_
935 * may be modified from now on. */
936
937 /* Step 4: Update MS and TBF and really allocate the resources */
938
Max77988d42018-02-19 18:00:38 +0100939 update_ms_reserved_slots(trx, ms_, reserved_ul_slots, reserved_dl_slots, ul_slots, dl_slots);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200940
941 tbf_->trx = trx;
942 tbf_->first_common_ts = first_common_ts;
943 tbf_->first_ts = first_ts;
944
Max77988d42018-02-19 18:00:38 +0100945 if (tbf->direction == GPRS_RLCMAC_DL_TBF)
946 assign_dl_tbf_slots(as_dl_tbf(tbf_), trx, dl_slots, tfi);
947 else
948 assign_ul_tbf_slots(as_ul_tbf(tbf_), trx, ul_slots, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200949
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200950 bts->bts->tbf_alloc_algo_b();
951
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200952 return 0;
953}
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200954
Maxe9fe0e32017-09-28 15:56:05 +0200955/*! Slot Allocation: Algorithm dynamic
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200956 *
957 * This meta algorithm automatically selects on of the other algorithms based
958 * on the current system state.
959 *
960 * The goal is to support as many MS and TBF as possible. On low usage, the
961 * goal is to provide the highest possible bandwidth per MS.
962 *
Maxe9fe0e32017-09-28 15:56:05 +0200963 * \param[in,out] bts Pointer to BTS struct
964 * \param[in,out] ms_ Pointer to MS object
965 * \param[in,out] tbf_ Pointer to TBF struct
966 * \param[in] single flag indicating if we should force single-slot allocation
967 * \param[in] use_trx which TRX to use or -1 if it should be selected during allocation
968 * \returns negative error code or 0 on success
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200969 */
Maxe9fe0e32017-09-28 15:56:05 +0200970int alloc_algorithm_dynamic(struct gprs_rlcmac_bts *bts, GprsMs *ms_, struct gprs_rlcmac_tbf *tbf_, bool single,
971 int8_t use_trx)
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200972{
973 int rc;
974
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200975 /* Reset load_is_high if there is at least one idle PDCH */
976 if (bts->multislot_disabled) {
Maxa76a7d02018-01-26 11:09:16 +0100977 bts->multislot_disabled = !idle_pdch_avail(bts);
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200978 if (!bts->multislot_disabled)
979 LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n");
980 }
981
982 if (!bts->multislot_disabled) {
Maxe9fe0e32017-09-28 15:56:05 +0200983 rc = alloc_algorithm_b(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200984 if (rc >= 0)
985 return rc;
986
987 if (!bts->multislot_disabled)
988 LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n");
989 bts->multislot_disabled = 1;
990 }
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200991
Maxe9fe0e32017-09-28 15:56:05 +0200992 return alloc_algorithm_a(bts, ms_, tbf_, single, use_trx);
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200993}
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200994
Max4da38592018-01-31 18:03:49 +0100995int gprs_alloc_max_dl_slots_per_ms(const struct gprs_rlcmac_bts *bts, uint8_t ms_class)
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200996{
Max842d7812017-11-01 18:11:24 +0100997 int rx = mslot_class_get_rx(ms_class);
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200998
999 if (rx == MS_NA)
1000 rx = 4;
1001
1002 if (bts->alloc_algorithm == alloc_algorithm_a)
1003 return 1;
1004
1005 if (bts->multislot_disabled)
1006 return 1;
1007
1008 return rx;
1009}