1 /*
2  * Copyright (c) 2021 Demant
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define LLL_CIS_FLUSH_NONE      0
8 #define LLL_CIS_FLUSH_PENDING   1
9 #define LLL_CIS_FLUSH_COMPLETE  2
10 
11 struct lll_conn_iso_stream_rxtx {
12 	uint64_t payload_count:39; /* cisPayloadCounter */
13 	uint64_t phy_flags:1;      /* S2 or S8 coding scheme */
14 	uint64_t max_pdu:8;        /* Maximum PDU size */
15 	uint64_t ft:8;             /* Flush timeout (FT) */
16 	uint64_t bn:4;             /* Burst number (BN) */
17 	uint64_t phy:3;            /* PHY */
18 	uint64_t rfu0:1;
19 
20 	uint8_t bn_curr:4;        /* Current burst number */
21 	uint8_t rfu1:4;
22 
23 #if defined(CONFIG_BT_CTLR_LE_ENC)
24 	struct ccm ccm;
25 #endif /* CONFIG_BT_CTLR_LE_ENC */
26 };
27 
28 struct lll_conn_iso_stream {
29 	uint16_t acl_handle;        /* ACL connection handle (for encryption,
30 				     * channel map, crc init)
31 				     */
32 	uint16_t handle;            /* CIS handle */
33 
34 	/* Connection parameters */
35 	uint8_t  access_addr[4];    /* Access address */
36 	uint32_t offset;            /* Offset of CIS from start of CIG in us */
37 	uint32_t sub_interval;      /* Interval between subevents in us */
38 	uint8_t  nse:5;             /* Number of subevents */
39 	struct lll_conn_iso_stream_rxtx rx; /* RX parameters */
40 	struct lll_conn_iso_stream_rxtx tx; /* TX parameters */
41 
42 	/* Event and payload counters */
43 	uint64_t event_count:39;    /* cisEventCount */
44 
45 	/* Acknowledgment and flow control */
46 	uint8_t sn:1;               /* Sequence number */
47 	uint8_t nesn:1;             /* Next expected sequence number */
48 	uint8_t cie:1;              /* Close isochronous event */
49 	uint8_t npi:1;              /* 1 if CIS LLL has Tx-ed Null PDU Indicator */
50 	uint8_t flush:2;            /* See states LLL_CIS_FLUSH_XXX */
51 	uint8_t active:1;           /* 1 if CIS LLL is active */
52 	uint8_t datapath_ready_rx:1;/* 1 if datapath for RX is ready */
53 
54 #if !defined(CONFIG_BT_CTLR_JIT_SCHEDULING)
55 	/* Lazy at CIS active. Number of previously skipped CIG events that is
56 	 * determined when CIS is made active and subtracted from total CIG
57 	 * events that where skipped when this CIS gets to use radio for the
58 	 * first time.
59 	 */
60 	uint16_t lazy_active;
61 #endif /* !CONFIG_BT_CTLR_JIT_SCHEDULING */
62 
63 	/* Resumption information */
64 	uint8_t next_subevent;      /* Next subevent to schedule */
65 
66 	/* Transmission queue */
67 	MEMQ_DECLARE(tx);
68 	memq_link_t link_tx;
69 	memq_link_t *link_tx_free;
70 };
71 
72 #define LLL_CONN_ISO_EVENT_COUNT_MAX BIT64_MASK(39)
73 
74 struct lll_conn_iso_group {
75 	struct lll_hdr hdr;
76 
77 	uint16_t handle;      /* CIG handle (internal) */
78 
79 	/* Resumption information */
80 	uint16_t resume_cis;  /* CIS handle to schedule at resume */
81 
82 	/* ISO group information */
83 	uint32_t num_cis:5;   /* Number of CISes in this CIG */
84 	uint32_t role:1;      /* 0: CENTRAL, 1: PERIPHERAL*/
85 	uint32_t paused:1;    /* 1: CIG is paused */
86 	uint32_t rfu0:1;
87 
88 	/* ISO interval to calculate timestamp under FT > 1,
89 	 * maximum ISO interval of 4 seconds can be represented in 22-bits.
90 	 */
91 	uint32_t iso_interval_us:22;
92 	uint32_t rfu1:2;
93 
94 	/* Accumulates LLL prepare callback latencies */
95 	uint16_t latency_prepare;
96 	uint16_t latency_event;
97 
98 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
99 	/* Window widening. Relies on vendor specific conversion macros, e.g.
100 	 * EVENT_US_FRAC_TO_TICKS().
101 	 */
102 	uint32_t window_widening_periodic_us_frac; /* Widening in us fractions
103 						    * per ISO interval.
104 						    */
105 	uint32_t window_widening_prepare_us_frac;  /* Widening in us fractions
106 						    * for active prepare.
107 						    */
108 	uint32_t window_widening_event_us_frac;    /* Accumulated widening in
109 						    * us fractions for active
110 						    * event.
111 						    */
112 	uint32_t window_widening_max_us;	   /* Maximum widening in us */
113 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
114 };
115 
116 int lll_conn_iso_init(void);
117 int lll_conn_iso_reset(void);
118 void lll_conn_iso_done(struct lll_conn_iso_group *cig, uint32_t trx_performed,
119 		       uint16_t prog_to_anchor_us, uint8_t mic_state);
120 void lll_conn_iso_flush(uint16_t handle, struct lll_conn_iso_stream *lll);
121 
122 extern struct lll_conn_iso_stream *
123 ull_conn_iso_lll_stream_get_by_group(struct lll_conn_iso_group *cig_lll,
124 				     uint16_t *handle_iter);
125 extern struct lll_conn_iso_group *
126 ull_conn_iso_lll_group_get_by_stream(struct lll_conn_iso_stream *cis_lll);
127 extern struct lll_conn_iso_stream *ull_conn_iso_lll_stream_get(uint16_t handle);
128 extern void
129 ull_conn_iso_lll_cis_established(struct lll_conn_iso_stream *cis_lll);
130 extern void ll_iso_rx_put(memq_link_t *link, void *rx);
131 extern void ll_rx_sched(void);
132