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