blob: 5769c2f54f559a607a34c61110fa711eec5ecef0 [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 Freyther17b0d832013-10-19 17:37:48 +020022#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020023#include <tbf_ul.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>
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010033#include <gprs_ms_storage.h>
34#include <sba.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020035
36extern "C" {
37 #include <osmocom/core/talloc.h>
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020038 #include <osmocom/core/msgb.h>
Holger Hans Peter Freyther8df447d2015-11-07 21:04:40 +010039 #include <osmocom/core/stats.h>
Max9bbe1602016-07-18 12:50:18 +020040 #include <osmocom/gsm/protocol/gsm_04_08.h>
Philipp Maier1275a3f2017-02-21 19:35:23 +010041 #include <osmocom/gsm/gsm_utils.h>
Pau Espin Pedrolfbfab292019-12-23 13:19:25 +010042 #include <osmocom/gsm/gsm48.h>
Harald Welte717cdf52017-07-21 21:56:23 +020043 #include <osmocom/core/gsmtap_util.h>
Harald Welte3e51d3e2017-10-29 10:57:27 +010044 #include <osmocom/core/application.h>
Max1187a772018-01-26 13:31:42 +010045 #include <osmocom/core/bitvec.h>
46 #include <osmocom/core/gsmtap.h>
47 #include <osmocom/core/logging.h>
48 #include <osmocom/core/utils.h>
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +020049}
Holger Hans Peter Freytherb78adcd2013-10-17 20:12:37 +020050
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +020051#include <errno.h>
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +020052#include <string.h>
53
Philipp Maier1275a3f2017-02-21 19:35:23 +010054#define RFN_MODULUS 42432
55#define RFN_THRESHOLD RFN_MODULUS / 2
56
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +020057extern void *tall_pcu_ctx;
58
Harald Welte3e51d3e2017-10-29 10:57:27 +010059extern "C" {
60 /* e must make sure to initialize logging before the BTS static
61 * constructors are executed below, as those call libosmocore APIs that
62 * require logging already to be initialized. */
Harald Welte0a369e52020-09-30 19:57:02 +020063 __attribute__((constructor (101))) static void early_init(void)
Harald Welte3e51d3e2017-10-29 10:57:27 +010064 {
Neels Hofmeyr42f2d612018-04-01 16:54:40 +020065 if (!tall_pcu_ctx) {
66 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
67 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
68 }
Harald Welte3e51d3e2017-10-29 10:57:27 +010069 }
70}
71
Pau Espin Pedrol906aafc2021-01-18 17:53:29 +010072void bts_trx_free_all_tbf(struct gprs_rlcmac_trx *trx)
73{
74 for (uint8_t ts = 0; ts < 8; ts++)
75 pdch_free_all_tbf(&trx->pdch[ts]);
76}
77
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020078static struct osmo_tdef T_defs_bts[] = {
Pau Espin Pedrol2ab840a2021-04-26 17:49:05 +020079 { .T=3142, .default_val=20, .unit=OSMO_TDEF_S, .desc="Wait Indication used in Imm Ass Reject during TBF Establishment (CCCH) (s)", .val=0, .min_val = 0, .max_val = 255 }, /* TS 44.018 10.5.2.43, TS 44.060 7.1.3.2.1 (T3172) */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020080 { .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 },
Pau Espin Pedrol2ab840a2021-04-26 17:49:05 +020081 { .T=3172, .default_val=5000,.unit=OSMO_TDEF_MS, .desc="Wait Indication used in Imm Ass Reject during TBF Establishment (PACCH) (s)", .val=0, .min_val = 0, .max_val = 255000 }, /* TS 44.060 7.1.3.2.1 */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020082 { .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 },
83 { .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 },
84 { .T=3195, .default_val=5, .unit=OSMO_TDEF_S, .desc="Reuse of TFI(s) upon no response from the MS (radio failure or cell change) for TBF/MBMS radio bearer (s)", .val=0 },
Pau Espin Pedrol5360ef52019-09-13 12:41:54 +020085 { .T=0, .default_val=0, .unit=OSMO_TDEF_S, .desc=NULL, .val=0 } /* empty item at the end */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020086};
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +020087
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +010088/**
89 * For gcc-4.4 compat do not use extended initializer list but keep the
90 * order from the enum here. Once we support GCC4.7 and up we can change
91 * the code below.
92 */
93static const struct rate_ctr_desc bts_ctr_description[] = {
Maxa5eb67d2017-11-21 20:24:54 +010094 { "tbf:dl:alloc", "TBF DL Allocated "},
95 { "tbf:dl:freed", "TBF DL Freed "},
96 { "tbf:dl:aborted", "TBF DL Aborted "},
97 { "tbf:ul:alloc", "TBF UL Allocated "},
98 { "tbf:ul:freed", "TBF UL Freed "},
99 { "tbf:ul:aborted", "TBF UL Aborted "},
100 { "tbf:reused", "TBF Reused "},
101 { "tbf:alloc:algo-a", "TBF Alloc Algo A "},
102 { "tbf:alloc:algo-b", "TBF Alloc Algo B "},
Pau Espin Pedrol9688dc92021-02-25 18:30:33 +0100103 { "tbf:alloc:failed", "TBF Alloc Failure (any reason)"},
104 { "tbf:alloc:failed:no_tfi", "TBF Alloc Failure (TFIs exhausted)"},
105 { "tbf:alloc:failed:no_usf", "TBF Alloc Failure (USFs exhausted)"},
106 { "tbf:alloc:failed:no_slot_combi", "TBF Alloc Failure (No valid UL/DL slot combination found)"},
107 { "tbf:alloc:failed:no_slot_avail", "TBF Alloc Failure (No slot available)"},
Maxa5eb67d2017-11-21 20:24:54 +0100108 { "rlc:sent", "RLC Sent "},
109 { "rlc:resent", "RLC Resent "},
110 { "rlc:restarted", "RLC Restarted "},
111 { "rlc:stalled", "RLC Stalled "},
112 { "rlc:nacked", "RLC Nacked "},
113 { "rlc:final_block_resent", "RLC Final Blk resent "},
114 { "rlc:ass:timedout", "RLC Assign Timeout "},
115 { "rlc:ass:failed", "RLC Assign Failed "},
116 { "rlc:ack:timedout", "RLC Ack Timeout "},
117 { "rlc:ack:failed", "RLC Ack Failed "},
118 { "rlc:rel:timedout", "RLC Release Timeout "},
119 { "rlc:late-block", "RLC Late Block "},
120 { "rlc:sent-dummy", "RLC Sent Dummy "},
121 { "rlc:sent-control", "RLC Sent Control "},
122 { "rlc:dl_bytes", "RLC DL Bytes "},
123 { "rlc:dl_payload_bytes", "RLC DL Payload Bytes "},
124 { "rlc:ul_bytes", "RLC UL Bytes "},
125 { "rlc:ul_payload_bytes", "RLC UL Payload Bytes "},
126 { "decode:errors", "Decode Errors "},
127 { "sba:allocated", "SBA Allocated "},
128 { "sba:freed", "SBA Freed "},
129 { "sba:timedout", "SBA Timeout "},
130 { "llc:timeout", "Timedout Frames "},
131 { "llc:dropped", "Dropped Frames "},
132 { "llc:scheduled", "Scheduled Frames "},
133 { "llc:dl_bytes", "RLC encapsulated PDUs"},
134 { "llc:ul_bytes", "full PDUs received "},
135 { "rach:requests", "RACH requests "},
136 { "11bit_rach:requests", "11BIT_RACH requests "},
137 { "spb:uplink_first_segment", "First seg of UL SPB "},
138 { "spb:uplink_second_segment", "Second seg of UL SPB "},
139 { "spb:downlink_first_segment", "First seg of DL SPB "},
140 { "spb:downlink_second_segment","Second seg of DL SPB "},
141 { "immediate:assignment_UL", "Immediate Assign UL "},
142 { "immediate:assignment_rej", "Immediate Assign Rej "},
143 { "immediate:assignment_DL", "Immediate Assign DL "},
144 { "channel:request_description","Channel Request Desc "},
145 { "pkt:ul_assignment", "Packet UL Assignment "},
146 { "pkt:access_reject", "Packet Access Reject "},
147 { "pkt:dl_assignment", "Packet DL Assignment "},
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100148 { "pkt:cell_chg_notification", "Packet Cell Change Notification"},
149 { "pkt:cell_chg_continue", "Packet Cell Change Continue"},
150 { "pkt:neigh_cell_data", "Packet Neighbour Cell Data"},
Maxa5eb67d2017-11-21 20:24:54 +0100151 { "ul:control", "UL control Block "},
152 { "ul:assignment_poll_timeout", "UL Assign Timeout "},
153 { "ul:assignment_failed", "UL Assign Failed "},
154 { "dl:assignment_timeout", "DL Assign Timeout "},
155 { "dl:assignment_failed", "DL Assign Failed "},
156 { "pkt:ul_ack_nack_timeout", "PUAN Poll Timeout "},
157 { "pkt:ul_ack_nack_failed", "PUAN poll Failed "},
158 { "pkt:dl_ack_nack_timeout", "PDAN poll Timeout "},
159 { "pkt:dl_ack_nack_failed", "PDAN poll Failed "},
160 { "gprs:downlink_cs1", "CS1 downlink "},
161 { "gprs:downlink_cs2", "CS2 downlink "},
162 { "gprs:downlink_cs3", "CS3 downlink "},
163 { "gprs:downlink_cs4", "CS4 downlink "},
164 { "egprs:downlink_mcs1", "MCS1 downlink "},
165 { "egprs:downlink_mcs2", "MCS2 downlink "},
166 { "egprs:downlink_mcs3", "MCS3 downlink "},
167 { "egprs:downlink_mcs4", "MCS4 downlink "},
168 { "egprs:downlink_mcs5", "MCS5 downlink "},
169 { "egprs:downlink_mcs6", "MCS6 downlink "},
170 { "egprs:downlink_mcs7", "MCS7 downlink "},
171 { "egprs:downlink_mcs8", "MCS8 downlink "},
172 { "egprs:downlink_mcs9", "MCS9 downlink "},
173 { "gprs:uplink_cs1", "CS1 Uplink "},
174 { "gprs:uplink_cs2", "CS2 Uplink "},
175 { "gprs:uplink_cs3", "CS3 Uplink "},
176 { "gprs:uplink_cs4", "CS4 Uplink "},
177 { "egprs:uplink_mcs1", "MCS1 Uplink "},
178 { "egprs:uplink_mcs2", "MCS2 Uplink "},
179 { "egprs:uplink_mcs3", "MCS3 Uplink "},
180 { "egprs:uplink_mcs4", "MCS4 Uplink "},
181 { "egprs:uplink_mcs5", "MCS5 Uplink "},
182 { "egprs:uplink_mcs6", "MCS6 Uplink "},
183 { "egprs:uplink_mcs7", "MCS7 Uplink "},
184 { "egprs:uplink_mcs8", "MCS8 Uplink "},
185 { "egprs:uplink_mcs9", "MCS9 Uplink "},
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100186};
187
188static const struct rate_ctr_group_desc bts_ctrg_desc = {
189 "bts",
190 "BTS Statistics",
Holger Hans Peter Freytherb8a54262015-11-07 21:00:00 +0100191 OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100192 ARRAY_SIZE(bts_ctr_description),
193 bts_ctr_description,
194};
195
Jacob Erlbeckf5898a02015-11-27 19:05:13 +0100196static const struct osmo_stat_item_desc bts_stat_item_description[] = {
197 { "ms.present", "MS Present ",
198 OSMO_STAT_ITEM_NO_UNIT, 4, 0},
199};
200
201static const struct osmo_stat_item_group_desc bts_statg_desc = {
202 "bts",
203 "BTS Statistics",
204 OSMO_STATS_CLASS_GLOBAL,
205 ARRAY_SIZE(bts_stat_item_description),
206 bts_stat_item_description,
207};
208
Pau Espin Pedrol289f9002021-01-18 12:57:32 +0100209static int bts_talloc_destructor(struct gprs_rlcmac_bts* bts)
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100210{
Pau Espin Pedrol289f9002021-01-18 12:57:32 +0100211 /* this can cause counter updates and must not be left to the
212 * m_ms_store's destructor */
213 bts->ms_store->cleanup();
214 delete bts->ms_store;
Pau Espin Pedrol289f9002021-01-18 12:57:32 +0100215
216 if (bts->ratectrs) {
217 rate_ctr_group_free(bts->ratectrs);
218 bts->ratectrs = NULL;
219 }
220
221 if (bts->statg) {
222 osmo_stat_item_group_free(bts->statg);
223 bts->statg = NULL;
224 }
225
226 if (bts->app_info) {
227 msgb_free(bts->app_info);
228 bts->app_info = NULL;
229 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100230
231 llist_del(&bts->list);
Pau Espin Pedrol289f9002021-01-18 12:57:32 +0100232 return 0;
233}
234
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100235struct gprs_rlcmac_bts* bts_alloc(struct gprs_pcu *pcu, uint8_t bts_nr)
Pau Espin Pedrol289f9002021-01-18 12:57:32 +0100236{
237 struct gprs_rlcmac_bts* bts;
238 bts = talloc_zero(pcu, struct gprs_rlcmac_bts);
239 if (!bts)
240 return bts;
241 talloc_set_destructor(bts, bts_talloc_destructor);
242
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100243 bts->pcu = pcu;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100244 bts->nr = bts_nr;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100245
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100246 bts->ms_store = new GprsMsStorage(bts);
247
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +0100248 bts->cur_fn = FN_UNSET;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100249 bts->cur_blk_fn = -1;
250 bts->max_cs_dl = MAX_GPRS_CS;
251 bts->max_cs_ul = MAX_GPRS_CS;
252 bts->max_mcs_dl = MAX_EDGE_MCS;
253 bts->max_mcs_ul = MAX_EDGE_MCS;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100254 bts->initial_cs_dl = bts->initial_cs_ul = 1;
255 bts->initial_mcs_dl = bts->initial_mcs_ul = 1;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100256 bts->cs_mask = 1 << 0; /* CS-1 always enabled by default */
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100257 bts->n3101 = 10;
258 bts->n3103 = 4;
259 bts->n3105 = 8;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100260 bts->si13_is_set = false;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100261
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100262 bts->app_info = NULL;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100263 bts->T_defs_bts = T_defs_bts;
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100264 osmo_tdefs_reset(bts->T_defs_bts);
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100265
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100266 INIT_LLIST_HEAD(&bts->ul_tbfs);
267 INIT_LLIST_HEAD(&bts->dl_tbfs);
268
Pau Espin Pedrol46fd7a02020-11-02 14:36:22 +0100269 /* initialize back pointers */
Pau Espin Pedrol702ebee2021-03-08 14:15:54 +0100270 for (size_t trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no)
271 bts_trx_init(&bts->trx[trx_no], bts, trx_no);
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100272
Maxea9968f2017-12-18 22:52:52 +0100273 /* The static allocator might have already registered the counter group.
Max1714aea2017-12-21 11:19:39 +0100274 If this happens and we still called explicitly (in tests/ for example)
Maxea9968f2017-12-18 22:52:52 +0100275 than just allocate the group with different index.
276 This shall be removed once weget rid of BTS singleton */
277 if (rate_ctr_get_group_by_name_idx(bts_ctrg_desc.group_name_prefix, 0))
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100278 bts->ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 1);
Maxea9968f2017-12-18 22:52:52 +0100279 else
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100280 bts->ratectrs = rate_ctr_group_alloc(tall_pcu_ctx, &bts_ctrg_desc, 0);
281 OSMO_ASSERT(bts->ratectrs);
Maxea9968f2017-12-18 22:52:52 +0100282
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100283 bts->statg = osmo_stat_item_group_alloc(tall_pcu_ctx, &bts_statg_desc, 0);
284 OSMO_ASSERT(bts->statg);
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200285
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100286 llist_add_tail(&bts->list, &pcu->bts_list);
287
Pau Espin Pedrol289f9002021-01-18 12:57:32 +0100288 return bts;
Holger Hans Peter Freytherf5372982013-10-27 09:02:31 +0100289}
290
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +0100291void bts_set_current_frame_number(struct gprs_rlcmac_bts *bts, uint32_t fn)
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200292{
Jacob Erlbeckac49d092015-08-27 13:18:24 +0200293 /* The UL frame numbers lag 3 behind the DL frames and the data
294 * indication is only sent after all 4 frames of the block have been
295 * received. Sometimes there is an idle frame between the end of one
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100296 * and start of another frame (every 3 blocks). */
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +0100297 if (fn != bts->cur_fn && bts->cur_fn != FN_UNSET && fn != fn_next_block(bts->cur_fn)) {
298 LOGP(DRLCMAC, LOGL_NOTICE,
299 "Detected FN jump! %u -> %u\n", bts->cur_fn, fn);
300 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100301 bts->cur_fn = fn;
Holger Hans Peter Freyther9b30c7f2013-10-17 19:59:56 +0200302}
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200303
Max9dabfa22017-05-16 16:10:45 +0200304static inline int delta_fn(int fn, int to)
305{
306 return (fn + GSM_MAX_FN * 3 / 2 - to) % GSM_MAX_FN - GSM_MAX_FN/2;
307}
308
Pau Espin Pedrolfecab502021-03-17 15:26:37 +0100309void bts_set_current_block_frame_number(struct gprs_rlcmac_bts *bts, int fn)
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200310{
311 int delay = 0;
312 const int late_block_delay_thresh = 13;
313 const int fn_update_ok_min_delay = -500;
314 const int fn_update_ok_max_delay = 0;
315
316 /* frame numbers in the received blocks are assumed to be strongly
317 * monotonic. */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100318 if (bts->cur_blk_fn >= 0) {
319 int delta = delta_fn(fn, bts->cur_blk_fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200320 if (delta <= 0)
321 return;
322 }
323
324 /* Check block delay vs. the current frame number */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100325 if (bts_current_frame_number(bts) != 0)
326 delay = delta_fn(fn, bts_current_frame_number(bts));
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200327 if (delay <= -late_block_delay_thresh) {
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200328 LOGP(DRLCMAC, LOGL_NOTICE,
329 "Late RLC block, FN delta: %d FN: %d curFN: %d\n",
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100330 delay, fn, bts_current_frame_number(bts));
331 bts_do_rate_ctr_inc(bts, CTR_RLC_LATE_BLOCK);
Jacob Erlbeckbe4a08b2015-08-25 15:19:31 +0200332 }
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200333
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100334 bts->cur_blk_fn = fn;
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200335 if (delay < fn_update_ok_min_delay || delay > fn_update_ok_max_delay ||
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +0100336 bts_current_frame_number(bts) == FN_UNSET)
337 bts_set_current_frame_number(bts, fn);
Jacob Erlbeck60f77032015-08-24 14:35:14 +0200338}
339
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100340int bts_add_paging(struct gprs_rlcmac_bts *bts, uint8_t chan_needed, const struct osmo_mobile_identity *mi)
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200341{
342 uint8_t l, trx, ts, any_tbf = 0;
343 struct gprs_rlcmac_tbf *tbf;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100344 struct llist_item *pos;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200345 uint8_t slot_mask[8];
346 int8_t first_ts; /* must be signed */
347
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100348 struct llist_head *tbfs_lists[] = {
349 &bts->ul_tbfs,
350 &bts->dl_tbfs,
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200351 NULL
352 };
353
Neels Hofmeyr00520512020-08-21 15:44:58 +0200354 if (log_check_level(DRLCMAC, LOGL_INFO)) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200355 char str[64];
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200356 osmo_mobile_identity_to_str_buf(str, sizeof(str), mi);
Neels Hofmeyr00520512020-08-21 15:44:58 +0200357 LOGP(DRLCMAC, LOGL_INFO, "Add RR paging: chan-needed=%d MI=%s\n", chan_needed, str);
358 }
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200359
360 /* collect slots to page
361 * Mark slots for every TBF, but only mark one of it.
362 * Mark only the first slot found.
363 * Don't mark, if TBF uses a different slot that is already marked. */
364 memset(slot_mask, 0, sizeof(slot_mask));
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200365 for (l = 0; tbfs_lists[l]; l++) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100366 llist_for_each_entry(pos, tbfs_lists[l], list) {
367 tbf = (struct gprs_rlcmac_tbf *)pos->entry;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200368 first_ts = -1;
369 for (ts = 0; ts < 8; ts++) {
370 if (tbf->pdch[ts]) {
371 /* remember the first slot found */
372 if (first_ts < 0)
373 first_ts = ts;
374 /* break, if we already marked a slot */
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100375 if ((slot_mask[tbf->trx->trx_no] & (1 << ts)))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200376 break;
377 }
378 }
379 /* mark first slot found, if none is marked already */
380 if (ts == 8 && first_ts >= 0) {
Maxc21f0072017-12-15 17:36:45 +0100381 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200382 "TRX=%d TS=%d, so we mark\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100383 tbf->trx->trx_no, first_ts);
Holger Hans Peter Freytherec80f822013-10-27 13:44:37 +0100384 slot_mask[tbf->trx->trx_no] |= (1 << first_ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200385 } else
Maxc21f0072017-12-15 17:36:45 +0100386 LOGPTBF(tbf, LOGL_DEBUG, "uses "
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200387 "already marked TRX=%d TS=%d\n",
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100388 tbf->trx->trx_no, ts);
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200389 }
390 }
391
392 /* Now we have a list of marked slots. Every TBF uses at least one
393 * of these slots. */
394
395 /* schedule paging to all marked slots */
396 for (trx = 0; trx < 8; trx++) {
397 if (slot_mask[trx] == 0)
398 continue;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200399 for (ts = 0; ts < 8; ts++) {
400 if ((slot_mask[trx] & (1 << ts))) {
401 /* schedule */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100402 if (!bts->trx[trx].pdch[ts].add_paging(chan_needed, mi))
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200403 return -ENOMEM;
Max4382e4e2018-01-30 12:00:08 +0100404
Pau Espin Pedrol4c51eaf2021-05-19 12:02:34 +0200405 LOGPDCH(&bts->trx[trx].pdch[ts], DRLCMAC, LOGL_INFO, "Paging on PACCH\n");
Holger Hans Peter Freyther94464852013-10-19 18:33:52 +0200406 any_tbf = 1;
Holger Hans Peter Freytherf0984892013-10-19 18:28:59 +0200407 }
408 }
409 }
410
411 if (!any_tbf)
412 LOGP(DRLCMAC, LOGL_INFO, "No paging, because no TBF\n");
413
414 return 0;
415}
416
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100417void bts_send_gsmtap_rach(struct gprs_rlcmac_bts *bts,
418 enum pcu_gsmtap_category categ, uint8_t channel,
419 const struct rach_ind_params *rip)
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700420{
Vadim Yanitskiy55022ea2021-01-06 22:44:06 +0100421 struct pcu_l1_meas meas = { 0 };
Vadim Yanitskiy81b610e2020-06-11 01:04:44 +0700422 uint8_t ra_buf[2];
423
424 /* 3GPP TS 44.004 defines 11 bit RA as follows: xxxx xxxx .... .yyy
425 * On the PCUIF, we get 16 bit machne dependent number (LE/BE)
426 * Over GSMTAP we send the following: xxxx xxxx yyy. ....
427 * This simplifies parsing in Wireshark using its CSN.1 codec. */
428 if (rip->is_11bit) {
429 ra_buf[0] = (uint8_t) ((rip->ra >> 3) & 0xff);
430 ra_buf[1] = (uint8_t) ((rip->ra << 5) & 0xff);
431 } else {
432 ra_buf[0] = (uint8_t) (rip->ra & 0xff);
433 }
434
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100435 bts_send_gsmtap_meas(bts, categ, true, rip->trx_nr, rip->ts_nr, channel,
436 bts_rfn_to_fn(bts, rip->rfn), ra_buf,
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700437 rip->is_11bit ? 2 : 1, &meas);
438}
439
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100440void bts_send_gsmtap(struct gprs_rlcmac_bts *bts,
441 enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
442 uint8_t ts_no, uint8_t channel, uint32_t fn,
443 const uint8_t *data, unsigned int len)
Harald Welte717cdf52017-07-21 21:56:23 +0200444{
Vadim Yanitskiyeb70a402021-01-13 13:58:14 +0100445 struct pcu_l1_meas meas = { 0 };
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100446 bts_send_gsmtap_meas(bts, categ, uplink, trx_no, ts_no, channel, fn, data, len, &meas);
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200447}
448
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100449void bts_send_gsmtap_meas(struct gprs_rlcmac_bts *bts,
450 enum pcu_gsmtap_category categ, bool uplink, uint8_t trx_no,
451 uint8_t ts_no, uint8_t channel, uint32_t fn,
452 const uint8_t *data, unsigned int len, struct pcu_l1_meas *meas)
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200453{
Harald Welte717cdf52017-07-21 21:56:23 +0200454 uint16_t arfcn;
455
456 /* check if category is activated at all */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100457 if (!(bts->pcu->gsmtap_categ_mask & (1 << categ)))
Harald Welte717cdf52017-07-21 21:56:23 +0200458 return;
459
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100460 arfcn = bts->trx[trx_no].arfcn;
Harald Welte717cdf52017-07-21 21:56:23 +0200461 if (uplink)
462 arfcn |= GSMTAP_ARFCN_F_UPLINK;
463
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200464 /* GSMTAP needs the SNR here, but we only have C/I (meas->link_qual).
465 Those are not the same, but there is no known way to convert them,
466 let's pass C/I instead of nothing */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100467 gsmtap_send(bts->pcu->gsmtap, arfcn, ts_no, channel, 0, fn,
Pau Espin Pedrol0b0391f2020-05-19 17:23:23 +0200468 meas->rssi, meas->link_qual, data, len);
Harald Welte717cdf52017-07-21 21:56:23 +0200469}
470
Daniel Willmann54044b02014-07-02 17:58:15 +0200471/* lookup downlink TBF Entity (by TFI) */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100472struct gprs_rlcmac_dl_tbf *bts_dl_tbf_by_tfi(struct gprs_rlcmac_bts *bts, 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
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100477 return 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) */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100481struct gprs_rlcmac_ul_tbf *bts_ul_tbf_by_tfi(struct gprs_rlcmac_bts *bts, 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
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100486 return bts->trx[trx].pdch[ts].ul_tbf_by_tfi(tfi);
Holger Hans Peter Freytherf63cabd2013-10-26 19:10:43 +0200487}
488
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100489static unsigned int trx_count_free_tfi(const struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir, uint8_t *first_free_tfi)
490{
491 const struct gprs_rlcmac_pdch *pdch;
492 uint8_t ts;
493 unsigned int i;
494 unsigned int free_tfi_cnt = 0;
495 bool has_pdch = false;
496 uint32_t mask = NO_FREE_TFI;
497
498 for (ts = 0; ts < ARRAY_SIZE(trx->pdch); ts++) {
499 pdch = &trx->pdch[ts];
500 if (!pdch->is_enabled())
501 continue;
502 has_pdch = true;
503 mask &= ~pdch->assigned_tfi(dir);
504 }
505
506 if (!has_pdch || !mask) {
507 *first_free_tfi = (uint8_t)-1;
508 return 0;
509 }
510
511 /* Count free tfis and return */
512 for (i = 0; i < sizeof(mask) * 8 ; i++) {
513 if (mask & 1) {
514 if (free_tfi_cnt == 0)
515 *first_free_tfi = i;
516 free_tfi_cnt++;
517 }
518 mask >>= 1;
519 }
520 return free_tfi_cnt;
521}
522
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200523/*
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100524 * Search for free TFI and return TFI, TRX. This method returns the first TFI
525 * that is currently not used in any PDCH of a the TRX with least TFIs currently
526 * assigned. Negative values indicate errors.
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200527 */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100528int bts_tfi_find_free(const struct gprs_rlcmac_bts *bts, enum gprs_rlcmac_tbf_direction dir,
529 uint8_t *_trx, int8_t use_trx)
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200530{
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100531 uint8_t trx_from, trx_to, trx;
532 uint8_t best_trx_nr = 0xff;
533 unsigned int best_cnt = 0;
534 uint8_t best_first_tfi = 0;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200535
536 if (use_trx >= 0 && use_trx < 8)
537 trx_from = trx_to = use_trx;
538 else {
539 trx_from = 0;
540 trx_to = 7;
541 }
542
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200543 /* find a TFI that is unused on all PDCH */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200544 for (trx = trx_from; trx <= trx_to; trx++) {
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100545 uint8_t tmp_first_tfi;
546 unsigned int tmp_cnt;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100547 tmp_cnt = trx_count_free_tfi(&bts->trx[trx], dir, &tmp_first_tfi);
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100548 if (tmp_cnt > best_cnt) {
549 best_cnt = tmp_cnt;
550 best_first_tfi = tmp_first_tfi;
551 best_trx_nr = trx;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200552 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200553 }
554
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100555 if (best_trx_nr == 0xff || best_cnt == 0) {
Max69d585e2018-01-26 12:36:43 +0100556 LOGP(DRLCMAC, LOGL_NOTICE, "No TFI available (suggested TRX: %d).\n", use_trx);
Pau Espin Pedrol9688dc92021-02-25 18:30:33 +0100557 bts_do_rate_ctr_inc(bts, CTR_TBF_ALLOC_FAIL_NO_TFI);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200558 return -EBUSY;
559 }
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200560
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100561 OSMO_ASSERT(best_first_tfi < 32);
Jacob Erlbeck3a10dbd2015-07-10 19:52:37 +0200562
Pau Espin Pedrol983bb7e2020-10-26 14:52:06 +0100563 LOGP(DRLCMAC, LOGL_DEBUG, "Found first unallocated TRX=%d TFI=%d\n",
564 best_trx_nr, best_first_tfi);
565 *_trx = best_trx_nr;
566 return best_first_tfi;
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200567}
568
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100569int bts_rcv_imm_ass_cnf(struct gprs_rlcmac_bts *bts, const uint8_t *data, uint32_t fn)
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200570{
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200571 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200572 uint8_t plen;
573 uint32_t tlli;
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200574 GprsMs *ms;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200575
576 /* move to IA Rest Octets */
577 plen = data[0] >> 2;
578 data += 1 + plen;
579
580 if ((*data & 0xf0) != 0xd0) {
581 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but rest "
582 "octets do not start with bit sequence 'HH01' "
583 "(Packet Downlink Assignment)\n");
584 return -EINVAL;
585 }
586
587 /* get TLLI from downlink assignment */
Eric Wildab8b01e2019-07-23 17:16:06 +0200588 tlli = (uint32_t)((*data++) & 0xf) << 28;
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200589 tlli |= (*data++) << 20;
590 tlli |= (*data++) << 12;
591 tlli |= (*data++) << 4;
592 tlli |= (*data++) >> 4;
593
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100594 ms = bts_ms_by_tlli(bts, tlli, GSM_RESERVED_TMSI);
Jacob Erlbeck767193e2015-05-20 12:06:46 +0200595 if (ms)
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100596 dl_tbf = ms_dl_tbf(ms);
Daniel Willmann1dac2eb2014-08-07 12:37:34 +0200597 if (!dl_tbf) {
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200598 LOGP(DRLCMAC, LOGL_ERROR, "Got IMM.ASS confirm, but TLLI=%08x "
599 "does not exit\n", tlli);
600 return -EINVAL;
601 }
602
603 LOGP(DRLCMAC, LOGL_DEBUG, "Got IMM.ASS confirm for TLLI=%08x\n", tlli);
604
Pau Espin Pedrol039ee822021-04-26 18:49:33 +0200605 if (dl_tbf->m_wait_confirm) {
606 /* Transition to FLOW in gprs_rlcmac_tbf::handle_timeout() when timer expires */
Pau Espin Pedrol28f160e2019-09-05 14:48:35 +0200607 T_START(dl_tbf, T0, -2002, "assignment (AGCH)", true);
Pau Espin Pedrol039ee822021-04-26 18:49:33 +0200608 }
Holger Hans Peter Freyther40cfaa62013-10-26 19:49:16 +0200609
610 return 0;
611}
612
Philipp Maier1275a3f2017-02-21 19:35:23 +0100613/* Determine the full frame number from a relative frame number */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100614uint32_t bts_rfn_to_fn(const struct gprs_rlcmac_bts *bts, int32_t rfn)
Philipp Maier1275a3f2017-02-21 19:35:23 +0100615{
Max5dd8d1b2017-03-08 12:06:42 +0100616 int32_t m_cur_rfn;
617 int32_t fn;
618 int32_t fn_rounded;
619
620 /* double-check that relative FN is not negative and fits into int32_t */
621 OSMO_ASSERT(rfn < GSM_MAX_FN);
622 OSMO_ASSERT(rfn >= 0);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100623
624 /* Note: If a BTS is sending in a rach request it will be fully aware
625 * of the frame number. If the PCU is used in a BSC-co-located setup.
626 * The BSC will forward the incoming RACH request. The RACH request
627 * only contains the relative frame number (Fn % 42432) in its request
628 * reference. This PCU implementation has to fit both scenarios, so
629 * we need to assume that Fn is a relative frame number. */
630
631 /* Ensure that all following calculations are performed with the
632 * relative frame number */
Max5dd8d1b2017-03-08 12:06:42 +0100633 if (rfn >= RFN_MODULUS)
634 return rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100635
636 /* Compute an internal relative frame number from the full internal
637 frame number */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100638 m_cur_rfn = bts->cur_fn % RFN_MODULUS;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100639
640 /* Compute a "rounded" version of the internal frame number, which
641 * exactly fits in the RFN_MODULUS raster */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100642 fn_rounded = bts->cur_fn - m_cur_rfn;
Philipp Maier1275a3f2017-02-21 19:35:23 +0100643
644 /* If the delta between the internal and the external relative frame
645 * number exceeds a certain limit, we need to assume that the incoming
646 * rach request belongs to a the previous rfn period. To correct this,
647 * we roll back the rounded frame number by one RFN_MODULUS */
648 if (abs(rfn - m_cur_rfn) > RFN_THRESHOLD) {
649 LOGP(DRLCMAC, LOGL_DEBUG,
Thorsten Alteholz8bb79042018-10-06 10:42:58 +0200650 "Race condition between rfn (%u) and m_cur_fn (%u) detected: rfn belongs to the previous modulus %u cycle, wrapping...\n",
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100651 rfn, bts->cur_fn, RFN_MODULUS);
Philipp Maier1275a3f2017-02-21 19:35:23 +0100652 if (fn_rounded < RFN_MODULUS) {
653 LOGP(DRLCMAC, LOGL_DEBUG,
654 "Cornercase detected: wrapping crosses %u border\n",
655 GSM_MAX_FN);
656 fn_rounded = GSM_MAX_FN - (RFN_MODULUS - fn_rounded);
657 }
658 else
659 fn_rounded -= RFN_MODULUS;
660 }
661
662 /* The real frame number is the sum of the rounded frame number and the
663 * relative framenumber computed via RACH */
664 fn = fn_rounded + rfn;
665
666 return fn;
667}
668
Pau Espin Pedrol9ee90d22020-05-08 14:20:32 +0200669/* 3GPP TS 44.060:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700670 * Table 11.2.5.2: PACKET CHANNEL REQUEST
671 * Table 11.2.5a.2: EGPRS PACKET CHANNEL REQUEST
672 * Both GPRS and EGPRS use same MultislotClass coding, but since PRACH is
Pau Espin Pedrol9ee90d22020-05-08 14:20:32 +0200673 * deprecated, no PACKET CHANNEL REQUEST exists, which means for GPRS we will
674 * receive CCCH RACH which doesn't contain any mslot class. Hence in the end we
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700675 * can only receive EGPRS mslot class through 11-bit EGPRS PACKET CHANNEL REQUEST. */
676static int parse_egprs_pkt_ch_req(uint16_t ra11, struct chan_req_params *chan_req)
Max58818582018-01-05 15:04:50 +0100677{
Vadim Yanitskiyb90d34b2020-05-21 20:20:34 +0700678 EGPRS_PacketChannelRequest_t req;
679 int rc;
680
681 rc = decode_egprs_pkt_ch_req(ra11, &req);
682 if (rc) {
683 LOGP(DRLCMAC, LOGL_NOTICE, "Failed to decode "
684 "EGPRS Packet Channel Request: rc=%d\n", rc);
685 return rc;
686 }
687
688 LOGP(DRLCMAC, LOGL_INFO, "Rx EGPRS Packet Channel Request: %s\n",
689 get_value_string(egprs_pkt_ch_req_type_names, req.Type));
690
691 switch (req.Type) {
692 case EGPRS_PKT_CHAN_REQ_ONE_PHASE:
693 chan_req->egprs_mslot_class = req.Content.MultislotClass + 1;
694 chan_req->priority = req.Content.Priority + 1;
695 break;
696 case EGPRS_PKT_CHAN_REQ_SHORT:
697 chan_req->priority = req.Content.Priority + 1;
698 if (req.Content.NumberOfBlocks == 0)
699 chan_req->single_block = true;
700 break;
701 case EGPRS_PKT_CHAN_REQ_ONE_PHASE_RED_LATENCY:
702 chan_req->priority = req.Content.Priority + 1;
703 break;
704 /* Two phase access => single block is needed */
705 case EGPRS_PKT_CHAN_REQ_TWO_PHASE:
706 case EGPRS_PKT_CHAN_REQ_TWO_PHASE_IPA:
707 chan_req->priority = req.Content.Priority + 1;
708 chan_req->single_block = true;
709 break;
710 /* Signalling => single block is needed */
711 case EGPRS_PKT_CHAN_REQ_SIGNALLING:
712 case EGPRS_PKT_CHAN_REQ_SIGNALLING_IPA:
713 chan_req->single_block = true;
714 break;
715
716 /* Neither unacknowledged RLC mode, nor emergency calls are supported */
717 case EGPRS_PKT_CHAN_REQ_ONE_PHASE_UNACK:
718 case EGPRS_PKT_CHAN_REQ_EMERGENCY_CALL:
719 case EGPRS_PKT_CHAN_REQ_DEDICATED_CHANNEL:
720 LOGP(DRLCMAC, LOGL_NOTICE, "%s is not supported, rejecting\n",
721 get_value_string(egprs_pkt_ch_req_type_names, req.Type));
722 return -ENOTSUP;
723
724 default:
725 LOGP(DRLCMAC, LOGL_ERROR, "Unknown EGPRS Packet Channel Request "
726 "type=0x%02x, probably a bug in CSN.1 codec\n", req.Type);
727 return -EINVAL;
728 }
Max58818582018-01-05 15:04:50 +0100729
730 return 0;
731}
732
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700733/* NOTE: chan_req needs to be zero-initialized by the caller */
734static int parse_rach_ind(const struct rach_ind_params *rip,
735 struct chan_req_params *chan_req)
Max58818582018-01-05 15:04:50 +0100736{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700737 int rc;
Max58818582018-01-05 15:04:50 +0100738
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700739 switch (rip->burst_type) {
Max137fd592018-01-05 15:53:08 +0100740 case GSM_L1_BURST_TYPE_NONE:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700741 LOGP(DRLCMAC, LOGL_ERROR, "RACH.ind contains no burst type, assuming TS0\n");
742 /* fall-through */
743 case GSM_L1_BURST_TYPE_ACCESS_0:
744 if (rip->is_11bit) { /* 11 bit Access Burst with TS0 => Packet Channel Request */
745 LOGP(DRLCMAC, LOGL_ERROR, "11 bit Packet Channel Request "
746 "is not supported (PBCCH is deprecated)\n");
747 return -ENOTSUP;
748 }
749
750 /* 3GPP TS 44.018, table 9.1.8.1: 8 bit CHANNEL REQUEST.
751 * Mask 01110xxx indicates single block packet access. */
752 chan_req->single_block = ((rip->ra & 0xf8) == 0x70);
753 break;
754 case GSM_L1_BURST_TYPE_ACCESS_1:
755 case GSM_L1_BURST_TYPE_ACCESS_2:
756 if (!rip->is_11bit) { /* TS1/TS2 => EGPRS Packet Channel Request (always 11 bit) */
757 LOGP(DRLCMAC, LOGL_ERROR, "11 bit Packet Channel Request "
758 "is not supported (PBCCH is deprecated)\n");
759 return -ENOTSUP;
760 }
761
762 rc = parse_egprs_pkt_ch_req(rip->ra, chan_req);
763 if (rc)
764 return rc;
Max137fd592018-01-05 15:53:08 +0100765 break;
766 default:
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700767 LOGP(DRLCMAC, LOGL_ERROR, "RACH.ind contains unknown burst type 0x%02x "
768 "(%u bit)\n", rip->burst_type, rip->is_11bit ? 11 : 8);
769 return -EINVAL;
Max58818582018-01-05 15:04:50 +0100770 }
771
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700772 return 0;
Max58818582018-01-05 15:04:50 +0100773}
774
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100775struct gprs_rlcmac_sba *bts_alloc_sba(struct gprs_rlcmac_bts *bts, uint8_t ta)
776{
777 struct gprs_rlcmac_pdch *pdch;
778 struct gprs_rlcmac_sba *sba = NULL;
779 int8_t trx, ts;
780
781 if (!gsm48_ta_is_valid(ta))
782 return NULL;
783
784 for (trx = 0; trx < 8; trx++) {
785 for (ts = 7; ts >= 0; ts--) {
786 pdch = &bts->trx[trx].pdch[ts];
787 if (!pdch->is_enabled())
788 continue;
789 break;
790 }
791 if (ts >= 0)
792 break;
793 }
794 if (trx == 8) {
795 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
796 return NULL;
797 }
798
799 sba = sba_alloc(bts, pdch, ta);
800 if (!sba)
801 return NULL;
802
803 bts_do_rate_ctr_inc(bts, CTR_SBA_ALLOCATED);
804 return sba;
805}
806
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100807int bts_rcv_rach(struct gprs_rlcmac_bts *bts, const struct rach_ind_params *rip)
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200808{
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700809 struct chan_req_params chan_req = { 0 };
Jacob Erlbeck1f332942015-05-04 08:21:17 +0200810 struct gprs_rlcmac_ul_tbf *tbf = NULL;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100811 struct gprs_rlcmac_sba *sba;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700812 uint8_t trx_no, ts_no;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200813 uint32_t sb_fn = 0;
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100814 uint8_t usf = 7;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700815 uint8_t tsc = 0;
816 int plen, rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200817
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100818 bts_do_rate_ctr_inc(bts, CTR_RACH_REQUESTS);
Holger Hans Peter Freytherc1ae2262013-10-27 10:50:35 +0100819
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700820 if (rip->is_11bit)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100821 bts_do_rate_ctr_inc(bts, CTR_11BIT_RACH_REQUESTS);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530822
Philipp Maier1275a3f2017-02-21 19:35:23 +0100823 /* Determine full frame number */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100824 uint32_t Fn = bts_rfn_to_fn(bts, rip->rfn);
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700825 uint8_t ta = qta2ta(rip->qta);
Philippd935d882016-11-07 13:07:36 +0100826
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100827 bts_send_gsmtap_rach(bts, PCU_GSMTAP_C_UL_RACH, GSMTAP_CHANNEL_RACH, rip);
Pau Espin Pedrol1ec211f2019-09-30 18:33:14 +0200828
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700829 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests Uplink resource on CCCH/RACH: "
830 "ra=0x%02x (%d bit) Fn=%u qta=%d\n", rip->ra,
831 rip->is_11bit ? 11 : 8, Fn, rip->qta);
bhargava628dcfb2016-09-14 20:30:31 +0530832
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700833 /* Parse [EGPRS Packet] Channel Request from RACH.ind */
834 rc = parse_rach_ind(rip, &chan_req);
835 if (rc) /* Send RR Immediate Assignment Reject */
836 goto send_imm_ass_rej;
bhargava628dcfb2016-09-14 20:30:31 +0530837
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700838 if (chan_req.single_block)
839 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation\n");
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100840 else if (bts->pcu->vty.force_two_phase) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700841 LOGP(DRLCMAC, LOGL_DEBUG, "MS requests single block allocation, "
842 "but we force two phase access\n");
843 chan_req.single_block = true;
844 }
845
Vadim Yanitskiyb90d34b2020-05-21 20:20:34 +0700846 /* TODO: handle Radio Priority (see 3GPP TS 44.060, table 11.2.5a.5) */
847 if (chan_req.priority > 0)
848 LOGP(DRLCMAC, LOGL_NOTICE, "EGPRS Packet Channel Request indicates "
849 "Radio Priority %u, however we ignore it\n", chan_req.priority);
850
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700851 /* Should we allocate a single block or an Uplink TBF? */
852 if (chan_req.single_block) {
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100853 sba = bts_alloc_sba(bts, ta);
854 if (!sba) {
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530855 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource for "
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100856 "single block allocation\n");
857 rc = -EBUSY;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700858 /* Send RR Immediate Assignment Reject */
859 goto send_imm_ass_rej;
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530860 }
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700861
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100862 trx_no = sba->pdch->trx_no();
863 ts_no = sba->pdch->ts_no;
864 sb_fn = sba->fn;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100865 tsc = bts->trx[trx_no].pdch[ts_no].tsc;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700866 LOGP(DRLCMAC, LOGL_DEBUG, "Allocated a single block at "
867 "SBFn=%u TRX=%u TS=%u\n", sb_fn, trx_no, ts_no);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200868 } else {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100869 GprsMs *ms = bts_alloc_ms(bts, 0, chan_req.egprs_mslot_class);
Pau Espin Pedrol20271c42021-05-10 17:21:03 +0200870 tbf = tbf_alloc_ul_ccch(bts, ms);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200871 if (!tbf) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700872 /* Send RR Immediate Assignment Reject */
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530873 rc = -EBUSY;
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700874 goto send_imm_ass_rej;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200875 }
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700876 tbf->set_ta(ta);
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700877 trx_no = tbf->trx->trx_no;
878 ts_no = tbf->first_ts;
879 usf = tbf->m_usf[ts_no];
880 tsc = tbf->tsc();
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200881 }
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100882
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700883send_imm_ass_rej:
884 /* Allocate a bit-vector for RR Immediate Assignment [Reject] */
885 struct bitvec *bv = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Vadim Yanitskiyeb1e0fa2020-07-18 20:33:08 +0700886 bitvec_unhex(bv, DUMMY_VEC); /* standard '2B'O padding */
Jacob Erlbeck2ca86af2016-01-20 18:26:13 +0100887
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700888 if (rc != 0) {
889 LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment Reject on AGCH\n");
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530890 plen = Encoding::write_immediate_assignment_reject(
Pau Espin Pedrol25ebf3c2021-04-26 13:36:16 +0200891 bv, rip->ra, Fn, rip->burst_type,
892 (uint8_t)osmo_tdef_get(bts->T_defs_bts, 3142, OSMO_TDEF_S, -1));
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100893 bts_do_rate_ctr_inc(bts, CTR_IMMEDIATE_ASSIGN_REJ);
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700894 } else {
895 LOGP(DRLCMAC, LOGL_DEBUG, "Tx Immediate Assignment on AGCH: "
896 "TRX=%u (ARFCN %u) TS=%u TA=%u TSC=%u TFI=%d USF=%d\n",
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100897 trx_no, bts->trx[trx_no].arfcn & ~ARFCN_FLAG_MASK,
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700898 ts_no, ta, tsc, tbf ? tbf->tfi() : -1, usf);
899 plen = Encoding::write_immediate_assignment(
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100900 &bts->trx[trx_no].pdch[ts_no], tbf, bv,
Vadim Yanitskiy11567762020-07-18 23:34:17 +0700901 false, rip->ra, Fn, ta, usf, false, sb_fn,
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100902 bts_get_ms_pwr_alpha(bts), bts->pcu->vty.gamma, -1,
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700903 rip->burst_type);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100904 bts_do_rate_ctr_inc(bts, CTR_IMMEDIATE_ASSIGN_UL_TBF);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530905 }
Jacob Erlbeck2647a332016-02-01 16:13:38 +0100906
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700907 if (plen >= 0)
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100908 pcu_l1if_tx_agch(bts, bv, plen);
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700909 else
910 rc = plen;
911
912 bitvec_free(bv);
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200913
aravind sirsikarc0c3afd2016-11-09 16:27:00 +0530914 return rc;
Holger Hans Peter Freyther02beed52013-10-26 20:56:20 +0200915}
916
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700917/* PTCCH/U sub-slot / frame-number mapping (see 3GPP TS 45.002, table 6) */
918static uint32_t ptcch_slot_map[PTCCH_TAI_NUM] = {
919 12, 38, 64, 90,
920 116, 142, 168, 194,
921 220, 246, 272, 298,
922 324, 350, 376, 402,
923};
924
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100925int bts_rcv_ptcch_rach(struct gprs_rlcmac_bts *bts, const struct rach_ind_params *rip)
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700926{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100927 uint32_t fn416 = bts_rfn_to_fn(bts, rip->rfn) % 416;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700928 struct gprs_rlcmac_pdch *pdch;
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700929 uint8_t ss;
930
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100931 bts_send_gsmtap_rach(bts, PCU_GSMTAP_C_UL_PTCCH, GSMTAP_CHANNEL_PTCCH, rip);
Vadim Yanitskiy07b64872020-06-11 00:26:15 +0700932
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700933 /* Prevent buffer overflow */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700934 if (rip->trx_nr >= ARRAY_SIZE(bts->trx) || rip->ts_nr >= 8) {
935 LOGP(DRLCMAC, LOGL_ERROR, "(TRX=%u TS=%u RFN=%u) Rx malformed "
936 "RACH.ind (PTCCH/U)\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700937 return -EINVAL;
938 }
939
940 /* Make sure PDCH time-slot is enabled */
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700941 pdch = &bts->trx[rip->trx_nr].pdch[rip->ts_nr];
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700942 if (!pdch->m_is_enabled) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700943 LOGP(DRLCMAC, LOGL_NOTICE, "(TRX=%u TS=%u RFN=%u) Rx RACH.ind (PTCCH/U) "
944 "for inactive PDCH\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700945 return -EAGAIN;
946 }
947
948 /* Convert TDMA frame-number to PTCCH/U sub-slot number */
949 for (ss = 0; ss < PTCCH_TAI_NUM; ss++)
950 if (ptcch_slot_map[ss] == fn416)
951 break;
952 if (ss == PTCCH_TAI_NUM) {
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700953 LOGP(DRLCMAC, LOGL_ERROR, "(TRX=%u TS=%u RFN=%u) Failed to map "
954 "PTCCH/U sub-slot\n", rip->trx_nr, rip->ts_nr, rip->rfn);
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700955 return -ENODEV;
956 }
957
958 /* Apply the new Timing Advance value */
959 LOGP(DRLCMAC, LOGL_INFO, "Continuous Timing Advance update "
Vadim Yanitskiya0a0b7f2020-05-21 19:55:46 +0700960 "for TAI %u, new TA is %u\n", ss, qta2ta(rip->qta));
961 pdch->update_ta(ss, qta2ta(rip->qta));
Vadim Yanitskiyffebd242019-10-05 23:45:31 +0700962
963 return 0;
964}
965
Pau Espin Pedrol2c0931c2021-03-29 18:40:18 +0200966void bts_snd_dl_ass(struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_tbf *tbf, uint16_t pgroup)
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200967{
Vadim Yanitskiy11567762020-07-18 23:34:17 +0700968 uint8_t trx_no = tbf->trx->trx_no;
969 uint8_t ts_no = tbf->first_ts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200970 int plen;
971
Maxc21f0072017-12-15 17:36:45 +0100972 LOGPTBF(tbf, LOGL_INFO, "TX: START Immediate Assignment Downlink (PCH)\n");
Alexander Couzensccde5c92017-02-04 03:10:08 +0100973 bitvec *immediate_assignment = bitvec_alloc(22, tall_pcu_ctx); /* without plen */
Vadim Yanitskiyeb1e0fa2020-07-18 20:33:08 +0700974 bitvec_unhex(immediate_assignment, DUMMY_VEC); /* standard '2B'O padding */
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200975 /* use request reference that has maximum distance to current time,
976 * so the assignment will not conflict with possible RACH requests. */
Pau Espin Pedrol2c0931c2021-03-29 18:40:18 +0200977 LOGP(DRLCMAC, LOGL_DEBUG, " - TRX=%d (%d) TS=%d TA=%d\n",
978 trx_no, tbf->trx->arfcn, ts_no, tbf->ta());
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100979 plen = Encoding::write_immediate_assignment(&bts->trx[trx_no].pdch[ts_no],
Vadim Yanitskiy11567762020-07-18 23:34:17 +0700980 tbf, immediate_assignment, true, 125,
981 (tbf->pdch[ts_no]->last_rts_fn + 21216) % GSM_MAX_FN,
Pau Espin Pedrol2c0931c2021-03-29 18:40:18 +0200982 tbf->ta(), 7, false, 0,
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +0100983 bts_get_ms_pwr_alpha(bts), bts->pcu->vty.gamma, -1,
Maxfc8afc22019-02-18 18:52:38 +0100984 GSM_L1_BURST_TYPE_ACCESS_0);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530985 if (plen >= 0) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100986 bts_do_rate_ctr_inc(bts, CTR_IMMEDIATE_ASSIGN_DL_TBF);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100987 pcu_l1if_tx_pch(bts, immediate_assignment, plen, pgroup);
Mrinal Mishraf86307e2016-11-10 18:16:30 +0530988 }
989
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200990 bitvec_free(immediate_assignment);
991}
992
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +0100993/* return maximum DL CS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100994uint8_t bts_max_cs_dl(const struct gprs_rlcmac_bts* bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +0100995{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100996 return bts->max_cs_dl;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +0100997}
998
999/* return maximum UL CS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001000uint8_t bts_max_cs_ul(const struct gprs_rlcmac_bts* bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001001{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001002 return bts->max_cs_ul;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001003}
1004
1005/* return maximum DL MCS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001006uint8_t bts_max_mcs_dl(const struct gprs_rlcmac_bts* bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001007{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001008 return bts->max_mcs_dl;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001009}
1010
1011/* return maximum UL MCS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001012uint8_t bts_max_mcs_ul(const struct gprs_rlcmac_bts* bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001013{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001014 return bts->max_mcs_ul;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001015}
1016
1017/* Set maximum DL CS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001018void bts_set_max_cs_dl(struct gprs_rlcmac_bts* bts, uint8_t cs_dl)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001019{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001020 bts->max_cs_dl = cs_dl;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001021}
1022
1023/* Set maximum UL CS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001024void bts_set_max_cs_ul(struct gprs_rlcmac_bts* bts, uint8_t cs_ul)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001025{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001026 bts->max_cs_ul = cs_ul;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001027}
1028
1029/* Set maximum DL MCS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001030void bts_set_max_mcs_dl(struct gprs_rlcmac_bts* bts, uint8_t mcs_dl)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001031{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001032 bts->max_mcs_dl = mcs_dl;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001033}
1034
1035/* Set maximum UL MCS supported by BTS and allowed by VTY */
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001036void bts_set_max_mcs_ul(struct gprs_rlcmac_bts* bts, uint8_t mcs_ul)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001037{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001038 bts->max_mcs_ul = mcs_ul;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001039}
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +02001040
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001041bool bts_cs_dl_is_supported(const struct gprs_rlcmac_bts* bts, CodingScheme cs)
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +01001042{
1043 OSMO_ASSERT(mcs_is_valid(cs));
1044 uint8_t num = mcs_chan_code(cs);
1045 if (mcs_is_gprs(cs)) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001046 return (bts_max_cs_dl(bts) >= num) && (bts->cs_mask & (1U << num));
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +01001047 } else {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001048 return (bts_max_mcs_dl(bts) >= num) && (bts->mcs_mask & (1U << num));
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +01001049 }
1050}
1051
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001052GprsMs *bts_alloc_ms(struct gprs_rlcmac_bts* bts, uint8_t ms_class, uint8_t egprs_ms_class)
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001053{
1054 GprsMs *ms;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001055 ms = bts_ms_store(bts)->create_ms();
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001056
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001057 ms_set_timeout(ms, osmo_tdef_get(bts->pcu->T_defs, -2030, OSMO_TDEF_S, -1));
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001058 ms_set_ms_class(ms, ms_class);
1059 ms_set_egprs_ms_class(ms, egprs_ms_class);
Jacob Erlbeckcb1b4942015-06-19 10:59:58 +02001060
1061 return ms;
1062}
1063
Pau Espin Pedrol4b7a71f2021-04-30 13:53:38 +02001064struct GprsMsStorage *bts_ms_store(const struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001065{
1066 return bts->ms_store;
1067}
1068
1069struct GprsMs *bts_ms_by_tlli(struct gprs_rlcmac_bts *bts, uint32_t tlli, uint32_t old_tlli)
1070{
1071 return bts_ms_store(bts)->get_ms(tlli, old_tlli);
1072}
1073
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001074/* update TA based on TA provided by PH-DATA-IND */
1075void update_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, int8_t ta_delta)
1076{
1077 int16_t ta_adj;
1078 uint8_t ta_target;
1079
1080 if (ta_delta) {
1081 /* adjust TA based on TA provided by PH-DATA-IND */
1082 ta_adj = tbf->ta() + ta_delta;
1083
1084 /* limit target TA in range 0..63 bits */
1085 ta_target = ta_limit(ta_adj);
1086
Pau Espin Pedrol58046e42021-03-29 19:01:13 +02001087 LOGP(DL1IF, LOGL_INFO, "PH-DATA-IND is updating %s: TA %u -> %u on "
1088 "TRX = %d\n", tbf_name(tbf), tbf->ta(), ta_target, tbf->trx->trx_no);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001089 tbf->set_ta(ta_target);
1090 }
1091}
1092
1093/* set TA based on TA provided by PH-RA-IND */
1094void set_tbf_ta(struct gprs_rlcmac_ul_tbf *tbf, uint8_t ta)
1095{
1096 uint8_t ta_target;
1097
1098 if (tbf->ta() != ta) {
1099 /* limit target TA in range 0..63 bits */
1100 ta_target = ta_limit(ta);
1101
Pau Espin Pedrol58046e42021-03-29 19:01:13 +02001102 LOGP(DL1IF, LOGL_INFO, "PH-RA-IND is updating %s: TA %u -> %u on "
1103 "TRX = %d\n", tbf_name(tbf), tbf->ta(), ta_target, tbf->trx->trx_no);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001104 tbf->set_ta(ta_target);
1105 }
1106}
1107
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +01001108void bts_update_tbf_ta(struct gprs_rlcmac_bts *bts, const char *p, uint32_t fn,
1109 uint8_t trx_no, uint8_t ts, int8_t ta, bool is_rach)
Maxd71e8b32016-09-19 16:17:06 +02001110{
Pau Espin Pedrol99360a32021-03-09 17:18:12 +01001111 struct gprs_rlcmac_pdch *pdch = &bts->trx[trx_no].pdch[ts];
1112 struct pdch_ulc_node *poll = pdch_ulc_get_node(pdch->ulc, fn);
1113 struct gprs_rlcmac_ul_tbf *tbf;
1114 if (!poll || poll->type !=PDCH_ULC_NODE_TBF_POLL ||
1115 poll->tbf_poll.poll_tbf->direction != GPRS_RLCMAC_UL_TBF)
Maxd71e8b32016-09-19 16:17:06 +02001116 LOGP(DL1IF, LOGL_DEBUG, "[%s] update TA = %u ignored due to "
1117 "unknown UL TBF on TRX = %d, TS = %d, FN = %d\n",
1118 p, ta, trx_no, ts, fn);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001119 else {
Pau Espin Pedrol99360a32021-03-09 17:18:12 +01001120 tbf = as_ul_tbf(poll->tbf_poll.poll_tbf);
Minh-Quang Nguyen1bcfa9a2017-11-01 14:41:37 -04001121 /* we need to distinguish TA information provided by L1
1122 * from PH-DATA-IND and PHY-RA-IND so that we can properly
1123 * update TA for given TBF
1124 */
1125 if (is_rach)
1126 set_tbf_ta(tbf, (uint8_t)ta);
1127 else
1128 update_tbf_ta(tbf, ta);
1129
Maxd71e8b32016-09-19 16:17:06 +02001130 }
1131}
1132
Pau Espin Pedrol702ebee2021-03-08 14:15:54 +01001133void bts_trx_init(struct gprs_rlcmac_trx *trx, struct gprs_rlcmac_bts *bts, uint8_t trx_no)
1134{
1135 trx->trx_no = trx_no;
1136 trx->bts = bts;
1137
1138 for (size_t ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ts_no++)
1139 pdch_init(&trx->pdch[ts_no], trx, ts_no);
1140}
1141
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001142void bts_trx_reserve_slots(struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001143 uint8_t slots)
1144{
1145 unsigned i;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001146 for (i = 0; i < ARRAY_SIZE(trx->pdch); i += 1)
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001147 if (slots & (1 << i))
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001148 trx->pdch[i].reserve(dir);
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001149}
1150
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001151void bts_trx_unreserve_slots(struct gprs_rlcmac_trx *trx, enum gprs_rlcmac_tbf_direction dir,
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001152 uint8_t slots)
1153{
1154 unsigned i;
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001155 for (i = 0; i < ARRAY_SIZE(trx->pdch); i += 1)
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001156 if (slots & (1 << i))
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001157 trx->pdch[i].unreserve(dir);
Jacob Erlbeck23f93a12015-06-30 08:52:54 +02001158}
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001159
Pau Espin Pedrol793583e2021-01-14 16:20:57 +01001160void bts_recalc_initial_cs(struct gprs_rlcmac_bts *bts)
1161{
1162 uint8_t max_cs_dl, max_cs_ul;
1163
1164 if (the_pcu->vty.force_initial_cs) {
1165 bts->initial_cs_dl = the_pcu->vty.initial_cs_dl;
1166 bts->initial_cs_ul = the_pcu->vty.initial_cs_ul;
1167 return;
1168 }
1169
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001170 max_cs_dl = bts_max_cs_dl(bts);
Pau Espin Pedrol793583e2021-01-14 16:20:57 +01001171 if (bts->pcuif_info_ind.initial_cs > max_cs_dl) {
1172 LOGP(DL1IF, LOGL_DEBUG, " downgrading initial_cs_dl to %d\n", max_cs_dl);
1173 bts->initial_cs_dl = max_cs_dl;
1174 } else {
1175 bts->initial_cs_dl = bts->pcuif_info_ind.initial_cs;
1176 }
1177 if (bts->initial_cs_dl == 0)
1178 bts->initial_cs_dl = 1; /* CS1 Must always be supported */
1179
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001180 max_cs_ul = bts_max_cs_ul(bts);
Pau Espin Pedrol793583e2021-01-14 16:20:57 +01001181 if (bts->pcuif_info_ind.initial_cs > max_cs_ul) {
1182 LOGP(DL1IF, LOGL_DEBUG, " downgrading initial_cs_ul to %d\n", max_cs_ul);
1183 bts->initial_cs_ul = max_cs_ul;
1184 } else {
1185 bts->initial_cs_ul = bts->pcuif_info_ind.initial_cs;
1186 }
1187 if (bts->initial_cs_ul == 0)
1188 bts->initial_cs_ul = 1; /* CS1 Must always be supported */
1189}
1190void bts_recalc_initial_mcs(struct gprs_rlcmac_bts *bts)
1191{
1192 uint8_t max_mcs_dl, max_mcs_ul;
1193
1194 if (the_pcu->vty.force_initial_mcs) {
1195 bts->initial_mcs_dl = the_pcu->vty.initial_mcs_dl;
1196 bts->initial_mcs_ul = the_pcu->vty.initial_mcs_ul;
1197 return;
1198 }
1199
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001200 max_mcs_dl = bts_max_mcs_dl(bts);
Pau Espin Pedrol793583e2021-01-14 16:20:57 +01001201 if (bts->pcuif_info_ind.initial_mcs > max_mcs_dl) {
1202 LOGP(DL1IF, LOGL_DEBUG, " downgrading initial_mcs_dl to %d\n", max_mcs_dl);
1203 bts->initial_mcs_dl = max_mcs_dl;
1204 } else {
1205 bts->initial_mcs_dl = bts->pcuif_info_ind.initial_mcs;
1206 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001207 max_mcs_ul = bts_max_mcs_ul(bts);
Pau Espin Pedrol793583e2021-01-14 16:20:57 +01001208 if (bts->pcuif_info_ind.initial_mcs > max_mcs_ul) {
1209 LOGP(DL1IF, LOGL_DEBUG, " downgrading initial_mcs_ul to %d\n", max_mcs_ul);
1210 bts->initial_mcs_ul = max_mcs_ul;
1211 } else {
1212 bts->initial_mcs_ul = bts->pcuif_info_ind.initial_mcs;
1213 }
1214}
1215
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001216void bts_recalc_max_cs(struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001217{
1218 int i;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001219 uint8_t cs_dl, cs_ul;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001220 struct gprs_pcu *pcu = bts->pcu;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001221
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001222 cs_dl = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001223 for (i = pcu->vty.max_cs_dl - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001224 if (bts->cs_mask & (1 << i)) {
1225 cs_dl = i + 1;
1226 break;
1227 }
1228 }
1229
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001230 cs_ul = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001231 for (i = pcu->vty.max_cs_ul - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001232 if (bts->cs_mask & (1 << i)) {
1233 cs_ul = i + 1;
1234 break;
1235 }
1236 }
1237
1238 LOGP(DRLCMAC, LOGL_DEBUG, "New max CS: DL=%u UL=%u\n", cs_dl, cs_ul);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001239 bts_set_max_cs_dl(bts, cs_dl);
1240 bts_set_max_cs_ul(bts, cs_ul);
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001241}
1242
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001243void bts_recalc_max_mcs(struct gprs_rlcmac_bts *bts)
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001244{
1245 int i;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001246 uint8_t mcs_dl, mcs_ul;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001247 struct gprs_pcu *pcu = bts->pcu;
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001248
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001249 mcs_dl = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001250 for (i = pcu->vty.max_mcs_dl - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001251 if (bts->mcs_mask & (1 << i)) {
1252 mcs_dl = i + 1;
1253 break;
1254 }
1255 }
1256
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001257 mcs_ul = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001258 for (i = pcu->vty.max_mcs_ul - 1; i >= 0; i--) {
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001259 if (bts->mcs_mask & (1 << i)) {
1260 mcs_ul = i + 1;
1261 break;
1262 }
1263 }
1264
1265 LOGP(DRLCMAC, LOGL_DEBUG, "New max MCS: DL=%u UL=%u\n", mcs_dl, mcs_ul);
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001266 bts_set_max_mcs_dl(bts, mcs_dl);
1267 bts_set_max_mcs_ul(bts, mcs_ul);
Pau Espin Pedrol343ec9b2020-10-30 18:35:54 +01001268}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001269
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001270struct GprsMs *bts_ms_by_imsi(struct gprs_rlcmac_bts *bts, const char *imsi)
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001271{
Pau Espin Pedrol2182e622021-01-14 16:48:38 +01001272 return bts_ms_store(bts)->get_ms(0, 0, imsi);
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +01001273}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +01001274
1275const struct llist_head* bts_ms_list(struct gprs_rlcmac_bts *bts)
1276{
1277 return bts_ms_store(bts)->ms_list();
1278}
Pau Espin Pedrolfe8de452021-02-09 18:47:34 +01001279
1280uint8_t bts_get_ms_pwr_alpha(const struct gprs_rlcmac_bts *bts)
1281{
1282 if (bts->pcu->vty.force_alpha != (uint8_t)-1)
1283 return bts->pcu->vty.force_alpha;
1284 if (bts->si13_is_set)
1285 return bts->si31_ro_decoded.pwr_ctrl_pars.alpha;
1286 /* default if no SI13 is received yet: closed loop control, TS 44.060
1287 * B.2 Closed loop control */
1288 return 0;
1289}