blob: 96632bcb9be4a0d0d6a57c6dfc0cf89381ff0027 [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{
41 struct gsm_bts_trx *trx = &bts->trx[0];
42 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 Welte4b634542008-12-27 01:55:51 +000056static const enum abis_nm_chan_comb chcomb4pchan[] = {
57 [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH,
58 [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCCHComb,
59 [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull,
60 [GSM_PCHAN_TCH_H] = NM_CHANC_TCHHalf,
61 [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH,
62 /* FIXME: bounds check */
63};
Harald Welte8470bf22008-12-25 23:28:35 +000064
Harald Welte4bfdfe72009-06-10 23:11:52 +080065/* Allocate a physical channel (TS) */
Harald Welte8470bf22008-12-25 23:28:35 +000066struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
67 enum gsm_phys_chan_config pchan)
68{
69 int i, j;
70 for (i = 0; i < bts->num_trx; i++) {
71 struct gsm_bts_trx *trx = &bts->trx[i];
Harald Welte75a983f2008-12-27 21:34:06 +000072 int from, to;
73
74 /* the following constraints are pure policy,
75 * no requirement to put this restriction in place */
76 switch (pchan) {
77 case GSM_PCHAN_CCCH:
78 case GSM_PCHAN_CCCH_SDCCH4:
79 from = 0; to = 0;
80 break;
81 case GSM_PCHAN_SDCCH8_SACCH8C:
82 from = 1; to = 1;
83 break;
84 case GSM_PCHAN_TCH_F:
85 case GSM_PCHAN_TCH_H:
86 from = 2; to = 7;
87 break;
Harald Weltef85497c2009-01-01 00:33:20 +000088 default:
89 return NULL;
Harald Welte75a983f2008-12-27 21:34:06 +000090 }
91
92 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +000093 struct gsm_bts_trx_ts *ts = &trx->ts[j];
94 if (ts->pchan == GSM_PCHAN_NONE) {
95 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +000096 /* set channel attribute on OML */
97 abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]);
Harald Welte8470bf22008-12-25 23:28:35 +000098 return ts;
99 }
100 }
101 }
102 return NULL;
103}
104
105/* Free a physical channel (TS) */
106void ts_free(struct gsm_bts_trx_ts *ts)
107{
108 ts->pchan = GSM_PCHAN_NONE;
109}
110
111static const u_int8_t subslots_per_pchan[] = {
112 [GSM_PCHAN_NONE] = 0,
113 [GSM_PCHAN_CCCH] = 0,
114 [GSM_PCHAN_CCCH_SDCCH4] = 4,
115 [GSM_PCHAN_TCH_F] = 1,
116 [GSM_PCHAN_TCH_H] = 2,
Harald Welte4bfdfe72009-06-10 23:11:52 +0800117 [GSM_PCHAN_SDCCH8_SACCH8C] = 8,
Harald Welte8470bf22008-12-25 23:28:35 +0000118};
119
120static struct gsm_lchan *
121_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
122{
123 struct gsm_bts_trx *trx;
124 struct gsm_bts_trx_ts *ts;
125 int i, j, ss;
126 for (i = 0; i < bts->num_trx; i++) {
127 trx = &bts->trx[i];
128 for (j = 0; j < 8; j++) {
129 ts = &trx->ts[j];
130 if (ts->pchan != pchan)
131 continue;
132 /* check if all sub-slots are allocated yet */
133 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
134 struct gsm_lchan *lc = &ts->lchan[ss];
135 if (lc->type == GSM_LCHAN_NONE)
136 return lc;
137 }
138 }
139 }
140 /* we cannot allocate more of these */
141 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
142 return NULL;
143
144 /* if we've reached here, we need to allocate a new physical
145 * channel for the logical channel type requested */
146 ts = ts_alloc(bts, pchan);
147 if (!ts) {
148 /* no more radio resources */
149 return NULL;
150 }
151 return &ts->lchan[0];
152}
153
154/* Allocate a logical channel */
155struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
156{
157 struct gsm_lchan *lchan = NULL;
158
159 switch (type) {
160 case GSM_LCHAN_SDCCH:
161 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
162 if (lchan == NULL)
163 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
164 break;
165 case GSM_LCHAN_TCH_F:
166 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
167 break;
168 case GSM_LCHAN_TCH_H:
169 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
170 break;
171 default:
172 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
173 }
174
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000175 if (lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000176 lchan->type = type;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000177 lchan->use_count = 0;
178
179 /* Configure the time and start it so it will be closed */
180 lchan->release_timer.cb = auto_release_channel;
181 lchan->release_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000182 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000183 }
Harald Welte8470bf22008-12-25 23:28:35 +0000184
185 return lchan;
186}
187
188/* Free a logical channel */
189void lchan_free(struct gsm_lchan *lchan)
190{
191 lchan->type = GSM_LCHAN_NONE;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000192 if (lchan->subscr) {
193 subscr_put(lchan->subscr);
194 lchan->subscr = 0;
195 }
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000196
Holger Freyther7c19f742009-06-06 13:54:35 +0000197 /* We might kill an active channel... */
Harald Weltec627afc2009-01-09 21:39:17 +0000198 if (lchan->use_count != 0) {
Holger Freyther7c19f742009-06-06 13:54:35 +0000199 dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan);
Harald Weltec627afc2009-01-09 21:39:17 +0000200 lchan->use_count = 0;
201 }
202
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000203 /* stop the timer */
Harald Welteff117a82009-05-23 05:22:08 +0000204 bsc_del_timer(&lchan->release_timer);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000205
Harald Welte8470bf22008-12-25 23:28:35 +0000206 /* FIXME: ts_free() the timeslot, if we're the last logical
207 * channel using it */
208}
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000209
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000210/* Consider releasing the channel now */
211int lchan_auto_release(struct gsm_lchan *lchan)
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000212{
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000213 if (lchan->use_count > 0) {
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000214 return 0;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000215 }
216
Holger Freythere64a7a32009-02-06 21:55:37 +0000217 /* Assume we have GSM04.08 running and send a release */
218 if (lchan->subscr) {
219 gsm48_send_rr_release(lchan);
220 }
221
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000222 /* spoofed? message */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000223 if (lchan->use_count < 0) {
224 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
225 }
226
Harald Welte44227dd2009-02-01 22:25:58 +0000227 DEBUGP(DRLL, "Recycling the channel with: %d (%x)\n", lchan->nr, lchan->nr);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000228 rsl_chan_release(lchan);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000229 return 1;
230}
231
232/* Auto release the channel when the use count is zero */
233static void auto_release_channel(void *_lchan)
234{
235 struct gsm_lchan *lchan = _lchan;
236
237 if (!lchan_auto_release(lchan))
Harald Welteff117a82009-05-23 05:22:08 +0000238 bsc_schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000239}
240
Holger Freytherd0e38c32009-01-04 03:48:30 +0000241struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) {
242 int trx, ts_no, lchan_no;
243
244 for (trx = 0; trx < bts->num_trx; ++trx) {
245 for (ts_no = 0; ts_no < 8; ++ts_no) {
246 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
247 struct gsm_lchan *lchan =
248 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
249 if (subscr == lchan->subscr)
250 return lchan;
251 }
252 }
253 }
254
255 return NULL;
256}