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