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, 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 htc_hdr:eid %d, flags 0x%x, len %d\n",
1357 htc_hdr->eid, htc_hdr->flags,
1358 le16_to_cpu(htc_hdr->payld_len));
1363 for (j = 0; j < n_msg; j++) {
1366 * Reset flag, any packets allocated using the
1367 * rx_alloc() API cannot be recycled on
1368 * cleanup,they must be explicitly returned.
1372 if (ep_cb.rx_allocthresh &&
1373 (full_len > ep_cb.rx_alloc_thresh)) {
1374 ep->ep_st.rx_alloc_thresh_hit += 1;
1375 ep->ep_st.rxalloc_thresh_byte +=
1376 le16_to_cpu(htc_hdr->payld_len);
1378 spin_unlock_bh(&target->rx_lock);
1381 packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
1383 spin_lock_bh(&target->rx_lock);
1385 /* refill handler is being used */
1386 if (list_empty(&ep->rx_bufq)) {
1387 if (ep_cb.rx_refill) {
1388 spin_unlock_bh(&target->rx_lock);
1389 ep_cb.rx_refill(ep->target, ep->eid);
1390 spin_lock_bh(&target->rx_lock);
1394 if (list_empty(&ep->rx_bufq))
1397 packet = list_first_entry(&ep->rx_bufq,
1398 struct htc_packet, list);
1399 list_del(&packet->list);
1404 target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
1405 target->ep_waiting = ep->eid;
1410 packet->info.rx.rx_flags = 0;
1411 packet->info.rx.indicat_flags = 0;
1416 * flag that these packets cannot be
1417 * recycled, they have to be returned to
1420 packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
1422 /* Caller needs to free this upon any failure */
1423 list_add_tail(&packet->list, queue);
1425 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1426 status = -ECANCELED;
1431 packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
1432 packet->info.rx.exp_hdr = 0xFFFFFFFF;
1434 /* set expected look ahead */
1435 packet->info.rx.exp_hdr = *lk_ahds;
1437 packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
1444 static int ath6kl_htc_rx_alloc(struct htc_target *target,
1445 u32 lk_ahds[], int msg,
1446 struct htc_endpoint *endpoint,
1447 struct list_head *queue)
1450 struct htc_packet *packet, *tmp_pkt;
1451 struct htc_frame_hdr *htc_hdr;
1454 spin_lock_bh(&target->rx_lock);
1456 for (i = 0; i < msg; i++) {
1458 htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
1460 if (htc_hdr->eid >= ENDPOINT_MAX) {
1461 ath6kl_err("invalid ep in look-ahead: %d\n",
1467 if (htc_hdr->eid != endpoint->eid) {
1468 ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
1469 htc_hdr->eid, endpoint->eid, i);
1474 if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
1475 ath6kl_err("payload len %d exceeds max htc : %d !\n",
1477 (u32) HTC_MAX_PAYLOAD_LENGTH);
1482 if (endpoint->svc_id == 0) {
1483 ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
1488 if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
1490 * HTC header indicates that every packet to follow
1491 * has the same padded length so that it can be
1492 * optimally fetched as a full bundle.
1494 n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
1495 HTC_FLG_RX_BNDL_CNT_S;
1497 /* the count doesn't include the starter frame */
1499 if (n_msg > target->msg_per_bndl_max) {
1504 endpoint->ep_st.rx_bundle_from_hdr += 1;
1505 ath6kl_dbg(ATH6KL_DBG_HTC,
1506 "htc rx bundle pkts %d\n",
1509 /* HTC header only indicates 1 message to fetch */
1512 /* Setup packet buffers for each message */
1513 status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
1517 * This is due to unavailabilty of buffers to rx entire data.
1518 * Return no error so that free buffers from queue can be used
1519 * to receive partial data.
1521 if (status == -ENOSPC) {
1522 spin_unlock_bh(&target->rx_lock);
1530 spin_unlock_bh(&target->rx_lock);
1533 list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
1534 list_del(&packet->list);
1535 htc_reclaim_rxbuf(target, packet,
1536 &target->endpoint[packet->endpoint]);
1543 static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
1545 if (packets->endpoint != ENDPOINT_0) {
1550 if (packets->status == -ECANCELED) {
1551 reclaim_rx_ctrl_buf(context, packets);
1555 if (packets->act_len > 0) {
1556 ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
1557 packets->act_len + HTC_HDR_LENGTH);
1559 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1560 "htc rx unexpected endpoint 0 message", "",
1561 packets->buf - HTC_HDR_LENGTH,
1562 packets->act_len + HTC_HDR_LENGTH);
1565 htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
1568 static void htc_proc_cred_rpt(struct htc_target *target,
1569 struct htc_credit_report *rpt,
1571 enum htc_endpoint_id from_ep)
1573 struct htc_endpoint *endpoint;
1574 int tot_credits = 0, i;
1577 spin_lock_bh(&target->tx_lock);
1579 for (i = 0; i < n_entries; i++, rpt++) {
1580 if (rpt->eid >= ENDPOINT_MAX) {
1582 spin_unlock_bh(&target->tx_lock);
1586 endpoint = &target->endpoint[rpt->eid];
1588 ath6kl_dbg(ATH6KL_DBG_CREDIT,
1589 "credit report ep %d credits %d\n",
1590 rpt->eid, rpt->credits);
1592 endpoint->ep_st.tx_cred_rpt += 1;
1593 endpoint->ep_st.cred_retnd += rpt->credits;
1595 if (from_ep == rpt->eid) {
1597 * This credit report arrived on the same endpoint
1598 * indicating it arrived in an RX packet.
1600 endpoint->ep_st.cred_from_rx += rpt->credits;
1601 endpoint->ep_st.cred_rpt_from_rx += 1;
1602 } else if (from_ep == ENDPOINT_0) {
1603 /* credit arrived on endpoint 0 as a NULL message */
1604 endpoint->ep_st.cred_from_ep0 += rpt->credits;
1605 endpoint->ep_st.cred_rpt_ep0 += 1;
1607 endpoint->ep_st.cred_from_other += rpt->credits;
1608 endpoint->ep_st.cred_rpt_from_other += 1;
1611 if (rpt->eid == ENDPOINT_0)
1612 /* always give endpoint 0 credits back */
1613 endpoint->cred_dist.credits += rpt->credits;
1615 endpoint->cred_dist.cred_to_dist += rpt->credits;
1620 * Refresh tx depth for distribution function that will
1621 * recover these credits NOTE: this is only valid when
1622 * there are credits to recover!
1624 endpoint->cred_dist.txq_depth =
1625 get_queue_depth(&endpoint->txq);
1627 tot_credits += rpt->credits;
1632 * This was a credit return based on a completed send
1633 * operations note, this is done with the lock held
1635 ath6kl_credit_distribute(target->credit_info,
1636 &target->cred_dist_list,
1637 HTC_CREDIT_DIST_SEND_COMPLETE);
1640 spin_unlock_bh(&target->tx_lock);
1643 htc_chk_ep_txq(target);
1646 static int htc_parse_trailer(struct htc_target *target,
1647 struct htc_record_hdr *record,
1648 u8 *record_buf, u32 *next_lk_ahds,
1649 enum htc_endpoint_id endpoint,
1652 struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
1653 struct htc_lookahead_report *lk_ahd;
1656 switch (record->rec_id) {
1657 case HTC_RECORD_CREDITS:
1658 len = record->len / sizeof(struct htc_credit_report);
1664 htc_proc_cred_rpt(target,
1665 (struct htc_credit_report *) record_buf,
1668 case HTC_RECORD_LOOKAHEAD:
1669 len = record->len / sizeof(*lk_ahd);
1675 lk_ahd = (struct htc_lookahead_report *) record_buf;
1676 if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF)) &&
1679 ath6kl_dbg(ATH6KL_DBG_HTC,
1680 "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
1681 lk_ahd->pre_valid, lk_ahd->post_valid);
1683 /* look ahead bytes are valid, copy them over */
1684 memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);
1686 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1687 "htc rx next look ahead",
1688 "", next_lk_ahds, 4);
1693 case HTC_RECORD_LOOKAHEAD_BUNDLE:
1694 len = record->len / sizeof(*bundle_lkahd_rpt);
1695 if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
1704 (struct htc_bundle_lkahd_rpt *) record_buf;
1706 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
1707 "", record_buf, record->len);
1709 for (i = 0; i < len; i++) {
1710 memcpy((u8 *)&next_lk_ahds[i],
1711 bundle_lkahd_rpt->lk_ahd, 4);
1719 ath6kl_err("unhandled record: id:%d len:%d\n",
1720 record->rec_id, record->len);
1728 static int htc_proc_trailer(struct htc_target *target,
1729 u8 *buf, int len, u32 *next_lk_ahds,
1730 int *n_lk_ahds, enum htc_endpoint_id endpoint)
1732 struct htc_record_hdr *record;
1738 ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
1739 ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
1747 if (len < sizeof(struct htc_record_hdr)) {
1751 /* these are byte aligned structs */
1752 record = (struct htc_record_hdr *) buf;
1753 len -= sizeof(struct htc_record_hdr);
1754 buf += sizeof(struct htc_record_hdr);
1756 if (record->len > len) {
1757 ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
1758 record->len, record->rec_id, len);
1764 status = htc_parse_trailer(target, record, record_buf,
1765 next_lk_ahds, endpoint, n_lk_ahds);
1770 /* advance buffer past this record for next time around */
1776 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
1777 "", orig_buf, orig_len);
1782 static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
1783 struct htc_packet *packet,
1784 u32 *next_lkahds, int *n_lkahds)
1789 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
1791 if (n_lkahds != NULL)
1795 * NOTE: we cannot assume the alignment of buf, so we use the safe
1796 * macros to retrieve 16 bit fields.
1798 payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
1800 memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
1802 if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
1804 * Refresh the expected header and the actual length as it
1805 * was unknown when this packet was grabbed as part of the
1808 packet->info.rx.exp_hdr = lk_ahd;
1809 packet->act_len = payload_len + HTC_HDR_LENGTH;
1811 /* validate the actual header that was refreshed */
1812 if (packet->act_len > packet->buf_len) {
1813 ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
1814 payload_len, lk_ahd);
1816 * Limit this to max buffer just to print out some
1819 packet->act_len = min(packet->act_len, packet->buf_len);
1824 if (packet->endpoint != htc_hdr->eid) {
1825 ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
1826 htc_hdr->eid, packet->endpoint);
1832 if (lk_ahd != packet->info.rx.exp_hdr) {
1833 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1834 __func__, packet, packet->info.rx.rx_flags);
1835 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
1836 "", &packet->info.rx.exp_hdr, 4);
1837 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
1838 "", (u8 *)&lk_ahd, sizeof(lk_ahd));
1843 if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
1844 if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
1845 htc_hdr->ctrl[0] > payload_len) {
1846 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1847 __func__, payload_len, htc_hdr->ctrl[0]);
1852 if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
1857 status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
1858 + payload_len - htc_hdr->ctrl[0],
1859 htc_hdr->ctrl[0], next_lkahds,
1860 n_lkahds, packet->endpoint);
1865 packet->act_len -= htc_hdr->ctrl[0];
1868 packet->buf += HTC_HDR_LENGTH;
1869 packet->act_len -= HTC_HDR_LENGTH;
1873 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1874 "", packet->buf, packet->act_len);
1879 static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1880 struct htc_packet *packet)
1882 ath6kl_dbg(ATH6KL_DBG_HTC,
1883 "htc rx complete ep %d packet 0x%p\n",
1884 endpoint->eid, packet);
1885 endpoint->ep_cb.rx(endpoint->target, packet);
1888 static int ath6kl_htc_rx_bundle(struct htc_target *target,
1889 struct list_head *rxq,
1890 struct list_head *sync_compq,
1891 int *n_pkt_fetched, bool part_bundle)
1893 struct hif_scatter_req *scat_req;
1894 struct htc_packet *packet;
1895 int rem_space = target->max_rx_bndl_sz;
1896 int n_scat_pkt, status = 0, i, len;
1898 n_scat_pkt = get_queue_depth(rxq);
1899 n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
1901 if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
1903 * We were forced to split this bundle receive operation
1904 * all packets in this partial bundle must have their
1905 * lookaheads ignored.
1910 * This would only happen if the target ignored our max
1913 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1914 __func__, get_queue_depth(rxq), n_scat_pkt);
1919 ath6kl_dbg(ATH6KL_DBG_HTC,
1920 "htc rx bundle depth %d pkts %d\n",
1921 get_queue_depth(rxq), n_scat_pkt);
1923 scat_req = hif_scatter_req_get(target->dev->ar);
1925 if (scat_req == NULL)
1928 for (i = 0; i < n_scat_pkt; i++) {
1931 packet = list_first_entry(rxq, struct htc_packet, list);
1932 list_del(&packet->list);
1934 pad_len = CALC_TXRX_PADDED_LEN(target,
1937 if ((rem_space - pad_len) < 0) {
1938 list_add(&packet->list, rxq);
1942 rem_space -= pad_len;
1944 if (part_bundle || (i < (n_scat_pkt - 1)))
1946 * Packet 0..n-1 cannot be checked for look-aheads
1947 * since we are fetching a bundle the last packet
1948 * however can have it's lookahead used
1950 packet->info.rx.rx_flags |=
1951 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1953 /* NOTE: 1 HTC packet per scatter entry */
1954 scat_req->scat_list[i].buf = packet->buf;
1955 scat_req->scat_list[i].len = pad_len;
1957 packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
1959 list_add_tail(&packet->list, sync_compq);
1961 WARN_ON(!scat_req->scat_list[i].len);
1962 len += scat_req->scat_list[i].len;
1965 scat_req->len = len;
1966 scat_req->scat_entries = i;
1968 status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
1973 /* free scatter request */
1974 hif_scatter_req_add(target->dev->ar, scat_req);
1981 static int ath6kl_htc_rx_process_packets(struct htc_target *target,
1982 struct list_head *comp_pktq,
1986 struct htc_packet *packet, *tmp_pkt;
1987 struct htc_endpoint *ep;
1990 list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
1991 ep = &target->endpoint[packet->endpoint];
1993 /* process header for each of the recv packet */
1994 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
1999 list_del(&packet->list);
2001 if (list_empty(comp_pktq)) {
2003 * Last packet's more packet flag is set
2004 * based on the lookahead.
2007 ath6kl_htc_rx_set_indicate(lk_ahds[0],
2011 * Packets in a bundle automatically have
2014 packet->info.rx.indicat_flags |=
2015 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
2017 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
2019 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
2020 ep->ep_st.rx_bundl += 1;
2022 ath6kl_htc_rx_complete(ep, packet);
2028 static int ath6kl_htc_rx_fetch(struct htc_target *target,
2029 struct list_head *rx_pktq,
2030 struct list_head *comp_pktq)
2033 bool part_bundle = false;
2035 struct list_head tmp_rxq;
2036 struct htc_packet *packet, *tmp_pkt;
2038 /* now go fetch the list of HTC packets */
2039 while (!list_empty(rx_pktq)) {
2042 INIT_LIST_HEAD(&tmp_rxq);
2044 if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
2046 * There are enough packets to attempt a
2047 * bundle transfer and recv bundling is
2050 status = ath6kl_htc_rx_bundle(target, rx_pktq,
2057 if (!list_empty(rx_pktq))
2060 list_splice_tail_init(&tmp_rxq, comp_pktq);
2063 if (!fetched_pkts) {
2065 packet = list_first_entry(rx_pktq, struct htc_packet,
2068 /* fully synchronous */
2069 packet->completion = NULL;
2071 if (!list_is_singular(rx_pktq))
2073 * look_aheads in all packet
2074 * except the last one in the
2075 * bundle must be ignored
2077 packet->info.rx.rx_flags |=
2078 HTC_RX_PKT_IGNORE_LOOKAHEAD;
2080 /* go fetch the packet */
2081 status = ath6kl_htc_rx_packet(target, packet,
2084 list_move_tail(&packet->list, &tmp_rxq);
2089 list_splice_tail_init(&tmp_rxq, comp_pktq);
2098 * Cleanup any packets we allocated but didn't use to
2099 * actually fetch any packets.
2102 list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
2103 list_del(&packet->list);
2104 htc_reclaim_rxbuf(target, packet,
2105 &target->endpoint[packet->endpoint]);
2108 list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
2109 list_del(&packet->list);
2110 htc_reclaim_rxbuf(target, packet,
2111 &target->endpoint[packet->endpoint]);
2117 int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
2118 u32 msg_look_ahead, int *num_pkts)
2120 struct htc_packet *packets, *tmp_pkt;
2121 struct htc_endpoint *endpoint;
2122 struct list_head rx_pktq, comp_pktq;
2124 u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
2125 int num_look_ahead = 1;
2126 enum htc_endpoint_id id;
2129 INIT_LIST_HEAD(&comp_pktq);
2133 * On first entry copy the look_aheads into our temp array for
2136 look_aheads[0] = msg_look_ahead;
2141 * First lookahead sets the expected endpoint IDs for all
2142 * packets in a bundle.
2144 id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
2145 endpoint = &target->endpoint[id];
2147 if (id >= ENDPOINT_MAX) {
2148 ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
2154 INIT_LIST_HEAD(&rx_pktq);
2155 INIT_LIST_HEAD(&comp_pktq);
2158 * Try to allocate as many HTC RX packets indicated by the
2161 status = ath6kl_htc_rx_alloc(target, look_aheads,
2162 num_look_ahead, endpoint,
2167 if (get_queue_depth(&rx_pktq) >= 2)
2169 * A recv bundle was detected, force IRQ status
2172 target->chk_irq_status_cnt = 1;
2174 n_fetched += get_queue_depth(&rx_pktq);
2178 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
2181 ath6kl_htc_rx_chk_water_mark(endpoint);
2183 /* Process fetched packets */
2184 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
2188 if (!num_look_ahead || status)
2192 * For SYNCH processing, if we get here, we are running
2193 * through the loop again due to a detected lookahead. Set
2194 * flag that we should re-check IRQ status registers again
2195 * before leaving IRQ processing, this can net better
2196 * performance in high throughput situations.
2198 target->chk_irq_status_cnt = 1;
2202 ath6kl_err("failed to get pending recv messages: %d\n",
2205 /* cleanup any packets in sync completion queue */
2206 list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
2207 list_del(&packets->list);
2208 htc_reclaim_rxbuf(target, packets,
2209 &target->endpoint[packets->endpoint]);
2212 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2213 ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
2214 ath6kl_hif_rx_control(target->dev, false);
2219 * Before leaving, check to see if host ran out of buffers and
2220 * needs to stop the receiver.
2222 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
2223 ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
2224 ath6kl_hif_rx_control(target->dev, false);
2226 *num_pkts = n_fetched;
2232 * Synchronously wait for a control message from the target,
2233 * This function is used at initialization time ONLY. At init messages
2234 * on ENDPOINT 0 are expected.
2236 static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
2238 struct htc_packet *packet = NULL;
2239 struct htc_frame_hdr *htc_hdr;
2242 if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
2243 HTC_TARGET_RESPONSE_TIMEOUT))
2246 ath6kl_dbg(ATH6KL_DBG_HTC,
2247 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
2249 htc_hdr = (struct htc_frame_hdr *)&look_ahead;
2251 if (htc_hdr->eid != ENDPOINT_0)
2254 packet = htc_get_control_buf(target, false);
2259 packet->info.rx.rx_flags = 0;
2260 packet->info.rx.exp_hdr = look_ahead;
2261 packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;
2263 if (packet->act_len > packet->buf_len)
2266 /* we want synchronous operation */
2267 packet->completion = NULL;
2269 /* get the message from the device, this will block */
2270 if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
2273 /* process receive header */
2274 packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
2276 if (packet->status) {
2277 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
2285 if (packet != NULL) {
2286 htc_rxpkt_reset(packet);
2287 reclaim_rx_ctrl_buf(target, packet);
2293 int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
2294 struct list_head *pkt_queue)
2296 struct htc_endpoint *endpoint;
2297 struct htc_packet *first_pkt;
2298 bool rx_unblock = false;
2299 int status = 0, depth;
2301 if (list_empty(pkt_queue))
2304 first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
2306 if (first_pkt->endpoint >= ENDPOINT_MAX)
2309 depth = get_queue_depth(pkt_queue);
2311 ath6kl_dbg(ATH6KL_DBG_HTC,
2312 "htc rx add multiple ep id %d cnt %d len %d\n",
2313 first_pkt->endpoint, depth, first_pkt->buf_len);
2315 endpoint = &target->endpoint[first_pkt->endpoint];
2317 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2318 struct htc_packet *packet, *tmp_pkt;
2320 /* walk through queue and mark each one canceled */
2321 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
2322 packet->status = -ECANCELED;
2323 list_del(&packet->list);
2324 ath6kl_htc_rx_complete(endpoint, packet);
2330 spin_lock_bh(&target->rx_lock);
2332 list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
2334 /* check if we are blocked waiting for a new buffer */
2335 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
2336 if (target->ep_waiting == first_pkt->endpoint) {
2337 ath6kl_dbg(ATH6KL_DBG_HTC,
2338 "htc rx blocked on ep %d, unblocking\n",
2339 target->ep_waiting);
2340 target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
2341 target->ep_waiting = ENDPOINT_MAX;
2346 spin_unlock_bh(&target->rx_lock);
2348 if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
2349 /* TODO : implement a buffer threshold count? */
2350 ath6kl_hif_rx_control(target->dev, true);
2355 void ath6kl_htc_flush_rx_buf(struct htc_target *target)
2357 struct htc_endpoint *endpoint;
2358 struct htc_packet *packet, *tmp_pkt;
2361 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2362 endpoint = &target->endpoint[i];
2363 if (!endpoint->svc_id)
2367 spin_lock_bh(&target->rx_lock);
2368 list_for_each_entry_safe(packet, tmp_pkt,
2369 &endpoint->rx_bufq, list) {
2370 list_del(&packet->list);
2371 spin_unlock_bh(&target->rx_lock);
2372 ath6kl_dbg(ATH6KL_DBG_HTC,
2373 "htc rx flush pkt 0x%p len %d ep %d\n",
2374 packet, packet->buf_len,
2377 * packets in rx_bufq of endpoint 0 have originally
2378 * been queued from target->free_ctrl_rxbuf where
2379 * packet and packet->buf_start are allocated
2380 * separately using kmalloc(). For other endpoint
2381 * rx_bufq, it is allocated as skb where packet is
2382 * skb->head. Take care of this difference while freeing
2385 if (packet->endpoint == ENDPOINT_0) {
2386 kfree(packet->buf_start);
2389 dev_kfree_skb(packet->pkt_cntxt);
2391 spin_lock_bh(&target->rx_lock);
2393 spin_unlock_bh(&target->rx_lock);
2397 int ath6kl_htc_conn_service(struct htc_target *target,
2398 struct htc_service_connect_req *conn_req,
2399 struct htc_service_connect_resp *conn_resp)
2401 struct htc_packet *rx_pkt = NULL;
2402 struct htc_packet *tx_pkt = NULL;
2403 struct htc_conn_service_resp *resp_msg;
2404 struct htc_conn_service_msg *conn_msg;
2405 struct htc_endpoint *endpoint;
2406 enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
2407 unsigned int max_msg_sz = 0;
2411 ath6kl_dbg(ATH6KL_DBG_HTC,
2412 "htc connect service target 0x%p service id 0x%x\n",
2413 target, conn_req->svc_id);
2415 if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
2416 /* special case for pseudo control service */
2417 assigned_ep = ENDPOINT_0;
2418 max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
2420 /* allocate a packet to send to the target */
2421 tx_pkt = htc_get_control_buf(target, true);
2426 conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
2427 memset(conn_msg, 0, sizeof(*conn_msg));
2428 conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
2429 conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
2430 conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
2432 set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
2433 sizeof(*conn_msg) + conn_msg->svc_meta_len,
2434 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
2436 /* we want synchronous operation */
2437 tx_pkt->completion = NULL;
2438 ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
2439 status = ath6kl_htc_tx_issue(target, tx_pkt);
2444 /* wait for response */
2445 rx_pkt = htc_wait_for_ctrl_msg(target);
2452 resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
2453 msg_id = le16_to_cpu(resp_msg->msg_id);
2455 if ((msg_id != HTC_MSG_CONN_SVC_RESP_ID) ||
2456 (rx_pkt->act_len < sizeof(*resp_msg))) {
2461 conn_resp->resp_code = resp_msg->status;
2462 /* check response status */
2463 if (resp_msg->status != HTC_SERVICE_SUCCESS) {
2464 ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
2465 resp_msg->svc_id, resp_msg->status);
2470 assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
2471 max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
2474 if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
2479 endpoint = &target->endpoint[assigned_ep];
2480 endpoint->eid = assigned_ep;
2481 if (endpoint->svc_id) {
2486 /* return assigned endpoint to caller */
2487 conn_resp->endpoint = assigned_ep;
2488 conn_resp->len_max = max_msg_sz;
2490 /* setup the endpoint */
2492 /* this marks the endpoint in use */
2493 endpoint->svc_id = conn_req->svc_id;
2495 endpoint->max_txq_depth = conn_req->max_txq_depth;
2496 endpoint->len_max = max_msg_sz;
2497 endpoint->ep_cb = conn_req->ep_cb;
2498 endpoint->cred_dist.svc_id = conn_req->svc_id;
2499 endpoint->cred_dist.htc_ep = endpoint;
2500 endpoint->cred_dist.endpoint = assigned_ep;
2501 endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
2503 switch (endpoint->svc_id) {
2504 case WMI_DATA_BK_SVC:
2505 endpoint->tx_drop_packet_threshold = MAX_DEF_COOKIE_NUM / 3;
2508 endpoint->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
2512 if (conn_req->max_rxmsg_sz) {
2514 * Override cred_per_msg calculation, this optimizes
2515 * the credit-low indications since the host will actually
2516 * issue smaller messages in the Send path.
2518 if (conn_req->max_rxmsg_sz > max_msg_sz) {
2522 endpoint->cred_dist.cred_per_msg =
2523 conn_req->max_rxmsg_sz / target->tgt_cred_sz;
2525 endpoint->cred_dist.cred_per_msg =
2526 max_msg_sz / target->tgt_cred_sz;
2528 if (!endpoint->cred_dist.cred_per_msg)
2529 endpoint->cred_dist.cred_per_msg = 1;
2531 /* save local connection flags */
2532 endpoint->conn_flags = conn_req->flags;
2536 htc_reclaim_txctrl_buf(target, tx_pkt);
2539 htc_rxpkt_reset(rx_pkt);
2540 reclaim_rx_ctrl_buf(target, rx_pkt);
2546 static void reset_ep_state(struct htc_target *target)
2548 struct htc_endpoint *endpoint;
2551 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2552 endpoint = &target->endpoint[i];
2553 memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
2554 endpoint->svc_id = 0;
2555 endpoint->len_max = 0;
2556 endpoint->max_txq_depth = 0;
2557 memset(&endpoint->ep_st, 0,
2558 sizeof(endpoint->ep_st));
2559 INIT_LIST_HEAD(&endpoint->rx_bufq);
2560 INIT_LIST_HEAD(&endpoint->txq);
2561 endpoint->target = target;
2564 /* reset distribution list */
2565 /* FIXME: free existing entries */
2566 INIT_LIST_HEAD(&target->cred_dist_list);
2569 int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
2570 enum htc_endpoint_id endpoint)
2574 spin_lock_bh(&target->rx_lock);
2575 num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
2576 spin_unlock_bh(&target->rx_lock);
2580 static void htc_setup_msg_bndl(struct htc_target *target)
2582 /* limit what HTC can handle */
2583 target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
2584 target->msg_per_bndl_max);
2586 if (ath6kl_hif_enable_scatter(target->dev->ar)) {
2587 target->msg_per_bndl_max = 0;
2591 /* limit bundle what the device layer can handle */
2592 target->msg_per_bndl_max = min(target->max_scat_entries,
2593 target->msg_per_bndl_max);
2595 ath6kl_dbg(ATH6KL_DBG_BOOT,
2596 "htc bundling allowed msg_per_bndl_max %d\n",
2597 target->msg_per_bndl_max);
2599 /* Max rx bundle size is limited by the max tx bundle size */
2600 target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
2601 /* Max tx bundle size if limited by the extended mbox address range */
2602 target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
2603 target->max_xfer_szper_scatreq);
2605 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
2606 target->max_rx_bndl_sz, target->max_tx_bndl_sz);
2608 if (target->max_tx_bndl_sz)
2609 /* tx_bndl_mask is enabled per AC, each has 1 bit */
2610 target->tx_bndl_mask = (1 << WMM_NUM_AC) - 1;
2612 if (target->max_rx_bndl_sz)
2613 target->rx_bndl_enable = true;
2615 if ((target->tgt_cred_sz % target->block_sz) != 0) {
2616 ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
2617 target->tgt_cred_sz);
2620 * Disallow send bundling since the credit size is
2621 * not aligned to a block size the I/O block
2622 * padding will spill into the next credit buffer
2625 target->tx_bndl_mask = 0;
2629 int ath6kl_htc_wait_target(struct htc_target *target)
2631 struct htc_packet *packet = NULL;
2632 struct htc_ready_ext_msg *rdy_msg;
2633 struct htc_service_connect_req connect;
2634 struct htc_service_connect_resp resp;
2637 /* FIXME: remove once USB support is implemented */
2638 if (target->dev->ar->hif_type == ATH6KL_HIF_TYPE_USB) {
2639 ath6kl_err("HTC doesn't support USB yet. Patience!\n");
2643 /* we should be getting 1 control message that the target is ready */
2644 packet = htc_wait_for_ctrl_msg(target);
2649 /* we controlled the buffer creation so it's properly aligned */
2650 rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
2652 if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
2653 (packet->act_len < sizeof(struct htc_ready_msg))) {
2655 goto fail_wait_target;
2658 if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
2660 goto fail_wait_target;
2663 target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
2664 target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
2666 ath6kl_dbg(ATH6KL_DBG_BOOT,
2667 "htc target ready credits %d size %d\n",
2668 target->tgt_creds, target->tgt_cred_sz);
2670 /* check if this is an extended ready message */
2671 if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
2672 /* this is an extended message */
2673 target->htc_tgt_ver = rdy_msg->htc_ver;
2674 target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
2677 target->htc_tgt_ver = HTC_VERSION_2P0;
2678 target->msg_per_bndl_max = 0;
2681 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc using protocol %s (%d)\n",
2682 (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
2683 target->htc_tgt_ver);
2685 if (target->msg_per_bndl_max > 0)
2686 htc_setup_msg_bndl(target);
2688 /* setup our pseudo HTC control endpoint connection */
2689 memset(&connect, 0, sizeof(connect));
2690 memset(&resp, 0, sizeof(resp));
2691 connect.ep_cb.rx = htc_ctrl_rx;
2692 connect.ep_cb.rx_refill = NULL;
2693 connect.ep_cb.tx_full = NULL;
2694 connect.max_txq_depth = NUM_CONTROL_BUFFERS;
2695 connect.svc_id = HTC_CTRL_RSVD_SVC;
2697 /* connect fake service */
2698 status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
2702 * FIXME: this call doesn't make sense, the caller should
2703 * call ath6kl_htc_cleanup() when it wants remove htc
2705 ath6kl_hif_cleanup_scatter(target->dev->ar);
2709 htc_rxpkt_reset(packet);
2710 reclaim_rx_ctrl_buf(target, packet);
2717 * Start HTC, enable interrupts and let the target know
2718 * host has finished setup.
2720 int ath6kl_htc_start(struct htc_target *target)
2722 struct htc_packet *packet;
2725 memset(&target->dev->irq_proc_reg, 0,
2726 sizeof(target->dev->irq_proc_reg));
2728 /* Disable interrupts at the chip level */
2729 ath6kl_hif_disable_intrs(target->dev);
2731 target->htc_flags = 0;
2732 target->rx_st_flags = 0;
2734 /* Push control receive buffers into htc control endpoint */
2735 while ((packet = htc_get_control_buf(target, false)) != NULL) {
2736 status = htc_add_rxbuf(target, packet);
2741 /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2742 ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
2745 dump_cred_dist_stats(target);
2747 /* Indicate to the target of the setup completion */
2748 status = htc_setup_tx_complete(target);
2753 /* unmask interrupts */
2754 status = ath6kl_hif_unmask_intrs(target->dev);
2757 ath6kl_htc_stop(target);
2762 static int ath6kl_htc_reset(struct htc_target *target)
2764 u32 block_size, ctrl_bufsz;
2765 struct htc_packet *packet;
2768 reset_ep_state(target);
2770 block_size = target->dev->ar->mbox_info.block_size;
2772 ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
2773 (block_size + HTC_HDR_LENGTH) :
2774 (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
2776 for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
2777 packet = kzalloc(sizeof(*packet), GFP_KERNEL);
2781 packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
2782 if (!packet->buf_start) {
2787 packet->buf_len = ctrl_bufsz;
2788 if (i < NUM_CONTROL_RX_BUFFERS) {
2789 packet->act_len = 0;
2790 packet->buf = packet->buf_start;
2791 packet->endpoint = ENDPOINT_0;
2792 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
2794 list_add_tail(&packet->list, &target->free_ctrl_txbuf);
2800 /* htc_stop: stop interrupt reception, and flush all queued buffers */
2801 void ath6kl_htc_stop(struct htc_target *target)
2803 spin_lock_bh(&target->htc_lock);
2804 target->htc_flags |= HTC_OP_STATE_STOPPING;
2805 spin_unlock_bh(&target->htc_lock);
2808 * Masking interrupts is a synchronous operation, when this
2809 * function returns all pending HIF I/O has completed, we can
2810 * safely flush the queues.
2812 ath6kl_hif_mask_intrs(target->dev);
2814 ath6kl_htc_flush_txep_all(target);
2816 ath6kl_htc_flush_rx_buf(target);
2818 ath6kl_htc_reset(target);
2821 void *ath6kl_htc_create(struct ath6kl *ar)
2823 struct htc_target *target = NULL;
2826 target = kzalloc(sizeof(*target), GFP_KERNEL);
2828 ath6kl_err("unable to allocate memory\n");
2832 target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
2834 ath6kl_err("unable to allocate memory\n");
2836 goto err_htc_cleanup;
2839 spin_lock_init(&target->htc_lock);
2840 spin_lock_init(&target->rx_lock);
2841 spin_lock_init(&target->tx_lock);
2843 INIT_LIST_HEAD(&target->free_ctrl_txbuf);
2844 INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
2845 INIT_LIST_HEAD(&target->cred_dist_list);
2847 target->dev->ar = ar;
2848 target->dev->htc_cnxt = target;
2849 target->ep_waiting = ENDPOINT_MAX;
2851 status = ath6kl_hif_setup(target->dev);
2853 goto err_htc_cleanup;
2855 status = ath6kl_htc_reset(target);
2857 goto err_htc_cleanup;
2862 ath6kl_htc_cleanup(target);
2867 /* cleanup the HTC instance */
2868 void ath6kl_htc_cleanup(struct htc_target *target)
2870 struct htc_packet *packet, *tmp_packet;
2872 /* FIXME: remove check once USB support is implemented */
2873 if (target->dev->ar->hif_type != ATH6KL_HIF_TYPE_USB)
2874 ath6kl_hif_cleanup_scatter(target->dev->ar);
2876 list_for_each_entry_safe(packet, tmp_packet,
2877 &target->free_ctrl_txbuf, list) {
2878 list_del(&packet->list);
2879 kfree(packet->buf_start);
2883 list_for_each_entry_safe(packet, tmp_packet,
2884 &target->free_ctrl_rxbuf, list) {
2885 list_del(&packet->list);
2886 kfree(packet->buf_start);