blob: 0ab740d97ccc6f543736d2a0274727cbae4d0be6 [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
32const struct osmo_tdef_state_timeout tbf_dl_fsm_timeouts[32] = {
33 [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
166 * to us here. */
167 fi->T = -2002;
168 val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1);
169 sec = val / 1000;
170 micro = (val % 1000) * 1000;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100171 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG,
172 "Starting timer X2002 [assignment (AGCH)] with %u sec. %u microsec\n",
173 sec, micro);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100174 osmo_timer_schedule(&fi->timer, sec, micro);
175 break;
176 case TBF_EV_ASSIGN_READY_CCCH:
177 /* change state to FLOW, so scheduler will start transmission */
178 tbf_dl_fsm_state_chg(fi, TBF_ST_FLOW);
179 break;
180 case TBF_EV_MAX_N3105:
181 /* We are going to release, so abort any Pkt Ul Ass pending to be scheduled: */
182 osmo_fsm_inst_dispatch(tbf_ul_ass_fi(ctx->tbf), TBF_UL_ASS_EV_ABORT, NULL);
183 ctx->T_release = 3195;
184 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
185 break;
186 default:
187 OSMO_ASSERT(0);
188 }
189}
190
191static void st_flow(struct osmo_fsm_inst *fi, uint32_t event, void *data)
192{
193 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
194
195 switch (event) {
196 case TBF_EV_DL_ACKNACK_MISS:
197 /* DL TBF: we missed a DL ACK/NACK. If we started assignment
198 * over CCCH and never received any DL ACK/NACK yet, it means we
199 * don't even know if the MS successfully received the Imm Ass on
200 * CCCH and hence is listening on PDCH. Let's better refrain
201 * from continuing and start assignment on CCCH again */
202 if ((ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))
203 && !(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) {
204 struct GprsMs *ms = tbf_ms(ctx->tbf);
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100205 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG,
206 "Re-send downlink assignment on PCH (IMSI=%s)\n",
207 ms_imsi_is_valid(ms) ? ms_imsi(ms) : "");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100208 tbf_dl_fsm_state_chg(fi, TBF_ST_ASSIGN);
209 /* send immediate assignment */
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100210 bts_snd_dl_ass(ms->bts, ctx->dl_tbf);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100211 }
212 break;
213 case TBF_EV_LAST_DL_DATA_SENT:
214 /* All data has been sent or received, change state to FINISHED */
215 tbf_dl_fsm_state_chg(fi, TBF_ST_FINISHED);
216 break;
217 case TBF_EV_FINAL_ACK_RECVD:
218 /* We received Final Ack (DL ACK/NACK) from MS. move to
219 * WAIT_RELEASE, we wait there for release or re-use the TBF in
220 * case we receive more DL data to tx */
221 tbf_dl_fsm_state_chg(fi, TBF_ST_WAIT_RELEASE);
222 break;
223 case TBF_EV_MAX_N3105:
224 ctx->T_release = 3195;
225 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
226 break;
227 default:
228 OSMO_ASSERT(0);
229 }
230}
231
232static void st_finished(struct osmo_fsm_inst *fi, uint32_t event, void *data)
233{
234 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
235
236 switch (event) {
237 case TBF_EV_DL_ACKNACK_MISS:
238 OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF);
239 break;
240 case TBF_EV_FINAL_ACK_RECVD:
241 /* We received Final Ack (DL ACK/NACK) from MS. move to
242 * WAIT_RELEASE, we wait there for release or re-use the TBF in
243 * case we receive more DL data to tx */
244 tbf_dl_fsm_state_chg(fi, TBF_ST_WAIT_RELEASE);
245 break;
246 case TBF_EV_MAX_N3105:
247 ctx->T_release = 3195;
248 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
249 break;
250 default:
251 OSMO_ASSERT(0);
252 }
253}
254
255static void st_wait_release_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
256{
257 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
258 unsigned long val_s, val_ms, val_us;
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100259
260 fi->T = 3193;
261 val_ms = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, fi->T, OSMO_TDEF_MS, -1);
262 val_s = val_ms / 1000;
263 val_us = (val_ms % 1000) * 1000;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100264 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %lu microsec\n",
265 fi->T, val_s, val_us);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100266 osmo_timer_schedule(&fi->timer, val_s, val_us);
267
268 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false);
269}
270
271static void st_wait_release(struct osmo_fsm_inst *fi, uint32_t event, void *data)
272{
273 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
274 switch (event) {
275 case TBF_EV_FINAL_ACK_RECVD:
276 /* ignore, duplicate ACK, we already know about since we are in WAIT_RELEASE */
277 break;
278 case TBF_EV_MAX_N3105:
279 ctx->T_release = 3195;
280 tbf_dl_fsm_state_chg(fi, TBF_ST_RELEASING);
281 break;
282 default:
283 OSMO_ASSERT(0);
284 }
285}
286
287static void st_releasing_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
288{
289 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
290 unsigned long val;
291
292 if (!ctx->T_release)
293 return;
294
295 /* In general we should end up here with an assigned timer in ctx->T_release. Possible values are:
296 * T3195: Wait for reuse of TFI(s) when there is no response from the MS
297 * (radio failure or cell change) for this TBF/MBMS radio bearer.
298 * T3169: Wait for reuse of USF and TFI(s) after the MS uplink assignment for this TBF is invalid.
299 */
300 val = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, ctx->T_release, OSMO_TDEF_S, -1);
301 fi->T = ctx->T_release;
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100302 LOGPTBFDL(ctx->dl_tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %u microsec\n",
303 ctx->T_release, val, 0);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100304 osmo_timer_schedule(&fi->timer, val, 0);
305}
306
307static void st_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
308{
309 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
310 switch (event) {
311 case TBF_EV_DL_ACKNACK_MISS:
312 OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF);
313 /* Ignore, we don't care about missed DL ACK/NACK poll timeouts
314 * anymore, we are already releasing the TBF */
315 break;
316 default:
317 OSMO_ASSERT(0);
318 }
319}
320
321static void handle_timeout_X2002(struct osmo_fsm_inst *fi)
322{
323 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100324 int rc;
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100325
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100326 if (fi->state != TBF_ST_ASSIGN) {
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100327 LOGPTBFDL(ctx->dl_tbf, LOGL_NOTICE, "Continue flow after IMM.ASS confirm\n");
Pau Espin Pedrolb7a2b592022-12-13 14:43:59 +0100328 return;
329 }
330
331 /* state TBF_ST_ASSIGN: */
332 tbf_assign_control_ts(ctx->tbf);
333
334 if (!tbf_can_upgrade_to_multislot(ctx->tbf)) {
335 /* change state to FLOW, so scheduler will start transmission */
336 osmo_fsm_inst_dispatch(fi, TBF_EV_ASSIGN_READY_CCCH, NULL);
337 return;
338 }
339
340 /* This tbf can be upgraded to use multiple DL timeslots and now that there is already
341 * one slot assigned send another DL assignment via PDCH.
342 */
343
344 /* keep TO flags */
345 ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
346
347 rc = dl_tbf_upgrade_to_multislot(ctx->dl_tbf);
348 if (rc < 0) {
349 tbf_free(ctx->tbf);
350 return;
351 }
352 dl_tbf_trigger_ass_on_pacch(ctx->dl_tbf, ctx->tbf);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100353}
354
355static int tbf_dl_fsm_timer_cb(struct osmo_fsm_inst *fi)
356{
357 struct tbf_dl_fsm_ctx *ctx = (struct tbf_dl_fsm_ctx *)fi->priv;
358 switch (fi->T) {
359 case -2002:
360 handle_timeout_X2002(fi);
361 break;
362 case -2001:
Pau Espin Pedrol83a08922022-12-12 19:46:01 +0100363 LOGPTBFDL(ctx->dl_tbf, LOGL_NOTICE, "releasing due to PACCH assignment timeout.\n");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100364 /* fall-through */
365 case 3169:
366 case 3193:
367 case 3195:
368 tbf_free(ctx->tbf);
369 break;
370 default:
371 OSMO_ASSERT(0);
372 }
373 return 0;
374}
375
376static struct osmo_fsm_state tbf_dl_fsm_states[] = {
377 [TBF_ST_NEW] = {
378 .in_event_mask =
379 X(TBF_EV_ASSIGN_ADD_CCCH) |
380 X(TBF_EV_ASSIGN_ADD_PACCH),
381 .out_state_mask =
382 X(TBF_ST_ASSIGN) |
383 X(TBF_ST_RELEASING),
384 .name = "NEW",
385 .action = st_new,
386 },
387 [TBF_ST_ASSIGN] = {
388 .in_event_mask =
389 X(TBF_EV_ASSIGN_ADD_CCCH) |
390 X(TBF_EV_ASSIGN_ADD_PACCH) |
391 X(TBF_EV_ASSIGN_ACK_PACCH) |
392 X(TBF_EV_ASSIGN_PCUIF_CNF) |
393 X(TBF_EV_ASSIGN_READY_CCCH) |
394 X(TBF_EV_MAX_N3105),
395 .out_state_mask =
396 X(TBF_ST_FLOW) |
397 X(TBF_ST_FINISHED) |
398 X(TBF_ST_RELEASING),
399 .name = "ASSIGN",
400 .action = st_assign,
401 .onenter = st_assign_on_enter,
402 },
403 [TBF_ST_FLOW] = {
404 .in_event_mask =
405 X(TBF_EV_DL_ACKNACK_MISS) |
406 X(TBF_EV_LAST_DL_DATA_SENT) |
407 X(TBF_EV_FINAL_ACK_RECVD) |
408 X(TBF_EV_MAX_N3105),
409 .out_state_mask =
410 X(TBF_ST_ASSIGN) |
411 X(TBF_ST_FINISHED) |
412 X(TBF_ST_WAIT_RELEASE) |
413 X(TBF_ST_RELEASING),
414 .name = "FLOW",
415 .action = st_flow,
416 },
417 [TBF_ST_FINISHED] = {
418 .in_event_mask =
419 X(TBF_EV_DL_ACKNACK_MISS) |
420 X(TBF_EV_FINAL_ACK_RECVD) |
421 X(TBF_EV_MAX_N3105),
422 .out_state_mask =
423 X(TBF_ST_WAIT_RELEASE) |
424 X(TBF_ST_RELEASING),
425 .name = "FINISHED",
426 .action = st_finished,
427 },
428 [TBF_ST_WAIT_RELEASE] = {
429 .in_event_mask =
430 X(TBF_EV_FINAL_ACK_RECVD) |
431 X(TBF_EV_MAX_N3105),
432 .out_state_mask =
433 X(TBF_ST_RELEASING),
434 .name = "WAIT_RELEASE",
435 .action = st_wait_release,
436 .onenter = st_wait_release_on_enter,
437 },
438 [TBF_ST_RELEASING] = {
439 .in_event_mask =
440 X(TBF_EV_DL_ACKNACK_MISS),
441 .out_state_mask =
442 0,
443 .name = "RELEASING",
444 .action = st_releasing,
445 .onenter = st_releasing_on_enter,
446 },
447};
448
449struct osmo_fsm tbf_dl_fsm = {
450 .name = "DL_TBF",
451 .states = tbf_dl_fsm_states,
452 .num_states = ARRAY_SIZE(tbf_dl_fsm_states),
453 .timer_cb = tbf_dl_fsm_timer_cb,
454 .log_subsys = DTBFDL,
455 .event_names = tbf_fsm_event_names,
456};
457
458static __attribute__((constructor)) void tbf_dl_fsm_init(void)
459{
460 OSMO_ASSERT(osmo_fsm_register(&tbf_dl_fsm) == 0);
461}