blob: f5c4ec65e1427d10c29d4d54c5a1466461874ff3 [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
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +080036#include <osmocore/talloc.h>
37
Harald Weltef85497c2009-01-01 00:33:20 +000038static void auto_release_channel(void *_lchan);
Harald Welte8470bf22008-12-25 23:28:35 +000039
Harald Welte4c704542009-12-24 10:10:16 +010040static int ts_is_usable(struct gsm_bts_trx_ts *ts)
41{
42 /* FIXME: How does this behave for BS-11 ? */
43 if (is_ipaccess_bts(ts->trx->bts)) {
Sylvain Munaut1f6c11f2010-01-02 16:32:17 +010044 if (!nm_is_running(&ts->nm_state))
Harald Welte4c704542009-12-24 10:10:16 +010045 return 0;
46 }
47
48 return 1;
49}
50
Harald Welte (local)82ff3972009-12-28 16:36:28 +010051int trx_is_usable(struct gsm_bts_trx *trx)
Harald Welte4c704542009-12-24 10:10:16 +010052{
53 /* FIXME: How does this behave for BS-11 ? */
54 if (is_ipaccess_bts(trx->bts)) {
Sylvain Munaut1f6c11f2010-01-02 16:32:17 +010055 if (!nm_is_running(&trx->nm_state) ||
56 !nm_is_running(&trx->bb_transc.nm_state))
Harald Welte4c704542009-12-24 10:10:16 +010057 return 0;
58 }
59
60 return 1;
61}
62
Harald Welte8470bf22008-12-25 23:28:35 +000063struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts,
64 enum gsm_phys_chan_config pchan)
65{
Harald Weltee441d9c2009-06-21 16:17:15 +020066 struct gsm_bts_trx *trx = bts->c0;
Harald Welte8470bf22008-12-25 23:28:35 +000067 struct gsm_bts_trx_ts *ts = &trx->ts[0];
68
69 if (pchan != GSM_PCHAN_CCCH &&
70 pchan != GSM_PCHAN_CCCH_SDCCH4)
71 return NULL;
72
73 if (ts->pchan != GSM_PCHAN_NONE)
74 return NULL;
75
76 ts->pchan = pchan;
77
78 return ts;
79}
80
Harald Welte4bfdfe72009-06-10 23:11:52 +080081/* Allocate a physical channel (TS) */
Harald Welte8470bf22008-12-25 23:28:35 +000082struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
83 enum gsm_phys_chan_config pchan)
84{
Harald Welte88367262009-08-10 13:25:55 +020085 int j;
86 struct gsm_bts_trx *trx;
87
88 llist_for_each_entry(trx, &bts->trx_list, list) {
Harald Welte75a983f2008-12-27 21:34:06 +000089 int from, to;
90
Harald Welte4c704542009-12-24 10:10:16 +010091 if (!trx_is_usable(trx))
92 continue;
93
Harald Welte75a983f2008-12-27 21:34:06 +000094 /* the following constraints are pure policy,
95 * no requirement to put this restriction in place */
Harald Welted46299d2009-07-29 16:46:37 +020096 if (trx == bts->c0) {
97 /* On the first TRX we run one CCCH and one SDCCH8 */
98 switch (pchan) {
99 case GSM_PCHAN_CCCH:
100 case GSM_PCHAN_CCCH_SDCCH4:
101 from = 0; to = 0;
102 break;
Harald Welted46299d2009-07-29 16:46:37 +0200103 case GSM_PCHAN_TCH_F:
104 case GSM_PCHAN_TCH_H:
Harald Welte89e9d592009-08-09 22:01:26 +0200105 from = 1; to = 7;
Harald Welted46299d2009-07-29 16:46:37 +0200106 break;
Harald Welte89e9d592009-08-09 22:01:26 +0200107 case GSM_PCHAN_SDCCH8_SACCH8C:
Harald Welted46299d2009-07-29 16:46:37 +0200108 default:
109 return NULL;
110 }
111 } else {
112 /* Every secondary TRX is configured for TCH/F
113 * and TCH/H only */
114 switch (pchan) {
Harald Welte89e9d592009-08-09 22:01:26 +0200115 case GSM_PCHAN_SDCCH8_SACCH8C:
116 from = 1; to = 1;
Harald Welted46299d2009-07-29 16:46:37 +0200117 case GSM_PCHAN_TCH_F:
118 case GSM_PCHAN_TCH_H:
Harald Welte89e9d592009-08-09 22:01:26 +0200119 from = 1; to = 7;
Harald Welted46299d2009-07-29 16:46:37 +0200120 break;
121 default:
122 return NULL;
123 }
Harald Welte75a983f2008-12-27 21:34:06 +0000124 }
125
126 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000127 struct gsm_bts_trx_ts *ts = &trx->ts[j];
Harald Welte4c704542009-12-24 10:10:16 +0100128
129 if (!ts_is_usable(ts))
130 continue;
131
Harald Welte8470bf22008-12-25 23:28:35 +0000132 if (ts->pchan == GSM_PCHAN_NONE) {
133 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +0000134 /* set channel attribute on OML */
Harald Welte21bd3a52009-08-10 12:21:22 +0200135 abis_nm_set_channel_attr(ts, abis_nm_chcomb4pchan(pchan));
Harald Welte8470bf22008-12-25 23:28:35 +0000136 return ts;
137 }
138 }
139 }
140 return NULL;
141}
142
143/* Free a physical channel (TS) */
144void ts_free(struct gsm_bts_trx_ts *ts)
145{
146 ts->pchan = GSM_PCHAN_NONE;
147}
148
149static const u_int8_t subslots_per_pchan[] = {
150 [GSM_PCHAN_NONE] = 0,
151 [GSM_PCHAN_CCCH] = 0,
152 [GSM_PCHAN_CCCH_SDCCH4] = 4,
153 [GSM_PCHAN_TCH_F] = 1,
154 [GSM_PCHAN_TCH_H] = 2,
Harald Welte4bfdfe72009-06-10 23:11:52 +0800155 [GSM_PCHAN_SDCCH8_SACCH8C] = 8,
Harald Welteb908cb72009-12-22 13:09:29 +0100156 /* FIXME: what about dynamic TCH_F_TCH_H ? */
Harald Weltec0d83b02010-03-28 15:58:03 +0800157 [GSM_PCHAN_TCH_F_PDCH] = 1,
Harald Welte8470bf22008-12-25 23:28:35 +0000158};
159
160static struct gsm_lchan *
Harald Weltefc0d9522009-08-10 13:46:55 +0200161_lc_find_trx(struct gsm_bts_trx *trx, enum gsm_phys_chan_config pchan)
Harald Welte8470bf22008-12-25 23:28:35 +0000162{
Harald Welte8470bf22008-12-25 23:28:35 +0000163 struct gsm_bts_trx_ts *ts;
Harald Welte88367262009-08-10 13:25:55 +0200164 int j, ss;
165
Harald Welte4c704542009-12-24 10:10:16 +0100166 if (!trx_is_usable(trx))
167 return NULL;
168
Harald Weltefc0d9522009-08-10 13:46:55 +0200169 for (j = 0; j < 8; j++) {
170 ts = &trx->ts[j];
Harald Welte4c704542009-12-24 10:10:16 +0100171 if (!ts_is_usable(ts))
172 continue;
Harald Weltec0d83b02010-03-28 15:58:03 +0800173 /* ip.access dynamic TCH/F + PDCH combination */
174 if (ts->pchan == GSM_PCHAN_TCH_F_PDCH &&
175 pchan == GSM_PCHAN_TCH_F) {
176 /* we can only consider such a dynamic channel
177 * if the PDCH is currently inactive */
178 if (ts->flags & TS_F_PDCH_MODE)
179 continue;
180 } else if (ts->pchan != pchan)
Harald Weltefc0d9522009-08-10 13:46:55 +0200181 continue;
182 /* check if all sub-slots are allocated yet */
183 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
184 struct gsm_lchan *lc = &ts->lchan[ss];
Harald Welte (local)3e460312009-12-27 18:12:29 +0100185 if (lc->type == GSM_LCHAN_NONE &&
186 lc->state == LCHAN_S_NONE)
Harald Weltefc0d9522009-08-10 13:46:55 +0200187 return lc;
Harald Welte8470bf22008-12-25 23:28:35 +0000188 }
189 }
Harald Weltec0d83b02010-03-28 15:58:03 +0800190
Harald Weltefc0d9522009-08-10 13:46:55 +0200191 return NULL;
192}
193
194static struct gsm_lchan *
195_lc_find_bts(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
196{
197 struct gsm_bts_trx *trx;
198 struct gsm_bts_trx_ts *ts;
199 struct gsm_lchan *lc;
200
201 if (bts->chan_alloc_reverse) {
202 llist_for_each_entry_reverse(trx, &bts->trx_list, list) {
203 lc = _lc_find_trx(trx, pchan);
204 if (lc)
205 return lc;
206 }
207 } else {
208 llist_for_each_entry(trx, &bts->trx_list, list) {
209 lc = _lc_find_trx(trx, pchan);
210 if (lc)
211 return lc;
212 }
213 }
214
Harald Welte8470bf22008-12-25 23:28:35 +0000215 /* we cannot allocate more of these */
216 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
217 return NULL;
218
219 /* if we've reached here, we need to allocate a new physical
220 * channel for the logical channel type requested */
221 ts = ts_alloc(bts, pchan);
222 if (!ts) {
223 /* no more radio resources */
224 return NULL;
225 }
226 return &ts->lchan[0];
227}
228
229/* Allocate a logical channel */
230struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
231{
232 struct gsm_lchan *lchan = NULL;
Harald Welte65676fe2009-08-10 14:44:24 +0200233 enum gsm_phys_chan_config first, second;
Harald Welte8470bf22008-12-25 23:28:35 +0000234
235 switch (type) {
236 case GSM_LCHAN_SDCCH:
Harald Welte65676fe2009-08-10 14:44:24 +0200237 if (bts->chan_alloc_reverse) {
238 first = GSM_PCHAN_SDCCH8_SACCH8C;
239 second = GSM_PCHAN_CCCH_SDCCH4;
240 } else {
241 first = GSM_PCHAN_CCCH_SDCCH4;
242 second = GSM_PCHAN_SDCCH8_SACCH8C;
243 }
244
245 lchan = _lc_find_bts(bts, first);
Harald Welte8470bf22008-12-25 23:28:35 +0000246 if (lchan == NULL)
Harald Welte65676fe2009-08-10 14:44:24 +0200247 lchan = _lc_find_bts(bts, second);
Harald Welte8470bf22008-12-25 23:28:35 +0000248 break;
249 case GSM_LCHAN_TCH_F:
Harald Weltefc0d9522009-08-10 13:46:55 +0200250 lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
Harald Welte8470bf22008-12-25 23:28:35 +0000251 break;
252 case GSM_LCHAN_TCH_H:
Harald Weltefc0d9522009-08-10 13:46:55 +0200253 lchan =_lc_find_bts(bts, GSM_PCHAN_TCH_H);
Harald Welte210c8502009-12-12 20:58:20 +0100254 /* If we don't have TCH/H available, fall-back to TCH/F */
Harald Welte487e6be2009-12-12 21:16:38 +0100255 if (!lchan) {
Harald Welte210c8502009-12-12 20:58:20 +0100256 lchan = _lc_find_bts(bts, GSM_PCHAN_TCH_F);
Harald Welte487e6be2009-12-12 21:16:38 +0100257 type = GSM_LCHAN_TCH_F;
258 }
Harald Welte8470bf22008-12-25 23:28:35 +0000259 break;
260 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100261 LOGP(DRLL, LOGL_ERROR, "Unknown gsm_chan_t %u\n", type);
Harald Welte8470bf22008-12-25 23:28:35 +0000262 }
263
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000264 if (lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000265 lchan->type = type;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000266
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +0100267 /* clear sapis */
Holger Hans Peter Freyther45b02432009-11-18 22:59:51 +0100268 memset(lchan->sapis, 0, ARRAY_SIZE(lchan->sapis));
Holger Hans Peter Freyther5ba6f482009-10-28 14:23:39 +0100269
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +0100270 /* clear multi rate config */
271 memset(&lchan->mr_conf, 0, sizeof(lchan->mr_conf));
272
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100273 /* clear per MSC/BSC data */
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800274 if (lchan->conn) {
275 LOGP(DRLL, LOGL_ERROR, "lchan->conn should be NULL.\n");
276 subscr_con_free(lchan->conn);
277 }
Harald Weltec0d83b02010-03-28 15:58:03 +0800278 } else {
279 struct challoc_signal_data sig;
280 sig.bts = bts;
281 sig.type = type;
282 dispatch_signal(SS_CHALLOC, S_CHALLOC_ALLOC_FAIL, &sig);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000283 }
Harald Welte8470bf22008-12-25 23:28:35 +0000284
285 return lchan;
286}
287
288/* Free a logical channel */
289void lchan_free(struct gsm_lchan *lchan)
290{
Harald Weltec0d83b02010-03-28 15:58:03 +0800291 struct challoc_signal_data sig;
Harald Welted12b0fd2009-12-15 21:36:05 +0100292 int i;
293
Harald Weltec0d83b02010-03-28 15:58:03 +0800294 sig.type = lchan->type;
Harald Welte8470bf22008-12-25 23:28:35 +0000295 lchan->type = GSM_LCHAN_NONE;
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800296
297
298 if (lchan->conn) {
299 if (lchan->conn->subscr) {
300 subscr_put(lchan->conn->subscr);
301 lchan->conn->subscr = NULL;
302 }
303
304 /* We might kill an active channel... */
305 if (lchan->conn->use_count != 0) {
306 dispatch_signal(SS_LCHAN, S_LCHAN_UNEXPECTED_RELEASE, lchan);
307 lchan->conn->use_count = 0;
308 }
309 /* stop the timer */
310 bsc_del_timer(&lchan->conn->release_timer);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000311 }
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000312
Harald Weltec627afc2009-01-09 21:39:17 +0000313
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000314 /* stop the timer */
Sylvain Munautf77af1f2009-12-21 01:11:25 +0100315 bsc_del_timer(&lchan->T3101);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000316
Harald Welted12b0fd2009-12-15 21:36:05 +0100317 /* clear cached measuement reports */
318 lchan->meas_rep_idx = 0;
319 for (i = 0; i < ARRAY_SIZE(lchan->meas_rep); i++) {
320 lchan->meas_rep[i].flags = 0;
321 lchan->meas_rep[i].nr = 0;
322 }
Harald Weltef7c28b02009-12-21 13:30:17 +0100323 for (i = 0; i < ARRAY_SIZE(lchan->neigh_meas); i++)
324 lchan->neigh_meas[i].arfcn = 0;
Harald Welted12b0fd2009-12-15 21:36:05 +0100325
Holger Hans Peter Freyther5ba05f42010-06-22 12:11:59 +0800326 if (lchan->rqd_ref) {
327 talloc_free(lchan->rqd_ref);
328 lchan->rqd_ref = NULL;
329 lchan->rqd_ta = 0;
330 }
331
Harald Weltec0d83b02010-03-28 15:58:03 +0800332 sig.lchan = lchan;
333 sig.bts = lchan->ts->trx->bts;
334 dispatch_signal(SS_CHALLOC, S_CHALLOC_FREED, &sig);
335
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800336 if (lchan->conn) {
337 subscr_con_free(lchan->conn);
338 lchan->conn = NULL;
339 }
340
Harald Welte8470bf22008-12-25 23:28:35 +0000341 /* FIXME: ts_free() the timeslot, if we're the last logical
342 * channel using it */
343}
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000344
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200345/*
346 * There was an error with the TRX and we need to forget
347 * any state so that a lchan can be allocated again after
348 * the trx is fully usable.
Holger Hans Peter Freytherf7a1c232010-06-22 12:19:06 +0800349 *
350 * This should be called after lchan_free to force a channel
351 * be available for allocation again. This means that this
352 * method will stop the "delay after error"-timer and set the
353 * state to LCHAN_S_NONE.
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200354 */
355void lchan_reset(struct gsm_lchan *lchan)
356{
357 bsc_del_timer(&lchan->T3101);
Holger Hans Peter Freytherde4b0a22010-06-08 12:12:26 +0800358 bsc_del_timer(&lchan->T3111);
359 bsc_del_timer(&lchan->error_timer);
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200360
361 lchan->type = GSM_LCHAN_NONE;
362 lchan->state = LCHAN_S_NONE;
363}
364
365
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000366/* Consider releasing the channel now */
367int lchan_auto_release(struct gsm_lchan *lchan)
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000368{
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800369 if (!lchan->conn)
370 return 0;
371
372 if (lchan->conn->use_count > 0) {
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000373 return 0;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000374 }
375
Holger Freythere64a7a32009-02-06 21:55:37 +0000376 /* Assume we have GSM04.08 running and send a release */
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800377 if (lchan->conn->subscr) {
Holger Freythere64a7a32009-02-06 21:55:37 +0000378 gsm48_send_rr_release(lchan);
379 }
380
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000381 /* spoofed? message */
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800382 if (lchan->conn->use_count < 0)
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100383 LOGP(DRLL, LOGL_ERROR, "Channel count is negative: %d\n",
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800384 lchan->conn->use_count);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000385
Harald Welte (local)0b114142009-12-28 23:22:43 +0100386 DEBUGP(DRLL, "%s Recycling Channel\n", gsm_lchan_name(lchan));
Holger Hans Peter Freyther63d18b52010-04-10 00:14:55 +0200387 rsl_lchan_set_state(lchan, LCHAN_S_REL_REQ);
Holger Hans Peter Freyther4f5848d2010-06-08 11:57:45 +0800388 rsl_release_request(lchan, 0, 0);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000389 return 1;
390}
391
392/* Auto release the channel when the use count is zero */
393static void auto_release_channel(void *_lchan)
394{
395 struct gsm_lchan *lchan = _lchan;
396
397 if (!lchan_auto_release(lchan))
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800398 bsc_schedule_timer(&lchan->conn->release_timer, LCHAN_RELEASE_TIMEOUT);
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000399}
400
Holger Hans Peter Freytherea01ca72010-06-16 13:05:35 +0800401static struct gsm_lchan* lchan_find(struct gsm_bts *bts, struct gsm_subscriber *subscr) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200402 struct gsm_bts_trx *trx;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200403 int ts_no, lchan_no;
Holger Freytherd0e38c32009-01-04 03:48:30 +0000404
Harald Weltee441d9c2009-06-21 16:17:15 +0200405 llist_for_each_entry(trx, &bts->trx_list, list) {
Holger Freytherd0e38c32009-01-04 03:48:30 +0000406 for (ts_no = 0; ts_no < 8; ++ts_no) {
407 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
408 struct gsm_lchan *lchan =
Harald Weltee441d9c2009-06-21 16:17:15 +0200409 &trx->ts[ts_no].lchan[lchan_no];
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800410 if (lchan->conn && subscr == lchan->conn->subscr)
Holger Freytherd0e38c32009-01-04 03:48:30 +0000411 return lchan;
412 }
413 }
414 }
415
416 return NULL;
417}
Harald Welte1a6f7982009-08-09 18:52:33 +0200418
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800419struct gsm_subscriber_connection *connection_for_subscr(struct gsm_subscriber *subscr)
Harald Welte1a6f7982009-08-09 18:52:33 +0200420{
421 struct gsm_bts *bts;
422 struct gsm_network *net = subscr->net;
423 struct gsm_lchan *lchan;
424
425 llist_for_each_entry(bts, &net->bts_list, list) {
426 lchan = lchan_find(bts, subscr);
427 if (lchan)
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800428 return lchan->conn;
Harald Welte1a6f7982009-08-09 18:52:33 +0200429 }
430
Holger Hans Peter Freytherba925872009-10-27 10:42:53 +0100431 return NULL;
Harald Welte1a6f7982009-08-09 18:52:33 +0200432}
Harald Welteb908cb72009-12-22 13:09:29 +0100433
434void bts_chan_load(struct pchan_load *cl, const struct gsm_bts *bts)
435{
436 struct gsm_bts_trx *trx;
437
438 llist_for_each_entry(trx, &bts->trx_list, list) {
439 int i;
440
441 /* skip administratively deactivated tranxsceivers */
Sylvain Munaut1f6c11f2010-01-02 16:32:17 +0100442 if (!nm_is_running(&trx->nm_state) ||
443 !nm_is_running(&trx->bb_transc.nm_state))
Harald Welteb908cb72009-12-22 13:09:29 +0100444 continue;
445
446 for (i = 0; i < ARRAY_SIZE(trx->ts); i++) {
447 struct gsm_bts_trx_ts *ts = &trx->ts[i];
448 struct load_counter *pl = &cl->pchan[ts->pchan];
449 int j;
450
451 /* skip administratively deactivated timeslots */
Sylvain Munaut1f6c11f2010-01-02 16:32:17 +0100452 if (!nm_is_running(&ts->nm_state))
Harald Welteb908cb72009-12-22 13:09:29 +0100453 continue;
454
455 for (j = 0; j < subslots_per_pchan[ts->pchan]; j++) {
456 struct gsm_lchan *lchan = &ts->lchan[j];
457
458 pl->total++;
459
460 switch (lchan->state) {
461 case LCHAN_S_NONE:
462 break;
463 default:
464 pl->used++;
465 break;
466 }
467 }
468 }
469 }
470}
471
472void network_chan_load(struct pchan_load *pl, struct gsm_network *net)
473{
474 struct gsm_bts *bts;
475
476 memset(pl, 0, sizeof(*pl));
477
478 llist_for_each_entry(bts, &net->bts_list, list)
479 bts_chan_load(pl, bts);
480}
Holger Hans Peter Freyther2412a072010-06-28 15:47:12 +0800481
482struct gsm_subscriber_connection *subscr_con_allocate(struct gsm_lchan *lchan)
483{
484 struct gsm_subscriber_connection *conn;
485
486 conn = talloc_zero(lchan->ts->trx->bts, struct gsm_subscriber_connection);
487 if (!conn)
488 return NULL;
489
490 /* Configure the time and start it so it will be closed */
491 conn->lchan = lchan;
492 conn->bts = lchan->ts->trx->bts;
493 conn->release_timer.cb = auto_release_channel;
494 conn->release_timer.data = lchan;
495 bsc_schedule_timer(&conn->release_timer, LCHAN_RELEASE_TIMEOUT);
496
497 lchan->conn = conn;
498 return conn;
499}
500
501/* TODO: move subscriber put here... */
502void subscr_con_free(struct gsm_subscriber_connection *conn)
503{
504 struct gsm_lchan *lchan;
505
506
507 if (!conn)
508 return;
509
510 lchan = conn->lchan;
511 talloc_free(conn);
512
513 if (lchan)
514 lchan->conn = NULL;
515}