blob: a02e9629d7378dae9e417a3388442ff8877d068e [file] [log] [blame]
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -04001/* 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 "oc2g_l1_if.h"
22
23#include <string.h>
24#include <errno.h>
25
26#include <nrw/oc2g/oc2g.h>
27#include <nrw/oc2g/gsml1prim.h>
28#include <nrw/oc2g/gsml1const.h>
29#include <nrw/oc2g/gsml1types.h>
30
31#include <osmocom/core/gsmtap.h>
32#include <osmocom/core/talloc.h>
33#include <osmocom/core/timer.h>
Minh-Quang Nguyen4a1796f2017-09-29 08:54:16 -040034#include <osmocom/gsm/protocol/gsm_04_08.h>
35
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -040036#include <gprs_debug.h>
37#include <pcu_l1_if.h>
38#include <osmocom/pcu/pcuif_proto.h>
39#include <bts.h>
40
41extern void *tall_pcu_ctx;
42
43uint32_t l1if_ts_to_hLayer2(uint8_t trx, uint8_t ts)
44{
45 return (ts << 16) | (trx << 24);
46}
47
48/* allocate a msgb containing a GsmL1_Prim_t */
49struct msgb *l1p_msgb_alloc(void)
50{
51 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
52
53 if (msg)
54 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
55
56 return msg;
57}
58
59static int l1if_req_pdch(struct oc2gl1_hdl *fl1h, struct msgb *msg)
60{
61 struct osmo_wqueue *wqueue = &fl1h->write_q[MQ_PDTCH_WRITE];
62
63 if (osmo_wqueue_enqueue(wqueue, msg) != 0) {
64 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
65 msgb_free(msg);
66 }
67
68 return 0;
69}
70
71static void *prim_init(GsmL1_Prim_t *prim, GsmL1_PrimId_t id, struct oc2gl1_hdl *gl1)
72{
73 prim->id = id;
74
75 switch (id) {
76 case GsmL1_PrimId_MphInitReq:
77 //prim->u.mphInitReq.hLayer1 = (HANDLE)gl1->hLayer1;
78 break;
79 case GsmL1_PrimId_MphCloseReq:
80 prim->u.mphCloseReq.hLayer1 = (HANDLE)gl1->hLayer1;
81 break;
82 case GsmL1_PrimId_MphConnectReq:
83 prim->u.mphConnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
84 break;
85 case GsmL1_PrimId_MphDisconnectReq:
86 prim->u.mphDisconnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
87 break;
88 case GsmL1_PrimId_MphActivateReq:
89 prim->u.mphActivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
90 break;
91 case GsmL1_PrimId_MphDeactivateReq:
92 prim->u.mphDeactivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
93 break;
94 case GsmL1_PrimId_MphConfigReq:
95 prim->u.mphConfigReq.hLayer1 = (HANDLE)gl1->hLayer1;
96 break;
97 case GsmL1_PrimId_MphMeasureReq:
98 prim->u.mphMeasureReq.hLayer1 = (HANDLE)gl1->hLayer1;
99 break;
100 case GsmL1_PrimId_MphInitCnf:
101 case GsmL1_PrimId_MphCloseCnf:
102 case GsmL1_PrimId_MphConnectCnf:
103 case GsmL1_PrimId_MphDisconnectCnf:
104 case GsmL1_PrimId_MphActivateCnf:
105 case GsmL1_PrimId_MphDeactivateCnf:
106 case GsmL1_PrimId_MphConfigCnf:
107 case GsmL1_PrimId_MphMeasureCnf:
108 break;
109 case GsmL1_PrimId_MphTimeInd:
110 break;
111 case GsmL1_PrimId_MphSyncInd:
112 break;
113 case GsmL1_PrimId_PhEmptyFrameReq:
114 prim->u.phEmptyFrameReq.hLayer1 = (HANDLE)gl1->hLayer1;
115 break;
116 case GsmL1_PrimId_PhDataReq:
117 prim->u.phDataReq.hLayer1 = (HANDLE)gl1->hLayer1;
118 break;
119 case GsmL1_PrimId_PhConnectInd:
120 break;
121 case GsmL1_PrimId_PhReadyToSendInd:
122 break;
123 case GsmL1_PrimId_PhDataInd:
124 break;
125 case GsmL1_PrimId_PhRaInd:
126 break;
127 default:
128 LOGP(DL1IF, LOGL_ERROR, "unknown L1 primitive %u\n", id);
129 break;
130 }
131 return &prim->u;
132}
133
134/* connect PDTCH */
135int l1if_connect_pdch(void *obj, uint8_t ts)
136{
137 struct oc2gl1_hdl *fl1h = obj;
138 struct msgb *msg = l1p_msgb_alloc();
139 GsmL1_MphConnectReq_t *cr;
140
141 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
142 cr->u8Tn = ts;
143 cr->logChComb = GsmL1_LogChComb_XIII;
144
145 return l1if_req_pdch(fl1h, msg);
146}
147
148static int handle_ph_readytosend_ind(struct oc2gl1_hdl *fl1h,
149 GsmL1_PhReadyToSendInd_t *rts_ind)
150{
151 struct gsm_time g_time;
152 int rc = 0;
153
154 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
155
156 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
157 g_time.t1, g_time.t2, g_time.t3,
158 get_value_string(oc2gbts_l1sapi_names, rts_ind->sapi));
159
160 switch (rts_ind->sapi) {
161 case GsmL1_Sapi_Pdtch:
162 case GsmL1_Sapi_Pacch:
163 rc = pcu_rx_rts_req_pdtch(fl1h->trx_no, rts_ind->u8Tn,
164 rts_ind->u32Fn, rts_ind->u8BlockNbr);
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700165 break;
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400166 case GsmL1_Sapi_Ptcch:
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700167 rc = pcu_rx_rts_req_ptcch(fl1h->trx_no, rts_ind->u8Tn,
168 rts_ind->u32Fn, rts_ind->u8BlockNbr);
169 break;
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400170 default:
171 break;
172 }
173
174 return rc;
175}
176
177static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
178{
179 meas->rssi = (int8_t) (l1_meas->fRssi);
180 meas->have_rssi = 1;
181 meas->ber = (uint8_t) (l1_meas->fBer * 100);
182 meas->have_ber = 1;
183 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
184 meas->have_bto = 1;
185 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
186 meas->have_link_qual = 1;
187}
188
189static int handle_ph_data_ind(struct oc2gl1_hdl *fl1h,
190 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
191{
192 int rc = 0;
193 struct pcu_l1_meas meas = {0};
194
195 DEBUGP(DL1IF, "Rx PH-DATA.ind %s (hL2 %08x): %s\n",
196 get_value_string(oc2gbts_l1sapi_names, data_ind->sapi),
197 data_ind->hLayer2,
198 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
199 data_ind->msgUnitParam.u8Size));
200
201 /*
202 * TODO: Add proper bad frame handling here. This could be used
203 * to switch the used CS. Avoid a crash with the PCU right now
204 * feed "0 - 1" amount of data.
205 */
206 if (data_ind->msgUnitParam.u8Size == 0)
207 return -1;
208
209 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
210 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
211 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
212 data_ind->msgUnitParam.u8Size-1);
213
214 get_meas(&meas, &data_ind->measParam);
215 bts_update_tbf_ta("PH-DATA", data_ind->u32Fn, fl1h->trx_no,
Minh-Quang Nguyen2ba60842017-11-07 15:17:12 -0500216 data_ind->u8Tn, sign_qta2ta(meas.bto), false);
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400217
218 switch (data_ind->sapi) {
219 case GsmL1_Sapi_Pdtch:
220 case GsmL1_Sapi_Pacch:
221 /* drop incomplete UL block */
222 if (data_ind->msgUnitParam.u8Buffer[0]
223 != GsmL1_PdtchPlType_Full)
224 break;
225 /* PDTCH / PACCH frame handling */
226 pcu_rx_data_ind_pdtch(fl1h->trx_no, data_ind->u8Tn,
227 data_ind->msgUnitParam.u8Buffer + 1,
228 data_ind->msgUnitParam.u8Size - 1,
229 data_ind->u32Fn,
230 &meas);
231 break;
232 case GsmL1_Sapi_Ptcch:
233 // FIXME
234 break;
235 default:
236 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-DATA.ind for unknown L1 SAPI %s\n",
237 get_value_string(oc2gbts_l1sapi_names, data_ind->sapi));
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400238 break;
239 }
240
241 return rc;
242}
243
244#define MIN_QUAL_RACH 5.0f
245
246static int handle_ph_ra_ind(struct oc2gl1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
247{
248 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
249 return 0;
250
Minh-Quang Nguyenba66ea42017-11-15 13:08:30 -0500251 LOGP(DL1IF, LOGL_DEBUG, "PH-RA-IND L1 qta=%d\n", ra_ind->measParam.i16BurstTiming);
252
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400253 bts_update_tbf_ta("PH-RA", ra_ind->u32Fn, fl1h->trx_no, ra_ind->u8Tn,
Minh-Quang Nguyenba66ea42017-11-15 13:08:30 -0500254 qta2ta(ra_ind->measParam.i16BurstTiming), true);
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400255
256 return 0;
257}
258
259
260/* handle any random indication from the L1 */
261int l1if_handle_l1prim(int wq, struct oc2gl1_hdl *fl1h, struct msgb *msg)
262{
263 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
264 int rc = 0;
265
266 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
267 get_value_string(oc2gbts_l1prim_names, l1p->id), wq);
268
269 switch (l1p->id) {
270#if 0
271 case GsmL1_PrimId_MphTimeInd:
272 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
273 break;
274 case GsmL1_PrimId_MphSyncInd:
275 break;
276 case GsmL1_PrimId_PhConnectInd:
277 break;
278#endif
279 case GsmL1_PrimId_PhReadyToSendInd:
280 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
281 break;
282 case GsmL1_PrimId_PhDataInd:
283 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
284 break;
285 case GsmL1_PrimId_PhRaInd:
286 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
287 break;
288 default:
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400289 break;
290 }
291
292 msgb_free(msg);
293
294 return rc;
295}
296
297int l1if_handle_sysprim(struct oc2gl1_hdl *fl1h, struct msgb *msg)
298{
299 return -ENOTSUP;
300}
301
302/* send packet data request to L1 */
303int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
304 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
305{
306 struct oc2gl1_hdl *fl1h = obj;
307 struct msgb *msg;
308 GsmL1_Prim_t *l1p;
309 GsmL1_PhDataReq_t *data_req;
310 GsmL1_MsgUnitParam_t *msu_param;
311 struct gsm_time g_time;
312
313 gsm_fn2gsmtime(&g_time, fn);
314
315 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
316 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
317 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
318
319 msg = l1p_msgb_alloc();
320 l1p = msgb_l1prim(msg);
321 l1p->id = GsmL1_PrimId_PhDataReq;
322 data_req = &l1p->u.phDataReq;
323 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
324 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
325 data_req->subCh = GsmL1_SubCh_NA;
326 data_req->u8BlockNbr = block_nr;
327 data_req->u8Tn = ts;
328 data_req->u32Fn = fn;
329 msu_param = &data_req->msgUnitParam;
330 msu_param->u8Size = len;
331 memcpy(msu_param->u8Buffer, data, len);
332
333 gsmtap_send(fl1h->gsmtap, arfcn, data_req->u8Tn, GSMTAP_CHANNEL_PACCH,
334 0, data_req->u32Fn, 0, 0,
335 data_req->msgUnitParam.u8Buffer,
336 data_req->msgUnitParam.u8Size);
337
338
339 /* transmit */
340 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
341 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400342 msgb_free(msg);
343 }
344
345 return 0;
346}
347
348void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1)
349{
350 struct oc2gl1_hdl *fl1h;
351 int rc;
352
353 fl1h = talloc_zero(tall_pcu_ctx, struct oc2gl1_hdl);
354 if (!fl1h)
355 return NULL;
356
357 fl1h->hLayer1 = hlayer1;
358 fl1h->trx_no = trx_no;
359 /* hardware queues are numbered starting from 0 */
360 fl1h->hw_info.trx_nr = trx_no;
361
362 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
363
364 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
365 if (rc < 0) {
Jean-Francois Dionnec1e44902017-06-05 14:44:57 -0400366 talloc_free(fl1h);
367 return NULL;
368 }
369
370 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
371 if (fl1h->gsmtap)
372 gsmtap_source_add_sink(fl1h->gsmtap);
373
374 return fl1h;
375}
376
377int l1if_close_pdch(void *obj)
378{
379 struct oc2gl1_hdl *fl1h = obj;
380 if (fl1h)
381 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
382 talloc_free(fl1h);
383 return 0;
384}
385