blob: 9899580eaa6dc6c38a813e773f8a7c4e251eb2cb [file] [log] [blame]
Daniel Willmannca102af2014-08-08 12:14:12 +02001/* Copied from tbf.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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 General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include <bts.h>
23#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020024#include <tbf_ul.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020025#include <rlc.h>
26#include <encoding.h>
27#include <gprs_rlcmac.h>
28#include <gprs_debug.h>
29#include <gprs_bssgp_pcu.h>
30#include <decoding.h>
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +020031#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010032#include <gprs_coding_scheme.h>
33#include <gprs_ms.h>
34#include <llc.h>
sivasankari8adfcd02017-01-16 15:41:21 +053035#include "pcu_utils.h"
36
Daniel Willmannca102af2014-08-08 12:14:12 +020037extern "C" {
38#include <osmocom/core/msgb.h>
39#include <osmocom/core/talloc.h>
Max1187a772018-01-26 13:31:42 +010040 #include <osmocom/core/bitvec.h>
41 #include <osmocom/core/logging.h>
42 #include <osmocom/core/rate_ctr.h>
43 #include <osmocom/core/utils.h>
44 #include <osmocom/gprs/gprs_bssgp_bss.h>
45 #include <osmocom/gprs/protocol/gsm_08_18.h>
46 #include <osmocom/gsm/tlv.h>
Max136ebcc2019-03-05 14:59:03 +010047 #include "coding_scheme.h"
Daniel Willmannca102af2014-08-08 12:14:12 +020048}
49
50#include <errno.h>
51#include <string.h>
52
53/* After receiving these frames, we send ack/nack. */
54#define SEND_ACK_AFTER_FRAMES 20
55
56extern void *tall_pcu_ctx;
57
Pau Espin Pedrolbddf1ad2019-09-26 14:41:18 +020058gprs_rlcmac_ul_tbf::gprs_rlcmac_ul_tbf(BTS *bts_) :
59 gprs_rlcmac_tbf(bts_, GPRS_RLCMAC_UL_TBF),
60 m_rx_counter(0),
61 m_contention_resolution_done(0),
62 m_final_ack_sent(0),
63 m_ul_gprs_ctrs(NULL),
64 m_ul_egprs_ctrs(NULL)
65{
66 memset(&m_usf, 0, sizeof(m_usf));
67}
68
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010069/*
70 * Store received block data in LLC message(s) and forward to SGSN
71 * if complete.
72 */
73int gprs_rlcmac_ul_tbf::assemble_forward_llc(const gprs_rlc_data *_data)
74{
75 const uint8_t *data = _data->block;
76 uint8_t len = _data->len;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +010077 const struct gprs_rlc_data_block_info *rdbi = &_data->block_info;
Aravind Sirsikar91495522016-07-12 14:17:12 +053078 GprsCodingScheme cs = _data->cs_last;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010079
80 Decoding::RlcData frames[16], *frame;
81 int i, num_frames = 0;
82 uint32_t dummy_tlli;
83
Max0524e382018-01-19 18:22:25 +010084 LOGPTBFUL(this, LOGL_DEBUG, "Assembling frames: (len=%d)\n", len);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010085
86 num_frames = Decoding::rlc_data_from_ul_data(
Alexander Couzensce936f32016-05-24 14:45:41 +020087 rdbi, cs, data, &(frames[0]), ARRAY_SIZE(frames),
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010088 &dummy_tlli);
89
90 /* create LLC frames */
91 for (i = 0; i < num_frames; i++) {
92 frame = frames + i;
93
Aravind Sirsikar22a90192016-09-15 17:24:49 +053094 if (frame->length) {
Pau Espin Pedrola107f8f2020-05-12 22:32:24 +020095 bts->do_rate_ctr_add(CTR_RLC_UL_PAYLOAD_BYTES, frame->length);
Alexander Couzens7fdbf892016-05-21 19:45:23 +020096
Max0524e382018-01-19 18:22:25 +010097 LOGPTBFUL(this, LOGL_DEBUG, "Frame %d "
Aravind Sirsikar22a90192016-09-15 17:24:49 +053098 "starts at offset %d, "
99 "length=%d, is_complete=%d\n",
100 i + 1, frame->offset, frame->length,
101 frame->is_complete);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100102
Aravind Sirsikar22a90192016-09-15 17:24:49 +0530103 m_llc.append_frame(data + frame->offset, frame->length);
104 m_llc.consume(frame->length);
105 }
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100106
107 if (frame->is_complete) {
108 /* send frame to SGSN */
Max0fdaa9d2018-01-30 16:03:10 +0100109 LOGPTBFUL(this, LOGL_DEBUG, "complete UL frame len=%d\n", m_llc.frame_length());
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100110 snd_ul_ud();
Pau Espin Pedrola107f8f2020-05-12 22:32:24 +0200111 bts->do_rate_ctr_add(CTR_LLC_UL_BYTES, m_llc.frame_length());
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100112 m_llc.reset();
113 }
114 }
115
116 return 0;
117}
118
Maxfd13f6c2017-07-07 14:29:36 +0200119bool gprs_rlcmac_ul_tbf::ctrl_ack_to_toggle()
120{
Max8dce1de2018-01-02 14:17:04 +0100121 if (check_n_clear(GPRS_RLCMAC_FLAG_TO_UL_ACK))
Maxfd13f6c2017-07-07 14:29:36 +0200122 return true; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was set, now cleared */
Maxfd13f6c2017-07-07 14:29:36 +0200123
124 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK);
125 return false; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was unset, now set */
126}
127
128bool gprs_rlcmac_ul_tbf::handle_ctrl_ack()
129{
130 /* check if this control ack belongs to packet uplink ack */
Max088c7df2018-01-23 20:16:23 +0100131 if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
132 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
Maxfd13f6c2017-07-07 14:29:36 +0200133 return true;
134 }
135
136 return false;
137}
Daniel Willmannca102af2014-08-08 12:14:12 +0200138
Jacob Erlbeck5a3c84d2016-01-22 17:25:38 +0100139struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn, uint8_t ts)
Daniel Willmannca102af2014-08-08 12:14:12 +0200140{
141 int final = (state_is(GPRS_RLCMAC_FINISHED));
142 struct msgb *msg;
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100143 int rc;
144 unsigned int rrbp = 0;
145 uint32_t new_poll_fn = 0;
Daniel Willmannca102af2014-08-08 12:14:12 +0200146
147 if (final) {
Maxcac6b662018-01-24 11:00:17 +0100148 if (poll_scheduled() && ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
Maxc21f0072017-12-15 17:36:45 +0100149 LOGPTBFUL(this, LOGL_DEBUG,
150 "Polling is already scheduled, so we must wait for the final uplink ack...\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200151 return NULL;
152 }
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100153
154 rc = check_polling(fn, ts, &new_poll_fn, &rrbp);
155 if (rc < 0)
Daniel Willmannca102af2014-08-08 12:14:12 +0200156 return NULL;
Daniel Willmannca102af2014-08-08 12:14:12 +0200157 }
158
159 msg = msgb_alloc(23, "rlcmac_ul_ack");
160 if (!msg)
161 return NULL;
Alexander Couzensccde5c92017-02-04 03:10:08 +0100162 bitvec *ack_vec = bitvec_alloc(23, tall_pcu_ctx);
Daniel Willmannca102af2014-08-08 12:14:12 +0200163 if (!ack_vec) {
164 msgb_free(msg);
165 return NULL;
166 }
Max7426c5f2019-02-18 20:42:42 +0100167 bitvec_unhex(ack_vec, DUMMY_VEC);
Alexander Couzens3a499f32019-06-16 15:25:30 +0200168 Encoding::write_packet_uplink_ack(ack_vec, this, final, rrbp);
Daniel Willmannca102af2014-08-08 12:14:12 +0200169 bitvec_pack(ack_vec, msgb_put(msg, 23));
170 bitvec_free(ack_vec);
Daniel Willmannca102af2014-08-08 12:14:12 +0200171
172 /* now we must set this flag, so we are allowed to assign downlink
173 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
174 m_contention_resolution_done = 1;
175
176 if (final) {
Maxf60cf622017-07-10 14:40:09 +0200177 set_polling(new_poll_fn, ts, GPRS_RLCMAC_POLL_UL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200178 /* waiting for final acknowledge */
Daniel Willmannca102af2014-08-08 12:14:12 +0200179 m_final_ack_sent = 1;
180 } else
Max088c7df2018-01-23 20:16:23 +0100181 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
Daniel Willmannca102af2014-08-08 12:14:12 +0200182
183 return msg;
184}
185
Alexander Couzens2fcfc292016-05-24 14:40:03 +0200186/*! \brief receive data from PDCH/L1 */
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100187int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100188 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeckfc1b3e62016-01-11 09:58:11 +0100189 uint8_t *data, struct pcu_l1_meas *meas)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100190{
191 int8_t rssi = meas->have_rssi ? meas->rssi : 0;
192
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100193 const uint16_t ws = m_window.ws();
194
195 this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
196
Max0524e382018-01-19 18:22:25 +0100197 LOGPTBFUL(this, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100198 "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(),
199 this->m_window.v_r());
200
201 /* process RSSI */
202 gprs_rlcmac_rssi(this, rssi);
203
204 /* store measurement values */
205 if (ms())
206 ms()->update_l1_meas(meas);
207
208 uint32_t new_tlli = 0;
209 unsigned int block_idx;
210
211 /* restart T3169 */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200212 T_START(this, T3169, 3169, "acked (data)", true);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100213
214 /* Increment RX-counter */
215 this->m_rx_counter++;
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530216 update_coding_scheme_counter_ul(rlc->cs);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100217 /* Loop over num_blocks */
218 for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) {
219 int num_chunks;
220 uint8_t *rlc_data;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100221 const struct gprs_rlc_data_block_info *rdbi =
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100222 &rlc->block_info[block_idx];
223 bool need_rlc_data = false;
224 struct gprs_rlc_data *block;
225
Max0524e382018-01-19 18:22:25 +0100226 LOGPTBFUL(this, LOGL_DEBUG,
227 "Got %s RLC data block: CV=%d, BSN=%d, SPB=%d, PI=%d, E=%d, TI=%d, bitoffs=%d\n",
Max136ebcc2019-03-05 14:59:03 +0100228 mcs_name(rlc->cs),
Max0524e382018-01-19 18:22:25 +0100229 rdbi->cv, rdbi->bsn, rdbi->spb,
230 rdbi->pi, rdbi->e, rdbi->ti,
231 rlc->data_offs_bits[block_idx]);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100232
233 /* Check whether the block needs to be decoded */
234
235 if (!m_window.is_in_window(rdbi->bsn)) {
Max0524e382018-01-19 18:22:25 +0100236 LOGPTBFUL(this, LOGL_DEBUG, "BSN %d out of window %d..%d (it's normal)\n",
237 rdbi->bsn,
238 m_window.v_q(), m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100239 } else if (m_window.is_received(rdbi->bsn)) {
Max0524e382018-01-19 18:22:25 +0100240 LOGPTBFUL(this, LOGL_DEBUG,
241 "BSN %d already received\n", rdbi->bsn);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100242 } else {
243 need_rlc_data = true;
244 }
245
246 if (!is_tlli_valid()) {
247 if (!rdbi->ti) {
Max0524e382018-01-19 18:22:25 +0100248 LOGPTBFUL(this, LOGL_NOTICE, "Missing TLLI within UL DATA.\n");
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100249 continue;
250 }
251 need_rlc_data = true;
252 }
253
254 if (!need_rlc_data)
255 continue;
256
257 /* Store block and meta info to BSN buffer */
258
Max0524e382018-01-19 18:22:25 +0100259 LOGPTBFUL(this, LOGL_DEBUG, "BSN %d storing in window (%d..%d)\n",
260 rdbi->bsn, m_window.v_q(),
261 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100262 block = m_rlc.block(rdbi->bsn);
Aravind Sirsikar550a5412016-06-14 18:59:18 +0530263 OSMO_ASSERT(rdbi->data_len <= sizeof(block->block));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100264 rlc_data = &(block->block[0]);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100265
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530266 if (rdbi->spb) {
267 egprs_rlc_ul_reseg_bsn_state assemble_status;
268
269 assemble_status = handle_egprs_ul_spb(rlc,
270 block, data, block_idx);
271
272 if (assemble_status != EGPRS_RESEG_DEFAULT)
273 return 0;
274 } else {
275 block->block_info = *rdbi;
276 block->cs_last = rlc->cs;
277 block->len =
278 Decoding::rlc_copy_to_aligned_buffer(rlc,
279 block_idx, data, rlc_data);
280 }
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100281
Max0524e382018-01-19 18:22:25 +0100282 LOGPTBFUL(this, LOGL_DEBUG,
283 "data_length=%d, data=%s\n",
284 block->len, osmo_hexdump(rlc_data, block->len));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100285 /* Get/Handle TLLI */
286 if (rdbi->ti) {
287 num_chunks = Decoding::rlc_data_from_ul_data(
288 rdbi, rlc->cs, rlc_data, NULL, 0, &new_tlli);
289
290 if (num_chunks < 0) {
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200291 bts->do_rate_ctr_inc(CTR_DECODE_ERRORS);
Max0524e382018-01-19 18:22:25 +0100292 LOGPTBFUL(this, LOGL_NOTICE,
293 "Failed to decode TLLI of %s UL DATA TFI=%d.\n",
Max136ebcc2019-03-05 14:59:03 +0100294 mcs_name(rlc->cs), rlc->tfi);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100295 m_window.invalidate_bsn(rdbi->bsn);
296 continue;
297 }
298 if (!this->is_tlli_valid()) {
299 if (!new_tlli) {
Max0524e382018-01-19 18:22:25 +0100300 LOGPTBFUL(this, LOGL_NOTICE,
301 "TLLI = 0 within UL DATA.\n");
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100302 m_window.invalidate_bsn(rdbi->bsn);
303 continue;
304 }
Max0524e382018-01-19 18:22:25 +0100305 LOGPTBFUL(this, LOGL_INFO,
306 "Decoded premier TLLI=0x%08x of UL DATA TFI=%d.\n",
Vadim Yanitskiy3f27fb52020-04-20 11:26:12 +0700307 new_tlli, rlc->tfi);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100308 set_tlli_from_ul(new_tlli);
309 } else if (new_tlli && new_tlli != tlli()) {
Max0524e382018-01-19 18:22:25 +0100310 LOGPTBFUL(this, LOGL_NOTICE,
311 "TLLI mismatch on UL DATA TFI=%d. (Ignoring due to contention resolution)\n",
312 rlc->tfi);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100313 m_window.invalidate_bsn(rdbi->bsn);
314 continue;
315 }
316 }
317
318 m_window.receive_bsn(rdbi->bsn);
319 }
320
321 /* Raise V(Q) if possible, and retrieve LLC frames from blocks.
322 * This is looped until there is a gap (non received block) or
323 * the window is empty.*/
324 const uint16_t v_q_beg = m_window.v_q();
325 const uint16_t count = m_window.raise_v_q();
326
327 /* Retrieve LLC frames from blocks that are ready */
328 for (uint16_t i = 0; i < count; ++i) {
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100329 uint16_t index = m_window.mod_sns(v_q_beg + i);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100330 assemble_forward_llc(m_rlc.block(index));
331 }
332
333 /* Check CV of last frame in buffer */
334 if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */
335 && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */
336 struct gprs_rlc_data *block =
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100337 m_rlc.block(m_window.mod_sns(m_window.v_r() - 1));
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100338 const struct gprs_rlc_data_block_info *rdbi =
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100339 &block->block_info;
340
Max0524e382018-01-19 18:22:25 +0100341 LOGPTBFUL(this, LOGL_DEBUG,
342 "No gaps in received block, last block: BSN=%d CV=%d\n",
343 rdbi->bsn, rdbi->cv);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100344 if (rdbi->cv == 0) {
Max0524e382018-01-19 18:22:25 +0100345 LOGPTBFUL(this, LOGL_DEBUG, "Finished with UL TBF\n");
Max2399b1d2018-01-12 15:48:12 +0100346 TBF_SET_STATE(this, GPRS_RLCMAC_FINISHED);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100347 /* Reset N3103 counter. */
Max847ed9f2018-02-20 18:16:11 +0100348 this->n_reset(N3103);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100349 }
350 }
351
352 /* If TLLI is included or if we received half of the window, we send
353 * an ack/nack */
354 maybe_schedule_uplink_acknack(rlc);
355
356 return 0;
357}
358
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100359void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100360 const gprs_rlc_data_info *rlc)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100361{
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200362 bool require_ack = false;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100363 bool have_ti = rlc->block_info[0].ti ||
364 (rlc->num_data_blocks > 1 && rlc->block_info[1].ti);
365
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200366 if (rlc->si) {
367 require_ack = true;
368 LOGPTBFUL(this, LOGL_NOTICE,
369 "Scheduling Ack/Nack, because MS is stalled.\n");
370 }
371 if (have_ti) {
372 require_ack = true;
373 LOGPTBFUL(this, LOGL_DEBUG,
374 "Scheduling Ack/Nack, because TLLI is included.\n");
375 }
376 if (state_is(GPRS_RLCMAC_FINISHED)) {
377 require_ack = true;
378 LOGPTBFUL(this, LOGL_DEBUG,
379 "Scheduling final Ack/Nack, because all data was received and last block has CV==0.\n");
380 }
381 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
382 require_ack = true;
383 LOGPTBFUL(this, LOGL_DEBUG,
384 "Scheduling Ack/Nack, because %d frames received.\n",
385 SEND_ACK_AFTER_FRAMES);
386 }
387
388 if (!require_ack)
389 return;
390
391 if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_NONE)) {
392 /* trigger sending at next RTS */
393 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_SEND_ACK);
394 } else {
395 /* already triggered */
396 LOGPTBFUL(this, LOGL_DEBUG,
397 "Sending Ack/Nack already scheduled, no need to re-schedule\n");
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100398 }
399}
400
Daniel Willmannca102af2014-08-08 12:14:12 +0200401/* Send Uplink unit-data to SGSN. */
402int gprs_rlcmac_ul_tbf::snd_ul_ud()
403{
404 uint8_t qos_profile[3];
405 struct msgb *llc_pdu;
406 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length();
407 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
408
409 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length());
410 if (!bctx) {
411 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
412 m_llc.reset_frame_space();
413 return -EIO;
414 }
Pau Espin Pedrol488aa292019-09-25 17:48:35 +0200415
Daniel Willmannca102af2014-08-08 12:14:12 +0200416 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
417 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length()));
418 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame);
419 qos_profile[0] = QOS_PROFILE >> 16;
420 qos_profile[1] = QOS_PROFILE >> 8;
421 qos_profile[2] = QOS_PROFILE;
422 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
423
424 m_llc.reset_frame_space();
425 return 0;
426}
427
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530428egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_second_seg(
429 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
430 uint8_t *data, const uint8_t block_idx)
431{
432 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
433 union split_block_status *spb_status = &block->spb_status;
434 uint8_t *rlc_data = &block->block[0];
435
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200436 bts->do_rate_ctr_inc(CTR_SPB_UL_SECOND_SEGMENT);
sivasankarida7250a2016-12-16 12:57:18 +0530437
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530438 if (spb_status->block_status_ul &
439 EGPRS_RESEG_FIRST_SEG_RXD) {
Max0524e382018-01-19 18:22:25 +0100440 LOGPTBFUL(this, LOGL_DEBUG,
441 "Second seg is received first seg is already present set the status to complete\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530442 spb_status->block_status_ul = EGPRS_RESEG_DEFAULT;
443
444 block->len += Decoding::rlc_copy_to_aligned_buffer(rlc,
445 block_idx, data, rlc_data + block->len);
446 block->block_info.data_len += rdbi->data_len;
447 } else if (spb_status->block_status_ul == EGPRS_RESEG_DEFAULT) {
Max0524e382018-01-19 18:22:25 +0100448 LOGPTBFUL(this, LOGL_DEBUG,
449 "Second seg is received first seg is not received set the status to second seg received\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530450
451 block->len = Decoding::rlc_copy_to_aligned_buffer(rlc,
452 block_idx, data,
453 rlc_data + rlc->block_info[block_idx].data_len);
454
455 spb_status->block_status_ul = EGPRS_RESEG_SECOND_SEG_RXD;
456 block->block_info = *rdbi;
457 }
458 return spb_status->block_status_ul;
459}
460
461egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_first_seg(
462 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
463 uint8_t *data, const uint8_t block_idx)
464{
465 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
466 uint8_t *rlc_data = &block->block[0];
467 union split_block_status *spb_status = &block->spb_status;
468
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200469 bts->do_rate_ctr_inc(CTR_SPB_UL_FIRST_SEGMENT);
sivasankarida7250a2016-12-16 12:57:18 +0530470
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530471 if (spb_status->block_status_ul & EGPRS_RESEG_SECOND_SEG_RXD) {
Max0524e382018-01-19 18:22:25 +0100472 LOGPTBFUL(this, LOGL_DEBUG,
473 "First seg is received second seg is already present set the status to complete\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530474
475 block->len += Decoding::rlc_copy_to_aligned_buffer(rlc,
476 block_idx, data, rlc_data);
477
478 block->block_info.data_len = block->len;
479 spb_status->block_status_ul = EGPRS_RESEG_DEFAULT;
480 } else if (spb_status->block_status_ul == EGPRS_RESEG_DEFAULT) {
Max0524e382018-01-19 18:22:25 +0100481 LOGPTBFUL(this, LOGL_DEBUG,
482 "First seg is received second seg is not received set the status to first seg received\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530483
484 spb_status->block_status_ul = EGPRS_RESEG_FIRST_SEG_RXD;
485 block->len = Decoding::rlc_copy_to_aligned_buffer(rlc,
486 block_idx, data, rlc_data);
487 block->block_info = *rdbi;
488 }
489 return spb_status->block_status_ul;
490}
491
492egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_spb(
493 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
494 uint8_t *data, const uint8_t block_idx)
495{
496 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
497
Max0524e382018-01-19 18:22:25 +0100498 LOGPTBFUL(this, LOGL_DEBUG,
499 "Got SPB(%d) cs(%s) data block with BSN (%d), TFI(%d).\n",
Max136ebcc2019-03-05 14:59:03 +0100500 rdbi->spb, mcs_name(rlc->cs), rdbi->bsn, rlc->tfi);
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530501
502 egprs_rlc_ul_reseg_bsn_state assemble_status = EGPRS_RESEG_INVALID;
503
504 /* Section 10.4.8b of 44.060*/
505 if (rdbi->spb == 2)
506 assemble_status = handle_egprs_ul_first_seg(rlc,
507 block, data, block_idx);
508 else if (rdbi->spb == 3)
509 assemble_status = handle_egprs_ul_second_seg(rlc,
510 block, data, block_idx);
511 else {
Max0524e382018-01-19 18:22:25 +0100512 LOGPTBFUL(this, LOGL_ERROR,
513 "spb(%d) Not supported SPB for this EGPRS configuration\n",
514 rdbi->spb);
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530515 }
516
517 /*
518 * When the block is successfully constructed out of segmented blocks
519 * upgrade the MCS to the type 2
520 */
521 if (assemble_status == EGPRS_RESEG_DEFAULT) {
Maxbea2edb2019-03-06 17:04:59 +0100522 switch (CodingScheme(rlc->cs)) {
523 case MCS3 :
524 block->cs_last = MCS6;
Max0524e382018-01-19 18:22:25 +0100525 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS6\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530526 break;
Maxbea2edb2019-03-06 17:04:59 +0100527 case MCS2 :
528 block->cs_last = MCS5;
Max0524e382018-01-19 18:22:25 +0100529 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS5\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530530 break;
Maxbea2edb2019-03-06 17:04:59 +0100531 case MCS1 :
Max0524e382018-01-19 18:22:25 +0100532 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS4\n");
Maxbea2edb2019-03-06 17:04:59 +0100533 block->cs_last = MCS4;
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530534 break;
535 default:
Max0524e382018-01-19 18:22:25 +0100536 LOGPTBFUL(this, LOGL_ERROR,
537 "cs(%s) Error in Upgrading to higher MCS\n",
Max136ebcc2019-03-05 14:59:03 +0100538 mcs_name(rlc->cs));
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530539 break;
540 }
541 }
542 return assemble_status;
543}
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530544
Maxfb59a932019-03-13 18:40:19 +0100545void gprs_rlcmac_ul_tbf::update_coding_scheme_counter_ul(enum CodingScheme cs)
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530546{
Maxfb59a932019-03-13 18:40:19 +0100547 switch (cs) {
548 case CS1:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200549 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS1);
Maxfb59a932019-03-13 18:40:19 +0100550 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS1]);
551 break;
552 case CS2:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200553 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS2);
Maxfb59a932019-03-13 18:40:19 +0100554 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS2]);
555 break;
556 case CS3:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200557 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS3);
Maxfb59a932019-03-13 18:40:19 +0100558 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS3]);
559 break;
560 case CS4:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200561 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS4);
Maxfb59a932019-03-13 18:40:19 +0100562 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS4]);
563 break;
564 case MCS1:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200565 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS1);
Maxfb59a932019-03-13 18:40:19 +0100566 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS1]);
567 break;
568 case MCS2:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200569 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS2);
Maxfb59a932019-03-13 18:40:19 +0100570 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS2]);
571 break;
572 case MCS3:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200573 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS3);
Maxfb59a932019-03-13 18:40:19 +0100574 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS3]);
575 break;
576 case MCS4:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200577 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS4);
Maxfb59a932019-03-13 18:40:19 +0100578 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS4]);
579 break;
580 case MCS5:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200581 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS5);
Maxfb59a932019-03-13 18:40:19 +0100582 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS5]);
583 break;
584 case MCS6:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200585 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS6);
Maxfb59a932019-03-13 18:40:19 +0100586 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS6]);
587 break;
588 case MCS7:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200589 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS7);
Maxfb59a932019-03-13 18:40:19 +0100590 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS7]);
591 break;
592 case MCS8:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200593 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS8);
Maxfb59a932019-03-13 18:40:19 +0100594 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS8]);
595 break;
596 case MCS9:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200597 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS9);
Maxfb59a932019-03-13 18:40:19 +0100598 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS9]);
599 break;
600 default:
601 LOGPTBFUL(this, LOGL_ERROR, "attempting to update rate counters for unsupported (M)CS %s\n",
602 mcs_name(cs));
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530603 }
604}
sivasankari8adfcd02017-01-16 15:41:21 +0530605
Max9d7357e2017-12-14 15:02:33 +0100606void gprs_rlcmac_ul_tbf::set_window_size()
sivasankari8adfcd02017-01-16 15:41:21 +0530607{
Max34513fe2018-12-11 16:47:30 +0100608 const struct gprs_rlcmac_bts *b = bts->bts_data();
609 uint16_t ws = egprs_window_size(b, ul_slots());
Max0524e382018-01-19 18:22:25 +0100610 LOGPTBFUL(this, LOGL_INFO, "setting EGPRS UL window size to %u, base(%u) slots(%u) ws_pdch(%u)\n",
Max34513fe2018-12-11 16:47:30 +0100611 ws, b->ws_base, pcu_bitcount(ul_slots()), b->ws_pdch);
sivasankari8adfcd02017-01-16 15:41:21 +0530612 m_window.set_ws(ws);
613}