1 /*
2 * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3 * Copyright 2019 NXP
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <limits.h>
12
13 #include <zephyr/bluetooth/hci_types.h>
14 #include <zephyr/sys/byteorder.h>
15
16 #include "hal/ccm.h"
17 #include "hal/radio.h"
18 #include "hal/ticker.h"
19
20 #include "util/util.h"
21 #include "util/memq.h"
22 #include "util/mayfly.h"
23
24 #include "ticker/ticker.h"
25
26 #include "pdu_vendor.h"
27 #include "pdu.h"
28
29 #include "lll.h"
30 #include "lll_vendor.h"
31 #include "lll_clock.h"
32 #include "lll_scan.h"
33 #include "lll_df_types.h"
34 #include "lll_conn.h"
35 #include "lll_chan.h"
36 #include "lll_filter.h"
37
38 #include "lll_internal.h"
39 #include "lll_tim_internal.h"
40 #include "lll_prof_internal.h"
41
42 #include <soc.h>
43 #include "hal/debug.h"
44
45 static int init_reset(void);
46 static int prepare_cb(struct lll_prepare_param *prepare_param);
47 static int is_abort_cb(void *next, void *curr, lll_prepare_cb_t *resume_cb);
48 static void abort_cb(struct lll_prepare_param *prepare_param, void *param);
49 static void ticker_stop_cb(uint32_t ticks_at_expire, uint32_t ticks_drift,
50 uint32_t remainder, uint16_t lazy, uint8_t force,
51 void *param);
52 static void ticker_op_start_cb(uint32_t status, void *param);
53 static void isr_rx(void *param);
54 static void isr_tx(void *param);
55 static void isr_done(void *param);
56 static void isr_window(void *param);
57 static void isr_abort(void *param);
58 static void isr_cleanup(void *param);
59 static void isr_race(void *param);
60
61 static inline bool isr_rx_scan_check(struct lll_scan *lll, uint8_t irkmatch_ok,
62 uint8_t devmatch_ok, uint8_t rl_idx);
63 static inline uint32_t isr_rx_pdu(struct lll_scan *lll, uint8_t devmatch_ok,
64 uint8_t devmatch_id, uint8_t irkmatch_ok,
65 uint8_t irkmatch_id, uint8_t rl_idx, uint8_t rssi_ready);
66 #if defined(CONFIG_BT_CENTRAL)
67 static inline bool isr_scan_init_check(struct lll_scan *lll,
68 struct pdu_adv *pdu, uint8_t rl_idx);
69 static inline bool isr_scan_init_adva_check(struct lll_scan *lll,
70 struct pdu_adv *pdu, uint8_t rl_idx);
71 #endif /* CONFIG_BT_CENTRAL */
72 static inline bool isr_scan_tgta_check(struct lll_scan *lll, bool init,
73 struct pdu_adv *pdu, uint8_t rl_idx,
74 bool *dir_report);
75 static inline bool isr_scan_tgta_rpa_check(struct lll_scan *lll,
76 struct pdu_adv *pdu,
77 bool *dir_report);
78 static inline bool isr_scan_rsp_adva_matches(struct pdu_adv *srsp);
79 static uint32_t isr_rx_scan_report(struct lll_scan *lll, uint8_t rssi_ready,
80 uint8_t rl_idx, bool dir_report);
81
82
lll_scan_init(void)83 int lll_scan_init(void)
84 {
85 int err;
86
87 err = init_reset();
88 if (err) {
89 return err;
90 }
91
92 return 0;
93 }
94
lll_scan_reset(void)95 int lll_scan_reset(void)
96 {
97 int err;
98
99 err = init_reset();
100 if (err) {
101 return err;
102 }
103
104 return 0;
105 }
106
lll_scan_prepare(void * param)107 void lll_scan_prepare(void *param)
108 {
109 struct lll_prepare_param *p = param;
110 int err;
111
112 err = lll_clk_on();
113 LL_ASSERT(!err || err == -EINPROGRESS);
114
115 err = lll_prepare(is_abort_cb, abort_cb, prepare_cb, 0, p);
116 LL_ASSERT(!err || err == -EINPROGRESS);
117 }
118
init_reset(void)119 static int init_reset(void)
120 {
121 return 0;
122 }
123
prepare_cb(struct lll_prepare_param * prepare_param)124 static int prepare_cb(struct lll_prepare_param *prepare_param)
125 {
126 struct lll_scan *lll = prepare_param->param;
127 uint32_t aa = sys_cpu_to_le32(PDU_AC_ACCESS_ADDR);
128 uint32_t ticks_at_event, ticks_at_start;
129 struct node_rx_pdu *node_rx;
130 uint32_t remainder_us;
131 struct ull_hdr *ull;
132 uint32_t remainder;
133
134 DEBUG_RADIO_START_O(1);
135
136 #if defined(CONFIG_BT_CENTRAL)
137 /* Check if stopped (on connection establishment race between LLL and
138 * ULL.
139 */
140 if (unlikely(lll->conn && lll->conn->central.initiated)) {
141 int err;
142
143 err = lll_clk_off();
144 LL_ASSERT(!err || err == -EBUSY);
145
146 lll_done(NULL);
147
148 DEBUG_RADIO_START_O(0);
149 return 0;
150 }
151 #endif /* CONFIG_BT_CENTRAL */
152
153 radio_reset();
154 /* TODO: other Tx Power settings */
155 radio_tx_power_set(RADIO_TXP_DEFAULT);
156
157 #if defined(CONFIG_BT_CTLR_ADV_EXT)
158 /* TODO: if coded we use S8? */
159 radio_phy_set(lll->phy, 1);
160 radio_pkt_configure(8, PDU_AC_LEG_PAYLOAD_SIZE_MAX, (lll->phy << 1));
161 #else /* !CONFIG_BT_CTLR_ADV_EXT */
162 radio_phy_set(0, 0);
163 radio_pkt_configure(8, PDU_AC_LEG_PAYLOAD_SIZE_MAX, 0);
164 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
165
166 node_rx = ull_pdu_rx_alloc_peek(1);
167 LL_ASSERT(node_rx);
168 radio_pkt_rx_set(node_rx->pdu);
169
170 radio_aa_set((uint8_t *)&aa);
171 radio_crc_configure(((0x5bUL) | ((0x06UL) << 8) | ((0x00UL) << 16)),
172 0x555555);
173
174 lll_chan_set(37 + lll->chan);
175
176 radio_isr_set(isr_rx, lll);
177
178 /* setup tIFS switching */
179 radio_tmr_tifs_set(EVENT_IFS_US);
180 radio_switch_complete_and_tx(0, 0, 0, 0);
181
182 #if defined(CONFIG_BT_CTLR_PRIVACY)
183 if (ull_filter_lll_rl_enabled()) {
184 struct lll_filter *filter =
185 ull_filter_lll_get((lll->filter_policy &
186 SCAN_FP_FILTER) != 0U);
187 uint8_t count, *irks = ull_filter_lll_irks_get(&count);
188
189 radio_filter_configure(filter->enable_bitmask,
190 filter->addr_type_bitmask,
191 (uint8_t *)filter->bdaddr);
192
193 radio_ar_configure(count, irks);
194 } else
195 #endif /* CONFIG_BT_CTLR_PRIVACY */
196
197 if (IS_ENABLED(CONFIG_BT_CTLR_FILTER_ACCEPT_LIST) && lll->filter_policy) {
198 /* Setup Radio Filter */
199 struct lll_filter *fal = ull_filter_lll_get(true);
200
201 radio_filter_configure(fal->enable_bitmask,
202 fal->addr_type_bitmask,
203 (uint8_t *)fal->bdaddr);
204 }
205
206 ticks_at_event = prepare_param->ticks_at_expire;
207 ull = HDR_LLL2ULL(lll);
208 ticks_at_event += lll_event_offset_get(ull);
209
210 ticks_at_start = ticks_at_event;
211 ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
212
213 remainder = prepare_param->remainder;
214 remainder_us = radio_tmr_start(0, ticks_at_start, remainder);
215
216 /* capture end of Rx-ed PDU, for initiator to calculate first
217 * central event.
218 */
219 radio_tmr_end_capture();
220
221 /* scanner always measures RSSI */
222 radio_rssi_measure();
223
224 #if defined(HAL_RADIO_GPIO_HAVE_LNA_PIN)
225 radio_gpio_lna_setup();
226 radio_gpio_pa_lna_enable(remainder_us +
227 radio_rx_ready_delay_get(0, 0) -
228 HAL_RADIO_GPIO_LNA_OFFSET);
229 #else /* !HAL_RADIO_GPIO_HAVE_LNA_PIN */
230 ARG_UNUSED(remainder_us);
231 #endif /* !HAL_RADIO_GPIO_HAVE_LNA_PIN */
232
233 #if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
234 (EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
235 /* check if preempt to start has changed */
236 if (lll_preempt_calc(ull, (TICKER_ID_SCAN_BASE +
237 ull_scan_lll_handle_get(lll)),
238 ticks_at_event)) {
239 radio_isr_set(isr_abort, lll);
240 radio_disable();
241 } else
242 #endif /* CONFIG_BT_CTLR_XTAL_ADVANCED */
243 {
244 uint32_t ret;
245
246 if (lll->ticks_window) {
247 /* start window close timeout */
248 ret = ticker_start(TICKER_INSTANCE_ID_CTLR,
249 TICKER_USER_ID_LLL,
250 TICKER_ID_SCAN_STOP,
251 ticks_at_event, lll->ticks_window,
252 TICKER_NULL_PERIOD,
253 TICKER_NULL_REMAINDER,
254 TICKER_NULL_LAZY, TICKER_NULL_SLOT,
255 ticker_stop_cb, lll,
256 ticker_op_start_cb,
257 (void *)__LINE__);
258 LL_ASSERT((ret == TICKER_STATUS_SUCCESS) ||
259 (ret == TICKER_STATUS_BUSY));
260 }
261
262 ret = lll_prepare_done(lll);
263 LL_ASSERT(!ret);
264 }
265
266 DEBUG_RADIO_START_O(1);
267
268 return 0;
269 }
270
resume_prepare_cb(struct lll_prepare_param * p)271 static int resume_prepare_cb(struct lll_prepare_param *p)
272 {
273 struct ull_hdr *ull = HDR_LLL2ULL(p->param);
274
275 p->ticks_at_expire = ticker_ticks_now_get() - lll_event_offset_get(ull);
276 p->remainder = 0;
277 p->lazy = 0;
278
279 return prepare_cb(p);
280 }
281
is_abort_cb(void * next,void * curr,lll_prepare_cb_t * resume_cb)282 static int is_abort_cb(void *next, void *curr, lll_prepare_cb_t *resume_cb)
283 {
284 struct lll_scan *lll = curr;
285
286 /* TODO: check prio */
287 if (next != curr) {
288 int err;
289
290 /* wrap back after the pre-empter */
291 *resume_cb = resume_prepare_cb;
292
293 /* Retain HF clk */
294 err = lll_clk_on();
295 LL_ASSERT(!err || err == -EINPROGRESS);
296
297 return -EAGAIN;
298 }
299
300 radio_isr_set(isr_window, lll);
301 radio_disable();
302
303 if (++lll->chan == 3U) {
304 lll->chan = 0U;
305 }
306
307 lll_chan_set(37 + lll->chan);
308
309 return 0;
310 }
311
abort_cb(struct lll_prepare_param * prepare_param,void * param)312 static void abort_cb(struct lll_prepare_param *prepare_param, void *param)
313 {
314 int err;
315
316 /* NOTE: This is not a prepare being cancelled */
317 if (!prepare_param) {
318 /* Perform event abort here.
319 * After event has been cleanly aborted, clean up resources
320 * and dispatch event done.
321 */
322 radio_isr_set(isr_abort, param);
323 radio_disable();
324 return;
325 }
326
327 /* NOTE: Else clean the top half preparations of the aborted event
328 * currently in preparation pipeline.
329 */
330 err = lll_clk_off();
331 LL_ASSERT(!err || err == -EBUSY);
332
333 lll_done(param);
334 }
335
ticker_stop_cb(uint32_t ticks_at_expire,uint32_t ticks_drift,uint32_t remainder,uint16_t lazy,uint8_t force,void * param)336 static void ticker_stop_cb(uint32_t ticks_at_expire, uint32_t ticks_drift,
337 uint32_t remainder, uint16_t lazy, uint8_t force,
338 void *param)
339 {
340 radio_isr_set(isr_cleanup, param);
341 radio_disable();
342 }
343
ticker_op_start_cb(uint32_t status,void * param)344 static void ticker_op_start_cb(uint32_t status, void *param)
345 {
346 ARG_UNUSED(param);
347
348 LL_ASSERT(status == TICKER_STATUS_SUCCESS);
349 }
350
isr_rx(void * param)351 static void isr_rx(void *param)
352 {
353 struct lll_scan *lll = (void *)param;
354 uint8_t trx_done;
355 uint8_t crc_ok;
356 uint8_t devmatch_ok;
357 uint8_t devmatch_id;
358 uint8_t irkmatch_ok;
359 uint8_t irkmatch_id;
360 uint8_t rssi_ready;
361 uint8_t rl_idx;
362
363 if (IS_ENABLED(CONFIG_BT_CTLR_PROFILE_ISR)) {
364 lll_prof_latency_capture();
365 }
366
367 /* Read radio status and events */
368 trx_done = radio_is_done();
369 if (trx_done) {
370 crc_ok = radio_crc_is_valid();
371 devmatch_ok = radio_filter_has_match();
372 devmatch_id = radio_filter_match_get();
373 if (IS_ENABLED(CONFIG_BT_CTLR_PRIVACY)) {
374 irkmatch_ok = radio_ar_has_match();
375 irkmatch_id = radio_ar_match_get();
376 } else {
377 irkmatch_ok = 0U;
378 irkmatch_id = FILTER_IDX_NONE;
379 }
380 rssi_ready = radio_rssi_is_ready();
381 } else {
382 crc_ok = devmatch_ok = irkmatch_ok = rssi_ready = 0U;
383 devmatch_id = irkmatch_id = FILTER_IDX_NONE;
384 }
385
386 /* Clear radio status and events */
387 lll_isr_status_reset();
388
389 if (IS_ENABLED(HAL_RADIO_GPIO_HAVE_PA_PIN) ||
390 IS_ENABLED(HAL_RADIO_GPIO_HAVE_LNA_PIN)) {
391 radio_gpio_pa_lna_disable();
392 }
393
394 if (!trx_done) {
395 goto isr_rx_do_close;
396 }
397
398 #if defined(CONFIG_BT_CTLR_PRIVACY)
399 rl_idx = devmatch_ok ?
400 ull_filter_lll_rl_idx(((lll->filter_policy &
401 SCAN_FP_FILTER) != 0U),
402 devmatch_id) :
403 irkmatch_ok ? ull_filter_lll_rl_irk_idx(irkmatch_id) :
404 FILTER_IDX_NONE;
405 #else
406 rl_idx = FILTER_IDX_NONE;
407 #endif
408 if (crc_ok && isr_rx_scan_check(lll, irkmatch_ok, devmatch_ok,
409 rl_idx)) {
410 uint32_t err;
411
412 err = isr_rx_pdu(lll, devmatch_ok, devmatch_id, irkmatch_ok,
413 irkmatch_id, rl_idx, rssi_ready);
414 if (!err) {
415 if (IS_ENABLED(CONFIG_BT_CTLR_PROFILE_ISR)) {
416 lll_prof_send();
417 }
418
419 return;
420 }
421 }
422
423 isr_rx_do_close:
424 radio_isr_set(isr_done, lll);
425 radio_disable();
426 }
427
isr_tx(void * param)428 static void isr_tx(void *param)
429 {
430 struct node_rx_pdu *node_rx;
431 uint32_t hcto;
432
433 /* TODO: MOVE to a common interface, isr_lll_radio_status? */
434 /* Clear radio status and events */
435 radio_status_reset();
436 radio_tmr_status_reset();
437
438 if (IS_ENABLED(HAL_RADIO_GPIO_HAVE_PA_PIN) ||
439 IS_ENABLED(HAL_RADIO_GPIO_HAVE_LNA_PIN)) {
440 radio_gpio_pa_lna_disable();
441 }
442 /* TODO: MOVE ^^ */
443
444 /* setup tIFS switching */
445 radio_tmr_tifs_set(EVENT_IFS_US);
446 radio_switch_complete_and_tx(0, 0, 0, 0);
447
448 node_rx = ull_pdu_rx_alloc_peek(1);
449 LL_ASSERT(node_rx);
450 radio_pkt_rx_set(node_rx->pdu);
451
452 /* assert if radio packet ptr is not set and radio started rx */
453 LL_ASSERT(!radio_is_ready());
454
455 #if defined(CONFIG_BT_CTLR_PRIVACY)
456 if (ull_filter_lll_rl_enabled()) {
457 uint8_t count, *irks = ull_filter_lll_irks_get(&count);
458
459 radio_ar_configure(count, irks);
460 }
461 #endif /* CONFIG_BT_CTLR_PRIVACY */
462
463 /* +/- 2us active clock jitter, +1 us hcto compensation */
464 hcto = radio_tmr_tifs_base_get() + EVENT_IFS_US + 4 + 1;
465 hcto += radio_rx_chain_delay_get(0, 0);
466 hcto += addr_us_get(0);
467 hcto -= radio_tx_chain_delay_get(0, 0);
468
469 radio_tmr_hcto_configure(hcto);
470
471 radio_rssi_measure();
472
473 #if defined(HAL_RADIO_GPIO_HAVE_LNA_PIN)
474 radio_gpio_lna_setup();
475 radio_gpio_pa_lna_enable(radio_tmr_tifs_base_get() + EVENT_IFS_US - 4 -
476 radio_tx_chain_delay_get(0, 0) -
477 HAL_RADIO_GPIO_LNA_OFFSET);
478 #endif /* HAL_RADIO_GPIO_HAVE_LNA_PIN */
479
480 radio_isr_set(isr_rx, param);
481 }
482
isr_common_done(void * param)483 static void isr_common_done(void *param)
484 {
485 struct node_rx_pdu *node_rx;
486
487 /* TODO: MOVE to a common interface, isr_lll_radio_status? */
488 /* Clear radio status and events */
489 lll_isr_status_reset();
490
491 if (IS_ENABLED(HAL_RADIO_GPIO_HAVE_PA_PIN) ||
492 IS_ENABLED(HAL_RADIO_GPIO_HAVE_LNA_PIN)) {
493 radio_gpio_pa_lna_disable();
494 }
495 /* TODO: MOVE ^^ */
496
497 /* setup tIFS switching */
498 radio_tmr_tifs_set(EVENT_IFS_US);
499 radio_switch_complete_and_tx(0, 0, 0, 0);
500
501 node_rx = ull_pdu_rx_alloc_peek(1);
502 LL_ASSERT(node_rx);
503 radio_pkt_rx_set(node_rx->pdu);
504
505 #if defined(CONFIG_BT_CTLR_PRIVACY)
506 if (ull_filter_lll_rl_enabled()) {
507 uint8_t count, *irks = ull_filter_lll_irks_get(&count);
508
509 radio_ar_configure(count, irks);
510 }
511 #endif /* CONFIG_BT_CTLR_PRIVACY */
512
513 radio_rssi_measure();
514
515 radio_isr_set(isr_rx, param);
516 }
517
isr_done(void * param)518 static void isr_done(void *param)
519 {
520 uint32_t start_us;
521
522 isr_common_done(param);
523
524 #if defined(HAL_RADIO_GPIO_HAVE_LNA_PIN)
525 start_us = radio_tmr_start_now(0);
526
527 radio_gpio_lna_setup();
528 radio_gpio_pa_lna_enable(start_us +
529 radio_rx_ready_delay_get(0, 0) -
530 HAL_RADIO_GPIO_LNA_OFFSET);
531 #else /* !HAL_RADIO_GPIO_HAVE_LNA_PIN */
532 ARG_UNUSED(start_us);
533
534 radio_rx_enable();
535 #endif /* !HAL_RADIO_GPIO_HAVE_LNA_PIN */
536
537 /* capture end of Rx-ed PDU, for initiator to calculate first
538 * central event.
539 */
540 radio_tmr_end_capture();
541 }
542
isr_window(void * param)543 static void isr_window(void *param)
544 {
545 uint32_t ticks_at_start, remainder_us;
546
547 isr_common_done(param);
548
549 ticks_at_start = ticker_ticks_now_get() +
550 HAL_TICKER_CNTR_CMP_OFFSET_MIN;
551 remainder_us = radio_tmr_start_tick(0, ticks_at_start);
552
553 /* capture end of Rx-ed PDU, for initiator to calculate first
554 * central event.
555 */
556 radio_tmr_end_capture();
557
558 #if defined(HAL_RADIO_GPIO_HAVE_LNA_PIN)
559 radio_gpio_lna_setup();
560 radio_gpio_pa_lna_enable(remainder_us +
561 radio_rx_ready_delay_get(0, 0) -
562 HAL_RADIO_GPIO_LNA_OFFSET);
563 #else /* !HAL_RADIO_GPIO_HAVE_LNA_PIN */
564 ARG_UNUSED(remainder_us);
565 #endif /* !HAL_RADIO_GPIO_HAVE_LNA_PIN */
566 }
567
isr_abort(void * param)568 static void isr_abort(void *param)
569 {
570 /* Clear radio status and events */
571 lll_isr_status_reset();
572
573 if (IS_ENABLED(HAL_RADIO_GPIO_HAVE_PA_PIN) ||
574 IS_ENABLED(HAL_RADIO_GPIO_HAVE_LNA_PIN)) {
575 radio_gpio_pa_lna_disable();
576 }
577
578 /* Scanner stop can expire while here in this ISR.
579 * Deferred attempt to stop can fail as it would have
580 * expired, hence ignore failure.
581 */
582 (void)ticker_stop(TICKER_INSTANCE_ID_CTLR, TICKER_USER_ID_LLL,
583 TICKER_ID_SCAN_STOP, NULL, NULL);
584
585 /* Under race conditions, radio could get started while entering ISR */
586 radio_disable();
587
588 isr_cleanup(param);
589 }
590
isr_cleanup(void * param)591 static void isr_cleanup(void *param)
592 {
593 struct lll_scan *lll = param;
594 struct node_rx_pdu *node_rx;
595 int err;
596
597 if (lll_is_done(param)) {
598 return;
599 }
600
601 radio_filter_disable();
602
603 if (++lll->chan == 3U) {
604 lll->chan = 0U;
605 }
606
607 #if defined(CONFIG_BT_HCI_MESH_EXT)
608 if (_radio.advertiser.is_enabled && _radio.advertiser.is_mesh &&
609 !_radio.advertiser.retry) {
610 mayfly_mesh_stop(NULL);
611 }
612 #endif /* CONFIG_BT_HCI_MESH_EXT */
613
614 #if defined(CONFIG_BT_CTLR_SCAN_INDICATION)
615 node_rx = ull_pdu_rx_alloc_peek(3);
616 if (node_rx) {
617 ull_pdu_rx_alloc();
618
619 /* TODO: add other info by defining a payload struct */
620 node_rx->hdr.type = NODE_RX_TYPE_SCAN_INDICATION;
621
622 ull_rx_put_sched(node_rx->hdr.link, node_rx);
623 }
624 #else /* !CONFIG_BT_CTLR_SCAN_INDICATION */
625 ARG_UNUSED(node_rx);
626 #endif /* !CONFIG_BT_CTLR_SCAN_INDICATION */
627
628 radio_isr_set(isr_race, param);
629 radio_tmr_stop();
630
631 err = lll_clk_off();
632 LL_ASSERT(!err || err == -EBUSY);
633
634 lll_done(NULL);
635 }
636
isr_race(void * param)637 static void isr_race(void *param)
638 {
639 /* NOTE: lll_disable could have a race with ... */
640 radio_status_reset();
641 }
642
isr_rx_scan_check(struct lll_scan * lll,uint8_t irkmatch_ok,uint8_t devmatch_ok,uint8_t rl_idx)643 static inline bool isr_rx_scan_check(struct lll_scan *lll, uint8_t irkmatch_ok,
644 uint8_t devmatch_ok, uint8_t rl_idx)
645 {
646 #if defined(CONFIG_BT_CTLR_PRIVACY)
647 return (((lll->filter_policy & SCAN_FP_FILTER) == 0U) &&
648 (!devmatch_ok || ull_filter_lll_rl_idx_allowed(irkmatch_ok,
649 rl_idx))) ||
650 (((lll->filter_policy & SCAN_FP_FILTER) != 0U) &&
651 (devmatch_ok || ull_filter_lll_irk_in_fal(rl_idx)));
652 #else
653 return ((lll->filter_policy & SCAN_FP_FILTER) == 0U) ||
654 devmatch_ok;
655 #endif /* CONFIG_BT_CTLR_PRIVACY */
656 }
657
isr_rx_pdu(struct lll_scan * lll,uint8_t devmatch_ok,uint8_t devmatch_id,uint8_t irkmatch_ok,uint8_t irkmatch_id,uint8_t rl_idx,uint8_t rssi_ready)658 static inline uint32_t isr_rx_pdu(struct lll_scan *lll, uint8_t devmatch_ok,
659 uint8_t devmatch_id, uint8_t irkmatch_ok,
660 uint8_t irkmatch_id, uint8_t rl_idx, uint8_t rssi_ready)
661 {
662 struct node_rx_pdu *node_rx;
663 struct pdu_adv *pdu_adv_rx;
664 bool dir_report = false;
665
666 node_rx = ull_pdu_rx_alloc_peek(1);
667 LL_ASSERT(node_rx);
668
669 pdu_adv_rx = (void *)node_rx->pdu;
670
671 if (0) {
672 #if defined(CONFIG_BT_CENTRAL)
673 /* Initiator */
674 } else if (lll->conn &&
675 isr_scan_init_check(lll, pdu_adv_rx, rl_idx)) {
676 struct lll_conn *lll_conn;
677 struct node_rx_ftr *ftr;
678 struct node_rx_pdu *rx;
679 struct pdu_adv *pdu_tx;
680 uint32_t conn_interval_us;
681 uint32_t conn_offset_us;
682 uint32_t conn_space_us;
683 struct ull_hdr *ull;
684 uint32_t pdu_end_us;
685 #if defined(CONFIG_BT_CTLR_PRIVACY)
686 bt_addr_t *lrpa;
687 #endif /* CONFIG_BT_CTLR_PRIVACY */
688
689 if (IS_ENABLED(CONFIG_BT_CTLR_CHAN_SEL_2)) {
690 rx = ull_pdu_rx_alloc_peek(4);
691 } else {
692 rx = ull_pdu_rx_alloc_peek(3);
693 }
694
695 if (!rx) {
696 return -ENOBUFS;
697 }
698
699 pdu_end_us = radio_tmr_end_get();
700 if (!lll->ticks_window) {
701 uint32_t scan_interval_us;
702
703 /* FIXME: is this correct for continuous scanning? */
704 scan_interval_us = lll->interval * SCAN_INT_UNIT_US;
705 pdu_end_us %= scan_interval_us;
706 }
707 ull = HDR_LLL2ULL(lll);
708 if (pdu_end_us > (HAL_TICKER_TICKS_TO_US(ull->ticks_slot) -
709 EVENT_IFS_US - 352 - EVENT_OVERHEAD_START_US -
710 EVENT_TICKER_RES_MARGIN_US)) {
711 return -ETIME;
712 }
713
714 radio_switch_complete_and_disable();
715
716 /* Acquire the connection context */
717 lll_conn = lll->conn;
718
719 /* Tx the connect request packet */
720 pdu_tx = (void *)radio_pkt_scratch_get();
721 pdu_tx->type = PDU_ADV_TYPE_CONNECT_IND;
722
723 if (IS_ENABLED(CONFIG_BT_CTLR_CHAN_SEL_2)) {
724 pdu_tx->chan_sel = 1;
725 } else {
726 pdu_tx->chan_sel = 0;
727 }
728
729 pdu_tx->rx_addr = pdu_adv_rx->tx_addr;
730 pdu_tx->len = sizeof(struct pdu_adv_connect_ind);
731 #if defined(CONFIG_BT_CTLR_PRIVACY)
732 lrpa = ull_filter_lll_lrpa_get(rl_idx);
733 if (lll->rpa_gen && lrpa) {
734 pdu_tx->tx_addr = 1;
735 memcpy(&pdu_tx->connect_ind.init_addr[0], lrpa->val,
736 BDADDR_SIZE);
737 } else {
738 #else
739 if (1) {
740 #endif /* CONFIG_BT_CTLR_PRIVACY */
741 pdu_tx->tx_addr = lll->init_addr_type;
742 memcpy(&pdu_tx->connect_ind.init_addr[0],
743 &lll->init_addr[0], BDADDR_SIZE);
744 }
745 memcpy(&pdu_tx->connect_ind.adv_addr[0],
746 &pdu_adv_rx->adv_ind.addr[0], BDADDR_SIZE);
747 memcpy(&pdu_tx->connect_ind.access_addr[0],
748 &lll_conn->access_addr[0], 4);
749 memcpy(&pdu_tx->connect_ind.crc_init[0],
750 &lll_conn->crc_init[0], 3);
751 pdu_tx->connect_ind.win_size = 1;
752
753 conn_interval_us = (uint32_t)lll_conn->interval *
754 CONN_INT_UNIT_US;
755 conn_offset_us = radio_tmr_end_get() + 502 +
756 CONN_INT_UNIT_US;
757
758 if (!IS_ENABLED(CONFIG_BT_CTLR_SCHED_ADVANCED) ||
759 lll->conn_win_offset_us == 0U) {
760 conn_space_us = conn_offset_us;
761 pdu_tx->connect_ind.win_offset = sys_cpu_to_le16(0);
762 } else {
763 conn_space_us = lll->conn_win_offset_us;
764 while ((conn_space_us & ((uint32_t)1 << 31)) ||
765 (conn_space_us < conn_offset_us)) {
766 conn_space_us += conn_interval_us;
767 }
768 pdu_tx->connect_ind.win_offset =
769 sys_cpu_to_le16((conn_space_us -
770 conn_offset_us) /
771 CONN_INT_UNIT_US);
772 pdu_tx->connect_ind.win_size++;
773 }
774
775 pdu_tx->connect_ind.interval =
776 sys_cpu_to_le16(lll_conn->interval);
777 pdu_tx->connect_ind.latency =
778 sys_cpu_to_le16(lll_conn->latency);
779 pdu_tx->connect_ind.timeout =
780 sys_cpu_to_le16(lll->conn_timeout);
781 memcpy(&pdu_tx->connect_ind.chan_map[0],
782 &lll_conn->data_chan_map[0],
783 sizeof(pdu_tx->connect_ind.chan_map));
784 pdu_tx->connect_ind.hop = lll_conn->data_chan_hop;
785 pdu_tx->connect_ind.sca = lll_clock_sca_local_get();
786
787 radio_pkt_tx_set(pdu_tx);
788
789 /* assert if radio packet ptr is not set and radio started tx */
790 LL_ASSERT(!radio_is_ready());
791
792 if (IS_ENABLED(CONFIG_BT_CTLR_PROFILE_ISR)) {
793 lll_prof_cputime_capture();
794 }
795
796 radio_isr_set(isr_cleanup, lll);
797
798 #if defined(HAL_RADIO_GPIO_HAVE_PA_PIN)
799 if (IS_ENABLED(CONFIG_BT_CTLR_PROFILE_ISR)) {
800 /* PA/LNA enable is overwriting packet end
801 * used in ISR profiling, hence back it up
802 * for later use.
803 */
804 lll_prof_radio_end_backup();
805 }
806 radio_gpio_pa_setup();
807 radio_gpio_pa_lna_enable(radio_tmr_tifs_base_get() +
808 EVENT_IFS_US -
809 radio_rx_chain_delay_get(0, 0) -
810 HAL_RADIO_GPIO_PA_OFFSET);
811 #endif /* HAL_RADIO_GPIO_HAVE_PA_PIN */
812
813 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
814 if (rssi_ready) {
815 lll_conn->rssi_latest = radio_rssi_get();
816 }
817 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
818
819 /* block CPU so that there is no CRC error on pdu tx,
820 * this is only needed if we want the CPU to sleep.
821 * while(!radio_has_disabled())
822 * {cpu_sleep();}
823 * radio_status_reset();
824 */
825
826 /* Stop further LLL radio events */
827 lll->conn->central.initiated = 1;
828
829 rx = ull_pdu_rx_alloc();
830
831 rx->hdr.type = NODE_RX_TYPE_CONNECTION;
832 rx->hdr.handle = 0xffff;
833
834 uint8_t pdu_adv_rx_chan_sel = pdu_adv_rx->chan_sel;
835
836 memcpy(rx->pdu, pdu_tx, (offsetof(struct pdu_adv, connect_ind) +
837 sizeof(struct pdu_adv_connect_ind)));
838
839 /*
840 * Overwrite the sent chan sel with received chan sel, when
841 * giving this PDU to the higher layer.
842 */
843 pdu_adv_rx = (void *)rx->pdu;
844 pdu_adv_rx->chan_sel = pdu_adv_rx_chan_sel;
845
846 ftr = &(rx->rx_ftr);
847
848 ftr->param = lll;
849 ftr->ticks_anchor = radio_tmr_start_get();
850 ftr->radio_end_us = conn_space_us -
851 radio_tx_chain_delay_get(0, 0);
852
853 #if defined(CONFIG_BT_CTLR_PRIVACY)
854 ftr->rl_idx = irkmatch_ok ? rl_idx : FILTER_IDX_NONE;
855 #endif /* CONFIG_BT_CTLR_PRIVACY */
856
857 if (IS_ENABLED(CONFIG_BT_CTLR_CHAN_SEL_2)) {
858 ftr->extra = ull_pdu_rx_alloc();
859 }
860
861 ull_rx_put_sched(rx->hdr.link, rx);
862
863 return 0;
864 #endif /* CONFIG_BT_CENTRAL */
865
866 /* Active scanner */
867 } else if (((pdu_adv_rx->type == PDU_ADV_TYPE_ADV_IND) ||
868 (pdu_adv_rx->type == PDU_ADV_TYPE_SCAN_IND)) &&
869 (pdu_adv_rx->len <= sizeof(struct pdu_adv_adv_ind)) &&
870 lll->type &&
871 #if defined(CONFIG_BT_CENTRAL)
872 !lll->conn) {
873 #else /* !CONFIG_BT_CENTRAL */
874 1) {
875 #endif /* !CONFIG_BT_CENTRAL */
876 struct pdu_adv *pdu_tx;
877 #if defined(CONFIG_BT_CTLR_PRIVACY)
878 bt_addr_t *lrpa;
879 #endif /* CONFIG_BT_CTLR_PRIVACY */
880 uint32_t err;
881
882 /* setup tIFS switching */
883 radio_tmr_tifs_set(EVENT_IFS_US);
884 radio_switch_complete_and_rx(0);
885
886 /* save the adv packet */
887 err = isr_rx_scan_report(lll, rssi_ready,
888 irkmatch_ok ? rl_idx : FILTER_IDX_NONE,
889 false);
890 if (err) {
891 return err;
892 }
893
894 /* prepare the scan request packet */
895 pdu_tx = (void *)radio_pkt_scratch_get();
896 pdu_tx->type = PDU_ADV_TYPE_SCAN_REQ;
897 pdu_tx->rx_addr = pdu_adv_rx->tx_addr;
898 pdu_tx->len = sizeof(struct pdu_adv_scan_req);
899 #if defined(CONFIG_BT_CTLR_PRIVACY)
900 lrpa = ull_filter_lll_lrpa_get(rl_idx);
901 if (lll->rpa_gen && lrpa) {
902 pdu_tx->tx_addr = 1;
903 memcpy(&pdu_tx->scan_req.scan_addr[0], lrpa->val,
904 BDADDR_SIZE);
905 } else {
906 #else
907 if (1) {
908 #endif /* CONFIG_BT_CTLR_PRIVACY */
909 pdu_tx->tx_addr = lll->init_addr_type;
910 memcpy(&pdu_tx->scan_req.scan_addr[0],
911 &lll->init_addr[0], BDADDR_SIZE);
912 }
913 memcpy(&pdu_tx->scan_req.adv_addr[0],
914 &pdu_adv_rx->adv_ind.addr[0], BDADDR_SIZE);
915
916 radio_pkt_tx_set(pdu_tx);
917
918 /* assert if radio packet ptr is not set and radio started tx */
919 LL_ASSERT(!radio_is_ready());
920
921 if (IS_ENABLED(CONFIG_BT_CTLR_PROFILE_ISR)) {
922 lll_prof_cputime_capture();
923
924 }
925
926 /* capture end of Tx-ed PDU, used to calculate HCTO. */
927 radio_tmr_end_capture();
928
929 #if defined(HAL_RADIO_GPIO_HAVE_PA_PIN)
930 if (IS_ENABLED(CONFIG_BT_CTLR_PROFILE_ISR)) {
931 /* PA/LNA enable is overwriting packet end
932 * used in ISR profiling, hence back it up
933 * for later use.
934 */
935 lll_prof_radio_end_backup();
936 }
937
938 radio_gpio_pa_setup();
939 radio_gpio_pa_lna_enable(radio_tmr_tifs_base_get() +
940 EVENT_IFS_US -
941 radio_rx_chain_delay_get(0, 0) -
942 HAL_RADIO_GPIO_PA_OFFSET);
943 #endif /* HAL_RADIO_GPIO_HAVE_PA_PIN */
944
945 /* switch scanner state to active */
946 lll->state = 1U;
947 radio_isr_set(isr_tx, lll);
948
949 return 0;
950 }
951 /* Passive scanner or scan responses */
952 else if (((((pdu_adv_rx->type == PDU_ADV_TYPE_ADV_IND) ||
953 (pdu_adv_rx->type == PDU_ADV_TYPE_NONCONN_IND) ||
954 (pdu_adv_rx->type == PDU_ADV_TYPE_SCAN_IND)) &&
955 (pdu_adv_rx->len <= sizeof(struct pdu_adv_adv_ind))) ||
956 ((pdu_adv_rx->type == PDU_ADV_TYPE_DIRECT_IND) &&
957 (pdu_adv_rx->len == sizeof(struct pdu_adv_direct_ind)) &&
958 (/* allow directed adv packets addressed to this device */
959 isr_scan_tgta_check(lll, false, pdu_adv_rx, rl_idx,
960 &dir_report))) ||
961 #if defined(CONFIG_BT_CTLR_ADV_EXT)
962 ((pdu_adv_rx->type == PDU_ADV_TYPE_EXT_IND) &&
963 (lll->phy)) ||
964 #endif /* CONFIG_BT_CTLR_ADV_EXT */
965 ((pdu_adv_rx->type == PDU_ADV_TYPE_SCAN_RSP) &&
966 (pdu_adv_rx->len <= sizeof(struct pdu_adv_scan_rsp)) &&
967 (lll->state != 0U) &&
968 isr_scan_rsp_adva_matches(pdu_adv_rx))) &&
969 (pdu_adv_rx->len != 0) &&
970 #if defined(CONFIG_BT_CENTRAL)
971 !lll->conn) {
972 #else /* !CONFIG_BT_CENTRAL */
973 1) {
974 #endif /* !CONFIG_BT_CENTRAL */
975 uint32_t err;
976
977 /* save the scan response packet */
978 err = isr_rx_scan_report(lll, rssi_ready,
979 irkmatch_ok ? rl_idx :
980 FILTER_IDX_NONE,
981 dir_report);
982 if (err) {
983 return err;
984 }
985 }
986 /* invalid PDU */
987 else {
988 /* ignore and close this rx/tx chain ( code below ) */
989 return 1;
990 }
991
992 return 1;
993 }
994
995 #if defined(CONFIG_BT_CENTRAL)
996 static inline bool isr_scan_init_check(struct lll_scan *lll,
997 struct pdu_adv *pdu, uint8_t rl_idx)
998 {
999 return ((((lll->filter_policy & SCAN_FP_FILTER) != 0U) ||
1000 isr_scan_init_adva_check(lll, pdu, rl_idx)) &&
1001 (((pdu->type == PDU_ADV_TYPE_ADV_IND) &&
1002 (pdu->len <= sizeof(struct pdu_adv_adv_ind))) ||
1003 ((pdu->type == PDU_ADV_TYPE_DIRECT_IND) &&
1004 (pdu->len == sizeof(struct pdu_adv_direct_ind)) &&
1005 (/* allow directed adv packets addressed to this device */
1006 isr_scan_tgta_check(lll, true, pdu, rl_idx, NULL)))));
1007 }
1008
1009 static inline bool isr_scan_init_adva_check(struct lll_scan *lll,
1010 struct pdu_adv *pdu, uint8_t rl_idx)
1011 {
1012 #if defined(CONFIG_BT_CTLR_PRIVACY)
1013 /* Only applies to initiator with no filter accept list */
1014 if (rl_idx != FILTER_IDX_NONE) {
1015 return (rl_idx == lll->rl_idx);
1016 } else if (!ull_filter_lll_rl_addr_allowed(pdu->tx_addr,
1017 pdu->adv_ind.addr,
1018 &rl_idx)) {
1019 return false;
1020 }
1021 #endif /* CONFIG_BT_CTLR_PRIVACY */
1022 return ((lll->adv_addr_type == pdu->tx_addr) &&
1023 !memcmp(lll->adv_addr, &pdu->adv_ind.addr[0], BDADDR_SIZE));
1024 }
1025 #endif /* CONFIG_BT_CENTRAL */
1026
1027 static inline bool isr_scan_tgta_check(struct lll_scan *lll, bool init,
1028 struct pdu_adv *pdu, uint8_t rl_idx,
1029 bool *dir_report)
1030 {
1031 #if defined(CONFIG_BT_CTLR_PRIVACY)
1032 if (ull_filter_lll_rl_addr_resolve(pdu->rx_addr,
1033 pdu->direct_ind.tgt_addr, rl_idx)) {
1034 return true;
1035 } else if (init && lll->rpa_gen &&
1036 ull_filter_lll_lrpa_get(rl_idx)) {
1037 /* Initiator generating RPAs, and could not resolve TargetA:
1038 * discard
1039 */
1040 return false;
1041 }
1042 #endif /* CONFIG_BT_CTLR_PRIVACY */
1043
1044 return (((lll->init_addr_type == pdu->rx_addr) &&
1045 !memcmp(lll->init_addr, pdu->direct_ind.tgt_addr,
1046 BDADDR_SIZE))) ||
1047 /* allow directed adv packets where TargetA address
1048 * is resolvable private address (scanner only)
1049 */
1050 isr_scan_tgta_rpa_check(lll, pdu, dir_report);
1051 }
1052
1053 static inline bool isr_scan_tgta_rpa_check(struct lll_scan *lll,
1054 struct pdu_adv *pdu,
1055 bool *dir_report)
1056 {
1057 if (((lll->filter_policy & SCAN_FP_EXT) != 0U) &&
1058 (pdu->rx_addr != 0) &&
1059 ((pdu->direct_ind.tgt_addr[5] & 0xc0) == 0x40)) {
1060
1061 if (dir_report) {
1062 *dir_report = true;
1063 }
1064
1065 return true;
1066 }
1067
1068 return false;
1069 }
1070
1071 static inline bool isr_scan_rsp_adva_matches(struct pdu_adv *srsp)
1072 {
1073 struct pdu_adv *sreq = (void *)radio_pkt_scratch_get();
1074
1075 return ((sreq->rx_addr == srsp->tx_addr) &&
1076 (memcmp(&sreq->scan_req.adv_addr[0],
1077 &srsp->scan_rsp.addr[0], BDADDR_SIZE) == 0));
1078 }
1079
1080 static uint32_t isr_rx_scan_report(struct lll_scan *lll, uint8_t rssi_ready,
1081 uint8_t rl_idx, bool dir_report)
1082 {
1083 struct node_rx_pdu *node_rx;
1084 struct pdu_adv *pdu_adv_rx;
1085
1086 node_rx = ull_pdu_rx_alloc_peek(3);
1087 if (!node_rx) {
1088 return 1;
1089 }
1090 ull_pdu_rx_alloc();
1091
1092 /* Prepare the report (adv or scan resp) */
1093 node_rx->hdr.handle = 0xffff;
1094 if (0) {
1095
1096 #if defined(CONFIG_BT_HCI_MESH_EXT)
1097 } else if (_radio.advertiser.is_enabled &&
1098 _radio.advertiser.is_mesh) {
1099 node_rx->hdr.type = NODE_RX_TYPE_MESH_REPORT;
1100 #endif /* CONFIG_BT_HCI_MESH_EXT */
1101
1102 #if defined(CONFIG_BT_CTLR_ADV_EXT)
1103 } else if (lll->phy) {
1104 switch (lll->phy) {
1105 case PHY_1M:
1106 node_rx->hdr.type = NODE_RX_TYPE_EXT_1M_REPORT;
1107 break;
1108
1109 case PHY_CODED:
1110 node_rx->hdr.type = NODE_RX_TYPE_EXT_CODED_REPORT;
1111 break;
1112
1113 default:
1114 LL_ASSERT(0);
1115 break;
1116 }
1117 #endif /* CONFIG_BT_CTLR_ADV_EXT */
1118 } else {
1119 node_rx->hdr.type = NODE_RX_TYPE_REPORT;
1120 }
1121
1122 pdu_adv_rx = (void *)node_rx->pdu;
1123
1124 node_rx->rx_ftr.rssi = (rssi_ready) ?
1125 (radio_rssi_get() & 0x7f)
1126 : 0x7f;
1127 #if defined(CONFIG_BT_CTLR_PRIVACY)
1128 /* save the resolving list index. */
1129 node_rx->rx_ftr.rl_idx = rl_idx;
1130 #endif /* CONFIG_BT_CTLR_PRIVACY */
1131 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
1132 /* save the directed adv report flag */
1133 node_rx->rx_ftr.direct = dir_report;
1134 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
1135 #if defined(CONFIG_BT_HCI_MESH_EXT)
1136 if (node_rx->hdr.type == NODE_RX_TYPE_MESH_REPORT) {
1137 /* save channel and anchor point ticks. */
1138 node_rx->rx_ftr.chan = _radio.scanner.chan - 1;
1139 node_rx->rx_ftr.ticks_anchor = _radio.ticks_anchor;
1140 }
1141 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
1142
1143 ull_rx_put_sched(node_rx->hdr.link, node_rx);
1144
1145 return 0;
1146 }
1147