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_dist_list;
177 list_for_each_entry(cur_dist_list, epdist_list, list) {
178 if (cur_dist_list->endpoint == ENDPOINT_0)
181 if (cur_dist_list->cred_to_dist > 0) {
182 cur_dist_list->credits +=
183 cur_dist_list->cred_to_dist;
184 cur_dist_list->cred_to_dist = 0;
185 if (cur_dist_list->credits >
186 cur_dist_list->cred_assngd)
187 ath6kl_credit_reduce(cred_info,
189 cur_dist_list->cred_assngd);
191 if (cur_dist_list->credits >
192 cur_dist_list->cred_norm)
193 ath6kl_credit_reduce(cred_info, cur_dist_list,
194 cur_dist_list->cred_norm);
196 if (!(cur_dist_list->dist_flags & HTC_EP_ACTIVE)) {
197 if (cur_dist_list->txq_depth == 0)
198 ath6kl_credit_reduce(cred_info,
206 * HTC has an endpoint that needs credits, ep_dist is the endpoint in
209 static void ath6kl_credit_seek(struct ath6kl_htc_credit_info *cred_info,
210 struct htc_endpoint_credit_dist *ep_dist)
212 struct htc_endpoint_credit_dist *curdist_list;
216 if (ep_dist->svc_id == WMI_CONTROL_SVC)
219 if ((ep_dist->svc_id == WMI_DATA_VI_SVC) ||
220 (ep_dist->svc_id == WMI_DATA_VO_SVC))
221 if ((ep_dist->cred_assngd >= ep_dist->cred_norm))
225 * For all other services, we follow a simple algorithm of:
227 * 1. checking the free pool for credits
228 * 2. checking lower priority endpoints for credits to take
231 credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
233 if (credits >= ep_dist->seek_cred)
237 * We don't have enough in the free pool, try taking away from
238 * lower priority services The rule for taking away credits:
240 * 1. Only take from lower priority endpoints
241 * 2. Only take what is allocated above the minimum (never
242 * starve an endpoint completely)
243 * 3. Only take what you need.
246 list_for_each_entry_reverse(curdist_list,
247 &cred_info->lowestpri_ep_dist,
249 if (curdist_list == ep_dist)
252 need = ep_dist->seek_cred - cred_info->cur_free_credits;
254 if ((curdist_list->cred_assngd - need) >=
255 curdist_list->cred_min) {
257 * The current one has been allocated more than
258 * it's minimum and it has enough credits assigned
259 * above it's minimum to fulfill our need try to
260 * take away just enough to fulfill our need.
262 ath6kl_credit_reduce(cred_info, curdist_list,
263 curdist_list->cred_assngd - need);
265 if (cred_info->cur_free_credits >=
270 if (curdist_list->endpoint == ENDPOINT_0)
274 credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
277 /* did we find some credits? */
279 ath6kl_credit_deposit(cred_info, ep_dist, credits);
281 ep_dist->seek_cred = 0;
284 /* redistribute credits based on activity change */
285 static void ath6kl_credit_redistribute(struct ath6kl_htc_credit_info *info,
286 struct list_head *ep_dist_list)
288 struct htc_endpoint_credit_dist *curdist_list;
290 list_for_each_entry(curdist_list, ep_dist_list, list) {
291 if (curdist_list->endpoint == ENDPOINT_0)
294 if ((curdist_list->svc_id == WMI_DATA_BK_SVC) ||
295 (curdist_list->svc_id == WMI_DATA_BE_SVC))
296 curdist_list->dist_flags |= HTC_EP_ACTIVE;
298 if ((curdist_list->svc_id != WMI_CONTROL_SVC) &&
299 !(curdist_list->dist_flags & HTC_EP_ACTIVE)) {
300 if (curdist_list->txq_depth == 0)
301 ath6kl_credit_reduce(info, curdist_list, 0);
303 ath6kl_credit_reduce(info,
305 curdist_list->cred_min);
312 * This function is invoked whenever endpoints require credit
313 * distributions. A lock is held while this function is invoked, this
314 * function shall NOT block. The ep_dist_list is a list of distribution
315 * structures in prioritized order as defined by the call to the
316 * htc_set_credit_dist() api.
318 static void ath6kl_credit_distribute(struct ath6kl_htc_credit_info *cred_info,
319 struct list_head *ep_dist_list,
320 enum htc_credit_dist_reason reason)
323 case HTC_CREDIT_DIST_SEND_COMPLETE:
324 ath6kl_credit_update(cred_info, ep_dist_list);
326 case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
327 ath6kl_credit_redistribute(cred_info, ep_dist_list);
333 WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
334 WARN_ON(cred_info->cur_free_credits < 0);
337 static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
341 if (!IS_ALIGNED((unsigned long) *buf, 4)) {
342 align_addr = PTR_ALIGN(*buf - 4, 4);
343 memmove(align_addr, *buf, len);
348 static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
349 int ctrl0, int ctrl1)
351 struct htc_frame_hdr *hdr;
353 packet->buf -= HTC_HDR_LENGTH;
354 hdr = (struct htc_frame_hdr *)packet->buf;
357 put_unaligned((u16)packet->act_len, &hdr->payld_len);
359 hdr->eid = packet->endpoint;
360 hdr->ctrl[0] = ctrl0;
361 hdr->ctrl[1] = ctrl1;
364 static void htc_reclaim_txctrl_buf(struct htc_target *target,
365 struct htc_packet *pkt)
367 spin_lock_bh(&target->htc_lock);
368 list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
369 spin_unlock_bh(&target->htc_lock);
372 static struct htc_packet *htc_get_control_buf(struct htc_target *target,
375 struct htc_packet *packet = NULL;
376 struct list_head *buf_list;
378 buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;
380 spin_lock_bh(&target->htc_lock);
382 if (list_empty(buf_list)) {
383 spin_unlock_bh(&target->htc_lock);
387 packet = list_first_entry(buf_list, struct htc_packet, list);
388 list_del(&packet->list);
389 spin_unlock_bh(&target->htc_lock);
392 packet->buf = packet->buf_start + HTC_HDR_LENGTH;
397 static void htc_tx_comp_update(struct htc_target *target,
398 struct htc_endpoint *endpoint,
399 struct htc_packet *packet)
401 packet->completion = NULL;
402 packet->buf += HTC_HDR_LENGTH;
407 ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
408 packet->status, packet->endpoint, packet->act_len,
409 packet->info.tx.cred_used);
411 /* on failure to submit, reclaim credits for this packet */
412 spin_lock_bh(&target->tx_lock);
413 endpoint->cred_dist.cred_to_dist +=
414 packet->info.tx.cred_used;
415 endpoint->cred_dist.txq_depth = get_queue_depth(&endpoint->txq);
417 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
418 target->credit_info, &target->cred_dist_list);
420 ath6kl_credit_distribute(target->credit_info,
421 &target->cred_dist_list,
422 HTC_CREDIT_DIST_SEND_COMPLETE);
424 spin_unlock_bh(&target->tx_lock);
427 static void htc_tx_complete(struct htc_endpoint *endpoint,
428 struct list_head *txq)
433 ath6kl_dbg(ATH6KL_DBG_HTC,
434 "htc tx complete ep %d pkts %d\n",
435 endpoint->eid, get_queue_depth(txq));
437 ath6kl_tx_complete(endpoint->target->dev->ar, txq);
440 static void htc_tx_comp_handler(struct htc_target *target,
441 struct htc_packet *packet)
443 struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
444 struct list_head container;
446 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx complete seqno %d\n",
447 packet->info.tx.seqno);
449 htc_tx_comp_update(target, endpoint, packet);
450 INIT_LIST_HEAD(&container);
451 list_add_tail(&packet->list, &container);
453 htc_tx_complete(endpoint, &container);
456 static void htc_async_tx_scat_complete(struct htc_target *target,
457 struct hif_scatter_req *scat_req)
459 struct htc_endpoint *endpoint;
460 struct htc_packet *packet;
461 struct list_head tx_compq;
464 INIT_LIST_HEAD(&tx_compq);
466 ath6kl_dbg(ATH6KL_DBG_HTC,
467 "htc tx scat complete len %d entries %d\n",
468 scat_req->len, scat_req->scat_entries);
470 if (scat_req->status)
471 ath6kl_err("send scatter req failed: %d\n", scat_req->status);
473 packet = scat_req->scat_list[0].packet;
474 endpoint = &target->endpoint[packet->endpoint];
476 /* walk through the scatter list and process */
477 for (i = 0; i < scat_req->scat_entries; i++) {
478 packet = scat_req->scat_list[i].packet;
484 packet->status = scat_req->status;
485 htc_tx_comp_update(target, endpoint, packet);
486 list_add_tail(&packet->list, &tx_compq);
489 /* free scatter request */
490 hif_scatter_req_add(target->dev->ar, scat_req);
492 /* complete all packets */
493 htc_tx_complete(endpoint, &tx_compq);
496 static int ath6kl_htc_tx_issue(struct htc_target *target,
497 struct htc_packet *packet)
501 u32 padded_len, send_len;
503 if (!packet->completion)
506 send_len = packet->act_len + HTC_HDR_LENGTH;
508 padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
510 ath6kl_dbg(ATH6KL_DBG_HTC,
511 "htc tx issue len %d seqno %d padded_len %d mbox 0x%X %s\n",
512 send_len, packet->info.tx.seqno, padded_len,
513 target->dev->ar->mbox_info.htc_addr,
514 sync ? "sync" : "async");
517 status = hif_read_write_sync(target->dev->ar,
518 target->dev->ar->mbox_info.htc_addr,
519 packet->buf, padded_len,
520 HIF_WR_SYNC_BLOCK_INC);
522 packet->status = status;
523 packet->buf += HTC_HDR_LENGTH;
525 status = hif_write_async(target->dev->ar,
526 target->dev->ar->mbox_info.htc_addr,
527 packet->buf, padded_len,
528 HIF_WR_ASYNC_BLOCK_INC, packet);
533 static int htc_check_credits(struct htc_target *target,
534 struct htc_endpoint *ep, u8 *flags,
535 enum htc_endpoint_id eid, unsigned int len,
539 *req_cred = (len > target->tgt_cred_sz) ?
540 DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
542 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit check need %d got %d\n",
543 *req_cred, ep->cred_dist.credits);
545 if (ep->cred_dist.credits < *req_cred) {
546 if (eid == ENDPOINT_0)
549 /* Seek more credits */
550 ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
552 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
554 ep->cred_dist.seek_cred = 0;
556 if (ep->cred_dist.credits < *req_cred) {
557 ath6kl_dbg(ATH6KL_DBG_CREDIT,
558 "credit not found for ep %d\n",
564 ep->cred_dist.credits -= *req_cred;
565 ep->ep_st.cred_cosumd += *req_cred;
567 /* When we are getting low on credits, ask for more */
568 if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
569 ep->cred_dist.seek_cred =
570 ep->cred_dist.cred_per_msg - ep->cred_dist.credits;
572 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
574 /* see if we were successful in getting more */
575 if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
576 /* tell the target we need credits ASAP! */
577 *flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
578 ep->ep_st.cred_low_indicate += 1;
579 ath6kl_dbg(ATH6KL_DBG_CREDIT,
580 "credit we need credits asap\n");
587 static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
588 struct htc_endpoint *endpoint,
589 struct list_head *queue)
593 struct htc_packet *packet;
600 if (list_empty(&endpoint->txq))
602 packet = list_first_entry(&endpoint->txq, struct htc_packet,
605 ath6kl_dbg(ATH6KL_DBG_HTC,
606 "htc tx got packet 0x%p queue depth %d\n",
607 packet, get_queue_depth(&endpoint->txq));
609 len = CALC_TXRX_PADDED_LEN(target,
610 packet->act_len + HTC_HDR_LENGTH);
612 if (htc_check_credits(target, endpoint, &flags,
613 packet->endpoint, len, &req_cred))
616 /* now we can fully move onto caller's queue */
617 packet = list_first_entry(&endpoint->txq, struct htc_packet,
619 list_move_tail(&packet->list, queue);
621 /* save the number of credits this packet consumed */
622 packet->info.tx.cred_used = req_cred;
624 /* all TX packets are handled asynchronously */
625 packet->completion = htc_tx_comp_handler;
626 packet->context = target;
627 endpoint->ep_st.tx_issued += 1;
629 /* save send flags */
630 packet->info.tx.flags = flags;
631 packet->info.tx.seqno = endpoint->seqno;
636 /* See if the padded tx length falls on a credit boundary */
637 static int htc_get_credit_padding(unsigned int cred_sz, int *len,
638 struct htc_endpoint *ep)
640 int rem_cred, cred_pad;
642 rem_cred = *len % cred_sz;
644 /* No padding needed */
648 if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
652 * The transfer consumes a "partial" credit, this
653 * packet cannot be bundled unless we add
654 * additional "dummy" padding (max 255 bytes) to
655 * consume the entire credit.
657 cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
659 if ((cred_pad > 0) && (cred_pad <= 255))
662 /* The amount of padding is too large, send as non-bundled */
668 static int ath6kl_htc_tx_setup_scat_list(struct htc_target *target,
669 struct htc_endpoint *endpoint,
670 struct hif_scatter_req *scat_req,
672 struct list_head *queue)
674 struct htc_packet *packet;
675 int i, len, rem_scat, cred_pad;
678 rem_scat = target->max_tx_bndl_sz;
680 for (i = 0; i < n_scat; i++) {
681 scat_req->scat_list[i].packet = NULL;
683 if (list_empty(queue))
686 packet = list_first_entry(queue, struct htc_packet, list);
687 len = CALC_TXRX_PADDED_LEN(target,
688 packet->act_len + HTC_HDR_LENGTH);
690 cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
692 if (cred_pad < 0 || rem_scat < len) {
698 /* now remove it from the queue */
699 list_del(&packet->list);
701 scat_req->scat_list[i].packet = packet;
702 /* prepare packet and flag message as part of a send bundle */
703 ath6kl_htc_tx_prep_pkt(packet,
704 packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE,
705 cred_pad, packet->info.tx.seqno);
706 /* Make sure the buffer is 4-byte aligned */
707 ath6kl_htc_tx_buf_align(&packet->buf,
708 packet->act_len + HTC_HDR_LENGTH);
709 scat_req->scat_list[i].buf = packet->buf;
710 scat_req->scat_list[i].len = len;
712 scat_req->len += len;
713 scat_req->scat_entries++;
714 ath6kl_dbg(ATH6KL_DBG_HTC,
715 "htc tx adding (%d) pkt 0x%p seqno %d len %d remaining %d\n",
716 i, packet, packet->info.tx.seqno, len, rem_scat);
719 /* Roll back scatter setup in case of any failure */
720 if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
721 for (i = scat_req->scat_entries - 1; i >= 0; i--) {
722 packet = scat_req->scat_list[i].packet;
724 packet->buf += HTC_HDR_LENGTH;
725 list_add(&packet->list, queue);
735 * Drain a queue and send as bundles this function may return without fully
736 * draining the queue when
738 * 1. scatter resources are exhausted
739 * 2. a message that will consume a partial credit will stop the
740 * bundling process early
741 * 3. we drop below the minimum number of messages for a bundle
743 static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
744 struct list_head *queue,
745 int *sent_bundle, int *n_bundle_pkts)
747 struct htc_target *target = endpoint->target;
748 struct hif_scatter_req *scat_req = NULL;
749 int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0;
754 if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) ||
755 (WMI_CONTROL_SVC != endpoint->svc_id))
756 ac = target->dev->ar->ep2ac_map[endpoint->eid];
760 n_scat = get_queue_depth(queue);
761 n_scat = min(n_scat, target->msg_per_bndl_max);
763 if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
764 /* not enough to bundle */
767 scat_req = hif_scatter_req_get(target->dev->ar);
770 /* no scatter resources */
771 ath6kl_dbg(ATH6KL_DBG_HTC,
772 "htc tx no more scatter resources\n");
776 if ((ac < WMM_NUM_AC) && (ac != WMM_AC_BK)) {
779 * BE, BK have priorities and bit
782 txb_mask = (1 << WMM_AC_BK);
785 * any AC with priority lower than
788 txb_mask = ((1 << ac) - 1);
790 * when the scatter request resources drop below a
791 * certain threshold, disable Tx bundling for all
792 * AC's with priority lower than the current requesting
793 * AC. Otherwise re-enable Tx bundling for them
795 if (scat_req->scat_q_depth < ATH6KL_SCATTER_REQS)
796 target->tx_bndl_mask &= ~txb_mask;
798 target->tx_bndl_mask |= txb_mask;
801 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
805 scat_req->scat_entries = 0;
807 status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
810 if (status == -EAGAIN) {
811 hif_scatter_req_add(target->dev->ar, scat_req);
815 /* send path is always asynchronous */
816 scat_req->complete = htc_async_tx_scat_complete;
818 tot_pkts_bundle += scat_req->scat_entries;
820 ath6kl_dbg(ATH6KL_DBG_HTC,
821 "htc tx scatter bytes %d entries %d\n",
822 scat_req->len, scat_req->scat_entries);
823 ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
829 *sent_bundle = n_sent_bundle;
830 *n_bundle_pkts = tot_pkts_bundle;
831 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
837 static void ath6kl_htc_tx_from_queue(struct htc_target *target,
838 struct htc_endpoint *endpoint)
840 struct list_head txq;
841 struct htc_packet *packet;
846 spin_lock_bh(&target->tx_lock);
848 endpoint->tx_proc_cnt++;
849 if (endpoint->tx_proc_cnt > 1) {
850 endpoint->tx_proc_cnt--;
851 spin_unlock_bh(&target->tx_lock);
852 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
857 * drain the endpoint TX queue for transmission as long
858 * as we have enough credits.
860 INIT_LIST_HEAD(&txq);
862 if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) ||
863 (WMI_CONTROL_SVC != endpoint->svc_id))
864 ac = target->dev->ar->ep2ac_map[endpoint->eid];
868 if (list_empty(&endpoint->txq))
871 ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
873 if (list_empty(&txq))
876 spin_unlock_bh(&target->tx_lock);
882 /* try to send a bundle on each pass */
883 if ((target->tx_bndl_mask) &&
884 (get_queue_depth(&txq) >=
885 HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
886 int temp1 = 0, temp2 = 0;
888 /* check if bundling is enabled for an AC */
889 if (target->tx_bndl_mask & (1 << ac)) {
890 ath6kl_htc_tx_bundle(endpoint, &txq,
892 bundle_sent += temp1;
893 n_pkts_bundle += temp2;
897 if (list_empty(&txq))
900 packet = list_first_entry(&txq, struct htc_packet,
902 list_del(&packet->list);
904 ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
905 0, packet->info.tx.seqno);
906 ath6kl_htc_tx_issue(target, packet);
909 spin_lock_bh(&target->tx_lock);
911 endpoint->ep_st.tx_bundles += bundle_sent;
912 endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
915 * if an AC has bundling disabled and no tx bundling
916 * has occured continously for a certain number of TX,
917 * enable tx bundling for this AC
920 if (!(target->tx_bndl_mask & (1 << ac)) &&
922 if (++target->ac_tx_count[ac] >=
923 TX_RESUME_BUNDLE_THRESHOLD) {
924 target->ac_tx_count[ac] = 0;
925 target->tx_bndl_mask |= (1 << ac);
929 /* tx bundling will reset the counter */
931 target->ac_tx_count[ac] = 0;
935 endpoint->tx_proc_cnt = 0;
936 spin_unlock_bh(&target->tx_lock);
939 static bool ath6kl_htc_tx_try(struct htc_target *target,
940 struct htc_endpoint *endpoint,
941 struct htc_packet *tx_pkt)
943 struct htc_ep_callbacks ep_cb;
945 bool overflow = false;
947 ep_cb = endpoint->ep_cb;
949 spin_lock_bh(&target->tx_lock);
950 txq_depth = get_queue_depth(&endpoint->txq);
951 spin_unlock_bh(&target->tx_lock);
953 if (txq_depth >= endpoint->max_txq_depth)
957 ath6kl_dbg(ATH6KL_DBG_HTC,
958 "htc tx overflow ep %d depth %d max %d\n",
959 endpoint->eid, txq_depth,
960 endpoint->max_txq_depth);
962 if (overflow && ep_cb.tx_full) {
963 if (ep_cb.tx_full(endpoint->target, tx_pkt) ==
964 HTC_SEND_FULL_DROP) {
965 endpoint->ep_st.tx_dropped += 1;
970 spin_lock_bh(&target->tx_lock);
971 list_add_tail(&tx_pkt->list, &endpoint->txq);
972 spin_unlock_bh(&target->tx_lock);
974 ath6kl_htc_tx_from_queue(target, endpoint);
979 static void htc_chk_ep_txq(struct htc_target *target)
981 struct htc_endpoint *endpoint;
982 struct htc_endpoint_credit_dist *cred_dist;
985 * Run through the credit distribution list to see if there are
986 * packets queued. NOTE: no locks need to be taken since the
987 * distribution list is not dynamic (cannot be re-ordered) and we
988 * are not modifying any state.
990 list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
991 endpoint = cred_dist->htc_ep;
993 spin_lock_bh(&target->tx_lock);
994 if (!list_empty(&endpoint->txq)) {
995 ath6kl_dbg(ATH6KL_DBG_HTC,
996 "htc creds ep %d credits %d pkts %d\n",
998 endpoint->cred_dist.credits,
999 get_queue_depth(&endpoint->txq));
1000 spin_unlock_bh(&target->tx_lock);
1002 * Try to start the stalled queue, this list is
1003 * ordered by priority. If there are credits
1004 * available the highest priority queue will get a
1005 * chance to reclaim credits from lower priority
1008 ath6kl_htc_tx_from_queue(target, endpoint);
1009 spin_lock_bh(&target->tx_lock);
1011 spin_unlock_bh(&target->tx_lock);
1015 static int htc_setup_tx_complete(struct htc_target *target)
1017 struct htc_packet *send_pkt = NULL;
1020 send_pkt = htc_get_control_buf(target, true);
1025 if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
1026 struct htc_setup_comp_ext_msg *setup_comp_ext;
1030 (struct htc_setup_comp_ext_msg *)send_pkt->buf;
1031 memset(setup_comp_ext, 0, sizeof(*setup_comp_ext));
1032 setup_comp_ext->msg_id =
1033 cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
1035 if (target->msg_per_bndl_max > 0) {
1036 /* Indicate HTC bundling to the target */
1037 flags |= HTC_SETUP_COMP_FLG_RX_BNDL_EN;
1038 setup_comp_ext->msg_per_rxbndl =
1039 target->msg_per_bndl_max;
1042 memcpy(&setup_comp_ext->flags, &flags,
1043 sizeof(setup_comp_ext->flags));
1044 set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp_ext,
1045 sizeof(struct htc_setup_comp_ext_msg),
1046 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
1049 struct htc_setup_comp_msg *setup_comp;
1050 setup_comp = (struct htc_setup_comp_msg *)send_pkt->buf;
1051 memset(setup_comp, 0, sizeof(struct htc_setup_comp_msg));
1052 setup_comp->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID);
1053 set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp,
1054 sizeof(struct htc_setup_comp_msg),
1055 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
1058 /* we want synchronous operation */
1059 send_pkt->completion = NULL;
1060 ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
1061 status = ath6kl_htc_tx_issue(target, send_pkt);
1063 if (send_pkt != NULL)
1064 htc_reclaim_txctrl_buf(target, send_pkt);
1069 void ath6kl_htc_set_credit_dist(struct htc_target *target,
1070 struct ath6kl_htc_credit_info *credit_info,
1071 u16 srvc_pri_order[], int list_len)
1073 struct htc_endpoint *endpoint;
1076 target->credit_info = credit_info;
1078 list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
1079 &target->cred_dist_list);
1081 for (i = 0; i < list_len; i++) {
1082 for (ep = ENDPOINT_1; ep < ENDPOINT_MAX; ep++) {
1083 endpoint = &target->endpoint[ep];
1084 if (endpoint->svc_id == srvc_pri_order[i]) {
1085 list_add_tail(&endpoint->cred_dist.list,
1086 &target->cred_dist_list);
1090 if (ep >= ENDPOINT_MAX) {
1097 int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet)
1099 struct htc_endpoint *endpoint;
1100 struct list_head queue;
1102 ath6kl_dbg(ATH6KL_DBG_HTC,
1103 "htc tx ep id %d buf 0x%p len %d\n",
1104 packet->endpoint, packet->buf, packet->act_len);
1106 if (packet->endpoint >= ENDPOINT_MAX) {
1111 endpoint = &target->endpoint[packet->endpoint];
1113 if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
1114 packet->status = (target->htc_flags & HTC_OP_STATE_STOPPING) ?
1115 -ECANCELED : -ENOSPC;
1116 INIT_LIST_HEAD(&queue);
1117 list_add(&packet->list, &queue);
1118 htc_tx_complete(endpoint, &queue);
1124 /* flush endpoint TX queue */
1125 void ath6kl_htc_flush_txep(struct htc_target *target,
1126 enum htc_endpoint_id eid, u16 tag)
1128 struct htc_packet *packet, *tmp_pkt;
1129 struct list_head discard_q, container;
1130 struct htc_endpoint *endpoint = &target->endpoint[eid];
1132 if (!endpoint->svc_id) {
1137 /* initialize the discard queue */
1138 INIT_LIST_HEAD(&discard_q);
1140 spin_lock_bh(&target->tx_lock);
1142 list_for_each_entry_safe(packet, tmp_pkt, &endpoint->txq, list) {
1143 if ((tag == HTC_TX_PACKET_TAG_ALL) ||
1144 (tag == packet->info.tx.tag))
1145 list_move_tail(&packet->list, &discard_q);
1148 spin_unlock_bh(&target->tx_lock);
1150 list_for_each_entry_safe(packet, tmp_pkt, &discard_q, list) {
1151 packet->status = -ECANCELED;
1152 list_del(&packet->list);
1153 ath6kl_dbg(ATH6KL_DBG_HTC,
1154 "htc tx flushing pkt 0x%p len %d ep %d tag 0x%x\n",
1155 packet, packet->act_len,
1156 packet->endpoint, packet->info.tx.tag);
1158 INIT_LIST_HEAD(&container);
1159 list_add_tail(&packet->list, &container);
1160 htc_tx_complete(endpoint, &container);
1165 static void ath6kl_htc_flush_txep_all(struct htc_target *target)
1167 struct htc_endpoint *endpoint;
1170 dump_cred_dist_stats(target);
1172 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
1173 endpoint = &target->endpoint[i];
1174 if (endpoint->svc_id == 0)
1177 ath6kl_htc_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
1181 void ath6kl_htc_indicate_activity_change(struct htc_target *target,
1182 enum htc_endpoint_id eid, bool active)
1184 struct htc_endpoint *endpoint = &target->endpoint[eid];
1187 if (endpoint->svc_id == 0) {
1192 spin_lock_bh(&target->tx_lock);
1195 if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
1196 endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
1200 if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
1201 endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
1207 endpoint->cred_dist.txq_depth =
1208 get_queue_depth(&endpoint->txq);
1210 ath6kl_dbg(ATH6KL_DBG_HTC,
1211 "htc tx activity ctxt 0x%p dist 0x%p\n",
1212 target->credit_info, &target->cred_dist_list);
1214 ath6kl_credit_distribute(target->credit_info,
1215 &target->cred_dist_list,
1216 HTC_CREDIT_DIST_ACTIVITY_CHANGE);
1219 spin_unlock_bh(&target->tx_lock);
1221 if (dist && !active)
1222 htc_chk_ep_txq(target);
1227 static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
1230 endpoint->ep_st.rx_pkts++;
1231 if (n_look_ahds == 1)
1232 endpoint->ep_st.rx_lkahds++;
1233 else if (n_look_ahds > 1)
1234 endpoint->ep_st.rx_bundle_lkahd++;
1237 static inline bool htc_valid_rx_frame_len(struct htc_target *target,
1238 enum htc_endpoint_id eid, int len)
1240 return (eid == target->dev->ar->ctrl_ep) ?
1241 len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
1244 static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
1246 struct list_head queue;
1248 INIT_LIST_HEAD(&queue);
1249 list_add_tail(&packet->list, &queue);
1250 return ath6kl_htc_add_rxbuf_multiple(target, &queue);
1253 static void htc_reclaim_rxbuf(struct htc_target *target,
1254 struct htc_packet *packet,
1255 struct htc_endpoint *ep)
1257 if (packet->info.rx.rx_flags & HTC_RX_PKT_NO_RECYCLE) {
1258 htc_rxpkt_reset(packet);
1259 packet->status = -ECANCELED;
1260 ep->ep_cb.rx(ep->target, packet);
1262 htc_rxpkt_reset(packet);
1263 htc_add_rxbuf((void *)(target), packet);
1267 static void reclaim_rx_ctrl_buf(struct htc_target *target,
1268 struct htc_packet *packet)
1270 spin_lock_bh(&target->htc_lock);
1271 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
1272 spin_unlock_bh(&target->htc_lock);
1275 static int ath6kl_htc_rx_packet(struct htc_target *target,
1276 struct htc_packet *packet,
1279 struct ath6kl_device *dev = target->dev;
1283 padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
1285 if (padded_len > packet->buf_len) {
1286 ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
1287 padded_len, rx_len, packet->buf_len);
1291 ath6kl_dbg(ATH6KL_DBG_HTC,
1292 "htc rx 0x%p hdr x%x len %d mbox 0x%x\n",
1293 packet, packet->info.rx.exp_hdr,
1294 padded_len, dev->ar->mbox_info.htc_addr);
1296 status = hif_read_write_sync(dev->ar,
1297 dev->ar->mbox_info.htc_addr,
1298 packet->buf, padded_len,
1299 HIF_RD_SYNC_BLOCK_FIX);
1301 packet->status = status;
1307 * optimization for recv packets, we can indicate a
1308 * "hint" that there are more single-packets to fetch
1311 static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
1312 struct htc_endpoint *endpoint,
1313 struct htc_packet *packet)
1315 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
1317 if (htc_hdr->eid == packet->endpoint) {
1318 if (!list_empty(&endpoint->rx_bufq))
1319 packet->info.rx.indicat_flags |=
1320 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
1324 static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
1326 struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
1328 if (ep_cb.rx_refill_thresh > 0) {
1329 spin_lock_bh(&endpoint->target->rx_lock);
1330 if (get_queue_depth(&endpoint->rx_bufq)
1331 < ep_cb.rx_refill_thresh) {
1332 spin_unlock_bh(&endpoint->target->rx_lock);
1333 ep_cb.rx_refill(endpoint->target, endpoint->eid);
1336 spin_unlock_bh(&endpoint->target->rx_lock);
1340 /* This function is called with rx_lock held */
1341 static int ath6kl_htc_rx_setup(struct htc_target *target,
1342 struct htc_endpoint *ep,
1343 u32 *lk_ahds, struct list_head *queue, int n_msg)
1345 struct htc_packet *packet;
1346 /* FIXME: type of lk_ahds can't be right */
1347 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
1348 struct htc_ep_callbacks ep_cb;
1349 int status = 0, j, full_len;
1352 full_len = CALC_TXRX_PADDED_LEN(target,
1353 le16_to_cpu(htc_hdr->payld_len) +
1356 if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
1357 ath6kl_warn("Rx buffer requested with invalid length\n");
1362 for (j = 0; j < n_msg; j++) {
1365 * Reset flag, any packets allocated using the
1366 * rx_alloc() API cannot be recycled on
1367 * cleanup,they must be explicitly returned.
1371 if (ep_cb.rx_allocthresh &&
1372 (full_len > ep_cb.rx_alloc_thresh)) {
1373 ep->ep_st.rx_alloc_thresh_hit += 1;
1374 ep->ep_st.rxalloc_thresh_byte +=
1375 le16_to_cpu(htc_hdr->payld_len);
1377 spin_unlock_bh(&target->rx_lock);
1380 packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
1382 spin_lock_bh(&target->rx_lock);
1384 /* refill handler is being used */
1385 if (list_empty(&ep->rx_bufq)) {
1386 if (ep_cb.rx_refill) {
1387 spin_unlock_bh(&target->rx_lock);
1388 ep_cb.rx_refill(ep->target, ep->eid);
1389 spin_lock_bh(&target->rx_lock);
1393 if (list_empty(&ep->rx_bufq))
1396 packet = list_first_entry(&ep->rx_bufq,
1397 struct htc_packet, list);
1398 list_del(&packet->list);
1403 target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
1404 target->ep_waiting = ep->eid;
1409 packet->info.rx.rx_flags = 0;
1410 packet->info.rx.indicat_flags = 0;
1415 * flag that these packets cannot be
1416 * recycled, they have to be returned to
1419 packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
1421 /* Caller needs to free this upon any failure */
1422 list_add_tail(&packet->list, queue);
1424 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1425 status = -ECANCELED;
1430 packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
1431 packet->info.rx.exp_hdr = 0xFFFFFFFF;
1433 /* set expected look ahead */
1434 packet->info.rx.exp_hdr = *lk_ahds;
1436 packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
1443 static int ath6kl_htc_rx_alloc(struct htc_target *target,
1444 u32 lk_ahds[], int msg,
1445 struct htc_endpoint *endpoint,
1446 struct list_head *queue)
1449 struct htc_packet *packet, *tmp_pkt;
1450 struct htc_frame_hdr *htc_hdr;
1453 spin_lock_bh(&target->rx_lock);
1455 for (i = 0; i < msg; i++) {
1457 htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
1459 if (htc_hdr->eid >= ENDPOINT_MAX) {
1460 ath6kl_err("invalid ep in look-ahead: %d\n",
1466 if (htc_hdr->eid != endpoint->eid) {
1467 ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
1468 htc_hdr->eid, endpoint->eid, i);
1473 if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
1474 ath6kl_err("payload len %d exceeds max htc : %d !\n",
1476 (u32) HTC_MAX_PAYLOAD_LENGTH);
1481 if (endpoint->svc_id == 0) {
1482 ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
1487 if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
1489 * HTC header indicates that every packet to follow
1490 * has the same padded length so that it can be
1491 * optimally fetched as a full bundle.
1493 n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
1494 HTC_FLG_RX_BNDL_CNT_S;
1496 /* the count doesn't include the starter frame */
1498 if (n_msg > target->msg_per_bndl_max) {
1503 endpoint->ep_st.rx_bundle_from_hdr += 1;
1504 ath6kl_dbg(ATH6KL_DBG_HTC,
1505 "htc rx bundle pkts %d\n",
1508 /* HTC header only indicates 1 message to fetch */
1511 /* Setup packet buffers for each message */
1512 status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
1516 * This is due to unavailabilty of buffers to rx entire data.
1517 * Return no error so that free buffers from queue can be used
1518 * to receive partial data.
1520 if (status == -ENOSPC) {
1521 spin_unlock_bh(&target->rx_lock);
1529 spin_unlock_bh(&target->rx_lock);
1532 list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
1533 list_del(&packet->list);
1534 htc_reclaim_rxbuf(target, packet,
1535 &target->endpoint[packet->endpoint]);
1542 static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
1544 if (packets->endpoint != ENDPOINT_0) {
1549 if (packets->status == -ECANCELED) {
1550 reclaim_rx_ctrl_buf(context, packets);
1554 if (packets->act_len > 0) {
1555 ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
1556 packets->act_len + HTC_HDR_LENGTH);
1558 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1559 "htc rx unexpected endpoint 0 message", "",
1560 packets->buf - HTC_HDR_LENGTH,
1561 packets->act_len + HTC_HDR_LENGTH);
1564 htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
1567 static void htc_proc_cred_rpt(struct htc_target *target,
1568 struct htc_credit_report *rpt,
1570 enum htc_endpoint_id from_ep)
1572 struct htc_endpoint *endpoint;
1573 int tot_credits = 0, i;
1576 spin_lock_bh(&target->tx_lock);
1578 for (i = 0; i < n_entries; i++, rpt++) {
1579 if (rpt->eid >= ENDPOINT_MAX) {
1581 spin_unlock_bh(&target->tx_lock);
1585 endpoint = &target->endpoint[rpt->eid];
1587 ath6kl_dbg(ATH6KL_DBG_CREDIT,
1588 "credit report ep %d credits %d\n",
1589 rpt->eid, rpt->credits);
1591 endpoint->ep_st.tx_cred_rpt += 1;
1592 endpoint->ep_st.cred_retnd += rpt->credits;
1594 if (from_ep == rpt->eid) {
1596 * This credit report arrived on the same endpoint
1597 * indicating it arrived in an RX packet.
1599 endpoint->ep_st.cred_from_rx += rpt->credits;
1600 endpoint->ep_st.cred_rpt_from_rx += 1;
1601 } else if (from_ep == ENDPOINT_0) {
1602 /* credit arrived on endpoint 0 as a NULL message */
1603 endpoint->ep_st.cred_from_ep0 += rpt->credits;
1604 endpoint->ep_st.cred_rpt_ep0 += 1;
1606 endpoint->ep_st.cred_from_other += rpt->credits;
1607 endpoint->ep_st.cred_rpt_from_other += 1;
1610 if (rpt->eid == ENDPOINT_0)
1611 /* always give endpoint 0 credits back */
1612 endpoint->cred_dist.credits += rpt->credits;
1614 endpoint->cred_dist.cred_to_dist += rpt->credits;
1619 * Refresh tx depth for distribution function that will
1620 * recover these credits NOTE: this is only valid when
1621 * there are credits to recover!
1623 endpoint->cred_dist.txq_depth =
1624 get_queue_depth(&endpoint->txq);
1626 tot_credits += rpt->credits;
1631 * This was a credit return based on a completed send
1632 * operations note, this is done with the lock held
1634 ath6kl_credit_distribute(target->credit_info,
1635 &target->cred_dist_list,
1636 HTC_CREDIT_DIST_SEND_COMPLETE);
1639 spin_unlock_bh(&target->tx_lock);
1642 htc_chk_ep_txq(target);
1645 static int htc_parse_trailer(struct htc_target *target,
1646 struct htc_record_hdr *record,
1647 u8 *record_buf, u32 *next_lk_ahds,
1648 enum htc_endpoint_id endpoint,
1651 struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
1652 struct htc_lookahead_report *lk_ahd;
1655 switch (record->rec_id) {
1656 case HTC_RECORD_CREDITS:
1657 len = record->len / sizeof(struct htc_credit_report);
1663 htc_proc_cred_rpt(target,
1664 (struct htc_credit_report *) record_buf,
1667 case HTC_RECORD_LOOKAHEAD:
1668 len = record->len / sizeof(*lk_ahd);
1674 lk_ahd = (struct htc_lookahead_report *) record_buf;
1675 if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF)) &&
1678 ath6kl_dbg(ATH6KL_DBG_HTC,
1679 "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
1680 lk_ahd->pre_valid, lk_ahd->post_valid);
1682 /* look ahead bytes are valid, copy them over */
1683 memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);
1685 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1686 "htc rx next look ahead",
1687 "", next_lk_ahds, 4);
1692 case HTC_RECORD_LOOKAHEAD_BUNDLE:
1693 len = record->len / sizeof(*bundle_lkahd_rpt);
1694 if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
1703 (struct htc_bundle_lkahd_rpt *) record_buf;
1705 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
1706 "", record_buf, record->len);
1708 for (i = 0; i < len; i++) {
1709 memcpy((u8 *)&next_lk_ahds[i],
1710 bundle_lkahd_rpt->lk_ahd, 4);
1718 ath6kl_err("unhandled record: id:%d len:%d\n",
1719 record->rec_id, record->len);
1727 static int htc_proc_trailer(struct htc_target *target,
1728 u8 *buf, int len, u32 *next_lk_ahds,
1729 int *n_lk_ahds, enum htc_endpoint_id endpoint)
1731 struct htc_record_hdr *record;
1737 ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
1738 ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
1746 if (len < sizeof(struct htc_record_hdr)) {
1750 /* these are byte aligned structs */
1751 record = (struct htc_record_hdr *) buf;
1752 len -= sizeof(struct htc_record_hdr);
1753 buf += sizeof(struct htc_record_hdr);
1755 if (record->len > len) {
1756 ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
1757 record->len, record->rec_id, len);
1763 status = htc_parse_trailer(target, record, record_buf,
1764 next_lk_ahds, endpoint, n_lk_ahds);
1769 /* advance buffer past this record for next time around */
1775 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
1776 "", orig_buf, orig_len);
1781 static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
1782 struct htc_packet *packet,
1783 u32 *next_lkahds, int *n_lkahds)
1788 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
1790 if (n_lkahds != NULL)
1794 * NOTE: we cannot assume the alignment of buf, so we use the safe
1795 * macros to retrieve 16 bit fields.
1797 payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
1799 memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
1801 if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
1803 * Refresh the expected header and the actual length as it
1804 * was unknown when this packet was grabbed as part of the
1807 packet->info.rx.exp_hdr = lk_ahd;
1808 packet->act_len = payload_len + HTC_HDR_LENGTH;
1810 /* validate the actual header that was refreshed */
1811 if (packet->act_len > packet->buf_len) {
1812 ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
1813 payload_len, lk_ahd);
1815 * Limit this to max buffer just to print out some
1818 packet->act_len = min(packet->act_len, packet->buf_len);
1823 if (packet->endpoint != htc_hdr->eid) {
1824 ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
1825 htc_hdr->eid, packet->endpoint);
1831 if (lk_ahd != packet->info.rx.exp_hdr) {
1832 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1833 __func__, packet, packet->info.rx.rx_flags);
1834 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
1835 "", &packet->info.rx.exp_hdr, 4);
1836 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
1837 "", (u8 *)&lk_ahd, sizeof(lk_ahd));
1842 if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
1843 if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
1844 htc_hdr->ctrl[0] > payload_len) {
1845 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1846 __func__, payload_len, htc_hdr->ctrl[0]);
1851 if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
1856 status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
1857 + payload_len - htc_hdr->ctrl[0],
1858 htc_hdr->ctrl[0], next_lkahds,
1859 n_lkahds, packet->endpoint);
1864 packet->act_len -= htc_hdr->ctrl[0];
1867 packet->buf += HTC_HDR_LENGTH;
1868 packet->act_len -= HTC_HDR_LENGTH;
1872 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1873 "", packet->buf, packet->act_len);
1878 static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1879 struct htc_packet *packet)
1881 ath6kl_dbg(ATH6KL_DBG_HTC,
1882 "htc rx complete ep %d packet 0x%p\n",
1883 endpoint->eid, packet);
1884 endpoint->ep_cb.rx(endpoint->target, packet);
1887 static int ath6kl_htc_rx_bundle(struct htc_target *target,
1888 struct list_head *rxq,
1889 struct list_head *sync_compq,
1890 int *n_pkt_fetched, bool part_bundle)
1892 struct hif_scatter_req *scat_req;
1893 struct htc_packet *packet;
1894 int rem_space = target->max_rx_bndl_sz;
1895 int n_scat_pkt, status = 0, i, len;
1897 n_scat_pkt = get_queue_depth(rxq);
1898 n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
1900 if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
1902 * We were forced to split this bundle receive operation
1903 * all packets in this partial bundle must have their
1904 * lookaheads ignored.
1909 * This would only happen if the target ignored our max
1912 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1913 __func__, get_queue_depth(rxq), n_scat_pkt);
1918 ath6kl_dbg(ATH6KL_DBG_HTC,
1919 "htc rx bundle depth %d pkts %d\n",
1920 get_queue_depth(rxq), n_scat_pkt);
1922 scat_req = hif_scatter_req_get(target->dev->ar);
1924 if (scat_req == NULL)
1927 for (i = 0; i < n_scat_pkt; i++) {
1930 packet = list_first_entry(rxq, struct htc_packet, list);
1931 list_del(&packet->list);
1933 pad_len = CALC_TXRX_PADDED_LEN(target,
1936 if ((rem_space - pad_len) < 0) {
1937 list_add(&packet->list, rxq);
1941 rem_space -= pad_len;
1943 if (part_bundle || (i < (n_scat_pkt - 1)))
1945 * Packet 0..n-1 cannot be checked for look-aheads
1946 * since we are fetching a bundle the last packet
1947 * however can have it's lookahead used
1949 packet->info.rx.rx_flags |=
1950 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1952 /* NOTE: 1 HTC packet per scatter entry */
1953 scat_req->scat_list[i].buf = packet->buf;
1954 scat_req->scat_list[i].len = pad_len;
1956 packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
1958 list_add_tail(&packet->list, sync_compq);
1960 WARN_ON(!scat_req->scat_list[i].len);
1961 len += scat_req->scat_list[i].len;
1964 scat_req->len = len;
1965 scat_req->scat_entries = i;
1967 status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
1972 /* free scatter request */
1973 hif_scatter_req_add(target->dev->ar, scat_req);
1980 static int ath6kl_htc_rx_process_packets(struct htc_target *target,
1981 struct list_head *comp_pktq,
1985 struct htc_packet *packet, *tmp_pkt;
1986 struct htc_endpoint *ep;
1989 list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
1990 ep = &target->endpoint[packet->endpoint];
1992 /* process header for each of the recv packet */
1993 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
1998 list_del(&packet->list);
2000 if (list_empty(comp_pktq)) {
2002 * Last packet's more packet flag is set
2003 * based on the lookahead.
2006 ath6kl_htc_rx_set_indicate(lk_ahds[0],
2010 * Packets in a bundle automatically have
2013 packet->info.rx.indicat_flags |=
2014 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
2016 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
2018 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
2019 ep->ep_st.rx_bundl += 1;
2021 ath6kl_htc_rx_complete(ep, packet);
2027 static int ath6kl_htc_rx_fetch(struct htc_target *target,
2028 struct list_head *rx_pktq,
2029 struct list_head *comp_pktq)
2032 bool part_bundle = false;
2034 struct list_head tmp_rxq;
2035 struct htc_packet *packet, *tmp_pkt;
2037 /* now go fetch the list of HTC packets */
2038 while (!list_empty(rx_pktq)) {
2041 INIT_LIST_HEAD(&tmp_rxq);
2043 if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
2045 * There are enough packets to attempt a
2046 * bundle transfer and recv bundling is
2049 status = ath6kl_htc_rx_bundle(target, rx_pktq,
2056 if (!list_empty(rx_pktq))
2059 list_splice_tail_init(&tmp_rxq, comp_pktq);
2062 if (!fetched_pkts) {
2064 packet = list_first_entry(rx_pktq, struct htc_packet,
2067 /* fully synchronous */
2068 packet->completion = NULL;
2070 if (!list_is_singular(rx_pktq))
2072 * look_aheads in all packet
2073 * except the last one in the
2074 * bundle must be ignored
2076 packet->info.rx.rx_flags |=
2077 HTC_RX_PKT_IGNORE_LOOKAHEAD;
2079 /* go fetch the packet */
2080 status = ath6kl_htc_rx_packet(target, packet,
2083 list_move_tail(&packet->list, &tmp_rxq);
2088 list_splice_tail_init(&tmp_rxq, comp_pktq);
2097 * Cleanup any packets we allocated but didn't use to
2098 * actually fetch any packets.
2101 list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
2102 list_del(&packet->list);
2103 htc_reclaim_rxbuf(target, packet,
2104 &target->endpoint[packet->endpoint]);
2107 list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
2108 list_del(&packet->list);
2109 htc_reclaim_rxbuf(target, packet,
2110 &target->endpoint[packet->endpoint]);
2116 int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
2117 u32 msg_look_ahead, int *num_pkts)
2119 struct htc_packet *packets, *tmp_pkt;
2120 struct htc_endpoint *endpoint;
2121 struct list_head rx_pktq, comp_pktq;
2123 u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
2124 int num_look_ahead = 1;
2125 enum htc_endpoint_id id;
2128 INIT_LIST_HEAD(&comp_pktq);
2132 * On first entry copy the look_aheads into our temp array for
2135 look_aheads[0] = msg_look_ahead;
2140 * First lookahead sets the expected endpoint IDs for all
2141 * packets in a bundle.
2143 id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
2144 endpoint = &target->endpoint[id];
2146 if (id >= ENDPOINT_MAX) {
2147 ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
2153 INIT_LIST_HEAD(&rx_pktq);
2154 INIT_LIST_HEAD(&comp_pktq);
2157 * Try to allocate as many HTC RX packets indicated by the
2160 status = ath6kl_htc_rx_alloc(target, look_aheads,
2161 num_look_ahead, endpoint,
2166 if (get_queue_depth(&rx_pktq) >= 2)
2168 * A recv bundle was detected, force IRQ status
2171 target->chk_irq_status_cnt = 1;
2173 n_fetched += get_queue_depth(&rx_pktq);
2177 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
2180 ath6kl_htc_rx_chk_water_mark(endpoint);
2182 /* Process fetched packets */
2183 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
2187 if (!num_look_ahead || status)
2191 * For SYNCH processing, if we get here, we are running
2192 * through the loop again due to a detected lookahead. Set
2193 * flag that we should re-check IRQ status registers again
2194 * before leaving IRQ processing, this can net better
2195 * performance in high throughput situations.
2197 target->chk_irq_status_cnt = 1;
2201 ath6kl_err("failed to get pending recv messages: %d\n",
2204 /* cleanup any packets in sync completion queue */
2205 list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
2206 list_del(&packets->list);
2207 htc_reclaim_rxbuf(target, packets,
2208 &target->endpoint[packets->endpoint]);
2211 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2212 ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
2213 ath6kl_hif_rx_control(target->dev, false);
2218 * Before leaving, check to see if host ran out of buffers and
2219 * needs to stop the receiver.
2221 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
2222 ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
2223 ath6kl_hif_rx_control(target->dev, false);
2225 *num_pkts = n_fetched;
2231 * Synchronously wait for a control message from the target,
2232 * This function is used at initialization time ONLY. At init messages
2233 * on ENDPOINT 0 are expected.
2235 static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
2237 struct htc_packet *packet = NULL;
2238 struct htc_frame_hdr *htc_hdr;
2241 if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
2242 HTC_TARGET_RESPONSE_TIMEOUT))
2245 ath6kl_dbg(ATH6KL_DBG_HTC,
2246 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
2248 htc_hdr = (struct htc_frame_hdr *)&look_ahead;
2250 if (htc_hdr->eid != ENDPOINT_0)
2253 packet = htc_get_control_buf(target, false);
2258 packet->info.rx.rx_flags = 0;
2259 packet->info.rx.exp_hdr = look_ahead;
2260 packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;
2262 if (packet->act_len > packet->buf_len)
2265 /* we want synchronous operation */
2266 packet->completion = NULL;
2268 /* get the message from the device, this will block */
2269 if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
2272 /* process receive header */
2273 packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
2275 if (packet->status) {
2276 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
2284 if (packet != NULL) {
2285 htc_rxpkt_reset(packet);
2286 reclaim_rx_ctrl_buf(target, packet);
2292 int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
2293 struct list_head *pkt_queue)
2295 struct htc_endpoint *endpoint;
2296 struct htc_packet *first_pkt;
2297 bool rx_unblock = false;
2298 int status = 0, depth;
2300 if (list_empty(pkt_queue))
2303 first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
2305 if (first_pkt->endpoint >= ENDPOINT_MAX)
2308 depth = get_queue_depth(pkt_queue);
2310 ath6kl_dbg(ATH6KL_DBG_HTC,
2311 "htc rx add multiple ep id %d cnt %d len %d\n",
2312 first_pkt->endpoint, depth, first_pkt->buf_len);
2314 endpoint = &target->endpoint[first_pkt->endpoint];
2316 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2317 struct htc_packet *packet, *tmp_pkt;
2319 /* walk through queue and mark each one canceled */
2320 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
2321 packet->status = -ECANCELED;
2322 list_del(&packet->list);
2323 ath6kl_htc_rx_complete(endpoint, packet);
2329 spin_lock_bh(&target->rx_lock);
2331 list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
2333 /* check if we are blocked waiting for a new buffer */
2334 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
2335 if (target->ep_waiting == first_pkt->endpoint) {
2336 ath6kl_dbg(ATH6KL_DBG_HTC,
2337 "htc rx blocked on ep %d, unblocking\n",
2338 target->ep_waiting);
2339 target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
2340 target->ep_waiting = ENDPOINT_MAX;
2345 spin_unlock_bh(&target->rx_lock);
2347 if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
2348 /* TODO : implement a buffer threshold count? */
2349 ath6kl_hif_rx_control(target->dev, true);
2354 void ath6kl_htc_flush_rx_buf(struct htc_target *target)
2356 struct htc_endpoint *endpoint;
2357 struct htc_packet *packet, *tmp_pkt;
2360 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2361 endpoint = &target->endpoint[i];
2362 if (!endpoint->svc_id)
2366 spin_lock_bh(&target->rx_lock);
2367 list_for_each_entry_safe(packet, tmp_pkt,
2368 &endpoint->rx_bufq, list) {
2369 list_del(&packet->list);
2370 spin_unlock_bh(&target->rx_lock);
2371 ath6kl_dbg(ATH6KL_DBG_HTC,
2372 "htc rx flush pkt 0x%p len %d ep %d\n",
2373 packet, packet->buf_len,
2376 * packets in rx_bufq of endpoint 0 have originally
2377 * been queued from target->free_ctrl_rxbuf where
2378 * packet and packet->buf_start are allocated
2379 * separately using kmalloc(). For other endpoint
2380 * rx_bufq, it is allocated as skb where packet is
2381 * skb->head. Take care of this difference while freeing
2384 if (packet->endpoint == ENDPOINT_0) {
2385 kfree(packet->buf_start);
2388 dev_kfree_skb(packet->pkt_cntxt);
2390 spin_lock_bh(&target->rx_lock);
2392 spin_unlock_bh(&target->rx_lock);
2396 int ath6kl_htc_conn_service(struct htc_target *target,
2397 struct htc_service_connect_req *conn_req,
2398 struct htc_service_connect_resp *conn_resp)
2400 struct htc_packet *rx_pkt = NULL;
2401 struct htc_packet *tx_pkt = NULL;
2402 struct htc_conn_service_resp *resp_msg;
2403 struct htc_conn_service_msg *conn_msg;
2404 struct htc_endpoint *endpoint;
2405 enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
2406 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;
2452 if ((le16_to_cpu(resp_msg->msg_id) != HTC_MSG_CONN_SVC_RESP_ID) ||
2453 (rx_pkt->act_len < sizeof(*resp_msg))) {
2458 conn_resp->resp_code = resp_msg->status;
2459 /* check response status */
2460 if (resp_msg->status != HTC_SERVICE_SUCCESS) {
2461 ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
2462 resp_msg->svc_id, resp_msg->status);
2467 assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
2468 max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
2471 if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
2476 endpoint = &target->endpoint[assigned_ep];
2477 endpoint->eid = assigned_ep;
2478 if (endpoint->svc_id) {
2483 /* return assigned endpoint to caller */
2484 conn_resp->endpoint = assigned_ep;
2485 conn_resp->len_max = max_msg_sz;
2487 /* setup the endpoint */
2489 /* this marks the endpoint in use */
2490 endpoint->svc_id = conn_req->svc_id;
2492 endpoint->max_txq_depth = conn_req->max_txq_depth;
2493 endpoint->len_max = max_msg_sz;
2494 endpoint->ep_cb = conn_req->ep_cb;
2495 endpoint->cred_dist.svc_id = conn_req->svc_id;
2496 endpoint->cred_dist.htc_ep = endpoint;
2497 endpoint->cred_dist.endpoint = assigned_ep;
2498 endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
2500 switch (endpoint->svc_id) {
2501 case WMI_DATA_BK_SVC:
2502 endpoint->tx_drop_packet_threshold = MAX_DEF_COOKIE_NUM / 3;
2505 endpoint->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
2509 if (conn_req->max_rxmsg_sz) {
2511 * Override cred_per_msg calculation, this optimizes
2512 * the credit-low indications since the host will actually
2513 * issue smaller messages in the Send path.
2515 if (conn_req->max_rxmsg_sz > max_msg_sz) {
2519 endpoint->cred_dist.cred_per_msg =
2520 conn_req->max_rxmsg_sz / target->tgt_cred_sz;
2522 endpoint->cred_dist.cred_per_msg =
2523 max_msg_sz / target->tgt_cred_sz;
2525 if (!endpoint->cred_dist.cred_per_msg)
2526 endpoint->cred_dist.cred_per_msg = 1;
2528 /* save local connection flags */
2529 endpoint->conn_flags = conn_req->flags;
2533 htc_reclaim_txctrl_buf(target, tx_pkt);
2536 htc_rxpkt_reset(rx_pkt);
2537 reclaim_rx_ctrl_buf(target, rx_pkt);
2543 static void reset_ep_state(struct htc_target *target)
2545 struct htc_endpoint *endpoint;
2548 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2549 endpoint = &target->endpoint[i];
2550 memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
2551 endpoint->svc_id = 0;
2552 endpoint->len_max = 0;
2553 endpoint->max_txq_depth = 0;
2554 memset(&endpoint->ep_st, 0,
2555 sizeof(endpoint->ep_st));
2556 INIT_LIST_HEAD(&endpoint->rx_bufq);
2557 INIT_LIST_HEAD(&endpoint->txq);
2558 endpoint->target = target;
2561 /* reset distribution list */
2562 /* FIXME: free existing entries */
2563 INIT_LIST_HEAD(&target->cred_dist_list);
2566 int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
2567 enum htc_endpoint_id endpoint)
2571 spin_lock_bh(&target->rx_lock);
2572 num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
2573 spin_unlock_bh(&target->rx_lock);
2577 static void htc_setup_msg_bndl(struct htc_target *target)
2579 /* limit what HTC can handle */
2580 target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
2581 target->msg_per_bndl_max);
2583 if (ath6kl_hif_enable_scatter(target->dev->ar)) {
2584 target->msg_per_bndl_max = 0;
2588 /* limit bundle what the device layer can handle */
2589 target->msg_per_bndl_max = min(target->max_scat_entries,
2590 target->msg_per_bndl_max);
2592 ath6kl_dbg(ATH6KL_DBG_BOOT,
2593 "htc bundling allowed msg_per_bndl_max %d\n",
2594 target->msg_per_bndl_max);
2596 /* Max rx bundle size is limited by the max tx bundle size */
2597 target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
2598 /* Max tx bundle size if limited by the extended mbox address range */
2599 target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
2600 target->max_xfer_szper_scatreq);
2602 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
2603 target->max_rx_bndl_sz, target->max_tx_bndl_sz);
2605 if (target->max_tx_bndl_sz)
2606 /* tx_bndl_mask is enabled per AC, each has 1 bit */
2607 target->tx_bndl_mask = (1 << WMM_NUM_AC) - 1;
2609 if (target->max_rx_bndl_sz)
2610 target->rx_bndl_enable = true;
2612 if ((target->tgt_cred_sz % target->block_sz) != 0) {
2613 ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
2614 target->tgt_cred_sz);
2617 * Disallow send bundling since the credit size is
2618 * not aligned to a block size the I/O block
2619 * padding will spill into the next credit buffer
2622 target->tx_bndl_mask = 0;
2626 int ath6kl_htc_wait_target(struct htc_target *target)
2628 struct htc_packet *packet = NULL;
2629 struct htc_ready_ext_msg *rdy_msg;
2630 struct htc_service_connect_req connect;
2631 struct htc_service_connect_resp resp;
2634 /* FIXME: remove once USB support is implemented */
2635 if (target->dev->ar->hif_type == ATH6KL_HIF_TYPE_USB) {
2636 ath6kl_err("HTC doesn't support USB yet. Patience!\n");
2640 /* we should be getting 1 control message that the target is ready */
2641 packet = htc_wait_for_ctrl_msg(target);
2646 /* we controlled the buffer creation so it's properly aligned */
2647 rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
2649 if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
2650 (packet->act_len < sizeof(struct htc_ready_msg))) {
2652 goto fail_wait_target;
2655 if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
2657 goto fail_wait_target;
2660 target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
2661 target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
2663 ath6kl_dbg(ATH6KL_DBG_BOOT,
2664 "htc target ready credits %d size %d\n",
2665 target->tgt_creds, target->tgt_cred_sz);
2667 /* check if this is an extended ready message */
2668 if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
2669 /* this is an extended message */
2670 target->htc_tgt_ver = rdy_msg->htc_ver;
2671 target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
2674 target->htc_tgt_ver = HTC_VERSION_2P0;
2675 target->msg_per_bndl_max = 0;
2678 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc using protocol %s (%d)\n",
2679 (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
2680 target->htc_tgt_ver);
2682 if (target->msg_per_bndl_max > 0)
2683 htc_setup_msg_bndl(target);
2685 /* setup our pseudo HTC control endpoint connection */
2686 memset(&connect, 0, sizeof(connect));
2687 memset(&resp, 0, sizeof(resp));
2688 connect.ep_cb.rx = htc_ctrl_rx;
2689 connect.ep_cb.rx_refill = NULL;
2690 connect.ep_cb.tx_full = NULL;
2691 connect.max_txq_depth = NUM_CONTROL_BUFFERS;
2692 connect.svc_id = HTC_CTRL_RSVD_SVC;
2694 /* connect fake service */
2695 status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
2699 * FIXME: this call doesn't make sense, the caller should
2700 * call ath6kl_htc_cleanup() when it wants remove htc
2702 ath6kl_hif_cleanup_scatter(target->dev->ar);
2706 htc_rxpkt_reset(packet);
2707 reclaim_rx_ctrl_buf(target, packet);
2714 * Start HTC, enable interrupts and let the target know
2715 * host has finished setup.
2717 int ath6kl_htc_start(struct htc_target *target)
2719 struct htc_packet *packet;
2722 memset(&target->dev->irq_proc_reg, 0,
2723 sizeof(target->dev->irq_proc_reg));
2725 /* Disable interrupts at the chip level */
2726 ath6kl_hif_disable_intrs(target->dev);
2728 target->htc_flags = 0;
2729 target->rx_st_flags = 0;
2731 /* Push control receive buffers into htc control endpoint */
2732 while ((packet = htc_get_control_buf(target, false)) != NULL) {
2733 status = htc_add_rxbuf(target, packet);
2738 /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2739 ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
2742 dump_cred_dist_stats(target);
2744 /* Indicate to the target of the setup completion */
2745 status = htc_setup_tx_complete(target);
2750 /* unmask interrupts */
2751 status = ath6kl_hif_unmask_intrs(target->dev);
2754 ath6kl_htc_stop(target);
2759 static int ath6kl_htc_reset(struct htc_target *target)
2761 u32 block_size, ctrl_bufsz;
2762 struct htc_packet *packet;
2765 reset_ep_state(target);
2767 block_size = target->dev->ar->mbox_info.block_size;
2769 ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
2770 (block_size + HTC_HDR_LENGTH) :
2771 (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
2773 for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
2774 packet = kzalloc(sizeof(*packet), GFP_KERNEL);
2778 packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
2779 if (!packet->buf_start) {
2784 packet->buf_len = ctrl_bufsz;
2785 if (i < NUM_CONTROL_RX_BUFFERS) {
2786 packet->act_len = 0;
2787 packet->buf = packet->buf_start;
2788 packet->endpoint = ENDPOINT_0;
2789 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
2791 list_add_tail(&packet->list, &target->free_ctrl_txbuf);
2797 /* htc_stop: stop interrupt reception, and flush all queued buffers */
2798 void ath6kl_htc_stop(struct htc_target *target)
2800 spin_lock_bh(&target->htc_lock);
2801 target->htc_flags |= HTC_OP_STATE_STOPPING;
2802 spin_unlock_bh(&target->htc_lock);
2805 * Masking interrupts is a synchronous operation, when this
2806 * function returns all pending HIF I/O has completed, we can
2807 * safely flush the queues.
2809 ath6kl_hif_mask_intrs(target->dev);
2811 ath6kl_htc_flush_txep_all(target);
2813 ath6kl_htc_flush_rx_buf(target);
2815 ath6kl_htc_reset(target);
2818 void *ath6kl_htc_create(struct ath6kl *ar)
2820 struct htc_target *target = NULL;
2823 target = kzalloc(sizeof(*target), GFP_KERNEL);
2825 ath6kl_err("unable to allocate memory\n");
2829 target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
2831 ath6kl_err("unable to allocate memory\n");
2833 goto err_htc_cleanup;
2836 spin_lock_init(&target->htc_lock);
2837 spin_lock_init(&target->rx_lock);
2838 spin_lock_init(&target->tx_lock);
2840 INIT_LIST_HEAD(&target->free_ctrl_txbuf);
2841 INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
2842 INIT_LIST_HEAD(&target->cred_dist_list);
2844 target->dev->ar = ar;
2845 target->dev->htc_cnxt = target;
2846 target->ep_waiting = ENDPOINT_MAX;
2848 status = ath6kl_hif_setup(target->dev);
2850 goto err_htc_cleanup;
2852 status = ath6kl_htc_reset(target);
2854 goto err_htc_cleanup;
2859 ath6kl_htc_cleanup(target);
2864 /* cleanup the HTC instance */
2865 void ath6kl_htc_cleanup(struct htc_target *target)
2867 struct htc_packet *packet, *tmp_packet;
2869 /* FIXME: remove check once USB support is implemented */
2870 if (target->dev->ar->hif_type != ATH6KL_HIF_TYPE_USB)
2871 ath6kl_hif_cleanup_scatter(target->dev->ar);
2873 list_for_each_entry_safe(packet, tmp_packet,
2874 &target->free_ctrl_txbuf, list) {
2875 list_del(&packet->list);
2876 kfree(packet->buf_start);
2880 list_for_each_entry_safe(packet, tmp_packet,
2881 &target->free_ctrl_rxbuf, list) {
2882 list_del(&packet->list);
2883 kfree(packet->buf_start);