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_BDR_DEFAULT_SIZE 1024
48 #define ENETC_DEFAULT_TX_WORK 256
51 struct device
*dev
; /* for DMA mapping */
52 struct net_device
*ndev
;
53 void *bd_base
; /* points to Rx or Tx BD ring */
59 int bd_count
; /* # of BDs */
63 struct enetc_tx_swbd
*tx_swbd
;
64 struct enetc_rx_swbd
*rx_swbd
;
67 void __iomem
*tcir
; /* Tx */
68 int next_to_alloc
; /* Rx */
70 void __iomem
*idr
; /* Interrupt Detect Register pointer */
72 struct enetc_ring_stats stats
;
74 dma_addr_t bd_dma_base
;
75 } ____cacheline_aligned_in_smp
;
77 static inline void enetc_bdr_idx_inc(struct enetc_bdr
*bdr
, int *i
)
79 if (unlikely(++*i
== bdr
->bd_count
))
83 static inline int enetc_bd_unused(struct enetc_bdr
*bdr
)
85 if (bdr
->next_to_clean
> bdr
->next_to_use
)
86 return bdr
->next_to_clean
- bdr
->next_to_use
- 1;
88 return bdr
->bd_count
+ bdr
->next_to_clean
- bdr
->next_to_use
- 1;
92 #define ENETC_CBDR_DEFAULT_SIZE 64
94 void *bd_base
; /* points to Rx or Tx BD ring */
98 int bd_count
; /* # of BDs */
102 dma_addr_t bd_dma_base
;
105 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
106 #define ENETC_RXBD(BDR, i) (&(((union enetc_rx_bd *)((BDR).bd_base))[i]))
108 struct enetc_msg_swbd
{
114 #define ENETC_REV1 0x1
116 ENETC_ERR_TXCSUM
= BIT(0),
117 ENETC_ERR_VLAN_ISOL
= BIT(1),
118 ENETC_ERR_UCMCSWP
= BIT(2),
121 /* PCI IEP device data */
123 struct pci_dev
*pdev
;
125 enum enetc_errata errata
;
127 struct net_device
*ndev
; /* back ref. */
129 struct enetc_cbdr cbd_ring
;
131 int num_rx_rings
; /* how many rings are available in the SI */
134 int num_rss
; /* number of RSS buckets */
138 #define ENETC_SI_ALIGN 32
140 static inline void *enetc_si_priv(const struct enetc_si
*si
)
142 return (char *)si
+ ALIGN(sizeof(struct enetc_si
), ENETC_SI_ALIGN
);
145 static inline bool enetc_si_is_pf(struct enetc_si
*si
)
147 return !!(si
->hw
.port
);
150 #define ENETC_MAX_NUM_TXQS 8
151 #define ENETC_INT_NAME_MAX (IFNAMSIZ + 8)
153 struct enetc_int_vector
{
155 void __iomem
*tbier_base
;
156 unsigned long tx_rings_map
;
158 struct napi_struct napi
;
159 char name
[ENETC_INT_NAME_MAX
];
161 struct enetc_bdr rx_ring ____cacheline_aligned_in_smp
;
162 struct enetc_bdr tx_ring
[0];
165 struct enetc_cls_rule
{
166 struct ethtool_rx_flow_spec fs
;
170 #define ENETC_MAX_BDR_INT 2 /* fixed to max # of available cpus */
172 /* TODO: more hardware offloads */
173 enum enetc_active_offloads
{
174 ENETC_F_RX_TSTAMP
= BIT(0),
175 ENETC_F_TX_TSTAMP
= BIT(1),
178 struct enetc_ndev_priv
{
179 struct net_device
*ndev
;
180 struct device
*dev
; /* dma-mapping device */
183 int bdr_int_num
; /* number of Rx/Tx ring interrupts */
184 struct enetc_int_vector
*int_vector
[ENETC_MAX_BDR_INT
];
185 u16 num_rx_rings
, num_tx_rings
;
186 u16 rx_bd_count
, tx_bd_count
;
191 struct enetc_bdr
*tx_ring
[16];
192 struct enetc_bdr
*rx_ring
[16];
194 struct enetc_cls_rule
*cls_rules
;
196 struct device_node
*phy_node
;
197 phy_interface_t if_mode
;
202 /* VF-PF set primary MAC address message format */
203 struct enetc_msg_cmd_set_primary_mac
{
204 struct enetc_msg_cmd_header header
;
208 #define ENETC_CBD(R, i) (&(((struct enetc_cbd *)((R).bd_base))[i]))
210 #define ENETC_CBDR_TIMEOUT 1000 /* usecs */
212 /* PTP driver exports */
213 extern int enetc_phc_index
;
216 int enetc_pci_probe(struct pci_dev
*pdev
, const char *name
, int sizeof_priv
);
217 void enetc_pci_remove(struct pci_dev
*pdev
);
218 int enetc_alloc_msix(struct enetc_ndev_priv
*priv
);
219 void enetc_free_msix(struct enetc_ndev_priv
*priv
);
220 void enetc_get_si_caps(struct enetc_si
*si
);
221 void enetc_init_si_rings_params(struct enetc_ndev_priv
*priv
);
222 int enetc_alloc_si_resources(struct enetc_ndev_priv
*priv
);
223 void enetc_free_si_resources(struct enetc_ndev_priv
*priv
);
225 int enetc_open(struct net_device
*ndev
);
226 int enetc_close(struct net_device
*ndev
);
227 netdev_tx_t
enetc_xmit(struct sk_buff
*skb
, struct net_device
*ndev
);
228 struct net_device_stats
*enetc_get_stats(struct net_device
*ndev
);
229 int enetc_set_features(struct net_device
*ndev
,
230 netdev_features_t features
);
231 int enetc_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
);
232 int enetc_setup_tc(struct net_device
*ndev
, enum tc_setup_type type
,
236 void enetc_set_ethtool_ops(struct net_device
*ndev
);
238 /* control buffer descriptor ring (CBDR) */
239 int enetc_set_mac_flt_entry(struct enetc_si
*si
, int index
,
240 char *mac_addr
, int si_map
);
241 int enetc_clear_mac_flt_entry(struct enetc_si
*si
, int index
);
242 int enetc_set_fs_entry(struct enetc_si
*si
, struct enetc_cmd_rfse
*rfse
,
244 void enetc_set_rss_key(struct enetc_hw
*hw
, const u8
*bytes
);
245 int enetc_get_rss_table(struct enetc_si
*si
, u32
*table
, int count
);
246 int enetc_set_rss_table(struct enetc_si
*si
, const u32
*table
, int count
);