1 /* SPDX-License-Identifier: ISC */
3 * Copyright (c) 2005-2011 Atheros Communications Inc.
4 * Copyright (c) 2011-2016 Qualcomm Atheros, Inc.
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 #include <linux/bitfield.h>
24 * HTC - host-target control protocol
26 * tx packets are generally <htc_hdr><payload>
27 * rx packets are more complex: <htc_hdr><payload><trailer>
29 * The payload + trailer length is stored in len.
30 * To get payload-only length one needs to payload - trailer_len.
32 * Trailer contains (possibly) multiple <htc_record>.
33 * Each record is a id-len-value.
35 * HTC header flags, control_byte0, control_byte1
36 * have different meaning depending whether its tx
39 * Alignment: htc_hdr, payload and trailer are
43 #define HTC_HOST_MAX_MSG_PER_RX_BUNDLE 32
45 enum ath10k_htc_tx_flags {
46 ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE = 0x01,
47 ATH10K_HTC_FLAG_SEND_BUNDLE = 0x02
50 enum ath10k_htc_rx_flags {
51 ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK = 0x01,
52 ATH10K_HTC_FLAG_TRAILER_PRESENT = 0x02,
55 #define ATH10K_HTC_FLAG_BUNDLE_MASK GENMASK(7, 4)
57 /* bits 2-3 are for extra bundle count bits 4-5 */
58 #define ATH10K_HTC_BUNDLE_EXTRA_MASK GENMASK(3, 2)
59 #define ATH10K_HTC_BUNDLE_EXTRA_SHIFT 4
61 static inline unsigned int ath10k_htc_get_bundle_count(u8 max_msgs, u8 flags)
63 unsigned int count, extra_count = 0;
65 count = FIELD_GET(ATH10K_HTC_FLAG_BUNDLE_MASK, flags);
68 extra_count = FIELD_GET(ATH10K_HTC_BUNDLE_EXTRA_MASK, flags) <<
69 ATH10K_HTC_BUNDLE_EXTRA_SHIFT;
71 return count + extra_count;
74 struct ath10k_htc_hdr {
75 u8 eid; /* @enum ath10k_htc_ep_id */
76 u8 flags; /* @enum ath10k_htc_tx_flags, ath10k_htc_rx_flags */
79 u8 trailer_len; /* for rx */
83 u8 seq_no; /* for tx */
94 } __packed __aligned(4);
96 enum ath10k_ath10k_htc_msg_id {
97 ATH10K_HTC_MSG_READY_ID = 1,
98 ATH10K_HTC_MSG_CONNECT_SERVICE_ID = 2,
99 ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID = 3,
100 ATH10K_HTC_MSG_SETUP_COMPLETE_ID = 4,
101 ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID = 5,
102 ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE = 6
105 enum ath10k_htc_version {
106 ATH10K_HTC_VERSION_2P0 = 0x00, /* 2.0 */
107 ATH10K_HTC_VERSION_2P1 = 0x01, /* 2.1 */
110 enum ath10k_htc_conn_flags {
111 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH = 0x0,
112 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF = 0x1,
113 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS = 0x2,
114 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY = 0x3,
115 #define ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK 0x3
116 ATH10K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE = 1 << 2,
117 ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL = 1 << 3
118 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_MASK 0xFF00
119 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_LSB 8
122 #define ATH10K_HTC_MSG_READY_EXT_ALT_DATA_MASK 0xFFF
124 enum ath10k_htc_conn_svc_status {
125 ATH10K_HTC_CONN_SVC_STATUS_SUCCESS = 0,
126 ATH10K_HTC_CONN_SVC_STATUS_NOT_FOUND = 1,
127 ATH10K_HTC_CONN_SVC_STATUS_FAILED = 2,
128 ATH10K_HTC_CONN_SVC_STATUS_NO_RESOURCES = 3,
129 ATH10K_HTC_CONN_SVC_STATUS_NO_MORE_EP = 4
132 #define ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE 32
133 #define ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE 2
134 #define ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE 2
136 enum ath10k_htc_setup_complete_flags {
137 ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN = 1
140 struct ath10k_ath10k_htc_msg_hdr {
141 __le16 message_id; /* @enum htc_message_id */
144 struct ath10k_htc_unknown {
149 struct ath10k_htc_ready {
156 struct ath10k_htc_ready_extended {
157 struct ath10k_htc_ready base;
158 u8 htc_version; /* @enum ath10k_htc_version */
159 u8 max_msgs_per_htc_bundle;
170 struct ath10k_htc_conn_svc {
172 __le16 flags; /* @enum ath10k_htc_conn_flags */
177 struct ath10k_htc_conn_svc_response {
179 u8 status; /* @enum ath10k_htc_conn_svc_status */
184 struct ath10k_htc_setup_complete_extended {
187 __le32 flags; /* @enum htc_setup_complete_flags */
188 u8 max_msgs_per_bundled_recv;
194 struct ath10k_htc_msg {
195 struct ath10k_ath10k_htc_msg_hdr hdr;
198 struct ath10k_htc_conn_svc connect_service;
199 struct ath10k_htc_ready ready;
200 struct ath10k_htc_ready_extended ready_ext;
201 struct ath10k_htc_unknown unknown;
202 struct ath10k_htc_setup_complete_extended setup_complete_ext;
205 struct ath10k_htc_conn_svc_response connect_service_response;
207 } __packed __aligned(4);
209 enum ath10k_ath10k_htc_record_id {
210 ATH10K_HTC_RECORD_NULL = 0,
211 ATH10K_HTC_RECORD_CREDITS = 1,
212 ATH10K_HTC_RECORD_LOOKAHEAD = 2,
213 ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE = 3,
216 struct ath10k_ath10k_htc_record_hdr {
217 u8 id; /* @enum ath10k_ath10k_htc_record_id */
223 struct ath10k_htc_credit_report {
224 u8 eid; /* @enum ath10k_htc_ep_id */
230 struct ath10k_htc_lookahead_report {
242 struct ath10k_htc_lookahead_bundle {
246 struct ath10k_htc_record {
247 struct ath10k_ath10k_htc_record_hdr hdr;
249 struct ath10k_htc_credit_report credit_report[0];
250 struct ath10k_htc_lookahead_report lookahead_report[0];
251 struct ath10k_htc_lookahead_bundle lookahead_bundle[0];
254 } __packed __aligned(4);
257 * note: the trailer offset is dynamic depending
258 * on payload length. this is only a struct layout draft
260 struct ath10k_htc_frame {
261 struct ath10k_htc_hdr hdr;
263 struct ath10k_htc_msg msg;
266 struct ath10k_htc_record trailer[0];
267 } __packed __aligned(4);
269 /*******************/
270 /* Host-side stuff */
271 /*******************/
273 enum ath10k_htc_svc_gid {
274 ATH10K_HTC_SVC_GRP_RSVD = 0,
275 ATH10K_HTC_SVC_GRP_WMI = 1,
276 ATH10K_HTC_SVC_GRP_NMI = 2,
277 ATH10K_HTC_SVC_GRP_HTT = 3,
278 ATH10K_LOG_SERVICE_GROUP = 6,
280 ATH10K_HTC_SVC_GRP_TEST = 254,
281 ATH10K_HTC_SVC_GRP_LAST = 255,
284 #define SVC(group, idx) \
285 (int)(((int)(group) << 8) | (int)(idx))
287 enum ath10k_htc_svc_id {
288 /* NOTE: service ID of 0x0000 is reserved and should never be used */
289 ATH10K_HTC_SVC_ID_RESERVED = 0x0000,
290 ATH10K_HTC_SVC_ID_UNUSED = ATH10K_HTC_SVC_ID_RESERVED,
292 ATH10K_HTC_SVC_ID_RSVD_CTRL = SVC(ATH10K_HTC_SVC_GRP_RSVD, 1),
293 ATH10K_HTC_SVC_ID_WMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_WMI, 0),
294 ATH10K_HTC_SVC_ID_WMI_DATA_BE = SVC(ATH10K_HTC_SVC_GRP_WMI, 1),
295 ATH10K_HTC_SVC_ID_WMI_DATA_BK = SVC(ATH10K_HTC_SVC_GRP_WMI, 2),
296 ATH10K_HTC_SVC_ID_WMI_DATA_VI = SVC(ATH10K_HTC_SVC_GRP_WMI, 3),
297 ATH10K_HTC_SVC_ID_WMI_DATA_VO = SVC(ATH10K_HTC_SVC_GRP_WMI, 4),
299 ATH10K_HTC_SVC_ID_NMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_NMI, 0),
300 ATH10K_HTC_SVC_ID_NMI_DATA = SVC(ATH10K_HTC_SVC_GRP_NMI, 1),
302 ATH10K_HTC_SVC_ID_HTT_DATA_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 0),
304 ATH10K_HTC_SVC_ID_HTT_DATA2_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 1),
305 ATH10K_HTC_SVC_ID_HTT_DATA3_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 2),
306 ATH10K_HTC_SVC_ID_HTT_LOG_MSG = SVC(ATH10K_LOG_SERVICE_GROUP, 0),
307 /* raw stream service (i.e. flash, tcmd, calibration apps) */
308 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS = SVC(ATH10K_HTC_SVC_GRP_TEST, 0),
313 enum ath10k_htc_ep_id {
314 ATH10K_HTC_EP_UNUSED = -1,
327 struct ath10k_htc_ops {
328 void (*target_send_suspend_complete)(struct ath10k *ar);
331 struct ath10k_htc_ep_ops {
332 void (*ep_tx_complete)(struct ath10k *, struct sk_buff *);
333 void (*ep_rx_complete)(struct ath10k *, struct sk_buff *);
334 void (*ep_tx_credits)(struct ath10k *);
337 /* service connection information */
338 struct ath10k_htc_svc_conn_req {
340 struct ath10k_htc_ep_ops ep_ops;
341 int max_send_queue_depth;
344 /* service connection response information */
345 struct ath10k_htc_svc_conn_resp {
348 enum ath10k_htc_ep_id eid;
349 unsigned int max_msg_len;
350 u8 connect_resp_code;
353 #define ATH10K_NUM_CONTROL_TX_BUFFERS 2
354 #define ATH10K_HTC_MAX_LEN 4096
355 #define ATH10K_HTC_MAX_CTRL_MSG_LEN 256
356 #define ATH10K_HTC_WAIT_TIMEOUT_HZ (1 * HZ)
357 #define ATH10K_HTC_CONTROL_BUFFER_SIZE (ATH10K_HTC_MAX_CTRL_MSG_LEN + \
358 sizeof(struct ath10k_htc_hdr))
359 #define ATH10K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ)
361 struct ath10k_htc_ep {
362 struct ath10k_htc *htc;
363 enum ath10k_htc_ep_id eid;
364 enum ath10k_htc_svc_id service_id;
365 struct ath10k_htc_ep_ops ep_ops;
367 int max_tx_queue_depth;
368 int max_ep_message_len;
372 u8 seq_no; /* for debugging */
375 bool tx_credit_flow_enabled;
377 struct sk_buff_head tx_req_head;
378 struct sk_buff_head tx_complete_head;
382 struct ath10k_htc_svc_tx_credits {
384 u8 credit_allocation;
389 struct ath10k_htc_ep endpoint[ATH10K_HTC_EP_COUNT];
391 /* protects endpoints */
394 struct ath10k_htc_ops htc_ops;
396 u8 control_resp_buffer[ATH10K_HTC_MAX_CTRL_MSG_LEN];
397 int control_resp_len;
399 struct completion ctl_resp;
401 int total_transmit_credits;
402 int target_credit_size;
403 u8 max_msgs_per_htc_bundle;
404 int alt_data_credit_size;
407 int ath10k_htc_init(struct ath10k *ar);
408 int ath10k_htc_wait_target(struct ath10k_htc *htc);
409 void ath10k_htc_setup_tx_req(struct ath10k_htc_ep *ep);
410 int ath10k_htc_start(struct ath10k_htc *htc);
411 int ath10k_htc_connect_service(struct ath10k_htc *htc,
412 struct ath10k_htc_svc_conn_req *conn_req,
413 struct ath10k_htc_svc_conn_resp *conn_resp);
414 void ath10k_htc_change_tx_credit_flow(struct ath10k_htc *htc,
415 enum ath10k_htc_ep_id eid,
417 int ath10k_htc_send(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid,
418 struct sk_buff *packet);
419 void ath10k_htc_stop_hl(struct ath10k *ar);
421 int ath10k_htc_send_hl(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid,
422 struct sk_buff *packet);
423 struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size);
424 void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
425 void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb);
426 void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
427 struct sk_buff *skb);
428 int ath10k_htc_process_trailer(struct ath10k_htc *htc,
431 enum ath10k_htc_ep_id src_eid,
432 void *next_lookaheads,
433 int *next_lookaheads_len);