blob: 9173d46a8026f3b79e2e42d2ff97d310329283da [file] [log] [blame]
Kalle Valobdcd8172011-07-18 00:22:30 +03001/*
2 * Copyright (c) 2007-2011 Atheros Communications Inc.
Vasanthakumar Thiagarajan1b2df402012-02-06 20:15:53 +05303 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
Kalle Valobdcd8172011-07-18 00:22:30 +03004 *
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"
Kalle Valo2e1cb232011-10-05 12:23:49 +030019#include "hif.h"
Kalle Valobdcd8172011-07-18 00:22:30 +030020#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
Chilam Ngb29072c2012-02-07 01:33:00 -080026/* threshold to re-enable Tx bundling for an AC*/
27#define TX_RESUME_BUNDLE_THRESHOLD 1500
28
Kalle Valof2f92192011-10-24 12:17:20 +030029/* Functions for Tx credit handling */
Kalle Valocb64a612011-10-24 12:17:28 +030030static void ath6kl_credit_deposit(struct ath6kl_htc_credit_info *cred_info,
31 struct htc_endpoint_credit_dist *ep_dist,
32 int credits)
Kalle Valof2f92192011-10-24 12:17:20 +030033{
Kalle Valo02f0d6f2011-10-24 12:17:59 +030034 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit deposit ep %d credits %d\n",
35 ep_dist->endpoint, credits);
36
Kalle Valof2f92192011-10-24 12:17:20 +030037 ep_dist->credits += credits;
38 ep_dist->cred_assngd += credits;
39 cred_info->cur_free_credits -= credits;
40}
41
42static 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
Kalle Valo02f0d6f2011-10-24 12:17:59 +030049 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit init total %d\n", tot_credits);
50
Kalle Valof2f92192011-10-24 12:17:20 +030051 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)) {
Kalle Valocb64a612011-10-24 12:17:28 +030063 ath6kl_credit_deposit(cred_info,
64 cur_ep_dist,
65 cur_ep_dist->cred_min);
Kalle Valof2f92192011-10-24 12:17:20 +030066 cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
67 }
68 }
69
70 if (cur_ep_dist->svc_id == WMI_CONTROL_SVC) {
Kalle Valocb64a612011-10-24 12:17:28 +030071 ath6kl_credit_deposit(cred_info, cur_ep_dist,
72 cur_ep_dist->cred_min);
Kalle Valof2f92192011-10-24 12:17:20 +030073 /*
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 }
Kalle Valo02f0d6f2011-10-24 12:17:59 +0300120
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);
Kalle Valof2f92192011-10-24 12:17:20 +0300129 }
130}
131
132/* initialize and setup credit distribution */
Kalle Valocb64a612011-10-24 12:17:28 +0300133int ath6kl_credit_setup(void *htc_handle,
134 struct ath6kl_htc_credit_info *cred_info)
Kalle Valof2f92192011-10-24 12:17:20 +0300135{
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 */
Kalle Valocb64a612011-10-24 12:17:28 +0300153static void ath6kl_credit_reduce(struct ath6kl_htc_credit_info *cred_info,
154 struct htc_endpoint_credit_dist *ep_dist,
155 int limit)
Kalle Valof2f92192011-10-24 12:17:20 +0300156{
157 int credits;
158
Kalle Valo02f0d6f2011-10-24 12:17:59 +0300159 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit reduce ep %d limit %d\n",
160 ep_dist->endpoint, limit);
161
Kalle Valof2f92192011-10-24 12:17:20 +0300162 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
172static void ath6kl_credit_update(struct ath6kl_htc_credit_info *cred_info,
173 struct list_head *epdist_list)
174{
Kalle Valo05aab172012-03-07 20:04:00 +0200175 struct htc_endpoint_credit_dist *cur_list;
Kalle Valof2f92192011-10-24 12:17:20 +0300176
Kalle Valo05aab172012-03-07 20:04:00 +0200177 list_for_each_entry(cur_list, epdist_list, list) {
178 if (cur_list->endpoint == ENDPOINT_0)
Kalle Valof2f92192011-10-24 12:17:20 +0300179 continue;
180
Kalle Valo05aab172012-03-07 20:04:00 +0200181 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)
Kalle Valocb64a612011-10-24 12:17:28 +0300186 ath6kl_credit_reduce(cred_info,
Kalle Valo05aab172012-03-07 20:04:00 +0200187 cur_list,
188 cur_list->cred_assngd);
Kalle Valof2f92192011-10-24 12:17:20 +0300189
Kalle Valo05aab172012-03-07 20:04:00 +0200190 if (cur_list->credits > cur_list->cred_norm)
191 ath6kl_credit_reduce(cred_info, cur_list,
192 cur_list->cred_norm);
Kalle Valof2f92192011-10-24 12:17:20 +0300193
Kalle Valo05aab172012-03-07 20:04:00 +0200194 if (!(cur_list->dist_flags & HTC_EP_ACTIVE)) {
195 if (cur_list->txq_depth == 0)
Kalle Valocb64a612011-10-24 12:17:28 +0300196 ath6kl_credit_reduce(cred_info,
Kalle Valo05aab172012-03-07 20:04:00 +0200197 cur_list, 0);
Kalle Valof2f92192011-10-24 12:17:20 +0300198 }
199 }
200 }
201}
202
203/*
204 * HTC has an endpoint that needs credits, ep_dist is the endpoint in
205 * question.
206 */
Kalle Valocb64a612011-10-24 12:17:28 +0300207static void ath6kl_credit_seek(struct ath6kl_htc_credit_info *cred_info,
Kalle Valof2f92192011-10-24 12:17:20 +0300208 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 */
Kalle Valocb64a612011-10-24 12:17:28 +0300260 ath6kl_credit_reduce(cred_info, curdist_list,
261 curdist_list->cred_assngd - need);
Kalle Valof2f92192011-10-24 12:17:20 +0300262
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
274out:
275 /* did we find some credits? */
276 if (credits)
Kalle Valocb64a612011-10-24 12:17:28 +0300277 ath6kl_credit_deposit(cred_info, ep_dist, credits);
Kalle Valof2f92192011-10-24 12:17:20 +0300278
279 ep_dist->seek_cred = 0;
280}
281
282/* redistribute credits based on activity change */
Kalle Valocb64a612011-10-24 12:17:28 +0300283static void ath6kl_credit_redistribute(struct ath6kl_htc_credit_info *info,
284 struct list_head *ep_dist_list)
Kalle Valof2f92192011-10-24 12:17:20 +0300285{
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)
Kalle Valocb64a612011-10-24 12:17:28 +0300299 ath6kl_credit_reduce(info, curdist_list, 0);
Kalle Valof2f92192011-10-24 12:17:20 +0300300 else
Kalle Valocb64a612011-10-24 12:17:28 +0300301 ath6kl_credit_reduce(info,
302 curdist_list,
303 curdist_list->cred_min);
Kalle Valof2f92192011-10-24 12:17:20 +0300304 }
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 */
316static 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:
Kalle Valocb64a612011-10-24 12:17:28 +0300325 ath6kl_credit_redistribute(cred_info, ep_dist_list);
Kalle Valof2f92192011-10-24 12:17:20 +0300326 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
Kalle Valodfa01042011-09-06 11:10:49 +0300335static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
Vasanthakumar Thiagarajan94e532d2011-08-22 20:14:31 +0530336{
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
Kalle Valodfa01042011-09-06 11:10:49 +0300346static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
347 int ctrl0, int ctrl1)
Kalle Valobdcd8172011-07-18 00:22:30 +0300348{
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
362static 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
370static 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
395static 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
Kalle Valo471e92f2011-10-13 15:21:37 +0300415 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
Kalle Valo3c370392011-10-24 12:17:12 +0300416 target->credit_info, &target->cred_dist_list);
Kalle Valobdcd8172011-07-18 00:22:30 +0300417
Kalle Valo3c370392011-10-24 12:17:12 +0300418 ath6kl_credit_distribute(target->credit_info,
419 &target->cred_dist_list,
420 HTC_CREDIT_DIST_SEND_COMPLETE);
Kalle Valobdcd8172011-07-18 00:22:30 +0300421
422 spin_unlock_bh(&target->tx_lock);
423}
424
425static void htc_tx_complete(struct htc_endpoint *endpoint,
426 struct list_head *txq)
427{
428 if (list_empty(txq))
429 return;
430
Kalle Valoebf29c92011-10-13 15:21:15 +0300431 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300432 "htc tx complete ep %d pkts %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300433 endpoint->eid, get_queue_depth(txq));
434
Kalle Valo63de1112012-03-25 17:15:22 +0300435 ath6kl_tx_complete(endpoint->target, txq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300436}
437
438static 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
Kalle Valo2387f0d2011-10-30 21:16:49 +0200444 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx complete seqno %d\n",
445 packet->info.tx.seqno);
446
Kalle Valobdcd8172011-07-18 00:22:30 +0300447 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
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530454static void htc_async_tx_scat_complete(struct htc_target *target,
455 struct hif_scatter_req *scat_req)
Kalle Valobdcd8172011-07-18 00:22:30 +0300456{
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530457 struct htc_endpoint *endpoint;
Kalle Valobdcd8172011-07-18 00:22:30 +0300458 struct htc_packet *packet;
459 struct list_head tx_compq;
460 int i;
461
462 INIT_LIST_HEAD(&tx_compq);
463
Kalle Valoebf29c92011-10-13 15:21:15 +0300464 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +0200465 "htc tx scat complete len %d entries %d\n",
466 scat_req->len, scat_req->scat_entries);
Kalle Valobdcd8172011-07-18 00:22:30 +0300467
468 if (scat_req->status)
469 ath6kl_err("send scatter req failed: %d\n", scat_req->status);
470
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530471 packet = scat_req->scat_list[0].packet;
472 endpoint = &target->endpoint[packet->endpoint];
473
Kalle Valobdcd8172011-07-18 00:22:30 +0300474 /* 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
Kalle Valodfa01042011-09-06 11:10:49 +0300494static int ath6kl_htc_tx_issue(struct htc_target *target,
495 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +0300496{
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
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530506 padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
Kalle Valobdcd8172011-07-18 00:22:30 +0300507
Kalle Valoebf29c92011-10-13 15:21:15 +0300508 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo2387f0d2011-10-30 21:16:49 +0200509 "htc tx issue len %d seqno %d padded_len %d mbox 0x%X %s\n",
510 send_len, packet->info.tx.seqno, padded_len,
Kalle Valo471e92f2011-10-13 15:21:37 +0300511 target->dev->ar->mbox_info.htc_addr,
512 sync ? "sync" : "async");
Kalle Valobdcd8172011-07-18 00:22:30 +0300513
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;
Kalle Valo65d2bb12011-08-14 18:10:03 -0700521 packet->buf += HTC_HDR_LENGTH;
Kalle Valobdcd8172011-07-18 00:22:30 +0300522 } 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
531static 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
Kalle Valo02f0d6f2011-10-24 12:17:59 +0300540 ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit check need %d got %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300541 *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
Kalle Valocb64a612011-10-24 12:17:28 +0300550 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
Kalle Valobdcd8172011-07-18 00:22:30 +0300551
552 ep->cred_dist.seek_cred = 0;
553
554 if (ep->cred_dist.credits < *req_cred) {
Kalle Valo02f0d6f2011-10-24 12:17:59 +0300555 ath6kl_dbg(ATH6KL_DBG_CREDIT,
556 "credit not found for ep %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300557 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
Kalle Valocb64a612011-10-24 12:17:28 +0300570 ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
Kalle Valobdcd8172011-07-18 00:22:30 +0300571
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;
Kalle Valo02f0d6f2011-10-24 12:17:59 +0300577 ath6kl_dbg(ATH6KL_DBG_CREDIT,
578 "credit we need credits asap\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300579 }
580 }
581
582 return 0;
583}
584
Kalle Valodfa01042011-09-06 11:10:49 +0300585static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
586 struct htc_endpoint *endpoint,
587 struct list_head *queue)
Kalle Valobdcd8172011-07-18 00:22:30 +0300588{
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
Kalle Valoebf29c92011-10-13 15:21:15 +0300603 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +0200604 "htc tx got packet 0x%p queue depth %d\n",
605 packet, get_queue_depth(&endpoint->txq));
Kalle Valobdcd8172011-07-18 00:22:30 +0300606
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530607 len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +0300608 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 */
635static 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
Kalle Valodfa01042011-09-06 11:10:49 +0300666static 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)
Kalle Valobdcd8172011-07-18 00:22:30 +0300671{
672 struct htc_packet *packet;
673 int i, len, rem_scat, cred_pad;
674 int status = 0;
Kalle Valo05aab172012-03-07 20:04:00 +0200675 u8 flags;
Kalle Valobdcd8172011-07-18 00:22:30 +0300676
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +0530677 rem_scat = target->max_tx_bndl_sz;
Kalle Valobdcd8172011-07-18 00:22:30 +0300678
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);
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530686 len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +0300687 packet->act_len + HTC_HDR_LENGTH);
688
689 cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
690 &len, endpoint);
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530691 if (cred_pad < 0 || rem_scat < len) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300692 status = -ENOSPC;
693 break;
694 }
695
696 rem_scat -= len;
697 /* now remove it from the queue */
Kalle Valobdcd8172011-07-18 00:22:30 +0300698 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 */
Kalle Valo05aab172012-03-07 20:04:00 +0200702 flags = packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE;
703 ath6kl_htc_tx_prep_pkt(packet, flags,
Kalle Valo96f1fad2012-03-07 20:03:57 +0200704 cred_pad, packet->info.tx.seqno);
Vasanthakumar Thiagarajan94e532d2011-08-22 20:14:31 +0530705 /* Make sure the buffer is 4-byte aligned */
Kalle Valodfa01042011-09-06 11:10:49 +0300706 ath6kl_htc_tx_buf_align(&packet->buf,
707 packet->act_len + HTC_HDR_LENGTH);
Kalle Valobdcd8172011-07-18 00:22:30 +0300708 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++;
Kalle Valoebf29c92011-10-13 15:21:15 +0300713 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo2387f0d2011-10-30 21:16:49 +0200714 "htc tx adding (%d) pkt 0x%p seqno %d len %d remaining %d\n",
715 i, packet, packet->info.tx.seqno, len, rem_scat);
Kalle Valobdcd8172011-07-18 00:22:30 +0300716 }
717
718 /* Roll back scatter setup in case of any failure */
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530719 if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300720 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 }
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530727 return -EAGAIN;
Kalle Valobdcd8172011-07-18 00:22:30 +0300728 }
729
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530730 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +0300731}
732
733/*
Kalle Valodfa01042011-09-06 11:10:49 +0300734 * Drain a queue and send as bundles this function may return without fully
735 * draining the queue when
Kalle Valobdcd8172011-07-18 00:22:30 +0300736 *
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 */
Kalle Valodfa01042011-09-06 11:10:49 +0300742static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
743 struct list_head *queue,
744 int *sent_bundle, int *n_bundle_pkts)
Kalle Valobdcd8172011-07-18 00:22:30 +0300745{
746 struct htc_target *target = endpoint->target;
747 struct hif_scatter_req *scat_req = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +0300748 int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0;
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530749 int status;
Chilam Ngb29072c2012-02-07 01:33:00 -0800750 u32 txb_mask;
751 u8 ac = WMM_NUM_AC;
752
753 if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) ||
Kalle Valo96f1fad2012-03-07 20:03:57 +0200754 (WMI_CONTROL_SVC != endpoint->svc_id))
Chilam Ngb29072c2012-02-07 01:33:00 -0800755 ac = target->dev->ar->ep2ac_map[endpoint->eid];
Kalle Valobdcd8172011-07-18 00:22:30 +0300756
Kalle Valobdcd8172011-07-18 00:22:30 +0300757 while (true) {
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530758 status = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300759 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 */
Kalle Valoebf29c92011-10-13 15:21:15 +0300770 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +0200771 "htc tx no more scatter resources\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300772 break;
773 }
774
Chilam Ngb29072c2012-02-07 01:33:00 -0800775 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
Kalle Valo471e92f2011-10-13 15:21:37 +0300800 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300801 n_scat);
802
803 scat_req->len = 0;
804 scat_req->scat_entries = 0;
805
Kalle Valodfa01042011-09-06 11:10:49 +0300806 status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
807 scat_req, n_scat,
808 queue);
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530809 if (status == -EAGAIN) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300810 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;
Kalle Valobdcd8172011-07-18 00:22:30 +0300816 n_sent_bundle++;
817 tot_pkts_bundle += scat_req->scat_entries;
818
Kalle Valoebf29c92011-10-13 15:21:15 +0300819 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300820 "htc tx scatter bytes %d entries %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300821 scat_req->len, scat_req->scat_entries);
Kalle Valo8e8ddb22011-10-05 12:23:33 +0300822 ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530823
824 if (status)
825 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300826 }
827
828 *sent_bundle = n_sent_bundle;
829 *n_bundle_pkts = tot_pkts_bundle;
Kalle Valo471e92f2011-10-13 15:21:37 +0300830 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
831 n_sent_bundle);
Kalle Valobdcd8172011-07-18 00:22:30 +0300832
833 return;
834}
835
Kalle Valodfa01042011-09-06 11:10:49 +0300836static void ath6kl_htc_tx_from_queue(struct htc_target *target,
837 struct htc_endpoint *endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +0300838{
839 struct list_head txq;
840 struct htc_packet *packet;
841 int bundle_sent;
842 int n_pkts_bundle;
Chilam Ngb29072c2012-02-07 01:33:00 -0800843 u8 ac = WMM_NUM_AC;
Kalle Valobdcd8172011-07-18 00:22:30 +0300844
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);
Kalle Valo471e92f2011-10-13 15:21:37 +0300851 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300852 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
Chilam Ngb29072c2012-02-07 01:33:00 -0800861 if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) ||
Kalle Valo96f1fad2012-03-07 20:03:57 +0200862 (WMI_CONTROL_SVC != endpoint->svc_id))
Chilam Ngb29072c2012-02-07 01:33:00 -0800863 ac = target->dev->ar->ep2ac_map[endpoint->eid];
864
Kalle Valobdcd8172011-07-18 00:22:30 +0300865 while (true) {
866
867 if (list_empty(&endpoint->txq))
868 break;
869
Kalle Valodfa01042011-09-06 11:10:49 +0300870 ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300871
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 */
Chilam Ngb29072c2012-02-07 01:33:00 -0800882 if ((target->tx_bndl_mask) &&
Kalle Valobdcd8172011-07-18 00:22:30 +0300883 (get_queue_depth(&txq) >=
884 HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
885 int temp1 = 0, temp2 = 0;
886
Chilam Ngb29072c2012-02-07 01:33:00 -0800887 /* 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 }
Kalle Valobdcd8172011-07-18 00:22:30 +0300894 }
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
Kalle Valodfa01042011-09-06 11:10:49 +0300903 ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
904 0, packet->info.tx.seqno);
905 ath6kl_htc_tx_issue(target, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +0300906 }
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;
Chilam Ngb29072c2012-02-07 01:33:00 -0800912
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)) &&
Kalle Valo96f1fad2012-03-07 20:03:57 +0200920 (ac < WMM_NUM_AC)) {
Chilam Ngb29072c2012-02-07 01:33:00 -0800921 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 }
Kalle Valobdcd8172011-07-18 00:22:30 +0300932 }
933
934 endpoint->tx_proc_cnt = 0;
935 spin_unlock_bh(&target->tx_lock);
936}
937
Kalle Valodfa01042011-09-06 11:10:49 +0300938static bool ath6kl_htc_tx_try(struct htc_target *target,
939 struct htc_endpoint *endpoint,
940 struct htc_packet *tx_pkt)
Kalle Valobdcd8172011-07-18 00:22:30 +0300941{
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)
Kalle Valoebf29c92011-10-13 15:21:15 +0300956 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300957 "htc tx overflow ep %d depth %d max %d\n",
958 endpoint->eid, txq_depth,
Kalle Valobdcd8172011-07-18 00:22:30 +0300959 endpoint->max_txq_depth);
960
961 if (overflow && ep_cb.tx_full) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300962 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
Kalle Valodfa01042011-09-06 11:10:49 +0300973 ath6kl_htc_tx_from_queue(target, endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +0300974
975 return true;
976}
977
978static 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) {
Kalle Valoe8c39792011-10-24 12:17:04 +0300990 endpoint = cred_dist->htc_ep;
Kalle Valobdcd8172011-07-18 00:22:30 +0300991
992 spin_lock_bh(&target->tx_lock);
993 if (!list_empty(&endpoint->txq)) {
Kalle Valoebf29c92011-10-13 15:21:15 +0300994 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300995 "htc creds ep %d credits %d pkts %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300996 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 */
Kalle Valodfa01042011-09-06 11:10:49 +03001007 ath6kl_htc_tx_from_queue(target, endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +03001008 spin_lock_bh(&target->tx_lock);
1009 }
1010 spin_unlock_bh(&target->tx_lock);
1011 }
1012}
1013
1014static 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,
Kalle Valo96f1fad2012-03-07 20:03:57 +02001044 sizeof(struct htc_setup_comp_ext_msg),
1045 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
Kalle Valobdcd8172011-07-18 00:22:30 +03001046
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,
Kalle Valo96f1fad2012-03-07 20:03:57 +02001053 sizeof(struct htc_setup_comp_msg),
1054 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
Kalle Valobdcd8172011-07-18 00:22:30 +03001055 }
1056
1057 /* we want synchronous operation */
1058 send_pkt->completion = NULL;
Kalle Valodfa01042011-09-06 11:10:49 +03001059 ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
1060 status = ath6kl_htc_tx_issue(target, send_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001061
1062 if (send_pkt != NULL)
1063 htc_reclaim_txctrl_buf(target, send_pkt);
1064
1065 return status;
1066}
1067
Kalle Vaload226ec2011-08-10 09:49:12 +03001068void ath6kl_htc_set_credit_dist(struct htc_target *target,
Kalle Valo3c370392011-10-24 12:17:12 +03001069 struct ath6kl_htc_credit_info *credit_info,
Kalle Vaload226ec2011-08-10 09:49:12 +03001070 u16 srvc_pri_order[], int list_len)
Kalle Valobdcd8172011-07-18 00:22:30 +03001071{
1072 struct htc_endpoint *endpoint;
1073 int i, ep;
1074
Kalle Valo3c370392011-10-24 12:17:12 +03001075 target->credit_info = credit_info;
Kalle Valobdcd8172011-07-18 00:22:30 +03001076
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
Kalle Vaload226ec2011-08-10 09:49:12 +03001096int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +03001097{
1098 struct htc_endpoint *endpoint;
1099 struct list_head queue;
1100
Kalle Valoebf29c92011-10-13 15:21:15 +03001101 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001102 "htc tx ep id %d buf 0x%p len %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001103 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
Kalle Valodfa01042011-09-06 11:10:49 +03001112 if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001113 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 */
Kalle Vaload226ec2011-08-10 09:49:12 +03001124void ath6kl_htc_flush_txep(struct htc_target *target,
1125 enum htc_endpoint_id eid, u16 tag)
Kalle Valobdcd8172011-07-18 00:22:30 +03001126{
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);
Kalle Valoebf29c92011-10-13 15:21:15 +03001152 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +02001153 "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);
Kalle Valobdcd8172011-07-18 00:22:30 +03001156
1157 INIT_LIST_HEAD(&container);
1158 list_add_tail(&packet->list, &container);
1159 htc_tx_complete(endpoint, &container);
1160 }
1161
1162}
1163
Kalle Vaload226ec2011-08-10 09:49:12 +03001164static void ath6kl_htc_flush_txep_all(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03001165{
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;
Kalle Vaload226ec2011-08-10 09:49:12 +03001176 ath6kl_htc_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
Kalle Valobdcd8172011-07-18 00:22:30 +03001177 }
1178}
1179
Kalle Vaload226ec2011-08-10 09:49:12 +03001180void ath6kl_htc_indicate_activity_change(struct htc_target *target,
1181 enum htc_endpoint_id eid, bool active)
Kalle Valobdcd8172011-07-18 00:22:30 +03001182{
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
Kalle Valo471e92f2011-10-13 15:21:37 +03001209 ath6kl_dbg(ATH6KL_DBG_HTC,
1210 "htc tx activity ctxt 0x%p dist 0x%p\n",
Kalle Valo3c370392011-10-24 12:17:12 +03001211 target->credit_info, &target->cred_dist_list);
Kalle Valobdcd8172011-07-18 00:22:30 +03001212
Kalle Valo3c370392011-10-24 12:17:12 +03001213 ath6kl_credit_distribute(target->credit_info,
Kalle Valofa99e962011-10-24 12:16:55 +03001214 &target->cred_dist_list,
1215 HTC_CREDIT_DIST_ACTIVITY_CHANGE);
Kalle Valobdcd8172011-07-18 00:22:30 +03001216 }
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
Kalle Valo689def92011-09-06 11:10:49 +03001226static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
1227 int n_look_ahds)
Kalle Valobdcd8172011-07-18 00:22:30 +03001228{
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
1236static 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
1243static 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);
Kalle Vaload226ec2011-08-10 09:49:12 +03001249 return ath6kl_htc_add_rxbuf_multiple(target, &queue);
Kalle Valobdcd8172011-07-18 00:22:30 +03001250}
1251
1252static 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
1266static 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
Kalle Valo689def92011-09-06 11:10:49 +03001274static int ath6kl_htc_rx_packet(struct htc_target *target,
1275 struct htc_packet *packet,
1276 u32 rx_len)
Kalle Valobdcd8172011-07-18 00:22:30 +03001277{
1278 struct ath6kl_device *dev = target->dev;
1279 u32 padded_len;
1280 int status;
1281
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05301282 padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001283
1284 if (padded_len > packet->buf_len) {
Kalle Valo471e92f2011-10-13 15:21:37 +03001285 ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001286 padded_len, rx_len, packet->buf_len);
1287 return -ENOMEM;
1288 }
1289
Kalle Valoebf29c92011-10-13 15:21:15 +03001290 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001291 "htc rx 0x%p hdr x%x len %d mbox 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001292 packet, packet->info.rx.exp_hdr,
Kalle Valo471e92f2011-10-13 15:21:37 +03001293 padded_len, dev->ar->mbox_info.htc_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +03001294
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 */
Kalle Valo689def92011-09-06 11:10:49 +03001310static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
1311 struct htc_endpoint *endpoint,
1312 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +03001313{
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
Kalle Valo689def92011-09-06 11:10:49 +03001323static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +03001324{
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 */
Kalle Valo689def92011-09-06 11:10:49 +03001340static 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)
Kalle Valobdcd8172011-07-18 00:22:30 +03001343{
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
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05301351 full_len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +03001352 le16_to_cpu(htc_hdr->payld_len) +
1353 sizeof(*htc_hdr));
1354
1355 if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
Vasanthakumar Thiagarajan5c980fb2012-03-21 14:52:16 +05301356 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));
Kalle Valobdcd8172011-07-18 00:22:30 +03001359 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
Kalle Valo689def92011-09-06 11:10:49 +03001444static 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)
Kalle Valobdcd8172011-07-18 00:22:30 +03001448{
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;
Kalle Valoebf29c92011-10-13 15:21:15 +03001505 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001506 "htc rx bundle pkts %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001507 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 */
Kalle Valo689def92011-09-06 11:10:49 +03001513 status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
1514 queue, n_msg);
Kalle Valobdcd8172011-07-18 00:22:30 +03001515
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
1543static 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",
Kalle Valo96f1fad2012-03-07 20:03:57 +02001557 packets->act_len + HTC_HDR_LENGTH);
Kalle Valobdcd8172011-07-18 00:22:30 +03001558
Kalle Valo471e92f2011-10-13 15:21:37 +03001559 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1560 "htc rx unexpected endpoint 0 message", "",
Kalle Valoef094102011-09-27 14:30:45 +03001561 packets->buf - HTC_HDR_LENGTH,
1562 packets->act_len + HTC_HDR_LENGTH);
Kalle Valobdcd8172011-07-18 00:22:30 +03001563 }
1564
1565 htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
1566}
1567
1568static 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
Kalle Valobdcd8172011-07-18 00:22:30 +03001577 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
Kalle Valo02f0d6f2011-10-24 12:17:59 +03001588 ath6kl_dbg(ATH6KL_DBG_CREDIT,
1589 "credit report ep %d credits %d\n",
Kalle Valo471e92f2011-10-13 15:21:37 +03001590 rpt->eid, rpt->credits);
Kalle Valobdcd8172011-07-18 00:22:30 +03001591
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
Raja Mani5ba3ee42011-07-19 19:27:31 +05301611 if (rpt->eid == ENDPOINT_0)
Kalle Valobdcd8172011-07-18 00:22:30 +03001612 /* 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
Kalle Valobdcd8172011-07-18 00:22:30 +03001630 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 */
Kalle Valo3c370392011-10-24 12:17:12 +03001635 ath6kl_credit_distribute(target->credit_info,
Kalle Valofa99e962011-10-24 12:16:55 +03001636 &target->cred_dist_list,
1637 HTC_CREDIT_DIST_SEND_COMPLETE);
Kalle Valobdcd8172011-07-18 00:22:30 +03001638 }
1639
1640 spin_unlock_bh(&target->tx_lock);
1641
1642 if (tot_credits)
1643 htc_chk_ep_txq(target);
1644}
1645
1646static 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;
Kalle Valoddc3d772012-03-07 20:03:58 +02001676 if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF)) &&
1677 next_lk_ahds) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001678
Kalle Valoebf29c92011-10-13 15:21:15 +03001679 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001680 "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001681 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
Kalle Valo471e92f2011-10-13 15:21:37 +03001686 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1687 "htc rx next look ahead",
Kalle Valoef094102011-09-27 14:30:45 +03001688 "", next_lk_ahds, 4);
Kalle Valobdcd8172011-07-18 00:22:30 +03001689
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
Kalle Valo471e92f2011-10-13 15:21:37 +03001706 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
Kalle Valoef094102011-09-27 14:30:45 +03001707 "", record_buf, record->len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001708
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
1728static 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
Kalle Valo471e92f2011-10-13 15:21:37 +03001738 ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
1739 ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001740
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
Raja Mani2588f552011-07-19 19:27:30 +05301775 if (status)
Kalle Valo471e92f2011-10-13 15:21:37 +03001776 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
Kalle Valoef094102011-09-27 14:30:45 +03001777 "", orig_buf, orig_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001778
1779 return status;
1780}
1781
Kalle Valo689def92011-09-06 11:10:49 +03001782static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
1783 struct htc_packet *packet,
1784 u32 *next_lkahds, int *n_lkahds)
Kalle Valobdcd8172011-07-18 00:22:30 +03001785{
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
Kalle Valobdcd8172011-07-18 00:22:30 +03001794 /*
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) {
Kalle Valo689def92011-09-06 11:10:49 +03001833 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1834 __func__, packet, packet->info.rx.rx_flags);
Kalle Valo471e92f2011-10-13 15:21:37 +03001835 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
Kalle Valoef094102011-09-27 14:30:45 +03001836 "", &packet->info.rx.exp_hdr, 4);
Kalle Valo471e92f2011-10-13 15:21:37 +03001837 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
Kalle Valoef094102011-09-27 14:30:45 +03001838 "", (u8 *)&lk_ahd, sizeof(lk_ahd));
Kalle Valobdcd8172011-07-18 00:22:30 +03001839 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) {
Kalle Valo689def92011-09-06 11:10:49 +03001846 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1847 __func__, payload_len, htc_hdr->ctrl[0]);
Kalle Valobdcd8172011-07-18 00:22:30 +03001848 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
1871fail_rx:
1872 if (status)
Kalle Valo471e92f2011-10-13 15:21:37 +03001873 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1874 "", packet->buf, packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001875
1876 return status;
1877}
1878
Kalle Valo689def92011-09-06 11:10:49 +03001879static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1880 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +03001881{
Kalle Valoebf29c92011-10-13 15:21:15 +03001882 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001883 "htc rx complete ep %d packet 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001884 endpoint->eid, packet);
1885 endpoint->ep_cb.rx(endpoint->target, packet);
1886}
1887
Kalle Valo689def92011-09-06 11:10:49 +03001888static 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)
Kalle Valobdcd8172011-07-18 00:22:30 +03001892{
1893 struct hif_scatter_req *scat_req;
1894 struct htc_packet *packet;
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05301895 int rem_space = target->max_rx_bndl_sz;
Kalle Valobdcd8172011-07-18 00:22:30 +03001896 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 */
Kalle Valo689def92011-09-06 11:10:49 +03001913 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1914 __func__, get_queue_depth(rxq), n_scat_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001915 }
1916
1917 len = 0;
1918
Kalle Valoebf29c92011-10-13 15:21:15 +03001919 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001920 "htc rx bundle depth %d pkts %d\n",
1921 get_queue_depth(rxq), n_scat_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001922
1923 scat_req = hif_scatter_req_get(target->dev->ar);
1924
1925 if (scat_req == NULL)
1926 goto fail_rx_pkt;
1927
Kalle Valobdcd8172011-07-18 00:22:30 +03001928 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
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05301934 pad_len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +03001935 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
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001968 status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001969
1970 if (!status)
1971 *n_pkt_fetched = i;
1972
1973 /* free scatter request */
1974 hif_scatter_req_add(target->dev->ar, scat_req);
1975
1976fail_rx_pkt:
1977
1978 return status;
1979}
1980
Kalle Valo689def92011-09-06 11:10:49 +03001981static 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)
Kalle Valobdcd8172011-07-18 00:22:30 +03001985{
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) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001991 ep = &target->endpoint[packet->endpoint];
1992
1993 /* process header for each of the recv packet */
Kalle Valo689def92011-09-06 11:10:49 +03001994 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
1995 n_lk_ahd);
Kalle Valobdcd8172011-07-18 00:22:30 +03001996 if (status)
1997 return status;
1998
Vasanthakumar Thiagarajan4159cc92011-10-03 17:28:07 +05301999 list_del(&packet->list);
2000
Kalle Valobdcd8172011-07-18 00:22:30 +03002001 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)
Kalle Valo689def92011-09-06 11:10:49 +03002007 ath6kl_htc_rx_set_indicate(lk_ahds[0],
2008 ep, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03002009 } 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
Kalle Valo689def92011-09-06 11:10:49 +03002017 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
Kalle Valobdcd8172011-07-18 00:22:30 +03002018
2019 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
2020 ep->ep_st.rx_bundl += 1;
2021
Kalle Valo689def92011-09-06 11:10:49 +03002022 ath6kl_htc_rx_complete(ep, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03002023 }
2024
2025 return status;
2026}
2027
Kalle Valo689def92011-09-06 11:10:49 +03002028static int ath6kl_htc_rx_fetch(struct htc_target *target,
2029 struct list_head *rx_pktq,
2030 struct list_head *comp_pktq)
Kalle Valobdcd8172011-07-18 00:22:30 +03002031{
2032 int fetched_pkts;
2033 bool part_bundle = false;
2034 int status = 0;
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302035 struct list_head tmp_rxq;
2036 struct htc_packet *packet, *tmp_pkt;
Kalle Valobdcd8172011-07-18 00:22:30 +03002037
2038 /* now go fetch the list of HTC packets */
2039 while (!list_empty(rx_pktq)) {
2040 fetched_pkts = 0;
2041
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302042 INIT_LIST_HEAD(&tmp_rxq);
2043
Kalle Valobdcd8172011-07-18 00:22:30 +03002044 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 */
Kalle Valo689def92011-09-06 11:10:49 +03002050 status = ath6kl_htc_rx_bundle(target, rx_pktq,
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302051 &tmp_rxq,
Kalle Valo689def92011-09-06 11:10:49 +03002052 &fetched_pkts,
2053 part_bundle);
Kalle Valobdcd8172011-07-18 00:22:30 +03002054 if (status)
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302055 goto fail_rx;
Kalle Valobdcd8172011-07-18 00:22:30 +03002056
2057 if (!list_empty(rx_pktq))
2058 part_bundle = true;
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302059
2060 list_splice_tail_init(&tmp_rxq, comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002061 }
2062
2063 if (!fetched_pkts) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002064
2065 packet = list_first_entry(rx_pktq, struct htc_packet,
2066 list);
2067
Kalle Valobdcd8172011-07-18 00:22:30 +03002068 /* fully synchronous */
2069 packet->completion = NULL;
2070
Vasanthakumar Thiagarajanb8d5d5f2011-10-03 17:28:25 +05302071 if (!list_is_singular(rx_pktq))
Kalle Valobdcd8172011-07-18 00:22:30 +03002072 /*
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 */
Kalle Valo689def92011-09-06 11:10:49 +03002081 status = ath6kl_htc_rx_packet(target, packet,
2082 packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03002083
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302084 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);
Kalle Valobdcd8172011-07-18 00:22:30 +03002090 }
2091 }
2092
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302093 return 0;
2094
2095fail_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,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002105 &target->endpoint[packet->endpoint]);
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302106 }
2107
2108 list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
2109 list_del(&packet->list);
2110 htc_reclaim_rxbuf(target, packet,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002111 &target->endpoint[packet->endpoint]);
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05302112 }
2113
Kalle Valobdcd8172011-07-18 00:22:30 +03002114 return status;
2115}
2116
Kalle Vaload226ec2011-08-10 09:49:12 +03002117int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
Vasanthakumar Thiagarajan4533d902011-10-03 17:26:27 +05302118 u32 msg_look_ahead, int *num_pkts)
Kalle Valobdcd8172011-07-18 00:22:30 +03002119{
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
Sujith Manoharancbec2672012-01-10 09:53:53 +05302129 INIT_LIST_HEAD(&comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002130 *num_pkts = 0;
2131
2132 /*
2133 * On first entry copy the look_aheads into our temp array for
2134 * processing
2135 */
Vasanthakumar Thiagarajan4533d902011-10-03 17:26:27 +05302136 look_aheads[0] = msg_look_ahead;
Kalle Valobdcd8172011-07-18 00:22:30 +03002137
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 */
Kalle Valo689def92011-09-06 11:10:49 +03002161 status = ath6kl_htc_rx_alloc(target, look_aheads,
2162 num_look_ahead, endpoint,
2163 &rx_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002164 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 */
Vasanthakumar Thiagarajanfcb82052011-07-18 14:23:31 +05302172 target->chk_irq_status_cnt = 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03002173
2174 n_fetched += get_queue_depth(&rx_pktq);
2175
2176 num_look_ahead = 0;
2177
Kalle Valo689def92011-09-06 11:10:49 +03002178 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002179
2180 if (!status)
Kalle Valo689def92011-09-06 11:10:49 +03002181 ath6kl_htc_rx_chk_water_mark(endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +03002182
2183 /* Process fetched packets */
Kalle Valo689def92011-09-06 11:10:49 +03002184 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
2185 look_aheads,
2186 &num_look_ahead);
Kalle Valobdcd8172011-07-18 00:22:30 +03002187
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 */
Vasanthakumar Thiagarajanfcb82052011-07-18 14:23:31 +05302198 target->chk_irq_status_cnt = 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03002199 }
2200
2201 if (status) {
2202 ath6kl_err("failed to get pending recv messages: %d\n",
2203 status);
Kalle Valobdcd8172011-07-18 00:22:30 +03002204
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");
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002214 ath6kl_hif_rx_control(target->dev, false);
Kalle Valobdcd8172011-07-18 00:22:30 +03002215 }
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");
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002224 ath6kl_hif_rx_control(target->dev, false);
Kalle Valobdcd8172011-07-18 00:22:30 +03002225 }
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 */
2236static 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
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002242 if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002243 HTC_TARGET_RESPONSE_TIMEOUT))
Kalle Valobdcd8172011-07-18 00:22:30 +03002244 return NULL;
2245
Kalle Valoebf29c92011-10-13 15:21:15 +03002246 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002247 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
Kalle Valobdcd8172011-07-18 00:22:30 +03002248
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 */
Kalle Valo689def92011-09-06 11:10:49 +03002270 if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
Kalle Valobdcd8172011-07-18 00:22:30 +03002271 goto fail_ctrl_rx;
2272
2273 /* process receive header */
Kalle Valo689def92011-09-06 11:10:49 +03002274 packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
Kalle Valobdcd8172011-07-18 00:22:30 +03002275
2276 if (packet->status) {
Kalle Valo689def92011-09-06 11:10:49 +03002277 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002278 packet->status);
2279 goto fail_ctrl_rx;
2280 }
2281
2282 return packet;
2283
2284fail_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
Kalle Vaload226ec2011-08-10 09:49:12 +03002293int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
2294 struct list_head *pkt_queue)
Kalle Valobdcd8172011-07-18 00:22:30 +03002295{
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
Kalle Valoebf29c92011-10-13 15:21:15 +03002311 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002312 "htc rx add multiple ep id %d cnt %d len %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002313 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);
Kalle Valo689def92011-09-06 11:10:49 +03002324 ath6kl_htc_rx_complete(endpoint, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03002325 }
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) {
Kalle Valoebf29c92011-10-13 15:21:15 +03002337 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002338 "htc rx blocked on ep %d, unblocking\n",
2339 target->ep_waiting);
Kalle Valobdcd8172011-07-18 00:22:30 +03002340 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? */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002350 ath6kl_hif_rx_control(target->dev, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03002351
2352 return status;
2353}
2354
Kalle Vaload226ec2011-08-10 09:49:12 +03002355void ath6kl_htc_flush_rx_buf(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002356{
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);
Kalle Valoebf29c92011-10-13 15:21:15 +03002372 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002373 "htc rx flush pkt 0x%p len %d ep %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002374 packet, packet->buf_len,
2375 packet->endpoint);
Vasanthakumar Thiagarajan5378f242012-02-10 20:40:33 +05302376 /*
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 }
Kalle Valobdcd8172011-07-18 00:22:30 +03002391 spin_lock_bh(&target->rx_lock);
2392 }
2393 spin_unlock_bh(&target->rx_lock);
2394 }
2395}
2396
Kalle Vaload226ec2011-08-10 09:49:12 +03002397int ath6kl_htc_conn_service(struct htc_target *target,
2398 struct htc_service_connect_req *conn_req,
2399 struct htc_service_connect_resp *conn_resp)
Kalle Valobdcd8172011-07-18 00:22:30 +03002400{
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;
Kalle Valo05aab172012-03-07 20:04:00 +02002409 u16 msg_id;
Kalle Valobdcd8172011-07-18 00:22:30 +03002410
Kalle Valoebf29c92011-10-13 15:21:15 +03002411 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002412 "htc connect service target 0x%p service id 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002413 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;
Kalle Valodfa01042011-09-06 11:10:49 +03002438 ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
2439 status = ath6kl_htc_tx_issue(target, tx_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03002440
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;
Kalle Valo05aab172012-03-07 20:04:00 +02002453 msg_id = le16_to_cpu(resp_msg->msg_id);
Kalle Valobdcd8172011-07-18 00:22:30 +03002454
Kalle Valo05aab172012-03-07 20:04:00 +02002455 if ((msg_id != HTC_MSG_CONN_SVC_RESP_ID) ||
Kalle Valoddc3d772012-03-07 20:03:58 +02002456 (rx_pkt->act_len < sizeof(*resp_msg))) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002457 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;
Kalle Valoe8c39792011-10-24 12:17:04 +03002499 endpoint->cred_dist.htc_ep = endpoint;
Kalle Valobdcd8172011-07-18 00:22:30 +03002500 endpoint->cred_dist.endpoint = assigned_ep;
2501 endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
2502
Chilam Ng0ea10f2b2012-02-09 02:17:01 -08002503 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
Kalle Valobdcd8172011-07-18 00:22:30 +03002512 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
2534fail_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
2546static 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 */
Kalle Valo3c370392011-10-24 12:17:12 +03002565 /* FIXME: free existing entries */
Kalle Valobdcd8172011-07-18 00:22:30 +03002566 INIT_LIST_HEAD(&target->cred_dist_list);
2567}
2568
Kalle Vaload226ec2011-08-10 09:49:12 +03002569int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
2570 enum htc_endpoint_id endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +03002571{
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
2580static void htc_setup_msg_bndl(struct htc_target *target)
2581{
Kalle Valobdcd8172011-07-18 00:22:30 +03002582 /* 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
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302586 if (ath6kl_hif_enable_scatter(target->dev->ar)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002587 target->msg_per_bndl_max = 0;
2588 return;
2589 }
2590
2591 /* limit bundle what the device layer can handle */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302592 target->msg_per_bndl_max = min(target->max_scat_entries,
Kalle Valobdcd8172011-07-18 00:22:30 +03002593 target->msg_per_bndl_max);
2594
Kalle Valo3ef987b2011-10-24 12:18:07 +03002595 ath6kl_dbg(ATH6KL_DBG_BOOT,
Kalle Valo471e92f2011-10-13 15:21:37 +03002596 "htc bundling allowed msg_per_bndl_max %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002597 target->msg_per_bndl_max);
2598
2599 /* Max rx bundle size is limited by the max tx bundle size */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302600 target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
Kalle Valobdcd8172011-07-18 00:22:30 +03002601 /* Max tx bundle size if limited by the extended mbox address range */
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302602 target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302603 target->max_xfer_szper_scatreq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002604
Kalle Valo3ef987b2011-10-24 12:18:07 +03002605 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302606 target->max_rx_bndl_sz, target->max_tx_bndl_sz);
Kalle Valobdcd8172011-07-18 00:22:30 +03002607
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302608 if (target->max_tx_bndl_sz)
Chilam Ngb29072c2012-02-07 01:33:00 -08002609 /* tx_bndl_mask is enabled per AC, each has 1 bit */
2610 target->tx_bndl_mask = (1 << WMM_NUM_AC) - 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03002611
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302612 if (target->max_rx_bndl_sz)
Kalle Valobdcd8172011-07-18 00:22:30 +03002613 target->rx_bndl_enable = true;
2614
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05302615 if ((target->tgt_cred_sz % target->block_sz) != 0) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002616 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 */
Chilam Ngb29072c2012-02-07 01:33:00 -08002625 target->tx_bndl_mask = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03002626 }
2627}
2628
Kalle Vaload226ec2011-08-10 09:49:12 +03002629int ath6kl_htc_wait_target(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002630{
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
Kalle Valo241b1282012-01-17 20:09:45 +02002637 /* 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
Kalle Valobdcd8172011-07-18 00:22:30 +03002643 /* 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
Kalle Valo3ef987b2011-10-24 12:18:07 +03002666 ath6kl_dbg(ATH6KL_DBG_BOOT,
Kalle Valo471e92f2011-10-13 15:21:37 +03002667 "htc target ready credits %d size %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002668 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
Kalle Valo3ef987b2011-10-24 12:18:07 +03002681 ath6kl_dbg(ATH6KL_DBG_BOOT, "htc using protocol %s (%d)\n",
Kalle Valo96f1fad2012-03-07 20:03:57 +02002682 (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
2683 target->htc_tgt_ver);
Kalle Valobdcd8172011-07-18 00:22:30 +03002684
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 */
Kalle Vaload226ec2011-08-10 09:49:12 +03002698 status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
Kalle Valobdcd8172011-07-18 00:22:30 +03002699
2700 if (status)
Kalle Valo4e3d54c2011-10-27 18:48:22 +03002701 /*
2702 * FIXME: this call doesn't make sense, the caller should
2703 * call ath6kl_htc_cleanup() when it wants remove htc
2704 */
Kalle Valobdcd8172011-07-18 00:22:30 +03002705 ath6kl_hif_cleanup_scatter(target->dev->ar);
2706
2707fail_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 */
Kalle Vaload226ec2011-08-10 09:49:12 +03002720int ath6kl_htc_start(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002721{
2722 struct htc_packet *packet;
2723 int status;
2724
Kalle Valo5fe4dff2011-10-30 21:16:15 +02002725 memset(&target->dev->irq_proc_reg, 0,
2726 sizeof(target->dev->irq_proc_reg));
2727
Kalle Valobdcd8172011-07-18 00:22:30 +03002728 /* Disable interrupts at the chip level */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002729 ath6kl_hif_disable_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002730
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 */
Kalle Valo3c370392011-10-24 12:17:12 +03002742 ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
Kalle Valofa99e962011-10-24 12:16:55 +03002743 target->tgt_creds);
Kalle Valobdcd8172011-07-18 00:22:30 +03002744
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 */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002754 status = ath6kl_hif_unmask_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002755
2756 if (status)
Kalle Vaload226ec2011-08-10 09:49:12 +03002757 ath6kl_htc_stop(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002758
2759 return status;
2760}
2761
Kalle Valo8a8109162011-10-27 18:49:00 +03002762static 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
Kalle Valobdcd8172011-07-18 00:22:30 +03002800/* htc_stop: stop interrupt reception, and flush all queued buffers */
Kalle Vaload226ec2011-08-10 09:49:12 +03002801void ath6kl_htc_stop(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002802{
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 */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002812 ath6kl_hif_mask_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002813
Kalle Vaload226ec2011-08-10 09:49:12 +03002814 ath6kl_htc_flush_txep_all(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002815
Kalle Vaload226ec2011-08-10 09:49:12 +03002816 ath6kl_htc_flush_rx_buf(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002817
Kalle Valo8a8109162011-10-27 18:49:00 +03002818 ath6kl_htc_reset(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002819}
2820
Kalle Vaload226ec2011-08-10 09:49:12 +03002821void *ath6kl_htc_create(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +03002822{
2823 struct htc_target *target = NULL;
Kalle Valo8a8109162011-10-27 18:49:00 +03002824 int status = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +03002825
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;
Kalle Valo8a8109162011-10-27 18:49:00 +03002836 goto err_htc_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03002837 }
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;
Kalle Valobdcd8172011-07-18 00:22:30 +03002849 target->ep_waiting = ENDPOINT_MAX;
2850
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002851 status = ath6kl_hif_setup(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002852 if (status)
Kalle Valo8a8109162011-10-27 18:49:00 +03002853 goto err_htc_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03002854
Kalle Valo8a8109162011-10-27 18:49:00 +03002855 status = ath6kl_htc_reset(target);
2856 if (status)
2857 goto err_htc_cleanup;
Kalle Valobdcd8172011-07-18 00:22:30 +03002858
2859 return target;
Kalle Valo8a8109162011-10-27 18:49:00 +03002860
2861err_htc_cleanup:
2862 ath6kl_htc_cleanup(target);
2863
2864 return NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +03002865}
2866
2867/* cleanup the HTC instance */
Kalle Vaload226ec2011-08-10 09:49:12 +03002868void ath6kl_htc_cleanup(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002869{
2870 struct htc_packet *packet, *tmp_packet;
2871
Kalle Valo241b1282012-01-17 20:09:45 +02002872 /* 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);
Kalle Valobdcd8172011-07-18 00:22:30 +03002875
2876 list_for_each_entry_safe(packet, tmp_packet,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002877 &target->free_ctrl_txbuf, list) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002878 list_del(&packet->list);
2879 kfree(packet->buf_start);
2880 kfree(packet);
2881 }
2882
2883 list_for_each_entry_safe(packet, tmp_packet,
Kalle Valo96f1fad2012-03-07 20:03:57 +02002884 &target->free_ctrl_rxbuf, list) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002885 list_del(&packet->list);
2886 kfree(packet->buf_start);
2887 kfree(packet);
2888 }
2889
2890 kfree(target->dev);
2891 kfree(target);
2892}