blob: 840f1b3e16b35f5c81ccb17074e7fa110b9423c4 [file] [log] [blame]
Kalle Valobdcd8172011-07-18 00:22:30 +03001/*
2 * Copyright (c) 2007-2011 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "core.h"
Kalle Valo2e1cb232011-10-05 12:23:49 +030018#include "hif.h"
Kalle Valobdcd8172011-07-18 00:22:30 +030019#include "debug.h"
20#include "hif-ops.h"
21#include <asm/unaligned.h>
22
23#define CALC_TXRX_PADDED_LEN(dev, len) (__ALIGN_MASK((len), (dev)->block_mask))
24
Kalle Valodfa01042011-09-06 11:10:49 +030025static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
Vasanthakumar Thiagarajan94e532d2011-08-22 20:14:31 +053026{
27 u8 *align_addr;
28
29 if (!IS_ALIGNED((unsigned long) *buf, 4)) {
30 align_addr = PTR_ALIGN(*buf - 4, 4);
31 memmove(align_addr, *buf, len);
32 *buf = align_addr;
33 }
34}
35
Kalle Valodfa01042011-09-06 11:10:49 +030036static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
37 int ctrl0, int ctrl1)
Kalle Valobdcd8172011-07-18 00:22:30 +030038{
39 struct htc_frame_hdr *hdr;
40
41 packet->buf -= HTC_HDR_LENGTH;
42 hdr = (struct htc_frame_hdr *)packet->buf;
43
44 /* Endianess? */
45 put_unaligned((u16)packet->act_len, &hdr->payld_len);
46 hdr->flags = flags;
47 hdr->eid = packet->endpoint;
48 hdr->ctrl[0] = ctrl0;
49 hdr->ctrl[1] = ctrl1;
50}
51
52static void htc_reclaim_txctrl_buf(struct htc_target *target,
53 struct htc_packet *pkt)
54{
55 spin_lock_bh(&target->htc_lock);
56 list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
57 spin_unlock_bh(&target->htc_lock);
58}
59
60static struct htc_packet *htc_get_control_buf(struct htc_target *target,
61 bool tx)
62{
63 struct htc_packet *packet = NULL;
64 struct list_head *buf_list;
65
66 buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;
67
68 spin_lock_bh(&target->htc_lock);
69
70 if (list_empty(buf_list)) {
71 spin_unlock_bh(&target->htc_lock);
72 return NULL;
73 }
74
75 packet = list_first_entry(buf_list, struct htc_packet, list);
76 list_del(&packet->list);
77 spin_unlock_bh(&target->htc_lock);
78
79 if (tx)
80 packet->buf = packet->buf_start + HTC_HDR_LENGTH;
81
82 return packet;
83}
84
85static void htc_tx_comp_update(struct htc_target *target,
86 struct htc_endpoint *endpoint,
87 struct htc_packet *packet)
88{
89 packet->completion = NULL;
90 packet->buf += HTC_HDR_LENGTH;
91
92 if (!packet->status)
93 return;
94
95 ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
96 packet->status, packet->endpoint, packet->act_len,
97 packet->info.tx.cred_used);
98
99 /* on failure to submit, reclaim credits for this packet */
100 spin_lock_bh(&target->tx_lock);
101 endpoint->cred_dist.cred_to_dist +=
102 packet->info.tx.cred_used;
103 endpoint->cred_dist.txq_depth = get_queue_depth(&endpoint->txq);
104
Kalle Valo471e92f2011-10-13 15:21:37 +0300105 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300106 target->cred_dist_cntxt, &target->cred_dist_list);
107
108 ath6k_credit_distribute(target->cred_dist_cntxt,
109 &target->cred_dist_list,
110 HTC_CREDIT_DIST_SEND_COMPLETE);
111
112 spin_unlock_bh(&target->tx_lock);
113}
114
115static void htc_tx_complete(struct htc_endpoint *endpoint,
116 struct list_head *txq)
117{
118 if (list_empty(txq))
119 return;
120
Kalle Valoebf29c92011-10-13 15:21:15 +0300121 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300122 "htc tx complete ep %d pkts %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300123 endpoint->eid, get_queue_depth(txq));
124
125 ath6kl_tx_complete(endpoint->target->dev->ar, txq);
126}
127
128static void htc_tx_comp_handler(struct htc_target *target,
129 struct htc_packet *packet)
130{
131 struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
132 struct list_head container;
133
134 htc_tx_comp_update(target, endpoint, packet);
135 INIT_LIST_HEAD(&container);
136 list_add_tail(&packet->list, &container);
137 /* do completion */
138 htc_tx_complete(endpoint, &container);
139}
140
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530141static void htc_async_tx_scat_complete(struct htc_target *target,
142 struct hif_scatter_req *scat_req)
Kalle Valobdcd8172011-07-18 00:22:30 +0300143{
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530144 struct htc_endpoint *endpoint;
Kalle Valobdcd8172011-07-18 00:22:30 +0300145 struct htc_packet *packet;
146 struct list_head tx_compq;
147 int i;
148
149 INIT_LIST_HEAD(&tx_compq);
150
Kalle Valoebf29c92011-10-13 15:21:15 +0300151 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300152 "htc tx scat complete len %d entries %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300153 scat_req->len, scat_req->scat_entries);
154
155 if (scat_req->status)
156 ath6kl_err("send scatter req failed: %d\n", scat_req->status);
157
Vasanthakumar Thiagarajane041c7f2011-07-16 20:29:09 +0530158 packet = scat_req->scat_list[0].packet;
159 endpoint = &target->endpoint[packet->endpoint];
160
Kalle Valobdcd8172011-07-18 00:22:30 +0300161 /* walk through the scatter list and process */
162 for (i = 0; i < scat_req->scat_entries; i++) {
163 packet = scat_req->scat_list[i].packet;
164 if (!packet) {
165 WARN_ON(1);
166 return;
167 }
168
169 packet->status = scat_req->status;
170 htc_tx_comp_update(target, endpoint, packet);
171 list_add_tail(&packet->list, &tx_compq);
172 }
173
174 /* free scatter request */
175 hif_scatter_req_add(target->dev->ar, scat_req);
176
177 /* complete all packets */
178 htc_tx_complete(endpoint, &tx_compq);
179}
180
Kalle Valodfa01042011-09-06 11:10:49 +0300181static int ath6kl_htc_tx_issue(struct htc_target *target,
182 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +0300183{
184 int status;
185 bool sync = false;
186 u32 padded_len, send_len;
187
188 if (!packet->completion)
189 sync = true;
190
191 send_len = packet->act_len + HTC_HDR_LENGTH;
192
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530193 padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
Kalle Valobdcd8172011-07-18 00:22:30 +0300194
Kalle Valoebf29c92011-10-13 15:21:15 +0300195 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300196 "htc tx issue len %d padded_len %d mbox 0x%X %s\n",
197 send_len, padded_len,
198 target->dev->ar->mbox_info.htc_addr,
199 sync ? "sync" : "async");
Kalle Valobdcd8172011-07-18 00:22:30 +0300200
201 if (sync) {
202 status = hif_read_write_sync(target->dev->ar,
203 target->dev->ar->mbox_info.htc_addr,
204 packet->buf, padded_len,
205 HIF_WR_SYNC_BLOCK_INC);
206
207 packet->status = status;
Kalle Valo65d2bb12011-08-14 18:10:03 -0700208 packet->buf += HTC_HDR_LENGTH;
Kalle Valobdcd8172011-07-18 00:22:30 +0300209 } else
210 status = hif_write_async(target->dev->ar,
211 target->dev->ar->mbox_info.htc_addr,
212 packet->buf, padded_len,
213 HIF_WR_ASYNC_BLOCK_INC, packet);
214
215 return status;
216}
217
218static int htc_check_credits(struct htc_target *target,
219 struct htc_endpoint *ep, u8 *flags,
220 enum htc_endpoint_id eid, unsigned int len,
221 int *req_cred)
222{
223
224 *req_cred = (len > target->tgt_cred_sz) ?
225 DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;
226
Kalle Valo471e92f2011-10-13 15:21:37 +0300227 ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds required %d got %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300228 *req_cred, ep->cred_dist.credits);
229
230 if (ep->cred_dist.credits < *req_cred) {
231 if (eid == ENDPOINT_0)
232 return -EINVAL;
233
234 /* Seek more credits */
235 ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;
236
Kalle Valo471e92f2011-10-13 15:21:37 +0300237 ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds ctxt 0x%p dist 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300238 target->cred_dist_cntxt, &ep->cred_dist);
239
240 ath6k_seek_credits(target->cred_dist_cntxt, &ep->cred_dist);
241
242 ep->cred_dist.seek_cred = 0;
243
244 if (ep->cred_dist.credits < *req_cred) {
Kalle Valoebf29c92011-10-13 15:21:15 +0300245 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300246 "htc creds not enough credits for ep %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300247 eid);
248 return -EINVAL;
249 }
250 }
251
252 ep->cred_dist.credits -= *req_cred;
253 ep->ep_st.cred_cosumd += *req_cred;
254
255 /* When we are getting low on credits, ask for more */
256 if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
257 ep->cred_dist.seek_cred =
258 ep->cred_dist.cred_per_msg - ep->cred_dist.credits;
259
Kalle Valo471e92f2011-10-13 15:21:37 +0300260 ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds ctxt 0x%p dist 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300261 target->cred_dist_cntxt, &ep->cred_dist);
262
263 ath6k_seek_credits(target->cred_dist_cntxt, &ep->cred_dist);
264
265 /* see if we were successful in getting more */
266 if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
267 /* tell the target we need credits ASAP! */
268 *flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
269 ep->ep_st.cred_low_indicate += 1;
Kalle Valo471e92f2011-10-13 15:21:37 +0300270 ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds host needs credits\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300271 }
272 }
273
274 return 0;
275}
276
Kalle Valodfa01042011-09-06 11:10:49 +0300277static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
278 struct htc_endpoint *endpoint,
279 struct list_head *queue)
Kalle Valobdcd8172011-07-18 00:22:30 +0300280{
281 int req_cred;
282 u8 flags;
283 struct htc_packet *packet;
284 unsigned int len;
285
286 while (true) {
287
288 flags = 0;
289
290 if (list_empty(&endpoint->txq))
291 break;
292 packet = list_first_entry(&endpoint->txq, struct htc_packet,
293 list);
294
Kalle Valoebf29c92011-10-13 15:21:15 +0300295 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300296 "htc tx got packet 0x%p queue depth %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300297 packet, get_queue_depth(&endpoint->txq));
298
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530299 len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +0300300 packet->act_len + HTC_HDR_LENGTH);
301
302 if (htc_check_credits(target, endpoint, &flags,
303 packet->endpoint, len, &req_cred))
304 break;
305
306 /* now we can fully move onto caller's queue */
307 packet = list_first_entry(&endpoint->txq, struct htc_packet,
308 list);
309 list_move_tail(&packet->list, queue);
310
311 /* save the number of credits this packet consumed */
312 packet->info.tx.cred_used = req_cred;
313
314 /* all TX packets are handled asynchronously */
315 packet->completion = htc_tx_comp_handler;
316 packet->context = target;
317 endpoint->ep_st.tx_issued += 1;
318
319 /* save send flags */
320 packet->info.tx.flags = flags;
321 packet->info.tx.seqno = endpoint->seqno;
322 endpoint->seqno++;
323 }
324}
325
326/* See if the padded tx length falls on a credit boundary */
327static int htc_get_credit_padding(unsigned int cred_sz, int *len,
328 struct htc_endpoint *ep)
329{
330 int rem_cred, cred_pad;
331
332 rem_cred = *len % cred_sz;
333
334 /* No padding needed */
335 if (!rem_cred)
336 return 0;
337
338 if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
339 return -1;
340
341 /*
342 * The transfer consumes a "partial" credit, this
343 * packet cannot be bundled unless we add
344 * additional "dummy" padding (max 255 bytes) to
345 * consume the entire credit.
346 */
347 cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;
348
349 if ((cred_pad > 0) && (cred_pad <= 255))
350 *len += cred_pad;
351 else
352 /* The amount of padding is too large, send as non-bundled */
353 return -1;
354
355 return cred_pad;
356}
357
Kalle Valodfa01042011-09-06 11:10:49 +0300358static int ath6kl_htc_tx_setup_scat_list(struct htc_target *target,
359 struct htc_endpoint *endpoint,
360 struct hif_scatter_req *scat_req,
361 int n_scat,
362 struct list_head *queue)
Kalle Valobdcd8172011-07-18 00:22:30 +0300363{
364 struct htc_packet *packet;
365 int i, len, rem_scat, cred_pad;
366 int status = 0;
367
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +0530368 rem_scat = target->max_tx_bndl_sz;
Kalle Valobdcd8172011-07-18 00:22:30 +0300369
370 for (i = 0; i < n_scat; i++) {
371 scat_req->scat_list[i].packet = NULL;
372
373 if (list_empty(queue))
374 break;
375
376 packet = list_first_entry(queue, struct htc_packet, list);
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530377 len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +0300378 packet->act_len + HTC_HDR_LENGTH);
379
380 cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
381 &len, endpoint);
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530382 if (cred_pad < 0 || rem_scat < len) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300383 status = -ENOSPC;
384 break;
385 }
386
387 rem_scat -= len;
388 /* now remove it from the queue */
Kalle Valobdcd8172011-07-18 00:22:30 +0300389 list_del(&packet->list);
390
391 scat_req->scat_list[i].packet = packet;
392 /* prepare packet and flag message as part of a send bundle */
Kalle Valodfa01042011-09-06 11:10:49 +0300393 ath6kl_htc_tx_prep_pkt(packet,
Kalle Valobdcd8172011-07-18 00:22:30 +0300394 packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE,
395 cred_pad, packet->info.tx.seqno);
Vasanthakumar Thiagarajan94e532d2011-08-22 20:14:31 +0530396 /* Make sure the buffer is 4-byte aligned */
Kalle Valodfa01042011-09-06 11:10:49 +0300397 ath6kl_htc_tx_buf_align(&packet->buf,
398 packet->act_len + HTC_HDR_LENGTH);
Kalle Valobdcd8172011-07-18 00:22:30 +0300399 scat_req->scat_list[i].buf = packet->buf;
400 scat_req->scat_list[i].len = len;
401
402 scat_req->len += len;
403 scat_req->scat_entries++;
Kalle Valoebf29c92011-10-13 15:21:15 +0300404 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300405 "htc tx adding (%d) pkt 0x%p len %d remaining %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300406 i, packet, len, rem_scat);
407 }
408
409 /* Roll back scatter setup in case of any failure */
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530410 if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300411 for (i = scat_req->scat_entries - 1; i >= 0; i--) {
412 packet = scat_req->scat_list[i].packet;
413 if (packet) {
414 packet->buf += HTC_HDR_LENGTH;
415 list_add(&packet->list, queue);
416 }
417 }
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530418 return -EAGAIN;
Kalle Valobdcd8172011-07-18 00:22:30 +0300419 }
420
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530421 return status;
Kalle Valobdcd8172011-07-18 00:22:30 +0300422}
423
424/*
Kalle Valodfa01042011-09-06 11:10:49 +0300425 * Drain a queue and send as bundles this function may return without fully
426 * draining the queue when
Kalle Valobdcd8172011-07-18 00:22:30 +0300427 *
428 * 1. scatter resources are exhausted
429 * 2. a message that will consume a partial credit will stop the
430 * bundling process early
431 * 3. we drop below the minimum number of messages for a bundle
432 */
Kalle Valodfa01042011-09-06 11:10:49 +0300433static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
434 struct list_head *queue,
435 int *sent_bundle, int *n_bundle_pkts)
Kalle Valobdcd8172011-07-18 00:22:30 +0300436{
437 struct htc_target *target = endpoint->target;
438 struct hif_scatter_req *scat_req = NULL;
Kalle Valobdcd8172011-07-18 00:22:30 +0300439 int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0;
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530440 int status;
Kalle Valobdcd8172011-07-18 00:22:30 +0300441
Kalle Valobdcd8172011-07-18 00:22:30 +0300442 while (true) {
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530443 status = 0;
Kalle Valobdcd8172011-07-18 00:22:30 +0300444 n_scat = get_queue_depth(queue);
445 n_scat = min(n_scat, target->msg_per_bndl_max);
446
447 if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
448 /* not enough to bundle */
449 break;
450
451 scat_req = hif_scatter_req_get(target->dev->ar);
452
453 if (!scat_req) {
454 /* no scatter resources */
Kalle Valoebf29c92011-10-13 15:21:15 +0300455 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300456 "htc tx no more scatter resources\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300457 break;
458 }
459
Kalle Valo471e92f2011-10-13 15:21:37 +0300460 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300461 n_scat);
462
463 scat_req->len = 0;
464 scat_req->scat_entries = 0;
465
Kalle Valodfa01042011-09-06 11:10:49 +0300466 status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
467 scat_req, n_scat,
468 queue);
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530469 if (status == -EAGAIN) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300470 hif_scatter_req_add(target->dev->ar, scat_req);
471 break;
472 }
473
474 /* send path is always asynchronous */
475 scat_req->complete = htc_async_tx_scat_complete;
Kalle Valobdcd8172011-07-18 00:22:30 +0300476 n_sent_bundle++;
477 tot_pkts_bundle += scat_req->scat_entries;
478
Kalle Valoebf29c92011-10-13 15:21:15 +0300479 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300480 "htc tx scatter bytes %d entries %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300481 scat_req->len, scat_req->scat_entries);
Kalle Valo8e8ddb22011-10-05 12:23:33 +0300482 ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
Vasanthakumar Thiagarajanf7a7e7a2011-08-22 20:40:22 +0530483
484 if (status)
485 break;
Kalle Valobdcd8172011-07-18 00:22:30 +0300486 }
487
488 *sent_bundle = n_sent_bundle;
489 *n_bundle_pkts = tot_pkts_bundle;
Kalle Valo471e92f2011-10-13 15:21:37 +0300490 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
491 n_sent_bundle);
Kalle Valobdcd8172011-07-18 00:22:30 +0300492
493 return;
494}
495
Kalle Valodfa01042011-09-06 11:10:49 +0300496static void ath6kl_htc_tx_from_queue(struct htc_target *target,
497 struct htc_endpoint *endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +0300498{
499 struct list_head txq;
500 struct htc_packet *packet;
501 int bundle_sent;
502 int n_pkts_bundle;
503
504 spin_lock_bh(&target->tx_lock);
505
506 endpoint->tx_proc_cnt++;
507 if (endpoint->tx_proc_cnt > 1) {
508 endpoint->tx_proc_cnt--;
509 spin_unlock_bh(&target->tx_lock);
Kalle Valo471e92f2011-10-13 15:21:37 +0300510 ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
Kalle Valobdcd8172011-07-18 00:22:30 +0300511 return;
512 }
513
514 /*
515 * drain the endpoint TX queue for transmission as long
516 * as we have enough credits.
517 */
518 INIT_LIST_HEAD(&txq);
519
520 while (true) {
521
522 if (list_empty(&endpoint->txq))
523 break;
524
Kalle Valodfa01042011-09-06 11:10:49 +0300525 ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
Kalle Valobdcd8172011-07-18 00:22:30 +0300526
527 if (list_empty(&txq))
528 break;
529
530 spin_unlock_bh(&target->tx_lock);
531
532 bundle_sent = 0;
533 n_pkts_bundle = 0;
534
535 while (true) {
536 /* try to send a bundle on each pass */
537 if ((target->tx_bndl_enable) &&
538 (get_queue_depth(&txq) >=
539 HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
540 int temp1 = 0, temp2 = 0;
541
Kalle Valodfa01042011-09-06 11:10:49 +0300542 ath6kl_htc_tx_bundle(endpoint, &txq,
543 &temp1, &temp2);
Kalle Valobdcd8172011-07-18 00:22:30 +0300544 bundle_sent += temp1;
545 n_pkts_bundle += temp2;
546 }
547
548 if (list_empty(&txq))
549 break;
550
551 packet = list_first_entry(&txq, struct htc_packet,
552 list);
553 list_del(&packet->list);
554
Kalle Valodfa01042011-09-06 11:10:49 +0300555 ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
556 0, packet->info.tx.seqno);
557 ath6kl_htc_tx_issue(target, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +0300558 }
559
560 spin_lock_bh(&target->tx_lock);
561
562 endpoint->ep_st.tx_bundles += bundle_sent;
563 endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
564 }
565
566 endpoint->tx_proc_cnt = 0;
567 spin_unlock_bh(&target->tx_lock);
568}
569
Kalle Valodfa01042011-09-06 11:10:49 +0300570static bool ath6kl_htc_tx_try(struct htc_target *target,
571 struct htc_endpoint *endpoint,
572 struct htc_packet *tx_pkt)
Kalle Valobdcd8172011-07-18 00:22:30 +0300573{
574 struct htc_ep_callbacks ep_cb;
575 int txq_depth;
576 bool overflow = false;
577
578 ep_cb = endpoint->ep_cb;
579
580 spin_lock_bh(&target->tx_lock);
581 txq_depth = get_queue_depth(&endpoint->txq);
582 spin_unlock_bh(&target->tx_lock);
583
584 if (txq_depth >= endpoint->max_txq_depth)
585 overflow = true;
586
587 if (overflow)
Kalle Valoebf29c92011-10-13 15:21:15 +0300588 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300589 "htc tx overflow ep %d depth %d max %d\n",
590 endpoint->eid, txq_depth,
Kalle Valobdcd8172011-07-18 00:22:30 +0300591 endpoint->max_txq_depth);
592
593 if (overflow && ep_cb.tx_full) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300594 if (ep_cb.tx_full(endpoint->target, tx_pkt) ==
595 HTC_SEND_FULL_DROP) {
596 endpoint->ep_st.tx_dropped += 1;
597 return false;
598 }
599 }
600
601 spin_lock_bh(&target->tx_lock);
602 list_add_tail(&tx_pkt->list, &endpoint->txq);
603 spin_unlock_bh(&target->tx_lock);
604
Kalle Valodfa01042011-09-06 11:10:49 +0300605 ath6kl_htc_tx_from_queue(target, endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +0300606
607 return true;
608}
609
610static void htc_chk_ep_txq(struct htc_target *target)
611{
612 struct htc_endpoint *endpoint;
613 struct htc_endpoint_credit_dist *cred_dist;
614
615 /*
616 * Run through the credit distribution list to see if there are
617 * packets queued. NOTE: no locks need to be taken since the
618 * distribution list is not dynamic (cannot be re-ordered) and we
619 * are not modifying any state.
620 */
621 list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
622 endpoint = (struct htc_endpoint *)cred_dist->htc_rsvd;
623
624 spin_lock_bh(&target->tx_lock);
625 if (!list_empty(&endpoint->txq)) {
Kalle Valoebf29c92011-10-13 15:21:15 +0300626 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300627 "htc creds ep %d credits %d pkts %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300628 cred_dist->endpoint,
629 endpoint->cred_dist.credits,
630 get_queue_depth(&endpoint->txq));
631 spin_unlock_bh(&target->tx_lock);
632 /*
633 * Try to start the stalled queue, this list is
634 * ordered by priority. If there are credits
635 * available the highest priority queue will get a
636 * chance to reclaim credits from lower priority
637 * ones.
638 */
Kalle Valodfa01042011-09-06 11:10:49 +0300639 ath6kl_htc_tx_from_queue(target, endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +0300640 spin_lock_bh(&target->tx_lock);
641 }
642 spin_unlock_bh(&target->tx_lock);
643 }
644}
645
646static int htc_setup_tx_complete(struct htc_target *target)
647{
648 struct htc_packet *send_pkt = NULL;
649 int status;
650
651 send_pkt = htc_get_control_buf(target, true);
652
653 if (!send_pkt)
654 return -ENOMEM;
655
656 if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
657 struct htc_setup_comp_ext_msg *setup_comp_ext;
658 u32 flags = 0;
659
660 setup_comp_ext =
661 (struct htc_setup_comp_ext_msg *)send_pkt->buf;
662 memset(setup_comp_ext, 0, sizeof(*setup_comp_ext));
663 setup_comp_ext->msg_id =
664 cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
665
666 if (target->msg_per_bndl_max > 0) {
667 /* Indicate HTC bundling to the target */
668 flags |= HTC_SETUP_COMP_FLG_RX_BNDL_EN;
669 setup_comp_ext->msg_per_rxbndl =
670 target->msg_per_bndl_max;
671 }
672
673 memcpy(&setup_comp_ext->flags, &flags,
674 sizeof(setup_comp_ext->flags));
675 set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp_ext,
676 sizeof(struct htc_setup_comp_ext_msg),
677 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
678
679 } else {
680 struct htc_setup_comp_msg *setup_comp;
681 setup_comp = (struct htc_setup_comp_msg *)send_pkt->buf;
682 memset(setup_comp, 0, sizeof(struct htc_setup_comp_msg));
683 setup_comp->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID);
684 set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp,
685 sizeof(struct htc_setup_comp_msg),
686 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
687 }
688
689 /* we want synchronous operation */
690 send_pkt->completion = NULL;
Kalle Valodfa01042011-09-06 11:10:49 +0300691 ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
692 status = ath6kl_htc_tx_issue(target, send_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +0300693
694 if (send_pkt != NULL)
695 htc_reclaim_txctrl_buf(target, send_pkt);
696
697 return status;
698}
699
Kalle Vaload226ec2011-08-10 09:49:12 +0300700void ath6kl_htc_set_credit_dist(struct htc_target *target,
701 struct htc_credit_state_info *cred_dist_cntxt,
702 u16 srvc_pri_order[], int list_len)
Kalle Valobdcd8172011-07-18 00:22:30 +0300703{
704 struct htc_endpoint *endpoint;
705 int i, ep;
706
707 target->cred_dist_cntxt = cred_dist_cntxt;
708
709 list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
710 &target->cred_dist_list);
711
712 for (i = 0; i < list_len; i++) {
713 for (ep = ENDPOINT_1; ep < ENDPOINT_MAX; ep++) {
714 endpoint = &target->endpoint[ep];
715 if (endpoint->svc_id == srvc_pri_order[i]) {
716 list_add_tail(&endpoint->cred_dist.list,
717 &target->cred_dist_list);
718 break;
719 }
720 }
721 if (ep >= ENDPOINT_MAX) {
722 WARN_ON(1);
723 return;
724 }
725 }
726}
727
Kalle Vaload226ec2011-08-10 09:49:12 +0300728int ath6kl_htc_tx(struct htc_target *target, struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +0300729{
730 struct htc_endpoint *endpoint;
731 struct list_head queue;
732
Kalle Valoebf29c92011-10-13 15:21:15 +0300733 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300734 "htc tx ep id %d buf 0x%p len %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300735 packet->endpoint, packet->buf, packet->act_len);
736
737 if (packet->endpoint >= ENDPOINT_MAX) {
738 WARN_ON(1);
739 return -EINVAL;
740 }
741
742 endpoint = &target->endpoint[packet->endpoint];
743
Kalle Valodfa01042011-09-06 11:10:49 +0300744 if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
Kalle Valobdcd8172011-07-18 00:22:30 +0300745 packet->status = (target->htc_flags & HTC_OP_STATE_STOPPING) ?
746 -ECANCELED : -ENOSPC;
747 INIT_LIST_HEAD(&queue);
748 list_add(&packet->list, &queue);
749 htc_tx_complete(endpoint, &queue);
750 }
751
752 return 0;
753}
754
755/* flush endpoint TX queue */
Kalle Vaload226ec2011-08-10 09:49:12 +0300756void ath6kl_htc_flush_txep(struct htc_target *target,
757 enum htc_endpoint_id eid, u16 tag)
Kalle Valobdcd8172011-07-18 00:22:30 +0300758{
759 struct htc_packet *packet, *tmp_pkt;
760 struct list_head discard_q, container;
761 struct htc_endpoint *endpoint = &target->endpoint[eid];
762
763 if (!endpoint->svc_id) {
764 WARN_ON(1);
765 return;
766 }
767
768 /* initialize the discard queue */
769 INIT_LIST_HEAD(&discard_q);
770
771 spin_lock_bh(&target->tx_lock);
772
773 list_for_each_entry_safe(packet, tmp_pkt, &endpoint->txq, list) {
774 if ((tag == HTC_TX_PACKET_TAG_ALL) ||
775 (tag == packet->info.tx.tag))
776 list_move_tail(&packet->list, &discard_q);
777 }
778
779 spin_unlock_bh(&target->tx_lock);
780
781 list_for_each_entry_safe(packet, tmp_pkt, &discard_q, list) {
782 packet->status = -ECANCELED;
783 list_del(&packet->list);
Kalle Valoebf29c92011-10-13 15:21:15 +0300784 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300785 "htc tx flushing pkt 0x%p len %d ep %d tag 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300786 packet, packet->act_len,
787 packet->endpoint, packet->info.tx.tag);
788
789 INIT_LIST_HEAD(&container);
790 list_add_tail(&packet->list, &container);
791 htc_tx_complete(endpoint, &container);
792 }
793
794}
795
Kalle Vaload226ec2011-08-10 09:49:12 +0300796static void ath6kl_htc_flush_txep_all(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +0300797{
798 struct htc_endpoint *endpoint;
799 int i;
800
801 dump_cred_dist_stats(target);
802
803 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
804 endpoint = &target->endpoint[i];
805 if (endpoint->svc_id == 0)
806 /* not in use.. */
807 continue;
Kalle Vaload226ec2011-08-10 09:49:12 +0300808 ath6kl_htc_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
Kalle Valobdcd8172011-07-18 00:22:30 +0300809 }
810}
811
Kalle Vaload226ec2011-08-10 09:49:12 +0300812void ath6kl_htc_indicate_activity_change(struct htc_target *target,
813 enum htc_endpoint_id eid, bool active)
Kalle Valobdcd8172011-07-18 00:22:30 +0300814{
815 struct htc_endpoint *endpoint = &target->endpoint[eid];
816 bool dist = false;
817
818 if (endpoint->svc_id == 0) {
819 WARN_ON(1);
820 return;
821 }
822
823 spin_lock_bh(&target->tx_lock);
824
825 if (active) {
826 if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
827 endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
828 dist = true;
829 }
830 } else {
831 if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
832 endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
833 dist = true;
834 }
835 }
836
837 if (dist) {
838 endpoint->cred_dist.txq_depth =
839 get_queue_depth(&endpoint->txq);
840
Kalle Valo471e92f2011-10-13 15:21:37 +0300841 ath6kl_dbg(ATH6KL_DBG_HTC,
842 "htc tx activity ctxt 0x%p dist 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300843 target->cred_dist_cntxt, &target->cred_dist_list);
844
845 ath6k_credit_distribute(target->cred_dist_cntxt,
846 &target->cred_dist_list,
847 HTC_CREDIT_DIST_ACTIVITY_CHANGE);
848 }
849
850 spin_unlock_bh(&target->tx_lock);
851
852 if (dist && !active)
853 htc_chk_ep_txq(target);
854}
855
856/* HTC Rx */
857
Kalle Valo689def92011-09-06 11:10:49 +0300858static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
859 int n_look_ahds)
Kalle Valobdcd8172011-07-18 00:22:30 +0300860{
861 endpoint->ep_st.rx_pkts++;
862 if (n_look_ahds == 1)
863 endpoint->ep_st.rx_lkahds++;
864 else if (n_look_ahds > 1)
865 endpoint->ep_st.rx_bundle_lkahd++;
866}
867
868static inline bool htc_valid_rx_frame_len(struct htc_target *target,
869 enum htc_endpoint_id eid, int len)
870{
871 return (eid == target->dev->ar->ctrl_ep) ?
872 len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
873}
874
875static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
876{
877 struct list_head queue;
878
879 INIT_LIST_HEAD(&queue);
880 list_add_tail(&packet->list, &queue);
Kalle Vaload226ec2011-08-10 09:49:12 +0300881 return ath6kl_htc_add_rxbuf_multiple(target, &queue);
Kalle Valobdcd8172011-07-18 00:22:30 +0300882}
883
884static void htc_reclaim_rxbuf(struct htc_target *target,
885 struct htc_packet *packet,
886 struct htc_endpoint *ep)
887{
888 if (packet->info.rx.rx_flags & HTC_RX_PKT_NO_RECYCLE) {
889 htc_rxpkt_reset(packet);
890 packet->status = -ECANCELED;
891 ep->ep_cb.rx(ep->target, packet);
892 } else {
893 htc_rxpkt_reset(packet);
894 htc_add_rxbuf((void *)(target), packet);
895 }
896}
897
898static void reclaim_rx_ctrl_buf(struct htc_target *target,
899 struct htc_packet *packet)
900{
901 spin_lock_bh(&target->htc_lock);
902 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
903 spin_unlock_bh(&target->htc_lock);
904}
905
Kalle Valo689def92011-09-06 11:10:49 +0300906static int ath6kl_htc_rx_packet(struct htc_target *target,
907 struct htc_packet *packet,
908 u32 rx_len)
Kalle Valobdcd8172011-07-18 00:22:30 +0300909{
910 struct ath6kl_device *dev = target->dev;
911 u32 padded_len;
912 int status;
913
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530914 padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
Kalle Valobdcd8172011-07-18 00:22:30 +0300915
916 if (padded_len > packet->buf_len) {
Kalle Valo471e92f2011-10-13 15:21:37 +0300917 ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300918 padded_len, rx_len, packet->buf_len);
919 return -ENOMEM;
920 }
921
Kalle Valoebf29c92011-10-13 15:21:15 +0300922 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +0300923 "htc rx 0x%p hdr x%x len %d mbox 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +0300924 packet, packet->info.rx.exp_hdr,
Kalle Valo471e92f2011-10-13 15:21:37 +0300925 padded_len, dev->ar->mbox_info.htc_addr);
Kalle Valobdcd8172011-07-18 00:22:30 +0300926
927 status = hif_read_write_sync(dev->ar,
928 dev->ar->mbox_info.htc_addr,
929 packet->buf, padded_len,
930 HIF_RD_SYNC_BLOCK_FIX);
931
932 packet->status = status;
933
934 return status;
935}
936
937/*
938 * optimization for recv packets, we can indicate a
939 * "hint" that there are more single-packets to fetch
940 * on this endpoint.
941 */
Kalle Valo689def92011-09-06 11:10:49 +0300942static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
943 struct htc_endpoint *endpoint,
944 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +0300945{
946 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;
947
948 if (htc_hdr->eid == packet->endpoint) {
949 if (!list_empty(&endpoint->rx_bufq))
950 packet->info.rx.indicat_flags |=
951 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
952 }
953}
954
Kalle Valo689def92011-09-06 11:10:49 +0300955static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +0300956{
957 struct htc_ep_callbacks ep_cb = endpoint->ep_cb;
958
959 if (ep_cb.rx_refill_thresh > 0) {
960 spin_lock_bh(&endpoint->target->rx_lock);
961 if (get_queue_depth(&endpoint->rx_bufq)
962 < ep_cb.rx_refill_thresh) {
963 spin_unlock_bh(&endpoint->target->rx_lock);
964 ep_cb.rx_refill(endpoint->target, endpoint->eid);
965 return;
966 }
967 spin_unlock_bh(&endpoint->target->rx_lock);
968 }
969}
970
971/* This function is called with rx_lock held */
Kalle Valo689def92011-09-06 11:10:49 +0300972static int ath6kl_htc_rx_setup(struct htc_target *target,
973 struct htc_endpoint *ep,
974 u32 *lk_ahds, struct list_head *queue, int n_msg)
Kalle Valobdcd8172011-07-18 00:22:30 +0300975{
976 struct htc_packet *packet;
977 /* FIXME: type of lk_ahds can't be right */
978 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
979 struct htc_ep_callbacks ep_cb;
980 int status = 0, j, full_len;
981 bool no_recycle;
982
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +0530983 full_len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +0300984 le16_to_cpu(htc_hdr->payld_len) +
985 sizeof(*htc_hdr));
986
987 if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
988 ath6kl_warn("Rx buffer requested with invalid length\n");
989 return -EINVAL;
990 }
991
992 ep_cb = ep->ep_cb;
993 for (j = 0; j < n_msg; j++) {
994
995 /*
996 * Reset flag, any packets allocated using the
997 * rx_alloc() API cannot be recycled on
998 * cleanup,they must be explicitly returned.
999 */
1000 no_recycle = false;
1001
1002 if (ep_cb.rx_allocthresh &&
1003 (full_len > ep_cb.rx_alloc_thresh)) {
1004 ep->ep_st.rx_alloc_thresh_hit += 1;
1005 ep->ep_st.rxalloc_thresh_byte +=
1006 le16_to_cpu(htc_hdr->payld_len);
1007
1008 spin_unlock_bh(&target->rx_lock);
1009 no_recycle = true;
1010
1011 packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
1012 full_len);
1013 spin_lock_bh(&target->rx_lock);
1014 } else {
1015 /* refill handler is being used */
1016 if (list_empty(&ep->rx_bufq)) {
1017 if (ep_cb.rx_refill) {
1018 spin_unlock_bh(&target->rx_lock);
1019 ep_cb.rx_refill(ep->target, ep->eid);
1020 spin_lock_bh(&target->rx_lock);
1021 }
1022 }
1023
1024 if (list_empty(&ep->rx_bufq))
1025 packet = NULL;
1026 else {
1027 packet = list_first_entry(&ep->rx_bufq,
1028 struct htc_packet, list);
1029 list_del(&packet->list);
1030 }
1031 }
1032
1033 if (!packet) {
1034 target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
1035 target->ep_waiting = ep->eid;
1036 return -ENOSPC;
1037 }
1038
1039 /* clear flags */
1040 packet->info.rx.rx_flags = 0;
1041 packet->info.rx.indicat_flags = 0;
1042 packet->status = 0;
1043
1044 if (no_recycle)
1045 /*
1046 * flag that these packets cannot be
1047 * recycled, they have to be returned to
1048 * the user
1049 */
1050 packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;
1051
1052 /* Caller needs to free this upon any failure */
1053 list_add_tail(&packet->list, queue);
1054
1055 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1056 status = -ECANCELED;
1057 break;
1058 }
1059
1060 if (j) {
1061 packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
1062 packet->info.rx.exp_hdr = 0xFFFFFFFF;
1063 } else
1064 /* set expected look ahead */
1065 packet->info.rx.exp_hdr = *lk_ahds;
1066
1067 packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
1068 HTC_HDR_LENGTH;
1069 }
1070
1071 return status;
1072}
1073
Kalle Valo689def92011-09-06 11:10:49 +03001074static int ath6kl_htc_rx_alloc(struct htc_target *target,
1075 u32 lk_ahds[], int msg,
1076 struct htc_endpoint *endpoint,
1077 struct list_head *queue)
Kalle Valobdcd8172011-07-18 00:22:30 +03001078{
1079 int status = 0;
1080 struct htc_packet *packet, *tmp_pkt;
1081 struct htc_frame_hdr *htc_hdr;
1082 int i, n_msg;
1083
1084 spin_lock_bh(&target->rx_lock);
1085
1086 for (i = 0; i < msg; i++) {
1087
1088 htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];
1089
1090 if (htc_hdr->eid >= ENDPOINT_MAX) {
1091 ath6kl_err("invalid ep in look-ahead: %d\n",
1092 htc_hdr->eid);
1093 status = -ENOMEM;
1094 break;
1095 }
1096
1097 if (htc_hdr->eid != endpoint->eid) {
1098 ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
1099 htc_hdr->eid, endpoint->eid, i);
1100 status = -ENOMEM;
1101 break;
1102 }
1103
1104 if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
1105 ath6kl_err("payload len %d exceeds max htc : %d !\n",
1106 htc_hdr->payld_len,
1107 (u32) HTC_MAX_PAYLOAD_LENGTH);
1108 status = -ENOMEM;
1109 break;
1110 }
1111
1112 if (endpoint->svc_id == 0) {
1113 ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
1114 status = -ENOMEM;
1115 break;
1116 }
1117
1118 if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
1119 /*
1120 * HTC header indicates that every packet to follow
1121 * has the same padded length so that it can be
1122 * optimally fetched as a full bundle.
1123 */
1124 n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
1125 HTC_FLG_RX_BNDL_CNT_S;
1126
1127 /* the count doesn't include the starter frame */
1128 n_msg++;
1129 if (n_msg > target->msg_per_bndl_max) {
1130 status = -ENOMEM;
1131 break;
1132 }
1133
1134 endpoint->ep_st.rx_bundle_from_hdr += 1;
Kalle Valoebf29c92011-10-13 15:21:15 +03001135 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001136 "htc rx bundle pkts %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001137 n_msg);
1138 } else
1139 /* HTC header only indicates 1 message to fetch */
1140 n_msg = 1;
1141
1142 /* Setup packet buffers for each message */
Kalle Valo689def92011-09-06 11:10:49 +03001143 status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
1144 queue, n_msg);
Kalle Valobdcd8172011-07-18 00:22:30 +03001145
1146 /*
1147 * This is due to unavailabilty of buffers to rx entire data.
1148 * Return no error so that free buffers from queue can be used
1149 * to receive partial data.
1150 */
1151 if (status == -ENOSPC) {
1152 spin_unlock_bh(&target->rx_lock);
1153 return 0;
1154 }
1155
1156 if (status)
1157 break;
1158 }
1159
1160 spin_unlock_bh(&target->rx_lock);
1161
1162 if (status) {
1163 list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
1164 list_del(&packet->list);
1165 htc_reclaim_rxbuf(target, packet,
1166 &target->endpoint[packet->endpoint]);
1167 }
1168 }
1169
1170 return status;
1171}
1172
1173static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
1174{
1175 if (packets->endpoint != ENDPOINT_0) {
1176 WARN_ON(1);
1177 return;
1178 }
1179
1180 if (packets->status == -ECANCELED) {
1181 reclaim_rx_ctrl_buf(context, packets);
1182 return;
1183 }
1184
1185 if (packets->act_len > 0) {
1186 ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
1187 packets->act_len + HTC_HDR_LENGTH);
1188
Kalle Valo471e92f2011-10-13 15:21:37 +03001189 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1190 "htc rx unexpected endpoint 0 message", "",
Kalle Valoef094102011-09-27 14:30:45 +03001191 packets->buf - HTC_HDR_LENGTH,
1192 packets->act_len + HTC_HDR_LENGTH);
Kalle Valobdcd8172011-07-18 00:22:30 +03001193 }
1194
1195 htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
1196}
1197
1198static void htc_proc_cred_rpt(struct htc_target *target,
1199 struct htc_credit_report *rpt,
1200 int n_entries,
1201 enum htc_endpoint_id from_ep)
1202{
1203 struct htc_endpoint *endpoint;
1204 int tot_credits = 0, i;
1205 bool dist = false;
1206
Kalle Valoebf29c92011-10-13 15:21:15 +03001207 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001208 "htc creds report entries %d\n", n_entries);
Kalle Valobdcd8172011-07-18 00:22:30 +03001209
1210 spin_lock_bh(&target->tx_lock);
1211
1212 for (i = 0; i < n_entries; i++, rpt++) {
1213 if (rpt->eid >= ENDPOINT_MAX) {
1214 WARN_ON(1);
1215 spin_unlock_bh(&target->tx_lock);
1216 return;
1217 }
1218
1219 endpoint = &target->endpoint[rpt->eid];
1220
Kalle Valo471e92f2011-10-13 15:21:37 +03001221 ath6kl_dbg(ATH6KL_DBG_HTC,
1222 "htc creds report ep %d credits %d\n",
1223 rpt->eid, rpt->credits);
Kalle Valobdcd8172011-07-18 00:22:30 +03001224
1225 endpoint->ep_st.tx_cred_rpt += 1;
1226 endpoint->ep_st.cred_retnd += rpt->credits;
1227
1228 if (from_ep == rpt->eid) {
1229 /*
1230 * This credit report arrived on the same endpoint
1231 * indicating it arrived in an RX packet.
1232 */
1233 endpoint->ep_st.cred_from_rx += rpt->credits;
1234 endpoint->ep_st.cred_rpt_from_rx += 1;
1235 } else if (from_ep == ENDPOINT_0) {
1236 /* credit arrived on endpoint 0 as a NULL message */
1237 endpoint->ep_st.cred_from_ep0 += rpt->credits;
1238 endpoint->ep_st.cred_rpt_ep0 += 1;
1239 } else {
1240 endpoint->ep_st.cred_from_other += rpt->credits;
1241 endpoint->ep_st.cred_rpt_from_other += 1;
1242 }
1243
Raja Mani5ba3ee42011-07-19 19:27:31 +05301244 if (rpt->eid == ENDPOINT_0)
Kalle Valobdcd8172011-07-18 00:22:30 +03001245 /* always give endpoint 0 credits back */
1246 endpoint->cred_dist.credits += rpt->credits;
1247 else {
1248 endpoint->cred_dist.cred_to_dist += rpt->credits;
1249 dist = true;
1250 }
1251
1252 /*
1253 * Refresh tx depth for distribution function that will
1254 * recover these credits NOTE: this is only valid when
1255 * there are credits to recover!
1256 */
1257 endpoint->cred_dist.txq_depth =
1258 get_queue_depth(&endpoint->txq);
1259
1260 tot_credits += rpt->credits;
1261 }
1262
Kalle Valoebf29c92011-10-13 15:21:15 +03001263 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001264 "htc creds report tot_credits %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001265 tot_credits);
1266
1267 if (dist) {
1268 /*
1269 * This was a credit return based on a completed send
1270 * operations note, this is done with the lock held
1271 */
Kalle Valo471e92f2011-10-13 15:21:37 +03001272 ath6kl_dbg(ATH6KL_DBG_HTC, "htc creds ctxt 0x%p dist 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001273 target->cred_dist_cntxt, &target->cred_dist_list);
1274
1275 ath6k_credit_distribute(target->cred_dist_cntxt,
1276 &target->cred_dist_list,
1277 HTC_CREDIT_DIST_SEND_COMPLETE);
1278 }
1279
1280 spin_unlock_bh(&target->tx_lock);
1281
1282 if (tot_credits)
1283 htc_chk_ep_txq(target);
1284}
1285
1286static int htc_parse_trailer(struct htc_target *target,
1287 struct htc_record_hdr *record,
1288 u8 *record_buf, u32 *next_lk_ahds,
1289 enum htc_endpoint_id endpoint,
1290 int *n_lk_ahds)
1291{
1292 struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
1293 struct htc_lookahead_report *lk_ahd;
1294 int len;
1295
1296 switch (record->rec_id) {
1297 case HTC_RECORD_CREDITS:
1298 len = record->len / sizeof(struct htc_credit_report);
1299 if (!len) {
1300 WARN_ON(1);
1301 return -EINVAL;
1302 }
1303
1304 htc_proc_cred_rpt(target,
1305 (struct htc_credit_report *) record_buf,
1306 len, endpoint);
1307 break;
1308 case HTC_RECORD_LOOKAHEAD:
1309 len = record->len / sizeof(*lk_ahd);
1310 if (!len) {
1311 WARN_ON(1);
1312 return -EINVAL;
1313 }
1314
1315 lk_ahd = (struct htc_lookahead_report *) record_buf;
1316 if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF))
1317 && next_lk_ahds) {
1318
Kalle Valoebf29c92011-10-13 15:21:15 +03001319 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001320 "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001321 lk_ahd->pre_valid, lk_ahd->post_valid);
1322
1323 /* look ahead bytes are valid, copy them over */
1324 memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);
1325
Kalle Valo471e92f2011-10-13 15:21:37 +03001326 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1327 "htc rx next look ahead",
Kalle Valoef094102011-09-27 14:30:45 +03001328 "", next_lk_ahds, 4);
Kalle Valobdcd8172011-07-18 00:22:30 +03001329
1330 *n_lk_ahds = 1;
1331 }
1332 break;
1333 case HTC_RECORD_LOOKAHEAD_BUNDLE:
1334 len = record->len / sizeof(*bundle_lkahd_rpt);
1335 if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
1336 WARN_ON(1);
1337 return -EINVAL;
1338 }
1339
1340 if (next_lk_ahds) {
1341 int i;
1342
1343 bundle_lkahd_rpt =
1344 (struct htc_bundle_lkahd_rpt *) record_buf;
1345
Kalle Valo471e92f2011-10-13 15:21:37 +03001346 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
Kalle Valoef094102011-09-27 14:30:45 +03001347 "", record_buf, record->len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001348
1349 for (i = 0; i < len; i++) {
1350 memcpy((u8 *)&next_lk_ahds[i],
1351 bundle_lkahd_rpt->lk_ahd, 4);
1352 bundle_lkahd_rpt++;
1353 }
1354
1355 *n_lk_ahds = i;
1356 }
1357 break;
1358 default:
1359 ath6kl_err("unhandled record: id:%d len:%d\n",
1360 record->rec_id, record->len);
1361 break;
1362 }
1363
1364 return 0;
1365
1366}
1367
1368static int htc_proc_trailer(struct htc_target *target,
1369 u8 *buf, int len, u32 *next_lk_ahds,
1370 int *n_lk_ahds, enum htc_endpoint_id endpoint)
1371{
1372 struct htc_record_hdr *record;
1373 int orig_len;
1374 int status;
1375 u8 *record_buf;
1376 u8 *orig_buf;
1377
Kalle Valo471e92f2011-10-13 15:21:37 +03001378 ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
1379 ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001380
1381 orig_buf = buf;
1382 orig_len = len;
1383 status = 0;
1384
1385 while (len > 0) {
1386
1387 if (len < sizeof(struct htc_record_hdr)) {
1388 status = -ENOMEM;
1389 break;
1390 }
1391 /* these are byte aligned structs */
1392 record = (struct htc_record_hdr *) buf;
1393 len -= sizeof(struct htc_record_hdr);
1394 buf += sizeof(struct htc_record_hdr);
1395
1396 if (record->len > len) {
1397 ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
1398 record->len, record->rec_id, len);
1399 status = -ENOMEM;
1400 break;
1401 }
1402 record_buf = buf;
1403
1404 status = htc_parse_trailer(target, record, record_buf,
1405 next_lk_ahds, endpoint, n_lk_ahds);
1406
1407 if (status)
1408 break;
1409
1410 /* advance buffer past this record for next time around */
1411 buf += record->len;
1412 len -= record->len;
1413 }
1414
Raja Mani2588f552011-07-19 19:27:30 +05301415 if (status)
Kalle Valo471e92f2011-10-13 15:21:37 +03001416 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
Kalle Valoef094102011-09-27 14:30:45 +03001417 "", orig_buf, orig_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001418
1419 return status;
1420}
1421
Kalle Valo689def92011-09-06 11:10:49 +03001422static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
1423 struct htc_packet *packet,
1424 u32 *next_lkahds, int *n_lkahds)
Kalle Valobdcd8172011-07-18 00:22:30 +03001425{
1426 int status = 0;
1427 u16 payload_len;
1428 u32 lk_ahd;
1429 struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;
1430
1431 if (n_lkahds != NULL)
1432 *n_lkahds = 0;
1433
Kalle Valo471e92f2011-10-13 15:21:37 +03001434 /* FIXME: is this needed? */
1435 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx", "htc rx",
Kalle Valoef094102011-09-27 14:30:45 +03001436 packet->buf, packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001437
1438 /*
1439 * NOTE: we cannot assume the alignment of buf, so we use the safe
1440 * macros to retrieve 16 bit fields.
1441 */
1442 payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
1443
1444 memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
1445
1446 if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
1447 /*
1448 * Refresh the expected header and the actual length as it
1449 * was unknown when this packet was grabbed as part of the
1450 * bundle.
1451 */
1452 packet->info.rx.exp_hdr = lk_ahd;
1453 packet->act_len = payload_len + HTC_HDR_LENGTH;
1454
1455 /* validate the actual header that was refreshed */
1456 if (packet->act_len > packet->buf_len) {
1457 ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
1458 payload_len, lk_ahd);
1459 /*
1460 * Limit this to max buffer just to print out some
1461 * of the buffer.
1462 */
1463 packet->act_len = min(packet->act_len, packet->buf_len);
1464 status = -ENOMEM;
1465 goto fail_rx;
1466 }
1467
1468 if (packet->endpoint != htc_hdr->eid) {
1469 ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
1470 htc_hdr->eid, packet->endpoint);
1471 status = -ENOMEM;
1472 goto fail_rx;
1473 }
1474 }
1475
1476 if (lk_ahd != packet->info.rx.exp_hdr) {
Kalle Valo689def92011-09-06 11:10:49 +03001477 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1478 __func__, packet, packet->info.rx.rx_flags);
Kalle Valo471e92f2011-10-13 15:21:37 +03001479 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
Kalle Valoef094102011-09-27 14:30:45 +03001480 "", &packet->info.rx.exp_hdr, 4);
Kalle Valo471e92f2011-10-13 15:21:37 +03001481 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
Kalle Valoef094102011-09-27 14:30:45 +03001482 "", (u8 *)&lk_ahd, sizeof(lk_ahd));
Kalle Valobdcd8172011-07-18 00:22:30 +03001483 status = -ENOMEM;
1484 goto fail_rx;
1485 }
1486
1487 if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
1488 if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
1489 htc_hdr->ctrl[0] > payload_len) {
Kalle Valo689def92011-09-06 11:10:49 +03001490 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1491 __func__, payload_len, htc_hdr->ctrl[0]);
Kalle Valobdcd8172011-07-18 00:22:30 +03001492 status = -ENOMEM;
1493 goto fail_rx;
1494 }
1495
1496 if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
1497 next_lkahds = NULL;
1498 n_lkahds = NULL;
1499 }
1500
1501 status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
1502 + payload_len - htc_hdr->ctrl[0],
1503 htc_hdr->ctrl[0], next_lkahds,
1504 n_lkahds, packet->endpoint);
1505
1506 if (status)
1507 goto fail_rx;
1508
1509 packet->act_len -= htc_hdr->ctrl[0];
1510 }
1511
1512 packet->buf += HTC_HDR_LENGTH;
1513 packet->act_len -= HTC_HDR_LENGTH;
1514
1515fail_rx:
1516 if (status)
Kalle Valo471e92f2011-10-13 15:21:37 +03001517 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1518 "", packet->buf, packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001519 else {
Kalle Valo471e92f2011-10-13 15:21:37 +03001520 /* FIXME: is this needed? */
Kalle Valobdcd8172011-07-18 00:22:30 +03001521 if (packet->act_len > 0)
Kalle Valo471e92f2011-10-13 15:21:37 +03001522 ath6kl_dbg_dump(ATH6KL_DBG_HTC,
1523 "htc rx application message", "",
Kalle Valobdcd8172011-07-18 00:22:30 +03001524 packet->buf, packet->act_len);
1525 }
1526
1527 return status;
1528}
1529
Kalle Valo689def92011-09-06 11:10:49 +03001530static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1531 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +03001532{
Kalle Valoebf29c92011-10-13 15:21:15 +03001533 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001534 "htc rx complete ep %d packet 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001535 endpoint->eid, packet);
1536 endpoint->ep_cb.rx(endpoint->target, packet);
1537}
1538
Kalle Valo689def92011-09-06 11:10:49 +03001539static int ath6kl_htc_rx_bundle(struct htc_target *target,
1540 struct list_head *rxq,
1541 struct list_head *sync_compq,
1542 int *n_pkt_fetched, bool part_bundle)
Kalle Valobdcd8172011-07-18 00:22:30 +03001543{
1544 struct hif_scatter_req *scat_req;
1545 struct htc_packet *packet;
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05301546 int rem_space = target->max_rx_bndl_sz;
Kalle Valobdcd8172011-07-18 00:22:30 +03001547 int n_scat_pkt, status = 0, i, len;
1548
1549 n_scat_pkt = get_queue_depth(rxq);
1550 n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
1551
1552 if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
1553 /*
1554 * We were forced to split this bundle receive operation
1555 * all packets in this partial bundle must have their
1556 * lookaheads ignored.
1557 */
1558 part_bundle = true;
1559
1560 /*
1561 * This would only happen if the target ignored our max
1562 * bundle limit.
1563 */
Kalle Valo689def92011-09-06 11:10:49 +03001564 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1565 __func__, get_queue_depth(rxq), n_scat_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001566 }
1567
1568 len = 0;
1569
Kalle Valoebf29c92011-10-13 15:21:15 +03001570 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001571 "htc rx bundle depth %d pkts %d\n",
1572 get_queue_depth(rxq), n_scat_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001573
1574 scat_req = hif_scatter_req_get(target->dev->ar);
1575
1576 if (scat_req == NULL)
1577 goto fail_rx_pkt;
1578
Kalle Valobdcd8172011-07-18 00:22:30 +03001579 for (i = 0; i < n_scat_pkt; i++) {
1580 int pad_len;
1581
1582 packet = list_first_entry(rxq, struct htc_packet, list);
1583 list_del(&packet->list);
1584
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05301585 pad_len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +03001586 packet->act_len);
1587
1588 if ((rem_space - pad_len) < 0) {
1589 list_add(&packet->list, rxq);
1590 break;
1591 }
1592
1593 rem_space -= pad_len;
1594
1595 if (part_bundle || (i < (n_scat_pkt - 1)))
1596 /*
1597 * Packet 0..n-1 cannot be checked for look-aheads
1598 * since we are fetching a bundle the last packet
1599 * however can have it's lookahead used
1600 */
1601 packet->info.rx.rx_flags |=
1602 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1603
1604 /* NOTE: 1 HTC packet per scatter entry */
1605 scat_req->scat_list[i].buf = packet->buf;
1606 scat_req->scat_list[i].len = pad_len;
1607
1608 packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
1609
1610 list_add_tail(&packet->list, sync_compq);
1611
1612 WARN_ON(!scat_req->scat_list[i].len);
1613 len += scat_req->scat_list[i].len;
1614 }
1615
1616 scat_req->len = len;
1617 scat_req->scat_entries = i;
1618
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001619 status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001620
1621 if (!status)
1622 *n_pkt_fetched = i;
1623
1624 /* free scatter request */
1625 hif_scatter_req_add(target->dev->ar, scat_req);
1626
1627fail_rx_pkt:
1628
1629 return status;
1630}
1631
Kalle Valo689def92011-09-06 11:10:49 +03001632static int ath6kl_htc_rx_process_packets(struct htc_target *target,
1633 struct list_head *comp_pktq,
1634 u32 lk_ahds[],
1635 int *n_lk_ahd)
Kalle Valobdcd8172011-07-18 00:22:30 +03001636{
1637 struct htc_packet *packet, *tmp_pkt;
1638 struct htc_endpoint *ep;
1639 int status = 0;
1640
1641 list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001642 ep = &target->endpoint[packet->endpoint];
1643
1644 /* process header for each of the recv packet */
Kalle Valo689def92011-09-06 11:10:49 +03001645 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
1646 n_lk_ahd);
Kalle Valobdcd8172011-07-18 00:22:30 +03001647 if (status)
1648 return status;
1649
Vasanthakumar Thiagarajan4159cc92011-10-03 17:28:07 +05301650 list_del(&packet->list);
1651
Kalle Valobdcd8172011-07-18 00:22:30 +03001652 if (list_empty(comp_pktq)) {
1653 /*
1654 * Last packet's more packet flag is set
1655 * based on the lookahead.
1656 */
1657 if (*n_lk_ahd > 0)
Kalle Valo689def92011-09-06 11:10:49 +03001658 ath6kl_htc_rx_set_indicate(lk_ahds[0],
1659 ep, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03001660 } else
1661 /*
1662 * Packets in a bundle automatically have
1663 * this flag set.
1664 */
1665 packet->info.rx.indicat_flags |=
1666 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
1667
Kalle Valo689def92011-09-06 11:10:49 +03001668 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
Kalle Valobdcd8172011-07-18 00:22:30 +03001669
1670 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
1671 ep->ep_st.rx_bundl += 1;
1672
Kalle Valo689def92011-09-06 11:10:49 +03001673 ath6kl_htc_rx_complete(ep, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03001674 }
1675
1676 return status;
1677}
1678
Kalle Valo689def92011-09-06 11:10:49 +03001679static int ath6kl_htc_rx_fetch(struct htc_target *target,
1680 struct list_head *rx_pktq,
1681 struct list_head *comp_pktq)
Kalle Valobdcd8172011-07-18 00:22:30 +03001682{
1683 int fetched_pkts;
1684 bool part_bundle = false;
1685 int status = 0;
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301686 struct list_head tmp_rxq;
1687 struct htc_packet *packet, *tmp_pkt;
Kalle Valobdcd8172011-07-18 00:22:30 +03001688
1689 /* now go fetch the list of HTC packets */
1690 while (!list_empty(rx_pktq)) {
1691 fetched_pkts = 0;
1692
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301693 INIT_LIST_HEAD(&tmp_rxq);
1694
Kalle Valobdcd8172011-07-18 00:22:30 +03001695 if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
1696 /*
1697 * There are enough packets to attempt a
1698 * bundle transfer and recv bundling is
1699 * allowed.
1700 */
Kalle Valo689def92011-09-06 11:10:49 +03001701 status = ath6kl_htc_rx_bundle(target, rx_pktq,
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301702 &tmp_rxq,
Kalle Valo689def92011-09-06 11:10:49 +03001703 &fetched_pkts,
1704 part_bundle);
Kalle Valobdcd8172011-07-18 00:22:30 +03001705 if (status)
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301706 goto fail_rx;
Kalle Valobdcd8172011-07-18 00:22:30 +03001707
1708 if (!list_empty(rx_pktq))
1709 part_bundle = true;
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301710
1711 list_splice_tail_init(&tmp_rxq, comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001712 }
1713
1714 if (!fetched_pkts) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001715
1716 packet = list_first_entry(rx_pktq, struct htc_packet,
1717 list);
1718
Kalle Valobdcd8172011-07-18 00:22:30 +03001719 /* fully synchronous */
1720 packet->completion = NULL;
1721
Vasanthakumar Thiagarajanb8d5d5f2011-10-03 17:28:25 +05301722 if (!list_is_singular(rx_pktq))
Kalle Valobdcd8172011-07-18 00:22:30 +03001723 /*
1724 * look_aheads in all packet
1725 * except the last one in the
1726 * bundle must be ignored
1727 */
1728 packet->info.rx.rx_flags |=
1729 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1730
1731 /* go fetch the packet */
Kalle Valo689def92011-09-06 11:10:49 +03001732 status = ath6kl_htc_rx_packet(target, packet,
1733 packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001734
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301735 list_move_tail(&packet->list, &tmp_rxq);
1736
1737 if (status)
1738 goto fail_rx;
1739
1740 list_splice_tail_init(&tmp_rxq, comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001741 }
1742 }
1743
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301744 return 0;
1745
1746fail_rx:
1747
1748 /*
1749 * Cleanup any packets we allocated but didn't use to
1750 * actually fetch any packets.
1751 */
1752
1753 list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
1754 list_del(&packet->list);
1755 htc_reclaim_rxbuf(target, packet,
1756 &target->endpoint[packet->endpoint]);
1757 }
1758
1759 list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
1760 list_del(&packet->list);
1761 htc_reclaim_rxbuf(target, packet,
1762 &target->endpoint[packet->endpoint]);
1763 }
1764
Kalle Valobdcd8172011-07-18 00:22:30 +03001765 return status;
1766}
1767
Kalle Vaload226ec2011-08-10 09:49:12 +03001768int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
Vasanthakumar Thiagarajan4533d902011-10-03 17:26:27 +05301769 u32 msg_look_ahead, int *num_pkts)
Kalle Valobdcd8172011-07-18 00:22:30 +03001770{
1771 struct htc_packet *packets, *tmp_pkt;
1772 struct htc_endpoint *endpoint;
1773 struct list_head rx_pktq, comp_pktq;
1774 int status = 0;
1775 u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
1776 int num_look_ahead = 1;
1777 enum htc_endpoint_id id;
1778 int n_fetched = 0;
1779
1780 *num_pkts = 0;
1781
1782 /*
1783 * On first entry copy the look_aheads into our temp array for
1784 * processing
1785 */
Vasanthakumar Thiagarajan4533d902011-10-03 17:26:27 +05301786 look_aheads[0] = msg_look_ahead;
Kalle Valobdcd8172011-07-18 00:22:30 +03001787
1788 while (true) {
1789
1790 /*
1791 * First lookahead sets the expected endpoint IDs for all
1792 * packets in a bundle.
1793 */
1794 id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
1795 endpoint = &target->endpoint[id];
1796
1797 if (id >= ENDPOINT_MAX) {
1798 ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
1799 id);
1800 status = -ENOMEM;
1801 break;
1802 }
1803
1804 INIT_LIST_HEAD(&rx_pktq);
1805 INIT_LIST_HEAD(&comp_pktq);
1806
1807 /*
1808 * Try to allocate as many HTC RX packets indicated by the
1809 * look_aheads.
1810 */
Kalle Valo689def92011-09-06 11:10:49 +03001811 status = ath6kl_htc_rx_alloc(target, look_aheads,
1812 num_look_ahead, endpoint,
1813 &rx_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001814 if (status)
1815 break;
1816
1817 if (get_queue_depth(&rx_pktq) >= 2)
1818 /*
1819 * A recv bundle was detected, force IRQ status
1820 * re-check again
1821 */
Vasanthakumar Thiagarajanfcb82052011-07-18 14:23:31 +05301822 target->chk_irq_status_cnt = 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03001823
1824 n_fetched += get_queue_depth(&rx_pktq);
1825
1826 num_look_ahead = 0;
1827
Kalle Valo689def92011-09-06 11:10:49 +03001828 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001829
1830 if (!status)
Kalle Valo689def92011-09-06 11:10:49 +03001831 ath6kl_htc_rx_chk_water_mark(endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +03001832
1833 /* Process fetched packets */
Kalle Valo689def92011-09-06 11:10:49 +03001834 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
1835 look_aheads,
1836 &num_look_ahead);
Kalle Valobdcd8172011-07-18 00:22:30 +03001837
1838 if (!num_look_ahead || status)
1839 break;
1840
1841 /*
1842 * For SYNCH processing, if we get here, we are running
1843 * through the loop again due to a detected lookahead. Set
1844 * flag that we should re-check IRQ status registers again
1845 * before leaving IRQ processing, this can net better
1846 * performance in high throughput situations.
1847 */
Vasanthakumar Thiagarajanfcb82052011-07-18 14:23:31 +05301848 target->chk_irq_status_cnt = 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03001849 }
1850
1851 if (status) {
1852 ath6kl_err("failed to get pending recv messages: %d\n",
1853 status);
Kalle Valobdcd8172011-07-18 00:22:30 +03001854
1855 /* cleanup any packets in sync completion queue */
1856 list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
1857 list_del(&packets->list);
1858 htc_reclaim_rxbuf(target, packets,
1859 &target->endpoint[packets->endpoint]);
1860 }
1861
1862 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1863 ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001864 ath6kl_hif_rx_control(target->dev, false);
Kalle Valobdcd8172011-07-18 00:22:30 +03001865 }
1866 }
1867
1868 /*
1869 * Before leaving, check to see if host ran out of buffers and
1870 * needs to stop the receiver.
1871 */
1872 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
1873 ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001874 ath6kl_hif_rx_control(target->dev, false);
Kalle Valobdcd8172011-07-18 00:22:30 +03001875 }
1876 *num_pkts = n_fetched;
1877
1878 return status;
1879}
1880
1881/*
1882 * Synchronously wait for a control message from the target,
1883 * This function is used at initialization time ONLY. At init messages
1884 * on ENDPOINT 0 are expected.
1885 */
1886static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
1887{
1888 struct htc_packet *packet = NULL;
1889 struct htc_frame_hdr *htc_hdr;
1890 u32 look_ahead;
1891
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001892 if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
Kalle Valobdcd8172011-07-18 00:22:30 +03001893 HTC_TARGET_RESPONSE_TIMEOUT))
1894 return NULL;
1895
Kalle Valoebf29c92011-10-13 15:21:15 +03001896 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001897 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
Kalle Valobdcd8172011-07-18 00:22:30 +03001898
1899 htc_hdr = (struct htc_frame_hdr *)&look_ahead;
1900
1901 if (htc_hdr->eid != ENDPOINT_0)
1902 return NULL;
1903
1904 packet = htc_get_control_buf(target, false);
1905
1906 if (!packet)
1907 return NULL;
1908
1909 packet->info.rx.rx_flags = 0;
1910 packet->info.rx.exp_hdr = look_ahead;
1911 packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;
1912
1913 if (packet->act_len > packet->buf_len)
1914 goto fail_ctrl_rx;
1915
1916 /* we want synchronous operation */
1917 packet->completion = NULL;
1918
1919 /* get the message from the device, this will block */
Kalle Valo689def92011-09-06 11:10:49 +03001920 if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
Kalle Valobdcd8172011-07-18 00:22:30 +03001921 goto fail_ctrl_rx;
1922
1923 /* process receive header */
Kalle Valo689def92011-09-06 11:10:49 +03001924 packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
Kalle Valobdcd8172011-07-18 00:22:30 +03001925
1926 if (packet->status) {
Kalle Valo689def92011-09-06 11:10:49 +03001927 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001928 packet->status);
1929 goto fail_ctrl_rx;
1930 }
1931
1932 return packet;
1933
1934fail_ctrl_rx:
1935 if (packet != NULL) {
1936 htc_rxpkt_reset(packet);
1937 reclaim_rx_ctrl_buf(target, packet);
1938 }
1939
1940 return NULL;
1941}
1942
Kalle Vaload226ec2011-08-10 09:49:12 +03001943int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
1944 struct list_head *pkt_queue)
Kalle Valobdcd8172011-07-18 00:22:30 +03001945{
1946 struct htc_endpoint *endpoint;
1947 struct htc_packet *first_pkt;
1948 bool rx_unblock = false;
1949 int status = 0, depth;
1950
1951 if (list_empty(pkt_queue))
1952 return -ENOMEM;
1953
1954 first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
1955
1956 if (first_pkt->endpoint >= ENDPOINT_MAX)
1957 return status;
1958
1959 depth = get_queue_depth(pkt_queue);
1960
Kalle Valoebf29c92011-10-13 15:21:15 +03001961 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001962 "htc rx add multiple ep id %d cnt %d len %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001963 first_pkt->endpoint, depth, first_pkt->buf_len);
1964
1965 endpoint = &target->endpoint[first_pkt->endpoint];
1966
1967 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1968 struct htc_packet *packet, *tmp_pkt;
1969
1970 /* walk through queue and mark each one canceled */
1971 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
1972 packet->status = -ECANCELED;
1973 list_del(&packet->list);
Kalle Valo689def92011-09-06 11:10:49 +03001974 ath6kl_htc_rx_complete(endpoint, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03001975 }
1976
1977 return status;
1978 }
1979
1980 spin_lock_bh(&target->rx_lock);
1981
1982 list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
1983
1984 /* check if we are blocked waiting for a new buffer */
1985 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
1986 if (target->ep_waiting == first_pkt->endpoint) {
Kalle Valoebf29c92011-10-13 15:21:15 +03001987 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001988 "htc rx blocked on ep %d, unblocking\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001989 target->ep_waiting);
1990 target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
1991 target->ep_waiting = ENDPOINT_MAX;
1992 rx_unblock = true;
1993 }
1994 }
1995
1996 spin_unlock_bh(&target->rx_lock);
1997
1998 if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
1999 /* TODO : implement a buffer threshold count? */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002000 ath6kl_hif_rx_control(target->dev, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03002001
2002 return status;
2003}
2004
Kalle Vaload226ec2011-08-10 09:49:12 +03002005void ath6kl_htc_flush_rx_buf(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002006{
2007 struct htc_endpoint *endpoint;
2008 struct htc_packet *packet, *tmp_pkt;
2009 int i;
2010
2011 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2012 endpoint = &target->endpoint[i];
2013 if (!endpoint->svc_id)
2014 /* not in use.. */
2015 continue;
2016
2017 spin_lock_bh(&target->rx_lock);
2018 list_for_each_entry_safe(packet, tmp_pkt,
2019 &endpoint->rx_bufq, list) {
2020 list_del(&packet->list);
2021 spin_unlock_bh(&target->rx_lock);
Kalle Valoebf29c92011-10-13 15:21:15 +03002022 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002023 "htc rx flush pkt 0x%p len %d ep %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002024 packet, packet->buf_len,
2025 packet->endpoint);
2026 dev_kfree_skb(packet->pkt_cntxt);
2027 spin_lock_bh(&target->rx_lock);
2028 }
2029 spin_unlock_bh(&target->rx_lock);
2030 }
2031}
2032
Kalle Vaload226ec2011-08-10 09:49:12 +03002033int ath6kl_htc_conn_service(struct htc_target *target,
2034 struct htc_service_connect_req *conn_req,
2035 struct htc_service_connect_resp *conn_resp)
Kalle Valobdcd8172011-07-18 00:22:30 +03002036{
2037 struct htc_packet *rx_pkt = NULL;
2038 struct htc_packet *tx_pkt = NULL;
2039 struct htc_conn_service_resp *resp_msg;
2040 struct htc_conn_service_msg *conn_msg;
2041 struct htc_endpoint *endpoint;
2042 enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
2043 unsigned int max_msg_sz = 0;
2044 int status = 0;
2045
Kalle Valoebf29c92011-10-13 15:21:15 +03002046 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002047 "htc connect service target 0x%p service id 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002048 target, conn_req->svc_id);
2049
2050 if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
2051 /* special case for pseudo control service */
2052 assigned_ep = ENDPOINT_0;
2053 max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
2054 } else {
2055 /* allocate a packet to send to the target */
2056 tx_pkt = htc_get_control_buf(target, true);
2057
2058 if (!tx_pkt)
2059 return -ENOMEM;
2060
2061 conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
2062 memset(conn_msg, 0, sizeof(*conn_msg));
2063 conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
2064 conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
2065 conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
2066
2067 set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
2068 sizeof(*conn_msg) + conn_msg->svc_meta_len,
2069 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
2070
2071 /* we want synchronous operation */
2072 tx_pkt->completion = NULL;
Kalle Valodfa01042011-09-06 11:10:49 +03002073 ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
2074 status = ath6kl_htc_tx_issue(target, tx_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03002075
2076 if (status)
2077 goto fail_tx;
2078
2079 /* wait for response */
2080 rx_pkt = htc_wait_for_ctrl_msg(target);
2081
2082 if (!rx_pkt) {
2083 status = -ENOMEM;
2084 goto fail_tx;
2085 }
2086
2087 resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
2088
2089 if ((le16_to_cpu(resp_msg->msg_id) != HTC_MSG_CONN_SVC_RESP_ID)
2090 || (rx_pkt->act_len < sizeof(*resp_msg))) {
2091 status = -ENOMEM;
2092 goto fail_tx;
2093 }
2094
2095 conn_resp->resp_code = resp_msg->status;
2096 /* check response status */
2097 if (resp_msg->status != HTC_SERVICE_SUCCESS) {
2098 ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
2099 resp_msg->svc_id, resp_msg->status);
2100 status = -ENOMEM;
2101 goto fail_tx;
2102 }
2103
2104 assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
2105 max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
2106 }
2107
2108 if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
2109 status = -ENOMEM;
2110 goto fail_tx;
2111 }
2112
2113 endpoint = &target->endpoint[assigned_ep];
2114 endpoint->eid = assigned_ep;
2115 if (endpoint->svc_id) {
2116 status = -ENOMEM;
2117 goto fail_tx;
2118 }
2119
2120 /* return assigned endpoint to caller */
2121 conn_resp->endpoint = assigned_ep;
2122 conn_resp->len_max = max_msg_sz;
2123
2124 /* setup the endpoint */
2125
2126 /* this marks the endpoint in use */
2127 endpoint->svc_id = conn_req->svc_id;
2128
2129 endpoint->max_txq_depth = conn_req->max_txq_depth;
2130 endpoint->len_max = max_msg_sz;
2131 endpoint->ep_cb = conn_req->ep_cb;
2132 endpoint->cred_dist.svc_id = conn_req->svc_id;
2133 endpoint->cred_dist.htc_rsvd = endpoint;
2134 endpoint->cred_dist.endpoint = assigned_ep;
2135 endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
2136
2137 if (conn_req->max_rxmsg_sz) {
2138 /*
2139 * Override cred_per_msg calculation, this optimizes
2140 * the credit-low indications since the host will actually
2141 * issue smaller messages in the Send path.
2142 */
2143 if (conn_req->max_rxmsg_sz > max_msg_sz) {
2144 status = -ENOMEM;
2145 goto fail_tx;
2146 }
2147 endpoint->cred_dist.cred_per_msg =
2148 conn_req->max_rxmsg_sz / target->tgt_cred_sz;
2149 } else
2150 endpoint->cred_dist.cred_per_msg =
2151 max_msg_sz / target->tgt_cred_sz;
2152
2153 if (!endpoint->cred_dist.cred_per_msg)
2154 endpoint->cred_dist.cred_per_msg = 1;
2155
2156 /* save local connection flags */
2157 endpoint->conn_flags = conn_req->flags;
2158
2159fail_tx:
2160 if (tx_pkt)
2161 htc_reclaim_txctrl_buf(target, tx_pkt);
2162
2163 if (rx_pkt) {
2164 htc_rxpkt_reset(rx_pkt);
2165 reclaim_rx_ctrl_buf(target, rx_pkt);
2166 }
2167
2168 return status;
2169}
2170
2171static void reset_ep_state(struct htc_target *target)
2172{
2173 struct htc_endpoint *endpoint;
2174 int i;
2175
2176 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2177 endpoint = &target->endpoint[i];
2178 memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
2179 endpoint->svc_id = 0;
2180 endpoint->len_max = 0;
2181 endpoint->max_txq_depth = 0;
2182 memset(&endpoint->ep_st, 0,
2183 sizeof(endpoint->ep_st));
2184 INIT_LIST_HEAD(&endpoint->rx_bufq);
2185 INIT_LIST_HEAD(&endpoint->txq);
2186 endpoint->target = target;
2187 }
2188
2189 /* reset distribution list */
2190 INIT_LIST_HEAD(&target->cred_dist_list);
2191}
2192
Kalle Vaload226ec2011-08-10 09:49:12 +03002193int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
2194 enum htc_endpoint_id endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +03002195{
2196 int num;
2197
2198 spin_lock_bh(&target->rx_lock);
2199 num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
2200 spin_unlock_bh(&target->rx_lock);
2201 return num;
2202}
2203
2204static void htc_setup_msg_bndl(struct htc_target *target)
2205{
Kalle Valobdcd8172011-07-18 00:22:30 +03002206 /* limit what HTC can handle */
2207 target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
2208 target->msg_per_bndl_max);
2209
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302210 if (ath6kl_hif_enable_scatter(target->dev->ar)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002211 target->msg_per_bndl_max = 0;
2212 return;
2213 }
2214
2215 /* limit bundle what the device layer can handle */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302216 target->msg_per_bndl_max = min(target->max_scat_entries,
Kalle Valobdcd8172011-07-18 00:22:30 +03002217 target->msg_per_bndl_max);
2218
Kalle Valoebf29c92011-10-13 15:21:15 +03002219 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002220 "htc bundling allowed msg_per_bndl_max %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002221 target->msg_per_bndl_max);
2222
2223 /* Max rx bundle size is limited by the max tx bundle size */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302224 target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
Kalle Valobdcd8172011-07-18 00:22:30 +03002225 /* Max tx bundle size if limited by the extended mbox address range */
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302226 target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302227 target->max_xfer_szper_scatreq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002228
Kalle Valo471e92f2011-10-13 15:21:37 +03002229 ath6kl_dbg(ATH6KL_DBG_HTC, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302230 target->max_rx_bndl_sz, target->max_tx_bndl_sz);
Kalle Valobdcd8172011-07-18 00:22:30 +03002231
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302232 if (target->max_tx_bndl_sz)
Kalle Valobdcd8172011-07-18 00:22:30 +03002233 target->tx_bndl_enable = true;
2234
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302235 if (target->max_rx_bndl_sz)
Kalle Valobdcd8172011-07-18 00:22:30 +03002236 target->rx_bndl_enable = true;
2237
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05302238 if ((target->tgt_cred_sz % target->block_sz) != 0) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002239 ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
2240 target->tgt_cred_sz);
2241
2242 /*
2243 * Disallow send bundling since the credit size is
2244 * not aligned to a block size the I/O block
2245 * padding will spill into the next credit buffer
2246 * which is fatal.
2247 */
2248 target->tx_bndl_enable = false;
2249 }
2250}
2251
Kalle Vaload226ec2011-08-10 09:49:12 +03002252int ath6kl_htc_wait_target(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002253{
2254 struct htc_packet *packet = NULL;
2255 struct htc_ready_ext_msg *rdy_msg;
2256 struct htc_service_connect_req connect;
2257 struct htc_service_connect_resp resp;
2258 int status;
2259
2260 /* we should be getting 1 control message that the target is ready */
2261 packet = htc_wait_for_ctrl_msg(target);
2262
2263 if (!packet)
2264 return -ENOMEM;
2265
2266 /* we controlled the buffer creation so it's properly aligned */
2267 rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
2268
2269 if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
2270 (packet->act_len < sizeof(struct htc_ready_msg))) {
2271 status = -ENOMEM;
2272 goto fail_wait_target;
2273 }
2274
2275 if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
2276 status = -ENOMEM;
2277 goto fail_wait_target;
2278 }
2279
2280 target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
2281 target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
2282
Kalle Valoebf29c92011-10-13 15:21:15 +03002283 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002284 "htc target ready credits %d size %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002285 target->tgt_creds, target->tgt_cred_sz);
2286
2287 /* check if this is an extended ready message */
2288 if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
2289 /* this is an extended message */
2290 target->htc_tgt_ver = rdy_msg->htc_ver;
2291 target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
2292 } else {
2293 /* legacy */
2294 target->htc_tgt_ver = HTC_VERSION_2P0;
2295 target->msg_per_bndl_max = 0;
2296 }
2297
Kalle Valo471e92f2011-10-13 15:21:37 +03002298 ath6kl_dbg(ATH6KL_DBG_HTC, "htc using protocol %s (%d)\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002299 (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
2300 target->htc_tgt_ver);
2301
2302 if (target->msg_per_bndl_max > 0)
2303 htc_setup_msg_bndl(target);
2304
2305 /* setup our pseudo HTC control endpoint connection */
2306 memset(&connect, 0, sizeof(connect));
2307 memset(&resp, 0, sizeof(resp));
2308 connect.ep_cb.rx = htc_ctrl_rx;
2309 connect.ep_cb.rx_refill = NULL;
2310 connect.ep_cb.tx_full = NULL;
2311 connect.max_txq_depth = NUM_CONTROL_BUFFERS;
2312 connect.svc_id = HTC_CTRL_RSVD_SVC;
2313
2314 /* connect fake service */
Kalle Vaload226ec2011-08-10 09:49:12 +03002315 status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
Kalle Valobdcd8172011-07-18 00:22:30 +03002316
2317 if (status)
2318 ath6kl_hif_cleanup_scatter(target->dev->ar);
2319
2320fail_wait_target:
2321 if (packet) {
2322 htc_rxpkt_reset(packet);
2323 reclaim_rx_ctrl_buf(target, packet);
2324 }
2325
2326 return status;
2327}
2328
2329/*
2330 * Start HTC, enable interrupts and let the target know
2331 * host has finished setup.
2332 */
Kalle Vaload226ec2011-08-10 09:49:12 +03002333int ath6kl_htc_start(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002334{
2335 struct htc_packet *packet;
2336 int status;
2337
2338 /* Disable interrupts at the chip level */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002339 ath6kl_hif_disable_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002340
2341 target->htc_flags = 0;
2342 target->rx_st_flags = 0;
2343
2344 /* Push control receive buffers into htc control endpoint */
2345 while ((packet = htc_get_control_buf(target, false)) != NULL) {
2346 status = htc_add_rxbuf(target, packet);
2347 if (status)
2348 return status;
2349 }
2350
2351 /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2352 ath6k_credit_init(target->cred_dist_cntxt, &target->cred_dist_list,
2353 target->tgt_creds);
2354
2355 dump_cred_dist_stats(target);
2356
2357 /* Indicate to the target of the setup completion */
2358 status = htc_setup_tx_complete(target);
2359
2360 if (status)
2361 return status;
2362
2363 /* unmask interrupts */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002364 status = ath6kl_hif_unmask_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002365
2366 if (status)
Kalle Vaload226ec2011-08-10 09:49:12 +03002367 ath6kl_htc_stop(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002368
2369 return status;
2370}
2371
2372/* htc_stop: stop interrupt reception, and flush all queued buffers */
Kalle Vaload226ec2011-08-10 09:49:12 +03002373void ath6kl_htc_stop(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002374{
2375 spin_lock_bh(&target->htc_lock);
2376 target->htc_flags |= HTC_OP_STATE_STOPPING;
2377 spin_unlock_bh(&target->htc_lock);
2378
2379 /*
2380 * Masking interrupts is a synchronous operation, when this
2381 * function returns all pending HIF I/O has completed, we can
2382 * safely flush the queues.
2383 */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002384 ath6kl_hif_mask_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002385
Kalle Vaload226ec2011-08-10 09:49:12 +03002386 ath6kl_htc_flush_txep_all(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002387
Kalle Vaload226ec2011-08-10 09:49:12 +03002388 ath6kl_htc_flush_rx_buf(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002389
2390 reset_ep_state(target);
2391}
2392
Kalle Vaload226ec2011-08-10 09:49:12 +03002393void *ath6kl_htc_create(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +03002394{
2395 struct htc_target *target = NULL;
2396 struct htc_packet *packet;
2397 int status = 0, i = 0;
2398 u32 block_size, ctrl_bufsz;
2399
2400 target = kzalloc(sizeof(*target), GFP_KERNEL);
2401 if (!target) {
2402 ath6kl_err("unable to allocate memory\n");
2403 return NULL;
2404 }
2405
2406 target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
2407 if (!target->dev) {
2408 ath6kl_err("unable to allocate memory\n");
2409 status = -ENOMEM;
2410 goto fail_create_htc;
2411 }
2412
2413 spin_lock_init(&target->htc_lock);
2414 spin_lock_init(&target->rx_lock);
2415 spin_lock_init(&target->tx_lock);
2416
2417 INIT_LIST_HEAD(&target->free_ctrl_txbuf);
2418 INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
2419 INIT_LIST_HEAD(&target->cred_dist_list);
2420
2421 target->dev->ar = ar;
2422 target->dev->htc_cnxt = target;
Kalle Valobdcd8172011-07-18 00:22:30 +03002423 target->ep_waiting = ENDPOINT_MAX;
2424
2425 reset_ep_state(target);
2426
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002427 status = ath6kl_hif_setup(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002428
2429 if (status)
2430 goto fail_create_htc;
2431
2432 block_size = ar->mbox_info.block_size;
2433
2434 ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
2435 (block_size + HTC_HDR_LENGTH) :
2436 (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
2437
2438 for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
2439 packet = kzalloc(sizeof(*packet), GFP_KERNEL);
2440 if (!packet)
2441 break;
2442
2443 packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
2444 if (!packet->buf_start) {
2445 kfree(packet);
2446 break;
2447 }
2448
2449 packet->buf_len = ctrl_bufsz;
2450 if (i < NUM_CONTROL_RX_BUFFERS) {
2451 packet->act_len = 0;
2452 packet->buf = packet->buf_start;
2453 packet->endpoint = ENDPOINT_0;
2454 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
2455 } else
2456 list_add_tail(&packet->list, &target->free_ctrl_txbuf);
2457 }
2458
2459fail_create_htc:
2460 if (i != NUM_CONTROL_BUFFERS || status) {
2461 if (target) {
Kalle Vaload226ec2011-08-10 09:49:12 +03002462 ath6kl_htc_cleanup(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002463 target = NULL;
2464 }
2465 }
2466
2467 return target;
2468}
2469
2470/* cleanup the HTC instance */
Kalle Vaload226ec2011-08-10 09:49:12 +03002471void ath6kl_htc_cleanup(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002472{
2473 struct htc_packet *packet, *tmp_packet;
2474
2475 ath6kl_hif_cleanup_scatter(target->dev->ar);
2476
2477 list_for_each_entry_safe(packet, tmp_packet,
2478 &target->free_ctrl_txbuf, list) {
2479 list_del(&packet->list);
2480 kfree(packet->buf_start);
2481 kfree(packet);
2482 }
2483
2484 list_for_each_entry_safe(packet, tmp_packet,
2485 &target->free_ctrl_rxbuf, list) {
2486 list_del(&packet->list);
2487 kfree(packet->buf_start);
2488 kfree(packet);
2489 }
2490
2491 kfree(target->dev);
2492 kfree(target);
2493}