1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2005-2011 Atheros Communications Inc.
4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
15 static void ath10k_htc_control_tx_complete(struct ath10k
*ar
,
21 static struct sk_buff
*ath10k_htc_build_tx_ctrl_skb(void *ar
)
24 struct ath10k_skb_cb
*skb_cb
;
26 skb
= dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE
);
30 skb_reserve(skb
, 20); /* FIXME: why 20 bytes? */
31 WARN_ONCE((unsigned long)skb
->data
& 3, "unaligned skb");
33 skb_cb
= ATH10K_SKB_CB(skb
);
34 memset(skb_cb
, 0, sizeof(*skb_cb
));
36 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "%s: skb %pK\n", __func__
, skb
);
40 static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc
*htc
,
43 struct ath10k_skb_cb
*skb_cb
= ATH10K_SKB_CB(skb
);
45 if (htc
->ar
->bus_param
.dev_type
!= ATH10K_DEV_TYPE_HL
)
46 dma_unmap_single(htc
->ar
->dev
, skb_cb
->paddr
, skb
->len
, DMA_TO_DEVICE
);
47 skb_pull(skb
, sizeof(struct ath10k_htc_hdr
));
50 void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep
*ep
,
53 struct ath10k
*ar
= ep
->htc
->ar
;
54 struct ath10k_htc_hdr
*hdr
;
56 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "%s: ep %d skb %pK\n", __func__
,
59 hdr
= (struct ath10k_htc_hdr
*)skb
->data
;
60 ath10k_htc_restore_tx_skb(ep
->htc
, skb
);
62 if (!ep
->ep_ops
.ep_tx_complete
) {
63 ath10k_warn(ar
, "no tx handler for eid %d\n", ep
->eid
);
64 dev_kfree_skb_any(skb
);
68 if (hdr
->flags
& ATH10K_HTC_FLAG_SEND_BUNDLE
) {
69 dev_kfree_skb_any(skb
);
73 ep
->ep_ops
.ep_tx_complete(ep
->htc
->ar
, skb
);
75 EXPORT_SYMBOL(ath10k_htc_notify_tx_completion
);
77 static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep
*ep
,
80 struct ath10k_htc_hdr
*hdr
;
82 hdr
= (struct ath10k_htc_hdr
*)skb
->data
;
83 memset(hdr
, 0, sizeof(struct ath10k_htc_hdr
));
86 hdr
->len
= __cpu_to_le16(skb
->len
- sizeof(*hdr
));
88 if (ep
->tx_credit_flow_enabled
&& !ep
->bundle_tx
)
89 hdr
->flags
|= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE
;
91 spin_lock_bh(&ep
->htc
->tx_lock
);
92 hdr
->seq_no
= ep
->seq_no
++;
93 spin_unlock_bh(&ep
->htc
->tx_lock
);
96 static int ath10k_htc_consume_credit(struct ath10k_htc_ep
*ep
,
100 struct ath10k_htc
*htc
= ep
->htc
;
101 struct ath10k
*ar
= htc
->ar
;
102 enum ath10k_htc_ep_id eid
= ep
->eid
;
103 int credits
, ret
= 0;
105 if (!ep
->tx_credit_flow_enabled
)
108 credits
= DIV_ROUND_UP(len
, ep
->tx_credit_size
);
109 spin_lock_bh(&htc
->tx_lock
);
111 if (ep
->tx_credits
< credits
) {
112 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
113 "htc insufficient credits ep %d required %d available %d consume %d\n",
114 eid
, credits
, ep
->tx_credits
, consume
);
120 ep
->tx_credits
-= credits
;
121 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
122 "htc ep %d consumed %d credits total %d\n",
123 eid
, credits
, ep
->tx_credits
);
127 spin_unlock_bh(&htc
->tx_lock
);
131 static void ath10k_htc_release_credit(struct ath10k_htc_ep
*ep
, unsigned int len
)
133 struct ath10k_htc
*htc
= ep
->htc
;
134 struct ath10k
*ar
= htc
->ar
;
135 enum ath10k_htc_ep_id eid
= ep
->eid
;
138 if (!ep
->tx_credit_flow_enabled
)
141 credits
= DIV_ROUND_UP(len
, ep
->tx_credit_size
);
142 spin_lock_bh(&htc
->tx_lock
);
143 ep
->tx_credits
+= credits
;
144 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
145 "htc ep %d reverted %d credits back total %d\n",
146 eid
, credits
, ep
->tx_credits
);
147 spin_unlock_bh(&htc
->tx_lock
);
149 if (ep
->ep_ops
.ep_tx_credits
)
150 ep
->ep_ops
.ep_tx_credits(htc
->ar
);
153 int ath10k_htc_send(struct ath10k_htc
*htc
,
154 enum ath10k_htc_ep_id eid
,
157 struct ath10k
*ar
= htc
->ar
;
158 struct ath10k_htc_ep
*ep
= &htc
->endpoint
[eid
];
159 struct ath10k_skb_cb
*skb_cb
= ATH10K_SKB_CB(skb
);
160 struct ath10k_hif_sg_item sg_item
;
161 struct device
*dev
= htc
->ar
->dev
;
163 unsigned int skb_len
;
165 if (htc
->ar
->state
== ATH10K_STATE_WEDGED
)
168 if (eid
>= ATH10K_HTC_EP_COUNT
) {
169 ath10k_warn(ar
, "Invalid endpoint id: %d\n", eid
);
173 skb_push(skb
, sizeof(struct ath10k_htc_hdr
));
176 ret
= ath10k_htc_consume_credit(ep
, skb_len
, true);
180 ath10k_htc_prepare_tx_skb(ep
, skb
);
183 if (ar
->bus_param
.dev_type
!= ATH10K_DEV_TYPE_HL
) {
184 skb_cb
->paddr
= dma_map_single(dev
, skb
->data
, skb
->len
,
186 ret
= dma_mapping_error(dev
, skb_cb
->paddr
);
193 sg_item
.transfer_id
= ep
->eid
;
194 sg_item
.transfer_context
= skb
;
195 sg_item
.vaddr
= skb
->data
;
196 sg_item
.paddr
= skb_cb
->paddr
;
197 sg_item
.len
= skb
->len
;
199 ret
= ath10k_hif_tx_sg(htc
->ar
, ep
->ul_pipe_id
, &sg_item
, 1);
206 if (ar
->bus_param
.dev_type
!= ATH10K_DEV_TYPE_HL
)
207 dma_unmap_single(dev
, skb_cb
->paddr
, skb
->len
, DMA_TO_DEVICE
);
209 ath10k_htc_release_credit(ep
, skb_len
);
211 skb_pull(skb
, sizeof(struct ath10k_htc_hdr
));
215 void ath10k_htc_tx_completion_handler(struct ath10k
*ar
, struct sk_buff
*skb
)
217 struct ath10k_htc
*htc
= &ar
->htc
;
218 struct ath10k_skb_cb
*skb_cb
;
219 struct ath10k_htc_ep
*ep
;
221 if (WARN_ON_ONCE(!skb
))
224 skb_cb
= ATH10K_SKB_CB(skb
);
225 ep
= &htc
->endpoint
[skb_cb
->eid
];
227 ath10k_htc_notify_tx_completion(ep
, skb
);
228 /* the skb now belongs to the completion handler */
230 EXPORT_SYMBOL(ath10k_htc_tx_completion_handler
);
237 ath10k_htc_process_credit_report(struct ath10k_htc
*htc
,
238 const struct ath10k_htc_credit_report
*report
,
240 enum ath10k_htc_ep_id eid
)
242 struct ath10k
*ar
= htc
->ar
;
243 struct ath10k_htc_ep
*ep
;
246 if (len
% sizeof(*report
))
247 ath10k_warn(ar
, "Uneven credit report len %d", len
);
249 n_reports
= len
/ sizeof(*report
);
251 spin_lock_bh(&htc
->tx_lock
);
252 for (i
= 0; i
< n_reports
; i
++, report
++) {
253 if (report
->eid
>= ATH10K_HTC_EP_COUNT
)
256 ep
= &htc
->endpoint
[report
->eid
];
257 ep
->tx_credits
+= report
->credits
;
259 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "htc ep %d got %d credits (total %d)\n",
260 report
->eid
, report
->credits
, ep
->tx_credits
);
262 if (ep
->ep_ops
.ep_tx_credits
) {
263 spin_unlock_bh(&htc
->tx_lock
);
264 ep
->ep_ops
.ep_tx_credits(htc
->ar
);
265 spin_lock_bh(&htc
->tx_lock
);
268 spin_unlock_bh(&htc
->tx_lock
);
272 ath10k_htc_process_lookahead(struct ath10k_htc
*htc
,
273 const struct ath10k_htc_lookahead_report
*report
,
275 enum ath10k_htc_ep_id eid
,
276 void *next_lookaheads
,
277 int *next_lookaheads_len
)
279 struct ath10k
*ar
= htc
->ar
;
281 /* Invalid lookahead flags are actually transmitted by
282 * the target in the HTC control message.
283 * Since this will happen at every boot we silently ignore
284 * the lookahead in this case
286 if (report
->pre_valid
!= ((~report
->post_valid
) & 0xFF))
289 if (next_lookaheads
&& next_lookaheads_len
) {
290 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
291 "htc rx lookahead found pre_valid 0x%x post_valid 0x%x\n",
292 report
->pre_valid
, report
->post_valid
);
294 /* look ahead bytes are valid, copy them over */
295 memcpy((u8
*)next_lookaheads
, report
->lookahead
, 4);
297 *next_lookaheads_len
= 1;
304 ath10k_htc_process_lookahead_bundle(struct ath10k_htc
*htc
,
305 const struct ath10k_htc_lookahead_bundle
*report
,
307 enum ath10k_htc_ep_id eid
,
308 void *next_lookaheads
,
309 int *next_lookaheads_len
)
311 struct ath10k
*ar
= htc
->ar
;
312 int bundle_cnt
= len
/ sizeof(*report
);
314 if (!bundle_cnt
|| (bundle_cnt
> htc
->max_msgs_per_htc_bundle
)) {
315 ath10k_warn(ar
, "Invalid lookahead bundle count: %d\n",
320 if (next_lookaheads
&& next_lookaheads_len
) {
323 for (i
= 0; i
< bundle_cnt
; i
++) {
324 memcpy(((u8
*)next_lookaheads
) + 4 * i
,
325 report
->lookahead
, 4);
329 *next_lookaheads_len
= bundle_cnt
;
335 int ath10k_htc_process_trailer(struct ath10k_htc
*htc
,
338 enum ath10k_htc_ep_id src_eid
,
339 void *next_lookaheads
,
340 int *next_lookaheads_len
)
342 struct ath10k_htc_lookahead_bundle
*bundle
;
343 struct ath10k
*ar
= htc
->ar
;
345 struct ath10k_htc_record
*record
;
350 orig_buffer
= buffer
;
351 orig_length
= length
;
354 record
= (struct ath10k_htc_record
*)buffer
;
356 if (length
< sizeof(record
->hdr
)) {
361 if (record
->hdr
.len
> length
) {
362 /* no room left in buffer for record */
363 ath10k_warn(ar
, "Invalid record length: %d\n",
369 switch (record
->hdr
.id
) {
370 case ATH10K_HTC_RECORD_CREDITS
:
371 len
= sizeof(struct ath10k_htc_credit_report
);
372 if (record
->hdr
.len
< len
) {
373 ath10k_warn(ar
, "Credit report too long\n");
377 ath10k_htc_process_credit_report(htc
,
378 record
->credit_report
,
382 case ATH10K_HTC_RECORD_LOOKAHEAD
:
383 len
= sizeof(struct ath10k_htc_lookahead_report
);
384 if (record
->hdr
.len
< len
) {
385 ath10k_warn(ar
, "Lookahead report too long\n");
389 status
= ath10k_htc_process_lookahead(htc
,
390 record
->lookahead_report
,
394 next_lookaheads_len
);
396 case ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE
:
397 bundle
= record
->lookahead_bundle
;
398 status
= ath10k_htc_process_lookahead_bundle(htc
,
403 next_lookaheads_len
);
406 ath10k_warn(ar
, "Unhandled record: id:%d length:%d\n",
407 record
->hdr
.id
, record
->hdr
.len
);
414 /* multiple records may be present in a trailer */
415 buffer
+= sizeof(record
->hdr
) + record
->hdr
.len
;
416 length
-= sizeof(record
->hdr
) + record
->hdr
.len
;
420 ath10k_dbg_dump(ar
, ATH10K_DBG_HTC
, "htc rx bad trailer", "",
421 orig_buffer
, orig_length
);
425 EXPORT_SYMBOL(ath10k_htc_process_trailer
);
427 void ath10k_htc_rx_completion_handler(struct ath10k
*ar
, struct sk_buff
*skb
)
430 struct ath10k_htc
*htc
= &ar
->htc
;
431 struct ath10k_htc_hdr
*hdr
;
432 struct ath10k_htc_ep
*ep
;
437 bool trailer_present
;
439 hdr
= (struct ath10k_htc_hdr
*)skb
->data
;
440 skb_pull(skb
, sizeof(*hdr
));
444 if (eid
>= ATH10K_HTC_EP_COUNT
) {
445 ath10k_warn(ar
, "HTC Rx: invalid eid %d\n", eid
);
446 ath10k_dbg_dump(ar
, ATH10K_DBG_HTC
, "htc bad header", "",
451 ep
= &htc
->endpoint
[eid
];
453 payload_len
= __le16_to_cpu(hdr
->len
);
455 if (payload_len
+ sizeof(*hdr
) > ATH10K_HTC_MAX_LEN
) {
456 ath10k_warn(ar
, "HTC rx frame too long, len: %zu\n",
457 payload_len
+ sizeof(*hdr
));
458 ath10k_dbg_dump(ar
, ATH10K_DBG_HTC
, "htc bad rx pkt len", "",
463 if (skb
->len
< payload_len
) {
464 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
465 "HTC Rx: insufficient length, got %d, expected %d\n",
466 skb
->len
, payload_len
);
467 ath10k_dbg_dump(ar
, ATH10K_DBG_HTC
, "htc bad rx pkt len",
468 "", hdr
, sizeof(*hdr
));
472 /* get flags to check for trailer */
473 trailer_present
= hdr
->flags
& ATH10K_HTC_FLAG_TRAILER_PRESENT
;
474 if (trailer_present
) {
477 trailer_len
= hdr
->trailer_len
;
478 min_len
= sizeof(struct ath10k_ath10k_htc_record_hdr
);
480 if ((trailer_len
< min_len
) ||
481 (trailer_len
> payload_len
)) {
482 ath10k_warn(ar
, "Invalid trailer length: %d\n",
488 trailer
+= sizeof(*hdr
);
489 trailer
+= payload_len
;
490 trailer
-= trailer_len
;
491 status
= ath10k_htc_process_trailer(htc
, trailer
,
492 trailer_len
, hdr
->eid
,
497 skb_trim(skb
, skb
->len
- trailer_len
);
500 if (((int)payload_len
- (int)trailer_len
) <= 0)
501 /* zero length packet with trailer data, just drop these */
504 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "htc rx completion ep %d skb %pK\n",
506 ep
->ep_ops
.ep_rx_complete(ar
, skb
);
508 /* skb is now owned by the rx completion handler */
513 EXPORT_SYMBOL(ath10k_htc_rx_completion_handler
);
515 static void ath10k_htc_control_rx_complete(struct ath10k
*ar
,
518 struct ath10k_htc
*htc
= &ar
->htc
;
519 struct ath10k_htc_msg
*msg
= (struct ath10k_htc_msg
*)skb
->data
;
521 switch (__le16_to_cpu(msg
->hdr
.message_id
)) {
522 case ATH10K_HTC_MSG_READY_ID
:
523 case ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID
:
524 /* handle HTC control message */
525 if (completion_done(&htc
->ctl_resp
)) {
526 /* this is a fatal error, target should not be
527 * sending unsolicited messages on the ep 0
529 ath10k_warn(ar
, "HTC rx ctrl still processing\n");
530 complete(&htc
->ctl_resp
);
534 htc
->control_resp_len
=
536 ATH10K_HTC_MAX_CTRL_MSG_LEN
);
538 memcpy(htc
->control_resp_buffer
, skb
->data
,
539 htc
->control_resp_len
);
541 complete(&htc
->ctl_resp
);
543 case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE
:
544 htc
->htc_ops
.target_send_suspend_complete(ar
);
547 ath10k_warn(ar
, "ignoring unsolicited htc ep0 event\n");
559 static const char *htc_service_name(enum ath10k_htc_svc_id id
)
562 case ATH10K_HTC_SVC_ID_RESERVED
:
564 case ATH10K_HTC_SVC_ID_RSVD_CTRL
:
566 case ATH10K_HTC_SVC_ID_WMI_CONTROL
:
568 case ATH10K_HTC_SVC_ID_WMI_DATA_BE
:
570 case ATH10K_HTC_SVC_ID_WMI_DATA_BK
:
572 case ATH10K_HTC_SVC_ID_WMI_DATA_VI
:
574 case ATH10K_HTC_SVC_ID_WMI_DATA_VO
:
576 case ATH10K_HTC_SVC_ID_NMI_CONTROL
:
577 return "NMI Control";
578 case ATH10K_HTC_SVC_ID_NMI_DATA
:
580 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG
:
582 case ATH10K_HTC_SVC_ID_HTT_DATA2_MSG
:
584 case ATH10K_HTC_SVC_ID_HTT_DATA3_MSG
:
586 case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS
:
588 case ATH10K_HTC_SVC_ID_HTT_LOG_MSG
:
595 static void ath10k_htc_reset_endpoint_states(struct ath10k_htc
*htc
)
597 struct ath10k_htc_ep
*ep
;
600 for (i
= ATH10K_HTC_EP_0
; i
< ATH10K_HTC_EP_COUNT
; i
++) {
601 ep
= &htc
->endpoint
[i
];
602 ep
->service_id
= ATH10K_HTC_SVC_ID_UNUSED
;
603 ep
->max_ep_message_len
= 0;
604 ep
->max_tx_queue_depth
= 0;
607 ep
->tx_credit_flow_enabled
= true;
611 static u8
ath10k_htc_get_credit_allocation(struct ath10k_htc
*htc
,
616 /* The WMI control service is the only service with flow control.
617 * Let it have all transmit credits.
619 if (service_id
== ATH10K_HTC_SVC_ID_WMI_CONTROL
)
620 allocation
= htc
->total_transmit_credits
;
625 static int ath10k_htc_send_bundle(struct ath10k_htc_ep
*ep
,
626 struct sk_buff
*bundle_skb
,
627 struct sk_buff_head
*tx_save_head
)
629 struct ath10k_hif_sg_item sg_item
;
630 struct ath10k_htc
*htc
= ep
->htc
;
631 struct ath10k
*ar
= htc
->ar
;
634 unsigned int skb_len
;
636 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "bundle skb len %d\n", bundle_skb
->len
);
637 skb_len
= bundle_skb
->len
;
638 ret
= ath10k_htc_consume_credit(ep
, skb_len
, true);
641 sg_item
.transfer_id
= ep
->eid
;
642 sg_item
.transfer_context
= bundle_skb
;
643 sg_item
.vaddr
= bundle_skb
->data
;
644 sg_item
.len
= bundle_skb
->len
;
646 ret
= ath10k_hif_tx_sg(htc
->ar
, ep
->ul_pipe_id
, &sg_item
, 1);
648 ath10k_htc_release_credit(ep
, skb_len
);
652 dev_kfree_skb_any(bundle_skb
);
654 for (cn
= 0; (skb
= skb_dequeue_tail(tx_save_head
)); cn
++) {
656 skb_pull(skb
, sizeof(struct ath10k_htc_hdr
));
657 skb_queue_head(&ep
->tx_req_head
, skb
);
659 skb_queue_tail(&ep
->tx_complete_head
, skb
);
664 queue_work(ar
->workqueue_tx_complete
, &ar
->tx_complete_work
);
666 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
667 "bundle tx status %d eid %d req count %d count %d len %d\n",
668 ret
, ep
->eid
, skb_queue_len(&ep
->tx_req_head
), cn
, bundle_skb
->len
);
672 static void ath10k_htc_send_one_skb(struct ath10k_htc_ep
*ep
, struct sk_buff
*skb
)
674 struct ath10k_htc
*htc
= ep
->htc
;
675 struct ath10k
*ar
= htc
->ar
;
678 ret
= ath10k_htc_send(htc
, ep
->eid
, skb
);
681 skb_queue_head(&ep
->tx_req_head
, skb
);
683 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "tx one status %d eid %d len %d pending count %d\n",
684 ret
, ep
->eid
, skb
->len
, skb_queue_len(&ep
->tx_req_head
));
687 static int ath10k_htc_send_bundle_skbs(struct ath10k_htc_ep
*ep
)
689 struct ath10k_htc
*htc
= ep
->htc
;
690 struct sk_buff
*bundle_skb
, *skb
;
691 struct sk_buff_head tx_save_head
;
692 struct ath10k_htc_hdr
*hdr
;
694 int ret
= 0, credit_pad
, credit_remainder
, trans_len
, bundles_left
= 0;
696 if (htc
->ar
->state
== ATH10K_STATE_WEDGED
)
699 if (ep
->tx_credit_flow_enabled
&&
700 ep
->tx_credits
< ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE
)
703 bundles_left
= ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE
* ep
->tx_credit_size
;
704 bundle_skb
= dev_alloc_skb(bundles_left
);
709 bundle_buf
= bundle_skb
->data
;
710 skb_queue_head_init(&tx_save_head
);
713 skb
= skb_dequeue(&ep
->tx_req_head
);
718 trans_len
= skb
->len
+ sizeof(*hdr
);
719 credit_remainder
= trans_len
% ep
->tx_credit_size
;
721 if (credit_remainder
!= 0) {
722 credit_pad
= ep
->tx_credit_size
- credit_remainder
;
723 trans_len
+= credit_pad
;
726 ret
= ath10k_htc_consume_credit(ep
,
727 bundle_buf
+ trans_len
- bundle_skb
->data
,
730 skb_queue_head(&ep
->tx_req_head
, skb
);
734 if (bundles_left
< trans_len
) {
735 bundle_skb
->len
= bundle_buf
- bundle_skb
->data
;
736 ret
= ath10k_htc_send_bundle(ep
, bundle_skb
, &tx_save_head
);
739 skb_queue_head(&ep
->tx_req_head
, skb
);
743 if (skb_queue_len(&ep
->tx_req_head
) == 0) {
744 ath10k_htc_send_one_skb(ep
, skb
);
748 if (ep
->tx_credit_flow_enabled
&&
749 ep
->tx_credits
< ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE
) {
750 skb_queue_head(&ep
->tx_req_head
, skb
);
755 ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE
* ep
->tx_credit_size
;
756 bundle_skb
= dev_alloc_skb(bundles_left
);
759 skb_queue_head(&ep
->tx_req_head
, skb
);
762 bundle_buf
= bundle_skb
->data
;
763 skb_queue_head_init(&tx_save_head
);
766 skb_push(skb
, sizeof(struct ath10k_htc_hdr
));
767 ath10k_htc_prepare_tx_skb(ep
, skb
);
769 memcpy(bundle_buf
, skb
->data
, skb
->len
);
770 hdr
= (struct ath10k_htc_hdr
*)bundle_buf
;
771 hdr
->flags
|= ATH10K_HTC_FLAG_SEND_BUNDLE
;
772 hdr
->pad_len
= __cpu_to_le16(credit_pad
);
773 bundle_buf
+= trans_len
;
774 bundles_left
-= trans_len
;
775 skb_queue_tail(&tx_save_head
, skb
);
778 if (bundle_buf
!= bundle_skb
->data
) {
779 bundle_skb
->len
= bundle_buf
- bundle_skb
->data
;
780 ret
= ath10k_htc_send_bundle(ep
, bundle_skb
, &tx_save_head
);
782 dev_kfree_skb_any(bundle_skb
);
788 static void ath10k_htc_bundle_tx_work(struct work_struct
*work
)
790 struct ath10k
*ar
= container_of(work
, struct ath10k
, bundle_tx_work
);
791 struct ath10k_htc_ep
*ep
;
795 for (i
= 0; i
< ARRAY_SIZE(ar
->htc
.endpoint
); i
++) {
796 ep
= &ar
->htc
.endpoint
[i
];
801 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "bundle tx work eid %d count %d\n",
802 ep
->eid
, skb_queue_len(&ep
->tx_req_head
));
804 if (skb_queue_len(&ep
->tx_req_head
) >=
805 ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE
) {
806 ath10k_htc_send_bundle_skbs(ep
);
808 skb
= skb_dequeue(&ep
->tx_req_head
);
812 ath10k_htc_send_one_skb(ep
, skb
);
817 static void ath10k_htc_tx_complete_work(struct work_struct
*work
)
819 struct ath10k
*ar
= container_of(work
, struct ath10k
, tx_complete_work
);
820 struct ath10k_htc_ep
*ep
;
821 enum ath10k_htc_ep_id eid
;
825 for (i
= 0; i
< ARRAY_SIZE(ar
->htc
.endpoint
); i
++) {
826 ep
= &ar
->htc
.endpoint
[i
];
828 if (ep
->bundle_tx
&& eid
== ar
->htt
.eid
) {
829 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "bundle tx complete eid %d pending complete count%d\n",
830 ep
->eid
, skb_queue_len(&ep
->tx_complete_head
));
833 skb
= skb_dequeue(&ep
->tx_complete_head
);
836 ath10k_htc_notify_tx_completion(ep
, skb
);
842 int ath10k_htc_send_hl(struct ath10k_htc
*htc
,
843 enum ath10k_htc_ep_id eid
,
846 struct ath10k_htc_ep
*ep
= &htc
->endpoint
[eid
];
847 struct ath10k
*ar
= htc
->ar
;
849 if (sizeof(struct ath10k_htc_hdr
) + skb
->len
> ep
->tx_credit_size
) {
850 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "tx exceed max len %d\n", skb
->len
);
854 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "htc send hl eid %d bundle %d tx count %d len %d\n",
855 eid
, ep
->bundle_tx
, skb_queue_len(&ep
->tx_req_head
), skb
->len
);
858 skb_queue_tail(&ep
->tx_req_head
, skb
);
859 queue_work(ar
->workqueue
, &ar
->bundle_tx_work
);
862 return ath10k_htc_send(htc
, eid
, skb
);
866 void ath10k_htc_setup_tx_req(struct ath10k_htc_ep
*ep
)
868 if (ep
->htc
->max_msgs_per_htc_bundle
>= ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE
&&
870 ep
->bundle_tx
= true;
871 skb_queue_head_init(&ep
->tx_req_head
);
872 skb_queue_head_init(&ep
->tx_complete_head
);
876 void ath10k_htc_stop_hl(struct ath10k
*ar
)
878 struct ath10k_htc_ep
*ep
;
881 cancel_work_sync(&ar
->bundle_tx_work
);
882 cancel_work_sync(&ar
->tx_complete_work
);
884 for (i
= 0; i
< ARRAY_SIZE(ar
->htc
.endpoint
); i
++) {
885 ep
= &ar
->htc
.endpoint
[i
];
890 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "stop tx work eid %d count %d\n",
891 ep
->eid
, skb_queue_len(&ep
->tx_req_head
));
893 skb_queue_purge(&ep
->tx_req_head
);
897 int ath10k_htc_wait_target(struct ath10k_htc
*htc
)
899 struct ath10k
*ar
= htc
->ar
;
901 unsigned long time_left
;
902 struct ath10k_htc_msg
*msg
;
905 time_left
= wait_for_completion_timeout(&htc
->ctl_resp
,
906 ATH10K_HTC_WAIT_TIMEOUT_HZ
);
908 /* Workaround: In some cases the PCI HIF doesn't
909 * receive interrupt for the control response message
910 * even if the buffer was completed. It is suspected
911 * iomap writes unmasking PCI CE irqs aren't propagated
912 * properly in KVM PCI-passthrough sometimes.
914 ath10k_warn(ar
, "failed to receive control response completion, polling..\n");
916 for (i
= 0; i
< CE_COUNT
; i
++)
917 ath10k_hif_send_complete_check(htc
->ar
, i
, 1);
920 wait_for_completion_timeout(&htc
->ctl_resp
,
921 ATH10K_HTC_WAIT_TIMEOUT_HZ
);
928 ath10k_err(ar
, "ctl_resp never came in (%d)\n", status
);
932 if (htc
->control_resp_len
< sizeof(msg
->hdr
) + sizeof(msg
->ready
)) {
933 ath10k_err(ar
, "Invalid HTC ready msg len:%d\n",
934 htc
->control_resp_len
);
938 msg
= (struct ath10k_htc_msg
*)htc
->control_resp_buffer
;
939 message_id
= __le16_to_cpu(msg
->hdr
.message_id
);
941 if (message_id
!= ATH10K_HTC_MSG_READY_ID
) {
942 ath10k_err(ar
, "Invalid HTC ready msg: 0x%x\n", message_id
);
946 htc
->total_transmit_credits
= __le16_to_cpu(msg
->ready
.credit_count
);
947 htc
->target_credit_size
= __le16_to_cpu(msg
->ready
.credit_size
);
949 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
950 "Target ready! transmit resources: %d size:%d\n",
951 htc
->total_transmit_credits
,
952 htc
->target_credit_size
);
954 if ((htc
->total_transmit_credits
== 0) ||
955 (htc
->target_credit_size
== 0)) {
956 ath10k_err(ar
, "Invalid credit size received\n");
960 /* The only way to determine if the ready message is an extended
961 * message is from the size.
963 if (htc
->control_resp_len
>=
964 sizeof(msg
->hdr
) + sizeof(msg
->ready_ext
)) {
965 htc
->alt_data_credit_size
=
966 __le16_to_cpu(msg
->ready_ext
.reserved
) &
967 ATH10K_HTC_MSG_READY_EXT_ALT_DATA_MASK
;
968 htc
->max_msgs_per_htc_bundle
=
969 min_t(u8
, msg
->ready_ext
.max_msgs_per_htc_bundle
,
970 HTC_HOST_MAX_MSG_PER_RX_BUNDLE
);
971 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
972 "Extended ready message RX bundle size %d alt size %d\n",
973 htc
->max_msgs_per_htc_bundle
,
974 htc
->alt_data_credit_size
);
977 INIT_WORK(&ar
->bundle_tx_work
, ath10k_htc_bundle_tx_work
);
978 INIT_WORK(&ar
->tx_complete_work
, ath10k_htc_tx_complete_work
);
983 void ath10k_htc_change_tx_credit_flow(struct ath10k_htc
*htc
,
984 enum ath10k_htc_ep_id eid
,
987 struct ath10k
*ar
= htc
->ar
;
988 struct ath10k_htc_ep
*ep
= &ar
->htc
.endpoint
[eid
];
990 ep
->tx_credit_flow_enabled
= enable
;
993 int ath10k_htc_connect_service(struct ath10k_htc
*htc
,
994 struct ath10k_htc_svc_conn_req
*conn_req
,
995 struct ath10k_htc_svc_conn_resp
*conn_resp
)
997 struct ath10k
*ar
= htc
->ar
;
998 struct ath10k_htc_msg
*msg
;
999 struct ath10k_htc_conn_svc
*req_msg
;
1000 struct ath10k_htc_conn_svc_response resp_msg_dummy
;
1001 struct ath10k_htc_conn_svc_response
*resp_msg
= &resp_msg_dummy
;
1002 enum ath10k_htc_ep_id assigned_eid
= ATH10K_HTC_EP_COUNT
;
1003 struct ath10k_htc_ep
*ep
;
1004 struct sk_buff
*skb
;
1005 unsigned int max_msg_size
= 0;
1007 unsigned long time_left
;
1008 bool disable_credit_flow_ctrl
= false;
1009 u16 message_id
, service_id
, flags
= 0;
1012 /* special case for HTC pseudo control service */
1013 if (conn_req
->service_id
== ATH10K_HTC_SVC_ID_RSVD_CTRL
) {
1014 disable_credit_flow_ctrl
= true;
1015 assigned_eid
= ATH10K_HTC_EP_0
;
1016 max_msg_size
= ATH10K_HTC_MAX_CTRL_MSG_LEN
;
1017 memset(&resp_msg_dummy
, 0, sizeof(resp_msg_dummy
));
1021 tx_alloc
= ath10k_htc_get_credit_allocation(htc
,
1022 conn_req
->service_id
);
1024 ath10k_dbg(ar
, ATH10K_DBG_BOOT
,
1025 "boot htc service %s does not allocate target credits\n",
1026 htc_service_name(conn_req
->service_id
));
1028 skb
= ath10k_htc_build_tx_ctrl_skb(htc
->ar
);
1030 ath10k_err(ar
, "Failed to allocate HTC packet\n");
1034 length
= sizeof(msg
->hdr
) + sizeof(msg
->connect_service
);
1035 skb_put(skb
, length
);
1036 memset(skb
->data
, 0, length
);
1038 msg
= (struct ath10k_htc_msg
*)skb
->data
;
1039 msg
->hdr
.message_id
=
1040 __cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID
);
1042 flags
|= SM(tx_alloc
, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC
);
1044 /* Only enable credit flow control for WMI ctrl service */
1045 if (conn_req
->service_id
!= ATH10K_HTC_SVC_ID_WMI_CONTROL
) {
1046 flags
|= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL
;
1047 disable_credit_flow_ctrl
= true;
1050 req_msg
= &msg
->connect_service
;
1051 req_msg
->flags
= __cpu_to_le16(flags
);
1052 req_msg
->service_id
= __cpu_to_le16(conn_req
->service_id
);
1054 reinit_completion(&htc
->ctl_resp
);
1056 status
= ath10k_htc_send(htc
, ATH10K_HTC_EP_0
, skb
);
1062 /* wait for response */
1063 time_left
= wait_for_completion_timeout(&htc
->ctl_resp
,
1064 ATH10K_HTC_CONN_SVC_TIMEOUT_HZ
);
1066 ath10k_err(ar
, "Service connect timeout\n");
1070 /* we controlled the buffer creation, it's aligned */
1071 msg
= (struct ath10k_htc_msg
*)htc
->control_resp_buffer
;
1072 resp_msg
= &msg
->connect_service_response
;
1073 message_id
= __le16_to_cpu(msg
->hdr
.message_id
);
1074 service_id
= __le16_to_cpu(resp_msg
->service_id
);
1076 if ((message_id
!= ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID
) ||
1077 (htc
->control_resp_len
< sizeof(msg
->hdr
) +
1078 sizeof(msg
->connect_service_response
))) {
1079 ath10k_err(ar
, "Invalid resp message ID 0x%x", message_id
);
1083 ath10k_dbg(ar
, ATH10K_DBG_HTC
,
1084 "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
1085 htc_service_name(service_id
),
1086 resp_msg
->status
, resp_msg
->eid
);
1088 conn_resp
->connect_resp_code
= resp_msg
->status
;
1090 /* check response status */
1091 if (resp_msg
->status
!= ATH10K_HTC_CONN_SVC_STATUS_SUCCESS
) {
1092 ath10k_err(ar
, "HTC Service %s connect request failed: 0x%x)\n",
1093 htc_service_name(service_id
),
1098 assigned_eid
= (enum ath10k_htc_ep_id
)resp_msg
->eid
;
1099 max_msg_size
= __le16_to_cpu(resp_msg
->max_msg_size
);
1103 if (assigned_eid
>= ATH10K_HTC_EP_COUNT
)
1106 if (max_msg_size
== 0)
1109 ep
= &htc
->endpoint
[assigned_eid
];
1110 ep
->eid
= assigned_eid
;
1112 if (ep
->service_id
!= ATH10K_HTC_SVC_ID_UNUSED
)
1115 /* return assigned endpoint to caller */
1116 conn_resp
->eid
= assigned_eid
;
1117 conn_resp
->max_msg_len
= __le16_to_cpu(resp_msg
->max_msg_size
);
1119 /* setup the endpoint */
1120 ep
->service_id
= conn_req
->service_id
;
1121 ep
->max_tx_queue_depth
= conn_req
->max_send_queue_depth
;
1122 ep
->max_ep_message_len
= __le16_to_cpu(resp_msg
->max_msg_size
);
1123 ep
->tx_credits
= tx_alloc
;
1124 ep
->tx_credit_size
= htc
->target_credit_size
;
1126 if (conn_req
->service_id
== ATH10K_HTC_SVC_ID_HTT_DATA_MSG
&&
1127 htc
->alt_data_credit_size
!= 0)
1128 ep
->tx_credit_size
= htc
->alt_data_credit_size
;
1130 /* copy all the callbacks */
1131 ep
->ep_ops
= conn_req
->ep_ops
;
1133 status
= ath10k_hif_map_service_to_pipe(htc
->ar
,
1138 ath10k_dbg(ar
, ATH10K_DBG_BOOT
, "unsupported HTC service id: %d\n",
1143 ath10k_dbg(ar
, ATH10K_DBG_BOOT
,
1144 "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
1145 htc_service_name(ep
->service_id
), ep
->ul_pipe_id
,
1146 ep
->dl_pipe_id
, ep
->eid
);
1148 if (disable_credit_flow_ctrl
&& ep
->tx_credit_flow_enabled
) {
1149 ep
->tx_credit_flow_enabled
= false;
1150 ath10k_dbg(ar
, ATH10K_DBG_BOOT
,
1151 "boot htc service '%s' eid %d TX flow control disabled\n",
1152 htc_service_name(ep
->service_id
), assigned_eid
);
1158 struct sk_buff
*ath10k_htc_alloc_skb(struct ath10k
*ar
, int size
)
1160 struct sk_buff
*skb
;
1162 skb
= dev_alloc_skb(size
+ sizeof(struct ath10k_htc_hdr
));
1166 skb_reserve(skb
, sizeof(struct ath10k_htc_hdr
));
1168 /* FW/HTC requires 4-byte aligned streams */
1169 if (!IS_ALIGNED((unsigned long)skb
->data
, 4))
1170 ath10k_warn(ar
, "Unaligned HTC tx skb\n");
1175 static void ath10k_htc_pktlog_process_rx(struct ath10k
*ar
, struct sk_buff
*skb
)
1177 trace_ath10k_htt_pktlog(ar
, skb
->data
, skb
->len
);
1178 dev_kfree_skb_any(skb
);
1181 static int ath10k_htc_pktlog_connect(struct ath10k
*ar
)
1183 struct ath10k_htc_svc_conn_resp conn_resp
;
1184 struct ath10k_htc_svc_conn_req conn_req
;
1187 memset(&conn_req
, 0, sizeof(conn_req
));
1188 memset(&conn_resp
, 0, sizeof(conn_resp
));
1190 conn_req
.ep_ops
.ep_tx_complete
= NULL
;
1191 conn_req
.ep_ops
.ep_rx_complete
= ath10k_htc_pktlog_process_rx
;
1192 conn_req
.ep_ops
.ep_tx_credits
= NULL
;
1194 /* connect to control service */
1195 conn_req
.service_id
= ATH10K_HTC_SVC_ID_HTT_LOG_MSG
;
1196 status
= ath10k_htc_connect_service(&ar
->htc
, &conn_req
, &conn_resp
);
1198 ath10k_warn(ar
, "failed to connect to PKTLOG service: %d\n",
1206 static bool ath10k_htc_pktlog_svc_supported(struct ath10k
*ar
)
1212 status
= ath10k_hif_map_service_to_pipe(ar
, ATH10K_HTC_SVC_ID_HTT_LOG_MSG
,
1216 ath10k_dbg(ar
, ATH10K_DBG_BOOT
, "unsupported HTC pktlog service id: %d\n",
1217 ATH10K_HTC_SVC_ID_HTT_LOG_MSG
);
1225 int ath10k_htc_start(struct ath10k_htc
*htc
)
1227 struct ath10k
*ar
= htc
->ar
;
1228 struct sk_buff
*skb
;
1230 struct ath10k_htc_msg
*msg
;
1232 skb
= ath10k_htc_build_tx_ctrl_skb(htc
->ar
);
1236 skb_put(skb
, sizeof(msg
->hdr
) + sizeof(msg
->setup_complete_ext
));
1237 memset(skb
->data
, 0, skb
->len
);
1239 msg
= (struct ath10k_htc_msg
*)skb
->data
;
1240 msg
->hdr
.message_id
=
1241 __cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID
);
1243 if (ar
->hif
.bus
== ATH10K_BUS_SDIO
) {
1244 /* Extra setup params used by SDIO */
1245 msg
->setup_complete_ext
.flags
=
1246 __cpu_to_le32(ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN
);
1247 msg
->setup_complete_ext
.max_msgs_per_bundled_recv
=
1248 htc
->max_msgs_per_htc_bundle
;
1250 ath10k_dbg(ar
, ATH10K_DBG_HTC
, "HTC is using TX credit flow control\n");
1252 status
= ath10k_htc_send(htc
, ATH10K_HTC_EP_0
, skb
);
1258 if (ath10k_htc_pktlog_svc_supported(ar
)) {
1259 status
= ath10k_htc_pktlog_connect(ar
);
1261 ath10k_err(ar
, "failed to connect to pktlog: %d\n", status
);
1269 /* registered target arrival callback from the HIF layer */
1270 int ath10k_htc_init(struct ath10k
*ar
)
1273 struct ath10k_htc
*htc
= &ar
->htc
;
1274 struct ath10k_htc_svc_conn_req conn_req
;
1275 struct ath10k_htc_svc_conn_resp conn_resp
;
1277 spin_lock_init(&htc
->tx_lock
);
1279 ath10k_htc_reset_endpoint_states(htc
);
1283 /* setup our pseudo HTC control endpoint connection */
1284 memset(&conn_req
, 0, sizeof(conn_req
));
1285 memset(&conn_resp
, 0, sizeof(conn_resp
));
1286 conn_req
.ep_ops
.ep_tx_complete
= ath10k_htc_control_tx_complete
;
1287 conn_req
.ep_ops
.ep_rx_complete
= ath10k_htc_control_rx_complete
;
1288 conn_req
.max_send_queue_depth
= ATH10K_NUM_CONTROL_TX_BUFFERS
;
1289 conn_req
.service_id
= ATH10K_HTC_SVC_ID_RSVD_CTRL
;
1291 /* connect fake service */
1292 status
= ath10k_htc_connect_service(htc
, &conn_req
, &conn_resp
);
1294 ath10k_err(ar
, "could not connect to htc service (%d)\n",
1299 init_completion(&htc
->ctl_resp
);