blob: 2f0d7b93f589fb5e4447502ce01a9e514a4b3fc3 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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
36static void auto_release_channel(void *_lchan);
37
38struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts,
39 enum gsm_phys_chan_config pchan)
40{
Harald Weltee712a5f2009-06-21 16:17:15 +020041 struct gsm_bts_trx *trx = bts->c0;
Harald Welte59b04682009-06-10 05:40:52 +080042 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 Welte03740842009-06-10 23:11:52 +080056/* Allocate a physical channel (TS) */
Harald Welte59b04682009-06-10 05:40:52 +080057struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
58 enum gsm_phys_chan_config pchan)
59{
Harald Welte351ff122009-08-10 13:25:55 +020060 int j;
61 struct gsm_bts_trx *trx;
62
63 llist_for_each_entry(trx, &bts->trx_list, list) {
Harald Welte59b04682009-06-10 05:40:52 +080064 int from, to;
65
66 /* the following constraints are pure policy,
67 * no requirement to put this restriction in place */
Harald Welte3a9c2c02009-07-29 16:46:37 +020068 if (trx == bts->c0) {
69 /* On the first TRX we run one CCCH and one SDCCH8 */
70 switch (pchan) {
71 case GSM_PCHAN_CCCH:
72 case GSM_PCHAN_CCCH_SDCCH4:
73 from = 0; to = 0;
74 break;
Harald Welte3a9c2c02009-07-29 16:46:37 +020075 case GSM_PCHAN_TCH_F:
76 case GSM_PCHAN_TCH_H:
Harald Welte7f863f52009-08-09 22:01:26 +020077 from = 1; to = 7;
Harald Welte3a9c2c02009-07-29 16:46:37 +020078 break;
Harald Welte7f863f52009-08-09 22:01:26 +020079 case GSM_PCHAN_SDCCH8_SACCH8C:
Harald Welte3a9c2c02009-07-29 16:46:37 +020080 default:
81 return NULL;
82 }
83 } else {
84 /* Every secondary TRX is configured for TCH/F
85 * and TCH/H only */
86 switch (pchan) {
Harald Welte7f863f52009-08-09 22:01:26 +020087 case GSM_PCHAN_SDCCH8_SACCH8C:
88 from = 1; to = 1;
Harald Welte3a9c2c02009-07-29 16:46:37 +020089 case GSM_PCHAN_TCH_F:
90 case GSM_PCHAN_TCH_H:
Harald Welte7f863f52009-08-09 22:01:26 +020091 from = 1; to = 7;
Harald Welte3a9c2c02009-07-29 16:46:37 +020092 break;
93 default:
94 return NULL;
95 }
Harald Welte59b04682009-06-10 05:40:52 +080096 }
97
98 for (j = from; j <= to; j++) {
99 struct gsm_bts_trx_ts *ts = &trx->ts[j];
100 if (ts->pchan == GSM_PCHAN_NONE) {
101 ts->pchan = pchan;
102 /* set channel attribute on OML */
Harald Welte35cd5e92009-08-10 12:21:22 +0200103 abis_nm_set_channel_attr(ts, abis_nm_chcomb4pchan(pchan));
Harald Welte59b04682009-06-10 05:40:52 +0800104 return ts;
105 }
106 }
107 }
108 return NULL;
109}
110
111/* Free a physical channel (TS) */
112void ts_free(struct gsm_bts_trx_ts *ts)
113{
114 ts->pchan = GSM_PCHAN_NONE;
115}
116
117static const u_int8_t subslots_per_pchan[] = {
118 [GSM_PCHAN_NONE] = 0,
119 [GSM_PCHAN_CCCH] = 0,
120 [GSM_PCHAN_CCCH_SDCCH4] = 4,
121 [GSM_PCHAN_TCH_F] = 1,
122 [GSM_PCHAN_TCH_H] = 2,
Harald Welte03740842009-06-10 23:11:52 +0800123 [GSM_PCHAN_SDCCH8_SACCH8C] = 8,
Harald Welte59b04682009-06-10 05:40:52 +0800124};
125
126static struct gsm_lchan *
Harald Welteceb3c042009-08-10 13:46:55 +0200127_lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan)
Harald Welte59b04682009-06-10 05:40:52 +0800128{
Harald Welte59b04682009-06-10 05:40:52 +0800129 struct gsm_bts_trx_ts *ts;
Harald Welte351ff122009-08-10 13:25:55 +0200130 int j, ss;
131
Harald Welteceb3c042009-08-10 13:46:55 +0200132 for (j = 0; j < 8; j++) {
133 ts = &trx->ts[j];
134 if (ts->pchan != pchan)
135 continue;
136 /* check if all sub-slots are allocated yet */
137 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
138 struct gsm_lchan *lc = &ts->lchan[ss];
139 if (lc->type == GSM_LCHAN_NONE)
140 return lc;
Harald Welte59b04682009-06-10 05:40:52 +0800141 }
142 }
Harald Welteceb3c042009-08-10 13:46:55 +0200143 return NULL;
144}
145
146static struct gsm_lchan *
147_lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
148{
149 struct gsm_bts_trx *trx;
150 struct gsm_bts_trx_ts *ts;
151 struct gsm_lchan *lc;
152
153 if (bts->chan_alloc_reverse) {
154 llist_for_each_entry_reverse(trx, &bts->trx_list, list) {
155 lc = _lc_find_trx(trx, pchan);
156 if (lc)
157 return lc;
158 }
159 } else {
160 llist_for_each_entry(trx, &bts->trx_list, list) {
161 lc = _lc_find_trx(trx, pchan);
162 if (lc)
163 return lc;
164 }
165 }
166
Harald Welte59b04682009-06-10 05:40:52 +0800167 /* we cannot allocate more of these */
168 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
169 return NULL;
170
171 /* if we've reached here, we need to allocate a new physical
172 * channel for the logical channel type requested */
173 ts = ts_alloc(bts, pchan);
174 if (!ts) {
175 /* no more radio resources */
176 return NULL;
177 }
178 return &ts->lchan[0];
179}
180
181/* Allocate a logical channel */
182struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
183{
184 struct gsm_lchan *lchan = NULL;
Harald Welteaf86c0f2009-08-10 14:44:24 +0200185 enum gsm_phys_chan_config first, second;
Harald Welte59b04682009-06-10 05:40:52 +0800186
187 switch (type) {
188 case GSM_LCHAN_SDCCH:
Harald Welteaf86c0f2009-08-10 14:44:24 +0200189 if (bts->chan_alloc_reverse) {
190 first = GSM_PCHAN_SDCCH8_SACCH8C;
191 second = GSM_PCHAN_CCCH_SDCCH4;
192 } else {
193 first = GSM_PCHAN_CCCH_SDCCH4;
194 second = GSM_PCHAN_SDCCH8_SACCH8C;
195 }
196
197 lchan = _lc_find_bts(bts, first);
Harald Welte59b04682009-06-10 05:40:52 +0800198 if (lchan == NULL)
Harald Welteaf86c0f2009-08-10 14:44:24 +0200199 lchan = _lc_find_bts(bts, second);
Harald Welte59b04682009-06-10 05:40:52 +0800200 break;
201 case GSM_LCHAN_TCH_F:
Harald Welteceb3c042009-08-10 13:46:55 +0200202 lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
Harald Welte59b04682009-06-10 05:40:52 +0800203 break;
204 case GSM_LCHAN_TCH_H:
Harald Welteceb3c042009-08-10 13:46:55 +0200205 lchan =_lc_find_bts(bts, GSM_PCHAN_TCH_H);
Harald Welte12503372009-12-12 20:58:20 +0100206 /* If we don't have TCH/H available, fall-back to TCH/F */
Harald Welte5ea9e8e2009-12-12 21:16:38 +0100207 if (!lchan) {
Harald Welte12503372009-12-12 20:58:20 +0100208 lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
Harald Welte5ea9e8e2009-12-12 21:16:38 +0100209 type = GSM_LCHAN_TCH_F;
210 }
Harald Welte59b04682009-06-10 05:40:52 +0800211 break;
212 default:
213 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
214 }
215
216 if (lchan) {
217 lchan->type = type;
218 lchan->use_count = 0;
219
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +0100220 /* clear sapis */
Holger Hans Peter Freyther29afdc42009-11-18 22:59:51 +0100221 memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis));
Holger Hans Peter Freytherd8318052009-10-28 14:23:39 +0100222
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +0100223 /* clear multi rate config */
224 memset(&lchan->mr_conf, 0, sizeof(lchan->mr_conf));
225
Harald Welte59b04682009-06-10 05:40:52 +0800226 /* Configure the time and start it so it will be closed */
227 lchan->release_timer.cb = auto_release_channel;
228 lchan->release_timer.data = lchan;
229 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
230 }
231
232 return lchan;
233}
234
235/* Free a logical channel */
236void lchan_free(struct gsm_lchan *lchan)
237{
238 lchan->type = GSM_LCHAN_NONE;
239 if (lchan->subscr) {
240 subscr_put(lchan->subscr);
Holger Hans Peter Freyther3265b1a2009-10-27 10:42:53 +0100241 lchan->subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800242 }
243
244 /* We might kill an active channel... */
245 if (lchan->use_count != 0) {
246 dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan);
247 lchan->use_count = 0;
248 }
249
250 /* stop the timer */
251 bsc_del_timer(&lchan->release_timer);
252
253 /* FIXME: ts_free() the timeslot, if we're the last logical
254 * channel using it */
255}
256
257/* Consider releasing the channel now */
258int lchan_auto_release(struct gsm_lchan *lchan)
259{
260 if (lchan->use_count > 0) {
261 return 0;
262 }
263
264 /* Assume we have GSM04.08 running and send a release */
265 if (lchan->subscr) {
266 gsm48_send_rr_release(lchan);
267 }
268
269 /* spoofed? message */
270 if (lchan->use_count < 0) {
271 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
272 }
273
274 DEBUGP(DRLL, "Recycling the channel with: %d (%x)\n", lchan->nr, lchan->nr);
Harald Welte0f2e3c12009-08-08 13:15:07 +0200275 rsl_release_request(lchan, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800276 return 1;
277}
278
279/* Auto release the channel when the use count is zero */
280static void auto_release_channel(void *_lchan)
281{
282 struct gsm_lchan *lchan = _lchan;
283
284 if (!lchan_auto_release(lchan))
285 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
286}
287
288struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) {
Harald Weltee712a5f2009-06-21 16:17:15 +0200289 struct gsm_bts_trx *trx;
290 int ts_no, lchan_no;
Harald Welte59b04682009-06-10 05:40:52 +0800291
Harald Weltee712a5f2009-06-21 16:17:15 +0200292 llist_for_each_entry(trx, &bts->trx_list, list) {
Harald Welte59b04682009-06-10 05:40:52 +0800293 for (ts_no = 0; ts_no < 8; ++ts_no) {
294 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
295 struct gsm_lchan *lchan =
Harald Weltee712a5f2009-06-21 16:17:15 +0200296 &trx->ts[ts_no].lchan[lchan_no];
Harald Welte59b04682009-06-10 05:40:52 +0800297 if (subscr == lchan->subscr)
298 return lchan;
299 }
300 }
301 }
302
303 return NULL;
304}
Harald Welteaa60edb2009-08-09 18:52:33 +0200305
306struct gsm_lchan *lchan_for_subscr(struct gsm_subscriber *subscr)
307{
308 struct gsm_bts *bts;
309 struct gsm_network *net = subscr->net;
310 struct gsm_lchan *lchan;
311
312 llist_for_each_entry(bts, &net->bts_list, list) {
313 lchan = lchan_find(bts, subscr);
314 if (lchan)
315 return lchan;
316 }
317
Holger Hans Peter Freyther3265b1a2009-10-27 10:42:53 +0100318 return NULL;
Harald Welteaa60edb2009-08-09 18:52:33 +0200319}