1 /*
2  * Copyright (c) 2017-2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 struct lll_adv_iso_stream {
8 	/* Associated BIG Handle */
9 	uint8_t big_handle;
10 	struct ll_iso_datapath *dp;
11 
12 	/* Transmission queue */
13 	MEMQ_DECLARE(tx);
14 	memq_link_t link_tx;
15 	memq_link_t *link_tx_free;
16 
17 	/* Downstream last packet sequence number */
18 	uint16_t pkt_seq_num;
19 };
20 
21 struct lll_adv_iso {
22 	struct lll_hdr hdr;
23 	struct lll_adv *adv;
24 
25 	uint8_t seed_access_addr[4];
26 	uint8_t base_crc_init[2];
27 	uint16_t latency_prepare;
28 	uint16_t latency_event;
29 	uint16_t data_chan_prn_s;
30 	uint16_t data_chan_remap_idx;
31 	uint8_t  next_chan_use;
32 
33 	uint64_t payload_count:39;
34 	uint64_t enc:1;
35 	uint64_t framing:1;
36 	uint64_t handle:8;
37 	uint64_t cssn:3;
38 	uint32_t iso_interval:12;
39 
40 	uint8_t data_chan_map[PDU_CHANNEL_MAP_SIZE];
41 	uint8_t data_chan_count:6;
42 	uint8_t num_bis:5;
43 	uint8_t bn:3;
44 	uint8_t nse:5;
45 	uint8_t phy:3;
46 
47 	uint32_t sub_interval:20;
48 	uint32_t max_pdu:8;
49 	uint32_t pto:4;
50 
51 	uint32_t bis_spacing:20;
52 	uint32_t max_sdu:8;
53 	uint32_t irc:4;
54 
55 	uint32_t sdu_interval:20;
56 	uint32_t irc_curr:4;
57 	uint32_t ptc_curr:4;
58 	uint32_t ptc:4;
59 
60 	uint8_t bn_curr:3;
61 	uint8_t bis_curr:5;
62 
63 	uint8_t phy_flags:1;
64 
65 	#define CHM_STATE_MASK BIT_MASK(2U)
66 	#define CHM_STATE_REQ  BIT(0U)
67 	#define CHM_STATE_SEND BIT(1U)
68 	uint8_t volatile chm_ack;
69 	uint8_t          chm_req;
70 	uint8_t chm_chan_map[PDU_CHANNEL_MAP_SIZE];
71 	uint8_t chm_chan_count:6;
72 
73 	uint8_t term_req:1;
74 	uint8_t term_ack:1;
75 	uint8_t term_reason;
76 
77 	uint8_t  ctrl_expire;
78 	uint16_t ctrl_instant;
79 
80 	/* Encryption */
81 	uint8_t giv[8];
82 	struct ccm ccm_tx;
83 
84 #if defined(CONFIG_BT_TICKER_EXT_EXPIRE_INFO)
85 	/* contains the offset in ticks from the adv_sync pointing to this ISO */
86 	uint32_t ticks_sync_pdu_offset;
87 	uint16_t iso_lazy;
88 #endif /* CONFIG_BT_TICKER_EXT_EXPIRE_INFO */
89 
90 	uint16_t stream_handle[BT_CTLR_ADV_ISO_STREAM_MAX];
91 };
92 
93 struct lll_adv_sync {
94 	struct lll_hdr hdr;
95 	struct lll_adv *adv;
96 
97 	uint8_t access_addr[4];
98 	uint8_t crc_init[3];
99 
100 	uint16_t latency_prepare;
101 	uint16_t latency_event;
102 	uint16_t event_counter;
103 
104 	uint16_t data_chan_id;
105 	struct {
106 		uint8_t data_chan_map[PDU_CHANNEL_MAP_SIZE];
107 		uint8_t data_chan_count:6;
108 	} chm[DOUBLE_BUFFER_SIZE];
109 	uint8_t  chm_first;
110 	uint8_t  chm_last;
111 	uint16_t chm_instant;
112 
113 	struct lll_adv_pdu data;
114 
115 #if defined(CONFIG_BT_CTLR_ADV_PDU_LINK)
116 	struct pdu_adv *last_pdu;
117 #endif /* CONFIG_BT_CTLR_ADV_PDU_LINK */
118 
119 #if defined(CONFIG_BT_TICKER_EXT_EXPIRE_INFO)
120 	/* contains the offset in us from adv_aux pointing to this sync */
121 	uint32_t us_adv_sync_pdu_offset;
122 	uint16_t sync_lazy;
123 #endif /* CONFIG_BT_TICKER_EXT_EXPIRE_INFO */
124 
125 #if defined(CONFIG_BT_CTLR_ADV_ISO)
126 	struct lll_adv_iso *iso;
127 	uint8_t    volatile iso_chm_done_req;
128 	uint8_t             iso_chm_done_ack;
129 #endif /* CONFIG_BT_CTLR_ADV_ISO */
130 
131 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
132 	/* This flag is used only by LLL. It holds information if CTE
133 	 * transmission was started by LLL.
134 	 */
135 	uint8_t cte_started:1;
136 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
137 };
138 
139 struct lll_adv_aux {
140 	struct lll_hdr hdr;
141 	struct lll_adv *adv;
142 
143 	/* Implementation defined radio event counter to calculate auxiliary
144 	 * PDU channel index.
145 	 */
146 	uint16_t data_chan_counter;
147 
148 	/* Store used by primary channel PDU event to fill the
149 	 * auxiliary offset to this auxiliary PDU event.
150 	 */
151 	uint32_t ticks_pri_pdu_offset;
152 	uint32_t us_pri_pdu_offset;
153 
154 	struct lll_adv_pdu data;
155 #if defined(CONFIG_BT_CTLR_ADV_PDU_LINK)
156 	struct pdu_adv     *last_pdu;
157 #endif /* CONFIG_BT_CTLR_ADV_PDU_LINK */
158 };
159 
160 struct lll_adv {
161 	struct lll_hdr hdr;
162 
163 #if defined(CONFIG_BT_PERIPHERAL)
164 	/* NOTE: conn context SHALL be after lll_hdr,
165 	 *       check ull_conn_setup how it access the connection LLL
166 	 *       context.
167 	 */
168 	struct lll_conn *conn;
169 	uint8_t is_hdcd:1;
170 #endif /* CONFIG_BT_PERIPHERAL */
171 
172 	uint8_t chan_map:3;
173 	uint8_t chan_map_curr:3;
174 	uint8_t filter_policy:2;
175 
176 #if defined(CONFIG_BT_CTLR_ADV_EXT)
177 	uint8_t phy_p:3;
178 	uint8_t phy_s:3;
179 	uint8_t phy_flags:1;
180 #endif /* CONFIG_BT_CTLR_ADV_EXT */
181 
182 #if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
183 	uint8_t scan_req_notify:1;
184 #endif
185 
186 #if defined(CONFIG_BT_HCI_MESH_EXT)
187 	uint8_t is_mesh:1;
188 #endif /* CONFIG_BT_HCI_MESH_EXT */
189 
190 #if defined(CONFIG_BT_CTLR_PRIVACY)
191 	uint8_t  rl_idx;
192 #endif /* CONFIG_BT_CTLR_PRIVACY */
193 
194 	struct lll_adv_pdu adv_data;
195 	struct lll_adv_pdu scan_rsp;
196 
197 #if defined(CONFIG_BT_CTLR_ADV_EXT)
198 	struct node_rx_pdu *node_rx_adv_term;
199 	struct lll_adv_aux *aux;
200 
201 #if defined(CONFIG_BT_CTLR_ADV_PERIODIC)
202 	struct lll_adv_sync *sync;
203 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
204 #endif /* CONFIG_BT_CTLR_ADV_EXT */
205 
206 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
207 	int8_t tx_pwr_lvl;
208 #endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */
209 };
210 
211 int lll_adv_init(void);
212 int lll_adv_reset(void);
213 void lll_adv_prepare(void *param);
214 
215 extern uint16_t ull_adv_lll_handle_get(struct lll_adv *lll);
216