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 	u32 dq_frames;
275 	u32 dq_bytes;
276 	struct dpaa2_eth_channel *channel;
277 	enum dpaa2_eth_fq_type type;
278 
279 	void (*consume)(struct dpaa2_eth_priv *priv,
280 			struct dpaa2_eth_channel *ch,
281 			const struct dpaa2_fd *fd,
282 			struct dpaa2_eth_fq *fq);
283 	struct dpaa2_eth_fq_stats stats;
284 };
285 
286 struct dpaa2_eth_channel {
287 	struct dpaa2_io_notification_ctx nctx;
288 	struct fsl_mc_device *dpcon;
289 	int dpcon_id;
290 	int ch_id;
291 	struct napi_struct napi;
292 	struct dpaa2_io *dpio;
293 	struct dpaa2_io_store *store;
294 	struct dpaa2_eth_priv *priv;
295 	int buf_count;
296 	struct dpaa2_eth_ch_stats stats;
297 };
298 
299 struct dpaa2_eth_dist_fields {
300 	u64 rxnfc_field;
301 	enum net_prot cls_prot;
302 	int cls_field;
303 	int size;
304 };
305 
306 struct dpaa2_eth_cls_rule {
307 	struct ethtool_rx_flow_spec fs;
308 	u8 in_use;
309 };
310 
311 /* Driver private data */
312 struct dpaa2_eth_priv {
313 	struct net_device *net_dev;
314 
315 	u8 num_fqs;
316 	struct dpaa2_eth_fq fq[DPAA2_ETH_MAX_QUEUES];
317 
318 	u8 num_channels;
319 	struct dpaa2_eth_channel *channel[DPAA2_ETH_MAX_DPCONS];
320 
321 	struct dpni_attr dpni_attrs;
322 	u16 dpni_ver_major;
323 	u16 dpni_ver_minor;
324 	u16 tx_data_offset;
325 
326 	struct fsl_mc_device *dpbp_dev;
327 	u16 bpid;
328 	struct iommu_domain *iommu_domain;
329 
330 	bool tx_tstamp; /* Tx timestamping enabled */
331 	bool rx_tstamp; /* Rx timestamping enabled */
332 
333 	u16 tx_qdid;
334 	u16 rx_buf_align;
335 	struct fsl_mc_io *mc_io;
336 	/* Cores which have an affine DPIO/DPCON.
337 	 * This is the cpu set on which Rx and Tx conf frames are processed
338 	 */
339 	struct cpumask dpio_cpumask;
340 
341 	/* Standard statistics */
342 	struct rtnl_link_stats64 __percpu *percpu_stats;
343 	/* Extra stats, in addition to the ones known by the kernel */
344 	struct dpaa2_eth_drv_stats __percpu *percpu_extras;
345 
346 	u16 mc_token;
347 
348 	struct dpni_link_state link_state;
349 	bool do_link_poll;
350 	struct task_struct *poll_thread;
351 
352 	/* enabled ethtool hashing bits */
353 	u64 rx_hash_fields;
354 	struct dpaa2_eth_cls_rule *cls_rules;
355 	u8 rx_cls_enabled;
356 };
357 
358 #define DPAA2_RXH_SUPPORTED	(RXH_L2DA | RXH_VLAN | RXH_L3_PROTO \
359 				| RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 \
360 				| RXH_L4_B_2_3)
361 
362 /* default Rx hash options, set during probing */
363 #define DPAA2_RXH_DEFAULT	(RXH_L3_PROTO | RXH_IP_SRC | RXH_IP_DST | \
364 				 RXH_L4_B_0_1 | RXH_L4_B_2_3)
365 
366 #define dpaa2_eth_hash_enabled(priv)	\
367 	((priv)->dpni_attrs.num_queues > 1)
368 
369 /* Required by struct dpni_rx_tc_dist_cfg::key_cfg_iova */
370 #define DPAA2_CLASSIFIER_DMA_SIZE 256
371 
372 extern const struct ethtool_ops dpaa2_ethtool_ops;
373 extern int dpaa2_phc_index;
374 
375 static inline int dpaa2_eth_cmp_dpni_ver(struct dpaa2_eth_priv *priv,
376 					 u16 ver_major, u16 ver_minor)
377 {
378 	if (priv->dpni_ver_major == ver_major)
379 		return priv->dpni_ver_minor - ver_minor;
380 	return priv->dpni_ver_major - ver_major;
381 }
382 
383 /* Minimum firmware version that supports a more flexible API
384  * for configuring the Rx flow hash key
385  */
386 #define DPNI_RX_DIST_KEY_VER_MAJOR	7
387 #define DPNI_RX_DIST_KEY_VER_MINOR	5
388 
389 #define dpaa2_eth_has_legacy_dist(priv)					\
390 	(dpaa2_eth_cmp_dpni_ver((priv), DPNI_RX_DIST_KEY_VER_MAJOR,	\
391 				DPNI_RX_DIST_KEY_VER_MINOR) < 0)
392 
393 #define dpaa2_eth_fs_count(priv)        \
394 	((priv)->dpni_attrs.fs_entries)
395 
396 enum dpaa2_eth_rx_dist {
397 	DPAA2_ETH_RX_DIST_HASH,
398 	DPAA2_ETH_RX_DIST_CLS
399 };
400 
401 /* Hardware only sees DPAA2_ETH_RX_BUF_SIZE, but the skb built around
402  * the buffer also needs space for its shared info struct, and we need
403  * to allocate enough to accommodate hardware alignment restrictions
404  */
405 static inline unsigned int dpaa2_eth_buf_raw_size(struct dpaa2_eth_priv *priv)
406 {
407 	return DPAA2_ETH_SKB_SIZE + priv->rx_buf_align;
408 }
409 
410 static inline
411 unsigned int dpaa2_eth_needed_headroom(struct dpaa2_eth_priv *priv,
412 				       struct sk_buff *skb)
413 {
414 	unsigned int headroom = DPAA2_ETH_SWA_SIZE;
415 
416 	/* For non-linear skbs we have no headroom requirement, as we build a
417 	 * SG frame with a newly allocated SGT buffer
418 	 */
419 	if (skb_is_nonlinear(skb))
420 		return 0;
421 
422 	/* If we have Tx timestamping, need 128B hardware annotation */
423 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
424 		headroom += DPAA2_ETH_TX_HWA_SIZE;
425 
426 	return headroom;
427 }
428 
429 /* Extra headroom space requested to hardware, in order to make sure there's
430  * no realloc'ing in forwarding scenarios
431  */
432 static inline unsigned int dpaa2_eth_rx_head_room(struct dpaa2_eth_priv *priv)
433 {
434 	return priv->tx_data_offset + DPAA2_ETH_TX_BUF_ALIGN -
435 	       DPAA2_ETH_RX_HWA_SIZE;
436 }
437 
438 /* We have exactly one {Rx, Tx conf} queue per channel */
439 static int dpaa2_eth_queue_count(struct dpaa2_eth_priv *priv)
440 {
441 	return priv->num_channels;
442 }
443 
444 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags);
445 int dpaa2_eth_cls_key_size(void);
446 int dpaa2_eth_cls_fld_off(int prot, int field);
447 
448 #endif	/* __DPAA2_H */
449