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