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