Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* GSM Channel allocation routines |
| 2 | * |
| 3 | * (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | |
| 29 | #include <openbsc/gsm_data.h> |
| 30 | #include <openbsc/chan_alloc.h> |
| 31 | #include <openbsc/abis_nm.h> |
| 32 | #include <openbsc/abis_rsl.h> |
| 33 | #include <openbsc/debug.h> |
| 34 | #include <openbsc/signal.h> |
| 35 | |
Holger Hans Peter Freyther | c08f6f0 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 36 | #include <osmocore/talloc.h> |
| 37 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 38 | static void auto_release_channel(void *_lchan); |
| 39 | |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 40 | static int ts_is_usable(struct gsm_bts_trx_ts *ts) |
| 41 | { |
| 42 | /* FIXME: How does this behave for BS-11 ? */ |
| 43 | if (is_ipaccess_bts(ts->trx->bts)) { |
Sylvain Munaut | ca3e04f | 2010-01-02 16:32:17 +0100 | [diff] [blame] | 44 | if (!nm_is_running(&ts->nm_state)) |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | return 1; |
| 49 | } |
| 50 | |
Harald Welte (local) | 43db2bf | 2009-12-28 16:36:28 +0100 | [diff] [blame] | 51 | int trx_is_usable(struct gsm_bts_trx *trx) |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 52 | { |
| 53 | /* FIXME: How does this behave for BS-11 ? */ |
| 54 | if (is_ipaccess_bts(trx->bts)) { |
Sylvain Munaut | ca3e04f | 2010-01-02 16:32:17 +0100 | [diff] [blame] | 55 | if (!nm_is_running(&trx->nm_state) || |
| 56 | !nm_is_running(&trx->bb_transc.nm_state)) |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | return 1; |
| 61 | } |
| 62 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 63 | struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts, |
| 64 | enum gsm_phys_chan_config pchan) |
| 65 | { |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 66 | struct gsm_bts_trx *trx = bts->c0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 67 | struct gsm_bts_trx_ts *ts = &trx->ts[0]; |
| 68 | |
| 69 | if (pchan != GSM_PCHAN_CCCH && |
| 70 | pchan != GSM_PCHAN_CCCH_SDCCH4) |
| 71 | return NULL; |
| 72 | |
| 73 | if (ts->pchan != GSM_PCHAN_NONE) |
| 74 | return NULL; |
| 75 | |
| 76 | ts->pchan = pchan; |
| 77 | |
| 78 | return ts; |
| 79 | } |
| 80 | |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 81 | /* Allocate a physical channel (TS) */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 82 | struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts, |
| 83 | enum gsm_phys_chan_config pchan) |
| 84 | { |
Harald Welte | 351ff12 | 2009-08-10 13:25:55 +0200 | [diff] [blame] | 85 | int j; |
| 86 | struct gsm_bts_trx *trx; |
| 87 | |
| 88 | llist_for_each_entry(trx, &bts->trx_list, list) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 89 | int from, to; |
| 90 | |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 91 | if (!trx_is_usable(trx)) |
| 92 | continue; |
| 93 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 94 | /* the following constraints are pure policy, |
| 95 | * no requirement to put this restriction in place */ |
Harald Welte | 3a9c2c0 | 2009-07-29 16:46:37 +0200 | [diff] [blame] | 96 | if (trx == bts->c0) { |
| 97 | /* On the first TRX we run one CCCH and one SDCCH8 */ |
| 98 | switch (pchan) { |
| 99 | case GSM_PCHAN_CCCH: |
| 100 | case GSM_PCHAN_CCCH_SDCCH4: |
| 101 | from = 0; to = 0; |
| 102 | break; |
Harald Welte | 3a9c2c0 | 2009-07-29 16:46:37 +0200 | [diff] [blame] | 103 | case GSM_PCHAN_TCH_F: |
| 104 | case GSM_PCHAN_TCH_H: |
Harald Welte | 7f863f5 | 2009-08-09 22:01:26 +0200 | [diff] [blame] | 105 | from = 1; to = 7; |
Harald Welte | 3a9c2c0 | 2009-07-29 16:46:37 +0200 | [diff] [blame] | 106 | break; |
Harald Welte | 7f863f5 | 2009-08-09 22:01:26 +0200 | [diff] [blame] | 107 | case GSM_PCHAN_SDCCH8_SACCH8C: |
Harald Welte | 3a9c2c0 | 2009-07-29 16:46:37 +0200 | [diff] [blame] | 108 | default: |
| 109 | return NULL; |
| 110 | } |
| 111 | } else { |
| 112 | /* Every secondary TRX is configured for TCH/F |
| 113 | * and TCH/H only */ |
| 114 | switch (pchan) { |
Harald Welte | 7f863f5 | 2009-08-09 22:01:26 +0200 | [diff] [blame] | 115 | case GSM_PCHAN_SDCCH8_SACCH8C: |
| 116 | from = 1; to = 1; |
Harald Welte | 3a9c2c0 | 2009-07-29 16:46:37 +0200 | [diff] [blame] | 117 | case GSM_PCHAN_TCH_F: |
| 118 | case GSM_PCHAN_TCH_H: |
Harald Welte | 7f863f5 | 2009-08-09 22:01:26 +0200 | [diff] [blame] | 119 | from = 1; to = 7; |
Harald Welte | 3a9c2c0 | 2009-07-29 16:46:37 +0200 | [diff] [blame] | 120 | break; |
| 121 | default: |
| 122 | return NULL; |
| 123 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | for (j = from; j <= to; j++) { |
| 127 | struct gsm_bts_trx_ts *ts = &trx->ts[j]; |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 128 | |
| 129 | if (!ts_is_usable(ts)) |
| 130 | continue; |
| 131 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 132 | if (ts->pchan == GSM_PCHAN_NONE) { |
| 133 | ts->pchan = pchan; |
| 134 | /* set channel attribute on OML */ |
Harald Welte | 35cd5e9 | 2009-08-10 12:21:22 +0200 | [diff] [blame] | 135 | abis_nm_set_channel_attr(ts, abis_nm_chcomb4pchan(pchan)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 136 | return ts; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | /* Free a physical channel (TS) */ |
| 144 | void ts_free(struct gsm_bts_trx_ts *ts) |
| 145 | { |
| 146 | ts->pchan = GSM_PCHAN_NONE; |
| 147 | } |
| 148 | |
| 149 | static const u_int8_t subslots_per_pchan[] = { |
| 150 | [GSM_PCHAN_NONE] = 0, |
| 151 | [GSM_PCHAN_CCCH] = 0, |
| 152 | [GSM_PCHAN_CCCH_SDCCH4] = 4, |
| 153 | [GSM_PCHAN_TCH_F] = 1, |
| 154 | [GSM_PCHAN_TCH_H] = 2, |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 155 | [GSM_PCHAN_SDCCH8_SACCH8C] = 8, |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 156 | /* FIXME: what about dynamic TCH_F_TCH_H ? */ |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 157 | [GSM_PCHAN_TCH_F_PDCH] = 1, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static struct gsm_lchan * |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 161 | _lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 162 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 163 | struct gsm_bts_trx_ts *ts; |
Harald Welte | 351ff12 | 2009-08-10 13:25:55 +0200 | [diff] [blame] | 164 | int j, ss; |
| 165 | |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 166 | if (!trx_is_usable(trx)) |
| 167 | return NULL; |
| 168 | |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 169 | for (j = 0; j < 8; j++) { |
| 170 | ts = &trx->ts[j]; |
Harald Welte | edc7b2f | 2009-12-24 10:10:16 +0100 | [diff] [blame] | 171 | if (!ts_is_usable(ts)) |
| 172 | continue; |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 173 | /* ip.access dynamic TCH/F + PDCH combination */ |
| 174 | if (ts->pchan == GSM_PCHAN_TCH_F_PDCH && |
| 175 | pchan == GSM_PCHAN_TCH_F) { |
| 176 | /* we can only consider such a dynamic channel |
| 177 | * if the PDCH is currently inactive */ |
| 178 | if (ts->flags & TS_F_PDCH_MODE) |
| 179 | continue; |
| 180 | } else if (ts->pchan != pchan) |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 181 | continue; |
| 182 | /* check if all sub-slots are allocated yet */ |
| 183 | for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) { |
| 184 | struct gsm_lchan *lc = &ts->lchan[ss]; |
Harald Welte (local) | c3be50c | 2009-12-27 18:12:29 +0100 | [diff] [blame] | 185 | if (lc->type == GSM_LCHAN_NONE && |
| 186 | lc->state == LCHAN_S_NONE) |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 187 | return lc; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 188 | } |
| 189 | } |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 190 | |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | static struct gsm_lchan * |
| 195 | _lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) |
| 196 | { |
| 197 | struct gsm_bts_trx *trx; |
| 198 | struct gsm_bts_trx_ts *ts; |
| 199 | struct gsm_lchan *lc; |
| 200 | |
| 201 | if (bts->chan_alloc_reverse) { |
| 202 | llist_for_each_entry_reverse(trx, &bts->trx_list, list) { |
| 203 | lc = _lc_find_trx(trx, pchan); |
| 204 | if (lc) |
| 205 | return lc; |
| 206 | } |
| 207 | } else { |
| 208 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 209 | lc = _lc_find_trx(trx, pchan); |
| 210 | if (lc) |
| 211 | return lc; |
| 212 | } |
| 213 | } |
| 214 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 215 | /* we cannot allocate more of these */ |
| 216 | if (pchan == GSM_PCHAN_CCCH_SDCCH4) |
| 217 | return NULL; |
| 218 | |
| 219 | /* if we've reached here, we need to allocate a new physical |
| 220 | * channel for the logical channel type requested */ |
| 221 | ts = ts_alloc(bts, pchan); |
| 222 | if (!ts) { |
| 223 | /* no more radio resources */ |
| 224 | return NULL; |
| 225 | } |
| 226 | return &ts->lchan[0]; |
| 227 | } |
| 228 | |
| 229 | /* Allocate a logical channel */ |
| 230 | struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type) |
| 231 | { |
| 232 | struct gsm_lchan *lchan = NULL; |
Harald Welte | af86c0f | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 233 | enum gsm_phys_chan_config first, second; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 234 | |
| 235 | switch (type) { |
| 236 | case GSM_LCHAN_SDCCH: |
Harald Welte | af86c0f | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 237 | if (bts->chan_alloc_reverse) { |
| 238 | first = GSM_PCHAN_SDCCH8_SACCH8C; |
| 239 | second = GSM_PCHAN_CCCH_SDCCH4; |
| 240 | } else { |
| 241 | first = GSM_PCHAN_CCCH_SDCCH4; |
| 242 | second = GSM_PCHAN_SDCCH8_SACCH8C; |
| 243 | } |
| 244 | |
| 245 | lchan = _lc_find_bts(bts, first); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 246 | if (lchan == NULL) |
Harald Welte | af86c0f | 2009-08-10 14:44:24 +0200 | [diff] [blame] | 247 | lchan = _lc_find_bts(bts, second); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 248 | break; |
| 249 | case GSM_LCHAN_TCH_F: |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 250 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 251 | break; |
| 252 | case GSM_LCHAN_TCH_H: |
Harald Welte | ceb3c04 | 2009-08-10 13:46:55 +0200 | [diff] [blame] | 253 | lchan =_lc_find_bts(bts, GSM_PCHAN_TCH_H); |
Harald Welte | 1250337 | 2009-12-12 20:58:20 +0100 | [diff] [blame] | 254 | /* If we don't have TCH/H available, fall-back to TCH/F */ |
Harald Welte | 5ea9e8e | 2009-12-12 21:16:38 +0100 | [diff] [blame] | 255 | if (!lchan) { |
Harald Welte | 1250337 | 2009-12-12 20:58:20 +0100 | [diff] [blame] | 256 | lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F); |
Harald Welte | 5ea9e8e | 2009-12-12 21:16:38 +0100 | [diff] [blame] | 257 | type = GSM_LCHAN_TCH_F; |
| 258 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 259 | break; |
| 260 | default: |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 261 | LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (lchan) { |
| 265 | lchan->type = type; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 266 | |
Holger Hans Peter Freyther | d831805 | 2009-10-28 14:23:39 +0100 | [diff] [blame] | 267 | /* clear sapis */ |
Holger Hans Peter Freyther | 29afdc4 | 2009-11-18 22:59:51 +0100 | [diff] [blame] | 268 | memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis)); |
Holger Hans Peter Freyther | d831805 | 2009-10-28 14:23:39 +0100 | [diff] [blame] | 269 | |
Holger Hans Peter Freyther | 3cce58f | 2009-11-18 22:57:02 +0100 | [diff] [blame] | 270 | /* clear multi rate config */ |
| 271 | memset(&lchan->mr_conf, 0, sizeof(lchan->mr_conf)); |
| 272 | |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 273 | /* clear per MSC/BSC data */ |
| 274 | memset(&lchan->conn, 0, sizeof(lchan->conn)); |
| 275 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 276 | /* Configure the time and start it so it will be closed */ |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 277 | lchan->conn.lchan = lchan; |
Holger Hans Peter Freyther | e978184 | 2010-03-23 07:52:17 +0100 | [diff] [blame] | 278 | lchan->conn.bts = lchan->ts->trx->bts; |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 279 | lchan->conn.release_timer.cb = auto_release_channel; |
| 280 | lchan->conn.release_timer.data = lchan; |
| 281 | bsc_schedule_timer(&lchan->conn.release_timer, LCHAN_RELEASE_TIMEOUT); |
| 282 | |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 283 | } else { |
| 284 | struct challoc_signal_data sig; |
| 285 | sig.bts = bts; |
| 286 | sig.type = type; |
| 287 | dispatch_signal(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return lchan; |
| 291 | } |
| 292 | |
| 293 | /* Free a logical channel */ |
| 294 | void lchan_free(struct gsm_lchan *lchan) |
| 295 | { |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 296 | struct challoc_signal_data sig; |
Harald Welte | f947681 | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 297 | int i; |
| 298 | |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 299 | sig.type = lchan->type; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 300 | lchan->type = GSM_LCHAN_NONE; |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 301 | if (lchan->conn.subscr) { |
| 302 | subscr_put(lchan->conn.subscr); |
| 303 | lchan->conn.subscr = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* We might kill an active channel... */ |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 307 | if (lchan->conn.use_count != 0) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 308 | dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan); |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 309 | lchan->conn.use_count = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* stop the timer */ |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 313 | bsc_del_timer(&lchan->conn.release_timer); |
Sylvain Munaut | fe787f0 | 2009-12-21 01:11:25 +0100 | [diff] [blame] | 314 | bsc_del_timer(&lchan->T3101); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 315 | |
Harald Welte | f947681 | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 316 | /* clear cached measuement reports */ |
| 317 | lchan->meas_rep_idx = 0; |
| 318 | for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) { |
| 319 | lchan->meas_rep[i].flags = 0; |
| 320 | lchan->meas_rep[i].nr = 0; |
| 321 | } |
Harald Welte | 92d8891 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 322 | for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++) |
| 323 | lchan->neigh_meas[i].arfcn = 0; |
Harald Welte | f947681 | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 324 | |
Holger Hans Peter Freyther | c08f6f0 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 325 | if (lchan->rqd_ref) { |
| 326 | talloc_free(lchan->rqd_ref); |
| 327 | lchan->rqd_ref = NULL; |
| 328 | lchan->rqd_ta = 0; |
| 329 | } |
| 330 | |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 331 | lchan->conn.silent_call = 0; |
Harald Welte | 51bd066 | 2009-12-29 11:17:18 +0100 | [diff] [blame] | 332 | |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 333 | sig.lchan = lchan; |
| 334 | sig.bts = lchan->ts->trx->bts; |
| 335 | dispatch_signal(SS_CHALLOC, S_CHALLOC_FREED, &sig); |
| 336 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 337 | /* FIXME: ts_free() the timeslot, if we're the last logical |
| 338 | * channel using it */ |
| 339 | } |
| 340 | |
Holger Hans Peter Freyther | 5a8b2b4 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 341 | /* |
| 342 | * There was an error with the TRX and we need to forget |
| 343 | * any state so that a lchan can be allocated again after |
| 344 | * the trx is fully usable. |
Holger Hans Peter Freyther | 66a3c77 | 2010-06-22 12:19:06 +0800 | [diff] [blame] | 345 | * |
| 346 | * This should be called after lchan_free to force a channel |
| 347 | * be available for allocation again. This means that this |
| 348 | * method will stop the "delay after error"-timer and set the |
| 349 | * state to LCHAN_S_NONE. |
Holger Hans Peter Freyther | 5a8b2b4 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 350 | */ |
| 351 | void lchan_reset(struct gsm_lchan *lchan) |
| 352 | { |
| 353 | bsc_del_timer(&lchan->T3101); |
Holger Hans Peter Freyther | fb03622 | 2010-06-08 12:12:26 +0800 | [diff] [blame] | 354 | bsc_del_timer(&lchan->T3111); |
| 355 | bsc_del_timer(&lchan->error_timer); |
Holger Hans Peter Freyther | 5a8b2b4 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 356 | |
| 357 | lchan->type = GSM_LCHAN_NONE; |
| 358 | lchan->state = LCHAN_S_NONE; |
| 359 | } |
| 360 | |
| 361 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 362 | /* Consider releasing the channel now */ |
| 363 | int lchan_auto_release(struct gsm_lchan *lchan) |
| 364 | { |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 365 | if (lchan->conn.use_count > 0) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* Assume we have GSM04.08 running and send a release */ |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 370 | if (lchan->conn.subscr) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 371 | gsm48_send_rr_release(lchan); |
| 372 | } |
| 373 | |
| 374 | /* spoofed? message */ |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 375 | if (lchan->conn.use_count < 0) |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 376 | LOGP(DRLL, LOGL_ERROR, "Channel count is negative: %d\n", |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 377 | lchan->conn.use_count); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 378 | |
Harald Welte (local) | 0a77fa1 | 2009-12-28 23:22:43 +0100 | [diff] [blame] | 379 | DEBUGP(DRLL, "%s Recycling Channel\n", gsm_lchan_name(lchan)); |
Holger Hans Peter Freyther | 79a62f4 | 2010-04-10 00:14:55 +0200 | [diff] [blame] | 380 | rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ); |
Holger Hans Peter Freyther | bcea9a7 | 2010-06-08 11:57:45 +0800 | [diff] [blame] | 381 | rsl_release_request(lchan, 0, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | /* Auto release the channel when the use count is zero */ |
| 386 | static void auto_release_channel(void *_lchan) |
| 387 | { |
| 388 | struct gsm_lchan *lchan = _lchan; |
| 389 | |
| 390 | if (!lchan_auto_release(lchan)) |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 391 | bsc_schedule_timer(&lchan->conn.release_timer, LCHAN_RELEASE_TIMEOUT); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Holger Hans Peter Freyther | 8bf60d4 | 2010-06-16 13:05:35 +0800 | [diff] [blame] | 394 | static struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) { |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 395 | struct gsm_bts_trx *trx; |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 396 | int ts_no, lchan_no; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 397 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 398 | llist_for_each_entry(trx, &bts->trx_list, list) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 399 | for (ts_no = 0; ts_no < 8; ++ts_no) { |
| 400 | for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) { |
| 401 | struct gsm_lchan *lchan = |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 402 | &trx->ts[ts_no].lchan[lchan_no]; |
Holger Hans Peter Freyther | 065b811 | 2010-03-23 06:41:45 +0100 | [diff] [blame] | 403 | if (subscr == lchan->conn.subscr) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 404 | return lchan; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return NULL; |
| 410 | } |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 411 | |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 412 | struct gsm_subscriber_connection *connection_for_subscr(struct gsm_subscriber *subscr) |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 413 | { |
| 414 | struct gsm_bts *bts; |
| 415 | struct gsm_network *net = subscr->net; |
| 416 | struct gsm_lchan *lchan; |
| 417 | |
| 418 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 419 | lchan = lchan_find(bts, subscr); |
| 420 | if (lchan) |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 421 | return &lchan->conn; |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Holger Hans Peter Freyther | 3265b1a | 2009-10-27 10:42:53 +0100 | [diff] [blame] | 424 | return NULL; |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 425 | } |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 426 | |
| 427 | void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts) |
| 428 | { |
| 429 | struct gsm_bts_trx *trx; |
| 430 | |
| 431 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 432 | int i; |
| 433 | |
| 434 | /* skip administratively deactivated tranxsceivers */ |
Sylvain Munaut | ca3e04f | 2010-01-02 16:32:17 +0100 | [diff] [blame] | 435 | if (!nm_is_running(&trx->nm_state) || |
| 436 | !nm_is_running(&trx->bb_transc.nm_state)) |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 437 | continue; |
| 438 | |
| 439 | for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { |
| 440 | struct gsm_bts_trx_ts *ts = &trx->ts[i]; |
| 441 | struct load_counter *pl = &cl->pchan[ts->pchan]; |
| 442 | int j; |
| 443 | |
| 444 | /* skip administratively deactivated timeslots */ |
Sylvain Munaut | ca3e04f | 2010-01-02 16:32:17 +0100 | [diff] [blame] | 445 | if (!nm_is_running(&ts->nm_state)) |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 446 | continue; |
| 447 | |
| 448 | for (j = 0; j < subslots_per_pchan[ts->pchan]; j++) { |
| 449 | struct gsm_lchan *lchan = &ts->lchan[j]; |
| 450 | |
| 451 | pl->total++; |
| 452 | |
| 453 | switch (lchan->state) { |
| 454 | case LCHAN_S_NONE: |
| 455 | break; |
| 456 | default: |
| 457 | pl->used++; |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void network_chan_load(struct pchan_load *pl, struct gsm_network *net) |
| 466 | { |
| 467 | struct gsm_bts *bts; |
| 468 | |
| 469 | memset(pl, 0, sizeof(*pl)); |
| 470 | |
| 471 | llist_for_each_entry(bts, &net->bts_list, list) |
| 472 | bts_chan_load(pl, bts); |
| 473 | } |