1 /******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2003 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11 * Copyright(c) 2018 - 2019 Intel Corporation
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of version 2 of the GNU General Public License as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * The full GNU General Public License is included in this distribution in the
23 * file called COPYING.
24 *
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 * BSD LICENSE
30 *
31 * Copyright(c) 2003 - 2015 Intel Corporation. All rights reserved.
32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34 * Copyright(c) 2018 - 2019 Intel Corporation
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 *
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
46 * distribution.
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 *****************************************************************************/
64 #ifndef __iwl_trans_int_pcie_h__
65 #define __iwl_trans_int_pcie_h__
66
67 #include <linux/spinlock.h>
68 #include <linux/interrupt.h>
69 #include <linux/skbuff.h>
70 #include <linux/wait.h>
71 #include <linux/pci.h>
72 #include <linux/timer.h>
73 #include <linux/cpu.h>
74
75 #include "iwl-fh.h"
76 #include "iwl-csr.h"
77 #include "iwl-trans.h"
78 #include "iwl-debug.h"
79 #include "iwl-io.h"
80 #include "iwl-op-mode.h"
81 #include "iwl-drv.h"
82 #include "queue/tx.h"
83
84 /*
85 * RX related structures and functions
86 */
87 #define RX_NUM_QUEUES 1
88 #define RX_POST_REQ_ALLOC 2
89 #define RX_CLAIM_REQ_ALLOC 8
90 #define RX_PENDING_WATERMARK 16
91 #define FIRST_RX_QUEUE 512
92
93 struct iwl_host_cmd;
94
95 /*This file includes the declaration that are internal to the
96 * trans_pcie layer */
97
98 /**
99 * struct iwl_rx_mem_buffer
100 * @page_dma: bus address of rxb page
101 * @page: driver's pointer to the rxb page
102 * @invalid: rxb is in driver ownership - not owned by HW
103 * @vid: index of this rxb in the global table
104 * @offset: indicates which offset of the page (in bytes)
105 * this buffer uses (if multiple RBs fit into one page)
106 */
107 struct iwl_rx_mem_buffer {
108 dma_addr_t page_dma;
109 struct page *page;
110 u16 vid;
111 bool invalid;
112 struct list_head list;
113 u32 offset;
114 };
115
116 /**
117 * struct isr_statistics - interrupt statistics
118 *
119 */
120 struct isr_statistics {
121 u32 hw;
122 u32 sw;
123 u32 err_code;
124 u32 sch;
125 u32 alive;
126 u32 rfkill;
127 u32 ctkill;
128 u32 wakeup;
129 u32 rx;
130 u32 tx;
131 u32 unhandled;
132 };
133
134 /**
135 * struct iwl_rx_transfer_desc - transfer descriptor
136 * @addr: ptr to free buffer start address
137 * @rbid: unique tag of the buffer
138 * @reserved: reserved
139 */
140 struct iwl_rx_transfer_desc {
141 __le16 rbid;
142 __le16 reserved[3];
143 __le64 addr;
144 } __packed;
145
146 #define IWL_RX_CD_FLAGS_FRAGMENTED BIT(0)
147
148 /**
149 * struct iwl_rx_completion_desc - completion descriptor
150 * @reserved1: reserved
151 * @rbid: unique tag of the received buffer
152 * @flags: flags (0: fragmented, all others: reserved)
153 * @reserved2: reserved
154 */
155 struct iwl_rx_completion_desc {
156 __le32 reserved1;
157 __le16 rbid;
158 u8 flags;
159 u8 reserved2[25];
160 } __packed;
161
162 /**
163 * struct iwl_rxq - Rx queue
164 * @id: queue index
165 * @bd: driver's pointer to buffer of receive buffer descriptors (rbd).
166 * Address size is 32 bit in pre-9000 devices and 64 bit in 9000 devices.
167 * In AX210 devices it is a pointer to a list of iwl_rx_transfer_desc's
168 * @bd_dma: bus address of buffer of receive buffer descriptors (rbd)
169 * @ubd: driver's pointer to buffer of used receive buffer descriptors (rbd)
170 * @ubd_dma: physical address of buffer of used receive buffer descriptors (rbd)
171 * @tr_tail: driver's pointer to the transmission ring tail buffer
172 * @tr_tail_dma: physical address of the buffer for the transmission ring tail
173 * @cr_tail: driver's pointer to the completion ring tail buffer
174 * @cr_tail_dma: physical address of the buffer for the completion ring tail
175 * @read: Shared index to newest available Rx buffer
176 * @write: Shared index to oldest written Rx packet
177 * @free_count: Number of pre-allocated buffers in rx_free
178 * @used_count: Number of RBDs handled to allocator to use for allocation
179 * @write_actual:
180 * @rx_free: list of RBDs with allocated RB ready for use
181 * @rx_used: list of RBDs with no RB attached
182 * @need_update: flag to indicate we need to update read/write index
183 * @rb_stts: driver's pointer to receive buffer status
184 * @rb_stts_dma: bus address of receive buffer status
185 * @lock:
186 * @queue: actual rx queue. Not used for multi-rx queue.
187 * @next_rb_is_fragment: indicates that the previous RB that we handled set
188 * the fragmented flag, so the next one is still another fragment
189 *
190 * NOTE: rx_free and rx_used are used as a FIFO for iwl_rx_mem_buffers
191 */
192 struct iwl_rxq {
193 int id;
194 void *bd;
195 dma_addr_t bd_dma;
196 union {
197 void *used_bd;
198 __le32 *bd_32;
199 struct iwl_rx_completion_desc *cd;
200 };
201 dma_addr_t used_bd_dma;
202 __le16 *tr_tail;
203 dma_addr_t tr_tail_dma;
204 __le16 *cr_tail;
205 dma_addr_t cr_tail_dma;
206 u32 read;
207 u32 write;
208 u32 free_count;
209 u32 used_count;
210 u32 write_actual;
211 u32 queue_size;
212 struct list_head rx_free;
213 struct list_head rx_used;
214 bool need_update, next_rb_is_fragment;
215 void *rb_stts;
216 dma_addr_t rb_stts_dma;
217 spinlock_t lock;
218 struct napi_struct napi;
219 struct iwl_rx_mem_buffer *queue[RX_QUEUE_SIZE];
220 };
221
222 /**
223 * struct iwl_rb_allocator - Rx allocator
224 * @req_pending: number of requests the allcator had not processed yet
225 * @req_ready: number of requests honored and ready for claiming
226 * @rbd_allocated: RBDs with pages allocated and ready to be handled to
227 * the queue. This is a list of &struct iwl_rx_mem_buffer
228 * @rbd_empty: RBDs with no page attached for allocator use. This is a list
229 * of &struct iwl_rx_mem_buffer
230 * @lock: protects the rbd_allocated and rbd_empty lists
231 * @alloc_wq: work queue for background calls
232 * @rx_alloc: work struct for background calls
233 */
234 struct iwl_rb_allocator {
235 atomic_t req_pending;
236 atomic_t req_ready;
237 struct list_head rbd_allocated;
238 struct list_head rbd_empty;
239 spinlock_t lock;
240 struct workqueue_struct *alloc_wq;
241 struct work_struct rx_alloc;
242 };
243
244 /**
245 * iwl_get_closed_rb_stts - get closed rb stts from different structs
246 * @rxq - the rxq to get the rb stts from
247 */
iwl_get_closed_rb_stts(struct iwl_trans * trans,struct iwl_rxq * rxq)248 static inline __le16 iwl_get_closed_rb_stts(struct iwl_trans *trans,
249 struct iwl_rxq *rxq)
250 {
251 if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
252 __le16 *rb_stts = rxq->rb_stts;
253
254 return READ_ONCE(*rb_stts);
255 } else {
256 struct iwl_rb_status *rb_stts = rxq->rb_stts;
257
258 return READ_ONCE(rb_stts->closed_rb_num);
259 }
260 }
261
262 #ifdef CONFIG_IWLWIFI_DEBUGFS
263 /**
264 * enum iwl_fw_mon_dbgfs_state - the different states of the monitor_data
265 * debugfs file
266 *
267 * @IWL_FW_MON_DBGFS_STATE_CLOSED: the file is closed.
268 * @IWL_FW_MON_DBGFS_STATE_OPEN: the file is open.
269 * @IWL_FW_MON_DBGFS_STATE_DISABLED: the file is disabled, once this state is
270 * set the file can no longer be used.
271 */
272 enum iwl_fw_mon_dbgfs_state {
273 IWL_FW_MON_DBGFS_STATE_CLOSED,
274 IWL_FW_MON_DBGFS_STATE_OPEN,
275 IWL_FW_MON_DBGFS_STATE_DISABLED,
276 };
277 #endif
278
279 /**
280 * enum iwl_shared_irq_flags - level of sharing for irq
281 * @IWL_SHARED_IRQ_NON_RX: interrupt vector serves non rx causes.
282 * @IWL_SHARED_IRQ_FIRST_RSS: interrupt vector serves first RSS queue.
283 */
284 enum iwl_shared_irq_flags {
285 IWL_SHARED_IRQ_NON_RX = BIT(0),
286 IWL_SHARED_IRQ_FIRST_RSS = BIT(1),
287 };
288
289 /**
290 * enum iwl_image_response_code - image response values
291 * @IWL_IMAGE_RESP_DEF: the default value of the register
292 * @IWL_IMAGE_RESP_SUCCESS: iml was read successfully
293 * @IWL_IMAGE_RESP_FAIL: iml reading failed
294 */
295 enum iwl_image_response_code {
296 IWL_IMAGE_RESP_DEF = 0,
297 IWL_IMAGE_RESP_SUCCESS = 1,
298 IWL_IMAGE_RESP_FAIL = 2,
299 };
300
301 /**
302 * struct cont_rec: continuous recording data structure
303 * @prev_wr_ptr: the last address that was read in monitor_data
304 * debugfs file
305 * @prev_wrap_cnt: the wrap count that was used during the last read in
306 * monitor_data debugfs file
307 * @state: the state of monitor_data debugfs file as described
308 * in &iwl_fw_mon_dbgfs_state enum
309 * @mutex: locked while reading from monitor_data debugfs file
310 */
311 #ifdef CONFIG_IWLWIFI_DEBUGFS
312 struct cont_rec {
313 u32 prev_wr_ptr;
314 u32 prev_wrap_cnt;
315 u8 state;
316 /* Used to sync monitor_data debugfs file with driver unload flow */
317 struct mutex mutex;
318 };
319 #endif
320
321 /**
322 * struct iwl_trans_pcie - PCIe transport specific data
323 * @rxq: all the RX queue data
324 * @rx_pool: initial pool of iwl_rx_mem_buffer for all the queues
325 * @global_table: table mapping received VID from hw to rxb
326 * @rba: allocator for RX replenishing
327 * @ctxt_info: context information for FW self init
328 * @ctxt_info_gen3: context information for gen3 devices
329 * @prph_info: prph info for self init
330 * @prph_scratch: prph scratch for self init
331 * @ctxt_info_dma_addr: dma addr of context information
332 * @prph_info_dma_addr: dma addr of prph info
333 * @prph_scratch_dma_addr: dma addr of prph scratch
334 * @ctxt_info_dma_addr: dma addr of context information
335 * @init_dram: DRAM data of firmware image (including paging).
336 * Context information addresses will be taken from here.
337 * This is driver's local copy for keeping track of size and
338 * count for allocating and freeing the memory.
339 * @trans: pointer to the generic transport area
340 * @scd_base_addr: scheduler sram base address in SRAM
341 * @kw: keep warm address
342 * @pnvm_dram: DRAM area that contains the PNVM data
343 * @pci_dev: basic pci-network driver stuff
344 * @hw_base: pci hardware address support
345 * @ucode_write_complete: indicates that the ucode has been copied.
346 * @ucode_write_waitq: wait queue for uCode load
347 * @cmd_queue - command queue number
348 * @def_rx_queue - default rx queue number
349 * @rx_buf_size: Rx buffer size
350 * @scd_set_active: should the transport configure the SCD for HCMD queue
351 * @sw_csum_tx: if true, then the transport will compute the csum of the TXed
352 * frame.
353 * @rx_page_order: page order for receive buffer size
354 * @rx_buf_bytes: RX buffer (RB) size in bytes
355 * @reg_lock: protect hw register access
356 * @mutex: to protect stop_device / start_fw / start_hw
357 * @cmd_in_flight: true when we have a host command in flight
358 #ifdef CONFIG_IWLWIFI_DEBUGFS
359 * @fw_mon_data: fw continuous recording data
360 #endif
361 * @msix_entries: array of MSI-X entries
362 * @msix_enabled: true if managed to enable MSI-X
363 * @shared_vec_mask: the type of causes the shared vector handles
364 * (see iwl_shared_irq_flags).
365 * @alloc_vecs: the number of interrupt vectors allocated by the OS
366 * @def_irq: default irq for non rx causes
367 * @fh_init_mask: initial unmasked fh causes
368 * @hw_init_mask: initial unmasked hw causes
369 * @fh_mask: current unmasked fh causes
370 * @hw_mask: current unmasked hw causes
371 * @in_rescan: true if we have triggered a device rescan
372 * @base_rb_stts: base virtual address of receive buffer status for all queues
373 * @base_rb_stts_dma: base physical address of receive buffer status
374 * @supported_dma_mask: DMA mask to validate the actual address against,
375 * will be DMA_BIT_MASK(11) or DMA_BIT_MASK(12) depending on the device
376 * @alloc_page_lock: spinlock for the page allocator
377 * @alloc_page: allocated page to still use parts of
378 * @alloc_page_used: how much of the allocated page was already used (bytes)
379 */
380 struct iwl_trans_pcie {
381 struct iwl_rxq *rxq;
382 struct iwl_rx_mem_buffer *rx_pool;
383 struct iwl_rx_mem_buffer **global_table;
384 struct iwl_rb_allocator rba;
385 union {
386 struct iwl_context_info *ctxt_info;
387 struct iwl_context_info_gen3 *ctxt_info_gen3;
388 };
389 struct iwl_prph_info *prph_info;
390 struct iwl_prph_scratch *prph_scratch;
391 dma_addr_t ctxt_info_dma_addr;
392 dma_addr_t prph_info_dma_addr;
393 dma_addr_t prph_scratch_dma_addr;
394 dma_addr_t iml_dma_addr;
395 struct iwl_trans *trans;
396
397 struct net_device napi_dev;
398
399 /* INT ICT Table */
400 __le32 *ict_tbl;
401 dma_addr_t ict_tbl_dma;
402 int ict_index;
403 bool use_ict;
404 bool is_down, opmode_down;
405 s8 debug_rfkill;
406 struct isr_statistics isr_stats;
407
408 spinlock_t irq_lock;
409 struct mutex mutex;
410 u32 inta_mask;
411 u32 scd_base_addr;
412 struct iwl_dma_ptr kw;
413
414 struct iwl_dram_data pnvm_dram;
415
416 struct iwl_txq *txq_memory;
417
418 /* PCI bus related data */
419 struct pci_dev *pci_dev;
420 void __iomem *hw_base;
421
422 bool ucode_write_complete;
423 bool sx_complete;
424 wait_queue_head_t ucode_write_waitq;
425 wait_queue_head_t wait_command_queue;
426 wait_queue_head_t sx_waitq;
427
428 u8 def_rx_queue;
429 u8 n_no_reclaim_cmds;
430 u8 no_reclaim_cmds[MAX_NO_RECLAIM_CMDS];
431 u16 num_rx_bufs;
432
433 enum iwl_amsdu_size rx_buf_size;
434 bool scd_set_active;
435 bool sw_csum_tx;
436 bool pcie_dbg_dumped_once;
437 u32 rx_page_order;
438 u32 rx_buf_bytes;
439 u32 supported_dma_mask;
440
441 /* allocator lock for the two values below */
442 spinlock_t alloc_page_lock;
443 struct page *alloc_page;
444 u32 alloc_page_used;
445
446 /*protect hw register */
447 spinlock_t reg_lock;
448 bool cmd_hold_nic_awake;
449
450 #ifdef CONFIG_IWLWIFI_DEBUGFS
451 struct cont_rec fw_mon_data;
452 #endif
453
454 struct msix_entry msix_entries[IWL_MAX_RX_HW_QUEUES];
455 bool msix_enabled;
456 u8 shared_vec_mask;
457 u32 alloc_vecs;
458 u32 def_irq;
459 u32 fh_init_mask;
460 u32 hw_init_mask;
461 u32 fh_mask;
462 u32 hw_mask;
463 cpumask_t affinity_mask[IWL_MAX_RX_HW_QUEUES];
464 u16 tx_cmd_queue_size;
465 bool in_rescan;
466
467 void *base_rb_stts;
468 dma_addr_t base_rb_stts_dma;
469 };
470
471 static inline struct iwl_trans_pcie *
IWL_TRANS_GET_PCIE_TRANS(struct iwl_trans * trans)472 IWL_TRANS_GET_PCIE_TRANS(struct iwl_trans *trans)
473 {
474 return (void *)trans->trans_specific;
475 }
476
iwl_pcie_clear_irq(struct iwl_trans * trans,struct msix_entry * entry)477 static inline void iwl_pcie_clear_irq(struct iwl_trans *trans,
478 struct msix_entry *entry)
479 {
480 /*
481 * Before sending the interrupt the HW disables it to prevent
482 * a nested interrupt. This is done by writing 1 to the corresponding
483 * bit in the mask register. After handling the interrupt, it should be
484 * re-enabled by clearing this bit. This register is defined as
485 * write 1 clear (W1C) register, meaning that it's being clear
486 * by writing 1 to the bit.
487 */
488 iwl_write32(trans, CSR_MSIX_AUTOMASK_ST_AD, BIT(entry->entry));
489 }
490
491 static inline struct iwl_trans *
iwl_trans_pcie_get_trans(struct iwl_trans_pcie * trans_pcie)492 iwl_trans_pcie_get_trans(struct iwl_trans_pcie *trans_pcie)
493 {
494 return container_of((void *)trans_pcie, struct iwl_trans,
495 trans_specific);
496 }
497
498 /*
499 * Convention: trans API functions: iwl_trans_pcie_XXX
500 * Other functions: iwl_pcie_XXX
501 */
502 struct iwl_trans
503 *iwl_trans_pcie_alloc(struct pci_dev *pdev,
504 const struct pci_device_id *ent,
505 const struct iwl_cfg_trans_params *cfg_trans);
506 void iwl_trans_pcie_free(struct iwl_trans *trans);
507
508 /*****************************************************
509 * RX
510 ******************************************************/
511 int iwl_pcie_rx_init(struct iwl_trans *trans);
512 int iwl_pcie_gen2_rx_init(struct iwl_trans *trans);
513 irqreturn_t iwl_pcie_msix_isr(int irq, void *data);
514 irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id);
515 irqreturn_t iwl_pcie_irq_msix_handler(int irq, void *dev_id);
516 irqreturn_t iwl_pcie_irq_rx_msix_handler(int irq, void *dev_id);
517 int iwl_pcie_rx_stop(struct iwl_trans *trans);
518 void iwl_pcie_rx_free(struct iwl_trans *trans);
519 void iwl_pcie_free_rbs_pool(struct iwl_trans *trans);
520 void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq);
521 int iwl_pcie_dummy_napi_poll(struct napi_struct *napi, int budget);
522 void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,
523 struct iwl_rxq *rxq);
524
525 /*****************************************************
526 * ICT - interrupt handling
527 ******************************************************/
528 irqreturn_t iwl_pcie_isr(int irq, void *data);
529 int iwl_pcie_alloc_ict(struct iwl_trans *trans);
530 void iwl_pcie_free_ict(struct iwl_trans *trans);
531 void iwl_pcie_reset_ict(struct iwl_trans *trans);
532 void iwl_pcie_disable_ict(struct iwl_trans *trans);
533
534 /*****************************************************
535 * TX / HCMD
536 ******************************************************/
537 int iwl_pcie_tx_init(struct iwl_trans *trans);
538 void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr);
539 int iwl_pcie_tx_stop(struct iwl_trans *trans);
540 void iwl_pcie_tx_free(struct iwl_trans *trans);
541 bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int queue, u16 ssn,
542 const struct iwl_trans_txq_scd_cfg *cfg,
543 unsigned int wdg_timeout);
544 void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int queue,
545 bool configure_scd);
546 void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
547 bool shared_mode);
548 int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
549 struct iwl_device_tx_cmd *dev_cmd, int txq_id);
550 void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans);
551 int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
552 void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
553 struct iwl_rx_cmd_buffer *rxb);
554 void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
555 struct sk_buff_head *skbs);
556 void iwl_trans_pcie_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr);
557 void iwl_trans_pcie_tx_reset(struct iwl_trans *trans);
558
559 /*****************************************************
560 * Error handling
561 ******************************************************/
562 void iwl_pcie_dump_csr(struct iwl_trans *trans);
563
564 /*****************************************************
565 * Helpers
566 ******************************************************/
_iwl_disable_interrupts(struct iwl_trans * trans)567 static inline void _iwl_disable_interrupts(struct iwl_trans *trans)
568 {
569 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
570
571 clear_bit(STATUS_INT_ENABLED, &trans->status);
572 if (!trans_pcie->msix_enabled) {
573 /* disable interrupts from uCode/NIC to host */
574 iwl_write32(trans, CSR_INT_MASK, 0x00000000);
575
576 /* acknowledge/clear/reset any interrupts still pending
577 * from uCode or flow handler (Rx/Tx DMA) */
578 iwl_write32(trans, CSR_INT, 0xffffffff);
579 iwl_write32(trans, CSR_FH_INT_STATUS, 0xffffffff);
580 } else {
581 /* disable all the interrupt we might use */
582 iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,
583 trans_pcie->fh_init_mask);
584 iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,
585 trans_pcie->hw_init_mask);
586 }
587 IWL_DEBUG_ISR(trans, "Disabled interrupts\n");
588 }
589
590 #define IWL_NUM_OF_COMPLETION_RINGS 31
591 #define IWL_NUM_OF_TRANSFER_RINGS 527
592
iwl_pcie_get_num_sections(const struct fw_img * fw,int start)593 static inline int iwl_pcie_get_num_sections(const struct fw_img *fw,
594 int start)
595 {
596 int i = 0;
597
598 while (start < fw->num_sec &&
599 fw->sec[start].offset != CPU1_CPU2_SEPARATOR_SECTION &&
600 fw->sec[start].offset != PAGING_SEPARATOR_SECTION) {
601 start++;
602 i++;
603 }
604
605 return i;
606 }
607
iwl_pcie_ctxt_info_free_fw_img(struct iwl_trans * trans)608 static inline void iwl_pcie_ctxt_info_free_fw_img(struct iwl_trans *trans)
609 {
610 struct iwl_self_init_dram *dram = &trans->init_dram;
611 int i;
612
613 if (!dram->fw) {
614 WARN_ON(dram->fw_cnt);
615 return;
616 }
617
618 for (i = 0; i < dram->fw_cnt; i++)
619 dma_free_coherent(trans->dev, dram->fw[i].size,
620 dram->fw[i].block, dram->fw[i].physical);
621
622 kfree(dram->fw);
623 dram->fw_cnt = 0;
624 dram->fw = NULL;
625 }
626
iwl_disable_interrupts(struct iwl_trans * trans)627 static inline void iwl_disable_interrupts(struct iwl_trans *trans)
628 {
629 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
630
631 spin_lock(&trans_pcie->irq_lock);
632 _iwl_disable_interrupts(trans);
633 spin_unlock(&trans_pcie->irq_lock);
634 }
635
_iwl_enable_interrupts(struct iwl_trans * trans)636 static inline void _iwl_enable_interrupts(struct iwl_trans *trans)
637 {
638 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
639
640 IWL_DEBUG_ISR(trans, "Enabling interrupts\n");
641 set_bit(STATUS_INT_ENABLED, &trans->status);
642 if (!trans_pcie->msix_enabled) {
643 trans_pcie->inta_mask = CSR_INI_SET_MASK;
644 iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
645 } else {
646 /*
647 * fh/hw_mask keeps all the unmasked causes.
648 * Unlike msi, in msix cause is enabled when it is unset.
649 */
650 trans_pcie->hw_mask = trans_pcie->hw_init_mask;
651 trans_pcie->fh_mask = trans_pcie->fh_init_mask;
652 iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,
653 ~trans_pcie->fh_mask);
654 iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,
655 ~trans_pcie->hw_mask);
656 }
657 }
658
iwl_enable_interrupts(struct iwl_trans * trans)659 static inline void iwl_enable_interrupts(struct iwl_trans *trans)
660 {
661 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
662
663 spin_lock(&trans_pcie->irq_lock);
664 _iwl_enable_interrupts(trans);
665 spin_unlock(&trans_pcie->irq_lock);
666 }
iwl_enable_hw_int_msk_msix(struct iwl_trans * trans,u32 msk)667 static inline void iwl_enable_hw_int_msk_msix(struct iwl_trans *trans, u32 msk)
668 {
669 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
670
671 iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD, ~msk);
672 trans_pcie->hw_mask = msk;
673 }
674
iwl_enable_fh_int_msk_msix(struct iwl_trans * trans,u32 msk)675 static inline void iwl_enable_fh_int_msk_msix(struct iwl_trans *trans, u32 msk)
676 {
677 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
678
679 iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~msk);
680 trans_pcie->fh_mask = msk;
681 }
682
iwl_enable_fw_load_int(struct iwl_trans * trans)683 static inline void iwl_enable_fw_load_int(struct iwl_trans *trans)
684 {
685 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
686
687 IWL_DEBUG_ISR(trans, "Enabling FW load interrupt\n");
688 if (!trans_pcie->msix_enabled) {
689 trans_pcie->inta_mask = CSR_INT_BIT_FH_TX;
690 iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
691 } else {
692 iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,
693 trans_pcie->hw_init_mask);
694 iwl_enable_fh_int_msk_msix(trans,
695 MSIX_FH_INT_CAUSES_D2S_CH0_NUM);
696 }
697 }
698
iwl_enable_fw_load_int_ctx_info(struct iwl_trans * trans)699 static inline void iwl_enable_fw_load_int_ctx_info(struct iwl_trans *trans)
700 {
701 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
702
703 IWL_DEBUG_ISR(trans, "Enabling ALIVE interrupt only\n");
704
705 if (!trans_pcie->msix_enabled) {
706 /*
707 * When we'll receive the ALIVE interrupt, the ISR will call
708 * iwl_enable_fw_load_int_ctx_info again to set the ALIVE
709 * interrupt (which is not really needed anymore) but also the
710 * RX interrupt which will allow us to receive the ALIVE
711 * notification (which is Rx) and continue the flow.
712 */
713 trans_pcie->inta_mask = CSR_INT_BIT_ALIVE | CSR_INT_BIT_FH_RX;
714 iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
715 } else {
716 iwl_enable_hw_int_msk_msix(trans,
717 MSIX_HW_INT_CAUSES_REG_ALIVE);
718 /*
719 * Leave all the FH causes enabled to get the ALIVE
720 * notification.
721 */
722 iwl_enable_fh_int_msk_msix(trans, trans_pcie->fh_init_mask);
723 }
724 }
725
queue_name(struct device * dev,struct iwl_trans_pcie * trans_p,int i)726 static inline const char *queue_name(struct device *dev,
727 struct iwl_trans_pcie *trans_p, int i)
728 {
729 if (trans_p->shared_vec_mask) {
730 int vec = trans_p->shared_vec_mask &
731 IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
732
733 if (i == 0)
734 return DRV_NAME ": shared IRQ";
735
736 return devm_kasprintf(dev, GFP_KERNEL,
737 DRV_NAME ": queue %d", i + vec);
738 }
739 if (i == 0)
740 return DRV_NAME ": default queue";
741
742 if (i == trans_p->alloc_vecs - 1)
743 return DRV_NAME ": exception";
744
745 return devm_kasprintf(dev, GFP_KERNEL,
746 DRV_NAME ": queue %d", i);
747 }
748
iwl_enable_rfkill_int(struct iwl_trans * trans)749 static inline void iwl_enable_rfkill_int(struct iwl_trans *trans)
750 {
751 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
752
753 IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
754 if (!trans_pcie->msix_enabled) {
755 trans_pcie->inta_mask = CSR_INT_BIT_RF_KILL;
756 iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
757 } else {
758 iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,
759 trans_pcie->fh_init_mask);
760 iwl_enable_hw_int_msk_msix(trans,
761 MSIX_HW_INT_CAUSES_REG_RF_KILL);
762 }
763
764 if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000) {
765 /*
766 * On 9000-series devices this bit isn't enabled by default, so
767 * when we power down the device we need set the bit to allow it
768 * to wake up the PCI-E bus for RF-kill interrupts.
769 */
770 iwl_set_bit(trans, CSR_GP_CNTRL,
771 CSR_GP_CNTRL_REG_FLAG_RFKILL_WAKE_L1A_EN);
772 }
773 }
774
775 void iwl_pcie_handle_rfkill_irq(struct iwl_trans *trans);
776
iwl_is_rfkill_set(struct iwl_trans * trans)777 static inline bool iwl_is_rfkill_set(struct iwl_trans *trans)
778 {
779 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
780
781 lockdep_assert_held(&trans_pcie->mutex);
782
783 if (trans_pcie->debug_rfkill == 1)
784 return true;
785
786 return !(iwl_read32(trans, CSR_GP_CNTRL) &
787 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
788 }
789
__iwl_trans_pcie_set_bits_mask(struct iwl_trans * trans,u32 reg,u32 mask,u32 value)790 static inline void __iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans,
791 u32 reg, u32 mask, u32 value)
792 {
793 u32 v;
794
795 #ifdef CONFIG_IWLWIFI_DEBUG
796 WARN_ON_ONCE(value & ~mask);
797 #endif
798
799 v = iwl_read32(trans, reg);
800 v &= ~mask;
801 v |= value;
802 iwl_write32(trans, reg, v);
803 }
804
__iwl_trans_pcie_clear_bit(struct iwl_trans * trans,u32 reg,u32 mask)805 static inline void __iwl_trans_pcie_clear_bit(struct iwl_trans *trans,
806 u32 reg, u32 mask)
807 {
808 __iwl_trans_pcie_set_bits_mask(trans, reg, mask, 0);
809 }
810
__iwl_trans_pcie_set_bit(struct iwl_trans * trans,u32 reg,u32 mask)811 static inline void __iwl_trans_pcie_set_bit(struct iwl_trans *trans,
812 u32 reg, u32 mask)
813 {
814 __iwl_trans_pcie_set_bits_mask(trans, reg, mask, mask);
815 }
816
iwl_pcie_dbg_on(struct iwl_trans * trans)817 static inline bool iwl_pcie_dbg_on(struct iwl_trans *trans)
818 {
819 return (trans->dbg.dest_tlv || iwl_trans_dbg_ini_valid(trans));
820 }
821
822 void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state);
823 void iwl_trans_pcie_dump_regs(struct iwl_trans *trans);
824 void iwl_trans_pcie_sync_nmi(struct iwl_trans *trans);
825
826 #ifdef CONFIG_IWLWIFI_DEBUGFS
827 void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans);
828 #else
iwl_trans_pcie_dbgfs_register(struct iwl_trans * trans)829 static inline void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans) { }
830 #endif
831
832 void iwl_pcie_rx_allocator_work(struct work_struct *data);
833
834 /* common functions that are used by gen2 transport */
835 int iwl_pcie_gen2_apm_init(struct iwl_trans *trans);
836 void iwl_pcie_apm_config(struct iwl_trans *trans);
837 int iwl_pcie_prepare_card_hw(struct iwl_trans *trans);
838 void iwl_pcie_synchronize_irqs(struct iwl_trans *trans);
839 bool iwl_pcie_check_hw_rf_kill(struct iwl_trans *trans);
840 void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans,
841 bool was_in_rfkill);
842 void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq);
843 void iwl_pcie_apm_stop_master(struct iwl_trans *trans);
844 void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie);
845 int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
846 struct iwl_dma_ptr *ptr, size_t size);
847 void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr);
848 void iwl_pcie_apply_destination(struct iwl_trans *trans);
849
850 /* common functions that are used by gen3 transport */
851 void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power);
852
853 /* transport gen 2 exported functions */
854 int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans,
855 const struct fw_img *fw, bool run_in_rfkill);
856 void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans, u32 scd_addr);
857 int iwl_trans_pcie_gen2_send_hcmd(struct iwl_trans *trans,
858 struct iwl_host_cmd *cmd);
859 void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans);
860 void _iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans);
861 void iwl_pcie_d3_complete_suspend(struct iwl_trans *trans,
862 bool test, bool reset);
863 #endif /* __iwl_trans_int_pcie_h__ */
864