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> |
| 26 | |
| 27 | #include <errno.h> |
| 28 | |
| 29 | /* 3GPP TS 05.02 Annex B.1 */ |
| 30 | |
| 31 | #define MS_NA 255 /* N/A */ |
| 32 | #define MS_A 254 /* 1 with hopping, 0 without */ |
| 33 | #define MS_B 253 /* 1 with hopping, 0 without (change Rx to Tx)*/ |
| 34 | #define MS_C 252 /* 1 with hopping, 0 without (change Tx to Rx)*/ |
| 35 | |
| 36 | struct gprs_ms_multislot_class { |
| 37 | uint8_t rx, tx, sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */ |
| 38 | uint8_t ta, tb, ra, rb; /* Minimum Number of Slots */ |
| 39 | uint8_t type; /* Type of Mobile */ |
| 40 | }; |
| 41 | |
| 42 | static const struct gprs_ms_multislot_class gprs_ms_multislot_class[32] = { |
| 43 | /* M-S Class Rx Tx Sum Tta Ttb Tra Trb Type */ |
| 44 | /* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA }, |
| 45 | /* 1 */ { 1, 1, 2, 3, 2, 4, 2, 1 }, |
| 46 | /* 2 */ { 2, 1, 3, 3, 2, 3, 1, 1 }, |
| 47 | /* 3 */ { 2, 2, 3, 3, 2, 3, 1, 1 }, |
| 48 | /* 4 */ { 3, 1, 4, 3, 1, 3, 1, 1 }, |
| 49 | /* 5 */ { 2, 2, 4, 3, 1, 3, 1, 1 }, |
| 50 | /* 6 */ { 3, 2, 4, 3, 1, 3, 1, 1 }, |
| 51 | /* 7 */ { 3, 3, 4, 3, 1, 3, 1, 1 }, |
| 52 | /* 8 */ { 4, 1, 5, 3, 1, 2, 1, 1 }, |
| 53 | /* 9 */ { 3, 2, 5, 3, 1, 2, 1, 1 }, |
| 54 | /* 10 */ { 4, 2, 5, 3, 1, 2, 1, 1 }, |
| 55 | /* 11 */ { 4, 3, 5, 3, 1, 2, 1, 1 }, |
| 56 | /* 12 */ { 4, 4, 5, 2, 1, 2, 1, 1 }, |
| 57 | /* 13 */ { 3, 3, MS_NA, MS_NA, MS_A, 3, MS_A, 2 }, |
| 58 | /* 14 */ { 4, 4, MS_NA, MS_NA, MS_A, 3, MS_A, 2 }, |
| 59 | /* 15 */ { 5, 5, MS_NA, MS_NA, MS_A, 3, MS_A, 2 }, |
| 60 | /* 16 */ { 6, 6, MS_NA, MS_NA, MS_A, 2, MS_A, 2 }, |
| 61 | /* 17 */ { 7, 7, MS_NA, MS_NA, MS_A, 1, 0, 2 }, |
| 62 | /* 18 */ { 8, 8, MS_NA, MS_NA, 0, 0, 0, 2 }, |
| 63 | /* 19 */ { 6, 2, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 64 | /* 20 */ { 6, 3, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 65 | /* 21 */ { 6, 4, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 66 | /* 22 */ { 6, 4, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 67 | /* 23 */ { 6, 6, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 68 | /* 24 */ { 8, 2, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 69 | /* 25 */ { 8, 3, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 70 | /* 26 */ { 8, 4, MS_NA, 3, MS_B, 2, MS_C, 1 }, |
| 71 | /* 27 */ { 8, 4, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 72 | /* 28 */ { 8, 6, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 73 | /* 29 */ { 8, 8, MS_NA, 2, MS_B, 2, MS_C, 1 }, |
| 74 | /* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA }, |
| 75 | /* N/A */ { MS_NA,MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA }, |
| 76 | }; |
| 77 | |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 78 | 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] | 79 | { |
| 80 | struct gprs_rlcmac_tbf *tbf; |
| 81 | uint8_t usf_map = 0; |
| 82 | uint8_t tfi, usf; |
| 83 | |
| 84 | /* make map of used USF */ |
| 85 | for (tfi = 0; tfi < 32; tfi++) { |
| 86 | tbf = pdch->ul_tbf[tfi]; |
| 87 | if (!tbf) |
| 88 | continue; |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 89 | usf_map |= (1 << tbf->dir.ul.usf[pdch->ts_no]); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* look for USF, don't use USF=7 */ |
| 93 | for (usf = 0; usf < 7; usf++) { |
| 94 | if (!(usf_map & (1 << usf))) |
| 95 | return usf; |
| 96 | } |
| 97 | |
| 98 | return -1; |
| 99 | } |
| 100 | |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 101 | static int find_enabled_pdch(struct gprs_rlcmac_trx *trx, const uint8_t start_ts) |
| 102 | { |
| 103 | int ts; |
| 104 | for (ts = start_ts; ts < 8; ts++) { |
| 105 | struct gprs_rlcmac_pdch *pdch; |
| 106 | |
| 107 | pdch = &trx->pdch[ts]; |
Holger Hans Peter Freyther | 17b0d83 | 2013-10-19 17:37:48 +0200 | [diff] [blame] | 108 | if (!pdch->is_enabled()) { |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 109 | LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because " |
| 110 | "not enabled\n", ts); |
| 111 | continue; |
| 112 | } |
| 113 | return ts; |
| 114 | } |
| 115 | |
| 116 | return 8; |
| 117 | } |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 118 | |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 119 | static void assign_uplink_tbf_usf( |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 120 | struct gprs_rlcmac_pdch *pdch, |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 121 | struct gprs_rlcmac_tbf *tbf, int8_t usf) |
| 122 | { |
Holger Hans Peter Freyther | 34f6e5e | 2013-10-27 20:31:47 +0100 | [diff] [blame] | 123 | tbf->trx->ul_tbf[tbf->tfi()] = tbf; |
| 124 | pdch->ul_tbf[tbf->tfi()] = tbf; |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 125 | tbf->pdch[pdch->ts_no] = pdch; |
| 126 | tbf->dir.ul.usf[pdch->ts_no] = usf; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Holger Hans Peter Freyther | 8481a05 | 2013-09-29 08:08:28 +0200 | [diff] [blame] | 129 | static void assign_dlink_tbf( |
| 130 | struct gprs_rlcmac_pdch *pdch, |
Holger Hans Peter Freyther | 8481a05 | 2013-09-29 08:08:28 +0200 | [diff] [blame] | 131 | struct gprs_rlcmac_tbf *tbf) |
| 132 | { |
Holger Hans Peter Freyther | 34f6e5e | 2013-10-27 20:31:47 +0100 | [diff] [blame] | 133 | tbf->trx->dl_tbf[tbf->tfi()] = tbf; |
| 134 | pdch->dl_tbf[tbf->tfi()] = tbf; |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 135 | tbf->pdch[pdch->ts_no] = pdch; |
Holger Hans Peter Freyther | 8481a05 | 2013-09-29 08:08:28 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 138 | |
| 139 | /* Slot Allocation: Algorithm A |
| 140 | * |
| 141 | * Assign single slot for uplink and downlink |
| 142 | */ |
| 143 | int alloc_algorithm_a(struct gprs_rlcmac_bts *bts, |
| 144 | struct gprs_rlcmac_tbf *old_tbf, |
| 145 | struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single) |
| 146 | { |
| 147 | struct gprs_rlcmac_pdch *pdch; |
| 148 | uint8_t ts; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 149 | |
| 150 | LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class " |
| 151 | "%d\n", tbf->ms_class); |
| 152 | |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 153 | ts = find_enabled_pdch(tbf->trx, 0); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 154 | if (ts == 8) |
| 155 | return -EINVAL; |
| 156 | |
Holger Hans Peter Freyther | b0a0075 | 2013-09-29 08:18:17 +0200 | [diff] [blame] | 157 | pdch = &tbf->trx->pdch[ts]; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 158 | tbf->tsc = pdch->tsc; |
| 159 | if (tbf->direction == GPRS_RLCMAC_UL_TBF) { |
Holger Hans Peter Freyther | 948a3d6 | 2013-09-30 14:10:23 +0200 | [diff] [blame] | 160 | int8_t usf; /* must be signed */ |
| 161 | |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 162 | /* if USF available */ |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 163 | usf = find_free_usf(pdch); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 164 | if (usf < 0) { |
| 165 | LOGP(DRLCMAC, LOGL_NOTICE, "- Failed " |
| 166 | "allocating TS=%d, no USF available\n", ts); |
| 167 | return -EBUSY; |
| 168 | } |
| 169 | LOGP(DRLCMAC, LOGL_DEBUG, "- Assign uplink " |
| 170 | "TS=%d USF=%d\n", ts, usf); |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 171 | assign_uplink_tbf_usf(pdch, tbf, usf); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 172 | } else { |
| 173 | LOGP(DRLCMAC, LOGL_DEBUG, "- Assign downlink TS=%d\n", ts); |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 174 | assign_dlink_tbf(pdch, tbf); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 175 | } |
| 176 | /* the only one TS is the common TS */ |
| 177 | tbf->first_ts = tbf->first_common_ts = ts; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* Slot Allocation: Algorithm B |
| 183 | * |
| 184 | * Assign as many downlink slots as possible. |
| 185 | * Assign one uplink slot. (With free USF) |
| 186 | * |
| 187 | */ |
| 188 | int alloc_algorithm_b(struct gprs_rlcmac_bts *bts, |
| 189 | struct gprs_rlcmac_tbf *old_tbf, |
| 190 | struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single) |
| 191 | { |
| 192 | struct gprs_rlcmac_pdch *pdch; |
| 193 | const struct gprs_ms_multislot_class *ms_class; |
| 194 | uint8_t Rx, Tx, Sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */ |
| 195 | uint8_t Tta, Ttb, Tra, Trb, Tt, Tr; /* Minimum Number of Slots */ |
| 196 | uint8_t Type; /* Type of Mobile */ |
| 197 | uint8_t rx_win_min = 0, rx_win_max = 7; |
| 198 | uint8_t tx_win_min, tx_win_max, tx_range; |
| 199 | uint8_t rx_window = 0, tx_window = 0; |
| 200 | static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" }; |
| 201 | int8_t usf[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* must be signed */ |
| 202 | int8_t tsc = -1; /* must be signed */ |
| 203 | int8_t first_common_ts = -1; |
| 204 | uint8_t i, ts; |
| 205 | uint8_t slotcount = 0; |
| 206 | |
| 207 | |
| 208 | if (tbf->ms_class >= 32) { |
| 209 | LOGP(DRLCMAC, LOGL_ERROR, "Multislot class %d out of range.\n", |
| 210 | tbf->ms_class); |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | if (tbf->ms_class) { |
| 215 | ms_class = &gprs_ms_multislot_class[tbf->ms_class]; |
| 216 | LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for " |
| 217 | "class %d\n", tbf->ms_class); |
| 218 | } else { |
| 219 | ms_class = &gprs_ms_multislot_class[12]; |
| 220 | LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm B) for " |
| 221 | "unknow class (assuming 12)\n"); |
| 222 | } |
| 223 | |
| 224 | if (ms_class->tx == MS_NA) { |
| 225 | LOGP(DRLCMAC, LOGL_NOTICE, "Multislot class %d not " |
| 226 | "applicable.\n", tbf->ms_class); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | Rx = ms_class->rx; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 231 | Tx = ms_class->tx; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 232 | Sum = ms_class->sum; |
| 233 | Tta = ms_class->ta; |
| 234 | Ttb = ms_class->tb; |
| 235 | Tra = ms_class->ra; |
| 236 | Trb = ms_class->rb; |
| 237 | Type = ms_class->type; |
| 238 | |
| 239 | /* Tta and Ttb may depend on hopping or frequency change */ |
Holger Hans Peter Freyther | 11a7489 | 2013-09-29 08:13:42 +0200 | [diff] [blame] | 240 | if (Ttb == MS_A) |
| 241 | Ttb = 0; |
| 242 | if (Trb == MS_A) |
| 243 | Trb = 0; |
| 244 | if (Ttb == MS_B) |
| 245 | Ttb = 0; |
| 246 | if (Trb == MS_C) |
| 247 | Trb = 0; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 248 | |
| 249 | LOGP(DRLCMAC, LOGL_DEBUG, "- Rx=%d Tx=%d Sum Rx+Tx=%s Tta=%s Ttb=%d " |
| 250 | " Tra=%d Trb=%d Type=%d\n", Rx, Tx, |
| 251 | (Sum == MS_NA) ? "N/A" : digit[Sum], |
| 252 | (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type); |
| 253 | |
| 254 | /* select the values for time contraints */ |
Holger Hans Peter Freyther | 11a7489 | 2013-09-29 08:13:42 +0200 | [diff] [blame] | 255 | /* applicable to type 1 and type 2 */ |
| 256 | Tt = Ttb; |
| 257 | Tr = Trb; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 258 | |
| 259 | /* select a window of Rx slots if available |
| 260 | * The maximum allowed slots depend on RX or the window of available |
| 261 | * slots. |
| 262 | * This must be done for uplink TBF also, because it is the basis |
| 263 | * for calculating control slot and uplink slot(s). */ |
| 264 | for (ts = 0, i = 0; ts < 8; ts++) { |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 265 | pdch = &tbf->trx->pdch[ts]; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 266 | /* check if enabled */ |
Holger Hans Peter Freyther | 17b0d83 | 2013-10-19 17:37:48 +0200 | [diff] [blame] | 267 | if (!pdch->is_enabled()) { |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 268 | LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, because " |
| 269 | "not enabled\n", ts); |
| 270 | /* increase window for Type 1 */ |
| 271 | if (Type == 1 && rx_window) |
| 272 | i++; |
| 273 | continue; |
| 274 | } |
| 275 | /* check if TSC changes */ |
| 276 | if (tsc < 0) |
| 277 | tbf->tsc = tsc = pdch->tsc; |
| 278 | else if (tsc != pdch->tsc) { |
| 279 | LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of TRX=%d, " |
| 280 | "because it has different TSC than lower TS " |
| 281 | "of TRX. In order to allow multislot, all " |
| 282 | "slots must be configured with the same " |
Holger Hans Peter Freyther | ec80f82 | 2013-10-27 13:44:37 +0100 | [diff] [blame] | 283 | "TSC!\n", ts, tbf->trx->trx_no); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 284 | /* increase window for Type 1 */ |
| 285 | if (Type == 1 && rx_window) |
| 286 | i++; |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (!rx_window) |
| 291 | rx_win_min = ts; |
| 292 | |
| 293 | rx_window |= (1 << ts); |
| 294 | LOGP(DRLCMAC, LOGL_DEBUG, "- Selected DL TS %d\n", ts); |
| 295 | |
| 296 | /* range of window (required for Type 1) */ |
| 297 | rx_win_max = ts; |
| 298 | |
| 299 | if (++i == Rx) { |
| 300 | LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because slots / " |
| 301 | "window reached maximum alowed Rx size\n"); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | LOGP(DRLCMAC, LOGL_DEBUG, "- Selected slots for RX: " |
| 307 | "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n", |
| 308 | ((rx_window & 0x01)) ? 'D' : '.', |
| 309 | ((rx_window & 0x02)) ? 'D' : '.', |
| 310 | ((rx_window & 0x04)) ? 'D' : '.', |
| 311 | ((rx_window & 0x08)) ? 'D' : '.', |
| 312 | ((rx_window & 0x10)) ? 'D' : '.', |
| 313 | ((rx_window & 0x20)) ? 'D' : '.', |
| 314 | ((rx_window & 0x40)) ? 'D' : '.', |
| 315 | ((rx_window & 0x80)) ? 'D' : '.'); |
| 316 | |
| 317 | /* reduce window, if existing uplink slots collide RX window */ |
| 318 | if (Type == 1 && old_tbf && old_tbf->direction == GPRS_RLCMAC_UL_TBF) { |
| 319 | uint8_t collide = 0, ul_usage = 0; |
| 320 | int j; |
| 321 | |
| 322 | /* calculate mask of colliding slots */ |
| 323 | for (ts = 0; ts < 8; ts++) { |
| 324 | if (old_tbf->pdch[ts]) { |
| 325 | ul_usage |= (1 << ts); |
| 326 | /* mark bits from TS-t .. TS+r */ |
| 327 | for (j = ts - Tt; j != ((ts + Tr + 1) & 7); |
| 328 | j = (j + 1) & 7) |
| 329 | collide |= (1 << j); |
| 330 | } |
| 331 | } |
| 332 | LOGP(DRLCMAC, LOGL_DEBUG, "- Not allowed slots due to existing " |
| 333 | "UL allocation: (TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7) " |
| 334 | " D=downlink x=not usable\n", |
| 335 | ((ul_usage & 0x01)) ? 'D' : ((collide & 0x01))?'x':'.', |
| 336 | ((ul_usage & 0x02)) ? 'D' : ((collide & 0x02))?'x':'.', |
| 337 | ((ul_usage & 0x04)) ? 'D' : ((collide & 0x04))?'x':'.', |
| 338 | ((ul_usage & 0x08)) ? 'D' : ((collide & 0x08))?'x':'.', |
| 339 | ((ul_usage & 0x10)) ? 'D' : ((collide & 0x10))?'x':'.', |
| 340 | ((ul_usage & 0x20)) ? 'D' : ((collide & 0x20))?'x':'.', |
| 341 | ((ul_usage & 0x40)) ? 'D' : ((collide & 0x40))?'x':'.', |
| 342 | ((ul_usage & 0x80)) ? 'D' : ((collide & 0x80))?'x':'.'); |
| 343 | |
| 344 | /* apply mask to reduce tx_window (shifted by 3 slots) */ |
| 345 | rx_window &= ~(collide << 3); |
| 346 | rx_window &= ~(collide >> 5); |
| 347 | LOGP(DRLCMAC, LOGL_DEBUG, "- Remaining slots for RX: " |
| 348 | "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n", |
| 349 | ((rx_window & 0x01)) ? 'D' : '.', |
| 350 | ((rx_window & 0x02)) ? 'D' : '.', |
| 351 | ((rx_window & 0x04)) ? 'D' : '.', |
| 352 | ((rx_window & 0x08)) ? 'D' : '.', |
| 353 | ((rx_window & 0x10)) ? 'D' : '.', |
| 354 | ((rx_window & 0x20)) ? 'D' : '.', |
| 355 | ((rx_window & 0x40)) ? 'D' : '.', |
| 356 | ((rx_window & 0x80)) ? 'D' : '.'); |
| 357 | if (!rx_window) { |
| 358 | LOGP(DRLCMAC, LOGL_NOTICE, "No suitable downlink slots " |
| 359 | "available with current uplink assignment\n"); |
| 360 | return -EBUSY; |
| 361 | } |
| 362 | |
| 363 | /* calculate new min/max */ |
| 364 | for (ts = rx_win_min; ts <= rx_win_max; ts++) { |
| 365 | if ((rx_window & (1 << ts))) |
| 366 | break; |
| 367 | rx_win_min = ts + 1; |
| 368 | LOGP(DRLCMAC, LOGL_DEBUG, "- TS has been deleted, so " |
| 369 | "raising start of DL window to %d\n", |
| 370 | rx_win_min); |
| 371 | } |
| 372 | for (ts = rx_win_max; ts >= rx_win_min; ts--) { |
| 373 | if ((rx_window & (1 << ts))) |
| 374 | break; |
| 375 | rx_win_max = ts - 1; |
| 376 | LOGP(DRLCMAC, LOGL_DEBUG, "- TS has been deleted, so " |
| 377 | "lowering end of DL window to %d\n", |
| 378 | rx_win_max); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* reduce window, to allow at least one uplink TX slot |
| 383 | * this is only required for Type 1 */ |
| 384 | if (Type == 1 && rx_win_max - rx_win_min + 1 + Tt + 1 + Tr > 8) { |
| 385 | rx_win_max = rx_win_min + 7 - Tt - 1 - Tr; |
| 386 | LOGP(DRLCMAC, LOGL_DEBUG, "- Reduce RX window due to time " |
| 387 | "contraints to %d slots\n", |
| 388 | rx_win_max - rx_win_min + 1); |
| 389 | } |
| 390 | |
| 391 | LOGP(DRLCMAC, LOGL_DEBUG, "- RX-Window is: %d..%d\n", rx_win_min, |
| 392 | rx_win_max); |
| 393 | |
| 394 | /* calculate TX window */ |
| 395 | if (Type == 1) { |
| 396 | /* calculate TX window (shifted by 3 timeslots) |
| 397 | * it uses the space between tx_win_max and tx_win_min */ |
| 398 | tx_win_min = (rx_win_max - 2 + Tt) & 7; |
| 399 | tx_win_max = (rx_win_min + 4 - Tr) & 7; |
| 400 | /* calculate the TX window size (might be larger than Tx) */ |
| 401 | tx_range = (tx_win_max - tx_win_min + 1) & 7; |
| 402 | } else { |
| 403 | /* TX and RX simultaniously */ |
| 404 | tx_win_min = rx_win_min; |
| 405 | tx_win_max = 7; |
| 406 | /* TX window size (might be larger than Tx) */ |
| 407 | tx_range = tx_win_max - tx_win_min + 1; |
| 408 | } |
| 409 | |
| 410 | LOGP(DRLCMAC, LOGL_DEBUG, "- TX-Window is: %d..%d\n", tx_win_min, |
| 411 | tx_win_max); |
| 412 | |
| 413 | /* select a window of Tx slots if available |
| 414 | * The maximum allowed slots depend on TX or the window of available |
| 415 | * slots. |
| 416 | * |
| 417 | * also assign the first common ts, which is used for control or single |
| 418 | * slot. */ |
| 419 | if (tbf->direction == GPRS_RLCMAC_UL_TBF) { |
| 420 | for (ts = tx_win_min, i = 0; i < tx_range; ts = (ts + 1) & 7) { |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 421 | pdch = &tbf->trx->pdch[ts]; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 422 | /* check if enabled */ |
Holger Hans Peter Freyther | 17b0d83 | 2013-10-19 17:37:48 +0200 | [diff] [blame] | 423 | if (!pdch->is_enabled()) { |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 424 | LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, " |
| 425 | "because not enabled\n", ts); |
| 426 | continue; |
| 427 | } |
| 428 | /* check if TSC changes */ |
| 429 | if (tsc < 0) |
| 430 | tbf->tsc = tsc = pdch->tsc; |
| 431 | else if (tsc != pdch->tsc) { |
| 432 | LOGP(DRLCMAC, LOGL_ERROR, "Skipping TS %d of " |
| 433 | "TRX=%d, because it has different TSC " |
| 434 | "than lower TS of TRX. In order to " |
| 435 | "allow multislot, all slots must be " |
| 436 | "configured with the same TSC!\n", |
Holger Hans Peter Freyther | ec80f82 | 2013-10-27 13:44:37 +0100 | [diff] [blame] | 437 | ts, tbf->trx->trx_no); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 438 | /* increase window for Type 1 */ |
| 439 | if (Type == 1) |
| 440 | i++; |
| 441 | continue; |
| 442 | } |
| 443 | /* check for free usf */ |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 444 | usf[ts] = find_free_usf(pdch); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 445 | if (usf[ts] < 0) { |
| 446 | LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, " |
| 447 | "because no USF available\n", ts); |
| 448 | /* increase window for Type 1 */ |
| 449 | if (Type == 1) |
| 450 | i++; |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | if (!tx_window) |
| 455 | first_common_ts = ts; |
| 456 | |
| 457 | tx_window |= (1 << ts); |
| 458 | LOGP(DRLCMAC, LOGL_DEBUG, "- Selected UL TS %d\n", ts); |
| 459 | |
| 460 | if (1 && Type == 1) { /* FIXME: multislot UL assignment */ |
| 461 | LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because " |
| 462 | "1 slot assigned\n"); |
| 463 | break; |
| 464 | } |
| 465 | if (++i == Tx) { |
| 466 | LOGP(DRLCMAC, LOGL_DEBUG, "- Done, because " |
| 467 | "slots / window reached maximum " |
| 468 | "allowed Tx size\n"); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | LOGP(DRLCMAC, LOGL_DEBUG, "- Selected TX window: " |
| 474 | "(TS=0)\"%c%c%c%c%c%c%c%c\"(TS=7)\n", |
| 475 | ((tx_window & 0x01)) ? 'U' : '.', |
| 476 | ((tx_window & 0x02)) ? 'U' : '.', |
| 477 | ((tx_window & 0x04)) ? 'U' : '.', |
| 478 | ((tx_window & 0x08)) ? 'U' : '.', |
| 479 | ((tx_window & 0x10)) ? 'U' : '.', |
| 480 | ((tx_window & 0x20)) ? 'U' : '.', |
| 481 | ((tx_window & 0x40)) ? 'U' : '.', |
| 482 | ((tx_window & 0x80)) ? 'U' : '.'); |
| 483 | |
| 484 | if (!tx_window) { |
| 485 | LOGP(DRLCMAC, LOGL_NOTICE, "No suitable uplink slots " |
| 486 | "available\n"); |
| 487 | return -EBUSY; |
| 488 | } |
| 489 | } else { |
| 490 | /* assign the first common ts, which is used for control or |
| 491 | * single slot. */ |
| 492 | for (ts = tx_win_min, i = 0; i < tx_range; ts = (ts + 1) & 7) { |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 493 | pdch = &tbf->trx->pdch[ts]; |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 494 | /* check if enabled */ |
Holger Hans Peter Freyther | 17b0d83 | 2013-10-19 17:37:48 +0200 | [diff] [blame] | 495 | if (!pdch->is_enabled()) { |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 496 | LOGP(DRLCMAC, LOGL_DEBUG, "- Skipping TS %d, " |
| 497 | "because not enabled\n", ts); |
| 498 | continue; |
| 499 | } |
| 500 | first_common_ts = ts; |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (first_common_ts < 0) { |
| 506 | LOGP(DRLCMAC, LOGL_NOTICE, "No first common slots available\n"); |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
| 510 | if (tbf->direction == GPRS_RLCMAC_DL_TBF) { |
| 511 | /* assign downlink */ |
| 512 | if (rx_window == 0) { |
| 513 | LOGP(DRLCMAC, LOGL_NOTICE, "No downlink slots " |
| 514 | "available\n"); |
| 515 | return -EINVAL; |
| 516 | } |
| 517 | for (ts = 0; ts < 8; ts++) { |
| 518 | if ((rx_window & (1 << ts))) { |
| 519 | /* be sure to select a single downlink slots |
| 520 | * that can be used for uplink, if multiple |
| 521 | * slots are assigned later. */ |
| 522 | if (single && first_common_ts != ts) |
| 523 | continue; |
| 524 | LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning DL TS " |
| 525 | "%d\n", ts); |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 526 | pdch = &tbf->trx->pdch[ts]; |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 527 | assign_dlink_tbf(pdch, tbf); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 528 | slotcount++; |
| 529 | if (slotcount == 1) |
| 530 | tbf->first_ts = ts; |
| 531 | if (single) |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | } else { |
| 536 | /* assign uplink */ |
| 537 | if (tx_window == 0) { |
| 538 | LOGP(DRLCMAC, LOGL_NOTICE, "No uplink slots " |
| 539 | "available\n"); |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | for (ts = 0; ts < 8; ts++) { |
| 543 | if ((tx_window & (1 << ts))) { |
| 544 | LOGP(DRLCMAC, LOGL_DEBUG, "- Assigning UL TS " |
| 545 | "%d\n", ts); |
Holger Hans Peter Freyther | 743bafa | 2013-09-29 07:50:50 +0200 | [diff] [blame] | 546 | pdch = &tbf->trx->pdch[ts]; |
Holger Hans Peter Freyther | 6796ed2 | 2013-10-20 16:45:10 +0200 | [diff] [blame] | 547 | assign_uplink_tbf_usf(pdch, tbf, usf[ts]); |
Holger Hans Peter Freyther | 02ab4a8 | 2013-09-29 07:37:40 +0200 | [diff] [blame] | 548 | slotcount++; |
| 549 | if (slotcount == 1) |
| 550 | tbf->first_ts = ts; |
| 551 | if (single) |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | if (single && slotcount) { |
| 557 | LOGP(DRLCMAC, LOGL_INFO, "Using single slot at TS %d for %s\n", |
| 558 | tbf->first_ts, |
| 559 | (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL"); |
| 560 | } else { |
| 561 | LOGP(DRLCMAC, LOGL_INFO, "Using %d slots for %s\n", slotcount, |
| 562 | (tbf->direction == GPRS_RLCMAC_DL_TBF) ? "DL" : "UL"); |
| 563 | } |
| 564 | if (slotcount == 0) |
| 565 | return -EBUSY; |
| 566 | |
| 567 | tbf->first_common_ts = first_common_ts; |
| 568 | |
| 569 | return 0; |
| 570 | } |