blob: 13e0fc9208a1fbcc6c1a93b3778e1126ed07a426 [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>
Max6dc90b82018-02-19 17:17:28 +010037#include <pdch.h>
Maxd71e8b32016-09-19 16:17:06 +020038#include <bts.h>
Yves Godin660709d2016-05-19 11:08:03 +020039
40extern void *tall_pcu_ctx;
41
42uint32_t l1if_ts_to_hLayer2(uint8_t trx, uint8_t ts)
43{
44 return (ts << 16) | (trx << 24);
45}
46
47/* allocate a msgb containing a GsmL1_Prim_t */
48struct msgb *l1p_msgb_alloc(void)
49{
50 struct msgb *msg = msgb_alloc(sizeof(GsmL1_Prim_t), "l1_prim");
51
52 if (msg)
53 msg->l1h = msgb_put(msg, sizeof(GsmL1_Prim_t));
54
55 return msg;
56}
57
58static int l1if_req_pdch(struct lc15l1_hdl *fl1h, struct msgb *msg)
59{
60 struct osmo_wqueue *wqueue = &fl1h->write_q[MQ_PDTCH_WRITE];
61
62 if (osmo_wqueue_enqueue(wqueue, msg) != 0) {
63 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
64 msgb_free(msg);
65 }
66
67 return 0;
68}
69
70static void *prim_init(GsmL1_Prim_t *prim, GsmL1_PrimId_t id, struct lc15l1_hdl *gl1)
71{
72 prim->id = id;
73
74 switch (id) {
75 case GsmL1_PrimId_MphInitReq:
76 //prim->u.mphInitReq.hLayer1 = (HANDLE)gl1->hLayer1;
77 break;
78 case GsmL1_PrimId_MphCloseReq:
79 prim->u.mphCloseReq.hLayer1 = (HANDLE)gl1->hLayer1;
80 break;
81 case GsmL1_PrimId_MphConnectReq:
82 prim->u.mphConnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
83 break;
84 case GsmL1_PrimId_MphDisconnectReq:
85 prim->u.mphDisconnectReq.hLayer1 = (HANDLE)gl1->hLayer1;
86 break;
87 case GsmL1_PrimId_MphActivateReq:
88 prim->u.mphActivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
89 break;
90 case GsmL1_PrimId_MphDeactivateReq:
91 prim->u.mphDeactivateReq.hLayer1 = (HANDLE)gl1->hLayer1;
92 break;
93 case GsmL1_PrimId_MphConfigReq:
94 prim->u.mphConfigReq.hLayer1 = (HANDLE)gl1->hLayer1;
95 break;
96 case GsmL1_PrimId_MphMeasureReq:
97 prim->u.mphMeasureReq.hLayer1 = (HANDLE)gl1->hLayer1;
98 break;
99 case GsmL1_PrimId_MphInitCnf:
100 case GsmL1_PrimId_MphCloseCnf:
101 case GsmL1_PrimId_MphConnectCnf:
102 case GsmL1_PrimId_MphDisconnectCnf:
103 case GsmL1_PrimId_MphActivateCnf:
104 case GsmL1_PrimId_MphDeactivateCnf:
105 case GsmL1_PrimId_MphConfigCnf:
106 case GsmL1_PrimId_MphMeasureCnf:
107 break;
108 case GsmL1_PrimId_MphTimeInd:
109 break;
110 case GsmL1_PrimId_MphSyncInd:
111 break;
112 case GsmL1_PrimId_PhEmptyFrameReq:
113 prim->u.phEmptyFrameReq.hLayer1 = (HANDLE)gl1->hLayer1;
114 break;
115 case GsmL1_PrimId_PhDataReq:
116 prim->u.phDataReq.hLayer1 = (HANDLE)gl1->hLayer1;
117 break;
118 case GsmL1_PrimId_PhConnectInd:
119 break;
120 case GsmL1_PrimId_PhReadyToSendInd:
121 break;
122 case GsmL1_PrimId_PhDataInd:
123 break;
124 case GsmL1_PrimId_PhRaInd:
125 break;
126 default:
127 LOGP(DL1IF, LOGL_ERROR, "unknown L1 primitive %u\n", id);
128 break;
129 }
130 return &prim->u;
131}
132
Yves Godin660709d2016-05-19 11:08:03 +0200133/* connect PDTCH */
134int l1if_connect_pdch(void *obj, uint8_t ts)
135{
136 struct lc15l1_hdl *fl1h = obj;
137 struct msgb *msg = l1p_msgb_alloc();
138 GsmL1_MphConnectReq_t *cr;
139
140 cr = prim_init(msgb_l1prim(msg), GsmL1_PrimId_MphConnectReq, fl1h);
141 cr->u8Tn = ts;
142 cr->logChComb = GsmL1_LogChComb_XIII;
143
144 return l1if_req_pdch(fl1h, msg);
145}
146
147static int handle_ph_readytosend_ind(struct lc15l1_hdl *fl1h,
148 GsmL1_PhReadyToSendInd_t *rts_ind)
149{
150 struct gsm_time g_time;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100151 struct gprs_rlcmac_bts *bts;
Yves Godin660709d2016-05-19 11:08:03 +0200152 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(lc15bts_l1sapi_names, rts_ind->sapi));
159
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100160 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
161
162
Yves Godin660709d2016-05-19 11:08:03 +0200163 switch (rts_ind->sapi) {
164 case GsmL1_Sapi_Pdtch:
165 case GsmL1_Sapi_Pacch:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100166 rc = pcu_rx_rts_req_pdtch(bts, fl1h->trx_no, rts_ind->u8Tn,
Max79cb2452016-08-09 19:21:34 +0200167 rts_ind->u32Fn, rts_ind->u8BlockNbr);
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700168 break;
Yves Godin660709d2016-05-19 11:08:03 +0200169 case GsmL1_Sapi_Ptcch:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100170 rc = pcu_rx_rts_req_ptcch(bts, fl1h->trx_no, rts_ind->u8Tn,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700171 rts_ind->u32Fn, rts_ind->u8BlockNbr);
172 break;
Yves Godin660709d2016-05-19 11:08:03 +0200173 default:
174 break;
175 }
176
177 return rc;
178}
179
180static void get_meas(struct pcu_l1_meas *meas, const GsmL1_MeasParam_t *l1_meas)
181{
182 meas->rssi = (int8_t) (l1_meas->fRssi);
183 meas->have_rssi = 1;
184 meas->ber = (uint8_t) (l1_meas->fBer * 100);
185 meas->have_ber = 1;
186 meas->bto = (int16_t) (l1_meas->i16BurstTiming);
187 meas->have_bto = 1;
188 meas->link_qual = (int16_t) (l1_meas->fLinkQuality);
189 meas->have_link_qual = 1;
190}
191
192static int handle_ph_data_ind(struct lc15l1_hdl *fl1h,
193 GsmL1_PhDataInd_t *data_ind, struct msgb *l1p_msg)
194{
195 int rc = 0;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100196 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100197 struct gprs_rlcmac_pdch *pdch;
Yves Godin660709d2016-05-19 11:08:03 +0200198 struct pcu_l1_meas meas = {0};
Pau Espin Pedrol81c549d2021-03-08 13:08:06 +0100199 uint8_t *data;
200 uint8_t data_len;
Yves Godin660709d2016-05-19 11:08:03 +0200201
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100202 DEBUGP(DL1IF, "(trx=%" PRIu8 ",ts=%u) FN=%u Rx PH-DATA.ind %s (hL2 %08x): %s\n",
203 fl1h->trx_no, data_ind->u8Tn, data_ind->u32Fn,
Yves Godin660709d2016-05-19 11:08:03 +0200204 get_value_string(lc15bts_l1sapi_names, data_ind->sapi),
205 data_ind->hLayer2,
206 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
207 data_ind->msgUnitParam.u8Size));
208
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100209 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
210
Yves Godin660709d2016-05-19 11:08:03 +0200211 get_meas(&meas, &data_ind->measParam);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100212 bts_update_tbf_ta(bts, "PH-DATA", data_ind->u32Fn, fl1h->trx_no,
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400213 data_ind->u8Tn, sign_qta2ta(meas.bto), false);
Yves Godin660709d2016-05-19 11:08:03 +0200214
215 switch (data_ind->sapi) {
216 case GsmL1_Sapi_Pdtch:
217 case GsmL1_Sapi_Pacch:
Yves Godin660709d2016-05-19 11:08:03 +0200218 /* PDTCH / PACCH frame handling */
Pau Espin Pedrol81c549d2021-03-08 13:08:06 +0100219 if (data_ind->msgUnitParam.u8Size != 0 &&
Pau Espin Pedrol755a8d62021-03-18 12:39:27 +0100220 data_ind->msgUnitParam.u8Buffer[0] == GsmL1_PdtchPlType_Full) {
Pau Espin Pedrol81c549d2021-03-08 13:08:06 +0100221 data = data_ind->msgUnitParam.u8Buffer + 1;
222 data_len = data_ind->msgUnitParam.u8Size - 1;
223 if (data_len == 0)
224 data = NULL;
225 } else {
226 data = NULL;
227 data_len = 0;
228 }
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100229 pdch = &bts->trx[fl1h->trx_no].pdch[data_ind->u8Tn];
230 pcu_rx_data_ind_pdtch(bts, pdch, data, data_len, data_ind->u32Fn, &meas);
Yves Godin660709d2016-05-19 11:08:03 +0200231 break;
Yves Godin660709d2016-05-19 11:08:03 +0200232 default:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100233 LOGP(DL1IF, LOGL_NOTICE,
234 "(trx=%" PRIu8 ",ts=%u) FN=%u Rx PH-DATA.ind for unknown L1 SAPI %s\n",
235 fl1h->trx_no, data_ind->u8Tn, data_ind->u32Fn,
236 get_value_string(lc15bts_l1sapi_names, data_ind->sapi));
Yves Godin660709d2016-05-19 11:08:03 +0200237 break;
238 }
239
Harald Welte717cdf52017-07-21 21:56:23 +0200240 if (rc < 0) {
241 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
242 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
243 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
244 data_ind->msgUnitParam.u8Size-1);
245 }
246
Yves Godin660709d2016-05-19 11:08:03 +0200247 return rc;
248}
249
250#define MIN_QUAL_RACH 5.0f
251
252static int handle_ph_ra_ind(struct lc15l1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
253{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100254 struct gprs_rlcmac_bts *bts;
Yves Godin660709d2016-05-19 11:08:03 +0200255 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
256 return 0;
257
258 DEBUGP(DL1IF, "Rx PH-RA.ind");
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700259
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100260 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
261
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700262 switch (ra_ind->sapi) {
263 case GsmL1_Sapi_Pdtch:
Vadim Yanitskiy811c90c2020-03-30 16:14:07 +0700264 case GsmL1_Sapi_Prach:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100265 bts_update_tbf_ta(bts, "PH-RA", ra_ind->u32Fn, fl1h->trx_no, ra_ind->u8Tn,
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700266 qta2ta(ra_ind->measParam.i16BurstTiming), true);
267 break;
268 case GsmL1_Sapi_Ptcch:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100269 pcu_rx_rach_ind_ptcch(bts, fl1h->trx_no, ra_ind->u8Tn, ra_ind->u32Fn,
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700270 ra_ind->measParam.i16BurstTiming);
271 break;
272 default:
273 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-RA.ind for unknown L1 SAPI %s\n",
274 get_value_string(lc15bts_l1sapi_names, ra_ind->sapi));
275 return -ENOTSUP;
276 }
Yves Godin660709d2016-05-19 11:08:03 +0200277
278 return 0;
279}
280
281
282/* handle any random indication from the L1 */
283int l1if_handle_l1prim(int wq, struct lc15l1_hdl *fl1h, struct msgb *msg)
284{
285 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
286 int rc = 0;
287
288 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
289 get_value_string(lc15bts_l1prim_names, l1p->id), wq);
290
291 switch (l1p->id) {
292#if 0
293 case GsmL1_PrimId_MphTimeInd:
294 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
295 break;
296 case GsmL1_PrimId_MphSyncInd:
297 break;
298 case GsmL1_PrimId_PhConnectInd:
299 break;
300#endif
301 case GsmL1_PrimId_PhReadyToSendInd:
302 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
303 break;
304 case GsmL1_PrimId_PhDataInd:
305 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
306 break;
307 case GsmL1_PrimId_PhRaInd:
308 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
309 break;
310 default:
311 break;
312 }
313
314 msgb_free(msg);
315
316 return rc;
317}
318
319int l1if_handle_sysprim(struct lc15l1_hdl *fl1h, struct msgb *msg)
320{
321 return -ENOTSUP;
322}
323
324/* send packet data request to L1 */
325int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
326 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
327{
328 struct lc15l1_hdl *fl1h = obj;
329 struct msgb *msg;
330 GsmL1_Prim_t *l1p;
331 GsmL1_PhDataReq_t *data_req;
332 GsmL1_MsgUnitParam_t *msu_param;
333 struct gsm_time g_time;
334
335 gsm_fn2gsmtime(&g_time, fn);
336
337 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
338 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
339 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
340
341 msg = l1p_msgb_alloc();
342 l1p = msgb_l1prim(msg);
343 l1p->id = GsmL1_PrimId_PhDataReq;
344 data_req = &l1p->u.phDataReq;
345 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
346 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
347 data_req->subCh = GsmL1_SubCh_NA;
348 data_req->u8BlockNbr = block_nr;
349 data_req->u8Tn = ts;
350 data_req->u32Fn = fn;
351 msu_param = &data_req->msgUnitParam;
352 msu_param->u8Size = len;
353 memcpy(msu_param->u8Buffer, data, len);
354
Yves Godin660709d2016-05-19 11:08:03 +0200355 /* transmit */
356 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
357 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
358 msgb_free(msg);
359 }
360
361 return 0;
362}
363
364void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1)
365{
366 struct lc15l1_hdl *fl1h;
367 int rc;
368
369 fl1h = talloc_zero(tall_pcu_ctx, struct lc15l1_hdl);
370 if (!fl1h)
371 return NULL;
372
373 fl1h->hLayer1 = hlayer1;
374 fl1h->trx_no = trx_no;
Minh-Quang Nguyen16ddc902016-09-06 10:27:11 -0400375 /* hardware queues are numbered starting from 0 */
376 fl1h->hw_info.trx_nr = trx_no;
Yves Godin660709d2016-05-19 11:08:03 +0200377
378 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
379
380 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
381 if (rc < 0) {
382 talloc_free(fl1h);
383 return NULL;
384 }
385
386 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
387 if (fl1h->gsmtap)
388 gsmtap_source_add_sink(fl1h->gsmtap);
389
390 return fl1h;
391}
392
393int l1if_close_pdch(void *obj)
394{
395 struct lc15l1_hdl *fl1h = obj;
396 if (fl1h)
397 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
398 talloc_free(fl1h);
399 return 0;
400}