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