Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 1 | /* tbf_fsm.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. |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 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 | #define X(s) (1 << (s)) |
| 29 | |
| 30 | const struct osmo_tdef_state_timeout tbf_fsm_timeouts[32] = { |
Pau Espin Pedrol | 0043005 | 2021-10-12 12:04:03 +0200 | [diff] [blame] | 31 | [TBF_ST_NEW] = {}, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 32 | [TBF_ST_ASSIGN] = { }, |
| 33 | [TBF_ST_FLOW] = { }, |
| 34 | [TBF_ST_FINISHED] = {}, |
| 35 | [TBF_ST_WAIT_RELEASE] = {}, |
| 36 | [TBF_ST_RELEASING] = {}, |
| 37 | }; |
| 38 | |
| 39 | const struct value_string tbf_fsm_event_names[] = { |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 40 | { TBF_EV_ASSIGN_ADD_CCCH, "ASSIGN_ADD_CCCH" }, |
| 41 | { TBF_EV_ASSIGN_ADD_PACCH, "ASSIGN_ADD_PACCH" }, |
Pau Espin Pedrol | 720e19e | 2021-07-22 19:56:37 +0200 | [diff] [blame] | 42 | { TBF_EV_ASSIGN_ACK_PACCH, "ASSIGN_ACK_PACCH" }, |
| 43 | { TBF_EV_ASSIGN_READY_CCCH, "ASSIGN_READY_CCCH" }, |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 44 | { TBF_EV_ASSIGN_PCUIF_CNF, "ASSIGN_PCUIF_CNF" }, |
| 45 | { TBF_EV_DL_ACKNACK_MISS, "DL_ACKNACK_MISS" }, |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 46 | { TBF_EV_LAST_DL_DATA_SENT, "LAST_DL_DATA_SENT" }, |
| 47 | { TBF_EV_LAST_UL_DATA_RECVD, "LAST_UL_DATA_RECVD" }, |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 48 | { TBF_EV_FINAL_ACK_RECVD, "FINAL_ACK_RECVD" }, |
Pau Espin Pedrol | 7a2b65e | 2021-10-11 19:18:13 +0200 | [diff] [blame] | 49 | { TBF_EV_FINAL_UL_ACK_CONFIRMED, "FINAL_UL_ACK_CONFIRMED" }, |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 50 | { TBF_EV_MAX_N3101 , "MAX_N3101" }, |
| 51 | { TBF_EV_MAX_N3103 , "MAX_N3103" }, |
| 52 | { TBF_EV_MAX_N3105 , "MAX_N3105" }, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 53 | { 0, NULL } |
| 54 | }; |
| 55 | |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 56 | static void mod_ass_type(struct tbf_fsm_ctx *ctx, uint8_t t, bool set) |
| 57 | { |
| 58 | const char *ch = "UNKNOWN"; |
| 59 | bool prev_set = ctx->state_flags & (1 << t); |
| 60 | |
| 61 | switch (t) { |
| 62 | case GPRS_RLCMAC_FLAG_CCCH: |
| 63 | ch = "CCCH"; |
| 64 | break; |
| 65 | case GPRS_RLCMAC_FLAG_PACCH: |
| 66 | ch = "PACCH"; |
| 67 | break; |
| 68 | default: |
| 69 | LOGPTBF(ctx->tbf, LOGL_ERROR, |
| 70 | "attempted to %sset unexpected ass. type %d - FIXME!\n", |
| 71 | set ? "" : "un", t); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (set && prev_set) { |
| 76 | LOGPTBF(ctx->tbf, LOGL_ERROR, |
| 77 | "attempted to set ass. type %s which is already set.\n", ch); |
| 78 | } else if (!set && !prev_set) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | LOGPTBF(ctx->tbf, LOGL_INFO, "%sset ass. type %s [prev CCCH:%u, PACCH:%u]\n", |
| 83 | set ? "" : "un", ch, |
| 84 | !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)), |
| 85 | !!(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH))); |
| 86 | |
| 87 | if (set) { |
| 88 | ctx->state_flags |= (1 << t); |
| 89 | } else { |
| 90 | ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep to flags */ |
| 91 | ctx->state_flags &= ~(1 << t); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
Pau Espin Pedrol | 0043005 | 2021-10-12 12:04:03 +0200 | [diff] [blame] | 96 | static void st_new(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 97 | { |
| 98 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
| 99 | switch (event) { |
| 100 | case TBF_EV_ASSIGN_ADD_CCCH: |
| 101 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true); |
| 102 | tbf_fsm_state_chg(fi, tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF ? |
| 103 | TBF_ST_ASSIGN : TBF_ST_FLOW); |
| 104 | break; |
| 105 | case TBF_EV_ASSIGN_ADD_PACCH: |
| 106 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true); |
| 107 | tbf_fsm_state_chg(fi, TBF_ST_ASSIGN); |
| 108 | break; |
| 109 | default: |
| 110 | OSMO_ASSERT(0); |
| 111 | } |
| 112 | } |
| 113 | |
Pau Espin Pedrol | 9d67e72 | 2021-07-28 19:25:38 +0200 | [diff] [blame] | 114 | static void st_assign_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 115 | { |
| 116 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
| 117 | unsigned long val; |
| 118 | unsigned int sec, micro; |
| 119 | |
| 120 | /* If assignment for this TBF is happening on PACCH, that means the |
| 121 | * actual Assignment procedure (tx/rx) is happening on another TBF (eg |
| 122 | * Ul TBF vs DL TBF). Hence we add a security timer here to free it in |
| 123 | * case the other TBF doesn't succeed in informing (assigning) the MS |
| 124 | * about this TBF, or simply because the scheduler takes too long to |
| 125 | * schedule it. This timer can probably be dropped once we make the |
| 126 | * other TBF always signal us assignment failure (we already get |
| 127 | * assignment success through TBF_EV_ASSIGN_ACK_PACCH) */ |
| 128 | if (ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)) { |
| 129 | fi->T = -2001; |
| 130 | val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1); |
| 131 | sec = val / 1000; |
| 132 | micro = (val % 1000) * 1000; |
| 133 | LOGPTBF(ctx->tbf, LOGL_DEBUG, |
| 134 | "Starting timer X2001 [assignment (PACCH)] with %u sec. %u microsec\n", |
| 135 | sec, micro); |
| 136 | osmo_timer_schedule(&fi->timer, sec, micro); |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 137 | } else if (tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF) { |
| 138 | /* GPRS_RLCMAC_FLAG_CCCH is set, so here we submitted an DL Ass through PCUIF on CCCH */ |
Pau Espin Pedrol | 9d67e72 | 2021-07-28 19:25:38 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 142 | static void st_assign(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 143 | { |
| 144 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 145 | unsigned long val; |
| 146 | unsigned int sec, micro; |
| 147 | |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 148 | switch (event) { |
| 149 | case TBF_EV_ASSIGN_ADD_CCCH: |
| 150 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, true); |
| 151 | break; |
| 152 | case TBF_EV_ASSIGN_ADD_PACCH: |
| 153 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true); |
| 154 | break; |
Pau Espin Pedrol | 720e19e | 2021-07-22 19:56:37 +0200 | [diff] [blame] | 155 | case TBF_EV_ASSIGN_ACK_PACCH: |
Pau Espin Pedrol | ffe998c | 2021-10-12 19:32:28 +0200 | [diff] [blame] | 156 | tbf_assign_control_ts(ctx->tbf); |
Pau Espin Pedrol | 720e19e | 2021-07-22 19:56:37 +0200 | [diff] [blame] | 157 | if (ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)) { |
| 158 | /* We now know that the PACCH really existed */ |
| 159 | LOGPTBF(ctx->tbf, LOGL_INFO, |
| 160 | "The TBF has been confirmed on the PACCH, " |
| 161 | "changed type from CCCH to PACCH\n"); |
| 162 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false); |
| 163 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_PACCH, true); |
| 164 | } |
| 165 | tbf_fsm_state_chg(fi, TBF_ST_FLOW); |
| 166 | break; |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 167 | case TBF_EV_ASSIGN_PCUIF_CNF: |
| 168 | /* BTS informs us it sent Imm Ass for DL TBF over CCCH. We now |
| 169 | * have to wait for X2002 to trigger (meaning MS is already |
| 170 | * listening on PDCH) in order to move to FLOW state and start |
| 171 | * transmitting data to it. When X2002 triggers (see cb timer |
| 172 | * end of the file) it will send TBF_EV_ASSIGN_READY_CCCH back |
| 173 | * to us here. */ |
| 174 | OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF); |
| 175 | fi->T = -2002; |
| 176 | val = osmo_tdef_get(the_pcu->T_defs, fi->T, OSMO_TDEF_MS, -1); |
| 177 | sec = val / 1000; |
| 178 | micro = (val % 1000) * 1000; |
| 179 | LOGPTBF(ctx->tbf, LOGL_DEBUG, |
| 180 | "Starting timer X2002 [assignment (AGCH)] with %u sec. %u microsec\n", |
| 181 | sec, micro); |
| 182 | osmo_timer_schedule(&fi->timer, sec, micro); |
| 183 | break; |
Pau Espin Pedrol | 720e19e | 2021-07-22 19:56:37 +0200 | [diff] [blame] | 184 | case TBF_EV_ASSIGN_READY_CCCH: |
| 185 | /* change state to FLOW, so scheduler will start transmission */ |
| 186 | tbf_fsm_state_chg(fi, TBF_ST_FLOW); |
| 187 | break; |
Pau Espin Pedrol | 85aa87b | 2021-10-12 14:02:52 +0200 | [diff] [blame] | 188 | case TBF_EV_MAX_N3105: |
Pau Espin Pedrol | b3291bc | 2021-10-14 19:21:38 +0200 | [diff] [blame] | 189 | /* We are going to release, so abort any Pkt Ul Ass pending to be scheduled: */ |
| 190 | osmo_fsm_inst_dispatch(tbf_ul_ass_fi(ctx->tbf), TBF_UL_ASS_EV_ABORT, NULL); |
Pau Espin Pedrol | 85aa87b | 2021-10-12 14:02:52 +0200 | [diff] [blame] | 191 | ctx->T_release = 3195; |
| 192 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 193 | break; |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 194 | default: |
| 195 | OSMO_ASSERT(0); |
| 196 | } |
| 197 | } |
| 198 | |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 199 | static void st_flow(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 200 | { |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 201 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 202 | |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 203 | switch (event) { |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 204 | case TBF_EV_DL_ACKNACK_MISS: |
| 205 | /* DL TBF: we missed a DL ACK/NACK. If we started assignment |
| 206 | * over CCCH and never received any DL ACK/NACK yet, it means we |
| 207 | * don't even know if the MS successfuly received the Imm Ass on |
| 208 | * CCCH and hence is listening on PDCH. Let's better refrain |
| 209 | * from continuing and start assignment on CCCH again */ |
| 210 | if ((ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)) |
| 211 | && !(ctx->state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) { |
| 212 | struct GprsMs *ms = tbf_ms(ctx->tbf); |
Pau Espin Pedrol | d06ec27 | 2021-07-30 15:03:21 +0200 | [diff] [blame] | 213 | LOGPTBF(ctx->tbf, LOGL_DEBUG, "Re-send downlink assignment on PCH (IMSI=%s)\n", |
Pau Espin Pedrol | 5deac14 | 2021-11-12 18:01:50 +0100 | [diff] [blame] | 214 | ms_imsi_is_valid(ms) ? ms_imsi(ms) : ""); |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 215 | tbf_fsm_state_chg(fi, TBF_ST_ASSIGN); |
| 216 | /* send immediate assignment */ |
Pau Espin Pedrol | 5deac14 | 2021-11-12 18:01:50 +0100 | [diff] [blame] | 217 | bts_snd_dl_ass(ms->bts, ctx->tbf); |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 218 | } |
| 219 | break; |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 220 | case TBF_EV_LAST_DL_DATA_SENT: |
| 221 | case TBF_EV_LAST_UL_DATA_RECVD: |
| 222 | /* All data has been sent or received, change state to FINISHED */ |
| 223 | tbf_fsm_state_chg(fi, TBF_ST_FINISHED); |
| 224 | break; |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 225 | case TBF_EV_FINAL_ACK_RECVD: |
| 226 | /* We received Final Ack (DL ACK/NACK) from MS. move to |
| 227 | WAIT_RELEASE, we wait there for release or re-use the TBF in |
| 228 | case we receive more DL data to tx */ |
| 229 | tbf_fsm_state_chg(fi, TBF_ST_WAIT_RELEASE); |
| 230 | break; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 231 | case TBF_EV_MAX_N3101: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 232 | ctx->T_release = 3169; |
| 233 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 234 | break; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 235 | case TBF_EV_MAX_N3105: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 236 | ctx->T_release = 3195; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 237 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 238 | break; |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 239 | default: |
| 240 | OSMO_ASSERT(0); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static void st_finished(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 245 | { |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 246 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 22b26d8 | 2022-10-26 15:44:14 +0200 | [diff] [blame] | 247 | struct GprsMs *ms; |
| 248 | bool new_ul_tbf_requested; |
| 249 | |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 250 | switch (event) { |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 251 | case TBF_EV_DL_ACKNACK_MISS: |
| 252 | break; |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 253 | case TBF_EV_FINAL_ACK_RECVD: |
| 254 | /* We received Final Ack (DL ACK/NACK) from MS. move to |
| 255 | WAIT_RELEASE, we wait there for release or re-use the TBF in |
| 256 | case we receive more DL data to tx */ |
| 257 | tbf_fsm_state_chg(fi, TBF_ST_WAIT_RELEASE); |
| 258 | break; |
Pau Espin Pedrol | 7a2b65e | 2021-10-11 19:18:13 +0200 | [diff] [blame] | 259 | case TBF_EV_FINAL_UL_ACK_CONFIRMED: |
Pau Espin Pedrol | 22b26d8 | 2022-10-26 15:44:14 +0200 | [diff] [blame] | 260 | OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF); |
| 261 | new_ul_tbf_requested = (bool)data; |
| 262 | /* Ref the MS, otherwise it may be freed after ul_tbf is |
| 263 | * detached when sending event below. */ |
| 264 | ms = tbf_ms(ctx->tbf); |
| 265 | ms_ref(ms); |
Pau Espin Pedrol | 7a2b65e | 2021-10-11 19:18:13 +0200 | [diff] [blame] | 266 | /* UL TBF ACKed our transmitted UL ACK/NACK with final Ack |
Pau Espin Pedrol | 22b26d8 | 2022-10-26 15:44:14 +0200 | [diff] [blame] | 267 | * Indicator set to '1'. We can free the TBF right away, the MS |
| 268 | * also just released its TBF on its side. */ |
| 269 | LOGPTBFUL(tbf_as_ul_tbf(ctx->tbf), LOGL_DEBUG, "[UPLINK] END\n"); |
Pau Espin Pedrol | 7a2b65e | 2021-10-11 19:18:13 +0200 | [diff] [blame] | 270 | tbf_free(ctx->tbf); |
Pau Espin Pedrol | 22b26d8 | 2022-10-26 15:44:14 +0200 | [diff] [blame] | 271 | /* Here fi, ctx and ctx->tbf are already freed! */ |
| 272 | /* TS 44.060 9.3.3.3.2: There might be LLC packets waiting in |
| 273 | * the queue but the DL TBF assignment might have been delayed |
| 274 | * because there was no way to reach the MS (because ul_tbf was |
| 275 | * in packet-active mode with FINISHED state). If MS is going |
| 276 | * back to packet-idle mode then we can assign the DL TBF on PCH |
| 277 | * now. */ |
| 278 | if (!new_ul_tbf_requested && ms_need_dl_tbf(ms)) |
Pau Espin Pedrol | 091642a | 2022-10-28 18:01:31 +0200 | [diff] [blame] | 279 | ms_new_dl_tbf_assigned_on_pch(ms); |
Pau Espin Pedrol | 22b26d8 | 2022-10-26 15:44:14 +0200 | [diff] [blame] | 280 | ms_unref(ms); |
Pau Espin Pedrol | 7a2b65e | 2021-10-11 19:18:13 +0200 | [diff] [blame] | 281 | break; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 282 | case TBF_EV_MAX_N3103: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 283 | ctx->T_release = 3169; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 284 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 285 | break; |
| 286 | case TBF_EV_MAX_N3105: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 287 | ctx->T_release = 3195; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 288 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 289 | break; |
| 290 | default: |
| 291 | OSMO_ASSERT(0); |
| 292 | } |
| 293 | } |
| 294 | |
Pau Espin Pedrol | f48de62 | 2021-07-30 14:48:06 +0200 | [diff] [blame] | 295 | static void st_wait_release_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 296 | { |
| 297 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
| 298 | unsigned long val_s, val_ms, val_us; |
| 299 | OSMO_ASSERT(tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF); |
| 300 | |
| 301 | fi->T = 3193; |
| 302 | val_ms = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, fi->T, OSMO_TDEF_MS, -1); |
| 303 | val_s = val_ms / 1000; |
| 304 | val_us = (val_ms % 1000) * 1000; |
| 305 | LOGPTBF(ctx->tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %lu microsec\n", |
| 306 | fi->T, val_s, val_us); |
| 307 | osmo_timer_schedule(&fi->timer, val_s, val_us); |
| 308 | |
| 309 | mod_ass_type(ctx, GPRS_RLCMAC_FLAG_CCCH, false); |
| 310 | } |
| 311 | |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 312 | static void st_wait_release(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 313 | { |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 314 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 315 | switch (event) { |
| 316 | case TBF_EV_FINAL_ACK_RECVD: |
| 317 | /* ignore, duplicate ACK, we already know about since we are in WAIT_RELEASE */ |
| 318 | break; |
| 319 | case TBF_EV_MAX_N3101: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 320 | ctx->T_release = 3169; |
| 321 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 322 | break; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 323 | case TBF_EV_MAX_N3105: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 324 | ctx->T_release = 3195; |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 325 | tbf_fsm_state_chg(fi, TBF_ST_RELEASING); |
| 326 | break; |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 327 | default: |
| 328 | OSMO_ASSERT(0); |
| 329 | } |
| 330 | } |
| 331 | |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 332 | static void st_releasing_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 333 | { |
| 334 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
| 335 | unsigned long val; |
| 336 | |
| 337 | if (!ctx->T_release) |
| 338 | return; |
| 339 | |
| 340 | /* In general we should end up here with an assigned timer in ctx->T_release. Possible values are: |
| 341 | * T3195: Wait for reuse of TFI(s) when there is no response from the MS |
| 342 | * (radio failure or cell change) for this TBF/MBMS radio bearer. |
| 343 | * T3169: Wait for reuse of USF and TFI(s) after the MS uplink assignment for this TBF is invalid. |
| 344 | */ |
| 345 | val = osmo_tdef_get(tbf_ms(ctx->tbf)->bts->T_defs_bts, ctx->T_release, OSMO_TDEF_S, -1); |
| 346 | fi->T = ctx->T_release; |
| 347 | LOGPTBF(ctx->tbf, LOGL_DEBUG, "starting timer T%u with %lu sec. %u microsec\n", |
| 348 | ctx->T_release, val, 0); |
| 349 | osmo_timer_schedule(&fi->timer, val, 0); |
| 350 | } |
| 351 | |
Pau Espin Pedrol | cee6b12 | 2021-09-28 16:49:33 +0200 | [diff] [blame] | 352 | static void st_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 353 | { |
| 354 | switch (event) { |
| 355 | case TBF_EV_DL_ACKNACK_MISS: |
| 356 | /* Ignore, we don't care about missed DL ACK/NACK poll timeouts |
| 357 | * anymore, we are already releasing the TBF */ |
| 358 | break; |
| 359 | default: |
| 360 | OSMO_ASSERT(0); |
| 361 | } |
| 362 | } |
| 363 | |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 364 | static void handle_timeout_X2002(struct tbf_fsm_ctx *ctx) |
| 365 | { |
Pau Espin Pedrol | cc30b05 | 2022-10-27 15:25:55 +0200 | [diff] [blame] | 366 | struct gprs_rlcmac_dl_tbf *dl_tbf = tbf_as_dl_tbf(ctx->tbf); |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 367 | |
Pau Espin Pedrol | 89a995a | 2021-10-18 14:25:17 +0200 | [diff] [blame] | 368 | /* X2002 is used only for DL TBF */ |
| 369 | OSMO_ASSERT(dl_tbf); |
| 370 | |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 371 | if (ctx->fi->state == TBF_ST_ASSIGN) { |
| 372 | tbf_assign_control_ts(ctx->tbf); |
| 373 | |
| 374 | if (!tbf_can_upgrade_to_multislot(ctx->tbf)) { |
| 375 | /* change state to FLOW, so scheduler |
| 376 | * will start transmission */ |
| 377 | osmo_fsm_inst_dispatch(ctx->fi, TBF_EV_ASSIGN_READY_CCCH, NULL); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | /* This tbf can be upgraded to use multiple DL |
| 382 | * timeslots and now that there is already one |
| 383 | * slot assigned send another DL assignment via |
| 384 | * PDCH. */ |
| 385 | |
| 386 | /* keep to flags */ |
| 387 | ctx->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; |
| 388 | |
| 389 | tbf_update(ctx->tbf); |
Pau Espin Pedrol | ee35008 | 2022-10-28 17:35:03 +0200 | [diff] [blame] | 390 | dl_tbf_trigger_ass_on_pacch(dl_tbf, ctx->tbf); |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 391 | } else |
| 392 | LOGPTBF(ctx->tbf, LOGL_NOTICE, "Continue flow after IMM.ASS confirm\n"); |
| 393 | } |
| 394 | |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 395 | static int tbf_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 396 | { |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 397 | struct tbf_fsm_ctx *ctx = (struct tbf_fsm_ctx *)fi->priv; |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 398 | switch (fi->T) { |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 399 | case -2002: |
| 400 | handle_timeout_X2002(ctx); |
| 401 | break; |
Pau Espin Pedrol | 9d67e72 | 2021-07-28 19:25:38 +0200 | [diff] [blame] | 402 | case -2001: |
| 403 | LOGPTBF(ctx->tbf, LOGL_NOTICE, "releasing due to PACCH assignment timeout.\n"); |
| 404 | /* fall-through */ |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 405 | case 3169: |
Pau Espin Pedrol | f48de62 | 2021-07-30 14:48:06 +0200 | [diff] [blame] | 406 | case 3193: |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 407 | case 3195: |
| 408 | tbf_free(ctx->tbf); |
| 409 | break; |
| 410 | default: |
| 411 | OSMO_ASSERT(0); |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 412 | } |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static struct osmo_fsm_state tbf_fsm_states[] = { |
Pau Espin Pedrol | 0043005 | 2021-10-12 12:04:03 +0200 | [diff] [blame] | 417 | [TBF_ST_NEW] = { |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 418 | .in_event_mask = |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 419 | X(TBF_EV_ASSIGN_ADD_CCCH) | |
| 420 | X(TBF_EV_ASSIGN_ADD_PACCH), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 421 | .out_state_mask = |
| 422 | X(TBF_ST_ASSIGN) | |
| 423 | X(TBF_ST_FLOW) | |
| 424 | X(TBF_ST_RELEASING), |
Pau Espin Pedrol | 0043005 | 2021-10-12 12:04:03 +0200 | [diff] [blame] | 425 | .name = "NEW", |
| 426 | .action = st_new, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 427 | }, |
| 428 | [TBF_ST_ASSIGN] = { |
| 429 | .in_event_mask = |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 430 | X(TBF_EV_ASSIGN_ADD_CCCH) | |
Pau Espin Pedrol | 720e19e | 2021-07-22 19:56:37 +0200 | [diff] [blame] | 431 | X(TBF_EV_ASSIGN_ADD_PACCH) | |
| 432 | X(TBF_EV_ASSIGN_ACK_PACCH) | |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 433 | X(TBF_EV_ASSIGN_PCUIF_CNF) | |
Pau Espin Pedrol | 85aa87b | 2021-10-12 14:02:52 +0200 | [diff] [blame] | 434 | X(TBF_EV_ASSIGN_READY_CCCH) | |
| 435 | X(TBF_EV_MAX_N3105), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 436 | .out_state_mask = |
| 437 | X(TBF_ST_FLOW) | |
| 438 | X(TBF_ST_FINISHED) | |
| 439 | X(TBF_ST_RELEASING), |
| 440 | .name = "ASSIGN", |
Pau Espin Pedrol | 33e8007 | 2021-07-22 19:20:50 +0200 | [diff] [blame] | 441 | .action = st_assign, |
Pau Espin Pedrol | 9d67e72 | 2021-07-28 19:25:38 +0200 | [diff] [blame] | 442 | .onenter = st_assign_on_enter, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 443 | }, |
| 444 | [TBF_ST_FLOW] = { |
| 445 | .in_event_mask = |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 446 | X(TBF_EV_DL_ACKNACK_MISS) | |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 447 | X(TBF_EV_LAST_DL_DATA_SENT) | |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 448 | X(TBF_EV_LAST_UL_DATA_RECVD) | |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 449 | X(TBF_EV_FINAL_ACK_RECVD) | |
| 450 | X(TBF_EV_MAX_N3101) | |
| 451 | X(TBF_EV_MAX_N3105), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 452 | .out_state_mask = |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 453 | X(TBF_ST_ASSIGN) | |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 454 | X(TBF_ST_FINISHED) | |
| 455 | X(TBF_ST_WAIT_RELEASE) | |
| 456 | X(TBF_ST_RELEASING), |
| 457 | .name = "FLOW", |
Pau Espin Pedrol | c32c4a3 | 2021-07-23 18:27:57 +0200 | [diff] [blame] | 458 | .action = st_flow, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 459 | }, |
| 460 | [TBF_ST_FINISHED] = { |
| 461 | .in_event_mask = |
Pau Espin Pedrol | 3225290 | 2021-07-29 16:44:11 +0200 | [diff] [blame] | 462 | X(TBF_EV_DL_ACKNACK_MISS) | |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 463 | X(TBF_EV_FINAL_ACK_RECVD) | |
Pau Espin Pedrol | 7a2b65e | 2021-10-11 19:18:13 +0200 | [diff] [blame] | 464 | X(TBF_EV_FINAL_UL_ACK_CONFIRMED) | |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 465 | X(TBF_EV_MAX_N3103) | |
| 466 | X(TBF_EV_MAX_N3105), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 467 | .out_state_mask = |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 468 | X(TBF_ST_WAIT_RELEASE) | |
| 469 | X(TBF_ST_RELEASING), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 470 | .name = "FINISHED", |
Pau Espin Pedrol | efcb046 | 2021-07-26 12:33:39 +0200 | [diff] [blame] | 471 | .action = st_finished, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 472 | }, |
| 473 | [TBF_ST_WAIT_RELEASE] = { |
| 474 | .in_event_mask = |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 475 | X(TBF_EV_FINAL_ACK_RECVD) | |
| 476 | X(TBF_EV_MAX_N3101) | |
| 477 | X(TBF_EV_MAX_N3105), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 478 | .out_state_mask = |
| 479 | X(TBF_ST_RELEASING), |
| 480 | .name = "WAIT_RELEASE", |
Pau Espin Pedrol | 55f600b | 2021-07-26 15:54:39 +0200 | [diff] [blame] | 481 | .action = st_wait_release, |
Pau Espin Pedrol | f48de62 | 2021-07-30 14:48:06 +0200 | [diff] [blame] | 482 | .onenter = st_wait_release_on_enter, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 483 | }, |
| 484 | [TBF_ST_RELEASING] = { |
| 485 | .in_event_mask = |
Pau Espin Pedrol | cee6b12 | 2021-09-28 16:49:33 +0200 | [diff] [blame] | 486 | X(TBF_EV_DL_ACKNACK_MISS), |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 487 | .out_state_mask = |
| 488 | 0, |
| 489 | .name = "RELEASING", |
Pau Espin Pedrol | cee6b12 | 2021-09-28 16:49:33 +0200 | [diff] [blame] | 490 | .action = st_releasing, |
Pau Espin Pedrol | cfb61d9 | 2021-07-26 17:17:02 +0200 | [diff] [blame] | 491 | .onenter = st_releasing_on_enter, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 492 | }, |
| 493 | }; |
| 494 | |
| 495 | struct osmo_fsm tbf_fsm = { |
| 496 | .name = "TBF", |
| 497 | .states = tbf_fsm_states, |
| 498 | .num_states = ARRAY_SIZE(tbf_fsm_states), |
| 499 | .timer_cb = tbf_fsm_timer_cb, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 500 | .log_subsys = DTBF, |
| 501 | .event_names = tbf_fsm_event_names, |
Pau Espin Pedrol | dc2aaac | 2021-05-14 12:50:46 +0200 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | static __attribute__((constructor)) void tbf_fsm_init(void) |
| 505 | { |
| 506 | OSMO_ASSERT(osmo_fsm_register(&tbf_fsm) == 0); |
| 507 | } |