blob: a913ca0aa7106c0d0514b60b23cf7744fe3f086a [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 },
77 {}
78};
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 },
83 {}
84};
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 Smith45fdc442019-09-05 15:12:20 +0200234 m_bts.dl_tbf_preemptive_retransmission = true;
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200235 m_bts.T_defs_bts = T_defs_bts;
236 m_bts.T_defs_pcu = T_defs_pcu;
237 osmo_tdefs_reset(m_bts.T_defs_bts);
238 osmo_tdefs_reset(m_bts.T_defs_pcu);
Holger Hans Peter Freyther4ed1dae2013-10-20 10:14:03 +0200239
240 /* initialize back pointers */
241 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(m_bts.trx); ++trx_no) {
242 struct gprs_rlcmac_trx *trx = &m_bts.trx[trx_no];
243 trx->trx_no = trx_no;
244 trx->bts = this;
245
246 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
247 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
248 pdch->ts_no = ts_no;
249 pdch->trx = trx;
250 }
251 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100252
Maxea9968f2017-12-18 22:52:52 +0100253 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100254 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100255 than just allocate the group with different index.
256 This shall be removed once weget rid of BTS singleton */
257 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
258 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
259 else
260 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200261 OSMO_ASSERT(m_ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100262
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100263 m_statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200264 OSMO_ASSERT(m_statg);
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200265}
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200266
Maxd5ffeb52019-03-18 15:48:38 +0100267void BTS::cleanup()
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100268{
Jacob Erlbeckc362df22016-01-20 22:02:19 +0100269 /* this can cause counter updates and must not be left to the
270 * m_ms_store's destructor */
271 m_ms_store.cleanup();
272
Maxd5ffeb52019-03-18 15:48:38 +0100273 if (m_ratectrs) {
274 rate_ctr_group_free(m_ratectrs);
275 m_ratectrs = NULL;
276 }
277
278 if (m_statg) {
279 osmo_stat_item_group_free(m_statg);
280 m_statg = NULL;
281 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100282}
283
Maxd5ffeb52019-03-18 15:48:38 +0100284BTS::~BTS()
285{
286 cleanup();
287}
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100288
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200289void BTS::set_current_frame_number(int fn)
290{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200291 /* The UL frame numbers lag 3 behind the DL frames and the data
292 * indication is only sent after all 4 frames of the block have been
293 * received. Sometimes there is an idle frame between the end of one
294 * and start of another frame (every 3 blocks). So the timeout should
295 * definitely be there if we're more than 8 frames past poll_fn. Let's
Jacob Erlbecke77d49f2015-08-27 13:28:05 +0200296 * stay on the safe side and say 13 or more. An additional delay can
297 * happen due to the block processing time in the DSP, so the delay of
298 * decoded blocks relative to the timing clock can be much larger.
299 * Values up to 50 frames have been observed under load. */
300 const static int max_delay = 60;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200301
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200302 m_cur_fn = fn;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200303 m_pollController.expireTimedout(m_cur_fn, max_delay);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200304}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200305
Max9dabfa22017-05-16 16:10:45 +0200306static inline int delta_fn(int fn, int to)
307{
308 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
309}
310
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200311void BTS::set_current_block_frame_number(int fn, unsigned max_delay)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200312{
313 int delay = 0;
314 const int late_block_delay_thresh = 13;
315 const int fn_update_ok_min_delay = -500;
316 const int fn_update_ok_max_delay = 0;
317
318 /* frame numbers in the received blocks are assumed to be strongly
319 * monotonic. */
320 if (m_cur_blk_fn >= 0) {
Max9dabfa22017-05-16 16:10:45 +0200321 int delta = delta_fn(fn, m_cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200322 if (delta <= 0)
323 return;
324 }
325
326 /* Check block delay vs. the current frame number */
327 if (current_frame_number() != 0)
Max9dabfa22017-05-16 16:10:45 +0200328 delay = delta_fn(fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200329 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200330 LOGP(DRLCMAC, LOGL_NOTICE,
331 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
332 delay, fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200333 rlc_late_block();
334 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200335
336 m_cur_blk_fn = fn;
337 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
338 current_frame_number() == 0)
339 m_cur_fn = fn;
340
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200341 m_pollController.expireTimedout(fn, max_delay);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200342}
343
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200344int BTS::add_paging(uint8_t chan_needed, uint8_t *identity_lv)
345{
346 uint8_t l, trx, ts, any_tbf = 0;
347 struct gprs_rlcmac_tbf *tbf;
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100348 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200349 uint8_t slot_mask[8];
350 int8_t first_ts; /* must be signed */
351
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100352 LListHead<gprs_rlcmac_tbf> *tbfs_lists[] = {
353 &m_ul_tbfs,
354 &m_dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200355 NULL
356 };
357
358
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200359 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n",
360 chan_needed, osmo_hexdump(identity_lv + 1, identity_lv[0]));
361
362 /* collect slots to page
363 * Mark slots for every TBF, but only mark one of it.
364 * Mark only the first slot found.
365 * Don't mark, if TBF uses a different slot that is already marked. */
366 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200367 for (l = 0; tbfs_lists[l]; l++) {
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100368 llist_for_each(pos, tbfs_lists[l]) {
369 tbf = pos->entry();
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200370 first_ts = -1;
371 for (ts = 0; ts < 8; ts++) {
372 if (tbf->pdch[ts]) {
373 /* remember the first slot found */
374 if (first_ts < 0)
375 first_ts = ts;
376 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100377 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200378 break;
379 }
380 }
381 /* mark first slot found, if none is marked already */
382 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100383 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200384 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100385 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100386 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200387 } else
Maxc21f0072017-12-15 17:36:45 +0100388 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200389 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100390 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200391 }
392 }
393
394 /* Now we have a list of marked slots. Every TBF uses at least one
395 * of these slots. */
396
397 /* schedule paging to all marked slots */
398 for (trx = 0; trx < 8; trx++) {
399 if (slot_mask[trx] == 0)
400 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200401 for (ts = 0; ts < 8; ts++) {
402 if ((slot_mask[trx] & (1 << ts))) {
403 /* schedule */
Max4382e4e2018-01-30 12:00:08 +0100404 if (!m_bts.trx[trx].pdch[ts].add_paging(chan_needed, identity_lv))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200405 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100406
407 LOGP(DRLCMAC, LOGL_INFO, "Paging on PACCH of TRX=%d TS=%d\n", trx, ts);
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200408 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200409 }
410 }
411 }
412
413 if (!any_tbf)
414 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
415
416 return 0;
417}
418
Harald Welte717cdf52017-07-21 21:56:23 +0200419void BTS::send_gsmtap(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
420 uint8_t ts_no, uint8_t channel, uint32_t fn,
421 const uint8_t *data, unsigned int len)
422{
423 uint16_t arfcn;
424
425 /* check if category is activated at all */
426 if (!(m_bts.gsmtap_categ_mask & (1 << categ)))
427 return;
428
429 arfcn = m_bts.trx[trx_no].arfcn;
430 if (uplink)
431 arfcn |= GSMTAP_ARFCN_F_UPLINK;
432
433 gsmtap_send(m_bts.gsmtap, arfcn, ts_no, channel, 0, fn, 0, 0, data, len);
434}
435
Max341dccd2017-08-14 15:49:59 +0200436static inline bool tbf_check(gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t trx_no, uint8_t ts)
437{
Maxcac6b662018-01-24 11:00:17 +0100438 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING) && tbf->poll_scheduled()
Max341dccd2017-08-14 15:49:59 +0200439 && tbf->poll_fn == fn && tbf->trx->trx_no == trx_no && tbf->poll_ts == ts)
440 return true;
441
442 return false;
443}
444
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200445gprs_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 +0200446{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100447 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200448
449 /* only one TBF can poll on specific TS/FN, because scheduler can only
450 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100451 llist_for_each(pos, &m_dl_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200452 if (tbf_check(pos->entry(), fn, trx, ts))
453 return as_dl_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200454 }
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200455 return NULL;
456}
Max341dccd2017-08-14 15:49:59 +0200457
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200458gprs_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 +0200459{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100460 LListHead<gprs_rlcmac_tbf> *pos;
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200461
462 /* only one TBF can poll on specific TS/FN, because scheduler can only
463 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100464 llist_for_each(pos, &m_ul_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200465 if (tbf_check(pos->entry(), fn, trx, ts))
466 return as_ul_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200467 }
468 return NULL;
469}
470
Daniel Willmann54044b02014-07-02 17:58:15 +0200471/* lookup downlink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200472gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200473{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200474 if (trx >= 8 || ts >= 8)
475 return NULL;
476
477 return m_bts.trx[trx].pdch[ts].dl_tbf_by_tfi(tfi);
Daniel Willmann54044b02014-07-02 17:58:15 +0200478}
479
480/* lookup uplink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200481gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200482{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200483 if (trx >= 8 || ts >= 8)
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200484 return NULL;
485
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200486 return m_bts.trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200487}
488
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200489/*
490 * Search for free TFI and return TFI, TRX.
491 * This method returns the first TFI that is currently not used in any PDCH of
492 * a TRX. The first TRX that contains such an TFI is returned. Negative values
493 * indicate errors.
494 */
Maxa76a7d02018-01-26 11:09:16 +0100495int 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 +0200496{
Maxa76a7d02018-01-26 11:09:16 +0100497 const struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200498 uint32_t free_tfis;
499 bool has_pdch = false;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200500 uint8_t trx_from, trx_to, trx, ts, tfi;
501
502 if (use_trx >= 0 && use_trx < 8)
503 trx_from = trx_to = use_trx;
504 else {
505 trx_from = 0;
506 trx_to = 7;
507 }
508
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200509 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200510 for (trx = trx_from; trx <= trx_to; trx++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200511 bool trx_has_pdch = false;
512
Maxd000d802017-09-20 17:55:28 +0200513 free_tfis = NO_FREE_TFI;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200514
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200515 for (ts = 0; ts < 8; ts++) {
516 pdch = &m_bts.trx[trx].pdch[ts];
517 if (!pdch->is_enabled())
518 continue;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200519 free_tfis &= ~pdch->assigned_tfi(dir);
520 trx_has_pdch = true;
521 has_pdch = true;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200522 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200523 if (trx_has_pdch && free_tfis)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200524 break;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200525
526 free_tfis = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200527 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200528 if (!has_pdch) {
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200529 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
530 return -EINVAL;
531 }
532
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200533 if (!free_tfis) {
Max69d585e2018-01-26 12:36:43 +0100534 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200535 return -EBUSY;
536 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200537
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200538
539 LOGP(DRLCMAC, LOGL_DEBUG,
540 "Searching for first unallocated TFI: TRX=%d\n", trx);
541
542 /* find the first */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200543 for (tfi = 0; tfi < 32; tfi++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200544 if (free_tfis & 1 << tfi)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200545 break;
546 }
547
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200548 OSMO_ASSERT(tfi < 32);
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200549
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200550 LOGP(DRLCMAC, LOGL_DEBUG, " Found TFI=%d.\n", tfi);
551 *_trx = trx;
552 return tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200553}
554
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200555int BTS::rcv_imm_ass_cnf(const uint8_t *data, uint32_t fn)
556{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200557 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200558 uint8_t plen;
559 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200560 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200561
562 /* move to IA Rest Octets */
563 plen = data[0] >> 2;
564 data += 1 + plen;
565
566 if ((*data & 0xf0) != 0xd0) {
567 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
568 "octets do not start with bit sequence 'HH01' "
569 "(Packet Downlink Assignment)\n");
570 return -EINVAL;
571 }
572
573 /* get TLLI from downlink assignment */
Eric Wildab8b01e2019-07-23 17:16:06 +0200574 tlli = (uint32_t)((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200575 tlli |= (*data++) << 20;
576 tlli |= (*data++) << 12;
577 tlli |= (*data++) << 4;
578 tlli |= (*data++) >> 4;
579
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200580 ms = ms_by_tlli(tlli);
581 if (ms)
582 dl_tbf = ms->dl_tbf();
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200583 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200584 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
585 "does not exit\n", tlli);
586 return -EINVAL;
587 }
588
589 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
590
Daniel Willmann7e994e32014-08-07 15:49:21 +0200591 if (dl_tbf->m_wait_confirm)
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200592 T_START(dl_tbf, T0, -2002, "assignment (AGCH)", true);
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200593
594 return 0;
595}
596
Philipp Maier1275a3f2017-02-21 19:35:23 +0100597/* Determine the full frame number from a relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100598uint32_t BTS::rfn_to_fn(int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100599{
Max5dd8d1b2017-03-08 12:06:42 +0100600 int32_t m_cur_rfn;
601 int32_t fn;
602 int32_t fn_rounded;
603
604 /* double-check that relative FN is not negative and fits into int32_t */
605 OSMO_ASSERT(rfn < GSM_MAX_FN);
606 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100607
608 /* Note: If a BTS is sending in a rach request it will be fully aware
609 * of the frame number. If the PCU is used in a BSC-co-located setup.
610 * The BSC will forward the incoming RACH request. The RACH request
611 * only contains the relative frame number (Fn % 42432) in its request
612 * reference. This PCU implementation has to fit both scenarios, so
613 * we need to assume that Fn is a relative frame number. */
614
615 /* Ensure that all following calculations are performed with the
616 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100617 if (rfn >= RFN_MODULUS)
618 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100619
620 /* Compute an internal relative frame number from the full internal
621 frame number */
622 m_cur_rfn = m_cur_fn % RFN_MODULUS;
623
624 /* Compute a "rounded" version of the internal frame number, which
625 * exactly fits in the RFN_MODULUS raster */
626 fn_rounded = m_cur_fn - m_cur_rfn;
627
628 /* If the delta between the internal and the external relative frame
629 * number exceeds a certain limit, we need to assume that the incoming
630 * rach request belongs to a the previous rfn period. To correct this,
631 * we roll back the rounded frame number by one RFN_MODULUS */
632 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
633 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200634 "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 +0100635 rfn, m_cur_fn, RFN_MODULUS);
636 if (fn_rounded < RFN_MODULUS) {
637 LOGP(DRLCMAC, LOGL_DEBUG,
638 "Cornercase detected: wrapping crosses %u border\n",
639 GSM_MAX_FN);
640 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
641 }
642 else
643 fn_rounded -= RFN_MODULUS;
644 }
645
646 /* The real frame number is the sum of the rounded frame number and the
647 * relative framenumber computed via RACH */
648 fn = fn_rounded + rfn;
649
650 return fn;
651}
652
Max58818582018-01-05 15:04:50 +0100653static inline uint16_t mslot_class_from_ra(uint16_t ra, bool is_11bit)
654{
655 if (is_11bit)
656 return (ra & 0x3e0) >> 5;
657
658 /* set multislot class to 0 for 8-bit RACH, since we don't know it yet */
659 return 0;
660}
661
662static inline uint16_t priority_from_ra(uint16_t ra, bool is_11bit)
663{
664 if (is_11bit)
665 return (ra & 0x18) >> 3;
666
667 return 0;
668}
669
670static inline bool is_single_block(bool force_two_phase, uint16_t ra, enum ph_burst_type burst_type, bool is_11bit)
671{
672 bool sb = false;
673
674 if ((ra & 0xf8) == 0x70)
675 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
676 else if (force_two_phase)
677 LOGP(DRLCMAC, LOGL_DEBUG,
678 "MS requests single phase access, but we force two phase access [RACH is %s bit]\n",
679 is_11bit ? "11" : "8");
680
Max137fd592018-01-05 15:53:08 +0100681 switch(burst_type) {
682 case GSM_L1_BURST_TYPE_ACCESS_0:
683 if (is_11bit) {
684 LOGP(DRLCMAC, LOGL_ERROR, "Error: GPRS 11 bit RACH not supported\n");
685 return false;
686 }
Max58818582018-01-05 15:04:50 +0100687
688 if ((ra & 0xf8) == 0x70)
Max137fd592018-01-05 15:53:08 +0100689 return true;
690
691 if (force_two_phase)
692 return true;
693 break;
694 case GSM_L1_BURST_TYPE_ACCESS_1: /* deliberate fall-through */
695 case GSM_L1_BURST_TYPE_ACCESS_2:
696 if (is_11bit) {
697 if (!(ra & (1 << 10))) {
698 if (force_two_phase)
699 return true;
700
701 return false;
702 }
703
704 return true;
Max58818582018-01-05 15:04:50 +0100705 }
Max137fd592018-01-05 15:53:08 +0100706 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for 8-bit RACH\n", burst_type);
707 break;
708 case GSM_L1_BURST_TYPE_NONE:
709 LOGP(DRLCMAC, LOGL_ERROR, "PCU has not received burst type from BTS\n");
710 break;
711 default:
712 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for %s-bit RACH\n",
713 burst_type, is_11bit ? "11" : "8");
Max58818582018-01-05 15:04:50 +0100714 }
715
716 return sb;
717}
718
719int BTS::rcv_rach(uint16_t ra, uint32_t Fn, int16_t qta, bool is_11bit,
bhargava959d1de2016-08-17 15:17:21 +0530720 enum ph_burst_type burst_type)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200721{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200722 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Holger Hans Peter Freytherf37e5142013-12-25 18:56:34 +0100723 uint8_t trx_no, ts_no = 0;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200724 uint8_t sb = 0;
725 uint32_t sb_fn = 0;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530726 int rc = 0;
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100727 int plen;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100728 uint8_t usf = 7;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530729 uint8_t tsc = 0, ta = qta2ta(qta);
Max58818582018-01-05 15:04:50 +0100730 uint16_t ms_class = mslot_class_from_ra(ra, is_11bit);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530731 bool failure = false;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200732
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100733 rach_frame();
734
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530735 if (is_11bit)
736 rach_frame_11bit();
737
Philipp Maier1275a3f2017-02-21 19:35:23 +0100738 /* Determine full frame number */
739 Fn = rfn_to_fn(Fn);
Philippd935d882016-11-07 13:07:36 +0100740
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530741 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF on RACH, "
Philipp Maier53f0b4d2017-01-26 11:39:16 +0100742 "so we provide one: ra=0x%02x Fn=%u qta=%d is_11bit=%d:\n",
743 ra, Fn, qta, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530744
Max58818582018-01-05 15:04:50 +0100745 sb = is_single_block(m_bts.force_two_phase, ra, burst_type, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530746
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200747 if (sb) {
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100748 rc = sba()->alloc(&trx_no, &ts_no, &sb_fn, ta);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530749 if (rc < 0) {
750 failure = true;
751 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
752 "single block allocation."
753 "sending Immediate "
754 "Assignment Uplink (AGCH) reject\n");
755 } else {
756 tsc = m_bts.trx[trx_no].pdch[ts_no].tsc;
757
758 LOGP(DRLCMAC, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
759 " qbit-ta=%d ra=0x%02x, Fn=%d (%d,%d,%d),"
760 " SBFn=%d\n",
761 qta, ra,
762 Fn, (Fn / (26 * 51)) % 32, Fn % 51, Fn % 26,
763 sb_fn);
764 LOGP(DRLCMAC, LOGL_INFO, "TX: Immediate Assignment "
765 "Uplink (AGCH)\n");
766 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200767 } else {
768 // Create new TBF
Max5a6bcfb2017-09-01 14:36:44 +0200769 /* FIXME: Copy and paste with other routines.. */
bhargava628dcfb2016-09-14 20:30:31 +0530770
Max92b7a502018-01-26 11:01:35 +0100771 tbf = tbf_alloc_ul_tbf(&m_bts, NULL, -1, 0, ms_class, true);
bhargava628dcfb2016-09-14 20:30:31 +0530772
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200773 if (!tbf) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530774 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource sending "
775 "Immediate Assignment Uplink (AGCH) "
776 "reject\n");
777 rc = -EBUSY;
778 failure = true;
779 } else {
780 tbf->set_ta(ta);
Max2399b1d2018-01-12 15:48:12 +0100781 TBF_SET_STATE(tbf, GPRS_RLCMAC_FLOW);
Max5d7f7572018-01-30 16:56:43 +0100782 TBF_ASS_TYPE_SET(tbf, GPRS_RLCMAC_FLAG_CCCH);
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200783 T_START(tbf, T3169, 3169, "RACH (new UL-TBF)", true);
Maxc21f0072017-12-15 17:36:45 +0100784 LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] START\n");
785 LOGPTBF(tbf, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530786 "qbit-ta=%d ra=0x%02x, Fn=%d "
787 " (%d,%d,%d)\n",
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530788 qta, ra, Fn, (Fn / (26 * 51)) % 32,
789 Fn % 51, Fn % 26);
Maxc21f0072017-12-15 17:36:45 +0100790 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Uplink (AGCH)\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530791 trx_no = tbf->trx->trx_no;
792 ts_no = tbf->first_ts;
793 usf = tbf->m_usf[ts_no];
794 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200795 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200796 }
Alexander Couzensccde5c92017-02-04 03:10:08 +0100797 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx) /* without plen */;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200798 bitvec_unhex(immediate_assignment,
799 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100800
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100801
sivasankari168911b2016-11-25 19:53:36 +0530802 if (failure) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530803 plen = Encoding::write_immediate_assignment_reject(
804 immediate_assignment, ra, Fn,
805 burst_type);
sivasankari168911b2016-11-25 19:53:36 +0530806 immediate_assignment_reject();
807 }
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530808 else {
809 LOGP(DRLCMAC, LOGL_DEBUG,
810 " - TRX=%d (%d) TS=%d TA=%d TSC=%d TFI=%d USF=%d\n",
811 trx_no, m_bts.trx[trx_no].arfcn, ts_no, ta, tsc,
812 tbf ? tbf->tfi() : -1, usf);
Maxfc8afc22019-02-18 18:52:38 +0100813 // N. B: if tbf == NULL then SBA is used for Imm. Ass. below
814 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, false, ra, Fn, ta,
815 m_bts.trx[trx_no].arfcn, ts_no, tsc, usf, false, sb_fn,
816 m_bts.alpha, m_bts.gamma, -1, burst_type);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530817 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100818
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530819 if (plen >= 0) {
820 immediate_assignment_ul_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100821 pcu_l1if_tx_agch(immediate_assignment, plen);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530822 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100823
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200824 bitvec_free(immediate_assignment);
825
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530826 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200827}
828
Maxfc8afc22019-02-18 18:52:38 +0100829void BTS::snd_dl_ass(gprs_rlcmac_tbf *tbf, bool poll, const char *imsi)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200830{
831 int plen;
Jacob Erlbeck29e3a2f2016-02-05 19:04:23 +0100832 unsigned int ts = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200833
Maxc21f0072017-12-15 17:36:45 +0100834 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +0100835 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200836 bitvec_unhex(immediate_assignment, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
837 /* use request reference that has maximum distance to current time,
838 * so the assignment will not conflict with possible RACH requests. */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100839 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d pollFN=%d\n",
840 tbf->trx->trx_no, tbf->trx->arfcn,
841 ts, tbf->ta(), poll ? tbf->poll_fn : -1);
Maxfc8afc22019-02-18 18:52:38 +0100842 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, true, 125,
843 (tbf->pdch[ts]->last_rts_fn + 21216) % GSM_MAX_FN, tbf->ta(),
844 tbf->trx->arfcn, ts, tbf->tsc(), 7, poll,
845 tbf->poll_fn, m_bts.alpha, m_bts.gamma, -1,
846 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530847 if (plen >= 0) {
848 immediate_assignment_dl_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100849 pcu_l1if_tx_pch(immediate_assignment, plen, imsi);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530850 }
851
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200852 bitvec_free(immediate_assignment);
853}
854
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200855
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100856GprsMs *BTS::ms_alloc(uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200857{
858 GprsMs *ms;
859 ms = ms_store().create_ms();
860
861 ms->set_timeout(m_bts.ms_idle_sec);
862 ms->set_ms_class(ms_class);
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100863 ms->set_egprs_ms_class(egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200864
865 return ms;
866}
867
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400868/* update TA based on TA provided by PH-DATA-IND */
869void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
870{
871 int16_t ta_adj;
872 uint8_t ta_target;
873
874 if (ta_delta) {
875 /* adjust TA based on TA provided by PH-DATA-IND */
876 ta_adj = tbf->ta() + ta_delta;
877
878 /* limit target TA in range 0..63 bits */
879 ta_target = ta_limit(ta_adj);
880
881 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating TLLI=0x%08x: TA %u -> %u on "
882 "TRX = %d, TS = %d, FN = %d\n",
883 tbf->tlli(), tbf->ta(), ta_target,
884 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
885 tbf->set_ta(ta_target);
886 }
887}
888
889/* set TA based on TA provided by PH-RA-IND */
890void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
891{
892 uint8_t ta_target;
893
894 if (tbf->ta() != ta) {
895 /* limit target TA in range 0..63 bits */
896 ta_target = ta_limit(ta);
897
898 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating TLLI=0x%08x: TA %u -> %u on "
899 "TRX = %d, TS = %d, FN = %d\n",
900 tbf->tlli(), tbf->ta(), ta_target,
901 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
902 tbf->set_ta(ta_target);
903 }
904}
905
906void 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 +0200907{
908 struct gprs_rlcmac_ul_tbf *tbf =
909 bts_main_data()->bts->ul_tbf_by_poll_fn(fn, trx_no, ts);
910 if (!tbf)
911 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
912 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
913 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400914 else {
915 /* we need to distinguish TA information provided by L1
916 * from PH-DATA-IND and PHY-RA-IND so that we can properly
917 * update TA for given TBF
918 */
919 if (is_rach)
920 set_tbf_ta(tbf, (uint8_t)ta);
921 else
922 update_tbf_ta(tbf, ta);
923
Maxd71e8b32016-09-19 16:17:06 +0200924 }
925}
926
Jacob Erlbeck23f93a12015-06-30 08:52:54 +0200927void gprs_rlcmac_trx::reserve_slots(enum gprs_rlcmac_tbf_direction dir,
928 uint8_t slots)
929{
930 unsigned i;
931 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
932 if (slots & (1 << i))
933 pdch[i].reserve(dir);
934}
935
936void gprs_rlcmac_trx::unreserve_slots(enum gprs_rlcmac_tbf_direction dir,
937 uint8_t slots)
938{
939 unsigned i;
940 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
941 if (slots & (1 << i))
942 pdch[i].unreserve(dir);
943}