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