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