blob: 6e507348cff3268d274fbc11596690615c6ae067 [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>
Oliver Smithd3c75912021-07-09 16:37:16 +020023#include <bts_pch_timer.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020024#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020025#include <tbf_ul.h>
Daniel Willmannca102af2014-08-08 12:14:12 +020026#include <rlc.h>
27#include <encoding.h>
28#include <gprs_rlcmac.h>
29#include <gprs_debug.h>
30#include <gprs_bssgp_pcu.h>
31#include <decoding.h>
Jacob Erlbeck20f6fd12015-06-08 11:05:45 +020032#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010033#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>
Pau Espin Pedrol442198c2020-10-23 22:30:04 +020043 #include <osmocom/core/stats.h>
Max1187a772018-01-26 13:31:42 +010044 #include <osmocom/core/utils.h>
45 #include <osmocom/gprs/gprs_bssgp_bss.h>
46 #include <osmocom/gprs/protocol/gsm_08_18.h>
47 #include <osmocom/gsm/tlv.h>
Max136ebcc2019-03-05 14:59:03 +010048 #include "coding_scheme.h"
Daniel Willmannca102af2014-08-08 12:14:12 +020049}
50
51#include <errno.h>
52#include <string.h>
53
54/* After receiving these frames, we send ack/nack. */
55#define SEND_ACK_AFTER_FRAMES 20
56
57extern void *tall_pcu_ctx;
58
Pau Espin Pedrol442198c2020-10-23 22:30:04 +020059static const struct rate_ctr_desc tbf_ul_gprs_ctr_description[] = {
60 { "gprs:uplink:cs1", "CS1 " },
61 { "gprs:uplink:cs2", "CS2 " },
62 { "gprs:uplink:cs3", "CS3 " },
63 { "gprs:uplink:cs4", "CS4 " },
64};
65
66static const struct rate_ctr_desc tbf_ul_egprs_ctr_description[] = {
67 { "egprs:uplink:mcs1", "MCS1 " },
68 { "egprs:uplink:mcs2", "MCS2 " },
69 { "egprs:uplink:mcs3", "MCS3 " },
70 { "egprs:uplink:mcs4", "MCS4 " },
71 { "egprs:uplink:mcs5", "MCS5 " },
72 { "egprs:uplink:mcs6", "MCS6 " },
73 { "egprs:uplink:mcs7", "MCS7 " },
74 { "egprs:uplink:mcs8", "MCS8 " },
75 { "egprs:uplink:mcs9", "MCS9 " },
76};
77
78static const struct rate_ctr_group_desc tbf_ul_gprs_ctrg_desc = {
79 "tbf:gprs",
80 "Data Blocks",
81 OSMO_STATS_CLASS_SUBSCRIBER,
82 ARRAY_SIZE(tbf_ul_gprs_ctr_description),
83 tbf_ul_gprs_ctr_description,
84};
85
86static const struct rate_ctr_group_desc tbf_ul_egprs_ctrg_desc = {
87 "tbf:egprs",
88 "Data Blocks",
89 OSMO_STATS_CLASS_SUBSCRIBER,
90 ARRAY_SIZE(tbf_ul_egprs_ctr_description),
91 tbf_ul_egprs_ctr_description,
92};
93
Pau Espin Pedrol131deb02021-07-26 18:24:14 +020094gprs_rlcmac_ul_tbf::~gprs_rlcmac_ul_tbf()
95{
96 rate_ctr_group_free(m_ul_egprs_ctrs);
97 rate_ctr_group_free(m_ul_gprs_ctrs);
98 /* ~gprs_rlcmac_tbf() is called automatically upon return */
99}
100
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200101static int ul_tbf_dtor(struct gprs_rlcmac_ul_tbf *tbf)
102{
103 tbf->~gprs_rlcmac_ul_tbf();
104 return 0;
105}
106
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200107/* Generic function to alloc a UL TBF, later configured to be assigned either over CCCH or PACCH */
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200108struct gprs_rlcmac_ul_tbf *tbf_alloc_ul_tbf(struct gprs_rlcmac_bts *bts, GprsMs *ms, int8_t use_trx, bool single_slot)
109{
110 struct gprs_rlcmac_ul_tbf *tbf;
111 int rc;
112
113 OSMO_ASSERT(ms != NULL);
114
Pau Espin Pedrol36177c62021-02-25 17:57:37 +0100115 LOGPMS(ms, DTBF, LOGL_DEBUG, "********** UL-TBF starts here **********\n");
116 LOGPMS(ms, DTBF, LOGL_INFO, "Allocating UL TBF\n");
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200117
118 tbf = talloc(tall_pcu_ctx, struct gprs_rlcmac_ul_tbf);
119 if (!tbf)
120 return NULL;
121 talloc_set_destructor(tbf, ul_tbf_dtor);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100122 new (tbf) gprs_rlcmac_ul_tbf(bts, ms);
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200123
124 rc = tbf->setup(use_trx, single_slot);
125
126 /* if no resource */
127 if (rc < 0) {
128 talloc_free(tbf);
129 return NULL;
130 }
131
132 if (tbf->is_egprs_enabled())
133 tbf->set_window_size();
134
135 tbf->m_ul_egprs_ctrs = rate_ctr_group_alloc(tbf,
136 &tbf_ul_egprs_ctrg_desc, tbf->m_ctrs->idx);
137 tbf->m_ul_gprs_ctrs = rate_ctr_group_alloc(tbf,
138 &tbf_ul_gprs_ctrg_desc, tbf->m_ctrs->idx);
139 if (!tbf->m_ul_egprs_ctrs || !tbf->m_ul_gprs_ctrs) {
140 LOGPTBF(tbf, LOGL_ERROR, "Couldn't allocate TBF UL counters\n");
141 talloc_free(tbf);
142 return NULL;
143 }
144
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +0200145 llist_add_tail(tbf_trx_list(tbf), &tbf->trx->ul_tbfs);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100146 bts_do_rate_ctr_inc(tbf->bts, CTR_TBF_UL_ALLOCATED);
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200147
148 return tbf;
149}
150
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200151/* Alloc a UL TBF to be assigned over PACCH */
Pau Espin Pedrolc6571b52021-05-10 17:10:37 +0200152gprs_rlcmac_ul_tbf *tbf_alloc_ul_pacch(struct gprs_rlcmac_bts *bts, GprsMs *ms, int8_t use_trx,
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200153 uint32_t tlli)
154{
155 struct gprs_rlcmac_ul_tbf *tbf;
156
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200157 tbf = tbf_alloc_ul_tbf(bts, ms, use_trx, false);
158 if (!tbf) {
Pau Espin Pedrol36177c62021-02-25 17:57:37 +0100159 LOGPMS(ms, DTBF, LOGL_NOTICE, "No PDCH resource\n");
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200160 /* Caller will most probably send a Imm Ass Reject after return */
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200161 return NULL;
162 }
163 tbf->m_contention_resolution_done = 1;
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200164 osmo_fsm_inst_dispatch(tbf->state_fsm.fi, TBF_EV_ASSIGN_ADD_PACCH, NULL);
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200165 tbf->update_ms(tlli, GPRS_RLCMAC_UL_TBF);
166 OSMO_ASSERT(tbf->ms());
167
168 return tbf;
169}
170
Pau Espin Pedrol20271c42021-05-10 17:21:03 +0200171/* Alloc a UL TBF to be assigned over CCCH */
172struct gprs_rlcmac_ul_tbf *tbf_alloc_ul_ccch(struct gprs_rlcmac_bts *bts, struct GprsMs *ms)
173{
174 struct gprs_rlcmac_ul_tbf *tbf;
175
176 tbf = tbf_alloc_ul_tbf(bts, ms, -1, true);
177 if (!tbf) {
178 LOGP(DTBF, LOGL_NOTICE, "No PDCH resource for Uplink TBF\n");
179 /* Caller will most probably send a Imm Ass Reject after return */
180 return NULL;
181 }
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200182 osmo_fsm_inst_dispatch(tbf->state_fsm.fi, TBF_EV_ASSIGN_ADD_CCCH, NULL);
Pau Espin Pedrol4b6f0bf2021-05-10 18:54:52 +0200183 tbf->contention_resolution_start();
Pau Espin Pedrol20271c42021-05-10 17:21:03 +0200184 OSMO_ASSERT(tbf->ms());
185
186 return tbf;
187}
188
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200189/* Create a temporary dummy TBF to Tx a ImmAssReject if allocating a new one during
190 * packet resource Request failed. This is similar as tbf_alloc_ul() but without
191 * calling tbf->setup() (in charge of TFI/USF allocation), and reusing resources
192 * from Packet Resource Request we received. See TS 44.060 sec 7.1.3.2.1 */
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200193struct gprs_rlcmac_ul_tbf *handle_tbf_reject(struct gprs_rlcmac_bts *bts,
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200194 GprsMs *ms, uint8_t trx_no, uint8_t ts)
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200195{
196 struct gprs_rlcmac_ul_tbf *ul_tbf = NULL;
197 struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200198 OSMO_ASSERT(ms);
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200199
200 ul_tbf = talloc(tall_pcu_ctx, struct gprs_rlcmac_ul_tbf);
201 if (!ul_tbf)
202 return ul_tbf;
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200203 talloc_set_destructor(ul_tbf, ul_tbf_dtor);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100204 new (ul_tbf) gprs_rlcmac_ul_tbf(bts, ms);
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200205
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200206 ul_tbf->control_ts = ts;
207 ul_tbf->trx = trx;
208 ul_tbf->m_ctrs = rate_ctr_group_alloc(ul_tbf, &tbf_ctrg_desc, next_tbf_ctr_group_id++);
209 ul_tbf->m_ul_egprs_ctrs = rate_ctr_group_alloc(ul_tbf,
210 &tbf_ul_egprs_ctrg_desc,
211 ul_tbf->m_ctrs->idx);
212 ul_tbf->m_ul_gprs_ctrs = rate_ctr_group_alloc(ul_tbf,
213 &tbf_ul_gprs_ctrg_desc,
214 ul_tbf->m_ctrs->idx);
215 if (!ul_tbf->m_ctrs || !ul_tbf->m_ul_egprs_ctrs || !ul_tbf->m_ul_gprs_ctrs) {
216 LOGPTBF(ul_tbf, LOGL_ERROR, "Cound not allocate TBF UL rate counters\n");
217 talloc_free(ul_tbf);
218 return NULL;
219 }
220
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200221 ms_attach_tbf(ms, ul_tbf);
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +0200222 llist_add(tbf_trx_list((struct gprs_rlcmac_tbf *)ul_tbf), &trx->ul_tbfs);
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200223 bts_do_rate_ctr_inc(ul_tbf->bts, CTR_TBF_UL_ALLOCATED);
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200224 osmo_fsm_inst_dispatch(ul_tbf->state_fsm.fi, TBF_EV_ASSIGN_ADD_PACCH, NULL);
Pau Espin Pedrol54742f22021-04-26 16:48:34 +0200225 TBF_SET_ASS_STATE_UL(ul_tbf, GPRS_RLCMAC_UL_ASS_SEND_ASS_REJ);
226
Pau Espin Pedrol442198c2020-10-23 22:30:04 +0200227 return ul_tbf;
228}
229
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100230gprs_rlcmac_ul_tbf::gprs_rlcmac_ul_tbf(struct gprs_rlcmac_bts *bts_, GprsMs *ms) :
Pau Espin Pedrole9f77d32020-10-23 18:41:40 +0200231 gprs_rlcmac_tbf(bts_, ms, GPRS_RLCMAC_UL_TBF),
Pau Espin Pedrolbddf1ad2019-09-26 14:41:18 +0200232 m_rx_counter(0),
233 m_contention_resolution_done(0),
234 m_final_ack_sent(0),
235 m_ul_gprs_ctrs(NULL),
236 m_ul_egprs_ctrs(NULL)
237{
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100238 memset(&m_usf, USF_INVALID, sizeof(m_usf));
Pau Espin Pedrolbddf1ad2019-09-26 14:41:18 +0200239}
240
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100241/*
242 * Store received block data in LLC message(s) and forward to SGSN
243 * if complete.
244 */
245int gprs_rlcmac_ul_tbf::assemble_forward_llc(const gprs_rlc_data *_data)
246{
247 const uint8_t *data = _data->block;
248 uint8_t len = _data->len;
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100249 const struct gprs_rlc_data_block_info *rdbi = &_data->block_info;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200250 enum CodingScheme cs = _data->cs_last;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100251
252 Decoding::RlcData frames[16], *frame;
253 int i, num_frames = 0;
254 uint32_t dummy_tlli;
255
Max0524e382018-01-19 18:22:25 +0100256 LOGPTBFUL(this, LOGL_DEBUG, "Assembling frames: (len=%d)\n", len);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100257
258 num_frames = Decoding::rlc_data_from_ul_data(
Alexander Couzensce936f32016-05-24 14:45:41 +0200259 rdbi, cs, data, &(frames[0]), ARRAY_SIZE(frames),
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100260 &dummy_tlli);
261
262 /* create LLC frames */
263 for (i = 0; i < num_frames; i++) {
264 frame = frames + i;
265
Aravind Sirsikar22a90192016-09-15 17:24:49 +0530266 if (frame->length) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100267 bts_do_rate_ctr_add(bts, CTR_RLC_UL_PAYLOAD_BYTES, frame->length);
Alexander Couzens7fdbf892016-05-21 19:45:23 +0200268
Max0524e382018-01-19 18:22:25 +0100269 LOGPTBFUL(this, LOGL_DEBUG, "Frame %d "
Aravind Sirsikar22a90192016-09-15 17:24:49 +0530270 "starts at offset %d, "
271 "length=%d, is_complete=%d\n",
272 i + 1, frame->offset, frame->length,
273 frame->is_complete);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100274
Aravind Sirsikar22a90192016-09-15 17:24:49 +0530275 m_llc.append_frame(data + frame->offset, frame->length);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100276 llc_consume(&m_llc, frame->length);
Aravind Sirsikar22a90192016-09-15 17:24:49 +0530277 }
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100278
279 if (frame->is_complete) {
280 /* send frame to SGSN */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100281 LOGPTBFUL(this, LOGL_DEBUG, "complete UL frame len=%d\n", llc_frame_length(&m_llc));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100282 snd_ul_ud();
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100283 bts_do_rate_ctr_add(bts, CTR_LLC_UL_BYTES, llc_frame_length(&m_llc));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100284 m_llc.reset();
285 }
286 }
287
288 return 0;
289}
290
Maxfd13f6c2017-07-07 14:29:36 +0200291bool gprs_rlcmac_ul_tbf::ctrl_ack_to_toggle()
292{
Max8dce1de2018-01-02 14:17:04 +0100293 if (check_n_clear(GPRS_RLCMAC_FLAG_TO_UL_ACK))
Maxfd13f6c2017-07-07 14:29:36 +0200294 return true; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was set, now cleared */
Maxfd13f6c2017-07-07 14:29:36 +0200295
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200296 state_fsm.state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK);
Maxfd13f6c2017-07-07 14:29:36 +0200297 return false; /* GPRS_RLCMAC_FLAG_TO_UL_ACK was unset, now set */
298}
299
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200300bool gprs_rlcmac_ul_tbf::handle_ctrl_ack(enum pdch_ulc_tbf_poll_reason reason)
Maxfd13f6c2017-07-07 14:29:36 +0200301{
302 /* check if this control ack belongs to packet uplink ack */
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200303 if (reason == PDCH_ULC_POLL_UL_ACK && ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
Max088c7df2018-01-23 20:16:23 +0100304 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
Maxfd13f6c2017-07-07 14:29:36 +0200305 return true;
306 }
307
308 return false;
309}
Daniel Willmannca102af2014-08-08 12:14:12 +0200310
Pau Espin Pedrol4b6f0bf2021-05-10 18:54:52 +0200311void gprs_rlcmac_ul_tbf::contention_resolution_start()
312{
313 /* 3GPP TS 44.018 sec 11.1.2 Timers on the network side: "This timer is
314 * started when a temporary block flow is allocated with an IMMEDIATE
315 * ASSIGNMENT or an IMMEDIATE PACKET ASSIGNMENT or an EC IMMEDIATE
316 * ASSIGNMENT TYPE 1 message during a packet access procedure. It is
317 * stopped when the mobile station has correctly seized the temporary
318 * block flow."
319 * In our code base, it means we want to do contention resolution
320 * timeout only for one-phase packet access, since two-phase is handled
321 * through SBA structs, which are freed by the PDCH UL Controller if the
322 * single allocated block is lost. */
323 T_START(this, T3141, 3141, "Contention resolution (UL-TBF, CCCH)", true);
324}
325void gprs_rlcmac_ul_tbf::contention_resolution_success()
326{
327 if (m_contention_resolution_done)
328 return;
329
330 /* 3GPP TS 44.060 sec 7a.2.1 Contention Resolution */
331 /* 3GPP TS 44.018 3.5.2.1.4 Packet access completion: The one phase
332 packet access procedure is completed at a successful contention
333 resolution. The mobile station has entered the packet transfer mode.
334 Timer T3141 is stopped on the network side */
335 t_stop(T3141, "Contention resolution success (UL-TBF, CCCH)");
336
337 /* now we must set this flag, so we are allowed to assign downlink
338 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
339 m_contention_resolution_done = 1;
340}
341
Jacob Erlbeck5a3c84d2016-01-22 17:25:38 +0100342struct msgb *gprs_rlcmac_ul_tbf::create_ul_ack(uint32_t fn, uint8_t ts)
Daniel Willmannca102af2014-08-08 12:14:12 +0200343{
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200344 int final = (state_is(TBF_ST_FINISHED));
Daniel Willmannca102af2014-08-08 12:14:12 +0200345 struct msgb *msg;
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100346 int rc;
347 unsigned int rrbp = 0;
348 uint32_t new_poll_fn = 0;
Daniel Willmannca102af2014-08-08 12:14:12 +0200349
350 if (final) {
Pau Espin Pedroled066b52021-03-29 16:16:27 +0200351 if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_WAIT_ACK)) {
Maxc21f0072017-12-15 17:36:45 +0100352 LOGPTBFUL(this, LOGL_DEBUG,
353 "Polling is already scheduled, so we must wait for the final uplink ack...\n");
Daniel Willmannca102af2014-08-08 12:14:12 +0200354 return NULL;
355 }
Jacob Erlbeckf2694b72016-01-26 21:46:26 +0100356
357 rc = check_polling(fn, ts, &new_poll_fn, &rrbp);
358 if (rc < 0)
Daniel Willmannca102af2014-08-08 12:14:12 +0200359 return NULL;
Daniel Willmannca102af2014-08-08 12:14:12 +0200360 }
361
362 msg = msgb_alloc(23, "rlcmac_ul_ack");
363 if (!msg)
364 return NULL;
Alexander Couzensccde5c92017-02-04 03:10:08 +0100365 bitvec *ack_vec = bitvec_alloc(23, tall_pcu_ctx);
Daniel Willmannca102af2014-08-08 12:14:12 +0200366 if (!ack_vec) {
367 msgb_free(msg);
368 return NULL;
369 }
Max7426c5f2019-02-18 20:42:42 +0100370 bitvec_unhex(ack_vec, DUMMY_VEC);
Alexander Couzens3a499f32019-06-16 15:25:30 +0200371 Encoding::write_packet_uplink_ack(ack_vec, this, final, rrbp);
Daniel Willmannca102af2014-08-08 12:14:12 +0200372 bitvec_pack(ack_vec, msgb_put(msg, 23));
373 bitvec_free(ack_vec);
Daniel Willmannca102af2014-08-08 12:14:12 +0200374
Pau Espin Pedrol4b6f0bf2021-05-10 18:54:52 +0200375 /* TS 44.060 7a.2.1.1: "The contention resolution is completed on
376 * the network side when the network receives an RLC data block that
377 * comprises the TLLI value that identifies the mobile station and the
378 * TFI value associated with the TBF."
379 * However, it's handier for us to mark contention resolution success
380 * here since according to spec upon rx UL ACK is the time at which MS
381 * realizes contention resolution succeeds. */
Pau Espin Pedrolfaf0ccb2021-05-11 12:55:31 +0200382 if (is_tlli_valid())
Pau Espin Pedrol4b6f0bf2021-05-10 18:54:52 +0200383 contention_resolution_success();
Daniel Willmannca102af2014-08-08 12:14:12 +0200384
385 if (final) {
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200386 set_polling(new_poll_fn, ts, PDCH_ULC_POLL_UL_ACK);
Daniel Willmannca102af2014-08-08 12:14:12 +0200387 /* waiting for final acknowledge */
Daniel Willmannca102af2014-08-08 12:14:12 +0200388 m_final_ack_sent = 1;
389 } else
Max088c7df2018-01-23 20:16:23 +0100390 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_NONE);
Daniel Willmannca102af2014-08-08 12:14:12 +0200391
392 return msg;
393}
394
Alexander Couzens2fcfc292016-05-24 14:40:03 +0200395/*! \brief receive data from PDCH/L1 */
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100396int gprs_rlcmac_ul_tbf::rcv_data_block_acknowledged(
Jacob Erlbeckf2ba4cb2016-01-07 18:59:28 +0100397 const struct gprs_rlc_data_info *rlc,
Jacob Erlbeckfc1b3e62016-01-11 09:58:11 +0100398 uint8_t *data, struct pcu_l1_meas *meas)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100399{
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200400 const struct gprs_rlc_data_block_info *rdbi;
401 struct gprs_rlc_data *block;
402
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100403 int8_t rssi = meas->have_rssi ? meas->rssi : 0;
404
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100405 const uint16_t ws = m_window.ws();
406
Pau Espin Pedrol33e80072021-07-22 19:20:50 +0200407 this->state_fsm.state_flags |= (1 << GPRS_RLCMAC_FLAG_UL_DATA);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100408
Max0524e382018-01-19 18:22:25 +0100409 LOGPTBFUL(this, LOGL_DEBUG, "UL DATA TFI=%d received (V(Q)=%d .. "
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100410 "V(R)=%d)\n", rlc->tfi, this->m_window.v_q(),
411 this->m_window.v_r());
412
413 /* process RSSI */
414 gprs_rlcmac_rssi(this, rssi);
415
416 /* store measurement values */
Pau Espin Pedrolf53815f2021-05-11 14:23:51 +0200417 ms_update_l1_meas(ms(), meas);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100418
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700419 uint32_t new_tlli = GSM_RESERVED_TMSI;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100420 unsigned int block_idx;
421
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100422 /* Increment RX-counter */
423 this->m_rx_counter++;
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530424 update_coding_scheme_counter_ul(rlc->cs);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100425 /* Loop over num_blocks */
426 for (block_idx = 0; block_idx < rlc->num_data_blocks; block_idx++) {
427 int num_chunks;
428 uint8_t *rlc_data;
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200429 rdbi = &rlc->block_info[block_idx];
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100430
Max0524e382018-01-19 18:22:25 +0100431 LOGPTBFUL(this, LOGL_DEBUG,
432 "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 +0100433 mcs_name(rlc->cs),
Max0524e382018-01-19 18:22:25 +0100434 rdbi->cv, rdbi->bsn, rdbi->spb,
435 rdbi->pi, rdbi->e, rdbi->ti,
436 rlc->data_offs_bits[block_idx]);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100437
438 /* Check whether the block needs to be decoded */
439
440 if (!m_window.is_in_window(rdbi->bsn)) {
Max0524e382018-01-19 18:22:25 +0100441 LOGPTBFUL(this, LOGL_DEBUG, "BSN %d out of window %d..%d (it's normal)\n",
442 rdbi->bsn,
443 m_window.v_q(), m_window.mod_sns(m_window.v_q() + ws - 1));
Pau Espin Pedrol58916312021-05-11 14:40:41 +0200444 continue;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100445 } else if (m_window.is_received(rdbi->bsn)) {
Max0524e382018-01-19 18:22:25 +0100446 LOGPTBFUL(this, LOGL_DEBUG,
447 "BSN %d already received\n", rdbi->bsn);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100448 continue;
Pau Espin Pedrol58916312021-05-11 14:40:41 +0200449 }
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100450
451 /* Store block and meta info to BSN buffer */
452
Max0524e382018-01-19 18:22:25 +0100453 LOGPTBFUL(this, LOGL_DEBUG, "BSN %d storing in window (%d..%d)\n",
454 rdbi->bsn, m_window.v_q(),
455 m_window.mod_sns(m_window.v_q() + ws - 1));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100456 block = m_rlc.block(rdbi->bsn);
Aravind Sirsikar550a5412016-06-14 18:59:18 +0530457 OSMO_ASSERT(rdbi->data_len <= sizeof(block->block));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100458 rlc_data = &(block->block[0]);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100459
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530460 if (rdbi->spb) {
461 egprs_rlc_ul_reseg_bsn_state assemble_status;
462
463 assemble_status = handle_egprs_ul_spb(rlc,
464 block, data, block_idx);
465
466 if (assemble_status != EGPRS_RESEG_DEFAULT)
467 return 0;
468 } else {
469 block->block_info = *rdbi;
470 block->cs_last = rlc->cs;
471 block->len =
472 Decoding::rlc_copy_to_aligned_buffer(rlc,
473 block_idx, data, rlc_data);
474 }
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100475
Max0524e382018-01-19 18:22:25 +0100476 LOGPTBFUL(this, LOGL_DEBUG,
477 "data_length=%d, data=%s\n",
478 block->len, osmo_hexdump(rlc_data, block->len));
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100479 /* Get/Handle TLLI */
480 if (rdbi->ti) {
481 num_chunks = Decoding::rlc_data_from_ul_data(
482 rdbi, rlc->cs, rlc_data, NULL, 0, &new_tlli);
483
484 if (num_chunks < 0) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100485 bts_do_rate_ctr_inc(bts, CTR_DECODE_ERRORS);
Max0524e382018-01-19 18:22:25 +0100486 LOGPTBFUL(this, LOGL_NOTICE,
487 "Failed to decode TLLI of %s UL DATA TFI=%d.\n",
Max136ebcc2019-03-05 14:59:03 +0100488 mcs_name(rlc->cs), rlc->tfi);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100489 m_window.invalidate_bsn(rdbi->bsn);
490 continue;
491 }
492 if (!this->is_tlli_valid()) {
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700493 if (new_tlli == GSM_RESERVED_TMSI) {
Max0524e382018-01-19 18:22:25 +0100494 LOGPTBFUL(this, LOGL_NOTICE,
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700495 "TLLI is 0x%08x within UL DATA?!?\n",
496 new_tlli);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100497 m_window.invalidate_bsn(rdbi->bsn);
498 continue;
499 }
Max0524e382018-01-19 18:22:25 +0100500 LOGPTBFUL(this, LOGL_INFO,
501 "Decoded premier TLLI=0x%08x of UL DATA TFI=%d.\n",
Vadim Yanitskiy3f27fb52020-04-20 11:26:12 +0700502 new_tlli, rlc->tfi);
Pau Espin Pedrold21e9612020-06-26 14:20:37 +0200503 update_ms(new_tlli, GPRS_RLCMAC_UL_TBF);
Oliver Smithd3c75912021-07-09 16:37:16 +0200504 bts_pch_timer_stop(bts, ms_imsi(ms()));
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700505 } else if (new_tlli != GSM_RESERVED_TMSI && new_tlli != tlli()) {
Max0524e382018-01-19 18:22:25 +0100506 LOGPTBFUL(this, LOGL_NOTICE,
Pau Espin Pedrol305763d2020-11-06 20:03:24 +0100507 "Decoded TLLI=%08x mismatch on UL DATA TFI=%d. (Ignoring due to contention resolution)\n",
508 new_tlli, rlc->tfi);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100509 m_window.invalidate_bsn(rdbi->bsn);
510 continue;
511 }
Pau Espin Pedrol58916312021-05-11 14:40:41 +0200512 } else if (!is_tlli_valid()) {
513 LOGPTBFUL(this, LOGL_NOTICE, "Missing TLLI within UL DATA.\n");
514 m_window.invalidate_bsn(rdbi->bsn);
515 continue;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100516 }
517
518 m_window.receive_bsn(rdbi->bsn);
519 }
520
521 /* Raise V(Q) if possible, and retrieve LLC frames from blocks.
522 * This is looped until there is a gap (non received block) or
523 * the window is empty.*/
524 const uint16_t v_q_beg = m_window.v_q();
525 const uint16_t count = m_window.raise_v_q();
526
527 /* Retrieve LLC frames from blocks that are ready */
528 for (uint16_t i = 0; i < count; ++i) {
Jacob Erlbeck93c55d02015-12-23 16:29:07 +0100529 uint16_t index = m_window.mod_sns(v_q_beg + i);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100530 assemble_forward_llc(m_rlc.block(index));
531 }
532
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200533 /* Last frame in buffer: */
534 block = m_rlc.block(m_window.mod_sns(m_window.v_r() - 1));
535 rdbi = &block->block_info;
536
537 /* Check if we already received all data TBF had to send: */
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200538 if (this->state_is(TBF_ST_FLOW) /* still in flow state */
Pau Espin Pedrol9b63cd02021-05-11 14:54:40 +0200539 && this->m_window.v_q() == this->m_window.v_r() /* if complete */
540 && block->len) { /* if there was ever a last block received */
Max0524e382018-01-19 18:22:25 +0100541 LOGPTBFUL(this, LOGL_DEBUG,
542 "No gaps in received block, last block: BSN=%d CV=%d\n",
543 rdbi->bsn, rdbi->cv);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100544 if (rdbi->cv == 0) {
Max0524e382018-01-19 18:22:25 +0100545 LOGPTBFUL(this, LOGL_DEBUG, "Finished with UL TBF\n");
Pau Espin Pedrolc32c4a32021-07-23 18:27:57 +0200546 osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_LAST_UL_DATA_RECVD, NULL);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100547 /* Reset N3103 counter. */
Max847ed9f2018-02-20 18:16:11 +0100548 this->n_reset(N3103);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100549 }
550 }
551
552 /* If TLLI is included or if we received half of the window, we send
553 * an ack/nack */
Pau Espin Pedrol9b63cd02021-05-11 14:54:40 +0200554 maybe_schedule_uplink_acknack(rlc, block->len && rdbi->cv == 0);
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100555
556 return 0;
557}
558
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100559void gprs_rlcmac_ul_tbf::maybe_schedule_uplink_acknack(
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200560 const gprs_rlc_data_info *rlc, bool countdown_finished)
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100561{
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200562 bool require_ack = false;
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100563 bool have_ti = rlc->block_info[0].ti ||
564 (rlc->num_data_blocks > 1 && rlc->block_info[1].ti);
565
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200566 if (rlc->si) {
567 require_ack = true;
568 LOGPTBFUL(this, LOGL_NOTICE,
569 "Scheduling Ack/Nack, because MS is stalled.\n");
570 }
571 if (have_ti) {
572 require_ack = true;
573 LOGPTBFUL(this, LOGL_DEBUG,
574 "Scheduling Ack/Nack, because TLLI is included.\n");
575 }
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200576 if (countdown_finished) {
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200577 require_ack = true;
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200578 if (state_is(TBF_ST_FLOW))
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200579 LOGPTBFUL(this, LOGL_DEBUG,
580 "Scheduling Ack/Nack, because some data is missing and last block has CV==0.\n");
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200581 else if (state_is(TBF_ST_FINISHED))
Pau Espin Pedrolc33a0242020-05-15 16:57:48 +0200582 LOGPTBFUL(this, LOGL_DEBUG,
583 "Scheduling final Ack/Nack, because all data was received and last block has CV==0.\n");
Pau Espin Pedrol09afd6f2020-05-15 16:40:18 +0200584 }
585 if ((m_rx_counter % SEND_ACK_AFTER_FRAMES) == 0) {
586 require_ack = true;
587 LOGPTBFUL(this, LOGL_DEBUG,
588 "Scheduling Ack/Nack, because %d frames received.\n",
589 SEND_ACK_AFTER_FRAMES);
590 }
591
592 if (!require_ack)
593 return;
594
595 if (ul_ack_state_is(GPRS_RLCMAC_UL_ACK_NONE)) {
596 /* trigger sending at next RTS */
597 TBF_SET_ACK_STATE(this, GPRS_RLCMAC_UL_ACK_SEND_ACK);
598 } else {
599 /* already triggered */
600 LOGPTBFUL(this, LOGL_DEBUG,
601 "Sending Ack/Nack already scheduled, no need to re-schedule\n");
Jacob Erlbeckb3100e12015-12-14 13:36:13 +0100602 }
603}
604
Daniel Willmannca102af2014-08-08 12:14:12 +0200605/* Send Uplink unit-data to SGSN. */
606int gprs_rlcmac_ul_tbf::snd_ul_ud()
607{
608 uint8_t qos_profile[3];
609 struct msgb *llc_pdu;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100610 unsigned msg_len = NS_HDR_LEN + BSSGP_HDR_LEN + llc_frame_length(&m_llc);
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100611 struct bssgp_bvc_ctx *bctx = bts->pcu->bssgp.bctx;
Daniel Willmannca102af2014-08-08 12:14:12 +0200612
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100613 LOGP(DBSSGP, LOGL_INFO, "LLC [PCU -> SGSN] %s len=%d\n", tbf_name(this), llc_frame_length(&m_llc));
Daniel Willmannca102af2014-08-08 12:14:12 +0200614 if (!bctx) {
615 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
616 m_llc.reset_frame_space();
617 return -EIO;
618 }
Pau Espin Pedrol488aa292019-09-25 17:48:35 +0200619
Daniel Willmannca102af2014-08-08 12:14:12 +0200620 llc_pdu = msgb_alloc_headroom(msg_len, msg_len,"llc_pdu");
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100621 uint8_t *buf = msgb_push(llc_pdu, TL16V_GROSS_LEN(sizeof(uint8_t)*llc_frame_length(&m_llc)));
622 tl16v_put(buf, BSSGP_IE_LLC_PDU, sizeof(uint8_t)*llc_frame_length(&m_llc), m_llc.frame);
Daniel Willmannca102af2014-08-08 12:14:12 +0200623 qos_profile[0] = QOS_PROFILE >> 16;
624 qos_profile[1] = QOS_PROFILE >> 8;
625 qos_profile[2] = QOS_PROFILE;
626 bssgp_tx_ul_ud(bctx, tlli(), qos_profile, llc_pdu);
627
628 m_llc.reset_frame_space();
629 return 0;
630}
631
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530632egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_second_seg(
633 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
634 uint8_t *data, const uint8_t block_idx)
635{
636 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
637 union split_block_status *spb_status = &block->spb_status;
638 uint8_t *rlc_data = &block->block[0];
639
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100640 bts_do_rate_ctr_inc(bts, CTR_SPB_UL_SECOND_SEGMENT);
sivasankarida7250a2016-12-16 12:57:18 +0530641
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530642 if (spb_status->block_status_ul &
643 EGPRS_RESEG_FIRST_SEG_RXD) {
Max0524e382018-01-19 18:22:25 +0100644 LOGPTBFUL(this, LOGL_DEBUG,
645 "Second seg is received first seg is already present set the status to complete\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530646 spb_status->block_status_ul = EGPRS_RESEG_DEFAULT;
647
648 block->len += Decoding::rlc_copy_to_aligned_buffer(rlc,
649 block_idx, data, rlc_data + block->len);
650 block->block_info.data_len += rdbi->data_len;
651 } else if (spb_status->block_status_ul == EGPRS_RESEG_DEFAULT) {
Max0524e382018-01-19 18:22:25 +0100652 LOGPTBFUL(this, LOGL_DEBUG,
653 "Second seg is received first seg is not received set the status to second seg received\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530654
655 block->len = Decoding::rlc_copy_to_aligned_buffer(rlc,
656 block_idx, data,
657 rlc_data + rlc->block_info[block_idx].data_len);
658
659 spb_status->block_status_ul = EGPRS_RESEG_SECOND_SEG_RXD;
660 block->block_info = *rdbi;
661 }
662 return spb_status->block_status_ul;
663}
664
665egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_first_seg(
666 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
667 uint8_t *data, const uint8_t block_idx)
668{
669 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
670 uint8_t *rlc_data = &block->block[0];
671 union split_block_status *spb_status = &block->spb_status;
672
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100673 bts_do_rate_ctr_inc(bts, CTR_SPB_UL_FIRST_SEGMENT);
sivasankarida7250a2016-12-16 12:57:18 +0530674
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530675 if (spb_status->block_status_ul & EGPRS_RESEG_SECOND_SEG_RXD) {
Max0524e382018-01-19 18:22:25 +0100676 LOGPTBFUL(this, LOGL_DEBUG,
677 "First seg is received second seg is already present set the status to complete\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530678
679 block->len += Decoding::rlc_copy_to_aligned_buffer(rlc,
680 block_idx, data, rlc_data);
681
682 block->block_info.data_len = block->len;
683 spb_status->block_status_ul = EGPRS_RESEG_DEFAULT;
684 } else if (spb_status->block_status_ul == EGPRS_RESEG_DEFAULT) {
Max0524e382018-01-19 18:22:25 +0100685 LOGPTBFUL(this, LOGL_DEBUG,
686 "First seg is received second seg is not received set the status to first seg received\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530687
688 spb_status->block_status_ul = EGPRS_RESEG_FIRST_SEG_RXD;
689 block->len = Decoding::rlc_copy_to_aligned_buffer(rlc,
690 block_idx, data, rlc_data);
691 block->block_info = *rdbi;
692 }
693 return spb_status->block_status_ul;
694}
695
696egprs_rlc_ul_reseg_bsn_state gprs_rlcmac_ul_tbf::handle_egprs_ul_spb(
697 const struct gprs_rlc_data_info *rlc, struct gprs_rlc_data *block,
698 uint8_t *data, const uint8_t block_idx)
699{
700 const gprs_rlc_data_block_info *rdbi = &rlc->block_info[block_idx];
701
Max0524e382018-01-19 18:22:25 +0100702 LOGPTBFUL(this, LOGL_DEBUG,
703 "Got SPB(%d) cs(%s) data block with BSN (%d), TFI(%d).\n",
Max136ebcc2019-03-05 14:59:03 +0100704 rdbi->spb, mcs_name(rlc->cs), rdbi->bsn, rlc->tfi);
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530705
706 egprs_rlc_ul_reseg_bsn_state assemble_status = EGPRS_RESEG_INVALID;
707
708 /* Section 10.4.8b of 44.060*/
709 if (rdbi->spb == 2)
710 assemble_status = handle_egprs_ul_first_seg(rlc,
711 block, data, block_idx);
712 else if (rdbi->spb == 3)
713 assemble_status = handle_egprs_ul_second_seg(rlc,
714 block, data, block_idx);
715 else {
Max0524e382018-01-19 18:22:25 +0100716 LOGPTBFUL(this, LOGL_ERROR,
717 "spb(%d) Not supported SPB for this EGPRS configuration\n",
718 rdbi->spb);
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530719 }
720
721 /*
722 * When the block is successfully constructed out of segmented blocks
723 * upgrade the MCS to the type 2
724 */
725 if (assemble_status == EGPRS_RESEG_DEFAULT) {
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200726 switch (rlc->cs) {
Maxbea2edb2019-03-06 17:04:59 +0100727 case MCS3 :
728 block->cs_last = MCS6;
Max0524e382018-01-19 18:22:25 +0100729 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS6\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530730 break;
Maxbea2edb2019-03-06 17:04:59 +0100731 case MCS2 :
732 block->cs_last = MCS5;
Max0524e382018-01-19 18:22:25 +0100733 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS5\n");
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530734 break;
Maxbea2edb2019-03-06 17:04:59 +0100735 case MCS1 :
Max0524e382018-01-19 18:22:25 +0100736 LOGPTBFUL(this, LOGL_DEBUG, "Upgrading to MCS4\n");
Maxbea2edb2019-03-06 17:04:59 +0100737 block->cs_last = MCS4;
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530738 break;
739 default:
Max0524e382018-01-19 18:22:25 +0100740 LOGPTBFUL(this, LOGL_ERROR,
741 "cs(%s) Error in Upgrading to higher MCS\n",
Max136ebcc2019-03-05 14:59:03 +0100742 mcs_name(rlc->cs));
Aravind Sirsikar505a86d2016-07-26 18:26:21 +0530743 break;
744 }
745 }
746 return assemble_status;
747}
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530748
Maxfb59a932019-03-13 18:40:19 +0100749void gprs_rlcmac_ul_tbf::update_coding_scheme_counter_ul(enum CodingScheme cs)
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530750{
Maxfb59a932019-03-13 18:40:19 +0100751 switch (cs) {
752 case CS1:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100753 bts_do_rate_ctr_inc(bts, CTR_GPRS_UL_CS1);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200754 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_gprs_ctrs, TBF_CTR_GPRS_UL_CS1));
Maxfb59a932019-03-13 18:40:19 +0100755 break;
756 case CS2:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100757 bts_do_rate_ctr_inc(bts, CTR_GPRS_UL_CS2);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200758 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_gprs_ctrs, TBF_CTR_GPRS_UL_CS2));
Maxfb59a932019-03-13 18:40:19 +0100759 break;
760 case CS3:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100761 bts_do_rate_ctr_inc(bts, CTR_GPRS_UL_CS3);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200762 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_gprs_ctrs, TBF_CTR_GPRS_UL_CS3));
Maxfb59a932019-03-13 18:40:19 +0100763 break;
764 case CS4:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100765 bts_do_rate_ctr_inc(bts, CTR_GPRS_UL_CS4);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200766 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_gprs_ctrs, TBF_CTR_GPRS_UL_CS4));
Maxfb59a932019-03-13 18:40:19 +0100767 break;
768 case MCS1:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100769 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS1);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200770 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS1));
Maxfb59a932019-03-13 18:40:19 +0100771 break;
772 case MCS2:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100773 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS2);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200774 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS2));
Maxfb59a932019-03-13 18:40:19 +0100775 break;
776 case MCS3:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100777 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS3);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200778 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS3));
Maxfb59a932019-03-13 18:40:19 +0100779 break;
780 case MCS4:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100781 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS4);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200782 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS4));
Maxfb59a932019-03-13 18:40:19 +0100783 break;
784 case MCS5:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100785 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS5);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200786 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS5));
Maxfb59a932019-03-13 18:40:19 +0100787 break;
788 case MCS6:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100789 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS6);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200790 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS6));
Maxfb59a932019-03-13 18:40:19 +0100791 break;
792 case MCS7:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100793 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS7);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200794 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS7));
Maxfb59a932019-03-13 18:40:19 +0100795 break;
796 case MCS8:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100797 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS8);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200798 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS8));
Maxfb59a932019-03-13 18:40:19 +0100799 break;
800 case MCS9:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100801 bts_do_rate_ctr_inc(bts, CTR_EGPRS_UL_MCS9);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200802 rate_ctr_inc(rate_ctr_group_get_ctr(m_ul_egprs_ctrs, TBF_CTR_EGPRS_UL_MCS9));
Maxfb59a932019-03-13 18:40:19 +0100803 break;
804 default:
805 LOGPTBFUL(this, LOGL_ERROR, "attempting to update rate counters for unsupported (M)CS %s\n",
806 mcs_name(cs));
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530807 }
808}
sivasankari8adfcd02017-01-16 15:41:21 +0530809
Max9d7357e2017-12-14 15:02:33 +0100810void gprs_rlcmac_ul_tbf::set_window_size()
sivasankari8adfcd02017-01-16 15:41:21 +0530811{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100812 const struct gprs_rlcmac_bts *b = bts;
Max34513fe2018-12-11 16:47:30 +0100813 uint16_t ws = egprs_window_size(b, ul_slots());
Max0524e382018-01-19 18:22:25 +0100814 LOGPTBFUL(this, LOGL_INFO, "setting EGPRS UL window size to %u, base(%u) slots(%u) ws_pdch(%u)\n",
Pau Espin Pedrol519d0712021-01-14 14:30:03 +0100815 ws, bts->pcu->vty.ws_base, pcu_bitcount(ul_slots()), bts->pcu->vty.ws_pdch);
sivasankari8adfcd02017-01-16 15:41:21 +0530816 m_window.set_ws(ws);
817}
Pau Espin Pedrol9767e442020-10-23 21:11:41 +0200818
Pau Espin Pedrolb3f23972020-10-23 21:00:23 +0200819gprs_rlc_window *gprs_rlcmac_ul_tbf::window()
Pau Espin Pedrol9767e442020-10-23 21:11:41 +0200820{
821 return &m_window;
822}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100823
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +0100824void gprs_rlcmac_ul_tbf::usf_timeout()
825{
Pau Espin Pedrolcfb61d92021-07-26 17:17:02 +0200826 if (n_inc(N3101))
Pau Espin Pedrol55f600b2021-07-26 15:54:39 +0200827 osmo_fsm_inst_dispatch(this->state_fsm.fi, TBF_EV_MAX_N3101, NULL);
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +0100828}
829
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100830struct gprs_rlcmac_ul_tbf *as_ul_tbf(struct gprs_rlcmac_tbf *tbf)
831{
832 if (tbf && tbf->direction == GPRS_RLCMAC_UL_TBF)
833 return static_cast<gprs_rlcmac_ul_tbf *>(tbf);
834 else
835 return NULL;
836}
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +0100837
838void tbf_usf_timeout(struct gprs_rlcmac_ul_tbf *tbf)
839{
840 tbf->usf_timeout();
841}