blob: ebab34aebcc9e8d884d895a43364f96a5782d581 [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;
101 uint8_t *cur;
102
103 msg = hsl_alloc_msgb();
104 cur = msgb_put(msg, sizeof(l1_msg));
105 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();
111 cur = msgb_put(msg, sizeof(conn_trau_msg));
112 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();
117 cur = msgb_put(msg, sizeof(conn_trau_msg2));
118 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();
126 cur = msgb_put(msg, sizeof(oml_arfcn_bsic));
127 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 "
208 "serial number %lx\n", dev->serno);
209 return NULL;
210 }
211 DEBUGP(DLINP, "Identified HSL BTS Serial Number %lx\n", dev->serno);
212
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{
232 struct e1inp_line *line;
233
234 if (!bts->oml_link)
235 return;
236
237 line = bts->oml_link->ts->line;
238 e1inp_sign_link_destroy(bts->oml_link);
239 bts->oml_link = NULL;
240
241 e1inp_sign_link_destroy(bts->c0->rsl_link);
242 bts->c0->rsl_link = NULL;
243
244 bts->ip_access.flags = 0;
245}
246
247static void hsl_sign_link_down(struct e1inp_line *line)
248{
249 /* No matter what link went down, we close both signal links. */
250 struct e1inp_ts *ts = &line->ts[E1INP_SIGN_OML-1];
251 struct e1inp_sign_link *link;
252
253 llist_for_each_entry(link, &ts->sign.sign_links, list) {
254 struct gsm_bts *bts = link->trx->bts;
255
256 hsl_drop_oml(bts);
257 /* Yes, we only use the first element of the list. */
258 break;
259 }
260}
261
262/* This function is called if we receive one OML/RSL message. */
263static int hsl_sign_link(struct msgb *msg)
264{
265 int ret = 0;
266 struct e1inp_sign_link *link = msg->dst;
267 struct e1inp_ts *e1i_ts = link->ts;
268
269 switch (link->type) {
270 case E1INP_SIGN_OML:
271 if (!(link->trx->bts->ip_access.flags & OML_UP)) {
272 e1inp_event(e1i_ts, S_L_INP_TEI_UP,
273 link->tei, link->sapi);
274 link->trx->bts->ip_access.flags |= OML_UP;
275 }
276 ret = abis_nm_rcvmsg(msg);
277 break;
278 case E1INP_SIGN_RSL:
279 if (!(link->trx->bts->ip_access.flags &
280 (RSL_UP << link->trx->nr))) {
281 e1inp_event(e1i_ts, S_L_INP_TEI_UP,
282 link->tei, link->sapi);
283 link->trx->bts->ip_access.flags |=
284 (RSL_UP << link->trx->nr);
285 }
286 ret = abis_rsl_rcvmsg(msg);
287 break;
288 default:
289 LOGP(DLINP, LOGL_ERROR, "Unknown signal link type %d\n",
290 link->type);
291 msgb_free(msg);
292 break;
293 }
294 return ret;
295}
296
297static struct e1inp_line_ops hsl_e1inp_line_ops = {
298 .cfg = {
299 .ipa = {
300 .addr = "0.0.0.0",
301 .role = E1INP_LINE_R_BSC,
302 },
303 },
304 .sign_link_up = hsl_sign_link_up,
305 .sign_link_down = hsl_sign_link_down,
306 .sign_link = hsl_sign_link,
307};
308
309static void bts_model_hslfemto_e1line_bind_ops(struct e1inp_line *line)
310{
311 e1inp_line_bind_ops(line, &hsl_e1inp_line_ops);
312}