iwlwifi: document the transport layer
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / iwlwifi / iwl-trans-pcie-rx.c
CommitLineData
ab697a9f
EG
1/******************************************************************************
2 *
fb4961db 3 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
ab697a9f
EG
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29#include <linux/sched.h>
30#include <linux/wait.h>
1a361cd8 31#include <linux/gfp.h>
ab697a9f 32
522376d2 33/*TODO: Remove include to iwl-core.h*/
ab697a9f
EG
34#include "iwl-core.h"
35#include "iwl-io.h"
c17d0681 36#include "iwl-trans-pcie-int.h"
ab697a9f 37
a5916977
GG
38#ifdef CONFIG_IWLWIFI_IDI
39#include "iwl-amfh.h"
40#endif
41
ab697a9f
EG
42/******************************************************************************
43 *
44 * RX path functions
45 *
46 ******************************************************************************/
47
48/*
49 * Rx theory of operation
50 *
51 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
52 * each of which point to Receive Buffers to be filled by the NIC. These get
53 * used not only for Rx frames, but for any command response or notification
54 * from the NIC. The driver and NIC manage the Rx buffers by means
55 * of indexes into the circular buffer.
56 *
57 * Rx Queue Indexes
58 * The host/firmware share two index registers for managing the Rx buffers.
59 *
60 * The READ index maps to the first position that the firmware may be writing
61 * to -- the driver can read up to (but not including) this position and get
62 * good data.
63 * The READ index is managed by the firmware once the card is enabled.
64 *
65 * The WRITE index maps to the last position the driver has read from -- the
66 * position preceding WRITE is the last slot the firmware can place a packet.
67 *
68 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
69 * WRITE = READ.
70 *
71 * During initialization, the host sets up the READ queue position to the first
72 * INDEX position, and WRITE to the last (READ - 1 wrapped)
73 *
74 * When the firmware places a packet in a buffer, it will advance the READ index
75 * and fire the RX interrupt. The driver can then query the READ index and
76 * process as many packets as possible, moving the WRITE index forward as it
77 * resets the Rx queue buffers with new memory.
78 *
79 * The management in the driver is as follows:
80 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
81 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
82 * to replenish the iwl->rxq->rx_free.
83 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
84 * iwl->rxq is replenished and the READ INDEX is updated (updating the
85 * 'processed' and 'read' driver indexes as well)
86 * + A received packet is processed and handed to the kernel network stack,
87 * detached from the iwl->rxq. The driver 'processed' index is updated.
88 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
89 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
90 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
91 * were enough free buffers and RX_STALLED is set it is cleared.
92 *
93 *
94 * Driver sequence:
95 *
96 * iwl_rx_queue_alloc() Allocates rx_free
97 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
98 * iwl_rx_queue_restock
99 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
100 * queue, updates firmware pointers, and updates
101 * the WRITE index. If insufficient rx_free buffers
102 * are available, schedules iwl_rx_replenish
103 *
104 * -- enable interrupts --
105 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
106 * READ INDEX, detaching the SKB from the pool.
107 * Moves the packet buffer from queue to rx_used.
108 * Calls iwl_rx_queue_restock to refill any empty
109 * slots.
110 * ...
111 *
112 */
113
114/**
115 * iwl_rx_queue_space - Return number of free slots available in queue.
116 */
117static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
118{
119 int s = q->read - q->write;
120 if (s <= 0)
121 s += RX_QUEUE_SIZE;
122 /* keep some buffer to not confuse full and empty queue */
123 s -= 2;
124 if (s < 0)
125 s = 0;
126 return s;
127}
128
129/**
130 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
131 */
5a878bf6 132void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
ab697a9f
EG
133 struct iwl_rx_queue *q)
134{
135 unsigned long flags;
136 u32 reg;
137
138 spin_lock_irqsave(&q->lock, flags);
139
140 if (q->need_update == 0)
141 goto exit_unlock;
142
fd656935 143 if (hw_params(trans).shadow_reg_enable) {
ab697a9f
EG
144 /* shadow register enabled */
145 /* Device expects a multiple of 8 */
146 q->write_actual = (q->write & ~0x7);
1042db2a 147 iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
ab697a9f
EG
148 } else {
149 /* If power-saving is in use, make sure device is awake */
5a878bf6 150 if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
1042db2a 151 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
ab697a9f
EG
152
153 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
5a878bf6 154 IWL_DEBUG_INFO(trans,
ab697a9f
EG
155 "Rx queue requesting wakeup,"
156 " GP1 = 0x%x\n", reg);
1042db2a 157 iwl_set_bit(trans, CSR_GP_CNTRL,
ab697a9f
EG
158 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
159 goto exit_unlock;
160 }
161
162 q->write_actual = (q->write & ~0x7);
1042db2a 163 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
ab697a9f
EG
164 q->write_actual);
165
166 /* Else device is assumed to be awake */
167 } else {
168 /* Device expects a multiple of 8 */
169 q->write_actual = (q->write & ~0x7);
1042db2a 170 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
ab697a9f
EG
171 q->write_actual);
172 }
173 }
174 q->need_update = 0;
175
176 exit_unlock:
177 spin_unlock_irqrestore(&q->lock, flags);
178}
179
180/**
181 * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
182 */
5a878bf6 183static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
ab697a9f
EG
184{
185 return cpu_to_le32((u32)(dma_addr >> 8));
186}
187
188/**
189 * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
190 *
191 * If there are slots in the RX queue that need to be restocked,
192 * and we have free pre-allocated buffers, fill the ranks as much
193 * as we can, pulling from rx_free.
194 *
195 * This moves the 'write' index forward to catch up with 'processed', and
196 * also updates the memory address in the firmware to reference the new
197 * target buffer.
198 */
5a878bf6 199static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
ab697a9f 200{
5a878bf6
EG
201 struct iwl_trans_pcie *trans_pcie =
202 IWL_TRANS_GET_PCIE_TRANS(trans);
203
204 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
ab697a9f
EG
205 struct list_head *element;
206 struct iwl_rx_mem_buffer *rxb;
207 unsigned long flags;
208
209 spin_lock_irqsave(&rxq->lock, flags);
210 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
211 /* The overwritten rxb must be a used one */
212 rxb = rxq->queue[rxq->write];
213 BUG_ON(rxb && rxb->page);
214
215 /* Get next free Rx buffer, remove from free list */
216 element = rxq->rx_free.next;
217 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
218 list_del(element);
219
220 /* Point to Rx buffer via next RBD in circular buffer */
5a878bf6 221 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
ab697a9f
EG
222 rxq->queue[rxq->write] = rxb;
223 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
224 rxq->free_count--;
225 }
226 spin_unlock_irqrestore(&rxq->lock, flags);
227 /* If the pre-allocated buffer pool is dropping low, schedule to
228 * refill it */
229 if (rxq->free_count <= RX_LOW_WATERMARK)
1ee158d8 230 schedule_work(&trans_pcie->rx_replenish);
ab697a9f
EG
231
232
233 /* If we've added more space for the firmware to place data, tell it.
234 * Increment device's write pointer in multiples of 8. */
235 if (rxq->write_actual != (rxq->write & ~0x7)) {
236 spin_lock_irqsave(&rxq->lock, flags);
237 rxq->need_update = 1;
238 spin_unlock_irqrestore(&rxq->lock, flags);
5a878bf6 239 iwl_rx_queue_update_write_ptr(trans, rxq);
ab697a9f
EG
240 }
241}
242
243/**
244 * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
245 *
246 * When moving to rx_free an SKB is allocated for the slot.
247 *
248 * Also restock the Rx queue via iwl_rx_queue_restock.
249 * This is called as a scheduled work item (except for during initialization)
250 */
5a878bf6 251static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
ab697a9f 252{
5a878bf6
EG
253 struct iwl_trans_pcie *trans_pcie =
254 IWL_TRANS_GET_PCIE_TRANS(trans);
255
256 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
ab697a9f
EG
257 struct list_head *element;
258 struct iwl_rx_mem_buffer *rxb;
259 struct page *page;
260 unsigned long flags;
261 gfp_t gfp_mask = priority;
262
263 while (1) {
264 spin_lock_irqsave(&rxq->lock, flags);
265 if (list_empty(&rxq->rx_used)) {
266 spin_unlock_irqrestore(&rxq->lock, flags);
267 return;
268 }
269 spin_unlock_irqrestore(&rxq->lock, flags);
270
271 if (rxq->free_count > RX_LOW_WATERMARK)
272 gfp_mask |= __GFP_NOWARN;
273
5a878bf6 274 if (hw_params(trans).rx_page_order > 0)
ab697a9f
EG
275 gfp_mask |= __GFP_COMP;
276
277 /* Alloc a new receive buffer */
d6189124 278 page = alloc_pages(gfp_mask,
5a878bf6 279 hw_params(trans).rx_page_order);
ab697a9f
EG
280 if (!page) {
281 if (net_ratelimit())
5a878bf6 282 IWL_DEBUG_INFO(trans, "alloc_pages failed, "
d6189124 283 "order: %d\n",
5a878bf6 284 hw_params(trans).rx_page_order);
ab697a9f
EG
285
286 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
287 net_ratelimit())
5a878bf6 288 IWL_CRIT(trans, "Failed to alloc_pages with %s."
ab697a9f
EG
289 "Only %u free buffers remaining.\n",
290 priority == GFP_ATOMIC ?
291 "GFP_ATOMIC" : "GFP_KERNEL",
292 rxq->free_count);
293 /* We don't reschedule replenish work here -- we will
294 * call the restock method and if it still needs
295 * more buffers it will schedule replenish */
296 return;
297 }
298
299 spin_lock_irqsave(&rxq->lock, flags);
300
301 if (list_empty(&rxq->rx_used)) {
302 spin_unlock_irqrestore(&rxq->lock, flags);
5a878bf6 303 __free_pages(page, hw_params(trans).rx_page_order);
ab697a9f
EG
304 return;
305 }
306 element = rxq->rx_used.next;
307 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
308 list_del(element);
309
310 spin_unlock_irqrestore(&rxq->lock, flags);
311
312 BUG_ON(rxb->page);
313 rxb->page = page;
314 /* Get physical address of the RB */
1042db2a 315 rxb->page_dma = dma_map_page(trans->dev, page, 0,
5a878bf6 316 PAGE_SIZE << hw_params(trans).rx_page_order,
ab697a9f
EG
317 DMA_FROM_DEVICE);
318 /* dma address must be no more than 36 bits */
319 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
320 /* and also 256 byte aligned! */
321 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
322
323 spin_lock_irqsave(&rxq->lock, flags);
324
325 list_add_tail(&rxb->list, &rxq->rx_free);
326 rxq->free_count++;
327
328 spin_unlock_irqrestore(&rxq->lock, flags);
329 }
330}
331
5a878bf6 332void iwlagn_rx_replenish(struct iwl_trans *trans)
ab697a9f 333{
7b11488f 334 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
ab697a9f
EG
335 unsigned long flags;
336
5a878bf6 337 iwlagn_rx_allocate(trans, GFP_KERNEL);
ab697a9f 338
7b11488f 339 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
5a878bf6 340 iwlagn_rx_queue_restock(trans);
7b11488f 341 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
ab697a9f
EG
342}
343
5a878bf6 344static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
ab697a9f 345{
5a878bf6 346 iwlagn_rx_allocate(trans, GFP_ATOMIC);
ab697a9f 347
5a878bf6 348 iwlagn_rx_queue_restock(trans);
ab697a9f
EG
349}
350
351void iwl_bg_rx_replenish(struct work_struct *data)
352{
5a878bf6
EG
353 struct iwl_trans_pcie *trans_pcie =
354 container_of(data, struct iwl_trans_pcie, rx_replenish);
ab697a9f 355
1ee158d8 356 iwlagn_rx_replenish(trans_pcie->trans);
ab697a9f
EG
357}
358
359/**
360 * iwl_rx_handle - Main entry function for receiving responses from uCode
361 *
362 * Uses the priv->rx_handlers callback function array to invoke
363 * the appropriate handlers, including command responses,
364 * frame-received notifications, and other notifications.
365 */
5a878bf6 366static void iwl_rx_handle(struct iwl_trans *trans)
ab697a9f
EG
367{
368 struct iwl_rx_mem_buffer *rxb;
369 struct iwl_rx_packet *pkt;
5a878bf6
EG
370 struct iwl_trans_pcie *trans_pcie =
371 IWL_TRANS_GET_PCIE_TRANS(trans);
372 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
247c61d6
EG
373 struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
374 struct iwl_device_cmd *cmd;
ab697a9f
EG
375 u32 r, i;
376 int reclaim;
377 unsigned long flags;
378 u8 fill_rx = 0;
379 u32 count = 8;
380 int total_empty;
247c61d6 381 int index, cmd_index;
ab697a9f
EG
382
383 /* uCode's read index (stored in shared DRAM) indicates the last Rx
384 * buffer that the driver may process (last buffer filled by ucode). */
385 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
386 i = rxq->read;
387
388 /* Rx interrupt, but nothing sent from uCode */
389 if (i == r)
5a878bf6 390 IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
ab697a9f
EG
391
392 /* calculate total frames need to be restock after handling RX */
393 total_empty = r - rxq->write_actual;
394 if (total_empty < 0)
395 total_empty += RX_QUEUE_SIZE;
396
397 if (total_empty > (RX_QUEUE_SIZE / 2))
398 fill_rx = 1;
399
400 while (i != r) {
247c61d6 401 int len, err;
d56da920 402 u16 sequence;
ab697a9f
EG
403
404 rxb = rxq->queue[i];
405
406 /* If an RXB doesn't have a Rx queue slot associated with it,
407 * then a bug has been introduced in the queue refilling
408 * routines -- catch it here */
409 if (WARN_ON(rxb == NULL)) {
410 i = (i + 1) & RX_QUEUE_MASK;
411 continue;
412 }
413
414 rxq->queue[i] = NULL;
415
1042db2a 416 dma_unmap_page(trans->dev, rxb->page_dma,
5a878bf6 417 PAGE_SIZE << hw_params(trans).rx_page_order,
ab697a9f
EG
418 DMA_FROM_DEVICE);
419 pkt = rxb_addr(rxb);
420
5a878bf6 421 IWL_DEBUG_RX(trans, "r = %d, i = %d, %s, 0x%02x\n", r,
ab697a9f
EG
422 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
423
424 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
425 len += sizeof(u32); /* account for status word */
5a878bf6 426 trace_iwlwifi_dev_rx(priv(trans), pkt, len);
ab697a9f
EG
427
428 /* Reclaim a command buffer only if this packet is a response
429 * to a (driver-originated) command.
430 * If the packet (e.g. Rx frame) originated from uCode,
431 * there is no command buffer to reclaim.
432 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
433 * but apparently a few don't get set; catch them here. */
434 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
435 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
436 (pkt->hdr.cmd != REPLY_RX) &&
437 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
438 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
439 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
440 (pkt->hdr.cmd != REPLY_TX);
441
17a68dd7 442 sequence = le16_to_cpu(pkt->hdr.sequence);
247c61d6
EG
443 index = SEQ_TO_INDEX(sequence);
444 cmd_index = get_cmd_index(&txq->q, index);
445
446 if (reclaim)
447 cmd = txq->cmd[cmd_index];
448 else
449 cmd = NULL;
17a68dd7
EG
450
451 /* warn if this is cmd response / notification and the uCode
452 * didn't set the SEQ_RX_FRAME for a frame that is
d56da920
EG
453 * uCode-originated
454 * If you saw this code after the second half of 2012, then
455 * please remove it
456 */
457 WARN(pkt->hdr.cmd != REPLY_TX && reclaim == false &&
17a68dd7
EG
458 (!(pkt->hdr.sequence & SEQ_RX_FRAME)),
459 "reclaim is false, SEQ_RX_FRAME unset: %s\n",
460 get_cmd_string(pkt->hdr.cmd));
461
247c61d6 462 err = iwl_rx_dispatch(priv(trans), rxb, cmd);
ab697a9f
EG
463
464 /*
465 * XXX: After here, we should always check rxb->page
466 * against NULL before touching it or its virtual
467 * memory (pkt). Because some rx_handler might have
468 * already taken or freed the pages.
469 */
470
471 if (reclaim) {
472 /* Invoke any callbacks, transfer the buffer to caller,
473 * and fire off the (possibly) blocking
e6bb4c9c 474 * iwl_trans_send_cmd()
ab697a9f
EG
475 * as we reclaim the driver command queue */
476 if (rxb->page)
247c61d6 477 iwl_tx_cmd_complete(trans, rxb, err);
ab697a9f 478 else
5a878bf6 479 IWL_WARN(trans, "Claim null rxb?\n");
ab697a9f
EG
480 }
481
482 /* Reuse the page if possible. For notification packets and
483 * SKBs that fail to Rx correctly, add them back into the
484 * rx_free list for reuse later. */
485 spin_lock_irqsave(&rxq->lock, flags);
486 if (rxb->page != NULL) {
1042db2a 487 rxb->page_dma = dma_map_page(trans->dev, rxb->page,
d6189124 488 0, PAGE_SIZE <<
5a878bf6 489 hw_params(trans).rx_page_order,
ab697a9f
EG
490 DMA_FROM_DEVICE);
491 list_add_tail(&rxb->list, &rxq->rx_free);
492 rxq->free_count++;
493 } else
494 list_add_tail(&rxb->list, &rxq->rx_used);
495
496 spin_unlock_irqrestore(&rxq->lock, flags);
497
498 i = (i + 1) & RX_QUEUE_MASK;
499 /* If there are a lot of unused frames,
500 * restock the Rx queue so ucode wont assert. */
501 if (fill_rx) {
502 count++;
503 if (count >= 8) {
504 rxq->read = i;
5a878bf6 505 iwlagn_rx_replenish_now(trans);
ab697a9f
EG
506 count = 0;
507 }
508 }
509 }
510
511 /* Backtrack one entry */
512 rxq->read = i;
513 if (fill_rx)
5a878bf6 514 iwlagn_rx_replenish_now(trans);
ab697a9f 515 else
5a878bf6 516 iwlagn_rx_queue_restock(trans);
ab697a9f
EG
517}
518
7ff94706
EG
519static const char * const desc_lookup_text[] = {
520 "OK",
521 "FAIL",
522 "BAD_PARAM",
523 "BAD_CHECKSUM",
524 "NMI_INTERRUPT_WDG",
525 "SYSASSERT",
526 "FATAL_ERROR",
527 "BAD_COMMAND",
528 "HW_ERROR_TUNE_LOCK",
529 "HW_ERROR_TEMPERATURE",
530 "ILLEGAL_CHAN_FREQ",
531 "VCC_NOT_STABLE",
532 "FH_ERROR",
533 "NMI_INTERRUPT_HOST",
534 "NMI_INTERRUPT_ACTION_PT",
535 "NMI_INTERRUPT_UNKNOWN",
536 "UCODE_VERSION_MISMATCH",
537 "HW_ERROR_ABS_LOCK",
538 "HW_ERROR_CAL_LOCK_FAIL",
539 "NMI_INTERRUPT_INST_ACTION_PT",
540 "NMI_INTERRUPT_DATA_ACTION_PT",
541 "NMI_TRM_HW_ER",
542 "NMI_INTERRUPT_TRM",
543 "NMI_INTERRUPT_BREAK_POINT",
544 "DEBUG_0",
545 "DEBUG_1",
546 "DEBUG_2",
547 "DEBUG_3",
548};
549
550static struct { char *name; u8 num; } advanced_lookup[] = {
551 { "NMI_INTERRUPT_WDG", 0x34 },
552 { "SYSASSERT", 0x35 },
553 { "UCODE_VERSION_MISMATCH", 0x37 },
554 { "BAD_COMMAND", 0x38 },
555 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
556 { "FATAL_ERROR", 0x3D },
557 { "NMI_TRM_HW_ERR", 0x46 },
558 { "NMI_INTERRUPT_TRM", 0x4C },
559 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
560 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
561 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
562 { "NMI_INTERRUPT_HOST", 0x66 },
563 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
564 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
565 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
566 { "ADVANCED_SYSASSERT", 0 },
567};
568
569static const char *desc_lookup(u32 num)
570{
571 int i;
572 int max = ARRAY_SIZE(desc_lookup_text);
573
574 if (num < max)
575 return desc_lookup_text[num];
576
577 max = ARRAY_SIZE(advanced_lookup) - 1;
578 for (i = 0; i < max; i++) {
579 if (advanced_lookup[i].num == num)
580 break;
581 }
582 return advanced_lookup[i].name;
583}
584
585#define ERROR_START_OFFSET (1 * sizeof(u32))
586#define ERROR_ELEM_SIZE (7 * sizeof(u32))
587
6bb78847 588static void iwl_dump_nic_error_log(struct iwl_trans *trans)
7ff94706
EG
589{
590 u32 base;
591 struct iwl_error_event_table table;
6bb78847 592 struct iwl_priv *priv = priv(trans);
1f7b6172
EG
593 struct iwl_trans_pcie *trans_pcie =
594 IWL_TRANS_GET_PCIE_TRANS(trans);
7ff94706 595
ae6130fc 596 base = trans->shrd->device_pointers.error_event_table;
3d6acefc 597 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
7ff94706
EG
598 if (!base)
599 base = priv->init_errlog_ptr;
600 } else {
601 if (!base)
602 base = priv->inst_errlog_ptr;
603 }
604
605 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
6bb78847 606 IWL_ERR(trans,
7ff94706
EG
607 "Not valid error log pointer 0x%08X for %s uCode\n",
608 base,
3d6acefc 609 (trans->shrd->ucode_type == IWL_UCODE_INIT)
7ff94706
EG
610 ? "Init" : "RT");
611 return;
612 }
613
1042db2a 614 iwl_read_targ_mem_words(trans(priv), base, &table, sizeof(table));
7ff94706
EG
615
616 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
6bb78847
EG
617 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
618 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
619 trans->shrd->status, table.valid);
7ff94706
EG
620 }
621
1f7b6172 622 trans_pcie->isr_stats.err_code = table.error_id;
7ff94706
EG
623
624 trace_iwlwifi_dev_ucode_error(priv, table.error_id, table.tsf_low,
625 table.data1, table.data2, table.line,
626 table.blink1, table.blink2, table.ilink1,
627 table.ilink2, table.bcon_time, table.gp1,
628 table.gp2, table.gp3, table.ucode_ver,
629 table.hw_ver, table.brd_ver);
6bb78847 630 IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
7ff94706 631 desc_lookup(table.error_id));
6bb78847
EG
632 IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
633 IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
634 IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
635 IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
636 IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
637 IWL_ERR(trans, "0x%08X | data1\n", table.data1);
638 IWL_ERR(trans, "0x%08X | data2\n", table.data2);
639 IWL_ERR(trans, "0x%08X | line\n", table.line);
640 IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
641 IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
642 IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
643 IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
644 IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
645 IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
646 IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
647 IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
648 IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
649 IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
d332f591
WYG
650
651 IWL_ERR(trans, "0x%08X | isr0\n", table.isr0);
652 IWL_ERR(trans, "0x%08X | isr1\n", table.isr1);
653 IWL_ERR(trans, "0x%08X | isr2\n", table.isr2);
654 IWL_ERR(trans, "0x%08X | isr3\n", table.isr3);
655 IWL_ERR(trans, "0x%08X | isr4\n", table.isr4);
656 IWL_ERR(trans, "0x%08X | isr_pref\n", table.isr_pref);
657 IWL_ERR(trans, "0x%08X | wait_event\n", table.wait_event);
658 IWL_ERR(trans, "0x%08X | l2p_control\n", table.l2p_control);
659 IWL_ERR(trans, "0x%08X | l2p_duration\n", table.l2p_duration);
660 IWL_ERR(trans, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
661 IWL_ERR(trans, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
662 IWL_ERR(trans, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
663 IWL_ERR(trans, "0x%08X | timestamp\n", table.u_timestamp);
664 IWL_ERR(trans, "0x%08X | flow_handler\n", table.flow_handler);
7ff94706
EG
665}
666
667/**
668 * iwl_irq_handle_error - called for HW or SW error interrupt from card
669 */
6bb78847 670static void iwl_irq_handle_error(struct iwl_trans *trans)
7ff94706 671{
6bb78847 672 struct iwl_priv *priv = priv(trans);
7ff94706 673 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
38622419 674 if (cfg(priv)->internal_wimax_coex &&
1042db2a 675 (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
7ff94706 676 APMS_CLK_VAL_MRB_FUNC_MODE) ||
1042db2a 677 (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
7ff94706
EG
678 APMG_PS_CTRL_VAL_RESET_REQ))) {
679 /*
680 * Keep the restart process from trying to send host
681 * commands by clearing the ready bit.
682 */
6bb78847
EG
683 clear_bit(STATUS_READY, &trans->shrd->status);
684 clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
effd4d9a 685 wake_up(&priv->shrd->wait_command_queue);
6bb78847 686 IWL_ERR(trans, "RF is used by WiMAX\n");
7ff94706
EG
687 return;
688 }
689
6bb78847 690 IWL_ERR(trans, "Loaded firmware version: %s\n",
7ff94706
EG
691 priv->hw->wiphy->fw_version);
692
6bb78847
EG
693 iwl_dump_nic_error_log(trans);
694 iwl_dump_csr(trans);
695 iwl_dump_fh(trans, NULL, false);
696 iwl_dump_nic_event_log(trans, false, NULL, false);
7ff94706 697#ifdef CONFIG_IWLWIFI_DEBUG
6bb78847 698 if (iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS)
522376d2 699 iwl_print_rx_config_cmd(priv(trans), IWL_RXON_CTX_BSS);
7ff94706
EG
700#endif
701
702 iwlagn_fw_error(priv, false);
703}
704
705#define EVENT_START_OFFSET (4 * sizeof(u32))
706
707/**
708 * iwl_print_event_log - Dump error event log to syslog
709 *
710 */
6bb78847 711static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
7ff94706
EG
712 u32 num_events, u32 mode,
713 int pos, char **buf, size_t bufsz)
714{
715 u32 i;
716 u32 base; /* SRAM byte address of event log header */
717 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
718 u32 ptr; /* SRAM byte address of log data */
719 u32 ev, time, data; /* event log data */
720 unsigned long reg_flags;
6bb78847 721 struct iwl_priv *priv = priv(trans);
7ff94706
EG
722
723 if (num_events == 0)
724 return pos;
725
ae6130fc 726 base = trans->shrd->device_pointers.log_event_table;
3d6acefc 727 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
7ff94706
EG
728 if (!base)
729 base = priv->init_evtlog_ptr;
730 } else {
731 if (!base)
732 base = priv->inst_evtlog_ptr;
733 }
734
735 if (mode == 0)
736 event_size = 2 * sizeof(u32);
737 else
738 event_size = 3 * sizeof(u32);
739
740 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
741
742 /* Make sure device is powered up for SRAM reads */
1042db2a
EG
743 spin_lock_irqsave(&trans->reg_lock, reg_flags);
744 iwl_grab_nic_access(trans);
7ff94706
EG
745
746 /* Set starting address; reads will auto-increment */
1042db2a 747 iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
7ff94706
EG
748 rmb();
749
750 /* "time" is actually "data" for mode 0 (no timestamp).
751 * place event id # at far right for easier visual parsing. */
752 for (i = 0; i < num_events; i++) {
1042db2a
EG
753 ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
754 time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
7ff94706
EG
755 if (mode == 0) {
756 /* data, ev */
757 if (bufsz) {
758 pos += scnprintf(*buf + pos, bufsz - pos,
759 "EVT_LOG:0x%08x:%04u\n",
760 time, ev);
761 } else {
762 trace_iwlwifi_dev_ucode_event(priv, 0,
763 time, ev);
6bb78847 764 IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
7ff94706
EG
765 time, ev);
766 }
767 } else {
1042db2a 768 data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
7ff94706
EG
769 if (bufsz) {
770 pos += scnprintf(*buf + pos, bufsz - pos,
771 "EVT_LOGT:%010u:0x%08x:%04u\n",
772 time, data, ev);
773 } else {
6bb78847 774 IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
7ff94706
EG
775 time, data, ev);
776 trace_iwlwifi_dev_ucode_event(priv, time,
777 data, ev);
778 }
779 }
780 }
781
782 /* Allow device to power down */
1042db2a
EG
783 iwl_release_nic_access(trans);
784 spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
7ff94706
EG
785 return pos;
786}
787
788/**
789 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
790 */
6bb78847 791static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
7ff94706
EG
792 u32 num_wraps, u32 next_entry,
793 u32 size, u32 mode,
794 int pos, char **buf, size_t bufsz)
795{
796 /*
797 * display the newest DEFAULT_LOG_ENTRIES entries
798 * i.e the entries just before the next ont that uCode would fill.
799 */
800 if (num_wraps) {
801 if (next_entry < size) {
6bb78847 802 pos = iwl_print_event_log(trans,
7ff94706
EG
803 capacity - (size - next_entry),
804 size - next_entry, mode,
805 pos, buf, bufsz);
6bb78847 806 pos = iwl_print_event_log(trans, 0,
7ff94706
EG
807 next_entry, mode,
808 pos, buf, bufsz);
809 } else
6bb78847 810 pos = iwl_print_event_log(trans, next_entry - size,
7ff94706
EG
811 size, mode, pos, buf, bufsz);
812 } else {
813 if (next_entry < size) {
6bb78847 814 pos = iwl_print_event_log(trans, 0, next_entry,
7ff94706
EG
815 mode, pos, buf, bufsz);
816 } else {
6bb78847 817 pos = iwl_print_event_log(trans, next_entry - size,
7ff94706
EG
818 size, mode, pos, buf, bufsz);
819 }
820 }
821 return pos;
822}
823
824#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
825
6bb78847 826int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
7ff94706
EG
827 char **buf, bool display)
828{
829 u32 base; /* SRAM byte address of event log header */
830 u32 capacity; /* event log capacity in # entries */
831 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
832 u32 num_wraps; /* # times uCode wrapped to top of log */
833 u32 next_entry; /* index of next entry to be written by uCode */
834 u32 size; /* # entries that we'll print */
835 u32 logsize;
836 int pos = 0;
837 size_t bufsz = 0;
6bb78847 838 struct iwl_priv *priv = priv(trans);
7ff94706 839
ae6130fc 840 base = trans->shrd->device_pointers.log_event_table;
3d6acefc 841 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
7ff94706
EG
842 logsize = priv->init_evtlog_size;
843 if (!base)
844 base = priv->init_evtlog_ptr;
845 } else {
846 logsize = priv->inst_evtlog_size;
847 if (!base)
848 base = priv->inst_evtlog_ptr;
849 }
850
851 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
6bb78847 852 IWL_ERR(trans,
7ff94706
EG
853 "Invalid event log pointer 0x%08X for %s uCode\n",
854 base,
3d6acefc 855 (trans->shrd->ucode_type == IWL_UCODE_INIT)
7ff94706
EG
856 ? "Init" : "RT");
857 return -EINVAL;
858 }
859
860 /* event log header */
1042db2a
EG
861 capacity = iwl_read_targ_mem(trans, base);
862 mode = iwl_read_targ_mem(trans, base + (1 * sizeof(u32)));
863 num_wraps = iwl_read_targ_mem(trans, base + (2 * sizeof(u32)));
864 next_entry = iwl_read_targ_mem(trans, base + (3 * sizeof(u32)));
7ff94706
EG
865
866 if (capacity > logsize) {
6bb78847
EG
867 IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
868 "entries\n", capacity, logsize);
7ff94706
EG
869 capacity = logsize;
870 }
871
872 if (next_entry > logsize) {
6bb78847 873 IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
7ff94706
EG
874 next_entry, logsize);
875 next_entry = logsize;
876 }
877
878 size = num_wraps ? capacity : next_entry;
879
880 /* bail out if nothing in log */
881 if (size == 0) {
6bb78847 882 IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
7ff94706
EG
883 return pos;
884 }
885
7ff94706 886#ifdef CONFIG_IWLWIFI_DEBUG
6bb78847 887 if (!(iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) && !full_log)
7ff94706
EG
888 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
889 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
890#else
891 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
892 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
893#endif
6bb78847 894 IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
7ff94706
EG
895 size);
896
897#ifdef CONFIG_IWLWIFI_DEBUG
898 if (display) {
899 if (full_log)
900 bufsz = capacity * 48;
901 else
902 bufsz = size * 48;
903 *buf = kmalloc(bufsz, GFP_KERNEL);
904 if (!*buf)
905 return -ENOMEM;
906 }
6bb78847 907 if ((iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) || full_log) {
7ff94706
EG
908 /*
909 * if uCode has wrapped back to top of log,
910 * start at the oldest entry,
911 * i.e the next one that uCode would fill.
912 */
913 if (num_wraps)
6bb78847 914 pos = iwl_print_event_log(trans, next_entry,
7ff94706
EG
915 capacity - next_entry, mode,
916 pos, buf, bufsz);
917 /* (then/else) start at top of log */
6bb78847 918 pos = iwl_print_event_log(trans, 0,
7ff94706
EG
919 next_entry, mode, pos, buf, bufsz);
920 } else
6bb78847 921 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
7ff94706
EG
922 next_entry, size, mode,
923 pos, buf, bufsz);
924#else
6bb78847 925 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
7ff94706
EG
926 next_entry, size, mode,
927 pos, buf, bufsz);
928#endif
929 return pos;
930}
931
ab697a9f 932/* tasklet for iwlagn interrupt */
0c325769 933void iwl_irq_tasklet(struct iwl_trans *trans)
ab697a9f
EG
934{
935 u32 inta = 0;
936 u32 handled = 0;
937 unsigned long flags;
938 u32 i;
939#ifdef CONFIG_IWLWIFI_DEBUG
940 u32 inta_mask;
941#endif
942
3e10caeb 943 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1f7b6172
EG
944 struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
945
0c325769 946
7b11488f 947 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
ab697a9f
EG
948
949 /* Ack/clear/reset pending uCode interrupts.
950 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
951 */
952 /* There is a hardware bug in the interrupt mask function that some
953 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
954 * they are disabled in the CSR_INT_MASK register. Furthermore the
955 * ICT interrupt handling mechanism has another bug that might cause
956 * these unmasked interrupts fail to be detected. We workaround the
957 * hardware bugs here by ACKing all the possible interrupts so that
958 * interrupt coalescing can still be achieved.
959 */
1042db2a 960 iwl_write32(trans, CSR_INT,
0c325769 961 trans_pcie->inta | ~trans_pcie->inta_mask);
ab697a9f 962
0c325769 963 inta = trans_pcie->inta;
ab697a9f
EG
964
965#ifdef CONFIG_IWLWIFI_DEBUG
0c325769 966 if (iwl_get_debug_level(trans->shrd) & IWL_DL_ISR) {
ab697a9f 967 /* just for debug */
1042db2a 968 inta_mask = iwl_read32(trans, CSR_INT_MASK);
0c325769 969 IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
ab697a9f
EG
970 inta, inta_mask);
971 }
972#endif
973
0c325769
EG
974 /* saved interrupt in inta variable now we can reset trans_pcie->inta */
975 trans_pcie->inta = 0;
ab697a9f 976
7b11488f 977 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
b49ba04a 978
ab697a9f
EG
979 /* Now service all interrupt bits discovered above. */
980 if (inta & CSR_INT_BIT_HW_ERR) {
0c325769 981 IWL_ERR(trans, "Hardware error detected. Restarting.\n");
ab697a9f
EG
982
983 /* Tell the device to stop sending interrupts */
0c325769 984 iwl_disable_interrupts(trans);
ab697a9f 985
1f7b6172 986 isr_stats->hw++;
6bb78847 987 iwl_irq_handle_error(trans);
ab697a9f
EG
988
989 handled |= CSR_INT_BIT_HW_ERR;
990
991 return;
992 }
993
994#ifdef CONFIG_IWLWIFI_DEBUG
0c325769 995 if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
ab697a9f
EG
996 /* NIC fires this, but we don't use it, redundant with WAKEUP */
997 if (inta & CSR_INT_BIT_SCD) {
0c325769 998 IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
ab697a9f 999 "the frame/frames.\n");
1f7b6172 1000 isr_stats->sch++;
ab697a9f
EG
1001 }
1002
1003 /* Alive notification via Rx interrupt will do the real work */
1004 if (inta & CSR_INT_BIT_ALIVE) {
0c325769 1005 IWL_DEBUG_ISR(trans, "Alive interrupt\n");
1f7b6172 1006 isr_stats->alive++;
ab697a9f
EG
1007 }
1008 }
1009#endif
1010 /* Safely ignore these bits for debug checks below */
1011 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1012
1013 /* HW RF KILL switch toggled */
1014 if (inta & CSR_INT_BIT_RF_KILL) {
1015 int hw_rf_kill = 0;
1042db2a 1016 if (!(iwl_read32(trans, CSR_GP_CNTRL) &
ab697a9f
EG
1017 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1018 hw_rf_kill = 1;
1019
0c325769 1020 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
ab697a9f
EG
1021 hw_rf_kill ? "disable radio" : "enable radio");
1022
1f7b6172 1023 isr_stats->rfkill++;
ab697a9f
EG
1024
1025 /* driver only loads ucode once setting the interface up.
1026 * the driver allows loading the ucode even if the radio
1027 * is killed. Hence update the killswitch state here. The
1028 * rfkill handler will care about restarting if needed.
1029 */
0c325769 1030 if (!test_bit(STATUS_ALIVE, &trans->shrd->status)) {
ab697a9f 1031 if (hw_rf_kill)
0c325769
EG
1032 set_bit(STATUS_RF_KILL_HW,
1033 &trans->shrd->status);
ab697a9f 1034 else
63013ae3 1035 clear_bit(STATUS_RF_KILL_HW,
0c325769 1036 &trans->shrd->status);
3e10caeb 1037 iwl_set_hw_rfkill_state(priv(trans), hw_rf_kill);
ab697a9f
EG
1038 }
1039
1040 handled |= CSR_INT_BIT_RF_KILL;
1041 }
1042
1043 /* Chip got too hot and stopped itself */
1044 if (inta & CSR_INT_BIT_CT_KILL) {
0c325769 1045 IWL_ERR(trans, "Microcode CT kill error detected.\n");
1f7b6172 1046 isr_stats->ctkill++;
ab697a9f
EG
1047 handled |= CSR_INT_BIT_CT_KILL;
1048 }
1049
1050 /* Error detected by uCode */
1051 if (inta & CSR_INT_BIT_SW_ERR) {
0c325769 1052 IWL_ERR(trans, "Microcode SW error detected. "
ab697a9f 1053 " Restarting 0x%X.\n", inta);
1f7b6172 1054 isr_stats->sw++;
6bb78847 1055 iwl_irq_handle_error(trans);
ab697a9f
EG
1056 handled |= CSR_INT_BIT_SW_ERR;
1057 }
1058
1059 /* uCode wakes up after power-down sleep */
1060 if (inta & CSR_INT_BIT_WAKEUP) {
0c325769
EG
1061 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1062 iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
1063 for (i = 0; i < hw_params(trans).max_txq_num; i++)
fd656935 1064 iwl_txq_update_write_ptr(trans,
8ad71bef 1065 &trans_pcie->txq[i]);
ab697a9f 1066
1f7b6172 1067 isr_stats->wakeup++;
ab697a9f
EG
1068
1069 handled |= CSR_INT_BIT_WAKEUP;
1070 }
1071
1072 /* All uCode command responses, including Tx command responses,
1073 * Rx "responses" (frame-received notification), and other
1074 * notifications from uCode come through here*/
1075 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1076 CSR_INT_BIT_RX_PERIODIC)) {
0c325769 1077 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
ab697a9f
EG
1078 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1079 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1042db2a 1080 iwl_write32(trans, CSR_FH_INT_STATUS,
ab697a9f
EG
1081 CSR_FH_INT_RX_MASK);
1082 }
1083 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1084 handled |= CSR_INT_BIT_RX_PERIODIC;
1042db2a 1085 iwl_write32(trans,
0c325769 1086 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
ab697a9f
EG
1087 }
1088 /* Sending RX interrupt require many steps to be done in the
1089 * the device:
1090 * 1- write interrupt to current index in ICT table.
1091 * 2- dma RX frame.
1092 * 3- update RX shared data to indicate last write index.
1093 * 4- send interrupt.
1094 * This could lead to RX race, driver could receive RX interrupt
1095 * but the shared data changes does not reflect this;
1096 * periodic interrupt will detect any dangling Rx activity.
1097 */
1098
1099 /* Disable periodic interrupt; we use it as just a one-shot. */
1042db2a 1100 iwl_write8(trans, CSR_INT_PERIODIC_REG,
ab697a9f 1101 CSR_INT_PERIODIC_DIS);
a5916977
GG
1102#ifdef CONFIG_IWLWIFI_IDI
1103 iwl_amfh_rx_handler();
1104#else
0c325769 1105 iwl_rx_handle(trans);
a5916977 1106#endif
ab697a9f
EG
1107 /*
1108 * Enable periodic interrupt in 8 msec only if we received
1109 * real RX interrupt (instead of just periodic int), to catch
1110 * any dangling Rx interrupt. If it was just the periodic
1111 * interrupt, there was no dangling Rx activity, and no need
1112 * to extend the periodic interrupt; one-shot is enough.
1113 */
1114 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
1042db2a 1115 iwl_write8(trans, CSR_INT_PERIODIC_REG,
ab697a9f
EG
1116 CSR_INT_PERIODIC_ENA);
1117
1f7b6172 1118 isr_stats->rx++;
ab697a9f
EG
1119 }
1120
1121 /* This "Tx" DMA channel is used only for loading uCode */
1122 if (inta & CSR_INT_BIT_FH_TX) {
1042db2a 1123 iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
0c325769 1124 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
1f7b6172 1125 isr_stats->tx++;
ab697a9f
EG
1126 handled |= CSR_INT_BIT_FH_TX;
1127 /* Wake up uCode load routine, now that load is complete */
5703ddb0 1128 trans->ucode_write_complete = 1;
effd4d9a 1129 wake_up(&trans->shrd->wait_command_queue);
ab697a9f
EG
1130 }
1131
1132 if (inta & ~handled) {
0c325769 1133 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1f7b6172 1134 isr_stats->unhandled++;
ab697a9f
EG
1135 }
1136
0c325769
EG
1137 if (inta & ~(trans_pcie->inta_mask)) {
1138 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1139 inta & ~trans_pcie->inta_mask);
ab697a9f
EG
1140 }
1141
1142 /* Re-enable all interrupts */
1143 /* only Re-enable if disabled by irq */
0c325769
EG
1144 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status))
1145 iwl_enable_interrupts(trans);
ab697a9f 1146 /* Re-enable RF_KILL if it occurred */
1df06bdc
EG
1147 else if (handled & CSR_INT_BIT_RF_KILL) {
1148 IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
1149 iwl_write32(trans, CSR_INT_MASK, CSR_INT_BIT_RF_KILL);
1150 }
ab697a9f
EG
1151}
1152
1a361cd8
EG
1153/******************************************************************************
1154 *
1155 * ICT functions
1156 *
1157 ******************************************************************************/
10667136
JB
1158
1159/* a device (PCI-E) page is 4096 bytes long */
1160#define ICT_SHIFT 12
1161#define ICT_SIZE (1 << ICT_SHIFT)
1162#define ICT_COUNT (ICT_SIZE / sizeof(u32))
1a361cd8
EG
1163
1164/* Free dram table */
0c325769 1165void iwl_free_isr_ict(struct iwl_trans *trans)
1a361cd8 1166{
0c325769
EG
1167 struct iwl_trans_pcie *trans_pcie =
1168 IWL_TRANS_GET_PCIE_TRANS(trans);
1169
10667136 1170 if (trans_pcie->ict_tbl) {
1042db2a 1171 dma_free_coherent(trans->dev, ICT_SIZE,
10667136 1172 trans_pcie->ict_tbl,
0c325769 1173 trans_pcie->ict_tbl_dma);
10667136
JB
1174 trans_pcie->ict_tbl = NULL;
1175 trans_pcie->ict_tbl_dma = 0;
1a361cd8
EG
1176 }
1177}
1178
1179
10667136
JB
1180/*
1181 * allocate dram shared table, it is an aligned memory
1182 * block of ICT_SIZE.
1a361cd8
EG
1183 * also reset all data related to ICT table interrupt.
1184 */
0c325769 1185int iwl_alloc_isr_ict(struct iwl_trans *trans)
1a361cd8 1186{
0c325769
EG
1187 struct iwl_trans_pcie *trans_pcie =
1188 IWL_TRANS_GET_PCIE_TRANS(trans);
1a361cd8 1189
10667136 1190 trans_pcie->ict_tbl =
1042db2a 1191 dma_alloc_coherent(trans->dev, ICT_SIZE,
10667136
JB
1192 &trans_pcie->ict_tbl_dma,
1193 GFP_KERNEL);
1194 if (!trans_pcie->ict_tbl)
1a361cd8
EG
1195 return -ENOMEM;
1196
10667136
JB
1197 /* just an API sanity check ... it is guaranteed to be aligned */
1198 if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
1199 iwl_free_isr_ict(trans);
1200 return -EINVAL;
1201 }
1a361cd8 1202
10667136
JB
1203 IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
1204 (unsigned long long)trans_pcie->ict_tbl_dma);
1a361cd8 1205
10667136 1206 IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
1a361cd8
EG
1207
1208 /* reset table and index to all 0 */
10667136 1209 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
0c325769 1210 trans_pcie->ict_index = 0;
1a361cd8
EG
1211
1212 /* add periodic RX interrupt */
0c325769 1213 trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
1a361cd8
EG
1214 return 0;
1215}
1216
1217/* Device is going up inform it about using ICT interrupt table,
1218 * also we need to tell the driver to start using ICT interrupt.
1219 */
ed6a3803 1220void iwl_reset_ict(struct iwl_trans *trans)
1a361cd8
EG
1221{
1222 u32 val;
1223 unsigned long flags;
0c325769
EG
1224 struct iwl_trans_pcie *trans_pcie =
1225 IWL_TRANS_GET_PCIE_TRANS(trans);
1a361cd8 1226
10667136 1227 if (!trans_pcie->ict_tbl)
ed6a3803 1228 return;
1a361cd8 1229
7b11488f 1230 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
0c325769 1231 iwl_disable_interrupts(trans);
1a361cd8 1232
10667136 1233 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
1a361cd8 1234
10667136 1235 val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
1a361cd8
EG
1236
1237 val |= CSR_DRAM_INT_TBL_ENABLE;
1238 val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
1239
10667136 1240 IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
1a361cd8 1241
1042db2a 1242 iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
0c325769
EG
1243 trans_pcie->use_ict = true;
1244 trans_pcie->ict_index = 0;
1042db2a 1245 iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
0c325769 1246 iwl_enable_interrupts(trans);
7b11488f 1247 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1248}
1249
1250/* Device is going down disable ict interrupt usage */
0c325769 1251void iwl_disable_ict(struct iwl_trans *trans)
1a361cd8 1252{
0c325769
EG
1253 struct iwl_trans_pcie *trans_pcie =
1254 IWL_TRANS_GET_PCIE_TRANS(trans);
1255
1a361cd8
EG
1256 unsigned long flags;
1257
7b11488f 1258 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
0c325769 1259 trans_pcie->use_ict = false;
7b11488f 1260 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1261}
1262
1263static irqreturn_t iwl_isr(int irq, void *data)
1264{
0c325769
EG
1265 struct iwl_trans *trans = data;
1266 struct iwl_trans_pcie *trans_pcie;
1a361cd8
EG
1267 u32 inta, inta_mask;
1268 unsigned long flags;
1269#ifdef CONFIG_IWLWIFI_DEBUG
1270 u32 inta_fh;
1271#endif
0c325769 1272 if (!trans)
1a361cd8
EG
1273 return IRQ_NONE;
1274
b80667ee
JB
1275 trace_iwlwifi_dev_irq(priv(trans));
1276
0c325769
EG
1277 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1278
7b11488f 1279 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1280
1281 /* Disable (but don't clear!) interrupts here to avoid
1282 * back-to-back ISRs and sporadic interrupts from our NIC.
1283 * If we have something to service, the tasklet will re-enable ints.
1284 * If we *don't* have something, we'll re-enable before leaving here. */
1042db2a
EG
1285 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1286 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
1a361cd8
EG
1287
1288 /* Discover which interrupts are active/pending */
1042db2a 1289 inta = iwl_read32(trans, CSR_INT);
1a361cd8
EG
1290
1291 /* Ignore interrupt if there's nothing in NIC to service.
1292 * This may be due to IRQ shared with another device,
1293 * or due to sporadic interrupts thrown from our NIC. */
1294 if (!inta) {
0c325769 1295 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
1a361cd8
EG
1296 goto none;
1297 }
1298
1299 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1300 /* Hardware disappeared. It might have already raised
1301 * an interrupt */
0c325769 1302 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1a361cd8
EG
1303 goto unplugged;
1304 }
1305
1306#ifdef CONFIG_IWLWIFI_DEBUG
0c325769 1307 if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
1042db2a 1308 inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
0c325769 1309 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
1a361cd8
EG
1310 "fh 0x%08x\n", inta, inta_mask, inta_fh);
1311 }
1312#endif
1313
0c325769 1314 trans_pcie->inta |= inta;
1a361cd8
EG
1315 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1316 if (likely(inta))
0c325769
EG
1317 tasklet_schedule(&trans_pcie->irq_tasklet);
1318 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1319 !trans_pcie->inta)
1320 iwl_enable_interrupts(trans);
1a361cd8
EG
1321
1322 unplugged:
7b11488f 1323 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1324 return IRQ_HANDLED;
1325
1326 none:
1327 /* re-enable interrupts here since we don't have anything to service. */
1328 /* only Re-enable if disabled by irq and no schedules tasklet. */
0c325769
EG
1329 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1330 !trans_pcie->inta)
1331 iwl_enable_interrupts(trans);
1a361cd8 1332
7b11488f 1333 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1334 return IRQ_NONE;
1335}
1336
1337/* interrupt handler using ict table, with this interrupt driver will
1338 * stop using INTA register to get device's interrupt, reading this register
1339 * is expensive, device will write interrupts in ICT dram table, increment
1340 * index then will fire interrupt to driver, driver will OR all ICT table
1341 * entries from current index up to table entry with 0 value. the result is
1342 * the interrupt we need to service, driver will set the entries back to 0 and
1343 * set index.
1344 */
1345irqreturn_t iwl_isr_ict(int irq, void *data)
1346{
0c325769
EG
1347 struct iwl_trans *trans = data;
1348 struct iwl_trans_pcie *trans_pcie;
1a361cd8
EG
1349 u32 inta, inta_mask;
1350 u32 val = 0;
b80667ee 1351 u32 read;
1a361cd8
EG
1352 unsigned long flags;
1353
0c325769 1354 if (!trans)
1a361cd8
EG
1355 return IRQ_NONE;
1356
0c325769
EG
1357 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1358
1a361cd8
EG
1359 /* dram interrupt table not set yet,
1360 * use legacy interrupt.
1361 */
0c325769 1362 if (!trans_pcie->use_ict)
1a361cd8
EG
1363 return iwl_isr(irq, data);
1364
b80667ee
JB
1365 trace_iwlwifi_dev_irq(priv(trans));
1366
7b11488f 1367 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1368
1369 /* Disable (but don't clear!) interrupts here to avoid
1370 * back-to-back ISRs and sporadic interrupts from our NIC.
1371 * If we have something to service, the tasklet will re-enable ints.
1372 * If we *don't* have something, we'll re-enable before leaving here.
1373 */
1042db2a
EG
1374 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1375 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
1a361cd8
EG
1376
1377
1378 /* Ignore interrupt if there's nothing in NIC to service.
1379 * This may be due to IRQ shared with another device,
1380 * or due to sporadic interrupts thrown from our NIC. */
b80667ee
JB
1381 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1382 trace_iwlwifi_dev_ict_read(priv(trans), trans_pcie->ict_index, read);
1383 if (!read) {
0c325769 1384 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
1a361cd8
EG
1385 goto none;
1386 }
1387
b80667ee
JB
1388 /*
1389 * Collect all entries up to the first 0, starting from ict_index;
1390 * note we already read at ict_index.
1391 */
1392 do {
1393 val |= read;
0c325769 1394 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
b80667ee 1395 trans_pcie->ict_index, read);
0c325769
EG
1396 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1397 trans_pcie->ict_index =
1398 iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
1a361cd8 1399
b80667ee
JB
1400 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1401 trace_iwlwifi_dev_ict_read(priv(trans), trans_pcie->ict_index,
1402 read);
1403 } while (read);
1a361cd8
EG
1404
1405 /* We should not get this value, just ignore it. */
1406 if (val == 0xffffffff)
1407 val = 0;
1408
1409 /*
1410 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1411 * (bit 15 before shifting it to 31) to clear when using interrupt
1412 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1413 * so we use them to decide on the real state of the Rx bit.
1414 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1415 */
1416 if (val & 0xC0000)
1417 val |= 0x8000;
1418
1419 inta = (0xff & val) | ((0xff00 & val) << 16);
0c325769 1420 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
1a361cd8
EG
1421 inta, inta_mask, val);
1422
0c325769
EG
1423 inta &= trans_pcie->inta_mask;
1424 trans_pcie->inta |= inta;
1a361cd8
EG
1425
1426 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1427 if (likely(inta))
0c325769
EG
1428 tasklet_schedule(&trans_pcie->irq_tasklet);
1429 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
b80667ee 1430 !trans_pcie->inta) {
1a361cd8
EG
1431 /* Allow interrupt if was disabled by this handler and
1432 * no tasklet was schedules, We should not enable interrupt,
1433 * tasklet will enable it.
1434 */
0c325769 1435 iwl_enable_interrupts(trans);
1a361cd8
EG
1436 }
1437
7b11488f 1438 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1439 return IRQ_HANDLED;
1440
1441 none:
1442 /* re-enable interrupts here since we don't have anything to service.
1443 * only Re-enable if disabled by irq.
1444 */
0c325769 1445 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
b80667ee 1446 !trans_pcie->inta)
0c325769 1447 iwl_enable_interrupts(trans);
1a361cd8 1448
7b11488f 1449 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
1a361cd8
EG
1450 return IRQ_NONE;
1451}