blob: e7a721c036714b0c6b8b12a97e976bde63bb1ad3 [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 Pedrolc6e911c2021-06-07 18:13:23 +020041const struct value_string pdch_ulc_tbf_poll_reason_names[] = {
42 { PDCH_ULC_POLL_UL_ASS, "UL_ASS" },
43 { PDCH_ULC_POLL_DL_ASS, "DL_ASS" },
44 { PDCH_ULC_POLL_UL_ACK, "UL_ACK" },
45 { PDCH_ULC_POLL_DL_ACK, "DL_ACK" },
46 { PDCH_ULC_POLL_CELL_CHG_CONTINUE, "CELL_CHG_CONTINUE" },
47 { 0, NULL }
48};
49
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010050#define GSM_MAX_FN_THRESH (GSM_MAX_FN >> 1)
51/* 0: equal, -1: fn1 BEFORE fn2, 1: fn1 AFTER fn2 */
52static inline int fn_cmp(uint32_t fn1, uint32_t fn2)
53{
54 if (fn1 == fn2)
55 return 0;
56 /* FN1 goes before FN2: */
57 if ((fn1 < fn2 && (fn2 - fn1) < GSM_MAX_FN_THRESH) ||
58 (fn1 > fn2 && (fn1 - fn2) > GSM_MAX_FN_THRESH))
59 return -1;
60 /* FN1 goes after FN2: */
61 return 1;
62}
63
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010064struct pdch_ulc *pdch_ulc_alloc(struct gprs_rlcmac_pdch *pdch, void *ctx)
65{
66 struct pdch_ulc* ulc;
67 ulc = talloc_zero(ctx, struct pdch_ulc);
68 if (!ulc)
69 return ulc;
70
71 ulc->pdch = pdch;
72 ulc->pool_ctx = talloc_pool(ulc, sizeof(struct pdch_ulc_node) * MAX_FN_RESERVED);
73 return ulc;
74}
75
76struct pdch_ulc_node *pdch_ulc_get_node(struct pdch_ulc *ulc, uint32_t fn)
77{
Pau Espin Pedrol222f6742021-03-29 11:29:22 +020078 struct rb_node *node = ulc->tree_root.rb_node;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010079 struct pdch_ulc_node *it;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010080 int res;
Pau Espin Pedrol222f6742021-03-29 11:29:22 +020081
82 while (node) {
83 it = rb_entry(node, struct pdch_ulc_node, node);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010084 res = fn_cmp(it->fn, fn);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +010085 if (res > 0) /* it->fn AFTER fn */
Pau Espin Pedrol222f6742021-03-29 11:29:22 +020086 node = node->rb_left;
87 else if (res < 0) /* it->fn BEFORE fn */
88 node = node->rb_right;
89 else /* it->fn == fn */
90 return it;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010091 }
92 return NULL;
93}
94struct pdch_ulc_node *pdch_ulc_pop_node(struct pdch_ulc *ulc, uint32_t fn)
95{
96 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
97 if (!item)
98 return NULL;
99 rb_erase(&item->node, &ulc->tree_root);
100 return item;
101}
102
103struct gprs_rlcmac_sba *pdch_ulc_get_sba(struct pdch_ulc *ulc, uint32_t fn)
104{
105 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
106 if (!item || item->type != PDCH_ULC_NODE_SBA)
107 return NULL;
108 return item->sba.sba;
109}
110
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100111struct gprs_rlcmac_tbf *pdch_ulc_get_tbf_poll(struct pdch_ulc *ulc, uint32_t fn)
112{
113 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
114 if (!item || item->type != PDCH_ULC_NODE_TBF_POLL)
115 return NULL;
116 return item->tbf_poll.poll_tbf;
117}
118
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100119bool pdch_ulc_fn_is_free(struct pdch_ulc *ulc, uint32_t fn)
120{
121 return !pdch_ulc_get_node(ulc, fn);
122}
123
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200124struct rrbp_opt {
125 uint8_t offset;
126 enum rrbp_field coding;
127};
128
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100129int pdch_ulc_get_next_free_rrbp_fn(struct pdch_ulc *ulc, uint32_t fn, uint32_t *poll_fn, unsigned int *rrbp)
130{
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200131 uint8_t i;
132 static const struct rrbp_opt rrbp_list[] = {
133 { 13, RRBP_N_plus_13 },
134 { 17, RRBP_N_plus_17_18 },
135 { 18, RRBP_N_plus_17_18 },
136 { 21, RRBP_N_plus_21_22 },
137 { 22, RRBP_N_plus_21_22 },
138 { 26, RRBP_N_plus_26 },
139 };
140
141 for (i = 0; i < ARRAY_SIZE(rrbp_list); i++) {
142 uint32_t new_poll_fn = next_fn(fn, rrbp_list[i].offset);
143 if (!fn_valid(new_poll_fn))
144 continue;
145 if (pdch_ulc_fn_is_free(ulc, new_poll_fn)) {
146 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_DEBUG, "POLL scheduled at FN %" PRIu32
147 " + %" PRIu8 " = %" PRIu32 "\n",
148 fn, rrbp_list[i].offset, new_poll_fn);
149 *poll_fn = new_poll_fn;
150 *rrbp = (unsigned int)rrbp_list[i].coding;
151 return 0;
152 }
153 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_DEBUG, "UL block already scheduled at FN %" PRIu32
154 " + %" PRIu8 " = %" PRIu32 "\n",
155 fn, rrbp_list[i].offset, new_poll_fn);
156
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100157 }
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +0200158 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR, "FN=%" PRIu32 " "
159 "Failed allocating POLL, all RRBP values are already reserved!\n", fn);
160 return -EBUSY;
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100161}
162
Pau Espin Pedrolce3bd252021-03-29 12:13:13 +0200163/* Get next free (unreserved) FN which is not located in time before "start_fn" */
164uint32_t pdch_ulc_get_next_free_fn(struct pdch_ulc *ulc, uint32_t start_fn)
165{
166 struct rb_node *node;
167 struct pdch_ulc_node *it;
168 int res;
169 uint32_t check_fn = start_fn;
170
171 for (node = rb_first(&ulc->tree_root); node; node = rb_next(node)) {
172 it = container_of(node, struct pdch_ulc_node, node);
173 res = fn_cmp(it->fn, check_fn);
174 if (res > 0) { /* it->fn AFTER check_fn */
175 /* Next reserved FN is passed check_fn, hence it means check_fn is free */
176 return check_fn;
177 }
178 /* if it->fn < check_fn, simply continue iterating, we want to reach at least check_fn */
179 if (res == 0)/* it->fn == fn */
180 check_fn = fn_next_block(check_fn);
181 /* if it->fn < check_fn, simply continue iterating, we want to reach at least check_fn */
182 }
183 return check_fn;
184}
185
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100186static struct pdch_ulc_node *_alloc_node(struct pdch_ulc *ulc, uint32_t fn)
187{
188 struct pdch_ulc_node *node;
189 node = talloc_zero(ulc->pool_ctx, struct pdch_ulc_node);
190 node->fn = fn;
191 return node;
192}
193
194static int pdch_ulc_add_node(struct pdch_ulc *ulc, struct pdch_ulc_node *item)
195{
196 struct rb_node **n = &(ulc->tree_root.rb_node);
197 struct rb_node *parent = NULL;
198
199 while (*n) {
200 struct pdch_ulc_node *it;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100201 int res;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100202
203 it = container_of(*n, struct pdch_ulc_node, node);
204
205 parent = *n;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100206 res = fn_cmp(item->fn, it->fn);
207 if (res < 0) { /* item->fn "BEFORE" it->fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100208 n = &((*n)->rb_left);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100209 } else if (res > 0) { /* item->fn "AFTER" it->fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100210 n = &((*n)->rb_right);
211 } else {
212 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR,
213 "Trying to reserve already reserved FN %u\n",
214 item->fn);
215 return -EEXIST;
216 }
217 }
218
219 rb_link_node(&item->node, parent, n);
220 rb_insert_color(&item->node, &ulc->tree_root);
221 return 0;
222}
223
224int pdch_ulc_reserve_tbf_usf(struct pdch_ulc *ulc, uint32_t fn, struct gprs_rlcmac_ul_tbf *ul_tbf)
225{
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100226 struct pdch_ulc_node *item = _alloc_node(ulc, fn);
227 item->type = PDCH_ULC_NODE_TBF_USF;
228 item->tbf_usf.ul_tbf = ul_tbf;
229 return pdch_ulc_add_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100230}
231
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200232int 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 +0100233{
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100234 struct pdch_ulc_node *item = _alloc_node(ulc, fn);
235 item->type = PDCH_ULC_NODE_TBF_POLL;
236 item->tbf_poll.poll_tbf = tbf;
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200237 item->tbf_poll.reason = reason;
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100238 return pdch_ulc_add_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100239}
240
241int pdch_ulc_reserve_sba(struct pdch_ulc *ulc, struct gprs_rlcmac_sba *sba)
242{
243 struct pdch_ulc_node *item = _alloc_node(ulc, sba->fn);
244 item->type = PDCH_ULC_NODE_SBA;
245 item->sba.sba = sba;
246 return pdch_ulc_add_node(ulc, item);
247}
248
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100249void pdch_ulc_release_node(struct pdch_ulc *ulc, struct pdch_ulc_node *item)
250{
251 rb_erase(&item->node, &ulc->tree_root);
252 talloc_free(item);
253}
254
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100255int pdch_ulc_release_fn(struct pdch_ulc *ulc, uint32_t fn)
256{
257 struct pdch_ulc_node *item = pdch_ulc_get_node(ulc, fn);
258 if (!item)
259 return -ENOKEY;
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100260 pdch_ulc_release_node(ulc, item);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100261 return 0;
262}
263
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100264void pdch_ulc_release_tbf(struct pdch_ulc *ulc, const struct gprs_rlcmac_tbf *tbf)
265{
266 bool tree_modified;
267 do {
268 struct rb_node *node;
269 struct pdch_ulc_node *item;
270 const struct gprs_rlcmac_tbf *item_tbf;
271
272 tree_modified = false;
273 for (node = rb_first(&ulc->tree_root); node; node = rb_next(node)) {
274 item = container_of(node, struct pdch_ulc_node, node);
275 switch (item->type) {
276 case PDCH_ULC_NODE_SBA:
277 continue;
278 case PDCH_ULC_NODE_TBF_POLL:
279 item_tbf = item->tbf_poll.poll_tbf;
280 break;
281 case PDCH_ULC_NODE_TBF_USF:
282 item_tbf = (struct gprs_rlcmac_tbf *)item->tbf_usf.ul_tbf;
283 break;
Harald Welted7f05582021-03-20 16:17:30 +0100284 default:
285 OSMO_ASSERT(0);
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100286 }
287 if (item_tbf != tbf)
288 continue;
289 /* One entry found, remove it from tree and restart
290 * search from start (to avoid traverse continue from
291 * no-more existant node */
292 tree_modified = true;
Pau Espin Pedrolade9c2f2021-03-24 14:02:49 +0100293 pdch_ulc_release_node(ulc, item);
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100294 break;
295 }
296 } while (tree_modified);
297}
298
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100299void pdch_ulc_expire_fn(struct pdch_ulc *ulc, uint32_t fn)
300{
301 struct gprs_rlcmac_sba *sba;
302 struct pdch_ulc_node *item;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100303 int res;
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100304
305 struct rb_node *first;
306 while((first = rb_first(&ulc->tree_root))) {
307 item = container_of(first, struct pdch_ulc_node, node);
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100308 res = fn_cmp(item->fn, fn);
309 if (res > 0) /* item->fn AFTER fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100310 break;
Pau Espin Pedrolc7cc4162021-03-12 18:24:57 +0100311 if (res < 0) { /* item->fn BEFORE fn */
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100312 /* Sanity check: */
313 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_ERROR,
314 "Expiring FN=%" PRIu32 " but previous FN=%" PRIu32 " is still reserved!\n",
315 fn, item->fn);
316 }
317 rb_erase(&item->node, &ulc->tree_root);
318
319 switch (item->type) {
320 case PDCH_ULC_NODE_TBF_USF:
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100321 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_INFO,
322 "Timeout for registered USF (FN=%u): %s\n",
323 item->fn, tbf_name((struct gprs_rlcmac_tbf *)item->tbf_usf.ul_tbf));
Pau Espin Pedrol0b998b12021-03-24 14:45:43 +0100324 tbf_usf_timeout(item->tbf_usf.ul_tbf);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100325 break;
326 case PDCH_ULC_NODE_TBF_POLL:
Pau Espin Pedrol99360a32021-03-09 17:18:12 +0100327 LOGPDCH(ulc->pdch, DRLCMAC, LOGL_NOTICE,
328 "Timeout for registered POLL (FN=%u): %s\n",
329 item->fn, tbf_name(item->tbf_poll.poll_tbf));
Pau Espin Pedrol16e16782021-03-29 19:10:19 +0200330 tbf_poll_timeout(item->tbf_poll.poll_tbf, ulc->pdch, item->fn, item->tbf_poll.reason);
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +0100331 break;
332 case PDCH_ULC_NODE_SBA:
333 sba = item->sba.sba;
334 LOGPDCH(sba->pdch, DRLCMAC, LOGL_NOTICE,
335 "Timeout for registered SBA (FN=%u, TA=%u)\n",
336 sba->fn, sba->ta);
337 sba_timeout(sba);
338 break;
339 }
340
341 talloc_free(item);
342 }
343}