blob: 9d4db4c5e2588f77818897f150ed5ca50b3196e7 [file] [log] [blame]
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +01001/* alloc_algo.cpp
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +02002 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +01006 * Copyright (C) 2022 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +02007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020017 */
18
19#include <gprs_rlcmac.h>
20#include <gprs_debug.h>
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020021#include <bts.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020022#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020023#include <tbf_ul.h>
Max6dc90b82018-02-19 17:17:28 +010024#include <pdch.h>
Jacob Erlbecke2e004e2015-06-18 17:16:26 +020025#include <gprs_ms.h>
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010026#include <pcu_utils.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020027
28#include <errno.h>
Jacob Erlbeckec478752015-06-19 16:35:38 +020029#include <values.h>
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +020030
Max842d7812017-11-01 18:11:24 +010031extern "C" {
32#include "mslot_class.h"
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +010033#include "alloc_algo.h"
Max1187a772018-01-26 13:31:42 +010034#include <osmocom/core/linuxlist.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/utils.h>
Max842d7812017-11-01 18:11:24 +010037}
38
Jacob Erlbeck77da3552015-07-16 18:33:46 +020039/* Consider a PDCH as idle if has at most this number of TBFs assigned to it */
40#define PDCH_IDLE_TBF_THRESH 1
41
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +020042#define LOGPSL(req, level, fmt, args...) LOGP(DRLCMAC, level, "[%s] " fmt, \
43 (req->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL", ## args)
Max0e6ac792018-02-19 18:43:01 +010044
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +020045#define LOGPAL(req, kind, level, fmt, args...) LOGPSL(req, level, \
Max0e6ac792018-02-19 18:43:01 +010046 "algo %s <%s> (suggested TRX: %d): " fmt, \
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +020047 kind, req->single ? "single" : "multi", req->use_trx, ## args)
Max0e6ac792018-02-19 18:43:01 +010048
Jacob Erlbeckea65c722015-06-22 16:14:23 +020049static char *set_flag_chars(char *buf, uint8_t val, char set_char, char unset_char = 0)
50{
51 int i;
52
53 for (i = 0; i < 8; i += 1, val = val >> 1) {
54 if (val & 1)
55 buf[i] = set_char;
56 else if (unset_char)
57 buf[i] = unset_char;
58 }
59
60 return buf;
61}
62
Max731e2bb2018-02-05 16:15:30 +010063static uint8_t find_possible_pdchs(const struct gprs_rlcmac_trx *trx, uint8_t max_slots, uint8_t mask,
64 const char *mask_reason = NULL)
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020065{
Jacob Erlbeckec478752015-06-19 16:35:38 +020066 unsigned ts;
Max731e2bb2018-02-05 16:15:30 +010067 uint8_t valid_ts_set = 0;
Jacob Erlbeck83426b22015-06-30 09:44:05 +020068 int8_t last_tsc = -1; /* must be signed */
Jacob Erlbeckec478752015-06-19 16:35:38 +020069
70 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +010071 const struct gprs_rlcmac_pdch *pdch;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020072
73 pdch = &trx->pdch[ts];
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020074 if (!pdch->is_enabled()) {
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +020075 LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because "
76 "not enabled\n", ts);
77 continue;
78 }
Jacob Erlbeckec478752015-06-19 16:35:38 +020079
80 if (((1 << ts) & mask) == 0) {
81 if (mask_reason)
82 LOGP(DRLCMAC, LOGL_DEBUG,
83 "- Skipping TS %d, because %s\n",
84 ts, mask_reason);
85 continue;
86 }
87
Jacob Erlbeck83426b22015-06-30 09:44:05 +020088 if (max_slots > 1) {
89 /* check if TSC changes, see TS 45.002, 6.4.2 */
90 if (last_tsc < 0)
91 last_tsc = pdch->tsc;
92 else if (last_tsc != pdch->tsc) {
93 LOGP(DRLCMAC, LOGL_ERROR,
94 "Skipping TS %d of TRX=%d, because it "
95 "has different TSC than lower TS of TRX. "
96 "In order to allow multislot, all "
97 "slots must be configured with the same "
98 "TSC!\n", ts, trx->trx_no);
99 continue;
100 }
101 }
102
Jacob Erlbeckec478752015-06-19 16:35:38 +0200103 valid_ts_set |= 1 << ts;
Holger Hans Peter Freytherb0a00752013-09-29 08:18:17 +0200104 }
105
Jacob Erlbeckec478752015-06-19 16:35:38 +0200106 return valid_ts_set;
107}
108
Maxa76a7d02018-01-26 11:09:16 +0100109static 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 +0200110{
111 return pdch->num_tbfs(dir);
112}
113
Maxa76a7d02018-01-26 11:09:16 +0100114static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction)
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200115{
116 return
117 pdch->num_reserved(GPRS_RLCMAC_DL_TBF) +
118 pdch->num_reserved(GPRS_RLCMAC_UL_TBF);
119}
120
Maxa76a7d02018-01-26 11:09:16 +0100121static 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 +0200122{
123 int usage =
124 pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) +
125 pdch->num_tbfs(GPRS_RLCMAC_UL_TBF) +
126 compute_usage_by_reservation(pdch, dir);
127
Maxd000d802017-09-20 17:55:28 +0200128 if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI)
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200129 /* No TFI in the opposite direction, avoid it */
130 usage += 32;
131
132 return usage;
133
134}
135
Maxa76a7d02018-01-26 11:09:16 +0100136/*! Return the TS which corresponds to least busy PDCH
137 *
138 * \param[in] trx Pointer to TRX object
139 * \param[in] dir TBF direction
140 * \param[in] mask set of available timeslots
141 * \param[in] fn Function pointer to function which computes number of associated TBFs
142 * \param[out] free_tfi Free TFI
143 * \param[out] free_usf Free USF
144 * \returns TS number or -1 if unable to find
145 */
146static int find_least_busy_pdch(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t mask,
147 int (*fn)(const struct gprs_rlcmac_pdch *, enum gprs_rlcmac_tbf_direction dir),
Pau Espin Pedrol1f8e2292021-02-19 16:49:16 +0100148 int *free_tfi = NULL, int *free_usf = NULL)
Jacob Erlbeckec478752015-06-19 16:35:38 +0200149{
150 unsigned ts;
151 int min_used = INT_MAX;
152 int min_ts = -1;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200153 int min_tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200154 int min_usf = -1;
155
156 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100157 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeckec478752015-06-19 16:35:38 +0200158 int num_tbfs;
159 int usf = -1; /* must be signed */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200160 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200161
162 if (((1 << ts) & mask) == 0)
163 continue;
164
Jacob Erlbeckc135b872015-07-09 13:44:18 +0200165 num_tbfs = fn(pdch, dir);
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200166
167 if (num_tbfs < min_used) {
168 /* We have found a candidate */
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200169 /* Make sure that a TFI is available */
170 if (free_tfi) {
Maxc5407c72018-02-05 16:11:36 +0100171 tfi = find_free_tfi(pdch->assigned_tfi(dir));
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200172 if (tfi < 0) {
173 LOGP(DRLCMAC, LOGL_DEBUG,
174 "- Skipping TS %d, because "
175 "no TFI available\n", ts);
176 continue;
177 }
178 }
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200179 /* Make sure that an USF is available */
180 if (dir == GPRS_RLCMAC_UL_TBF) {
Maxadca67b2018-01-31 15:22:36 +0100181 usf = find_free_usf(pdch->assigned_usf());
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200182 if (usf < 0) {
183 LOGP(DRLCMAC, LOGL_DEBUG,
184 "- Skipping TS %d, because "
185 "no USF available\n", ts);
186 continue;
187 }
188 }
189 if (min_ts >= 0)
190 LOGP(DRLCMAC, LOGL_DEBUG,
191 "- Skipping TS %d, because "
192 "num TBFs %d > %d\n",
193 min_ts, min_used, num_tbfs);
194 min_used = num_tbfs;
195 min_ts = ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200196 min_tfi = tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200197 min_usf = usf;
198 } else {
199 LOGP(DRLCMAC, LOGL_DEBUG,
200 "- Skipping TS %d, because "
201 "num TBFs %d >= %d\n",
202 ts, num_tbfs, min_used);
203 }
204 }
205
206 if (min_ts < 0)
207 return -1;
208
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200209 if (free_tfi)
210 *free_tfi = min_tfi;
Jacob Erlbeckefe62a72015-07-02 15:48:25 +0200211 if (free_usf)
212 *free_usf = min_usf;
213
214 return min_ts;
215}
216
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200217static void attach_tbf_to_pdch(struct gprs_rlcmac_pdch *pdch,
218 struct gprs_rlcmac_tbf *tbf)
219{
220 if (tbf->pdch[pdch->ts_no])
221 tbf->pdch[pdch->ts_no]->detach_tbf(tbf);
222
223 tbf->pdch[pdch->ts_no] = pdch;
224 pdch->attach_tbf(tbf);
225}
226
Maxa76a7d02018-01-26 11:09:16 +0100227static 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 +0200228{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200229 tbf->m_tfi = tfi;
Daniel Willmann7e994e32014-08-07 15:49:21 +0200230 tbf->m_usf[pdch->ts_no] = usf;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200231 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200232}
233
Maxa76a7d02018-01-26 11:09:16 +0100234static 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 +0200235{
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200236 tbf->m_tfi = tfi;
Jacob Erlbeckccc34e42015-06-29 13:45:05 +0200237 attach_tbf_to_pdch(pdch, tbf);
Holger Hans Peter Freyther8481a052013-09-29 08:08:28 +0200238}
239
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200240static int find_trx(const struct alloc_resources_req *req)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200241{
242 unsigned trx_no;
243 unsigned ts;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200244
245 /* We must use the TRX currently actively used by an MS */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200246 if (req->ms && ms_current_trx(req->ms))
247 return ms_current_trx(req->ms)->trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200248
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200249 if (req->use_trx >= 0 && req->use_trx < 8)
250 return req->use_trx;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200251
252 /* Find the first TRX that has a PDCH with a free UL and DL TFI */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200253 for (trx_no = 0; trx_no < ARRAY_SIZE(req->bts->trx); trx_no += 1) {
254 const struct gprs_rlcmac_trx *trx = &req->bts->trx[trx_no];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200255 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100256 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200257 if (!pdch->is_enabled())
258 continue;
259
Maxd000d802017-09-20 17:55:28 +0200260 if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200261 continue;
262
Maxd000d802017-09-20 17:55:28 +0200263 if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI)
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200264 continue;
265
266 return trx_no;
267 }
268 }
269
270 return -EBUSY;
271}
272
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100273static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200274{
275 unsigned trx_no;
276 unsigned ts;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200277
278 /* Find the first PDCH with an unused DL TS */
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100279 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); trx_no += 1) {
280 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200281 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
Maxa76a7d02018-01-26 11:09:16 +0100282 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200283 if (!pdch->is_enabled())
284 continue;
285
286 if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH)
287 continue;
288
Maxa76a7d02018-01-26 11:09:16 +0100289 return true;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200290 }
291 }
292
Maxa76a7d02018-01-26 11:09:16 +0100293 return false;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200294}
295
Maxa76a7d02018-01-26 11:09:16 +0100296/*! Return free TFI
297 *
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200298 * \param[in] req Contains all the requested params
Maxa76a7d02018-01-26 11:09:16 +0100299 * \param[out] trx_no_ TRX number on which TFI was found
300 * \returns negative error code or 0 on success
301 */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200302static int tfi_find_free(const struct alloc_resources_req *req, uint8_t *trx_no_)
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200303{
Pau Espin Pedrold9066272021-11-09 16:52:31 +0100304 const struct gprs_rlcmac_trx *trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200305 int tfi;
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200306 int8_t use_trx = req->use_trx;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200307 uint8_t trx_no;
308
Pau Espin Pedrold9066272021-11-09 16:52:31 +0100309 /* If MS is already doing stuff on a TRX, set use_trx to it: */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200310 if ((trx = ms_current_trx(req->ms))) {
Max7e4921d2017-09-28 16:41:24 +0200311 if (use_trx >= 0 && use_trx != trx->trx_no) {
312 LOGP(DRLCMAC, LOGL_ERROR, "- Requested incompatible TRX %d (current is %d)\n",
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200313 req->use_trx, trx->trx_no);
Max7e4921d2017-09-28 16:41:24 +0200314 return -EINVAL;
315 }
316 use_trx = trx->trx_no;
317 }
318
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200319 tfi = bts_tfi_find_free(req->bts, req->direction, &trx_no, use_trx);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200320 if (tfi < 0)
321 return -EBUSY;
322
323 if (trx_no_)
324 *trx_no_ = trx_no;
325
326 return tfi;
327}
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200328
Maxe9fe0e32017-09-28 15:56:05 +0200329/*! Slot Allocation: Algorithm A
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200330 *
331 * Assign single slot for uplink and downlink
Maxe9fe0e32017-09-28 15:56:05 +0200332 *
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200333 * \param[in] req Contains all the requested params
Maxe9fe0e32017-09-28 15:56:05 +0200334 * \returns negative error code or 0 on success
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200335 */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200336int alloc_algorithm_a(const struct alloc_resources_req *req)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200337{
338 struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200339 int ts = -1;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200340 uint8_t ul_slots, dl_slots;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200341 int trx_no;
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200342 int tfi = -1;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200343 int usf = -1;
Max731e2bb2018-02-05 16:15:30 +0100344 uint8_t mask = 0xff;
Jacob Erlbeckec478752015-06-19 16:35:38 +0200345 const char *mask_reason = NULL;
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200346 gprs_rlcmac_trx *trx = ms_current_trx(req->ms);
347 struct gprs_rlcmac_pdch *first_common_ts = ms_first_common_ts(req->ms);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200348
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200349 LOGPAL(req, "A", LOGL_DEBUG, "Alloc start\n");
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200350
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200351 trx_no = find_trx(req);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200352 if (trx_no < 0) {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200353 LOGPAL(req, "A", LOGL_NOTICE,
Max0e6ac792018-02-19 18:43:01 +0100354 "failed to find a usable TRX (TFI exhausted)\n");
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200355 return trx_no;
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200356 }
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200357 if (!trx)
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200358 trx = &req->bts->trx[trx_no];
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200359
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200360 dl_slots = ms_reserved_dl_slots(req->ms);
361 ul_slots = ms_reserved_ul_slots(req->ms);
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200362
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100363 if (first_common_ts) {
Jacob Erlbeckec478752015-06-19 16:35:38 +0200364 mask_reason = "need to reuse TS";
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100365 mask = 1 << first_common_ts->ts_no;
Jacob Erlbeck5cd496d2015-06-30 10:24:37 +0200366 } else if (dl_slots || ul_slots) {
367 mask_reason = "need to use a reserved common TS";
368 mask = dl_slots & ul_slots;
369 }
Jacob Erlbeckec478752015-06-19 16:35:38 +0200370
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200371 mask = find_possible_pdchs(trx, 1, mask, mask_reason);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200372 if (!mask)
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200373 return -EINVAL;
374
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200375 ts = find_least_busy_pdch(trx, req->direction, mask,
Jacob Erlbeck7af53e62015-07-16 15:04:07 +0200376 compute_usage_for_algo_a,
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200377 &tfi, &usf);
Jacob Erlbeckec478752015-06-19 16:35:38 +0200378
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200379 if (req->direction == GPRS_RLCMAC_UL_TBF && usf < 0) {
380 LOGPAL(req, "A", LOGL_NOTICE,
Max0e6ac792018-02-19 18:43:01 +0100381 "failed to allocate a TS, no USF available\n");
Jacob Erlbeckec478752015-06-19 16:35:38 +0200382 return -EBUSY;
383 }
384
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200385 if (ts < 0) {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200386 LOGPAL(req, "A", LOGL_NOTICE,
Max0e6ac792018-02-19 18:43:01 +0100387 "failed to allocate a TS, no TFI available\n");
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200388 return -EBUSY;
389 }
390
391 pdch = &trx->pdch[ts];
392
Pau Espin Pedrol77e2ff32021-10-18 14:00:24 +0200393 /* The allocation will be successful, so the system state and tbf/ms
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200394 * may be modified from now on. */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200395 if (req->direction == GPRS_RLCMAC_UL_TBF) {
396 struct gprs_rlcmac_ul_tbf *ul_tbf = tbf_as_ul_tbf(req->tbf);
397 LOGPSL(req, LOGL_DEBUG, "Assign uplink TS=%d TFI=%d USF=%d\n", ts, tfi, usf);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200398 assign_uplink_tbf_usf(pdch, ul_tbf, tfi, usf);
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +0200399 } else {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200400 struct gprs_rlcmac_dl_tbf *dl_tbf = tbf_as_dl_tbf(req->tbf);
401 LOGPSL(req, LOGL_DEBUG, "Assign downlink TS=%d TFI=%d\n", ts, tfi);
Jacob Erlbecke0853cd2015-07-10 12:25:25 +0200402 assign_dlink_tbf(pdch, dl_tbf, tfi);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200403 }
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200404
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200405 req->tbf->trx = trx;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200406 /* the only one TS is the common TS */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200407 ms_set_reserved_slots(req->ms, trx, 1 << ts, 1 << ts);
408 ms_set_first_common_ts(req->ms, pdch);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200409
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200410 req->tbf->upgrade_to_multislot = false;
411 bts_do_rate_ctr_inc(req->bts, CTR_TBF_ALLOC_ALGO_A);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200412 return 0;
413}
414
Maxadca67b2018-01-31 15:22:36 +0100415/*! Compute capacity of a given TRX
416 *
417 * \param[in] trx Pointer to TRX object
418 * \param[in] rx_window Receive window
419 * \param[in] tx_window Transmit window
420 * \returns non-negative capacity
421 */
422static inline unsigned compute_capacity(const struct gprs_rlcmac_trx *trx, int rx_window, int tx_window)
423{
424 const struct gprs_rlcmac_pdch *pdch;
425 unsigned ts, capacity = 0;
426
427 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
428 pdch = &trx->pdch[ts];
429 if (rx_window & (1 << ts))
430 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF), 1);
431
432 /* Only consider common slots for UL */
433 if (tx_window & rx_window & (1 << ts)) {
434 if (find_free_usf(pdch->assigned_usf()) >= 0)
435 capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF), 1);
436 }
437 }
438
439 return capacity;
440}
441
Max731e2bb2018-02-05 16:15:30 +0100442/*! Decide if a given slot should be skipped by multislot allocator
443 *
444 * \param[in] ms_class Pointer to MS Class object
445 * \param[in] check_tr Flag indicating whether we should check for Tra or Tta parameters for a given MS class
446 * \param[in] rx_window Receive window
447 * \param[in] tx_window Transmit window
448 * \param[in,out] checked_rx array with already checked RX timeslots
449 * \returns true if the slot should be skipped, false otherwise
450 */
451static bool skip_slot(uint8_t mslot_class, bool check_tr,
452 int16_t rx_window, int16_t tx_window,
453 uint32_t *checked_rx)
454{
455 uint8_t common_slot_count, req_common_slots,
456 rx_slot_count = pcu_bitcount(rx_window),
457 tx_slot_count = pcu_bitcount(tx_window);
458
459 /* Check compliance with TS 45.002, table 6.4.2.2.1 */
460 /* Whether to skip this round doesn not only depend on the bit
461 * sets but also on check_tr. Therefore this check must be done
462 * before doing the mslot_test_and_set_bit shortcut. */
463 if (mslot_class_get_type(mslot_class) == 1) {
464 uint16_t slot_sum = rx_slot_count + tx_slot_count;
465 /* Assume down + up / dynamic.
466 * TODO: For ext-dynamic, down only, up only add more cases.
467 */
468 if (slot_sum <= 6 && tx_slot_count < 3) {
469 if (!check_tr)
470 return true; /* Skip Tta */
471 } else if (slot_sum > 6 && tx_slot_count < 3) {
472 if (check_tr)
473 return true; /* Skip Tra */
474 } else
475 return true; /* No supported row in TS 45.002, table 6.4.2.2.1. */
476 }
477
478 /* Avoid repeated RX combination check */
479 if (mslot_test_and_set_bit(checked_rx, rx_window))
480 return true;
481
482 /* Check number of common slots according to TS 45.002, §6.4.2.2 */
483 common_slot_count = pcu_bitcount(tx_window & rx_window);
484 req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count);
485 if (mslot_class_get_type(mslot_class) == 1)
486 req_common_slots = OSMO_MIN(req_common_slots, 2);
487
488 if (req_common_slots != common_slot_count)
489 return true;
490
491 return false;
492}
493
Maxa76a7d02018-01-26 11:09:16 +0100494/*! Find set of slots available for allocation while taking MS class into account
495 *
496 * \param[in] trx Pointer to TRX object
497 * \param[in] mslot_class The multislot class
498 * \param[in,out] ul_slots set of UL timeslots
499 * \param[in,out] dl_slots set of DL timeslots
500 * \returns negative error code or 0 on success
501 */
Max46fbfce2017-11-01 19:22:25 +0100502int 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 +0200503{
Pau Espin Pedrol10475f52021-02-19 17:33:21 +0100504 const uint8_t Rx = mslot_class_get_rx(mslot_class), /* Max number of Rx slots */
505 Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */
Pau Espin Pedrol4df26582021-02-19 17:35:11 +0100506 Sum = mslot_class_get_sum(mslot_class), /* Max number of Tx + Rx slots */
507 Type = mslot_class_get_type(mslot_class);
Pau Espin Pedrol10475f52021-02-19 17:33:21 +0100508 uint8_t max_slots, num_rx, num_tx, mask_sel, pdch_slots, ul_ts, dl_ts;
Max731e2bb2018-02-05 16:15:30 +0100509 int16_t rx_window, tx_window;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200510 char slot_info[9] = {0};
Maxf633b8d2018-01-31 15:28:53 +0100511 int max_capacity = -1;
512 uint8_t max_ul_slots = 0, max_dl_slots = 0;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200513
Max842d7812017-11-01 18:11:24 +0100514 if (mslot_class)
515 LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n",
516 mslot_class);
517
Max842d7812017-11-01 18:11:24 +0100518 if (Tx == MS_NA) {
519 LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n",
520 mslot_class);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200521 return -EINVAL;
522 }
523
Pau Espin Pedroldfbf3d22021-02-19 17:31:24 +0100524 max_slots = OSMO_MAX(Rx, Tx);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200525
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200526 if (*dl_slots == 0)
527 *dl_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200528
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200529 if (*ul_slots == 0)
530 *ul_slots = 0xff;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200531
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200532 pdch_slots = find_possible_pdchs(trx, max_slots, 0xff);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200533
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200534 *dl_slots &= pdch_slots;
535 *ul_slots &= pdch_slots;
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200536
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200537 LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n",
538 set_flag_chars(set_flag_chars(set_flag_chars(slot_info,
539 *dl_slots, 'D', '.'),
540 *ul_slots, 'U'),
541 *ul_slots & *dl_slots, 'C'));
542
543 /* Check for each UL (TX) slot */
544
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200545 /* Iterate through possible numbers of TX slots */
Pau Espin Pedrol47a3b782021-02-19 16:56:36 +0100546 for (num_tx = 1; num_tx <= Tx; num_tx += 1) {
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200547 uint16_t tx_valid_win = (1 << num_tx) - 1;
Maxf633b8d2018-01-31 15:28:53 +0100548 uint8_t rx_mask[MASK_TR + 1];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200549
Maxf633b8d2018-01-31 15:28:53 +0100550 mslot_fill_rx_mask(mslot_class, num_tx, rx_mask);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200551
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200552 /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */
553 for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) {
554 uint16_t rx_valid_win;
555 uint32_t checked_rx[256/32] = {0};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200556
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200557 /* Wrap valid window */
558 tx_valid_win = mslot_wrap_window(tx_valid_win);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200559
Alexander Couzens695ce772021-01-12 19:33:53 +0100560 /* for multislot type 1: don't split the window to wrap around.
561 * E.g. 'UU-----U' is invalid for a 4 TN window. Except 8 TN window.
562 * See 45.002 B.1 */
Pau Espin Pedrol4df26582021-02-19 17:35:11 +0100563 if (Type == 1 && num_tx < 8 &&
Alexander Couzens695ce772021-01-12 19:33:53 +0100564 tx_valid_win & (1 << 0) && tx_valid_win & (1 << 7))
565 continue;
566
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200567 tx_window = tx_valid_win;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200568
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200569 /* Filter out unavailable slots */
570 tx_window &= *ul_slots;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200571
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200572 /* Skip if the the first TS (ul_ts) is not in the set */
573 if ((tx_window & (1 << ul_ts)) == 0)
574 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200575
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200576 /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */
577 if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0)
578 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200579
Pau Espin Pedroldfbf3d22021-02-19 17:31:24 +0100580 num_rx = OSMO_MIN(Rx, Sum - num_tx);
Alexander Couzens695ce772021-01-12 19:33:53 +0100581 rx_valid_win = (1 << num_rx) - 1;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200582
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200583 /* 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;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200587
Alexander Couzens695ce772021-01-12 19:33:53 +0100588 /* for multislot type 1: don't split the window to wrap around.
589 * E.g. 'DD-----D' is invalid for a 4 TN window. Except 8 TN window.
590 * See 45.002 B.1 */
Pau Espin Pedrol4df26582021-02-19 17:35:11 +0100591 if (Type == 1 && num_rx < 8 &&
Alexander Couzens695ce772021-01-12 19:33:53 +0100592 (rx_valid_win & (1 << 0)) && (rx_valid_win & (1 << 7)))
593 continue;
594
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200595 /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */
596 for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) {
597 int capacity;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200598
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200599 rx_window = mslot_filter_bad(rx_mask[mask_sel], ul_ts, *dl_slots, rx_valid_win);
600 if (rx_window < 0)
601 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200602
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200603 if (skip_slot(mslot_class, mask_sel != MASK_TT, rx_window, tx_window, checked_rx))
604 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200605
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200606 /* Compute capacity */
607 capacity = compute_capacity(trx, rx_window, tx_window);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200608
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200609#ifdef ENABLE_TS_ALLOC_DEBUG
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200610 LOGP(DRLCMAC, LOGL_DEBUG,
611 "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), "
612 "capacity = %d\n",
613 set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars(
614 slot_info,
615 rx_bad, 'x', '.'),
616 rx_window, 'D'),
617 tx_window, 'U'),
618 rx_window & tx_window, 'C'),
619 capacity);
Jacob Erlbeck1653f832015-06-30 14:48:13 +0200620#endif
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200621
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200622 if (capacity <= max_capacity)
623 continue;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200624
Pau Espin Pedrol5d2d2ec2020-09-22 18:03:56 +0200625 max_capacity = capacity;
626 max_ul_slots = tx_window;
627 max_dl_slots = rx_window;
628 }
629 }
630 }
Maxadca67b2018-01-31 15:22:36 +0100631 }
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200632
633 if (!max_ul_slots || !max_dl_slots) {
634 LOGP(DRLCMAC, LOGL_NOTICE,
635 "No valid UL/DL slot combination found\n");
Pau Espin Pedrol9688dc92021-02-25 18:30:33 +0100636 bts_do_rate_ctr_inc(trx->bts, CTR_TBF_ALLOC_FAIL_NO_SLOT_COMBI);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200637 return -EINVAL;
638 }
639
640 *ul_slots = max_ul_slots;
641 *dl_slots = max_dl_slots;
642
643 return 0;
644}
645
Max0cc72122018-01-31 17:00:06 +0100646/*! Count used bits in slots and reserved_slots bitmasks
647 *
648 * \param[in] slots Timeslots in use
649 * \param[in] reserved_slots Reserved timeslots
650 * \param[out] slotcount Number of TS in use
Pau Espin Pedrol853cdf82021-11-09 17:35:30 +0100651 * \param[out] reserve_count Number of reserved TS
Max0cc72122018-01-31 17:00:06 +0100652 */
Pau Espin Pedrol853cdf82021-11-09 17:35:30 +0100653static void count_slots(uint8_t slots, uint8_t reserved_slots, uint8_t *slotcount, uint8_t *reserve_count)
Max0cc72122018-01-31 17:00:06 +0100654{
655 (*slotcount) = pcu_bitcount(slots);
Pau Espin Pedrol853cdf82021-11-09 17:35:30 +0100656 (*reserve_count) = pcu_bitcount(reserved_slots);
Max0cc72122018-01-31 17:00:06 +0100657}
658
659/*! Return slot mask with single TS from a given UL/DL set according to TBF's direction, ts pointer is set to that TS
660 * number or to negative value on error
661 *
662 * \param[in] trx Pointer to TRX object
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200663 * \param[in] direction Direction of the TBF to allocate
Max0cc72122018-01-31 17:00:06 +0100664 * \param[in] dl_slots set of DL timeslots
665 * \param[in] ul_slots set of UL timeslots
666 * \param[in] ts corresponding TS or -1 for autoselection
667 * \returns slot mask with single UL or DL timeslot number if possible
668 */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200669static uint8_t get_single_ts(const gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction direction, uint8_t dl_slots, uint8_t ul_slots,
Max0cc72122018-01-31 17:00:06 +0100670 int ts)
671{
672 uint8_t ret = dl_slots & ul_slots; /* Make sure to consider the first common slot only */
673
674 if (ts < 0)
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200675 ts = find_least_busy_pdch(trx, direction, ret, compute_usage_by_num_tbfs, NULL, NULL);
Max0cc72122018-01-31 17:00:06 +0100676
677 if (ts < 0)
678 return ffs(ret);
679
680 return ret & (1 << ts);
681}
682
683/*! Find set of timeslots available for allocation
684 *
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200685 * \param[in] req Contains all the requested params
Max0cc72122018-01-31 17:00:06 +0100686 * \param[in] trx Pointer to TRX object
Max0cc72122018-01-31 17:00:06 +0100687 * \param[in] ul_slots set of UL timeslots
688 * \param[in] dl_slots set of DL timeslots
689 * \param[in] reserved_ul_slots set of reserved UL timeslots
690 * \param[in] reserved_dl_slots set of reserved DL timeslots
691 * \param[in] first_common_ts First TS common for both UL and DL or -1 if unknown
692 * \returns negative error code or selected TS on success
693 */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200694static int tbf_select_slot_set(const struct alloc_resources_req *req, const gprs_rlcmac_trx *trx,
Max0cc72122018-01-31 17:00:06 +0100695 uint8_t ul_slots, uint8_t dl_slots,
696 uint8_t reserved_ul_slots, uint8_t reserved_dl_slots,
697 int8_t first_common_ts)
698{
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200699 bool is_ul = req->direction == GPRS_RLCMAC_UL_TBF;
Pau Espin Pedrol393484a2021-05-10 11:21:50 +0200700 uint8_t sl = is_ul ? ul_slots : dl_slots;
Max0cc72122018-01-31 17:00:06 +0100701 char slot_info[9] = { 0 };
702
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200703 if (req->single)
704 sl = get_single_ts(trx, req->direction, dl_slots, ul_slots, first_common_ts);
Max0cc72122018-01-31 17:00:06 +0100705
706 if (!sl) {
707 LOGP(DRLCMAC, LOGL_NOTICE, "No %s slots available\n",
Pau Espin Pedrol393484a2021-05-10 11:21:50 +0200708 is_ul ? "uplink" : "downlink");
Pau Espin Pedrol9688dc92021-02-25 18:30:33 +0100709 bts_do_rate_ctr_inc(trx->bts, CTR_TBF_ALLOC_FAIL_NO_SLOT_AVAIL);
Max0cc72122018-01-31 17:00:06 +0100710 return -EINVAL;
711 }
712
Pau Espin Pedrol393484a2021-05-10 11:21:50 +0200713 if (is_ul) {
Max0cc72122018-01-31 17:00:06 +0100714 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_ul_slots, 'u'));
715 masked_override_with(slot_info, sl, 'U');
Max0cc72122018-01-31 17:00:06 +0100716 } else {
717 snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_dl_slots, 'd'));
718 masked_override_with(slot_info, sl, 'D');
Max0cc72122018-01-31 17:00:06 +0100719 }
720
Pau Espin Pedrol393484a2021-05-10 11:21:50 +0200721 LOGPC(DRLCMAC, LOGL_DEBUG, "Selected %s slots: (TS=0)\"%s\"(TS=7), %s\n",
722 is_ul ? "UL" : "DL",
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200723 slot_info, req->single ? "single" : "multi");
Max0cc72122018-01-31 17:00:06 +0100724
725 return sl;
726}
727
Max2afec6d2018-01-31 17:21:21 +0100728/*! Allocate USF according to a given UL TS mapping
729 *
Max2afec6d2018-01-31 17:21:21 +0100730 * \param[in] trx Pointer to TRX object
Max2afec6d2018-01-31 17:21:21 +0100731 * \param[in] selected_ul_slots set of UL timeslots selected for allocation
732 * \param[in] dl_slots set of DL timeslots
733 * \param[out] usf array for allocated USF
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100734 * \returns updated UL TS mask or negative on error
Max2afec6d2018-01-31 17:21:21 +0100735 */
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100736static int allocate_usf(const gprs_rlcmac_trx *trx, uint8_t selected_ul_slots, uint8_t dl_slots,
737 int *usf_list)
Max2afec6d2018-01-31 17:21:21 +0100738{
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100739 uint8_t ul_slots = selected_ul_slots & dl_slots;
740 unsigned int ts;
Max2afec6d2018-01-31 17:21:21 +0100741
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100742 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
743 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts];
744 int8_t free_usf;
Max2afec6d2018-01-31 17:21:21 +0100745
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100746 if (((1 << ts) & ul_slots) == 0)
747 continue;
Max2afec6d2018-01-31 17:21:21 +0100748
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100749 free_usf = find_free_usf(pdch->assigned_usf());
750 if (free_usf < 0) {
751 LOGP(DRLCMAC, LOGL_DEBUG,
752 "- Skipping TS %d, because "
753 "no USF available\n", ts);
754 ul_slots &= (~(1 << ts)) & 0xff;
755 continue;
756 }
757 usf_list[ts] = free_usf;
758 }
759
760 if (!ul_slots) {
Max2afec6d2018-01-31 17:21:21 +0100761 LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n");
Pau Espin Pedrol9688dc92021-02-25 18:30:33 +0100762 bts_do_rate_ctr_inc(trx->bts, CTR_TBF_ALLOC_FAIL_NO_USF);
Max2afec6d2018-01-31 17:21:21 +0100763 return -EBUSY;
764 }
765
Max2afec6d2018-01-31 17:21:21 +0100766 return ul_slots;
767}
768
Max77988d42018-02-19 18:00:38 +0100769/*! Update MS' reserved timeslots
770 *
771 * \param[in,out] trx Pointer to TRX struct
772 * \param[in,out] ms_ Pointer to MS object
773 * \param[in] tbf_ Pointer to TBF struct
774 * \param[in] res_ul_slots Newly reserved UL slots
775 * \param[in] res_dl_slots Newly reserved DL slots
776 * \param[in] ul_slots available UL slots (for logging only)
777 * \param[in] dl_slots available DL slots (for logging only)
778 */
779static void update_ms_reserved_slots(gprs_rlcmac_trx *trx, GprsMs *ms, uint8_t res_ul_slots, uint8_t res_dl_slots,
780 uint8_t ul_slots, uint8_t dl_slots)
781{
782 char slot_info[9] = { 0 };
783
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100784 if (res_ul_slots == ms_reserved_ul_slots(ms) && res_dl_slots == ms_reserved_dl_slots(ms))
Max77988d42018-02-19 18:00:38 +0100785 return;
786
787 /* The reserved slots have changed, update the MS */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100788 ms_set_reserved_slots(ms, trx, res_ul_slots, res_dl_slots);
Max77988d42018-02-19 18:00:38 +0100789
790 ts_format(slot_info, dl_slots, ul_slots);
791 LOGP(DRLCMAC, LOGL_DEBUG, "- Reserved DL/UL slots: (TS=0)\"%s\"(TS=7)\n", slot_info);
792}
793
794/*! Assign given UL timeslots to UL TBF
795 *
796 * \param[in,out] ul_tbf Pointer to UL TBF struct
797 * \param[in,out] trx Pointer to TRX object
798 * \param[in] ul_slots Set of slots to be assigned
799 * \param[in] tfi selected TFI
800 * \param[in] usf selected USF
801 */
802static void assign_ul_tbf_slots(struct gprs_rlcmac_ul_tbf *ul_tbf, gprs_rlcmac_trx *trx, uint8_t ul_slots, int tfi,
803 int *usf)
804{
805 uint8_t ts;
806
807 for (ts = 0; ts < 8; ts++) {
808 if (!(ul_slots & (1 << ts)))
809 continue;
810
811 OSMO_ASSERT(usf[ts] >= 0);
812
813 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS %u\n", ts);
814 assign_uplink_tbf_usf(&trx->pdch[ts], ul_tbf, tfi, usf[ts]);
815 }
816}
817
818/*! Assign given DL timeslots to DL TBF
819 *
820 * \param[in,out] dl_tbf Pointer to DL TBF struct
821 * \param[in,out] trx Pointer to TRX object
822 * \param[in] ul_slots Set of slots to be assigned
823 * \param[in] tfi selected TFI
824 */
825static void assign_dl_tbf_slots(struct gprs_rlcmac_dl_tbf *dl_tbf, gprs_rlcmac_trx *trx, uint8_t dl_slots, int tfi)
826{
827 uint8_t ts;
828
829 for (ts = 0; ts < 8; ts++) {
830 if (!(dl_slots & (1 << ts)))
831 continue;
832
833 LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS %u\n", ts);
834 assign_dlink_tbf(&trx->pdch[ts], dl_tbf, tfi);
835 }
836}
837
Maxe9fe0e32017-09-28 15:56:05 +0200838/*! Slot Allocation: Algorithm B
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200839 *
840 * Assign as many downlink slots as possible.
841 * Assign one uplink slot. (With free USF)
842 *
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200843 * \param[in] req Contains all the requested params
Maxe9fe0e32017-09-28 15:56:05 +0200844 * \returns negative error code or 0 on success
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200845 */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200846int alloc_algorithm_b(const struct alloc_resources_req *req)
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200847{
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200848 uint8_t dl_slots;
849 uint8_t ul_slots;
850 uint8_t reserved_dl_slots;
851 uint8_t reserved_ul_slots;
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100852 int8_t first_common_tn;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200853 uint8_t slotcount = 0;
Pau Espin Pedrol853cdf82021-11-09 17:35:30 +0100854 uint8_t reserve_count = 0, trx_no;
Pau Espin Pedrol140c97c2022-12-13 14:14:32 +0100855 int first_ts;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200856 int usf[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200857 int rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200858 int tfi;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200859 gprs_rlcmac_trx *trx;
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200860 struct gprs_rlcmac_pdch *first_common_ts = ms_first_common_ts(req->ms);
Jacob Erlbeck5879c642015-07-10 10:41:36 +0200861
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200862 LOGPAL(req, "B", LOGL_DEBUG, "Alloc start\n");
Pau Espin Pedrol8353d972020-10-23 17:07:10 +0200863
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200864 /* Step 1: Get current state from the MS object */
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200865
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200866 reserved_dl_slots = ms_reserved_dl_slots(req->ms);
867 reserved_ul_slots = ms_reserved_ul_slots(req->ms);
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100868 first_common_tn = first_common_ts ? first_common_ts->ts_no : -1;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200869
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200870 /* Step 2a: Find usable TRX and TFI */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200871 tfi = tfi_find_free(req, &trx_no);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200872 if (tfi < 0) {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200873 LOGPAL(req, "B", LOGL_NOTICE, "failed to allocate a TFI\n");
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200874 return tfi;
875 }
876
877 /* Step 2b: Reserve slots on the TRX for the MS */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200878 trx = &req->bts->trx[trx_no];
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200879
Pau Espin Pedrol50272a42021-05-10 12:25:20 +0200880 if (!reserved_dl_slots || !reserved_ul_slots) {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200881 rc = find_multi_slots(trx, ms_ms_class(req->ms), &reserved_ul_slots, &reserved_dl_slots);
Holger Hans Peter Freyther73193112013-12-26 09:49:05 +0100882 if (rc < 0)
883 return rc;
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200884 }
Pau Espin Pedrol50272a42021-05-10 12:25:20 +0200885 dl_slots = reserved_dl_slots;
886 ul_slots = reserved_ul_slots;
Max92e9c172017-09-28 16:25:25 +0200887
Max0cc72122018-01-31 17:00:06 +0100888 /* Step 3a: Derive the slot set for the current TBF */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200889 rc = tbf_select_slot_set(req, trx, ul_slots, dl_slots, reserved_ul_slots, reserved_dl_slots,
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100890 first_common_tn);
Max0cc72122018-01-31 17:00:06 +0100891 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 /* Step 3b: Derive the slot set for a given direction */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200895 if (req->direction == GPRS_RLCMAC_DL_TBF) {
Max0cc72122018-01-31 17:00:06 +0100896 dl_slots = rc;
Pau Espin Pedrol853cdf82021-11-09 17:35:30 +0100897 count_slots(dl_slots, reserved_dl_slots, &slotcount, &reserve_count);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200898 } else {
Pau Espin Pedroled2afa32021-02-22 17:20:15 +0100899 rc = allocate_usf(trx, rc, dl_slots, usf);
Max2afec6d2018-01-31 17:21:21 +0100900 if (rc < 0)
901 return rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200902
Max2afec6d2018-01-31 17:21:21 +0100903 ul_slots = rc;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200904 reserved_ul_slots = ul_slots;
Jacob Erlbeck5f494b82015-07-01 13:10:41 +0200905
Pau Espin Pedrol853cdf82021-11-09 17:35:30 +0100906 count_slots(ul_slots, reserved_ul_slots, &slotcount, &reserve_count);
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200907 }
908
Pau Espin Pedrol84abd2f2020-09-22 20:08:18 +0200909 first_ts = ffs(rc) - 1;
Pau Espin Pedrol140c97c2022-12-13 14:14:32 +0100910 if (first_ts < 0) {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200911 LOGPAL(req, "B", LOGL_NOTICE, "first slot unavailable\n");
Jacob Erlbeckea65c722015-06-22 16:14:23 +0200912 return -EINVAL;
913 }
Max0e6ac792018-02-19 18:43:01 +0100914
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100915 first_common_tn = ffs(dl_slots & ul_slots) - 1;
916 if (first_common_tn < 0) {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200917 LOGPAL(req, "B", LOGL_NOTICE, "first common slot unavailable\n");
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200918 return -EINVAL;
919 }
Pau Espin Pedrol9935d0d2022-12-13 18:29:25 +0100920 first_common_ts = &trx->pdch[first_common_tn];
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200921
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200922 if (req->single && slotcount) {
923 req->tbf->upgrade_to_multislot = (reserve_count > slotcount);
924 LOGPAL(req, "B", LOGL_INFO, "using single slot at TS %d\n", first_ts);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200925 } else {
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200926 req->tbf->upgrade_to_multislot = false;
927 LOGPAL(req, "B", LOGL_INFO, "using %d slots\n", slotcount);
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200928 }
929
Pau Espin Pedrol77e2ff32021-10-18 14:00:24 +0200930 /* The allocation will be successful, so the system state and tbf/ms
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200931 * may be modified from now on. */
932
933 /* Step 4: Update MS and TBF and really allocate the resources */
934
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200935 update_ms_reserved_slots(trx, req->ms, reserved_ul_slots, reserved_dl_slots, ul_slots, dl_slots);
936 ms_set_first_common_ts(req->ms, first_common_ts);
937 req->tbf->trx = trx;
Jacob Erlbeck5a2b8be2015-07-14 11:35:21 +0200938
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200939 if (req->direction == GPRS_RLCMAC_DL_TBF)
940 assign_dl_tbf_slots(tbf_as_dl_tbf(req->tbf), trx, dl_slots, tfi);
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +0200941 else
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200942 assign_ul_tbf_slots(tbf_as_ul_tbf(req->tbf), trx, ul_slots, tfi, usf);
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200943
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200944 bts_do_rate_ctr_inc(req->bts, CTR_TBF_ALLOC_ALGO_B);
Jacob Erlbeck5979fe92015-07-14 14:02:41 +0200945
Holger Hans Peter Freyther02ab4a82013-09-29 07:37:40 +0200946 return 0;
947}
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200948
Maxe9fe0e32017-09-28 15:56:05 +0200949/*! Slot Allocation: Algorithm dynamic
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200950 *
951 * This meta algorithm automatically selects on of the other algorithms based
952 * on the current system state.
953 *
954 * The goal is to support as many MS and TBF as possible. On low usage, the
955 * goal is to provide the highest possible bandwidth per MS.
956 *
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200957 * \param[in] req Contains all the requested params
Maxe9fe0e32017-09-28 15:56:05 +0200958 * \returns negative error code or 0 on success
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200959 */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200960int alloc_algorithm_dynamic(const struct alloc_resources_req *req)
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200961{
962 int rc;
963
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200964 /* Reset load_is_high if there is at least one idle PDCH */
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200965 if (req->bts->multislot_disabled) {
966 req->bts->multislot_disabled = !idle_pdch_avail(req->bts);
967 if (!req->bts->multislot_disabled)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200968 LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n");
969 }
970
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200971 if (!req->bts->multislot_disabled) {
972 rc = alloc_algorithm_b(req);
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200973 if (rc >= 0)
974 return rc;
975
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200976 if (!req->bts->multislot_disabled)
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200977 LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n");
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200978 req->bts->multislot_disabled = 1;
Jacob Erlbeck77da3552015-07-16 18:33:46 +0200979 }
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200980
Pau Espin Pedrola6a972e2023-04-20 19:23:50 +0200981 return alloc_algorithm_a(req);
Jacob Erlbeck400ec022015-07-14 13:31:48 +0200982}
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200983
Max4da38592018-01-31 18:03:49 +0100984int gprs_alloc_max_dl_slots_per_ms(const struct gprs_rlcmac_bts *bts, uint8_t ms_class)
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200985{
Max842d7812017-11-01 18:11:24 +0100986 int rx = mslot_class_get_rx(ms_class);
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200987
988 if (rx == MS_NA)
989 rx = 4;
990
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100991 if (the_pcu->alloc_algorithm == alloc_algorithm_a)
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200992 return 1;
993
994 if (bts->multislot_disabled)
995 return 1;
996
997 return rx;
998}