1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3cefec29eSJohannes Berg  * This file is provided under a dual BSD/GPLv2 license.  When using or
4cefec29eSJohannes Berg  * redistributing this file, you may do so under either license.
5cefec29eSJohannes Berg  *
6cefec29eSJohannes Berg  * GPL LICENSE SUMMARY
7cefec29eSJohannes Berg  *
8e705c121SKalle Valo  * Copyright(c) 2003 - 2015 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10eda50cdeSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
116cc6ba3aSTriebitz  * Copyright(c) 2018 - 2019 Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify it
14e705c121SKalle Valo  * under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but WITHOUT
18e705c121SKalle Valo  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19e705c121SKalle Valo  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20e705c121SKalle Valo  * more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution in the
23cefec29eSJohannes Berg  * file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29cefec29eSJohannes Berg  * BSD LICENSE
30cefec29eSJohannes Berg  *
31cefec29eSJohannes Berg  * Copyright(c) 2003 - 2015 Intel Corporation. All rights reserved.
32cefec29eSJohannes Berg  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33cefec29eSJohannes Berg  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
346cc6ba3aSTriebitz  * Copyright(c) 2018 - 2019 Intel Corporation
35cefec29eSJohannes Berg  * All rights reserved.
36cefec29eSJohannes Berg  *
37cefec29eSJohannes Berg  * Redistribution and use in source and binary forms, with or without
38cefec29eSJohannes Berg  * modification, are permitted provided that the following conditions
39cefec29eSJohannes Berg  * are met:
40cefec29eSJohannes Berg  *
41cefec29eSJohannes Berg  *  * Redistributions of source code must retain the above copyright
42cefec29eSJohannes Berg  *    notice, this list of conditions and the following disclaimer.
43cefec29eSJohannes Berg  *  * Redistributions in binary form must reproduce the above copyright
44cefec29eSJohannes Berg  *    notice, this list of conditions and the following disclaimer in
45cefec29eSJohannes Berg  *    the documentation and/or other materials provided with the
46cefec29eSJohannes Berg  *    distribution.
47cefec29eSJohannes Berg  *  * Neither the name Intel Corporation nor the names of its
48cefec29eSJohannes Berg  *    contributors may be used to endorse or promote products derived
49cefec29eSJohannes Berg  *    from this software without specific prior written permission.
50cefec29eSJohannes Berg  *
51cefec29eSJohannes Berg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52cefec29eSJohannes Berg  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53cefec29eSJohannes Berg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54cefec29eSJohannes Berg  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55cefec29eSJohannes Berg  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56cefec29eSJohannes Berg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57cefec29eSJohannes Berg  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58cefec29eSJohannes Berg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59cefec29eSJohannes Berg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60cefec29eSJohannes Berg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61cefec29eSJohannes Berg  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62cefec29eSJohannes Berg  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo #ifndef __iwl_trans_int_pcie_h__
65e705c121SKalle Valo #define __iwl_trans_int_pcie_h__
66e705c121SKalle Valo 
67e705c121SKalle Valo #include <linux/spinlock.h>
68e705c121SKalle Valo #include <linux/interrupt.h>
69e705c121SKalle Valo #include <linux/skbuff.h>
70e705c121SKalle Valo #include <linux/wait.h>
71e705c121SKalle Valo #include <linux/pci.h>
72e705c121SKalle Valo #include <linux/timer.h>
737c8d91ebSHaim Dreyfuss #include <linux/cpu.h>
74e705c121SKalle Valo 
75e705c121SKalle Valo #include "iwl-fh.h"
76e705c121SKalle Valo #include "iwl-csr.h"
77e705c121SKalle Valo #include "iwl-trans.h"
78e705c121SKalle Valo #include "iwl-debug.h"
79e705c121SKalle Valo #include "iwl-io.h"
80e705c121SKalle Valo #include "iwl-op-mode.h"
81ff932f61SGolan Ben Ami #include "iwl-drv.h"
82e705c121SKalle Valo 
83e705c121SKalle Valo /* We need 2 entries for the TX command and header, and another one might
84e705c121SKalle Valo  * be needed for potential data in the SKB's head. The remaining ones can
85e705c121SKalle Valo  * be used for frags.
86e705c121SKalle Valo  */
873cd1980bSSara Sharon #define IWL_PCIE_MAX_FRAGS(x) (x->max_tbs - 3)
88e705c121SKalle Valo 
89e705c121SKalle Valo /*
90e705c121SKalle Valo  * RX related structures and functions
91e705c121SKalle Valo  */
92e705c121SKalle Valo #define RX_NUM_QUEUES 1
93e705c121SKalle Valo #define RX_POST_REQ_ALLOC 2
94e705c121SKalle Valo #define RX_CLAIM_REQ_ALLOC 8
9578485054SSara Sharon #define RX_PENDING_WATERMARK 16
961b493e30SGolan Ben Ami #define FIRST_RX_QUEUE 512
97e705c121SKalle Valo 
98e705c121SKalle Valo struct iwl_host_cmd;
99e705c121SKalle Valo 
100e705c121SKalle Valo /*This file includes the declaration that are internal to the
101e705c121SKalle Valo  * trans_pcie layer */
102e705c121SKalle Valo 
10396a6497bSSara Sharon /**
10496a6497bSSara Sharon  * struct iwl_rx_mem_buffer
10596a6497bSSara Sharon  * @page_dma: bus address of rxb page
10696a6497bSSara Sharon  * @page: driver's pointer to the rxb page
107b1753c62SSara Sharon  * @invalid: rxb is in driver ownership - not owned by HW
10896a6497bSSara Sharon  * @vid: index of this rxb in the global table
10996a6497bSSara Sharon  */
110e705c121SKalle Valo struct iwl_rx_mem_buffer {
111e705c121SKalle Valo 	dma_addr_t page_dma;
112e705c121SKalle Valo 	struct page *page;
11396a6497bSSara Sharon 	u16 vid;
114b1753c62SSara Sharon 	bool invalid;
115e705c121SKalle Valo 	struct list_head list;
116e705c121SKalle Valo };
117e705c121SKalle Valo 
118e705c121SKalle Valo /**
119e705c121SKalle Valo  * struct isr_statistics - interrupt statistics
120e705c121SKalle Valo  *
121e705c121SKalle Valo  */
122e705c121SKalle Valo struct isr_statistics {
123e705c121SKalle Valo 	u32 hw;
124e705c121SKalle Valo 	u32 sw;
125e705c121SKalle Valo 	u32 err_code;
126e705c121SKalle Valo 	u32 sch;
127e705c121SKalle Valo 	u32 alive;
128e705c121SKalle Valo 	u32 rfkill;
129e705c121SKalle Valo 	u32 ctkill;
130e705c121SKalle Valo 	u32 wakeup;
131e705c121SKalle Valo 	u32 rx;
132e705c121SKalle Valo 	u32 tx;
133e705c121SKalle Valo 	u32 unhandled;
134e705c121SKalle Valo };
135e705c121SKalle Valo 
136cf495496SGolan Ben Ami /**
137cf495496SGolan Ben Ami  * struct iwl_rx_transfer_desc - transfer descriptor
138cf495496SGolan Ben Ami  * @addr: ptr to free buffer start address
139cf495496SGolan Ben Ami  * @rbid: unique tag of the buffer
140cf495496SGolan Ben Ami  * @reserved: reserved
141cf495496SGolan Ben Ami  */
142cf495496SGolan Ben Ami struct iwl_rx_transfer_desc {
143cf495496SGolan Ben Ami 	__le16 rbid;
144f826faaaSJohannes Berg 	__le16 reserved[3];
145f826faaaSJohannes Berg 	__le64 addr;
146cf495496SGolan Ben Ami } __packed;
147cf495496SGolan Ben Ami 
148f826faaaSJohannes Berg #define IWL_RX_CD_FLAGS_FRAGMENTED	BIT(0)
149cf495496SGolan Ben Ami 
150cf495496SGolan Ben Ami /**
151cf495496SGolan Ben Ami  * struct iwl_rx_completion_desc - completion descriptor
152cf495496SGolan Ben Ami  * @reserved1: reserved
153cf495496SGolan Ben Ami  * @rbid: unique tag of the received buffer
154f826faaaSJohannes Berg  * @flags: flags (0: fragmented, all others: reserved)
155cf495496SGolan Ben Ami  * @reserved2: reserved
156cf495496SGolan Ben Ami  */
157cf495496SGolan Ben Ami struct iwl_rx_completion_desc {
158f826faaaSJohannes Berg 	__le32 reserved1;
159cf495496SGolan Ben Ami 	__le16 rbid;
160f826faaaSJohannes Berg 	u8 flags;
161f826faaaSJohannes Berg 	u8 reserved2[25];
162cf495496SGolan Ben Ami } __packed;
163cf495496SGolan Ben Ami 
164e705c121SKalle Valo /**
165e705c121SKalle Valo  * struct iwl_rxq - Rx queue
16696a6497bSSara Sharon  * @id: queue index
16796a6497bSSara Sharon  * @bd: driver's pointer to buffer of receive buffer descriptors (rbd).
16896a6497bSSara Sharon  *	Address size is 32 bit in pre-9000 devices and 64 bit in 9000 devices.
1690307c839SGolan Ben Ami  *	In 22560 devices it is a pointer to a list of iwl_rx_transfer_desc's
170e705c121SKalle Valo  * @bd_dma: bus address of buffer of receive buffer descriptors (rbd)
17196a6497bSSara Sharon  * @ubd: driver's pointer to buffer of used receive buffer descriptors (rbd)
17296a6497bSSara Sharon  * @ubd_dma: physical address of buffer of used receive buffer descriptors (rbd)
1731b493e30SGolan Ben Ami  * @tr_tail: driver's pointer to the transmission ring tail buffer
1741b493e30SGolan Ben Ami  * @tr_tail_dma: physical address of the buffer for the transmission ring tail
1751b493e30SGolan Ben Ami  * @cr_tail: driver's pointer to the completion ring tail buffer
1761b493e30SGolan Ben Ami  * @cr_tail_dma: physical address of the buffer for the completion ring tail
177e705c121SKalle Valo  * @read: Shared index to newest available Rx buffer
178e705c121SKalle Valo  * @write: Shared index to oldest written Rx packet
179e705c121SKalle Valo  * @free_count: Number of pre-allocated buffers in rx_free
180e705c121SKalle Valo  * @used_count: Number of RBDs handled to allocator to use for allocation
181e705c121SKalle Valo  * @write_actual:
182e705c121SKalle Valo  * @rx_free: list of RBDs with allocated RB ready for use
183e705c121SKalle Valo  * @rx_used: list of RBDs with no RB attached
184e705c121SKalle Valo  * @need_update: flag to indicate we need to update read/write index
185e705c121SKalle Valo  * @rb_stts: driver's pointer to receive buffer status
186e705c121SKalle Valo  * @rb_stts_dma: bus address of receive buffer status
187e705c121SKalle Valo  * @lock:
18896a6497bSSara Sharon  * @queue: actual rx queue. Not used for multi-rx queue.
189e705c121SKalle Valo  *
190e705c121SKalle Valo  * NOTE:  rx_free and rx_used are used as a FIFO for iwl_rx_mem_buffers
191e705c121SKalle Valo  */
192e705c121SKalle Valo struct iwl_rxq {
19396a6497bSSara Sharon 	int id;
19496a6497bSSara Sharon 	void *bd;
195e705c121SKalle Valo 	dma_addr_t bd_dma;
196b2a58c97SSara Sharon 	union {
1970307c839SGolan Ben Ami 		void *used_bd;
198b2a58c97SSara Sharon 		__le32 *bd_32;
199b2a58c97SSara Sharon 		struct iwl_rx_completion_desc *cd;
200b2a58c97SSara Sharon 	};
20196a6497bSSara Sharon 	dma_addr_t used_bd_dma;
2021b493e30SGolan Ben Ami 	__le16 *tr_tail;
2031b493e30SGolan Ben Ami 	dma_addr_t tr_tail_dma;
2041b493e30SGolan Ben Ami 	__le16 *cr_tail;
2051b493e30SGolan Ben Ami 	dma_addr_t cr_tail_dma;
206e705c121SKalle Valo 	u32 read;
207e705c121SKalle Valo 	u32 write;
208e705c121SKalle Valo 	u32 free_count;
209e705c121SKalle Valo 	u32 used_count;
210e705c121SKalle Valo 	u32 write_actual;
21196a6497bSSara Sharon 	u32 queue_size;
212e705c121SKalle Valo 	struct list_head rx_free;
213e705c121SKalle Valo 	struct list_head rx_used;
214e705c121SKalle Valo 	bool need_update;
2150307c839SGolan Ben Ami 	void *rb_stts;
216e705c121SKalle Valo 	dma_addr_t rb_stts_dma;
217e705c121SKalle Valo 	spinlock_t lock;
218bce97731SSara Sharon 	struct napi_struct napi;
219e705c121SKalle Valo 	struct iwl_rx_mem_buffer *queue[RX_QUEUE_SIZE];
220e705c121SKalle Valo };
221e705c121SKalle Valo 
222e705c121SKalle Valo /**
223e705c121SKalle Valo  * struct iwl_rb_allocator - Rx allocator
224e705c121SKalle Valo  * @req_pending: number of requests the allcator had not processed yet
225e705c121SKalle Valo  * @req_ready: number of requests honored and ready for claiming
226e705c121SKalle Valo  * @rbd_allocated: RBDs with pages allocated and ready to be handled to
227e705c121SKalle Valo  *	the queue. This is a list of &struct iwl_rx_mem_buffer
228e705c121SKalle Valo  * @rbd_empty: RBDs with no page attached for allocator use. This is a list
229e705c121SKalle Valo  *	of &struct iwl_rx_mem_buffer
230e705c121SKalle Valo  * @lock: protects the rbd_allocated and rbd_empty lists
231e705c121SKalle Valo  * @alloc_wq: work queue for background calls
232e705c121SKalle Valo  * @rx_alloc: work struct for background calls
233e705c121SKalle Valo  */
234e705c121SKalle Valo struct iwl_rb_allocator {
235e705c121SKalle Valo 	atomic_t req_pending;
236e705c121SKalle Valo 	atomic_t req_ready;
237e705c121SKalle Valo 	struct list_head rbd_allocated;
238e705c121SKalle Valo 	struct list_head rbd_empty;
239e705c121SKalle Valo 	spinlock_t lock;
240e705c121SKalle Valo 	struct workqueue_struct *alloc_wq;
241e705c121SKalle Valo 	struct work_struct rx_alloc;
242e705c121SKalle Valo };
243e705c121SKalle Valo 
244e705c121SKalle Valo struct iwl_dma_ptr {
245e705c121SKalle Valo 	dma_addr_t dma;
246e705c121SKalle Valo 	void *addr;
247e705c121SKalle Valo 	size_t size;
248e705c121SKalle Valo };
249e705c121SKalle Valo 
250e705c121SKalle Valo /**
251e705c121SKalle Valo  * iwl_queue_inc_wrap - increment queue index, wrap back to beginning
252e705c121SKalle Valo  * @index -- current index
253e705c121SKalle Valo  */
2547b3e42eaSGolan Ben Ami static inline int iwl_queue_inc_wrap(struct iwl_trans *trans, int index)
255e705c121SKalle Valo {
25679b6c8feSLuca Coelho 	return ++index &
25779b6c8feSLuca Coelho 		(trans->cfg->trans.base_params->max_tfd_queue_size - 1);
258e705c121SKalle Valo }
259e705c121SKalle Valo 
260e705c121SKalle Valo /**
2610307c839SGolan Ben Ami  * iwl_get_closed_rb_stts - get closed rb stts from different structs
2620307c839SGolan Ben Ami  * @rxq - the rxq to get the rb stts from
2630307c839SGolan Ben Ami  */
2640307c839SGolan Ben Ami static inline __le16 iwl_get_closed_rb_stts(struct iwl_trans *trans,
2650307c839SGolan Ben Ami 					    struct iwl_rxq *rxq)
2660307c839SGolan Ben Ami {
26779b6c8feSLuca Coelho 	if (trans->cfg->trans.device_family >= IWL_DEVICE_FAMILY_22560) {
2680307c839SGolan Ben Ami 		__le16 *rb_stts = rxq->rb_stts;
2690307c839SGolan Ben Ami 
2700307c839SGolan Ben Ami 		return READ_ONCE(*rb_stts);
2710307c839SGolan Ben Ami 	} else {
2720307c839SGolan Ben Ami 		struct iwl_rb_status *rb_stts = rxq->rb_stts;
2730307c839SGolan Ben Ami 
2740307c839SGolan Ben Ami 		return READ_ONCE(rb_stts->closed_rb_num);
2750307c839SGolan Ben Ami 	}
2760307c839SGolan Ben Ami }
2770307c839SGolan Ben Ami 
2780307c839SGolan Ben Ami /**
279e705c121SKalle Valo  * iwl_queue_dec_wrap - decrement queue index, wrap back to end
280e705c121SKalle Valo  * @index -- current index
281e705c121SKalle Valo  */
2827b3e42eaSGolan Ben Ami static inline int iwl_queue_dec_wrap(struct iwl_trans *trans, int index)
283e705c121SKalle Valo {
28479b6c8feSLuca Coelho 	return --index &
28579b6c8feSLuca Coelho 		(trans->cfg->trans.base_params->max_tfd_queue_size - 1);
286e705c121SKalle Valo }
287e705c121SKalle Valo 
288e705c121SKalle Valo struct iwl_cmd_meta {
289e705c121SKalle Valo 	/* only for SYNC commands, iff the reply skb is wanted */
290e705c121SKalle Valo 	struct iwl_host_cmd *source;
291e705c121SKalle Valo 	u32 flags;
2923cd1980bSSara Sharon 	u32 tbs;
293e705c121SKalle Valo };
294e705c121SKalle Valo 
295e705c121SKalle Valo /*
2968de437c7SSara Sharon  * The FH will write back to the first TB only, so we need to copy some data
2978de437c7SSara Sharon  * into the buffer regardless of whether it should be mapped or not.
2988de437c7SSara Sharon  * This indicates how big the first TB must be to include the scratch buffer
2998de437c7SSara Sharon  * and the assigned PN.
300b97277ccSSara Sharon  * Since PN location is 8 bytes at offset 12, it's 20 now.
3018de437c7SSara Sharon  * If we make it bigger then allocations will be bigger and copy slower, so
3028de437c7SSara Sharon  * that's probably not useful.
303e705c121SKalle Valo  */
304b97277ccSSara Sharon #define IWL_FIRST_TB_SIZE	20
3058de437c7SSara Sharon #define IWL_FIRST_TB_SIZE_ALIGN ALIGN(IWL_FIRST_TB_SIZE, 64)
306e705c121SKalle Valo 
307e705c121SKalle Valo struct iwl_pcie_txq_entry {
308e705c121SKalle Valo 	struct iwl_device_cmd *cmd;
309e705c121SKalle Valo 	struct sk_buff *skb;
310e705c121SKalle Valo 	/* buffer to free after command completes */
311e705c121SKalle Valo 	const void *free_buf;
312e705c121SKalle Valo 	struct iwl_cmd_meta meta;
313e705c121SKalle Valo };
314e705c121SKalle Valo 
3158de437c7SSara Sharon struct iwl_pcie_first_tb_buf {
3168de437c7SSara Sharon 	u8 buf[IWL_FIRST_TB_SIZE_ALIGN];
317e705c121SKalle Valo };
318e705c121SKalle Valo 
319e705c121SKalle Valo /**
320e705c121SKalle Valo  * struct iwl_txq - Tx Queue for DMA
321e705c121SKalle Valo  * @q: generic Rx/Tx queue descriptor
322e705c121SKalle Valo  * @tfds: transmit frame descriptors (DMA memory)
3238de437c7SSara Sharon  * @first_tb_bufs: start of command headers, including scratch buffers, for
324e705c121SKalle Valo  *	the writeback -- this is DMA memory and an array holding one buffer
325e705c121SKalle Valo  *	for each command on the queue
3268de437c7SSara Sharon  * @first_tb_dma: DMA address for the first_tb_bufs start
327e705c121SKalle Valo  * @entries: transmit entries (driver state)
328e705c121SKalle Valo  * @lock: queue lock
329e705c121SKalle Valo  * @stuck_timer: timer that fires if queue gets stuck
330e705c121SKalle Valo  * @trans_pcie: pointer back to transport (for timer)
331e705c121SKalle Valo  * @need_update: indicates need to update read/write index
332e705c121SKalle Valo  * @ampdu: true if this queue is an ampdu queue for an specific RA/TID
333e705c121SKalle Valo  * @wd_timeout: queue watchdog timeout (jiffies) - per queue
334e705c121SKalle Valo  * @frozen: tx stuck queue timer is frozen
335e705c121SKalle Valo  * @frozen_expiry_remainder: remember how long until the timer fires
33613a3a390SSara Sharon  * @bc_tbl: byte count table of the queue (relevant only for gen2 transport)
337bb98ecd4SSara Sharon  * @write_ptr: 1-st empty entry (index) host_w
338bb98ecd4SSara Sharon  * @read_ptr: last used entry (index) host_r
339bb98ecd4SSara Sharon  * @dma_addr:  physical addr for BD's
340bb98ecd4SSara Sharon  * @n_window: safe queue window
341bb98ecd4SSara Sharon  * @id: queue id
342bb98ecd4SSara Sharon  * @low_mark: low watermark, resume queue if free space more than this
343bb98ecd4SSara Sharon  * @high_mark: high watermark, stop queue if free space less than this
344e705c121SKalle Valo  *
345e705c121SKalle Valo  * A Tx queue consists of circular buffer of BDs (a.k.a. TFDs, transmit frame
346e705c121SKalle Valo  * descriptors) and required locking structures.
347bb98ecd4SSara Sharon  *
348bb98ecd4SSara Sharon  * Note the difference between TFD_QUEUE_SIZE_MAX and n_window: the hardware
349bb98ecd4SSara Sharon  * always assumes 256 descriptors, so TFD_QUEUE_SIZE_MAX is always 256 (unless
350bb98ecd4SSara Sharon  * there might be HW changes in the future). For the normal TX
351bb98ecd4SSara Sharon  * queues, n_window, which is the size of the software queue data
352bb98ecd4SSara Sharon  * is also 256; however, for the command queue, n_window is only
353bb98ecd4SSara Sharon  * 32 since we don't need so many commands pending. Since the HW
354bb98ecd4SSara Sharon  * still uses 256 BDs for DMA though, TFD_QUEUE_SIZE_MAX stays 256.
355bb98ecd4SSara Sharon  * This means that we end up with the following:
356bb98ecd4SSara Sharon  *  HW entries: | 0 | ... | N * 32 | ... | N * 32 + 31 | ... | 255 |
357bb98ecd4SSara Sharon  *  SW entries:           | 0      | ... | 31          |
358bb98ecd4SSara Sharon  * where N is a number between 0 and 7. This means that the SW
359bb98ecd4SSara Sharon  * data is a window overlayed over the HW queue.
360e705c121SKalle Valo  */
361e705c121SKalle Valo struct iwl_txq {
3626983ba69SSara Sharon 	void *tfds;
3638de437c7SSara Sharon 	struct iwl_pcie_first_tb_buf *first_tb_bufs;
3648de437c7SSara Sharon 	dma_addr_t first_tb_dma;
365e705c121SKalle Valo 	struct iwl_pcie_txq_entry *entries;
366e705c121SKalle Valo 	spinlock_t lock;
367e705c121SKalle Valo 	unsigned long frozen_expiry_remainder;
368e705c121SKalle Valo 	struct timer_list stuck_timer;
369e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie;
370e705c121SKalle Valo 	bool need_update;
371e705c121SKalle Valo 	bool frozen;
372e705c121SKalle Valo 	bool ampdu;
37304fa3e68SEmmanuel Grumbach 	int block;
374e705c121SKalle Valo 	unsigned long wd_timeout;
3753955525dSEmmanuel Grumbach 	struct sk_buff_head overflow_q;
37613a3a390SSara Sharon 	struct iwl_dma_ptr bc_tbl;
377bb98ecd4SSara Sharon 
378bb98ecd4SSara Sharon 	int write_ptr;
379bb98ecd4SSara Sharon 	int read_ptr;
380bb98ecd4SSara Sharon 	dma_addr_t dma_addr;
381bb98ecd4SSara Sharon 	int n_window;
382bb98ecd4SSara Sharon 	u32 id;
383bb98ecd4SSara Sharon 	int low_mark;
384bb98ecd4SSara Sharon 	int high_mark;
3852ae48edcSSara Sharon 
3862ae48edcSSara Sharon 	bool overflow_tx;
387e705c121SKalle Valo };
388e705c121SKalle Valo 
389e705c121SKalle Valo static inline dma_addr_t
3908de437c7SSara Sharon iwl_pcie_get_first_tb_dma(struct iwl_txq *txq, int idx)
391e705c121SKalle Valo {
3928de437c7SSara Sharon 	return txq->first_tb_dma +
3938de437c7SSara Sharon 	       sizeof(struct iwl_pcie_first_tb_buf) * idx;
394e705c121SKalle Valo }
395e705c121SKalle Valo 
3966eb5e529SEmmanuel Grumbach struct iwl_tso_hdr_page {
3976eb5e529SEmmanuel Grumbach 	struct page *page;
3986eb5e529SEmmanuel Grumbach 	u8 *pos;
3996eb5e529SEmmanuel Grumbach };
4006eb5e529SEmmanuel Grumbach 
401f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS
402f7805b33SLior Cohen /**
403f7805b33SLior Cohen  * enum iwl_fw_mon_dbgfs_state - the different states of the monitor_data
404f7805b33SLior Cohen  * debugfs file
405f7805b33SLior Cohen  *
406f7805b33SLior Cohen  * @IWL_FW_MON_DBGFS_STATE_CLOSED: the file is closed.
407f7805b33SLior Cohen  * @IWL_FW_MON_DBGFS_STATE_OPEN: the file is open.
408f7805b33SLior Cohen  * @IWL_FW_MON_DBGFS_STATE_DISABLED: the file is disabled, once this state is
409f7805b33SLior Cohen  *	set the file can no longer be used.
410f7805b33SLior Cohen  */
411f7805b33SLior Cohen enum iwl_fw_mon_dbgfs_state {
412f7805b33SLior Cohen 	IWL_FW_MON_DBGFS_STATE_CLOSED,
413f7805b33SLior Cohen 	IWL_FW_MON_DBGFS_STATE_OPEN,
414f7805b33SLior Cohen 	IWL_FW_MON_DBGFS_STATE_DISABLED,
415f7805b33SLior Cohen };
416f7805b33SLior Cohen #endif
417f7805b33SLior Cohen 
418e705c121SKalle Valo /**
419496d83caSHaim Dreyfuss  * enum iwl_shared_irq_flags - level of sharing for irq
420496d83caSHaim Dreyfuss  * @IWL_SHARED_IRQ_NON_RX: interrupt vector serves non rx causes.
421496d83caSHaim Dreyfuss  * @IWL_SHARED_IRQ_FIRST_RSS: interrupt vector serves first RSS queue.
422496d83caSHaim Dreyfuss  */
423496d83caSHaim Dreyfuss enum iwl_shared_irq_flags {
424496d83caSHaim Dreyfuss 	IWL_SHARED_IRQ_NON_RX		= BIT(0),
425496d83caSHaim Dreyfuss 	IWL_SHARED_IRQ_FIRST_RSS	= BIT(1),
426496d83caSHaim Dreyfuss };
427496d83caSHaim Dreyfuss 
428496d83caSHaim Dreyfuss /**
4299b58419eSGolan Ben Ami  * enum iwl_image_response_code - image response values
4309b58419eSGolan Ben Ami  * @IWL_IMAGE_RESP_DEF: the default value of the register
4319b58419eSGolan Ben Ami  * @IWL_IMAGE_RESP_SUCCESS: iml was read successfully
4329b58419eSGolan Ben Ami  * @IWL_IMAGE_RESP_FAIL: iml reading failed
4339b58419eSGolan Ben Ami  */
4349b58419eSGolan Ben Ami enum iwl_image_response_code {
4359b58419eSGolan Ben Ami 	IWL_IMAGE_RESP_DEF		= 0,
4369b58419eSGolan Ben Ami 	IWL_IMAGE_RESP_SUCCESS		= 1,
4379b58419eSGolan Ben Ami 	IWL_IMAGE_RESP_FAIL		= 2,
4389b58419eSGolan Ben Ami };
4399b58419eSGolan Ben Ami 
4409b58419eSGolan Ben Ami /**
441f7805b33SLior Cohen  * struct cont_rec: continuous recording data structure
442f7805b33SLior Cohen  * @prev_wr_ptr: the last address that was read in monitor_data
443f7805b33SLior Cohen  *	debugfs file
444f7805b33SLior Cohen  * @prev_wrap_cnt: the wrap count that was used during the last read in
445f7805b33SLior Cohen  *	monitor_data debugfs file
446f7805b33SLior Cohen  * @state: the state of monitor_data debugfs file as described
447f7805b33SLior Cohen  *	in &iwl_fw_mon_dbgfs_state enum
448f7805b33SLior Cohen  * @mutex: locked while reading from monitor_data debugfs file
449f7805b33SLior Cohen  */
450f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS
451f7805b33SLior Cohen struct cont_rec {
452f7805b33SLior Cohen 	u32 prev_wr_ptr;
453f7805b33SLior Cohen 	u32 prev_wrap_cnt;
454f7805b33SLior Cohen 	u8  state;
455f7805b33SLior Cohen 	/* Used to sync monitor_data debugfs file with driver unload flow */
456f7805b33SLior Cohen 	struct mutex mutex;
457f7805b33SLior Cohen };
458f7805b33SLior Cohen #endif
459f7805b33SLior Cohen 
460f7805b33SLior Cohen /**
461e705c121SKalle Valo  * struct iwl_trans_pcie - PCIe transport specific data
462e705c121SKalle Valo  * @rxq: all the RX queue data
46378485054SSara Sharon  * @rx_pool: initial pool of iwl_rx_mem_buffer for all the queues
46496a6497bSSara Sharon  * @global_table: table mapping received VID from hw to rxb
465e705c121SKalle Valo  * @rba: allocator for RX replenishing
466eda50cdeSSara Sharon  * @ctxt_info: context information for FW self init
4672ee82402SGolan Ben Ami  * @ctxt_info_gen3: context information for gen3 devices
4682ee82402SGolan Ben Ami  * @prph_info: prph info for self init
4692ee82402SGolan Ben Ami  * @prph_scratch: prph scratch for self init
4702ee82402SGolan Ben Ami  * @ctxt_info_dma_addr: dma addr of context information
4712ee82402SGolan Ben Ami  * @prph_info_dma_addr: dma addr of prph info
4722ee82402SGolan Ben Ami  * @prph_scratch_dma_addr: dma addr of prph scratch
473eda50cdeSSara Sharon  * @ctxt_info_dma_addr: dma addr of context information
474eda50cdeSSara Sharon  * @init_dram: DRAM data of firmware image (including paging).
475eda50cdeSSara Sharon  *	Context information addresses will be taken from here.
476eda50cdeSSara Sharon  *	This is driver's local copy for keeping track of size and
477eda50cdeSSara Sharon  *	count for allocating and freeing the memory.
478e705c121SKalle Valo  * @trans: pointer to the generic transport area
479e705c121SKalle Valo  * @scd_base_addr: scheduler sram base address in SRAM
480e705c121SKalle Valo  * @scd_bc_tbls: pointer to the byte count table of the scheduler
481e705c121SKalle Valo  * @kw: keep warm address
482e705c121SKalle Valo  * @pci_dev: basic pci-network driver stuff
483e705c121SKalle Valo  * @hw_base: pci hardware address support
484e705c121SKalle Valo  * @ucode_write_complete: indicates that the ucode has been copied.
485e705c121SKalle Valo  * @ucode_write_waitq: wait queue for uCode load
486e705c121SKalle Valo  * @cmd_queue - command queue number
4879416560eSGolan Ben Ami  * @def_rx_queue - default rx queue number
4886c4fbcbcSEmmanuel Grumbach  * @rx_buf_size: Rx buffer size
489e705c121SKalle Valo  * @bc_table_dword: true if the BC table expects DWORD (as opposed to bytes)
490e705c121SKalle Valo  * @scd_set_active: should the transport configure the SCD for HCMD queue
49141837ca9SEmmanuel Grumbach  * @sw_csum_tx: if true, then the transport will compute the csum of the TXed
49241837ca9SEmmanuel Grumbach  *	frame.
493e705c121SKalle Valo  * @rx_page_order: page order for receive buffer size
494e705c121SKalle Valo  * @reg_lock: protect hw register access
495e705c121SKalle Valo  * @mutex: to protect stop_device / start_fw / start_hw
496e705c121SKalle Valo  * @cmd_in_flight: true when we have a host command in flight
497f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS
498f7805b33SLior Cohen  * @fw_mon_data: fw continuous recording data
499f7805b33SLior Cohen #endif
5002e5d4a8fSHaim Dreyfuss  * @msix_entries: array of MSI-X entries
5012e5d4a8fSHaim Dreyfuss  * @msix_enabled: true if managed to enable MSI-X
502496d83caSHaim Dreyfuss  * @shared_vec_mask: the type of causes the shared vector handles
503496d83caSHaim Dreyfuss  *	(see iwl_shared_irq_flags).
504496d83caSHaim Dreyfuss  * @alloc_vecs: the number of interrupt vectors allocated by the OS
505496d83caSHaim Dreyfuss  * @def_irq: default irq for non rx causes
5062e5d4a8fSHaim Dreyfuss  * @fh_init_mask: initial unmasked fh causes
5072e5d4a8fSHaim Dreyfuss  * @hw_init_mask: initial unmasked hw causes
5082e5d4a8fSHaim Dreyfuss  * @fh_mask: current unmasked fh causes
5092e5d4a8fSHaim Dreyfuss  * @hw_mask: current unmasked hw causes
51049564a80SLuca Coelho  * @in_rescan: true if we have triggered a device rescan
5116cc6ba3aSTriebitz  * @base_rb_stts: base virtual address of receive buffer status for all queues
5126cc6ba3aSTriebitz  * @base_rb_stts_dma: base physical address of receive buffer status
513e705c121SKalle Valo  */
514e705c121SKalle Valo struct iwl_trans_pcie {
51578485054SSara Sharon 	struct iwl_rxq *rxq;
5167b542436SSara Sharon 	struct iwl_rx_mem_buffer rx_pool[RX_POOL_SIZE];
51743146925SSara Sharon 	struct iwl_rx_mem_buffer *global_table[RX_POOL_SIZE];
518e705c121SKalle Valo 	struct iwl_rb_allocator rba;
5192ee82402SGolan Ben Ami 	union {
520eda50cdeSSara Sharon 		struct iwl_context_info *ctxt_info;
5212ee82402SGolan Ben Ami 		struct iwl_context_info_gen3 *ctxt_info_gen3;
5222ee82402SGolan Ben Ami 	};
5232ee82402SGolan Ben Ami 	struct iwl_prph_info *prph_info;
5242ee82402SGolan Ben Ami 	struct iwl_prph_scratch *prph_scratch;
525eda50cdeSSara Sharon 	dma_addr_t ctxt_info_dma_addr;
5262ee82402SGolan Ben Ami 	dma_addr_t prph_info_dma_addr;
5272ee82402SGolan Ben Ami 	dma_addr_t prph_scratch_dma_addr;
5282ee82402SGolan Ben Ami 	dma_addr_t iml_dma_addr;
529e705c121SKalle Valo 	struct iwl_trans *trans;
530e705c121SKalle Valo 
531e705c121SKalle Valo 	struct net_device napi_dev;
532e705c121SKalle Valo 
5336eb5e529SEmmanuel Grumbach 	struct __percpu iwl_tso_hdr_page *tso_hdr_page;
5346eb5e529SEmmanuel Grumbach 
535e705c121SKalle Valo 	/* INT ICT Table */
536e705c121SKalle Valo 	__le32 *ict_tbl;
537e705c121SKalle Valo 	dma_addr_t ict_tbl_dma;
538e705c121SKalle Valo 	int ict_index;
539e705c121SKalle Valo 	bool use_ict;
540326477e4SJohannes Berg 	bool is_down, opmode_down;
541c5bf4fa1SJohannes Berg 	s8 debug_rfkill;
542e705c121SKalle Valo 	struct isr_statistics isr_stats;
543e705c121SKalle Valo 
544e705c121SKalle Valo 	spinlock_t irq_lock;
545e705c121SKalle Valo 	struct mutex mutex;
546e705c121SKalle Valo 	u32 inta_mask;
547e705c121SKalle Valo 	u32 scd_base_addr;
548e705c121SKalle Valo 	struct iwl_dma_ptr scd_bc_tbls;
549e705c121SKalle Valo 	struct iwl_dma_ptr kw;
550e705c121SKalle Valo 
551b2a3b1c1SSara Sharon 	struct iwl_txq *txq_memory;
552e982bc2cSSara Sharon 	struct iwl_txq *txq[IWL_MAX_TVQM_QUEUES];
553e982bc2cSSara Sharon 	unsigned long queue_used[BITS_TO_LONGS(IWL_MAX_TVQM_QUEUES)];
554e982bc2cSSara Sharon 	unsigned long queue_stopped[BITS_TO_LONGS(IWL_MAX_TVQM_QUEUES)];
555e705c121SKalle Valo 
556e705c121SKalle Valo 	/* PCI bus related data */
557e705c121SKalle Valo 	struct pci_dev *pci_dev;
558e705c121SKalle Valo 	void __iomem *hw_base;
559e705c121SKalle Valo 
560e705c121SKalle Valo 	bool ucode_write_complete;
561e705c121SKalle Valo 	wait_queue_head_t ucode_write_waitq;
562e705c121SKalle Valo 	wait_queue_head_t wait_command_queue;
563e705c121SKalle Valo 
56421cb3222SJohannes Berg 	u8 page_offs, dev_cmd_offs;
56521cb3222SJohannes Berg 
566e705c121SKalle Valo 	u8 cmd_queue;
5679416560eSGolan Ben Ami 	u8 def_rx_queue;
568e705c121SKalle Valo 	u8 cmd_fifo;
569e705c121SKalle Valo 	unsigned int cmd_q_wdg_timeout;
570e705c121SKalle Valo 	u8 n_no_reclaim_cmds;
571e705c121SKalle Valo 	u8 no_reclaim_cmds[MAX_NO_RECLAIM_CMDS];
5723cd1980bSSara Sharon 	u8 max_tbs;
5736983ba69SSara Sharon 	u16 tfd_size;
574e705c121SKalle Valo 
5756c4fbcbcSEmmanuel Grumbach 	enum iwl_amsdu_size rx_buf_size;
576e705c121SKalle Valo 	bool bc_table_dword;
577e705c121SKalle Valo 	bool scd_set_active;
57841837ca9SEmmanuel Grumbach 	bool sw_csum_tx;
579a6d24fadSRajat Jain 	bool pcie_dbg_dumped_once;
580e705c121SKalle Valo 	u32 rx_page_order;
581e705c121SKalle Valo 
582e705c121SKalle Valo 	/*protect hw register */
583e705c121SKalle Valo 	spinlock_t reg_lock;
584e705c121SKalle Valo 	bool cmd_hold_nic_awake;
585e705c121SKalle Valo 
586f7805b33SLior Cohen #ifdef CONFIG_IWLWIFI_DEBUGFS
587f7805b33SLior Cohen 	struct cont_rec fw_mon_data;
588f7805b33SLior Cohen #endif
589f7805b33SLior Cohen 
5902e5d4a8fSHaim Dreyfuss 	struct msix_entry msix_entries[IWL_MAX_RX_HW_QUEUES];
5912e5d4a8fSHaim Dreyfuss 	bool msix_enabled;
592496d83caSHaim Dreyfuss 	u8 shared_vec_mask;
593496d83caSHaim Dreyfuss 	u32 alloc_vecs;
594496d83caSHaim Dreyfuss 	u32 def_irq;
5952e5d4a8fSHaim Dreyfuss 	u32 fh_init_mask;
5962e5d4a8fSHaim Dreyfuss 	u32 hw_init_mask;
5972e5d4a8fSHaim Dreyfuss 	u32 fh_mask;
5982e5d4a8fSHaim Dreyfuss 	u32 hw_mask;
5997c8d91ebSHaim Dreyfuss 	cpumask_t affinity_mask[IWL_MAX_RX_HW_QUEUES];
60049564a80SLuca Coelho 	u16 tx_cmd_queue_size;
60149564a80SLuca Coelho 	bool in_rescan;
6026cc6ba3aSTriebitz 
6036cc6ba3aSTriebitz 	void *base_rb_stts;
6046cc6ba3aSTriebitz 	dma_addr_t base_rb_stts_dma;
605e705c121SKalle Valo };
606e705c121SKalle Valo 
60785e5a387SJohannes Berg static inline struct iwl_trans_pcie *
60885e5a387SJohannes Berg IWL_TRANS_GET_PCIE_TRANS(struct iwl_trans *trans)
60985e5a387SJohannes Berg {
61085e5a387SJohannes Berg 	return (void *)trans->trans_specific;
61185e5a387SJohannes Berg }
612e705c121SKalle Valo 
613ff932f61SGolan Ben Ami static inline void iwl_pcie_clear_irq(struct iwl_trans *trans,
614ff932f61SGolan Ben Ami 				      struct msix_entry *entry)
615ff932f61SGolan Ben Ami {
616ff932f61SGolan Ben Ami 	/*
617ff932f61SGolan Ben Ami 	 * Before sending the interrupt the HW disables it to prevent
618ff932f61SGolan Ben Ami 	 * a nested interrupt. This is done by writing 1 to the corresponding
619ff932f61SGolan Ben Ami 	 * bit in the mask register. After handling the interrupt, it should be
620ff932f61SGolan Ben Ami 	 * re-enabled by clearing this bit. This register is defined as
621ff932f61SGolan Ben Ami 	 * write 1 clear (W1C) register, meaning that it's being clear
622ff932f61SGolan Ben Ami 	 * by writing 1 to the bit.
623ff932f61SGolan Ben Ami 	 */
624ff932f61SGolan Ben Ami 	iwl_write32(trans, CSR_MSIX_AUTOMASK_ST_AD, BIT(entry->entry));
625ff932f61SGolan Ben Ami }
626ff932f61SGolan Ben Ami 
627e705c121SKalle Valo static inline struct iwl_trans *
628e705c121SKalle Valo iwl_trans_pcie_get_trans(struct iwl_trans_pcie *trans_pcie)
629e705c121SKalle Valo {
630e705c121SKalle Valo 	return container_of((void *)trans_pcie, struct iwl_trans,
631e705c121SKalle Valo 			    trans_specific);
632e705c121SKalle Valo }
633e705c121SKalle Valo 
634e705c121SKalle Valo /*
635e705c121SKalle Valo  * Convention: trans API functions: iwl_trans_pcie_XXX
636e705c121SKalle Valo  *	Other functions: iwl_pcie_XXX
637e705c121SKalle Valo  */
6387e8258c0SLuca Coelho struct iwl_trans
6397e8258c0SLuca Coelho *iwl_trans_pcie_alloc(struct pci_dev *pdev,
640e705c121SKalle Valo 		      const struct pci_device_id *ent,
6417e8258c0SLuca Coelho 		      const struct iwl_cfg_trans_params *cfg_trans);
642e705c121SKalle Valo void iwl_trans_pcie_free(struct iwl_trans *trans);
643e705c121SKalle Valo 
644e705c121SKalle Valo /*****************************************************
645e705c121SKalle Valo * RX
646e705c121SKalle Valo ******************************************************/
64789d5e833SGolan Ben Ami int _iwl_pcie_rx_init(struct iwl_trans *trans);
648e705c121SKalle Valo int iwl_pcie_rx_init(struct iwl_trans *trans);
649eda50cdeSSara Sharon int iwl_pcie_gen2_rx_init(struct iwl_trans *trans);
6502e5d4a8fSHaim Dreyfuss irqreturn_t iwl_pcie_msix_isr(int irq, void *data);
651e705c121SKalle Valo irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id);
6522e5d4a8fSHaim Dreyfuss irqreturn_t iwl_pcie_irq_msix_handler(int irq, void *dev_id);
6532e5d4a8fSHaim Dreyfuss irqreturn_t iwl_pcie_irq_rx_msix_handler(int irq, void *dev_id);
654e705c121SKalle Valo int iwl_pcie_rx_stop(struct iwl_trans *trans);
655e705c121SKalle Valo void iwl_pcie_rx_free(struct iwl_trans *trans);
656ff932f61SGolan Ben Ami void iwl_pcie_free_rbs_pool(struct iwl_trans *trans);
657ff932f61SGolan Ben Ami void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq);
658ff932f61SGolan Ben Ami int iwl_pcie_dummy_napi_poll(struct napi_struct *napi, int budget);
659ff932f61SGolan Ben Ami void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,
660ff932f61SGolan Ben Ami 			    struct iwl_rxq *rxq);
66189d5e833SGolan Ben Ami int iwl_pcie_rx_alloc(struct iwl_trans *trans);
662e705c121SKalle Valo 
663e705c121SKalle Valo /*****************************************************
664e705c121SKalle Valo * ICT - interrupt handling
665e705c121SKalle Valo ******************************************************/
666e705c121SKalle Valo irqreturn_t iwl_pcie_isr(int irq, void *data);
667e705c121SKalle Valo int iwl_pcie_alloc_ict(struct iwl_trans *trans);
668e705c121SKalle Valo void iwl_pcie_free_ict(struct iwl_trans *trans);
669e705c121SKalle Valo void iwl_pcie_reset_ict(struct iwl_trans *trans);
670e705c121SKalle Valo void iwl_pcie_disable_ict(struct iwl_trans *trans);
671e705c121SKalle Valo 
672e705c121SKalle Valo /*****************************************************
673e705c121SKalle Valo * TX / HCMD
674e705c121SKalle Valo ******************************************************/
675e705c121SKalle Valo int iwl_pcie_tx_init(struct iwl_trans *trans);
6769b3089bdSGolan Ben Ami int iwl_pcie_gen2_tx_init(struct iwl_trans *trans, int txq_id,
6779b3089bdSGolan Ben Ami 			  int queue_size);
678e705c121SKalle Valo void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr);
679e705c121SKalle Valo int iwl_pcie_tx_stop(struct iwl_trans *trans);
680e705c121SKalle Valo void iwl_pcie_tx_free(struct iwl_trans *trans);
681dcfbd67bSEmmanuel Grumbach bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int queue, u16 ssn,
682e705c121SKalle Valo 			       const struct iwl_trans_txq_scd_cfg *cfg,
683e705c121SKalle Valo 			       unsigned int wdg_timeout);
684e705c121SKalle Valo void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int queue,
685e705c121SKalle Valo 				bool configure_scd);
68642db09c1SLiad Kaufman void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
68742db09c1SLiad Kaufman 					bool shared_mode);
68838398efbSSara Sharon void iwl_trans_pcie_log_scd_error(struct iwl_trans *trans,
68938398efbSSara Sharon 				  struct iwl_txq *txq);
690e705c121SKalle Valo int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
691e705c121SKalle Valo 		      struct iwl_device_cmd *dev_cmd, int txq_id);
692e705c121SKalle Valo void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans);
693e705c121SKalle Valo int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
69489d5e833SGolan Ben Ami void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx);
69589d5e833SGolan Ben Ami void iwl_pcie_gen2_txq_inc_wr_ptr(struct iwl_trans *trans,
69689d5e833SGolan Ben Ami 				  struct iwl_txq *txq);
697e705c121SKalle Valo void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
698e705c121SKalle Valo 			    struct iwl_rx_cmd_buffer *rxb);
699e705c121SKalle Valo void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
700e705c121SKalle Valo 			    struct sk_buff_head *skbs);
701ba7136f3SAlex Malamud void iwl_trans_pcie_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr);
702e705c121SKalle Valo void iwl_trans_pcie_tx_reset(struct iwl_trans *trans);
70389d5e833SGolan Ben Ami void iwl_pcie_gen2_update_byte_tbl(struct iwl_trans_pcie *trans_pcie,
70489d5e833SGolan Ben Ami 				   struct iwl_txq *txq, u16 byte_cnt,
70589d5e833SGolan Ben Ami 				   int num_tbs);
706e705c121SKalle Valo 
707cc2f41f8SJohannes Berg static inline u16 iwl_pcie_tfd_tb_get_len(struct iwl_trans *trans, void *_tfd,
7086983ba69SSara Sharon 					  u8 idx)
709e705c121SKalle Valo {
71079b6c8feSLuca Coelho 	if (trans->cfg->trans.use_tfh) {
711cc2f41f8SJohannes Berg 		struct iwl_tfh_tfd *tfd = _tfd;
712cc2f41f8SJohannes Berg 		struct iwl_tfh_tb *tb = &tfd->tbs[idx];
7136983ba69SSara Sharon 
7146983ba69SSara Sharon 		return le16_to_cpu(tb->tb_len);
715cc2f41f8SJohannes Berg 	} else {
716cc2f41f8SJohannes Berg 		struct iwl_tfd *tfd = _tfd;
717cc2f41f8SJohannes Berg 		struct iwl_tfd_tb *tb = &tfd->tbs[idx];
718e705c121SKalle Valo 
719e705c121SKalle Valo 		return le16_to_cpu(tb->hi_n_len) >> 4;
720e705c121SKalle Valo 	}
721cc2f41f8SJohannes Berg }
722e705c121SKalle Valo 
723e705c121SKalle Valo /*****************************************************
724e705c121SKalle Valo * Error handling
725e705c121SKalle Valo ******************************************************/
726e705c121SKalle Valo void iwl_pcie_dump_csr(struct iwl_trans *trans);
727e705c121SKalle Valo 
728e705c121SKalle Valo /*****************************************************
729e705c121SKalle Valo * Helpers
730e705c121SKalle Valo ******************************************************/
731f16c3ebfSEmmanuel Grumbach static inline void _iwl_disable_interrupts(struct iwl_trans *trans)
732e705c121SKalle Valo {
7332e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
734e705c121SKalle Valo 
7352e5d4a8fSHaim Dreyfuss 	clear_bit(STATUS_INT_ENABLED, &trans->status);
7362e5d4a8fSHaim Dreyfuss 	if (!trans_pcie->msix_enabled) {
737e705c121SKalle Valo 		/* disable interrupts from uCode/NIC to host */
738e705c121SKalle Valo 		iwl_write32(trans, CSR_INT_MASK, 0x00000000);
739e705c121SKalle Valo 
740e705c121SKalle Valo 		/* acknowledge/clear/reset any interrupts still pending
741e705c121SKalle Valo 		 * from uCode or flow handler (Rx/Tx DMA) */
742e705c121SKalle Valo 		iwl_write32(trans, CSR_INT, 0xffffffff);
743e705c121SKalle Valo 		iwl_write32(trans, CSR_FH_INT_STATUS, 0xffffffff);
7442e5d4a8fSHaim Dreyfuss 	} else {
7452e5d4a8fSHaim Dreyfuss 		/* disable all the interrupt we might use */
7462e5d4a8fSHaim Dreyfuss 		iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,
7472e5d4a8fSHaim Dreyfuss 			    trans_pcie->fh_init_mask);
7482e5d4a8fSHaim Dreyfuss 		iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,
7492e5d4a8fSHaim Dreyfuss 			    trans_pcie->hw_init_mask);
7502e5d4a8fSHaim Dreyfuss 	}
751e705c121SKalle Valo 	IWL_DEBUG_ISR(trans, "Disabled interrupts\n");
752e705c121SKalle Valo }
753e705c121SKalle Valo 
7542ee82402SGolan Ben Ami #define IWL_NUM_OF_COMPLETION_RINGS	31
7552ee82402SGolan Ben Ami #define IWL_NUM_OF_TRANSFER_RINGS	527
7562ee82402SGolan Ben Ami 
7572ee82402SGolan Ben Ami static inline int iwl_pcie_get_num_sections(const struct fw_img *fw,
7582ee82402SGolan Ben Ami 					    int start)
7592ee82402SGolan Ben Ami {
7602ee82402SGolan Ben Ami 	int i = 0;
7612ee82402SGolan Ben Ami 
7622ee82402SGolan Ben Ami 	while (start < fw->num_sec &&
7632ee82402SGolan Ben Ami 	       fw->sec[start].offset != CPU1_CPU2_SEPARATOR_SECTION &&
7642ee82402SGolan Ben Ami 	       fw->sec[start].offset != PAGING_SEPARATOR_SECTION) {
7652ee82402SGolan Ben Ami 		start++;
7662ee82402SGolan Ben Ami 		i++;
7672ee82402SGolan Ben Ami 	}
7682ee82402SGolan Ben Ami 
7692ee82402SGolan Ben Ami 	return i;
7702ee82402SGolan Ben Ami }
7712ee82402SGolan Ben Ami 
7722ee82402SGolan Ben Ami static inline int iwl_pcie_ctxt_info_alloc_dma(struct iwl_trans *trans,
7732ee82402SGolan Ben Ami 					       const struct fw_desc *sec,
7742ee82402SGolan Ben Ami 					       struct iwl_dram_data *dram)
7752ee82402SGolan Ben Ami {
7762ee82402SGolan Ben Ami 	dram->block = dma_alloc_coherent(trans->dev, sec->len,
7772ee82402SGolan Ben Ami 					 &dram->physical,
7782ee82402SGolan Ben Ami 					 GFP_KERNEL);
7792ee82402SGolan Ben Ami 	if (!dram->block)
7802ee82402SGolan Ben Ami 		return -ENOMEM;
7812ee82402SGolan Ben Ami 
7822ee82402SGolan Ben Ami 	dram->size = sec->len;
7832ee82402SGolan Ben Ami 	memcpy(dram->block, sec->data, sec->len);
7842ee82402SGolan Ben Ami 
7852ee82402SGolan Ben Ami 	return 0;
7862ee82402SGolan Ben Ami }
7872ee82402SGolan Ben Ami 
7882ee82402SGolan Ben Ami static inline void iwl_pcie_ctxt_info_free_fw_img(struct iwl_trans *trans)
7892ee82402SGolan Ben Ami {
790505a00c0SShahar S Matityahu 	struct iwl_self_init_dram *dram = &trans->init_dram;
7912ee82402SGolan Ben Ami 	int i;
7922ee82402SGolan Ben Ami 
7932ee82402SGolan Ben Ami 	if (!dram->fw) {
7942ee82402SGolan Ben Ami 		WARN_ON(dram->fw_cnt);
7952ee82402SGolan Ben Ami 		return;
7962ee82402SGolan Ben Ami 	}
7972ee82402SGolan Ben Ami 
7982ee82402SGolan Ben Ami 	for (i = 0; i < dram->fw_cnt; i++)
7992ee82402SGolan Ben Ami 		dma_free_coherent(trans->dev, dram->fw[i].size,
8002ee82402SGolan Ben Ami 				  dram->fw[i].block, dram->fw[i].physical);
8012ee82402SGolan Ben Ami 
8022ee82402SGolan Ben Ami 	kfree(dram->fw);
8032ee82402SGolan Ben Ami 	dram->fw_cnt = 0;
8042ee82402SGolan Ben Ami 	dram->fw = NULL;
8052ee82402SGolan Ben Ami }
8062ee82402SGolan Ben Ami 
807f16c3ebfSEmmanuel Grumbach static inline void iwl_disable_interrupts(struct iwl_trans *trans)
808f16c3ebfSEmmanuel Grumbach {
809f16c3ebfSEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
810f16c3ebfSEmmanuel Grumbach 
811f16c3ebfSEmmanuel Grumbach 	spin_lock(&trans_pcie->irq_lock);
812f16c3ebfSEmmanuel Grumbach 	_iwl_disable_interrupts(trans);
813f16c3ebfSEmmanuel Grumbach 	spin_unlock(&trans_pcie->irq_lock);
814f16c3ebfSEmmanuel Grumbach }
815f16c3ebfSEmmanuel Grumbach 
816f16c3ebfSEmmanuel Grumbach static inline void _iwl_enable_interrupts(struct iwl_trans *trans)
817e705c121SKalle Valo {
818e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
819e705c121SKalle Valo 
820e705c121SKalle Valo 	IWL_DEBUG_ISR(trans, "Enabling interrupts\n");
821e705c121SKalle Valo 	set_bit(STATUS_INT_ENABLED, &trans->status);
8222e5d4a8fSHaim Dreyfuss 	if (!trans_pcie->msix_enabled) {
823e705c121SKalle Valo 		trans_pcie->inta_mask = CSR_INI_SET_MASK;
824e705c121SKalle Valo 		iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
8252e5d4a8fSHaim Dreyfuss 	} else {
8262e5d4a8fSHaim Dreyfuss 		/*
8272e5d4a8fSHaim Dreyfuss 		 * fh/hw_mask keeps all the unmasked causes.
8282e5d4a8fSHaim Dreyfuss 		 * Unlike msi, in msix cause is enabled when it is unset.
8292e5d4a8fSHaim Dreyfuss 		 */
8302e5d4a8fSHaim Dreyfuss 		trans_pcie->hw_mask = trans_pcie->hw_init_mask;
8312e5d4a8fSHaim Dreyfuss 		trans_pcie->fh_mask = trans_pcie->fh_init_mask;
8322e5d4a8fSHaim Dreyfuss 		iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,
8332e5d4a8fSHaim Dreyfuss 			    ~trans_pcie->fh_mask);
8342e5d4a8fSHaim Dreyfuss 		iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,
8352e5d4a8fSHaim Dreyfuss 			    ~trans_pcie->hw_mask);
8362e5d4a8fSHaim Dreyfuss 	}
8372e5d4a8fSHaim Dreyfuss }
8382e5d4a8fSHaim Dreyfuss 
839f16c3ebfSEmmanuel Grumbach static inline void iwl_enable_interrupts(struct iwl_trans *trans)
840f16c3ebfSEmmanuel Grumbach {
841f16c3ebfSEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
842f16c3ebfSEmmanuel Grumbach 
843f16c3ebfSEmmanuel Grumbach 	spin_lock(&trans_pcie->irq_lock);
844f16c3ebfSEmmanuel Grumbach 	_iwl_enable_interrupts(trans);
845f16c3ebfSEmmanuel Grumbach 	spin_unlock(&trans_pcie->irq_lock);
846f16c3ebfSEmmanuel Grumbach }
8472e5d4a8fSHaim Dreyfuss static inline void iwl_enable_hw_int_msk_msix(struct iwl_trans *trans, u32 msk)
8482e5d4a8fSHaim Dreyfuss {
8492e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
8502e5d4a8fSHaim Dreyfuss 
8512e5d4a8fSHaim Dreyfuss 	iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD, ~msk);
8522e5d4a8fSHaim Dreyfuss 	trans_pcie->hw_mask = msk;
8532e5d4a8fSHaim Dreyfuss }
8542e5d4a8fSHaim Dreyfuss 
8552e5d4a8fSHaim Dreyfuss static inline void iwl_enable_fh_int_msk_msix(struct iwl_trans *trans, u32 msk)
8562e5d4a8fSHaim Dreyfuss {
8572e5d4a8fSHaim Dreyfuss 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
8582e5d4a8fSHaim Dreyfuss 
8592e5d4a8fSHaim Dreyfuss 	iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~msk);
8602e5d4a8fSHaim Dreyfuss 	trans_pcie->fh_mask = msk;
861e705c121SKalle Valo }
862e705c121SKalle Valo 
863a6bd005fSEmmanuel Grumbach static inline void iwl_enable_fw_load_int(struct iwl_trans *trans)
864a6bd005fSEmmanuel Grumbach {
865a6bd005fSEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
866a6bd005fSEmmanuel Grumbach 
867a6bd005fSEmmanuel Grumbach 	IWL_DEBUG_ISR(trans, "Enabling FW load interrupt\n");
8682e5d4a8fSHaim Dreyfuss 	if (!trans_pcie->msix_enabled) {
869a6bd005fSEmmanuel Grumbach 		trans_pcie->inta_mask = CSR_INT_BIT_FH_TX;
870a6bd005fSEmmanuel Grumbach 		iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
8712e5d4a8fSHaim Dreyfuss 	} else {
8722e5d4a8fSHaim Dreyfuss 		iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,
8732e5d4a8fSHaim Dreyfuss 			    trans_pcie->hw_init_mask);
8742e5d4a8fSHaim Dreyfuss 		iwl_enable_fh_int_msk_msix(trans,
8752e5d4a8fSHaim Dreyfuss 					   MSIX_FH_INT_CAUSES_D2S_CH0_NUM);
8762e5d4a8fSHaim Dreyfuss 	}
877a6bd005fSEmmanuel Grumbach }
878a6bd005fSEmmanuel Grumbach 
879ed3e4c6dSEmmanuel Grumbach static inline void iwl_enable_fw_load_int_ctx_info(struct iwl_trans *trans)
880ed3e4c6dSEmmanuel Grumbach {
881ed3e4c6dSEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
882ed3e4c6dSEmmanuel Grumbach 
883ed3e4c6dSEmmanuel Grumbach 	IWL_DEBUG_ISR(trans, "Enabling ALIVE interrupt only\n");
884ed3e4c6dSEmmanuel Grumbach 
885ed3e4c6dSEmmanuel Grumbach 	if (!trans_pcie->msix_enabled) {
886ed3e4c6dSEmmanuel Grumbach 		/*
887ed3e4c6dSEmmanuel Grumbach 		 * When we'll receive the ALIVE interrupt, the ISR will call
888ed3e4c6dSEmmanuel Grumbach 		 * iwl_enable_fw_load_int_ctx_info again to set the ALIVE
889ed3e4c6dSEmmanuel Grumbach 		 * interrupt (which is not really needed anymore) but also the
890ed3e4c6dSEmmanuel Grumbach 		 * RX interrupt which will allow us to receive the ALIVE
891ed3e4c6dSEmmanuel Grumbach 		 * notification (which is Rx) and continue the flow.
892ed3e4c6dSEmmanuel Grumbach 		 */
893ed3e4c6dSEmmanuel Grumbach 		trans_pcie->inta_mask =  CSR_INT_BIT_ALIVE | CSR_INT_BIT_FH_RX;
894ed3e4c6dSEmmanuel Grumbach 		iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
895ed3e4c6dSEmmanuel Grumbach 	} else {
896ed3e4c6dSEmmanuel Grumbach 		iwl_enable_hw_int_msk_msix(trans,
897ed3e4c6dSEmmanuel Grumbach 					   MSIX_HW_INT_CAUSES_REG_ALIVE);
898ed3e4c6dSEmmanuel Grumbach 		/*
899ed3e4c6dSEmmanuel Grumbach 		 * Leave all the FH causes enabled to get the ALIVE
900ed3e4c6dSEmmanuel Grumbach 		 * notification.
901ed3e4c6dSEmmanuel Grumbach 		 */
902ed3e4c6dSEmmanuel Grumbach 		iwl_enable_fh_int_msk_msix(trans, trans_pcie->fh_init_mask);
903ed3e4c6dSEmmanuel Grumbach 	}
904ed3e4c6dSEmmanuel Grumbach }
905ed3e4c6dSEmmanuel Grumbach 
9067b3e42eaSGolan Ben Ami static inline u16 iwl_pcie_get_cmd_index(const struct iwl_txq *q, u32 index)
9074ecab561SEmmanuel Grumbach {
9084ecab561SEmmanuel Grumbach 	return index & (q->n_window - 1);
9094ecab561SEmmanuel Grumbach }
9104ecab561SEmmanuel Grumbach 
911943309d4SEmmanuel Grumbach static inline void *iwl_pcie_get_tfd(struct iwl_trans *trans,
912ab6c6445SSara Sharon 				     struct iwl_txq *txq, int idx)
913ab6c6445SSara Sharon {
914943309d4SEmmanuel Grumbach 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
915943309d4SEmmanuel Grumbach 
91679b6c8feSLuca Coelho 	if (trans->cfg->trans.use_tfh)
917943309d4SEmmanuel Grumbach 		idx = iwl_pcie_get_cmd_index(txq, idx);
918943309d4SEmmanuel Grumbach 
919943309d4SEmmanuel Grumbach 	return txq->tfds + trans_pcie->tfd_size * idx;
920ab6c6445SSara Sharon }
921ab6c6445SSara Sharon 
922ff932f61SGolan Ben Ami static inline const char *queue_name(struct device *dev,
923ff932f61SGolan Ben Ami 				     struct iwl_trans_pcie *trans_p, int i)
924ff932f61SGolan Ben Ami {
925ff932f61SGolan Ben Ami 	if (trans_p->shared_vec_mask) {
926ff932f61SGolan Ben Ami 		int vec = trans_p->shared_vec_mask &
927ff932f61SGolan Ben Ami 			  IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
928ff932f61SGolan Ben Ami 
929ff932f61SGolan Ben Ami 		if (i == 0)
930ff932f61SGolan Ben Ami 			return DRV_NAME ": shared IRQ";
931ff932f61SGolan Ben Ami 
932ff932f61SGolan Ben Ami 		return devm_kasprintf(dev, GFP_KERNEL,
933ff932f61SGolan Ben Ami 				      DRV_NAME ": queue %d", i + vec);
934ff932f61SGolan Ben Ami 	}
935ff932f61SGolan Ben Ami 	if (i == 0)
936ff932f61SGolan Ben Ami 		return DRV_NAME ": default queue";
937ff932f61SGolan Ben Ami 
938ff932f61SGolan Ben Ami 	if (i == trans_p->alloc_vecs - 1)
939ff932f61SGolan Ben Ami 		return DRV_NAME ": exception";
940ff932f61SGolan Ben Ami 
941ff932f61SGolan Ben Ami 	return devm_kasprintf(dev, GFP_KERNEL,
942ff932f61SGolan Ben Ami 			      DRV_NAME  ": queue %d", i);
943ff932f61SGolan Ben Ami }
944ff932f61SGolan Ben Ami 
945e705c121SKalle Valo static inline void iwl_enable_rfkill_int(struct iwl_trans *trans)
946e705c121SKalle Valo {
947e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
948e705c121SKalle Valo 
949e705c121SKalle Valo 	IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
9502e5d4a8fSHaim Dreyfuss 	if (!trans_pcie->msix_enabled) {
951e705c121SKalle Valo 		trans_pcie->inta_mask = CSR_INT_BIT_RF_KILL;
952e705c121SKalle Valo 		iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
9532e5d4a8fSHaim Dreyfuss 	} else {
9542e5d4a8fSHaim Dreyfuss 		iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,
9552e5d4a8fSHaim Dreyfuss 			    trans_pcie->fh_init_mask);
9562e5d4a8fSHaim Dreyfuss 		iwl_enable_hw_int_msk_msix(trans,
9572e5d4a8fSHaim Dreyfuss 					   MSIX_HW_INT_CAUSES_REG_RF_KILL);
9582e5d4a8fSHaim Dreyfuss 	}
959ae5bb2a6SJohannes Berg 
96079b6c8feSLuca Coelho 	if (trans->cfg->trans.device_family >= IWL_DEVICE_FAMILY_9000) {
961ae5bb2a6SJohannes Berg 		/*
962ae5bb2a6SJohannes Berg 		 * On 9000-series devices this bit isn't enabled by default, so
963ae5bb2a6SJohannes Berg 		 * when we power down the device we need set the bit to allow it
964ae5bb2a6SJohannes Berg 		 * to wake up the PCI-E bus for RF-kill interrupts.
965ae5bb2a6SJohannes Berg 		 */
966ae5bb2a6SJohannes Berg 		iwl_set_bit(trans, CSR_GP_CNTRL,
967ae5bb2a6SJohannes Berg 			    CSR_GP_CNTRL_REG_FLAG_RFKILL_WAKE_L1A_EN);
968ae5bb2a6SJohannes Berg 	}
969e705c121SKalle Valo }
970e705c121SKalle Valo 
971fa4de7f7SJohannes Berg void iwl_pcie_handle_rfkill_irq(struct iwl_trans *trans);
972fa4de7f7SJohannes Berg 
973e705c121SKalle Valo static inline void iwl_wake_queue(struct iwl_trans *trans,
974e705c121SKalle Valo 				  struct iwl_txq *txq)
975e705c121SKalle Valo {
976e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
977e705c121SKalle Valo 
978bb98ecd4SSara Sharon 	if (test_and_clear_bit(txq->id, trans_pcie->queue_stopped)) {
979bb98ecd4SSara Sharon 		IWL_DEBUG_TX_QUEUES(trans, "Wake hwq %d\n", txq->id);
980bb98ecd4SSara Sharon 		iwl_op_mode_queue_not_full(trans->op_mode, txq->id);
981e705c121SKalle Valo 	}
982e705c121SKalle Valo }
983e705c121SKalle Valo 
984e705c121SKalle Valo static inline void iwl_stop_queue(struct iwl_trans *trans,
985e705c121SKalle Valo 				  struct iwl_txq *txq)
986e705c121SKalle Valo {
987e705c121SKalle Valo 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
988e705c121SKalle Valo 
989bb98ecd4SSara Sharon 	if (!test_and_set_bit(txq->id, trans_pcie->queue_stopped)) {
990bb98ecd4SSara Sharon 		iwl_op_mode_queue_full(trans->op_mode, txq->id);
991bb98ecd4SSara Sharon 		IWL_DEBUG_TX_QUEUES(trans, "Stop hwq %d\n", txq->id);
992e705c121SKalle Valo 	} else
993e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(trans, "hwq %d already stopped\n",
994bb98ecd4SSara Sharon 				    txq->id);
995e705c121SKalle Valo }
996e705c121SKalle Valo 
997bb98ecd4SSara Sharon static inline bool iwl_queue_used(const struct iwl_txq *q, int i)
998e705c121SKalle Valo {
999f5955a6cSGolan Ben Ami 	int index = iwl_pcie_get_cmd_index(q, i);
1000f5955a6cSGolan Ben Ami 	int r = iwl_pcie_get_cmd_index(q, q->read_ptr);
1001f5955a6cSGolan Ben Ami 	int w = iwl_pcie_get_cmd_index(q, q->write_ptr);
1002f5955a6cSGolan Ben Ami 
1003f5955a6cSGolan Ben Ami 	return w >= r ?
1004f5955a6cSGolan Ben Ami 		(index >= r && index < w) :
1005f5955a6cSGolan Ben Ami 		!(index < r && index >= w);
1006e705c121SKalle Valo }
1007e705c121SKalle Valo 
1008e705c121SKalle Valo static inline bool iwl_is_rfkill_set(struct iwl_trans *trans)
1009e705c121SKalle Valo {
1010fa4de7f7SJohannes Berg 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1011fa4de7f7SJohannes Berg 
1012fa4de7f7SJohannes Berg 	lockdep_assert_held(&trans_pcie->mutex);
1013fa4de7f7SJohannes Berg 
1014c5bf4fa1SJohannes Berg 	if (trans_pcie->debug_rfkill == 1)
1015fa4de7f7SJohannes Berg 		return true;
101623aeea94SJohannes Berg 
1017e705c121SKalle Valo 	return !(iwl_read32(trans, CSR_GP_CNTRL) &
1018e705c121SKalle Valo 		CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
1019e705c121SKalle Valo }
1020e705c121SKalle Valo 
1021e705c121SKalle Valo static inline void __iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans,
1022e705c121SKalle Valo 						  u32 reg, u32 mask, u32 value)
1023e705c121SKalle Valo {
1024e705c121SKalle Valo 	u32 v;
1025e705c121SKalle Valo 
1026e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUG
1027e705c121SKalle Valo 	WARN_ON_ONCE(value & ~mask);
1028e705c121SKalle Valo #endif
1029e705c121SKalle Valo 
1030e705c121SKalle Valo 	v = iwl_read32(trans, reg);
1031e705c121SKalle Valo 	v &= ~mask;
1032e705c121SKalle Valo 	v |= value;
1033e705c121SKalle Valo 	iwl_write32(trans, reg, v);
1034e705c121SKalle Valo }
1035e705c121SKalle Valo 
1036e705c121SKalle Valo static inline void __iwl_trans_pcie_clear_bit(struct iwl_trans *trans,
1037e705c121SKalle Valo 					      u32 reg, u32 mask)
1038e705c121SKalle Valo {
1039e705c121SKalle Valo 	__iwl_trans_pcie_set_bits_mask(trans, reg, mask, 0);
1040e705c121SKalle Valo }
1041e705c121SKalle Valo 
1042e705c121SKalle Valo static inline void __iwl_trans_pcie_set_bit(struct iwl_trans *trans,
1043e705c121SKalle Valo 					    u32 reg, u32 mask)
1044e705c121SKalle Valo {
1045e705c121SKalle Valo 	__iwl_trans_pcie_set_bits_mask(trans, reg, mask, mask);
1046e705c121SKalle Valo }
1047e705c121SKalle Valo 
10487a14c23dSSara Sharon static inline bool iwl_pcie_dbg_on(struct iwl_trans *trans)
10497a14c23dSSara Sharon {
1050a1af4c48SShahar S Matityahu 	return (trans->dbg.dest_tlv || iwl_trans_dbg_ini_valid(trans));
10517a14c23dSSara Sharon }
10527a14c23dSSara Sharon 
1053e705c121SKalle Valo void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state);
10544290eaadSJohannes Berg void iwl_trans_pcie_dump_regs(struct iwl_trans *trans);
1055d1967ce6SShahar S Matityahu void iwl_trans_pcie_sync_nmi(struct iwl_trans *trans);
1056e705c121SKalle Valo 
1057f8a1edb7SJohannes Berg #ifdef CONFIG_IWLWIFI_DEBUGFS
1058cf5d5663SGreg Kroah-Hartman void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans);
1059f8a1edb7SJohannes Berg #else
1060cf5d5663SGreg Kroah-Hartman static inline void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans) { }
1061f8a1edb7SJohannes Berg #endif
1062f8a1edb7SJohannes Berg 
106310a54d81SLuca Coelho void iwl_pcie_rx_allocator_work(struct work_struct *data);
106410a54d81SLuca Coelho 
1065eda50cdeSSara Sharon /* common functions that are used by gen2 transport */
1066b6fe2757SGolan Ben Ami int iwl_pcie_gen2_apm_init(struct iwl_trans *trans);
1067eda50cdeSSara Sharon void iwl_pcie_apm_config(struct iwl_trans *trans);
1068eda50cdeSSara Sharon int iwl_pcie_prepare_card_hw(struct iwl_trans *trans);
1069eda50cdeSSara Sharon void iwl_pcie_synchronize_irqs(struct iwl_trans *trans);
10709ad8fd0bSJohannes Berg bool iwl_pcie_check_hw_rf_kill(struct iwl_trans *trans);
1071326477e4SJohannes Berg void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans,
1072326477e4SJohannes Berg 				       bool was_in_rfkill);
10736b35ff91SSara Sharon void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq);
10747b3e42eaSGolan Ben Ami int iwl_queue_space(struct iwl_trans *trans, const struct iwl_txq *q);
1075e8c8935eSJohannes Berg void iwl_pcie_apm_stop_master(struct iwl_trans *trans);
107677c09bc8SSara Sharon void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie);
107713a3a390SSara Sharon int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
1078b8e8d7ceSSara Sharon 		      int slots_num, bool cmd_queue);
107913a3a390SSara Sharon int iwl_pcie_txq_alloc(struct iwl_trans *trans,
1080b8e8d7ceSSara Sharon 		       struct iwl_txq *txq, int slots_num,  bool cmd_queue);
108113a3a390SSara Sharon int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
108213a3a390SSara Sharon 			   struct iwl_dma_ptr *ptr, size_t size);
108313a3a390SSara Sharon void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr);
1084c9be849dSLiad Kaufman void iwl_pcie_apply_destination(struct iwl_trans *trans);
10859bb3d5a0SEmmanuel Grumbach void iwl_pcie_free_tso_page(struct iwl_trans_pcie *trans_pcie,
10869bb3d5a0SEmmanuel Grumbach 			    struct sk_buff *skb);
10876ffe5de3SSara Sharon #ifdef CONFIG_INET
10886ffe5de3SSara Sharon struct iwl_tso_hdr_page *get_page_hdr(struct iwl_trans *trans, size_t len);
10896ffe5de3SSara Sharon #endif
1090eda50cdeSSara Sharon 
10919f358c17SGolan Ben Ami /* common functions that are used by gen3 transport */
10929f358c17SGolan Ben Ami void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power);
10939f358c17SGolan Ben Ami 
1094eda50cdeSSara Sharon /* transport gen 2 exported functions */
1095eda50cdeSSara Sharon int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans,
1096eda50cdeSSara Sharon 				 const struct fw_img *fw, bool run_in_rfkill);
1097eda50cdeSSara Sharon void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans, u32 scd_addr);
10981169310fSGolan Ben Ami void iwl_pcie_gen2_txq_free_memory(struct iwl_trans *trans,
10991169310fSGolan Ben Ami 				   struct iwl_txq *txq);
11001169310fSGolan Ben Ami int iwl_trans_pcie_dyn_txq_alloc_dma(struct iwl_trans *trans,
11011169310fSGolan Ben Ami 				     struct iwl_txq **intxq, int size,
11021169310fSGolan Ben Ami 				     unsigned int timeout);
11031169310fSGolan Ben Ami int iwl_trans_pcie_txq_alloc_response(struct iwl_trans *trans,
11041169310fSGolan Ben Ami 				      struct iwl_txq *txq,
11051169310fSGolan Ben Ami 				      struct iwl_host_cmd *hcmd);
11066b35ff91SSara Sharon int iwl_trans_pcie_dyn_txq_alloc(struct iwl_trans *trans,
11071169310fSGolan Ben Ami 				 __le16 flags, u8 sta_id, u8 tid,
11085369774cSSara Sharon 				 int cmd_id, int size,
11096b35ff91SSara Sharon 				 unsigned int timeout);
11106b35ff91SSara Sharon void iwl_trans_pcie_dyn_txq_free(struct iwl_trans *trans, int queue);
1111ab6c6445SSara Sharon int iwl_trans_pcie_gen2_tx(struct iwl_trans *trans, struct sk_buff *skb,
1112ab6c6445SSara Sharon 			   struct iwl_device_cmd *dev_cmd, int txq_id);
1113ca60da2eSSara Sharon int iwl_trans_pcie_gen2_send_hcmd(struct iwl_trans *trans,
1114ca60da2eSSara Sharon 				  struct iwl_host_cmd *cmd);
1115bab3cb92SEmmanuel Grumbach void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans);
1116bab3cb92SEmmanuel Grumbach void _iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans);
111713a3a390SSara Sharon void iwl_pcie_gen2_txq_unmap(struct iwl_trans *trans, int txq_id);
111813a3a390SSara Sharon void iwl_pcie_gen2_tx_free(struct iwl_trans *trans);
111913a3a390SSara Sharon void iwl_pcie_gen2_tx_stop(struct iwl_trans *trans);
1120e705c121SKalle Valo #endif /* __iwl_trans_int_pcie_h__ */
1121