blob: 68e89be60bef2e055b40fdf0710367072ec15d8c [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"
Don Fry86551122012-02-07 14:03:55 -080037#include "iwl-wifi.h"
Emmanuel Grumbachdb70f292012-02-09 16:08:15 +020038#include "iwl-op-mode.h"
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070039
Gregory Greenmana5916972012-01-10 19:22:56 +020040#ifdef CONFIG_IWLWIFI_IDI
41#include "iwl-amfh.h"
42#endif
43
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070044/******************************************************************************
45 *
46 * RX path functions
47 *
48 ******************************************************************************/
49
50/*
51 * Rx theory of operation
52 *
53 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
54 * each of which point to Receive Buffers to be filled by the NIC. These get
55 * used not only for Rx frames, but for any command response or notification
56 * from the NIC. The driver and NIC manage the Rx buffers by means
57 * of indexes into the circular buffer.
58 *
59 * Rx Queue Indexes
60 * The host/firmware share two index registers for managing the Rx buffers.
61 *
62 * The READ index maps to the first position that the firmware may be writing
63 * to -- the driver can read up to (but not including) this position and get
64 * good data.
65 * The READ index is managed by the firmware once the card is enabled.
66 *
67 * The WRITE index maps to the last position the driver has read from -- the
68 * position preceding WRITE is the last slot the firmware can place a packet.
69 *
70 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
71 * WRITE = READ.
72 *
73 * During initialization, the host sets up the READ queue position to the first
74 * INDEX position, and WRITE to the last (READ - 1 wrapped)
75 *
76 * When the firmware places a packet in a buffer, it will advance the READ index
77 * and fire the RX interrupt. The driver can then query the READ index and
78 * process as many packets as possible, moving the WRITE index forward as it
79 * resets the Rx queue buffers with new memory.
80 *
81 * The management in the driver is as follows:
82 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
83 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
84 * to replenish the iwl->rxq->rx_free.
85 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
86 * iwl->rxq is replenished and the READ INDEX is updated (updating the
87 * 'processed' and 'read' driver indexes as well)
88 * + A received packet is processed and handed to the kernel network stack,
89 * detached from the iwl->rxq. The driver 'processed' index is updated.
90 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
91 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
92 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
93 * were enough free buffers and RX_STALLED is set it is cleared.
94 *
95 *
96 * Driver sequence:
97 *
98 * iwl_rx_queue_alloc() Allocates rx_free
99 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
100 * iwl_rx_queue_restock
101 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
102 * queue, updates firmware pointers, and updates
103 * the WRITE index. If insufficient rx_free buffers
104 * are available, schedules iwl_rx_replenish
105 *
106 * -- enable interrupts --
107 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
108 * READ INDEX, detaching the SKB from the pool.
109 * Moves the packet buffer from queue to rx_used.
110 * Calls iwl_rx_queue_restock to refill any empty
111 * slots.
112 * ...
113 *
114 */
115
116/**
117 * iwl_rx_queue_space - Return number of free slots available in queue.
118 */
119static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
120{
121 int s = q->read - q->write;
122 if (s <= 0)
123 s += RX_QUEUE_SIZE;
124 /* keep some buffer to not confuse full and empty queue */
125 s -= 2;
126 if (s < 0)
127 s = 0;
128 return s;
129}
130
131/**
132 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
133 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700134void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700135 struct iwl_rx_queue *q)
136{
137 unsigned long flags;
138 u32 reg;
139
140 spin_lock_irqsave(&q->lock, flags);
141
142 if (q->need_update == 0)
143 goto exit_unlock;
144
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700145 if (hw_params(trans).shadow_reg_enable) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700146 /* shadow register enabled */
147 /* Device expects a multiple of 8 */
148 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200149 iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700150 } else {
151 /* If power-saving is in use, make sure device is awake */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700152 if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200153 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700154
155 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700156 IWL_DEBUG_INFO(trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700157 "Rx queue requesting wakeup,"
158 " GP1 = 0x%x\n", reg);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200159 iwl_set_bit(trans, CSR_GP_CNTRL,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700160 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
161 goto exit_unlock;
162 }
163
164 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200165 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700166 q->write_actual);
167
168 /* Else device is assumed to be awake */
169 } else {
170 /* Device expects a multiple of 8 */
171 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200172 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700173 q->write_actual);
174 }
175 }
176 q->need_update = 0;
177
178 exit_unlock:
179 spin_unlock_irqrestore(&q->lock, flags);
180}
181
182/**
183 * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
184 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700185static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700186{
187 return cpu_to_le32((u32)(dma_addr >> 8));
188}
189
190/**
191 * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
192 *
193 * If there are slots in the RX queue that need to be restocked,
194 * and we have free pre-allocated buffers, fill the ranks as much
195 * as we can, pulling from rx_free.
196 *
197 * This moves the 'write' index forward to catch up with 'processed', and
198 * also updates the memory address in the firmware to reference the new
199 * target buffer.
200 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700201static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700202{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700203 struct iwl_trans_pcie *trans_pcie =
204 IWL_TRANS_GET_PCIE_TRANS(trans);
205
206 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700207 struct list_head *element;
208 struct iwl_rx_mem_buffer *rxb;
209 unsigned long flags;
210
211 spin_lock_irqsave(&rxq->lock, flags);
212 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
213 /* The overwritten rxb must be a used one */
214 rxb = rxq->queue[rxq->write];
215 BUG_ON(rxb && rxb->page);
216
217 /* Get next free Rx buffer, remove from free list */
218 element = rxq->rx_free.next;
219 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
220 list_del(element);
221
222 /* Point to Rx buffer via next RBD in circular buffer */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700223 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700224 rxq->queue[rxq->write] = rxb;
225 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
226 rxq->free_count--;
227 }
228 spin_unlock_irqrestore(&rxq->lock, flags);
229 /* If the pre-allocated buffer pool is dropping low, schedule to
230 * refill it */
231 if (rxq->free_count <= RX_LOW_WATERMARK)
Johannes Berg1ee158d2012-02-17 10:07:44 -0800232 schedule_work(&trans_pcie->rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700233
234
235 /* If we've added more space for the firmware to place data, tell it.
236 * Increment device's write pointer in multiples of 8. */
237 if (rxq->write_actual != (rxq->write & ~0x7)) {
238 spin_lock_irqsave(&rxq->lock, flags);
239 rxq->need_update = 1;
240 spin_unlock_irqrestore(&rxq->lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700241 iwl_rx_queue_update_write_ptr(trans, rxq);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700242 }
243}
244
245/**
246 * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
247 *
248 * When moving to rx_free an SKB is allocated for the slot.
249 *
250 * Also restock the Rx queue via iwl_rx_queue_restock.
251 * This is called as a scheduled work item (except for during initialization)
252 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700253static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700254{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700255 struct iwl_trans_pcie *trans_pcie =
256 IWL_TRANS_GET_PCIE_TRANS(trans);
257
258 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700259 struct list_head *element;
260 struct iwl_rx_mem_buffer *rxb;
261 struct page *page;
262 unsigned long flags;
263 gfp_t gfp_mask = priority;
264
265 while (1) {
266 spin_lock_irqsave(&rxq->lock, flags);
267 if (list_empty(&rxq->rx_used)) {
268 spin_unlock_irqrestore(&rxq->lock, flags);
269 return;
270 }
271 spin_unlock_irqrestore(&rxq->lock, flags);
272
273 if (rxq->free_count > RX_LOW_WATERMARK)
274 gfp_mask |= __GFP_NOWARN;
275
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700276 if (hw_params(trans).rx_page_order > 0)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700277 gfp_mask |= __GFP_COMP;
278
279 /* Alloc a new receive buffer */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700280 page = alloc_pages(gfp_mask,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700281 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700282 if (!page) {
283 if (net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700284 IWL_DEBUG_INFO(trans, "alloc_pages failed, "
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700285 "order: %d\n",
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700286 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700287
288 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
289 net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700290 IWL_CRIT(trans, "Failed to alloc_pages with %s."
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700291 "Only %u free buffers remaining.\n",
292 priority == GFP_ATOMIC ?
293 "GFP_ATOMIC" : "GFP_KERNEL",
294 rxq->free_count);
295 /* We don't reschedule replenish work here -- we will
296 * call the restock method and if it still needs
297 * more buffers it will schedule replenish */
298 return;
299 }
300
301 spin_lock_irqsave(&rxq->lock, flags);
302
303 if (list_empty(&rxq->rx_used)) {
304 spin_unlock_irqrestore(&rxq->lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700305 __free_pages(page, hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700306 return;
307 }
308 element = rxq->rx_used.next;
309 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
310 list_del(element);
311
312 spin_unlock_irqrestore(&rxq->lock, flags);
313
314 BUG_ON(rxb->page);
315 rxb->page = page;
316 /* Get physical address of the RB */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200317 rxb->page_dma = dma_map_page(trans->dev, page, 0,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700318 PAGE_SIZE << hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700319 DMA_FROM_DEVICE);
320 /* dma address must be no more than 36 bits */
321 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
322 /* and also 256 byte aligned! */
323 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
324
325 spin_lock_irqsave(&rxq->lock, flags);
326
327 list_add_tail(&rxb->list, &rxq->rx_free);
328 rxq->free_count++;
329
330 spin_unlock_irqrestore(&rxq->lock, flags);
331 }
332}
333
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700334void iwlagn_rx_replenish(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700335{
Johannes Berg7b114882012-02-05 13:55:11 -0800336 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700337 unsigned long flags;
338
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700339 iwlagn_rx_allocate(trans, GFP_KERNEL);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700340
Johannes Berg7b114882012-02-05 13:55:11 -0800341 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700342 iwlagn_rx_queue_restock(trans);
Johannes Berg7b114882012-02-05 13:55:11 -0800343 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700344}
345
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700346static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700347{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700348 iwlagn_rx_allocate(trans, GFP_ATOMIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700349
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700350 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700351}
352
353void iwl_bg_rx_replenish(struct work_struct *data)
354{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700355 struct iwl_trans_pcie *trans_pcie =
356 container_of(data, struct iwl_trans_pcie, rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700357
Johannes Berg1ee158d2012-02-17 10:07:44 -0800358 iwlagn_rx_replenish(trans_pcie->trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700359}
360
361/**
362 * iwl_rx_handle - Main entry function for receiving responses from uCode
363 *
364 * Uses the priv->rx_handlers callback function array to invoke
365 * the appropriate handlers, including command responses,
366 * frame-received notifications, and other notifications.
367 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700368static void iwl_rx_handle(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700369{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700370 struct iwl_trans_pcie *trans_pcie =
371 IWL_TRANS_GET_PCIE_TRANS(trans);
372 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700373 struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
374 struct iwl_device_cmd *cmd;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700375 u32 r, i;
376 int reclaim;
377 unsigned long flags;
378 u8 fill_rx = 0;
379 u32 count = 8;
380 int total_empty;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700381 int index, cmd_index;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700382
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) {
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700401 int len, err;
Emmanuel Grumbachd56da922011-09-22 07:15:36 -0700402 u16 sequence;
Johannes Berg48a2d662012-03-05 11:24:39 -0800403 struct iwl_rx_mem_buffer *rxb;
404 struct iwl_rx_cmd_buffer rxcb;
405 struct iwl_rx_packet *pkt;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700406
407 rxb = rxq->queue[i];
408
409 /* If an RXB doesn't have a Rx queue slot associated with it,
410 * then a bug has been introduced in the queue refilling
411 * routines -- catch it here */
412 if (WARN_ON(rxb == NULL)) {
413 i = (i + 1) & RX_QUEUE_MASK;
414 continue;
415 }
416
417 rxq->queue[i] = NULL;
418
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200419 dma_unmap_page(trans->dev, rxb->page_dma,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700420 PAGE_SIZE << hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700421 DMA_FROM_DEVICE);
Johannes Berg48a2d662012-03-05 11:24:39 -0800422
423 rxcb._page = rxb->page;
424 pkt = rxb_addr(&rxcb);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700425
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700426 IWL_DEBUG_RX(trans, "r = %d, i = %d, %s, 0x%02x\n", r,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700427 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
428
429 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
430 len += sizeof(u32); /* account for status word */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700431 trace_iwlwifi_dev_rx(priv(trans), pkt, len);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700432
433 /* Reclaim a command buffer only if this packet is a response
434 * to a (driver-originated) command.
435 * If the packet (e.g. Rx frame) originated from uCode,
436 * there is no command buffer to reclaim.
437 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
438 * but apparently a few don't get set; catch them here. */
439 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
440 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
441 (pkt->hdr.cmd != REPLY_RX) &&
442 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
443 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
444 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
445 (pkt->hdr.cmd != REPLY_TX);
446
Emmanuel Grumbach17a68dd2011-09-15 11:46:28 -0700447 sequence = le16_to_cpu(pkt->hdr.sequence);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700448 index = SEQ_TO_INDEX(sequence);
449 cmd_index = get_cmd_index(&txq->q, index);
450
451 if (reclaim)
452 cmd = txq->cmd[cmd_index];
453 else
454 cmd = NULL;
Emmanuel Grumbach17a68dd2011-09-15 11:46:28 -0700455
456 /* warn if this is cmd response / notification and the uCode
457 * didn't set the SEQ_RX_FRAME for a frame that is
Emmanuel Grumbachd56da922011-09-22 07:15:36 -0700458 * uCode-originated
459 * If you saw this code after the second half of 2012, then
460 * please remove it
461 */
462 WARN(pkt->hdr.cmd != REPLY_TX && reclaim == false &&
Emmanuel Grumbach17a68dd2011-09-15 11:46:28 -0700463 (!(pkt->hdr.sequence & SEQ_RX_FRAME)),
464 "reclaim is false, SEQ_RX_FRAME unset: %s\n",
465 get_cmd_string(pkt->hdr.cmd));
466
Johannes Berg48a2d662012-03-05 11:24:39 -0800467 err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700468
469 /*
Johannes Berg48a2d662012-03-05 11:24:39 -0800470 * XXX: After here, we should always check rxcb._page
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700471 * against NULL before touching it or its virtual
472 * memory (pkt). Because some rx_handler might have
473 * already taken or freed the pages.
474 */
475
476 if (reclaim) {
477 /* Invoke any callbacks, transfer the buffer to caller,
478 * and fire off the (possibly) blocking
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700479 * iwl_trans_send_cmd()
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700480 * as we reclaim the driver command queue */
Johannes Berg48a2d662012-03-05 11:24:39 -0800481 if (rxcb._page)
482 iwl_tx_cmd_complete(trans, &rxcb, err);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700483 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700484 IWL_WARN(trans, "Claim null rxb?\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700485 }
486
Johannes Berg48a2d662012-03-05 11:24:39 -0800487 /* page was stolen from us */
488 if (rxcb._page == NULL)
489 rxb->page = NULL;
490
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700491 /* Reuse the page if possible. For notification packets and
492 * SKBs that fail to Rx correctly, add them back into the
493 * rx_free list for reuse later. */
494 spin_lock_irqsave(&rxq->lock, flags);
495 if (rxb->page != NULL) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200496 rxb->page_dma = dma_map_page(trans->dev, rxb->page,
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700497 0, PAGE_SIZE <<
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700498 hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700499 DMA_FROM_DEVICE);
500 list_add_tail(&rxb->list, &rxq->rx_free);
501 rxq->free_count++;
502 } else
503 list_add_tail(&rxb->list, &rxq->rx_used);
504
505 spin_unlock_irqrestore(&rxq->lock, flags);
506
507 i = (i + 1) & RX_QUEUE_MASK;
508 /* If there are a lot of unused frames,
509 * restock the Rx queue so ucode wont assert. */
510 if (fill_rx) {
511 count++;
512 if (count >= 8) {
513 rxq->read = i;
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700514 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700515 count = 0;
516 }
517 }
518 }
519
520 /* Backtrack one entry */
521 rxq->read = i;
522 if (fill_rx)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700523 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700524 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700525 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700526}
527
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700528static const char * const desc_lookup_text[] = {
529 "OK",
530 "FAIL",
531 "BAD_PARAM",
532 "BAD_CHECKSUM",
533 "NMI_INTERRUPT_WDG",
534 "SYSASSERT",
535 "FATAL_ERROR",
536 "BAD_COMMAND",
537 "HW_ERROR_TUNE_LOCK",
538 "HW_ERROR_TEMPERATURE",
539 "ILLEGAL_CHAN_FREQ",
540 "VCC_NOT_STABLE",
541 "FH_ERROR",
542 "NMI_INTERRUPT_HOST",
543 "NMI_INTERRUPT_ACTION_PT",
544 "NMI_INTERRUPT_UNKNOWN",
545 "UCODE_VERSION_MISMATCH",
546 "HW_ERROR_ABS_LOCK",
547 "HW_ERROR_CAL_LOCK_FAIL",
548 "NMI_INTERRUPT_INST_ACTION_PT",
549 "NMI_INTERRUPT_DATA_ACTION_PT",
550 "NMI_TRM_HW_ER",
551 "NMI_INTERRUPT_TRM",
552 "NMI_INTERRUPT_BREAK_POINT",
553 "DEBUG_0",
554 "DEBUG_1",
555 "DEBUG_2",
556 "DEBUG_3",
557};
558
559static struct { char *name; u8 num; } advanced_lookup[] = {
560 { "NMI_INTERRUPT_WDG", 0x34 },
561 { "SYSASSERT", 0x35 },
562 { "UCODE_VERSION_MISMATCH", 0x37 },
563 { "BAD_COMMAND", 0x38 },
564 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
565 { "FATAL_ERROR", 0x3D },
566 { "NMI_TRM_HW_ERR", 0x46 },
567 { "NMI_INTERRUPT_TRM", 0x4C },
568 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
569 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
570 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
571 { "NMI_INTERRUPT_HOST", 0x66 },
572 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
573 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
574 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
575 { "ADVANCED_SYSASSERT", 0 },
576};
577
578static const char *desc_lookup(u32 num)
579{
580 int i;
581 int max = ARRAY_SIZE(desc_lookup_text);
582
583 if (num < max)
584 return desc_lookup_text[num];
585
586 max = ARRAY_SIZE(advanced_lookup) - 1;
587 for (i = 0; i < max; i++) {
588 if (advanced_lookup[i].num == num)
589 break;
590 }
591 return advanced_lookup[i].name;
592}
593
594#define ERROR_START_OFFSET (1 * sizeof(u32))
595#define ERROR_ELEM_SIZE (7 * sizeof(u32))
596
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700597static void iwl_dump_nic_error_log(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700598{
599 u32 base;
600 struct iwl_error_event_table table;
Don Fry86551122012-02-07 14:03:55 -0800601 struct iwl_nic *nic = nic(trans);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700602 struct iwl_trans_pcie *trans_pcie =
603 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700604
Don Fryae6130f2011-11-30 16:12:59 -0800605 base = trans->shrd->device_pointers.error_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800606 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700607 if (!base)
Don Fry86551122012-02-07 14:03:55 -0800608 base = nic->init_errlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700609 } else {
610 if (!base)
Don Fry86551122012-02-07 14:03:55 -0800611 base = nic->inst_errlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700612 }
613
614 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700615 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700616 "Not valid error log pointer 0x%08X for %s uCode\n",
617 base,
Don Fry3d6acef2011-11-28 17:05:01 -0800618 (trans->shrd->ucode_type == IWL_UCODE_INIT)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700619 ? "Init" : "RT");
620 return;
621 }
622
Don Fry86551122012-02-07 14:03:55 -0800623 iwl_read_targ_mem_words(trans, base, &table, sizeof(table));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700624
625 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700626 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
627 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
628 trans->shrd->status, table.valid);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700629 }
630
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700631 trans_pcie->isr_stats.err_code = table.error_id;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700632
Don Fry86551122012-02-07 14:03:55 -0800633 trace_iwlwifi_dev_ucode_error(priv(nic), table.error_id, table.tsf_low,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700634 table.data1, table.data2, table.line,
635 table.blink1, table.blink2, table.ilink1,
636 table.ilink2, table.bcon_time, table.gp1,
637 table.gp2, table.gp3, table.ucode_ver,
638 table.hw_ver, table.brd_ver);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700639 IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700640 desc_lookup(table.error_id));
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700641 IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
642 IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
643 IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
644 IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
645 IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
646 IWL_ERR(trans, "0x%08X | data1\n", table.data1);
647 IWL_ERR(trans, "0x%08X | data2\n", table.data2);
648 IWL_ERR(trans, "0x%08X | line\n", table.line);
649 IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
650 IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
651 IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
652 IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
653 IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
654 IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
655 IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
656 IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
657 IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
658 IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
Wey-Yi Guyd332f592011-11-30 12:32:42 -0800659
660 IWL_ERR(trans, "0x%08X | isr0\n", table.isr0);
661 IWL_ERR(trans, "0x%08X | isr1\n", table.isr1);
662 IWL_ERR(trans, "0x%08X | isr2\n", table.isr2);
663 IWL_ERR(trans, "0x%08X | isr3\n", table.isr3);
664 IWL_ERR(trans, "0x%08X | isr4\n", table.isr4);
665 IWL_ERR(trans, "0x%08X | isr_pref\n", table.isr_pref);
666 IWL_ERR(trans, "0x%08X | wait_event\n", table.wait_event);
667 IWL_ERR(trans, "0x%08X | l2p_control\n", table.l2p_control);
668 IWL_ERR(trans, "0x%08X | l2p_duration\n", table.l2p_duration);
669 IWL_ERR(trans, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
670 IWL_ERR(trans, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
671 IWL_ERR(trans, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
672 IWL_ERR(trans, "0x%08X | timestamp\n", table.u_timestamp);
673 IWL_ERR(trans, "0x%08X | flow_handler\n", table.flow_handler);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700674}
675
676/**
677 * iwl_irq_handle_error - called for HW or SW error interrupt from card
678 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700679static void iwl_irq_handle_error(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700680{
681 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
Emmanuel Grumbachff6e75c2012-02-12 15:21:08 +0200682 if (cfg(trans)->internal_wimax_coex &&
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200683 (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700684 APMS_CLK_VAL_MRB_FUNC_MODE) ||
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200685 (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700686 APMG_PS_CTRL_VAL_RESET_REQ))) {
687 /*
688 * Keep the restart process from trying to send host
689 * commands by clearing the ready bit.
690 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700691 clear_bit(STATUS_READY, &trans->shrd->status);
692 clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
Emmanuel Grumbachff6e75c2012-02-12 15:21:08 +0200693 wake_up(&trans->shrd->wait_command_queue);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700694 IWL_ERR(trans, "RF is used by WiMAX\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700695 return;
696 }
697
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700698 IWL_ERR(trans, "Loaded firmware version: %s\n",
Emmanuel Grumbachff6e75c2012-02-12 15:21:08 +0200699 nic(trans)->fw.fw_version);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700700
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700701 iwl_dump_nic_error_log(trans);
702 iwl_dump_csr(trans);
703 iwl_dump_fh(trans, NULL, false);
704 iwl_dump_nic_event_log(trans, false, NULL, false);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700705
Emmanuel Grumbachbcb93212012-02-09 16:08:15 +0200706 iwl_op_mode_nic_error(trans->op_mode);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700707}
708
709#define EVENT_START_OFFSET (4 * sizeof(u32))
710
711/**
712 * iwl_print_event_log - Dump error event log to syslog
713 *
714 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700715static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700716 u32 num_events, u32 mode,
717 int pos, char **buf, size_t bufsz)
718{
719 u32 i;
720 u32 base; /* SRAM byte address of event log header */
721 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
722 u32 ptr; /* SRAM byte address of log data */
723 u32 ev, time, data; /* event log data */
724 unsigned long reg_flags;
Don Fry86551122012-02-07 14:03:55 -0800725 struct iwl_nic *nic = nic(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700726
727 if (num_events == 0)
728 return pos;
729
Don Fryae6130f2011-11-30 16:12:59 -0800730 base = trans->shrd->device_pointers.log_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800731 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700732 if (!base)
Don Fry86551122012-02-07 14:03:55 -0800733 base = nic->init_evtlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700734 } else {
735 if (!base)
Don Fry86551122012-02-07 14:03:55 -0800736 base = nic->inst_evtlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700737 }
738
739 if (mode == 0)
740 event_size = 2 * sizeof(u32);
741 else
742 event_size = 3 * sizeof(u32);
743
744 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
745
746 /* Make sure device is powered up for SRAM reads */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200747 spin_lock_irqsave(&trans->reg_lock, reg_flags);
748 iwl_grab_nic_access(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700749
750 /* Set starting address; reads will auto-increment */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200751 iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700752 rmb();
753
754 /* "time" is actually "data" for mode 0 (no timestamp).
755 * place event id # at far right for easier visual parsing. */
756 for (i = 0; i < num_events; i++) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200757 ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
758 time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700759 if (mode == 0) {
760 /* data, ev */
761 if (bufsz) {
762 pos += scnprintf(*buf + pos, bufsz - pos,
763 "EVT_LOG:0x%08x:%04u\n",
764 time, ev);
765 } else {
Don Fry86551122012-02-07 14:03:55 -0800766 trace_iwlwifi_dev_ucode_event(priv(trans), 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700767 time, ev);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700768 IWL_ERR(trans, "EVT_LOG:0x%08x:%04u\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700769 time, ev);
770 }
771 } else {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200772 data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700773 if (bufsz) {
774 pos += scnprintf(*buf + pos, bufsz - pos,
775 "EVT_LOGT:%010u:0x%08x:%04u\n",
776 time, data, ev);
777 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700778 IWL_ERR(trans, "EVT_LOGT:%010u:0x%08x:%04u\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700779 time, data, ev);
Don Fry86551122012-02-07 14:03:55 -0800780 trace_iwlwifi_dev_ucode_event(priv(trans), time,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700781 data, ev);
782 }
783 }
784 }
785
786 /* Allow device to power down */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200787 iwl_release_nic_access(trans);
788 spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700789 return pos;
790}
791
792/**
793 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
794 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700795static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700796 u32 num_wraps, u32 next_entry,
797 u32 size, u32 mode,
798 int pos, char **buf, size_t bufsz)
799{
800 /*
801 * display the newest DEFAULT_LOG_ENTRIES entries
802 * i.e the entries just before the next ont that uCode would fill.
803 */
804 if (num_wraps) {
805 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700806 pos = iwl_print_event_log(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700807 capacity - (size - next_entry),
808 size - next_entry, mode,
809 pos, buf, bufsz);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700810 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700811 next_entry, mode,
812 pos, buf, bufsz);
813 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700814 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700815 size, mode, pos, buf, bufsz);
816 } else {
817 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700818 pos = iwl_print_event_log(trans, 0, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700819 mode, pos, buf, bufsz);
820 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700821 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700822 size, mode, pos, buf, bufsz);
823 }
824 }
825 return pos;
826}
827
828#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
829
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700830int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700831 char **buf, bool display)
832{
833 u32 base; /* SRAM byte address of event log header */
834 u32 capacity; /* event log capacity in # entries */
835 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
836 u32 num_wraps; /* # times uCode wrapped to top of log */
837 u32 next_entry; /* index of next entry to be written by uCode */
838 u32 size; /* # entries that we'll print */
839 u32 logsize;
840 int pos = 0;
841 size_t bufsz = 0;
Don Fry86551122012-02-07 14:03:55 -0800842 struct iwl_nic *nic = nic(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700843
Don Fryae6130f2011-11-30 16:12:59 -0800844 base = trans->shrd->device_pointers.log_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800845 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Don Fry86551122012-02-07 14:03:55 -0800846 logsize = nic->init_evtlog_size;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700847 if (!base)
Don Fry86551122012-02-07 14:03:55 -0800848 base = nic->init_evtlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700849 } else {
Don Fry86551122012-02-07 14:03:55 -0800850 logsize = nic->inst_evtlog_size;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700851 if (!base)
Don Fry86551122012-02-07 14:03:55 -0800852 base = nic->inst_evtlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700853 }
854
855 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700856 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700857 "Invalid event log pointer 0x%08X for %s uCode\n",
858 base,
Don Fry3d6acef2011-11-28 17:05:01 -0800859 (trans->shrd->ucode_type == IWL_UCODE_INIT)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700860 ? "Init" : "RT");
861 return -EINVAL;
862 }
863
864 /* event log header */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200865 capacity = iwl_read_targ_mem(trans, base);
866 mode = iwl_read_targ_mem(trans, base + (1 * sizeof(u32)));
867 num_wraps = iwl_read_targ_mem(trans, base + (2 * sizeof(u32)));
868 next_entry = iwl_read_targ_mem(trans, base + (3 * sizeof(u32)));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700869
870 if (capacity > logsize) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700871 IWL_ERR(trans, "Log capacity %d is bogus, limit to %d "
872 "entries\n", capacity, logsize);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700873 capacity = logsize;
874 }
875
876 if (next_entry > logsize) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700877 IWL_ERR(trans, "Log write index %d is bogus, limit to %d\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700878 next_entry, logsize);
879 next_entry = logsize;
880 }
881
882 size = num_wraps ? capacity : next_entry;
883
884 /* bail out if nothing in log */
885 if (size == 0) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700886 IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700887 return pos;
888 }
889
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700890#ifdef CONFIG_IWLWIFI_DEBUG
Johannes Berga8bceb32012-03-05 11:24:30 -0800891 if (!(iwl_have_debug_level(IWL_DL_FW_ERRORS)) && !full_log)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700892 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
893 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
894#else
895 size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
896 ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
897#endif
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700898 IWL_ERR(trans, "Start IWL Event Log Dump: display last %u entries\n",
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700899 size);
900
901#ifdef CONFIG_IWLWIFI_DEBUG
902 if (display) {
903 if (full_log)
904 bufsz = capacity * 48;
905 else
906 bufsz = size * 48;
907 *buf = kmalloc(bufsz, GFP_KERNEL);
908 if (!*buf)
909 return -ENOMEM;
910 }
Johannes Berga8bceb32012-03-05 11:24:30 -0800911 if (iwl_have_debug_level(IWL_DL_FW_ERRORS) || full_log) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700912 /*
913 * if uCode has wrapped back to top of log,
914 * start at the oldest entry,
915 * i.e the next one that uCode would fill.
916 */
917 if (num_wraps)
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700918 pos = iwl_print_event_log(trans, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700919 capacity - next_entry, mode,
920 pos, buf, bufsz);
921 /* (then/else) start at top of log */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700922 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700923 next_entry, mode, pos, buf, bufsz);
924 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700925 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700926 next_entry, size, mode,
927 pos, buf, bufsz);
928#else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700929 pos = iwl_print_last_event_logs(trans, capacity, num_wraps,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700930 next_entry, size, mode,
931 pos, buf, bufsz);
932#endif
933 return pos;
934}
935
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700936/* tasklet for iwlagn interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700937void iwl_irq_tasklet(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700938{
939 u32 inta = 0;
940 u32 handled = 0;
941 unsigned long flags;
942 u32 i;
943#ifdef CONFIG_IWLWIFI_DEBUG
944 u32 inta_mask;
945#endif
946
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -0700947 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700948 struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
949
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700950
Johannes Berg7b114882012-02-05 13:55:11 -0800951 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700952
953 /* Ack/clear/reset pending uCode interrupts.
954 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
955 */
956 /* There is a hardware bug in the interrupt mask function that some
957 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
958 * they are disabled in the CSR_INT_MASK register. Furthermore the
959 * ICT interrupt handling mechanism has another bug that might cause
960 * these unmasked interrupts fail to be detected. We workaround the
961 * hardware bugs here by ACKing all the possible interrupts so that
962 * interrupt coalescing can still be achieved.
963 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200964 iwl_write32(trans, CSR_INT,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700965 trans_pcie->inta | ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700966
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700967 inta = trans_pcie->inta;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700968
969#ifdef CONFIG_IWLWIFI_DEBUG
Johannes Berga8bceb32012-03-05 11:24:30 -0800970 if (iwl_have_debug_level(IWL_DL_ISR)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700971 /* just for debug */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200972 inta_mask = iwl_read32(trans, CSR_INT_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700973 IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n ",
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700974 inta, inta_mask);
975 }
976#endif
977
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700978 /* saved interrupt in inta variable now we can reset trans_pcie->inta */
979 trans_pcie->inta = 0;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700980
Johannes Berg7b114882012-02-05 13:55:11 -0800981 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Johannes Bergb49ba042012-01-19 08:20:57 -0800982
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700983 /* Now service all interrupt bits discovered above. */
984 if (inta & CSR_INT_BIT_HW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700985 IWL_ERR(trans, "Hardware error detected. Restarting.\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700986
987 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -0700988 iwl_disable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700989
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700990 isr_stats->hw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700991 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700992
993 handled |= CSR_INT_BIT_HW_ERR;
994
995 return;
996 }
997
998#ifdef CONFIG_IWLWIFI_DEBUG
Johannes Berga8bceb32012-03-05 11:24:30 -0800999 if (iwl_have_debug_level(IWL_DL_ISR)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001000 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1001 if (inta & CSR_INT_BIT_SCD) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001002 IWL_DEBUG_ISR(trans, "Scheduler finished to transmit "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001003 "the frame/frames.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001004 isr_stats->sch++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001005 }
1006
1007 /* Alive notification via Rx interrupt will do the real work */
1008 if (inta & CSR_INT_BIT_ALIVE) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001009 IWL_DEBUG_ISR(trans, "Alive interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001010 isr_stats->alive++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001011 }
1012 }
1013#endif
1014 /* Safely ignore these bits for debug checks below */
1015 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1016
1017 /* HW RF KILL switch toggled */
1018 if (inta & CSR_INT_BIT_RF_KILL) {
1019 int hw_rf_kill = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001020 if (!(iwl_read32(trans, CSR_GP_CNTRL) &
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001021 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1022 hw_rf_kill = 1;
1023
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001024 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001025 hw_rf_kill ? "disable radio" : "enable radio");
1026
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001027 isr_stats->rfkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001028
1029 /* driver only loads ucode once setting the interface up.
1030 * the driver allows loading the ucode even if the radio
1031 * is killed. Hence update the killswitch state here. The
1032 * rfkill handler will care about restarting if needed.
1033 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001034 if (!test_bit(STATUS_ALIVE, &trans->shrd->status)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001035 if (hw_rf_kill)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001036 set_bit(STATUS_RF_KILL_HW,
1037 &trans->shrd->status);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001038 else
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -07001039 clear_bit(STATUS_RF_KILL_HW,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001040 &trans->shrd->status);
Emmanuel Grumbach7120d982012-02-09 16:08:15 +02001041 iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rf_kill);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001042 }
1043
1044 handled |= CSR_INT_BIT_RF_KILL;
1045 }
1046
1047 /* Chip got too hot and stopped itself */
1048 if (inta & CSR_INT_BIT_CT_KILL) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001049 IWL_ERR(trans, "Microcode CT kill error detected.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001050 isr_stats->ctkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001051 handled |= CSR_INT_BIT_CT_KILL;
1052 }
1053
1054 /* Error detected by uCode */
1055 if (inta & CSR_INT_BIT_SW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001056 IWL_ERR(trans, "Microcode SW error detected. "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001057 " Restarting 0x%X.\n", inta);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001058 isr_stats->sw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -07001059 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001060 handled |= CSR_INT_BIT_SW_ERR;
1061 }
1062
1063 /* uCode wakes up after power-down sleep */
1064 if (inta & CSR_INT_BIT_WAKEUP) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001065 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1066 iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
1067 for (i = 0; i < hw_params(trans).max_txq_num; i++)
Emmanuel Grumbachfd656932011-08-25 23:11:19 -07001068 iwl_txq_update_write_ptr(trans,
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001069 &trans_pcie->txq[i]);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001070
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001071 isr_stats->wakeup++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001072
1073 handled |= CSR_INT_BIT_WAKEUP;
1074 }
1075
1076 /* All uCode command responses, including Tx command responses,
1077 * Rx "responses" (frame-received notification), and other
1078 * notifications from uCode come through here*/
1079 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1080 CSR_INT_BIT_RX_PERIODIC)) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001081 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001082 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1083 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001084 iwl_write32(trans, CSR_FH_INT_STATUS,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001085 CSR_FH_INT_RX_MASK);
1086 }
1087 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1088 handled |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001089 iwl_write32(trans,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001090 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001091 }
1092 /* Sending RX interrupt require many steps to be done in the
1093 * the device:
1094 * 1- write interrupt to current index in ICT table.
1095 * 2- dma RX frame.
1096 * 3- update RX shared data to indicate last write index.
1097 * 4- send interrupt.
1098 * This could lead to RX race, driver could receive RX interrupt
1099 * but the shared data changes does not reflect this;
1100 * periodic interrupt will detect any dangling Rx activity.
1101 */
1102
1103 /* Disable periodic interrupt; we use it as just a one-shot. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001104 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001105 CSR_INT_PERIODIC_DIS);
Gregory Greenmana5916972012-01-10 19:22:56 +02001106#ifdef CONFIG_IWLWIFI_IDI
1107 iwl_amfh_rx_handler();
1108#else
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001109 iwl_rx_handle(trans);
Gregory Greenmana5916972012-01-10 19:22:56 +02001110#endif
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001111 /*
1112 * Enable periodic interrupt in 8 msec only if we received
1113 * real RX interrupt (instead of just periodic int), to catch
1114 * any dangling Rx interrupt. If it was just the periodic
1115 * interrupt, there was no dangling Rx activity, and no need
1116 * to extend the periodic interrupt; one-shot is enough.
1117 */
1118 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001119 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001120 CSR_INT_PERIODIC_ENA);
1121
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001122 isr_stats->rx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001123 }
1124
1125 /* This "Tx" DMA channel is used only for loading uCode */
1126 if (inta & CSR_INT_BIT_FH_TX) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001127 iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001128 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001129 isr_stats->tx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001130 handled |= CSR_INT_BIT_FH_TX;
1131 /* Wake up uCode load routine, now that load is complete */
Don Fry5703ddb2011-11-10 06:55:07 -08001132 trans->ucode_write_complete = 1;
Johannes Bergeffd4d92011-09-15 11:46:52 -07001133 wake_up(&trans->shrd->wait_command_queue);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001134 }
1135
1136 if (inta & ~handled) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001137 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001138 isr_stats->unhandled++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001139 }
1140
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001141 if (inta & ~(trans_pcie->inta_mask)) {
1142 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1143 inta & ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001144 }
1145
1146 /* Re-enable all interrupts */
1147 /* only Re-enable if disabled by irq */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001148 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status))
1149 iwl_enable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001150 /* Re-enable RF_KILL if it occurred */
Emmanuel Grumbach1df06bd2012-01-09 16:35:08 +02001151 else if (handled & CSR_INT_BIT_RF_KILL) {
1152 IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
1153 iwl_write32(trans, CSR_INT_MASK, CSR_INT_BIT_RF_KILL);
1154 }
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001155}
1156
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001157/******************************************************************************
1158 *
1159 * ICT functions
1160 *
1161 ******************************************************************************/
Johannes Berg10667132011-12-19 14:00:59 -08001162
1163/* a device (PCI-E) page is 4096 bytes long */
1164#define ICT_SHIFT 12
1165#define ICT_SIZE (1 << ICT_SHIFT)
1166#define ICT_COUNT (ICT_SIZE / sizeof(u32))
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001167
1168/* Free dram table */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001169void iwl_free_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001170{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001171 struct iwl_trans_pcie *trans_pcie =
1172 IWL_TRANS_GET_PCIE_TRANS(trans);
1173
Johannes Berg10667132011-12-19 14:00:59 -08001174 if (trans_pcie->ict_tbl) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001175 dma_free_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001176 trans_pcie->ict_tbl,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001177 trans_pcie->ict_tbl_dma);
Johannes Berg10667132011-12-19 14:00:59 -08001178 trans_pcie->ict_tbl = NULL;
1179 trans_pcie->ict_tbl_dma = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001180 }
1181}
1182
1183
Johannes Berg10667132011-12-19 14:00:59 -08001184/*
1185 * allocate dram shared table, it is an aligned memory
1186 * block of ICT_SIZE.
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001187 * also reset all data related to ICT table interrupt.
1188 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001189int iwl_alloc_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001190{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001191 struct iwl_trans_pcie *trans_pcie =
1192 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001193
Johannes Berg10667132011-12-19 14:00:59 -08001194 trans_pcie->ict_tbl =
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001195 dma_alloc_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001196 &trans_pcie->ict_tbl_dma,
1197 GFP_KERNEL);
1198 if (!trans_pcie->ict_tbl)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001199 return -ENOMEM;
1200
Johannes Berg10667132011-12-19 14:00:59 -08001201 /* just an API sanity check ... it is guaranteed to be aligned */
1202 if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
1203 iwl_free_isr_ict(trans);
1204 return -EINVAL;
1205 }
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001206
Johannes Berg10667132011-12-19 14:00:59 -08001207 IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
1208 (unsigned long long)trans_pcie->ict_tbl_dma);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001209
Johannes Berg10667132011-12-19 14:00:59 -08001210 IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001211
1212 /* reset table and index to all 0 */
Johannes Berg10667132011-12-19 14:00:59 -08001213 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001214 trans_pcie->ict_index = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001215
1216 /* add periodic RX interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001217 trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001218 return 0;
1219}
1220
1221/* Device is going up inform it about using ICT interrupt table,
1222 * also we need to tell the driver to start using ICT interrupt.
1223 */
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001224void iwl_reset_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001225{
1226 u32 val;
1227 unsigned long flags;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001228 struct iwl_trans_pcie *trans_pcie =
1229 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001230
Johannes Berg10667132011-12-19 14:00:59 -08001231 if (!trans_pcie->ict_tbl)
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001232 return;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001233
Johannes Berg7b114882012-02-05 13:55:11 -08001234 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001235 iwl_disable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001236
Johannes Berg10667132011-12-19 14:00:59 -08001237 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001238
Johannes Berg10667132011-12-19 14:00:59 -08001239 val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001240
1241 val |= CSR_DRAM_INT_TBL_ENABLE;
1242 val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
1243
Johannes Berg10667132011-12-19 14:00:59 -08001244 IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001245
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001246 iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001247 trans_pcie->use_ict = true;
1248 trans_pcie->ict_index = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001249 iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001250 iwl_enable_interrupts(trans);
Johannes Berg7b114882012-02-05 13:55:11 -08001251 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001252}
1253
1254/* Device is going down disable ict interrupt usage */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001255void iwl_disable_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001256{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001257 struct iwl_trans_pcie *trans_pcie =
1258 IWL_TRANS_GET_PCIE_TRANS(trans);
1259
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001260 unsigned long flags;
1261
Johannes Berg7b114882012-02-05 13:55:11 -08001262 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001263 trans_pcie->use_ict = false;
Johannes Berg7b114882012-02-05 13:55:11 -08001264 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001265}
1266
1267static irqreturn_t iwl_isr(int irq, void *data)
1268{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001269 struct iwl_trans *trans = data;
1270 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001271 u32 inta, inta_mask;
1272 unsigned long flags;
1273#ifdef CONFIG_IWLWIFI_DEBUG
1274 u32 inta_fh;
1275#endif
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001276 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001277 return IRQ_NONE;
1278
Johannes Bergb80667e2011-12-09 07:26:13 -08001279 trace_iwlwifi_dev_irq(priv(trans));
1280
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001281 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1282
Johannes Berg7b114882012-02-05 13:55:11 -08001283 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001284
1285 /* Disable (but don't clear!) interrupts here to avoid
1286 * back-to-back ISRs and sporadic interrupts from our NIC.
1287 * If we have something to service, the tasklet will re-enable ints.
1288 * If we *don't* have something, we'll re-enable before leaving here. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001289 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1290 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001291
1292 /* Discover which interrupts are active/pending */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001293 inta = iwl_read32(trans, CSR_INT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001294
1295 /* Ignore interrupt if there's nothing in NIC to service.
1296 * This may be due to IRQ shared with another device,
1297 * or due to sporadic interrupts thrown from our NIC. */
1298 if (!inta) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001299 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001300 goto none;
1301 }
1302
1303 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1304 /* Hardware disappeared. It might have already raised
1305 * an interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001306 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001307 goto unplugged;
1308 }
1309
1310#ifdef CONFIG_IWLWIFI_DEBUG
Johannes Berga8bceb32012-03-05 11:24:30 -08001311 if (iwl_have_debug_level(IWL_DL_ISR)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001312 inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001313 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001314 "fh 0x%08x\n", inta, inta_mask, inta_fh);
1315 }
1316#endif
1317
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001318 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001319 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1320 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001321 tasklet_schedule(&trans_pcie->irq_tasklet);
1322 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1323 !trans_pcie->inta)
1324 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001325
1326 unplugged:
Johannes Berg7b114882012-02-05 13:55:11 -08001327 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001328 return IRQ_HANDLED;
1329
1330 none:
1331 /* re-enable interrupts here since we don't have anything to service. */
1332 /* only Re-enable if disabled by irq and no schedules tasklet. */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001333 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
1334 !trans_pcie->inta)
1335 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001336
Johannes Berg7b114882012-02-05 13:55:11 -08001337 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001338 return IRQ_NONE;
1339}
1340
1341/* interrupt handler using ict table, with this interrupt driver will
1342 * stop using INTA register to get device's interrupt, reading this register
1343 * is expensive, device will write interrupts in ICT dram table, increment
1344 * index then will fire interrupt to driver, driver will OR all ICT table
1345 * entries from current index up to table entry with 0 value. the result is
1346 * the interrupt we need to service, driver will set the entries back to 0 and
1347 * set index.
1348 */
1349irqreturn_t iwl_isr_ict(int irq, void *data)
1350{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001351 struct iwl_trans *trans = data;
1352 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001353 u32 inta, inta_mask;
1354 u32 val = 0;
Johannes Bergb80667e2011-12-09 07:26:13 -08001355 u32 read;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001356 unsigned long flags;
1357
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001358 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001359 return IRQ_NONE;
1360
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001361 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1362
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001363 /* dram interrupt table not set yet,
1364 * use legacy interrupt.
1365 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001366 if (!trans_pcie->use_ict)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001367 return iwl_isr(irq, data);
1368
Johannes Bergb80667e2011-12-09 07:26:13 -08001369 trace_iwlwifi_dev_irq(priv(trans));
1370
Johannes Berg7b114882012-02-05 13:55:11 -08001371 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001372
1373 /* Disable (but don't clear!) interrupts here to avoid
1374 * back-to-back ISRs and sporadic interrupts from our NIC.
1375 * If we have something to service, the tasklet will re-enable ints.
1376 * If we *don't* have something, we'll re-enable before leaving here.
1377 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001378 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1379 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001380
1381
1382 /* Ignore interrupt if there's nothing in NIC to service.
1383 * This may be due to IRQ shared with another device,
1384 * or due to sporadic interrupts thrown from our NIC. */
Johannes Bergb80667e2011-12-09 07:26:13 -08001385 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1386 trace_iwlwifi_dev_ict_read(priv(trans), trans_pcie->ict_index, read);
1387 if (!read) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001388 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001389 goto none;
1390 }
1391
Johannes Bergb80667e2011-12-09 07:26:13 -08001392 /*
1393 * Collect all entries up to the first 0, starting from ict_index;
1394 * note we already read at ict_index.
1395 */
1396 do {
1397 val |= read;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001398 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
Johannes Bergb80667e2011-12-09 07:26:13 -08001399 trans_pcie->ict_index, read);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001400 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1401 trans_pcie->ict_index =
1402 iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001403
Johannes Bergb80667e2011-12-09 07:26:13 -08001404 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1405 trace_iwlwifi_dev_ict_read(priv(trans), trans_pcie->ict_index,
1406 read);
1407 } while (read);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001408
1409 /* We should not get this value, just ignore it. */
1410 if (val == 0xffffffff)
1411 val = 0;
1412
1413 /*
1414 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1415 * (bit 15 before shifting it to 31) to clear when using interrupt
1416 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1417 * so we use them to decide on the real state of the Rx bit.
1418 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1419 */
1420 if (val & 0xC0000)
1421 val |= 0x8000;
1422
1423 inta = (0xff & val) | ((0xff00 & val) << 16);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001424 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001425 inta, inta_mask, val);
1426
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001427 inta &= trans_pcie->inta_mask;
1428 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001429
1430 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1431 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001432 tasklet_schedule(&trans_pcie->irq_tasklet);
1433 else if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
Johannes Bergb80667e2011-12-09 07:26:13 -08001434 !trans_pcie->inta) {
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001435 /* Allow interrupt if was disabled by this handler and
1436 * no tasklet was schedules, We should not enable interrupt,
1437 * tasklet will enable it.
1438 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001439 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001440 }
1441
Johannes Berg7b114882012-02-05 13:55:11 -08001442 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001443 return IRQ_HANDLED;
1444
1445 none:
1446 /* re-enable interrupts here since we don't have anything to service.
1447 * only Re-enable if disabled by irq.
1448 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001449 if (test_bit(STATUS_INT_ENABLED, &trans->shrd->status) &&
Johannes Bergb80667e2011-12-09 07:26:13 -08001450 !trans_pcie->inta)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001451 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001452
Johannes Berg7b114882012-02-05 13:55:11 -08001453 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001454 return IRQ_NONE;
1455}