blob: bb71d35d9f11c85b2a85f365eb8a6c3ff8f8274c [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 Freytherc6ea9db2008-12-30 19:18:21 +000032#include <openbsc/debug.h>
33
Harald Weltef85497c2009-01-01 00:33:20 +000034static void auto_release_channel(void *_lchan);
Harald Welte8470bf22008-12-25 23:28:35 +000035
36struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts,
37 enum gsm_phys_chan_config pchan)
38{
39 struct gsm_bts_trx *trx = &bts->trx[0];
40 struct gsm_bts_trx_ts *ts = &trx->ts[0];
41
42 if (pchan != GSM_PCHAN_CCCH &&
43 pchan != GSM_PCHAN_CCCH_SDCCH4)
44 return NULL;
45
46 if (ts->pchan != GSM_PCHAN_NONE)
47 return NULL;
48
49 ts->pchan = pchan;
50
51 return ts;
52}
53
Harald Welte4b634542008-12-27 01:55:51 +000054static const enum abis_nm_chan_comb chcomb4pchan[] = {
55 [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH,
56 [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCCHComb,
57 [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull,
58 [GSM_PCHAN_TCH_H] = NM_CHANC_TCHHalf,
59 [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH,
60 /* FIXME: bounds check */
61};
Harald Welte8470bf22008-12-25 23:28:35 +000062
63/* Allocate a logical channel (TS) */
64struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
65 enum gsm_phys_chan_config pchan)
66{
67 int i, j;
68 for (i = 0; i < bts->num_trx; i++) {
69 struct gsm_bts_trx *trx = &bts->trx[i];
Harald Welte75a983f2008-12-27 21:34:06 +000070 int from, to;
71
72 /* the following constraints are pure policy,
73 * no requirement to put this restriction in place */
74 switch (pchan) {
75 case GSM_PCHAN_CCCH:
76 case GSM_PCHAN_CCCH_SDCCH4:
77 from = 0; to = 0;
78 break;
79 case GSM_PCHAN_SDCCH8_SACCH8C:
80 from = 1; to = 1;
81 break;
82 case GSM_PCHAN_TCH_F:
83 case GSM_PCHAN_TCH_H:
84 from = 2; to = 7;
85 break;
Harald Weltef85497c2009-01-01 00:33:20 +000086 default:
87 return NULL;
Harald Welte75a983f2008-12-27 21:34:06 +000088 }
89
90 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +000091 struct gsm_bts_trx_ts *ts = &trx->ts[j];
92 if (ts->pchan == GSM_PCHAN_NONE) {
93 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +000094 /* set channel attribute on OML */
95 abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]);
Harald Welte8470bf22008-12-25 23:28:35 +000096 return ts;
97 }
98 }
99 }
100 return NULL;
101}
102
103/* Free a physical channel (TS) */
104void ts_free(struct gsm_bts_trx_ts *ts)
105{
106 ts->pchan = GSM_PCHAN_NONE;
107}
108
109static const u_int8_t subslots_per_pchan[] = {
110 [GSM_PCHAN_NONE] = 0,
111 [GSM_PCHAN_CCCH] = 0,
112 [GSM_PCHAN_CCCH_SDCCH4] = 4,
113 [GSM_PCHAN_TCH_F] = 1,
114 [GSM_PCHAN_TCH_H] = 2,
115 [GSM_PCHAN_SDCCH8_SACCH8C] = 8.
116};
117
118static struct gsm_lchan *
119_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
120{
121 struct gsm_bts_trx *trx;
122 struct gsm_bts_trx_ts *ts;
123 int i, j, ss;
124 for (i = 0; i < bts->num_trx; i++) {
125 trx = &bts->trx[i];
126 for (j = 0; j < 8; j++) {
127 ts = &trx->ts[j];
128 if (ts->pchan != pchan)
129 continue;
130 /* check if all sub-slots are allocated yet */
131 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
132 struct gsm_lchan *lc = &ts->lchan[ss];
133 if (lc->type == GSM_LCHAN_NONE)
134 return lc;
135 }
136 }
137 }
138 /* we cannot allocate more of these */
139 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
140 return NULL;
141
142 /* if we've reached here, we need to allocate a new physical
143 * channel for the logical channel type requested */
144 ts = ts_alloc(bts, pchan);
145 if (!ts) {
146 /* no more radio resources */
147 return NULL;
148 }
149 return &ts->lchan[0];
150}
151
152/* Allocate a logical channel */
153struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
154{
155 struct gsm_lchan *lchan = NULL;
156
157 switch (type) {
158 case GSM_LCHAN_SDCCH:
159 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
160 if (lchan == NULL)
161 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
162 break;
163 case GSM_LCHAN_TCH_F:
164 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
165 break;
166 case GSM_LCHAN_TCH_H:
167 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
168 break;
169 default:
170 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
171 }
172
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000173 if (lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000174 lchan->type = type;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000175 lchan->use_count = 0;
176
177 /* Configure the time and start it so it will be closed */
178 lchan->release_timer.cb = auto_release_channel;
179 lchan->release_timer.data = lchan;
180 schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
181 }
Harald Welte8470bf22008-12-25 23:28:35 +0000182
183 return lchan;
184}
185
186/* Free a logical channel */
187void lchan_free(struct gsm_lchan *lchan)
188{
189 lchan->type = GSM_LCHAN_NONE;
Holger Freyther41ed3002008-12-31 18:52:54 +0000190 lchan->subscr = 0;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000191
192 /* stop the timer */
193 del_timer(&lchan->release_timer);
194
Harald Welte8470bf22008-12-25 23:28:35 +0000195 /* FIXME: ts_free() the timeslot, if we're the last logical
196 * channel using it */
197}
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000198
199/*
200 * Auto release the channel when the use count is zero
201 */
Harald Weltef85497c2009-01-01 00:33:20 +0000202static void auto_release_channel(void *_lchan)
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000203{
Harald Weltef85497c2009-01-01 00:33:20 +0000204 struct gsm_lchan *lchan = _lchan;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000205 /*
206 * Busy...
207 */
208 if (lchan->use_count > 0) {
209 schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
210 return;
211 }
212
213 /*
214 * spoofed? message
215 */
216 if (lchan->use_count < 0) {
217 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
218 }
219
220 DEBUGP(DRLL, "Recylcing the channel with: %d (%x)\n", lchan->nr, lchan->nr);
221 rsl_chan_release(lchan);
222}
223