12774f206SBjoern A. Zeeb /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */ 22774f206SBjoern A. Zeeb /* Copyright(c) 2018-2019 Realtek Corporation 32774f206SBjoern A. Zeeb */ 42774f206SBjoern A. Zeeb 52774f206SBjoern A. Zeeb #ifndef __RTK_PCI_H_ 62774f206SBjoern A. Zeeb #define __RTK_PCI_H_ 72774f206SBjoern A. Zeeb 82774f206SBjoern A. Zeeb #include "main.h" 92774f206SBjoern A. Zeeb 102774f206SBjoern A. Zeeb #define RTK_DEFAULT_TX_DESC_NUM 128 112774f206SBjoern A. Zeeb #define RTK_BEQ_TX_DESC_NUM 256 122774f206SBjoern A. Zeeb 132774f206SBjoern A. Zeeb #define RTK_MAX_RX_DESC_NUM 512 142774f206SBjoern A. Zeeb /* 11K + rx desc size */ 152774f206SBjoern A. Zeeb #define RTK_PCI_RX_BUF_SIZE (11454 + 24) 162774f206SBjoern A. Zeeb 172774f206SBjoern A. Zeeb #define RTK_PCI_CTRL 0x300 182774f206SBjoern A. Zeeb #define BIT_RST_TRXDMA_INTF BIT(20) 192774f206SBjoern A. Zeeb #define BIT_RX_TAG_EN BIT(15) 202774f206SBjoern A. Zeeb #define REG_DBI_WDATA_V1 0x03E8 212774f206SBjoern A. Zeeb #define REG_DBI_RDATA_V1 0x03EC 222774f206SBjoern A. Zeeb #define REG_DBI_FLAG_V1 0x03F0 232774f206SBjoern A. Zeeb #define BIT_DBI_RFLAG BIT(17) 242774f206SBjoern A. Zeeb #define BIT_DBI_WFLAG BIT(16) 252774f206SBjoern A. Zeeb #define BITS_DBI_WREN GENMASK(15, 12) 262774f206SBjoern A. Zeeb #define BITS_DBI_ADDR_MASK GENMASK(11, 2) 272774f206SBjoern A. Zeeb 282774f206SBjoern A. Zeeb #define REG_MDIO_V1 0x03F4 292774f206SBjoern A. Zeeb #define REG_PCIE_MIX_CFG 0x03F8 302774f206SBjoern A. Zeeb #define BITS_MDIO_ADDR_MASK GENMASK(4, 0) 312774f206SBjoern A. Zeeb #define BIT_MDIO_WFLAG_V1 BIT(5) 322774f206SBjoern A. Zeeb #define RTW_PCI_MDIO_PG_SZ BIT(5) 332774f206SBjoern A. Zeeb #define RTW_PCI_MDIO_PG_OFFS_G1 0 342774f206SBjoern A. Zeeb #define RTW_PCI_MDIO_PG_OFFS_G2 2 352774f206SBjoern A. Zeeb #define RTW_PCI_WR_RETRY_CNT 20 362774f206SBjoern A. Zeeb 372774f206SBjoern A. Zeeb #define RTK_PCIE_LINK_CFG 0x0719 382774f206SBjoern A. Zeeb #define BIT_CLKREQ_SW_EN BIT(4) 392774f206SBjoern A. Zeeb #define BIT_L1_SW_EN BIT(3) 402774f206SBjoern A. Zeeb #define BIT_CLKREQ_N_PAD BIT(0) 412774f206SBjoern A. Zeeb #define RTK_PCIE_CLKDLY_CTRL 0x0725 422774f206SBjoern A. Zeeb 432774f206SBjoern A. Zeeb #define BIT_PCI_BCNQ_FLAG BIT(4) 442774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_BCNQ 0x308 452774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_H2CQ 0x1320 462774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_MGMTQ 0x310 472774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_BKQ 0x330 482774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_BEQ 0x328 492774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_VIQ 0x320 502774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_VOQ 0x318 512774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_DESA_HI0Q 0x340 522774f206SBjoern A. Zeeb #define RTK_PCI_RXBD_DESA_MPDUQ 0x338 532774f206SBjoern A. Zeeb 542774f206SBjoern A. Zeeb #define TRX_BD_IDX_MASK GENMASK(11, 0) 552774f206SBjoern A. Zeeb #define TRX_BD_HW_IDX_MASK GENMASK(27, 16) 562774f206SBjoern A. Zeeb 572774f206SBjoern A. Zeeb /* BCNQ is specialized for rsvd page, does not need to specify a number */ 582774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_H2CQ 0x1328 592774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_MGMTQ 0x380 602774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_BKQ 0x38A 612774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_BEQ 0x388 622774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_VIQ 0x386 632774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_VOQ 0x384 642774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_NUM_HI0Q 0x38C 652774f206SBjoern A. Zeeb #define RTK_PCI_RXBD_NUM_MPDUQ 0x382 662774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_H2CQ 0x132C 672774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_MGMTQ 0x3B0 682774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_BKQ 0x3AC 692774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_BEQ 0x3A8 702774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_VIQ 0x3A4 712774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_VOQ 0x3A0 722774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_IDX_HI0Q 0x3B8 732774f206SBjoern A. Zeeb #define RTK_PCI_RXBD_IDX_MPDUQ 0x3B4 742774f206SBjoern A. Zeeb 752774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_RWPTR_CLR 0x39C 762774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_H2CQ_CSR 0x1330 772774f206SBjoern A. Zeeb 782774f206SBjoern A. Zeeb #define BIT_CLR_H2CQ_HOST_IDX BIT(16) 792774f206SBjoern A. Zeeb #define BIT_CLR_H2CQ_HW_IDX BIT(8) 802774f206SBjoern A. Zeeb 812774f206SBjoern A. Zeeb #define RTK_PCI_HIMR0 0x0B0 822774f206SBjoern A. Zeeb #define RTK_PCI_HISR0 0x0B4 832774f206SBjoern A. Zeeb #define RTK_PCI_HIMR1 0x0B8 842774f206SBjoern A. Zeeb #define RTK_PCI_HISR1 0x0BC 852774f206SBjoern A. Zeeb #define RTK_PCI_HIMR2 0x10B0 862774f206SBjoern A. Zeeb #define RTK_PCI_HISR2 0x10B4 872774f206SBjoern A. Zeeb #define RTK_PCI_HIMR3 0x10B8 882774f206SBjoern A. Zeeb #define RTK_PCI_HISR3 0x10BC 892774f206SBjoern A. Zeeb /* IMR 0 */ 902774f206SBjoern A. Zeeb #define IMR_TIMER2 BIT(31) 912774f206SBjoern A. Zeeb #define IMR_TIMER1 BIT(30) 922774f206SBjoern A. Zeeb #define IMR_PSTIMEOUT BIT(29) 932774f206SBjoern A. Zeeb #define IMR_GTINT4 BIT(28) 942774f206SBjoern A. Zeeb #define IMR_GTINT3 BIT(27) 952774f206SBjoern A. Zeeb #define IMR_TBDER BIT(26) 962774f206SBjoern A. Zeeb #define IMR_TBDOK BIT(25) 972774f206SBjoern A. Zeeb #define IMR_TSF_BIT32_TOGGLE BIT(24) 982774f206SBjoern A. Zeeb #define IMR_BCNDMAINT0 BIT(20) 992774f206SBjoern A. Zeeb #define IMR_BCNDOK0 BIT(16) 1002774f206SBjoern A. Zeeb #define IMR_HSISR_IND_ON_INT BIT(15) 1012774f206SBjoern A. Zeeb #define IMR_BCNDMAINT_E BIT(14) 1022774f206SBjoern A. Zeeb #define IMR_ATIMEND BIT(12) 1032774f206SBjoern A. Zeeb #define IMR_HISR1_IND_INT BIT(11) 1042774f206SBjoern A. Zeeb #define IMR_C2HCMD BIT(10) 1052774f206SBjoern A. Zeeb #define IMR_CPWM2 BIT(9) 1062774f206SBjoern A. Zeeb #define IMR_CPWM BIT(8) 1072774f206SBjoern A. Zeeb #define IMR_HIGHDOK BIT(7) 1082774f206SBjoern A. Zeeb #define IMR_MGNTDOK BIT(6) 1092774f206SBjoern A. Zeeb #define IMR_BKDOK BIT(5) 1102774f206SBjoern A. Zeeb #define IMR_BEDOK BIT(4) 1112774f206SBjoern A. Zeeb #define IMR_VIDOK BIT(3) 1122774f206SBjoern A. Zeeb #define IMR_VODOK BIT(2) 1132774f206SBjoern A. Zeeb #define IMR_RDU BIT(1) 1142774f206SBjoern A. Zeeb #define IMR_ROK BIT(0) 1152774f206SBjoern A. Zeeb /* IMR 1 */ 1162774f206SBjoern A. Zeeb #define IMR_TXFIFO_TH_INT BIT(30) 1172774f206SBjoern A. Zeeb #define IMR_BTON_STS_UPDATE BIT(29) 1182774f206SBjoern A. Zeeb #define IMR_MCUERR BIT(28) 1192774f206SBjoern A. Zeeb #define IMR_BCNDMAINT7 BIT(27) 1202774f206SBjoern A. Zeeb #define IMR_BCNDMAINT6 BIT(26) 1212774f206SBjoern A. Zeeb #define IMR_BCNDMAINT5 BIT(25) 1222774f206SBjoern A. Zeeb #define IMR_BCNDMAINT4 BIT(24) 1232774f206SBjoern A. Zeeb #define IMR_BCNDMAINT3 BIT(23) 1242774f206SBjoern A. Zeeb #define IMR_BCNDMAINT2 BIT(22) 1252774f206SBjoern A. Zeeb #define IMR_BCNDMAINT1 BIT(21) 1262774f206SBjoern A. Zeeb #define IMR_BCNDOK7 BIT(20) 1272774f206SBjoern A. Zeeb #define IMR_BCNDOK6 BIT(19) 1282774f206SBjoern A. Zeeb #define IMR_BCNDOK5 BIT(18) 1292774f206SBjoern A. Zeeb #define IMR_BCNDOK4 BIT(17) 1302774f206SBjoern A. Zeeb #define IMR_BCNDOK3 BIT(16) 1312774f206SBjoern A. Zeeb #define IMR_BCNDOK2 BIT(15) 1322774f206SBjoern A. Zeeb #define IMR_BCNDOK1 BIT(14) 1332774f206SBjoern A. Zeeb #define IMR_ATIMEND_E BIT(13) 1342774f206SBjoern A. Zeeb #define IMR_ATIMEND BIT(12) 1352774f206SBjoern A. Zeeb #define IMR_TXERR BIT(11) 1362774f206SBjoern A. Zeeb #define IMR_RXERR BIT(10) 1372774f206SBjoern A. Zeeb #define IMR_TXFOVW BIT(9) 1382774f206SBjoern A. Zeeb #define IMR_RXFOVW BIT(8) 1392774f206SBjoern A. Zeeb #define IMR_CPU_MGQ_TXDONE BIT(5) 1402774f206SBjoern A. Zeeb #define IMR_PS_TIMER_C BIT(4) 1412774f206SBjoern A. Zeeb #define IMR_PS_TIMER_B BIT(3) 1422774f206SBjoern A. Zeeb #define IMR_PS_TIMER_A BIT(2) 1432774f206SBjoern A. Zeeb #define IMR_CPUMGQ_TX_TIMER BIT(1) 1442774f206SBjoern A. Zeeb /* IMR 3 */ 1452774f206SBjoern A. Zeeb #define IMR_H2CDOK BIT(16) 1462774f206SBjoern A. Zeeb 1472774f206SBjoern A. Zeeb enum rtw_pci_flags { 1482774f206SBjoern A. Zeeb RTW_PCI_FLAG_NAPI_RUNNING, 1492774f206SBjoern A. Zeeb 1502774f206SBjoern A. Zeeb NUM_OF_RTW_PCI_FLAGS, 1512774f206SBjoern A. Zeeb }; 1522774f206SBjoern A. Zeeb 1532774f206SBjoern A. Zeeb /* one element is reserved to know if the ring is closed */ 1542774f206SBjoern A. Zeeb static inline int avail_desc(u32 wp, u32 rp, u32 len) 1552774f206SBjoern A. Zeeb { 1562774f206SBjoern A. Zeeb if (rp > wp) 1572774f206SBjoern A. Zeeb return rp - wp - 1; 1582774f206SBjoern A. Zeeb else 1592774f206SBjoern A. Zeeb return len - wp + rp - 1; 1602774f206SBjoern A. Zeeb } 1612774f206SBjoern A. Zeeb 1622774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_OWN_OFFSET 15 1632774f206SBjoern A. Zeeb #define RTK_PCI_TXBD_BCN_WORK 0x383 1642774f206SBjoern A. Zeeb 1652774f206SBjoern A. Zeeb struct rtw_pci_tx_buffer_desc { 1662774f206SBjoern A. Zeeb __le16 buf_size; 1672774f206SBjoern A. Zeeb __le16 psb_len; 1682774f206SBjoern A. Zeeb __le32 dma; 1692774f206SBjoern A. Zeeb }; 1702774f206SBjoern A. Zeeb 1712774f206SBjoern A. Zeeb struct rtw_pci_tx_data { 1722774f206SBjoern A. Zeeb dma_addr_t dma; 1732774f206SBjoern A. Zeeb u8 sn; 1742774f206SBjoern A. Zeeb }; 1752774f206SBjoern A. Zeeb 1762774f206SBjoern A. Zeeb struct rtw_pci_ring { 1772774f206SBjoern A. Zeeb u8 *head; 1782774f206SBjoern A. Zeeb dma_addr_t dma; 1792774f206SBjoern A. Zeeb 1802774f206SBjoern A. Zeeb u8 desc_size; 1812774f206SBjoern A. Zeeb 1822774f206SBjoern A. Zeeb u32 len; 1832774f206SBjoern A. Zeeb u32 wp; 1842774f206SBjoern A. Zeeb u32 rp; 1852774f206SBjoern A. Zeeb }; 1862774f206SBjoern A. Zeeb 1872774f206SBjoern A. Zeeb struct rtw_pci_tx_ring { 1882774f206SBjoern A. Zeeb struct rtw_pci_ring r; 1892774f206SBjoern A. Zeeb struct sk_buff_head queue; 1902774f206SBjoern A. Zeeb bool queue_stopped; 1912774f206SBjoern A. Zeeb }; 1922774f206SBjoern A. Zeeb 1932774f206SBjoern A. Zeeb struct rtw_pci_rx_buffer_desc { 1942774f206SBjoern A. Zeeb __le16 buf_size; 1952774f206SBjoern A. Zeeb __le16 total_pkt_size; 1962774f206SBjoern A. Zeeb __le32 dma; 1972774f206SBjoern A. Zeeb }; 1982774f206SBjoern A. Zeeb 1992774f206SBjoern A. Zeeb struct rtw_pci_rx_ring { 2002774f206SBjoern A. Zeeb struct rtw_pci_ring r; 2012774f206SBjoern A. Zeeb struct sk_buff *buf[RTK_MAX_RX_DESC_NUM]; 2022774f206SBjoern A. Zeeb }; 2032774f206SBjoern A. Zeeb 2042774f206SBjoern A. Zeeb #define RX_TAG_MAX 8192 2052774f206SBjoern A. Zeeb 2062774f206SBjoern A. Zeeb struct rtw_pci { 2072774f206SBjoern A. Zeeb struct pci_dev *pdev; 2082774f206SBjoern A. Zeeb 2092774f206SBjoern A. Zeeb /* Used for PCI interrupt. */ 2102774f206SBjoern A. Zeeb spinlock_t hwirq_lock; 2112774f206SBjoern A. Zeeb /* Used for PCI TX ring/queueing, and enable INT. */ 2122774f206SBjoern A. Zeeb spinlock_t irq_lock; 2132774f206SBjoern A. Zeeb u32 irq_mask[4]; 2142774f206SBjoern A. Zeeb bool irq_enabled; 2152774f206SBjoern A. Zeeb bool running; 2162774f206SBjoern A. Zeeb 2172774f206SBjoern A. Zeeb /* napi structure */ 218*11c53278SBjoern A. Zeeb struct net_device *netdev; 2192774f206SBjoern A. Zeeb struct napi_struct napi; 2202774f206SBjoern A. Zeeb 2212774f206SBjoern A. Zeeb u16 rx_tag; 2222774f206SBjoern A. Zeeb DECLARE_BITMAP(tx_queued, RTK_MAX_TX_QUEUE_NUM); 2232774f206SBjoern A. Zeeb struct rtw_pci_tx_ring tx_rings[RTK_MAX_TX_QUEUE_NUM]; 2242774f206SBjoern A. Zeeb struct rtw_pci_rx_ring rx_rings[RTK_MAX_RX_QUEUE_NUM]; 2252774f206SBjoern A. Zeeb u16 link_ctrl; 2262774f206SBjoern A. Zeeb atomic_t link_usage; 2272774f206SBjoern A. Zeeb bool rx_no_aspm; 2282774f206SBjoern A. Zeeb DECLARE_BITMAP(flags, NUM_OF_RTW_PCI_FLAGS); 2292774f206SBjoern A. Zeeb 2302774f206SBjoern A. Zeeb void __iomem *mmap; 2312774f206SBjoern A. Zeeb }; 2322774f206SBjoern A. Zeeb 2332774f206SBjoern A. Zeeb extern const struct dev_pm_ops rtw_pm_ops; 2342774f206SBjoern A. Zeeb 2352774f206SBjoern A. Zeeb int rtw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id); 2362774f206SBjoern A. Zeeb void rtw_pci_remove(struct pci_dev *pdev); 2372774f206SBjoern A. Zeeb void rtw_pci_shutdown(struct pci_dev *pdev); 2382774f206SBjoern A. Zeeb 2392774f206SBjoern A. Zeeb static inline u32 max_num_of_tx_queue(u8 queue) 2402774f206SBjoern A. Zeeb { 2412774f206SBjoern A. Zeeb u32 max_num; 2422774f206SBjoern A. Zeeb 2432774f206SBjoern A. Zeeb switch (queue) { 2442774f206SBjoern A. Zeeb case RTW_TX_QUEUE_BE: 2452774f206SBjoern A. Zeeb max_num = RTK_BEQ_TX_DESC_NUM; 2462774f206SBjoern A. Zeeb break; 2472774f206SBjoern A. Zeeb case RTW_TX_QUEUE_BCN: 2482774f206SBjoern A. Zeeb max_num = 1; 2492774f206SBjoern A. Zeeb break; 2502774f206SBjoern A. Zeeb default: 2512774f206SBjoern A. Zeeb max_num = RTK_DEFAULT_TX_DESC_NUM; 2522774f206SBjoern A. Zeeb break; 2532774f206SBjoern A. Zeeb } 2542774f206SBjoern A. Zeeb 2552774f206SBjoern A. Zeeb return max_num; 2562774f206SBjoern A. Zeeb } 2572774f206SBjoern A. Zeeb 2582774f206SBjoern A. Zeeb static inline struct 2592774f206SBjoern A. Zeeb rtw_pci_tx_data *rtw_pci_get_tx_data(struct sk_buff *skb) 2602774f206SBjoern A. Zeeb { 2612774f206SBjoern A. Zeeb struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2622774f206SBjoern A. Zeeb 2632774f206SBjoern A. Zeeb BUILD_BUG_ON(sizeof(struct rtw_pci_tx_data) > 2642774f206SBjoern A. Zeeb sizeof(info->status.status_driver_data)); 2652774f206SBjoern A. Zeeb 2662774f206SBjoern A. Zeeb return (struct rtw_pci_tx_data *)info->status.status_driver_data; 2672774f206SBjoern A. Zeeb } 2682774f206SBjoern A. Zeeb 2692774f206SBjoern A. Zeeb static inline 2702774f206SBjoern A. Zeeb struct rtw_pci_tx_buffer_desc *get_tx_buffer_desc(struct rtw_pci_tx_ring *ring, 2712774f206SBjoern A. Zeeb u32 size) 2722774f206SBjoern A. Zeeb { 2732774f206SBjoern A. Zeeb u8 *buf_desc; 2742774f206SBjoern A. Zeeb 2752774f206SBjoern A. Zeeb buf_desc = ring->r.head + ring->r.wp * size; 2762774f206SBjoern A. Zeeb return (struct rtw_pci_tx_buffer_desc *)buf_desc; 2772774f206SBjoern A. Zeeb } 2782774f206SBjoern A. Zeeb 2792774f206SBjoern A. Zeeb #endif 280