blob: 218724575e20f29cef78447118d2126f1b2189ff [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
Harald Weltefd355a32011-03-04 13:41:31 +010023
24#include <arpa/inet.h>
25
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010026#include <osmocom/gsm/tlv.h>
Harald Weltefd355a32011-03-04 13:41:31 +010027#include <openbsc/gsm_data.h>
28#include <openbsc/abis_nm.h>
29#include <openbsc/abis_rsl.h>
30#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020031#include <openbsc/debug.h>
32#include <osmocom/core/logging.h>
33#include <osmocom/abis/e1_input.h>
34#include <osmocom/abis/ipaccess.h>
Harald Weltefd355a32011-03-04 13:41:31 +010035
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020036static int bts_model_hslfemto_start(struct gsm_network *net);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020037static void bts_model_hslfemto_e1line_bind_ops(struct e1inp_line *line);
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020038
Harald Weltefd355a32011-03-04 13:41:31 +010039static struct gsm_bts_model model_hslfemto = {
40 .type = GSM_BTS_TYPE_HSL_FEMTO,
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +020041 .start = bts_model_hslfemto_start,
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020042 .e1line_bind_ops = &bts_model_hslfemto_e1line_bind_ops,
Harald Weltefd355a32011-03-04 13:41:31 +010043 .nm_att_tlvdef = {
44 .def = {
45 /* no HSL specific OML attributes that we know of */
46 },
47 },
48};
49
50
51static const uint8_t l1_msg[] = {
Harald Welte388cbbf2011-01-15 18:07:34 +010052#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010053 0x80, 0x8a,
Harald Welte388cbbf2011-01-15 18:07:34 +010054#else
55 0x81, 0x8a,
56#endif
Harald Weltefd355a32011-03-04 13:41:31 +010057 0xC4, 0x0b,
58};
59
60static const uint8_t conn_trau_msg[] = {
Harald Welte388cbbf2011-01-15 18:07:34 +010061#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010062 0x80, 0x81,
Harald Welte388cbbf2011-01-15 18:07:34 +010063#else
64 0x81, 0x81,
65#endif
Harald Weltefd355a32011-03-04 13:41:31 +010066 0xC1, 16,
67 0x02, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0xEA, 0x01,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
69};
70
71static const uint8_t conn_trau_msg2[] = {
Harald Welte388cbbf2011-01-15 18:07:34 +010072#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010073 0x80, 0x81,
Harald Welte388cbbf2011-01-15 18:07:34 +010074#else
75 0x81, 0x81,
76#endif
Harald Weltefd355a32011-03-04 13:41:31 +010077 0xC1, 16,
78 0x02, 0x00, 0xd4, 0x07, 0xC0, 0xA8, 0xEA, 0x01,
79 0x38, 0xA4, 0x45, 0x00, 0x04, 0x59, 0x40, 0x00
80};
81
82static uint8_t oml_arfcn_bsic[] = {
Harald Welte26d79072011-01-14 23:18:59 +010083#ifdef HSL_SR_1_0
Harald Weltefd355a32011-03-04 13:41:31 +010084 0x81, 0x80, 0x00, 10,
Harald Welte26d79072011-01-14 23:18:59 +010085#else
86 0x80, 0x80, 0x00, 10,
87#endif
Harald Weltefd355a32011-03-04 13:41:31 +010088 NM_MT_SET_BTS_ATTR, NM_OC_BTS, 0xff, 0xff, 0xff,
89 NM_ATT_BCCH_ARFCN, 0x03, 0x67,
90 NM_ATT_BSIC, 0x00
91};
92
93static inline struct msgb *hsl_alloc_msgb(void)
94{
95 return msgb_alloc_headroom(1024, 127, "HSL");
96}
97
98static int hslfemto_bootstrap_om(struct gsm_bts *bts)
99{
100 struct msgb *msg;
Harald Weltefd355a32011-03-04 13:41:31 +0100101
102 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200103 msgb_put(msg, sizeof(l1_msg));
Harald Weltefd355a32011-03-04 13:41:31 +0100104 memcpy(msg->data, l1_msg, sizeof(l1_msg));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200105 msg->dst = bts->c0->rsl_link;
Harald Weltefd355a32011-03-04 13:41:31 +0100106 abis_rsl_sendmsg(msg);
107
108#if 1
109 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200110 msgb_put(msg, sizeof(conn_trau_msg));
Harald Weltefd355a32011-03-04 13:41:31 +0100111 memcpy(msg->data, conn_trau_msg, sizeof(conn_trau_msg));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200112 msg->dst = bts->c0->rsl_link;
Harald Weltefd355a32011-03-04 13:41:31 +0100113 abis_rsl_sendmsg(msg);
114#endif
115 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200116 msgb_put(msg, sizeof(conn_trau_msg2));
Harald Weltefd355a32011-03-04 13:41:31 +0100117 memcpy(msg->data, conn_trau_msg2, sizeof(conn_trau_msg2));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200118 msg->dst = bts->c0->rsl_link;
Harald Weltefd355a32011-03-04 13:41:31 +0100119 abis_rsl_sendmsg(msg);
120
121 *((uint16_t *)oml_arfcn_bsic+10) = htons(bts->c0->arfcn);
122 oml_arfcn_bsic[13] = bts->bsic;
123
124 msg = hsl_alloc_msgb();
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200125 msgb_put(msg, sizeof(oml_arfcn_bsic));
Harald Weltefd355a32011-03-04 13:41:31 +0100126 memcpy(msg->data, oml_arfcn_bsic, sizeof(oml_arfcn_bsic));
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200127 msg->dst = bts->c0->rsl_link;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200128 abis_sendmsg(msg);
Harald Weltefd355a32011-03-04 13:41:31 +0100129
130 /* Delay the OPSTART until after SI have been set via RSL */
131 //abis_nm_opstart(bts, NM_OC_BTS, 255, 255, 255);
132
133 return 0;
134}
135
136/* Callback function to be called every time we receive a signal from INPUT */
137static int inp_sig_cb(unsigned int subsys, unsigned int signal,
138 void *handler_data, void *signal_data)
139{
140 struct input_signal_data *isd = signal_data;
141
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200142 if (subsys != SS_L_INPUT)
Harald Weltefd355a32011-03-04 13:41:31 +0100143 return 0;
144
145 switch (signal) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200146 case S_L_INP_TEI_UP:
Harald Weltefd355a32011-03-04 13:41:31 +0100147 switch (isd->link_type) {
148 case E1INP_SIGN_OML:
Harald Welted4571f42011-03-05 16:24:20 +0100149 if (isd->trx->bts->type == GSM_BTS_TYPE_HSL_FEMTO)
150 hslfemto_bootstrap_om(isd->trx->bts);
Harald Weltefd355a32011-03-04 13:41:31 +0100151 break;
152 }
153 }
154
155 return 0;
156}
157
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200158static struct gsm_network *hsl_gsmnet;
159
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200160static int bts_model_hslfemto_start(struct gsm_network *net)
Harald Weltefd355a32011-03-04 13:41:31 +0100161{
162 model_hslfemto.features.data = &model_hslfemto._features_data[0];
163 model_hslfemto.features.data_len = sizeof(model_hslfemto._features_data);
164
165 gsm_btsmodel_set_feature(&model_hslfemto, BTS_FEAT_GPRS);
166 gsm_btsmodel_set_feature(&model_hslfemto, BTS_FEAT_EGPRS);
167
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200168 osmo_signal_register_handler(SS_L_INPUT, inp_sig_cb, NULL);
Harald Weltefd355a32011-03-04 13:41:31 +0100169
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200170 hsl_gsmnet = net;
171 return 0;
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200172}
173
174int bts_model_hslfemto_init(void)
175{
Harald Weltefd355a32011-03-04 13:41:31 +0100176 return gsm_bts_model_register(&model_hslfemto);
177}
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200178
179#define OML_UP 0x0001
180#define RSL_UP 0x0002
181
182struct gsm_bts *find_bts_by_serno(struct gsm_network *net, uint64_t serno)
183{
184 struct gsm_bts *bts;
185
186 llist_for_each_entry(bts, &net->bts_list, list) {
187 if (bts->type != GSM_BTS_TYPE_HSL_FEMTO)
188 continue;
189
190 if (serno == bts->hsl.serno)
191 return bts;
192 }
193 return NULL;
194}
195
196/* This function is called once the OML/RSL link becomes up. */
197static struct e1inp_sign_link *
198hsl_sign_link_up(void *unit_data, struct e1inp_line *line,
199 enum e1inp_sign_type type)
200{
201 struct hsl_unit *dev = unit_data;
202 struct gsm_bts *bts;
203
204 bts = find_bts_by_serno(hsl_gsmnet, dev->serno);
205 if (!bts) {
206 LOGP(DLINP, LOGL_ERROR, "Unable to find BTS config for "
207 "serial number %lx\n", dev->serno);
208 return NULL;
209 }
210 DEBUGP(DLINP, "Identified HSL BTS Serial Number %lx\n", dev->serno);
211
212 /* we shouldn't hardcode it, but HSL femto also hardcodes it... */
213 bts->oml_tei = 255;
214 bts->c0->rsl_tei = 0;
215 bts->oml_link = e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML-1],
216 E1INP_SIGN_OML, bts->c0,
217 bts->oml_tei, 0);
218 bts->c0->rsl_link = e1inp_sign_link_create(&line->ts[E1INP_SIGN_OML-1],
219 E1INP_SIGN_RSL, bts->c0,
220 bts->c0->rsl_tei, 0);
221 e1inp_event(&line->ts[E1INP_SIGN_OML-1], S_L_INP_TEI_UP, 255, 0);
222 e1inp_event(&line->ts[E1INP_SIGN_OML-1], S_L_INP_TEI_UP, 0, 0);
223 bts->ip_access.flags |= OML_UP;
224 bts->ip_access.flags |= (RSL_UP << 0);
225
226 return bts->oml_link;
227}
228
229void hsl_drop_oml(struct gsm_bts *bts)
230{
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200231 if (!bts->oml_link)
232 return;
233
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200234 e1inp_sign_link_destroy(bts->oml_link);
235 bts->oml_link = NULL;
236
237 e1inp_sign_link_destroy(bts->c0->rsl_link);
238 bts->c0->rsl_link = NULL;
239
240 bts->ip_access.flags = 0;
241}
242
243static void hsl_sign_link_down(struct e1inp_line *line)
244{
245 /* No matter what link went down, we close both signal links. */
246 struct e1inp_ts *ts = &line->ts[E1INP_SIGN_OML-1];
247 struct e1inp_sign_link *link;
248
249 llist_for_each_entry(link, &ts->sign.sign_links, list) {
250 struct gsm_bts *bts = link->trx->bts;
251
252 hsl_drop_oml(bts);
253 /* Yes, we only use the first element of the list. */
254 break;
255 }
256}
257
258/* This function is called if we receive one OML/RSL message. */
259static int hsl_sign_link(struct msgb *msg)
260{
261 int ret = 0;
262 struct e1inp_sign_link *link = msg->dst;
263 struct e1inp_ts *e1i_ts = link->ts;
264
265 switch (link->type) {
266 case E1INP_SIGN_OML:
267 if (!(link->trx->bts->ip_access.flags & OML_UP)) {
268 e1inp_event(e1i_ts, S_L_INP_TEI_UP,
269 link->tei, link->sapi);
270 link->trx->bts->ip_access.flags |= OML_UP;
271 }
272 ret = abis_nm_rcvmsg(msg);
273 break;
274 case E1INP_SIGN_RSL:
275 if (!(link->trx->bts->ip_access.flags &
276 (RSL_UP << link->trx->nr))) {
277 e1inp_event(e1i_ts, S_L_INP_TEI_UP,
278 link->tei, link->sapi);
279 link->trx->bts->ip_access.flags |=
280 (RSL_UP << link->trx->nr);
281 }
282 ret = abis_rsl_rcvmsg(msg);
283 break;
284 default:
285 LOGP(DLINP, LOGL_ERROR, "Unknown signal link type %d\n",
286 link->type);
287 msgb_free(msg);
288 break;
289 }
290 return ret;
291}
292
293static struct e1inp_line_ops hsl_e1inp_line_ops = {
294 .cfg = {
295 .ipa = {
296 .addr = "0.0.0.0",
297 .role = E1INP_LINE_R_BSC,
298 },
299 },
300 .sign_link_up = hsl_sign_link_up,
301 .sign_link_down = hsl_sign_link_down,
302 .sign_link = hsl_sign_link,
303};
304
305static void bts_model_hslfemto_e1line_bind_ops(struct e1inp_line *line)
306{
307 e1inp_line_bind_ops(line, &hsl_e1inp_line_ops);
308}