blob: e22cc6d8c07fab9c957a4a24998dcb43e178bf21 [file] [log] [blame]
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
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>
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -070031#include <linux/gfp.h>
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070032
33#include "iwl-dev.h"
34#include "iwl-agn.h"
35#include "iwl-core.h"
36#include "iwl-io.h"
37#include "iwl-helpers.h"
38#include "iwl-trans-int-pcie.h"
39
40/******************************************************************************
41 *
42 * RX path functions
43 *
44 ******************************************************************************/
45
46/*
47 * Rx theory of operation
48 *
49 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
50 * each of which point to Receive Buffers to be filled by the NIC. These get
51 * used not only for Rx frames, but for any command response or notification
52 * from the NIC. The driver and NIC manage the Rx buffers by means
53 * of indexes into the circular buffer.
54 *
55 * Rx Queue Indexes
56 * The host/firmware share two index registers for managing the Rx buffers.
57 *
58 * The READ index maps to the first position that the firmware may be writing
59 * to -- the driver can read up to (but not including) this position and get
60 * good data.
61 * The READ index is managed by the firmware once the card is enabled.
62 *
63 * The WRITE index maps to the last position the driver has read from -- the
64 * position preceding WRITE is the last slot the firmware can place a packet.
65 *
66 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
67 * WRITE = READ.
68 *
69 * During initialization, the host sets up the READ queue position to the first
70 * INDEX position, and WRITE to the last (READ - 1 wrapped)
71 *
72 * When the firmware places a packet in a buffer, it will advance the READ index
73 * and fire the RX interrupt. The driver can then query the READ index and
74 * process as many packets as possible, moving the WRITE index forward as it
75 * resets the Rx queue buffers with new memory.
76 *
77 * The management in the driver is as follows:
78 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
79 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
80 * to replenish the iwl->rxq->rx_free.
81 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
82 * iwl->rxq is replenished and the READ INDEX is updated (updating the
83 * 'processed' and 'read' driver indexes as well)
84 * + A received packet is processed and handed to the kernel network stack,
85 * detached from the iwl->rxq. The driver 'processed' index is updated.
86 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
87 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
88 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
89 * were enough free buffers and RX_STALLED is set it is cleared.
90 *
91 *
92 * Driver sequence:
93 *
94 * iwl_rx_queue_alloc() Allocates rx_free
95 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
96 * iwl_rx_queue_restock
97 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
98 * queue, updates firmware pointers, and updates
99 * the WRITE index. If insufficient rx_free buffers
100 * are available, schedules iwl_rx_replenish
101 *
102 * -- enable interrupts --
103 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
104 * READ INDEX, detaching the SKB from the pool.
105 * Moves the packet buffer from queue to rx_used.
106 * Calls iwl_rx_queue_restock to refill any empty
107 * slots.
108 * ...
109 *
110 */
111
112/**
113 * iwl_rx_queue_space - Return number of free slots available in queue.
114 */
115static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
116{
117 int s = q->read - q->write;
118 if (s <= 0)
119 s += RX_QUEUE_SIZE;
120 /* keep some buffer to not confuse full and empty queue */
121 s -= 2;
122 if (s < 0)
123 s = 0;
124 return s;
125}
126
127/**
128 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
129 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700130void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700131 struct iwl_rx_queue *q)
132{
133 unsigned long flags;
134 u32 reg;
135
136 spin_lock_irqsave(&q->lock, flags);
137
138 if (q->need_update == 0)
139 goto exit_unlock;
140
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700141 if (hw_params(trans).shadow_reg_enable) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700142 /* shadow register enabled */
143 /* Device expects a multiple of 8 */
144 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700145 iwl_write32(bus(trans), FH_RSCSR_CHNL0_WPTR, q->write_actual);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700146 } else {
147 /* If power-saving is in use, make sure device is awake */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700148 if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700149 reg = iwl_read32(bus(trans), CSR_UCODE_DRV_GP1);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700150
151 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700152 IWL_DEBUG_INFO(trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700153 "Rx queue requesting wakeup,"
154 " GP1 = 0x%x\n", reg);
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700155 iwl_set_bit(bus(trans), CSR_GP_CNTRL,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700156 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
157 goto exit_unlock;
158 }
159
160 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700161 iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700162 q->write_actual);
163
164 /* Else device is assumed to be awake */
165 } else {
166 /* Device expects a multiple of 8 */
167 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700168 iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700169 q->write_actual);
170 }
171 }
172 q->need_update = 0;
173
174 exit_unlock:
175 spin_unlock_irqrestore(&q->lock, flags);
176}
177
178/**
179 * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
180 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700181static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700182{
183 return cpu_to_le32((u32)(dma_addr >> 8));
184}
185
186/**
187 * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
188 *
189 * If there are slots in the RX queue that need to be restocked,
190 * and we have free pre-allocated buffers, fill the ranks as much
191 * as we can, pulling from rx_free.
192 *
193 * This moves the 'write' index forward to catch up with 'processed', and
194 * also updates the memory address in the firmware to reference the new
195 * target buffer.
196 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700197static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700198{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700199 struct iwl_trans_pcie *trans_pcie =
200 IWL_TRANS_GET_PCIE_TRANS(trans);
201
202 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700203 struct list_head *element;
204 struct iwl_rx_mem_buffer *rxb;
205 unsigned long flags;
206
207 spin_lock_irqsave(&rxq->lock, flags);
208 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
209 /* The overwritten rxb must be a used one */
210 rxb = rxq->queue[rxq->write];
211 BUG_ON(rxb && rxb->page);
212
213 /* Get next free Rx buffer, remove from free list */
214 element = rxq->rx_free.next;
215 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
216 list_del(element);
217
218 /* Point to Rx buffer via next RBD in circular buffer */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700219 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700220 rxq->queue[rxq->write] = rxb;
221 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
222 rxq->free_count--;
223 }
224 spin_unlock_irqrestore(&rxq->lock, flags);
225 /* If the pre-allocated buffer pool is dropping low, schedule to
226 * refill it */
227 if (rxq->free_count <= RX_LOW_WATERMARK)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700228 queue_work(trans->shrd->workqueue, &trans_pcie->rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700229
230
231 /* If we've added more space for the firmware to place data, tell it.
232 * Increment device's write pointer in multiples of 8. */
233 if (rxq->write_actual != (rxq->write & ~0x7)) {
234 spin_lock_irqsave(&rxq->lock, flags);
235 rxq->need_update = 1;
236 spin_unlock_irqrestore(&rxq->lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700237 iwl_rx_queue_update_write_ptr(trans, rxq);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700238 }
239}
240
241/**
242 * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
243 *
244 * When moving to rx_free an SKB is allocated for the slot.
245 *
246 * Also restock the Rx queue via iwl_rx_queue_restock.
247 * This is called as a scheduled work item (except for during initialization)
248 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700249static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700250{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700251 struct iwl_trans_pcie *trans_pcie =
252 IWL_TRANS_GET_PCIE_TRANS(trans);
253
254 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700255 struct list_head *element;
256 struct iwl_rx_mem_buffer *rxb;
257 struct page *page;
258 unsigned long flags;
259 gfp_t gfp_mask = priority;
260
261 while (1) {
262 spin_lock_irqsave(&rxq->lock, flags);
263 if (list_empty(&rxq->rx_used)) {
264 spin_unlock_irqrestore(&rxq->lock, flags);
265 return;
266 }
267 spin_unlock_irqrestore(&rxq->lock, flags);
268
269 if (rxq->free_count > RX_LOW_WATERMARK)
270 gfp_mask |= __GFP_NOWARN;
271
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700272 if (hw_params(trans).rx_page_order > 0)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700273 gfp_mask |= __GFP_COMP;
274
275 /* Alloc a new receive buffer */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700276 page = alloc_pages(gfp_mask,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700277 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700278 if (!page) {
279 if (net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700280 IWL_DEBUG_INFO(trans, "alloc_pages failed, "
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700281 "order: %d\n",
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700282 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700283
284 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
285 net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700286 IWL_CRIT(trans, "Failed to alloc_pages with %s."
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700287 "Only %u free buffers remaining.\n",
288 priority == GFP_ATOMIC ?
289 "GFP_ATOMIC" : "GFP_KERNEL",
290 rxq->free_count);
291 /* We don't reschedule replenish work here -- we will
292 * call the restock method and if it still needs
293 * more buffers it will schedule replenish */
294 return;
295 }
296
297 spin_lock_irqsave(&rxq->lock, flags);
298
299 if (list_empty(&rxq->rx_used)) {
300 spin_unlock_irqrestore(&rxq->lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700301 __free_pages(page, hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700302 return;
303 }
304 element = rxq->rx_used.next;
305 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
306 list_del(element);
307
308 spin_unlock_irqrestore(&rxq->lock, flags);
309
310 BUG_ON(rxb->page);
311 rxb->page = page;
312 /* Get physical address of the RB */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700313 rxb->page_dma = dma_map_page(bus(trans)->dev, page, 0,
314 PAGE_SIZE << hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700315 DMA_FROM_DEVICE);
316 /* dma address must be no more than 36 bits */
317 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
318 /* and also 256 byte aligned! */
319 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
320
321 spin_lock_irqsave(&rxq->lock, flags);
322
323 list_add_tail(&rxb->list, &rxq->rx_free);
324 rxq->free_count++;
325
326 spin_unlock_irqrestore(&rxq->lock, flags);
327 }
328}
329
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700330void iwlagn_rx_replenish(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700331{
332 unsigned long flags;
333
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700334 iwlagn_rx_allocate(trans, GFP_KERNEL);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700335
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700336 spin_lock_irqsave(&trans->shrd->lock, flags);
337 iwlagn_rx_queue_restock(trans);
338 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700339}
340
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700341static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700342{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700343 iwlagn_rx_allocate(trans, GFP_ATOMIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700344
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700345 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700346}
347
348void iwl_bg_rx_replenish(struct work_struct *data)
349{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700350 struct iwl_trans_pcie *trans_pcie =
351 container_of(data, struct iwl_trans_pcie, rx_replenish);
352 struct iwl_trans *trans = trans_pcie->trans;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700353
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700354 if (test_bit(STATUS_EXIT_PENDING, &trans->shrd->status))
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700355 return;
356
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700357 mutex_lock(&trans->shrd->mutex);
358 iwlagn_rx_replenish(trans);
359 mutex_unlock(&trans->shrd->mutex);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700360}
361
362/**
363 * iwl_rx_handle - Main entry function for receiving responses from uCode
364 *
365 * Uses the priv->rx_handlers callback function array to invoke
366 * the appropriate handlers, including command responses,
367 * frame-received notifications, and other notifications.
368 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700369static void iwl_rx_handle(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700370{
371 struct iwl_rx_mem_buffer *rxb;
372 struct iwl_rx_packet *pkt;
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700373 struct iwl_trans_pcie *trans_pcie =
374 IWL_TRANS_GET_PCIE_TRANS(trans);
375 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700376 u32 r, i;
377 int reclaim;
378 unsigned long flags;
379 u8 fill_rx = 0;
380 u32 count = 8;
381 int total_empty;
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)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700390 IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700391
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) {
401 int len;
402
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 Grumbach5a878bf2011-08-25 23:10:51 -0700415 dma_unmap_page(bus(trans)->dev, rxb->page_dma,
416 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 Grumbach5a878bf2011-08-25 23:10:51 -0700441 iwl_rx_dispatch(priv(trans), rxb);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700442
443 /*
444 * XXX: After here, we should always check rxb->page
445 * against NULL before touching it or its virtual
446 * memory (pkt). Because some rx_handler might have
447 * already taken or freed the pages.
448 */
449
450 if (reclaim) {
451 /* Invoke any callbacks, transfer the buffer to caller,
452 * and fire off the (possibly) blocking
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700453 * iwl_trans_send_cmd()
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700454 * as we reclaim the driver command queue */
455 if (rxb->page)
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700456 iwl_tx_cmd_complete(trans, rxb);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700457 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700458 IWL_WARN(trans, "Claim null rxb?\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700459 }
460
461 /* Reuse the page if possible. For notification packets and
462 * SKBs that fail to Rx correctly, add them back into the
463 * rx_free list for reuse later. */
464 spin_lock_irqsave(&rxq->lock, flags);
465 if (rxb->page != NULL) {
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700466 rxb->page_dma = dma_map_page(bus(trans)->dev, rxb->page,
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700467 0, PAGE_SIZE <<
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700468 hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700469 DMA_FROM_DEVICE);
470 list_add_tail(&rxb->list, &rxq->rx_free);
471 rxq->free_count++;
472 } else
473 list_add_tail(&rxb->list, &rxq->rx_used);
474
475 spin_unlock_irqrestore(&rxq->lock, flags);
476
477 i = (i + 1) & RX_QUEUE_MASK;
478 /* If there are a lot of unused frames,
479 * restock the Rx queue so ucode wont assert. */
480 if (fill_rx) {
481 count++;
482 if (count >= 8) {
483 rxq->read = i;
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700484 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700485 count = 0;
486 }
487 }
488 }
489
490 /* Backtrack one entry */
491 rxq->read = i;
492 if (fill_rx)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700493 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700494 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700495 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700496}
497
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700498static const char * const desc_lookup_text[] = {
499 "OK",
500 "FAIL",
501 "BAD_PARAM",
502 "BAD_CHECKSUM",
503 "NMI_INTERRUPT_WDG",
504 "SYSASSERT",
505 "FATAL_ERROR",
506 "BAD_COMMAND",
507 "HW_ERROR_TUNE_LOCK",
508 "HW_ERROR_TEMPERATURE",
509 "ILLEGAL_CHAN_FREQ",
510 "VCC_NOT_STABLE",
511 "FH_ERROR",
512 "NMI_INTERRUPT_HOST",
513 "NMI_INTERRUPT_ACTION_PT",
514 "NMI_INTERRUPT_UNKNOWN",
515 "UCODE_VERSION_MISMATCH",
516 "HW_ERROR_ABS_LOCK",
517 "HW_ERROR_CAL_LOCK_FAIL",
518 "NMI_INTERRUPT_INST_ACTION_PT",
519 "NMI_INTERRUPT_DATA_ACTION_PT",
520 "NMI_TRM_HW_ER",
521 "NMI_INTERRUPT_TRM",
522 "NMI_INTERRUPT_BREAK_POINT",
523 "DEBUG_0",
524 "DEBUG_1",
525 "DEBUG_2",
526 "DEBUG_3",
527};
528
529static struct { char *name; u8 num; } advanced_lookup[] = {
530 { "NMI_INTERRUPT_WDG", 0x34 },
531 { "SYSASSERT", 0x35 },
532 { "UCODE_VERSION_MISMATCH", 0x37 },
533 { "BAD_COMMAND", 0x38 },
534 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
535 { "FATAL_ERROR", 0x3D },
536 { "NMI_TRM_HW_ERR", 0x46 },
537 { "NMI_INTERRUPT_TRM", 0x4C },
538 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
539 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
540 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
541 { "NMI_INTERRUPT_HOST", 0x66 },
542 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
543 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
544 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
545 { "ADVANCED_SYSASSERT", 0 },
546};
547
548static const char *desc_lookup(u32 num)
549{
550 int i;
551 int max = ARRAY_SIZE(desc_lookup_text);
552
553 if (num < max)
554 return desc_lookup_text[num];
555
556 max = ARRAY_SIZE(advanced_lookup) - 1;
557 for (i = 0; i < max; i++) {
558 if (advanced_lookup[i].num == num)
559 break;
560 }
561 return advanced_lookup[i].name;
562}
563
564#define ERROR_START_OFFSET (1 * sizeof(u32))
565#define ERROR_ELEM_SIZE (7 * sizeof(u32))
566
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700567static void iwl_dump_nic_error_log(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700568{
569 u32 base;
570 struct iwl_error_event_table table;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700571 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700572 struct iwl_trans_pcie *trans_pcie =
573 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700574
575 base = priv->device_pointers.error_event_table;
576 if (priv->ucode_type == IWL_UCODE_INIT) {
577 if (!base)
578 base = priv->init_errlog_ptr;
579 } else {
580 if (!base)
581 base = priv->inst_errlog_ptr;
582 }
583
584 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700585 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700586 "Not valid error log pointer 0x%08X for %s uCode\n",
587 base,
588 (priv->ucode_type == IWL_UCODE_INIT)
589 ? "Init" : "RT");
590 return;
591 }
592
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700593 iwl_read_targ_mem_words(bus(priv), base, &table, sizeof(table));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700594
595 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700596 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
597 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
598 trans->shrd->status, table.valid);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700599 }
600
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700601 trans_pcie->isr_stats.err_code = table.error_id;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700602
603 trace_iwlwifi_dev_ucode_error(priv, table.error_id, table.tsf_low,
604 table.data1, table.data2, table.line,
605 table.blink1, table.blink2, table.ilink1,
606 table.ilink2, table.bcon_time, table.gp1,
607 table.gp2, table.gp3, table.ucode_ver,
608 table.hw_ver, table.brd_ver);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700609 IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700610 desc_lookup(table.error_id));
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700611 IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
612 IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
613 IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
614 IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
615 IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
616 IWL_ERR(trans, "0x%08X | data1\n", table.data1);
617 IWL_ERR(trans, "0x%08X | data2\n", table.data2);
618 IWL_ERR(trans, "0x%08X | line\n", table.line);
619 IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
620 IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
621 IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
622 IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
623 IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
624 IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
625 IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
626 IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
627 IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
628 IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700629}
630
631/**
632 * iwl_irq_handle_error - called for HW or SW error interrupt from card
633 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700634static void iwl_irq_handle_error(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700635{
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700636 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700637 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
638 if (priv->cfg->internal_wimax_coex &&
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700639 (!(iwl_read_prph(bus(trans), APMG_CLK_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700640 APMS_CLK_VAL_MRB_FUNC_MODE) ||
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700641 (iwl_read_prph(bus(trans), APMG_PS_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700642 APMG_PS_CTRL_VAL_RESET_REQ))) {
643 /*
644 * Keep the restart process from trying to send host
645 * commands by clearing the ready bit.
646 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700647 clear_bit(STATUS_READY, &trans->shrd->status);
648 clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700649 wake_up_interruptible(&priv->shrd->wait_command_queue);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700650 IWL_ERR(trans, "RF is used by WiMAX\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700651 return;
652 }
653
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700654 IWL_ERR(trans, "Loaded firmware version: %s\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700655 priv->hw->wiphy->fw_version);
656
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700657 iwl_dump_nic_error_log(trans);
658 iwl_dump_csr(trans);
659 iwl_dump_fh(trans, NULL, false);
660 iwl_dump_nic_event_log(trans, false, NULL, false);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700661#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700662 if (iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700663 iwl_print_rx_config_cmd(priv,
664 &priv->contexts[IWL_RXON_CTX_BSS]);
665#endif
666
667 iwlagn_fw_error(priv, false);
668}
669
670#define EVENT_START_OFFSET (4 * sizeof(u32))
671
672/**
673 * iwl_print_event_log - Dump error event log to syslog
674 *
675 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700676static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700677 u32 num_events, u32 mode,
678 int pos, char **buf, size_t bufsz)
679{
680 u32 i;
681 u32 base; /* SRAM byte address of event log header */
682 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
683 u32 ptr; /* SRAM byte address of log data */
684 u32 ev, time, data; /* event log data */
685 unsigned long reg_flags;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700686 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700687
688 if (num_events == 0)
689 return pos;
690
691 base = priv->device_pointers.log_event_table;
692 if (priv->ucode_type == IWL_UCODE_INIT) {
693 if (!base)
694 base = priv->init_evtlog_ptr;
695 } else {
696 if (!base)
697 base = priv->inst_evtlog_ptr;
698 }
699
700 if (mode == 0)
701 event_size = 2 * sizeof(u32);
702 else
703 event_size = 3 * sizeof(u32);
704
705 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
706
707 /* Make sure device is powered up for SRAM reads */
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700708 spin_lock_irqsave(&bus(trans)->reg_lock, reg_flags);
709 iwl_grab_nic_access(bus(trans));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700710
711 /* Set starting address; reads will auto-increment */
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700712 iwl_write32(bus(trans), HBUS_TARG_MEM_RADDR, ptr);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700713 rmb();
714
715 /* "time" is actually "data" for mode 0 (no timestamp).
716 * place event id # at far right for easier visual parsing. */
717 for (i = 0; i < num_events; i++) {
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700718 ev = iwl_read32(bus(trans), HBUS_TARG_MEM_RDAT);
719 time = iwl_read32(bus(trans), HBUS_TARG_MEM_RDAT);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700720 if (mode == 0) {
721 /* data, ev */
722 if (bufsz) {
723 pos += scnprintf(*buf + pos, bufsz - pos,
724 "EVT_LOG:0x%08x:%04u\n",
725 time, ev);
726 } else {
727 trace_iwlwifi_dev_ucode_event(priv, 0,
728 time, ev);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700729 IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700730 time, ev);
731 }
732 } else {
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700733 data = iwl_read32(bus(trans), HBUS_TARG_MEM_RDAT);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700734 if (bufsz) {
735 pos += scnprintf(*buf + pos, bufsz - pos,
736 "EVT_LOGT:%010u:0x%08x:%04u\n",
737 time, data, ev);
738 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700739 IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700740 time, data, ev);
741 trace_iwlwifi_dev_ucode_event(priv, time,
742 data, ev);
743 }
744 }
745 }
746
747 /* Allow device to power down */
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700748 iwl_release_nic_access(bus(trans));
749 spin_unlock_irqrestore(&bus(trans)->reg_lock, reg_flags);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700750 return pos;
751}
752
753/**
754 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
755 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700756static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700757 u32 num_wraps, u32 next_entry,
758 u32 size, u32 mode,
759 int pos, char **buf, size_t bufsz)
760{
761 /*
762 * display the newest DEFAULT_LOG_ENTRIES entries
763 * i.e the entries just before the next ont that uCode would fill.
764 */
765 if (num_wraps) {
766 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700767 pos = iwl_print_event_log(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700768 capacity - (size - next_entry),
769 size - next_entry, mode,
770 pos, buf, bufsz);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700771 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700772 next_entry, mode,
773 pos, buf, bufsz);
774 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700775 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700776 size, mode, pos, buf, bufsz);
777 } else {
778 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700779 pos = iwl_print_event_log(trans, 0, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700780 mode, pos, buf, bufsz);
781 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700782 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700783 size, mode, pos, buf, bufsz);
784 }
785 }
786 return pos;
787}
788
789#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
790
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700791int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700792 char **buf, bool display)
793{
794 u32 base; /* SRAM byte address of event log header */
795 u32 capacity; /* event log capacity in # entries */
796 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
797 u32 num_wraps; /* # times uCode wrapped to top of log */
798 u32 next_entry; /* index of next entry to be written by uCode */
799 u32 size; /* # entries that we'll print */
800 u32 logsize;
801 int pos = 0;
802 size_t bufsz = 0;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700803 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700804
805 base = priv->device_pointers.log_event_table;
806 if (priv->ucode_type == IWL_UCODE_INIT) {
807 logsize = priv->init_evtlog_size;
808 if (!base)
809 base = priv->init_evtlog_ptr;
810 } else {
811 logsize = priv->inst_evtlog_size;
812 if (!base)
813 base = priv->inst_evtlog_ptr;
814 }
815
816 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700817 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700818 "Invalid event log pointer 0x%08X for %s uCode\n",
819 base,
820 (priv->ucode_type == IWL_UCODE_INIT)
821 ? "Init" : "RT");
822 return -EINVAL;
823 }
824
825 /* event log header */
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700826 capacity = iwl_read_targ_mem(bus(trans), base);
827 mode = iwl_read_targ_mem(bus(trans), base + (1 * sizeof(u32)));
828 num_wraps = iwl_read_targ_mem(bus(trans), base + (2 * sizeof(u32)));
829 next_entry = iwl_read_targ_mem(bus(trans), base + (3 * sizeof(u32)));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700830
831 if (capacity > logsize) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700832 IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
833 "entries\n", capacity, logsize);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700834 capacity = logsize;
835 }
836
837 if (next_entry > logsize) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700838 IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700839 next_entry, logsize);
840 next_entry = logsize;
841 }
842
843 size = num_wraps ? capacity : next_entry;
844
845 /* bail out if nothing in log */
846 if (size == 0) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700847 IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700848 return pos;
849 }
850
851 /* enable/disable bt channel inhibition */
852 priv->bt_ch_announce = iwlagn_mod_params.bt_ch_announce;
853
854#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700855 if (!(iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) && !full_log)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700856 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
857 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
858#else
859 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
860 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
861#endif
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700862 IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700863 size);
864
865#ifdef CONFIG_IWLWIFI_DEBUG
866 if (display) {
867 if (full_log)
868 bufsz = capacity * 48;
869 else
870 bufsz = size * 48;
871 *buf = kmalloc(bufsz, GFP_KERNEL);
872 if (!*buf)
873 return -ENOMEM;
874 }
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700875 if ((iwl_get_debug_level(trans->shrd) & IWL_DL_FW_ERRORS) || full_log) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700876 /*
877 * if uCode has wrapped back to top of log,
878 * start at the oldest entry,
879 * i.e the next one that uCode would fill.
880 */
881 if (num_wraps)
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700882 pos = iwl_print_event_log(trans, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700883 capacity - next_entry, mode,
884 pos, buf, bufsz);
885 /* (then/else) start at top of log */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700886 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700887 next_entry, mode, pos, buf, bufsz);
888 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700889 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700890 next_entry, size, mode,
891 pos, buf, bufsz);
892#else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700893 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700894 next_entry, size, mode,
895 pos, buf, bufsz);
896#endif
897 return pos;
898}
899
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700900/* tasklet for iwlagn interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700901void iwl_irq_tasklet(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700902{
903 u32 inta = 0;
904 u32 handled = 0;
905 unsigned long flags;
906 u32 i;
907#ifdef CONFIG_IWLWIFI_DEBUG
908 u32 inta_mask;
909#endif
910
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700911 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700912 struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
913
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700914
915 spin_lock_irqsave(&trans->shrd->lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700916
917 /* Ack/clear/reset pending uCode interrupts.
918 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
919 */
920 /* There is a hardware bug in the interrupt mask function that some
921 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
922 * they are disabled in the CSR_INT_MASK register. Furthermore the
923 * ICT interrupt handling mechanism has another bug that might cause
924 * these unmasked interrupts fail to be detected. We workaround the
925 * hardware bugs here by ACKing all the possible interrupts so that
926 * interrupt coalescing can still be achieved.
927 */
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700928 iwl_write32(bus(trans), CSR_INT,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700929 trans_pcie->inta | ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700930
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700931 inta = trans_pcie->inta;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700932
933#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700934 if (iwl_get_debug_level(trans->shrd) & IWL_DL_ISR) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700935 /* just for debug */
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700936 inta_mask = iwl_read32(bus(trans), CSR_INT_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700937 IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700938 inta, inta_mask);
939 }
940#endif
941
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700942 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700943
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700944 /* saved interrupt in inta variable now we can reset trans_pcie->inta */
945 trans_pcie->inta = 0;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700946
947 /* Now service all interrupt bits discovered above. */
948 if (inta & CSR_INT_BIT_HW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700949 IWL_ERR(trans, "Hardware error detected. Restarting.\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700950
951 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700952 iwl_disable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700953
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700954 isr_stats->hw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700955 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700956
957 handled |= CSR_INT_BIT_HW_ERR;
958
959 return;
960 }
961
962#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700963 if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700964 /* NIC fires this, but we don't use it, redundant with WAKEUP */
965 if (inta & CSR_INT_BIT_SCD) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700966 IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700967 "the frame/frames.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700968 isr_stats->sch++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700969 }
970
971 /* Alive notification via Rx interrupt will do the real work */
972 if (inta & CSR_INT_BIT_ALIVE) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700973 IWL_DEBUG_ISR(trans, "Alive interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700974 isr_stats->alive++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700975 }
976 }
977#endif
978 /* Safely ignore these bits for debug checks below */
979 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
980
981 /* HW RF KILL switch toggled */
982 if (inta & CSR_INT_BIT_RF_KILL) {
983 int hw_rf_kill = 0;
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700984 if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700985 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
986 hw_rf_kill = 1;
987
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700988 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700989 hw_rf_kill ? "disable radio" : "enable radio");
990
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700991 isr_stats->rfkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700992
993 /* driver only loads ucode once setting the interface up.
994 * the driver allows loading the ucode even if the radio
995 * is killed. Hence update the killswitch state here. The
996 * rfkill handler will care about restarting if needed.
997 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700998 if (!test_bit(STATUS_ALIVE, &trans->shrd->status)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700999 if (hw_rf_kill)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001000 set_bit(STATUS_RF_KILL_HW,
1001 &trans->shrd->status);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001002 else
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -07001003 clear_bit(STATUS_RF_KILL_HW,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001004 &trans->shrd->status);
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001005 iwl_set_hw_rfkill_state(priv(trans), hw_rf_kill);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001006 }
1007
1008 handled |= CSR_INT_BIT_RF_KILL;
1009 }
1010
1011 /* Chip got too hot and stopped itself */
1012 if (inta & CSR_INT_BIT_CT_KILL) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001013 IWL_ERR(trans, "Microcode CT kill error detected.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001014 isr_stats->ctkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001015 handled |= CSR_INT_BIT_CT_KILL;
1016 }
1017
1018 /* Error detected by uCode */
1019 if (inta & CSR_INT_BIT_SW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001020 IWL_ERR(trans, "Microcode SW error detected. "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001021 " Restarting 0x%X.\n", inta);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001022 isr_stats->sw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -07001023 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001024 handled |= CSR_INT_BIT_SW_ERR;
1025 }
1026
1027 /* uCode wakes up after power-down sleep */
1028 if (inta & CSR_INT_BIT_WAKEUP) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001029 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1030 iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
1031 for (i = 0; i < hw_params(trans).max_txq_num; i++)
Emmanuel Grumbachfd656932011-08-25 23:11:19 -07001032 iwl_txq_update_write_ptr(trans,
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001033 &trans_pcie->txq[i]);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001034
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001035 isr_stats->wakeup++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001036
1037 handled |= CSR_INT_BIT_WAKEUP;
1038 }
1039
1040 /* All uCode command responses, including Tx command responses,
1041 * Rx "responses" (frame-received notification), and other
1042 * notifications from uCode come through here*/
1043 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1044 CSR_INT_BIT_RX_PERIODIC)) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001045 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001046 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1047 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001048 iwl_write32(bus(trans), CSR_FH_INT_STATUS,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001049 CSR_FH_INT_RX_MASK);
1050 }
1051 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1052 handled |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001053 iwl_write32(bus(trans),
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001054 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001055 }
1056 /* Sending RX interrupt require many steps to be done in the
1057 * the device:
1058 * 1- write interrupt to current index in ICT table.
1059 * 2- dma RX frame.
1060 * 3- update RX shared data to indicate last write index.
1061 * 4- send interrupt.
1062 * This could lead to RX race, driver could receive RX interrupt
1063 * but the shared data changes does not reflect this;
1064 * periodic interrupt will detect any dangling Rx activity.
1065 */
1066
1067 /* Disable periodic interrupt; we use it as just a one-shot. */
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001068 iwl_write8(bus(trans), CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001069 CSR_INT_PERIODIC_DIS);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001070 iwl_rx_handle(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001071
1072 /*
1073 * Enable periodic interrupt in 8 msec only if we received
1074 * real RX interrupt (instead of just periodic int), to catch
1075 * any dangling Rx interrupt. If it was just the periodic
1076 * interrupt, there was no dangling Rx activity, and no need
1077 * to extend the periodic interrupt; one-shot is enough.
1078 */
1079 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001080 iwl_write8(bus(trans), CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001081 CSR_INT_PERIODIC_ENA);
1082
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001083 isr_stats->rx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001084 }
1085
1086 /* This "Tx" DMA channel is used only for loading uCode */
1087 if (inta & CSR_INT_BIT_FH_TX) {
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001088 iwl_write32(bus(trans), CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001089 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001090 isr_stats->tx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001091 handled |= CSR_INT_BIT_FH_TX;
1092 /* Wake up uCode load routine, now that load is complete */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001093 priv(trans)->ucode_write_complete = 1;
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001094 wake_up_interruptible(&trans->shrd->wait_command_queue);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001095 }
1096
1097 if (inta & ~handled) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001098 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001099 isr_stats->unhandled++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001100 }
1101
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001102 if (inta & ~(trans_pcie->inta_mask)) {
1103 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1104 inta & ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001105 }
1106
1107 /* Re-enable all interrupts */
1108 /* only Re-enable if disabled by irq */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001109 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status))
1110 iwl_enable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001111 /* Re-enable RF_KILL if it occurred */
1112 else if (handled & CSR_INT_BIT_RF_KILL)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001113 iwl_enable_rfkill_int(priv(trans));
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001114}
1115
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001116/******************************************************************************
1117 *
1118 * ICT functions
1119 *
1120 ******************************************************************************/
1121#define ICT_COUNT (PAGE_SIZE/sizeof(u32))
1122
1123/* Free dram table */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001124void iwl_free_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001125{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001126 struct iwl_trans_pcie *trans_pcie =
1127 IWL_TRANS_GET_PCIE_TRANS(trans);
1128
1129 if (trans_pcie->ict_tbl_vir) {
1130 dma_free_coherent(bus(trans)->dev,
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001131 (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001132 trans_pcie->ict_tbl_vir,
1133 trans_pcie->ict_tbl_dma);
1134 trans_pcie->ict_tbl_vir = NULL;
1135 memset(&trans_pcie->ict_tbl_dma, 0,
1136 sizeof(trans_pcie->ict_tbl_dma));
1137 memset(&trans_pcie->aligned_ict_tbl_dma, 0,
1138 sizeof(trans_pcie->aligned_ict_tbl_dma));
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001139 }
1140}
1141
1142
1143/* allocate dram shared table it is a PAGE_SIZE aligned
1144 * also reset all data related to ICT table interrupt.
1145 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001146int iwl_alloc_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001147{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001148 struct iwl_trans_pcie *trans_pcie =
1149 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001150
1151 /* allocate shrared data table */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001152 trans_pcie->ict_tbl_vir =
1153 dma_alloc_coherent(bus(trans)->dev,
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001154 (sizeof(u32) * ICT_COUNT) + PAGE_SIZE,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001155 &trans_pcie->ict_tbl_dma, GFP_KERNEL);
1156 if (!trans_pcie->ict_tbl_vir)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001157 return -ENOMEM;
1158
1159 /* align table to PAGE_SIZE boundary */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001160 trans_pcie->aligned_ict_tbl_dma =
1161 ALIGN(trans_pcie->ict_tbl_dma, PAGE_SIZE);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001162
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001163 IWL_DEBUG_ISR(trans, "ict dma addr %Lx dma aligned %Lx diff %d\n",
1164 (unsigned long long)trans_pcie->ict_tbl_dma,
1165 (unsigned long long)trans_pcie->aligned_ict_tbl_dma,
1166 (int)(trans_pcie->aligned_ict_tbl_dma -
1167 trans_pcie->ict_tbl_dma));
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001168
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001169 trans_pcie->ict_tbl = trans_pcie->ict_tbl_vir +
1170 (trans_pcie->aligned_ict_tbl_dma -
1171 trans_pcie->ict_tbl_dma);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001172
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001173 IWL_DEBUG_ISR(trans, "ict vir addr %p vir aligned %p diff %d\n",
1174 trans_pcie->ict_tbl, trans_pcie->ict_tbl_vir,
1175 (int)(trans_pcie->aligned_ict_tbl_dma -
1176 trans_pcie->ict_tbl_dma));
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001177
1178 /* reset table and index to all 0 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001179 memset(trans_pcie->ict_tbl_vir, 0,
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001180 (sizeof(u32) * ICT_COUNT) + PAGE_SIZE);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001181 trans_pcie->ict_index = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001182
1183 /* add periodic RX interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001184 trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001185 return 0;
1186}
1187
1188/* Device is going up inform it about using ICT interrupt table,
1189 * also we need to tell the driver to start using ICT interrupt.
1190 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -07001191int iwl_reset_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001192{
1193 u32 val;
1194 unsigned long flags;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001195 struct iwl_trans_pcie *trans_pcie =
1196 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001197
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001198 if (!trans_pcie->ict_tbl_vir)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001199 return 0;
1200
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001201 spin_lock_irqsave(&trans->shrd->lock, flags);
1202 iwl_disable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001203
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001204 memset(&trans_pcie->ict_tbl[0], 0, sizeof(u32) * ICT_COUNT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001205
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001206 val = trans_pcie->aligned_ict_tbl_dma >> PAGE_SHIFT;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001207
1208 val |= CSR_DRAM_INT_TBL_ENABLE;
1209 val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
1210
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001211 IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%X "
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001212 "aligned dma address %Lx\n",
1213 val,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001214 (unsigned long long)trans_pcie->aligned_ict_tbl_dma);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001215
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001216 iwl_write32(bus(trans), CSR_DRAM_INT_TBL_REG, val);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001217 trans_pcie->use_ict = true;
1218 trans_pcie->ict_index = 0;
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001219 iwl_write32(bus(trans), CSR_INT, trans_pcie->inta_mask);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001220 iwl_enable_interrupts(trans);
1221 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001222
1223 return 0;
1224}
1225
1226/* Device is going down disable ict interrupt usage */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001227void iwl_disable_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001228{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001229 struct iwl_trans_pcie *trans_pcie =
1230 IWL_TRANS_GET_PCIE_TRANS(trans);
1231
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001232 unsigned long flags;
1233
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001234 spin_lock_irqsave(&trans->shrd->lock, flags);
1235 trans_pcie->use_ict = false;
1236 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001237}
1238
1239static irqreturn_t iwl_isr(int irq, void *data)
1240{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001241 struct iwl_trans *trans = data;
1242 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001243 u32 inta, inta_mask;
1244 unsigned long flags;
1245#ifdef CONFIG_IWLWIFI_DEBUG
1246 u32 inta_fh;
1247#endif
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001248 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001249 return IRQ_NONE;
1250
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001251 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1252
1253 spin_lock_irqsave(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001254
1255 /* Disable (but don't clear!) interrupts here to avoid
1256 * back-to-back ISRs and sporadic interrupts from our NIC.
1257 * If we have something to service, the tasklet will re-enable ints.
1258 * If we *don't* have something, we'll re-enable before leaving here. */
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001259 inta_mask = iwl_read32(bus(trans), CSR_INT_MASK); /* just for debug */
1260 iwl_write32(bus(trans), CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001261
1262 /* Discover which interrupts are active/pending */
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001263 inta = iwl_read32(bus(trans), CSR_INT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001264
1265 /* Ignore interrupt if there's nothing in NIC to service.
1266 * This may be due to IRQ shared with another device,
1267 * or due to sporadic interrupts thrown from our NIC. */
1268 if (!inta) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001269 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001270 goto none;
1271 }
1272
1273 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1274 /* Hardware disappeared. It might have already raised
1275 * an interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001276 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001277 goto unplugged;
1278 }
1279
1280#ifdef CONFIG_IWLWIFI_DEBUG
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001281 if (iwl_get_debug_level(trans->shrd) & (IWL_DL_ISR)) {
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001282 inta_fh = iwl_read32(bus(trans), CSR_FH_INT_STATUS);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001283 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001284 "fh 0x%08x\n", inta, inta_mask, inta_fh);
1285 }
1286#endif
1287
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001288 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001289 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1290 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001291 tasklet_schedule(&trans_pcie->irq_tasklet);
1292 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1293 !trans_pcie->inta)
1294 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001295
1296 unplugged:
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001297 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001298 return IRQ_HANDLED;
1299
1300 none:
1301 /* re-enable interrupts here since we don't have anything to service. */
1302 /* only Re-enable if disabled by irq and no schedules tasklet. */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001303 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1304 !trans_pcie->inta)
1305 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001306
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001307 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001308 return IRQ_NONE;
1309}
1310
1311/* interrupt handler using ict table, with this interrupt driver will
1312 * stop using INTA register to get device's interrupt, reading this register
1313 * is expensive, device will write interrupts in ICT dram table, increment
1314 * index then will fire interrupt to driver, driver will OR all ICT table
1315 * entries from current index up to table entry with 0 value. the result is
1316 * the interrupt we need to service, driver will set the entries back to 0 and
1317 * set index.
1318 */
1319irqreturn_t iwl_isr_ict(int irq, void *data)
1320{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001321 struct iwl_trans *trans = data;
1322 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001323 u32 inta, inta_mask;
1324 u32 val = 0;
1325 unsigned long flags;
1326
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001327 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001328 return IRQ_NONE;
1329
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001330 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1331
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001332 /* dram interrupt table not set yet,
1333 * use legacy interrupt.
1334 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001335 if (!trans_pcie->use_ict)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001336 return iwl_isr(irq, data);
1337
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001338 spin_lock_irqsave(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001339
1340 /* Disable (but don't clear!) interrupts here to avoid
1341 * back-to-back ISRs and sporadic interrupts from our NIC.
1342 * If we have something to service, the tasklet will re-enable ints.
1343 * If we *don't* have something, we'll re-enable before leaving here.
1344 */
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -07001345 inta_mask = iwl_read32(bus(trans), CSR_INT_MASK); /* just for debug */
1346 iwl_write32(bus(trans), CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001347
1348
1349 /* Ignore interrupt if there's nothing in NIC to service.
1350 * This may be due to IRQ shared with another device,
1351 * or due to sporadic interrupts thrown from our NIC. */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001352 if (!trans_pcie->ict_tbl[trans_pcie->ict_index]) {
1353 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001354 goto none;
1355 }
1356
1357 /* read all entries that not 0 start with ict_index */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001358 while (trans_pcie->ict_tbl[trans_pcie->ict_index]) {
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001359
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001360 val |= le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1361 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
1362 trans_pcie->ict_index,
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001363 le32_to_cpu(
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001364 trans_pcie->ict_tbl[trans_pcie->ict_index]));
1365 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1366 trans_pcie->ict_index =
1367 iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001368
1369 }
1370
1371 /* We should not get this value, just ignore it. */
1372 if (val == 0xffffffff)
1373 val = 0;
1374
1375 /*
1376 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1377 * (bit 15 before shifting it to 31) to clear when using interrupt
1378 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1379 * so we use them to decide on the real state of the Rx bit.
1380 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1381 */
1382 if (val & 0xC0000)
1383 val |= 0x8000;
1384
1385 inta = (0xff & val) | ((0xff00 & val) << 16);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001386 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001387 inta, inta_mask, val);
1388
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001389 inta &= trans_pcie->inta_mask;
1390 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001391
1392 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1393 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001394 tasklet_schedule(&trans_pcie->irq_tasklet);
1395 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1396 !trans_pcie->inta) {
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001397 /* Allow interrupt if was disabled by this handler and
1398 * no tasklet was schedules, We should not enable interrupt,
1399 * tasklet will enable it.
1400 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001401 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001402 }
1403
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001404 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001405 return IRQ_HANDLED;
1406
1407 none:
1408 /* re-enable interrupts here since we don't have anything to service.
1409 * only Re-enable if disabled by irq.
1410 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001411 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1412 !trans_pcie->inta)
1413 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001414
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001415 spin_unlock_irqrestore(&trans->shrd->lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001416 return IRQ_NONE;
1417}