blob: a5547a155b9579aa2aac9be28f39808139cc4522 [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
Johannes Berg1b29dc92012-03-06 13:30:50 -080033#include "iwl-prph.h"
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070034#include "iwl-io.h"
Johannes Bergc17d0682011-09-15 11:46:42 -070035#include "iwl-trans-pcie-int.h"
Emmanuel Grumbachdb70f292012-02-09 16:08:15 +020036#include "iwl-op-mode.h"
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070037
Gregory Greenmana5916972012-01-10 19:22:56 +020038#ifdef CONFIG_IWLWIFI_IDI
39#include "iwl-amfh.h"
40#endif
41
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070042/******************************************************************************
43 *
44 * RX path functions
45 *
46 ******************************************************************************/
47
48/*
49 * Rx theory of operation
50 *
51 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
52 * each of which point to Receive Buffers to be filled by the NIC. These get
53 * used not only for Rx frames, but for any command response or notification
54 * from the NIC. The driver and NIC manage the Rx buffers by means
55 * of indexes into the circular buffer.
56 *
57 * Rx Queue Indexes
58 * The host/firmware share two index registers for managing the Rx buffers.
59 *
60 * The READ index maps to the first position that the firmware may be writing
61 * to -- the driver can read up to (but not including) this position and get
62 * good data.
63 * The READ index is managed by the firmware once the card is enabled.
64 *
65 * The WRITE index maps to the last position the driver has read from -- the
66 * position preceding WRITE is the last slot the firmware can place a packet.
67 *
68 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
69 * WRITE = READ.
70 *
71 * During initialization, the host sets up the READ queue position to the first
72 * INDEX position, and WRITE to the last (READ - 1 wrapped)
73 *
74 * When the firmware places a packet in a buffer, it will advance the READ index
75 * and fire the RX interrupt. The driver can then query the READ index and
76 * process as many packets as possible, moving the WRITE index forward as it
77 * resets the Rx queue buffers with new memory.
78 *
79 * The management in the driver is as follows:
80 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
81 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
82 * to replenish the iwl->rxq->rx_free.
83 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
84 * iwl->rxq is replenished and the READ INDEX is updated (updating the
85 * 'processed' and 'read' driver indexes as well)
86 * + A received packet is processed and handed to the kernel network stack,
87 * detached from the iwl->rxq. The driver 'processed' index is updated.
88 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
89 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
90 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
91 * were enough free buffers and RX_STALLED is set it is cleared.
92 *
93 *
94 * Driver sequence:
95 *
96 * iwl_rx_queue_alloc() Allocates rx_free
97 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
98 * iwl_rx_queue_restock
99 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
100 * queue, updates firmware pointers, and updates
101 * the WRITE index. If insufficient rx_free buffers
102 * are available, schedules iwl_rx_replenish
103 *
104 * -- enable interrupts --
105 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
106 * READ INDEX, detaching the SKB from the pool.
107 * Moves the packet buffer from queue to rx_used.
108 * Calls iwl_rx_queue_restock to refill any empty
109 * slots.
110 * ...
111 *
112 */
113
114/**
115 * iwl_rx_queue_space - Return number of free slots available in queue.
116 */
117static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
118{
119 int s = q->read - q->write;
120 if (s <= 0)
121 s += RX_QUEUE_SIZE;
122 /* keep some buffer to not confuse full and empty queue */
123 s -= 2;
124 if (s < 0)
125 s = 0;
126 return s;
127}
128
129/**
130 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
131 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700132void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700133 struct iwl_rx_queue *q)
134{
135 unsigned long flags;
136 u32 reg;
137
138 spin_lock_irqsave(&q->lock, flags);
139
140 if (q->need_update == 0)
141 goto exit_unlock;
142
Johannes Berg0dde86b2012-03-06 13:30:46 -0800143 if (cfg(trans)->base_params->shadow_reg_enable) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700144 /* shadow register enabled */
145 /* Device expects a multiple of 8 */
146 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200147 iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, q->write_actual);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700148 } else {
149 /* If power-saving is in use, make sure device is awake */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700150 if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200151 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700152
153 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700154 IWL_DEBUG_INFO(trans,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700155 "Rx queue requesting wakeup,"
156 " GP1 = 0x%x\n", reg);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200157 iwl_set_bit(trans, CSR_GP_CNTRL,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700158 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
159 goto exit_unlock;
160 }
161
162 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200163 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700164 q->write_actual);
165
166 /* Else device is assumed to be awake */
167 } else {
168 /* Device expects a multiple of 8 */
169 q->write_actual = (q->write & ~0x7);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200170 iwl_write_direct32(trans, FH_RSCSR_CHNL0_WPTR,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700171 q->write_actual);
172 }
173 }
174 q->need_update = 0;
175
176 exit_unlock:
177 spin_unlock_irqrestore(&q->lock, flags);
178}
179
180/**
181 * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
182 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700183static inline __le32 iwlagn_dma_addr2rbd_ptr(dma_addr_t dma_addr)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700184{
185 return cpu_to_le32((u32)(dma_addr >> 8));
186}
187
188/**
189 * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
190 *
191 * If there are slots in the RX queue that need to be restocked,
192 * and we have free pre-allocated buffers, fill the ranks as much
193 * as we can, pulling from rx_free.
194 *
195 * This moves the 'write' index forward to catch up with 'processed', and
196 * also updates the memory address in the firmware to reference the new
197 * target buffer.
198 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700199static void iwlagn_rx_queue_restock(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700200{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700201 struct iwl_trans_pcie *trans_pcie =
202 IWL_TRANS_GET_PCIE_TRANS(trans);
203
204 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700205 struct list_head *element;
206 struct iwl_rx_mem_buffer *rxb;
207 unsigned long flags;
208
209 spin_lock_irqsave(&rxq->lock, flags);
210 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
211 /* The overwritten rxb must be a used one */
212 rxb = rxq->queue[rxq->write];
213 BUG_ON(rxb && rxb->page);
214
215 /* Get next free Rx buffer, remove from free list */
216 element = rxq->rx_free.next;
217 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
218 list_del(element);
219
220 /* Point to Rx buffer via next RBD in circular buffer */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700221 rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(rxb->page_dma);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700222 rxq->queue[rxq->write] = rxb;
223 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
224 rxq->free_count--;
225 }
226 spin_unlock_irqrestore(&rxq->lock, flags);
227 /* If the pre-allocated buffer pool is dropping low, schedule to
228 * refill it */
229 if (rxq->free_count <= RX_LOW_WATERMARK)
Johannes Berg1ee158d2012-02-17 10:07:44 -0800230 schedule_work(&trans_pcie->rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700231
232
233 /* If we've added more space for the firmware to place data, tell it.
234 * Increment device's write pointer in multiples of 8. */
235 if (rxq->write_actual != (rxq->write & ~0x7)) {
236 spin_lock_irqsave(&rxq->lock, flags);
237 rxq->need_update = 1;
238 spin_unlock_irqrestore(&rxq->lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700239 iwl_rx_queue_update_write_ptr(trans, rxq);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700240 }
241}
242
243/**
244 * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
245 *
246 * When moving to rx_free an SKB is allocated for the slot.
247 *
248 * Also restock the Rx queue via iwl_rx_queue_restock.
249 * This is called as a scheduled work item (except for during initialization)
250 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700251static void iwlagn_rx_allocate(struct iwl_trans *trans, gfp_t priority)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700252{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700253 struct iwl_trans_pcie *trans_pcie =
254 IWL_TRANS_GET_PCIE_TRANS(trans);
255
256 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700257 struct list_head *element;
258 struct iwl_rx_mem_buffer *rxb;
259 struct page *page;
260 unsigned long flags;
261 gfp_t gfp_mask = priority;
262
263 while (1) {
264 spin_lock_irqsave(&rxq->lock, flags);
265 if (list_empty(&rxq->rx_used)) {
266 spin_unlock_irqrestore(&rxq->lock, flags);
267 return;
268 }
269 spin_unlock_irqrestore(&rxq->lock, flags);
270
271 if (rxq->free_count > RX_LOW_WATERMARK)
272 gfp_mask |= __GFP_NOWARN;
273
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700274 if (hw_params(trans).rx_page_order > 0)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700275 gfp_mask |= __GFP_COMP;
276
277 /* Alloc a new receive buffer */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700278 page = alloc_pages(gfp_mask,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700279 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700280 if (!page) {
281 if (net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700282 IWL_DEBUG_INFO(trans, "alloc_pages failed, "
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700283 "order: %d\n",
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700284 hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700285
286 if ((rxq->free_count <= RX_LOW_WATERMARK) &&
287 net_ratelimit())
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700288 IWL_CRIT(trans, "Failed to alloc_pages with %s."
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700289 "Only %u free buffers remaining.\n",
290 priority == GFP_ATOMIC ?
291 "GFP_ATOMIC" : "GFP_KERNEL",
292 rxq->free_count);
293 /* We don't reschedule replenish work here -- we will
294 * call the restock method and if it still needs
295 * more buffers it will schedule replenish */
296 return;
297 }
298
299 spin_lock_irqsave(&rxq->lock, flags);
300
301 if (list_empty(&rxq->rx_used)) {
302 spin_unlock_irqrestore(&rxq->lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700303 __free_pages(page, hw_params(trans).rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700304 return;
305 }
306 element = rxq->rx_used.next;
307 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
308 list_del(element);
309
310 spin_unlock_irqrestore(&rxq->lock, flags);
311
312 BUG_ON(rxb->page);
313 rxb->page = page;
314 /* Get physical address of the RB */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200315 rxb->page_dma = dma_map_page(trans->dev, page, 0,
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700316 PAGE_SIZE << hw_params(trans).rx_page_order,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700317 DMA_FROM_DEVICE);
318 /* dma address must be no more than 36 bits */
319 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
320 /* and also 256 byte aligned! */
321 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
322
323 spin_lock_irqsave(&rxq->lock, flags);
324
325 list_add_tail(&rxb->list, &rxq->rx_free);
326 rxq->free_count++;
327
328 spin_unlock_irqrestore(&rxq->lock, flags);
329 }
330}
331
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700332void iwlagn_rx_replenish(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700333{
Johannes Berg7b114882012-02-05 13:55:11 -0800334 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700335 unsigned long flags;
336
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700337 iwlagn_rx_allocate(trans, GFP_KERNEL);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700338
Johannes Berg7b114882012-02-05 13:55:11 -0800339 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700340 iwlagn_rx_queue_restock(trans);
Johannes Berg7b114882012-02-05 13:55:11 -0800341 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700342}
343
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700344static void iwlagn_rx_replenish_now(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700345{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700346 iwlagn_rx_allocate(trans, GFP_ATOMIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700347
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700348 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700349}
350
351void iwl_bg_rx_replenish(struct work_struct *data)
352{
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700353 struct iwl_trans_pcie *trans_pcie =
354 container_of(data, struct iwl_trans_pcie, rx_replenish);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700355
Johannes Berg1ee158d2012-02-17 10:07:44 -0800356 iwlagn_rx_replenish(trans_pcie->trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700357}
358
Johannes Bergdf2f3212012-03-05 11:24:40 -0800359static void iwl_rx_handle_rxbuf(struct iwl_trans *trans,
360 struct iwl_rx_mem_buffer *rxb)
361{
362 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
363 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -0800364 struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Johannes Bergdf2f3212012-03-05 11:24:40 -0800365 struct iwl_device_cmd *cmd;
366 unsigned long flags;
367 int len, err;
368 u16 sequence;
369 struct iwl_rx_cmd_buffer rxcb;
370 struct iwl_rx_packet *pkt;
371 bool reclaim;
372 int index, cmd_index;
373
374 if (WARN_ON(!rxb))
375 return;
376
377 dma_unmap_page(trans->dev, rxb->page_dma,
378 PAGE_SIZE << hw_params(trans).rx_page_order,
379 DMA_FROM_DEVICE);
380
381 rxcb._page = rxb->page;
382 pkt = rxb_addr(&rxcb);
383
384 IWL_DEBUG_RX(trans, "%s, 0x%02x\n",
385 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
386
387
388 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
389 len += sizeof(u32); /* account for status word */
Johannes Berg6c1011e2012-03-06 13:30:48 -0800390 trace_iwlwifi_dev_rx(trans->dev, pkt, len);
Johannes Bergdf2f3212012-03-05 11:24:40 -0800391
392 /* Reclaim a command buffer only if this packet is a response
393 * to a (driver-originated) command.
394 * If the packet (e.g. Rx frame) originated from uCode,
395 * there is no command buffer to reclaim.
396 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
397 * but apparently a few don't get set; catch them here. */
398 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
399 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
400 (pkt->hdr.cmd != REPLY_RX) &&
401 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
402 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
403 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
404 (pkt->hdr.cmd != REPLY_TX);
405
406 sequence = le16_to_cpu(pkt->hdr.sequence);
407 index = SEQ_TO_INDEX(sequence);
408 cmd_index = get_cmd_index(&txq->q, index);
409
410 if (reclaim)
411 cmd = txq->cmd[cmd_index];
412 else
413 cmd = NULL;
414
415 /* warn if this is cmd response / notification and the uCode
416 * didn't set the SEQ_RX_FRAME for a frame that is
417 * uCode-originated
418 * If you saw this code after the second half of 2012, then
419 * please remove it
420 */
421 WARN(pkt->hdr.cmd != REPLY_TX && reclaim == false &&
422 (!(pkt->hdr.sequence & SEQ_RX_FRAME)),
423 "reclaim is false, SEQ_RX_FRAME unset: %s\n",
424 get_cmd_string(pkt->hdr.cmd));
425
426 err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
427
428 /*
429 * XXX: After here, we should always check rxcb._page
430 * against NULL before touching it or its virtual
431 * memory (pkt). Because some rx_handler might have
432 * already taken or freed the pages.
433 */
434
435 if (reclaim) {
436 /* Invoke any callbacks, transfer the buffer to caller,
437 * and fire off the (possibly) blocking
438 * iwl_trans_send_cmd()
439 * as we reclaim the driver command queue */
440 if (rxcb._page)
441 iwl_tx_cmd_complete(trans, &rxcb, err);
442 else
443 IWL_WARN(trans, "Claim null rxb?\n");
444 }
445
446 /* page was stolen from us */
447 if (rxcb._page == NULL)
448 rxb->page = NULL;
449
450 /* Reuse the page if possible. For notification packets and
451 * SKBs that fail to Rx correctly, add them back into the
452 * rx_free list for reuse later. */
453 spin_lock_irqsave(&rxq->lock, flags);
454 if (rxb->page != NULL) {
455 rxb->page_dma =
456 dma_map_page(trans->dev, rxb->page, 0,
457 PAGE_SIZE << hw_params(trans).rx_page_order,
458 DMA_FROM_DEVICE);
459 list_add_tail(&rxb->list, &rxq->rx_free);
460 rxq->free_count++;
461 } else
462 list_add_tail(&rxb->list, &rxq->rx_used);
463 spin_unlock_irqrestore(&rxq->lock, flags);
464}
465
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700466/**
467 * iwl_rx_handle - Main entry function for receiving responses from uCode
468 *
469 * Uses the priv->rx_handlers callback function array to invoke
470 * the appropriate handlers, including command responses,
471 * frame-received notifications, and other notifications.
472 */
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700473static void iwl_rx_handle(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700474{
Johannes Bergdf2f3212012-03-05 11:24:40 -0800475 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700476 struct iwl_rx_queue *rxq = &trans_pcie->rxq;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700477 u32 r, i;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700478 u8 fill_rx = 0;
479 u32 count = 8;
480 int total_empty;
481
482 /* uCode's read index (stored in shared DRAM) indicates the last Rx
483 * buffer that the driver may process (last buffer filled by ucode). */
484 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
485 i = rxq->read;
486
487 /* Rx interrupt, but nothing sent from uCode */
488 if (i == r)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700489 IWL_DEBUG_RX(trans, "r = %d, i = %d\n", r, i);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700490
491 /* calculate total frames need to be restock after handling RX */
492 total_empty = r - rxq->write_actual;
493 if (total_empty < 0)
494 total_empty += RX_QUEUE_SIZE;
495
496 if (total_empty > (RX_QUEUE_SIZE / 2))
497 fill_rx = 1;
498
499 while (i != r) {
Johannes Berg48a2d662012-03-05 11:24:39 -0800500 struct iwl_rx_mem_buffer *rxb;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700501
502 rxb = rxq->queue[i];
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700503 rxq->queue[i] = NULL;
504
Johannes Bergdf2f3212012-03-05 11:24:40 -0800505 IWL_DEBUG_RX(trans, "rxbuf: r = %d, i = %d (%p)\n", rxb);
Johannes Berg48a2d662012-03-05 11:24:39 -0800506
Johannes Bergdf2f3212012-03-05 11:24:40 -0800507 iwl_rx_handle_rxbuf(trans, rxb);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700508
509 i = (i + 1) & RX_QUEUE_MASK;
510 /* If there are a lot of unused frames,
511 * restock the Rx queue so ucode wont assert. */
512 if (fill_rx) {
513 count++;
514 if (count >= 8) {
515 rxq->read = i;
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700516 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700517 count = 0;
518 }
519 }
520 }
521
522 /* Backtrack one entry */
523 rxq->read = i;
524 if (fill_rx)
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700525 iwlagn_rx_replenish_now(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700526 else
Emmanuel Grumbach5a878bf2011-08-25 23:10:51 -0700527 iwlagn_rx_queue_restock(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700528}
529
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700530static const char * const desc_lookup_text[] = {
531 "OK",
532 "FAIL",
533 "BAD_PARAM",
534 "BAD_CHECKSUM",
535 "NMI_INTERRUPT_WDG",
536 "SYSASSERT",
537 "FATAL_ERROR",
538 "BAD_COMMAND",
539 "HW_ERROR_TUNE_LOCK",
540 "HW_ERROR_TEMPERATURE",
541 "ILLEGAL_CHAN_FREQ",
542 "VCC_NOT_STABLE",
543 "FH_ERROR",
544 "NMI_INTERRUPT_HOST",
545 "NMI_INTERRUPT_ACTION_PT",
546 "NMI_INTERRUPT_UNKNOWN",
547 "UCODE_VERSION_MISMATCH",
548 "HW_ERROR_ABS_LOCK",
549 "HW_ERROR_CAL_LOCK_FAIL",
550 "NMI_INTERRUPT_INST_ACTION_PT",
551 "NMI_INTERRUPT_DATA_ACTION_PT",
552 "NMI_TRM_HW_ER",
553 "NMI_INTERRUPT_TRM",
554 "NMI_INTERRUPT_BREAK_POINT",
555 "DEBUG_0",
556 "DEBUG_1",
557 "DEBUG_2",
558 "DEBUG_3",
559};
560
561static struct { char *name; u8 num; } advanced_lookup[] = {
562 { "NMI_INTERRUPT_WDG", 0x34 },
563 { "SYSASSERT", 0x35 },
564 { "UCODE_VERSION_MISMATCH", 0x37 },
565 { "BAD_COMMAND", 0x38 },
566 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
567 { "FATAL_ERROR", 0x3D },
568 { "NMI_TRM_HW_ERR", 0x46 },
569 { "NMI_INTERRUPT_TRM", 0x4C },
570 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
571 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
572 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
573 { "NMI_INTERRUPT_HOST", 0x66 },
574 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
575 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
576 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
577 { "ADVANCED_SYSASSERT", 0 },
578};
579
580static const char *desc_lookup(u32 num)
581{
582 int i;
583 int max = ARRAY_SIZE(desc_lookup_text);
584
585 if (num < max)
586 return desc_lookup_text[num];
587
588 max = ARRAY_SIZE(advanced_lookup) - 1;
589 for (i = 0; i < max; i++) {
590 if (advanced_lookup[i].num == num)
591 break;
592 }
593 return advanced_lookup[i].name;
594}
595
596#define ERROR_START_OFFSET (1 * sizeof(u32))
597#define ERROR_ELEM_SIZE (7 * sizeof(u32))
598
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700599static void iwl_dump_nic_error_log(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700600{
601 u32 base;
602 struct iwl_error_event_table table;
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700603 struct iwl_trans_pcie *trans_pcie =
604 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700605
Don Fryae6130f2011-11-30 16:12:59 -0800606 base = trans->shrd->device_pointers.error_event_table;
Don Fry3d6acef2011-11-28 17:05:01 -0800607 if (trans->shrd->ucode_type == IWL_UCODE_INIT) {
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700608 if (!base)
Johannes Berg0692fe42012-03-06 13:30:37 -0800609 base = trans->shrd->fw->init_errlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700610 } else {
611 if (!base)
Johannes Berg0692fe42012-03-06 13:30:37 -0800612 base = trans->shrd->fw->inst_errlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700613 }
614
615 if (!iwlagn_hw_valid_rtc_data_addr(base)) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700616 IWL_ERR(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700617 "Not valid error log pointer 0x%08X for %s uCode\n",
618 base,
Don Fry3d6acef2011-11-28 17:05:01 -0800619 (trans->shrd->ucode_type == IWL_UCODE_INIT)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700620 ? "Init" : "RT");
621 return;
622 }
623
Don Fry86551122012-02-07 14:03:55 -0800624 iwl_read_targ_mem_words(trans, base, &table, sizeof(table));
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700625
626 if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700627 IWL_ERR(trans, "Start IWL Error Log Dump:\n");
628 IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
629 trans->shrd->status, table.valid);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700630 }
631
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -0700632 trans_pcie->isr_stats.err_code = table.error_id;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700633
Johannes Berg6c1011e2012-03-06 13:30:48 -0800634 trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700635 table.data1, table.data2, table.line,
636 table.blink1, table.blink2, table.ilink1,
637 table.ilink2, table.bcon_time, table.gp1,
638 table.gp2, table.gp3, table.ucode_ver,
639 table.hw_ver, table.brd_ver);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700640 IWL_ERR(trans, "0x%08X | %-28s\n", table.error_id,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700641 desc_lookup(table.error_id));
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700642 IWL_ERR(trans, "0x%08X | uPc\n", table.pc);
643 IWL_ERR(trans, "0x%08X | branchlink1\n", table.blink1);
644 IWL_ERR(trans, "0x%08X | branchlink2\n", table.blink2);
645 IWL_ERR(trans, "0x%08X | interruptlink1\n", table.ilink1);
646 IWL_ERR(trans, "0x%08X | interruptlink2\n", table.ilink2);
647 IWL_ERR(trans, "0x%08X | data1\n", table.data1);
648 IWL_ERR(trans, "0x%08X | data2\n", table.data2);
649 IWL_ERR(trans, "0x%08X | line\n", table.line);
650 IWL_ERR(trans, "0x%08X | beacon time\n", table.bcon_time);
651 IWL_ERR(trans, "0x%08X | tsf low\n", table.tsf_low);
652 IWL_ERR(trans, "0x%08X | tsf hi\n", table.tsf_hi);
653 IWL_ERR(trans, "0x%08X | time gp1\n", table.gp1);
654 IWL_ERR(trans, "0x%08X | time gp2\n", table.gp2);
655 IWL_ERR(trans, "0x%08X | time gp3\n", table.gp3);
656 IWL_ERR(trans, "0x%08X | uCode version\n", table.ucode_ver);
657 IWL_ERR(trans, "0x%08X | hw version\n", table.hw_ver);
658 IWL_ERR(trans, "0x%08X | board version\n", table.brd_ver);
659 IWL_ERR(trans, "0x%08X | hcmd\n", table.hcmd);
Wey-Yi Guyd332f592011-11-30 12:32:42 -0800660
661 IWL_ERR(trans, "0x%08X | isr0\n", table.isr0);
662 IWL_ERR(trans, "0x%08X | isr1\n", table.isr1);
663 IWL_ERR(trans, "0x%08X | isr2\n", table.isr2);
664 IWL_ERR(trans, "0x%08X | isr3\n", table.isr3);
665 IWL_ERR(trans, "0x%08X | isr4\n", table.isr4);
666 IWL_ERR(trans, "0x%08X | isr_pref\n", table.isr_pref);
667 IWL_ERR(trans, "0x%08X | wait_event\n", table.wait_event);
668 IWL_ERR(trans, "0x%08X | l2p_control\n", table.l2p_control);
669 IWL_ERR(trans, "0x%08X | l2p_duration\n", table.l2p_duration);
670 IWL_ERR(trans, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
671 IWL_ERR(trans, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
672 IWL_ERR(trans, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
673 IWL_ERR(trans, "0x%08X | timestamp\n", table.u_timestamp);
674 IWL_ERR(trans, "0x%08X | flow_handler\n", table.flow_handler);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700675}
676
677/**
678 * iwl_irq_handle_error - called for HW or SW error interrupt from card
679 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700680static void iwl_irq_handle_error(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700681{
682 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
Emmanuel Grumbachff6e75c2012-02-12 15:21:08 +0200683 if (cfg(trans)->internal_wimax_coex &&
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200684 (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700685 APMS_CLK_VAL_MRB_FUNC_MODE) ||
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200686 (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700687 APMG_PS_CTRL_VAL_RESET_REQ))) {
688 /*
689 * Keep the restart process from trying to send host
690 * commands by clearing the ready bit.
691 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700692 clear_bit(STATUS_READY, &trans->shrd->status);
693 clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
Emmanuel Grumbachff6e75c2012-02-12 15:21:08 +0200694 wake_up(&trans->shrd->wait_command_queue);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700695 IWL_ERR(trans, "RF is used by WiMAX\n");
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700696 return;
697 }
698
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700699 IWL_ERR(trans, "Loaded firmware version: %s\n",
Johannes Berg0692fe42012-03-06 13:30:37 -0800700 trans->shrd->fw->fw_version);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700701
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700702 iwl_dump_nic_error_log(trans);
703 iwl_dump_csr(trans);
704 iwl_dump_fh(trans, NULL, false);
705 iwl_dump_nic_event_log(trans, false, NULL, false);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700706
Emmanuel Grumbachbcb93212012-02-09 16:08:15 +0200707 iwl_op_mode_nic_error(trans->op_mode);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700708}
709
710#define EVENT_START_OFFSET (4 * sizeof(u32))
711
712/**
713 * iwl_print_event_log - Dump error event log to syslog
714 *
715 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700716static int iwl_print_event_log(struct iwl_trans *trans, u32 start_idx,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700717 u32 num_events, u32 mode,
718 int pos, char **buf, size_t bufsz)
719{
720 u32 i;
721 u32 base; /* SRAM byte address of event log header */
722 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
723 u32 ptr; /* SRAM byte address of log data */
724 u32 ev, time, data; /* event log data */
725 unsigned long reg_flags;
726
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)
Johannes Berg0692fe42012-03-06 13:30:37 -0800733 base = trans->shrd->fw->init_evtlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700734 } else {
735 if (!base)
Johannes Berg0692fe42012-03-06 13:30:37 -0800736 base = trans->shrd->fw->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);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800748 if (unlikely(!iwl_grab_nic_access(trans)))
749 goto out_unlock;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700750
751 /* Set starting address; reads will auto-increment */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200752 iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700753
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 {
Johannes Berg6c1011e2012-03-06 13:30:48 -0800766 trace_iwlwifi_dev_ucode_event(trans->dev, 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);
Johannes Berg6c1011e2012-03-06 13:30:48 -0800780 trace_iwlwifi_dev_ucode_event(trans->dev, 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);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800788out_unlock:
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200789 spin_unlock_irqrestore(&trans->reg_lock, reg_flags);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700790 return pos;
791}
792
793/**
794 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
795 */
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700796static int iwl_print_last_event_logs(struct iwl_trans *trans, u32 capacity,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700797 u32 num_wraps, u32 next_entry,
798 u32 size, u32 mode,
799 int pos, char **buf, size_t bufsz)
800{
801 /*
802 * display the newest DEFAULT_LOG_ENTRIES entries
803 * i.e the entries just before the next ont that uCode would fill.
804 */
805 if (num_wraps) {
806 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700807 pos = iwl_print_event_log(trans,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700808 capacity - (size - next_entry),
809 size - next_entry, mode,
810 pos, buf, bufsz);
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700811 pos = iwl_print_event_log(trans, 0,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700812 next_entry, mode,
813 pos, buf, bufsz);
814 } else
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700815 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700816 size, mode, pos, buf, bufsz);
817 } else {
818 if (next_entry < size) {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700819 pos = iwl_print_event_log(trans, 0, next_entry,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700820 mode, pos, buf, bufsz);
821 } else {
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700822 pos = iwl_print_event_log(trans, next_entry - size,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700823 size, mode, pos, buf, bufsz);
824 }
825 }
826 return pos;
827}
828
829#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
830
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -0700831int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700832 char **buf, bool display)
833{
834 u32 base; /* SRAM byte address of event log header */
835 u32 capacity; /* event log capacity in # entries */
836 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
837 u32 num_wraps; /* # times uCode wrapped to top of log */
838 u32 next_entry; /* index of next entry to be written by uCode */
839 u32 size; /* # entries that we'll print */
840 u32 logsize;
841 int pos = 0;
842 size_t bufsz = 0;
843
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) {
Johannes Berg0692fe42012-03-06 13:30:37 -0800846 logsize = trans->shrd->fw->init_evtlog_size;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700847 if (!base)
Johannes Berg0692fe42012-03-06 13:30:37 -0800848 base = trans->shrd->fw->init_evtlog_ptr;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700849 } else {
Johannes Berg0692fe42012-03-06 13:30:37 -0800850 logsize = trans->shrd->fw->inst_evtlog_size;
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -0700851 if (!base)
Johannes Berg0692fe42012-03-06 13:30:37 -0800852 base = trans->shrd->fw->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) {
Johannes Bergc9eec952012-03-06 13:30:43 -08001019 bool hw_rfkill;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001020
Johannes Bergc9eec952012-03-06 13:30:43 -08001021 hw_rfkill = !(iwl_read32(trans, CSR_GP_CNTRL) &
1022 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001023 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
Johannes Bergc9eec952012-03-06 13:30:43 -08001024 hw_rfkill ? "disable radio" : "enable radio");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001025
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001026 isr_stats->rfkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001027
Johannes Bergc9eec952012-03-06 13:30:43 -08001028 iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001029
1030 handled |= CSR_INT_BIT_RF_KILL;
1031 }
1032
1033 /* Chip got too hot and stopped itself */
1034 if (inta & CSR_INT_BIT_CT_KILL) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001035 IWL_ERR(trans, "Microcode CT kill error detected.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001036 isr_stats->ctkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001037 handled |= CSR_INT_BIT_CT_KILL;
1038 }
1039
1040 /* Error detected by uCode */
1041 if (inta & CSR_INT_BIT_SW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001042 IWL_ERR(trans, "Microcode SW error detected. "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001043 " Restarting 0x%X.\n", inta);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001044 isr_stats->sw++;
Emmanuel Grumbach6bb78842011-08-25 23:11:09 -07001045 iwl_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001046 handled |= CSR_INT_BIT_SW_ERR;
1047 }
1048
1049 /* uCode wakes up after power-down sleep */
1050 if (inta & CSR_INT_BIT_WAKEUP) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001051 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1052 iwl_rx_queue_update_write_ptr(trans, &trans_pcie->rxq);
1053 for (i = 0; i < hw_params(trans).max_txq_num; i++)
Emmanuel Grumbachfd656932011-08-25 23:11:19 -07001054 iwl_txq_update_write_ptr(trans,
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001055 &trans_pcie->txq[i]);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001056
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001057 isr_stats->wakeup++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001058
1059 handled |= CSR_INT_BIT_WAKEUP;
1060 }
1061
1062 /* All uCode command responses, including Tx command responses,
1063 * Rx "responses" (frame-received notification), and other
1064 * notifications from uCode come through here*/
1065 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1066 CSR_INT_BIT_RX_PERIODIC)) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001067 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001068 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1069 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001070 iwl_write32(trans, CSR_FH_INT_STATUS,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001071 CSR_FH_INT_RX_MASK);
1072 }
1073 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1074 handled |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001075 iwl_write32(trans,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001076 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001077 }
1078 /* Sending RX interrupt require many steps to be done in the
1079 * the device:
1080 * 1- write interrupt to current index in ICT table.
1081 * 2- dma RX frame.
1082 * 3- update RX shared data to indicate last write index.
1083 * 4- send interrupt.
1084 * This could lead to RX race, driver could receive RX interrupt
1085 * but the shared data changes does not reflect this;
1086 * periodic interrupt will detect any dangling Rx activity.
1087 */
1088
1089 /* Disable periodic interrupt; we use it as just a one-shot. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001090 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001091 CSR_INT_PERIODIC_DIS);
Gregory Greenmana5916972012-01-10 19:22:56 +02001092#ifdef CONFIG_IWLWIFI_IDI
1093 iwl_amfh_rx_handler();
1094#else
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001095 iwl_rx_handle(trans);
Gregory Greenmana5916972012-01-10 19:22:56 +02001096#endif
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001097 /*
1098 * Enable periodic interrupt in 8 msec only if we received
1099 * real RX interrupt (instead of just periodic int), to catch
1100 * any dangling Rx interrupt. If it was just the periodic
1101 * interrupt, there was no dangling Rx activity, and no need
1102 * to extend the periodic interrupt; one-shot is enough.
1103 */
1104 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001105 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001106 CSR_INT_PERIODIC_ENA);
1107
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001108 isr_stats->rx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001109 }
1110
1111 /* This "Tx" DMA channel is used only for loading uCode */
1112 if (inta & CSR_INT_BIT_FH_TX) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001113 iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001114 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001115 isr_stats->tx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001116 handled |= CSR_INT_BIT_FH_TX;
1117 /* Wake up uCode load routine, now that load is complete */
Johannes Berg13df1aa2012-03-06 13:31:00 -08001118 trans_pcie->ucode_write_complete = true;
1119 wake_up(&trans_pcie->ucode_write_waitq);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001120 }
1121
1122 if (inta & ~handled) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001123 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001124 isr_stats->unhandled++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001125 }
1126
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001127 if (inta & ~(trans_pcie->inta_mask)) {
1128 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1129 inta & ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001130 }
1131
1132 /* Re-enable all interrupts */
1133 /* only Re-enable if disabled by irq */
Don Fry83626402012-03-07 09:52:37 -08001134 if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001135 iwl_enable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001136 /* Re-enable RF_KILL if it occurred */
Stanislaw Gruszka8722c892012-03-07 09:52:28 -08001137 else if (handled & CSR_INT_BIT_RF_KILL)
1138 iwl_enable_rfkill_int(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001139}
1140
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001141/******************************************************************************
1142 *
1143 * ICT functions
1144 *
1145 ******************************************************************************/
Johannes Berg10667132011-12-19 14:00:59 -08001146
1147/* a device (PCI-E) page is 4096 bytes long */
1148#define ICT_SHIFT 12
1149#define ICT_SIZE (1 << ICT_SHIFT)
1150#define ICT_COUNT (ICT_SIZE / sizeof(u32))
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001151
1152/* Free dram table */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001153void iwl_free_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001154{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001155 struct iwl_trans_pcie *trans_pcie =
1156 IWL_TRANS_GET_PCIE_TRANS(trans);
1157
Johannes Berg10667132011-12-19 14:00:59 -08001158 if (trans_pcie->ict_tbl) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001159 dma_free_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001160 trans_pcie->ict_tbl,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001161 trans_pcie->ict_tbl_dma);
Johannes Berg10667132011-12-19 14:00:59 -08001162 trans_pcie->ict_tbl = NULL;
1163 trans_pcie->ict_tbl_dma = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001164 }
1165}
1166
1167
Johannes Berg10667132011-12-19 14:00:59 -08001168/*
1169 * allocate dram shared table, it is an aligned memory
1170 * block of ICT_SIZE.
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001171 * also reset all data related to ICT table interrupt.
1172 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001173int iwl_alloc_isr_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001174{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001175 struct iwl_trans_pcie *trans_pcie =
1176 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001177
Johannes Berg10667132011-12-19 14:00:59 -08001178 trans_pcie->ict_tbl =
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001179 dma_alloc_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001180 &trans_pcie->ict_tbl_dma,
1181 GFP_KERNEL);
1182 if (!trans_pcie->ict_tbl)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001183 return -ENOMEM;
1184
Johannes Berg10667132011-12-19 14:00:59 -08001185 /* just an API sanity check ... it is guaranteed to be aligned */
1186 if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
1187 iwl_free_isr_ict(trans);
1188 return -EINVAL;
1189 }
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001190
Johannes Berg10667132011-12-19 14:00:59 -08001191 IWL_DEBUG_ISR(trans, "ict dma addr %Lx\n",
1192 (unsigned long long)trans_pcie->ict_tbl_dma);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001193
Johannes Berg10667132011-12-19 14:00:59 -08001194 IWL_DEBUG_ISR(trans, "ict vir addr %p\n", trans_pcie->ict_tbl);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001195
1196 /* reset table and index to all 0 */
Johannes Berg10667132011-12-19 14:00:59 -08001197 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001198 trans_pcie->ict_index = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001199
1200 /* add periodic RX interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001201 trans_pcie->inta_mask |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001202 return 0;
1203}
1204
1205/* Device is going up inform it about using ICT interrupt table,
1206 * also we need to tell the driver to start using ICT interrupt.
1207 */
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001208void iwl_reset_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001209{
1210 u32 val;
1211 unsigned long flags;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001212 struct iwl_trans_pcie *trans_pcie =
1213 IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001214
Johannes Berg10667132011-12-19 14:00:59 -08001215 if (!trans_pcie->ict_tbl)
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001216 return;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001217
Johannes Berg7b114882012-02-05 13:55:11 -08001218 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001219 iwl_disable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001220
Johannes Berg10667132011-12-19 14:00:59 -08001221 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001222
Johannes Berg10667132011-12-19 14:00:59 -08001223 val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001224
1225 val |= CSR_DRAM_INT_TBL_ENABLE;
1226 val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
1227
Johannes Berg10667132011-12-19 14:00:59 -08001228 IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001229
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001230 iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001231 trans_pcie->use_ict = true;
1232 trans_pcie->ict_index = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001233 iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001234 iwl_enable_interrupts(trans);
Johannes Berg7b114882012-02-05 13:55:11 -08001235 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001236}
1237
1238/* Device is going down disable ict interrupt usage */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001239void iwl_disable_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001240{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001241 struct iwl_trans_pcie *trans_pcie =
1242 IWL_TRANS_GET_PCIE_TRANS(trans);
1243
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001244 unsigned long flags;
1245
Johannes Berg7b114882012-02-05 13:55:11 -08001246 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001247 trans_pcie->use_ict = false;
Johannes Berg7b114882012-02-05 13:55:11 -08001248 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001249}
1250
1251static irqreturn_t iwl_isr(int irq, void *data)
1252{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001253 struct iwl_trans *trans = data;
1254 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001255 u32 inta, inta_mask;
1256 unsigned long flags;
1257#ifdef CONFIG_IWLWIFI_DEBUG
1258 u32 inta_fh;
1259#endif
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001260 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001261 return IRQ_NONE;
1262
Johannes Berg6c1011e2012-03-06 13:30:48 -08001263 trace_iwlwifi_dev_irq(trans->dev);
Johannes Bergb80667e2011-12-09 07:26:13 -08001264
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001265 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1266
Johannes Berg7b114882012-02-05 13:55:11 -08001267 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001268
1269 /* Disable (but don't clear!) interrupts here to avoid
1270 * back-to-back ISRs and sporadic interrupts from our NIC.
1271 * If we have something to service, the tasklet will re-enable ints.
1272 * If we *don't* have something, we'll re-enable before leaving here. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001273 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1274 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001275
1276 /* Discover which interrupts are active/pending */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001277 inta = iwl_read32(trans, CSR_INT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001278
1279 /* Ignore interrupt if there's nothing in NIC to service.
1280 * This may be due to IRQ shared with another device,
1281 * or due to sporadic interrupts thrown from our NIC. */
1282 if (!inta) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001283 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001284 goto none;
1285 }
1286
1287 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1288 /* Hardware disappeared. It might have already raised
1289 * an interrupt */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001290 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001291 goto unplugged;
1292 }
1293
1294#ifdef CONFIG_IWLWIFI_DEBUG
Johannes Berga8bceb32012-03-05 11:24:30 -08001295 if (iwl_have_debug_level(IWL_DL_ISR)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001296 inta_fh = iwl_read32(trans, CSR_FH_INT_STATUS);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001297 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x, "
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001298 "fh 0x%08x\n", inta, inta_mask, inta_fh);
1299 }
1300#endif
1301
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001302 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001303 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1304 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001305 tasklet_schedule(&trans_pcie->irq_tasklet);
Don Fry83626402012-03-07 09:52:37 -08001306 else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001307 !trans_pcie->inta)
1308 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001309
1310 unplugged:
Johannes Berg7b114882012-02-05 13:55:11 -08001311 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001312 return IRQ_HANDLED;
1313
1314 none:
1315 /* re-enable interrupts here since we don't have anything to service. */
1316 /* only Re-enable if disabled by irq and no schedules tasklet. */
Don Fry83626402012-03-07 09:52:37 -08001317 if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001318 !trans_pcie->inta)
1319 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001320
Johannes Berg7b114882012-02-05 13:55:11 -08001321 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001322 return IRQ_NONE;
1323}
1324
1325/* interrupt handler using ict table, with this interrupt driver will
1326 * stop using INTA register to get device's interrupt, reading this register
1327 * is expensive, device will write interrupts in ICT dram table, increment
1328 * index then will fire interrupt to driver, driver will OR all ICT table
1329 * entries from current index up to table entry with 0 value. the result is
1330 * the interrupt we need to service, driver will set the entries back to 0 and
1331 * set index.
1332 */
1333irqreturn_t iwl_isr_ict(int irq, void *data)
1334{
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001335 struct iwl_trans *trans = data;
1336 struct iwl_trans_pcie *trans_pcie;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001337 u32 inta, inta_mask;
1338 u32 val = 0;
Johannes Bergb80667e2011-12-09 07:26:13 -08001339 u32 read;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001340 unsigned long flags;
1341
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001342 if (!trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001343 return IRQ_NONE;
1344
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001345 trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1346
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001347 /* dram interrupt table not set yet,
1348 * use legacy interrupt.
1349 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001350 if (!trans_pcie->use_ict)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001351 return iwl_isr(irq, data);
1352
Johannes Berg6c1011e2012-03-06 13:30:48 -08001353 trace_iwlwifi_dev_irq(trans->dev);
Johannes Bergb80667e2011-12-09 07:26:13 -08001354
Johannes Berg7b114882012-02-05 13:55:11 -08001355 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001356
1357 /* Disable (but don't clear!) interrupts here to avoid
1358 * back-to-back ISRs and sporadic interrupts from our NIC.
1359 * If we have something to service, the tasklet will re-enable ints.
1360 * If we *don't* have something, we'll re-enable before leaving here.
1361 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001362 inta_mask = iwl_read32(trans, CSR_INT_MASK); /* just for debug */
1363 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001364
1365
1366 /* Ignore interrupt if there's nothing in NIC to service.
1367 * This may be due to IRQ shared with another device,
1368 * or due to sporadic interrupts thrown from our NIC. */
Johannes Bergb80667e2011-12-09 07:26:13 -08001369 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
Johannes Berg6c1011e2012-03-06 13:30:48 -08001370 trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
Johannes Bergb80667e2011-12-09 07:26:13 -08001371 if (!read) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001372 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001373 goto none;
1374 }
1375
Johannes Bergb80667e2011-12-09 07:26:13 -08001376 /*
1377 * Collect all entries up to the first 0, starting from ict_index;
1378 * note we already read at ict_index.
1379 */
1380 do {
1381 val |= read;
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001382 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
Johannes Bergb80667e2011-12-09 07:26:13 -08001383 trans_pcie->ict_index, read);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001384 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1385 trans_pcie->ict_index =
1386 iwl_queue_inc_wrap(trans_pcie->ict_index, ICT_COUNT);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001387
Johannes Bergb80667e2011-12-09 07:26:13 -08001388 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
Johannes Berg6c1011e2012-03-06 13:30:48 -08001389 trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
Johannes Bergb80667e2011-12-09 07:26:13 -08001390 read);
1391 } while (read);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001392
1393 /* We should not get this value, just ignore it. */
1394 if (val == 0xffffffff)
1395 val = 0;
1396
1397 /*
1398 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1399 * (bit 15 before shifting it to 31) to clear when using interrupt
1400 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1401 * so we use them to decide on the real state of the Rx bit.
1402 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1403 */
1404 if (val & 0xC0000)
1405 val |= 0x8000;
1406
1407 inta = (0xff & val) | ((0xff00 & val) << 16);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001408 IWL_DEBUG_ISR(trans, "ISR inta 0x%08x, enabled 0x%08x ict 0x%08x\n",
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001409 inta, inta_mask, val);
1410
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001411 inta &= trans_pcie->inta_mask;
1412 trans_pcie->inta |= inta;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001413
1414 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1415 if (likely(inta))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001416 tasklet_schedule(&trans_pcie->irq_tasklet);
Don Fry83626402012-03-07 09:52:37 -08001417 else if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
Johannes Bergb80667e2011-12-09 07:26:13 -08001418 !trans_pcie->inta) {
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001419 /* Allow interrupt if was disabled by this handler and
1420 * no tasklet was schedules, We should not enable interrupt,
1421 * tasklet will enable it.
1422 */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001423 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001424 }
1425
Johannes Berg7b114882012-02-05 13:55:11 -08001426 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001427 return IRQ_HANDLED;
1428
1429 none:
1430 /* re-enable interrupts here since we don't have anything to service.
1431 * only Re-enable if disabled by irq.
1432 */
Don Fry83626402012-03-07 09:52:37 -08001433 if (test_bit(STATUS_INT_ENABLED, &trans_pcie->status) &&
Johannes Bergb80667e2011-12-09 07:26:13 -08001434 !trans_pcie->inta)
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001435 iwl_enable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001436
Johannes Berg7b114882012-02-05 13:55:11 -08001437 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001438 return IRQ_NONE;
1439}