blob: 24dfc02225cb5af4da5726e849271eae90f28f8f [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 Valo3c370392011-10-24 12:17:12 +0300106 target->credit_info, &target->cred_dist_list);
Kalle Valobdcd8172011-07-18 00:22:30 +0300107
Kalle Valo3c370392011-10-24 12:17:12 +0300108 ath6kl_credit_distribute(target->credit_info,
109 &target->cred_dist_list,
110 HTC_CREDIT_DIST_SEND_COMPLETE);
Kalle Valobdcd8172011-07-18 00:22:30 +0300111
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 Valo3c370392011-10-24 12:17:12 +0300238 target->credit_info, &ep->cred_dist);
Kalle Valobdcd8172011-07-18 00:22:30 +0300239
Kalle Valo3c370392011-10-24 12:17:12 +0300240 ath6kl_seek_credits(target->credit_info, &ep->cred_dist);
Kalle Valobdcd8172011-07-18 00:22:30 +0300241
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 Valo3c370392011-10-24 12:17:12 +0300261 target->credit_info, &ep->cred_dist);
Kalle Valobdcd8172011-07-18 00:22:30 +0300262
Kalle Valo3c370392011-10-24 12:17:12 +0300263 ath6kl_seek_credits(target->credit_info, &ep->cred_dist);
Kalle Valobdcd8172011-07-18 00:22:30 +0300264
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) {
Kalle Valoe8c39792011-10-24 12:17:04 +0300622 endpoint = cred_dist->htc_ep;
Kalle Valobdcd8172011-07-18 00:22:30 +0300623
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,
Kalle Valo3c370392011-10-24 12:17:12 +0300701 struct ath6kl_htc_credit_info *credit_info,
Kalle Vaload226ec2011-08-10 09:49:12 +0300702 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
Kalle Valo3c370392011-10-24 12:17:12 +0300707 target->credit_info = credit_info;
Kalle Valobdcd8172011-07-18 00:22:30 +0300708
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 Valo3c370392011-10-24 12:17:12 +0300843 target->credit_info, &target->cred_dist_list);
Kalle Valobdcd8172011-07-18 00:22:30 +0300844
Kalle Valo3c370392011-10-24 12:17:12 +0300845 ath6kl_credit_distribute(target->credit_info,
Kalle Valofa99e962011-10-24 12:16:55 +0300846 &target->cred_dist_list,
847 HTC_CREDIT_DIST_ACTIVITY_CHANGE);
Kalle Valobdcd8172011-07-18 00:22:30 +0300848 }
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 Valo3c370392011-10-24 12:17:12 +03001273 target->credit_info, &target->cred_dist_list);
Kalle Valobdcd8172011-07-18 00:22:30 +03001274
Kalle Valo3c370392011-10-24 12:17:12 +03001275 ath6kl_credit_distribute(target->credit_info,
Kalle Valofa99e962011-10-24 12:16:55 +03001276 &target->cred_dist_list,
1277 HTC_CREDIT_DIST_SEND_COMPLETE);
Kalle Valobdcd8172011-07-18 00:22:30 +03001278 }
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 Valobdcd8172011-07-18 00:22:30 +03001434 /*
1435 * NOTE: we cannot assume the alignment of buf, so we use the safe
1436 * macros to retrieve 16 bit fields.
1437 */
1438 payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
1439
1440 memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));
1441
1442 if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
1443 /*
1444 * Refresh the expected header and the actual length as it
1445 * was unknown when this packet was grabbed as part of the
1446 * bundle.
1447 */
1448 packet->info.rx.exp_hdr = lk_ahd;
1449 packet->act_len = payload_len + HTC_HDR_LENGTH;
1450
1451 /* validate the actual header that was refreshed */
1452 if (packet->act_len > packet->buf_len) {
1453 ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
1454 payload_len, lk_ahd);
1455 /*
1456 * Limit this to max buffer just to print out some
1457 * of the buffer.
1458 */
1459 packet->act_len = min(packet->act_len, packet->buf_len);
1460 status = -ENOMEM;
1461 goto fail_rx;
1462 }
1463
1464 if (packet->endpoint != htc_hdr->eid) {
1465 ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
1466 htc_hdr->eid, packet->endpoint);
1467 status = -ENOMEM;
1468 goto fail_rx;
1469 }
1470 }
1471
1472 if (lk_ahd != packet->info.rx.exp_hdr) {
Kalle Valo689def92011-09-06 11:10:49 +03001473 ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
1474 __func__, packet, packet->info.rx.rx_flags);
Kalle Valo471e92f2011-10-13 15:21:37 +03001475 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
Kalle Valoef094102011-09-27 14:30:45 +03001476 "", &packet->info.rx.exp_hdr, 4);
Kalle Valo471e92f2011-10-13 15:21:37 +03001477 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
Kalle Valoef094102011-09-27 14:30:45 +03001478 "", (u8 *)&lk_ahd, sizeof(lk_ahd));
Kalle Valobdcd8172011-07-18 00:22:30 +03001479 status = -ENOMEM;
1480 goto fail_rx;
1481 }
1482
1483 if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
1484 if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
1485 htc_hdr->ctrl[0] > payload_len) {
Kalle Valo689def92011-09-06 11:10:49 +03001486 ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
1487 __func__, payload_len, htc_hdr->ctrl[0]);
Kalle Valobdcd8172011-07-18 00:22:30 +03001488 status = -ENOMEM;
1489 goto fail_rx;
1490 }
1491
1492 if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
1493 next_lkahds = NULL;
1494 n_lkahds = NULL;
1495 }
1496
1497 status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
1498 + payload_len - htc_hdr->ctrl[0],
1499 htc_hdr->ctrl[0], next_lkahds,
1500 n_lkahds, packet->endpoint);
1501
1502 if (status)
1503 goto fail_rx;
1504
1505 packet->act_len -= htc_hdr->ctrl[0];
1506 }
1507
1508 packet->buf += HTC_HDR_LENGTH;
1509 packet->act_len -= HTC_HDR_LENGTH;
1510
1511fail_rx:
1512 if (status)
Kalle Valo471e92f2011-10-13 15:21:37 +03001513 ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
1514 "", packet->buf, packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001515
1516 return status;
1517}
1518
Kalle Valo689def92011-09-06 11:10:49 +03001519static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
1520 struct htc_packet *packet)
Kalle Valobdcd8172011-07-18 00:22:30 +03001521{
Kalle Valoebf29c92011-10-13 15:21:15 +03001522 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001523 "htc rx complete ep %d packet 0x%p\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001524 endpoint->eid, packet);
1525 endpoint->ep_cb.rx(endpoint->target, packet);
1526}
1527
Kalle Valo689def92011-09-06 11:10:49 +03001528static int ath6kl_htc_rx_bundle(struct htc_target *target,
1529 struct list_head *rxq,
1530 struct list_head *sync_compq,
1531 int *n_pkt_fetched, bool part_bundle)
Kalle Valobdcd8172011-07-18 00:22:30 +03001532{
1533 struct hif_scatter_req *scat_req;
1534 struct htc_packet *packet;
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05301535 int rem_space = target->max_rx_bndl_sz;
Kalle Valobdcd8172011-07-18 00:22:30 +03001536 int n_scat_pkt, status = 0, i, len;
1537
1538 n_scat_pkt = get_queue_depth(rxq);
1539 n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);
1540
1541 if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
1542 /*
1543 * We were forced to split this bundle receive operation
1544 * all packets in this partial bundle must have their
1545 * lookaheads ignored.
1546 */
1547 part_bundle = true;
1548
1549 /*
1550 * This would only happen if the target ignored our max
1551 * bundle limit.
1552 */
Kalle Valo689def92011-09-06 11:10:49 +03001553 ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
1554 __func__, get_queue_depth(rxq), n_scat_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001555 }
1556
1557 len = 0;
1558
Kalle Valoebf29c92011-10-13 15:21:15 +03001559 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001560 "htc rx bundle depth %d pkts %d\n",
1561 get_queue_depth(rxq), n_scat_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03001562
1563 scat_req = hif_scatter_req_get(target->dev->ar);
1564
1565 if (scat_req == NULL)
1566 goto fail_rx_pkt;
1567
Kalle Valobdcd8172011-07-18 00:22:30 +03001568 for (i = 0; i < n_scat_pkt; i++) {
1569 int pad_len;
1570
1571 packet = list_first_entry(rxq, struct htc_packet, list);
1572 list_del(&packet->list);
1573
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05301574 pad_len = CALC_TXRX_PADDED_LEN(target,
Kalle Valobdcd8172011-07-18 00:22:30 +03001575 packet->act_len);
1576
1577 if ((rem_space - pad_len) < 0) {
1578 list_add(&packet->list, rxq);
1579 break;
1580 }
1581
1582 rem_space -= pad_len;
1583
1584 if (part_bundle || (i < (n_scat_pkt - 1)))
1585 /*
1586 * Packet 0..n-1 cannot be checked for look-aheads
1587 * since we are fetching a bundle the last packet
1588 * however can have it's lookahead used
1589 */
1590 packet->info.rx.rx_flags |=
1591 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1592
1593 /* NOTE: 1 HTC packet per scatter entry */
1594 scat_req->scat_list[i].buf = packet->buf;
1595 scat_req->scat_list[i].len = pad_len;
1596
1597 packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;
1598
1599 list_add_tail(&packet->list, sync_compq);
1600
1601 WARN_ON(!scat_req->scat_list[i].len);
1602 len += scat_req->scat_list[i].len;
1603 }
1604
1605 scat_req->len = len;
1606 scat_req->scat_entries = i;
1607
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001608 status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001609
1610 if (!status)
1611 *n_pkt_fetched = i;
1612
1613 /* free scatter request */
1614 hif_scatter_req_add(target->dev->ar, scat_req);
1615
1616fail_rx_pkt:
1617
1618 return status;
1619}
1620
Kalle Valo689def92011-09-06 11:10:49 +03001621static int ath6kl_htc_rx_process_packets(struct htc_target *target,
1622 struct list_head *comp_pktq,
1623 u32 lk_ahds[],
1624 int *n_lk_ahd)
Kalle Valobdcd8172011-07-18 00:22:30 +03001625{
1626 struct htc_packet *packet, *tmp_pkt;
1627 struct htc_endpoint *ep;
1628 int status = 0;
1629
1630 list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001631 ep = &target->endpoint[packet->endpoint];
1632
1633 /* process header for each of the recv packet */
Kalle Valo689def92011-09-06 11:10:49 +03001634 status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
1635 n_lk_ahd);
Kalle Valobdcd8172011-07-18 00:22:30 +03001636 if (status)
1637 return status;
1638
Vasanthakumar Thiagarajan4159cc92011-10-03 17:28:07 +05301639 list_del(&packet->list);
1640
Kalle Valobdcd8172011-07-18 00:22:30 +03001641 if (list_empty(comp_pktq)) {
1642 /*
1643 * Last packet's more packet flag is set
1644 * based on the lookahead.
1645 */
1646 if (*n_lk_ahd > 0)
Kalle Valo689def92011-09-06 11:10:49 +03001647 ath6kl_htc_rx_set_indicate(lk_ahds[0],
1648 ep, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03001649 } else
1650 /*
1651 * Packets in a bundle automatically have
1652 * this flag set.
1653 */
1654 packet->info.rx.indicat_flags |=
1655 HTC_RX_FLAGS_INDICATE_MORE_PKTS;
1656
Kalle Valo689def92011-09-06 11:10:49 +03001657 ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
Kalle Valobdcd8172011-07-18 00:22:30 +03001658
1659 if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
1660 ep->ep_st.rx_bundl += 1;
1661
Kalle Valo689def92011-09-06 11:10:49 +03001662 ath6kl_htc_rx_complete(ep, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03001663 }
1664
1665 return status;
1666}
1667
Kalle Valo689def92011-09-06 11:10:49 +03001668static int ath6kl_htc_rx_fetch(struct htc_target *target,
1669 struct list_head *rx_pktq,
1670 struct list_head *comp_pktq)
Kalle Valobdcd8172011-07-18 00:22:30 +03001671{
1672 int fetched_pkts;
1673 bool part_bundle = false;
1674 int status = 0;
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301675 struct list_head tmp_rxq;
1676 struct htc_packet *packet, *tmp_pkt;
Kalle Valobdcd8172011-07-18 00:22:30 +03001677
1678 /* now go fetch the list of HTC packets */
1679 while (!list_empty(rx_pktq)) {
1680 fetched_pkts = 0;
1681
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301682 INIT_LIST_HEAD(&tmp_rxq);
1683
Kalle Valobdcd8172011-07-18 00:22:30 +03001684 if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
1685 /*
1686 * There are enough packets to attempt a
1687 * bundle transfer and recv bundling is
1688 * allowed.
1689 */
Kalle Valo689def92011-09-06 11:10:49 +03001690 status = ath6kl_htc_rx_bundle(target, rx_pktq,
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301691 &tmp_rxq,
Kalle Valo689def92011-09-06 11:10:49 +03001692 &fetched_pkts,
1693 part_bundle);
Kalle Valobdcd8172011-07-18 00:22:30 +03001694 if (status)
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301695 goto fail_rx;
Kalle Valobdcd8172011-07-18 00:22:30 +03001696
1697 if (!list_empty(rx_pktq))
1698 part_bundle = true;
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301699
1700 list_splice_tail_init(&tmp_rxq, comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001701 }
1702
1703 if (!fetched_pkts) {
Kalle Valobdcd8172011-07-18 00:22:30 +03001704
1705 packet = list_first_entry(rx_pktq, struct htc_packet,
1706 list);
1707
Kalle Valobdcd8172011-07-18 00:22:30 +03001708 /* fully synchronous */
1709 packet->completion = NULL;
1710
Vasanthakumar Thiagarajanb8d5d5f2011-10-03 17:28:25 +05301711 if (!list_is_singular(rx_pktq))
Kalle Valobdcd8172011-07-18 00:22:30 +03001712 /*
1713 * look_aheads in all packet
1714 * except the last one in the
1715 * bundle must be ignored
1716 */
1717 packet->info.rx.rx_flags |=
1718 HTC_RX_PKT_IGNORE_LOOKAHEAD;
1719
1720 /* go fetch the packet */
Kalle Valo689def92011-09-06 11:10:49 +03001721 status = ath6kl_htc_rx_packet(target, packet,
1722 packet->act_len);
Kalle Valobdcd8172011-07-18 00:22:30 +03001723
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301724 list_move_tail(&packet->list, &tmp_rxq);
1725
1726 if (status)
1727 goto fail_rx;
1728
1729 list_splice_tail_init(&tmp_rxq, comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001730 }
1731 }
1732
Vasanthakumar Thiagarajan99f54292011-10-03 17:26:26 +05301733 return 0;
1734
1735fail_rx:
1736
1737 /*
1738 * Cleanup any packets we allocated but didn't use to
1739 * actually fetch any packets.
1740 */
1741
1742 list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
1743 list_del(&packet->list);
1744 htc_reclaim_rxbuf(target, packet,
1745 &target->endpoint[packet->endpoint]);
1746 }
1747
1748 list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
1749 list_del(&packet->list);
1750 htc_reclaim_rxbuf(target, packet,
1751 &target->endpoint[packet->endpoint]);
1752 }
1753
Kalle Valobdcd8172011-07-18 00:22:30 +03001754 return status;
1755}
1756
Kalle Vaload226ec2011-08-10 09:49:12 +03001757int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
Vasanthakumar Thiagarajan4533d902011-10-03 17:26:27 +05301758 u32 msg_look_ahead, int *num_pkts)
Kalle Valobdcd8172011-07-18 00:22:30 +03001759{
1760 struct htc_packet *packets, *tmp_pkt;
1761 struct htc_endpoint *endpoint;
1762 struct list_head rx_pktq, comp_pktq;
1763 int status = 0;
1764 u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
1765 int num_look_ahead = 1;
1766 enum htc_endpoint_id id;
1767 int n_fetched = 0;
1768
1769 *num_pkts = 0;
1770
1771 /*
1772 * On first entry copy the look_aheads into our temp array for
1773 * processing
1774 */
Vasanthakumar Thiagarajan4533d902011-10-03 17:26:27 +05301775 look_aheads[0] = msg_look_ahead;
Kalle Valobdcd8172011-07-18 00:22:30 +03001776
1777 while (true) {
1778
1779 /*
1780 * First lookahead sets the expected endpoint IDs for all
1781 * packets in a bundle.
1782 */
1783 id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
1784 endpoint = &target->endpoint[id];
1785
1786 if (id >= ENDPOINT_MAX) {
1787 ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
1788 id);
1789 status = -ENOMEM;
1790 break;
1791 }
1792
1793 INIT_LIST_HEAD(&rx_pktq);
1794 INIT_LIST_HEAD(&comp_pktq);
1795
1796 /*
1797 * Try to allocate as many HTC RX packets indicated by the
1798 * look_aheads.
1799 */
Kalle Valo689def92011-09-06 11:10:49 +03001800 status = ath6kl_htc_rx_alloc(target, look_aheads,
1801 num_look_ahead, endpoint,
1802 &rx_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001803 if (status)
1804 break;
1805
1806 if (get_queue_depth(&rx_pktq) >= 2)
1807 /*
1808 * A recv bundle was detected, force IRQ status
1809 * re-check again
1810 */
Vasanthakumar Thiagarajanfcb82052011-07-18 14:23:31 +05301811 target->chk_irq_status_cnt = 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03001812
1813 n_fetched += get_queue_depth(&rx_pktq);
1814
1815 num_look_ahead = 0;
1816
Kalle Valo689def92011-09-06 11:10:49 +03001817 status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
Kalle Valobdcd8172011-07-18 00:22:30 +03001818
1819 if (!status)
Kalle Valo689def92011-09-06 11:10:49 +03001820 ath6kl_htc_rx_chk_water_mark(endpoint);
Kalle Valobdcd8172011-07-18 00:22:30 +03001821
1822 /* Process fetched packets */
Kalle Valo689def92011-09-06 11:10:49 +03001823 status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
1824 look_aheads,
1825 &num_look_ahead);
Kalle Valobdcd8172011-07-18 00:22:30 +03001826
1827 if (!num_look_ahead || status)
1828 break;
1829
1830 /*
1831 * For SYNCH processing, if we get here, we are running
1832 * through the loop again due to a detected lookahead. Set
1833 * flag that we should re-check IRQ status registers again
1834 * before leaving IRQ processing, this can net better
1835 * performance in high throughput situations.
1836 */
Vasanthakumar Thiagarajanfcb82052011-07-18 14:23:31 +05301837 target->chk_irq_status_cnt = 1;
Kalle Valobdcd8172011-07-18 00:22:30 +03001838 }
1839
1840 if (status) {
1841 ath6kl_err("failed to get pending recv messages: %d\n",
1842 status);
Kalle Valobdcd8172011-07-18 00:22:30 +03001843
1844 /* cleanup any packets in sync completion queue */
1845 list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
1846 list_del(&packets->list);
1847 htc_reclaim_rxbuf(target, packets,
1848 &target->endpoint[packets->endpoint]);
1849 }
1850
1851 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1852 ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001853 ath6kl_hif_rx_control(target->dev, false);
Kalle Valobdcd8172011-07-18 00:22:30 +03001854 }
1855 }
1856
1857 /*
1858 * Before leaving, check to see if host ran out of buffers and
1859 * needs to stop the receiver.
1860 */
1861 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
1862 ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001863 ath6kl_hif_rx_control(target->dev, false);
Kalle Valobdcd8172011-07-18 00:22:30 +03001864 }
1865 *num_pkts = n_fetched;
1866
1867 return status;
1868}
1869
1870/*
1871 * Synchronously wait for a control message from the target,
1872 * This function is used at initialization time ONLY. At init messages
1873 * on ENDPOINT 0 are expected.
1874 */
1875static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
1876{
1877 struct htc_packet *packet = NULL;
1878 struct htc_frame_hdr *htc_hdr;
1879 u32 look_ahead;
1880
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001881 if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
Kalle Valobdcd8172011-07-18 00:22:30 +03001882 HTC_TARGET_RESPONSE_TIMEOUT))
1883 return NULL;
1884
Kalle Valoebf29c92011-10-13 15:21:15 +03001885 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001886 "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
Kalle Valobdcd8172011-07-18 00:22:30 +03001887
1888 htc_hdr = (struct htc_frame_hdr *)&look_ahead;
1889
1890 if (htc_hdr->eid != ENDPOINT_0)
1891 return NULL;
1892
1893 packet = htc_get_control_buf(target, false);
1894
1895 if (!packet)
1896 return NULL;
1897
1898 packet->info.rx.rx_flags = 0;
1899 packet->info.rx.exp_hdr = look_ahead;
1900 packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;
1901
1902 if (packet->act_len > packet->buf_len)
1903 goto fail_ctrl_rx;
1904
1905 /* we want synchronous operation */
1906 packet->completion = NULL;
1907
1908 /* get the message from the device, this will block */
Kalle Valo689def92011-09-06 11:10:49 +03001909 if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
Kalle Valobdcd8172011-07-18 00:22:30 +03001910 goto fail_ctrl_rx;
1911
1912 /* process receive header */
Kalle Valo689def92011-09-06 11:10:49 +03001913 packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
Kalle Valobdcd8172011-07-18 00:22:30 +03001914
1915 if (packet->status) {
Kalle Valo689def92011-09-06 11:10:49 +03001916 ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001917 packet->status);
1918 goto fail_ctrl_rx;
1919 }
1920
1921 return packet;
1922
1923fail_ctrl_rx:
1924 if (packet != NULL) {
1925 htc_rxpkt_reset(packet);
1926 reclaim_rx_ctrl_buf(target, packet);
1927 }
1928
1929 return NULL;
1930}
1931
Kalle Vaload226ec2011-08-10 09:49:12 +03001932int ath6kl_htc_add_rxbuf_multiple(struct htc_target *target,
1933 struct list_head *pkt_queue)
Kalle Valobdcd8172011-07-18 00:22:30 +03001934{
1935 struct htc_endpoint *endpoint;
1936 struct htc_packet *first_pkt;
1937 bool rx_unblock = false;
1938 int status = 0, depth;
1939
1940 if (list_empty(pkt_queue))
1941 return -ENOMEM;
1942
1943 first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);
1944
1945 if (first_pkt->endpoint >= ENDPOINT_MAX)
1946 return status;
1947
1948 depth = get_queue_depth(pkt_queue);
1949
Kalle Valoebf29c92011-10-13 15:21:15 +03001950 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001951 "htc rx add multiple ep id %d cnt %d len %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001952 first_pkt->endpoint, depth, first_pkt->buf_len);
1953
1954 endpoint = &target->endpoint[first_pkt->endpoint];
1955
1956 if (target->htc_flags & HTC_OP_STATE_STOPPING) {
1957 struct htc_packet *packet, *tmp_pkt;
1958
1959 /* walk through queue and mark each one canceled */
1960 list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
1961 packet->status = -ECANCELED;
1962 list_del(&packet->list);
Kalle Valo689def92011-09-06 11:10:49 +03001963 ath6kl_htc_rx_complete(endpoint, packet);
Kalle Valobdcd8172011-07-18 00:22:30 +03001964 }
1965
1966 return status;
1967 }
1968
1969 spin_lock_bh(&target->rx_lock);
1970
1971 list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);
1972
1973 /* check if we are blocked waiting for a new buffer */
1974 if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
1975 if (target->ep_waiting == first_pkt->endpoint) {
Kalle Valoebf29c92011-10-13 15:21:15 +03001976 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03001977 "htc rx blocked on ep %d, unblocking\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03001978 target->ep_waiting);
1979 target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
1980 target->ep_waiting = ENDPOINT_MAX;
1981 rx_unblock = true;
1982 }
1983 }
1984
1985 spin_unlock_bh(&target->rx_lock);
1986
1987 if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
1988 /* TODO : implement a buffer threshold count? */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03001989 ath6kl_hif_rx_control(target->dev, true);
Kalle Valobdcd8172011-07-18 00:22:30 +03001990
1991 return status;
1992}
1993
Kalle Vaload226ec2011-08-10 09:49:12 +03001994void ath6kl_htc_flush_rx_buf(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03001995{
1996 struct htc_endpoint *endpoint;
1997 struct htc_packet *packet, *tmp_pkt;
1998 int i;
1999
2000 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2001 endpoint = &target->endpoint[i];
2002 if (!endpoint->svc_id)
2003 /* not in use.. */
2004 continue;
2005
2006 spin_lock_bh(&target->rx_lock);
2007 list_for_each_entry_safe(packet, tmp_pkt,
2008 &endpoint->rx_bufq, list) {
2009 list_del(&packet->list);
2010 spin_unlock_bh(&target->rx_lock);
Kalle Valoebf29c92011-10-13 15:21:15 +03002011 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002012 "htc rx flush pkt 0x%p len %d ep %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002013 packet, packet->buf_len,
2014 packet->endpoint);
2015 dev_kfree_skb(packet->pkt_cntxt);
2016 spin_lock_bh(&target->rx_lock);
2017 }
2018 spin_unlock_bh(&target->rx_lock);
2019 }
2020}
2021
Kalle Vaload226ec2011-08-10 09:49:12 +03002022int ath6kl_htc_conn_service(struct htc_target *target,
2023 struct htc_service_connect_req *conn_req,
2024 struct htc_service_connect_resp *conn_resp)
Kalle Valobdcd8172011-07-18 00:22:30 +03002025{
2026 struct htc_packet *rx_pkt = NULL;
2027 struct htc_packet *tx_pkt = NULL;
2028 struct htc_conn_service_resp *resp_msg;
2029 struct htc_conn_service_msg *conn_msg;
2030 struct htc_endpoint *endpoint;
2031 enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
2032 unsigned int max_msg_sz = 0;
2033 int status = 0;
2034
Kalle Valoebf29c92011-10-13 15:21:15 +03002035 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002036 "htc connect service target 0x%p service id 0x%x\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002037 target, conn_req->svc_id);
2038
2039 if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
2040 /* special case for pseudo control service */
2041 assigned_ep = ENDPOINT_0;
2042 max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
2043 } else {
2044 /* allocate a packet to send to the target */
2045 tx_pkt = htc_get_control_buf(target, true);
2046
2047 if (!tx_pkt)
2048 return -ENOMEM;
2049
2050 conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
2051 memset(conn_msg, 0, sizeof(*conn_msg));
2052 conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
2053 conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
2054 conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);
2055
2056 set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
2057 sizeof(*conn_msg) + conn_msg->svc_meta_len,
2058 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
2059
2060 /* we want synchronous operation */
2061 tx_pkt->completion = NULL;
Kalle Valodfa01042011-09-06 11:10:49 +03002062 ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
2063 status = ath6kl_htc_tx_issue(target, tx_pkt);
Kalle Valobdcd8172011-07-18 00:22:30 +03002064
2065 if (status)
2066 goto fail_tx;
2067
2068 /* wait for response */
2069 rx_pkt = htc_wait_for_ctrl_msg(target);
2070
2071 if (!rx_pkt) {
2072 status = -ENOMEM;
2073 goto fail_tx;
2074 }
2075
2076 resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
2077
2078 if ((le16_to_cpu(resp_msg->msg_id) != HTC_MSG_CONN_SVC_RESP_ID)
2079 || (rx_pkt->act_len < sizeof(*resp_msg))) {
2080 status = -ENOMEM;
2081 goto fail_tx;
2082 }
2083
2084 conn_resp->resp_code = resp_msg->status;
2085 /* check response status */
2086 if (resp_msg->status != HTC_SERVICE_SUCCESS) {
2087 ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
2088 resp_msg->svc_id, resp_msg->status);
2089 status = -ENOMEM;
2090 goto fail_tx;
2091 }
2092
2093 assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
2094 max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
2095 }
2096
2097 if (assigned_ep >= ENDPOINT_MAX || !max_msg_sz) {
2098 status = -ENOMEM;
2099 goto fail_tx;
2100 }
2101
2102 endpoint = &target->endpoint[assigned_ep];
2103 endpoint->eid = assigned_ep;
2104 if (endpoint->svc_id) {
2105 status = -ENOMEM;
2106 goto fail_tx;
2107 }
2108
2109 /* return assigned endpoint to caller */
2110 conn_resp->endpoint = assigned_ep;
2111 conn_resp->len_max = max_msg_sz;
2112
2113 /* setup the endpoint */
2114
2115 /* this marks the endpoint in use */
2116 endpoint->svc_id = conn_req->svc_id;
2117
2118 endpoint->max_txq_depth = conn_req->max_txq_depth;
2119 endpoint->len_max = max_msg_sz;
2120 endpoint->ep_cb = conn_req->ep_cb;
2121 endpoint->cred_dist.svc_id = conn_req->svc_id;
Kalle Valoe8c39792011-10-24 12:17:04 +03002122 endpoint->cred_dist.htc_ep = endpoint;
Kalle Valobdcd8172011-07-18 00:22:30 +03002123 endpoint->cred_dist.endpoint = assigned_ep;
2124 endpoint->cred_dist.cred_sz = target->tgt_cred_sz;
2125
2126 if (conn_req->max_rxmsg_sz) {
2127 /*
2128 * Override cred_per_msg calculation, this optimizes
2129 * the credit-low indications since the host will actually
2130 * issue smaller messages in the Send path.
2131 */
2132 if (conn_req->max_rxmsg_sz > max_msg_sz) {
2133 status = -ENOMEM;
2134 goto fail_tx;
2135 }
2136 endpoint->cred_dist.cred_per_msg =
2137 conn_req->max_rxmsg_sz / target->tgt_cred_sz;
2138 } else
2139 endpoint->cred_dist.cred_per_msg =
2140 max_msg_sz / target->tgt_cred_sz;
2141
2142 if (!endpoint->cred_dist.cred_per_msg)
2143 endpoint->cred_dist.cred_per_msg = 1;
2144
2145 /* save local connection flags */
2146 endpoint->conn_flags = conn_req->flags;
2147
2148fail_tx:
2149 if (tx_pkt)
2150 htc_reclaim_txctrl_buf(target, tx_pkt);
2151
2152 if (rx_pkt) {
2153 htc_rxpkt_reset(rx_pkt);
2154 reclaim_rx_ctrl_buf(target, rx_pkt);
2155 }
2156
2157 return status;
2158}
2159
2160static void reset_ep_state(struct htc_target *target)
2161{
2162 struct htc_endpoint *endpoint;
2163 int i;
2164
2165 for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
2166 endpoint = &target->endpoint[i];
2167 memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
2168 endpoint->svc_id = 0;
2169 endpoint->len_max = 0;
2170 endpoint->max_txq_depth = 0;
2171 memset(&endpoint->ep_st, 0,
2172 sizeof(endpoint->ep_st));
2173 INIT_LIST_HEAD(&endpoint->rx_bufq);
2174 INIT_LIST_HEAD(&endpoint->txq);
2175 endpoint->target = target;
2176 }
2177
2178 /* reset distribution list */
Kalle Valo3c370392011-10-24 12:17:12 +03002179 /* FIXME: free existing entries */
Kalle Valobdcd8172011-07-18 00:22:30 +03002180 INIT_LIST_HEAD(&target->cred_dist_list);
2181}
2182
Kalle Vaload226ec2011-08-10 09:49:12 +03002183int ath6kl_htc_get_rxbuf_num(struct htc_target *target,
2184 enum htc_endpoint_id endpoint)
Kalle Valobdcd8172011-07-18 00:22:30 +03002185{
2186 int num;
2187
2188 spin_lock_bh(&target->rx_lock);
2189 num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
2190 spin_unlock_bh(&target->rx_lock);
2191 return num;
2192}
2193
2194static void htc_setup_msg_bndl(struct htc_target *target)
2195{
Kalle Valobdcd8172011-07-18 00:22:30 +03002196 /* limit what HTC can handle */
2197 target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
2198 target->msg_per_bndl_max);
2199
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302200 if (ath6kl_hif_enable_scatter(target->dev->ar)) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002201 target->msg_per_bndl_max = 0;
2202 return;
2203 }
2204
2205 /* limit bundle what the device layer can handle */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302206 target->msg_per_bndl_max = min(target->max_scat_entries,
Kalle Valobdcd8172011-07-18 00:22:30 +03002207 target->msg_per_bndl_max);
2208
Kalle Valoebf29c92011-10-13 15:21:15 +03002209 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002210 "htc bundling allowed msg_per_bndl_max %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002211 target->msg_per_bndl_max);
2212
2213 /* Max rx bundle size is limited by the max tx bundle size */
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302214 target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
Kalle Valobdcd8172011-07-18 00:22:30 +03002215 /* Max tx bundle size if limited by the extended mbox address range */
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302216 target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
Vasanthakumar Thiagarajan50745af2011-07-18 14:23:29 +05302217 target->max_xfer_szper_scatreq);
Kalle Valobdcd8172011-07-18 00:22:30 +03002218
Kalle Valo471e92f2011-10-13 15:21:37 +03002219 ath6kl_dbg(ATH6KL_DBG_HTC, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302220 target->max_rx_bndl_sz, target->max_tx_bndl_sz);
Kalle Valobdcd8172011-07-18 00:22:30 +03002221
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302222 if (target->max_tx_bndl_sz)
Kalle Valobdcd8172011-07-18 00:22:30 +03002223 target->tx_bndl_enable = true;
2224
Vasanthakumar Thiagarajan3b2f5e52011-07-18 14:23:27 +05302225 if (target->max_rx_bndl_sz)
Kalle Valobdcd8172011-07-18 00:22:30 +03002226 target->rx_bndl_enable = true;
2227
Vasanthakumar Thiagarajan5be88242011-07-18 14:23:28 +05302228 if ((target->tgt_cred_sz % target->block_sz) != 0) {
Kalle Valobdcd8172011-07-18 00:22:30 +03002229 ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
2230 target->tgt_cred_sz);
2231
2232 /*
2233 * Disallow send bundling since the credit size is
2234 * not aligned to a block size the I/O block
2235 * padding will spill into the next credit buffer
2236 * which is fatal.
2237 */
2238 target->tx_bndl_enable = false;
2239 }
2240}
2241
Kalle Vaload226ec2011-08-10 09:49:12 +03002242int ath6kl_htc_wait_target(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002243{
2244 struct htc_packet *packet = NULL;
2245 struct htc_ready_ext_msg *rdy_msg;
2246 struct htc_service_connect_req connect;
2247 struct htc_service_connect_resp resp;
2248 int status;
2249
2250 /* we should be getting 1 control message that the target is ready */
2251 packet = htc_wait_for_ctrl_msg(target);
2252
2253 if (!packet)
2254 return -ENOMEM;
2255
2256 /* we controlled the buffer creation so it's properly aligned */
2257 rdy_msg = (struct htc_ready_ext_msg *)packet->buf;
2258
2259 if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
2260 (packet->act_len < sizeof(struct htc_ready_msg))) {
2261 status = -ENOMEM;
2262 goto fail_wait_target;
2263 }
2264
2265 if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
2266 status = -ENOMEM;
2267 goto fail_wait_target;
2268 }
2269
2270 target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
2271 target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);
2272
Kalle Valoebf29c92011-10-13 15:21:15 +03002273 ath6kl_dbg(ATH6KL_DBG_HTC,
Kalle Valo471e92f2011-10-13 15:21:37 +03002274 "htc target ready credits %d size %d\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002275 target->tgt_creds, target->tgt_cred_sz);
2276
2277 /* check if this is an extended ready message */
2278 if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
2279 /* this is an extended message */
2280 target->htc_tgt_ver = rdy_msg->htc_ver;
2281 target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
2282 } else {
2283 /* legacy */
2284 target->htc_tgt_ver = HTC_VERSION_2P0;
2285 target->msg_per_bndl_max = 0;
2286 }
2287
Kalle Valo471e92f2011-10-13 15:21:37 +03002288 ath6kl_dbg(ATH6KL_DBG_HTC, "htc using protocol %s (%d)\n",
Kalle Valobdcd8172011-07-18 00:22:30 +03002289 (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
2290 target->htc_tgt_ver);
2291
2292 if (target->msg_per_bndl_max > 0)
2293 htc_setup_msg_bndl(target);
2294
2295 /* setup our pseudo HTC control endpoint connection */
2296 memset(&connect, 0, sizeof(connect));
2297 memset(&resp, 0, sizeof(resp));
2298 connect.ep_cb.rx = htc_ctrl_rx;
2299 connect.ep_cb.rx_refill = NULL;
2300 connect.ep_cb.tx_full = NULL;
2301 connect.max_txq_depth = NUM_CONTROL_BUFFERS;
2302 connect.svc_id = HTC_CTRL_RSVD_SVC;
2303
2304 /* connect fake service */
Kalle Vaload226ec2011-08-10 09:49:12 +03002305 status = ath6kl_htc_conn_service((void *)target, &connect, &resp);
Kalle Valobdcd8172011-07-18 00:22:30 +03002306
2307 if (status)
2308 ath6kl_hif_cleanup_scatter(target->dev->ar);
2309
2310fail_wait_target:
2311 if (packet) {
2312 htc_rxpkt_reset(packet);
2313 reclaim_rx_ctrl_buf(target, packet);
2314 }
2315
2316 return status;
2317}
2318
2319/*
2320 * Start HTC, enable interrupts and let the target know
2321 * host has finished setup.
2322 */
Kalle Vaload226ec2011-08-10 09:49:12 +03002323int ath6kl_htc_start(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002324{
2325 struct htc_packet *packet;
2326 int status;
2327
2328 /* Disable interrupts at the chip level */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002329 ath6kl_hif_disable_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002330
2331 target->htc_flags = 0;
2332 target->rx_st_flags = 0;
2333
2334 /* Push control receive buffers into htc control endpoint */
2335 while ((packet = htc_get_control_buf(target, false)) != NULL) {
2336 status = htc_add_rxbuf(target, packet);
2337 if (status)
2338 return status;
2339 }
2340
2341 /* NOTE: the first entry in the distribution list is ENDPOINT_0 */
Kalle Valo3c370392011-10-24 12:17:12 +03002342 ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
Kalle Valofa99e962011-10-24 12:16:55 +03002343 target->tgt_creds);
Kalle Valobdcd8172011-07-18 00:22:30 +03002344
2345 dump_cred_dist_stats(target);
2346
2347 /* Indicate to the target of the setup completion */
2348 status = htc_setup_tx_complete(target);
2349
2350 if (status)
2351 return status;
2352
2353 /* unmask interrupts */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002354 status = ath6kl_hif_unmask_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002355
2356 if (status)
Kalle Vaload226ec2011-08-10 09:49:12 +03002357 ath6kl_htc_stop(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002358
2359 return status;
2360}
2361
2362/* htc_stop: stop interrupt reception, and flush all queued buffers */
Kalle Vaload226ec2011-08-10 09:49:12 +03002363void ath6kl_htc_stop(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002364{
2365 spin_lock_bh(&target->htc_lock);
2366 target->htc_flags |= HTC_OP_STATE_STOPPING;
2367 spin_unlock_bh(&target->htc_lock);
2368
2369 /*
2370 * Masking interrupts is a synchronous operation, when this
2371 * function returns all pending HIF I/O has completed, we can
2372 * safely flush the queues.
2373 */
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002374 ath6kl_hif_mask_intrs(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002375
Kalle Vaload226ec2011-08-10 09:49:12 +03002376 ath6kl_htc_flush_txep_all(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002377
Kalle Vaload226ec2011-08-10 09:49:12 +03002378 ath6kl_htc_flush_rx_buf(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002379
2380 reset_ep_state(target);
2381}
2382
Kalle Vaload226ec2011-08-10 09:49:12 +03002383void *ath6kl_htc_create(struct ath6kl *ar)
Kalle Valobdcd8172011-07-18 00:22:30 +03002384{
2385 struct htc_target *target = NULL;
2386 struct htc_packet *packet;
2387 int status = 0, i = 0;
2388 u32 block_size, ctrl_bufsz;
2389
2390 target = kzalloc(sizeof(*target), GFP_KERNEL);
2391 if (!target) {
2392 ath6kl_err("unable to allocate memory\n");
2393 return NULL;
2394 }
2395
2396 target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
2397 if (!target->dev) {
2398 ath6kl_err("unable to allocate memory\n");
2399 status = -ENOMEM;
2400 goto fail_create_htc;
2401 }
2402
2403 spin_lock_init(&target->htc_lock);
2404 spin_lock_init(&target->rx_lock);
2405 spin_lock_init(&target->tx_lock);
2406
2407 INIT_LIST_HEAD(&target->free_ctrl_txbuf);
2408 INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
2409 INIT_LIST_HEAD(&target->cred_dist_list);
2410
2411 target->dev->ar = ar;
2412 target->dev->htc_cnxt = target;
Kalle Valobdcd8172011-07-18 00:22:30 +03002413 target->ep_waiting = ENDPOINT_MAX;
2414
2415 reset_ep_state(target);
2416
Kalle Valo8e8ddb22011-10-05 12:23:33 +03002417 status = ath6kl_hif_setup(target->dev);
Kalle Valobdcd8172011-07-18 00:22:30 +03002418
2419 if (status)
2420 goto fail_create_htc;
2421
2422 block_size = ar->mbox_info.block_size;
2423
2424 ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
2425 (block_size + HTC_HDR_LENGTH) :
2426 (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);
2427
2428 for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
2429 packet = kzalloc(sizeof(*packet), GFP_KERNEL);
2430 if (!packet)
2431 break;
2432
2433 packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
2434 if (!packet->buf_start) {
2435 kfree(packet);
2436 break;
2437 }
2438
2439 packet->buf_len = ctrl_bufsz;
2440 if (i < NUM_CONTROL_RX_BUFFERS) {
2441 packet->act_len = 0;
2442 packet->buf = packet->buf_start;
2443 packet->endpoint = ENDPOINT_0;
2444 list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
2445 } else
2446 list_add_tail(&packet->list, &target->free_ctrl_txbuf);
2447 }
2448
2449fail_create_htc:
2450 if (i != NUM_CONTROL_BUFFERS || status) {
2451 if (target) {
Kalle Vaload226ec2011-08-10 09:49:12 +03002452 ath6kl_htc_cleanup(target);
Kalle Valobdcd8172011-07-18 00:22:30 +03002453 target = NULL;
2454 }
2455 }
2456
2457 return target;
2458}
2459
2460/* cleanup the HTC instance */
Kalle Vaload226ec2011-08-10 09:49:12 +03002461void ath6kl_htc_cleanup(struct htc_target *target)
Kalle Valobdcd8172011-07-18 00:22:30 +03002462{
2463 struct htc_packet *packet, *tmp_packet;
2464
2465 ath6kl_hif_cleanup_scatter(target->dev->ar);
2466
2467 list_for_each_entry_safe(packet, tmp_packet,
2468 &target->free_ctrl_txbuf, list) {
2469 list_del(&packet->list);
2470 kfree(packet->buf_start);
2471 kfree(packet);
2472 }
2473
2474 list_for_each_entry_safe(packet, tmp_packet,
2475 &target->free_ctrl_rxbuf, list) {
2476 list_del(&packet->list);
2477 kfree(packet->buf_start);
2478 kfree(packet);
2479 }
2480
2481 kfree(target->dev);
2482 kfree(target);
2483}