blob: a7d24ec7b17ca7786f8ed0662222da50275d36af [file] [log] [blame]
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +01001/* tbf_ul_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_ul_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_RELEASING] = {},
38};
39
40/* Transition to a state, using the T timer defined in tbf_fsm_timeouts.
41 * The actual timeout value is in turn obtained from conn->T_defs.
42 * Assumes local variable fi exists. */
43#define tbf_ul_fsm_state_chg(fi, NEXT_STATE) \
44 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
45 tbf_ul_fsm_timeouts, \
46 the_pcu->T_defs, \
47 -1)
48
49static void mod_ass_type(struct tbf_ul_fsm_ctx *ctx, uint8_t t, bool set)
50{
51 const char *ch = "UNKNOWN";
52 bool prev_set = ctx->state_flags & (1 << t);
53
54 switch (t) {
55 case GPRS_RLCMAC_FLAG_CCCH:
56 ch = "CCCH";
57 break;
58 case GPRS_RLCMAC_FLAG_PACCH:
59 ch = "PACCH";
60 break;
61 default:
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +010062 LOGPTBFUL(ctx->ul_tbf, LOGL_ERROR,
63 "attempted to %sset unexpected ass. type %d - FIXME!\n",
64 set ? "" : "un", t);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010065 return;
66 }
67
68 if (set && prev_set)
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +010069 LOGPTBFUL(ctx->ul_tbf, LOGL_ERROR,
70 "attempted to set ass. type %s which is already set.\n", ch);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010071 else if (!set && !prev_set)
72 return;
73
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +010074 LOGPTBFUL(ctx->ul_tbf, LOGL_INFO, "%sset ass. type %s [prev CCCH:%u, PACCH:%u]\n",
75 set ? "" : "un", ch,
76 !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)),
77 !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)));
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010078
79 if (set) {
80 ctx->state_flags |= (1 << t);
81 } else {
82 ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep to flags */
83 ctx->state_flags &= ~(1 << t);
84 }
85}
86
87
88static void st_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
89{
90 struct tbf_ul_fsm_ctx *ctx = (struct tbf_ul_fsm_ctx *)fi->priv;
91 switch (event) {
92 case TBF_EV_ASSIGN_ADD_CCCH:
93 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true);
94 tbf_ul_fsm_state_chg(fi, TBF_ST_FLOW);
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +010095 ul_tbf_contention_resolution_start(ctx->ul_tbf);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +010096 break;
97 case TBF_EV_ASSIGN_ADD_PACCH:
98 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
99 tbf_ul_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_ul_fsm_ctx *ctx = (struct tbf_ul_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 Pedrolcdc762a2022-12-12 19:58:37 +0100125 LOGPTBFUL(ctx->ul_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 }
130}
131
132static void st_assign(struct osmo_fsm_inst *fi, uint32_t event, void *data)
133{
134 struct tbf_ul_fsm_ctx *ctx = (struct tbf_ul_fsm_ctx *)fi->priv;
135
136 switch (event) {
137 case TBF_EV_ASSIGN_ADD_CCCH:
138 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true);
139 break;
140 case TBF_EV_ASSIGN_ADD_PACCH:
141 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
142 break;
143 case TBF_EV_ASSIGN_ACK_PACCH:
144 tbf_assign_control_ts(ctx->tbf);
145 if (ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)) {
146 /* We now know that the PACCH really existed */
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +0100147 LOGPTBFUL(ctx->ul_tbf, LOGL_INFO,
148 "The TBF has been confirmed on the PACCH, "
149 "changed type from CCCH to PACCH\n");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100150 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false);
151 mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true);
152 }
153 tbf_ul_fsm_state_chg(fi, TBF_ST_FLOW);
154 break;
155 case TBF_EV_MAX_N3105:
156 /* We are going to release, so abort any Pkt Ul Ass pending to be scheduled: */
157 osmo_fsm_inst_dispatch(tbf_ul_ass_fi(ctx->tbf), TBF_UL_ASS_EV_ABORT, NULL);
158 ctx->T_release = 3195;
159 tbf_ul_fsm_state_chg(fi, TBF_ST_RELEASING);
160 break;
161 default:
162 OSMO_ASSERT(0);
163 }
164}
165
166static void st_flow(struct osmo_fsm_inst *fi, uint32_t event, void *data)
167{
168 struct tbf_ul_fsm_ctx *ctx = (struct tbf_ul_fsm_ctx *)fi->priv;
169 struct GprsMs *ms = tbf_ms(ctx->tbf);
170 struct gprs_rlcmac_dl_tbf *dl_tbf = NULL;
171
172 switch (event) {
173 case TBF_EV_FIRST_UL_DATA_RECVD:
174 OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF);
175 /* TS 44.060 7a.2.1.1: "The contention resolution is completed on
176 * the network side when the network receives an RLC data block that
177 * comprises the TLLI value that identifies the mobile station and the
178 * TFI value associated with the TBF." */
179 bts_pch_timer_stop(ms->bts, ms);
180 /* We may still have some DL-TBF waiting for assignment in PCH,
181 * which clearly won't happen since the MS is on PDCH now. Get rid
182 * of it, it will be re-assigned on PACCH when contention
183 * resolution at the MS side is done (1st UL ACK/NACK sent) */
184 if ((dl_tbf = ms_dl_tbf(ms))) {
185 /* Get rid of previous finished UL TBF before providing a new one */
186 LOGPTBFDL(dl_tbf, LOGL_NOTICE,
187 "Got first UL data while DL-TBF pending, killing it\n");
188 tbf_free(dl_tbf_as_tbf(dl_tbf));
189 dl_tbf = NULL;
190 }
191 break;
192 case TBF_EV_CONTENTION_RESOLUTION_MS_SUCCESS:
193 ul_tbf_contention_resolution_success(tbf_as_ul_tbf(ctx->tbf));
194 break;
195 case TBF_EV_LAST_UL_DATA_RECVD:
196 /* All data has been sent or received, change state to FINISHED */
197 tbf_ul_fsm_state_chg(fi, TBF_ST_FINISHED);
198 break;
199 case TBF_EV_MAX_N3101:
200 ctx->T_release = 3169;
201 tbf_ul_fsm_state_chg(fi, TBF_ST_RELEASING);
202 break;
203 case TBF_EV_MAX_N3105:
204 ctx->T_release = 3195;
205 tbf_ul_fsm_state_chg(fi, TBF_ST_RELEASING);
206 break;
207 default:
208 OSMO_ASSERT(0);
209 }
210}
211
212static void st_finished(struct osmo_fsm_inst *fi, uint32_t event, void *data)
213{
214 struct tbf_ul_fsm_ctx *ctx = (struct tbf_ul_fsm_ctx *)fi->priv;
215 struct GprsMs *ms;
216 bool new_ul_tbf_requested;
217
218 switch (event) {
219 case TBF_EV_CONTENTION_RESOLUTION_MS_SUCCESS:
220 /* UL TBF: If MS only sends 1 RLCMAC UL block, it can be that we
221 * end up in FINISHED state before sending the first UL ACK/NACK */
222 ul_tbf_contention_resolution_success(tbf_as_ul_tbf(ctx->tbf));
223 break;
224 case TBF_EV_FINAL_UL_ACK_CONFIRMED:
225 OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF);
226 new_ul_tbf_requested = (bool)data;
227 /* Ref the MS, otherwise it may be freed after ul_tbf is
228 * detached when sending event below. */
229 ms = tbf_ms(ctx->tbf);
230 ms_ref(ms);
231 /* UL TBF ACKed our transmitted UL ACK/NACK with final Ack
232 * Indicator set to '1'. We can free the TBF right away, the MS
233 * also just released its TBF on its side. */
234 LOGPTBFUL(tbf_as_ul_tbf(ctx->tbf), LOGL_DEBUG, "[UPLINK] END\n");
235 tbf_free(ctx->tbf);
236 /* Here fi, ctx and ctx->tbf are already freed! */
237 /* TS 44.060 9.3.3.3.2: There might be LLC packets waiting in
238 * the queue but the DL TBF assignment might have been delayed
239 * because there was no way to reach the MS (because ul_tbf was
240 * in packet-active mode with FINISHED state). If MS is going
241 * back to packet-idle mode then we can assign the DL TBF on PCH
242 * now. */
243 if (!new_ul_tbf_requested && ms_need_dl_tbf(ms))
244 ms_new_dl_tbf_assigned_on_pch(ms);
245 ms_unref(ms);
246 break;
247 case TBF_EV_MAX_N3103:
248 ctx->T_release = 3169;
249 tbf_ul_fsm_state_chg(fi, TBF_ST_RELEASING);
250 break;
251 case TBF_EV_MAX_N3105:
252 ctx->T_release = 3195;
253 tbf_ul_fsm_state_chg(fi, TBF_ST_RELEASING);
254 break;
255 default:
256 OSMO_ASSERT(0);
257 }
258}
259
260static void st_releasing_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
261{
262 struct tbf_ul_fsm_ctx *ctx = (struct tbf_ul_fsm_ctx *)fi->priv;
263 unsigned long val;
264
265 if (!ctx->T_release)
266 return;
267
268 /* In general we should end up here with an assigned timer in ctx->T_release. Possible values are:
269 * T3195: Wait for reuse of TFI(s) when there is no response from the MS
270 * (radio failure or cell change) for this TBF/MBMS radio bearer.
271 * T3169: Wait for reuse of USF and TFI(s) after the MS uplink assignment for this TBF is invalid.
272 */
273 val = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, ctx->T_release, OSMO_TDEF_S, -1);
274 fi->T = ctx->T_release;
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +0100275 LOGPTBFUL(ctx->ul_tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %u microsec\n",
276 ctx->T_release, val, 0);
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100277 osmo_timer_schedule(&fi->timer, val, 0);
278}
279
280static void st_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
281{
Pau Espin Pedrol02d4b2a2023-03-07 17:57:05 +0100282 switch (event) {
283 case TBF_EV_MAX_N3105:
284 /* This may be received here if the TBF had several polls
285 * allocated concurrently and several failed each increasing N3105
286 * over MAX_N3015. We are already releasing, ignore.*/
287 break;
288 default:
289 OSMO_ASSERT(0);
290 }
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100291}
292
293static int tbf_ul_fsm_timer_cb(struct osmo_fsm_inst *fi)
294{
295 struct tbf_ul_fsm_ctx *ctx = (struct tbf_ul_fsm_ctx *)fi->priv;
296 switch (fi->T) {
297 case -2001:
Pau Espin Pedrolcdc762a2022-12-12 19:58:37 +0100298 LOGPTBFUL(ctx->ul_tbf, LOGL_NOTICE, "releasing due to PACCH assignment timeout.\n");
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100299 /* fall-through */
300 case 3169:
301 case 3195:
302 tbf_free(ctx->tbf);
303 break;
304 default:
305 OSMO_ASSERT(0);
306 }
307 return 0;
308}
309
310static struct osmo_fsm_state tbf_ul_fsm_states[] = {
311 [TBF_ST_NEW] = {
312 .in_event_mask =
313 X(TBF_EV_ASSIGN_ADD_CCCH) |
314 X(TBF_EV_ASSIGN_ADD_PACCH),
315 .out_state_mask =
316 X(TBF_ST_ASSIGN) |
317 X(TBF_ST_FLOW) |
318 X(TBF_ST_RELEASING),
319 .name = "NEW",
320 .action = st_new,
321 },
322 [TBF_ST_ASSIGN] = {
323 .in_event_mask =
324 X(TBF_EV_ASSIGN_ADD_CCCH) |
325 X(TBF_EV_ASSIGN_ADD_PACCH) |
326 X(TBF_EV_ASSIGN_ACK_PACCH) |
327 X(TBF_EV_MAX_N3105),
328 .out_state_mask =
329 X(TBF_ST_FLOW) |
330 X(TBF_ST_FINISHED) |
331 X(TBF_ST_RELEASING),
332 .name = "ASSIGN",
333 .action = st_assign,
334 .onenter = st_assign_on_enter,
335 },
336 [TBF_ST_FLOW] = {
337 .in_event_mask =
338 X(TBF_EV_FIRST_UL_DATA_RECVD) |
339 X(TBF_EV_CONTENTION_RESOLUTION_MS_SUCCESS) |
340 X(TBF_EV_LAST_UL_DATA_RECVD) |
341 X(TBF_EV_MAX_N3101) |
342 X(TBF_EV_MAX_N3105),
343 .out_state_mask =
344 X(TBF_ST_ASSIGN) |
345 X(TBF_ST_FINISHED) |
346 X(TBF_ST_RELEASING),
347 .name = "FLOW",
348 .action = st_flow,
349 },
350 [TBF_ST_FINISHED] = {
351 .in_event_mask =
352 X(TBF_EV_CONTENTION_RESOLUTION_MS_SUCCESS) |
353 X(TBF_EV_FINAL_UL_ACK_CONFIRMED) |
354 X(TBF_EV_MAX_N3103) |
355 X(TBF_EV_MAX_N3105),
356 .out_state_mask =
357 X(TBF_ST_RELEASING),
358 .name = "FINISHED",
359 .action = st_finished,
360 },
361 [TBF_ST_RELEASING] = {
Pau Espin Pedrol02d4b2a2023-03-07 17:57:05 +0100362 .in_event_mask =
363 X(TBF_EV_MAX_N3105),
Pau Espin Pedrol343c0ee2022-11-17 19:55:08 +0100364 .out_state_mask =
365 0,
366 .name = "RELEASING",
367 .action = st_releasing,
368 .onenter = st_releasing_on_enter,
369 },
370};
371
372struct osmo_fsm tbf_ul_fsm = {
373 .name = "UL_TBF",
374 .states = tbf_ul_fsm_states,
375 .num_states = ARRAY_SIZE(tbf_ul_fsm_states),
376 .timer_cb = tbf_ul_fsm_timer_cb,
377 .log_subsys = DTBFUL,
378 .event_names = tbf_fsm_event_names,
379};
380
381static __attribute__((constructor)) void tbf_ul_fsm_init(void)
382{
383 OSMO_ASSERT(osmo_fsm_register(&tbf_ul_fsm) == 0);
384}