blob: 31150801bb21f8e2e305d39efcc6c97d7790f943 [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_ms.h>
33#include <llc.h>
sivasankari8adfcd02017-01-16 15:41:21 +053034#include "pcu_utils.h"
35
Daniel Willmannca102af2014-08-08 12:14:12 +020036extern "C" {
37#include <osmocom/core/msgb.h>
38#include <osmocom/core/talloc.h>
Max1187a772018-01-26 13:31:42 +010039 #include <osmocom/core/bitvec.h>
40 #include <osmocom/core/logging.h>
41 #include <osmocom/core/rate_ctr.h>
42 #include <osmocom/core/utils.h>
43 #include <osmocom/gprs/gprs_bssgp_bss.h>
44 #include <osmocom/gprs/protocol/gsm_08_18.h>
45 #include <osmocom/gsm/tlv.h>
Max136ebcc2019-03-05 14:59:03 +010046 #include "coding_scheme.h"
Daniel Willmannca102af2014-08-08 12:14:12 +020047}
48
49#include <errno.h>
50#include <string.h>
51
52/* After receiving these frames, we send ack/nack. */
53#define SEND_ACK_AFTER_FRAMES 20
54
55extern void *tall_pcu_ctx;
56
Pau Espin Pedrolbddf1ad2019-09-26 14:41:18 +020057gprs_rlcmac_ul_tbf::gprs_rlcmac_ul_tbf(BTS *bts_) :
58 gprs_rlcmac_tbf(bts_, GPRS_RLCMAC_UL_TBF),
59 m_rx_counter(0),
60 m_contention_resolution_done(0),
61 m_final_ack_sent(0),
62 m_ul_gprs_ctrs(NULL),
63 m_ul_egprs_ctrs(NULL)
64{
65 memset(&m_usf, 0, sizeof(m_usf));
66}
67
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010068/*
69 * Store received block data in LLC message(s) and forward to SGSN
70 * if complete.
71 */
72int gprs_rlcmac_ul_tbf::assemble_forward_llc(const gprs_rlc_data *_data)
73{
74 const uint8_t *data = _data->block;
75 uint8_t len = _data->len;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +010076 const struct gprs_rlc_data_block_info *rdbi = &_data->block_info;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +020077 enum CodingScheme cs = _data->cs_last;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010078
79 Decoding::RlcData frames[16], *frame;
80 int i, num_frames = 0;
81 uint32_t dummy_tlli;
82
Max0524e382018-01-19 18:22:25 +010083 LOGPTBFUL(this, LOGL_DEBUG, "Assembling frames: (len=%d)\n", len);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010084
85 num_frames = Decoding::rlc_data_from_ul_data(
Alexander Couzensce936f32016-05-24 14:45:41 +020086 rdbi, cs, data, &(frames[0]), ARRAY_SIZE(frames),
Jacob Erlbeckb3100e12015-12-14 13:36:13 +010087 &dummy_tlli);
88
89 /* create LLC frames */
90 for (i = 0; i < num_frames; i++) {
91 frame = frames + i;
92
Aravind Sirsikar22a90192016-09-15 17:24:49 +053093 if (frame->length) {
Pau Espin Pedrola107f8f2020-05-12 22:32:24 +020094 bts->do_rate_ctr_add(CTR_RLC_UL_PAYLOAD_BYTES, frame->length);
Alexander Couzens7fdbf892016-05-21 19:45:23 +020095
Max0524e382018-01-19 18:22:25 +010096 LOGPTBFUL(this, LOGL_DEBUG, "Frame %d "
Aravind Sirsikar22a90192016-09-15 17:24:49 +053097 "starts at offset %d, "
98 "length=%d, is_complete=%d\n",
99 i + 1, frame->offset, frame->length,
100 frame->is_complete);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100101
Aravind Sirsikar22a90192016-09-15 17:24:49 +0530102 m_llc.append_frame(data + frame->offset, frame->length);
103 m_llc.consume(frame->length);
104 }
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100105
106 if (frame->is_complete) {
107 /* send frame to SGSN */
Max0fdaa9d2018-01-30 16:03:10 +0100108 LOGPTBFUL(this, LOGL_DEBUG, "complete UL frame len=%d\n", m_llc.frame_length());
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100109 snd_ul_ud();
Pau Espin Pedrola107f8f2020-05-12 22:32:24 +0200110 bts->do_rate_ctr_add(CTR_LLC_UL_BYTES, m_llc.frame_length());
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100111 m_llc.reset();
112 }
113 }
114
115 return 0;
116}
117
Maxfd13f6c2017-07-07 14:29:36 +0200118bool gprs_rlcmac_ul_tbf::ctrl_ack_to_toggle()
119{
Max8dce1de2018-01-02 14:17:04 +0100120 if (check_n_clear(GPRS_RLCMAC_FLAG_TO_UL_ACK))
Maxfd13f6c2017-07-07 14:29:36 +0200121 return true; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was set, now cleared */
Maxfd13f6c2017-07-07 14:29:36 +0200122
123 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK);
124 return false; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was unset, now set */
125}
126
127bool gprs_rlcmac_ul_tbf::handle_ctrl_ack()
128{
129 /* check if this control ack belongs to packet uplink ack */
Max088c7df2018-01-23 20:16:23 +0100130 if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
131 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
Maxfd13f6c2017-07-07 14:29:36 +0200132 return true;
133 }
134
135 return false;
136}
Daniel Willmannca102af2014-08-08 12:14:12 +0200137
Jacob Erlbeck5a3c84d2016-01-22 17:25:38 +0100138struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn, uint8_t ts)
Daniel Willmannca102af2014-08-08 12:14:12 +0200139{
140 int final = (state_is(GPRS_RLCMAC_FINISHED));
141 struct msgb *msg;
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100142 int rc;
143 unsigned int rrbp = 0;
144 uint32_t new_poll_fn = 0;
Daniel Willmannca102af2014-08-08 12:14:12 +0200145
146 if (final) {
Maxcac6b662018-01-24 11:00:17 +0100147 if (poll_scheduled() && ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
Maxc21f0072017-12-15 17:36:45 +0100148 LOGPTBFUL(this, LOGL_DEBUG,
149 "Polling is already scheduled, so we must wait for the final uplink ack...\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200150 return NULL;
151 }
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100152
153 rc = check_polling(fn, ts, &new_poll_fn, &rrbp);
154 if (rc < 0)
Daniel Willmannca102af2014-08-08 12:14:12 +0200155 return NULL;
Daniel Willmannca102af2014-08-08 12:14:12 +0200156 }
157
158 msg = msgb_alloc(23, "rlcmac_ul_ack");
159 if (!msg)
160 return NULL;
Alexander Couzensccde5c92017-02-04 03:10:08 +0100161 bitvec *ack_vec = bitvec_alloc(23, tall_pcu_ctx);
Daniel Willmannca102af2014-08-08 12:14:12 +0200162 if (!ack_vec) {
163 msgb_free(msg);
164 return NULL;
165 }
Max7426c5f2019-02-18 20:42:42 +0100166 bitvec_unhex(ack_vec, DUMMY_VEC);
Alexander Couzens3a499f32019-06-16 15:25:30 +0200167 Encoding::write_packet_uplink_ack(ack_vec, this, final, rrbp);
Daniel Willmannca102af2014-08-08 12:14:12 +0200168 bitvec_pack(ack_vec, msgb_put(msg, 23));
169 bitvec_free(ack_vec);
Daniel Willmannca102af2014-08-08 12:14:12 +0200170
171 /* now we must set this flag, so we are allowed to assign downlink
172 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
173 m_contention_resolution_done = 1;
174
175 if (final) {
Maxf60cf622017-07-10 14:40:09 +0200176 set_polling(new_poll_fn, ts, GPRS_RLCMAC_POLL_UL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200177 /* waiting for final acknowledge */
Daniel Willmannca102af2014-08-08 12:14:12 +0200178 m_final_ack_sent = 1;
179 } else
Max088c7df2018-01-23 20:16:23 +0100180 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
Daniel Willmannca102af2014-08-08 12:14:12 +0200181
182 return msg;
183}
184
Alexander Couzens2fcfc292016-05-24 14:40:03 +0200185/*! \brief receive data from PDCH/L1 */
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100186int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100187 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeckfc1b3e62016-01-11 09:58:11 +0100188 uint8_t *data, struct pcu_l1_meas *meas)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100189{
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200190 const struct gprs_rlc_data_block_info *rdbi;
191 struct gprs_rlc_data *block;
192
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100193 int8_t rssi = meas->have_rssi ? meas->rssi : 0;
194
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100195 const uint16_t ws = m_window.ws();
196
197 this->state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
198
Max0524e382018-01-19 18:22:25 +0100199 LOGPTBFUL(this, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100200 "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(),
201 this->m_window.v_r());
202
203 /* process RSSI */
204 gprs_rlcmac_rssi(this, rssi);
205
206 /* store measurement values */
207 if (ms())
208 ms()->update_l1_meas(meas);
209
210 uint32_t new_tlli = 0;
211 unsigned int block_idx;
212
213 /* restart T3169 */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200214 T_START(this, T3169, 3169, "acked (data)", true);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100215
216 /* Increment RX-counter */
217 this->m_rx_counter++;
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530218 update_coding_scheme_counter_ul(rlc->cs);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100219 /* Loop over num_blocks */
220 for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) {
221 int num_chunks;
222 uint8_t *rlc_data;
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200223 rdbi = &rlc->block_info[block_idx];
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100224 bool need_rlc_data = false;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100225
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
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200333 /* Last frame in buffer: */
334 block = m_rlc.block(m_window.mod_sns(m_window.v_r() - 1));
335 rdbi = &block->block_info;
336
337 /* Check if we already received all data TBF had to send: */
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100338 if (this->state_is(GPRS_RLCMAC_FLOW) /* still in flow state */
339 && this->m_window.v_q() == this->m_window.v_r()) { /* if complete */
Max0524e382018-01-19 18:22:25 +0100340 LOGPTBFUL(this, LOGL_DEBUG,
341 "No gaps in received block, last block: BSN=%d CV=%d\n",
342 rdbi->bsn, rdbi->cv);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100343 if (rdbi->cv == 0) {
Max0524e382018-01-19 18:22:25 +0100344 LOGPTBFUL(this, LOGL_DEBUG, "Finished with UL TBF\n");
Max2399b1d2018-01-12 15:48:12 +0100345 TBF_SET_STATE(this, GPRS_RLCMAC_FINISHED);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100346 /* Reset N3103 counter. */
Max847ed9f2018-02-20 18:16:11 +0100347 this->n_reset(N3103);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100348 }
349 }
350
351 /* If TLLI is included or if we received half of the window, we send
352 * an ack/nack */
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200353 maybe_schedule_uplink_acknack(rlc, rdbi->cv == 0);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100354
355 return 0;
356}
357
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100358void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200359 const gprs_rlc_data_info *rlc, bool countdown_finished)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100360{
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200361 bool require_ack = false;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100362 bool have_ti = rlc->block_info[0].ti ||
363 (rlc->num_data_blocks > 1 && rlc->block_info[1].ti);
364
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200365 if (rlc->si) {
366 require_ack = true;
367 LOGPTBFUL(this, LOGL_NOTICE,
368 "Scheduling Ack/Nack, because MS is stalled.\n");
369 }
370 if (have_ti) {
371 require_ack = true;
372 LOGPTBFUL(this, LOGL_DEBUG,
373 "Scheduling Ack/Nack, because TLLI is included.\n");
374 }
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200375 if (countdown_finished) {
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200376 require_ack = true;
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200377 if (state_is(GPRS_RLCMAC_FLOW))
378 LOGPTBFUL(this, LOGL_DEBUG,
379 "Scheduling Ack/Nack, because some data is missing and last block has CV==0.\n");
380 else if (state_is(GPRS_RLCMAC_FINISHED))
381 LOGPTBFUL(this, LOGL_DEBUG,
382 "Scheduling final Ack/Nack, because all data was received and last block has CV==0.\n");
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200383 }
384 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
385 require_ack = true;
386 LOGPTBFUL(this, LOGL_DEBUG,
387 "Scheduling Ack/Nack, because %d frames received.\n",
388 SEND_ACK_AFTER_FRAMES);
389 }
390
391 if (!require_ack)
392 return;
393
394 if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_NONE)) {
395 /* trigger sending at next RTS */
396 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_SEND_ACK);
397 } else {
398 /* already triggered */
399 LOGPTBFUL(this, LOGL_DEBUG,
400 "Sending Ack/Nack already scheduled, no need to re-schedule\n");
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100401 }
402}
403
Daniel Willmannca102af2014-08-08 12:14:12 +0200404/* Send Uplink unit-data to SGSN. */
405int gprs_rlcmac_ul_tbf::snd_ul_ud()
406{
407 uint8_t qos_profile[3];
408 struct msgb *llc_pdu;
409 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + m_llc.frame_length();
410 struct bssgp_bvc_ctx *bctx = gprs_bssgp_pcu_current_bctx();
411
412 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), m_llc.frame_length());
413 if (!bctx) {
414 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
415 m_llc.reset_frame_space();
416 return -EIO;
417 }
Pau Espin Pedrol488aa292019-09-25 17:48:35 +0200418
Daniel Willmannca102af2014-08-08 12:14:12 +0200419 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
420 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*m_llc.frame_length()));
421 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*m_llc.frame_length(), m_llc.frame);
422 qos_profile[0] = QOS_PROFILE >> 16;
423 qos_profile[1] = QOS_PROFILE >> 8;
424 qos_profile[2] = QOS_PROFILE;
425 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
426
427 m_llc.reset_frame_space();
428 return 0;
429}
430
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530431egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_second_seg(
432 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
433 uint8_t *data, const uint8_t block_idx)
434{
435 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
436 union split_block_status *spb_status = &block->spb_status;
437 uint8_t *rlc_data = &block->block[0];
438
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200439 bts->do_rate_ctr_inc(CTR_SPB_UL_SECOND_SEGMENT);
sivasankarida7250a2016-12-16 12:57:18 +0530440
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530441 if (spb_status->block_status_ul &
442 EGPRS_RESEG_FIRST_SEG_RXD) {
Max0524e382018-01-19 18:22:25 +0100443 LOGPTBFUL(this, LOGL_DEBUG,
444 "Second seg is received first seg is already present set the status to complete\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530445 spb_status->block_status_ul = EGPRS_RESEG_DEFAULT;
446
447 block->len += Decoding::rlc_copy_to_aligned_buffer(rlc,
448 block_idx, data, rlc_data + block->len);
449 block->block_info.data_len += rdbi->data_len;
450 } else if (spb_status->block_status_ul == EGPRS_RESEG_DEFAULT) {
Max0524e382018-01-19 18:22:25 +0100451 LOGPTBFUL(this, LOGL_DEBUG,
452 "Second seg is received first seg is not received set the status to second seg received\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530453
454 block->len = Decoding::rlc_copy_to_aligned_buffer(rlc,
455 block_idx, data,
456 rlc_data + rlc->block_info[block_idx].data_len);
457
458 spb_status->block_status_ul = EGPRS_RESEG_SECOND_SEG_RXD;
459 block->block_info = *rdbi;
460 }
461 return spb_status->block_status_ul;
462}
463
464egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_first_seg(
465 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
466 uint8_t *data, const uint8_t block_idx)
467{
468 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
469 uint8_t *rlc_data = &block->block[0];
470 union split_block_status *spb_status = &block->spb_status;
471
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200472 bts->do_rate_ctr_inc(CTR_SPB_UL_FIRST_SEGMENT);
sivasankarida7250a2016-12-16 12:57:18 +0530473
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530474 if (spb_status->block_status_ul & EGPRS_RESEG_SECOND_SEG_RXD) {
Max0524e382018-01-19 18:22:25 +0100475 LOGPTBFUL(this, LOGL_DEBUG,
476 "First seg is received second seg is already present set the status to complete\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530477
478 block->len += Decoding::rlc_copy_to_aligned_buffer(rlc,
479 block_idx, data, rlc_data);
480
481 block->block_info.data_len = block->len;
482 spb_status->block_status_ul = EGPRS_RESEG_DEFAULT;
483 } else if (spb_status->block_status_ul == EGPRS_RESEG_DEFAULT) {
Max0524e382018-01-19 18:22:25 +0100484 LOGPTBFUL(this, LOGL_DEBUG,
485 "First seg is received second seg is not received set the status to first seg received\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530486
487 spb_status->block_status_ul = EGPRS_RESEG_FIRST_SEG_RXD;
488 block->len = Decoding::rlc_copy_to_aligned_buffer(rlc,
489 block_idx, data, rlc_data);
490 block->block_info = *rdbi;
491 }
492 return spb_status->block_status_ul;
493}
494
495egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_spb(
496 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
497 uint8_t *data, const uint8_t block_idx)
498{
499 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
500
Max0524e382018-01-19 18:22:25 +0100501 LOGPTBFUL(this, LOGL_DEBUG,
502 "Got SPB(%d) cs(%s) data block with BSN (%d), TFI(%d).\n",
Max136ebcc2019-03-05 14:59:03 +0100503 rdbi->spb, mcs_name(rlc->cs), rdbi->bsn, rlc->tfi);
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530504
505 egprs_rlc_ul_reseg_bsn_state assemble_status = EGPRS_RESEG_INVALID;
506
507 /* Section 10.4.8b of 44.060*/
508 if (rdbi->spb == 2)
509 assemble_status = handle_egprs_ul_first_seg(rlc,
510 block, data, block_idx);
511 else if (rdbi->spb == 3)
512 assemble_status = handle_egprs_ul_second_seg(rlc,
513 block, data, block_idx);
514 else {
Max0524e382018-01-19 18:22:25 +0100515 LOGPTBFUL(this, LOGL_ERROR,
516 "spb(%d) Not supported SPB for this EGPRS configuration\n",
517 rdbi->spb);
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530518 }
519
520 /*
521 * When the block is successfully constructed out of segmented blocks
522 * upgrade the MCS to the type 2
523 */
524 if (assemble_status == EGPRS_RESEG_DEFAULT) {
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200525 switch (rlc->cs) {
Maxbea2edb2019-03-06 17:04:59 +0100526 case MCS3 :
527 block->cs_last = MCS6;
Max0524e382018-01-19 18:22:25 +0100528 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS6\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530529 break;
Maxbea2edb2019-03-06 17:04:59 +0100530 case MCS2 :
531 block->cs_last = MCS5;
Max0524e382018-01-19 18:22:25 +0100532 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS5\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530533 break;
Maxbea2edb2019-03-06 17:04:59 +0100534 case MCS1 :
Max0524e382018-01-19 18:22:25 +0100535 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS4\n");
Maxbea2edb2019-03-06 17:04:59 +0100536 block->cs_last = MCS4;
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530537 break;
538 default:
Max0524e382018-01-19 18:22:25 +0100539 LOGPTBFUL(this, LOGL_ERROR,
540 "cs(%s) Error in Upgrading to higher MCS\n",
Max136ebcc2019-03-05 14:59:03 +0100541 mcs_name(rlc->cs));
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530542 break;
543 }
544 }
545 return assemble_status;
546}
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530547
Maxfb59a932019-03-13 18:40:19 +0100548void gprs_rlcmac_ul_tbf::update_coding_scheme_counter_ul(enum CodingScheme cs)
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530549{
Maxfb59a932019-03-13 18:40:19 +0100550 switch (cs) {
551 case CS1:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200552 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS1);
Maxfb59a932019-03-13 18:40:19 +0100553 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS1]);
554 break;
555 case CS2:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200556 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS2);
Maxfb59a932019-03-13 18:40:19 +0100557 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS2]);
558 break;
559 case CS3:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200560 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS3);
Maxfb59a932019-03-13 18:40:19 +0100561 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS3]);
562 break;
563 case CS4:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200564 bts->do_rate_ctr_inc(CTR_GPRS_UL_CS4);
Maxfb59a932019-03-13 18:40:19 +0100565 rate_ctr_inc(&m_ul_gprs_ctrs->ctr[TBF_CTR_GPRS_UL_CS4]);
566 break;
567 case MCS1:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200568 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS1);
Maxfb59a932019-03-13 18:40:19 +0100569 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS1]);
570 break;
571 case MCS2:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200572 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS2);
Maxfb59a932019-03-13 18:40:19 +0100573 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS2]);
574 break;
575 case MCS3:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200576 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS3);
Maxfb59a932019-03-13 18:40:19 +0100577 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS3]);
578 break;
579 case MCS4:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200580 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS4);
Maxfb59a932019-03-13 18:40:19 +0100581 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS4]);
582 break;
583 case MCS5:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200584 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS5);
Maxfb59a932019-03-13 18:40:19 +0100585 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS5]);
586 break;
587 case MCS6:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200588 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS6);
Maxfb59a932019-03-13 18:40:19 +0100589 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS6]);
590 break;
591 case MCS7:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200592 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS7);
Maxfb59a932019-03-13 18:40:19 +0100593 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS7]);
594 break;
595 case MCS8:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200596 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS8);
Maxfb59a932019-03-13 18:40:19 +0100597 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS8]);
598 break;
599 case MCS9:
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200600 bts->do_rate_ctr_inc(CTR_EGPRS_UL_MCS9);
Maxfb59a932019-03-13 18:40:19 +0100601 rate_ctr_inc(&m_ul_egprs_ctrs->ctr[TBF_CTR_EGPRS_UL_MCS9]);
602 break;
603 default:
604 LOGPTBFUL(this, LOGL_ERROR, "attempting to update rate counters for unsupported (M)CS %s\n",
605 mcs_name(cs));
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530606 }
607}
sivasankari8adfcd02017-01-16 15:41:21 +0530608
Max9d7357e2017-12-14 15:02:33 +0100609void gprs_rlcmac_ul_tbf::set_window_size()
sivasankari8adfcd02017-01-16 15:41:21 +0530610{
Max34513fe2018-12-11 16:47:30 +0100611 const struct gprs_rlcmac_bts *b = bts->bts_data();
612 uint16_t ws = egprs_window_size(b, ul_slots());
Max0524e382018-01-19 18:22:25 +0100613 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 +0100614 ws, b->ws_base, pcu_bitcount(ul_slots()), b->ws_pdch);
sivasankari8adfcd02017-01-16 15:41:21 +0530615 m_window.set_ws(ws);
616}