blob: 1a47b379b91433a6e9c7258d1c9542fe595cfe9b [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_l1_if.cpp
2 *
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +04003 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040020#include <stdio.h>
21#include <unistd.h>
Pau Espin Pedrol32499b62019-12-09 13:55:12 +010022#include <inttypes.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040023#include <stdlib.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020024#include <string.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040025#include <errno.h>
26#include <assert.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020027#include <sys/socket.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040028#include <sys/un.h>
Andreas Eversberg81a12be2013-03-16 16:16:41 +010029#include <arpa/inet.h>
Pau Espin Pedrol754b0932020-03-26 14:47:13 +010030#include <ctype.h>
Max1187a772018-01-26 13:31:42 +010031
Andreas Eversberg0aed6542012-06-23 10:33:16 +020032extern "C" {
33#include <osmocom/core/talloc.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040034#include <osmocom/core/select.h>
35#include <osmocom/core/msgb.h>
Max528ff392016-02-22 11:42:33 +010036#include <osmocom/core/gsmtap_util.h>
37#include <osmocom/core/gsmtap.h>
Max1187a772018-01-26 13:31:42 +010038#include <osmocom/core/bitvec.h>
Alexander Couzens290d9032020-09-16 21:52:02 +020039#include <osmocom/core/sockaddr_str.h>
Max1187a772018-01-26 13:31:42 +010040#include <osmocom/core/logging.h>
41#include <osmocom/core/utils.h>
Alexander Couzens5bece2a2020-10-12 02:27:22 +020042#include <osmocom/gprs/gprs_ns2.h>
Max1187a772018-01-26 13:31:42 +010043#include <osmocom/gsm/l1sap.h>
44#include <osmocom/gsm/protocol/gsm_04_08.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020045}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040046
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040047#include <gprs_rlcmac.h>
48#include <pcu_l1_if.h>
49#include <gprs_debug.h>
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040050#include <gprs_bssgp_pcu.h>
Harald Welte68fc1272016-11-16 22:48:33 +010051#include <osmocom/pcu/pcuif_proto.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020052#include <bts.h>
Max6dc90b82018-02-19 17:17:28 +010053#include <pdch.h>
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +020054#include <tbf_ul.h>
55#include <tbf_dl.h>
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010056#include <gprs_ms_storage.h>
Andreas Eversberg0aed6542012-06-23 10:33:16 +020057
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010058// FIXME: move this, when changed from c++ to c.
59extern "C" {
Max58b66462016-05-13 10:34:15 +020060void *l1if_open_pdch(uint8_t trx_no, uint32_t hlayer1,
61 struct gsmtap_inst *gsmtap);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +010062int l1if_connect_pdch(void *obj, uint8_t ts);
63int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn,
64 uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len);
65}
66
Andreas Eversberge266bd42012-07-13 14:00:21 +020067extern void *tall_pcu_ctx;
68
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010069#define PAGING_GROUP_LEN 3
70
71/* returns [0,999] on success, > 999 on error */
72uint16_t imsi2paging_group(const char* imsi)
73{
74 uint16_t pgroup = 0;
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070075 size_t len;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010076
Vadim Yanitskiybd0b0b32020-01-16 00:25:48 +070077 len = (imsi != NULL) ? strlen(imsi) : 0;
78 if (len < PAGING_GROUP_LEN)
Pau Espin Pedrold7c32652019-12-23 12:41:34 +010079 return 0xFFFF;
80 imsi += len - PAGING_GROUP_LEN;
81
82 while (*imsi != '\0') {
83 if (!isdigit(*imsi))
84 return 0xFFFF;
85 pgroup *= 10;
86 pgroup += *imsi - '0';
87 imsi++;
88 }
89 return pgroup;
90}
91
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040092/*
93 * PCU messages
94 */
Ivan Kluchnikove3a05962012-03-18 15:48:51 +040095
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +040096struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
97{
98 struct msgb *msg;
99 struct gsm_pcu_if *pcu_prim;
100
101 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
102 if (!msg)
103 return NULL;
104 msgb_put(msg, sizeof(struct gsm_pcu_if));
105 pcu_prim = (struct gsm_pcu_if *) msg->data;
106 pcu_prim->msg_type = msg_type;
107 pcu_prim->bts_nr = bts_nr;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400108
109 return msg;
110}
111
Max0a8fae82017-03-08 18:53:30 +0100112const struct value_string gsm_pcu_if_text_type_names[] = {
113 OSMO_VALUE_STRING(PCU_VERSION),
114 OSMO_VALUE_STRING(PCU_OML_ALERT),
115 { 0, NULL }
116};
117
118int pcu_tx_txt_ind(enum gsm_pcu_if_text_type t, const char *fmt, ...)
119{
120 struct gsm_pcu_if *pcu_prim;
121 struct gsm_pcu_if_txt_ind *txt;
122 va_list ap;
123 char *rep;
124 struct msgb *msg = pcu_msgb_alloc(PCU_IF_MSG_TXT_IND, 0);
125 if (!msg)
126 return -ENOMEM;
127
128 pcu_prim = (struct gsm_pcu_if *) msg->data;
129 txt = &pcu_prim->u.txt_ind;
130 txt->type = t;
131
132 va_start(ap, fmt);
133 rep = talloc_vasprintf(tall_pcu_ctx, fmt, ap);
134 va_end(ap);
135
136 if (!rep)
137 return -ENOMEM;
138
139 osmo_strlcpy(txt->text, rep, TXT_MAX_LEN);
140 talloc_free(rep);
141
142 LOGP(DL1IF, LOGL_INFO, "Sending %s TXT as %s to BTS\n", txt->text,
143 get_value_string(gsm_pcu_if_text_type_names, t));
144
145 return pcu_sock_send(msg);
146}
147
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400148static int pcu_tx_act_req(uint8_t trx, uint8_t ts, uint8_t activate)
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400149{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400150 struct msgb *msg;
151 struct gsm_pcu_if *pcu_prim;
152 struct gsm_pcu_if_act_req *act_req;
153
154 LOGP(DL1IF, LOGL_INFO, "Sending %s request: trx=%d ts=%d\n",
155 (activate) ? "activate" : "deactivate", trx, ts);
156
157 msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, 0);
158 if (!msg)
159 return -ENOMEM;
160 pcu_prim = (struct gsm_pcu_if *) msg->data;
161 act_req = &pcu_prim->u.act_req;
162 act_req->activate = activate;
163 act_req->trx_nr = trx;
164 act_req->ts_nr = ts;
165
166 return pcu_sock_send(msg);
167}
168
169static int pcu_tx_data_req(uint8_t trx, uint8_t ts, uint8_t sapi,
170 uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data,
171 uint8_t len)
172{
173 struct msgb *msg;
174 struct gsm_pcu_if *pcu_prim;
175 struct gsm_pcu_if_data *data_req;
Philipp Maier7e8e3972018-04-10 13:31:45 +0200176 int current_fn = get_current_fn();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400177
178 LOGP(DL1IF, LOGL_DEBUG, "Sending data request: trx=%d ts=%d sapi=%d "
Philipp Maier7e8e3972018-04-10 13:31:45 +0200179 "arfcn=%d fn=%d cur_fn=%d block=%d data=%s\n", trx, ts, sapi, arfcn, fn, current_fn,
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400180 block_nr, osmo_hexdump(data, len));
181
182 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, 0);
183 if (!msg)
184 return -ENOMEM;
185 pcu_prim = (struct gsm_pcu_if *) msg->data;
186 data_req = &pcu_prim->u.data_req;
187
188 data_req->sapi = sapi;
189 data_req->fn = fn;
190 data_req->arfcn = arfcn;
191 data_req->trx_nr = trx;
192 data_req->ts_nr = ts;
193 data_req->block_nr = block_nr;
194 memcpy(data_req->data, data, len);
195 data_req->len = len;
196
197 return pcu_sock_send(msg);
198}
199
200void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn,
201 uint32_t fn, uint8_t block_nr)
202{
Maxb216c6b2017-09-01 11:00:39 +0200203#ifdef ENABLE_DIRECT_PHY
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200204 struct gprs_rlcmac_bts *bts = bts_main_data();
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100205
Max528ff392016-02-22 11:42:33 +0100206 if (bts->trx[trx].fl1h) {
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100207 l1if_pdch_req(bts->trx[trx].fl1h, ts, 0, fn, arfcn, block_nr,
208 msg->data, msg->len);
Max528ff392016-02-22 11:42:33 +0100209 msgb_free(msg);
210 return;
211 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100212#endif
Max528ff392016-02-22 11:42:33 +0100213 pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100214 msg->data, msg->len);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200215 msgb_free(msg);
Ivan Kluchnikov4277f0c2012-04-12 14:24:35 +0400216}
217
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100218void pcu_l1if_tx_ptcch(struct gprs_rlcmac_bts *bts,
219 uint8_t trx, uint8_t ts, uint16_t arfcn,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700220 uint32_t fn, uint8_t block_nr,
221 uint8_t *data, size_t data_len)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400222{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100223 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PTCCH))
224 gsmtap_send(the_pcu->gsmtap, arfcn, ts, GSMTAP_CHANNEL_PTCCH, 0, fn, 0, 0, data, data_len);
Maxcad867e2016-04-21 14:35:55 +0200225#ifdef ENABLE_DIRECT_PHY
Max528ff392016-02-22 11:42:33 +0100226 if (bts->trx[trx].fl1h) {
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700227 l1if_pdch_req(bts->trx[trx].fl1h, ts, 1, fn, arfcn, block_nr, data, data_len);
Max528ff392016-02-22 11:42:33 +0100228 return;
229 }
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100230#endif
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700231 pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, data, data_len);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400232}
233
Andreas Eversberg7b045012012-07-05 07:38:49 +0200234void pcu_l1if_tx_agch(bitvec * block, int plen)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400235{
Pau Espin Pedrold752d7c2019-10-07 20:56:48 +0200236 uint8_t data[GSM_MACBLOCK_LEN]; /* prefix PLEN */
237
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400238 /* FIXME: why does OpenBTS has no PLEN and no fill in message? */
239 bitvec_pack(block, data + 1);
240 data[0] = (plen << 2) | 0x01;
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200241
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100242 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_AGCH))
243 gsmtap_send(the_pcu->gsmtap, 0, 0, GSMTAP_CHANNEL_AGCH, 0, 0, 0, 0, data, GSM_MACBLOCK_LEN);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200244
Pau Espin Pedrold752d7c2019-10-07 20:56:48 +0200245 pcu_tx_data_req(0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, GSM_MACBLOCK_LEN);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400246}
247
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100248void pcu_l1if_tx_pch(bitvec * block, int plen, uint16_t pgroup)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400249{
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200250 uint8_t data[PAGING_GROUP_LEN + GSM_MACBLOCK_LEN];
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100251 int i;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400252
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200253 /* prepend paging group */
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100254 for (i = 0; i < PAGING_GROUP_LEN; i++) {
255 data[PAGING_GROUP_LEN - 1 - i] = '0' + (char)(pgroup % 10);
256 pgroup = pgroup / 10;
257 }
258 OSMO_ASSERT(pgroup == 0);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400259
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200260 /* block provided by upper layer comes without first byte (plen),
261 * prepend it manually:
262 */
263 OSMO_ASSERT(sizeof(data) >= PAGING_GROUP_LEN + 1 + block->data_len);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400264 data[3] = (plen << 2) | 0x01;
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200265 bitvec_pack(block, data + PAGING_GROUP_LEN + 1);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200266
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100267 if (the_pcu->gsmtap_categ_mask & (1 << PCU_GSMTAP_C_DL_PCH))
268 gsmtap_send(the_pcu->gsmtap, 0, 0, GSMTAP_CHANNEL_PCH, 0, 0, 0, 0, data + 3, GSM_MACBLOCK_LEN);
Pau Espin Pedrol58543702019-09-30 19:07:16 +0200269
Pau Espin Pedrol8dc09e72019-10-07 20:56:02 +0200270 pcu_tx_data_req(0, 0, PCU_IF_SAPI_PCH, 0, 0, 0, data, PAGING_GROUP_LEN + GSM_MACBLOCK_LEN);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400271}
272
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200273extern "C" void pcu_rx_block_time(uint16_t arfcn, uint32_t fn, uint8_t ts_no)
274{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100275 bts_set_current_block_frame_number(the_pcu->bts,fn, 0);
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200276}
277
278extern "C" void pcu_rx_ra_time(uint16_t arfcn, uint32_t fn, uint8_t ts_no)
279{
280 /* access bursts may arrive some bursts earlier */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100281 bts_set_current_block_frame_number(the_pcu->bts,fn, 5);
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200282}
283
Holger Hans Peter Freyther9ae367f2013-10-26 16:42:38 +0200284extern "C" int pcu_rx_data_ind_pdtch(uint8_t trx_no, uint8_t ts_no, uint8_t *data,
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200285 uint8_t len, uint32_t fn, struct pcu_l1_meas *meas)
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100286{
Holger Hans Peter Freyther9ae367f2013-10-26 16:42:38 +0200287 struct gprs_rlcmac_pdch *pdch;
288
289 pdch = &bts_main_data()->trx[trx_no].pdch[ts_no];
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200290 return pdch->rcv_block(data, len, fn, meas);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100291}
292
Max84bf0fa2017-09-01 11:02:40 +0200293static int pcu_rx_data_ind_bcch(uint8_t *data, uint8_t len)
294{
295 struct gprs_rlcmac_bts *bts = bts_main_data();
296
297 if (len == 0) {
298 bts->si13_is_set = false;
299 LOGP(DL1IF, LOGL_INFO, "Received PCU data indication with empty SI13: cache cleaned\n");
300 return 0;
301 }
302
303 if (len != GSM_MACBLOCK_LEN) {
304 LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with SI13 with unexpected length %u\n", len);
305 return -EINVAL;
306 }
307
308 memcpy(bts->si13, data, GSM_MACBLOCK_LEN);
309 bts->si13_is_set = true;
310
311 return 0;
312}
313
Harald Welte717cdf52017-07-21 21:56:23 +0200314static int pcu_rx_data_ind(struct gsm_pcu_if_data *data_ind)
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400315{
Max528ff392016-02-22 11:42:33 +0100316 int rc;
Philipp Maier7e8e3972018-04-10 13:31:45 +0200317 int current_fn = get_current_fn();
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100318 struct pcu_l1_meas meas = {0};
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200319 uint8_t gsmtap_chantype;
Maxb216c6b2017-09-01 11:00:39 +0200320
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400321 LOGP(DL1IF, LOGL_DEBUG, "Data indication received: sapi=%d arfcn=%d "
Philipp Maier7e8e3972018-04-10 13:31:45 +0200322 "fn=%d cur_fn=%d block=%d data=%s\n", data_ind->sapi,
323 data_ind->arfcn, data_ind->fn, current_fn, data_ind->block_nr,
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400324 osmo_hexdump(data_ind->data, data_ind->len));
325
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400326 switch (data_ind->sapi) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400327 case PCU_IF_SAPI_PDTCH:
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100328 pcu_l1_meas_set_rssi(&meas, data_ind->rssi);
Maxb216c6b2017-09-01 11:00:39 +0200329 /* convert BER to % value */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100330 pcu_l1_meas_set_ber(&meas, data_ind->ber10k / 100);
331 pcu_l1_meas_set_bto(&meas, data_ind->ta_offs_qbits);
332 pcu_l1_meas_set_link_qual(&meas, data_ind->lqual_cb / 10);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700333
Maxb216c6b2017-09-01 11:00:39 +0200334 LOGP(DL1IF, LOGL_DEBUG, "Data indication with raw measurements received: BER10k = %d, BTO = %d, Q = %d\n",
335 data_ind->ber10k, data_ind->ta_offs_qbits, data_ind->lqual_cb);
Vadim Yanitskiyf17dfc02019-07-09 16:16:35 +0700336
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100337 rc = pcu_rx_data_ind_pdtch(data_ind->trx_nr, data_ind->ts_nr,
Andreas Eversberg570b44b2013-03-16 16:15:01 +0100338 data_ind->data, data_ind->len, data_ind->fn,
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +0200339 &meas);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200340 gsmtap_chantype = GSMTAP_CHANNEL_PDTCH;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400341 break;
Max84bf0fa2017-09-01 11:02:40 +0200342 case PCU_IF_SAPI_BCCH:
343 rc = pcu_rx_data_ind_bcch(data_ind->data, data_ind->len);
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200344 gsmtap_chantype = GSMTAP_CHANNEL_BCCH;
Max84bf0fa2017-09-01 11:02:40 +0200345 break;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400346 default:
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400347 LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with "
348 "unsupported sapi %d\n", data_ind->sapi);
349 rc = -EINVAL;
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200350 gsmtap_chantype = GSMTAP_CHANNEL_UNKNOWN;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400351 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400352
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100353 if (rc < 0 && (the_pcu->gsmtap_categ_mask & (1 <<PCU_GSMTAP_C_UL_UNKNOWN))) {
354 gsmtap_send(the_pcu->gsmtap, data_ind->arfcn | GSMTAP_ARFCN_F_UPLINK, data_ind->ts_nr,
Pau Espin Pedrol72e39562020-06-30 17:57:42 +0200355 gsmtap_chantype, 0, data_ind->fn, meas.rssi, meas.link_qual, data_ind->data, data_ind->len);
Harald Welte717cdf52017-07-21 21:56:23 +0200356 }
357
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400358 return rc;
359}
360
Andreas Eversberga9be1542012-09-27 09:23:24 +0200361static int pcu_rx_data_cnf(struct gsm_pcu_if_data *data_cnf)
362{
363 int rc = 0;
Philipp Maier7e8e3972018-04-10 13:31:45 +0200364 int current_fn = get_current_fn();
Andreas Eversberga9be1542012-09-27 09:23:24 +0200365
Philipp Maier7e8e3972018-04-10 13:31:45 +0200366 LOGP(DL1IF, LOGL_DEBUG, "Data confirm received: sapi=%d fn=%d cur_fn=%d\n",
367 data_cnf->sapi, data_cnf->fn, current_fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200368
369 switch (data_cnf->sapi) {
370 case PCU_IF_SAPI_PCH:
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200371 if (data_cnf->data[2] == 0x3f)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100372 bts_rcv_imm_ass_cnf(the_pcu->bts, data_cnf->data, data_cnf->fn);
Andreas Eversberga9be1542012-09-27 09:23:24 +0200373 break;
374 default:
375 LOGP(DL1IF, LOGL_ERROR, "Received PCU data confirm with "
376 "unsupported sapi %d\n", data_cnf->sapi);
377 rc = -EINVAL;
378 }
379
380 return rc;
381}
382
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100383// FIXME: remove this, when changed from c++ to c.
Max878bd1f2016-07-20 13:05:05 +0200384extern "C" int pcu_rx_rts_req_pdtch(uint8_t trx, uint8_t ts,
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100385 uint32_t fn, uint8_t block_nr)
386{
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200387 return gprs_rlcmac_rcv_rts_block(bts_main_data(),
Max878bd1f2016-07-20 13:05:05 +0200388 trx, ts, fn, block_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100389}
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700390extern "C" int pcu_rx_rts_req_ptcch(uint8_t trx, uint8_t ts,
391 uint32_t fn, uint8_t block_nr)
392{
393 struct gprs_rlcmac_bts *bts = bts_main_data();
394 struct gprs_rlcmac_pdch *pdch;
395
396 /* Prevent buffer overflow */
397 if (trx >= ARRAY_SIZE(bts->trx) || ts >= 8)
398 return -EINVAL;
399
400 /* Make sure PDCH time-slot is enabled */
401 pdch = &bts->trx[trx].pdch[ts];
402 if (!pdch->m_is_enabled)
403 return -EAGAIN;
404
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100405 pcu_l1if_tx_ptcch(bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr,
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700406 pdch->ptcch_msg, GSM_MACBLOCK_LEN);
407 return 0;
408}
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100409
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400410static int pcu_rx_rts_req(struct gsm_pcu_if_rts_req *rts_req)
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400411{
412 int rc = 0;
Philipp Maier7e8e3972018-04-10 13:31:45 +0200413 int current_fn = get_current_fn();
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400414
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400415 LOGP(DL1IF, LOGL_DEBUG, "RTS request received: trx=%d ts=%d sapi=%d "
Philipp Maier7e8e3972018-04-10 13:31:45 +0200416 "arfcn=%d fn=%d cur_fn=%d block=%d\n", rts_req->trx_nr, rts_req->ts_nr,
417 rts_req->sapi, rts_req->arfcn, rts_req->fn, current_fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400418
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400419 switch (rts_req->sapi) {
420 case PCU_IF_SAPI_PDTCH:
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100421 pcu_rx_rts_req_pdtch(rts_req->trx_nr, rts_req->ts_nr,
Max878bd1f2016-07-20 13:05:05 +0200422 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400423 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400424 case PCU_IF_SAPI_PTCCH:
Vadim Yanitskiy78f58612019-10-05 22:22:08 +0700425 pcu_rx_rts_req_ptcch(rts_req->trx_nr, rts_req->ts_nr,
426 rts_req->fn, rts_req->block_nr);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400427 break;
428 default:
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400429 LOGP(DL1IF, LOGL_ERROR, "Received PCU RTS request with "
430 "unsupported sapi %d\n", rts_req->sapi);
431 rc = -EINVAL;
432 }
433
434 return rc;
435}
436
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700437/* C -> C++ adapter for direct DSP access code (e.g. osmo-bts-sysmo) */
Vadim Yanitskiy6fd47332020-05-21 19:23:33 +0700438extern "C" int pcu_rx_rach_ind_ptcch(uint8_t trx_nr, uint8_t ts_nr, uint32_t fn, int16_t qta)
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700439{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700440 struct rach_ind_params rip = {
441 /* The content of RA is not of interest on PTCCH/U */
442 .burst_type = GSM_L1_BURST_TYPE_ACCESS_0,
443 .is_11bit = false,
444 .ra = 0x00,
445 .trx_nr = trx_nr,
446 .ts_nr = ts_nr,
447 .rfn = fn,
448 .qta = qta,
449 };
450
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100451 return bts_rcv_ptcch_rach(the_pcu->bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700452}
453
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700454static int pcu_rx_rach_ind(const struct gsm_pcu_if_rach_ind *rach_ind)
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400455{
456 int rc = 0;
Philipp Maier7e8e3972018-04-10 13:31:45 +0200457 int current_fn = get_current_fn();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400458
459 LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d "
Vadim Yanitskiyb30f28f2020-03-29 19:01:58 +0700460 "qta=%d, ra=0x%02x, fn=%u, cur_fn=%d, is_11bit=%d\n", rach_ind->sapi, rach_ind->qta,
Philipp Maier7e8e3972018-04-10 13:31:45 +0200461 rach_ind->ra, rach_ind->fn, current_fn, rach_ind->is_11bit);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400462
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700463 struct rach_ind_params rip = {
464 .burst_type = (enum ph_burst_type) rach_ind->burst_type,
465 .is_11bit = rach_ind->is_11bit > 0,
466 .ra = rach_ind->ra,
467 .trx_nr = rach_ind->trx_nr,
468 .ts_nr = rach_ind->ts_nr,
469 .rfn = rach_ind->fn,
470 .qta = rach_ind->qta,
471 };
472
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400473 switch (rach_ind->sapi) {
474 case PCU_IF_SAPI_RACH:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100475 rc = bts_rcv_rach(the_pcu->bts, &rip);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400476 break;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700477 case PCU_IF_SAPI_PTCCH:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100478 rc = bts_rcv_ptcch_rach(the_pcu->bts, &rip);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700479 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400480 default:
481 LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with "
482 "unsupported sapi %d\n", rach_ind->sapi);
483 rc = -EINVAL;
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400484 }
485
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400486 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400487}
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200488
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200489static int pcu_info_ind_ns(struct gprs_rlcmac_bts *bts,
490 const struct gsm_pcu_if_info_ind *info_ind)
491{
492 struct osmo_sockaddr remote[PCU_IF_NUM_NSVC] = { };
493 struct osmo_sockaddr local[PCU_IF_NUM_NSVC] = { };
494 uint16_t nsvci[PCU_IF_NUM_NSVC] = { };
495 uint16_t valid = 0;
496
497 for (unsigned int i = 0; i < PCU_IF_NUM_NSVC; i++) {
498 struct osmo_sockaddr_str sockstr;
499
500 switch (info_ind->address_type[i]) {
501 case PCU_IF_ADDR_TYPE_IPV4:
502 local[i].u.sin.sin_family = AF_INET;
503 local[i].u.sin.sin_addr.s_addr = INADDR_ANY;
504 local[i].u.sin.sin_port = htons(info_ind->local_port[i]);
505
506 remote[i].u.sin.sin_family = AF_INET;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100507 memcpy(&remote[i].u.sin.sin_addr, &info_ind->remote_ip[i].v4,
508 sizeof(struct in_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200509 remote[i].u.sin.sin_port = htons(info_ind->remote_port[i]);
510 break;
511 case PCU_IF_ADDR_TYPE_IPV6:
512 local[i].u.sin6.sin6_family = AF_INET6;
513 local[i].u.sin6.sin6_addr = in6addr_any;
514 local[i].u.sin6.sin6_port = htons(info_ind->local_port[i]);
515
516 remote[i].u.sin6.sin6_family = AF_INET6;
Alexander Couzens87c6dd32020-10-27 13:24:40 +0100517 memcpy(&remote[i].u.sin6.sin6_addr,
518 &info_ind->remote_ip[i].v6,
519 sizeof(struct in6_addr));
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200520 remote[i].u.sin6.sin6_port = htons(info_ind->remote_port[i]);
521 break;
522 default:
523 continue;
524 }
525 nsvci[i] = info_ind->nsvci[i];
526
527 LOGP(DL1IF, LOGL_DEBUG, " NS%u nsvci=%u\n", i, nsvci[i]);
528 if (osmo_sockaddr_str_from_sockaddr(&sockstr, &remote[i].u.sas))
529 strcpy(sockstr.ip, "invalid");
530
531 LOGP(DL1IF, LOGL_DEBUG, " NS%u address: r=%s:%u<->l=NULL:%u\n",
532 i, sockstr.ip, sockstr.port, info_ind->local_port[i]);
533
534 valid |= 1 << i;
535 }
536
537 if (valid == 0) {
538 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
539 return -EINVAL;
540 }
541
542 return gprs_ns_config(bts, info_ind->nsei, local, remote, nsvci, valid);
543}
544
Vadim Yanitskiyce0dae92020-07-24 01:16:36 +0700545static int pcu_rx_info_ind(const struct gsm_pcu_if_info_ind *info_ind)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200546{
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200547 struct gprs_rlcmac_bts *bts = bts_main_data();
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +0200548 struct gprs_bssgp_pcu *pcu;
Harald Weltebb47d952016-11-17 18:40:02 +0100549 int rc = 0;
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700550 unsigned int trx_nr, ts_nr;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100551 unsigned int i;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200552
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200553 if (info_ind->version != PCU_IF_VERSION) {
Vadim Yanitskiyfad21282020-07-19 02:05:33 +0700554 fprintf(stderr, "PCU interface version number of BTS (%u) is "
555 "different (%u).\nPlease re-compile!\n",
Andreas Eversberg8389fd02012-07-18 10:06:48 +0200556 info_ind->version, PCU_IF_VERSION);
557 exit(-1);
558 }
559
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400560 LOGP(DL1IF, LOGL_DEBUG, "Info indication received:\n");
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200561
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400562 if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) {
563 LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n");
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100564 if (!bts->active)
565 return -EAGAIN;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400566bssgp_failed:
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100567 bts->active = false;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400568 /* free all TBF */
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700569 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
570 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
571 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++)
572 bts->trx[trx_nr].pdch[ts_nr].free_resources();
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400573 }
Alexander Couzens290d9032020-09-16 21:52:02 +0200574 gprs_bssgp_destroy(bts);
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200575 exit(0);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200576 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400577 LOGP(DL1IF, LOGL_INFO, "BTS available\n");
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100578 LOGP(DL1IF, LOGL_DEBUG, " mcc=%03u\n", info_ind->mcc);
579 LOGP(DL1IF, LOGL_DEBUG, " mnc=%0*u\n", info_ind->mnc_3_digits, info_ind->mnc);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400580 LOGP(DL1IF, LOGL_DEBUG, " lac=%d\n", info_ind->lac);
581 LOGP(DL1IF, LOGL_DEBUG, " rac=%d\n", info_ind->rac);
Harald Welte1473b372019-03-21 19:48:55 +0100582 LOGP(DL1IF, LOGL_DEBUG, " cell_id=%d\n", info_ind->cell_id);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100583 LOGP(DL1IF, LOGL_DEBUG, " bsic=%d\n", info_ind->bsic);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400584 LOGP(DL1IF, LOGL_DEBUG, " nsei=%d\n", info_ind->nsei);
585 LOGP(DL1IF, LOGL_DEBUG, " nse_timer=%d %d %d %d %d %d %d\n",
586 info_ind->nse_timer[0], info_ind->nse_timer[1],
587 info_ind->nse_timer[2], info_ind->nse_timer[3],
588 info_ind->nse_timer[4], info_ind->nse_timer[5],
589 info_ind->nse_timer[6]);
590 LOGP(DL1IF, LOGL_DEBUG, " cell_timer=%d %d %d %d %d %d %d %d %d %d "
591 "%d\n",
592 info_ind->cell_timer[0], info_ind->cell_timer[1],
593 info_ind->cell_timer[2], info_ind->cell_timer[3],
594 info_ind->cell_timer[4], info_ind->cell_timer[5],
595 info_ind->cell_timer[6], info_ind->cell_timer[7],
596 info_ind->cell_timer[8], info_ind->cell_timer[9],
597 info_ind->cell_timer[10]);
598 LOGP(DL1IF, LOGL_DEBUG, " repeat_time=%d\n", info_ind->repeat_time);
599 LOGP(DL1IF, LOGL_DEBUG, " repeat_count=%d\n", info_ind->repeat_count);
600 LOGP(DL1IF, LOGL_DEBUG, " bvci=%d\n", info_ind->bvci);
601 LOGP(DL1IF, LOGL_DEBUG, " t3142=%d\n", info_ind->t3142);
602 LOGP(DL1IF, LOGL_DEBUG, " t3169=%d\n", info_ind->t3169);
603 LOGP(DL1IF, LOGL_DEBUG, " t3191=%d\n", info_ind->t3191);
604 LOGP(DL1IF, LOGL_DEBUG, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10);
605 LOGP(DL1IF, LOGL_DEBUG, " t3195=%d\n", info_ind->t3195);
606 LOGP(DL1IF, LOGL_DEBUG, " n3101=%d\n", info_ind->n3101);
607 LOGP(DL1IF, LOGL_DEBUG, " n3103=%d\n", info_ind->n3103);
608 LOGP(DL1IF, LOGL_DEBUG, " n3105=%d\n", info_ind->n3105);
609 LOGP(DL1IF, LOGL_DEBUG, " cv_countdown=%d\n", info_ind->cv_countdown);
610 LOGP(DL1IF, LOGL_DEBUG, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext);
611 LOGP(DL1IF, LOGL_DEBUG, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100612 bts->bsic = info_ind->bsic;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100613
614 bts->cs_mask = 1 << 0; /* We need at least 1 CS, let's enable CS1 */
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400615 for (i = 0; i < 4; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100616 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_CS1 << i));
617 bts->cs_mask |= allowed << i;
618 if (allowed)
619 LOGP(DL1IF, LOGL_DEBUG, " Use CS%d\n", i + 1);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400620 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100621 bts_recalc_max_cs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100622
623 bts->mcs_mask = 0;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400624 for (i = 0; i < 9; i++) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100625 uint8_t allowed = !!(info_ind->flags & (PCU_IF_FLAG_MCS1 << i));
626 bts->mcs_mask |= allowed << i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100627 if (allowed)
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100628 LOGP(DL1IF, LOGL_DEBUG, " Use MCS%d\n", i + 1);
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100629
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400630 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100631 bts_recalc_max_mcs(bts);
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100632
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100633 LOGP(DL1IF, LOGL_DEBUG, " initial_cs=%u%s\n", info_ind->initial_cs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100634 the_pcu->vty.force_initial_cs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100635 bts->pcuif_info_ind.initial_cs = info_ind->initial_cs;
636 bts_recalc_initial_cs(bts);
Pau Espin Pedrolb47b1372020-11-03 15:59:39 +0100637
638 LOGP(DL1IF, LOGL_DEBUG, " initial_mcs=%u%s\n", info_ind->initial_mcs,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100639 the_pcu->vty.force_initial_mcs ? " (VTY forced, ignoring)" : "");
Pau Espin Pedrol793583e2021-01-14 16:20:57 +0100640 bts->pcuif_info_ind.initial_mcs = info_ind->initial_mcs;
641 bts_recalc_initial_mcs(bts);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200642
Alexander Couzens290d9032020-09-16 21:52:02 +0200643 pcu = gprs_bssgp_init(
644 bts,
645 info_ind->nsei, info_ind->bvci,
646 info_ind->mcc, info_ind->mnc, info_ind->mnc_3_digits,
647 info_ind->lac, info_ind->rac, info_ind->cell_id);
648 if (!pcu) {
Vadim Yanitskiyc1a726c2020-10-08 22:46:26 +0700649 LOGP(DL1IF, LOGL_ERROR, "Failed to init BSSGP\n");
Alexander Couzens9b5c9602020-07-28 13:47:18 +0200650 goto bssgp_failed;
651 }
652
Alexander Couzens5bece2a2020-10-12 02:27:22 +0200653 rc = pcu_info_ind_ns(pcu->bts, info_ind);
654 if (rc < 0) {
Alexander Couzens290d9032020-09-16 21:52:02 +0200655 LOGP(DL1IF, LOGL_ERROR, "No NSVC available to connect to the SGSN!\n");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400656 goto bssgp_failed;
657 }
658
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400659 if (info_ind->t3142) { /* if timer values are set */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200660 osmo_tdef_set(bts->T_defs_bts, 3142, info_ind->t3142, OSMO_TDEF_S);
661 osmo_tdef_set(bts->T_defs_bts, 3169, info_ind->t3169, OSMO_TDEF_S);
662 osmo_tdef_set(bts->T_defs_bts, 3191, info_ind->t3191, OSMO_TDEF_S);
663 osmo_tdef_set(bts->T_defs_bts, 3193, info_ind->t3193_10ms * 10, OSMO_TDEF_MS);
664 osmo_tdef_set(bts->T_defs_bts, 3195, info_ind->t3195, OSMO_TDEF_S);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400665 bts->n3101 = info_ind->n3101;
666 bts->n3103 = info_ind->n3103;
667 bts->n3105 = info_ind->n3105;
668 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400669
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700670 for (trx_nr = 0; trx_nr < ARRAY_SIZE(bts->trx); trx_nr++) {
671 bts->trx[trx_nr].arfcn = info_ind->trx[trx_nr].arfcn;
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100672 if ((info_ind->flags & PCU_IF_FLAG_SYSMO)
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700673 && info_ind->trx[trx_nr].hlayer1) {
Maxcad867e2016-04-21 14:35:55 +0200674#ifdef ENABLE_DIRECT_PHY
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700675 LOGP(DL1IF, LOGL_DEBUG, " TRX %d hlayer1=%x\n", trx_nr,
676 info_ind->trx[trx_nr].hlayer1);
677 if (!bts->trx[trx_nr].fl1h)
678 bts->trx[trx_nr].fl1h = l1if_open_pdch(
679 trx_nr,
680 info_ind->trx[trx_nr].hlayer1,
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100681 the_pcu->gsmtap);
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700682 if (!bts->trx[trx_nr].fl1h) {
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100683 LOGP(DL1IF, LOGL_FATAL, "Failed to open direct "
684 "DSP access for PDCH.\n");
685 exit(0);
686 }
687#else
688 LOGP(DL1IF, LOGL_FATAL, "Compiled without direct DSP "
689 "access for PDCH, but enabled at "
690 "BTS. Please deactivate it!\n");
691 exit(0);
692#endif
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100693 }
694
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700695 for (ts_nr = 0; ts_nr < ARRAY_SIZE(bts->trx[0].pdch); ts_nr++) {
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700696 const struct gsm_pcu_if_info_ts *its = &info_ind->trx[trx_nr].ts[ts_nr];
Vadim Yanitskiy0f41b712020-07-24 01:28:13 +0700697 struct gprs_rlcmac_pdch *pdch = &bts->trx[trx_nr].pdch[ts_nr];
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700698 if ((info_ind->trx[trx_nr].pdch_mask & (1 << ts_nr))) {
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400699 /* FIXME: activate dynamically at RLCMAC */
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200700 if (!pdch->is_enabled()) {
Maxcad867e2016-04-21 14:35:55 +0200701#ifdef ENABLE_DIRECT_PHY
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100702 if ((info_ind->flags &
703 PCU_IF_FLAG_SYSMO))
704 l1if_connect_pdch(
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700705 bts->trx[trx_nr].fl1h, ts_nr);
Andreas Eversberga23c7ee2012-12-18 10:47:28 +0100706#endif
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700707 pcu_tx_act_req(trx_nr, ts_nr, 1);
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200708 pdch->enable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200709 }
Vadim Yanitskiy8e2bd1e2020-07-21 00:54:42 +0700710
711 pdch->tsc = its->tsc;
712
713 /* (Optional) frequency hopping parameters */
714 if (its->h) {
715 pdch->fh.enabled = true;
716 pdch->fh.maio = its->maio;
717 pdch->fh.hsn = its->hsn;
718
719 OSMO_ASSERT(its->ma_bit_len <= sizeof(pdch->fh.ma) * 8);
720 pdch->fh.ma_oct_len = OSMO_BYTES_FOR_BITS(its->ma_bit_len);
721 pdch->fh.ma_bit_len = its->ma_bit_len;
722
723 /* Mobile Allocation + padding (byte/bit order as on the wire):
724 * | 00 00 00 00 00 cc bb aa | -> | cc bb aa 00 00 00 00 00 | */
725 unsigned int offset = sizeof(pdch->fh.ma) - pdch->fh.ma_oct_len;
726 memcpy(pdch->fh.ma, its->ma + offset, pdch->fh.ma_oct_len);
727 }
728
729 LOGP(DL1IF, LOGL_INFO, "PDCH (trx=%u, ts=%u): tsc=%u, hopping=%s\n",
730 trx_nr, ts_nr, pdch->tsc, pdch->fh.enabled ? "yes" : "no");
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400731 } else {
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200732 if (pdch->is_enabled()) {
Vadim Yanitskiy11b0f002020-07-24 01:24:47 +0700733 pcu_tx_act_req(trx_nr, ts_nr, 0);
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +0200734 pdch->free_resources();
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200735 pdch->disable();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200736 }
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400737 }
738 }
739 }
740
Stefan Sperling5b22fb72018-02-14 19:46:33 +0100741 bts->active = true;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200742 return rc;
743}
744
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400745static int pcu_rx_time_ind(struct gsm_pcu_if_time_ind *time_ind)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200746{
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400747 uint8_t fn13 = time_ind->fn % 13;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200748
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400749 /* omit frame numbers not starting at a MAC block */
750 if (fn13 != 0 && fn13 != 4 && fn13 != 8)
751 return 0;
Max3741f142017-06-26 12:10:12 +0200752
753 LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n", time_ind->fn % 52);
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400754
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100755 bts_set_current_frame_number(the_pcu->bts, time_ind->fn);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200756 return 0;
757}
758
Andreas Eversberg2b914642012-07-19 13:06:26 +0200759static int pcu_rx_pag_req(struct gsm_pcu_if_pag_req *pag_req)
760{
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200761 struct osmo_mobile_identity mi;
762 int rc;
763
Andreas Eversberg2b914642012-07-19 13:06:26 +0200764 LOGP(DL1IF, LOGL_DEBUG, "Paging request received: chan_needed=%d "
765 "length=%d\n", pag_req->chan_needed, pag_req->identity_lv[0]);
766
Pau Espin Pedrol32499b62019-12-09 13:55:12 +0100767 /* check if identity does not fit: length > sizeof(lv) - 1 */
768 if (pag_req->identity_lv[0] >= sizeof(pag_req->identity_lv)) {
769 LOGP(DL1IF, LOGL_ERROR, "Paging identity too large (%" PRIu8 ")\n",
770 pag_req->identity_lv[0]);
771 return -EINVAL;
772 }
773
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200774 rc = osmo_mobile_identity_decode(&mi, &pag_req->identity_lv[1], pag_req->identity_lv[0], true);
775 if (rc < 0) {
776 LOGP(DL1IF, LOGL_ERROR, "Failed to decode Mobile Identity in Paging Request (rc=%d)\n", rc);
777 return -EINVAL;
778 }
779
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100780 return bts_add_paging(the_pcu->bts, pag_req->chan_needed, &mi);
Andreas Eversberg2b914642012-07-19 13:06:26 +0200781}
782
Harald Welte3447c4a2017-07-11 00:06:38 +0200783static int pcu_rx_susp_req(struct gsm_pcu_if_susp_req *susp_req)
784{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100785 struct gprs_rlcmac_bts *bts = the_pcu->bts;
Harald Welte3447c4a2017-07-11 00:06:38 +0200786 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200787 GprsMs *ms;
788 struct gprs_rlcmac_dl_tbf *dl_tbf;
789 struct gprs_rlcmac_ul_tbf *ul_tbf;
Harald Welte3447c4a2017-07-11 00:06:38 +0200790 struct gprs_ra_id ra_id;
791
792 gsm48_parse_ra(&ra_id, susp_req->ra_id);
793
794 LOGP(DL1IF, LOGL_INFO, "GPRS Suspend request received: TLLI=0x%08x RAI=%s\n",
795 susp_req->tlli, osmo_rai_name(&ra_id));
796
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100797 if ((ms = bts_ms_store(bts)->get_ms(susp_req->tlli))) {
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200798 /* We need to catch both pointers here since MS may become freed
799 after first tbf_free(dl_tbf) if only DL TBF was available */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100800 dl_tbf = ms_dl_tbf(ms);
801 ul_tbf = ms_ul_tbf(ms);
Pau Espin Pedrolbd9973a2020-09-22 15:57:37 +0200802 if (dl_tbf)
803 tbf_free(dl_tbf);
804 if (ul_tbf)
805 tbf_free(ul_tbf);
806 }
807
Harald Welte3447c4a2017-07-11 00:06:38 +0200808 if (!bctx)
809 return -1;
810
811 return bssgp_tx_suspend(bctx->nsei, susp_req->tlli, &ra_id);
812}
813
Oliver Smithcfb63212019-09-05 17:13:33 +0200814static int pcu_rx_app_info_req(struct gsm_pcu_if_app_info_req *app_info_req)
815{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100816 struct gprs_rlcmac_bts *bts = the_pcu->bts;
817 struct gprs_rlcmac_bts *bts_data = bts;
Pau Espin Pedrol54faf022021-01-14 12:12:43 +0100818 struct llist_head *tmp;
Oliver Smithcfb63212019-09-05 17:13:33 +0200819
820 LOGP(DL1IF, LOGL_DEBUG, "Application Information Request received: type=0x%08x len=%i\n",
821 app_info_req->application_type, app_info_req->len);
822
823 bts_data->app_info_pending = 0;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100824 llist_for_each(tmp, bts_ms_store(bts)->ms_list()) {
Pau Espin Pedrol54faf022021-01-14 12:12:43 +0100825 GprsMs *ms = llist_entry(tmp, typeof(*ms), list);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100826 if (!ms_dl_tbf(ms))
Oliver Smithcfb63212019-09-05 17:13:33 +0200827 continue;
828 bts_data->app_info_pending++;
829 ms->app_info_pending = true;
830 }
831
832 if (!bts_data->app_info_pending) {
833 LOGP(DL1IF, LOGL_NOTICE, "Packet Application Information will not be sent, no subscribers with active"
834 " TBF\n");
835 return -1;
836 }
837
838 if (bts_data->app_info) {
839 LOGP(DL1IF, LOGL_NOTICE, "Previous Packet Application Information was not sent to all subscribers,"
840 " overwriting with new one\n");
841 msgb_free(bts_data->app_info);
842 }
843
844 LOGP(DL1IF, LOGL_INFO, "Sending Packet Application Information to %i subscribers with active TBF\n",
845 bts_data->app_info_pending);
846 bts_data->app_info = gprs_rlcmac_app_info_msg(app_info_req);
847 return 0;
848}
849
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400850int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim)
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200851{
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400852 int rc = 0;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200853
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400854 switch (msg_type) {
855 case PCU_IF_MSG_DATA_IND:
Harald Welte717cdf52017-07-21 21:56:23 +0200856 rc = pcu_rx_data_ind(&pcu_prim->u.data_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400857 break;
Andreas Eversberga9be1542012-09-27 09:23:24 +0200858 case PCU_IF_MSG_DATA_CNF:
859 rc = pcu_rx_data_cnf(&pcu_prim->u.data_cnf);
860 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400861 case PCU_IF_MSG_RTS_REQ:
862 rc = pcu_rx_rts_req(&pcu_prim->u.rts_req);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400863 break;
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400864 case PCU_IF_MSG_RACH_IND:
865 rc = pcu_rx_rach_ind(&pcu_prim->u.rach_ind);
866 break;
867 case PCU_IF_MSG_INFO_IND:
868 rc = pcu_rx_info_ind(&pcu_prim->u.info_ind);
869 break;
870 case PCU_IF_MSG_TIME_IND:
871 rc = pcu_rx_time_ind(&pcu_prim->u.time_ind);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400872 break;
Andreas Eversberg2b914642012-07-19 13:06:26 +0200873 case PCU_IF_MSG_PAG_REQ:
874 rc = pcu_rx_pag_req(&pcu_prim->u.pag_req);
875 break;
Harald Welte3447c4a2017-07-11 00:06:38 +0200876 case PCU_IF_MSG_SUSP_REQ:
877 rc = pcu_rx_susp_req(&pcu_prim->u.susp_req);
878 break;
Oliver Smithcfb63212019-09-05 17:13:33 +0200879 case PCU_IF_MSG_APP_INFO_REQ:
880 rc = pcu_rx_app_info_req(&pcu_prim->u.app_info_req);
881 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400882 default:
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200883 LOGP(DL1IF, LOGL_ERROR, "Received unknown PCU msg type %d\n",
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400884 msg_type);
885 rc = -EINVAL;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200886 }
Philipp Maier8ccf7042018-04-10 13:12:42 +0200887
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400888 return rc;
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200889}