blob: 60f74dd6ce5f9d1dbc7bd9b16859ddc0c405d018 [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;
Oliver Smith45fdc442019-09-05 15:12:20 +0200219 m_bts.dl_tbf_preemptive_retransmission = true;
Holger Hans Peter Freyther4ed1dae2013-10-20 10:14:03 +0200220
221 /* initialize back pointers */
222 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(m_bts.trx); ++trx_no) {
223 struct gprs_rlcmac_trx *trx = &m_bts.trx[trx_no];
224 trx->trx_no = trx_no;
225 trx->bts = this;
226
227 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
228 struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
229 pdch->ts_no = ts_no;
230 pdch->trx = trx;
231 }
232 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100233
Maxea9968f2017-12-18 22:52:52 +0100234 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100235 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100236 than just allocate the group with different index.
237 This shall be removed once weget rid of BTS singleton */
238 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
239 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
240 else
241 m_ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200242 OSMO_ASSERT(m_ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100243
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100244 m_statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
Harald Welte9530a402017-07-12 00:46:36 +0200245 OSMO_ASSERT(m_statg);
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200246}
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200247
Maxd5ffeb52019-03-18 15:48:38 +0100248void BTS::cleanup()
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100249{
Jacob Erlbeckc362df22016-01-20 22:02:19 +0100250 /* this can cause counter updates and must not be left to the
251 * m_ms_store's destructor */
252 m_ms_store.cleanup();
253
Maxd5ffeb52019-03-18 15:48:38 +0100254 if (m_ratectrs) {
255 rate_ctr_group_free(m_ratectrs);
256 m_ratectrs = NULL;
257 }
258
259 if (m_statg) {
260 osmo_stat_item_group_free(m_statg);
261 m_statg = NULL;
262 }
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100263}
264
Maxd5ffeb52019-03-18 15:48:38 +0100265BTS::~BTS()
266{
267 cleanup();
268}
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100269
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200270void BTS::set_current_frame_number(int fn)
271{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200272 /* The UL frame numbers lag 3 behind the DL frames and the data
273 * indication is only sent after all 4 frames of the block have been
274 * received. Sometimes there is an idle frame between the end of one
275 * and start of another frame (every 3 blocks). So the timeout should
276 * definitely be there if we're more than 8 frames past poll_fn. Let's
Jacob Erlbecke77d49f2015-08-27 13:28:05 +0200277 * stay on the safe side and say 13 or more. An additional delay can
278 * happen due to the block processing time in the DSP, so the delay of
279 * decoded blocks relative to the timing clock can be much larger.
280 * Values up to 50 frames have been observed under load. */
281 const static int max_delay = 60;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200282
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200283 m_cur_fn = fn;
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200284 m_pollController.expireTimedout(m_cur_fn, max_delay);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200285}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200286
Max9dabfa22017-05-16 16:10:45 +0200287static inline int delta_fn(int fn, int to)
288{
289 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
290}
291
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200292void BTS::set_current_block_frame_number(int fn, unsigned max_delay)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200293{
294 int delay = 0;
295 const int late_block_delay_thresh = 13;
296 const int fn_update_ok_min_delay = -500;
297 const int fn_update_ok_max_delay = 0;
298
299 /* frame numbers in the received blocks are assumed to be strongly
300 * monotonic. */
301 if (m_cur_blk_fn >= 0) {
Max9dabfa22017-05-16 16:10:45 +0200302 int delta = delta_fn(fn, m_cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200303 if (delta <= 0)
304 return;
305 }
306
307 /* Check block delay vs. the current frame number */
308 if (current_frame_number() != 0)
Max9dabfa22017-05-16 16:10:45 +0200309 delay = delta_fn(fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200310 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200311 LOGP(DRLCMAC, LOGL_NOTICE,
312 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
313 delay, fn, current_frame_number());
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200314 rlc_late_block();
315 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200316
317 m_cur_blk_fn = fn;
318 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
319 current_frame_number() == 0)
320 m_cur_fn = fn;
321
Jacob Erlbeckaf75ce82015-08-26 13:22:28 +0200322 m_pollController.expireTimedout(fn, max_delay);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200323}
324
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200325int BTS::add_paging(uint8_t chan_needed, uint8_t *identity_lv)
326{
327 uint8_t l, trx, ts, any_tbf = 0;
328 struct gprs_rlcmac_tbf *tbf;
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100329 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200330 uint8_t slot_mask[8];
331 int8_t first_ts; /* must be signed */
332
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100333 LListHead<gprs_rlcmac_tbf> *tbfs_lists[] = {
334 &m_ul_tbfs,
335 &m_dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200336 NULL
337 };
338
339
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200340 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n",
341 chan_needed, osmo_hexdump(identity_lv + 1, identity_lv[0]));
342
343 /* collect slots to page
344 * Mark slots for every TBF, but only mark one of it.
345 * Mark only the first slot found.
346 * Don't mark, if TBF uses a different slot that is already marked. */
347 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200348 for (l = 0; tbfs_lists[l]; l++) {
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100349 llist_for_each(pos, tbfs_lists[l]) {
350 tbf = pos->entry();
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200351 first_ts = -1;
352 for (ts = 0; ts < 8; ts++) {
353 if (tbf->pdch[ts]) {
354 /* remember the first slot found */
355 if (first_ts < 0)
356 first_ts = ts;
357 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100358 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200359 break;
360 }
361 }
362 /* mark first slot found, if none is marked already */
363 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100364 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200365 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100366 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100367 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200368 } else
Maxc21f0072017-12-15 17:36:45 +0100369 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200370 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100371 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200372 }
373 }
374
375 /* Now we have a list of marked slots. Every TBF uses at least one
376 * of these slots. */
377
378 /* schedule paging to all marked slots */
379 for (trx = 0; trx < 8; trx++) {
380 if (slot_mask[trx] == 0)
381 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200382 for (ts = 0; ts < 8; ts++) {
383 if ((slot_mask[trx] & (1 << ts))) {
384 /* schedule */
Max4382e4e2018-01-30 12:00:08 +0100385 if (!m_bts.trx[trx].pdch[ts].add_paging(chan_needed, identity_lv))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200386 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100387
388 LOGP(DRLCMAC, LOGL_INFO, "Paging on PACCH of TRX=%d TS=%d\n", trx, ts);
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200389 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200390 }
391 }
392 }
393
394 if (!any_tbf)
395 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
396
397 return 0;
398}
399
Harald Welte717cdf52017-07-21 21:56:23 +0200400void BTS::send_gsmtap(enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
401 uint8_t ts_no, uint8_t channel, uint32_t fn,
402 const uint8_t *data, unsigned int len)
403{
404 uint16_t arfcn;
405
406 /* check if category is activated at all */
407 if (!(m_bts.gsmtap_categ_mask & (1 << categ)))
408 return;
409
410 arfcn = m_bts.trx[trx_no].arfcn;
411 if (uplink)
412 arfcn |= GSMTAP_ARFCN_F_UPLINK;
413
414 gsmtap_send(m_bts.gsmtap, arfcn, ts_no, channel, 0, fn, 0, 0, data, len);
415}
416
Max341dccd2017-08-14 15:49:59 +0200417static inline bool tbf_check(gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t trx_no, uint8_t ts)
418{
Maxcac6b662018-01-24 11:00:17 +0100419 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING) && tbf->poll_scheduled()
Max341dccd2017-08-14 15:49:59 +0200420 && tbf->poll_fn == fn && tbf->trx->trx_no == trx_no && tbf->poll_ts == ts)
421 return true;
422
423 return false;
424}
425
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200426gprs_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 +0200427{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100428 LListHead<gprs_rlcmac_tbf> *pos;
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200429
430 /* only one TBF can poll on specific TS/FN, because scheduler can only
431 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100432 llist_for_each(pos, &m_dl_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200433 if (tbf_check(pos->entry(), fn, trx, ts))
434 return as_dl_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200435 }
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200436 return NULL;
437}
Max341dccd2017-08-14 15:49:59 +0200438
Daniel Willmannfe6e2e42014-07-10 17:44:06 +0200439gprs_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 +0200440{
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100441 LListHead<gprs_rlcmac_tbf> *pos;
Daniel Willmannfebf1a02014-07-02 17:58:16 +0200442
443 /* only one TBF can poll on specific TS/FN, because scheduler can only
444 * schedule one downlink control block (with polling) at a FN per TS */
Jacob Erlbecked2dbf62015-12-28 19:15:40 +0100445 llist_for_each(pos, &m_ul_tbfs) {
Max341dccd2017-08-14 15:49:59 +0200446 if (tbf_check(pos->entry(), fn, trx, ts))
447 return as_ul_tbf(pos->entry());
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200448 }
449 return NULL;
450}
451
Daniel Willmann54044b02014-07-02 17:58:15 +0200452/* lookup downlink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200453gprs_rlcmac_dl_tbf *BTS::dl_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200454{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200455 if (trx >= 8 || ts >= 8)
456 return NULL;
457
458 return m_bts.trx[trx].pdch[ts].dl_tbf_by_tfi(tfi);
Daniel Willmann54044b02014-07-02 17:58:15 +0200459}
460
461/* lookup uplink TBF Entity (by TFI) */
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200462gprs_rlcmac_ul_tbf *BTS::ul_tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts)
Daniel Willmann54044b02014-07-02 17:58:15 +0200463{
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200464 if (trx >= 8 || ts >= 8)
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200465 return NULL;
466
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200467 return m_bts.trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200468}
469
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200470/*
471 * Search for free TFI and return TFI, TRX.
472 * This method returns the first TFI that is currently not used in any PDCH of
473 * a TRX. The first TRX that contains such an TFI is returned. Negative values
474 * indicate errors.
475 */
Maxa76a7d02018-01-26 11:09:16 +0100476int 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 +0200477{
Maxa76a7d02018-01-26 11:09:16 +0100478 const struct gprs_rlcmac_pdch *pdch;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200479 uint32_t free_tfis;
480 bool has_pdch = false;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200481 uint8_t trx_from, trx_to, trx, ts, tfi;
482
483 if (use_trx >= 0 && use_trx < 8)
484 trx_from = trx_to = use_trx;
485 else {
486 trx_from = 0;
487 trx_to = 7;
488 }
489
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200490 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200491 for (trx = trx_from; trx <= trx_to; trx++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200492 bool trx_has_pdch = false;
493
Maxd000d802017-09-20 17:55:28 +0200494 free_tfis = NO_FREE_TFI;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200495
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200496 for (ts = 0; ts < 8; ts++) {
497 pdch = &m_bts.trx[trx].pdch[ts];
498 if (!pdch->is_enabled())
499 continue;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200500 free_tfis &= ~pdch->assigned_tfi(dir);
501 trx_has_pdch = true;
502 has_pdch = true;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200503 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200504 if (trx_has_pdch && free_tfis)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200505 break;
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200506
507 free_tfis = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200508 }
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200509 if (!has_pdch) {
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200510 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
511 return -EINVAL;
512 }
513
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200514 if (!free_tfis) {
Max69d585e2018-01-26 12:36:43 +0100515 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200516 return -EBUSY;
517 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200518
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200519
520 LOGP(DRLCMAC, LOGL_DEBUG,
521 "Searching for first unallocated TFI: TRX=%d\n", trx);
522
523 /* find the first */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200524 for (tfi = 0; tfi < 32; tfi++) {
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200525 if (free_tfis & 1 << tfi)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200526 break;
527 }
528
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200529 OSMO_ASSERT(tfi < 32);
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200530
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200531 LOGP(DRLCMAC, LOGL_DEBUG, " Found TFI=%d.\n", tfi);
532 *_trx = trx;
533 return tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200534}
535
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200536int BTS::rcv_imm_ass_cnf(const uint8_t *data, uint32_t fn)
537{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200538 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200539 uint8_t plen;
540 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200541 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200542
543 /* move to IA Rest Octets */
544 plen = data[0] >> 2;
545 data += 1 + plen;
546
547 if ((*data & 0xf0) != 0xd0) {
548 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
549 "octets do not start with bit sequence 'HH01' "
550 "(Packet Downlink Assignment)\n");
551 return -EINVAL;
552 }
553
554 /* get TLLI from downlink assignment */
Eric Wildab8b01e2019-07-23 17:16:06 +0200555 tlli = (uint32_t)((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200556 tlli |= (*data++) << 20;
557 tlli |= (*data++) << 12;
558 tlli |= (*data++) << 4;
559 tlli |= (*data++) >> 4;
560
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200561 ms = ms_by_tlli(tlli);
562 if (ms)
563 dl_tbf = ms->dl_tbf();
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200564 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200565 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
566 "does not exit\n", tlli);
567 return -EINVAL;
568 }
569
570 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
571
Daniel Willmann7e994e32014-08-07 15:49:21 +0200572 if (dl_tbf->m_wait_confirm)
Max467f6332017-12-20 18:13:29 +0100573 T_START(dl_tbf, T0, 0, T_ASS_AGCH_USEC, "assignment (AGCH)", true);
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200574
575 return 0;
576}
577
Philipp Maier1275a3f2017-02-21 19:35:23 +0100578/* Determine the full frame number from a relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100579uint32_t BTS::rfn_to_fn(int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100580{
Max5dd8d1b2017-03-08 12:06:42 +0100581 int32_t m_cur_rfn;
582 int32_t fn;
583 int32_t fn_rounded;
584
585 /* double-check that relative FN is not negative and fits into int32_t */
586 OSMO_ASSERT(rfn < GSM_MAX_FN);
587 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100588
589 /* Note: If a BTS is sending in a rach request it will be fully aware
590 * of the frame number. If the PCU is used in a BSC-co-located setup.
591 * The BSC will forward the incoming RACH request. The RACH request
592 * only contains the relative frame number (Fn % 42432) in its request
593 * reference. This PCU implementation has to fit both scenarios, so
594 * we need to assume that Fn is a relative frame number. */
595
596 /* Ensure that all following calculations are performed with the
597 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100598 if (rfn >= RFN_MODULUS)
599 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100600
601 /* Compute an internal relative frame number from the full internal
602 frame number */
603 m_cur_rfn = m_cur_fn % RFN_MODULUS;
604
605 /* Compute a "rounded" version of the internal frame number, which
606 * exactly fits in the RFN_MODULUS raster */
607 fn_rounded = m_cur_fn - m_cur_rfn;
608
609 /* If the delta between the internal and the external relative frame
610 * number exceeds a certain limit, we need to assume that the incoming
611 * rach request belongs to a the previous rfn period. To correct this,
612 * we roll back the rounded frame number by one RFN_MODULUS */
613 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
614 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200615 "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 +0100616 rfn, m_cur_fn, RFN_MODULUS);
617 if (fn_rounded < RFN_MODULUS) {
618 LOGP(DRLCMAC, LOGL_DEBUG,
619 "Cornercase detected: wrapping crosses %u border\n",
620 GSM_MAX_FN);
621 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
622 }
623 else
624 fn_rounded -= RFN_MODULUS;
625 }
626
627 /* The real frame number is the sum of the rounded frame number and the
628 * relative framenumber computed via RACH */
629 fn = fn_rounded + rfn;
630
631 return fn;
632}
633
Max58818582018-01-05 15:04:50 +0100634static inline uint16_t mslot_class_from_ra(uint16_t ra, bool is_11bit)
635{
636 if (is_11bit)
637 return (ra & 0x3e0) >> 5;
638
639 /* set multislot class to 0 for 8-bit RACH, since we don't know it yet */
640 return 0;
641}
642
643static inline uint16_t priority_from_ra(uint16_t ra, bool is_11bit)
644{
645 if (is_11bit)
646 return (ra & 0x18) >> 3;
647
648 return 0;
649}
650
651static inline bool is_single_block(bool force_two_phase, uint16_t ra, enum ph_burst_type burst_type, bool is_11bit)
652{
653 bool sb = false;
654
655 if ((ra & 0xf8) == 0x70)
656 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
657 else if (force_two_phase)
658 LOGP(DRLCMAC, LOGL_DEBUG,
659 "MS requests single phase access, but we force two phase access [RACH is %s bit]\n",
660 is_11bit ? "11" : "8");
661
Max137fd592018-01-05 15:53:08 +0100662 switch(burst_type) {
663 case GSM_L1_BURST_TYPE_ACCESS_0:
664 if (is_11bit) {
665 LOGP(DRLCMAC, LOGL_ERROR, "Error: GPRS 11 bit RACH not supported\n");
666 return false;
667 }
Max58818582018-01-05 15:04:50 +0100668
669 if ((ra & 0xf8) == 0x70)
Max137fd592018-01-05 15:53:08 +0100670 return true;
671
672 if (force_two_phase)
673 return true;
674 break;
675 case GSM_L1_BURST_TYPE_ACCESS_1: /* deliberate fall-through */
676 case GSM_L1_BURST_TYPE_ACCESS_2:
677 if (is_11bit) {
678 if (!(ra & (1 << 10))) {
679 if (force_two_phase)
680 return true;
681
682 return false;
683 }
684
685 return true;
Max58818582018-01-05 15:04:50 +0100686 }
Max137fd592018-01-05 15:53:08 +0100687 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for 8-bit RACH\n", burst_type);
688 break;
689 case GSM_L1_BURST_TYPE_NONE:
690 LOGP(DRLCMAC, LOGL_ERROR, "PCU has not received burst type from BTS\n");
691 break;
692 default:
693 LOGP(DRLCMAC, LOGL_ERROR, "Unexpected RACH burst type %u for %s-bit RACH\n",
694 burst_type, is_11bit ? "11" : "8");
Max58818582018-01-05 15:04:50 +0100695 }
696
697 return sb;
698}
699
700int BTS::rcv_rach(uint16_t ra, uint32_t Fn, int16_t qta, bool is_11bit,
bhargava959d1de2016-08-17 15:17:21 +0530701 enum ph_burst_type burst_type)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200702{
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200703 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Holger Hans Peter Freytherf37e5142013-12-25 18:56:34 +0100704 uint8_t trx_no, ts_no = 0;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200705 uint8_t sb = 0;
706 uint32_t sb_fn = 0;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530707 int rc = 0;
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100708 int plen;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100709 uint8_t usf = 7;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530710 uint8_t tsc = 0, ta = qta2ta(qta);
Max58818582018-01-05 15:04:50 +0100711 uint16_t ms_class = mslot_class_from_ra(ra, is_11bit);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530712 bool failure = false;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200713
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100714 rach_frame();
715
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530716 if (is_11bit)
717 rach_frame_11bit();
718
Philipp Maier1275a3f2017-02-21 19:35:23 +0100719 /* Determine full frame number */
720 Fn = rfn_to_fn(Fn);
Philippd935d882016-11-07 13:07:36 +0100721
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530722 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests UL TBF on RACH, "
Philipp Maier53f0b4d2017-01-26 11:39:16 +0100723 "so we provide one: ra=0x%02x Fn=%u qta=%d is_11bit=%d:\n",
724 ra, Fn, qta, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530725
Max58818582018-01-05 15:04:50 +0100726 sb = is_single_block(m_bts.force_two_phase, ra, burst_type, is_11bit);
bhargava628dcfb2016-09-14 20:30:31 +0530727
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200728 if (sb) {
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100729 rc = sba()->alloc(&trx_no, &ts_no, &sb_fn, ta);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530730 if (rc < 0) {
731 failure = true;
732 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
733 "single block allocation."
734 "sending Immediate "
735 "Assignment Uplink (AGCH) reject\n");
736 } else {
737 tsc = m_bts.trx[trx_no].pdch[ts_no].tsc;
738
739 LOGP(DRLCMAC, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
740 " qbit-ta=%d ra=0x%02x, Fn=%d (%d,%d,%d),"
741 " SBFn=%d\n",
742 qta, ra,
743 Fn, (Fn / (26 * 51)) % 32, Fn % 51, Fn % 26,
744 sb_fn);
745 LOGP(DRLCMAC, LOGL_INFO, "TX: Immediate Assignment "
746 "Uplink (AGCH)\n");
747 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200748 } else {
749 // Create new TBF
Max5a6bcfb2017-09-01 14:36:44 +0200750 /* FIXME: Copy and paste with other routines.. */
bhargava628dcfb2016-09-14 20:30:31 +0530751
Max92b7a502018-01-26 11:01:35 +0100752 tbf = tbf_alloc_ul_tbf(&m_bts, NULL, -1, 0, ms_class, true);
bhargava628dcfb2016-09-14 20:30:31 +0530753
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200754 if (!tbf) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530755 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource sending "
756 "Immediate Assignment Uplink (AGCH) "
757 "reject\n");
758 rc = -EBUSY;
759 failure = true;
760 } else {
761 tbf->set_ta(ta);
Max2399b1d2018-01-12 15:48:12 +0100762 TBF_SET_STATE(tbf, GPRS_RLCMAC_FLOW);
Max5d7f7572018-01-30 16:56:43 +0100763 TBF_ASS_TYPE_SET(tbf, GPRS_RLCMAC_FLAG_CCCH);
Max467f6332017-12-20 18:13:29 +0100764 T_START(tbf, T3169, m_bts.t3169, 0, "RACH (new UL-TBF)", true);
Maxc21f0072017-12-15 17:36:45 +0100765 LOGPTBF(tbf, LOGL_DEBUG, "[UPLINK] START\n");
766 LOGPTBF(tbf, LOGL_DEBUG, "RX: [PCU <- BTS] RACH "
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530767 "qbit-ta=%d ra=0x%02x, Fn=%d "
768 " (%d,%d,%d)\n",
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530769 qta, ra, Fn, (Fn / (26 * 51)) % 32,
770 Fn % 51, Fn % 26);
Maxc21f0072017-12-15 17:36:45 +0100771 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Uplink (AGCH)\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530772 trx_no = tbf->trx->trx_no;
773 ts_no = tbf->first_ts;
774 usf = tbf->m_usf[ts_no];
775 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200776 }
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200777 }
Alexander Couzensccde5c92017-02-04 03:10:08 +0100778 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx) /* without plen */;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200779 bitvec_unhex(immediate_assignment,
780 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100781
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100782
sivasankari168911b2016-11-25 19:53:36 +0530783 if (failure) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530784 plen = Encoding::write_immediate_assignment_reject(
785 immediate_assignment, ra, Fn,
786 burst_type);
sivasankari168911b2016-11-25 19:53:36 +0530787 immediate_assignment_reject();
788 }
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530789 else {
790 LOGP(DRLCMAC, LOGL_DEBUG,
791 " - TRX=%d (%d) TS=%d TA=%d TSC=%d TFI=%d USF=%d\n",
792 trx_no, m_bts.trx[trx_no].arfcn, ts_no, ta, tsc,
793 tbf ? tbf->tfi() : -1, usf);
Maxfc8afc22019-02-18 18:52:38 +0100794 // N. B: if tbf == NULL then SBA is used for Imm. Ass. below
795 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, false, ra, Fn, ta,
796 m_bts.trx[trx_no].arfcn, ts_no, tsc, usf, false, sb_fn,
797 m_bts.alpha, m_bts.gamma, -1, burst_type);
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530798 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100799
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530800 if (plen >= 0) {
801 immediate_assignment_ul_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100802 pcu_l1if_tx_agch(immediate_assignment, plen);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530803 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100804
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200805 bitvec_free(immediate_assignment);
806
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530807 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200808}
809
Maxfc8afc22019-02-18 18:52:38 +0100810void BTS::snd_dl_ass(gprs_rlcmac_tbf *tbf, bool poll, const char *imsi)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200811{
812 int plen;
Jacob Erlbeck29e3a2f2016-02-05 19:04:23 +0100813 unsigned int ts = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200814
Maxc21f0072017-12-15 17:36:45 +0100815 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +0100816 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200817 bitvec_unhex(immediate_assignment, "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
818 /* use request reference that has maximum distance to current time,
819 * so the assignment will not conflict with possible RACH requests. */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100820 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d pollFN=%d\n",
821 tbf->trx->trx_no, tbf->trx->arfcn,
822 ts, tbf->ta(), poll ? tbf->poll_fn : -1);
Maxfc8afc22019-02-18 18:52:38 +0100823 plen = Encoding::write_immediate_assignment(tbf, immediate_assignment, true, 125,
824 (tbf->pdch[ts]->last_rts_fn + 21216) % GSM_MAX_FN, tbf->ta(),
825 tbf->trx->arfcn, ts, tbf->tsc(), 7, poll,
826 tbf->poll_fn, m_bts.alpha, m_bts.gamma, -1,
827 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530828 if (plen >= 0) {
829 immediate_assignment_dl_tbf();
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100830 pcu_l1if_tx_pch(immediate_assignment, plen, imsi);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530831 }
832
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200833 bitvec_free(immediate_assignment);
834}
835
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200836
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100837GprsMs *BTS::ms_alloc(uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200838{
839 GprsMs *ms;
840 ms = ms_store().create_ms();
841
842 ms->set_timeout(m_bts.ms_idle_sec);
843 ms->set_ms_class(ms_class);
Jacob Erlbeck86b6f052015-11-27 15:17:34 +0100844 ms->set_egprs_ms_class(egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +0200845
846 return ms;
847}
848
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400849/* update TA based on TA provided by PH-DATA-IND */
850void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
851{
852 int16_t ta_adj;
853 uint8_t ta_target;
854
855 if (ta_delta) {
856 /* adjust TA based on TA provided by PH-DATA-IND */
857 ta_adj = tbf->ta() + ta_delta;
858
859 /* limit target TA in range 0..63 bits */
860 ta_target = ta_limit(ta_adj);
861
862 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating TLLI=0x%08x: TA %u -> %u on "
863 "TRX = %d, TS = %d, FN = %d\n",
864 tbf->tlli(), tbf->ta(), ta_target,
865 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
866 tbf->set_ta(ta_target);
867 }
868}
869
870/* set TA based on TA provided by PH-RA-IND */
871void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
872{
873 uint8_t ta_target;
874
875 if (tbf->ta() != ta) {
876 /* limit target TA in range 0..63 bits */
877 ta_target = ta_limit(ta);
878
879 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating TLLI=0x%08x: TA %u -> %u on "
880 "TRX = %d, TS = %d, FN = %d\n",
881 tbf->tlli(), tbf->ta(), ta_target,
882 tbf->trx->trx_no , tbf->poll_ts, tbf->poll_fn);
883 tbf->set_ta(ta_target);
884 }
885}
886
887void 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 +0200888{
889 struct gprs_rlcmac_ul_tbf *tbf =
890 bts_main_data()->bts->ul_tbf_by_poll_fn(fn, trx_no, ts);
891 if (!tbf)
892 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
893 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
894 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -0400895 else {
896 /* we need to distinguish TA information provided by L1
897 * from PH-DATA-IND and PHY-RA-IND so that we can properly
898 * update TA for given TBF
899 */
900 if (is_rach)
901 set_tbf_ta(tbf, (uint8_t)ta);
902 else
903 update_tbf_ta(tbf, ta);
904
Maxd71e8b32016-09-19 16:17:06 +0200905 }
906}
907
Jacob Erlbeck23f93a12015-06-30 08:52:54 +0200908void gprs_rlcmac_trx::reserve_slots(enum gprs_rlcmac_tbf_direction dir,
909 uint8_t slots)
910{
911 unsigned i;
912 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
913 if (slots & (1 << i))
914 pdch[i].reserve(dir);
915}
916
917void gprs_rlcmac_trx::unreserve_slots(enum gprs_rlcmac_tbf_direction dir,
918 uint8_t slots)
919{
920 unsigned i;
921 for (i = 0; i < ARRAY_SIZE(pdch); i += 1)
922 if (slots & (1 << i))
923 pdch[i].unreserve(dir);
924}