1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016 NXP
4  */
5 
6 #ifndef __DPAA2_ETH_H
7 #define __DPAA2_ETH_H
8 
9 #include <linux/netdevice.h>
10 #include <linux/if_vlan.h>
11 #include <linux/fsl/mc.h>
12 
13 #include <soc/fsl/dpaa2-io.h>
14 #include <soc/fsl/dpaa2-fd.h>
15 #include "dpni.h"
16 #include "dpni-cmd.h"
17 
18 #include "dpaa2-eth-trace.h"
19 
20 #define DPAA2_WRIOP_VERSION(x, y, z) ((x) << 10 | (y) << 5 | (z) << 0)
21 
22 #define DPAA2_ETH_STORE_SIZE		16
23 
24 /* Maximum number of scatter-gather entries in an ingress frame,
25  * considering the maximum receive frame size is 64K
26  */
27 #define DPAA2_ETH_MAX_SG_ENTRIES	((64 * 1024) / DPAA2_ETH_RX_BUF_SIZE)
28 
29 /* Maximum acceptable MTU value. It is in direct relation with the hardware
30  * enforced Max Frame Length (currently 10k).
31  */
32 #define DPAA2_ETH_MFL			(10 * 1024)
33 #define DPAA2_ETH_MAX_MTU		(DPAA2_ETH_MFL - VLAN_ETH_HLEN)
34 /* Convert L3 MTU to L2 MFL */
35 #define DPAA2_ETH_L2_MAX_FRM(mtu)	((mtu) + VLAN_ETH_HLEN)
36 
37 /* Set the taildrop threshold (in bytes) to allow the enqueue of several jumbo
38  * frames in the Rx queues (length of the current frame is not
39  * taken into account when making the taildrop decision)
40  */
41 #define DPAA2_ETH_TAILDROP_THRESH	(64 * 1024)
42 
43 /* Maximum number of Tx confirmation frames to be processed
44  * in a single NAPI call
45  */
46 #define DPAA2_ETH_TXCONF_PER_NAPI	256
47 
48 /* Buffer quota per queue. Must be large enough such that for minimum sized
49  * frames taildrop kicks in before the bpool gets depleted, so we compute
50  * how many 64B frames fit inside the taildrop threshold and add a margin
51  * to accommodate the buffer refill delay.
52  */
53 #define DPAA2_ETH_MAX_FRAMES_PER_QUEUE	(DPAA2_ETH_TAILDROP_THRESH / 64)
54 #define DPAA2_ETH_NUM_BUFS		(DPAA2_ETH_MAX_FRAMES_PER_QUEUE + 256)
55 #define DPAA2_ETH_REFILL_THRESH		DPAA2_ETH_MAX_FRAMES_PER_QUEUE
56 
57 /* Maximum number of buffers that can be acquired/released through a single
58  * QBMan command
59  */
60 #define DPAA2_ETH_BUFS_PER_CMD		7
61 
62 /* Hardware requires alignment for ingress/egress buffer addresses */
63 #define DPAA2_ETH_TX_BUF_ALIGN		64
64 
65 #define DPAA2_ETH_RX_BUF_SIZE		2048
66 #define DPAA2_ETH_SKB_SIZE \
67 	(DPAA2_ETH_RX_BUF_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
68 
69 /* Hardware annotation area in RX/TX buffers */
70 #define DPAA2_ETH_RX_HWA_SIZE		64
71 #define DPAA2_ETH_TX_HWA_SIZE		128
72 
73 /* PTP nominal frequency 1GHz */
74 #define DPAA2_PTP_CLK_PERIOD_NS		1
75 
76 /* Due to a limitation in WRIOP 1.0.0, the RX buffer data must be aligned
77  * to 256B. For newer revisions, the requirement is only for 64B alignment
78  */
79 #define DPAA2_ETH_RX_BUF_ALIGN_REV1	256
80 #define DPAA2_ETH_RX_BUF_ALIGN		64
81 
82 /* We are accommodating a skb backpointer and some S/G info
83  * in the frame's software annotation. The hardware
84  * options are either 0 or 64, so we choose the latter.
85  */
86 #define DPAA2_ETH_SWA_SIZE		64
87 
88 /* Must keep this struct smaller than DPAA2_ETH_SWA_SIZE */
89 struct dpaa2_eth_swa {
90 	struct sk_buff *skb;
91 	struct scatterlist *scl;
92 	int num_sg;
93 	int sgt_size;
94 };
95 
96 /* Annotation valid bits in FD FRC */
97 #define DPAA2_FD_FRC_FASV		0x8000
98 #define DPAA2_FD_FRC_FAEADV		0x4000
99 #define DPAA2_FD_FRC_FAPRV		0x2000
100 #define DPAA2_FD_FRC_FAIADV		0x1000
101 #define DPAA2_FD_FRC_FASWOV		0x0800
102 #define DPAA2_FD_FRC_FAICFDV		0x0400
103 
104 /* Error bits in FD CTRL */
105 #define DPAA2_FD_RX_ERR_MASK		(FD_CTRL_SBE | FD_CTRL_FAERR)
106 #define DPAA2_FD_TX_ERR_MASK		(FD_CTRL_UFD	| \
107 					 FD_CTRL_SBE	| \
108 					 FD_CTRL_FSE	| \
109 					 FD_CTRL_FAERR)
110 
111 /* Annotation bits in FD CTRL */
112 #define DPAA2_FD_CTRL_ASAL		0x00020000	/* ASAL = 128B */
113 
114 /* Frame annotation status */
115 struct dpaa2_fas {
116 	u8 reserved;
117 	u8 ppid;
118 	__le16 ifpid;
119 	__le32 status;
120 };
121 
122 /* Frame annotation status word is located in the first 8 bytes
123  * of the buffer's hardware annoatation area
124  */
125 #define DPAA2_FAS_OFFSET		0
126 #define DPAA2_FAS_SIZE			(sizeof(struct dpaa2_fas))
127 
128 /* Timestamp is located in the next 8 bytes of the buffer's
129  * hardware annotation area
130  */
131 #define DPAA2_TS_OFFSET			0x8
132 
133 /* Frame annotation egress action descriptor */
134 #define DPAA2_FAEAD_OFFSET		0x58
135 
136 struct dpaa2_faead {
137 	__le32 conf_fqid;
138 	__le32 ctrl;
139 };
140 
141 #define DPAA2_FAEAD_A2V			0x20000000
142 #define DPAA2_FAEAD_UPDV		0x00001000
143 #define DPAA2_FAEAD_UPD			0x00000010
144 
145 /* Accessors for the hardware annotation fields that we use */
146 static inline void *dpaa2_get_hwa(void *buf_addr, bool swa)
147 {
148 	return buf_addr + (swa ? DPAA2_ETH_SWA_SIZE : 0);
149 }
150 
151 static inline struct dpaa2_fas *dpaa2_get_fas(void *buf_addr, bool swa)
152 {
153 	return dpaa2_get_hwa(buf_addr, swa) + DPAA2_FAS_OFFSET;
154 }
155 
156 static inline __le64 *dpaa2_get_ts(void *buf_addr, bool swa)
157 {
158 	return dpaa2_get_hwa(buf_addr, swa) + DPAA2_TS_OFFSET;
159 }
160 
161 static inline struct dpaa2_faead *dpaa2_get_faead(void *buf_addr, bool swa)
162 {
163 	return dpaa2_get_hwa(buf_addr, swa) + DPAA2_FAEAD_OFFSET;
164 }
165 
166 /* Error and status bits in the frame annotation status word */
167 /* Debug frame, otherwise supposed to be discarded */
168 #define DPAA2_FAS_DISC			0x80000000
169 /* MACSEC frame */
170 #define DPAA2_FAS_MS			0x40000000
171 #define DPAA2_FAS_PTP			0x08000000
172 /* Ethernet multicast frame */
173 #define DPAA2_FAS_MC			0x04000000
174 /* Ethernet broadcast frame */
175 #define DPAA2_FAS_BC			0x02000000
176 #define DPAA2_FAS_KSE			0x00040000
177 #define DPAA2_FAS_EOFHE			0x00020000
178 #define DPAA2_FAS_MNLE			0x00010000
179 #define DPAA2_FAS_TIDE			0x00008000
180 #define DPAA2_FAS_PIEE			0x00004000
181 /* Frame length error */
182 #define DPAA2_FAS_FLE			0x00002000
183 /* Frame physical error */
184 #define DPAA2_FAS_FPE			0x00001000
185 #define DPAA2_FAS_PTE			0x00000080
186 #define DPAA2_FAS_ISP			0x00000040
187 #define DPAA2_FAS_PHE			0x00000020
188 #define DPAA2_FAS_BLE			0x00000010
189 /* L3 csum validation performed */
190 #define DPAA2_FAS_L3CV			0x00000008
191 /* L3 csum error */
192 #define DPAA2_FAS_L3CE			0x00000004
193 /* L4 csum validation performed */
194 #define DPAA2_FAS_L4CV			0x00000002
195 /* L4 csum error */
196 #define DPAA2_FAS_L4CE			0x00000001
197 /* Possible errors on the ingress path */
198 #define DPAA2_FAS_RX_ERR_MASK		(DPAA2_FAS_KSE		| \
199 					 DPAA2_FAS_EOFHE	| \
200 					 DPAA2_FAS_MNLE		| \
201 					 DPAA2_FAS_TIDE		| \
202 					 DPAA2_FAS_PIEE		| \
203 					 DPAA2_FAS_FLE		| \
204 					 DPAA2_FAS_FPE		| \
205 					 DPAA2_FAS_PTE		| \
206 					 DPAA2_FAS_ISP		| \
207 					 DPAA2_FAS_PHE		| \
208 					 DPAA2_FAS_BLE		| \
209 					 DPAA2_FAS_L3CE		| \
210 					 DPAA2_FAS_L4CE)
211 
212 /* Time in milliseconds between link state updates */
213 #define DPAA2_ETH_LINK_STATE_REFRESH	1000
214 
215 /* Number of times to retry a frame enqueue before giving up.
216  * Value determined empirically, in order to minimize the number
217  * of frames dropped on Tx
218  */
219 #define DPAA2_ETH_ENQUEUE_RETRIES	10
220 
221 /* Driver statistics, other than those in struct rtnl_link_stats64.
222  * These are usually collected per-CPU and aggregated by ethtool.
223  */
224 struct dpaa2_eth_drv_stats {
225 	__u64	tx_conf_frames;
226 	__u64	tx_conf_bytes;
227 	__u64	tx_sg_frames;
228 	__u64	tx_sg_bytes;
229 	__u64	tx_reallocs;
230 	__u64	rx_sg_frames;
231 	__u64	rx_sg_bytes;
232 	/* Enqueues retried due to portal busy */
233 	__u64	tx_portal_busy;
234 };
235 
236 /* Per-FQ statistics */
237 struct dpaa2_eth_fq_stats {
238 	/* Number of frames received on this queue */
239 	__u64 frames;
240 };
241 
242 /* Per-channel statistics */
243 struct dpaa2_eth_ch_stats {
244 	/* Volatile dequeues retried due to portal busy */
245 	__u64 dequeue_portal_busy;
246 	/* Number of CDANs; useful to estimate avg NAPI len */
247 	__u64 cdan;
248 	/* Number of frames received on queues from this channel */
249 	__u64 frames;
250 	/* Pull errors */
251 	__u64 pull_err;
252 };
253 
254 /* Maximum number of queues associated with a DPNI */
255 #define DPAA2_ETH_MAX_RX_QUEUES		16
256 #define DPAA2_ETH_MAX_TX_QUEUES		16
257 #define DPAA2_ETH_MAX_QUEUES		(DPAA2_ETH_MAX_RX_QUEUES + \
258 					DPAA2_ETH_MAX_TX_QUEUES)
259 
260 #define DPAA2_ETH_MAX_DPCONS		16
261 
262 enum dpaa2_eth_fq_type {
263 	DPAA2_RX_FQ = 0,
264 	DPAA2_TX_CONF_FQ,
265 };
266 
267 struct dpaa2_eth_priv;
268 
269 struct dpaa2_eth_fq {
270 	u32 fqid;
271 	u32 tx_qdbin;
272 	u16 flowid;
273 	int target_cpu;
274 	struct dpaa2_eth_channel *channel;
275 	enum dpaa2_eth_fq_type type;
276 
277 	void (*consume)(struct dpaa2_eth_priv *priv,
278 			struct dpaa2_eth_channel *ch,
279 			const struct dpaa2_fd *fd,
280 			struct napi_struct *napi,
281 			u16 queue_id);
282 	struct dpaa2_eth_fq_stats stats;
283 };
284 
285 struct dpaa2_eth_channel {
286 	struct dpaa2_io_notification_ctx nctx;
287 	struct fsl_mc_device *dpcon;
288 	int dpcon_id;
289 	int ch_id;
290 	struct napi_struct napi;
291 	struct dpaa2_io *dpio;
292 	struct dpaa2_io_store *store;
293 	struct dpaa2_eth_priv *priv;
294 	int buf_count;
295 	struct dpaa2_eth_ch_stats stats;
296 };
297 
298 struct dpaa2_eth_dist_fields {
299 	u64 rxnfc_field;
300 	enum net_prot cls_prot;
301 	int cls_field;
302 	int size;
303 };
304 
305 struct dpaa2_eth_cls_rule {
306 	struct ethtool_rx_flow_spec fs;
307 	u8 in_use;
308 };
309 
310 /* Driver private data */
311 struct dpaa2_eth_priv {
312 	struct net_device *net_dev;
313 
314 	u8 num_fqs;
315 	struct dpaa2_eth_fq fq[DPAA2_ETH_MAX_QUEUES];
316 
317 	u8 num_channels;
318 	struct dpaa2_eth_channel *channel[DPAA2_ETH_MAX_DPCONS];
319 
320 	struct dpni_attr dpni_attrs;
321 	u16 dpni_ver_major;
322 	u16 dpni_ver_minor;
323 	u16 tx_data_offset;
324 
325 	struct fsl_mc_device *dpbp_dev;
326 	u16 bpid;
327 	struct iommu_domain *iommu_domain;
328 
329 	bool tx_tstamp; /* Tx timestamping enabled */
330 	bool rx_tstamp; /* Rx timestamping enabled */
331 
332 	u16 tx_qdid;
333 	u16 rx_buf_align;
334 	struct fsl_mc_io *mc_io;
335 	/* Cores which have an affine DPIO/DPCON.
336 	 * This is the cpu set on which Rx and Tx conf frames are processed
337 	 */
338 	struct cpumask dpio_cpumask;
339 
340 	/* Standard statistics */
341 	struct rtnl_link_stats64 __percpu *percpu_stats;
342 	/* Extra stats, in addition to the ones known by the kernel */
343 	struct dpaa2_eth_drv_stats __percpu *percpu_extras;
344 
345 	u16 mc_token;
346 
347 	struct dpni_link_state link_state;
348 	bool do_link_poll;
349 	struct task_struct *poll_thread;
350 
351 	/* enabled ethtool hashing bits */
352 	u64 rx_hash_fields;
353 	struct dpaa2_eth_cls_rule *cls_rules;
354 	u8 rx_cls_enabled;
355 };
356 
357 #define DPAA2_RXH_SUPPORTED	(RXH_L2DA | RXH_VLAN | RXH_L3_PROTO \
358 				| RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 \
359 				| RXH_L4_B_2_3)
360 
361 /* default Rx hash options, set during probing */
362 #define DPAA2_RXH_DEFAULT	(RXH_L3_PROTO | RXH_IP_SRC | RXH_IP_DST | \
363 				 RXH_L4_B_0_1 | RXH_L4_B_2_3)
364 
365 #define dpaa2_eth_hash_enabled(priv)	\
366 	((priv)->dpni_attrs.num_queues > 1)
367 
368 /* Required by struct dpni_rx_tc_dist_cfg::key_cfg_iova */
369 #define DPAA2_CLASSIFIER_DMA_SIZE 256
370 
371 extern const struct ethtool_ops dpaa2_ethtool_ops;
372 extern int dpaa2_phc_index;
373 
374 static inline int dpaa2_eth_cmp_dpni_ver(struct dpaa2_eth_priv *priv,
375 					 u16 ver_major, u16 ver_minor)
376 {
377 	if (priv->dpni_ver_major == ver_major)
378 		return priv->dpni_ver_minor - ver_minor;
379 	return priv->dpni_ver_major - ver_major;
380 }
381 
382 /* Minimum firmware version that supports a more flexible API
383  * for configuring the Rx flow hash key
384  */
385 #define DPNI_RX_DIST_KEY_VER_MAJOR	7
386 #define DPNI_RX_DIST_KEY_VER_MINOR	5
387 
388 #define dpaa2_eth_has_legacy_dist(priv)					\
389 	(dpaa2_eth_cmp_dpni_ver((priv), DPNI_RX_DIST_KEY_VER_MAJOR,	\
390 				DPNI_RX_DIST_KEY_VER_MINOR) < 0)
391 
392 #define dpaa2_eth_fs_count(priv)        \
393 	((priv)->dpni_attrs.fs_entries)
394 
395 enum dpaa2_eth_rx_dist {
396 	DPAA2_ETH_RX_DIST_HASH,
397 	DPAA2_ETH_RX_DIST_CLS
398 };
399 
400 /* Hardware only sees DPAA2_ETH_RX_BUF_SIZE, but the skb built around
401  * the buffer also needs space for its shared info struct, and we need
402  * to allocate enough to accommodate hardware alignment restrictions
403  */
404 static inline unsigned int dpaa2_eth_buf_raw_size(struct dpaa2_eth_priv *priv)
405 {
406 	return DPAA2_ETH_SKB_SIZE + priv->rx_buf_align;
407 }
408 
409 static inline
410 unsigned int dpaa2_eth_needed_headroom(struct dpaa2_eth_priv *priv,
411 				       struct sk_buff *skb)
412 {
413 	unsigned int headroom = DPAA2_ETH_SWA_SIZE;
414 
415 	/* For non-linear skbs we have no headroom requirement, as we build a
416 	 * SG frame with a newly allocated SGT buffer
417 	 */
418 	if (skb_is_nonlinear(skb))
419 		return 0;
420 
421 	/* If we have Tx timestamping, need 128B hardware annotation */
422 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
423 		headroom += DPAA2_ETH_TX_HWA_SIZE;
424 
425 	return headroom;
426 }
427 
428 /* Extra headroom space requested to hardware, in order to make sure there's
429  * no realloc'ing in forwarding scenarios
430  */
431 static inline unsigned int dpaa2_eth_rx_head_room(struct dpaa2_eth_priv *priv)
432 {
433 	return priv->tx_data_offset + DPAA2_ETH_TX_BUF_ALIGN -
434 	       DPAA2_ETH_RX_HWA_SIZE;
435 }
436 
437 static int dpaa2_eth_queue_count(struct dpaa2_eth_priv *priv)
438 {
439 	return priv->dpni_attrs.num_queues;
440 }
441 
442 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags);
443 int dpaa2_eth_cls_key_size(void);
444 int dpaa2_eth_cls_fld_off(int prot, int field);
445 
446 #endif	/* __DPAA2_H */
447