Pau Espin Pedrol | ff7c581 | 2022-12-14 18:49:06 +0100 | [diff] [blame] | 1 | /* alloc_algo.cpp |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 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 |
Pau Espin Pedrol | ff7c581 | 2022-12-14 18:49:06 +0100 | [diff] [blame] | 6 | * Copyright (C) 2022 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de> |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 7 | * |
| 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 Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <gprs_rlcmac.h> |
| 20 | #include <gprs_debug.h> |
Holger Hans Peter Freyther | 34bd8bd | 2013-10-19 21:10:38 +0200 | [diff] [blame] | 21 | #include <bts.h> |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 22 | #include <tbf.h> |
Pau Espin Pedrol | 9d1cdb1 | 2019-09-25 17:47:02 +0200 | [diff] [blame] | 23 | #include <tbf_ul.h> |
Max | 6dc90b8 | 2018-02-19 17:17:28 +0100 | [diff] [blame] | 24 | #include <pdch.h> |
Jacob Erlbeck | e2e004e | 2015-06-18 17:16:26 +0200 | [diff] [blame] | 25 | #include <gprs_ms.h> |
Jacob Erlbeck | 8cba7e9 | 2016-01-19 15:48:03 +0100 | [diff] [blame] | 26 | #include <pcu_utils.h> |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 27 | |
| 28 | #include <errno.h> |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 29 | #include <values.h> |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 30 | |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 31 | extern "C" { |
| 32 | #include "mslot_class.h" |
Pau Espin Pedrol | ff7c581 | 2022-12-14 18:49:06 +0100 | [diff] [blame] | 33 | #include "alloc_algo.h" |
Max | 1187a77 | 2018-01-26 13:31:42 +0100 | [diff] [blame] | 34 | #include <osmocom/core/linuxlist.h> |
| 35 | #include <osmocom/core/logging.h> |
| 36 | #include <osmocom/core/utils.h> |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 39 | /* 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 Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 42 | #define LOGPSL(req, level, fmt, args...) LOGP(DRLCMAC, level, "[%s] " fmt, \ |
| 43 | (req->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL", ## args) |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 44 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 45 | #define LOGPAL(req, kind, level, fmt, args...) LOGPSL(req, level, \ |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 46 | "algo %s <%s> (suggested TRX: %d): " fmt, \ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 47 | kind, req->single ? "single" : "multi", req->use_trx, ## args) |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 48 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 49 | static 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 | |
Max | 731e2bb | 2018-02-05 16:15:30 +0100 | [diff] [blame] | 63 | static 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 Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 65 | { |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 66 | unsigned ts; |
Max | 731e2bb | 2018-02-05 16:15:30 +0100 | [diff] [blame] | 67 | uint8_t valid_ts_set = 0; |
Jacob Erlbeck | 83426b2 | 2015-06-30 09:44:05 +0200 | [diff] [blame] | 68 | int8_t last_tsc = -1; /* must be signed */ |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 69 | |
| 70 | for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) { |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 71 | const struct gprs_rlcmac_pdch *pdch; |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 72 | |
| 73 | pdch = &trx->pdch[ts]; |
Holger Hans Peter Freyther | 17b0d83 | 2013-10-19 17:37:48 +0200 | [diff] [blame] | 74 | if (!pdch->is_enabled()) { |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 75 | LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because " |
| 76 | "not enabled\n", ts); |
| 77 | continue; |
| 78 | } |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 79 | |
| 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 Erlbeck | 83426b2 | 2015-06-30 09:44:05 +0200 | [diff] [blame] | 88 | 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 Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 103 | valid_ts_set |= 1 << ts; |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 106 | return valid_ts_set; |
| 107 | } |
| 108 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 109 | static int compute_usage_by_num_tbfs(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction dir) |
Jacob Erlbeck | c135b87 | 2015-07-09 13:44:18 +0200 | [diff] [blame] | 110 | { |
| 111 | return pdch->num_tbfs(dir); |
| 112 | } |
| 113 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 114 | static int compute_usage_by_reservation(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction) |
Jacob Erlbeck | c135b87 | 2015-07-09 13:44:18 +0200 | [diff] [blame] | 115 | { |
| 116 | return |
| 117 | pdch->num_reserved(GPRS_RLCMAC_DL_TBF) + |
| 118 | pdch->num_reserved(GPRS_RLCMAC_UL_TBF); |
| 119 | } |
| 120 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 121 | static int compute_usage_for_algo_a(const struct gprs_rlcmac_pdch *pdch, enum gprs_rlcmac_tbf_direction dir) |
Jacob Erlbeck | 7af53e6 | 2015-07-16 15:04:07 +0200 | [diff] [blame] | 122 | { |
| 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 | |
Max | d000d80 | 2017-09-20 17:55:28 +0200 | [diff] [blame] | 128 | if (pdch->assigned_tfi(reverse(dir)) == NO_FREE_TFI) |
Jacob Erlbeck | 7af53e6 | 2015-07-16 15:04:07 +0200 | [diff] [blame] | 129 | /* No TFI in the opposite direction, avoid it */ |
| 130 | usage += 32; |
| 131 | |
| 132 | return usage; |
| 133 | |
| 134 | } |
| 135 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 136 | /*! 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 | */ |
| 146 | static 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 Pedrol | 1f8e229 | 2021-02-19 16:49:16 +0100 | [diff] [blame] | 148 | int *free_tfi = NULL, int *free_usf = NULL) |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 149 | { |
| 150 | unsigned ts; |
| 151 | int min_used = INT_MAX; |
| 152 | int min_ts = -1; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 153 | int min_tfi = -1; |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 154 | int min_usf = -1; |
| 155 | |
| 156 | for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) { |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 157 | const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts]; |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 158 | int num_tbfs; |
| 159 | int usf = -1; /* must be signed */ |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 160 | int tfi = -1; |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 161 | |
| 162 | if (((1 << ts) & mask) == 0) |
| 163 | continue; |
| 164 | |
Jacob Erlbeck | c135b87 | 2015-07-09 13:44:18 +0200 | [diff] [blame] | 165 | num_tbfs = fn(pdch, dir); |
Jacob Erlbeck | efe62a7 | 2015-07-02 15:48:25 +0200 | [diff] [blame] | 166 | |
| 167 | if (num_tbfs < min_used) { |
| 168 | /* We have found a candidate */ |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 169 | /* Make sure that a TFI is available */ |
| 170 | if (free_tfi) { |
Max | c5407c7 | 2018-02-05 16:11:36 +0100 | [diff] [blame] | 171 | tfi = find_free_tfi(pdch->assigned_tfi(dir)); |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 172 | if (tfi < 0) { |
| 173 | LOGP(DRLCMAC, LOGL_DEBUG, |
| 174 | "- Skipping TS %d, because " |
| 175 | "no TFI available\n", ts); |
| 176 | continue; |
| 177 | } |
| 178 | } |
Jacob Erlbeck | efe62a7 | 2015-07-02 15:48:25 +0200 | [diff] [blame] | 179 | /* Make sure that an USF is available */ |
| 180 | if (dir == GPRS_RLCMAC_UL_TBF) { |
Max | adca67b | 2018-01-31 15:22:36 +0100 | [diff] [blame] | 181 | usf = find_free_usf(pdch->assigned_usf()); |
Jacob Erlbeck | efe62a7 | 2015-07-02 15:48:25 +0200 | [diff] [blame] | 182 | 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 Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 196 | min_tfi = tfi; |
Jacob Erlbeck | efe62a7 | 2015-07-02 15:48:25 +0200 | [diff] [blame] | 197 | 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 Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 209 | if (free_tfi) |
| 210 | *free_tfi = min_tfi; |
Jacob Erlbeck | efe62a7 | 2015-07-02 15:48:25 +0200 | [diff] [blame] | 211 | if (free_usf) |
| 212 | *free_usf = min_usf; |
| 213 | |
| 214 | return min_ts; |
| 215 | } |
| 216 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 217 | static int find_trx(const struct alloc_resources_req *req) |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 218 | { |
| 219 | unsigned trx_no; |
| 220 | unsigned ts; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 221 | |
| 222 | /* We must use the TRX currently actively used by an MS */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 223 | if (req->ms && ms_current_trx(req->ms)) |
| 224 | return ms_current_trx(req->ms)->trx_no; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 225 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 226 | if (req->use_trx >= 0 && req->use_trx < 8) |
| 227 | return req->use_trx; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 228 | |
| 229 | /* Find the first TRX that has a PDCH with a free UL and DL TFI */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 230 | for (trx_no = 0; trx_no < ARRAY_SIZE(req->bts->trx); trx_no += 1) { |
| 231 | const struct gprs_rlcmac_trx *trx = &req->bts->trx[trx_no]; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 232 | for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) { |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 233 | const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts]; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 234 | if (!pdch->is_enabled()) |
| 235 | continue; |
| 236 | |
Max | d000d80 | 2017-09-20 17:55:28 +0200 | [diff] [blame] | 237 | if (pdch->assigned_tfi(GPRS_RLCMAC_UL_TBF) == NO_FREE_TFI) |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 238 | continue; |
| 239 | |
Max | d000d80 | 2017-09-20 17:55:28 +0200 | [diff] [blame] | 240 | if (pdch->assigned_tfi(GPRS_RLCMAC_DL_TBF) == NO_FREE_TFI) |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 241 | continue; |
| 242 | |
| 243 | return trx_no; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return -EBUSY; |
| 248 | } |
| 249 | |
Pau Espin Pedrol | 0ece97d | 2021-01-18 12:53:54 +0100 | [diff] [blame] | 250 | static bool idle_pdch_avail(const struct gprs_rlcmac_bts *bts) |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 251 | { |
| 252 | unsigned trx_no; |
| 253 | unsigned ts; |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 254 | |
| 255 | /* Find the first PDCH with an unused DL TS */ |
Pau Espin Pedrol | 0ece97d | 2021-01-18 12:53:54 +0100 | [diff] [blame] | 256 | for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); trx_no += 1) { |
| 257 | const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no]; |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 258 | for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) { |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 259 | const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts]; |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 260 | if (!pdch->is_enabled()) |
| 261 | continue; |
| 262 | |
| 263 | if (pdch->num_tbfs(GPRS_RLCMAC_DL_TBF) > PDCH_IDLE_TBF_THRESH) |
| 264 | continue; |
| 265 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 266 | return true; |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 270 | return false; |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 273 | /*! Return free TFI |
| 274 | * |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 275 | * \param[in] req Contains all the requested params |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 276 | * \param[out] trx_no_ TRX number on which TFI was found |
| 277 | * \returns negative error code or 0 on success |
| 278 | */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 279 | static int tfi_find_free(const struct alloc_resources_req *req, uint8_t *trx_no_) |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 280 | { |
Pau Espin Pedrol | d906627 | 2021-11-09 16:52:31 +0100 | [diff] [blame] | 281 | const struct gprs_rlcmac_trx *trx; |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 282 | int tfi; |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 283 | int8_t use_trx = req->use_trx; |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 284 | uint8_t trx_no; |
| 285 | |
Pau Espin Pedrol | d906627 | 2021-11-09 16:52:31 +0100 | [diff] [blame] | 286 | /* If MS is already doing stuff on a TRX, set use_trx to it: */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 287 | if ((trx = ms_current_trx(req->ms))) { |
Max | 7e4921d | 2017-09-28 16:41:24 +0200 | [diff] [blame] | 288 | if (use_trx >= 0 && use_trx != trx->trx_no) { |
| 289 | LOGP(DRLCMAC, LOGL_ERROR, "- Requested incompatible TRX %d (current is %d)\n", |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 290 | req->use_trx, trx->trx_no); |
Max | 7e4921d | 2017-09-28 16:41:24 +0200 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | } |
| 293 | use_trx = trx->trx_no; |
| 294 | } |
| 295 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 296 | tfi = bts_tfi_find_free(req->bts, req->direction, &trx_no, use_trx); |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 297 | if (tfi < 0) |
| 298 | return -EBUSY; |
| 299 | |
| 300 | if (trx_no_) |
| 301 | *trx_no_ = trx_no; |
| 302 | |
| 303 | return tfi; |
| 304 | } |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 305 | |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 306 | /*! Slot Allocation: Algorithm A |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 307 | * |
| 308 | * Assign single slot for uplink and downlink |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 309 | * |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 310 | * \param[in] req Contains all the requested params |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 311 | * \param[out] res The resolution/response for the allocation request |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 312 | * \returns negative error code or 0 on success |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 313 | */ |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 314 | int alloc_algorithm_a(const struct alloc_resources_req *req, |
| 315 | struct alloc_resources_res *res) |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 316 | { |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 317 | int ts = -1; |
Jacob Erlbeck | 5cd496d | 2015-06-30 10:24:37 +0200 | [diff] [blame] | 318 | uint8_t ul_slots, dl_slots; |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 319 | int trx_no; |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 320 | int tfi = -1; |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 321 | int usf = -1; |
Max | 731e2bb | 2018-02-05 16:15:30 +0100 | [diff] [blame] | 322 | uint8_t mask = 0xff; |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 323 | const char *mask_reason = NULL; |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 324 | gprs_rlcmac_trx *trx = ms_current_trx(req->ms); |
| 325 | struct gprs_rlcmac_pdch *first_common_ts = ms_first_common_ts(req->ms); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 326 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 327 | LOGPAL(req, "A", LOGL_DEBUG, "Alloc start\n"); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 328 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 329 | trx_no = find_trx(req); |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 330 | if (trx_no < 0) { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 331 | LOGPAL(req, "A", LOGL_NOTICE, |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 332 | "failed to find a usable TRX (TFI exhausted)\n"); |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 333 | return trx_no; |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 334 | } |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 335 | if (!trx) |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 336 | trx = &req->bts->trx[trx_no]; |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 337 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 338 | dl_slots = ms_reserved_dl_slots(req->ms); |
| 339 | ul_slots = ms_reserved_ul_slots(req->ms); |
Jacob Erlbeck | 5cd496d | 2015-06-30 10:24:37 +0200 | [diff] [blame] | 340 | |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 341 | if (first_common_ts) { |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 342 | mask_reason = "need to reuse TS"; |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 343 | mask = 1 << first_common_ts->ts_no; |
Jacob Erlbeck | 5cd496d | 2015-06-30 10:24:37 +0200 | [diff] [blame] | 344 | } else if (dl_slots || ul_slots) { |
| 345 | mask_reason = "need to use a reserved common TS"; |
| 346 | mask = dl_slots & ul_slots; |
| 347 | } |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 348 | |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 349 | mask = find_possible_pdchs(trx, 1, mask, mask_reason); |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 350 | if (!mask) |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 351 | return -EINVAL; |
| 352 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 353 | ts = find_least_busy_pdch(trx, req->direction, mask, |
Jacob Erlbeck | 7af53e6 | 2015-07-16 15:04:07 +0200 | [diff] [blame] | 354 | compute_usage_for_algo_a, |
Jacob Erlbeck | e0853cd | 2015-07-10 12:25:25 +0200 | [diff] [blame] | 355 | &tfi, &usf); |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 356 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 357 | if (req->direction == GPRS_RLCMAC_UL_TBF && usf < 0) { |
| 358 | LOGPAL(req, "A", LOGL_NOTICE, |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 359 | "failed to allocate a TS, no USF available\n"); |
Jacob Erlbeck | ec47875 | 2015-06-19 16:35:38 +0200 | [diff] [blame] | 360 | return -EBUSY; |
| 361 | } |
| 362 | |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 363 | if (ts < 0) { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 364 | LOGPAL(req, "A", LOGL_NOTICE, |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 365 | "failed to allocate a TS, no TFI available\n"); |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 366 | return -EBUSY; |
| 367 | } |
| 368 | |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 369 | res->trx = trx; |
| 370 | res->first_common_ts = &trx->pdch[ts]; |
| 371 | res->reserved_ul_slots = 1 << ts; |
| 372 | res->reserved_dl_slots = 1 << ts; |
| 373 | res->ass_slots_mask = 1 << ts; |
| 374 | res->upgrade_to_multislot = false; |
| 375 | res->tfi = tfi; |
| 376 | res->usf[ts] = usf; |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 377 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 378 | bts_do_rate_ctr_inc(req->bts, CTR_TBF_ALLOC_ALGO_A); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Max | adca67b | 2018-01-31 15:22:36 +0100 | [diff] [blame] | 382 | /*! Compute capacity of a given TRX |
| 383 | * |
| 384 | * \param[in] trx Pointer to TRX object |
| 385 | * \param[in] rx_window Receive window |
| 386 | * \param[in] tx_window Transmit window |
| 387 | * \returns non-negative capacity |
| 388 | */ |
| 389 | static inline unsigned compute_capacity(const struct gprs_rlcmac_trx *trx, int rx_window, int tx_window) |
| 390 | { |
| 391 | const struct gprs_rlcmac_pdch *pdch; |
| 392 | unsigned ts, capacity = 0; |
| 393 | |
| 394 | for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) { |
| 395 | pdch = &trx->pdch[ts]; |
| 396 | if (rx_window & (1 << ts)) |
| 397 | capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_DL_TBF), 1); |
| 398 | |
| 399 | /* Only consider common slots for UL */ |
| 400 | if (tx_window & rx_window & (1 << ts)) { |
| 401 | if (find_free_usf(pdch->assigned_usf()) >= 0) |
| 402 | capacity += OSMO_MAX(32 - pdch->num_reserved(GPRS_RLCMAC_UL_TBF), 1); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return capacity; |
| 407 | } |
| 408 | |
Max | 731e2bb | 2018-02-05 16:15:30 +0100 | [diff] [blame] | 409 | /*! Decide if a given slot should be skipped by multislot allocator |
| 410 | * |
| 411 | * \param[in] ms_class Pointer to MS Class object |
| 412 | * \param[in] check_tr Flag indicating whether we should check for Tra or Tta parameters for a given MS class |
| 413 | * \param[in] rx_window Receive window |
| 414 | * \param[in] tx_window Transmit window |
| 415 | * \param[in,out] checked_rx array with already checked RX timeslots |
| 416 | * \returns true if the slot should be skipped, false otherwise |
| 417 | */ |
| 418 | static bool skip_slot(uint8_t mslot_class, bool check_tr, |
| 419 | int16_t rx_window, int16_t tx_window, |
| 420 | uint32_t *checked_rx) |
| 421 | { |
| 422 | uint8_t common_slot_count, req_common_slots, |
| 423 | rx_slot_count = pcu_bitcount(rx_window), |
| 424 | tx_slot_count = pcu_bitcount(tx_window); |
| 425 | |
| 426 | /* Check compliance with TS 45.002, table 6.4.2.2.1 */ |
| 427 | /* Whether to skip this round doesn not only depend on the bit |
| 428 | * sets but also on check_tr. Therefore this check must be done |
| 429 | * before doing the mslot_test_and_set_bit shortcut. */ |
| 430 | if (mslot_class_get_type(mslot_class) == 1) { |
| 431 | uint16_t slot_sum = rx_slot_count + tx_slot_count; |
| 432 | /* Assume down + up / dynamic. |
| 433 | * TODO: For ext-dynamic, down only, up only add more cases. |
| 434 | */ |
| 435 | if (slot_sum <= 6 && tx_slot_count < 3) { |
| 436 | if (!check_tr) |
| 437 | return true; /* Skip Tta */ |
| 438 | } else if (slot_sum > 6 && tx_slot_count < 3) { |
| 439 | if (check_tr) |
| 440 | return true; /* Skip Tra */ |
| 441 | } else |
| 442 | return true; /* No supported row in TS 45.002, table 6.4.2.2.1. */ |
| 443 | } |
| 444 | |
| 445 | /* Avoid repeated RX combination check */ |
| 446 | if (mslot_test_and_set_bit(checked_rx, rx_window)) |
| 447 | return true; |
| 448 | |
| 449 | /* Check number of common slots according to TS 45.002, §6.4.2.2 */ |
| 450 | common_slot_count = pcu_bitcount(tx_window & rx_window); |
| 451 | req_common_slots = OSMO_MIN(tx_slot_count, rx_slot_count); |
| 452 | if (mslot_class_get_type(mslot_class) == 1) |
| 453 | req_common_slots = OSMO_MIN(req_common_slots, 2); |
| 454 | |
| 455 | if (req_common_slots != common_slot_count) |
| 456 | return true; |
| 457 | |
| 458 | return false; |
| 459 | } |
| 460 | |
Max | a76a7d0 | 2018-01-26 11:09:16 +0100 | [diff] [blame] | 461 | /*! Find set of slots available for allocation while taking MS class into account |
| 462 | * |
| 463 | * \param[in] trx Pointer to TRX object |
| 464 | * \param[in] mslot_class The multislot class |
| 465 | * \param[in,out] ul_slots set of UL timeslots |
| 466 | * \param[in,out] dl_slots set of DL timeslots |
| 467 | * \returns negative error code or 0 on success |
| 468 | */ |
Max | 46fbfce | 2017-11-01 19:22:25 +0100 | [diff] [blame] | 469 | int find_multi_slots(struct gprs_rlcmac_trx *trx, uint8_t mslot_class, uint8_t *ul_slots, uint8_t *dl_slots) |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 470 | { |
Pau Espin Pedrol | 10475f5 | 2021-02-19 17:33:21 +0100 | [diff] [blame] | 471 | const uint8_t Rx = mslot_class_get_rx(mslot_class), /* Max number of Rx slots */ |
| 472 | Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */ |
Pau Espin Pedrol | 4df2658 | 2021-02-19 17:35:11 +0100 | [diff] [blame] | 473 | Sum = mslot_class_get_sum(mslot_class), /* Max number of Tx + Rx slots */ |
| 474 | Type = mslot_class_get_type(mslot_class); |
Pau Espin Pedrol | 10475f5 | 2021-02-19 17:33:21 +0100 | [diff] [blame] | 475 | uint8_t max_slots, num_rx, num_tx, mask_sel, pdch_slots, ul_ts, dl_ts; |
Max | 731e2bb | 2018-02-05 16:15:30 +0100 | [diff] [blame] | 476 | int16_t rx_window, tx_window; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 477 | char slot_info[9] = {0}; |
Max | f633b8d | 2018-01-31 15:28:53 +0100 | [diff] [blame] | 478 | int max_capacity = -1; |
| 479 | uint8_t max_ul_slots = 0, max_dl_slots = 0; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 480 | |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 481 | if (mslot_class) |
| 482 | LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for class %d\n", |
| 483 | mslot_class); |
| 484 | |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 485 | if (Tx == MS_NA) { |
| 486 | LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not applicable.\n", |
| 487 | mslot_class); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 488 | return -EINVAL; |
| 489 | } |
| 490 | |
Pau Espin Pedrol | dfbf3d2 | 2021-02-19 17:31:24 +0100 | [diff] [blame] | 491 | max_slots = OSMO_MAX(Rx, Tx); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 492 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 493 | if (*dl_slots == 0) |
| 494 | *dl_slots = 0xff; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 495 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 496 | if (*ul_slots == 0) |
| 497 | *ul_slots = 0xff; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 498 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 499 | pdch_slots = find_possible_pdchs(trx, max_slots, 0xff); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 500 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 501 | *dl_slots &= pdch_slots; |
| 502 | *ul_slots &= pdch_slots; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 503 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 504 | LOGP(DRLCMAC, LOGL_DEBUG, "- Possible DL/UL slots: (TS=0)\"%s\"(TS=7)\n", |
| 505 | set_flag_chars(set_flag_chars(set_flag_chars(slot_info, |
| 506 | *dl_slots, 'D', '.'), |
| 507 | *ul_slots, 'U'), |
| 508 | *ul_slots & *dl_slots, 'C')); |
| 509 | |
| 510 | /* Check for each UL (TX) slot */ |
| 511 | |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 512 | /* Iterate through possible numbers of TX slots */ |
Pau Espin Pedrol | 47a3b78 | 2021-02-19 16:56:36 +0100 | [diff] [blame] | 513 | for (num_tx = 1; num_tx <= Tx; num_tx += 1) { |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 514 | uint16_t tx_valid_win = (1 << num_tx) - 1; |
Max | f633b8d | 2018-01-31 15:28:53 +0100 | [diff] [blame] | 515 | uint8_t rx_mask[MASK_TR + 1]; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 516 | |
Max | f633b8d | 2018-01-31 15:28:53 +0100 | [diff] [blame] | 517 | mslot_fill_rx_mask(mslot_class, num_tx, rx_mask); |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 518 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 519 | /* Rotate group of TX slots: UUU-----, -UUU----, ..., UU-----U */ |
| 520 | for (ul_ts = 0; ul_ts < 8; ul_ts += 1, tx_valid_win <<= 1) { |
| 521 | uint16_t rx_valid_win; |
| 522 | uint32_t checked_rx[256/32] = {0}; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 523 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 524 | /* Wrap valid window */ |
| 525 | tx_valid_win = mslot_wrap_window(tx_valid_win); |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 526 | |
Alexander Couzens | 695ce77 | 2021-01-12 19:33:53 +0100 | [diff] [blame] | 527 | /* for multislot type 1: don't split the window to wrap around. |
| 528 | * E.g. 'UU-----U' is invalid for a 4 TN window. Except 8 TN window. |
| 529 | * See 45.002 B.1 */ |
Pau Espin Pedrol | 4df2658 | 2021-02-19 17:35:11 +0100 | [diff] [blame] | 530 | if (Type == 1 && num_tx < 8 && |
Alexander Couzens | 695ce77 | 2021-01-12 19:33:53 +0100 | [diff] [blame] | 531 | tx_valid_win & (1 << 0) && tx_valid_win & (1 << 7)) |
| 532 | continue; |
| 533 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 534 | tx_window = tx_valid_win; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 535 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 536 | /* Filter out unavailable slots */ |
| 537 | tx_window &= *ul_slots; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 538 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 539 | /* Skip if the the first TS (ul_ts) is not in the set */ |
| 540 | if ((tx_window & (1 << ul_ts)) == 0) |
| 541 | continue; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 542 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 543 | /* Skip if the the last TS (ul_ts+num_tx-1) is not in the set */ |
| 544 | if ((tx_window & (1 << ((ul_ts+num_tx-1) % 8))) == 0) |
| 545 | continue; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 546 | |
Pau Espin Pedrol | dfbf3d2 | 2021-02-19 17:31:24 +0100 | [diff] [blame] | 547 | num_rx = OSMO_MIN(Rx, Sum - num_tx); |
Alexander Couzens | 695ce77 | 2021-01-12 19:33:53 +0100 | [diff] [blame] | 548 | rx_valid_win = (1 << num_rx) - 1; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 549 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 550 | /* Rotate group of RX slots: DDD-----, -DDD----, ..., DD-----D */ |
| 551 | for (dl_ts = 0; dl_ts < 8; dl_ts += 1, rx_valid_win <<= 1) { |
| 552 | /* Wrap valid window */ |
| 553 | rx_valid_win = (rx_valid_win | rx_valid_win >> 8) & 0xff; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 554 | |
Alexander Couzens | 695ce77 | 2021-01-12 19:33:53 +0100 | [diff] [blame] | 555 | /* for multislot type 1: don't split the window to wrap around. |
| 556 | * E.g. 'DD-----D' is invalid for a 4 TN window. Except 8 TN window. |
| 557 | * See 45.002 B.1 */ |
Pau Espin Pedrol | 4df2658 | 2021-02-19 17:35:11 +0100 | [diff] [blame] | 558 | if (Type == 1 && num_rx < 8 && |
Alexander Couzens | 695ce77 | 2021-01-12 19:33:53 +0100 | [diff] [blame] | 559 | (rx_valid_win & (1 << 0)) && (rx_valid_win & (1 << 7))) |
| 560 | continue; |
| 561 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 562 | /* Validate with both Tta/Ttb/Trb and Ttb/Tra/Trb */ |
| 563 | for (mask_sel = MASK_TT; mask_sel <= MASK_TR; mask_sel += 1) { |
| 564 | int capacity; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 565 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 566 | rx_window = mslot_filter_bad(rx_mask[mask_sel], ul_ts, *dl_slots, rx_valid_win); |
| 567 | if (rx_window < 0) |
| 568 | continue; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 569 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 570 | if (skip_slot(mslot_class, mask_sel != MASK_TT, rx_window, tx_window, checked_rx)) |
| 571 | continue; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 572 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 573 | /* Compute capacity */ |
| 574 | capacity = compute_capacity(trx, rx_window, tx_window); |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 575 | |
Jacob Erlbeck | 1653f83 | 2015-06-30 14:48:13 +0200 | [diff] [blame] | 576 | #ifdef ENABLE_TS_ALLOC_DEBUG |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 577 | LOGP(DRLCMAC, LOGL_DEBUG, |
| 578 | "- Considering DL/UL slots: (TS=0)\"%s\"(TS=7), " |
| 579 | "capacity = %d\n", |
| 580 | set_flag_chars(set_flag_chars(set_flag_chars(set_flag_chars( |
| 581 | slot_info, |
| 582 | rx_bad, 'x', '.'), |
| 583 | rx_window, 'D'), |
| 584 | tx_window, 'U'), |
| 585 | rx_window & tx_window, 'C'), |
| 586 | capacity); |
Jacob Erlbeck | 1653f83 | 2015-06-30 14:48:13 +0200 | [diff] [blame] | 587 | #endif |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 588 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 589 | if (capacity <= max_capacity) |
| 590 | continue; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 591 | |
Pau Espin Pedrol | 5d2d2ec | 2020-09-22 18:03:56 +0200 | [diff] [blame] | 592 | max_capacity = capacity; |
| 593 | max_ul_slots = tx_window; |
| 594 | max_dl_slots = rx_window; |
| 595 | } |
| 596 | } |
| 597 | } |
Max | adca67b | 2018-01-31 15:22:36 +0100 | [diff] [blame] | 598 | } |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 599 | |
| 600 | if (!max_ul_slots || !max_dl_slots) { |
| 601 | LOGP(DRLCMAC, LOGL_NOTICE, |
| 602 | "No valid UL/DL slot combination found\n"); |
Pau Espin Pedrol | 9688dc9 | 2021-02-25 18:30:33 +0100 | [diff] [blame] | 603 | bts_do_rate_ctr_inc(trx->bts, CTR_TBF_ALLOC_FAIL_NO_SLOT_COMBI); |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 604 | return -EINVAL; |
| 605 | } |
| 606 | |
| 607 | *ul_slots = max_ul_slots; |
| 608 | *dl_slots = max_dl_slots; |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 613 | /*! Count used bits in slots and reserved_slots bitmasks |
| 614 | * |
| 615 | * \param[in] slots Timeslots in use |
| 616 | * \param[in] reserved_slots Reserved timeslots |
| 617 | * \param[out] slotcount Number of TS in use |
Pau Espin Pedrol | 853cdf8 | 2021-11-09 17:35:30 +0100 | [diff] [blame] | 618 | * \param[out] reserve_count Number of reserved TS |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 619 | */ |
Pau Espin Pedrol | 853cdf8 | 2021-11-09 17:35:30 +0100 | [diff] [blame] | 620 | static void count_slots(uint8_t slots, uint8_t reserved_slots, uint8_t *slotcount, uint8_t *reserve_count) |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 621 | { |
| 622 | (*slotcount) = pcu_bitcount(slots); |
Pau Espin Pedrol | 853cdf8 | 2021-11-09 17:35:30 +0100 | [diff] [blame] | 623 | (*reserve_count) = pcu_bitcount(reserved_slots); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /*! Return slot mask with single TS from a given UL/DL set according to TBF's direction, ts pointer is set to that TS |
| 627 | * number or to negative value on error |
| 628 | * |
| 629 | * \param[in] trx Pointer to TRX object |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 630 | * \param[in] direction Direction of the TBF to allocate |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 631 | * \param[in] dl_slots set of DL timeslots |
| 632 | * \param[in] ul_slots set of UL timeslots |
| 633 | * \param[in] ts corresponding TS or -1 for autoselection |
| 634 | * \returns slot mask with single UL or DL timeslot number if possible |
| 635 | */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 636 | static uint8_t get_single_ts(const gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction direction, uint8_t dl_slots, uint8_t ul_slots, |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 637 | int ts) |
| 638 | { |
| 639 | uint8_t ret = dl_slots & ul_slots; /* Make sure to consider the first common slot only */ |
| 640 | |
| 641 | if (ts < 0) |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 642 | ts = find_least_busy_pdch(trx, direction, ret, compute_usage_by_num_tbfs, NULL, NULL); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 643 | |
| 644 | if (ts < 0) |
| 645 | return ffs(ret); |
| 646 | |
| 647 | return ret & (1 << ts); |
| 648 | } |
| 649 | |
| 650 | /*! Find set of timeslots available for allocation |
| 651 | * |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 652 | * \param[in] req Contains all the requested params |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 653 | * \param[in] trx Pointer to TRX object |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 654 | * \param[in] ul_slots set of UL timeslots |
| 655 | * \param[in] dl_slots set of DL timeslots |
| 656 | * \param[in] reserved_ul_slots set of reserved UL timeslots |
| 657 | * \param[in] reserved_dl_slots set of reserved DL timeslots |
| 658 | * \param[in] first_common_ts First TS common for both UL and DL or -1 if unknown |
| 659 | * \returns negative error code or selected TS on success |
| 660 | */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 661 | static int tbf_select_slot_set(const struct alloc_resources_req *req, const gprs_rlcmac_trx *trx, |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 662 | uint8_t ul_slots, uint8_t dl_slots, |
| 663 | uint8_t reserved_ul_slots, uint8_t reserved_dl_slots, |
| 664 | int8_t first_common_ts) |
| 665 | { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 666 | bool is_ul = req->direction == GPRS_RLCMAC_UL_TBF; |
Pau Espin Pedrol | 393484a | 2021-05-10 11:21:50 +0200 | [diff] [blame] | 667 | uint8_t sl = is_ul ? ul_slots : dl_slots; |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 668 | char slot_info[9] = { 0 }; |
| 669 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 670 | if (req->single) |
| 671 | sl = get_single_ts(trx, req->direction, dl_slots, ul_slots, first_common_ts); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 672 | |
| 673 | if (!sl) { |
| 674 | LOGP(DRLCMAC, LOGL_NOTICE, "No %s slots available\n", |
Pau Espin Pedrol | 393484a | 2021-05-10 11:21:50 +0200 | [diff] [blame] | 675 | is_ul ? "uplink" : "downlink"); |
Pau Espin Pedrol | 9688dc9 | 2021-02-25 18:30:33 +0100 | [diff] [blame] | 676 | bts_do_rate_ctr_inc(trx->bts, CTR_TBF_ALLOC_FAIL_NO_SLOT_AVAIL); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 677 | return -EINVAL; |
| 678 | } |
| 679 | |
Pau Espin Pedrol | 393484a | 2021-05-10 11:21:50 +0200 | [diff] [blame] | 680 | if (is_ul) { |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 681 | snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_ul_slots, 'u')); |
| 682 | masked_override_with(slot_info, sl, 'U'); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 683 | } else { |
| 684 | snprintf(slot_info, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(reserved_dl_slots, 'd')); |
| 685 | masked_override_with(slot_info, sl, 'D'); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Pau Espin Pedrol | 393484a | 2021-05-10 11:21:50 +0200 | [diff] [blame] | 688 | LOGPC(DRLCMAC, LOGL_DEBUG, "Selected %s slots: (TS=0)\"%s\"(TS=7), %s\n", |
| 689 | is_ul ? "UL" : "DL", |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 690 | slot_info, req->single ? "single" : "multi"); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 691 | |
| 692 | return sl; |
| 693 | } |
| 694 | |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 695 | /*! Allocate USF according to a given UL TS mapping |
| 696 | * |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 697 | * \param[in] trx Pointer to TRX object |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 698 | * \param[in] selected_ul_slots set of UL timeslots selected for allocation |
| 699 | * \param[in] dl_slots set of DL timeslots |
| 700 | * \param[out] usf array for allocated USF |
Pau Espin Pedrol | ed2afa3 | 2021-02-22 17:20:15 +0100 | [diff] [blame] | 701 | * \returns updated UL TS mask or negative on error |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 702 | */ |
Pau Espin Pedrol | ed2afa3 | 2021-02-22 17:20:15 +0100 | [diff] [blame] | 703 | static int allocate_usf(const gprs_rlcmac_trx *trx, uint8_t selected_ul_slots, uint8_t dl_slots, |
| 704 | int *usf_list) |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 705 | { |
Pau Espin Pedrol | ed2afa3 | 2021-02-22 17:20:15 +0100 | [diff] [blame] | 706 | uint8_t ul_slots = selected_ul_slots & dl_slots; |
| 707 | unsigned int ts; |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 708 | |
Pau Espin Pedrol | ed2afa3 | 2021-02-22 17:20:15 +0100 | [diff] [blame] | 709 | for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) { |
| 710 | const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts]; |
| 711 | int8_t free_usf; |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 712 | |
Pau Espin Pedrol | ed2afa3 | 2021-02-22 17:20:15 +0100 | [diff] [blame] | 713 | if (((1 << ts) & ul_slots) == 0) |
| 714 | continue; |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 715 | |
Pau Espin Pedrol | ed2afa3 | 2021-02-22 17:20:15 +0100 | [diff] [blame] | 716 | free_usf = find_free_usf(pdch->assigned_usf()); |
| 717 | if (free_usf < 0) { |
| 718 | LOGP(DRLCMAC, LOGL_DEBUG, |
| 719 | "- Skipping TS %d, because " |
| 720 | "no USF available\n", ts); |
| 721 | ul_slots &= (~(1 << ts)) & 0xff; |
| 722 | continue; |
| 723 | } |
| 724 | usf_list[ts] = free_usf; |
| 725 | } |
| 726 | |
| 727 | if (!ul_slots) { |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 728 | LOGP(DRLCMAC, LOGL_NOTICE, "No USF available\n"); |
Pau Espin Pedrol | 9688dc9 | 2021-02-25 18:30:33 +0100 | [diff] [blame] | 729 | bts_do_rate_ctr_inc(trx->bts, CTR_TBF_ALLOC_FAIL_NO_USF); |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 730 | return -EBUSY; |
| 731 | } |
| 732 | |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 733 | return ul_slots; |
| 734 | } |
| 735 | |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 736 | /*! Slot Allocation: Algorithm B |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 737 | * |
| 738 | * Assign as many downlink slots as possible. |
| 739 | * Assign one uplink slot. (With free USF) |
| 740 | * |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 741 | * \param[in] req Contains all the requested params |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 742 | * \param[out] res The resolution/response for the allocation request |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 743 | * \returns negative error code or 0 on success |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 744 | */ |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 745 | int alloc_algorithm_b(const struct alloc_resources_req *req, |
| 746 | struct alloc_resources_res *res) |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 747 | { |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 748 | uint8_t dl_slots; |
| 749 | uint8_t ul_slots; |
| 750 | uint8_t reserved_dl_slots; |
| 751 | uint8_t reserved_ul_slots; |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 752 | int8_t first_common_tn; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 753 | uint8_t slotcount = 0; |
Pau Espin Pedrol | 853cdf8 | 2021-11-09 17:35:30 +0100 | [diff] [blame] | 754 | uint8_t reserve_count = 0, trx_no; |
Pau Espin Pedrol | 140c97c | 2022-12-13 14:14:32 +0100 | [diff] [blame] | 755 | int first_ts; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 756 | int rc; |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 757 | int tfi; |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 758 | unsigned int i; |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 759 | gprs_rlcmac_trx *trx; |
Pau Espin Pedrol | a42b174 | 2023-04-20 20:20:48 +0200 | [diff] [blame] | 760 | char slot_info[9] = { 0 }; |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 761 | struct gprs_rlcmac_pdch *first_common_ts = ms_first_common_ts(req->ms); |
Jacob Erlbeck | 5879c64 | 2015-07-10 10:41:36 +0200 | [diff] [blame] | 762 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 763 | LOGPAL(req, "B", LOGL_DEBUG, "Alloc start\n"); |
Pau Espin Pedrol | 8353d97 | 2020-10-23 17:07:10 +0200 | [diff] [blame] | 764 | |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 765 | for (i = 0; i < ARRAY_SIZE(res->usf); i++) |
| 766 | res->usf[i] = -1; |
| 767 | |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 768 | /* Step 1: Get current state from the MS object */ |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 769 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 770 | reserved_dl_slots = ms_reserved_dl_slots(req->ms); |
| 771 | reserved_ul_slots = ms_reserved_ul_slots(req->ms); |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 772 | first_common_tn = first_common_ts ? first_common_ts->ts_no : -1; |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 773 | |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 774 | /* Step 2a: Find usable TRX and TFI */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 775 | tfi = tfi_find_free(req, &trx_no); |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 776 | if (tfi < 0) { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 777 | LOGPAL(req, "B", LOGL_NOTICE, "failed to allocate a TFI\n"); |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 778 | return tfi; |
| 779 | } |
| 780 | |
| 781 | /* Step 2b: Reserve slots on the TRX for the MS */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 782 | trx = &req->bts->trx[trx_no]; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 783 | |
Pau Espin Pedrol | 50272a4 | 2021-05-10 12:25:20 +0200 | [diff] [blame] | 784 | if (!reserved_dl_slots || !reserved_ul_slots) { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 785 | rc = find_multi_slots(trx, ms_ms_class(req->ms), &reserved_ul_slots, &reserved_dl_slots); |
Holger Hans Peter Freyther | 7319311 | 2013-12-26 09:49:05 +0100 | [diff] [blame] | 786 | if (rc < 0) |
| 787 | return rc; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 788 | } |
Pau Espin Pedrol | 50272a4 | 2021-05-10 12:25:20 +0200 | [diff] [blame] | 789 | dl_slots = reserved_dl_slots; |
| 790 | ul_slots = reserved_ul_slots; |
Max | 92e9c17 | 2017-09-28 16:25:25 +0200 | [diff] [blame] | 791 | |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 792 | /* Step 3a: Derive the slot set for the current TBF */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 793 | rc = tbf_select_slot_set(req, trx, ul_slots, dl_slots, reserved_ul_slots, reserved_dl_slots, |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 794 | first_common_tn); |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 795 | if (rc < 0) |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 796 | return -EINVAL; |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 797 | |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 798 | /* Step 3b: Derive the slot set for a given direction */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 799 | if (req->direction == GPRS_RLCMAC_DL_TBF) { |
Max | 0cc7212 | 2018-01-31 17:00:06 +0100 | [diff] [blame] | 800 | dl_slots = rc; |
Pau Espin Pedrol | 853cdf8 | 2021-11-09 17:35:30 +0100 | [diff] [blame] | 801 | count_slots(dl_slots, reserved_dl_slots, &slotcount, &reserve_count); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 802 | } else { |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 803 | rc = allocate_usf(trx, rc, dl_slots, &res->usf[0]); |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 804 | if (rc < 0) |
| 805 | return rc; |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 806 | |
Max | 2afec6d | 2018-01-31 17:21:21 +0100 | [diff] [blame] | 807 | ul_slots = rc; |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 808 | reserved_ul_slots = ul_slots; |
Jacob Erlbeck | 5f494b8 | 2015-07-01 13:10:41 +0200 | [diff] [blame] | 809 | |
Pau Espin Pedrol | 853cdf8 | 2021-11-09 17:35:30 +0100 | [diff] [blame] | 810 | count_slots(ul_slots, reserved_ul_slots, &slotcount, &reserve_count); |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Pau Espin Pedrol | 84abd2f | 2020-09-22 20:08:18 +0200 | [diff] [blame] | 813 | first_ts = ffs(rc) - 1; |
Pau Espin Pedrol | 140c97c | 2022-12-13 14:14:32 +0100 | [diff] [blame] | 814 | if (first_ts < 0) { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 815 | LOGPAL(req, "B", LOGL_NOTICE, "first slot unavailable\n"); |
Jacob Erlbeck | ea65c72 | 2015-06-22 16:14:23 +0200 | [diff] [blame] | 816 | return -EINVAL; |
| 817 | } |
Max | 0e6ac79 | 2018-02-19 18:43:01 +0100 | [diff] [blame] | 818 | |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 819 | first_common_tn = ffs(dl_slots & ul_slots) - 1; |
| 820 | if (first_common_tn < 0) { |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 821 | LOGPAL(req, "B", LOGL_NOTICE, "first common slot unavailable\n"); |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 822 | return -EINVAL; |
| 823 | } |
Pau Espin Pedrol | 9935d0d | 2022-12-13 18:29:25 +0100 | [diff] [blame] | 824 | first_common_ts = &trx->pdch[first_common_tn]; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 825 | |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 826 | res->trx = trx; |
| 827 | res->first_common_ts = first_common_ts; |
| 828 | res->reserved_ul_slots = reserved_ul_slots; |
| 829 | res->reserved_dl_slots = reserved_dl_slots; |
| 830 | res->tfi = tfi; |
| 831 | /* res->usf is already filled in above */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 832 | if (req->single && slotcount) { |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 833 | res->upgrade_to_multislot = (reserve_count > slotcount); |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 834 | LOGPAL(req, "B", LOGL_INFO, "using single slot at TS %d\n", first_ts); |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 835 | } else { |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 836 | res->upgrade_to_multislot = false; |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 837 | LOGPAL(req, "B", LOGL_INFO, "using %d slots\n", slotcount); |
Jacob Erlbeck | 5a2b8be | 2015-07-14 11:35:21 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Pau Espin Pedrol | a42b174 | 2023-04-20 20:20:48 +0200 | [diff] [blame] | 840 | ts_format(slot_info, dl_slots, ul_slots); |
| 841 | LOGP(DRLCMAC, LOGL_DEBUG, "- Available DL/UL slots: (TS=0)\"%s\"(TS=7)\n", slot_info); |
| 842 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 843 | if (req->direction == GPRS_RLCMAC_DL_TBF) |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 844 | res->ass_slots_mask = dl_slots; |
Pau Espin Pedrol | b5fece9 | 2021-08-23 16:58:04 +0200 | [diff] [blame] | 845 | else |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 846 | res->ass_slots_mask = ul_slots; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 847 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 848 | bts_do_rate_ctr_inc(req->bts, CTR_TBF_ALLOC_ALGO_B); |
Jacob Erlbeck | 5979fe9 | 2015-07-14 14:02:41 +0200 | [diff] [blame] | 849 | |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 850 | return 0; |
| 851 | } |
Jacob Erlbeck | 400ec02 | 2015-07-14 13:31:48 +0200 | [diff] [blame] | 852 | |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 853 | /*! Slot Allocation: Algorithm dynamic |
Jacob Erlbeck | 400ec02 | 2015-07-14 13:31:48 +0200 | [diff] [blame] | 854 | * |
| 855 | * This meta algorithm automatically selects on of the other algorithms based |
| 856 | * on the current system state. |
| 857 | * |
| 858 | * The goal is to support as many MS and TBF as possible. On low usage, the |
| 859 | * goal is to provide the highest possible bandwidth per MS. |
| 860 | * |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 861 | * \param[in] req Contains all the requested params |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 862 | * \param[out] res The resolution/response for the allocation request |
Max | e9fe0e3 | 2017-09-28 15:56:05 +0200 | [diff] [blame] | 863 | * \returns negative error code or 0 on success |
Jacob Erlbeck | 400ec02 | 2015-07-14 13:31:48 +0200 | [diff] [blame] | 864 | */ |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 865 | int alloc_algorithm_dynamic(const struct alloc_resources_req *req, |
| 866 | struct alloc_resources_res *res) |
Jacob Erlbeck | 400ec02 | 2015-07-14 13:31:48 +0200 | [diff] [blame] | 867 | { |
| 868 | int rc; |
| 869 | |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 870 | /* Reset load_is_high if there is at least one idle PDCH */ |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 871 | if (req->bts->multislot_disabled) { |
| 872 | req->bts->multislot_disabled = !idle_pdch_avail(req->bts); |
| 873 | if (!req->bts->multislot_disabled) |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 874 | LOGP(DRLCMAC, LOGL_DEBUG, "Enabling algorithm B\n"); |
| 875 | } |
| 876 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 877 | if (!req->bts->multislot_disabled) { |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 878 | rc = alloc_algorithm_b(req, res); |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 879 | if (rc >= 0) |
| 880 | return rc; |
| 881 | |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 882 | if (!req->bts->multislot_disabled) |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 883 | LOGP(DRLCMAC, LOGL_DEBUG, "Disabling algorithm B\n"); |
Pau Espin Pedrol | a6a972e | 2023-04-20 19:23:50 +0200 | [diff] [blame] | 884 | req->bts->multislot_disabled = 1; |
Jacob Erlbeck | 77da355 | 2015-07-16 18:33:46 +0200 | [diff] [blame] | 885 | } |
Jacob Erlbeck | 400ec02 | 2015-07-14 13:31:48 +0200 | [diff] [blame] | 886 | |
Pau Espin Pedrol | d6c555e | 2023-04-20 20:10:53 +0200 | [diff] [blame] | 887 | return alloc_algorithm_a(req, res); |
Jacob Erlbeck | 400ec02 | 2015-07-14 13:31:48 +0200 | [diff] [blame] | 888 | } |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 889 | |
Max | 4da3859 | 2018-01-31 18:03:49 +0100 | [diff] [blame] | 890 | int gprs_alloc_max_dl_slots_per_ms(const struct gprs_rlcmac_bts *bts, uint8_t ms_class) |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 891 | { |
Max | 842d781 | 2017-11-01 18:11:24 +0100 | [diff] [blame] | 892 | int rx = mslot_class_get_rx(ms_class); |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 893 | |
| 894 | if (rx == MS_NA) |
| 895 | rx = 4; |
| 896 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 897 | if (the_pcu->alloc_algorithm == alloc_algorithm_a) |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 898 | return 1; |
| 899 | |
| 900 | if (bts->multislot_disabled) |
| 901 | return 1; |
| 902 | |
| 903 | return rx; |
| 904 | } |