Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / wireless / ath / ath6kl / hif.c
blobe57da35e59fa78d66566841765a803f2f36e1637
1 /*
2 * Copyright (c) 2007-2011 Atheros Communications Inc.
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.
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.
16 #include "hif.h"
18 #include "core.h"
19 #include "target.h"
20 #include "hif-ops.h"
21 #include "debug.h"
23 #define MAILBOX_FOR_BLOCK_SIZE 1
25 #define ATH6KL_TIME_QUANTUM 10 /* in ms */
27 static int ath6kl_hif_cp_scat_dma_buf(struct hif_scatter_req *req,
28 bool from_dma)
30 u8 *buf;
31 int i;
33 buf = req->virt_dma_buf;
35 for (i = 0; i < req->scat_entries; i++) {
37 if (from_dma)
38 memcpy(req->scat_list[i].buf, buf,
39 req->scat_list[i].len);
40 else
41 memcpy(buf, req->scat_list[i].buf,
42 req->scat_list[i].len);
44 buf += req->scat_list[i].len;
47 return 0;
50 int ath6kl_hif_rw_comp_handler(void *context, int status)
52 struct htc_packet *packet = context;
54 ath6kl_dbg(ATH6KL_DBG_HIF, "hif rw completion pkt 0x%p status %d\n",
55 packet, status);
57 packet->status = status;
58 packet->completion(packet->context, packet);
60 return 0;
62 #define REG_DUMP_COUNT_AR6003 60
63 #define REGISTER_DUMP_LEN_MAX 60
65 static void ath6kl_hif_dump_fw_crash(struct ath6kl *ar)
67 __le32 regdump_val[REGISTER_DUMP_LEN_MAX];
68 u32 i, address, regdump_addr = 0;
69 int ret;
71 if (ar->target_type != TARGET_TYPE_AR6003)
72 return;
74 /* the reg dump pointer is copied to the host interest area */
75 address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_failure_state));
76 address = TARG_VTOP(ar->target_type, address);
78 /* read RAM location through diagnostic window */
79 ret = ath6kl_diag_read32(ar, address, &regdump_addr);
81 if (ret || !regdump_addr) {
82 ath6kl_warn("failed to get ptr to register dump area: %d\n",
83 ret);
84 return;
87 ath6kl_dbg(ATH6KL_DBG_IRQ, "register dump data address 0x%x\n",
88 regdump_addr);
89 regdump_addr = TARG_VTOP(ar->target_type, regdump_addr);
91 /* fetch register dump data */
92 ret = ath6kl_diag_read(ar, regdump_addr, (u8 *)&regdump_val[0],
93 REG_DUMP_COUNT_AR6003 * (sizeof(u32)));
94 if (ret) {
95 ath6kl_warn("failed to get register dump: %d\n", ret);
96 return;
99 ath6kl_info("crash dump:\n");
100 ath6kl_info("hw 0x%x fw %s\n", ar->wiphy->hw_version,
101 ar->wiphy->fw_version);
103 BUILD_BUG_ON(REG_DUMP_COUNT_AR6003 % 4);
105 for (i = 0; i < REG_DUMP_COUNT_AR6003 / 4; i++) {
106 ath6kl_info("%d: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
107 4 * i,
108 le32_to_cpu(regdump_val[i]),
109 le32_to_cpu(regdump_val[i + 1]),
110 le32_to_cpu(regdump_val[i + 2]),
111 le32_to_cpu(regdump_val[i + 3]));
116 static int ath6kl_hif_proc_dbg_intr(struct ath6kl_device *dev)
118 u32 dummy;
119 int ret;
121 ath6kl_warn("firmware crashed\n");
124 * read counter to clear the interrupt, the debug error interrupt is
125 * counter 0.
127 ret = hif_read_write_sync(dev->ar, COUNT_DEC_ADDRESS,
128 (u8 *)&dummy, 4, HIF_RD_SYNC_BYTE_INC);
129 if (ret)
130 ath6kl_warn("Failed to clear debug interrupt: %d\n", ret);
132 ath6kl_hif_dump_fw_crash(dev->ar);
134 return ret;
137 /* mailbox recv message polling */
138 int ath6kl_hif_poll_mboxmsg_rx(struct ath6kl_device *dev, u32 *lk_ahd,
139 int timeout)
141 struct ath6kl_irq_proc_registers *rg;
142 int status = 0, i;
143 u8 htc_mbox = 1 << HTC_MAILBOX;
145 for (i = timeout / ATH6KL_TIME_QUANTUM; i > 0; i--) {
146 /* this is the standard HIF way, load the reg table */
147 status = hif_read_write_sync(dev->ar, HOST_INT_STATUS_ADDRESS,
148 (u8 *) &dev->irq_proc_reg,
149 sizeof(dev->irq_proc_reg),
150 HIF_RD_SYNC_BYTE_INC);
152 if (status) {
153 ath6kl_err("failed to read reg table\n");
154 return status;
157 /* check for MBOX data and valid lookahead */
158 if (dev->irq_proc_reg.host_int_status & htc_mbox) {
159 if (dev->irq_proc_reg.rx_lkahd_valid &
160 htc_mbox) {
162 * Mailbox has a message and the look ahead
163 * is valid.
165 rg = &dev->irq_proc_reg;
166 *lk_ahd =
167 le32_to_cpu(rg->rx_lkahd[HTC_MAILBOX]);
168 break;
172 /* delay a little */
173 mdelay(ATH6KL_TIME_QUANTUM);
174 ath6kl_dbg(ATH6KL_DBG_HIF, "hif retry mbox poll try %d\n", i);
177 if (i == 0) {
178 ath6kl_err("timeout waiting for recv message\n");
179 status = -ETIME;
180 /* check if the target asserted */
181 if (dev->irq_proc_reg.counter_int_status &
182 ATH6KL_TARGET_DEBUG_INTR_MASK)
184 * Target failure handler will be called in case of
185 * an assert.
187 ath6kl_hif_proc_dbg_intr(dev);
190 return status;
194 * Disable packet reception (used in case the host runs out of buffers)
195 * using the interrupt enable registers through the host I/F
197 int ath6kl_hif_rx_control(struct ath6kl_device *dev, bool enable_rx)
199 struct ath6kl_irq_enable_reg regs;
200 int status = 0;
202 ath6kl_dbg(ATH6KL_DBG_HIF, "hif rx %s\n",
203 enable_rx ? "enable" : "disable");
205 /* take the lock to protect interrupt enable shadows */
206 spin_lock_bh(&dev->lock);
208 if (enable_rx)
209 dev->irq_en_reg.int_status_en |=
210 SM(INT_STATUS_ENABLE_MBOX_DATA, 0x01);
211 else
212 dev->irq_en_reg.int_status_en &=
213 ~SM(INT_STATUS_ENABLE_MBOX_DATA, 0x01);
215 memcpy(&regs, &dev->irq_en_reg, sizeof(regs));
217 spin_unlock_bh(&dev->lock);
219 status = hif_read_write_sync(dev->ar, INT_STATUS_ENABLE_ADDRESS,
220 &regs.int_status_en,
221 sizeof(struct ath6kl_irq_enable_reg),
222 HIF_WR_SYNC_BYTE_INC);
224 return status;
227 int ath6kl_hif_submit_scat_req(struct ath6kl_device *dev,
228 struct hif_scatter_req *scat_req, bool read)
230 int status = 0;
232 if (read) {
233 scat_req->req = HIF_RD_SYNC_BLOCK_FIX;
234 scat_req->addr = dev->ar->mbox_info.htc_addr;
235 } else {
236 scat_req->req = HIF_WR_ASYNC_BLOCK_INC;
238 scat_req->addr =
239 (scat_req->len > HIF_MBOX_WIDTH) ?
240 dev->ar->mbox_info.htc_ext_addr :
241 dev->ar->mbox_info.htc_addr;
244 ath6kl_dbg(ATH6KL_DBG_HIF,
245 "hif submit scatter request entries %d len %d mbox 0x%x %s %s\n",
246 scat_req->scat_entries, scat_req->len,
247 scat_req->addr, !read ? "async" : "sync",
248 (read) ? "rd" : "wr");
250 if (!read && scat_req->virt_scat) {
251 status = ath6kl_hif_cp_scat_dma_buf(scat_req, false);
252 if (status) {
253 scat_req->status = status;
254 scat_req->complete(dev->ar->htc_target, scat_req);
255 return 0;
259 status = ath6kl_hif_scat_req_rw(dev->ar, scat_req);
261 if (read) {
262 /* in sync mode, we can touch the scatter request */
263 scat_req->status = status;
264 if (!status && scat_req->virt_scat)
265 scat_req->status =
266 ath6kl_hif_cp_scat_dma_buf(scat_req, true);
269 return status;
272 static int ath6kl_hif_proc_counter_intr(struct ath6kl_device *dev)
274 u8 counter_int_status;
276 ath6kl_dbg(ATH6KL_DBG_IRQ, "counter interrupt\n");
278 counter_int_status = dev->irq_proc_reg.counter_int_status &
279 dev->irq_en_reg.cntr_int_status_en;
281 ath6kl_dbg(ATH6KL_DBG_IRQ,
282 "valid interrupt source(s) in COUNTER_INT_STATUS: 0x%x\n",
283 counter_int_status);
286 * NOTE: other modules like GMBOX may use the counter interrupt for
287 * credit flow control on other counters, we only need to check for
288 * the debug assertion counter interrupt.
290 if (counter_int_status & ATH6KL_TARGET_DEBUG_INTR_MASK)
291 return ath6kl_hif_proc_dbg_intr(dev);
293 return 0;
296 static int ath6kl_hif_proc_err_intr(struct ath6kl_device *dev)
298 int status;
299 u8 error_int_status;
300 u8 reg_buf[4];
302 ath6kl_dbg(ATH6KL_DBG_IRQ, "error interrupt\n");
304 error_int_status = dev->irq_proc_reg.error_int_status & 0x0F;
305 if (!error_int_status) {
306 WARN_ON(1);
307 return -EIO;
310 ath6kl_dbg(ATH6KL_DBG_IRQ,
311 "valid interrupt source(s) in ERROR_INT_STATUS: 0x%x\n",
312 error_int_status);
314 if (MS(ERROR_INT_STATUS_WAKEUP, error_int_status))
315 ath6kl_dbg(ATH6KL_DBG_IRQ, "error : wakeup\n");
317 if (MS(ERROR_INT_STATUS_RX_UNDERFLOW, error_int_status))
318 ath6kl_err("rx underflow\n");
320 if (MS(ERROR_INT_STATUS_TX_OVERFLOW, error_int_status))
321 ath6kl_err("tx overflow\n");
323 /* Clear the interrupt */
324 dev->irq_proc_reg.error_int_status &= ~error_int_status;
326 /* set W1C value to clear the interrupt, this hits the register first */
327 reg_buf[0] = error_int_status;
328 reg_buf[1] = 0;
329 reg_buf[2] = 0;
330 reg_buf[3] = 0;
332 status = hif_read_write_sync(dev->ar, ERROR_INT_STATUS_ADDRESS,
333 reg_buf, 4, HIF_WR_SYNC_BYTE_FIX);
335 if (status)
336 WARN_ON(1);
338 return status;
341 static int ath6kl_hif_proc_cpu_intr(struct ath6kl_device *dev)
343 int status;
344 u8 cpu_int_status;
345 u8 reg_buf[4];
347 ath6kl_dbg(ATH6KL_DBG_IRQ, "cpu interrupt\n");
349 cpu_int_status = dev->irq_proc_reg.cpu_int_status &
350 dev->irq_en_reg.cpu_int_status_en;
351 if (!cpu_int_status) {
352 WARN_ON(1);
353 return -EIO;
356 ath6kl_dbg(ATH6KL_DBG_IRQ,
357 "valid interrupt source(s) in CPU_INT_STATUS: 0x%x\n",
358 cpu_int_status);
360 /* Clear the interrupt */
361 dev->irq_proc_reg.cpu_int_status &= ~cpu_int_status;
364 * Set up the register transfer buffer to hit the register 4 times ,
365 * this is done to make the access 4-byte aligned to mitigate issues
366 * with host bus interconnects that restrict bus transfer lengths to
367 * be a multiple of 4-bytes.
370 /* set W1C value to clear the interrupt, this hits the register first */
371 reg_buf[0] = cpu_int_status;
372 /* the remaining are set to zero which have no-effect */
373 reg_buf[1] = 0;
374 reg_buf[2] = 0;
375 reg_buf[3] = 0;
377 status = hif_read_write_sync(dev->ar, CPU_INT_STATUS_ADDRESS,
378 reg_buf, 4, HIF_WR_SYNC_BYTE_FIX);
380 if (status)
381 WARN_ON(1);
383 return status;
386 /* process pending interrupts synchronously */
387 static int proc_pending_irqs(struct ath6kl_device *dev, bool *done)
389 struct ath6kl_irq_proc_registers *rg;
390 int status = 0;
391 u8 host_int_status = 0;
392 u32 lk_ahd = 0;
393 u8 htc_mbox = 1 << HTC_MAILBOX;
395 ath6kl_dbg(ATH6KL_DBG_IRQ, "proc_pending_irqs: (dev: 0x%p)\n", dev);
398 * NOTE: HIF implementation guarantees that the context of this
399 * call allows us to perform SYNCHRONOUS I/O, that is we can block,
400 * sleep or call any API that can block or switch thread/task
401 * contexts. This is a fully schedulable context.
405 * Process pending intr only when int_status_en is clear, it may
406 * result in unnecessary bus transaction otherwise. Target may be
407 * unresponsive at the time.
409 if (dev->irq_en_reg.int_status_en) {
411 * Read the first 28 bytes of the HTC register table. This
412 * will yield us the value of different int status
413 * registers and the lookahead registers.
415 * length = sizeof(int_status) + sizeof(cpu_int_status)
416 * + sizeof(error_int_status) +
417 * sizeof(counter_int_status) +
418 * sizeof(mbox_frame) + sizeof(rx_lkahd_valid)
419 * + sizeof(hole) + sizeof(rx_lkahd) +
420 * sizeof(int_status_en) +
421 * sizeof(cpu_int_status_en) +
422 * sizeof(err_int_status_en) +
423 * sizeof(cntr_int_status_en);
425 status = hif_read_write_sync(dev->ar, HOST_INT_STATUS_ADDRESS,
426 (u8 *) &dev->irq_proc_reg,
427 sizeof(dev->irq_proc_reg),
428 HIF_RD_SYNC_BYTE_INC);
429 if (status)
430 goto out;
432 if (AR_DBG_LVL_CHECK(ATH6KL_DBG_IRQ))
433 ath6kl_dump_registers(dev, &dev->irq_proc_reg,
434 &dev->irq_en_reg);
436 /* Update only those registers that are enabled */
437 host_int_status = dev->irq_proc_reg.host_int_status &
438 dev->irq_en_reg.int_status_en;
440 /* Look at mbox status */
441 if (host_int_status & htc_mbox) {
443 * Mask out pending mbox value, we use "lookAhead as
444 * the real flag for mbox processing.
446 host_int_status &= ~htc_mbox;
447 if (dev->irq_proc_reg.rx_lkahd_valid &
448 htc_mbox) {
449 rg = &dev->irq_proc_reg;
450 lk_ahd = le32_to_cpu(rg->rx_lkahd[HTC_MAILBOX]);
451 if (!lk_ahd)
452 ath6kl_err("lookAhead is zero!\n");
457 if (!host_int_status && !lk_ahd) {
458 *done = true;
459 goto out;
462 if (lk_ahd) {
463 int fetched = 0;
465 ath6kl_dbg(ATH6KL_DBG_IRQ,
466 "pending mailbox msg, lk_ahd: 0x%X\n", lk_ahd);
468 * Mailbox Interrupt, the HTC layer may issue async
469 * requests to empty the mailbox. When emptying the recv
470 * mailbox we use the async handler above called from the
471 * completion routine of the callers read request. This can
472 * improve performance by reducing context switching when
473 * we rapidly pull packets.
475 status = ath6kl_htc_rxmsg_pending_handler(dev->htc_cnxt,
476 lk_ahd, &fetched);
477 if (status)
478 goto out;
480 if (!fetched)
482 * HTC could not pull any messages out due to lack
483 * of resources.
485 dev->htc_cnxt->chk_irq_status_cnt = 0;
488 /* now handle the rest of them */
489 ath6kl_dbg(ATH6KL_DBG_IRQ,
490 "valid interrupt source(s) for other interrupts: 0x%x\n",
491 host_int_status);
493 if (MS(HOST_INT_STATUS_CPU, host_int_status)) {
494 /* CPU Interrupt */
495 status = ath6kl_hif_proc_cpu_intr(dev);
496 if (status)
497 goto out;
500 if (MS(HOST_INT_STATUS_ERROR, host_int_status)) {
501 /* Error Interrupt */
502 status = ath6kl_hif_proc_err_intr(dev);
503 if (status)
504 goto out;
507 if (MS(HOST_INT_STATUS_COUNTER, host_int_status))
508 /* Counter Interrupt */
509 status = ath6kl_hif_proc_counter_intr(dev);
511 out:
513 * An optimization to bypass reading the IRQ status registers
514 * unecessarily which can re-wake the target, if upper layers
515 * determine that we are in a low-throughput mode, we can rely on
516 * taking another interrupt rather than re-checking the status
517 * registers which can re-wake the target.
519 * NOTE : for host interfaces that makes use of detecting pending
520 * mbox messages at hif can not use this optimization due to
521 * possible side effects, SPI requires the host to drain all
522 * messages from the mailbox before exiting the ISR routine.
525 ath6kl_dbg(ATH6KL_DBG_IRQ,
526 "bypassing irq status re-check, forcing done\n");
528 if (!dev->htc_cnxt->chk_irq_status_cnt)
529 *done = true;
531 ath6kl_dbg(ATH6KL_DBG_IRQ,
532 "proc_pending_irqs: (done:%d, status=%d\n", *done, status);
534 return status;
537 /* interrupt handler, kicks off all interrupt processing */
538 int ath6kl_hif_intr_bh_handler(struct ath6kl *ar)
540 struct ath6kl_device *dev = ar->htc_target->dev;
541 unsigned long timeout;
542 int status = 0;
543 bool done = false;
546 * Reset counter used to flag a re-scan of IRQ status registers on
547 * the target.
549 dev->htc_cnxt->chk_irq_status_cnt = 0;
552 * IRQ processing is synchronous, interrupt status registers can be
553 * re-read.
555 timeout = jiffies + msecs_to_jiffies(ATH6KL_HIF_COMMUNICATION_TIMEOUT);
556 while (time_before(jiffies, timeout) && !done) {
557 status = proc_pending_irqs(dev, &done);
558 if (status)
559 break;
562 return status;
565 static int ath6kl_hif_enable_intrs(struct ath6kl_device *dev)
567 struct ath6kl_irq_enable_reg regs;
568 int status;
570 spin_lock_bh(&dev->lock);
572 /* Enable all but ATH6KL CPU interrupts */
573 dev->irq_en_reg.int_status_en =
574 SM(INT_STATUS_ENABLE_ERROR, 0x01) |
575 SM(INT_STATUS_ENABLE_CPU, 0x01) |
576 SM(INT_STATUS_ENABLE_COUNTER, 0x01);
579 * NOTE: There are some cases where HIF can do detection of
580 * pending mbox messages which is disabled now.
582 dev->irq_en_reg.int_status_en |= SM(INT_STATUS_ENABLE_MBOX_DATA, 0x01);
584 /* Set up the CPU Interrupt status Register */
585 dev->irq_en_reg.cpu_int_status_en = 0;
587 /* Set up the Error Interrupt status Register */
588 dev->irq_en_reg.err_int_status_en =
589 SM(ERROR_STATUS_ENABLE_RX_UNDERFLOW, 0x01) |
590 SM(ERROR_STATUS_ENABLE_TX_OVERFLOW, 0x1);
593 * Enable Counter interrupt status register to get fatal errors for
594 * debugging.
596 dev->irq_en_reg.cntr_int_status_en = SM(COUNTER_INT_STATUS_ENABLE_BIT,
597 ATH6KL_TARGET_DEBUG_INTR_MASK);
598 memcpy(&regs, &dev->irq_en_reg, sizeof(regs));
600 spin_unlock_bh(&dev->lock);
602 status = hif_read_write_sync(dev->ar, INT_STATUS_ENABLE_ADDRESS,
603 &regs.int_status_en, sizeof(regs),
604 HIF_WR_SYNC_BYTE_INC);
606 if (status)
607 ath6kl_err("failed to update interrupt ctl reg err: %d\n",
608 status);
610 return status;
613 int ath6kl_hif_disable_intrs(struct ath6kl_device *dev)
615 struct ath6kl_irq_enable_reg regs;
617 spin_lock_bh(&dev->lock);
618 /* Disable all interrupts */
619 dev->irq_en_reg.int_status_en = 0;
620 dev->irq_en_reg.cpu_int_status_en = 0;
621 dev->irq_en_reg.err_int_status_en = 0;
622 dev->irq_en_reg.cntr_int_status_en = 0;
623 memcpy(&regs, &dev->irq_en_reg, sizeof(regs));
624 spin_unlock_bh(&dev->lock);
626 return hif_read_write_sync(dev->ar, INT_STATUS_ENABLE_ADDRESS,
627 &regs.int_status_en, sizeof(regs),
628 HIF_WR_SYNC_BYTE_INC);
631 /* enable device interrupts */
632 int ath6kl_hif_unmask_intrs(struct ath6kl_device *dev)
634 int status = 0;
637 * Make sure interrupt are disabled before unmasking at the HIF
638 * layer. The rationale here is that between device insertion
639 * (where we clear the interrupts the first time) and when HTC
640 * is finally ready to handle interrupts, other software can perform
641 * target "soft" resets. The ATH6KL interrupt enables reset back to an
642 * "enabled" state when this happens.
644 ath6kl_hif_disable_intrs(dev);
646 /* unmask the host controller interrupts */
647 ath6kl_hif_irq_enable(dev->ar);
648 status = ath6kl_hif_enable_intrs(dev);
650 return status;
653 /* disable all device interrupts */
654 int ath6kl_hif_mask_intrs(struct ath6kl_device *dev)
657 * Mask the interrupt at the HIF layer to avoid any stray interrupt
658 * taken while we zero out our shadow registers in
659 * ath6kl_hif_disable_intrs().
661 ath6kl_hif_irq_disable(dev->ar);
663 return ath6kl_hif_disable_intrs(dev);
666 int ath6kl_hif_setup(struct ath6kl_device *dev)
668 int status = 0;
670 spin_lock_init(&dev->lock);
673 * NOTE: we actually get the block size of a mailbox other than 0,
674 * for SDIO the block size on mailbox 0 is artificially set to 1.
675 * So we use the block size that is set for the other 3 mailboxes.
677 dev->htc_cnxt->block_sz = dev->ar->mbox_info.block_size;
679 /* must be a power of 2 */
680 if ((dev->htc_cnxt->block_sz & (dev->htc_cnxt->block_sz - 1)) != 0) {
681 WARN_ON(1);
682 status = -EINVAL;
683 goto fail_setup;
686 /* assemble mask, used for padding to a block */
687 dev->htc_cnxt->block_mask = dev->htc_cnxt->block_sz - 1;
689 ath6kl_dbg(ATH6KL_DBG_HIF, "hif block size %d mbox addr 0x%x\n",
690 dev->htc_cnxt->block_sz, dev->ar->mbox_info.htc_addr);
692 status = ath6kl_hif_disable_intrs(dev);
694 fail_setup:
695 return status;