blob: 37b7f789f5b34802100978ecd070a216a9720011 [file] [log] [blame]
Yves Godin660709d2016-05-19 11:08:03 +02001/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
2 * based on:
3 * femto_l1_if.c
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21#include <string.h>
22#include <errno.h>
23
24#include <nrw/litecell15/litecell15.h>
25#include <nrw/litecell15/gsml1prim.h>
26#include <nrw/litecell15/gsml1const.h>
27#include <nrw/litecell15/gsml1types.h>
28
29#include <osmocom/core/gsmtap.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/timer.h>
Max84bf0fa2017-09-01 11:02:40 +020032#include <osmocom/gsm/protocol/gsm_04_08.h>
33
Yves Godin660709d2016-05-19 11:08:03 +020034#include <lc15_l1_if.h>
35#include <gprs_debug.h>
36#include <pcu_l1_if.h>
Maxd71e8b32016-09-19 16:17:06 +020037#include <bts.h>
Yves Godin660709d2016-05-19 11:08:03 +020038
39extern void *tall_pcu_ctx;
40
41uint32_t l1if_ts_to_hLayer2(uint8_t trx, uint8_t ts)
42{
43 return (ts << 16) | (trx << 24);
44}
45
46/* allocate a msgb containing a GsmL1_Prim_t */
47struct msgb *l1p_msgb_alloc(void)
48{
49 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
50
51 if (msg)
52 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
53
54 return msg;
55}
56
57static int l1if_req_pdch(struct lc15l1_hdl *fl1h, struct msgb *msg)
58{
59 struct osmo_wqueue *wqueue = &fl1h->write_q[MQ_PDTCH_WRITE];
60
61 if (osmo_wqueue_enqueue(wqueue, msg) != 0) {
62 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
63 msgb_free(msg);
64 }
65
66 return 0;
67}
68
69static void *prim_init(GsmL1_Prim_t *prim, GsmL1_PrimId_t id, struct lc15l1_hdl *gl1)
70{
71 prim->id = id;
72
73 switch (id) {
74 case GsmL1_PrimId_MphInitReq:
75 //prim->u.mphInitReq.hLayer1 = (HANDLE)gl1->hLayer1;
76 break;
77 case GsmL1_PrimId_MphCloseReq:
78 prim->u.mphCloseReq.hLayer1 = (HANDLE)gl1->hLayer1;
79 break;
80 case GsmL1_PrimId_MphConnectReq:
81 prim->u.mphConnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
82 break;
83 case GsmL1_PrimId_MphDisconnectReq:
84 prim->u.mphDisconnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
85 break;
86 case GsmL1_PrimId_MphActivateReq:
87 prim->u.mphActivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
88 break;
89 case GsmL1_PrimId_MphDeactivateReq:
90 prim->u.mphDeactivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
91 break;
92 case GsmL1_PrimId_MphConfigReq:
93 prim->u.mphConfigReq.hLayer1 = (HANDLE)gl1->hLayer1;
94 break;
95 case GsmL1_PrimId_MphMeasureReq:
96 prim->u.mphMeasureReq.hLayer1 = (HANDLE)gl1->hLayer1;
97 break;
98 case GsmL1_PrimId_MphInitCnf:
99 case GsmL1_PrimId_MphCloseCnf:
100 case GsmL1_PrimId_MphConnectCnf:
101 case GsmL1_PrimId_MphDisconnectCnf:
102 case GsmL1_PrimId_MphActivateCnf:
103 case GsmL1_PrimId_MphDeactivateCnf:
104 case GsmL1_PrimId_MphConfigCnf:
105 case GsmL1_PrimId_MphMeasureCnf:
106 break;
107 case GsmL1_PrimId_MphTimeInd:
108 break;
109 case GsmL1_PrimId_MphSyncInd:
110 break;
111 case GsmL1_PrimId_PhEmptyFrameReq:
112 prim->u.phEmptyFrameReq.hLayer1 = (HANDLE)gl1->hLayer1;
113 break;
114 case GsmL1_PrimId_PhDataReq:
115 prim->u.phDataReq.hLayer1 = (HANDLE)gl1->hLayer1;
116 break;
117 case GsmL1_PrimId_PhConnectInd:
118 break;
119 case GsmL1_PrimId_PhReadyToSendInd:
120 break;
121 case GsmL1_PrimId_PhDataInd:
122 break;
123 case GsmL1_PrimId_PhRaInd:
124 break;
125 default:
126 LOGP(DL1IF, LOGL_ERROR, "unknown L1 primitive %u\n", id);
127 break;
128 }
129 return &prim->u;
130}
131
Yves Godin660709d2016-05-19 11:08:03 +0200132/* connect PDTCH */
133int l1if_connect_pdch(void *obj, uint8_t ts)
134{
135 struct lc15l1_hdl *fl1h = obj;
136 struct msgb *msg = l1p_msgb_alloc();
137 GsmL1_MphConnectReq_t *cr;
138
139 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
140 cr->u8Tn = ts;
141 cr->logChComb = GsmL1_LogChComb_XIII;
142
143 return l1if_req_pdch(fl1h, msg);
144}
145
146static int handle_ph_readytosend_ind(struct lc15l1_hdl *fl1h,
147 GsmL1_PhReadyToSendInd_t *rts_ind)
148{
149 struct gsm_time g_time;
150 int rc = 0;
151
152 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
153
154 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
155 g_time.t1, g_time.t2, g_time.t3,
156 get_value_string(lc15bts_l1sapi_names, rts_ind->sapi));
157
158 switch (rts_ind->sapi) {
159 case GsmL1_Sapi_Pdtch:
160 case GsmL1_Sapi_Pacch:
161 rc = pcu_rx_rts_req_pdtch(fl1h->trx_no, rts_ind->u8Tn,
Max79cb2452016-08-09 19:21:34 +0200162 rts_ind->u32Fn, rts_ind->u8BlockNbr);
Yves Godin660709d2016-05-19 11:08:03 +0200163 case GsmL1_Sapi_Ptcch:
164 // FIXME
165 default:
166 break;
167 }
168
169 return rc;
170}
171
172static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
173{
174 meas->rssi = (int8_t) (l1_meas->fRssi);
175 meas->have_rssi = 1;
176 meas->ber = (uint8_t) (l1_meas->fBer * 100);
177 meas->have_ber = 1;
178 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
179 meas->have_bto = 1;
180 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
181 meas->have_link_qual = 1;
182}
183
184static int handle_ph_data_ind(struct lc15l1_hdl *fl1h,
185 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
186{
187 int rc = 0;
188 struct pcu_l1_meas meas = {0};
189
190 DEBUGP(DL1IF, "Rx PH-DATA.ind %s (hL2 %08x): %s\n",
191 get_value_string(lc15bts_l1sapi_names, data_ind->sapi),
192 data_ind->hLayer2,
193 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
194 data_ind->msgUnitParam.u8Size));
195
196 /*
197 * TODO: Add proper bad frame handling here. This could be used
198 * to switch the used CS. Avoid a crash with the PCU right now
199 * feed "0 - 1" amount of data.
200 */
201 if (data_ind->msgUnitParam.u8Size == 0)
202 return -1;
203
Yves Godin660709d2016-05-19 11:08:03 +0200204 get_meas(&meas, &data_ind->measParam);
Maxd71e8b32016-09-19 16:17:06 +0200205 bts_update_tbf_ta("PH-DATA", data_ind->u32Fn, fl1h->trx_no,
206 data_ind->u8Tn, qta2ta(meas.bto));
Yves Godin660709d2016-05-19 11:08:03 +0200207
208 switch (data_ind->sapi) {
209 case GsmL1_Sapi_Pdtch:
210 case GsmL1_Sapi_Pacch:
211 /* drop incomplete UL block */
212 if (data_ind->msgUnitParam.u8Buffer[0]
213 != GsmL1_PdtchPlType_Full)
214 break;
215 /* PDTCH / PACCH frame handling */
Harald Welte717cdf52017-07-21 21:56:23 +0200216 rc = pcu_rx_data_ind_pdtch(fl1h->trx_no, data_ind->u8Tn,
Yves Godin660709d2016-05-19 11:08:03 +0200217 data_ind->msgUnitParam.u8Buffer + 1,
218 data_ind->msgUnitParam.u8Size - 1,
219 data_ind->u32Fn,
220 &meas);
221 break;
222 case GsmL1_Sapi_Ptcch:
223 // FIXME
Harald Welte717cdf52017-07-21 21:56:23 +0200224 rc = -1;
Yves Godin660709d2016-05-19 11:08:03 +0200225 break;
226 default:
227 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %s\n",
228 get_value_string(lc15bts_l1sapi_names, data_ind->sapi));
229 break;
230 }
231
Harald Welte717cdf52017-07-21 21:56:23 +0200232 if (rc < 0) {
233 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
234 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
235 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
236 data_ind->msgUnitParam.u8Size-1);
237 }
238
Yves Godin660709d2016-05-19 11:08:03 +0200239 return rc;
240}
241
242#define MIN_QUAL_RACH 5.0f
243
244static int handle_ph_ra_ind(struct lc15l1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
245{
Yves Godin660709d2016-05-19 11:08:03 +0200246 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
247 return 0;
248
249 DEBUGP(DL1IF, "Rx PH-RA.ind");
Maxd71e8b32016-09-19 16:17:06 +0200250 bts_update_tbf_ta("PH-RA", ra_ind->u32Fn, fl1h->trx_no, ra_ind->u8Tn,
251 qta2ta(ra_ind->measParam.i16BurstTiming));
Yves Godin660709d2016-05-19 11:08:03 +0200252
253 return 0;
254}
255
256
257/* handle any random indication from the L1 */
258int l1if_handle_l1prim(int wq, struct lc15l1_hdl *fl1h, struct msgb *msg)
259{
260 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
261 int rc = 0;
262
263 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
264 get_value_string(lc15bts_l1prim_names, l1p->id), wq);
265
266 switch (l1p->id) {
267#if 0
268 case GsmL1_PrimId_MphTimeInd:
269 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
270 break;
271 case GsmL1_PrimId_MphSyncInd:
272 break;
273 case GsmL1_PrimId_PhConnectInd:
274 break;
275#endif
276 case GsmL1_PrimId_PhReadyToSendInd:
277 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
278 break;
279 case GsmL1_PrimId_PhDataInd:
280 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
281 break;
282 case GsmL1_PrimId_PhRaInd:
283 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
284 break;
285 default:
286 break;
287 }
288
289 msgb_free(msg);
290
291 return rc;
292}
293
294int l1if_handle_sysprim(struct lc15l1_hdl *fl1h, struct msgb *msg)
295{
296 return -ENOTSUP;
297}
298
299/* send packet data request to L1 */
300int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
301 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
302{
303 struct lc15l1_hdl *fl1h = obj;
304 struct msgb *msg;
305 GsmL1_Prim_t *l1p;
306 GsmL1_PhDataReq_t *data_req;
307 GsmL1_MsgUnitParam_t *msu_param;
308 struct gsm_time g_time;
309
310 gsm_fn2gsmtime(&g_time, fn);
311
312 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
313 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
314 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
315
316 msg = l1p_msgb_alloc();
317 l1p = msgb_l1prim(msg);
318 l1p->id = GsmL1_PrimId_PhDataReq;
319 data_req = &l1p->u.phDataReq;
320 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
321 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
322 data_req->subCh = GsmL1_SubCh_NA;
323 data_req->u8BlockNbr = block_nr;
324 data_req->u8Tn = ts;
325 data_req->u32Fn = fn;
326 msu_param = &data_req->msgUnitParam;
327 msu_param->u8Size = len;
328 memcpy(msu_param->u8Buffer, data, len);
329
Yves Godin660709d2016-05-19 11:08:03 +0200330 /* transmit */
331 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
332 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
333 msgb_free(msg);
334 }
335
336 return 0;
337}
338
339void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1)
340{
341 struct lc15l1_hdl *fl1h;
342 int rc;
343
344 fl1h = talloc_zero(tall_pcu_ctx, struct lc15l1_hdl);
345 if (!fl1h)
346 return NULL;
347
348 fl1h->hLayer1 = hlayer1;
349 fl1h->trx_no = trx_no;
Minh-Quang Nguyen16ddc902016-09-06 10:27:11 -0400350 /* hardware queues are numbered starting from 0 */
351 fl1h->hw_info.trx_nr = trx_no;
Yves Godin660709d2016-05-19 11:08:03 +0200352
353 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
354
355 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
356 if (rc < 0) {
357 talloc_free(fl1h);
358 return NULL;
359 }
360
361 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
362 if (fl1h->gsmtap)
363 gsmtap_source_add_sink(fl1h->gsmtap);
364
365 return fl1h;
366}
367
368int l1if_close_pdch(void *obj)
369{
370 struct lc15l1_hdl *fl1h = obj;
371 if (fl1h)
372 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
373 talloc_free(fl1h);
374 return 0;
375}
376