blob: 580cdf093911127d2108c2642bc914b0b045f564 [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>
Philipp Maier84ece232023-02-15 15:15:31 +010037#include <pcu_l1_if_phy.h>
Max6dc90b82018-02-19 17:17:28 +010038#include <pdch.h>
Maxd71e8b32016-09-19 16:17:06 +020039#include <bts.h>
Yves Godin660709d2016-05-19 11:08:03 +020040
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 lc15l1_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 lc15l1_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
Yves Godin660709d2016-05-19 11:08:03 +0200134/* connect PDTCH */
135int l1if_connect_pdch(void *obj, uint8_t ts)
136{
137 struct lc15l1_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
Philipp Maier72ed3332023-02-27 15:32:00 +0100148int l1if_disconnect_pdch(void *obj, uint8_t ts)
149{
150 return 0;
151}
152
Yves Godin660709d2016-05-19 11:08:03 +0200153static int handle_ph_readytosend_ind(struct lc15l1_hdl *fl1h,
154 GsmL1_PhReadyToSendInd_t *rts_ind)
155{
156 struct gsm_time g_time;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100157 struct gprs_rlcmac_bts *bts;
Yves Godin660709d2016-05-19 11:08:03 +0200158 int rc = 0;
159
160 gsm_fn2gsmtime(&g_time, rts_ind->u32Fn);
161
162 DEBUGP(DL1IF, "Rx PH-RTS.ind %02u/%02u/%02u SAPI=%s\n",
163 g_time.t1, g_time.t2, g_time.t3,
164 get_value_string(lc15bts_l1sapi_names, rts_ind->sapi));
165
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100166 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
167
168
Yves Godin660709d2016-05-19 11:08:03 +0200169 switch (rts_ind->sapi) {
170 case GsmL1_Sapi_Pdtch:
171 case GsmL1_Sapi_Pacch:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100172 rc = pcu_rx_rts_req_pdtch(bts, fl1h->trx_no, rts_ind->u8Tn,
Max79cb2452016-08-09 19:21:34 +0200173 rts_ind->u32Fn, rts_ind->u8BlockNbr);
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700174 break;
Yves Godin660709d2016-05-19 11:08:03 +0200175 case GsmL1_Sapi_Ptcch:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100176 rc = pcu_rx_rts_req_ptcch(bts, fl1h->trx_no, rts_ind->u8Tn,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700177 rts_ind->u32Fn, rts_ind->u8BlockNbr);
178 break;
Yves Godin660709d2016-05-19 11:08:03 +0200179 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;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100202 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100203 struct gprs_rlcmac_pdch *pdch;
Yves Godin660709d2016-05-19 11:08:03 +0200204 struct pcu_l1_meas meas = {0};
Pau Espin Pedrol81c549d2021-03-08 13:08:06 +0100205 uint8_t *data;
206 uint8_t data_len;
Yves Godin660709d2016-05-19 11:08:03 +0200207
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100208 DEBUGP(DL1IF, "(trx=%" PRIu8 ",ts=%u) FN=%u Rx PH-DATA.ind %s (hL2 %08x): %s\n",
209 fl1h->trx_no, data_ind->u8Tn, data_ind->u32Fn,
Yves Godin660709d2016-05-19 11:08:03 +0200210 get_value_string(lc15bts_l1sapi_names, data_ind->sapi),
211 data_ind->hLayer2,
212 osmo_hexdump(data_ind->msgUnitParam.u8Buffer,
213 data_ind->msgUnitParam.u8Size));
214
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100215 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
216
Yves Godin660709d2016-05-19 11:08:03 +0200217 get_meas(&meas, &data_ind->measParam);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100218 bts_update_tbf_ta(bts, "PH-DATA", data_ind->u32Fn, fl1h->trx_no,
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400219 data_ind->u8Tn, sign_qta2ta(meas.bto), false);
Yves Godin660709d2016-05-19 11:08:03 +0200220
221 switch (data_ind->sapi) {
222 case GsmL1_Sapi_Pdtch:
223 case GsmL1_Sapi_Pacch:
Yves Godin660709d2016-05-19 11:08:03 +0200224 /* PDTCH / PACCH frame handling */
Pau Espin Pedrol81c549d2021-03-08 13:08:06 +0100225 if (data_ind->msgUnitParam.u8Size != 0 &&
Pau Espin Pedrol755a8d62021-03-18 12:39:27 +0100226 data_ind->msgUnitParam.u8Buffer[0] == GsmL1_PdtchPlType_Full) {
Pau Espin Pedrol81c549d2021-03-08 13:08:06 +0100227 data = data_ind->msgUnitParam.u8Buffer + 1;
228 data_len = data_ind->msgUnitParam.u8Size - 1;
229 if (data_len == 0)
230 data = NULL;
231 } else {
232 data = NULL;
233 data_len = 0;
234 }
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100235 pdch = &bts->trx[fl1h->trx_no].pdch[data_ind->u8Tn];
236 pcu_rx_data_ind_pdtch(bts, pdch, data, data_len, data_ind->u32Fn, &meas);
Yves Godin660709d2016-05-19 11:08:03 +0200237 break;
Yves Godin660709d2016-05-19 11:08:03 +0200238 default:
Pau Espin Pedrolc1f31c42021-03-09 14:50:14 +0100239 LOGP(DL1IF, LOGL_NOTICE,
240 "(trx=%" PRIu8 ",ts=%u) FN=%u Rx PH-DATA.ind for unknown L1 SAPI %s\n",
241 fl1h->trx_no, data_ind->u8Tn, data_ind->u32Fn,
242 get_value_string(lc15bts_l1sapi_names, data_ind->sapi));
Yves Godin660709d2016-05-19 11:08:03 +0200243 break;
244 }
245
Harald Welte717cdf52017-07-21 21:56:23 +0200246 if (rc < 0) {
247 gsmtap_send(fl1h->gsmtap, data_ind->u16Arfcn | GSMTAP_ARFCN_F_UPLINK,
248 data_ind->u8Tn, GSMTAP_CHANNEL_PACCH, 0,
249 data_ind->u32Fn, 0, 0, data_ind->msgUnitParam.u8Buffer+1,
250 data_ind->msgUnitParam.u8Size-1);
251 }
252
Yves Godin660709d2016-05-19 11:08:03 +0200253 return rc;
254}
255
256#define MIN_QUAL_RACH 5.0f
257
258static int handle_ph_ra_ind(struct lc15l1_hdl *fl1h, GsmL1_PhRaInd_t *ra_ind)
259{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100260 struct gprs_rlcmac_bts *bts;
Yves Godin660709d2016-05-19 11:08:03 +0200261 if (ra_ind->measParam.fLinkQuality < MIN_QUAL_RACH)
262 return 0;
263
264 DEBUGP(DL1IF, "Rx PH-RA.ind");
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700265
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100266 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
267
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700268 switch (ra_ind->sapi) {
269 case GsmL1_Sapi_Pdtch:
Vadim Yanitskiy811c90c2020-03-30 16:14:07 +0700270 case GsmL1_Sapi_Prach:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100271 bts_update_tbf_ta(bts, "PH-RA", ra_ind->u32Fn, fl1h->trx_no, ra_ind->u8Tn,
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700272 qta2ta(ra_ind->measParam.i16BurstTiming), true);
273 break;
274 case GsmL1_Sapi_Ptcch:
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100275 pcu_rx_rach_ind_ptcch(bts, fl1h->trx_no, ra_ind->u8Tn, ra_ind->u32Fn,
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700276 ra_ind->measParam.i16BurstTiming);
277 break;
278 default:
279 LOGP(DL1IF, LOGL_NOTICE, "Rx PH-RA.ind for unknown L1 SAPI %s\n",
280 get_value_string(lc15bts_l1sapi_names, ra_ind->sapi));
281 return -ENOTSUP;
282 }
Yves Godin660709d2016-05-19 11:08:03 +0200283
284 return 0;
285}
286
287
288/* handle any random indication from the L1 */
289int l1if_handle_l1prim(int wq, struct lc15l1_hdl *fl1h, struct msgb *msg)
290{
291 GsmL1_Prim_t *l1p = msgb_l1prim(msg);
292 int rc = 0;
293
294 LOGP(DL1IF, LOGL_DEBUG, "Rx L1 prim %s on queue %d\n",
295 get_value_string(lc15bts_l1prim_names, l1p->id), wq);
296
297 switch (l1p->id) {
298#if 0
299 case GsmL1_PrimId_MphTimeInd:
300 rc = handle_mph_time_ind(fl1h, &l1p->u.mphTimeInd);
301 break;
302 case GsmL1_PrimId_MphSyncInd:
303 break;
304 case GsmL1_PrimId_PhConnectInd:
305 break;
306#endif
307 case GsmL1_PrimId_PhReadyToSendInd:
308 rc = handle_ph_readytosend_ind(fl1h, &l1p->u.phReadyToSendInd);
309 break;
310 case GsmL1_PrimId_PhDataInd:
311 rc = handle_ph_data_ind(fl1h, &l1p->u.phDataInd, msg);
312 break;
313 case GsmL1_PrimId_PhRaInd:
314 rc = handle_ph_ra_ind(fl1h, &l1p->u.phRaInd);
315 break;
316 default:
317 break;
318 }
319
320 msgb_free(msg);
321
322 return rc;
323}
324
325int l1if_handle_sysprim(struct lc15l1_hdl *fl1h, struct msgb *msg)
326{
327 return -ENOTSUP;
328}
329
330/* send packet data request to L1 */
331int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
332 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len)
333{
334 struct lc15l1_hdl *fl1h = obj;
335 struct msgb *msg;
336 GsmL1_Prim_t *l1p;
337 GsmL1_PhDataReq_t *data_req;
338 GsmL1_MsgUnitParam_t *msu_param;
339 struct gsm_time g_time;
340
341 gsm_fn2gsmtime(&g_time, fn);
342
343 DEBUGP(DL1IF, "TX packet data %02u/%02u/%02u is_ptcch=%d ts=%d "
344 "block_nr=%d, arfcn=%d, len=%d\n", g_time.t1, g_time.t2,
345 g_time.t3, is_ptcch, ts, block_nr, arfcn, len);
346
347 msg = l1p_msgb_alloc();
348 l1p = msgb_l1prim(msg);
349 l1p->id = GsmL1_PrimId_PhDataReq;
350 data_req = &l1p->u.phDataReq;
351 data_req->hLayer1 = (HANDLE)fl1h->hLayer1;
352 data_req->sapi = (is_ptcch) ? GsmL1_Sapi_Ptcch : GsmL1_Sapi_Pdtch;
353 data_req->subCh = GsmL1_SubCh_NA;
354 data_req->u8BlockNbr = block_nr;
355 data_req->u8Tn = ts;
356 data_req->u32Fn = fn;
357 msu_param = &data_req->msgUnitParam;
358 msu_param->u8Size = len;
359 memcpy(msu_param->u8Buffer, data, len);
360
Yves Godin660709d2016-05-19 11:08:03 +0200361 /* transmit */
362 if (osmo_wqueue_enqueue(&fl1h->write_q[MQ_PDTCH_WRITE], msg) != 0) {
363 LOGP(DL1IF, LOGL_ERROR, "PDTCH queue full. dropping message.\n");
364 msgb_free(msg);
365 }
366
367 return 0;
368}
369
Philipp Maier80828f02023-02-15 15:35:33 +0100370void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1, struct gsmtap_inst *gsmtap)
Yves Godin660709d2016-05-19 11:08:03 +0200371{
372 struct lc15l1_hdl *fl1h;
373 int rc;
374
375 fl1h = talloc_zero(tall_pcu_ctx, struct lc15l1_hdl);
376 if (!fl1h)
377 return NULL;
378
379 fl1h->hLayer1 = hlayer1;
380 fl1h->trx_no = trx_no;
Minh-Quang Nguyen16ddc902016-09-06 10:27:11 -0400381 /* hardware queues are numbered starting from 0 */
382 fl1h->hw_info.trx_nr = trx_no;
Yves Godin660709d2016-05-19 11:08:03 +0200383
384 DEBUGP(DL1IF, "PCU: Using TRX HW#%u\n", fl1h->hw_info.trx_nr);
385
386 rc = l1if_transport_open(MQ_PDTCH_WRITE, fl1h);
387 if (rc < 0) {
388 talloc_free(fl1h);
389 return NULL;
390 }
391
392 fl1h->gsmtap = gsmtap_source_init("localhost", GSMTAP_UDP_PORT, 1);
393 if (fl1h->gsmtap)
394 gsmtap_source_add_sink(fl1h->gsmtap);
395
396 return fl1h;
397}
398
399int l1if_close_pdch(void *obj)
400{
401 struct lc15l1_hdl *fl1h = obj;
402 if (fl1h)
403 l1if_transport_close(MQ_PDTCH_WRITE, fl1h);
404 talloc_free(fl1h);
405 return 0;
406}