1 /*
2  * Copyright (c) 2018-2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #if defined(CONFIG_BT_CTLR_RX_PDU_META)
8 #include "lll_meta.h"
9 #endif /* CONFIG_BT_CTLR_RX_PDU_META */
10 
11 #define TICKER_INSTANCE_ID_CTLR 0
12 #define TICKER_USER_ID_LLL      MAYFLY_CALL_ID_0
13 #define TICKER_USER_ID_ULL_HIGH MAYFLY_CALL_ID_1
14 #define TICKER_USER_ID_ULL_LOW  MAYFLY_CALL_ID_2
15 #define TICKER_USER_ID_THREAD   MAYFLY_CALL_ID_PROGRAM
16 
17 #define EVENT_PIPELINE_MAX 7
18 
19 #define ADV_INT_UNIT_US      625U
20 #define SCAN_INT_UNIT_US     625U
21 #define CONN_INT_UNIT_US     1250U
22 #define ISO_INT_UNIT_US      CONN_INT_UNIT_US
23 #define PERIODIC_INT_UNIT_US 1250U
24 
25 /* Timeout for Host to accept/reject cis create request */
26 /* See BTCore5.3, 4.E.6.7 - Default value 0x1f40 * 625us */
27 #define DEFAULT_CONNECTION_ACCEPT_TIMEOUT_US (5 * USEC_PER_SEC)
28 
29 /* Intervals after which connection or sync establishment is considered lost */
30 #define CONN_ESTAB_COUNTDOWN 6U
31 
32 #if defined(CONFIG_BT_CTLR_XTAL_ADVANCED)
33 #define XON_BITMASK BIT(31) /* XTAL has been retained from previous prepare */
34 #endif /* CONFIG_BT_CTLR_XTAL_ADVANCED */
35 
36 #if defined(CONFIG_BT_BROADCASTER)
37 #if defined(CONFIG_BT_CTLR_ADV_SET)
38 #define BT_CTLR_ADV_SET CONFIG_BT_CTLR_ADV_SET
39 #else /* CONFIG_BT_CTLR_ADV_SET */
40 #define BT_CTLR_ADV_SET 1
41 #endif /* CONFIG_BT_CTLR_ADV_SET */
42 #else /* !CONFIG_BT_BROADCASTER */
43 #define BT_CTLR_ADV_SET 0
44 #endif /* !CONFIG_BT_BROADCASTER */
45 
46 #if defined(CONFIG_BT_OBSERVER)
47 #if defined(CONFIG_BT_CTLR_ADV_EXT)
48 #if defined(CONFIG_BT_CTLR_PHY_CODED)
49 #define BT_CTLR_SCAN_SET 2
50 #else /* !CONFIG_BT_CTLR_PHY_CODED */
51 #define BT_CTLR_SCAN_SET 1
52 #endif /* !CONFIG_BT_CTLR_PHY_CODED */
53 #else /* !CONFIG_BT_CTLR_ADV_EXT */
54 #define BT_CTLR_SCAN_SET 1
55 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
56 #else /* !CONFIG_BT_OBSERVER */
57 #define BT_CTLR_SCAN_SET 0
58 #endif /* !CONFIG_BT_OBSERVER */
59 
60 enum {
61 	TICKER_ID_LLL_PREEMPT = 0,
62 
63 #if defined(CONFIG_BT_BROADCASTER)
64 	TICKER_ID_ADV_STOP,
65 	TICKER_ID_ADV_BASE,
66 #if defined(CONFIG_BT_CTLR_ADV_EXT) || defined(CONFIG_BT_HCI_MESH_EXT)
67 	TICKER_ID_ADV_LAST = ((TICKER_ID_ADV_BASE) + (BT_CTLR_ADV_SET) - 1),
68 #if defined(CONFIG_BT_CTLR_ADV_EXT)
69 #if (CONFIG_BT_CTLR_ADV_AUX_SET > 0)
70 	TICKER_ID_ADV_AUX_BASE,
71 	TICKER_ID_ADV_AUX_LAST = ((TICKER_ID_ADV_AUX_BASE) +
72 				  (CONFIG_BT_CTLR_ADV_AUX_SET) - 1),
73 #if defined(CONFIG_BT_CTLR_ADV_PERIODIC)
74 	TICKER_ID_ADV_SYNC_BASE,
75 	TICKER_ID_ADV_SYNC_LAST = ((TICKER_ID_ADV_SYNC_BASE) +
76 				   (CONFIG_BT_CTLR_ADV_SYNC_SET) - 1),
77 #if defined(CONFIG_BT_CTLR_ADV_ISO)
78 	TICKER_ID_ADV_ISO_BASE,
79 	TICKER_ID_ADV_ISO_LAST = ((TICKER_ID_ADV_ISO_BASE) +
80 				  (CONFIG_BT_CTLR_ADV_ISO_SET) - 1),
81 #endif /* CONFIG_BT_CTLR_ADV_ISO */
82 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
83 #endif /* CONFIG_BT_CTLR_ADV_AUX_SET > 0 */
84 #endif /* CONFIG_BT_CTLR_ADV_EXT */
85 #endif /* !CONFIG_BT_CTLR_ADV_EXT || !CONFIG_BT_HCI_MESH_EXT */
86 #endif /* CONFIG_BT_BROADCASTER */
87 
88 #if defined(CONFIG_BT_OBSERVER)
89 	TICKER_ID_SCAN_STOP,
90 	TICKER_ID_SCAN_BASE,
91 	TICKER_ID_SCAN_LAST = ((TICKER_ID_SCAN_BASE) + (BT_CTLR_SCAN_SET) - 1),
92 #if defined(CONFIG_BT_CTLR_ADV_EXT)
93 	TICKER_ID_SCAN_AUX_BASE,
94 	TICKER_ID_SCAN_AUX_LAST = ((TICKER_ID_SCAN_AUX_BASE) +
95 				   (CONFIG_BT_CTLR_SCAN_AUX_SET) - 1),
96 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
97 	TICKER_ID_SCAN_SYNC_BASE,
98 	TICKER_ID_SCAN_SYNC_LAST = ((TICKER_ID_SCAN_SYNC_BASE) +
99 				    (CONFIG_BT_PER_ADV_SYNC_MAX) - 1),
100 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
101 	TICKER_ID_SCAN_SYNC_ISO_BASE,
102 	TICKER_ID_SCAN_SYNC_ISO_LAST = ((TICKER_ID_SCAN_SYNC_ISO_BASE) +
103 					(CONFIG_BT_CTLR_SCAN_SYNC_ISO_SET) - 1),
104 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
105 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
106 #endif /* CONFIG_BT_CTLR_ADV_EXT */
107 #endif /* CONFIG_BT_OBSERVER */
108 
109 #if defined(CONFIG_BT_CONN)
110 	TICKER_ID_CONN_BASE,
111 	TICKER_ID_CONN_LAST = ((TICKER_ID_CONN_BASE) + (CONFIG_BT_MAX_CONN) -
112 			       1),
113 #endif /* CONFIG_BT_CONN */
114 
115 #if defined(CONFIG_BT_CTLR_CONN_ISO)
116 	TICKER_ID_CONN_ISO_BASE,
117 	TICKER_ID_CONN_ISO_LAST = ((TICKER_ID_CONN_ISO_BASE) +
118 				   (CONFIG_BT_CTLR_CONN_ISO_GROUPS) - 1),
119 	TICKER_ID_CONN_ISO_RESUME_BASE,
120 	TICKER_ID_CONN_ISO_RESUME_LAST = ((TICKER_ID_CONN_ISO_RESUME_BASE) +
121 					  (CONFIG_BT_CTLR_CONN_ISO_GROUPS) - 1),
122 #endif /* CONFIG_BT_CTLR_CONN_ISO */
123 
124 #if defined(CONFIG_BT_CTLR_USER_EXT) && \
125 	(CONFIG_BT_CTLR_USER_TICKER_ID_RANGE > 0)
126 	TICKER_ID_USER_BASE,
127 	TICKER_ID_USER_LAST = (TICKER_ID_USER_BASE +
128 			       CONFIG_BT_CTLR_USER_TICKER_ID_RANGE - 1),
129 #endif /* CONFIG_BT_CTLR_USER_EXT */
130 
131 	TICKER_ID_MAX,
132 };
133 
134 #if defined(CONFIG_BT_BROADCASTER) && !defined(CONFIG_BT_CTLR_ADV_EXT) && \
135 	!defined(CONFIG_BT_HCI_MESH_EXT)
136 #define TICKER_ID_ADV_LAST TICKER_ID_ADV_BASE
137 #endif
138 
139 /* Define the Broadcast ISO Stream Handle base value */
140 #if defined(CONFIG_BT_CTLR_ADV_ISO)
141 #define BT_CTLR_ADV_ISO_STREAM_MAX CONFIG_BT_CTLR_ADV_ISO_STREAM_MAX
142 #if defined(CONFIG_BT_MAX_CONN)
143 #define BT_CTLR_ADV_ISO_STREAM_HANDLE_BASE (CONFIG_BT_MAX_CONN)
144 #else /* !CONFIG_BT_MAX_CONN */
145 #define BT_CTLR_ADV_ISO_STREAM_HANDLE_BASE 0
146 #endif /* !CONFIG_BT_MAX_CONN */
147 #define LL_BIS_ADV_HANDLE_FROM_IDX(stream_handle) \
148 	((stream_handle) + (BT_CTLR_ADV_ISO_STREAM_HANDLE_BASE))
149 #else /* !CONFIG_BT_CTLR_ADV_ISO */
150 #define BT_CTLR_ADV_ISO_STREAM_MAX 0
151 #endif /* CONFIG_BT_CTLR_ADV_ISO */
152 
153 /* Define the ISO Synchronized Receiver Stream Handle base value */
154 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
155 #define BT_CTLR_SYNC_ISO_STREAM_MAX CONFIG_BT_CTLR_SYNC_ISO_STREAM_MAX
156 #if defined(CONFIG_BT_CTLR_ADV_ISO)
157 #define BT_CTLR_SYNC_ISO_STREAM_HANDLE_BASE \
158 	(BT_CTLR_ADV_ISO_STREAM_HANDLE_BASE + \
159 	 CONFIG_BT_CTLR_ADV_ISO_STREAM_COUNT)
160 #elif defined(CONFIG_BT_MAX_CONN)
161 #define BT_CTLR_SYNC_ISO_STREAM_HANDLE_BASE (CONFIG_BT_MAX_CONN)
162 #else /* !CONFIG_BT_MAX_CONN */
163 #define BT_CTLR_SYNC_ISO_STREAM_HANDLE_BASE 0
164 #endif /* !CONFIG_BT_MAX_CONN */
165 #define LL_BIS_SYNC_HANDLE_FROM_IDX(stream_handle) \
166 	((stream_handle) + (BT_CTLR_SYNC_ISO_STREAM_HANDLE_BASE))
167 #else /* !CONFIG_BT_CTLR_SYNC_ISO */
168 #define BT_CTLR_SYNC_ISO_STREAM_MAX 0
169 #endif /* !CONFIG_BT_CTLR_SYNC_ISO */
170 
171 /* Define the ISO Connections Stream Handle base value */
172 #if defined(CONFIG_BT_CTLR_CONN_ISO)
173 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
174 #define BT_CTLR_CONN_ISO_STREAM_HANDLE_BASE \
175 	(BT_CTLR_SYNC_ISO_STREAM_HANDLE_BASE + \
176 	 CONFIG_BT_CTLR_SYNC_ISO_STREAM_COUNT)
177 #elif defined(CONFIG_BT_CTLR_ADV_ISO)
178 #define BT_CTLR_CONN_ISO_STREAM_HANDLE_BASE \
179 	(BT_CTLR_ADV_ISO_STREAM_HANDLE_BASE + \
180 	 CONFIG_BT_CTLR_ADV_ISO_STREAM_COUNT)
181 #else /* !CONFIG_BT_CTLR_ADV_ISO && !CONFIG_BT_CTLR_SYNC_ISO */
182 #define BT_CTLR_CONN_ISO_STREAM_HANDLE_BASE (CONFIG_BT_MAX_CONN)
183 #endif /* !CONFIG_BT_CTLR_ADV_ISO && !CONFIG_BT_CTLR_SYNC_ISO */
184 #define LL_CIS_HANDLE_BASE (BT_CTLR_CONN_ISO_STREAM_HANDLE_BASE)
185 #define LL_CIS_IDX_FROM_HANDLE(handle) \
186 	((handle) - LL_CIS_HANDLE_BASE)
187 #endif /* CONFIG_BT_CTLR_CONN_ISO */
188 
189 #define TICKER_ID_ULL_BASE ((TICKER_ID_LLL_PREEMPT) + 1)
190 
191 enum done_result {
192 	DONE_COMPLETED,
193 	DONE_ABORTED,
194 	DONE_LATE
195 };
196 
197 /* Forward declaration data type to store CTE IQ samples report related data */
198 struct cte_conn_iq_report;
199 
200 struct ull_hdr {
201 	uint8_t volatile ref;  /* Number of ongoing (between Prepare and Done)
202 				* events
203 				*/
204 
205 	/* Event parameters */
206 	/* TODO: The intention is to use the greater of the
207 	 *       ticks_prepare_to_start or ticks_active_to_start as the prepare
208 	 *       offset. At the prepare tick generate a software interrupt
209 	 *       serviceable by application as the per role configurable advance
210 	 *       radio event notification, usable for data acquisitions.
211 	 *       ticks_preempt_to_start is the per role dynamic preempt offset,
212 	 *       which shall be based on role's preparation CPU usage
213 	 *       requirements.
214 	 */
215 	struct {
216 		uint32_t ticks_active_to_start;
217 		uint32_t ticks_prepare_to_start;
218 		uint32_t ticks_preempt_to_start;
219 		uint32_t ticks_slot;
220 	};
221 
222 	/* ULL context disabled callback and its parameter */
223 	void (*disabled_cb)(void *param);
224 	void *disabled_param;
225 };
226 
227 struct lll_hdr {
228 	void *parent;
229 #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING)
230 	uint8_t score;
231 	uint8_t latency;
232 	int8_t  prio;
233 #endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
234 };
235 
236 #define HDR_LLL2ULL(p) (((struct lll_hdr *)(p))->parent)
237 
238 struct lll_prepare_param {
239 	uint32_t ticks_at_expire;
240 	uint32_t remainder;
241 	uint16_t lazy;
242 #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING)
243 	int8_t  prio;
244 #endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
245 	uint8_t force;
246 	void *param;
247 };
248 
249 typedef int (*lll_prepare_cb_t)(struct lll_prepare_param *prepare_param);
250 typedef int (*lll_is_abort_cb_t)(void *next, void *curr,
251 				 lll_prepare_cb_t *resume_cb);
252 typedef void (*lll_abort_cb_t)(struct lll_prepare_param *prepare_param,
253 			       void *param);
254 
255 struct lll_event {
256 	struct lll_prepare_param prepare_param;
257 	lll_prepare_cb_t         prepare_cb;
258 	lll_is_abort_cb_t        is_abort_cb;
259 	lll_abort_cb_t           abort_cb;
260 	uint8_t                  is_resume:1;
261 	uint8_t                  is_aborted:1;
262 };
263 
264 #define DEFINE_NODE_RX_USER_TYPE(i, _) NODE_RX_TYPE_##i
265 
266 enum node_rx_type {
267 	/* Unused */
268 	NODE_RX_TYPE_NONE = 0x00,
269 	/* Signals release of node */
270 	NODE_RX_TYPE_RELEASE,
271 	/* Signals completion of RX event */
272 	NODE_RX_TYPE_EVENT_DONE,
273 	/* Signals arrival of RX Data Channel payload */
274 	NODE_RX_TYPE_DC_PDU,
275 	/* Signals arrival of isochronous payload */
276 	NODE_RX_TYPE_ISO_PDU,
277 	/* Advertisement report from scanning */
278 	NODE_RX_TYPE_REPORT,
279 	NODE_RX_TYPE_EXT_1M_REPORT,
280 	NODE_RX_TYPE_EXT_2M_REPORT,
281 	NODE_RX_TYPE_EXT_CODED_REPORT,
282 	NODE_RX_TYPE_EXT_AUX_REPORT,
283 	NODE_RX_TYPE_EXT_AUX_RELEASE,
284 	NODE_RX_TYPE_EXT_SCAN_TERMINATE,
285 	NODE_RX_TYPE_SYNC,
286 	NODE_RX_TYPE_SYNC_REPORT,
287 	NODE_RX_TYPE_SYNC_LOST,
288 	NODE_RX_TYPE_SYNC_CHM_COMPLETE,
289 	NODE_RX_TYPE_SYNC_ISO,
290 	NODE_RX_TYPE_SYNC_ISO_LOST,
291 	NODE_RX_TYPE_EXT_ADV_TERMINATE,
292 	NODE_RX_TYPE_BIG_COMPLETE,
293 	NODE_RX_TYPE_BIG_CHM_COMPLETE,
294 	NODE_RX_TYPE_BIG_TERMINATE,
295 	NODE_RX_TYPE_SCAN_REQ,
296 	NODE_RX_TYPE_CONNECTION,
297 	NODE_RX_TYPE_TERMINATE,
298 	NODE_RX_TYPE_CONN_UPDATE,
299 	NODE_RX_TYPE_ENC_REFRESH,
300 	NODE_RX_TYPE_APTO,
301 	NODE_RX_TYPE_CHAN_SEL_ALGO,
302 	NODE_RX_TYPE_PHY_UPDATE,
303 	NODE_RX_TYPE_RSSI,
304 	NODE_RX_TYPE_PROFILE,
305 	NODE_RX_TYPE_ADV_INDICATION,
306 	NODE_RX_TYPE_SCAN_INDICATION,
307 	NODE_RX_TYPE_CIS_REQUEST,
308 	NODE_RX_TYPE_CIS_ESTABLISHED,
309 	NODE_RX_TYPE_REQ_PEER_SCA_COMPLETE,
310 	NODE_RX_TYPE_MESH_ADV_CPLT,
311 	NODE_RX_TYPE_MESH_REPORT,
312 	NODE_RX_TYPE_SYNC_IQ_SAMPLE_REPORT,
313 	NODE_RX_TYPE_CONN_IQ_SAMPLE_REPORT,
314 	NODE_RX_TYPE_DTM_IQ_SAMPLE_REPORT,
315 	NODE_RX_TYPE_IQ_SAMPLE_REPORT_ULL_RELEASE,
316 	NODE_RX_TYPE_IQ_SAMPLE_REPORT_LLL_RELEASE,
317 	/* Signals retention (ie non-release) of rx node */
318 	NODE_RX_TYPE_RETAIN,
319 
320 #if defined(CONFIG_BT_CTLR_USER_EXT)
321 	/* No entries shall be added after the NODE_RX_TYPE_USER_START/END */
322 	NODE_RX_TYPE_USER_START,
323 	LISTIFY(CONFIG_BT_CTLR_USER_EVT_RANGE, DEFINE_NODE_RX_USER_TYPE, (,), _),
324 	NODE_RX_TYPE_USER_END,
325 #endif /* CONFIG_BT_CTLR_USER_EXT */
326 };
327 
328 /* Footer of node_rx_hdr */
329 struct node_rx_ftr {
330 	union {
331 		void *param;
332 		struct {
333 			uint8_t  status;
334 			uint8_t  num_events;
335 			uint16_t conn_handle;
336 		} param_adv_term;
337 	};
338 	union {
339 		void *extra;   /* Used as next pointer for extended PDU
340 				* chaining, to reserve node_rx for CSA#2 event
341 				* generation etc.
342 				*/
343 		void *aux_ptr;
344 		uint8_t aux_phy;
345 		struct cte_conn_iq_report *iq_report;
346 	};
347 	uint32_t ticks_anchor;
348 	uint32_t radio_end_us;
349 	uint8_t  rssi;
350 
351 #if defined(CONFIG_BT_CTLR_PRIVACY)
352 	uint8_t  rl_idx;
353 	uint8_t  lrpa_used:1;
354 #endif /* CONFIG_BT_CTLR_PRIVACY */
355 
356 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
357 	uint8_t  direct:1;
358 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
359 
360 #if defined(CONFIG_BT_CTLR_ADV_EXT) && defined(CONFIG_BT_OBSERVER)
361 	uint8_t  phy_flags:1;
362 	uint8_t  scan_req:1;
363 	uint8_t  scan_rsp:1;
364 
365 #if defined(CONFIG_BT_CTLR_PRIVACY)
366 	uint8_t  direct_resolved:1;
367 #endif /* CONFIG_BT_CTLR_PRIVACY */
368 
369 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
370 	uint8_t  sync_status:2;
371 	uint8_t  sync_rx_enabled:1;
372 #if defined(CONFIG_BT_CTLR_FILTER_ACCEPT_LIST)
373 	uint8_t  devmatch:1;
374 #endif /* CONFIG_BT_CTLR_FILTER_ACCEPT_LIST */
375 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
376 
377 	uint8_t  aux_sched:1;
378 	uint8_t  aux_lll_sched:1;
379 	uint8_t  aux_failed:1;
380 
381 	uint16_t aux_data_len;
382 #endif /* CONFIG_BT_CTLR_ADV_EXT && CONFIG_BT_OBSERVER */
383 
384 #if defined(CONFIG_BT_HCI_MESH_EXT)
385 	uint8_t  chan_idx;
386 #endif /* CONFIG_BT_HCI_MESH_EXT */
387 };
388 
389 /* Meta-information for isochronous PDUs in node_rx_hdr */
390 struct node_rx_iso_meta {
391 	uint64_t payload_number:39; /* cisPayloadNumber */
392 	uint64_t status:8;          /* Status of reception (OK/not OK) */
393 	uint32_t timestamp;         /* Time of reception */
394 };
395 
396 /* Define invalid/unassigned Controller state/role instance handle */
397 #define NODE_RX_HANDLE_INVALID 0xFFFF
398 
399 /* Define invalid/unassigned Controller LLL context handle */
400 #define LLL_HANDLE_INVALID     0xFFFF
401 
402 /* Define invalid/unassigned Controller Advertising LLL context handle */
403 #define LLL_ADV_HANDLE_INVALID 0xFF
404 
405 /* Header of node_rx_pdu */
406 struct node_rx_hdr {
407 	union {
408 		void        *next;    /* For slist, by hci module */
409 		memq_link_t *link;    /* Supply memq_link from ULL to LLL */
410 		uint8_t     ack_last; /* Tx ack queue index at this node rx */
411 	};
412 
413 	enum node_rx_type type;
414 	uint8_t           user_meta; /* User metadata */
415 	uint16_t          handle;    /* State/Role instance handle */
416 
417 	union {
418 		struct node_rx_ftr rx_ftr;
419 #if defined(CONFIG_BT_CTLR_SYNC_ISO) || defined(CONFIG_BT_CTLR_CONN_ISO)
420 		struct node_rx_iso_meta rx_iso_meta;
421 #endif
422 #if defined(CONFIG_BT_CTLR_RX_PDU_META)
423 		lll_rx_pdu_meta_t  rx_pdu_meta;
424 #endif /* CONFIG_BT_CTLR_RX_PDU_META */
425 	};
426 };
427 
428 /* Template node rx type with memory aligned offset to PDU buffer.
429  * NOTE: offset to memory aligned pdu buffer location is used to reference
430  *       node rx type specific information, like, terminate or sync lost reason
431  *       from a dedicated node rx structure storage location.
432  */
433 struct node_rx_pdu {
434 	struct node_rx_hdr hdr;
435 	union {
436 		uint8_t    pdu[0] __aligned(4);
437 	};
438 };
439 
440 enum {
441 	EVENT_DONE_EXTRA_TYPE_NONE,
442 
443 #if defined(CONFIG_BT_CONN)
444 	EVENT_DONE_EXTRA_TYPE_CONN,
445 #endif /* CONFIG_BT_CONN */
446 
447 #if defined(CONFIG_BT_CTLR_ADV_EXT) || defined(CONFIG_BT_CTLR_JIT_SCHEDULING)
448 #if defined(CONFIG_BT_BROADCASTER)
449 	EVENT_DONE_EXTRA_TYPE_ADV,
450 	EVENT_DONE_EXTRA_TYPE_ADV_AUX,
451 #if defined(CONFIG_BT_CTLR_ADV_ISO)
452 	EVENT_DONE_EXTRA_TYPE_ADV_ISO_COMPLETE,
453 	EVENT_DONE_EXTRA_TYPE_ADV_ISO_TERMINATE,
454 #endif /* CONFIG_BT_CTLR_ADV_ISO */
455 #endif /* CONFIG_BT_BROADCASTER */
456 #endif /* CONFIG_BT_CTLR_ADV_EXT || CONFIG_BT_CTLR_JIT_SCHEDULING */
457 
458 #if defined(CONFIG_BT_OBSERVER)
459 #if defined(CONFIG_BT_CTLR_ADV_EXT)
460 	EVENT_DONE_EXTRA_TYPE_SCAN,
461 	EVENT_DONE_EXTRA_TYPE_SCAN_AUX,
462 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
463 	EVENT_DONE_EXTRA_TYPE_SYNC,
464 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
465 	EVENT_DONE_EXTRA_TYPE_SYNC_ISO_ESTAB,
466 	EVENT_DONE_EXTRA_TYPE_SYNC_ISO,
467 	EVENT_DONE_EXTRA_TYPE_SYNC_ISO_TERMINATE,
468 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
469 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
470 #endif /* CONFIG_BT_CTLR_ADV_EXT */
471 #endif /* CONFIG_BT_OBSERVER */
472 
473 #if defined(CONFIG_BT_CTLR_CONN_ISO)
474 	EVENT_DONE_EXTRA_TYPE_CIS,
475 #endif /* CONFIG_BT_CTLR_CONN_ISO */
476 
477 /* Following proprietary defines must be at end of enum range */
478 #if defined(CONFIG_BT_CTLR_USER_EXT)
479 	EVENT_DONE_EXTRA_TYPE_USER_START,
480 	EVENT_DONE_EXTRA_TYPE_USER_END = EVENT_DONE_EXTRA_TYPE_USER_START +
481 		CONFIG_BT_CTLR_USER_EVT_RANGE,
482 #endif /* CONFIG_BT_CTLR_USER_EXT */
483 
484 };
485 
486 struct event_done_extra_drift {
487 	uint32_t start_to_address_actual_us;
488 	uint32_t window_widening_event_us;
489 	uint32_t preamble_to_addr_us;
490 };
491 
492 struct event_done_extra {
493 	uint8_t type;
494 #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING)
495 	uint8_t result;
496 #endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
497 	union {
498 		struct {
499 			union {
500 #if defined(CONFIG_BT_CTLR_CONN_ISO)
501 				uint32_t trx_performed_bitmask;
502 #endif /* CONFIG_BT_CTLR_CONN_ISO */
503 
504 				struct {
505 					uint16_t trx_cnt;
506 					uint8_t  crc_valid:1;
507 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING) && \
508 	defined(CONFIG_BT_CTLR_CTEINLINE_SUPPORT)
509 					/* Used to inform ULL that periodic
510 					 * advertising sync scan should be
511 					 * terminated.
512 					 */
513 					uint8_t  sync_term:1;
514 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING && \
515 	* CONFIG_BT_CTLR_CTEINLINE_SUPPORT
516 	*/
517 				};
518 			};
519 
520 #if defined(CONFIG_BT_CTLR_LE_ENC)
521 			uint8_t  mic_state;
522 #endif /* CONFIG_BT_CTLR_LE_ENC */
523 
524 #if defined(CONFIG_BT_PERIPHERAL) || defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
525 			union {
526 				struct event_done_extra_drift drift;
527 			};
528 #endif /* CONFIG_BT_PERIPHERAL || CONFIG_BT_CTLR_SYNC_PERIODIC */
529 		};
530 	};
531 };
532 
533 struct node_rx_event_done {
534 	struct node_rx_hdr      hdr;
535 	void                    *param;
536 	struct event_done_extra extra;
537 };
538 
lll_hdr_init(void * lll,void * parent)539 static inline void lll_hdr_init(void *lll, void *parent)
540 {
541 	struct lll_hdr *hdr = lll;
542 
543 	hdr->parent = parent;
544 
545 #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING)
546 	hdr->score = 0U;
547 	hdr->latency = 0U;
548 #endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
549 }
550 
551 /* If ISO vendor data path is not used, queue directly to ll_iso_rx */
552 #if defined(CONFIG_BT_CTLR_ISO_VENDOR_DATA_PATH)
553 #define iso_rx_put(link, rx) ull_iso_rx_put(link, rx)
554 #define iso_rx_sched() ull_iso_rx_sched()
555 #else
556 #define iso_rx_put(link, rx) ll_iso_rx_put(link, rx)
557 #define iso_rx_sched() ll_rx_sched()
558 #endif /* CONFIG_BT_CTLR_ISO_VENDOR_DATA_PATH */
559 
560 struct node_tx_iso;
561 
562 void lll_done_score(void *param, uint8_t result);
563 
564 int lll_init(void);
565 int lll_deinit(void);
566 int lll_reset(void);
567 void lll_resume(void *param);
568 void lll_disable(void *param);
569 void lll_done_ull_inc(void);
570 uint32_t lll_radio_is_idle(void);
571 uint32_t lll_radio_tx_ready_delay_get(uint8_t phy, uint8_t flags);
572 uint32_t lll_radio_rx_ready_delay_get(uint8_t phy, uint8_t flags);
573 int8_t lll_radio_tx_pwr_min_get(void);
574 int8_t lll_radio_tx_pwr_max_get(void);
575 int8_t lll_radio_tx_pwr_floor(int8_t tx_pwr_lvl);
576 
577 int lll_csrand_get(void *buf, size_t len);
578 int lll_csrand_isr_get(void *buf, size_t len);
579 int lll_rand_get(void *buf, size_t len);
580 int lll_rand_isr_get(void *buf, size_t len);
581 
582 struct lll_event *ull_prepare_enqueue(lll_is_abort_cb_t is_abort_cb,
583 				      lll_abort_cb_t abort_cb,
584 				      struct lll_prepare_param *prepare_param,
585 				      lll_prepare_cb_t prepare_cb,
586 				      uint8_t is_resume);
587 void *ull_prepare_dequeue_get(void);
588 void *ull_prepare_dequeue_iter(uint8_t *idx);
589 void ull_prepare_dequeue(uint8_t caller_id);
590 void *ull_pdu_rx_alloc_peek(uint8_t count);
591 void *ull_pdu_rx_alloc_peek_iter(uint8_t *idx);
592 void *ull_pdu_rx_alloc(void);
593 void *ull_iso_pdu_rx_alloc_peek(uint8_t count);
594 void *ull_iso_pdu_rx_alloc(void);
595 void ull_rx_put(memq_link_t *link, void *rx);
596 void ull_rx_sched(void);
597 void ull_rx_put_sched(memq_link_t *link, void *rx);
598 void ull_iso_rx_put(memq_link_t *link, void *rx);
599 void ull_iso_rx_sched(void);
600 void *ull_iso_tx_ack_dequeue(void);
601 void ull_iso_lll_ack_enqueue(uint16_t handle, struct node_tx_iso *tx);
602 void ull_iso_lll_event_prepare(uint16_t handle, uint64_t event_count);
603 struct event_done_extra *ull_event_done_extra_get(void);
604 struct event_done_extra *ull_done_extra_type_set(uint8_t type);
605 void *ull_event_done(void *param);
606 
607 int lll_prepare(lll_is_abort_cb_t is_abort_cb,
608 		lll_abort_cb_t abort_cb,
609 		lll_prepare_cb_t prepare_cb, int8_t event_prio,
610 		struct lll_prepare_param *prepare_param);
611 int lll_resume_enqueue(lll_prepare_cb_t resume_cb, int resume_prio);
612 int lll_prepare_resolve(lll_is_abort_cb_t is_abort_cb, lll_abort_cb_t abort_cb,
613 			lll_prepare_cb_t prepare_cb,
614 			struct lll_prepare_param *prepare_param,
615 			uint8_t is_resume, uint8_t is_dequeue);
616