blob: 1b11cfec5da6fe253200547673c35c08f6518225 [file] [log] [blame]
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001/******************************************************************************
2 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -08003 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07004 *
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>
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -070031#include <linux/gfp.h>
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070032
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070033/*TODO: Remove include to iwl-core.h*/
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070034#include "iwl-core.h"
35#include "iwl-io.h"
Johannes Bergc17d0682011-09-15 11:46:42 -070036#include "iwl-trans-pcie-int.h"
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070037
Gregory Greenmana5916972012-01-10 19:22:56 +020038#ifdef CONFIG_IWLWIFI_IDI
39#include "iwl-amfh.h"
40#endif
41
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070042/******************************************************************************
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 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700132void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700133 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
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700143 if (hw_params(trans).shadow_reg_enable) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700144 /* shadow register enabled */
145 /* Device expects a multiple of 8 */
146 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200147 iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700148 } else {
149 /* If power-saving is in use, make sure device is awake */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700150 if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200151 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700152
153 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700154 IWL_DEBUG_INFO(trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700155 "Rx queue requesting wakeup,"
156 " GP1 = 0x%x\n", reg);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200157 iwl_set_bit(trans, CSR_GP_CNTRL,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700158 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
159 goto exit_unlock;
160 }
161
162 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200163 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700164 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);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200170 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700171 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 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700183static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700184{
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 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700199static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700200{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700201 struct iwl_trans_pcie *trans_pcie =
202 IWL_TRANS_GET_PCIE_TRANS(trans);
203
204 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700205 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 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700221 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700222 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)
Johannes Berg1ee158d2012-02-17 10:07:44 -0800230 schedule_work(&trans_pcie->rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700231
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);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700239 iwl_rx_queue_update_write_ptr(trans, rxq);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700240 }
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 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700251static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700252{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700253 struct iwl_trans_pcie *trans_pcie =
254 IWL_TRANS_GET_PCIE_TRANS(trans);
255
256 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700257 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
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700274 if (hw_params(trans).rx_page_order > 0)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700275 gfp_mask |= __GFP_COMP;
276
277 /* Alloc a new receive buffer */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700278 page = alloc_pages(gfp_mask,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700279 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700280 if (!page) {
281 if (net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700282 IWL_DEBUG_INFO(trans, "alloc_pages failed, "
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700283 "order: %d\n",
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700284 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700285
286 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
287 net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700288 IWL_CRIT(trans, "Failed to alloc_pages with %s."
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700289 "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);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700303 __free_pages(page, hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700304 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 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200315 rxb->page_dma = dma_map_page(trans->dev, page, 0,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700316 PAGE_SIZE << hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700317 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
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700332void iwlagn_rx_replenish(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700333{
334 unsigned long flags;
335
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700336 iwlagn_rx_allocate(trans, GFP_KERNEL);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700337
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700338 spin_lock_irqsave(&trans->shrd->lock, flags);
339 iwlagn_rx_queue_restock(trans);
340 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700341}
342
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700343static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700344{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700345 iwlagn_rx_allocate(trans, GFP_ATOMIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700346
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700347 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700348}
349
350void iwl_bg_rx_replenish(struct work_struct *data)
351{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700352 struct iwl_trans_pcie *trans_pcie =
353 container_of(data, struct iwl_trans_pcie, rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700354
Johannes Berg1ee158d2012-02-17 10:07:44 -0800355 iwlagn_rx_replenish(trans_pcie->trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700356}
357
358/**
359 * iwl_rx_handle - Main entry function for receiving responses from uCode
360 *
361 * Uses the priv->rx_handlers callback function array to invoke
362 * the appropriate handlers, including command responses,
363 * frame-received notifications, and other notifications.
364 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700365static void iwl_rx_handle(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700366{
367 struct iwl_rx_mem_buffer *rxb;
368 struct iwl_rx_packet *pkt;
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700369 struct iwl_trans_pcie *trans_pcie =
370 IWL_TRANS_GET_PCIE_TRANS(trans);
371 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700372 struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
373 struct iwl_device_cmd *cmd;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700374 u32 r, i;
375 int reclaim;
376 unsigned long flags;
377 u8 fill_rx = 0;
378 u32 count = 8;
379 int total_empty;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700380 int index, cmd_index;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700381
382 /* uCode's read index (stored in shared DRAM) indicates the last Rx
383 * buffer that the driver may process (last buffer filled by ucode). */
384 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
385 i = rxq->read;
386
387 /* Rx interrupt, but nothing sent from uCode */
388 if (i == r)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700389 IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700390
391 /* calculate total frames need to be restock after handling RX */
392 total_empty = r - rxq->write_actual;
393 if (total_empty < 0)
394 total_empty += RX_QUEUE_SIZE;
395
396 if (total_empty > (RX_QUEUE_SIZE / 2))
397 fill_rx = 1;
398
399 while (i != r) {
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700400 int len, err;
Emmanuel Grumbachd56da922011-09-22 07:15:36 -0700401 u16 sequence;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700402
403 rxb = rxq->queue[i];
404
405 /* If an RXB doesn't have a Rx queue slot associated with it,
406 * then a bug has been introduced in the queue refilling
407 * routines -- catch it here */
408 if (WARN_ON(rxb == NULL)) {
409 i = (i + 1) & RX_QUEUE_MASK;
410 continue;
411 }
412
413 rxq->queue[i] = NULL;
414
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200415 dma_unmap_page(trans->dev, rxb->page_dma,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700416 PAGE_SIZE << hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700417 DMA_FROM_DEVICE);
418 pkt = rxb_addr(rxb);
419
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700420 IWL_DEBUG_RX(trans, "r = %d, i = %d, %s, 0x%02x\n", r,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700421 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
422
423 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
424 len += sizeof(u32); /* account for status word */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700425 trace_iwlwifi_dev_rx(priv(trans), pkt, len);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700426
427 /* Reclaim a command buffer only if this packet is a response
428 * to a (driver-originated) command.
429 * If the packet (e.g. Rx frame) originated from uCode,
430 * there is no command buffer to reclaim.
431 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
432 * but apparently a few don't get set; catch them here. */
433 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
434 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
435 (pkt->hdr.cmd != REPLY_RX) &&
436 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
437 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
438 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
439 (pkt->hdr.cmd != REPLY_TX);
440
Emmanuel Grumbach17a68dd2011-09-15 11:46:28 -0700441 sequence = le16_to_cpu(pkt->hdr.sequence);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700442 index = SEQ_TO_INDEX(sequence);
443 cmd_index = get_cmd_index(&txq->q, index);
444
445 if (reclaim)
446 cmd = txq->cmd[cmd_index];
447 else
448 cmd = NULL;
Emmanuel Grumbach17a68dd2011-09-15 11:46:28 -0700449
450 /* warn if this is cmd response / notification and the uCode
451 * didn't set the SEQ_RX_FRAME for a frame that is
Emmanuel Grumbachd56da922011-09-22 07:15:36 -0700452 * uCode-originated
453 * If you saw this code after the second half of 2012, then
454 * please remove it
455 */
456 WARN(pkt->hdr.cmd != REPLY_TX && reclaim == false &&
Emmanuel Grumbach17a68dd2011-09-15 11:46:28 -0700457 (!(pkt->hdr.sequence & SEQ_RX_FRAME)),
458 "reclaim is false, SEQ_RX_FRAME unset: %s\n",
459 get_cmd_string(pkt->hdr.cmd));
460
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700461 err = iwl_rx_dispatch(priv(trans), rxb, cmd);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700462
463 /*
464 * XXX: After here, we should always check rxb->page
465 * against NULL before touching it or its virtual
466 * memory (pkt). Because some rx_handler might have
467 * already taken or freed the pages.
468 */
469
470 if (reclaim) {
471 /* Invoke any callbacks, transfer the buffer to caller,
472 * and fire off the (possibly) blocking
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700473 * iwl_trans_send_cmd()
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700474 * as we reclaim the driver command queue */
475 if (rxb->page)
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700476 iwl_tx_cmd_complete(trans, rxb, err);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700477 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700478 IWL_WARN(trans, "Claim null rxb?\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700479 }
480
481 /* Reuse the page if possible. For notification packets and
482 * SKBs that fail to Rx correctly, add them back into the
483 * rx_free list for reuse later. */
484 spin_lock_irqsave(&rxq->lock, flags);
485 if (rxb->page != NULL) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200486 rxb->page_dma = dma_map_page(trans->dev, rxb->page,
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700487 0, PAGE_SIZE <<
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700488 hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700489 DMA_FROM_DEVICE);
490 list_add_tail(&rxb->list, &rxq->rx_free);
491 rxq->free_count++;
492 } else
493 list_add_tail(&rxb->list, &rxq->rx_used);
494
495 spin_unlock_irqrestore(&rxq->lock, flags);
496
497 i = (i + 1) & RX_QUEUE_MASK;
498 /* If there are a lot of unused frames,
499 * restock the Rx queue so ucode wont assert. */
500 if (fill_rx) {
501 count++;
502 if (count >= 8) {
503 rxq->read = i;
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700504 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700505 count = 0;
506 }
507 }
508 }
509
510 /* Backtrack one entry */
511 rxq->read = i;
512 if (fill_rx)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700513 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700514 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700515 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700516}
517
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700518static const char * const desc_lookup_text[] = {
519 "OK",
520 "FAIL",
521 "BAD_PARAM",
522 "BAD_CHECKSUM",
523 "NMI_INTERRUPT_WDG",
524 "SYSASSERT",
525 "FATAL_ERROR",
526 "BAD_COMMAND",
527 "HW_ERROR_TUNE_LOCK",
528 "HW_ERROR_TEMPERATURE",
529 "ILLEGAL_CHAN_FREQ",
530 "VCC_NOT_STABLE",
531 "FH_ERROR",
532 "NMI_INTERRUPT_HOST",
533 "NMI_INTERRUPT_ACTION_PT",
534 "NMI_INTERRUPT_UNKNOWN",
535 "UCODE_VERSION_MISMATCH",
536 "HW_ERROR_ABS_LOCK",
537 "HW_ERROR_CAL_LOCK_FAIL",
538 "NMI_INTERRUPT_INST_ACTION_PT",
539 "NMI_INTERRUPT_DATA_ACTION_PT",
540 "NMI_TRM_HW_ER",
541 "NMI_INTERRUPT_TRM",
542 "NMI_INTERRUPT_BREAK_POINT",
543 "DEBUG_0",
544 "DEBUG_1",
545 "DEBUG_2",
546 "DEBUG_3",
547};
548
549static struct { char *name; u8 num; } advanced_lookup[] = {
550 { "NMI_INTERRUPT_WDG", 0x34 },
551 { "SYSASSERT", 0x35 },
552 { "UCODE_VERSION_MISMATCH", 0x37 },
553 { "BAD_COMMAND", 0x38 },
554 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
555 { "FATAL_ERROR", 0x3D },
556 { "NMI_TRM_HW_ERR", 0x46 },
557 { "NMI_INTERRUPT_TRM", 0x4C },
558 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
559 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
560 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
561 { "NMI_INTERRUPT_HOST", 0x66 },
562 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
563 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
564 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
565 { "ADVANCED_SYSASSERT", 0 },
566};
567
568static const char *desc_lookup(u32 num)
569{
570 int i;
571 int max = ARRAY_SIZE(desc_lookup_text);
572
573 if (num < max)
574 return desc_lookup_text[num];
575
576 max = ARRAY_SIZE(advanced_lookup) - 1;
577 for (i = 0; i < max; i++) {
578 if (advanced_lookup[i].num == num)
579 break;
580 }
581 return advanced_lookup[i].name;
582}
583
584#define ERROR_START_OFFSET (1 * sizeof(u32))
585#define ERROR_ELEM_SIZE (7 * sizeof(u32))
586
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700587static void iwl_dump_nic_error_log(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700588{
589 u32 base;
590 struct iwl_error_event_table table;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700591 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700592 struct iwl_trans_pcie *trans_pcie =
593 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700594
Don Fryae6130f2011-11-30 16:12:59 -0800595 base = trans->shrd->device_pointers.error_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800596 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700597 if (!base)
598 base = priv->init_errlog_ptr;
599 } else {
600 if (!base)
601 base = priv->inst_errlog_ptr;
602 }
603
604 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700605 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700606 "Not valid error log pointer 0x%08X for %s uCode\n",
607 base,
Don Fry3d6acef2011-11-28 17:05:01 -0800608 (trans->shrd->ucode_type == IWL_UCODE_INIT)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700609 ? "Init" : "RT");
610 return;
611 }
612
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200613 iwl_read_targ_mem_words(trans(priv), base, &table, sizeof(table));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700614
615 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700616 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
617 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
618 trans->shrd->status, table.valid);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700619 }
620
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700621 trans_pcie->isr_stats.err_code = table.error_id;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700622
623 trace_iwlwifi_dev_ucode_error(priv, table.error_id, table.tsf_low,
624 table.data1, table.data2, table.line,
625 table.blink1, table.blink2, table.ilink1,
626 table.ilink2, table.bcon_time, table.gp1,
627 table.gp2, table.gp3, table.ucode_ver,
628 table.hw_ver, table.brd_ver);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700629 IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700630 desc_lookup(table.error_id));
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700631 IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
632 IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
633 IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
634 IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
635 IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
636 IWL_ERR(trans, "0x%08X | data1\n", table.data1);
637 IWL_ERR(trans, "0x%08X | data2\n", table.data2);
638 IWL_ERR(trans, "0x%08X | line\n", table.line);
639 IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
640 IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
641 IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
642 IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
643 IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
644 IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
645 IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
646 IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
647 IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
648 IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
Wey-Yi Guyd332f592011-11-30 12:32:42 -0800649
650 IWL_ERR(trans, "0x%08X | isr0\n", table.isr0);
651 IWL_ERR(trans, "0x%08X | isr1\n", table.isr1);
652 IWL_ERR(trans, "0x%08X | isr2\n", table.isr2);
653 IWL_ERR(trans, "0x%08X | isr3\n", table.isr3);
654 IWL_ERR(trans, "0x%08X | isr4\n", table.isr4);
655 IWL_ERR(trans, "0x%08X | isr_pref\n", table.isr_pref);
656 IWL_ERR(trans, "0x%08X | wait_event\n", table.wait_event);
657 IWL_ERR(trans, "0x%08X | l2p_control\n", table.l2p_control);
658 IWL_ERR(trans, "0x%08X | l2p_duration\n", table.l2p_duration);
659 IWL_ERR(trans, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
660 IWL_ERR(trans, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
661 IWL_ERR(trans, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
662 IWL_ERR(trans, "0x%08X | timestamp\n", table.u_timestamp);
663 IWL_ERR(trans, "0x%08X | flow_handler\n", table.flow_handler);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700664}
665
666/**
667 * iwl_irq_handle_error - called for HW or SW error interrupt from card
668 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700669static void iwl_irq_handle_error(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700670{
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700671 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700672 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
Don Fry38622412011-12-16 07:07:36 -0800673 if (cfg(priv)->internal_wimax_coex &&
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200674 (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700675 APMS_CLK_VAL_MRB_FUNC_MODE) ||
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200676 (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700677 APMG_PS_CTRL_VAL_RESET_REQ))) {
678 /*
679 * Keep the restart process from trying to send host
680 * commands by clearing the ready bit.
681 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700682 clear_bit(STATUS_READY, &trans->shrd->status);
683 clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
Johannes Bergeffd4d92011-09-15 11:46:52 -0700684 wake_up(&priv->shrd->wait_command_queue);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700685 IWL_ERR(trans, "RF is used by WiMAX\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700686 return;
687 }
688
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700689 IWL_ERR(trans, "Loaded firmware version: %s\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700690 priv->hw->wiphy->fw_version);
691
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700692 iwl_dump_nic_error_log(trans);
693 iwl_dump_csr(trans);
694 iwl_dump_fh(trans, NULL, false);
695 iwl_dump_nic_event_log(trans, false, NULL, false);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700696#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700697 if (iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS)
Emmanuel Grumbach522376d2011-09-06 09:31:19 -0700698 iwl_print_rx_config_cmd(priv(trans), IWL_RXON_CTX_BSS);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700699#endif
700
701 iwlagn_fw_error(priv, false);
702}
703
704#define EVENT_START_OFFSET (4 * sizeof(u32))
705
706/**
707 * iwl_print_event_log - Dump error event log to syslog
708 *
709 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700710static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700711 u32 num_events, u32 mode,
712 int pos, char **buf, size_t bufsz)
713{
714 u32 i;
715 u32 base; /* SRAM byte address of event log header */
716 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
717 u32 ptr; /* SRAM byte address of log data */
718 u32 ev, time, data; /* event log data */
719 unsigned long reg_flags;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700720 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700721
722 if (num_events == 0)
723 return pos;
724
Don Fryae6130f2011-11-30 16:12:59 -0800725 base = trans->shrd->device_pointers.log_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800726 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700727 if (!base)
728 base = priv->init_evtlog_ptr;
729 } else {
730 if (!base)
731 base = priv->inst_evtlog_ptr;
732 }
733
734 if (mode == 0)
735 event_size = 2 * sizeof(u32);
736 else
737 event_size = 3 * sizeof(u32);
738
739 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
740
741 /* Make sure device is powered up for SRAM reads */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200742 spin_lock_irqsave(&trans->reg_lock, reg_flags);
743 iwl_grab_nic_access(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700744
745 /* Set starting address; reads will auto-increment */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200746 iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700747 rmb();
748
749 /* "time" is actually "data" for mode 0 (no timestamp).
750 * place event id # at far right for easier visual parsing. */
751 for (i = 0; i < num_events; i++) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200752 ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
753 time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700754 if (mode == 0) {
755 /* data, ev */
756 if (bufsz) {
757 pos += scnprintf(*buf + pos, bufsz - pos,
758 "EVT_LOG:0x%08x:%04u\n",
759 time, ev);
760 } else {
761 trace_iwlwifi_dev_ucode_event(priv, 0,
762 time, ev);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700763 IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700764 time, ev);
765 }
766 } else {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200767 data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700768 if (bufsz) {
769 pos += scnprintf(*buf + pos, bufsz - pos,
770 "EVT_LOGT:%010u:0x%08x:%04u\n",
771 time, data, ev);
772 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700773 IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700774 time, data, ev);
775 trace_iwlwifi_dev_ucode_event(priv, time,
776 data, ev);
777 }
778 }
779 }
780
781 /* Allow device to power down */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200782 iwl_release_nic_access(trans);
783 spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700784 return pos;
785}
786
787/**
788 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
789 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700790static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700791 u32 num_wraps, u32 next_entry,
792 u32 size, u32 mode,
793 int pos, char **buf, size_t bufsz)
794{
795 /*
796 * display the newest DEFAULT_LOG_ENTRIES entries
797 * i.e the entries just before the next ont that uCode would fill.
798 */
799 if (num_wraps) {
800 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700801 pos = iwl_print_event_log(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700802 capacity - (size - next_entry),
803 size - next_entry, mode,
804 pos, buf, bufsz);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700805 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700806 next_entry, mode,
807 pos, buf, bufsz);
808 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700809 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700810 size, mode, pos, buf, bufsz);
811 } else {
812 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700813 pos = iwl_print_event_log(trans, 0, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700814 mode, pos, buf, bufsz);
815 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700816 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700817 size, mode, pos, buf, bufsz);
818 }
819 }
820 return pos;
821}
822
823#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
824
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700825int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700826 char **buf, bool display)
827{
828 u32 base; /* SRAM byte address of event log header */
829 u32 capacity; /* event log capacity in # entries */
830 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
831 u32 num_wraps; /* # times uCode wrapped to top of log */
832 u32 next_entry; /* index of next entry to be written by uCode */
833 u32 size; /* # entries that we'll print */
834 u32 logsize;
835 int pos = 0;
836 size_t bufsz = 0;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700837 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700838
Don Fryae6130f2011-11-30 16:12:59 -0800839 base = trans->shrd->device_pointers.log_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800840 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700841 logsize = priv->init_evtlog_size;
842 if (!base)
843 base = priv->init_evtlog_ptr;
844 } else {
845 logsize = priv->inst_evtlog_size;
846 if (!base)
847 base = priv->inst_evtlog_ptr;
848 }
849
850 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700851 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700852 "Invalid event log pointer 0x%08X for %s uCode\n",
853 base,
Don Fry3d6acef2011-11-28 17:05:01 -0800854 (trans->shrd->ucode_type == IWL_UCODE_INIT)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700855 ? "Init" : "RT");
856 return -EINVAL;
857 }
858
859 /* event log header */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200860 capacity = iwl_read_targ_mem(trans, base);
861 mode = iwl_read_targ_mem(trans, base + (1 * sizeof(u32)));
862 num_wraps = iwl_read_targ_mem(trans, base + (2 * sizeof(u32)));
863 next_entry = iwl_read_targ_mem(trans, base + (3 * sizeof(u32)));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700864
865 if (capacity > logsize) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700866 IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
867 "entries\n", capacity, logsize);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700868 capacity = logsize;
869 }
870
871 if (next_entry > logsize) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700872 IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700873 next_entry, logsize);
874 next_entry = logsize;
875 }
876
877 size = num_wraps ? capacity : next_entry;
878
879 /* bail out if nothing in log */
880 if (size == 0) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700881 IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700882 return pos;
883 }
884
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700885#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700886 if (!(iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) && !full_log)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700887 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
888 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
889#else
890 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
891 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
892#endif
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700893 IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700894 size);
895
896#ifdef CONFIG_IWLWIFI_DEBUG
897 if (display) {
898 if (full_log)
899 bufsz = capacity * 48;
900 else
901 bufsz = size * 48;
902 *buf = kmalloc(bufsz, GFP_KERNEL);
903 if (!*buf)
904 return -ENOMEM;
905 }
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700906 if ((iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) || full_log) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700907 /*
908 * if uCode has wrapped back to top of log,
909 * start at the oldest entry,
910 * i.e the next one that uCode would fill.
911 */
912 if (num_wraps)
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700913 pos = iwl_print_event_log(trans, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700914 capacity - next_entry, mode,
915 pos, buf, bufsz);
916 /* (then/else) start at top of log */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700917 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700918 next_entry, mode, pos, buf, bufsz);
919 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700920 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700921 next_entry, size, mode,
922 pos, buf, bufsz);
923#else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700924 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700925 next_entry, size, mode,
926 pos, buf, bufsz);
927#endif
928 return pos;
929}
930
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700931/* tasklet for iwlagn interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700932void iwl_irq_tasklet(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700933{
934 u32 inta = 0;
935 u32 handled = 0;
936 unsigned long flags;
937 u32 i;
938#ifdef CONFIG_IWLWIFI_DEBUG
939 u32 inta_mask;
940#endif
941
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700942 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700943 struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
944
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700945
946 spin_lock_irqsave(&trans->shrd->lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700947
948 /* Ack/clear/reset pending uCode interrupts.
949 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
950 */
951 /* There is a hardware bug in the interrupt mask function that some
952 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
953 * they are disabled in the CSR_INT_MASK register. Furthermore the
954 * ICT interrupt handling mechanism has another bug that might cause
955 * these unmasked interrupts fail to be detected. We workaround the
956 * hardware bugs here by ACKing all the possible interrupts so that
957 * interrupt coalescing can still be achieved.
958 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200959 iwl_write32(trans, CSR_INT,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700960 trans_pcie->inta | ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700961
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700962 inta = trans_pcie->inta;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700963
964#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700965 if (iwl_get_debug_level(trans->shrd) & IWL_DL_ISR) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700966 /* just for debug */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200967 inta_mask = iwl_read32(trans, CSR_INT_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700968 IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700969 inta, inta_mask);
970 }
971#endif
972
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700973 /* saved interrupt in inta variable now we can reset trans_pcie->inta */
974 trans_pcie->inta = 0;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700975
Johannes Bergb49ba042012-01-19 08:20:57 -0800976 spin_unlock_irqrestore(&trans->shrd->lock, flags);
977
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700978 /* Now service all interrupt bits discovered above. */
979 if (inta & CSR_INT_BIT_HW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700980 IWL_ERR(trans, "Hardware error detected. Restarting.\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700981
982 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700983 iwl_disable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700984
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700985 isr_stats->hw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700986 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700987
988 handled |= CSR_INT_BIT_HW_ERR;
989
990 return;
991 }
992
993#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700994 if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700995 /* NIC fires this, but we don't use it, redundant with WAKEUP */
996 if (inta & CSR_INT_BIT_SCD) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700997 IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700998 "the frame/frames.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700999 isr_stats->sch++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001000 }
1001
1002 /* Alive notification via Rx interrupt will do the real work */
1003 if (inta & CSR_INT_BIT_ALIVE) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001004 IWL_DEBUG_ISR(trans, "Alive interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001005 isr_stats->alive++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001006 }
1007 }
1008#endif
1009 /* Safely ignore these bits for debug checks below */
1010 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1011
1012 /* HW RF KILL switch toggled */
1013 if (inta & CSR_INT_BIT_RF_KILL) {
1014 int hw_rf_kill = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001015 if (!(iwl_read32(trans, CSR_GP_CNTRL) &
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001016 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1017 hw_rf_kill = 1;
1018
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001019 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001020 hw_rf_kill ? "disable radio" : "enable radio");
1021
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001022 isr_stats->rfkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001023
1024 /* driver only loads ucode once setting the interface up.
1025 * the driver allows loading the ucode even if the radio
1026 * is killed. Hence update the killswitch state here. The
1027 * rfkill handler will care about restarting if needed.
1028 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001029 if (!test_bit(STATUS_ALIVE, &trans->shrd->status)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001030 if (hw_rf_kill)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001031 set_bit(STATUS_RF_KILL_HW,
1032 &trans->shrd->status);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001033 else
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -07001034 clear_bit(STATUS_RF_KILL_HW,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001035 &trans->shrd->status);
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001036 iwl_set_hw_rfkill_state(priv(trans), hw_rf_kill);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001037 }
1038
1039 handled |= CSR_INT_BIT_RF_KILL;
1040 }
1041
1042 /* Chip got too hot and stopped itself */
1043 if (inta & CSR_INT_BIT_CT_KILL) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001044 IWL_ERR(trans, "Microcode CT kill error detected.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001045 isr_stats->ctkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001046 handled |= CSR_INT_BIT_CT_KILL;
1047 }
1048
1049 /* Error detected by uCode */
1050 if (inta & CSR_INT_BIT_SW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001051 IWL_ERR(trans, "Microcode SW error detected. "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001052 " Restarting 0x%X.\n", inta);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001053 isr_stats->sw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -07001054 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001055 handled |= CSR_INT_BIT_SW_ERR;
1056 }
1057
1058 /* uCode wakes up after power-down sleep */
1059 if (inta & CSR_INT_BIT_WAKEUP) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001060 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1061 iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
1062 for (i = 0; i < hw_params(trans).max_txq_num; i++)
Emmanuel Grumbachfd656932011-08-25 23:11:19 -07001063 iwl_txq_update_write_ptr(trans,
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001064 &trans_pcie->txq[i]);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001065
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001066 isr_stats->wakeup++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001067
1068 handled |= CSR_INT_BIT_WAKEUP;
1069 }
1070
1071 /* All uCode command responses, including Tx command responses,
1072 * Rx "responses" (frame-received notification), and other
1073 * notifications from uCode come through here*/
1074 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1075 CSR_INT_BIT_RX_PERIODIC)) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001076 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001077 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1078 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001079 iwl_write32(trans, CSR_FH_INT_STATUS,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001080 CSR_FH_INT_RX_MASK);
1081 }
1082 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1083 handled |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001084 iwl_write32(trans,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001085 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001086 }
1087 /* Sending RX interrupt require many steps to be done in the
1088 * the device:
1089 * 1- write interrupt to current index in ICT table.
1090 * 2- dma RX frame.
1091 * 3- update RX shared data to indicate last write index.
1092 * 4- send interrupt.
1093 * This could lead to RX race, driver could receive RX interrupt
1094 * but the shared data changes does not reflect this;
1095 * periodic interrupt will detect any dangling Rx activity.
1096 */
1097
1098 /* Disable periodic interrupt; we use it as just a one-shot. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001099 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001100 CSR_INT_PERIODIC_DIS);
Gregory Greenmana5916972012-01-10 19:22:56 +02001101#ifdef CONFIG_IWLWIFI_IDI
1102 iwl_amfh_rx_handler();
1103#else
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001104 iwl_rx_handle(trans);
Gregory Greenmana5916972012-01-10 19:22:56 +02001105#endif
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001106 /*
1107 * Enable periodic interrupt in 8 msec only if we received
1108 * real RX interrupt (instead of just periodic int), to catch
1109 * any dangling Rx interrupt. If it was just the periodic
1110 * interrupt, there was no dangling Rx activity, and no need
1111 * to extend the periodic interrupt; one-shot is enough.
1112 */
1113 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001114 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001115 CSR_INT_PERIODIC_ENA);
1116
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001117 isr_stats->rx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001118 }
1119
1120 /* This "Tx" DMA channel is used only for loading uCode */
1121 if (inta & CSR_INT_BIT_FH_TX) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001122 iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001123 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001124 isr_stats->tx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001125 handled |= CSR_INT_BIT_FH_TX;
1126 /* Wake up uCode load routine, now that load is complete */
Don Fry5703ddb2011-11-10 06:55:07 -08001127 trans->ucode_write_complete = 1;
Johannes Bergeffd4d92011-09-15 11:46:52 -07001128 wake_up(&trans->shrd->wait_command_queue);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001129 }
1130
1131 if (inta & ~handled) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001132 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001133 isr_stats->unhandled++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001134 }
1135
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001136 if (inta & ~(trans_pcie->inta_mask)) {
1137 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1138 inta & ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001139 }
1140
1141 /* Re-enable all interrupts */
1142 /* only Re-enable if disabled by irq */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001143 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status))
1144 iwl_enable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001145 /* Re-enable RF_KILL if it occurred */
Emmanuel Grumbach1df06bd2012-01-09 16:35:08 +02001146 else if (handled & CSR_INT_BIT_RF_KILL) {
1147 IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
1148 iwl_write32(trans, CSR_INT_MASK, CSR_INT_BIT_RF_KILL);
1149 }
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001150}
1151
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001152/******************************************************************************
1153 *
1154 * ICT functions
1155 *
1156 ******************************************************************************/
Johannes Berg10667132011-12-19 14:00:59 -08001157
1158/* a device (PCI-E) page is 4096 bytes long */
1159#define ICT_SHIFT 12
1160#define ICT_SIZE (1 << ICT_SHIFT)
1161#define ICT_COUNT (ICT_SIZE / sizeof(u32))
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001162
1163/* Free dram table */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001164void iwl_free_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001165{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001166 struct iwl_trans_pcie *trans_pcie =
1167 IWL_TRANS_GET_PCIE_TRANS(trans);
1168
Johannes Berg10667132011-12-19 14:00:59 -08001169 if (trans_pcie->ict_tbl) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001170 dma_free_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001171 trans_pcie->ict_tbl,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001172 trans_pcie->ict_tbl_dma);
Johannes Berg10667132011-12-19 14:00:59 -08001173 trans_pcie->ict_tbl = NULL;
1174 trans_pcie->ict_tbl_dma = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001175 }
1176}
1177
1178
Johannes Berg10667132011-12-19 14:00:59 -08001179/*
1180 * allocate dram shared table, it is an aligned memory
1181 * block of ICT_SIZE.
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001182 * also reset all data related to ICT table interrupt.
1183 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001184int iwl_alloc_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001185{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001186 struct iwl_trans_pcie *trans_pcie =
1187 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001188
Johannes Berg10667132011-12-19 14:00:59 -08001189 trans_pcie->ict_tbl =
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001190 dma_alloc_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001191 &trans_pcie->ict_tbl_dma,
1192 GFP_KERNEL);
1193 if (!trans_pcie->ict_tbl)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001194 return -ENOMEM;
1195
Johannes Berg10667132011-12-19 14:00:59 -08001196 /* just an API sanity check ... it is guaranteed to be aligned */
1197 if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
1198 iwl_free_isr_ict(trans);
1199 return -EINVAL;
1200 }
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001201
Johannes Berg10667132011-12-19 14:00:59 -08001202 IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
1203 (unsigned long long)trans_pcie->ict_tbl_dma);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001204
Johannes Berg10667132011-12-19 14:00:59 -08001205 IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001206
1207 /* reset table and index to all 0 */
Johannes Berg10667132011-12-19 14:00:59 -08001208 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001209 trans_pcie->ict_index = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001210
1211 /* add periodic RX interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001212 trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001213 return 0;
1214}
1215
1216/* Device is going up inform it about using ICT interrupt table,
1217 * also we need to tell the driver to start using ICT interrupt.
1218 */
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001219void iwl_reset_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001220{
1221 u32 val;
1222 unsigned long flags;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001223 struct iwl_trans_pcie *trans_pcie =
1224 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001225
Johannes Berg10667132011-12-19 14:00:59 -08001226 if (!trans_pcie->ict_tbl)
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001227 return;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001228
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001229 spin_lock_irqsave(&trans->shrd->lock, flags);
1230 iwl_disable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001231
Johannes Berg10667132011-12-19 14:00:59 -08001232 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001233
Johannes Berg10667132011-12-19 14:00:59 -08001234 val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001235
1236 val |= CSR_DRAM_INT_TBL_ENABLE;
1237 val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
1238
Johannes Berg10667132011-12-19 14:00:59 -08001239 IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001240
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001241 iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001242 trans_pcie->use_ict = true;
1243 trans_pcie->ict_index = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001244 iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001245 iwl_enable_interrupts(trans);
1246 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001247}
1248
1249/* Device is going down disable ict interrupt usage */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001250void iwl_disable_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001251{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001252 struct iwl_trans_pcie *trans_pcie =
1253 IWL_TRANS_GET_PCIE_TRANS(trans);
1254
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001255 unsigned long flags;
1256
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001257 spin_lock_irqsave(&trans->shrd->lock, flags);
1258 trans_pcie->use_ict = false;
1259 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001260}
1261
1262static irqreturn_t iwl_isr(int irq, void *data)
1263{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001264 struct iwl_trans *trans = data;
1265 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001266 u32 inta, inta_mask;
1267 unsigned long flags;
1268#ifdef CONFIG_IWLWIFI_DEBUG
1269 u32 inta_fh;
1270#endif
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001271 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001272 return IRQ_NONE;
1273
Johannes Bergb80667e2011-12-09 07:26:13 -08001274 trace_iwlwifi_dev_irq(priv(trans));
1275
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001276 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1277
1278 spin_lock_irqsave(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001279
1280 /* Disable (but don't clear!) interrupts here to avoid
1281 * back-to-back ISRs and sporadic interrupts from our NIC.
1282 * If we have something to service, the tasklet will re-enable ints.
1283 * If we *don't* have something, we'll re-enable before leaving here. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001284 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1285 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001286
1287 /* Discover which interrupts are active/pending */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001288 inta = iwl_read32(trans, CSR_INT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001289
1290 /* Ignore interrupt if there's nothing in NIC to service.
1291 * This may be due to IRQ shared with another device,
1292 * or due to sporadic interrupts thrown from our NIC. */
1293 if (!inta) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001294 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001295 goto none;
1296 }
1297
1298 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1299 /* Hardware disappeared. It might have already raised
1300 * an interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001301 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001302 goto unplugged;
1303 }
1304
1305#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001306 if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001307 inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001308 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001309 "fh 0x%08x\n", inta, inta_mask, inta_fh);
1310 }
1311#endif
1312
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001313 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001314 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1315 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001316 tasklet_schedule(&trans_pcie->irq_tasklet);
1317 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1318 !trans_pcie->inta)
1319 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001320
1321 unplugged:
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001322 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001323 return IRQ_HANDLED;
1324
1325 none:
1326 /* re-enable interrupts here since we don't have anything to service. */
1327 /* only Re-enable if disabled by irq and no schedules tasklet. */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001328 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1329 !trans_pcie->inta)
1330 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001331
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001332 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001333 return IRQ_NONE;
1334}
1335
1336/* interrupt handler using ict table, with this interrupt driver will
1337 * stop using INTA register to get device's interrupt, reading this register
1338 * is expensive, device will write interrupts in ICT dram table, increment
1339 * index then will fire interrupt to driver, driver will OR all ICT table
1340 * entries from current index up to table entry with 0 value. the result is
1341 * the interrupt we need to service, driver will set the entries back to 0 and
1342 * set index.
1343 */
1344irqreturn_t iwl_isr_ict(int irq, void *data)
1345{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001346 struct iwl_trans *trans = data;
1347 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001348 u32 inta, inta_mask;
1349 u32 val = 0;
Johannes Bergb80667e2011-12-09 07:26:13 -08001350 u32 read;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001351 unsigned long flags;
1352
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001353 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001354 return IRQ_NONE;
1355
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001356 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1357
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001358 /* dram interrupt table not set yet,
1359 * use legacy interrupt.
1360 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001361 if (!trans_pcie->use_ict)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001362 return iwl_isr(irq, data);
1363
Johannes Bergb80667e2011-12-09 07:26:13 -08001364 trace_iwlwifi_dev_irq(priv(trans));
1365
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001366 spin_lock_irqsave(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001367
1368 /* Disable (but don't clear!) interrupts here to avoid
1369 * back-to-back ISRs and sporadic interrupts from our NIC.
1370 * If we have something to service, the tasklet will re-enable ints.
1371 * If we *don't* have something, we'll re-enable before leaving here.
1372 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001373 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1374 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001375
1376
1377 /* Ignore interrupt if there's nothing in NIC to service.
1378 * This may be due to IRQ shared with another device,
1379 * or due to sporadic interrupts thrown from our NIC. */
Johannes Bergb80667e2011-12-09 07:26:13 -08001380 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1381 trace_iwlwifi_dev_ict_read(priv(trans), trans_pcie->ict_index, read);
1382 if (!read) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001383 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001384 goto none;
1385 }
1386
Johannes Bergb80667e2011-12-09 07:26:13 -08001387 /*
1388 * Collect all entries up to the first 0, starting from ict_index;
1389 * note we already read at ict_index.
1390 */
1391 do {
1392 val |= read;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001393 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
Johannes Bergb80667e2011-12-09 07:26:13 -08001394 trans_pcie->ict_index, read);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001395 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1396 trans_pcie->ict_index =
1397 iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001398
Johannes Bergb80667e2011-12-09 07:26:13 -08001399 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1400 trace_iwlwifi_dev_ict_read(priv(trans), trans_pcie->ict_index,
1401 read);
1402 } while (read);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001403
1404 /* We should not get this value, just ignore it. */
1405 if (val == 0xffffffff)
1406 val = 0;
1407
1408 /*
1409 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1410 * (bit 15 before shifting it to 31) to clear when using interrupt
1411 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1412 * so we use them to decide on the real state of the Rx bit.
1413 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1414 */
1415 if (val & 0xC0000)
1416 val |= 0x8000;
1417
1418 inta = (0xff & val) | ((0xff00 & val) << 16);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001419 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001420 inta, inta_mask, val);
1421
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001422 inta &= trans_pcie->inta_mask;
1423 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001424
1425 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1426 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001427 tasklet_schedule(&trans_pcie->irq_tasklet);
1428 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
Johannes Bergb80667e2011-12-09 07:26:13 -08001429 !trans_pcie->inta) {
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001430 /* Allow interrupt if was disabled by this handler and
1431 * no tasklet was schedules, We should not enable interrupt,
1432 * tasklet will enable it.
1433 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001434 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001435 }
1436
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001437 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001438 return IRQ_HANDLED;
1439
1440 none:
1441 /* re-enable interrupts here since we don't have anything to service.
1442 * only Re-enable if disabled by irq.
1443 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001444 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
Johannes Bergb80667e2011-12-09 07:26:13 -08001445 !trans_pcie->inta)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001446 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001447
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001448 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001449 return IRQ_NONE;
1450}