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 */
88 } __packed
__aligned(4);
90 enum ath10k_ath10k_htc_msg_id
{
91 ATH10K_HTC_MSG_READY_ID
= 1,
92 ATH10K_HTC_MSG_CONNECT_SERVICE_ID
= 2,
93 ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID
= 3,
94 ATH10K_HTC_MSG_SETUP_COMPLETE_ID
= 4,
95 ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID
= 5,
96 ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE
= 6
99 enum ath10k_htc_version
{
100 ATH10K_HTC_VERSION_2P0
= 0x00, /* 2.0 */
101 ATH10K_HTC_VERSION_2P1
= 0x01, /* 2.1 */
104 enum ath10k_htc_conn_flags
{
105 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH
= 0x0,
106 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF
= 0x1,
107 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS
= 0x2,
108 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY
= 0x3,
109 #define ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK 0x3
110 ATH10K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE
= 1 << 2,
111 ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL
= 1 << 3
112 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_MASK 0xFF00
113 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_LSB 8
116 enum ath10k_htc_conn_svc_status
{
117 ATH10K_HTC_CONN_SVC_STATUS_SUCCESS
= 0,
118 ATH10K_HTC_CONN_SVC_STATUS_NOT_FOUND
= 1,
119 ATH10K_HTC_CONN_SVC_STATUS_FAILED
= 2,
120 ATH10K_HTC_CONN_SVC_STATUS_NO_RESOURCES
= 3,
121 ATH10K_HTC_CONN_SVC_STATUS_NO_MORE_EP
= 4
124 enum ath10k_htc_setup_complete_flags
{
125 ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN
= 1
128 struct ath10k_ath10k_htc_msg_hdr
{
129 __le16 message_id
; /* @enum htc_message_id */
132 struct ath10k_htc_unknown
{
137 struct ath10k_htc_ready
{
144 struct ath10k_htc_ready_extended
{
145 struct ath10k_htc_ready base
;
146 u8 htc_version
; /* @enum ath10k_htc_version */
147 u8 max_msgs_per_htc_bundle
;
152 struct ath10k_htc_conn_svc
{
154 __le16 flags
; /* @enum ath10k_htc_conn_flags */
159 struct ath10k_htc_conn_svc_response
{
161 u8 status
; /* @enum ath10k_htc_conn_svc_status */
166 struct ath10k_htc_setup_complete_extended
{
169 __le32 flags
; /* @enum htc_setup_complete_flags */
170 u8 max_msgs_per_bundled_recv
;
176 struct ath10k_htc_msg
{
177 struct ath10k_ath10k_htc_msg_hdr hdr
;
180 struct ath10k_htc_conn_svc connect_service
;
181 struct ath10k_htc_ready ready
;
182 struct ath10k_htc_ready_extended ready_ext
;
183 struct ath10k_htc_unknown unknown
;
184 struct ath10k_htc_setup_complete_extended setup_complete_ext
;
187 struct ath10k_htc_conn_svc_response connect_service_response
;
189 } __packed
__aligned(4);
191 enum ath10k_ath10k_htc_record_id
{
192 ATH10K_HTC_RECORD_NULL
= 0,
193 ATH10K_HTC_RECORD_CREDITS
= 1,
194 ATH10K_HTC_RECORD_LOOKAHEAD
= 2,
195 ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE
= 3,
198 struct ath10k_ath10k_htc_record_hdr
{
199 u8 id
; /* @enum ath10k_ath10k_htc_record_id */
205 struct ath10k_htc_credit_report
{
206 u8 eid
; /* @enum ath10k_htc_ep_id */
212 struct ath10k_htc_lookahead_report
{
224 struct ath10k_htc_lookahead_bundle
{
228 struct ath10k_htc_record
{
229 struct ath10k_ath10k_htc_record_hdr hdr
;
231 struct ath10k_htc_credit_report credit_report
[0];
232 struct ath10k_htc_lookahead_report lookahead_report
[0];
233 struct ath10k_htc_lookahead_bundle lookahead_bundle
[0];
236 } __packed
__aligned(4);
239 * note: the trailer offset is dynamic depending
240 * on payload length. this is only a struct layout draft
242 struct ath10k_htc_frame
{
243 struct ath10k_htc_hdr hdr
;
245 struct ath10k_htc_msg msg
;
248 struct ath10k_htc_record trailer
[0];
249 } __packed
__aligned(4);
251 /*******************/
252 /* Host-side stuff */
253 /*******************/
255 enum ath10k_htc_svc_gid
{
256 ATH10K_HTC_SVC_GRP_RSVD
= 0,
257 ATH10K_HTC_SVC_GRP_WMI
= 1,
258 ATH10K_HTC_SVC_GRP_NMI
= 2,
259 ATH10K_HTC_SVC_GRP_HTT
= 3,
260 ATH10K_LOG_SERVICE_GROUP
= 6,
262 ATH10K_HTC_SVC_GRP_TEST
= 254,
263 ATH10K_HTC_SVC_GRP_LAST
= 255,
266 #define SVC(group, idx) \
267 (int)(((int)(group) << 8) | (int)(idx))
269 enum ath10k_htc_svc_id
{
270 /* NOTE: service ID of 0x0000 is reserved and should never be used */
271 ATH10K_HTC_SVC_ID_RESERVED
= 0x0000,
272 ATH10K_HTC_SVC_ID_UNUSED
= ATH10K_HTC_SVC_ID_RESERVED
,
274 ATH10K_HTC_SVC_ID_RSVD_CTRL
= SVC(ATH10K_HTC_SVC_GRP_RSVD
, 1),
275 ATH10K_HTC_SVC_ID_WMI_CONTROL
= SVC(ATH10K_HTC_SVC_GRP_WMI
, 0),
276 ATH10K_HTC_SVC_ID_WMI_DATA_BE
= SVC(ATH10K_HTC_SVC_GRP_WMI
, 1),
277 ATH10K_HTC_SVC_ID_WMI_DATA_BK
= SVC(ATH10K_HTC_SVC_GRP_WMI
, 2),
278 ATH10K_HTC_SVC_ID_WMI_DATA_VI
= SVC(ATH10K_HTC_SVC_GRP_WMI
, 3),
279 ATH10K_HTC_SVC_ID_WMI_DATA_VO
= SVC(ATH10K_HTC_SVC_GRP_WMI
, 4),
281 ATH10K_HTC_SVC_ID_NMI_CONTROL
= SVC(ATH10K_HTC_SVC_GRP_NMI
, 0),
282 ATH10K_HTC_SVC_ID_NMI_DATA
= SVC(ATH10K_HTC_SVC_GRP_NMI
, 1),
284 ATH10K_HTC_SVC_ID_HTT_DATA_MSG
= SVC(ATH10K_HTC_SVC_GRP_HTT
, 0),
286 ATH10K_HTC_SVC_ID_HTT_DATA2_MSG
= SVC(ATH10K_HTC_SVC_GRP_HTT
, 1),
287 ATH10K_HTC_SVC_ID_HTT_DATA3_MSG
= SVC(ATH10K_HTC_SVC_GRP_HTT
, 2),
288 ATH10K_HTC_SVC_ID_HTT_LOG_MSG
= SVC(ATH10K_LOG_SERVICE_GROUP
, 0),
289 /* raw stream service (i.e. flash, tcmd, calibration apps) */
290 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS
= SVC(ATH10K_HTC_SVC_GRP_TEST
, 0),
295 enum ath10k_htc_ep_id
{
296 ATH10K_HTC_EP_UNUSED
= -1,
309 struct ath10k_htc_ops
{
310 void (*target_send_suspend_complete
)(struct ath10k
*ar
);
313 struct ath10k_htc_ep_ops
{
314 void (*ep_tx_complete
)(struct ath10k
*, struct sk_buff
*);
315 void (*ep_rx_complete
)(struct ath10k
*, struct sk_buff
*);
316 void (*ep_tx_credits
)(struct ath10k
*);
319 /* service connection information */
320 struct ath10k_htc_svc_conn_req
{
322 struct ath10k_htc_ep_ops ep_ops
;
323 int max_send_queue_depth
;
326 /* service connection response information */
327 struct ath10k_htc_svc_conn_resp
{
330 enum ath10k_htc_ep_id eid
;
331 unsigned int max_msg_len
;
332 u8 connect_resp_code
;
335 #define ATH10K_NUM_CONTROL_TX_BUFFERS 2
336 #define ATH10K_HTC_MAX_LEN 4096
337 #define ATH10K_HTC_MAX_CTRL_MSG_LEN 256
338 #define ATH10K_HTC_WAIT_TIMEOUT_HZ (1 * HZ)
339 #define ATH10K_HTC_CONTROL_BUFFER_SIZE (ATH10K_HTC_MAX_CTRL_MSG_LEN + \
340 sizeof(struct ath10k_htc_hdr))
341 #define ATH10K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ)
343 struct ath10k_htc_ep
{
344 struct ath10k_htc
*htc
;
345 enum ath10k_htc_ep_id eid
;
346 enum ath10k_htc_svc_id service_id
;
347 struct ath10k_htc_ep_ops ep_ops
;
349 int max_tx_queue_depth
;
350 int max_ep_message_len
;
354 u8 seq_no
; /* for debugging */
356 bool tx_credit_flow_enabled
;
359 struct ath10k_htc_svc_tx_credits
{
361 u8 credit_allocation
;
366 struct ath10k_htc_ep endpoint
[ATH10K_HTC_EP_COUNT
];
368 /* protects endpoints */
371 struct ath10k_htc_ops htc_ops
;
373 u8 control_resp_buffer
[ATH10K_HTC_MAX_CTRL_MSG_LEN
];
374 int control_resp_len
;
376 struct completion ctl_resp
;
378 int total_transmit_credits
;
379 int target_credit_size
;
380 u8 max_msgs_per_htc_bundle
;
383 int ath10k_htc_init(struct ath10k
*ar
);
384 int ath10k_htc_wait_target(struct ath10k_htc
*htc
);
385 int ath10k_htc_start(struct ath10k_htc
*htc
);
386 int ath10k_htc_connect_service(struct ath10k_htc
*htc
,
387 struct ath10k_htc_svc_conn_req
*conn_req
,
388 struct ath10k_htc_svc_conn_resp
*conn_resp
);
389 int ath10k_htc_send(struct ath10k_htc
*htc
, enum ath10k_htc_ep_id eid
,
390 struct sk_buff
*packet
);
391 struct sk_buff
*ath10k_htc_alloc_skb(struct ath10k
*ar
, int size
);
392 void ath10k_htc_tx_completion_handler(struct ath10k
*ar
, struct sk_buff
*skb
);
393 void ath10k_htc_rx_completion_handler(struct ath10k
*ar
, struct sk_buff
*skb
);
394 void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep
*ep
,
395 struct sk_buff
*skb
);
396 int ath10k_htc_process_trailer(struct ath10k_htc
*htc
,
399 enum ath10k_htc_ep_id src_eid
,
400 void *next_lookaheads
,
401 int *next_lookaheads_len
);