blob: a385f3cddb5dab9122758ec18b1ecccd1d43072a [file] [log] [blame]
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
Sara Sharon26d535a2015-04-28 12:56:54 +03004 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07005 *
6 * Portions of this file are derived from the ipw3945 project, as well
7 * as portions of the ieee80211 subsystem header files.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 *
22 * The full GNU General Public License is included in this distribution in the
23 * file called LICENSE.
24 *
25 * Contact Information:
Emmanuel Grumbachd01c5362015-11-17 15:39:56 +020026 * Intel Linux Wireless <linuxwifi@intel.com>
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070027 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 *****************************************************************************/
30#include <linux/sched.h>
31#include <linux/wait.h>
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -070032#include <linux/gfp.h>
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070033
Johannes Berg1b29dc92012-03-06 13:30:50 -080034#include "iwl-prph.h"
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070035#include "iwl-io.h"
Johannes Berg6468a012012-05-16 19:13:54 +020036#include "internal.h"
Emmanuel Grumbachdb70f292012-02-09 16:08:15 +020037#include "iwl-op-mode.h"
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070038
39/******************************************************************************
40 *
41 * RX path functions
42 *
43 ******************************************************************************/
44
45/*
46 * Rx theory of operation
47 *
48 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
49 * each of which point to Receive Buffers to be filled by the NIC. These get
50 * used not only for Rx frames, but for any command response or notification
51 * from the NIC. The driver and NIC manage the Rx buffers by means
52 * of indexes into the circular buffer.
53 *
54 * Rx Queue Indexes
55 * The host/firmware share two index registers for managing the Rx buffers.
56 *
57 * The READ index maps to the first position that the firmware may be writing
58 * to -- the driver can read up to (but not including) this position and get
59 * good data.
60 * The READ index is managed by the firmware once the card is enabled.
61 *
62 * The WRITE index maps to the last position the driver has read from -- the
63 * position preceding WRITE is the last slot the firmware can place a packet.
64 *
65 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
66 * WRITE = READ.
67 *
68 * During initialization, the host sets up the READ queue position to the first
69 * INDEX position, and WRITE to the last (READ - 1 wrapped)
70 *
71 * When the firmware places a packet in a buffer, it will advance the READ index
72 * and fire the RX interrupt. The driver can then query the READ index and
73 * process as many packets as possible, moving the WRITE index forward as it
74 * resets the Rx queue buffers with new memory.
75 *
76 * The management in the driver is as follows:
Sara Sharon26d535a2015-04-28 12:56:54 +030077 * + A list of pre-allocated RBDs is stored in iwl->rxq->rx_free.
78 * When the interrupt handler is called, the request is processed.
79 * The page is either stolen - transferred to the upper layer
80 * or reused - added immediately to the iwl->rxq->rx_free list.
81 * + When the page is stolen - the driver updates the matching queue's used
82 * count, detaches the RBD and transfers it to the queue used list.
83 * When there are two used RBDs - they are transferred to the allocator empty
84 * list. Work is then scheduled for the allocator to start allocating
85 * eight buffers.
86 * When there are another 6 used RBDs - they are transferred to the allocator
87 * empty list and the driver tries to claim the pre-allocated buffers and
88 * add them to iwl->rxq->rx_free. If it fails - it continues to claim them
89 * until ready.
90 * When there are 8+ buffers in the free list - either from allocation or from
91 * 8 reused unstolen pages - restock is called to update the FW and indexes.
92 * + In order to make sure the allocator always has RBDs to use for allocation
93 * the allocator has initial pool in the size of num_queues*(8-2) - the
94 * maximum missing RBDs per allocation request (request posted with 2
95 * empty RBDs, there is no guarantee when the other 6 RBDs are supplied).
96 * The queues supplies the recycle of the rest of the RBDs.
Emmanuel Grumbachab697a92011-07-11 07:35:34 -070097 * + A received packet is processed and handed to the kernel network stack,
98 * detached from the iwl->rxq. The driver 'processed' index is updated.
Sara Sharon26d535a2015-04-28 12:56:54 +030099 * + If there are no allocated buffers in iwl->rxq->rx_free,
Johannes Berg2bfb5092012-12-27 21:43:48 +0100100 * the READ INDEX is not incremented and iwl->status(RX_STALLED) is set.
101 * If there were enough free buffers and RX_STALLED is set it is cleared.
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700102 *
103 *
104 * Driver sequence:
105 *
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200106 * iwl_rxq_alloc() Allocates rx_free
107 * iwl_pcie_rx_replenish() Replenishes rx_free list from rx_used, and calls
Sara Sharon26d535a2015-04-28 12:56:54 +0300108 * iwl_pcie_rxq_restock.
109 * Used only during initialization.
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200110 * iwl_pcie_rxq_restock() Moves available buffers from rx_free into Rx
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700111 * queue, updates firmware pointers, and updates
Sara Sharon26d535a2015-04-28 12:56:54 +0300112 * the WRITE index.
113 * iwl_pcie_rx_allocator() Background work for allocating pages.
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700114 *
115 * -- enable interrupts --
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200116 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700117 * READ INDEX, detaching the SKB from the pool.
118 * Moves the packet buffer from queue to rx_used.
Sara Sharon26d535a2015-04-28 12:56:54 +0300119 * Posts and claims requests to the allocator.
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200120 * Calls iwl_pcie_rxq_restock to refill any empty
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700121 * slots.
Sara Sharon26d535a2015-04-28 12:56:54 +0300122 *
123 * RBD life-cycle:
124 *
125 * Init:
126 * rxq.pool -> rxq.rx_used -> rxq.rx_free -> rxq.queue
127 *
128 * Regular Receive interrupt:
129 * Page Stolen:
130 * rxq.queue -> rxq.rx_used -> allocator.rbd_empty ->
131 * allocator.rbd_allocated -> rxq.rx_free -> rxq.queue
132 * Page not Stolen:
133 * rxq.queue -> rxq.rx_free -> rxq.queue
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700134 * ...
135 *
136 */
137
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200138/*
139 * iwl_rxq_space - Return number of free slots available in queue.
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700140 */
Johannes Bergfecba092013-06-20 21:56:49 +0200141static int iwl_rxq_space(const struct iwl_rxq *rxq)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700142{
Sara Sharon96a64972015-12-23 15:10:03 +0200143 /* Make sure rx queue size is a power of 2 */
144 WARN_ON(rxq->queue_size & (rxq->queue_size - 1));
Johannes Bergfecba092013-06-20 21:56:49 +0200145
Ido Yariv351746c2013-07-15 12:41:27 -0400146 /*
147 * There can be up to (RX_QUEUE_SIZE - 1) free slots, to avoid ambiguity
148 * between empty and completely full queues.
149 * The following is equivalent to modulo by RX_QUEUE_SIZE and is well
150 * defined for negative dividends.
151 */
Sara Sharon96a64972015-12-23 15:10:03 +0200152 return (rxq->read - rxq->write - 1) & (rxq->queue_size - 1);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700153}
154
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200155/*
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200156 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700157 */
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200158static inline __le32 iwl_pcie_dma_addr2rbd_ptr(dma_addr_t dma_addr)
159{
160 return cpu_to_le32((u32)(dma_addr >> 8));
161}
162
Sara Sharon96a64972015-12-23 15:10:03 +0200163static void iwl_pcie_write_prph_64(struct iwl_trans *trans, u64 ofs, u64 val)
164{
165 iwl_write_prph(trans, ofs, val & 0xffffffff);
166 iwl_write_prph(trans, ofs + 4, val >> 32);
167}
168
Emmanuel Grumbach49bd072d2012-11-18 13:14:51 +0200169/*
170 * iwl_pcie_rx_stop - stops the Rx DMA
171 */
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200172int iwl_pcie_rx_stop(struct iwl_trans *trans)
173{
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200174 iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
175 return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
176 FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
177}
178
179/*
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200180 * iwl_pcie_rxq_inc_wr_ptr - Update the write pointer for the RX queue
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700181 */
Sara Sharon78485052015-12-14 17:44:11 +0200182static void iwl_pcie_rxq_inc_wr_ptr(struct iwl_trans *trans,
183 struct iwl_rxq *rxq)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700184{
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700185 u32 reg;
186
Johannes Berg5d63f922014-02-27 11:20:07 +0100187 lockdep_assert_held(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700188
Eliad Peller50453882014-02-05 19:12:24 +0200189 /*
190 * explicitly wake up the NIC if:
191 * 1. shadow registers aren't enabled
192 * 2. there is a chance that the NIC is asleep
193 */
194 if (!trans->cfg->base_params->shadow_reg_enable &&
195 test_bit(STATUS_TPOWER_PMI, &trans->status)) {
196 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700197
Eliad Peller50453882014-02-05 19:12:24 +0200198 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
199 IWL_DEBUG_INFO(trans, "Rx queue requesting wakeup, GP1 = 0x%x\n",
200 reg);
201 iwl_set_bit(trans, CSR_GP_CNTRL,
202 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Johannes Berg5d63f922014-02-27 11:20:07 +0100203 rxq->need_update = true;
204 return;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700205 }
206 }
Eliad Peller50453882014-02-05 19:12:24 +0200207
208 rxq->write_actual = round_down(rxq->write, 8);
Sara Sharon96a64972015-12-23 15:10:03 +0200209 if (trans->cfg->mq_rx_supported)
210 iwl_write_prph(trans, RFH_Q_FRBDCB_WIDX(rxq->id),
211 rxq->write_actual);
212 else
213 iwl_write32(trans, FH_RSCSR_CHNL0_WPTR, rxq->write_actual);
Johannes Berg5d63f922014-02-27 11:20:07 +0100214}
215
216static void iwl_pcie_rxq_check_wrptr(struct iwl_trans *trans)
217{
218 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon78485052015-12-14 17:44:11 +0200219 int i;
Johannes Berg5d63f922014-02-27 11:20:07 +0100220
Sara Sharon78485052015-12-14 17:44:11 +0200221 for (i = 0; i < trans->num_rx_queues; i++) {
222 struct iwl_rxq *rxq = &trans_pcie->rxq[i];
Johannes Berg5d63f922014-02-27 11:20:07 +0100223
Sara Sharon78485052015-12-14 17:44:11 +0200224 if (!rxq->need_update)
225 continue;
226 spin_lock(&rxq->lock);
227 iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
228 rxq->need_update = false;
229 spin_unlock(&rxq->lock);
230 }
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700231}
232
Sara Sharon96a64972015-12-23 15:10:03 +0200233static void iwl_pcie_rxq_mq_restock(struct iwl_trans *trans,
234 struct iwl_rxq *rxq)
235{
236 struct iwl_rx_mem_buffer *rxb;
237
238 /*
239 * If the device isn't enabled - no need to try to add buffers...
240 * This can happen when we stop the device and still have an interrupt
241 * pending. We stop the APM before we sync the interrupts because we
242 * have to (see comment there). On the other hand, since the APM is
243 * stopped, we cannot access the HW (in particular not prph).
244 * So don't try to restock if the APM has been already stopped.
245 */
246 if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
247 return;
248
249 spin_lock(&rxq->lock);
250 while (rxq->free_count) {
251 __le64 *bd = (__le64 *)rxq->bd;
252
253 /* Get next free Rx buffer, remove from free list */
254 rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
255 list);
256 list_del(&rxb->list);
257
258 /* 12 first bits are expected to be empty */
259 WARN_ON(rxb->page_dma & DMA_BIT_MASK(12));
260 /* Point to Rx buffer via next RBD in circular buffer */
261 bd[rxq->write] = cpu_to_le64(rxb->page_dma | rxb->vid);
262 rxq->write = (rxq->write + 1) & MQ_RX_TABLE_MASK;
263 rxq->free_count--;
264 }
265 spin_unlock(&rxq->lock);
266
267 /*
268 * If we've added more space for the firmware to place data, tell it.
269 * Increment device's write pointer in multiples of 8.
270 */
271 if (rxq->write_actual != (rxq->write & ~0x7)) {
272 spin_lock(&rxq->lock);
273 iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
274 spin_unlock(&rxq->lock);
275 }
276}
277
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200278/*
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200279 * iwl_pcie_rxq_restock - refill RX queue from pre-allocated pool
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700280 *
281 * If there are slots in the RX queue that need to be restocked,
282 * and we have free pre-allocated buffers, fill the ranks as much
283 * as we can, pulling from rx_free.
284 *
285 * This moves the 'write' index forward to catch up with 'processed', and
286 * also updates the memory address in the firmware to reference the new
287 * target buffer.
288 */
Sara Sharon78485052015-12-14 17:44:11 +0200289static void iwl_pcie_rxq_restock(struct iwl_trans *trans, struct iwl_rxq *rxq)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700290{
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700291 struct iwl_rx_mem_buffer *rxb;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700292
Emmanuel Grumbach74390462012-09-09 16:58:07 +0300293 /*
294 * If the device isn't enabled - not need to try to add buffers...
295 * This can happen when we stop the device and still have an interrupt
Johannes Berg2bfb5092012-12-27 21:43:48 +0100296 * pending. We stop the APM before we sync the interrupts because we
297 * have to (see comment there). On the other hand, since the APM is
298 * stopped, we cannot access the HW (in particular not prph).
Emmanuel Grumbach74390462012-09-09 16:58:07 +0300299 * So don't try to restock if the APM has been already stopped.
300 */
Arik Nemtsoveb7ff772013-12-01 12:30:38 +0200301 if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
Emmanuel Grumbach74390462012-09-09 16:58:07 +0300302 return;
303
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200304 spin_lock(&rxq->lock);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200305 while ((iwl_rxq_space(rxq) > 0) && (rxq->free_count)) {
Sara Sharon96a64972015-12-23 15:10:03 +0200306 __le32 *bd = (__le32 *)rxq->bd;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700307 /* The overwritten rxb must be a used one */
308 rxb = rxq->queue[rxq->write];
309 BUG_ON(rxb && rxb->page);
310
311 /* Get next free Rx buffer, remove from free list */
Johannes Berge2b19302012-11-04 09:31:25 +0100312 rxb = list_first_entry(&rxq->rx_free, struct iwl_rx_mem_buffer,
313 list);
314 list_del(&rxb->list);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700315
316 /* Point to Rx buffer via next RBD in circular buffer */
Sara Sharon96a64972015-12-23 15:10:03 +0200317 bd[rxq->write] = iwl_pcie_dma_addr2rbd_ptr(rxb->page_dma);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700318 rxq->queue[rxq->write] = rxb;
319 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
320 rxq->free_count--;
321 }
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200322 spin_unlock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700323
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700324 /* If we've added more space for the firmware to place data, tell it.
325 * Increment device's write pointer in multiples of 8. */
326 if (rxq->write_actual != (rxq->write & ~0x7)) {
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200327 spin_lock(&rxq->lock);
Sara Sharon78485052015-12-14 17:44:11 +0200328 iwl_pcie_rxq_inc_wr_ptr(trans, rxq);
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200329 spin_unlock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700330 }
331}
332
Emmanuel Grumbach358a46d2012-09-09 16:39:18 +0300333/*
Sara Sharon26d535a2015-04-28 12:56:54 +0300334 * iwl_pcie_rx_alloc_page - allocates and returns a page.
335 *
336 */
337static struct page *iwl_pcie_rx_alloc_page(struct iwl_trans *trans,
338 gfp_t priority)
339{
340 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon26d535a2015-04-28 12:56:54 +0300341 struct page *page;
342 gfp_t gfp_mask = priority;
343
Sara Sharon26d535a2015-04-28 12:56:54 +0300344 if (trans_pcie->rx_page_order > 0)
345 gfp_mask |= __GFP_COMP;
346
347 /* Alloc a new receive buffer */
348 page = alloc_pages(gfp_mask, trans_pcie->rx_page_order);
349 if (!page) {
350 if (net_ratelimit())
351 IWL_DEBUG_INFO(trans, "alloc_pages failed, order: %d\n",
352 trans_pcie->rx_page_order);
Sara Sharon78485052015-12-14 17:44:11 +0200353 /*
354 * Issue an error if we don't have enough pre-allocated
355 * buffers.
Sara Sharon26d535a2015-04-28 12:56:54 +0300356` */
Sara Sharon78485052015-12-14 17:44:11 +0200357 if (!(gfp_mask & __GFP_NOWARN) && net_ratelimit())
Sara Sharon26d535a2015-04-28 12:56:54 +0300358 IWL_CRIT(trans,
Sara Sharon78485052015-12-14 17:44:11 +0200359 "Failed to alloc_pages\n");
Sara Sharon26d535a2015-04-28 12:56:54 +0300360 return NULL;
361 }
362 return page;
363}
364
365/*
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200366 * iwl_pcie_rxq_alloc_rbs - allocate a page for each used RBD
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700367 *
Emmanuel Grumbach358a46d2012-09-09 16:39:18 +0300368 * A used RBD is an Rx buffer that has been given to the stack. To use it again
369 * a page must be allocated and the RBD must point to the page. This function
370 * doesn't change the HW pointer but handles the list of pages that is used by
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200371 * iwl_pcie_rxq_restock. The latter function will update the HW to use the newly
Emmanuel Grumbach358a46d2012-09-09 16:39:18 +0300372 * allocated buffers.
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700373 */
Sara Sharon78485052015-12-14 17:44:11 +0200374static void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,
375 struct iwl_rxq *rxq)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700376{
Johannes Berg20d3b642012-05-16 22:54:29 +0200377 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700378 struct iwl_rx_mem_buffer *rxb;
379 struct page *page;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700380
381 while (1) {
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200382 spin_lock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700383 if (list_empty(&rxq->rx_used)) {
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200384 spin_unlock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700385 return;
386 }
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200387 spin_unlock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700388
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700389 /* Alloc a new receive buffer */
Sara Sharon26d535a2015-04-28 12:56:54 +0300390 page = iwl_pcie_rx_alloc_page(trans, priority);
391 if (!page)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700392 return;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700393
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200394 spin_lock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700395
396 if (list_empty(&rxq->rx_used)) {
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200397 spin_unlock(&rxq->lock);
Johannes Bergb2cf4102012-04-09 17:46:51 -0700398 __free_pages(page, trans_pcie->rx_page_order);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700399 return;
400 }
Johannes Berge2b19302012-11-04 09:31:25 +0100401 rxb = list_first_entry(&rxq->rx_used, struct iwl_rx_mem_buffer,
402 list);
403 list_del(&rxb->list);
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200404 spin_unlock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700405
406 BUG_ON(rxb->page);
407 rxb->page = page;
408 /* Get physical address of the RB */
Johannes Berg20d3b642012-05-16 22:54:29 +0200409 rxb->page_dma =
410 dma_map_page(trans->dev, page, 0,
411 PAGE_SIZE << trans_pcie->rx_page_order,
412 DMA_FROM_DEVICE);
Johannes Berg7c3415822012-11-04 09:29:17 +0100413 if (dma_mapping_error(trans->dev, rxb->page_dma)) {
414 rxb->page = NULL;
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200415 spin_lock(&rxq->lock);
Johannes Berg7c3415822012-11-04 09:29:17 +0100416 list_add(&rxb->list, &rxq->rx_used);
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200417 spin_unlock(&rxq->lock);
Johannes Berg7c3415822012-11-04 09:29:17 +0100418 __free_pages(page, trans_pcie->rx_page_order);
419 return;
420 }
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700421
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200422 spin_lock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700423
424 list_add_tail(&rxb->list, &rxq->rx_free);
425 rxq->free_count++;
426
Emmanuel Grumbach51232f72013-12-11 10:22:28 +0200427 spin_unlock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700428 }
429}
430
Sara Sharon78485052015-12-14 17:44:11 +0200431static void iwl_pcie_free_rbs_pool(struct iwl_trans *trans)
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200432{
433 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200434 int i;
435
Sara Sharon96a64972015-12-23 15:10:03 +0200436 for (i = 0; i < MQ_RX_POOL_SIZE; i++) {
Sara Sharon78485052015-12-14 17:44:11 +0200437 if (!trans_pcie->rx_pool[i].page)
Johannes Bergc7df1f42013-06-20 20:59:34 +0200438 continue;
Sara Sharon78485052015-12-14 17:44:11 +0200439 dma_unmap_page(trans->dev, trans_pcie->rx_pool[i].page_dma,
Johannes Bergc7df1f42013-06-20 20:59:34 +0200440 PAGE_SIZE << trans_pcie->rx_page_order,
441 DMA_FROM_DEVICE);
Sara Sharon78485052015-12-14 17:44:11 +0200442 __free_pages(trans_pcie->rx_pool[i].page,
443 trans_pcie->rx_page_order);
444 trans_pcie->rx_pool[i].page = NULL;
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200445 }
446}
447
Emmanuel Grumbach358a46d2012-09-09 16:39:18 +0300448/*
Sara Sharon26d535a2015-04-28 12:56:54 +0300449 * iwl_pcie_rx_allocator - Allocates pages in the background for RX queues
450 *
451 * Allocates for each received request 8 pages
452 * Called as a scheduled work item.
453 */
454static void iwl_pcie_rx_allocator(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700455{
Sara Sharon26d535a2015-04-28 12:56:54 +0300456 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
457 struct iwl_rb_allocator *rba = &trans_pcie->rba;
458 struct list_head local_empty;
459 int pending = atomic_xchg(&rba->req_pending, 0);
Sara Sharon5f175702015-04-28 12:56:54 +0300460
Sara Sharon26d535a2015-04-28 12:56:54 +0300461 IWL_DEBUG_RX(trans, "Pending allocation requests = %d\n", pending);
462
463 /* If we were scheduled - there is at least one request */
464 spin_lock(&rba->lock);
465 /* swap out the rba->rbd_empty to a local list */
466 list_replace_init(&rba->rbd_empty, &local_empty);
467 spin_unlock(&rba->lock);
468
469 while (pending) {
470 int i;
471 struct list_head local_allocated;
Sara Sharon78485052015-12-14 17:44:11 +0200472 gfp_t gfp_mask = GFP_KERNEL;
473
474 /* Do not post a warning if there are only a few requests */
475 if (pending < RX_PENDING_WATERMARK)
476 gfp_mask |= __GFP_NOWARN;
Sara Sharon26d535a2015-04-28 12:56:54 +0300477
478 INIT_LIST_HEAD(&local_allocated);
479
480 for (i = 0; i < RX_CLAIM_REQ_ALLOC;) {
481 struct iwl_rx_mem_buffer *rxb;
482 struct page *page;
483
484 /* List should never be empty - each reused RBD is
485 * returned to the list, and initial pool covers any
486 * possible gap between the time the page is allocated
487 * to the time the RBD is added.
488 */
489 BUG_ON(list_empty(&local_empty));
490 /* Get the first rxb from the rbd list */
491 rxb = list_first_entry(&local_empty,
492 struct iwl_rx_mem_buffer, list);
493 BUG_ON(rxb->page);
494
495 /* Alloc a new receive buffer */
Sara Sharon78485052015-12-14 17:44:11 +0200496 page = iwl_pcie_rx_alloc_page(trans, gfp_mask);
Sara Sharon26d535a2015-04-28 12:56:54 +0300497 if (!page)
498 continue;
499 rxb->page = page;
500
501 /* Get physical address of the RB */
502 rxb->page_dma = dma_map_page(trans->dev, page, 0,
503 PAGE_SIZE << trans_pcie->rx_page_order,
504 DMA_FROM_DEVICE);
505 if (dma_mapping_error(trans->dev, rxb->page_dma)) {
506 rxb->page = NULL;
507 __free_pages(page, trans_pcie->rx_page_order);
508 continue;
509 }
Sara Sharon26d535a2015-04-28 12:56:54 +0300510
511 /* move the allocated entry to the out list */
512 list_move(&rxb->list, &local_allocated);
513 i++;
514 }
515
516 pending--;
517 if (!pending) {
518 pending = atomic_xchg(&rba->req_pending, 0);
519 IWL_DEBUG_RX(trans,
520 "Pending allocation requests = %d\n",
521 pending);
522 }
523
524 spin_lock(&rba->lock);
525 /* add the allocated rbds to the allocator allocated list */
526 list_splice_tail(&local_allocated, &rba->rbd_allocated);
527 /* get more empty RBDs for current pending requests */
528 list_splice_tail_init(&rba->rbd_empty, &local_empty);
529 spin_unlock(&rba->lock);
530
531 atomic_inc(&rba->req_ready);
532 }
533
534 spin_lock(&rba->lock);
535 /* return unused rbds to the allocator empty list */
536 list_splice_tail(&local_empty, &rba->rbd_empty);
537 spin_unlock(&rba->lock);
538}
539
540/*
541 * iwl_pcie_rx_allocator_get - Returns the pre-allocated pages
542.*
543.* Called by queue when the queue posted allocation request and
544 * has freed 8 RBDs in order to restock itself.
545 */
546static int iwl_pcie_rx_allocator_get(struct iwl_trans *trans,
547 struct iwl_rx_mem_buffer
548 *out[RX_CLAIM_REQ_ALLOC])
549{
550 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
551 struct iwl_rb_allocator *rba = &trans_pcie->rba;
552 int i;
553
554 /*
555 * atomic_dec_if_positive returns req_ready - 1 for any scenario.
556 * If req_ready is 0 atomic_dec_if_positive will return -1 and this
557 * function will return -ENOMEM, as there are no ready requests.
558 * atomic_dec_if_positive will perofrm the *actual* decrement only if
559 * req_ready > 0, i.e. - there are ready requests and the function
560 * hands one request to the caller.
561 */
562 if (atomic_dec_if_positive(&rba->req_ready) < 0)
563 return -ENOMEM;
564
565 spin_lock(&rba->lock);
566 for (i = 0; i < RX_CLAIM_REQ_ALLOC; i++) {
567 /* Get next free Rx buffer, remove it from free list */
568 out[i] = list_first_entry(&rba->rbd_allocated,
569 struct iwl_rx_mem_buffer, list);
570 list_del(&out[i]->list);
571 }
572 spin_unlock(&rba->lock);
573
574 return 0;
575}
576
577static void iwl_pcie_rx_allocator_work(struct work_struct *data)
578{
579 struct iwl_rb_allocator *rba_p =
580 container_of(data, struct iwl_rb_allocator, rx_alloc);
581 struct iwl_trans_pcie *trans_pcie =
582 container_of(rba_p, struct iwl_trans_pcie, rba);
583
584 iwl_pcie_rx_allocator(trans_pcie->trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -0700585}
586
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200587static int iwl_pcie_rx_alloc(struct iwl_trans *trans)
588{
589 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon26d535a2015-04-28 12:56:54 +0300590 struct iwl_rb_allocator *rba = &trans_pcie->rba;
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200591 struct device *dev = trans->dev;
Sara Sharon78485052015-12-14 17:44:11 +0200592 int i;
Sara Sharon96a64972015-12-23 15:10:03 +0200593 int free_size = trans->cfg->mq_rx_supported ? sizeof(__le64) :
594 sizeof(__le32);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200595
Sara Sharon78485052015-12-14 17:44:11 +0200596 if (WARN_ON(trans_pcie->rxq))
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200597 return -EINVAL;
598
Sara Sharon78485052015-12-14 17:44:11 +0200599 trans_pcie->rxq = kcalloc(trans->num_rx_queues, sizeof(struct iwl_rxq),
600 GFP_KERNEL);
601 if (!trans_pcie->rxq)
602 return -EINVAL;
603
604 spin_lock_init(&rba->lock);
605
606 for (i = 0; i < trans->num_rx_queues; i++) {
607 struct iwl_rxq *rxq = &trans_pcie->rxq[i];
608
609 spin_lock_init(&rxq->lock);
Sara Sharon96a64972015-12-23 15:10:03 +0200610 if (trans->cfg->mq_rx_supported)
611 rxq->queue_size = MQ_RX_TABLE_SIZE;
612 else
613 rxq->queue_size = RX_QUEUE_SIZE;
614
Sara Sharon78485052015-12-14 17:44:11 +0200615 /*
616 * Allocate the circular buffer of Read Buffer Descriptors
617 * (RBDs)
618 */
619 rxq->bd = dma_zalloc_coherent(dev,
Sara Sharon96a64972015-12-23 15:10:03 +0200620 free_size * rxq->queue_size,
621 &rxq->bd_dma, GFP_KERNEL);
Sara Sharon78485052015-12-14 17:44:11 +0200622 if (!rxq->bd)
623 goto err;
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200624
Sara Sharon96a64972015-12-23 15:10:03 +0200625 if (trans->cfg->mq_rx_supported) {
626 rxq->used_bd = dma_zalloc_coherent(dev,
627 sizeof(__le32) *
628 rxq->queue_size,
629 &rxq->used_bd_dma,
630 GFP_KERNEL);
631 if (!rxq->used_bd)
632 goto err;
633 }
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200634
Sara Sharon78485052015-12-14 17:44:11 +0200635 /*Allocate the driver's pointer to receive buffer status */
636 rxq->rb_stts = dma_zalloc_coherent(dev, sizeof(*rxq->rb_stts),
637 &rxq->rb_stts_dma,
638 GFP_KERNEL);
639 if (!rxq->rb_stts)
640 goto err;
641 }
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200642 return 0;
643
Sara Sharon78485052015-12-14 17:44:11 +0200644err:
645 for (i = 0; i < trans->num_rx_queues; i++) {
646 struct iwl_rxq *rxq = &trans_pcie->rxq[i];
647
648 if (rxq->bd)
Sara Sharon96a64972015-12-23 15:10:03 +0200649 dma_free_coherent(dev, free_size * rxq->queue_size,
Sara Sharon78485052015-12-14 17:44:11 +0200650 rxq->bd, rxq->bd_dma);
651 rxq->bd_dma = 0;
652 rxq->bd = NULL;
653
654 if (rxq->rb_stts)
655 dma_free_coherent(trans->dev,
656 sizeof(struct iwl_rb_status),
657 rxq->rb_stts, rxq->rb_stts_dma);
Sara Sharon96a64972015-12-23 15:10:03 +0200658
659 if (rxq->used_bd)
660 dma_free_coherent(dev, sizeof(__le32) * rxq->queue_size,
661 rxq->used_bd, rxq->used_bd_dma);
662 rxq->used_bd_dma = 0;
663 rxq->used_bd = NULL;
Sara Sharon78485052015-12-14 17:44:11 +0200664 }
665 kfree(trans_pcie->rxq);
Sara Sharon96a64972015-12-23 15:10:03 +0200666
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200667 return -ENOMEM;
668}
669
670static void iwl_pcie_rx_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
671{
672 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
673 u32 rb_size;
674 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
675
Emmanuel Grumbach6c4fbcb2015-11-10 11:57:41 +0200676 switch (trans_pcie->rx_buf_size) {
677 case IWL_AMSDU_4K:
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200678 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
Emmanuel Grumbach6c4fbcb2015-11-10 11:57:41 +0200679 break;
680 case IWL_AMSDU_8K:
681 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
682 break;
683 case IWL_AMSDU_12K:
684 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_12K;
685 break;
686 default:
687 WARN_ON(1);
688 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
689 }
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200690
691 /* Stop Rx DMA */
692 iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
Johannes Bergddaf5a52013-01-08 11:25:44 +0100693 /* reset and flush pointers */
694 iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_RBDCB_WPTR, 0);
695 iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_FLUSH_RB_REQ, 0);
696 iwl_write_direct32(trans, FH_RSCSR_CHNL0_RDPTR, 0);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200697
698 /* Reset driver's Rx queue write index */
699 iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
700
701 /* Tell device where to find RBD circular buffer in DRAM */
702 iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
703 (u32)(rxq->bd_dma >> 8));
704
705 /* Tell device where in DRAM to update its Rx status */
706 iwl_write_direct32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
707 rxq->rb_stts_dma >> 4);
708
709 /* Enable Rx DMA
710 * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
711 * the credit mechanism in 5000 HW RX FIFO
712 * Direct rx interrupts to hosts
Emmanuel Grumbach6c4fbcb2015-11-10 11:57:41 +0200713 * Rx buffer size 4 or 8k or 12k
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200714 * RB timeout 0x10
715 * 256 RBDs
716 */
717 iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
718 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
719 FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
720 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
721 rb_size|
Emmanuel Grumbach49bd072d2012-11-18 13:14:51 +0200722 (RX_RB_TIMEOUT << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200723 (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
724
725 /* Set interrupt coalescing timer to default (2048 usecs) */
726 iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
Emmanuel Grumbach6960a052013-11-11 15:23:01 +0200727
728 /* W/A for interrupt coalescing bug in 7260 and 3160 */
729 if (trans->cfg->host_interrupt_operation_mode)
730 iwl_set_bit(trans, CSR_INT_COALESCING, IWL_HOST_INT_OPER_MODE);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200731}
732
Sara Sharon96a64972015-12-23 15:10:03 +0200733static void iwl_pcie_rx_mq_hw_init(struct iwl_trans *trans, struct iwl_rxq *rxq)
734{
735 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
736 u32 rb_size, enabled = 0;
737 int i;
738
739 switch (trans_pcie->rx_buf_size) {
740 case IWL_AMSDU_4K:
741 rb_size = RFH_RXF_DMA_RB_SIZE_4K;
742 break;
743 case IWL_AMSDU_8K:
744 rb_size = RFH_RXF_DMA_RB_SIZE_8K;
745 break;
746 case IWL_AMSDU_12K:
747 rb_size = RFH_RXF_DMA_RB_SIZE_12K;
748 break;
749 default:
750 WARN_ON(1);
751 rb_size = RFH_RXF_DMA_RB_SIZE_4K;
752 }
753
754 /* Stop Rx DMA */
755 iwl_write_prph(trans, RFH_RXF_DMA_CFG, 0);
756 /* disable free amd used rx queue operation */
757 iwl_write_prph(trans, RFH_RXF_RXQ_ACTIVE, 0);
758
759 for (i = 0; i < trans->num_rx_queues; i++) {
760 /* Tell device where to find RBD free table in DRAM */
761 iwl_pcie_write_prph_64(trans, RFH_Q_FRBDCB_BA_LSB(i),
762 (u64)(rxq->bd_dma));
763 /* Tell device where to find RBD used table in DRAM */
764 iwl_pcie_write_prph_64(trans, RFH_Q_URBDCB_BA_LSB(i),
765 (u64)(rxq->used_bd_dma));
766 /* Tell device where in DRAM to update its Rx status */
767 iwl_pcie_write_prph_64(trans, RFH_Q_URBD_STTS_WPTR_LSB(i),
768 rxq->rb_stts_dma);
769 /* Reset device indice tables */
770 iwl_write_prph(trans, RFH_Q_FRBDCB_WIDX(i), 0);
771 iwl_write_prph(trans, RFH_Q_FRBDCB_RIDX(i), 0);
772 iwl_write_prph(trans, RFH_Q_URBDCB_WIDX(i), 0);
773
774 enabled |= BIT(i) | BIT(i + 16);
775 }
776
777 /* restock default queue */
778 iwl_pcie_rxq_mq_restock(trans, &trans_pcie->rxq[0]);
779
780 /*
781 * Enable Rx DMA
782 * Single frame mode
783 * Rx buffer size 4 or 8k or 12k
784 * Min RB size 4 or 8
785 * 512 RBDs
786 */
787 iwl_write_prph(trans, RFH_RXF_DMA_CFG,
788 RFH_DMA_EN_ENABLE_VAL |
789 rb_size | RFH_RXF_DMA_SINGLE_FRAME_MASK |
790 RFH_RXF_DMA_MIN_RB_4_8 |
791 RFH_RXF_DMA_RBDCB_SIZE_512);
792
793 iwl_write_prph(trans, RFH_GEN_CFG, RFH_GEN_CFG_RFH_DMA_SNOOP |
794 RFH_GEN_CFG_SERVICE_DMA_SNOOP);
795 iwl_write_prph(trans, RFH_RXF_RXQ_ACTIVE, enabled);
796
797 /* Set interrupt coalescing timer to default (2048 usecs) */
798 iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
799}
800
Johannes Bergc7df1f42013-06-20 20:59:34 +0200801static void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq)
802{
Johannes Bergc7df1f42013-06-20 20:59:34 +0200803 lockdep_assert_held(&rxq->lock);
804
805 INIT_LIST_HEAD(&rxq->rx_free);
806 INIT_LIST_HEAD(&rxq->rx_used);
807 rxq->free_count = 0;
Sara Sharon26d535a2015-04-28 12:56:54 +0300808 rxq->used_count = 0;
Johannes Bergc7df1f42013-06-20 20:59:34 +0200809}
810
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200811int iwl_pcie_rx_init(struct iwl_trans *trans)
812{
813 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon78485052015-12-14 17:44:11 +0200814 struct iwl_rxq *def_rxq;
Sara Sharon26d535a2015-04-28 12:56:54 +0300815 struct iwl_rb_allocator *rba = &trans_pcie->rba;
Sara Sharon96a64972015-12-23 15:10:03 +0200816 int i, err, num_rbds, allocator_pool_size;
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200817
Sara Sharon78485052015-12-14 17:44:11 +0200818 if (!trans_pcie->rxq) {
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200819 err = iwl_pcie_rx_alloc(trans);
820 if (err)
821 return err;
822 }
Sara Sharon78485052015-12-14 17:44:11 +0200823 def_rxq = trans_pcie->rxq;
Sara Sharon26d535a2015-04-28 12:56:54 +0300824 if (!rba->alloc_wq)
825 rba->alloc_wq = alloc_workqueue("rb_allocator",
826 WQ_HIGHPRI | WQ_UNBOUND, 1);
827 INIT_WORK(&rba->rx_alloc, iwl_pcie_rx_allocator_work);
828
829 spin_lock(&rba->lock);
830 atomic_set(&rba->req_pending, 0);
831 atomic_set(&rba->req_ready, 0);
Sara Sharon96a64972015-12-23 15:10:03 +0200832 INIT_LIST_HEAD(&rba->rbd_allocated);
833 INIT_LIST_HEAD(&rba->rbd_empty);
Sara Sharon26d535a2015-04-28 12:56:54 +0300834 spin_unlock(&rba->lock);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200835
Johannes Bergc7df1f42013-06-20 20:59:34 +0200836 /* free all first - we might be reconfigured for a different size */
Sara Sharon78485052015-12-14 17:44:11 +0200837 iwl_pcie_free_rbs_pool(trans);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200838
839 for (i = 0; i < RX_QUEUE_SIZE; i++)
Sara Sharon78485052015-12-14 17:44:11 +0200840 def_rxq->queue[i] = NULL;
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200841
Sara Sharon78485052015-12-14 17:44:11 +0200842 for (i = 0; i < trans->num_rx_queues; i++) {
843 struct iwl_rxq *rxq = &trans_pcie->rxq[i];
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200844
Sara Sharon96a64972015-12-23 15:10:03 +0200845 rxq->id = i;
846
Sara Sharon78485052015-12-14 17:44:11 +0200847 spin_lock(&rxq->lock);
848 /*
849 * Set read write pointer to reflect that we have processed
850 * and used all buffers, but have not restocked the Rx queue
851 * with fresh buffers
852 */
853 rxq->read = 0;
854 rxq->write = 0;
855 rxq->write_actual = 0;
856 memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200857
Sara Sharon78485052015-12-14 17:44:11 +0200858 iwl_pcie_rx_init_rxb_lists(rxq);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200859
Sara Sharon78485052015-12-14 17:44:11 +0200860 spin_unlock(&rxq->lock);
861 }
862
Sara Sharon96a64972015-12-23 15:10:03 +0200863 /* move the pool to the default queue and allocator ownerships */
864 num_rbds = trans->cfg->mq_rx_supported ?
865 MQ_RX_POOL_SIZE : RX_QUEUE_SIZE;
866 allocator_pool_size = trans->num_rx_queues *
867 (RX_CLAIM_REQ_ALLOC - RX_POST_REQ_ALLOC);
868 for (i = 0; i < num_rbds; i++) {
869 struct iwl_rx_mem_buffer *rxb = &trans_pcie->rx_pool[i];
870
871 if (i < allocator_pool_size)
872 list_add(&rxb->list, &rba->rbd_empty);
873 else
874 list_add(&rxb->list, &def_rxq->rx_used);
875 trans_pcie->global_table[i] = rxb;
876 rxb->vid = (u16)i;
877 }
Sara Sharon78485052015-12-14 17:44:11 +0200878
879 iwl_pcie_rxq_alloc_rbs(trans, GFP_KERNEL, def_rxq);
Sara Sharon96a64972015-12-23 15:10:03 +0200880 if (trans->cfg->mq_rx_supported) {
881 iwl_pcie_rx_mq_hw_init(trans, def_rxq);
882 } else {
883 iwl_pcie_rxq_restock(trans, def_rxq);
884 iwl_pcie_rx_hw_init(trans, def_rxq);
885 }
Sara Sharon78485052015-12-14 17:44:11 +0200886
887 spin_lock(&def_rxq->lock);
888 iwl_pcie_rxq_inc_wr_ptr(trans, def_rxq);
889 spin_unlock(&def_rxq->lock);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200890
891 return 0;
892}
893
894void iwl_pcie_rx_free(struct iwl_trans *trans)
895{
896 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon26d535a2015-04-28 12:56:54 +0300897 struct iwl_rb_allocator *rba = &trans_pcie->rba;
Sara Sharon96a64972015-12-23 15:10:03 +0200898 int free_size = trans->cfg->mq_rx_supported ? sizeof(__le64) :
899 sizeof(__le32);
Sara Sharon78485052015-12-14 17:44:11 +0200900 int i;
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200901
Sara Sharon78485052015-12-14 17:44:11 +0200902 /*
903 * if rxq is NULL, it means that nothing has been allocated,
904 * exit now
905 */
906 if (!trans_pcie->rxq) {
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200907 IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
908 return;
909 }
910
Sara Sharon26d535a2015-04-28 12:56:54 +0300911 cancel_work_sync(&rba->rx_alloc);
912 if (rba->alloc_wq) {
913 destroy_workqueue(rba->alloc_wq);
914 rba->alloc_wq = NULL;
915 }
916
Sara Sharon78485052015-12-14 17:44:11 +0200917 iwl_pcie_free_rbs_pool(trans);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200918
Sara Sharon78485052015-12-14 17:44:11 +0200919 for (i = 0; i < trans->num_rx_queues; i++) {
920 struct iwl_rxq *rxq = &trans_pcie->rxq[i];
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200921
Sara Sharon78485052015-12-14 17:44:11 +0200922 if (rxq->bd)
923 dma_free_coherent(trans->dev,
Sara Sharon96a64972015-12-23 15:10:03 +0200924 free_size * rxq->queue_size,
Sara Sharon78485052015-12-14 17:44:11 +0200925 rxq->bd, rxq->bd_dma);
926 rxq->bd_dma = 0;
927 rxq->bd = NULL;
928
929 if (rxq->rb_stts)
930 dma_free_coherent(trans->dev,
931 sizeof(struct iwl_rb_status),
932 rxq->rb_stts, rxq->rb_stts_dma);
933 else
934 IWL_DEBUG_INFO(trans,
935 "Free rxq->rb_stts which is NULL\n");
Sara Sharon78485052015-12-14 17:44:11 +0200936
Sara Sharon96a64972015-12-23 15:10:03 +0200937 if (rxq->used_bd)
938 dma_free_coherent(trans->dev,
939 sizeof(__le32) * rxq->queue_size,
940 rxq->used_bd, rxq->used_bd_dma);
941 rxq->used_bd_dma = 0;
942 rxq->used_bd = NULL;
943 }
Sara Sharon78485052015-12-14 17:44:11 +0200944 kfree(trans_pcie->rxq);
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200945}
946
Sara Sharon26d535a2015-04-28 12:56:54 +0300947/*
948 * iwl_pcie_rx_reuse_rbd - Recycle used RBDs
949 *
950 * Called when a RBD can be reused. The RBD is transferred to the allocator.
951 * When there are 2 empty RBDs - a request for allocation is posted
952 */
953static void iwl_pcie_rx_reuse_rbd(struct iwl_trans *trans,
954 struct iwl_rx_mem_buffer *rxb,
955 struct iwl_rxq *rxq, bool emergency)
956{
957 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
958 struct iwl_rb_allocator *rba = &trans_pcie->rba;
959
960 /* Move the RBD to the used list, will be moved to allocator in batches
961 * before claiming or posting a request*/
962 list_add_tail(&rxb->list, &rxq->rx_used);
963
964 if (unlikely(emergency))
965 return;
966
967 /* Count the allocator owned RBDs */
968 rxq->used_count++;
969
970 /* If we have RX_POST_REQ_ALLOC new released rx buffers -
971 * issue a request for allocator. Modulo RX_CLAIM_REQ_ALLOC is
972 * used for the case we failed to claim RX_CLAIM_REQ_ALLOC,
973 * after but we still need to post another request.
974 */
975 if ((rxq->used_count % RX_CLAIM_REQ_ALLOC) == RX_POST_REQ_ALLOC) {
976 /* Move the 2 RBDs to the allocator ownership.
977 Allocator has another 6 from pool for the request completion*/
978 spin_lock(&rba->lock);
979 list_splice_tail_init(&rxq->rx_used, &rba->rbd_empty);
980 spin_unlock(&rba->lock);
981
982 atomic_inc(&rba->req_pending);
983 queue_work(rba->alloc_wq, &rba->rx_alloc);
984 }
985}
986
Emmanuel Grumbach9805c4462012-11-14 14:44:18 +0200987static void iwl_pcie_rx_handle_rb(struct iwl_trans *trans,
Sara Sharon78485052015-12-14 17:44:11 +0200988 struct iwl_rxq *rxq,
Sara Sharon26d535a2015-04-28 12:56:54 +0300989 struct iwl_rx_mem_buffer *rxb,
990 bool emergency)
Johannes Bergdf2f3212012-03-05 11:24:40 -0800991{
992 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200993 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Johannes Berg0c197442012-03-15 13:26:43 -0700994 bool page_stolen = false;
Johannes Bergb2cf4102012-04-09 17:46:51 -0700995 int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
Johannes Berg0c197442012-03-15 13:26:43 -0700996 u32 offset = 0;
Johannes Bergdf2f3212012-03-05 11:24:40 -0800997
998 if (WARN_ON(!rxb))
999 return;
1000
Johannes Berg0c197442012-03-15 13:26:43 -07001001 dma_unmap_page(trans->dev, rxb->page_dma, max_len, DMA_FROM_DEVICE);
Johannes Bergdf2f3212012-03-05 11:24:40 -08001002
Johannes Berg0c197442012-03-15 13:26:43 -07001003 while (offset + sizeof(u32) + sizeof(struct iwl_cmd_header) < max_len) {
1004 struct iwl_rx_packet *pkt;
Johannes Berg0c197442012-03-15 13:26:43 -07001005 u16 sequence;
1006 bool reclaim;
Johannes Bergf7e64692015-06-23 21:58:17 +02001007 int index, cmd_index, len;
Johannes Berg0c197442012-03-15 13:26:43 -07001008 struct iwl_rx_cmd_buffer rxcb = {
1009 ._offset = offset,
Emmanuel Grumbachd13f1862013-01-23 10:59:29 +02001010 ._rx_page_order = trans_pcie->rx_page_order,
Johannes Berg0c197442012-03-15 13:26:43 -07001011 ._page = rxb->page,
1012 ._page_stolen = false,
David S. Miller0d6c4a22012-05-07 23:35:40 -04001013 .truesize = max_len,
Johannes Berg0c197442012-03-15 13:26:43 -07001014 };
Johannes Bergdf2f3212012-03-05 11:24:40 -08001015
Johannes Berg0c197442012-03-15 13:26:43 -07001016 pkt = rxb_addr(&rxcb);
Johannes Bergdf2f3212012-03-05 11:24:40 -08001017
Johannes Berg0c197442012-03-15 13:26:43 -07001018 if (pkt->len_n_flags == cpu_to_le32(FH_RSCSR_FRAME_INVALID))
1019 break;
Johannes Bergdf2f3212012-03-05 11:24:40 -08001020
Liad Kaufman9243efc2015-03-15 17:38:22 +02001021 IWL_DEBUG_RX(trans,
1022 "cmd at offset %d: %s (0x%.2x, seq 0x%x)\n",
1023 rxcb._offset,
Sharon Dvir39bdb172015-10-15 18:18:09 +03001024 iwl_get_cmd_string(trans,
1025 iwl_cmd_id(pkt->hdr.cmd,
1026 pkt->hdr.group_id,
1027 0)),
Liad Kaufman9243efc2015-03-15 17:38:22 +02001028 pkt->hdr.cmd, le16_to_cpu(pkt->hdr.sequence));
Johannes Bergdf2f3212012-03-05 11:24:40 -08001029
Johannes Berg65b30342014-01-08 13:16:33 +01001030 len = iwl_rx_packet_len(pkt);
Johannes Berg0c197442012-03-15 13:26:43 -07001031 len += sizeof(u32); /* account for status word */
Johannes Bergf042c2e2012-09-05 22:34:44 +02001032 trace_iwlwifi_dev_rx(trans->dev, trans, pkt, len);
1033 trace_iwlwifi_dev_rx_data(trans->dev, trans, pkt, len);
Johannes Bergd663ee72012-03-10 13:00:07 -08001034
Johannes Berg0c197442012-03-15 13:26:43 -07001035 /* Reclaim a command buffer only if this packet is a response
1036 * to a (driver-originated) command.
1037 * If the packet (e.g. Rx frame) originated from uCode,
1038 * there is no command buffer to reclaim.
1039 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1040 * but apparently a few don't get set; catch them here. */
1041 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME);
1042 if (reclaim) {
1043 int i;
1044
1045 for (i = 0; i < trans_pcie->n_no_reclaim_cmds; i++) {
1046 if (trans_pcie->no_reclaim_cmds[i] ==
1047 pkt->hdr.cmd) {
1048 reclaim = false;
1049 break;
1050 }
Johannes Bergd663ee72012-03-10 13:00:07 -08001051 }
1052 }
Johannes Bergdf2f3212012-03-05 11:24:40 -08001053
Johannes Berg0c197442012-03-15 13:26:43 -07001054 sequence = le16_to_cpu(pkt->hdr.sequence);
1055 index = SEQ_TO_INDEX(sequence);
1056 cmd_index = get_cmd_index(&txq->q, index);
Johannes Bergdf2f3212012-03-05 11:24:40 -08001057
Johannes Berg1be5d8c2015-06-11 16:51:24 +02001058 iwl_op_mode_rx(trans->op_mode, &trans_pcie->napi, &rxcb);
Johannes Berg0c197442012-03-15 13:26:43 -07001059
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001060 if (reclaim) {
Johannes Berg5d4185a2014-09-09 21:16:06 +02001061 kzfree(txq->entries[cmd_index].free_buf);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001062 txq->entries[cmd_index].free_buf = NULL;
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001063 }
1064
Johannes Berg0c197442012-03-15 13:26:43 -07001065 /*
1066 * After here, we should always check rxcb._page_stolen,
1067 * if it is true then one of the handlers took the page.
1068 */
1069
1070 if (reclaim) {
1071 /* Invoke any callbacks, transfer the buffer to caller,
1072 * and fire off the (possibly) blocking
1073 * iwl_trans_send_cmd()
1074 * as we reclaim the driver command queue */
1075 if (!rxcb._page_stolen)
Johannes Bergf7e64692015-06-23 21:58:17 +02001076 iwl_pcie_hcmd_complete(trans, &rxcb);
Johannes Berg0c197442012-03-15 13:26:43 -07001077 else
1078 IWL_WARN(trans, "Claim null rxb?\n");
1079 }
1080
1081 page_stolen |= rxcb._page_stolen;
1082 offset += ALIGN(len, FH_RSCSR_FRAME_ALIGN);
Johannes Bergdf2f3212012-03-05 11:24:40 -08001083 }
1084
Johannes Berg0c197442012-03-15 13:26:43 -07001085 /* page was stolen from us -- free our reference */
1086 if (page_stolen) {
Johannes Bergb2cf4102012-04-09 17:46:51 -07001087 __free_pages(rxb->page, trans_pcie->rx_page_order);
Johannes Bergdf2f3212012-03-05 11:24:40 -08001088 rxb->page = NULL;
Johannes Berg0c197442012-03-15 13:26:43 -07001089 }
Johannes Bergdf2f3212012-03-05 11:24:40 -08001090
1091 /* Reuse the page if possible. For notification packets and
1092 * SKBs that fail to Rx correctly, add them back into the
1093 * rx_free list for reuse later. */
Johannes Bergdf2f3212012-03-05 11:24:40 -08001094 if (rxb->page != NULL) {
1095 rxb->page_dma =
1096 dma_map_page(trans->dev, rxb->page, 0,
Johannes Berg20d3b642012-05-16 22:54:29 +02001097 PAGE_SIZE << trans_pcie->rx_page_order,
1098 DMA_FROM_DEVICE);
Johannes Berg7c3415822012-11-04 09:29:17 +01001099 if (dma_mapping_error(trans->dev, rxb->page_dma)) {
1100 /*
1101 * free the page(s) as well to not break
1102 * the invariant that the items on the used
1103 * list have no page(s)
1104 */
1105 __free_pages(rxb->page, trans_pcie->rx_page_order);
1106 rxb->page = NULL;
Sara Sharon26d535a2015-04-28 12:56:54 +03001107 iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
Johannes Berg7c3415822012-11-04 09:29:17 +01001108 } else {
1109 list_add_tail(&rxb->list, &rxq->rx_free);
1110 rxq->free_count++;
1111 }
Johannes Bergdf2f3212012-03-05 11:24:40 -08001112 } else
Sara Sharon26d535a2015-04-28 12:56:54 +03001113 iwl_pcie_rx_reuse_rbd(trans, rxb, rxq, emergency);
Johannes Bergdf2f3212012-03-05 11:24:40 -08001114}
1115
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001116/*
1117 * iwl_pcie_rx_handle - Main entry function for receiving responses from fw
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001118 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001119static void iwl_pcie_rx_handle(struct iwl_trans *trans)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001120{
Johannes Bergdf2f3212012-03-05 11:24:40 -08001121 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon78485052015-12-14 17:44:11 +02001122 struct iwl_rxq *rxq = &trans_pcie->rxq[0];
Sara Sharon26d535a2015-04-28 12:56:54 +03001123 u32 r, i, j, count = 0;
1124 bool emergency = false;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001125
Johannes Bergf14d6b32014-03-21 13:30:03 +01001126restart:
1127 spin_lock(&rxq->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001128 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1129 * buffer that the driver may process (last buffer filled by ucode). */
Emmanuel Grumbach52e2a992012-11-25 14:42:25 +02001130 r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001131 i = rxq->read;
1132
1133 /* Rx interrupt, but nothing sent from uCode */
1134 if (i == r)
Emmanuel Grumbach726f23f2012-05-16 22:40:49 +02001135 IWL_DEBUG_RX(trans, "HW = SW = %d\n", r);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001136
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001137 while (i != r) {
Johannes Berg48a2d662012-03-05 11:24:39 -08001138 struct iwl_rx_mem_buffer *rxb;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001139
Sara Sharon96a64972015-12-23 15:10:03 +02001140 if (unlikely(rxq->used_count == rxq->queue_size / 2))
Sara Sharon26d535a2015-04-28 12:56:54 +03001141 emergency = true;
1142
Sara Sharon96a64972015-12-23 15:10:03 +02001143 if (trans->cfg->mq_rx_supported) {
1144 /*
1145 * used_bd is a 32 bit but only 12 are used to retrieve
1146 * the vid
1147 */
1148 u16 vid = (u16)le32_to_cpu(rxq->used_bd[i]);
1149
1150 rxb = trans_pcie->global_table[vid];
1151 } else {
1152 rxb = rxq->queue[i];
1153 rxq->queue[i] = NULL;
1154 }
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001155
Johannes Bergf02d2cc2015-11-06 11:27:23 +01001156 IWL_DEBUG_RX(trans, "rxbuf: HW = %d, SW = %d\n", r, i);
Sara Sharon78485052015-12-14 17:44:11 +02001157 iwl_pcie_rx_handle_rb(trans, rxq, rxb, emergency);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001158
Sara Sharon96a64972015-12-23 15:10:03 +02001159 i = (i + 1) & (rxq->queue_size - 1);
Sara Sharon26d535a2015-04-28 12:56:54 +03001160
1161 /* If we have RX_CLAIM_REQ_ALLOC released rx buffers -
1162 * try to claim the pre-allocated buffers from the allocator */
1163 if (rxq->used_count >= RX_CLAIM_REQ_ALLOC) {
1164 struct iwl_rb_allocator *rba = &trans_pcie->rba;
1165 struct iwl_rx_mem_buffer *out[RX_CLAIM_REQ_ALLOC];
1166
1167 if (rxq->used_count % RX_CLAIM_REQ_ALLOC == 0 &&
1168 !emergency) {
1169 /* Add the remaining 6 empty RBDs
1170 * for allocator use
1171 */
1172 spin_lock(&rba->lock);
1173 list_splice_tail_init(&rxq->rx_used,
1174 &rba->rbd_empty);
1175 spin_unlock(&rba->lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001176 }
Sara Sharon26d535a2015-04-28 12:56:54 +03001177
1178 /* If not ready - continue, will try to reclaim later.
1179 * No need to reschedule work - allocator exits only on
1180 * success */
1181 if (!iwl_pcie_rx_allocator_get(trans, out)) {
1182 /* If success - then RX_CLAIM_REQ_ALLOC
1183 * buffers were retrieved and should be added
1184 * to free list */
1185 rxq->used_count -= RX_CLAIM_REQ_ALLOC;
1186 for (j = 0; j < RX_CLAIM_REQ_ALLOC; j++) {
1187 list_add_tail(&out[j]->list,
1188 &rxq->rx_free);
1189 rxq->free_count++;
1190 }
1191 }
1192 }
1193 if (emergency) {
1194 count++;
1195 if (count == 8) {
1196 count = 0;
Sara Sharon96a64972015-12-23 15:10:03 +02001197 if (rxq->used_count < rxq->queue_size / 3)
Sara Sharon26d535a2015-04-28 12:56:54 +03001198 emergency = false;
1199 spin_unlock(&rxq->lock);
Sara Sharon78485052015-12-14 17:44:11 +02001200 iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
Sara Sharon26d535a2015-04-28 12:56:54 +03001201 spin_lock(&rxq->lock);
1202 }
1203 }
1204 /* handle restock for three cases, can be all of them at once:
1205 * - we just pulled buffers from the allocator
1206 * - we have 8+ unstolen pages accumulated
1207 * - we are in emergency and allocated buffers
1208 */
1209 if (rxq->free_count >= RX_CLAIM_REQ_ALLOC) {
1210 rxq->read = i;
1211 spin_unlock(&rxq->lock);
Sara Sharon96a64972015-12-23 15:10:03 +02001212 if (trans->cfg->mq_rx_supported)
1213 iwl_pcie_rxq_mq_restock(trans, rxq);
1214 else
1215 iwl_pcie_rxq_restock(trans, rxq);
Sara Sharon26d535a2015-04-28 12:56:54 +03001216 goto restart;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001217 }
1218 }
1219
1220 /* Backtrack one entry */
1221 rxq->read = i;
Johannes Bergf14d6b32014-03-21 13:30:03 +01001222 spin_unlock(&rxq->lock);
1223
Sara Sharon26d535a2015-04-28 12:56:54 +03001224 /*
1225 * handle a case where in emergency there are some unallocated RBDs.
1226 * those RBDs are in the used list, but are not tracked by the queue's
1227 * used_count which counts allocator owned RBDs.
1228 * unallocated emergency RBDs must be allocated on exit, otherwise
1229 * when called again the function may not be in emergency mode and
1230 * they will be handed to the allocator with no tracking in the RBD
1231 * allocator counters, which will lead to them never being claimed back
1232 * by the queue.
1233 * by allocating them here, they are now in the queue free list, and
1234 * will be restocked by the next call of iwl_pcie_rxq_restock.
1235 */
1236 if (unlikely(emergency && count))
Sara Sharon78485052015-12-14 17:44:11 +02001237 iwl_pcie_rxq_alloc_rbs(trans, GFP_ATOMIC, rxq);
Emmanuel Grumbach255ba062015-07-11 22:30:49 +03001238
Johannes Bergf14d6b32014-03-21 13:30:03 +01001239 if (trans_pcie->napi.poll)
1240 napi_gro_flush(&trans_pcie->napi, false);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001241}
1242
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001243/*
1244 * iwl_pcie_irq_handle_error - called for HW or SW error interrupt from card
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -07001245 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001246static void iwl_pcie_irq_handle_error(struct iwl_trans *trans)
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -07001247{
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001248 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach11033232015-06-24 14:58:13 +03001249 int i;
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001250
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -07001251 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
Emmanuel Grumbach035f7ff2012-03-26 08:57:01 -07001252 if (trans->cfg->internal_wimax_coex &&
Avri Altman95411d02015-05-11 11:04:34 +03001253 !trans->cfg->apmg_not_supported &&
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001254 (!(iwl_read_prph(trans, APMG_CLK_CTRL_REG) &
Johannes Berg20d3b642012-05-16 22:54:29 +02001255 APMS_CLK_VAL_MRB_FUNC_MODE) ||
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001256 (iwl_read_prph(trans, APMG_PS_CTRL_REG) &
Johannes Berg20d3b642012-05-16 22:54:29 +02001257 APMG_PS_CTRL_VAL_RESET_REQ))) {
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001258 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Don Fry8a8bbdb2012-03-20 10:33:34 -07001259 iwl_op_mode_wimax_active(trans->op_mode);
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001260 wake_up(&trans_pcie->wait_command_queue);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -07001261 return;
1262 }
1263
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001264 iwl_pcie_dump_csr(trans);
Inbal Hacohen313b0a22013-06-24 10:35:53 +03001265 iwl_dump_fh(trans, NULL);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -07001266
Arik Nemtsov2a988e92013-12-01 13:50:40 +02001267 local_bh_disable();
1268 /* The STATUS_FW_ERROR bit is set in this function. This must happen
1269 * before we wake up the command caller, to ensure a proper cleanup. */
1270 iwl_trans_fw_error(trans);
1271 local_bh_enable();
1272
Emmanuel Grumbach11033232015-06-24 14:58:13 +03001273 for (i = 0; i < trans->cfg->base_params->num_of_queues; i++)
1274 del_timer(&trans_pcie->txq[i].stuck_timer);
1275
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001276 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001277 wake_up(&trans_pcie->wait_command_queue);
Emmanuel Grumbach7ff94702011-08-25 23:10:54 -07001278}
1279
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001280static u32 iwl_pcie_int_cause_non_ict(struct iwl_trans *trans)
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001281{
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001282 u32 inta;
1283
Emmanuel Grumbach46e81af2014-01-14 10:33:54 +02001284 lockdep_assert_held(&IWL_TRANS_GET_PCIE_TRANS(trans)->irq_lock);
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001285
1286 trace_iwlwifi_dev_irq(trans->dev);
1287
1288 /* Discover which interrupts are active/pending */
1289 inta = iwl_read32(trans, CSR_INT);
1290
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001291 /* the thread will service interrupts and re-enable them */
Emmanuel Grumbachfe523dc2013-12-11 09:24:39 +02001292 return inta;
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001293}
1294
1295/* a device (PCI-E) page is 4096 bytes long */
1296#define ICT_SHIFT 12
1297#define ICT_SIZE (1 << ICT_SHIFT)
1298#define ICT_COUNT (ICT_SIZE / sizeof(u32))
1299
1300/* interrupt handler using ict table, with this interrupt driver will
1301 * stop using INTA register to get device's interrupt, reading this register
1302 * is expensive, device will write interrupts in ICT dram table, increment
1303 * index then will fire interrupt to driver, driver will OR all ICT table
1304 * entries from current index up to table entry with 0 value. the result is
1305 * the interrupt we need to service, driver will set the entries back to 0 and
1306 * set index.
1307 */
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001308static u32 iwl_pcie_int_cause_ict(struct iwl_trans *trans)
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001309{
1310 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001311 u32 inta;
1312 u32 val = 0;
1313 u32 read;
1314
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001315 trace_iwlwifi_dev_irq(trans->dev);
1316
1317 /* Ignore interrupt if there's nothing in NIC to service.
1318 * This may be due to IRQ shared with another device,
1319 * or due to sporadic interrupts thrown from our NIC. */
1320 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1321 trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index, read);
Emmanuel Grumbach7ba1faa2013-12-11 09:39:30 +02001322 if (!read)
1323 return 0;
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001324
1325 /*
1326 * Collect all entries up to the first 0, starting from ict_index;
1327 * note we already read at ict_index.
1328 */
1329 do {
1330 val |= read;
1331 IWL_DEBUG_ISR(trans, "ICT index %d value 0x%08X\n",
1332 trans_pcie->ict_index, read);
1333 trans_pcie->ict_tbl[trans_pcie->ict_index] = 0;
1334 trans_pcie->ict_index =
Johannes Berg83f32a42014-04-24 09:57:40 +02001335 ((trans_pcie->ict_index + 1) & (ICT_COUNT - 1));
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001336
1337 read = le32_to_cpu(trans_pcie->ict_tbl[trans_pcie->ict_index]);
1338 trace_iwlwifi_dev_ict_read(trans->dev, trans_pcie->ict_index,
1339 read);
1340 } while (read);
1341
1342 /* We should not get this value, just ignore it. */
1343 if (val == 0xffffffff)
1344 val = 0;
1345
1346 /*
1347 * this is a w/a for a h/w bug. the h/w bug may cause the Rx bit
1348 * (bit 15 before shifting it to 31) to clear when using interrupt
1349 * coalescing. fortunately, bits 18 and 19 stay set when this happens
1350 * so we use them to decide on the real state of the Rx bit.
1351 * In order words, bit 15 is set if bit 18 or bit 19 are set.
1352 */
1353 if (val & 0xC0000)
1354 val |= 0x8000;
1355
1356 inta = (0xff & val) | ((0xff00 & val) << 16);
Emmanuel Grumbachfe523dc2013-12-11 09:24:39 +02001357 return inta;
Emmanuel Grumbachfc844722013-12-09 14:27:44 +02001358}
1359
Johannes Berg2bfb5092012-12-27 21:43:48 +01001360irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id)
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001361{
Johannes Berg2bfb5092012-12-27 21:43:48 +01001362 struct iwl_trans *trans = dev_id;
Johannes Berg20d3b642012-05-16 22:54:29 +02001363 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1364 struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001365 u32 inta = 0;
1366 u32 handled = 0;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001367
Johannes Berg2bfb5092012-12-27 21:43:48 +01001368 lock_map_acquire(&trans->sync_cmd_lockdep_map);
1369
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001370 spin_lock(&trans_pcie->irq_lock);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001371
Emmanuel Grumbach0fec9542013-12-11 09:02:25 +02001372 /* dram interrupt table not set yet,
1373 * use legacy interrupt.
1374 */
1375 if (likely(trans_pcie->use_ict))
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001376 inta = iwl_pcie_int_cause_ict(trans);
Emmanuel Grumbach0fec9542013-12-11 09:02:25 +02001377 else
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001378 inta = iwl_pcie_int_cause_non_ict(trans);
Emmanuel Grumbach0fec9542013-12-11 09:02:25 +02001379
Emmanuel Grumbach7ba1faa2013-12-11 09:39:30 +02001380 if (iwl_have_debug_level(IWL_DL_ISR)) {
1381 IWL_DEBUG_ISR(trans,
1382 "ISR inta 0x%08x, enabled 0x%08x(sw), enabled(hw) 0x%08x, fh 0x%08x\n",
1383 inta, trans_pcie->inta_mask,
1384 iwl_read32(trans, CSR_INT_MASK),
1385 iwl_read32(trans, CSR_FH_INT_STATUS));
1386 if (inta & (~trans_pcie->inta_mask))
1387 IWL_DEBUG_ISR(trans,
1388 "We got a masked interrupt (0x%08x)\n",
1389 inta & (~trans_pcie->inta_mask));
1390 }
1391
1392 inta &= trans_pcie->inta_mask;
1393
1394 /*
1395 * Ignore interrupt if there's nothing in NIC to service.
1396 * This may be due to IRQ shared with another device,
1397 * or due to sporadic interrupts thrown from our NIC.
1398 */
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001399 if (unlikely(!inta)) {
Emmanuel Grumbach7ba1faa2013-12-11 09:39:30 +02001400 IWL_DEBUG_ISR(trans, "Ignore interrupt, inta == 0\n");
1401 /*
1402 * Re-enable interrupts here since we don't
1403 * have anything to service
1404 */
1405 if (test_bit(STATUS_INT_ENABLED, &trans->status))
1406 iwl_enable_interrupts(trans);
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001407 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001408 lock_map_release(&trans->sync_cmd_lockdep_map);
1409 return IRQ_NONE;
1410 }
1411
Emmanuel Grumbach7ba1faa2013-12-11 09:39:30 +02001412 if (unlikely(inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1413 /*
1414 * Hardware disappeared. It might have
1415 * already raised an interrupt.
1416 */
1417 IWL_WARN(trans, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001418 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001419 goto out;
Emmanuel Grumbacha0f337c2013-12-11 09:00:03 +02001420 }
1421
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001422 /* Ack/clear/reset pending uCode interrupts.
1423 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1424 */
1425 /* There is a hardware bug in the interrupt mask function that some
1426 * interrupts (i.e. CSR_INT_BIT_SCD) can still be generated even if
1427 * they are disabled in the CSR_INT_MASK register. Furthermore the
1428 * ICT interrupt handling mechanism has another bug that might cause
1429 * these unmasked interrupts fail to be detected. We workaround the
1430 * hardware bugs here by ACKing all the possible interrupts so that
1431 * interrupt coalescing can still be achieved.
1432 */
Emmanuel Grumbach7117c002013-12-11 09:20:34 +02001433 iwl_write32(trans, CSR_INT, inta | ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001434
Johannes Berg51cd53a2013-06-12 09:56:51 +02001435 if (iwl_have_debug_level(IWL_DL_ISR))
Johannes Berg0ca24da2012-03-15 13:26:46 -07001436 IWL_DEBUG_ISR(trans, "inta 0x%08x, enabled 0x%08x\n",
Johannes Berg51cd53a2013-06-12 09:56:51 +02001437 inta, iwl_read32(trans, CSR_INT_MASK));
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001438
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001439 spin_unlock(&trans_pcie->irq_lock);
Johannes Bergb49ba042012-01-19 08:20:57 -08001440
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001441 /* Now service all interrupt bits discovered above. */
1442 if (inta & CSR_INT_BIT_HW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001443 IWL_ERR(trans, "Hardware error detected. Restarting.\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001444
1445 /* Tell the device to stop sending interrupts */
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001446 iwl_disable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001447
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001448 isr_stats->hw++;
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001449 iwl_pcie_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001450
1451 handled |= CSR_INT_BIT_HW_ERR;
1452
Johannes Berg2bfb5092012-12-27 21:43:48 +01001453 goto out;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001454 }
1455
Johannes Berga8bceb32012-03-05 11:24:30 -08001456 if (iwl_have_debug_level(IWL_DL_ISR)) {
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001457 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1458 if (inta & CSR_INT_BIT_SCD) {
Johannes Berg51cd53a2013-06-12 09:56:51 +02001459 IWL_DEBUG_ISR(trans,
1460 "Scheduler finished to transmit the frame/frames.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001461 isr_stats->sch++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001462 }
1463
1464 /* Alive notification via Rx interrupt will do the real work */
1465 if (inta & CSR_INT_BIT_ALIVE) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001466 IWL_DEBUG_ISR(trans, "Alive interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001467 isr_stats->alive++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001468 }
1469 }
Johannes Berg51cd53a2013-06-12 09:56:51 +02001470
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001471 /* Safely ignore these bits for debug checks below */
1472 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1473
1474 /* HW RF KILL switch toggled */
1475 if (inta & CSR_INT_BIT_RF_KILL) {
Johannes Bergc9eec952012-03-06 13:30:43 -08001476 bool hw_rfkill;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001477
Emmanuel Grumbach8d425512012-03-28 11:00:58 +02001478 hw_rfkill = iwl_is_rfkill_set(trans);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001479 IWL_WARN(trans, "RF_KILL bit toggled to %s.\n",
Johannes Berg20d3b642012-05-16 22:54:29 +02001480 hw_rfkill ? "disable radio" : "enable radio");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001481
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001482 isr_stats->rfkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001483
Emmanuel Grumbachfa9f3282015-06-11 20:45:49 +03001484 mutex_lock(&trans_pcie->mutex);
Johannes Berg14cfca72014-02-25 20:50:53 +01001485 iwl_trans_pcie_rf_kill(trans, hw_rfkill);
Emmanuel Grumbachfa9f3282015-06-11 20:45:49 +03001486 mutex_unlock(&trans_pcie->mutex);
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001487 if (hw_rfkill) {
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001488 set_bit(STATUS_RFKILL, &trans->status);
1489 if (test_and_clear_bit(STATUS_SYNC_HCMD_ACTIVE,
1490 &trans->status))
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001491 IWL_DEBUG_RF_KILL(trans,
1492 "Rfkill while SYNC HCMD in flight\n");
1493 wake_up(&trans_pcie->wait_command_queue);
1494 } else {
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001495 clear_bit(STATUS_RFKILL, &trans->status);
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001496 }
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001497
1498 handled |= CSR_INT_BIT_RF_KILL;
1499 }
1500
1501 /* Chip got too hot and stopped itself */
1502 if (inta & CSR_INT_BIT_CT_KILL) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001503 IWL_ERR(trans, "Microcode CT kill error detected.\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001504 isr_stats->ctkill++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001505 handled |= CSR_INT_BIT_CT_KILL;
1506 }
1507
1508 /* Error detected by uCode */
1509 if (inta & CSR_INT_BIT_SW_ERR) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001510 IWL_ERR(trans, "Microcode SW error detected. "
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001511 " Restarting 0x%X.\n", inta);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001512 isr_stats->sw++;
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001513 iwl_pcie_irq_handle_error(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001514 handled |= CSR_INT_BIT_SW_ERR;
1515 }
1516
1517 /* uCode wakes up after power-down sleep */
1518 if (inta & CSR_INT_BIT_WAKEUP) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001519 IWL_DEBUG_ISR(trans, "Wakeup interrupt\n");
Johannes Berg5d63f922014-02-27 11:20:07 +01001520 iwl_pcie_rxq_check_wrptr(trans);
Johannes Bergea68f462014-02-27 14:36:55 +01001521 iwl_pcie_txq_check_wrptrs(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001522
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001523 isr_stats->wakeup++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001524
1525 handled |= CSR_INT_BIT_WAKEUP;
1526 }
1527
1528 /* All uCode command responses, including Tx command responses,
1529 * Rx "responses" (frame-received notification), and other
1530 * notifications from uCode come through here*/
1531 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX |
Johannes Berg20d3b642012-05-16 22:54:29 +02001532 CSR_INT_BIT_RX_PERIODIC)) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001533 IWL_DEBUG_ISR(trans, "Rx interrupt\n");
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001534 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1535 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001536 iwl_write32(trans, CSR_FH_INT_STATUS,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001537 CSR_FH_INT_RX_MASK);
1538 }
1539 if (inta & CSR_INT_BIT_RX_PERIODIC) {
1540 handled |= CSR_INT_BIT_RX_PERIODIC;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001541 iwl_write32(trans,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001542 CSR_INT, CSR_INT_BIT_RX_PERIODIC);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001543 }
1544 /* Sending RX interrupt require many steps to be done in the
1545 * the device:
1546 * 1- write interrupt to current index in ICT table.
1547 * 2- dma RX frame.
1548 * 3- update RX shared data to indicate last write index.
1549 * 4- send interrupt.
1550 * This could lead to RX race, driver could receive RX interrupt
1551 * but the shared data changes does not reflect this;
1552 * periodic interrupt will detect any dangling Rx activity.
1553 */
1554
1555 /* Disable periodic interrupt; we use it as just a one-shot. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001556 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001557 CSR_INT_PERIODIC_DIS);
Johannes Berg63791032012-09-06 15:33:42 +02001558
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001559 /*
1560 * Enable periodic interrupt in 8 msec only if we received
1561 * real RX interrupt (instead of just periodic int), to catch
1562 * any dangling Rx interrupt. If it was just the periodic
1563 * interrupt, there was no dangling Rx activity, and no need
1564 * to extend the periodic interrupt; one-shot is enough.
1565 */
1566 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX))
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001567 iwl_write8(trans, CSR_INT_PERIODIC_REG,
Johannes Berg20d3b642012-05-16 22:54:29 +02001568 CSR_INT_PERIODIC_ENA);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001569
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001570 isr_stats->rx++;
Johannes Bergf14d6b32014-03-21 13:30:03 +01001571
1572 local_bh_disable();
1573 iwl_pcie_rx_handle(trans);
1574 local_bh_enable();
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001575 }
1576
1577 /* This "Tx" DMA channel is used only for loading uCode */
1578 if (inta & CSR_INT_BIT_FH_TX) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001579 iwl_write32(trans, CSR_FH_INT_STATUS, CSR_FH_INT_TX_MASK);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001580 IWL_DEBUG_ISR(trans, "uCode load interrupt\n");
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001581 isr_stats->tx++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001582 handled |= CSR_INT_BIT_FH_TX;
1583 /* Wake up uCode load routine, now that load is complete */
Johannes Berg13df1aa2012-03-06 13:31:00 -08001584 trans_pcie->ucode_write_complete = true;
1585 wake_up(&trans_pcie->ucode_write_waitq);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001586 }
1587
1588 if (inta & ~handled) {
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001589 IWL_ERR(trans, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001590 isr_stats->unhandled++;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001591 }
1592
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001593 if (inta & ~(trans_pcie->inta_mask)) {
1594 IWL_WARN(trans, "Disabled INTA bits 0x%08x were pending\n",
1595 inta & ~trans_pcie->inta_mask);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001596 }
1597
1598 /* Re-enable all interrupts */
1599 /* only Re-enable if disabled by irq */
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001600 if (test_bit(STATUS_INT_ENABLED, &trans->status))
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001601 iwl_enable_interrupts(trans);
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001602 /* Re-enable RF_KILL if it occurred */
Stanislaw Gruszka8722c892012-03-07 09:52:28 -08001603 else if (handled & CSR_INT_BIT_RF_KILL)
1604 iwl_enable_rfkill_int(trans);
Johannes Berg2bfb5092012-12-27 21:43:48 +01001605
1606out:
1607 lock_map_release(&trans->sync_cmd_lockdep_map);
1608 return IRQ_HANDLED;
Emmanuel Grumbachab697a92011-07-11 07:35:34 -07001609}
1610
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001611/******************************************************************************
1612 *
1613 * ICT functions
1614 *
1615 ******************************************************************************/
Johannes Berg10667132011-12-19 14:00:59 -08001616
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001617/* Free dram table */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001618void iwl_pcie_free_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001619{
Johannes Berg20d3b642012-05-16 22:54:29 +02001620 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001621
Johannes Berg10667132011-12-19 14:00:59 -08001622 if (trans_pcie->ict_tbl) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001623 dma_free_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001624 trans_pcie->ict_tbl,
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001625 trans_pcie->ict_tbl_dma);
Johannes Berg10667132011-12-19 14:00:59 -08001626 trans_pcie->ict_tbl = NULL;
1627 trans_pcie->ict_tbl_dma = 0;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001628 }
1629}
1630
Johannes Berg10667132011-12-19 14:00:59 -08001631/*
1632 * allocate dram shared table, it is an aligned memory
1633 * block of ICT_SIZE.
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001634 * also reset all data related to ICT table interrupt.
1635 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001636int iwl_pcie_alloc_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001637{
Johannes Berg20d3b642012-05-16 22:54:29 +02001638 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001639
Johannes Berg10667132011-12-19 14:00:59 -08001640 trans_pcie->ict_tbl =
Emmanuel Grumbacheef31712013-12-09 09:47:46 +02001641 dma_zalloc_coherent(trans->dev, ICT_SIZE,
Johannes Berg10667132011-12-19 14:00:59 -08001642 &trans_pcie->ict_tbl_dma,
1643 GFP_KERNEL);
1644 if (!trans_pcie->ict_tbl)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001645 return -ENOMEM;
1646
Johannes Berg10667132011-12-19 14:00:59 -08001647 /* just an API sanity check ... it is guaranteed to be aligned */
1648 if (WARN_ON(trans_pcie->ict_tbl_dma & (ICT_SIZE - 1))) {
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001649 iwl_pcie_free_ict(trans);
Johannes Berg10667132011-12-19 14:00:59 -08001650 return -EINVAL;
1651 }
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001652
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001653 return 0;
1654}
1655
1656/* Device is going up inform it about using ICT interrupt table,
1657 * also we need to tell the driver to start using ICT interrupt.
1658 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001659void iwl_pcie_reset_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001660{
Johannes Berg20d3b642012-05-16 22:54:29 +02001661 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001662 u32 val;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001663
Johannes Berg10667132011-12-19 14:00:59 -08001664 if (!trans_pcie->ict_tbl)
Emmanuel Grumbached6a3802012-01-02 16:10:08 +02001665 return;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001666
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001667 spin_lock(&trans_pcie->irq_lock);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001668 iwl_disable_interrupts(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001669
Johannes Berg10667132011-12-19 14:00:59 -08001670 memset(trans_pcie->ict_tbl, 0, ICT_SIZE);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001671
Johannes Berg10667132011-12-19 14:00:59 -08001672 val = trans_pcie->ict_tbl_dma >> ICT_SHIFT;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001673
Eliad Peller18f5a372015-07-16 20:17:42 +03001674 val |= CSR_DRAM_INT_TBL_ENABLE |
1675 CSR_DRAM_INIT_TBL_WRAP_CHECK |
1676 CSR_DRAM_INIT_TBL_WRITE_POINTER;
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001677
Johannes Berg10667132011-12-19 14:00:59 -08001678 IWL_DEBUG_ISR(trans, "CSR_DRAM_INT_TBL_REG =0x%x\n", val);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001679
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001680 iwl_write32(trans, CSR_DRAM_INT_TBL_REG, val);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001681 trans_pcie->use_ict = true;
1682 trans_pcie->ict_index = 0;
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001683 iwl_write32(trans, CSR_INT, trans_pcie->inta_mask);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001684 iwl_enable_interrupts(trans);
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001685 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001686}
1687
1688/* Device is going down disable ict interrupt usage */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001689void iwl_pcie_disable_ict(struct iwl_trans *trans)
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001690{
Johannes Berg20d3b642012-05-16 22:54:29 +02001691 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001692
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001693 spin_lock(&trans_pcie->irq_lock);
Emmanuel Grumbach0c325762011-08-25 23:10:53 -07001694 trans_pcie->use_ict = false;
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001695 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbach1a361cd2011-07-11 07:44:57 -07001696}
1697
Emmanuel Grumbach85bf9da2013-12-09 11:48:30 +02001698irqreturn_t iwl_pcie_isr(int irq, void *data)
1699{
1700 struct iwl_trans *trans = data;
1701
1702 if (!trans)
1703 return IRQ_NONE;
1704
1705 /* Disable (but don't clear!) interrupts here to avoid
1706 * back-to-back ISRs and sporadic interrupts from our NIC.
1707 * If we have something to service, the tasklet will re-enable ints.
1708 * If we *don't* have something, we'll re-enable before leaving here.
1709 */
1710 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
1711
Emmanuel Grumbacha0f337c2013-12-11 09:00:03 +02001712 return IRQ_WAKE_THREAD;
Emmanuel Grumbach85bf9da2013-12-09 11:48:30 +02001713}