blob: 98588b18f098efd17a555d95b7ff9c7bdadba386 [file] [log] [blame]
Harald Welte8470bf22008-12-25 23:28:35 +00001/* GSM Channel allocation routines
2 *
3 * (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freytherc6ea9db2008-12-30 19:18:21 +00004 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
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 Welte4b634542008-12-27 01:55:51 +000031#include <openbsc/abis_nm.h>
Holger Freyther67b4b9a2009-01-01 03:46:11 +000032#include <openbsc/abis_rsl.h>
Holger Freytherc6ea9db2008-12-30 19:18:21 +000033#include <openbsc/debug.h>
34
Harald Weltef85497c2009-01-01 00:33:20 +000035static void auto_release_channel(void *_lchan);
Harald Welte8470bf22008-12-25 23:28:35 +000036
37struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts,
38 enum gsm_phys_chan_config pchan)
39{
40 struct gsm_bts_trx *trx = &bts->trx[0];
41 struct gsm_bts_trx_ts *ts = &trx->ts[0];
42
43 if (pchan != GSM_PCHAN_CCCH &&
44 pchan != GSM_PCHAN_CCCH_SDCCH4)
45 return NULL;
46
47 if (ts->pchan != GSM_PCHAN_NONE)
48 return NULL;
49
50 ts->pchan = pchan;
51
52 return ts;
53}
54
Harald Welte4b634542008-12-27 01:55:51 +000055static const enum abis_nm_chan_comb chcomb4pchan[] = {
56 [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH,
57 [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCCHComb,
58 [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull,
59 [GSM_PCHAN_TCH_H] = NM_CHANC_TCHHalf,
60 [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH,
61 /* FIXME: bounds check */
62};
Harald Welte8470bf22008-12-25 23:28:35 +000063
64/* Allocate a logical channel (TS) */
65struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
66 enum gsm_phys_chan_config pchan)
67{
68 int i, j;
69 for (i = 0; i < bts->num_trx; i++) {
70 struct gsm_bts_trx *trx = &bts->trx[i];
Harald Welte75a983f2008-12-27 21:34:06 +000071 int from, to;
72
73 /* the following constraints are pure policy,
74 * no requirement to put this restriction in place */
75 switch (pchan) {
76 case GSM_PCHAN_CCCH:
77 case GSM_PCHAN_CCCH_SDCCH4:
78 from = 0; to = 0;
79 break;
80 case GSM_PCHAN_SDCCH8_SACCH8C:
81 from = 1; to = 1;
82 break;
83 case GSM_PCHAN_TCH_F:
84 case GSM_PCHAN_TCH_H:
85 from = 2; to = 7;
86 break;
Harald Weltef85497c2009-01-01 00:33:20 +000087 default:
88 return NULL;
Harald Welte75a983f2008-12-27 21:34:06 +000089 }
90
91 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +000092 struct gsm_bts_trx_ts *ts = &trx->ts[j];
93 if (ts->pchan == GSM_PCHAN_NONE) {
94 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +000095 /* set channel attribute on OML */
96 abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]);
Harald Welte8470bf22008-12-25 23:28:35 +000097 return ts;
98 }
99 }
100 }
101 return NULL;
102}
103
104/* Free a physical channel (TS) */
105void ts_free(struct gsm_bts_trx_ts *ts)
106{
107 ts->pchan = GSM_PCHAN_NONE;
108}
109
110static const u_int8_t subslots_per_pchan[] = {
111 [GSM_PCHAN_NONE] = 0,
112 [GSM_PCHAN_CCCH] = 0,
113 [GSM_PCHAN_CCCH_SDCCH4] = 4,
114 [GSM_PCHAN_TCH_F] = 1,
115 [GSM_PCHAN_TCH_H] = 2,
116 [GSM_PCHAN_SDCCH8_SACCH8C] = 8.
117};
118
119static struct gsm_lchan *
120_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
121{
122 struct gsm_bts_trx *trx;
123 struct gsm_bts_trx_ts *ts;
124 int i, j, ss;
125 for (i = 0; i < bts->num_trx; i++) {
126 trx = &bts->trx[i];
127 for (j = 0; j < 8; j++) {
128 ts = &trx->ts[j];
129 if (ts->pchan != pchan)
130 continue;
131 /* check if all sub-slots are allocated yet */
132 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
133 struct gsm_lchan *lc = &ts->lchan[ss];
134 if (lc->type == GSM_LCHAN_NONE)
135 return lc;
136 }
137 }
138 }
139 /* we cannot allocate more of these */
140 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
141 return NULL;
142
143 /* if we've reached here, we need to allocate a new physical
144 * channel for the logical channel type requested */
145 ts = ts_alloc(bts, pchan);
146 if (!ts) {
147 /* no more radio resources */
148 return NULL;
149 }
150 return &ts->lchan[0];
151}
152
153/* Allocate a logical channel */
154struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
155{
156 struct gsm_lchan *lchan = NULL;
157
158 switch (type) {
159 case GSM_LCHAN_SDCCH:
160 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
161 if (lchan == NULL)
162 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
163 break;
164 case GSM_LCHAN_TCH_F:
165 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
166 break;
167 case GSM_LCHAN_TCH_H:
168 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
169 break;
170 default:
171 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
172 }
173
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000174 if (lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000175 lchan->type = type;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000176 lchan->use_count = 0;
177
178 /* Configure the time and start it so it will be closed */
179 lchan->release_timer.cb = auto_release_channel;
180 lchan->release_timer.data = lchan;
181 schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
182 }
Harald Welte8470bf22008-12-25 23:28:35 +0000183
184 return lchan;
185}
186
187/* Free a logical channel */
188void lchan_free(struct gsm_lchan *lchan)
189{
190 lchan->type = GSM_LCHAN_NONE;
Holger Freyther41ed3002008-12-31 18:52:54 +0000191 lchan->subscr = 0;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000192
193 /* stop the timer */
194 del_timer(&lchan->release_timer);
195
Harald Welte8470bf22008-12-25 23:28:35 +0000196 /* FIXME: ts_free() the timeslot, if we're the last logical
197 * channel using it */
198}
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000199
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000200/* Consider releasing the channel now */
201int lchan_auto_release(struct gsm_lchan *lchan)
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000202{
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000203 if (lchan->use_count > 0) {
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000204 return 0;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000205 }
206
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000207 /* spoofed? message */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000208 if (lchan->use_count < 0) {
209 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
210 }
211
212 DEBUGP(DRLL, "Recylcing the channel with: %d (%x)\n", lchan->nr, lchan->nr);
213 rsl_chan_release(lchan);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000214 return 1;
215}
216
217/* Auto release the channel when the use count is zero */
218static void auto_release_channel(void *_lchan)
219{
220 struct gsm_lchan *lchan = _lchan;
221
222 if (!lchan_auto_release(lchan))
223 schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000224}
225