1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2874aeea5SJeff Kirsher /**************************************************************************** 3f7a6d2c4SBen Hutchings * Driver for Solarflare network controllers and boards 4874aeea5SJeff Kirsher * Copyright 2005-2006 Fen Systems Ltd. 5f7a6d2c4SBen Hutchings * Copyright 2006-2013 Solarflare Communications Inc. 6874aeea5SJeff Kirsher */ 7874aeea5SJeff Kirsher 8874aeea5SJeff Kirsher #include <linux/bitops.h> 9874aeea5SJeff Kirsher #include <linux/delay.h> 10874aeea5SJeff Kirsher #include <linux/interrupt.h> 11874aeea5SJeff Kirsher #include <linux/pci.h> 12874aeea5SJeff Kirsher #include <linux/module.h> 13874aeea5SJeff Kirsher #include <linux/seq_file.h> 141899c111SBen Hutchings #include <linux/cpu_rmap.h> 15874aeea5SJeff Kirsher #include "net_driver.h" 16874aeea5SJeff Kirsher #include "bitfield.h" 17874aeea5SJeff Kirsher #include "efx.h" 18874aeea5SJeff Kirsher #include "nic.h" 19137b7922SBen Hutchings #include "ef10_regs.h" 208b8a95a1SBen Hutchings #include "farch_regs.h" 21874aeea5SJeff Kirsher #include "io.h" 22874aeea5SJeff Kirsher #include "workarounds.h" 23d3142c19SEdward Cree #include "mcdi_pcol.h" 24874aeea5SJeff Kirsher 25874aeea5SJeff Kirsher /************************************************************************** 26874aeea5SJeff Kirsher * 27874aeea5SJeff Kirsher * Generic buffer handling 28f7251a9cSBen Hutchings * These buffers are used for interrupt status, MAC stats, etc. 29874aeea5SJeff Kirsher * 30874aeea5SJeff Kirsher **************************************************************************/ 31874aeea5SJeff Kirsher 32874aeea5SJeff Kirsher int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer, 330d19a540SBen Hutchings unsigned int len, gfp_t gfp_flags) 34874aeea5SJeff Kirsher { 35750afb08SLuis Chamberlain buffer->addr = dma_alloc_coherent(&efx->pci_dev->dev, len, 36ede23fa8SJoe Perches &buffer->dma_addr, gfp_flags); 37874aeea5SJeff Kirsher if (!buffer->addr) 38874aeea5SJeff Kirsher return -ENOMEM; 39874aeea5SJeff Kirsher buffer->len = len; 40874aeea5SJeff Kirsher return 0; 41874aeea5SJeff Kirsher } 42874aeea5SJeff Kirsher 43874aeea5SJeff Kirsher void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer) 44874aeea5SJeff Kirsher { 45874aeea5SJeff Kirsher if (buffer->addr) { 460e33d870SBen Hutchings dma_free_coherent(&efx->pci_dev->dev, buffer->len, 47874aeea5SJeff Kirsher buffer->addr, buffer->dma_addr); 48874aeea5SJeff Kirsher buffer->addr = NULL; 49874aeea5SJeff Kirsher } 50874aeea5SJeff Kirsher } 51874aeea5SJeff Kirsher 52874aeea5SJeff Kirsher /* Check whether an event is present in the eventq at the current 53874aeea5SJeff Kirsher * read pointer. Only useful for self-test. 54874aeea5SJeff Kirsher */ 55874aeea5SJeff Kirsher bool efx_nic_event_present(struct efx_channel *channel) 56874aeea5SJeff Kirsher { 57874aeea5SJeff Kirsher return efx_event_present(efx_event(channel, channel->eventq_read_ptr)); 58874aeea5SJeff Kirsher } 59874aeea5SJeff Kirsher 60eee6f6a9SBen Hutchings void efx_nic_event_test_start(struct efx_channel *channel) 61874aeea5SJeff Kirsher { 62dd40781eSBen Hutchings channel->event_test_cpu = -1; 63eee6f6a9SBen Hutchings smp_wmb(); 6486094f7fSBen Hutchings channel->efx->type->ev_test_generate(channel); 65874aeea5SJeff Kirsher } 66874aeea5SJeff Kirsher 67942e298eSJon Cooper int efx_nic_irq_test_start(struct efx_nic *efx) 68874aeea5SJeff Kirsher { 69eee6f6a9SBen Hutchings efx->last_irq_cpu = -1; 70eee6f6a9SBen Hutchings smp_wmb(); 71942e298eSJon Cooper return efx->type->irq_test_generate(efx); 72874aeea5SJeff Kirsher } 73874aeea5SJeff Kirsher 74874aeea5SJeff Kirsher /* Hook interrupt handler(s) 75874aeea5SJeff Kirsher * Try MSI and then legacy interrupts. 76874aeea5SJeff Kirsher */ 77874aeea5SJeff Kirsher int efx_nic_init_interrupt(struct efx_nic *efx) 78874aeea5SJeff Kirsher { 79874aeea5SJeff Kirsher struct efx_channel *channel; 801899c111SBen Hutchings unsigned int n_irqs; 81874aeea5SJeff Kirsher int rc; 82874aeea5SJeff Kirsher 83874aeea5SJeff Kirsher if (!EFX_INT_MODE_USE_MSI(efx)) { 8486094f7fSBen Hutchings rc = request_irq(efx->legacy_irq, 8586094f7fSBen Hutchings efx->type->irq_handle_legacy, IRQF_SHARED, 86874aeea5SJeff Kirsher efx->name, efx); 87874aeea5SJeff Kirsher if (rc) { 88874aeea5SJeff Kirsher netif_err(efx, drv, efx->net_dev, 89874aeea5SJeff Kirsher "failed to hook legacy IRQ %d\n", 90874aeea5SJeff Kirsher efx->pci_dev->irq); 91874aeea5SJeff Kirsher goto fail1; 92874aeea5SJeff Kirsher } 93874aeea5SJeff Kirsher return 0; 94874aeea5SJeff Kirsher } 95874aeea5SJeff Kirsher 961899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL 971899c111SBen Hutchings if (efx->interrupt_mode == EFX_INT_MODE_MSIX) { 981899c111SBen Hutchings efx->net_dev->rx_cpu_rmap = 991899c111SBen Hutchings alloc_irq_cpu_rmap(efx->n_rx_channels); 1001899c111SBen Hutchings if (!efx->net_dev->rx_cpu_rmap) { 1011899c111SBen Hutchings rc = -ENOMEM; 1021899c111SBen Hutchings goto fail1; 1031899c111SBen Hutchings } 1041899c111SBen Hutchings } 1051899c111SBen Hutchings #endif 1061899c111SBen Hutchings 107874aeea5SJeff Kirsher /* Hook MSI or MSI-X interrupt */ 1081899c111SBen Hutchings n_irqs = 0; 109874aeea5SJeff Kirsher efx_for_each_channel(channel, efx) { 11086094f7fSBen Hutchings rc = request_irq(channel->irq, efx->type->irq_handle_msi, 111874aeea5SJeff Kirsher IRQF_PROBE_SHARED, /* Not shared */ 112d8291187SBen Hutchings efx->msi_context[channel->channel].name, 113d8291187SBen Hutchings &efx->msi_context[channel->channel]); 114874aeea5SJeff Kirsher if (rc) { 115874aeea5SJeff Kirsher netif_err(efx, drv, efx->net_dev, 116874aeea5SJeff Kirsher "failed to hook IRQ %d\n", channel->irq); 117874aeea5SJeff Kirsher goto fail2; 118874aeea5SJeff Kirsher } 1191899c111SBen Hutchings ++n_irqs; 1201899c111SBen Hutchings 1211899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL 1221899c111SBen Hutchings if (efx->interrupt_mode == EFX_INT_MODE_MSIX && 1231899c111SBen Hutchings channel->channel < efx->n_rx_channels) { 1241899c111SBen Hutchings rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap, 1251899c111SBen Hutchings channel->irq); 1261899c111SBen Hutchings if (rc) 1271899c111SBen Hutchings goto fail2; 1281899c111SBen Hutchings } 1291899c111SBen Hutchings #endif 130874aeea5SJeff Kirsher } 131874aeea5SJeff Kirsher 132*e6a43910SEdward Cree efx->irqs_hooked = true; 133874aeea5SJeff Kirsher return 0; 134874aeea5SJeff Kirsher 135874aeea5SJeff Kirsher fail2: 1361899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL 1371899c111SBen Hutchings free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap); 1381899c111SBen Hutchings efx->net_dev->rx_cpu_rmap = NULL; 1391899c111SBen Hutchings #endif 1401899c111SBen Hutchings efx_for_each_channel(channel, efx) { 1411899c111SBen Hutchings if (n_irqs-- == 0) 1421899c111SBen Hutchings break; 143d8291187SBen Hutchings free_irq(channel->irq, &efx->msi_context[channel->channel]); 1441899c111SBen Hutchings } 145874aeea5SJeff Kirsher fail1: 146874aeea5SJeff Kirsher return rc; 147874aeea5SJeff Kirsher } 148874aeea5SJeff Kirsher 149874aeea5SJeff Kirsher void efx_nic_fini_interrupt(struct efx_nic *efx) 150874aeea5SJeff Kirsher { 151874aeea5SJeff Kirsher struct efx_channel *channel; 152874aeea5SJeff Kirsher 1531899c111SBen Hutchings #ifdef CONFIG_RFS_ACCEL 1541899c111SBen Hutchings free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap); 1551899c111SBen Hutchings efx->net_dev->rx_cpu_rmap = NULL; 1561899c111SBen Hutchings #endif 1571899c111SBen Hutchings 158*e6a43910SEdward Cree if (!efx->irqs_hooked) 159*e6a43910SEdward Cree return; 1601c363900SNikolay Aleksandrov if (EFX_INT_MODE_USE_MSI(efx)) { 161874aeea5SJeff Kirsher /* Disable MSI/MSI-X interrupts */ 1621899c111SBen Hutchings efx_for_each_channel(channel, efx) 1631c363900SNikolay Aleksandrov free_irq(channel->irq, 1641c363900SNikolay Aleksandrov &efx->msi_context[channel->channel]); 1651c363900SNikolay Aleksandrov } else { 166874aeea5SJeff Kirsher /* Disable legacy interrupt */ 167874aeea5SJeff Kirsher free_irq(efx->legacy_irq, efx); 168874aeea5SJeff Kirsher } 169*e6a43910SEdward Cree efx->irqs_hooked = false; 1701c363900SNikolay Aleksandrov } 171874aeea5SJeff Kirsher 172874aeea5SJeff Kirsher /* Register dump */ 173874aeea5SJeff Kirsher 174137b7922SBen Hutchings #define REGISTER_REVISION_FA 1 175137b7922SBen Hutchings #define REGISTER_REVISION_FB 2 176137b7922SBen Hutchings #define REGISTER_REVISION_FC 3 177137b7922SBen Hutchings #define REGISTER_REVISION_FZ 3 /* last Falcon arch revision */ 178137b7922SBen Hutchings #define REGISTER_REVISION_ED 4 179137b7922SBen Hutchings #define REGISTER_REVISION_EZ 4 /* latest EF10 revision */ 180874aeea5SJeff Kirsher 181874aeea5SJeff Kirsher struct efx_nic_reg { 182874aeea5SJeff Kirsher u32 offset:24; 183137b7922SBen Hutchings u32 min_revision:3, max_revision:3; 184874aeea5SJeff Kirsher }; 185874aeea5SJeff Kirsher 186137b7922SBen Hutchings #define REGISTER(name, arch, min_rev, max_rev) { \ 187137b7922SBen Hutchings arch ## R_ ## min_rev ## max_rev ## _ ## name, \ 188137b7922SBen Hutchings REGISTER_REVISION_ ## arch ## min_rev, \ 189137b7922SBen Hutchings REGISTER_REVISION_ ## arch ## max_rev \ 190874aeea5SJeff Kirsher } 191137b7922SBen Hutchings #define REGISTER_AA(name) REGISTER(name, F, A, A) 192137b7922SBen Hutchings #define REGISTER_AB(name) REGISTER(name, F, A, B) 193137b7922SBen Hutchings #define REGISTER_AZ(name) REGISTER(name, F, A, Z) 194137b7922SBen Hutchings #define REGISTER_BB(name) REGISTER(name, F, B, B) 195137b7922SBen Hutchings #define REGISTER_BZ(name) REGISTER(name, F, B, Z) 196137b7922SBen Hutchings #define REGISTER_CZ(name) REGISTER(name, F, C, Z) 197137b7922SBen Hutchings #define REGISTER_DZ(name) REGISTER(name, E, D, Z) 198874aeea5SJeff Kirsher 199874aeea5SJeff Kirsher static const struct efx_nic_reg efx_nic_regs[] = { 200874aeea5SJeff Kirsher REGISTER_AZ(ADR_REGION), 201874aeea5SJeff Kirsher REGISTER_AZ(INT_EN_KER), 202874aeea5SJeff Kirsher REGISTER_BZ(INT_EN_CHAR), 203874aeea5SJeff Kirsher REGISTER_AZ(INT_ADR_KER), 204874aeea5SJeff Kirsher REGISTER_BZ(INT_ADR_CHAR), 205874aeea5SJeff Kirsher /* INT_ACK_KER is WO */ 206874aeea5SJeff Kirsher /* INT_ISR0 is RC */ 207874aeea5SJeff Kirsher REGISTER_AZ(HW_INIT), 208874aeea5SJeff Kirsher REGISTER_CZ(USR_EV_CFG), 209874aeea5SJeff Kirsher REGISTER_AB(EE_SPI_HCMD), 210874aeea5SJeff Kirsher REGISTER_AB(EE_SPI_HADR), 211874aeea5SJeff Kirsher REGISTER_AB(EE_SPI_HDATA), 212874aeea5SJeff Kirsher REGISTER_AB(EE_BASE_PAGE), 213874aeea5SJeff Kirsher REGISTER_AB(EE_VPD_CFG0), 214874aeea5SJeff Kirsher /* EE_VPD_SW_CNTL and EE_VPD_SW_DATA are not used */ 215874aeea5SJeff Kirsher /* PMBX_DBG_IADDR and PBMX_DBG_IDATA are indirect */ 216874aeea5SJeff Kirsher /* PCIE_CORE_INDIRECT is indirect */ 217874aeea5SJeff Kirsher REGISTER_AB(NIC_STAT), 218874aeea5SJeff Kirsher REGISTER_AB(GPIO_CTL), 219874aeea5SJeff Kirsher REGISTER_AB(GLB_CTL), 220874aeea5SJeff Kirsher /* FATAL_INTR_KER and FATAL_INTR_CHAR are partly RC */ 221874aeea5SJeff Kirsher REGISTER_BZ(DP_CTRL), 222874aeea5SJeff Kirsher REGISTER_AZ(MEM_STAT), 223874aeea5SJeff Kirsher REGISTER_AZ(CS_DEBUG), 224874aeea5SJeff Kirsher REGISTER_AZ(ALTERA_BUILD), 225874aeea5SJeff Kirsher REGISTER_AZ(CSR_SPARE), 226874aeea5SJeff Kirsher REGISTER_AB(PCIE_SD_CTL0123), 227874aeea5SJeff Kirsher REGISTER_AB(PCIE_SD_CTL45), 228874aeea5SJeff Kirsher REGISTER_AB(PCIE_PCS_CTL_STAT), 229874aeea5SJeff Kirsher /* DEBUG_DATA_OUT is not used */ 230874aeea5SJeff Kirsher /* DRV_EV is WO */ 231874aeea5SJeff Kirsher REGISTER_AZ(EVQ_CTL), 232874aeea5SJeff Kirsher REGISTER_AZ(EVQ_CNT1), 233874aeea5SJeff Kirsher REGISTER_AZ(EVQ_CNT2), 234874aeea5SJeff Kirsher REGISTER_AZ(BUF_TBL_CFG), 235874aeea5SJeff Kirsher REGISTER_AZ(SRM_RX_DC_CFG), 236874aeea5SJeff Kirsher REGISTER_AZ(SRM_TX_DC_CFG), 237874aeea5SJeff Kirsher REGISTER_AZ(SRM_CFG), 238874aeea5SJeff Kirsher /* BUF_TBL_UPD is WO */ 239874aeea5SJeff Kirsher REGISTER_AZ(SRM_UPD_EVQ), 240874aeea5SJeff Kirsher REGISTER_AZ(SRAM_PARITY), 241874aeea5SJeff Kirsher REGISTER_AZ(RX_CFG), 242874aeea5SJeff Kirsher REGISTER_BZ(RX_FILTER_CTL), 243874aeea5SJeff Kirsher /* RX_FLUSH_DESCQ is WO */ 244874aeea5SJeff Kirsher REGISTER_AZ(RX_DC_CFG), 245874aeea5SJeff Kirsher REGISTER_AZ(RX_DC_PF_WM), 246874aeea5SJeff Kirsher REGISTER_BZ(RX_RSS_TKEY), 247874aeea5SJeff Kirsher /* RX_NODESC_DROP is RC */ 248874aeea5SJeff Kirsher REGISTER_AA(RX_SELF_RST), 249874aeea5SJeff Kirsher /* RX_DEBUG, RX_PUSH_DROP are not used */ 250874aeea5SJeff Kirsher REGISTER_CZ(RX_RSS_IPV6_REG1), 251874aeea5SJeff Kirsher REGISTER_CZ(RX_RSS_IPV6_REG2), 252874aeea5SJeff Kirsher REGISTER_CZ(RX_RSS_IPV6_REG3), 253874aeea5SJeff Kirsher /* TX_FLUSH_DESCQ is WO */ 254874aeea5SJeff Kirsher REGISTER_AZ(TX_DC_CFG), 255874aeea5SJeff Kirsher REGISTER_AA(TX_CHKSM_CFG), 256874aeea5SJeff Kirsher REGISTER_AZ(TX_CFG), 257874aeea5SJeff Kirsher /* TX_PUSH_DROP is not used */ 258874aeea5SJeff Kirsher REGISTER_AZ(TX_RESERVED), 259874aeea5SJeff Kirsher REGISTER_BZ(TX_PACE), 260874aeea5SJeff Kirsher /* TX_PACE_DROP_QID is RC */ 261874aeea5SJeff Kirsher REGISTER_BB(TX_VLAN), 262874aeea5SJeff Kirsher REGISTER_BZ(TX_IPFIL_PORTEN), 263874aeea5SJeff Kirsher REGISTER_AB(MD_TXD), 264874aeea5SJeff Kirsher REGISTER_AB(MD_RXD), 265874aeea5SJeff Kirsher REGISTER_AB(MD_CS), 266874aeea5SJeff Kirsher REGISTER_AB(MD_PHY_ADR), 267874aeea5SJeff Kirsher REGISTER_AB(MD_ID), 268874aeea5SJeff Kirsher /* MD_STAT is RC */ 269874aeea5SJeff Kirsher REGISTER_AB(MAC_STAT_DMA), 270874aeea5SJeff Kirsher REGISTER_AB(MAC_CTRL), 271874aeea5SJeff Kirsher REGISTER_BB(GEN_MODE), 272874aeea5SJeff Kirsher REGISTER_AB(MAC_MC_HASH_REG0), 273874aeea5SJeff Kirsher REGISTER_AB(MAC_MC_HASH_REG1), 274874aeea5SJeff Kirsher REGISTER_AB(GM_CFG1), 275874aeea5SJeff Kirsher REGISTER_AB(GM_CFG2), 276874aeea5SJeff Kirsher /* GM_IPG and GM_HD are not used */ 277874aeea5SJeff Kirsher REGISTER_AB(GM_MAX_FLEN), 278874aeea5SJeff Kirsher /* GM_TEST is not used */ 279874aeea5SJeff Kirsher REGISTER_AB(GM_ADR1), 280874aeea5SJeff Kirsher REGISTER_AB(GM_ADR2), 281874aeea5SJeff Kirsher REGISTER_AB(GMF_CFG0), 282874aeea5SJeff Kirsher REGISTER_AB(GMF_CFG1), 283874aeea5SJeff Kirsher REGISTER_AB(GMF_CFG2), 284874aeea5SJeff Kirsher REGISTER_AB(GMF_CFG3), 285874aeea5SJeff Kirsher REGISTER_AB(GMF_CFG4), 286874aeea5SJeff Kirsher REGISTER_AB(GMF_CFG5), 287874aeea5SJeff Kirsher REGISTER_BB(TX_SRC_MAC_CTL), 288874aeea5SJeff Kirsher REGISTER_AB(XM_ADR_LO), 289874aeea5SJeff Kirsher REGISTER_AB(XM_ADR_HI), 290874aeea5SJeff Kirsher REGISTER_AB(XM_GLB_CFG), 291874aeea5SJeff Kirsher REGISTER_AB(XM_TX_CFG), 292874aeea5SJeff Kirsher REGISTER_AB(XM_RX_CFG), 293874aeea5SJeff Kirsher REGISTER_AB(XM_MGT_INT_MASK), 294874aeea5SJeff Kirsher REGISTER_AB(XM_FC), 295874aeea5SJeff Kirsher REGISTER_AB(XM_PAUSE_TIME), 296874aeea5SJeff Kirsher REGISTER_AB(XM_TX_PARAM), 297874aeea5SJeff Kirsher REGISTER_AB(XM_RX_PARAM), 298874aeea5SJeff Kirsher /* XM_MGT_INT_MSK (note no 'A') is RC */ 299874aeea5SJeff Kirsher REGISTER_AB(XX_PWR_RST), 300874aeea5SJeff Kirsher REGISTER_AB(XX_SD_CTL), 301874aeea5SJeff Kirsher REGISTER_AB(XX_TXDRV_CTL), 302874aeea5SJeff Kirsher /* XX_PRBS_CTL, XX_PRBS_CHK and XX_PRBS_ERR are not used */ 303874aeea5SJeff Kirsher /* XX_CORE_STAT is partly RC */ 304137b7922SBen Hutchings REGISTER_DZ(BIU_HW_REV_ID), 305137b7922SBen Hutchings REGISTER_DZ(MC_DB_LWRD), 306137b7922SBen Hutchings REGISTER_DZ(MC_DB_HWRD), 307874aeea5SJeff Kirsher }; 308874aeea5SJeff Kirsher 309874aeea5SJeff Kirsher struct efx_nic_reg_table { 310874aeea5SJeff Kirsher u32 offset:24; 311137b7922SBen Hutchings u32 min_revision:3, max_revision:3; 312874aeea5SJeff Kirsher u32 step:6, rows:21; 313874aeea5SJeff Kirsher }; 314874aeea5SJeff Kirsher 315137b7922SBen Hutchings #define REGISTER_TABLE_DIMENSIONS(_, offset, arch, min_rev, max_rev, step, rows) { \ 316874aeea5SJeff Kirsher offset, \ 317137b7922SBen Hutchings REGISTER_REVISION_ ## arch ## min_rev, \ 318137b7922SBen Hutchings REGISTER_REVISION_ ## arch ## max_rev, \ 319874aeea5SJeff Kirsher step, rows \ 320874aeea5SJeff Kirsher } 321137b7922SBen Hutchings #define REGISTER_TABLE(name, arch, min_rev, max_rev) \ 322874aeea5SJeff Kirsher REGISTER_TABLE_DIMENSIONS( \ 323137b7922SBen Hutchings name, arch ## R_ ## min_rev ## max_rev ## _ ## name, \ 324137b7922SBen Hutchings arch, min_rev, max_rev, \ 325137b7922SBen Hutchings arch ## R_ ## min_rev ## max_rev ## _ ## name ## _STEP, \ 326137b7922SBen Hutchings arch ## R_ ## min_rev ## max_rev ## _ ## name ## _ROWS) 327137b7922SBen Hutchings #define REGISTER_TABLE_AA(name) REGISTER_TABLE(name, F, A, A) 328137b7922SBen Hutchings #define REGISTER_TABLE_AZ(name) REGISTER_TABLE(name, F, A, Z) 329137b7922SBen Hutchings #define REGISTER_TABLE_BB(name) REGISTER_TABLE(name, F, B, B) 330137b7922SBen Hutchings #define REGISTER_TABLE_BZ(name) REGISTER_TABLE(name, F, B, Z) 331874aeea5SJeff Kirsher #define REGISTER_TABLE_BB_CZ(name) \ 332137b7922SBen Hutchings REGISTER_TABLE_DIMENSIONS(name, FR_BZ_ ## name, F, B, B, \ 333874aeea5SJeff Kirsher FR_BZ_ ## name ## _STEP, \ 334874aeea5SJeff Kirsher FR_BB_ ## name ## _ROWS), \ 335137b7922SBen Hutchings REGISTER_TABLE_DIMENSIONS(name, FR_BZ_ ## name, F, C, Z, \ 336874aeea5SJeff Kirsher FR_BZ_ ## name ## _STEP, \ 337874aeea5SJeff Kirsher FR_CZ_ ## name ## _ROWS) 338137b7922SBen Hutchings #define REGISTER_TABLE_CZ(name) REGISTER_TABLE(name, F, C, Z) 339137b7922SBen Hutchings #define REGISTER_TABLE_DZ(name) REGISTER_TABLE(name, E, D, Z) 340874aeea5SJeff Kirsher 341874aeea5SJeff Kirsher static const struct efx_nic_reg_table efx_nic_reg_tables[] = { 342874aeea5SJeff Kirsher /* DRIVER is not used */ 343874aeea5SJeff Kirsher /* EVQ_RPTR, TIMER_COMMAND, USR_EV and {RX,TX}_DESC_UPD are WO */ 344874aeea5SJeff Kirsher REGISTER_TABLE_BB(TX_IPFIL_TBL), 345874aeea5SJeff Kirsher REGISTER_TABLE_BB(TX_SRC_MAC_TBL), 346874aeea5SJeff Kirsher REGISTER_TABLE_AA(RX_DESC_PTR_TBL_KER), 347874aeea5SJeff Kirsher REGISTER_TABLE_BB_CZ(RX_DESC_PTR_TBL), 348874aeea5SJeff Kirsher REGISTER_TABLE_AA(TX_DESC_PTR_TBL_KER), 349874aeea5SJeff Kirsher REGISTER_TABLE_BB_CZ(TX_DESC_PTR_TBL), 350874aeea5SJeff Kirsher REGISTER_TABLE_AA(EVQ_PTR_TBL_KER), 351874aeea5SJeff Kirsher REGISTER_TABLE_BB_CZ(EVQ_PTR_TBL), 352874aeea5SJeff Kirsher /* We can't reasonably read all of the buffer table (up to 8MB!). 353874aeea5SJeff Kirsher * However this driver will only use a few entries. Reading 354874aeea5SJeff Kirsher * 1K entries allows for some expansion of queue count and 355874aeea5SJeff Kirsher * size before we need to change the version. */ 356874aeea5SJeff Kirsher REGISTER_TABLE_DIMENSIONS(BUF_FULL_TBL_KER, FR_AA_BUF_FULL_TBL_KER, 357137b7922SBen Hutchings F, A, A, 8, 1024), 358874aeea5SJeff Kirsher REGISTER_TABLE_DIMENSIONS(BUF_FULL_TBL, FR_BZ_BUF_FULL_TBL, 359137b7922SBen Hutchings F, B, Z, 8, 1024), 360874aeea5SJeff Kirsher REGISTER_TABLE_CZ(RX_MAC_FILTER_TBL0), 361874aeea5SJeff Kirsher REGISTER_TABLE_BB_CZ(TIMER_TBL), 362874aeea5SJeff Kirsher REGISTER_TABLE_BB_CZ(TX_PACE_TBL), 363874aeea5SJeff Kirsher REGISTER_TABLE_BZ(RX_INDIRECTION_TBL), 364874aeea5SJeff Kirsher /* TX_FILTER_TBL0 is huge and not used by this driver */ 365874aeea5SJeff Kirsher REGISTER_TABLE_CZ(TX_MAC_FILTER_TBL0), 366874aeea5SJeff Kirsher REGISTER_TABLE_CZ(MC_TREG_SMEM), 367874aeea5SJeff Kirsher /* MSIX_PBA_TABLE is not mapped */ 368874aeea5SJeff Kirsher /* SRM_DBG is not mapped (and is redundant with BUF_FLL_TBL) */ 369874aeea5SJeff Kirsher REGISTER_TABLE_BZ(RX_FILTER_TBL0), 370137b7922SBen Hutchings REGISTER_TABLE_DZ(BIU_MC_SFT_STATUS), 371874aeea5SJeff Kirsher }; 372874aeea5SJeff Kirsher 373874aeea5SJeff Kirsher size_t efx_nic_get_regs_len(struct efx_nic *efx) 374874aeea5SJeff Kirsher { 375874aeea5SJeff Kirsher const struct efx_nic_reg *reg; 376874aeea5SJeff Kirsher const struct efx_nic_reg_table *table; 377874aeea5SJeff Kirsher size_t len = 0; 378874aeea5SJeff Kirsher 379874aeea5SJeff Kirsher for (reg = efx_nic_regs; 380874aeea5SJeff Kirsher reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs); 381874aeea5SJeff Kirsher reg++) 382874aeea5SJeff Kirsher if (efx->type->revision >= reg->min_revision && 383874aeea5SJeff Kirsher efx->type->revision <= reg->max_revision) 384874aeea5SJeff Kirsher len += sizeof(efx_oword_t); 385874aeea5SJeff Kirsher 386874aeea5SJeff Kirsher for (table = efx_nic_reg_tables; 387874aeea5SJeff Kirsher table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables); 388874aeea5SJeff Kirsher table++) 389874aeea5SJeff Kirsher if (efx->type->revision >= table->min_revision && 390874aeea5SJeff Kirsher efx->type->revision <= table->max_revision) 391874aeea5SJeff Kirsher len += table->rows * min_t(size_t, table->step, 16); 392874aeea5SJeff Kirsher 393874aeea5SJeff Kirsher return len; 394874aeea5SJeff Kirsher } 395874aeea5SJeff Kirsher 396874aeea5SJeff Kirsher void efx_nic_get_regs(struct efx_nic *efx, void *buf) 397874aeea5SJeff Kirsher { 398874aeea5SJeff Kirsher const struct efx_nic_reg *reg; 399874aeea5SJeff Kirsher const struct efx_nic_reg_table *table; 400874aeea5SJeff Kirsher 401874aeea5SJeff Kirsher for (reg = efx_nic_regs; 402874aeea5SJeff Kirsher reg < efx_nic_regs + ARRAY_SIZE(efx_nic_regs); 403874aeea5SJeff Kirsher reg++) { 404874aeea5SJeff Kirsher if (efx->type->revision >= reg->min_revision && 405874aeea5SJeff Kirsher efx->type->revision <= reg->max_revision) { 406874aeea5SJeff Kirsher efx_reado(efx, (efx_oword_t *)buf, reg->offset); 407874aeea5SJeff Kirsher buf += sizeof(efx_oword_t); 408874aeea5SJeff Kirsher } 409874aeea5SJeff Kirsher } 410874aeea5SJeff Kirsher 411874aeea5SJeff Kirsher for (table = efx_nic_reg_tables; 412874aeea5SJeff Kirsher table < efx_nic_reg_tables + ARRAY_SIZE(efx_nic_reg_tables); 413874aeea5SJeff Kirsher table++) { 414874aeea5SJeff Kirsher size_t size, i; 415874aeea5SJeff Kirsher 416874aeea5SJeff Kirsher if (!(efx->type->revision >= table->min_revision && 417874aeea5SJeff Kirsher efx->type->revision <= table->max_revision)) 418874aeea5SJeff Kirsher continue; 419874aeea5SJeff Kirsher 420874aeea5SJeff Kirsher size = min_t(size_t, table->step, 16); 421874aeea5SJeff Kirsher 422874aeea5SJeff Kirsher for (i = 0; i < table->rows; i++) { 423874aeea5SJeff Kirsher switch (table->step) { 424778cdaf6SBen Hutchings case 4: /* 32-bit SRAM */ 425778cdaf6SBen Hutchings efx_readd(efx, buf, table->offset + 4 * i); 426874aeea5SJeff Kirsher break; 427874aeea5SJeff Kirsher case 8: /* 64-bit SRAM */ 428874aeea5SJeff Kirsher efx_sram_readq(efx, 429874aeea5SJeff Kirsher efx->membase + table->offset, 430874aeea5SJeff Kirsher buf, i); 431874aeea5SJeff Kirsher break; 432778cdaf6SBen Hutchings case 16: /* 128-bit-readable register */ 433874aeea5SJeff Kirsher efx_reado_table(efx, buf, table->offset, i); 434874aeea5SJeff Kirsher break; 435874aeea5SJeff Kirsher case 32: /* 128-bit register, interleaved */ 436874aeea5SJeff Kirsher efx_reado_table(efx, buf, table->offset, 2 * i); 437874aeea5SJeff Kirsher break; 438874aeea5SJeff Kirsher default: 439874aeea5SJeff Kirsher WARN_ON(1); 440874aeea5SJeff Kirsher return; 441874aeea5SJeff Kirsher } 442874aeea5SJeff Kirsher buf += size; 443874aeea5SJeff Kirsher } 444874aeea5SJeff Kirsher } 445874aeea5SJeff Kirsher } 446cd0ecc9aSBen Hutchings 447cd0ecc9aSBen Hutchings /** 448cd0ecc9aSBen Hutchings * efx_nic_describe_stats - Describe supported statistics for ethtool 449cd0ecc9aSBen Hutchings * @desc: Array of &struct efx_hw_stat_desc describing the statistics 450cd0ecc9aSBen Hutchings * @count: Length of the @desc array 451cd0ecc9aSBen Hutchings * @mask: Bitmask of which elements of @desc are enabled 452cd0ecc9aSBen Hutchings * @names: Buffer to copy names to, or %NULL. The names are copied 453cd0ecc9aSBen Hutchings * starting at intervals of %ETH_GSTRING_LEN bytes. 454cd0ecc9aSBen Hutchings * 455cd0ecc9aSBen Hutchings * Returns the number of visible statistics, i.e. the number of set 456cd0ecc9aSBen Hutchings * bits in the first @count bits of @mask for which a name is defined. 457cd0ecc9aSBen Hutchings */ 458cd0ecc9aSBen Hutchings size_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count, 459cd0ecc9aSBen Hutchings const unsigned long *mask, u8 *names) 460cd0ecc9aSBen Hutchings { 461cd0ecc9aSBen Hutchings size_t visible = 0; 462cd0ecc9aSBen Hutchings size_t index; 463cd0ecc9aSBen Hutchings 464cd0ecc9aSBen Hutchings for_each_set_bit(index, mask, count) { 465cd0ecc9aSBen Hutchings if (desc[index].name) { 466cd0ecc9aSBen Hutchings if (names) { 467cd0ecc9aSBen Hutchings strlcpy(names, desc[index].name, 468cd0ecc9aSBen Hutchings ETH_GSTRING_LEN); 469cd0ecc9aSBen Hutchings names += ETH_GSTRING_LEN; 470cd0ecc9aSBen Hutchings } 471cd0ecc9aSBen Hutchings ++visible; 472cd0ecc9aSBen Hutchings } 473cd0ecc9aSBen Hutchings } 474cd0ecc9aSBen Hutchings 475cd0ecc9aSBen Hutchings return visible; 476cd0ecc9aSBen Hutchings } 477cd0ecc9aSBen Hutchings 478cd0ecc9aSBen Hutchings /** 479d3142c19SEdward Cree * efx_nic_copy_stats - Copy stats from the DMA buffer in to an 480d3142c19SEdward Cree * intermediate buffer. This is used to get a consistent 481d3142c19SEdward Cree * set of stats while the DMA buffer can be written at any time 482d3142c19SEdward Cree * by the NIC. 483d3142c19SEdward Cree * @efx: The associated NIC. 484d3142c19SEdward Cree * @dest: Destination buffer. Must be the same size as the DMA buffer. 485d3142c19SEdward Cree */ 486d3142c19SEdward Cree int efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest) 487d3142c19SEdward Cree { 488d3142c19SEdward Cree __le64 *dma_stats = efx->stats_buffer.addr; 489d3142c19SEdward Cree __le64 generation_start, generation_end; 490d3142c19SEdward Cree int rc = 0, retry; 491d3142c19SEdward Cree 492d3142c19SEdward Cree if (!dest) 493d3142c19SEdward Cree return 0; 494d3142c19SEdward Cree 495d3142c19SEdward Cree if (!dma_stats) 496d3142c19SEdward Cree goto return_zeroes; 497d3142c19SEdward Cree 498d3142c19SEdward Cree /* If we're unlucky enough to read statistics during the DMA, wait 499d3142c19SEdward Cree * up to 10ms for it to finish (typically takes <500us) 500d3142c19SEdward Cree */ 501d3142c19SEdward Cree for (retry = 0; retry < 100; ++retry) { 502d3142c19SEdward Cree generation_end = dma_stats[efx->num_mac_stats - 1]; 503d3142c19SEdward Cree if (generation_end == EFX_MC_STATS_GENERATION_INVALID) 504d3142c19SEdward Cree goto return_zeroes; 505d3142c19SEdward Cree rmb(); 506d3142c19SEdward Cree memcpy(dest, dma_stats, efx->num_mac_stats * sizeof(__le64)); 507d3142c19SEdward Cree rmb(); 508d3142c19SEdward Cree generation_start = dma_stats[MC_CMD_MAC_GENERATION_START]; 509d3142c19SEdward Cree if (generation_end == generation_start) 510d3142c19SEdward Cree return 0; /* return good data */ 511d3142c19SEdward Cree udelay(100); 512d3142c19SEdward Cree } 513d3142c19SEdward Cree 514d3142c19SEdward Cree rc = -EIO; 515d3142c19SEdward Cree 516d3142c19SEdward Cree return_zeroes: 517d3142c19SEdward Cree memset(dest, 0, efx->num_mac_stats * sizeof(u64)); 518d3142c19SEdward Cree return rc; 519d3142c19SEdward Cree } 520d3142c19SEdward Cree 521d3142c19SEdward Cree /** 522cd0ecc9aSBen Hutchings * efx_nic_update_stats - Convert statistics DMA buffer to array of u64 523cd0ecc9aSBen Hutchings * @desc: Array of &struct efx_hw_stat_desc describing the DMA buffer 524cd0ecc9aSBen Hutchings * layout. DMA widths of 0, 16, 32 and 64 are supported; where 525cd0ecc9aSBen Hutchings * the width is specified as 0 the corresponding element of 526cd0ecc9aSBen Hutchings * @stats is not updated. 527cd0ecc9aSBen Hutchings * @count: Length of the @desc array 528cd0ecc9aSBen Hutchings * @mask: Bitmask of which elements of @desc are enabled 529cd0ecc9aSBen Hutchings * @stats: Buffer to update with the converted statistics. The length 53087648cc9SEdward Cree * of this array must be at least @count. 531cd0ecc9aSBen Hutchings * @dma_buf: DMA buffer containing hardware statistics 532cd0ecc9aSBen Hutchings * @accumulate: If set, the converted values will be added rather than 533cd0ecc9aSBen Hutchings * directly stored to the corresponding elements of @stats 534cd0ecc9aSBen Hutchings */ 535cd0ecc9aSBen Hutchings void efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count, 536cd0ecc9aSBen Hutchings const unsigned long *mask, 537cd0ecc9aSBen Hutchings u64 *stats, const void *dma_buf, bool accumulate) 538cd0ecc9aSBen Hutchings { 539cd0ecc9aSBen Hutchings size_t index; 540cd0ecc9aSBen Hutchings 541cd0ecc9aSBen Hutchings for_each_set_bit(index, mask, count) { 542cd0ecc9aSBen Hutchings if (desc[index].dma_width) { 543cd0ecc9aSBen Hutchings const void *addr = dma_buf + desc[index].offset; 544cd0ecc9aSBen Hutchings u64 val; 545cd0ecc9aSBen Hutchings 546cd0ecc9aSBen Hutchings switch (desc[index].dma_width) { 547cd0ecc9aSBen Hutchings case 16: 548cd0ecc9aSBen Hutchings val = le16_to_cpup((__le16 *)addr); 549cd0ecc9aSBen Hutchings break; 550cd0ecc9aSBen Hutchings case 32: 551cd0ecc9aSBen Hutchings val = le32_to_cpup((__le32 *)addr); 552cd0ecc9aSBen Hutchings break; 553cd0ecc9aSBen Hutchings case 64: 554cd0ecc9aSBen Hutchings val = le64_to_cpup((__le64 *)addr); 555cd0ecc9aSBen Hutchings break; 556cd0ecc9aSBen Hutchings default: 557cd0ecc9aSBen Hutchings WARN_ON(1); 558cd0ecc9aSBen Hutchings val = 0; 559cd0ecc9aSBen Hutchings break; 560cd0ecc9aSBen Hutchings } 561cd0ecc9aSBen Hutchings 562cd0ecc9aSBen Hutchings if (accumulate) 56387648cc9SEdward Cree stats[index] += val; 564cd0ecc9aSBen Hutchings else 56587648cc9SEdward Cree stats[index] = val; 566cd0ecc9aSBen Hutchings } 567cd0ecc9aSBen Hutchings } 568cd0ecc9aSBen Hutchings } 569f8f3b5aeSJon Cooper 570f8f3b5aeSJon Cooper void efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *rx_nodesc_drops) 571f8f3b5aeSJon Cooper { 572f8f3b5aeSJon Cooper /* if down, or this is the first update after coming up */ 573f8f3b5aeSJon Cooper if (!(efx->net_dev->flags & IFF_UP) || !efx->rx_nodesc_drops_prev_state) 574f8f3b5aeSJon Cooper efx->rx_nodesc_drops_while_down += 575f8f3b5aeSJon Cooper *rx_nodesc_drops - efx->rx_nodesc_drops_total; 576f8f3b5aeSJon Cooper efx->rx_nodesc_drops_total = *rx_nodesc_drops; 577f8f3b5aeSJon Cooper efx->rx_nodesc_drops_prev_state = !!(efx->net_dev->flags & IFF_UP); 578f8f3b5aeSJon Cooper *rx_nodesc_drops -= efx->rx_nodesc_drops_while_down; 579f8f3b5aeSJon Cooper } 580