xref: /freebsd/sys/contrib/dev/iwlwifi/pcie/rx.c (revision a4128aad8503277614f2d214011ef60a19447b83)
1bfcc09ddSBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2bfcc09ddSBjoern A. Zeeb /*
3*a4128aadSBjoern A. Zeeb  * Copyright (C) 2003-2014, 2018-2024 Intel Corporation
4bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6bfcc09ddSBjoern A. Zeeb  */
7bfcc09ddSBjoern A. Zeeb #include <linux/sched.h>
8bfcc09ddSBjoern A. Zeeb #include <linux/wait.h>
9bfcc09ddSBjoern A. Zeeb #include <linux/gfp.h>
10bfcc09ddSBjoern A. Zeeb 
11bfcc09ddSBjoern A. Zeeb #include "iwl-prph.h"
12bfcc09ddSBjoern A. Zeeb #include "iwl-io.h"
13bfcc09ddSBjoern A. Zeeb #include "internal.h"
14bfcc09ddSBjoern A. Zeeb #include "iwl-op-mode.h"
15bfcc09ddSBjoern A. Zeeb #include "iwl-context-info-gen3.h"
16bfcc09ddSBjoern A. Zeeb 
17bfcc09ddSBjoern A. Zeeb /******************************************************************************
18bfcc09ddSBjoern A. Zeeb  *
19bfcc09ddSBjoern A. Zeeb  * RX path functions
20bfcc09ddSBjoern A. Zeeb  *
21bfcc09ddSBjoern A. Zeeb  ******************************************************************************/
22bfcc09ddSBjoern A. Zeeb 
23bfcc09ddSBjoern A. Zeeb /*
24bfcc09ddSBjoern A. Zeeb  * Rx theory of operation
25bfcc09ddSBjoern A. Zeeb  *
26bfcc09ddSBjoern A. Zeeb  * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
27bfcc09ddSBjoern A. Zeeb  * each of which point to Receive Buffers to be filled by the NIC.  These get
28bfcc09ddSBjoern A. Zeeb  * used not only for Rx frames, but for any command response or notification
29bfcc09ddSBjoern A. Zeeb  * from the NIC.  The driver and NIC manage the Rx buffers by means
30bfcc09ddSBjoern A. Zeeb  * of indexes into the circular buffer.
31bfcc09ddSBjoern A. Zeeb  *
32bfcc09ddSBjoern A. Zeeb  * Rx Queue Indexes
33bfcc09ddSBjoern A. Zeeb  * The host/firmware share two index registers for managing the Rx buffers.
34bfcc09ddSBjoern A. Zeeb  *
35bfcc09ddSBjoern A. Zeeb  * The READ index maps to the first position that the firmware may be writing
36bfcc09ddSBjoern A. Zeeb  * to -- the driver can read up to (but not including) this position and get
37bfcc09ddSBjoern A. Zeeb  * good data.
38bfcc09ddSBjoern A. Zeeb  * The READ index is managed by the firmware once the card is enabled.
39bfcc09ddSBjoern A. Zeeb  *
40bfcc09ddSBjoern A. Zeeb  * The WRITE index maps to the last position the driver has read from -- the
41bfcc09ddSBjoern A. Zeeb  * position preceding WRITE is the last slot the firmware can place a packet.
42bfcc09ddSBjoern A. Zeeb  *
43bfcc09ddSBjoern A. Zeeb  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
44bfcc09ddSBjoern A. Zeeb  * WRITE = READ.
45bfcc09ddSBjoern A. Zeeb  *
46bfcc09ddSBjoern A. Zeeb  * During initialization, the host sets up the READ queue position to the first
47bfcc09ddSBjoern A. Zeeb  * INDEX position, and WRITE to the last (READ - 1 wrapped)
48bfcc09ddSBjoern A. Zeeb  *
49bfcc09ddSBjoern A. Zeeb  * When the firmware places a packet in a buffer, it will advance the READ index
50bfcc09ddSBjoern A. Zeeb  * and fire the RX interrupt.  The driver can then query the READ index and
51bfcc09ddSBjoern A. Zeeb  * process as many packets as possible, moving the WRITE index forward as it
52bfcc09ddSBjoern A. Zeeb  * resets the Rx queue buffers with new memory.
53bfcc09ddSBjoern A. Zeeb  *
54bfcc09ddSBjoern A. Zeeb  * The management in the driver is as follows:
55bfcc09ddSBjoern A. Zeeb  * + A list of pre-allocated RBDs is stored in iwl->rxq->rx_free.
56bfcc09ddSBjoern A. Zeeb  *   When the interrupt handler is called, the request is processed.
57bfcc09ddSBjoern A. Zeeb  *   The page is either stolen - transferred to the upper layer
58bfcc09ddSBjoern A. Zeeb  *   or reused - added immediately to the iwl->rxq->rx_free list.
59bfcc09ddSBjoern A. Zeeb  * + When the page is stolen - the driver updates the matching queue's used
60bfcc09ddSBjoern A. Zeeb  *   count, detaches the RBD and transfers it to the queue used list.
61bfcc09ddSBjoern A. Zeeb  *   When there are two used RBDs - they are transferred to the allocator empty
62bfcc09ddSBjoern A. Zeeb  *   list. Work is then scheduled for the allocator to start allocating
63bfcc09ddSBjoern A. Zeeb  *   eight buffers.
64bfcc09ddSBjoern A. Zeeb  *   When there are another 6 used RBDs - they are transferred to the allocator
65bfcc09ddSBjoern A. Zeeb  *   empty list and the driver tries to claim the pre-allocated buffers and
66bfcc09ddSBjoern A. Zeeb  *   add them to iwl->rxq->rx_free. If it fails - it continues to claim them
67bfcc09ddSBjoern A. Zeeb  *   until ready.
68bfcc09ddSBjoern A. Zeeb  *   When there are 8+ buffers in the free list - either from allocation or from
69bfcc09ddSBjoern A. Zeeb  *   8 reused unstolen pages - restock is called to update the FW and indexes.
70bfcc09ddSBjoern A. Zeeb  * + In order to make sure the allocator always has RBDs to use for allocation
71bfcc09ddSBjoern A. Zeeb  *   the allocator has initial pool in the size of num_queues*(8-2) - the
72bfcc09ddSBjoern A. Zeeb  *   maximum missing RBDs per allocation request (request posted with 2
73bfcc09ddSBjoern A. Zeeb  *    empty RBDs, there is no guarantee when the other 6 RBDs are supplied).
74bfcc09ddSBjoern A. Zeeb  *   The queues supplies the recycle of the rest of the RBDs.
75bfcc09ddSBjoern A. Zeeb  * + A received packet is processed and handed to the kernel network stack,
76bfcc09ddSBjoern A. Zeeb  *   detached from the iwl->rxq.  The driver 'processed' index is updated.
77bfcc09ddSBjoern A. Zeeb  * + If there are no allocated buffers in iwl->rxq->rx_free,
78bfcc09ddSBjoern A. Zeeb  *   the READ INDEX is not incremented and iwl->status(RX_STALLED) is set.
79bfcc09ddSBjoern A. Zeeb  *   If there were enough free buffers and RX_STALLED is set it is cleared.
80bfcc09ddSBjoern A. Zeeb  *
81bfcc09ddSBjoern A. Zeeb  *
82bfcc09ddSBjoern A. Zeeb  * Driver sequence:
83bfcc09ddSBjoern A. Zeeb  *
84bfcc09ddSBjoern A. Zeeb  * iwl_rxq_alloc()            Allocates rx_free
85bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_replenish()    Replenishes rx_free list from rx_used, and calls
86bfcc09ddSBjoern A. Zeeb  *                            iwl_pcie_rxq_restock.
87bfcc09ddSBjoern A. Zeeb  *                            Used only during initialization.
88bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxq_restock()     Moves available buffers from rx_free into Rx
89bfcc09ddSBjoern A. Zeeb  *                            queue, updates firmware pointers, and updates
90bfcc09ddSBjoern A. Zeeb  *                            the WRITE index.
91bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_allocator()     Background work for allocating pages.
92bfcc09ddSBjoern A. Zeeb  *
93bfcc09ddSBjoern A. Zeeb  * -- enable interrupts --
94bfcc09ddSBjoern A. Zeeb  * ISR - iwl_rx()             Detach iwl_rx_mem_buffers from pool up to the
95bfcc09ddSBjoern A. Zeeb  *                            READ INDEX, detaching the SKB from the pool.
96bfcc09ddSBjoern A. Zeeb  *                            Moves the packet buffer from queue to rx_used.
97bfcc09ddSBjoern A. Zeeb  *                            Posts and claims requests to the allocator.
98bfcc09ddSBjoern A. Zeeb  *                            Calls iwl_pcie_rxq_restock to refill any empty
99bfcc09ddSBjoern A. Zeeb  *                            slots.
100bfcc09ddSBjoern A. Zeeb  *
101bfcc09ddSBjoern A. Zeeb  * RBD life-cycle:
102bfcc09ddSBjoern A. Zeeb  *
103bfcc09ddSBjoern A. Zeeb  * Init:
104bfcc09ddSBjoern A. Zeeb  * rxq.pool -> rxq.rx_used -> rxq.rx_free -> rxq.queue
105bfcc09ddSBjoern A. Zeeb  *
106bfcc09ddSBjoern A. Zeeb  * Regular Receive interrupt:
107bfcc09ddSBjoern A. Zeeb  * Page Stolen:
108bfcc09ddSBjoern A. Zeeb  * rxq.queue -> rxq.rx_used -> allocator.rbd_empty ->
109bfcc09ddSBjoern A. Zeeb  * allocator.rbd_allocated -> rxq.rx_free -> rxq.queue
110bfcc09ddSBjoern A. Zeeb  * Page not Stolen:
111bfcc09ddSBjoern A. Zeeb  * rxq.queue -> rxq.rx_free -> rxq.queue
112bfcc09ddSBjoern A. Zeeb  * ...
113bfcc09ddSBjoern A. Zeeb  *
114bfcc09ddSBjoern A. Zeeb  */
115bfcc09ddSBjoern A. Zeeb 
116bfcc09ddSBjoern A. Zeeb /*
117bfcc09ddSBjoern A. Zeeb  * iwl_rxq_space - Return number of free slots available in queue.
118bfcc09ddSBjoern A. Zeeb  */
119bfcc09ddSBjoern A. Zeeb static int iwl_rxq_space(const struct iwl_rxq *rxq)
120bfcc09ddSBjoern A. Zeeb {
121bfcc09ddSBjoern A. Zeeb 	/* Make sure rx queue size is a power of 2 */
122bfcc09ddSBjoern A. Zeeb 	WARN_ON(rxq->queue_size & (rxq->queue_size - 1));
123bfcc09ddSBjoern A. Zeeb 
124bfcc09ddSBjoern A. Zeeb 	/*
125bfcc09ddSBjoern A. Zeeb 	 * There can be up to (RX_QUEUE_SIZE - 1) free slots, to avoid ambiguity
126bfcc09ddSBjoern A. Zeeb 	 * between empty and completely full queues.
127bfcc09ddSBjoern A. Zeeb 	 * The following is equivalent to modulo by RX_QUEUE_SIZE and is well
128bfcc09ddSBjoern A. Zeeb 	 * defined for negative dividends.
129bfcc09ddSBjoern A. Zeeb 	 */
130bfcc09ddSBjoern A. Zeeb 	return (rxq->read - rxq->write - 1) & (rxq->queue_size - 1);
131bfcc09ddSBjoern A. Zeeb }
132bfcc09ddSBjoern A. Zeeb 
133bfcc09ddSBjoern A. Zeeb /*
134bfcc09ddSBjoern A. Zeeb  * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
135bfcc09ddSBjoern A. Zeeb  */
136bfcc09ddSBjoern A. Zeeb static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
137bfcc09ddSBjoern A. Zeeb {
138bfcc09ddSBjoern A. Zeeb 	return cpu_to_le32((u32)(dma_addr >> 8));
139bfcc09ddSBjoern A. Zeeb }
140bfcc09ddSBjoern A. Zeeb 
141bfcc09ddSBjoern A. Zeeb /*
142bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_stop - stops the Rx DMA
143bfcc09ddSBjoern A. Zeeb  */
144bfcc09ddSBjoern A. Zeeb int iwl_pcie_rx_stop(struct iwl_trans *trans)
145bfcc09ddSBjoern A. Zeeb {
146bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
147bfcc09ddSBjoern A. Zeeb 		/* TODO: remove this once fw does it */
148bfcc09ddSBjoern A. Zeeb 		iwl_write_umac_prph(trans, RFH_RXF_DMA_CFG_GEN3, 0);
149bfcc09ddSBjoern A. Zeeb 		return iwl_poll_umac_prph_bit(trans, RFH_GEN_STATUS_GEN3,
150bfcc09ddSBjoern A. Zeeb 					      RXF_DMA_IDLE, RXF_DMA_IDLE, 1000);
151bfcc09ddSBjoern A. Zeeb 	} else if (trans->trans_cfg->mq_rx_supported) {
152bfcc09ddSBjoern A. Zeeb 		iwl_write_prph(trans, RFH_RXF_DMA_CFG, 0);
153bfcc09ddSBjoern A. Zeeb 		return iwl_poll_prph_bit(trans, RFH_GEN_STATUS,
154bfcc09ddSBjoern A. Zeeb 					   RXF_DMA_IDLE, RXF_DMA_IDLE, 1000);
155bfcc09ddSBjoern A. Zeeb 	} else {
156bfcc09ddSBjoern A. Zeeb 		iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
157bfcc09ddSBjoern A. Zeeb 		return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
158bfcc09ddSBjoern A. Zeeb 					   FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
159bfcc09ddSBjoern A. Zeeb 					   1000);
160bfcc09ddSBjoern A. Zeeb 	}
161bfcc09ddSBjoern A. Zeeb }
162bfcc09ddSBjoern A. Zeeb 
163bfcc09ddSBjoern A. Zeeb /*
164bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
165bfcc09ddSBjoern A. Zeeb  */
166bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans,
167bfcc09ddSBjoern A. Zeeb 				    struct iwl_rxq *rxq)
168bfcc09ddSBjoern A. Zeeb {
169bfcc09ddSBjoern A. Zeeb 	u32 reg;
170bfcc09ddSBjoern A. Zeeb 
171bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&rxq->lock);
172bfcc09ddSBjoern A. Zeeb 
173bfcc09ddSBjoern A. Zeeb 	/*
174bfcc09ddSBjoern A. Zeeb 	 * explicitly wake up the NIC if:
175bfcc09ddSBjoern A. Zeeb 	 * 1. shadow registers aren't enabled
176bfcc09ddSBjoern A. Zeeb 	 * 2. there is a chance that the NIC is asleep
177bfcc09ddSBjoern A. Zeeb 	 */
178bfcc09ddSBjoern A. Zeeb 	if (!trans->trans_cfg->base_params->shadow_reg_enable &&
179bfcc09ddSBjoern A. Zeeb 	    test_bit(STATUS_TPOWER_PMI, &trans->status)) {
180bfcc09ddSBjoern A. Zeeb 		reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
181bfcc09ddSBjoern A. Zeeb 
182bfcc09ddSBjoern A. Zeeb 		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
183bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_INFO(trans, "Rx queue requesting wakeup, GP1 = 0x%x\n",
184bfcc09ddSBjoern A. Zeeb 				       reg);
185bfcc09ddSBjoern A. Zeeb 			iwl_set_bit(trans, CSR_GP_CNTRL,
186bfcc09ddSBjoern A. Zeeb 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
187bfcc09ddSBjoern A. Zeeb 			rxq->need_update = true;
188bfcc09ddSBjoern A. Zeeb 			return;
189bfcc09ddSBjoern A. Zeeb 		}
190bfcc09ddSBjoern A. Zeeb 	}
191bfcc09ddSBjoern A. Zeeb 
192bfcc09ddSBjoern A. Zeeb 	rxq->write_actual = round_down(rxq->write, 8);
193d9836fb4SBjoern A. Zeeb 	if (!trans->trans_cfg->mq_rx_supported)
194d9836fb4SBjoern A. Zeeb 		iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, rxq->write_actual);
195d9836fb4SBjoern A. Zeeb 	else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
196d9836fb4SBjoern A. Zeeb 		iwl_write32(trans, HBUS_TARG_WRPTR, rxq->write_actual |
197d9836fb4SBjoern A. Zeeb 			    HBUS_TARG_WRPTR_RX_Q(rxq->id));
198d9836fb4SBjoern A. Zeeb 	else
199bfcc09ddSBjoern A. Zeeb 		iwl_write32(trans, RFH_Q_FRBDCB_WIDX_TRG(rxq->id),
200bfcc09ddSBjoern A. Zeeb 			    rxq->write_actual);
201bfcc09ddSBjoern A. Zeeb }
202bfcc09ddSBjoern A. Zeeb 
203bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rxq_check_wrptr(struct iwl_trans *trans)
204bfcc09ddSBjoern A. Zeeb {
205bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
206bfcc09ddSBjoern A. Zeeb 	int i;
207bfcc09ddSBjoern A. Zeeb 
208bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
209bfcc09ddSBjoern A. Zeeb 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
210bfcc09ddSBjoern A. Zeeb 
211bfcc09ddSBjoern A. Zeeb 		if (!rxq->need_update)
212bfcc09ddSBjoern A. Zeeb 			continue;
213bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
214bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
215bfcc09ddSBjoern A. Zeeb 		rxq->need_update = false;
216bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
217bfcc09ddSBjoern A. Zeeb 	}
218bfcc09ddSBjoern A. Zeeb }
219bfcc09ddSBjoern A. Zeeb 
220bfcc09ddSBjoern A. Zeeb static void iwl_pcie_restock_bd(struct iwl_trans *trans,
221bfcc09ddSBjoern A. Zeeb 				struct iwl_rxq *rxq,
222bfcc09ddSBjoern A. Zeeb 				struct iwl_rx_mem_buffer *rxb)
223bfcc09ddSBjoern A. Zeeb {
224bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
225bfcc09ddSBjoern A. Zeeb 		struct iwl_rx_transfer_desc *bd = rxq->bd;
226bfcc09ddSBjoern A. Zeeb 
227bfcc09ddSBjoern A. Zeeb 		BUILD_BUG_ON(sizeof(*bd) != 2 * sizeof(u64));
228bfcc09ddSBjoern A. Zeeb 
229bfcc09ddSBjoern A. Zeeb 		bd[rxq->write].addr = cpu_to_le64(rxb->page_dma);
230bfcc09ddSBjoern A. Zeeb 		bd[rxq->write].rbid = cpu_to_le16(rxb->vid);
231bfcc09ddSBjoern A. Zeeb 	} else {
232bfcc09ddSBjoern A. Zeeb 		__le64 *bd = rxq->bd;
233bfcc09ddSBjoern A. Zeeb 
234bfcc09ddSBjoern A. Zeeb 		bd[rxq->write] = cpu_to_le64(rxb->page_dma | rxb->vid);
235bfcc09ddSBjoern A. Zeeb 	}
236bfcc09ddSBjoern A. Zeeb 
237d9836fb4SBjoern A. Zeeb #if defined(__linux__)
238d9836fb4SBjoern A. Zeeb 	IWL_DEBUG_RX(trans, "Assigned virtual RB ID %u to queue %d index %d\n",
239d9836fb4SBjoern A. Zeeb #elif defined(__FreeBSD__)
240bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_PCI_RW(trans, "Assigned virtual RB ID %u to queue %d index %d\n",
241bfcc09ddSBjoern A. Zeeb 		     (u32)rxb->vid, rxq->id, rxq->write);
242d9836fb4SBjoern A. Zeeb #endif
243bfcc09ddSBjoern A. Zeeb }
244bfcc09ddSBjoern A. Zeeb 
245bfcc09ddSBjoern A. Zeeb /*
246bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxmq_restock - restock implementation for multi-queue rx
247bfcc09ddSBjoern A. Zeeb  */
248bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rxmq_restock(struct iwl_trans *trans,
249bfcc09ddSBjoern A. Zeeb 				  struct iwl_rxq *rxq)
250bfcc09ddSBjoern A. Zeeb {
251bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
252bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_mem_buffer *rxb;
253bfcc09ddSBjoern A. Zeeb 
254bfcc09ddSBjoern A. Zeeb 	/*
255bfcc09ddSBjoern A. Zeeb 	 * If the device isn't enabled - no need to try to add buffers...
256bfcc09ddSBjoern A. Zeeb 	 * This can happen when we stop the device and still have an interrupt
257bfcc09ddSBjoern A. Zeeb 	 * pending. We stop the APM before we sync the interrupts because we
258bfcc09ddSBjoern A. Zeeb 	 * have to (see comment there). On the other hand, since the APM is
259bfcc09ddSBjoern A. Zeeb 	 * stopped, we cannot access the HW (in particular not prph).
260bfcc09ddSBjoern A. Zeeb 	 * So don't try to restock if the APM has been already stopped.
261bfcc09ddSBjoern A. Zeeb 	 */
262bfcc09ddSBjoern A. Zeeb 	if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
263bfcc09ddSBjoern A. Zeeb 		return;
264bfcc09ddSBjoern A. Zeeb 
265bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&rxq->lock);
266bfcc09ddSBjoern A. Zeeb 	while (rxq->free_count) {
267bfcc09ddSBjoern A. Zeeb 		/* Get next free Rx buffer, remove from free list */
268bfcc09ddSBjoern A. Zeeb 		rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
269bfcc09ddSBjoern A. Zeeb 				       list);
270bfcc09ddSBjoern A. Zeeb 		list_del(&rxb->list);
271bfcc09ddSBjoern A. Zeeb 		rxb->invalid = false;
272bfcc09ddSBjoern A. Zeeb 		/* some low bits are expected to be unset (depending on hw) */
273bfcc09ddSBjoern A. Zeeb 		WARN_ON(rxb->page_dma & trans_pcie->supported_dma_mask);
274bfcc09ddSBjoern A. Zeeb 		/* Point to Rx buffer via next RBD in circular buffer */
275bfcc09ddSBjoern A. Zeeb 		iwl_pcie_restock_bd(trans, rxq, rxb);
276bfcc09ddSBjoern A. Zeeb 		rxq->write = (rxq->write + 1) & (rxq->queue_size - 1);
277bfcc09ddSBjoern A. Zeeb 		rxq->free_count--;
278bfcc09ddSBjoern A. Zeeb 	}
279bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&rxq->lock);
280bfcc09ddSBjoern A. Zeeb 
281bfcc09ddSBjoern A. Zeeb 	/*
282bfcc09ddSBjoern A. Zeeb 	 * If we've added more space for the firmware to place data, tell it.
283bfcc09ddSBjoern A. Zeeb 	 * Increment device's write pointer in multiples of 8.
284bfcc09ddSBjoern A. Zeeb 	 */
285bfcc09ddSBjoern A. Zeeb 	if (rxq->write_actual != (rxq->write & ~0x7)) {
286bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
287bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
288bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
289bfcc09ddSBjoern A. Zeeb 	}
290bfcc09ddSBjoern A. Zeeb }
291bfcc09ddSBjoern A. Zeeb 
292bfcc09ddSBjoern A. Zeeb /*
293bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxsq_restock - restock implementation for single queue rx
294bfcc09ddSBjoern A. Zeeb  */
295bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rxsq_restock(struct iwl_trans *trans,
296bfcc09ddSBjoern A. Zeeb 				  struct iwl_rxq *rxq)
297bfcc09ddSBjoern A. Zeeb {
298bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_mem_buffer *rxb;
299bfcc09ddSBjoern A. Zeeb 
300bfcc09ddSBjoern A. Zeeb 	/*
301bfcc09ddSBjoern A. Zeeb 	 * If the device isn't enabled - not need to try to add buffers...
302bfcc09ddSBjoern A. Zeeb 	 * This can happen when we stop the device and still have an interrupt
303bfcc09ddSBjoern A. Zeeb 	 * pending. We stop the APM before we sync the interrupts because we
304bfcc09ddSBjoern A. Zeeb 	 * have to (see comment there). On the other hand, since the APM is
305bfcc09ddSBjoern A. Zeeb 	 * stopped, we cannot access the HW (in particular not prph).
306bfcc09ddSBjoern A. Zeeb 	 * So don't try to restock if the APM has been already stopped.
307bfcc09ddSBjoern A. Zeeb 	 */
308bfcc09ddSBjoern A. Zeeb 	if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
309bfcc09ddSBjoern A. Zeeb 		return;
310bfcc09ddSBjoern A. Zeeb 
311bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&rxq->lock);
312bfcc09ddSBjoern A. Zeeb 	while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
313bfcc09ddSBjoern A. Zeeb 		__le32 *bd = (__le32 *)rxq->bd;
314bfcc09ddSBjoern A. Zeeb 		/* The overwritten rxb must be a used one */
315bfcc09ddSBjoern A. Zeeb 		rxb = rxq->queue[rxq->write];
316bfcc09ddSBjoern A. Zeeb 		BUG_ON(rxb && rxb->page);
317bfcc09ddSBjoern A. Zeeb 
318bfcc09ddSBjoern A. Zeeb 		/* Get next free Rx buffer, remove from free list */
319bfcc09ddSBjoern A. Zeeb 		rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
320bfcc09ddSBjoern A. Zeeb 				       list);
321bfcc09ddSBjoern A. Zeeb 		list_del(&rxb->list);
322bfcc09ddSBjoern A. Zeeb 		rxb->invalid = false;
323bfcc09ddSBjoern A. Zeeb 
324bfcc09ddSBjoern A. Zeeb 		/* Point to Rx buffer via next RBD in circular buffer */
325bfcc09ddSBjoern A. Zeeb 		bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
326bfcc09ddSBjoern A. Zeeb 		rxq->queue[rxq->write] = rxb;
327bfcc09ddSBjoern A. Zeeb 		rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
328bfcc09ddSBjoern A. Zeeb 		rxq->free_count--;
329bfcc09ddSBjoern A. Zeeb 	}
330bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&rxq->lock);
331bfcc09ddSBjoern A. Zeeb 
332bfcc09ddSBjoern A. Zeeb 	/* If we've added more space for the firmware to place data, tell it.
333bfcc09ddSBjoern A. Zeeb 	 * Increment device's write pointer in multiples of 8. */
334bfcc09ddSBjoern A. Zeeb 	if (rxq->write_actual != (rxq->write & ~0x7)) {
335bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
336bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
337bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
338bfcc09ddSBjoern A. Zeeb 	}
339bfcc09ddSBjoern A. Zeeb }
340bfcc09ddSBjoern A. Zeeb 
341bfcc09ddSBjoern A. Zeeb /*
342bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
343bfcc09ddSBjoern A. Zeeb  *
344bfcc09ddSBjoern A. Zeeb  * If there are slots in the RX queue that need to be restocked,
345bfcc09ddSBjoern A. Zeeb  * and we have free pre-allocated buffers, fill the ranks as much
346bfcc09ddSBjoern A. Zeeb  * as we can, pulling from rx_free.
347bfcc09ddSBjoern A. Zeeb  *
348bfcc09ddSBjoern A. Zeeb  * This moves the 'write' index forward to catch up with 'processed', and
349bfcc09ddSBjoern A. Zeeb  * also updates the memory address in the firmware to reference the new
350bfcc09ddSBjoern A. Zeeb  * target buffer.
351bfcc09ddSBjoern A. Zeeb  */
352bfcc09ddSBjoern A. Zeeb static
353bfcc09ddSBjoern A. Zeeb void iwl_pcie_rxq_restock(struct iwl_trans *trans, struct iwl_rxq *rxq)
354bfcc09ddSBjoern A. Zeeb {
355bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->mq_rx_supported)
356bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxmq_restock(trans, rxq);
357bfcc09ddSBjoern A. Zeeb 	else
358bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxsq_restock(trans, rxq);
359bfcc09ddSBjoern A. Zeeb }
360bfcc09ddSBjoern A. Zeeb 
361bfcc09ddSBjoern A. Zeeb /*
362bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_alloc_page - allocates and returns a page.
363bfcc09ddSBjoern A. Zeeb  *
364bfcc09ddSBjoern A. Zeeb  */
365bfcc09ddSBjoern A. Zeeb static struct page *iwl_pcie_rx_alloc_page(struct iwl_trans *trans,
366bfcc09ddSBjoern A. Zeeb 					   u32 *offset, gfp_t priority)
367bfcc09ddSBjoern A. Zeeb {
368bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
369bfcc09ddSBjoern A. Zeeb 	unsigned int rbsize = iwl_trans_get_rb_size(trans_pcie->rx_buf_size);
370bfcc09ddSBjoern A. Zeeb 	unsigned int allocsize = PAGE_SIZE << trans_pcie->rx_page_order;
371bfcc09ddSBjoern A. Zeeb 	struct page *page;
372bfcc09ddSBjoern A. Zeeb 	gfp_t gfp_mask = priority;
373bfcc09ddSBjoern A. Zeeb 
374bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->rx_page_order > 0)
375bfcc09ddSBjoern A. Zeeb 		gfp_mask |= __GFP_COMP;
376bfcc09ddSBjoern A. Zeeb 
377bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->alloc_page) {
378bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&trans_pcie->alloc_page_lock);
379bfcc09ddSBjoern A. Zeeb 		/* recheck */
380bfcc09ddSBjoern A. Zeeb 		if (trans_pcie->alloc_page) {
381bfcc09ddSBjoern A. Zeeb 			*offset = trans_pcie->alloc_page_used;
382bfcc09ddSBjoern A. Zeeb 			page = trans_pcie->alloc_page;
383bfcc09ddSBjoern A. Zeeb 			trans_pcie->alloc_page_used += rbsize;
384bfcc09ddSBjoern A. Zeeb 			if (trans_pcie->alloc_page_used >= allocsize)
385bfcc09ddSBjoern A. Zeeb 				trans_pcie->alloc_page = NULL;
386bfcc09ddSBjoern A. Zeeb 			else
387bfcc09ddSBjoern A. Zeeb 				get_page(page);
388bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&trans_pcie->alloc_page_lock);
389bfcc09ddSBjoern A. Zeeb 			return page;
390bfcc09ddSBjoern A. Zeeb 		}
391bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&trans_pcie->alloc_page_lock);
392bfcc09ddSBjoern A. Zeeb 	}
393bfcc09ddSBjoern A. Zeeb 
394bfcc09ddSBjoern A. Zeeb 	/* Alloc a new receive buffer */
395bfcc09ddSBjoern A. Zeeb 	page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
396bfcc09ddSBjoern A. Zeeb 	if (!page) {
397bfcc09ddSBjoern A. Zeeb 		if (net_ratelimit())
398bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_INFO(trans, "alloc_pages failed, order: %d\n",
399bfcc09ddSBjoern A. Zeeb 				       trans_pcie->rx_page_order);
400bfcc09ddSBjoern A. Zeeb 		/*
401bfcc09ddSBjoern A. Zeeb 		 * Issue an error if we don't have enough pre-allocated
402bfcc09ddSBjoern A. Zeeb 		  * buffers.
403bfcc09ddSBjoern A. Zeeb 		 */
404bfcc09ddSBjoern A. Zeeb 		if (!(gfp_mask & __GFP_NOWARN) && net_ratelimit())
405bfcc09ddSBjoern A. Zeeb 			IWL_CRIT(trans,
406bfcc09ddSBjoern A. Zeeb 				 "Failed to alloc_pages\n");
407bfcc09ddSBjoern A. Zeeb 		return NULL;
408bfcc09ddSBjoern A. Zeeb 	}
409bfcc09ddSBjoern A. Zeeb 
410bfcc09ddSBjoern A. Zeeb 	if (2 * rbsize <= allocsize) {
411bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&trans_pcie->alloc_page_lock);
412bfcc09ddSBjoern A. Zeeb 		if (!trans_pcie->alloc_page) {
413bfcc09ddSBjoern A. Zeeb 			get_page(page);
414bfcc09ddSBjoern A. Zeeb 			trans_pcie->alloc_page = page;
415bfcc09ddSBjoern A. Zeeb 			trans_pcie->alloc_page_used = rbsize;
416bfcc09ddSBjoern A. Zeeb 		}
417bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&trans_pcie->alloc_page_lock);
418bfcc09ddSBjoern A. Zeeb 	}
419bfcc09ddSBjoern A. Zeeb 
420bfcc09ddSBjoern A. Zeeb 	*offset = 0;
421bfcc09ddSBjoern A. Zeeb 	return page;
422bfcc09ddSBjoern A. Zeeb }
423bfcc09ddSBjoern A. Zeeb 
424bfcc09ddSBjoern A. Zeeb /*
425bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
426bfcc09ddSBjoern A. Zeeb  *
427bfcc09ddSBjoern A. Zeeb  * A used RBD is an Rx buffer that has been given to the stack. To use it again
428bfcc09ddSBjoern A. Zeeb  * a page must be allocated and the RBD must point to the page. This function
429bfcc09ddSBjoern A. Zeeb  * doesn't change the HW pointer but handles the list of pages that is used by
430bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
431bfcc09ddSBjoern A. Zeeb  * allocated buffers.
432bfcc09ddSBjoern A. Zeeb  */
433bfcc09ddSBjoern A. Zeeb void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,
434bfcc09ddSBjoern A. Zeeb 			    struct iwl_rxq *rxq)
435bfcc09ddSBjoern A. Zeeb {
436bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
437bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_mem_buffer *rxb;
438bfcc09ddSBjoern A. Zeeb 	struct page *page;
439bfcc09ddSBjoern A. Zeeb 
440bfcc09ddSBjoern A. Zeeb 	while (1) {
441bfcc09ddSBjoern A. Zeeb 		unsigned int offset;
442bfcc09ddSBjoern A. Zeeb 
443bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
444bfcc09ddSBjoern A. Zeeb 		if (list_empty(&rxq->rx_used)) {
445bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&rxq->lock);
446bfcc09ddSBjoern A. Zeeb 			return;
447bfcc09ddSBjoern A. Zeeb 		}
448bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
449bfcc09ddSBjoern A. Zeeb 
450bfcc09ddSBjoern A. Zeeb 		page = iwl_pcie_rx_alloc_page(trans, &offset, priority);
451bfcc09ddSBjoern A. Zeeb 		if (!page)
452bfcc09ddSBjoern A. Zeeb 			return;
453bfcc09ddSBjoern A. Zeeb 
454bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
455bfcc09ddSBjoern A. Zeeb 
456bfcc09ddSBjoern A. Zeeb 		if (list_empty(&rxq->rx_used)) {
457bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&rxq->lock);
458bfcc09ddSBjoern A. Zeeb 			__free_pages(page, trans_pcie->rx_page_order);
459bfcc09ddSBjoern A. Zeeb 			return;
460bfcc09ddSBjoern A. Zeeb 		}
461bfcc09ddSBjoern A. Zeeb 		rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
462bfcc09ddSBjoern A. Zeeb 				       list);
463bfcc09ddSBjoern A. Zeeb 		list_del(&rxb->list);
464bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
465bfcc09ddSBjoern A. Zeeb 
466bfcc09ddSBjoern A. Zeeb 		BUG_ON(rxb->page);
467bfcc09ddSBjoern A. Zeeb 		rxb->page = page;
468bfcc09ddSBjoern A. Zeeb 		rxb->offset = offset;
469bfcc09ddSBjoern A. Zeeb 		/* Get physical address of the RB */
470bfcc09ddSBjoern A. Zeeb 		rxb->page_dma =
471bfcc09ddSBjoern A. Zeeb 			dma_map_page(trans->dev, page, rxb->offset,
472bfcc09ddSBjoern A. Zeeb 				     trans_pcie->rx_buf_bytes,
473bfcc09ddSBjoern A. Zeeb 				     DMA_FROM_DEVICE);
474bfcc09ddSBjoern A. Zeeb 		if (dma_mapping_error(trans->dev, rxb->page_dma)) {
475bfcc09ddSBjoern A. Zeeb 			rxb->page = NULL;
476bfcc09ddSBjoern A. Zeeb 			spin_lock_bh(&rxq->lock);
477bfcc09ddSBjoern A. Zeeb 			list_add(&rxb->list, &rxq->rx_used);
478bfcc09ddSBjoern A. Zeeb 			spin_unlock_bh(&rxq->lock);
479bfcc09ddSBjoern A. Zeeb 			__free_pages(page, trans_pcie->rx_page_order);
480bfcc09ddSBjoern A. Zeeb 			return;
481bfcc09ddSBjoern A. Zeeb 		}
482bfcc09ddSBjoern A. Zeeb 
483bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
484bfcc09ddSBjoern A. Zeeb 
485bfcc09ddSBjoern A. Zeeb 		list_add_tail(&rxb->list, &rxq->rx_free);
486bfcc09ddSBjoern A. Zeeb 		rxq->free_count++;
487bfcc09ddSBjoern A. Zeeb 
488bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
489bfcc09ddSBjoern A. Zeeb 	}
490bfcc09ddSBjoern A. Zeeb }
491bfcc09ddSBjoern A. Zeeb 
492bfcc09ddSBjoern A. Zeeb void iwl_pcie_free_rbs_pool(struct iwl_trans *trans)
493bfcc09ddSBjoern A. Zeeb {
494bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
495bfcc09ddSBjoern A. Zeeb 	int i;
496bfcc09ddSBjoern A. Zeeb 
497bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->rx_pool)
498bfcc09ddSBjoern A. Zeeb 		return;
499bfcc09ddSBjoern A. Zeeb 
500bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < RX_POOL_SIZE(trans_pcie->num_rx_bufs); i++) {
501bfcc09ddSBjoern A. Zeeb 		if (!trans_pcie->rx_pool[i].page)
502bfcc09ddSBjoern A. Zeeb 			continue;
503bfcc09ddSBjoern A. Zeeb 		dma_unmap_page(trans->dev, trans_pcie->rx_pool[i].page_dma,
504bfcc09ddSBjoern A. Zeeb 			       trans_pcie->rx_buf_bytes, DMA_FROM_DEVICE);
505bfcc09ddSBjoern A. Zeeb 		__free_pages(trans_pcie->rx_pool[i].page,
506bfcc09ddSBjoern A. Zeeb 			     trans_pcie->rx_page_order);
507bfcc09ddSBjoern A. Zeeb 		trans_pcie->rx_pool[i].page = NULL;
508bfcc09ddSBjoern A. Zeeb 	}
509bfcc09ddSBjoern A. Zeeb }
510bfcc09ddSBjoern A. Zeeb 
511bfcc09ddSBjoern A. Zeeb /*
512bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_allocator - Allocates pages in the background for RX queues
513bfcc09ddSBjoern A. Zeeb  *
514bfcc09ddSBjoern A. Zeeb  * Allocates for each received request 8 pages
515bfcc09ddSBjoern A. Zeeb  * Called as a scheduled work item.
516bfcc09ddSBjoern A. Zeeb  */
517bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_allocator(struct iwl_trans *trans)
518bfcc09ddSBjoern A. Zeeb {
519bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
520bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
521bfcc09ddSBjoern A. Zeeb 	struct list_head local_empty;
522bfcc09ddSBjoern A. Zeeb 	int pending = atomic_read(&rba->req_pending);
523bfcc09ddSBjoern A. Zeeb 
524bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TPT(trans, "Pending allocation requests = %d\n", pending);
525bfcc09ddSBjoern A. Zeeb 
526bfcc09ddSBjoern A. Zeeb 	/* If we were scheduled - there is at least one request */
527bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&rba->lock);
528bfcc09ddSBjoern A. Zeeb 	/* swap out the rba->rbd_empty to a local list */
529bfcc09ddSBjoern A. Zeeb 	list_replace_init(&rba->rbd_empty, &local_empty);
530bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&rba->lock);
531bfcc09ddSBjoern A. Zeeb 
532bfcc09ddSBjoern A. Zeeb 	while (pending) {
533bfcc09ddSBjoern A. Zeeb 		int i;
534bfcc09ddSBjoern A. Zeeb 		LIST_HEAD(local_allocated);
535bfcc09ddSBjoern A. Zeeb 		gfp_t gfp_mask = GFP_KERNEL;
536bfcc09ddSBjoern A. Zeeb 
537bfcc09ddSBjoern A. Zeeb 		/* Do not post a warning if there are only a few requests */
538bfcc09ddSBjoern A. Zeeb 		if (pending < RX_PENDING_WATERMARK)
539bfcc09ddSBjoern A. Zeeb 			gfp_mask |= __GFP_NOWARN;
540bfcc09ddSBjoern A. Zeeb 
541bfcc09ddSBjoern A. Zeeb 		for (i = 0; i < RX_CLAIM_REQ_ALLOC;) {
542bfcc09ddSBjoern A. Zeeb 			struct iwl_rx_mem_buffer *rxb;
543bfcc09ddSBjoern A. Zeeb 			struct page *page;
544bfcc09ddSBjoern A. Zeeb 
545bfcc09ddSBjoern A. Zeeb 			/* List should never be empty - each reused RBD is
546bfcc09ddSBjoern A. Zeeb 			 * returned to the list, and initial pool covers any
547bfcc09ddSBjoern A. Zeeb 			 * possible gap between the time the page is allocated
548bfcc09ddSBjoern A. Zeeb 			 * to the time the RBD is added.
549bfcc09ddSBjoern A. Zeeb 			 */
550bfcc09ddSBjoern A. Zeeb 			BUG_ON(list_empty(&local_empty));
551bfcc09ddSBjoern A. Zeeb 			/* Get the first rxb from the rbd list */
552bfcc09ddSBjoern A. Zeeb 			rxb = list_first_entry(&local_empty,
553bfcc09ddSBjoern A. Zeeb 					       struct iwl_rx_mem_buffer, list);
554bfcc09ddSBjoern A. Zeeb 			BUG_ON(rxb->page);
555bfcc09ddSBjoern A. Zeeb 
556bfcc09ddSBjoern A. Zeeb 			/* Alloc a new receive buffer */
557bfcc09ddSBjoern A. Zeeb 			page = iwl_pcie_rx_alloc_page(trans, &rxb->offset,
558bfcc09ddSBjoern A. Zeeb 						      gfp_mask);
559bfcc09ddSBjoern A. Zeeb 			if (!page)
560bfcc09ddSBjoern A. Zeeb 				continue;
561bfcc09ddSBjoern A. Zeeb 			rxb->page = page;
562bfcc09ddSBjoern A. Zeeb 
563bfcc09ddSBjoern A. Zeeb 			/* Get physical address of the RB */
564bfcc09ddSBjoern A. Zeeb 			rxb->page_dma = dma_map_page(trans->dev, page,
565bfcc09ddSBjoern A. Zeeb 						     rxb->offset,
566bfcc09ddSBjoern A. Zeeb 						     trans_pcie->rx_buf_bytes,
567bfcc09ddSBjoern A. Zeeb 						     DMA_FROM_DEVICE);
568bfcc09ddSBjoern A. Zeeb 			if (dma_mapping_error(trans->dev, rxb->page_dma)) {
569bfcc09ddSBjoern A. Zeeb 				rxb->page = NULL;
570bfcc09ddSBjoern A. Zeeb 				__free_pages(page, trans_pcie->rx_page_order);
571bfcc09ddSBjoern A. Zeeb 				continue;
572bfcc09ddSBjoern A. Zeeb 			}
573bfcc09ddSBjoern A. Zeeb 
574bfcc09ddSBjoern A. Zeeb 			/* move the allocated entry to the out list */
575bfcc09ddSBjoern A. Zeeb 			list_move(&rxb->list, &local_allocated);
576bfcc09ddSBjoern A. Zeeb 			i++;
577bfcc09ddSBjoern A. Zeeb 		}
578bfcc09ddSBjoern A. Zeeb 
579bfcc09ddSBjoern A. Zeeb 		atomic_dec(&rba->req_pending);
580bfcc09ddSBjoern A. Zeeb 		pending--;
581bfcc09ddSBjoern A. Zeeb 
582bfcc09ddSBjoern A. Zeeb 		if (!pending) {
583bfcc09ddSBjoern A. Zeeb 			pending = atomic_read(&rba->req_pending);
584bfcc09ddSBjoern A. Zeeb 			if (pending)
585bfcc09ddSBjoern A. Zeeb 				IWL_DEBUG_TPT(trans,
586bfcc09ddSBjoern A. Zeeb 					      "Got more pending allocation requests = %d\n",
587bfcc09ddSBjoern A. Zeeb 					      pending);
588bfcc09ddSBjoern A. Zeeb 		}
589bfcc09ddSBjoern A. Zeeb 
590bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rba->lock);
591bfcc09ddSBjoern A. Zeeb 		/* add the allocated rbds to the allocator allocated list */
592bfcc09ddSBjoern A. Zeeb 		list_splice_tail(&local_allocated, &rba->rbd_allocated);
593bfcc09ddSBjoern A. Zeeb 		/* get more empty RBDs for current pending requests */
594bfcc09ddSBjoern A. Zeeb 		list_splice_tail_init(&rba->rbd_empty, &local_empty);
595bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rba->lock);
596bfcc09ddSBjoern A. Zeeb 
597bfcc09ddSBjoern A. Zeeb 		atomic_inc(&rba->req_ready);
598bfcc09ddSBjoern A. Zeeb 
599bfcc09ddSBjoern A. Zeeb 	}
600bfcc09ddSBjoern A. Zeeb 
601bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&rba->lock);
602bfcc09ddSBjoern A. Zeeb 	/* return unused rbds to the allocator empty list */
603bfcc09ddSBjoern A. Zeeb 	list_splice_tail(&local_empty, &rba->rbd_empty);
604bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&rba->lock);
605bfcc09ddSBjoern A. Zeeb 
606bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_TPT(trans, "%s, exit.\n", __func__);
607bfcc09ddSBjoern A. Zeeb }
608bfcc09ddSBjoern A. Zeeb 
609bfcc09ddSBjoern A. Zeeb /*
610bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_allocator_get - returns the pre-allocated pages
611bfcc09ddSBjoern A. Zeeb .*
612bfcc09ddSBjoern A. Zeeb .* Called by queue when the queue posted allocation request and
613bfcc09ddSBjoern A. Zeeb  * has freed 8 RBDs in order to restock itself.
614bfcc09ddSBjoern A. Zeeb  * This function directly moves the allocated RBs to the queue's ownership
615bfcc09ddSBjoern A. Zeeb  * and updates the relevant counters.
616bfcc09ddSBjoern A. Zeeb  */
617bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_allocator_get(struct iwl_trans *trans,
618bfcc09ddSBjoern A. Zeeb 				      struct iwl_rxq *rxq)
619bfcc09ddSBjoern A. Zeeb {
620bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
621bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
622bfcc09ddSBjoern A. Zeeb 	int i;
623bfcc09ddSBjoern A. Zeeb 
624bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&rxq->lock);
625bfcc09ddSBjoern A. Zeeb 
626bfcc09ddSBjoern A. Zeeb 	/*
627bfcc09ddSBjoern A. Zeeb 	 * atomic_dec_if_positive returns req_ready - 1 for any scenario.
628bfcc09ddSBjoern A. Zeeb 	 * If req_ready is 0 atomic_dec_if_positive will return -1 and this
629bfcc09ddSBjoern A. Zeeb 	 * function will return early, as there are no ready requests.
630bfcc09ddSBjoern A. Zeeb 	 * atomic_dec_if_positive will perofrm the *actual* decrement only if
631bfcc09ddSBjoern A. Zeeb 	 * req_ready > 0, i.e. - there are ready requests and the function
632bfcc09ddSBjoern A. Zeeb 	 * hands one request to the caller.
633bfcc09ddSBjoern A. Zeeb 	 */
634bfcc09ddSBjoern A. Zeeb 	if (atomic_dec_if_positive(&rba->req_ready) < 0)
635bfcc09ddSBjoern A. Zeeb 		return;
636bfcc09ddSBjoern A. Zeeb 
637bfcc09ddSBjoern A. Zeeb 	spin_lock(&rba->lock);
638bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < RX_CLAIM_REQ_ALLOC; i++) {
639bfcc09ddSBjoern A. Zeeb 		/* Get next free Rx buffer, remove it from free list */
640bfcc09ddSBjoern A. Zeeb 		struct iwl_rx_mem_buffer *rxb =
641bfcc09ddSBjoern A. Zeeb 			list_first_entry(&rba->rbd_allocated,
642bfcc09ddSBjoern A. Zeeb 					 struct iwl_rx_mem_buffer, list);
643bfcc09ddSBjoern A. Zeeb 
644bfcc09ddSBjoern A. Zeeb 		list_move(&rxb->list, &rxq->rx_free);
645bfcc09ddSBjoern A. Zeeb 	}
646bfcc09ddSBjoern A. Zeeb 	spin_unlock(&rba->lock);
647bfcc09ddSBjoern A. Zeeb 
648bfcc09ddSBjoern A. Zeeb 	rxq->used_count -= RX_CLAIM_REQ_ALLOC;
649bfcc09ddSBjoern A. Zeeb 	rxq->free_count += RX_CLAIM_REQ_ALLOC;
650bfcc09ddSBjoern A. Zeeb }
651bfcc09ddSBjoern A. Zeeb 
652bfcc09ddSBjoern A. Zeeb void iwl_pcie_rx_allocator_work(struct work_struct *data)
653bfcc09ddSBjoern A. Zeeb {
654bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba_p =
655bfcc09ddSBjoern A. Zeeb 		container_of(data, struct iwl_rb_allocator, rx_alloc);
656bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie =
657bfcc09ddSBjoern A. Zeeb 		container_of(rba_p, struct iwl_trans_pcie, rba);
658bfcc09ddSBjoern A. Zeeb 
659bfcc09ddSBjoern A. Zeeb 	iwl_pcie_rx_allocator(trans_pcie->trans);
660bfcc09ddSBjoern A. Zeeb }
661bfcc09ddSBjoern A. Zeeb 
662d9836fb4SBjoern A. Zeeb static int iwl_pcie_free_bd_size(struct iwl_trans *trans)
663bfcc09ddSBjoern A. Zeeb {
664d9836fb4SBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
665d9836fb4SBjoern A. Zeeb 		return sizeof(struct iwl_rx_transfer_desc);
666bfcc09ddSBjoern A. Zeeb 
667d9836fb4SBjoern A. Zeeb 	return trans->trans_cfg->mq_rx_supported ?
668d9836fb4SBjoern A. Zeeb 			sizeof(__le64) : sizeof(__le32);
669d9836fb4SBjoern A. Zeeb }
670d9836fb4SBjoern A. Zeeb 
671d9836fb4SBjoern A. Zeeb static int iwl_pcie_used_bd_size(struct iwl_trans *trans)
672d9836fb4SBjoern A. Zeeb {
673d9836fb4SBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
674d9836fb4SBjoern A. Zeeb 		return sizeof(struct iwl_rx_completion_desc_bz);
675d9836fb4SBjoern A. Zeeb 
676d9836fb4SBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
677d9836fb4SBjoern A. Zeeb 		return sizeof(struct iwl_rx_completion_desc);
678d9836fb4SBjoern A. Zeeb 
679d9836fb4SBjoern A. Zeeb 	return sizeof(__le32);
680bfcc09ddSBjoern A. Zeeb }
681bfcc09ddSBjoern A. Zeeb 
682bfcc09ddSBjoern A. Zeeb static void iwl_pcie_free_rxq_dma(struct iwl_trans *trans,
683bfcc09ddSBjoern A. Zeeb 				  struct iwl_rxq *rxq)
684bfcc09ddSBjoern A. Zeeb {
685d9836fb4SBjoern A. Zeeb 	int free_size = iwl_pcie_free_bd_size(trans);
686bfcc09ddSBjoern A. Zeeb 
687bfcc09ddSBjoern A. Zeeb 	if (rxq->bd)
688bfcc09ddSBjoern A. Zeeb 		dma_free_coherent(trans->dev,
689bfcc09ddSBjoern A. Zeeb 				  free_size * rxq->queue_size,
690bfcc09ddSBjoern A. Zeeb 				  rxq->bd, rxq->bd_dma);
691bfcc09ddSBjoern A. Zeeb 	rxq->bd_dma = 0;
692bfcc09ddSBjoern A. Zeeb 	rxq->bd = NULL;
693bfcc09ddSBjoern A. Zeeb 
694bfcc09ddSBjoern A. Zeeb 	rxq->rb_stts_dma = 0;
695bfcc09ddSBjoern A. Zeeb 	rxq->rb_stts = NULL;
696bfcc09ddSBjoern A. Zeeb 
697bfcc09ddSBjoern A. Zeeb 	if (rxq->used_bd)
698bfcc09ddSBjoern A. Zeeb 		dma_free_coherent(trans->dev,
699d9836fb4SBjoern A. Zeeb 				  iwl_pcie_used_bd_size(trans) *
700d9836fb4SBjoern A. Zeeb 					rxq->queue_size,
701bfcc09ddSBjoern A. Zeeb 				  rxq->used_bd, rxq->used_bd_dma);
702bfcc09ddSBjoern A. Zeeb 	rxq->used_bd_dma = 0;
703bfcc09ddSBjoern A. Zeeb 	rxq->used_bd = NULL;
704bfcc09ddSBjoern A. Zeeb }
705bfcc09ddSBjoern A. Zeeb 
7069af1bba4SBjoern A. Zeeb static size_t iwl_pcie_rb_stts_size(struct iwl_trans *trans)
7079af1bba4SBjoern A. Zeeb {
7089af1bba4SBjoern A. Zeeb 	bool use_rx_td = (trans->trans_cfg->device_family >=
7099af1bba4SBjoern A. Zeeb 			  IWL_DEVICE_FAMILY_AX210);
7109af1bba4SBjoern A. Zeeb 
7119af1bba4SBjoern A. Zeeb 	if (use_rx_td)
7129af1bba4SBjoern A. Zeeb 		return sizeof(__le16);
7139af1bba4SBjoern A. Zeeb 
7149af1bba4SBjoern A. Zeeb 	return sizeof(struct iwl_rb_status);
7159af1bba4SBjoern A. Zeeb }
7169af1bba4SBjoern A. Zeeb 
717bfcc09ddSBjoern A. Zeeb static int iwl_pcie_alloc_rxq_dma(struct iwl_trans *trans,
718bfcc09ddSBjoern A. Zeeb 				  struct iwl_rxq *rxq)
719bfcc09ddSBjoern A. Zeeb {
720bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
7219af1bba4SBjoern A. Zeeb 	size_t rb_stts_size = iwl_pcie_rb_stts_size(trans);
722bfcc09ddSBjoern A. Zeeb 	struct device *dev = trans->dev;
723bfcc09ddSBjoern A. Zeeb 	int i;
724bfcc09ddSBjoern A. Zeeb 	int free_size;
725bfcc09ddSBjoern A. Zeeb 
726bfcc09ddSBjoern A. Zeeb 	spin_lock_init(&rxq->lock);
727bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->mq_rx_supported)
728bfcc09ddSBjoern A. Zeeb 		rxq->queue_size = trans->cfg->num_rbds;
729bfcc09ddSBjoern A. Zeeb 	else
730bfcc09ddSBjoern A. Zeeb 		rxq->queue_size = RX_QUEUE_SIZE;
731bfcc09ddSBjoern A. Zeeb 
732d9836fb4SBjoern A. Zeeb 	free_size = iwl_pcie_free_bd_size(trans);
733bfcc09ddSBjoern A. Zeeb 
734bfcc09ddSBjoern A. Zeeb 	/*
735bfcc09ddSBjoern A. Zeeb 	 * Allocate the circular buffer of Read Buffer Descriptors
736bfcc09ddSBjoern A. Zeeb 	 * (RBDs)
737bfcc09ddSBjoern A. Zeeb 	 */
738bfcc09ddSBjoern A. Zeeb 	rxq->bd = dma_alloc_coherent(dev, free_size * rxq->queue_size,
739bfcc09ddSBjoern A. Zeeb 				     &rxq->bd_dma, GFP_KERNEL);
740bfcc09ddSBjoern A. Zeeb 	if (!rxq->bd)
741bfcc09ddSBjoern A. Zeeb 		goto err;
742bfcc09ddSBjoern A. Zeeb 
743bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->mq_rx_supported) {
744bfcc09ddSBjoern A. Zeeb 		rxq->used_bd = dma_alloc_coherent(dev,
745d9836fb4SBjoern A. Zeeb 						  iwl_pcie_used_bd_size(trans) *
746d9836fb4SBjoern A. Zeeb 							rxq->queue_size,
747bfcc09ddSBjoern A. Zeeb 						  &rxq->used_bd_dma,
748bfcc09ddSBjoern A. Zeeb 						  GFP_KERNEL);
749bfcc09ddSBjoern A. Zeeb 		if (!rxq->used_bd)
750bfcc09ddSBjoern A. Zeeb 			goto err;
751bfcc09ddSBjoern A. Zeeb 	}
752bfcc09ddSBjoern A. Zeeb 
753d9836fb4SBjoern A. Zeeb 	rxq->rb_stts = (u8 *)trans_pcie->base_rb_stts + rxq->id * rb_stts_size;
754bfcc09ddSBjoern A. Zeeb 	rxq->rb_stts_dma =
755bfcc09ddSBjoern A. Zeeb 		trans_pcie->base_rb_stts_dma + rxq->id * rb_stts_size;
756bfcc09ddSBjoern A. Zeeb 
757bfcc09ddSBjoern A. Zeeb 	return 0;
758bfcc09ddSBjoern A. Zeeb 
759bfcc09ddSBjoern A. Zeeb err:
760bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
761bfcc09ddSBjoern A. Zeeb 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
762bfcc09ddSBjoern A. Zeeb 
763bfcc09ddSBjoern A. Zeeb 		iwl_pcie_free_rxq_dma(trans, rxq);
764bfcc09ddSBjoern A. Zeeb 	}
765bfcc09ddSBjoern A. Zeeb 
766bfcc09ddSBjoern A. Zeeb 	return -ENOMEM;
767bfcc09ddSBjoern A. Zeeb }
768bfcc09ddSBjoern A. Zeeb 
769bfcc09ddSBjoern A. Zeeb static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
770bfcc09ddSBjoern A. Zeeb {
771bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
7729af1bba4SBjoern A. Zeeb 	size_t rb_stts_size = iwl_pcie_rb_stts_size(trans);
773bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
774bfcc09ddSBjoern A. Zeeb 	int i, ret;
775bfcc09ddSBjoern A. Zeeb 
776bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(trans_pcie->rxq))
777bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
778bfcc09ddSBjoern A. Zeeb 
779bfcc09ddSBjoern A. Zeeb 	trans_pcie->rxq = kcalloc(trans->num_rx_queues, sizeof(struct iwl_rxq),
780bfcc09ddSBjoern A. Zeeb 				  GFP_KERNEL);
781bfcc09ddSBjoern A. Zeeb 	trans_pcie->rx_pool = kcalloc(RX_POOL_SIZE(trans_pcie->num_rx_bufs),
782bfcc09ddSBjoern A. Zeeb 				      sizeof(trans_pcie->rx_pool[0]),
783bfcc09ddSBjoern A. Zeeb 				      GFP_KERNEL);
784bfcc09ddSBjoern A. Zeeb 	trans_pcie->global_table =
785bfcc09ddSBjoern A. Zeeb 		kcalloc(RX_POOL_SIZE(trans_pcie->num_rx_bufs),
786bfcc09ddSBjoern A. Zeeb 			sizeof(trans_pcie->global_table[0]),
787bfcc09ddSBjoern A. Zeeb 			GFP_KERNEL);
788bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->rxq || !trans_pcie->rx_pool ||
789bfcc09ddSBjoern A. Zeeb 	    !trans_pcie->global_table) {
790bfcc09ddSBjoern A. Zeeb 		ret = -ENOMEM;
791bfcc09ddSBjoern A. Zeeb 		goto err;
792bfcc09ddSBjoern A. Zeeb 	}
793bfcc09ddSBjoern A. Zeeb 
794bfcc09ddSBjoern A. Zeeb 	spin_lock_init(&rba->lock);
795bfcc09ddSBjoern A. Zeeb 
796bfcc09ddSBjoern A. Zeeb 	/*
797bfcc09ddSBjoern A. Zeeb 	 * Allocate the driver's pointer to receive buffer status.
798bfcc09ddSBjoern A. Zeeb 	 * Allocate for all queues continuously (HW requirement).
799bfcc09ddSBjoern A. Zeeb 	 */
800bfcc09ddSBjoern A. Zeeb 	trans_pcie->base_rb_stts =
801bfcc09ddSBjoern A. Zeeb 			dma_alloc_coherent(trans->dev,
802bfcc09ddSBjoern A. Zeeb 					   rb_stts_size * trans->num_rx_queues,
803bfcc09ddSBjoern A. Zeeb 					   &trans_pcie->base_rb_stts_dma,
804bfcc09ddSBjoern A. Zeeb 					   GFP_KERNEL);
805bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->base_rb_stts) {
806bfcc09ddSBjoern A. Zeeb 		ret = -ENOMEM;
807bfcc09ddSBjoern A. Zeeb 		goto err;
808bfcc09ddSBjoern A. Zeeb 	}
809bfcc09ddSBjoern A. Zeeb 
810bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
811bfcc09ddSBjoern A. Zeeb 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
812bfcc09ddSBjoern A. Zeeb 
813bfcc09ddSBjoern A. Zeeb 		rxq->id = i;
814bfcc09ddSBjoern A. Zeeb 		ret = iwl_pcie_alloc_rxq_dma(trans, rxq);
815bfcc09ddSBjoern A. Zeeb 		if (ret)
816bfcc09ddSBjoern A. Zeeb 			goto err;
817bfcc09ddSBjoern A. Zeeb 	}
818bfcc09ddSBjoern A. Zeeb 	return 0;
819bfcc09ddSBjoern A. Zeeb 
820bfcc09ddSBjoern A. Zeeb err:
821bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->base_rb_stts) {
822bfcc09ddSBjoern A. Zeeb 		dma_free_coherent(trans->dev,
823bfcc09ddSBjoern A. Zeeb 				  rb_stts_size * trans->num_rx_queues,
824bfcc09ddSBjoern A. Zeeb 				  trans_pcie->base_rb_stts,
825bfcc09ddSBjoern A. Zeeb 				  trans_pcie->base_rb_stts_dma);
826bfcc09ddSBjoern A. Zeeb 		trans_pcie->base_rb_stts = NULL;
827bfcc09ddSBjoern A. Zeeb 		trans_pcie->base_rb_stts_dma = 0;
828bfcc09ddSBjoern A. Zeeb 	}
829bfcc09ddSBjoern A. Zeeb 	kfree(trans_pcie->rx_pool);
830bfcc09ddSBjoern A. Zeeb 	trans_pcie->rx_pool = NULL;
831bfcc09ddSBjoern A. Zeeb 	kfree(trans_pcie->global_table);
832bfcc09ddSBjoern A. Zeeb 	trans_pcie->global_table = NULL;
833bfcc09ddSBjoern A. Zeeb 	kfree(trans_pcie->rxq);
834bfcc09ddSBjoern A. Zeeb 	trans_pcie->rxq = NULL;
835bfcc09ddSBjoern A. Zeeb 
836bfcc09ddSBjoern A. Zeeb 	return ret;
837bfcc09ddSBjoern A. Zeeb }
838bfcc09ddSBjoern A. Zeeb 
839bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
840bfcc09ddSBjoern A. Zeeb {
841bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
842bfcc09ddSBjoern A. Zeeb 	u32 rb_size;
843bfcc09ddSBjoern A. Zeeb 	const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
844bfcc09ddSBjoern A. Zeeb 
845bfcc09ddSBjoern A. Zeeb 	switch (trans_pcie->rx_buf_size) {
846bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_4K:
847bfcc09ddSBjoern A. Zeeb 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
848bfcc09ddSBjoern A. Zeeb 		break;
849bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_8K:
850bfcc09ddSBjoern A. Zeeb 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
851bfcc09ddSBjoern A. Zeeb 		break;
852bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_12K:
853bfcc09ddSBjoern A. Zeeb 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_12K;
854bfcc09ddSBjoern A. Zeeb 		break;
855bfcc09ddSBjoern A. Zeeb 	default:
856bfcc09ddSBjoern A. Zeeb 		WARN_ON(1);
857bfcc09ddSBjoern A. Zeeb 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
858bfcc09ddSBjoern A. Zeeb 	}
859bfcc09ddSBjoern A. Zeeb 
860bfcc09ddSBjoern A. Zeeb 	if (!iwl_trans_grab_nic_access(trans))
861bfcc09ddSBjoern A. Zeeb 		return;
862bfcc09ddSBjoern A. Zeeb 
863bfcc09ddSBjoern A. Zeeb 	/* Stop Rx DMA */
864bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
865bfcc09ddSBjoern A. Zeeb 	/* reset and flush pointers */
866bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_RBDCB_WPTR, 0);
867bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_FLUSH_RB_REQ, 0);
868bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_RSCSR_CHNL0_RDPTR, 0);
869bfcc09ddSBjoern A. Zeeb 
870bfcc09ddSBjoern A. Zeeb 	/* Reset driver's Rx queue write index */
871bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
872bfcc09ddSBjoern A. Zeeb 
873bfcc09ddSBjoern A. Zeeb 	/* Tell device where to find RBD circular buffer in DRAM */
874bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
875bfcc09ddSBjoern A. Zeeb 		    (u32)(rxq->bd_dma >> 8));
876bfcc09ddSBjoern A. Zeeb 
877bfcc09ddSBjoern A. Zeeb 	/* Tell device where in DRAM to update its Rx status */
878bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
879bfcc09ddSBjoern A. Zeeb 		    rxq->rb_stts_dma >> 4);
880bfcc09ddSBjoern A. Zeeb 
881bfcc09ddSBjoern A. Zeeb 	/* Enable Rx DMA
882bfcc09ddSBjoern A. Zeeb 	 * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
883bfcc09ddSBjoern A. Zeeb 	 *      the credit mechanism in 5000 HW RX FIFO
884bfcc09ddSBjoern A. Zeeb 	 * Direct rx interrupts to hosts
885bfcc09ddSBjoern A. Zeeb 	 * Rx buffer size 4 or 8k or 12k
886bfcc09ddSBjoern A. Zeeb 	 * RB timeout 0x10
887bfcc09ddSBjoern A. Zeeb 	 * 256 RBDs
888bfcc09ddSBjoern A. Zeeb 	 */
889bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
890bfcc09ddSBjoern A. Zeeb 		    FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
891bfcc09ddSBjoern A. Zeeb 		    FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
892bfcc09ddSBjoern A. Zeeb 		    FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
893bfcc09ddSBjoern A. Zeeb 		    rb_size |
894bfcc09ddSBjoern A. Zeeb 		    (RX_RB_TIMEOUT << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
895bfcc09ddSBjoern A. Zeeb 		    (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
896bfcc09ddSBjoern A. Zeeb 
897bfcc09ddSBjoern A. Zeeb 	iwl_trans_release_nic_access(trans);
898bfcc09ddSBjoern A. Zeeb 
899bfcc09ddSBjoern A. Zeeb 	/* Set interrupt coalescing timer to default (2048 usecs) */
900bfcc09ddSBjoern A. Zeeb 	iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
901bfcc09ddSBjoern A. Zeeb 
902bfcc09ddSBjoern A. Zeeb 	/* W/A for interrupt coalescing bug in 7260 and 3160 */
903bfcc09ddSBjoern A. Zeeb 	if (trans->cfg->host_interrupt_operation_mode)
904bfcc09ddSBjoern A. Zeeb 		iwl_set_bit(trans, CSR_INT_COALESCING, IWL_HOST_INT_OPER_MODE);
905bfcc09ddSBjoern A. Zeeb }
906bfcc09ddSBjoern A. Zeeb 
907bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_mq_hw_init(struct iwl_trans *trans)
908bfcc09ddSBjoern A. Zeeb {
909bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
910bfcc09ddSBjoern A. Zeeb 	u32 rb_size, enabled = 0;
911bfcc09ddSBjoern A. Zeeb 	int i;
912bfcc09ddSBjoern A. Zeeb 
913bfcc09ddSBjoern A. Zeeb 	switch (trans_pcie->rx_buf_size) {
914bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_2K:
915bfcc09ddSBjoern A. Zeeb 		rb_size = RFH_RXF_DMA_RB_SIZE_2K;
916bfcc09ddSBjoern A. Zeeb 		break;
917bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_4K:
918bfcc09ddSBjoern A. Zeeb 		rb_size = RFH_RXF_DMA_RB_SIZE_4K;
919bfcc09ddSBjoern A. Zeeb 		break;
920bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_8K:
921bfcc09ddSBjoern A. Zeeb 		rb_size = RFH_RXF_DMA_RB_SIZE_8K;
922bfcc09ddSBjoern A. Zeeb 		break;
923bfcc09ddSBjoern A. Zeeb 	case IWL_AMSDU_12K:
924bfcc09ddSBjoern A. Zeeb 		rb_size = RFH_RXF_DMA_RB_SIZE_12K;
925bfcc09ddSBjoern A. Zeeb 		break;
926bfcc09ddSBjoern A. Zeeb 	default:
927bfcc09ddSBjoern A. Zeeb 		WARN_ON(1);
928bfcc09ddSBjoern A. Zeeb 		rb_size = RFH_RXF_DMA_RB_SIZE_4K;
929bfcc09ddSBjoern A. Zeeb 	}
930bfcc09ddSBjoern A. Zeeb 
931bfcc09ddSBjoern A. Zeeb 	if (!iwl_trans_grab_nic_access(trans))
932bfcc09ddSBjoern A. Zeeb 		return;
933bfcc09ddSBjoern A. Zeeb 
934bfcc09ddSBjoern A. Zeeb 	/* Stop Rx DMA */
935bfcc09ddSBjoern A. Zeeb 	iwl_write_prph_no_grab(trans, RFH_RXF_DMA_CFG, 0);
936bfcc09ddSBjoern A. Zeeb 	/* disable free amd used rx queue operation */
937bfcc09ddSBjoern A. Zeeb 	iwl_write_prph_no_grab(trans, RFH_RXF_RXQ_ACTIVE, 0);
938bfcc09ddSBjoern A. Zeeb 
939bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
940bfcc09ddSBjoern A. Zeeb 		/* Tell device where to find RBD free table in DRAM */
941bfcc09ddSBjoern A. Zeeb 		iwl_write_prph64_no_grab(trans,
942bfcc09ddSBjoern A. Zeeb 					 RFH_Q_FRBDCB_BA_LSB(i),
943bfcc09ddSBjoern A. Zeeb 					 trans_pcie->rxq[i].bd_dma);
944bfcc09ddSBjoern A. Zeeb 		/* Tell device where to find RBD used table in DRAM */
945bfcc09ddSBjoern A. Zeeb 		iwl_write_prph64_no_grab(trans,
946bfcc09ddSBjoern A. Zeeb 					 RFH_Q_URBDCB_BA_LSB(i),
947bfcc09ddSBjoern A. Zeeb 					 trans_pcie->rxq[i].used_bd_dma);
948bfcc09ddSBjoern A. Zeeb 		/* Tell device where in DRAM to update its Rx status */
949bfcc09ddSBjoern A. Zeeb 		iwl_write_prph64_no_grab(trans,
950bfcc09ddSBjoern A. Zeeb 					 RFH_Q_URBD_STTS_WPTR_LSB(i),
951bfcc09ddSBjoern A. Zeeb 					 trans_pcie->rxq[i].rb_stts_dma);
952bfcc09ddSBjoern A. Zeeb 		/* Reset device indice tables */
953bfcc09ddSBjoern A. Zeeb 		iwl_write_prph_no_grab(trans, RFH_Q_FRBDCB_WIDX(i), 0);
954bfcc09ddSBjoern A. Zeeb 		iwl_write_prph_no_grab(trans, RFH_Q_FRBDCB_RIDX(i), 0);
955bfcc09ddSBjoern A. Zeeb 		iwl_write_prph_no_grab(trans, RFH_Q_URBDCB_WIDX(i), 0);
956bfcc09ddSBjoern A. Zeeb 
957bfcc09ddSBjoern A. Zeeb 		enabled |= BIT(i) | BIT(i + 16);
958bfcc09ddSBjoern A. Zeeb 	}
959bfcc09ddSBjoern A. Zeeb 
960bfcc09ddSBjoern A. Zeeb 	/*
961bfcc09ddSBjoern A. Zeeb 	 * Enable Rx DMA
962bfcc09ddSBjoern A. Zeeb 	 * Rx buffer size 4 or 8k or 12k
963bfcc09ddSBjoern A. Zeeb 	 * Min RB size 4 or 8
964bfcc09ddSBjoern A. Zeeb 	 * Drop frames that exceed RB size
965bfcc09ddSBjoern A. Zeeb 	 * 512 RBDs
966bfcc09ddSBjoern A. Zeeb 	 */
967bfcc09ddSBjoern A. Zeeb 	iwl_write_prph_no_grab(trans, RFH_RXF_DMA_CFG,
968bfcc09ddSBjoern A. Zeeb 			       RFH_DMA_EN_ENABLE_VAL | rb_size |
969bfcc09ddSBjoern A. Zeeb 			       RFH_RXF_DMA_MIN_RB_4_8 |
970bfcc09ddSBjoern A. Zeeb 			       RFH_RXF_DMA_DROP_TOO_LARGE_MASK |
971bfcc09ddSBjoern A. Zeeb 			       RFH_RXF_DMA_RBDCB_SIZE_512);
972bfcc09ddSBjoern A. Zeeb 
973bfcc09ddSBjoern A. Zeeb 	/*
974bfcc09ddSBjoern A. Zeeb 	 * Activate DMA snooping.
975bfcc09ddSBjoern A. Zeeb 	 * Set RX DMA chunk size to 64B for IOSF and 128B for PCIe
976bfcc09ddSBjoern A. Zeeb 	 * Default queue is 0
977bfcc09ddSBjoern A. Zeeb 	 */
978bfcc09ddSBjoern A. Zeeb 	iwl_write_prph_no_grab(trans, RFH_GEN_CFG,
979bfcc09ddSBjoern A. Zeeb 			       RFH_GEN_CFG_RFH_DMA_SNOOP |
980bfcc09ddSBjoern A. Zeeb 			       RFH_GEN_CFG_VAL(DEFAULT_RXQ_NUM, 0) |
981bfcc09ddSBjoern A. Zeeb 			       RFH_GEN_CFG_SERVICE_DMA_SNOOP |
982bfcc09ddSBjoern A. Zeeb 			       RFH_GEN_CFG_VAL(RB_CHUNK_SIZE,
983bfcc09ddSBjoern A. Zeeb 					       trans->trans_cfg->integrated ?
984bfcc09ddSBjoern A. Zeeb 					       RFH_GEN_CFG_RB_CHUNK_SIZE_64 :
985bfcc09ddSBjoern A. Zeeb 					       RFH_GEN_CFG_RB_CHUNK_SIZE_128));
986bfcc09ddSBjoern A. Zeeb 	/* Enable the relevant rx queues */
987bfcc09ddSBjoern A. Zeeb 	iwl_write_prph_no_grab(trans, RFH_RXF_RXQ_ACTIVE, enabled);
988bfcc09ddSBjoern A. Zeeb 
989bfcc09ddSBjoern A. Zeeb 	iwl_trans_release_nic_access(trans);
990bfcc09ddSBjoern A. Zeeb 
991bfcc09ddSBjoern A. Zeeb 	/* Set interrupt coalescing timer to default (2048 usecs) */
992bfcc09ddSBjoern A. Zeeb 	iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
993bfcc09ddSBjoern A. Zeeb }
994bfcc09ddSBjoern A. Zeeb 
995bfcc09ddSBjoern A. Zeeb void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq)
996bfcc09ddSBjoern A. Zeeb {
997bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&rxq->lock);
998bfcc09ddSBjoern A. Zeeb 
999bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&rxq->rx_free);
1000bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&rxq->rx_used);
1001bfcc09ddSBjoern A. Zeeb 	rxq->free_count = 0;
1002bfcc09ddSBjoern A. Zeeb 	rxq->used_count = 0;
1003bfcc09ddSBjoern A. Zeeb }
1004bfcc09ddSBjoern A. Zeeb 
1005bfcc09ddSBjoern A. Zeeb static int iwl_pcie_rx_handle(struct iwl_trans *trans, int queue, int budget);
1006bfcc09ddSBjoern A. Zeeb 
1007*a4128aadSBjoern A. Zeeb static inline struct iwl_trans_pcie *iwl_netdev_to_trans_pcie(struct net_device *dev)
1008*a4128aadSBjoern A. Zeeb {
1009*a4128aadSBjoern A. Zeeb 	return *(struct iwl_trans_pcie **)netdev_priv(dev);
1010*a4128aadSBjoern A. Zeeb }
1011*a4128aadSBjoern A. Zeeb 
1012bfcc09ddSBjoern A. Zeeb static int iwl_pcie_napi_poll(struct napi_struct *napi, int budget)
1013bfcc09ddSBjoern A. Zeeb {
1014bfcc09ddSBjoern A. Zeeb 	struct iwl_rxq *rxq = container_of(napi, struct iwl_rxq, napi);
1015bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie;
1016bfcc09ddSBjoern A. Zeeb 	struct iwl_trans *trans;
1017bfcc09ddSBjoern A. Zeeb 	int ret;
1018bfcc09ddSBjoern A. Zeeb 
1019*a4128aadSBjoern A. Zeeb 	trans_pcie = iwl_netdev_to_trans_pcie(napi->dev);
1020bfcc09ddSBjoern A. Zeeb 	trans = trans_pcie->trans;
1021bfcc09ddSBjoern A. Zeeb 
1022bfcc09ddSBjoern A. Zeeb 	ret = iwl_pcie_rx_handle(trans, rxq->id, budget);
1023bfcc09ddSBjoern A. Zeeb 
1024bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_ISR(trans, "[%d] handled %d, budget %d\n",
1025bfcc09ddSBjoern A. Zeeb 		      rxq->id, ret, budget);
1026bfcc09ddSBjoern A. Zeeb 
1027bfcc09ddSBjoern A. Zeeb 	if (ret < budget) {
1028bfcc09ddSBjoern A. Zeeb 		spin_lock(&trans_pcie->irq_lock);
1029bfcc09ddSBjoern A. Zeeb 		if (test_bit(STATUS_INT_ENABLED, &trans->status))
1030bfcc09ddSBjoern A. Zeeb 			_iwl_enable_interrupts(trans);
1031bfcc09ddSBjoern A. Zeeb 		spin_unlock(&trans_pcie->irq_lock);
1032bfcc09ddSBjoern A. Zeeb 
1033bfcc09ddSBjoern A. Zeeb 		napi_complete_done(&rxq->napi, ret);
1034bfcc09ddSBjoern A. Zeeb 	}
1035bfcc09ddSBjoern A. Zeeb 
1036bfcc09ddSBjoern A. Zeeb 	return ret;
1037bfcc09ddSBjoern A. Zeeb }
1038bfcc09ddSBjoern A. Zeeb 
1039bfcc09ddSBjoern A. Zeeb static int iwl_pcie_napi_poll_msix(struct napi_struct *napi, int budget)
1040bfcc09ddSBjoern A. Zeeb {
1041bfcc09ddSBjoern A. Zeeb 	struct iwl_rxq *rxq = container_of(napi, struct iwl_rxq, napi);
1042bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie;
1043bfcc09ddSBjoern A. Zeeb 	struct iwl_trans *trans;
1044bfcc09ddSBjoern A. Zeeb 	int ret;
1045bfcc09ddSBjoern A. Zeeb 
1046*a4128aadSBjoern A. Zeeb 	trans_pcie = iwl_netdev_to_trans_pcie(napi->dev);
1047bfcc09ddSBjoern A. Zeeb 	trans = trans_pcie->trans;
1048bfcc09ddSBjoern A. Zeeb 
1049bfcc09ddSBjoern A. Zeeb 	ret = iwl_pcie_rx_handle(trans, rxq->id, budget);
1050bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_ISR(trans, "[%d] handled %d, budget %d\n", rxq->id, ret,
1051bfcc09ddSBjoern A. Zeeb 		      budget);
1052bfcc09ddSBjoern A. Zeeb 
1053bfcc09ddSBjoern A. Zeeb 	if (ret < budget) {
1054bfcc09ddSBjoern A. Zeeb 		int irq_line = rxq->id;
1055bfcc09ddSBjoern A. Zeeb 
1056bfcc09ddSBjoern A. Zeeb 		/* FIRST_RSS is shared with line 0 */
1057bfcc09ddSBjoern A. Zeeb 		if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS &&
1058bfcc09ddSBjoern A. Zeeb 		    rxq->id == 1)
1059bfcc09ddSBjoern A. Zeeb 			irq_line = 0;
1060bfcc09ddSBjoern A. Zeeb 
1061bfcc09ddSBjoern A. Zeeb 		spin_lock(&trans_pcie->irq_lock);
1062bfcc09ddSBjoern A. Zeeb 		iwl_pcie_clear_irq(trans, irq_line);
1063bfcc09ddSBjoern A. Zeeb 		spin_unlock(&trans_pcie->irq_lock);
1064bfcc09ddSBjoern A. Zeeb 
1065bfcc09ddSBjoern A. Zeeb 		napi_complete_done(&rxq->napi, ret);
1066bfcc09ddSBjoern A. Zeeb 	}
1067bfcc09ddSBjoern A. Zeeb 
1068bfcc09ddSBjoern A. Zeeb 	return ret;
1069bfcc09ddSBjoern A. Zeeb }
1070bfcc09ddSBjoern A. Zeeb 
10719af1bba4SBjoern A. Zeeb void iwl_pcie_rx_napi_sync(struct iwl_trans *trans)
10729af1bba4SBjoern A. Zeeb {
10739af1bba4SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
10749af1bba4SBjoern A. Zeeb 	int i;
10759af1bba4SBjoern A. Zeeb 
10769af1bba4SBjoern A. Zeeb 	if (unlikely(!trans_pcie->rxq))
10779af1bba4SBjoern A. Zeeb 		return;
10789af1bba4SBjoern A. Zeeb 
10799af1bba4SBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
10809af1bba4SBjoern A. Zeeb 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
10819af1bba4SBjoern A. Zeeb 
10829af1bba4SBjoern A. Zeeb 		if (rxq && rxq->napi.poll)
10839af1bba4SBjoern A. Zeeb 			napi_synchronize(&rxq->napi);
10849af1bba4SBjoern A. Zeeb 	}
10859af1bba4SBjoern A. Zeeb }
10869af1bba4SBjoern A. Zeeb 
1087bfcc09ddSBjoern A. Zeeb static int _iwl_pcie_rx_init(struct iwl_trans *trans)
1088bfcc09ddSBjoern A. Zeeb {
1089bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1090bfcc09ddSBjoern A. Zeeb 	struct iwl_rxq *def_rxq;
1091bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
1092bfcc09ddSBjoern A. Zeeb 	int i, err, queue_size, allocator_pool_size, num_alloc;
1093bfcc09ddSBjoern A. Zeeb 
1094bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->rxq) {
1095bfcc09ddSBjoern A. Zeeb 		err = iwl_pcie_rx_alloc(trans);
1096bfcc09ddSBjoern A. Zeeb 		if (err)
1097bfcc09ddSBjoern A. Zeeb 			return err;
1098bfcc09ddSBjoern A. Zeeb 	}
1099bfcc09ddSBjoern A. Zeeb 	def_rxq = trans_pcie->rxq;
1100bfcc09ddSBjoern A. Zeeb 
1101bfcc09ddSBjoern A. Zeeb 	cancel_work_sync(&rba->rx_alloc);
1102bfcc09ddSBjoern A. Zeeb 
1103bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&rba->lock);
1104bfcc09ddSBjoern A. Zeeb 	atomic_set(&rba->req_pending, 0);
1105bfcc09ddSBjoern A. Zeeb 	atomic_set(&rba->req_ready, 0);
1106bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&rba->rbd_allocated);
1107bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&rba->rbd_empty);
1108bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&rba->lock);
1109bfcc09ddSBjoern A. Zeeb 
1110bfcc09ddSBjoern A. Zeeb 	/* free all first - we overwrite everything here */
1111bfcc09ddSBjoern A. Zeeb 	iwl_pcie_free_rbs_pool(trans);
1112bfcc09ddSBjoern A. Zeeb 
1113bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < RX_QUEUE_SIZE; i++)
1114bfcc09ddSBjoern A. Zeeb 		def_rxq->queue[i] = NULL;
1115bfcc09ddSBjoern A. Zeeb 
1116bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
1117bfcc09ddSBjoern A. Zeeb 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
1118bfcc09ddSBjoern A. Zeeb 
1119bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&rxq->lock);
1120bfcc09ddSBjoern A. Zeeb 		/*
1121bfcc09ddSBjoern A. Zeeb 		 * Set read write pointer to reflect that we have processed
1122bfcc09ddSBjoern A. Zeeb 		 * and used all buffers, but have not restocked the Rx queue
1123bfcc09ddSBjoern A. Zeeb 		 * with fresh buffers
1124bfcc09ddSBjoern A. Zeeb 		 */
1125bfcc09ddSBjoern A. Zeeb 		rxq->read = 0;
1126bfcc09ddSBjoern A. Zeeb 		rxq->write = 0;
1127bfcc09ddSBjoern A. Zeeb 		rxq->write_actual = 0;
1128bfcc09ddSBjoern A. Zeeb 		memset(rxq->rb_stts, 0,
1129bfcc09ddSBjoern A. Zeeb 		       (trans->trans_cfg->device_family >=
1130bfcc09ddSBjoern A. Zeeb 			IWL_DEVICE_FAMILY_AX210) ?
1131bfcc09ddSBjoern A. Zeeb 		       sizeof(__le16) : sizeof(struct iwl_rb_status));
1132bfcc09ddSBjoern A. Zeeb 
1133bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rx_init_rxb_lists(rxq);
1134bfcc09ddSBjoern A. Zeeb 
1135bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&rxq->lock);
1136bfcc09ddSBjoern A. Zeeb 
1137bfcc09ddSBjoern A. Zeeb 		if (!rxq->napi.poll) {
1138bfcc09ddSBjoern A. Zeeb 			int (*poll)(struct napi_struct *, int) = iwl_pcie_napi_poll;
1139bfcc09ddSBjoern A. Zeeb 
1140bfcc09ddSBjoern A. Zeeb 			if (trans_pcie->msix_enabled)
1141bfcc09ddSBjoern A. Zeeb 				poll = iwl_pcie_napi_poll_msix;
1142bfcc09ddSBjoern A. Zeeb 
1143*a4128aadSBjoern A. Zeeb 			netif_napi_add(trans_pcie->napi_dev, &rxq->napi,
11449af1bba4SBjoern A. Zeeb 				       poll);
1145bfcc09ddSBjoern A. Zeeb 			napi_enable(&rxq->napi);
1146bfcc09ddSBjoern A. Zeeb 		}
1147bfcc09ddSBjoern A. Zeeb 
1148bfcc09ddSBjoern A. Zeeb 	}
1149bfcc09ddSBjoern A. Zeeb 
1150bfcc09ddSBjoern A. Zeeb 	/* move the pool to the default queue and allocator ownerships */
1151bfcc09ddSBjoern A. Zeeb 	queue_size = trans->trans_cfg->mq_rx_supported ?
1152bfcc09ddSBjoern A. Zeeb 			trans_pcie->num_rx_bufs - 1 : RX_QUEUE_SIZE;
1153bfcc09ddSBjoern A. Zeeb 	allocator_pool_size = trans->num_rx_queues *
1154bfcc09ddSBjoern A. Zeeb 		(RX_CLAIM_REQ_ALLOC - RX_POST_REQ_ALLOC);
1155bfcc09ddSBjoern A. Zeeb 	num_alloc = queue_size + allocator_pool_size;
1156bfcc09ddSBjoern A. Zeeb 
1157bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < num_alloc; i++) {
1158bfcc09ddSBjoern A. Zeeb 		struct iwl_rx_mem_buffer *rxb = &trans_pcie->rx_pool[i];
1159bfcc09ddSBjoern A. Zeeb 
1160bfcc09ddSBjoern A. Zeeb 		if (i < allocator_pool_size)
1161bfcc09ddSBjoern A. Zeeb 			list_add(&rxb->list, &rba->rbd_empty);
1162bfcc09ddSBjoern A. Zeeb 		else
1163bfcc09ddSBjoern A. Zeeb 			list_add(&rxb->list, &def_rxq->rx_used);
1164bfcc09ddSBjoern A. Zeeb 		trans_pcie->global_table[i] = rxb;
1165bfcc09ddSBjoern A. Zeeb 		rxb->vid = (u16)(i + 1);
1166bfcc09ddSBjoern A. Zeeb 		rxb->invalid = true;
1167bfcc09ddSBjoern A. Zeeb 	}
1168bfcc09ddSBjoern A. Zeeb 
1169bfcc09ddSBjoern A. Zeeb 	iwl_pcie_rxq_alloc_rbs(trans, GFP_KERNEL, def_rxq);
1170bfcc09ddSBjoern A. Zeeb 
1171bfcc09ddSBjoern A. Zeeb 	return 0;
1172bfcc09ddSBjoern A. Zeeb }
1173bfcc09ddSBjoern A. Zeeb 
1174bfcc09ddSBjoern A. Zeeb int iwl_pcie_rx_init(struct iwl_trans *trans)
1175bfcc09ddSBjoern A. Zeeb {
1176bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1177bfcc09ddSBjoern A. Zeeb 	int ret = _iwl_pcie_rx_init(trans);
1178bfcc09ddSBjoern A. Zeeb 
1179bfcc09ddSBjoern A. Zeeb 	if (ret)
1180bfcc09ddSBjoern A. Zeeb 		return ret;
1181bfcc09ddSBjoern A. Zeeb 
1182bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->mq_rx_supported)
1183bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rx_mq_hw_init(trans);
1184bfcc09ddSBjoern A. Zeeb 	else
1185bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rx_hw_init(trans, trans_pcie->rxq);
1186bfcc09ddSBjoern A. Zeeb 
1187bfcc09ddSBjoern A. Zeeb 	iwl_pcie_rxq_restock(trans, trans_pcie->rxq);
1188bfcc09ddSBjoern A. Zeeb 
1189bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&trans_pcie->rxq->lock);
1190bfcc09ddSBjoern A. Zeeb 	iwl_pcie_rxq_inc_wr_ptr(trans, trans_pcie->rxq);
1191bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&trans_pcie->rxq->lock);
1192bfcc09ddSBjoern A. Zeeb 
1193bfcc09ddSBjoern A. Zeeb 	return 0;
1194bfcc09ddSBjoern A. Zeeb }
1195bfcc09ddSBjoern A. Zeeb 
1196bfcc09ddSBjoern A. Zeeb int iwl_pcie_gen2_rx_init(struct iwl_trans *trans)
1197bfcc09ddSBjoern A. Zeeb {
1198bfcc09ddSBjoern A. Zeeb 	/* Set interrupt coalescing timer to default (2048 usecs) */
1199bfcc09ddSBjoern A. Zeeb 	iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
1200bfcc09ddSBjoern A. Zeeb 
1201bfcc09ddSBjoern A. Zeeb 	/*
1202bfcc09ddSBjoern A. Zeeb 	 * We don't configure the RFH.
1203bfcc09ddSBjoern A. Zeeb 	 * Restock will be done at alive, after firmware configured the RFH.
1204bfcc09ddSBjoern A. Zeeb 	 */
1205bfcc09ddSBjoern A. Zeeb 	return _iwl_pcie_rx_init(trans);
1206bfcc09ddSBjoern A. Zeeb }
1207bfcc09ddSBjoern A. Zeeb 
1208bfcc09ddSBjoern A. Zeeb void iwl_pcie_rx_free(struct iwl_trans *trans)
1209bfcc09ddSBjoern A. Zeeb {
1210bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
12119af1bba4SBjoern A. Zeeb 	size_t rb_stts_size = iwl_pcie_rb_stts_size(trans);
1212bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
1213bfcc09ddSBjoern A. Zeeb 	int i;
1214bfcc09ddSBjoern A. Zeeb 
1215bfcc09ddSBjoern A. Zeeb 	/*
1216bfcc09ddSBjoern A. Zeeb 	 * if rxq is NULL, it means that nothing has been allocated,
1217bfcc09ddSBjoern A. Zeeb 	 * exit now
1218bfcc09ddSBjoern A. Zeeb 	 */
1219bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->rxq) {
1220bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
1221bfcc09ddSBjoern A. Zeeb 		return;
1222bfcc09ddSBjoern A. Zeeb 	}
1223bfcc09ddSBjoern A. Zeeb 
1224bfcc09ddSBjoern A. Zeeb 	cancel_work_sync(&rba->rx_alloc);
1225bfcc09ddSBjoern A. Zeeb 
1226bfcc09ddSBjoern A. Zeeb 	iwl_pcie_free_rbs_pool(trans);
1227bfcc09ddSBjoern A. Zeeb 
1228bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->base_rb_stts) {
1229bfcc09ddSBjoern A. Zeeb 		dma_free_coherent(trans->dev,
1230bfcc09ddSBjoern A. Zeeb 				  rb_stts_size * trans->num_rx_queues,
1231bfcc09ddSBjoern A. Zeeb 				  trans_pcie->base_rb_stts,
1232bfcc09ddSBjoern A. Zeeb 				  trans_pcie->base_rb_stts_dma);
1233bfcc09ddSBjoern A. Zeeb 		trans_pcie->base_rb_stts = NULL;
1234bfcc09ddSBjoern A. Zeeb 		trans_pcie->base_rb_stts_dma = 0;
1235bfcc09ddSBjoern A. Zeeb 	}
1236bfcc09ddSBjoern A. Zeeb 
1237bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->num_rx_queues; i++) {
1238bfcc09ddSBjoern A. Zeeb 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
1239bfcc09ddSBjoern A. Zeeb 
1240bfcc09ddSBjoern A. Zeeb 		iwl_pcie_free_rxq_dma(trans, rxq);
1241bfcc09ddSBjoern A. Zeeb 
1242bfcc09ddSBjoern A. Zeeb 		if (rxq->napi.poll) {
1243bfcc09ddSBjoern A. Zeeb 			napi_disable(&rxq->napi);
1244bfcc09ddSBjoern A. Zeeb 			netif_napi_del(&rxq->napi);
1245bfcc09ddSBjoern A. Zeeb 		}
1246bfcc09ddSBjoern A. Zeeb 	}
1247bfcc09ddSBjoern A. Zeeb 	kfree(trans_pcie->rx_pool);
1248bfcc09ddSBjoern A. Zeeb 	kfree(trans_pcie->global_table);
1249bfcc09ddSBjoern A. Zeeb 	kfree(trans_pcie->rxq);
1250bfcc09ddSBjoern A. Zeeb 
1251bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->alloc_page)
1252bfcc09ddSBjoern A. Zeeb 		__free_pages(trans_pcie->alloc_page, trans_pcie->rx_page_order);
1253bfcc09ddSBjoern A. Zeeb }
1254bfcc09ddSBjoern A. Zeeb 
1255bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_move_to_allocator(struct iwl_rxq *rxq,
1256bfcc09ddSBjoern A. Zeeb 					  struct iwl_rb_allocator *rba)
1257bfcc09ddSBjoern A. Zeeb {
1258bfcc09ddSBjoern A. Zeeb 	spin_lock(&rba->lock);
1259bfcc09ddSBjoern A. Zeeb 	list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
1260bfcc09ddSBjoern A. Zeeb 	spin_unlock(&rba->lock);
1261bfcc09ddSBjoern A. Zeeb }
1262bfcc09ddSBjoern A. Zeeb 
1263bfcc09ddSBjoern A. Zeeb /*
1264bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
1265bfcc09ddSBjoern A. Zeeb  *
1266bfcc09ddSBjoern A. Zeeb  * Called when a RBD can be reused. The RBD is transferred to the allocator.
1267bfcc09ddSBjoern A. Zeeb  * When there are 2 empty RBDs - a request for allocation is posted
1268bfcc09ddSBjoern A. Zeeb  */
1269bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
1270bfcc09ddSBjoern A. Zeeb 				  struct iwl_rx_mem_buffer *rxb,
1271bfcc09ddSBjoern A. Zeeb 				  struct iwl_rxq *rxq, bool emergency)
1272bfcc09ddSBjoern A. Zeeb {
1273bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1274bfcc09ddSBjoern A. Zeeb 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
1275bfcc09ddSBjoern A. Zeeb 
1276bfcc09ddSBjoern A. Zeeb 	/* Move the RBD to the used list, will be moved to allocator in batches
1277bfcc09ddSBjoern A. Zeeb 	 * before claiming or posting a request*/
1278bfcc09ddSBjoern A. Zeeb 	list_add_tail(&rxb->list, &rxq->rx_used);
1279bfcc09ddSBjoern A. Zeeb 
1280bfcc09ddSBjoern A. Zeeb 	if (unlikely(emergency))
1281bfcc09ddSBjoern A. Zeeb 		return;
1282bfcc09ddSBjoern A. Zeeb 
1283bfcc09ddSBjoern A. Zeeb 	/* Count the allocator owned RBDs */
1284bfcc09ddSBjoern A. Zeeb 	rxq->used_count++;
1285bfcc09ddSBjoern A. Zeeb 
1286bfcc09ddSBjoern A. Zeeb 	/* If we have RX_POST_REQ_ALLOC new released rx buffers -
1287bfcc09ddSBjoern A. Zeeb 	 * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
1288bfcc09ddSBjoern A. Zeeb 	 * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
1289bfcc09ddSBjoern A. Zeeb 	 * after but we still need to post another request.
1290bfcc09ddSBjoern A. Zeeb 	 */
1291bfcc09ddSBjoern A. Zeeb 	if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
1292bfcc09ddSBjoern A. Zeeb 		/* Move the 2 RBDs to the allocator ownership.
1293bfcc09ddSBjoern A. Zeeb 		 Allocator has another 6 from pool for the request completion*/
1294bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rx_move_to_allocator(rxq, rba);
1295bfcc09ddSBjoern A. Zeeb 
1296bfcc09ddSBjoern A. Zeeb 		atomic_inc(&rba->req_pending);
1297bfcc09ddSBjoern A. Zeeb 		queue_work(rba->alloc_wq, &rba->rx_alloc);
1298bfcc09ddSBjoern A. Zeeb 	}
1299bfcc09ddSBjoern A. Zeeb }
1300bfcc09ddSBjoern A. Zeeb 
1301bfcc09ddSBjoern A. Zeeb static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
1302bfcc09ddSBjoern A. Zeeb 				struct iwl_rxq *rxq,
1303bfcc09ddSBjoern A. Zeeb 				struct iwl_rx_mem_buffer *rxb,
1304bfcc09ddSBjoern A. Zeeb 				bool emergency,
1305bfcc09ddSBjoern A. Zeeb 				int i)
1306bfcc09ddSBjoern A. Zeeb {
1307bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1308*a4128aadSBjoern A. Zeeb 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans_pcie->txqs.cmd.q_id];
1309bfcc09ddSBjoern A. Zeeb 	bool page_stolen = false;
1310bfcc09ddSBjoern A. Zeeb 	int max_len = trans_pcie->rx_buf_bytes;
1311bfcc09ddSBjoern A. Zeeb 	u32 offset = 0;
1312bfcc09ddSBjoern A. Zeeb 
1313bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(!rxb))
1314bfcc09ddSBjoern A. Zeeb 		return;
1315bfcc09ddSBjoern A. Zeeb 
1316bfcc09ddSBjoern A. Zeeb 	dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
1317bfcc09ddSBjoern A. Zeeb 
1318bfcc09ddSBjoern A. Zeeb 	while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
1319bfcc09ddSBjoern A. Zeeb 		struct iwl_rx_packet *pkt;
1320bfcc09ddSBjoern A. Zeeb 		bool reclaim;
1321bfcc09ddSBjoern A. Zeeb 		int len;
1322bfcc09ddSBjoern A. Zeeb 		struct iwl_rx_cmd_buffer rxcb = {
1323bfcc09ddSBjoern A. Zeeb 			._offset = rxb->offset + offset,
1324bfcc09ddSBjoern A. Zeeb 			._rx_page_order = trans_pcie->rx_page_order,
1325bfcc09ddSBjoern A. Zeeb 			._page = rxb->page,
1326bfcc09ddSBjoern A. Zeeb 			._page_stolen = false,
1327bfcc09ddSBjoern A. Zeeb 			.truesize = max_len,
1328bfcc09ddSBjoern A. Zeeb 		};
1329bfcc09ddSBjoern A. Zeeb 
1330bfcc09ddSBjoern A. Zeeb 		pkt = rxb_addr(&rxcb);
1331bfcc09ddSBjoern A. Zeeb 
1332bfcc09ddSBjoern A. Zeeb 		if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID)) {
1333bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_RX(trans,
1334bfcc09ddSBjoern A. Zeeb 				     "Q %d: RB end marker at offset %d\n",
1335bfcc09ddSBjoern A. Zeeb 				     rxq->id, offset);
1336bfcc09ddSBjoern A. Zeeb 			break;
1337bfcc09ddSBjoern A. Zeeb 		}
1338bfcc09ddSBjoern A. Zeeb 
1339bfcc09ddSBjoern A. Zeeb 		WARN((le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_RXQ_MASK) >>
1340bfcc09ddSBjoern A. Zeeb 			FH_RSCSR_RXQ_POS != rxq->id,
1341bfcc09ddSBjoern A. Zeeb 		     "frame on invalid queue - is on %d and indicates %d\n",
1342bfcc09ddSBjoern A. Zeeb 		     rxq->id,
1343bfcc09ddSBjoern A. Zeeb 		     (le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_RXQ_MASK) >>
1344bfcc09ddSBjoern A. Zeeb 			FH_RSCSR_RXQ_POS);
1345bfcc09ddSBjoern A. Zeeb 
1346bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_RX(trans,
1347bfcc09ddSBjoern A. Zeeb 			     "Q %d: cmd at offset %d: %s (%.2x.%2x, seq 0x%x)\n",
1348bfcc09ddSBjoern A. Zeeb 			     rxq->id, offset,
1349bfcc09ddSBjoern A. Zeeb 			     iwl_get_cmd_string(trans,
1350d9836fb4SBjoern A. Zeeb 						WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)),
1351bfcc09ddSBjoern A. Zeeb 			     pkt->hdr.group_id, pkt->hdr.cmd,
1352bfcc09ddSBjoern A. Zeeb 			     le16_to_cpu(pkt->hdr.sequence));
1353bfcc09ddSBjoern A. Zeeb 
1354bfcc09ddSBjoern A. Zeeb 		len = iwl_rx_packet_len(pkt);
1355bfcc09ddSBjoern A. Zeeb 		len += sizeof(u32); /* account for status word */
1356bfcc09ddSBjoern A. Zeeb 
1357bfcc09ddSBjoern A. Zeeb 		offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
1358bfcc09ddSBjoern A. Zeeb 
1359bfcc09ddSBjoern A. Zeeb 		/* check that what the device tells us made sense */
1360d9836fb4SBjoern A. Zeeb 		if (len < sizeof(*pkt) || offset > max_len)
1361bfcc09ddSBjoern A. Zeeb 			break;
1362bfcc09ddSBjoern A. Zeeb 
1363*a4128aadSBjoern A. Zeeb 		maybe_trace_iwlwifi_dev_rx(trans, pkt, len);
1364bfcc09ddSBjoern A. Zeeb 
1365bfcc09ddSBjoern A. Zeeb 		/* Reclaim a command buffer only if this packet is a response
1366bfcc09ddSBjoern A. Zeeb 		 *   to a (driver-originated) command.
1367bfcc09ddSBjoern A. Zeeb 		 * If the packet (e.g. Rx frame) originated from uCode,
1368bfcc09ddSBjoern A. Zeeb 		 *   there is no command buffer to reclaim.
1369bfcc09ddSBjoern A. Zeeb 		 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1370bfcc09ddSBjoern A. Zeeb 		 *   but apparently a few don't get set; catch them here. */
1371bfcc09ddSBjoern A. Zeeb 		reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
1372bfcc09ddSBjoern A. Zeeb 		if (reclaim && !pkt->hdr.group_id) {
1373bfcc09ddSBjoern A. Zeeb 			int i;
1374bfcc09ddSBjoern A. Zeeb 
1375bfcc09ddSBjoern A. Zeeb 			for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
1376bfcc09ddSBjoern A. Zeeb 				if (trans_pcie->no_reclaim_cmds[i] ==
1377bfcc09ddSBjoern A. Zeeb 							pkt->hdr.cmd) {
1378bfcc09ddSBjoern A. Zeeb 					reclaim = false;
1379bfcc09ddSBjoern A. Zeeb 					break;
1380bfcc09ddSBjoern A. Zeeb 				}
1381bfcc09ddSBjoern A. Zeeb 			}
1382bfcc09ddSBjoern A. Zeeb 		}
1383bfcc09ddSBjoern A. Zeeb 
1384*a4128aadSBjoern A. Zeeb 		if (rxq->id == IWL_DEFAULT_RX_QUEUE)
1385bfcc09ddSBjoern A. Zeeb 			iwl_op_mode_rx(trans->op_mode, &rxq->napi,
1386bfcc09ddSBjoern A. Zeeb 				       &rxcb);
1387bfcc09ddSBjoern A. Zeeb 		else
1388bfcc09ddSBjoern A. Zeeb 			iwl_op_mode_rx_rss(trans->op_mode, &rxq->napi,
1389bfcc09ddSBjoern A. Zeeb 					   &rxcb, rxq->id);
1390bfcc09ddSBjoern A. Zeeb 
1391bfcc09ddSBjoern A. Zeeb 		/*
1392bfcc09ddSBjoern A. Zeeb 		 * After here, we should always check rxcb._page_stolen,
1393bfcc09ddSBjoern A. Zeeb 		 * if it is true then one of the handlers took the page.
1394bfcc09ddSBjoern A. Zeeb 		 */
1395bfcc09ddSBjoern A. Zeeb 
1396*a4128aadSBjoern A. Zeeb 		if (reclaim && txq) {
1397bfcc09ddSBjoern A. Zeeb 			u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1398bfcc09ddSBjoern A. Zeeb 			int index = SEQ_TO_INDEX(sequence);
1399bfcc09ddSBjoern A. Zeeb 			int cmd_index = iwl_txq_get_cmd_index(txq, index);
1400bfcc09ddSBjoern A. Zeeb 
1401bfcc09ddSBjoern A. Zeeb 			kfree_sensitive(txq->entries[cmd_index].free_buf);
1402bfcc09ddSBjoern A. Zeeb 			txq->entries[cmd_index].free_buf = NULL;
1403bfcc09ddSBjoern A. Zeeb 
1404bfcc09ddSBjoern A. Zeeb 			/* Invoke any callbacks, transfer the buffer to caller,
1405bfcc09ddSBjoern A. Zeeb 			 * and fire off the (possibly) blocking
1406bfcc09ddSBjoern A. Zeeb 			 * iwl_trans_send_cmd()
1407bfcc09ddSBjoern A. Zeeb 			 * as we reclaim the driver command queue */
1408bfcc09ddSBjoern A. Zeeb 			if (!rxcb._page_stolen)
1409bfcc09ddSBjoern A. Zeeb 				iwl_pcie_hcmd_complete(trans, &rxcb);
1410bfcc09ddSBjoern A. Zeeb 			else
1411bfcc09ddSBjoern A. Zeeb 				IWL_WARN(trans, "Claim null rxb?\n");
1412bfcc09ddSBjoern A. Zeeb 		}
1413bfcc09ddSBjoern A. Zeeb 
1414bfcc09ddSBjoern A. Zeeb 		page_stolen |= rxcb._page_stolen;
1415bfcc09ddSBjoern A. Zeeb 		if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1416bfcc09ddSBjoern A. Zeeb 			break;
1417bfcc09ddSBjoern A. Zeeb 	}
1418bfcc09ddSBjoern A. Zeeb 
1419bfcc09ddSBjoern A. Zeeb 	/* page was stolen from us -- free our reference */
1420bfcc09ddSBjoern A. Zeeb 	if (page_stolen) {
1421bfcc09ddSBjoern A. Zeeb 		__free_pages(rxb->page, trans_pcie->rx_page_order);
1422bfcc09ddSBjoern A. Zeeb 		rxb->page = NULL;
1423bfcc09ddSBjoern A. Zeeb 	}
1424bfcc09ddSBjoern A. Zeeb 
1425bfcc09ddSBjoern A. Zeeb 	/* Reuse the page if possible. For notification packets and
1426bfcc09ddSBjoern A. Zeeb 	 * SKBs that fail to Rx correctly, add them back into the
1427bfcc09ddSBjoern A. Zeeb 	 * rx_free list for reuse later. */
1428bfcc09ddSBjoern A. Zeeb 	if (rxb->page != NULL) {
1429bfcc09ddSBjoern A. Zeeb 		rxb->page_dma =
1430bfcc09ddSBjoern A. Zeeb 			dma_map_page(trans->dev, rxb->page, rxb->offset,
1431bfcc09ddSBjoern A. Zeeb 				     trans_pcie->rx_buf_bytes,
1432bfcc09ddSBjoern A. Zeeb 				     DMA_FROM_DEVICE);
1433bfcc09ddSBjoern A. Zeeb 		if (dma_mapping_error(trans->dev, rxb->page_dma)) {
1434bfcc09ddSBjoern A. Zeeb 			/*
1435bfcc09ddSBjoern A. Zeeb 			 * free the page(s) as well to not break
1436bfcc09ddSBjoern A. Zeeb 			 * the invariant that the items on the used
1437bfcc09ddSBjoern A. Zeeb 			 * list have no page(s)
1438bfcc09ddSBjoern A. Zeeb 			 */
1439bfcc09ddSBjoern A. Zeeb 			__free_pages(rxb->page, trans_pcie->rx_page_order);
1440bfcc09ddSBjoern A. Zeeb 			rxb->page = NULL;
1441bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
1442bfcc09ddSBjoern A. Zeeb 		} else {
1443bfcc09ddSBjoern A. Zeeb 			list_add_tail(&rxb->list, &rxq->rx_free);
1444bfcc09ddSBjoern A. Zeeb 			rxq->free_count++;
1445bfcc09ddSBjoern A. Zeeb 		}
1446bfcc09ddSBjoern A. Zeeb 	} else
1447bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
1448bfcc09ddSBjoern A. Zeeb }
1449bfcc09ddSBjoern A. Zeeb 
1450bfcc09ddSBjoern A. Zeeb static struct iwl_rx_mem_buffer *iwl_pcie_get_rxb(struct iwl_trans *trans,
1451bfcc09ddSBjoern A. Zeeb 						  struct iwl_rxq *rxq, int i,
1452bfcc09ddSBjoern A. Zeeb 						  bool *join)
1453bfcc09ddSBjoern A. Zeeb {
1454bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1455bfcc09ddSBjoern A. Zeeb 	struct iwl_rx_mem_buffer *rxb;
1456bfcc09ddSBjoern A. Zeeb 	u16 vid;
1457bfcc09ddSBjoern A. Zeeb 
1458bfcc09ddSBjoern A. Zeeb 	BUILD_BUG_ON(sizeof(struct iwl_rx_completion_desc) != 32);
1459d9836fb4SBjoern A. Zeeb 	BUILD_BUG_ON(sizeof(struct iwl_rx_completion_desc_bz) != 4);
1460bfcc09ddSBjoern A. Zeeb 
1461bfcc09ddSBjoern A. Zeeb 	if (!trans->trans_cfg->mq_rx_supported) {
1462bfcc09ddSBjoern A. Zeeb 		rxb = rxq->queue[i];
1463bfcc09ddSBjoern A. Zeeb 		rxq->queue[i] = NULL;
1464bfcc09ddSBjoern A. Zeeb 		return rxb;
1465bfcc09ddSBjoern A. Zeeb 	}
1466bfcc09ddSBjoern A. Zeeb 
1467d9836fb4SBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
1468d9836fb4SBjoern A. Zeeb 		struct iwl_rx_completion_desc_bz *cd = rxq->used_bd;
1469d9836fb4SBjoern A. Zeeb 
1470d9836fb4SBjoern A. Zeeb 		vid = le16_to_cpu(cd[i].rbid);
1471d9836fb4SBjoern A. Zeeb 		*join = cd[i].flags & IWL_RX_CD_FLAGS_FRAGMENTED;
1472d9836fb4SBjoern A. Zeeb 	} else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
1473d9836fb4SBjoern A. Zeeb 		struct iwl_rx_completion_desc *cd = rxq->used_bd;
1474d9836fb4SBjoern A. Zeeb 
1475d9836fb4SBjoern A. Zeeb 		vid = le16_to_cpu(cd[i].rbid);
1476d9836fb4SBjoern A. Zeeb 		*join = cd[i].flags & IWL_RX_CD_FLAGS_FRAGMENTED;
1477bfcc09ddSBjoern A. Zeeb 	} else {
1478d9836fb4SBjoern A. Zeeb 		__le32 *cd = rxq->used_bd;
1479d9836fb4SBjoern A. Zeeb 
1480d9836fb4SBjoern A. Zeeb 		vid = le32_to_cpu(cd[i]) & 0x0FFF; /* 12-bit VID */
1481bfcc09ddSBjoern A. Zeeb 	}
1482bfcc09ddSBjoern A. Zeeb 
1483bfcc09ddSBjoern A. Zeeb 	if (!vid || vid > RX_POOL_SIZE(trans_pcie->num_rx_bufs))
1484bfcc09ddSBjoern A. Zeeb 		goto out_err;
1485bfcc09ddSBjoern A. Zeeb 
1486bfcc09ddSBjoern A. Zeeb 	rxb = trans_pcie->global_table[vid - 1];
1487bfcc09ddSBjoern A. Zeeb 	if (rxb->invalid)
1488bfcc09ddSBjoern A. Zeeb 		goto out_err;
1489bfcc09ddSBjoern A. Zeeb 
1490bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_RX(trans, "Got virtual RB ID %u\n", (u32)rxb->vid);
1491bfcc09ddSBjoern A. Zeeb 
1492bfcc09ddSBjoern A. Zeeb 	rxb->invalid = true;
1493bfcc09ddSBjoern A. Zeeb 
1494bfcc09ddSBjoern A. Zeeb 	return rxb;
1495bfcc09ddSBjoern A. Zeeb 
1496bfcc09ddSBjoern A. Zeeb out_err:
1497bfcc09ddSBjoern A. Zeeb 	WARN(1, "Invalid rxb from HW %u\n", (u32)vid);
1498bfcc09ddSBjoern A. Zeeb 	iwl_force_nmi(trans);
1499bfcc09ddSBjoern A. Zeeb 	return NULL;
1500bfcc09ddSBjoern A. Zeeb }
1501bfcc09ddSBjoern A. Zeeb 
1502bfcc09ddSBjoern A. Zeeb /*
1503bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
1504bfcc09ddSBjoern A. Zeeb  */
1505bfcc09ddSBjoern A. Zeeb static int iwl_pcie_rx_handle(struct iwl_trans *trans, int queue, int budget)
1506bfcc09ddSBjoern A. Zeeb {
1507bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1508bfcc09ddSBjoern A. Zeeb 	struct iwl_rxq *rxq;
1509bfcc09ddSBjoern A. Zeeb 	u32 r, i, count = 0, handled = 0;
1510bfcc09ddSBjoern A. Zeeb 	bool emergency = false;
1511bfcc09ddSBjoern A. Zeeb 
1512bfcc09ddSBjoern A. Zeeb 	if (WARN_ON_ONCE(!trans_pcie->rxq || !trans_pcie->rxq[queue].bd))
1513bfcc09ddSBjoern A. Zeeb 		return budget;
1514bfcc09ddSBjoern A. Zeeb 
1515bfcc09ddSBjoern A. Zeeb 	rxq = &trans_pcie->rxq[queue];
1516bfcc09ddSBjoern A. Zeeb 
1517bfcc09ddSBjoern A. Zeeb restart:
1518bfcc09ddSBjoern A. Zeeb 	spin_lock(&rxq->lock);
1519bfcc09ddSBjoern A. Zeeb 	/* uCode's read index (stored in shared DRAM) indicates the last Rx
1520bfcc09ddSBjoern A. Zeeb 	 * buffer that the driver may process (last buffer filled by ucode). */
1521*a4128aadSBjoern A. Zeeb 	r = iwl_get_closed_rb_stts(trans, rxq);
1522bfcc09ddSBjoern A. Zeeb 	i = rxq->read;
1523bfcc09ddSBjoern A. Zeeb 
1524bfcc09ddSBjoern A. Zeeb 	/* W/A 9000 device step A0 wrap-around bug */
1525bfcc09ddSBjoern A. Zeeb 	r &= (rxq->queue_size - 1);
1526bfcc09ddSBjoern A. Zeeb 
1527bfcc09ddSBjoern A. Zeeb 	/* Rx interrupt, but nothing sent from uCode */
1528bfcc09ddSBjoern A. Zeeb 	if (i == r)
1529bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_RX(trans, "Q %d: HW = SW = %d\n", rxq->id, r);
1530bfcc09ddSBjoern A. Zeeb 
1531bfcc09ddSBjoern A. Zeeb 	while (i != r && ++handled < budget) {
1532bfcc09ddSBjoern A. Zeeb 		struct iwl_rb_allocator *rba = &trans_pcie->rba;
1533bfcc09ddSBjoern A. Zeeb 		struct iwl_rx_mem_buffer *rxb;
1534bfcc09ddSBjoern A. Zeeb 		/* number of RBDs still waiting for page allocation */
1535bfcc09ddSBjoern A. Zeeb 		u32 rb_pending_alloc =
1536bfcc09ddSBjoern A. Zeeb 			atomic_read(&trans_pcie->rba.req_pending) *
1537bfcc09ddSBjoern A. Zeeb 			RX_CLAIM_REQ_ALLOC;
1538bfcc09ddSBjoern A. Zeeb 		bool join = false;
1539bfcc09ddSBjoern A. Zeeb 
1540bfcc09ddSBjoern A. Zeeb 		if (unlikely(rb_pending_alloc >= rxq->queue_size / 2 &&
1541bfcc09ddSBjoern A. Zeeb 			     !emergency)) {
1542bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rx_move_to_allocator(rxq, rba);
1543bfcc09ddSBjoern A. Zeeb 			emergency = true;
1544bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_TPT(trans,
1545bfcc09ddSBjoern A. Zeeb 				      "RX path is in emergency. Pending allocations %d\n",
1546bfcc09ddSBjoern A. Zeeb 				      rb_pending_alloc);
1547bfcc09ddSBjoern A. Zeeb 		}
1548bfcc09ddSBjoern A. Zeeb 
1549bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_RX(trans, "Q %d: HW = %d, SW = %d\n", rxq->id, r, i);
1550bfcc09ddSBjoern A. Zeeb 
1551bfcc09ddSBjoern A. Zeeb 		rxb = iwl_pcie_get_rxb(trans, rxq, i, &join);
1552bfcc09ddSBjoern A. Zeeb 		if (!rxb)
1553bfcc09ddSBjoern A. Zeeb 			goto out;
1554bfcc09ddSBjoern A. Zeeb 
1555bfcc09ddSBjoern A. Zeeb 		if (unlikely(join || rxq->next_rb_is_fragment)) {
1556bfcc09ddSBjoern A. Zeeb 			rxq->next_rb_is_fragment = join;
1557bfcc09ddSBjoern A. Zeeb 			/*
1558bfcc09ddSBjoern A. Zeeb 			 * We can only get a multi-RB in the following cases:
1559bfcc09ddSBjoern A. Zeeb 			 *  - firmware issue, sending a too big notification
1560bfcc09ddSBjoern A. Zeeb 			 *  - sniffer mode with a large A-MSDU
1561bfcc09ddSBjoern A. Zeeb 			 *  - large MTU frames (>2k)
1562bfcc09ddSBjoern A. Zeeb 			 * since the multi-RB functionality is limited to newer
1563bfcc09ddSBjoern A. Zeeb 			 * hardware that cannot put multiple entries into a
1564bfcc09ddSBjoern A. Zeeb 			 * single RB.
1565bfcc09ddSBjoern A. Zeeb 			 *
1566bfcc09ddSBjoern A. Zeeb 			 * Right now, the higher layers aren't set up to deal
1567bfcc09ddSBjoern A. Zeeb 			 * with that, so discard all of these.
1568bfcc09ddSBjoern A. Zeeb 			 */
1569bfcc09ddSBjoern A. Zeeb 			list_add_tail(&rxb->list, &rxq->rx_free);
1570bfcc09ddSBjoern A. Zeeb 			rxq->free_count++;
1571bfcc09ddSBjoern A. Zeeb 		} else {
1572bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rx_handle_rb(trans, rxq, rxb, emergency, i);
1573bfcc09ddSBjoern A. Zeeb 		}
1574bfcc09ddSBjoern A. Zeeb 
1575bfcc09ddSBjoern A. Zeeb 		i = (i + 1) & (rxq->queue_size - 1);
1576bfcc09ddSBjoern A. Zeeb 
1577bfcc09ddSBjoern A. Zeeb 		/*
1578bfcc09ddSBjoern A. Zeeb 		 * If we have RX_CLAIM_REQ_ALLOC released rx buffers -
1579bfcc09ddSBjoern A. Zeeb 		 * try to claim the pre-allocated buffers from the allocator.
1580bfcc09ddSBjoern A. Zeeb 		 * If not ready - will try to reclaim next time.
1581bfcc09ddSBjoern A. Zeeb 		 * There is no need to reschedule work - allocator exits only
1582bfcc09ddSBjoern A. Zeeb 		 * on success
1583bfcc09ddSBjoern A. Zeeb 		 */
1584bfcc09ddSBjoern A. Zeeb 		if (rxq->used_count >= RX_CLAIM_REQ_ALLOC)
1585bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rx_allocator_get(trans, rxq);
1586bfcc09ddSBjoern A. Zeeb 
1587bfcc09ddSBjoern A. Zeeb 		if (rxq->used_count % RX_CLAIM_REQ_ALLOC == 0 && !emergency) {
1588bfcc09ddSBjoern A. Zeeb 			/* Add the remaining empty RBDs for allocator use */
1589bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rx_move_to_allocator(rxq, rba);
1590bfcc09ddSBjoern A. Zeeb 		} else if (emergency) {
1591bfcc09ddSBjoern A. Zeeb 			count++;
1592bfcc09ddSBjoern A. Zeeb 			if (count == 8) {
1593bfcc09ddSBjoern A. Zeeb 				count = 0;
1594bfcc09ddSBjoern A. Zeeb 				if (rb_pending_alloc < rxq->queue_size / 3) {
1595bfcc09ddSBjoern A. Zeeb 					IWL_DEBUG_TPT(trans,
1596bfcc09ddSBjoern A. Zeeb 						      "RX path exited emergency. Pending allocations %d\n",
1597bfcc09ddSBjoern A. Zeeb 						      rb_pending_alloc);
1598bfcc09ddSBjoern A. Zeeb 					emergency = false;
1599bfcc09ddSBjoern A. Zeeb 				}
1600bfcc09ddSBjoern A. Zeeb 
1601bfcc09ddSBjoern A. Zeeb 				rxq->read = i;
1602bfcc09ddSBjoern A. Zeeb 				spin_unlock(&rxq->lock);
1603bfcc09ddSBjoern A. Zeeb 				iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
1604bfcc09ddSBjoern A. Zeeb 				iwl_pcie_rxq_restock(trans, rxq);
1605bfcc09ddSBjoern A. Zeeb 				goto restart;
1606bfcc09ddSBjoern A. Zeeb 			}
1607bfcc09ddSBjoern A. Zeeb 		}
1608bfcc09ddSBjoern A. Zeeb 	}
1609bfcc09ddSBjoern A. Zeeb out:
1610bfcc09ddSBjoern A. Zeeb 	/* Backtrack one entry */
1611bfcc09ddSBjoern A. Zeeb 	rxq->read = i;
1612bfcc09ddSBjoern A. Zeeb 	spin_unlock(&rxq->lock);
1613bfcc09ddSBjoern A. Zeeb 
1614bfcc09ddSBjoern A. Zeeb 	/*
1615bfcc09ddSBjoern A. Zeeb 	 * handle a case where in emergency there are some unallocated RBDs.
1616bfcc09ddSBjoern A. Zeeb 	 * those RBDs are in the used list, but are not tracked by the queue's
1617bfcc09ddSBjoern A. Zeeb 	 * used_count which counts allocator owned RBDs.
1618bfcc09ddSBjoern A. Zeeb 	 * unallocated emergency RBDs must be allocated on exit, otherwise
1619bfcc09ddSBjoern A. Zeeb 	 * when called again the function may not be in emergency mode and
1620bfcc09ddSBjoern A. Zeeb 	 * they will be handed to the allocator with no tracking in the RBD
1621bfcc09ddSBjoern A. Zeeb 	 * allocator counters, which will lead to them never being claimed back
1622bfcc09ddSBjoern A. Zeeb 	 * by the queue.
1623bfcc09ddSBjoern A. Zeeb 	 * by allocating them here, they are now in the queue free list, and
1624bfcc09ddSBjoern A. Zeeb 	 * will be restocked by the next call of iwl_pcie_rxq_restock.
1625bfcc09ddSBjoern A. Zeeb 	 */
1626bfcc09ddSBjoern A. Zeeb 	if (unlikely(emergency && count))
1627bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
1628bfcc09ddSBjoern A. Zeeb 
1629bfcc09ddSBjoern A. Zeeb 	iwl_pcie_rxq_restock(trans, rxq);
1630bfcc09ddSBjoern A. Zeeb 
1631bfcc09ddSBjoern A. Zeeb 	return handled;
1632bfcc09ddSBjoern A. Zeeb }
1633bfcc09ddSBjoern A. Zeeb 
1634bfcc09ddSBjoern A. Zeeb static struct iwl_trans_pcie *iwl_pcie_get_trans_pcie(struct msix_entry *entry)
1635bfcc09ddSBjoern A. Zeeb {
1636bfcc09ddSBjoern A. Zeeb 	u8 queue = entry->entry;
1637bfcc09ddSBjoern A. Zeeb 	struct msix_entry *entries = entry - queue;
1638bfcc09ddSBjoern A. Zeeb 
1639bfcc09ddSBjoern A. Zeeb 	return container_of(entries, struct iwl_trans_pcie, msix_entries[0]);
1640bfcc09ddSBjoern A. Zeeb }
1641bfcc09ddSBjoern A. Zeeb 
1642bfcc09ddSBjoern A. Zeeb /*
1643bfcc09ddSBjoern A. Zeeb  * iwl_pcie_rx_msix_handle - Main entry function for receiving responses from fw
1644bfcc09ddSBjoern A. Zeeb  * This interrupt handler should be used with RSS queue only.
1645bfcc09ddSBjoern A. Zeeb  */
1646bfcc09ddSBjoern A. Zeeb irqreturn_t iwl_pcie_irq_rx_msix_handler(int irq, void *dev_id)
1647bfcc09ddSBjoern A. Zeeb {
1648bfcc09ddSBjoern A. Zeeb 	struct msix_entry *entry = dev_id;
1649bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = iwl_pcie_get_trans_pcie(entry);
1650bfcc09ddSBjoern A. Zeeb 	struct iwl_trans *trans = trans_pcie->trans;
16519af1bba4SBjoern A. Zeeb 	struct iwl_rxq *rxq;
1652bfcc09ddSBjoern A. Zeeb 
1653bfcc09ddSBjoern A. Zeeb 	trace_iwlwifi_dev_irq_msix(trans->dev, entry, false, 0, 0);
1654bfcc09ddSBjoern A. Zeeb 
1655bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(entry->entry >= trans->num_rx_queues))
1656bfcc09ddSBjoern A. Zeeb 		return IRQ_NONE;
1657bfcc09ddSBjoern A. Zeeb 
16589af1bba4SBjoern A. Zeeb 	if (!trans_pcie->rxq) {
1659d9836fb4SBjoern A. Zeeb 		if (net_ratelimit())
1660d9836fb4SBjoern A. Zeeb 			IWL_ERR(trans,
1661d9836fb4SBjoern A. Zeeb 				"[%d] Got MSI-X interrupt before we have Rx queues\n",
1662d9836fb4SBjoern A. Zeeb 				entry->entry);
1663bfcc09ddSBjoern A. Zeeb 		return IRQ_NONE;
1664d9836fb4SBjoern A. Zeeb 	}
1665bfcc09ddSBjoern A. Zeeb 
16669af1bba4SBjoern A. Zeeb 	rxq = &trans_pcie->rxq[entry->entry];
1667bfcc09ddSBjoern A. Zeeb 	lock_map_acquire(&trans->sync_cmd_lockdep_map);
1668bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_ISR(trans, "[%d] Got interrupt\n", entry->entry);
1669bfcc09ddSBjoern A. Zeeb 
1670bfcc09ddSBjoern A. Zeeb 	local_bh_disable();
1671*a4128aadSBjoern A. Zeeb 	if (!napi_schedule(&rxq->napi))
1672bfcc09ddSBjoern A. Zeeb 		iwl_pcie_clear_irq(trans, entry->entry);
1673bfcc09ddSBjoern A. Zeeb 	local_bh_enable();
1674bfcc09ddSBjoern A. Zeeb 
1675bfcc09ddSBjoern A. Zeeb 	lock_map_release(&trans->sync_cmd_lockdep_map);
1676bfcc09ddSBjoern A. Zeeb 
1677bfcc09ddSBjoern A. Zeeb 	return IRQ_HANDLED;
1678bfcc09ddSBjoern A. Zeeb }
1679bfcc09ddSBjoern A. Zeeb 
1680bfcc09ddSBjoern A. Zeeb /*
1681bfcc09ddSBjoern A. Zeeb  * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
1682bfcc09ddSBjoern A. Zeeb  */
1683bfcc09ddSBjoern A. Zeeb static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
1684bfcc09ddSBjoern A. Zeeb {
1685*a4128aadSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1686bfcc09ddSBjoern A. Zeeb 	int i;
1687bfcc09ddSBjoern A. Zeeb 
1688bfcc09ddSBjoern A. Zeeb 	/* W/A for WiFi/WiMAX coex and WiMAX own the RF */
1689bfcc09ddSBjoern A. Zeeb 	if (trans->cfg->internal_wimax_coex &&
1690bfcc09ddSBjoern A. Zeeb 	    !trans->cfg->apmg_not_supported &&
1691bfcc09ddSBjoern A. Zeeb 	    (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
1692bfcc09ddSBjoern A. Zeeb 			     APMS_CLK_VAL_MRB_FUNC_MODE) ||
1693bfcc09ddSBjoern A. Zeeb 	     (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
1694bfcc09ddSBjoern A. Zeeb 			    APMG_PS_CTRL_VAL_RESET_REQ))) {
1695bfcc09ddSBjoern A. Zeeb 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1696bfcc09ddSBjoern A. Zeeb 		iwl_op_mode_wimax_active(trans->op_mode);
1697bfcc09ddSBjoern A. Zeeb 		wake_up(&trans->wait_command_queue);
1698bfcc09ddSBjoern A. Zeeb 		return;
1699bfcc09ddSBjoern A. Zeeb 	}
1700bfcc09ddSBjoern A. Zeeb 
1701bfcc09ddSBjoern A. Zeeb 	for (i = 0; i < trans->trans_cfg->base_params->num_of_queues; i++) {
1702*a4128aadSBjoern A. Zeeb 		if (!trans_pcie->txqs.txq[i])
1703bfcc09ddSBjoern A. Zeeb 			continue;
1704*a4128aadSBjoern A. Zeeb 		del_timer(&trans_pcie->txqs.txq[i]->stuck_timer);
1705bfcc09ddSBjoern A. Zeeb 	}
1706bfcc09ddSBjoern A. Zeeb 
1707bfcc09ddSBjoern A. Zeeb 	/* The STATUS_FW_ERROR bit is set in this function. This must happen
1708bfcc09ddSBjoern A. Zeeb 	 * before we wake up the command caller, to ensure a proper cleanup. */
1709bfcc09ddSBjoern A. Zeeb 	iwl_trans_fw_error(trans, false);
1710bfcc09ddSBjoern A. Zeeb 
1711bfcc09ddSBjoern A. Zeeb 	clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1712bfcc09ddSBjoern A. Zeeb 	wake_up(&trans->wait_command_queue);
1713bfcc09ddSBjoern A. Zeeb }
1714bfcc09ddSBjoern A. Zeeb 
1715bfcc09ddSBjoern A. Zeeb static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
1716bfcc09ddSBjoern A. Zeeb {
1717bfcc09ddSBjoern A. Zeeb 	u32 inta;
1718bfcc09ddSBjoern A. Zeeb 
1719bfcc09ddSBjoern A. Zeeb 	lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
1720bfcc09ddSBjoern A. Zeeb 
1721bfcc09ddSBjoern A. Zeeb 	trace_iwlwifi_dev_irq(trans->dev);
1722bfcc09ddSBjoern A. Zeeb 
1723bfcc09ddSBjoern A. Zeeb 	/* Discover which interrupts are active/pending */
1724bfcc09ddSBjoern A. Zeeb 	inta = iwl_read32(trans, CSR_INT);
1725bfcc09ddSBjoern A. Zeeb 
1726bfcc09ddSBjoern A. Zeeb 	/* the thread will service interrupts and re-enable them */
1727bfcc09ddSBjoern A. Zeeb 	return inta;
1728bfcc09ddSBjoern A. Zeeb }
1729bfcc09ddSBjoern A. Zeeb 
1730bfcc09ddSBjoern A. Zeeb /* a device (PCI-E) page is 4096 bytes long */
1731bfcc09ddSBjoern A. Zeeb #define ICT_SHIFT	12
1732bfcc09ddSBjoern A. Zeeb #define ICT_SIZE	(1 << ICT_SHIFT)
1733bfcc09ddSBjoern A. Zeeb #define ICT_COUNT	(ICT_SIZE / sizeof(u32))
1734bfcc09ddSBjoern A. Zeeb 
1735bfcc09ddSBjoern A. Zeeb /* interrupt handler using ict table, with this interrupt driver will
1736bfcc09ddSBjoern A. Zeeb  * stop using INTA register to get device's interrupt, reading this register
1737bfcc09ddSBjoern A. Zeeb  * is expensive, device will write interrupts in ICT dram table, increment
1738bfcc09ddSBjoern A. Zeeb  * index then will fire interrupt to driver, driver will OR all ICT table
1739bfcc09ddSBjoern A. Zeeb  * entries from current index up to table entry with 0 value. the result is
1740bfcc09ddSBjoern A. Zeeb  * the interrupt we need to service, driver will set the entries back to 0 and
1741bfcc09ddSBjoern A. Zeeb  * set index.
1742bfcc09ddSBjoern A. Zeeb  */
1743bfcc09ddSBjoern A. Zeeb static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
1744bfcc09ddSBjoern A. Zeeb {
1745bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1746bfcc09ddSBjoern A. Zeeb 	u32 inta;
1747bfcc09ddSBjoern A. Zeeb 	u32 val = 0;
1748bfcc09ddSBjoern A. Zeeb 	u32 read;
1749bfcc09ddSBjoern A. Zeeb 
1750bfcc09ddSBjoern A. Zeeb 	trace_iwlwifi_dev_irq(trans->dev);
1751bfcc09ddSBjoern A. Zeeb 
1752bfcc09ddSBjoern A. Zeeb 	/* Ignore interrupt if there's nothing in NIC to service.
1753bfcc09ddSBjoern A. Zeeb 	 * This may be due to IRQ shared with another device,
1754bfcc09ddSBjoern A. Zeeb 	 * or due to sporadic interrupts thrown from our NIC. */
1755bfcc09ddSBjoern A. Zeeb 	read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1756bfcc09ddSBjoern A. Zeeb 	trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
1757bfcc09ddSBjoern A. Zeeb 	if (!read)
1758bfcc09ddSBjoern A. Zeeb 		return 0;
1759bfcc09ddSBjoern A. Zeeb 
1760bfcc09ddSBjoern A. Zeeb 	/*
1761bfcc09ddSBjoern A. Zeeb 	 * Collect all entries up to the first 0, starting from ict_index;
1762bfcc09ddSBjoern A. Zeeb 	 * note we already read at ict_index.
1763bfcc09ddSBjoern A. Zeeb 	 */
1764bfcc09ddSBjoern A. Zeeb 	do {
1765bfcc09ddSBjoern A. Zeeb 		val |= read;
1766bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
1767bfcc09ddSBjoern A. Zeeb 				trans_pcie->ict_index, read);
1768bfcc09ddSBjoern A. Zeeb 		trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1769bfcc09ddSBjoern A. Zeeb 		trans_pcie->ict_index =
1770bfcc09ddSBjoern A. Zeeb 			((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
1771bfcc09ddSBjoern A. Zeeb 
1772bfcc09ddSBjoern A. Zeeb 		read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1773bfcc09ddSBjoern A. Zeeb 		trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
1774bfcc09ddSBjoern A. Zeeb 					   read);
1775bfcc09ddSBjoern A. Zeeb 	} while (read);
1776bfcc09ddSBjoern A. Zeeb 
1777bfcc09ddSBjoern A. Zeeb 	/* We should not get this value, just ignore it. */
1778bfcc09ddSBjoern A. Zeeb 	if (val == 0xffffffff)
1779bfcc09ddSBjoern A. Zeeb 		val = 0;
1780bfcc09ddSBjoern A. Zeeb 
1781bfcc09ddSBjoern A. Zeeb 	/*
1782bfcc09ddSBjoern A. Zeeb 	 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1783bfcc09ddSBjoern A. Zeeb 	 * (bit 15 before shifting it to 31) to clear when using interrupt
1784bfcc09ddSBjoern A. Zeeb 	 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1785bfcc09ddSBjoern A. Zeeb 	 * so we use them to decide on the real state of the Rx bit.
1786bfcc09ddSBjoern A. Zeeb 	 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1787bfcc09ddSBjoern A. Zeeb 	 */
1788bfcc09ddSBjoern A. Zeeb 	if (val & 0xC0000)
1789bfcc09ddSBjoern A. Zeeb 		val |= 0x8000;
1790bfcc09ddSBjoern A. Zeeb 
1791bfcc09ddSBjoern A. Zeeb 	inta = (0xff & val) | ((0xff00 & val) << 16);
1792bfcc09ddSBjoern A. Zeeb 	return inta;
1793bfcc09ddSBjoern A. Zeeb }
1794bfcc09ddSBjoern A. Zeeb 
1795*a4128aadSBjoern A. Zeeb void iwl_pcie_handle_rfkill_irq(struct iwl_trans *trans, bool from_irq)
1796bfcc09ddSBjoern A. Zeeb {
1797bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1798bfcc09ddSBjoern A. Zeeb 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
1799bfcc09ddSBjoern A. Zeeb 	bool hw_rfkill, prev, report;
1800bfcc09ddSBjoern A. Zeeb 
1801bfcc09ddSBjoern A. Zeeb 	mutex_lock(&trans_pcie->mutex);
1802bfcc09ddSBjoern A. Zeeb 	prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1803bfcc09ddSBjoern A. Zeeb 	hw_rfkill = iwl_is_rfkill_set(trans);
1804bfcc09ddSBjoern A. Zeeb 	if (hw_rfkill) {
1805bfcc09ddSBjoern A. Zeeb 		set_bit(STATUS_RFKILL_OPMODE, &trans->status);
1806bfcc09ddSBjoern A. Zeeb 		set_bit(STATUS_RFKILL_HW, &trans->status);
1807bfcc09ddSBjoern A. Zeeb 	}
1808bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->opmode_down)
1809bfcc09ddSBjoern A. Zeeb 		report = hw_rfkill;
1810bfcc09ddSBjoern A. Zeeb 	else
1811bfcc09ddSBjoern A. Zeeb 		report = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
1812bfcc09ddSBjoern A. Zeeb 
1813bfcc09ddSBjoern A. Zeeb 	IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
1814bfcc09ddSBjoern A. Zeeb 		 hw_rfkill ? "disable radio" : "enable radio");
1815bfcc09ddSBjoern A. Zeeb 
1816bfcc09ddSBjoern A. Zeeb 	isr_stats->rfkill++;
1817bfcc09ddSBjoern A. Zeeb 
1818bfcc09ddSBjoern A. Zeeb 	if (prev != report)
1819*a4128aadSBjoern A. Zeeb 		iwl_trans_pcie_rf_kill(trans, report, from_irq);
1820bfcc09ddSBjoern A. Zeeb 	mutex_unlock(&trans_pcie->mutex);
1821bfcc09ddSBjoern A. Zeeb 
1822bfcc09ddSBjoern A. Zeeb 	if (hw_rfkill) {
1823bfcc09ddSBjoern A. Zeeb 		if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
1824bfcc09ddSBjoern A. Zeeb 				       &trans->status))
1825bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_RF_KILL(trans,
1826bfcc09ddSBjoern A. Zeeb 					  "Rfkill while SYNC HCMD in flight\n");
1827bfcc09ddSBjoern A. Zeeb 		wake_up(&trans->wait_command_queue);
1828bfcc09ddSBjoern A. Zeeb 	} else {
1829bfcc09ddSBjoern A. Zeeb 		clear_bit(STATUS_RFKILL_HW, &trans->status);
1830bfcc09ddSBjoern A. Zeeb 		if (trans_pcie->opmode_down)
1831bfcc09ddSBjoern A. Zeeb 			clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
1832bfcc09ddSBjoern A. Zeeb 	}
1833bfcc09ddSBjoern A. Zeeb }
1834bfcc09ddSBjoern A. Zeeb 
1835bfcc09ddSBjoern A. Zeeb irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
1836bfcc09ddSBjoern A. Zeeb {
1837bfcc09ddSBjoern A. Zeeb 	struct iwl_trans *trans = dev_id;
1838bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1839bfcc09ddSBjoern A. Zeeb 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
1840bfcc09ddSBjoern A. Zeeb 	u32 inta = 0;
1841bfcc09ddSBjoern A. Zeeb 	u32 handled = 0;
1842bfcc09ddSBjoern A. Zeeb 	bool polling = false;
1843bfcc09ddSBjoern A. Zeeb 
1844bfcc09ddSBjoern A. Zeeb 	lock_map_acquire(&trans->sync_cmd_lockdep_map);
1845bfcc09ddSBjoern A. Zeeb 
1846bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&trans_pcie->irq_lock);
1847bfcc09ddSBjoern A. Zeeb 
1848bfcc09ddSBjoern A. Zeeb 	/* dram interrupt table not set yet,
1849bfcc09ddSBjoern A. Zeeb 	 * use legacy interrupt.
1850bfcc09ddSBjoern A. Zeeb 	 */
1851bfcc09ddSBjoern A. Zeeb 	if (likely(trans_pcie->use_ict))
1852bfcc09ddSBjoern A. Zeeb 		inta = iwl_pcie_int_cause_ict(trans);
1853bfcc09ddSBjoern A. Zeeb 	else
1854bfcc09ddSBjoern A. Zeeb 		inta = iwl_pcie_int_cause_non_ict(trans);
1855bfcc09ddSBjoern A. Zeeb 
1856bfcc09ddSBjoern A. Zeeb #ifdef CONFIG_IWLWIFI_DEBUG
1857bfcc09ddSBjoern A. Zeeb 	if (iwl_have_debug_level(IWL_DL_ISR)) {
1858bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans,
1859bfcc09ddSBjoern A. Zeeb 			      "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
1860bfcc09ddSBjoern A. Zeeb 			      inta, trans_pcie->inta_mask,
1861bfcc09ddSBjoern A. Zeeb 			      iwl_read32(trans, CSR_INT_MASK),
1862bfcc09ddSBjoern A. Zeeb 			      iwl_read32(trans, CSR_FH_INT_STATUS));
1863bfcc09ddSBjoern A. Zeeb 		if (inta & (~trans_pcie->inta_mask))
1864bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_ISR(trans,
1865bfcc09ddSBjoern A. Zeeb 				      "We got a masked interrupt (0x%08x)\n",
1866bfcc09ddSBjoern A. Zeeb 				      inta & (~trans_pcie->inta_mask));
1867bfcc09ddSBjoern A. Zeeb 	}
1868bfcc09ddSBjoern A. Zeeb #endif
1869bfcc09ddSBjoern A. Zeeb 
1870bfcc09ddSBjoern A. Zeeb 	inta &= trans_pcie->inta_mask;
1871bfcc09ddSBjoern A. Zeeb 
1872bfcc09ddSBjoern A. Zeeb 	/*
1873bfcc09ddSBjoern A. Zeeb 	 * Ignore interrupt if there's nothing in NIC to service.
1874bfcc09ddSBjoern A. Zeeb 	 * This may be due to IRQ shared with another device,
1875bfcc09ddSBjoern A. Zeeb 	 * or due to sporadic interrupts thrown from our NIC.
1876bfcc09ddSBjoern A. Zeeb 	 */
1877bfcc09ddSBjoern A. Zeeb 	if (unlikely(!inta)) {
1878bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
1879bfcc09ddSBjoern A. Zeeb 		/*
1880bfcc09ddSBjoern A. Zeeb 		 * Re-enable interrupts here since we don't
1881bfcc09ddSBjoern A. Zeeb 		 * have anything to service
1882bfcc09ddSBjoern A. Zeeb 		 */
1883bfcc09ddSBjoern A. Zeeb 		if (test_bit(STATUS_INT_ENABLED, &trans->status))
1884bfcc09ddSBjoern A. Zeeb 			_iwl_enable_interrupts(trans);
1885bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&trans_pcie->irq_lock);
1886bfcc09ddSBjoern A. Zeeb 		lock_map_release(&trans->sync_cmd_lockdep_map);
1887bfcc09ddSBjoern A. Zeeb 		return IRQ_NONE;
1888bfcc09ddSBjoern A. Zeeb 	}
1889bfcc09ddSBjoern A. Zeeb 
18909af1bba4SBjoern A. Zeeb 	if (unlikely(inta == 0xFFFFFFFF || iwl_trans_is_hw_error_value(inta))) {
1891bfcc09ddSBjoern A. Zeeb 		/*
1892bfcc09ddSBjoern A. Zeeb 		 * Hardware disappeared. It might have
1893bfcc09ddSBjoern A. Zeeb 		 * already raised an interrupt.
1894bfcc09ddSBjoern A. Zeeb 		 */
1895bfcc09ddSBjoern A. Zeeb 		IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1896bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&trans_pcie->irq_lock);
1897bfcc09ddSBjoern A. Zeeb 		goto out;
1898bfcc09ddSBjoern A. Zeeb 	}
1899bfcc09ddSBjoern A. Zeeb 
1900bfcc09ddSBjoern A. Zeeb 	/* Ack/clear/reset pending uCode interrupts.
1901bfcc09ddSBjoern A. Zeeb 	 * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1902bfcc09ddSBjoern A. Zeeb 	 */
1903bfcc09ddSBjoern A. Zeeb 	/* There is a hardware bug in the interrupt mask function that some
1904bfcc09ddSBjoern A. Zeeb 	 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
1905bfcc09ddSBjoern A. Zeeb 	 * they are disabled in the CSR_INT_MASK register. Furthermore the
1906bfcc09ddSBjoern A. Zeeb 	 * ICT interrupt handling mechanism has another bug that might cause
1907bfcc09ddSBjoern A. Zeeb 	 * these unmasked interrupts fail to be detected. We workaround the
1908bfcc09ddSBjoern A. Zeeb 	 * hardware bugs here by ACKing all the possible interrupts so that
1909bfcc09ddSBjoern A. Zeeb 	 * interrupt coalescing can still be achieved.
1910bfcc09ddSBjoern A. Zeeb 	 */
1911bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
1912bfcc09ddSBjoern A. Zeeb 
1913bfcc09ddSBjoern A. Zeeb #ifdef CONFIG_IWLWIFI_DEBUG
1914bfcc09ddSBjoern A. Zeeb 	if (iwl_have_debug_level(IWL_DL_ISR))
1915bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
1916bfcc09ddSBjoern A. Zeeb 			      inta, iwl_read32(trans, CSR_INT_MASK));
1917bfcc09ddSBjoern A. Zeeb #endif
1918bfcc09ddSBjoern A. Zeeb 
1919bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&trans_pcie->irq_lock);
1920bfcc09ddSBjoern A. Zeeb 
1921bfcc09ddSBjoern A. Zeeb 	/* Now service all interrupt bits discovered above. */
1922bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_HW_ERR) {
1923bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans, "Hardware error detected.  Restarting.\n");
1924bfcc09ddSBjoern A. Zeeb 
1925bfcc09ddSBjoern A. Zeeb 		/* Tell the device to stop sending interrupts */
1926bfcc09ddSBjoern A. Zeeb 		iwl_disable_interrupts(trans);
1927bfcc09ddSBjoern A. Zeeb 
1928bfcc09ddSBjoern A. Zeeb 		isr_stats->hw++;
1929bfcc09ddSBjoern A. Zeeb 		iwl_pcie_irq_handle_error(trans);
1930bfcc09ddSBjoern A. Zeeb 
1931bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_HW_ERR;
1932bfcc09ddSBjoern A. Zeeb 
1933bfcc09ddSBjoern A. Zeeb 		goto out;
1934bfcc09ddSBjoern A. Zeeb 	}
1935bfcc09ddSBjoern A. Zeeb 
1936bfcc09ddSBjoern A. Zeeb 	/* NIC fires this, but we don't use it, redundant with WAKEUP */
1937bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_SCD) {
1938bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans,
1939bfcc09ddSBjoern A. Zeeb 			      "Scheduler finished to transmit the frame/frames.\n");
1940bfcc09ddSBjoern A. Zeeb 		isr_stats->sch++;
1941bfcc09ddSBjoern A. Zeeb 	}
1942bfcc09ddSBjoern A. Zeeb 
1943bfcc09ddSBjoern A. Zeeb 	/* Alive notification via Rx interrupt will do the real work */
1944bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_ALIVE) {
1945bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Alive interrupt\n");
1946bfcc09ddSBjoern A. Zeeb 		isr_stats->alive++;
1947bfcc09ddSBjoern A. Zeeb 		if (trans->trans_cfg->gen2) {
1948bfcc09ddSBjoern A. Zeeb 			/*
1949bfcc09ddSBjoern A. Zeeb 			 * We can restock, since firmware configured
1950bfcc09ddSBjoern A. Zeeb 			 * the RFH
1951bfcc09ddSBjoern A. Zeeb 			 */
1952bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rxmq_restock(trans, trans_pcie->rxq);
1953bfcc09ddSBjoern A. Zeeb 		}
1954bfcc09ddSBjoern A. Zeeb 
1955bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_ALIVE;
1956bfcc09ddSBjoern A. Zeeb 	}
1957bfcc09ddSBjoern A. Zeeb 
1958bfcc09ddSBjoern A. Zeeb 	/* Safely ignore these bits for debug checks below */
1959bfcc09ddSBjoern A. Zeeb 	inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1960bfcc09ddSBjoern A. Zeeb 
1961bfcc09ddSBjoern A. Zeeb 	/* HW RF KILL switch toggled */
1962bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_RF_KILL) {
1963*a4128aadSBjoern A. Zeeb 		iwl_pcie_handle_rfkill_irq(trans, true);
1964bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_RF_KILL;
1965bfcc09ddSBjoern A. Zeeb 	}
1966bfcc09ddSBjoern A. Zeeb 
1967bfcc09ddSBjoern A. Zeeb 	/* Chip got too hot and stopped itself */
1968bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_CT_KILL) {
1969bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans, "Microcode CT kill error detected.\n");
1970bfcc09ddSBjoern A. Zeeb 		isr_stats->ctkill++;
1971bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_CT_KILL;
1972bfcc09ddSBjoern A. Zeeb 	}
1973bfcc09ddSBjoern A. Zeeb 
1974bfcc09ddSBjoern A. Zeeb 	/* Error detected by uCode */
1975bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_SW_ERR) {
1976bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans, "Microcode SW error detected. "
1977bfcc09ddSBjoern A. Zeeb 			" Restarting 0x%X.\n", inta);
1978bfcc09ddSBjoern A. Zeeb 		isr_stats->sw++;
1979bfcc09ddSBjoern A. Zeeb 		iwl_pcie_irq_handle_error(trans);
1980bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_SW_ERR;
1981bfcc09ddSBjoern A. Zeeb 	}
1982bfcc09ddSBjoern A. Zeeb 
1983bfcc09ddSBjoern A. Zeeb 	/* uCode wakes up after power-down sleep */
1984bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_WAKEUP) {
1985bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1986bfcc09ddSBjoern A. Zeeb 		iwl_pcie_rxq_check_wrptr(trans);
1987bfcc09ddSBjoern A. Zeeb 		iwl_pcie_txq_check_wrptrs(trans);
1988bfcc09ddSBjoern A. Zeeb 
1989bfcc09ddSBjoern A. Zeeb 		isr_stats->wakeup++;
1990bfcc09ddSBjoern A. Zeeb 
1991bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_WAKEUP;
1992bfcc09ddSBjoern A. Zeeb 	}
1993bfcc09ddSBjoern A. Zeeb 
1994bfcc09ddSBjoern A. Zeeb 	/* All uCode command responses, including Tx command responses,
1995bfcc09ddSBjoern A. Zeeb 	 * Rx "responses" (frame-received notification), and other
1996bfcc09ddSBjoern A. Zeeb 	 * notifications from uCode come through here*/
1997bfcc09ddSBjoern A. Zeeb 	if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1998bfcc09ddSBjoern A. Zeeb 		    CSR_INT_BIT_RX_PERIODIC)) {
1999bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Rx interrupt\n");
2000bfcc09ddSBjoern A. Zeeb 		if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
2001bfcc09ddSBjoern A. Zeeb 			handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2002bfcc09ddSBjoern A. Zeeb 			iwl_write32(trans, CSR_FH_INT_STATUS,
2003bfcc09ddSBjoern A. Zeeb 					CSR_FH_INT_RX_MASK);
2004bfcc09ddSBjoern A. Zeeb 		}
2005bfcc09ddSBjoern A. Zeeb 		if (inta & CSR_INT_BIT_RX_PERIODIC) {
2006bfcc09ddSBjoern A. Zeeb 			handled |= CSR_INT_BIT_RX_PERIODIC;
2007bfcc09ddSBjoern A. Zeeb 			iwl_write32(trans,
2008bfcc09ddSBjoern A. Zeeb 				CSR_INT, CSR_INT_BIT_RX_PERIODIC);
2009bfcc09ddSBjoern A. Zeeb 		}
2010bfcc09ddSBjoern A. Zeeb 		/* Sending RX interrupt require many steps to be done in the
2011d9836fb4SBjoern A. Zeeb 		 * device:
2012bfcc09ddSBjoern A. Zeeb 		 * 1- write interrupt to current index in ICT table.
2013bfcc09ddSBjoern A. Zeeb 		 * 2- dma RX frame.
2014bfcc09ddSBjoern A. Zeeb 		 * 3- update RX shared data to indicate last write index.
2015bfcc09ddSBjoern A. Zeeb 		 * 4- send interrupt.
2016bfcc09ddSBjoern A. Zeeb 		 * This could lead to RX race, driver could receive RX interrupt
2017bfcc09ddSBjoern A. Zeeb 		 * but the shared data changes does not reflect this;
2018bfcc09ddSBjoern A. Zeeb 		 * periodic interrupt will detect any dangling Rx activity.
2019bfcc09ddSBjoern A. Zeeb 		 */
2020bfcc09ddSBjoern A. Zeeb 
2021bfcc09ddSBjoern A. Zeeb 		/* Disable periodic interrupt; we use it as just a one-shot. */
2022bfcc09ddSBjoern A. Zeeb 		iwl_write8(trans, CSR_INT_PERIODIC_REG,
2023bfcc09ddSBjoern A. Zeeb 			    CSR_INT_PERIODIC_DIS);
2024bfcc09ddSBjoern A. Zeeb 
2025bfcc09ddSBjoern A. Zeeb 		/*
2026bfcc09ddSBjoern A. Zeeb 		 * Enable periodic interrupt in 8 msec only if we received
2027bfcc09ddSBjoern A. Zeeb 		 * real RX interrupt (instead of just periodic int), to catch
2028bfcc09ddSBjoern A. Zeeb 		 * any dangling Rx interrupt.  If it was just the periodic
2029bfcc09ddSBjoern A. Zeeb 		 * interrupt, there was no dangling Rx activity, and no need
2030bfcc09ddSBjoern A. Zeeb 		 * to extend the periodic interrupt; one-shot is enough.
2031bfcc09ddSBjoern A. Zeeb 		 */
2032bfcc09ddSBjoern A. Zeeb 		if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
2033bfcc09ddSBjoern A. Zeeb 			iwl_write8(trans, CSR_INT_PERIODIC_REG,
2034bfcc09ddSBjoern A. Zeeb 				   CSR_INT_PERIODIC_ENA);
2035bfcc09ddSBjoern A. Zeeb 
2036bfcc09ddSBjoern A. Zeeb 		isr_stats->rx++;
2037bfcc09ddSBjoern A. Zeeb 
2038bfcc09ddSBjoern A. Zeeb 		local_bh_disable();
2039bfcc09ddSBjoern A. Zeeb 		if (napi_schedule_prep(&trans_pcie->rxq[0].napi)) {
2040bfcc09ddSBjoern A. Zeeb 			polling = true;
2041bfcc09ddSBjoern A. Zeeb 			__napi_schedule(&trans_pcie->rxq[0].napi);
2042bfcc09ddSBjoern A. Zeeb 		}
2043bfcc09ddSBjoern A. Zeeb 		local_bh_enable();
2044bfcc09ddSBjoern A. Zeeb 	}
2045bfcc09ddSBjoern A. Zeeb 
2046bfcc09ddSBjoern A. Zeeb 	/* This "Tx" DMA channel is used only for loading uCode */
2047bfcc09ddSBjoern A. Zeeb 	if (inta & CSR_INT_BIT_FH_TX) {
2048bfcc09ddSBjoern A. Zeeb 		iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
2049bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
2050bfcc09ddSBjoern A. Zeeb 		isr_stats->tx++;
2051bfcc09ddSBjoern A. Zeeb 		handled |= CSR_INT_BIT_FH_TX;
2052bfcc09ddSBjoern A. Zeeb 		/* Wake up uCode load routine, now that load is complete */
2053bfcc09ddSBjoern A. Zeeb 		trans_pcie->ucode_write_complete = true;
2054bfcc09ddSBjoern A. Zeeb 		wake_up(&trans_pcie->ucode_write_waitq);
2055d9836fb4SBjoern A. Zeeb 		/* Wake up IMR write routine, now that write to SRAM is complete */
2056d9836fb4SBjoern A. Zeeb 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2057d9836fb4SBjoern A. Zeeb 			trans_pcie->imr_status = IMR_D2S_COMPLETED;
2058d9836fb4SBjoern A. Zeeb 			wake_up(&trans_pcie->ucode_write_waitq);
2059d9836fb4SBjoern A. Zeeb 		}
2060bfcc09ddSBjoern A. Zeeb 	}
2061bfcc09ddSBjoern A. Zeeb 
2062bfcc09ddSBjoern A. Zeeb 	if (inta & ~handled) {
2063bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
2064bfcc09ddSBjoern A. Zeeb 		isr_stats->unhandled++;
2065bfcc09ddSBjoern A. Zeeb 	}
2066bfcc09ddSBjoern A. Zeeb 
2067bfcc09ddSBjoern A. Zeeb 	if (inta & ~(trans_pcie->inta_mask)) {
2068bfcc09ddSBjoern A. Zeeb 		IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
2069bfcc09ddSBjoern A. Zeeb 			 inta & ~trans_pcie->inta_mask);
2070bfcc09ddSBjoern A. Zeeb 	}
2071bfcc09ddSBjoern A. Zeeb 
2072bfcc09ddSBjoern A. Zeeb 	if (!polling) {
2073bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&trans_pcie->irq_lock);
2074bfcc09ddSBjoern A. Zeeb 		/* only Re-enable all interrupt if disabled by irq */
2075bfcc09ddSBjoern A. Zeeb 		if (test_bit(STATUS_INT_ENABLED, &trans->status))
2076bfcc09ddSBjoern A. Zeeb 			_iwl_enable_interrupts(trans);
2077bfcc09ddSBjoern A. Zeeb 		/* we are loading the firmware, enable FH_TX interrupt only */
2078bfcc09ddSBjoern A. Zeeb 		else if (handled & CSR_INT_BIT_FH_TX)
2079bfcc09ddSBjoern A. Zeeb 			iwl_enable_fw_load_int(trans);
2080bfcc09ddSBjoern A. Zeeb 		/* Re-enable RF_KILL if it occurred */
2081bfcc09ddSBjoern A. Zeeb 		else if (handled & CSR_INT_BIT_RF_KILL)
2082bfcc09ddSBjoern A. Zeeb 			iwl_enable_rfkill_int(trans);
2083bfcc09ddSBjoern A. Zeeb 		/* Re-enable the ALIVE / Rx interrupt if it occurred */
2084bfcc09ddSBjoern A. Zeeb 		else if (handled & (CSR_INT_BIT_ALIVE | CSR_INT_BIT_FH_RX))
2085bfcc09ddSBjoern A. Zeeb 			iwl_enable_fw_load_int_ctx_info(trans);
2086bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&trans_pcie->irq_lock);
2087bfcc09ddSBjoern A. Zeeb 	}
2088bfcc09ddSBjoern A. Zeeb 
2089bfcc09ddSBjoern A. Zeeb out:
2090bfcc09ddSBjoern A. Zeeb 	lock_map_release(&trans->sync_cmd_lockdep_map);
2091bfcc09ddSBjoern A. Zeeb 	return IRQ_HANDLED;
2092bfcc09ddSBjoern A. Zeeb }
2093bfcc09ddSBjoern A. Zeeb 
2094bfcc09ddSBjoern A. Zeeb /******************************************************************************
2095bfcc09ddSBjoern A. Zeeb  *
2096bfcc09ddSBjoern A. Zeeb  * ICT functions
2097bfcc09ddSBjoern A. Zeeb  *
2098bfcc09ddSBjoern A. Zeeb  ******************************************************************************/
2099bfcc09ddSBjoern A. Zeeb 
2100bfcc09ddSBjoern A. Zeeb /* Free dram table */
2101bfcc09ddSBjoern A. Zeeb void iwl_pcie_free_ict(struct iwl_trans *trans)
2102bfcc09ddSBjoern A. Zeeb {
2103bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2104bfcc09ddSBjoern A. Zeeb 
2105bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->ict_tbl) {
2106bfcc09ddSBjoern A. Zeeb 		dma_free_coherent(trans->dev, ICT_SIZE,
2107bfcc09ddSBjoern A. Zeeb 				  trans_pcie->ict_tbl,
2108bfcc09ddSBjoern A. Zeeb 				  trans_pcie->ict_tbl_dma);
2109bfcc09ddSBjoern A. Zeeb 		trans_pcie->ict_tbl = NULL;
2110bfcc09ddSBjoern A. Zeeb 		trans_pcie->ict_tbl_dma = 0;
2111bfcc09ddSBjoern A. Zeeb 	}
2112bfcc09ddSBjoern A. Zeeb }
2113bfcc09ddSBjoern A. Zeeb 
2114bfcc09ddSBjoern A. Zeeb /*
2115bfcc09ddSBjoern A. Zeeb  * allocate dram shared table, it is an aligned memory
2116bfcc09ddSBjoern A. Zeeb  * block of ICT_SIZE.
2117bfcc09ddSBjoern A. Zeeb  * also reset all data related to ICT table interrupt.
2118bfcc09ddSBjoern A. Zeeb  */
2119bfcc09ddSBjoern A. Zeeb int iwl_pcie_alloc_ict(struct iwl_trans *trans)
2120bfcc09ddSBjoern A. Zeeb {
2121bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2122bfcc09ddSBjoern A. Zeeb 
2123bfcc09ddSBjoern A. Zeeb 	trans_pcie->ict_tbl =
2124bfcc09ddSBjoern A. Zeeb 		dma_alloc_coherent(trans->dev, ICT_SIZE,
2125bfcc09ddSBjoern A. Zeeb 				   &trans_pcie->ict_tbl_dma, GFP_KERNEL);
2126bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->ict_tbl)
2127bfcc09ddSBjoern A. Zeeb 		return -ENOMEM;
2128bfcc09ddSBjoern A. Zeeb 
2129bfcc09ddSBjoern A. Zeeb 	/* just an API sanity check ... it is guaranteed to be aligned */
2130bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
2131bfcc09ddSBjoern A. Zeeb 		iwl_pcie_free_ict(trans);
2132bfcc09ddSBjoern A. Zeeb 		return -EINVAL;
2133bfcc09ddSBjoern A. Zeeb 	}
2134bfcc09ddSBjoern A. Zeeb 
2135bfcc09ddSBjoern A. Zeeb 	return 0;
2136bfcc09ddSBjoern A. Zeeb }
2137bfcc09ddSBjoern A. Zeeb 
2138bfcc09ddSBjoern A. Zeeb /* Device is going up inform it about using ICT interrupt table,
2139bfcc09ddSBjoern A. Zeeb  * also we need to tell the driver to start using ICT interrupt.
2140bfcc09ddSBjoern A. Zeeb  */
2141bfcc09ddSBjoern A. Zeeb void iwl_pcie_reset_ict(struct iwl_trans *trans)
2142bfcc09ddSBjoern A. Zeeb {
2143bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2144bfcc09ddSBjoern A. Zeeb 	u32 val;
2145bfcc09ddSBjoern A. Zeeb 
2146bfcc09ddSBjoern A. Zeeb 	if (!trans_pcie->ict_tbl)
2147bfcc09ddSBjoern A. Zeeb 		return;
2148bfcc09ddSBjoern A. Zeeb 
2149bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&trans_pcie->irq_lock);
2150bfcc09ddSBjoern A. Zeeb 	_iwl_disable_interrupts(trans);
2151bfcc09ddSBjoern A. Zeeb 
2152bfcc09ddSBjoern A. Zeeb 	memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
2153bfcc09ddSBjoern A. Zeeb 
2154bfcc09ddSBjoern A. Zeeb 	val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
2155bfcc09ddSBjoern A. Zeeb 
2156bfcc09ddSBjoern A. Zeeb 	val |= CSR_DRAM_INT_TBL_ENABLE |
2157bfcc09ddSBjoern A. Zeeb 	       CSR_DRAM_INIT_TBL_WRAP_CHECK |
2158bfcc09ddSBjoern A. Zeeb 	       CSR_DRAM_INIT_TBL_WRITE_POINTER;
2159bfcc09ddSBjoern A. Zeeb 
2160bfcc09ddSBjoern A. Zeeb 	IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
2161bfcc09ddSBjoern A. Zeeb 
2162bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
2163bfcc09ddSBjoern A. Zeeb 	trans_pcie->use_ict = true;
2164bfcc09ddSBjoern A. Zeeb 	trans_pcie->ict_index = 0;
2165bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
2166bfcc09ddSBjoern A. Zeeb 	_iwl_enable_interrupts(trans);
2167bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&trans_pcie->irq_lock);
2168bfcc09ddSBjoern A. Zeeb }
2169bfcc09ddSBjoern A. Zeeb 
2170bfcc09ddSBjoern A. Zeeb /* Device is going down disable ict interrupt usage */
2171bfcc09ddSBjoern A. Zeeb void iwl_pcie_disable_ict(struct iwl_trans *trans)
2172bfcc09ddSBjoern A. Zeeb {
2173bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2174bfcc09ddSBjoern A. Zeeb 
2175bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&trans_pcie->irq_lock);
2176bfcc09ddSBjoern A. Zeeb 	trans_pcie->use_ict = false;
2177bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&trans_pcie->irq_lock);
2178bfcc09ddSBjoern A. Zeeb }
2179bfcc09ddSBjoern A. Zeeb 
2180bfcc09ddSBjoern A. Zeeb irqreturn_t iwl_pcie_isr(int irq, void *data)
2181bfcc09ddSBjoern A. Zeeb {
2182bfcc09ddSBjoern A. Zeeb 	struct iwl_trans *trans = data;
2183bfcc09ddSBjoern A. Zeeb 
2184bfcc09ddSBjoern A. Zeeb 	if (!trans)
2185bfcc09ddSBjoern A. Zeeb 		return IRQ_NONE;
2186bfcc09ddSBjoern A. Zeeb 
2187bfcc09ddSBjoern A. Zeeb 	/* Disable (but don't clear!) interrupts here to avoid
2188bfcc09ddSBjoern A. Zeeb 	 * back-to-back ISRs and sporadic interrupts from our NIC.
2189bfcc09ddSBjoern A. Zeeb 	 * If we have something to service, the tasklet will re-enable ints.
2190bfcc09ddSBjoern A. Zeeb 	 * If we *don't* have something, we'll re-enable before leaving here.
2191bfcc09ddSBjoern A. Zeeb 	 */
2192bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_INT_MASK, 0x00000000);
2193bfcc09ddSBjoern A. Zeeb 
2194bfcc09ddSBjoern A. Zeeb 	return IRQ_WAKE_THREAD;
2195bfcc09ddSBjoern A. Zeeb }
2196bfcc09ddSBjoern A. Zeeb 
2197bfcc09ddSBjoern A. Zeeb irqreturn_t iwl_pcie_msix_isr(int irq, void *data)
2198bfcc09ddSBjoern A. Zeeb {
2199bfcc09ddSBjoern A. Zeeb 	return IRQ_WAKE_THREAD;
2200bfcc09ddSBjoern A. Zeeb }
2201bfcc09ddSBjoern A. Zeeb 
2202bfcc09ddSBjoern A. Zeeb irqreturn_t iwl_pcie_irq_msix_handler(int irq, void *dev_id)
2203bfcc09ddSBjoern A. Zeeb {
2204bfcc09ddSBjoern A. Zeeb 	struct msix_entry *entry = dev_id;
2205bfcc09ddSBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = iwl_pcie_get_trans_pcie(entry);
2206bfcc09ddSBjoern A. Zeeb 	struct iwl_trans *trans = trans_pcie->trans;
2207bfcc09ddSBjoern A. Zeeb 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
2208bfcc09ddSBjoern A. Zeeb 	u32 inta_fh_msk = ~MSIX_FH_INT_CAUSES_DATA_QUEUE;
2209bfcc09ddSBjoern A. Zeeb 	u32 inta_fh, inta_hw;
2210bfcc09ddSBjoern A. Zeeb 	bool polling = false;
2211bfcc09ddSBjoern A. Zeeb 	bool sw_err;
2212bfcc09ddSBjoern A. Zeeb 
2213bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX)
2214bfcc09ddSBjoern A. Zeeb 		inta_fh_msk |= MSIX_FH_INT_CAUSES_Q0;
2215bfcc09ddSBjoern A. Zeeb 
2216bfcc09ddSBjoern A. Zeeb 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS)
2217bfcc09ddSBjoern A. Zeeb 		inta_fh_msk |= MSIX_FH_INT_CAUSES_Q1;
2218bfcc09ddSBjoern A. Zeeb 
2219bfcc09ddSBjoern A. Zeeb 	lock_map_acquire(&trans->sync_cmd_lockdep_map);
2220bfcc09ddSBjoern A. Zeeb 
2221bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&trans_pcie->irq_lock);
2222bfcc09ddSBjoern A. Zeeb 	inta_fh = iwl_read32(trans, CSR_MSIX_FH_INT_CAUSES_AD);
2223bfcc09ddSBjoern A. Zeeb 	inta_hw = iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD);
2224bfcc09ddSBjoern A. Zeeb 	/*
2225bfcc09ddSBjoern A. Zeeb 	 * Clear causes registers to avoid being handling the same cause.
2226bfcc09ddSBjoern A. Zeeb 	 */
2227bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_MSIX_FH_INT_CAUSES_AD, inta_fh & inta_fh_msk);
2228bfcc09ddSBjoern A. Zeeb 	iwl_write32(trans, CSR_MSIX_HW_INT_CAUSES_AD, inta_hw);
2229bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&trans_pcie->irq_lock);
2230bfcc09ddSBjoern A. Zeeb 
2231bfcc09ddSBjoern A. Zeeb 	trace_iwlwifi_dev_irq_msix(trans->dev, entry, true, inta_fh, inta_hw);
2232bfcc09ddSBjoern A. Zeeb 
2233bfcc09ddSBjoern A. Zeeb 	if (unlikely(!(inta_fh | inta_hw))) {
2234bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
2235bfcc09ddSBjoern A. Zeeb 		lock_map_release(&trans->sync_cmd_lockdep_map);
2236bfcc09ddSBjoern A. Zeeb 		return IRQ_NONE;
2237bfcc09ddSBjoern A. Zeeb 	}
2238bfcc09ddSBjoern A. Zeeb 
2239bfcc09ddSBjoern A. Zeeb #ifdef CONFIG_IWLWIFI_DEBUG
2240bfcc09ddSBjoern A. Zeeb 	if (iwl_have_debug_level(IWL_DL_ISR)) {
2241bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans,
2242bfcc09ddSBjoern A. Zeeb 			      "ISR[%d] inta_fh 0x%08x, enabled (sw) 0x%08x (hw) 0x%08x\n",
2243bfcc09ddSBjoern A. Zeeb 			      entry->entry, inta_fh, trans_pcie->fh_mask,
2244bfcc09ddSBjoern A. Zeeb 			      iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD));
2245bfcc09ddSBjoern A. Zeeb 		if (inta_fh & ~trans_pcie->fh_mask)
2246bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_ISR(trans,
2247bfcc09ddSBjoern A. Zeeb 				      "We got a masked interrupt (0x%08x)\n",
2248bfcc09ddSBjoern A. Zeeb 				      inta_fh & ~trans_pcie->fh_mask);
2249bfcc09ddSBjoern A. Zeeb 	}
2250bfcc09ddSBjoern A. Zeeb #endif
2251bfcc09ddSBjoern A. Zeeb 
2252bfcc09ddSBjoern A. Zeeb 	inta_fh &= trans_pcie->fh_mask;
2253bfcc09ddSBjoern A. Zeeb 
2254bfcc09ddSBjoern A. Zeeb 	if ((trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX) &&
2255bfcc09ddSBjoern A. Zeeb 	    inta_fh & MSIX_FH_INT_CAUSES_Q0) {
2256bfcc09ddSBjoern A. Zeeb 		local_bh_disable();
2257bfcc09ddSBjoern A. Zeeb 		if (napi_schedule_prep(&trans_pcie->rxq[0].napi)) {
2258bfcc09ddSBjoern A. Zeeb 			polling = true;
2259bfcc09ddSBjoern A. Zeeb 			__napi_schedule(&trans_pcie->rxq[0].napi);
2260bfcc09ddSBjoern A. Zeeb 		}
2261bfcc09ddSBjoern A. Zeeb 		local_bh_enable();
2262bfcc09ddSBjoern A. Zeeb 	}
2263bfcc09ddSBjoern A. Zeeb 
2264bfcc09ddSBjoern A. Zeeb 	if ((trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS) &&
2265bfcc09ddSBjoern A. Zeeb 	    inta_fh & MSIX_FH_INT_CAUSES_Q1) {
2266bfcc09ddSBjoern A. Zeeb 		local_bh_disable();
2267bfcc09ddSBjoern A. Zeeb 		if (napi_schedule_prep(&trans_pcie->rxq[1].napi)) {
2268bfcc09ddSBjoern A. Zeeb 			polling = true;
2269bfcc09ddSBjoern A. Zeeb 			__napi_schedule(&trans_pcie->rxq[1].napi);
2270bfcc09ddSBjoern A. Zeeb 		}
2271bfcc09ddSBjoern A. Zeeb 		local_bh_enable();
2272bfcc09ddSBjoern A. Zeeb 	}
2273bfcc09ddSBjoern A. Zeeb 
2274bfcc09ddSBjoern A. Zeeb 	/* This "Tx" DMA channel is used only for loading uCode */
2275d9836fb4SBjoern A. Zeeb 	if (inta_fh & MSIX_FH_INT_CAUSES_D2S_CH0_NUM &&
2276d9836fb4SBjoern A. Zeeb 	    trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2277d9836fb4SBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "IMR Complete interrupt\n");
2278d9836fb4SBjoern A. Zeeb 		isr_stats->tx++;
2279d9836fb4SBjoern A. Zeeb 
2280d9836fb4SBjoern A. Zeeb 		/* Wake up IMR routine once write to SRAM is complete */
2281d9836fb4SBjoern A. Zeeb 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2282d9836fb4SBjoern A. Zeeb 			trans_pcie->imr_status = IMR_D2S_COMPLETED;
2283d9836fb4SBjoern A. Zeeb 			wake_up(&trans_pcie->ucode_write_waitq);
2284d9836fb4SBjoern A. Zeeb 		}
2285d9836fb4SBjoern A. Zeeb 	} else if (inta_fh & MSIX_FH_INT_CAUSES_D2S_CH0_NUM) {
2286bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
2287bfcc09ddSBjoern A. Zeeb 		isr_stats->tx++;
2288bfcc09ddSBjoern A. Zeeb 		/*
2289bfcc09ddSBjoern A. Zeeb 		 * Wake up uCode load routine,
2290bfcc09ddSBjoern A. Zeeb 		 * now that load is complete
2291bfcc09ddSBjoern A. Zeeb 		 */
2292bfcc09ddSBjoern A. Zeeb 		trans_pcie->ucode_write_complete = true;
2293bfcc09ddSBjoern A. Zeeb 		wake_up(&trans_pcie->ucode_write_waitq);
2294d9836fb4SBjoern A. Zeeb 
2295d9836fb4SBjoern A. Zeeb 		/* Wake up IMR routine once write to SRAM is complete */
2296d9836fb4SBjoern A. Zeeb 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2297d9836fb4SBjoern A. Zeeb 			trans_pcie->imr_status = IMR_D2S_COMPLETED;
2298d9836fb4SBjoern A. Zeeb 			wake_up(&trans_pcie->ucode_write_waitq);
2299d9836fb4SBjoern A. Zeeb 		}
2300bfcc09ddSBjoern A. Zeeb 	}
2301bfcc09ddSBjoern A. Zeeb 
2302bfcc09ddSBjoern A. Zeeb 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
2303bfcc09ddSBjoern A. Zeeb 		sw_err = inta_hw & MSIX_HW_INT_CAUSES_REG_SW_ERR_BZ;
2304bfcc09ddSBjoern A. Zeeb 	else
2305bfcc09ddSBjoern A. Zeeb 		sw_err = inta_hw & MSIX_HW_INT_CAUSES_REG_SW_ERR;
2306bfcc09ddSBjoern A. Zeeb 
2307*a4128aadSBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_TOP_FATAL_ERR) {
2308*a4128aadSBjoern A. Zeeb 		IWL_ERR(trans, "TOP Fatal error detected, inta_hw=0x%x.\n",
2309*a4128aadSBjoern A. Zeeb 			inta_hw);
2310*a4128aadSBjoern A. Zeeb 		/* TODO: PLDR flow required here for >= Bz */
2311*a4128aadSBjoern A. Zeeb 	}
2312*a4128aadSBjoern A. Zeeb 
2313bfcc09ddSBjoern A. Zeeb 	/* Error detected by uCode */
2314bfcc09ddSBjoern A. Zeeb 	if ((inta_fh & MSIX_FH_INT_CAUSES_FH_ERR) || sw_err) {
2315bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans,
2316bfcc09ddSBjoern A. Zeeb 			"Microcode SW error detected. Restarting 0x%X.\n",
2317bfcc09ddSBjoern A. Zeeb 			inta_fh);
2318bfcc09ddSBjoern A. Zeeb 		isr_stats->sw++;
2319bfcc09ddSBjoern A. Zeeb 		/* during FW reset flow report errors from there */
2320d9836fb4SBjoern A. Zeeb 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2321d9836fb4SBjoern A. Zeeb 			trans_pcie->imr_status = IMR_D2S_ERROR;
2322d9836fb4SBjoern A. Zeeb 			wake_up(&trans_pcie->imr_waitq);
2323d9836fb4SBjoern A. Zeeb 		} else if (trans_pcie->fw_reset_state == FW_RESET_REQUESTED) {
2324bfcc09ddSBjoern A. Zeeb 			trans_pcie->fw_reset_state = FW_RESET_ERROR;
2325bfcc09ddSBjoern A. Zeeb 			wake_up(&trans_pcie->fw_reset_waitq);
2326bfcc09ddSBjoern A. Zeeb 		} else {
2327bfcc09ddSBjoern A. Zeeb 			iwl_pcie_irq_handle_error(trans);
2328bfcc09ddSBjoern A. Zeeb 		}
2329bfcc09ddSBjoern A. Zeeb 	}
2330bfcc09ddSBjoern A. Zeeb 
2331bfcc09ddSBjoern A. Zeeb 	/* After checking FH register check HW register */
2332bfcc09ddSBjoern A. Zeeb #ifdef CONFIG_IWLWIFI_DEBUG
2333bfcc09ddSBjoern A. Zeeb 	if (iwl_have_debug_level(IWL_DL_ISR)) {
2334bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans,
2335bfcc09ddSBjoern A. Zeeb 			      "ISR[%d] inta_hw 0x%08x, enabled (sw) 0x%08x (hw) 0x%08x\n",
2336bfcc09ddSBjoern A. Zeeb 			      entry->entry, inta_hw, trans_pcie->hw_mask,
2337bfcc09ddSBjoern A. Zeeb 			      iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD));
2338bfcc09ddSBjoern A. Zeeb 		if (inta_hw & ~trans_pcie->hw_mask)
2339bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_ISR(trans,
2340bfcc09ddSBjoern A. Zeeb 				      "We got a masked interrupt 0x%08x\n",
2341bfcc09ddSBjoern A. Zeeb 				      inta_hw & ~trans_pcie->hw_mask);
2342bfcc09ddSBjoern A. Zeeb 	}
2343bfcc09ddSBjoern A. Zeeb #endif
2344bfcc09ddSBjoern A. Zeeb 
2345bfcc09ddSBjoern A. Zeeb 	inta_hw &= trans_pcie->hw_mask;
2346bfcc09ddSBjoern A. Zeeb 
2347bfcc09ddSBjoern A. Zeeb 	/* Alive notification via Rx interrupt will do the real work */
2348bfcc09ddSBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_ALIVE) {
2349bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Alive interrupt\n");
2350bfcc09ddSBjoern A. Zeeb 		isr_stats->alive++;
2351bfcc09ddSBjoern A. Zeeb 		if (trans->trans_cfg->gen2) {
2352bfcc09ddSBjoern A. Zeeb 			/* We can restock, since firmware configured the RFH */
2353bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rxmq_restock(trans, trans_pcie->rxq);
2354bfcc09ddSBjoern A. Zeeb 		}
2355bfcc09ddSBjoern A. Zeeb 	}
2356bfcc09ddSBjoern A. Zeeb 
2357d9836fb4SBjoern A. Zeeb 	/*
2358d9836fb4SBjoern A. Zeeb 	 * In some rare cases when the HW is in a bad state, we may
2359d9836fb4SBjoern A. Zeeb 	 * get this interrupt too early, when prph_info is still NULL.
2360d9836fb4SBjoern A. Zeeb 	 * So make sure that it's not NULL to prevent crashing.
2361d9836fb4SBjoern A. Zeeb 	 */
2362d9836fb4SBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_WAKEUP && trans_pcie->prph_info) {
2363bfcc09ddSBjoern A. Zeeb 		u32 sleep_notif =
2364bfcc09ddSBjoern A. Zeeb 			le32_to_cpu(trans_pcie->prph_info->sleep_notif);
2365bfcc09ddSBjoern A. Zeeb 		if (sleep_notif == IWL_D3_SLEEP_STATUS_SUSPEND ||
2366bfcc09ddSBjoern A. Zeeb 		    sleep_notif == IWL_D3_SLEEP_STATUS_RESUME) {
2367bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_ISR(trans,
2368bfcc09ddSBjoern A. Zeeb 				      "Sx interrupt: sleep notification = 0x%x\n",
2369bfcc09ddSBjoern A. Zeeb 				      sleep_notif);
2370bfcc09ddSBjoern A. Zeeb 			trans_pcie->sx_complete = true;
2371bfcc09ddSBjoern A. Zeeb 			wake_up(&trans_pcie->sx_waitq);
2372bfcc09ddSBjoern A. Zeeb 		} else {
2373bfcc09ddSBjoern A. Zeeb 			/* uCode wakes up after power-down sleep */
2374bfcc09ddSBjoern A. Zeeb 			IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
2375bfcc09ddSBjoern A. Zeeb 			iwl_pcie_rxq_check_wrptr(trans);
2376bfcc09ddSBjoern A. Zeeb 			iwl_pcie_txq_check_wrptrs(trans);
2377bfcc09ddSBjoern A. Zeeb 
2378bfcc09ddSBjoern A. Zeeb 			isr_stats->wakeup++;
2379bfcc09ddSBjoern A. Zeeb 		}
2380bfcc09ddSBjoern A. Zeeb 	}
2381bfcc09ddSBjoern A. Zeeb 
2382bfcc09ddSBjoern A. Zeeb 	/* Chip got too hot and stopped itself */
2383bfcc09ddSBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_CT_KILL) {
2384bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans, "Microcode CT kill error detected.\n");
2385bfcc09ddSBjoern A. Zeeb 		isr_stats->ctkill++;
2386bfcc09ddSBjoern A. Zeeb 	}
2387bfcc09ddSBjoern A. Zeeb 
2388bfcc09ddSBjoern A. Zeeb 	/* HW RF KILL switch toggled */
2389bfcc09ddSBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_RF_KILL)
2390*a4128aadSBjoern A. Zeeb 		iwl_pcie_handle_rfkill_irq(trans, true);
2391bfcc09ddSBjoern A. Zeeb 
2392bfcc09ddSBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_HW_ERR) {
2393bfcc09ddSBjoern A. Zeeb 		IWL_ERR(trans,
2394bfcc09ddSBjoern A. Zeeb 			"Hardware error detected. Restarting.\n");
2395bfcc09ddSBjoern A. Zeeb 
2396bfcc09ddSBjoern A. Zeeb 		isr_stats->hw++;
2397bfcc09ddSBjoern A. Zeeb 		trans->dbg.hw_error = true;
2398bfcc09ddSBjoern A. Zeeb 		iwl_pcie_irq_handle_error(trans);
2399bfcc09ddSBjoern A. Zeeb 	}
2400bfcc09ddSBjoern A. Zeeb 
2401bfcc09ddSBjoern A. Zeeb 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_RESET_DONE) {
2402bfcc09ddSBjoern A. Zeeb 		IWL_DEBUG_ISR(trans, "Reset flow completed\n");
2403bfcc09ddSBjoern A. Zeeb 		trans_pcie->fw_reset_state = FW_RESET_OK;
2404bfcc09ddSBjoern A. Zeeb 		wake_up(&trans_pcie->fw_reset_waitq);
2405bfcc09ddSBjoern A. Zeeb 	}
2406bfcc09ddSBjoern A. Zeeb 
2407bfcc09ddSBjoern A. Zeeb 	if (!polling)
2408bfcc09ddSBjoern A. Zeeb 		iwl_pcie_clear_irq(trans, entry->entry);
2409bfcc09ddSBjoern A. Zeeb 
2410bfcc09ddSBjoern A. Zeeb 	lock_map_release(&trans->sync_cmd_lockdep_map);
2411bfcc09ddSBjoern A. Zeeb 
2412bfcc09ddSBjoern A. Zeeb 	return IRQ_HANDLED;
2413bfcc09ddSBjoern A. Zeeb }
2414