ath6kl: Add tx_complete() to struct htc_ep_callbacks
[cascardo/linux.git] / drivers / net / wireless / ath / ath6kl / htc.c
1 /*
2  * Copyright (c) 2007-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4  *
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.
8  *
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.
16  */
17
18 #include "core.h"
19 #include "hif.h"
20 #include "debug.h"
21 #include "hif-ops.h"
22 #include <asm/unaligned.h>
23
24 #define CALC_TXRX_PADDED_LEN(dev, len)  (__ALIGN_MASK((len), (dev)->block_mask))
25
26 /* threshold to re-enable Tx bundling for an AC*/
27 #define TX_RESUME_BUNDLE_THRESHOLD      1500
28
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,
32                                   int credits)
33 {
34         ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit deposit ep %d credits %d\n",
35                    ep_dist->endpoint, credits);
36
37         ep_dist->credits += credits;
38         ep_dist->cred_assngd += credits;
39         cred_info->cur_free_credits -= credits;
40 }
41
42 static void ath6kl_credit_init(struct ath6kl_htc_credit_info *cred_info,
43                                struct list_head *ep_list,
44                                int tot_credits)
45 {
46         struct htc_endpoint_credit_dist *cur_ep_dist;
47         int count;
48
49         ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit init total %d\n", tot_credits);
50
51         cred_info->cur_free_credits = tot_credits;
52         cred_info->total_avail_credits = tot_credits;
53
54         list_for_each_entry(cur_ep_dist, ep_list, list) {
55                 if (cur_ep_dist->endpoint == ENDPOINT_0)
56                         continue;
57
58                 cur_ep_dist->cred_min = cur_ep_dist->cred_per_msg;
59
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,
64                                                       cur_ep_dist,
65                                                       cur_ep_dist->cred_min);
66                                 cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
67                         }
68                 }
69
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);
73                         /*
74                          * Control service is always marked active, it
75                          * never goes inactive EVER.
76                          */
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;
82
83                 /*
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.
88                  *
89                  * Note: all other endpoints have minimums set but are
90                  * initially given NO credits. credits will be distributed
91                  * as traffic activity demands
92                  */
93         }
94
95         WARN_ON(cred_info->cur_free_credits <= 0);
96
97         list_for_each_entry(cur_ep_dist, ep_list, list) {
98                 if (cur_ep_dist->endpoint == ENDPOINT_0)
99                         continue;
100
101                 if (cur_ep_dist->svc_id == WMI_CONTROL_SVC)
102                         cur_ep_dist->cred_norm = cur_ep_dist->cred_per_msg;
103                 else {
104                         /*
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.
111                          */
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;
118
119                 }
120
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,
124                            cur_ep_dist->svc_id,
125                            cur_ep_dist->credits,
126                            cur_ep_dist->cred_per_msg,
127                            cur_ep_dist->cred_norm,
128                            cur_ep_dist->cred_min);
129         }
130 }
131
132 /* initialize and setup credit distribution */
133 int ath6kl_credit_setup(void *htc_handle,
134                         struct ath6kl_htc_credit_info *cred_info)
135 {
136         u16 servicepriority[5];
137
138         memset(cred_info, 0, sizeof(struct ath6kl_htc_credit_info));
139
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 */
145
146         /* set priority list */
147         ath6kl_htc_set_credit_dist(htc_handle, cred_info, servicepriority, 5);
148
149         return 0;
150 }
151
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,
155                                  int limit)
156 {
157         int credits;
158
159         ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit reduce ep %d limit %d\n",
160                    ep_dist->endpoint, limit);
161
162         ep_dist->cred_assngd = limit;
163
164         if (ep_dist->credits <= limit)
165                 return;
166
167         credits = ep_dist->credits - limit;
168         ep_dist->credits -= credits;
169         cred_info->cur_free_credits += credits;
170 }
171
172 static void ath6kl_credit_update(struct ath6kl_htc_credit_info *cred_info,
173                                  struct list_head *epdist_list)
174 {
175         struct htc_endpoint_credit_dist *cur_list;
176
177         list_for_each_entry(cur_list, epdist_list, list) {
178                 if (cur_list->endpoint == ENDPOINT_0)
179                         continue;
180
181                 if (cur_list->cred_to_dist > 0) {
182                         cur_list->credits += cur_list->cred_to_dist;
183                         cur_list->cred_to_dist = 0;
184
185                         if (cur_list->credits > cur_list->cred_assngd)
186                                 ath6kl_credit_reduce(cred_info,
187                                                      cur_list,
188                                                      cur_list->cred_assngd);
189
190                         if (cur_list->credits > cur_list->cred_norm)
191                                 ath6kl_credit_reduce(cred_info, cur_list,
192                                                      cur_list->cred_norm);
193
194                         if (!(cur_list->dist_flags & HTC_EP_ACTIVE)) {
195                                 if (cur_list->txq_depth == 0)
196                                         ath6kl_credit_reduce(cred_info,
197                                                              cur_list, 0);
198                         }
199                 }
200         }
201 }
202
203 /*
204  * HTC has an endpoint that needs credits, ep_dist is the endpoint in
205  * question.
206  */
207 static void ath6kl_credit_seek(struct ath6kl_htc_credit_info *cred_info,
208                                 struct htc_endpoint_credit_dist *ep_dist)
209 {
210         struct htc_endpoint_credit_dist *curdist_list;
211         int credits = 0;
212         int need;
213
214         if (ep_dist->svc_id == WMI_CONTROL_SVC)
215                 goto out;
216
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))
220                         goto out;
221
222         /*
223          * For all other services, we follow a simple algorithm of:
224          *
225          * 1. checking the free pool for credits
226          * 2. checking lower priority endpoints for credits to take
227          */
228
229         credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
230
231         if (credits >= ep_dist->seek_cred)
232                 goto out;
233
234         /*
235          * We don't have enough in the free pool, try taking away from
236          * lower priority services The rule for taking away credits:
237          *
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.
242          */
243
244         list_for_each_entry_reverse(curdist_list,
245                                     &cred_info->lowestpri_ep_dist,
246                                     list) {
247                 if (curdist_list == ep_dist)
248                         break;
249
250                 need = ep_dist->seek_cred - cred_info->cur_free_credits;
251
252                 if ((curdist_list->cred_assngd - need) >=
253                      curdist_list->cred_min) {
254                         /*
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.
259                          */
260                         ath6kl_credit_reduce(cred_info, curdist_list,
261                                              curdist_list->cred_assngd - need);
262
263                         if (cred_info->cur_free_credits >=
264                             ep_dist->seek_cred)
265                                 break;
266                 }
267
268                 if (curdist_list->endpoint == ENDPOINT_0)
269                         break;
270         }
271
272         credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
273
274 out:
275         /* did we find some credits? */
276         if (credits)
277                 ath6kl_credit_deposit(cred_info, ep_dist, credits);
278
279         ep_dist->seek_cred = 0;
280 }
281
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)
285 {
286         struct htc_endpoint_credit_dist *curdist_list;
287
288         list_for_each_entry(curdist_list, ep_dist_list, list) {
289                 if (curdist_list->endpoint == ENDPOINT_0)
290                         continue;
291
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;
295
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);
300                         else
301                                 ath6kl_credit_reduce(info,
302                                                      curdist_list,
303                                                      curdist_list->cred_min);
304                 }
305         }
306 }
307
308 /*
309  *
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.
315  */
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)
319 {
320         switch (reason) {
321         case HTC_CREDIT_DIST_SEND_COMPLETE:
322                 ath6kl_credit_update(cred_info, ep_dist_list);
323                 break;
324         case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
325                 ath6kl_credit_redistribute(cred_info, ep_dist_list);
326                 break;
327         default:
328                 break;
329         }
330
331         WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
332         WARN_ON(cred_info->cur_free_credits < 0);
333 }
334
335 static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
336 {
337         u8 *align_addr;
338
339         if (!IS_ALIGNED((unsigned long) *buf, 4)) {
340                 align_addr = PTR_ALIGN(*buf - 4, 4);
341                 memmove(align_addr, *buf, len);
342                 *buf = align_addr;
343         }
344 }
345
346 static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
347                                    int ctrl0, int ctrl1)
348 {
349         struct htc_frame_hdr *hdr;
350
351         packet->buf -= HTC_HDR_LENGTH;
352         hdr =  (struct htc_frame_hdr *)packet->buf;
353
354         /* Endianess? */
355         put_unaligned((u16)packet->act_len, &hdr->payld_len);
356         hdr->flags = flags;
357         hdr->eid = packet->endpoint;
358         hdr->ctrl[0] = ctrl0;
359         hdr->ctrl[1] = ctrl1;
360 }
361
362 static void htc_reclaim_txctrl_buf(struct htc_target *target,
363                                    struct htc_packet *pkt)
364 {
365         spin_lock_bh(&target->htc_lock);
366         list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
367         spin_unlock_bh(&target->htc_lock);
368 }
369
370 static struct htc_packet *htc_get_control_buf(struct htc_target *target,
371                                               bool tx)
372 {
373         struct htc_packet *packet = NULL;
374         struct list_head *buf_list;
375
376         buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;
377
378         spin_lock_bh(&target->htc_lock);
379
380         if (list_empty(buf_list)) {
381                 spin_unlock_bh(&target->htc_lock);
382                 return NULL;
383         }
384
385         packet = list_first_entry(buf_list, struct htc_packet, list);
386         list_del(&packet->list);
387         spin_unlock_bh(&target->htc_lock);
388
389         if (tx)
390                 packet->buf = packet->buf_start + HTC_HDR_LENGTH;
391
392         return packet;
393 }
394
395 static void htc_tx_comp_update(struct htc_target *target,
396                                struct htc_endpoint *endpoint,
397                                struct htc_packet *packet)
398 {
399         packet->completion = NULL;
400         packet->buf += HTC_HDR_LENGTH;
401
402         if (!packet->status)
403                 return;
404
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);
408
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);
414
415         ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
416                    target->credit_info, &target->cred_dist_list);
417
418         ath6kl_credit_distribute(target->credit_info,
419                                  &target->cred_dist_list,
420                                  HTC_CREDIT_DIST_SEND_COMPLETE);
421
422         spin_unlock_bh(&target->tx_lock);
423 }
424
425 static void htc_tx_complete(struct htc_endpoint *endpoint,
426                             struct list_head *txq)
427 {
428         if (list_empty(txq))
429                 return;
430
431         ath6kl_dbg(ATH6KL_DBG_HTC,
432                    "htc tx complete ep %d pkts %d\n",
433                    endpoint->eid, get_queue_depth(txq));
434
435         ath6kl_tx_complete(endpoint->target, txq);
436 }
437
438 static void htc_tx_comp_handler(struct htc_target *target,
439                                 struct htc_packet *packet)
440 {
441         struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
442         struct list_head container;
443
444         ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx complete seqno %d\n",
445                    packet->info.tx.seqno);
446
447         htc_tx_comp_update(target, endpoint, packet);
448         INIT_LIST_HEAD(&container);
449         list_add_tail(&packet->list, &container);
450         /* do completion */
451         htc_tx_complete(endpoint, &container);
452 }
453
454 static void htc_async_tx_scat_complete(struct htc_target *target,
455                                        struct hif_scatter_req *scat_req)
456 {
457         struct htc_endpoint *endpoint;
458         struct htc_packet *packet;
459         struct list_head tx_compq;
460         int i;
461
462         INIT_LIST_HEAD(&tx_compq);
463
464         ath6kl_dbg(ATH6KL_DBG_HTC,
465                    "htc tx scat complete len %d entries %d\n",
466                    scat_req->len, scat_req->scat_entries);
467
468         if (scat_req->status)
469                 ath6kl_err("send scatter req failed: %d\n", scat_req->status);
470
471         packet = scat_req->scat_list[0].packet;
472         endpoint = &target->endpoint[packet->endpoint];
473
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;
477                 if (!packet) {
478                         WARN_ON(1);
479                         return;
480                 }
481
482                 packet->status = scat_req->status;
483                 htc_tx_comp_update(target, endpoint, packet);
484                 list_add_tail(&packet->list, &tx_compq);
485         }
486
487         /* free scatter request */
488         hif_scatter_req_add(target->dev->ar, scat_req);
489
490         /* complete all packets */
491         htc_tx_complete(endpoint, &tx_compq);
492 }
493
494 static int ath6kl_htc_tx_issue(struct htc_target *target,
495                                struct htc_packet *packet)
496 {
497         int status;
498         bool sync = false;
499         u32 padded_len, send_len;
500
501         if (!packet->completion)
502                 sync = true;
503
504         send_len = packet->act_len + HTC_HDR_LENGTH;
505
506         padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
507
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");
513
514         if (sync) {
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);
519
520                 packet->status = status;
521                 packet->buf += HTC_HDR_LENGTH;
522         } else
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);
527
528         return status;
529 }
530
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,
534                              int *req_cred)
535 {
536
537         *req_cred = (len > target->tgt_cred_sz) ?
538                      DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
539
540         ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit check need %d got %d\n",
541                    *req_cred, ep->cred_dist.credits);
542
543         if (ep->cred_dist.credits < *req_cred) {
544                 if (eid == ENDPOINT_0)
545                         return -EINVAL;
546
547                 /* Seek more credits */
548                 ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
549
550                 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
551
552                 ep->cred_dist.seek_cred = 0;
553
554                 if (ep->cred_dist.credits < *req_cred) {
555                         ath6kl_dbg(ATH6KL_DBG_CREDIT,
556                                    "credit not found for ep %d\n",
557                                    eid);
558                         return -EINVAL;
559                 }
560         }
561
562         ep->cred_dist.credits -= *req_cred;
563         ep->ep_st.cred_cosumd += *req_cred;
564
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;
569
570                 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
571
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");
579                 }
580         }
581
582         return 0;
583 }
584
585 static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
586                                    struct htc_endpoint *endpoint,
587                                    struct list_head *queue)
588 {
589         int req_cred;
590         u8 flags;
591         struct htc_packet *packet;
592         unsigned int len;
593
594         while (true) {
595
596                 flags = 0;
597
598                 if (list_empty(&endpoint->txq))
599                         break;
600                 packet = list_first_entry(&endpoint->txq, struct htc_packet,
601                                           list);
602
603                 ath6kl_dbg(ATH6KL_DBG_HTC,
604                            "htc tx got packet 0x%p queue depth %d\n",
605                            packet, get_queue_depth(&endpoint->txq));
606
607                 len = CALC_TXRX_PADDED_LEN(target,
608                                            packet->act_len + HTC_HDR_LENGTH);
609
610                 if (htc_check_credits(target, endpoint, &flags,
611                                       packet->endpoint, len, &req_cred))
612                         break;
613
614                 /* now we can fully move onto caller's queue */
615                 packet = list_first_entry(&endpoint->txq, struct htc_packet,
616                                           list);
617                 list_move_tail(&packet->list, queue);
618
619                 /* save the number of credits this packet consumed */
620                 packet->info.tx.cred_used = req_cred;
621
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;
626
627                 /* save send flags */
628                 packet->info.tx.flags = flags;
629                 packet->info.tx.seqno = endpoint->seqno;
630                 endpoint->seqno++;
631         }
632 }
633
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)
637 {
638         int rem_cred, cred_pad;
639
640         rem_cred = *len % cred_sz;
641
642         /* No padding needed */
643         if  (!rem_cred)
644                 return 0;
645
646         if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
647                 return -1;
648
649         /*
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.
654          */
655         cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
656
657         if ((cred_pad > 0) && (cred_pad <= 255))
658                 *len += cred_pad;
659         else
660                 /* The amount of padding is too large, send as non-bundled */
661                 return -1;
662
663         return cred_pad;
664 }
665
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,
669                                          int n_scat,
670                                          struct list_head *queue)
671 {
672         struct htc_packet *packet;
673         int i, len, rem_scat, cred_pad;
674         int status = 0;
675         u8 flags;
676
677         rem_scat = target->max_tx_bndl_sz;
678
679         for (i = 0; i < n_scat; i++) {
680                 scat_req->scat_list[i].packet = NULL;
681
682                 if (list_empty(queue))
683                         break;
684
685                 packet = list_first_entry(queue, struct htc_packet, list);
686                 len = CALC_TXRX_PADDED_LEN(target,
687                                            packet->act_len + HTC_HDR_LENGTH);
688
689                 cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
690                                                   &len, endpoint);
691                 if (cred_pad < 0 || rem_scat < len) {
692                         status = -ENOSPC;
693                         break;
694                 }
695
696                 rem_scat -= len;
697                 /* now remove it from the queue */
698                 list_del(&packet->list);
699
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;
710
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);
716         }
717
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;
722                         if (packet) {
723                                 packet->buf += HTC_HDR_LENGTH;
724                                 list_add(&packet->list, queue);
725                         }
726                 }
727                 return -EAGAIN;
728         }
729
730         return status;
731 }
732
733 /*
734  * Drain a queue and send as bundles this function may return without fully
735  * draining the queue when
736  *
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
741  */
742 static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
743                                  struct list_head *queue,
744                                  int *sent_bundle, int *n_bundle_pkts)
745 {
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;
749         int status;
750         u32 txb_mask;
751         u8 ac = WMM_NUM_AC;
752
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];
756
757         while (true) {
758                 status = 0;
759                 n_scat = get_queue_depth(queue);
760                 n_scat = min(n_scat, target->msg_per_bndl_max);
761
762                 if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
763                         /* not enough to bundle */
764                         break;
765
766                 scat_req = hif_scatter_req_get(target->dev->ar);
767
768                 if (!scat_req) {
769                         /* no scatter resources  */
770                         ath6kl_dbg(ATH6KL_DBG_HTC,
771                                    "htc tx no more scatter resources\n");
772                         break;
773                 }
774
775                 if ((ac < WMM_NUM_AC) && (ac != WMM_AC_BK)) {
776                         if (WMM_AC_BE == ac)
777                                 /*
778                                  * BE, BK have priorities and bit
779                                  * positions reversed
780                                  */
781                                 txb_mask = (1 << WMM_AC_BK);
782                         else
783                                 /*
784                                  * any AC with priority lower than
785                                  * itself
786                                  */
787                                 txb_mask = ((1 << ac) - 1);
788                 /*
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
793                  */
794                 if (scat_req->scat_q_depth < ATH6KL_SCATTER_REQS)
795                         target->tx_bndl_mask &= ~txb_mask;
796                 else
797                         target->tx_bndl_mask |= txb_mask;
798                 }
799
800                 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
801                            n_scat);
802
803                 scat_req->len = 0;
804                 scat_req->scat_entries = 0;
805
806                 status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
807                                                        scat_req, n_scat,
808                                                        queue);
809                 if (status == -EAGAIN) {
810                         hif_scatter_req_add(target->dev->ar, scat_req);
811                         break;
812                 }
813
814                 /* send path is always asynchronous */
815                 scat_req->complete = htc_async_tx_scat_complete;
816                 n_sent_bundle++;
817                 tot_pkts_bundle += scat_req->scat_entries;
818
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);
823
824                 if (status)
825                         break;
826         }
827
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",
831                    n_sent_bundle);
832
833         return;
834 }
835
836 static void ath6kl_htc_tx_from_queue(struct htc_target *target,
837                                      struct htc_endpoint *endpoint)
838 {
839         struct list_head txq;
840         struct htc_packet *packet;
841         int bundle_sent;
842         int n_pkts_bundle;
843         u8 ac = WMM_NUM_AC;
844
845         spin_lock_bh(&target->tx_lock);
846
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");
852                 return;
853         }
854
855         /*
856          * drain the endpoint TX queue for transmission as long
857          * as we have enough credits.
858          */
859         INIT_LIST_HEAD(&txq);
860
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];
864
865         while (true) {
866
867                 if (list_empty(&endpoint->txq))
868                         break;
869
870                 ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
871
872                 if (list_empty(&txq))
873                         break;
874
875                 spin_unlock_bh(&target->tx_lock);
876
877                 bundle_sent = 0;
878                 n_pkts_bundle = 0;
879
880                 while (true) {
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;
886
887                                 /* check if bundling is enabled for an AC */
888                                 if (target->tx_bndl_mask & (1 << ac)) {
889                                         ath6kl_htc_tx_bundle(endpoint, &txq,
890                                                              &temp1, &temp2);
891                                         bundle_sent += temp1;
892                                         n_pkts_bundle += temp2;
893                                 }
894                         }
895
896                         if (list_empty(&txq))
897                                 break;
898
899                         packet = list_first_entry(&txq, struct htc_packet,
900                                                   list);
901                         list_del(&packet->list);
902
903                         ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
904                                                0, packet->info.tx.seqno);
905                         ath6kl_htc_tx_issue(target, packet);
906                 }
907
908                 spin_lock_bh(&target->tx_lock);
909
910                 endpoint->ep_st.tx_bundles += bundle_sent;
911                 endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
912
913                 /*
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
917                  */
918                 if (!bundle_sent) {
919                         if (!(target->tx_bndl_mask & (1 << ac)) &&
920                             (ac < WMM_NUM_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);
925                                 }
926                         }
927                 } else {
928                         /* tx bundling will reset the counter */
929                         if (ac < WMM_NUM_AC)
930                                 target->ac_tx_count[ac] = 0;
931                 }
932         }
933
934         endpoint->tx_proc_cnt = 0;
935         spin_unlock_bh(&target->tx_lock);
936 }
937
938 static bool ath6kl_htc_tx_try(struct htc_target *target,
939                               struct htc_endpoint *endpoint,
940                               struct htc_packet *tx_pkt)
941 {
942         struct htc_ep_callbacks ep_cb;
943         int txq_depth;
944         bool overflow = false;
945
946         ep_cb = endpoint->ep_cb;
947
948         spin_lock_bh(&target->tx_lock);
949         txq_depth = get_queue_depth(&endpoint->txq);
950         spin_unlock_bh(&target->tx_lock);
951
952         if (txq_depth >= endpoint->max_txq_depth)
953                 overflow = true;
954
955         if (overflow)
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);
960
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;
965                         return false;
966                 }
967         }
968
969         spin_lock_bh(&target->tx_lock);
970         list_add_tail(&tx_pkt->list, &endpoint->txq);
971         spin_unlock_bh(&target->tx_lock);
972
973         ath6kl_htc_tx_from_queue(target, endpoint);
974
975         return true;
976 }
977
978 static void htc_chk_ep_txq(struct htc_target *target)
979 {
980         struct htc_endpoint *endpoint;
981         struct htc_endpoint_credit_dist *cred_dist;
982
983         /*
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.
988          */
989         list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
990                 endpoint = cred_dist->htc_ep;
991
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",
996                                    cred_dist->endpoint,
997                                    endpoint->cred_dist.credits,
998                                    get_queue_depth(&endpoint->txq));
999                         spin_unlock_bh(&target->tx_lock);
1000                         /*
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
1005                          * ones.
1006                          */
1007                         ath6kl_htc_tx_from_queue(target, endpoint);
1008                         spin_lock_bh(&target->tx_lock);
1009                 }
1010                 spin_unlock_bh(&target->tx_lock);
1011         }
1012 }
1013
1014 static int htc_setup_tx_complete(struct htc_target *target)
1015 {
1016         struct htc_packet *send_pkt = NULL;
1017         int status;
1018
1019         send_pkt = htc_get_control_buf(target, true);
1020
1021         if (!send_pkt)
1022                 return -ENOMEM;
1023
1024         if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
1025                 struct htc_setup_comp_ext_msg *setup_comp_ext;
1026                 u32 flags = 0;
1027
1028                 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);
1033
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;
1039                 }
1040
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);
1046
1047         } else {
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);
1055         }
1056
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);
1061
1062         if (send_pkt != NULL)
1063                 htc_reclaim_txctrl_buf(target, send_pkt);
1064
1065         return status;
1066 }
1067
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)
1071 {
1072         struct htc_endpoint *endpoint;
1073         int i, ep;
1074
1075         target->credit_info = credit_info;
1076
1077         list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
1078                       &target->cred_dist_list);
1079
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);
1086                                 break;
1087                         }
1088                 }
1089                 if (ep >= ENDPOINT_MAX) {
1090                         WARN_ON(1);
1091                         return;
1092                 }
1093         }
1094 }
1095
1096 int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet)
1097 {
1098         struct htc_endpoint *endpoint;
1099         struct list_head queue;
1100
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);
1104
1105         if (packet->endpoint >= ENDPOINT_MAX) {
1106                 WARN_ON(1);
1107                 return -EINVAL;
1108         }
1109
1110         endpoint = &target->endpoint[packet->endpoint];
1111
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);
1118         }
1119
1120         return 0;
1121 }
1122
1123 /* flush endpoint TX queue */
1124 void ath6kl_htc_flush_txep(struct htc_target *target,
1125                            enum htc_endpoint_id eid, u16 tag)
1126 {
1127         struct htc_packet *packet, *tmp_pkt;
1128         struct list_head discard_q, container;
1129         struct htc_endpoint *endpoint = &target->endpoint[eid];
1130
1131         if (!endpoint->svc_id) {
1132                 WARN_ON(1);
1133                 return;
1134         }
1135
1136         /* initialize the discard queue */
1137         INIT_LIST_HEAD(&discard_q);
1138
1139         spin_lock_bh(&target->tx_lock);
1140
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);
1145         }
1146
1147         spin_unlock_bh(&target->tx_lock);
1148
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);
1156
1157                 INIT_LIST_HEAD(&container);
1158                 list_add_tail(&packet->list, &container);
1159                 htc_tx_complete(endpoint, &container);
1160         }
1161
1162 }
1163
1164 static void ath6kl_htc_flush_txep_all(struct htc_target *target)
1165 {
1166         struct htc_endpoint *endpoint;
1167         int i;
1168
1169         dump_cred_dist_stats(target);
1170
1171         for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
1172                 endpoint = &target->endpoint[i];
1173                 if (endpoint->svc_id == 0)
1174                         /* not in use.. */
1175                         continue;
1176                 ath6kl_htc_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
1177         }
1178 }
1179
1180 void ath6kl_htc_indicate_activity_change(struct htc_target *target,
1181                                          enum htc_endpoint_id eid, bool active)
1182 {
1183         struct htc_endpoint *endpoint = &target->endpoint[eid];
1184         bool dist = false;
1185
1186         if (endpoint->svc_id == 0) {
1187                 WARN_ON(1);
1188                 return;
1189         }
1190
1191         spin_lock_bh(&target->tx_lock);
1192
1193         if (active) {
1194                 if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
1195                         endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
1196                         dist = true;
1197                 }
1198         } else {
1199                 if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
1200                         endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
1201                         dist = true;
1202                 }
1203         }
1204
1205         if (dist) {
1206                 endpoint->cred_dist.txq_depth =
1207                         get_queue_depth(&endpoint->txq);
1208
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);
1212
1213                 ath6kl_credit_distribute(target->credit_info,
1214                                          &target->cred_dist_list,
1215                                          HTC_CREDIT_DIST_ACTIVITY_CHANGE);
1216         }
1217
1218         spin_unlock_bh(&target->tx_lock);
1219
1220         if (dist && !active)
1221                 htc_chk_ep_txq(target);
1222 }
1223
1224 /* HTC Rx */
1225
1226 static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
1227                                               int n_look_ahds)
1228 {
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++;
1234 }
1235
1236 static inline bool htc_valid_rx_frame_len(struct htc_target *target,
1237                                           enum htc_endpoint_id eid, int len)
1238 {
1239         return (eid == target->dev->ar->ctrl_ep) ?
1240                 len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
1241 }
1242
1243 static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
1244 {
1245         struct list_head queue;
1246
1247         INIT_LIST_HEAD(&queue);
1248         list_add_tail(&packet->list, &queue);
1249         return ath6kl_htc_add_rxbuf_multiple(target, &queue);
1250 }
1251
1252 static void htc_reclaim_rxbuf(struct htc_target *target,
1253                               struct htc_packet *packet,
1254                               struct htc_endpoint *ep)
1255 {
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);
1260         } else {
1261                 htc_rxpkt_reset(packet);
1262                 htc_add_rxbuf((void *)(target), packet);
1263         }
1264 }
1265
1266 static void reclaim_rx_ctrl_buf(struct htc_target *target,
1267                                 struct htc_packet *packet)
1268 {
1269         spin_lock_bh(&target->htc_lock);
1270         list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
1271         spin_unlock_bh(&target->htc_lock);
1272 }
1273
1274 static int ath6kl_htc_rx_packet(struct htc_target *target,
1275                                 struct htc_packet *packet,
1276                                 u32 rx_len)
1277 {
1278         struct ath6kl_device *dev = target->dev;
1279         u32 padded_len;
1280         int status;
1281
1282         padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
1283
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);
1287                 return -ENOMEM;
1288         }
1289
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);
1294
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);
1299
1300         packet->status = status;
1301
1302         return status;
1303 }
1304
1305 /*
1306  * optimization for recv packets, we can indicate a
1307  * "hint" that there are more  single-packets to fetch
1308  * on this endpoint.
1309  */
1310 static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
1311                                        struct htc_endpoint *endpoint,
1312                                        struct htc_packet *packet)
1313 {
1314         struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
1315
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;
1320         }
1321 }
1322
1323 static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
1324 {
1325         struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
1326
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);
1333                         return;
1334                 }
1335                 spin_unlock_bh(&endpoint->target->rx_lock);
1336         }
1337 }
1338
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)
1343 {
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;
1349         bool no_recycle;
1350
1351         full_len = CALC_TXRX_PADDED_LEN(target,
1352                                         le16_to_cpu(htc_hdr->payld_len) +
1353                                         sizeof(*htc_hdr));
1354
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));
1359                 return -EINVAL;
1360         }
1361
1362         ep_cb = ep->ep_cb;
1363         for (j = 0; j < n_msg; j++) {
1364
1365                 /*
1366                  * Reset flag, any packets allocated using the
1367                  * rx_alloc() API cannot be recycled on
1368                  * cleanup,they must be explicitly returned.
1369                  */
1370                 no_recycle = false;
1371
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);
1377
1378                         spin_unlock_bh(&target->rx_lock);
1379                         no_recycle = true;
1380
1381                         packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
1382                                                       full_len);
1383                         spin_lock_bh(&target->rx_lock);
1384                 } else {
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);
1391                                 }
1392                         }
1393
1394                         if (list_empty(&ep->rx_bufq))
1395                                 packet = NULL;
1396                         else {
1397                                 packet = list_first_entry(&ep->rx_bufq,
1398                                                 struct htc_packet, list);
1399                                 list_del(&packet->list);
1400                         }
1401                 }
1402
1403                 if (!packet) {
1404                         target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
1405                         target->ep_waiting = ep->eid;
1406                         return -ENOSPC;
1407                 }
1408
1409                 /* clear flags */
1410                 packet->info.rx.rx_flags = 0;
1411                 packet->info.rx.indicat_flags = 0;
1412                 packet->status = 0;
1413
1414                 if (no_recycle)
1415                         /*
1416                          * flag that these packets cannot be
1417                          * recycled, they have to be returned to
1418                          * the user
1419                          */
1420                         packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
1421
1422                 /* Caller needs to free this upon any failure */
1423                 list_add_tail(&packet->list, queue);
1424
1425                 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1426                         status = -ECANCELED;
1427                         break;
1428                 }
1429
1430                 if (j) {
1431                         packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
1432                         packet->info.rx.exp_hdr = 0xFFFFFFFF;
1433                 } else
1434                         /* set expected look ahead */
1435                         packet->info.rx.exp_hdr = *lk_ahds;
1436
1437                 packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
1438                         HTC_HDR_LENGTH;
1439         }
1440
1441         return status;
1442 }
1443
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)
1448 {
1449         int status = 0;
1450         struct htc_packet *packet, *tmp_pkt;
1451         struct htc_frame_hdr *htc_hdr;
1452         int i, n_msg;
1453
1454         spin_lock_bh(&target->rx_lock);
1455
1456         for (i = 0; i < msg; i++) {
1457
1458                 htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
1459
1460                 if (htc_hdr->eid >= ENDPOINT_MAX) {
1461                         ath6kl_err("invalid ep in look-ahead: %d\n",
1462                                    htc_hdr->eid);
1463                         status = -ENOMEM;
1464                         break;
1465                 }
1466
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);
1470                         status = -ENOMEM;
1471                         break;
1472                 }
1473
1474                 if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
1475                         ath6kl_err("payload len %d exceeds max htc : %d !\n",
1476                                    htc_hdr->payld_len,
1477                                    (u32) HTC_MAX_PAYLOAD_LENGTH);
1478                         status = -ENOMEM;
1479                         break;
1480                 }
1481
1482                 if (endpoint->svc_id == 0) {
1483                         ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
1484                         status = -ENOMEM;
1485                         break;
1486                 }
1487
1488                 if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
1489                         /*
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.
1493                          */
1494                         n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
1495                                 HTC_FLG_RX_BNDL_CNT_S;
1496
1497                         /* the count doesn't include the starter frame */
1498                         n_msg++;
1499                         if (n_msg > target->msg_per_bndl_max) {
1500                                 status = -ENOMEM;
1501                                 break;
1502                         }
1503
1504                         endpoint->ep_st.rx_bundle_from_hdr += 1;
1505                         ath6kl_dbg(ATH6KL_DBG_HTC,
1506                                    "htc rx bundle pkts %d\n",
1507                                    n_msg);
1508                 } else
1509                         /* HTC header only indicates 1 message to fetch */
1510                         n_msg = 1;
1511
1512                 /* Setup packet buffers for each message */
1513                 status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
1514                                              queue, n_msg);
1515
1516                 /*
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.
1520                  */
1521                 if (status == -ENOSPC) {
1522                         spin_unlock_bh(&target->rx_lock);
1523                         return 0;
1524                 }
1525
1526                 if (status)
1527                         break;
1528         }
1529
1530         spin_unlock_bh(&target->rx_lock);
1531
1532         if (status) {
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]);
1537                 }
1538         }
1539
1540         return status;
1541 }
1542
1543 static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
1544 {
1545         if (packets->endpoint != ENDPOINT_0) {
1546                 WARN_ON(1);
1547                 return;
1548         }
1549
1550         if (packets->status == -ECANCELED) {
1551                 reclaim_rx_ctrl_buf(context, packets);
1552                 return;
1553         }
1554
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);
1558
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);
1563         }
1564
1565         htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
1566 }
1567
1568 static void htc_proc_cred_rpt(struct htc_target *target,
1569                               struct htc_credit_report *rpt,
1570                               int n_entries,
1571                               enum htc_endpoint_id from_ep)
1572 {
1573         struct htc_endpoint *endpoint;
1574         int tot_credits = 0, i;
1575         bool dist = false;
1576
1577         spin_lock_bh(&target->tx_lock);
1578
1579         for (i = 0; i < n_entries; i++, rpt++) {
1580                 if (rpt->eid >= ENDPOINT_MAX) {
1581                         WARN_ON(1);
1582                         spin_unlock_bh(&target->tx_lock);
1583                         return;
1584                 }
1585
1586                 endpoint = &target->endpoint[rpt->eid];
1587
1588                 ath6kl_dbg(ATH6KL_DBG_CREDIT,
1589                            "credit report ep %d credits %d\n",
1590                            rpt->eid, rpt->credits);
1591
1592                 endpoint->ep_st.tx_cred_rpt += 1;
1593                 endpoint->ep_st.cred_retnd += rpt->credits;
1594
1595                 if (from_ep == rpt->eid) {
1596                         /*
1597                          * This credit report arrived on the same endpoint
1598                          * indicating it arrived in an RX packet.
1599                          */
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;
1606                 } else {
1607                         endpoint->ep_st.cred_from_other += rpt->credits;
1608                         endpoint->ep_st.cred_rpt_from_other += 1;
1609                 }
1610
1611                 if (rpt->eid == ENDPOINT_0)
1612                         /* always give endpoint 0 credits back */
1613                         endpoint->cred_dist.credits += rpt->credits;
1614                 else {
1615                         endpoint->cred_dist.cred_to_dist += rpt->credits;
1616                         dist = true;
1617                 }
1618
1619                 /*
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!
1623                  */
1624                 endpoint->cred_dist.txq_depth =
1625                         get_queue_depth(&endpoint->txq);
1626
1627                 tot_credits += rpt->credits;
1628         }
1629
1630         if (dist) {
1631                 /*
1632                  * This was a credit return based on a completed send
1633                  * operations note, this is done with the lock held
1634                  */
1635                 ath6kl_credit_distribute(target->credit_info,
1636                                          &target->cred_dist_list,
1637                                          HTC_CREDIT_DIST_SEND_COMPLETE);
1638         }
1639
1640         spin_unlock_bh(&target->tx_lock);
1641
1642         if (tot_credits)
1643                 htc_chk_ep_txq(target);
1644 }
1645
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,
1650                              int *n_lk_ahds)
1651 {
1652         struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
1653         struct htc_lookahead_report *lk_ahd;
1654         int len;
1655
1656         switch (record->rec_id) {
1657         case HTC_RECORD_CREDITS:
1658                 len = record->len / sizeof(struct htc_credit_report);
1659                 if (!len) {
1660                         WARN_ON(1);
1661                         return -EINVAL;
1662                 }
1663
1664                 htc_proc_cred_rpt(target,
1665                                   (struct htc_credit_report *) record_buf,
1666                                   len, endpoint);
1667                 break;
1668         case HTC_RECORD_LOOKAHEAD:
1669                 len = record->len / sizeof(*lk_ahd);
1670                 if (!len) {
1671                         WARN_ON(1);
1672                         return -EINVAL;
1673                 }
1674
1675                 lk_ahd = (struct htc_lookahead_report *) record_buf;
1676                 if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF)) &&
1677                     next_lk_ahds) {
1678
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);
1682
1683                         /* look ahead bytes are valid, copy them over */
1684                         memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);
1685
1686                         ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1687                                         "htc rx next look ahead",
1688                                         "", next_lk_ahds, 4);
1689
1690                         *n_lk_ahds = 1;
1691                 }
1692                 break;
1693         case HTC_RECORD_LOOKAHEAD_BUNDLE:
1694                 len = record->len / sizeof(*bundle_lkahd_rpt);
1695                 if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
1696                         WARN_ON(1);
1697                         return -EINVAL;
1698                 }
1699
1700                 if (next_lk_ahds) {
1701                         int i;
1702
1703                         bundle_lkahd_rpt =
1704                                 (struct htc_bundle_lkahd_rpt *) record_buf;
1705
1706                         ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
1707                                         "", record_buf, record->len);
1708
1709                         for (i = 0; i < len; i++) {
1710                                 memcpy((u8 *)&next_lk_ahds[i],
1711                                        bundle_lkahd_rpt->lk_ahd, 4);
1712                                 bundle_lkahd_rpt++;
1713                         }
1714
1715                         *n_lk_ahds = i;
1716                 }
1717                 break;
1718         default:
1719                 ath6kl_err("unhandled record: id:%d len:%d\n",
1720                            record->rec_id, record->len);
1721                 break;
1722         }
1723
1724         return 0;
1725
1726 }
1727
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)
1731 {
1732         struct htc_record_hdr *record;
1733         int orig_len;
1734         int status;
1735         u8 *record_buf;
1736         u8 *orig_buf;
1737
1738         ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
1739         ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
1740
1741         orig_buf = buf;
1742         orig_len = len;
1743         status = 0;
1744
1745         while (len > 0) {
1746
1747                 if (len < sizeof(struct htc_record_hdr)) {
1748                         status = -ENOMEM;
1749                         break;
1750                 }
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);
1755
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);
1759                         status = -ENOMEM;
1760                         break;
1761                 }
1762                 record_buf = buf;
1763
1764                 status = htc_parse_trailer(target, record, record_buf,
1765                                            next_lk_ahds, endpoint, n_lk_ahds);
1766
1767                 if (status)
1768                         break;
1769
1770                 /* advance buffer past this record for next time around */
1771                 buf += record->len;
1772                 len -= record->len;
1773         }
1774
1775         if (status)
1776                 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
1777                                 "", orig_buf, orig_len);
1778
1779         return status;
1780 }
1781
1782 static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
1783                                      struct htc_packet *packet,
1784                                      u32 *next_lkahds, int *n_lkahds)
1785 {
1786         int status = 0;
1787         u16 payload_len;
1788         u32 lk_ahd;
1789         struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
1790
1791         if (n_lkahds != NULL)
1792                 *n_lkahds = 0;
1793
1794         /*
1795          * NOTE: we cannot assume the alignment of buf, so we use the safe
1796          * macros to retrieve 16 bit fields.
1797          */
1798         payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
1799
1800         memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
1801
1802         if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
1803                 /*
1804                  * Refresh the expected header and the actual length as it
1805                  * was unknown when this packet was grabbed as part of the
1806                  * bundle.
1807                  */
1808                 packet->info.rx.exp_hdr = lk_ahd;
1809                 packet->act_len = payload_len + HTC_HDR_LENGTH;
1810
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);
1815                         /*
1816                          * Limit this to max buffer just to print out some
1817                          * of the buffer.
1818                          */
1819                         packet->act_len = min(packet->act_len, packet->buf_len);
1820                         status = -ENOMEM;
1821                         goto fail_rx;
1822                 }
1823
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);
1827                         status = -ENOMEM;
1828                         goto fail_rx;
1829                 }
1830         }
1831
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));
1839                 status = -ENOMEM;
1840                 goto fail_rx;
1841         }
1842
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]);
1848                         status = -ENOMEM;
1849                         goto fail_rx;
1850                 }
1851
1852                 if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
1853                         next_lkahds = NULL;
1854                         n_lkahds = NULL;
1855                 }
1856
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);
1861
1862                 if (status)
1863                         goto fail_rx;
1864
1865                 packet->act_len -= htc_hdr->ctrl[0];
1866         }
1867
1868         packet->buf += HTC_HDR_LENGTH;
1869         packet->act_len -= HTC_HDR_LENGTH;
1870
1871 fail_rx:
1872         if (status)
1873                 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1874                                 "", packet->buf, packet->act_len);
1875
1876         return status;
1877 }
1878
1879 static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1880                                    struct htc_packet *packet)
1881 {
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);
1886 }
1887
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)
1892 {
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;
1897
1898         n_scat_pkt = get_queue_depth(rxq);
1899         n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
1900
1901         if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
1902                 /*
1903                  * We were forced to split this bundle receive operation
1904                  * all packets in this partial bundle must have their
1905                  * lookaheads ignored.
1906                  */
1907                 part_bundle = true;
1908
1909                 /*
1910                  * This would only happen if the target ignored our max
1911                  * bundle limit.
1912                  */
1913                 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1914                             __func__, get_queue_depth(rxq), n_scat_pkt);
1915         }
1916
1917         len = 0;
1918
1919         ath6kl_dbg(ATH6KL_DBG_HTC,
1920                    "htc rx bundle depth %d pkts %d\n",
1921                    get_queue_depth(rxq), n_scat_pkt);
1922
1923         scat_req = hif_scatter_req_get(target->dev->ar);
1924
1925         if (scat_req == NULL)
1926                 goto fail_rx_pkt;
1927
1928         for (i = 0; i < n_scat_pkt; i++) {
1929                 int pad_len;
1930
1931                 packet = list_first_entry(rxq, struct htc_packet, list);
1932                 list_del(&packet->list);
1933
1934                 pad_len = CALC_TXRX_PADDED_LEN(target,
1935                                                    packet->act_len);
1936
1937                 if ((rem_space - pad_len) < 0) {
1938                         list_add(&packet->list, rxq);
1939                         break;
1940                 }
1941
1942                 rem_space -= pad_len;
1943
1944                 if (part_bundle || (i < (n_scat_pkt - 1)))
1945                         /*
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
1949                          */
1950                         packet->info.rx.rx_flags |=
1951                             HTC_RX_PKT_IGNORE_LOOKAHEAD;
1952
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;
1956
1957                 packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
1958
1959                 list_add_tail(&packet->list, sync_compq);
1960
1961                 WARN_ON(!scat_req->scat_list[i].len);
1962                 len += scat_req->scat_list[i].len;
1963         }
1964
1965         scat_req->len = len;
1966         scat_req->scat_entries = i;
1967
1968         status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
1969
1970         if (!status)
1971                 *n_pkt_fetched = i;
1972
1973         /* free scatter request */
1974         hif_scatter_req_add(target->dev->ar, scat_req);
1975
1976 fail_rx_pkt:
1977
1978         return status;
1979 }
1980
1981 static int ath6kl_htc_rx_process_packets(struct htc_target *target,
1982                                          struct list_head *comp_pktq,
1983                                          u32 lk_ahds[],
1984                                          int *n_lk_ahd)
1985 {
1986         struct htc_packet *packet, *tmp_pkt;
1987         struct htc_endpoint *ep;
1988         int status = 0;
1989
1990         list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
1991                 ep = &target->endpoint[packet->endpoint];
1992
1993                 /* process header for each of the recv packet */
1994                 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
1995                                                    n_lk_ahd);
1996                 if (status)
1997                         return status;
1998
1999                 list_del(&packet->list);
2000
2001                 if (list_empty(comp_pktq)) {
2002                         /*
2003                          * Last packet's more packet flag is set
2004                          * based on the lookahead.
2005                          */
2006                         if (*n_lk_ahd > 0)
2007                                 ath6kl_htc_rx_set_indicate(lk_ahds[0],
2008                                                            ep, packet);
2009                 } else
2010                         /*
2011                          * Packets in a bundle automatically have
2012                          * this flag set.
2013                          */
2014                         packet->info.rx.indicat_flags |=
2015                                 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
2016
2017                 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
2018
2019                 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
2020                         ep->ep_st.rx_bundl += 1;
2021
2022                 ath6kl_htc_rx_complete(ep, packet);
2023         }
2024
2025         return status;
2026 }
2027
2028 static int ath6kl_htc_rx_fetch(struct htc_target *target,
2029                                struct list_head *rx_pktq,
2030                                struct list_head *comp_pktq)
2031 {
2032         int fetched_pkts;
2033         bool part_bundle = false;
2034         int status = 0;
2035         struct list_head tmp_rxq;
2036         struct htc_packet *packet, *tmp_pkt;
2037
2038         /* now go fetch the list of HTC packets */
2039         while (!list_empty(rx_pktq)) {
2040                 fetched_pkts = 0;
2041
2042                 INIT_LIST_HEAD(&tmp_rxq);
2043
2044                 if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
2045                         /*
2046                          * There are enough packets to attempt a
2047                          * bundle transfer and recv bundling is
2048                          * allowed.
2049                          */
2050                         status = ath6kl_htc_rx_bundle(target, rx_pktq,
2051                                                       &tmp_rxq,
2052                                                       &fetched_pkts,
2053                                                       part_bundle);
2054                         if (status)
2055                                 goto fail_rx;
2056
2057                         if (!list_empty(rx_pktq))
2058                                 part_bundle = true;
2059
2060                         list_splice_tail_init(&tmp_rxq, comp_pktq);
2061                 }
2062
2063                 if (!fetched_pkts) {
2064
2065                         packet = list_first_entry(rx_pktq, struct htc_packet,
2066                                                    list);
2067
2068                         /* fully synchronous */
2069                         packet->completion = NULL;
2070
2071                         if (!list_is_singular(rx_pktq))
2072                                 /*
2073                                  * look_aheads in all packet
2074                                  * except the last one in the
2075                                  * bundle must be ignored
2076                                  */
2077                                 packet->info.rx.rx_flags |=
2078                                         HTC_RX_PKT_IGNORE_LOOKAHEAD;
2079
2080                         /* go fetch the packet */
2081                         status = ath6kl_htc_rx_packet(target, packet,
2082                                                       packet->act_len);
2083
2084                         list_move_tail(&packet->list, &tmp_rxq);
2085
2086                         if (status)
2087                                 goto fail_rx;
2088
2089                         list_splice_tail_init(&tmp_rxq, comp_pktq);
2090                 }
2091         }
2092
2093         return 0;
2094
2095 fail_rx:
2096
2097         /*
2098          * Cleanup any packets we allocated but didn't use to
2099          * actually fetch any packets.
2100          */
2101
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]);
2106         }
2107
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]);
2112         }
2113
2114         return status;
2115 }
2116
2117 int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
2118                                      u32 msg_look_ahead, int *num_pkts)
2119 {
2120         struct htc_packet *packets, *tmp_pkt;
2121         struct htc_endpoint *endpoint;
2122         struct list_head rx_pktq, comp_pktq;
2123         int status = 0;
2124         u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
2125         int num_look_ahead = 1;
2126         enum htc_endpoint_id id;
2127         int n_fetched = 0;
2128
2129         INIT_LIST_HEAD(&comp_pktq);
2130         *num_pkts = 0;
2131
2132         /*
2133          * On first entry copy the look_aheads into our temp array for
2134          * processing
2135          */
2136         look_aheads[0] = msg_look_ahead;
2137
2138         while (true) {
2139
2140                 /*
2141                  * First lookahead sets the expected endpoint IDs for all
2142                  * packets in a bundle.
2143                  */
2144                 id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
2145                 endpoint = &target->endpoint[id];
2146
2147                 if (id >= ENDPOINT_MAX) {
2148                         ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
2149                                    id);
2150                         status = -ENOMEM;
2151                         break;
2152                 }
2153
2154                 INIT_LIST_HEAD(&rx_pktq);
2155                 INIT_LIST_HEAD(&comp_pktq);
2156
2157                 /*
2158                  * Try to allocate as many HTC RX packets indicated by the
2159                  * look_aheads.
2160                  */
2161                 status = ath6kl_htc_rx_alloc(target, look_aheads,
2162                                              num_look_ahead, endpoint,
2163                                              &rx_pktq);
2164                 if (status)
2165                         break;
2166
2167                 if (get_queue_depth(&rx_pktq) >= 2)
2168                         /*
2169                          * A recv bundle was detected, force IRQ status
2170                          * re-check again
2171                          */
2172                         target->chk_irq_status_cnt = 1;
2173
2174                 n_fetched += get_queue_depth(&rx_pktq);
2175
2176                 num_look_ahead = 0;
2177
2178                 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
2179
2180                 if (!status)
2181                         ath6kl_htc_rx_chk_water_mark(endpoint);
2182
2183                 /* Process fetched packets */
2184                 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
2185                                                        look_aheads,
2186                                                        &num_look_ahead);
2187
2188                 if (!num_look_ahead || status)
2189                         break;
2190
2191                 /*
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.
2197                  */
2198                 target->chk_irq_status_cnt = 1;
2199         }
2200
2201         if (status) {
2202                 ath6kl_err("failed to get pending recv messages: %d\n",
2203                            status);
2204
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]);
2210                 }
2211
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);
2215                 }
2216         }
2217
2218         /*
2219          * Before leaving, check to see if host ran out of buffers and
2220          * needs to stop the receiver.
2221          */
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);
2225         }
2226         *num_pkts = n_fetched;
2227
2228         return status;
2229 }
2230
2231 /*
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.
2235  */
2236 static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
2237 {
2238         struct htc_packet *packet = NULL;
2239         struct htc_frame_hdr *htc_hdr;
2240         u32 look_ahead;
2241
2242         if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
2243                                        HTC_TARGET_RESPONSE_TIMEOUT))
2244                 return NULL;
2245
2246         ath6kl_dbg(ATH6KL_DBG_HTC,
2247                    "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
2248
2249         htc_hdr = (struct htc_frame_hdr *)&look_ahead;
2250
2251         if (htc_hdr->eid != ENDPOINT_0)
2252                 return NULL;
2253
2254         packet = htc_get_control_buf(target, false);
2255
2256         if (!packet)
2257                 return NULL;
2258
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;
2262
2263         if (packet->act_len > packet->buf_len)
2264                 goto fail_ctrl_rx;
2265
2266         /* we want synchronous operation */
2267         packet->completion = NULL;
2268
2269         /* get the message from the device, this will block */
2270         if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
2271                 goto fail_ctrl_rx;
2272
2273         /* process receive header */
2274         packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
2275
2276         if (packet->status) {
2277                 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
2278                            packet->status);
2279                 goto fail_ctrl_rx;
2280         }
2281
2282         return packet;
2283
2284 fail_ctrl_rx:
2285         if (packet != NULL) {
2286                 htc_rxpkt_reset(packet);
2287                 reclaim_rx_ctrl_buf(target, packet);
2288         }
2289
2290         return NULL;
2291 }
2292
2293 int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
2294                                   struct list_head *pkt_queue)
2295 {
2296         struct htc_endpoint *endpoint;
2297         struct htc_packet *first_pkt;
2298         bool rx_unblock = false;
2299         int status = 0, depth;
2300
2301         if (list_empty(pkt_queue))
2302                 return -ENOMEM;
2303
2304         first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
2305
2306         if (first_pkt->endpoint >= ENDPOINT_MAX)
2307                 return status;
2308
2309         depth = get_queue_depth(pkt_queue);
2310
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);
2314
2315         endpoint = &target->endpoint[first_pkt->endpoint];
2316
2317         if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2318                 struct htc_packet *packet, *tmp_pkt;
2319
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);
2325                 }
2326
2327                 return status;
2328         }
2329
2330         spin_lock_bh(&target->rx_lock);
2331
2332         list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
2333
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;
2342                         rx_unblock = true;
2343                 }
2344         }
2345
2346         spin_unlock_bh(&target->rx_lock);
2347
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);
2351
2352         return status;
2353 }
2354
2355 void ath6kl_htc_flush_rx_buf(struct htc_target *target)
2356 {
2357         struct htc_endpoint *endpoint;
2358         struct htc_packet *packet, *tmp_pkt;
2359         int i;
2360
2361         for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2362                 endpoint = &target->endpoint[i];
2363                 if (!endpoint->svc_id)
2364                         /* not in use.. */
2365                         continue;
2366
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,
2375                                    packet->endpoint);
2376                         /*
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
2383                          * the memory.
2384                          */
2385                         if (packet->endpoint == ENDPOINT_0) {
2386                                 kfree(packet->buf_start);
2387                                 kfree(packet);
2388                         } else {
2389                                 dev_kfree_skb(packet->pkt_cntxt);
2390                         }
2391                         spin_lock_bh(&target->rx_lock);
2392                 }
2393                 spin_unlock_bh(&target->rx_lock);
2394         }
2395 }
2396
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)
2400 {
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;
2408         int status = 0;
2409         u16 msg_id;
2410
2411         ath6kl_dbg(ATH6KL_DBG_HTC,
2412                    "htc connect service target 0x%p service id 0x%x\n",
2413                    target, conn_req->svc_id);
2414
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;
2419         } else {
2420                 /* allocate a packet to send to the target */
2421                 tx_pkt = htc_get_control_buf(target, true);
2422
2423                 if (!tx_pkt)
2424                         return -ENOMEM;
2425
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);
2431
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);
2435
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);
2440
2441                 if (status)
2442                         goto fail_tx;
2443
2444                 /* wait for response */
2445                 rx_pkt = htc_wait_for_ctrl_msg(target);
2446
2447                 if (!rx_pkt) {
2448                         status = -ENOMEM;
2449                         goto fail_tx;
2450                 }
2451
2452                 resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
2453                 msg_id = le16_to_cpu(resp_msg->msg_id);
2454
2455                 if ((msg_id != HTC_MSG_CONN_SVC_RESP_ID) ||
2456                     (rx_pkt->act_len < sizeof(*resp_msg))) {
2457                         status = -ENOMEM;
2458                         goto fail_tx;
2459                 }
2460
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);
2466                         status = -ENOMEM;
2467                         goto fail_tx;
2468                 }
2469
2470                 assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
2471                 max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
2472         }
2473
2474         if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
2475                 status = -ENOMEM;
2476                 goto fail_tx;
2477         }
2478
2479         endpoint = &target->endpoint[assigned_ep];
2480         endpoint->eid = assigned_ep;
2481         if (endpoint->svc_id) {
2482                 status = -ENOMEM;
2483                 goto fail_tx;
2484         }
2485
2486         /* return assigned endpoint to caller */
2487         conn_resp->endpoint = assigned_ep;
2488         conn_resp->len_max = max_msg_sz;
2489
2490         /* setup the endpoint */
2491
2492         /* this marks the endpoint in use */
2493         endpoint->svc_id = conn_req->svc_id;
2494
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;
2502
2503         switch (endpoint->svc_id) {
2504         case WMI_DATA_BK_SVC:
2505                 endpoint->tx_drop_packet_threshold = MAX_DEF_COOKIE_NUM / 3;
2506                 break;
2507         default:
2508                 endpoint->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
2509                 break;
2510         }
2511
2512         if (conn_req->max_rxmsg_sz) {
2513                 /*
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.
2517                  */
2518                 if (conn_req->max_rxmsg_sz > max_msg_sz) {
2519                         status = -ENOMEM;
2520                         goto fail_tx;
2521                 }
2522                 endpoint->cred_dist.cred_per_msg =
2523                     conn_req->max_rxmsg_sz / target->tgt_cred_sz;
2524         } else
2525                 endpoint->cred_dist.cred_per_msg =
2526                     max_msg_sz / target->tgt_cred_sz;
2527
2528         if (!endpoint->cred_dist.cred_per_msg)
2529                 endpoint->cred_dist.cred_per_msg = 1;
2530
2531         /* save local connection flags */
2532         endpoint->conn_flags = conn_req->flags;
2533
2534 fail_tx:
2535         if (tx_pkt)
2536                 htc_reclaim_txctrl_buf(target, tx_pkt);
2537
2538         if (rx_pkt) {
2539                 htc_rxpkt_reset(rx_pkt);
2540                 reclaim_rx_ctrl_buf(target, rx_pkt);
2541         }
2542
2543         return status;
2544 }
2545
2546 static void reset_ep_state(struct htc_target *target)
2547 {
2548         struct htc_endpoint *endpoint;
2549         int i;
2550
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;
2562         }
2563
2564         /* reset distribution list */
2565         /* FIXME: free existing entries */
2566         INIT_LIST_HEAD(&target->cred_dist_list);
2567 }
2568
2569 int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
2570                              enum htc_endpoint_id endpoint)
2571 {
2572         int num;
2573
2574         spin_lock_bh(&target->rx_lock);
2575         num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
2576         spin_unlock_bh(&target->rx_lock);
2577         return num;
2578 }
2579
2580 static void htc_setup_msg_bndl(struct htc_target *target)
2581 {
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);
2585
2586         if (ath6kl_hif_enable_scatter(target->dev->ar)) {
2587                 target->msg_per_bndl_max = 0;
2588                 return;
2589         }
2590
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);
2594
2595         ath6kl_dbg(ATH6KL_DBG_BOOT,
2596                    "htc bundling allowed msg_per_bndl_max %d\n",
2597                    target->msg_per_bndl_max);
2598
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);
2604
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);
2607
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;
2611
2612         if (target->max_rx_bndl_sz)
2613                 target->rx_bndl_enable = true;
2614
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);
2618
2619                 /*
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
2623                  * which is fatal.
2624                  */
2625                 target->tx_bndl_mask = 0;
2626         }
2627 }
2628
2629 int ath6kl_htc_wait_target(struct htc_target *target)
2630 {
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;
2635         int status;
2636
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");
2640                 return -EOPNOTSUPP;
2641         }
2642
2643         /* we should be getting 1 control message that the target is ready */
2644         packet = htc_wait_for_ctrl_msg(target);
2645
2646         if (!packet)
2647                 return -ENOMEM;
2648
2649         /* we controlled the buffer creation so it's properly aligned */
2650         rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
2651
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))) {
2654                 status = -ENOMEM;
2655                 goto fail_wait_target;
2656         }
2657
2658         if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
2659                 status = -ENOMEM;
2660                 goto fail_wait_target;
2661         }
2662
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);
2665
2666         ath6kl_dbg(ATH6KL_DBG_BOOT,
2667                    "htc target ready credits %d size %d\n",
2668                    target->tgt_creds, target->tgt_cred_sz);
2669
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;
2675         } else {
2676                 /* legacy */
2677                 target->htc_tgt_ver = HTC_VERSION_2P0;
2678                 target->msg_per_bndl_max = 0;
2679         }
2680
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);
2684
2685         if (target->msg_per_bndl_max > 0)
2686                 htc_setup_msg_bndl(target);
2687
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;
2696
2697         /* connect fake service */
2698         status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
2699
2700         if (status)
2701                 /*
2702                  * FIXME: this call doesn't make sense, the caller should
2703                  * call ath6kl_htc_cleanup() when it wants remove htc
2704                  */
2705                 ath6kl_hif_cleanup_scatter(target->dev->ar);
2706
2707 fail_wait_target:
2708         if (packet) {
2709                 htc_rxpkt_reset(packet);
2710                 reclaim_rx_ctrl_buf(target, packet);
2711         }
2712
2713         return status;
2714 }
2715
2716 /*
2717  * Start HTC, enable interrupts and let the target know
2718  * host has finished setup.
2719  */
2720 int ath6kl_htc_start(struct htc_target *target)
2721 {
2722         struct htc_packet *packet;
2723         int status;
2724
2725         memset(&target->dev->irq_proc_reg, 0,
2726                sizeof(target->dev->irq_proc_reg));
2727
2728         /* Disable interrupts at the chip level */
2729         ath6kl_hif_disable_intrs(target->dev);
2730
2731         target->htc_flags = 0;
2732         target->rx_st_flags = 0;
2733
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);
2737                 if (status)
2738                         return status;
2739         }
2740
2741         /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2742         ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
2743                            target->tgt_creds);
2744
2745         dump_cred_dist_stats(target);
2746
2747         /* Indicate to the target of the setup completion */
2748         status = htc_setup_tx_complete(target);
2749
2750         if (status)
2751                 return status;
2752
2753         /* unmask interrupts */
2754         status = ath6kl_hif_unmask_intrs(target->dev);
2755
2756         if (status)
2757                 ath6kl_htc_stop(target);
2758
2759         return status;
2760 }
2761
2762 static int ath6kl_htc_reset(struct htc_target *target)
2763 {
2764         u32 block_size, ctrl_bufsz;
2765         struct htc_packet *packet;
2766         int i;
2767
2768         reset_ep_state(target);
2769
2770         block_size = target->dev->ar->mbox_info.block_size;
2771
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);
2775
2776         for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
2777                 packet = kzalloc(sizeof(*packet), GFP_KERNEL);
2778                 if (!packet)
2779                         return -ENOMEM;
2780
2781                 packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
2782                 if (!packet->buf_start) {
2783                         kfree(packet);
2784                         return -ENOMEM;
2785                 }
2786
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);
2793                 } else
2794                         list_add_tail(&packet->list, &target->free_ctrl_txbuf);
2795         }
2796
2797         return 0;
2798 }
2799
2800 /* htc_stop: stop interrupt reception, and flush all queued buffers */
2801 void ath6kl_htc_stop(struct htc_target *target)
2802 {
2803         spin_lock_bh(&target->htc_lock);
2804         target->htc_flags |= HTC_OP_STATE_STOPPING;
2805         spin_unlock_bh(&target->htc_lock);
2806
2807         /*
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.
2811          */
2812         ath6kl_hif_mask_intrs(target->dev);
2813
2814         ath6kl_htc_flush_txep_all(target);
2815
2816         ath6kl_htc_flush_rx_buf(target);
2817
2818         ath6kl_htc_reset(target);
2819 }
2820
2821 void *ath6kl_htc_create(struct ath6kl *ar)
2822 {
2823         struct htc_target *target = NULL;
2824         int status = 0;
2825
2826         target = kzalloc(sizeof(*target), GFP_KERNEL);
2827         if (!target) {
2828                 ath6kl_err("unable to allocate memory\n");
2829                 return NULL;
2830         }
2831
2832         target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
2833         if (!target->dev) {
2834                 ath6kl_err("unable to allocate memory\n");
2835                 status = -ENOMEM;
2836                 goto err_htc_cleanup;
2837         }
2838
2839         spin_lock_init(&target->htc_lock);
2840         spin_lock_init(&target->rx_lock);
2841         spin_lock_init(&target->tx_lock);
2842
2843         INIT_LIST_HEAD(&target->free_ctrl_txbuf);
2844         INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
2845         INIT_LIST_HEAD(&target->cred_dist_list);
2846
2847         target->dev->ar = ar;
2848         target->dev->htc_cnxt = target;
2849         target->ep_waiting = ENDPOINT_MAX;
2850
2851         status = ath6kl_hif_setup(target->dev);
2852         if (status)
2853                 goto err_htc_cleanup;
2854
2855         status = ath6kl_htc_reset(target);
2856         if (status)
2857                 goto err_htc_cleanup;
2858
2859         return target;
2860
2861 err_htc_cleanup:
2862         ath6kl_htc_cleanup(target);
2863
2864         return NULL;
2865 }
2866
2867 /* cleanup the HTC instance */
2868 void ath6kl_htc_cleanup(struct htc_target *target)
2869 {
2870         struct htc_packet *packet, *tmp_packet;
2871
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);
2875
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);
2880                 kfree(packet);
2881         }
2882
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);
2887                 kfree(packet);
2888         }
2889
2890         kfree(target->dev);
2891         kfree(target);
2892 }