blob: 3c4e676080ff1408e25a2726217809a80e42d1e2 [file] [log] [blame]
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +02001/*
2 * Copyright (C) 2013 by Holger Hans Peter Freyther
3 *
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <bts.h>
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +020022#include <poll_controller.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020023#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020024#include <tbf_ul.h>
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +020025#include <encoding.h>
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +020026#include <decoding.h>
27#include <rlc.h>
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +020028#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010029#include <gprs_ms.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020030#include <gprs_rlcmac.h>
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020031#include <gprs_debug.h>
Max1187a772018-01-26 13:31:42 +010032#include <cxx_linuxlist.h>
Max6dc90b82018-02-19 17:17:28 +010033#include <pdch.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020034
35extern "C" {
36 #include <osmocom/core/talloc.h>
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020037 #include <osmocom/core/msgb.h>
Holger Hans Peter Freyther8df447d2015-11-07 21:04:40 +010038 #include <osmocom/core/stats.h>
Max9bbe1602016-07-18 12:50:18 +020039 #include <osmocom/gsm/protocol/gsm_04_08.h>
Philipp Maier1275a3f2017-02-21 19:35:23 +010040 #include <osmocom/gsm/gsm_utils.h>
Pau Espin Pedrolfbfab292019-12-23 13:19:25 +010041 #include <osmocom/gsm/gsm48.h>
Harald Welte717cdf52017-07-21 21:56:23 +020042 #include <osmocom/core/gsmtap_util.h>
Harald Welte3e51d3e2017-10-29 10:57:27 +010043 #include <osmocom/core/application.h>
Max1187a772018-01-26 13:31:42 +010044 #include <osmocom/core/bitvec.h>
45 #include <osmocom/core/gsmtap.h>
46 #include <osmocom/core/logging.h>
47 #include <osmocom/core/utils.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020048}
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +020049
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +020050#include <errno.h>
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +020051#include <string.h>
52
Philipp Maier1275a3f2017-02-21 19:35:23 +010053#define RFN_MODULUS 42432
54#define RFN_THRESHOLD RFN_MODULUS / 2
55
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020056extern void *tall_pcu_ctx;
57
Harald Welte3e51d3e2017-10-29 10:57:27 +010058extern "C" {
59 /* e must make sure to initialize logging before the BTS static
60 * constructors are executed below, as those call libosmocore APIs that
61 * require logging already to be initialized. */
Harald Welte0a369e52020-09-30 19:57:02 +020062 __attribute__((constructor (101))) static void early_init(void)
Harald Welte3e51d3e2017-10-29 10:57:27 +010063 {
Neels Hofmeyr42f2d612018-04-01 16:54:40 +020064 if (!tall_pcu_ctx) {
65 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
66 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
67 }
Harald Welte3e51d3e2017-10-29 10:57:27 +010068 }
69}
70
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020071static struct osmo_tdef T_defs_bts[] = {
72 { .T=3142, .default_val=20, .unit=OSMO_TDEF_S, .desc="timer (s)", .val=0 },
73 { .T=3169, .default_val=5, .unit=OSMO_TDEF_S, .desc="Reuse of USF and TFI(s) after the MS uplink TBF assignment is invalid (s)", .val=0 },
74 { .T=3191, .default_val=5, .unit=OSMO_TDEF_S, .desc="Reuse of TFI(s) after sending (1) last RLC Data Block on TBF(s), or (2) PACKET TBF RELEASE for an MBMS radio bearer (s)", .val=0 },
75 { .T=3193, .default_val=100, .unit=OSMO_TDEF_MS, .desc="Reuse of TFI(s) after reception of final PACKET DOWNLINK ACK/NACK from MS for TBF (ms)", .val=0 },
76 { .T=3195, .default_val=5, .unit=OSMO_TDEF_S, .desc="Reuse of TFI(s) upon no response from the MS (radio failure or cell change) for TBF/MBMS radio bearer (s)", .val=0 },
Pau Espin Pedrol5360ef52019-09-13 12:41:54 +020077 { .T=0, .default_val=0, .unit=OSMO_TDEF_S, .desc=NULL, .val=0 } /* empty item at the end */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020078};
79static struct osmo_tdef T_defs_pcu[] = {
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +020080 { .T=1, .default_val=30, .unit=OSMO_TDEF_S, .desc="BSSGP (un)blocking procedures timer (s)", .val=0 },
81 { .T=2, .default_val=30, .unit=OSMO_TDEF_S, .desc="BSSGP reset procedure timer (s)", .val=0 },
Pau Espin Pedrol38cfa732019-09-09 12:55:01 +020082 { .T=3190, .default_val=5, .unit=OSMO_TDEF_S, .desc="Return to packet idle mode after Packet DL Assignment on CCCH (s)", .val=0},
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020083 { .T=-2000, .default_val=2, .unit=OSMO_TDEF_MS, .desc="Tbf reject for PRR timer (ms)", .val=0 },
84 { .T=-2001, .default_val=2, .unit=OSMO_TDEF_S, .desc="PACCH assignment timer (s)", .val=0 },
85 { .T=-2002, .default_val=200, .unit=OSMO_TDEF_MS, .desc="Waiting after IMM.ASS confirm timer (ms)", .val=0 },
Pau Espin Pedrol63700ea2019-09-09 13:19:06 +020086 { .T=-2030, .default_val=60, .unit=OSMO_TDEF_S, .desc="Time to keep an idle MS object alive (s)", .val=0 }, /* slightly above T3314 (default 44s, 24.008, 11.2.2) */
Pau Espin Pedrol2b5c6292019-09-09 13:41:00 +020087 { .T=-2031, .default_val=2000, .unit=OSMO_TDEF_MS, .desc="Time to keep an idle DL TBF alive (ms)", .val=0 },
Pau Espin Pedrol5360ef52019-09-13 12:41:54 +020088 { .T=0, .default_val=0, .unit=OSMO_TDEF_S, .desc=NULL, .val=0 } /* empty item at the end */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020089};
90
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +010091/**
92 * For gcc-4.4 compat do not use extended initializer list but keep the
93 * order from the enum here. Once we support GCC4.7 and up we can change
94 * the code below.
95 */
96static const struct rate_ctr_desc bts_ctr_description[] = {
Maxa5eb67d2017-11-21 20:24:54 +010097 { "tbf:dl:alloc", "TBF DL Allocated "},
98 { "tbf:dl:freed", "TBF DL Freed "},
99 { "tbf:dl:aborted", "TBF DL Aborted "},
100 { "tbf:ul:alloc", "TBF UL Allocated "},
101 { "tbf:ul:freed", "TBF UL Freed "},
102 { "tbf:ul:aborted", "TBF UL Aborted "},
103 { "tbf:reused", "TBF Reused "},
104 { "tbf:alloc:algo-a", "TBF Alloc Algo A "},
105 { "tbf:alloc:algo-b", "TBF Alloc Algo B "},
Maxa5eb67d2017-11-21 20:24:54 +0100106 { "rlc:sent", "RLC Sent "},
107 { "rlc:resent", "RLC Resent "},
108 { "rlc:restarted", "RLC Restarted "},
109 { "rlc:stalled", "RLC Stalled "},
110 { "rlc:nacked", "RLC Nacked "},
111 { "rlc:final_block_resent", "RLC Final Blk resent "},
112 { "rlc:ass:timedout", "RLC Assign Timeout "},
113 { "rlc:ass:failed", "RLC Assign Failed "},
114 { "rlc:ack:timedout", "RLC Ack Timeout "},
115 { "rlc:ack:failed", "RLC Ack Failed "},
116 { "rlc:rel:timedout", "RLC Release Timeout "},
117 { "rlc:late-block", "RLC Late Block "},
118 { "rlc:sent-dummy", "RLC Sent Dummy "},
119 { "rlc:sent-control", "RLC Sent Control "},
120 { "rlc:dl_bytes", "RLC DL Bytes "},
121 { "rlc:dl_payload_bytes", "RLC DL Payload Bytes "},
122 { "rlc:ul_bytes", "RLC UL Bytes "},
123 { "rlc:ul_payload_bytes", "RLC UL Payload Bytes "},
124 { "decode:errors", "Decode Errors "},
125 { "sba:allocated", "SBA Allocated "},
126 { "sba:freed", "SBA Freed "},
127 { "sba:timedout", "SBA Timeout "},
128 { "llc:timeout", "Timedout Frames "},
129 { "llc:dropped", "Dropped Frames "},
130 { "llc:scheduled", "Scheduled Frames "},
131 { "llc:dl_bytes", "RLC encapsulated PDUs"},
132 { "llc:ul_bytes", "full PDUs received "},
133 { "rach:requests", "RACH requests "},
134 { "11bit_rach:requests", "11BIT_RACH requests "},
135 { "spb:uplink_first_segment", "First seg of UL SPB "},
136 { "spb:uplink_second_segment", "Second seg of UL SPB "},
137 { "spb:downlink_first_segment", "First seg of DL SPB "},
138 { "spb:downlink_second_segment","Second seg of DL SPB "},
139 { "immediate:assignment_UL", "Immediate Assign UL "},
140 { "immediate:assignment_rej", "Immediate Assign Rej "},
141 { "immediate:assignment_DL", "Immediate Assign DL "},
142 { "channel:request_description","Channel Request Desc "},
143 { "pkt:ul_assignment", "Packet UL Assignment "},
144 { "pkt:access_reject", "Packet Access Reject "},
145 { "pkt:dl_assignment", "Packet DL Assignment "},
146 { "ul:control", "UL control Block "},
147 { "ul:assignment_poll_timeout", "UL Assign Timeout "},
148 { "ul:assignment_failed", "UL Assign Failed "},
149 { "dl:assignment_timeout", "DL Assign Timeout "},
150 { "dl:assignment_failed", "DL Assign Failed "},
151 { "pkt:ul_ack_nack_timeout", "PUAN Poll Timeout "},
152 { "pkt:ul_ack_nack_failed", "PUAN poll Failed "},
153 { "pkt:dl_ack_nack_timeout", "PDAN poll Timeout "},
154 { "pkt:dl_ack_nack_failed", "PDAN poll Failed "},
155 { "gprs:downlink_cs1", "CS1 downlink "},
156 { "gprs:downlink_cs2", "CS2 downlink "},
157 { "gprs:downlink_cs3", "CS3 downlink "},
158 { "gprs:downlink_cs4", "CS4 downlink "},
159 { "egprs:downlink_mcs1", "MCS1 downlink "},
160 { "egprs:downlink_mcs2", "MCS2 downlink "},
161 { "egprs:downlink_mcs3", "MCS3 downlink "},
162 { "egprs:downlink_mcs4", "MCS4 downlink "},
163 { "egprs:downlink_mcs5", "MCS5 downlink "},
164 { "egprs:downlink_mcs6", "MCS6 downlink "},
165 { "egprs:downlink_mcs7", "MCS7 downlink "},
166 { "egprs:downlink_mcs8", "MCS8 downlink "},
167 { "egprs:downlink_mcs9", "MCS9 downlink "},
168 { "gprs:uplink_cs1", "CS1 Uplink "},
169 { "gprs:uplink_cs2", "CS2 Uplink "},
170 { "gprs:uplink_cs3", "CS3 Uplink "},
171 { "gprs:uplink_cs4", "CS4 Uplink "},
172 { "egprs:uplink_mcs1", "MCS1 Uplink "},
173 { "egprs:uplink_mcs2", "MCS2 Uplink "},
174 { "egprs:uplink_mcs3", "MCS3 Uplink "},
175 { "egprs:uplink_mcs4", "MCS4 Uplink "},
176 { "egprs:uplink_mcs5", "MCS5 Uplink "},
177 { "egprs:uplink_mcs6", "MCS6 Uplink "},
178 { "egprs:uplink_mcs7", "MCS7 Uplink "},
179 { "egprs:uplink_mcs8", "MCS8 Uplink "},
180 { "egprs:uplink_mcs9", "MCS9 Uplink "},
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100181};
182
183static const struct rate_ctr_group_desc bts_ctrg_desc = {
184 "bts",
185 "BTS Statistics",
Holger Hans Peter Freytherb8a54262015-11-07 21:00:00 +0100186 OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100187 ARRAY_SIZE(bts_ctr_description),
188 bts_ctr_description,
189};
190
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100191static const struct osmo_stat_item_desc bts_stat_item_description[] = {
192 { "ms.present", "MS Present ",
193 OSMO_STAT_ITEM_NO_UNIT, 4, 0},
194};
195
196static const struct osmo_stat_item_group_desc bts_statg_desc = {
197 "bts",
198 "BTS Statistics",
199 OSMO_STATS_CLASS_GLOBAL,
200 ARRAY_SIZE(bts_stat_item_description),
201 bts_stat_item_description,
202};
203
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100204static void bts_init(struct gprs_rlcmac_bts *bts, BTS* bts_obj)
205{
206 memset(bts, 0, sizeof(*bts));
207 bts->fc_interval = 1;
208 bts->initial_cs_dl = bts->initial_cs_ul = 1;
209 bts->initial_mcs_dl = bts->initial_mcs_ul = 1;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100210 bts->cs_mask = 1 << 0; /* CS-1 always enabled by default */
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100211 bts->n3101 = 10;
212 bts->n3103 = 4;
213 bts->n3105 = 8;
214 bts->alpha = 0; /* a = 0.0 */
215 bts->si13_is_set = false;
216 bts->cs_adj_enabled = 1;
217 bts->cs_adj_upper_limit = 33; /* Decrease CS if the error rate is above */
218 bts->cs_adj_lower_limit = 10; /* Increase CS if the error rate is below */
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100219 /* CS-1 to CS-4 */
220 bts->cs_lqual_ranges[0].low = -256;
221 bts->cs_lqual_ranges[0].high = 6;
222 bts->cs_lqual_ranges[1].low = 5;
223 bts->cs_lqual_ranges[1].high = 8;
224 bts->cs_lqual_ranges[2].low = 7;
225 bts->cs_lqual_ranges[2].high = 13;
226 bts->cs_lqual_ranges[3].low = 12;
227 bts->cs_lqual_ranges[3].high = 256;
228
229 /* MCS-1 to MCS-9 */
230 /* Default thresholds are referenced from literature */
231 /* Fig. 2.3, Chapter 2, Optimizing Wireless Communication Systems, Springer (2009) */
232 bts->mcs_lqual_ranges[0].low = -256;
233 bts->mcs_lqual_ranges[0].high = 6;
234 bts->mcs_lqual_ranges[1].low = 5;
235 bts->mcs_lqual_ranges[1].high = 8;
236 bts->mcs_lqual_ranges[2].low = 7;
237 bts->mcs_lqual_ranges[2].high = 13;
238 bts->mcs_lqual_ranges[3].low = 12;
239 bts->mcs_lqual_ranges[3].high = 15;
240 bts->mcs_lqual_ranges[4].low = 14;
241 bts->mcs_lqual_ranges[4].high = 17;
242 bts->mcs_lqual_ranges[5].low = 16;
243 bts->mcs_lqual_ranges[5].high = 18;
244 bts->mcs_lqual_ranges[6].low = 17;
245 bts->mcs_lqual_ranges[6].high = 20;
246 bts->mcs_lqual_ranges[7].low = 19;
247 bts->mcs_lqual_ranges[7].high = 24;
248 bts->mcs_lqual_ranges[8].low = 23;
249 bts->mcs_lqual_ranges[8].high = 256;
250 bts->cs_downgrade_threshold = 200;
Alexander Couzens3db47892020-12-16 21:26:26 +0100251 bts->ns_dialect = NS2_DIALECT_IPACCESS;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100252
253 /* TODO: increase them when CRBB decoding is implemented */
254 bts->ws_base = 64;
255 bts->ws_pdch = 0;
256
257 bts->llc_codel_interval_msec = LLC_CODEL_USE_DEFAULT;
258 bts->llc_idle_ack_csec = 10;
259
260 /*
261 * By default resegmentation is supported in DL
262 * can also be configured through VTY
263 */
264 bts->dl_arq_type = EGPRS_ARQ1;
265
266 bts->app_info = NULL;
267 bts->bts = bts_obj;
268 bts->dl_tbf_preemptive_retransmission = true;
269 bts->T_defs_bts = T_defs_bts;
270 bts->T_defs_pcu = T_defs_pcu;
271 osmo_tdefs_reset(bts->T_defs_bts);
272 osmo_tdefs_reset(bts->T_defs_pcu);
273
274 /* initialize back pointers */
275 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
276 struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
277 trx->trx_no = trx_no;
278 trx->bts = bts_obj;
279
280 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
281 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
282 pdch->init_ptcch_msg();
283 pdch->ts_no = ts_no;
284 pdch->trx = trx;
285 }
286 }
287}
288
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200289BTS* BTS::main_bts()
290{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100291 return the_pcu->bts;
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200292}
293
294struct gprs_rlcmac_bts *BTS::bts_data()
295{
296 return &m_bts;
297}
298
299struct gprs_rlcmac_bts *bts_main_data()
300{
301 return BTS::main_bts()->bts_data();
302}
303
Maxd5ffeb52019-03-18 15:48:38 +0100304void bts_cleanup()
305{
306 return BTS::main_bts()->cleanup();
307}
308
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100309struct rate_ctr_group *bts_main_data_stats()
310{
311 return BTS::main_bts()->rate_counters();
312}
313
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100314BTS::BTS(struct gprs_pcu *pcu)
315 : pcu(pcu)
316 , m_cur_fn(0)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200317 , m_cur_blk_fn(-1)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +0100318 , m_max_cs_dl(MAX_GPRS_CS)
319 , m_max_cs_ul(MAX_GPRS_CS)
320 , m_max_mcs_dl(MAX_EDGE_MCS)
321 , m_max_mcs_ul(MAX_EDGE_MCS)
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +0200322 , m_pollController(*this)
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200323 , m_sba(*this)
Jacob Erlbeck17214bb2015-06-02 14:06:12 +0200324 , m_ms_store(this)
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200325{
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100326 bts_init(&m_bts, this);
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100327
Maxea9968f2017-12-18 22:52:52 +0100328 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100329 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100330 than just allocate the group with different index.
331 This shall be removed once weget rid of BTS singleton */
332 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
333 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
334 else
335 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200336 OSMO_ASSERT(m_ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100337
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100338 m_statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200339 OSMO_ASSERT(m_statg);
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200340}
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200341
Maxd5ffeb52019-03-18 15:48:38 +0100342void BTS::cleanup()
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100343{
Jacob Erlbeckc362df22016-01-20 22:02:19 +0100344 /* this can cause counter updates and must not be left to the
345 * m_ms_store's destructor */
346 m_ms_store.cleanup();
347
Maxd5ffeb52019-03-18 15:48:38 +0100348 if (m_ratectrs) {
349 rate_ctr_group_free(m_ratectrs);
350 m_ratectrs = NULL;
351 }
352
353 if (m_statg) {
354 osmo_stat_item_group_free(m_statg);
355 m_statg = NULL;
356 }
Oliver Smithcfb63212019-09-05 17:13:33 +0200357
358 if (m_bts.app_info) {
359 msgb_free(m_bts.app_info);
360 m_bts.app_info = NULL;
361 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100362}
363
Maxd5ffeb52019-03-18 15:48:38 +0100364BTS::~BTS()
365{
366 cleanup();
367}
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100368
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200369void BTS::set_current_frame_number(int fn)
370{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200371 /* The UL frame numbers lag 3 behind the DL frames and the data
372 * indication is only sent after all 4 frames of the block have been
373 * received. Sometimes there is an idle frame between the end of one
374 * and start of another frame (every 3 blocks). So the timeout should
375 * definitely be there if we're more than 8 frames past poll_fn. Let's
Jacob Erlbecke77d49f2015-08-27 13:28:05 +0200376 * stay on the safe side and say 13 or more. An additional delay can
377 * happen due to the block processing time in the DSP, so the delay of
378 * decoded blocks relative to the timing clock can be much larger.
379 * Values up to 50 frames have been observed under load. */
380 const static int max_delay = 60;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200381
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200382 m_cur_fn = fn;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200383 m_pollController.expireTimedout(m_cur_fn, max_delay);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200384}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200385
Max9dabfa22017-05-16 16:10:45 +0200386static inline int delta_fn(int fn, int to)
387{
388 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
389}
390
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200391void BTS::set_current_block_frame_number(int fn, unsigned max_delay)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200392{
393 int delay = 0;
394 const int late_block_delay_thresh = 13;
395 const int fn_update_ok_min_delay = -500;
396 const int fn_update_ok_max_delay = 0;
397
398 /* frame numbers in the received blocks are assumed to be strongly
399 * monotonic. */
400 if (m_cur_blk_fn >= 0) {
Max9dabfa22017-05-16 16:10:45 +0200401 int delta = delta_fn(fn, m_cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200402 if (delta <= 0)
403 return;
404 }
405
406 /* Check block delay vs. the current frame number */
407 if (current_frame_number() != 0)
Max9dabfa22017-05-16 16:10:45 +0200408 delay = delta_fn(fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200409 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200410 LOGP(DRLCMAC, LOGL_NOTICE,
411 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
412 delay, fn, current_frame_number());
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200413 do_rate_ctr_inc(CTR_RLC_LATE_BLOCK);
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200414 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200415
416 m_cur_blk_fn = fn;
417 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
418 current_frame_number() == 0)
419 m_cur_fn = fn;
420
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200421 m_pollController.expireTimedout(fn, max_delay);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200422}
423
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200424int BTS::add_paging(uint8_t chan_needed, const struct osmo_mobile_identity *mi)
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200425{
426 uint8_t l, trx, ts, any_tbf = 0;
427 struct gprs_rlcmac_tbf *tbf;
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100428 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200429 uint8_t slot_mask[8];
430 int8_t first_ts; /* must be signed */
431
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100432 LListHead<gprs_rlcmac_tbf> *tbfs_lists[] = {
433 &m_ul_tbfs,
434 &m_dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200435 NULL
436 };
437
Neels Hofmeyr00520512020-08-21 15:44:58 +0200438 if (log_check_level(DRLCMAC, LOGL_INFO)) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200439 char str[64];
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200440 osmo_mobile_identity_to_str_buf(str, sizeof(str), mi);
Neels Hofmeyr00520512020-08-21 15:44:58 +0200441 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n", chan_needed, str);
442 }
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200443
444 /* collect slots to page
445 * Mark slots for every TBF, but only mark one of it.
446 * Mark only the first slot found.
447 * Don't mark, if TBF uses a different slot that is already marked. */
448 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200449 for (l = 0; tbfs_lists[l]; l++) {
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100450 llist_for_each(pos, tbfs_lists[l]) {
451 tbf = pos->entry();
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200452 first_ts = -1;
453 for (ts = 0; ts < 8; ts++) {
454 if (tbf->pdch[ts]) {
455 /* remember the first slot found */
456 if (first_ts < 0)
457 first_ts = ts;
458 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100459 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200460 break;
461 }
462 }
463 /* mark first slot found, if none is marked already */
464 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100465 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200466 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100467 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100468 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200469 } else
Maxc21f0072017-12-15 17:36:45 +0100470 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200471 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100472 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200473 }
474 }
475
476 /* Now we have a list of marked slots. Every TBF uses at least one
477 * of these slots. */
478
479 /* schedule paging to all marked slots */
480 for (trx = 0; trx < 8; trx++) {
481 if (slot_mask[trx] == 0)
482 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200483 for (ts = 0; ts < 8; ts++) {
484 if ((slot_mask[trx] & (1 << ts))) {
485 /* schedule */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200486 if (!m_bts.trx[trx].pdch[ts].add_paging(chan_needed, mi))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200487 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100488
489 LOGP(DRLCMAC, LOGL_INFO, "Paging on PACCH of TRX=%d TS=%d\n", trx, ts);
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200490 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200491 }
492 }
493 }
494
495 if (!any_tbf)
496 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
497
498 return 0;
499}
500
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700501void BTS::send_gsmtap_rach(enum pcu_gsmtap_category categ, uint8_t channel,
502 const struct rach_ind_params *rip)
503{
Vadim Yanitskiy55022ea2021-01-06 22:44:06 +0100504 struct pcu_l1_meas meas = { 0 };
Vadim Yanitskiy81b610e2020-06-11 01:04:44 +0700505 uint8_t ra_buf[2];
506
507 /* 3GPP TS 44.004 defines 11 bit RA as follows: xxxx xxxx .... .yyy
508 * On the PCUIF, we get 16 bit machne dependent number (LE/BE)
509 * Over GSMTAP we send the following: xxxx xxxx yyy. ....
510 * This simplifies parsing in Wireshark using its CSN.1 codec. */
511 if (rip->is_11bit) {
512 ra_buf[0] = (uint8_t) ((rip->ra >> 3) & 0xff);
513 ra_buf[1] = (uint8_t) ((rip->ra << 5) & 0xff);
514 } else {
515 ra_buf[0] = (uint8_t) (rip->ra & 0xff);
516 }
517
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700518 send_gsmtap_meas(categ, true, rip->trx_nr, rip->ts_nr, channel,
Vadim Yanitskiy81b610e2020-06-11 01:04:44 +0700519 rfn_to_fn(rip->rfn), ra_buf,
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700520 rip->is_11bit ? 2 : 1, &meas);
521}
522
Harald Welte717cdf52017-07-21 21:56:23 +0200523void BTS::send_gsmtap(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
524 uint8_t ts_no, uint8_t channel, uint32_t fn,
525 const uint8_t *data, unsigned int len)
526{
Vadim Yanitskiyeb70a402021-01-13 13:58:14 +0100527 struct pcu_l1_meas meas = { 0 };
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200528 send_gsmtap_meas(categ, uplink, trx_no, ts_no, channel, fn, data, len, &meas);
529}
530
531void BTS::send_gsmtap_meas(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
532 uint8_t ts_no, uint8_t channel, uint32_t fn,
533 const uint8_t *data, unsigned int len, struct pcu_l1_meas *meas)
534{
Harald Welte717cdf52017-07-21 21:56:23 +0200535 uint16_t arfcn;
536
537 /* check if category is activated at all */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100538 if (!(pcu->gsmtap_categ_mask & (1 << categ)))
Harald Welte717cdf52017-07-21 21:56:23 +0200539 return;
540
541 arfcn = m_bts.trx[trx_no].arfcn;
542 if (uplink)
543 arfcn |= GSMTAP_ARFCN_F_UPLINK;
544
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200545 /* GSMTAP needs the SNR here, but we only have C/I (meas->link_qual).
546 Those are not the same, but there is no known way to convert them,
547 let's pass C/I instead of nothing */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100548 gsmtap_send(pcu->gsmtap, arfcn, ts_no, channel, 0, fn,
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200549 meas->rssi, meas->link_qual, data, len);
Harald Welte717cdf52017-07-21 21:56:23 +0200550}
551
Max341dccd2017-08-14 15:49:59 +0200552static inline bool tbf_check(gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t trx_no, uint8_t ts)
553{
Maxcac6b662018-01-24 11:00:17 +0100554 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING) && tbf->poll_scheduled()
Max341dccd2017-08-14 15:49:59 +0200555 && tbf->poll_fn == fn && tbf->trx->trx_no == trx_no && tbf->poll_ts == ts)
556 return true;
557
558 return false;
559}
560
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200561gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts)
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200562{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100563 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200564
565 /* only one TBF can poll on specific TS/FN, because scheduler can only
566 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100567 llist_for_each(pos, &m_dl_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200568 if (tbf_check(pos->entry(), fn, trx, ts))
569 return as_dl_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200570 }
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200571 return NULL;
572}
Max341dccd2017-08-14 15:49:59 +0200573
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200574gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts)
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200575{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100576 LListHead<gprs_rlcmac_tbf> *pos;
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200577
578 /* only one TBF can poll on specific TS/FN, because scheduler can only
579 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100580 llist_for_each(pos, &m_ul_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200581 if (tbf_check(pos->entry(), fn, trx, ts))
582 return as_ul_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200583 }
584 return NULL;
585}
586
Daniel Willmann54044b02014-07-02 17:58:15 +0200587/* lookup downlink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200588gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200589{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200590 if (trx >= 8 || ts >= 8)
591 return NULL;
592
593 return m_bts.trx[trx].pdch[ts].dl_tbf_by_tfi(tfi);
Daniel Willmann54044b02014-07-02 17:58:15 +0200594}
595
596/* lookup uplink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200597gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200598{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200599 if (trx >= 8 || ts >= 8)
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200600 return NULL;
601
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200602 return m_bts.trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200603}
604
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100605static unsigned int trx_count_free_tfi(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t *first_free_tfi)
606{
607 const struct gprs_rlcmac_pdch *pdch;
608 uint8_t ts;
609 unsigned int i;
610 unsigned int free_tfi_cnt = 0;
611 bool has_pdch = false;
612 uint32_t mask = NO_FREE_TFI;
613
614 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
615 pdch = &trx->pdch[ts];
616 if (!pdch->is_enabled())
617 continue;
618 has_pdch = true;
619 mask &= ~pdch->assigned_tfi(dir);
620 }
621
622 if (!has_pdch || !mask) {
623 *first_free_tfi = (uint8_t)-1;
624 return 0;
625 }
626
627 /* Count free tfis and return */
628 for (i = 0; i < sizeof(mask) * 8 ; i++) {
629 if (mask & 1) {
630 if (free_tfi_cnt == 0)
631 *first_free_tfi = i;
632 free_tfi_cnt++;
633 }
634 mask >>= 1;
635 }
636 return free_tfi_cnt;
637}
638
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200639/*
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100640 * Search for free TFI and return TFI, TRX. This method returns the first TFI
641 * that is currently not used in any PDCH of a the TRX with least TFIs currently
642 * assigned. Negative values indicate errors.
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200643 */
Maxa76a7d02018-01-26 11:09:16 +0100644int BTS::tfi_find_free(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx, int8_t use_trx) const
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200645{
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100646 uint8_t trx_from, trx_to, trx;
647 uint8_t best_trx_nr = 0xff;
648 unsigned int best_cnt = 0;
649 uint8_t best_first_tfi = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200650
651 if (use_trx >= 0 && use_trx < 8)
652 trx_from = trx_to = use_trx;
653 else {
654 trx_from = 0;
655 trx_to = 7;
656 }
657
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200658 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200659 for (trx = trx_from; trx <= trx_to; trx++) {
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100660 uint8_t tmp_first_tfi;
661 unsigned int tmp_cnt;
662 tmp_cnt = trx_count_free_tfi(&m_bts.trx[trx], dir, &tmp_first_tfi);
663 if (tmp_cnt > best_cnt) {
664 best_cnt = tmp_cnt;
665 best_first_tfi = tmp_first_tfi;
666 best_trx_nr = trx;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200667 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200668 }
669
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100670 if (best_trx_nr == 0xff || best_cnt == 0) {
Max69d585e2018-01-26 12:36:43 +0100671 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200672 return -EBUSY;
673 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200674
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100675 OSMO_ASSERT(best_first_tfi < 32);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200676
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100677 LOGP(DRLCMAC, LOGL_DEBUG, "Found first unallocated TRX=%d TFI=%d\n",
678 best_trx_nr, best_first_tfi);
679 *_trx = best_trx_nr;
680 return best_first_tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200681}
682
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200683int BTS::rcv_imm_ass_cnf(const uint8_t *data, uint32_t fn)
684{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200685 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200686 uint8_t plen;
687 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200688 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200689
690 /* move to IA Rest Octets */
691 plen = data[0] >> 2;
692 data += 1 + plen;
693
694 if ((*data & 0xf0) != 0xd0) {
695 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
696 "octets do not start with bit sequence 'HH01' "
697 "(Packet Downlink Assignment)\n");
698 return -EINVAL;
699 }
700
701 /* get TLLI from downlink assignment */
Eric Wildab8b01e2019-07-23 17:16:06 +0200702 tlli = (uint32_t)((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200703 tlli |= (*data++) << 20;
704 tlli |= (*data++) << 12;
705 tlli |= (*data++) << 4;
706 tlli |= (*data++) >> 4;
707
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200708 ms = ms_by_tlli(tlli);
709 if (ms)
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100710 dl_tbf = ms_dl_tbf(ms);
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200711 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200712 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
713 "does not exit\n", tlli);
714 return -EINVAL;
715 }
716
717 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
718
Daniel Willmann7e994e32014-08-07 15:49:21 +0200719 if (dl_tbf->m_wait_confirm)
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200720 T_START(dl_tbf, T0, -2002, "assignment (AGCH)", true);
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200721
722 return 0;
723}
724
Philipp Maier1275a3f2017-02-21 19:35:23 +0100725/* Determine the full frame number from a relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100726uint32_t BTS::rfn_to_fn(int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100727{
Max5dd8d1b2017-03-08 12:06:42 +0100728 int32_t m_cur_rfn;
729 int32_t fn;
730 int32_t fn_rounded;
731
732 /* double-check that relative FN is not negative and fits into int32_t */
733 OSMO_ASSERT(rfn < GSM_MAX_FN);
734 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100735
736 /* Note: If a BTS is sending in a rach request it will be fully aware
737 * of the frame number. If the PCU is used in a BSC-co-located setup.
738 * The BSC will forward the incoming RACH request. The RACH request
739 * only contains the relative frame number (Fn % 42432) in its request
740 * reference. This PCU implementation has to fit both scenarios, so
741 * we need to assume that Fn is a relative frame number. */
742
743 /* Ensure that all following calculations are performed with the
744 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100745 if (rfn >= RFN_MODULUS)
746 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100747
748 /* Compute an internal relative frame number from the full internal
749 frame number */
750 m_cur_rfn = m_cur_fn % RFN_MODULUS;
751
752 /* Compute a "rounded" version of the internal frame number, which
753 * exactly fits in the RFN_MODULUS raster */
754 fn_rounded = m_cur_fn - m_cur_rfn;
755
756 /* If the delta between the internal and the external relative frame
757 * number exceeds a certain limit, we need to assume that the incoming
758 * rach request belongs to a the previous rfn period. To correct this,
759 * we roll back the rounded frame number by one RFN_MODULUS */
760 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
761 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200762 "Race condition between rfn (%u) and m_cur_fn (%u) detected: rfn belongs to the previous modulus %u cycle, wrapping...\n",
Philipp Maier1275a3f2017-02-21 19:35:23 +0100763 rfn, m_cur_fn, RFN_MODULUS);
764 if (fn_rounded < RFN_MODULUS) {
765 LOGP(DRLCMAC, LOGL_DEBUG,
766 "Cornercase detected: wrapping crosses %u border\n",
767 GSM_MAX_FN);
768 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
769 }
770 else
771 fn_rounded -= RFN_MODULUS;
772 }
773
774 /* The real frame number is the sum of the rounded frame number and the
775 * relative framenumber computed via RACH */
776 fn = fn_rounded + rfn;
777
778 return fn;
779}
780
Pau Espin Pedrol9ee90d22020-05-08 14:20:32 +0200781/* 3GPP TS 44.060:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700782 * Table 11.2.5.2: PACKET CHANNEL REQUEST
783 * Table 11.2.5a.2: EGPRS PACKET CHANNEL REQUEST
784 * Both GPRS and EGPRS use same MultislotClass coding, but since PRACH is
Pau Espin Pedrol9ee90d22020-05-08 14:20:32 +0200785 * deprecated, no PACKET CHANNEL REQUEST exists, which means for GPRS we will
786 * receive CCCH RACH which doesn't contain any mslot class. Hence in the end we
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700787 * can only receive EGPRS mslot class through 11-bit EGPRS PACKET CHANNEL REQUEST. */
788static int parse_egprs_pkt_ch_req(uint16_t ra11, struct chan_req_params *chan_req)
Max58818582018-01-05 15:04:50 +0100789{
Vadim Yanitskiyb90d34b2020-05-21 20:20:34 +0700790 EGPRS_PacketChannelRequest_t req;
791 int rc;
792
793 rc = decode_egprs_pkt_ch_req(ra11, &req);
794 if (rc) {
795 LOGP(DRLCMAC, LOGL_NOTICE, "Failed to decode "
796 "EGPRS Packet Channel Request: rc=%d\n", rc);
797 return rc;
798 }
799
800 LOGP(DRLCMAC, LOGL_INFO, "Rx EGPRS Packet Channel Request: %s\n",
801 get_value_string(egprs_pkt_ch_req_type_names, req.Type));
802
803 switch (req.Type) {
804 case EGPRS_PKT_CHAN_REQ_ONE_PHASE:
805 chan_req->egprs_mslot_class = req.Content.MultislotClass + 1;
806 chan_req->priority = req.Content.Priority + 1;
807 break;
808 case EGPRS_PKT_CHAN_REQ_SHORT:
809 chan_req->priority = req.Content.Priority + 1;
810 if (req.Content.NumberOfBlocks == 0)
811 chan_req->single_block = true;
812 break;
813 case EGPRS_PKT_CHAN_REQ_ONE_PHASE_RED_LATENCY:
814 chan_req->priority = req.Content.Priority + 1;
815 break;
816 /* Two phase access => single block is needed */
817 case EGPRS_PKT_CHAN_REQ_TWO_PHASE:
818 case EGPRS_PKT_CHAN_REQ_TWO_PHASE_IPA:
819 chan_req->priority = req.Content.Priority + 1;
820 chan_req->single_block = true;
821 break;
822 /* Signalling => single block is needed */
823 case EGPRS_PKT_CHAN_REQ_SIGNALLING:
824 case EGPRS_PKT_CHAN_REQ_SIGNALLING_IPA:
825 chan_req->single_block = true;
826 break;
827
828 /* Neither unacknowledged RLC mode, nor emergency calls are supported */
829 case EGPRS_PKT_CHAN_REQ_ONE_PHASE_UNACK:
830 case EGPRS_PKT_CHAN_REQ_EMERGENCY_CALL:
831 case EGPRS_PKT_CHAN_REQ_DEDICATED_CHANNEL:
832 LOGP(DRLCMAC, LOGL_NOTICE, "%s is not supported, rejecting\n",
833 get_value_string(egprs_pkt_ch_req_type_names, req.Type));
834 return -ENOTSUP;
835
836 default:
837 LOGP(DRLCMAC, LOGL_ERROR, "Unknown EGPRS Packet Channel Request "
838 "type=0x%02x, probably a bug in CSN.1 codec\n", req.Type);
839 return -EINVAL;
840 }
Max58818582018-01-05 15:04:50 +0100841
842 return 0;
843}
844
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700845/* NOTE: chan_req needs to be zero-initialized by the caller */
846static int parse_rach_ind(const struct rach_ind_params *rip,
847 struct chan_req_params *chan_req)
Max58818582018-01-05 15:04:50 +0100848{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700849 int rc;
Max58818582018-01-05 15:04:50 +0100850
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700851 switch (rip->burst_type) {
Max137fd592018-01-05 15:53:08 +0100852 case GSM_L1_BURST_TYPE_NONE:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700853 LOGP(DRLCMAC, LOGL_ERROR, "RACH.ind contains no burst type, assuming TS0\n");
854 /* fall-through */
855 case GSM_L1_BURST_TYPE_ACCESS_0:
856 if (rip->is_11bit) { /* 11 bit Access Burst with TS0 => Packet Channel Request */
857 LOGP(DRLCMAC, LOGL_ERROR, "11 bit Packet Channel Request "
858 "is not supported (PBCCH is deprecated)\n");
859 return -ENOTSUP;
860 }
861
862 /* 3GPP TS 44.018, table 9.1.8.1: 8 bit CHANNEL REQUEST.
863 * Mask 01110xxx indicates single block packet access. */
864 chan_req->single_block = ((rip->ra & 0xf8) == 0x70);
865 break;
866 case GSM_L1_BURST_TYPE_ACCESS_1:
867 case GSM_L1_BURST_TYPE_ACCESS_2:
868 if (!rip->is_11bit) { /* TS1/TS2 => EGPRS Packet Channel Request (always 11 bit) */
869 LOGP(DRLCMAC, LOGL_ERROR, "11 bit Packet Channel Request "
870 "is not supported (PBCCH is deprecated)\n");
871 return -ENOTSUP;
872 }
873
874 rc = parse_egprs_pkt_ch_req(rip->ra, chan_req);
875 if (rc)
876 return rc;
Max137fd592018-01-05 15:53:08 +0100877 break;
878 default:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700879 LOGP(DRLCMAC, LOGL_ERROR, "RACH.ind contains unknown burst type 0x%02x "
880 "(%u bit)\n", rip->burst_type, rip->is_11bit ? 11 : 8);
881 return -EINVAL;
Max58818582018-01-05 15:04:50 +0100882 }
883
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700884 return 0;
Max58818582018-01-05 15:04:50 +0100885}
886
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700887int BTS::rcv_rach(const struct rach_ind_params *rip)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200888{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700889 struct chan_req_params chan_req = { 0 };
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200890 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700891 uint8_t trx_no, ts_no;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200892 uint32_t sb_fn = 0;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100893 uint8_t usf = 7;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700894 uint8_t tsc = 0;
895 int plen, rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200896
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200897 do_rate_ctr_inc(CTR_RACH_REQUESTS);
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100898
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700899 if (rip->is_11bit)
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200900 do_rate_ctr_inc(CTR_11BIT_RACH_REQUESTS);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530901
Philipp Maier1275a3f2017-02-21 19:35:23 +0100902 /* Determine full frame number */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700903 uint32_t Fn = rfn_to_fn(rip->rfn);
904 uint8_t ta = qta2ta(rip->qta);
Philippd935d882016-11-07 13:07:36 +0100905
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700906 send_gsmtap_rach(PCU_GSMTAP_C_UL_RACH, GSMTAP_CHANNEL_RACH, rip);
Pau Espin Pedrol1ec211f2019-09-30 18:33:14 +0200907
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700908 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests Uplink resource on CCCH/RACH: "
909 "ra=0x%02x (%d bit) Fn=%u qta=%d\n", rip->ra,
910 rip->is_11bit ? 11 : 8, Fn, rip->qta);
bhargava628dcfb2016-09-14 20:30:31 +0530911
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700912 /* Parse [EGPRS Packet] Channel Request from RACH.ind */
913 rc = parse_rach_ind(rip, &chan_req);
914 if (rc) /* Send RR Immediate Assignment Reject */
915 goto send_imm_ass_rej;
bhargava628dcfb2016-09-14 20:30:31 +0530916
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700917 if (chan_req.single_block)
918 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
919 else if (m_bts.force_two_phase) {
920 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation, "
921 "but we force two phase access\n");
922 chan_req.single_block = true;
923 }
924
Vadim Yanitskiyb90d34b2020-05-21 20:20:34 +0700925 /* TODO: handle Radio Priority (see 3GPP TS 44.060, table 11.2.5a.5) */
926 if (chan_req.priority > 0)
927 LOGP(DRLCMAC, LOGL_NOTICE, "EGPRS Packet Channel Request indicates "
928 "Radio Priority %u, however we ignore it\n", chan_req.priority);
929
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700930 /* Should we allocate a single block or an Uplink TBF? */
931 if (chan_req.single_block) {
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100932 rc = sba()->alloc(&trx_no, &ts_no, &sb_fn, ta);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530933 if (rc < 0) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530934 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700935 "single block allocation: rc=%d\n", rc);
936 /* Send RR Immediate Assignment Reject */
937 goto send_imm_ass_rej;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530938 }
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700939
940 tsc = m_bts.trx[trx_no].pdch[ts_no].tsc;
941 LOGP(DRLCMAC, LOGL_DEBUG, "Allocated a single block at "
942 "SBFn=%u TRX=%u TS=%u\n", sb_fn, trx_no, ts_no);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200943 } else {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700944 GprsMs *ms = ms_alloc(0, chan_req.egprs_mslot_class);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200945 tbf = tbf_alloc_ul_tbf(&m_bts, ms, -1, true);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200946 if (!tbf) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700947 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for Uplink TBF\n");
948 /* Send RR Immediate Assignment Reject */
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530949 rc = -EBUSY;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700950 goto send_imm_ass_rej;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200951 }
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700952
953 /* FIXME: Copy and paste with other routines.. */
954 tbf->set_ta(ta);
955 TBF_SET_STATE(tbf, GPRS_RLCMAC_FLOW);
956 TBF_ASS_TYPE_SET(tbf, GPRS_RLCMAC_FLAG_CCCH);
957 T_START(tbf, T3169, 3169, "RACH (new UL-TBF)", true);
958 trx_no = tbf->trx->trx_no;
959 ts_no = tbf->first_ts;
960 usf = tbf->m_usf[ts_no];
961 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200962 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100963
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700964send_imm_ass_rej:
965 /* Allocate a bit-vector for RR Immediate Assignment [Reject] */
966 struct bitvec *bv = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Vadim Yanitskiyeb1e0fa2020-07-18 20:33:08 +0700967 bitvec_unhex(bv, DUMMY_VEC); /* standard '2B'O padding */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100968
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700969 if (rc != 0) {
970 LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment Reject on AGCH\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530971 plen = Encoding::write_immediate_assignment_reject(
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700972 bv, rip->ra, Fn, rip->burst_type);
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200973 do_rate_ctr_inc(CTR_IMMEDIATE_ASSIGN_REJ);
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700974 } else {
975 LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment on AGCH: "
976 "TRX=%u (ARFCN %u) TS=%u TA=%u TSC=%u TFI=%d USF=%d\n",
977 trx_no, m_bts.trx[trx_no].arfcn & ~ARFCN_FLAG_MASK,
978 ts_no, ta, tsc, tbf ? tbf->tfi() : -1, usf);
979 plen = Encoding::write_immediate_assignment(
Vadim Yanitskiy11567762020-07-18 23:34:17 +0700980 &m_bts.trx[trx_no].pdch[ts_no], tbf, bv,
981 false, rip->ra, Fn, ta, usf, false, sb_fn,
982 m_bts.alpha, m_bts.gamma, -1,
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700983 rip->burst_type);
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200984 do_rate_ctr_inc(CTR_IMMEDIATE_ASSIGN_UL_TBF);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530985 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100986
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700987 if (plen >= 0)
988 pcu_l1if_tx_agch(bv, plen);
989 else
990 rc = plen;
991
992 bitvec_free(bv);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200993
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530994 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200995}
996
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700997/* PTCCH/U sub-slot / frame-number mapping (see 3GPP TS 45.002, table 6) */
998static uint32_t ptcch_slot_map[PTCCH_TAI_NUM] = {
999 12, 38, 64, 90,
1000 116, 142, 168, 194,
1001 220, 246, 272, 298,
1002 324, 350, 376, 402,
1003};
1004
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001005int BTS::rcv_ptcch_rach(const struct rach_ind_params *rip)
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001006{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001007 uint32_t fn416 = rfn_to_fn(rip->rfn) % 416;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001008 struct gprs_rlcmac_bts *bts = bts_data();
1009 struct gprs_rlcmac_pdch *pdch;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001010 uint8_t ss;
1011
Vadim Yanitskiy07b64872020-06-11 00:26:15 +07001012 send_gsmtap_rach(PCU_GSMTAP_C_UL_PTCCH, GSMTAP_CHANNEL_PTCCH, rip);
1013
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001014 /* Prevent buffer overflow */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001015 if (rip->trx_nr >= ARRAY_SIZE(bts->trx) || rip->ts_nr >= 8) {
1016 LOGP(DRLCMAC, LOGL_ERROR, "(TRX=%u TS=%u RFN=%u) Rx malformed "
1017 "RACH.ind (PTCCH/U)\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001018 return -EINVAL;
1019 }
1020
1021 /* Make sure PDCH time-slot is enabled */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001022 pdch = &bts->trx[rip->trx_nr].pdch[rip->ts_nr];
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001023 if (!pdch->m_is_enabled) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001024 LOGP(DRLCMAC, LOGL_NOTICE, "(TRX=%u TS=%u RFN=%u) Rx RACH.ind (PTCCH/U) "
1025 "for inactive PDCH\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001026 return -EAGAIN;
1027 }
1028
1029 /* Convert TDMA frame-number to PTCCH/U sub-slot number */
1030 for (ss = 0; ss < PTCCH_TAI_NUM; ss++)
1031 if (ptcch_slot_map[ss] == fn416)
1032 break;
1033 if (ss == PTCCH_TAI_NUM) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001034 LOGP(DRLCMAC, LOGL_ERROR, "(TRX=%u TS=%u RFN=%u) Failed to map "
1035 "PTCCH/U sub-slot\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001036 return -ENODEV;
1037 }
1038
1039 /* Apply the new Timing Advance value */
1040 LOGP(DRLCMAC, LOGL_INFO, "Continuous Timing Advance update "
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +07001041 "for TAI %u, new TA is %u\n", ss, qta2ta(rip->qta));
1042 pdch->update_ta(ss, qta2ta(rip->qta));
Vadim Yanitskiyffebd242019-10-05 23:45:31 +07001043
1044 return 0;
1045}
1046
Pau Espin Pedrold7c32652019-12-23 12:41:34 +01001047void BTS::snd_dl_ass(gprs_rlcmac_tbf *tbf, bool poll, uint16_t pgroup)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +02001048{
Vadim Yanitskiy11567762020-07-18 23:34:17 +07001049 uint8_t trx_no = tbf->trx->trx_no;
1050 uint8_t ts_no = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +02001051 int plen;
1052
Maxc21f0072017-12-15 17:36:45 +01001053 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +01001054 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Vadim Yanitskiyeb1e0fa2020-07-18 20:33:08 +07001055 bitvec_unhex(immediate_assignment, DUMMY_VEC); /* standard '2B'O padding */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +02001056 /* use request reference that has maximum distance to current time,
1057 * so the assignment will not conflict with possible RACH requests. */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +01001058 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d pollFN=%d\n",
Vadim Yanitskiy11567762020-07-18 23:34:17 +07001059 trx_no, tbf->trx->arfcn, ts_no, tbf->ta(), poll ? tbf->poll_fn : -1);
1060 plen = Encoding::write_immediate_assignment(&m_bts.trx[trx_no].pdch[ts_no],
1061 tbf, immediate_assignment, true, 125,
1062 (tbf->pdch[ts_no]->last_rts_fn + 21216) % GSM_MAX_FN,
1063 tbf->ta(), 7, poll, tbf->poll_fn,
1064 m_bts.alpha, m_bts.gamma, -1,
Maxfc8afc22019-02-18 18:52:38 +01001065 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +05301066 if (plen >= 0) {
Pau Espin Pedrol2338e532020-05-12 20:54:35 +02001067 do_rate_ctr_inc(CTR_IMMEDIATE_ASSIGN_DL_TBF);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +01001068 pcu_l1if_tx_pch(immediate_assignment, plen, pgroup);
Mrinal Mishraf86307e2016-11-10 18:16:30 +05301069 }
1070
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +02001071 bitvec_free(immediate_assignment);
1072}
1073
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001074/* return maximum DL CS supported by BTS and allowed by VTY */
1075uint8_t BTS::max_cs_dl(void) const
1076{
1077 return m_max_cs_dl;
1078}
1079
1080/* return maximum UL CS supported by BTS and allowed by VTY */
1081uint8_t BTS::max_cs_ul(void) const
1082{
1083 return m_max_cs_ul;
1084}
1085
1086/* return maximum DL MCS supported by BTS and allowed by VTY */
1087uint8_t BTS::max_mcs_dl(void) const
1088{
1089 return m_max_mcs_dl;
1090}
1091
1092/* return maximum UL MCS supported by BTS and allowed by VTY */
1093uint8_t BTS::max_mcs_ul(void) const
1094{
1095 return m_max_mcs_ul;
1096}
1097
1098/* Set maximum DL CS supported by BTS and allowed by VTY */
1099void BTS::set_max_cs_dl(uint8_t cs_dl)
1100{
1101 m_max_cs_dl = cs_dl;
1102}
1103
1104/* Set maximum UL CS supported by BTS and allowed by VTY */
1105void BTS::set_max_cs_ul(uint8_t cs_ul)
1106{
1107 m_max_cs_ul = cs_ul;
1108}
1109
1110/* Set maximum DL MCS supported by BTS and allowed by VTY */
1111void BTS::set_max_mcs_dl(uint8_t mcs_dl)
1112{
1113 m_max_mcs_dl = mcs_dl;
1114}
1115
1116/* Set maximum UL MCS supported by BTS and allowed by VTY */
1117void BTS::set_max_mcs_ul(uint8_t mcs_ul)
1118{
1119 m_max_mcs_ul = mcs_ul;
1120}
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +02001121
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +01001122bool BTS::cs_dl_is_supported(CodingScheme cs)
1123{
1124 OSMO_ASSERT(mcs_is_valid(cs));
1125 uint8_t num = mcs_chan_code(cs);
1126 if (mcs_is_gprs(cs)) {
1127 return (max_cs_dl() >= num) && (m_bts.cs_mask & (1U << num));
1128 } else {
1129 return (max_mcs_dl() >= num) && (m_bts.mcs_mask & (1U << num));
1130 }
1131}
1132
Jacob Erlbeck86b6f052015-11-27 15:17:34 +01001133GprsMs *BTS::ms_alloc(uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001134{
1135 GprsMs *ms;
1136 ms = ms_store().create_ms();
1137
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001138 ms_set_timeout(ms, osmo_tdef_get(m_bts.T_defs_pcu, -2030, OSMO_TDEF_S, -1));
1139 ms_set_ms_class(ms, ms_class);
1140 ms_set_egprs_ms_class(ms, egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001141
1142 return ms;
1143}
1144
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001145
1146static int bts_talloc_destructor(struct BTS* bts)
1147{
1148 bts->~BTS();
1149 return 0;
1150}
1151
1152struct BTS* bts_alloc(struct gprs_pcu *pcu)
1153{
1154 struct BTS* bts;
1155 bts = talloc(pcu, struct BTS);
1156 if (!bts)
1157 return bts;
1158 talloc_set_destructor(bts, bts_talloc_destructor);
1159 new (bts) BTS(pcu);
1160 return bts;
1161}
1162
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001163/* update TA based on TA provided by PH-DATA-IND */
1164void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
1165{
1166 int16_t ta_adj;
1167 uint8_t ta_target;
1168
1169 if (ta_delta) {
1170 /* adjust TA based on TA provided by PH-DATA-IND */
1171 ta_adj = tbf->ta() + ta_delta;
1172
1173 /* limit target TA in range 0..63 bits */
1174 ta_target = ta_limit(ta_adj);
1175
1176 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating TLLI=0x%08x: TA %u -> %u on "
1177 "TRX = %d, TS = %d, FN = %d\n",
1178 tbf->tlli(), tbf->ta(), ta_target,
1179 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
1180 tbf->set_ta(ta_target);
1181 }
1182}
1183
1184/* set TA based on TA provided by PH-RA-IND */
1185void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
1186{
1187 uint8_t ta_target;
1188
1189 if (tbf->ta() != ta) {
1190 /* limit target TA in range 0..63 bits */
1191 ta_target = ta_limit(ta);
1192
1193 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating TLLI=0x%08x: TA %u -> %u on "
1194 "TRX = %d, TS = %d, FN = %d\n",
1195 tbf->tlli(), tbf->ta(), ta_target,
1196 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
1197 tbf->set_ta(ta_target);
1198 }
1199}
1200
1201void bts_update_tbf_ta(const char *p, uint32_t fn, uint8_t trx_no, uint8_t ts, int8_t ta, bool is_rach)
Maxd71e8b32016-09-19 16:17:06 +02001202{
1203 struct gprs_rlcmac_ul_tbf *tbf =
1204 bts_main_data()->bts->ul_tbf_by_poll_fn(fn, trx_no, ts);
1205 if (!tbf)
1206 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
1207 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
1208 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001209 else {
1210 /* we need to distinguish TA information provided by L1
1211 * from PH-DATA-IND and PHY-RA-IND so that we can properly
1212 * update TA for given TBF
1213 */
1214 if (is_rach)
1215 set_tbf_ta(tbf, (uint8_t)ta);
1216 else
1217 update_tbf_ta(tbf, ta);
1218
Maxd71e8b32016-09-19 16:17:06 +02001219 }
1220}
1221
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001222void bts_trx_reserve_slots(struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001223 uint8_t slots)
1224{
1225 unsigned i;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001226 for (i = 0; i < ARRAY_SIZE(trx->pdch); i += 1)
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001227 if (slots & (1 << i))
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001228 trx->pdch[i].reserve(dir);
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001229}
1230
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001231void bts_trx_unreserve_slots(struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001232 uint8_t slots)
1233{
1234 unsigned i;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001235 for (i = 0; i < ARRAY_SIZE(trx->pdch); i += 1)
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001236 if (slots & (1 << i))
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001237 trx->pdch[i].unreserve(dir);
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001238}
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001239
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001240void bts_recalc_max_cs(struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001241{
1242 int i;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001243 uint8_t cs_dl, cs_ul;
1244 struct gprs_pcu *pcu = bts->bts->pcu;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001245
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001246 cs_dl = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001247 for (i = pcu->vty.max_cs_dl - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001248 if (bts->cs_mask & (1 << i)) {
1249 cs_dl = i + 1;
1250 break;
1251 }
1252 }
1253
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001254 cs_ul = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001255 for (i = pcu->vty.max_cs_ul - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001256 if (bts->cs_mask & (1 << i)) {
1257 cs_ul = i + 1;
1258 break;
1259 }
1260 }
1261
1262 LOGP(DRLCMAC, LOGL_DEBUG, "New max CS: DL=%u UL=%u\n", cs_dl, cs_ul);
1263 bts->bts->set_max_cs_dl(cs_dl);
1264 bts->bts->set_max_cs_ul(cs_ul);
1265}
1266
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001267void bts_recalc_max_mcs(struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001268{
1269 int i;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001270 uint8_t mcs_dl, mcs_ul;
1271 struct gprs_pcu *pcu = bts->bts->pcu;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001272
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001273 mcs_dl = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001274 for (i = pcu->vty.max_mcs_dl - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001275 if (bts->mcs_mask & (1 << i)) {
1276 mcs_dl = i + 1;
1277 break;
1278 }
1279 }
1280
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001281 mcs_ul = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001282 for (i = pcu->vty.max_mcs_ul - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001283 if (bts->mcs_mask & (1 << i)) {
1284 mcs_ul = i + 1;
1285 break;
1286 }
1287 }
1288
1289 LOGP(DRLCMAC, LOGL_DEBUG, "New max MCS: DL=%u UL=%u\n", mcs_dl, mcs_ul);
1290 bts->bts->set_max_mcs_dl(mcs_dl);
1291 bts->bts->set_max_mcs_ul(mcs_ul);
1292}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001293
1294
1295struct gprs_rlcmac_bts *bts_data(struct BTS *bts)
1296{
1297 return &bts->m_bts;
1298}
1299
1300struct GprsMs *bts_ms_by_imsi(struct BTS *bts, const char *imsi)
1301{
1302 return bts->ms_by_imsi(imsi);
1303}
1304
1305uint8_t bts_max_cs_dl(const struct BTS *bts)
1306{
1307 return bts->max_cs_dl();
1308}
1309
1310uint8_t bts_max_cs_ul(const struct BTS *bts)
1311{
1312 return bts->max_cs_ul();
1313}
1314
1315uint8_t bts_max_mcs_dl(const struct BTS *bts)
1316{
1317 return bts->max_mcs_dl();
1318}
1319
1320uint8_t bts_max_mcs_ul(const struct BTS *bts)
1321{
1322 return bts->max_mcs_ul();
1323}