1 /*
2  * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 enum llcp {
8 	LLCP_NONE,
9 	LLCP_CONN_UPD,
10 	LLCP_CHAN_MAP,
11 
12 	/*
13 	 * LLCP_TERMINATE,
14 	 * LLCP_FEATURE_EXCHANGE,
15 	 * LLCP_VERSION_EXCHANGE,
16 	 */
17 
18 #if defined(CONFIG_BT_CTLR_LE_ENC)
19 	LLCP_ENCRYPTION,
20 #endif /* CONFIG_BT_CTLR_LE_ENC */
21 
22 	LLCP_CONNECTION_PARAM_REQ,
23 
24 #if defined(CONFIG_BT_CTLR_LE_PING)
25 	LLCP_PING,
26 #endif /* CONFIG_BT_CTLR_LE_PING */
27 
28 #if defined(CONFIG_BT_CTLR_PHY)
29 	LLCP_PHY_UPD,
30 #endif /* CONFIG_BT_CTLR_PHY */
31 };
32 
33 struct ll_conn {
34 	struct ull_hdr  ull;
35 	struct lll_conn lll;
36 
37 	uint16_t connect_expire;
38 	uint16_t supervision_reload;
39 	uint16_t supervision_expire;
40 	uint16_t procedure_reload;
41 	uint16_t procedure_expire;
42 
43 #if defined(CONFIG_BT_CTLR_LE_PING)
44 	uint16_t appto_reload;
45 	uint16_t appto_expire;
46 	uint16_t apto_reload;
47 	uint16_t apto_expire;
48 #endif /* CONFIG_BT_CTLR_LE_PING */
49 
50 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
51 	uint16_t default_tx_octets;
52 
53 #if defined(CONFIG_BT_CTLR_PHY)
54 	uint16_t default_tx_time;
55 #endif /* CONFIG_BT_CTLR_PHY */
56 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
57 
58 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN)
59 	uint8_t own_id_addr_type:1;
60 	uint8_t peer_id_addr_type:1;
61 	uint8_t own_id_addr[BDADDR_SIZE];
62 	uint8_t peer_id_addr[BDADDR_SIZE];
63 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN */
64 
65 	union {
66 		struct {
67 			uint8_t fex_valid:1;
68 			uint8_t txn_lock:1;
69 #if defined(CONFIG_BT_CTLR_CONN_META)
70 			uint8_t is_must_expire:1;
71 #endif /* CONFIG_BT_CTLR_CONN_META */
72 		} common;
73 
74 #if defined(CONFIG_BT_PERIPHERAL)
75 		struct {
76 			uint8_t  fex_valid:1;
77 			uint8_t  txn_lock:1;
78 #if defined(CONFIG_BT_CTLR_CONN_META)
79 			uint8_t  is_must_expire:1;
80 #endif /* CONFIG_BT_CTLR_CONN_META */
81 			uint8_t  latency_cancel:1;
82 			uint8_t  sca:3;
83 #if defined(CONFIG_BT_CTLR_LE_ENC)
84 			uint8_t  llcp_type;
85 #endif /* CONFIG_BT_CTLR_LE_ENC */
86 #if defined(CONFIG_BT_CTLR_CONN_RANDOM_FORCE)
87 			uint32_t force;
88 #endif /* CONFIG_BT_CTLR_CONN_RANDOM_FORCE */
89 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
90 			uint32_t ticks_to_offset;
91 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
92 		} periph;
93 #endif /* CONFIG_BT_PERIPHERAL */
94 
95 #if defined(CONFIG_BT_CENTRAL)
96 		struct {
97 			uint8_t fex_valid:1;
98 			uint8_t txn_lock:1;
99 #if defined(CONFIG_BT_CTLR_CONN_META)
100 			uint8_t is_must_expire:1;
101 #endif /* CONFIG_BT_CTLR_CONN_META */
102 			uint8_t terminate_ack:1;
103 		} central;
104 #endif /* CONFIG_BT_CENTRAL */
105 	};
106 
107 	uint8_t llcp_req;
108 	uint8_t llcp_ack;
109 
110 	uint8_t llcp_type;
111 
112 	struct {
113 		union {
114 			struct {
115 				uint16_t instant;
116 				uint16_t *pdu_win_offset;
117 				uint32_t ticks_anchor;
118 			} conn_upd;
119 
120 			struct {
121 				uint8_t  initiate:1;
122 				uint8_t  chm[5];
123 				uint16_t instant;
124 			} chan_map;
125 
126 #if defined(CONFIG_BT_CTLR_PHY)
127 			struct {
128 				uint8_t initiate:1;
129 				uint8_t cmd:1;
130 				uint8_t tx:3;
131 				uint8_t rx:3;
132 				uint16_t instant;
133 			} phy_upd_ind;
134 #endif /* CONFIG_BT_CTLR_PHY */
135 		};
136 
137 #if defined(CONFIG_BT_CTLR_LE_ENC)
138 		struct {
139 			enum {
140 				LLCP_ENC_STATE_INPROG,
141 				LLCP_ENC_STATE_INIT,
142 				LLCP_ENC_STATE_LTK_WAIT,
143 				LLCP_ENC_STATE_ENC_WAIT,
144 			} state:2 __packed;
145 			uint8_t  error_code;
146 			uint8_t  skd[16];
147 		} encryption;
148 #endif /* CONFIG_BT_CTLR_LE_ENC */
149 	} llcp;
150 
151 	struct node_rx_pdu *llcp_rx;
152 
153 	struct {
154 		uint8_t  req;
155 		uint8_t  ack;
156 		enum {
157 			LLCP_CUI_STATE_INPROG,
158 			LLCP_CUI_STATE_USE,
159 			LLCP_CUI_STATE_SELECT,
160 			LLCP_CUI_STATE_OFFS_REQ,
161 			LLCP_CUI_STATE_OFFS_RDY,
162 			LLCP_CUI_STATE_REJECT,
163 		} state:3 __packed;
164 		uint8_t  cmd:1;
165 		uint16_t interval;
166 		uint16_t latency;
167 		uint16_t timeout;
168 		uint32_t win_offset_us;
169 		uint8_t  win_size;
170 	} llcp_cu;
171 
172 	struct {
173 		uint8_t  req;
174 		uint8_t  ack;
175 		/* TODO: 8, 16, 32 or 64 based on local supported features */
176 		uint64_t features_conn;
177 		uint64_t features_peer;
178 	} llcp_feature;
179 
180 	struct {
181 		uint8_t  req;
182 		uint8_t  ack;
183 		uint8_t  tx:1;
184 		uint8_t  rx:1;
185 		uint8_t  version_number;
186 		uint16_t company_id;
187 		uint16_t sub_version_number;
188 	} llcp_version;
189 
190 	struct {
191 		uint8_t req;
192 		uint8_t ack;
193 		uint8_t reason_own;
194 		uint8_t reason_final;
195 		/* node rx type with memory aligned storage for terminate
196 		 * reason.
197 		 * HCI will reference the value using the pdu member of
198 		 * struct node_rx_pdu.
199 		 */
200 		struct {
201 			struct node_rx_hdr hdr;
202 			union {
203 				uint8_t    pdu[0] __aligned(4);
204 				uint8_t    reason;
205 			};
206 		} node_rx;
207 	} llcp_terminate;
208 
209 #if defined(CONFIG_BT_CTLR_LE_ENC)
210 	struct {
211 		uint8_t req;
212 		uint8_t ack;
213 		uint8_t pause_rx:1;
214 		uint8_t pause_tx:1;
215 		uint8_t refresh:1;
216 		uint8_t ediv[2];
217 		uint8_t rand[8];
218 		uint8_t ltk[16];
219 	} llcp_enc;
220 #endif /* CONFIG_BT_CTLR_LE_ENC */
221 
222 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
223 	struct {
224 		uint8_t  req;
225 		uint8_t  ack;
226 		enum {
227 			LLCP_CPR_STATE_REQ,
228 			LLCP_CPR_STATE_RSP,
229 			LLCP_CPR_STATE_APP_REQ,
230 			LLCP_CPR_STATE_APP_WAIT,
231 			LLCP_CPR_STATE_RSP_WAIT,
232 			LLCP_CPR_STATE_UPD_WAIT,
233 			LLCP_CPR_STATE_UPD,
234 			LLCP_CPR_STATE_OFFS_REQ,
235 			LLCP_CPR_STATE_OFFS_RDY,
236 		} state:4 __packed;
237 		uint8_t  cmd:1;
238 		uint8_t  disabled:1;
239 		uint8_t  status;
240 		uint16_t interval_min;
241 		uint16_t interval_max;
242 		uint16_t latency;
243 		uint16_t timeout;
244 		uint8_t  preferred_periodicity;
245 		uint16_t reference_conn_event_count;
246 		uint16_t offset0;
247 		uint16_t offset1;
248 		uint16_t offset2;
249 		uint16_t offset3;
250 		uint16_t offset4;
251 		uint16_t offset5;
252 		uint16_t *pdu_win_offset0;
253 		uint32_t ticks_ref;
254 		uint32_t ticks_to_offset_next;
255 	} llcp_conn_param;
256 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
257 
258 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
259 	struct {
260 		uint8_t  req;
261 		uint8_t  ack;
262 		uint8_t  state:3;
263 #define LLCP_LENGTH_STATE_REQ                 0
264 #define LLCP_LENGTH_STATE_REQ_ACK_WAIT        1
265 #define LLCP_LENGTH_STATE_RSP_WAIT            2
266 #define LLCP_LENGTH_STATE_RSP_ACK_WAIT        3
267 #define LLCP_LENGTH_STATE_RESIZE              4
268 #define LLCP_LENGTH_STATE_RESIZE_RSP          5
269 #define LLCP_LENGTH_STATE_RESIZE_RSP_ACK_WAIT 6
270 		uint8_t  disabled:1;
271 		uint16_t rx_octets;
272 		uint16_t tx_octets;
273 #if defined(CONFIG_BT_CTLR_PHY)
274 		uint16_t rx_time;
275 		uint16_t tx_time;
276 #endif /* CONFIG_BT_CTLR_PHY */
277 		struct {
278 			uint16_t tx_octets;
279 #if defined(CONFIG_BT_CTLR_PHY)
280 			uint16_t tx_time;
281 #endif /* CONFIG_BT_CTLR_PHY */
282 		} cache;
283 	} llcp_length;
284 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
285 
286 #if defined(CONFIG_BT_CTLR_PHY)
287 	struct {
288 		uint8_t req;
289 		uint8_t ack;
290 		uint8_t state:2;
291 #define LLCP_PHY_STATE_REQ      0
292 #define LLCP_PHY_STATE_ACK_WAIT 1
293 #define LLCP_PHY_STATE_RSP_WAIT 2
294 #define LLCP_PHY_STATE_UPD      3
295 		uint8_t tx:3;
296 		uint8_t rx:3;
297 		uint8_t pause_tx:1;
298 		uint8_t flags:1;
299 		uint8_t cmd:1;
300 		uint8_t disabled:1;
301 	} llcp_phy;
302 
303 	uint8_t phy_pref_tx:3;
304 	uint8_t phy_pref_rx:3;
305 #endif /* CONFIG_BT_CTLR_PHY */
306 
307 #if defined(CONFIG_BT_CTLR_LLID_DATA_START_EMPTY)
308 	/* Detect empty L2CAP start frame */
309 	uint8_t  start_empty:1;
310 #endif /* CONFIG_BT_CTLR_LLID_DATA_START_EMPTY */
311 
312 	struct node_tx *tx_head;
313 	struct node_tx *tx_ctrl;
314 	struct node_tx *tx_ctrl_last;
315 	struct node_tx *tx_data;
316 	struct node_tx *tx_data_last;
317 
318 	uint8_t chm_updated;
319 
320 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
321 	struct {
322 		uint8_t  req;
323 		uint8_t  ack;
324 		enum {
325 			LLCP_CIS_STATE_REQ,
326 			LLCP_CIS_STATE_RSP_WAIT,
327 			LLCP_CIS_STATE_IND_WAIT,
328 			LLCP_CIS_STATE_INST_WAIT
329 		} state:8 __packed;
330 		uint8_t  cig_id;
331 		uint16_t cis_handle;
332 		uint8_t  cis_id;
333 		uint32_t c_max_sdu:12;
334 		uint32_t p_max_sdu:12;
335 		uint32_t framed:1;
336 		uint32_t cis_offset_min;
337 		uint32_t cis_offset_max;
338 		uint16_t conn_event_count;
339 	} llcp_cis;
340 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
341 
342 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
343 	struct lll_df_conn_rx_params df_rx_params;
344 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
345 };
346 
347 struct node_rx_cc {
348 	uint8_t  status;
349 	uint8_t  role;
350 	uint8_t  peer_addr_type;
351 	uint8_t  peer_addr[BDADDR_SIZE];
352 #if defined(CONFIG_BT_CTLR_PRIVACY)
353 	uint8_t  peer_rpa[BDADDR_SIZE];
354 	uint8_t  local_rpa[BDADDR_SIZE];
355 #endif /* CONFIG_BT_CTLR_PRIVACY */
356 	uint16_t interval;
357 	uint16_t latency;
358 	uint16_t timeout;
359 	uint8_t  sca;
360 };
361 
362 struct node_rx_cu {
363 	uint8_t  status;
364 	uint16_t interval;
365 	uint16_t latency;
366 	uint16_t timeout;
367 };
368 
369 struct node_rx_cs {
370 	uint8_t csa;
371 };
372 
373 struct node_rx_pu {
374 	uint8_t status;
375 	uint8_t tx;
376 	uint8_t rx;
377 };
378