18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
35af2bb31SGregory Greenman  * Copyright (C) 2003-2014, 2018-2023 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/sched.h>
8e705c121SKalle Valo #include <linux/wait.h>
9e705c121SKalle Valo #include <linux/gfp.h>
10e705c121SKalle Valo 
11e705c121SKalle Valo #include "iwl-prph.h"
12e705c121SKalle Valo #include "iwl-io.h"
13e705c121SKalle Valo #include "internal.h"
14e705c121SKalle Valo #include "iwl-op-mode.h"
159b58419eSGolan Ben Ami #include "iwl-context-info-gen3.h"
16e705c121SKalle Valo 
17e705c121SKalle Valo /******************************************************************************
18e705c121SKalle Valo  *
19e705c121SKalle Valo  * RX path functions
20e705c121SKalle Valo  *
21e705c121SKalle Valo  ******************************************************************************/
22e705c121SKalle Valo 
23e705c121SKalle Valo /*
24e705c121SKalle Valo  * Rx theory of operation
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
27e705c121SKalle Valo  * each of which point to Receive Buffers to be filled by the NIC.  These get
28e705c121SKalle Valo  * used not only for Rx frames, but for any command response or notification
29e705c121SKalle Valo  * from the NIC.  The driver and NIC manage the Rx buffers by means
30e705c121SKalle Valo  * of indexes into the circular buffer.
31e705c121SKalle Valo  *
32e705c121SKalle Valo  * Rx Queue Indexes
33e705c121SKalle Valo  * The host/firmware share two index registers for managing the Rx buffers.
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * The READ index maps to the first position that the firmware may be writing
36e705c121SKalle Valo  * to -- the driver can read up to (but not including) this position and get
37e705c121SKalle Valo  * good data.
38e705c121SKalle Valo  * The READ index is managed by the firmware once the card is enabled.
39e705c121SKalle Valo  *
40e705c121SKalle Valo  * The WRITE index maps to the last position the driver has read from -- the
41e705c121SKalle Valo  * position preceding WRITE is the last slot the firmware can place a packet.
42e705c121SKalle Valo  *
43e705c121SKalle Valo  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
44e705c121SKalle Valo  * WRITE = READ.
45e705c121SKalle Valo  *
46e705c121SKalle Valo  * During initialization, the host sets up the READ queue position to the first
47e705c121SKalle Valo  * INDEX position, and WRITE to the last (READ - 1 wrapped)
48e705c121SKalle Valo  *
49e705c121SKalle Valo  * When the firmware places a packet in a buffer, it will advance the READ index
50e705c121SKalle Valo  * and fire the RX interrupt.  The driver can then query the READ index and
51e705c121SKalle Valo  * process as many packets as possible, moving the WRITE index forward as it
52e705c121SKalle Valo  * resets the Rx queue buffers with new memory.
53e705c121SKalle Valo  *
54e705c121SKalle Valo  * The management in the driver is as follows:
55e705c121SKalle Valo  * + A list of pre-allocated RBDs is stored in iwl->rxq->rx_free.
56e705c121SKalle Valo  *   When the interrupt handler is called, the request is processed.
57e705c121SKalle Valo  *   The page is either stolen - transferred to the upper layer
58e705c121SKalle Valo  *   or reused - added immediately to the iwl->rxq->rx_free list.
59e705c121SKalle Valo  * + When the page is stolen - the driver updates the matching queue's used
60e705c121SKalle Valo  *   count, detaches the RBD and transfers it to the queue used list.
61e705c121SKalle Valo  *   When there are two used RBDs - they are transferred to the allocator empty
62e705c121SKalle Valo  *   list. Work is then scheduled for the allocator to start allocating
63e705c121SKalle Valo  *   eight buffers.
64e705c121SKalle Valo  *   When there are another 6 used RBDs - they are transferred to the allocator
65e705c121SKalle Valo  *   empty list and the driver tries to claim the pre-allocated buffers and
66e705c121SKalle Valo  *   add them to iwl->rxq->rx_free. If it fails - it continues to claim them
67e705c121SKalle Valo  *   until ready.
68e705c121SKalle Valo  *   When there are 8+ buffers in the free list - either from allocation or from
69e705c121SKalle Valo  *   8 reused unstolen pages - restock is called to update the FW and indexes.
70e705c121SKalle Valo  * + In order to make sure the allocator always has RBDs to use for allocation
71e705c121SKalle Valo  *   the allocator has initial pool in the size of num_queues*(8-2) - the
72e705c121SKalle Valo  *   maximum missing RBDs per allocation request (request posted with 2
73e705c121SKalle Valo  *    empty RBDs, there is no guarantee when the other 6 RBDs are supplied).
74e705c121SKalle Valo  *   The queues supplies the recycle of the rest of the RBDs.
75e705c121SKalle Valo  * + A received packet is processed and handed to the kernel network stack,
76e705c121SKalle Valo  *   detached from the iwl->rxq.  The driver 'processed' index is updated.
77e705c121SKalle Valo  * + If there are no allocated buffers in iwl->rxq->rx_free,
78e705c121SKalle Valo  *   the READ INDEX is not incremented and iwl->status(RX_STALLED) is set.
79e705c121SKalle Valo  *   If there were enough free buffers and RX_STALLED is set it is cleared.
80e705c121SKalle Valo  *
81e705c121SKalle Valo  *
82e705c121SKalle Valo  * Driver sequence:
83e705c121SKalle Valo  *
84e705c121SKalle Valo  * iwl_rxq_alloc()            Allocates rx_free
85e705c121SKalle Valo  * iwl_pcie_rx_replenish()    Replenishes rx_free list from rx_used, and calls
86e705c121SKalle Valo  *                            iwl_pcie_rxq_restock.
87e705c121SKalle Valo  *                            Used only during initialization.
88e705c121SKalle Valo  * iwl_pcie_rxq_restock()     Moves available buffers from rx_free into Rx
89e705c121SKalle Valo  *                            queue, updates firmware pointers, and updates
90e705c121SKalle Valo  *                            the WRITE index.
91e705c121SKalle Valo  * iwl_pcie_rx_allocator()     Background work for allocating pages.
92e705c121SKalle Valo  *
93e705c121SKalle Valo  * -- enable interrupts --
94e705c121SKalle Valo  * ISR - iwl_rx()             Detach iwl_rx_mem_buffers from pool up to the
95e705c121SKalle Valo  *                            READ INDEX, detaching the SKB from the pool.
96e705c121SKalle Valo  *                            Moves the packet buffer from queue to rx_used.
97e705c121SKalle Valo  *                            Posts and claims requests to the allocator.
98e705c121SKalle Valo  *                            Calls iwl_pcie_rxq_restock to refill any empty
99e705c121SKalle Valo  *                            slots.
100e705c121SKalle Valo  *
101e705c121SKalle Valo  * RBD life-cycle:
102e705c121SKalle Valo  *
103e705c121SKalle Valo  * Init:
104e705c121SKalle Valo  * rxq.pool -> rxq.rx_used -> rxq.rx_free -> rxq.queue
105e705c121SKalle Valo  *
106e705c121SKalle Valo  * Regular Receive interrupt:
107e705c121SKalle Valo  * Page Stolen:
108e705c121SKalle Valo  * rxq.queue -> rxq.rx_used -> allocator.rbd_empty ->
109e705c121SKalle Valo  * allocator.rbd_allocated -> rxq.rx_free -> rxq.queue
110e705c121SKalle Valo  * Page not Stolen:
111e705c121SKalle Valo  * rxq.queue -> rxq.rx_free -> rxq.queue
112e705c121SKalle Valo  * ...
113e705c121SKalle Valo  *
114e705c121SKalle Valo  */
115e705c121SKalle Valo 
116e705c121SKalle Valo /*
117e705c121SKalle Valo  * iwl_rxq_space - Return number of free slots available in queue.
118e705c121SKalle Valo  */
iwl_rxq_space(const struct iwl_rxq * rxq)119e705c121SKalle Valo static int iwl_rxq_space(const struct iwl_rxq *rxq)
120e705c121SKalle Valo {
12196a6497bSSara Sharon 	/* Make sure rx queue size is a power of 2 */
12296a6497bSSara Sharon 	WARN_ON(rxq->queue_size & (rxq->queue_size - 1));
123e705c121SKalle Valo 
124e705c121SKalle Valo 	/*
125e705c121SKalle Valo 	 * There can be up to (RX_QUEUE_SIZE - 1) free slots, to avoid ambiguity
126e705c121SKalle Valo 	 * between empty and completely full queues.
127e705c121SKalle Valo 	 * The following is equivalent to modulo by RX_QUEUE_SIZE and is well
128e705c121SKalle Valo 	 * defined for negative dividends.
129e705c121SKalle Valo 	 */
13096a6497bSSara Sharon 	return (rxq->read - rxq->write - 1) & (rxq->queue_size - 1);
131e705c121SKalle Valo }
132e705c121SKalle Valo 
133e705c121SKalle Valo /*
134e705c121SKalle Valo  * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
135e705c121SKalle Valo  */
iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)136e705c121SKalle Valo static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
137e705c121SKalle Valo {
138e705c121SKalle Valo 	return cpu_to_le32((u32)(dma_addr >> 8));
139e705c121SKalle Valo }
140e705c121SKalle Valo 
141e705c121SKalle Valo /*
142e705c121SKalle Valo  * iwl_pcie_rx_stop - stops the Rx DMA
143e705c121SKalle Valo  */
iwl_pcie_rx_stop(struct iwl_trans * trans)144e705c121SKalle Valo int iwl_pcie_rx_stop(struct iwl_trans *trans)
145e705c121SKalle Valo {
1463681021fSJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
1473681021fSJohannes Berg 		/* TODO: remove this once fw does it */
148ea695b7cSShaul Triebitz 		iwl_write_umac_prph(trans, RFH_RXF_DMA_CFG_GEN3, 0);
149ea695b7cSShaul Triebitz 		return iwl_poll_umac_prph_bit(trans, RFH_GEN_STATUS_GEN3,
150d0158235SGolan Ben Ami 					      RXF_DMA_IDLE, RXF_DMA_IDLE, 1000);
151286ca8ebSLuca Coelho 	} else if (trans->trans_cfg->mq_rx_supported) {
152d7fdd0e5SSara Sharon 		iwl_write_prph(trans, RFH_RXF_DMA_CFG, 0);
153d7fdd0e5SSara Sharon 		return iwl_poll_prph_bit(trans, RFH_GEN_STATUS,
154d7fdd0e5SSara Sharon 					   RXF_DMA_IDLE, RXF_DMA_IDLE, 1000);
155d7fdd0e5SSara Sharon 	} else {
156e705c121SKalle Valo 		iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
157e705c121SKalle Valo 		return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
158d7fdd0e5SSara Sharon 					   FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
159d7fdd0e5SSara Sharon 					   1000);
160d7fdd0e5SSara Sharon 	}
161e705c121SKalle Valo }
162e705c121SKalle Valo 
163e705c121SKalle Valo /*
164e705c121SKalle Valo  * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
165e705c121SKalle Valo  */
iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans * trans,struct iwl_rxq * rxq)16678485054SSara Sharon static void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans,
16778485054SSara Sharon 				    struct iwl_rxq *rxq)
168e705c121SKalle Valo {
169e705c121SKalle Valo 	u32 reg;
170e705c121SKalle Valo 
171e705c121SKalle Valo 	lockdep_assert_held(&rxq->lock);
172e705c121SKalle Valo 
173e705c121SKalle Valo 	/*
174e705c121SKalle Valo 	 * explicitly wake up the NIC if:
175e705c121SKalle Valo 	 * 1. shadow registers aren't enabled
176e705c121SKalle Valo 	 * 2. there is a chance that the NIC is asleep
177e705c121SKalle Valo 	 */
178286ca8ebSLuca Coelho 	if (!trans->trans_cfg->base_params->shadow_reg_enable &&
179e705c121SKalle Valo 	    test_bit(STATUS_TPOWER_PMI, &trans->status)) {
180e705c121SKalle Valo 		reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
181e705c121SKalle Valo 
182e705c121SKalle Valo 		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
183e705c121SKalle Valo 			IWL_DEBUG_INFO(trans, "Rx queue requesting wakeup, GP1 = 0x%x\n",
184e705c121SKalle Valo 				       reg);
185e705c121SKalle Valo 			iwl_set_bit(trans, CSR_GP_CNTRL,
1866dece0e9SLuca Coelho 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
187e705c121SKalle Valo 			rxq->need_update = true;
188e705c121SKalle Valo 			return;
189e705c121SKalle Valo 		}
190e705c121SKalle Valo 	}
191e705c121SKalle Valo 
192e705c121SKalle Valo 	rxq->write_actual = round_down(rxq->write, 8);
193fba58d37SMatti Gottlieb 	if (!trans->trans_cfg->mq_rx_supported)
194fba58d37SMatti Gottlieb 		iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, rxq->write_actual);
195fba58d37SMatti Gottlieb 	else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
196fba58d37SMatti Gottlieb 		iwl_write32(trans, HBUS_TARG_WRPTR, rxq->write_actual |
197fba58d37SMatti Gottlieb 			    HBUS_TARG_WRPTR_RX_Q(rxq->id));
198fba58d37SMatti Gottlieb 	else
1991554ed20SSara Sharon 		iwl_write32(trans, RFH_Q_FRBDCB_WIDX_TRG(rxq->id),
20096a6497bSSara Sharon 			    rxq->write_actual);
201e705c121SKalle Valo }
202e705c121SKalle Valo 
iwl_pcie_rxq_check_wrptr(struct iwl_trans * trans)203e705c121SKalle Valo static void iwl_pcie_rxq_check_wrptr(struct iwl_trans *trans)
204e705c121SKalle Valo {
205e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
20678485054SSara Sharon 	int i;
207e705c121SKalle Valo 
20878485054SSara Sharon 	for (i = 0; i < trans->num_rx_queues; i++) {
20978485054SSara Sharon 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
210e705c121SKalle Valo 
211e705c121SKalle Valo 		if (!rxq->need_update)
21278485054SSara Sharon 			continue;
21325edc8f2SJohannes Berg 		spin_lock_bh(&rxq->lock);
21478485054SSara Sharon 		iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
215e705c121SKalle Valo 		rxq->need_update = false;
21625edc8f2SJohannes Berg 		spin_unlock_bh(&rxq->lock);
217e705c121SKalle Valo 	}
21878485054SSara Sharon }
219e705c121SKalle Valo 
iwl_pcie_restock_bd(struct iwl_trans * trans,struct iwl_rxq * rxq,struct iwl_rx_mem_buffer * rxb)2200307c839SGolan Ben Ami static void iwl_pcie_restock_bd(struct iwl_trans *trans,
2210307c839SGolan Ben Ami 				struct iwl_rxq *rxq,
2220307c839SGolan Ben Ami 				struct iwl_rx_mem_buffer *rxb)
2230307c839SGolan Ben Ami {
2243681021fSJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
2250307c839SGolan Ben Ami 		struct iwl_rx_transfer_desc *bd = rxq->bd;
2260307c839SGolan Ben Ami 
227f826faaaSJohannes Berg 		BUILD_BUG_ON(sizeof(*bd) != 2 * sizeof(u64));
228f826faaaSJohannes Berg 
2290307c839SGolan Ben Ami 		bd[rxq->write].addr = cpu_to_le64(rxb->page_dma);
2300307c839SGolan Ben Ami 		bd[rxq->write].rbid = cpu_to_le16(rxb->vid);
2310307c839SGolan Ben Ami 	} else {
2320307c839SGolan Ben Ami 		__le64 *bd = rxq->bd;
2330307c839SGolan Ben Ami 
2340307c839SGolan Ben Ami 		bd[rxq->write] = cpu_to_le64(rxb->page_dma | rxb->vid);
2350307c839SGolan Ben Ami 	}
23685d78bb1SSara Sharon 
23785d78bb1SSara Sharon 	IWL_DEBUG_RX(trans, "Assigned virtual RB ID %u to queue %d index %d\n",
23885d78bb1SSara Sharon 		     (u32)rxb->vid, rxq->id, rxq->write);
2390307c839SGolan Ben Ami }
2400307c839SGolan Ben Ami 
241e0e168dcSGregory Greenman /*
2422047fa54SSara Sharon  * iwl_pcie_rxmq_restock - restock implementation for multi-queue rx
243e0e168dcSGregory Greenman  */
iwl_pcie_rxmq_restock(struct iwl_trans * trans,struct iwl_rxq * rxq)2442047fa54SSara Sharon static void iwl_pcie_rxmq_restock(struct iwl_trans *trans,
24596a6497bSSara Sharon 				  struct iwl_rxq *rxq)
24696a6497bSSara Sharon {
247cfdc20efSJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
24896a6497bSSara Sharon 	struct iwl_rx_mem_buffer *rxb;
24996a6497bSSara Sharon 
25096a6497bSSara Sharon 	/*
25196a6497bSSara Sharon 	 * If the device isn't enabled - no need to try to add buffers...
25296a6497bSSara Sharon 	 * This can happen when we stop the device and still have an interrupt
25396a6497bSSara Sharon 	 * pending. We stop the APM before we sync the interrupts because we
25496a6497bSSara Sharon 	 * have to (see comment there). On the other hand, since the APM is
25596a6497bSSara Sharon 	 * stopped, we cannot access the HW (in particular not prph).
25696a6497bSSara Sharon 	 * So don't try to restock if the APM has been already stopped.
25796a6497bSSara Sharon 	 */
25896a6497bSSara Sharon 	if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
25996a6497bSSara Sharon 		return;
26096a6497bSSara Sharon 
26125edc8f2SJohannes Berg 	spin_lock_bh(&rxq->lock);
26296a6497bSSara Sharon 	while (rxq->free_count) {
26396a6497bSSara Sharon 		/* Get next free Rx buffer, remove from free list */
26496a6497bSSara Sharon 		rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
26596a6497bSSara Sharon 				       list);
26696a6497bSSara Sharon 		list_del(&rxb->list);
267b1753c62SSara Sharon 		rxb->invalid = false;
268cfdc20efSJohannes Berg 		/* some low bits are expected to be unset (depending on hw) */
269cfdc20efSJohannes Berg 		WARN_ON(rxb->page_dma & trans_pcie->supported_dma_mask);
27096a6497bSSara Sharon 		/* Point to Rx buffer via next RBD in circular buffer */
2710307c839SGolan Ben Ami 		iwl_pcie_restock_bd(trans, rxq, rxb);
2725661925aSJohannes Berg 		rxq->write = (rxq->write + 1) & (rxq->queue_size - 1);
27396a6497bSSara Sharon 		rxq->free_count--;
27496a6497bSSara Sharon 	}
27525edc8f2SJohannes Berg 	spin_unlock_bh(&rxq->lock);
27696a6497bSSara Sharon 
27796a6497bSSara Sharon 	/*
27896a6497bSSara Sharon 	 * If we've added more space for the firmware to place data, tell it.
27996a6497bSSara Sharon 	 * Increment device's write pointer in multiples of 8.
28096a6497bSSara Sharon 	 */
28196a6497bSSara Sharon 	if (rxq->write_actual != (rxq->write & ~0x7)) {
28225edc8f2SJohannes Berg 		spin_lock_bh(&rxq->lock);
28396a6497bSSara Sharon 		iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
28425edc8f2SJohannes Berg 		spin_unlock_bh(&rxq->lock);
28596a6497bSSara Sharon 	}
28696a6497bSSara Sharon }
28796a6497bSSara Sharon 
288e705c121SKalle Valo /*
2892047fa54SSara Sharon  * iwl_pcie_rxsq_restock - restock implementation for single queue rx
290e705c121SKalle Valo  */
iwl_pcie_rxsq_restock(struct iwl_trans * trans,struct iwl_rxq * rxq)2912047fa54SSara Sharon static void iwl_pcie_rxsq_restock(struct iwl_trans *trans,
292e0e168dcSGregory Greenman 				  struct iwl_rxq *rxq)
293e705c121SKalle Valo {
294e705c121SKalle Valo 	struct iwl_rx_mem_buffer *rxb;
295e705c121SKalle Valo 
296e705c121SKalle Valo 	/*
297e705c121SKalle Valo 	 * If the device isn't enabled - not need to try to add buffers...
298e705c121SKalle Valo 	 * This can happen when we stop the device and still have an interrupt
299e705c121SKalle Valo 	 * pending. We stop the APM before we sync the interrupts because we
300e705c121SKalle Valo 	 * have to (see comment there). On the other hand, since the APM is
301e705c121SKalle Valo 	 * stopped, we cannot access the HW (in particular not prph).
302e705c121SKalle Valo 	 * So don't try to restock if the APM has been already stopped.
303e705c121SKalle Valo 	 */
304e705c121SKalle Valo 	if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
305e705c121SKalle Valo 		return;
306e705c121SKalle Valo 
30747ef328cSIlan Peer 	spin_lock_bh(&rxq->lock);
308e705c121SKalle Valo 	while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
30996a6497bSSara Sharon 		__le32 *bd = (__le32 *)rxq->bd;
310e705c121SKalle Valo 		/* The overwritten rxb must be a used one */
311e705c121SKalle Valo 		rxb = rxq->queue[rxq->write];
312e705c121SKalle Valo 		BUG_ON(rxb && rxb->page);
313e705c121SKalle Valo 
314e705c121SKalle Valo 		/* Get next free Rx buffer, remove from free list */
315e705c121SKalle Valo 		rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
316e705c121SKalle Valo 				       list);
317e705c121SKalle Valo 		list_del(&rxb->list);
318b1753c62SSara Sharon 		rxb->invalid = false;
319e705c121SKalle Valo 
320e705c121SKalle Valo 		/* Point to Rx buffer via next RBD in circular buffer */
32196a6497bSSara Sharon 		bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
322e705c121SKalle Valo 		rxq->queue[rxq->write] = rxb;
323e705c121SKalle Valo 		rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
324e705c121SKalle Valo 		rxq->free_count--;
325e705c121SKalle Valo 	}
32647ef328cSIlan Peer 	spin_unlock_bh(&rxq->lock);
327e705c121SKalle Valo 
328e705c121SKalle Valo 	/* If we've added more space for the firmware to place data, tell it.
329e705c121SKalle Valo 	 * Increment device's write pointer in multiples of 8. */
330e705c121SKalle Valo 	if (rxq->write_actual != (rxq->write & ~0x7)) {
33147ef328cSIlan Peer 		spin_lock_bh(&rxq->lock);
33278485054SSara Sharon 		iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
33347ef328cSIlan Peer 		spin_unlock_bh(&rxq->lock);
334e705c121SKalle Valo 	}
335e705c121SKalle Valo }
336e705c121SKalle Valo 
337e705c121SKalle Valo /*
338e0e168dcSGregory Greenman  * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
339e0e168dcSGregory Greenman  *
340e0e168dcSGregory Greenman  * If there are slots in the RX queue that need to be restocked,
341e0e168dcSGregory Greenman  * and we have free pre-allocated buffers, fill the ranks as much
342e0e168dcSGregory Greenman  * as we can, pulling from rx_free.
343e0e168dcSGregory Greenman  *
344e0e168dcSGregory Greenman  * This moves the 'write' index forward to catch up with 'processed', and
345e0e168dcSGregory Greenman  * also updates the memory address in the firmware to reference the new
346e0e168dcSGregory Greenman  * target buffer.
347e0e168dcSGregory Greenman  */
348e0e168dcSGregory Greenman static
iwl_pcie_rxq_restock(struct iwl_trans * trans,struct iwl_rxq * rxq)349e0e168dcSGregory Greenman void iwl_pcie_rxq_restock(struct iwl_trans *trans, struct iwl_rxq *rxq)
350e0e168dcSGregory Greenman {
351286ca8ebSLuca Coelho 	if (trans->trans_cfg->mq_rx_supported)
3522047fa54SSara Sharon 		iwl_pcie_rxmq_restock(trans, rxq);
353e0e168dcSGregory Greenman 	else
3542047fa54SSara Sharon 		iwl_pcie_rxsq_restock(trans, rxq);
355e0e168dcSGregory Greenman }
356e0e168dcSGregory Greenman 
357e0e168dcSGregory Greenman /*
358e705c121SKalle Valo  * iwl_pcie_rx_alloc_page - allocates and returns a page.
359e705c121SKalle Valo  *
360e705c121SKalle Valo  */
iwl_pcie_rx_alloc_page(struct iwl_trans * trans,u32 * offset,gfp_t priority)361e705c121SKalle Valo static struct page *iwl_pcie_rx_alloc_page(struct iwl_trans *trans,
362cfdc20efSJohannes Berg 					   u32 *offset, gfp_t priority)
363e705c121SKalle Valo {
364e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
365cfdc20efSJohannes Berg 	unsigned int rbsize = iwl_trans_get_rb_size(trans_pcie->rx_buf_size);
366cfdc20efSJohannes Berg 	unsigned int allocsize = PAGE_SIZE << trans_pcie->rx_page_order;
367e705c121SKalle Valo 	struct page *page;
368e705c121SKalle Valo 	gfp_t gfp_mask = priority;
369e705c121SKalle Valo 
370e705c121SKalle Valo 	if (trans_pcie->rx_page_order > 0)
371e705c121SKalle Valo 		gfp_mask |= __GFP_COMP;
372e705c121SKalle Valo 
373cfdc20efSJohannes Berg 	if (trans_pcie->alloc_page) {
374cfdc20efSJohannes Berg 		spin_lock_bh(&trans_pcie->alloc_page_lock);
375cfdc20efSJohannes Berg 		/* recheck */
376cfdc20efSJohannes Berg 		if (trans_pcie->alloc_page) {
377cfdc20efSJohannes Berg 			*offset = trans_pcie->alloc_page_used;
378cfdc20efSJohannes Berg 			page = trans_pcie->alloc_page;
379cfdc20efSJohannes Berg 			trans_pcie->alloc_page_used += rbsize;
380cfdc20efSJohannes Berg 			if (trans_pcie->alloc_page_used >= allocsize)
381cfdc20efSJohannes Berg 				trans_pcie->alloc_page = NULL;
382cfdc20efSJohannes Berg 			else
383cfdc20efSJohannes Berg 				get_page(page);
384cfdc20efSJohannes Berg 			spin_unlock_bh(&trans_pcie->alloc_page_lock);
385cfdc20efSJohannes Berg 			return page;
386cfdc20efSJohannes Berg 		}
387cfdc20efSJohannes Berg 		spin_unlock_bh(&trans_pcie->alloc_page_lock);
388cfdc20efSJohannes Berg 	}
389cfdc20efSJohannes Berg 
390e705c121SKalle Valo 	/* Alloc a new receive buffer */
391e705c121SKalle Valo 	page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
392e705c121SKalle Valo 	if (!page) {
393e705c121SKalle Valo 		if (net_ratelimit())
394e705c121SKalle Valo 			IWL_DEBUG_INFO(trans, "alloc_pages failed, order: %d\n",
395e705c121SKalle Valo 				       trans_pcie->rx_page_order);
39678485054SSara Sharon 		/*
39778485054SSara Sharon 		 * Issue an error if we don't have enough pre-allocated
39878485054SSara Sharon 		  * buffers.
3991da3823dSLuca Coelho 		 */
40078485054SSara Sharon 		if (!(gfp_mask & __GFP_NOWARN) && net_ratelimit())
401e705c121SKalle Valo 			IWL_CRIT(trans,
40278485054SSara Sharon 				 "Failed to alloc_pages\n");
403e705c121SKalle Valo 		return NULL;
404e705c121SKalle Valo 	}
405cfdc20efSJohannes Berg 
406cfdc20efSJohannes Berg 	if (2 * rbsize <= allocsize) {
407cfdc20efSJohannes Berg 		spin_lock_bh(&trans_pcie->alloc_page_lock);
408cfdc20efSJohannes Berg 		if (!trans_pcie->alloc_page) {
409cfdc20efSJohannes Berg 			get_page(page);
410cfdc20efSJohannes Berg 			trans_pcie->alloc_page = page;
411cfdc20efSJohannes Berg 			trans_pcie->alloc_page_used = rbsize;
412cfdc20efSJohannes Berg 		}
413cfdc20efSJohannes Berg 		spin_unlock_bh(&trans_pcie->alloc_page_lock);
414cfdc20efSJohannes Berg 	}
415cfdc20efSJohannes Berg 
416cfdc20efSJohannes Berg 	*offset = 0;
417e705c121SKalle Valo 	return page;
418e705c121SKalle Valo }
419e705c121SKalle Valo 
420e705c121SKalle Valo /*
421e705c121SKalle Valo  * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
422e705c121SKalle Valo  *
423e705c121SKalle Valo  * A used RBD is an Rx buffer that has been given to the stack. To use it again
424e705c121SKalle Valo  * a page must be allocated and the RBD must point to the page. This function
425e705c121SKalle Valo  * doesn't change the HW pointer but handles the list of pages that is used by
426e705c121SKalle Valo  * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
427e705c121SKalle Valo  * allocated buffers.
428e705c121SKalle Valo  */
iwl_pcie_rxq_alloc_rbs(struct iwl_trans * trans,gfp_t priority,struct iwl_rxq * rxq)429ff932f61SGolan Ben Ami void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,
43078485054SSara Sharon 			    struct iwl_rxq *rxq)
431e705c121SKalle Valo {
432e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
433e705c121SKalle Valo 	struct iwl_rx_mem_buffer *rxb;
434e705c121SKalle Valo 	struct page *page;
435e705c121SKalle Valo 
436e705c121SKalle Valo 	while (1) {
437cfdc20efSJohannes Berg 		unsigned int offset;
438cfdc20efSJohannes Berg 
43947ef328cSIlan Peer 		spin_lock_bh(&rxq->lock);
440e705c121SKalle Valo 		if (list_empty(&rxq->rx_used)) {
44147ef328cSIlan Peer 			spin_unlock_bh(&rxq->lock);
442e705c121SKalle Valo 			return;
443e705c121SKalle Valo 		}
44447ef328cSIlan Peer 		spin_unlock_bh(&rxq->lock);
445e705c121SKalle Valo 
446cfdc20efSJohannes Berg 		page = iwl_pcie_rx_alloc_page(trans, &offset, priority);
447e705c121SKalle Valo 		if (!page)
448e705c121SKalle Valo 			return;
449e705c121SKalle Valo 
45047ef328cSIlan Peer 		spin_lock_bh(&rxq->lock);
451e705c121SKalle Valo 
452e705c121SKalle Valo 		if (list_empty(&rxq->rx_used)) {
45347ef328cSIlan Peer 			spin_unlock_bh(&rxq->lock);
454e705c121SKalle Valo 			__free_pages(page, trans_pcie->rx_page_order);
455e705c121SKalle Valo 			return;
456e705c121SKalle Valo 		}
457e705c121SKalle Valo 		rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
458e705c121SKalle Valo 				       list);
459e705c121SKalle Valo 		list_del(&rxb->list);
46047ef328cSIlan Peer 		spin_unlock_bh(&rxq->lock);
461e705c121SKalle Valo 
462e705c121SKalle Valo 		BUG_ON(rxb->page);
463e705c121SKalle Valo 		rxb->page = page;
464cfdc20efSJohannes Berg 		rxb->offset = offset;
465e705c121SKalle Valo 		/* Get physical address of the RB */
466e705c121SKalle Valo 		rxb->page_dma =
467cfdc20efSJohannes Berg 			dma_map_page(trans->dev, page, rxb->offset,
46880084e35SJohannes Berg 				     trans_pcie->rx_buf_bytes,
469e705c121SKalle Valo 				     DMA_FROM_DEVICE);
470e705c121SKalle Valo 		if (dma_mapping_error(trans->dev, rxb->page_dma)) {
471e705c121SKalle Valo 			rxb->page = NULL;
47247ef328cSIlan Peer 			spin_lock_bh(&rxq->lock);
473e705c121SKalle Valo 			list_add(&rxb->list, &rxq->rx_used);
47447ef328cSIlan Peer 			spin_unlock_bh(&rxq->lock);
475e705c121SKalle Valo 			__free_pages(page, trans_pcie->rx_page_order);
476e705c121SKalle Valo 			return;
477e705c121SKalle Valo 		}
478e705c121SKalle Valo 
47947ef328cSIlan Peer 		spin_lock_bh(&rxq->lock);
480e705c121SKalle Valo 
481e705c121SKalle Valo 		list_add_tail(&rxb->list, &rxq->rx_free);
482e705c121SKalle Valo 		rxq->free_count++;
483e705c121SKalle Valo 
48447ef328cSIlan Peer 		spin_unlock_bh(&rxq->lock);
485e705c121SKalle Valo 	}
486e705c121SKalle Valo }
487e705c121SKalle Valo 
iwl_pcie_free_rbs_pool(struct iwl_trans * trans)488ff932f61SGolan Ben Ami void iwl_pcie_free_rbs_pool(struct iwl_trans *trans)
489e705c121SKalle Valo {
490e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
491e705c121SKalle Valo 	int i;
492e705c121SKalle Valo 
4936ac57200SJohannes Berg 	if (!trans_pcie->rx_pool)
4946ac57200SJohannes Berg 		return;
4956ac57200SJohannes Berg 
496c042f0c7SJohannes Berg 	for (i = 0; i < RX_POOL_SIZE(trans_pcie->num_rx_bufs); i++) {
49778485054SSara Sharon 		if (!trans_pcie->rx_pool[i].page)
498e705c121SKalle Valo 			continue;
49978485054SSara Sharon 		dma_unmap_page(trans->dev, trans_pcie->rx_pool[i].page_dma,
50080084e35SJohannes Berg 			       trans_pcie->rx_buf_bytes, DMA_FROM_DEVICE);
50178485054SSara Sharon 		__free_pages(trans_pcie->rx_pool[i].page,
50278485054SSara Sharon 			     trans_pcie->rx_page_order);
50378485054SSara Sharon 		trans_pcie->rx_pool[i].page = NULL;
504e705c121SKalle Valo 	}
505e705c121SKalle Valo }
506e705c121SKalle Valo 
507e705c121SKalle Valo /*
508e705c121SKalle Valo  * iwl_pcie_rx_allocator - Allocates pages in the background for RX queues
509e705c121SKalle Valo  *
510e705c121SKalle Valo  * Allocates for each received request 8 pages
511e705c121SKalle Valo  * Called as a scheduled work item.
512e705c121SKalle Valo  */
iwl_pcie_rx_allocator(struct iwl_trans * trans)513e705c121SKalle Valo static void iwl_pcie_rx_allocator(struct iwl_trans *trans)
514e705c121SKalle Valo {
515e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
516e705c121SKalle Valo 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
517e705c121SKalle Valo 	struct list_head local_empty;
518c6ac9f9fSSara Sharon 	int pending = atomic_read(&rba->req_pending);
519e705c121SKalle Valo 
5206dcdd165SSara Sharon 	IWL_DEBUG_TPT(trans, "Pending allocation requests = %d\n", pending);
521e705c121SKalle Valo 
522e705c121SKalle Valo 	/* If we were scheduled - there is at least one request */
52325edc8f2SJohannes Berg 	spin_lock_bh(&rba->lock);
524e705c121SKalle Valo 	/* swap out the rba->rbd_empty to a local list */
525e705c121SKalle Valo 	list_replace_init(&rba->rbd_empty, &local_empty);
52625edc8f2SJohannes Berg 	spin_unlock_bh(&rba->lock);
527e705c121SKalle Valo 
528e705c121SKalle Valo 	while (pending) {
529e705c121SKalle Valo 		int i;
5300979a913SJohannes Berg 		LIST_HEAD(local_allocated);
53178485054SSara Sharon 		gfp_t gfp_mask = GFP_KERNEL;
53278485054SSara Sharon 
53378485054SSara Sharon 		/* Do not post a warning if there are only a few requests */
53478485054SSara Sharon 		if (pending < RX_PENDING_WATERMARK)
53578485054SSara Sharon 			gfp_mask |= __GFP_NOWARN;
536e705c121SKalle Valo 
537e705c121SKalle Valo 		for (i = 0; i < RX_CLAIM_REQ_ALLOC;) {
538e705c121SKalle Valo 			struct iwl_rx_mem_buffer *rxb;
539e705c121SKalle Valo 			struct page *page;
540e705c121SKalle Valo 
541e705c121SKalle Valo 			/* List should never be empty - each reused RBD is
542e705c121SKalle Valo 			 * returned to the list, and initial pool covers any
543e705c121SKalle Valo 			 * possible gap between the time the page is allocated
544e705c121SKalle Valo 			 * to the time the RBD is added.
545e705c121SKalle Valo 			 */
546e705c121SKalle Valo 			BUG_ON(list_empty(&local_empty));
547e705c121SKalle Valo 			/* Get the first rxb from the rbd list */
548e705c121SKalle Valo 			rxb = list_first_entry(&local_empty,
549e705c121SKalle Valo 					       struct iwl_rx_mem_buffer, list);
550e705c121SKalle Valo 			BUG_ON(rxb->page);
551e705c121SKalle Valo 
552e705c121SKalle Valo 			/* Alloc a new receive buffer */
553cfdc20efSJohannes Berg 			page = iwl_pcie_rx_alloc_page(trans, &rxb->offset,
554cfdc20efSJohannes Berg 						      gfp_mask);
555e705c121SKalle Valo 			if (!page)
556e705c121SKalle Valo 				continue;
557e705c121SKalle Valo 			rxb->page = page;
558e705c121SKalle Valo 
559e705c121SKalle Valo 			/* Get physical address of the RB */
560cfdc20efSJohannes Berg 			rxb->page_dma = dma_map_page(trans->dev, page,
561cfdc20efSJohannes Berg 						     rxb->offset,
56280084e35SJohannes Berg 						     trans_pcie->rx_buf_bytes,
563e705c121SKalle Valo 						     DMA_FROM_DEVICE);
564e705c121SKalle Valo 			if (dma_mapping_error(trans->dev, rxb->page_dma)) {
565e705c121SKalle Valo 				rxb->page = NULL;
566e705c121SKalle Valo 				__free_pages(page, trans_pcie->rx_page_order);
567e705c121SKalle Valo 				continue;
568e705c121SKalle Valo 			}
569e705c121SKalle Valo 
570e705c121SKalle Valo 			/* move the allocated entry to the out list */
571e705c121SKalle Valo 			list_move(&rxb->list, &local_allocated);
572e705c121SKalle Valo 			i++;
573e705c121SKalle Valo 		}
574e705c121SKalle Valo 
575c6ac9f9fSSara Sharon 		atomic_dec(&rba->req_pending);
576e705c121SKalle Valo 		pending--;
577c6ac9f9fSSara Sharon 
578e705c121SKalle Valo 		if (!pending) {
579c6ac9f9fSSara Sharon 			pending = atomic_read(&rba->req_pending);
5806dcdd165SSara Sharon 			if (pending)
5816dcdd165SSara Sharon 				IWL_DEBUG_TPT(trans,
582c6ac9f9fSSara Sharon 					      "Got more pending allocation requests = %d\n",
583e705c121SKalle Valo 					      pending);
584e705c121SKalle Valo 		}
585e705c121SKalle Valo 
58625edc8f2SJohannes Berg 		spin_lock_bh(&rba->lock);
587e705c121SKalle Valo 		/* add the allocated rbds to the allocator allocated list */
588e705c121SKalle Valo 		list_splice_tail(&local_allocated, &rba->rbd_allocated);
589e705c121SKalle Valo 		/* get more empty RBDs for current pending requests */
590e705c121SKalle Valo 		list_splice_tail_init(&rba->rbd_empty, &local_empty);
59125edc8f2SJohannes Berg 		spin_unlock_bh(&rba->lock);
592e705c121SKalle Valo 
593e705c121SKalle Valo 		atomic_inc(&rba->req_ready);
594c6ac9f9fSSara Sharon 
595e705c121SKalle Valo 	}
596e705c121SKalle Valo 
59725edc8f2SJohannes Berg 	spin_lock_bh(&rba->lock);
598e705c121SKalle Valo 	/* return unused rbds to the allocator empty list */
599e705c121SKalle Valo 	list_splice_tail(&local_empty, &rba->rbd_empty);
60025edc8f2SJohannes Berg 	spin_unlock_bh(&rba->lock);
601c6ac9f9fSSara Sharon 
6026dcdd165SSara Sharon 	IWL_DEBUG_TPT(trans, "%s, exit.\n", __func__);
603e705c121SKalle Valo }
604e705c121SKalle Valo 
605e705c121SKalle Valo /*
606d56daea4SSara Sharon  * iwl_pcie_rx_allocator_get - returns the pre-allocated pages
607e705c121SKalle Valo .*
608e705c121SKalle Valo .* Called by queue when the queue posted allocation request and
609e705c121SKalle Valo  * has freed 8 RBDs in order to restock itself.
610d56daea4SSara Sharon  * This function directly moves the allocated RBs to the queue's ownership
611d56daea4SSara Sharon  * and updates the relevant counters.
612e705c121SKalle Valo  */
iwl_pcie_rx_allocator_get(struct iwl_trans * trans,struct iwl_rxq * rxq)613d56daea4SSara Sharon static void iwl_pcie_rx_allocator_get(struct iwl_trans *trans,
614d56daea4SSara Sharon 				      struct iwl_rxq *rxq)
615e705c121SKalle Valo {
616e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
617e705c121SKalle Valo 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
618e705c121SKalle Valo 	int i;
619e705c121SKalle Valo 
620d56daea4SSara Sharon 	lockdep_assert_held(&rxq->lock);
621d56daea4SSara Sharon 
622e705c121SKalle Valo 	/*
623e705c121SKalle Valo 	 * atomic_dec_if_positive returns req_ready - 1 for any scenario.
624e705c121SKalle Valo 	 * If req_ready is 0 atomic_dec_if_positive will return -1 and this
625d56daea4SSara Sharon 	 * function will return early, as there are no ready requests.
626e705c121SKalle Valo 	 * atomic_dec_if_positive will perofrm the *actual* decrement only if
627e705c121SKalle Valo 	 * req_ready > 0, i.e. - there are ready requests and the function
628e705c121SKalle Valo 	 * hands one request to the caller.
629e705c121SKalle Valo 	 */
630e705c121SKalle Valo 	if (atomic_dec_if_positive(&rba->req_ready) < 0)
631d56daea4SSara Sharon 		return;
632e705c121SKalle Valo 
633e705c121SKalle Valo 	spin_lock(&rba->lock);
634e705c121SKalle Valo 	for (i = 0; i < RX_CLAIM_REQ_ALLOC; i++) {
635e705c121SKalle Valo 		/* Get next free Rx buffer, remove it from free list */
636d56daea4SSara Sharon 		struct iwl_rx_mem_buffer *rxb =
637d56daea4SSara Sharon 			list_first_entry(&rba->rbd_allocated,
638e705c121SKalle Valo 					 struct iwl_rx_mem_buffer, list);
639d56daea4SSara Sharon 
640d56daea4SSara Sharon 		list_move(&rxb->list, &rxq->rx_free);
641e705c121SKalle Valo 	}
642e705c121SKalle Valo 	spin_unlock(&rba->lock);
643e705c121SKalle Valo 
644d56daea4SSara Sharon 	rxq->used_count -= RX_CLAIM_REQ_ALLOC;
645d56daea4SSara Sharon 	rxq->free_count += RX_CLAIM_REQ_ALLOC;
646e705c121SKalle Valo }
647e705c121SKalle Valo 
iwl_pcie_rx_allocator_work(struct work_struct * data)64810a54d81SLuca Coelho void iwl_pcie_rx_allocator_work(struct work_struct *data)
649e705c121SKalle Valo {
650e705c121SKalle Valo 	struct iwl_rb_allocator *rba_p =
651e705c121SKalle Valo 		container_of(data, struct iwl_rb_allocator, rx_alloc);
652e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie =
653e705c121SKalle Valo 		container_of(rba_p, struct iwl_trans_pcie, rba);
654e705c121SKalle Valo 
655e705c121SKalle Valo 	iwl_pcie_rx_allocator(trans_pcie->trans);
656e705c121SKalle Valo }
657e705c121SKalle Valo 
iwl_pcie_free_bd_size(struct iwl_trans * trans)6585d19e208SJohannes Berg static int iwl_pcie_free_bd_size(struct iwl_trans *trans)
6590307c839SGolan Ben Ami {
6605d19e208SJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
6615d19e208SJohannes Berg 		return sizeof(struct iwl_rx_transfer_desc);
6620307c839SGolan Ben Ami 
6635d19e208SJohannes Berg 	return trans->trans_cfg->mq_rx_supported ?
6645d19e208SJohannes Berg 			sizeof(__le64) : sizeof(__le32);
6655d19e208SJohannes Berg }
6665d19e208SJohannes Berg 
iwl_pcie_used_bd_size(struct iwl_trans * trans)6675d19e208SJohannes Berg static int iwl_pcie_used_bd_size(struct iwl_trans *trans)
6685d19e208SJohannes Berg {
6695d19e208SJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
6705d19e208SJohannes Berg 		return sizeof(struct iwl_rx_completion_desc_bz);
6715d19e208SJohannes Berg 
6725d19e208SJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
6735d19e208SJohannes Berg 		return sizeof(struct iwl_rx_completion_desc);
6745d19e208SJohannes Berg 
6755d19e208SJohannes Berg 	return sizeof(__le32);
6760307c839SGolan Ben Ami }
6770307c839SGolan Ben Ami 
iwl_pcie_free_rxq_dma(struct iwl_trans * trans,struct iwl_rxq * rxq)6781b493e30SGolan Ben Ami static void iwl_pcie_free_rxq_dma(struct iwl_trans *trans,
6791b493e30SGolan Ben Ami 				  struct iwl_rxq *rxq)
6801b493e30SGolan Ben Ami {
6815d19e208SJohannes Berg 	int free_size = iwl_pcie_free_bd_size(trans);
6821b493e30SGolan Ben Ami 
6831b493e30SGolan Ben Ami 	if (rxq->bd)
6840307c839SGolan Ben Ami 		dma_free_coherent(trans->dev,
6850307c839SGolan Ben Ami 				  free_size * rxq->queue_size,
6861b493e30SGolan Ben Ami 				  rxq->bd, rxq->bd_dma);
6871b493e30SGolan Ben Ami 	rxq->bd_dma = 0;
6881b493e30SGolan Ben Ami 	rxq->bd = NULL;
6891b493e30SGolan Ben Ami 
6901b493e30SGolan Ben Ami 	rxq->rb_stts_dma = 0;
6911b493e30SGolan Ben Ami 	rxq->rb_stts = NULL;
6921b493e30SGolan Ben Ami 
6931b493e30SGolan Ben Ami 	if (rxq->used_bd)
6940307c839SGolan Ben Ami 		dma_free_coherent(trans->dev,
6955d19e208SJohannes Berg 				  iwl_pcie_used_bd_size(trans) *
6965d19e208SJohannes Berg 					rxq->queue_size,
6971b493e30SGolan Ben Ami 				  rxq->used_bd, rxq->used_bd_dma);
6981b493e30SGolan Ben Ami 	rxq->used_bd_dma = 0;
6991b493e30SGolan Ben Ami 	rxq->used_bd = NULL;
7001b493e30SGolan Ben Ami }
7011b493e30SGolan Ben Ami 
iwl_pcie_rb_stts_size(struct iwl_trans * trans)7024742c732SJohannes Berg static size_t iwl_pcie_rb_stts_size(struct iwl_trans *trans)
7034742c732SJohannes Berg {
7044742c732SJohannes Berg 	bool use_rx_td = (trans->trans_cfg->device_family >=
7054742c732SJohannes Berg 			  IWL_DEVICE_FAMILY_AX210);
7064742c732SJohannes Berg 
7074742c732SJohannes Berg 	if (use_rx_td)
7084742c732SJohannes Berg 		return sizeof(__le16);
7094742c732SJohannes Berg 
7104742c732SJohannes Berg 	return sizeof(struct iwl_rb_status);
7114742c732SJohannes Berg }
7124742c732SJohannes Berg 
iwl_pcie_alloc_rxq_dma(struct iwl_trans * trans,struct iwl_rxq * rxq)7131b493e30SGolan Ben Ami static int iwl_pcie_alloc_rxq_dma(struct iwl_trans *trans,
7141b493e30SGolan Ben Ami 				  struct iwl_rxq *rxq)
715e705c121SKalle Valo {
716e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
7174742c732SJohannes Berg 	size_t rb_stts_size = iwl_pcie_rb_stts_size(trans);
718e705c121SKalle Valo 	struct device *dev = trans->dev;
71978485054SSara Sharon 	int i;
7200307c839SGolan Ben Ami 	int free_size;
721e705c121SKalle Valo 
72278485054SSara Sharon 	spin_lock_init(&rxq->lock);
723286ca8ebSLuca Coelho 	if (trans->trans_cfg->mq_rx_supported)
724c042f0c7SJohannes Berg 		rxq->queue_size = trans->cfg->num_rbds;
72596a6497bSSara Sharon 	else
72696a6497bSSara Sharon 		rxq->queue_size = RX_QUEUE_SIZE;
72796a6497bSSara Sharon 
7285d19e208SJohannes Berg 	free_size = iwl_pcie_free_bd_size(trans);
7290307c839SGolan Ben Ami 
73078485054SSara Sharon 	/*
73178485054SSara Sharon 	 * Allocate the circular buffer of Read Buffer Descriptors
73278485054SSara Sharon 	 * (RBDs)
73378485054SSara Sharon 	 */
734750afb08SLuis Chamberlain 	rxq->bd = dma_alloc_coherent(dev, free_size * rxq->queue_size,
735e705c121SKalle Valo 				     &rxq->bd_dma, GFP_KERNEL);
736e705c121SKalle Valo 	if (!rxq->bd)
73778485054SSara Sharon 		goto err;
73878485054SSara Sharon 
739286ca8ebSLuca Coelho 	if (trans->trans_cfg->mq_rx_supported) {
740750afb08SLuis Chamberlain 		rxq->used_bd = dma_alloc_coherent(dev,
7415d19e208SJohannes Berg 						  iwl_pcie_used_bd_size(trans) *
7425d19e208SJohannes Berg 							rxq->queue_size,
74396a6497bSSara Sharon 						  &rxq->used_bd_dma,
74496a6497bSSara Sharon 						  GFP_KERNEL);
74596a6497bSSara Sharon 		if (!rxq->used_bd)
74696a6497bSSara Sharon 			goto err;
74796a6497bSSara Sharon 	}
748e705c121SKalle Valo 
7493827cb59SJohannes Berg 	rxq->rb_stts = (u8 *)trans_pcie->base_rb_stts + rxq->id * rb_stts_size;
7506cc6ba3aSTriebitz 	rxq->rb_stts_dma =
7516cc6ba3aSTriebitz 		trans_pcie->base_rb_stts_dma + rxq->id * rb_stts_size;
7521b493e30SGolan Ben Ami 
753e705c121SKalle Valo 	return 0;
754e705c121SKalle Valo 
75578485054SSara Sharon err:
75678485054SSara Sharon 	for (i = 0; i < trans->num_rx_queues; i++) {
75778485054SSara Sharon 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
75878485054SSara Sharon 
7591b493e30SGolan Ben Ami 		iwl_pcie_free_rxq_dma(trans, rxq);
76078485054SSara Sharon 	}
76196a6497bSSara Sharon 
762e705c121SKalle Valo 	return -ENOMEM;
763e705c121SKalle Valo }
764e705c121SKalle Valo 
iwl_pcie_rx_alloc(struct iwl_trans * trans)765ab393cb1SJohannes Berg static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
7661b493e30SGolan Ben Ami {
7671b493e30SGolan Ben Ami 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
7684742c732SJohannes Berg 	size_t rb_stts_size = iwl_pcie_rb_stts_size(trans);
7691b493e30SGolan Ben Ami 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
7701b493e30SGolan Ben Ami 	int i, ret;
7711b493e30SGolan Ben Ami 
7721b493e30SGolan Ben Ami 	if (WARN_ON(trans_pcie->rxq))
7731b493e30SGolan Ben Ami 		return -EINVAL;
7741b493e30SGolan Ben Ami 
7751b493e30SGolan Ben Ami 	trans_pcie->rxq = kcalloc(trans->num_rx_queues, sizeof(struct iwl_rxq),
7761b493e30SGolan Ben Ami 				  GFP_KERNEL);
777c042f0c7SJohannes Berg 	trans_pcie->rx_pool = kcalloc(RX_POOL_SIZE(trans_pcie->num_rx_bufs),
778c042f0c7SJohannes Berg 				      sizeof(trans_pcie->rx_pool[0]),
779c042f0c7SJohannes Berg 				      GFP_KERNEL);
780c042f0c7SJohannes Berg 	trans_pcie->global_table =
781c042f0c7SJohannes Berg 		kcalloc(RX_POOL_SIZE(trans_pcie->num_rx_bufs),
782c042f0c7SJohannes Berg 			sizeof(trans_pcie->global_table[0]),
783c042f0c7SJohannes Berg 			GFP_KERNEL);
784c042f0c7SJohannes Berg 	if (!trans_pcie->rxq || !trans_pcie->rx_pool ||
785c042f0c7SJohannes Berg 	    !trans_pcie->global_table) {
786c042f0c7SJohannes Berg 		ret = -ENOMEM;
787c042f0c7SJohannes Berg 		goto err;
788c042f0c7SJohannes Berg 	}
7891b493e30SGolan Ben Ami 
7901b493e30SGolan Ben Ami 	spin_lock_init(&rba->lock);
7911b493e30SGolan Ben Ami 
7926cc6ba3aSTriebitz 	/*
7936cc6ba3aSTriebitz 	 * Allocate the driver's pointer to receive buffer status.
7946cc6ba3aSTriebitz 	 * Allocate for all queues continuously (HW requirement).
7956cc6ba3aSTriebitz 	 */
7966cc6ba3aSTriebitz 	trans_pcie->base_rb_stts =
7976cc6ba3aSTriebitz 			dma_alloc_coherent(trans->dev,
7986cc6ba3aSTriebitz 					   rb_stts_size * trans->num_rx_queues,
7996cc6ba3aSTriebitz 					   &trans_pcie->base_rb_stts_dma,
8006cc6ba3aSTriebitz 					   GFP_KERNEL);
8016cc6ba3aSTriebitz 	if (!trans_pcie->base_rb_stts) {
8026cc6ba3aSTriebitz 		ret = -ENOMEM;
8036cc6ba3aSTriebitz 		goto err;
8046cc6ba3aSTriebitz 	}
8056cc6ba3aSTriebitz 
8061b493e30SGolan Ben Ami 	for (i = 0; i < trans->num_rx_queues; i++) {
8071b493e30SGolan Ben Ami 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
8081b493e30SGolan Ben Ami 
8096cc6ba3aSTriebitz 		rxq->id = i;
8101b493e30SGolan Ben Ami 		ret = iwl_pcie_alloc_rxq_dma(trans, rxq);
8111b493e30SGolan Ben Ami 		if (ret)
8126cc6ba3aSTriebitz 			goto err;
8131b493e30SGolan Ben Ami 	}
8141b493e30SGolan Ben Ami 	return 0;
8156cc6ba3aSTriebitz 
8166cc6ba3aSTriebitz err:
8176cc6ba3aSTriebitz 	if (trans_pcie->base_rb_stts) {
8186cc6ba3aSTriebitz 		dma_free_coherent(trans->dev,
8196cc6ba3aSTriebitz 				  rb_stts_size * trans->num_rx_queues,
8206cc6ba3aSTriebitz 				  trans_pcie->base_rb_stts,
8216cc6ba3aSTriebitz 				  trans_pcie->base_rb_stts_dma);
8226cc6ba3aSTriebitz 		trans_pcie->base_rb_stts = NULL;
8236cc6ba3aSTriebitz 		trans_pcie->base_rb_stts_dma = 0;
8246cc6ba3aSTriebitz 	}
825c042f0c7SJohannes Berg 	kfree(trans_pcie->rx_pool);
8269cf671d6SEmmanuel Grumbach 	trans_pcie->rx_pool = NULL;
827c042f0c7SJohannes Berg 	kfree(trans_pcie->global_table);
8289cf671d6SEmmanuel Grumbach 	trans_pcie->global_table = NULL;
8296cc6ba3aSTriebitz 	kfree(trans_pcie->rxq);
8309cf671d6SEmmanuel Grumbach 	trans_pcie->rxq = NULL;
8316cc6ba3aSTriebitz 
8326cc6ba3aSTriebitz 	return ret;
8331b493e30SGolan Ben Ami }
8341b493e30SGolan Ben Ami 
iwl_pcie_rx_hw_init(struct iwl_trans * trans,struct iwl_rxq * rxq)835e705c121SKalle Valo static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
836e705c121SKalle Valo {
837e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
838e705c121SKalle Valo 	u32 rb_size;
839e705c121SKalle Valo 	const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
840e705c121SKalle Valo 
8416c4fbcbcSEmmanuel Grumbach 	switch (trans_pcie->rx_buf_size) {
8426c4fbcbcSEmmanuel Grumbach 	case IWL_AMSDU_4K:
843e705c121SKalle Valo 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
8446c4fbcbcSEmmanuel Grumbach 		break;
8456c4fbcbcSEmmanuel Grumbach 	case IWL_AMSDU_8K:
8466c4fbcbcSEmmanuel Grumbach 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
8476c4fbcbcSEmmanuel Grumbach 		break;
8486c4fbcbcSEmmanuel Grumbach 	case IWL_AMSDU_12K:
8496c4fbcbcSEmmanuel Grumbach 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_12K;
8506c4fbcbcSEmmanuel Grumbach 		break;
8516c4fbcbcSEmmanuel Grumbach 	default:
8526c4fbcbcSEmmanuel Grumbach 		WARN_ON(1);
8536c4fbcbcSEmmanuel Grumbach 		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
8546c4fbcbcSEmmanuel Grumbach 	}
855e705c121SKalle Valo 
8561ed08f6fSJohannes Berg 	if (!iwl_trans_grab_nic_access(trans))
857dfcfeef9SSara Sharon 		return;
858dfcfeef9SSara Sharon 
859e705c121SKalle Valo 	/* Stop Rx DMA */
860dfcfeef9SSara Sharon 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
861e705c121SKalle Valo 	/* reset and flush pointers */
862dfcfeef9SSara Sharon 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_RBDCB_WPTR, 0);
863dfcfeef9SSara Sharon 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_FLUSH_RB_REQ, 0);
864dfcfeef9SSara Sharon 	iwl_write32(trans, FH_RSCSR_CHNL0_RDPTR, 0);
865e705c121SKalle Valo 
866e705c121SKalle Valo 	/* Reset driver's Rx queue write index */
867dfcfeef9SSara Sharon 	iwl_write32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
868e705c121SKalle Valo 
869e705c121SKalle Valo 	/* Tell device where to find RBD circular buffer in DRAM */
870dfcfeef9SSara Sharon 	iwl_write32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
871e705c121SKalle Valo 		    (u32)(rxq->bd_dma >> 8));
872e705c121SKalle Valo 
873e705c121SKalle Valo 	/* Tell device where in DRAM to update its Rx status */
874dfcfeef9SSara Sharon 	iwl_write32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
875e705c121SKalle Valo 		    rxq->rb_stts_dma >> 4);
876e705c121SKalle Valo 
877e705c121SKalle Valo 	/* Enable Rx DMA
878e705c121SKalle Valo 	 * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
879e705c121SKalle Valo 	 *      the credit mechanism in 5000 HW RX FIFO
880e705c121SKalle Valo 	 * Direct rx interrupts to hosts
8816c4fbcbcSEmmanuel Grumbach 	 * Rx buffer size 4 or 8k or 12k
882e705c121SKalle Valo 	 * RB timeout 0x10
883e705c121SKalle Valo 	 * 256 RBDs
884e705c121SKalle Valo 	 */
885dfcfeef9SSara Sharon 	iwl_write32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
886e705c121SKalle Valo 		    FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
887e705c121SKalle Valo 		    FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
888e705c121SKalle Valo 		    FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
889e705c121SKalle Valo 		    rb_size |
890e705c121SKalle Valo 		    (RX_RB_TIMEOUT << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
891e705c121SKalle Valo 		    (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
892e705c121SKalle Valo 
8931ed08f6fSJohannes Berg 	iwl_trans_release_nic_access(trans);
894dfcfeef9SSara Sharon 
895e705c121SKalle Valo 	/* Set interrupt coalescing timer to default (2048 usecs) */
896e705c121SKalle Valo 	iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
897e705c121SKalle Valo 
898e705c121SKalle Valo 	/* W/A for interrupt coalescing bug in 7260 and 3160 */
899e705c121SKalle Valo 	if (trans->cfg->host_interrupt_operation_mode)
900e705c121SKalle Valo 		iwl_set_bit(trans, CSR_INT_COALESCING, IWL_HOST_INT_OPER_MODE);
901e705c121SKalle Valo }
902e705c121SKalle Valo 
iwl_pcie_rx_mq_hw_init(struct iwl_trans * trans)903bce97731SSara Sharon static void iwl_pcie_rx_mq_hw_init(struct iwl_trans *trans)
90496a6497bSSara Sharon {
90596a6497bSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
90696a6497bSSara Sharon 	u32 rb_size, enabled = 0;
90796a6497bSSara Sharon 	int i;
90896a6497bSSara Sharon 
90996a6497bSSara Sharon 	switch (trans_pcie->rx_buf_size) {
9101a4968d1SGolan Ben Ami 	case IWL_AMSDU_2K:
9111a4968d1SGolan Ben Ami 		rb_size = RFH_RXF_DMA_RB_SIZE_2K;
9121a4968d1SGolan Ben Ami 		break;
91396a6497bSSara Sharon 	case IWL_AMSDU_4K:
91496a6497bSSara Sharon 		rb_size = RFH_RXF_DMA_RB_SIZE_4K;
91596a6497bSSara Sharon 		break;
91696a6497bSSara Sharon 	case IWL_AMSDU_8K:
91796a6497bSSara Sharon 		rb_size = RFH_RXF_DMA_RB_SIZE_8K;
91896a6497bSSara Sharon 		break;
91996a6497bSSara Sharon 	case IWL_AMSDU_12K:
92096a6497bSSara Sharon 		rb_size = RFH_RXF_DMA_RB_SIZE_12K;
92196a6497bSSara Sharon 		break;
92296a6497bSSara Sharon 	default:
92396a6497bSSara Sharon 		WARN_ON(1);
92496a6497bSSara Sharon 		rb_size = RFH_RXF_DMA_RB_SIZE_4K;
92596a6497bSSara Sharon 	}
92696a6497bSSara Sharon 
9271ed08f6fSJohannes Berg 	if (!iwl_trans_grab_nic_access(trans))
928dfcfeef9SSara Sharon 		return;
929dfcfeef9SSara Sharon 
93096a6497bSSara Sharon 	/* Stop Rx DMA */
931dfcfeef9SSara Sharon 	iwl_write_prph_no_grab(trans, RFH_RXF_DMA_CFG, 0);
93296a6497bSSara Sharon 	/* disable free amd used rx queue operation */
933dfcfeef9SSara Sharon 	iwl_write_prph_no_grab(trans, RFH_RXF_RXQ_ACTIVE, 0);
93496a6497bSSara Sharon 
93596a6497bSSara Sharon 	for (i = 0; i < trans->num_rx_queues; i++) {
93696a6497bSSara Sharon 		/* Tell device where to find RBD free table in DRAM */
93712a17458SSara Sharon 		iwl_write_prph64_no_grab(trans,
938dfcfeef9SSara Sharon 					 RFH_Q_FRBDCB_BA_LSB(i),
939dfcfeef9SSara Sharon 					 trans_pcie->rxq[i].bd_dma);
94096a6497bSSara Sharon 		/* Tell device where to find RBD used table in DRAM */
94112a17458SSara Sharon 		iwl_write_prph64_no_grab(trans,
942dfcfeef9SSara Sharon 					 RFH_Q_URBDCB_BA_LSB(i),
943dfcfeef9SSara Sharon 					 trans_pcie->rxq[i].used_bd_dma);
94496a6497bSSara Sharon 		/* Tell device where in DRAM to update its Rx status */
94512a17458SSara Sharon 		iwl_write_prph64_no_grab(trans,
946dfcfeef9SSara Sharon 					 RFH_Q_URBD_STTS_WPTR_LSB(i),
947bce97731SSara Sharon 					 trans_pcie->rxq[i].rb_stts_dma);
94896a6497bSSara Sharon 		/* Reset device indice tables */
949dfcfeef9SSara Sharon 		iwl_write_prph_no_grab(trans, RFH_Q_FRBDCB_WIDX(i), 0);
950dfcfeef9SSara Sharon 		iwl_write_prph_no_grab(trans, RFH_Q_FRBDCB_RIDX(i), 0);
951dfcfeef9SSara Sharon 		iwl_write_prph_no_grab(trans, RFH_Q_URBDCB_WIDX(i), 0);
95296a6497bSSara Sharon 
95396a6497bSSara Sharon 		enabled |= BIT(i) | BIT(i + 16);
95496a6497bSSara Sharon 	}
95596a6497bSSara Sharon 
95696a6497bSSara Sharon 	/*
95796a6497bSSara Sharon 	 * Enable Rx DMA
95896a6497bSSara Sharon 	 * Rx buffer size 4 or 8k or 12k
95996a6497bSSara Sharon 	 * Min RB size 4 or 8
96088076015SSara Sharon 	 * Drop frames that exceed RB size
96196a6497bSSara Sharon 	 * 512 RBDs
96296a6497bSSara Sharon 	 */
963dfcfeef9SSara Sharon 	iwl_write_prph_no_grab(trans, RFH_RXF_DMA_CFG,
96463044335SSara Sharon 			       RFH_DMA_EN_ENABLE_VAL | rb_size |
96596a6497bSSara Sharon 			       RFH_RXF_DMA_MIN_RB_4_8 |
96688076015SSara Sharon 			       RFH_RXF_DMA_DROP_TOO_LARGE_MASK |
96796a6497bSSara Sharon 			       RFH_RXF_DMA_RBDCB_SIZE_512);
96896a6497bSSara Sharon 
96988076015SSara Sharon 	/*
97088076015SSara Sharon 	 * Activate DMA snooping.
971b0262f07SSara Sharon 	 * Set RX DMA chunk size to 64B for IOSF and 128B for PCIe
97288076015SSara Sharon 	 * Default queue is 0
97388076015SSara Sharon 	 */
974f3779f47SJohannes Berg 	iwl_write_prph_no_grab(trans, RFH_GEN_CFG,
975f3779f47SJohannes Berg 			       RFH_GEN_CFG_RFH_DMA_SNOOP |
976f3779f47SJohannes Berg 			       RFH_GEN_CFG_VAL(DEFAULT_RXQ_NUM, 0) |
977b0262f07SSara Sharon 			       RFH_GEN_CFG_SERVICE_DMA_SNOOP |
978f3779f47SJohannes Berg 			       RFH_GEN_CFG_VAL(RB_CHUNK_SIZE,
9797897dfa2SLuca Coelho 					       trans->trans_cfg->integrated ?
980b0262f07SSara Sharon 					       RFH_GEN_CFG_RB_CHUNK_SIZE_64 :
981f3779f47SJohannes Berg 					       RFH_GEN_CFG_RB_CHUNK_SIZE_128));
98288076015SSara Sharon 	/* Enable the relevant rx queues */
983dfcfeef9SSara Sharon 	iwl_write_prph_no_grab(trans, RFH_RXF_RXQ_ACTIVE, enabled);
984dfcfeef9SSara Sharon 
9851ed08f6fSJohannes Berg 	iwl_trans_release_nic_access(trans);
98696a6497bSSara Sharon 
98796a6497bSSara Sharon 	/* Set interrupt coalescing timer to default (2048 usecs) */
98896a6497bSSara Sharon 	iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
98996a6497bSSara Sharon }
99096a6497bSSara Sharon 
iwl_pcie_rx_init_rxb_lists(struct iwl_rxq * rxq)991ff932f61SGolan Ben Ami void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq)
992e705c121SKalle Valo {
993e705c121SKalle Valo 	lockdep_assert_held(&rxq->lock);
994e705c121SKalle Valo 
995e705c121SKalle Valo 	INIT_LIST_HEAD(&rxq->rx_free);
996e705c121SKalle Valo 	INIT_LIST_HEAD(&rxq->rx_used);
997e705c121SKalle Valo 	rxq->free_count = 0;
998e705c121SKalle Valo 	rxq->used_count = 0;
999e705c121SKalle Valo }
1000e705c121SKalle Valo 
100125edc8f2SJohannes Berg static int iwl_pcie_rx_handle(struct iwl_trans *trans, int queue, int budget);
100225edc8f2SJohannes Berg 
iwl_pcie_napi_poll(struct napi_struct * napi,int budget)100325edc8f2SJohannes Berg static int iwl_pcie_napi_poll(struct napi_struct *napi, int budget)
1004bce97731SSara Sharon {
100525edc8f2SJohannes Berg 	struct iwl_rxq *rxq = container_of(napi, struct iwl_rxq, napi);
100625edc8f2SJohannes Berg 	struct iwl_trans_pcie *trans_pcie;
100725edc8f2SJohannes Berg 	struct iwl_trans *trans;
100825edc8f2SJohannes Berg 	int ret;
100925edc8f2SJohannes Berg 
101025edc8f2SJohannes Berg 	trans_pcie = container_of(napi->dev, struct iwl_trans_pcie, napi_dev);
101125edc8f2SJohannes Berg 	trans = trans_pcie->trans;
101225edc8f2SJohannes Berg 
101325edc8f2SJohannes Berg 	ret = iwl_pcie_rx_handle(trans, rxq->id, budget);
101425edc8f2SJohannes Berg 
10159d401222SMordechay Goodstein 	IWL_DEBUG_ISR(trans, "[%d] handled %d, budget %d\n",
10169d401222SMordechay Goodstein 		      rxq->id, ret, budget);
10179d401222SMordechay Goodstein 
101825edc8f2SJohannes Berg 	if (ret < budget) {
101925edc8f2SJohannes Berg 		spin_lock(&trans_pcie->irq_lock);
102025edc8f2SJohannes Berg 		if (test_bit(STATUS_INT_ENABLED, &trans->status))
102125edc8f2SJohannes Berg 			_iwl_enable_interrupts(trans);
102225edc8f2SJohannes Berg 		spin_unlock(&trans_pcie->irq_lock);
102325edc8f2SJohannes Berg 
102425edc8f2SJohannes Berg 		napi_complete_done(&rxq->napi, ret);
102525edc8f2SJohannes Berg 	}
102625edc8f2SJohannes Berg 
102725edc8f2SJohannes Berg 	return ret;
102825edc8f2SJohannes Berg }
102925edc8f2SJohannes Berg 
iwl_pcie_napi_poll_msix(struct napi_struct * napi,int budget)103025edc8f2SJohannes Berg static int iwl_pcie_napi_poll_msix(struct napi_struct *napi, int budget)
103125edc8f2SJohannes Berg {
103225edc8f2SJohannes Berg 	struct iwl_rxq *rxq = container_of(napi, struct iwl_rxq, napi);
103325edc8f2SJohannes Berg 	struct iwl_trans_pcie *trans_pcie;
103425edc8f2SJohannes Berg 	struct iwl_trans *trans;
103525edc8f2SJohannes Berg 	int ret;
103625edc8f2SJohannes Berg 
103725edc8f2SJohannes Berg 	trans_pcie = container_of(napi->dev, struct iwl_trans_pcie, napi_dev);
103825edc8f2SJohannes Berg 	trans = trans_pcie->trans;
103925edc8f2SJohannes Berg 
104025edc8f2SJohannes Berg 	ret = iwl_pcie_rx_handle(trans, rxq->id, budget);
10412b616666SMordechay Goodstein 	IWL_DEBUG_ISR(trans, "[%d] handled %d, budget %d\n", rxq->id, ret,
10422b616666SMordechay Goodstein 		      budget);
104325edc8f2SJohannes Berg 
104425edc8f2SJohannes Berg 	if (ret < budget) {
10452b616666SMordechay Goodstein 		int irq_line = rxq->id;
10462b616666SMordechay Goodstein 
10472b616666SMordechay Goodstein 		/* FIRST_RSS is shared with line 0 */
10482b616666SMordechay Goodstein 		if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS &&
10492b616666SMordechay Goodstein 		    rxq->id == 1)
10502b616666SMordechay Goodstein 			irq_line = 0;
10512b616666SMordechay Goodstein 
105225edc8f2SJohannes Berg 		spin_lock(&trans_pcie->irq_lock);
10532b616666SMordechay Goodstein 		iwl_pcie_clear_irq(trans, irq_line);
105425edc8f2SJohannes Berg 		spin_unlock(&trans_pcie->irq_lock);
105525edc8f2SJohannes Berg 
105625edc8f2SJohannes Berg 		napi_complete_done(&rxq->napi, ret);
105725edc8f2SJohannes Berg 	}
105825edc8f2SJohannes Berg 
105925edc8f2SJohannes Berg 	return ret;
1060bce97731SSara Sharon }
1061bce97731SSara Sharon 
iwl_pcie_rx_napi_sync(struct iwl_trans * trans)10625af2bb31SGregory Greenman void iwl_pcie_rx_napi_sync(struct iwl_trans *trans)
10635af2bb31SGregory Greenman {
10645af2bb31SGregory Greenman 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
10655af2bb31SGregory Greenman 	int i;
10665af2bb31SGregory Greenman 
10675af2bb31SGregory Greenman 	if (unlikely(!trans_pcie->rxq))
10685af2bb31SGregory Greenman 		return;
10695af2bb31SGregory Greenman 
10705af2bb31SGregory Greenman 	for (i = 0; i < trans->num_rx_queues; i++) {
10715af2bb31SGregory Greenman 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
10725af2bb31SGregory Greenman 
10735af2bb31SGregory Greenman 		if (rxq && rxq->napi.poll)
10745af2bb31SGregory Greenman 			napi_synchronize(&rxq->napi);
10755af2bb31SGregory Greenman 	}
10765af2bb31SGregory Greenman }
10775af2bb31SGregory Greenman 
_iwl_pcie_rx_init(struct iwl_trans * trans)1078ab393cb1SJohannes Berg static int _iwl_pcie_rx_init(struct iwl_trans *trans)
1079e705c121SKalle Valo {
1080e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
108178485054SSara Sharon 	struct iwl_rxq *def_rxq;
1082e705c121SKalle Valo 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
10837b542436SSara Sharon 	int i, err, queue_size, allocator_pool_size, num_alloc;
1084e705c121SKalle Valo 
108578485054SSara Sharon 	if (!trans_pcie->rxq) {
1086e705c121SKalle Valo 		err = iwl_pcie_rx_alloc(trans);
1087e705c121SKalle Valo 		if (err)
1088e705c121SKalle Valo 			return err;
1089e705c121SKalle Valo 	}
109078485054SSara Sharon 	def_rxq = trans_pcie->rxq;
1091e705c121SKalle Valo 
10920f22e400SShaul Triebitz 	cancel_work_sync(&rba->rx_alloc);
10930f22e400SShaul Triebitz 
109425edc8f2SJohannes Berg 	spin_lock_bh(&rba->lock);
1095e705c121SKalle Valo 	atomic_set(&rba->req_pending, 0);
1096e705c121SKalle Valo 	atomic_set(&rba->req_ready, 0);
109796a6497bSSara Sharon 	INIT_LIST_HEAD(&rba->rbd_allocated);
109896a6497bSSara Sharon 	INIT_LIST_HEAD(&rba->rbd_empty);
109925edc8f2SJohannes Berg 	spin_unlock_bh(&rba->lock);
1100e705c121SKalle Valo 
11016ac57200SJohannes Berg 	/* free all first - we overwrite everything here */
110278485054SSara Sharon 	iwl_pcie_free_rbs_pool(trans);
1103e705c121SKalle Valo 
1104e705c121SKalle Valo 	for (i = 0; i < RX_QUEUE_SIZE; i++)
110578485054SSara Sharon 		def_rxq->queue[i] = NULL;
1106e705c121SKalle Valo 
110778485054SSara Sharon 	for (i = 0; i < trans->num_rx_queues; i++) {
110878485054SSara Sharon 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
1109e705c121SKalle Valo 
111047ef328cSIlan Peer 		spin_lock_bh(&rxq->lock);
111178485054SSara Sharon 		/*
111278485054SSara Sharon 		 * Set read write pointer to reflect that we have processed
111378485054SSara Sharon 		 * and used all buffers, but have not restocked the Rx queue
111478485054SSara Sharon 		 * with fresh buffers
111578485054SSara Sharon 		 */
111678485054SSara Sharon 		rxq->read = 0;
111778485054SSara Sharon 		rxq->write = 0;
111878485054SSara Sharon 		rxq->write_actual = 0;
11193681021fSJohannes Berg 		memset(rxq->rb_stts, 0,
11203681021fSJohannes Berg 		       (trans->trans_cfg->device_family >=
11213681021fSJohannes Berg 			IWL_DEVICE_FAMILY_AX210) ?
11220307c839SGolan Ben Ami 		       sizeof(__le16) : sizeof(struct iwl_rb_status));
112378485054SSara Sharon 
112478485054SSara Sharon 		iwl_pcie_rx_init_rxb_lists(rxq);
112578485054SSara Sharon 
1126295d4cd8SJiri Kosina 		spin_unlock_bh(&rxq->lock);
1127295d4cd8SJiri Kosina 
112825edc8f2SJohannes Berg 		if (!rxq->napi.poll) {
112925edc8f2SJohannes Berg 			int (*poll)(struct napi_struct *, int) = iwl_pcie_napi_poll;
113025edc8f2SJohannes Berg 
11312b616666SMordechay Goodstein 			if (trans_pcie->msix_enabled)
113225edc8f2SJohannes Berg 				poll = iwl_pcie_napi_poll_msix;
113325edc8f2SJohannes Berg 
1134bce97731SSara Sharon 			netif_napi_add(&trans_pcie->napi_dev, &rxq->napi,
1135b48b89f9SJakub Kicinski 				       poll);
113625edc8f2SJohannes Berg 			napi_enable(&rxq->napi);
113725edc8f2SJohannes Berg 		}
1138bce97731SSara Sharon 
113978485054SSara Sharon 	}
114078485054SSara Sharon 
114196a6497bSSara Sharon 	/* move the pool to the default queue and allocator ownerships */
1142286ca8ebSLuca Coelho 	queue_size = trans->trans_cfg->mq_rx_supported ?
1143c042f0c7SJohannes Berg 			trans_pcie->num_rx_bufs - 1 : RX_QUEUE_SIZE;
114496a6497bSSara Sharon 	allocator_pool_size = trans->num_rx_queues *
114596a6497bSSara Sharon 		(RX_CLAIM_REQ_ALLOC - RX_POST_REQ_ALLOC);
11467b542436SSara Sharon 	num_alloc = queue_size + allocator_pool_size;
1147c042f0c7SJohannes Berg 
11487b542436SSara Sharon 	for (i = 0; i < num_alloc; i++) {
114996a6497bSSara Sharon 		struct iwl_rx_mem_buffer *rxb = &trans_pcie->rx_pool[i];
115096a6497bSSara Sharon 
115196a6497bSSara Sharon 		if (i < allocator_pool_size)
115296a6497bSSara Sharon 			list_add(&rxb->list, &rba->rbd_empty);
115396a6497bSSara Sharon 		else
115496a6497bSSara Sharon 			list_add(&rxb->list, &def_rxq->rx_used);
115596a6497bSSara Sharon 		trans_pcie->global_table[i] = rxb;
1156e25d65f2SSara Sharon 		rxb->vid = (u16)(i + 1);
1157b1753c62SSara Sharon 		rxb->invalid = true;
115896a6497bSSara Sharon 	}
115978485054SSara Sharon 
116078485054SSara Sharon 	iwl_pcie_rxq_alloc_rbs(trans, GFP_KERNEL, def_rxq);
11612047fa54SSara Sharon 
1162eda50cdeSSara Sharon 	return 0;
1163eda50cdeSSara Sharon }
1164eda50cdeSSara Sharon 
iwl_pcie_rx_init(struct iwl_trans * trans)1165eda50cdeSSara Sharon int iwl_pcie_rx_init(struct iwl_trans *trans)
1166eda50cdeSSara Sharon {
1167eda50cdeSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1168eda50cdeSSara Sharon 	int ret = _iwl_pcie_rx_init(trans);
1169eda50cdeSSara Sharon 
1170eda50cdeSSara Sharon 	if (ret)
1171eda50cdeSSara Sharon 		return ret;
1172eda50cdeSSara Sharon 
1173286ca8ebSLuca Coelho 	if (trans->trans_cfg->mq_rx_supported)
1174bce97731SSara Sharon 		iwl_pcie_rx_mq_hw_init(trans);
11752047fa54SSara Sharon 	else
1176eda50cdeSSara Sharon 		iwl_pcie_rx_hw_init(trans, trans_pcie->rxq);
11772047fa54SSara Sharon 
1178eda50cdeSSara Sharon 	iwl_pcie_rxq_restock(trans, trans_pcie->rxq);
117978485054SSara Sharon 
118047ef328cSIlan Peer 	spin_lock_bh(&trans_pcie->rxq->lock);
1181eda50cdeSSara Sharon 	iwl_pcie_rxq_inc_wr_ptr(trans, trans_pcie->rxq);
118247ef328cSIlan Peer 	spin_unlock_bh(&trans_pcie->rxq->lock);
1183e705c121SKalle Valo 
1184e705c121SKalle Valo 	return 0;
1185e705c121SKalle Valo }
1186e705c121SKalle Valo 
iwl_pcie_gen2_rx_init(struct iwl_trans * trans)1187eda50cdeSSara Sharon int iwl_pcie_gen2_rx_init(struct iwl_trans *trans)
1188eda50cdeSSara Sharon {
1189e506b481SSara Sharon 	/* Set interrupt coalescing timer to default (2048 usecs) */
1190e506b481SSara Sharon 	iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
1191e506b481SSara Sharon 
1192eda50cdeSSara Sharon 	/*
1193eda50cdeSSara Sharon 	 * We don't configure the RFH.
1194eda50cdeSSara Sharon 	 * Restock will be done at alive, after firmware configured the RFH.
1195eda50cdeSSara Sharon 	 */
1196eda50cdeSSara Sharon 	return _iwl_pcie_rx_init(trans);
1197eda50cdeSSara Sharon }
1198eda50cdeSSara Sharon 
iwl_pcie_rx_free(struct iwl_trans * trans)1199e705c121SKalle Valo void iwl_pcie_rx_free(struct iwl_trans *trans)
1200e705c121SKalle Valo {
1201e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
12024742c732SJohannes Berg 	size_t rb_stts_size = iwl_pcie_rb_stts_size(trans);
1203e705c121SKalle Valo 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
120478485054SSara Sharon 	int i;
1205e705c121SKalle Valo 
120678485054SSara Sharon 	/*
120778485054SSara Sharon 	 * if rxq is NULL, it means that nothing has been allocated,
120878485054SSara Sharon 	 * exit now
120978485054SSara Sharon 	 */
121078485054SSara Sharon 	if (!trans_pcie->rxq) {
1211e705c121SKalle Valo 		IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
1212e705c121SKalle Valo 		return;
1213e705c121SKalle Valo 	}
1214e705c121SKalle Valo 
1215e705c121SKalle Valo 	cancel_work_sync(&rba->rx_alloc);
1216e705c121SKalle Valo 
121778485054SSara Sharon 	iwl_pcie_free_rbs_pool(trans);
1218e705c121SKalle Valo 
12196cc6ba3aSTriebitz 	if (trans_pcie->base_rb_stts) {
12206cc6ba3aSTriebitz 		dma_free_coherent(trans->dev,
12216cc6ba3aSTriebitz 				  rb_stts_size * trans->num_rx_queues,
12226cc6ba3aSTriebitz 				  trans_pcie->base_rb_stts,
12236cc6ba3aSTriebitz 				  trans_pcie->base_rb_stts_dma);
12246cc6ba3aSTriebitz 		trans_pcie->base_rb_stts = NULL;
12256cc6ba3aSTriebitz 		trans_pcie->base_rb_stts_dma = 0;
12266cc6ba3aSTriebitz 	}
12276cc6ba3aSTriebitz 
122878485054SSara Sharon 	for (i = 0; i < trans->num_rx_queues; i++) {
122978485054SSara Sharon 		struct iwl_rxq *rxq = &trans_pcie->rxq[i];
123078485054SSara Sharon 
12311b493e30SGolan Ben Ami 		iwl_pcie_free_rxq_dma(trans, rxq);
1232bce97731SSara Sharon 
123325edc8f2SJohannes Berg 		if (rxq->napi.poll) {
123425edc8f2SJohannes Berg 			napi_disable(&rxq->napi);
1235bce97731SSara Sharon 			netif_napi_del(&rxq->napi);
123696a6497bSSara Sharon 		}
123725edc8f2SJohannes Berg 	}
1238c042f0c7SJohannes Berg 	kfree(trans_pcie->rx_pool);
1239c042f0c7SJohannes Berg 	kfree(trans_pcie->global_table);
124078485054SSara Sharon 	kfree(trans_pcie->rxq);
1241cfdc20efSJohannes Berg 
1242cfdc20efSJohannes Berg 	if (trans_pcie->alloc_page)
1243cfdc20efSJohannes Berg 		__free_pages(trans_pcie->alloc_page, trans_pcie->rx_page_order);
1244e705c121SKalle Valo }
1245e705c121SKalle Valo 
iwl_pcie_rx_move_to_allocator(struct iwl_rxq * rxq,struct iwl_rb_allocator * rba)1246868a1e86SShaul Triebitz static void iwl_pcie_rx_move_to_allocator(struct iwl_rxq *rxq,
1247868a1e86SShaul Triebitz 					  struct iwl_rb_allocator *rba)
1248868a1e86SShaul Triebitz {
1249868a1e86SShaul Triebitz 	spin_lock(&rba->lock);
1250868a1e86SShaul Triebitz 	list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
1251868a1e86SShaul Triebitz 	spin_unlock(&rba->lock);
1252868a1e86SShaul Triebitz }
1253868a1e86SShaul Triebitz 
1254e705c121SKalle Valo /*
1255e705c121SKalle Valo  * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
1256e705c121SKalle Valo  *
1257e705c121SKalle Valo  * Called when a RBD can be reused. The RBD is transferred to the allocator.
1258e705c121SKalle Valo  * When there are 2 empty RBDs - a request for allocation is posted
1259e705c121SKalle Valo  */
iwl_pcie_rx_reuse_rbd(struct iwl_trans * trans,struct iwl_rx_mem_buffer * rxb,struct iwl_rxq * rxq,bool emergency)1260e705c121SKalle Valo static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
1261e705c121SKalle Valo 				  struct iwl_rx_mem_buffer *rxb,
1262e705c121SKalle Valo 				  struct iwl_rxq *rxq, bool emergency)
1263e705c121SKalle Valo {
1264e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1265e705c121SKalle Valo 	struct iwl_rb_allocator *rba = &trans_pcie->rba;
1266e705c121SKalle Valo 
1267e705c121SKalle Valo 	/* Move the RBD to the used list, will be moved to allocator in batches
1268e705c121SKalle Valo 	 * before claiming or posting a request*/
1269e705c121SKalle Valo 	list_add_tail(&rxb->list, &rxq->rx_used);
1270e705c121SKalle Valo 
1271e705c121SKalle Valo 	if (unlikely(emergency))
1272e705c121SKalle Valo 		return;
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	/* Count the allocator owned RBDs */
1275e705c121SKalle Valo 	rxq->used_count++;
1276e705c121SKalle Valo 
1277e705c121SKalle Valo 	/* If we have RX_POST_REQ_ALLOC new released rx buffers -
1278e705c121SKalle Valo 	 * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
1279e705c121SKalle Valo 	 * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
1280e705c121SKalle Valo 	 * after but we still need to post another request.
1281e705c121SKalle Valo 	 */
1282e705c121SKalle Valo 	if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
1283e705c121SKalle Valo 		/* Move the 2 RBDs to the allocator ownership.
1284e705c121SKalle Valo 		 Allocator has another 6 from pool for the request completion*/
1285868a1e86SShaul Triebitz 		iwl_pcie_rx_move_to_allocator(rxq, rba);
1286e705c121SKalle Valo 
1287e705c121SKalle Valo 		atomic_inc(&rba->req_pending);
1288e705c121SKalle Valo 		queue_work(rba->alloc_wq, &rba->rx_alloc);
1289e705c121SKalle Valo 	}
1290e705c121SKalle Valo }
1291e705c121SKalle Valo 
iwl_pcie_rx_handle_rb(struct iwl_trans * trans,struct iwl_rxq * rxq,struct iwl_rx_mem_buffer * rxb,bool emergency,int i)1292e705c121SKalle Valo static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
129378485054SSara Sharon 				struct iwl_rxq *rxq,
1294e705c121SKalle Valo 				struct iwl_rx_mem_buffer *rxb,
12957891965dSSara Sharon 				bool emergency,
12967891965dSSara Sharon 				int i)
1297e705c121SKalle Valo {
1298e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
12994f4822b7SMordechay Goodstein 	struct iwl_txq *txq = trans->txqs.txq[trans->txqs.cmd.q_id];
1300e705c121SKalle Valo 	bool page_stolen = false;
130180084e35SJohannes Berg 	int max_len = trans_pcie->rx_buf_bytes;
1302e705c121SKalle Valo 	u32 offset = 0;
1303e705c121SKalle Valo 
1304e705c121SKalle Valo 	if (WARN_ON(!rxb))
1305e705c121SKalle Valo 		return;
1306e705c121SKalle Valo 
1307e705c121SKalle Valo 	dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
1308e705c121SKalle Valo 
1309e705c121SKalle Valo 	while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
1310e705c121SKalle Valo 		struct iwl_rx_packet *pkt;
1311e705c121SKalle Valo 		bool reclaim;
1312e4475583SJohannes Berg 		int len;
1313e705c121SKalle Valo 		struct iwl_rx_cmd_buffer rxcb = {
1314cfdc20efSJohannes Berg 			._offset = rxb->offset + offset,
1315e705c121SKalle Valo 			._rx_page_order = trans_pcie->rx_page_order,
1316e705c121SKalle Valo 			._page = rxb->page,
1317e705c121SKalle Valo 			._page_stolen = false,
1318e705c121SKalle Valo 			.truesize = max_len,
1319e705c121SKalle Valo 		};
1320e705c121SKalle Valo 
1321e705c121SKalle Valo 		pkt = rxb_addr(&rxcb);
1322e705c121SKalle Valo 
13233bfdee76SJohannes Berg 		if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID)) {
13243bfdee76SJohannes Berg 			IWL_DEBUG_RX(trans,
13253bfdee76SJohannes Berg 				     "Q %d: RB end marker at offset %d\n",
13263bfdee76SJohannes Berg 				     rxq->id, offset);
1327e705c121SKalle Valo 			break;
13283bfdee76SJohannes Berg 		}
1329e705c121SKalle Valo 
1330a395058eSJohannes Berg 		WARN((le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_RXQ_MASK) >>
1331a395058eSJohannes Berg 			FH_RSCSR_RXQ_POS != rxq->id,
1332a395058eSJohannes Berg 		     "frame on invalid queue - is on %d and indicates %d\n",
1333a395058eSJohannes Berg 		     rxq->id,
1334a395058eSJohannes Berg 		     (le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_RXQ_MASK) >>
1335a395058eSJohannes Berg 			FH_RSCSR_RXQ_POS);
1336ab2e696bSSara Sharon 
1337e705c121SKalle Valo 		IWL_DEBUG_RX(trans,
13383bfdee76SJohannes Berg 			     "Q %d: cmd at offset %d: %s (%.2x.%2x, seq 0x%x)\n",
13393bfdee76SJohannes Berg 			     rxq->id, offset,
134039bdb17eSSharon Dvir 			     iwl_get_cmd_string(trans,
1341f0c86427SJohannes Berg 						WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)),
134235177c99SSara Sharon 			     pkt->hdr.group_id, pkt->hdr.cmd,
134335177c99SSara Sharon 			     le16_to_cpu(pkt->hdr.sequence));
1344e705c121SKalle Valo 
1345e705c121SKalle Valo 		len = iwl_rx_packet_len(pkt);
1346e705c121SKalle Valo 		len += sizeof(u32); /* account for status word */
1347df72138dSJohannes Berg 
1348df72138dSJohannes Berg 		offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
1349df72138dSJohannes Berg 
1350df72138dSJohannes Berg 		/* check that what the device tells us made sense */
1351f1658dcbSAndrei Otcheretianski 		if (len < sizeof(*pkt) || offset > max_len)
1352df72138dSJohannes Berg 			break;
1353df72138dSJohannes Berg 
1354e705c121SKalle Valo 		trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
1355e705c121SKalle Valo 		trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
1356e705c121SKalle Valo 
1357e705c121SKalle Valo 		/* Reclaim a command buffer only if this packet is a response
1358e705c121SKalle Valo 		 *   to a (driver-originated) command.
1359e705c121SKalle Valo 		 * If the packet (e.g. Rx frame) originated from uCode,
1360e705c121SKalle Valo 		 *   there is no command buffer to reclaim.
1361e705c121SKalle Valo 		 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1362e705c121SKalle Valo 		 *   but apparently a few don't get set; catch them here. */
1363e705c121SKalle Valo 		reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
1364d8a130b0SJohannes Berg 		if (reclaim && !pkt->hdr.group_id) {
1365e705c121SKalle Valo 			int i;
1366e705c121SKalle Valo 
1367e705c121SKalle Valo 			for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
1368e705c121SKalle Valo 				if (trans_pcie->no_reclaim_cmds[i] ==
1369e705c121SKalle Valo 							pkt->hdr.cmd) {
1370e705c121SKalle Valo 					reclaim = false;
1371e705c121SKalle Valo 					break;
1372e705c121SKalle Valo 				}
1373e705c121SKalle Valo 			}
1374e705c121SKalle Valo 		}
1375e705c121SKalle Valo 
1376d5050543SJohannes Berg 		if (rxq->id == IWL_DEFAULT_RX_QUEUE)
1377bce97731SSara Sharon 			iwl_op_mode_rx(trans->op_mode, &rxq->napi,
1378bce97731SSara Sharon 				       &rxcb);
1379bce97731SSara Sharon 		else
1380bce97731SSara Sharon 			iwl_op_mode_rx_rss(trans->op_mode, &rxq->napi,
1381bce97731SSara Sharon 					   &rxcb, rxq->id);
1382e705c121SKalle Valo 
1383e705c121SKalle Valo 		/*
1384e705c121SKalle Valo 		 * After here, we should always check rxcb._page_stolen,
1385e705c121SKalle Valo 		 * if it is true then one of the handlers took the page.
1386e705c121SKalle Valo 		 */
1387e705c121SKalle Valo 
1388015cfa30SAvraham Stern 		if (reclaim && txq) {
1389e4475583SJohannes Berg 			u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1390e4475583SJohannes Berg 			int index = SEQ_TO_INDEX(sequence);
1391e4475583SJohannes Berg 			int cmd_index = iwl_txq_get_cmd_index(txq, index);
1392e4475583SJohannes Berg 
1393e4475583SJohannes Berg 			kfree_sensitive(txq->entries[cmd_index].free_buf);
1394e4475583SJohannes Berg 			txq->entries[cmd_index].free_buf = NULL;
1395e4475583SJohannes Berg 
1396e705c121SKalle Valo 			/* Invoke any callbacks, transfer the buffer to caller,
1397e705c121SKalle Valo 			 * and fire off the (possibly) blocking
1398e705c121SKalle Valo 			 * iwl_trans_send_cmd()
1399e705c121SKalle Valo 			 * as we reclaim the driver command queue */
1400e705c121SKalle Valo 			if (!rxcb._page_stolen)
1401e705c121SKalle Valo 				iwl_pcie_hcmd_complete(trans, &rxcb);
1402e705c121SKalle Valo 			else
1403e705c121SKalle Valo 				IWL_WARN(trans, "Claim null rxb?\n");
1404e705c121SKalle Valo 		}
1405e705c121SKalle Valo 
1406e705c121SKalle Valo 		page_stolen |= rxcb._page_stolen;
14073681021fSJohannes Berg 		if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
14080307c839SGolan Ben Ami 			break;
1409e705c121SKalle Valo 	}
1410e705c121SKalle Valo 
1411e705c121SKalle Valo 	/* page was stolen from us -- free our reference */
1412e705c121SKalle Valo 	if (page_stolen) {
1413e705c121SKalle Valo 		__free_pages(rxb->page, trans_pcie->rx_page_order);
1414e705c121SKalle Valo 		rxb->page = NULL;
1415e705c121SKalle Valo 	}
1416e705c121SKalle Valo 
1417e705c121SKalle Valo 	/* Reuse the page if possible. For notification packets and
1418e705c121SKalle Valo 	 * SKBs that fail to Rx correctly, add them back into the
1419e705c121SKalle Valo 	 * rx_free list for reuse later. */
1420e705c121SKalle Valo 	if (rxb->page != NULL) {
1421e705c121SKalle Valo 		rxb->page_dma =
1422cfdc20efSJohannes Berg 			dma_map_page(trans->dev, rxb->page, rxb->offset,
142380084e35SJohannes Berg 				     trans_pcie->rx_buf_bytes,
1424e705c121SKalle Valo 				     DMA_FROM_DEVICE);
1425e705c121SKalle Valo 		if (dma_mapping_error(trans->dev, rxb->page_dma)) {
1426e705c121SKalle Valo 			/*
1427e705c121SKalle Valo 			 * free the page(s) as well to not break
1428e705c121SKalle Valo 			 * the invariant that the items on the used
1429e705c121SKalle Valo 			 * list have no page(s)
1430e705c121SKalle Valo 			 */
1431e705c121SKalle Valo 			__free_pages(rxb->page, trans_pcie->rx_page_order);
1432e705c121SKalle Valo 			rxb->page = NULL;
1433e705c121SKalle Valo 			iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
1434e705c121SKalle Valo 		} else {
1435e705c121SKalle Valo 			list_add_tail(&rxb->list, &rxq->rx_free);
1436e705c121SKalle Valo 			rxq->free_count++;
1437e705c121SKalle Valo 		}
1438e705c121SKalle Valo 	} else
1439e705c121SKalle Valo 		iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
1440e705c121SKalle Valo }
1441e705c121SKalle Valo 
iwl_pcie_get_rxb(struct iwl_trans * trans,struct iwl_rxq * rxq,int i,bool * join)14421b4bbe8bSSara Sharon static struct iwl_rx_mem_buffer *iwl_pcie_get_rxb(struct iwl_trans *trans,
1443b1c860f6SJohannes Berg 						  struct iwl_rxq *rxq, int i,
1444b1c860f6SJohannes Berg 						  bool *join)
14451b4bbe8bSSara Sharon {
14461b4bbe8bSSara Sharon 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
14471b4bbe8bSSara Sharon 	struct iwl_rx_mem_buffer *rxb;
14481b4bbe8bSSara Sharon 	u16 vid;
14491b4bbe8bSSara Sharon 
1450f826faaaSJohannes Berg 	BUILD_BUG_ON(sizeof(struct iwl_rx_completion_desc) != 32);
14515d19e208SJohannes Berg 	BUILD_BUG_ON(sizeof(struct iwl_rx_completion_desc_bz) != 4);
1452f826faaaSJohannes Berg 
1453286ca8ebSLuca Coelho 	if (!trans->trans_cfg->mq_rx_supported) {
14541b4bbe8bSSara Sharon 		rxb = rxq->queue[i];
14551b4bbe8bSSara Sharon 		rxq->queue[i] = NULL;
14561b4bbe8bSSara Sharon 		return rxb;
14571b4bbe8bSSara Sharon 	}
14581b4bbe8bSSara Sharon 
14595d19e208SJohannes Berg 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ) {
14605d19e208SJohannes Berg 		struct iwl_rx_completion_desc_bz *cd = rxq->used_bd;
14615d19e208SJohannes Berg 
14625d19e208SJohannes Berg 		vid = le16_to_cpu(cd[i].rbid);
14635d19e208SJohannes Berg 		*join = cd[i].flags & IWL_RX_CD_FLAGS_FRAGMENTED;
14645d19e208SJohannes Berg 	} else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
14655d19e208SJohannes Berg 		struct iwl_rx_completion_desc *cd = rxq->used_bd;
14665d19e208SJohannes Berg 
14675d19e208SJohannes Berg 		vid = le16_to_cpu(cd[i].rbid);
14685d19e208SJohannes Berg 		*join = cd[i].flags & IWL_RX_CD_FLAGS_FRAGMENTED;
1469b1c860f6SJohannes Berg 	} else {
14705d19e208SJohannes Berg 		__le32 *cd = rxq->used_bd;
14715d19e208SJohannes Berg 
14725d19e208SJohannes Berg 		vid = le32_to_cpu(cd[i]) & 0x0FFF; /* 12-bit VID */
1473b1c860f6SJohannes Berg 	}
14741b4bbe8bSSara Sharon 
1475c042f0c7SJohannes Berg 	if (!vid || vid > RX_POOL_SIZE(trans_pcie->num_rx_bufs))
14761b4bbe8bSSara Sharon 		goto out_err;
14771b4bbe8bSSara Sharon 
14781b4bbe8bSSara Sharon 	rxb = trans_pcie->global_table[vid - 1];
14791b4bbe8bSSara Sharon 	if (rxb->invalid)
14801b4bbe8bSSara Sharon 		goto out_err;
14811b4bbe8bSSara Sharon 
148285d78bb1SSara Sharon 	IWL_DEBUG_RX(trans, "Got virtual RB ID %u\n", (u32)rxb->vid);
148385d78bb1SSara Sharon 
14841b4bbe8bSSara Sharon 	rxb->invalid = true;
14851b4bbe8bSSara Sharon 
14861b4bbe8bSSara Sharon 	return rxb;
14871b4bbe8bSSara Sharon 
14881b4bbe8bSSara Sharon out_err:
14891b4bbe8bSSara Sharon 	WARN(1, "Invalid rxb from HW %u\n", (u32)vid);
14901b4bbe8bSSara Sharon 	iwl_force_nmi(trans);
14911b4bbe8bSSara Sharon 	return NULL;
14921b4bbe8bSSara Sharon }
14931b4bbe8bSSara Sharon 
1494e705c121SKalle Valo /*
1495e705c121SKalle Valo  * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
1496e705c121SKalle Valo  */
iwl_pcie_rx_handle(struct iwl_trans * trans,int queue,int budget)149725edc8f2SJohannes Berg static int iwl_pcie_rx_handle(struct iwl_trans *trans, int queue, int budget)
1498e705c121SKalle Valo {
1499e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
150030f24eabSJohannes Berg 	struct iwl_rxq *rxq;
150125edc8f2SJohannes Berg 	u32 r, i, count = 0, handled = 0;
1502e705c121SKalle Valo 	bool emergency = false;
1503e705c121SKalle Valo 
150430f24eabSJohannes Berg 	if (WARN_ON_ONCE(!trans_pcie->rxq || !trans_pcie->rxq[queue].bd))
150525edc8f2SJohannes Berg 		return budget;
150630f24eabSJohannes Berg 
150730f24eabSJohannes Berg 	rxq = &trans_pcie->rxq[queue];
150830f24eabSJohannes Berg 
1509e705c121SKalle Valo restart:
1510e705c121SKalle Valo 	spin_lock(&rxq->lock);
1511e705c121SKalle Valo 	/* uCode's read index (stored in shared DRAM) indicates the last Rx
1512e705c121SKalle Valo 	 * buffer that the driver may process (last buffer filled by ucode). */
15139fe75ad3SJohannes Berg 	r = iwl_get_closed_rb_stts(trans, rxq);
1514e705c121SKalle Valo 	i = rxq->read;
1515e705c121SKalle Valo 
15165eae443eSSara Sharon 	/* W/A 9000 device step A0 wrap-around bug */
15175eae443eSSara Sharon 	r &= (rxq->queue_size - 1);
15185eae443eSSara Sharon 
1519e705c121SKalle Valo 	/* Rx interrupt, but nothing sent from uCode */
1520e705c121SKalle Valo 	if (i == r)
15215eae443eSSara Sharon 		IWL_DEBUG_RX(trans, "Q %d: HW = SW = %d\n", rxq->id, r);
1522e705c121SKalle Valo 
152325edc8f2SJohannes Berg 	while (i != r && ++handled < budget) {
1524868a1e86SShaul Triebitz 		struct iwl_rb_allocator *rba = &trans_pcie->rba;
1525e705c121SKalle Valo 		struct iwl_rx_mem_buffer *rxb;
1526868a1e86SShaul Triebitz 		/* number of RBDs still waiting for page allocation */
1527868a1e86SShaul Triebitz 		u32 rb_pending_alloc =
1528868a1e86SShaul Triebitz 			atomic_read(&trans_pcie->rba.req_pending) *
1529868a1e86SShaul Triebitz 			RX_CLAIM_REQ_ALLOC;
1530b1c860f6SJohannes Berg 		bool join = false;
1531e705c121SKalle Valo 
1532868a1e86SShaul Triebitz 		if (unlikely(rb_pending_alloc >= rxq->queue_size / 2 &&
1533868a1e86SShaul Triebitz 			     !emergency)) {
1534868a1e86SShaul Triebitz 			iwl_pcie_rx_move_to_allocator(rxq, rba);
1535e705c121SKalle Valo 			emergency = true;
15366dcdd165SSara Sharon 			IWL_DEBUG_TPT(trans,
15376dcdd165SSara Sharon 				      "RX path is in emergency. Pending allocations %d\n",
15386dcdd165SSara Sharon 				      rb_pending_alloc);
1539868a1e86SShaul Triebitz 		}
1540e705c121SKalle Valo 
154185d78bb1SSara Sharon 		IWL_DEBUG_RX(trans, "Q %d: HW = %d, SW = %d\n", rxq->id, r, i);
154285d78bb1SSara Sharon 
1543b1c860f6SJohannes Berg 		rxb = iwl_pcie_get_rxb(trans, rxq, i, &join);
15441b4bbe8bSSara Sharon 		if (!rxb)
15455eae443eSSara Sharon 			goto out;
1546e705c121SKalle Valo 
1547b1c860f6SJohannes Berg 		if (unlikely(join || rxq->next_rb_is_fragment)) {
1548b1c860f6SJohannes Berg 			rxq->next_rb_is_fragment = join;
1549b1c860f6SJohannes Berg 			/*
1550b1c860f6SJohannes Berg 			 * We can only get a multi-RB in the following cases:
1551b1c860f6SJohannes Berg 			 *  - firmware issue, sending a too big notification
1552b1c860f6SJohannes Berg 			 *  - sniffer mode with a large A-MSDU
1553b1c860f6SJohannes Berg 			 *  - large MTU frames (>2k)
1554b1c860f6SJohannes Berg 			 * since the multi-RB functionality is limited to newer
1555b1c860f6SJohannes Berg 			 * hardware that cannot put multiple entries into a
1556b1c860f6SJohannes Berg 			 * single RB.
1557b1c860f6SJohannes Berg 			 *
1558b1c860f6SJohannes Berg 			 * Right now, the higher layers aren't set up to deal
1559b1c860f6SJohannes Berg 			 * with that, so discard all of these.
1560b1c860f6SJohannes Berg 			 */
1561b1c860f6SJohannes Berg 			list_add_tail(&rxb->list, &rxq->rx_free);
1562b1c860f6SJohannes Berg 			rxq->free_count++;
1563b1c860f6SJohannes Berg 		} else {
15647891965dSSara Sharon 			iwl_pcie_rx_handle_rb(trans, rxq, rxb, emergency, i);
1565b1c860f6SJohannes Berg 		}
1566e705c121SKalle Valo 
156796a6497bSSara Sharon 		i = (i + 1) & (rxq->queue_size - 1);
1568e705c121SKalle Valo 
1569d56daea4SSara Sharon 		/*
1570d56daea4SSara Sharon 		 * If we have RX_CLAIM_REQ_ALLOC released rx buffers -
1571d56daea4SSara Sharon 		 * try to claim the pre-allocated buffers from the allocator.
1572d56daea4SSara Sharon 		 * If not ready - will try to reclaim next time.
1573d56daea4SSara Sharon 		 * There is no need to reschedule work - allocator exits only
1574d56daea4SSara Sharon 		 * on success
1575e705c121SKalle Valo 		 */
1576d56daea4SSara Sharon 		if (rxq->used_count >= RX_CLAIM_REQ_ALLOC)
1577d56daea4SSara Sharon 			iwl_pcie_rx_allocator_get(trans, rxq);
1578e705c121SKalle Valo 
1579d56daea4SSara Sharon 		if (rxq->used_count % RX_CLAIM_REQ_ALLOC == 0 && !emergency) {
1580d56daea4SSara Sharon 			/* Add the remaining empty RBDs for allocator use */
1581868a1e86SShaul Triebitz 			iwl_pcie_rx_move_to_allocator(rxq, rba);
1582d56daea4SSara Sharon 		} else if (emergency) {
1583e705c121SKalle Valo 			count++;
1584e705c121SKalle Valo 			if (count == 8) {
1585e705c121SKalle Valo 				count = 0;
15866dcdd165SSara Sharon 				if (rb_pending_alloc < rxq->queue_size / 3) {
15876dcdd165SSara Sharon 					IWL_DEBUG_TPT(trans,
15886dcdd165SSara Sharon 						      "RX path exited emergency. Pending allocations %d\n",
15896dcdd165SSara Sharon 						      rb_pending_alloc);
1590e705c121SKalle Valo 					emergency = false;
15916dcdd165SSara Sharon 				}
1592e0e168dcSGregory Greenman 
1593e705c121SKalle Valo 				rxq->read = i;
1594e705c121SKalle Valo 				spin_unlock(&rxq->lock);
1595e0e168dcSGregory Greenman 				iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
159678485054SSara Sharon 				iwl_pcie_rxq_restock(trans, rxq);
1597e705c121SKalle Valo 				goto restart;
1598e705c121SKalle Valo 			}
1599e705c121SKalle Valo 		}
1600e0e168dcSGregory Greenman 	}
16015eae443eSSara Sharon out:
1602e705c121SKalle Valo 	/* Backtrack one entry */
1603e705c121SKalle Valo 	rxq->read = i;
1604e705c121SKalle Valo 	spin_unlock(&rxq->lock);
1605e705c121SKalle Valo 
1606e705c121SKalle Valo 	/*
1607e705c121SKalle Valo 	 * handle a case where in emergency there are some unallocated RBDs.
1608e705c121SKalle Valo 	 * those RBDs are in the used list, but are not tracked by the queue's
1609e705c121SKalle Valo 	 * used_count which counts allocator owned RBDs.
1610e705c121SKalle Valo 	 * unallocated emergency RBDs must be allocated on exit, otherwise
1611e705c121SKalle Valo 	 * when called again the function may not be in emergency mode and
1612e705c121SKalle Valo 	 * they will be handed to the allocator with no tracking in the RBD
1613e705c121SKalle Valo 	 * allocator counters, which will lead to them never being claimed back
1614e705c121SKalle Valo 	 * by the queue.
1615e705c121SKalle Valo 	 * by allocating them here, they are now in the queue free list, and
1616e705c121SKalle Valo 	 * will be restocked by the next call of iwl_pcie_rxq_restock.
1617e705c121SKalle Valo 	 */
1618e705c121SKalle Valo 	if (unlikely(emergency && count))
161978485054SSara Sharon 		iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
1620e705c121SKalle Valo 
1621e0e168dcSGregory Greenman 	iwl_pcie_rxq_restock(trans, rxq);
162225edc8f2SJohannes Berg 
162325edc8f2SJohannes Berg 	return handled;
1624e705c121SKalle Valo }
1625e705c121SKalle Valo 
iwl_pcie_get_trans_pcie(struct msix_entry * entry)16262e5d4a8fSHaim Dreyfuss static struct iwl_trans_pcie *iwl_pcie_get_trans_pcie(struct msix_entry *entry)
16272e5d4a8fSHaim Dreyfuss {
16282e5d4a8fSHaim Dreyfuss 	u8 queue = entry->entry;
16292e5d4a8fSHaim Dreyfuss 	struct msix_entry *entries = entry - queue;
16302e5d4a8fSHaim Dreyfuss 
16312e5d4a8fSHaim Dreyfuss 	return container_of(entries, struct iwl_trans_pcie, msix_entries[0]);
16322e5d4a8fSHaim Dreyfuss }
16332e5d4a8fSHaim Dreyfuss 
16342e5d4a8fSHaim Dreyfuss /*
16352e5d4a8fSHaim Dreyfuss  * iwl_pcie_rx_msix_handle - Main entry function for receiving responses from fw
16362e5d4a8fSHaim Dreyfuss  * This interrupt handler should be used with RSS queue only.
16372e5d4a8fSHaim Dreyfuss  */
iwl_pcie_irq_rx_msix_handler(int irq,void * dev_id)16382e5d4a8fSHaim Dreyfuss irqreturn_t iwl_pcie_irq_rx_msix_handler(int irq, void *dev_id)
16392e5d4a8fSHaim Dreyfuss {
16402e5d4a8fSHaim Dreyfuss 	struct msix_entry *entry = dev_id;
16412e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = iwl_pcie_get_trans_pcie(entry);
16422e5d4a8fSHaim Dreyfuss 	struct iwl_trans *trans = trans_pcie->trans;
16431902f195SAnjaneyulu 	struct iwl_rxq *rxq;
16442e5d4a8fSHaim Dreyfuss 
1645c42ff65dSJohannes Berg 	trace_iwlwifi_dev_irq_msix(trans->dev, entry, false, 0, 0);
1646c42ff65dSJohannes Berg 
16475eae443eSSara Sharon 	if (WARN_ON(entry->entry >= trans->num_rx_queues))
16485eae443eSSara Sharon 		return IRQ_NONE;
16495eae443eSSara Sharon 
16501902f195SAnjaneyulu 	if (!trans_pcie->rxq) {
165191ca9c3aSEmmanuel Grumbach 		if (net_ratelimit())
165291ca9c3aSEmmanuel Grumbach 			IWL_ERR(trans,
165391ca9c3aSEmmanuel Grumbach 				"[%d] Got MSI-X interrupt before we have Rx queues\n",
165491ca9c3aSEmmanuel Grumbach 				entry->entry);
1655abc599efSEmmanuel Grumbach 		return IRQ_NONE;
165691ca9c3aSEmmanuel Grumbach 	}
1657abc599efSEmmanuel Grumbach 
16581902f195SAnjaneyulu 	rxq = &trans_pcie->rxq[entry->entry];
16592e5d4a8fSHaim Dreyfuss 	lock_map_acquire(&trans->sync_cmd_lockdep_map);
16609d401222SMordechay Goodstein 	IWL_DEBUG_ISR(trans, "[%d] Got interrupt\n", entry->entry);
16612e5d4a8fSHaim Dreyfuss 
16622e5d4a8fSHaim Dreyfuss 	local_bh_disable();
166325edc8f2SJohannes Berg 	if (napi_schedule_prep(&rxq->napi))
166425edc8f2SJohannes Berg 		__napi_schedule(&rxq->napi);
166525edc8f2SJohannes Berg 	else
166625edc8f2SJohannes Berg 		iwl_pcie_clear_irq(trans, entry->entry);
16672e5d4a8fSHaim Dreyfuss 	local_bh_enable();
16682e5d4a8fSHaim Dreyfuss 
16692e5d4a8fSHaim Dreyfuss 	lock_map_release(&trans->sync_cmd_lockdep_map);
16702e5d4a8fSHaim Dreyfuss 
16712e5d4a8fSHaim Dreyfuss 	return IRQ_HANDLED;
16722e5d4a8fSHaim Dreyfuss }
16732e5d4a8fSHaim Dreyfuss 
1674e705c121SKalle Valo /*
1675e705c121SKalle Valo  * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
1676e705c121SKalle Valo  */
iwl_pcie_irq_handle_error(struct iwl_trans * trans)1677e705c121SKalle Valo static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
1678e705c121SKalle Valo {
1679e705c121SKalle Valo 	int i;
1680e705c121SKalle Valo 
1681e705c121SKalle Valo 	/* W/A for WiFi/WiMAX coex and WiMAX own the RF */
1682e705c121SKalle Valo 	if (trans->cfg->internal_wimax_coex &&
1683e705c121SKalle Valo 	    !trans->cfg->apmg_not_supported &&
1684e705c121SKalle Valo 	    (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
1685e705c121SKalle Valo 			     APMS_CLK_VAL_MRB_FUNC_MODE) ||
1686e705c121SKalle Valo 	     (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
1687e705c121SKalle Valo 			    APMG_PS_CTRL_VAL_RESET_REQ))) {
1688e705c121SKalle Valo 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1689e705c121SKalle Valo 		iwl_op_mode_wimax_active(trans->op_mode);
169013f028b4SMordechay Goodstein 		wake_up(&trans->wait_command_queue);
1691e705c121SKalle Valo 		return;
1692e705c121SKalle Valo 	}
1693e705c121SKalle Valo 
1694286ca8ebSLuca Coelho 	for (i = 0; i < trans->trans_cfg->base_params->num_of_queues; i++) {
16954f4822b7SMordechay Goodstein 		if (!trans->txqs.txq[i])
169613a3a390SSara Sharon 			continue;
16974f4822b7SMordechay Goodstein 		del_timer(&trans->txqs.txq[i]->stuck_timer);
169813a3a390SSara Sharon 	}
1699e705c121SKalle Valo 
17007d75f32eSEmmanuel Grumbach 	/* The STATUS_FW_ERROR bit is set in this function. This must happen
17017d75f32eSEmmanuel Grumbach 	 * before we wake up the command caller, to ensure a proper cleanup. */
1702b8221b0fSJohannes Berg 	iwl_trans_fw_error(trans, false);
17037d75f32eSEmmanuel Grumbach 
1704e705c121SKalle Valo 	clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
170513f028b4SMordechay Goodstein 	wake_up(&trans->wait_command_queue);
1706e705c121SKalle Valo }
1707e705c121SKalle Valo 
iwl_pcie_int_cause_non_ict(struct iwl_trans * trans)1708e705c121SKalle Valo static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
1709e705c121SKalle Valo {
1710e705c121SKalle Valo 	u32 inta;
1711e705c121SKalle Valo 
1712e705c121SKalle Valo 	lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
1713e705c121SKalle Valo 
1714e705c121SKalle Valo 	trace_iwlwifi_dev_irq(trans->dev);
1715e705c121SKalle Valo 
1716e705c121SKalle Valo 	/* Discover which interrupts are active/pending */
1717e705c121SKalle Valo 	inta = iwl_read32(trans, CSR_INT);
1718e705c121SKalle Valo 
1719e705c121SKalle Valo 	/* the thread will service interrupts and re-enable them */
1720e705c121SKalle Valo 	return inta;
1721e705c121SKalle Valo }
1722e705c121SKalle Valo 
1723e705c121SKalle Valo /* a device (PCI-E) page is 4096 bytes long */
1724e705c121SKalle Valo #define ICT_SHIFT	12
1725e705c121SKalle Valo #define ICT_SIZE	(1 << ICT_SHIFT)
1726e705c121SKalle Valo #define ICT_COUNT	(ICT_SIZE / sizeof(u32))
1727e705c121SKalle Valo 
1728e705c121SKalle Valo /* interrupt handler using ict table, with this interrupt driver will
1729e705c121SKalle Valo  * stop using INTA register to get device's interrupt, reading this register
1730e705c121SKalle Valo  * is expensive, device will write interrupts in ICT dram table, increment
1731e705c121SKalle Valo  * index then will fire interrupt to driver, driver will OR all ICT table
1732e705c121SKalle Valo  * entries from current index up to table entry with 0 value. the result is
1733e705c121SKalle Valo  * the interrupt we need to service, driver will set the entries back to 0 and
1734e705c121SKalle Valo  * set index.
1735e705c121SKalle Valo  */
iwl_pcie_int_cause_ict(struct iwl_trans * trans)1736e705c121SKalle Valo static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
1737e705c121SKalle Valo {
1738e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1739e705c121SKalle Valo 	u32 inta;
1740e705c121SKalle Valo 	u32 val = 0;
1741e705c121SKalle Valo 	u32 read;
1742e705c121SKalle Valo 
1743e705c121SKalle Valo 	trace_iwlwifi_dev_irq(trans->dev);
1744e705c121SKalle Valo 
1745e705c121SKalle Valo 	/* Ignore interrupt if there's nothing in NIC to service.
1746e705c121SKalle Valo 	 * This may be due to IRQ shared with another device,
1747e705c121SKalle Valo 	 * or due to sporadic interrupts thrown from our NIC. */
1748e705c121SKalle Valo 	read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1749e705c121SKalle Valo 	trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
1750e705c121SKalle Valo 	if (!read)
1751e705c121SKalle Valo 		return 0;
1752e705c121SKalle Valo 
1753e705c121SKalle Valo 	/*
1754e705c121SKalle Valo 	 * Collect all entries up to the first 0, starting from ict_index;
1755e705c121SKalle Valo 	 * note we already read at ict_index.
1756e705c121SKalle Valo 	 */
1757e705c121SKalle Valo 	do {
1758e705c121SKalle Valo 		val |= read;
1759e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
1760e705c121SKalle Valo 				trans_pcie->ict_index, read);
1761e705c121SKalle Valo 		trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1762e705c121SKalle Valo 		trans_pcie->ict_index =
1763e705c121SKalle Valo 			((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
1764e705c121SKalle Valo 
1765e705c121SKalle Valo 		read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1766e705c121SKalle Valo 		trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
1767e705c121SKalle Valo 					   read);
1768e705c121SKalle Valo 	} while (read);
1769e705c121SKalle Valo 
1770e705c121SKalle Valo 	/* We should not get this value, just ignore it. */
1771e705c121SKalle Valo 	if (val == 0xffffffff)
1772e705c121SKalle Valo 		val = 0;
1773e705c121SKalle Valo 
1774e705c121SKalle Valo 	/*
1775e705c121SKalle Valo 	 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1776e705c121SKalle Valo 	 * (bit 15 before shifting it to 31) to clear when using interrupt
1777e705c121SKalle Valo 	 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1778e705c121SKalle Valo 	 * so we use them to decide on the real state of the Rx bit.
1779e705c121SKalle Valo 	 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1780e705c121SKalle Valo 	 */
1781e705c121SKalle Valo 	if (val & 0xC0000)
1782e705c121SKalle Valo 		val |= 0x8000;
1783e705c121SKalle Valo 
1784e705c121SKalle Valo 	inta = (0xff & val) | ((0xff00 & val) << 16);
1785e705c121SKalle Valo 	return inta;
1786e705c121SKalle Valo }
1787e705c121SKalle Valo 
iwl_pcie_handle_rfkill_irq(struct iwl_trans * trans,bool from_irq)1788ad1220bbSJohannes Berg void iwl_pcie_handle_rfkill_irq(struct iwl_trans *trans, bool from_irq)
17893a6e168bSJohannes Berg {
17903a6e168bSJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
17913a6e168bSJohannes Berg 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
1792326477e4SJohannes Berg 	bool hw_rfkill, prev, report;
17933a6e168bSJohannes Berg 
17943a6e168bSJohannes Berg 	mutex_lock(&trans_pcie->mutex);
1795326477e4SJohannes Berg 	prev = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
17963a6e168bSJohannes Berg 	hw_rfkill = iwl_is_rfkill_set(trans);
1797326477e4SJohannes Berg 	if (hw_rfkill) {
1798326477e4SJohannes Berg 		set_bit(STATUS_RFKILL_OPMODE, &trans->status);
1799326477e4SJohannes Berg 		set_bit(STATUS_RFKILL_HW, &trans->status);
1800326477e4SJohannes Berg 	}
1801326477e4SJohannes Berg 	if (trans_pcie->opmode_down)
1802326477e4SJohannes Berg 		report = hw_rfkill;
1803326477e4SJohannes Berg 	else
1804326477e4SJohannes Berg 		report = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
18053a6e168bSJohannes Berg 
18063a6e168bSJohannes Berg 	IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
18073a6e168bSJohannes Berg 		 hw_rfkill ? "disable radio" : "enable radio");
18083a6e168bSJohannes Berg 
18093a6e168bSJohannes Berg 	isr_stats->rfkill++;
18103a6e168bSJohannes Berg 
1811326477e4SJohannes Berg 	if (prev != report)
1812ad1220bbSJohannes Berg 		iwl_trans_pcie_rf_kill(trans, report, from_irq);
18133a6e168bSJohannes Berg 	mutex_unlock(&trans_pcie->mutex);
18143a6e168bSJohannes Berg 
18153a6e168bSJohannes Berg 	if (hw_rfkill) {
18163a6e168bSJohannes Berg 		if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
18173a6e168bSJohannes Berg 				       &trans->status))
18183a6e168bSJohannes Berg 			IWL_DEBUG_RF_KILL(trans,
18193a6e168bSJohannes Berg 					  "Rfkill while SYNC HCMD in flight\n");
182013f028b4SMordechay Goodstein 		wake_up(&trans->wait_command_queue);
18213a6e168bSJohannes Berg 	} else {
1822326477e4SJohannes Berg 		clear_bit(STATUS_RFKILL_HW, &trans->status);
1823326477e4SJohannes Berg 		if (trans_pcie->opmode_down)
1824326477e4SJohannes Berg 			clear_bit(STATUS_RFKILL_OPMODE, &trans->status);
18253a6e168bSJohannes Berg 	}
18263a6e168bSJohannes Berg }
18273a6e168bSJohannes Berg 
iwl_pcie_irq_handler(int irq,void * dev_id)1828e705c121SKalle Valo irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
1829e705c121SKalle Valo {
1830e705c121SKalle Valo 	struct iwl_trans *trans = dev_id;
1831e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1832e705c121SKalle Valo 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
1833e705c121SKalle Valo 	u32 inta = 0;
1834e705c121SKalle Valo 	u32 handled = 0;
183525edc8f2SJohannes Berg 	bool polling = false;
1836e705c121SKalle Valo 
1837e705c121SKalle Valo 	lock_map_acquire(&trans->sync_cmd_lockdep_map);
1838e705c121SKalle Valo 
183925edc8f2SJohannes Berg 	spin_lock_bh(&trans_pcie->irq_lock);
1840e705c121SKalle Valo 
1841e705c121SKalle Valo 	/* dram interrupt table not set yet,
1842e705c121SKalle Valo 	 * use legacy interrupt.
1843e705c121SKalle Valo 	 */
1844e705c121SKalle Valo 	if (likely(trans_pcie->use_ict))
1845e705c121SKalle Valo 		inta = iwl_pcie_int_cause_ict(trans);
1846e705c121SKalle Valo 	else
1847e705c121SKalle Valo 		inta = iwl_pcie_int_cause_non_ict(trans);
1848e705c121SKalle Valo 
1849e705c121SKalle Valo 	if (iwl_have_debug_level(IWL_DL_ISR)) {
1850e705c121SKalle Valo 		IWL_DEBUG_ISR(trans,
1851e705c121SKalle Valo 			      "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
1852e705c121SKalle Valo 			      inta, trans_pcie->inta_mask,
1853e705c121SKalle Valo 			      iwl_read32(trans, CSR_INT_MASK),
1854e705c121SKalle Valo 			      iwl_read32(trans, CSR_FH_INT_STATUS));
1855e705c121SKalle Valo 		if (inta & (~trans_pcie->inta_mask))
1856e705c121SKalle Valo 			IWL_DEBUG_ISR(trans,
1857e705c121SKalle Valo 				      "We got a masked interrupt (0x%08x)\n",
1858e705c121SKalle Valo 				      inta & (~trans_pcie->inta_mask));
1859e705c121SKalle Valo 	}
1860e705c121SKalle Valo 
1861e705c121SKalle Valo 	inta &= trans_pcie->inta_mask;
1862e705c121SKalle Valo 
1863e705c121SKalle Valo 	/*
1864e705c121SKalle Valo 	 * Ignore interrupt if there's nothing in NIC to service.
1865e705c121SKalle Valo 	 * This may be due to IRQ shared with another device,
1866e705c121SKalle Valo 	 * or due to sporadic interrupts thrown from our NIC.
1867e705c121SKalle Valo 	 */
1868e705c121SKalle Valo 	if (unlikely(!inta)) {
1869e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
1870e705c121SKalle Valo 		/*
1871e705c121SKalle Valo 		 * Re-enable interrupts here since we don't
1872e705c121SKalle Valo 		 * have anything to service
1873e705c121SKalle Valo 		 */
1874e705c121SKalle Valo 		if (test_bit(STATUS_INT_ENABLED, &trans->status))
1875f16c3ebfSEmmanuel Grumbach 			_iwl_enable_interrupts(trans);
187625edc8f2SJohannes Berg 		spin_unlock_bh(&trans_pcie->irq_lock);
1877e705c121SKalle Valo 		lock_map_release(&trans->sync_cmd_lockdep_map);
1878e705c121SKalle Valo 		return IRQ_NONE;
1879e705c121SKalle Valo 	}
1880e705c121SKalle Valo 
1881d4f1a50cSJohannes Berg 	if (unlikely(inta == 0xFFFFFFFF || iwl_trans_is_hw_error_value(inta))) {
1882e705c121SKalle Valo 		/*
1883e705c121SKalle Valo 		 * Hardware disappeared. It might have
1884e705c121SKalle Valo 		 * already raised an interrupt.
1885e705c121SKalle Valo 		 */
1886e705c121SKalle Valo 		IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
188725edc8f2SJohannes Berg 		spin_unlock_bh(&trans_pcie->irq_lock);
1888e705c121SKalle Valo 		goto out;
1889e705c121SKalle Valo 	}
1890e705c121SKalle Valo 
1891e705c121SKalle Valo 	/* Ack/clear/reset pending uCode interrupts.
1892e705c121SKalle Valo 	 * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1893e705c121SKalle Valo 	 */
1894e705c121SKalle Valo 	/* There is a hardware bug in the interrupt mask function that some
1895e705c121SKalle Valo 	 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
1896e705c121SKalle Valo 	 * they are disabled in the CSR_INT_MASK register. Furthermore the
1897e705c121SKalle Valo 	 * ICT interrupt handling mechanism has another bug that might cause
1898e705c121SKalle Valo 	 * these unmasked interrupts fail to be detected. We workaround the
1899e705c121SKalle Valo 	 * hardware bugs here by ACKing all the possible interrupts so that
1900e705c121SKalle Valo 	 * interrupt coalescing can still be achieved.
1901e705c121SKalle Valo 	 */
1902e705c121SKalle Valo 	iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
1903e705c121SKalle Valo 
1904e705c121SKalle Valo 	if (iwl_have_debug_level(IWL_DL_ISR))
1905e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
1906e705c121SKalle Valo 			      inta, iwl_read32(trans, CSR_INT_MASK));
1907e705c121SKalle Valo 
190825edc8f2SJohannes Berg 	spin_unlock_bh(&trans_pcie->irq_lock);
1909e705c121SKalle Valo 
1910e705c121SKalle Valo 	/* Now service all interrupt bits discovered above. */
1911e705c121SKalle Valo 	if (inta & CSR_INT_BIT_HW_ERR) {
1912e705c121SKalle Valo 		IWL_ERR(trans, "Hardware error detected.  Restarting.\n");
1913e705c121SKalle Valo 
1914e705c121SKalle Valo 		/* Tell the device to stop sending interrupts */
1915e705c121SKalle Valo 		iwl_disable_interrupts(trans);
1916e705c121SKalle Valo 
1917e705c121SKalle Valo 		isr_stats->hw++;
1918e705c121SKalle Valo 		iwl_pcie_irq_handle_error(trans);
1919e705c121SKalle Valo 
1920e705c121SKalle Valo 		handled |= CSR_INT_BIT_HW_ERR;
1921e705c121SKalle Valo 
1922e705c121SKalle Valo 		goto out;
1923e705c121SKalle Valo 	}
1924e705c121SKalle Valo 
1925e705c121SKalle Valo 	/* NIC fires this, but we don't use it, redundant with WAKEUP */
1926e705c121SKalle Valo 	if (inta & CSR_INT_BIT_SCD) {
1927e705c121SKalle Valo 		IWL_DEBUG_ISR(trans,
1928e705c121SKalle Valo 			      "Scheduler finished to transmit the frame/frames.\n");
1929e705c121SKalle Valo 		isr_stats->sch++;
1930e705c121SKalle Valo 	}
1931e705c121SKalle Valo 
1932e705c121SKalle Valo 	/* Alive notification via Rx interrupt will do the real work */
1933e705c121SKalle Valo 	if (inta & CSR_INT_BIT_ALIVE) {
1934e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "Alive interrupt\n");
1935e705c121SKalle Valo 		isr_stats->alive++;
1936286ca8ebSLuca Coelho 		if (trans->trans_cfg->gen2) {
1937eda50cdeSSara Sharon 			/*
1938eda50cdeSSara Sharon 			 * We can restock, since firmware configured
1939eda50cdeSSara Sharon 			 * the RFH
1940eda50cdeSSara Sharon 			 */
1941eda50cdeSSara Sharon 			iwl_pcie_rxmq_restock(trans, trans_pcie->rxq);
1942eda50cdeSSara Sharon 		}
1943ed3e4c6dSEmmanuel Grumbach 
1944ed3e4c6dSEmmanuel Grumbach 		handled |= CSR_INT_BIT_ALIVE;
1945e705c121SKalle Valo 	}
1946e705c121SKalle Valo 
1947e705c121SKalle Valo 	/* Safely ignore these bits for debug checks below */
1948e705c121SKalle Valo 	inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1949e705c121SKalle Valo 
1950e705c121SKalle Valo 	/* HW RF KILL switch toggled */
1951e705c121SKalle Valo 	if (inta & CSR_INT_BIT_RF_KILL) {
1952ad1220bbSJohannes Berg 		iwl_pcie_handle_rfkill_irq(trans, true);
1953e705c121SKalle Valo 		handled |= CSR_INT_BIT_RF_KILL;
1954e705c121SKalle Valo 	}
1955e705c121SKalle Valo 
1956e705c121SKalle Valo 	/* Chip got too hot and stopped itself */
1957e705c121SKalle Valo 	if (inta & CSR_INT_BIT_CT_KILL) {
1958e705c121SKalle Valo 		IWL_ERR(trans, "Microcode CT kill error detected.\n");
1959e705c121SKalle Valo 		isr_stats->ctkill++;
1960e705c121SKalle Valo 		handled |= CSR_INT_BIT_CT_KILL;
1961e705c121SKalle Valo 	}
1962e705c121SKalle Valo 
1963e705c121SKalle Valo 	/* Error detected by uCode */
1964e705c121SKalle Valo 	if (inta & CSR_INT_BIT_SW_ERR) {
1965e705c121SKalle Valo 		IWL_ERR(trans, "Microcode SW error detected. "
1966e705c121SKalle Valo 			" Restarting 0x%X.\n", inta);
1967e705c121SKalle Valo 		isr_stats->sw++;
1968e705c121SKalle Valo 		iwl_pcie_irq_handle_error(trans);
1969e705c121SKalle Valo 		handled |= CSR_INT_BIT_SW_ERR;
1970e705c121SKalle Valo 	}
1971e705c121SKalle Valo 
1972e705c121SKalle Valo 	/* uCode wakes up after power-down sleep */
1973e705c121SKalle Valo 	if (inta & CSR_INT_BIT_WAKEUP) {
1974e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
1975e705c121SKalle Valo 		iwl_pcie_rxq_check_wrptr(trans);
1976e705c121SKalle Valo 		iwl_pcie_txq_check_wrptrs(trans);
1977e705c121SKalle Valo 
1978e705c121SKalle Valo 		isr_stats->wakeup++;
1979e705c121SKalle Valo 
1980e705c121SKalle Valo 		handled |= CSR_INT_BIT_WAKEUP;
1981e705c121SKalle Valo 	}
1982e705c121SKalle Valo 
1983e705c121SKalle Valo 	/* All uCode command responses, including Tx command responses,
1984e705c121SKalle Valo 	 * Rx "responses" (frame-received notification), and other
1985e705c121SKalle Valo 	 * notifications from uCode come through here*/
1986e705c121SKalle Valo 	if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
1987e705c121SKalle Valo 		    CSR_INT_BIT_RX_PERIODIC)) {
1988e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "Rx interrupt\n");
1989e705c121SKalle Valo 		if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1990e705c121SKalle Valo 			handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1991e705c121SKalle Valo 			iwl_write32(trans, CSR_FH_INT_STATUS,
1992e705c121SKalle Valo 					CSR_FH_INT_RX_MASK);
1993e705c121SKalle Valo 		}
1994e705c121SKalle Valo 		if (inta & CSR_INT_BIT_RX_PERIODIC) {
1995e705c121SKalle Valo 			handled |= CSR_INT_BIT_RX_PERIODIC;
1996e705c121SKalle Valo 			iwl_write32(trans,
1997e705c121SKalle Valo 				CSR_INT, CSR_INT_BIT_RX_PERIODIC);
1998e705c121SKalle Valo 		}
1999e705c121SKalle Valo 		/* Sending RX interrupt require many steps to be done in the
2000195a367eSXiang wangx 		 * device:
2001e705c121SKalle Valo 		 * 1- write interrupt to current index in ICT table.
2002e705c121SKalle Valo 		 * 2- dma RX frame.
2003e705c121SKalle Valo 		 * 3- update RX shared data to indicate last write index.
2004e705c121SKalle Valo 		 * 4- send interrupt.
2005e705c121SKalle Valo 		 * This could lead to RX race, driver could receive RX interrupt
2006e705c121SKalle Valo 		 * but the shared data changes does not reflect this;
2007e705c121SKalle Valo 		 * periodic interrupt will detect any dangling Rx activity.
2008e705c121SKalle Valo 		 */
2009e705c121SKalle Valo 
2010e705c121SKalle Valo 		/* Disable periodic interrupt; we use it as just a one-shot. */
2011e705c121SKalle Valo 		iwl_write8(trans, CSR_INT_PERIODIC_REG,
2012e705c121SKalle Valo 			    CSR_INT_PERIODIC_DIS);
2013e705c121SKalle Valo 
2014e705c121SKalle Valo 		/*
2015e705c121SKalle Valo 		 * Enable periodic interrupt in 8 msec only if we received
2016e705c121SKalle Valo 		 * real RX interrupt (instead of just periodic int), to catch
2017e705c121SKalle Valo 		 * any dangling Rx interrupt.  If it was just the periodic
2018e705c121SKalle Valo 		 * interrupt, there was no dangling Rx activity, and no need
2019e705c121SKalle Valo 		 * to extend the periodic interrupt; one-shot is enough.
2020e705c121SKalle Valo 		 */
2021e705c121SKalle Valo 		if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
2022e705c121SKalle Valo 			iwl_write8(trans, CSR_INT_PERIODIC_REG,
2023e705c121SKalle Valo 				   CSR_INT_PERIODIC_ENA);
2024e705c121SKalle Valo 
2025e705c121SKalle Valo 		isr_stats->rx++;
2026e705c121SKalle Valo 
2027e705c121SKalle Valo 		local_bh_disable();
202825edc8f2SJohannes Berg 		if (napi_schedule_prep(&trans_pcie->rxq[0].napi)) {
202925edc8f2SJohannes Berg 			polling = true;
203025edc8f2SJohannes Berg 			__napi_schedule(&trans_pcie->rxq[0].napi);
203125edc8f2SJohannes Berg 		}
2032e705c121SKalle Valo 		local_bh_enable();
2033e705c121SKalle Valo 	}
2034e705c121SKalle Valo 
2035e705c121SKalle Valo 	/* This "Tx" DMA channel is used only for loading uCode */
2036e705c121SKalle Valo 	if (inta & CSR_INT_BIT_FH_TX) {
2037e705c121SKalle Valo 		iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
2038e705c121SKalle Valo 		IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
2039e705c121SKalle Valo 		isr_stats->tx++;
2040e705c121SKalle Valo 		handled |= CSR_INT_BIT_FH_TX;
2041e705c121SKalle Valo 		/* Wake up uCode load routine, now that load is complete */
2042e705c121SKalle Valo 		trans_pcie->ucode_write_complete = true;
2043e705c121SKalle Valo 		wake_up(&trans_pcie->ucode_write_waitq);
2044c0941aceSMukesh Sisodiya 		/* Wake up IMR write routine, now that write to SRAM is complete */
2045c0941aceSMukesh Sisodiya 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2046c0941aceSMukesh Sisodiya 			trans_pcie->imr_status = IMR_D2S_COMPLETED;
2047c0941aceSMukesh Sisodiya 			wake_up(&trans_pcie->ucode_write_waitq);
2048c0941aceSMukesh Sisodiya 		}
2049e705c121SKalle Valo 	}
2050e705c121SKalle Valo 
2051e705c121SKalle Valo 	if (inta & ~handled) {
2052e705c121SKalle Valo 		IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
2053e705c121SKalle Valo 		isr_stats->unhandled++;
2054e705c121SKalle Valo 	}
2055e705c121SKalle Valo 
2056e705c121SKalle Valo 	if (inta & ~(trans_pcie->inta_mask)) {
2057e705c121SKalle Valo 		IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
2058e705c121SKalle Valo 			 inta & ~trans_pcie->inta_mask);
2059e705c121SKalle Valo 	}
2060e705c121SKalle Valo 
206125edc8f2SJohannes Berg 	if (!polling) {
206225edc8f2SJohannes Berg 		spin_lock_bh(&trans_pcie->irq_lock);
2063a6bd005fSEmmanuel Grumbach 		/* only Re-enable all interrupt if disabled by irq */
2064f16c3ebfSEmmanuel Grumbach 		if (test_bit(STATUS_INT_ENABLED, &trans->status))
2065f16c3ebfSEmmanuel Grumbach 			_iwl_enable_interrupts(trans);
2066f16c3ebfSEmmanuel Grumbach 		/* we are loading the firmware, enable FH_TX interrupt only */
2067f16c3ebfSEmmanuel Grumbach 		else if (handled & CSR_INT_BIT_FH_TX)
2068f16c3ebfSEmmanuel Grumbach 			iwl_enable_fw_load_int(trans);
2069e705c121SKalle Valo 		/* Re-enable RF_KILL if it occurred */
2070e705c121SKalle Valo 		else if (handled & CSR_INT_BIT_RF_KILL)
2071e705c121SKalle Valo 			iwl_enable_rfkill_int(trans);
2072ed3e4c6dSEmmanuel Grumbach 		/* Re-enable the ALIVE / Rx interrupt if it occurred */
2073ed3e4c6dSEmmanuel Grumbach 		else if (handled & (CSR_INT_BIT_ALIVE | CSR_INT_BIT_FH_RX))
2074ed3e4c6dSEmmanuel Grumbach 			iwl_enable_fw_load_int_ctx_info(trans);
207525edc8f2SJohannes Berg 		spin_unlock_bh(&trans_pcie->irq_lock);
207625edc8f2SJohannes Berg 	}
2077e705c121SKalle Valo 
2078e705c121SKalle Valo out:
2079e705c121SKalle Valo 	lock_map_release(&trans->sync_cmd_lockdep_map);
2080e705c121SKalle Valo 	return IRQ_HANDLED;
2081e705c121SKalle Valo }
2082e705c121SKalle Valo 
2083e705c121SKalle Valo /******************************************************************************
2084e705c121SKalle Valo  *
2085e705c121SKalle Valo  * ICT functions
2086e705c121SKalle Valo  *
2087e705c121SKalle Valo  ******************************************************************************/
2088e705c121SKalle Valo 
2089e705c121SKalle Valo /* Free dram table */
iwl_pcie_free_ict(struct iwl_trans * trans)2090e705c121SKalle Valo void iwl_pcie_free_ict(struct iwl_trans *trans)
2091e705c121SKalle Valo {
2092e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2093e705c121SKalle Valo 
2094e705c121SKalle Valo 	if (trans_pcie->ict_tbl) {
2095e705c121SKalle Valo 		dma_free_coherent(trans->dev, ICT_SIZE,
2096e705c121SKalle Valo 				  trans_pcie->ict_tbl,
2097e705c121SKalle Valo 				  trans_pcie->ict_tbl_dma);
2098e705c121SKalle Valo 		trans_pcie->ict_tbl = NULL;
2099e705c121SKalle Valo 		trans_pcie->ict_tbl_dma = 0;
2100e705c121SKalle Valo 	}
2101e705c121SKalle Valo }
2102e705c121SKalle Valo 
2103e705c121SKalle Valo /*
2104e705c121SKalle Valo  * allocate dram shared table, it is an aligned memory
2105e705c121SKalle Valo  * block of ICT_SIZE.
2106e705c121SKalle Valo  * also reset all data related to ICT table interrupt.
2107e705c121SKalle Valo  */
iwl_pcie_alloc_ict(struct iwl_trans * trans)2108e705c121SKalle Valo int iwl_pcie_alloc_ict(struct iwl_trans *trans)
2109e705c121SKalle Valo {
2110e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2111e705c121SKalle Valo 
2112e705c121SKalle Valo 	trans_pcie->ict_tbl =
2113750afb08SLuis Chamberlain 		dma_alloc_coherent(trans->dev, ICT_SIZE,
2114750afb08SLuis Chamberlain 				   &trans_pcie->ict_tbl_dma, GFP_KERNEL);
2115e705c121SKalle Valo 	if (!trans_pcie->ict_tbl)
2116e705c121SKalle Valo 		return -ENOMEM;
2117e705c121SKalle Valo 
2118e705c121SKalle Valo 	/* just an API sanity check ... it is guaranteed to be aligned */
2119e705c121SKalle Valo 	if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
2120e705c121SKalle Valo 		iwl_pcie_free_ict(trans);
2121e705c121SKalle Valo 		return -EINVAL;
2122e705c121SKalle Valo 	}
2123e705c121SKalle Valo 
2124e705c121SKalle Valo 	return 0;
2125e705c121SKalle Valo }
2126e705c121SKalle Valo 
2127e705c121SKalle Valo /* Device is going up inform it about using ICT interrupt table,
2128e705c121SKalle Valo  * also we need to tell the driver to start using ICT interrupt.
2129e705c121SKalle Valo  */
iwl_pcie_reset_ict(struct iwl_trans * trans)2130e705c121SKalle Valo void iwl_pcie_reset_ict(struct iwl_trans *trans)
2131e705c121SKalle Valo {
2132e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2133e705c121SKalle Valo 	u32 val;
2134e705c121SKalle Valo 
2135e705c121SKalle Valo 	if (!trans_pcie->ict_tbl)
2136e705c121SKalle Valo 		return;
2137e705c121SKalle Valo 
213825edc8f2SJohannes Berg 	spin_lock_bh(&trans_pcie->irq_lock);
2139f16c3ebfSEmmanuel Grumbach 	_iwl_disable_interrupts(trans);
2140e705c121SKalle Valo 
2141e705c121SKalle Valo 	memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
2142e705c121SKalle Valo 
2143e705c121SKalle Valo 	val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
2144e705c121SKalle Valo 
2145e705c121SKalle Valo 	val |= CSR_DRAM_INT_TBL_ENABLE |
2146e705c121SKalle Valo 	       CSR_DRAM_INIT_TBL_WRAP_CHECK |
2147e705c121SKalle Valo 	       CSR_DRAM_INIT_TBL_WRITE_POINTER;
2148e705c121SKalle Valo 
2149e705c121SKalle Valo 	IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
2150e705c121SKalle Valo 
2151e705c121SKalle Valo 	iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
2152e705c121SKalle Valo 	trans_pcie->use_ict = true;
2153e705c121SKalle Valo 	trans_pcie->ict_index = 0;
2154e705c121SKalle Valo 	iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
2155f16c3ebfSEmmanuel Grumbach 	_iwl_enable_interrupts(trans);
215625edc8f2SJohannes Berg 	spin_unlock_bh(&trans_pcie->irq_lock);
2157e705c121SKalle Valo }
2158e705c121SKalle Valo 
2159e705c121SKalle Valo /* Device is going down disable ict interrupt usage */
iwl_pcie_disable_ict(struct iwl_trans * trans)2160e705c121SKalle Valo void iwl_pcie_disable_ict(struct iwl_trans *trans)
2161e705c121SKalle Valo {
2162e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2163e705c121SKalle Valo 
216425edc8f2SJohannes Berg 	spin_lock_bh(&trans_pcie->irq_lock);
2165e705c121SKalle Valo 	trans_pcie->use_ict = false;
216625edc8f2SJohannes Berg 	spin_unlock_bh(&trans_pcie->irq_lock);
2167e705c121SKalle Valo }
2168e705c121SKalle Valo 
iwl_pcie_isr(int irq,void * data)2169e705c121SKalle Valo irqreturn_t iwl_pcie_isr(int irq, void *data)
2170e705c121SKalle Valo {
2171e705c121SKalle Valo 	struct iwl_trans *trans = data;
2172e705c121SKalle Valo 
2173e705c121SKalle Valo 	if (!trans)
2174e705c121SKalle Valo 		return IRQ_NONE;
2175e705c121SKalle Valo 
2176e705c121SKalle Valo 	/* Disable (but don't clear!) interrupts here to avoid
2177e705c121SKalle Valo 	 * back-to-back ISRs and sporadic interrupts from our NIC.
2178e705c121SKalle Valo 	 * If we have something to service, the tasklet will re-enable ints.
2179e705c121SKalle Valo 	 * If we *don't* have something, we'll re-enable before leaving here.
2180e705c121SKalle Valo 	 */
2181e705c121SKalle Valo 	iwl_write32(trans, CSR_INT_MASK, 0x00000000);
2182e705c121SKalle Valo 
2183e705c121SKalle Valo 	return IRQ_WAKE_THREAD;
2184e705c121SKalle Valo }
21852e5d4a8fSHaim Dreyfuss 
iwl_pcie_msix_isr(int irq,void * data)21862e5d4a8fSHaim Dreyfuss irqreturn_t iwl_pcie_msix_isr(int irq, void *data)
21872e5d4a8fSHaim Dreyfuss {
21882e5d4a8fSHaim Dreyfuss 	return IRQ_WAKE_THREAD;
21892e5d4a8fSHaim Dreyfuss }
21902e5d4a8fSHaim Dreyfuss 
iwl_pcie_irq_msix_handler(int irq,void * dev_id)21912e5d4a8fSHaim Dreyfuss irqreturn_t iwl_pcie_irq_msix_handler(int irq, void *dev_id)
21922e5d4a8fSHaim Dreyfuss {
21932e5d4a8fSHaim Dreyfuss 	struct msix_entry *entry = dev_id;
21942e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = iwl_pcie_get_trans_pcie(entry);
21952e5d4a8fSHaim Dreyfuss 	struct iwl_trans *trans = trans_pcie->trans;
219646167a8fSColin Ian King 	struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
2197d4626f91SMordechay Goodstein 	u32 inta_fh_msk = ~MSIX_FH_INT_CAUSES_DATA_QUEUE;
21982e5d4a8fSHaim Dreyfuss 	u32 inta_fh, inta_hw;
219925edc8f2SJohannes Berg 	bool polling = false;
2200571836a0SMike Golant 	bool sw_err;
22012e5d4a8fSHaim Dreyfuss 
2202d4626f91SMordechay Goodstein 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX)
2203d4626f91SMordechay Goodstein 		inta_fh_msk |= MSIX_FH_INT_CAUSES_Q0;
2204d4626f91SMordechay Goodstein 
2205d4626f91SMordechay Goodstein 	if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS)
2206d4626f91SMordechay Goodstein 		inta_fh_msk |= MSIX_FH_INT_CAUSES_Q1;
2207d4626f91SMordechay Goodstein 
22082e5d4a8fSHaim Dreyfuss 	lock_map_acquire(&trans->sync_cmd_lockdep_map);
22092e5d4a8fSHaim Dreyfuss 
221025edc8f2SJohannes Berg 	spin_lock_bh(&trans_pcie->irq_lock);
22117ef3dd26SHaim Dreyfuss 	inta_fh = iwl_read32(trans, CSR_MSIX_FH_INT_CAUSES_AD);
22127ef3dd26SHaim Dreyfuss 	inta_hw = iwl_read32(trans, CSR_MSIX_HW_INT_CAUSES_AD);
22132e5d4a8fSHaim Dreyfuss 	/*
22142e5d4a8fSHaim Dreyfuss 	 * Clear causes registers to avoid being handling the same cause.
22152e5d4a8fSHaim Dreyfuss 	 */
2216d4626f91SMordechay Goodstein 	iwl_write32(trans, CSR_MSIX_FH_INT_CAUSES_AD, inta_fh & inta_fh_msk);
22177ef3dd26SHaim Dreyfuss 	iwl_write32(trans, CSR_MSIX_HW_INT_CAUSES_AD, inta_hw);
221825edc8f2SJohannes Berg 	spin_unlock_bh(&trans_pcie->irq_lock);
22192e5d4a8fSHaim Dreyfuss 
2220c42ff65dSJohannes Berg 	trace_iwlwifi_dev_irq_msix(trans->dev, entry, true, inta_fh, inta_hw);
2221c42ff65dSJohannes Berg 
22222e5d4a8fSHaim Dreyfuss 	if (unlikely(!(inta_fh | inta_hw))) {
22232e5d4a8fSHaim Dreyfuss 		IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
22242e5d4a8fSHaim Dreyfuss 		lock_map_release(&trans->sync_cmd_lockdep_map);
22252e5d4a8fSHaim Dreyfuss 		return IRQ_NONE;
22262e5d4a8fSHaim Dreyfuss 	}
22272e5d4a8fSHaim Dreyfuss 
22283b57a10cSEmmanuel Grumbach 	if (iwl_have_debug_level(IWL_DL_ISR)) {
22293b57a10cSEmmanuel Grumbach 		IWL_DEBUG_ISR(trans,
22309d401222SMordechay Goodstein 			      "ISR[%d] inta_fh 0x%08x, enabled (sw) 0x%08x (hw) 0x%08x\n",
22319d401222SMordechay Goodstein 			      entry->entry, inta_fh, trans_pcie->fh_mask,
22322e5d4a8fSHaim Dreyfuss 			      iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD));
22333b57a10cSEmmanuel Grumbach 		if (inta_fh & ~trans_pcie->fh_mask)
22343b57a10cSEmmanuel Grumbach 			IWL_DEBUG_ISR(trans,
22353b57a10cSEmmanuel Grumbach 				      "We got a masked interrupt (0x%08x)\n",
22363b57a10cSEmmanuel Grumbach 				      inta_fh & ~trans_pcie->fh_mask);
22373b57a10cSEmmanuel Grumbach 	}
22383b57a10cSEmmanuel Grumbach 
22393b57a10cSEmmanuel Grumbach 	inta_fh &= trans_pcie->fh_mask;
22402e5d4a8fSHaim Dreyfuss 
2241496d83caSHaim Dreyfuss 	if ((trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX) &&
2242496d83caSHaim Dreyfuss 	    inta_fh & MSIX_FH_INT_CAUSES_Q0) {
2243496d83caSHaim Dreyfuss 		local_bh_disable();
224425edc8f2SJohannes Berg 		if (napi_schedule_prep(&trans_pcie->rxq[0].napi)) {
224525edc8f2SJohannes Berg 			polling = true;
224625edc8f2SJohannes Berg 			__napi_schedule(&trans_pcie->rxq[0].napi);
224725edc8f2SJohannes Berg 		}
2248496d83caSHaim Dreyfuss 		local_bh_enable();
2249496d83caSHaim Dreyfuss 	}
2250496d83caSHaim Dreyfuss 
2251496d83caSHaim Dreyfuss 	if ((trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS) &&
2252496d83caSHaim Dreyfuss 	    inta_fh & MSIX_FH_INT_CAUSES_Q1) {
2253496d83caSHaim Dreyfuss 		local_bh_disable();
225425edc8f2SJohannes Berg 		if (napi_schedule_prep(&trans_pcie->rxq[1].napi)) {
225525edc8f2SJohannes Berg 			polling = true;
225625edc8f2SJohannes Berg 			__napi_schedule(&trans_pcie->rxq[1].napi);
225725edc8f2SJohannes Berg 		}
2258496d83caSHaim Dreyfuss 		local_bh_enable();
2259496d83caSHaim Dreyfuss 	}
2260496d83caSHaim Dreyfuss 
22612e5d4a8fSHaim Dreyfuss 	/* This "Tx" DMA channel is used only for loading uCode */
2262c0941aceSMukesh Sisodiya 	if (inta_fh & MSIX_FH_INT_CAUSES_D2S_CH0_NUM &&
2263c0941aceSMukesh Sisodiya 	    trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2264c0941aceSMukesh Sisodiya 		IWL_DEBUG_ISR(trans, "IMR Complete interrupt\n");
2265c0941aceSMukesh Sisodiya 		isr_stats->tx++;
2266c0941aceSMukesh Sisodiya 
2267c0941aceSMukesh Sisodiya 		/* Wake up IMR routine once write to SRAM is complete */
2268c0941aceSMukesh Sisodiya 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2269c0941aceSMukesh Sisodiya 			trans_pcie->imr_status = IMR_D2S_COMPLETED;
2270c0941aceSMukesh Sisodiya 			wake_up(&trans_pcie->ucode_write_waitq);
2271c0941aceSMukesh Sisodiya 		}
2272c0941aceSMukesh Sisodiya 	} else if (inta_fh & MSIX_FH_INT_CAUSES_D2S_CH0_NUM) {
22732e5d4a8fSHaim Dreyfuss 		IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
22742e5d4a8fSHaim Dreyfuss 		isr_stats->tx++;
22752e5d4a8fSHaim Dreyfuss 		/*
22762e5d4a8fSHaim Dreyfuss 		 * Wake up uCode load routine,
22772e5d4a8fSHaim Dreyfuss 		 * now that load is complete
22782e5d4a8fSHaim Dreyfuss 		 */
22792e5d4a8fSHaim Dreyfuss 		trans_pcie->ucode_write_complete = true;
22802e5d4a8fSHaim Dreyfuss 		wake_up(&trans_pcie->ucode_write_waitq);
2281c0941aceSMukesh Sisodiya 
2282c0941aceSMukesh Sisodiya 		/* Wake up IMR routine once write to SRAM is complete */
2283c0941aceSMukesh Sisodiya 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2284c0941aceSMukesh Sisodiya 			trans_pcie->imr_status = IMR_D2S_COMPLETED;
2285c0941aceSMukesh Sisodiya 			wake_up(&trans_pcie->ucode_write_waitq);
2286c0941aceSMukesh Sisodiya 		}
22872e5d4a8fSHaim Dreyfuss 	}
22882e5d4a8fSHaim Dreyfuss 
2289571836a0SMike Golant 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
2290571836a0SMike Golant 		sw_err = inta_hw & MSIX_HW_INT_CAUSES_REG_SW_ERR_BZ;
2291571836a0SMike Golant 	else
2292571836a0SMike Golant 		sw_err = inta_hw & MSIX_HW_INT_CAUSES_REG_SW_ERR;
2293571836a0SMike Golant 
22942e5d4a8fSHaim Dreyfuss 	/* Error detected by uCode */
2295571836a0SMike Golant 	if ((inta_fh & MSIX_FH_INT_CAUSES_FH_ERR) || sw_err) {
22962e5d4a8fSHaim Dreyfuss 		IWL_ERR(trans,
22972e5d4a8fSHaim Dreyfuss 			"Microcode SW error detected. Restarting 0x%X.\n",
22982e5d4a8fSHaim Dreyfuss 			inta_fh);
22992e5d4a8fSHaim Dreyfuss 		isr_stats->sw++;
2300e63aafeaSJohannes Berg 		/* during FW reset flow report errors from there */
2301c0941aceSMukesh Sisodiya 		if (trans_pcie->imr_status == IMR_D2S_REQUESTED) {
2302c0941aceSMukesh Sisodiya 			trans_pcie->imr_status = IMR_D2S_ERROR;
2303c0941aceSMukesh Sisodiya 			wake_up(&trans_pcie->imr_waitq);
2304c0941aceSMukesh Sisodiya 		} else if (trans_pcie->fw_reset_state == FW_RESET_REQUESTED) {
2305e63aafeaSJohannes Berg 			trans_pcie->fw_reset_state = FW_RESET_ERROR;
2306e63aafeaSJohannes Berg 			wake_up(&trans_pcie->fw_reset_waitq);
2307e63aafeaSJohannes Berg 		} else {
23082e5d4a8fSHaim Dreyfuss 			iwl_pcie_irq_handle_error(trans);
23092e5d4a8fSHaim Dreyfuss 		}
2310e63aafeaSJohannes Berg 	}
23112e5d4a8fSHaim Dreyfuss 
23122e5d4a8fSHaim Dreyfuss 	/* After checking FH register check HW register */
23133b57a10cSEmmanuel Grumbach 	if (iwl_have_debug_level(IWL_DL_ISR)) {
23142e5d4a8fSHaim Dreyfuss 		IWL_DEBUG_ISR(trans,
23159d401222SMordechay Goodstein 			      "ISR[%d] inta_hw 0x%08x, enabled (sw) 0x%08x (hw) 0x%08x\n",
23169d401222SMordechay Goodstein 			      entry->entry, inta_hw, trans_pcie->hw_mask,
23172e5d4a8fSHaim Dreyfuss 			      iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD));
23183b57a10cSEmmanuel Grumbach 		if (inta_hw & ~trans_pcie->hw_mask)
23193b57a10cSEmmanuel Grumbach 			IWL_DEBUG_ISR(trans,
23203b57a10cSEmmanuel Grumbach 				      "We got a masked interrupt 0x%08x\n",
23213b57a10cSEmmanuel Grumbach 				      inta_hw & ~trans_pcie->hw_mask);
23223b57a10cSEmmanuel Grumbach 	}
23233b57a10cSEmmanuel Grumbach 
23243b57a10cSEmmanuel Grumbach 	inta_hw &= trans_pcie->hw_mask;
23252e5d4a8fSHaim Dreyfuss 
23262e5d4a8fSHaim Dreyfuss 	/* Alive notification via Rx interrupt will do the real work */
23272e5d4a8fSHaim Dreyfuss 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_ALIVE) {
23282e5d4a8fSHaim Dreyfuss 		IWL_DEBUG_ISR(trans, "Alive interrupt\n");
23292e5d4a8fSHaim Dreyfuss 		isr_stats->alive++;
2330286ca8ebSLuca Coelho 		if (trans->trans_cfg->gen2) {
2331eda50cdeSSara Sharon 			/* We can restock, since firmware configured the RFH */
2332eda50cdeSSara Sharon 			iwl_pcie_rxmq_restock(trans, trans_pcie->rxq);
2333eda50cdeSSara Sharon 		}
23342e5d4a8fSHaim Dreyfuss 	}
23352e5d4a8fSHaim Dreyfuss 
2336459fc0f2SLuca Coelho 	/*
2337459fc0f2SLuca Coelho 	 * In some rare cases when the HW is in a bad state, we may
2338459fc0f2SLuca Coelho 	 * get this interrupt too early, when prph_info is still NULL.
2339459fc0f2SLuca Coelho 	 * So make sure that it's not NULL to prevent crashing.
2340459fc0f2SLuca Coelho 	 */
2341459fc0f2SLuca Coelho 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_WAKEUP && trans_pcie->prph_info) {
2342e5f3f215SHaim Dreyfuss 		u32 sleep_notif =
2343e5f3f215SHaim Dreyfuss 			le32_to_cpu(trans_pcie->prph_info->sleep_notif);
2344e5f3f215SHaim Dreyfuss 		if (sleep_notif == IWL_D3_SLEEP_STATUS_SUSPEND ||
2345e5f3f215SHaim Dreyfuss 		    sleep_notif == IWL_D3_SLEEP_STATUS_RESUME) {
2346e5f3f215SHaim Dreyfuss 			IWL_DEBUG_ISR(trans,
2347e5f3f215SHaim Dreyfuss 				      "Sx interrupt: sleep notification = 0x%x\n",
2348e5f3f215SHaim Dreyfuss 				      sleep_notif);
2349e5f3f215SHaim Dreyfuss 			trans_pcie->sx_complete = true;
2350e5f3f215SHaim Dreyfuss 			wake_up(&trans_pcie->sx_waitq);
2351e5f3f215SHaim Dreyfuss 		} else {
23522e5d4a8fSHaim Dreyfuss 			/* uCode wakes up after power-down sleep */
23532e5d4a8fSHaim Dreyfuss 			IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
23542e5d4a8fSHaim Dreyfuss 			iwl_pcie_rxq_check_wrptr(trans);
23552e5d4a8fSHaim Dreyfuss 			iwl_pcie_txq_check_wrptrs(trans);
23562e5d4a8fSHaim Dreyfuss 
23572e5d4a8fSHaim Dreyfuss 			isr_stats->wakeup++;
23582e5d4a8fSHaim Dreyfuss 		}
2359e5f3f215SHaim Dreyfuss 	}
23602e5d4a8fSHaim Dreyfuss 
23612e5d4a8fSHaim Dreyfuss 	/* Chip got too hot and stopped itself */
23622e5d4a8fSHaim Dreyfuss 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_CT_KILL) {
23632e5d4a8fSHaim Dreyfuss 		IWL_ERR(trans, "Microcode CT kill error detected.\n");
23642e5d4a8fSHaim Dreyfuss 		isr_stats->ctkill++;
23652e5d4a8fSHaim Dreyfuss 	}
23662e5d4a8fSHaim Dreyfuss 
23672e5d4a8fSHaim Dreyfuss 	/* HW RF KILL switch toggled */
23683a6e168bSJohannes Berg 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_RF_KILL)
2369ad1220bbSJohannes Berg 		iwl_pcie_handle_rfkill_irq(trans, true);
23702e5d4a8fSHaim Dreyfuss 
23712e5d4a8fSHaim Dreyfuss 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_HW_ERR) {
23722e5d4a8fSHaim Dreyfuss 		IWL_ERR(trans,
23732e5d4a8fSHaim Dreyfuss 			"Hardware error detected. Restarting.\n");
23742e5d4a8fSHaim Dreyfuss 
23752e5d4a8fSHaim Dreyfuss 		isr_stats->hw++;
237691c28b83SShahar S Matityahu 		trans->dbg.hw_error = true;
23772e5d4a8fSHaim Dreyfuss 		iwl_pcie_irq_handle_error(trans);
23782e5d4a8fSHaim Dreyfuss 	}
23792e5d4a8fSHaim Dreyfuss 
2380906d4eb8SJohannes Berg 	if (inta_hw & MSIX_HW_INT_CAUSES_REG_RESET_DONE) {
2381906d4eb8SJohannes Berg 		IWL_DEBUG_ISR(trans, "Reset flow completed\n");
2382e63aafeaSJohannes Berg 		trans_pcie->fw_reset_state = FW_RESET_OK;
2383906d4eb8SJohannes Berg 		wake_up(&trans_pcie->fw_reset_waitq);
2384906d4eb8SJohannes Berg 	}
2385906d4eb8SJohannes Berg 
238625edc8f2SJohannes Berg 	if (!polling)
238725edc8f2SJohannes Berg 		iwl_pcie_clear_irq(trans, entry->entry);
23882e5d4a8fSHaim Dreyfuss 
23892e5d4a8fSHaim Dreyfuss 	lock_map_release(&trans->sync_cmd_lockdep_map);
23902e5d4a8fSHaim Dreyfuss 
23912e5d4a8fSHaim Dreyfuss 	return IRQ_HANDLED;
23922e5d4a8fSHaim Dreyfuss }
2393