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 "trace.h"
23
24 #include <linux/unaligned.h>
25
26 #define CALC_TXRX_PADDED_LEN(dev, len) (__ALIGN_MASK((len), (dev)->block_mask))
27
28 static void ath6kl_htc_mbox_cleanup(struct htc_target *target);
29 static void ath6kl_htc_mbox_stop(struct htc_target *target);
30 static int ath6kl_htc_mbox_add_rxbuf_multiple(struct htc_target *target,
31 struct list_head *pkt_queue);
32 static void ath6kl_htc_set_credit_dist(struct htc_target *target,
33 struct ath6kl_htc_credit_info *cred_info,
34 u16 svc_pri_order[], int len);
35
36 /* threshold to re-enable Tx bundling for an AC*/
37 #define TX_RESUME_BUNDLE_THRESHOLD 1500
38
39 /* Functions for Tx credit handling */
ath6kl_credit_deposit(struct ath6kl_htc_credit_info * cred_info,struct htc_endpoint_credit_dist * ep_dist,int credits)40 static void ath6kl_credit_deposit(struct ath6kl_htc_credit_info *cred_info,
41 struct htc_endpoint_credit_dist *ep_dist,
42 int credits)
43 {
44 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit deposit ep %d credits %d\n",
45 ep_dist->endpoint, credits);
46
47 ep_dist->credits += credits;
48 ep_dist->cred_assngd += credits;
49 cred_info->cur_free_credits -= credits;
50 }
51
ath6kl_credit_init(struct ath6kl_htc_credit_info * cred_info,struct list_head * ep_list,int tot_credits)52 static void ath6kl_credit_init(struct ath6kl_htc_credit_info *cred_info,
53 struct list_head *ep_list,
54 int tot_credits)
55 {
56 struct htc_endpoint_credit_dist *cur_ep_dist;
57 int count;
58
59 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit init total %d\n", tot_credits);
60
61 cred_info->cur_free_credits = tot_credits;
62 cred_info->total_avail_credits = tot_credits;
63
64 list_for_each_entry(cur_ep_dist, ep_list, list) {
65 if (cur_ep_dist->endpoint == ENDPOINT_0)
66 continue;
67
68 cur_ep_dist->cred_min = cur_ep_dist->cred_per_msg;
69
70 if (tot_credits > 4) {
71 if ((cur_ep_dist->svc_id == WMI_DATA_BK_SVC) ||
72 (cur_ep_dist->svc_id == WMI_DATA_BE_SVC)) {
73 ath6kl_credit_deposit(cred_info,
74 cur_ep_dist,
75 cur_ep_dist->cred_min);
76 cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
77 }
78 }
79
80 if (cur_ep_dist->svc_id == WMI_CONTROL_SVC) {
81 ath6kl_credit_deposit(cred_info, cur_ep_dist,
82 cur_ep_dist->cred_min);
83 /*
84 * Control service is always marked active, it
85 * never goes inactive EVER.
86 */
87 cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
88 }
89
90 /*
91 * Streams have to be created (explicit | implicit) for all
92 * kinds of traffic. BE endpoints are also inactive in the
93 * beginning. When BE traffic starts it creates implicit
94 * streams that redistributes credits.
95 *
96 * Note: all other endpoints have minimums set but are
97 * initially given NO credits. credits will be distributed
98 * as traffic activity demands
99 */
100 }
101
102 /*
103 * For ath6kl_credit_seek function,
104 * it use list_for_each_entry_reverse to walk around the whole ep list.
105 * Therefore assign this lowestpri_ep_dist after walk around the ep_list
106 */
107 cred_info->lowestpri_ep_dist = cur_ep_dist->list;
108
109 WARN_ON(cred_info->cur_free_credits <= 0);
110
111 list_for_each_entry(cur_ep_dist, ep_list, list) {
112 if (cur_ep_dist->endpoint == ENDPOINT_0)
113 continue;
114
115 if (cur_ep_dist->svc_id == WMI_CONTROL_SVC) {
116 cur_ep_dist->cred_norm = cur_ep_dist->cred_per_msg;
117 } else {
118 /*
119 * For the remaining data endpoints, we assume that
120 * each cred_per_msg are the same. We use a simple
121 * calculation here, we take the remaining credits
122 * and determine how many max messages this can
123 * cover and then set each endpoint's normal value
124 * equal to 3/4 this amount.
125 */
126 count = (cred_info->cur_free_credits /
127 cur_ep_dist->cred_per_msg)
128 * cur_ep_dist->cred_per_msg;
129 count = (count * 3) >> 2;
130 count = max(count, cur_ep_dist->cred_per_msg);
131 cur_ep_dist->cred_norm = count;
132 }
133
134 ath6kl_dbg(ATH6KL_DBG_CREDIT,
135 "credit ep %d svc_id %d credits %d per_msg %d norm %d min %d\n",
136 cur_ep_dist->endpoint,
137 cur_ep_dist->svc_id,
138 cur_ep_dist->credits,
139 cur_ep_dist->cred_per_msg,
140 cur_ep_dist->cred_norm,
141 cur_ep_dist->cred_min);
142 }
143 }
144
145 /* initialize and setup credit distribution */
ath6kl_htc_mbox_credit_setup(struct htc_target * htc_target,struct ath6kl_htc_credit_info * cred_info)146 static int ath6kl_htc_mbox_credit_setup(struct htc_target *htc_target,
147 struct ath6kl_htc_credit_info *cred_info)
148 {
149 u16 servicepriority[5];
150
151 memset(cred_info, 0, sizeof(struct ath6kl_htc_credit_info));
152
153 servicepriority[0] = WMI_CONTROL_SVC; /* highest */
154 servicepriority[1] = WMI_DATA_VO_SVC;
155 servicepriority[2] = WMI_DATA_VI_SVC;
156 servicepriority[3] = WMI_DATA_BE_SVC;
157 servicepriority[4] = WMI_DATA_BK_SVC; /* lowest */
158
159 /* set priority list */
160 ath6kl_htc_set_credit_dist(htc_target, cred_info, servicepriority, 5);
161
162 return 0;
163 }
164
165 /* reduce an ep's credits back to a set limit */
ath6kl_credit_reduce(struct ath6kl_htc_credit_info * cred_info,struct htc_endpoint_credit_dist * ep_dist,int limit)166 static void ath6kl_credit_reduce(struct ath6kl_htc_credit_info *cred_info,
167 struct htc_endpoint_credit_dist *ep_dist,
168 int limit)
169 {
170 int credits;
171
172 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit reduce ep %d limit %d\n",
173 ep_dist->endpoint, limit);
174
175 ep_dist->cred_assngd = limit;
176
177 if (ep_dist->credits <= limit)
178 return;
179
180 credits = ep_dist->credits - limit;
181 ep_dist->credits -= credits;
182 cred_info->cur_free_credits += credits;
183 }
184
ath6kl_credit_update(struct ath6kl_htc_credit_info * cred_info,struct list_head * epdist_list)185 static void ath6kl_credit_update(struct ath6kl_htc_credit_info *cred_info,
186 struct list_head *epdist_list)
187 {
188 struct htc_endpoint_credit_dist *cur_list;
189
190 list_for_each_entry(cur_list, epdist_list, list) {
191 if (cur_list->endpoint == ENDPOINT_0)
192 continue;
193
194 if (cur_list->cred_to_dist > 0) {
195 cur_list->credits += cur_list->cred_to_dist;
196 cur_list->cred_to_dist = 0;
197
198 if (cur_list->credits > cur_list->cred_assngd)
199 ath6kl_credit_reduce(cred_info,
200 cur_list,
201 cur_list->cred_assngd);
202
203 if (cur_list->credits > cur_list->cred_norm)
204 ath6kl_credit_reduce(cred_info, cur_list,
205 cur_list->cred_norm);
206
207 if (!(cur_list->dist_flags & HTC_EP_ACTIVE)) {
208 if (cur_list->txq_depth == 0)
209 ath6kl_credit_reduce(cred_info,
210 cur_list, 0);
211 }
212 }
213 }
214 }
215
216 /*
217 * HTC has an endpoint that needs credits, ep_dist is the endpoint in
218 * question.
219 */
ath6kl_credit_seek(struct ath6kl_htc_credit_info * cred_info,struct htc_endpoint_credit_dist * ep_dist)220 static void ath6kl_credit_seek(struct ath6kl_htc_credit_info *cred_info,
221 struct htc_endpoint_credit_dist *ep_dist)
222 {
223 struct htc_endpoint_credit_dist *curdist_list;
224 int credits = 0;
225 int need;
226
227 if (ep_dist->svc_id == WMI_CONTROL_SVC)
228 goto out;
229
230 if ((ep_dist->svc_id == WMI_DATA_VI_SVC) ||
231 (ep_dist->svc_id == WMI_DATA_VO_SVC))
232 if ((ep_dist->cred_assngd >= ep_dist->cred_norm))
233 goto out;
234
235 /*
236 * For all other services, we follow a simple algorithm of:
237 *
238 * 1. checking the free pool for credits
239 * 2. checking lower priority endpoints for credits to take
240 */
241
242 credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
243
244 if (credits >= ep_dist->seek_cred)
245 goto out;
246
247 /*
248 * We don't have enough in the free pool, try taking away from
249 * lower priority services The rule for taking away credits:
250 *
251 * 1. Only take from lower priority endpoints
252 * 2. Only take what is allocated above the minimum (never
253 * starve an endpoint completely)
254 * 3. Only take what you need.
255 */
256
257 list_for_each_entry_reverse(curdist_list,
258 &cred_info->lowestpri_ep_dist,
259 list) {
260 if (curdist_list == ep_dist)
261 break;
262
263 need = ep_dist->seek_cred - cred_info->cur_free_credits;
264
265 if ((curdist_list->cred_assngd - need) >=
266 curdist_list->cred_min) {
267 /*
268 * The current one has been allocated more than
269 * it's minimum and it has enough credits assigned
270 * above it's minimum to fulfill our need try to
271 * take away just enough to fulfill our need.
272 */
273 ath6kl_credit_reduce(cred_info, curdist_list,
274 curdist_list->cred_assngd - need);
275
276 if (cred_info->cur_free_credits >=
277 ep_dist->seek_cred)
278 break;
279 }
280
281 if (curdist_list->endpoint == ENDPOINT_0)
282 break;
283 }
284
285 credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
286
287 out:
288 /* did we find some credits? */
289 if (credits)
290 ath6kl_credit_deposit(cred_info, ep_dist, credits);
291
292 ep_dist->seek_cred = 0;
293 }
294
295 /* redistribute credits based on activity change */
ath6kl_credit_redistribute(struct ath6kl_htc_credit_info * info,struct list_head * ep_dist_list)296 static void ath6kl_credit_redistribute(struct ath6kl_htc_credit_info *info,
297 struct list_head *ep_dist_list)
298 {
299 struct htc_endpoint_credit_dist *curdist_list;
300
301 list_for_each_entry(curdist_list, ep_dist_list, list) {
302 if (curdist_list->endpoint == ENDPOINT_0)
303 continue;
304
305 if ((curdist_list->svc_id == WMI_DATA_BK_SVC) ||
306 (curdist_list->svc_id == WMI_DATA_BE_SVC))
307 curdist_list->dist_flags |= HTC_EP_ACTIVE;
308
309 if ((curdist_list->svc_id != WMI_CONTROL_SVC) &&
310 !(curdist_list->dist_flags & HTC_EP_ACTIVE)) {
311 if (curdist_list->txq_depth == 0)
312 ath6kl_credit_reduce(info, curdist_list, 0);
313 else
314 ath6kl_credit_reduce(info,
315 curdist_list,
316 curdist_list->cred_min);
317 }
318 }
319 }
320
321 /*
322 *
323 * This function is invoked whenever endpoints require credit
324 * distributions. A lock is held while this function is invoked, this
325 * function shall NOT block. The ep_dist_list is a list of distribution
326 * structures in prioritized order as defined by the call to the
327 * htc_set_credit_dist() api.
328 */
ath6kl_credit_distribute(struct ath6kl_htc_credit_info * cred_info,struct list_head * ep_dist_list,enum htc_credit_dist_reason reason)329 static void ath6kl_credit_distribute(struct ath6kl_htc_credit_info *cred_info,
330 struct list_head *ep_dist_list,
331 enum htc_credit_dist_reason reason)
332 {
333 switch (reason) {
334 case HTC_CREDIT_DIST_SEND_COMPLETE:
335 ath6kl_credit_update(cred_info, ep_dist_list);
336 break;
337 case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
338 ath6kl_credit_redistribute(cred_info, ep_dist_list);
339 break;
340 default:
341 break;
342 }
343
344 WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
345 WARN_ON(cred_info->cur_free_credits < 0);
346 }
347
ath6kl_htc_tx_buf_align(u8 ** buf,unsigned long len)348 static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
349 {
350 u8 *align_addr;
351
352 if (!IS_ALIGNED((unsigned long) *buf, 4)) {
353 align_addr = PTR_ALIGN(*buf - 4, 4);
354 memmove(align_addr, *buf, len);
355 *buf = align_addr;
356 }
357 }
358
ath6kl_htc_tx_prep_pkt(struct htc_packet * packet,u8 flags,int ctrl0,int ctrl1)359 static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
360 int ctrl0, int ctrl1)
361 {
362 struct htc_frame_hdr *hdr;
363
364 packet->buf -= HTC_HDR_LENGTH;
365 hdr = (struct htc_frame_hdr *)packet->buf;
366
367 put_unaligned_le16(packet->act_len, &hdr->payld_len);
368 hdr->flags = flags;
369 hdr->eid = packet->endpoint;
370 hdr->ctrl[0] = ctrl0;
371 hdr->ctrl[1] = ctrl1;
372 }
373
htc_reclaim_txctrl_buf(struct htc_target * target,struct htc_packet * pkt)374 static void htc_reclaim_txctrl_buf(struct htc_target *target,
375 struct htc_packet *pkt)
376 {
377 spin_lock_bh(&target->htc_lock);
378 list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
379 spin_unlock_bh(&target->htc_lock);
380 }
381
htc_get_control_buf(struct htc_target * target,bool tx)382 static struct htc_packet *htc_get_control_buf(struct htc_target *target,
383 bool tx)
384 {
385 struct htc_packet *packet = NULL;
386 struct list_head *buf_list;
387
388 buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;
389
390 spin_lock_bh(&target->htc_lock);
391
392 if (list_empty(buf_list)) {
393 spin_unlock_bh(&target->htc_lock);
394 return NULL;
395 }
396
397 packet = list_first_entry(buf_list, struct htc_packet, list);
398 list_del(&packet->list);
399 spin_unlock_bh(&target->htc_lock);
400
401 if (tx)
402 packet->buf = packet->buf_start + HTC_HDR_LENGTH;
403
404 return packet;
405 }
406
htc_tx_comp_update(struct htc_target * target,struct htc_endpoint * endpoint,struct htc_packet * packet)407 static void htc_tx_comp_update(struct htc_target *target,
408 struct htc_endpoint *endpoint,
409 struct htc_packet *packet)
410 {
411 packet->completion = NULL;
412 packet->buf += HTC_HDR_LENGTH;
413
414 if (!packet->status)
415 return;
416
417 ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
418 packet->status, packet->endpoint, packet->act_len,
419 packet->info.tx.cred_used);
420
421 /* on failure to submit, reclaim credits for this packet */
422 spin_lock_bh(&target->tx_lock);
423 endpoint->cred_dist.cred_to_dist +=
424 packet->info.tx.cred_used;
425 endpoint->cred_dist.txq_depth = get_queue_depth(&endpoint->txq);
426
427 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
428 target->credit_info, &target->cred_dist_list);
429
430 ath6kl_credit_distribute(target->credit_info,
431 &target->cred_dist_list,
432 HTC_CREDIT_DIST_SEND_COMPLETE);
433
434 spin_unlock_bh(&target->tx_lock);
435 }
436
htc_tx_complete(struct htc_endpoint * endpoint,struct list_head * txq)437 static void htc_tx_complete(struct htc_endpoint *endpoint,
438 struct list_head *txq)
439 {
440 if (list_empty(txq))
441 return;
442
443 ath6kl_dbg(ATH6KL_DBG_HTC,
444 "htc tx complete ep %d pkts %d\n",
445 endpoint->eid, get_queue_depth(txq));
446
447 ath6kl_tx_complete(endpoint->target, txq);
448 }
449
htc_tx_comp_handler(struct htc_target * target,struct htc_packet * packet)450 static void htc_tx_comp_handler(struct htc_target *target,
451 struct htc_packet *packet)
452 {
453 struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
454 struct list_head container;
455
456 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx complete seqno %d\n",
457 packet->info.tx.seqno);
458
459 htc_tx_comp_update(target, endpoint, packet);
460 INIT_LIST_HEAD(&container);
461 list_add_tail(&packet->list, &container);
462 /* do completion */
463 htc_tx_complete(endpoint, &container);
464 }
465
htc_async_tx_scat_complete(struct htc_target * target,struct hif_scatter_req * scat_req)466 static void htc_async_tx_scat_complete(struct htc_target *target,
467 struct hif_scatter_req *scat_req)
468 {
469 struct htc_endpoint *endpoint;
470 struct htc_packet *packet;
471 struct list_head tx_compq;
472 int i;
473
474 INIT_LIST_HEAD(&tx_compq);
475
476 ath6kl_dbg(ATH6KL_DBG_HTC,
477 "htc tx scat complete len %d entries %d\n",
478 scat_req->len, scat_req->scat_entries);
479
480 if (scat_req->status)
481 ath6kl_err("send scatter req failed: %d\n", scat_req->status);
482
483 packet = scat_req->scat_list[0].packet;
484 endpoint = &target->endpoint[packet->endpoint];
485
486 /* walk through the scatter list and process */
487 for (i = 0; i < scat_req->scat_entries; i++) {
488 packet = scat_req->scat_list[i].packet;
489 if (!packet) {
490 WARN_ON(1);
491 return;
492 }
493
494 packet->status = scat_req->status;
495 htc_tx_comp_update(target, endpoint, packet);
496 list_add_tail(&packet->list, &tx_compq);
497 }
498
499 /* free scatter request */
500 hif_scatter_req_add(target->dev->ar, scat_req);
501
502 /* complete all packets */
503 htc_tx_complete(endpoint, &tx_compq);
504 }
505
ath6kl_htc_tx_issue(struct htc_target * target,struct htc_packet * packet)506 static int ath6kl_htc_tx_issue(struct htc_target *target,
507 struct htc_packet *packet)
508 {
509 int status;
510 bool sync = false;
511 u32 padded_len, send_len;
512
513 if (!packet->completion)
514 sync = true;
515
516 send_len = packet->act_len + HTC_HDR_LENGTH;
517
518 padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
519
520 ath6kl_dbg(ATH6KL_DBG_HTC,
521 "htc tx issue len %d seqno %d padded_len %d mbox 0x%X %s\n",
522 send_len, packet->info.tx.seqno, padded_len,
523 target->dev->ar->mbox_info.htc_addr,
524 sync ? "sync" : "async");
525
526 if (sync) {
527 status = hif_read_write_sync(target->dev->ar,
528 target->dev->ar->mbox_info.htc_addr,
529 packet->buf, padded_len,
530 HIF_WR_SYNC_BLOCK_INC);
531
532 packet->status = status;
533 packet->buf += HTC_HDR_LENGTH;
534 } else
535 status = hif_write_async(target->dev->ar,
536 target->dev->ar->mbox_info.htc_addr,
537 packet->buf, padded_len,
538 HIF_WR_ASYNC_BLOCK_INC, packet);
539
540 trace_ath6kl_htc_tx(status, packet->endpoint, packet->buf, send_len);
541
542 return status;
543 }
544
htc_check_credits(struct htc_target * target,struct htc_endpoint * ep,u8 * flags,enum htc_endpoint_id eid,unsigned int len,int * req_cred)545 static int htc_check_credits(struct htc_target *target,
546 struct htc_endpoint *ep, u8 *flags,
547 enum htc_endpoint_id eid, unsigned int len,
548 int *req_cred)
549 {
550 *req_cred = (len > target->tgt_cred_sz) ?
551 DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
552
553 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit check need %d got %d\n",
554 *req_cred, ep->cred_dist.credits);
555
556 if (ep->cred_dist.credits < *req_cred) {
557 if (eid == ENDPOINT_0)
558 return -EINVAL;
559
560 /* Seek more credits */
561 ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
562
563 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
564
565 ep->cred_dist.seek_cred = 0;
566
567 if (ep->cred_dist.credits < *req_cred) {
568 ath6kl_dbg(ATH6KL_DBG_CREDIT,
569 "credit not found for ep %d\n",
570 eid);
571 return -EINVAL;
572 }
573 }
574
575 ep->cred_dist.credits -= *req_cred;
576 ep->ep_st.cred_cosumd += *req_cred;
577
578 /* When we are getting low on credits, ask for more */
579 if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
580 ep->cred_dist.seek_cred =
581 ep->cred_dist.cred_per_msg - ep->cred_dist.credits;
582
583 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
584
585 /* see if we were successful in getting more */
586 if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
587 /* tell the target we need credits ASAP! */
588 *flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
589 ep->ep_st.cred_low_indicate += 1;
590 ath6kl_dbg(ATH6KL_DBG_CREDIT,
591 "credit we need credits asap\n");
592 }
593 }
594
595 return 0;
596 }
597
ath6kl_htc_tx_pkts_get(struct htc_target * target,struct htc_endpoint * endpoint,struct list_head * queue)598 static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
599 struct htc_endpoint *endpoint,
600 struct list_head *queue)
601 {
602 int req_cred;
603 u8 flags;
604 struct htc_packet *packet;
605 unsigned int len;
606
607 while (true) {
608 flags = 0;
609
610 if (list_empty(&endpoint->txq))
611 break;
612 packet = list_first_entry(&endpoint->txq, struct htc_packet,
613 list);
614
615 ath6kl_dbg(ATH6KL_DBG_HTC,
616 "htc tx got packet 0x%p queue depth %d\n",
617 packet, get_queue_depth(&endpoint->txq));
618
619 len = CALC_TXRX_PADDED_LEN(target,
620 packet->act_len + HTC_HDR_LENGTH);
621
622 if (htc_check_credits(target, endpoint, &flags,
623 packet->endpoint, len, &req_cred))
624 break;
625
626 /* now we can fully move onto caller's queue */
627 packet = list_first_entry(&endpoint->txq, struct htc_packet,
628 list);
629 list_move_tail(&packet->list, queue);
630
631 /* save the number of credits this packet consumed */
632 packet->info.tx.cred_used = req_cred;
633
634 /* all TX packets are handled asynchronously */
635 packet->completion = htc_tx_comp_handler;
636 packet->context = target;
637 endpoint->ep_st.tx_issued += 1;
638
639 /* save send flags */
640 packet->info.tx.flags = flags;
641 packet->info.tx.seqno = endpoint->seqno;
642 endpoint->seqno++;
643 }
644 }
645
646 /* See if the padded tx length falls on a credit boundary */
htc_get_credit_padding(unsigned int cred_sz,int * len,struct htc_endpoint * ep)647 static int htc_get_credit_padding(unsigned int cred_sz, int *len,
648 struct htc_endpoint *ep)
649 {
650 int rem_cred, cred_pad;
651
652 rem_cred = *len % cred_sz;
653
654 /* No padding needed */
655 if (!rem_cred)
656 return 0;
657
658 if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
659 return -1;
660
661 /*
662 * The transfer consumes a "partial" credit, this
663 * packet cannot be bundled unless we add
664 * additional "dummy" padding (max 255 bytes) to
665 * consume the entire credit.
666 */
667 cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
668
669 if ((cred_pad > 0) && (cred_pad <= 255))
670 *len += cred_pad;
671 else
672 /* The amount of padding is too large, send as non-bundled */
673 return -1;
674
675 return cred_pad;
676 }
677
ath6kl_htc_tx_setup_scat_list(struct htc_target * target,struct htc_endpoint * endpoint,struct hif_scatter_req * scat_req,int n_scat,struct list_head * queue)678 static int ath6kl_htc_tx_setup_scat_list(struct htc_target *target,
679 struct htc_endpoint *endpoint,
680 struct hif_scatter_req *scat_req,
681 int n_scat,
682 struct list_head *queue)
683 {
684 struct htc_packet *packet;
685 int i, len, rem_scat, cred_pad;
686 int status = 0;
687 u8 flags;
688
689 rem_scat = target->max_tx_bndl_sz;
690
691 for (i = 0; i < n_scat; i++) {
692 scat_req->scat_list[i].packet = NULL;
693
694 if (list_empty(queue))
695 break;
696
697 packet = list_first_entry(queue, struct htc_packet, list);
698 len = CALC_TXRX_PADDED_LEN(target,
699 packet->act_len + HTC_HDR_LENGTH);
700
701 cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
702 &len, endpoint);
703 if (cred_pad < 0 || rem_scat < len) {
704 status = -ENOSPC;
705 break;
706 }
707
708 rem_scat -= len;
709 /* now remove it from the queue */
710 list_del(&packet->list);
711
712 scat_req->scat_list[i].packet = packet;
713 /* prepare packet and flag message as part of a send bundle */
714 flags = packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE;
715 ath6kl_htc_tx_prep_pkt(packet, flags,
716 cred_pad, packet->info.tx.seqno);
717 /* Make sure the buffer is 4-byte aligned */
718 ath6kl_htc_tx_buf_align(&packet->buf,
719 packet->act_len + HTC_HDR_LENGTH);
720 scat_req->scat_list[i].buf = packet->buf;
721 scat_req->scat_list[i].len = len;
722
723 scat_req->len += len;
724 scat_req->scat_entries++;
725 ath6kl_dbg(ATH6KL_DBG_HTC,
726 "htc tx adding (%d) pkt 0x%p seqno %d len %d remaining %d\n",
727 i, packet, packet->info.tx.seqno, len, rem_scat);
728 }
729
730 /* Roll back scatter setup in case of any failure */
731 if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
732 for (i = scat_req->scat_entries - 1; i >= 0; i--) {
733 packet = scat_req->scat_list[i].packet;
734 if (packet) {
735 packet->buf += HTC_HDR_LENGTH;
736 list_add(&packet->list, queue);
737 }
738 }
739 return -EAGAIN;
740 }
741
742 return status;
743 }
744
745 /*
746 * Drain a queue and send as bundles this function may return without fully
747 * draining the queue when
748 *
749 * 1. scatter resources are exhausted
750 * 2. a message that will consume a partial credit will stop the
751 * bundling process early
752 * 3. we drop below the minimum number of messages for a bundle
753 */
ath6kl_htc_tx_bundle(struct htc_endpoint * endpoint,struct list_head * queue,int * sent_bundle,int * n_bundle_pkts)754 static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
755 struct list_head *queue,
756 int *sent_bundle, int *n_bundle_pkts)
757 {
758 struct htc_target *target = endpoint->target;
759 struct hif_scatter_req *scat_req = NULL;
760 int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0, i;
761 struct htc_packet *packet;
762 int status;
763 u32 txb_mask;
764 u8 ac = WMM_NUM_AC;
765
766 if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) &&
767 (WMI_CONTROL_SVC != endpoint->svc_id))
768 ac = target->dev->ar->ep2ac_map[endpoint->eid];
769
770 while (true) {
771 status = 0;
772 n_scat = get_queue_depth(queue);
773 n_scat = min(n_scat, target->msg_per_bndl_max);
774
775 if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
776 /* not enough to bundle */
777 break;
778
779 scat_req = hif_scatter_req_get(target->dev->ar);
780
781 if (!scat_req) {
782 /* no scatter resources */
783 ath6kl_dbg(ATH6KL_DBG_HTC,
784 "htc tx no more scatter resources\n");
785 break;
786 }
787
788 if ((ac < WMM_NUM_AC) && (ac != WMM_AC_BK)) {
789 if (WMM_AC_BE == ac)
790 /*
791 * BE, BK have priorities and bit
792 * positions reversed
793 */
794 txb_mask = (1 << WMM_AC_BK);
795 else
796 /*
797 * any AC with priority lower than
798 * itself
799 */
800 txb_mask = ((1 << ac) - 1);
801
802 /*
803 * when the scatter request resources drop below a
804 * certain threshold, disable Tx bundling for all
805 * AC's with priority lower than the current requesting
806 * AC. Otherwise re-enable Tx bundling for them
807 */
808 if (scat_req->scat_q_depth < ATH6KL_SCATTER_REQS)
809 target->tx_bndl_mask &= ~txb_mask;
810 else
811 target->tx_bndl_mask |= txb_mask;
812 }
813
814 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
815 n_scat);
816
817 scat_req->len = 0;
818 scat_req->scat_entries = 0;
819
820 status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
821 scat_req, n_scat,
822 queue);
823 if (status == -EAGAIN) {
824 hif_scatter_req_add(target->dev->ar, scat_req);
825 break;
826 }
827
828 /* send path is always asynchronous */
829 scat_req->complete = htc_async_tx_scat_complete;
830 n_sent_bundle++;
831 tot_pkts_bundle += scat_req->scat_entries;
832
833 ath6kl_dbg(ATH6KL_DBG_HTC,
834 "htc tx scatter bytes %d entries %d\n",
835 scat_req->len, scat_req->scat_entries);
836
837 for (i = 0; i < scat_req->scat_entries; i++) {
838 packet = scat_req->scat_list[i].packet;
839 trace_ath6kl_htc_tx(packet->status, packet->endpoint,
840 packet->buf, packet->act_len);
841 }
842
843 ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
844
845 if (status)
846 break;
847 }
848
849 *sent_bundle = n_sent_bundle;
850 *n_bundle_pkts = tot_pkts_bundle;
851 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
852 n_sent_bundle);
853
854 return;
855 }
856
ath6kl_htc_tx_from_queue(struct htc_target * target,struct htc_endpoint * endpoint)857 static void ath6kl_htc_tx_from_queue(struct htc_target *target,
858 struct htc_endpoint *endpoint)
859 {
860 struct list_head txq;
861 struct htc_packet *packet;
862 int bundle_sent;
863 int n_pkts_bundle;
864 u8 ac = WMM_NUM_AC;
865 int status;
866
867 spin_lock_bh(&target->tx_lock);
868
869 endpoint->tx_proc_cnt++;
870 if (endpoint->tx_proc_cnt > 1) {
871 endpoint->tx_proc_cnt--;
872 spin_unlock_bh(&target->tx_lock);
873 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
874 return;
875 }
876
877 /*
878 * drain the endpoint TX queue for transmission as long
879 * as we have enough credits.
880 */
881 INIT_LIST_HEAD(&txq);
882
883 if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) &&
884 (WMI_CONTROL_SVC != endpoint->svc_id))
885 ac = target->dev->ar->ep2ac_map[endpoint->eid];
886
887 while (true) {
888 if (list_empty(&endpoint->txq))
889 break;
890
891 ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
892
893 if (list_empty(&txq))
894 break;
895
896 spin_unlock_bh(&target->tx_lock);
897
898 bundle_sent = 0;
899 n_pkts_bundle = 0;
900
901 while (true) {
902 /* try to send a bundle on each pass */
903 if ((target->tx_bndl_mask) &&
904 (get_queue_depth(&txq) >=
905 HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
906 int temp1 = 0, temp2 = 0;
907
908 /* check if bundling is enabled for an AC */
909 if (target->tx_bndl_mask & (1 << ac)) {
910 ath6kl_htc_tx_bundle(endpoint, &txq,
911 &temp1, &temp2);
912 bundle_sent += temp1;
913 n_pkts_bundle += temp2;
914 }
915 }
916
917 if (list_empty(&txq))
918 break;
919
920 packet = list_first_entry(&txq, struct htc_packet,
921 list);
922 list_del(&packet->list);
923
924 ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
925 0, packet->info.tx.seqno);
926 status = ath6kl_htc_tx_issue(target, packet);
927
928 if (status) {
929 packet->status = status;
930 packet->completion(packet->context, packet);
931 }
932 }
933
934 spin_lock_bh(&target->tx_lock);
935
936 endpoint->ep_st.tx_bundles += bundle_sent;
937 endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
938
939 /*
940 * if an AC has bundling disabled and no tx bundling
941 * has occured continously for a certain number of TX,
942 * enable tx bundling for this AC
943 */
944 if (!bundle_sent) {
945 if (!(target->tx_bndl_mask & (1 << ac)) &&
946 (ac < WMM_NUM_AC)) {
947 if (++target->ac_tx_count[ac] >=
948 TX_RESUME_BUNDLE_THRESHOLD) {
949 target->ac_tx_count[ac] = 0;
950 target->tx_bndl_mask |= (1 << ac);
951 }
952 }
953 } else {
954 /* tx bundling will reset the counter */
955 if (ac < WMM_NUM_AC)
956 target->ac_tx_count[ac] = 0;
957 }
958 }
959
960 endpoint->tx_proc_cnt = 0;
961 spin_unlock_bh(&target->tx_lock);
962 }
963
ath6kl_htc_tx_try(struct htc_target * target,struct htc_endpoint * endpoint,struct htc_packet * tx_pkt)964 static bool ath6kl_htc_tx_try(struct htc_target *target,
965 struct htc_endpoint *endpoint,
966 struct htc_packet *tx_pkt)
967 {
968 struct htc_ep_callbacks ep_cb;
969 int txq_depth;
970 bool overflow = false;
971
972 ep_cb = endpoint->ep_cb;
973
974 spin_lock_bh(&target->tx_lock);
975 txq_depth = get_queue_depth(&endpoint->txq);
976 spin_unlock_bh(&target->tx_lock);
977
978 if (txq_depth >= endpoint->max_txq_depth)
979 overflow = true;
980
981 if (overflow)
982 ath6kl_dbg(ATH6KL_DBG_HTC,
983 "htc tx overflow ep %d depth %d max %d\n",
984 endpoint->eid, txq_depth,
985 endpoint->max_txq_depth);
986
987 if (overflow && ep_cb.tx_full) {
988 if (ep_cb.tx_full(endpoint->target, tx_pkt) ==
989 HTC_SEND_FULL_DROP) {
990 endpoint->ep_st.tx_dropped += 1;
991 return false;
992 }
993 }
994
995 spin_lock_bh(&target->tx_lock);
996 list_add_tail(&tx_pkt->list, &endpoint->txq);
997 spin_unlock_bh(&target->tx_lock);
998
999 ath6kl_htc_tx_from_queue(target, endpoint);
1000
1001 return true;
1002 }
1003
htc_chk_ep_txq(struct htc_target * target)1004 static void htc_chk_ep_txq(struct htc_target *target)
1005 {
1006 struct htc_endpoint *endpoint;
1007 struct htc_endpoint_credit_dist *cred_dist;
1008
1009 /*
1010 * Run through the credit distribution list to see if there are
1011 * packets queued. NOTE: no locks need to be taken since the
1012 * distribution list is not dynamic (cannot be re-ordered) and we
1013 * are not modifying any state.
1014 */
1015 list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
1016 endpoint = cred_dist->htc_ep;
1017
1018 spin_lock_bh(&target->tx_lock);
1019 if (!list_empty(&endpoint->txq)) {
1020 ath6kl_dbg(ATH6KL_DBG_HTC,
1021 "htc creds ep %d credits %d pkts %d\n",
1022 cred_dist->endpoint,
1023 endpoint->cred_dist.credits,
1024 get_queue_depth(&endpoint->txq));
1025 spin_unlock_bh(&target->tx_lock);
1026 /*
1027 * Try to start the stalled queue, this list is
1028 * ordered by priority. If there are credits
1029 * available the highest priority queue will get a
1030 * chance to reclaim credits from lower priority
1031 * ones.
1032 */
1033 ath6kl_htc_tx_from_queue(target, endpoint);
1034 spin_lock_bh(&target->tx_lock);
1035 }
1036 spin_unlock_bh(&target->tx_lock);
1037 }
1038 }
1039
htc_setup_tx_complete(struct htc_target * target)1040 static int htc_setup_tx_complete(struct htc_target *target)
1041 {
1042 struct htc_packet *send_pkt = NULL;
1043 int status;
1044
1045 send_pkt = htc_get_control_buf(target, true);
1046
1047 if (!send_pkt)
1048 return -ENOMEM;
1049
1050 if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
1051 struct htc_setup_comp_ext_msg *setup_comp_ext;
1052 u32 flags = 0;
1053
1054 setup_comp_ext =
1055 (struct htc_setup_comp_ext_msg *)send_pkt->buf;
1056 memset(setup_comp_ext, 0, sizeof(*setup_comp_ext));
1057 setup_comp_ext->msg_id =
1058 cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
1059
1060 if (target->msg_per_bndl_max > 0) {
1061 /* Indicate HTC bundling to the target */
1062 flags |= HTC_SETUP_COMP_FLG_RX_BNDL_EN;
1063 setup_comp_ext->msg_per_rxbndl =
1064 target->msg_per_bndl_max;
1065 }
1066
1067 memcpy(&setup_comp_ext->flags, &flags,
1068 sizeof(setup_comp_ext->flags));
1069 set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp_ext,
1070 sizeof(struct htc_setup_comp_ext_msg),
1071 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
1072
1073 } else {
1074 struct htc_setup_comp_msg *setup_comp;
1075 setup_comp = (struct htc_setup_comp_msg *)send_pkt->buf;
1076 memset(setup_comp, 0, sizeof(struct htc_setup_comp_msg));
1077 setup_comp->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID);
1078 set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp,
1079 sizeof(struct htc_setup_comp_msg),
1080 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
1081 }
1082
1083 /* we want synchronous operation */
1084 send_pkt->completion = NULL;
1085 ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
1086 status = ath6kl_htc_tx_issue(target, send_pkt);
1087 htc_reclaim_txctrl_buf(target, send_pkt);
1088
1089 return status;
1090 }
1091
ath6kl_htc_set_credit_dist(struct htc_target * target,struct ath6kl_htc_credit_info * credit_info,u16 srvc_pri_order[],int list_len)1092 static void ath6kl_htc_set_credit_dist(struct htc_target *target,
1093 struct ath6kl_htc_credit_info *credit_info,
1094 u16 srvc_pri_order[], int list_len)
1095 {
1096 struct htc_endpoint *endpoint;
1097 int i, ep;
1098
1099 target->credit_info = credit_info;
1100
1101 list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
1102 &target->cred_dist_list);
1103
1104 for (i = 0; i < list_len; i++) {
1105 for (ep = ENDPOINT_1; ep < ENDPOINT_MAX; ep++) {
1106 endpoint = &target->endpoint[ep];
1107 if (endpoint->svc_id == srvc_pri_order[i]) {
1108 list_add_tail(&endpoint->cred_dist.list,
1109 &target->cred_dist_list);
1110 break;
1111 }
1112 }
1113 if (ep >= ENDPOINT_MAX) {
1114 WARN_ON(1);
1115 return;
1116 }
1117 }
1118 }
1119
ath6kl_htc_mbox_tx(struct htc_target * target,struct htc_packet * packet)1120 static int ath6kl_htc_mbox_tx(struct htc_target *target,
1121 struct htc_packet *packet)
1122 {
1123 struct htc_endpoint *endpoint;
1124 struct list_head queue;
1125
1126 ath6kl_dbg(ATH6KL_DBG_HTC,
1127 "htc tx ep id %d buf 0x%p len %d\n",
1128 packet->endpoint, packet->buf, packet->act_len);
1129
1130 if (packet->endpoint >= ENDPOINT_MAX) {
1131 WARN_ON(1);
1132 return -EINVAL;
1133 }
1134
1135 endpoint = &target->endpoint[packet->endpoint];
1136
1137 if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
1138 packet->status = (target->htc_flags & HTC_OP_STATE_STOPPING) ?
1139 -ECANCELED : -ENOSPC;
1140 INIT_LIST_HEAD(&queue);
1141 list_add(&packet->list, &queue);
1142 htc_tx_complete(endpoint, &queue);
1143 }
1144
1145 return 0;
1146 }
1147
1148 /* flush endpoint TX queue */
ath6kl_htc_mbox_flush_txep(struct htc_target * target,enum htc_endpoint_id eid,u16 tag)1149 static void ath6kl_htc_mbox_flush_txep(struct htc_target *target,
1150 enum htc_endpoint_id eid, u16 tag)
1151 {
1152 struct htc_packet *packet, *tmp_pkt;
1153 struct list_head discard_q, container;
1154 struct htc_endpoint *endpoint = &target->endpoint[eid];
1155
1156 if (!endpoint->svc_id) {
1157 WARN_ON(1);
1158 return;
1159 }
1160
1161 /* initialize the discard queue */
1162 INIT_LIST_HEAD(&discard_q);
1163
1164 spin_lock_bh(&target->tx_lock);
1165
1166 list_for_each_entry_safe(packet, tmp_pkt, &endpoint->txq, list) {
1167 if ((tag == HTC_TX_PACKET_TAG_ALL) ||
1168 (tag == packet->info.tx.tag))
1169 list_move_tail(&packet->list, &discard_q);
1170 }
1171
1172 spin_unlock_bh(&target->tx_lock);
1173
1174 list_for_each_entry_safe(packet, tmp_pkt, &discard_q, list) {
1175 packet->status = -ECANCELED;
1176 list_del(&packet->list);
1177 ath6kl_dbg(ATH6KL_DBG_HTC,
1178 "htc tx flushing pkt 0x%p len %d ep %d tag 0x%x\n",
1179 packet, packet->act_len,
1180 packet->endpoint, packet->info.tx.tag);
1181
1182 INIT_LIST_HEAD(&container);
1183 list_add_tail(&packet->list, &container);
1184 htc_tx_complete(endpoint, &container);
1185 }
1186 }
1187
ath6kl_htc_flush_txep_all(struct htc_target * target)1188 static void ath6kl_htc_flush_txep_all(struct htc_target *target)
1189 {
1190 struct htc_endpoint *endpoint;
1191 int i;
1192
1193 dump_cred_dist_stats(target);
1194
1195 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
1196 endpoint = &target->endpoint[i];
1197 if (endpoint->svc_id == 0)
1198 /* not in use.. */
1199 continue;
1200 ath6kl_htc_mbox_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
1201 }
1202 }
1203
ath6kl_htc_mbox_activity_changed(struct htc_target * target,enum htc_endpoint_id eid,bool active)1204 static void ath6kl_htc_mbox_activity_changed(struct htc_target *target,
1205 enum htc_endpoint_id eid,
1206 bool active)
1207 {
1208 struct htc_endpoint *endpoint = &target->endpoint[eid];
1209 bool dist = false;
1210
1211 if (endpoint->svc_id == 0) {
1212 WARN_ON(1);
1213 return;
1214 }
1215
1216 spin_lock_bh(&target->tx_lock);
1217
1218 if (active) {
1219 if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
1220 endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
1221 dist = true;
1222 }
1223 } else {
1224 if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
1225 endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
1226 dist = true;
1227 }
1228 }
1229
1230 if (dist) {
1231 endpoint->cred_dist.txq_depth =
1232 get_queue_depth(&endpoint->txq);
1233
1234 ath6kl_dbg(ATH6KL_DBG_HTC,
1235 "htc tx activity ctxt 0x%p dist 0x%p\n",
1236 target->credit_info, &target->cred_dist_list);
1237
1238 ath6kl_credit_distribute(target->credit_info,
1239 &target->cred_dist_list,
1240 HTC_CREDIT_DIST_ACTIVITY_CHANGE);
1241 }
1242
1243 spin_unlock_bh(&target->tx_lock);
1244
1245 if (dist && !active)
1246 htc_chk_ep_txq(target);
1247 }
1248
1249 /* HTC Rx */
1250
ath6kl_htc_rx_update_stats(struct htc_endpoint * endpoint,int n_look_ahds)1251 static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
1252 int n_look_ahds)
1253 {
1254 endpoint->ep_st.rx_pkts++;
1255 if (n_look_ahds == 1)
1256 endpoint->ep_st.rx_lkahds++;
1257 else if (n_look_ahds > 1)
1258 endpoint->ep_st.rx_bundle_lkahd++;
1259 }
1260
htc_valid_rx_frame_len(struct htc_target * target,enum htc_endpoint_id eid,int len)1261 static inline bool htc_valid_rx_frame_len(struct htc_target *target,
1262 enum htc_endpoint_id eid, int len)
1263 {
1264 return (eid == target->dev->ar->ctrl_ep) ?
1265 len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
1266 }
1267
htc_add_rxbuf(struct htc_target * target,struct htc_packet * packet)1268 static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
1269 {
1270 struct list_head queue;
1271
1272 INIT_LIST_HEAD(&queue);
1273 list_add_tail(&packet->list, &queue);
1274 return ath6kl_htc_mbox_add_rxbuf_multiple(target, &queue);
1275 }
1276
htc_reclaim_rxbuf(struct htc_target * target,struct htc_packet * packet,struct htc_endpoint * ep)1277 static void htc_reclaim_rxbuf(struct htc_target *target,
1278 struct htc_packet *packet,
1279 struct htc_endpoint *ep)
1280 {
1281 if (packet->info.rx.rx_flags & HTC_RX_PKT_NO_RECYCLE) {
1282 htc_rxpkt_reset(packet);
1283 packet->status = -ECANCELED;
1284 ep->ep_cb.rx(ep->target, packet);
1285 } else {
1286 htc_rxpkt_reset(packet);
1287 htc_add_rxbuf((void *)(target), packet);
1288 }
1289 }
1290
reclaim_rx_ctrl_buf(struct htc_target * target,struct htc_packet * packet)1291 static void reclaim_rx_ctrl_buf(struct htc_target *target,
1292 struct htc_packet *packet)
1293 {
1294 spin_lock_bh(&target->htc_lock);
1295 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
1296 spin_unlock_bh(&target->htc_lock);
1297 }
1298
ath6kl_htc_rx_packet(struct htc_target * target,struct htc_packet * packet,u32 rx_len)1299 static int ath6kl_htc_rx_packet(struct htc_target *target,
1300 struct htc_packet *packet,
1301 u32 rx_len)
1302 {
1303 struct ath6kl_device *dev = target->dev;
1304 u32 padded_len;
1305 int status;
1306
1307 padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
1308
1309 if (padded_len > packet->buf_len) {
1310 ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
1311 padded_len, rx_len, packet->buf_len);
1312 return -ENOMEM;
1313 }
1314
1315 ath6kl_dbg(ATH6KL_DBG_HTC,
1316 "htc rx 0x%p hdr 0x%x len %d mbox 0x%x\n",
1317 packet, packet->info.rx.exp_hdr,
1318 padded_len, dev->ar->mbox_info.htc_addr);
1319
1320 status = hif_read_write_sync(dev->ar,
1321 dev->ar->mbox_info.htc_addr,
1322 packet->buf, padded_len,
1323 HIF_RD_SYNC_BLOCK_FIX);
1324
1325 packet->status = status;
1326
1327 return status;
1328 }
1329
1330 /*
1331 * optimization for recv packets, we can indicate a
1332 * "hint" that there are more single-packets to fetch
1333 * on this endpoint.
1334 */
ath6kl_htc_rx_set_indicate(u32 lk_ahd,struct htc_endpoint * endpoint,struct htc_packet * packet)1335 static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
1336 struct htc_endpoint *endpoint,
1337 struct htc_packet *packet)
1338 {
1339 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
1340
1341 if (htc_hdr->eid == packet->endpoint) {
1342 if (!list_empty(&endpoint->rx_bufq))
1343 packet->info.rx.indicat_flags |=
1344 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
1345 }
1346 }
1347
ath6kl_htc_rx_chk_water_mark(struct htc_endpoint * endpoint)1348 static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
1349 {
1350 struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
1351
1352 if (ep_cb.rx_refill_thresh > 0) {
1353 spin_lock_bh(&endpoint->target->rx_lock);
1354 if (get_queue_depth(&endpoint->rx_bufq)
1355 < ep_cb.rx_refill_thresh) {
1356 spin_unlock_bh(&endpoint->target->rx_lock);
1357 ep_cb.rx_refill(endpoint->target, endpoint->eid);
1358 return;
1359 }
1360 spin_unlock_bh(&endpoint->target->rx_lock);
1361 }
1362 }
1363
1364 /* This function is called with rx_lock held */
ath6kl_htc_rx_setup(struct htc_target * target,struct htc_endpoint * ep,u32 * lk_ahds,struct list_head * queue,int n_msg)1365 static int ath6kl_htc_rx_setup(struct htc_target *target,
1366 struct htc_endpoint *ep,
1367 u32 *lk_ahds, struct list_head *queue, int n_msg)
1368 {
1369 struct htc_packet *packet;
1370 /* FIXME: type of lk_ahds can't be right */
1371 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
1372 struct htc_ep_callbacks ep_cb;
1373 int status = 0, j, full_len;
1374 bool no_recycle;
1375
1376 full_len = CALC_TXRX_PADDED_LEN(target,
1377 le16_to_cpu(htc_hdr->payld_len) +
1378 sizeof(*htc_hdr));
1379
1380 if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
1381 ath6kl_warn("Rx buffer requested with invalid length htc_hdr:eid %d, flags 0x%x, len %d\n",
1382 htc_hdr->eid, htc_hdr->flags,
1383 le16_to_cpu(htc_hdr->payld_len));
1384 return -EINVAL;
1385 }
1386
1387 ep_cb = ep->ep_cb;
1388 for (j = 0; j < n_msg; j++) {
1389 /*
1390 * Reset flag, any packets allocated using the
1391 * rx_alloc() API cannot be recycled on
1392 * cleanup,they must be explicitly returned.
1393 */
1394 no_recycle = false;
1395
1396 if (ep_cb.rx_allocthresh &&
1397 (full_len > ep_cb.rx_alloc_thresh)) {
1398 ep->ep_st.rx_alloc_thresh_hit += 1;
1399 ep->ep_st.rxalloc_thresh_byte +=
1400 le16_to_cpu(htc_hdr->payld_len);
1401
1402 spin_unlock_bh(&target->rx_lock);
1403 no_recycle = true;
1404
1405 packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
1406 full_len);
1407 spin_lock_bh(&target->rx_lock);
1408 } else {
1409 /* refill handler is being used */
1410 if (list_empty(&ep->rx_bufq)) {
1411 if (ep_cb.rx_refill) {
1412 spin_unlock_bh(&target->rx_lock);
1413 ep_cb.rx_refill(ep->target, ep->eid);
1414 spin_lock_bh(&target->rx_lock);
1415 }
1416 }
1417
1418 if (list_empty(&ep->rx_bufq)) {
1419 packet = NULL;
1420 } else {
1421 packet = list_first_entry(&ep->rx_bufq,
1422 struct htc_packet, list);
1423 list_del(&packet->list);
1424 }
1425 }
1426
1427 if (!packet) {
1428 target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
1429 target->ep_waiting = ep->eid;
1430 return -ENOSPC;
1431 }
1432
1433 /* clear flags */
1434 packet->info.rx.rx_flags = 0;
1435 packet->info.rx.indicat_flags = 0;
1436 packet->status = 0;
1437
1438 if (no_recycle)
1439 /*
1440 * flag that these packets cannot be
1441 * recycled, they have to be returned to
1442 * the user
1443 */
1444 packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
1445
1446 /* Caller needs to free this upon any failure */
1447 list_add_tail(&packet->list, queue);
1448
1449 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1450 status = -ECANCELED;
1451 break;
1452 }
1453
1454 if (j) {
1455 packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
1456 packet->info.rx.exp_hdr = 0xFFFFFFFF;
1457 } else
1458 /* set expected look ahead */
1459 packet->info.rx.exp_hdr = *lk_ahds;
1460
1461 packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
1462 HTC_HDR_LENGTH;
1463 }
1464
1465 return status;
1466 }
1467
ath6kl_htc_rx_alloc(struct htc_target * target,u32 lk_ahds[],int msg,struct htc_endpoint * endpoint,struct list_head * queue)1468 static int ath6kl_htc_rx_alloc(struct htc_target *target,
1469 u32 lk_ahds[], int msg,
1470 struct htc_endpoint *endpoint,
1471 struct list_head *queue)
1472 {
1473 int status = 0;
1474 struct htc_packet *packet, *tmp_pkt;
1475 struct htc_frame_hdr *htc_hdr;
1476 int i, n_msg;
1477
1478 spin_lock_bh(&target->rx_lock);
1479
1480 for (i = 0; i < msg; i++) {
1481 htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
1482
1483 if (htc_hdr->eid >= ENDPOINT_MAX) {
1484 ath6kl_err("invalid ep in look-ahead: %d\n",
1485 htc_hdr->eid);
1486 status = -ENOMEM;
1487 break;
1488 }
1489
1490 if (htc_hdr->eid != endpoint->eid) {
1491 ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
1492 htc_hdr->eid, endpoint->eid, i);
1493 status = -ENOMEM;
1494 break;
1495 }
1496
1497 if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
1498 ath6kl_err("payload len %d exceeds max htc : %d !\n",
1499 htc_hdr->payld_len,
1500 (u32) HTC_MAX_PAYLOAD_LENGTH);
1501 status = -ENOMEM;
1502 break;
1503 }
1504
1505 if (endpoint->svc_id == 0) {
1506 ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
1507 status = -ENOMEM;
1508 break;
1509 }
1510
1511 if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
1512 /*
1513 * HTC header indicates that every packet to follow
1514 * has the same padded length so that it can be
1515 * optimally fetched as a full bundle.
1516 */
1517 n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
1518 HTC_FLG_RX_BNDL_CNT_S;
1519
1520 /* the count doesn't include the starter frame */
1521 n_msg++;
1522 if (n_msg > target->msg_per_bndl_max) {
1523 status = -ENOMEM;
1524 break;
1525 }
1526
1527 endpoint->ep_st.rx_bundle_from_hdr += 1;
1528 ath6kl_dbg(ATH6KL_DBG_HTC,
1529 "htc rx bundle pkts %d\n",
1530 n_msg);
1531 } else
1532 /* HTC header only indicates 1 message to fetch */
1533 n_msg = 1;
1534
1535 /* Setup packet buffers for each message */
1536 status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
1537 queue, n_msg);
1538
1539 /*
1540 * This is due to unavailability of buffers to rx entire data.
1541 * Return no error so that free buffers from queue can be used
1542 * to receive partial data.
1543 */
1544 if (status == -ENOSPC) {
1545 spin_unlock_bh(&target->rx_lock);
1546 return 0;
1547 }
1548
1549 if (status)
1550 break;
1551 }
1552
1553 spin_unlock_bh(&target->rx_lock);
1554
1555 if (status) {
1556 list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
1557 list_del(&packet->list);
1558 htc_reclaim_rxbuf(target, packet,
1559 &target->endpoint[packet->endpoint]);
1560 }
1561 }
1562
1563 return status;
1564 }
1565
htc_ctrl_rx(struct htc_target * context,struct htc_packet * packets)1566 static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
1567 {
1568 if (packets->endpoint != ENDPOINT_0) {
1569 WARN_ON(1);
1570 return;
1571 }
1572
1573 if (packets->status == -ECANCELED) {
1574 reclaim_rx_ctrl_buf(context, packets);
1575 return;
1576 }
1577
1578 if (packets->act_len > 0) {
1579 ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
1580 packets->act_len + HTC_HDR_LENGTH);
1581
1582 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1583 "htc rx unexpected endpoint 0 message", "",
1584 packets->buf - HTC_HDR_LENGTH,
1585 packets->act_len + HTC_HDR_LENGTH);
1586 }
1587
1588 htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
1589 }
1590
htc_proc_cred_rpt(struct htc_target * target,struct htc_credit_report * rpt,int n_entries,enum htc_endpoint_id from_ep)1591 static void htc_proc_cred_rpt(struct htc_target *target,
1592 struct htc_credit_report *rpt,
1593 int n_entries,
1594 enum htc_endpoint_id from_ep)
1595 {
1596 struct htc_endpoint *endpoint;
1597 int tot_credits = 0, i;
1598 bool dist = false;
1599
1600 spin_lock_bh(&target->tx_lock);
1601
1602 for (i = 0; i < n_entries; i++, rpt++) {
1603 if (rpt->eid >= ENDPOINT_MAX) {
1604 WARN_ON(1);
1605 spin_unlock_bh(&target->tx_lock);
1606 return;
1607 }
1608
1609 endpoint = &target->endpoint[rpt->eid];
1610
1611 ath6kl_dbg(ATH6KL_DBG_CREDIT,
1612 "credit report ep %d credits %d\n",
1613 rpt->eid, rpt->credits);
1614
1615 endpoint->ep_st.tx_cred_rpt += 1;
1616 endpoint->ep_st.cred_retnd += rpt->credits;
1617
1618 if (from_ep == rpt->eid) {
1619 /*
1620 * This credit report arrived on the same endpoint
1621 * indicating it arrived in an RX packet.
1622 */
1623 endpoint->ep_st.cred_from_rx += rpt->credits;
1624 endpoint->ep_st.cred_rpt_from_rx += 1;
1625 } else if (from_ep == ENDPOINT_0) {
1626 /* credit arrived on endpoint 0 as a NULL message */
1627 endpoint->ep_st.cred_from_ep0 += rpt->credits;
1628 endpoint->ep_st.cred_rpt_ep0 += 1;
1629 } else {
1630 endpoint->ep_st.cred_from_other += rpt->credits;
1631 endpoint->ep_st.cred_rpt_from_other += 1;
1632 }
1633
1634 if (rpt->eid == ENDPOINT_0)
1635 /* always give endpoint 0 credits back */
1636 endpoint->cred_dist.credits += rpt->credits;
1637 else {
1638 endpoint->cred_dist.cred_to_dist += rpt->credits;
1639 dist = true;
1640 }
1641
1642 /*
1643 * Refresh tx depth for distribution function that will
1644 * recover these credits NOTE: this is only valid when
1645 * there are credits to recover!
1646 */
1647 endpoint->cred_dist.txq_depth =
1648 get_queue_depth(&endpoint->txq);
1649
1650 tot_credits += rpt->credits;
1651 }
1652
1653 if (dist) {
1654 /*
1655 * This was a credit return based on a completed send
1656 * operations note, this is done with the lock held
1657 */
1658 ath6kl_credit_distribute(target->credit_info,
1659 &target->cred_dist_list,
1660 HTC_CREDIT_DIST_SEND_COMPLETE);
1661 }
1662
1663 spin_unlock_bh(&target->tx_lock);
1664
1665 if (tot_credits)
1666 htc_chk_ep_txq(target);
1667 }
1668
htc_parse_trailer(struct htc_target * target,struct htc_record_hdr * record,u8 * record_buf,u32 * next_lk_ahds,enum htc_endpoint_id endpoint,int * n_lk_ahds)1669 static int htc_parse_trailer(struct htc_target *target,
1670 struct htc_record_hdr *record,
1671 u8 *record_buf, u32 *next_lk_ahds,
1672 enum htc_endpoint_id endpoint,
1673 int *n_lk_ahds)
1674 {
1675 struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
1676 struct htc_lookahead_report *lk_ahd;
1677 int len;
1678
1679 switch (record->rec_id) {
1680 case HTC_RECORD_CREDITS:
1681 len = record->len / sizeof(struct htc_credit_report);
1682 if (!len) {
1683 WARN_ON(1);
1684 return -EINVAL;
1685 }
1686
1687 htc_proc_cred_rpt(target,
1688 (struct htc_credit_report *) record_buf,
1689 len, endpoint);
1690 break;
1691 case HTC_RECORD_LOOKAHEAD:
1692 len = record->len / sizeof(*lk_ahd);
1693 if (!len) {
1694 WARN_ON(1);
1695 return -EINVAL;
1696 }
1697
1698 lk_ahd = (struct htc_lookahead_report *) record_buf;
1699 if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF)) &&
1700 next_lk_ahds) {
1701 ath6kl_dbg(ATH6KL_DBG_HTC,
1702 "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
1703 lk_ahd->pre_valid, lk_ahd->post_valid);
1704
1705 /* look ahead bytes are valid, copy them over */
1706 memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);
1707
1708 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1709 "htc rx next look ahead",
1710 "", next_lk_ahds, 4);
1711
1712 *n_lk_ahds = 1;
1713 }
1714 break;
1715 case HTC_RECORD_LOOKAHEAD_BUNDLE:
1716 len = record->len / sizeof(*bundle_lkahd_rpt);
1717 if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
1718 WARN_ON(1);
1719 return -EINVAL;
1720 }
1721
1722 if (next_lk_ahds) {
1723 int i;
1724
1725 bundle_lkahd_rpt =
1726 (struct htc_bundle_lkahd_rpt *) record_buf;
1727
1728 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
1729 "", record_buf, record->len);
1730
1731 for (i = 0; i < len; i++) {
1732 memcpy((u8 *)&next_lk_ahds[i],
1733 bundle_lkahd_rpt->lk_ahd, 4);
1734 bundle_lkahd_rpt++;
1735 }
1736
1737 *n_lk_ahds = i;
1738 }
1739 break;
1740 default:
1741 ath6kl_err("unhandled record: id:%d len:%d\n",
1742 record->rec_id, record->len);
1743 break;
1744 }
1745
1746 return 0;
1747 }
1748
htc_proc_trailer(struct htc_target * target,u8 * buf,int len,u32 * next_lk_ahds,int * n_lk_ahds,enum htc_endpoint_id endpoint)1749 static int htc_proc_trailer(struct htc_target *target,
1750 u8 *buf, int len, u32 *next_lk_ahds,
1751 int *n_lk_ahds, enum htc_endpoint_id endpoint)
1752 {
1753 struct htc_record_hdr *record;
1754 int orig_len;
1755 int status;
1756 u8 *record_buf;
1757 u8 *orig_buf;
1758
1759 ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
1760 ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
1761
1762 orig_buf = buf;
1763 orig_len = len;
1764 status = 0;
1765
1766 while (len > 0) {
1767 if (len < sizeof(struct htc_record_hdr)) {
1768 status = -ENOMEM;
1769 break;
1770 }
1771 /* these are byte aligned structs */
1772 record = (struct htc_record_hdr *) buf;
1773 len -= sizeof(struct htc_record_hdr);
1774 buf += sizeof(struct htc_record_hdr);
1775
1776 if (record->len > len) {
1777 ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
1778 record->len, record->rec_id, len);
1779 status = -ENOMEM;
1780 break;
1781 }
1782 record_buf = buf;
1783
1784 status = htc_parse_trailer(target, record, record_buf,
1785 next_lk_ahds, endpoint, n_lk_ahds);
1786
1787 if (status)
1788 break;
1789
1790 /* advance buffer past this record for next time around */
1791 buf += record->len;
1792 len -= record->len;
1793 }
1794
1795 if (status)
1796 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
1797 "", orig_buf, orig_len);
1798
1799 return status;
1800 }
1801
ath6kl_htc_rx_process_hdr(struct htc_target * target,struct htc_packet * packet,u32 * next_lkahds,int * n_lkahds)1802 static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
1803 struct htc_packet *packet,
1804 u32 *next_lkahds, int *n_lkahds)
1805 {
1806 int status = 0;
1807 u16 payload_len;
1808 u32 lk_ahd;
1809 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
1810
1811 if (n_lkahds != NULL)
1812 *n_lkahds = 0;
1813
1814 /*
1815 * NOTE: we cannot assume the alignment of buf, so we use the safe
1816 * macros to retrieve 16 bit fields.
1817 */
1818 payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
1819
1820 memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
1821
1822 if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
1823 /*
1824 * Refresh the expected header and the actual length as it
1825 * was unknown when this packet was grabbed as part of the
1826 * bundle.
1827 */
1828 packet->info.rx.exp_hdr = lk_ahd;
1829 packet->act_len = payload_len + HTC_HDR_LENGTH;
1830
1831 /* validate the actual header that was refreshed */
1832 if (packet->act_len > packet->buf_len) {
1833 ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
1834 payload_len, lk_ahd);
1835 /*
1836 * Limit this to max buffer just to print out some
1837 * of the buffer.
1838 */
1839 packet->act_len = min(packet->act_len, packet->buf_len);
1840 status = -ENOMEM;
1841 goto fail_rx;
1842 }
1843
1844 if (packet->endpoint != htc_hdr->eid) {
1845 ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
1846 htc_hdr->eid, packet->endpoint);
1847 status = -ENOMEM;
1848 goto fail_rx;
1849 }
1850 }
1851
1852 if (lk_ahd != packet->info.rx.exp_hdr) {
1853 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1854 __func__, packet, packet->info.rx.rx_flags);
1855 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
1856 "", &packet->info.rx.exp_hdr, 4);
1857 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
1858 "", (u8 *)&lk_ahd, sizeof(lk_ahd));
1859 status = -ENOMEM;
1860 goto fail_rx;
1861 }
1862
1863 if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
1864 if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
1865 htc_hdr->ctrl[0] > payload_len) {
1866 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1867 __func__, payload_len, htc_hdr->ctrl[0]);
1868 status = -ENOMEM;
1869 goto fail_rx;
1870 }
1871
1872 if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
1873 next_lkahds = NULL;
1874 n_lkahds = NULL;
1875 }
1876
1877 status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
1878 + payload_len - htc_hdr->ctrl[0],
1879 htc_hdr->ctrl[0], next_lkahds,
1880 n_lkahds, packet->endpoint);
1881
1882 if (status)
1883 goto fail_rx;
1884
1885 packet->act_len -= htc_hdr->ctrl[0];
1886 }
1887
1888 packet->buf += HTC_HDR_LENGTH;
1889 packet->act_len -= HTC_HDR_LENGTH;
1890
1891 fail_rx:
1892 if (status)
1893 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1894 "", packet->buf, packet->act_len);
1895
1896 return status;
1897 }
1898
ath6kl_htc_rx_complete(struct htc_endpoint * endpoint,struct htc_packet * packet)1899 static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1900 struct htc_packet *packet)
1901 {
1902 ath6kl_dbg(ATH6KL_DBG_HTC,
1903 "htc rx complete ep %d packet 0x%p\n",
1904 endpoint->eid, packet);
1905
1906 endpoint->ep_cb.rx(endpoint->target, packet);
1907 }
1908
ath6kl_htc_rx_bundle(struct htc_target * target,struct list_head * rxq,struct list_head * sync_compq,int * n_pkt_fetched,bool part_bundle)1909 static int ath6kl_htc_rx_bundle(struct htc_target *target,
1910 struct list_head *rxq,
1911 struct list_head *sync_compq,
1912 int *n_pkt_fetched, bool part_bundle)
1913 {
1914 struct hif_scatter_req *scat_req;
1915 struct htc_packet *packet;
1916 int rem_space = target->max_rx_bndl_sz;
1917 int n_scat_pkt, status = 0, i, len;
1918
1919 n_scat_pkt = get_queue_depth(rxq);
1920 n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
1921
1922 if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
1923 /*
1924 * We were forced to split this bundle receive operation
1925 * all packets in this partial bundle must have their
1926 * lookaheads ignored.
1927 */
1928 part_bundle = true;
1929
1930 /*
1931 * This would only happen if the target ignored our max
1932 * bundle limit.
1933 */
1934 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1935 __func__, get_queue_depth(rxq), n_scat_pkt);
1936 }
1937
1938 len = 0;
1939
1940 ath6kl_dbg(ATH6KL_DBG_HTC,
1941 "htc rx bundle depth %d pkts %d\n",
1942 get_queue_depth(rxq), n_scat_pkt);
1943
1944 scat_req = hif_scatter_req_get(target->dev->ar);
1945
1946 if (scat_req == NULL)
1947 goto fail_rx_pkt;
1948
1949 for (i = 0; i < n_scat_pkt; i++) {
1950 int pad_len;
1951
1952 packet = list_first_entry(rxq, struct htc_packet, list);
1953 list_del(&packet->list);
1954
1955 pad_len = CALC_TXRX_PADDED_LEN(target,
1956 packet->act_len);
1957
1958 if ((rem_space - pad_len) < 0) {
1959 list_add(&packet->list, rxq);
1960 break;
1961 }
1962
1963 rem_space -= pad_len;
1964
1965 if (part_bundle || (i < (n_scat_pkt - 1)))
1966 /*
1967 * Packet 0..n-1 cannot be checked for look-aheads
1968 * since we are fetching a bundle the last packet
1969 * however can have it's lookahead used
1970 */
1971 packet->info.rx.rx_flags |=
1972 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1973
1974 /* NOTE: 1 HTC packet per scatter entry */
1975 scat_req->scat_list[i].buf = packet->buf;
1976 scat_req->scat_list[i].len = pad_len;
1977
1978 packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
1979
1980 list_add_tail(&packet->list, sync_compq);
1981
1982 WARN_ON(!scat_req->scat_list[i].len);
1983 len += scat_req->scat_list[i].len;
1984 }
1985
1986 scat_req->len = len;
1987 scat_req->scat_entries = i;
1988
1989 status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
1990
1991 if (!status)
1992 *n_pkt_fetched = i;
1993
1994 /* free scatter request */
1995 hif_scatter_req_add(target->dev->ar, scat_req);
1996
1997 fail_rx_pkt:
1998
1999 return status;
2000 }
2001
ath6kl_htc_rx_process_packets(struct htc_target * target,struct list_head * comp_pktq,u32 lk_ahds[],int * n_lk_ahd)2002 static int ath6kl_htc_rx_process_packets(struct htc_target *target,
2003 struct list_head *comp_pktq,
2004 u32 lk_ahds[],
2005 int *n_lk_ahd)
2006 {
2007 struct htc_packet *packet, *tmp_pkt;
2008 struct htc_endpoint *ep;
2009 int status = 0;
2010
2011 list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
2012 ep = &target->endpoint[packet->endpoint];
2013
2014 trace_ath6kl_htc_rx(packet->status, packet->endpoint,
2015 packet->buf, packet->act_len);
2016
2017 /* process header for each of the recv packet */
2018 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
2019 n_lk_ahd);
2020 if (status)
2021 return status;
2022
2023 list_del(&packet->list);
2024
2025 if (list_empty(comp_pktq)) {
2026 /*
2027 * Last packet's more packet flag is set
2028 * based on the lookahead.
2029 */
2030 if (*n_lk_ahd > 0)
2031 ath6kl_htc_rx_set_indicate(lk_ahds[0],
2032 ep, packet);
2033 } else
2034 /*
2035 * Packets in a bundle automatically have
2036 * this flag set.
2037 */
2038 packet->info.rx.indicat_flags |=
2039 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
2040
2041 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
2042
2043 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
2044 ep->ep_st.rx_bundl += 1;
2045
2046 ath6kl_htc_rx_complete(ep, packet);
2047 }
2048
2049 return status;
2050 }
2051
ath6kl_htc_rx_fetch(struct htc_target * target,struct list_head * rx_pktq,struct list_head * comp_pktq)2052 static int ath6kl_htc_rx_fetch(struct htc_target *target,
2053 struct list_head *rx_pktq,
2054 struct list_head *comp_pktq)
2055 {
2056 int fetched_pkts;
2057 bool part_bundle = false;
2058 int status = 0;
2059 struct list_head tmp_rxq;
2060 struct htc_packet *packet, *tmp_pkt;
2061
2062 /* now go fetch the list of HTC packets */
2063 while (!list_empty(rx_pktq)) {
2064 fetched_pkts = 0;
2065
2066 INIT_LIST_HEAD(&tmp_rxq);
2067
2068 if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
2069 /*
2070 * There are enough packets to attempt a
2071 * bundle transfer and recv bundling is
2072 * allowed.
2073 */
2074 status = ath6kl_htc_rx_bundle(target, rx_pktq,
2075 &tmp_rxq,
2076 &fetched_pkts,
2077 part_bundle);
2078 if (status)
2079 goto fail_rx;
2080
2081 if (!list_empty(rx_pktq))
2082 part_bundle = true;
2083
2084 list_splice_tail_init(&tmp_rxq, comp_pktq);
2085 }
2086
2087 if (!fetched_pkts) {
2088 packet = list_first_entry(rx_pktq, struct htc_packet,
2089 list);
2090
2091 /* fully synchronous */
2092 packet->completion = NULL;
2093
2094 if (!list_is_singular(rx_pktq))
2095 /*
2096 * look_aheads in all packet
2097 * except the last one in the
2098 * bundle must be ignored
2099 */
2100 packet->info.rx.rx_flags |=
2101 HTC_RX_PKT_IGNORE_LOOKAHEAD;
2102
2103 /* go fetch the packet */
2104 status = ath6kl_htc_rx_packet(target, packet,
2105 packet->act_len);
2106
2107 list_move_tail(&packet->list, &tmp_rxq);
2108
2109 if (status)
2110 goto fail_rx;
2111
2112 list_splice_tail_init(&tmp_rxq, comp_pktq);
2113 }
2114 }
2115
2116 return 0;
2117
2118 fail_rx:
2119
2120 /*
2121 * Cleanup any packets we allocated but didn't use to
2122 * actually fetch any packets.
2123 */
2124
2125 list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
2126 list_del(&packet->list);
2127 htc_reclaim_rxbuf(target, packet,
2128 &target->endpoint[packet->endpoint]);
2129 }
2130
2131 list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
2132 list_del(&packet->list);
2133 htc_reclaim_rxbuf(target, packet,
2134 &target->endpoint[packet->endpoint]);
2135 }
2136
2137 return status;
2138 }
2139
ath6kl_htc_rxmsg_pending_handler(struct htc_target * target,u32 msg_look_ahead,int * num_pkts)2140 int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
2141 u32 msg_look_ahead, int *num_pkts)
2142 {
2143 struct htc_packet *packets, *tmp_pkt;
2144 struct htc_endpoint *endpoint;
2145 struct list_head rx_pktq, comp_pktq;
2146 int status = 0;
2147 u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
2148 int num_look_ahead = 1;
2149 enum htc_endpoint_id id;
2150 int n_fetched = 0;
2151
2152 INIT_LIST_HEAD(&comp_pktq);
2153 *num_pkts = 0;
2154
2155 /*
2156 * On first entry copy the look_aheads into our temp array for
2157 * processing
2158 */
2159 look_aheads[0] = msg_look_ahead;
2160
2161 while (true) {
2162 /*
2163 * First lookahead sets the expected endpoint IDs for all
2164 * packets in a bundle.
2165 */
2166 id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
2167 endpoint = &target->endpoint[id];
2168
2169 if (id >= ENDPOINT_MAX) {
2170 ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
2171 id);
2172 status = -ENOMEM;
2173 break;
2174 }
2175
2176 INIT_LIST_HEAD(&rx_pktq);
2177 INIT_LIST_HEAD(&comp_pktq);
2178
2179 /*
2180 * Try to allocate as many HTC RX packets indicated by the
2181 * look_aheads.
2182 */
2183 status = ath6kl_htc_rx_alloc(target, look_aheads,
2184 num_look_ahead, endpoint,
2185 &rx_pktq);
2186 if (status)
2187 break;
2188
2189 if (get_queue_depth(&rx_pktq) >= 2)
2190 /*
2191 * A recv bundle was detected, force IRQ status
2192 * re-check again
2193 */
2194 target->chk_irq_status_cnt = 1;
2195
2196 n_fetched += get_queue_depth(&rx_pktq);
2197
2198 num_look_ahead = 0;
2199
2200 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
2201
2202 if (!status)
2203 ath6kl_htc_rx_chk_water_mark(endpoint);
2204
2205 /* Process fetched packets */
2206 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
2207 look_aheads,
2208 &num_look_ahead);
2209
2210 if (!num_look_ahead || status)
2211 break;
2212
2213 /*
2214 * For SYNCH processing, if we get here, we are running
2215 * through the loop again due to a detected lookahead. Set
2216 * flag that we should re-check IRQ status registers again
2217 * before leaving IRQ processing, this can net better
2218 * performance in high throughput situations.
2219 */
2220 target->chk_irq_status_cnt = 1;
2221 }
2222
2223 if (status) {
2224 if (status != -ECANCELED)
2225 ath6kl_err("failed to get pending recv messages: %d\n",
2226 status);
2227
2228 /* cleanup any packets in sync completion queue */
2229 list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
2230 list_del(&packets->list);
2231 htc_reclaim_rxbuf(target, packets,
2232 &target->endpoint[packets->endpoint]);
2233 }
2234
2235 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2236 ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
2237 ath6kl_hif_rx_control(target->dev, false);
2238 }
2239 }
2240
2241 /*
2242 * Before leaving, check to see if host ran out of buffers and
2243 * needs to stop the receiver.
2244 */
2245 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
2246 ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
2247 ath6kl_hif_rx_control(target->dev, false);
2248 }
2249 *num_pkts = n_fetched;
2250
2251 return status;
2252 }
2253
2254 /*
2255 * Synchronously wait for a control message from the target,
2256 * This function is used at initialization time ONLY. At init messages
2257 * on ENDPOINT 0 are expected.
2258 */
htc_wait_for_ctrl_msg(struct htc_target * target)2259 static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
2260 {
2261 struct htc_packet *packet = NULL;
2262 struct htc_frame_look_ahead look_ahead;
2263
2264 if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead.word,
2265 HTC_TARGET_RESPONSE_TIMEOUT))
2266 return NULL;
2267
2268 ath6kl_dbg(ATH6KL_DBG_HTC,
2269 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead.word);
2270
2271 if (look_ahead.eid != ENDPOINT_0)
2272 return NULL;
2273
2274 packet = htc_get_control_buf(target, false);
2275
2276 if (!packet)
2277 return NULL;
2278
2279 packet->info.rx.rx_flags = 0;
2280 packet->info.rx.exp_hdr = look_ahead.word;
2281 packet->act_len = le16_to_cpu(look_ahead.payld_len) + HTC_HDR_LENGTH;
2282
2283 if (packet->act_len > packet->buf_len)
2284 goto fail_ctrl_rx;
2285
2286 /* we want synchronous operation */
2287 packet->completion = NULL;
2288
2289 /* get the message from the device, this will block */
2290 if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
2291 goto fail_ctrl_rx;
2292
2293 trace_ath6kl_htc_rx(packet->status, packet->endpoint,
2294 packet->buf, packet->act_len);
2295
2296 /* process receive header */
2297 packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
2298
2299 if (packet->status) {
2300 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
2301 packet->status);
2302 goto fail_ctrl_rx;
2303 }
2304
2305 return packet;
2306
2307 fail_ctrl_rx:
2308 if (packet != NULL) {
2309 htc_rxpkt_reset(packet);
2310 reclaim_rx_ctrl_buf(target, packet);
2311 }
2312
2313 return NULL;
2314 }
2315
ath6kl_htc_mbox_add_rxbuf_multiple(struct htc_target * target,struct list_head * pkt_queue)2316 static int ath6kl_htc_mbox_add_rxbuf_multiple(struct htc_target *target,
2317 struct list_head *pkt_queue)
2318 {
2319 struct htc_endpoint *endpoint;
2320 struct htc_packet *first_pkt;
2321 bool rx_unblock = false;
2322 int status = 0, depth;
2323
2324 if (list_empty(pkt_queue))
2325 return -ENOMEM;
2326
2327 first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
2328
2329 if (first_pkt->endpoint >= ENDPOINT_MAX)
2330 return status;
2331
2332 depth = get_queue_depth(pkt_queue);
2333
2334 ath6kl_dbg(ATH6KL_DBG_HTC,
2335 "htc rx add multiple ep id %d cnt %d len %d\n",
2336 first_pkt->endpoint, depth, first_pkt->buf_len);
2337
2338 endpoint = &target->endpoint[first_pkt->endpoint];
2339
2340 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
2341 struct htc_packet *packet, *tmp_pkt;
2342
2343 /* walk through queue and mark each one canceled */
2344 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
2345 packet->status = -ECANCELED;
2346 list_del(&packet->list);
2347 ath6kl_htc_rx_complete(endpoint, packet);
2348 }
2349
2350 return status;
2351 }
2352
2353 spin_lock_bh(&target->rx_lock);
2354
2355 list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
2356
2357 /* check if we are blocked waiting for a new buffer */
2358 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
2359 if (target->ep_waiting == first_pkt->endpoint) {
2360 ath6kl_dbg(ATH6KL_DBG_HTC,
2361 "htc rx blocked on ep %d, unblocking\n",
2362 target->ep_waiting);
2363 target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
2364 target->ep_waiting = ENDPOINT_MAX;
2365 rx_unblock = true;
2366 }
2367 }
2368
2369 spin_unlock_bh(&target->rx_lock);
2370
2371 if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
2372 /* TODO : implement a buffer threshold count? */
2373 ath6kl_hif_rx_control(target->dev, true);
2374
2375 return status;
2376 }
2377
ath6kl_htc_mbox_flush_rx_buf(struct htc_target * target)2378 static void ath6kl_htc_mbox_flush_rx_buf(struct htc_target *target)
2379 {
2380 struct htc_endpoint *endpoint;
2381 struct htc_packet *packet, *tmp_pkt;
2382 int i;
2383
2384 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2385 endpoint = &target->endpoint[i];
2386 if (!endpoint->svc_id)
2387 /* not in use.. */
2388 continue;
2389
2390 spin_lock_bh(&target->rx_lock);
2391 list_for_each_entry_safe(packet, tmp_pkt,
2392 &endpoint->rx_bufq, list) {
2393 list_del(&packet->list);
2394 spin_unlock_bh(&target->rx_lock);
2395 ath6kl_dbg(ATH6KL_DBG_HTC,
2396 "htc rx flush pkt 0x%p len %d ep %d\n",
2397 packet, packet->buf_len,
2398 packet->endpoint);
2399 /*
2400 * packets in rx_bufq of endpoint 0 have originally
2401 * been queued from target->free_ctrl_rxbuf where
2402 * packet and packet->buf_start are allocated
2403 * separately using kmalloc(). For other endpoint
2404 * rx_bufq, it is allocated as skb where packet is
2405 * skb->head. Take care of this difference while freeing
2406 * the memory.
2407 */
2408 if (packet->endpoint == ENDPOINT_0) {
2409 kfree(packet->buf_start);
2410 kfree(packet);
2411 } else {
2412 dev_kfree_skb(packet->pkt_cntxt);
2413 }
2414 spin_lock_bh(&target->rx_lock);
2415 }
2416 spin_unlock_bh(&target->rx_lock);
2417 }
2418 }
2419
ath6kl_htc_mbox_conn_service(struct htc_target * target,struct htc_service_connect_req * conn_req,struct htc_service_connect_resp * conn_resp)2420 static int ath6kl_htc_mbox_conn_service(struct htc_target *target,
2421 struct htc_service_connect_req *conn_req,
2422 struct htc_service_connect_resp *conn_resp)
2423 {
2424 struct htc_packet *rx_pkt = NULL;
2425 struct htc_packet *tx_pkt = NULL;
2426 struct htc_conn_service_resp *resp_msg;
2427 struct htc_conn_service_msg *conn_msg;
2428 struct htc_endpoint *endpoint;
2429 enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
2430 unsigned int max_msg_sz = 0;
2431 int status = 0;
2432 u16 msg_id;
2433
2434 ath6kl_dbg(ATH6KL_DBG_HTC,
2435 "htc connect service target 0x%p service id 0x%x\n",
2436 target, conn_req->svc_id);
2437
2438 if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
2439 /* special case for pseudo control service */
2440 assigned_ep = ENDPOINT_0;
2441 max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
2442 } else {
2443 /* allocate a packet to send to the target */
2444 tx_pkt = htc_get_control_buf(target, true);
2445
2446 if (!tx_pkt)
2447 return -ENOMEM;
2448
2449 conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
2450 memset(conn_msg, 0, sizeof(*conn_msg));
2451 conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
2452 conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
2453 conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
2454
2455 set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
2456 sizeof(*conn_msg) + conn_msg->svc_meta_len,
2457 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
2458
2459 /* we want synchronous operation */
2460 tx_pkt->completion = NULL;
2461 ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
2462 status = ath6kl_htc_tx_issue(target, tx_pkt);
2463
2464 if (status)
2465 goto fail_tx;
2466
2467 /* wait for response */
2468 rx_pkt = htc_wait_for_ctrl_msg(target);
2469
2470 if (!rx_pkt) {
2471 status = -ENOMEM;
2472 goto fail_tx;
2473 }
2474
2475 resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
2476 msg_id = le16_to_cpu(resp_msg->msg_id);
2477
2478 if ((msg_id != HTC_MSG_CONN_SVC_RESP_ID) ||
2479 (rx_pkt->act_len < sizeof(*resp_msg))) {
2480 status = -ENOMEM;
2481 goto fail_tx;
2482 }
2483
2484 conn_resp->resp_code = resp_msg->status;
2485 /* check response status */
2486 if (resp_msg->status != HTC_SERVICE_SUCCESS) {
2487 ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
2488 resp_msg->svc_id, resp_msg->status);
2489 status = -ENOMEM;
2490 goto fail_tx;
2491 }
2492
2493 assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
2494 max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
2495 }
2496
2497 if (WARN_ON_ONCE(assigned_ep == ENDPOINT_UNUSED ||
2498 assigned_ep >= ENDPOINT_MAX || !max_msg_sz)) {
2499 status = -ENOMEM;
2500 goto fail_tx;
2501 }
2502
2503 endpoint = &target->endpoint[assigned_ep];
2504 endpoint->eid = assigned_ep;
2505 if (endpoint->svc_id) {
2506 status = -ENOMEM;
2507 goto fail_tx;
2508 }
2509
2510 /* return assigned endpoint to caller */
2511 conn_resp->endpoint = assigned_ep;
2512 conn_resp->len_max = max_msg_sz;
2513
2514 /* setup the endpoint */
2515
2516 /* this marks the endpoint in use */
2517 endpoint->svc_id = conn_req->svc_id;
2518
2519 endpoint->max_txq_depth = conn_req->max_txq_depth;
2520 endpoint->len_max = max_msg_sz;
2521 endpoint->ep_cb = conn_req->ep_cb;
2522 endpoint->cred_dist.svc_id = conn_req->svc_id;
2523 endpoint->cred_dist.htc_ep = endpoint;
2524 endpoint->cred_dist.endpoint = assigned_ep;
2525 endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
2526
2527 switch (endpoint->svc_id) {
2528 case WMI_DATA_BK_SVC:
2529 endpoint->tx_drop_packet_threshold = MAX_DEF_COOKIE_NUM / 3;
2530 break;
2531 default:
2532 endpoint->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
2533 break;
2534 }
2535
2536 if (conn_req->max_rxmsg_sz) {
2537 /*
2538 * Override cred_per_msg calculation, this optimizes
2539 * the credit-low indications since the host will actually
2540 * issue smaller messages in the Send path.
2541 */
2542 if (conn_req->max_rxmsg_sz > max_msg_sz) {
2543 status = -ENOMEM;
2544 goto fail_tx;
2545 }
2546 endpoint->cred_dist.cred_per_msg =
2547 conn_req->max_rxmsg_sz / target->tgt_cred_sz;
2548 } else
2549 endpoint->cred_dist.cred_per_msg =
2550 max_msg_sz / target->tgt_cred_sz;
2551
2552 if (!endpoint->cred_dist.cred_per_msg)
2553 endpoint->cred_dist.cred_per_msg = 1;
2554
2555 /* save local connection flags */
2556 endpoint->conn_flags = conn_req->flags;
2557
2558 fail_tx:
2559 if (tx_pkt)
2560 htc_reclaim_txctrl_buf(target, tx_pkt);
2561
2562 if (rx_pkt) {
2563 htc_rxpkt_reset(rx_pkt);
2564 reclaim_rx_ctrl_buf(target, rx_pkt);
2565 }
2566
2567 return status;
2568 }
2569
reset_ep_state(struct htc_target * target)2570 static void reset_ep_state(struct htc_target *target)
2571 {
2572 struct htc_endpoint *endpoint;
2573 int i;
2574
2575 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2576 endpoint = &target->endpoint[i];
2577 memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
2578 endpoint->svc_id = 0;
2579 endpoint->len_max = 0;
2580 endpoint->max_txq_depth = 0;
2581 memset(&endpoint->ep_st, 0,
2582 sizeof(endpoint->ep_st));
2583 INIT_LIST_HEAD(&endpoint->rx_bufq);
2584 INIT_LIST_HEAD(&endpoint->txq);
2585 endpoint->target = target;
2586 }
2587
2588 /* reset distribution list */
2589 /* FIXME: free existing entries */
2590 INIT_LIST_HEAD(&target->cred_dist_list);
2591 }
2592
ath6kl_htc_mbox_get_rxbuf_num(struct htc_target * target,enum htc_endpoint_id endpoint)2593 static int ath6kl_htc_mbox_get_rxbuf_num(struct htc_target *target,
2594 enum htc_endpoint_id endpoint)
2595 {
2596 int num;
2597
2598 spin_lock_bh(&target->rx_lock);
2599 num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
2600 spin_unlock_bh(&target->rx_lock);
2601 return num;
2602 }
2603
htc_setup_msg_bndl(struct htc_target * target)2604 static void htc_setup_msg_bndl(struct htc_target *target)
2605 {
2606 /* limit what HTC can handle */
2607 target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
2608 target->msg_per_bndl_max);
2609
2610 if (ath6kl_hif_enable_scatter(target->dev->ar)) {
2611 target->msg_per_bndl_max = 0;
2612 return;
2613 }
2614
2615 /* limit bundle what the device layer can handle */
2616 target->msg_per_bndl_max = min(target->max_scat_entries,
2617 target->msg_per_bndl_max);
2618
2619 ath6kl_dbg(ATH6KL_DBG_BOOT,
2620 "htc bundling allowed msg_per_bndl_max %d\n",
2621 target->msg_per_bndl_max);
2622
2623 /* Max rx bundle size is limited by the max tx bundle size */
2624 target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
2625 /* Max tx bundle size if limited by the extended mbox address range */
2626 target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
2627 target->max_xfer_szper_scatreq);
2628
2629 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
2630 target->max_rx_bndl_sz, target->max_tx_bndl_sz);
2631
2632 if (target->max_tx_bndl_sz)
2633 /* tx_bndl_mask is enabled per AC, each has 1 bit */
2634 target->tx_bndl_mask = (1 << WMM_NUM_AC) - 1;
2635
2636 if (target->max_rx_bndl_sz)
2637 target->rx_bndl_enable = true;
2638
2639 if ((target->tgt_cred_sz % target->block_sz) != 0) {
2640 ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
2641 target->tgt_cred_sz);
2642
2643 /*
2644 * Disallow send bundling since the credit size is
2645 * not aligned to a block size the I/O block
2646 * padding will spill into the next credit buffer
2647 * which is fatal.
2648 */
2649 target->tx_bndl_mask = 0;
2650 }
2651 }
2652
ath6kl_htc_mbox_wait_target(struct htc_target * target)2653 static int ath6kl_htc_mbox_wait_target(struct htc_target *target)
2654 {
2655 struct htc_packet *packet = NULL;
2656 struct htc_ready_ext_msg *rdy_msg;
2657 struct htc_service_connect_req connect;
2658 struct htc_service_connect_resp resp;
2659 int status;
2660
2661 /* we should be getting 1 control message that the target is ready */
2662 packet = htc_wait_for_ctrl_msg(target);
2663
2664 if (!packet)
2665 return -ENOMEM;
2666
2667 /* we controlled the buffer creation so it's properly aligned */
2668 rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
2669
2670 if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
2671 (packet->act_len < sizeof(struct htc_ready_msg))) {
2672 status = -ENOMEM;
2673 goto fail_wait_target;
2674 }
2675
2676 if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
2677 status = -ENOMEM;
2678 goto fail_wait_target;
2679 }
2680
2681 target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
2682 target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
2683
2684 ath6kl_dbg(ATH6KL_DBG_BOOT,
2685 "htc target ready credits %d size %d\n",
2686 target->tgt_creds, target->tgt_cred_sz);
2687
2688 /* check if this is an extended ready message */
2689 if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
2690 /* this is an extended message */
2691 target->htc_tgt_ver = rdy_msg->htc_ver;
2692 target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
2693 } else {
2694 /* legacy */
2695 target->htc_tgt_ver = HTC_VERSION_2P0;
2696 target->msg_per_bndl_max = 0;
2697 }
2698
2699 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc using protocol %s (%d)\n",
2700 (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
2701 target->htc_tgt_ver);
2702
2703 if (target->msg_per_bndl_max > 0)
2704 htc_setup_msg_bndl(target);
2705
2706 /* setup our pseudo HTC control endpoint connection */
2707 memset(&connect, 0, sizeof(connect));
2708 memset(&resp, 0, sizeof(resp));
2709 connect.ep_cb.rx = htc_ctrl_rx;
2710 connect.ep_cb.rx_refill = NULL;
2711 connect.ep_cb.tx_full = NULL;
2712 connect.max_txq_depth = NUM_CONTROL_BUFFERS;
2713 connect.svc_id = HTC_CTRL_RSVD_SVC;
2714
2715 /* connect fake service */
2716 status = ath6kl_htc_mbox_conn_service((void *)target, &connect, &resp);
2717
2718 if (status)
2719 /*
2720 * FIXME: this call doesn't make sense, the caller should
2721 * call ath6kl_htc_mbox_cleanup() when it wants remove htc
2722 */
2723 ath6kl_hif_cleanup_scatter(target->dev->ar);
2724
2725 fail_wait_target:
2726 if (packet) {
2727 htc_rxpkt_reset(packet);
2728 reclaim_rx_ctrl_buf(target, packet);
2729 }
2730
2731 return status;
2732 }
2733
2734 /*
2735 * Start HTC, enable interrupts and let the target know
2736 * host has finished setup.
2737 */
ath6kl_htc_mbox_start(struct htc_target * target)2738 static int ath6kl_htc_mbox_start(struct htc_target *target)
2739 {
2740 struct htc_packet *packet;
2741 int status;
2742
2743 memset(&target->dev->irq_proc_reg, 0,
2744 sizeof(target->dev->irq_proc_reg));
2745
2746 /* Disable interrupts at the chip level */
2747 ath6kl_hif_disable_intrs(target->dev);
2748
2749 target->htc_flags = 0;
2750 target->rx_st_flags = 0;
2751
2752 /* Push control receive buffers into htc control endpoint */
2753 while ((packet = htc_get_control_buf(target, false)) != NULL) {
2754 status = htc_add_rxbuf(target, packet);
2755 if (status)
2756 return status;
2757 }
2758
2759 /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2760 ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
2761 target->tgt_creds);
2762
2763 dump_cred_dist_stats(target);
2764
2765 /* Indicate to the target of the setup completion */
2766 status = htc_setup_tx_complete(target);
2767
2768 if (status)
2769 return status;
2770
2771 /* unmask interrupts */
2772 status = ath6kl_hif_unmask_intrs(target->dev);
2773
2774 if (status)
2775 ath6kl_htc_mbox_stop(target);
2776
2777 return status;
2778 }
2779
ath6kl_htc_reset(struct htc_target * target)2780 static int ath6kl_htc_reset(struct htc_target *target)
2781 {
2782 u32 block_size, ctrl_bufsz;
2783 struct htc_packet *packet;
2784 int i;
2785
2786 reset_ep_state(target);
2787
2788 block_size = target->dev->ar->mbox_info.block_size;
2789
2790 ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
2791 (block_size + HTC_HDR_LENGTH) :
2792 (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
2793
2794 for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
2795 packet = kzalloc(sizeof(*packet), GFP_KERNEL);
2796 if (!packet)
2797 return -ENOMEM;
2798
2799 packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
2800 if (!packet->buf_start) {
2801 kfree(packet);
2802 return -ENOMEM;
2803 }
2804
2805 packet->buf_len = ctrl_bufsz;
2806 if (i < NUM_CONTROL_RX_BUFFERS) {
2807 packet->act_len = 0;
2808 packet->buf = packet->buf_start;
2809 packet->endpoint = ENDPOINT_0;
2810 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
2811 } else {
2812 list_add_tail(&packet->list, &target->free_ctrl_txbuf);
2813 }
2814 }
2815
2816 return 0;
2817 }
2818
2819 /* htc_stop: stop interrupt reception, and flush all queued buffers */
ath6kl_htc_mbox_stop(struct htc_target * target)2820 static void ath6kl_htc_mbox_stop(struct htc_target *target)
2821 {
2822 spin_lock_bh(&target->htc_lock);
2823 target->htc_flags |= HTC_OP_STATE_STOPPING;
2824 spin_unlock_bh(&target->htc_lock);
2825
2826 /*
2827 * Masking interrupts is a synchronous operation, when this
2828 * function returns all pending HIF I/O has completed, we can
2829 * safely flush the queues.
2830 */
2831 ath6kl_hif_mask_intrs(target->dev);
2832
2833 ath6kl_htc_flush_txep_all(target);
2834
2835 ath6kl_htc_mbox_flush_rx_buf(target);
2836
2837 ath6kl_htc_reset(target);
2838 }
2839
ath6kl_htc_mbox_create(struct ath6kl * ar)2840 static void *ath6kl_htc_mbox_create(struct ath6kl *ar)
2841 {
2842 struct htc_target *target = NULL;
2843 int status = 0;
2844
2845 target = kzalloc(sizeof(*target), GFP_KERNEL);
2846 if (!target) {
2847 ath6kl_err("unable to allocate memory\n");
2848 return NULL;
2849 }
2850
2851 target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
2852 if (!target->dev) {
2853 ath6kl_err("unable to allocate memory\n");
2854 kfree(target);
2855 return NULL;
2856 }
2857
2858 spin_lock_init(&target->htc_lock);
2859 spin_lock_init(&target->rx_lock);
2860 spin_lock_init(&target->tx_lock);
2861
2862 INIT_LIST_HEAD(&target->free_ctrl_txbuf);
2863 INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
2864 INIT_LIST_HEAD(&target->cred_dist_list);
2865
2866 target->dev->ar = ar;
2867 target->dev->htc_cnxt = target;
2868 target->ep_waiting = ENDPOINT_MAX;
2869
2870 status = ath6kl_hif_setup(target->dev);
2871 if (status)
2872 goto err_htc_cleanup;
2873
2874 status = ath6kl_htc_reset(target);
2875 if (status)
2876 goto err_htc_cleanup;
2877
2878 return target;
2879
2880 err_htc_cleanup:
2881 ath6kl_htc_mbox_cleanup(target);
2882
2883 return NULL;
2884 }
2885
2886 /* cleanup the HTC instance */
ath6kl_htc_mbox_cleanup(struct htc_target * target)2887 static void ath6kl_htc_mbox_cleanup(struct htc_target *target)
2888 {
2889 struct htc_packet *packet, *tmp_packet;
2890
2891 ath6kl_hif_cleanup_scatter(target->dev->ar);
2892
2893 list_for_each_entry_safe(packet, tmp_packet,
2894 &target->free_ctrl_txbuf, list) {
2895 list_del(&packet->list);
2896 kfree(packet->buf_start);
2897 kfree(packet);
2898 }
2899
2900 list_for_each_entry_safe(packet, tmp_packet,
2901 &target->free_ctrl_rxbuf, list) {
2902 list_del(&packet->list);
2903 kfree(packet->buf_start);
2904 kfree(packet);
2905 }
2906
2907 kfree(target->dev);
2908 kfree(target);
2909 }
2910
2911 static const struct ath6kl_htc_ops ath6kl_htc_mbox_ops = {
2912 .create = ath6kl_htc_mbox_create,
2913 .wait_target = ath6kl_htc_mbox_wait_target,
2914 .start = ath6kl_htc_mbox_start,
2915 .conn_service = ath6kl_htc_mbox_conn_service,
2916 .tx = ath6kl_htc_mbox_tx,
2917 .stop = ath6kl_htc_mbox_stop,
2918 .cleanup = ath6kl_htc_mbox_cleanup,
2919 .flush_txep = ath6kl_htc_mbox_flush_txep,
2920 .flush_rx_buf = ath6kl_htc_mbox_flush_rx_buf,
2921 .activity_changed = ath6kl_htc_mbox_activity_changed,
2922 .get_rxbuf_num = ath6kl_htc_mbox_get_rxbuf_num,
2923 .add_rxbuf_multiple = ath6kl_htc_mbox_add_rxbuf_multiple,
2924 .credit_setup = ath6kl_htc_mbox_credit_setup,
2925 };
2926
ath6kl_htc_mbox_attach(struct ath6kl * ar)2927 void ath6kl_htc_mbox_attach(struct ath6kl *ar)
2928 {
2929 ar->htc_ops = &ath6kl_htc_mbox_ops;
2930 }
2931