Skip to content

Commit 82925a8

Browse files
cvinayakcarlescufi
authored andcommitted
Bluetooth: controller: Avoid sizeof to access ULL/LLL struct member
Avoid using sizeof to access ULL and LLL struct members. Based on the alignment requirements of structures, due to padding between structure members, use of sizeof of previous struct member to access next struct member is incorrect. Continue to use explicitly stored parent pointer to access ULL context. Combine event header and ULL header so that the parent pointer point directly to the combined ULL struct. Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent e395c6c commit 82925a8

37 files changed

+327
-306
lines changed

subsys/bluetooth/controller/ll_sw/lll.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
#define SCAN_INT_UNIT_US 625U
2727
#define CONN_INT_UNIT_US 1250U
2828

29-
#define HDR_ULL(p) ((void *)((uint8_t *)(p) + sizeof(struct evt_hdr)))
30-
#define HDR_ULL2LLL(p) ((struct lll_hdr *)((uint8_t *)(p) + \
31-
sizeof(struct ull_hdr)))
32-
#define HDR_LLL2EVT(p) ((struct evt_hdr *)((struct lll_hdr *)(p))->parent)
33-
3429
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED)
3530
#define XON_BITMASK BIT(31) /* XTAL has been retained from previous prepare */
3631
#endif /* CONFIG_BT_CTLR_XTAL_ADVANCED */
@@ -137,23 +132,29 @@ enum {
137132

138133
#define TICKER_ID_ULL_BASE ((TICKER_ID_LLL_PREEMPT) + 1)
139134

140-
enum ull_status {
141-
ULL_STATUS_SUCCESS,
142-
ULL_STATUS_FAILURE,
143-
ULL_STATUS_BUSY,
144-
};
145-
146-
struct evt_hdr {
147-
uint32_t ticks_xtal_to_start;
148-
uint32_t ticks_active_to_start;
149-
uint32_t ticks_preempt_to_start;
150-
uint32_t ticks_slot;
151-
};
152-
153135
struct ull_hdr {
154136
uint8_t volatile ref; /* Number of ongoing (between Prepare and Done)
155137
* events
156138
*/
139+
140+
/* Event parameters */
141+
/* TODO: The intention is to use the greater of the
142+
* ticks_prepare_to_start or ticks_active_to_start as the prepare
143+
* offset. At the prepare tick generate a software interrupt
144+
* servicable by application as the per role configurable advance
145+
* radio event notification, usable for data acquisitions.
146+
* ticks_preempt_to_start is the per role dynamic preempt offset,
147+
* which shall be based on role's preparation CPU usage
148+
* requirements.
149+
*/
150+
struct {
151+
uint32_t ticks_active_to_start;
152+
uint32_t ticks_prepare_to_start;
153+
uint32_t ticks_preempt_to_start;
154+
uint32_t ticks_slot;
155+
};
156+
157+
/* ULL context disabled callback and its parameter */
157158
void (*disabled_cb)(void *param);
158159
void *disabled_param;
159160
};
@@ -166,6 +167,8 @@ struct lll_hdr {
166167
#endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
167168
};
168169

170+
#define HDR_LLL2ULL(p) (((struct lll_hdr *)(p))->parent)
171+
169172
struct lll_prepare_param {
170173
uint32_t ticks_at_expire;
171174
uint32_t remainder;

subsys/bluetooth/controller/ll_sw/nordic/lll/lll.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ int lll_done(void *param)
325325
#endif /* CONFIG_BT_CTLR_LOW_LAT_ULL_DONE */
326326

327327
if (param) {
328-
ull = HDR_ULL(((struct lll_hdr *)param)->parent);
328+
ull = HDR_LLL2ULL(param);
329329
}
330330

331331
if (IS_ENABLED(CONFIG_BT_CTLR_LOW_LAT) &&
@@ -337,7 +337,7 @@ int lll_done(void *param)
337337

338338
DEBUG_RADIO_CLOSE(0);
339339
} else {
340-
ull = HDR_ULL(((struct lll_hdr *)param)->parent);
340+
ull = HDR_LLL2ULL(param);
341341
}
342342

343343
#if !defined(CONFIG_BT_CTLR_LOW_LAT_ULL_DONE)
@@ -397,21 +397,21 @@ void lll_abort_cb(struct lll_prepare_param *prepare_param, void *param)
397397
lll_done(param);
398398
}
399399

400-
uint32_t lll_evt_offset_get(struct evt_hdr *evt)
400+
uint32_t lll_event_offset_get(struct ull_hdr *ull)
401401
{
402402
if (0) {
403403
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED)
404-
} else if (evt->ticks_xtal_to_start & XON_BITMASK) {
405-
return MAX(evt->ticks_active_to_start,
406-
evt->ticks_preempt_to_start);
404+
} else if (ull->ticks_prepare_to_start & XON_BITMASK) {
405+
return MAX(ull->ticks_active_to_start,
406+
ull->ticks_preempt_to_start);
407407
#endif /* CONFIG_BT_CTLR_XTAL_ADVANCED */
408408
} else {
409-
return MAX(evt->ticks_active_to_start,
410-
evt->ticks_xtal_to_start);
409+
return MAX(ull->ticks_active_to_start,
410+
ull->ticks_prepare_to_start);
411411
}
412412
}
413413

414-
uint32_t lll_preempt_calc(struct evt_hdr *evt, uint8_t ticker_id,
414+
uint32_t lll_preempt_calc(struct ull_hdr *ull, uint8_t ticker_id,
415415
uint32_t ticks_at_event)
416416
{
417417
uint32_t ticks_now;
@@ -740,16 +740,16 @@ static void ticker_start_op_cb(uint32_t status, void *param)
740740
static void preempt_ticker_start(struct lll_prepare_param *prepare_param)
741741
{
742742
uint32_t preempt_anchor;
743-
struct evt_hdr *evt;
743+
struct ull_hdr *ull;
744744
uint32_t preempt_to;
745745
uint32_t ret;
746746

747747
/* Calc the preempt timeout */
748-
evt = HDR_LLL2EVT(prepare_param->param);
748+
ull = HDR_LLL2ULL(prepare_param->param);
749749
preempt_anchor = prepare_param->ticks_at_expire;
750-
preempt_to = MAX(evt->ticks_active_to_start,
751-
evt->ticks_xtal_to_start) -
752-
evt->ticks_preempt_to_start;
750+
preempt_to = MAX(ull->ticks_active_to_start,
751+
ull->ticks_prepare_to_start) -
752+
ull->ticks_preempt_to_start;
753753

754754
/* Setup pre empt timeout */
755755
ret = ticker_start(TICKER_INSTANCE_ID_CTLR,

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_adv.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ static int prepare_cb(struct lll_prepare_param *p)
730730
uint32_t ticks_at_event;
731731
uint32_t ticks_at_start;
732732
struct pdu_adv *pdu;
733-
struct evt_hdr *evt;
733+
struct ull_hdr *ull;
734734
struct lll_adv *lll;
735735
uint32_t remainder;
736736
uint32_t start_us;
@@ -809,8 +809,8 @@ static int prepare_cb(struct lll_prepare_param *p)
809809
}
810810

811811
ticks_at_event = p->ticks_at_expire;
812-
evt = HDR_LLL2EVT(lll);
813-
ticks_at_event += lll_evt_offset_get(evt);
812+
ull = HDR_LLL2ULL(lll);
813+
ticks_at_event += lll_event_offset_get(ull);
814814

815815
ticks_at_start = ticks_at_event;
816816
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -832,7 +832,7 @@ static int prepare_cb(struct lll_prepare_param *p)
832832
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
833833
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
834834
/* check if preempt to start has changed */
835-
if (lll_preempt_calc(evt, (TICKER_ID_ADV_BASE +
835+
if (lll_preempt_calc(ull, (TICKER_ID_ADV_BASE +
836836
ull_adv_lll_handle_get(lll)),
837837
ticks_at_event)) {
838838
radio_isr_set(isr_abort, lll);
@@ -854,10 +854,10 @@ static int prepare_cb(struct lll_prepare_param *p)
854854
#if defined(CONFIG_BT_PERIPHERAL)
855855
static int resume_prepare_cb(struct lll_prepare_param *p)
856856
{
857-
struct evt_hdr *evt;
857+
struct ull_hdr *ull;
858858

859-
evt = HDR_LLL2EVT(p->param);
860-
p->ticks_at_expire = ticker_ticks_now_get() - lll_evt_offset_get(evt);
859+
ull = HDR_LLL2ULL(p->param);
860+
p->ticks_at_expire = ticker_ticks_now_get() - lll_event_offset_get(ull);
861861
p->remainder = 0;
862862
p->lazy = 0;
863863

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_adv_aux.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static int prepare_cb(struct lll_prepare_param *p)
188188
struct pdu_adv_ext_hdr *pri_hdr;
189189
struct lll_adv_aux *lll;
190190
struct lll_adv *lll_adv;
191-
struct evt_hdr *evt;
191+
struct ull_hdr *ull;
192192
uint32_t remainder;
193193
uint32_t start_us;
194194
uint8_t *pri_dptr;
@@ -293,8 +293,8 @@ static int prepare_cb(struct lll_prepare_param *p)
293293
}
294294

295295
ticks_at_event = p->ticks_at_expire;
296-
evt = HDR_LLL2EVT(lll);
297-
ticks_at_event += lll_evt_offset_get(evt);
296+
ull = HDR_LLL2ULL(lll);
297+
ticks_at_event += lll_event_offset_get(ull);
298298

299299
ticks_at_start = ticks_at_event;
300300
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -316,7 +316,7 @@ static int prepare_cb(struct lll_prepare_param *p)
316316
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
317317
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
318318
/* check if preempt to start has changed */
319-
if (lll_preempt_calc(evt, (TICKER_ID_ADV_AUX_BASE +
319+
if (lll_preempt_calc(ull, (TICKER_ID_ADV_AUX_BASE +
320320
ull_adv_aux_lll_handle_get(lll)),
321321
ticks_at_event)) {
322322
radio_isr_set(lll_isr_abort, lll);

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_adv_sync.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int prepare_cb(struct lll_prepare_param *p)
108108
uint16_t event_counter;
109109
uint8_t data_chan_use;
110110
struct pdu_adv *pdu;
111-
struct evt_hdr *evt;
111+
struct ull_hdr *ull;
112112
uint32_t remainder;
113113
uint32_t start_us;
114114
void *extra_data;
@@ -186,8 +186,8 @@ static int prepare_cb(struct lll_prepare_param *p)
186186
}
187187

188188
ticks_at_event = p->ticks_at_expire;
189-
evt = HDR_LLL2EVT(lll);
190-
ticks_at_event += lll_evt_offset_get(evt);
189+
ull = HDR_LLL2ULL(lll);
190+
ticks_at_event += lll_event_offset_get(ull);
191191

192192
ticks_at_start = ticks_at_event;
193193
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -207,7 +207,7 @@ static int prepare_cb(struct lll_prepare_param *p)
207207
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
208208
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
209209
/* check if preempt to start has changed */
210-
if (lll_preempt_calc(evt, (TICKER_ID_ADV_SYNC_BASE +
210+
if (lll_preempt_calc(ull, (TICKER_ID_ADV_SYNC_BASE +
211211
ull_adv_sync_lll_handle_get(lll)),
212212
ticks_at_event)) {
213213
radio_isr_set(lll_isr_abort, lll);

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_internal.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ bool lll_is_done(void *param);
1010
int lll_is_abort_cb(void *next, void *curr, lll_prepare_cb_t *resume_cb);
1111
void lll_abort_cb(struct lll_prepare_param *prepare_param, void *param);
1212

13-
uint32_t lll_evt_offset_get(struct evt_hdr *evt);
14-
uint32_t lll_preempt_calc(struct evt_hdr *evt, uint8_t ticker_id,
15-
uint32_t ticks_at_event);
13+
uint32_t lll_event_offset_get(struct ull_hdr *ull);
14+
uint32_t lll_preempt_calc(struct ull_hdr *ull, uint8_t ticker_id,
15+
uint32_t ticks_at_event);
16+
1617
void lll_chan_set(uint32_t chan);
18+
1719
void lll_isr_tx_status_reset(void);
1820
void lll_isr_rx_status_reset(void);
1921
void lll_isr_status_reset(void);

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_master.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int prepare_cb(struct lll_prepare_param *p)
101101
uint32_t remainder_us;
102102
uint8_t data_chan_use;
103103
struct lll_conn *lll;
104-
struct evt_hdr *evt;
104+
struct ull_hdr *ull;
105105
uint32_t remainder;
106106

107107
DEBUG_RADIO_START_M(1);
@@ -183,8 +183,8 @@ static int prepare_cb(struct lll_prepare_param *p)
183183
#endif /* !CONFIG_BT_CTLR_PHY */
184184

185185
ticks_at_event = p->ticks_at_expire;
186-
evt = HDR_LLL2EVT(lll);
187-
ticks_at_event += lll_evt_offset_get(evt);
186+
ull = HDR_LLL2ULL(lll);
187+
ticks_at_event += lll_event_offset_get(ull);
188188

189189
ticks_at_start = ticks_at_event;
190190
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -215,7 +215,7 @@ static int prepare_cb(struct lll_prepare_param *p)
215215
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
216216
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
217217
/* check if preempt to start has changed */
218-
if (lll_preempt_calc(evt, (TICKER_ID_CONN_BASE + lll->handle),
218+
if (lll_preempt_calc(ull, (TICKER_ID_CONN_BASE + lll->handle),
219219
ticks_at_event)) {
220220
radio_isr_set(lll_isr_abort, lll);
221221
radio_disable();

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_scan.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int prepare_cb(struct lll_prepare_param *p)
128128
struct node_rx_pdu *node_rx;
129129
uint32_t remainder_us;
130130
struct lll_scan *lll;
131-
struct evt_hdr *evt;
131+
struct ull_hdr *ull;
132132
uint32_t remainder;
133133
uint32_t aa;
134134

@@ -218,8 +218,8 @@ static int prepare_cb(struct lll_prepare_param *p)
218218
}
219219

220220
ticks_at_event = p->ticks_at_expire;
221-
evt = HDR_LLL2EVT(lll);
222-
ticks_at_event += lll_evt_offset_get(evt);
221+
ull = HDR_LLL2ULL(lll);
222+
ticks_at_event += lll_event_offset_get(ull);
223223

224224
ticks_at_start = ticks_at_event;
225225
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -248,7 +248,7 @@ static int prepare_cb(struct lll_prepare_param *p)
248248
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
249249
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
250250
/* check if preempt to start has changed */
251-
if (lll_preempt_calc(evt, (TICKER_ID_SCAN_BASE +
251+
if (lll_preempt_calc(ull, (TICKER_ID_SCAN_BASE +
252252
ull_scan_lll_handle_get(lll)),
253253
ticks_at_event)) {
254254
radio_isr_set(isr_abort, lll);
@@ -305,10 +305,10 @@ static int prepare_cb(struct lll_prepare_param *p)
305305

306306
static int resume_prepare_cb(struct lll_prepare_param *p)
307307
{
308-
struct evt_hdr *evt;
308+
struct ull_hdr *ull;
309309

310-
evt = HDR_LLL2EVT(p->param);
311-
p->ticks_at_expire = ticker_ticks_now_get() - lll_evt_offset_get(evt);
310+
ull = HDR_LLL2ULL(p->param);
311+
p->ticks_at_expire = ticker_ticks_now_get() - lll_event_offset_get(ull);
312312
p->remainder = 0;
313313
p->lazy = 0;
314314

@@ -783,7 +783,7 @@ static inline int isr_rx_pdu(struct lll_scan *lll, struct pdu_adv *pdu_adv_rx,
783783
uint32_t conn_interval_us;
784784
uint32_t conn_offset_us;
785785
uint32_t conn_space_us;
786-
struct evt_hdr *evt;
786+
struct ull_hdr *ull;
787787
uint32_t pdu_end_us;
788788
#if defined(CONFIG_BT_CTLR_PRIVACY)
789789
bt_addr_t *lrpa;
@@ -807,8 +807,8 @@ static inline int isr_rx_pdu(struct lll_scan *lll, struct pdu_adv *pdu_adv_rx,
807807
scan_interval_us = lll->interval * SCAN_INT_UNIT_US;
808808
pdu_end_us %= scan_interval_us;
809809
}
810-
evt = HDR_LLL2EVT(lll);
811-
if (pdu_end_us > (HAL_TICKER_TICKS_TO_US(evt->ticks_slot) -
810+
ull = HDR_LLL2ULL(lll);
811+
if (pdu_end_us > (HAL_TICKER_TICKS_TO_US(ull->ticks_slot) -
812812
EVENT_IFS_US - 352 - EVENT_OVERHEAD_START_US -
813813
EVENT_TICKER_RES_MARGIN_US)) {
814814
return -ETIME;

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_scan_aux.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static int prepare_cb(struct lll_prepare_param *p)
9292
struct lll_scan_aux *lll;
9393
uint32_t ticks_at_event;
9494
uint32_t ticks_at_start;
95-
struct evt_hdr *evt;
95+
struct ull_hdr *ull;
9696
uint32_t remainder_us;
9797
uint32_t remainder;
9898
uint32_t hcto;
@@ -139,8 +139,8 @@ static int prepare_cb(struct lll_prepare_param *p)
139139

140140
/* Calculate event timings, coarse and fine */
141141
ticks_at_event = p->ticks_at_expire;
142-
evt = HDR_LLL2EVT(lll);
143-
ticks_at_event += lll_evt_offset_get(evt);
142+
ull = HDR_LLL2ULL(lll);
143+
ticks_at_event += lll_event_offset_get(ull);
144144

145145
ticks_at_start = ticks_at_event;
146146
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -173,7 +173,7 @@ static int prepare_cb(struct lll_prepare_param *p)
173173
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
174174
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
175175
/* check if preempt to start has changed */
176-
if (lll_preempt_calc(evt, (TICKER_ID_SCAN_AUX_BASE +
176+
if (lll_preempt_calc(ull, (TICKER_ID_SCAN_AUX_BASE +
177177
ull_scan_aux_lll_handle_get(lll)),
178178
ticks_at_event)) {
179179
radio_isr_set(isr_done, lll);

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_slave.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int prepare_cb(struct lll_prepare_param *p)
108108
uint32_t remainder_us;
109109
uint8_t data_chan_use;
110110
struct lll_conn *lll;
111-
struct evt_hdr *evt;
111+
struct ull_hdr *ull;
112112
uint32_t remainder;
113113
uint32_t hcto;
114114

@@ -202,8 +202,8 @@ static int prepare_cb(struct lll_prepare_param *p)
202202
#endif /* !CONFIG_BT_CTLR_PHY */
203203

204204
ticks_at_event = p->ticks_at_expire;
205-
evt = HDR_LLL2EVT(lll);
206-
ticks_at_event += lll_evt_offset_get(evt);
205+
ull = HDR_LLL2ULL(lll);
206+
ticks_at_event += lll_event_offset_get(ull);
207207

208208
ticks_at_start = ticks_at_event;
209209
ticks_at_start += HAL_TICKER_US_TO_TICKS(EVENT_OVERHEAD_START_US);
@@ -257,7 +257,7 @@ static int prepare_cb(struct lll_prepare_param *p)
257257
#if defined(CONFIG_BT_CTLR_XTAL_ADVANCED) && \
258258
(EVENT_OVERHEAD_PREEMPT_US <= EVENT_OVERHEAD_PREEMPT_MIN_US)
259259
/* check if preempt to start has changed */
260-
if (lll_preempt_calc(evt, (TICKER_ID_CONN_BASE + lll->handle),
260+
if (lll_preempt_calc(ull, (TICKER_ID_CONN_BASE + lll->handle),
261261
ticks_at_event)) {
262262
radio_isr_set(lll_isr_abort, lll);
263263
radio_disable();

0 commit comments

Comments
 (0)