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 |
| 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> |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 31 | #include <openbsc/abis_nm.h> |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 32 | #include <openbsc/abis_rsl.h> |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Holger Freyther | 7c19f74 | 2009-06-06 13:54:35 +0000 | [diff] [blame] | 34 | #include <openbsc/signal.h> |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 35 | |
Harald Welte | f85497c | 2009-01-01 00:33:20 +0000 | [diff] [blame] | 36 | static void auto_release_channel(void *_lchan); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 37 | |
| 38 | struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts, |
| 39 | enum gsm_phys_chan_config pchan) |
| 40 | { |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 41 | struct gsm_bts_trx *trx = bts->c0; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 42 | struct gsm_bts_trx_ts *ts = &trx->ts[0]; |
| 43 | |
| 44 | if (pchan != GSM_PCHAN_CCCH && |
| 45 | pchan != GSM_PCHAN_CCCH_SDCCH4) |
| 46 | return NULL; |
| 47 | |
| 48 | if (ts->pchan != GSM_PCHAN_NONE) |
| 49 | return NULL; |
| 50 | |
| 51 | ts->pchan = pchan; |
| 52 | |
| 53 | return ts; |
| 54 | } |
| 55 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 56 | static const enum abis_nm_chan_comb chcomb4pchan[] = { |
| 57 | [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH, |
Harald Welte | 560982b | 2009-06-26 13:21:57 +0200 | [diff] [blame] | 58 | [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCHComb, |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 59 | [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull, |
| 60 | [GSM_PCHAN_TCH_H] = NM_CHANC_TCHHalf, |
| 61 | [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH, |
| 62 | /* FIXME: bounds check */ |
| 63 | }; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 64 | |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 65 | /* Allocate a physical channel (TS) */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 66 | struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts, |
| 67 | enum gsm_phys_chan_config pchan) |
| 68 | { |
| 69 | int i, j; |
| 70 | for (i = 0; i < bts->num_trx; i++) { |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 71 | struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, i); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 72 | int from, to; |
| 73 | |
| 74 | /* the following constraints are pure policy, |
| 75 | * no requirement to put this restriction in place */ |
| 76 | switch (pchan) { |
| 77 | case GSM_PCHAN_CCCH: |
| 78 | case GSM_PCHAN_CCCH_SDCCH4: |
| 79 | from = 0; to = 0; |
| 80 | break; |
| 81 | case GSM_PCHAN_SDCCH8_SACCH8C: |
| 82 | from = 1; to = 1; |
| 83 | break; |
| 84 | case GSM_PCHAN_TCH_F: |
| 85 | case GSM_PCHAN_TCH_H: |
| 86 | from = 2; to = 7; |
| 87 | break; |
Harald Welte | f85497c | 2009-01-01 00:33:20 +0000 | [diff] [blame] | 88 | default: |
| 89 | return NULL; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | for (j = from; j <= to; j++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 93 | struct gsm_bts_trx_ts *ts = &trx->ts[j]; |
| 94 | if (ts->pchan == GSM_PCHAN_NONE) { |
| 95 | ts->pchan = pchan; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 96 | /* set channel attribute on OML */ |
| 97 | abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 98 | return ts; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | /* Free a physical channel (TS) */ |
| 106 | void ts_free(struct gsm_bts_trx_ts *ts) |
| 107 | { |
| 108 | ts->pchan = GSM_PCHAN_NONE; |
| 109 | } |
| 110 | |
| 111 | static const u_int8_t subslots_per_pchan[] = { |
| 112 | [GSM_PCHAN_NONE] = 0, |
| 113 | [GSM_PCHAN_CCCH] = 0, |
| 114 | [GSM_PCHAN_CCCH_SDCCH4] = 4, |
| 115 | [GSM_PCHAN_TCH_F] = 1, |
| 116 | [GSM_PCHAN_TCH_H] = 2, |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 117 | [GSM_PCHAN_SDCCH8_SACCH8C] = 8, |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static struct gsm_lchan * |
| 121 | _lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) |
| 122 | { |
| 123 | struct gsm_bts_trx *trx; |
| 124 | struct gsm_bts_trx_ts *ts; |
| 125 | int i, j, ss; |
| 126 | for (i = 0; i < bts->num_trx; i++) { |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 127 | trx = gsm_bts_trx_num(bts, i); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 128 | for (j = 0; j < 8; j++) { |
| 129 | ts = &trx->ts[j]; |
| 130 | if (ts->pchan != pchan) |
| 131 | continue; |
| 132 | /* check if all sub-slots are allocated yet */ |
| 133 | for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) { |
| 134 | struct gsm_lchan *lc = &ts->lchan[ss]; |
| 135 | if (lc->type == GSM_LCHAN_NONE) |
| 136 | return lc; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | /* we cannot allocate more of these */ |
| 141 | if (pchan == GSM_PCHAN_CCCH_SDCCH4) |
| 142 | return NULL; |
| 143 | |
| 144 | /* if we've reached here, we need to allocate a new physical |
| 145 | * channel for the logical channel type requested */ |
| 146 | ts = ts_alloc(bts, pchan); |
| 147 | if (!ts) { |
| 148 | /* no more radio resources */ |
| 149 | return NULL; |
| 150 | } |
| 151 | return &ts->lchan[0]; |
| 152 | } |
| 153 | |
| 154 | /* Allocate a logical channel */ |
| 155 | struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type) |
| 156 | { |
| 157 | struct gsm_lchan *lchan = NULL; |
| 158 | |
| 159 | switch (type) { |
| 160 | case GSM_LCHAN_SDCCH: |
| 161 | lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4); |
| 162 | if (lchan == NULL) |
| 163 | lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C); |
| 164 | break; |
| 165 | case GSM_LCHAN_TCH_F: |
| 166 | lchan = _lc_find(bts, GSM_PCHAN_TCH_F); |
| 167 | break; |
| 168 | case GSM_LCHAN_TCH_H: |
| 169 | lchan =_lc_find(bts, GSM_PCHAN_TCH_H); |
| 170 | break; |
| 171 | default: |
| 172 | fprintf(stderr, "Unknown gsm_chan_t %u\n", type); |
| 173 | } |
| 174 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 175 | if (lchan) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 176 | lchan->type = type; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 177 | lchan->use_count = 0; |
| 178 | |
| 179 | /* Configure the time and start it so it will be closed */ |
| 180 | lchan->release_timer.cb = auto_release_channel; |
| 181 | lchan->release_timer.data = lchan; |
Harald Welte | ff117a8 | 2009-05-23 05:22:08 +0000 | [diff] [blame] | 182 | bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 183 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 184 | |
| 185 | return lchan; |
| 186 | } |
| 187 | |
| 188 | /* Free a logical channel */ |
| 189 | void lchan_free(struct gsm_lchan *lchan) |
| 190 | { |
| 191 | lchan->type = GSM_LCHAN_NONE; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 192 | if (lchan->subscr) { |
| 193 | subscr_put(lchan->subscr); |
| 194 | lchan->subscr = 0; |
| 195 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 196 | |
Holger Freyther | 7c19f74 | 2009-06-06 13:54:35 +0000 | [diff] [blame] | 197 | /* We might kill an active channel... */ |
Harald Welte | c627afc | 2009-01-09 21:39:17 +0000 | [diff] [blame] | 198 | if (lchan->use_count != 0) { |
Holger Freyther | 7c19f74 | 2009-06-06 13:54:35 +0000 | [diff] [blame] | 199 | dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan); |
Harald Welte | c627afc | 2009-01-09 21:39:17 +0000 | [diff] [blame] | 200 | lchan->use_count = 0; |
| 201 | } |
| 202 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 203 | /* stop the timer */ |
Harald Welte | ff117a8 | 2009-05-23 05:22:08 +0000 | [diff] [blame] | 204 | bsc_del_timer(&lchan->release_timer); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 205 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 206 | /* FIXME: ts_free() the timeslot, if we're the last logical |
| 207 | * channel using it */ |
| 208 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 209 | |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 210 | /* Consider releasing the channel now */ |
| 211 | int lchan_auto_release(struct gsm_lchan *lchan) |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 212 | { |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 213 | if (lchan->use_count > 0) { |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 214 | return 0; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Holger Freyther | e64a7a3 | 2009-02-06 21:55:37 +0000 | [diff] [blame] | 217 | /* Assume we have GSM04.08 running and send a release */ |
| 218 | if (lchan->subscr) { |
| 219 | gsm48_send_rr_release(lchan); |
| 220 | } |
| 221 | |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 222 | /* spoofed? message */ |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 223 | if (lchan->use_count < 0) { |
| 224 | DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count); |
| 225 | } |
| 226 | |
Harald Welte | 44227dd | 2009-02-01 22:25:58 +0000 | [diff] [blame] | 227 | DEBUGP(DRLL, "Recycling the channel with: %d (%x)\n", lchan->nr, lchan->nr); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 228 | rsl_chan_release(lchan); |
Holger Freyther | 67b4b9a | 2009-01-01 03:46:11 +0000 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | |
| 232 | /* Auto release the channel when the use count is zero */ |
| 233 | static void auto_release_channel(void *_lchan) |
| 234 | { |
| 235 | struct gsm_lchan *lchan = _lchan; |
| 236 | |
| 237 | if (!lchan_auto_release(lchan)) |
Harald Welte | ff117a8 | 2009-05-23 05:22:08 +0000 | [diff] [blame] | 238 | bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT); |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Holger Freyther | d0e38c3 | 2009-01-04 03:48:30 +0000 | [diff] [blame] | 241 | struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) { |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 242 | struct gsm_bts_trx *trx; |
| 243 | int ts_no, lchan_no; |
Holger Freyther | d0e38c3 | 2009-01-04 03:48:30 +0000 | [diff] [blame] | 244 | |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 245 | llist_for_each_entry(trx, &bts->trx_list, list) { |
Holger Freyther | d0e38c3 | 2009-01-04 03:48:30 +0000 | [diff] [blame] | 246 | for (ts_no = 0; ts_no < 8; ++ts_no) { |
| 247 | for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) { |
| 248 | struct gsm_lchan *lchan = |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 249 | &trx->ts[ts_no].lchan[lchan_no]; |
Holger Freyther | d0e38c3 | 2009-01-04 03:48:30 +0000 | [diff] [blame] | 250 | if (subscr == lchan->subscr) |
| 251 | return lchan; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return NULL; |
| 257 | } |