1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright 2014-2016 Freescale Semiconductor, Inc.
4  * Copyright 2017 NXP
5  */
6 
7 #ifndef __LDPAA_ETH_H
8 #define __LDPAA_ETH_H
9 
10 #include <linux/netdevice.h>
11 #include <fsl-mc/fsl_mc.h>
12 #include <fsl-mc/fsl_dpaa_fd.h>
13 #include <fsl-mc/fsl_dprc.h>
14 #include <fsl-mc/fsl_dpni.h>
15 #include <fsl-mc/fsl_dpbp.h>
16 #include <fsl-mc/fsl_dpio.h>
17 #include <fsl-mc/fsl_qbman_portal.h>
18 #include <fsl-mc/fsl_mc_private.h>
19 
20 enum ldpaa_eth_type {
21 	LDPAA_ETH_1G_E,
22 	LDPAA_ETH_10G_E,
23 };
24 
25 /* Arbitrary values for now, but we'll need to tune */
26 #define LDPAA_ETH_NUM_BUFS		(7 * 7)
27 #define LDPAA_ETH_REFILL_THRESH		(LDPAA_ETH_NUM_BUFS/2)
28 #define LDPAA_ETH_RX_BUFFER_SIZE	2048
29 
30 /* Hardware requires alignment for buffer address and length: 256-byte
31  * for ingress, 64-byte for egress. Using 256 for both.
32  */
33 #define LDPAA_ETH_BUF_ALIGN		256
34 
35 /* So far we're only accomodating a skb backpointer in the frame's
36  * software annotation, but the hardware options are either 0 or 64.
37  */
38 #define LDPAA_ETH_SWA_SIZE		64
39 
40 /* Annotation valid bits in FD FRC */
41 #define LDPAA_FD_FRC_FASV		0x8000
42 #define LDPAA_FD_FRC_FAEADV		0x4000
43 #define LDPAA_FD_FRC_FAPRV		0x2000
44 #define LDPAA_FD_FRC_FAIADV		0x1000
45 #define LDPAA_FD_FRC_FASWOV		0x0800
46 #define LDPAA_FD_FRC_FAICFDV		0x0400
47 
48 /* Annotation bits in FD CTRL */
49 #define LDPAA_FD_CTRL_ASAL		0x00020000	/* ASAL = 128 */
50 #define LDPAA_FD_CTRL_PTA		0x00800000
51 #define LDPAA_FD_CTRL_PTV1		0x00400000
52 
53 /* TODO: we may want to move this and other WRIOP related defines
54  * to a separate header
55  */
56 /* Frame annotation status */
57 struct ldpaa_fas {
58 	u8 reserved;
59 	u8 ppid;
60 	__le16 ifpid;
61 	__le32 status;
62 } __packed;
63 
64 /* Debug frame, otherwise supposed to be discarded */
65 #define LDPAA_ETH_FAS_DISC		0x80000000
66 /* MACSEC frame */
67 #define LDPAA_ETH_FAS_MS		0x40000000
68 #define LDPAA_ETH_FAS_PTP		0x08000000
69 /* Ethernet multicast frame */
70 #define LDPAA_ETH_FAS_MC		0x04000000
71 /* Ethernet broadcast frame */
72 #define LDPAA_ETH_FAS_BC		0x02000000
73 #define LDPAA_ETH_FAS_KSE		0x00040000
74 #define LDPAA_ETH_FAS_EOFHE		0x00020000
75 #define LDPAA_ETH_FAS_MNLE		0x00010000
76 #define LDPAA_ETH_FAS_TIDE		0x00008000
77 #define LDPAA_ETH_FAS_PIEE		0x00004000
78 /* Frame length error */
79 #define LDPAA_ETH_FAS_FLE		0x00002000
80 /* Frame physical error; our favourite pastime */
81 #define LDPAA_ETH_FAS_FPE		0x00001000
82 #define LDPAA_ETH_FAS_PTE		0x00000080
83 #define LDPAA_ETH_FAS_ISP		0x00000040
84 #define LDPAA_ETH_FAS_PHE		0x00000020
85 #define LDPAA_ETH_FAS_BLE		0x00000010
86 /* L3 csum validation performed */
87 #define LDPAA_ETH_FAS_L3CV		0x00000008
88 /* L3 csum error */
89 #define LDPAA_ETH_FAS_L3CE		0x00000004
90 /* L4 csum validation performed */
91 #define LDPAA_ETH_FAS_L4CV		0x00000002
92 /* L4 csum error */
93 #define LDPAA_ETH_FAS_L4CE		0x00000001
94 /* These bits always signal errors */
95 #define LDPAA_ETH_RX_ERR_MASK		(LDPAA_ETH_FAS_DISC	| \
96 					 LDPAA_ETH_FAS_KSE	| \
97 					 LDPAA_ETH_FAS_EOFHE	| \
98 					 LDPAA_ETH_FAS_MNLE	| \
99 					 LDPAA_ETH_FAS_TIDE	| \
100 					 LDPAA_ETH_FAS_PIEE	| \
101 					 LDPAA_ETH_FAS_FLE	| \
102 					 LDPAA_ETH_FAS_FPE	| \
103 					 LDPAA_ETH_FAS_PTE	| \
104 					 LDPAA_ETH_FAS_ISP	| \
105 					 LDPAA_ETH_FAS_PHE	| \
106 					 LDPAA_ETH_FAS_BLE	| \
107 					 LDPAA_ETH_FAS_L3CE	| \
108 					 LDPAA_ETH_FAS_L4CE)
109 /* Unsupported features in the ingress */
110 #define LDPAA_ETH_RX_UNSUPP_MASK	LDPAA_ETH_FAS_MS
111 /* TODO trim down the bitmask; not all of them apply to Tx-confirm */
112 #define LDPAA_ETH_TXCONF_ERR_MASK	(LDPAA_ETH_FAS_KSE	| \
113 					 LDPAA_ETH_FAS_EOFHE	| \
114 					 LDPAA_ETH_FAS_MNLE	| \
115 					 LDPAA_ETH_FAS_TIDE)
116 
117 static const char ldpaa_eth_dpni_stat_strings[][ETH_GSTRING_LEN] = {
118 	"[dpni ] rx frames",
119 	"[dpni ] rx bytes",
120 	"[dpni ] rx mcast frames",
121 	"[dpni ] rx mcast bytes",
122 	"[dpni ] rx bcast frames",
123 	"[dpni ] rx bcast bytes",
124 	"[dpni ] tx frames",
125 	"[dpni ] tx bytes",
126 	"[dpni ] tx mcast frames",
127 	"[dpni ] tx mcast bytes",
128 	"[dpni ] tx bcast frames",
129 	"[dpni ] tx bcast bytes",
130 	"[dpni ] rx filtered frames",
131 	"[dpni ] rx discarded frames",
132 	"[dpni ] rx nobuffer discards",
133 	"[dpni ] tx discarded frames",
134 	"[dpni ] tx confirmed frames",
135 	"[dpni ] tx dequeued bytes",
136 	"[dpni ] tx dequeued frames",
137 	"[dpni ] tx rejected bytes",
138 	"[dpni ] tx rejected frames",
139 	"[dpni ] tx pending frames",
140 };
141 
142 #define LDPAA_ETH_DPNI_NUM_STATS	ARRAY_SIZE(ldpaa_eth_dpni_stat_strings)
143 
144 static const char ldpaa_eth_dpmac_stat_strings[][ETH_GSTRING_LEN] = {
145 	[DPMAC_CNT_ING_ALL_FRAME]		= "[mac] rx all frames",
146 	[DPMAC_CNT_ING_GOOD_FRAME]		= "[mac] rx frames ok",
147 	[DPMAC_CNT_ING_ERR_FRAME]		= "[mac] rx frame errors",
148 	[DPMAC_CNT_ING_FRAME_DISCARD]		= "[mac] rx frame discards",
149 	[DPMAC_CNT_ING_UCAST_FRAME]		= "[mac] rx u-cast",
150 	[DPMAC_CNT_ING_BCAST_FRAME]		= "[mac] rx b-cast",
151 	[DPMAC_CNT_ING_MCAST_FRAME]		= "[mac] rx m-cast",
152 	[DPMAC_CNT_ING_FRAME_64]		= "[mac] rx 64 bytes",
153 	[DPMAC_CNT_ING_FRAME_127]		= "[mac] rx 65-127 bytes",
154 	[DPMAC_CNT_ING_FRAME_255]		= "[mac] rx 128-255 bytes",
155 	[DPMAC_CNT_ING_FRAME_511]		= "[mac] rx 256-511 bytes",
156 	[DPMAC_CNT_ING_FRAME_1023]		= "[mac] rx 512-1023 bytes",
157 	[DPMAC_CNT_ING_FRAME_1518]		= "[mac] rx 1024-1518 bytes",
158 	[DPMAC_CNT_ING_FRAME_1519_MAX]		= "[mac] rx 1519-max bytes",
159 	[DPMAC_CNT_ING_FRAG]			= "[mac] rx frags",
160 	[DPMAC_CNT_ING_JABBER]			= "[mac] rx jabber",
161 	[DPMAC_CNT_ING_ALIGN_ERR]		= "[mac] rx align errors",
162 	[DPMAC_CNT_ING_OVERSIZED]		= "[mac] rx oversized",
163 	[DPMAC_CNT_ING_VALID_PAUSE_FRAME]	= "[mac] rx pause",
164 	[DPMAC_CNT_ING_BYTE]			= "[mac] rx bytes",
165 	[DPMAC_CNT_EGR_GOOD_FRAME]		= "[mac] tx frames ok",
166 	[DPMAC_CNT_EGR_UCAST_FRAME]		= "[mac] tx u-cast",
167 	[DPMAC_CNT_EGR_MCAST_FRAME]		= "[mac] tx m-cast",
168 	[DPMAC_CNT_EGR_BCAST_FRAME]		= "[mac] tx b-cast",
169 	[DPMAC_CNT_EGR_ERR_FRAME]		= "[mac] tx frame errors",
170 	[DPMAC_CNT_EGR_UNDERSIZED]		= "[mac] tx undersized",
171 	[DPMAC_CNT_EGR_VALID_PAUSE_FRAME]	= "[mac] tx b-pause",
172 	[DPMAC_CNT_EGR_BYTE]			= "[mac] tx bytes",
173 };
174 
175 #define LDPAA_ETH_DPMAC_NUM_STATS	ARRAY_SIZE(ldpaa_eth_dpmac_stat_strings)
176 
177 struct ldpaa_eth_priv {
178 	struct phy_device *phy;
179 	int phy_mode;
180 	bool started;
181 	uint32_t dpmac_id;
182 	uint16_t dpmac_handle;
183 
184 	uint16_t tx_data_offset;
185 
186 	uint32_t rx_dflt_fqid;
187 	uint16_t tx_qdid;
188 	uint16_t tx_flow_id;
189 
190 	enum ldpaa_eth_type type;	/* 1G or 10G ethernet */
191 
192 	/* SW kept statistics */
193 	u64 dpni_stats[LDPAA_ETH_DPNI_NUM_STATS];
194 	u64 dpmac_stats[LDPAA_ETH_DPMAC_NUM_STATS];
195 };
196 
197 struct dprc_endpoint dpmac_endpoint;
198 struct dprc_endpoint dpni_endpoint;
199 
200 extern struct fsl_mc_io *dflt_mc_io;
201 extern struct fsl_dpbp_obj *dflt_dpbp;
202 extern struct fsl_dpio_obj *dflt_dpio;
203 extern struct fsl_dpni_obj *dflt_dpni;
204 extern uint16_t dflt_dprc_handle;
205 
206 static void ldpaa_dpbp_drain_cnt(int count);
207 static void ldpaa_dpbp_drain(void);
208 static int ldpaa_dpbp_seed(uint16_t bpid);
209 static void ldpaa_dpbp_free(void);
210 static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv);
211 static int ldpaa_dpbp_setup(void);
212 static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv);
213 static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv);
214 static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv);
215 #endif	/* __LDPAA_H */
216