2 * Copyright (c) 2007-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
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.
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.
22 #include <asm/unaligned.h>
24 #define CALC_TXRX_PADDED_LEN(dev, len) (__ALIGN_MASK((len), (dev)->block_mask))
26 /* threshold to re-enable Tx bundling for an AC*/
27 #define TX_RESUME_BUNDLE_THRESHOLD 1500
29 /* Functions for Tx credit handling */
30 static void ath6kl_credit_deposit(struct ath6kl_htc_credit_info
*cred_info
,
31 struct htc_endpoint_credit_dist
*ep_dist
,
34 ath6kl_dbg(ATH6KL_DBG_CREDIT
, "credit deposit ep %d credits %d\n",
35 ep_dist
->endpoint
, credits
);
37 ep_dist
->credits
+= credits
;
38 ep_dist
->cred_assngd
+= credits
;
39 cred_info
->cur_free_credits
-= credits
;
42 static void ath6kl_credit_init(struct ath6kl_htc_credit_info
*cred_info
,
43 struct list_head
*ep_list
,
46 struct htc_endpoint_credit_dist
*cur_ep_dist
;
49 ath6kl_dbg(ATH6KL_DBG_CREDIT
, "credit init total %d\n", tot_credits
);
51 cred_info
->cur_free_credits
= tot_credits
;
52 cred_info
->total_avail_credits
= tot_credits
;
54 list_for_each_entry(cur_ep_dist
, ep_list
, list
) {
55 if (cur_ep_dist
->endpoint
== ENDPOINT_0
)
58 cur_ep_dist
->cred_min
= cur_ep_dist
->cred_per_msg
;
60 if (tot_credits
> 4) {
61 if ((cur_ep_dist
->svc_id
== WMI_DATA_BK_SVC
) ||
62 (cur_ep_dist
->svc_id
== WMI_DATA_BE_SVC
)) {
63 ath6kl_credit_deposit(cred_info
,
65 cur_ep_dist
->cred_min
);
66 cur_ep_dist
->dist_flags
|= HTC_EP_ACTIVE
;
70 if (cur_ep_dist
->svc_id
== WMI_CONTROL_SVC
) {
71 ath6kl_credit_deposit(cred_info
, cur_ep_dist
,
72 cur_ep_dist
->cred_min
);
74 * Control service is always marked active, it
75 * never goes inactive EVER.
77 cur_ep_dist
->dist_flags
|= HTC_EP_ACTIVE
;
78 } else if (cur_ep_dist
->svc_id
== WMI_DATA_BK_SVC
)
79 /* this is the lowest priority data endpoint */
80 /* FIXME: this looks fishy, check */
81 cred_info
->lowestpri_ep_dist
= cur_ep_dist
->list
;
84 * Streams have to be created (explicit | implicit) for all
85 * kinds of traffic. BE endpoints are also inactive in the
86 * beginning. When BE traffic starts it creates implicit
87 * streams that redistributes credits.
89 * Note: all other endpoints have minimums set but are
90 * initially given NO credits. credits will be distributed
91 * as traffic activity demands
95 WARN_ON(cred_info
->cur_free_credits
<= 0);
97 list_for_each_entry(cur_ep_dist
, ep_list
, list
) {
98 if (cur_ep_dist
->endpoint
== ENDPOINT_0
)
101 if (cur_ep_dist
->svc_id
== WMI_CONTROL_SVC
)
102 cur_ep_dist
->cred_norm
= cur_ep_dist
->cred_per_msg
;
105 * For the remaining data endpoints, we assume that
106 * each cred_per_msg are the same. We use a simple
107 * calculation here, we take the remaining credits
108 * and determine how many max messages this can
109 * cover and then set each endpoint's normal value
110 * equal to 3/4 this amount.
112 count
= (cred_info
->cur_free_credits
/
113 cur_ep_dist
->cred_per_msg
)
114 * cur_ep_dist
->cred_per_msg
;
115 count
= (count
* 3) >> 2;
116 count
= max(count
, cur_ep_dist
->cred_per_msg
);
117 cur_ep_dist
->cred_norm
= count
;
121 ath6kl_dbg(ATH6KL_DBG_CREDIT
,
122 "credit ep %d svc_id %d credits %d per_msg %d norm %d min %d\n",
123 cur_ep_dist
->endpoint
,
125 cur_ep_dist
->credits
,
126 cur_ep_dist
->cred_per_msg
,
127 cur_ep_dist
->cred_norm
,
128 cur_ep_dist
->cred_min
);
132 /* initialize and setup credit distribution */
133 int ath6kl_credit_setup(void *htc_handle
,
134 struct ath6kl_htc_credit_info
*cred_info
)
136 u16 servicepriority
[5];
138 memset(cred_info
, 0, sizeof(struct ath6kl_htc_credit_info
));
140 servicepriority
[0] = WMI_CONTROL_SVC
; /* highest */
141 servicepriority
[1] = WMI_DATA_VO_SVC
;
142 servicepriority
[2] = WMI_DATA_VI_SVC
;
143 servicepriority
[3] = WMI_DATA_BE_SVC
;
144 servicepriority
[4] = WMI_DATA_BK_SVC
; /* lowest */
146 /* set priority list */
147 ath6kl_htc_set_credit_dist(htc_handle
, cred_info
, servicepriority
, 5);
152 /* reduce an ep's credits back to a set limit */
153 static void ath6kl_credit_reduce(struct ath6kl_htc_credit_info
*cred_info
,
154 struct htc_endpoint_credit_dist
*ep_dist
,
159 ath6kl_dbg(ATH6KL_DBG_CREDIT
, "credit reduce ep %d limit %d\n",
160 ep_dist
->endpoint
, limit
);
162 ep_dist
->cred_assngd
= limit
;
164 if (ep_dist
->credits
<= limit
)
167 credits
= ep_dist
->credits
- limit
;
168 ep_dist
->credits
-= credits
;
169 cred_info
->cur_free_credits
+= credits
;
172 static void ath6kl_credit_update(struct ath6kl_htc_credit_info
*cred_info
,
173 struct list_head
*epdist_list
)
175 struct htc_endpoint_credit_dist
*cur_list
;
177 list_for_each_entry(cur_list
, epdist_list
, list
) {
178 if (cur_list
->endpoint
== ENDPOINT_0
)
181 if (cur_list
->cred_to_dist
> 0) {
182 cur_list
->credits
+= cur_list
->cred_to_dist
;
183 cur_list
->cred_to_dist
= 0;
185 if (cur_list
->credits
> cur_list
->cred_assngd
)
186 ath6kl_credit_reduce(cred_info
,
188 cur_list
->cred_assngd
);
190 if (cur_list
->credits
> cur_list
->cred_norm
)
191 ath6kl_credit_reduce(cred_info
, cur_list
,
192 cur_list
->cred_norm
);
194 if (!(cur_list
->dist_flags
& HTC_EP_ACTIVE
)) {
195 if (cur_list
->txq_depth
== 0)
196 ath6kl_credit_reduce(cred_info
,
204 * HTC has an endpoint that needs credits, ep_dist is the endpoint in
207 static void ath6kl_credit_seek(struct ath6kl_htc_credit_info
*cred_info
,
208 struct htc_endpoint_credit_dist
*ep_dist
)
210 struct htc_endpoint_credit_dist
*curdist_list
;
214 if (ep_dist
->svc_id
== WMI_CONTROL_SVC
)
217 if ((ep_dist
->svc_id
== WMI_DATA_VI_SVC
) ||
218 (ep_dist
->svc_id
== WMI_DATA_VO_SVC
))
219 if ((ep_dist
->cred_assngd
>= ep_dist
->cred_norm
))
223 * For all other services, we follow a simple algorithm of:
225 * 1. checking the free pool for credits
226 * 2. checking lower priority endpoints for credits to take
229 credits
= min(cred_info
->cur_free_credits
, ep_dist
->seek_cred
);
231 if (credits
>= ep_dist
->seek_cred
)
235 * We don't have enough in the free pool, try taking away from
236 * lower priority services The rule for taking away credits:
238 * 1. Only take from lower priority endpoints
239 * 2. Only take what is allocated above the minimum (never
240 * starve an endpoint completely)
241 * 3. Only take what you need.
244 list_for_each_entry_reverse(curdist_list
,
245 &cred_info
->lowestpri_ep_dist
,
247 if (curdist_list
== ep_dist
)
250 need
= ep_dist
->seek_cred
- cred_info
->cur_free_credits
;
252 if ((curdist_list
->cred_assngd
- need
) >=
253 curdist_list
->cred_min
) {
255 * The current one has been allocated more than
256 * it's minimum and it has enough credits assigned
257 * above it's minimum to fulfill our need try to
258 * take away just enough to fulfill our need.
260 ath6kl_credit_reduce(cred_info
, curdist_list
,
261 curdist_list
->cred_assngd
- need
);
263 if (cred_info
->cur_free_credits
>=
268 if (curdist_list
->endpoint
== ENDPOINT_0
)
272 credits
= min(cred_info
->cur_free_credits
, ep_dist
->seek_cred
);
275 /* did we find some credits? */
277 ath6kl_credit_deposit(cred_info
, ep_dist
, credits
);
279 ep_dist
->seek_cred
= 0;
282 /* redistribute credits based on activity change */
283 static void ath6kl_credit_redistribute(struct ath6kl_htc_credit_info
*info
,
284 struct list_head
*ep_dist_list
)
286 struct htc_endpoint_credit_dist
*curdist_list
;
288 list_for_each_entry(curdist_list
, ep_dist_list
, list
) {
289 if (curdist_list
->endpoint
== ENDPOINT_0
)
292 if ((curdist_list
->svc_id
== WMI_DATA_BK_SVC
) ||
293 (curdist_list
->svc_id
== WMI_DATA_BE_SVC
))
294 curdist_list
->dist_flags
|= HTC_EP_ACTIVE
;
296 if ((curdist_list
->svc_id
!= WMI_CONTROL_SVC
) &&
297 !(curdist_list
->dist_flags
& HTC_EP_ACTIVE
)) {
298 if (curdist_list
->txq_depth
== 0)
299 ath6kl_credit_reduce(info
, curdist_list
, 0);
301 ath6kl_credit_reduce(info
,
303 curdist_list
->cred_min
);
310 * This function is invoked whenever endpoints require credit
311 * distributions. A lock is held while this function is invoked, this
312 * function shall NOT block. The ep_dist_list is a list of distribution
313 * structures in prioritized order as defined by the call to the
314 * htc_set_credit_dist() api.
316 static void ath6kl_credit_distribute(struct ath6kl_htc_credit_info
*cred_info
,
317 struct list_head
*ep_dist_list
,
318 enum htc_credit_dist_reason reason
)
321 case HTC_CREDIT_DIST_SEND_COMPLETE
:
322 ath6kl_credit_update(cred_info
, ep_dist_list
);
324 case HTC_CREDIT_DIST_ACTIVITY_CHANGE
:
325 ath6kl_credit_redistribute(cred_info
, ep_dist_list
);
331 WARN_ON(cred_info
->cur_free_credits
> cred_info
->total_avail_credits
);
332 WARN_ON(cred_info
->cur_free_credits
< 0);
335 static void ath6kl_htc_tx_buf_align(u8
**buf
, unsigned long len
)
339 if (!IS_ALIGNED((unsigned long) *buf
, 4)) {
340 align_addr
= PTR_ALIGN(*buf
- 4, 4);
341 memmove(align_addr
, *buf
, len
);
346 static void ath6kl_htc_tx_prep_pkt(struct htc_packet
*packet
, u8 flags
,
347 int ctrl0
, int ctrl1
)
349 struct htc_frame_hdr
*hdr
;
351 packet
->buf
-= HTC_HDR_LENGTH
;
352 hdr
= (struct htc_frame_hdr
*)packet
->buf
;
355 put_unaligned((u16
)packet
->act_len
, &hdr
->payld_len
);
357 hdr
->eid
= packet
->endpoint
;
358 hdr
->ctrl
[0] = ctrl0
;
359 hdr
->ctrl
[1] = ctrl1
;
362 static void htc_reclaim_txctrl_buf(struct htc_target
*target
,
363 struct htc_packet
*pkt
)
365 spin_lock_bh(&target
->htc_lock
);
366 list_add_tail(&pkt
->list
, &target
->free_ctrl_txbuf
);
367 spin_unlock_bh(&target
->htc_lock
);
370 static struct htc_packet
*htc_get_control_buf(struct htc_target
*target
,
373 struct htc_packet
*packet
= NULL
;
374 struct list_head
*buf_list
;
376 buf_list
= tx
? &target
->free_ctrl_txbuf
: &target
->free_ctrl_rxbuf
;
378 spin_lock_bh(&target
->htc_lock
);
380 if (list_empty(buf_list
)) {
381 spin_unlock_bh(&target
->htc_lock
);
385 packet
= list_first_entry(buf_list
, struct htc_packet
, list
);
386 list_del(&packet
->list
);
387 spin_unlock_bh(&target
->htc_lock
);
390 packet
->buf
= packet
->buf_start
+ HTC_HDR_LENGTH
;
395 static void htc_tx_comp_update(struct htc_target
*target
,
396 struct htc_endpoint
*endpoint
,
397 struct htc_packet
*packet
)
399 packet
->completion
= NULL
;
400 packet
->buf
+= HTC_HDR_LENGTH
;
405 ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
406 packet
->status
, packet
->endpoint
, packet
->act_len
,
407 packet
->info
.tx
.cred_used
);
409 /* on failure to submit, reclaim credits for this packet */
410 spin_lock_bh(&target
->tx_lock
);
411 endpoint
->cred_dist
.cred_to_dist
+=
412 packet
->info
.tx
.cred_used
;
413 endpoint
->cred_dist
.txq_depth
= get_queue_depth(&endpoint
->txq
);
415 ath6kl_dbg(ATH6KL_DBG_HTC
, "htc tx ctxt 0x%p dist 0x%p\n",
416 target
->credit_info
, &target
->cred_dist_list
);
418 ath6kl_credit_distribute(target
->credit_info
,
419 &target
->cred_dist_list
,
420 HTC_CREDIT_DIST_SEND_COMPLETE
);
422 spin_unlock_bh(&target
->tx_lock
);
425 static void htc_tx_complete(struct htc_endpoint
*endpoint
,
426 struct list_head
*txq
)
431 ath6kl_dbg(ATH6KL_DBG_HTC
,
432 "htc tx complete ep %d pkts %d\n",
433 endpoint
->eid
, get_queue_depth(txq
));
435 ath6kl_tx_complete(endpoint
->target
->dev
->ar
, txq
);
438 static void htc_tx_comp_handler(struct htc_target
*target
,
439 struct htc_packet
*packet
)
441 struct htc_endpoint
*endpoint
= &target
->endpoint
[packet
->endpoint
];
442 struct list_head container
;
444 ath6kl_dbg(ATH6KL_DBG_HTC
, "htc tx complete seqno %d\n",
445 packet
->info
.tx
.seqno
);
447 htc_tx_comp_update(target
, endpoint
, packet
);
448 INIT_LIST_HEAD(&container
);
449 list_add_tail(&packet
->list
, &container
);
451 htc_tx_complete(endpoint
, &container
);
454 static void htc_async_tx_scat_complete(struct htc_target
*target
,
455 struct hif_scatter_req
*scat_req
)
457 struct htc_endpoint
*endpoint
;
458 struct htc_packet
*packet
;
459 struct list_head tx_compq
;
462 INIT_LIST_HEAD(&tx_compq
);
464 ath6kl_dbg(ATH6KL_DBG_HTC
,
465 "htc tx scat complete len %d entries %d\n",
466 scat_req
->len
, scat_req
->scat_entries
);
468 if (scat_req
->status
)
469 ath6kl_err("send scatter req failed: %d\n", scat_req
->status
);
471 packet
= scat_req
->scat_list
[0].packet
;
472 endpoint
= &target
->endpoint
[packet
->endpoint
];
474 /* walk through the scatter list and process */
475 for (i
= 0; i
< scat_req
->scat_entries
; i
++) {
476 packet
= scat_req
->scat_list
[i
].packet
;
482 packet
->status
= scat_req
->status
;
483 htc_tx_comp_update(target
, endpoint
, packet
);
484 list_add_tail(&packet
->list
, &tx_compq
);
487 /* free scatter request */
488 hif_scatter_req_add(target
->dev
->ar
, scat_req
);
490 /* complete all packets */
491 htc_tx_complete(endpoint
, &tx_compq
);
494 static int ath6kl_htc_tx_issue(struct htc_target
*target
,
495 struct htc_packet
*packet
)
499 u32 padded_len
, send_len
;
501 if (!packet
->completion
)
504 send_len
= packet
->act_len
+ HTC_HDR_LENGTH
;
506 padded_len
= CALC_TXRX_PADDED_LEN(target
, send_len
);
508 ath6kl_dbg(ATH6KL_DBG_HTC
,
509 "htc tx issue len %d seqno %d padded_len %d mbox 0x%X %s\n",
510 send_len
, packet
->info
.tx
.seqno
, padded_len
,
511 target
->dev
->ar
->mbox_info
.htc_addr
,
512 sync
? "sync" : "async");
515 status
= hif_read_write_sync(target
->dev
->ar
,
516 target
->dev
->ar
->mbox_info
.htc_addr
,
517 packet
->buf
, padded_len
,
518 HIF_WR_SYNC_BLOCK_INC
);
520 packet
->status
= status
;
521 packet
->buf
+= HTC_HDR_LENGTH
;
523 status
= hif_write_async(target
->dev
->ar
,
524 target
->dev
->ar
->mbox_info
.htc_addr
,
525 packet
->buf
, padded_len
,
526 HIF_WR_ASYNC_BLOCK_INC
, packet
);
531 static int htc_check_credits(struct htc_target
*target
,
532 struct htc_endpoint
*ep
, u8
*flags
,
533 enum htc_endpoint_id eid
, unsigned int len
,
537 *req_cred
= (len
> target
->tgt_cred_sz
) ?
538 DIV_ROUND_UP(len
, target
->tgt_cred_sz
) : 1;
540 ath6kl_dbg(ATH6KL_DBG_CREDIT
, "credit check need %d got %d\n",
541 *req_cred
, ep
->cred_dist
.credits
);
543 if (ep
->cred_dist
.credits
< *req_cred
) {
544 if (eid
== ENDPOINT_0
)
547 /* Seek more credits */
548 ep
->cred_dist
.seek_cred
= *req_cred
- ep
->cred_dist
.credits
;
550 ath6kl_credit_seek(target
->credit_info
, &ep
->cred_dist
);
552 ep
->cred_dist
.seek_cred
= 0;
554 if (ep
->cred_dist
.credits
< *req_cred
) {
555 ath6kl_dbg(ATH6KL_DBG_CREDIT
,
556 "credit not found for ep %d\n",
562 ep
->cred_dist
.credits
-= *req_cred
;
563 ep
->ep_st
.cred_cosumd
+= *req_cred
;
565 /* When we are getting low on credits, ask for more */
566 if (ep
->cred_dist
.credits
< ep
->cred_dist
.cred_per_msg
) {
567 ep
->cred_dist
.seek_cred
=
568 ep
->cred_dist
.cred_per_msg
- ep
->cred_dist
.credits
;
570 ath6kl_credit_seek(target
->credit_info
, &ep
->cred_dist
);
572 /* see if we were successful in getting more */
573 if (ep
->cred_dist
.credits
< ep
->cred_dist
.cred_per_msg
) {
574 /* tell the target we need credits ASAP! */
575 *flags
|= HTC_FLAGS_NEED_CREDIT_UPDATE
;
576 ep
->ep_st
.cred_low_indicate
+= 1;
577 ath6kl_dbg(ATH6KL_DBG_CREDIT
,
578 "credit we need credits asap\n");
585 static void ath6kl_htc_tx_pkts_get(struct htc_target
*target
,
586 struct htc_endpoint
*endpoint
,
587 struct list_head
*queue
)
591 struct htc_packet
*packet
;
598 if (list_empty(&endpoint
->txq
))
600 packet
= list_first_entry(&endpoint
->txq
, struct htc_packet
,
603 ath6kl_dbg(ATH6KL_DBG_HTC
,
604 "htc tx got packet 0x%p queue depth %d\n",
605 packet
, get_queue_depth(&endpoint
->txq
));
607 len
= CALC_TXRX_PADDED_LEN(target
,
608 packet
->act_len
+ HTC_HDR_LENGTH
);
610 if (htc_check_credits(target
, endpoint
, &flags
,
611 packet
->endpoint
, len
, &req_cred
))
614 /* now we can fully move onto caller's queue */
615 packet
= list_first_entry(&endpoint
->txq
, struct htc_packet
,
617 list_move_tail(&packet
->list
, queue
);
619 /* save the number of credits this packet consumed */
620 packet
->info
.tx
.cred_used
= req_cred
;
622 /* all TX packets are handled asynchronously */
623 packet
->completion
= htc_tx_comp_handler
;
624 packet
->context
= target
;
625 endpoint
->ep_st
.tx_issued
+= 1;
627 /* save send flags */
628 packet
->info
.tx
.flags
= flags
;
629 packet
->info
.tx
.seqno
= endpoint
->seqno
;
634 /* See if the padded tx length falls on a credit boundary */
635 static int htc_get_credit_padding(unsigned int cred_sz
, int *len
,
636 struct htc_endpoint
*ep
)
638 int rem_cred
, cred_pad
;
640 rem_cred
= *len
% cred_sz
;
642 /* No padding needed */
646 if (!(ep
->conn_flags
& HTC_FLGS_TX_BNDL_PAD_EN
))
650 * The transfer consumes a "partial" credit, this
651 * packet cannot be bundled unless we add
652 * additional "dummy" padding (max 255 bytes) to
653 * consume the entire credit.
655 cred_pad
= *len
< cred_sz
? (cred_sz
- *len
) : rem_cred
;
657 if ((cred_pad
> 0) && (cred_pad
<= 255))
660 /* The amount of padding is too large, send as non-bundled */
666 static int ath6kl_htc_tx_setup_scat_list(struct htc_target
*target
,
667 struct htc_endpoint
*endpoint
,
668 struct hif_scatter_req
*scat_req
,
670 struct list_head
*queue
)
672 struct htc_packet
*packet
;
673 int i
, len
, rem_scat
, cred_pad
;
677 rem_scat
= target
->max_tx_bndl_sz
;
679 for (i
= 0; i
< n_scat
; i
++) {
680 scat_req
->scat_list
[i
].packet
= NULL
;
682 if (list_empty(queue
))
685 packet
= list_first_entry(queue
, struct htc_packet
, list
);
686 len
= CALC_TXRX_PADDED_LEN(target
,
687 packet
->act_len
+ HTC_HDR_LENGTH
);
689 cred_pad
= htc_get_credit_padding(target
->tgt_cred_sz
,
691 if (cred_pad
< 0 || rem_scat
< len
) {
697 /* now remove it from the queue */
698 list_del(&packet
->list
);
700 scat_req
->scat_list
[i
].packet
= packet
;
701 /* prepare packet and flag message as part of a send bundle */
702 flags
= packet
->info
.tx
.flags
| HTC_FLAGS_SEND_BUNDLE
;
703 ath6kl_htc_tx_prep_pkt(packet
, flags
,
704 cred_pad
, packet
->info
.tx
.seqno
);
705 /* Make sure the buffer is 4-byte aligned */
706 ath6kl_htc_tx_buf_align(&packet
->buf
,
707 packet
->act_len
+ HTC_HDR_LENGTH
);
708 scat_req
->scat_list
[i
].buf
= packet
->buf
;
709 scat_req
->scat_list
[i
].len
= len
;
711 scat_req
->len
+= len
;
712 scat_req
->scat_entries
++;
713 ath6kl_dbg(ATH6KL_DBG_HTC
,
714 "htc tx adding (%d) pkt 0x%p seqno %d len %d remaining %d\n",
715 i
, packet
, packet
->info
.tx
.seqno
, len
, rem_scat
);
718 /* Roll back scatter setup in case of any failure */
719 if (scat_req
->scat_entries
< HTC_MIN_HTC_MSGS_TO_BUNDLE
) {
720 for (i
= scat_req
->scat_entries
- 1; i
>= 0; i
--) {
721 packet
= scat_req
->scat_list
[i
].packet
;
723 packet
->buf
+= HTC_HDR_LENGTH
;
724 list_add(&packet
->list
, queue
);
734 * Drain a queue and send as bundles this function may return without fully
735 * draining the queue when
737 * 1. scatter resources are exhausted
738 * 2. a message that will consume a partial credit will stop the
739 * bundling process early
740 * 3. we drop below the minimum number of messages for a bundle
742 static void ath6kl_htc_tx_bundle(struct htc_endpoint
*endpoint
,
743 struct list_head
*queue
,
744 int *sent_bundle
, int *n_bundle_pkts
)
746 struct htc_target
*target
= endpoint
->target
;
747 struct hif_scatter_req
*scat_req
= NULL
;
748 int n_scat
, n_sent_bundle
= 0, tot_pkts_bundle
= 0;
753 if ((HTC_CTRL_RSVD_SVC
!= endpoint
->svc_id
) ||
754 (WMI_CONTROL_SVC
!= endpoint
->svc_id
))
755 ac
= target
->dev
->ar
->ep2ac_map
[endpoint
->eid
];
759 n_scat
= get_queue_depth(queue
);
760 n_scat
= min(n_scat
, target
->msg_per_bndl_max
);
762 if (n_scat
< HTC_MIN_HTC_MSGS_TO_BUNDLE
)
763 /* not enough to bundle */
766 scat_req
= hif_scatter_req_get(target
->dev
->ar
);
769 /* no scatter resources */
770 ath6kl_dbg(ATH6KL_DBG_HTC
,
771 "htc tx no more scatter resources\n");
775 if ((ac
< WMM_NUM_AC
) && (ac
!= WMM_AC_BK
)) {
778 * BE, BK have priorities and bit
781 txb_mask
= (1 << WMM_AC_BK
);
784 * any AC with priority lower than
787 txb_mask
= ((1 << ac
) - 1);
789 * when the scatter request resources drop below a
790 * certain threshold, disable Tx bundling for all
791 * AC's with priority lower than the current requesting
792 * AC. Otherwise re-enable Tx bundling for them
794 if (scat_req
->scat_q_depth
< ATH6KL_SCATTER_REQS
)
795 target
->tx_bndl_mask
&= ~txb_mask
;
797 target
->tx_bndl_mask
|= txb_mask
;
800 ath6kl_dbg(ATH6KL_DBG_HTC
, "htc tx pkts to scatter: %d\n",
804 scat_req
->scat_entries
= 0;
806 status
= ath6kl_htc_tx_setup_scat_list(target
, endpoint
,
809 if (status
== -EAGAIN
) {
810 hif_scatter_req_add(target
->dev
->ar
, scat_req
);
814 /* send path is always asynchronous */
815 scat_req
->complete
= htc_async_tx_scat_complete
;
817 tot_pkts_bundle
+= scat_req
->scat_entries
;
819 ath6kl_dbg(ATH6KL_DBG_HTC
,
820 "htc tx scatter bytes %d entries %d\n",
821 scat_req
->len
, scat_req
->scat_entries
);
822 ath6kl_hif_submit_scat_req(target
->dev
, scat_req
, false);
828 *sent_bundle
= n_sent_bundle
;
829 *n_bundle_pkts
= tot_pkts_bundle
;
830 ath6kl_dbg(ATH6KL_DBG_HTC
, "htc tx bundle sent %d pkts\n",
836 static void ath6kl_htc_tx_from_queue(struct htc_target
*target
,
837 struct htc_endpoint
*endpoint
)
839 struct list_head txq
;
840 struct htc_packet
*packet
;
845 spin_lock_bh(&target
->tx_lock
);
847 endpoint
->tx_proc_cnt
++;
848 if (endpoint
->tx_proc_cnt
> 1) {
849 endpoint
->tx_proc_cnt
--;
850 spin_unlock_bh(&target
->tx_lock
);
851 ath6kl_dbg(ATH6KL_DBG_HTC
, "htc tx busy\n");
856 * drain the endpoint TX queue for transmission as long
857 * as we have enough credits.
859 INIT_LIST_HEAD(&txq
);
861 if ((HTC_CTRL_RSVD_SVC
!= endpoint
->svc_id
) ||
862 (WMI_CONTROL_SVC
!= endpoint
->svc_id
))
863 ac
= target
->dev
->ar
->ep2ac_map
[endpoint
->eid
];
867 if (list_empty(&endpoint
->txq
))
870 ath6kl_htc_tx_pkts_get(target
, endpoint
, &txq
);
872 if (list_empty(&txq
))
875 spin_unlock_bh(&target
->tx_lock
);
881 /* try to send a bundle on each pass */
882 if ((target
->tx_bndl_mask
) &&
883 (get_queue_depth(&txq
) >=
884 HTC_MIN_HTC_MSGS_TO_BUNDLE
)) {
885 int temp1
= 0, temp2
= 0;
887 /* check if bundling is enabled for an AC */
888 if (target
->tx_bndl_mask
& (1 << ac
)) {
889 ath6kl_htc_tx_bundle(endpoint
, &txq
,
891 bundle_sent
+= temp1
;
892 n_pkts_bundle
+= temp2
;
896 if (list_empty(&txq
))
899 packet
= list_first_entry(&txq
, struct htc_packet
,
901 list_del(&packet
->list
);
903 ath6kl_htc_tx_prep_pkt(packet
, packet
->info
.tx
.flags
,
904 0, packet
->info
.tx
.seqno
);
905 ath6kl_htc_tx_issue(target
, packet
);
908 spin_lock_bh(&target
->tx_lock
);
910 endpoint
->ep_st
.tx_bundles
+= bundle_sent
;
911 endpoint
->ep_st
.tx_pkt_bundled
+= n_pkts_bundle
;
914 * if an AC has bundling disabled and no tx bundling
915 * has occured continously for a certain number of TX,
916 * enable tx bundling for this AC
919 if (!(target
->tx_bndl_mask
& (1 << ac
)) &&
921 if (++target
->ac_tx_count
[ac
] >=
922 TX_RESUME_BUNDLE_THRESHOLD
) {
923 target
->ac_tx_count
[ac
] = 0;
924 target
->tx_bndl_mask
|= (1 << ac
);
928 /* tx bundling will reset the counter */
930 target
->ac_tx_count
[ac
] = 0;
934 endpoint
->tx_proc_cnt
= 0;
935 spin_unlock_bh(&target
->tx_lock
);
938 static bool ath6kl_htc_tx_try(struct htc_target
*target
,
939 struct htc_endpoint
*endpoint
,
940 struct htc_packet
*tx_pkt
)
942 struct htc_ep_callbacks ep_cb
;
944 bool overflow
= false;
946 ep_cb
= endpoint
->ep_cb
;
948 spin_lock_bh(&target
->tx_lock
);
949 txq_depth
= get_queue_depth(&endpoint
->txq
);
950 spin_unlock_bh(&target
->tx_lock
);
952 if (txq_depth
>= endpoint
->max_txq_depth
)
956 ath6kl_dbg(ATH6KL_DBG_HTC
,
957 "htc tx overflow ep %d depth %d max %d\n",
958 endpoint
->eid
, txq_depth
,
959 endpoint
->max_txq_depth
);
961 if (overflow
&& ep_cb
.tx_full
) {
962 if (ep_cb
.tx_full(endpoint
->target
, tx_pkt
) ==
963 HTC_SEND_FULL_DROP
) {
964 endpoint
->ep_st
.tx_dropped
+= 1;
969 spin_lock_bh(&target
->tx_lock
);
970 list_add_tail(&tx_pkt
->list
, &endpoint
->txq
);
971 spin_unlock_bh(&target
->tx_lock
);
973 ath6kl_htc_tx_from_queue(target
, endpoint
);
978 static void htc_chk_ep_txq(struct htc_target
*target
)
980 struct htc_endpoint
*endpoint
;
981 struct htc_endpoint_credit_dist
*cred_dist
;
984 * Run through the credit distribution list to see if there are
985 * packets queued. NOTE: no locks need to be taken since the
986 * distribution list is not dynamic (cannot be re-ordered) and we
987 * are not modifying any state.
989 list_for_each_entry(cred_dist
, &target
->cred_dist_list
, list
) {
990 endpoint
= cred_dist
->htc_ep
;
992 spin_lock_bh(&target
->tx_lock
);
993 if (!list_empty(&endpoint
->txq
)) {
994 ath6kl_dbg(ATH6KL_DBG_HTC
,
995 "htc creds ep %d credits %d pkts %d\n",
997 endpoint
->cred_dist
.credits
,
998 get_queue_depth(&endpoint
->txq
));
999 spin_unlock_bh(&target
->tx_lock
);
1001 * Try to start the stalled queue, this list is
1002 * ordered by priority. If there are credits
1003 * available the highest priority queue will get a
1004 * chance to reclaim credits from lower priority
1007 ath6kl_htc_tx_from_queue(target
, endpoint
);
1008 spin_lock_bh(&target
->tx_lock
);
1010 spin_unlock_bh(&target
->tx_lock
);
1014 static int htc_setup_tx_complete(struct htc_target
*target
)
1016 struct htc_packet
*send_pkt
= NULL
;
1019 send_pkt
= htc_get_control_buf(target
, true);
1024 if (target
->htc_tgt_ver
>= HTC_VERSION_2P1
) {
1025 struct htc_setup_comp_ext_msg
*setup_comp_ext
;
1029 (struct htc_setup_comp_ext_msg
*)send_pkt
->buf
;
1030 memset(setup_comp_ext
, 0, sizeof(*setup_comp_ext
));
1031 setup_comp_ext
->msg_id
=
1032 cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID
);
1034 if (target
->msg_per_bndl_max
> 0) {
1035 /* Indicate HTC bundling to the target */
1036 flags
|= HTC_SETUP_COMP_FLG_RX_BNDL_EN
;
1037 setup_comp_ext
->msg_per_rxbndl
=
1038 target
->msg_per_bndl_max
;
1041 memcpy(&setup_comp_ext
->flags
, &flags
,
1042 sizeof(setup_comp_ext
->flags
));
1043 set_htc_pkt_info(send_pkt
, NULL
, (u8
*) setup_comp_ext
,
1044 sizeof(struct htc_setup_comp_ext_msg
),
1045 ENDPOINT_0
, HTC_SERVICE_TX_PACKET_TAG
);
1048 struct htc_setup_comp_msg
*setup_comp
;
1049 setup_comp
= (struct htc_setup_comp_msg
*)send_pkt
->buf
;
1050 memset(setup_comp
, 0, sizeof(struct htc_setup_comp_msg
));
1051 setup_comp
->msg_id
= cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID
);
1052 set_htc_pkt_info(send_pkt
, NULL
, (u8
*) setup_comp
,
1053 sizeof(struct htc_setup_comp_msg
),
1054 ENDPOINT_0
, HTC_SERVICE_TX_PACKET_TAG
);
1057 /* we want synchronous operation */
1058 send_pkt
->completion
= NULL
;
1059 ath6kl_htc_tx_prep_pkt(send_pkt
, 0, 0, 0);
1060 status
= ath6kl_htc_tx_issue(target
, send_pkt
);
1062 if (send_pkt
!= NULL
)
1063 htc_reclaim_txctrl_buf(target
, send_pkt
);
1068 void ath6kl_htc_set_credit_dist(struct htc_target
*target
,
1069 struct ath6kl_htc_credit_info
*credit_info
,
1070 u16 srvc_pri_order
[], int list_len
)
1072 struct htc_endpoint
*endpoint
;
1075 target
->credit_info
= credit_info
;
1077 list_add_tail(&target
->endpoint
[ENDPOINT_0
].cred_dist
.list
,
1078 &target
->cred_dist_list
);
1080 for (i
= 0; i
< list_len
; i
++) {
1081 for (ep
= ENDPOINT_1
; ep
< ENDPOINT_MAX
; ep
++) {
1082 endpoint
= &target
->endpoint
[ep
];
1083 if (endpoint
->svc_id
== srvc_pri_order
[i
]) {
1084 list_add_tail(&endpoint
->cred_dist
.list
,
1085 &target
->cred_dist_list
);
1089 if (ep
>= ENDPOINT_MAX
) {
1096 int ath6kl_htc_tx(struct htc_target
*target
, struct htc_packet
*packet
)
1098 struct htc_endpoint
*endpoint
;
1099 struct list_head queue
;
1101 ath6kl_dbg(ATH6KL_DBG_HTC
,
1102 "htc tx ep id %d buf 0x%p len %d\n",
1103 packet
->endpoint
, packet
->buf
, packet
->act_len
);
1105 if (packet
->endpoint
>= ENDPOINT_MAX
) {
1110 endpoint
= &target
->endpoint
[packet
->endpoint
];
1112 if (!ath6kl_htc_tx_try(target
, endpoint
, packet
)) {
1113 packet
->status
= (target
->htc_flags
& HTC_OP_STATE_STOPPING
) ?
1114 -ECANCELED
: -ENOSPC
;
1115 INIT_LIST_HEAD(&queue
);
1116 list_add(&packet
->list
, &queue
);
1117 htc_tx_complete(endpoint
, &queue
);
1123 /* flush endpoint TX queue */
1124 void ath6kl_htc_flush_txep(struct htc_target
*target
,
1125 enum htc_endpoint_id eid
, u16 tag
)
1127 struct htc_packet
*packet
, *tmp_pkt
;
1128 struct list_head discard_q
, container
;
1129 struct htc_endpoint
*endpoint
= &target
->endpoint
[eid
];
1131 if (!endpoint
->svc_id
) {
1136 /* initialize the discard queue */
1137 INIT_LIST_HEAD(&discard_q
);
1139 spin_lock_bh(&target
->tx_lock
);
1141 list_for_each_entry_safe(packet
, tmp_pkt
, &endpoint
->txq
, list
) {
1142 if ((tag
== HTC_TX_PACKET_TAG_ALL
) ||
1143 (tag
== packet
->info
.tx
.tag
))
1144 list_move_tail(&packet
->list
, &discard_q
);
1147 spin_unlock_bh(&target
->tx_lock
);
1149 list_for_each_entry_safe(packet
, tmp_pkt
, &discard_q
, list
) {
1150 packet
->status
= -ECANCELED
;
1151 list_del(&packet
->list
);
1152 ath6kl_dbg(ATH6KL_DBG_HTC
,
1153 "htc tx flushing pkt 0x%p len %d ep %d tag 0x%x\n",
1154 packet
, packet
->act_len
,
1155 packet
->endpoint
, packet
->info
.tx
.tag
);
1157 INIT_LIST_HEAD(&container
);
1158 list_add_tail(&packet
->list
, &container
);
1159 htc_tx_complete(endpoint
, &container
);
1164 static void ath6kl_htc_flush_txep_all(struct htc_target
*target
)
1166 struct htc_endpoint
*endpoint
;
1169 dump_cred_dist_stats(target
);
1171 for (i
= ENDPOINT_0
; i
< ENDPOINT_MAX
; i
++) {
1172 endpoint
= &target
->endpoint
[i
];
1173 if (endpoint
->svc_id
== 0)
1176 ath6kl_htc_flush_txep(target
, i
, HTC_TX_PACKET_TAG_ALL
);
1180 void ath6kl_htc_indicate_activity_change(struct htc_target
*target
,
1181 enum htc_endpoint_id eid
, bool active
)
1183 struct htc_endpoint
*endpoint
= &target
->endpoint
[eid
];
1186 if (endpoint
->svc_id
== 0) {
1191 spin_lock_bh(&target
->tx_lock
);
1194 if (!(endpoint
->cred_dist
.dist_flags
& HTC_EP_ACTIVE
)) {
1195 endpoint
->cred_dist
.dist_flags
|= HTC_EP_ACTIVE
;
1199 if (endpoint
->cred_dist
.dist_flags
& HTC_EP_ACTIVE
) {
1200 endpoint
->cred_dist
.dist_flags
&= ~HTC_EP_ACTIVE
;
1206 endpoint
->cred_dist
.txq_depth
=
1207 get_queue_depth(&endpoint
->txq
);
1209 ath6kl_dbg(ATH6KL_DBG_HTC
,
1210 "htc tx activity ctxt 0x%p dist 0x%p\n",
1211 target
->credit_info
, &target
->cred_dist_list
);
1213 ath6kl_credit_distribute(target
->credit_info
,
1214 &target
->cred_dist_list
,
1215 HTC_CREDIT_DIST_ACTIVITY_CHANGE
);
1218 spin_unlock_bh(&target
->tx_lock
);
1220 if (dist
&& !active
)
1221 htc_chk_ep_txq(target
);
1226 static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint
*endpoint
,
1229 endpoint
->ep_st
.rx_pkts
++;
1230 if (n_look_ahds
== 1)
1231 endpoint
->ep_st
.rx_lkahds
++;
1232 else if (n_look_ahds
> 1)
1233 endpoint
->ep_st
.rx_bundle_lkahd
++;
1236 static inline bool htc_valid_rx_frame_len(struct htc_target
*target
,
1237 enum htc_endpoint_id eid
, int len
)
1239 return (eid
== target
->dev
->ar
->ctrl_ep
) ?
1240 len
<= ATH6KL_BUFFER_SIZE
: len
<= ATH6KL_AMSDU_BUFFER_SIZE
;
1243 static int htc_add_rxbuf(struct htc_target
*target
, struct htc_packet
*packet
)
1245 struct list_head queue
;
1247 INIT_LIST_HEAD(&queue
);
1248 list_add_tail(&packet
->list
, &queue
);
1249 return ath6kl_htc_add_rxbuf_multiple(target
, &queue
);
1252 static void htc_reclaim_rxbuf(struct htc_target
*target
,
1253 struct htc_packet
*packet
,
1254 struct htc_endpoint
*ep
)
1256 if (packet
->info
.rx
.rx_flags
& HTC_RX_PKT_NO_RECYCLE
) {
1257 htc_rxpkt_reset(packet
);
1258 packet
->status
= -ECANCELED
;
1259 ep
->ep_cb
.rx(ep
->target
, packet
);
1261 htc_rxpkt_reset(packet
);
1262 htc_add_rxbuf((void *)(target
), packet
);
1266 static void reclaim_rx_ctrl_buf(struct htc_target
*target
,
1267 struct htc_packet
*packet
)
1269 spin_lock_bh(&target
->htc_lock
);
1270 list_add_tail(&packet
->list
, &target
->free_ctrl_rxbuf
);
1271 spin_unlock_bh(&target
->htc_lock
);
1274 static int ath6kl_htc_rx_packet(struct htc_target
*target
,
1275 struct htc_packet
*packet
,
1278 struct ath6kl_device
*dev
= target
->dev
;
1282 padded_len
= CALC_TXRX_PADDED_LEN(target
, rx_len
);
1284 if (padded_len
> packet
->buf_len
) {
1285 ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
1286 padded_len
, rx_len
, packet
->buf_len
);
1290 ath6kl_dbg(ATH6KL_DBG_HTC
,
1291 "htc rx 0x%p hdr x%x len %d mbox 0x%x\n",
1292 packet
, packet
->info
.rx
.exp_hdr
,
1293 padded_len
, dev
->ar
->mbox_info
.htc_addr
);
1295 status
= hif_read_write_sync(dev
->ar
,
1296 dev
->ar
->mbox_info
.htc_addr
,
1297 packet
->buf
, padded_len
,
1298 HIF_RD_SYNC_BLOCK_FIX
);
1300 packet
->status
= status
;
1306 * optimization for recv packets, we can indicate a
1307 * "hint" that there are more single-packets to fetch
1310 static void ath6kl_htc_rx_set_indicate(u32 lk_ahd
,
1311 struct htc_endpoint
*endpoint
,
1312 struct htc_packet
*packet
)
1314 struct htc_frame_hdr
*htc_hdr
= (struct htc_frame_hdr
*)&lk_ahd
;
1316 if (htc_hdr
->eid
== packet
->endpoint
) {
1317 if (!list_empty(&endpoint
->rx_bufq
))
1318 packet
->info
.rx
.indicat_flags
|=
1319 HTC_RX_FLAGS_INDICATE_MORE_PKTS
;
1323 static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint
*endpoint
)
1325 struct htc_ep_callbacks ep_cb
= endpoint
->ep_cb
;
1327 if (ep_cb
.rx_refill_thresh
> 0) {
1328 spin_lock_bh(&endpoint
->target
->rx_lock
);
1329 if (get_queue_depth(&endpoint
->rx_bufq
)
1330 < ep_cb
.rx_refill_thresh
) {
1331 spin_unlock_bh(&endpoint
->target
->rx_lock
);
1332 ep_cb
.rx_refill(endpoint
->target
, endpoint
->eid
);
1335 spin_unlock_bh(&endpoint
->target
->rx_lock
);
1339 /* This function is called with rx_lock held */
1340 static int ath6kl_htc_rx_setup(struct htc_target
*target
,
1341 struct htc_endpoint
*ep
,
1342 u32
*lk_ahds
, struct list_head
*queue
, int n_msg
)
1344 struct htc_packet
*packet
;
1345 /* FIXME: type of lk_ahds can't be right */
1346 struct htc_frame_hdr
*htc_hdr
= (struct htc_frame_hdr
*)lk_ahds
;
1347 struct htc_ep_callbacks ep_cb
;
1348 int status
= 0, j
, full_len
;
1351 full_len
= CALC_TXRX_PADDED_LEN(target
,
1352 le16_to_cpu(htc_hdr
->payld_len
) +
1355 if (!htc_valid_rx_frame_len(target
, ep
->eid
, full_len
)) {
1356 ath6kl_warn("Rx buffer requested with invalid length\n");
1361 for (j
= 0; j
< n_msg
; j
++) {
1364 * Reset flag, any packets allocated using the
1365 * rx_alloc() API cannot be recycled on
1366 * cleanup,they must be explicitly returned.
1370 if (ep_cb
.rx_allocthresh
&&
1371 (full_len
> ep_cb
.rx_alloc_thresh
)) {
1372 ep
->ep_st
.rx_alloc_thresh_hit
+= 1;
1373 ep
->ep_st
.rxalloc_thresh_byte
+=
1374 le16_to_cpu(htc_hdr
->payld_len
);
1376 spin_unlock_bh(&target
->rx_lock
);
1379 packet
= ep_cb
.rx_allocthresh(ep
->target
, ep
->eid
,
1381 spin_lock_bh(&target
->rx_lock
);
1383 /* refill handler is being used */
1384 if (list_empty(&ep
->rx_bufq
)) {
1385 if (ep_cb
.rx_refill
) {
1386 spin_unlock_bh(&target
->rx_lock
);
1387 ep_cb
.rx_refill(ep
->target
, ep
->eid
);
1388 spin_lock_bh(&target
->rx_lock
);
1392 if (list_empty(&ep
->rx_bufq
))
1395 packet
= list_first_entry(&ep
->rx_bufq
,
1396 struct htc_packet
, list
);
1397 list_del(&packet
->list
);
1402 target
->rx_st_flags
|= HTC_RECV_WAIT_BUFFERS
;
1403 target
->ep_waiting
= ep
->eid
;
1408 packet
->info
.rx
.rx_flags
= 0;
1409 packet
->info
.rx
.indicat_flags
= 0;
1414 * flag that these packets cannot be
1415 * recycled, they have to be returned to
1418 packet
->info
.rx
.rx_flags
|= HTC_RX_PKT_NO_RECYCLE
;
1420 /* Caller needs to free this upon any failure */
1421 list_add_tail(&packet
->list
, queue
);
1423 if (target
->htc_flags
& HTC_OP_STATE_STOPPING
) {
1424 status
= -ECANCELED
;
1429 packet
->info
.rx
.rx_flags
|= HTC_RX_PKT_REFRESH_HDR
;
1430 packet
->info
.rx
.exp_hdr
= 0xFFFFFFFF;
1432 /* set expected look ahead */
1433 packet
->info
.rx
.exp_hdr
= *lk_ahds
;
1435 packet
->act_len
= le16_to_cpu(htc_hdr
->payld_len
) +
1442 static int ath6kl_htc_rx_alloc(struct htc_target
*target
,
1443 u32 lk_ahds
[], int msg
,
1444 struct htc_endpoint
*endpoint
,
1445 struct list_head
*queue
)
1448 struct htc_packet
*packet
, *tmp_pkt
;
1449 struct htc_frame_hdr
*htc_hdr
;
1452 spin_lock_bh(&target
->rx_lock
);
1454 for (i
= 0; i
< msg
; i
++) {
1456 htc_hdr
= (struct htc_frame_hdr
*)&lk_ahds
[i
];
1458 if (htc_hdr
->eid
>= ENDPOINT_MAX
) {
1459 ath6kl_err("invalid ep in look-ahead: %d\n",
1465 if (htc_hdr
->eid
!= endpoint
->eid
) {
1466 ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
1467 htc_hdr
->eid
, endpoint
->eid
, i
);
1472 if (le16_to_cpu(htc_hdr
->payld_len
) > HTC_MAX_PAYLOAD_LENGTH
) {
1473 ath6kl_err("payload len %d exceeds max htc : %d !\n",
1475 (u32
) HTC_MAX_PAYLOAD_LENGTH
);
1480 if (endpoint
->svc_id
== 0) {
1481 ath6kl_err("ep %d is not connected !\n", htc_hdr
->eid
);
1486 if (htc_hdr
->flags
& HTC_FLG_RX_BNDL_CNT
) {
1488 * HTC header indicates that every packet to follow
1489 * has the same padded length so that it can be
1490 * optimally fetched as a full bundle.
1492 n_msg
= (htc_hdr
->flags
& HTC_FLG_RX_BNDL_CNT
) >>
1493 HTC_FLG_RX_BNDL_CNT_S
;
1495 /* the count doesn't include the starter frame */
1497 if (n_msg
> target
->msg_per_bndl_max
) {
1502 endpoint
->ep_st
.rx_bundle_from_hdr
+= 1;
1503 ath6kl_dbg(ATH6KL_DBG_HTC
,
1504 "htc rx bundle pkts %d\n",
1507 /* HTC header only indicates 1 message to fetch */
1510 /* Setup packet buffers for each message */
1511 status
= ath6kl_htc_rx_setup(target
, endpoint
, &lk_ahds
[i
],
1515 * This is due to unavailabilty of buffers to rx entire data.
1516 * Return no error so that free buffers from queue can be used
1517 * to receive partial data.
1519 if (status
== -ENOSPC
) {
1520 spin_unlock_bh(&target
->rx_lock
);
1528 spin_unlock_bh(&target
->rx_lock
);
1531 list_for_each_entry_safe(packet
, tmp_pkt
, queue
, list
) {
1532 list_del(&packet
->list
);
1533 htc_reclaim_rxbuf(target
, packet
,
1534 &target
->endpoint
[packet
->endpoint
]);
1541 static void htc_ctrl_rx(struct htc_target
*context
, struct htc_packet
*packets
)
1543 if (packets
->endpoint
!= ENDPOINT_0
) {
1548 if (packets
->status
== -ECANCELED
) {
1549 reclaim_rx_ctrl_buf(context
, packets
);
1553 if (packets
->act_len
> 0) {
1554 ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
1555 packets
->act_len
+ HTC_HDR_LENGTH
);
1557 ath6kl_dbg_dump(ATH6KL_DBG_HTC
,
1558 "htc rx unexpected endpoint 0 message", "",
1559 packets
->buf
- HTC_HDR_LENGTH
,
1560 packets
->act_len
+ HTC_HDR_LENGTH
);
1563 htc_reclaim_rxbuf(context
, packets
, &context
->endpoint
[0]);
1566 static void htc_proc_cred_rpt(struct htc_target
*target
,
1567 struct htc_credit_report
*rpt
,
1569 enum htc_endpoint_id from_ep
)
1571 struct htc_endpoint
*endpoint
;
1572 int tot_credits
= 0, i
;
1575 spin_lock_bh(&target
->tx_lock
);
1577 for (i
= 0; i
< n_entries
; i
++, rpt
++) {
1578 if (rpt
->eid
>= ENDPOINT_MAX
) {
1580 spin_unlock_bh(&target
->tx_lock
);
1584 endpoint
= &target
->endpoint
[rpt
->eid
];
1586 ath6kl_dbg(ATH6KL_DBG_CREDIT
,
1587 "credit report ep %d credits %d\n",
1588 rpt
->eid
, rpt
->credits
);
1590 endpoint
->ep_st
.tx_cred_rpt
+= 1;
1591 endpoint
->ep_st
.cred_retnd
+= rpt
->credits
;
1593 if (from_ep
== rpt
->eid
) {
1595 * This credit report arrived on the same endpoint
1596 * indicating it arrived in an RX packet.
1598 endpoint
->ep_st
.cred_from_rx
+= rpt
->credits
;
1599 endpoint
->ep_st
.cred_rpt_from_rx
+= 1;
1600 } else if (from_ep
== ENDPOINT_0
) {
1601 /* credit arrived on endpoint 0 as a NULL message */
1602 endpoint
->ep_st
.cred_from_ep0
+= rpt
->credits
;
1603 endpoint
->ep_st
.cred_rpt_ep0
+= 1;
1605 endpoint
->ep_st
.cred_from_other
+= rpt
->credits
;
1606 endpoint
->ep_st
.cred_rpt_from_other
+= 1;
1609 if (rpt
->eid
== ENDPOINT_0
)
1610 /* always give endpoint 0 credits back */
1611 endpoint
->cred_dist
.credits
+= rpt
->credits
;
1613 endpoint
->cred_dist
.cred_to_dist
+= rpt
->credits
;
1618 * Refresh tx depth for distribution function that will
1619 * recover these credits NOTE: this is only valid when
1620 * there are credits to recover!
1622 endpoint
->cred_dist
.txq_depth
=
1623 get_queue_depth(&endpoint
->txq
);
1625 tot_credits
+= rpt
->credits
;
1630 * This was a credit return based on a completed send
1631 * operations note, this is done with the lock held
1633 ath6kl_credit_distribute(target
->credit_info
,
1634 &target
->cred_dist_list
,
1635 HTC_CREDIT_DIST_SEND_COMPLETE
);
1638 spin_unlock_bh(&target
->tx_lock
);
1641 htc_chk_ep_txq(target
);
1644 static int htc_parse_trailer(struct htc_target
*target
,
1645 struct htc_record_hdr
*record
,
1646 u8
*record_buf
, u32
*next_lk_ahds
,
1647 enum htc_endpoint_id endpoint
,
1650 struct htc_bundle_lkahd_rpt
*bundle_lkahd_rpt
;
1651 struct htc_lookahead_report
*lk_ahd
;
1654 switch (record
->rec_id
) {
1655 case HTC_RECORD_CREDITS
:
1656 len
= record
->len
/ sizeof(struct htc_credit_report
);
1662 htc_proc_cred_rpt(target
,
1663 (struct htc_credit_report
*) record_buf
,
1666 case HTC_RECORD_LOOKAHEAD
:
1667 len
= record
->len
/ sizeof(*lk_ahd
);
1673 lk_ahd
= (struct htc_lookahead_report
*) record_buf
;
1674 if ((lk_ahd
->pre_valid
== ((~lk_ahd
->post_valid
) & 0xFF)) &&
1677 ath6kl_dbg(ATH6KL_DBG_HTC
,
1678 "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
1679 lk_ahd
->pre_valid
, lk_ahd
->post_valid
);
1681 /* look ahead bytes are valid, copy them over */
1682 memcpy((u8
*)&next_lk_ahds
[0], lk_ahd
->lk_ahd
, 4);
1684 ath6kl_dbg_dump(ATH6KL_DBG_HTC
,
1685 "htc rx next look ahead",
1686 "", next_lk_ahds
, 4);
1691 case HTC_RECORD_LOOKAHEAD_BUNDLE
:
1692 len
= record
->len
/ sizeof(*bundle_lkahd_rpt
);
1693 if (!len
|| (len
> HTC_HOST_MAX_MSG_PER_BUNDLE
)) {
1702 (struct htc_bundle_lkahd_rpt
*) record_buf
;
1704 ath6kl_dbg_dump(ATH6KL_DBG_HTC
, "htc rx bundle lk_ahd",
1705 "", record_buf
, record
->len
);
1707 for (i
= 0; i
< len
; i
++) {
1708 memcpy((u8
*)&next_lk_ahds
[i
],
1709 bundle_lkahd_rpt
->lk_ahd
, 4);
1717 ath6kl_err("unhandled record: id:%d len:%d\n",
1718 record
->rec_id
, record
->len
);
1726 static int htc_proc_trailer(struct htc_target
*target
,
1727 u8
*buf
, int len
, u32
*next_lk_ahds
,
1728 int *n_lk_ahds
, enum htc_endpoint_id endpoint
)
1730 struct htc_record_hdr
*record
;
1736 ath6kl_dbg(ATH6KL_DBG_HTC
, "htc rx trailer len %d\n", len
);
1737 ath6kl_dbg_dump(ATH6KL_DBG_HTC
, NULL
, "", buf
, len
);
1745 if (len
< sizeof(struct htc_record_hdr
)) {
1749 /* these are byte aligned structs */
1750 record
= (struct htc_record_hdr
*) buf
;
1751 len
-= sizeof(struct htc_record_hdr
);
1752 buf
+= sizeof(struct htc_record_hdr
);
1754 if (record
->len
> len
) {
1755 ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
1756 record
->len
, record
->rec_id
, len
);
1762 status
= htc_parse_trailer(target
, record
, record_buf
,
1763 next_lk_ahds
, endpoint
, n_lk_ahds
);
1768 /* advance buffer past this record for next time around */
1774 ath6kl_dbg_dump(ATH6KL_DBG_HTC
, "htc rx bad trailer",
1775 "", orig_buf
, orig_len
);
1780 static int ath6kl_htc_rx_process_hdr(struct htc_target
*target
,
1781 struct htc_packet
*packet
,
1782 u32
*next_lkahds
, int *n_lkahds
)
1787 struct htc_frame_hdr
*htc_hdr
= (struct htc_frame_hdr
*)packet
->buf
;
1789 if (n_lkahds
!= NULL
)
1793 * NOTE: we cannot assume the alignment of buf, so we use the safe
1794 * macros to retrieve 16 bit fields.
1796 payload_len
= le16_to_cpu(get_unaligned(&htc_hdr
->payld_len
));
1798 memcpy((u8
*)&lk_ahd
, packet
->buf
, sizeof(lk_ahd
));
1800 if (packet
->info
.rx
.rx_flags
& HTC_RX_PKT_REFRESH_HDR
) {
1802 * Refresh the expected header and the actual length as it
1803 * was unknown when this packet was grabbed as part of the
1806 packet
->info
.rx
.exp_hdr
= lk_ahd
;
1807 packet
->act_len
= payload_len
+ HTC_HDR_LENGTH
;
1809 /* validate the actual header that was refreshed */
1810 if (packet
->act_len
> packet
->buf_len
) {
1811 ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
1812 payload_len
, lk_ahd
);
1814 * Limit this to max buffer just to print out some
1817 packet
->act_len
= min(packet
->act_len
, packet
->buf_len
);
1822 if (packet
->endpoint
!= htc_hdr
->eid
) {
1823 ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
1824 htc_hdr
->eid
, packet
->endpoint
);
1830 if (lk_ahd
!= packet
->info
.rx
.exp_hdr
) {
1831 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1832 __func__
, packet
, packet
->info
.rx
.rx_flags
);
1833 ath6kl_dbg_dump(ATH6KL_DBG_HTC
, "htc rx expected lk_ahd",
1834 "", &packet
->info
.rx
.exp_hdr
, 4);
1835 ath6kl_dbg_dump(ATH6KL_DBG_HTC
, "htc rx current header",
1836 "", (u8
*)&lk_ahd
, sizeof(lk_ahd
));
1841 if (htc_hdr
->flags
& HTC_FLG_RX_TRAILER
) {
1842 if (htc_hdr
->ctrl
[0] < sizeof(struct htc_record_hdr
) ||
1843 htc_hdr
->ctrl
[0] > payload_len
) {
1844 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1845 __func__
, payload_len
, htc_hdr
->ctrl
[0]);
1850 if (packet
->info
.rx
.rx_flags
& HTC_RX_PKT_IGNORE_LOOKAHEAD
) {
1855 status
= htc_proc_trailer(target
, packet
->buf
+ HTC_HDR_LENGTH
1856 + payload_len
- htc_hdr
->ctrl
[0],
1857 htc_hdr
->ctrl
[0], next_lkahds
,
1858 n_lkahds
, packet
->endpoint
);
1863 packet
->act_len
-= htc_hdr
->ctrl
[0];
1866 packet
->buf
+= HTC_HDR_LENGTH
;
1867 packet
->act_len
-= HTC_HDR_LENGTH
;
1871 ath6kl_dbg_dump(ATH6KL_DBG_HTC
, "htc rx bad packet",
1872 "", packet
->buf
, packet
->act_len
);
1877 static void ath6kl_htc_rx_complete(struct htc_endpoint
*endpoint
,
1878 struct htc_packet
*packet
)
1880 ath6kl_dbg(ATH6KL_DBG_HTC
,
1881 "htc rx complete ep %d packet 0x%p\n",
1882 endpoint
->eid
, packet
);
1883 endpoint
->ep_cb
.rx(endpoint
->target
, packet
);
1886 static int ath6kl_htc_rx_bundle(struct htc_target
*target
,
1887 struct list_head
*rxq
,
1888 struct list_head
*sync_compq
,
1889 int *n_pkt_fetched
, bool part_bundle
)
1891 struct hif_scatter_req
*scat_req
;
1892 struct htc_packet
*packet
;
1893 int rem_space
= target
->max_rx_bndl_sz
;
1894 int n_scat_pkt
, status
= 0, i
, len
;
1896 n_scat_pkt
= get_queue_depth(rxq
);
1897 n_scat_pkt
= min(n_scat_pkt
, target
->msg_per_bndl_max
);
1899 if ((get_queue_depth(rxq
) - n_scat_pkt
) > 0) {
1901 * We were forced to split this bundle receive operation
1902 * all packets in this partial bundle must have their
1903 * lookaheads ignored.
1908 * This would only happen if the target ignored our max
1911 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1912 __func__
, get_queue_depth(rxq
), n_scat_pkt
);
1917 ath6kl_dbg(ATH6KL_DBG_HTC
,
1918 "htc rx bundle depth %d pkts %d\n",
1919 get_queue_depth(rxq
), n_scat_pkt
);
1921 scat_req
= hif_scatter_req_get(target
->dev
->ar
);
1923 if (scat_req
== NULL
)
1926 for (i
= 0; i
< n_scat_pkt
; i
++) {
1929 packet
= list_first_entry(rxq
, struct htc_packet
, list
);
1930 list_del(&packet
->list
);
1932 pad_len
= CALC_TXRX_PADDED_LEN(target
,
1935 if ((rem_space
- pad_len
) < 0) {
1936 list_add(&packet
->list
, rxq
);
1940 rem_space
-= pad_len
;
1942 if (part_bundle
|| (i
< (n_scat_pkt
- 1)))
1944 * Packet 0..n-1 cannot be checked for look-aheads
1945 * since we are fetching a bundle the last packet
1946 * however can have it's lookahead used
1948 packet
->info
.rx
.rx_flags
|=
1949 HTC_RX_PKT_IGNORE_LOOKAHEAD
;
1951 /* NOTE: 1 HTC packet per scatter entry */
1952 scat_req
->scat_list
[i
].buf
= packet
->buf
;
1953 scat_req
->scat_list
[i
].len
= pad_len
;
1955 packet
->info
.rx
.rx_flags
|= HTC_RX_PKT_PART_OF_BUNDLE
;
1957 list_add_tail(&packet
->list
, sync_compq
);
1959 WARN_ON(!scat_req
->scat_list
[i
].len
);
1960 len
+= scat_req
->scat_list
[i
].len
;
1963 scat_req
->len
= len
;
1964 scat_req
->scat_entries
= i
;
1966 status
= ath6kl_hif_submit_scat_req(target
->dev
, scat_req
, true);
1971 /* free scatter request */
1972 hif_scatter_req_add(target
->dev
->ar
, scat_req
);
1979 static int ath6kl_htc_rx_process_packets(struct htc_target
*target
,
1980 struct list_head
*comp_pktq
,
1984 struct htc_packet
*packet
, *tmp_pkt
;
1985 struct htc_endpoint
*ep
;
1988 list_for_each_entry_safe(packet
, tmp_pkt
, comp_pktq
, list
) {
1989 ep
= &target
->endpoint
[packet
->endpoint
];
1991 /* process header for each of the recv packet */
1992 status
= ath6kl_htc_rx_process_hdr(target
, packet
, lk_ahds
,
1997 list_del(&packet
->list
);
1999 if (list_empty(comp_pktq
)) {
2001 * Last packet's more packet flag is set
2002 * based on the lookahead.
2005 ath6kl_htc_rx_set_indicate(lk_ahds
[0],
2009 * Packets in a bundle automatically have
2012 packet
->info
.rx
.indicat_flags
|=
2013 HTC_RX_FLAGS_INDICATE_MORE_PKTS
;
2015 ath6kl_htc_rx_update_stats(ep
, *n_lk_ahd
);
2017 if (packet
->info
.rx
.rx_flags
& HTC_RX_PKT_PART_OF_BUNDLE
)
2018 ep
->ep_st
.rx_bundl
+= 1;
2020 ath6kl_htc_rx_complete(ep
, packet
);
2026 static int ath6kl_htc_rx_fetch(struct htc_target
*target
,
2027 struct list_head
*rx_pktq
,
2028 struct list_head
*comp_pktq
)
2031 bool part_bundle
= false;
2033 struct list_head tmp_rxq
;
2034 struct htc_packet
*packet
, *tmp_pkt
;
2036 /* now go fetch the list of HTC packets */
2037 while (!list_empty(rx_pktq
)) {
2040 INIT_LIST_HEAD(&tmp_rxq
);
2042 if (target
->rx_bndl_enable
&& (get_queue_depth(rx_pktq
) > 1)) {
2044 * There are enough packets to attempt a
2045 * bundle transfer and recv bundling is
2048 status
= ath6kl_htc_rx_bundle(target
, rx_pktq
,
2055 if (!list_empty(rx_pktq
))
2058 list_splice_tail_init(&tmp_rxq
, comp_pktq
);
2061 if (!fetched_pkts
) {
2063 packet
= list_first_entry(rx_pktq
, struct htc_packet
,
2066 /* fully synchronous */
2067 packet
->completion
= NULL
;
2069 if (!list_is_singular(rx_pktq
))
2071 * look_aheads in all packet
2072 * except the last one in the
2073 * bundle must be ignored
2075 packet
->info
.rx
.rx_flags
|=
2076 HTC_RX_PKT_IGNORE_LOOKAHEAD
;
2078 /* go fetch the packet */
2079 status
= ath6kl_htc_rx_packet(target
, packet
,
2082 list_move_tail(&packet
->list
, &tmp_rxq
);
2087 list_splice_tail_init(&tmp_rxq
, comp_pktq
);
2096 * Cleanup any packets we allocated but didn't use to
2097 * actually fetch any packets.
2100 list_for_each_entry_safe(packet
, tmp_pkt
, rx_pktq
, list
) {
2101 list_del(&packet
->list
);
2102 htc_reclaim_rxbuf(target
, packet
,
2103 &target
->endpoint
[packet
->endpoint
]);
2106 list_for_each_entry_safe(packet
, tmp_pkt
, &tmp_rxq
, list
) {
2107 list_del(&packet
->list
);
2108 htc_reclaim_rxbuf(target
, packet
,
2109 &target
->endpoint
[packet
->endpoint
]);
2115 int ath6kl_htc_rxmsg_pending_handler(struct htc_target
*target
,
2116 u32 msg_look_ahead
, int *num_pkts
)
2118 struct htc_packet
*packets
, *tmp_pkt
;
2119 struct htc_endpoint
*endpoint
;
2120 struct list_head rx_pktq
, comp_pktq
;
2122 u32 look_aheads
[HTC_HOST_MAX_MSG_PER_BUNDLE
];
2123 int num_look_ahead
= 1;
2124 enum htc_endpoint_id id
;
2127 INIT_LIST_HEAD(&comp_pktq
);
2131 * On first entry copy the look_aheads into our temp array for
2134 look_aheads
[0] = msg_look_ahead
;
2139 * First lookahead sets the expected endpoint IDs for all
2140 * packets in a bundle.
2142 id
= ((struct htc_frame_hdr
*)&look_aheads
[0])->eid
;
2143 endpoint
= &target
->endpoint
[id
];
2145 if (id
>= ENDPOINT_MAX
) {
2146 ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
2152 INIT_LIST_HEAD(&rx_pktq
);
2153 INIT_LIST_HEAD(&comp_pktq
);
2156 * Try to allocate as many HTC RX packets indicated by the
2159 status
= ath6kl_htc_rx_alloc(target
, look_aheads
,
2160 num_look_ahead
, endpoint
,
2165 if (get_queue_depth(&rx_pktq
) >= 2)
2167 * A recv bundle was detected, force IRQ status
2170 target
->chk_irq_status_cnt
= 1;
2172 n_fetched
+= get_queue_depth(&rx_pktq
);
2176 status
= ath6kl_htc_rx_fetch(target
, &rx_pktq
, &comp_pktq
);
2179 ath6kl_htc_rx_chk_water_mark(endpoint
);
2181 /* Process fetched packets */
2182 status
= ath6kl_htc_rx_process_packets(target
, &comp_pktq
,
2186 if (!num_look_ahead
|| status
)
2190 * For SYNCH processing, if we get here, we are running
2191 * through the loop again due to a detected lookahead. Set
2192 * flag that we should re-check IRQ status registers again
2193 * before leaving IRQ processing, this can net better
2194 * performance in high throughput situations.
2196 target
->chk_irq_status_cnt
= 1;
2200 ath6kl_err("failed to get pending recv messages: %d\n",
2203 /* cleanup any packets in sync completion queue */
2204 list_for_each_entry_safe(packets
, tmp_pkt
, &comp_pktq
, list
) {
2205 list_del(&packets
->list
);
2206 htc_reclaim_rxbuf(target
, packets
,
2207 &target
->endpoint
[packets
->endpoint
]);
2210 if (target
->htc_flags
& HTC_OP_STATE_STOPPING
) {
2211 ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
2212 ath6kl_hif_rx_control(target
->dev
, false);
2217 * Before leaving, check to see if host ran out of buffers and
2218 * needs to stop the receiver.
2220 if (target
->rx_st_flags
& HTC_RECV_WAIT_BUFFERS
) {
2221 ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
2222 ath6kl_hif_rx_control(target
->dev
, false);
2224 *num_pkts
= n_fetched
;
2230 * Synchronously wait for a control message from the target,
2231 * This function is used at initialization time ONLY. At init messages
2232 * on ENDPOINT 0 are expected.
2234 static struct htc_packet
*htc_wait_for_ctrl_msg(struct htc_target
*target
)
2236 struct htc_packet
*packet
= NULL
;
2237 struct htc_frame_hdr
*htc_hdr
;
2240 if (ath6kl_hif_poll_mboxmsg_rx(target
->dev
, &look_ahead
,
2241 HTC_TARGET_RESPONSE_TIMEOUT
))
2244 ath6kl_dbg(ATH6KL_DBG_HTC
,
2245 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead
);
2247 htc_hdr
= (struct htc_frame_hdr
*)&look_ahead
;
2249 if (htc_hdr
->eid
!= ENDPOINT_0
)
2252 packet
= htc_get_control_buf(target
, false);
2257 packet
->info
.rx
.rx_flags
= 0;
2258 packet
->info
.rx
.exp_hdr
= look_ahead
;
2259 packet
->act_len
= le16_to_cpu(htc_hdr
->payld_len
) + HTC_HDR_LENGTH
;
2261 if (packet
->act_len
> packet
->buf_len
)
2264 /* we want synchronous operation */
2265 packet
->completion
= NULL
;
2267 /* get the message from the device, this will block */
2268 if (ath6kl_htc_rx_packet(target
, packet
, packet
->act_len
))
2271 /* process receive header */
2272 packet
->status
= ath6kl_htc_rx_process_hdr(target
, packet
, NULL
, NULL
);
2274 if (packet
->status
) {
2275 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
2283 if (packet
!= NULL
) {
2284 htc_rxpkt_reset(packet
);
2285 reclaim_rx_ctrl_buf(target
, packet
);
2291 int ath6kl_htc_add_rxbuf_multiple(struct htc_target
*target
,
2292 struct list_head
*pkt_queue
)
2294 struct htc_endpoint
*endpoint
;
2295 struct htc_packet
*first_pkt
;
2296 bool rx_unblock
= false;
2297 int status
= 0, depth
;
2299 if (list_empty(pkt_queue
))
2302 first_pkt
= list_first_entry(pkt_queue
, struct htc_packet
, list
);
2304 if (first_pkt
->endpoint
>= ENDPOINT_MAX
)
2307 depth
= get_queue_depth(pkt_queue
);
2309 ath6kl_dbg(ATH6KL_DBG_HTC
,
2310 "htc rx add multiple ep id %d cnt %d len %d\n",
2311 first_pkt
->endpoint
, depth
, first_pkt
->buf_len
);
2313 endpoint
= &target
->endpoint
[first_pkt
->endpoint
];
2315 if (target
->htc_flags
& HTC_OP_STATE_STOPPING
) {
2316 struct htc_packet
*packet
, *tmp_pkt
;
2318 /* walk through queue and mark each one canceled */
2319 list_for_each_entry_safe(packet
, tmp_pkt
, pkt_queue
, list
) {
2320 packet
->status
= -ECANCELED
;
2321 list_del(&packet
->list
);
2322 ath6kl_htc_rx_complete(endpoint
, packet
);
2328 spin_lock_bh(&target
->rx_lock
);
2330 list_splice_tail_init(pkt_queue
, &endpoint
->rx_bufq
);
2332 /* check if we are blocked waiting for a new buffer */
2333 if (target
->rx_st_flags
& HTC_RECV_WAIT_BUFFERS
) {
2334 if (target
->ep_waiting
== first_pkt
->endpoint
) {
2335 ath6kl_dbg(ATH6KL_DBG_HTC
,
2336 "htc rx blocked on ep %d, unblocking\n",
2337 target
->ep_waiting
);
2338 target
->rx_st_flags
&= ~HTC_RECV_WAIT_BUFFERS
;
2339 target
->ep_waiting
= ENDPOINT_MAX
;
2344 spin_unlock_bh(&target
->rx_lock
);
2346 if (rx_unblock
&& !(target
->htc_flags
& HTC_OP_STATE_STOPPING
))
2347 /* TODO : implement a buffer threshold count? */
2348 ath6kl_hif_rx_control(target
->dev
, true);
2353 void ath6kl_htc_flush_rx_buf(struct htc_target
*target
)
2355 struct htc_endpoint
*endpoint
;
2356 struct htc_packet
*packet
, *tmp_pkt
;
2359 for (i
= ENDPOINT_0
; i
< ENDPOINT_MAX
; i
++) {
2360 endpoint
= &target
->endpoint
[i
];
2361 if (!endpoint
->svc_id
)
2365 spin_lock_bh(&target
->rx_lock
);
2366 list_for_each_entry_safe(packet
, tmp_pkt
,
2367 &endpoint
->rx_bufq
, list
) {
2368 list_del(&packet
->list
);
2369 spin_unlock_bh(&target
->rx_lock
);
2370 ath6kl_dbg(ATH6KL_DBG_HTC
,
2371 "htc rx flush pkt 0x%p len %d ep %d\n",
2372 packet
, packet
->buf_len
,
2375 * packets in rx_bufq of endpoint 0 have originally
2376 * been queued from target->free_ctrl_rxbuf where
2377 * packet and packet->buf_start are allocated
2378 * separately using kmalloc(). For other endpoint
2379 * rx_bufq, it is allocated as skb where packet is
2380 * skb->head. Take care of this difference while freeing
2383 if (packet
->endpoint
== ENDPOINT_0
) {
2384 kfree(packet
->buf_start
);
2387 dev_kfree_skb(packet
->pkt_cntxt
);
2389 spin_lock_bh(&target
->rx_lock
);
2391 spin_unlock_bh(&target
->rx_lock
);
2395 int ath6kl_htc_conn_service(struct htc_target
*target
,
2396 struct htc_service_connect_req
*conn_req
,
2397 struct htc_service_connect_resp
*conn_resp
)
2399 struct htc_packet
*rx_pkt
= NULL
;
2400 struct htc_packet
*tx_pkt
= NULL
;
2401 struct htc_conn_service_resp
*resp_msg
;
2402 struct htc_conn_service_msg
*conn_msg
;
2403 struct htc_endpoint
*endpoint
;
2404 enum htc_endpoint_id assigned_ep
= ENDPOINT_MAX
;
2405 unsigned int max_msg_sz
= 0;
2409 ath6kl_dbg(ATH6KL_DBG_HTC
,
2410 "htc connect service target 0x%p service id 0x%x\n",
2411 target
, conn_req
->svc_id
);
2413 if (conn_req
->svc_id
== HTC_CTRL_RSVD_SVC
) {
2414 /* special case for pseudo control service */
2415 assigned_ep
= ENDPOINT_0
;
2416 max_msg_sz
= HTC_MAX_CTRL_MSG_LEN
;
2418 /* allocate a packet to send to the target */
2419 tx_pkt
= htc_get_control_buf(target
, true);
2424 conn_msg
= (struct htc_conn_service_msg
*)tx_pkt
->buf
;
2425 memset(conn_msg
, 0, sizeof(*conn_msg
));
2426 conn_msg
->msg_id
= cpu_to_le16(HTC_MSG_CONN_SVC_ID
);
2427 conn_msg
->svc_id
= cpu_to_le16(conn_req
->svc_id
);
2428 conn_msg
->conn_flags
= cpu_to_le16(conn_req
->conn_flags
);
2430 set_htc_pkt_info(tx_pkt
, NULL
, (u8
*) conn_msg
,
2431 sizeof(*conn_msg
) + conn_msg
->svc_meta_len
,
2432 ENDPOINT_0
, HTC_SERVICE_TX_PACKET_TAG
);
2434 /* we want synchronous operation */
2435 tx_pkt
->completion
= NULL
;
2436 ath6kl_htc_tx_prep_pkt(tx_pkt
, 0, 0, 0);
2437 status
= ath6kl_htc_tx_issue(target
, tx_pkt
);
2442 /* wait for response */
2443 rx_pkt
= htc_wait_for_ctrl_msg(target
);
2450 resp_msg
= (struct htc_conn_service_resp
*)rx_pkt
->buf
;
2451 msg_id
= le16_to_cpu(resp_msg
->msg_id
);
2453 if ((msg_id
!= HTC_MSG_CONN_SVC_RESP_ID
) ||
2454 (rx_pkt
->act_len
< sizeof(*resp_msg
))) {
2459 conn_resp
->resp_code
= resp_msg
->status
;
2460 /* check response status */
2461 if (resp_msg
->status
!= HTC_SERVICE_SUCCESS
) {
2462 ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
2463 resp_msg
->svc_id
, resp_msg
->status
);
2468 assigned_ep
= (enum htc_endpoint_id
)resp_msg
->eid
;
2469 max_msg_sz
= le16_to_cpu(resp_msg
->max_msg_sz
);
2472 if (assigned_ep
>= ENDPOINT_MAX
|| !max_msg_sz
) {
2477 endpoint
= &target
->endpoint
[assigned_ep
];
2478 endpoint
->eid
= assigned_ep
;
2479 if (endpoint
->svc_id
) {
2484 /* return assigned endpoint to caller */
2485 conn_resp
->endpoint
= assigned_ep
;
2486 conn_resp
->len_max
= max_msg_sz
;
2488 /* setup the endpoint */
2490 /* this marks the endpoint in use */
2491 endpoint
->svc_id
= conn_req
->svc_id
;
2493 endpoint
->max_txq_depth
= conn_req
->max_txq_depth
;
2494 endpoint
->len_max
= max_msg_sz
;
2495 endpoint
->ep_cb
= conn_req
->ep_cb
;
2496 endpoint
->cred_dist
.svc_id
= conn_req
->svc_id
;
2497 endpoint
->cred_dist
.htc_ep
= endpoint
;
2498 endpoint
->cred_dist
.endpoint
= assigned_ep
;
2499 endpoint
->cred_dist
.cred_sz
= target
->tgt_cred_sz
;
2501 switch (endpoint
->svc_id
) {
2502 case WMI_DATA_BK_SVC
:
2503 endpoint
->tx_drop_packet_threshold
= MAX_DEF_COOKIE_NUM
/ 3;
2506 endpoint
->tx_drop_packet_threshold
= MAX_HI_COOKIE_NUM
;
2510 if (conn_req
->max_rxmsg_sz
) {
2512 * Override cred_per_msg calculation, this optimizes
2513 * the credit-low indications since the host will actually
2514 * issue smaller messages in the Send path.
2516 if (conn_req
->max_rxmsg_sz
> max_msg_sz
) {
2520 endpoint
->cred_dist
.cred_per_msg
=
2521 conn_req
->max_rxmsg_sz
/ target
->tgt_cred_sz
;
2523 endpoint
->cred_dist
.cred_per_msg
=
2524 max_msg_sz
/ target
->tgt_cred_sz
;
2526 if (!endpoint
->cred_dist
.cred_per_msg
)
2527 endpoint
->cred_dist
.cred_per_msg
= 1;
2529 /* save local connection flags */
2530 endpoint
->conn_flags
= conn_req
->flags
;
2534 htc_reclaim_txctrl_buf(target
, tx_pkt
);
2537 htc_rxpkt_reset(rx_pkt
);
2538 reclaim_rx_ctrl_buf(target
, rx_pkt
);
2544 static void reset_ep_state(struct htc_target
*target
)
2546 struct htc_endpoint
*endpoint
;
2549 for (i
= ENDPOINT_0
; i
< ENDPOINT_MAX
; i
++) {
2550 endpoint
= &target
->endpoint
[i
];
2551 memset(&endpoint
->cred_dist
, 0, sizeof(endpoint
->cred_dist
));
2552 endpoint
->svc_id
= 0;
2553 endpoint
->len_max
= 0;
2554 endpoint
->max_txq_depth
= 0;
2555 memset(&endpoint
->ep_st
, 0,
2556 sizeof(endpoint
->ep_st
));
2557 INIT_LIST_HEAD(&endpoint
->rx_bufq
);
2558 INIT_LIST_HEAD(&endpoint
->txq
);
2559 endpoint
->target
= target
;
2562 /* reset distribution list */
2563 /* FIXME: free existing entries */
2564 INIT_LIST_HEAD(&target
->cred_dist_list
);
2567 int ath6kl_htc_get_rxbuf_num(struct htc_target
*target
,
2568 enum htc_endpoint_id endpoint
)
2572 spin_lock_bh(&target
->rx_lock
);
2573 num
= get_queue_depth(&(target
->endpoint
[endpoint
].rx_bufq
));
2574 spin_unlock_bh(&target
->rx_lock
);
2578 static void htc_setup_msg_bndl(struct htc_target
*target
)
2580 /* limit what HTC can handle */
2581 target
->msg_per_bndl_max
= min(HTC_HOST_MAX_MSG_PER_BUNDLE
,
2582 target
->msg_per_bndl_max
);
2584 if (ath6kl_hif_enable_scatter(target
->dev
->ar
)) {
2585 target
->msg_per_bndl_max
= 0;
2589 /* limit bundle what the device layer can handle */
2590 target
->msg_per_bndl_max
= min(target
->max_scat_entries
,
2591 target
->msg_per_bndl_max
);
2593 ath6kl_dbg(ATH6KL_DBG_BOOT
,
2594 "htc bundling allowed msg_per_bndl_max %d\n",
2595 target
->msg_per_bndl_max
);
2597 /* Max rx bundle size is limited by the max tx bundle size */
2598 target
->max_rx_bndl_sz
= target
->max_xfer_szper_scatreq
;
2599 /* Max tx bundle size if limited by the extended mbox address range */
2600 target
->max_tx_bndl_sz
= min(HIF_MBOX0_EXT_WIDTH
,
2601 target
->max_xfer_szper_scatreq
);
2603 ath6kl_dbg(ATH6KL_DBG_BOOT
, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
2604 target
->max_rx_bndl_sz
, target
->max_tx_bndl_sz
);
2606 if (target
->max_tx_bndl_sz
)
2607 /* tx_bndl_mask is enabled per AC, each has 1 bit */
2608 target
->tx_bndl_mask
= (1 << WMM_NUM_AC
) - 1;
2610 if (target
->max_rx_bndl_sz
)
2611 target
->rx_bndl_enable
= true;
2613 if ((target
->tgt_cred_sz
% target
->block_sz
) != 0) {
2614 ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
2615 target
->tgt_cred_sz
);
2618 * Disallow send bundling since the credit size is
2619 * not aligned to a block size the I/O block
2620 * padding will spill into the next credit buffer
2623 target
->tx_bndl_mask
= 0;
2627 int ath6kl_htc_wait_target(struct htc_target
*target
)
2629 struct htc_packet
*packet
= NULL
;
2630 struct htc_ready_ext_msg
*rdy_msg
;
2631 struct htc_service_connect_req connect
;
2632 struct htc_service_connect_resp resp
;
2635 /* FIXME: remove once USB support is implemented */
2636 if (target
->dev
->ar
->hif_type
== ATH6KL_HIF_TYPE_USB
) {
2637 ath6kl_err("HTC doesn't support USB yet. Patience!\n");
2641 /* we should be getting 1 control message that the target is ready */
2642 packet
= htc_wait_for_ctrl_msg(target
);
2647 /* we controlled the buffer creation so it's properly aligned */
2648 rdy_msg
= (struct htc_ready_ext_msg
*)packet
->buf
;
2650 if ((le16_to_cpu(rdy_msg
->ver2_0_info
.msg_id
) != HTC_MSG_READY_ID
) ||
2651 (packet
->act_len
< sizeof(struct htc_ready_msg
))) {
2653 goto fail_wait_target
;
2656 if (!rdy_msg
->ver2_0_info
.cred_cnt
|| !rdy_msg
->ver2_0_info
.cred_sz
) {
2658 goto fail_wait_target
;
2661 target
->tgt_creds
= le16_to_cpu(rdy_msg
->ver2_0_info
.cred_cnt
);
2662 target
->tgt_cred_sz
= le16_to_cpu(rdy_msg
->ver2_0_info
.cred_sz
);
2664 ath6kl_dbg(ATH6KL_DBG_BOOT
,
2665 "htc target ready credits %d size %d\n",
2666 target
->tgt_creds
, target
->tgt_cred_sz
);
2668 /* check if this is an extended ready message */
2669 if (packet
->act_len
>= sizeof(struct htc_ready_ext_msg
)) {
2670 /* this is an extended message */
2671 target
->htc_tgt_ver
= rdy_msg
->htc_ver
;
2672 target
->msg_per_bndl_max
= rdy_msg
->msg_per_htc_bndl
;
2675 target
->htc_tgt_ver
= HTC_VERSION_2P0
;
2676 target
->msg_per_bndl_max
= 0;
2679 ath6kl_dbg(ATH6KL_DBG_BOOT
, "htc using protocol %s (%d)\n",
2680 (target
->htc_tgt_ver
== HTC_VERSION_2P0
) ? "2.0" : ">= 2.1",
2681 target
->htc_tgt_ver
);
2683 if (target
->msg_per_bndl_max
> 0)
2684 htc_setup_msg_bndl(target
);
2686 /* setup our pseudo HTC control endpoint connection */
2687 memset(&connect
, 0, sizeof(connect
));
2688 memset(&resp
, 0, sizeof(resp
));
2689 connect
.ep_cb
.rx
= htc_ctrl_rx
;
2690 connect
.ep_cb
.rx_refill
= NULL
;
2691 connect
.ep_cb
.tx_full
= NULL
;
2692 connect
.max_txq_depth
= NUM_CONTROL_BUFFERS
;
2693 connect
.svc_id
= HTC_CTRL_RSVD_SVC
;
2695 /* connect fake service */
2696 status
= ath6kl_htc_conn_service((void *)target
, &connect
, &resp
);
2700 * FIXME: this call doesn't make sense, the caller should
2701 * call ath6kl_htc_cleanup() when it wants remove htc
2703 ath6kl_hif_cleanup_scatter(target
->dev
->ar
);
2707 htc_rxpkt_reset(packet
);
2708 reclaim_rx_ctrl_buf(target
, packet
);
2715 * Start HTC, enable interrupts and let the target know
2716 * host has finished setup.
2718 int ath6kl_htc_start(struct htc_target
*target
)
2720 struct htc_packet
*packet
;
2723 memset(&target
->dev
->irq_proc_reg
, 0,
2724 sizeof(target
->dev
->irq_proc_reg
));
2726 /* Disable interrupts at the chip level */
2727 ath6kl_hif_disable_intrs(target
->dev
);
2729 target
->htc_flags
= 0;
2730 target
->rx_st_flags
= 0;
2732 /* Push control receive buffers into htc control endpoint */
2733 while ((packet
= htc_get_control_buf(target
, false)) != NULL
) {
2734 status
= htc_add_rxbuf(target
, packet
);
2739 /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2740 ath6kl_credit_init(target
->credit_info
, &target
->cred_dist_list
,
2743 dump_cred_dist_stats(target
);
2745 /* Indicate to the target of the setup completion */
2746 status
= htc_setup_tx_complete(target
);
2751 /* unmask interrupts */
2752 status
= ath6kl_hif_unmask_intrs(target
->dev
);
2755 ath6kl_htc_stop(target
);
2760 static int ath6kl_htc_reset(struct htc_target
*target
)
2762 u32 block_size
, ctrl_bufsz
;
2763 struct htc_packet
*packet
;
2766 reset_ep_state(target
);
2768 block_size
= target
->dev
->ar
->mbox_info
.block_size
;
2770 ctrl_bufsz
= (block_size
> HTC_MAX_CTRL_MSG_LEN
) ?
2771 (block_size
+ HTC_HDR_LENGTH
) :
2772 (HTC_MAX_CTRL_MSG_LEN
+ HTC_HDR_LENGTH
);
2774 for (i
= 0; i
< NUM_CONTROL_BUFFERS
; i
++) {
2775 packet
= kzalloc(sizeof(*packet
), GFP_KERNEL
);
2779 packet
->buf_start
= kzalloc(ctrl_bufsz
, GFP_KERNEL
);
2780 if (!packet
->buf_start
) {
2785 packet
->buf_len
= ctrl_bufsz
;
2786 if (i
< NUM_CONTROL_RX_BUFFERS
) {
2787 packet
->act_len
= 0;
2788 packet
->buf
= packet
->buf_start
;
2789 packet
->endpoint
= ENDPOINT_0
;
2790 list_add_tail(&packet
->list
, &target
->free_ctrl_rxbuf
);
2792 list_add_tail(&packet
->list
, &target
->free_ctrl_txbuf
);
2798 /* htc_stop: stop interrupt reception, and flush all queued buffers */
2799 void ath6kl_htc_stop(struct htc_target
*target
)
2801 spin_lock_bh(&target
->htc_lock
);
2802 target
->htc_flags
|= HTC_OP_STATE_STOPPING
;
2803 spin_unlock_bh(&target
->htc_lock
);
2806 * Masking interrupts is a synchronous operation, when this
2807 * function returns all pending HIF I/O has completed, we can
2808 * safely flush the queues.
2810 ath6kl_hif_mask_intrs(target
->dev
);
2812 ath6kl_htc_flush_txep_all(target
);
2814 ath6kl_htc_flush_rx_buf(target
);
2816 ath6kl_htc_reset(target
);
2819 void *ath6kl_htc_create(struct ath6kl
*ar
)
2821 struct htc_target
*target
= NULL
;
2824 target
= kzalloc(sizeof(*target
), GFP_KERNEL
);
2826 ath6kl_err("unable to allocate memory\n");
2830 target
->dev
= kzalloc(sizeof(*target
->dev
), GFP_KERNEL
);
2832 ath6kl_err("unable to allocate memory\n");
2834 goto err_htc_cleanup
;
2837 spin_lock_init(&target
->htc_lock
);
2838 spin_lock_init(&target
->rx_lock
);
2839 spin_lock_init(&target
->tx_lock
);
2841 INIT_LIST_HEAD(&target
->free_ctrl_txbuf
);
2842 INIT_LIST_HEAD(&target
->free_ctrl_rxbuf
);
2843 INIT_LIST_HEAD(&target
->cred_dist_list
);
2845 target
->dev
->ar
= ar
;
2846 target
->dev
->htc_cnxt
= target
;
2847 target
->ep_waiting
= ENDPOINT_MAX
;
2849 status
= ath6kl_hif_setup(target
->dev
);
2851 goto err_htc_cleanup
;
2853 status
= ath6kl_htc_reset(target
);
2855 goto err_htc_cleanup
;
2860 ath6kl_htc_cleanup(target
);
2865 /* cleanup the HTC instance */
2866 void ath6kl_htc_cleanup(struct htc_target
*target
)
2868 struct htc_packet
*packet
, *tmp_packet
;
2870 /* FIXME: remove check once USB support is implemented */
2871 if (target
->dev
->ar
->hif_type
!= ATH6KL_HIF_TYPE_USB
)
2872 ath6kl_hif_cleanup_scatter(target
->dev
->ar
);
2874 list_for_each_entry_safe(packet
, tmp_packet
,
2875 &target
->free_ctrl_txbuf
, list
) {
2876 list_del(&packet
->list
);
2877 kfree(packet
->buf_start
);
2881 list_for_each_entry_safe(packet
, tmp_packet
,
2882 &target
->free_ctrl_rxbuf
, list
) {
2883 list_del(&packet
->list
);
2884 kfree(packet
->buf_start
);