blob: 37ced616cd50d25ff30189ec5fe6b6c122a4d1f1 [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
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +020068 rc = tbf_check_polling(ctx->tbf, d->fn, d->ts, &new_poll_fn, &rrbp);
69 if (rc < 0)
70 return NULL;
71
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +020072 new_dl_tbf = ms_dl_tbf(ms);
73 if (!new_dl_tbf) {
74 LOGPTBF(ctx->tbf, LOGL_ERROR,
75 "We have a schedule for downlink assignment, but there is no downlink TBF\n");
76 tbf_dl_ass_fsm_state_chg(ctx->fi, TBF_DL_ASS_NONE);
77 return NULL;
78 }
79
80 if (new_dl_tbf == as_dl_tbf(ctx->tbf))
81 LOGPTBF(ctx->tbf, LOGL_DEBUG, "New and old TBF are the same.\n");
82
83 if (old_tfi_is_valid && ms_tlli(ms) == GSM_RESERVED_TMSI) {
84 LOGPTBF(ctx->tbf, LOGL_ERROR,
85 "The old TFI is not assigned and there is no TLLI. New TBF %s\n",
86 tbf_name((struct gprs_rlcmac_tbf *)new_dl_tbf));
87 tbf_dl_ass_fsm_state_chg(ctx->fi, TBF_DL_ASS_NONE);
88 return NULL;
89 }
90
91 msg = msgb_alloc(GSM_MACBLOCK_LEN, "rlcmac_dl_ass");
92 if (!msg)
93 return NULL;
94
95 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
96 struct bitvec bv = {
97 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
98 .data_len = GSM_MACBLOCK_LEN,
99 };
100 bitvec_unhex(&bv, DUMMY_VEC);
101
102 LOGPTBF((struct gprs_rlcmac_tbf *)new_dl_tbf, LOGL_INFO, "start Packet Downlink Assignment (PACCH)\n");
103 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->tbf, RlcMacDownlink_t);
104 write_packet_downlink_assignment(mac_control_block, old_tfi_is_valid,
105 tbf_tfi(ctx->tbf), (tbf_direction(ctx->tbf) == GPRS_RLCMAC_DL_TBF),
106 new_dl_tbf, poll_ass_dl, rrbp,
107 bts_get_ms_pwr_alpha(ms->bts), the_pcu->vty.gamma, -1, 0,
108 tbf_is_egprs_enabled(ctx->tbf), tbf_state(ctx->tbf) == TBF_ST_WAIT_RELEASE);
109 LOGP(DTBF, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n");
110 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
111 if (rc < 0) {
112 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Downlink Ass failed (%d)\n", rc);
113 goto free_ret;
114 }
115 LOGP(DTBF, LOGL_DEBUG, "------------------------- TX : Packet Downlink Assignment -------------------------\n");
116 bts_do_rate_ctr_inc(ms->bts, CTR_PKT_DL_ASSIGNMENT);
117
118 tbf_set_polling(ctx->tbf, new_poll_fn, d->ts, PDCH_ULC_POLL_DL_ASS);
Pau Espin Pedrola161bf42021-07-30 13:42:06 +0200119 LOGPTBFDL(ctx->tbf, LOGL_INFO, "Scheduled DL Assignment polling on PACCH (FN=%d, TS=%d)\n",
120 new_poll_fn, d->ts);
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200121
122 talloc_free(mac_control_block);
123 return msg;
124
125free_ret:
126 talloc_free(mac_control_block);
127 msgb_free(msg);
128 return NULL;
129}
130
131static void st_none_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
132{
133}
134
135static void st_none(struct osmo_fsm_inst *fi, uint32_t event, void *data)
136{
137 switch (event) {
138 case TBF_DL_ASS_EV_SCHED_ASS:
139 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_SEND_ASS);
140 break;
141 default:
142 OSMO_ASSERT(0);
143 }
144}
145
146static void st_send_ass(struct osmo_fsm_inst *fi, uint32_t event, void *data)
147{
148 struct tbf_dl_ass_fsm_ctx *ctx = (struct tbf_dl_ass_fsm_ctx *)fi->priv;
149 struct tbf_dl_ass_ev_create_rlcmac_msg_ctx *data_ctx;
150
151 switch (event) {
152 case TBF_DL_ASS_EV_CREATE_RLCMAC_MSG:
153 data_ctx = (struct tbf_dl_ass_ev_create_rlcmac_msg_ctx *)data;
154 data_ctx->msg = create_packet_dl_assign(ctx, data_ctx);
155 if (!data_ctx->msg)
156 return;
157 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_WAIT_ACK);
158 break;
159 default:
160 OSMO_ASSERT(0);
161 }
162}
163
164static void st_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
165{
Pau Espin Pedrol5bc65602021-07-27 18:09:58 +0200166 struct tbf_dl_ass_fsm_ctx *ctx = (struct tbf_dl_ass_fsm_ctx *)fi->priv;
167
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200168 switch (event) {
169 case TBF_DL_ASS_EV_RX_ASS_CTRL_ACK:
170 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_NONE);
171 break;
172 case TBF_DL_ASS_EV_ASS_POLL_TIMEOUT:
Pau Espin Pedrol5bc65602021-07-27 18:09:58 +0200173 LOGPTBF(ctx->tbf, LOGL_NOTICE,
174 "Timeout for polling PACKET CONTROL ACK for PACKET DOWNLINK ASSIGNMENT: %s\n",
175 tbf_rlcmac_diag(ctx->tbf));
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200176 /* Reschedule Pkt Dl Ass */
177 tbf_dl_ass_fsm_state_chg(fi, TBF_DL_ASS_SEND_ASS);
178 break;
179 default:
180 OSMO_ASSERT(0);
181 }
182}
183
184static int tbf_dl_ass_fsm_timer_cb(struct osmo_fsm_inst *fi)
185{
186 struct tbf_dl_ass_fsm_ctx *ctx = (struct tbf_dl_ass_fsm_ctx *)fi->priv;
187 switch (fi->T) {
188 case -2000:
189 tbf_free(ctx->tbf);
190 break;
191 default:
192 OSMO_ASSERT(0);
193 }
194 return 0;
195}
196
197static struct osmo_fsm_state tbf_dl_ass_fsm_states[] = {
198 [TBF_DL_ASS_NONE] = {
199 .in_event_mask =
200 X(TBF_DL_ASS_EV_SCHED_ASS),
201 .out_state_mask =
202 X(TBF_DL_ASS_SEND_ASS),
203 .name = "NONE",
204 .action = st_none,
205 .onenter = st_none_on_enter,
206 },
207 [TBF_DL_ASS_SEND_ASS] = {
208 .in_event_mask = X(TBF_DL_ASS_EV_CREATE_RLCMAC_MSG),
209 .out_state_mask = X(TBF_DL_ASS_WAIT_ACK),
210 .name = "SEND_ASS",
211 .action = st_send_ass,
212 },
213 [TBF_DL_ASS_WAIT_ACK] = {
214 .in_event_mask =
215 X(TBF_DL_ASS_EV_RX_ASS_CTRL_ACK) |
216 X(TBF_DL_ASS_EV_ASS_POLL_TIMEOUT),
217 .out_state_mask =
218 X(TBF_DL_ASS_NONE) |
219 X(TBF_DL_ASS_SEND_ASS),
220 .name = "WAIT_ACK",
221 .action = st_wait_ack,
222 },
223};
224
225struct osmo_fsm tbf_dl_ass_fsm = {
226 .name = "DL_ASS_TBF",
227 .states = tbf_dl_ass_fsm_states,
228 .num_states = ARRAY_SIZE(tbf_dl_ass_fsm_states),
229 .timer_cb = tbf_dl_ass_fsm_timer_cb,
230 .log_subsys = DTBF,
231 .event_names = tbf_dl_ass_fsm_event_names,
232};
233
234static __attribute__((constructor)) void tbf_dl_ass_fsm_init(void)
235{
236 OSMO_ASSERT(osmo_fsm_register(&tbf_dl_ass_fsm) == 0);
237}
238
239
240struct msgb *tbf_dl_ass_create_rlcmac_msg(const struct gprs_rlcmac_tbf* tbf, uint32_t fn, uint8_t ts)
241{
242 int rc;
243 struct tbf_dl_ass_ev_create_rlcmac_msg_ctx data_ctx = {
244 .fn = fn,
245 .ts = ts,
246 .msg = NULL,
247 };
248
249 rc = osmo_fsm_inst_dispatch(tbf_dl_ass_fi(tbf), TBF_DL_ASS_EV_CREATE_RLCMAC_MSG, &data_ctx);
250 if (rc != 0 || !data_ctx.msg)
251 return NULL;
252 return data_ctx.msg;
253}
254
Pau Espin Pedrol78ddfbc2021-10-12 13:08:27 +0200255bool tbf_dl_ass_rts(const struct gprs_rlcmac_tbf *tbf)
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200256{
257 struct osmo_fsm_inst *fi = tbf_dl_ass_fi(tbf);
Pau Espin Pedrol78ddfbc2021-10-12 13:08:27 +0200258 if (fi->state != TBF_DL_ASS_SEND_ASS)
259 return false;
260
261 if (tbf_ul_ass_fi(tbf)->state == TBF_UL_ASS_WAIT_ACK) {
262 LOGPTBF(tbf, LOGL_DEBUG,
263 "Polling is already scheduled, so we must wait for the uplink assignment...\n");
264 return false;
265 }
266
267 /* on uplink TBF we get the downlink TBF to be assigned. */
268 if (tbf_direction(tbf) == GPRS_RLCMAC_UL_TBF) {
269 const struct gprs_rlcmac_ul_tbf *ul_tbf = (const struct gprs_rlcmac_ul_tbf *)tbf;
270 /* be sure to check first, if contention resolution is done,
271 * otherwise we cannot send the assignment yet (3GPP TS 44.060 sec 7.1.3.1) */
272 if (!ul_tbf_contention_resolution_done(ul_tbf)) {
273 LOGPTBF(tbf, LOGL_DEBUG,
274 "Cannot assign DL TBF now, because contention resolution is not finished.\n");
275 return false;
276 }
277 }
278 return true;
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200279}