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 | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 4 | * (C) 2008 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 | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
| 33 | |
| 34 | static void auto_release_channel(struct gsm_lchan* lchan); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 35 | |
| 36 | struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts, |
| 37 | enum gsm_phys_chan_config pchan) |
| 38 | { |
| 39 | struct gsm_bts_trx *trx = &bts->trx[0]; |
| 40 | struct gsm_bts_trx_ts *ts = &trx->ts[0]; |
| 41 | |
| 42 | if (pchan != GSM_PCHAN_CCCH && |
| 43 | pchan != GSM_PCHAN_CCCH_SDCCH4) |
| 44 | return NULL; |
| 45 | |
| 46 | if (ts->pchan != GSM_PCHAN_NONE) |
| 47 | return NULL; |
| 48 | |
| 49 | ts->pchan = pchan; |
| 50 | |
| 51 | return ts; |
| 52 | } |
| 53 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 54 | static const enum abis_nm_chan_comb chcomb4pchan[] = { |
| 55 | [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH, |
| 56 | [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCCHComb, |
| 57 | [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull, |
| 58 | [GSM_PCHAN_TCH_H] = NM_CHANC_TCHHalf, |
| 59 | [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH, |
| 60 | /* FIXME: bounds check */ |
| 61 | }; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 62 | |
| 63 | /* Allocate a logical channel (TS) */ |
| 64 | struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts, |
| 65 | enum gsm_phys_chan_config pchan) |
| 66 | { |
| 67 | int i, j; |
| 68 | for (i = 0; i < bts->num_trx; i++) { |
| 69 | struct gsm_bts_trx *trx = &bts->trx[i]; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 70 | int from, to; |
| 71 | |
| 72 | /* the following constraints are pure policy, |
| 73 | * no requirement to put this restriction in place */ |
| 74 | switch (pchan) { |
| 75 | case GSM_PCHAN_CCCH: |
| 76 | case GSM_PCHAN_CCCH_SDCCH4: |
| 77 | from = 0; to = 0; |
| 78 | break; |
| 79 | case GSM_PCHAN_SDCCH8_SACCH8C: |
| 80 | from = 1; to = 1; |
| 81 | break; |
| 82 | case GSM_PCHAN_TCH_F: |
| 83 | case GSM_PCHAN_TCH_H: |
| 84 | from = 2; to = 7; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | for (j = from; j <= to; j++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 89 | struct gsm_bts_trx_ts *ts = &trx->ts[j]; |
| 90 | if (ts->pchan == GSM_PCHAN_NONE) { |
| 91 | ts->pchan = pchan; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 92 | /* set channel attribute on OML */ |
| 93 | abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 94 | return ts; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | /* Free a physical channel (TS) */ |
| 102 | void ts_free(struct gsm_bts_trx_ts *ts) |
| 103 | { |
| 104 | ts->pchan = GSM_PCHAN_NONE; |
| 105 | } |
| 106 | |
| 107 | static const u_int8_t subslots_per_pchan[] = { |
| 108 | [GSM_PCHAN_NONE] = 0, |
| 109 | [GSM_PCHAN_CCCH] = 0, |
| 110 | [GSM_PCHAN_CCCH_SDCCH4] = 4, |
| 111 | [GSM_PCHAN_TCH_F] = 1, |
| 112 | [GSM_PCHAN_TCH_H] = 2, |
| 113 | [GSM_PCHAN_SDCCH8_SACCH8C] = 8. |
| 114 | }; |
| 115 | |
| 116 | static struct gsm_lchan * |
| 117 | _lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan) |
| 118 | { |
| 119 | struct gsm_bts_trx *trx; |
| 120 | struct gsm_bts_trx_ts *ts; |
| 121 | int i, j, ss; |
| 122 | for (i = 0; i < bts->num_trx; i++) { |
| 123 | trx = &bts->trx[i]; |
| 124 | for (j = 0; j < 8; j++) { |
| 125 | ts = &trx->ts[j]; |
| 126 | if (ts->pchan != pchan) |
| 127 | continue; |
| 128 | /* check if all sub-slots are allocated yet */ |
| 129 | for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) { |
| 130 | struct gsm_lchan *lc = &ts->lchan[ss]; |
| 131 | if (lc->type == GSM_LCHAN_NONE) |
| 132 | return lc; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | /* we cannot allocate more of these */ |
| 137 | if (pchan == GSM_PCHAN_CCCH_SDCCH4) |
| 138 | return NULL; |
| 139 | |
| 140 | /* if we've reached here, we need to allocate a new physical |
| 141 | * channel for the logical channel type requested */ |
| 142 | ts = ts_alloc(bts, pchan); |
| 143 | if (!ts) { |
| 144 | /* no more radio resources */ |
| 145 | return NULL; |
| 146 | } |
| 147 | return &ts->lchan[0]; |
| 148 | } |
| 149 | |
| 150 | /* Allocate a logical channel */ |
| 151 | struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type) |
| 152 | { |
| 153 | struct gsm_lchan *lchan = NULL; |
| 154 | |
| 155 | switch (type) { |
| 156 | case GSM_LCHAN_SDCCH: |
| 157 | lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4); |
| 158 | if (lchan == NULL) |
| 159 | lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C); |
| 160 | break; |
| 161 | case GSM_LCHAN_TCH_F: |
| 162 | lchan = _lc_find(bts, GSM_PCHAN_TCH_F); |
| 163 | break; |
| 164 | case GSM_LCHAN_TCH_H: |
| 165 | lchan =_lc_find(bts, GSM_PCHAN_TCH_H); |
| 166 | break; |
| 167 | default: |
| 168 | fprintf(stderr, "Unknown gsm_chan_t %u\n", type); |
| 169 | } |
| 170 | |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 171 | if (lchan) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 172 | lchan->type = type; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 173 | lchan->use_count = 0; |
| 174 | |
| 175 | /* Configure the time and start it so it will be closed */ |
| 176 | lchan->release_timer.cb = auto_release_channel; |
| 177 | lchan->release_timer.data = lchan; |
| 178 | schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT); |
| 179 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 180 | |
| 181 | return lchan; |
| 182 | } |
| 183 | |
| 184 | /* Free a logical channel */ |
| 185 | void lchan_free(struct gsm_lchan *lchan) |
| 186 | { |
| 187 | lchan->type = GSM_LCHAN_NONE; |
Holger Freyther | 41ed300 | 2008-12-31 18:52:54 +0000 | [diff] [blame] | 188 | lchan->subscr = 0; |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 189 | |
| 190 | /* stop the timer */ |
| 191 | del_timer(&lchan->release_timer); |
| 192 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 193 | /* FIXME: ts_free() the timeslot, if we're the last logical |
| 194 | * channel using it */ |
| 195 | } |
Holger Freyther | c6ea9db | 2008-12-30 19:18:21 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * Auto release the channel when the use count is zero |
| 199 | */ |
| 200 | static void auto_release_channel(struct gsm_lchan* lchan) |
| 201 | { |
| 202 | /* |
| 203 | * Busy... |
| 204 | */ |
| 205 | if (lchan->use_count > 0) { |
| 206 | schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * spoofed? message |
| 212 | */ |
| 213 | if (lchan->use_count < 0) { |
| 214 | DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count); |
| 215 | } |
| 216 | |
| 217 | DEBUGP(DRLL, "Recylcing the channel with: %d (%x)\n", lchan->nr, lchan->nr); |
| 218 | rsl_chan_release(lchan); |
| 219 | } |
| 220 | |