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 */ |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 274 | if (lchan->conn) { |
| 275 | LOGP(DRLL, LOGL_ERROR, "lchan->conn should be NULL.\n"); |
| 276 | subscr_con_free(lchan->conn); |
| 277 | } |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 278 | } else { |
| 279 | struct challoc_signal_data sig; |
| 280 | sig.bts = bts; |
| 281 | sig.type = type; |
| 282 | dispatch_signal(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return lchan; |
| 286 | } |
| 287 | |
| 288 | /* Free a logical channel */ |
| 289 | void lchan_free(struct gsm_lchan *lchan) |
| 290 | { |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 291 | struct challoc_signal_data sig; |
Harald Welte | f947681 | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 292 | int i; |
| 293 | |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 294 | sig.type = lchan->type; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 295 | lchan->type = GSM_LCHAN_NONE; |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 296 | |
| 297 | |
| 298 | if (lchan->conn) { |
| 299 | if (lchan->conn->subscr) { |
| 300 | subscr_put(lchan->conn->subscr); |
| 301 | lchan->conn->subscr = NULL; |
| 302 | } |
| 303 | |
| 304 | /* We might kill an active channel... */ |
| 305 | if (lchan->conn->use_count != 0) { |
| 306 | dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan); |
| 307 | lchan->conn->use_count = 0; |
| 308 | } |
| 309 | /* stop the timer */ |
| 310 | bsc_del_timer(&lchan->conn->release_timer); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 311 | } |
| 312 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 313 | |
| 314 | /* stop the timer */ |
Sylvain Munaut | fe787f0 | 2009-12-21 01:11:25 +0100 | [diff] [blame] | 315 | bsc_del_timer(&lchan->T3101); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 316 | |
Harald Welte | f947681 | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 317 | /* clear cached measuement reports */ |
| 318 | lchan->meas_rep_idx = 0; |
| 319 | for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) { |
| 320 | lchan->meas_rep[i].flags = 0; |
| 321 | lchan->meas_rep[i].nr = 0; |
| 322 | } |
Harald Welte | 92d8891 | 2009-12-21 13:30:17 +0100 | [diff] [blame] | 323 | for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++) |
| 324 | lchan->neigh_meas[i].arfcn = 0; |
Harald Welte | f947681 | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 325 | |
Holger Hans Peter Freyther | c08f6f0 | 2010-06-22 12:11:59 +0800 | [diff] [blame] | 326 | if (lchan->rqd_ref) { |
| 327 | talloc_free(lchan->rqd_ref); |
| 328 | lchan->rqd_ref = NULL; |
| 329 | lchan->rqd_ta = 0; |
| 330 | } |
| 331 | |
Harald Welte | 8f72544 | 2010-03-28 15:58:03 +0800 | [diff] [blame] | 332 | sig.lchan = lchan; |
| 333 | sig.bts = lchan->ts->trx->bts; |
| 334 | dispatch_signal(SS_CHALLOC, S_CHALLOC_FREED, &sig); |
| 335 | |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 336 | if (lchan->conn) { |
| 337 | subscr_con_free(lchan->conn); |
| 338 | lchan->conn = NULL; |
| 339 | } |
| 340 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 341 | /* FIXME: ts_free() the timeslot, if we're the last logical |
| 342 | * channel using it */ |
| 343 | } |
| 344 | |
Holger Hans Peter Freyther | 5a8b2b4 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 345 | /* |
| 346 | * There was an error with the TRX and we need to forget |
| 347 | * any state so that a lchan can be allocated again after |
| 348 | * the trx is fully usable. |
Holger Hans Peter Freyther | 66a3c77 | 2010-06-22 12:19:06 +0800 | [diff] [blame] | 349 | * |
| 350 | * This should be called after lchan_free to force a channel |
| 351 | * be available for allocation again. This means that this |
| 352 | * method will stop the "delay after error"-timer and set the |
| 353 | * state to LCHAN_S_NONE. |
Holger Hans Peter Freyther | 5a8b2b4 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 354 | */ |
| 355 | void lchan_reset(struct gsm_lchan *lchan) |
| 356 | { |
| 357 | bsc_del_timer(&lchan->T3101); |
Holger Hans Peter Freyther | fb03622 | 2010-06-08 12:12:26 +0800 | [diff] [blame] | 358 | bsc_del_timer(&lchan->T3111); |
| 359 | bsc_del_timer(&lchan->error_timer); |
Holger Hans Peter Freyther | 5a8b2b4 | 2010-04-15 11:21:02 +0200 | [diff] [blame] | 360 | |
| 361 | lchan->type = GSM_LCHAN_NONE; |
| 362 | lchan->state = LCHAN_S_NONE; |
| 363 | } |
| 364 | |
| 365 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 366 | /* Consider releasing the channel now */ |
| 367 | int lchan_auto_release(struct gsm_lchan *lchan) |
| 368 | { |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 369 | if (!lchan->conn) |
| 370 | return 0; |
| 371 | |
| 372 | if (lchan->conn->use_count > 0) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* Assume we have GSM04.08 running and send a release */ |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 377 | if (lchan->conn->subscr) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 378 | gsm48_send_rr_release(lchan); |
| 379 | } |
| 380 | |
| 381 | /* spoofed? message */ |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 382 | if (lchan->conn->use_count < 0) |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 383 | LOGP(DRLL, LOGL_ERROR, "Channel count is negative: %d\n", |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 384 | lchan->conn->use_count); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 385 | |
Harald Welte (local) | 0a77fa1 | 2009-12-28 23:22:43 +0100 | [diff] [blame] | 386 | DEBUGP(DRLL, "%s Recycling Channel\n", gsm_lchan_name(lchan)); |
Holger Hans Peter Freyther | 79a62f4 | 2010-04-10 00:14:55 +0200 | [diff] [blame] | 387 | rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ); |
Holger Hans Peter Freyther | bcea9a7 | 2010-06-08 11:57:45 +0800 | [diff] [blame] | 388 | rsl_release_request(lchan, 0, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | /* Auto release the channel when the use count is zero */ |
| 393 | static void auto_release_channel(void *_lchan) |
| 394 | { |
| 395 | struct gsm_lchan *lchan = _lchan; |
| 396 | |
| 397 | if (!lchan_auto_release(lchan)) |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 398 | bsc_schedule_timer(&lchan->conn->release_timer, LCHAN_RELEASE_TIMEOUT); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Holger Hans Peter Freyther | 8bf60d4 | 2010-06-16 13:05:35 +0800 | [diff] [blame] | 401 | 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] | 402 | struct gsm_bts_trx *trx; |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 403 | int ts_no, lchan_no; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 404 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 405 | llist_for_each_entry(trx, &bts->trx_list, list) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 406 | for (ts_no = 0; ts_no < 8; ++ts_no) { |
| 407 | for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) { |
| 408 | struct gsm_lchan *lchan = |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 409 | &trx->ts[ts_no].lchan[lchan_no]; |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 410 | if (lchan->conn && subscr == lchan->conn->subscr) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 411 | return lchan; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return NULL; |
| 417 | } |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 418 | |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 419 | struct gsm_subscriber_connection *connection_for_subscr(struct gsm_subscriber *subscr) |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 420 | { |
| 421 | struct gsm_bts *bts; |
| 422 | struct gsm_network *net = subscr->net; |
| 423 | struct gsm_lchan *lchan; |
| 424 | |
| 425 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 426 | lchan = lchan_find(bts, subscr); |
| 427 | if (lchan) |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 428 | return lchan->conn; |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Holger Hans Peter Freyther | 3265b1a | 2009-10-27 10:42:53 +0100 | [diff] [blame] | 431 | return NULL; |
Harald Welte | aa60edb | 2009-08-09 18:52:33 +0200 | [diff] [blame] | 432 | } |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 433 | |
| 434 | void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts) |
| 435 | { |
| 436 | struct gsm_bts_trx *trx; |
| 437 | |
| 438 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 439 | int i; |
| 440 | |
| 441 | /* skip administratively deactivated tranxsceivers */ |
Sylvain Munaut | ca3e04f | 2010-01-02 16:32:17 +0100 | [diff] [blame] | 442 | if (!nm_is_running(&trx->nm_state) || |
| 443 | !nm_is_running(&trx->bb_transc.nm_state)) |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 444 | continue; |
| 445 | |
| 446 | for (i = 0; i < ARRAY_SIZE(trx->ts); i++) { |
| 447 | struct gsm_bts_trx_ts *ts = &trx->ts[i]; |
| 448 | struct load_counter *pl = &cl->pchan[ts->pchan]; |
| 449 | int j; |
| 450 | |
| 451 | /* skip administratively deactivated timeslots */ |
Sylvain Munaut | ca3e04f | 2010-01-02 16:32:17 +0100 | [diff] [blame] | 452 | if (!nm_is_running(&ts->nm_state)) |
Harald Welte | fe96f38 | 2009-12-22 13:09:29 +0100 | [diff] [blame] | 453 | continue; |
| 454 | |
| 455 | for (j = 0; j < subslots_per_pchan[ts->pchan]; j++) { |
| 456 | struct gsm_lchan *lchan = &ts->lchan[j]; |
| 457 | |
| 458 | pl->total++; |
| 459 | |
| 460 | switch (lchan->state) { |
| 461 | case LCHAN_S_NONE: |
| 462 | break; |
| 463 | default: |
| 464 | pl->used++; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void network_chan_load(struct pchan_load *pl, struct gsm_network *net) |
| 473 | { |
| 474 | struct gsm_bts *bts; |
| 475 | |
| 476 | memset(pl, 0, sizeof(*pl)); |
| 477 | |
| 478 | llist_for_each_entry(bts, &net->bts_list, list) |
| 479 | bts_chan_load(pl, bts); |
| 480 | } |
Holger Hans Peter Freyther | 1a95fa8 | 2010-06-28 15:47:12 +0800 | [diff] [blame^] | 481 | |
| 482 | struct gsm_subscriber_connection *subscr_con_allocate(struct gsm_lchan *lchan) |
| 483 | { |
| 484 | struct gsm_subscriber_connection *conn; |
| 485 | |
| 486 | conn = talloc_zero(lchan->ts->trx->bts, struct gsm_subscriber_connection); |
| 487 | if (!conn) |
| 488 | return NULL; |
| 489 | |
| 490 | /* Configure the time and start it so it will be closed */ |
| 491 | conn->lchan = lchan; |
| 492 | conn->bts = lchan->ts->trx->bts; |
| 493 | conn->release_timer.cb = auto_release_channel; |
| 494 | conn->release_timer.data = lchan; |
| 495 | bsc_schedule_timer(&conn->release_timer, LCHAN_RELEASE_TIMEOUT); |
| 496 | |
| 497 | lchan->conn = conn; |
| 498 | return conn; |
| 499 | } |
| 500 | |
| 501 | /* TODO: move subscriber put here... */ |
| 502 | void subscr_con_free(struct gsm_subscriber_connection *conn) |
| 503 | { |
| 504 | struct gsm_lchan *lchan; |
| 505 | |
| 506 | |
| 507 | if (!conn) |
| 508 | return; |
| 509 | |
| 510 | lchan = conn->lchan; |
| 511 | talloc_free(conn); |
| 512 | |
| 513 | if (lchan) |
| 514 | lchan->conn = NULL; |
| 515 | } |