OSDN Git Service

Merge remote-tracking branches 'asoc/fix/fsl-ssi', 'asoc/fix/intel', 'asoc/fix/intel...
[uclinux-h8/linux.git] / drivers / net / wireless / iwlwifi / pcie / rx.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
4  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
5  *
6  * Portions of this file are derived from the ipw3945 project, as well
7  * as portions of the ieee80211 subsystem header files.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21  *
22  * The full GNU General Public License is included in this distribution in the
23  * file called LICENSE.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <ilw@linux.intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  *****************************************************************************/
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/gfp.h>
33
34 #include "iwl-prph.h"
35 #include "iwl-io.h"
36 #include "internal.h"
37 #include "iwl-op-mode.h"
38
39 /******************************************************************************
40  *
41  * RX path functions
42  *
43  ******************************************************************************/
44
45 /*
46  * Rx theory of operation
47  *
48  * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
49  * each of which point to Receive Buffers to be filled by the NIC.  These get
50  * used not only for Rx frames, but for any command response or notification
51  * from the NIC.  The driver and NIC manage the Rx buffers by means
52  * of indexes into the circular buffer.
53  *
54  * Rx Queue Indexes
55  * The host/firmware share two index registers for managing the Rx buffers.
56  *
57  * The READ index maps to the first position that the firmware may be writing
58  * to -- the driver can read up to (but not including) this position and get
59  * good data.
60  * The READ index is managed by the firmware once the card is enabled.
61  *
62  * The WRITE index maps to the last position the driver has read from -- the
63  * position preceding WRITE is the last slot the firmware can place a packet.
64  *
65  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
66  * WRITE = READ.
67  *
68  * During initialization, the host sets up the READ queue position to the first
69  * INDEX position, and WRITE to the last (READ - 1 wrapped)
70  *
71  * When the firmware places a packet in a buffer, it will advance the READ index
72  * and fire the RX interrupt.  The driver can then query the READ index and
73  * process as many packets as possible, moving the WRITE index forward as it
74  * resets the Rx queue buffers with new memory.
75  *
76  * The management in the driver is as follows:
77  * + A list of pre-allocated RBDs is stored in iwl->rxq->rx_free.
78  *   When the interrupt handler is called, the request is processed.
79  *   The page is either stolen - transferred to the upper layer
80  *   or reused - added immediately to the iwl->rxq->rx_free list.
81  * + When the page is stolen - the driver updates the matching queue's used
82  *   count, detaches the RBD and transfers it to the queue used list.
83  *   When there are two used RBDs - they are transferred to the allocator empty
84  *   list. Work is then scheduled for the allocator to start allocating
85  *   eight buffers.
86  *   When there are another 6 used RBDs - they are transferred to the allocator
87  *   empty list and the driver tries to claim the pre-allocated buffers and
88  *   add them to iwl->rxq->rx_free. If it fails - it continues to claim them
89  *   until ready.
90  *   When there are 8+ buffers in the free list - either from allocation or from
91  *   8 reused unstolen pages - restock is called to update the FW and indexes.
92  * + In order to make sure the allocator always has RBDs to use for allocation
93  *   the allocator has initial pool in the size of num_queues*(8-2) - the
94  *   maximum missing RBDs per allocation request (request posted with 2
95  *    empty RBDs, there is no guarantee when the other 6 RBDs are supplied).
96  *   The queues supplies the recycle of the rest of the RBDs.
97  * + A received packet is processed and handed to the kernel network stack,
98  *   detached from the iwl->rxq.  The driver 'processed' index is updated.
99  * + If there are no allocated buffers in iwl->rxq->rx_free,
100  *   the READ INDEX is not incremented and iwl->status(RX_STALLED) is set.
101  *   If there were enough free buffers and RX_STALLED is set it is cleared.
102  *
103  *
104  * Driver sequence:
105  *
106  * iwl_rxq_alloc()            Allocates rx_free
107  * iwl_pcie_rx_replenish()    Replenishes rx_free list from rx_used, and calls
108  *                            iwl_pcie_rxq_restock.
109  *                            Used only during initialization.
110  * iwl_pcie_rxq_restock()     Moves available buffers from rx_free into Rx
111  *                            queue, updates firmware pointers, and updates
112  *                            the WRITE index.
113  * iwl_pcie_rx_allocator()     Background work for allocating pages.
114  *
115  * -- enable interrupts --
116  * ISR - iwl_rx()             Detach iwl_rx_mem_buffers from pool up to the
117  *                            READ INDEX, detaching the SKB from the pool.
118  *                            Moves the packet buffer from queue to rx_used.
119  *                            Posts and claims requests to the allocator.
120  *                            Calls iwl_pcie_rxq_restock to refill any empty
121  *                            slots.
122  *
123  * RBD life-cycle:
124  *
125  * Init:
126  * rxq.pool -> rxq.rx_used -> rxq.rx_free -> rxq.queue
127  *
128  * Regular Receive interrupt:
129  * Page Stolen:
130  * rxq.queue -> rxq.rx_used -> allocator.rbd_empty ->
131  * allocator.rbd_allocated -> rxq.rx_free -> rxq.queue
132  * Page not Stolen:
133  * rxq.queue -> rxq.rx_free -> rxq.queue
134  * ...
135  *
136  */
137
138 /*
139  * iwl_rxq_space - Return number of free slots available in queue.
140  */
141 static int iwl_rxq_space(const struct iwl_rxq *rxq)
142 {
143         /* Make sure RX_QUEUE_SIZE is a power of 2 */
144         BUILD_BUG_ON(RX_QUEUE_SIZE & (RX_QUEUE_SIZE - 1));
145
146         /*
147          * There can be up to (RX_QUEUE_SIZE - 1) free slots, to avoid ambiguity
148          * between empty and completely full queues.
149          * The following is equivalent to modulo by RX_QUEUE_SIZE and is well
150          * defined for negative dividends.
151          */
152         return (rxq->read - rxq->write - 1) & (RX_QUEUE_SIZE - 1);
153 }
154
155 /*
156  * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
157  */
158 static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
159 {
160         return cpu_to_le32((u32)(dma_addr >> 8));
161 }
162
163 /*
164  * iwl_pcie_rx_stop - stops the Rx DMA
165  */
166 int iwl_pcie_rx_stop(struct iwl_trans *trans)
167 {
168         iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
169         return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
170                                    FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
171 }
172
173 /*
174  * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
175  */
176 static void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans)
177 {
178         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
179         struct iwl_rxq *rxq = &trans_pcie->rxq;
180         u32 reg;
181
182         lockdep_assert_held(&rxq->lock);
183
184         /*
185          * explicitly wake up the NIC if:
186          * 1. shadow registers aren't enabled
187          * 2. there is a chance that the NIC is asleep
188          */
189         if (!trans->cfg->base_params->shadow_reg_enable &&
190             test_bit(STATUS_TPOWER_PMI, &trans->status)) {
191                 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
192
193                 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
194                         IWL_DEBUG_INFO(trans, "Rx queue requesting wakeup, GP1 = 0x%x\n",
195                                        reg);
196                         iwl_set_bit(trans, CSR_GP_CNTRL,
197                                     CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
198                         rxq->need_update = true;
199                         return;
200                 }
201         }
202
203         rxq->write_actual = round_down(rxq->write, 8);
204         iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, rxq->write_actual);
205 }
206
207 static void iwl_pcie_rxq_check_wrptr(struct iwl_trans *trans)
208 {
209         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
210         struct iwl_rxq *rxq = &trans_pcie->rxq;
211
212         spin_lock(&rxq->lock);
213
214         if (!rxq->need_update)
215                 goto exit_unlock;
216
217         iwl_pcie_rxq_inc_wr_ptr(trans);
218         rxq->need_update = false;
219
220  exit_unlock:
221         spin_unlock(&rxq->lock);
222 }
223
224 /*
225  * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
226  *
227  * If there are slots in the RX queue that need to be restocked,
228  * and we have free pre-allocated buffers, fill the ranks as much
229  * as we can, pulling from rx_free.
230  *
231  * This moves the 'write' index forward to catch up with 'processed', and
232  * also updates the memory address in the firmware to reference the new
233  * target buffer.
234  */
235 static void iwl_pcie_rxq_restock(struct iwl_trans *trans)
236 {
237         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
238         struct iwl_rxq *rxq = &trans_pcie->rxq;
239         struct iwl_rx_mem_buffer *rxb;
240
241         /*
242          * If the device isn't enabled - not need to try to add buffers...
243          * This can happen when we stop the device and still have an interrupt
244          * pending. We stop the APM before we sync the interrupts because we
245          * have to (see comment there). On the other hand, since the APM is
246          * stopped, we cannot access the HW (in particular not prph).
247          * So don't try to restock if the APM has been already stopped.
248          */
249         if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
250                 return;
251
252         spin_lock(&rxq->lock);
253         while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
254                 /* The overwritten rxb must be a used one */
255                 rxb = rxq->queue[rxq->write];
256                 BUG_ON(rxb && rxb->page);
257
258                 /* Get next free Rx buffer, remove from free list */
259                 rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
260                                        list);
261                 list_del(&rxb->list);
262
263                 /* Point to Rx buffer via next RBD in circular buffer */
264                 rxq->bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
265                 rxq->queue[rxq->write] = rxb;
266                 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
267                 rxq->free_count--;
268         }
269         spin_unlock(&rxq->lock);
270
271         /* If we've added more space for the firmware to place data, tell it.
272          * Increment device's write pointer in multiples of 8. */
273         if (rxq->write_actual != (rxq->write & ~0x7)) {
274                 spin_lock(&rxq->lock);
275                 iwl_pcie_rxq_inc_wr_ptr(trans);
276                 spin_unlock(&rxq->lock);
277         }
278 }
279
280 /*
281  * iwl_pcie_rx_alloc_page - allocates and returns a page.
282  *
283  */
284 static struct page *iwl_pcie_rx_alloc_page(struct iwl_trans *trans)
285 {
286         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
287         struct iwl_rxq *rxq = &trans_pcie->rxq;
288         struct page *page;
289         gfp_t gfp_mask = GFP_KERNEL;
290
291         if (rxq->free_count > RX_LOW_WATERMARK)
292                 gfp_mask |= __GFP_NOWARN;
293
294         if (trans_pcie->rx_page_order > 0)
295                 gfp_mask |= __GFP_COMP;
296
297         /* Alloc a new receive buffer */
298         page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
299         if (!page) {
300                 if (net_ratelimit())
301                         IWL_DEBUG_INFO(trans, "alloc_pages failed, order: %d\n",
302                                        trans_pcie->rx_page_order);
303                 /* Issue an error if the hardware has consumed more than half
304                  * of its free buffer list and we don't have enough
305                  * pre-allocated buffers.
306 `                */
307                 if (rxq->free_count <= RX_LOW_WATERMARK &&
308                     iwl_rxq_space(rxq) > (RX_QUEUE_SIZE / 2) &&
309                     net_ratelimit())
310                         IWL_CRIT(trans,
311                                  "Failed to alloc_pages with GFP_KERNEL. Only %u free buffers remaining.\n",
312                                  rxq->free_count);
313                 return NULL;
314         }
315         return page;
316 }
317
318 /*
319  * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
320  *
321  * A used RBD is an Rx buffer that has been given to the stack. To use it again
322  * a page must be allocated and the RBD must point to the page. This function
323  * doesn't change the HW pointer but handles the list of pages that is used by
324  * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
325  * allocated buffers.
326  */
327 static void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans)
328 {
329         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
330         struct iwl_rxq *rxq = &trans_pcie->rxq;
331         struct iwl_rx_mem_buffer *rxb;
332         struct page *page;
333
334         while (1) {
335                 spin_lock(&rxq->lock);
336                 if (list_empty(&rxq->rx_used)) {
337                         spin_unlock(&rxq->lock);
338                         return;
339                 }
340                 spin_unlock(&rxq->lock);
341
342                 /* Alloc a new receive buffer */
343                 page = iwl_pcie_rx_alloc_page(trans);
344                 if (!page)
345                         return;
346
347                 spin_lock(&rxq->lock);
348
349                 if (list_empty(&rxq->rx_used)) {
350                         spin_unlock(&rxq->lock);
351                         __free_pages(page, trans_pcie->rx_page_order);
352                         return;
353                 }
354                 rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
355                                        list);
356                 list_del(&rxb->list);
357                 spin_unlock(&rxq->lock);
358
359                 BUG_ON(rxb->page);
360                 rxb->page = page;
361                 /* Get physical address of the RB */
362                 rxb->page_dma =
363                         dma_map_page(trans->dev, page, 0,
364                                      PAGE_SIZE << trans_pcie->rx_page_order,
365                                      DMA_FROM_DEVICE);
366                 if (dma_mapping_error(trans->dev, rxb->page_dma)) {
367                         rxb->page = NULL;
368                         spin_lock(&rxq->lock);
369                         list_add(&rxb->list, &rxq->rx_used);
370                         spin_unlock(&rxq->lock);
371                         __free_pages(page, trans_pcie->rx_page_order);
372                         return;
373                 }
374                 /* dma address must be no more than 36 bits */
375                 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
376                 /* and also 256 byte aligned! */
377                 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
378
379                 spin_lock(&rxq->lock);
380
381                 list_add_tail(&rxb->list, &rxq->rx_free);
382                 rxq->free_count++;
383
384                 spin_unlock(&rxq->lock);
385         }
386 }
387
388 static void iwl_pcie_rxq_free_rbs(struct iwl_trans *trans)
389 {
390         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
391         struct iwl_rxq *rxq = &trans_pcie->rxq;
392         int i;
393
394         lockdep_assert_held(&rxq->lock);
395
396         for (i = 0; i < RX_QUEUE_SIZE; i++) {
397                 if (!rxq->pool[i].page)
398                         continue;
399                 dma_unmap_page(trans->dev, rxq->pool[i].page_dma,
400                                PAGE_SIZE << trans_pcie->rx_page_order,
401                                DMA_FROM_DEVICE);
402                 __free_pages(rxq->pool[i].page, trans_pcie->rx_page_order);
403                 rxq->pool[i].page = NULL;
404         }
405 }
406
407 /*
408  * iwl_pcie_rx_replenish - Move all used buffers from rx_used to rx_free
409  *
410  * When moving to rx_free an page is allocated for the slot.
411  *
412  * Also restock the Rx queue via iwl_pcie_rxq_restock.
413  * This is called only during initialization
414  */
415 static void iwl_pcie_rx_replenish(struct iwl_trans *trans)
416 {
417         iwl_pcie_rxq_alloc_rbs(trans);
418
419         iwl_pcie_rxq_restock(trans);
420 }
421
422 /*
423  * iwl_pcie_rx_allocator - Allocates pages in the background for RX queues
424  *
425  * Allocates for each received request 8 pages
426  * Called as a scheduled work item.
427  */
428 static void iwl_pcie_rx_allocator(struct iwl_trans *trans)
429 {
430         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
431         struct iwl_rb_allocator *rba = &trans_pcie->rba;
432
433         while (atomic_read(&rba->req_pending)) {
434                 int i;
435                 struct list_head local_empty;
436                 struct list_head local_allocated;
437
438                 INIT_LIST_HEAD(&local_allocated);
439                 spin_lock(&rba->lock);
440                 /* swap out the entire rba->rbd_empty to a local list */
441                 list_replace_init(&rba->rbd_empty, &local_empty);
442                 spin_unlock(&rba->lock);
443
444                 for (i = 0; i < RX_CLAIM_REQ_ALLOC;) {
445                         struct iwl_rx_mem_buffer *rxb;
446                         struct page *page;
447
448                         /* List should never be empty - each reused RBD is
449                          * returned to the list, and initial pool covers any
450                          * possible gap between the time the page is allocated
451                          * to the time the RBD is added.
452                          */
453                         BUG_ON(list_empty(&local_empty));
454                         /* Get the first rxb from the rbd list */
455                         rxb = list_first_entry(&local_empty,
456                                                struct iwl_rx_mem_buffer, list);
457                         BUG_ON(rxb->page);
458
459                         /* Alloc a new receive buffer */
460                         page = iwl_pcie_rx_alloc_page(trans);
461                         if (!page)
462                                 continue;
463                         rxb->page = page;
464
465                         /* Get physical address of the RB */
466                         rxb->page_dma = dma_map_page(trans->dev, page, 0,
467                                         PAGE_SIZE << trans_pcie->rx_page_order,
468                                         DMA_FROM_DEVICE);
469                         if (dma_mapping_error(trans->dev, rxb->page_dma)) {
470                                 rxb->page = NULL;
471                                 __free_pages(page, trans_pcie->rx_page_order);
472                                 continue;
473                         }
474                         /* dma address must be no more than 36 bits */
475                         BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
476                         /* and also 256 byte aligned! */
477                         BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
478
479                         /* move the allocated entry to the out list */
480                         list_move(&rxb->list, &local_allocated);
481                         i++;
482                 }
483
484                 spin_lock(&rba->lock);
485                 /* add the allocated rbds to the allocator allocated list */
486                 list_splice_tail(&local_allocated, &rba->rbd_allocated);
487                 /* add the unused rbds back to the allocator empty list */
488                 list_splice_tail(&local_empty, &rba->rbd_empty);
489                 spin_unlock(&rba->lock);
490
491                 atomic_dec(&rba->req_pending);
492                 atomic_inc(&rba->req_ready);
493         }
494 }
495
496 /*
497  * iwl_pcie_rx_allocator_get - Returns the pre-allocated pages
498 .*
499 .* Called by queue when the queue posted allocation request and
500  * has freed 8 RBDs in order to restock itself.
501  */
502 static int iwl_pcie_rx_allocator_get(struct iwl_trans *trans,
503                                      struct iwl_rx_mem_buffer
504                                      *out[RX_CLAIM_REQ_ALLOC])
505 {
506         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
507         struct iwl_rb_allocator *rba = &trans_pcie->rba;
508         int i;
509
510         if (atomic_dec_return(&rba->req_ready) < 0) {
511                 atomic_inc(&rba->req_ready);
512                 IWL_DEBUG_RX(trans,
513                              "Allocation request not ready, pending requests = %d\n",
514                              atomic_read(&rba->req_pending));
515                 return -ENOMEM;
516         }
517
518         spin_lock(&rba->lock);
519         for (i = 0; i < RX_CLAIM_REQ_ALLOC; i++) {
520                 /* Get next free Rx buffer, remove it from free list */
521                 out[i] = list_first_entry(&rba->rbd_allocated,
522                                struct iwl_rx_mem_buffer, list);
523                 list_del(&out[i]->list);
524         }
525         spin_unlock(&rba->lock);
526
527         return 0;
528 }
529
530 static void iwl_pcie_rx_allocator_work(struct work_struct *data)
531 {
532         struct iwl_rb_allocator *rba_p =
533                 container_of(data, struct iwl_rb_allocator, rx_alloc);
534         struct iwl_trans_pcie *trans_pcie =
535                 container_of(rba_p, struct iwl_trans_pcie, rba);
536
537         iwl_pcie_rx_allocator(trans_pcie->trans);
538 }
539
540 static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
541 {
542         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
543         struct iwl_rxq *rxq = &trans_pcie->rxq;
544         struct iwl_rb_allocator *rba = &trans_pcie->rba;
545         struct device *dev = trans->dev;
546
547         memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
548
549         spin_lock_init(&rxq->lock);
550         spin_lock_init(&rba->lock);
551
552         if (WARN_ON(rxq->bd || rxq->rb_stts))
553                 return -EINVAL;
554
555         /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
556         rxq->bd = dma_zalloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
557                                       &rxq->bd_dma, GFP_KERNEL);
558         if (!rxq->bd)
559                 goto err_bd;
560
561         /*Allocate the driver's pointer to receive buffer status */
562         rxq->rb_stts = dma_zalloc_coherent(dev, sizeof(*rxq->rb_stts),
563                                            &rxq->rb_stts_dma, GFP_KERNEL);
564         if (!rxq->rb_stts)
565                 goto err_rb_stts;
566
567         return 0;
568
569 err_rb_stts:
570         dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
571                           rxq->bd, rxq->bd_dma);
572         rxq->bd_dma = 0;
573         rxq->bd = NULL;
574 err_bd:
575         return -ENOMEM;
576 }
577
578 static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
579 {
580         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
581         u32 rb_size;
582         const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
583
584         if (trans_pcie->rx_buf_size_8k)
585                 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
586         else
587                 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
588
589         /* Stop Rx DMA */
590         iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
591         /* reset and flush pointers */
592         iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_RBDCB_WPTR, 0);
593         iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_FLUSH_RB_REQ, 0);
594         iwl_write_direct32(trans, FH_RSCSR_CHNL0_RDPTR, 0);
595
596         /* Reset driver's Rx queue write index */
597         iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
598
599         /* Tell device where to find RBD circular buffer in DRAM */
600         iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
601                            (u32)(rxq->bd_dma >> 8));
602
603         /* Tell device where in DRAM to update its Rx status */
604         iwl_write_direct32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
605                            rxq->rb_stts_dma >> 4);
606
607         /* Enable Rx DMA
608          * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
609          *      the credit mechanism in 5000 HW RX FIFO
610          * Direct rx interrupts to hosts
611          * Rx buffer size 4 or 8k
612          * RB timeout 0x10
613          * 256 RBDs
614          */
615         iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
616                            FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
617                            FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
618                            FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
619                            rb_size|
620                            (RX_RB_TIMEOUT << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
621                            (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
622
623         /* Set interrupt coalescing timer to default (2048 usecs) */
624         iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
625
626         /* W/A for interrupt coalescing bug in 7260 and 3160 */
627         if (trans->cfg->host_interrupt_operation_mode)
628                 iwl_set_bit(trans, CSR_INT_COALESCING, IWL_HOST_INT_OPER_MODE);
629 }
630
631 static void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq)
632 {
633         int i;
634
635         lockdep_assert_held(&rxq->lock);
636
637         INIT_LIST_HEAD(&rxq->rx_free);
638         INIT_LIST_HEAD(&rxq->rx_used);
639         rxq->free_count = 0;
640         rxq->used_count = 0;
641
642         for (i = 0; i < RX_QUEUE_SIZE; i++)
643                 list_add(&rxq->pool[i].list, &rxq->rx_used);
644 }
645
646 static void iwl_pcie_rx_init_rba(struct iwl_rb_allocator *rba)
647 {
648         int i;
649
650         lockdep_assert_held(&rba->lock);
651
652         INIT_LIST_HEAD(&rba->rbd_allocated);
653         INIT_LIST_HEAD(&rba->rbd_empty);
654
655         for (i = 0; i < RX_POOL_SIZE; i++)
656                 list_add(&rba->pool[i].list, &rba->rbd_empty);
657 }
658
659 static void iwl_pcie_rx_free_rba(struct iwl_trans *trans)
660 {
661         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
662         struct iwl_rb_allocator *rba = &trans_pcie->rba;
663         int i;
664
665         lockdep_assert_held(&rba->lock);
666
667         for (i = 0; i < RX_POOL_SIZE; i++) {
668                 if (!rba->pool[i].page)
669                         continue;
670                 dma_unmap_page(trans->dev, rba->pool[i].page_dma,
671                                PAGE_SIZE << trans_pcie->rx_page_order,
672                                DMA_FROM_DEVICE);
673                 __free_pages(rba->pool[i].page, trans_pcie->rx_page_order);
674                 rba->pool[i].page = NULL;
675         }
676 }
677
678 int iwl_pcie_rx_init(struct iwl_trans *trans)
679 {
680         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
681         struct iwl_rxq *rxq = &trans_pcie->rxq;
682         struct iwl_rb_allocator *rba = &trans_pcie->rba;
683         int i, err;
684
685         if (!rxq->bd) {
686                 err = iwl_pcie_rx_alloc(trans);
687                 if (err)
688                         return err;
689         }
690         if (!rba->alloc_wq)
691                 rba->alloc_wq = alloc_workqueue("rb_allocator",
692                                                 WQ_HIGHPRI | WQ_UNBOUND, 1);
693         INIT_WORK(&rba->rx_alloc, iwl_pcie_rx_allocator_work);
694
695         spin_lock(&rba->lock);
696         atomic_set(&rba->req_pending, 0);
697         atomic_set(&rba->req_ready, 0);
698         /* free all first - we might be reconfigured for a different size */
699         iwl_pcie_rx_free_rba(trans);
700         iwl_pcie_rx_init_rba(rba);
701         spin_unlock(&rba->lock);
702
703         spin_lock(&rxq->lock);
704
705         /* free all first - we might be reconfigured for a different size */
706         iwl_pcie_rxq_free_rbs(trans);
707         iwl_pcie_rx_init_rxb_lists(rxq);
708
709         for (i = 0; i < RX_QUEUE_SIZE; i++)
710                 rxq->queue[i] = NULL;
711
712         /* Set us so that we have processed and used all buffers, but have
713          * not restocked the Rx queue with fresh buffers */
714         rxq->read = rxq->write = 0;
715         rxq->write_actual = 0;
716         memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
717         spin_unlock(&rxq->lock);
718
719         iwl_pcie_rx_replenish(trans);
720
721         iwl_pcie_rx_hw_init(trans, rxq);
722
723         spin_lock(&rxq->lock);
724         iwl_pcie_rxq_inc_wr_ptr(trans);
725         spin_unlock(&rxq->lock);
726
727         return 0;
728 }
729
730 void iwl_pcie_rx_free(struct iwl_trans *trans)
731 {
732         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
733         struct iwl_rxq *rxq = &trans_pcie->rxq;
734         struct iwl_rb_allocator *rba = &trans_pcie->rba;
735
736         /*if rxq->bd is NULL, it means that nothing has been allocated,
737          * exit now */
738         if (!rxq->bd) {
739                 IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
740                 return;
741         }
742
743         cancel_work_sync(&rba->rx_alloc);
744         if (rba->alloc_wq) {
745                 destroy_workqueue(rba->alloc_wq);
746                 rba->alloc_wq = NULL;
747         }
748
749         spin_lock(&rba->lock);
750         iwl_pcie_rx_free_rba(trans);
751         spin_unlock(&rba->lock);
752
753         spin_lock(&rxq->lock);
754         iwl_pcie_rxq_free_rbs(trans);
755         spin_unlock(&rxq->lock);
756
757         dma_free_coherent(trans->dev, sizeof(__le32) * RX_QUEUE_SIZE,
758                           rxq->bd, rxq->bd_dma);
759         rxq->bd_dma = 0;
760         rxq->bd = NULL;
761
762         if (rxq->rb_stts)
763                 dma_free_coherent(trans->dev,
764                                   sizeof(struct iwl_rb_status),
765                                   rxq->rb_stts, rxq->rb_stts_dma);
766         else
767                 IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
768         rxq->rb_stts_dma = 0;
769         rxq->rb_stts = NULL;
770 }
771
772 /*
773  * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
774  *
775  * Called when a RBD can be reused. The RBD is transferred to the allocator.
776  * When there are 2 empty RBDs - a request for allocation is posted
777  */
778 static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
779                                   struct iwl_rx_mem_buffer *rxb,
780                                   struct iwl_rxq *rxq)
781 {
782         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
783         struct iwl_rb_allocator *rba = &trans_pcie->rba;
784
785         /* Count the used RBDs */
786         rxq->used_count++;
787
788         /* Move the RBD to the used list, will be moved to allocator in batches
789          * before claiming or posting a request*/
790         list_add_tail(&rxb->list, &rxq->rx_used);
791
792         /* If we have RX_POST_REQ_ALLOC new released rx buffers -
793          * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
794          * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
795          * after but we still need to post another request.
796          */
797         if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
798                 /* Move the 2 RBDs to the allocator ownership.
799                  Allocator has another 6 from pool for the request completion*/
800                 spin_lock(&rba->lock);
801                 list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
802                 spin_unlock(&rba->lock);
803
804                 atomic_inc(&rba->req_pending);
805                 queue_work(rba->alloc_wq, &rba->rx_alloc);
806         }
807 }
808
809 static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
810                                 struct iwl_rx_mem_buffer *rxb)
811 {
812         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
813         struct iwl_rxq *rxq = &trans_pcie->rxq;
814         struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
815         bool page_stolen = false;
816         int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
817         u32 offset = 0;
818
819         if (WARN_ON(!rxb))
820                 return;
821
822         dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
823
824         while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
825                 struct iwl_rx_packet *pkt;
826                 struct iwl_device_cmd *cmd;
827                 u16 sequence;
828                 bool reclaim;
829                 int index, cmd_index, err, len;
830                 struct iwl_rx_cmd_buffer rxcb = {
831                         ._offset = offset,
832                         ._rx_page_order = trans_pcie->rx_page_order,
833                         ._page = rxb->page,
834                         ._page_stolen = false,
835                         .truesize = max_len,
836                 };
837
838                 pkt = rxb_addr(&rxcb);
839
840                 if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
841                         break;
842
843                 IWL_DEBUG_RX(trans,
844                              "cmd at offset %d: %s (0x%.2x, seq 0x%x)\n",
845                              rxcb._offset,
846                              get_cmd_string(trans_pcie, pkt->hdr.cmd),
847                              pkt->hdr.cmd, le16_to_cpu(pkt->hdr.sequence));
848
849                 len = iwl_rx_packet_len(pkt);
850                 len += sizeof(u32); /* account for status word */
851                 trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
852                 trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
853
854                 /* Reclaim a command buffer only if this packet is a response
855                  *   to a (driver-originated) command.
856                  * If the packet (e.g. Rx frame) originated from uCode,
857                  *   there is no command buffer to reclaim.
858                  * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
859                  *   but apparently a few don't get set; catch them here. */
860                 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
861                 if (reclaim) {
862                         int i;
863
864                         for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
865                                 if (trans_pcie->no_reclaim_cmds[i] ==
866                                                         pkt->hdr.cmd) {
867                                         reclaim = false;
868                                         break;
869                                 }
870                         }
871                 }
872
873                 sequence = le16_to_cpu(pkt->hdr.sequence);
874                 index = SEQ_TO_INDEX(sequence);
875                 cmd_index = get_cmd_index(&txq->q, index);
876
877                 if (reclaim)
878                         cmd = txq->entries[cmd_index].cmd;
879                 else
880                         cmd = NULL;
881
882                 err = iwl_op_mode_rx(trans->op_mode, &rxcb, cmd);
883
884                 if (reclaim) {
885                         kzfree(txq->entries[cmd_index].free_buf);
886                         txq->entries[cmd_index].free_buf = NULL;
887                 }
888
889                 /*
890                  * After here, we should always check rxcb._page_stolen,
891                  * if it is true then one of the handlers took the page.
892                  */
893
894                 if (reclaim) {
895                         /* Invoke any callbacks, transfer the buffer to caller,
896                          * and fire off the (possibly) blocking
897                          * iwl_trans_send_cmd()
898                          * as we reclaim the driver command queue */
899                         if (!rxcb._page_stolen)
900                                 iwl_pcie_hcmd_complete(trans, &rxcb, err);
901                         else
902                                 IWL_WARN(trans, "Claim null rxb?\n");
903                 }
904
905                 page_stolen |= rxcb._page_stolen;
906                 offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
907         }
908
909         /* page was stolen from us -- free our reference */
910         if (page_stolen) {
911                 __free_pages(rxb->page, trans_pcie->rx_page_order);
912                 rxb->page = NULL;
913         }
914
915         /* Reuse the page if possible. For notification packets and
916          * SKBs that fail to Rx correctly, add them back into the
917          * rx_free list for reuse later. */
918         if (rxb->page != NULL) {
919                 rxb->page_dma =
920                         dma_map_page(trans->dev, rxb->page, 0,
921                                      PAGE_SIZE << trans_pcie->rx_page_order,
922                                      DMA_FROM_DEVICE);
923                 if (dma_mapping_error(trans->dev, rxb->page_dma)) {
924                         /*
925                          * free the page(s) as well to not break
926                          * the invariant that the items on the used
927                          * list have no page(s)
928                          */
929                         __free_pages(rxb->page, trans_pcie->rx_page_order);
930                         rxb->page = NULL;
931                         iwl_pcie_rx_reuse_rbd(trans, rxb, rxq);
932                 } else {
933                         list_add_tail(&rxb->list, &rxq->rx_free);
934                         rxq->free_count++;
935                 }
936         } else
937                 iwl_pcie_rx_reuse_rbd(trans, rxb, rxq);
938 }
939
940 /*
941  * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
942  */
943 static void iwl_pcie_rx_handle(struct iwl_trans *trans)
944 {
945         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
946         struct iwl_rxq *rxq = &trans_pcie->rxq;
947         u32 r, i, j;
948
949 restart:
950         spin_lock(&rxq->lock);
951         /* uCode's read index (stored in shared DRAM) indicates the last Rx
952          * buffer that the driver may process (last buffer filled by ucode). */
953         r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
954         i = rxq->read;
955
956         /* Rx interrupt, but nothing sent from uCode */
957         if (i == r)
958                 IWL_DEBUG_RX(trans, "HW = SW = %d\n", r);
959
960         while (i != r) {
961                 struct iwl_rx_mem_buffer *rxb;
962
963                 rxb = rxq->queue[i];
964                 rxq->queue[i] = NULL;
965
966                 IWL_DEBUG_RX(trans, "rxbuf: HW = %d, SW = %d (%p)\n",
967                              r, i, rxb);
968                 iwl_pcie_rx_handle_rb(trans, rxb);
969
970                 i = (i + 1) & RX_QUEUE_MASK;
971
972                 /* If we have RX_CLAIM_REQ_ALLOC released rx buffers -
973                  * try to claim the pre-allocated buffers from the allocator */
974                 if (rxq->used_count >= RX_CLAIM_REQ_ALLOC) {
975                         struct iwl_rb_allocator *rba = &trans_pcie->rba;
976                         struct iwl_rx_mem_buffer *out[RX_CLAIM_REQ_ALLOC];
977
978                         /* Add the remaining 6 empty RBDs for allocator use */
979                         spin_lock(&rba->lock);
980                         list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
981                         spin_unlock(&rba->lock);
982
983                         /* If not ready - continue, will try to reclaim later.
984                         * No need to reschedule work - allocator exits only on
985                         * success */
986                         if (!iwl_pcie_rx_allocator_get(trans, out)) {
987                                 /* If success - then RX_CLAIM_REQ_ALLOC
988                                  * buffers were retrieved and should be added
989                                  * to free list */
990                                 rxq->used_count -= RX_CLAIM_REQ_ALLOC;
991                                 for (j = 0; j < RX_CLAIM_REQ_ALLOC; j++) {
992                                         list_add_tail(&out[j]->list,
993                                                       &rxq->rx_free);
994                                         rxq->free_count++;
995                                 }
996                         }
997                 }
998                 /* handle restock for two cases:
999                 * - we just pulled buffers from the allocator
1000                 * - we have 8+ unstolen pages accumulated */
1001                 if (rxq->free_count >=  RX_CLAIM_REQ_ALLOC) {
1002                         rxq->read = i;
1003                         spin_unlock(&rxq->lock);
1004                         iwl_pcie_rxq_restock(trans);
1005                         goto restart;
1006                 }
1007         }
1008
1009         /* Backtrack one entry */
1010         rxq->read = i;
1011         spin_unlock(&rxq->lock);
1012
1013         if (trans_pcie->napi.poll)
1014                 napi_gro_flush(&trans_pcie->napi, false);
1015 }
1016
1017 /*
1018  * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
1019  */
1020 static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
1021 {
1022         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1023
1024         /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
1025         if (trans->cfg->internal_wimax_coex &&
1026             !trans->cfg->apmg_not_supported &&
1027             (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
1028                              APMS_CLK_VAL_MRB_FUNC_MODE) ||
1029              (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
1030                             APMG_PS_CTRL_VAL_RESET_REQ))) {
1031                 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1032                 iwl_op_mode_wimax_active(trans->op_mode);
1033                 wake_up(&trans_pcie->wait_command_queue);
1034                 return;
1035         }
1036
1037         iwl_pcie_dump_csr(trans);
1038         iwl_dump_fh(trans, NULL);
1039
1040         local_bh_disable();
1041         /* The STATUS_FW_ERROR bit is set in this function. This must happen
1042          * before we wake up the command caller, to ensure a proper cleanup. */
1043         iwl_trans_fw_error(trans);
1044         local_bh_enable();
1045
1046         clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1047         wake_up(&trans_pcie->wait_command_queue);
1048 }
1049
1050 static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
1051 {
1052         u32 inta;
1053
1054         lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
1055
1056         trace_iwlwifi_dev_irq(trans->dev);
1057
1058         /* Discover which interrupts are active/pending */
1059         inta = iwl_read32(trans, CSR_INT);
1060
1061         /* the thread will service interrupts and re-enable them */
1062         return inta;
1063 }
1064
1065 /* a device (PCI-E) page is 4096 bytes long */
1066 #define ICT_SHIFT       12
1067 #define ICT_SIZE        (1 << ICT_SHIFT)
1068 #define ICT_COUNT       (ICT_SIZE / sizeof(u32))
1069
1070 /* interrupt handler using ict table, with this interrupt driver will
1071  * stop using INTA register to get device's interrupt, reading this register
1072  * is expensive, device will write interrupts in ICT dram table, increment
1073  * index then will fire interrupt to driver, driver will OR all ICT table
1074  * entries from current index up to table entry with 0 value. the result is
1075  * the interrupt we need to service, driver will set the entries back to 0 and
1076  * set index.
1077  */
1078 static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
1079 {
1080         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1081         u32 inta;
1082         u32 val = 0;
1083         u32 read;
1084
1085         trace_iwlwifi_dev_irq(trans->dev);
1086
1087         /* Ignore interrupt if there's nothing in NIC to service.
1088          * This may be due to IRQ shared with another device,
1089          * or due to sporadic interrupts thrown from our NIC. */
1090         read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1091         trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
1092         if (!read)
1093                 return 0;
1094
1095         /*
1096          * Collect all entries up to the first 0, starting from ict_index;
1097          * note we already read at ict_index.
1098          */
1099         do {
1100                 val |= read;
1101                 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
1102                                 trans_pcie->ict_index, read);
1103                 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1104                 trans_pcie->ict_index =
1105                         ((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
1106
1107                 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1108                 trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
1109                                            read);
1110         } while (read);
1111
1112         /* We should not get this value, just ignore it. */
1113         if (val == 0xffffffff)
1114                 val = 0;
1115
1116         /*
1117          * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1118          * (bit 15 before shifting it to 31) to clear when using interrupt
1119          * coalescing. fortunately, bits 18 and 19 stay set when this happens
1120          * so we use them to decide on the real state of the Rx bit.
1121          * In order words, bit 15 is set if bit 18 or bit 19 are set.
1122          */
1123         if (val & 0xC0000)
1124                 val |= 0x8000;
1125
1126         inta = (0xff & val) | ((0xff00 & val) << 16);
1127         return inta;
1128 }
1129
1130 irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
1131 {
1132         struct iwl_trans *trans = dev_id;
1133         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1134         struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
1135         u32 inta = 0;
1136         u32 handled = 0;
1137
1138         lock_map_acquire(&trans->sync_cmd_lockdep_map);
1139
1140         spin_lock(&trans_pcie->irq_lock);
1141
1142         /* dram interrupt table not set yet,
1143          * use legacy interrupt.
1144          */
1145         if (likely(trans_pcie->use_ict))
1146                 inta = iwl_pcie_int_cause_ict(trans);
1147         else
1148                 inta = iwl_pcie_int_cause_non_ict(trans);
1149
1150         if (iwl_have_debug_level(IWL_DL_ISR)) {
1151                 IWL_DEBUG_ISR(trans,
1152                               "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
1153                               inta, trans_pcie->inta_mask,
1154                               iwl_read32(trans, CSR_INT_MASK),
1155                               iwl_read32(trans, CSR_FH_INT_STATUS));
1156                 if (inta & (~trans_pcie->inta_mask))
1157                         IWL_DEBUG_ISR(trans,
1158                                       "We got a masked interrupt (0x%08x)\n",
1159                                       inta & (~trans_pcie->inta_mask));
1160         }
1161
1162         inta &= trans_pcie->inta_mask;
1163
1164         /*
1165          * Ignore interrupt if there's nothing in NIC to service.
1166          * This may be due to IRQ shared with another device,
1167          * or due to sporadic interrupts thrown from our NIC.
1168          */
1169         if (unlikely(!inta)) {
1170                 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
1171                 /*
1172                  * Re-enable interrupts here since we don't
1173                  * have anything to service
1174                  */
1175                 if (test_bit(STATUS_INT_ENABLED, &trans->status))
1176                         iwl_enable_interrupts(trans);
1177                 spin_unlock(&trans_pcie->irq_lock);
1178                 lock_map_release(&trans->sync_cmd_lockdep_map);
1179                 return IRQ_NONE;
1180         }
1181
1182         if (unlikely(inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1183                 /*
1184                  * Hardware disappeared. It might have
1185                  * already raised an interrupt.
1186                  */
1187                 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1188                 spin_unlock(&trans_pcie->irq_lock);
1189                 goto out;
1190         }
1191
1192         /* Ack/clear/reset pending uCode interrupts.
1193          * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1194          */
1195         /* There is a hardware bug in the interrupt mask function that some
1196          * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
1197          * they are disabled in the CSR_INT_MASK register. Furthermore the
1198          * ICT interrupt handling mechanism has another bug that might cause
1199          * these unmasked interrupts fail to be detected. We workaround the
1200          * hardware bugs here by ACKing all the possible interrupts so that
1201          * interrupt coalescing can still be achieved.
1202          */
1203         iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
1204
1205         if (iwl_have_debug_level(IWL_DL_ISR))
1206                 IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
1207                               inta, iwl_read32(trans, CSR_INT_MASK));
1208
1209         spin_unlock(&trans_pcie->irq_lock);
1210
1211         /* Now service all interrupt bits discovered above. */
1212         if (inta & CSR_INT_BIT_HW_ERR) {
1213                 IWL_ERR(trans, "Hardware error detected.  Restarting.\n");
1214
1215                 /* Tell the device to stop sending interrupts */
1216                 iwl_disable_interrupts(trans);
1217
1218                 isr_stats->hw++;
1219                 iwl_pcie_irq_handle_error(trans);
1220
1221                 handled |= CSR_INT_BIT_HW_ERR;
1222
1223                 goto out;
1224         }
1225
1226         if (iwl_have_debug_level(IWL_DL_ISR)) {
1227                 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1228                 if (inta & CSR_INT_BIT_SCD) {
1229                         IWL_DEBUG_ISR(trans,
1230                                       "Scheduler finished to transmit the frame/frames.\n");
1231                         isr_stats->sch++;
1232                 }
1233
1234                 /* Alive notification via Rx interrupt will do the real work */
1235                 if (inta & CSR_INT_BIT_ALIVE) {
1236                         IWL_DEBUG_ISR(trans, "Alive interrupt\n");
1237                         isr_stats->alive++;
1238                 }
1239         }
1240
1241         /* Safely ignore these bits for debug checks below */
1242         inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1243
1244         /* HW RF KILL switch toggled */
1245         if (inta & CSR_INT_BIT_RF_KILL) {
1246                 bool hw_rfkill;
1247
1248                 hw_rfkill = iwl_is_rfkill_set(trans);
1249                 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
1250                          hw_rfkill ? "disable radio" : "enable radio");
1251
1252                 isr_stats->rfkill++;
1253
1254                 iwl_trans_pcie_rf_kill(trans, hw_rfkill);
1255                 if (hw_rfkill) {
1256                         set_bit(STATUS_RFKILL, &trans->status);
1257                         if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
1258                                                &trans->status))
1259                                 IWL_DEBUG_RF_KILL(trans,
1260                                                   "Rfkill while SYNC HCMD in flight\n");
1261                         wake_up(&trans_pcie->wait_command_queue);
1262                 } else {
1263                         clear_bit(STATUS_RFKILL, &trans->status);
1264                 }
1265
1266                 handled |= CSR_INT_BIT_RF_KILL;
1267         }
1268
1269         /* Chip got too hot and stopped itself */
1270         if (inta & CSR_INT_BIT_CT_KILL) {
1271                 IWL_ERR(trans, "Microcode CT kill error detected.\n");
1272                 isr_stats->ctkill++;
1273                 handled |= CSR_INT_BIT_CT_KILL;
1274         }
1275
1276         /* Error detected by uCode */
1277         if (inta & CSR_INT_BIT_SW_ERR) {
1278                 IWL_ERR(trans, "Microcode SW error detected. "
1279                         " Restarting 0x%X.\n", inta);
1280                 isr_stats->sw++;
1281                 iwl_pcie_irq_handle_error(trans);
1282                 handled |= CSR_INT_BIT_SW_ERR;
1283         }
1284
1285         /* uCode wakes up after power-down sleep */
1286         if (inta & CSR_INT_BIT_WAKEUP) {
1287                 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1288                 iwl_pcie_rxq_check_wrptr(trans);
1289                 iwl_pcie_txq_check_wrptrs(trans);
1290
1291                 isr_stats->wakeup++;
1292
1293                 handled |= CSR_INT_BIT_WAKEUP;
1294         }
1295
1296         /* All uCode command responses, including Tx command responses,
1297          * Rx "responses" (frame-received notification), and other
1298          * notifications from uCode come through here*/
1299         if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1300                     CSR_INT_BIT_RX_PERIODIC)) {
1301                 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
1302                 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1303                         handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1304                         iwl_write32(trans, CSR_FH_INT_STATUS,
1305                                         CSR_FH_INT_RX_MASK);
1306                 }
1307                 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1308                         handled |= CSR_INT_BIT_RX_PERIODIC;
1309                         iwl_write32(trans,
1310                                 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
1311                 }
1312                 /* Sending RX interrupt require many steps to be done in the
1313                  * the device:
1314                  * 1- write interrupt to current index in ICT table.
1315                  * 2- dma RX frame.
1316                  * 3- update RX shared data to indicate last write index.
1317                  * 4- send interrupt.
1318                  * This could lead to RX race, driver could receive RX interrupt
1319                  * but the shared data changes does not reflect this;
1320                  * periodic interrupt will detect any dangling Rx activity.
1321                  */
1322
1323                 /* Disable periodic interrupt; we use it as just a one-shot. */
1324                 iwl_write8(trans, CSR_INT_PERIODIC_REG,
1325                             CSR_INT_PERIODIC_DIS);
1326
1327                 /*
1328                  * Enable periodic interrupt in 8 msec only if we received
1329                  * real RX interrupt (instead of just periodic int), to catch
1330                  * any dangling Rx interrupt.  If it was just the periodic
1331                  * interrupt, there was no dangling Rx activity, and no need
1332                  * to extend the periodic interrupt; one-shot is enough.
1333                  */
1334                 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
1335                         iwl_write8(trans, CSR_INT_PERIODIC_REG,
1336                                    CSR_INT_PERIODIC_ENA);
1337
1338                 isr_stats->rx++;
1339
1340                 local_bh_disable();
1341                 iwl_pcie_rx_handle(trans);
1342                 local_bh_enable();
1343         }
1344
1345         /* This "Tx" DMA channel is used only for loading uCode */
1346         if (inta & CSR_INT_BIT_FH_TX) {
1347                 iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
1348                 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
1349                 isr_stats->tx++;
1350                 handled |= CSR_INT_BIT_FH_TX;
1351                 /* Wake up uCode load routine, now that load is complete */
1352                 trans_pcie->ucode_write_complete = true;
1353                 wake_up(&trans_pcie->ucode_write_waitq);
1354         }
1355
1356         if (inta & ~handled) {
1357                 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
1358                 isr_stats->unhandled++;
1359         }
1360
1361         if (inta & ~(trans_pcie->inta_mask)) {
1362                 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1363                          inta & ~trans_pcie->inta_mask);
1364         }
1365
1366         /* Re-enable all interrupts */
1367         /* only Re-enable if disabled by irq */
1368         if (test_bit(STATUS_INT_ENABLED, &trans->status))
1369                 iwl_enable_interrupts(trans);
1370         /* Re-enable RF_KILL if it occurred */
1371         else if (handled & CSR_INT_BIT_RF_KILL)
1372                 iwl_enable_rfkill_int(trans);
1373
1374 out:
1375         lock_map_release(&trans->sync_cmd_lockdep_map);
1376         return IRQ_HANDLED;
1377 }
1378
1379 /******************************************************************************
1380  *
1381  * ICT functions
1382  *
1383  ******************************************************************************/
1384
1385 /* Free dram table */
1386 void iwl_pcie_free_ict(struct iwl_trans *trans)
1387 {
1388         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1389
1390         if (trans_pcie->ict_tbl) {
1391                 dma_free_coherent(trans->dev, ICT_SIZE,
1392                                   trans_pcie->ict_tbl,
1393                                   trans_pcie->ict_tbl_dma);
1394                 trans_pcie->ict_tbl = NULL;
1395                 trans_pcie->ict_tbl_dma = 0;
1396         }
1397 }
1398
1399 /*
1400  * allocate dram shared table, it is an aligned memory
1401  * block of ICT_SIZE.
1402  * also reset all data related to ICT table interrupt.
1403  */
1404 int iwl_pcie_alloc_ict(struct iwl_trans *trans)
1405 {
1406         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1407
1408         trans_pcie->ict_tbl =
1409                 dma_zalloc_coherent(trans->dev, ICT_SIZE,
1410                                    &trans_pcie->ict_tbl_dma,
1411                                    GFP_KERNEL);
1412         if (!trans_pcie->ict_tbl)
1413                 return -ENOMEM;
1414
1415         /* just an API sanity check ... it is guaranteed to be aligned */
1416         if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
1417                 iwl_pcie_free_ict(trans);
1418                 return -EINVAL;
1419         }
1420
1421         IWL_DEBUG_ISR(trans, "ict dma addr %Lx ict vir addr %p\n",
1422                       (unsigned long long)trans_pcie->ict_tbl_dma,
1423                       trans_pcie->ict_tbl);
1424
1425         return 0;
1426 }
1427
1428 /* Device is going up inform it about using ICT interrupt table,
1429  * also we need to tell the driver to start using ICT interrupt.
1430  */
1431 void iwl_pcie_reset_ict(struct iwl_trans *trans)
1432 {
1433         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1434         u32 val;
1435
1436         if (!trans_pcie->ict_tbl)
1437                 return;
1438
1439         spin_lock(&trans_pcie->irq_lock);
1440         iwl_disable_interrupts(trans);
1441
1442         memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
1443
1444         val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
1445
1446         val |= CSR_DRAM_INT_TBL_ENABLE;
1447         val |= CSR_DRAM_INIT_TBL_WRAP_CHECK;
1448
1449         IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
1450
1451         iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
1452         trans_pcie->use_ict = true;
1453         trans_pcie->ict_index = 0;
1454         iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
1455         iwl_enable_interrupts(trans);
1456         spin_unlock(&trans_pcie->irq_lock);
1457 }
1458
1459 /* Device is going down disable ict interrupt usage */
1460 void iwl_pcie_disable_ict(struct iwl_trans *trans)
1461 {
1462         struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1463
1464         spin_lock(&trans_pcie->irq_lock);
1465         trans_pcie->use_ict = false;
1466         spin_unlock(&trans_pcie->irq_lock);
1467 }
1468
1469 irqreturn_t iwl_pcie_isr(int irq, void *data)
1470 {
1471         struct iwl_trans *trans = data;
1472
1473         if (!trans)
1474                 return IRQ_NONE;
1475
1476         /* Disable (but don't clear!) interrupts here to avoid
1477          * back-to-back ISRs and sporadic interrupts from our NIC.
1478          * If we have something to service, the tasklet will re-enable ints.
1479          * If we *don't* have something, we'll re-enable before leaving here.
1480          */
1481         iwl_write32(trans, CSR_INT_MASK, 0x00000000);
1482
1483         return IRQ_WAKE_THREAD;
1484 }