blob: 1b2ab2fbdc078e324a658bd73207f1c746b449fd [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>
Holger Hans Peter Freyther63f29d62013-10-19 19:04:03 +020024#include <encoding.h>
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +020025#include <decoding.h>
26#include <rlc.h>
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +020027#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010028#include <gprs_ms.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020029#include <gprs_rlcmac.h>
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020030#include <gprs_debug.h>
Max1187a772018-01-26 13:31:42 +010031#include <cxx_linuxlist.h>
Max6dc90b82018-02-19 17:17:28 +010032#include <pdch.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020033
34extern "C" {
35 #include <osmocom/core/talloc.h>
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020036 #include <osmocom/core/msgb.h>
Holger Hans Peter Freyther8df447d2015-11-07 21:04:40 +010037 #include <osmocom/core/stats.h>
Max9bbe1602016-07-18 12:50:18 +020038 #include <osmocom/gsm/protocol/gsm_04_08.h>
Philipp Maier1275a3f2017-02-21 19:35:23 +010039 #include <osmocom/gsm/gsm_utils.h>
Harald Welte717cdf52017-07-21 21:56:23 +020040 #include <osmocom/core/gsmtap_util.h>
Harald Welte3e51d3e2017-10-29 10:57:27 +010041 #include <osmocom/core/application.h>
Max1187a772018-01-26 13:31:42 +010042 #include <osmocom/core/bitvec.h>
43 #include <osmocom/core/gsmtap.h>
44 #include <osmocom/core/logging.h>
45 #include <osmocom/core/utils.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020046}
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +020047
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +020048#include <errno.h>
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +020049#include <string.h>
50
Philipp Maier1275a3f2017-02-21 19:35:23 +010051#define RFN_MODULUS 42432
52#define RFN_THRESHOLD RFN_MODULUS / 2
53
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020054extern void *tall_pcu_ctx;
55
Harald Welte3e51d3e2017-10-29 10:57:27 +010056extern "C" {
57 /* e must make sure to initialize logging before the BTS static
58 * constructors are executed below, as those call libosmocore APIs that
59 * require logging already to be initialized. */
60 __attribute__((constructor)) static void early_init(void)
61 {
Neels Hofmeyr42f2d612018-04-01 16:54:40 +020062 if (!tall_pcu_ctx) {
63 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
64 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
65 }
Harald Welte3e51d3e2017-10-29 10:57:27 +010066 }
67}
68
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +020069static BTS s_bts;
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[] = {
80 { .T=-2000, .default_val=2, .unit=OSMO_TDEF_MS, .desc="Tbf reject for PRR timer (ms)", .val=0 },
81 { .T=-2001, .default_val=2, .unit=OSMO_TDEF_S, .desc="PACCH assignment timer (s)", .val=0 },
82 { .T=-2002, .default_val=200, .unit=OSMO_TDEF_MS, .desc="Waiting after IMM.ASS confirm timer (ms)", .val=0 },
Pau Espin Pedrol5360ef52019-09-13 12:41:54 +020083 { .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 +020084};
85
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +010086/**
87 * For gcc-4.4 compat do not use extended initializer list but keep the
88 * order from the enum here. Once we support GCC4.7 and up we can change
89 * the code below.
90 */
91static const struct rate_ctr_desc bts_ctr_description[] = {
Maxa5eb67d2017-11-21 20:24:54 +010092 { "tbf:dl:alloc", "TBF DL Allocated "},
93 { "tbf:dl:freed", "TBF DL Freed "},
94 { "tbf:dl:aborted", "TBF DL Aborted "},
95 { "tbf:ul:alloc", "TBF UL Allocated "},
96 { "tbf:ul:freed", "TBF UL Freed "},
97 { "tbf:ul:aborted", "TBF UL Aborted "},
98 { "tbf:reused", "TBF Reused "},
99 { "tbf:alloc:algo-a", "TBF Alloc Algo A "},
100 { "tbf:alloc:algo-b", "TBF Alloc Algo B "},
101 { "tbf:failed:egprs-only", "TBF Failed EGPRS-only"},
102 { "rlc:sent", "RLC Sent "},
103 { "rlc:resent", "RLC Resent "},
104 { "rlc:restarted", "RLC Restarted "},
105 { "rlc:stalled", "RLC Stalled "},
106 { "rlc:nacked", "RLC Nacked "},
107 { "rlc:final_block_resent", "RLC Final Blk resent "},
108 { "rlc:ass:timedout", "RLC Assign Timeout "},
109 { "rlc:ass:failed", "RLC Assign Failed "},
110 { "rlc:ack:timedout", "RLC Ack Timeout "},
111 { "rlc:ack:failed", "RLC Ack Failed "},
112 { "rlc:rel:timedout", "RLC Release Timeout "},
113 { "rlc:late-block", "RLC Late Block "},
114 { "rlc:sent-dummy", "RLC Sent Dummy "},
115 { "rlc:sent-control", "RLC Sent Control "},
116 { "rlc:dl_bytes", "RLC DL Bytes "},
117 { "rlc:dl_payload_bytes", "RLC DL Payload Bytes "},
118 { "rlc:ul_bytes", "RLC UL Bytes "},
119 { "rlc:ul_payload_bytes", "RLC UL Payload Bytes "},
120 { "decode:errors", "Decode Errors "},
121 { "sba:allocated", "SBA Allocated "},
122 { "sba:freed", "SBA Freed "},
123 { "sba:timedout", "SBA Timeout "},
124 { "llc:timeout", "Timedout Frames "},
125 { "llc:dropped", "Dropped Frames "},
126 { "llc:scheduled", "Scheduled Frames "},
127 { "llc:dl_bytes", "RLC encapsulated PDUs"},
128 { "llc:ul_bytes", "full PDUs received "},
129 { "rach:requests", "RACH requests "},
130 { "11bit_rach:requests", "11BIT_RACH requests "},
131 { "spb:uplink_first_segment", "First seg of UL SPB "},
132 { "spb:uplink_second_segment", "Second seg of UL SPB "},
133 { "spb:downlink_first_segment", "First seg of DL SPB "},
134 { "spb:downlink_second_segment","Second seg of DL SPB "},
135 { "immediate:assignment_UL", "Immediate Assign UL "},
136 { "immediate:assignment_rej", "Immediate Assign Rej "},
137 { "immediate:assignment_DL", "Immediate Assign DL "},
138 { "channel:request_description","Channel Request Desc "},
139 { "pkt:ul_assignment", "Packet UL Assignment "},
140 { "pkt:access_reject", "Packet Access Reject "},
141 { "pkt:dl_assignment", "Packet DL Assignment "},
142 { "ul:control", "UL control Block "},
143 { "ul:assignment_poll_timeout", "UL Assign Timeout "},
144 { "ul:assignment_failed", "UL Assign Failed "},
145 { "dl:assignment_timeout", "DL Assign Timeout "},
146 { "dl:assignment_failed", "DL Assign Failed "},
147 { "pkt:ul_ack_nack_timeout", "PUAN Poll Timeout "},
148 { "pkt:ul_ack_nack_failed", "PUAN poll Failed "},
149 { "pkt:dl_ack_nack_timeout", "PDAN poll Timeout "},
150 { "pkt:dl_ack_nack_failed", "PDAN poll Failed "},
151 { "gprs:downlink_cs1", "CS1 downlink "},
152 { "gprs:downlink_cs2", "CS2 downlink "},
153 { "gprs:downlink_cs3", "CS3 downlink "},
154 { "gprs:downlink_cs4", "CS4 downlink "},
155 { "egprs:downlink_mcs1", "MCS1 downlink "},
156 { "egprs:downlink_mcs2", "MCS2 downlink "},
157 { "egprs:downlink_mcs3", "MCS3 downlink "},
158 { "egprs:downlink_mcs4", "MCS4 downlink "},
159 { "egprs:downlink_mcs5", "MCS5 downlink "},
160 { "egprs:downlink_mcs6", "MCS6 downlink "},
161 { "egprs:downlink_mcs7", "MCS7 downlink "},
162 { "egprs:downlink_mcs8", "MCS8 downlink "},
163 { "egprs:downlink_mcs9", "MCS9 downlink "},
164 { "gprs:uplink_cs1", "CS1 Uplink "},
165 { "gprs:uplink_cs2", "CS2 Uplink "},
166 { "gprs:uplink_cs3", "CS3 Uplink "},
167 { "gprs:uplink_cs4", "CS4 Uplink "},
168 { "egprs:uplink_mcs1", "MCS1 Uplink "},
169 { "egprs:uplink_mcs2", "MCS2 Uplink "},
170 { "egprs:uplink_mcs3", "MCS3 Uplink "},
171 { "egprs:uplink_mcs4", "MCS4 Uplink "},
172 { "egprs:uplink_mcs5", "MCS5 Uplink "},
173 { "egprs:uplink_mcs6", "MCS6 Uplink "},
174 { "egprs:uplink_mcs7", "MCS7 Uplink "},
175 { "egprs:uplink_mcs8", "MCS8 Uplink "},
176 { "egprs:uplink_mcs9", "MCS9 Uplink "},
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100177};
178
179static const struct rate_ctr_group_desc bts_ctrg_desc = {
180 "bts",
181 "BTS Statistics",
Holger Hans Peter Freytherb8a54262015-11-07 21:00:00 +0100182 OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100183 ARRAY_SIZE(bts_ctr_description),
184 bts_ctr_description,
185};
186
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100187static const struct osmo_stat_item_desc bts_stat_item_description[] = {
188 { "ms.present", "MS Present ",
189 OSMO_STAT_ITEM_NO_UNIT, 4, 0},
190};
191
192static const struct osmo_stat_item_group_desc bts_statg_desc = {
193 "bts",
194 "BTS Statistics",
195 OSMO_STATS_CLASS_GLOBAL,
196 ARRAY_SIZE(bts_stat_item_description),
197 bts_stat_item_description,
198};
199
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200200BTS* BTS::main_bts()
201{
202 return &s_bts;
203}
204
205struct gprs_rlcmac_bts *BTS::bts_data()
206{
207 return &m_bts;
208}
209
210struct gprs_rlcmac_bts *bts_main_data()
211{
212 return BTS::main_bts()->bts_data();
213}
214
Maxd5ffeb52019-03-18 15:48:38 +0100215void bts_cleanup()
216{
217 return BTS::main_bts()->cleanup();
218}
219
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100220struct rate_ctr_group *bts_main_data_stats()
221{
222 return BTS::main_bts()->rate_counters();
223}
224
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200225BTS::BTS()
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200226 : m_cur_fn(0)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200227 , m_cur_blk_fn(-1)
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +0200228 , m_pollController(*this)
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200229 , m_sba(*this)
Jacob Erlbeck17214bb2015-06-02 14:06:12 +0200230 , m_ms_store(this)
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200231{
232 memset(&m_bts, 0, sizeof(m_bts));
233 m_bts.bts = this;
Oliver Smithcfb63212019-09-05 17:13:33 +0200234 m_bts.app_info = NULL;
Oliver Smith45fdc442019-09-05 15:12:20 +0200235 m_bts.dl_tbf_preemptive_retransmission = true;
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200236 m_bts.T_defs_bts = T_defs_bts;
237 m_bts.T_defs_pcu = T_defs_pcu;
238 osmo_tdefs_reset(m_bts.T_defs_bts);
239 osmo_tdefs_reset(m_bts.T_defs_pcu);
Holger Hans Peter Freyther4ed1dae2013-10-20 10:14:03 +0200240
241 /* initialize back pointers */
242 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(m_bts.trx); ++trx_no) {
243 struct gprs_rlcmac_trx *trx = &m_bts.trx[trx_no];
244 trx->trx_no = trx_no;
245 trx->bts = this;
246
247 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
248 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
249 pdch->ts_no = ts_no;
250 pdch->trx = trx;
251 }
252 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100253
Maxea9968f2017-12-18 22:52:52 +0100254 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100255 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100256 than just allocate the group with different index.
257 This shall be removed once weget rid of BTS singleton */
258 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
259 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
260 else
261 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200262 OSMO_ASSERT(m_ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100263
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100264 m_statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200265 OSMO_ASSERT(m_statg);
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200266}
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200267
Maxd5ffeb52019-03-18 15:48:38 +0100268void BTS::cleanup()
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100269{
Jacob Erlbeckc362df22016-01-20 22:02:19 +0100270 /* this can cause counter updates and must not be left to the
271 * m_ms_store's destructor */
272 m_ms_store.cleanup();
273
Maxd5ffeb52019-03-18 15:48:38 +0100274 if (m_ratectrs) {
275 rate_ctr_group_free(m_ratectrs);
276 m_ratectrs = NULL;
277 }
278
279 if (m_statg) {
280 osmo_stat_item_group_free(m_statg);
281 m_statg = NULL;
282 }
Oliver Smithcfb63212019-09-05 17:13:33 +0200283
284 if (m_bts.app_info) {
285 msgb_free(m_bts.app_info);
286 m_bts.app_info = NULL;
287 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100288}
289
Maxd5ffeb52019-03-18 15:48:38 +0100290BTS::~BTS()
291{
292 cleanup();
293}
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100294
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200295void BTS::set_current_frame_number(int fn)
296{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200297 /* The UL frame numbers lag 3 behind the DL frames and the data
298 * indication is only sent after all 4 frames of the block have been
299 * received. Sometimes there is an idle frame between the end of one
300 * and start of another frame (every 3 blocks). So the timeout should
301 * definitely be there if we're more than 8 frames past poll_fn. Let's
Jacob Erlbecke77d49f2015-08-27 13:28:05 +0200302 * stay on the safe side and say 13 or more. An additional delay can
303 * happen due to the block processing time in the DSP, so the delay of
304 * decoded blocks relative to the timing clock can be much larger.
305 * Values up to 50 frames have been observed under load. */
306 const static int max_delay = 60;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200307
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200308 m_cur_fn = fn;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200309 m_pollController.expireTimedout(m_cur_fn, max_delay);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200310}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200311
Max9dabfa22017-05-16 16:10:45 +0200312static inline int delta_fn(int fn, int to)
313{
314 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
315}
316
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200317void BTS::set_current_block_frame_number(int fn, unsigned max_delay)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200318{
319 int delay = 0;
320 const int late_block_delay_thresh = 13;
321 const int fn_update_ok_min_delay = -500;
322 const int fn_update_ok_max_delay = 0;
323
324 /* frame numbers in the received blocks are assumed to be strongly
325 * monotonic. */
326 if (m_cur_blk_fn >= 0) {
Max9dabfa22017-05-16 16:10:45 +0200327 int delta = delta_fn(fn, m_cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200328 if (delta <= 0)
329 return;
330 }
331
332 /* Check block delay vs. the current frame number */
333 if (current_frame_number() != 0)
Max9dabfa22017-05-16 16:10:45 +0200334 delay = delta_fn(fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200335 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200336 LOGP(DRLCMAC, LOGL_NOTICE,
337 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
338 delay, fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200339 rlc_late_block();
340 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200341
342 m_cur_blk_fn = fn;
343 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
344 current_frame_number() == 0)
345 m_cur_fn = fn;
346
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200347 m_pollController.expireTimedout(fn, max_delay);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200348}
349
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200350int BTS::add_paging(uint8_t chan_needed, uint8_t *identity_lv)
351{
352 uint8_t l, trx, ts, any_tbf = 0;
353 struct gprs_rlcmac_tbf *tbf;
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100354 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200355 uint8_t slot_mask[8];
356 int8_t first_ts; /* must be signed */
357
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100358 LListHead<gprs_rlcmac_tbf> *tbfs_lists[] = {
359 &m_ul_tbfs,
360 &m_dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200361 NULL
362 };
363
364
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200365 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n",
366 chan_needed, osmo_hexdump(identity_lv + 1, identity_lv[0]));
367
368 /* collect slots to page
369 * Mark slots for every TBF, but only mark one of it.
370 * Mark only the first slot found.
371 * Don't mark, if TBF uses a different slot that is already marked. */
372 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200373 for (l = 0; tbfs_lists[l]; l++) {
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100374 llist_for_each(pos, tbfs_lists[l]) {
375 tbf = pos->entry();
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200376 first_ts = -1;
377 for (ts = 0; ts < 8; ts++) {
378 if (tbf->pdch[ts]) {
379 /* remember the first slot found */
380 if (first_ts < 0)
381 first_ts = ts;
382 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100383 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200384 break;
385 }
386 }
387 /* mark first slot found, if none is marked already */
388 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100389 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200390 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100391 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100392 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200393 } else
Maxc21f0072017-12-15 17:36:45 +0100394 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200395 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100396 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200397 }
398 }
399
400 /* Now we have a list of marked slots. Every TBF uses at least one
401 * of these slots. */
402
403 /* schedule paging to all marked slots */
404 for (trx = 0; trx < 8; trx++) {
405 if (slot_mask[trx] == 0)
406 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200407 for (ts = 0; ts < 8; ts++) {
408 if ((slot_mask[trx] & (1 << ts))) {
409 /* schedule */
Max4382e4e2018-01-30 12:00:08 +0100410 if (!m_bts.trx[trx].pdch[ts].add_paging(chan_needed, identity_lv))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200411 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100412
413 LOGP(DRLCMAC, LOGL_INFO, "Paging on PACCH of TRX=%d TS=%d\n", trx, ts);
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200414 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200415 }
416 }
417 }
418
419 if (!any_tbf)
420 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
421
422 return 0;
423}
424
Harald Welte717cdf52017-07-21 21:56:23 +0200425void BTS::send_gsmtap(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
426 uint8_t ts_no, uint8_t channel, uint32_t fn,
427 const uint8_t *data, unsigned int len)
428{
429 uint16_t arfcn;
430
431 /* check if category is activated at all */
432 if (!(m_bts.gsmtap_categ_mask & (1 << categ)))
433 return;
434
435 arfcn = m_bts.trx[trx_no].arfcn;
436 if (uplink)
437 arfcn |= GSMTAP_ARFCN_F_UPLINK;
438
439 gsmtap_send(m_bts.gsmtap, arfcn, ts_no, channel, 0, fn, 0, 0, data, len);
440}
441
Max341dccd2017-08-14 15:49:59 +0200442static inline bool tbf_check(gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t trx_no, uint8_t ts)
443{
Maxcac6b662018-01-24 11:00:17 +0100444 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING) && tbf->poll_scheduled()
Max341dccd2017-08-14 15:49:59 +0200445 && tbf->poll_fn == fn && tbf->trx->trx_no == trx_no && tbf->poll_ts == ts)
446 return true;
447
448 return false;
449}
450
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200451gprs_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 +0200452{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100453 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200454
455 /* only one TBF can poll on specific TS/FN, because scheduler can only
456 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100457 llist_for_each(pos, &m_dl_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200458 if (tbf_check(pos->entry(), fn, trx, ts))
459 return as_dl_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200460 }
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200461 return NULL;
462}
Max341dccd2017-08-14 15:49:59 +0200463
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200464gprs_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 +0200465{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100466 LListHead<gprs_rlcmac_tbf> *pos;
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200467
468 /* only one TBF can poll on specific TS/FN, because scheduler can only
469 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100470 llist_for_each(pos, &m_ul_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200471 if (tbf_check(pos->entry(), fn, trx, ts))
472 return as_ul_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200473 }
474 return NULL;
475}
476
Daniel Willmann54044b02014-07-02 17:58:15 +0200477/* lookup downlink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200478gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200479{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200480 if (trx >= 8 || ts >= 8)
481 return NULL;
482
483 return m_bts.trx[trx].pdch[ts].dl_tbf_by_tfi(tfi);
Daniel Willmann54044b02014-07-02 17:58:15 +0200484}
485
486/* lookup uplink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200487gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200488{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200489 if (trx >= 8 || ts >= 8)
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200490 return NULL;
491
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200492 return m_bts.trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200493}
494
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200495/*
496 * Search for free TFI and return TFI, TRX.
497 * This method returns the first TFI that is currently not used in any PDCH of
498 * a TRX. The first TRX that contains such an TFI is returned. Negative values
499 * indicate errors.
500 */
Maxa76a7d02018-01-26 11:09:16 +0100501int 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 +0200502{
Maxa76a7d02018-01-26 11:09:16 +0100503 const struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200504 uint32_t free_tfis;
505 bool has_pdch = false;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200506 uint8_t trx_from, trx_to, trx, ts, tfi;
507
508 if (use_trx >= 0 && use_trx < 8)
509 trx_from = trx_to = use_trx;
510 else {
511 trx_from = 0;
512 trx_to = 7;
513 }
514
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200515 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200516 for (trx = trx_from; trx <= trx_to; trx++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200517 bool trx_has_pdch = false;
518
Maxd000d802017-09-20 17:55:28 +0200519 free_tfis = NO_FREE_TFI;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200520
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200521 for (ts = 0; ts < 8; ts++) {
522 pdch = &m_bts.trx[trx].pdch[ts];
523 if (!pdch->is_enabled())
524 continue;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200525 free_tfis &= ~pdch->assigned_tfi(dir);
526 trx_has_pdch = true;
527 has_pdch = true;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200528 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200529 if (trx_has_pdch && free_tfis)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200530 break;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200531
532 free_tfis = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200533 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200534 if (!has_pdch) {
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200535 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
536 return -EINVAL;
537 }
538
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200539 if (!free_tfis) {
Max69d585e2018-01-26 12:36:43 +0100540 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200541 return -EBUSY;
542 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200543
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200544
545 LOGP(DRLCMAC, LOGL_DEBUG,
546 "Searching for first unallocated TFI: TRX=%d\n", trx);
547
548 /* find the first */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200549 for (tfi = 0; tfi < 32; tfi++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200550 if (free_tfis & 1 << tfi)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200551 break;
552 }
553
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200554 OSMO_ASSERT(tfi < 32);
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200555
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200556 LOGP(DRLCMAC, LOGL_DEBUG, " Found TFI=%d.\n", tfi);
557 *_trx = trx;
558 return tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200559}
560
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200561int BTS::rcv_imm_ass_cnf(const uint8_t *data, uint32_t fn)
562{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200563 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200564 uint8_t plen;
565 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200566 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200567
568 /* move to IA Rest Octets */
569 plen = data[0] >> 2;
570 data += 1 + plen;
571
572 if ((*data & 0xf0) != 0xd0) {
573 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
574 "octets do not start with bit sequence 'HH01' "
575 "(Packet Downlink Assignment)\n");
576 return -EINVAL;
577 }
578
579 /* get TLLI from downlink assignment */
Eric Wildab8b01e2019-07-23 17:16:06 +0200580 tlli = (uint32_t)((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200581 tlli |= (*data++) << 20;
582 tlli |= (*data++) << 12;
583 tlli |= (*data++) << 4;
584 tlli |= (*data++) >> 4;
585
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200586 ms = ms_by_tlli(tlli);
587 if (ms)
588 dl_tbf = ms->dl_tbf();
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200589 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200590 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
591 "does not exit\n", tlli);
592 return -EINVAL;
593 }
594
595 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
596
Daniel Willmann7e994e32014-08-07 15:49:21 +0200597 if (dl_tbf->m_wait_confirm)
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200598 T_START(dl_tbf, T0, -2002, "assignment (AGCH)", true);
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200599
600 return 0;
601}
602
Philipp Maier1275a3f2017-02-21 19:35:23 +0100603/* Determine the full frame number from a relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100604uint32_t BTS::rfn_to_fn(int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100605{
Max5dd8d1b2017-03-08 12:06:42 +0100606 int32_t m_cur_rfn;
607 int32_t fn;
608 int32_t fn_rounded;
609
610 /* double-check that relative FN is not negative and fits into int32_t */
611 OSMO_ASSERT(rfn < GSM_MAX_FN);
612 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100613
614 /* Note: If a BTS is sending in a rach request it will be fully aware
615 * of the frame number. If the PCU is used in a BSC-co-located setup.
616 * The BSC will forward the incoming RACH request. The RACH request
617 * only contains the relative frame number (Fn % 42432) in its request
618 * reference. This PCU implementation has to fit both scenarios, so
619 * we need to assume that Fn is a relative frame number. */
620
621 /* Ensure that all following calculations are performed with the
622 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100623 if (rfn >= RFN_MODULUS)
624 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100625
626 /* Compute an internal relative frame number from the full internal
627 frame number */
628 m_cur_rfn = m_cur_fn % RFN_MODULUS;
629
630 /* Compute a "rounded" version of the internal frame number, which
631 * exactly fits in the RFN_MODULUS raster */
632 fn_rounded = m_cur_fn - m_cur_rfn;
633
634 /* If the delta between the internal and the external relative frame
635 * number exceeds a certain limit, we need to assume that the incoming
636 * rach request belongs to a the previous rfn period. To correct this,
637 * we roll back the rounded frame number by one RFN_MODULUS */
638 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
639 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200640 "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 +0100641 rfn, m_cur_fn, RFN_MODULUS);
642 if (fn_rounded < RFN_MODULUS) {
643 LOGP(DRLCMAC, LOGL_DEBUG,
644 "Cornercase detected: wrapping crosses %u border\n",
645 GSM_MAX_FN);
646 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
647 }
648 else
649 fn_rounded -= RFN_MODULUS;
650 }
651
652 /* The real frame number is the sum of the rounded frame number and the
653 * relative framenumber computed via RACH */
654 fn = fn_rounded + rfn;
655
656 return fn;
657}
658
Max58818582018-01-05 15:04:50 +0100659static inline uint16_t mslot_class_from_ra(uint16_t ra, bool is_11bit)
660{
661 if (is_11bit)
662 return (ra & 0x3e0) >> 5;
663
664 /* set multislot class to 0 for 8-bit RACH, since we don't know it yet */
665 return 0;
666}
667
668static inline uint16_t priority_from_ra(uint16_t ra, bool is_11bit)
669{
670 if (is_11bit)
671 return (ra & 0x18) >> 3;
672
673 return 0;
674}
675
676static inline bool is_single_block(bool force_two_phase, uint16_t ra, enum ph_burst_type burst_type, bool is_11bit)
677{
678 bool sb = false;
679
680 if ((ra & 0xf8) == 0x70)
681 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
682 else if (force_two_phase)
683 LOGP(DRLCMAC, LOGL_DEBUG,
684 "MS requests single phase access, but we force two phase access [RACH is %s bit]\n",
685 is_11bit ? "11" : "8");
686
Max137fd592018-01-05 15:53:08 +0100687 switch(burst_type) {
688 case GSM_L1_BURST_TYPE_ACCESS_0:
689 if (is_11bit) {
690 LOGP(DRLCMAC, LOGL_ERROR, "Error: GPRS 11 bit RACH not supported\n");
691 return false;
692 }
Max58818582018-01-05 15:04:50 +0100693
694 if ((ra & 0xf8) == 0x70)
Max137fd592018-01-05 15:53:08 +0100695 return true;
696
697 if (force_two_phase)
698 return true;
699 break;
700 case GSM_L1_BURST_TYPE_ACCESS_1: /* deliberate fall-through */
701 case GSM_L1_BURST_TYPE_ACCESS_2:
702 if (is_11bit) {
703 if (!(ra & (1 << 10))) {
704 if (force_two_phase)
705 return true;
706
707 return false;
708 }
709
710 return true;
Max58818582018-01-05 15:04:50 +0100711 }
Max137fd592018-01-05 15:53:08 +0100712 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for 8-bit RACH\n", burst_type);
713 break;
714 case GSM_L1_BURST_TYPE_NONE:
715 LOGP(DRLCMAC, LOGL_ERROR, "PCU has not received burst type from BTS\n");
716 break;
717 default:
718 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for %s-bit RACH\n",
719 burst_type, is_11bit ? "11" : "8");
Max58818582018-01-05 15:04:50 +0100720 }
721
722 return sb;
723}
724
725int BTS::rcv_rach(uint16_t ra, uint32_t Fn, int16_t qta, bool is_11bit,
bhargava959d1de2016-08-17 15:17:21 +0530726 enum ph_burst_type burst_type)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200727{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200728 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Holger Hans Peter Freytherf37e5142013-12-25 18:56:34 +0100729 uint8_t trx_no, ts_no = 0;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200730 uint8_t sb = 0;
731 uint32_t sb_fn = 0;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530732 int rc = 0;
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100733 int plen;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100734 uint8_t usf = 7;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530735 uint8_t tsc = 0, ta = qta2ta(qta);
Max58818582018-01-05 15:04:50 +0100736 uint16_t ms_class = mslot_class_from_ra(ra, is_11bit);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530737 bool failure = false;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200738
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100739 rach_frame();
740
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530741 if (is_11bit)
742 rach_frame_11bit();
743
Philipp Maier1275a3f2017-02-21 19:35:23 +0100744 /* Determine full frame number */
745 Fn = rfn_to_fn(Fn);
Philippd935d882016-11-07 13:07:36 +0100746
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530747 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF on RACH, "
Philipp Maier53f0b4d2017-01-26 11:39:16 +0100748 "so we provide one: ra=0x%02x Fn=%u qta=%d is_11bit=%d:\n",
749 ra, Fn, qta, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530750
Max58818582018-01-05 15:04:50 +0100751 sb = is_single_block(m_bts.force_two_phase, ra, burst_type, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530752
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200753 if (sb) {
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100754 rc = sba()->alloc(&trx_no, &ts_no, &sb_fn, ta);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530755 if (rc < 0) {
756 failure = true;
757 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
758 "single block allocation."
759 "sending Immediate "
760 "Assignment Uplink (AGCH) reject\n");
761 } else {
762 tsc = m_bts.trx[trx_no].pdch[ts_no].tsc;
763
764 LOGP(DRLCMAC, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
765 " qbit-ta=%d ra=0x%02x, Fn=%d (%d,%d,%d),"
766 " SBFn=%d\n",
767 qta, ra,
768 Fn, (Fn / (26 * 51)) % 32, Fn % 51, Fn % 26,
769 sb_fn);
770 LOGP(DRLCMAC, LOGL_INFO, "TX: Immediate Assignment "
771 "Uplink (AGCH)\n");
772 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200773 } else {
774 // Create new TBF
Max5a6bcfb2017-09-01 14:36:44 +0200775 /* FIXME: Copy and paste with other routines.. */
bhargava628dcfb2016-09-14 20:30:31 +0530776
Max92b7a502018-01-26 11:01:35 +0100777 tbf = tbf_alloc_ul_tbf(&m_bts, NULL, -1, 0, ms_class, true);
bhargava628dcfb2016-09-14 20:30:31 +0530778
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200779 if (!tbf) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530780 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource sending "
781 "Immediate Assignment Uplink (AGCH) "
782 "reject\n");
783 rc = -EBUSY;
784 failure = true;
785 } else {
786 tbf->set_ta(ta);
Max2399b1d2018-01-12 15:48:12 +0100787 TBF_SET_STATE(tbf, GPRS_RLCMAC_FLOW);
Max5d7f7572018-01-30 16:56:43 +0100788 TBF_ASS_TYPE_SET(tbf, GPRS_RLCMAC_FLAG_CCCH);
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200789 T_START(tbf, T3169, 3169, "RACH (new UL-TBF)", true);
Maxc21f0072017-12-15 17:36:45 +0100790 LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] START\n");
791 LOGPTBF(tbf, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530792 "qbit-ta=%d ra=0x%02x, Fn=%d "
793 " (%d,%d,%d)\n",
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530794 qta, ra, Fn, (Fn / (26 * 51)) % 32,
795 Fn % 51, Fn % 26);
Maxc21f0072017-12-15 17:36:45 +0100796 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Uplink (AGCH)\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530797 trx_no = tbf->trx->trx_no;
798 ts_no = tbf->first_ts;
799 usf = tbf->m_usf[ts_no];
800 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200801 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200802 }
Alexander Couzensccde5c92017-02-04 03:10:08 +0100803 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx) /* without plen */;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200804 bitvec_unhex(immediate_assignment,
805 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100806
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100807
sivasankari168911b2016-11-25 19:53:36 +0530808 if (failure) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530809 plen = Encoding::write_immediate_assignment_reject(
810 immediate_assignment, ra, Fn,
811 burst_type);
sivasankari168911b2016-11-25 19:53:36 +0530812 immediate_assignment_reject();
813 }
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530814 else {
815 LOGP(DRLCMAC, LOGL_DEBUG,
816 " - TRX=%d (%d) TS=%d TA=%d TSC=%d TFI=%d USF=%d\n",
817 trx_no, m_bts.trx[trx_no].arfcn, ts_no, ta, tsc,
818 tbf ? tbf->tfi() : -1, usf);
Maxfc8afc22019-02-18 18:52:38 +0100819 // N. B: if tbf == NULL then SBA is used for Imm. Ass. below
820 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, false, ra, Fn, ta,
821 m_bts.trx[trx_no].arfcn, ts_no, tsc, usf, false, sb_fn,
822 m_bts.alpha, m_bts.gamma, -1, burst_type);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530823 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100824
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530825 if (plen >= 0) {
826 immediate_assignment_ul_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100827 pcu_l1if_tx_agch(immediate_assignment, plen);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530828 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100829
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200830 bitvec_free(immediate_assignment);
831
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530832 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200833}
834
Maxfc8afc22019-02-18 18:52:38 +0100835void BTS::snd_dl_ass(gprs_rlcmac_tbf *tbf, bool poll, const char *imsi)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200836{
837 int plen;
Jacob Erlbeck29e3a2f2016-02-05 19:04:23 +0100838 unsigned int ts = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200839
Maxc21f0072017-12-15 17:36:45 +0100840 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +0100841 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200842 bitvec_unhex(immediate_assignment, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
843 /* use request reference that has maximum distance to current time,
844 * so the assignment will not conflict with possible RACH requests. */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100845 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d pollFN=%d\n",
846 tbf->trx->trx_no, tbf->trx->arfcn,
847 ts, tbf->ta(), poll ? tbf->poll_fn : -1);
Maxfc8afc22019-02-18 18:52:38 +0100848 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, true, 125,
849 (tbf->pdch[ts]->last_rts_fn + 21216) % GSM_MAX_FN, tbf->ta(),
850 tbf->trx->arfcn, ts, tbf->tsc(), 7, poll,
851 tbf->poll_fn, m_bts.alpha, m_bts.gamma, -1,
852 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530853 if (plen >= 0) {
854 immediate_assignment_dl_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100855 pcu_l1if_tx_pch(immediate_assignment, plen, imsi);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530856 }
857
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200858 bitvec_free(immediate_assignment);
859}
860
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200861
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100862GprsMs *BTS::ms_alloc(uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200863{
864 GprsMs *ms;
865 ms = ms_store().create_ms();
866
867 ms->set_timeout(m_bts.ms_idle_sec);
868 ms->set_ms_class(ms_class);
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100869 ms->set_egprs_ms_class(egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200870
871 return ms;
872}
873
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400874/* update TA based on TA provided by PH-DATA-IND */
875void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
876{
877 int16_t ta_adj;
878 uint8_t ta_target;
879
880 if (ta_delta) {
881 /* adjust TA based on TA provided by PH-DATA-IND */
882 ta_adj = tbf->ta() + ta_delta;
883
884 /* limit target TA in range 0..63 bits */
885 ta_target = ta_limit(ta_adj);
886
887 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating TLLI=0x%08x: TA %u -> %u on "
888 "TRX = %d, TS = %d, FN = %d\n",
889 tbf->tlli(), tbf->ta(), ta_target,
890 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
891 tbf->set_ta(ta_target);
892 }
893}
894
895/* set TA based on TA provided by PH-RA-IND */
896void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
897{
898 uint8_t ta_target;
899
900 if (tbf->ta() != ta) {
901 /* limit target TA in range 0..63 bits */
902 ta_target = ta_limit(ta);
903
904 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating TLLI=0x%08x: TA %u -> %u on "
905 "TRX = %d, TS = %d, FN = %d\n",
906 tbf->tlli(), tbf->ta(), ta_target,
907 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
908 tbf->set_ta(ta_target);
909 }
910}
911
912void 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 +0200913{
914 struct gprs_rlcmac_ul_tbf *tbf =
915 bts_main_data()->bts->ul_tbf_by_poll_fn(fn, trx_no, ts);
916 if (!tbf)
917 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
918 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
919 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400920 else {
921 /* we need to distinguish TA information provided by L1
922 * from PH-DATA-IND and PHY-RA-IND so that we can properly
923 * update TA for given TBF
924 */
925 if (is_rach)
926 set_tbf_ta(tbf, (uint8_t)ta);
927 else
928 update_tbf_ta(tbf, ta);
929
Maxd71e8b32016-09-19 16:17:06 +0200930 }
931}
932
Jacob Erlbeck23f93a12015-06-30 08:52:54 +0200933void gprs_rlcmac_trx::reserve_slots(enum gprs_rlcmac_tbf_direction dir,
934 uint8_t slots)
935{
936 unsigned i;
937 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
938 if (slots & (1 << i))
939 pdch[i].reserve(dir);
940}
941
942void gprs_rlcmac_trx::unreserve_slots(enum gprs_rlcmac_tbf_direction dir,
943 uint8_t slots)
944{
945 unsigned i;
946 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
947 if (slots & (1 << i))
948 pdch[i].unreserve(dir);
949}