blob: d76c929138001863ebc9b3ff7dbc1eadd75e39b5 [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
34static void auto_release_channel(struct gsm_lchan* 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;
86 }
87
88 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +000089 struct gsm_bts_trx_ts *ts = &trx->ts[j];
90 if (ts->pchan == GSM_PCHAN_NONE) {
91 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +000092 /* set channel attribute on OML */
93 abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]);
Harald Welte8470bf22008-12-25 23:28:35 +000094 return ts;
95 }
96 }
97 }
98 return NULL;
99}
100
101/* Free a physical channel (TS) */
102void ts_free(struct gsm_bts_trx_ts *ts)
103{
104 ts->pchan = GSM_PCHAN_NONE;
105}
106
107static const u_int8_t subslots_per_pchan[] = {
108 [GSM_PCHAN_NONE] = 0,
109 [GSM_PCHAN_CCCH] = 0,
110 [GSM_PCHAN_CCCH_SDCCH4] = 4,
111 [GSM_PCHAN_TCH_F] = 1,
112 [GSM_PCHAN_TCH_H] = 2,
113 [GSM_PCHAN_SDCCH8_SACCH8C] = 8.
114};
115
116static struct gsm_lchan *
117_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
118{
119 struct gsm_bts_trx *trx;
120 struct gsm_bts_trx_ts *ts;
121 int i, j, ss;
122 for (i = 0; i < bts->num_trx; i++) {
123 trx = &bts->trx[i];
124 for (j = 0; j < 8; j++) {
125 ts = &trx->ts[j];
126 if (ts->pchan != pchan)
127 continue;
128 /* check if all sub-slots are allocated yet */
129 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
130 struct gsm_lchan *lc = &ts->lchan[ss];
131 if (lc->type == GSM_LCHAN_NONE)
132 return lc;
133 }
134 }
135 }
136 /* we cannot allocate more of these */
137 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
138 return NULL;
139
140 /* if we've reached here, we need to allocate a new physical
141 * channel for the logical channel type requested */
142 ts = ts_alloc(bts, pchan);
143 if (!ts) {
144 /* no more radio resources */
145 return NULL;
146 }
147 return &ts->lchan[0];
148}
149
150/* Allocate a logical channel */
151struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
152{
153 struct gsm_lchan *lchan = NULL;
154
155 switch (type) {
156 case GSM_LCHAN_SDCCH:
157 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
158 if (lchan == NULL)
159 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
160 break;
161 case GSM_LCHAN_TCH_F:
162 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
163 break;
164 case GSM_LCHAN_TCH_H:
165 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
166 break;
167 default:
168 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
169 }
170
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000171 if (lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000172 lchan->type = type;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000173 lchan->use_count = 0;
174
175 /* Configure the time and start it so it will be closed */
176 lchan->release_timer.cb = auto_release_channel;
177 lchan->release_timer.data = lchan;
178 schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
179 }
Harald Welte8470bf22008-12-25 23:28:35 +0000180
181 return lchan;
182}
183
184/* Free a logical channel */
185void lchan_free(struct gsm_lchan *lchan)
186{
187 lchan->type = GSM_LCHAN_NONE;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000188
189 /* stop the timer */
190 del_timer(&lchan->release_timer);
191
Harald Welte8470bf22008-12-25 23:28:35 +0000192 /* FIXME: ts_free() the timeslot, if we're the last logical
193 * channel using it */
194}
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000195
196/*
197 * Auto release the channel when the use count is zero
198 */
199static void auto_release_channel(struct gsm_lchan* lchan)
200{
201 /*
202 * Busy...
203 */
204 if (lchan->use_count > 0) {
205 schedule_timer(&lchan->release_timer, LCHAN_RELEASE_TIMEOUT);
206 return;
207 }
208
209 /*
210 * spoofed? message
211 */
212 if (lchan->use_count < 0) {
213 DEBUGP(DRLL, "Channel count is negative: %d\n", lchan->use_count);
214 }
215
216 DEBUGP(DRLL, "Recylcing the channel with: %d (%x)\n", lchan->nr, lchan->nr);
217 rsl_chan_release(lchan);
218}
219