blob: 5ac1c1a1a87e4fff53a28572b47e8a464f50f999 [file] [log] [blame]
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +02001/* tbf_dl_ass_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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this 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
23#include <talloc.h>
24
25#include <osmocom/core/bitvec.h>
26
27#include <tbf_dl_ass_fsm.h>
28#include <gprs_rlcmac.h>
29#include <gprs_debug.h>
30#include <gprs_ms.h>
31#include <encoding.h>
32#include <bts.h>
33#include <tbf.h>
Pau Espin Pedrola161bf42021-07-30 13:42:06 +020034#include <tbf_dl.h>
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +020035
36#define X(s) (1 << (s))
37
38const struct osmo_tdef_state_timeout tbf_dl_ass_fsm_timeouts[32] = {
39 [TBF_DL_ASS_NONE] = {},
40 [TBF_DL_ASS_SEND_ASS] = {},
41 [TBF_DL_ASS_WAIT_ACK] = {},
42};
43
44const struct value_string tbf_dl_ass_fsm_event_names[] = {
45 { TBF_DL_ASS_EV_SCHED_ASS, "SCHED_ASS" },
46 { TBF_DL_ASS_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
47 { TBF_DL_ASS_EV_RX_ASS_CTRL_ACK, "RX_ASS_CTRL_ACK" },
48 { TBF_DL_ASS_EV_ASS_POLL_TIMEOUT, "ASS_POLL_TIMEOUT" },
49 { 0, NULL }
50};
51
52struct msgb *create_packet_dl_assign(const struct tbf_dl_ass_fsm_ctx *ctx,
53 const struct tbf_dl_ass_ev_create_rlcmac_msg_ctx *d)
54{
55 struct msgb *msg;
56 struct gprs_rlcmac_dl_tbf *new_dl_tbf = NULL;
57 RlcMacDownlink_t *mac_control_block = NULL;
58 struct GprsMs *ms = tbf_ms(ctx->tbf);
59 const int poll_ass_dl = 1;
60 unsigned int rrbp = 0;
61 uint32_t new_poll_fn = 0;
62 int rc;
63 bool old_tfi_is_valid = tbf_is_tfi_assigned(ctx->tbf);
64
65 /* We only use this function in control TS (PACCH) so that MS can always answer the poll */
66 OSMO_ASSERT(tbf_is_control_ts(ctx->tbf, d->ts));
67
68 if (tbf_ul_ass_fi(ctx->tbf)->state == TBF_UL_ASS_WAIT_ACK)
69 {
70 LOGPTBF(ctx->tbf, LOGL_DEBUG,
71 "Polling is already scheduled, so we must wait for the uplink assignment...\n");
72 // FIXME: call tbf_dl_ass_fsm_state_chg(ctx->fi, TBF_DL_ASS_NONE); ?
73 return NULL;
74 }
75 rc = tbf_check_polling(ctx->tbf, d->fn, d->ts, &new_poll_fn, &rrbp);
76 if (rc < 0)
77 return NULL;
78
79 /* on uplink TBF we get the downlink TBF to be assigned. */
80 if (tbf_direction(ctx->tbf) == GPRS_RLCMAC_UL_TBF) {
81 struct gprs_rlcmac_ul_tbf *ul_tbf = as_ul_tbf(ctx->tbf);
82
83 /* be sure to check first, if contention resolution is done,
84 * otherwise we cannot send the assignment yet (3GPP TS 44.060 sec 7.1.3.1) */
85 if (!ul_tbf_contention_resolution_done(ul_tbf)) {
86 LOGPTBF(ctx->tbf, LOGL_DEBUG,
87 "Cannot assign DL TBF now, because contention resolution is not finished.\n");
88 // FIXME: call tbf_dl_ass_fsm_state_chg(ctx->fi, TBF_DL_ASS_NONE); ?
89 return NULL;
90 }
91 }
92
93 new_dl_tbf = ms_dl_tbf(ms);
94 if (!new_dl_tbf) {
95 LOGPTBF(ctx->tbf, LOGL_ERROR,
96 "We have a schedule for downlink assignment, but there is no downlink TBF\n");
97 tbf_dl_ass_fsm_state_chg(ctx->fi, TBF_DL_ASS_NONE);
98 return NULL;
99 }
100
101 if (new_dl_tbf == as_dl_tbf(ctx->tbf))
102 LOGPTBF(ctx->tbf, LOGL_DEBUG, "New and old TBF are the same.\n");
103
104 if (old_tfi_is_valid && ms_tlli(ms) == GSM_RESERVED_TMSI) {
105 LOGPTBF(ctx->tbf, LOGL_ERROR,
106 "The old TFI is not assigned and there is no TLLI. New TBF %s\n",
107 tbf_name((struct gprs_rlcmac_tbf *)new_dl_tbf));
108 tbf_dl_ass_fsm_state_chg(ctx->fi, TBF_DL_ASS_NONE);
109 return NULL;
110 }
111
112 msg = msgb_alloc(GSM_MACBLOCK_LEN, "rlcmac_dl_ass");
113 if (!msg)
114 return NULL;
115
116 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
117 struct bitvec bv = {
118 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
119 .data_len = GSM_MACBLOCK_LEN,
120 };
121 bitvec_unhex(&bv, DUMMY_VEC);
122
123 LOGPTBF((struct gprs_rlcmac_tbf *)new_dl_tbf, LOGL_INFO, "start Packet Downlink Assignment (PACCH)\n");
124 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->tbf, RlcMacDownlink_t);
125 write_packet_downlink_assignment(mac_control_block, old_tfi_is_valid,
126 tbf_tfi(ctx->tbf), (tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF),
127 new_dl_tbf, poll_ass_dl, rrbp,
128 bts_get_ms_pwr_alpha(ms->bts), the_pcu->vty.gamma, -1, 0,
129 tbf_is_egprs_enabled(ctx->tbf), tbf_state(ctx->tbf) == TBF_ST_WAIT_RELEASE);
130 LOGP(DTBF, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n");
131 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
132 if (rc < 0) {
133 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Downlink Ass failed (%d)\n", rc);
134 goto free_ret;
135 }
136 LOGP(DTBF, LOGL_DEBUG, "------------------------- TX : Packet Downlink Assignment -------------------------\n");
137 bts_do_rate_ctr_inc(ms->bts, CTR_PKT_DL_ASSIGNMENT);
138
139 tbf_set_polling(ctx->tbf, new_poll_fn, d->ts, PDCH_ULC_POLL_DL_ASS);
Pau Espin Pedrola161bf42021-07-30 13:42:06 +0200140 LOGPTBFDL(ctx->tbf, LOGL_INFO, "Scheduled DL Assignment polling on PACCH (FN=%d, TS=%d)\n",
141 new_poll_fn, d->ts);
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200142
143 talloc_free(mac_control_block);
144 return msg;
145
146free_ret:
147 talloc_free(mac_control_block);
148 msgb_free(msg);
149 return NULL;
150}
151
152static void st_none_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
153{
154}
155
156static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
157{
158 switch (event) {
159 case TBF_DL_ASS_EV_SCHED_ASS:
160 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_SEND_ASS);
161 break;
162 default:
163 OSMO_ASSERT(0);
164 }
165}
166
167static void st_send_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
168{
169 struct tbf_dl_ass_fsm_ctx *ctx = (struct tbf_dl_ass_fsm_ctx *)fi->priv;
170 struct tbf_dl_ass_ev_create_rlcmac_msg_ctx *data_ctx;
171
172 switch (event) {
173 case TBF_DL_ASS_EV_CREATE_RLCMAC_MSG:
174 data_ctx = (struct tbf_dl_ass_ev_create_rlcmac_msg_ctx *)data;
175 data_ctx->msg = create_packet_dl_assign(ctx, data_ctx);
176 if (!data_ctx->msg)
177 return;
178 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_WAIT_ACK);
179 break;
180 default:
181 OSMO_ASSERT(0);
182 }
183}
184
185static void st_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
186{
Pau Espin Pedrol5bc65602021-07-27 18:09:58 +0200187 struct tbf_dl_ass_fsm_ctx *ctx = (struct tbf_dl_ass_fsm_ctx *)fi->priv;
188
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200189 switch (event) {
190 case TBF_DL_ASS_EV_RX_ASS_CTRL_ACK:
191 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_NONE);
192 break;
193 case TBF_DL_ASS_EV_ASS_POLL_TIMEOUT:
Pau Espin Pedrol5bc65602021-07-27 18:09:58 +0200194 LOGPTBF(ctx->tbf, LOGL_NOTICE,
195 "Timeout for polling PACKET CONTROL ACK for PACKET DOWNLINK ASSIGNMENT: %s\n",
196 tbf_rlcmac_diag(ctx->tbf));
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200197 /* Reschedule Pkt Dl Ass */
198 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_SEND_ASS);
199 break;
200 default:
201 OSMO_ASSERT(0);
202 }
203}
204
205static int tbf_dl_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
206{
207 struct tbf_dl_ass_fsm_ctx *ctx = (struct tbf_dl_ass_fsm_ctx *)fi->priv;
208 switch (fi->T) {
209 case -2000:
210 tbf_free(ctx->tbf);
211 break;
212 default:
213 OSMO_ASSERT(0);
214 }
215 return 0;
216}
217
218static struct osmo_fsm_state tbf_dl_ass_fsm_states[] = {
219 [TBF_DL_ASS_NONE] = {
220 .in_event_mask =
221 X(TBF_DL_ASS_EV_SCHED_ASS),
222 .out_state_mask =
223 X(TBF_DL_ASS_SEND_ASS),
224 .name = "NONE",
225 .action = st_none,
226 .onenter = st_none_on_enter,
227 },
228 [TBF_DL_ASS_SEND_ASS] = {
229 .in_event_mask = X(TBF_DL_ASS_EV_CREATE_RLCMAC_MSG),
230 .out_state_mask = X(TBF_DL_ASS_WAIT_ACK),
231 .name = "SEND_ASS",
232 .action = st_send_ass,
233 },
234 [TBF_DL_ASS_WAIT_ACK] = {
235 .in_event_mask =
236 X(TBF_DL_ASS_EV_RX_ASS_CTRL_ACK) |
237 X(TBF_DL_ASS_EV_ASS_POLL_TIMEOUT),
238 .out_state_mask =
239 X(TBF_DL_ASS_NONE) |
240 X(TBF_DL_ASS_SEND_ASS),
241 .name = "WAIT_ACK",
242 .action = st_wait_ack,
243 },
244};
245
246struct osmo_fsm tbf_dl_ass_fsm = {
247 .name = "DL_ASS_TBF",
248 .states = tbf_dl_ass_fsm_states,
249 .num_states = ARRAY_SIZE(tbf_dl_ass_fsm_states),
250 .timer_cb = tbf_dl_ass_fsm_timer_cb,
251 .log_subsys = DTBF,
252 .event_names = tbf_dl_ass_fsm_event_names,
253};
254
255static __attribute__((constructor)) void tbf_dl_ass_fsm_init(void)
256{
257 OSMO_ASSERT(osmo_fsm_register(&tbf_dl_ass_fsm) == 0);
258}
259
260
261struct msgb *tbf_dl_ass_create_rlcmac_msg(const struct gprs_rlcmac_tbf* tbf, uint32_t fn, uint8_t ts)
262{
263 int rc;
264 struct tbf_dl_ass_ev_create_rlcmac_msg_ctx data_ctx = {
265 .fn = fn,
266 .ts = ts,
267 .msg = NULL,
268 };
269
270 rc = osmo_fsm_inst_dispatch(tbf_dl_ass_fi(tbf), TBF_DL_ASS_EV_CREATE_RLCMAC_MSG, &data_ctx);
271 if (rc != 0 || !data_ctx.msg)
272 return NULL;
273 return data_ctx.msg;
274}
275
276bool tbf_dl_ass_rts(const struct gprs_rlcmac_tbf* tbf)
277{
278 struct osmo_fsm_inst *fi = tbf_dl_ass_fi(tbf);
279 return fi->state == TBF_DL_ASS_SEND_ASS;
280}