Path: blob/main/sys/contrib/dev/iwlwifi/pcie/gen1_2/internal.h
48406 views
/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */1/*2* Copyright (C) 2003-2015, 2018-2025 Intel Corporation3* Copyright (C) 2013-2015 Intel Mobile Communications GmbH4* Copyright (C) 2016-2017 Intel Deutschland GmbH5*/6#ifndef __iwl_trans_int_pcie_h__7#define __iwl_trans_int_pcie_h__89#include <linux/spinlock.h>10#include <linux/interrupt.h>11#include <linux/skbuff.h>12#include <linux/wait.h>13#include <linux/pci.h>14#include <linux/timer.h>15#include <linux/cpu.h>1617#include "iwl-fh.h"18#include "iwl-csr.h"19#include "iwl-trans.h"20#include "iwl-debug.h"21#include "iwl-io.h"22#include "iwl-op-mode.h"23#include "iwl-drv.h"24#include "pcie/iwl-context-info.h"2526/*27* RX related structures and functions28*/29#define RX_NUM_QUEUES 130#define RX_POST_REQ_ALLOC 231#define RX_CLAIM_REQ_ALLOC 832#define RX_PENDING_WATERMARK 1633#define FIRST_RX_QUEUE 5123435struct iwl_host_cmd;3637/*This file includes the declaration that are internal to the38* trans_pcie layer */3940/**41* struct iwl_rx_mem_buffer - driver-side RX buffer descriptor42* @page_dma: bus address of rxb page43* @page: driver's pointer to the rxb page44* @list: list entry for the membuffer45* @invalid: rxb is in driver ownership - not owned by HW46* @vid: index of this rxb in the global table47* @offset: indicates which offset of the page (in bytes)48* this buffer uses (if multiple RBs fit into one page)49*/50struct iwl_rx_mem_buffer {51dma_addr_t page_dma;52struct page *page;53struct list_head list;54u32 offset;55u16 vid;56bool invalid;57};5859/* interrupt statistics */60struct isr_statistics {61u32 hw;62u32 sw;63u32 err_code;64u32 sch;65u32 alive;66u32 rfkill;67u32 ctkill;68u32 wakeup;69u32 rx;70u32 tx;71u32 unhandled;72};7374/**75* struct iwl_rx_transfer_desc - transfer descriptor76* @addr: ptr to free buffer start address77* @rbid: unique tag of the buffer78* @reserved: reserved79*/80struct iwl_rx_transfer_desc {81__le16 rbid;82__le16 reserved[3];83__le64 addr;84} __packed;8586#define IWL_RX_CD_FLAGS_FRAGMENTED BIT(0)8788/**89* struct iwl_rx_completion_desc - completion descriptor90* @reserved1: reserved91* @rbid: unique tag of the received buffer92* @flags: flags (0: fragmented, all others: reserved)93* @reserved2: reserved94*/95struct iwl_rx_completion_desc {96__le32 reserved1;97__le16 rbid;98u8 flags;99u8 reserved2[25];100} __packed;101102/**103* struct iwl_rx_completion_desc_bz - Bz completion descriptor104* @rbid: unique tag of the received buffer105* @flags: flags (0: fragmented, all others: reserved)106* @reserved: reserved107*/108struct iwl_rx_completion_desc_bz {109__le16 rbid;110u8 flags;111u8 reserved[1];112} __packed;113114/**115* struct iwl_rxq - Rx queue116* @id: queue index117* @bd: driver's pointer to buffer of receive buffer descriptors (rbd).118* Address size is 32 bit in pre-9000 devices and 64 bit in 9000 devices.119* In AX210 devices it is a pointer to a list of iwl_rx_transfer_desc's120* @bd_dma: bus address of buffer of receive buffer descriptors (rbd)121* @used_bd: driver's pointer to buffer of used receive buffer descriptors (rbd)122* @used_bd_dma: physical address of buffer of used receive buffer descriptors (rbd)123* @read: Shared index to newest available Rx buffer124* @write: Shared index to oldest written Rx packet125* @write_actual: actual write pointer written to device, since we update in126* blocks of 8 only127* @free_count: Number of pre-allocated buffers in rx_free128* @used_count: Number of RBDs handled to allocator to use for allocation129* @write_actual:130* @rx_free: list of RBDs with allocated RB ready for use131* @rx_used: list of RBDs with no RB attached132* @need_update: flag to indicate we need to update read/write index133* @rb_stts: driver's pointer to receive buffer status134* @rb_stts_dma: bus address of receive buffer status135* @lock: per-queue lock136* @queue: actual rx queue. Not used for multi-rx queue.137* @next_rb_is_fragment: indicates that the previous RB that we handled set138* the fragmented flag, so the next one is still another fragment139* @napi: NAPI struct for this queue140* @queue_size: size of this queue141*142* NOTE: rx_free and rx_used are used as a FIFO for iwl_rx_mem_buffers143*/144struct iwl_rxq {145int id;146void *bd;147dma_addr_t bd_dma;148void *used_bd;149dma_addr_t used_bd_dma;150u32 read;151u32 write;152u32 free_count;153u32 used_count;154u32 write_actual;155u32 queue_size;156struct list_head rx_free;157struct list_head rx_used;158bool need_update, next_rb_is_fragment;159void *rb_stts;160dma_addr_t rb_stts_dma;161spinlock_t lock;162struct napi_struct napi;163struct iwl_rx_mem_buffer *queue[RX_QUEUE_SIZE];164};165166/**167* struct iwl_rb_allocator - Rx allocator168* @req_pending: number of requests the allcator had not processed yet169* @req_ready: number of requests honored and ready for claiming170* @rbd_allocated: RBDs with pages allocated and ready to be handled to171* the queue. This is a list of &struct iwl_rx_mem_buffer172* @rbd_empty: RBDs with no page attached for allocator use. This is a list173* of &struct iwl_rx_mem_buffer174* @lock: protects the rbd_allocated and rbd_empty lists175* @alloc_wq: work queue for background calls176* @rx_alloc: work struct for background calls177*/178struct iwl_rb_allocator {179atomic_t req_pending;180atomic_t req_ready;181struct list_head rbd_allocated;182struct list_head rbd_empty;183spinlock_t lock;184struct workqueue_struct *alloc_wq;185struct work_struct rx_alloc;186};187188/**189* iwl_get_closed_rb_stts - get closed rb stts from different structs190* @trans: transport pointer (for configuration)191* @rxq: the rxq to get the rb stts from192* Return: last closed RB index193*/194static inline u16 iwl_get_closed_rb_stts(struct iwl_trans *trans,195struct iwl_rxq *rxq)196{197if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {198__le16 *rb_stts = rxq->rb_stts;199200return le16_to_cpu(READ_ONCE(*rb_stts));201} else {202struct iwl_rb_status *rb_stts = rxq->rb_stts;203204return le16_to_cpu(READ_ONCE(rb_stts->closed_rb_num)) & 0xFFF;205}206}207208#ifdef CONFIG_IWLWIFI_DEBUGFS209/**210* enum iwl_fw_mon_dbgfs_state - the different states of the monitor_data211* debugfs file212*213* @IWL_FW_MON_DBGFS_STATE_CLOSED: the file is closed.214* @IWL_FW_MON_DBGFS_STATE_OPEN: the file is open.215* @IWL_FW_MON_DBGFS_STATE_DISABLED: the file is disabled, once this state is216* set the file can no longer be used.217*/218enum iwl_fw_mon_dbgfs_state {219IWL_FW_MON_DBGFS_STATE_CLOSED,220IWL_FW_MON_DBGFS_STATE_OPEN,221IWL_FW_MON_DBGFS_STATE_DISABLED,222};223#endif224225/**226* enum iwl_shared_irq_flags - level of sharing for irq227* @IWL_SHARED_IRQ_NON_RX: interrupt vector serves non rx causes.228* @IWL_SHARED_IRQ_FIRST_RSS: interrupt vector serves first RSS queue.229*/230enum iwl_shared_irq_flags {231IWL_SHARED_IRQ_NON_RX = BIT(0),232IWL_SHARED_IRQ_FIRST_RSS = BIT(1),233};234235/**236* enum iwl_image_response_code - image response values237* @IWL_IMAGE_RESP_DEF: the default value of the register238* @IWL_IMAGE_RESP_SUCCESS: iml was read successfully239* @IWL_IMAGE_RESP_FAIL: iml reading failed240*/241enum iwl_image_response_code {242IWL_IMAGE_RESP_DEF = 0,243IWL_IMAGE_RESP_SUCCESS = 1,244IWL_IMAGE_RESP_FAIL = 2,245};246247#ifdef CONFIG_IWLWIFI_DEBUGFS248/**249* struct cont_rec: continuous recording data structure250* @prev_wr_ptr: the last address that was read in monitor_data251* debugfs file252* @prev_wrap_cnt: the wrap count that was used during the last read in253* monitor_data debugfs file254* @state: the state of monitor_data debugfs file as described255* in &iwl_fw_mon_dbgfs_state enum256* @mutex: locked while reading from monitor_data debugfs file257*/258struct cont_rec {259u32 prev_wr_ptr;260u32 prev_wrap_cnt;261u8 state;262/* Used to sync monitor_data debugfs file with driver unload flow */263struct mutex mutex;264};265#endif266267enum iwl_pcie_fw_reset_state {268FW_RESET_IDLE,269FW_RESET_REQUESTED,270FW_RESET_OK,271FW_RESET_ERROR,272FW_RESET_TOP_REQUESTED,273};274275/**276* enum iwl_pcie_imr_status - imr dma transfer state277* @IMR_D2S_IDLE: default value of the dma transfer278* @IMR_D2S_REQUESTED: dma transfer requested279* @IMR_D2S_COMPLETED: dma transfer completed280* @IMR_D2S_ERROR: dma transfer error281*/282enum iwl_pcie_imr_status {283IMR_D2S_IDLE,284IMR_D2S_REQUESTED,285IMR_D2S_COMPLETED,286IMR_D2S_ERROR,287};288289/**290* struct iwl_pcie_txqs - TX queues data291*292* @queue_used: bit mask of used queues293* @queue_stopped: bit mask of stopped queues294* @txq: array of TXQ data structures representing the TXQs295* @scd_bc_tbls: gen1 pointer to the byte count table of the scheduler296* @bc_pool: bytecount DMA allocations pool297* @bc_tbl_size: bytecount table size298* @tso_hdr_page: page allocated (per CPU) for A-MSDU headers when doing TSO299* (and similar usage)300* @tfd: TFD data301* @tfd.max_tbs: max number of buffers per TFD302* @tfd.size: TFD size303* @tfd.addr_size: TFD/TB address size304*/305struct iwl_pcie_txqs {306unsigned long queue_used[BITS_TO_LONGS(IWL_MAX_TVQM_QUEUES)];307unsigned long queue_stopped[BITS_TO_LONGS(IWL_MAX_TVQM_QUEUES)];308struct iwl_txq *txq[IWL_MAX_TVQM_QUEUES];309struct dma_pool *bc_pool;310size_t bc_tbl_size;311struct iwl_tso_hdr_page __percpu *tso_hdr_page;312313struct {314u8 max_tbs;315u16 size;316u8 addr_size;317} tfd;318319struct iwl_dma_ptr scd_bc_tbls;320};321322/**323* struct iwl_trans_pcie - PCIe transport specific data324* @rxq: all the RX queue data325* @rx_pool: initial pool of iwl_rx_mem_buffer for all the queues326* @global_table: table mapping received VID from hw to rxb327* @rba: allocator for RX replenishing328* @ctxt_info: context information for FW self init329* @ctxt_info_v2: context information for v1 devices330* @prph_info: prph info for self init331* @prph_scratch: prph scratch for self init332* @ctxt_info_dma_addr: dma addr of context information333* @prph_info_dma_addr: dma addr of prph info334* @prph_scratch_dma_addr: dma addr of prph scratch335* @ctxt_info_dma_addr: dma addr of context information336* @iml: image loader image virtual address337* @iml_len: image loader image size338* @iml_dma_addr: image loader image DMA address339* @trans: pointer to the generic transport area340* @scd_base_addr: scheduler sram base address in SRAM341* @kw: keep warm address342* @pnvm_data: holds info about pnvm payloads allocated in DRAM343* @reduced_tables_data: holds info about power reduced tablse344* payloads allocated in DRAM345* @pci_dev: basic pci-network driver stuff346* @hw_base: pci hardware address support347* @ucode_write_complete: indicates that the ucode has been copied.348* @ucode_write_waitq: wait queue for uCode load349* @rx_page_order: page order for receive buffer size350* @rx_buf_bytes: RX buffer (RB) size in bytes351* @reg_lock: protect hw register access352* @mutex: to protect stop_device / start_fw / start_hw353* @fw_mon_data: fw continuous recording data354* @cmd_hold_nic_awake: indicates NIC is held awake for APMG workaround355* during commands in flight356* @msix_entries: array of MSI-X entries357* @msix_enabled: true if managed to enable MSI-X358* @shared_vec_mask: the type of causes the shared vector handles359* (see iwl_shared_irq_flags).360* @alloc_vecs: the number of interrupt vectors allocated by the OS361* @def_irq: default irq for non rx causes362* @fh_init_mask: initial unmasked fh causes363* @hw_init_mask: initial unmasked hw causes364* @fh_mask: current unmasked fh causes365* @hw_mask: current unmasked hw causes366* @in_rescan: true if we have triggered a device rescan367* @base_rb_stts: base virtual address of receive buffer status for all queues368* @base_rb_stts_dma: base physical address of receive buffer status369* @supported_dma_mask: DMA mask to validate the actual address against,370* will be DMA_BIT_MASK(11) or DMA_BIT_MASK(12) depending on the device371* @alloc_page_lock: spinlock for the page allocator372* @alloc_page: allocated page to still use parts of373* @alloc_page_used: how much of the allocated page was already used (bytes)374* @imr_status: imr dma state machine375* @imr_waitq: imr wait queue for dma completion376* @rf_name: name/version of the CRF, if any377* @use_ict: whether or not ICT (interrupt table) is used378* @ict_index: current ICT read index379* @ict_tbl: ICT table pointer380* @ict_tbl_dma: ICT table DMA address381* @inta_mask: interrupt (INT-A) mask382* @irq_lock: lock to synchronize IRQ handling383* @txq_memory: TXQ allocation array384* @sx_waitq: waitqueue for Sx transitions385* @sx_state: state tracking Sx transitions386* @opmode_down: indicates opmode went away387* @num_rx_bufs: number of RX buffers to allocate/use388* @affinity_mask: IRQ affinity mask for each RX queue389* @debug_rfkill: RF-kill debugging state, -1 for unset, 0/1 for radio390* enable/disable391* @fw_reset_state: state of FW reset handshake392* @fw_reset_waitq: waitqueue for FW reset handshake393* @is_down: indicates the NIC is down394* @isr_stats: interrupt statistics395* @napi_dev: (fake) netdev for NAPI registration396* @txqs: transport tx queues data.397* @me_present: WiAMT/CSME is detected as present (1), not present (0)398* or unknown (-1, so can still use it as a boolean safely)399* @me_recheck_wk: worker to recheck WiAMT/CSME presence400* @invalid_tx_cmd: invalid TX command buffer401* @wait_command_queue: wait queue for sync commands402*/403struct iwl_trans_pcie {404struct iwl_rxq *rxq;405struct iwl_rx_mem_buffer *rx_pool;406struct iwl_rx_mem_buffer **global_table;407struct iwl_rb_allocator rba;408union {409struct iwl_context_info *ctxt_info;410struct iwl_context_info_v2 *ctxt_info_v2;411};412struct iwl_prph_info *prph_info;413struct iwl_prph_scratch *prph_scratch;414void *iml;415size_t iml_len;416dma_addr_t ctxt_info_dma_addr;417dma_addr_t prph_info_dma_addr;418dma_addr_t prph_scratch_dma_addr;419dma_addr_t iml_dma_addr;420struct iwl_trans *trans;421422struct net_device *napi_dev;423424/* INT ICT Table */425__le32 *ict_tbl;426dma_addr_t ict_tbl_dma;427int ict_index;428bool use_ict;429bool is_down, opmode_down;430s8 debug_rfkill;431struct isr_statistics isr_stats;432433spinlock_t irq_lock;434struct mutex mutex;435u32 inta_mask;436u32 scd_base_addr;437struct iwl_dma_ptr kw;438439/* pnvm data */440struct iwl_dram_regions pnvm_data;441struct iwl_dram_regions reduced_tables_data;442443struct iwl_txq *txq_memory;444445/* PCI bus related data */446struct pci_dev *pci_dev;447u8 __iomem *hw_base;448449bool ucode_write_complete;450enum {451IWL_SX_INVALID = 0,452IWL_SX_WAITING,453IWL_SX_ERROR,454IWL_SX_COMPLETE,455} sx_state;456wait_queue_head_t ucode_write_waitq;457wait_queue_head_t sx_waitq;458459u16 num_rx_bufs;460461u32 rx_page_order;462u32 rx_buf_bytes;463u32 supported_dma_mask;464465/* allocator lock for the two values below */466spinlock_t alloc_page_lock;467struct page *alloc_page;468u32 alloc_page_used;469470/*protect hw register */471spinlock_t reg_lock;472bool cmd_hold_nic_awake;473474#ifdef CONFIG_IWLWIFI_DEBUGFS475struct cont_rec fw_mon_data;476#endif477478struct msix_entry msix_entries[IWL_MAX_RX_HW_QUEUES];479bool msix_enabled;480u8 shared_vec_mask;481u32 alloc_vecs;482u32 def_irq;483u32 fh_init_mask;484u32 hw_init_mask;485u32 fh_mask;486u32 hw_mask;487cpumask_t affinity_mask[IWL_MAX_RX_HW_QUEUES];488u16 tx_cmd_queue_size;489bool in_rescan;490491void *base_rb_stts;492dma_addr_t base_rb_stts_dma;493494enum iwl_pcie_fw_reset_state fw_reset_state;495wait_queue_head_t fw_reset_waitq;496enum iwl_pcie_imr_status imr_status;497wait_queue_head_t imr_waitq;498char rf_name[32];499500struct iwl_pcie_txqs txqs;501502s8 me_present;503struct delayed_work me_recheck_wk;504505struct iwl_dma_ptr invalid_tx_cmd;506507wait_queue_head_t wait_command_queue;508};509510static inline struct iwl_trans_pcie *511IWL_TRANS_GET_PCIE_TRANS(struct iwl_trans *trans)512{513return (void *)trans->trans_specific;514}515516static inline void iwl_pcie_clear_irq(struct iwl_trans *trans, int queue)517{518/*519* Before sending the interrupt the HW disables it to prevent520* a nested interrupt. This is done by writing 1 to the corresponding521* bit in the mask register. After handling the interrupt, it should be522* re-enabled by clearing this bit. This register is defined as523* write 1 clear (W1C) register, meaning that it's being clear524* by writing 1 to the bit.525*/526iwl_write32(trans, CSR_MSIX_AUTOMASK_ST_AD, BIT(queue));527}528529static inline struct iwl_trans *530iwl_trans_pcie_get_trans(struct iwl_trans_pcie *trans_pcie)531{532return container_of((void *)trans_pcie, struct iwl_trans,533trans_specific);534}535536/*537* Convention: trans API functions: iwl_trans_pcie_XXX538* Other functions: iwl_pcie_XXX539*/540void iwl_trans_pcie_free(struct iwl_trans *trans);541void iwl_trans_pcie_free_pnvm_dram_regions(struct iwl_dram_regions *dram_regions,542struct device *dev);543544bool __iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, bool silent);545#define _iwl_trans_pcie_grab_nic_access(trans, silent) \546__cond_lock(nic_access_nobh, \547likely(__iwl_trans_pcie_grab_nic_access(trans, silent)))548549void iwl_trans_pcie_check_product_reset_status(struct pci_dev *pdev);550void iwl_trans_pcie_check_product_reset_mode(struct pci_dev *pdev);551552/*****************************************************553* RX554******************************************************/555int iwl_pcie_rx_init(struct iwl_trans *trans);556int iwl_pcie_gen2_rx_init(struct iwl_trans *trans);557irqreturn_t iwl_pcie_msix_isr(int irq, void *data);558irqreturn_t iwl_pcie_irq_handler(int irq, void *dev_id);559irqreturn_t iwl_pcie_irq_msix_handler(int irq, void *dev_id);560irqreturn_t iwl_pcie_irq_rx_msix_handler(int irq, void *dev_id);561int iwl_pcie_rx_stop(struct iwl_trans *trans);562void iwl_pcie_rx_free(struct iwl_trans *trans);563void iwl_pcie_free_rbs_pool(struct iwl_trans *trans);564void iwl_pcie_rx_init_rxb_lists(struct iwl_rxq *rxq);565void iwl_pcie_rx_napi_sync(struct iwl_trans *trans);566void iwl_pcie_rxq_alloc_rbs(struct iwl_trans *trans, gfp_t priority,567struct iwl_rxq *rxq);568569/*****************************************************570* ICT - interrupt handling571******************************************************/572irqreturn_t iwl_pcie_isr(int irq, void *data);573int iwl_pcie_alloc_ict(struct iwl_trans *trans);574void iwl_pcie_free_ict(struct iwl_trans *trans);575void iwl_pcie_reset_ict(struct iwl_trans *trans);576void iwl_pcie_disable_ict(struct iwl_trans *trans);577578/*****************************************************579* TX / HCMD580******************************************************/581/* We need 2 entries for the TX command and header, and another one might582* be needed for potential data in the SKB's head. The remaining ones can583* be used for frags.584*/585#define IWL_TRANS_PCIE_MAX_FRAGS(trans_pcie) ((trans_pcie)->txqs.tfd.max_tbs - 3)586587struct iwl_tso_hdr_page {588struct page *page;589u8 *pos;590};591592/*593* Note that we put this struct *last* in the page. By doing that, we ensure594* that no TB referencing this page can trigger the 32-bit boundary hardware595* bug.596*/597struct iwl_tso_page_info {598dma_addr_t dma_addr;599struct page *next;600refcount_t use_count;601};602603#define IWL_TSO_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(struct iwl_tso_page_info))604#define IWL_TSO_PAGE_INFO(addr) \605((struct iwl_tso_page_info *)(((unsigned long)addr & PAGE_MASK) + \606IWL_TSO_PAGE_DATA_SIZE))607608int iwl_pcie_tx_init(struct iwl_trans *trans);609void iwl_pcie_tx_start(struct iwl_trans *trans);610int iwl_pcie_tx_stop(struct iwl_trans *trans);611void iwl_pcie_tx_free(struct iwl_trans *trans);612bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int queue, u16 ssn,613const struct iwl_trans_txq_scd_cfg *cfg,614unsigned int wdg_timeout);615void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int queue,616bool configure_scd);617void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,618bool shared_mode);619int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,620struct iwl_device_tx_cmd *dev_cmd, int txq_id);621void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans);622void iwl_pcie_hcmd_complete(struct iwl_trans *trans,623struct iwl_rx_cmd_buffer *rxb);624void iwl_trans_pcie_tx_reset(struct iwl_trans *trans);625int iwl_pcie_txq_alloc(struct iwl_trans *trans, struct iwl_txq *txq,626int slots_num, bool cmd_queue);627628dma_addr_t iwl_pcie_get_sgt_tb_phys(struct sg_table *sgt, unsigned int offset,629unsigned int len);630struct sg_table *iwl_pcie_prep_tso(struct iwl_trans *trans, struct sk_buff *skb,631struct iwl_cmd_meta *cmd_meta,632u8 **hdr, unsigned int hdr_room,633unsigned int offset);634635void iwl_pcie_free_tso_pages(struct iwl_trans *trans, struct sk_buff *skb,636struct iwl_cmd_meta *cmd_meta);637638static inline dma_addr_t iwl_pcie_get_tso_page_phys(void *addr)639{640dma_addr_t res;641642res = IWL_TSO_PAGE_INFO(addr)->dma_addr;643res += (unsigned long)addr & ~PAGE_MASK;644645return res;646}647648static inline dma_addr_t649iwl_txq_get_first_tb_dma(struct iwl_txq *txq, int idx)650{651return txq->first_tb_dma +652sizeof(struct iwl_pcie_first_tb_buf) * idx;653}654655static inline u16 iwl_txq_get_cmd_index(const struct iwl_txq *q, u32 index)656{657return index & (q->n_window - 1);658}659660static inline void *iwl_txq_get_tfd(struct iwl_trans *trans,661struct iwl_txq *txq, int idx)662{663struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);664665if (trans->mac_cfg->gen2)666idx = iwl_txq_get_cmd_index(txq, idx);667668return (u8 *)txq->tfds + trans_pcie->txqs.tfd.size * idx;669}670671/*672* We need this inline in case dma_addr_t is only 32-bits - since the673* hardware is always 64-bit, the issue can still occur in that case,674* so use u64 for 'phys' here to force the addition in 64-bit.675*/676static inline bool iwl_txq_crosses_4g_boundary(u64 phys, u16 len)677{678return upper_32_bits(phys) != upper_32_bits(phys + len);679}680681int iwl_txq_space(struct iwl_trans *trans, const struct iwl_txq *q);682683static inline void iwl_txq_stop(struct iwl_trans *trans, struct iwl_txq *txq)684{685struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);686687if (!test_and_set_bit(txq->id, trans_pcie->txqs.queue_stopped)) {688iwl_op_mode_queue_full(trans->op_mode, txq->id);689IWL_DEBUG_TX_QUEUES(trans, "Stop hwq %d\n", txq->id);690} else {691IWL_DEBUG_TX_QUEUES(trans, "hwq %d already stopped\n",692txq->id);693}694}695696/**697* iwl_txq_inc_wrap - increment queue index, wrap back to beginning698* @trans: the transport (for configuration data)699* @index: current index700* Return: the queue index incremented, subject to wrapping701*/702static inline int iwl_txq_inc_wrap(struct iwl_trans *trans, int index)703{704return ++index &705(trans->mac_cfg->base->max_tfd_queue_size - 1);706}707708/**709* iwl_txq_dec_wrap - decrement queue index, wrap back to end710* @trans: the transport (for configuration data)711* @index: current index712* Return: the queue index decremented, subject to wrapping713*/714static inline int iwl_txq_dec_wrap(struct iwl_trans *trans, int index)715{716return --index &717(trans->mac_cfg->base->max_tfd_queue_size - 1);718}719720void iwl_txq_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq);721722static inline void723iwl_trans_pcie_wake_queue(struct iwl_trans *trans, struct iwl_txq *txq)724{725struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);726727if (test_and_clear_bit(txq->id, trans_pcie->txqs.queue_stopped)) {728IWL_DEBUG_TX_QUEUES(trans, "Wake hwq %d\n", txq->id);729iwl_op_mode_queue_not_full(trans->op_mode, txq->id);730}731}732733int iwl_txq_gen2_set_tb(struct iwl_trans *trans,734struct iwl_tfh_tfd *tfd, dma_addr_t addr,735u16 len);736737static inline void iwl_txq_set_tfd_invalid_gen2(struct iwl_trans *trans,738struct iwl_tfh_tfd *tfd)739{740struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);741742tfd->num_tbs = 0;743744iwl_txq_gen2_set_tb(trans, tfd, trans_pcie->invalid_tx_cmd.dma,745trans_pcie->invalid_tx_cmd.size);746}747748void iwl_txq_gen2_tfd_unmap(struct iwl_trans *trans,749struct iwl_cmd_meta *meta,750struct iwl_tfh_tfd *tfd);751752int iwl_txq_dyn_alloc(struct iwl_trans *trans, u32 flags,753u32 sta_mask, u8 tid,754int size, unsigned int timeout);755756int iwl_txq_gen2_tx(struct iwl_trans *trans, struct sk_buff *skb,757struct iwl_device_tx_cmd *dev_cmd, int txq_id);758759void iwl_txq_dyn_free(struct iwl_trans *trans, int queue);760void iwl_txq_gen2_tx_free(struct iwl_trans *trans);761int iwl_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,762int slots_num, bool cmd_queue);763int iwl_txq_gen2_init(struct iwl_trans *trans, int txq_id,764int queue_size);765766static inline u16 iwl_txq_gen1_tfd_tb_get_len(struct iwl_trans *trans,767void *_tfd, u8 idx)768{769struct iwl_tfd *tfd;770struct iwl_tfd_tb *tb;771772if (trans->mac_cfg->gen2) {773struct iwl_tfh_tfd *tfh_tfd = _tfd;774struct iwl_tfh_tb *tfh_tb = &tfh_tfd->tbs[idx];775776return le16_to_cpu(tfh_tb->tb_len);777}778779tfd = (struct iwl_tfd *)_tfd;780tb = &tfd->tbs[idx];781782return le16_to_cpu(tb->hi_n_len) >> 4;783}784785void iwl_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,786struct sk_buff_head *skbs, bool is_flush);787void iwl_pcie_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr);788void iwl_pcie_freeze_txq_timer(struct iwl_trans *trans,789unsigned long txqs, bool freeze);790int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, int txq_idx);791int iwl_trans_pcie_wait_txqs_empty(struct iwl_trans *trans, u32 txq_bm);792793/*****************************************************794* Error handling795******************************************************/796void iwl_pcie_dump_csr(struct iwl_trans *trans);797798/*****************************************************799* Helpers800******************************************************/801static inline void _iwl_disable_interrupts(struct iwl_trans *trans)802{803struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);804805clear_bit(STATUS_INT_ENABLED, &trans->status);806if (!trans_pcie->msix_enabled) {807/* disable interrupts from uCode/NIC to host */808iwl_write32(trans, CSR_INT_MASK, 0x00000000);809810/* acknowledge/clear/reset any interrupts still pending811* from uCode or flow handler (Rx/Tx DMA) */812iwl_write32(trans, CSR_INT, 0xffffffff);813iwl_write32(trans, CSR_FH_INT_STATUS, 0xffffffff);814} else {815/* disable all the interrupt we might use */816iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,817trans_pcie->fh_init_mask);818iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,819trans_pcie->hw_init_mask);820}821IWL_DEBUG_ISR(trans, "Disabled interrupts\n");822}823824static inline int iwl_pcie_get_num_sections(const struct fw_img *fw,825int start)826{827int i = 0;828829while (start < fw->num_sec &&830fw->sec[start].offset != CPU1_CPU2_SEPARATOR_SECTION &&831fw->sec[start].offset != PAGING_SEPARATOR_SECTION) {832start++;833i++;834}835836return i;837}838839static inline void iwl_pcie_ctxt_info_free_fw_img(struct iwl_trans *trans)840{841struct iwl_self_init_dram *dram = &trans->init_dram;842int i;843844if (!dram->fw) {845WARN_ON(dram->fw_cnt);846return;847}848849for (i = 0; i < dram->fw_cnt; i++)850dma_free_coherent(trans->dev, dram->fw[i].size,851dram->fw[i].block, dram->fw[i].physical);852853kfree(dram->fw);854dram->fw_cnt = 0;855dram->fw = NULL;856}857858static inline void iwl_disable_interrupts(struct iwl_trans *trans)859{860struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);861862spin_lock_bh(&trans_pcie->irq_lock);863_iwl_disable_interrupts(trans);864spin_unlock_bh(&trans_pcie->irq_lock);865}866867static inline void _iwl_enable_interrupts(struct iwl_trans *trans)868{869struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);870871IWL_DEBUG_ISR(trans, "Enabling interrupts\n");872set_bit(STATUS_INT_ENABLED, &trans->status);873if (!trans_pcie->msix_enabled) {874trans_pcie->inta_mask = CSR_INI_SET_MASK;875iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);876} else {877/*878* fh/hw_mask keeps all the unmasked causes.879* Unlike msi, in msix cause is enabled when it is unset.880*/881trans_pcie->hw_mask = trans_pcie->hw_init_mask;882trans_pcie->fh_mask = trans_pcie->fh_init_mask;883iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,884~trans_pcie->fh_mask);885iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,886~trans_pcie->hw_mask);887}888}889890static inline void iwl_enable_interrupts(struct iwl_trans *trans)891{892struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);893894spin_lock_bh(&trans_pcie->irq_lock);895_iwl_enable_interrupts(trans);896spin_unlock_bh(&trans_pcie->irq_lock);897}898static inline void iwl_enable_hw_int_msk_msix(struct iwl_trans *trans, u32 msk)899{900struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);901902iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD, ~msk);903trans_pcie->hw_mask = msk;904}905906static inline void iwl_enable_fh_int_msk_msix(struct iwl_trans *trans, u32 msk)907{908struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);909910iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~msk);911trans_pcie->fh_mask = msk;912}913914static inline void iwl_enable_fw_load_int(struct iwl_trans *trans)915{916struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);917918IWL_DEBUG_ISR(trans, "Enabling FW load interrupt\n");919if (!trans_pcie->msix_enabled) {920trans_pcie->inta_mask = CSR_INT_BIT_FH_TX;921iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);922} else {923iwl_write32(trans, CSR_MSIX_HW_INT_MASK_AD,924trans_pcie->hw_init_mask);925iwl_enable_fh_int_msk_msix(trans,926MSIX_FH_INT_CAUSES_D2S_CH0_NUM);927}928}929930static inline void iwl_enable_fw_load_int_ctx_info(struct iwl_trans *trans,931bool top_reset)932{933struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);934935IWL_DEBUG_ISR(trans, "Enabling %s interrupt only\n",936top_reset ? "RESET" : "ALIVE");937938if (!trans_pcie->msix_enabled) {939/*940* When we'll receive the ALIVE interrupt, the ISR will call941* iwl_enable_fw_load_int_ctx_info again to set the ALIVE942* interrupt (which is not really needed anymore) but also the943* RX interrupt which will allow us to receive the ALIVE944* notification (which is Rx) and continue the flow.945*/946if (top_reset)947trans_pcie->inta_mask = CSR_INT_BIT_RESET_DONE;948else949trans_pcie->inta_mask = CSR_INT_BIT_ALIVE |950CSR_INT_BIT_FH_RX;951iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);952} else {953u32 val = top_reset ? MSIX_HW_INT_CAUSES_REG_RESET_DONE954: MSIX_HW_INT_CAUSES_REG_ALIVE;955956iwl_enable_hw_int_msk_msix(trans, val);957958if (top_reset)959return;960/*961* Leave all the FH causes enabled to get the ALIVE962* notification.963*/964iwl_enable_fh_int_msk_msix(trans, trans_pcie->fh_init_mask);965}966}967968static inline const char *queue_name(struct device *dev,969struct iwl_trans_pcie *trans_p, int i)970{971if (trans_p->shared_vec_mask) {972int vec = trans_p->shared_vec_mask &973IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;974975if (i == 0)976return DRV_NAME ":shared_IRQ";977978return devm_kasprintf(dev, GFP_KERNEL,979DRV_NAME ":queue_%d", i + vec);980}981if (i == 0)982return DRV_NAME ":default_queue";983984if (i == trans_p->alloc_vecs - 1)985return DRV_NAME ":exception";986987return devm_kasprintf(dev, GFP_KERNEL,988DRV_NAME ":queue_%d", i);989}990991static inline void iwl_enable_rfkill_int(struct iwl_trans *trans)992{993struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);994995IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");996if (!trans_pcie->msix_enabled) {997trans_pcie->inta_mask = CSR_INT_BIT_RF_KILL;998iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);999} else {1000iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD,1001trans_pcie->fh_init_mask);1002iwl_enable_hw_int_msk_msix(trans,1003MSIX_HW_INT_CAUSES_REG_RF_KILL);1004}10051006if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_9000) {1007/*1008* On 9000-series devices this bit isn't enabled by default, so1009* when we power down the device we need set the bit to allow it1010* to wake up the PCI-E bus for RF-kill interrupts.1011*/1012iwl_set_bit(trans, CSR_GP_CNTRL,1013CSR_GP_CNTRL_REG_FLAG_RFKILL_WAKE_L1A_EN);1014}1015}10161017void iwl_pcie_handle_rfkill_irq(struct iwl_trans *trans, bool from_irq);10181019static inline bool iwl_is_rfkill_set(struct iwl_trans *trans)1020{1021struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);10221023lockdep_assert_held(&trans_pcie->mutex);10241025if (trans_pcie->debug_rfkill == 1)1026return true;10271028return !(iwl_read32(trans, CSR_GP_CNTRL) &1029CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);1030}10311032static inline bool iwl_pcie_dbg_on(struct iwl_trans *trans)1033{1034return (trans->dbg.dest_tlv || iwl_trans_dbg_ini_valid(trans));1035}10361037void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state, bool from_irq);10381039#ifdef CONFIG_IWLWIFI_DEBUGFS1040void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans);1041void iwl_trans_pcie_debugfs_cleanup(struct iwl_trans *trans);1042#else1043static inline void iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans) { }1044#endif10451046void iwl_pcie_rx_allocator_work(struct work_struct *data);10471048/* common trans ops for all generations transports */1049void iwl_trans_pcie_op_mode_enter(struct iwl_trans *trans);1050int _iwl_trans_pcie_start_hw(struct iwl_trans *trans);1051int iwl_trans_pcie_start_hw(struct iwl_trans *trans);1052void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans);1053void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val);1054void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val);1055u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs);1056u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg);1057void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr, u32 val);1058int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,1059void *buf, int dwords);1060int iwl_trans_pcie_sw_reset(struct iwl_trans *trans, bool retake_ownership);1061struct iwl_trans_dump_data *1062iwl_trans_pcie_dump_data(struct iwl_trans *trans, u32 dump_mask,1063const struct iwl_dump_sanitize_ops *sanitize_ops,1064void *sanitize_ctx);1065int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,1066enum iwl_d3_status *status,1067bool test, bool reset);1068int iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test, bool reset);1069void iwl_trans_pci_interrupts(struct iwl_trans *trans, bool enable);1070void iwl_trans_pcie_sync_nmi(struct iwl_trans *trans);1071void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,1072u32 mask, u32 value);1073int iwl_trans_pcie_read_config32(struct iwl_trans *trans, u32 ofs,1074u32 *val);1075bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans);1076void __releases(nic_access_nobh)1077iwl_trans_pcie_release_nic_access(struct iwl_trans *trans);1078void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power);1079int iwl_pci_gen1_2_probe(struct pci_dev *pdev,1080const struct pci_device_id *ent,1081const struct iwl_mac_cfg *mac_cfg,1082u8 __iomem *hw_base, u32 hw_rev);10831084/* transport gen 1 exported functions */1085void iwl_trans_pcie_fw_alive(struct iwl_trans *trans);1086int iwl_trans_pcie_start_fw(struct iwl_trans *trans,1087const struct iwl_fw *fw,1088const struct fw_img *img,1089bool run_in_rfkill);1090void iwl_trans_pcie_stop_device(struct iwl_trans *trans);10911092/* common functions that are used by gen2 transport */1093void iwl_trans_pcie_gen2_op_mode_leave(struct iwl_trans *trans);1094int iwl_pcie_gen2_apm_init(struct iwl_trans *trans);1095void iwl_pcie_apm_config(struct iwl_trans *trans);1096int iwl_pcie_prepare_card_hw(struct iwl_trans *trans);1097void iwl_pcie_synchronize_irqs(struct iwl_trans *trans);1098bool iwl_pcie_check_hw_rf_kill(struct iwl_trans *trans);1099void iwl_trans_pcie_handle_stop_rfkill(struct iwl_trans *trans,1100bool was_in_rfkill);1101void iwl_pcie_apm_stop_master(struct iwl_trans *trans);1102void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie);1103int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,1104struct iwl_dma_ptr *ptr, size_t size);1105void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr);1106void iwl_pcie_apply_destination(struct iwl_trans *trans);11071108/* transport gen 2 exported functions */1109int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans,1110const struct iwl_fw *fw,1111const struct fw_img *img,1112bool run_in_rfkill);1113void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans);1114void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans);1115int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans,1116struct iwl_host_cmd *cmd);1117int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,1118struct iwl_host_cmd *cmd);1119void iwl_trans_pcie_copy_imr_fh(struct iwl_trans *trans,1120u32 dst_addr, u64 src_addr, u32 byte_cnt);1121int iwl_trans_pcie_copy_imr(struct iwl_trans *trans,1122u32 dst_addr, u64 src_addr, u32 byte_cnt);1123int iwl_trans_pcie_rxq_dma_data(struct iwl_trans *trans, int queue,1124struct iwl_trans_rxq_dma_data *data);11251126#endif /* __iwl_trans_int_pcie_h__ */112711281129