blob: fe9dff1cdbb919612c4f5fe9aa675f39404536f0 [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>
32#include <lc15_l1_if.h>
33#include <gprs_debug.h>
34#include <pcu_l1_if.h>
35
36extern void *tall_pcu_ctx;
37
38uint32_t l1if_ts_to_hLayer2(uint8_t trx, uint8_t ts)
39{
40 return (ts << 16) | (trx << 24);
41}
42
43/* allocate a msgb containing a GsmL1_Prim_t */
44struct msgb *l1p_msgb_alloc(void)
45{
46 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
47
48 if (msg)
49 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
50
51 return msg;
52}
53
54static int l1if_req_pdch(struct lc15l1_hdl *fl1h, struct msgb *msg)
55{
56 struct osmo_wqueue *wqueue = &fl1h->write_q[MQ_PDTCH_WRITE];
57
58 if (osmo_wqueue_enqueue(wqueue, msg) != 0) {
59 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
60 msgb_free(msg);
61 }
62
63 return 0;
64}
65
66static void *prim_init(GsmL1_Prim_t *prim, GsmL1_PrimId_t id, struct lc15l1_hdl *gl1)
67{
68 prim->id = id;
69
70 switch (id) {
71 case GsmL1_PrimId_MphInitReq:
72 //prim->u.mphInitReq.hLayer1 = (HANDLE)gl1->hLayer1;
73 break;
74 case GsmL1_PrimId_MphCloseReq:
75 prim->u.mphCloseReq.hLayer1 = (HANDLE)gl1->hLayer1;
76 break;
77 case GsmL1_PrimId_MphConnectReq:
78 prim->u.mphConnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
79 break;
80 case GsmL1_PrimId_MphDisconnectReq:
81 prim->u.mphDisconnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
82 break;
83 case GsmL1_PrimId_MphActivateReq:
84 prim->u.mphActivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
85 break;
86 case GsmL1_PrimId_MphDeactivateReq:
87 prim->u.mphDeactivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
88 break;
89 case GsmL1_PrimId_MphConfigReq:
90 prim->u.mphConfigReq.hLayer1 = (HANDLE)gl1->hLayer1;
91 break;
92 case GsmL1_PrimId_MphMeasureReq:
93 prim->u.mphMeasureReq.hLayer1 = (HANDLE)gl1->hLayer1;
94 break;
95 case GsmL1_PrimId_MphInitCnf:
96 case GsmL1_PrimId_MphCloseCnf:
97 case GsmL1_PrimId_MphConnectCnf:
98 case GsmL1_PrimId_MphDisconnectCnf:
99 case GsmL1_PrimId_MphActivateCnf:
100 case GsmL1_PrimId_MphDeactivateCnf:
101 case GsmL1_PrimId_MphConfigCnf:
102 case GsmL1_PrimId_MphMeasureCnf:
103 break;
104 case GsmL1_PrimId_MphTimeInd:
105 break;
106 case GsmL1_PrimId_MphSyncInd:
107 break;
108 case GsmL1_PrimId_PhEmptyFrameReq:
109 prim->u.phEmptyFrameReq.hLayer1 = (HANDLE)gl1->hLayer1;
110 break;
111 case GsmL1_PrimId_PhDataReq:
112 prim->u.phDataReq.hLayer1 = (HANDLE)gl1->hLayer1;
113 break;
114 case GsmL1_PrimId_PhConnectInd:
115 break;
116 case GsmL1_PrimId_PhReadyToSendInd:
117 break;
118 case GsmL1_PrimId_PhDataInd:
119 break;
120 case GsmL1_PrimId_PhRaInd:
121 break;
122 default:
123 LOGP(DL1IF, LOGL_ERROR, "unknown L1 primitive %u\n", id);
124 break;
125 }
126 return &prim->u;
127}
128
Yves Godin660709d2016-05-19 11:08:03 +0200129/* connect PDTCH */
130int l1if_connect_pdch(void *obj, uint8_t ts)
131{
132 struct lc15l1_hdl *fl1h = obj;
133 struct msgb *msg = l1p_msgb_alloc();
134 GsmL1_MphConnectReq_t *cr;
135
136 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
137 cr->u8Tn = ts;
138 cr->logChComb = GsmL1_LogChComb_XIII;
139
140 return l1if_req_pdch(fl1h, msg);
141}
142
143static int handle_ph_readytosend_ind(struct lc15l1_hdl *fl1h,
144 GsmL1_PhReadyToSendInd_t *rts_ind)
145{
146 struct gsm_time g_time;
147 int rc = 0;
148
149 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
150
151 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
152 g_time.t1, g_time.t2, g_time.t3,
153 get_value_string(lc15bts_l1sapi_names, rts_ind->sapi));
154
155 switch (rts_ind->sapi) {
156 case GsmL1_Sapi_Pdtch:
157 case GsmL1_Sapi_Pacch:
158 rc = pcu_rx_rts_req_pdtch(fl1h->trx_no, rts_ind->u8Tn,
Max79cb2452016-08-09 19:21:34 +0200159 rts_ind->u32Fn, rts_ind->u8BlockNbr);
Yves Godin660709d2016-05-19 11:08:03 +0200160 case GsmL1_Sapi_Ptcch:
161 // FIXME
162 default:
163 break;
164 }
165
166 return rc;
167}
168
169static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
170{
171 meas->rssi = (int8_t) (l1_meas->fRssi);
172 meas->have_rssi = 1;
173 meas->ber = (uint8_t) (l1_meas->fBer * 100);
174 meas->have_ber = 1;
175 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
176 meas->have_bto = 1;
177 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
178 meas->have_link_qual = 1;
179}
180
181static int handle_ph_data_ind(struct lc15l1_hdl *fl1h,
182 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
183{
184 int rc = 0;
185 struct pcu_l1_meas meas = {0};
186
187 DEBUGP(DL1IF, "Rx PH-DATA.ind %s (hL2 %08x): %s\n",
188 get_value_string(lc15bts_l1sapi_names, data_ind->sapi),
189 data_ind->hLayer2,
190 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
191 data_ind->msgUnitParam.u8Size));
192
193 /*
194 * TODO: Add proper bad frame handling here. This could be used
195 * to switch the used CS. Avoid a crash with the PCU right now
196 * feed "0 - 1" amount of data.
197 */
198 if (data_ind->msgUnitParam.u8Size == 0)
199 return -1;
200
201 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
202 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
203 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
204 data_ind->msgUnitParam.u8Size-1);
205
206 get_meas(&meas, &data_ind->measParam);
207
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 */
216 pcu_rx_data_ind_pdtch(fl1h->trx_no, data_ind->u8Tn,
217 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
224 break;
225 default:
226 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %s\n",
227 get_value_string(lc15bts_l1sapi_names, data_ind->sapi));
228 break;
229 }
230
231 return rc;
232}
233
234#define MIN_QUAL_RACH 5.0f
235
236static int handle_ph_ra_ind(struct lc15l1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
237{
238 uint8_t acc_delay;
239
240 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
241 return 0;
242
243 DEBUGP(DL1IF, "Rx PH-RA.ind");
244
245 /* check for under/overflow / sign */
246 if (ra_ind->measParam.i16BurstTiming < 0)
247 acc_delay = 0;
248 else
249 acc_delay = ra_ind->measParam.i16BurstTiming >> 2;
250
251 LOGP(DL1IF, LOGL_NOTICE, "got (P)RACH request, TA = %u (ignored)\n",
252 acc_delay);
253
254#warning "The (P)RACH request is just dropped here"
255
256#if 0
257 if (acc_delay > bts->max_ta) {
258 LOGP(DL1C, LOGL_INFO, "ignoring RACH request %u > max_ta(%u)\n",
259 acc_delay, btsb->max_ta);
260 return 0;
261 }
262#endif
263
264 return 0;
265}
266
267
268/* handle any random indication from the L1 */
269int l1if_handle_l1prim(int wq, struct lc15l1_hdl *fl1h, struct msgb *msg)
270{
271 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
272 int rc = 0;
273
274 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
275 get_value_string(lc15bts_l1prim_names, l1p->id), wq);
276
277 switch (l1p->id) {
278#if 0
279 case GsmL1_PrimId_MphTimeInd:
280 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
281 break;
282 case GsmL1_PrimId_MphSyncInd:
283 break;
284 case GsmL1_PrimId_PhConnectInd:
285 break;
286#endif
287 case GsmL1_PrimId_PhReadyToSendInd:
288 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
289 break;
290 case GsmL1_PrimId_PhDataInd:
291 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
292 break;
293 case GsmL1_PrimId_PhRaInd:
294 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
295 break;
296 default:
297 break;
298 }
299
300 msgb_free(msg);
301
302 return rc;
303}
304
305int l1if_handle_sysprim(struct lc15l1_hdl *fl1h, struct msgb *msg)
306{
307 return -ENOTSUP;
308}
309
310/* send packet data request to L1 */
311int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
312 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
313{
314 struct lc15l1_hdl *fl1h = obj;
315 struct msgb *msg;
316 GsmL1_Prim_t *l1p;
317 GsmL1_PhDataReq_t *data_req;
318 GsmL1_MsgUnitParam_t *msu_param;
319 struct gsm_time g_time;
320
321 gsm_fn2gsmtime(&g_time, fn);
322
323 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
324 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
325 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
326
327 msg = l1p_msgb_alloc();
328 l1p = msgb_l1prim(msg);
329 l1p->id = GsmL1_PrimId_PhDataReq;
330 data_req = &l1p->u.phDataReq;
331 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
332 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
333 data_req->subCh = GsmL1_SubCh_NA;
334 data_req->u8BlockNbr = block_nr;
335 data_req->u8Tn = ts;
336 data_req->u32Fn = fn;
337 msu_param = &data_req->msgUnitParam;
338 msu_param->u8Size = len;
339 memcpy(msu_param->u8Buffer, data, len);
340
341 gsmtap_send(fl1h->gsmtap, arfcn, data_req->u8Tn, GSMTAP_CHANNEL_PACCH,
342 0, data_req->u32Fn, 0, 0,
343 data_req->msgUnitParam.u8Buffer,
344 data_req->msgUnitParam.u8Size);
345
346
347 /* transmit */
348 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
349 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
350 msgb_free(msg);
351 }
352
353 return 0;
354}
355
356void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1)
357{
358 struct lc15l1_hdl *fl1h;
359 int rc;
360
361 fl1h = talloc_zero(tall_pcu_ctx, struct lc15l1_hdl);
362 if (!fl1h)
363 return NULL;
364
365 fl1h->hLayer1 = hlayer1;
366 fl1h->trx_no = trx_no;
367 /* hardware queues are numbered starting from 1 */
368 fl1h->hw_info.trx_nr = trx_no + 1;
369
370 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
371
372 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
373 if (rc < 0) {
374 talloc_free(fl1h);
375 return NULL;
376 }
377
378 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
379 if (fl1h->gsmtap)
380 gsmtap_source_add_sink(fl1h->gsmtap);
381
382 return fl1h;
383}
384
385int l1if_close_pdch(void *obj)
386{
387 struct lc15l1_hdl *fl1h = obj;
388 if (fl1h)
389 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
390 talloc_free(fl1h);
391 return 0;
392}
393