blob: a0b1c036fbaaba566cd707d0e2085360fb3808c1 [file] [log] [blame]
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +01001/* tbf_dl_fsm.c
2 *
3 * Copyright (C) 2021-2022 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
17#include <unistd.h>
18
19#include <talloc.h>
20
21#include <tbf_fsm.h>
22#include <gprs_rlcmac.h>
23#include <gprs_debug.h>
24#include <gprs_ms.h>
25#include <encoding.h>
26#include <bts.h>
27
28#include <bts_pch_timer.h>
29
30#define X(s) (1 << (s))
31
Pau Espin Pedrol76ef6662023-01-27 13:19:55 +010032static const struct osmo_tdef_state_timeout tbf_dl_fsm_timeouts[32] = {
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010033 [TBF_ST_NEW] = {},
34 [TBF_ST_ASSIGN] = { },
35 [TBF_ST_FLOW] = { },
36 [TBF_ST_FINISHED] = {},
37 [TBF_ST_WAIT_RELEASE] = {},
38 [TBF_ST_RELEASING] = {},
39};
40
41/* Transition to a state, using the T timer defined in tbf_fsm_timeouts.
42 * The actual timeout value is in turn obtained from conn->T_defs.
43 * Assumes local variable fi exists. */
44#define tbf_dl_fsm_state_chg(fi, NEXT_STATE) \
45 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
46 tbf_dl_fsm_timeouts, \
47 the_pcu->T_defs, \
48 -1)
49
50static void mod_ass_type(struct tbf_dl_fsm_ctx *ctx, uint8_t t, bool set)
51{
52 const char *ch = "UNKNOWN";
53 bool prev_set = ctx->state_flags & (1 << t);
54
55 switch (t) {
56 case GPRS_RLCMAC_FLAG_CCCH:
57 ch = "CCCH";
58 break;
59 case GPRS_RLCMAC_FLAG_PACCH:
60 ch = "PACCH";
61 break;
62 default:
Pau Espin Pedrol83a08922022-12-12 19:46:01 +010063 LOGPTBFDL(ctx->dl_tbf, LOGL_ERROR,
64 "attempted to %sset unexpected ass. type %d - FIXME!\n",
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010065 set ? "" : "un", t);
66 return;
67 }
68
69 if (set && prev_set)
Pau Espin Pedrol83a08922022-12-12 19:46:01 +010070 LOGPTBFDL(ctx->dl_tbf, LOGL_ERROR,
71 "attempted to set ass. type %s which is already set.\n", ch);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010072 else if (!set && !prev_set)
73 return;
74
Pau Espin Pedrol83a08922022-12-12 19:46:01 +010075 LOGPTBFDL(ctx->dl_tbf, LOGL_INFO, "%sset ass. type %s [prev CCCH:%u, PACCH:%u]\n",
76 set ? "" : "un", ch,
77 !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)),
78 !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)));
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010079
80 if (set) {
81 ctx->state_flags |= (1 << t);
82 } else {
83 ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep to flags */
84 ctx->state_flags &= ~(1 << t);
85 }
86}
87
88
89static void st_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
90{
91 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
92 switch (event) {
93 case TBF_EV_ASSIGN_ADD_CCCH:
94 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true);
95 tbf_dl_fsm_state_chg(fi, TBF_ST_ASSIGN);
96 break;
97 case TBF_EV_ASSIGN_ADD_PACCH:
98 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
99 tbf_dl_fsm_state_chg(fi, TBF_ST_ASSIGN);
100 break;
101 default:
102 OSMO_ASSERT(0);
103 }
104}
105
106static void st_assign_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
107{
108 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
109 unsigned long val;
110 unsigned int sec, micro;
111
112 /* If assignment for this TBF is happening on PACCH, that means the
113 * actual Assignment procedure (tx/rx) is happening on another TBF (eg
114 * Ul TBF vs DL TBF). Hence we add a security timer here to free it in
115 * case the other TBF doesn't succeed in informing (assigning) the MS
116 * about this TBF, or simply because the scheduler takes too long to
117 * schedule it. This timer can probably be dropped once we make the
118 * other TBF always signal us assignment failure (we already get
119 * assignment success through TBF_EV_ASSIGN_ACK_PACCH) */
120 if (ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)) {
121 fi->T = -2001;
122 val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1);
123 sec = val / 1000;
124 micro = (val % 1000) * 1000;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100125 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG,
126 "Starting timer X2001 [assignment (PACCH)] with %u sec. %u microsec\n",
127 sec, micro);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100128 osmo_timer_schedule(&fi->timer, sec, micro);
129 } else {
130 /* GPRS_RLCMAC_FLAG_CCCH is set, so here we submitted an DL Ass
131 * through PCUIF on CCCH */
132 }
133}
134
135static void st_assign(struct osmo_fsm_inst *fi, uint32_t event, void *data)
136{
137 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
138 unsigned long val;
139 unsigned int sec, micro;
140
141 switch (event) {
142 case TBF_EV_ASSIGN_ADD_CCCH:
143 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true);
144 break;
145 case TBF_EV_ASSIGN_ADD_PACCH:
146 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
147 break;
148 case TBF_EV_ASSIGN_ACK_PACCH:
149 tbf_assign_control_ts(ctx->tbf);
150 if (ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)) {
151 /* We now know that the PACCH really existed */
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100152 LOGPTBFDL(ctx->dl_tbf, LOGL_INFO,
153 "The TBF has been confirmed on the PACCH, "
154 "changed type from CCCH to PACCH\n");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100155 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false);
156 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
157 }
158 tbf_dl_fsm_state_chg(fi, TBF_ST_FLOW);
159 break;
160 case TBF_EV_ASSIGN_PCUIF_CNF:
161 /* BTS informs us it sent Imm Ass for DL TBF over CCCH. We now
162 * have to wait for X2002 to trigger (meaning MS is already
163 * listening on PDCH) in order to move to FLOW state and start
164 * transmitting data to it. When X2002 triggers (see cb timer
165 * end of the file) it will send TBF_EV_ASSIGN_READY_CCCH back
Pau Espin Pedrold1058b92022-12-16 18:39:29 +0100166 * to us here.
167 */
168 if (!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) {
169 /* This can happen if we initiated a CCCH DlAss from an
170 * older TBF object (same TLLI) towards BTS, and the DL-TBF
171 * was recreated and is now trying to be assigned throguh
172 * PACCH.
173 */
174 LOGPTBFDL(ctx->dl_tbf, LOGL_INFO,
175 "Ignoring event ASSIGN_PCUIF_CNF from BTS "
176 "(CCCH was not requested on current assignment)\n");
177 break;
178 }
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100179 fi->T = -2002;
180 val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1);
181 sec = val / 1000;
182 micro = (val % 1000) * 1000;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100183 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG,
184 "Starting timer X2002 [assignment (AGCH)] with %u sec. %u microsec\n",
185 sec, micro);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100186 osmo_timer_schedule(&fi->timer, sec, micro);
187 break;
188 case TBF_EV_ASSIGN_READY_CCCH:
189 /* change state to FLOW, so scheduler will start transmission */
190 tbf_dl_fsm_state_chg(fi, TBF_ST_FLOW);
191 break;
192 case TBF_EV_MAX_N3105:
193 /* We are going to release, so abort any Pkt Ul Ass pending to be scheduled: */
194 osmo_fsm_inst_dispatch(tbf_ul_ass_fi(ctx->tbf), TBF_UL_ASS_EV_ABORT, NULL);
195 ctx->T_release = 3195;
196 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
197 break;
198 default:
199 OSMO_ASSERT(0);
200 }
201}
202
203static void st_flow(struct osmo_fsm_inst *fi, uint32_t event, void *data)
204{
205 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
206
207 switch (event) {
208 case TBF_EV_DL_ACKNACK_MISS:
209 /* DL TBF: we missed a DL ACK/NACK. If we started assignment
210 * over CCCH and never received any DL ACK/NACK yet, it means we
211 * don't even know if the MS successfully received the Imm Ass on
212 * CCCH and hence is listening on PDCH. Let's better refrain
213 * from continuing and start assignment on CCCH again */
214 if ((ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))
215 && !(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) {
216 struct GprsMs *ms = tbf_ms(ctx->tbf);
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100217 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG,
218 "Re-send downlink assignment on PCH (IMSI=%s)\n",
219 ms_imsi_is_valid(ms) ? ms_imsi(ms) : "");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100220 tbf_dl_fsm_state_chg(fi, TBF_ST_ASSIGN);
221 /* send immediate assignment */
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100222 bts_snd_dl_ass(ms->bts, ctx->dl_tbf);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100223 }
224 break;
225 case TBF_EV_LAST_DL_DATA_SENT:
226 /* All data has been sent or received, change state to FINISHED */
227 tbf_dl_fsm_state_chg(fi, TBF_ST_FINISHED);
228 break;
229 case TBF_EV_FINAL_ACK_RECVD:
230 /* We received Final Ack (DL ACK/NACK) from MS. move to
231 * WAIT_RELEASE, we wait there for release or re-use the TBF in
232 * case we receive more DL data to tx */
233 tbf_dl_fsm_state_chg(fi, TBF_ST_WAIT_RELEASE);
234 break;
235 case TBF_EV_MAX_N3105:
236 ctx->T_release = 3195;
237 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
238 break;
239 default:
240 OSMO_ASSERT(0);
241 }
242}
243
244static void st_finished(struct osmo_fsm_inst *fi, uint32_t event, void *data)
245{
246 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
247
248 switch (event) {
249 case TBF_EV_DL_ACKNACK_MISS:
250 OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF);
251 break;
252 case TBF_EV_FINAL_ACK_RECVD:
253 /* We received Final Ack (DL ACK/NACK) from MS. move to
254 * WAIT_RELEASE, we wait there for release or re-use the TBF in
255 * case we receive more DL data to tx */
256 tbf_dl_fsm_state_chg(fi, TBF_ST_WAIT_RELEASE);
257 break;
258 case TBF_EV_MAX_N3105:
259 ctx->T_release = 3195;
260 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
261 break;
262 default:
263 OSMO_ASSERT(0);
264 }
265}
266
267static void st_wait_release_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
268{
269 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
270 unsigned long val_s, val_ms, val_us;
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100271
272 fi->T = 3193;
273 val_ms = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, fi->T, OSMO_TDEF_MS, -1);
274 val_s = val_ms / 1000;
275 val_us = (val_ms % 1000) * 1000;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100276 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %lu microsec\n",
277 fi->T, val_s, val_us);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100278 osmo_timer_schedule(&fi->timer, val_s, val_us);
279
280 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false);
281}
282
283static void st_wait_release(struct osmo_fsm_inst *fi, uint32_t event, void *data)
284{
285 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
286 switch (event) {
287 case TBF_EV_FINAL_ACK_RECVD:
288 /* ignore, duplicate ACK, we already know about since we are in WAIT_RELEASE */
289 break;
290 case TBF_EV_MAX_N3105:
291 ctx->T_release = 3195;
292 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
293 break;
294 default:
295 OSMO_ASSERT(0);
296 }
297}
298
299static void st_releasing_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
300{
301 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
302 unsigned long val;
303
304 if (!ctx->T_release)
305 return;
306
307 /* In general we should end up here with an assigned timer in ctx->T_release. Possible values are:
308 * T3195: Wait for reuse of TFI(s) when there is no response from the MS
309 * (radio failure or cell change) for this TBF/MBMS radio bearer.
310 * T3169: Wait for reuse of USF and TFI(s) after the MS uplink assignment for this TBF is invalid.
311 */
312 val = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, ctx->T_release, OSMO_TDEF_S, -1);
313 fi->T = ctx->T_release;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100314 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %u microsec\n",
315 ctx->T_release, val, 0);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100316 osmo_timer_schedule(&fi->timer, val, 0);
317}
318
319static void st_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
320{
321 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
322 switch (event) {
323 case TBF_EV_DL_ACKNACK_MISS:
324 OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF);
325 /* Ignore, we don't care about missed DL ACK/NACK poll timeouts
326 * anymore, we are already releasing the TBF */
327 break;
328 default:
329 OSMO_ASSERT(0);
330 }
331}
332
333static void handle_timeout_X2002(struct osmo_fsm_inst *fi)
334{
335 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100336 int rc;
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100337
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100338 if (fi->state != TBF_ST_ASSIGN) {
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100339 LOGPTBFDL(ctx->dl_tbf, LOGL_NOTICE, "Continue flow after IMM.ASS confirm\n");
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100340 return;
341 }
342
343 /* state TBF_ST_ASSIGN: */
344 tbf_assign_control_ts(ctx->tbf);
345
346 if (!tbf_can_upgrade_to_multislot(ctx->tbf)) {
347 /* change state to FLOW, so scheduler will start transmission */
348 osmo_fsm_inst_dispatch(fi, TBF_EV_ASSIGN_READY_CCCH, NULL);
349 return;
350 }
351
352 /* This tbf can be upgraded to use multiple DL timeslots and now that there is already
353 * one slot assigned send another DL assignment via PDCH.
354 */
355
356 /* keep TO flags */
357 ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
358
359 rc = dl_tbf_upgrade_to_multislot(ctx->dl_tbf);
Pau Espin Pedrol4a090542022-12-13 14:48:31 +0100360 if (rc < 0)
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100361 tbf_free(ctx->tbf);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100362}
363
364static int tbf_dl_fsm_timer_cb(struct osmo_fsm_inst *fi)
365{
366 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
367 switch (fi->T) {
368 case -2002:
369 handle_timeout_X2002(fi);
370 break;
371 case -2001:
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100372 LOGPTBFDL(ctx->dl_tbf, LOGL_NOTICE, "releasing due to PACCH assignment timeout.\n");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100373 /* fall-through */
374 case 3169:
375 case 3193:
376 case 3195:
377 tbf_free(ctx->tbf);
378 break;
379 default:
380 OSMO_ASSERT(0);
381 }
382 return 0;
383}
384
385static struct osmo_fsm_state tbf_dl_fsm_states[] = {
386 [TBF_ST_NEW] = {
387 .in_event_mask =
388 X(TBF_EV_ASSIGN_ADD_CCCH) |
389 X(TBF_EV_ASSIGN_ADD_PACCH),
390 .out_state_mask =
391 X(TBF_ST_ASSIGN) |
392 X(TBF_ST_RELEASING),
393 .name = "NEW",
394 .action = st_new,
395 },
396 [TBF_ST_ASSIGN] = {
397 .in_event_mask =
398 X(TBF_EV_ASSIGN_ADD_CCCH) |
399 X(TBF_EV_ASSIGN_ADD_PACCH) |
400 X(TBF_EV_ASSIGN_ACK_PACCH) |
401 X(TBF_EV_ASSIGN_PCUIF_CNF) |
402 X(TBF_EV_ASSIGN_READY_CCCH) |
403 X(TBF_EV_MAX_N3105),
404 .out_state_mask =
405 X(TBF_ST_FLOW) |
406 X(TBF_ST_FINISHED) |
407 X(TBF_ST_RELEASING),
408 .name = "ASSIGN",
409 .action = st_assign,
410 .onenter = st_assign_on_enter,
411 },
412 [TBF_ST_FLOW] = {
413 .in_event_mask =
414 X(TBF_EV_DL_ACKNACK_MISS) |
415 X(TBF_EV_LAST_DL_DATA_SENT) |
416 X(TBF_EV_FINAL_ACK_RECVD) |
417 X(TBF_EV_MAX_N3105),
418 .out_state_mask =
419 X(TBF_ST_ASSIGN) |
420 X(TBF_ST_FINISHED) |
421 X(TBF_ST_WAIT_RELEASE) |
422 X(TBF_ST_RELEASING),
423 .name = "FLOW",
424 .action = st_flow,
425 },
426 [TBF_ST_FINISHED] = {
427 .in_event_mask =
428 X(TBF_EV_DL_ACKNACK_MISS) |
429 X(TBF_EV_FINAL_ACK_RECVD) |
430 X(TBF_EV_MAX_N3105),
431 .out_state_mask =
432 X(TBF_ST_WAIT_RELEASE) |
433 X(TBF_ST_RELEASING),
434 .name = "FINISHED",
435 .action = st_finished,
436 },
437 [TBF_ST_WAIT_RELEASE] = {
438 .in_event_mask =
439 X(TBF_EV_FINAL_ACK_RECVD) |
440 X(TBF_EV_MAX_N3105),
441 .out_state_mask =
442 X(TBF_ST_RELEASING),
443 .name = "WAIT_RELEASE",
444 .action = st_wait_release,
445 .onenter = st_wait_release_on_enter,
446 },
447 [TBF_ST_RELEASING] = {
448 .in_event_mask =
449 X(TBF_EV_DL_ACKNACK_MISS),
450 .out_state_mask =
451 0,
452 .name = "RELEASING",
453 .action = st_releasing,
454 .onenter = st_releasing_on_enter,
455 },
456};
457
458struct osmo_fsm tbf_dl_fsm = {
459 .name = "DL_TBF",
460 .states = tbf_dl_fsm_states,
461 .num_states = ARRAY_SIZE(tbf_dl_fsm_states),
462 .timer_cb = tbf_dl_fsm_timer_cb,
463 .log_subsys = DTBFDL,
464 .event_names = tbf_fsm_event_names,
465};
466
467static __attribute__((constructor)) void tbf_dl_fsm_init(void)
468{
469 OSMO_ASSERT(osmo_fsm_register(&tbf_dl_fsm) == 0);
470}