blob: 6848d97a3a2c6b67cd20eeb2abc1065ffb85cef6 [file] [log] [blame]
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +01001/* pdch_ul_controller.c
2 *
3 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (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 General Public License
17 * along with it program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <unistd.h>
22#include <talloc.h>
23
24#include "pdch_ul_controller.h"
25#include "bts.h"
26#include "sba.h"
27#include "pdch.h"
Pau Espin Pedrol99360a32021-03-09 17:18:12 +010028#include "pcu_utils.h"
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +010029#include "tbf_ul.h"
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010030
31/* TS 44.060 Table 10.4.5.1 states maximum RRBP is N + 26. Give extra space for time diff between Tx and Rx? */
32#define MAX_FN_RESERVED (27 + 50)
33
34const struct value_string pdch_ul_node_names[] = {
35 { PDCH_ULC_NODE_TBF_USF, "USF" },
36 { PDCH_ULC_NODE_TBF_POLL, "POLL" },
37 { PDCH_ULC_NODE_SBA, "SBA" },
38 { 0, NULL }
39};
40
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010041#define GSM_MAX_FN_THRESH (GSM_MAX_FN >> 1)
42/* 0: equal, -1: fn1 BEFORE fn2, 1: fn1 AFTER fn2 */
43static inline int fn_cmp(uint32_t fn1, uint32_t fn2)
44{
45 if (fn1 == fn2)
46 return 0;
47 /* FN1 goes before FN2: */
48 if ((fn1 < fn2 && (fn2 - fn1) < GSM_MAX_FN_THRESH) ||
49 (fn1 > fn2 && (fn1 - fn2) > GSM_MAX_FN_THRESH))
50 return -1;
51 /* FN1 goes after FN2: */
52 return 1;
53}
54
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010055struct pdch_ulc *pdch_ulc_alloc(struct gprs_rlcmac_pdch *pdch, void *ctx)
56{
57 struct pdch_ulc* ulc;
58 ulc = talloc_zero(ctx, struct pdch_ulc);
59 if (!ulc)
60 return ulc;
61
62 ulc->pdch = pdch;
63 ulc->pool_ctx = talloc_pool(ulc, sizeof(struct pdch_ulc_node) * MAX_FN_RESERVED);
64 return ulc;
65}
66
67struct pdch_ulc_node *pdch_ulc_get_node(struct pdch_ulc *ulc, uint32_t fn)
68{
Pau Espin Pedrol222f6742021-03-29 11:29:22 +020069 struct rb_node *node = ulc->tree_root.rb_node;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010070 struct pdch_ulc_node *it;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010071 int res;
Pau Espin Pedrol222f6742021-03-29 11:29:22 +020072
73 while (node) {
74 it = rb_entry(node, struct pdch_ulc_node, node);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010075 res = fn_cmp(it->fn, fn);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010076 if (res > 0) /* it->fn AFTER fn */
Pau Espin Pedrol222f6742021-03-29 11:29:22 +020077 node = node->rb_left;
78 else if (res < 0) /* it->fn BEFORE fn */
79 node = node->rb_right;
80 else /* it->fn == fn */
81 return it;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010082 }
83 return NULL;
84}
85struct pdch_ulc_node *pdch_ulc_pop_node(struct pdch_ulc *ulc, uint32_t fn)
86{
87 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
88 if (!item)
89 return NULL;
90 rb_erase(&item->node, &ulc->tree_root);
91 return item;
92}
93
94struct gprs_rlcmac_sba *pdch_ulc_get_sba(struct pdch_ulc *ulc, uint32_t fn)
95{
96 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
97 if (!item || item->type != PDCH_ULC_NODE_SBA)
98 return NULL;
99 return item->sba.sba;
100}
101
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100102struct gprs_rlcmac_tbf *pdch_ulc_get_tbf_poll(struct pdch_ulc *ulc, uint32_t fn)
103{
104 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
105 if (!item || item->type != PDCH_ULC_NODE_TBF_POLL)
106 return NULL;
107 return item->tbf_poll.poll_tbf;
108}
109
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100110bool pdch_ulc_fn_is_free(struct pdch_ulc *ulc, uint32_t fn)
111{
112 return !pdch_ulc_get_node(ulc, fn);
113}
114
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200115struct rrbp_opt {
116 uint8_t offset;
117 enum rrbp_field coding;
118};
119
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100120int pdch_ulc_get_next_free_rrbp_fn(struct pdch_ulc *ulc, uint32_t fn, uint32_t *poll_fn, unsigned int *rrbp)
121{
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200122 uint8_t i;
123 static const struct rrbp_opt rrbp_list[] = {
124 { 13, RRBP_N_plus_13 },
125 { 17, RRBP_N_plus_17_18 },
126 { 18, RRBP_N_plus_17_18 },
127 { 21, RRBP_N_plus_21_22 },
128 { 22, RRBP_N_plus_21_22 },
129 { 26, RRBP_N_plus_26 },
130 };
131
132 for (i = 0; i < ARRAY_SIZE(rrbp_list); i++) {
133 uint32_t new_poll_fn = next_fn(fn, rrbp_list[i].offset);
134 if (!fn_valid(new_poll_fn))
135 continue;
136 if (pdch_ulc_fn_is_free(ulc, new_poll_fn)) {
137 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_DEBUG, "POLL scheduled at FN %" PRIu32
138 " + %" PRIu8 " = %" PRIu32 "\n",
139 fn, rrbp_list[i].offset, new_poll_fn);
140 *poll_fn = new_poll_fn;
141 *rrbp = (unsigned int)rrbp_list[i].coding;
142 return 0;
143 }
144 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_DEBUG, "UL block already scheduled at FN %" PRIu32
145 " + %" PRIu8 " = %" PRIu32 "\n",
146 fn, rrbp_list[i].offset, new_poll_fn);
147
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100148 }
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200149 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR, "FN=%" PRIu32 " "
150 "Failed allocating POLL, all RRBP values are already reserved!\n", fn);
151 return -EBUSY;
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100152}
153
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200154/* Get next free (unreserved) FN which is not located in time before "start_fn" */
155uint32_t pdch_ulc_get_next_free_fn(struct pdch_ulc *ulc, uint32_t start_fn)
156{
157 struct rb_node *node;
158 struct pdch_ulc_node *it;
159 int res;
160 uint32_t check_fn = start_fn;
161
162 for (node = rb_first(&ulc->tree_root); node; node = rb_next(node)) {
163 it = container_of(node, struct pdch_ulc_node, node);
164 res = fn_cmp(it->fn, check_fn);
165 if (res > 0) { /* it->fn AFTER check_fn */
166 /* Next reserved FN is passed check_fn, hence it means check_fn is free */
167 return check_fn;
168 }
169 /* if it->fn < check_fn, simply continue iterating, we want to reach at least check_fn */
170 if (res == 0)/* it->fn == fn */
171 check_fn = fn_next_block(check_fn);
172 /* if it->fn < check_fn, simply continue iterating, we want to reach at least check_fn */
173 }
174 return check_fn;
175}
176
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100177static struct pdch_ulc_node *_alloc_node(struct pdch_ulc *ulc, uint32_t fn)
178{
179 struct pdch_ulc_node *node;
180 node = talloc_zero(ulc->pool_ctx, struct pdch_ulc_node);
181 node->fn = fn;
182 return node;
183}
184
185static int pdch_ulc_add_node(struct pdch_ulc *ulc, struct pdch_ulc_node *item)
186{
187 struct rb_node **n = &(ulc->tree_root.rb_node);
188 struct rb_node *parent = NULL;
189
190 while (*n) {
191 struct pdch_ulc_node *it;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100192 int res;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100193
194 it = container_of(*n, struct pdch_ulc_node, node);
195
196 parent = *n;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100197 res = fn_cmp(item->fn, it->fn);
198 if (res < 0) { /* item->fn "BEFORE" it->fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100199 n = &((*n)->rb_left);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100200 } else if (res > 0) { /* item->fn "AFTER" it->fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100201 n = &((*n)->rb_right);
202 } else {
203 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR,
204 "Trying to reserve already reserved FN %u\n",
205 item->fn);
206 return -EEXIST;
207 }
208 }
209
210 rb_link_node(&item->node, parent, n);
211 rb_insert_color(&item->node, &ulc->tree_root);
212 return 0;
213}
214
215int pdch_ulc_reserve_tbf_usf(struct pdch_ulc *ulc, uint32_t fn, struct gprs_rlcmac_ul_tbf *ul_tbf)
216{
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100217 struct pdch_ulc_node *item = _alloc_node(ulc, fn);
218 item->type = PDCH_ULC_NODE_TBF_USF;
219 item->tbf_usf.ul_tbf = ul_tbf;
220 return pdch_ulc_add_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100221}
222
223int pdch_ulc_reserve_tbf_poll(struct pdch_ulc *ulc, uint32_t fn, struct gprs_rlcmac_tbf *tbf)
224{
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100225 struct pdch_ulc_node *item = _alloc_node(ulc, fn);
226 item->type = PDCH_ULC_NODE_TBF_POLL;
227 item->tbf_poll.poll_tbf = tbf;
228 return pdch_ulc_add_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100229}
230
231int pdch_ulc_reserve_sba(struct pdch_ulc *ulc, struct gprs_rlcmac_sba *sba)
232{
233 struct pdch_ulc_node *item = _alloc_node(ulc, sba->fn);
234 item->type = PDCH_ULC_NODE_SBA;
235 item->sba.sba = sba;
236 return pdch_ulc_add_node(ulc, item);
237}
238
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100239void pdch_ulc_release_node(struct pdch_ulc *ulc, struct pdch_ulc_node *item)
240{
241 rb_erase(&item->node, &ulc->tree_root);
242 talloc_free(item);
243}
244
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100245int pdch_ulc_release_fn(struct pdch_ulc *ulc, uint32_t fn)
246{
247 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
248 if (!item)
249 return -ENOKEY;
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100250 pdch_ulc_release_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100251 return 0;
252}
253
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100254void pdch_ulc_release_tbf(struct pdch_ulc *ulc, const struct gprs_rlcmac_tbf *tbf)
255{
256 bool tree_modified;
257 do {
258 struct rb_node *node;
259 struct pdch_ulc_node *item;
260 const struct gprs_rlcmac_tbf *item_tbf;
261
262 tree_modified = false;
263 for (node = rb_first(&ulc->tree_root); node; node = rb_next(node)) {
264 item = container_of(node, struct pdch_ulc_node, node);
265 switch (item->type) {
266 case PDCH_ULC_NODE_SBA:
267 continue;
268 case PDCH_ULC_NODE_TBF_POLL:
269 item_tbf = item->tbf_poll.poll_tbf;
270 break;
271 case PDCH_ULC_NODE_TBF_USF:
272 item_tbf = (struct gprs_rlcmac_tbf *)item->tbf_usf.ul_tbf;
273 break;
Harald Welted7f05582021-03-20 16:17:30 +0100274 default:
275 OSMO_ASSERT(0);
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100276 }
277 if (item_tbf != tbf)
278 continue;
279 /* One entry found, remove it from tree and restart
280 * search from start (to avoid traverse continue from
281 * no-more existant node */
282 tree_modified = true;
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100283 pdch_ulc_release_node(ulc, item);
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100284 break;
285 }
286 } while (tree_modified);
287}
288
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100289void pdch_ulc_expire_fn(struct pdch_ulc *ulc, uint32_t fn)
290{
291 struct gprs_rlcmac_sba *sba;
292 struct pdch_ulc_node *item;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100293 int res;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100294
295 struct rb_node *first;
296 while((first = rb_first(&ulc->tree_root))) {
297 item = container_of(first, struct pdch_ulc_node, node);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100298 res = fn_cmp(item->fn, fn);
299 if (res > 0) /* item->fn AFTER fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100300 break;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100301 if (res < 0) { /* item->fn BEFORE fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100302 /* Sanity check: */
303 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR,
304 "Expiring FN=%" PRIu32 " but previous FN=%" PRIu32 " is still reserved!\n",
305 fn, item->fn);
306 }
307 rb_erase(&item->node, &ulc->tree_root);
308
309 switch (item->type) {
310 case PDCH_ULC_NODE_TBF_USF:
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100311 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_INFO,
312 "Timeout for registered USF (FN=%u): %s\n",
313 item->fn, tbf_name((struct gprs_rlcmac_tbf *)item->tbf_usf.ul_tbf));
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +0100314 tbf_usf_timeout(item->tbf_usf.ul_tbf);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100315 break;
316 case PDCH_ULC_NODE_TBF_POLL:
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100317 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_NOTICE,
318 "Timeout for registered POLL (FN=%u): %s\n",
319 item->fn, tbf_name(item->tbf_poll.poll_tbf));
320 tbf_poll_timeout(item->tbf_poll.poll_tbf);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100321 break;
322 case PDCH_ULC_NODE_SBA:
323 sba = item->sba.sba;
324 LOGPDCH(sba->pdch, DRLCMAC, LOGL_NOTICE,
325 "Timeout for registered SBA (FN=%u, TA=%u)\n",
326 sba->fn, sba->ta);
327 sba_timeout(sba);
328 break;
329 }
330
331 talloc_free(item);
332 }
333}