1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/slist.h>
11 #include <zephyr/sys/util.h>
12 
13 #include <zephyr/bluetooth/hci_types.h>
14 
15 #include "hal/ccm.h"
16 
17 #include "util/util.h"
18 #include "util/mem.h"
19 #include "util/memq.h"
20 #include "util/dbuf.h"
21 
22 #include "pdu_df.h"
23 #include "lll/pdu_vendor.h"
24 #include "pdu.h"
25 
26 #include "ll.h"
27 #include "ll_settings.h"
28 
29 #include "lll.h"
30 #include "lll/lll_df_types.h"
31 #include "lll_conn.h"
32 #include "lll_conn_iso.h"
33 
34 #include "ull_tx_queue.h"
35 
36 #include "isoal.h"
37 #include "ull_iso_types.h"
38 #include "ull_conn_iso_types.h"
39 #include "ull_conn_iso_internal.h"
40 
41 #include "ull_conn_types.h"
42 #include "ull_llcp.h"
43 #include "ull_llcp_internal.h"
44 #include "ull_conn_internal.h"
45 
46 #include <soc.h>
47 #include "hal/debug.h"
48 
49 /* Hardcoded instant delta +6 */
50 #define CHMU_INSTANT_DELTA 6U
51 
52 /* LLCP Local Procedure Channel Map Update FSM states */
53 enum {
54 	LP_CHMU_STATE_IDLE,
55 	LP_CHMU_STATE_WAIT_TX_CHAN_MAP_IND,
56 	LP_CHMU_STATE_WAIT_INSTANT,
57 };
58 
59 /* LLCP Local Procedure Channel Map Update FSM events */
60 enum {
61 	/* Procedure run */
62 	LP_CHMU_EVT_RUN,
63 };
64 
65 /* LLCP Remote Procedure Channel Map Update FSM states */
66 enum {
67 	RP_CHMU_STATE_IDLE,
68 	RP_CHMU_STATE_WAIT_RX_CHAN_MAP_IND,
69 	RP_CHMU_STATE_WAIT_INSTANT,
70 };
71 
72 /* LLCP Remote Procedure Channel Map Update FSM events */
73 enum {
74 	/* Procedure run */
75 	RP_CHMU_EVT_RUN,
76 
77 	/* Indication received */
78 	RP_CHMU_EVT_RX_CHAN_MAP_IND,
79 };
80 
81 #if defined(CONFIG_BT_CENTRAL)
82 /*
83  * LLCP Local Procedure Channel Map Update FSM
84  */
lp_chmu_tx(struct ll_conn * conn,struct proc_ctx * ctx)85 static void lp_chmu_tx(struct ll_conn *conn, struct proc_ctx *ctx)
86 {
87 	struct node_tx *tx;
88 	struct pdu_data *pdu;
89 
90 	/* Allocate tx node */
91 	tx = llcp_tx_alloc(conn, ctx);
92 	LL_ASSERT(tx);
93 
94 	pdu = (struct pdu_data *)tx->pdu;
95 
96 	/* Encode LL Control PDU */
97 	llcp_pdu_encode_chan_map_update_ind(ctx, pdu);
98 
99 	ctx->tx_opcode = pdu->llctrl.opcode;
100 
101 	/* Enqueue LL Control PDU towards LLL */
102 	llcp_tx_enqueue(conn, tx);
103 }
104 
lp_chmu_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)105 static void lp_chmu_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
106 {
107 	ull_conn_chan_map_set(conn, ctx->data.chmu.chm);
108 	llcp_lr_complete(conn);
109 	ctx->state = LP_CHMU_STATE_IDLE;
110 }
111 
lp_chmu_send_channel_map_update_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)112 static void lp_chmu_send_channel_map_update_ind(struct ll_conn *conn, struct proc_ctx *ctx,
113 						uint8_t evt, void *param)
114 {
115 	if (llcp_lr_ispaused(conn) || llcp_rr_get_collision(conn) ||
116 	    !llcp_tx_alloc_peek(conn, ctx)) {
117 		ctx->state = LP_CHMU_STATE_WAIT_TX_CHAN_MAP_IND;
118 	} else {
119 		llcp_rr_set_incompat(conn, INCOMPAT_RESOLVABLE);
120 
121 		ctx->data.chmu.instant = ull_conn_event_counter(conn) + CHMU_INSTANT_DELTA;
122 
123 		lp_chmu_tx(conn, ctx);
124 
125 		ctx->state = LP_CHMU_STATE_WAIT_INSTANT;
126 	}
127 }
128 
lp_chmu_st_wait_tx_chan_map_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)129 static void lp_chmu_st_wait_tx_chan_map_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
130 					    void *param)
131 {
132 	switch (evt) {
133 	case LP_CHMU_EVT_RUN:
134 		lp_chmu_send_channel_map_update_ind(conn, ctx, evt, param);
135 		break;
136 	default:
137 		/* Ignore other evts */
138 		break;
139 	}
140 }
141 
lp_chmu_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)142 static void lp_chmu_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
143 				  void *param)
144 {
145 	uint16_t event_counter = ull_conn_event_counter(conn);
146 
147 	if (is_instant_reached_or_passed(ctx->data.chmu.instant, event_counter)) {
148 		llcp_rr_set_incompat(conn, INCOMPAT_NO_COLLISION);
149 		lp_chmu_complete(conn, ctx, evt, param);
150 	}
151 }
152 
lp_chmu_st_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)153 static void lp_chmu_st_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
154 				    void *param)
155 {
156 	switch (evt) {
157 	case LP_CHMU_EVT_RUN:
158 		lp_chmu_check_instant(conn, ctx, evt, param);
159 		break;
160 	default:
161 		/* Ignore other evts */
162 		break;
163 	}
164 }
165 
lp_chmu_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)166 static void lp_chmu_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
167 				void *param)
168 {
169 	switch (ctx->state) {
170 	case LP_CHMU_STATE_IDLE:
171 		/* Empty/fallthrough on purpose as idle state handling is equivalent to
172 		 * 'wait for tx state' - simply to attempt TX'ing chan map ind
173 		 */
174 	case LP_CHMU_STATE_WAIT_TX_CHAN_MAP_IND:
175 		lp_chmu_st_wait_tx_chan_map_ind(conn, ctx, evt, param);
176 		break;
177 	case LP_CHMU_STATE_WAIT_INSTANT:
178 		lp_chmu_st_wait_instant(conn, ctx, evt, param);
179 		break;
180 	default:
181 		/* Unknown state */
182 		LL_ASSERT(0);
183 	}
184 }
185 
llcp_lp_chmu_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)186 void llcp_lp_chmu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
187 {
188 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
189 
190 	switch (pdu->llctrl.opcode) {
191 	default:
192 		/* Invalid behaviour */
193 		/* Invalid PDU received so terminate connection */
194 		conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
195 		llcp_lr_complete(conn);
196 		ctx->state = LP_CHMU_STATE_IDLE;
197 		break;
198 	}
199 }
200 
llcp_lp_chmu_init_proc(struct proc_ctx * ctx)201 void llcp_lp_chmu_init_proc(struct proc_ctx *ctx)
202 {
203 	ctx->state = LP_CHMU_STATE_IDLE;
204 }
205 
llcp_lp_chmu_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)206 void llcp_lp_chmu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
207 {
208 	lp_chmu_execute_fsm(conn, ctx, LP_CHMU_EVT_RUN, param);
209 }
210 
llcp_lp_chmu_awaiting_instant(struct proc_ctx * ctx)211 bool llcp_lp_chmu_awaiting_instant(struct proc_ctx *ctx)
212 {
213 	return (ctx->state == LP_CHMU_STATE_WAIT_INSTANT);
214 }
215 #endif /* CONFIG_BT_CENTRAL */
216 
217 #if defined(CONFIG_BT_PERIPHERAL)
218 /*
219  * LLCP Remote Procedure Channel Map Update FSM
220  */
rp_chmu_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)221 static void rp_chmu_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
222 {
223 	ull_conn_chan_map_set(conn, ctx->data.chmu.chm);
224 	llcp_rr_complete(conn);
225 	ctx->state = RP_CHMU_STATE_IDLE;
226 }
227 
rp_chmu_st_idle(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)228 static void rp_chmu_st_idle(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
229 {
230 	switch (evt) {
231 	case RP_CHMU_EVT_RUN:
232 		ctx->state = RP_CHMU_STATE_WAIT_RX_CHAN_MAP_IND;
233 		break;
234 	default:
235 		/* Ignore other evts */
236 		break;
237 	}
238 }
239 
rp_chmu_st_wait_rx_channel_map_update_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)240 static void rp_chmu_st_wait_rx_channel_map_update_ind(struct ll_conn *conn, struct proc_ctx *ctx,
241 						      uint8_t evt, void *param)
242 {
243 	switch (evt) {
244 	case RP_CHMU_EVT_RX_CHAN_MAP_IND:
245 		llcp_pdu_decode_chan_map_update_ind(ctx, param);
246 		if (is_instant_not_passed(ctx->data.chmu.instant,
247 					  ull_conn_event_counter(conn))) {
248 
249 			ctx->state = RP_CHMU_STATE_WAIT_INSTANT;
250 		} else {
251 			conn->llcp_terminate.reason_final = BT_HCI_ERR_INSTANT_PASSED;
252 			llcp_rr_complete(conn);
253 			ctx->state = RP_CHMU_STATE_IDLE;
254 		}
255 		break;
256 	default:
257 		/* Ignore other evts */
258 		break;
259 	}
260 }
261 
rp_chmu_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)262 static void rp_chmu_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
263 				  void *param)
264 {
265 	uint16_t event_counter = ull_conn_event_counter(conn);
266 
267 	if (((event_counter - ctx->data.chmu.instant) & 0xFFFF) <= 0x7FFF) {
268 		rp_chmu_complete(conn, ctx, evt, param);
269 	}
270 }
271 
rp_chmu_st_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)272 static void rp_chmu_st_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
273 				    void *param)
274 {
275 	switch (evt) {
276 	case RP_CHMU_EVT_RUN:
277 		rp_chmu_check_instant(conn, ctx, evt, param);
278 		break;
279 	default:
280 		/* Ignore other evts */
281 		break;
282 	}
283 }
284 
rp_chmu_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)285 static void rp_chmu_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
286 				void *param)
287 {
288 	switch (ctx->state) {
289 	case RP_CHMU_STATE_IDLE:
290 		rp_chmu_st_idle(conn, ctx, evt, param);
291 		break;
292 	case RP_CHMU_STATE_WAIT_RX_CHAN_MAP_IND:
293 		rp_chmu_st_wait_rx_channel_map_update_ind(conn, ctx, evt, param);
294 		break;
295 	case RP_CHMU_STATE_WAIT_INSTANT:
296 		rp_chmu_st_wait_instant(conn, ctx, evt, param);
297 		break;
298 	default:
299 		/* Unknown state */
300 		LL_ASSERT(0);
301 	}
302 }
303 
llcp_rp_chmu_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)304 void llcp_rp_chmu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
305 {
306 	struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
307 
308 	switch (pdu->llctrl.opcode) {
309 	case PDU_DATA_LLCTRL_TYPE_CHAN_MAP_IND:
310 		rp_chmu_execute_fsm(conn, ctx, RP_CHMU_EVT_RX_CHAN_MAP_IND, pdu);
311 		break;
312 	default:
313 		/* Invalid behaviour */
314 		/* Invalid PDU received so terminate connection */
315 		conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
316 		llcp_rr_complete(conn);
317 		ctx->state = RP_CHMU_STATE_IDLE;
318 		break;
319 	}
320 }
321 
llcp_rp_chmu_init_proc(struct proc_ctx * ctx)322 void llcp_rp_chmu_init_proc(struct proc_ctx *ctx)
323 {
324 	ctx->state = RP_CHMU_STATE_IDLE;
325 }
326 
llcp_rp_chmu_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)327 void llcp_rp_chmu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
328 {
329 	rp_chmu_execute_fsm(conn, ctx, RP_CHMU_EVT_RUN, param);
330 }
331 
llcp_rp_chmu_awaiting_instant(struct proc_ctx * ctx)332 bool llcp_rp_chmu_awaiting_instant(struct proc_ctx *ctx)
333 {
334 	return (ctx->state == RP_CHMU_STATE_WAIT_INSTANT);
335 }
336 #endif /* CONFIG_BT_PERIPHERAL */
337