blob: c6c677cf4d13a3aee9c1599a71d204ae7527711e [file] [log] [blame]
Max6dc90b82018-02-19 17:17:28 +01001/*
2 * Copyright (C) 2013 by Holger Hans Peter Freyther
3 * Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
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
22#include <bts.h>
23#include <pdch.h>
24#include <decoding.h>
25#include <encoding.h>
26#include <gprs_rlcmac.h>
27#include <gprs_debug.h>
28#include <gprs_coding_scheme.h>
29#include <gprs_ms.h>
30#include <gprs_ms_storage.h>
31#include <pcu_l1_if.h>
32#include <rlc.h>
33#include <sba.h>
34#include <tbf.h>
35#include <cxx_linuxlist.h>
36#include <gsm_rlcmac.h>
37
38extern "C" {
39 #include <osmocom/core/talloc.h>
40 #include <osmocom/core/msgb.h>
41 #include <osmocom/gsm/protocol/gsm_04_08.h>
42 #include <osmocom/core/bitvec.h>
43 #include <osmocom/core/gsmtap.h>
44 #include <osmocom/core/logging.h>
45 #include <osmocom/core/utils.h>
Max136ebcc2019-03-05 14:59:03 +010046 #include "coding_scheme.h"
Max6dc90b82018-02-19 17:17:28 +010047}
48
49#include <errno.h>
50#include <arpa/inet.h>
51
52extern void *tall_pcu_ctx;
53
54static void get_rx_qual_meas(struct pcu_l1_meas *meas, uint8_t rx_qual_enc)
55{
56 static const int16_t rx_qual_map[] = {
57 0, /* 0,14 % */
58 0, /* 0,28 % */
59 1, /* 0,57 % */
60 1, /* 1,13 % */
61 2, /* 2,26 % */
62 5, /* 4,53 % */
63 9, /* 9,05 % */
64 18, /* 18,10 % */
65 };
66
67 meas->set_ms_rx_qual(rx_qual_map[
68 OSMO_MIN(rx_qual_enc, ARRAY_SIZE(rx_qual_map)-1)
69 ]);
70}
71
72static void get_meas(struct pcu_l1_meas *meas,
73 const Packet_Resource_Request_t *qr)
74{
75 unsigned i;
76
77 meas->set_ms_c_value(qr->C_VALUE);
78 if (qr->Exist_SIGN_VAR)
79 meas->set_ms_sign_var((qr->SIGN_VAR + 2) / 4); /* SIGN_VAR * 0.25 dB */
80
81 for (i = 0; i < OSMO_MIN(ARRAY_SIZE(qr->Slot), ARRAY_SIZE(meas->ts)); i++)
82 {
83 if (qr->Slot[i].Exist) {
84 LOGP(DRLCMAC, LOGL_INFO,
85 "Packet resource request: i_level[%d] = %d\n",
86 i, qr->Slot[i].I_LEVEL);
87 meas->set_ms_i_level(i, -2 * qr->Slot[i].I_LEVEL);
88 }
89 }
90}
91
92static void get_meas(struct pcu_l1_meas *meas,
93 const Channel_Quality_Report_t *qr)
94{
95 unsigned i;
96
97 get_rx_qual_meas(meas, qr->RXQUAL);
98 meas->set_ms_c_value(qr->C_VALUE);
99 meas->set_ms_sign_var((qr->SIGN_VAR + 2) / 4); /* SIGN_VAR * 0.25 dB */
100
101 for (i = 0; i < OSMO_MIN(ARRAY_SIZE(qr->Slot), ARRAY_SIZE(meas->ts)); i++)
102 {
103 if (qr->Slot[i].Exist) {
104 LOGP(DRLCMAC, LOGL_DEBUG,
105 "Channel quality report: i_level[%d] = %d\n",
106 i, qr->Slot[i].I_LEVEL_TN);
107 meas->set_ms_i_level(i, -2 * qr->Slot[i].I_LEVEL_TN);
108 }
109 }
110}
111
112static inline void sched_ul_ass_or_rej(BTS *bts, gprs_rlcmac_bts *bts_data, struct gprs_rlcmac_dl_tbf *tbf)
113{
114 bts->channel_request_description();
115
116 /* This call will register the new TBF with the MS on success */
117 gprs_rlcmac_ul_tbf *ul_tbf = tbf_alloc_ul(bts_data, tbf->trx->trx_no, tbf->ms_class(),
118 tbf->ms()->egprs_ms_class(), tbf->tlli(), tbf->ta(), tbf->ms());
119
120 /* schedule uplink assignment or reject */
121 if (ul_tbf) {
122 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF in ack message, so we provide one:\n");
123 TBF_SET_ASS_STATE_UL(tbf, GPRS_RLCMAC_UL_ASS_SEND_ASS);
124 } else {
125 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF in ack message, so we packet access reject:\n");
126 TBF_SET_ASS_STATE_UL(tbf, GPRS_RLCMAC_UL_ASS_SEND_ASS_REJ);
127 }
128}
129
130void gprs_rlcmac_pdch::enable()
131{
132 /* TODO: Check if there are still allocated resources.. */
133 INIT_LLIST_HEAD(&paging_list);
134 m_is_enabled = 1;
135}
136
137void gprs_rlcmac_pdch::disable()
138{
139 /* TODO.. kick free_resources once we know the TRX/TS we are on */
140 m_is_enabled = 0;
141}
142
143void gprs_rlcmac_pdch::free_resources()
144{
145 struct gprs_rlcmac_paging *pag;
146
147 /* we are not enabled. there should be no resources */
148 if (!is_enabled())
149 return;
150
151 /* kick all TBF on slot */
152 gprs_rlcmac_tbf::free_all(this);
153
154 /* flush all pending paging messages */
155 while ((pag = dequeue_paging()))
156 talloc_free(pag);
157
158 trx->bts->sba()->free_resources(this);
159}
160
161struct gprs_rlcmac_paging *gprs_rlcmac_pdch::dequeue_paging()
162{
163 struct gprs_rlcmac_paging *pag;
164
165 if (llist_empty(&paging_list))
166 return NULL;
167 pag = llist_entry(paging_list.next, struct gprs_rlcmac_paging, list);
168 llist_del(&pag->list);
169
170 return pag;
171}
172
173struct msgb *gprs_rlcmac_pdch::packet_paging_request()
174{
175 struct gprs_rlcmac_paging *pag;
176 struct msgb *msg;
177 unsigned wp = 0, len;
178
179 /* no paging, no message */
180 pag = dequeue_paging();
181 if (!pag)
182 return NULL;
183
184 LOGP(DRLCMAC, LOGL_DEBUG, "Scheduling paging\n");
185
186 /* alloc message */
187 msg = msgb_alloc(23, "pag ctrl block");
188 if (!msg) {
189 talloc_free(pag);
190 return NULL;
191 }
192 bitvec *pag_vec = bitvec_alloc(23, tall_pcu_ctx);
193 if (!pag_vec) {
194 msgb_free(msg);
195 talloc_free(pag);
196 return NULL;
197 }
198 wp = Encoding::write_packet_paging_request(pag_vec);
199
200 /* loop until message is full */
201 while (pag) {
202 /* try to add paging */
203 if ((pag->identity_lv[1] & 0x07) == 4) {
204 /* TMSI */
205 LOGP(DRLCMAC, LOGL_DEBUG, "- TMSI=0x%08x\n",
206 ntohl(*((uint32_t *)(pag->identity_lv + 1))));
207 len = 1 + 1 + 1 + 32 + 2 + 1;
208 if (pag->identity_lv[0] != 5) {
209 LOGP(DRLCMAC, LOGL_ERROR, "TMSI paging with "
210 "MI != 5 octets!\n");
211 goto continue_next;
212 }
213 } else {
214 /* MI */
215 LOGP(DRLCMAC, LOGL_DEBUG, "- MI=%s\n",
216 osmo_hexdump(pag->identity_lv + 1,
217 pag->identity_lv[0]));
218 len = 1 + 1 + 1 + 4 + (pag->identity_lv[0]<<3) + 2 + 1;
219 if (pag->identity_lv[0] > 8) {
220 LOGP(DRLCMAC, LOGL_ERROR, "Paging with "
221 "MI > 8 octets!\n");
222 goto continue_next;
223 }
224 }
225 if (wp + len > 184) {
226 LOGP(DRLCMAC, LOGL_DEBUG, "- Does not fit, so schedule "
227 "next time\n");
228 /* put back paging record, because does not fit */
229 llist_add_tail(&pag->list, &paging_list);
230 break;
231 }
232 Encoding::write_repeated_page_info(pag_vec, wp, pag->identity_lv[0],
233 pag->identity_lv + 1, pag->chan_needed);
234
235continue_next:
236 talloc_free(pag);
237 pag = dequeue_paging();
238 }
239
240 bitvec_pack(pag_vec, msgb_put(msg, 23));
241 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
242 LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Paging Request +++++++++++++++++++++++++\n");
243 decode_gsm_rlcmac_downlink(pag_vec, mac_control_block);
244 LOGPC(DCSN1, LOGL_NOTICE, "\n");
245 LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Paging Request -------------------------\n");
246 bitvec_free(pag_vec);
247 talloc_free(mac_control_block);
248
249 return msg;
250}
251
252bool gprs_rlcmac_pdch::add_paging(uint8_t chan_needed, uint8_t *identity_lv)
253{
254 struct gprs_rlcmac_paging *pag = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_paging);
255 if (!pag)
256 return false;
257
258 pag->chan_needed = chan_needed;
259 memcpy(pag->identity_lv, identity_lv, identity_lv[0] + 1);
260
261 llist_add(&pag->list, &paging_list);
262
263 return true;
264}
265
266void gprs_rlcmac_pdch::rcv_control_ack(Packet_Control_Acknowledgement_t *packet, uint32_t fn)
267{
268 struct gprs_rlcmac_tbf *tbf, *new_tbf;
269 uint32_t tlli = packet->TLLI;
270 GprsMs *ms = bts()->ms_by_tlli(tlli);
271 gprs_rlcmac_ul_tbf *ul_tbf;
272
273 tbf = bts()->ul_tbf_by_poll_fn(fn, trx_no(), ts_no);
274 if (!tbf)
275 tbf = bts()->dl_tbf_by_poll_fn(fn, trx_no(), ts_no);
276
277 if (!tbf) {
278 LOGP(DRLCMAC, LOGL_NOTICE, "PACKET CONTROL ACK with "
279 "unknown FN=%u TLLI=0x%08x (TRX %d TS %d)\n",
280 fn, tlli, trx_no(), ts_no);
281 if (ms)
282 LOGP(DRLCMAC, LOGL_NOTICE, "PACKET CONTROL ACK with "
283 "unknown TBF corresponds to MS with IMSI %s, TA %d, "
284 "uTBF (TFI=%d, state=%s), dTBF (TFI=%d, state=%s)\n",
285 ms->imsi(), ms->ta(),
286 ms->ul_tbf() ? ms->ul_tbf()->tfi() : 0,
287 ms->ul_tbf() ? ms->ul_tbf()->state_name() : "None",
288 ms->dl_tbf() ? ms->dl_tbf()->tfi() : 0,
289 ms->dl_tbf() ? ms->dl_tbf()->state_name() : "None");
290 return;
291 }
292
293 /* Reset N3101 counter: */
Max847ed9f2018-02-20 18:16:11 +0100294 tbf->n_reset(N3101);
Max6dc90b82018-02-19 17:17:28 +0100295
296 tbf->update_ms(tlli, GPRS_RLCMAC_UL_TBF);
297
298 LOGPTBF(tbf, LOGL_DEBUG, "RX: [PCU <- BTS] Packet Control Ack\n");
299 TBF_POLL_SCHED_UNSET(tbf);
300
301 /* check if this control ack belongs to packet uplink ack */
302 ul_tbf = as_ul_tbf(tbf);
303 if (ul_tbf && ul_tbf->handle_ctrl_ack()) {
304 LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] END\n");
305 if (ul_tbf->ctrl_ack_to_toggle())
306 LOGPTBF(tbf, LOGL_NOTICE, "Recovered uplink ack for UL\n");
307
308 tbf_free(tbf);
309 return;
310 }
311 if (tbf->dl_ass_state_is(GPRS_RLCMAC_DL_ASS_WAIT_ACK)) {
312 LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] DOWNLINK ASSIGNED\n");
313 /* reset N3105 */
Max847ed9f2018-02-20 18:16:11 +0100314 tbf->n_reset(N3105);
Max6dc90b82018-02-19 17:17:28 +0100315 TBF_SET_ASS_STATE_DL(tbf, GPRS_RLCMAC_DL_ASS_NONE);
316
317 new_tbf = tbf->ms() ? tbf->ms()->dl_tbf() : NULL;
318 if (!new_tbf) {
319 LOGP(DRLCMAC, LOGL_ERROR, "Got ACK, but DL "
320 "TBF is gone TLLI=0x%08x\n", tlli);
321 return;
322 }
323 if (tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE) &&
324 tbf->direction == new_tbf->direction)
325 tbf_free(tbf);
326
327 if (new_tbf->check_n_clear(GPRS_RLCMAC_FLAG_CCCH)) {
328 /* We now know that the PACCH really existed */
329 LOGPTBF(new_tbf, LOGL_INFO,
330 "The TBF has been confirmed on the PACCH, "
331 "changed type from CCCH to PACCH\n");
332 TBF_ASS_TYPE_SET(new_tbf, GPRS_RLCMAC_FLAG_PACCH);
333 }
334 TBF_SET_STATE(new_tbf, GPRS_RLCMAC_FLOW);
335 /* stop pending assignment timer */
336 new_tbf->t_stop(T0, "control acked (DL-TBF)");
337 if (new_tbf->check_n_clear(GPRS_RLCMAC_FLAG_TO_DL_ASS))
338 LOGPTBF(new_tbf, LOGL_NOTICE, "Recovered downlink assignment\n");
339
340 tbf_assign_control_ts(new_tbf);
341 return;
342 }
343 if (tbf->ul_ass_state_is(GPRS_RLCMAC_UL_ASS_WAIT_ACK)) {
344 LOGPTBF(tbf, LOGL_DEBUG, "[DOWNLINK] UPLINK ASSIGNED\n");
345 /* reset N3105 */
Max847ed9f2018-02-20 18:16:11 +0100346 tbf->n_reset(N3105);
Max6dc90b82018-02-19 17:17:28 +0100347 TBF_SET_ASS_STATE_UL(tbf, GPRS_RLCMAC_UL_ASS_NONE);
348
349 new_tbf = tbf->ms() ? tbf->ms()->ul_tbf() : NULL;
350 if (!new_tbf) {
351 LOGP(DRLCMAC, LOGL_ERROR, "Got ACK, but UL "
352 "TBF is gone TLLI=0x%08x\n", tlli);
353 return;
354 }
355 if (tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE) &&
356 tbf->direction == new_tbf->direction)
357 tbf_free(tbf);
358
359 TBF_SET_STATE(new_tbf, GPRS_RLCMAC_FLOW);
360 if (new_tbf->check_n_clear(GPRS_RLCMAC_FLAG_TO_UL_ASS))
361 LOGPTBF(new_tbf, LOGL_NOTICE, "Recovered uplink assignment for UL\n");
362
363 tbf_assign_control_ts(new_tbf);
364 /* there might be LLC packets waiting in the queue, but the DL
365 * TBF might have been released while the UL TBF has been
366 * established */
367 if (new_tbf->ms()->need_dl_tbf())
368 new_tbf->establish_dl_tbf_on_pacch();
369
370 return;
371 }
372 LOGP(DRLCMAC, LOGL_ERROR, "Error: received PACET CONTROL ACK "
373 "at no request\n");
374}
375
376void gprs_rlcmac_pdch::rcv_control_dl_ack_nack(Packet_Downlink_Ack_Nack_t *ack_nack, uint32_t fn)
377{
378 int8_t tfi = 0; /* must be signed */
379 struct gprs_rlcmac_dl_tbf *tbf;
380 int rc;
381 struct pcu_l1_meas meas;
382 int num_blocks;
383 uint8_t bits_data[RLC_GPRS_WS/8];
384 bitvec bits;
385 int bsn_begin, bsn_end;
386 char show_bits[RLC_GPRS_WS + 1];
387
388 tfi = ack_nack->DOWNLINK_TFI;
389 tbf = bts()->dl_tbf_by_poll_fn(fn, trx_no(), ts_no);
390 if (!tbf) {
391 LOGP(DRLCMAC, LOGL_NOTICE, "PACKET DOWNLINK ACK with "
392 "unknown FN=%u TFI=%d (TRX %d TS %d)\n",
393 fn, tfi, trx_no(), ts_no);
394 return;
395 }
396 if (tbf->tfi() != tfi) {
397 LOGP(DRLCMAC, LOGL_NOTICE, "PACKET DOWNLINK ACK with "
398 "wrong TFI=%d, ignoring!\n", tfi);
399 return;
400 }
401
402 /* Reset N3101 counter: */
Max847ed9f2018-02-20 18:16:11 +0100403 tbf->n_reset(N3101);
Max6dc90b82018-02-19 17:17:28 +0100404
405 if (tbf->handle_ack_nack())
406 LOGPTBF(tbf, LOGL_NOTICE, "Recovered downlink ack\n");
407
408 LOGPTBF(tbf, LOGL_DEBUG, "RX: [PCU <- BTS] Packet Downlink Ack/Nack\n");
409
410 bits.data = bits_data;
411 bits.data_len = sizeof(bits_data);
412 bits.cur_bit = 0;
413
414 num_blocks = Decoding::decode_gprs_acknack_bits(
415 &ack_nack->Ack_Nack_Description, &bits,
416 &bsn_begin, &bsn_end, tbf->window());
417
418 LOGP(DRLCMAC, LOGL_DEBUG,
419 "Got GPRS DL ACK bitmap: SSN: %d, BSN %d to %d - 1 (%d blocks), "
420 "\"%s\"\n",
421 ack_nack->Ack_Nack_Description.STARTING_SEQUENCE_NUMBER,
422 bsn_begin, bsn_end, num_blocks,
423 (Decoding::extract_rbb(&bits, show_bits), show_bits));
424
425 rc = tbf->rcvd_dl_ack(
426 ack_nack->Ack_Nack_Description.FINAL_ACK_INDICATION,
427 bsn_begin, &bits);
428 if (rc == 1) {
429 tbf_free(tbf);
430 return;
431 }
432 /* check for channel request */
433 if (ack_nack->Exist_Channel_Request_Description)
434 sched_ul_ass_or_rej(bts(), bts_data(), tbf);
435
436 /* get measurements */
437 if (tbf->ms()) {
438 get_meas(&meas, &ack_nack->Channel_Quality_Report);
439 tbf->ms()->update_l1_meas(&meas);
440 }
441}
442
443void gprs_rlcmac_pdch::rcv_control_egprs_dl_ack_nack(EGPRS_PD_AckNack_t *ack_nack, uint32_t fn)
444{
445 int8_t tfi = 0; /* must be signed */
446 struct gprs_rlcmac_dl_tbf *tbf;
447 struct pcu_l1_meas meas;
448 int rc;
449 int num_blocks;
450 uint8_t bits_data[RLC_EGPRS_MAX_WS/8];
451 char show_bits[RLC_EGPRS_MAX_WS + 1];
452 bitvec bits;
453 int bsn_begin, bsn_end;
454
455 tfi = ack_nack->DOWNLINK_TFI;
456 tbf = bts()->dl_tbf_by_poll_fn(fn, trx_no(), ts_no);
457 if (!tbf) {
458 LOGP(DRLCMAC, LOGL_NOTICE, "EGPRS PACKET DOWNLINK ACK with "
459 "unknown FN=%u TFI=%d (TRX %d TS %d)\n",
460 fn, tfi, trx_no(), ts_no);
461 return;
462 }
463 if (tbf->tfi() != tfi) {
464 LOGP(DRLCMAC, LOGL_NOTICE, "EGPRS PACKET DOWNLINK ACK with "
465 "wrong TFI=%d, ignoring!\n", tfi);
466 return;
467 }
468
469 /* Reset N3101 counter: */
Max847ed9f2018-02-20 18:16:11 +0100470 tbf->n_reset(N3101);
Max6dc90b82018-02-19 17:17:28 +0100471
472 if (tbf->handle_ack_nack())
473 LOGPTBF(tbf, LOGL_NOTICE, "Recovered EGPRS downlink ack\n");
474
475 LOGPTBF(tbf, LOGL_DEBUG,
476 "RX: [PCU <- BTS] EGPRS Packet Downlink Ack/Nack\n");
477
478 LOGP(DRLCMAC, LOGL_DEBUG, "EGPRS ACK/NACK: "
479 "ut: %d, final: %d, bow: %d, eow: %d, ssn: %d, have_crbb: %d, "
480 "urbb_len:%d, %p, %p, %d, %d, win: %d-%d, urbb: %s\n",
481 (int)ack_nack->EGPRS_AckNack.UnionType,
482 (int)ack_nack->EGPRS_AckNack.Desc.FINAL_ACK_INDICATION,
483 (int)ack_nack->EGPRS_AckNack.Desc.BEGINNING_OF_WINDOW,
484 (int)ack_nack->EGPRS_AckNack.Desc.END_OF_WINDOW,
485 (int)ack_nack->EGPRS_AckNack.Desc.STARTING_SEQUENCE_NUMBER,
486 (int)ack_nack->EGPRS_AckNack.Desc.Exist_CRBB,
487 (int)ack_nack->EGPRS_AckNack.Desc.URBB_LENGTH,
488 (void *)&ack_nack->EGPRS_AckNack.UnionType,
489 (void *)&ack_nack->EGPRS_AckNack.Desc,
490 (int)offsetof(EGPRS_AckNack_t, Desc),
491 (int)offsetof(EGPRS_AckNack_w_len_t, Desc),
492 tbf->window()->v_a(),
493 tbf->window()->v_s(),
494 osmo_hexdump((const uint8_t *)&ack_nack->EGPRS_AckNack.Desc.URBB,
495 sizeof(ack_nack->EGPRS_AckNack.Desc.URBB)));
496
497 bits.data = bits_data;
498 bits.data_len = sizeof(bits_data);
499 bits.cur_bit = 0;
500
501 num_blocks = Decoding::decode_egprs_acknack_bits(
502 &ack_nack->EGPRS_AckNack.Desc, &bits,
503 &bsn_begin, &bsn_end, tbf->window());
504
505 LOGP(DRLCMAC, LOGL_DEBUG,
506 "Got EGPRS DL ACK bitmap: SSN: %d, BSN %d to %d - 1 (%d blocks), "
507 "\"%s\"\n",
508 ack_nack->EGPRS_AckNack.Desc.STARTING_SEQUENCE_NUMBER,
509 bsn_begin, bsn_end, num_blocks,
510 (Decoding::extract_rbb(&bits, show_bits), show_bits)
511 );
512
513 rc = tbf->rcvd_dl_ack(
514 ack_nack->EGPRS_AckNack.Desc.FINAL_ACK_INDICATION,
515 bsn_begin, &bits);
516 if (rc == 1) {
517 tbf_free(tbf);
518 return;
519 }
520
521 /* check for channel request */
522 if (ack_nack->Exist_ChannelRequestDescription)
523 sched_ul_ass_or_rej(bts(), bts_data(), tbf);
524
525 /* get measurements */
526 if (tbf->ms()) {
527 /* TODO: Implement Measurements parsing for EGPRS */
528 /*
529 get_meas(&meas, &ack_nack->Channel_Quality_Report);
530 tbf->ms()->update_l1_meas(&meas);
531 */
532 }
533}
534
535void gprs_rlcmac_pdch::rcv_resource_request(Packet_Resource_Request_t *request, uint32_t fn)
536{
537 struct gprs_rlcmac_sba *sba;
538
539 if (request->ID.UnionType) {
540 struct gprs_rlcmac_ul_tbf *ul_tbf = NULL;
541 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
542 uint32_t tlli = request->ID.u.TLLI;
543 uint8_t ms_class = 0;
544 uint8_t egprs_ms_class = 0;
545 uint8_t ta = GSM48_TA_INVALID;
546 struct pcu_l1_meas meas;
547
548 GprsMs *ms = bts()->ms_by_tlli(tlli);
549 /* Keep the ms, even if it gets idle temporarily */
550 GprsMs::Guard guard(ms);
551
552 if (ms) {
553 ul_tbf = ms->ul_tbf();
554 dl_tbf = ms->dl_tbf();
555 ta = ms->ta();
556 }
557
558 /* We got a RACH so the MS was in packet idle mode and thus
559 * didn't have any active TBFs */
560 if (ul_tbf) {
561 LOGPTBFUL(ul_tbf, LOGL_NOTICE,
562 "Got RACH from TLLI=0x%08x while TBF still exists. Killing pending UL TBF\n",
563 tlli);
564 tbf_free(ul_tbf);
565 ul_tbf = NULL;
566 }
567
568 if (dl_tbf) {
569 LOGPTBFUL(dl_tbf, LOGL_NOTICE,
570 "Got RACH from TLLI=0x%08x while TBF still exists. Release pending DL TBF\n",
571 tlli);
572 tbf_free(dl_tbf);
573 dl_tbf = NULL;
574 }
575 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF "
576 "in packet resource request of single "
577 "block, so we provide one:\n");
578 sba = bts()->sba()->find(this, fn);
579 if (!sba) {
580 LOGP(DRLCMAC, LOGL_NOTICE, "MS requests UL TBF "
581 "in packet resource request of single "
582 "block, but there is no resource request "
583 "scheduled!\n");
584 } else {
585 ta = sba->ta;
586 bts()->sba()->free_sba(sba);
587 }
588 if (request->Exist_MS_Radio_Access_capability) {
589 ms_class = Decoding::get_ms_class_by_capability(
590 &request->MS_Radio_Access_capability);
591 egprs_ms_class =
592 Decoding::get_egprs_ms_class_by_capability(
593 &request->MS_Radio_Access_capability);
594 }
595 if (!ms_class)
596 LOGP(DRLCMAC, LOGL_NOTICE, "MS does not give us a class.\n");
597 if (egprs_ms_class)
598 LOGP(DRLCMAC, LOGL_NOTICE,
599 "MS supports EGPRS multislot class %d.\n",
600 egprs_ms_class);
601 ul_tbf = tbf_alloc_ul(bts_data(), trx_no(), ms_class,
602 egprs_ms_class, tlli, ta, ms);
603
604 if (!ul_tbf) {
605 handle_tbf_reject(bts_data(), ms, tlli,
606 trx_no(), ts_no);
607 return;
608 }
609
610 /* set control ts to current MS's TS, until assignment complete */
611 LOGPTBF(ul_tbf, LOGL_DEBUG, "change control TS %d -> %d until assinment is complete.\n",
612 ul_tbf->control_ts, ts_no);
613
614 ul_tbf->control_ts = ts_no;
615 /* schedule uplink assignment */
616 TBF_SET_ASS_STATE_UL(ul_tbf, GPRS_RLCMAC_UL_ASS_SEND_ASS);
617
618 /* get capabilities */
619 if (ul_tbf->ms())
620 ul_tbf->ms()->set_egprs_ms_class(egprs_ms_class);
621
622 /* get measurements */
623 if (ul_tbf->ms()) {
624 get_meas(&meas, request);
625 ul_tbf->ms()->update_l1_meas(&meas);
626 }
627 return;
628 }
629
630 if (request->ID.u.Global_TFI.UnionType) {
631 struct gprs_rlcmac_dl_tbf *dl_tbf;
632 int8_t tfi = request->ID.u.Global_TFI.u.DOWNLINK_TFI;
633 dl_tbf = bts()->dl_tbf_by_tfi(tfi, trx_no(), ts_no);
634 if (!dl_tbf) {
635 LOGP(DRLCMAC, LOGL_NOTICE, "PACKET RESSOURCE REQ unknown downlink TFI=%d\n", tfi);
636 return;
637 }
638 LOGPTBFDL(dl_tbf, LOGL_ERROR,
639 "RX: [PCU <- BTS] FIXME: Packet resource request\n");
640
641 /* Reset N3101 counter: */
Max847ed9f2018-02-20 18:16:11 +0100642 dl_tbf->n_reset(N3101);
Max6dc90b82018-02-19 17:17:28 +0100643 } else {
644 struct gprs_rlcmac_ul_tbf *ul_tbf;
645 int8_t tfi = request->ID.u.Global_TFI.u.UPLINK_TFI;
646 ul_tbf = bts()->ul_tbf_by_tfi(tfi, trx_no(), ts_no);
647 if (!ul_tbf) {
648 LOGP(DRLCMAC, LOGL_NOTICE, "PACKET RESSOURCE REQ unknown uplink TFI=%d\n", tfi);
649 return;
650 }
651 LOGPTBFUL(ul_tbf, LOGL_ERROR,
652 "RX: [PCU <- BTS] FIXME: Packet resource request\n");
653
654 /* Reset N3101 counter: */
Max847ed9f2018-02-20 18:16:11 +0100655 ul_tbf->n_reset(N3101);
Max6dc90b82018-02-19 17:17:28 +0100656 }
657}
658
659void gprs_rlcmac_pdch::rcv_measurement_report(Packet_Measurement_Report_t *report, uint32_t fn)
660{
661 struct gprs_rlcmac_sba *sba;
662
663 sba = bts()->sba()->find(this, fn);
664 if (!sba) {
665 LOGP(DRLCMAC, LOGL_NOTICE, "MS send measurement "
666 "in packet resource request of single "
667 "block, but there is no resource request "
668 "scheduled! TLLI=0x%08x\n", report->TLLI);
669 } else {
670 GprsMs *ms = bts()->ms_store().get_ms(report->TLLI);
671 if (!ms)
672 LOGP(DRLCMAC, LOGL_NOTICE, "MS send measurement "
673 "but TLLI 0x%08x is unknown\n", report->TLLI);
674 else
675 ms->set_ta(sba->ta);
676
677 bts()->sba()->free_sba(sba);
678 }
679 gprs_rlcmac_meas_rep(report);
680}
681
682/* Received Uplink RLC control block. */
683int gprs_rlcmac_pdch::rcv_control_block(
684 const uint8_t *data, uint8_t data_len, bitvec *rlc_block, uint32_t fn)
685{
686 RlcMacUplink_t * ul_control_block = (RlcMacUplink_t *)talloc_zero(tall_pcu_ctx, RlcMacUplink_t);
687 LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ RX : Uplink Control Block +++++++++++++++++++++++++\n");
688 decode_gsm_rlcmac_uplink(rlc_block, ul_control_block);
689 LOGPC(DCSN1, LOGL_NOTICE, "\n");
690 LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- RX : Uplink Control Block -------------------------\n");
691
692 if (ul_control_block->u.MESSAGE_TYPE == MT_PACKET_UPLINK_DUMMY_CONTROL_BLOCK)
693 bts()->send_gsmtap(PCU_GSMTAP_C_UL_DUMMY, true, trx_no(), ts_no, GSMTAP_CHANNEL_PACCH, fn, data, data_len);
694 else
695 bts()->send_gsmtap(PCU_GSMTAP_C_UL_CTRL, true, trx_no(), ts_no, GSMTAP_CHANNEL_PACCH, fn, data, data_len);
696
697 bts()->rlc_rcvd_control();
698 switch (ul_control_block->u.MESSAGE_TYPE) {
699 case MT_PACKET_CONTROL_ACK:
700 rcv_control_ack(&ul_control_block->u.Packet_Control_Acknowledgement, fn);
701 break;
702 case MT_PACKET_DOWNLINK_ACK_NACK:
703 rcv_control_dl_ack_nack(&ul_control_block->u.Packet_Downlink_Ack_Nack, fn);
704 break;
705 case MT_EGPRS_PACKET_DOWNLINK_ACK_NACK:
706 rcv_control_egprs_dl_ack_nack(&ul_control_block->u.Egprs_Packet_Downlink_Ack_Nack, fn);
707 break;
708 case MT_PACKET_RESOURCE_REQUEST:
709 rcv_resource_request(&ul_control_block->u.Packet_Resource_Request, fn);
710 break;
711 case MT_PACKET_MEASUREMENT_REPORT:
712 rcv_measurement_report(&ul_control_block->u.Packet_Measurement_Report, fn);
713 break;
714 case MT_PACKET_UPLINK_DUMMY_CONTROL_BLOCK:
715 /* ignoring it. change the SI to not force sending these? */
716 break;
717 default:
718 bts()->decode_error();
719 LOGP(DRLCMAC, LOGL_NOTICE,
720 "RX: [PCU <- BTS] unknown control block(%d) received\n",
721 ul_control_block->u.MESSAGE_TYPE);
722 }
723 talloc_free(ul_control_block);
724 return 1;
725}
726
727/* received RLC/MAC block from L1 */
728int gprs_rlcmac_pdch::rcv_block(uint8_t *data, uint8_t len, uint32_t fn,
729 struct pcu_l1_meas *meas)
730{
731 GprsCodingScheme cs = GprsCodingScheme::getBySizeUL(len);
732 if (!cs) {
733 bts()->decode_error();
734 LOGP(DRLCMACUL, LOGL_ERROR, "Dropping data block with invalid"
735 "length: %d)\n", len);
736 return -EINVAL;
737 }
738
739 bts()->rlc_ul_bytes(len);
740
741 LOGP(DRLCMACUL, LOGL_DEBUG, "Got RLC block, coding scheme: %s, "
Max136ebcc2019-03-05 14:59:03 +0100742 "length: %d (%d))\n", mcs_name(cs), len, cs.usedSizeUL());
Max6dc90b82018-02-19 17:17:28 +0100743
744 if (cs.isGprs())
745 return rcv_block_gprs(data, len, fn, meas, cs);
746
747 if (cs.isEgprs())
748 return rcv_data_block(data, len, fn, meas, cs);
749
750 bts()->decode_error();
751 LOGP(DRLCMACUL, LOGL_ERROR, "Unsupported coding scheme %s\n",
Max136ebcc2019-03-05 14:59:03 +0100752 mcs_name(cs));
Max6dc90b82018-02-19 17:17:28 +0100753 return -EINVAL;
754}
755
756/*! \brief process egprs and gprs data blocks */
757int gprs_rlcmac_pdch::rcv_data_block(uint8_t *data, uint8_t data_len, uint32_t fn,
758 struct pcu_l1_meas *meas, GprsCodingScheme cs)
759{
760 int rc;
761 struct gprs_rlc_data_info rlc_dec;
762 struct gprs_rlcmac_ul_tbf *tbf;
763 unsigned len = cs.sizeUL();
764
765 /* These are always data blocks, since EGPRS still uses CS-1 for
766 * control blocks (see 44.060, section 10.3, 1st par.)
767 */
768 if (cs.isEgprs()) {
769 if (!bts()->bts_data()->egprs_enabled) {
770 LOGP(DRLCMACUL, LOGL_ERROR,
771 "Got %s RLC block but EGPRS is not enabled\n",
Max136ebcc2019-03-05 14:59:03 +0100772 mcs_name(cs));
Max6dc90b82018-02-19 17:17:28 +0100773 return -EINVAL;
774 }
775 bts()->send_gsmtap(PCU_GSMTAP_C_UL_DATA_EGPRS, true, trx_no(), ts_no, GSMTAP_CHANNEL_PDTCH, fn,
776 data, data_len);
777 } else {
778 bts()->send_gsmtap(PCU_GSMTAP_C_UL_DATA_GPRS, true, trx_no(), ts_no, GSMTAP_CHANNEL_PDTCH, fn,
779 data, data_len);
780 }
781
782 LOGP(DRLCMACUL, LOGL_DEBUG, " UL data: %s\n", osmo_hexdump(data, len));
783
784 rc = Decoding::rlc_parse_ul_data_header(&rlc_dec, data, cs);
785 if (rc < 0) {
786 LOGP(DRLCMACUL, LOGL_ERROR,
787 "Got %s RLC block but header parsing has failed\n",
Max136ebcc2019-03-05 14:59:03 +0100788 mcs_name(cs));
Max6dc90b82018-02-19 17:17:28 +0100789 bts()->decode_error();
790 return rc;
791 }
792
793 LOGP(DRLCMACUL, LOGL_INFO,
794 "Got %s RLC block: "
795 "R=%d, SI=%d, TFI=%d, CPS=%d, RSB=%d, "
796 "rc=%d\n",
Max136ebcc2019-03-05 14:59:03 +0100797 mcs_name(cs),
Max6dc90b82018-02-19 17:17:28 +0100798 rlc_dec.r, rlc_dec.si, rlc_dec.tfi, rlc_dec.cps, rlc_dec.rsb,
799 rc);
800
801 /* find TBF inst from given TFI */
802 tbf = ul_tbf_by_tfi(rlc_dec.tfi);
803 if (!tbf) {
804 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA unknown TFI=%d\n",
805 rlc_dec.tfi);
806 return 0;
807 }
808
809 /* Reset N3101 counter: */
Max847ed9f2018-02-20 18:16:11 +0100810 tbf->n_reset(N3101);
Max6dc90b82018-02-19 17:17:28 +0100811
812 return tbf->rcv_data_block_acknowledged(&rlc_dec, data, meas);
813}
814
815int gprs_rlcmac_pdch::rcv_block_gprs(uint8_t *data, uint8_t data_len, uint32_t fn,
816 struct pcu_l1_meas *meas, GprsCodingScheme cs)
817{
818 unsigned payload = data[0] >> 6;
819 bitvec *block;
820 int rc = 0;
821 unsigned len = cs.maxBytesUL();
822
823 switch (payload) {
824 case GPRS_RLCMAC_DATA_BLOCK:
825 rc = rcv_data_block(data, data_len, fn, meas, cs);
826 break;
827 case GPRS_RLCMAC_CONTROL_BLOCK:
828 block = bitvec_alloc(len, tall_pcu_ctx);
829 if (!block)
830 return -ENOMEM;
831 bitvec_unpack(block, data);
832 rc = rcv_control_block(data, data_len, block, fn);
833 bitvec_free(block);
834 break;
835 case GPRS_RLCMAC_CONTROL_BLOCK_OPT:
836 LOGP(DRLCMAC, LOGL_NOTICE, "GPRS_RLCMAC_CONTROL_BLOCK_OPT block payload is not supported.\n");
837 break;
838 default:
839 LOGP(DRLCMAC, LOGL_NOTICE, "Unknown RLCMAC block payload(%u).\n", payload);
840 rc = -EINVAL;
841 }
842
843 return rc;
844}
845
846gprs_rlcmac_tbf *gprs_rlcmac_pdch::tbf_from_list_by_tfi(
847 LListHead<gprs_rlcmac_tbf> *tbf_list, uint8_t tfi,
848 enum gprs_rlcmac_tbf_direction dir)
849{
850 gprs_rlcmac_tbf *tbf;
851 LListHead<gprs_rlcmac_tbf> *pos;
852
853 llist_for_each(pos, tbf_list) {
854 tbf = pos->entry();
855 if (tbf->tfi() != tfi)
856 continue;
857 if (!tbf->pdch[ts_no])
858 continue;
859 return tbf;
860 }
861 return NULL;
862}
863
864gprs_rlcmac_ul_tbf *gprs_rlcmac_pdch::ul_tbf_by_tfi(uint8_t tfi)
865{
866 return as_ul_tbf(tbf_by_tfi(tfi, GPRS_RLCMAC_UL_TBF));
867}
868
869gprs_rlcmac_dl_tbf *gprs_rlcmac_pdch::dl_tbf_by_tfi(uint8_t tfi)
870{
871 return as_dl_tbf(tbf_by_tfi(tfi, GPRS_RLCMAC_DL_TBF));
872}
873
874/* lookup TBF Entity (by TFI) */
875gprs_rlcmac_tbf *gprs_rlcmac_pdch::tbf_by_tfi(uint8_t tfi,
876 enum gprs_rlcmac_tbf_direction dir)
877{
878 struct gprs_rlcmac_tbf *tbf;
879
880 if (tfi >= 32)
881 return NULL;
882
883 tbf = m_tbfs[dir][tfi];
884
885 if (!tbf)
886 return NULL;
887
888 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING)) {
889 return tbf;
890 }
891
892 return NULL;
893}
894
895void gprs_rlcmac_pdch::attach_tbf(gprs_rlcmac_tbf *tbf)
896{
897 gprs_rlcmac_ul_tbf *ul_tbf;
898
899 if (m_tbfs[tbf->direction][tbf->tfi()])
900 LOGP(DRLCMAC, LOGL_ERROR, "PDCH(TS %d, TRX %d): "
901 "%s has not been detached, overwriting it\n",
902 ts_no, trx_no(),
903 m_tbfs[tbf->direction][tbf->tfi()]->name());
904
905 m_num_tbfs[tbf->direction] += 1;
906 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
907 ul_tbf = as_ul_tbf(tbf);
908 m_assigned_usf |= 1 << ul_tbf->m_usf[ts_no];
909 }
910 m_assigned_tfi[tbf->direction] |= 1UL << tbf->tfi();
911 m_tbfs[tbf->direction][tbf->tfi()] = tbf;
912
913 LOGP(DRLCMAC, LOGL_INFO, "PDCH(TS %d, TRX %d): Attaching %s, %d TBFs, "
914 "USFs = %02x, TFIs = %08x.\n",
915 ts_no, trx_no(), tbf->name(), m_num_tbfs[tbf->direction],
916 m_assigned_usf, m_assigned_tfi[tbf->direction]);
917}
918
919void gprs_rlcmac_pdch::detach_tbf(gprs_rlcmac_tbf *tbf)
920{
921 gprs_rlcmac_ul_tbf *ul_tbf;
922
923 OSMO_ASSERT(m_num_tbfs[tbf->direction] > 0);
924
925 m_num_tbfs[tbf->direction] -= 1;
926 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
927 ul_tbf = as_ul_tbf(tbf);
928 m_assigned_usf &= ~(1 << ul_tbf->m_usf[ts_no]);
929 }
930 m_assigned_tfi[tbf->direction] &= ~(1UL << tbf->tfi());
931 m_tbfs[tbf->direction][tbf->tfi()] = NULL;
932
933 LOGP(DRLCMAC, LOGL_INFO, "PDCH(TS %d, TRX %d): Detaching %s, %d TBFs, "
934 "USFs = %02x, TFIs = %08x.\n",
935 ts_no, trx_no(), tbf->name(), m_num_tbfs[tbf->direction],
936 m_assigned_usf, m_assigned_tfi[tbf->direction]);
937}
938
939void gprs_rlcmac_pdch::reserve(enum gprs_rlcmac_tbf_direction dir)
940{
941 m_num_reserved[dir] += 1;
942}
943
944void gprs_rlcmac_pdch::unreserve(enum gprs_rlcmac_tbf_direction dir)
945{
946 OSMO_ASSERT(m_num_reserved[dir] > 0);
947 m_num_reserved[dir] -= 1;
948}
949
950inline BTS *gprs_rlcmac_pdch::bts() const
951{
952 return trx->bts;
953}
954
955uint8_t gprs_rlcmac_pdch::trx_no() const
956{
957 return trx->trx_no;
958}
959
960inline gprs_rlcmac_bts *gprs_rlcmac_pdch::bts_data() const
961{
962 return trx->bts->bts_data();
963}
964