blob: 039fccf71dec06a559f48a2bfb8614a709e48e0e [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{
60 int i, j;
61 for (i = 0; i < bts->num_trx; i++) {
Harald Weltee712a5f2009-06-21 16:17:15 +020062 struct gsm_bts_trx *trx = gsm_bts_trx_num(bts, i);
Harald Welte59b04682009-06-10 05:40:52 +080063 int from, to;
64
65 /* the following constraints are pure policy,
66 * no requirement to put this restriction in place */
Harald Welte3a9c2c02009-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 Welte3a9c2c02009-07-29 16:46:37 +020074 case GSM_PCHAN_TCH_F:
75 case GSM_PCHAN_TCH_H:
Harald Welte7f863f52009-08-09 22:01:26 +020076 from = 1; to = 7;
Harald Welte3a9c2c02009-07-29 16:46:37 +020077 break;
Harald Welte7f863f52009-08-09 22:01:26 +020078 case GSM_PCHAN_SDCCH8_SACCH8C:
Harald Welte3a9c2c02009-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 Welte7f863f52009-08-09 22:01:26 +020086 case GSM_PCHAN_SDCCH8_SACCH8C:
87 from = 1; to = 1;
Harald Welte3a9c2c02009-07-29 16:46:37 +020088 case GSM_PCHAN_TCH_F:
89 case GSM_PCHAN_TCH_H:
Harald Welte7f863f52009-08-09 22:01:26 +020090 from = 1; to = 7;
Harald Welte3a9c2c02009-07-29 16:46:37 +020091 break;
92 default:
93 return NULL;
94 }
Harald Welte59b04682009-06-10 05:40:52 +080095 }
96
97 for (j = from; j <= to; j++) {
98 struct gsm_bts_trx_ts *ts = &trx->ts[j];
99 if (ts->pchan == GSM_PCHAN_NONE) {
100 ts->pchan = pchan;
101 /* set channel attribute on OML */
Harald Welte35cd5e92009-08-10 12:21:22 +0200102 abis_nm_set_channel_attr(ts, abis_nm_chcomb4pchan(pchan));
Harald Welte59b04682009-06-10 05:40:52 +0800103 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 Welte03740842009-06-10 23:11:52 +0800122 [GSM_PCHAN_SDCCH8_SACCH8C] = 8,
Harald Welte59b04682009-06-10 05:40:52 +0800123};
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 Weltee712a5f2009-06-21 16:17:15 +0200132 trx = gsm_bts_trx_num(bts, i);
Harald Welte59b04682009-06-10 05:40:52 +0800133 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
180 if (lchan) {
181 lchan->type = type;
182 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;
187 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
188 }
189
190 return lchan;
191}
192
193/* Free a logical channel */
194void lchan_free(struct gsm_lchan *lchan)
195{
196 lchan->type = GSM_LCHAN_NONE;
197 if (lchan->subscr) {
198 subscr_put(lchan->subscr);
199 lchan->subscr = 0;
200 }
201
202 /* We might kill an active channel... */
203 if (lchan->use_count != 0) {
204 dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan);
205 lchan->use_count = 0;
206 }
207
208 /* stop the timer */
209 bsc_del_timer(&lchan->release_timer);
210
211 /* FIXME: ts_free() the timeslot, if we're the last logical
212 * channel using it */
213}
214
215/* Consider releasing the channel now */
216int lchan_auto_release(struct gsm_lchan *lchan)
217{
218 if (lchan->use_count > 0) {
219 return 0;
220 }
221
222 /* Assume we have GSM04.08 running and send a release */
223 if (lchan->subscr) {
224 gsm48_send_rr_release(lchan);
225 }
226
227 /* spoofed? message */
228 if (lchan->use_count < 0) {
229 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
230 }
231
232 DEBUGP(DRLL, "Recycling the channel with: %d (%x)\n", lchan->nr, lchan->nr);
Harald Welte0f2e3c12009-08-08 13:15:07 +0200233 rsl_release_request(lchan, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800234 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))
243 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
244}
245
246struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) {
Harald Weltee712a5f2009-06-21 16:17:15 +0200247 struct gsm_bts_trx *trx;
248 int ts_no, lchan_no;
Harald Welte59b04682009-06-10 05:40:52 +0800249
Harald Weltee712a5f2009-06-21 16:17:15 +0200250 llist_for_each_entry(trx, &bts->trx_list, list) {
Harald Welte59b04682009-06-10 05:40:52 +0800251 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 Weltee712a5f2009-06-21 16:17:15 +0200254 &trx->ts[ts_no].lchan[lchan_no];
Harald Welte59b04682009-06-10 05:40:52 +0800255 if (subscr == lchan->subscr)
256 return lchan;
257 }
258 }
259 }
260
261 return NULL;
262}
Harald Welteaa60edb2009-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}