blob: c2b86964da1ce889ee4112c9665337d5ea26b336 [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};
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020079
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +010080/**
81 * For gcc-4.4 compat do not use extended initializer list but keep the
82 * order from the enum here. Once we support GCC4.7 and up we can change
83 * the code below.
84 */
85static const struct rate_ctr_desc bts_ctr_description[] = {
Maxa5eb67d2017-11-21 20:24:54 +010086 { "tbf:dl:alloc", "TBF DL Allocated "},
87 { "tbf:dl:freed", "TBF DL Freed "},
88 { "tbf:dl:aborted", "TBF DL Aborted "},
89 { "tbf:ul:alloc", "TBF UL Allocated "},
90 { "tbf:ul:freed", "TBF UL Freed "},
91 { "tbf:ul:aborted", "TBF UL Aborted "},
92 { "tbf:reused", "TBF Reused "},
93 { "tbf:alloc:algo-a", "TBF Alloc Algo A "},
94 { "tbf:alloc:algo-b", "TBF Alloc Algo B "},
Maxa5eb67d2017-11-21 20:24:54 +010095 { "rlc:sent", "RLC Sent "},
96 { "rlc:resent", "RLC Resent "},
97 { "rlc:restarted", "RLC Restarted "},
98 { "rlc:stalled", "RLC Stalled "},
99 { "rlc:nacked", "RLC Nacked "},
100 { "rlc:final_block_resent", "RLC Final Blk resent "},
101 { "rlc:ass:timedout", "RLC Assign Timeout "},
102 { "rlc:ass:failed", "RLC Assign Failed "},
103 { "rlc:ack:timedout", "RLC Ack Timeout "},
104 { "rlc:ack:failed", "RLC Ack Failed "},
105 { "rlc:rel:timedout", "RLC Release Timeout "},
106 { "rlc:late-block", "RLC Late Block "},
107 { "rlc:sent-dummy", "RLC Sent Dummy "},
108 { "rlc:sent-control", "RLC Sent Control "},
109 { "rlc:dl_bytes", "RLC DL Bytes "},
110 { "rlc:dl_payload_bytes", "RLC DL Payload Bytes "},
111 { "rlc:ul_bytes", "RLC UL Bytes "},
112 { "rlc:ul_payload_bytes", "RLC UL Payload Bytes "},
113 { "decode:errors", "Decode Errors "},
114 { "sba:allocated", "SBA Allocated "},
115 { "sba:freed", "SBA Freed "},
116 { "sba:timedout", "SBA Timeout "},
117 { "llc:timeout", "Timedout Frames "},
118 { "llc:dropped", "Dropped Frames "},
119 { "llc:scheduled", "Scheduled Frames "},
120 { "llc:dl_bytes", "RLC encapsulated PDUs"},
121 { "llc:ul_bytes", "full PDUs received "},
122 { "rach:requests", "RACH requests "},
123 { "11bit_rach:requests", "11BIT_RACH requests "},
124 { "spb:uplink_first_segment", "First seg of UL SPB "},
125 { "spb:uplink_second_segment", "Second seg of UL SPB "},
126 { "spb:downlink_first_segment", "First seg of DL SPB "},
127 { "spb:downlink_second_segment","Second seg of DL SPB "},
128 { "immediate:assignment_UL", "Immediate Assign UL "},
129 { "immediate:assignment_rej", "Immediate Assign Rej "},
130 { "immediate:assignment_DL", "Immediate Assign DL "},
131 { "channel:request_description","Channel Request Desc "},
132 { "pkt:ul_assignment", "Packet UL Assignment "},
133 { "pkt:access_reject", "Packet Access Reject "},
134 { "pkt:dl_assignment", "Packet DL Assignment "},
135 { "ul:control", "UL control Block "},
136 { "ul:assignment_poll_timeout", "UL Assign Timeout "},
137 { "ul:assignment_failed", "UL Assign Failed "},
138 { "dl:assignment_timeout", "DL Assign Timeout "},
139 { "dl:assignment_failed", "DL Assign Failed "},
140 { "pkt:ul_ack_nack_timeout", "PUAN Poll Timeout "},
141 { "pkt:ul_ack_nack_failed", "PUAN poll Failed "},
142 { "pkt:dl_ack_nack_timeout", "PDAN poll Timeout "},
143 { "pkt:dl_ack_nack_failed", "PDAN poll Failed "},
144 { "gprs:downlink_cs1", "CS1 downlink "},
145 { "gprs:downlink_cs2", "CS2 downlink "},
146 { "gprs:downlink_cs3", "CS3 downlink "},
147 { "gprs:downlink_cs4", "CS4 downlink "},
148 { "egprs:downlink_mcs1", "MCS1 downlink "},
149 { "egprs:downlink_mcs2", "MCS2 downlink "},
150 { "egprs:downlink_mcs3", "MCS3 downlink "},
151 { "egprs:downlink_mcs4", "MCS4 downlink "},
152 { "egprs:downlink_mcs5", "MCS5 downlink "},
153 { "egprs:downlink_mcs6", "MCS6 downlink "},
154 { "egprs:downlink_mcs7", "MCS7 downlink "},
155 { "egprs:downlink_mcs8", "MCS8 downlink "},
156 { "egprs:downlink_mcs9", "MCS9 downlink "},
157 { "gprs:uplink_cs1", "CS1 Uplink "},
158 { "gprs:uplink_cs2", "CS2 Uplink "},
159 { "gprs:uplink_cs3", "CS3 Uplink "},
160 { "gprs:uplink_cs4", "CS4 Uplink "},
161 { "egprs:uplink_mcs1", "MCS1 Uplink "},
162 { "egprs:uplink_mcs2", "MCS2 Uplink "},
163 { "egprs:uplink_mcs3", "MCS3 Uplink "},
164 { "egprs:uplink_mcs4", "MCS4 Uplink "},
165 { "egprs:uplink_mcs5", "MCS5 Uplink "},
166 { "egprs:uplink_mcs6", "MCS6 Uplink "},
167 { "egprs:uplink_mcs7", "MCS7 Uplink "},
168 { "egprs:uplink_mcs8", "MCS8 Uplink "},
169 { "egprs:uplink_mcs9", "MCS9 Uplink "},
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100170};
171
172static const struct rate_ctr_group_desc bts_ctrg_desc = {
173 "bts",
174 "BTS Statistics",
Holger Hans Peter Freytherb8a54262015-11-07 21:00:00 +0100175 OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100176 ARRAY_SIZE(bts_ctr_description),
177 bts_ctr_description,
178};
179
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100180static const struct osmo_stat_item_desc bts_stat_item_description[] = {
181 { "ms.present", "MS Present ",
182 OSMO_STAT_ITEM_NO_UNIT, 4, 0},
183};
184
185static const struct osmo_stat_item_group_desc bts_statg_desc = {
186 "bts",
187 "BTS Statistics",
188 OSMO_STATS_CLASS_GLOBAL,
189 ARRAY_SIZE(bts_stat_item_description),
190 bts_stat_item_description,
191};
192
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100193static void bts_init(struct gprs_rlcmac_bts *bts, BTS* bts_obj)
194{
195 memset(bts, 0, sizeof(*bts));
196 bts->fc_interval = 1;
197 bts->initial_cs_dl = bts->initial_cs_ul = 1;
198 bts->initial_mcs_dl = bts->initial_mcs_ul = 1;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100199 bts->cs_mask = 1 << 0; /* CS-1 always enabled by default */
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100200 bts->n3101 = 10;
201 bts->n3103 = 4;
202 bts->n3105 = 8;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100203 bts->si13_is_set = false;
Alexander Couzens3db47892020-12-16 21:26:26 +0100204 bts->ns_dialect = NS2_DIALECT_IPACCESS;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100205
206 /* TODO: increase them when CRBB decoding is implemented */
207 bts->ws_base = 64;
208 bts->ws_pdch = 0;
209
210 bts->llc_codel_interval_msec = LLC_CODEL_USE_DEFAULT;
211 bts->llc_idle_ack_csec = 10;
212
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100213 bts->app_info = NULL;
214 bts->bts = bts_obj;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100215 bts->T_defs_bts = T_defs_bts;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100216 osmo_tdefs_reset(bts->T_defs_bts);
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100217
218 /* initialize back pointers */
219 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
220 struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
221 trx->trx_no = trx_no;
222 trx->bts = bts_obj;
223
224 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
225 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
226 pdch->init_ptcch_msg();
227 pdch->ts_no = ts_no;
228 pdch->trx = trx;
229 }
230 }
231}
232
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200233BTS* BTS::main_bts()
234{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100235 return the_pcu->bts;
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200236}
237
238struct gprs_rlcmac_bts *BTS::bts_data()
239{
240 return &m_bts;
241}
242
243struct gprs_rlcmac_bts *bts_main_data()
244{
245 return BTS::main_bts()->bts_data();
246}
247
Maxd5ffeb52019-03-18 15:48:38 +0100248void bts_cleanup()
249{
250 return BTS::main_bts()->cleanup();
251}
252
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100253struct rate_ctr_group *bts_main_data_stats()
254{
255 return BTS::main_bts()->rate_counters();
256}
257
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100258BTS::BTS(struct gprs_pcu *pcu)
259 : pcu(pcu)
260 , m_cur_fn(0)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200261 , m_cur_blk_fn(-1)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +0100262 , m_max_cs_dl(MAX_GPRS_CS)
263 , m_max_cs_ul(MAX_GPRS_CS)
264 , m_max_mcs_dl(MAX_EDGE_MCS)
265 , m_max_mcs_ul(MAX_EDGE_MCS)
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +0200266 , m_pollController(*this)
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200267 , m_sba(*this)
Jacob Erlbeck17214bb2015-06-02 14:06:12 +0200268 , m_ms_store(this)
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200269{
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100270 bts_init(&m_bts, this);
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100271
Maxea9968f2017-12-18 22:52:52 +0100272 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100273 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100274 than just allocate the group with different index.
275 This shall be removed once weget rid of BTS singleton */
276 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
277 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
278 else
279 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200280 OSMO_ASSERT(m_ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100281
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100282 m_statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200283 OSMO_ASSERT(m_statg);
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200284}
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200285
Maxd5ffeb52019-03-18 15:48:38 +0100286void BTS::cleanup()
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100287{
Jacob Erlbeckc362df22016-01-20 22:02:19 +0100288 /* this can cause counter updates and must not be left to the
289 * m_ms_store's destructor */
290 m_ms_store.cleanup();
291
Maxd5ffeb52019-03-18 15:48:38 +0100292 if (m_ratectrs) {
293 rate_ctr_group_free(m_ratectrs);
294 m_ratectrs = NULL;
295 }
296
297 if (m_statg) {
298 osmo_stat_item_group_free(m_statg);
299 m_statg = NULL;
300 }
Oliver Smithcfb63212019-09-05 17:13:33 +0200301
302 if (m_bts.app_info) {
303 msgb_free(m_bts.app_info);
304 m_bts.app_info = NULL;
305 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100306}
307
Maxd5ffeb52019-03-18 15:48:38 +0100308BTS::~BTS()
309{
310 cleanup();
311}
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100312
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200313void BTS::set_current_frame_number(int fn)
314{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200315 /* The UL frame numbers lag 3 behind the DL frames and the data
316 * indication is only sent after all 4 frames of the block have been
317 * received. Sometimes there is an idle frame between the end of one
318 * and start of another frame (every 3 blocks). So the timeout should
319 * definitely be there if we're more than 8 frames past poll_fn. Let's
Jacob Erlbecke77d49f2015-08-27 13:28:05 +0200320 * stay on the safe side and say 13 or more. An additional delay can
321 * happen due to the block processing time in the DSP, so the delay of
322 * decoded blocks relative to the timing clock can be much larger.
323 * Values up to 50 frames have been observed under load. */
324 const static int max_delay = 60;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200325
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200326 m_cur_fn = fn;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200327 m_pollController.expireTimedout(m_cur_fn, max_delay);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200328}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200329
Max9dabfa22017-05-16 16:10:45 +0200330static inline int delta_fn(int fn, int to)
331{
332 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
333}
334
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200335void BTS::set_current_block_frame_number(int fn, unsigned max_delay)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200336{
337 int delay = 0;
338 const int late_block_delay_thresh = 13;
339 const int fn_update_ok_min_delay = -500;
340 const int fn_update_ok_max_delay = 0;
341
342 /* frame numbers in the received blocks are assumed to be strongly
343 * monotonic. */
344 if (m_cur_blk_fn >= 0) {
Max9dabfa22017-05-16 16:10:45 +0200345 int delta = delta_fn(fn, m_cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200346 if (delta <= 0)
347 return;
348 }
349
350 /* Check block delay vs. the current frame number */
351 if (current_frame_number() != 0)
Max9dabfa22017-05-16 16:10:45 +0200352 delay = delta_fn(fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200353 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200354 LOGP(DRLCMAC, LOGL_NOTICE,
355 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
356 delay, fn, current_frame_number());
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200357 do_rate_ctr_inc(CTR_RLC_LATE_BLOCK);
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200358 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200359
360 m_cur_blk_fn = fn;
361 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
362 current_frame_number() == 0)
363 m_cur_fn = fn;
364
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200365 m_pollController.expireTimedout(fn, max_delay);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200366}
367
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200368int BTS::add_paging(uint8_t chan_needed, const struct osmo_mobile_identity *mi)
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200369{
370 uint8_t l, trx, ts, any_tbf = 0;
371 struct gprs_rlcmac_tbf *tbf;
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100372 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200373 uint8_t slot_mask[8];
374 int8_t first_ts; /* must be signed */
375
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100376 LListHead<gprs_rlcmac_tbf> *tbfs_lists[] = {
377 &m_ul_tbfs,
378 &m_dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200379 NULL
380 };
381
Neels Hofmeyr00520512020-08-21 15:44:58 +0200382 if (log_check_level(DRLCMAC, LOGL_INFO)) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200383 char str[64];
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200384 osmo_mobile_identity_to_str_buf(str, sizeof(str), mi);
Neels Hofmeyr00520512020-08-21 15:44:58 +0200385 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n", chan_needed, str);
386 }
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200387
388 /* collect slots to page
389 * Mark slots for every TBF, but only mark one of it.
390 * Mark only the first slot found.
391 * Don't mark, if TBF uses a different slot that is already marked. */
392 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200393 for (l = 0; tbfs_lists[l]; l++) {
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100394 llist_for_each(pos, tbfs_lists[l]) {
395 tbf = pos->entry();
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200396 first_ts = -1;
397 for (ts = 0; ts < 8; ts++) {
398 if (tbf->pdch[ts]) {
399 /* remember the first slot found */
400 if (first_ts < 0)
401 first_ts = ts;
402 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100403 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200404 break;
405 }
406 }
407 /* mark first slot found, if none is marked already */
408 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100409 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200410 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100411 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100412 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200413 } else
Maxc21f0072017-12-15 17:36:45 +0100414 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200415 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100416 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200417 }
418 }
419
420 /* Now we have a list of marked slots. Every TBF uses at least one
421 * of these slots. */
422
423 /* schedule paging to all marked slots */
424 for (trx = 0; trx < 8; trx++) {
425 if (slot_mask[trx] == 0)
426 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200427 for (ts = 0; ts < 8; ts++) {
428 if ((slot_mask[trx] & (1 << ts))) {
429 /* schedule */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200430 if (!m_bts.trx[trx].pdch[ts].add_paging(chan_needed, mi))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200431 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100432
433 LOGP(DRLCMAC, LOGL_INFO, "Paging on PACCH of TRX=%d TS=%d\n", trx, ts);
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200434 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200435 }
436 }
437 }
438
439 if (!any_tbf)
440 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
441
442 return 0;
443}
444
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700445void BTS::send_gsmtap_rach(enum pcu_gsmtap_category categ, uint8_t channel,
446 const struct rach_ind_params *rip)
447{
Vadim Yanitskiy55022ea2021-01-06 22:44:06 +0100448 struct pcu_l1_meas meas = { 0 };
Vadim Yanitskiy81b610e2020-06-11 01:04:44 +0700449 uint8_t ra_buf[2];
450
451 /* 3GPP TS 44.004 defines 11 bit RA as follows: xxxx xxxx .... .yyy
452 * On the PCUIF, we get 16 bit machne dependent number (LE/BE)
453 * Over GSMTAP we send the following: xxxx xxxx yyy. ....
454 * This simplifies parsing in Wireshark using its CSN.1 codec. */
455 if (rip->is_11bit) {
456 ra_buf[0] = (uint8_t) ((rip->ra >> 3) & 0xff);
457 ra_buf[1] = (uint8_t) ((rip->ra << 5) & 0xff);
458 } else {
459 ra_buf[0] = (uint8_t) (rip->ra & 0xff);
460 }
461
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700462 send_gsmtap_meas(categ, true, rip->trx_nr, rip->ts_nr, channel,
Vadim Yanitskiy81b610e2020-06-11 01:04:44 +0700463 rfn_to_fn(rip->rfn), ra_buf,
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700464 rip->is_11bit ? 2 : 1, &meas);
465}
466
Harald Welte717cdf52017-07-21 21:56:23 +0200467void BTS::send_gsmtap(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
468 uint8_t ts_no, uint8_t channel, uint32_t fn,
469 const uint8_t *data, unsigned int len)
470{
Vadim Yanitskiyeb70a402021-01-13 13:58:14 +0100471 struct pcu_l1_meas meas = { 0 };
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200472 send_gsmtap_meas(categ, uplink, trx_no, ts_no, channel, fn, data, len, &meas);
473}
474
475void BTS::send_gsmtap_meas(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
476 uint8_t ts_no, uint8_t channel, uint32_t fn,
477 const uint8_t *data, unsigned int len, struct pcu_l1_meas *meas)
478{
Harald Welte717cdf52017-07-21 21:56:23 +0200479 uint16_t arfcn;
480
481 /* check if category is activated at all */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100482 if (!(pcu->gsmtap_categ_mask & (1 << categ)))
Harald Welte717cdf52017-07-21 21:56:23 +0200483 return;
484
485 arfcn = m_bts.trx[trx_no].arfcn;
486 if (uplink)
487 arfcn |= GSMTAP_ARFCN_F_UPLINK;
488
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200489 /* GSMTAP needs the SNR here, but we only have C/I (meas->link_qual).
490 Those are not the same, but there is no known way to convert them,
491 let's pass C/I instead of nothing */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100492 gsmtap_send(pcu->gsmtap, arfcn, ts_no, channel, 0, fn,
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200493 meas->rssi, meas->link_qual, data, len);
Harald Welte717cdf52017-07-21 21:56:23 +0200494}
495
Max341dccd2017-08-14 15:49:59 +0200496static inline bool tbf_check(gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t trx_no, uint8_t ts)
497{
Maxcac6b662018-01-24 11:00:17 +0100498 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING) && tbf->poll_scheduled()
Max341dccd2017-08-14 15:49:59 +0200499 && tbf->poll_fn == fn && tbf->trx->trx_no == trx_no && tbf->poll_ts == ts)
500 return true;
501
502 return false;
503}
504
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200505gprs_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 +0200506{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100507 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200508
509 /* only one TBF can poll on specific TS/FN, because scheduler can only
510 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100511 llist_for_each(pos, &m_dl_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200512 if (tbf_check(pos->entry(), fn, trx, ts))
513 return as_dl_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200514 }
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200515 return NULL;
516}
Max341dccd2017-08-14 15:49:59 +0200517
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200518gprs_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 +0200519{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100520 LListHead<gprs_rlcmac_tbf> *pos;
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200521
522 /* only one TBF can poll on specific TS/FN, because scheduler can only
523 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100524 llist_for_each(pos, &m_ul_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200525 if (tbf_check(pos->entry(), fn, trx, ts))
526 return as_ul_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200527 }
528 return NULL;
529}
530
Daniel Willmann54044b02014-07-02 17:58:15 +0200531/* lookup downlink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200532gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200533{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200534 if (trx >= 8 || ts >= 8)
535 return NULL;
536
537 return m_bts.trx[trx].pdch[ts].dl_tbf_by_tfi(tfi);
Daniel Willmann54044b02014-07-02 17:58:15 +0200538}
539
540/* lookup uplink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200541gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200542{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200543 if (trx >= 8 || ts >= 8)
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200544 return NULL;
545
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200546 return m_bts.trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200547}
548
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100549static unsigned int trx_count_free_tfi(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t *first_free_tfi)
550{
551 const struct gprs_rlcmac_pdch *pdch;
552 uint8_t ts;
553 unsigned int i;
554 unsigned int free_tfi_cnt = 0;
555 bool has_pdch = false;
556 uint32_t mask = NO_FREE_TFI;
557
558 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
559 pdch = &trx->pdch[ts];
560 if (!pdch->is_enabled())
561 continue;
562 has_pdch = true;
563 mask &= ~pdch->assigned_tfi(dir);
564 }
565
566 if (!has_pdch || !mask) {
567 *first_free_tfi = (uint8_t)-1;
568 return 0;
569 }
570
571 /* Count free tfis and return */
572 for (i = 0; i < sizeof(mask) * 8 ; i++) {
573 if (mask & 1) {
574 if (free_tfi_cnt == 0)
575 *first_free_tfi = i;
576 free_tfi_cnt++;
577 }
578 mask >>= 1;
579 }
580 return free_tfi_cnt;
581}
582
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200583/*
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100584 * Search for free TFI and return TFI, TRX. This method returns the first TFI
585 * that is currently not used in any PDCH of a the TRX with least TFIs currently
586 * assigned. Negative values indicate errors.
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200587 */
Maxa76a7d02018-01-26 11:09:16 +0100588int 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 +0200589{
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100590 uint8_t trx_from, trx_to, trx;
591 uint8_t best_trx_nr = 0xff;
592 unsigned int best_cnt = 0;
593 uint8_t best_first_tfi = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200594
595 if (use_trx >= 0 && use_trx < 8)
596 trx_from = trx_to = use_trx;
597 else {
598 trx_from = 0;
599 trx_to = 7;
600 }
601
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200602 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200603 for (trx = trx_from; trx <= trx_to; trx++) {
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100604 uint8_t tmp_first_tfi;
605 unsigned int tmp_cnt;
606 tmp_cnt = trx_count_free_tfi(&m_bts.trx[trx], dir, &tmp_first_tfi);
607 if (tmp_cnt > best_cnt) {
608 best_cnt = tmp_cnt;
609 best_first_tfi = tmp_first_tfi;
610 best_trx_nr = trx;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200611 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200612 }
613
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100614 if (best_trx_nr == 0xff || best_cnt == 0) {
Max69d585e2018-01-26 12:36:43 +0100615 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200616 return -EBUSY;
617 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200618
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100619 OSMO_ASSERT(best_first_tfi < 32);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200620
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100621 LOGP(DRLCMAC, LOGL_DEBUG, "Found first unallocated TRX=%d TFI=%d\n",
622 best_trx_nr, best_first_tfi);
623 *_trx = best_trx_nr;
624 return best_first_tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200625}
626
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200627int BTS::rcv_imm_ass_cnf(const uint8_t *data, uint32_t fn)
628{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200629 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200630 uint8_t plen;
631 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200632 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200633
634 /* move to IA Rest Octets */
635 plen = data[0] >> 2;
636 data += 1 + plen;
637
638 if ((*data & 0xf0) != 0xd0) {
639 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
640 "octets do not start with bit sequence 'HH01' "
641 "(Packet Downlink Assignment)\n");
642 return -EINVAL;
643 }
644
645 /* get TLLI from downlink assignment */
Eric Wildab8b01e2019-07-23 17:16:06 +0200646 tlli = (uint32_t)((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200647 tlli |= (*data++) << 20;
648 tlli |= (*data++) << 12;
649 tlli |= (*data++) << 4;
650 tlli |= (*data++) >> 4;
651
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200652 ms = ms_by_tlli(tlli);
653 if (ms)
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100654 dl_tbf = ms_dl_tbf(ms);
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200655 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200656 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
657 "does not exit\n", tlli);
658 return -EINVAL;
659 }
660
661 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
662
Daniel Willmann7e994e32014-08-07 15:49:21 +0200663 if (dl_tbf->m_wait_confirm)
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200664 T_START(dl_tbf, T0, -2002, "assignment (AGCH)", true);
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200665
666 return 0;
667}
668
Philipp Maier1275a3f2017-02-21 19:35:23 +0100669/* Determine the full frame number from a relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100670uint32_t BTS::rfn_to_fn(int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100671{
Max5dd8d1b2017-03-08 12:06:42 +0100672 int32_t m_cur_rfn;
673 int32_t fn;
674 int32_t fn_rounded;
675
676 /* double-check that relative FN is not negative and fits into int32_t */
677 OSMO_ASSERT(rfn < GSM_MAX_FN);
678 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100679
680 /* Note: If a BTS is sending in a rach request it will be fully aware
681 * of the frame number. If the PCU is used in a BSC-co-located setup.
682 * The BSC will forward the incoming RACH request. The RACH request
683 * only contains the relative frame number (Fn % 42432) in its request
684 * reference. This PCU implementation has to fit both scenarios, so
685 * we need to assume that Fn is a relative frame number. */
686
687 /* Ensure that all following calculations are performed with the
688 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100689 if (rfn >= RFN_MODULUS)
690 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100691
692 /* Compute an internal relative frame number from the full internal
693 frame number */
694 m_cur_rfn = m_cur_fn % RFN_MODULUS;
695
696 /* Compute a "rounded" version of the internal frame number, which
697 * exactly fits in the RFN_MODULUS raster */
698 fn_rounded = m_cur_fn - m_cur_rfn;
699
700 /* If the delta between the internal and the external relative frame
701 * number exceeds a certain limit, we need to assume that the incoming
702 * rach request belongs to a the previous rfn period. To correct this,
703 * we roll back the rounded frame number by one RFN_MODULUS */
704 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
705 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200706 "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 +0100707 rfn, m_cur_fn, RFN_MODULUS);
708 if (fn_rounded < RFN_MODULUS) {
709 LOGP(DRLCMAC, LOGL_DEBUG,
710 "Cornercase detected: wrapping crosses %u border\n",
711 GSM_MAX_FN);
712 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
713 }
714 else
715 fn_rounded -= RFN_MODULUS;
716 }
717
718 /* The real frame number is the sum of the rounded frame number and the
719 * relative framenumber computed via RACH */
720 fn = fn_rounded + rfn;
721
722 return fn;
723}
724
Pau Espin Pedrol9ee90d22020-05-08 14:20:32 +0200725/* 3GPP TS 44.060:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700726 * Table 11.2.5.2: PACKET CHANNEL REQUEST
727 * Table 11.2.5a.2: EGPRS PACKET CHANNEL REQUEST
728 * Both GPRS and EGPRS use same MultislotClass coding, but since PRACH is
Pau Espin Pedrol9ee90d22020-05-08 14:20:32 +0200729 * deprecated, no PACKET CHANNEL REQUEST exists, which means for GPRS we will
730 * receive CCCH RACH which doesn't contain any mslot class. Hence in the end we
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700731 * can only receive EGPRS mslot class through 11-bit EGPRS PACKET CHANNEL REQUEST. */
732static int parse_egprs_pkt_ch_req(uint16_t ra11, struct chan_req_params *chan_req)
Max58818582018-01-05 15:04:50 +0100733{
Vadim Yanitskiyb90d34b2020-05-21 20:20:34 +0700734 EGPRS_PacketChannelRequest_t req;
735 int rc;
736
737 rc = decode_egprs_pkt_ch_req(ra11, &req);
738 if (rc) {
739 LOGP(DRLCMAC, LOGL_NOTICE, "Failed to decode "
740 "EGPRS Packet Channel Request: rc=%d\n", rc);
741 return rc;
742 }
743
744 LOGP(DRLCMAC, LOGL_INFO, "Rx EGPRS Packet Channel Request: %s\n",
745 get_value_string(egprs_pkt_ch_req_type_names, req.Type));
746
747 switch (req.Type) {
748 case EGPRS_PKT_CHAN_REQ_ONE_PHASE:
749 chan_req->egprs_mslot_class = req.Content.MultislotClass + 1;
750 chan_req->priority = req.Content.Priority + 1;
751 break;
752 case EGPRS_PKT_CHAN_REQ_SHORT:
753 chan_req->priority = req.Content.Priority + 1;
754 if (req.Content.NumberOfBlocks == 0)
755 chan_req->single_block = true;
756 break;
757 case EGPRS_PKT_CHAN_REQ_ONE_PHASE_RED_LATENCY:
758 chan_req->priority = req.Content.Priority + 1;
759 break;
760 /* Two phase access => single block is needed */
761 case EGPRS_PKT_CHAN_REQ_TWO_PHASE:
762 case EGPRS_PKT_CHAN_REQ_TWO_PHASE_IPA:
763 chan_req->priority = req.Content.Priority + 1;
764 chan_req->single_block = true;
765 break;
766 /* Signalling => single block is needed */
767 case EGPRS_PKT_CHAN_REQ_SIGNALLING:
768 case EGPRS_PKT_CHAN_REQ_SIGNALLING_IPA:
769 chan_req->single_block = true;
770 break;
771
772 /* Neither unacknowledged RLC mode, nor emergency calls are supported */
773 case EGPRS_PKT_CHAN_REQ_ONE_PHASE_UNACK:
774 case EGPRS_PKT_CHAN_REQ_EMERGENCY_CALL:
775 case EGPRS_PKT_CHAN_REQ_DEDICATED_CHANNEL:
776 LOGP(DRLCMAC, LOGL_NOTICE, "%s is not supported, rejecting\n",
777 get_value_string(egprs_pkt_ch_req_type_names, req.Type));
778 return -ENOTSUP;
779
780 default:
781 LOGP(DRLCMAC, LOGL_ERROR, "Unknown EGPRS Packet Channel Request "
782 "type=0x%02x, probably a bug in CSN.1 codec\n", req.Type);
783 return -EINVAL;
784 }
Max58818582018-01-05 15:04:50 +0100785
786 return 0;
787}
788
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700789/* NOTE: chan_req needs to be zero-initialized by the caller */
790static int parse_rach_ind(const struct rach_ind_params *rip,
791 struct chan_req_params *chan_req)
Max58818582018-01-05 15:04:50 +0100792{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700793 int rc;
Max58818582018-01-05 15:04:50 +0100794
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700795 switch (rip->burst_type) {
Max137fd592018-01-05 15:53:08 +0100796 case GSM_L1_BURST_TYPE_NONE:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700797 LOGP(DRLCMAC, LOGL_ERROR, "RACH.ind contains no burst type, assuming TS0\n");
798 /* fall-through */
799 case GSM_L1_BURST_TYPE_ACCESS_0:
800 if (rip->is_11bit) { /* 11 bit Access Burst with TS0 => Packet Channel Request */
801 LOGP(DRLCMAC, LOGL_ERROR, "11 bit Packet Channel Request "
802 "is not supported (PBCCH is deprecated)\n");
803 return -ENOTSUP;
804 }
805
806 /* 3GPP TS 44.018, table 9.1.8.1: 8 bit CHANNEL REQUEST.
807 * Mask 01110xxx indicates single block packet access. */
808 chan_req->single_block = ((rip->ra & 0xf8) == 0x70);
809 break;
810 case GSM_L1_BURST_TYPE_ACCESS_1:
811 case GSM_L1_BURST_TYPE_ACCESS_2:
812 if (!rip->is_11bit) { /* TS1/TS2 => EGPRS Packet Channel Request (always 11 bit) */
813 LOGP(DRLCMAC, LOGL_ERROR, "11 bit Packet Channel Request "
814 "is not supported (PBCCH is deprecated)\n");
815 return -ENOTSUP;
816 }
817
818 rc = parse_egprs_pkt_ch_req(rip->ra, chan_req);
819 if (rc)
820 return rc;
Max137fd592018-01-05 15:53:08 +0100821 break;
822 default:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700823 LOGP(DRLCMAC, LOGL_ERROR, "RACH.ind contains unknown burst type 0x%02x "
824 "(%u bit)\n", rip->burst_type, rip->is_11bit ? 11 : 8);
825 return -EINVAL;
Max58818582018-01-05 15:04:50 +0100826 }
827
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700828 return 0;
Max58818582018-01-05 15:04:50 +0100829}
830
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700831int BTS::rcv_rach(const struct rach_ind_params *rip)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200832{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700833 struct chan_req_params chan_req = { 0 };
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200834 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700835 uint8_t trx_no, ts_no;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200836 uint32_t sb_fn = 0;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100837 uint8_t usf = 7;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700838 uint8_t tsc = 0;
839 int plen, rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200840
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200841 do_rate_ctr_inc(CTR_RACH_REQUESTS);
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100842
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700843 if (rip->is_11bit)
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200844 do_rate_ctr_inc(CTR_11BIT_RACH_REQUESTS);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530845
Philipp Maier1275a3f2017-02-21 19:35:23 +0100846 /* Determine full frame number */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700847 uint32_t Fn = rfn_to_fn(rip->rfn);
848 uint8_t ta = qta2ta(rip->qta);
Philippd935d882016-11-07 13:07:36 +0100849
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700850 send_gsmtap_rach(PCU_GSMTAP_C_UL_RACH, GSMTAP_CHANNEL_RACH, rip);
Pau Espin Pedrol1ec211f2019-09-30 18:33:14 +0200851
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700852 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests Uplink resource on CCCH/RACH: "
853 "ra=0x%02x (%d bit) Fn=%u qta=%d\n", rip->ra,
854 rip->is_11bit ? 11 : 8, Fn, rip->qta);
bhargava628dcfb2016-09-14 20:30:31 +0530855
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700856 /* Parse [EGPRS Packet] Channel Request from RACH.ind */
857 rc = parse_rach_ind(rip, &chan_req);
858 if (rc) /* Send RR Immediate Assignment Reject */
859 goto send_imm_ass_rej;
bhargava628dcfb2016-09-14 20:30:31 +0530860
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700861 if (chan_req.single_block)
862 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
Pau Espin Pedrol03de8982021-01-14 12:48:50 +0100863 else if (pcu->vty.force_two_phase) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700864 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation, "
865 "but we force two phase access\n");
866 chan_req.single_block = true;
867 }
868
Vadim Yanitskiyb90d34b2020-05-21 20:20:34 +0700869 /* TODO: handle Radio Priority (see 3GPP TS 44.060, table 11.2.5a.5) */
870 if (chan_req.priority > 0)
871 LOGP(DRLCMAC, LOGL_NOTICE, "EGPRS Packet Channel Request indicates "
872 "Radio Priority %u, however we ignore it\n", chan_req.priority);
873
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700874 /* Should we allocate a single block or an Uplink TBF? */
875 if (chan_req.single_block) {
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100876 rc = sba()->alloc(&trx_no, &ts_no, &sb_fn, ta);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530877 if (rc < 0) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530878 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700879 "single block allocation: rc=%d\n", rc);
880 /* Send RR Immediate Assignment Reject */
881 goto send_imm_ass_rej;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530882 }
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700883
884 tsc = m_bts.trx[trx_no].pdch[ts_no].tsc;
885 LOGP(DRLCMAC, LOGL_DEBUG, "Allocated a single block at "
886 "SBFn=%u TRX=%u TS=%u\n", sb_fn, trx_no, ts_no);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200887 } else {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700888 GprsMs *ms = ms_alloc(0, chan_req.egprs_mslot_class);
Pau Espin Pedrol17402a52020-05-08 17:44:33 +0200889 tbf = tbf_alloc_ul_tbf(&m_bts, ms, -1, true);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200890 if (!tbf) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700891 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for Uplink TBF\n");
892 /* Send RR Immediate Assignment Reject */
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530893 rc = -EBUSY;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700894 goto send_imm_ass_rej;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200895 }
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700896
897 /* FIXME: Copy and paste with other routines.. */
898 tbf->set_ta(ta);
899 TBF_SET_STATE(tbf, GPRS_RLCMAC_FLOW);
900 TBF_ASS_TYPE_SET(tbf, GPRS_RLCMAC_FLAG_CCCH);
901 T_START(tbf, T3169, 3169, "RACH (new UL-TBF)", true);
902 trx_no = tbf->trx->trx_no;
903 ts_no = tbf->first_ts;
904 usf = tbf->m_usf[ts_no];
905 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200906 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100907
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700908send_imm_ass_rej:
909 /* Allocate a bit-vector for RR Immediate Assignment [Reject] */
910 struct bitvec *bv = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Vadim Yanitskiyeb1e0fa2020-07-18 20:33:08 +0700911 bitvec_unhex(bv, DUMMY_VEC); /* standard '2B'O padding */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100912
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700913 if (rc != 0) {
914 LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment Reject on AGCH\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530915 plen = Encoding::write_immediate_assignment_reject(
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700916 bv, rip->ra, Fn, rip->burst_type);
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200917 do_rate_ctr_inc(CTR_IMMEDIATE_ASSIGN_REJ);
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700918 } else {
919 LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment on AGCH: "
920 "TRX=%u (ARFCN %u) TS=%u TA=%u TSC=%u TFI=%d USF=%d\n",
921 trx_no, m_bts.trx[trx_no].arfcn & ~ARFCN_FLAG_MASK,
922 ts_no, ta, tsc, tbf ? tbf->tfi() : -1, usf);
923 plen = Encoding::write_immediate_assignment(
Vadim Yanitskiy11567762020-07-18 23:34:17 +0700924 &m_bts.trx[trx_no].pdch[ts_no], tbf, bv,
925 false, rip->ra, Fn, ta, usf, false, sb_fn,
Pau Espin Pedrola2814952021-01-14 12:53:53 +0100926 pcu->vty.alpha, pcu->vty.gamma, -1,
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700927 rip->burst_type);
Pau Espin Pedrol2338e532020-05-12 20:54:35 +0200928 do_rate_ctr_inc(CTR_IMMEDIATE_ASSIGN_UL_TBF);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530929 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100930
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700931 if (plen >= 0)
932 pcu_l1if_tx_agch(bv, plen);
933 else
934 rc = plen;
935
936 bitvec_free(bv);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200937
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530938 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200939}
940
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700941/* PTCCH/U sub-slot / frame-number mapping (see 3GPP TS 45.002, table 6) */
942static uint32_t ptcch_slot_map[PTCCH_TAI_NUM] = {
943 12, 38, 64, 90,
944 116, 142, 168, 194,
945 220, 246, 272, 298,
946 324, 350, 376, 402,
947};
948
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700949int BTS::rcv_ptcch_rach(const struct rach_ind_params *rip)
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700950{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700951 uint32_t fn416 = rfn_to_fn(rip->rfn) % 416;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700952 struct gprs_rlcmac_bts *bts = bts_data();
953 struct gprs_rlcmac_pdch *pdch;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700954 uint8_t ss;
955
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700956 send_gsmtap_rach(PCU_GSMTAP_C_UL_PTCCH, GSMTAP_CHANNEL_PTCCH, rip);
957
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700958 /* Prevent buffer overflow */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700959 if (rip->trx_nr >= ARRAY_SIZE(bts->trx) || rip->ts_nr >= 8) {
960 LOGP(DRLCMAC, LOGL_ERROR, "(TRX=%u TS=%u RFN=%u) Rx malformed "
961 "RACH.ind (PTCCH/U)\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700962 return -EINVAL;
963 }
964
965 /* Make sure PDCH time-slot is enabled */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700966 pdch = &bts->trx[rip->trx_nr].pdch[rip->ts_nr];
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700967 if (!pdch->m_is_enabled) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700968 LOGP(DRLCMAC, LOGL_NOTICE, "(TRX=%u TS=%u RFN=%u) Rx RACH.ind (PTCCH/U) "
969 "for inactive PDCH\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700970 return -EAGAIN;
971 }
972
973 /* Convert TDMA frame-number to PTCCH/U sub-slot number */
974 for (ss = 0; ss < PTCCH_TAI_NUM; ss++)
975 if (ptcch_slot_map[ss] == fn416)
976 break;
977 if (ss == PTCCH_TAI_NUM) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700978 LOGP(DRLCMAC, LOGL_ERROR, "(TRX=%u TS=%u RFN=%u) Failed to map "
979 "PTCCH/U sub-slot\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700980 return -ENODEV;
981 }
982
983 /* Apply the new Timing Advance value */
984 LOGP(DRLCMAC, LOGL_INFO, "Continuous Timing Advance update "
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700985 "for TAI %u, new TA is %u\n", ss, qta2ta(rip->qta));
986 pdch->update_ta(ss, qta2ta(rip->qta));
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700987
988 return 0;
989}
990
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100991void BTS::snd_dl_ass(gprs_rlcmac_tbf *tbf, bool poll, uint16_t pgroup)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200992{
Vadim Yanitskiy11567762020-07-18 23:34:17 +0700993 uint8_t trx_no = tbf->trx->trx_no;
994 uint8_t ts_no = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200995 int plen;
996
Maxc21f0072017-12-15 17:36:45 +0100997 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +0100998 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Vadim Yanitskiyeb1e0fa2020-07-18 20:33:08 +0700999 bitvec_unhex(immediate_assignment, DUMMY_VEC); /* standard '2B'O padding */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +02001000 /* use request reference that has maximum distance to current time,
1001 * so the assignment will not conflict with possible RACH requests. */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +01001002 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d pollFN=%d\n",
Vadim Yanitskiy11567762020-07-18 23:34:17 +07001003 trx_no, tbf->trx->arfcn, ts_no, tbf->ta(), poll ? tbf->poll_fn : -1);
1004 plen = Encoding::write_immediate_assignment(&m_bts.trx[trx_no].pdch[ts_no],
1005 tbf, immediate_assignment, true, 125,
1006 (tbf->pdch[ts_no]->last_rts_fn + 21216) % GSM_MAX_FN,
1007 tbf->ta(), 7, poll, tbf->poll_fn,
Pau Espin Pedrola2814952021-01-14 12:53:53 +01001008 pcu->vty.alpha, pcu->vty.gamma, -1,
Maxfc8afc22019-02-18 18:52:38 +01001009 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +05301010 if (plen >= 0) {
Pau Espin Pedrol2338e532020-05-12 20:54:35 +02001011 do_rate_ctr_inc(CTR_IMMEDIATE_ASSIGN_DL_TBF);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +01001012 pcu_l1if_tx_pch(immediate_assignment, plen, pgroup);
Mrinal Mishraf86307e2016-11-10 18:16:30 +05301013 }
1014
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +02001015 bitvec_free(immediate_assignment);
1016}
1017
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001018/* return maximum DL CS supported by BTS and allowed by VTY */
1019uint8_t BTS::max_cs_dl(void) const
1020{
1021 return m_max_cs_dl;
1022}
1023
1024/* return maximum UL CS supported by BTS and allowed by VTY */
1025uint8_t BTS::max_cs_ul(void) const
1026{
1027 return m_max_cs_ul;
1028}
1029
1030/* return maximum DL MCS supported by BTS and allowed by VTY */
1031uint8_t BTS::max_mcs_dl(void) const
1032{
1033 return m_max_mcs_dl;
1034}
1035
1036/* return maximum UL MCS supported by BTS and allowed by VTY */
1037uint8_t BTS::max_mcs_ul(void) const
1038{
1039 return m_max_mcs_ul;
1040}
1041
1042/* Set maximum DL CS supported by BTS and allowed by VTY */
1043void BTS::set_max_cs_dl(uint8_t cs_dl)
1044{
1045 m_max_cs_dl = cs_dl;
1046}
1047
1048/* Set maximum UL CS supported by BTS and allowed by VTY */
1049void BTS::set_max_cs_ul(uint8_t cs_ul)
1050{
1051 m_max_cs_ul = cs_ul;
1052}
1053
1054/* Set maximum DL MCS supported by BTS and allowed by VTY */
1055void BTS::set_max_mcs_dl(uint8_t mcs_dl)
1056{
1057 m_max_mcs_dl = mcs_dl;
1058}
1059
1060/* Set maximum UL MCS supported by BTS and allowed by VTY */
1061void BTS::set_max_mcs_ul(uint8_t mcs_ul)
1062{
1063 m_max_mcs_ul = mcs_ul;
1064}
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +02001065
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +01001066bool BTS::cs_dl_is_supported(CodingScheme cs)
1067{
1068 OSMO_ASSERT(mcs_is_valid(cs));
1069 uint8_t num = mcs_chan_code(cs);
1070 if (mcs_is_gprs(cs)) {
1071 return (max_cs_dl() >= num) && (m_bts.cs_mask & (1U << num));
1072 } else {
1073 return (max_mcs_dl() >= num) && (m_bts.mcs_mask & (1U << num));
1074 }
1075}
1076
Jacob Erlbeck86b6f052015-11-27 15:17:34 +01001077GprsMs *BTS::ms_alloc(uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001078{
1079 GprsMs *ms;
1080 ms = ms_store().create_ms();
1081
Pau Espin Pedrol924aaad2021-01-14 12:01:42 +01001082 ms_set_timeout(ms, osmo_tdef_get(pcu->T_defs, -2030, OSMO_TDEF_S, -1));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001083 ms_set_ms_class(ms, ms_class);
1084 ms_set_egprs_ms_class(ms, egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001085
1086 return ms;
1087}
1088
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001089
1090static int bts_talloc_destructor(struct BTS* bts)
1091{
1092 bts->~BTS();
1093 return 0;
1094}
1095
1096struct BTS* bts_alloc(struct gprs_pcu *pcu)
1097{
1098 struct BTS* bts;
1099 bts = talloc(pcu, struct BTS);
1100 if (!bts)
1101 return bts;
1102 talloc_set_destructor(bts, bts_talloc_destructor);
1103 new (bts) BTS(pcu);
1104 return bts;
1105}
1106
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001107/* update TA based on TA provided by PH-DATA-IND */
1108void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
1109{
1110 int16_t ta_adj;
1111 uint8_t ta_target;
1112
1113 if (ta_delta) {
1114 /* adjust TA based on TA provided by PH-DATA-IND */
1115 ta_adj = tbf->ta() + ta_delta;
1116
1117 /* limit target TA in range 0..63 bits */
1118 ta_target = ta_limit(ta_adj);
1119
1120 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating TLLI=0x%08x: TA %u -> %u on "
1121 "TRX = %d, TS = %d, FN = %d\n",
1122 tbf->tlli(), tbf->ta(), ta_target,
1123 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
1124 tbf->set_ta(ta_target);
1125 }
1126}
1127
1128/* set TA based on TA provided by PH-RA-IND */
1129void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
1130{
1131 uint8_t ta_target;
1132
1133 if (tbf->ta() != ta) {
1134 /* limit target TA in range 0..63 bits */
1135 ta_target = ta_limit(ta);
1136
1137 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating TLLI=0x%08x: TA %u -> %u on "
1138 "TRX = %d, TS = %d, FN = %d\n",
1139 tbf->tlli(), tbf->ta(), ta_target,
1140 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
1141 tbf->set_ta(ta_target);
1142 }
1143}
1144
1145void 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 +02001146{
1147 struct gprs_rlcmac_ul_tbf *tbf =
1148 bts_main_data()->bts->ul_tbf_by_poll_fn(fn, trx_no, ts);
1149 if (!tbf)
1150 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
1151 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
1152 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001153 else {
1154 /* we need to distinguish TA information provided by L1
1155 * from PH-DATA-IND and PHY-RA-IND so that we can properly
1156 * update TA for given TBF
1157 */
1158 if (is_rach)
1159 set_tbf_ta(tbf, (uint8_t)ta);
1160 else
1161 update_tbf_ta(tbf, ta);
1162
Maxd71e8b32016-09-19 16:17:06 +02001163 }
1164}
1165
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001166void bts_trx_reserve_slots(struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001167 uint8_t slots)
1168{
1169 unsigned i;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001170 for (i = 0; i < ARRAY_SIZE(trx->pdch); i += 1)
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001171 if (slots & (1 << i))
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001172 trx->pdch[i].reserve(dir);
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001173}
1174
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001175void bts_trx_unreserve_slots(struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001176 uint8_t slots)
1177{
1178 unsigned i;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001179 for (i = 0; i < ARRAY_SIZE(trx->pdch); i += 1)
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001180 if (slots & (1 << i))
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001181 trx->pdch[i].unreserve(dir);
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001182}
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001183
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001184void bts_recalc_max_cs(struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001185{
1186 int i;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001187 uint8_t cs_dl, cs_ul;
1188 struct gprs_pcu *pcu = bts->bts->pcu;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001189
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001190 cs_dl = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001191 for (i = pcu->vty.max_cs_dl - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001192 if (bts->cs_mask & (1 << i)) {
1193 cs_dl = i + 1;
1194 break;
1195 }
1196 }
1197
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001198 cs_ul = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001199 for (i = pcu->vty.max_cs_ul - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001200 if (bts->cs_mask & (1 << i)) {
1201 cs_ul = i + 1;
1202 break;
1203 }
1204 }
1205
1206 LOGP(DRLCMAC, LOGL_DEBUG, "New max CS: DL=%u UL=%u\n", cs_dl, cs_ul);
1207 bts->bts->set_max_cs_dl(cs_dl);
1208 bts->bts->set_max_cs_ul(cs_ul);
1209}
1210
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001211void bts_recalc_max_mcs(struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001212{
1213 int i;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001214 uint8_t mcs_dl, mcs_ul;
1215 struct gprs_pcu *pcu = bts->bts->pcu;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001216
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001217 mcs_dl = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001218 for (i = pcu->vty.max_mcs_dl - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001219 if (bts->mcs_mask & (1 << i)) {
1220 mcs_dl = i + 1;
1221 break;
1222 }
1223 }
1224
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001225 mcs_ul = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001226 for (i = pcu->vty.max_mcs_ul - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001227 if (bts->mcs_mask & (1 << i)) {
1228 mcs_ul = i + 1;
1229 break;
1230 }
1231 }
1232
1233 LOGP(DRLCMAC, LOGL_DEBUG, "New max MCS: DL=%u UL=%u\n", mcs_dl, mcs_ul);
1234 bts->bts->set_max_mcs_dl(mcs_dl);
1235 bts->bts->set_max_mcs_ul(mcs_ul);
1236}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001237
1238
1239struct gprs_rlcmac_bts *bts_data(struct BTS *bts)
1240{
1241 return &bts->m_bts;
1242}
1243
1244struct GprsMs *bts_ms_by_imsi(struct BTS *bts, const char *imsi)
1245{
1246 return bts->ms_by_imsi(imsi);
1247}
1248
1249uint8_t bts_max_cs_dl(const struct BTS *bts)
1250{
1251 return bts->max_cs_dl();
1252}
1253
1254uint8_t bts_max_cs_ul(const struct BTS *bts)
1255{
1256 return bts->max_cs_ul();
1257}
1258
1259uint8_t bts_max_mcs_dl(const struct BTS *bts)
1260{
1261 return bts->max_mcs_dl();
1262}
1263
1264uint8_t bts_max_mcs_ul(const struct BTS *bts)
1265{
1266 return bts->max_mcs_ul();
1267}