blob: f8089ffbfdf586eedb4ee3db8fe36cb21e0d989d [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
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200223int pdch_ulc_reserve_tbf_poll(struct pdch_ulc *ulc, uint32_t fn, struct gprs_rlcmac_tbf *tbf, enum pdch_ulc_tbf_poll_reason reason)
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100224{
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;
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200228 item->tbf_poll.reason = reason;
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100229 return pdch_ulc_add_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100230}
231
232int pdch_ulc_reserve_sba(struct pdch_ulc *ulc, struct gprs_rlcmac_sba *sba)
233{
234 struct pdch_ulc_node *item = _alloc_node(ulc, sba->fn);
235 item->type = PDCH_ULC_NODE_SBA;
236 item->sba.sba = sba;
237 return pdch_ulc_add_node(ulc, item);
238}
239
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100240void pdch_ulc_release_node(struct pdch_ulc *ulc, struct pdch_ulc_node *item)
241{
242 rb_erase(&item->node, &ulc->tree_root);
243 talloc_free(item);
244}
245
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100246int pdch_ulc_release_fn(struct pdch_ulc *ulc, uint32_t fn)
247{
248 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
249 if (!item)
250 return -ENOKEY;
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100251 pdch_ulc_release_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100252 return 0;
253}
254
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100255void pdch_ulc_release_tbf(struct pdch_ulc *ulc, const struct gprs_rlcmac_tbf *tbf)
256{
257 bool tree_modified;
258 do {
259 struct rb_node *node;
260 struct pdch_ulc_node *item;
261 const struct gprs_rlcmac_tbf *item_tbf;
262
263 tree_modified = false;
264 for (node = rb_first(&ulc->tree_root); node; node = rb_next(node)) {
265 item = container_of(node, struct pdch_ulc_node, node);
266 switch (item->type) {
267 case PDCH_ULC_NODE_SBA:
268 continue;
269 case PDCH_ULC_NODE_TBF_POLL:
270 item_tbf = item->tbf_poll.poll_tbf;
271 break;
272 case PDCH_ULC_NODE_TBF_USF:
273 item_tbf = (struct gprs_rlcmac_tbf *)item->tbf_usf.ul_tbf;
274 break;
Harald Welted7f05582021-03-20 16:17:30 +0100275 default:
276 OSMO_ASSERT(0);
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100277 }
278 if (item_tbf != tbf)
279 continue;
280 /* One entry found, remove it from tree and restart
281 * search from start (to avoid traverse continue from
282 * no-more existant node */
283 tree_modified = true;
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100284 pdch_ulc_release_node(ulc, item);
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100285 break;
286 }
287 } while (tree_modified);
288}
289
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100290void pdch_ulc_expire_fn(struct pdch_ulc *ulc, uint32_t fn)
291{
292 struct gprs_rlcmac_sba *sba;
293 struct pdch_ulc_node *item;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100294 int res;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100295
296 struct rb_node *first;
297 while((first = rb_first(&ulc->tree_root))) {
298 item = container_of(first, struct pdch_ulc_node, node);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100299 res = fn_cmp(item->fn, fn);
300 if (res > 0) /* item->fn AFTER fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100301 break;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100302 if (res < 0) { /* item->fn BEFORE fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100303 /* Sanity check: */
304 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR,
305 "Expiring FN=%" PRIu32 " but previous FN=%" PRIu32 " is still reserved!\n",
306 fn, item->fn);
307 }
308 rb_erase(&item->node, &ulc->tree_root);
309
310 switch (item->type) {
311 case PDCH_ULC_NODE_TBF_USF:
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100312 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_INFO,
313 "Timeout for registered USF (FN=%u): %s\n",
314 item->fn, tbf_name((struct gprs_rlcmac_tbf *)item->tbf_usf.ul_tbf));
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +0100315 tbf_usf_timeout(item->tbf_usf.ul_tbf);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100316 break;
317 case PDCH_ULC_NODE_TBF_POLL:
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100318 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_NOTICE,
319 "Timeout for registered POLL (FN=%u): %s\n",
320 item->fn, tbf_name(item->tbf_poll.poll_tbf));
Pau Espin Pedrol16e16782021-03-29 19:10:19 +0200321 tbf_poll_timeout(item->tbf_poll.poll_tbf, ulc->pdch, item->fn, item->tbf_poll.reason);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100322 break;
323 case PDCH_ULC_NODE_SBA:
324 sba = item->sba.sba;
325 LOGPDCH(sba->pdch, DRLCMAC, LOGL_NOTICE,
326 "Timeout for registered SBA (FN=%u, TA=%u)\n",
327 sba->fn, sba->ta);
328 sba_timeout(sba);
329 break;
330 }
331
332 talloc_free(item);
333 }
334}