blob: 395fc73360774fb57769028f1d727e787dfe5b9b [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
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +010071/**
72 * For gcc-4.4 compat do not use extended initializer list but keep the
73 * order from the enum here. Once we support GCC4.7 and up we can change
74 * the code below.
75 */
76static const struct rate_ctr_desc bts_ctr_description[] = {
Maxa5eb67d2017-11-21 20:24:54 +010077 { "tbf:dl:alloc", "TBF DL Allocated "},
78 { "tbf:dl:freed", "TBF DL Freed "},
79 { "tbf:dl:aborted", "TBF DL Aborted "},
80 { "tbf:ul:alloc", "TBF UL Allocated "},
81 { "tbf:ul:freed", "TBF UL Freed "},
82 { "tbf:ul:aborted", "TBF UL Aborted "},
83 { "tbf:reused", "TBF Reused "},
84 { "tbf:alloc:algo-a", "TBF Alloc Algo A "},
85 { "tbf:alloc:algo-b", "TBF Alloc Algo B "},
86 { "tbf:failed:egprs-only", "TBF Failed EGPRS-only"},
87 { "rlc:sent", "RLC Sent "},
88 { "rlc:resent", "RLC Resent "},
89 { "rlc:restarted", "RLC Restarted "},
90 { "rlc:stalled", "RLC Stalled "},
91 { "rlc:nacked", "RLC Nacked "},
92 { "rlc:final_block_resent", "RLC Final Blk resent "},
93 { "rlc:ass:timedout", "RLC Assign Timeout "},
94 { "rlc:ass:failed", "RLC Assign Failed "},
95 { "rlc:ack:timedout", "RLC Ack Timeout "},
96 { "rlc:ack:failed", "RLC Ack Failed "},
97 { "rlc:rel:timedout", "RLC Release Timeout "},
98 { "rlc:late-block", "RLC Late Block "},
99 { "rlc:sent-dummy", "RLC Sent Dummy "},
100 { "rlc:sent-control", "RLC Sent Control "},
101 { "rlc:dl_bytes", "RLC DL Bytes "},
102 { "rlc:dl_payload_bytes", "RLC DL Payload Bytes "},
103 { "rlc:ul_bytes", "RLC UL Bytes "},
104 { "rlc:ul_payload_bytes", "RLC UL Payload Bytes "},
105 { "decode:errors", "Decode Errors "},
106 { "sba:allocated", "SBA Allocated "},
107 { "sba:freed", "SBA Freed "},
108 { "sba:timedout", "SBA Timeout "},
109 { "llc:timeout", "Timedout Frames "},
110 { "llc:dropped", "Dropped Frames "},
111 { "llc:scheduled", "Scheduled Frames "},
112 { "llc:dl_bytes", "RLC encapsulated PDUs"},
113 { "llc:ul_bytes", "full PDUs received "},
114 { "rach:requests", "RACH requests "},
115 { "11bit_rach:requests", "11BIT_RACH requests "},
116 { "spb:uplink_first_segment", "First seg of UL SPB "},
117 { "spb:uplink_second_segment", "Second seg of UL SPB "},
118 { "spb:downlink_first_segment", "First seg of DL SPB "},
119 { "spb:downlink_second_segment","Second seg of DL SPB "},
120 { "immediate:assignment_UL", "Immediate Assign UL "},
121 { "immediate:assignment_rej", "Immediate Assign Rej "},
122 { "immediate:assignment_DL", "Immediate Assign DL "},
123 { "channel:request_description","Channel Request Desc "},
124 { "pkt:ul_assignment", "Packet UL Assignment "},
125 { "pkt:access_reject", "Packet Access Reject "},
126 { "pkt:dl_assignment", "Packet DL Assignment "},
127 { "ul:control", "UL control Block "},
128 { "ul:assignment_poll_timeout", "UL Assign Timeout "},
129 { "ul:assignment_failed", "UL Assign Failed "},
130 { "dl:assignment_timeout", "DL Assign Timeout "},
131 { "dl:assignment_failed", "DL Assign Failed "},
132 { "pkt:ul_ack_nack_timeout", "PUAN Poll Timeout "},
133 { "pkt:ul_ack_nack_failed", "PUAN poll Failed "},
134 { "pkt:dl_ack_nack_timeout", "PDAN poll Timeout "},
135 { "pkt:dl_ack_nack_failed", "PDAN poll Failed "},
136 { "gprs:downlink_cs1", "CS1 downlink "},
137 { "gprs:downlink_cs2", "CS2 downlink "},
138 { "gprs:downlink_cs3", "CS3 downlink "},
139 { "gprs:downlink_cs4", "CS4 downlink "},
140 { "egprs:downlink_mcs1", "MCS1 downlink "},
141 { "egprs:downlink_mcs2", "MCS2 downlink "},
142 { "egprs:downlink_mcs3", "MCS3 downlink "},
143 { "egprs:downlink_mcs4", "MCS4 downlink "},
144 { "egprs:downlink_mcs5", "MCS5 downlink "},
145 { "egprs:downlink_mcs6", "MCS6 downlink "},
146 { "egprs:downlink_mcs7", "MCS7 downlink "},
147 { "egprs:downlink_mcs8", "MCS8 downlink "},
148 { "egprs:downlink_mcs9", "MCS9 downlink "},
149 { "gprs:uplink_cs1", "CS1 Uplink "},
150 { "gprs:uplink_cs2", "CS2 Uplink "},
151 { "gprs:uplink_cs3", "CS3 Uplink "},
152 { "gprs:uplink_cs4", "CS4 Uplink "},
153 { "egprs:uplink_mcs1", "MCS1 Uplink "},
154 { "egprs:uplink_mcs2", "MCS2 Uplink "},
155 { "egprs:uplink_mcs3", "MCS3 Uplink "},
156 { "egprs:uplink_mcs4", "MCS4 Uplink "},
157 { "egprs:uplink_mcs5", "MCS5 Uplink "},
158 { "egprs:uplink_mcs6", "MCS6 Uplink "},
159 { "egprs:uplink_mcs7", "MCS7 Uplink "},
160 { "egprs:uplink_mcs8", "MCS8 Uplink "},
161 { "egprs:uplink_mcs9", "MCS9 Uplink "},
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100162};
163
164static const struct rate_ctr_group_desc bts_ctrg_desc = {
165 "bts",
166 "BTS Statistics",
Holger Hans Peter Freytherb8a54262015-11-07 21:00:00 +0100167 OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100168 ARRAY_SIZE(bts_ctr_description),
169 bts_ctr_description,
170};
171
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100172static const struct osmo_stat_item_desc bts_stat_item_description[] = {
173 { "ms.present", "MS Present ",
174 OSMO_STAT_ITEM_NO_UNIT, 4, 0},
175};
176
177static const struct osmo_stat_item_group_desc bts_statg_desc = {
178 "bts",
179 "BTS Statistics",
180 OSMO_STATS_CLASS_GLOBAL,
181 ARRAY_SIZE(bts_stat_item_description),
182 bts_stat_item_description,
183};
184
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200185BTS* BTS::main_bts()
186{
187 return &s_bts;
188}
189
190struct gprs_rlcmac_bts *BTS::bts_data()
191{
192 return &m_bts;
193}
194
195struct gprs_rlcmac_bts *bts_main_data()
196{
197 return BTS::main_bts()->bts_data();
198}
199
Maxd5ffeb52019-03-18 15:48:38 +0100200void bts_cleanup()
201{
202 return BTS::main_bts()->cleanup();
203}
204
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100205struct rate_ctr_group *bts_main_data_stats()
206{
207 return BTS::main_bts()->rate_counters();
208}
209
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200210BTS::BTS()
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200211 : m_cur_fn(0)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200212 , m_cur_blk_fn(-1)
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +0200213 , m_pollController(*this)
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200214 , m_sba(*this)
Jacob Erlbeck17214bb2015-06-02 14:06:12 +0200215 , m_ms_store(this)
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200216{
217 memset(&m_bts, 0, sizeof(m_bts));
218 m_bts.bts = this;
Holger Hans Peter Freyther4ed1dae2013-10-20 10:14:03 +0200219
220 /* initialize back pointers */
221 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(m_bts.trx); ++trx_no) {
222 struct gprs_rlcmac_trx *trx = &m_bts.trx[trx_no];
223 trx->trx_no = trx_no;
224 trx->bts = this;
225
226 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
227 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
228 pdch->ts_no = ts_no;
229 pdch->trx = trx;
230 }
231 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100232
Maxea9968f2017-12-18 22:52:52 +0100233 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100234 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100235 than just allocate the group with different index.
236 This shall be removed once weget rid of BTS singleton */
237 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
238 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
239 else
240 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200241 OSMO_ASSERT(m_ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100242
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100243 m_statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200244 OSMO_ASSERT(m_statg);
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200245}
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200246
Maxd5ffeb52019-03-18 15:48:38 +0100247void BTS::cleanup()
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100248{
Jacob Erlbeckc362df22016-01-20 22:02:19 +0100249 /* this can cause counter updates and must not be left to the
250 * m_ms_store's destructor */
251 m_ms_store.cleanup();
252
Maxd5ffeb52019-03-18 15:48:38 +0100253 if (m_ratectrs) {
254 rate_ctr_group_free(m_ratectrs);
255 m_ratectrs = NULL;
256 }
257
258 if (m_statg) {
259 osmo_stat_item_group_free(m_statg);
260 m_statg = NULL;
261 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100262}
263
Maxd5ffeb52019-03-18 15:48:38 +0100264BTS::~BTS()
265{
266 cleanup();
267}
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100268
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200269void BTS::set_current_frame_number(int fn)
270{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200271 /* The UL frame numbers lag 3 behind the DL frames and the data
272 * indication is only sent after all 4 frames of the block have been
273 * received. Sometimes there is an idle frame between the end of one
274 * and start of another frame (every 3 blocks). So the timeout should
275 * definitely be there if we're more than 8 frames past poll_fn. Let's
Jacob Erlbecke77d49f2015-08-27 13:28:05 +0200276 * stay on the safe side and say 13 or more. An additional delay can
277 * happen due to the block processing time in the DSP, so the delay of
278 * decoded blocks relative to the timing clock can be much larger.
279 * Values up to 50 frames have been observed under load. */
280 const static int max_delay = 60;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200281
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200282 m_cur_fn = fn;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200283 m_pollController.expireTimedout(m_cur_fn, max_delay);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200284}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200285
Max9dabfa22017-05-16 16:10:45 +0200286static inline int delta_fn(int fn, int to)
287{
288 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
289}
290
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200291void BTS::set_current_block_frame_number(int fn, unsigned max_delay)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200292{
293 int delay = 0;
294 const int late_block_delay_thresh = 13;
295 const int fn_update_ok_min_delay = -500;
296 const int fn_update_ok_max_delay = 0;
297
298 /* frame numbers in the received blocks are assumed to be strongly
299 * monotonic. */
300 if (m_cur_blk_fn >= 0) {
Max9dabfa22017-05-16 16:10:45 +0200301 int delta = delta_fn(fn, m_cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200302 if (delta <= 0)
303 return;
304 }
305
306 /* Check block delay vs. the current frame number */
307 if (current_frame_number() != 0)
Max9dabfa22017-05-16 16:10:45 +0200308 delay = delta_fn(fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200309 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200310 LOGP(DRLCMAC, LOGL_NOTICE,
311 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
312 delay, fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200313 rlc_late_block();
314 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200315
316 m_cur_blk_fn = fn;
317 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
318 current_frame_number() == 0)
319 m_cur_fn = fn;
320
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200321 m_pollController.expireTimedout(fn, max_delay);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200322}
323
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200324int BTS::add_paging(uint8_t chan_needed, uint8_t *identity_lv)
325{
326 uint8_t l, trx, ts, any_tbf = 0;
327 struct gprs_rlcmac_tbf *tbf;
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100328 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200329 uint8_t slot_mask[8];
330 int8_t first_ts; /* must be signed */
331
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100332 LListHead<gprs_rlcmac_tbf> *tbfs_lists[] = {
333 &m_ul_tbfs,
334 &m_dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200335 NULL
336 };
337
338
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200339 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n",
340 chan_needed, osmo_hexdump(identity_lv + 1, identity_lv[0]));
341
342 /* collect slots to page
343 * Mark slots for every TBF, but only mark one of it.
344 * Mark only the first slot found.
345 * Don't mark, if TBF uses a different slot that is already marked. */
346 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200347 for (l = 0; tbfs_lists[l]; l++) {
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100348 llist_for_each(pos, tbfs_lists[l]) {
349 tbf = pos->entry();
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200350 first_ts = -1;
351 for (ts = 0; ts < 8; ts++) {
352 if (tbf->pdch[ts]) {
353 /* remember the first slot found */
354 if (first_ts < 0)
355 first_ts = ts;
356 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100357 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200358 break;
359 }
360 }
361 /* mark first slot found, if none is marked already */
362 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100363 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200364 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100365 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100366 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200367 } else
Maxc21f0072017-12-15 17:36:45 +0100368 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200369 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100370 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200371 }
372 }
373
374 /* Now we have a list of marked slots. Every TBF uses at least one
375 * of these slots. */
376
377 /* schedule paging to all marked slots */
378 for (trx = 0; trx < 8; trx++) {
379 if (slot_mask[trx] == 0)
380 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200381 for (ts = 0; ts < 8; ts++) {
382 if ((slot_mask[trx] & (1 << ts))) {
383 /* schedule */
Max4382e4e2018-01-30 12:00:08 +0100384 if (!m_bts.trx[trx].pdch[ts].add_paging(chan_needed, identity_lv))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200385 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100386
387 LOGP(DRLCMAC, LOGL_INFO, "Paging on PACCH of TRX=%d TS=%d\n", trx, ts);
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200388 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200389 }
390 }
391 }
392
393 if (!any_tbf)
394 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
395
396 return 0;
397}
398
Harald Welte717cdf52017-07-21 21:56:23 +0200399void BTS::send_gsmtap(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
400 uint8_t ts_no, uint8_t channel, uint32_t fn,
401 const uint8_t *data, unsigned int len)
402{
403 uint16_t arfcn;
404
405 /* check if category is activated at all */
406 if (!(m_bts.gsmtap_categ_mask & (1 << categ)))
407 return;
408
409 arfcn = m_bts.trx[trx_no].arfcn;
410 if (uplink)
411 arfcn |= GSMTAP_ARFCN_F_UPLINK;
412
413 gsmtap_send(m_bts.gsmtap, arfcn, ts_no, channel, 0, fn, 0, 0, data, len);
414}
415
Max341dccd2017-08-14 15:49:59 +0200416static inline bool tbf_check(gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t trx_no, uint8_t ts)
417{
Maxcac6b662018-01-24 11:00:17 +0100418 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING) && tbf->poll_scheduled()
Max341dccd2017-08-14 15:49:59 +0200419 && tbf->poll_fn == fn && tbf->trx->trx_no == trx_no && tbf->poll_ts == ts)
420 return true;
421
422 return false;
423}
424
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200425gprs_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 +0200426{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100427 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200428
429 /* only one TBF can poll on specific TS/FN, because scheduler can only
430 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100431 llist_for_each(pos, &m_dl_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200432 if (tbf_check(pos->entry(), fn, trx, ts))
433 return as_dl_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200434 }
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200435 return NULL;
436}
Max341dccd2017-08-14 15:49:59 +0200437
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200438gprs_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 +0200439{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100440 LListHead<gprs_rlcmac_tbf> *pos;
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200441
442 /* only one TBF can poll on specific TS/FN, because scheduler can only
443 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100444 llist_for_each(pos, &m_ul_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200445 if (tbf_check(pos->entry(), fn, trx, ts))
446 return as_ul_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200447 }
448 return NULL;
449}
450
Daniel Willmann54044b02014-07-02 17:58:15 +0200451/* lookup downlink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200452gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200453{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200454 if (trx >= 8 || ts >= 8)
455 return NULL;
456
457 return m_bts.trx[trx].pdch[ts].dl_tbf_by_tfi(tfi);
Daniel Willmann54044b02014-07-02 17:58:15 +0200458}
459
460/* lookup uplink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200461gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200462{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200463 if (trx >= 8 || ts >= 8)
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200464 return NULL;
465
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200466 return m_bts.trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200467}
468
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200469/*
470 * Search for free TFI and return TFI, TRX.
471 * This method returns the first TFI that is currently not used in any PDCH of
472 * a TRX. The first TRX that contains such an TFI is returned. Negative values
473 * indicate errors.
474 */
Maxa76a7d02018-01-26 11:09:16 +0100475int 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 +0200476{
Maxa76a7d02018-01-26 11:09:16 +0100477 const struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200478 uint32_t free_tfis;
479 bool has_pdch = false;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200480 uint8_t trx_from, trx_to, trx, ts, tfi;
481
482 if (use_trx >= 0 && use_trx < 8)
483 trx_from = trx_to = use_trx;
484 else {
485 trx_from = 0;
486 trx_to = 7;
487 }
488
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200489 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200490 for (trx = trx_from; trx <= trx_to; trx++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200491 bool trx_has_pdch = false;
492
Maxd000d802017-09-20 17:55:28 +0200493 free_tfis = NO_FREE_TFI;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200494
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200495 for (ts = 0; ts < 8; ts++) {
496 pdch = &m_bts.trx[trx].pdch[ts];
497 if (!pdch->is_enabled())
498 continue;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200499 free_tfis &= ~pdch->assigned_tfi(dir);
500 trx_has_pdch = true;
501 has_pdch = true;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200502 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200503 if (trx_has_pdch && free_tfis)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200504 break;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200505
506 free_tfis = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200507 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200508 if (!has_pdch) {
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200509 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
510 return -EINVAL;
511 }
512
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200513 if (!free_tfis) {
Max69d585e2018-01-26 12:36:43 +0100514 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200515 return -EBUSY;
516 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200517
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200518
519 LOGP(DRLCMAC, LOGL_DEBUG,
520 "Searching for first unallocated TFI: TRX=%d\n", trx);
521
522 /* find the first */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200523 for (tfi = 0; tfi < 32; tfi++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200524 if (free_tfis & 1 << tfi)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200525 break;
526 }
527
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200528 OSMO_ASSERT(tfi < 32);
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200529
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200530 LOGP(DRLCMAC, LOGL_DEBUG, " Found TFI=%d.\n", tfi);
531 *_trx = trx;
532 return tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200533}
534
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200535int BTS::rcv_imm_ass_cnf(const uint8_t *data, uint32_t fn)
536{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200537 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200538 uint8_t plen;
539 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200540 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200541
542 /* move to IA Rest Octets */
543 plen = data[0] >> 2;
544 data += 1 + plen;
545
546 if ((*data & 0xf0) != 0xd0) {
547 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
548 "octets do not start with bit sequence 'HH01' "
549 "(Packet Downlink Assignment)\n");
550 return -EINVAL;
551 }
552
553 /* get TLLI from downlink assignment */
Alexander Couzensfba931b2019-07-10 21:03:23 +0200554 tlli = ((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200555 tlli |= (*data++) << 20;
556 tlli |= (*data++) << 12;
557 tlli |= (*data++) << 4;
558 tlli |= (*data++) >> 4;
559
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200560 ms = ms_by_tlli(tlli);
561 if (ms)
562 dl_tbf = ms->dl_tbf();
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200563 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200564 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
565 "does not exit\n", tlli);
566 return -EINVAL;
567 }
568
569 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
570
Daniel Willmann7e994e32014-08-07 15:49:21 +0200571 if (dl_tbf->m_wait_confirm)
Max467f6332017-12-20 18:13:29 +0100572 T_START(dl_tbf, T0, 0, T_ASS_AGCH_USEC, "assignment (AGCH)", true);
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200573
574 return 0;
575}
576
Philipp Maier1275a3f2017-02-21 19:35:23 +0100577/* Determine the full frame number from a relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100578uint32_t BTS::rfn_to_fn(int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100579{
Max5dd8d1b2017-03-08 12:06:42 +0100580 int32_t m_cur_rfn;
581 int32_t fn;
582 int32_t fn_rounded;
583
584 /* double-check that relative FN is not negative and fits into int32_t */
585 OSMO_ASSERT(rfn < GSM_MAX_FN);
586 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100587
588 /* Note: If a BTS is sending in a rach request it will be fully aware
589 * of the frame number. If the PCU is used in a BSC-co-located setup.
590 * The BSC will forward the incoming RACH request. The RACH request
591 * only contains the relative frame number (Fn % 42432) in its request
592 * reference. This PCU implementation has to fit both scenarios, so
593 * we need to assume that Fn is a relative frame number. */
594
595 /* Ensure that all following calculations are performed with the
596 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100597 if (rfn >= RFN_MODULUS)
598 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100599
600 /* Compute an internal relative frame number from the full internal
601 frame number */
602 m_cur_rfn = m_cur_fn % RFN_MODULUS;
603
604 /* Compute a "rounded" version of the internal frame number, which
605 * exactly fits in the RFN_MODULUS raster */
606 fn_rounded = m_cur_fn - m_cur_rfn;
607
608 /* If the delta between the internal and the external relative frame
609 * number exceeds a certain limit, we need to assume that the incoming
610 * rach request belongs to a the previous rfn period. To correct this,
611 * we roll back the rounded frame number by one RFN_MODULUS */
612 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
613 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200614 "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 +0100615 rfn, m_cur_fn, RFN_MODULUS);
616 if (fn_rounded < RFN_MODULUS) {
617 LOGP(DRLCMAC, LOGL_DEBUG,
618 "Cornercase detected: wrapping crosses %u border\n",
619 GSM_MAX_FN);
620 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
621 }
622 else
623 fn_rounded -= RFN_MODULUS;
624 }
625
626 /* The real frame number is the sum of the rounded frame number and the
627 * relative framenumber computed via RACH */
628 fn = fn_rounded + rfn;
629
630 return fn;
631}
632
Max58818582018-01-05 15:04:50 +0100633static inline uint16_t mslot_class_from_ra(uint16_t ra, bool is_11bit)
634{
635 if (is_11bit)
636 return (ra & 0x3e0) >> 5;
637
638 /* set multislot class to 0 for 8-bit RACH, since we don't know it yet */
639 return 0;
640}
641
642static inline uint16_t priority_from_ra(uint16_t ra, bool is_11bit)
643{
644 if (is_11bit)
645 return (ra & 0x18) >> 3;
646
647 return 0;
648}
649
650static inline bool is_single_block(bool force_two_phase, uint16_t ra, enum ph_burst_type burst_type, bool is_11bit)
651{
652 bool sb = false;
653
654 if ((ra & 0xf8) == 0x70)
655 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
656 else if (force_two_phase)
657 LOGP(DRLCMAC, LOGL_DEBUG,
658 "MS requests single phase access, but we force two phase access [RACH is %s bit]\n",
659 is_11bit ? "11" : "8");
660
Max137fd592018-01-05 15:53:08 +0100661 switch(burst_type) {
662 case GSM_L1_BURST_TYPE_ACCESS_0:
663 if (is_11bit) {
664 LOGP(DRLCMAC, LOGL_ERROR, "Error: GPRS 11 bit RACH not supported\n");
665 return false;
666 }
Max58818582018-01-05 15:04:50 +0100667
668 if ((ra & 0xf8) == 0x70)
Max137fd592018-01-05 15:53:08 +0100669 return true;
670
671 if (force_two_phase)
672 return true;
673 break;
674 case GSM_L1_BURST_TYPE_ACCESS_1: /* deliberate fall-through */
675 case GSM_L1_BURST_TYPE_ACCESS_2:
676 if (is_11bit) {
677 if (!(ra & (1 << 10))) {
678 if (force_two_phase)
679 return true;
680
681 return false;
682 }
683
684 return true;
Max58818582018-01-05 15:04:50 +0100685 }
Max137fd592018-01-05 15:53:08 +0100686 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for 8-bit RACH\n", burst_type);
687 break;
688 case GSM_L1_BURST_TYPE_NONE:
689 LOGP(DRLCMAC, LOGL_ERROR, "PCU has not received burst type from BTS\n");
690 break;
691 default:
692 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for %s-bit RACH\n",
693 burst_type, is_11bit ? "11" : "8");
Max58818582018-01-05 15:04:50 +0100694 }
695
696 return sb;
697}
698
699int BTS::rcv_rach(uint16_t ra, uint32_t Fn, int16_t qta, bool is_11bit,
bhargava959d1de2016-08-17 15:17:21 +0530700 enum ph_burst_type burst_type)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200701{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200702 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Holger Hans Peter Freytherf37e5142013-12-25 18:56:34 +0100703 uint8_t trx_no, ts_no = 0;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200704 uint8_t sb = 0;
705 uint32_t sb_fn = 0;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530706 int rc = 0;
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100707 int plen;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100708 uint8_t usf = 7;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530709 uint8_t tsc = 0, ta = qta2ta(qta);
Max58818582018-01-05 15:04:50 +0100710 uint16_t ms_class = mslot_class_from_ra(ra, is_11bit);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530711 bool failure = false;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200712
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100713 rach_frame();
714
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530715 if (is_11bit)
716 rach_frame_11bit();
717
Philipp Maier1275a3f2017-02-21 19:35:23 +0100718 /* Determine full frame number */
719 Fn = rfn_to_fn(Fn);
Philippd935d882016-11-07 13:07:36 +0100720
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530721 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF on RACH, "
Philipp Maier53f0b4d2017-01-26 11:39:16 +0100722 "so we provide one: ra=0x%02x Fn=%u qta=%d is_11bit=%d:\n",
723 ra, Fn, qta, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530724
Max58818582018-01-05 15:04:50 +0100725 sb = is_single_block(m_bts.force_two_phase, ra, burst_type, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530726
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200727 if (sb) {
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100728 rc = sba()->alloc(&trx_no, &ts_no, &sb_fn, ta);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530729 if (rc < 0) {
730 failure = true;
731 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
732 "single block allocation."
733 "sending Immediate "
734 "Assignment Uplink (AGCH) reject\n");
735 } else {
736 tsc = m_bts.trx[trx_no].pdch[ts_no].tsc;
737
738 LOGP(DRLCMAC, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
739 " qbit-ta=%d ra=0x%02x, Fn=%d (%d,%d,%d),"
740 " SBFn=%d\n",
741 qta, ra,
742 Fn, (Fn / (26 * 51)) % 32, Fn % 51, Fn % 26,
743 sb_fn);
744 LOGP(DRLCMAC, LOGL_INFO, "TX: Immediate Assignment "
745 "Uplink (AGCH)\n");
746 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200747 } else {
748 // Create new TBF
Max5a6bcfb2017-09-01 14:36:44 +0200749 /* FIXME: Copy and paste with other routines.. */
bhargava628dcfb2016-09-14 20:30:31 +0530750
Max92b7a502018-01-26 11:01:35 +0100751 tbf = tbf_alloc_ul_tbf(&m_bts, NULL, -1, 0, ms_class, true);
bhargava628dcfb2016-09-14 20:30:31 +0530752
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200753 if (!tbf) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530754 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource sending "
755 "Immediate Assignment Uplink (AGCH) "
756 "reject\n");
757 rc = -EBUSY;
758 failure = true;
759 } else {
760 tbf->set_ta(ta);
Max2399b1d2018-01-12 15:48:12 +0100761 TBF_SET_STATE(tbf, GPRS_RLCMAC_FLOW);
Max5d7f7572018-01-30 16:56:43 +0100762 TBF_ASS_TYPE_SET(tbf, GPRS_RLCMAC_FLAG_CCCH);
Max467f6332017-12-20 18:13:29 +0100763 T_START(tbf, T3169, m_bts.t3169, 0, "RACH (new UL-TBF)", true);
Maxc21f0072017-12-15 17:36:45 +0100764 LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] START\n");
765 LOGPTBF(tbf, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530766 "qbit-ta=%d ra=0x%02x, Fn=%d "
767 " (%d,%d,%d)\n",
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530768 qta, ra, Fn, (Fn / (26 * 51)) % 32,
769 Fn % 51, Fn % 26);
Maxc21f0072017-12-15 17:36:45 +0100770 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Uplink (AGCH)\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530771 trx_no = tbf->trx->trx_no;
772 ts_no = tbf->first_ts;
773 usf = tbf->m_usf[ts_no];
774 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200775 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200776 }
Alexander Couzensccde5c92017-02-04 03:10:08 +0100777 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx) /* without plen */;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200778 bitvec_unhex(immediate_assignment,
779 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100780
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100781
sivasankari168911b2016-11-25 19:53:36 +0530782 if (failure) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530783 plen = Encoding::write_immediate_assignment_reject(
784 immediate_assignment, ra, Fn,
785 burst_type);
sivasankari168911b2016-11-25 19:53:36 +0530786 immediate_assignment_reject();
787 }
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530788 else {
789 LOGP(DRLCMAC, LOGL_DEBUG,
790 " - TRX=%d (%d) TS=%d TA=%d TSC=%d TFI=%d USF=%d\n",
791 trx_no, m_bts.trx[trx_no].arfcn, ts_no, ta, tsc,
792 tbf ? tbf->tfi() : -1, usf);
Maxfc8afc22019-02-18 18:52:38 +0100793 // N. B: if tbf == NULL then SBA is used for Imm. Ass. below
794 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, false, ra, Fn, ta,
795 m_bts.trx[trx_no].arfcn, ts_no, tsc, usf, false, sb_fn,
796 m_bts.alpha, m_bts.gamma, -1, burst_type);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530797 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100798
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530799 if (plen >= 0) {
800 immediate_assignment_ul_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100801 pcu_l1if_tx_agch(immediate_assignment, plen);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530802 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100803
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200804 bitvec_free(immediate_assignment);
805
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530806 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200807}
808
Maxfc8afc22019-02-18 18:52:38 +0100809void BTS::snd_dl_ass(gprs_rlcmac_tbf *tbf, bool poll, const char *imsi)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200810{
811 int plen;
Jacob Erlbeck29e3a2f2016-02-05 19:04:23 +0100812 unsigned int ts = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200813
Maxc21f0072017-12-15 17:36:45 +0100814 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +0100815 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200816 bitvec_unhex(immediate_assignment, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
817 /* use request reference that has maximum distance to current time,
818 * so the assignment will not conflict with possible RACH requests. */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100819 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d pollFN=%d\n",
820 tbf->trx->trx_no, tbf->trx->arfcn,
821 ts, tbf->ta(), poll ? tbf->poll_fn : -1);
Maxfc8afc22019-02-18 18:52:38 +0100822 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, true, 125,
823 (tbf->pdch[ts]->last_rts_fn + 21216) % GSM_MAX_FN, tbf->ta(),
824 tbf->trx->arfcn, ts, tbf->tsc(), 7, poll,
825 tbf->poll_fn, m_bts.alpha, m_bts.gamma, -1,
826 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530827 if (plen >= 0) {
828 immediate_assignment_dl_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100829 pcu_l1if_tx_pch(immediate_assignment, plen, imsi);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530830 }
831
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200832 bitvec_free(immediate_assignment);
833}
834
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200835
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100836GprsMs *BTS::ms_alloc(uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200837{
838 GprsMs *ms;
839 ms = ms_store().create_ms();
840
841 ms->set_timeout(m_bts.ms_idle_sec);
842 ms->set_ms_class(ms_class);
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100843 ms->set_egprs_ms_class(egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200844
845 return ms;
846}
847
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400848/* update TA based on TA provided by PH-DATA-IND */
849void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
850{
851 int16_t ta_adj;
852 uint8_t ta_target;
853
854 if (ta_delta) {
855 /* adjust TA based on TA provided by PH-DATA-IND */
856 ta_adj = tbf->ta() + ta_delta;
857
858 /* limit target TA in range 0..63 bits */
859 ta_target = ta_limit(ta_adj);
860
861 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating TLLI=0x%08x: TA %u -> %u on "
862 "TRX = %d, TS = %d, FN = %d\n",
863 tbf->tlli(), tbf->ta(), ta_target,
864 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
865 tbf->set_ta(ta_target);
866 }
867}
868
869/* set TA based on TA provided by PH-RA-IND */
870void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
871{
872 uint8_t ta_target;
873
874 if (tbf->ta() != ta) {
875 /* limit target TA in range 0..63 bits */
876 ta_target = ta_limit(ta);
877
878 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating TLLI=0x%08x: TA %u -> %u on "
879 "TRX = %d, TS = %d, FN = %d\n",
880 tbf->tlli(), tbf->ta(), ta_target,
881 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
882 tbf->set_ta(ta_target);
883 }
884}
885
886void 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 +0200887{
888 struct gprs_rlcmac_ul_tbf *tbf =
889 bts_main_data()->bts->ul_tbf_by_poll_fn(fn, trx_no, ts);
890 if (!tbf)
891 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
892 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
893 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400894 else {
895 /* we need to distinguish TA information provided by L1
896 * from PH-DATA-IND and PHY-RA-IND so that we can properly
897 * update TA for given TBF
898 */
899 if (is_rach)
900 set_tbf_ta(tbf, (uint8_t)ta);
901 else
902 update_tbf_ta(tbf, ta);
903
Maxd71e8b32016-09-19 16:17:06 +0200904 }
905}
906
Jacob Erlbeck23f93a12015-06-30 08:52:54 +0200907void gprs_rlcmac_trx::reserve_slots(enum gprs_rlcmac_tbf_direction dir,
908 uint8_t slots)
909{
910 unsigned i;
911 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
912 if (slots & (1 << i))
913 pdch[i].reserve(dir);
914}
915
916void gprs_rlcmac_trx::unreserve_slots(enum gprs_rlcmac_tbf_direction dir,
917 uint8_t slots)
918{
919 unsigned i;
920 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
921 if (slots & (1 << i))
922 pdch[i].unreserve(dir);
923}