1 /* SPDX-License-Identifier: BSD-3-Clause-Clear */
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #ifndef ATH12K_HTC_H
8 #define ATH12K_HTC_H
9 
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/bug.h>
13 #include <linux/skbuff.h>
14 #include <linux/timer.h>
15 
16 struct ath12k_base;
17 
18 #define HTC_HDR_ENDPOINTID                       GENMASK(7, 0)
19 #define HTC_HDR_FLAGS                            GENMASK(15, 8)
20 #define HTC_HDR_PAYLOADLEN                       GENMASK(31, 16)
21 #define HTC_HDR_CONTROLBYTES0                    GENMASK(7, 0)
22 #define HTC_HDR_CONTROLBYTES1                    GENMASK(15, 8)
23 #define HTC_HDR_RESERVED                         GENMASK(31, 16)
24 
25 #define HTC_SVC_MSG_SERVICE_ID                   GENMASK(31, 16)
26 #define HTC_SVC_MSG_CONNECTIONFLAGS              GENMASK(15, 0)
27 #define HTC_SVC_MSG_SERVICEMETALENGTH            GENMASK(23, 16)
28 #define HTC_READY_MSG_CREDITCOUNT                GENMASK(31, 16)
29 #define HTC_READY_MSG_CREDITSIZE                 GENMASK(15, 0)
30 #define HTC_READY_MSG_MAXENDPOINTS               GENMASK(23, 16)
31 
32 #define HTC_READY_EX_MSG_HTCVERSION              GENMASK(7, 0)
33 #define HTC_READY_EX_MSG_MAXMSGSPERHTCBUNDLE     GENMASK(15, 8)
34 
35 #define HTC_SVC_RESP_MSG_SERVICEID           GENMASK(31, 16)
36 #define HTC_SVC_RESP_MSG_STATUS              GENMASK(7, 0)
37 #define HTC_SVC_RESP_MSG_ENDPOINTID          GENMASK(15, 8)
38 #define HTC_SVC_RESP_MSG_MAXMSGSIZE          GENMASK(31, 16)
39 #define HTC_SVC_RESP_MSG_SERVICEMETALENGTH   GENMASK(7, 0)
40 
41 #define HTC_MSG_MESSAGEID                        GENMASK(15, 0)
42 #define HTC_SETUP_COMPLETE_EX_MSG_SETUPFLAGS     GENMASK(31, 0)
43 #define HTC_SETUP_COMPLETE_EX_MSG_MAXMSGSPERBUNDLEDRECV      GENMASK(7, 0)
44 #define HTC_SETUP_COMPLETE_EX_MSG_RSVD0          GENMASK(15, 8)
45 #define HTC_SETUP_COMPLETE_EX_MSG_RSVD1          GENMASK(23, 16)
46 #define HTC_SETUP_COMPLETE_EX_MSG_RSVD2          GENMASK(31, 24)
47 
48 enum ath12k_htc_tx_flags {
49 	ATH12K_HTC_FLAG_NEED_CREDIT_UPDATE = 0x01,
50 	ATH12K_HTC_FLAG_SEND_BUNDLE        = 0x02
51 };
52 
53 enum ath12k_htc_rx_flags {
54 	ATH12K_HTC_FLAG_TRAILER_PRESENT = 0x02,
55 	ATH12K_HTC_FLAG_BUNDLE_MASK     = 0xF0
56 };
57 
58 struct ath12k_htc_hdr {
59 	__le32 htc_info;
60 	__le32 ctrl_info;
61 } __packed __aligned(4);
62 
63 enum ath12k_htc_msg_id {
64 	ATH12K_HTC_MSG_READY_ID                = 1,
65 	ATH12K_HTC_MSG_CONNECT_SERVICE_ID      = 2,
66 	ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID = 3,
67 	ATH12K_HTC_MSG_SETUP_COMPLETE_ID       = 4,
68 	ATH12K_HTC_MSG_SETUP_COMPLETE_EX_ID    = 5,
69 	ATH12K_HTC_MSG_SEND_SUSPEND_COMPLETE   = 6,
70 	ATH12K_HTC_MSG_NACK_SUSPEND	       = 7,
71 	ATH12K_HTC_MSG_WAKEUP_FROM_SUSPEND_ID  = 8,
72 };
73 
74 enum ath12k_htc_version {
75 	ATH12K_HTC_VERSION_2P0 = 0x00, /* 2.0 */
76 	ATH12K_HTC_VERSION_2P1 = 0x01, /* 2.1 */
77 };
78 
79 enum ath12k_htc_conn_flag_threshold_level {
80 	ATH12K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH,
81 	ATH12K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF,
82 	ATH12K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS,
83 	ATH12K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY,
84 };
85 
86 #define ATH12K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK	GENMASK(1, 0)
87 #define ATH12K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE	BIT(2)
88 #define ATH12K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL	BIT(3)
89 #define ATH12K_HTC_CONN_FLAGS_RECV_ALLOC		GENMASK(15, 8)
90 
91 enum ath12k_htc_conn_svc_status {
92 	ATH12K_HTC_CONN_SVC_STATUS_SUCCESS      = 0,
93 	ATH12K_HTC_CONN_SVC_STATUS_NOT_FOUND    = 1,
94 	ATH12K_HTC_CONN_SVC_STATUS_FAILED       = 2,
95 	ATH12K_HTC_CONN_SVC_STATUS_NO_RESOURCES = 3,
96 	ATH12K_HTC_CONN_SVC_STATUS_NO_MORE_EP   = 4
97 };
98 
99 struct ath12k_htc_ready {
100 	__le32 id_credit_count;
101 	__le32 size_ep;
102 } __packed;
103 
104 struct ath12k_htc_ready_extended {
105 	struct ath12k_htc_ready base;
106 	__le32 ver_bundle;
107 } __packed;
108 
109 struct ath12k_htc_conn_svc {
110 	__le32 msg_svc_id;
111 	__le32 flags_len;
112 } __packed;
113 
114 struct ath12k_htc_conn_svc_resp {
115 	__le32 msg_svc_id;
116 	__le32 flags_len;
117 	__le32 svc_meta_pad;
118 } __packed;
119 
120 struct ath12k_htc_setup_complete_extended {
121 	__le32 msg_id;
122 	__le32 flags;
123 	__le32 max_msgs_per_bundled_recv;
124 } __packed;
125 
126 struct ath12k_htc_msg {
127 	__le32 msg_svc_id;
128 	__le32 flags_len;
129 } __packed __aligned(4);
130 
131 enum ath12k_htc_record_id {
132 	ATH12K_HTC_RECORD_NULL    = 0,
133 	ATH12K_HTC_RECORD_CREDITS = 1
134 };
135 
136 struct ath12k_htc_record_hdr {
137 	u8 id; /* @enum ath12k_htc_record_id */
138 	u8 len;
139 	u8 pad0;
140 	u8 pad1;
141 } __packed;
142 
143 struct ath12k_htc_credit_report {
144 	u8 eid; /* @enum ath12k_htc_ep_id */
145 	u8 credits;
146 	u8 pad0;
147 	u8 pad1;
148 } __packed;
149 
150 struct ath12k_htc_record {
151 	struct ath12k_htc_record_hdr hdr;
152 	struct ath12k_htc_credit_report credit_report[];
153 } __packed __aligned(4);
154 
155 /* HTC FRAME structure layout draft
156  *
157  * note: the trailer offset is dynamic depending
158  * on payload length. this is only a struct layout draft
159  *
160  *=======================================================
161  *
162  *                 HTC HEADER
163  *
164  *=======================================================
165  *                      |
166  *      HTC message     |        payload
167  *   (variable length)  |    (variable length)
168  *=======================================================
169  *
170  *                  HTC Record
171  *
172  *=======================================================
173  */
174 
175 enum ath12k_htc_svc_gid {
176 	ATH12K_HTC_SVC_GRP_RSVD = 0,
177 	ATH12K_HTC_SVC_GRP_WMI = 1,
178 	ATH12K_HTC_SVC_GRP_NMI = 2,
179 	ATH12K_HTC_SVC_GRP_HTT = 3,
180 	ATH12K_HTC_SVC_GRP_CFG = 4,
181 	ATH12K_HTC_SVC_GRP_IPA = 5,
182 	ATH12K_HTC_SVC_GRP_PKTLOG = 6,
183 
184 	ATH12K_HTC_SVC_GRP_TEST = 254,
185 	ATH12K_HTC_SVC_GRP_LAST = 255,
186 };
187 
188 #define SVC(group, idx) \
189 	(int)(((int)(group) << 8) | (int)(idx))
190 
191 enum ath12k_htc_svc_id {
192 	/* NOTE: service ID of 0x0000 is reserved and should never be used */
193 	ATH12K_HTC_SVC_ID_RESERVED	= 0x0000,
194 	ATH12K_HTC_SVC_ID_UNUSED	= ATH12K_HTC_SVC_ID_RESERVED,
195 
196 	ATH12K_HTC_SVC_ID_RSVD_CTRL	= SVC(ATH12K_HTC_SVC_GRP_RSVD, 1),
197 	ATH12K_HTC_SVC_ID_WMI_CONTROL	= SVC(ATH12K_HTC_SVC_GRP_WMI, 0),
198 	ATH12K_HTC_SVC_ID_WMI_DATA_BE	= SVC(ATH12K_HTC_SVC_GRP_WMI, 1),
199 	ATH12K_HTC_SVC_ID_WMI_DATA_BK	= SVC(ATH12K_HTC_SVC_GRP_WMI, 2),
200 	ATH12K_HTC_SVC_ID_WMI_DATA_VI	= SVC(ATH12K_HTC_SVC_GRP_WMI, 3),
201 	ATH12K_HTC_SVC_ID_WMI_DATA_VO	= SVC(ATH12K_HTC_SVC_GRP_WMI, 4),
202 	ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1 = SVC(ATH12K_HTC_SVC_GRP_WMI, 5),
203 	ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2 = SVC(ATH12K_HTC_SVC_GRP_WMI, 6),
204 	ATH12K_HTC_SVC_ID_WMI_CONTROL_DIAG = SVC(ATH12K_HTC_SVC_GRP_WMI, 7),
205 
206 	ATH12K_HTC_SVC_ID_NMI_CONTROL	= SVC(ATH12K_HTC_SVC_GRP_NMI, 0),
207 	ATH12K_HTC_SVC_ID_NMI_DATA	= SVC(ATH12K_HTC_SVC_GRP_NMI, 1),
208 
209 	ATH12K_HTC_SVC_ID_HTT_DATA_MSG	= SVC(ATH12K_HTC_SVC_GRP_HTT, 0),
210 
211 	/* raw stream service (i.e. flash, tcmd, calibration apps) */
212 	ATH12K_HTC_SVC_ID_TEST_RAW_STREAMS = SVC(ATH12K_HTC_SVC_GRP_TEST, 0),
213 	ATH12K_HTC_SVC_ID_IPA_TX = SVC(ATH12K_HTC_SVC_GRP_IPA, 0),
214 	ATH12K_HTC_SVC_ID_PKT_LOG = SVC(ATH12K_HTC_SVC_GRP_PKTLOG, 0),
215 };
216 
217 #undef SVC
218 
219 enum ath12k_htc_ep_id {
220 	ATH12K_HTC_EP_UNUSED = -1,
221 	ATH12K_HTC_EP_0 = 0,
222 	ATH12K_HTC_EP_1 = 1,
223 	ATH12K_HTC_EP_2,
224 	ATH12K_HTC_EP_3,
225 	ATH12K_HTC_EP_4,
226 	ATH12K_HTC_EP_5,
227 	ATH12K_HTC_EP_6,
228 	ATH12K_HTC_EP_7,
229 	ATH12K_HTC_EP_8,
230 	ATH12K_HTC_EP_COUNT,
231 };
232 
233 struct ath12k_htc_ep_ops {
234 	void (*ep_tx_complete)(struct ath12k_base *ab, struct sk_buff *skb);
235 	void (*ep_rx_complete)(struct ath12k_base *ab, struct sk_buff *skb);
236 	void (*ep_tx_credits)(struct ath12k_base *ab);
237 };
238 
239 /* service connection information */
240 struct ath12k_htc_svc_conn_req {
241 	u16 service_id;
242 	struct ath12k_htc_ep_ops ep_ops;
243 	int max_send_queue_depth;
244 };
245 
246 /* service connection response information */
247 struct ath12k_htc_svc_conn_resp {
248 	u8 buffer_len;
249 	u8 actual_len;
250 	enum ath12k_htc_ep_id eid;
251 	unsigned int max_msg_len;
252 	u8 connect_resp_code;
253 };
254 
255 #define ATH12K_NUM_CONTROL_TX_BUFFERS 2
256 #define ATH12K_HTC_MAX_LEN 4096
257 #define ATH12K_HTC_MAX_CTRL_MSG_LEN 256
258 #define ATH12K_HTC_WAIT_TIMEOUT_HZ (1 * HZ)
259 #define ATH12K_HTC_CONTROL_BUFFER_SIZE (ATH12K_HTC_MAX_CTRL_MSG_LEN + \
260 					sizeof(struct ath12k_htc_hdr))
261 #define ATH12K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ)
262 #define ATH12K_HTC_MAX_SERVICE_ALLOC_ENTRIES 8
263 
264 struct ath12k_htc_ep {
265 	struct ath12k_htc *htc;
266 	enum ath12k_htc_ep_id eid;
267 	enum ath12k_htc_svc_id service_id;
268 	struct ath12k_htc_ep_ops ep_ops;
269 
270 	int max_tx_queue_depth;
271 	int max_ep_message_len;
272 	u8 ul_pipe_id;
273 	u8 dl_pipe_id;
274 
275 	u8 seq_no; /* for debugging */
276 	int tx_credits;
277 	bool tx_credit_flow_enabled;
278 };
279 
280 struct ath12k_htc_svc_tx_credits {
281 	u16 service_id;
282 	u8  credit_allocation;
283 };
284 
285 struct ath12k_htc {
286 	struct ath12k_base *ab;
287 	struct ath12k_htc_ep endpoint[ATH12K_HTC_EP_COUNT];
288 
289 	/* protects endpoints */
290 	spinlock_t tx_lock;
291 
292 	u8 control_resp_buffer[ATH12K_HTC_MAX_CTRL_MSG_LEN];
293 	int control_resp_len;
294 
295 	struct completion ctl_resp;
296 
297 	int total_transmit_credits;
298 	struct ath12k_htc_svc_tx_credits
299 		service_alloc_table[ATH12K_HTC_MAX_SERVICE_ALLOC_ENTRIES];
300 	int target_credit_size;
301 	u8 wmi_ep_count;
302 };
303 
304 int ath12k_htc_init(struct ath12k_base *ar);
305 int ath12k_htc_wait_target(struct ath12k_htc *htc);
306 int ath12k_htc_start(struct ath12k_htc *htc);
307 int ath12k_htc_connect_service(struct ath12k_htc *htc,
308 			       struct ath12k_htc_svc_conn_req  *conn_req,
309 			       struct ath12k_htc_svc_conn_resp *conn_resp);
310 int ath12k_htc_send(struct ath12k_htc *htc, enum ath12k_htc_ep_id eid,
311 		    struct sk_buff *packet);
312 struct sk_buff *ath12k_htc_alloc_skb(struct ath12k_base *ar, int size);
313 void ath12k_htc_rx_completion_handler(struct ath12k_base *ar,
314 				      struct sk_buff *skb);
315 
316 #endif
317