Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1 | /* GSM Channel allocation routines |
| 2 | * |
| 3 | * (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | e64a7a3 | 2009-02-06 21:55:37 +0000 | [diff] [blame] | 4 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
Stefan Sperling | 5b6aa65 | 2018-04-09 13:31:51 +0200 | [diff] [blame] | 27 | #include <inttypes.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 28 | |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 29 | #include <osmocom/bsc/chan_alloc.h> |
| 30 | #include <osmocom/bsc/abis_nm.h> |
| 31 | #include <osmocom/bsc/abis_rsl.h> |
| 32 | #include <osmocom/bsc/debug.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 33 | #include <osmocom/bsc/signal.h> |
| 34 | #include <osmocom/bsc/gsm_04_08_utils.h> |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 35 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 36 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 5ba05f4 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 37 | |
Andreas Eversberg | 7c41cd1 | 2013-06-02 10:36:53 +0200 | [diff] [blame] | 38 | bool ts_is_usable(const struct gsm_bts_trx_ts *ts) |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 39 | { |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 40 | if (!trx_is_usable(ts->trx)) { |
| 41 | LOGP(DRLL, LOGL_DEBUG, "%s not usable\n", gsm_trx_name(ts->trx)); |
Max | 6e4f184 | 2018-01-07 16:45:42 +0100 | [diff] [blame] | 42 | return false; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 43 | } |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 44 | |
Neels Hofmeyr | c6926d0 | 2016-07-14 02:51:13 +0200 | [diff] [blame] | 45 | /* If a TCH/F_PDCH TS is busy changing, it is already taken or not |
Neels Hofmeyr | 832afa3 | 2016-06-14 13:12:00 +0200 | [diff] [blame] | 46 | * yet available. */ |
| 47 | if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) { |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 48 | if (ts->flags & TS_F_PDCH_PENDING_MASK) { |
| 49 | LOGP(DRLL, LOGL_DEBUG, "%s in switchover, not available\n", |
| 50 | gsm_ts_and_pchan_name(ts)); |
Max | d0ff694 | 2017-11-29 12:45:34 +0100 | [diff] [blame] | 51 | return false; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 52 | } |
Neels Hofmeyr | 832afa3 | 2016-06-14 13:12:00 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 55 | /* If a dynamic channel is busy changing, it is already taken or not |
| 56 | * yet available. */ |
| 57 | if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) { |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 58 | if (ts->dyn.pchan_is != ts->dyn.pchan_want) { |
| 59 | LOGP(DRLL, LOGL_DEBUG, "%s in switchover, not available\n", |
| 60 | gsm_ts_and_pchan_name(ts)); |
Max | d0ff694 | 2017-11-29 12:45:34 +0100 | [diff] [blame] | 61 | return false; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 62 | } |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Max | d0ff694 | 2017-11-29 12:45:34 +0100 | [diff] [blame] | 65 | return true; |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Max | d0ff694 | 2017-11-29 12:45:34 +0100 | [diff] [blame] | 68 | bool trx_is_usable(const struct gsm_bts_trx *trx) |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 69 | { |
| 70 | /* FIXME: How does this behave for BS-11 ? */ |
| 71 | if (is_ipaccess_bts(trx->bts)) { |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 72 | if (!nm_is_running(&trx->mo.nm_state) || |
| 73 | !nm_is_running(&trx->bb_transc.mo.nm_state)) |
Max | d0ff694 | 2017-11-29 12:45:34 +0100 | [diff] [blame] | 74 | return false; |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Max | d0ff694 | 2017-11-29 12:45:34 +0100 | [diff] [blame] | 77 | return true; |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Andreas Eversberg | df65c9a | 2013-05-30 10:29:36 +0200 | [diff] [blame] | 80 | static int trx_count_free_ts(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan) |
| 81 | { |
| 82 | struct gsm_bts_trx_ts *ts; |
| 83 | int j, ss; |
| 84 | int count = 0; |
| 85 | |
| 86 | if (!trx_is_usable(trx)) |
| 87 | return 0; |
| 88 | |
| 89 | for (j = 0; j < ARRAY_SIZE(trx->ts); j++) { |
| 90 | enum gsm_phys_chan_config ts_pchan_is; |
| 91 | ts = &trx->ts[j]; |
| 92 | if (!ts_is_usable(ts)) |
| 93 | continue; |
| 94 | |
| 95 | ts_pchan_is = ts_pchan(ts); |
| 96 | |
| 97 | if (ts_pchan_is == GSM_PCHAN_PDCH) { |
| 98 | /* Dynamic timeslots in PDCH mode will become TCH if needed. */ |
| 99 | switch (ts->pchan) { |
| 100 | case GSM_PCHAN_TCH_F_PDCH: |
| 101 | if (pchan == GSM_PCHAN_TCH_F) |
| 102 | count++; |
| 103 | continue; |
| 104 | |
| 105 | case GSM_PCHAN_TCH_F_TCH_H_PDCH: |
| 106 | if (pchan == GSM_PCHAN_TCH_F) |
| 107 | count++; |
| 108 | else if (pchan == GSM_PCHAN_TCH_H) |
| 109 | count += 2; |
| 110 | continue; |
| 111 | |
| 112 | default: |
| 113 | /* Not dynamic, not applicable. */ |
| 114 | continue; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (ts_pchan_is != pchan) |
| 119 | continue; |
| 120 | /* check if all sub-slots are allocated yet */ |
| 121 | for (ss = 0; ss < ts_subslots(ts); ss++) { |
| 122 | struct gsm_lchan *lc = &ts->lchan[ss]; |
| 123 | if (lc->type == GSM_LCHAN_NONE && |
| 124 | lc->state == LCHAN_S_NONE) |
| 125 | count++; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return count; |
| 130 | } |
| 131 | |
| 132 | /* Count number of free TS of given pchan type */ |
| 133 | int bts_count_free_ts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) |
| 134 | { |
| 135 | struct gsm_bts_trx *trx; |
| 136 | int count = 0; |
| 137 | |
| 138 | llist_for_each_entry(trx, &bts->trx_list, list) |
| 139 | count += trx_count_free_ts(trx, pchan); |
| 140 | |
| 141 | return count; |
| 142 | } |
| 143 | |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 144 | static bool ts_usable_as_pchan(struct gsm_bts_trx_ts *ts, |
| 145 | enum gsm_phys_chan_config as_pchan) |
| 146 | { |
| 147 | switch (ts->pchan) { |
| 148 | case GSM_PCHAN_TCH_F_PDCH: |
| 149 | if (ts->flags & TS_F_PDCH_PENDING_MASK) { |
| 150 | /* currently being switched over. Not usable. */ |
| 151 | return false; |
| 152 | } |
| 153 | switch (as_pchan) { |
| 154 | case GSM_PCHAN_TCH_F: |
| 155 | case GSM_PCHAN_PDCH: |
| 156 | /* continue to check below. */ |
| 157 | break; |
| 158 | default: |
| 159 | return false; |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | case GSM_PCHAN_TCH_F_TCH_H_PDCH: |
| 164 | if (ts->dyn.pchan_is != ts->dyn.pchan_want) { |
| 165 | /* currently being switched over. Not usable. */ |
| 166 | return false; |
| 167 | } |
| 168 | switch (as_pchan) { |
| 169 | case GSM_PCHAN_TCH_F: |
| 170 | case GSM_PCHAN_TCH_H: |
| 171 | case GSM_PCHAN_PDCH: |
| 172 | /* continue to check below. */ |
| 173 | break; |
| 174 | default: |
| 175 | return false; |
| 176 | } |
| 177 | break; |
| 178 | |
| 179 | default: |
| 180 | /* static timeslots never switch. */ |
| 181 | return ts->pchan == as_pchan; |
| 182 | } |
| 183 | |
| 184 | /* Dynamic timeslots -- Checks depending on the current actual pchan mode: */ |
| 185 | switch (ts_pchan(ts)) { |
| 186 | case GSM_PCHAN_NONE: |
| 187 | /* Not initialized, possibly because GPRS was disabled. We may switch. */ |
| 188 | return true; |
| 189 | |
| 190 | case GSM_PCHAN_PDCH: |
| 191 | /* This slot is in PDCH mode and available to switch pchan mode. But check for |
| 192 | * error states: */ |
| 193 | if (ts->lchan->state != LCHAN_S_NONE && ts->lchan->state != LCHAN_S_ACTIVE) |
| 194 | return false; |
| 195 | return true; |
| 196 | |
| 197 | case GSM_PCHAN_TCH_F: |
| 198 | case GSM_PCHAN_TCH_H: |
| 199 | /* No need to switch at all? */ |
| 200 | if (ts_pchan(ts) == as_pchan) |
| 201 | return true; |
| 202 | |
| 203 | /* If any lchan is in use, we can't change the pchan kind */ |
| 204 | { |
| 205 | int ss; |
| 206 | int subslots = ts_subslots(ts); |
| 207 | for (ss = 0; ss < subslots; ss++) { |
| 208 | struct gsm_lchan *lc = &ts->lchan[ss]; |
| 209 | if (lc->type != GSM_LCHAN_NONE || lc->state != LCHAN_S_NONE) |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | return true; |
| 214 | |
| 215 | default: |
| 216 | /* Not implemented. */ |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 221 | static struct gsm_lchan * |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 222 | _lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan, |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 223 | enum gsm_phys_chan_config as_pchan) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 224 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 225 | struct gsm_bts_trx_ts *ts; |
Andreas Eversberg | 0434efa | 2013-10-11 13:05:16 +0200 | [diff] [blame] | 226 | int j, start, stop, dir, ss; |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 227 | int check_subslots; |
Harald Welte | 8836726 | 2009-08-10 13:25:55 +0200 | [diff] [blame] | 228 | |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 229 | #define LOGPLCHANALLOC(fmt, args...) \ |
| 230 | LOGP(DRLL, LOGL_DEBUG, "looking for lchan %s as %s: " fmt, \ |
| 231 | gsm_pchan_name(pchan), gsm_pchan_name(as_pchan), ## args) |
| 232 | |
| 233 | if (!trx_is_usable(trx)) { |
| 234 | LOGPLCHANALLOC("%s trx not usable\n", gsm_trx_name(trx)); |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 235 | return NULL; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 236 | } |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 237 | |
Andreas Eversberg | 0434efa | 2013-10-11 13:05:16 +0200 | [diff] [blame] | 238 | if (trx->bts->chan_alloc_reverse) { |
| 239 | /* check TS 7..0 */ |
| 240 | start = 7; |
| 241 | stop = -1; |
| 242 | dir = -1; |
| 243 | } else { |
| 244 | /* check TS 0..7 */ |
| 245 | start = 0; |
| 246 | stop = 8; |
| 247 | dir = 1; |
| 248 | } |
| 249 | |
| 250 | for (j = start; j != stop; j += dir) { |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 251 | ts = &trx->ts[j]; |
Harald Welte | 4c70454 | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 252 | if (!ts_is_usable(ts)) |
| 253 | continue; |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 254 | /* The caller first selects what kind of TS to search in, e.g. looking for exact |
| 255 | * GSM_PCHAN_TCH_F, or maybe among dynamic GSM_PCHAN_TCH_F_TCH_H_PDCH... */ |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 256 | if (ts->pchan != pchan) { |
| 257 | LOGPLCHANALLOC("%s is != %s\n", gsm_ts_and_pchan_name(ts), |
| 258 | gsm_pchan_name(pchan)); |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 259 | continue; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 260 | } |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 261 | /* Next, is this timeslot in or can it be switched to the pchan we want to use it for? */ |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 262 | if (!ts_usable_as_pchan(ts, as_pchan)) { |
| 263 | LOGPLCHANALLOC("%s is not usable as %s\n", gsm_ts_and_pchan_name(ts), |
| 264 | gsm_pchan_name(as_pchan)); |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 265 | continue; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 266 | } |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 267 | /* If we need to switch it, after above check we are also allowed to switch it, and we |
| 268 | * will always use the first lchan after the switch. Return that lchan and rely on the |
| 269 | * caller to perform the pchan switchover. */ |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 270 | if (ts_pchan(ts) != as_pchan) { |
| 271 | LOGPLCHANALLOC("%s is a match, will switch to %s\n", gsm_ts_and_pchan_name(ts), |
| 272 | gsm_pchan_name(as_pchan)); |
Neels Hofmeyr | 8825c69 | 2016-12-05 16:50:47 +0100 | [diff] [blame] | 273 | return ts->lchan; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 274 | } |
Neels Hofmeyr | 8825c69 | 2016-12-05 16:50:47 +0100 | [diff] [blame] | 275 | |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 276 | /* TS is in desired pchan mode. Go ahead and check for an available lchan. */ |
| 277 | check_subslots = ts_subslots(ts); |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 278 | for (ss = 0; ss < check_subslots; ss++) { |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 279 | struct gsm_lchan *lc = &ts->lchan[ss]; |
Harald Welte (local) | 3e46031 | 2009-12-27 18:12:29 +0100 | [diff] [blame] | 280 | if (lc->type == GSM_LCHAN_NONE && |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 281 | lc->state == LCHAN_S_NONE) { |
| 282 | LOGPLCHANALLOC("%s ss=%d is available\n", gsm_ts_and_pchan_name(ts), |
| 283 | lc->nr); |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 284 | return lc; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 285 | } |
| 286 | LOGPLCHANALLOC("%s ss=%d in type=%s,state=%s not suitable\n", |
| 287 | gsm_ts_and_pchan_name(ts), lc->nr, gsm_lchant_name(lc->type), |
| 288 | gsm_lchans_name(lc->state)); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 291 | |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 292 | return NULL; |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 293 | #undef LOGPLCHANALLOC |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static struct gsm_lchan * |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 297 | _lc_dyn_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan, |
| 298 | enum gsm_phys_chan_config dyn_as_pchan) |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 299 | { |
| 300 | struct gsm_bts_trx *trx; |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 301 | struct gsm_lchan *lc; |
| 302 | |
| 303 | if (bts->chan_alloc_reverse) { |
| 304 | llist_for_each_entry_reverse(trx, &bts->trx_list, list) { |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 305 | lc = _lc_find_trx(trx, pchan, dyn_as_pchan); |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 306 | if (lc) |
| 307 | return lc; |
| 308 | } |
| 309 | } else { |
| 310 | llist_for_each_entry(trx, &bts->trx_list, list) { |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 311 | lc = _lc_find_trx(trx, pchan, dyn_as_pchan); |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 312 | if (lc) |
| 313 | return lc; |
| 314 | } |
| 315 | } |
| 316 | |
Harald Welte | f86852c | 2015-01-01 12:46:26 +0100 | [diff] [blame] | 317 | return NULL; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 320 | static struct gsm_lchan * |
| 321 | _lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) |
| 322 | { |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 323 | return _lc_dyn_find_bts(bts, pchan, pchan); |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 326 | /* Allocate a logical channel. |
| 327 | * |
Neels Hofmeyr | c6926d0 | 2016-07-14 02:51:13 +0200 | [diff] [blame] | 328 | * Dynamic channel types: we always prefer a dedicated TS, and only pick + |
| 329 | * switch a dynamic TS if no pure TS of the requested PCHAN is available. |
| 330 | * |
| 331 | * TCH_F/PDCH: if we pick a PDCH ACT style dynamic TS as TCH/F channel, PDCH |
| 332 | * will be disabled in rsl_chan_activate_lchan(); there is no need to check |
| 333 | * whether PDCH mode is currently active, here. |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 334 | */ |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 335 | struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type, |
| 336 | int allow_bigger) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 337 | { |
| 338 | struct gsm_lchan *lchan = NULL; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 339 | enum gsm_phys_chan_config first, first_cbch, second, second_cbch; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 340 | |
Pau Espin Pedrol | 18a4ae8 | 2018-05-30 12:49:47 +0200 | [diff] [blame] | 341 | LOGP(DRLL, LOGL_DEBUG, "(bts=%d) lchan_alloc(%s)\n", bts->nr, gsm_lchant_name(type)); |
Neels Hofmeyr | a70084b | 2018-05-03 16:16:03 +0200 | [diff] [blame] | 342 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 343 | switch (type) { |
| 344 | case GSM_LCHAN_SDCCH: |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 345 | if (bts->chan_alloc_reverse) { |
| 346 | first = GSM_PCHAN_SDCCH8_SACCH8C; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 347 | first_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 348 | second = GSM_PCHAN_CCCH_SDCCH4; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 349 | second_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 350 | } else { |
| 351 | first = GSM_PCHAN_CCCH_SDCCH4; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 352 | first_cbch = GSM_PCHAN_CCCH_SDCCH4_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 353 | second = GSM_PCHAN_SDCCH8_SACCH8C; |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 354 | second_cbch = GSM_PCHAN_SDCCH8_SACCH8C_CBCH; |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | lchan = _lc_find_bts(bts, first); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 358 | if (lchan == NULL) |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 359 | lchan = _lc_find_bts(bts, first_cbch); |
| 360 | if (lchan == NULL) |
Harald Welte | 65676fe | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 361 | lchan = _lc_find_bts(bts, second); |
Harald Welte | 30f1f37 | 2014-12-28 15:00:45 +0100 | [diff] [blame] | 362 | if (lchan == NULL) |
| 363 | lchan = _lc_find_bts(bts, second_cbch); |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 364 | |
| 365 | /* allow to assign bigger channels */ |
| 366 | if (allow_bigger) { |
| 367 | if (lchan == NULL) { |
| 368 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 369 | if (lchan) |
| 370 | type = GSM_LCHAN_TCH_H; |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (lchan == NULL) { |
| 374 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 375 | if (lchan) |
| 376 | type = GSM_LCHAN_TCH_F; |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 377 | } |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 378 | |
| 379 | /* try dynamic TCH/F_PDCH */ |
| 380 | if (lchan == NULL) { |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 381 | lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, |
| 382 | GSM_PCHAN_TCH_F); |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 383 | /* TCH/F_PDCH will be used as TCH/F */ |
| 384 | if (lchan) |
| 385 | type = GSM_LCHAN_TCH_F; |
| 386 | } |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 387 | |
| 388 | /* try fully dynamic TCH/F_TCH/H_PDCH */ |
| 389 | if (lchan == NULL) { |
| 390 | lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 391 | GSM_PCHAN_TCH_H); |
| 392 | if (lchan) |
| 393 | type = GSM_LCHAN_TCH_H; |
| 394 | } |
| 395 | /* |
| 396 | * No need to check fully dynamic channels for TCH/F: |
| 397 | * if no TCH/H was available, neither will be TCH/F. |
| 398 | */ |
Holger Hans Peter Freyther | 457c2a8 | 2010-09-06 08:58:42 +0800 | [diff] [blame] | 399 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 400 | break; |
| 401 | case GSM_LCHAN_TCH_F: |
Harald Welte | fc0d952 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 402 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Harald Welte | a4c63b0 | 2014-05-18 22:23:26 +0200 | [diff] [blame] | 403 | /* If we don't have TCH/F available, fall-back to TCH/H */ |
| 404 | if (!lchan) { |
| 405 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 406 | if (lchan) |
| 407 | type = GSM_LCHAN_TCH_H; |
Harald Welte | a4c63b0 | 2014-05-18 22:23:26 +0200 | [diff] [blame] | 408 | } |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 409 | /* If we don't have TCH/H either, try dynamic TCH/F_PDCH */ |
| 410 | if (!lchan) { |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 411 | lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, |
| 412 | GSM_PCHAN_TCH_F); |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 413 | /* TCH/F_PDCH used as TCH/F -- here, type is already |
| 414 | * set to GSM_LCHAN_TCH_F, but for clarity's sake... */ |
| 415 | if (lchan) |
| 416 | type = GSM_LCHAN_TCH_F; |
| 417 | } |
Neels Hofmeyr | 5f0c71b | 2016-07-23 20:15:28 +0200 | [diff] [blame] | 418 | |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 419 | /* Try fully dynamic TCH/F_TCH/H_PDCH as TCH/F... */ |
Neels Hofmeyr | 5f0c71b | 2016-07-23 20:15:28 +0200 | [diff] [blame] | 420 | if (!lchan && bts->network->dyn_ts_allow_tch_f) { |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 421 | lchan = _lc_dyn_find_bts(bts, |
| 422 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 423 | GSM_PCHAN_TCH_F); |
| 424 | if (lchan) |
| 425 | type = GSM_LCHAN_TCH_F; |
| 426 | } |
| 427 | /* ...and as TCH/H. */ |
| 428 | if (!lchan) { |
| 429 | lchan = _lc_dyn_find_bts(bts, |
| 430 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 431 | GSM_PCHAN_TCH_H); |
| 432 | if (lchan) |
| 433 | type = GSM_LCHAN_TCH_H; |
| 434 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 435 | break; |
| 436 | case GSM_LCHAN_TCH_H: |
Alexander Couzens | fbd96f5 | 2016-08-29 18:40:02 +0200 | [diff] [blame] | 437 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Harald Welte | 210c850 | 2009-12-12 20:58:20 +0100 | [diff] [blame] | 438 | /* If we don't have TCH/H available, fall-back to TCH/F */ |
Harald Welte | 487e6be | 2009-12-12 21:16:38 +0100 | [diff] [blame] | 439 | if (!lchan) { |
Harald Welte | 210c850 | 2009-12-12 20:58:20 +0100 | [diff] [blame] | 440 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Neels Hofmeyr | f5713a5 | 2016-06-06 12:57:22 +0200 | [diff] [blame] | 441 | if (lchan) |
| 442 | type = GSM_LCHAN_TCH_F; |
Harald Welte | 487e6be | 2009-12-12 21:16:38 +0100 | [diff] [blame] | 443 | } |
Neels Hofmeyr | fdd9ad7 | 2016-07-14 03:01:24 +0200 | [diff] [blame] | 444 | /* No dedicated TCH/x available -- try fully dynamic |
| 445 | * TCH/F_TCH/H_PDCH */ |
| 446 | if (!lchan) { |
| 447 | lchan = _lc_dyn_find_bts(bts, |
| 448 | GSM_PCHAN_TCH_F_TCH_H_PDCH, |
| 449 | GSM_PCHAN_TCH_H); |
| 450 | if (lchan) |
| 451 | type = GSM_LCHAN_TCH_H; |
| 452 | } |
| 453 | /* |
| 454 | * No need to check TCH/F_TCH/H_PDCH channels for TCH/F: |
| 455 | * if no TCH/H was available, neither will be TCH/F. |
| 456 | */ |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 457 | /* If we don't have TCH/F either, try dynamic TCH/F_PDCH */ |
| 458 | if (!lchan) { |
Neels Hofmeyr | 4281b7d | 2018-05-08 01:31:19 +0200 | [diff] [blame] | 459 | lchan = _lc_dyn_find_bts(bts, GSM_PCHAN_TCH_F_PDCH, |
| 460 | GSM_PCHAN_TCH_F); |
Neels Hofmeyr | a668525 | 2016-06-06 12:53:25 +0200 | [diff] [blame] | 461 | if (lchan) |
| 462 | type = GSM_LCHAN_TCH_F; |
| 463 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 464 | break; |
| 465 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 466 | LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 469 | if (lchan) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 470 | lchan->type = type; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 471 | |
Neels Hofmeyr | d1c0e37 | 2016-07-25 12:33:02 +0200 | [diff] [blame] | 472 | LOGP(DRLL, LOGL_INFO, "%s Allocating lchan=%u as %s\n", |
| 473 | gsm_ts_and_pchan_name(lchan->ts), |
| 474 | lchan->nr, gsm_lchant_name(lchan->type)); |
| 475 | |
Andreas Eversberg | 970b434 | 2013-06-01 16:46:39 +0200 | [diff] [blame] | 476 | /* reset measurement report counter and index */ |
| 477 | lchan->meas_rep_count = 0; |
| 478 | lchan->meas_rep_idx = 0; |
Neels Hofmeyr | 35ba85c | 2018-02-12 16:47:40 +0100 | [diff] [blame] | 479 | lchan->meas_rep_last_seen_nr = 255; |
Andreas Eversberg | 970b434 | 2013-06-01 16:46:39 +0200 | [diff] [blame] | 480 | |
Holger Hans Peter Freyther | 5ba6f48 | 2009-10-28 14:23:39 +0100 | [diff] [blame] | 481 | /* clear sapis */ |
Holger Hans Peter Freyther | 45b0243 | 2009-11-18 22:59:51 +0100 | [diff] [blame] | 482 | memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis)); |
Holger Hans Peter Freyther | 5ba6f48 | 2009-10-28 14:23:39 +0100 | [diff] [blame] | 483 | |
Holger Hans Peter Freyther | ea52802 | 2009-11-18 22:57:02 +0100 | [diff] [blame] | 484 | /* clear multi rate config */ |
Andreas Eversberg | 7326652 | 2014-01-19 11:47:44 +0100 | [diff] [blame] | 485 | memset(&lchan->mr_ms_lv, 0, sizeof(lchan->mr_ms_lv)); |
| 486 | memset(&lchan->mr_bts_lv, 0, sizeof(lchan->mr_bts_lv)); |
Holger Hans Peter Freyther | 454140e | 2014-12-28 12:08:28 +0100 | [diff] [blame] | 487 | lchan->broken_reason = ""; |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 488 | } else { |
| 489 | struct challoc_signal_data sig; |
Neels Hofmeyr | d1c0e37 | 2016-07-25 12:33:02 +0200 | [diff] [blame] | 490 | |
Pau Espin Pedrol | 18a4ae8 | 2018-05-30 12:49:47 +0200 | [diff] [blame] | 491 | LOGP(DRLL, LOGL_ERROR, "(bts=%d) Failed to allocate %s channel\n", |
| 492 | bts->nr, gsm_lchant_name(type)); |
Neels Hofmeyr | d1c0e37 | 2016-07-25 12:33:02 +0200 | [diff] [blame] | 493 | |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 494 | sig.bts = bts; |
| 495 | sig.type = type; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 496 | osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 497 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 498 | |
| 499 | return lchan; |
| 500 | } |
| 501 | |
| 502 | /* Free a logical channel */ |
| 503 | void lchan_free(struct gsm_lchan *lchan) |
| 504 | { |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 505 | struct challoc_signal_data sig; |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 506 | int i; |
| 507 | |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 508 | sig.type = lchan->type; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 509 | lchan->type = GSM_LCHAN_NONE; |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 510 | |
| 511 | |
Neels Hofmeyr | 968c142 | 2018-05-04 23:01:01 +0200 | [diff] [blame] | 512 | if (lchan->conn |
| 513 | && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH |
| 514 | && lchan->ts->dyn.pchan_is != lchan->ts->dyn.pchan_want)) { |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 515 | struct lchan_signal_data sig; |
| 516 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 517 | /* We might kill an active channel... */ |
Holger Hans Peter Freyther | 08eebd5 | 2010-12-27 13:28:20 +0100 | [diff] [blame] | 518 | sig.lchan = lchan; |
| 519 | sig.mr = NULL; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 520 | osmo_signal_dispatch(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, &sig); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 521 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 522 | |
| 523 | /* stop the timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 524 | osmo_timer_del(&lchan->T3101); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 525 | |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 526 | /* clear cached measuement reports */ |
| 527 | lchan->meas_rep_idx = 0; |
| 528 | for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) { |
| 529 | lchan->meas_rep[i].flags = 0; |
| 530 | lchan->meas_rep[i].nr = 0; |
| 531 | } |
Harald Welte | f7c28b0 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 532 | for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++) |
| 533 | lchan->neigh_meas[i].arfcn = 0; |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 534 | |
Holger Hans Peter Freyther | 5ba05f4 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 535 | if (lchan->rqd_ref) { |
| 536 | talloc_free(lchan->rqd_ref); |
| 537 | lchan->rqd_ref = NULL; |
| 538 | lchan->rqd_ta = 0; |
| 539 | } |
| 540 | |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 541 | sig.lchan = lchan; |
| 542 | sig.bts = lchan->ts->trx->bts; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 543 | osmo_signal_dispatch(SS_CHALLOC, S_CHALLOC_FREED, &sig); |
Harald Welte | c0d83b0 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 544 | |
Neels Hofmeyr | 968c142 | 2018-05-04 23:01:01 +0200 | [diff] [blame] | 545 | if (lchan->conn |
| 546 | && !(lchan->ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH |
| 547 | && lchan->ts->dyn.pchan_is != lchan->ts->dyn.pchan_want)) { |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 548 | LOGP(DRLL, LOGL_ERROR, "the subscriber connection should be gone.\n"); |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 549 | lchan->conn = NULL; |
| 550 | } |
| 551 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 552 | /* FIXME: ts_free() the timeslot, if we're the last logical |
| 553 | * channel using it */ |
| 554 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 555 | |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 556 | /* |
| 557 | * There was an error with the TRX and we need to forget |
| 558 | * any state so that a lchan can be allocated again after |
| 559 | * the trx is fully usable. |
Holger Hans Peter Freyther | f7a1c23 | 2010-06-22 12:19:06 +0800 | [diff] [blame] | 560 | * |
| 561 | * This should be called after lchan_free to force a channel |
| 562 | * be available for allocation again. This means that this |
| 563 | * method will stop the "delay after error"-timer and set the |
| 564 | * state to LCHAN_S_NONE. |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 565 | */ |
| 566 | void lchan_reset(struct gsm_lchan *lchan) |
| 567 | { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 568 | osmo_timer_del(&lchan->T3101); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 569 | osmo_timer_del(&lchan->T3109); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 570 | osmo_timer_del(&lchan->T3111); |
| 571 | osmo_timer_del(&lchan->error_timer); |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 572 | |
| 573 | lchan->type = GSM_LCHAN_NONE; |
Max | dc10ce9 | 2017-09-06 18:20:36 +0200 | [diff] [blame] | 574 | rsl_lchan_set_state(lchan, LCHAN_S_NONE); |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 577 | /* Drive the release process of the lchan */ |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 578 | static void _lchan_handle_release(struct gsm_lchan *lchan, |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 579 | int sacch_deact, int mode) |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 580 | { |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 581 | /* Release all SAPIs on the local end and continue */ |
| 582 | rsl_release_sapis_from(lchan, 1, RSL_REL_LOCAL_END); |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 583 | |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 584 | /* |
| 585 | * Shall we send a RR Release, start T3109 and wait for the |
| 586 | * release indication from the BTS or just take it down (e.g. |
| 587 | * on assignment requests) |
| 588 | */ |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 589 | if (sacch_deact) { |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 590 | gsm48_send_rr_release(lchan); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 591 | |
| 592 | /* Deactivate the SACCH on the BTS side */ |
| 593 | rsl_deact_sacch(lchan); |
| 594 | rsl_start_t3109(lchan); |
Holger Hans Peter Freyther | 006e3d8 | 2012-12-25 23:45:14 +0100 | [diff] [blame] | 595 | } else if (lchan->sapis[0] == LCHAN_SAPI_UNUSED) { |
| 596 | rsl_direct_rf_release(lchan); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 597 | } else { |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 598 | rsl_release_request(lchan, 0, mode); |
Holger Hans Peter Freyther | b348939 | 2011-12-28 16:21:05 +0100 | [diff] [blame] | 599 | } |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 600 | } |
Holger Hans Peter Freyther | 135f797 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 601 | |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 602 | /* Consider releasing the channel now */ |
Holger Hans Peter Freyther | 5ca825e | 2012-12-06 12:01:38 +0100 | [diff] [blame] | 603 | int lchan_release(struct gsm_lchan *lchan, int sacch_deact, enum rsl_rel_mode mode) |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 604 | { |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 605 | DEBUGP(DRLL, "%s starting release sequence\n", gsm_lchan_name(lchan)); |
Holger Hans Peter Freyther | 63d18b5 | 2010-04-10 00:14:55 +0200 | [diff] [blame] | 606 | rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ); |
Holger Hans Peter Freyther | 4b85a32 | 2010-07-29 17:09:36 +0800 | [diff] [blame] | 607 | |
Holger Hans Peter Freyther | abf962b | 2010-11-14 16:04:46 +0100 | [diff] [blame] | 608 | lchan->conn = NULL; |
Holger Hans Peter Freyther | 8582535 | 2011-12-27 22:24:17 +0100 | [diff] [blame] | 609 | _lchan_handle_release(lchan, sacch_deact, mode); |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 610 | return 1; |
| 611 | } |
| 612 | |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 613 | void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 614 | { |
| 615 | struct gsm_bts_trx *trx; |
| 616 | |
| 617 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 618 | int i; |
| 619 | |
| 620 | /* skip administratively deactivated tranxsceivers */ |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 621 | if (!nm_is_running(&trx->mo.nm_state) || |
| 622 | !nm_is_running(&trx->bb_transc.mo.nm_state)) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 623 | continue; |
| 624 | |
| 625 | for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { |
| 626 | struct gsm_bts_trx_ts *ts = &trx->ts[i]; |
| 627 | struct load_counter *pl = &cl->pchan[ts->pchan]; |
| 628 | int j; |
Neels Hofmeyr | 3673380 | 2016-07-29 18:10:59 +0200 | [diff] [blame] | 629 | int subslots; |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 630 | |
| 631 | /* skip administratively deactivated timeslots */ |
Harald Welte | d64c0bc | 2011-05-30 12:07:53 +0200 | [diff] [blame] | 632 | if (!nm_is_running(&ts->mo.nm_state)) |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 633 | continue; |
| 634 | |
Neels Hofmeyr | 3673380 | 2016-07-29 18:10:59 +0200 | [diff] [blame] | 635 | subslots = ts_subslots(ts); |
| 636 | for (j = 0; j < subslots; j++) { |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 637 | struct gsm_lchan *lchan = &ts->lchan[j]; |
| 638 | |
| 639 | pl->total++; |
| 640 | |
| 641 | switch (lchan->state) { |
| 642 | case LCHAN_S_NONE: |
| 643 | break; |
| 644 | default: |
| 645 | pl->used++; |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | void network_chan_load(struct pchan_load *pl, struct gsm_network *net) |
| 654 | { |
| 655 | struct gsm_bts *bts; |
| 656 | |
| 657 | memset(pl, 0, sizeof(*pl)); |
| 658 | |
| 659 | llist_for_each_entry(bts, &net->bts_list, list) |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 660 | bts_chan_load(pl, bts); |
Harald Welte | b908cb7 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 661 | } |
Neels Hofmeyr | 2afffd5 | 2016-09-25 17:01:20 +0200 | [diff] [blame] | 662 | |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 663 | /* Update T3122 wait indicator based on samples of BTS channel load. */ |
| 664 | void |
| 665 | bts_update_t3122_chan_load(struct gsm_bts *bts) |
| 666 | { |
| 667 | struct pchan_load pl; |
| 668 | uint64_t used = 0; |
| 669 | uint32_t total = 0; |
| 670 | uint64_t load; |
| 671 | uint64_t wait_ind; |
| 672 | static const uint8_t min_wait_ind = GSM_T3122_DEFAULT; |
| 673 | static const uint8_t max_wait_ind = 128; /* max wait ~2 minutes */ |
| 674 | int i; |
| 675 | |
Neels Hofmeyr | f802f7f | 2018-02-15 18:46:39 +0100 | [diff] [blame] | 676 | /* Ignore BTS that are not in operation, in order to not flood the log with "bogus channel load" |
| 677 | * messages */ |
| 678 | if (!trx_is_usable(bts->c0)) |
| 679 | return; |
| 680 | |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 681 | /* Sum up current load across all channels. */ |
| 682 | memset(&pl, 0, sizeof(pl)); |
| 683 | bts_chan_load(&pl, bts); |
| 684 | for (i = 0; i < ARRAY_SIZE(pl.pchan); i++) { |
| 685 | struct load_counter *lc = &pl.pchan[i]; |
| 686 | |
| 687 | /* Ignore samples too large for fixed-point calculations (shouldn't happen). */ |
| 688 | if (lc->used > UINT16_MAX || lc->total > UINT16_MAX) { |
| 689 | LOGP(DRLL, LOGL_NOTICE, "(bts=%d) numbers in channel load sample " |
| 690 | "too large (used=%u / total=%u)\n", bts->nr, lc->used, lc->total); |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | used += lc->used; |
| 695 | total += lc->total; |
| 696 | } |
| 697 | |
| 698 | /* Check for invalid samples (shouldn't happen). */ |
| 699 | if (total == 0 || used > total) { |
Pau Espin Pedrol | fa42255 | 2018-04-19 14:31:13 +0200 | [diff] [blame] | 700 | LOGP(DRLL, LOGL_NOTICE, "(bts=%d) bogus channel load sample (used=%"PRIu64" / total=%"PRIu32")\n", |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 701 | bts->nr, used, total); |
| 702 | bts->T3122 = 0; /* disable override of network-wide default value */ |
| 703 | bts->chan_load_samples_idx = 0; /* invalidate other samples collected so far */ |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | /* If we haven't got enough samples yet, store measurement for later use. */ |
| 708 | if (bts->chan_load_samples_idx < ARRAY_SIZE(bts->chan_load_samples)) { |
| 709 | struct load_counter *sample = &bts->chan_load_samples[bts->chan_load_samples_idx++]; |
| 710 | sample->total = (unsigned int)total; |
| 711 | sample->used = (unsigned int)used; |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | /* We have enough samples and will overwrite our current samples later. */ |
| 716 | bts->chan_load_samples_idx = 0; |
| 717 | |
| 718 | /* Add all previous samples to the current sample. */ |
| 719 | for (i = 0; i < ARRAY_SIZE(bts->chan_load_samples); i++) { |
| 720 | struct load_counter *sample = &bts->chan_load_samples[i]; |
| 721 | total += sample->total; |
| 722 | used += sample->used; |
| 723 | } |
| 724 | |
| 725 | used <<= 8; /* convert to fixed-point */ |
| 726 | |
| 727 | /* Log channel load average. */ |
| 728 | load = ((used / total) * 100); |
Stefan Sperling | 5b6aa65 | 2018-04-09 13:31:51 +0200 | [diff] [blame] | 729 | LOGP(DRLL, LOGL_DEBUG, "(bts=%d) channel load average is %"PRIu64".%.2"PRIu64"%%\n", |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 730 | bts->nr, (load & 0xffffff00) >> 8, (load & 0xff) / 10); |
Stefan Sperling | 6442e43 | 2018-02-06 14:44:54 +0100 | [diff] [blame] | 731 | bts->chan_load_avg = ((load & 0xffffff00) >> 8); |
| 732 | OSMO_ASSERT(bts->chan_load_avg <= 100); |
| 733 | osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_CHAN_LOAD_AVERAGE], bts->chan_load_avg); |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 734 | |
| 735 | /* Calculate new T3122 wait indicator. */ |
| 736 | wait_ind = ((used / total) * max_wait_ind); |
| 737 | wait_ind >>= 8; /* convert from fixed-point to integer */ |
| 738 | if (wait_ind < min_wait_ind) |
| 739 | wait_ind = min_wait_ind; |
| 740 | else if (wait_ind > max_wait_ind) |
| 741 | wait_ind = max_wait_ind; |
| 742 | |
Pau Espin Pedrol | fa42255 | 2018-04-19 14:31:13 +0200 | [diff] [blame] | 743 | LOGP(DRLL, LOGL_DEBUG, "(bts=%d) T3122 wait indicator set to %"PRIu64" seconds\n", bts->nr, wait_ind); |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 744 | bts->T3122 = (uint8_t)wait_ind; |
Stefan Sperling | 81dc9e7 | 2018-02-05 17:34:36 +0100 | [diff] [blame] | 745 | osmo_stat_item_set(bts->bts_statg->items[BTS_STAT_T3122], wait_ind); |
Stefan Sperling | 6cee893 | 2018-01-30 18:14:22 +0100 | [diff] [blame] | 746 | } |