blob: 039fccf71dec06a559f48a2bfb8614a709e48e0e [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 Freythere64a7a32009-02-06 21:55:37 +00004 * (C) 2008, 2009 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>
Holger Freyther7c19f742009-06-06 13:54:35 +000034#include <openbsc/signal.h>
Holger Freytherc6ea9db2008-12-30 19:18:21 +000035
Harald Weltef85497c2009-01-01 00:33:20 +000036static void auto_release_channel(void *_lchan);
Harald Welte8470bf22008-12-25 23:28:35 +000037
38struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts,
39 enum gsm_phys_chan_config pchan)
40{
Harald Weltee441d9c2009-06-21 16:17:15 +020041 struct gsm_bts_trx *trx = bts->c0;
Harald Welte8470bf22008-12-25 23:28:35 +000042 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 Welte4bfdfe72009-06-10 23:11:52 +080056/* Allocate a physical channel (TS) */
Harald Welte8470bf22008-12-25 23:28:35 +000057struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
58 enum gsm_phys_chan_config pchan)
59{
60 int i, j;
61 for (i = 0; i < bts->num_trx; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +020062 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, i);
Harald Welte75a983f2008-12-27 21:34:06 +000063 int from, to;
64
65 /* the following constraints are pure policy,
66 * no requirement to put this restriction in place */
Harald Welted46299d2009-07-29 16:46:37 +020067 if (trx == bts->c0) {
68 /* On the first TRX we run one CCCH and one SDCCH8 */
69 switch (pchan) {
70 case GSM_PCHAN_CCCH:
71 case GSM_PCHAN_CCCH_SDCCH4:
72 from = 0; to = 0;
73 break;
Harald Welted46299d2009-07-29 16:46:37 +020074 case GSM_PCHAN_TCH_F:
75 case GSM_PCHAN_TCH_H:
Harald Welte89e9d592009-08-09 22:01:26 +020076 from = 1; to = 7;
Harald Welted46299d2009-07-29 16:46:37 +020077 break;
Harald Welte89e9d592009-08-09 22:01:26 +020078 case GSM_PCHAN_SDCCH8_SACCH8C:
Harald Welted46299d2009-07-29 16:46:37 +020079 default:
80 return NULL;
81 }
82 } else {
83 /* Every secondary TRX is configured for TCH/F
84 * and TCH/H only */
85 switch (pchan) {
Harald Welte89e9d592009-08-09 22:01:26 +020086 case GSM_PCHAN_SDCCH8_SACCH8C:
87 from = 1; to = 1;
Harald Welted46299d2009-07-29 16:46:37 +020088 case GSM_PCHAN_TCH_F:
89 case GSM_PCHAN_TCH_H:
Harald Welte89e9d592009-08-09 22:01:26 +020090 from = 1; to = 7;
Harald Welted46299d2009-07-29 16:46:37 +020091 break;
92 default:
93 return NULL;
94 }
Harald Welte75a983f2008-12-27 21:34:06 +000095 }
96
97 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +000098 struct gsm_bts_trx_ts *ts = &trx->ts[j];
99 if (ts->pchan == GSM_PCHAN_NONE) {
100 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +0000101 /* set channel attribute on OML */
Harald Welte21bd3a52009-08-10 12:21:22 +0200102 abis_nm_set_channel_attr(ts, abis_nm_chcomb4pchan(pchan));
Harald Welte8470bf22008-12-25 23:28:35 +0000103 return ts;
104 }
105 }
106 }
107 return NULL;
108}
109
110/* Free a physical channel (TS) */
111void ts_free(struct gsm_bts_trx_ts *ts)
112{
113 ts->pchan = GSM_PCHAN_NONE;
114}
115
116static const u_int8_t subslots_per_pchan[] = {
117 [GSM_PCHAN_NONE] = 0,
118 [GSM_PCHAN_CCCH] = 0,
119 [GSM_PCHAN_CCCH_SDCCH4] = 4,
120 [GSM_PCHAN_TCH_F] = 1,
121 [GSM_PCHAN_TCH_H] = 2,
Harald Welte4bfdfe72009-06-10 23:11:52 +0800122 [GSM_PCHAN_SDCCH8_SACCH8C] = 8,
Harald Welte8470bf22008-12-25 23:28:35 +0000123};
124
125static struct gsm_lchan *
126_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
127{
128 struct gsm_bts_trx *trx;
129 struct gsm_bts_trx_ts *ts;
130 int i, j, ss;
131 for (i = 0; i < bts->num_trx; i++) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200132 trx = gsm_bts_trx_num(bts, i);
Harald Welte8470bf22008-12-25 23:28:35 +0000133 for (j = 0; j < 8; j++) {
134 ts = &trx->ts[j];
135 if (ts->pchan != pchan)
136 continue;
137 /* check if all sub-slots are allocated yet */
138 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
139 struct gsm_lchan *lc = &ts->lchan[ss];
140 if (lc->type == GSM_LCHAN_NONE)
141 return lc;
142 }
143 }
144 }
145 /* we cannot allocate more of these */
146 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
147 return NULL;
148
149 /* if we've reached here, we need to allocate a new physical
150 * channel for the logical channel type requested */
151 ts = ts_alloc(bts, pchan);
152 if (!ts) {
153 /* no more radio resources */
154 return NULL;
155 }
156 return &ts->lchan[0];
157}
158
159/* Allocate a logical channel */
160struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
161{
162 struct gsm_lchan *lchan = NULL;
163
164 switch (type) {
165 case GSM_LCHAN_SDCCH:
166 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
167 if (lchan == NULL)
168 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
169 break;
170 case GSM_LCHAN_TCH_F:
171 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
172 break;
173 case GSM_LCHAN_TCH_H:
174 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
175 break;
176 default:
177 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
178 }
179
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000180 if (lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000181 lchan->type = type;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000182 lchan->use_count = 0;
183
184 /* Configure the time and start it so it will be closed */
185 lchan->release_timer.cb = auto_release_channel;
186 lchan->release_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000187 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000188 }
Harald Welte8470bf22008-12-25 23:28:35 +0000189
190 return lchan;
191}
192
193/* Free a logical channel */
194void lchan_free(struct gsm_lchan *lchan)
195{
196 lchan->type = GSM_LCHAN_NONE;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000197 if (lchan->subscr) {
198 subscr_put(lchan->subscr);
199 lchan->subscr = 0;
200 }
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000201
Holger Freyther7c19f742009-06-06 13:54:35 +0000202 /* We might kill an active channel... */
Harald Weltec627afc2009-01-09 21:39:17 +0000203 if (lchan->use_count != 0) {
Holger Freyther7c19f742009-06-06 13:54:35 +0000204 dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan);
Harald Weltec627afc2009-01-09 21:39:17 +0000205 lchan->use_count = 0;
206 }
207
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000208 /* stop the timer */
Harald Welteff117a82009-05-23 05:22:08 +0000209 bsc_del_timer(&lchan->release_timer);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000210
Harald Welte8470bf22008-12-25 23:28:35 +0000211 /* FIXME: ts_free() the timeslot, if we're the last logical
212 * channel using it */
213}
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000214
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000215/* Consider releasing the channel now */
216int lchan_auto_release(struct gsm_lchan *lchan)
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000217{
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000218 if (lchan->use_count > 0) {
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000219 return 0;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000220 }
221
Holger Freythere64a7a32009-02-06 21:55:37 +0000222 /* Assume we have GSM04.08 running and send a release */
223 if (lchan->subscr) {
224 gsm48_send_rr_release(lchan);
225 }
226
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000227 /* spoofed? message */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000228 if (lchan->use_count < 0) {
229 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
230 }
231
Harald Welte44227dd2009-02-01 22:25:58 +0000232 DEBUGP(DRLL, "Recycling the channel with: %d (%x)\n", lchan->nr, lchan->nr);
Harald Welted2dc1de2009-08-08 13:15:07 +0200233 rsl_release_request(lchan, 0);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000234 return 1;
235}
236
237/* Auto release the channel when the use count is zero */
238static void auto_release_channel(void *_lchan)
239{
240 struct gsm_lchan *lchan = _lchan;
241
242 if (!lchan_auto_release(lchan))
Harald Welteff117a82009-05-23 05:22:08 +0000243 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000244}
245
Holger Freytherd0e38c32009-01-04 03:48:30 +0000246struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200247 struct gsm_bts_trx *trx;
248 int ts_no, lchan_no;
Holger Freytherd0e38c32009-01-04 03:48:30 +0000249
Harald Weltee441d9c2009-06-21 16:17:15 +0200250 llist_for_each_entry(trx, &bts->trx_list, list) {
Holger Freytherd0e38c32009-01-04 03:48:30 +0000251 for (ts_no = 0; ts_no < 8; ++ts_no) {
252 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
253 struct gsm_lchan *lchan =
Harald Weltee441d9c2009-06-21 16:17:15 +0200254 &trx->ts[ts_no].lchan[lchan_no];
Holger Freytherd0e38c32009-01-04 03:48:30 +0000255 if (subscr == lchan->subscr)
256 return lchan;
257 }
258 }
259 }
260
261 return NULL;
262}
Harald Welte1a6f7982009-08-09 18:52:33 +0200263
264struct gsm_lchan *lchan_for_subscr(struct gsm_subscriber *subscr)
265{
266 struct gsm_bts *bts;
267 struct gsm_network *net = subscr->net;
268 struct gsm_lchan *lchan;
269
270 llist_for_each_entry(bts, &net->bts_list, list) {
271 lchan = lchan_find(bts, subscr);
272 if (lchan)
273 return lchan;
274 }
275
276 return 0;
277}