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 	u8 is_dma_page:1;
25 	u8 check_wb:1;
26 	u8 do_tstamp:1;
27 };
28 
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)
34 
35 struct enetc_rx_swbd {
36 	dma_addr_t dma;
37 	struct page *page;
38 	u16 page_offset;
39 };
40 
41 struct enetc_ring_stats {
42 	unsigned int packets;
43 	unsigned int bytes;
44 	unsigned int rx_alloc_errs;
45 };
46 
47 #define ENETC_BDR_DEFAULT_SIZE	1024
48 #define ENETC_DEFAULT_TX_WORK	256
49 
50 struct enetc_bdr {
51 	struct device *dev; /* for DMA mapping */
52 	struct net_device *ndev;
53 	void *bd_base; /* points to Rx or Tx BD ring */
54 	union {
55 		void __iomem *tpir;
56 		void __iomem *rcir;
57 	};
58 	u16 index;
59 	int bd_count; /* # of BDs */
60 	int next_to_use;
61 	int next_to_clean;
62 	union {
63 		struct enetc_tx_swbd *tx_swbd;
64 		struct enetc_rx_swbd *rx_swbd;
65 	};
66 	union {
67 		void __iomem *tcir; /* Tx */
68 		int next_to_alloc; /* Rx */
69 	};
70 	void __iomem *idr; /* Interrupt Detect Register pointer */
71 
72 	struct enetc_ring_stats stats;
73 
74 	dma_addr_t bd_dma_base;
75 	u8 tsd_enable; /* Time specific departure */
76 } ____cacheline_aligned_in_smp;
77 
78 static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i)
79 {
80 	if (unlikely(++*i == bdr->bd_count))
81 		*i = 0;
82 }
83 
84 static inline int enetc_bd_unused(struct enetc_bdr *bdr)
85 {
86 	if (bdr->next_to_clean > bdr->next_to_use)
87 		return bdr->next_to_clean - bdr->next_to_use - 1;
88 
89 	return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1;
90 }
91 
92 /* Control BD ring */
93 #define ENETC_CBDR_DEFAULT_SIZE	64
94 struct enetc_cbdr {
95 	void *bd_base; /* points to Rx or Tx BD ring */
96 	void __iomem *pir;
97 	void __iomem *cir;
98 
99 	int bd_count; /* # of BDs */
100 	int next_to_use;
101 	int next_to_clean;
102 
103 	dma_addr_t bd_dma_base;
104 };
105 
106 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
107 #define ENETC_RXBD(BDR, i) (&(((union enetc_rx_bd *)((BDR).bd_base))[i]))
108 
109 struct enetc_msg_swbd {
110 	void *vaddr;
111 	dma_addr_t dma;
112 	int size;
113 };
114 
115 #define ENETC_REV1	0x1
116 enum enetc_errata {
117 	ENETC_ERR_TXCSUM	= BIT(0),
118 	ENETC_ERR_VLAN_ISOL	= BIT(1),
119 	ENETC_ERR_UCMCSWP	= BIT(2),
120 };
121 
122 #define ENETC_SI_F_QBV BIT(0)
123 
124 /* PCI IEP device data */
125 struct enetc_si {
126 	struct pci_dev *pdev;
127 	struct enetc_hw hw;
128 	enum enetc_errata errata;
129 
130 	struct net_device *ndev; /* back ref. */
131 
132 	struct enetc_cbdr cbd_ring;
133 
134 	int num_rx_rings; /* how many rings are available in the SI */
135 	int num_tx_rings;
136 	int num_fs_entries;
137 	int num_rss; /* number of RSS buckets */
138 	unsigned short pad;
139 	int hw_features;
140 };
141 
142 #define ENETC_SI_ALIGN	32
143 
144 static inline void *enetc_si_priv(const struct enetc_si *si)
145 {
146 	return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN);
147 }
148 
149 static inline bool enetc_si_is_pf(struct enetc_si *si)
150 {
151 	return !!(si->hw.port);
152 }
153 
154 #define ENETC_MAX_NUM_TXQS	8
155 #define ENETC_INT_NAME_MAX	(IFNAMSIZ + 8)
156 
157 struct enetc_int_vector {
158 	void __iomem *rbier;
159 	void __iomem *tbier_base;
160 	unsigned long tx_rings_map;
161 	int count_tx_rings;
162 	struct napi_struct napi;
163 	char name[ENETC_INT_NAME_MAX];
164 
165 	struct enetc_bdr rx_ring ____cacheline_aligned_in_smp;
166 	struct enetc_bdr tx_ring[0];
167 };
168 
169 struct enetc_cls_rule {
170 	struct ethtool_rx_flow_spec fs;
171 	int used;
172 };
173 
174 #define ENETC_MAX_BDR_INT	2 /* fixed to max # of available cpus */
175 
176 /* TODO: more hardware offloads */
177 enum enetc_active_offloads {
178 	ENETC_F_RX_TSTAMP	= BIT(0),
179 	ENETC_F_TX_TSTAMP	= BIT(1),
180 	ENETC_F_QBV             = BIT(2),
181 };
182 
183 struct enetc_ndev_priv {
184 	struct net_device *ndev;
185 	struct device *dev; /* dma-mapping device */
186 	struct enetc_si *si;
187 
188 	int bdr_int_num; /* number of Rx/Tx ring interrupts */
189 	struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT];
190 	u16 num_rx_rings, num_tx_rings;
191 	u16 rx_bd_count, tx_bd_count;
192 
193 	u16 msg_enable;
194 	int active_offloads;
195 
196 	u32 speed; /* store speed for compare update pspeed */
197 
198 	struct enetc_bdr *tx_ring[16];
199 	struct enetc_bdr *rx_ring[16];
200 
201 	struct enetc_cls_rule *cls_rules;
202 
203 	struct device_node *phy_node;
204 	phy_interface_t if_mode;
205 };
206 
207 /* Messaging */
208 
209 /* VF-PF set primary MAC address message format */
210 struct enetc_msg_cmd_set_primary_mac {
211 	struct enetc_msg_cmd_header header;
212 	struct sockaddr mac;
213 };
214 
215 #define ENETC_CBD(R, i)	(&(((struct enetc_cbd *)((R).bd_base))[i]))
216 
217 #define ENETC_CBDR_TIMEOUT	1000 /* usecs */
218 
219 /* PTP driver exports */
220 extern int enetc_phc_index;
221 
222 /* SI common */
223 int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv);
224 void enetc_pci_remove(struct pci_dev *pdev);
225 int enetc_alloc_msix(struct enetc_ndev_priv *priv);
226 void enetc_free_msix(struct enetc_ndev_priv *priv);
227 void enetc_get_si_caps(struct enetc_si *si);
228 void enetc_init_si_rings_params(struct enetc_ndev_priv *priv);
229 int enetc_alloc_si_resources(struct enetc_ndev_priv *priv);
230 void enetc_free_si_resources(struct enetc_ndev_priv *priv);
231 
232 int enetc_open(struct net_device *ndev);
233 int enetc_close(struct net_device *ndev);
234 netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev);
235 struct net_device_stats *enetc_get_stats(struct net_device *ndev);
236 int enetc_set_features(struct net_device *ndev,
237 		       netdev_features_t features);
238 int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd);
239 int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type,
240 		   void *type_data);
241 
242 /* ethtool */
243 void enetc_set_ethtool_ops(struct net_device *ndev);
244 
245 /* control buffer descriptor ring (CBDR) */
246 int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
247 			    char *mac_addr, int si_map);
248 int enetc_clear_mac_flt_entry(struct enetc_si *si, int index);
249 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
250 		       int index);
251 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes);
252 int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
253 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
254 int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);
255 
256 #ifdef CONFIG_FSL_ENETC_QOS
257 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
258 void enetc_sched_speed_set(struct net_device *ndev);
259 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data);
260 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data);
261 #else
262 #define enetc_setup_tc_taprio(ndev, type_data) -EOPNOTSUPP
263 #define enetc_sched_speed_set(ndev) (void)0
264 #define enetc_setup_tc_cbs(ndev, type_data) -EOPNOTSUPP
265 #define enetc_setup_tc_txtime(ndev, type_data) -EOPNOTSUPP
266 #endif
267