1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */ 2 /* Copyright 2017-2019 NXP */ 3 4 #include <linux/timer.h> 5 #include <linux/pci.h> 6 #include <linux/netdevice.h> 7 #include <linux/etherdevice.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/skbuff.h> 10 #include <linux/ethtool.h> 11 #include <linux/if_vlan.h> 12 #include <linux/phy.h> 13 14 #include "enetc_hw.h" 15 16 #define ENETC_MAC_MAXFRM_SIZE 9600 17 #define ENETC_MAX_MTU (ENETC_MAC_MAXFRM_SIZE - \ 18 (ETH_FCS_LEN + ETH_HLEN + VLAN_HLEN)) 19 20 struct enetc_tx_swbd { 21 struct sk_buff *skb; 22 dma_addr_t dma; 23 u16 len; 24 u16 is_dma_page; 25 }; 26 27 #define ENETC_RX_MAXFRM_SIZE ENETC_MAC_MAXFRM_SIZE 28 #define ENETC_RXB_TRUESIZE 2048 /* PAGE_SIZE >> 1 */ 29 #define ENETC_RXB_PAD NET_SKB_PAD /* add extra space if needed */ 30 #define ENETC_RXB_DMA_SIZE \ 31 (SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - ENETC_RXB_PAD) 32 33 struct enetc_rx_swbd { 34 dma_addr_t dma; 35 struct page *page; 36 u16 page_offset; 37 }; 38 39 struct enetc_ring_stats { 40 unsigned int packets; 41 unsigned int bytes; 42 unsigned int rx_alloc_errs; 43 }; 44 45 #define ENETC_BDR_DEFAULT_SIZE 1024 46 #define ENETC_DEFAULT_TX_WORK 256 47 48 struct enetc_bdr { 49 struct device *dev; /* for DMA mapping */ 50 struct net_device *ndev; 51 void *bd_base; /* points to Rx or Tx BD ring */ 52 union { 53 void __iomem *tpir; 54 void __iomem *rcir; 55 }; 56 u16 index; 57 int bd_count; /* # of BDs */ 58 int next_to_use; 59 int next_to_clean; 60 union { 61 struct enetc_tx_swbd *tx_swbd; 62 struct enetc_rx_swbd *rx_swbd; 63 }; 64 union { 65 void __iomem *tcir; /* Tx */ 66 int next_to_alloc; /* Rx */ 67 }; 68 void __iomem *idr; /* Interrupt Detect Register pointer */ 69 70 struct enetc_ring_stats stats; 71 72 dma_addr_t bd_dma_base; 73 } ____cacheline_aligned_in_smp; 74 75 static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i) 76 { 77 if (unlikely(++*i == bdr->bd_count)) 78 *i = 0; 79 } 80 81 static inline int enetc_bd_unused(struct enetc_bdr *bdr) 82 { 83 if (bdr->next_to_clean > bdr->next_to_use) 84 return bdr->next_to_clean - bdr->next_to_use - 1; 85 86 return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1; 87 } 88 89 /* Control BD ring */ 90 #define ENETC_CBDR_DEFAULT_SIZE 64 91 struct enetc_cbdr { 92 void *bd_base; /* points to Rx or Tx BD ring */ 93 void __iomem *pir; 94 void __iomem *cir; 95 96 int bd_count; /* # of BDs */ 97 int next_to_use; 98 int next_to_clean; 99 100 dma_addr_t bd_dma_base; 101 }; 102 103 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i])) 104 #define ENETC_RXBD(BDR, i) (&(((union enetc_rx_bd *)((BDR).bd_base))[i])) 105 106 struct enetc_msg_swbd { 107 void *vaddr; 108 dma_addr_t dma; 109 int size; 110 }; 111 112 #define ENETC_REV1 0x1 113 enum enetc_errata { 114 ENETC_ERR_TXCSUM = BIT(0), 115 ENETC_ERR_VLAN_ISOL = BIT(1), 116 ENETC_ERR_UCMCSWP = BIT(2), 117 }; 118 119 /* PCI IEP device data */ 120 struct enetc_si { 121 struct pci_dev *pdev; 122 struct enetc_hw hw; 123 enum enetc_errata errata; 124 125 struct net_device *ndev; /* back ref. */ 126 127 struct enetc_cbdr cbd_ring; 128 129 int num_rx_rings; /* how many rings are available in the SI */ 130 int num_tx_rings; 131 int num_fs_entries; 132 int num_rss; /* number of RSS buckets */ 133 unsigned short pad; 134 }; 135 136 #define ENETC_SI_ALIGN 32 137 138 static inline void *enetc_si_priv(const struct enetc_si *si) 139 { 140 return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN); 141 } 142 143 static inline bool enetc_si_is_pf(struct enetc_si *si) 144 { 145 return !!(si->hw.port); 146 } 147 148 #define ENETC_MAX_NUM_TXQS 8 149 #define ENETC_INT_NAME_MAX (IFNAMSIZ + 8) 150 151 struct enetc_int_vector { 152 void __iomem *rbier; 153 void __iomem *tbier_base; 154 unsigned long tx_rings_map; 155 int count_tx_rings; 156 struct napi_struct napi; 157 char name[ENETC_INT_NAME_MAX]; 158 159 struct enetc_bdr rx_ring ____cacheline_aligned_in_smp; 160 struct enetc_bdr tx_ring[0]; 161 }; 162 163 struct enetc_cls_rule { 164 struct ethtool_rx_flow_spec fs; 165 int used; 166 }; 167 168 #define ENETC_MAX_BDR_INT 2 /* fixed to max # of available cpus */ 169 170 struct enetc_ndev_priv { 171 struct net_device *ndev; 172 struct device *dev; /* dma-mapping device */ 173 struct enetc_si *si; 174 175 int bdr_int_num; /* number of Rx/Tx ring interrupts */ 176 struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT]; 177 u16 num_rx_rings, num_tx_rings; 178 u16 rx_bd_count, tx_bd_count; 179 180 u16 msg_enable; 181 182 struct enetc_bdr *tx_ring[16]; 183 struct enetc_bdr *rx_ring[16]; 184 185 struct enetc_cls_rule *cls_rules; 186 187 struct device_node *phy_node; 188 phy_interface_t if_mode; 189 }; 190 191 /* Messaging */ 192 193 /* VF-PF set primary MAC address message format */ 194 struct enetc_msg_cmd_set_primary_mac { 195 struct enetc_msg_cmd_header header; 196 struct sockaddr mac; 197 }; 198 199 #define ENETC_CBD(R, i) (&(((struct enetc_cbd *)((R).bd_base))[i])) 200 201 #define ENETC_CBDR_TIMEOUT 1000 /* usecs */ 202 203 /* SI common */ 204 int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv); 205 void enetc_pci_remove(struct pci_dev *pdev); 206 int enetc_alloc_msix(struct enetc_ndev_priv *priv); 207 void enetc_free_msix(struct enetc_ndev_priv *priv); 208 void enetc_get_si_caps(struct enetc_si *si); 209 void enetc_init_si_rings_params(struct enetc_ndev_priv *priv); 210 int enetc_alloc_si_resources(struct enetc_ndev_priv *priv); 211 void enetc_free_si_resources(struct enetc_ndev_priv *priv); 212 213 int enetc_open(struct net_device *ndev); 214 int enetc_close(struct net_device *ndev); 215 netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev); 216 struct net_device_stats *enetc_get_stats(struct net_device *ndev); 217 int enetc_set_features(struct net_device *ndev, 218 netdev_features_t features); 219 /* ethtool */ 220 void enetc_set_ethtool_ops(struct net_device *ndev); 221 222 /* control buffer descriptor ring (CBDR) */ 223 int enetc_set_mac_flt_entry(struct enetc_si *si, int index, 224 char *mac_addr, int si_map); 225 int enetc_clear_mac_flt_entry(struct enetc_si *si, int index); 226 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse, 227 int index); 228 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes); 229 int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count); 230 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count); 231