blob: 2cda5b8a956be6b1d25a7182aa23ac6ed5569944 [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
129struct sapi_dir {
130 GsmL1_Sapi_t sapi;
131 GsmL1_Dir_t dir;
132};
133
134static const struct sapi_dir pdtch_sapis[] = {
135 { GsmL1_Sapi_Pdtch, GsmL1_Dir_TxDownlink },
136 { GsmL1_Sapi_Pdtch, GsmL1_Dir_RxUplink },
137 { GsmL1_Sapi_Ptcch, GsmL1_Dir_TxDownlink },
138 { GsmL1_Sapi_Prach, GsmL1_Dir_RxUplink },
139#if 0
140 { GsmL1_Sapi_Ptcch, GsmL1_Dir_RxUplink },
141 { GsmL1_Sapi_Pacch, GsmL1_Dir_TxDownlink },
142#endif
143};
144
145
146/* connect PDTCH */
147int l1if_connect_pdch(void *obj, uint8_t ts)
148{
149 struct lc15l1_hdl *fl1h = obj;
150 struct msgb *msg = l1p_msgb_alloc();
151 GsmL1_MphConnectReq_t *cr;
152
153 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
154 cr->u8Tn = ts;
155 cr->logChComb = GsmL1_LogChComb_XIII;
156
157 return l1if_req_pdch(fl1h, msg);
158}
159
160static int handle_ph_readytosend_ind(struct lc15l1_hdl *fl1h,
161 GsmL1_PhReadyToSendInd_t *rts_ind)
162{
163 struct gsm_time g_time;
164 int rc = 0;
165
166 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
167
168 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
169 g_time.t1, g_time.t2, g_time.t3,
170 get_value_string(lc15bts_l1sapi_names, rts_ind->sapi));
171
172 switch (rts_ind->sapi) {
173 case GsmL1_Sapi_Pdtch:
174 case GsmL1_Sapi_Pacch:
175 rc = pcu_rx_rts_req_pdtch(fl1h->trx_no, rts_ind->u8Tn,
176 rts_ind->u16Arfcn, rts_ind->u32Fn, rts_ind->u8BlockNbr);
177 case GsmL1_Sapi_Ptcch:
178 // FIXME
179 default:
180 break;
181 }
182
183 return rc;
184}
185
186static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
187{
188 meas->rssi = (int8_t) (l1_meas->fRssi);
189 meas->have_rssi = 1;
190 meas->ber = (uint8_t) (l1_meas->fBer * 100);
191 meas->have_ber = 1;
192 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
193 meas->have_bto = 1;
194 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
195 meas->have_link_qual = 1;
196}
197
198static int handle_ph_data_ind(struct lc15l1_hdl *fl1h,
199 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
200{
201 int rc = 0;
202 struct pcu_l1_meas meas = {0};
203
204 DEBUGP(DL1IF, "Rx PH-DATA.ind %s (hL2 %08x): %s\n",
205 get_value_string(lc15bts_l1sapi_names, data_ind->sapi),
206 data_ind->hLayer2,
207 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
208 data_ind->msgUnitParam.u8Size));
209
210 /*
211 * TODO: Add proper bad frame handling here. This could be used
212 * to switch the used CS. Avoid a crash with the PCU right now
213 * feed "0 - 1" amount of data.
214 */
215 if (data_ind->msgUnitParam.u8Size == 0)
216 return -1;
217
218 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
219 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
220 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
221 data_ind->msgUnitParam.u8Size-1);
222
223 get_meas(&meas, &data_ind->measParam);
224
225 switch (data_ind->sapi) {
226 case GsmL1_Sapi_Pdtch:
227 case GsmL1_Sapi_Pacch:
228 /* drop incomplete UL block */
229 if (data_ind->msgUnitParam.u8Buffer[0]
230 != GsmL1_PdtchPlType_Full)
231 break;
232 /* PDTCH / PACCH frame handling */
233 pcu_rx_data_ind_pdtch(fl1h->trx_no, data_ind->u8Tn,
234 data_ind->msgUnitParam.u8Buffer + 1,
235 data_ind->msgUnitParam.u8Size - 1,
236 data_ind->u32Fn,
237 &meas);
238 break;
239 case GsmL1_Sapi_Ptcch:
240 // FIXME
241 break;
242 default:
243 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %s\n",
244 get_value_string(lc15bts_l1sapi_names, data_ind->sapi));
245 break;
246 }
247
248 return rc;
249}
250
251#define MIN_QUAL_RACH 5.0f
252
253static int handle_ph_ra_ind(struct lc15l1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
254{
255 uint8_t acc_delay;
256
257 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
258 return 0;
259
260 DEBUGP(DL1IF, "Rx PH-RA.ind");
261
262 /* check for under/overflow / sign */
263 if (ra_ind->measParam.i16BurstTiming < 0)
264 acc_delay = 0;
265 else
266 acc_delay = ra_ind->measParam.i16BurstTiming >> 2;
267
268 LOGP(DL1IF, LOGL_NOTICE, "got (P)RACH request, TA = %u (ignored)\n",
269 acc_delay);
270
271#warning "The (P)RACH request is just dropped here"
272
273#if 0
274 if (acc_delay > bts->max_ta) {
275 LOGP(DL1C, LOGL_INFO, "ignoring RACH request %u > max_ta(%u)\n",
276 acc_delay, btsb->max_ta);
277 return 0;
278 }
279#endif
280
281 return 0;
282}
283
284
285/* handle any random indication from the L1 */
286int l1if_handle_l1prim(int wq, struct lc15l1_hdl *fl1h, struct msgb *msg)
287{
288 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
289 int rc = 0;
290
291 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
292 get_value_string(lc15bts_l1prim_names, l1p->id), wq);
293
294 switch (l1p->id) {
295#if 0
296 case GsmL1_PrimId_MphTimeInd:
297 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
298 break;
299 case GsmL1_PrimId_MphSyncInd:
300 break;
301 case GsmL1_PrimId_PhConnectInd:
302 break;
303#endif
304 case GsmL1_PrimId_PhReadyToSendInd:
305 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
306 break;
307 case GsmL1_PrimId_PhDataInd:
308 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
309 break;
310 case GsmL1_PrimId_PhRaInd:
311 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
312 break;
313 default:
314 break;
315 }
316
317 msgb_free(msg);
318
319 return rc;
320}
321
322int l1if_handle_sysprim(struct lc15l1_hdl *fl1h, struct msgb *msg)
323{
324 return -ENOTSUP;
325}
326
327/* send packet data request to L1 */
328int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
329 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
330{
331 struct lc15l1_hdl *fl1h = obj;
332 struct msgb *msg;
333 GsmL1_Prim_t *l1p;
334 GsmL1_PhDataReq_t *data_req;
335 GsmL1_MsgUnitParam_t *msu_param;
336 struct gsm_time g_time;
337
338 gsm_fn2gsmtime(&g_time, fn);
339
340 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
341 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
342 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
343
344 msg = l1p_msgb_alloc();
345 l1p = msgb_l1prim(msg);
346 l1p->id = GsmL1_PrimId_PhDataReq;
347 data_req = &l1p->u.phDataReq;
348 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
349 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
350 data_req->subCh = GsmL1_SubCh_NA;
351 data_req->u8BlockNbr = block_nr;
352 data_req->u8Tn = ts;
353 data_req->u32Fn = fn;
354 msu_param = &data_req->msgUnitParam;
355 msu_param->u8Size = len;
356 memcpy(msu_param->u8Buffer, data, len);
357
358 gsmtap_send(fl1h->gsmtap, arfcn, data_req->u8Tn, GSMTAP_CHANNEL_PACCH,
359 0, data_req->u32Fn, 0, 0,
360 data_req->msgUnitParam.u8Buffer,
361 data_req->msgUnitParam.u8Size);
362
363
364 /* transmit */
365 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
366 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
367 msgb_free(msg);
368 }
369
370 return 0;
371}
372
373void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1)
374{
375 struct lc15l1_hdl *fl1h;
376 int rc;
377
378 fl1h = talloc_zero(tall_pcu_ctx, struct lc15l1_hdl);
379 if (!fl1h)
380 return NULL;
381
382 fl1h->hLayer1 = hlayer1;
383 fl1h->trx_no = trx_no;
384 /* hardware queues are numbered starting from 1 */
385 fl1h->hw_info.trx_nr = trx_no + 1;
386
387 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
388
389 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
390 if (rc < 0) {
391 talloc_free(fl1h);
392 return NULL;
393 }
394
395 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
396 if (fl1h->gsmtap)
397 gsmtap_source_add_sink(fl1h->gsmtap);
398
399 return fl1h;
400}
401
402int l1if_close_pdch(void *obj)
403{
404 struct lc15l1_hdl *fl1h = obj;
405 if (fl1h)
406 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
407 talloc_free(fl1h);
408 return 0;
409}
410