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