blob: 1809cd767f376783a659ccc537134b623c385eda [file] [log] [blame]
Max842d7812017-11-01 18:11:24 +01001/* mslot_class.c
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 * Copyright (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Max842d7812017-11-01 18:11:24 +010017 */
18
19#include <mslot_class.h>
Maxf633b8d2018-01-31 15:28:53 +010020#include <gprs_debug.h>
Max842d7812017-11-01 18:11:24 +010021
Max77988d42018-02-19 18:00:38 +010022#include <osmocom/core/bits.h>
Max842d7812017-11-01 18:11:24 +010023#include <osmocom/core/utils.h>
24#include <osmocom/core/logging.h>
25
26#include <errno.h>
27
Max01bd0cc2018-01-23 20:58:49 +010028/* 3GPP TS 45.002 Annex B Table B.1 */
Max842d7812017-11-01 18:11:24 +010029
30struct gprs_ms_multislot_class {
31 uint8_t rx, tx, sum; /* Maximum Number of Slots: RX, Tx, Sum Rx+Tx */
32 uint8_t ta, tb, ra, rb; /* Minimum Number of Slots */
33 uint8_t type; /* Type of Mobile */
34};
35
Max01bd0cc2018-01-23 20:58:49 +010036static const struct gprs_ms_multislot_class gprs_ms_multislot_class[] = {
Max842d7812017-11-01 18:11:24 +010037 /* M-S Class | Max # of slots | Min # of slots | Type */
38 /* | Rx Tx Sum | Tta Ttb Tra Trb | */
39 /* N/A */ { MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA, MS_NA },
40 /* 1 */ { 1, 1, 2, 3, 2, 4, 2, 1 },
41 /* 2 */ { 2, 1, 3, 3, 2, 3, 1, 1 },
42 /* 3 */ { 2, 2, 3, 3, 2, 3, 1, 1 },
43 /* 4 */ { 3, 1, 4, 3, 1, 3, 1, 1 },
44 /* 5 */ { 2, 2, 4, 3, 1, 3, 1, 1 },
45 /* 6 */ { 3, 2, 4, 3, 1, 3, 1, 1 },
46 /* 7 */ { 3, 3, 4, 3, 1, 3, 1, 1 },
47 /* 8 */ { 4, 1, 5, 3, 1, 2, 1, 1 },
48 /* 9 */ { 3, 2, 5, 3, 1, 2, 1, 1 },
49 /* 10 */ { 4, 2, 5, 3, 1, 2, 1, 1 },
50 /* 11 */ { 4, 3, 5, 3, 1, 2, 1, 1 },
51 /* 12 */ { 4, 4, 5, 2, 1, 2, 1, 1 },
52 /* 13 */ { 3, 3, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
53 /* 14 */ { 4, 4, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
54 /* 15 */ { 5, 5, MS_NA, MS_NA, MS_A, 3, MS_A, 2 },
55 /* 16 */ { 6, 6, MS_NA, MS_NA, MS_A, 2, MS_A, 2 },
56 /* 17 */ { 7, 7, MS_NA, MS_NA, MS_A, 1, 0, 2 },
57 /* 18 */ { 8, 8, MS_NA, MS_NA, 0, 0, 0, 2 },
58 /* 19 */ { 6, 2, MS_NA, 3, MS_B, 2, MS_C, 1 },
59 /* 20 */ { 6, 3, MS_NA, 3, MS_B, 2, MS_C, 1 },
60 /* 21 */ { 6, 4, MS_NA, 3, MS_B, 2, MS_C, 1 },
61 /* 22 */ { 6, 4, MS_NA, 2, MS_B, 2, MS_C, 1 },
62 /* 23 */ { 6, 6, MS_NA, 2, MS_B, 2, MS_C, 1 },
63 /* 24 */ { 8, 2, MS_NA, 3, MS_B, 2, MS_C, 1 },
64 /* 25 */ { 8, 3, MS_NA, 3, MS_B, 2, MS_C, 1 },
65 /* 26 */ { 8, 4, MS_NA, 3, MS_B, 2, MS_C, 1 },
66 /* 27 */ { 8, 4, MS_NA, 2, MS_B, 2, MS_C, 1 },
67 /* 28 */ { 8, 6, MS_NA, 2, MS_B, 2, MS_C, 1 },
68 /* 29 */ { 8, 8, MS_NA, 2, MS_B, 2, MS_C, 1 },
Max01bd0cc2018-01-23 20:58:49 +010069 /* 30 */ { 5, 1, 6, 2, 1, 1, 1, 1 },
70 /* 31 */ { 5, 2, 6, 2, 1, 1, 1, 1 },
71 /* 32 */ { 5, 3, 6, 2, 1, 1, 1, 1 },
72 /* 33 */ { 5, 4, 6, 2, 1, 1, 1, 1 },
73 /* 34 */ { 5, 5, 6, 2, 1, 1, 1, 1 },
74 /* 35 */ { 5, 1, 6, 2, 1, MS_TO, 1, 1 },
75 /* 36 */ { 5, 2, 6, 2, 1, MS_TO, 1, 1 },
76 /* 37 */ { 5, 3, 6, 2, 1, MS_TO, 1, 1 },
77 /* 38 */ { 5, 4, 6, 2, 1, MS_TO, 1, 1 },
78 /* 39 */ { 5, 5, 6, 2, 1, MS_TO, 1, 1 },
79 /* 40 */ { 6, 1, 7, 1, 1, 1, MS_TO, 1 },
80 /* 41 */ { 6, 2, 7, 1, 1, 1, MS_TO, 1 },
81 /* 42 */ { 6, 3, 7, 1, 1, 1, MS_TO, 1 },
82 /* 43 */ { 6, 4, 7, 1, 1, 1, MS_TO, 1 },
83 /* 44 */ { 6, 5, 7, 1, 1, 1, MS_TO, 1 },
84 /* 45 */ { 6, 6, 7, 1, 1, 1, MS_TO, 1 },
Max842d7812017-11-01 18:11:24 +010085};
86
87static inline const struct gprs_ms_multislot_class *get_mslot_table(uint8_t ms_cl)
88{
89 uint8_t index = ms_cl ? ms_cl : DEFAULT_MSLOT_CLASS;
90
91 if (ms_cl >= ARRAY_SIZE(gprs_ms_multislot_class))
92 index = 0;
93
94 return &gprs_ms_multislot_class[index];
95}
96
Max327e1212017-11-21 13:01:19 +010097uint8_t mslot_class_max()
98{
99 return ARRAY_SIZE(gprs_ms_multislot_class);
100}
101
Max842d7812017-11-01 18:11:24 +0100102uint8_t mslot_class_get_ta(uint8_t ms_cl)
103{
104 return get_mslot_table(ms_cl)->ta;
105}
106
107/* TODO: Set it to 1 if FH is implemented and enabled
108 * MS_A and MS_B are 0 iff FH is disabled and there is no Tx/Rx change.
109 * This is never the case with the current implementation, so 1 will always be used. */
110uint8_t mslot_class_get_tb(uint8_t ms_cl)
111{
112 const struct gprs_ms_multislot_class *t = get_mslot_table(ms_cl);
113
114 switch (t->tb) {
115 case MS_A:
116 return 0;
117 case MS_B:
118 return 1;
119 default:
120 return t->tb;
121 }
122}
123
Max01bd0cc2018-01-23 20:58:49 +0100124uint8_t mslot_class_get_ra(uint8_t ms_cl, uint8_t ta)
Max842d7812017-11-01 18:11:24 +0100125{
Max01bd0cc2018-01-23 20:58:49 +0100126 const struct gprs_ms_multislot_class *t = get_mslot_table(ms_cl);
127
128 switch (t->ra) {
129 case MS_TO:
130 return ta + 1;
131 default:
132 return t->ra;
133 }
Max842d7812017-11-01 18:11:24 +0100134}
135
Max01bd0cc2018-01-23 20:58:49 +0100136uint8_t mslot_class_get_rb(uint8_t ms_cl, uint8_t ta)
Max842d7812017-11-01 18:11:24 +0100137{
138 const struct gprs_ms_multislot_class *t = get_mslot_table(ms_cl);
139
140 switch (t->rb) {
141 case MS_A:
142 return 0;
143 case MS_C:
144 return 1;
Max01bd0cc2018-01-23 20:58:49 +0100145 case MS_TO:
146 return ta;
Max842d7812017-11-01 18:11:24 +0100147 default:
148 return t->rb;
149 }
150}
151
152uint8_t mslot_class_get_tx(uint8_t ms_cl)
153{
154 return get_mslot_table(ms_cl)->tx;
155}
156
157uint8_t mslot_class_get_rx(uint8_t ms_cl)
158{
159 return get_mslot_table(ms_cl)->rx;
160}
161
162uint8_t mslot_class_get_sum(uint8_t ms_cl)
163{
164 return get_mslot_table(ms_cl)->sum;
165}
166
167uint8_t mslot_class_get_type(uint8_t ms_cl)
168{
169 return get_mslot_table(ms_cl)->type;
170}
Maxf633b8d2018-01-31 15:28:53 +0100171
172/*! Fill in RX mask table for a given MS Class
173 *
174 * \param[in] ms_cl MS Class pointer
175 * \param[in] num_tx Number of TX slots to consider
176 * \param[out] rx_mask RX mask table
177 */
178void mslot_fill_rx_mask(uint8_t mslot_class, uint8_t num_tx, uint8_t *rx_mask)
179{
180 static const char *digit[10] = { "0","1","2","3","4","5","6","7","8","9" };
181 uint8_t Tx = mslot_class_get_tx(mslot_class), /* Max number of Tx slots */
182 Sum = mslot_class_get_sum(mslot_class), /* Max number of Tx + Rx slots */
183 Type = mslot_class_get_type(mslot_class), /* Type of Mobile */
184 Tta = mslot_class_get_ta(mslot_class), /* Minimum number of slots */
185 Ttb = mslot_class_get_tb(mslot_class),
186 /* FIXME: use actual TA offset for computation - make sure to adjust "1 + MS_TO" accordingly
187 see also "Offset required" bit in 3GPP TS 24.008 ยง10.5.1.7 */
188 Tra = mslot_class_get_ra(mslot_class, 0),
189 Trb = mslot_class_get_rb(mslot_class, 0);
190
191 if (num_tx == 1) /* it's enough to log this once per TX slot set iteration */
192 LOGP(DRLCMAC, LOGL_DEBUG,
193 "Rx=%d Tx=%d Sum Rx+Tx=%s, Tta=%s Ttb=%d, Tra=%d Trb=%d, Type=%d\n",
194 mslot_class_get_rx(mslot_class), Tx,
195 (Sum == MS_NA) ? "N/A" : digit[Sum],
196 (Tta == MS_NA) ? "N/A" : digit[Tta], Ttb, Tra, Trb, Type);
197
198 if (Type == 1) {
199 rx_mask[MASK_TT] = (0x100 >> OSMO_MAX(Ttb, Tta)) - 1;
200 rx_mask[MASK_TT] &= ~((1 << (Trb + num_tx)) - 1);
201 rx_mask[MASK_TR] = (0x100 >> Ttb) - 1;
202 rx_mask[MASK_TR] &= ~((1 << (OSMO_MAX(Trb, Tra) + num_tx)) - 1);
203 } else {
204 /* Class type 2 MS have independant RX and TX */
205 rx_mask[MASK_TT] = 0xff;
206 rx_mask[MASK_TR] = 0xff;
207 }
208
209 rx_mask[MASK_TT] = (rx_mask[MASK_TT] << 3) | (rx_mask[MASK_TT] >> 5);
210 rx_mask[MASK_TR] = (rx_mask[MASK_TR] << 3) | (rx_mask[MASK_TR] >> 5);
211}
Maxadca67b2018-01-31 15:22:36 +0100212
213/* look for USF, don't use USF=7 */
214int8_t find_free_usf(uint8_t usf_map)
215{
216 uint8_t usf;
217
218 if (usf_map == (1 << 7) - 1)
219 return -1;
220
221 for (usf = 0; usf < 7; usf++) {
222 if (!(usf_map & (1 << usf)))
223 return usf;
224 }
225
226 return -1;
227}
Max0cc72122018-01-31 17:00:06 +0100228
Maxc5407c72018-02-05 16:11:36 +0100229/* look for USF, don't use USF=7 */
230int8_t find_free_tfi(uint32_t tfi_map)
231{
232 int8_t tfi;
233
234 if (tfi_map == NO_FREE_TFI)
235 return -1;
236
237 for (tfi = 0; tfi < 32; tfi++) {
Neels Hofmeyr8b4bd462018-03-26 23:24:14 +0200238 if (!(tfi_map & (((uint32_t)1) << tfi)))
Maxc5407c72018-02-05 16:11:36 +0100239 return tfi;
240 }
241
242 return -1;
243}
244
Max0cc72122018-01-31 17:00:06 +0100245void masked_override_with(char *buf, uint8_t mask, char set_char)
246{
247 int i;
248 for (i = 0; mask; i++, mask >>= 1)
249 if (mask & 1)
250 buf[i] = set_char;
251}
Max77988d42018-02-19 18:00:38 +0100252
253void ts_format(char *buf, uint8_t dl_mask, uint8_t ul_mask)
254{
255 snprintf(buf, 9, OSMO_BIT_SPEC, OSMO_BIT_PRINT_EX(dl_mask, 'D'));
256 masked_override_with(buf, ul_mask, 'U');
257 masked_override_with(buf, ul_mask & dl_mask, 'C');
258}
Max731e2bb2018-02-05 16:15:30 +0100259
260uint16_t mslot_wrap_window(uint16_t win)
261{
262 return (win | win >> 8) & 0xFF;
263}
264
265bool mslot_test_and_set_bit(uint32_t *bits, size_t elem)
266{
Neels Hofmeyrf7538142018-03-26 23:24:14 +0200267 bool was_set = bits[elem/32] & (((uint32_t)1) << (elem % 32));
268 bits[elem/32] |= (((uint32_t)1) << (elem % 32));
Max731e2bb2018-02-05 16:15:30 +0100269
270 return was_set;
271}
272
273/*! Filter out bad slots
274 *
275 * \param[in] mask TS selection mask
276 * \param[in] ul_slots set of UL timeslots
277 * \param[in] dl_slots set of DL timeslots
278 * \param[in] rx_valid_win Mask for valid RX window value
279 * \returns negative error code or RX window on success
280 */
281int16_t mslot_filter_bad(uint8_t mask, uint8_t ul_slots, uint8_t dl_slots, uint16_t rx_valid_win)
282{
283 uint8_t rx_good;
284 uint16_t rx_bad = (uint16_t)(0xFF & ~mask) << ul_slots;
285
286 /* TODO: CHECK this calculation -> separate function for unit testing */
287 rx_bad = (rx_bad | (rx_bad >> 8)) & 0xFF;
288 rx_good = dl_slots & ~rx_bad;
289 if (!rx_good)
290 return -1;
291
292 return rx_good & rx_valid_win;
293}