1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /* Copyright 2017-2019 NXP */
4 #include <linux/timer.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>
16 #define ENETC_MAC_MAXFRM_SIZE 9600
17 #define ENETC_MAX_MTU (ENETC_MAC_MAXFRM_SIZE - \
18 (ETH_FCS_LEN + ETH_HLEN + VLAN_HLEN))
20 struct enetc_tx_swbd {
29 #define ENETC_RX_MAXFRM_SIZE ENETC_MAC_MAXFRM_SIZE
30 #define ENETC_RXB_TRUESIZE 2048 /* PAGE_SIZE >> 1 */
31 #define ENETC_RXB_PAD NET_SKB_PAD /* add extra space if needed */
32 #define ENETC_RXB_DMA_SIZE \
33 (SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - ENETC_RXB_PAD)
35 struct enetc_rx_swbd {
41 struct enetc_ring_stats {
44 unsigned int rx_alloc_errs;
47 #define ENETC_RX_RING_DEFAULT_SIZE 512
48 #define ENETC_TX_RING_DEFAULT_SIZE 256
49 #define ENETC_DEFAULT_TX_WORK (ENETC_TX_RING_DEFAULT_SIZE / 2)
52 struct device *dev; /* for DMA mapping */
53 struct net_device *ndev;
54 void *bd_base; /* points to Rx or Tx BD ring */
60 int bd_count; /* # of BDs */
64 struct enetc_tx_swbd *tx_swbd;
65 struct enetc_rx_swbd *rx_swbd;
68 void __iomem *tcir; /* Tx */
69 int next_to_alloc; /* Rx */
71 void __iomem *idr; /* Interrupt Detect Register pointer */
73 struct enetc_ring_stats stats;
75 dma_addr_t bd_dma_base;
76 u8 tsd_enable; /* Time specific departure */
77 bool ext_en; /* enable h/w descriptor extensions */
78 } ____cacheline_aligned_in_smp;
80 static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i)
82 if (unlikely(++*i == bdr->bd_count))
86 static inline int enetc_bd_unused(struct enetc_bdr *bdr)
88 if (bdr->next_to_clean > bdr->next_to_use)
89 return bdr->next_to_clean - bdr->next_to_use - 1;
91 return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1;
95 #define ENETC_CBDR_DEFAULT_SIZE 64
97 void *bd_base; /* points to Rx or Tx BD ring */
101 int bd_count; /* # of BDs */
105 dma_addr_t bd_dma_base;
108 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
110 static inline union enetc_rx_bd *enetc_rxbd(struct enetc_bdr *rx_ring, int i)
114 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
118 return &(((union enetc_rx_bd *)rx_ring->bd_base)[hw_idx]);
121 static inline union enetc_rx_bd *enetc_rxbd_next(struct enetc_bdr *rx_ring,
122 union enetc_rx_bd *rxbd,
126 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
130 if (unlikely(++i == rx_ring->bd_count))
131 rxbd = rx_ring->bd_base;
136 static inline union enetc_rx_bd *enetc_rxbd_ext(union enetc_rx_bd *rxbd)
141 struct enetc_msg_swbd {
147 #define ENETC_REV1 0x1
149 ENETC_ERR_TXCSUM = BIT(0),
150 ENETC_ERR_VLAN_ISOL = BIT(1),
151 ENETC_ERR_UCMCSWP = BIT(2),
154 #define ENETC_SI_F_QBV BIT(0)
155 #define ENETC_SI_F_PSFP BIT(1)
157 /* PCI IEP device data */
159 struct pci_dev *pdev;
161 enum enetc_errata errata;
163 struct net_device *ndev; /* back ref. */
165 struct enetc_cbdr cbd_ring;
167 int num_rx_rings; /* how many rings are available in the SI */
170 int num_rss; /* number of RSS buckets */
175 #define ENETC_SI_ALIGN 32
177 static inline void *enetc_si_priv(const struct enetc_si *si)
179 return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN);
182 static inline bool enetc_si_is_pf(struct enetc_si *si)
184 return !!(si->hw.port);
187 #define ENETC_MAX_NUM_TXQS 8
188 #define ENETC_INT_NAME_MAX (IFNAMSIZ + 8)
190 struct enetc_int_vector {
192 void __iomem *tbier_base;
193 unsigned long tx_rings_map;
195 struct napi_struct napi;
196 char name[ENETC_INT_NAME_MAX];
198 struct enetc_bdr rx_ring;
199 struct enetc_bdr tx_ring[];
202 struct enetc_cls_rule {
203 struct ethtool_rx_flow_spec fs;
207 #define ENETC_MAX_BDR_INT 2 /* fixed to max # of available cpus */
212 u32 max_psfp_gatelist;
216 /* TODO: more hardware offloads */
217 enum enetc_active_offloads {
218 ENETC_F_RX_TSTAMP = BIT(0),
219 ENETC_F_TX_TSTAMP = BIT(1),
220 ENETC_F_QBV = BIT(2),
221 ENETC_F_QCI = BIT(3),
224 struct enetc_ndev_priv {
225 struct net_device *ndev;
226 struct device *dev; /* dma-mapping device */
229 int bdr_int_num; /* number of Rx/Tx ring interrupts */
230 struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT];
231 u16 num_rx_rings, num_tx_rings;
232 u16 rx_bd_count, tx_bd_count;
237 u32 speed; /* store speed for compare update pspeed */
239 struct enetc_bdr *tx_ring[16];
240 struct enetc_bdr *rx_ring[16];
242 struct enetc_cls_rule *cls_rules;
244 struct psfp_cap psfp_cap;
246 struct device_node *phy_node;
247 phy_interface_t if_mode;
252 /* VF-PF set primary MAC address message format */
253 struct enetc_msg_cmd_set_primary_mac {
254 struct enetc_msg_cmd_header header;
258 #define ENETC_CBD(R, i) (&(((struct enetc_cbd *)((R).bd_base))[i]))
260 #define ENETC_CBDR_TIMEOUT 1000 /* usecs */
262 /* PTP driver exports */
263 extern int enetc_phc_index;
266 int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv);
267 void enetc_pci_remove(struct pci_dev *pdev);
268 int enetc_alloc_msix(struct enetc_ndev_priv *priv);
269 void enetc_free_msix(struct enetc_ndev_priv *priv);
270 void enetc_get_si_caps(struct enetc_si *si);
271 void enetc_init_si_rings_params(struct enetc_ndev_priv *priv);
272 int enetc_alloc_si_resources(struct enetc_ndev_priv *priv);
273 void enetc_free_si_resources(struct enetc_ndev_priv *priv);
275 int enetc_open(struct net_device *ndev);
276 int enetc_close(struct net_device *ndev);
277 netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev);
278 struct net_device_stats *enetc_get_stats(struct net_device *ndev);
279 int enetc_set_features(struct net_device *ndev,
280 netdev_features_t features);
281 int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd);
282 int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type,
286 void enetc_set_ethtool_ops(struct net_device *ndev);
288 /* control buffer descriptor ring (CBDR) */
289 int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
290 char *mac_addr, int si_map);
291 int enetc_clear_mac_flt_entry(struct enetc_si *si, int index);
292 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
294 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes);
295 int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
296 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
297 int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);
299 #ifdef CONFIG_FSL_ENETC_QOS
300 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
301 void enetc_sched_speed_set(struct net_device *ndev);
302 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data);
303 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data);
304 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
306 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data);
307 int enetc_psfp_init(struct enetc_ndev_priv *priv);
308 int enetc_psfp_clean(struct enetc_ndev_priv *priv);
310 static inline void enetc_get_max_cap(struct enetc_ndev_priv *priv)
314 reg = enetc_port_rd(&priv->si->hw, ENETC_PSIDCAPR);
315 priv->psfp_cap.max_streamid = reg & ENETC_PSIDCAPR_MSK;
316 /* Port stream filter capability */
317 reg = enetc_port_rd(&priv->si->hw, ENETC_PSFCAPR);
318 priv->psfp_cap.max_psfp_filter = reg & ENETC_PSFCAPR_MSK;
319 /* Port stream gate capability */
320 reg = enetc_port_rd(&priv->si->hw, ENETC_PSGCAPR);
321 priv->psfp_cap.max_psfp_gate = (reg & ENETC_PSGCAPR_SGIT_MSK);
322 priv->psfp_cap.max_psfp_gatelist = (reg & ENETC_PSGCAPR_GCL_MSK) >> 16;
323 /* Port flow meter capability */
324 reg = enetc_port_rd(&priv->si->hw, ENETC_PFMCAPR);
325 priv->psfp_cap.max_psfp_meter = reg & ENETC_PFMCAPR_MSK;
328 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
330 struct enetc_hw *hw = &priv->si->hw;
333 enetc_get_max_cap(priv);
335 err = enetc_psfp_init(priv);
339 enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) |
340 ENETC_PPSFPMR_PSFPEN | ENETC_PPSFPMR_VS |
341 ENETC_PPSFPMR_PVC | ENETC_PPSFPMR_PVZC);
346 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
348 struct enetc_hw *hw = &priv->si->hw;
351 err = enetc_psfp_clean(priv);
355 enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) &
356 ~ENETC_PPSFPMR_PSFPEN & ~ENETC_PPSFPMR_VS &
357 ~ENETC_PPSFPMR_PVC & ~ENETC_PPSFPMR_PVZC);
359 memset(&priv->psfp_cap, 0, sizeof(struct psfp_cap));
365 #define enetc_setup_tc_taprio(ndev, type_data) -EOPNOTSUPP
366 #define enetc_sched_speed_set(ndev) (void)0
367 #define enetc_setup_tc_cbs(ndev, type_data) -EOPNOTSUPP
368 #define enetc_setup_tc_txtime(ndev, type_data) -EOPNOTSUPP
369 #define enetc_setup_tc_psfp(ndev, type_data) -EOPNOTSUPP
370 #define enetc_setup_tc_block_cb NULL
372 #define enetc_get_max_cap(p) \
373 memset(&((p)->psfp_cap), 0, sizeof(struct psfp_cap))
375 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
380 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)