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 	bool ext_en; /* enable h/w descriptor extensions */
77 } ____cacheline_aligned_in_smp;
78 
79 static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i)
80 {
81 	if (unlikely(++*i == bdr->bd_count))
82 		*i = 0;
83 }
84 
85 static inline int enetc_bd_unused(struct enetc_bdr *bdr)
86 {
87 	if (bdr->next_to_clean > bdr->next_to_use)
88 		return bdr->next_to_clean - bdr->next_to_use - 1;
89 
90 	return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1;
91 }
92 
93 /* Control BD ring */
94 #define ENETC_CBDR_DEFAULT_SIZE	64
95 struct enetc_cbdr {
96 	void *bd_base; /* points to Rx or Tx BD ring */
97 	void __iomem *pir;
98 	void __iomem *cir;
99 
100 	int bd_count; /* # of BDs */
101 	int next_to_use;
102 	int next_to_clean;
103 
104 	dma_addr_t bd_dma_base;
105 };
106 
107 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
108 
109 static inline union enetc_rx_bd *enetc_rxbd(struct enetc_bdr *rx_ring, int i)
110 {
111 	int hw_idx = i;
112 
113 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
114 	if (rx_ring->ext_en)
115 		hw_idx = 2 * i;
116 #endif
117 	return &(((union enetc_rx_bd *)rx_ring->bd_base)[hw_idx]);
118 }
119 
120 static inline union enetc_rx_bd *enetc_rxbd_next(struct enetc_bdr *rx_ring,
121 						 union enetc_rx_bd *rxbd,
122 						 int i)
123 {
124 	rxbd++;
125 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
126 	if (rx_ring->ext_en)
127 		rxbd++;
128 #endif
129 	if (unlikely(++i == rx_ring->bd_count))
130 		rxbd = rx_ring->bd_base;
131 
132 	return rxbd;
133 }
134 
135 static inline union enetc_rx_bd *enetc_rxbd_ext(union enetc_rx_bd *rxbd)
136 {
137 	return ++rxbd;
138 }
139 
140 struct enetc_msg_swbd {
141 	void *vaddr;
142 	dma_addr_t dma;
143 	int size;
144 };
145 
146 #define ENETC_REV1	0x1
147 enum enetc_errata {
148 	ENETC_ERR_TXCSUM	= BIT(0),
149 	ENETC_ERR_VLAN_ISOL	= BIT(1),
150 	ENETC_ERR_UCMCSWP	= BIT(2),
151 };
152 
153 #define ENETC_SI_F_QBV BIT(0)
154 #define ENETC_SI_F_PSFP BIT(1)
155 
156 /* PCI IEP device data */
157 struct enetc_si {
158 	struct pci_dev *pdev;
159 	struct enetc_hw hw;
160 	enum enetc_errata errata;
161 
162 	struct net_device *ndev; /* back ref. */
163 
164 	struct enetc_cbdr cbd_ring;
165 
166 	int num_rx_rings; /* how many rings are available in the SI */
167 	int num_tx_rings;
168 	int num_fs_entries;
169 	int num_rss; /* number of RSS buckets */
170 	unsigned short pad;
171 	int hw_features;
172 };
173 
174 #define ENETC_SI_ALIGN	32
175 
176 static inline void *enetc_si_priv(const struct enetc_si *si)
177 {
178 	return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN);
179 }
180 
181 static inline bool enetc_si_is_pf(struct enetc_si *si)
182 {
183 	return !!(si->hw.port);
184 }
185 
186 #define ENETC_MAX_NUM_TXQS	8
187 #define ENETC_INT_NAME_MAX	(IFNAMSIZ + 8)
188 
189 struct enetc_int_vector {
190 	void __iomem *rbier;
191 	void __iomem *tbier_base;
192 	unsigned long tx_rings_map;
193 	int count_tx_rings;
194 	struct napi_struct napi;
195 	char name[ENETC_INT_NAME_MAX];
196 
197 	struct enetc_bdr rx_ring ____cacheline_aligned_in_smp;
198 	struct enetc_bdr tx_ring[];
199 };
200 
201 struct enetc_cls_rule {
202 	struct ethtool_rx_flow_spec fs;
203 	int used;
204 };
205 
206 #define ENETC_MAX_BDR_INT	2 /* fixed to max # of available cpus */
207 struct psfp_cap {
208 	u32 max_streamid;
209 	u32 max_psfp_filter;
210 	u32 max_psfp_gate;
211 	u32 max_psfp_gatelist;
212 	u32 max_psfp_meter;
213 };
214 
215 /* TODO: more hardware offloads */
216 enum enetc_active_offloads {
217 	ENETC_F_RX_TSTAMP	= BIT(0),
218 	ENETC_F_TX_TSTAMP	= BIT(1),
219 	ENETC_F_QBV             = BIT(2),
220 	ENETC_F_QCI		= BIT(3),
221 };
222 
223 struct enetc_ndev_priv {
224 	struct net_device *ndev;
225 	struct device *dev; /* dma-mapping device */
226 	struct enetc_si *si;
227 
228 	int bdr_int_num; /* number of Rx/Tx ring interrupts */
229 	struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT];
230 	u16 num_rx_rings, num_tx_rings;
231 	u16 rx_bd_count, tx_bd_count;
232 
233 	u16 msg_enable;
234 	int active_offloads;
235 
236 	u32 speed; /* store speed for compare update pspeed */
237 
238 	struct enetc_bdr *tx_ring[16];
239 	struct enetc_bdr *rx_ring[16];
240 
241 	struct enetc_cls_rule *cls_rules;
242 
243 	struct psfp_cap psfp_cap;
244 
245 	struct device_node *phy_node;
246 	phy_interface_t if_mode;
247 };
248 
249 /* Messaging */
250 
251 /* VF-PF set primary MAC address message format */
252 struct enetc_msg_cmd_set_primary_mac {
253 	struct enetc_msg_cmd_header header;
254 	struct sockaddr mac;
255 };
256 
257 #define ENETC_CBD(R, i)	(&(((struct enetc_cbd *)((R).bd_base))[i]))
258 
259 #define ENETC_CBDR_TIMEOUT	1000 /* usecs */
260 
261 /* PTP driver exports */
262 extern int enetc_phc_index;
263 
264 /* SI common */
265 int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv);
266 void enetc_pci_remove(struct pci_dev *pdev);
267 int enetc_alloc_msix(struct enetc_ndev_priv *priv);
268 void enetc_free_msix(struct enetc_ndev_priv *priv);
269 void enetc_get_si_caps(struct enetc_si *si);
270 void enetc_init_si_rings_params(struct enetc_ndev_priv *priv);
271 int enetc_alloc_si_resources(struct enetc_ndev_priv *priv);
272 void enetc_free_si_resources(struct enetc_ndev_priv *priv);
273 
274 int enetc_open(struct net_device *ndev);
275 int enetc_close(struct net_device *ndev);
276 netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev);
277 struct net_device_stats *enetc_get_stats(struct net_device *ndev);
278 int enetc_set_features(struct net_device *ndev,
279 		       netdev_features_t features);
280 int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd);
281 int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type,
282 		   void *type_data);
283 
284 /* ethtool */
285 void enetc_set_ethtool_ops(struct net_device *ndev);
286 
287 /* control buffer descriptor ring (CBDR) */
288 int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
289 			    char *mac_addr, int si_map);
290 int enetc_clear_mac_flt_entry(struct enetc_si *si, int index);
291 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
292 		       int index);
293 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes);
294 int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
295 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
296 int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);
297 
298 #ifdef CONFIG_FSL_ENETC_QOS
299 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
300 void enetc_sched_speed_set(struct net_device *ndev);
301 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data);
302 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data);
303 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
304 			    void *cb_priv);
305 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data);
306 int enetc_psfp_init(struct enetc_ndev_priv *priv);
307 int enetc_psfp_clean(struct enetc_ndev_priv *priv);
308 
309 static inline void enetc_get_max_cap(struct enetc_ndev_priv *priv)
310 {
311 	u32 reg;
312 
313 	reg = enetc_port_rd(&priv->si->hw, ENETC_PSIDCAPR);
314 	priv->psfp_cap.max_streamid = reg & ENETC_PSIDCAPR_MSK;
315 	/* Port stream filter capability */
316 	reg = enetc_port_rd(&priv->si->hw, ENETC_PSFCAPR);
317 	priv->psfp_cap.max_psfp_filter = reg & ENETC_PSFCAPR_MSK;
318 	/* Port stream gate capability */
319 	reg = enetc_port_rd(&priv->si->hw, ENETC_PSGCAPR);
320 	priv->psfp_cap.max_psfp_gate = (reg & ENETC_PSGCAPR_SGIT_MSK);
321 	priv->psfp_cap.max_psfp_gatelist = (reg & ENETC_PSGCAPR_GCL_MSK) >> 16;
322 	/* Port flow meter capability */
323 	reg = enetc_port_rd(&priv->si->hw, ENETC_PFMCAPR);
324 	priv->psfp_cap.max_psfp_meter = reg & ENETC_PFMCAPR_MSK;
325 }
326 
327 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
328 {
329 	struct enetc_hw *hw = &priv->si->hw;
330 	int err;
331 
332 	enetc_get_max_cap(priv);
333 
334 	err = enetc_psfp_init(priv);
335 	if (err)
336 		return err;
337 
338 	enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) |
339 		 ENETC_PPSFPMR_PSFPEN | ENETC_PPSFPMR_VS |
340 		 ENETC_PPSFPMR_PVC | ENETC_PPSFPMR_PVZC);
341 
342 	return 0;
343 }
344 
345 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
346 {
347 	struct enetc_hw *hw = &priv->si->hw;
348 	int err;
349 
350 	err = enetc_psfp_clean(priv);
351 	if (err)
352 		return err;
353 
354 	enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) &
355 		 ~ENETC_PPSFPMR_PSFPEN & ~ENETC_PPSFPMR_VS &
356 		 ~ENETC_PPSFPMR_PVC & ~ENETC_PPSFPMR_PVZC);
357 
358 	memset(&priv->psfp_cap, 0, sizeof(struct psfp_cap));
359 
360 	return 0;
361 }
362 
363 #else
364 #define enetc_setup_tc_taprio(ndev, type_data) -EOPNOTSUPP
365 #define enetc_sched_speed_set(ndev) (void)0
366 #define enetc_setup_tc_cbs(ndev, type_data) -EOPNOTSUPP
367 #define enetc_setup_tc_txtime(ndev, type_data) -EOPNOTSUPP
368 #define enetc_setup_tc_psfp(ndev, type_data) -EOPNOTSUPP
369 #define enetc_setup_tc_block_cb NULL
370 
371 #define enetc_get_max_cap(p)		\
372 	memset(&((p)->psfp_cap), 0, sizeof(struct psfp_cap))
373 
374 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
375 {
376 	return 0;
377 }
378 
379 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
380 {
381 	return 0;
382 }
383 #endif
384