1 /* SPDX-License-Identifier: ISC */
2 /*
3  * Copyright (c) 2005-2011 Atheros Communications Inc.
4  * Copyright (c) 2011-2016 Qualcomm Atheros, Inc.
5  */
6 
7 #ifndef _HTC_H_
8 #define _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 ath10k;
17 
18 /****************/
19 /* HTC protocol */
20 /****************/
21 
22 /*
23  * HTC - host-target control protocol
24  *
25  * tx packets are generally <htc_hdr><payload>
26  * rx packets are more complex: <htc_hdr><payload><trailer>
27  *
28  * The payload + trailer length is stored in len.
29  * To get payload-only length one needs to payload - trailer_len.
30  *
31  * Trailer contains (possibly) multiple <htc_record>.
32  * Each record is a id-len-value.
33  *
34  * HTC header flags, control_byte0, control_byte1
35  * have different meaning depending whether its tx
36  * or rx.
37  *
38  * Alignment: htc_hdr, payload and trailer are
39  * 4-byte aligned.
40  */
41 
42 #define HTC_HOST_MAX_MSG_PER_RX_BUNDLE        8
43 
44 enum ath10k_htc_tx_flags {
45 	ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE = 0x01,
46 	ATH10K_HTC_FLAG_SEND_BUNDLE        = 0x02
47 };
48 
49 enum ath10k_htc_rx_flags {
50 	ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK = 0x01,
51 	ATH10K_HTC_FLAG_TRAILER_PRESENT = 0x02,
52 	ATH10K_HTC_FLAG_BUNDLE_MASK     = 0xF0
53 };
54 
55 struct ath10k_htc_hdr {
56 	u8 eid; /* @enum ath10k_htc_ep_id */
57 	u8 flags; /* @enum ath10k_htc_tx_flags, ath10k_htc_rx_flags */
58 	__le16 len;
59 	union {
60 		u8 trailer_len; /* for rx */
61 		u8 control_byte0;
62 	} __packed;
63 	union {
64 		u8 seq_no; /* for tx */
65 		u8 control_byte1;
66 	} __packed;
67 	u8 pad0;
68 	u8 pad1;
69 } __packed __aligned(4);
70 
71 enum ath10k_ath10k_htc_msg_id {
72 	ATH10K_HTC_MSG_READY_ID                = 1,
73 	ATH10K_HTC_MSG_CONNECT_SERVICE_ID      = 2,
74 	ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID = 3,
75 	ATH10K_HTC_MSG_SETUP_COMPLETE_ID       = 4,
76 	ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID    = 5,
77 	ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE   = 6
78 };
79 
80 enum ath10k_htc_version {
81 	ATH10K_HTC_VERSION_2P0 = 0x00, /* 2.0 */
82 	ATH10K_HTC_VERSION_2P1 = 0x01, /* 2.1 */
83 };
84 
85 enum ath10k_htc_conn_flags {
86 	ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH    = 0x0,
87 	ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF      = 0x1,
88 	ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS = 0x2,
89 	ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY         = 0x3,
90 #define ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK 0x3
91 	ATH10K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE    = 1 << 2,
92 	ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL = 1 << 3
93 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_MASK 0xFF00
94 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_LSB  8
95 };
96 
97 enum ath10k_htc_conn_svc_status {
98 	ATH10K_HTC_CONN_SVC_STATUS_SUCCESS      = 0,
99 	ATH10K_HTC_CONN_SVC_STATUS_NOT_FOUND    = 1,
100 	ATH10K_HTC_CONN_SVC_STATUS_FAILED       = 2,
101 	ATH10K_HTC_CONN_SVC_STATUS_NO_RESOURCES = 3,
102 	ATH10K_HTC_CONN_SVC_STATUS_NO_MORE_EP   = 4
103 };
104 
105 enum ath10k_htc_setup_complete_flags {
106 	ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN = 1
107 };
108 
109 struct ath10k_ath10k_htc_msg_hdr {
110 	__le16 message_id; /* @enum htc_message_id */
111 } __packed;
112 
113 struct ath10k_htc_unknown {
114 	u8 pad0;
115 	u8 pad1;
116 } __packed;
117 
118 struct ath10k_htc_ready {
119 	__le16 credit_count;
120 	__le16 credit_size;
121 	u8 max_endpoints;
122 	u8 pad0;
123 } __packed;
124 
125 struct ath10k_htc_ready_extended {
126 	struct ath10k_htc_ready base;
127 	u8 htc_version; /* @enum ath10k_htc_version */
128 	u8 max_msgs_per_htc_bundle;
129 	u8 pad0;
130 	u8 pad1;
131 } __packed;
132 
133 struct ath10k_htc_conn_svc {
134 	__le16 service_id;
135 	__le16 flags; /* @enum ath10k_htc_conn_flags */
136 	u8 pad0;
137 	u8 pad1;
138 } __packed;
139 
140 struct ath10k_htc_conn_svc_response {
141 	__le16 service_id;
142 	u8 status; /* @enum ath10k_htc_conn_svc_status */
143 	u8 eid;
144 	__le16 max_msg_size;
145 } __packed;
146 
147 struct ath10k_htc_setup_complete_extended {
148 	u8 pad0;
149 	u8 pad1;
150 	__le32 flags; /* @enum htc_setup_complete_flags */
151 	u8 max_msgs_per_bundled_recv;
152 	u8 pad2;
153 	u8 pad3;
154 	u8 pad4;
155 } __packed;
156 
157 struct ath10k_htc_msg {
158 	struct ath10k_ath10k_htc_msg_hdr hdr;
159 	union {
160 		/* host-to-target */
161 		struct ath10k_htc_conn_svc connect_service;
162 		struct ath10k_htc_ready ready;
163 		struct ath10k_htc_ready_extended ready_ext;
164 		struct ath10k_htc_unknown unknown;
165 		struct ath10k_htc_setup_complete_extended setup_complete_ext;
166 
167 		/* target-to-host */
168 		struct ath10k_htc_conn_svc_response connect_service_response;
169 	};
170 } __packed __aligned(4);
171 
172 enum ath10k_ath10k_htc_record_id {
173 	ATH10K_HTC_RECORD_NULL             = 0,
174 	ATH10K_HTC_RECORD_CREDITS          = 1,
175 	ATH10K_HTC_RECORD_LOOKAHEAD        = 2,
176 	ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE = 3,
177 };
178 
179 struct ath10k_ath10k_htc_record_hdr {
180 	u8 id; /* @enum ath10k_ath10k_htc_record_id */
181 	u8 len;
182 	u8 pad0;
183 	u8 pad1;
184 } __packed;
185 
186 struct ath10k_htc_credit_report {
187 	u8 eid; /* @enum ath10k_htc_ep_id */
188 	u8 credits;
189 	u8 pad0;
190 	u8 pad1;
191 } __packed;
192 
193 struct ath10k_htc_lookahead_report {
194 	u8 pre_valid;
195 	u8 pad0;
196 	u8 pad1;
197 	u8 pad2;
198 	u8 lookahead[4];
199 	u8 post_valid;
200 	u8 pad3;
201 	u8 pad4;
202 	u8 pad5;
203 } __packed;
204 
205 struct ath10k_htc_lookahead_bundle {
206 	u8 lookahead[4];
207 } __packed;
208 
209 struct ath10k_htc_record {
210 	struct ath10k_ath10k_htc_record_hdr hdr;
211 	union {
212 		struct ath10k_htc_credit_report credit_report[0];
213 		struct ath10k_htc_lookahead_report lookahead_report[0];
214 		struct ath10k_htc_lookahead_bundle lookahead_bundle[0];
215 		u8 pauload[0];
216 	};
217 } __packed __aligned(4);
218 
219 /*
220  * note: the trailer offset is dynamic depending
221  * on payload length. this is only a struct layout draft
222  */
223 struct ath10k_htc_frame {
224 	struct ath10k_htc_hdr hdr;
225 	union {
226 		struct ath10k_htc_msg msg;
227 		u8 payload[0];
228 	};
229 	struct ath10k_htc_record trailer[0];
230 } __packed __aligned(4);
231 
232 /*******************/
233 /* Host-side stuff */
234 /*******************/
235 
236 enum ath10k_htc_svc_gid {
237 	ATH10K_HTC_SVC_GRP_RSVD = 0,
238 	ATH10K_HTC_SVC_GRP_WMI = 1,
239 	ATH10K_HTC_SVC_GRP_NMI = 2,
240 	ATH10K_HTC_SVC_GRP_HTT = 3,
241 	ATH10K_LOG_SERVICE_GROUP = 6,
242 
243 	ATH10K_HTC_SVC_GRP_TEST = 254,
244 	ATH10K_HTC_SVC_GRP_LAST = 255,
245 };
246 
247 #define SVC(group, idx) \
248 	(int)(((int)(group) << 8) | (int)(idx))
249 
250 enum ath10k_htc_svc_id {
251 	/* NOTE: service ID of 0x0000 is reserved and should never be used */
252 	ATH10K_HTC_SVC_ID_RESERVED	= 0x0000,
253 	ATH10K_HTC_SVC_ID_UNUSED	= ATH10K_HTC_SVC_ID_RESERVED,
254 
255 	ATH10K_HTC_SVC_ID_RSVD_CTRL	= SVC(ATH10K_HTC_SVC_GRP_RSVD, 1),
256 	ATH10K_HTC_SVC_ID_WMI_CONTROL	= SVC(ATH10K_HTC_SVC_GRP_WMI, 0),
257 	ATH10K_HTC_SVC_ID_WMI_DATA_BE	= SVC(ATH10K_HTC_SVC_GRP_WMI, 1),
258 	ATH10K_HTC_SVC_ID_WMI_DATA_BK	= SVC(ATH10K_HTC_SVC_GRP_WMI, 2),
259 	ATH10K_HTC_SVC_ID_WMI_DATA_VI	= SVC(ATH10K_HTC_SVC_GRP_WMI, 3),
260 	ATH10K_HTC_SVC_ID_WMI_DATA_VO	= SVC(ATH10K_HTC_SVC_GRP_WMI, 4),
261 
262 	ATH10K_HTC_SVC_ID_NMI_CONTROL	= SVC(ATH10K_HTC_SVC_GRP_NMI, 0),
263 	ATH10K_HTC_SVC_ID_NMI_DATA	= SVC(ATH10K_HTC_SVC_GRP_NMI, 1),
264 
265 	ATH10K_HTC_SVC_ID_HTT_DATA_MSG	= SVC(ATH10K_HTC_SVC_GRP_HTT, 0),
266 
267 	ATH10K_HTC_SVC_ID_HTT_DATA2_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 1),
268 	ATH10K_HTC_SVC_ID_HTT_DATA3_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 2),
269 	ATH10K_HTC_SVC_ID_HTT_LOG_MSG = SVC(ATH10K_LOG_SERVICE_GROUP, 0),
270 	/* raw stream service (i.e. flash, tcmd, calibration apps) */
271 	ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS = SVC(ATH10K_HTC_SVC_GRP_TEST, 0),
272 };
273 
274 #undef SVC
275 
276 enum ath10k_htc_ep_id {
277 	ATH10K_HTC_EP_UNUSED = -1,
278 	ATH10K_HTC_EP_0 = 0,
279 	ATH10K_HTC_EP_1 = 1,
280 	ATH10K_HTC_EP_2,
281 	ATH10K_HTC_EP_3,
282 	ATH10K_HTC_EP_4,
283 	ATH10K_HTC_EP_5,
284 	ATH10K_HTC_EP_6,
285 	ATH10K_HTC_EP_7,
286 	ATH10K_HTC_EP_8,
287 	ATH10K_HTC_EP_COUNT,
288 };
289 
290 struct ath10k_htc_ops {
291 	void (*target_send_suspend_complete)(struct ath10k *ar);
292 };
293 
294 struct ath10k_htc_ep_ops {
295 	void (*ep_tx_complete)(struct ath10k *, struct sk_buff *);
296 	void (*ep_rx_complete)(struct ath10k *, struct sk_buff *);
297 	void (*ep_tx_credits)(struct ath10k *);
298 };
299 
300 /* service connection information */
301 struct ath10k_htc_svc_conn_req {
302 	u16 service_id;
303 	struct ath10k_htc_ep_ops ep_ops;
304 	int max_send_queue_depth;
305 };
306 
307 /* service connection response information */
308 struct ath10k_htc_svc_conn_resp {
309 	u8 buffer_len;
310 	u8 actual_len;
311 	enum ath10k_htc_ep_id eid;
312 	unsigned int max_msg_len;
313 	u8 connect_resp_code;
314 };
315 
316 #define ATH10K_NUM_CONTROL_TX_BUFFERS 2
317 #define ATH10K_HTC_MAX_LEN 4096
318 #define ATH10K_HTC_MAX_CTRL_MSG_LEN 256
319 #define ATH10K_HTC_WAIT_TIMEOUT_HZ (1 * HZ)
320 #define ATH10K_HTC_CONTROL_BUFFER_SIZE (ATH10K_HTC_MAX_CTRL_MSG_LEN + \
321 					sizeof(struct ath10k_htc_hdr))
322 #define ATH10K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ)
323 
324 struct ath10k_htc_ep {
325 	struct ath10k_htc *htc;
326 	enum ath10k_htc_ep_id eid;
327 	enum ath10k_htc_svc_id service_id;
328 	struct ath10k_htc_ep_ops ep_ops;
329 
330 	int max_tx_queue_depth;
331 	int max_ep_message_len;
332 	u8 ul_pipe_id;
333 	u8 dl_pipe_id;
334 
335 	u8 seq_no; /* for debugging */
336 	int tx_credits;
337 	bool tx_credit_flow_enabled;
338 };
339 
340 struct ath10k_htc_svc_tx_credits {
341 	u16 service_id;
342 	u8  credit_allocation;
343 };
344 
345 struct ath10k_htc {
346 	struct ath10k *ar;
347 	struct ath10k_htc_ep endpoint[ATH10K_HTC_EP_COUNT];
348 
349 	/* protects endpoints */
350 	spinlock_t tx_lock;
351 
352 	struct ath10k_htc_ops htc_ops;
353 
354 	u8 control_resp_buffer[ATH10K_HTC_MAX_CTRL_MSG_LEN];
355 	int control_resp_len;
356 
357 	struct completion ctl_resp;
358 
359 	int total_transmit_credits;
360 	int target_credit_size;
361 	u8 max_msgs_per_htc_bundle;
362 };
363 
364 int ath10k_htc_init(struct ath10k *ar);
365 int ath10k_htc_wait_target(struct ath10k_htc *htc);
366 int ath10k_htc_start(struct ath10k_htc *htc);
367 int ath10k_htc_connect_service(struct ath10k_htc *htc,
368 			       struct ath10k_htc_svc_conn_req  *conn_req,
369 			       struct ath10k_htc_svc_conn_resp *conn_resp);
370 int ath10k_htc_send(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid,
371 		    struct sk_buff *packet);
372 struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size);
373 void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
374 void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
375 void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
376 				     struct sk_buff *skb);
377 int ath10k_htc_process_trailer(struct ath10k_htc *htc,
378 			       u8 *buffer,
379 			       int length,
380 			       enum ath10k_htc_ep_id src_eid,
381 			       void *next_lookaheads,
382 			       int *next_lookaheads_len);
383 
384 #endif
385