1 /*
2 * Copyright (c) 2018-2021 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <soc.h>
9 #include <zephyr/bluetooth/hci_types.h>
10 #include <zephyr/sys/byteorder.h>
11
12 #include "util/util.h"
13 #include "util/memq.h"
14 #include "util/mem.h"
15 #include "util/mayfly.h"
16 #include "util/dbuf.h"
17
18 #include "hal/cpu.h"
19 #include "hal/ccm.h"
20 #include "hal/radio.h"
21 #include "hal/ticker.h"
22
23 #include "ticker/ticker.h"
24
25 #include "pdu_df.h"
26 #include "lll/pdu_vendor.h"
27 #include "pdu.h"
28
29 #include "lll.h"
30 #include "lll_clock.h"
31 #include "lll/lll_vendor.h"
32 #include "lll/lll_adv_types.h"
33 #include "lll_adv.h"
34 #include "lll/lll_adv_pdu.h"
35 #include "lll_chan.h"
36 #include "lll_scan.h"
37 #include "lll/lll_df_types.h"
38 #include "lll_conn.h"
39 #include "lll_central.h"
40 #include "lll_filter.h"
41 #include "lll_conn_iso.h"
42
43 #include "ll_sw/ull_tx_queue.h"
44
45 #include "ull_adv_types.h"
46 #include "ull_scan_types.h"
47 #include "ull_conn_types.h"
48 #include "ull_filter.h"
49
50 #include "ull_internal.h"
51 #include "ull_chan_internal.h"
52 #include "ull_scan_internal.h"
53 #include "ull_conn_internal.h"
54 #include "ull_central_internal.h"
55
56 #include "ll.h"
57 #include "ll_feat.h"
58 #include "ll_settings.h"
59
60 #include "ll_sw/isoal.h"
61 #include "ll_sw/ull_iso_types.h"
62 #include "ll_sw/ull_conn_iso_types.h"
63 #include "ll_sw/ull_conn_iso_internal.h"
64
65 #include "ll_sw/ull_llcp.h"
66
67 #include "hal/debug.h"
68
69 static void ticker_op_stop_scan_cb(uint32_t status, void *param);
70 #if defined(CONFIG_BT_CTLR_ADV_EXT) && defined(CONFIG_BT_CTLR_PHY_CODED)
71 static void ticker_op_stop_scan_other_cb(uint32_t status, void *param);
72 #endif /* CONFIG_BT_CTLR_ADV_EXT && CONFIG_BT_CTLR_PHY_CODED */
73 static void ticker_op_cb(uint32_t status, void *param);
74 static inline void conn_release(struct ll_scan_set *scan);
75
76 #if defined(CONFIG_BT_CTLR_ADV_EXT)
ll_create_connection(uint16_t scan_interval,uint16_t scan_window,uint8_t filter_policy,uint8_t peer_addr_type,uint8_t const * const peer_addr,uint8_t own_addr_type,uint16_t interval,uint16_t latency,uint16_t timeout,uint8_t phy)77 uint8_t ll_create_connection(uint16_t scan_interval, uint16_t scan_window,
78 uint8_t filter_policy, uint8_t peer_addr_type,
79 uint8_t const *const peer_addr, uint8_t own_addr_type,
80 uint16_t interval, uint16_t latency, uint16_t timeout,
81 uint8_t phy)
82 #else /* !CONFIG_BT_CTLR_ADV_EXT */
83 uint8_t ll_create_connection(uint16_t scan_interval, uint16_t scan_window,
84 uint8_t filter_policy, uint8_t peer_addr_type,
85 uint8_t const *const peer_addr, uint8_t own_addr_type,
86 uint16_t interval, uint16_t latency, uint16_t timeout)
87 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
88 {
89 struct lll_conn *conn_lll;
90 uint32_t conn_interval_us;
91 uint8_t own_id_addr_type;
92 struct ll_scan_set *scan;
93 uint32_t ready_delay_us;
94 uint8_t *own_id_addr;
95 struct lll_scan *lll;
96 struct ll_conn *conn;
97 uint16_t max_tx_time;
98 uint16_t max_rx_time;
99 memq_link_t *link;
100 uint32_t slot_us;
101 uint8_t hop;
102 int err;
103
104 scan = ull_scan_is_disabled_get(SCAN_HANDLE_1M);
105 if (!scan) {
106 return BT_HCI_ERR_CMD_DISALLOWED;
107 }
108
109 /* Check if random address has been set */
110 own_id_addr_type = (own_addr_type & 0x01);
111 own_id_addr = ll_addr_get(own_id_addr_type);
112 if (own_id_addr_type && !mem_nz((void *)own_id_addr, BDADDR_SIZE)) {
113 return BT_HCI_ERR_INVALID_PARAM;
114 }
115
116 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN)
117 /* Do not connect twice to the same peer */
118 if (ull_conn_peer_connected(own_id_addr_type, own_id_addr,
119 peer_addr_type, peer_addr)) {
120 return BT_HCI_ERR_CONN_ALREADY_EXISTS;
121 }
122 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN */
123
124 #if defined(CONFIG_BT_CTLR_ADV_EXT)
125 #if defined(CONFIG_BT_CTLR_PHY_CODED)
126 struct ll_scan_set *scan_coded;
127 struct lll_scan *lll_coded;
128
129 scan_coded = ull_scan_is_disabled_get(SCAN_HANDLE_PHY_CODED);
130 if (!scan_coded) {
131 return BT_HCI_ERR_CMD_DISALLOWED;
132 }
133
134 lll = &scan->lll;
135 lll_coded = &scan_coded->lll;
136
137 /* NOTE: When coded PHY is supported, and connection establishment
138 * over coded PHY is selected by application then look for
139 * a connection context already assigned to 1M PHY scanning
140 * context. Use the same connection context in the coded PHY
141 * scanning context.
142 */
143 if (phy & BT_HCI_LE_EXT_SCAN_PHY_CODED) {
144 if (!lll_coded->conn) {
145 lll_coded->conn = lll->conn;
146 }
147 scan = scan_coded;
148 lll = lll_coded;
149 } else {
150 if (!lll->conn) {
151 lll->conn = lll_coded->conn;
152 }
153 }
154
155 #else /* !CONFIG_BT_CTLR_PHY_CODED */
156 if (phy & ~BT_HCI_LE_EXT_SCAN_PHY_1M) {
157 return BT_HCI_ERR_CMD_DISALLOWED;
158 }
159
160 lll = &scan->lll;
161
162 #endif /* !CONFIG_BT_CTLR_PHY_CODED */
163
164 /* NOTE: non-zero PHY value enables initiating connection on that PHY */
165 lll->phy = phy;
166
167 #else /* !CONFIG_BT_CTLR_ADV_EXT */
168 lll = &scan->lll;
169 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
170
171 if (lll->conn) {
172 conn_lll = lll->conn;
173 conn = HDR_LLL2ULL(conn_lll);
174
175 goto conn_is_valid;
176 }
177
178 link = ll_rx_link_alloc();
179 if (!link) {
180 return BT_HCI_ERR_MEM_CAPACITY_EXCEEDED;
181 }
182
183 conn = ll_conn_acquire();
184 if (!conn) {
185 ll_rx_link_release(link);
186
187 return BT_HCI_ERR_MEM_CAPACITY_EXCEEDED;
188 }
189
190 conn_lll = &conn->lll;
191
192 err = util_aa_le32(conn_lll->access_addr);
193 LL_ASSERT(!err);
194
195 lll_csrand_get(conn_lll->crc_init, sizeof(conn_lll->crc_init));
196
197 conn_lll->handle = 0xFFFF;
198 conn_lll->interval = interval;
199 conn_lll->latency = latency;
200
201 if (!conn_lll->link_tx_free) {
202 conn_lll->link_tx_free = &conn_lll->link_tx;
203 }
204
205 memq_init(conn_lll->link_tx_free, &conn_lll->memq_tx.head,
206 &conn_lll->memq_tx.tail);
207 conn_lll->link_tx_free = NULL;
208
209 conn_lll->packet_tx_head_len = 0;
210 conn_lll->packet_tx_head_offset = 0;
211
212 conn_lll->sn = 0;
213 conn_lll->nesn = 0;
214 conn_lll->empty = 0;
215
216 #if defined(CONFIG_BT_CTLR_PHY)
217 /* Use the default 1M PHY, extended connection initiation in LLL will
218 * update this with the correct PHY and defaults using the coding on
219 * which the connection is established.
220 */
221 conn_lll->phy_tx = PHY_1M;
222 conn_lll->phy_tx_time = PHY_1M;
223 conn_lll->phy_flags = PHY_FLAGS_S8;
224 conn_lll->phy_rx = PHY_1M;
225 #endif /* CONFIG_BT_CTLR_PHY */
226
227 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
228 ull_dle_init(conn, PHY_1M);
229 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
230
231 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
232 conn_lll->rssi_latest = BT_HCI_LE_RSSI_NOT_AVAILABLE;
233 #if defined(CONFIG_BT_CTLR_CONN_RSSI_EVENT)
234 conn_lll->rssi_reported = BT_HCI_LE_RSSI_NOT_AVAILABLE;
235 conn_lll->rssi_sample_count = 0;
236 #endif /* CONFIG_BT_CTLR_CONN_RSSI_EVENT */
237 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
238
239 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
240 conn_lll->tx_pwr_lvl = RADIO_TXP_DEFAULT;
241 #endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */
242
243 /* FIXME: BEGIN: Move to ULL? */
244 conn_lll->latency_prepare = 0;
245 conn_lll->latency_event = 0;
246 conn_lll->event_counter = 0;
247
248 conn_lll->data_chan_count = ull_chan_map_get(conn_lll->data_chan_map);
249 lll_csrand_get(&hop, sizeof(uint8_t));
250 conn_lll->data_chan_hop = 5 + (hop % 12);
251 conn_lll->data_chan_sel = 0;
252 conn_lll->data_chan_use = 0;
253 conn_lll->role = 0;
254 conn_lll->central.initiated = 0;
255 conn_lll->central.cancelled = 0;
256 conn_lll->central.forced = 0;
257 /* FIXME: END: Move to ULL? */
258 #if defined(CONFIG_BT_CTLR_CONN_META)
259 memset(&conn_lll->conn_meta, 0, sizeof(conn_lll->conn_meta));
260 #endif /* CONFIG_BT_CTLR_CONN_META */
261
262 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX)
263 conn_lll->df_rx_cfg.is_initialized = 0U;
264 conn_lll->df_rx_cfg.hdr.elem_size = sizeof(struct lll_df_conn_rx_params);
265 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RX */
266 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_TX)
267 conn_lll->df_tx_cfg.is_initialized = 0U;
268 conn_lll->df_tx_cfg.cte_rsp_en = 0U;
269 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_TX */
270
271 conn->connect_expire = CONN_ESTAB_COUNTDOWN;
272 conn->supervision_expire = 0U;
273 conn_interval_us = (uint32_t)interval * CONN_INT_UNIT_US;
274 conn->supervision_timeout = timeout;
275
276 #if defined(CONFIG_BT_CTLR_LE_PING)
277 conn->apto_expire = 0U;
278 /* APTO in no. of connection events */
279 conn->apto_reload = RADIO_CONN_EVENTS((30000000), conn_interval_us);
280 conn->appto_expire = 0U;
281 /* Dispatch LE Ping PDU 6 connection events (that peer would listen to)
282 * before 30s timeout
283 * TODO: "peer listens to" is greater than 30s due to latency
284 */
285 conn->appto_reload = (conn->apto_reload > (conn_lll->latency + 6)) ?
286 (conn->apto_reload - (conn_lll->latency + 6)) :
287 conn->apto_reload;
288 #endif /* CONFIG_BT_CTLR_LE_PING */
289
290 /* Re-initialize the control procedure data structures */
291 ull_llcp_init(conn);
292
293 /* Setup the PRT reload */
294 ull_cp_prt_reload_set(conn, conn_interval_us);
295
296 conn->llcp_terminate.reason_final = 0U;
297 /* NOTE: use allocated link for generating dedicated
298 * terminate ind rx node
299 */
300 conn->llcp_terminate.node_rx.rx.hdr.link = link;
301
302 #if defined(CONFIG_BT_CTLR_PHY)
303 conn->phy_pref_tx = ull_conn_default_phy_tx_get();
304 conn->phy_pref_rx = ull_conn_default_phy_rx_get();
305 #endif /* CONFIG_BT_CTLR_PHY */
306
307 #if defined(CONFIG_BT_CTLR_LE_ENC)
308 conn->pause_rx_data = 0U;
309 #endif /* CONFIG_BT_CTLR_LE_ENC */
310
311 /* Re-initialize the Tx Q */
312 ull_tx_q_init(&conn->tx_q);
313
314 conn_lll->tifs_tx_us = EVENT_IFS_DEFAULT_US;
315 conn_lll->tifs_rx_us = EVENT_IFS_DEFAULT_US;
316 conn_lll->tifs_hcto_us = EVENT_IFS_DEFAULT_US;
317 conn_lll->tifs_cis_us = EVENT_IFS_DEFAULT_US;
318
319 /* TODO: active_to_start feature port */
320 conn->ull.ticks_active_to_start = 0U;
321 conn->ull.ticks_prepare_to_start =
322 HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_XTAL_US);
323 conn->ull.ticks_preempt_to_start =
324 HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_PREEMPT_MIN_US);
325
326 #if defined(CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN)
327 /* Remember peer and own identity address */
328 conn->peer_id_addr_type = peer_addr_type;
329 (void)memcpy(conn->peer_id_addr, peer_addr, sizeof(conn->peer_id_addr));
330 conn->own_id_addr_type = own_id_addr_type;
331 (void)memcpy(conn->own_id_addr, own_id_addr, sizeof(conn->own_id_addr));
332 #endif /* CONFIG_BT_CTLR_CHECK_SAME_PEER_CONN */
333
334 lll->conn = conn_lll;
335
336 ull_hdr_init(&conn->ull);
337 lll_hdr_init(&conn->lll, conn);
338
339 conn_is_valid:
340 #if defined(CONFIG_BT_CTLR_PHY)
341 ready_delay_us = lll_radio_tx_ready_delay_get(conn_lll->phy_tx,
342 conn_lll->phy_flags);
343 #else
344 ready_delay_us = lll_radio_tx_ready_delay_get(0, 0);
345 #endif
346
347 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
348 #if defined(CONFIG_BT_CTLR_ADV_EXT)
349 conn_lll->dle.eff.max_tx_time = MAX(conn_lll->dle.eff.max_tx_time,
350 PDU_DC_MAX_US(PDU_DC_PAYLOAD_SIZE_MIN,
351 lll->phy));
352 conn_lll->dle.eff.max_rx_time = MAX(conn_lll->dle.eff.max_rx_time,
353 PDU_DC_MAX_US(PDU_DC_PAYLOAD_SIZE_MIN,
354 lll->phy));
355 #endif /* CONFIG_BT_CTLR_ADV_EXT */
356 max_tx_time = conn_lll->dle.eff.max_tx_time;
357 max_rx_time = conn_lll->dle.eff.max_rx_time;
358 #else /* CONFIG_BT_CTLR_DATA_LENGTH */
359 max_tx_time = PDU_DC_MAX_US(PDU_DC_PAYLOAD_SIZE_MIN, PHY_1M);
360 max_rx_time = PDU_DC_MAX_US(PDU_DC_PAYLOAD_SIZE_MIN, PHY_1M);
361 #if defined(CONFIG_BT_CTLR_ADV_EXT)
362 max_tx_time = MAX(max_tx_time,
363 PDU_DC_MAX_US(PDU_DC_PAYLOAD_SIZE_MIN, lll->phy));
364 max_rx_time = MAX(max_rx_time,
365 PDU_DC_MAX_US(PDU_DC_PAYLOAD_SIZE_MIN, lll->phy));
366 #endif /* CONFIG_BT_CTLR_ADV_EXT */
367 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
368
369 /* Calculate event time reservation */
370 slot_us = max_tx_time + max_rx_time;
371 slot_us += conn_lll->tifs_rx_us + (EVENT_CLOCK_JITTER_US << 1);
372 slot_us += ready_delay_us;
373 slot_us += EVENT_OVERHEAD_START_US + EVENT_OVERHEAD_END_US;
374
375 conn->ull.ticks_slot = HAL_TICKER_US_TO_TICKS_CEIL(slot_us);
376
377 #if defined(CONFIG_BT_CTLR_PRIVACY)
378 ull_filter_scan_update(filter_policy);
379
380 lll->rl_idx = FILTER_IDX_NONE;
381 lll->rpa_gen = 0;
382 if (!filter_policy && ull_filter_lll_rl_enabled()) {
383 /* Look up the resolving list */
384 lll->rl_idx = ull_filter_rl_find(peer_addr_type, peer_addr,
385 NULL);
386 }
387
388 if (own_addr_type == BT_HCI_OWN_ADDR_RPA_OR_PUBLIC ||
389 own_addr_type == BT_HCI_OWN_ADDR_RPA_OR_RANDOM) {
390
391 /* Generate RPAs if required */
392 ull_filter_rpa_update(false);
393 own_addr_type &= 0x1;
394 lll->rpa_gen = 1;
395 }
396 #endif
397
398 scan->own_addr_type = own_addr_type;
399 lll->adv_addr_type = peer_addr_type;
400 memcpy(lll->adv_addr, peer_addr, BDADDR_SIZE);
401 lll->conn_timeout = timeout;
402
403 scan->ticks_window = ull_scan_params_set(lll, 0U, scan_interval,
404 scan_window, filter_policy);
405
406 #if defined(CONFIG_BT_CTLR_ADV_EXT)
407 return 0;
408 #else /* !CONFIG_BT_CTLR_ADV_EXT */
409 /* wait for stable clocks */
410 err = lll_clock_wait();
411 if (err) {
412 conn_release(scan);
413
414 return BT_HCI_ERR_HW_FAILURE;
415 }
416
417 return ull_scan_enable(scan);
418 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
419 }
420
421 #if defined(CONFIG_BT_CTLR_ADV_EXT)
ll_connect_enable(uint8_t is_coded_included)422 uint8_t ll_connect_enable(uint8_t is_coded_included)
423 {
424 uint8_t err = BT_HCI_ERR_CMD_DISALLOWED;
425 struct ll_scan_set *scan;
426
427 scan = ull_scan_set_get(SCAN_HANDLE_1M);
428
429 /* wait for stable clocks */
430 err = lll_clock_wait();
431 if (err) {
432 conn_release(scan);
433
434 return BT_HCI_ERR_HW_FAILURE;
435 }
436
437 if (!is_coded_included ||
438 (scan->lll.phy & PHY_1M)) {
439 err = ull_scan_enable(scan);
440 if (err) {
441 return err;
442 }
443 }
444
445 if (IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED) && is_coded_included) {
446 scan = ull_scan_set_get(SCAN_HANDLE_PHY_CODED);
447 err = ull_scan_enable(scan);
448 if (err) {
449 return err;
450 }
451 }
452
453 return err;
454 }
455 #endif /* CONFIG_BT_CTLR_ADV_EXT */
456
ll_connect_disable(void ** rx)457 uint8_t ll_connect_disable(void **rx)
458 {
459 struct ll_scan_set *scan_coded;
460 struct lll_scan *scan_lll;
461 struct lll_conn *conn_lll;
462 struct ll_scan_set *scan;
463 uint8_t err;
464
465 scan = ull_scan_is_enabled_get(SCAN_HANDLE_1M);
466
467 if (IS_ENABLED(CONFIG_BT_CTLR_ADV_EXT) &&
468 IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED)) {
469 scan_coded = ull_scan_is_enabled_get(SCAN_HANDLE_PHY_CODED);
470 } else {
471 scan_coded = NULL;
472 }
473
474 if (!scan) {
475 if (!scan_coded) {
476 return BT_HCI_ERR_CMD_DISALLOWED;
477 }
478
479 scan_lll = &scan_coded->lll;
480 } else {
481 scan_lll = &scan->lll;
482 }
483
484 /* Check if initiator active */
485 conn_lll = scan_lll->conn;
486 if (!conn_lll) {
487 /* Scanning not associated with initiation of a connection or
488 * connection setup already complete (was set to NULL in
489 * ull_central_setup), but HCI event not processed by host.
490 */
491 return BT_HCI_ERR_CMD_DISALLOWED;
492 }
493
494 /* Indicate to LLL that a cancellation is requested */
495 conn_lll->central.cancelled = 1U;
496 cpu_dmb();
497
498 /* Check if connection was established under race condition, i.e.
499 * before the cancelled flag was set.
500 */
501 conn_lll = scan_lll->conn;
502 if (!conn_lll) {
503 /* Connection setup completed on race condition with cancelled
504 * flag, before it was set.
505 */
506 return BT_HCI_ERR_CMD_DISALLOWED;
507 }
508
509 if (scan) {
510 err = ull_scan_disable(SCAN_HANDLE_1M, scan);
511 } else {
512 err = 0U;
513 }
514
515 if (!err && scan_coded) {
516 err = ull_scan_disable(SCAN_HANDLE_PHY_CODED, scan_coded);
517 }
518
519 if (!err) {
520 struct node_rx_pdu *node_rx;
521 struct node_rx_cc *cc;
522 struct ll_conn *conn;
523 memq_link_t *link;
524
525 conn = HDR_LLL2ULL(conn_lll);
526 node_rx = (void *)&conn->llcp_terminate.node_rx.rx;
527 link = node_rx->hdr.link;
528 LL_ASSERT(link);
529
530 /* free the memq link early, as caller could overwrite it */
531 ll_rx_link_release(link);
532
533 node_rx->hdr.type = NODE_RX_TYPE_CONNECTION;
534 node_rx->hdr.handle = 0xffff;
535
536 /* NOTE: struct llcp_terminate.node_rx has uint8_t member
537 * following the struct node_rx_hdr to store the reason.
538 */
539 cc = (void *)node_rx->pdu;
540 cc->status = BT_HCI_ERR_UNKNOWN_CONN_ID;
541
542 /* NOTE: Since NODE_RX_TYPE_CONNECTION is also generated from
543 * LLL context for other cases, pass LLL context as
544 * parameter.
545 */
546 node_rx->rx_ftr.param = scan_lll;
547
548 *rx = node_rx;
549 }
550
551 return err;
552 }
553
554 #if defined(CONFIG_BT_CTLR_LE_ENC)
ll_enc_req_send(uint16_t handle,uint8_t const * const rand_num,uint8_t const * const ediv,uint8_t const * const ltk)555 uint8_t ll_enc_req_send(uint16_t handle, uint8_t const *const rand_num,
556 uint8_t const *const ediv, uint8_t const *const ltk)
557 {
558 struct ll_conn *conn;
559
560 conn = ll_connected_get(handle);
561 if (!conn) {
562 return BT_HCI_ERR_UNKNOWN_CONN_ID;
563 }
564
565 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
566 struct ll_conn_iso_stream *cis = ll_conn_iso_stream_get_by_acl(conn, NULL);
567
568 if (cis || ull_lp_cc_is_enqueued(conn)) {
569 return BT_HCI_ERR_CMD_DISALLOWED;
570 }
571 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
572
573 if (!conn->lll.enc_tx && !conn->lll.enc_rx) {
574 /* Encryption is fully disabled */
575 return ull_cp_encryption_start(conn, rand_num, ediv, ltk);
576 } else if (conn->lll.enc_tx && conn->lll.enc_rx) {
577 /* Encryption is fully enabled */
578 return ull_cp_encryption_pause(conn, rand_num, ediv, ltk);
579 }
580
581 return BT_HCI_ERR_CMD_DISALLOWED;
582 }
583 #endif /* CONFIG_BT_CTLR_LE_ENC */
584
ull_central_reset(void)585 int ull_central_reset(void)
586 {
587 int err;
588 void *rx;
589
590 err = ll_connect_disable(&rx);
591 if (!err) {
592 struct ll_scan_set *scan;
593
594 scan = ull_scan_is_enabled_get(SCAN_HANDLE_1M);
595
596 if (IS_ENABLED(CONFIG_BT_CTLR_ADV_EXT) &&
597 IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED)) {
598 struct ll_scan_set *scan_other;
599
600 scan_other =
601 ull_scan_is_enabled_get(SCAN_HANDLE_PHY_CODED);
602 if (scan_other) {
603 if (scan) {
604 scan->is_enabled = 0U;
605 scan->lll.conn = NULL;
606 }
607
608 scan = scan_other;
609 }
610 }
611
612 LL_ASSERT(scan);
613
614 scan->is_enabled = 0U;
615 scan->lll.conn = NULL;
616 }
617
618 ARG_UNUSED(rx);
619
620 return err;
621 }
622
ull_central_cleanup(struct node_rx_pdu * rx_free)623 void ull_central_cleanup(struct node_rx_pdu *rx_free)
624 {
625 struct lll_conn *conn_lll;
626 struct ll_scan_set *scan;
627 struct ll_conn *conn;
628 memq_link_t *link;
629
630 /* NOTE: `scan` variable can be 1M PHY or coded PHY scanning context.
631 * Single connection context is allocated in both the 1M PHY and
632 * coded PHY scanning context, hence releasing only this one
633 * connection context.
634 */
635 scan = HDR_LLL2ULL(rx_free->rx_ftr.param);
636 conn_lll = scan->lll.conn;
637 LL_ASSERT(conn_lll);
638 scan->lll.conn = NULL;
639
640 LL_ASSERT(!conn_lll->link_tx_free);
641 link = memq_deinit(&conn_lll->memq_tx.head,
642 &conn_lll->memq_tx.tail);
643 LL_ASSERT(link);
644 conn_lll->link_tx_free = link;
645
646 conn = HDR_LLL2ULL(conn_lll);
647 ll_conn_release(conn);
648
649 /* 1M PHY is disabled here if both 1M and coded PHY was enabled for
650 * connection establishment.
651 */
652 scan->is_enabled = 0U;
653
654 #if defined(CONFIG_BT_CTLR_ADV_EXT) && defined(CONFIG_BT_CTLR_PHY_CODED)
655 scan->lll.phy = 0U;
656
657 /* Determine if coded PHY was also enabled, if so, reset the assigned
658 * connection context, enabled flag and phy value.
659 */
660 struct ll_scan_set *scan_coded =
661 ull_scan_is_enabled_get(SCAN_HANDLE_PHY_CODED);
662 if (scan_coded && scan_coded != scan) {
663 conn_lll = scan_coded->lll.conn;
664 LL_ASSERT(conn_lll);
665 scan_coded->lll.conn = NULL;
666
667 scan_coded->is_enabled = 0U;
668 scan_coded->lll.phy = 0U;
669 }
670 #endif /* CONFIG_BT_CTLR_ADV_EXT && CONFIG_BT_CTLR_PHY_CODED */
671 }
672
ull_central_setup(struct node_rx_pdu * rx,struct node_rx_ftr * ftr,struct lll_conn * lll)673 void ull_central_setup(struct node_rx_pdu *rx, struct node_rx_ftr *ftr,
674 struct lll_conn *lll)
675 {
676 uint32_t conn_offset_us, conn_interval_us;
677 uint8_t ticker_id_scan, ticker_id_conn;
678 uint8_t peer_addr[BDADDR_SIZE];
679 uint32_t ticks_slot_overhead;
680 uint32_t ticks_slot_offset;
681 struct ll_scan_set *scan;
682 struct pdu_adv *pdu_tx;
683 uint8_t peer_addr_type;
684 uint32_t ticker_status;
685 uint32_t ticks_at_stop;
686 struct node_rx_cc *cc;
687 struct ll_conn *conn;
688 memq_link_t *link;
689 uint8_t chan_sel;
690 void *node;
691
692 /* Get reference to Tx-ed CONNECT_IND PDU */
693 pdu_tx = (void *)rx->pdu;
694
695 /* Backup peer addr and type, as we reuse the Tx-ed PDU to generate
696 * event towards LL
697 */
698 peer_addr_type = pdu_tx->rx_addr;
699 memcpy(peer_addr, &pdu_tx->connect_ind.adv_addr[0], BDADDR_SIZE);
700
701 /* This is the chan sel bit from the received adv pdu */
702 chan_sel = pdu_tx->chan_sel;
703
704 /* Check for pdu field being aligned before populating connection
705 * complete event.
706 */
707 node = pdu_tx;
708 LL_ASSERT(IS_PTR_ALIGNED(node, struct node_rx_cc));
709
710 /* Populate the fields required for connection complete event */
711 cc = node;
712 cc->status = 0U;
713 cc->role = 0U;
714
715 #if defined(CONFIG_BT_CTLR_PRIVACY)
716 uint8_t rl_idx = ftr->rl_idx;
717
718 if (ftr->lrpa_used) {
719 memcpy(&cc->local_rpa[0], &pdu_tx->connect_ind.init_addr[0],
720 BDADDR_SIZE);
721 } else {
722 memset(&cc->local_rpa[0], 0x0, BDADDR_SIZE);
723 }
724
725 if (rl_idx != FILTER_IDX_NONE) {
726 /* Store identity address */
727 ll_rl_id_addr_get(rl_idx, &cc->peer_addr_type,
728 &cc->peer_addr[0]);
729 /* Mark it as identity address from RPA (0x02, 0x03) */
730 MARK_AS_IDENTITY_ADDR(cc->peer_addr_type);
731
732 /* Store peer RPA */
733 memcpy(&cc->peer_rpa[0], &peer_addr[0], BDADDR_SIZE);
734 } else {
735 memset(&cc->peer_rpa[0], 0x0, BDADDR_SIZE);
736 #else
737 if (1) {
738 #endif /* CONFIG_BT_CTLR_PRIVACY */
739 cc->peer_addr_type = peer_addr_type;
740 memcpy(cc->peer_addr, &peer_addr[0], BDADDR_SIZE);
741 }
742
743 scan = HDR_LLL2ULL(ftr->param);
744
745 cc->interval = lll->interval;
746 cc->latency = lll->latency;
747 cc->timeout = scan->lll.conn_timeout;
748 cc->sca = lll_clock_sca_local_get();
749
750 conn = lll->hdr.parent;
751 lll->handle = ll_conn_handle_get(conn);
752 rx->hdr.handle = lll->handle;
753
754 /* Set LLCP as connection-wise connected */
755 ull_cp_state_set(conn, ULL_CP_CONNECTED);
756
757 #if defined(CONFIG_BT_CTLR_SYNC_TRANSFER_RECEIVER)
758 /* Set default PAST parameters */
759 conn->past = ull_conn_default_past_param_get();
760 #endif /* CONFIG_BT_CTLR_SYNC_TRANSFER_RECEIVER */
761
762 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
763 lll->tx_pwr_lvl = RADIO_TXP_DEFAULT;
764 #endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */
765
766 /* Use the link stored in the node rx to enqueue connection
767 * complete node rx towards LL context.
768 */
769 link = rx->hdr.link;
770
771 /* Use Channel Selection Algorithm #2 if peer too supports it */
772 if (IS_ENABLED(CONFIG_BT_CTLR_CHAN_SEL_2)) {
773 struct node_rx_pdu *rx_csa;
774 struct node_rx_cs *cs;
775
776 /* pick the rx node instance stored within the connection
777 * rx node.
778 */
779 rx_csa = (void *)ftr->extra;
780
781 /* Enqueue the connection event */
782 ll_rx_put(link, rx);
783
784 /* use the rx node for CSA event */
785 rx = rx_csa;
786 link = rx->hdr.link;
787
788 rx->hdr.handle = lll->handle;
789 rx->hdr.type = NODE_RX_TYPE_CHAN_SEL_ALGO;
790
791 cs = (void *)rx_csa->pdu;
792
793 if (chan_sel) {
794 lll->data_chan_sel = 1;
795 lll->data_chan_id = lll_chan_id(lll->access_addr);
796
797 cs->csa = 0x01;
798 } else {
799 cs->csa = 0x00;
800 }
801 }
802
803 ll_rx_put_sched(link, rx);
804
805 ticks_slot_offset = MAX(conn->ull.ticks_active_to_start,
806 conn->ull.ticks_prepare_to_start);
807 if (IS_ENABLED(CONFIG_BT_CTLR_LOW_LAT)) {
808 ticks_slot_overhead = ticks_slot_offset;
809 } else {
810 ticks_slot_overhead = 0U;
811 }
812 ticks_slot_offset += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
813
814 conn_interval_us = lll->interval * CONN_INT_UNIT_US;
815 conn_offset_us = ftr->radio_end_us;
816
817 #if defined(CONFIG_BT_CTLR_PHY)
818 conn_offset_us -= lll_radio_tx_ready_delay_get(lll->phy_tx,
819 lll->phy_flags);
820 #else
821 conn_offset_us -= lll_radio_tx_ready_delay_get(0, 0);
822 #endif
823
824
825 #if (CONFIG_BT_CTLR_ULL_HIGH_PRIO == CONFIG_BT_CTLR_ULL_LOW_PRIO)
826 /* disable ticker job, in order to chain stop and start to avoid RTC
827 * being stopped if no tickers active.
828 */
829 mayfly_enable(TICKER_USER_ID_ULL_HIGH, TICKER_USER_ID_ULL_LOW, 0);
830 #endif
831
832 /* Stop Scanner */
833 ticker_id_scan = TICKER_ID_SCAN_BASE + ull_scan_handle_get(scan);
834 ticks_at_stop = ftr->ticks_anchor +
835 HAL_TICKER_US_TO_TICKS(conn_offset_us) -
836 ticks_slot_offset;
837 ticker_status = ticker_stop_abs(TICKER_INSTANCE_ID_CTLR,
838 TICKER_USER_ID_ULL_HIGH,
839 ticker_id_scan, ticks_at_stop,
840 ticker_op_stop_scan_cb, scan);
841 LL_ASSERT((ticker_status == TICKER_STATUS_SUCCESS) ||
842 (ticker_status == TICKER_STATUS_BUSY));
843
844 #if defined(CONFIG_BT_CTLR_ADV_EXT) && defined(CONFIG_BT_CTLR_PHY_CODED)
845 /* Determine if coded PHY was also enabled, if so, reset the assigned
846 * connection context.
847 */
848 struct ll_scan_set *scan_other =
849 ull_scan_is_enabled_get(SCAN_HANDLE_PHY_CODED);
850 if (scan_other) {
851 if (scan_other == scan) {
852 scan_other = ull_scan_is_enabled_get(SCAN_HANDLE_1M);
853 }
854
855 if (scan_other) {
856 ticker_id_scan = TICKER_ID_SCAN_BASE +
857 ull_scan_handle_get(scan_other);
858 ticker_status = ticker_stop(TICKER_INSTANCE_ID_CTLR,
859 TICKER_USER_ID_ULL_HIGH,
860 ticker_id_scan,
861 ticker_op_stop_scan_other_cb,
862 scan_other);
863 LL_ASSERT((ticker_status == TICKER_STATUS_SUCCESS) ||
864 (ticker_status == TICKER_STATUS_BUSY));
865 }
866 }
867 #endif /* CONFIG_BT_CTLR_ADV_EXT && CONFIG_BT_CTLR_PHY_CODED */
868
869 /* Scanner stop can expire while here in this ISR.
870 * Deferred attempt to stop can fail as it would have
871 * expired, hence ignore failure.
872 */
873 (void)ticker_stop(TICKER_INSTANCE_ID_CTLR, TICKER_USER_ID_ULL_HIGH,
874 TICKER_ID_SCAN_STOP, NULL, NULL);
875
876 /* Start central */
877 ticker_id_conn = TICKER_ID_CONN_BASE + ll_conn_handle_get(conn);
878 ticker_status = ticker_start_us(TICKER_INSTANCE_ID_CTLR,
879 TICKER_USER_ID_ULL_HIGH,
880 ticker_id_conn,
881 ftr->ticks_anchor - ticks_slot_offset,
882 HAL_TICKER_US_TO_TICKS(conn_offset_us),
883 HAL_TICKER_REMAINDER(conn_offset_us),
884 HAL_TICKER_US_TO_TICKS(conn_interval_us),
885 HAL_TICKER_REMAINDER(conn_interval_us),
886 TICKER_NULL_LAZY,
887 (conn->ull.ticks_slot + ticks_slot_overhead),
888 ull_central_ticker_cb, conn,
889 ticker_op_cb, (void *)__LINE__);
890 LL_ASSERT((ticker_status == TICKER_STATUS_SUCCESS) ||
891 (ticker_status == TICKER_STATUS_BUSY));
892
893 #if (CONFIG_BT_CTLR_ULL_HIGH_PRIO == CONFIG_BT_CTLR_ULL_LOW_PRIO)
894 /* enable ticker job, irrespective of disabled in this function so
895 * first connection event can be scheduled as soon as possible.
896 */
897 mayfly_enable(TICKER_USER_ID_ULL_HIGH, TICKER_USER_ID_ULL_LOW, 1);
898 #endif
899 }
900
901 void ull_central_ticker_cb(uint32_t ticks_at_expire, uint32_t ticks_drift,
902 uint32_t remainder, uint16_t lazy, uint8_t force,
903 void *param)
904 {
905 static memq_link_t link;
906 static struct mayfly mfy = {0, 0, &link, NULL, lll_central_prepare};
907 static struct lll_prepare_param p;
908 struct ll_conn *conn;
909 uint32_t err;
910 uint8_t ref;
911
912 DEBUG_RADIO_PREPARE_M(1);
913
914 conn = param;
915
916 /* Check if stopping ticker (on disconnection, race with ticker expiry)
917 */
918 if (unlikely(conn->lll.handle == 0xFFFF)) {
919 DEBUG_RADIO_CLOSE_M(0);
920 return;
921 }
922
923 #if defined(CONFIG_BT_CTLR_CONN_META)
924 conn->common.is_must_expire = (lazy == TICKER_LAZY_MUST_EXPIRE);
925 #endif
926 /* If this is a must-expire callback, LLCP state machine does not need
927 * to know. Will be called with lazy > 0 when scheduled in air.
928 */
929 if (!IS_ENABLED(CONFIG_BT_CTLR_CONN_META) ||
930 (lazy != TICKER_LAZY_MUST_EXPIRE)) {
931 int ret;
932
933 /* Handle any LL Control Procedures */
934 ret = ull_conn_llcp(conn, ticks_at_expire, remainder, lazy);
935 if (ret) {
936 /* NOTE: Under BT_CTLR_LOW_LAT, ULL_LOW context is
937 * disabled inside radio events, hence, abort any
938 * active radio event which will re-enable
939 * ULL_LOW context that permits ticker job to run.
940 */
941 if (IS_ENABLED(CONFIG_BT_CTLR_LOW_LAT) &&
942 (CONFIG_BT_CTLR_LLL_PRIO ==
943 CONFIG_BT_CTLR_ULL_LOW_PRIO)) {
944 ll_radio_state_abort();
945 }
946
947 DEBUG_RADIO_CLOSE_M(0);
948 return;
949 }
950 }
951
952 /* Increment prepare reference count */
953 ref = ull_ref_inc(&conn->ull);
954 LL_ASSERT(ref);
955
956 /* De-mux 2 tx node from FIFO, sufficient to be able to set MD bit */
957 ull_conn_tx_demux(2);
958
959 /* Enqueue towards LLL */
960 ull_conn_tx_lll_enqueue(conn, 2);
961
962 /* Append timing parameters */
963 p.ticks_at_expire = ticks_at_expire;
964 p.remainder = remainder;
965 p.lazy = lazy;
966 p.force = force;
967 p.param = &conn->lll;
968 mfy.param = &p;
969
970 /* Kick LLL prepare */
971 err = mayfly_enqueue(TICKER_USER_ID_ULL_HIGH, TICKER_USER_ID_LLL,
972 0, &mfy);
973 LL_ASSERT(!err);
974
975 /* De-mux remaining tx nodes from FIFO */
976 ull_conn_tx_demux(UINT8_MAX);
977
978 /* Enqueue towards LLL */
979 ull_conn_tx_lll_enqueue(conn, UINT8_MAX);
980
981 DEBUG_RADIO_PREPARE_M(1);
982 }
983
984 uint8_t ull_central_chm_update(void)
985 {
986 uint16_t handle;
987
988 handle = CONFIG_BT_MAX_CONN;
989 while (handle--) {
990 struct ll_conn *conn;
991 uint8_t ret;
992 uint8_t chm[5];
993
994 conn = ll_connected_get(handle);
995 if (!conn || conn->lll.role) {
996 continue;
997 }
998
999 ull_chan_map_get(chm);
1000 ret = ull_cp_chan_map_update(conn, chm);
1001 if (ret) {
1002 return ret;
1003 }
1004 }
1005
1006 return 0;
1007 }
1008
1009 static void ticker_op_stop_scan_cb(uint32_t status, void *param)
1010 {
1011 /* NOTE: Nothing to do here, present here to add debug code if required
1012 */
1013 }
1014
1015 #if defined(CONFIG_BT_CTLR_ADV_EXT) && defined(CONFIG_BT_CTLR_PHY_CODED)
1016 static void ticker_op_stop_scan_other_cb(uint32_t status, void *param)
1017 {
1018 static memq_link_t link;
1019 static struct mayfly mfy = {0, 0, &link, NULL, NULL};
1020 struct ll_scan_set *scan;
1021 struct ull_hdr *hdr;
1022
1023 /* Ignore if race between thread and ULL */
1024 if (status != TICKER_STATUS_SUCCESS) {
1025 /* TODO: detect race */
1026
1027 return;
1028 }
1029
1030 /* NOTE: We are in ULL_LOW which can be pre-empted by ULL_HIGH.
1031 * As we are in the callback after successful stop of the
1032 * ticker, the ULL reference count will not be modified
1033 * further hence it is safe to check and act on either the need
1034 * to call lll_disable or not.
1035 */
1036 scan = param;
1037 hdr = &scan->ull;
1038 mfy.param = &scan->lll;
1039 if (ull_ref_get(hdr)) {
1040 uint32_t ret;
1041
1042 mfy.fp = lll_disable;
1043 ret = mayfly_enqueue(TICKER_USER_ID_ULL_LOW,
1044 TICKER_USER_ID_LLL, 0, &mfy);
1045 LL_ASSERT(!ret);
1046 }
1047 }
1048 #endif /* CONFIG_BT_CTLR_ADV_EXT && CONFIG_BT_CTLR_PHY_CODED */
1049
1050 static void ticker_op_cb(uint32_t status, void *param)
1051 {
1052 ARG_UNUSED(param);
1053
1054 LL_ASSERT(status == TICKER_STATUS_SUCCESS);
1055 }
1056
1057 static inline void conn_release(struct ll_scan_set *scan)
1058 {
1059 struct node_rx_pdu *cc;
1060 struct lll_conn *lll;
1061 struct ll_conn *conn;
1062 memq_link_t *link;
1063
1064 lll = scan->lll.conn;
1065 LL_ASSERT(!lll->link_tx_free);
1066 link = memq_deinit(&lll->memq_tx.head, &lll->memq_tx.tail);
1067 LL_ASSERT(link);
1068 lll->link_tx_free = link;
1069
1070 conn = HDR_LLL2ULL(lll);
1071
1072 cc = (void *)&conn->llcp_terminate.node_rx.rx;
1073 link = cc->hdr.link;
1074 LL_ASSERT(link);
1075
1076 ll_rx_link_release(link);
1077
1078 ll_conn_release(conn);
1079 scan->lll.conn = NULL;
1080 }
1081