blob: 1ce3eaaa14a53414f30f6589df463eaa599e7200 [file] [log] [blame]
Harald Weltefd355a32011-03-04 13:41:31 +01001/* OpenBSC support code for HSL Femtocell */
2
3/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2011 by OnWaves
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 Affero General Public License as published by
10 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Holger Hans Peter Freyther4dd84ff2012-09-11 12:33:51 +020023#include <inttypes.h>
Harald Weltefd355a32011-03-04 13:41:31 +010024
25#include <arpa/inet.h>
26
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/gsm/tlv.h>
Harald Weltefd355a32011-03-04 13:41:31 +010028#include <openbsc/gsm_data.h>
29#include <openbsc/abis_nm.h>
30#include <openbsc/abis_rsl.h>
31#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020032#include <openbsc/debug.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/abis/e1_input.h>
35#include <osmocom/abis/ipaccess.h>
Harald Weltefd355a32011-03-04 13:41:31 +010036
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020037static int bts_model_hslfemto_start(struct gsm_network *net);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020038static void bts_model_hslfemto_e1line_bind_ops(struct e1inp_line *line);
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020039
Harald Weltefd355a32011-03-04 13:41:31 +010040static struct gsm_bts_model model_hslfemto = {
41 .type = GSM_BTS_TYPE_HSL_FEMTO,
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020042 .start = bts_model_hslfemto_start,
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020043 .e1line_bind_ops = &bts_model_hslfemto_e1line_bind_ops,
Harald Weltefd355a32011-03-04 13:41:31 +010044 .nm_att_tlvdef = {
45 .def = {
46 /* no HSL specific OML attributes that we know of */
47 },
48 },
49};
50
51
52static const uint8_t l1_msg[] = {
Harald Welte388cbbf2011-01-15 18:07:34 +010053#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010054 0x80, 0x8a,
Harald Welte388cbbf2011-01-15 18:07:34 +010055#else
56 0x81, 0x8a,
57#endif
Harald Weltefd355a32011-03-04 13:41:31 +010058 0xC4, 0x0b,
59};
60
61static const uint8_t conn_trau_msg[] = {
Harald Welte388cbbf2011-01-15 18:07:34 +010062#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010063 0x80, 0x81,
Harald Welte388cbbf2011-01-15 18:07:34 +010064#else
65 0x81, 0x81,
66#endif
Harald Weltefd355a32011-03-04 13:41:31 +010067 0xC1, 16,
68 0x02, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0xEA, 0x01,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
70};
71
72static const uint8_t conn_trau_msg2[] = {
Harald Welte388cbbf2011-01-15 18:07:34 +010073#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010074 0x80, 0x81,
Harald Welte388cbbf2011-01-15 18:07:34 +010075#else
76 0x81, 0x81,
77#endif
Harald Weltefd355a32011-03-04 13:41:31 +010078 0xC1, 16,
79 0x02, 0x00, 0xd4, 0x07, 0xC0, 0xA8, 0xEA, 0x01,
80 0x38, 0xA4, 0x45, 0x00, 0x04, 0x59, 0x40, 0x00
81};
82
83static uint8_t oml_arfcn_bsic[] = {
Harald Welte26d79072011-01-14 23:18:59 +010084#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010085 0x81, 0x80, 0x00, 10,
Harald Welte26d79072011-01-14 23:18:59 +010086#else
87 0x80, 0x80, 0x00, 10,
88#endif
Harald Weltefd355a32011-03-04 13:41:31 +010089 NM_MT_SET_BTS_ATTR, NM_OC_BTS, 0xff, 0xff, 0xff,
90 NM_ATT_BCCH_ARFCN, 0x03, 0x67,
91 NM_ATT_BSIC, 0x00
92};
93
94static inline struct msgb *hsl_alloc_msgb(void)
95{
96 return msgb_alloc_headroom(1024, 127, "HSL");
97}
98
99static int hslfemto_bootstrap_om(struct gsm_bts *bts)
100{
101 struct msgb *msg;
Harald Weltefd355a32011-03-04 13:41:31 +0100102
103 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200104 msgb_put(msg, sizeof(l1_msg));
Harald Weltefd355a32011-03-04 13:41:31 +0100105 memcpy(msg->data, l1_msg, sizeof(l1_msg));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200106 msg->dst = bts->c0->rsl_link;
Harald Weltefd355a32011-03-04 13:41:31 +0100107 abis_rsl_sendmsg(msg);
108
109#if 1
110 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200111 msgb_put(msg, sizeof(conn_trau_msg));
Harald Weltefd355a32011-03-04 13:41:31 +0100112 memcpy(msg->data, conn_trau_msg, sizeof(conn_trau_msg));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200113 msg->dst = bts->c0->rsl_link;
Harald Weltefd355a32011-03-04 13:41:31 +0100114 abis_rsl_sendmsg(msg);
115#endif
116 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200117 msgb_put(msg, sizeof(conn_trau_msg2));
Harald Weltefd355a32011-03-04 13:41:31 +0100118 memcpy(msg->data, conn_trau_msg2, sizeof(conn_trau_msg2));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200119 msg->dst = bts->c0->rsl_link;
Harald Weltefd355a32011-03-04 13:41:31 +0100120 abis_rsl_sendmsg(msg);
121
122 *((uint16_t *)oml_arfcn_bsic+10) = htons(bts->c0->arfcn);
123 oml_arfcn_bsic[13] = bts->bsic;
124
125 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200126 msgb_put(msg, sizeof(oml_arfcn_bsic));
Harald Weltefd355a32011-03-04 13:41:31 +0100127 memcpy(msg->data, oml_arfcn_bsic, sizeof(oml_arfcn_bsic));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200128 msg->dst = bts->c0->rsl_link;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200129 abis_sendmsg(msg);
Harald Weltefd355a32011-03-04 13:41:31 +0100130
131 /* Delay the OPSTART until after SI have been set via RSL */
132 //abis_nm_opstart(bts, NM_OC_BTS, 255, 255, 255);
133
134 return 0;
135}
136
137/* Callback function to be called every time we receive a signal from INPUT */
138static int inp_sig_cb(unsigned int subsys, unsigned int signal,
139 void *handler_data, void *signal_data)
140{
141 struct input_signal_data *isd = signal_data;
142
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200143 if (subsys != SS_L_INPUT)
Harald Weltefd355a32011-03-04 13:41:31 +0100144 return 0;
145
146 switch (signal) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200147 case S_L_INP_TEI_UP:
Harald Weltefd355a32011-03-04 13:41:31 +0100148 switch (isd->link_type) {
149 case E1INP_SIGN_OML:
Harald Welted4571f42011-03-05 16:24:20 +0100150 if (isd->trx->bts->type == GSM_BTS_TYPE_HSL_FEMTO)
151 hslfemto_bootstrap_om(isd->trx->bts);
Harald Weltefd355a32011-03-04 13:41:31 +0100152 break;
153 }
154 }
155
156 return 0;
157}
158
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200159static struct gsm_network *hsl_gsmnet;
160
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200161static int bts_model_hslfemto_start(struct gsm_network *net)
Harald Weltefd355a32011-03-04 13:41:31 +0100162{
163 model_hslfemto.features.data = &model_hslfemto._features_data[0];
164 model_hslfemto.features.data_len = sizeof(model_hslfemto._features_data);
165
166 gsm_btsmodel_set_feature(&model_hslfemto, BTS_FEAT_GPRS);
167 gsm_btsmodel_set_feature(&model_hslfemto, BTS_FEAT_EGPRS);
168
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200169 osmo_signal_register_handler(SS_L_INPUT, inp_sig_cb, NULL);
Harald Weltefd355a32011-03-04 13:41:31 +0100170
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200171 hsl_gsmnet = net;
172 return 0;
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200173}
174
175int bts_model_hslfemto_init(void)
176{
Harald Weltefd355a32011-03-04 13:41:31 +0100177 return gsm_bts_model_register(&model_hslfemto);
178}
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200179
180#define OML_UP 0x0001
181#define RSL_UP 0x0002
182
183struct gsm_bts *find_bts_by_serno(struct gsm_network *net, uint64_t serno)
184{
185 struct gsm_bts *bts;
186
187 llist_for_each_entry(bts, &net->bts_list, list) {
188 if (bts->type != GSM_BTS_TYPE_HSL_FEMTO)
189 continue;
190
191 if (serno == bts->hsl.serno)
192 return bts;
193 }
194 return NULL;
195}
196
197/* This function is called once the OML/RSL link becomes up. */
198static struct e1inp_sign_link *
199hsl_sign_link_up(void *unit_data, struct e1inp_line *line,
200 enum e1inp_sign_type type)
201{
202 struct hsl_unit *dev = unit_data;
203 struct gsm_bts *bts;
204
205 bts = find_bts_by_serno(hsl_gsmnet, dev->serno);
206 if (!bts) {
207 LOGP(DLINP, LOGL_ERROR, "Unable to find BTS config for "
Holger Hans Peter Freyther4dd84ff2012-09-11 12:33:51 +0200208 "serial number %"PRIx64"\n", dev->serno);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200209 return NULL;
210 }
Holger Hans Peter Freyther4dd84ff2012-09-11 12:33:51 +0200211 DEBUGP(DLINP, "Identified HSL BTS Serial Number %"PRIx64"\n", dev->serno);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200212
213 /* we shouldn't hardcode it, but HSL femto also hardcodes it... */
214 bts->oml_tei = 255;
215 bts->c0->rsl_tei = 0;
216 bts->oml_link = e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML-1],
217 E1INP_SIGN_OML, bts->c0,
218 bts->oml_tei, 0);
219 bts->c0->rsl_link = e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML-1],
220 E1INP_SIGN_RSL, bts->c0,
221 bts->c0->rsl_tei, 0);
222 e1inp_event(&line->ts[E1INP_SIGN_OML-1], S_L_INP_TEI_UP, 255, 0);
223 e1inp_event(&line->ts[E1INP_SIGN_OML-1], S_L_INP_TEI_UP, 0, 0);
224 bts->ip_access.flags |= OML_UP;
225 bts->ip_access.flags |= (RSL_UP << 0);
226
227 return bts->oml_link;
228}
229
230void hsl_drop_oml(struct gsm_bts *bts)
231{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200232 if (!bts->oml_link)
233 return;
234
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200235 e1inp_sign_link_destroy(bts->oml_link);
236 bts->oml_link = NULL;
237
238 e1inp_sign_link_destroy(bts->c0->rsl_link);
239 bts->c0->rsl_link = NULL;
240
241 bts->ip_access.flags = 0;
242}
243
244static void hsl_sign_link_down(struct e1inp_line *line)
245{
246 /* No matter what link went down, we close both signal links. */
247 struct e1inp_ts *ts = &line->ts[E1INP_SIGN_OML-1];
248 struct e1inp_sign_link *link;
249
250 llist_for_each_entry(link, &ts->sign.sign_links, list) {
251 struct gsm_bts *bts = link->trx->bts;
252
253 hsl_drop_oml(bts);
254 /* Yes, we only use the first element of the list. */
255 break;
256 }
257}
258
259/* This function is called if we receive one OML/RSL message. */
260static int hsl_sign_link(struct msgb *msg)
261{
262 int ret = 0;
263 struct e1inp_sign_link *link = msg->dst;
264 struct e1inp_ts *e1i_ts = link->ts;
265
266 switch (link->type) {
267 case E1INP_SIGN_OML:
268 if (!(link->trx->bts->ip_access.flags & OML_UP)) {
269 e1inp_event(e1i_ts, S_L_INP_TEI_UP,
270 link->tei, link->sapi);
271 link->trx->bts->ip_access.flags |= OML_UP;
272 }
273 ret = abis_nm_rcvmsg(msg);
274 break;
275 case E1INP_SIGN_RSL:
276 if (!(link->trx->bts->ip_access.flags &
277 (RSL_UP << link->trx->nr))) {
278 e1inp_event(e1i_ts, S_L_INP_TEI_UP,
279 link->tei, link->sapi);
280 link->trx->bts->ip_access.flags |=
281 (RSL_UP << link->trx->nr);
282 }
283 ret = abis_rsl_rcvmsg(msg);
284 break;
285 default:
286 LOGP(DLINP, LOGL_ERROR, "Unknown signal link type %d\n",
287 link->type);
288 msgb_free(msg);
289 break;
290 }
291 return ret;
292}
293
294static struct e1inp_line_ops hsl_e1inp_line_ops = {
295 .cfg = {
296 .ipa = {
297 .addr = "0.0.0.0",
298 .role = E1INP_LINE_R_BSC,
299 },
300 },
301 .sign_link_up = hsl_sign_link_up,
302 .sign_link_down = hsl_sign_link_down,
303 .sign_link = hsl_sign_link,
304};
305
306static void bts_model_hslfemto_e1line_bind_ops(struct e1inp_line *line)
307{
308 e1inp_line_bind_ops(line, &hsl_e1inp_line_ops);
309}