1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/types.h>
4 #include <linux/clk.h>
5 #include <linux/platform_device.h>
6 #include <linux/pm_runtime.h>
7 #include <linux/acpi.h>
8 #include <linux/of_mdio.h>
9 #include <linux/etherdevice.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/netlink.h>
13 #include <linux/bpf.h>
14 #include <linux/bpf_trace.h>
15 
16 #include <net/tcp.h>
17 #include <net/page_pool.h>
18 #include <net/ip6_checksum.h>
19 
20 #define NETSEC_REG_SOFT_RST			0x104
21 #define NETSEC_REG_COM_INIT			0x120
22 
23 #define NETSEC_REG_TOP_STATUS			0x200
24 #define NETSEC_IRQ_RX				BIT(1)
25 #define NETSEC_IRQ_TX				BIT(0)
26 
27 #define NETSEC_REG_TOP_INTEN			0x204
28 #define NETSEC_REG_INTEN_SET			0x234
29 #define NETSEC_REG_INTEN_CLR			0x238
30 
31 #define NETSEC_REG_NRM_TX_STATUS		0x400
32 #define NETSEC_REG_NRM_TX_INTEN			0x404
33 #define NETSEC_REG_NRM_TX_INTEN_SET		0x428
34 #define NETSEC_REG_NRM_TX_INTEN_CLR		0x42c
35 #define NRM_TX_ST_NTOWNR	BIT(17)
36 #define NRM_TX_ST_TR_ERR	BIT(16)
37 #define NRM_TX_ST_TXDONE	BIT(15)
38 #define NRM_TX_ST_TMREXP	BIT(14)
39 
40 #define NETSEC_REG_NRM_RX_STATUS		0x440
41 #define NETSEC_REG_NRM_RX_INTEN			0x444
42 #define NETSEC_REG_NRM_RX_INTEN_SET		0x468
43 #define NETSEC_REG_NRM_RX_INTEN_CLR		0x46c
44 #define NRM_RX_ST_RC_ERR	BIT(16)
45 #define NRM_RX_ST_PKTCNT	BIT(15)
46 #define NRM_RX_ST_TMREXP	BIT(14)
47 
48 #define NETSEC_REG_PKT_CMD_BUF			0xd0
49 
50 #define NETSEC_REG_CLK_EN			0x100
51 
52 #define NETSEC_REG_PKT_CTRL			0x140
53 
54 #define NETSEC_REG_DMA_TMR_CTRL			0x20c
55 #define NETSEC_REG_F_TAIKI_MC_VER		0x22c
56 #define NETSEC_REG_F_TAIKI_VER			0x230
57 #define NETSEC_REG_DMA_HM_CTRL			0x214
58 #define NETSEC_REG_DMA_MH_CTRL			0x220
59 #define NETSEC_REG_ADDR_DIS_CORE		0x218
60 #define NETSEC_REG_DMAC_HM_CMD_BUF		0x210
61 #define NETSEC_REG_DMAC_MH_CMD_BUF		0x21c
62 
63 #define NETSEC_REG_NRM_TX_PKTCNT		0x410
64 
65 #define NETSEC_REG_NRM_TX_DONE_PKTCNT		0x414
66 #define NETSEC_REG_NRM_TX_DONE_TXINT_PKTCNT	0x418
67 
68 #define NETSEC_REG_NRM_TX_TMR			0x41c
69 
70 #define NETSEC_REG_NRM_RX_PKTCNT		0x454
71 #define NETSEC_REG_NRM_RX_RXINT_PKTCNT		0x458
72 #define NETSEC_REG_NRM_TX_TXINT_TMR		0x420
73 #define NETSEC_REG_NRM_RX_RXINT_TMR		0x460
74 
75 #define NETSEC_REG_NRM_RX_TMR			0x45c
76 
77 #define NETSEC_REG_NRM_TX_DESC_START_UP		0x434
78 #define NETSEC_REG_NRM_TX_DESC_START_LW		0x408
79 #define NETSEC_REG_NRM_RX_DESC_START_UP		0x474
80 #define NETSEC_REG_NRM_RX_DESC_START_LW		0x448
81 
82 #define NETSEC_REG_NRM_TX_CONFIG		0x430
83 #define NETSEC_REG_NRM_RX_CONFIG		0x470
84 
85 #define MAC_REG_STATUS				0x1024
86 #define MAC_REG_DATA				0x11c0
87 #define MAC_REG_CMD				0x11c4
88 #define MAC_REG_FLOW_TH				0x11cc
89 #define MAC_REG_INTF_SEL			0x11d4
90 #define MAC_REG_DESC_INIT			0x11fc
91 #define MAC_REG_DESC_SOFT_RST			0x1204
92 #define NETSEC_REG_MODE_TRANS_COMP_STATUS	0x500
93 
94 #define GMAC_REG_MCR				0x0000
95 #define GMAC_REG_MFFR				0x0004
96 #define GMAC_REG_GAR				0x0010
97 #define GMAC_REG_GDR				0x0014
98 #define GMAC_REG_FCR				0x0018
99 #define GMAC_REG_BMR				0x1000
100 #define GMAC_REG_RDLAR				0x100c
101 #define GMAC_REG_TDLAR				0x1010
102 #define GMAC_REG_OMR				0x1018
103 
104 #define MHZ(n)		((n) * 1000 * 1000)
105 
106 #define NETSEC_TX_SHIFT_OWN_FIELD		31
107 #define NETSEC_TX_SHIFT_LD_FIELD		30
108 #define NETSEC_TX_SHIFT_DRID_FIELD		24
109 #define NETSEC_TX_SHIFT_PT_FIELD		21
110 #define NETSEC_TX_SHIFT_TDRID_FIELD		16
111 #define NETSEC_TX_SHIFT_CC_FIELD		15
112 #define NETSEC_TX_SHIFT_FS_FIELD		9
113 #define NETSEC_TX_LAST				8
114 #define NETSEC_TX_SHIFT_CO			7
115 #define NETSEC_TX_SHIFT_SO			6
116 #define NETSEC_TX_SHIFT_TRS_FIELD		4
117 
118 #define NETSEC_RX_PKT_OWN_FIELD			31
119 #define NETSEC_RX_PKT_LD_FIELD			30
120 #define NETSEC_RX_PKT_SDRID_FIELD		24
121 #define NETSEC_RX_PKT_FR_FIELD			23
122 #define NETSEC_RX_PKT_ER_FIELD			21
123 #define NETSEC_RX_PKT_ERR_FIELD			16
124 #define NETSEC_RX_PKT_TDRID_FIELD		12
125 #define NETSEC_RX_PKT_FS_FIELD			9
126 #define NETSEC_RX_PKT_LS_FIELD			8
127 #define NETSEC_RX_PKT_CO_FIELD			6
128 
129 #define NETSEC_RX_PKT_ERR_MASK			3
130 
131 #define NETSEC_MAX_TX_PKT_LEN			1518
132 #define NETSEC_MAX_TX_JUMBO_PKT_LEN		9018
133 
134 #define NETSEC_RING_GMAC			15
135 #define NETSEC_RING_MAX				2
136 
137 #define NETSEC_TCP_SEG_LEN_MAX			1460
138 #define NETSEC_TCP_JUMBO_SEG_LEN_MAX		8960
139 
140 #define NETSEC_RX_CKSUM_NOTAVAIL		0
141 #define NETSEC_RX_CKSUM_OK			1
142 #define NETSEC_RX_CKSUM_NG			2
143 
144 #define NETSEC_TOP_IRQ_REG_CODE_LOAD_END	BIT(20)
145 #define NETSEC_IRQ_TRANSITION_COMPLETE		BIT(4)
146 
147 #define NETSEC_MODE_TRANS_COMP_IRQ_N2T		BIT(20)
148 #define NETSEC_MODE_TRANS_COMP_IRQ_T2N		BIT(19)
149 
150 #define NETSEC_INT_PKTCNT_MAX			2047
151 
152 #define NETSEC_FLOW_START_TH_MAX		95
153 #define NETSEC_FLOW_STOP_TH_MAX			95
154 #define NETSEC_FLOW_PAUSE_TIME_MIN		5
155 
156 #define NETSEC_CLK_EN_REG_DOM_ALL		0x3f
157 
158 #define NETSEC_PKT_CTRL_REG_MODE_NRM		BIT(28)
159 #define NETSEC_PKT_CTRL_REG_EN_JUMBO		BIT(27)
160 #define NETSEC_PKT_CTRL_REG_LOG_CHKSUM_ER	BIT(3)
161 #define NETSEC_PKT_CTRL_REG_LOG_HD_INCOMPLETE	BIT(2)
162 #define NETSEC_PKT_CTRL_REG_LOG_HD_ER		BIT(1)
163 #define NETSEC_PKT_CTRL_REG_DRP_NO_MATCH	BIT(0)
164 
165 #define NETSEC_CLK_EN_REG_DOM_G			BIT(5)
166 #define NETSEC_CLK_EN_REG_DOM_C			BIT(1)
167 #define NETSEC_CLK_EN_REG_DOM_D			BIT(0)
168 
169 #define NETSEC_COM_INIT_REG_DB			BIT(2)
170 #define NETSEC_COM_INIT_REG_CLS			BIT(1)
171 #define NETSEC_COM_INIT_REG_ALL			(NETSEC_COM_INIT_REG_CLS | \
172 						 NETSEC_COM_INIT_REG_DB)
173 
174 #define NETSEC_SOFT_RST_REG_RESET		0
175 #define NETSEC_SOFT_RST_REG_RUN			BIT(31)
176 
177 #define NETSEC_DMA_CTRL_REG_STOP		1
178 #define MH_CTRL__MODE_TRANS			BIT(20)
179 
180 #define NETSEC_GMAC_CMD_ST_READ			0
181 #define NETSEC_GMAC_CMD_ST_WRITE		BIT(28)
182 #define NETSEC_GMAC_CMD_ST_BUSY			BIT(31)
183 
184 #define NETSEC_GMAC_BMR_REG_COMMON		0x00412080
185 #define NETSEC_GMAC_BMR_REG_RESET		0x00020181
186 #define NETSEC_GMAC_BMR_REG_SWR			0x00000001
187 
188 #define NETSEC_GMAC_OMR_REG_ST			BIT(13)
189 #define NETSEC_GMAC_OMR_REG_SR			BIT(1)
190 
191 #define NETSEC_GMAC_MCR_REG_IBN			BIT(30)
192 #define NETSEC_GMAC_MCR_REG_CST			BIT(25)
193 #define NETSEC_GMAC_MCR_REG_JE			BIT(20)
194 #define NETSEC_MCR_PS				BIT(15)
195 #define NETSEC_GMAC_MCR_REG_FES			BIT(14)
196 #define NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON	0x0000280c
197 #define NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON	0x0001a00c
198 
199 #define NETSEC_FCR_RFE				BIT(2)
200 #define NETSEC_FCR_TFE				BIT(1)
201 
202 #define NETSEC_GMAC_GAR_REG_GW			BIT(1)
203 #define NETSEC_GMAC_GAR_REG_GB			BIT(0)
204 
205 #define NETSEC_GMAC_GAR_REG_SHIFT_PA		11
206 #define NETSEC_GMAC_GAR_REG_SHIFT_GR		6
207 #define GMAC_REG_SHIFT_CR_GAR			2
208 
209 #define NETSEC_GMAC_GAR_REG_CR_25_35_MHZ	2
210 #define NETSEC_GMAC_GAR_REG_CR_35_60_MHZ	3
211 #define NETSEC_GMAC_GAR_REG_CR_60_100_MHZ	0
212 #define NETSEC_GMAC_GAR_REG_CR_100_150_MHZ	1
213 #define NETSEC_GMAC_GAR_REG_CR_150_250_MHZ	4
214 #define NETSEC_GMAC_GAR_REG_CR_250_300_MHZ	5
215 
216 #define NETSEC_GMAC_RDLAR_REG_COMMON		0x18000
217 #define NETSEC_GMAC_TDLAR_REG_COMMON		0x1c000
218 
219 #define NETSEC_REG_NETSEC_VER_F_TAIKI		0x50000
220 
221 #define NETSEC_REG_DESC_RING_CONFIG_CFG_UP	BIT(31)
222 #define NETSEC_REG_DESC_RING_CONFIG_CH_RST	BIT(30)
223 #define NETSEC_REG_DESC_TMR_MODE		4
224 #define NETSEC_REG_DESC_ENDIAN			0
225 
226 #define NETSEC_MAC_DESC_SOFT_RST_SOFT_RST	1
227 #define NETSEC_MAC_DESC_INIT_REG_INIT		1
228 
229 #define NETSEC_EEPROM_MAC_ADDRESS		0x00
230 #define NETSEC_EEPROM_HM_ME_ADDRESS_H		0x08
231 #define NETSEC_EEPROM_HM_ME_ADDRESS_L		0x0C
232 #define NETSEC_EEPROM_HM_ME_SIZE		0x10
233 #define NETSEC_EEPROM_MH_ME_ADDRESS_H		0x14
234 #define NETSEC_EEPROM_MH_ME_ADDRESS_L		0x18
235 #define NETSEC_EEPROM_MH_ME_SIZE		0x1C
236 #define NETSEC_EEPROM_PKT_ME_ADDRESS		0x20
237 #define NETSEC_EEPROM_PKT_ME_SIZE		0x24
238 
239 #define DESC_NUM	256
240 
241 #define NETSEC_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN)
242 #define NETSEC_RXBUF_HEADROOM (max(XDP_PACKET_HEADROOM, NET_SKB_PAD) + \
243 			       NET_IP_ALIGN)
244 #define NETSEC_RX_BUF_NON_DATA (NETSEC_RXBUF_HEADROOM + \
245 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
246 
247 #define DESC_SZ	sizeof(struct netsec_de)
248 
249 #define NETSEC_F_NETSEC_VER_MAJOR_NUM(x)	((x) & 0xffff0000)
250 
251 #define NETSEC_XDP_PASS          0
252 #define NETSEC_XDP_CONSUMED      BIT(0)
253 #define NETSEC_XDP_TX            BIT(1)
254 #define NETSEC_XDP_REDIR         BIT(2)
255 
256 enum ring_id {
257 	NETSEC_RING_TX = 0,
258 	NETSEC_RING_RX
259 };
260 
261 enum buf_type {
262 	TYPE_NETSEC_SKB = 0,
263 	TYPE_NETSEC_XDP_TX,
264 	TYPE_NETSEC_XDP_NDO,
265 };
266 
267 struct netsec_desc {
268 	union {
269 		struct sk_buff *skb;
270 		struct xdp_frame *xdpf;
271 	};
272 	dma_addr_t dma_addr;
273 	void *addr;
274 	u16 len;
275 	u8 buf_type;
276 };
277 
278 struct netsec_desc_ring {
279 	dma_addr_t desc_dma;
280 	struct netsec_desc *desc;
281 	void *vaddr;
282 	u16 head, tail;
283 	u16 xdp_xmit; /* netsec_xdp_xmit packets */
284 	struct page_pool *page_pool;
285 	struct xdp_rxq_info xdp_rxq;
286 	spinlock_t lock; /* XDP tx queue locking */
287 };
288 
289 struct netsec_priv {
290 	struct netsec_desc_ring desc_ring[NETSEC_RING_MAX];
291 	struct ethtool_coalesce et_coalesce;
292 	struct bpf_prog *xdp_prog;
293 	spinlock_t reglock; /* protect reg access */
294 	struct napi_struct napi;
295 	phy_interface_t phy_interface;
296 	struct net_device *ndev;
297 	struct device_node *phy_np;
298 	struct phy_device *phydev;
299 	struct mii_bus *mii_bus;
300 	void __iomem *ioaddr;
301 	void __iomem *eeprom_base;
302 	struct device *dev;
303 	struct clk *clk;
304 	u32 msg_enable;
305 	u32 freq;
306 	u32 phy_addr;
307 	bool rx_cksum_offload_flag;
308 };
309 
310 struct netsec_de { /* Netsec Descriptor layout */
311 	u32 attr;
312 	u32 data_buf_addr_up;
313 	u32 data_buf_addr_lw;
314 	u32 buf_len_info;
315 };
316 
317 struct netsec_tx_pkt_ctrl {
318 	u16 tcp_seg_len;
319 	bool tcp_seg_offload_flag;
320 	bool cksum_offload_flag;
321 };
322 
323 struct netsec_rx_pkt_info {
324 	int rx_cksum_result;
325 	int err_code;
326 	bool err_flag;
327 };
328 
329 static void netsec_write(struct netsec_priv *priv, u32 reg_addr, u32 val)
330 {
331 	writel(val, priv->ioaddr + reg_addr);
332 }
333 
334 static u32 netsec_read(struct netsec_priv *priv, u32 reg_addr)
335 {
336 	return readl(priv->ioaddr + reg_addr);
337 }
338 
339 /************* MDIO BUS OPS FOLLOW *************/
340 
341 #define TIMEOUT_SPINS_MAC		1000
342 #define TIMEOUT_SECONDARY_MS_MAC	100
343 
344 static u32 netsec_clk_type(u32 freq)
345 {
346 	if (freq < MHZ(35))
347 		return NETSEC_GMAC_GAR_REG_CR_25_35_MHZ;
348 	if (freq < MHZ(60))
349 		return NETSEC_GMAC_GAR_REG_CR_35_60_MHZ;
350 	if (freq < MHZ(100))
351 		return NETSEC_GMAC_GAR_REG_CR_60_100_MHZ;
352 	if (freq < MHZ(150))
353 		return NETSEC_GMAC_GAR_REG_CR_100_150_MHZ;
354 	if (freq < MHZ(250))
355 		return NETSEC_GMAC_GAR_REG_CR_150_250_MHZ;
356 
357 	return NETSEC_GMAC_GAR_REG_CR_250_300_MHZ;
358 }
359 
360 static int netsec_wait_while_busy(struct netsec_priv *priv, u32 addr, u32 mask)
361 {
362 	u32 timeout = TIMEOUT_SPINS_MAC;
363 
364 	while (--timeout && netsec_read(priv, addr) & mask)
365 		cpu_relax();
366 	if (timeout)
367 		return 0;
368 
369 	timeout = TIMEOUT_SECONDARY_MS_MAC;
370 	while (--timeout && netsec_read(priv, addr) & mask)
371 		usleep_range(1000, 2000);
372 
373 	if (timeout)
374 		return 0;
375 
376 	netdev_WARN(priv->ndev, "%s: timeout\n", __func__);
377 
378 	return -ETIMEDOUT;
379 }
380 
381 static int netsec_mac_write(struct netsec_priv *priv, u32 addr, u32 value)
382 {
383 	netsec_write(priv, MAC_REG_DATA, value);
384 	netsec_write(priv, MAC_REG_CMD, addr | NETSEC_GMAC_CMD_ST_WRITE);
385 	return netsec_wait_while_busy(priv,
386 				      MAC_REG_CMD, NETSEC_GMAC_CMD_ST_BUSY);
387 }
388 
389 static int netsec_mac_read(struct netsec_priv *priv, u32 addr, u32 *read)
390 {
391 	int ret;
392 
393 	netsec_write(priv, MAC_REG_CMD, addr | NETSEC_GMAC_CMD_ST_READ);
394 	ret = netsec_wait_while_busy(priv,
395 				     MAC_REG_CMD, NETSEC_GMAC_CMD_ST_BUSY);
396 	if (ret)
397 		return ret;
398 
399 	*read = netsec_read(priv, MAC_REG_DATA);
400 
401 	return 0;
402 }
403 
404 static int netsec_mac_wait_while_busy(struct netsec_priv *priv,
405 				      u32 addr, u32 mask)
406 {
407 	u32 timeout = TIMEOUT_SPINS_MAC;
408 	int ret, data;
409 
410 	do {
411 		ret = netsec_mac_read(priv, addr, &data);
412 		if (ret)
413 			break;
414 		cpu_relax();
415 	} while (--timeout && (data & mask));
416 
417 	if (timeout)
418 		return 0;
419 
420 	timeout = TIMEOUT_SECONDARY_MS_MAC;
421 	do {
422 		usleep_range(1000, 2000);
423 
424 		ret = netsec_mac_read(priv, addr, &data);
425 		if (ret)
426 			break;
427 		cpu_relax();
428 	} while (--timeout && (data & mask));
429 
430 	if (timeout && !ret)
431 		return 0;
432 
433 	netdev_WARN(priv->ndev, "%s: timeout\n", __func__);
434 
435 	return -ETIMEDOUT;
436 }
437 
438 static int netsec_mac_update_to_phy_state(struct netsec_priv *priv)
439 {
440 	struct phy_device *phydev = priv->ndev->phydev;
441 	u32 value = 0;
442 
443 	value = phydev->duplex ? NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON :
444 				 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON;
445 
446 	if (phydev->speed != SPEED_1000)
447 		value |= NETSEC_MCR_PS;
448 
449 	if (priv->phy_interface != PHY_INTERFACE_MODE_GMII &&
450 	    phydev->speed == SPEED_100)
451 		value |= NETSEC_GMAC_MCR_REG_FES;
452 
453 	value |= NETSEC_GMAC_MCR_REG_CST | NETSEC_GMAC_MCR_REG_JE;
454 
455 	if (phy_interface_mode_is_rgmii(priv->phy_interface))
456 		value |= NETSEC_GMAC_MCR_REG_IBN;
457 
458 	if (netsec_mac_write(priv, GMAC_REG_MCR, value))
459 		return -ETIMEDOUT;
460 
461 	return 0;
462 }
463 
464 static int netsec_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr);
465 
466 static int netsec_phy_write(struct mii_bus *bus,
467 			    int phy_addr, int reg, u16 val)
468 {
469 	int status;
470 	struct netsec_priv *priv = bus->priv;
471 
472 	if (netsec_mac_write(priv, GMAC_REG_GDR, val))
473 		return -ETIMEDOUT;
474 	if (netsec_mac_write(priv, GMAC_REG_GAR,
475 			     phy_addr << NETSEC_GMAC_GAR_REG_SHIFT_PA |
476 			     reg << NETSEC_GMAC_GAR_REG_SHIFT_GR |
477 			     NETSEC_GMAC_GAR_REG_GW | NETSEC_GMAC_GAR_REG_GB |
478 			     (netsec_clk_type(priv->freq) <<
479 			      GMAC_REG_SHIFT_CR_GAR)))
480 		return -ETIMEDOUT;
481 
482 	status = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR,
483 					    NETSEC_GMAC_GAR_REG_GB);
484 
485 	/* Developerbox implements RTL8211E PHY and there is
486 	 * a compatibility problem with F_GMAC4.
487 	 * RTL8211E expects MDC clock must be kept toggling for several
488 	 * clock cycle with MDIO high before entering the IDLE state.
489 	 * To meet this requirement, netsec driver needs to issue dummy
490 	 * read(e.g. read PHYID1(offset 0x2) register) right after write.
491 	 */
492 	netsec_phy_read(bus, phy_addr, MII_PHYSID1);
493 
494 	return status;
495 }
496 
497 static int netsec_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr)
498 {
499 	struct netsec_priv *priv = bus->priv;
500 	u32 data;
501 	int ret;
502 
503 	if (netsec_mac_write(priv, GMAC_REG_GAR, NETSEC_GMAC_GAR_REG_GB |
504 			     phy_addr << NETSEC_GMAC_GAR_REG_SHIFT_PA |
505 			     reg_addr << NETSEC_GMAC_GAR_REG_SHIFT_GR |
506 			     (netsec_clk_type(priv->freq) <<
507 			      GMAC_REG_SHIFT_CR_GAR)))
508 		return -ETIMEDOUT;
509 
510 	ret = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR,
511 					 NETSEC_GMAC_GAR_REG_GB);
512 	if (ret)
513 		return ret;
514 
515 	ret = netsec_mac_read(priv, GMAC_REG_GDR, &data);
516 	if (ret)
517 		return ret;
518 
519 	return data;
520 }
521 
522 /************* ETHTOOL_OPS FOLLOW *************/
523 
524 static void netsec_et_get_drvinfo(struct net_device *net_device,
525 				  struct ethtool_drvinfo *info)
526 {
527 	strlcpy(info->driver, "netsec", sizeof(info->driver));
528 	strlcpy(info->bus_info, dev_name(net_device->dev.parent),
529 		sizeof(info->bus_info));
530 }
531 
532 static int netsec_et_get_coalesce(struct net_device *net_device,
533 				  struct ethtool_coalesce *et_coalesce)
534 {
535 	struct netsec_priv *priv = netdev_priv(net_device);
536 
537 	*et_coalesce = priv->et_coalesce;
538 
539 	return 0;
540 }
541 
542 static int netsec_et_set_coalesce(struct net_device *net_device,
543 				  struct ethtool_coalesce *et_coalesce)
544 {
545 	struct netsec_priv *priv = netdev_priv(net_device);
546 
547 	priv->et_coalesce = *et_coalesce;
548 
549 	if (priv->et_coalesce.tx_coalesce_usecs < 50)
550 		priv->et_coalesce.tx_coalesce_usecs = 50;
551 	if (priv->et_coalesce.tx_max_coalesced_frames < 1)
552 		priv->et_coalesce.tx_max_coalesced_frames = 1;
553 
554 	netsec_write(priv, NETSEC_REG_NRM_TX_DONE_TXINT_PKTCNT,
555 		     priv->et_coalesce.tx_max_coalesced_frames);
556 	netsec_write(priv, NETSEC_REG_NRM_TX_TXINT_TMR,
557 		     priv->et_coalesce.tx_coalesce_usecs);
558 	netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_SET, NRM_TX_ST_TXDONE);
559 	netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_SET, NRM_TX_ST_TMREXP);
560 
561 	if (priv->et_coalesce.rx_coalesce_usecs < 50)
562 		priv->et_coalesce.rx_coalesce_usecs = 50;
563 	if (priv->et_coalesce.rx_max_coalesced_frames < 1)
564 		priv->et_coalesce.rx_max_coalesced_frames = 1;
565 
566 	netsec_write(priv, NETSEC_REG_NRM_RX_RXINT_PKTCNT,
567 		     priv->et_coalesce.rx_max_coalesced_frames);
568 	netsec_write(priv, NETSEC_REG_NRM_RX_RXINT_TMR,
569 		     priv->et_coalesce.rx_coalesce_usecs);
570 	netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_SET, NRM_RX_ST_PKTCNT);
571 	netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_SET, NRM_RX_ST_TMREXP);
572 
573 	return 0;
574 }
575 
576 static u32 netsec_et_get_msglevel(struct net_device *dev)
577 {
578 	struct netsec_priv *priv = netdev_priv(dev);
579 
580 	return priv->msg_enable;
581 }
582 
583 static void netsec_et_set_msglevel(struct net_device *dev, u32 datum)
584 {
585 	struct netsec_priv *priv = netdev_priv(dev);
586 
587 	priv->msg_enable = datum;
588 }
589 
590 static const struct ethtool_ops netsec_ethtool_ops = {
591 	.get_drvinfo		= netsec_et_get_drvinfo,
592 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
593 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
594 	.get_link		= ethtool_op_get_link,
595 	.get_coalesce		= netsec_et_get_coalesce,
596 	.set_coalesce		= netsec_et_set_coalesce,
597 	.get_msglevel		= netsec_et_get_msglevel,
598 	.set_msglevel		= netsec_et_set_msglevel,
599 };
600 
601 /************* NETDEV_OPS FOLLOW *************/
602 
603 
604 static void netsec_set_rx_de(struct netsec_priv *priv,
605 			     struct netsec_desc_ring *dring, u16 idx,
606 			     const struct netsec_desc *desc)
607 {
608 	struct netsec_de *de = dring->vaddr + DESC_SZ * idx;
609 	u32 attr = (1 << NETSEC_RX_PKT_OWN_FIELD) |
610 		   (1 << NETSEC_RX_PKT_FS_FIELD) |
611 		   (1 << NETSEC_RX_PKT_LS_FIELD);
612 
613 	if (idx == DESC_NUM - 1)
614 		attr |= (1 << NETSEC_RX_PKT_LD_FIELD);
615 
616 	de->data_buf_addr_up = upper_32_bits(desc->dma_addr);
617 	de->data_buf_addr_lw = lower_32_bits(desc->dma_addr);
618 	de->buf_len_info = desc->len;
619 	de->attr = attr;
620 	dma_wmb();
621 
622 	dring->desc[idx].dma_addr = desc->dma_addr;
623 	dring->desc[idx].addr = desc->addr;
624 	dring->desc[idx].len = desc->len;
625 }
626 
627 static bool netsec_clean_tx_dring(struct netsec_priv *priv)
628 {
629 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX];
630 	struct netsec_de *entry;
631 	int tail = dring->tail;
632 	unsigned int bytes;
633 	int cnt = 0;
634 
635 	spin_lock(&dring->lock);
636 
637 	bytes = 0;
638 	entry = dring->vaddr + DESC_SZ * tail;
639 
640 	while (!(entry->attr & (1U << NETSEC_TX_SHIFT_OWN_FIELD)) &&
641 	       cnt < DESC_NUM) {
642 		struct netsec_desc *desc;
643 		int eop;
644 
645 		desc = &dring->desc[tail];
646 		eop = (entry->attr >> NETSEC_TX_LAST) & 1;
647 		dma_rmb();
648 
649 		/* if buf_type is either TYPE_NETSEC_SKB or
650 		 * TYPE_NETSEC_XDP_NDO we mapped it
651 		 */
652 		if (desc->buf_type != TYPE_NETSEC_XDP_TX)
653 			dma_unmap_single(priv->dev, desc->dma_addr, desc->len,
654 					 DMA_TO_DEVICE);
655 
656 		if (!eop)
657 			goto next;
658 
659 		if (desc->buf_type == TYPE_NETSEC_SKB) {
660 			bytes += desc->skb->len;
661 			dev_kfree_skb(desc->skb);
662 		} else {
663 			bytes += desc->xdpf->len;
664 			xdp_return_frame(desc->xdpf);
665 		}
666 next:
667 		/* clean up so netsec_uninit_pkt_dring() won't free the skb
668 		 * again
669 		 */
670 		*desc = (struct netsec_desc){};
671 
672 		/* entry->attr is not going to be accessed by the NIC until
673 		 * netsec_set_tx_de() is called. No need for a dma_wmb() here
674 		 */
675 		entry->attr = 1U << NETSEC_TX_SHIFT_OWN_FIELD;
676 		/* move tail ahead */
677 		dring->tail = (tail + 1) % DESC_NUM;
678 
679 		tail = dring->tail;
680 		entry = dring->vaddr + DESC_SZ * tail;
681 		cnt++;
682 	}
683 
684 	spin_unlock(&dring->lock);
685 
686 	if (!cnt)
687 		return false;
688 
689 	/* reading the register clears the irq */
690 	netsec_read(priv, NETSEC_REG_NRM_TX_DONE_PKTCNT);
691 
692 	priv->ndev->stats.tx_packets += cnt;
693 	priv->ndev->stats.tx_bytes += bytes;
694 
695 	netdev_completed_queue(priv->ndev, cnt, bytes);
696 
697 	return true;
698 }
699 
700 static void netsec_process_tx(struct netsec_priv *priv)
701 {
702 	struct net_device *ndev = priv->ndev;
703 	bool cleaned;
704 
705 	cleaned = netsec_clean_tx_dring(priv);
706 
707 	if (cleaned && netif_queue_stopped(ndev)) {
708 		/* Make sure we update the value, anyone stopping the queue
709 		 * after this will read the proper consumer idx
710 		 */
711 		smp_wmb();
712 		netif_wake_queue(ndev);
713 	}
714 }
715 
716 static void *netsec_alloc_rx_data(struct netsec_priv *priv,
717 				  dma_addr_t *dma_handle, u16 *desc_len)
718 
719 {
720 
721 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
722 	enum dma_data_direction dma_dir;
723 	struct page *page;
724 
725 	page = page_pool_dev_alloc_pages(dring->page_pool);
726 	if (!page)
727 		return NULL;
728 
729 	/* We allocate the same buffer length for XDP and non-XDP cases.
730 	 * page_pool API will map the whole page, skip what's needed for
731 	 * network payloads and/or XDP
732 	 */
733 	*dma_handle = page_pool_get_dma_addr(page) + NETSEC_RXBUF_HEADROOM;
734 	/* Make sure the incoming payload fits in the page for XDP and non-XDP
735 	 * cases and reserve enough space for headroom + skb_shared_info
736 	 */
737 	*desc_len = PAGE_SIZE - NETSEC_RX_BUF_NON_DATA;
738 	dma_dir = page_pool_get_dma_dir(dring->page_pool);
739 	dma_sync_single_for_device(priv->dev, *dma_handle, *desc_len, dma_dir);
740 
741 	return page_address(page);
742 }
743 
744 static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num)
745 {
746 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
747 	u16 idx = from;
748 
749 	while (num) {
750 		netsec_set_rx_de(priv, dring, idx, &dring->desc[idx]);
751 		idx++;
752 		if (idx >= DESC_NUM)
753 			idx = 0;
754 		num--;
755 	}
756 }
757 
758 static void netsec_xdp_ring_tx_db(struct netsec_priv *priv, u16 pkts)
759 {
760 	if (likely(pkts))
761 		netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, pkts);
762 }
763 
764 static void netsec_finalize_xdp_rx(struct netsec_priv *priv, u32 xdp_res,
765 				   u16 pkts)
766 {
767 	if (xdp_res & NETSEC_XDP_REDIR)
768 		xdp_do_flush_map();
769 
770 	if (xdp_res & NETSEC_XDP_TX)
771 		netsec_xdp_ring_tx_db(priv, pkts);
772 }
773 
774 static void netsec_set_tx_de(struct netsec_priv *priv,
775 			     struct netsec_desc_ring *dring,
776 			     const struct netsec_tx_pkt_ctrl *tx_ctrl,
777 			     const struct netsec_desc *desc, void *buf)
778 {
779 	int idx = dring->head;
780 	struct netsec_de *de;
781 	u32 attr;
782 
783 	de = dring->vaddr + (DESC_SZ * idx);
784 
785 	attr = (1 << NETSEC_TX_SHIFT_OWN_FIELD) |
786 	       (1 << NETSEC_TX_SHIFT_PT_FIELD) |
787 	       (NETSEC_RING_GMAC << NETSEC_TX_SHIFT_TDRID_FIELD) |
788 	       (1 << NETSEC_TX_SHIFT_FS_FIELD) |
789 	       (1 << NETSEC_TX_LAST) |
790 	       (tx_ctrl->cksum_offload_flag << NETSEC_TX_SHIFT_CO) |
791 	       (tx_ctrl->tcp_seg_offload_flag << NETSEC_TX_SHIFT_SO) |
792 	       (1 << NETSEC_TX_SHIFT_TRS_FIELD);
793 	if (idx == DESC_NUM - 1)
794 		attr |= (1 << NETSEC_TX_SHIFT_LD_FIELD);
795 
796 	de->data_buf_addr_up = upper_32_bits(desc->dma_addr);
797 	de->data_buf_addr_lw = lower_32_bits(desc->dma_addr);
798 	de->buf_len_info = (tx_ctrl->tcp_seg_len << 16) | desc->len;
799 	de->attr = attr;
800 
801 	dring->desc[idx] = *desc;
802 	if (desc->buf_type == TYPE_NETSEC_SKB)
803 		dring->desc[idx].skb = buf;
804 	else if (desc->buf_type == TYPE_NETSEC_XDP_TX ||
805 		 desc->buf_type == TYPE_NETSEC_XDP_NDO)
806 		dring->desc[idx].xdpf = buf;
807 
808 	/* move head ahead */
809 	dring->head = (dring->head + 1) % DESC_NUM;
810 }
811 
812 /* The current driver only supports 1 Txq, this should run under spin_lock() */
813 static u32 netsec_xdp_queue_one(struct netsec_priv *priv,
814 				struct xdp_frame *xdpf, bool is_ndo)
815 
816 {
817 	struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX];
818 	struct page *page = virt_to_page(xdpf->data);
819 	struct netsec_tx_pkt_ctrl tx_ctrl = {};
820 	struct netsec_desc tx_desc;
821 	dma_addr_t dma_handle;
822 	u16 filled;
823 
824 	if (tx_ring->head >= tx_ring->tail)
825 		filled = tx_ring->head - tx_ring->tail;
826 	else
827 		filled = tx_ring->head + DESC_NUM - tx_ring->tail;
828 
829 	if (DESC_NUM - filled <= 1)
830 		return NETSEC_XDP_CONSUMED;
831 
832 	if (is_ndo) {
833 		/* this is for ndo_xdp_xmit, the buffer needs mapping before
834 		 * sending
835 		 */
836 		dma_handle = dma_map_single(priv->dev, xdpf->data, xdpf->len,
837 					    DMA_TO_DEVICE);
838 		if (dma_mapping_error(priv->dev, dma_handle))
839 			return NETSEC_XDP_CONSUMED;
840 		tx_desc.buf_type = TYPE_NETSEC_XDP_NDO;
841 	} else {
842 		/* This is the device Rx buffer from page_pool. No need to remap
843 		 * just sync and send it
844 		 */
845 		struct netsec_desc_ring *rx_ring =
846 			&priv->desc_ring[NETSEC_RING_RX];
847 		enum dma_data_direction dma_dir =
848 			page_pool_get_dma_dir(rx_ring->page_pool);
849 
850 		dma_handle = page_pool_get_dma_addr(page) + xdpf->headroom +
851 			sizeof(*xdpf);
852 		dma_sync_single_for_device(priv->dev, dma_handle, xdpf->len,
853 					   dma_dir);
854 		tx_desc.buf_type = TYPE_NETSEC_XDP_TX;
855 	}
856 
857 	tx_desc.dma_addr = dma_handle;
858 	tx_desc.addr = xdpf->data;
859 	tx_desc.len = xdpf->len;
860 
861 	netdev_sent_queue(priv->ndev, xdpf->len);
862 	netsec_set_tx_de(priv, tx_ring, &tx_ctrl, &tx_desc, xdpf);
863 
864 	return NETSEC_XDP_TX;
865 }
866 
867 static u32 netsec_xdp_xmit_back(struct netsec_priv *priv, struct xdp_buff *xdp)
868 {
869 	struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX];
870 	struct xdp_frame *xdpf = convert_to_xdp_frame(xdp);
871 	u32 ret;
872 
873 	if (unlikely(!xdpf))
874 		return NETSEC_XDP_CONSUMED;
875 
876 	spin_lock(&tx_ring->lock);
877 	ret = netsec_xdp_queue_one(priv, xdpf, false);
878 	spin_unlock(&tx_ring->lock);
879 
880 	return ret;
881 }
882 
883 static u32 netsec_run_xdp(struct netsec_priv *priv, struct bpf_prog *prog,
884 			  struct xdp_buff *xdp)
885 {
886 	u32 ret = NETSEC_XDP_PASS;
887 	int err;
888 	u32 act;
889 
890 	act = bpf_prog_run_xdp(prog, xdp);
891 
892 	switch (act) {
893 	case XDP_PASS:
894 		ret = NETSEC_XDP_PASS;
895 		break;
896 	case XDP_TX:
897 		ret = netsec_xdp_xmit_back(priv, xdp);
898 		if (ret != NETSEC_XDP_TX)
899 			xdp_return_buff(xdp);
900 		break;
901 	case XDP_REDIRECT:
902 		err = xdp_do_redirect(priv->ndev, xdp, prog);
903 		if (!err) {
904 			ret = NETSEC_XDP_REDIR;
905 		} else {
906 			ret = NETSEC_XDP_CONSUMED;
907 			xdp_return_buff(xdp);
908 		}
909 		break;
910 	default:
911 		bpf_warn_invalid_xdp_action(act);
912 		/* fall through */
913 	case XDP_ABORTED:
914 		trace_xdp_exception(priv->ndev, prog, act);
915 		/* fall through -- handle aborts by dropping packet */
916 	case XDP_DROP:
917 		ret = NETSEC_XDP_CONSUMED;
918 		xdp_return_buff(xdp);
919 		break;
920 	}
921 
922 	return ret;
923 }
924 
925 static int netsec_process_rx(struct netsec_priv *priv, int budget)
926 {
927 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
928 	struct net_device *ndev = priv->ndev;
929 	struct netsec_rx_pkt_info rx_info;
930 	enum dma_data_direction dma_dir;
931 	struct bpf_prog *xdp_prog;
932 	struct sk_buff *skb = NULL;
933 	u16 xdp_xmit = 0;
934 	u32 xdp_act = 0;
935 	int done = 0;
936 
937 	rcu_read_lock();
938 	xdp_prog = READ_ONCE(priv->xdp_prog);
939 	dma_dir = page_pool_get_dma_dir(dring->page_pool);
940 
941 	while (done < budget) {
942 		u16 idx = dring->tail;
943 		struct netsec_de *de = dring->vaddr + (DESC_SZ * idx);
944 		struct netsec_desc *desc = &dring->desc[idx];
945 		struct page *page = virt_to_page(desc->addr);
946 		u32 xdp_result = XDP_PASS;
947 		u16 pkt_len, desc_len;
948 		dma_addr_t dma_handle;
949 		struct xdp_buff xdp;
950 		void *buf_addr;
951 
952 		if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD)) {
953 			/* reading the register clears the irq */
954 			netsec_read(priv, NETSEC_REG_NRM_RX_PKTCNT);
955 			break;
956 		}
957 
958 		/* This  barrier is needed to keep us from reading
959 		 * any other fields out of the netsec_de until we have
960 		 * verified the descriptor has been written back
961 		 */
962 		dma_rmb();
963 		done++;
964 
965 		pkt_len = de->buf_len_info >> 16;
966 		rx_info.err_code = (de->attr >> NETSEC_RX_PKT_ERR_FIELD) &
967 			NETSEC_RX_PKT_ERR_MASK;
968 		rx_info.err_flag = (de->attr >> NETSEC_RX_PKT_ER_FIELD) & 1;
969 		if (rx_info.err_flag) {
970 			netif_err(priv, drv, priv->ndev,
971 				  "%s: rx fail err(%d)\n", __func__,
972 				  rx_info.err_code);
973 			ndev->stats.rx_dropped++;
974 			dring->tail = (dring->tail + 1) % DESC_NUM;
975 			/* reuse buffer page frag */
976 			netsec_rx_fill(priv, idx, 1);
977 			continue;
978 		}
979 		rx_info.rx_cksum_result =
980 			(de->attr >> NETSEC_RX_PKT_CO_FIELD) & 3;
981 
982 		/* allocate a fresh buffer and map it to the hardware.
983 		 * This will eventually replace the old buffer in the hardware
984 		 */
985 		buf_addr = netsec_alloc_rx_data(priv, &dma_handle, &desc_len);
986 
987 		if (unlikely(!buf_addr))
988 			break;
989 
990 		dma_sync_single_for_cpu(priv->dev, desc->dma_addr, pkt_len,
991 					dma_dir);
992 		prefetch(desc->addr);
993 
994 		xdp.data_hard_start = desc->addr;
995 		xdp.data = desc->addr + NETSEC_RXBUF_HEADROOM;
996 		xdp_set_data_meta_invalid(&xdp);
997 		xdp.data_end = xdp.data + pkt_len;
998 		xdp.rxq = &dring->xdp_rxq;
999 
1000 		if (xdp_prog) {
1001 			xdp_result = netsec_run_xdp(priv, xdp_prog, &xdp);
1002 			if (xdp_result != NETSEC_XDP_PASS) {
1003 				xdp_act |= xdp_result;
1004 				if (xdp_result == NETSEC_XDP_TX)
1005 					xdp_xmit++;
1006 				goto next;
1007 			}
1008 		}
1009 		skb = build_skb(desc->addr, desc->len + NETSEC_RX_BUF_NON_DATA);
1010 
1011 		if (unlikely(!skb)) {
1012 			/* If skb fails recycle_direct will either unmap and
1013 			 * free the page or refill the cache depending on the
1014 			 * cache state. Since we paid the allocation cost if
1015 			 * building an skb fails try to put the page into cache
1016 			 */
1017 			page_pool_recycle_direct(dring->page_pool, page);
1018 			netif_err(priv, drv, priv->ndev,
1019 				  "rx failed to build skb\n");
1020 			break;
1021 		}
1022 		page_pool_release_page(dring->page_pool, page);
1023 
1024 		skb_reserve(skb, xdp.data - xdp.data_hard_start);
1025 		skb_put(skb, xdp.data_end - xdp.data);
1026 		skb->protocol = eth_type_trans(skb, priv->ndev);
1027 
1028 		if (priv->rx_cksum_offload_flag &&
1029 		    rx_info.rx_cksum_result == NETSEC_RX_CKSUM_OK)
1030 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1031 
1032 next:
1033 		if ((skb && napi_gro_receive(&priv->napi, skb) != GRO_DROP) ||
1034 		    xdp_result) {
1035 			ndev->stats.rx_packets++;
1036 			ndev->stats.rx_bytes += xdp.data_end - xdp.data;
1037 		}
1038 
1039 		/* Update the descriptor with fresh buffers */
1040 		desc->len = desc_len;
1041 		desc->dma_addr = dma_handle;
1042 		desc->addr = buf_addr;
1043 
1044 		netsec_rx_fill(priv, idx, 1);
1045 		dring->tail = (dring->tail + 1) % DESC_NUM;
1046 	}
1047 	netsec_finalize_xdp_rx(priv, xdp_act, xdp_xmit);
1048 
1049 	rcu_read_unlock();
1050 
1051 	return done;
1052 }
1053 
1054 static int netsec_napi_poll(struct napi_struct *napi, int budget)
1055 {
1056 	struct netsec_priv *priv;
1057 	int done;
1058 
1059 	priv = container_of(napi, struct netsec_priv, napi);
1060 
1061 	netsec_process_tx(priv);
1062 	done = netsec_process_rx(priv, budget);
1063 
1064 	if (done < budget && napi_complete_done(napi, done)) {
1065 		unsigned long flags;
1066 
1067 		spin_lock_irqsave(&priv->reglock, flags);
1068 		netsec_write(priv, NETSEC_REG_INTEN_SET,
1069 			     NETSEC_IRQ_RX | NETSEC_IRQ_TX);
1070 		spin_unlock_irqrestore(&priv->reglock, flags);
1071 	}
1072 
1073 	return done;
1074 }
1075 
1076 
1077 static int netsec_desc_used(struct netsec_desc_ring *dring)
1078 {
1079 	int used;
1080 
1081 	if (dring->head >= dring->tail)
1082 		used = dring->head - dring->tail;
1083 	else
1084 		used = dring->head + DESC_NUM - dring->tail;
1085 
1086 	return used;
1087 }
1088 
1089 static int netsec_check_stop_tx(struct netsec_priv *priv, int used)
1090 {
1091 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX];
1092 
1093 	/* keep tail from touching the queue */
1094 	if (DESC_NUM - used < 2) {
1095 		netif_stop_queue(priv->ndev);
1096 
1097 		/* Make sure we read the updated value in case
1098 		 * descriptors got freed
1099 		 */
1100 		smp_rmb();
1101 
1102 		used = netsec_desc_used(dring);
1103 		if (DESC_NUM - used < 2)
1104 			return NETDEV_TX_BUSY;
1105 
1106 		netif_wake_queue(priv->ndev);
1107 	}
1108 
1109 	return 0;
1110 }
1111 
1112 static netdev_tx_t netsec_netdev_start_xmit(struct sk_buff *skb,
1113 					    struct net_device *ndev)
1114 {
1115 	struct netsec_priv *priv = netdev_priv(ndev);
1116 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX];
1117 	struct netsec_tx_pkt_ctrl tx_ctrl = {};
1118 	struct netsec_desc tx_desc;
1119 	u16 tso_seg_len = 0;
1120 	int filled;
1121 
1122 	spin_lock_bh(&dring->lock);
1123 	filled = netsec_desc_used(dring);
1124 	if (netsec_check_stop_tx(priv, filled)) {
1125 		spin_unlock_bh(&dring->lock);
1126 		net_warn_ratelimited("%s %s Tx queue full\n",
1127 				     dev_name(priv->dev), ndev->name);
1128 		return NETDEV_TX_BUSY;
1129 	}
1130 
1131 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1132 		tx_ctrl.cksum_offload_flag = true;
1133 
1134 	if (skb_is_gso(skb))
1135 		tso_seg_len = skb_shinfo(skb)->gso_size;
1136 
1137 	if (tso_seg_len > 0) {
1138 		if (skb->protocol == htons(ETH_P_IP)) {
1139 			ip_hdr(skb)->tot_len = 0;
1140 			tcp_hdr(skb)->check =
1141 				~tcp_v4_check(0, ip_hdr(skb)->saddr,
1142 					      ip_hdr(skb)->daddr, 0);
1143 		} else {
1144 			ipv6_hdr(skb)->payload_len = 0;
1145 			tcp_hdr(skb)->check =
1146 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1147 						 &ipv6_hdr(skb)->daddr,
1148 						 0, IPPROTO_TCP, 0);
1149 		}
1150 
1151 		tx_ctrl.tcp_seg_offload_flag = true;
1152 		tx_ctrl.tcp_seg_len = tso_seg_len;
1153 	}
1154 
1155 	tx_desc.dma_addr = dma_map_single(priv->dev, skb->data,
1156 					  skb_headlen(skb), DMA_TO_DEVICE);
1157 	if (dma_mapping_error(priv->dev, tx_desc.dma_addr)) {
1158 		spin_unlock_bh(&dring->lock);
1159 		netif_err(priv, drv, priv->ndev,
1160 			  "%s: DMA mapping failed\n", __func__);
1161 		ndev->stats.tx_dropped++;
1162 		dev_kfree_skb_any(skb);
1163 		return NETDEV_TX_OK;
1164 	}
1165 	tx_desc.addr = skb->data;
1166 	tx_desc.len = skb_headlen(skb);
1167 	tx_desc.buf_type = TYPE_NETSEC_SKB;
1168 
1169 	skb_tx_timestamp(skb);
1170 	netdev_sent_queue(priv->ndev, skb->len);
1171 
1172 	netsec_set_tx_de(priv, dring, &tx_ctrl, &tx_desc, skb);
1173 	spin_unlock_bh(&dring->lock);
1174 	netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, 1); /* submit another tx */
1175 
1176 	return NETDEV_TX_OK;
1177 }
1178 
1179 static void netsec_uninit_pkt_dring(struct netsec_priv *priv, int id)
1180 {
1181 	struct netsec_desc_ring *dring = &priv->desc_ring[id];
1182 	struct netsec_desc *desc;
1183 	u16 idx;
1184 
1185 	if (!dring->vaddr || !dring->desc)
1186 		return;
1187 	for (idx = 0; idx < DESC_NUM; idx++) {
1188 		desc = &dring->desc[idx];
1189 		if (!desc->addr)
1190 			continue;
1191 
1192 		if (id == NETSEC_RING_RX) {
1193 			struct page *page = virt_to_page(desc->addr);
1194 
1195 			page_pool_put_page(dring->page_pool, page, false);
1196 		} else if (id == NETSEC_RING_TX) {
1197 			dma_unmap_single(priv->dev, desc->dma_addr, desc->len,
1198 					 DMA_TO_DEVICE);
1199 			dev_kfree_skb(desc->skb);
1200 		}
1201 	}
1202 
1203 	/* Rx is currently using page_pool */
1204 	if (id == NETSEC_RING_RX) {
1205 		if (xdp_rxq_info_is_reg(&dring->xdp_rxq))
1206 			xdp_rxq_info_unreg(&dring->xdp_rxq);
1207 		page_pool_destroy(dring->page_pool);
1208 	}
1209 
1210 	memset(dring->desc, 0, sizeof(struct netsec_desc) * DESC_NUM);
1211 	memset(dring->vaddr, 0, DESC_SZ * DESC_NUM);
1212 
1213 	dring->head = 0;
1214 	dring->tail = 0;
1215 
1216 	if (id == NETSEC_RING_TX)
1217 		netdev_reset_queue(priv->ndev);
1218 }
1219 
1220 static void netsec_free_dring(struct netsec_priv *priv, int id)
1221 {
1222 	struct netsec_desc_ring *dring = &priv->desc_ring[id];
1223 
1224 	if (dring->vaddr) {
1225 		dma_free_coherent(priv->dev, DESC_SZ * DESC_NUM,
1226 				  dring->vaddr, dring->desc_dma);
1227 		dring->vaddr = NULL;
1228 	}
1229 
1230 	kfree(dring->desc);
1231 	dring->desc = NULL;
1232 }
1233 
1234 static int netsec_alloc_dring(struct netsec_priv *priv, enum ring_id id)
1235 {
1236 	struct netsec_desc_ring *dring = &priv->desc_ring[id];
1237 
1238 	dring->vaddr = dma_alloc_coherent(priv->dev, DESC_SZ * DESC_NUM,
1239 					  &dring->desc_dma, GFP_KERNEL);
1240 	if (!dring->vaddr)
1241 		goto err;
1242 
1243 	dring->desc = kcalloc(DESC_NUM, sizeof(*dring->desc), GFP_KERNEL);
1244 	if (!dring->desc)
1245 		goto err;
1246 
1247 	return 0;
1248 err:
1249 	netsec_free_dring(priv, id);
1250 
1251 	return -ENOMEM;
1252 }
1253 
1254 static void netsec_setup_tx_dring(struct netsec_priv *priv)
1255 {
1256 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX];
1257 	int i;
1258 
1259 	for (i = 0; i < DESC_NUM; i++) {
1260 		struct netsec_de *de;
1261 
1262 		de = dring->vaddr + (DESC_SZ * i);
1263 		/* de->attr is not going to be accessed by the NIC
1264 		 * until netsec_set_tx_de() is called.
1265 		 * No need for a dma_wmb() here
1266 		 */
1267 		de->attr = 1U << NETSEC_TX_SHIFT_OWN_FIELD;
1268 	}
1269 }
1270 
1271 static int netsec_setup_rx_dring(struct netsec_priv *priv)
1272 {
1273 	struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
1274 	struct bpf_prog *xdp_prog = READ_ONCE(priv->xdp_prog);
1275 	struct page_pool_params pp_params = { 0 };
1276 	int i, err;
1277 
1278 	pp_params.order = 0;
1279 	/* internal DMA mapping in page_pool */
1280 	pp_params.flags = PP_FLAG_DMA_MAP;
1281 	pp_params.pool_size = DESC_NUM;
1282 	pp_params.nid = cpu_to_node(0);
1283 	pp_params.dev = priv->dev;
1284 	pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1285 
1286 	dring->page_pool = page_pool_create(&pp_params);
1287 	if (IS_ERR(dring->page_pool)) {
1288 		err = PTR_ERR(dring->page_pool);
1289 		dring->page_pool = NULL;
1290 		goto err_out;
1291 	}
1292 
1293 	err = xdp_rxq_info_reg(&dring->xdp_rxq, priv->ndev, 0);
1294 	if (err)
1295 		goto err_out;
1296 
1297 	err = xdp_rxq_info_reg_mem_model(&dring->xdp_rxq, MEM_TYPE_PAGE_POOL,
1298 					 dring->page_pool);
1299 	if (err)
1300 		goto err_out;
1301 
1302 	for (i = 0; i < DESC_NUM; i++) {
1303 		struct netsec_desc *desc = &dring->desc[i];
1304 		dma_addr_t dma_handle;
1305 		void *buf;
1306 		u16 len;
1307 
1308 		buf = netsec_alloc_rx_data(priv, &dma_handle, &len);
1309 
1310 		if (!buf) {
1311 			err = -ENOMEM;
1312 			goto err_out;
1313 		}
1314 		desc->dma_addr = dma_handle;
1315 		desc->addr = buf;
1316 		desc->len = len;
1317 	}
1318 
1319 	netsec_rx_fill(priv, 0, DESC_NUM);
1320 
1321 	return 0;
1322 
1323 err_out:
1324 	netsec_uninit_pkt_dring(priv, NETSEC_RING_RX);
1325 	return err;
1326 }
1327 
1328 static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg,
1329 					   u32 addr_h, u32 addr_l, u32 size)
1330 {
1331 	u64 base = (u64)addr_h << 32 | addr_l;
1332 	void __iomem *ucode;
1333 	u32 i;
1334 
1335 	ucode = ioremap(base, size * sizeof(u32));
1336 	if (!ucode)
1337 		return -ENOMEM;
1338 
1339 	for (i = 0; i < size; i++)
1340 		netsec_write(priv, reg, readl(ucode + i * 4));
1341 
1342 	iounmap(ucode);
1343 	return 0;
1344 }
1345 
1346 static int netsec_netdev_load_microcode(struct netsec_priv *priv)
1347 {
1348 	u32 addr_h, addr_l, size;
1349 	int err;
1350 
1351 	addr_h = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_ADDRESS_H);
1352 	addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_ADDRESS_L);
1353 	size = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_SIZE);
1354 	err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_HM_CMD_BUF,
1355 					      addr_h, addr_l, size);
1356 	if (err)
1357 		return err;
1358 
1359 	addr_h = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_ADDRESS_H);
1360 	addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_ADDRESS_L);
1361 	size = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_SIZE);
1362 	err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_MH_CMD_BUF,
1363 					      addr_h, addr_l, size);
1364 	if (err)
1365 		return err;
1366 
1367 	addr_h = 0;
1368 	addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_PKT_ME_ADDRESS);
1369 	size = readl(priv->eeprom_base + NETSEC_EEPROM_PKT_ME_SIZE);
1370 	err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_PKT_CMD_BUF,
1371 					      addr_h, addr_l, size);
1372 	if (err)
1373 		return err;
1374 
1375 	return 0;
1376 }
1377 
1378 static int netsec_reset_hardware(struct netsec_priv *priv,
1379 				 bool load_ucode)
1380 {
1381 	u32 value;
1382 	int err;
1383 
1384 	/* stop DMA engines */
1385 	if (!netsec_read(priv, NETSEC_REG_ADDR_DIS_CORE)) {
1386 		netsec_write(priv, NETSEC_REG_DMA_HM_CTRL,
1387 			     NETSEC_DMA_CTRL_REG_STOP);
1388 		netsec_write(priv, NETSEC_REG_DMA_MH_CTRL,
1389 			     NETSEC_DMA_CTRL_REG_STOP);
1390 
1391 		while (netsec_read(priv, NETSEC_REG_DMA_HM_CTRL) &
1392 		       NETSEC_DMA_CTRL_REG_STOP)
1393 			cpu_relax();
1394 
1395 		while (netsec_read(priv, NETSEC_REG_DMA_MH_CTRL) &
1396 		       NETSEC_DMA_CTRL_REG_STOP)
1397 			cpu_relax();
1398 	}
1399 
1400 	netsec_write(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RESET);
1401 	netsec_write(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RUN);
1402 	netsec_write(priv, NETSEC_REG_COM_INIT, NETSEC_COM_INIT_REG_ALL);
1403 
1404 	while (netsec_read(priv, NETSEC_REG_COM_INIT) != 0)
1405 		cpu_relax();
1406 
1407 	/* set desc_start addr */
1408 	netsec_write(priv, NETSEC_REG_NRM_RX_DESC_START_UP,
1409 		     upper_32_bits(priv->desc_ring[NETSEC_RING_RX].desc_dma));
1410 	netsec_write(priv, NETSEC_REG_NRM_RX_DESC_START_LW,
1411 		     lower_32_bits(priv->desc_ring[NETSEC_RING_RX].desc_dma));
1412 
1413 	netsec_write(priv, NETSEC_REG_NRM_TX_DESC_START_UP,
1414 		     upper_32_bits(priv->desc_ring[NETSEC_RING_TX].desc_dma));
1415 	netsec_write(priv, NETSEC_REG_NRM_TX_DESC_START_LW,
1416 		     lower_32_bits(priv->desc_ring[NETSEC_RING_TX].desc_dma));
1417 
1418 	/* set normal tx dring ring config */
1419 	netsec_write(priv, NETSEC_REG_NRM_TX_CONFIG,
1420 		     1 << NETSEC_REG_DESC_ENDIAN);
1421 	netsec_write(priv, NETSEC_REG_NRM_RX_CONFIG,
1422 		     1 << NETSEC_REG_DESC_ENDIAN);
1423 
1424 	if (load_ucode) {
1425 		err = netsec_netdev_load_microcode(priv);
1426 		if (err) {
1427 			netif_err(priv, probe, priv->ndev,
1428 				  "%s: failed to load microcode (%d)\n",
1429 				  __func__, err);
1430 			return err;
1431 		}
1432 	}
1433 
1434 	/* start DMA engines */
1435 	netsec_write(priv, NETSEC_REG_DMA_TMR_CTRL, priv->freq / 1000000 - 1);
1436 	netsec_write(priv, NETSEC_REG_ADDR_DIS_CORE, 0);
1437 
1438 	usleep_range(1000, 2000);
1439 
1440 	if (!(netsec_read(priv, NETSEC_REG_TOP_STATUS) &
1441 	      NETSEC_TOP_IRQ_REG_CODE_LOAD_END)) {
1442 		netif_err(priv, probe, priv->ndev,
1443 			  "microengine start failed\n");
1444 		return -ENXIO;
1445 	}
1446 	netsec_write(priv, NETSEC_REG_TOP_STATUS,
1447 		     NETSEC_TOP_IRQ_REG_CODE_LOAD_END);
1448 
1449 	value = NETSEC_PKT_CTRL_REG_MODE_NRM;
1450 	if (priv->ndev->mtu > ETH_DATA_LEN)
1451 		value |= NETSEC_PKT_CTRL_REG_EN_JUMBO;
1452 
1453 	/* change to normal mode */
1454 	netsec_write(priv, NETSEC_REG_DMA_MH_CTRL, MH_CTRL__MODE_TRANS);
1455 	netsec_write(priv, NETSEC_REG_PKT_CTRL, value);
1456 
1457 	while ((netsec_read(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS) &
1458 		NETSEC_MODE_TRANS_COMP_IRQ_T2N) == 0)
1459 		cpu_relax();
1460 
1461 	/* clear any pending EMPTY/ERR irq status */
1462 	netsec_write(priv, NETSEC_REG_NRM_TX_STATUS, ~0);
1463 
1464 	/* Disable TX & RX intr */
1465 	netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0);
1466 
1467 	return 0;
1468 }
1469 
1470 static int netsec_start_gmac(struct netsec_priv *priv)
1471 {
1472 	struct phy_device *phydev = priv->ndev->phydev;
1473 	u32 value = 0;
1474 	int ret;
1475 
1476 	if (phydev->speed != SPEED_1000)
1477 		value = (NETSEC_GMAC_MCR_REG_CST |
1478 			 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON);
1479 
1480 	if (netsec_mac_write(priv, GMAC_REG_MCR, value))
1481 		return -ETIMEDOUT;
1482 	if (netsec_mac_write(priv, GMAC_REG_BMR,
1483 			     NETSEC_GMAC_BMR_REG_RESET))
1484 		return -ETIMEDOUT;
1485 
1486 	/* Wait soft reset */
1487 	usleep_range(1000, 5000);
1488 
1489 	ret = netsec_mac_read(priv, GMAC_REG_BMR, &value);
1490 	if (ret)
1491 		return ret;
1492 	if (value & NETSEC_GMAC_BMR_REG_SWR)
1493 		return -EAGAIN;
1494 
1495 	netsec_write(priv, MAC_REG_DESC_SOFT_RST, 1);
1496 	if (netsec_wait_while_busy(priv, MAC_REG_DESC_SOFT_RST, 1))
1497 		return -ETIMEDOUT;
1498 
1499 	netsec_write(priv, MAC_REG_DESC_INIT, 1);
1500 	if (netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1))
1501 		return -ETIMEDOUT;
1502 
1503 	if (netsec_mac_write(priv, GMAC_REG_BMR,
1504 			     NETSEC_GMAC_BMR_REG_COMMON))
1505 		return -ETIMEDOUT;
1506 	if (netsec_mac_write(priv, GMAC_REG_RDLAR,
1507 			     NETSEC_GMAC_RDLAR_REG_COMMON))
1508 		return -ETIMEDOUT;
1509 	if (netsec_mac_write(priv, GMAC_REG_TDLAR,
1510 			     NETSEC_GMAC_TDLAR_REG_COMMON))
1511 		return -ETIMEDOUT;
1512 	if (netsec_mac_write(priv, GMAC_REG_MFFR, 0x80000001))
1513 		return -ETIMEDOUT;
1514 
1515 	ret = netsec_mac_update_to_phy_state(priv);
1516 	if (ret)
1517 		return ret;
1518 
1519 	ret = netsec_mac_read(priv, GMAC_REG_OMR, &value);
1520 	if (ret)
1521 		return ret;
1522 
1523 	value |= NETSEC_GMAC_OMR_REG_SR;
1524 	value |= NETSEC_GMAC_OMR_REG_ST;
1525 
1526 	netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0);
1527 	netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0);
1528 
1529 	netsec_et_set_coalesce(priv->ndev, &priv->et_coalesce);
1530 
1531 	if (netsec_mac_write(priv, GMAC_REG_OMR, value))
1532 		return -ETIMEDOUT;
1533 
1534 	return 0;
1535 }
1536 
1537 static int netsec_stop_gmac(struct netsec_priv *priv)
1538 {
1539 	u32 value;
1540 	int ret;
1541 
1542 	ret = netsec_mac_read(priv, GMAC_REG_OMR, &value);
1543 	if (ret)
1544 		return ret;
1545 	value &= ~NETSEC_GMAC_OMR_REG_SR;
1546 	value &= ~NETSEC_GMAC_OMR_REG_ST;
1547 
1548 	/* disable all interrupts */
1549 	netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0);
1550 	netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0);
1551 
1552 	return netsec_mac_write(priv, GMAC_REG_OMR, value);
1553 }
1554 
1555 static void netsec_phy_adjust_link(struct net_device *ndev)
1556 {
1557 	struct netsec_priv *priv = netdev_priv(ndev);
1558 
1559 	if (ndev->phydev->link)
1560 		netsec_start_gmac(priv);
1561 	else
1562 		netsec_stop_gmac(priv);
1563 
1564 	phy_print_status(ndev->phydev);
1565 }
1566 
1567 static irqreturn_t netsec_irq_handler(int irq, void *dev_id)
1568 {
1569 	struct netsec_priv *priv = dev_id;
1570 	u32 val, status = netsec_read(priv, NETSEC_REG_TOP_STATUS);
1571 	unsigned long flags;
1572 
1573 	/* Disable interrupts */
1574 	if (status & NETSEC_IRQ_TX) {
1575 		val = netsec_read(priv, NETSEC_REG_NRM_TX_STATUS);
1576 		netsec_write(priv, NETSEC_REG_NRM_TX_STATUS, val);
1577 	}
1578 	if (status & NETSEC_IRQ_RX) {
1579 		val = netsec_read(priv, NETSEC_REG_NRM_RX_STATUS);
1580 		netsec_write(priv, NETSEC_REG_NRM_RX_STATUS, val);
1581 	}
1582 
1583 	spin_lock_irqsave(&priv->reglock, flags);
1584 	netsec_write(priv, NETSEC_REG_INTEN_CLR, NETSEC_IRQ_RX | NETSEC_IRQ_TX);
1585 	spin_unlock_irqrestore(&priv->reglock, flags);
1586 
1587 	napi_schedule(&priv->napi);
1588 
1589 	return IRQ_HANDLED;
1590 }
1591 
1592 static int netsec_netdev_open(struct net_device *ndev)
1593 {
1594 	struct netsec_priv *priv = netdev_priv(ndev);
1595 	int ret;
1596 
1597 	pm_runtime_get_sync(priv->dev);
1598 
1599 	netsec_setup_tx_dring(priv);
1600 	ret = netsec_setup_rx_dring(priv);
1601 	if (ret) {
1602 		netif_err(priv, probe, priv->ndev,
1603 			  "%s: fail setup ring\n", __func__);
1604 		goto err1;
1605 	}
1606 
1607 	ret = request_irq(priv->ndev->irq, netsec_irq_handler,
1608 			  IRQF_SHARED, "netsec", priv);
1609 	if (ret) {
1610 		netif_err(priv, drv, priv->ndev, "request_irq failed\n");
1611 		goto err2;
1612 	}
1613 
1614 	if (dev_of_node(priv->dev)) {
1615 		if (!of_phy_connect(priv->ndev, priv->phy_np,
1616 				    netsec_phy_adjust_link, 0,
1617 				    priv->phy_interface)) {
1618 			netif_err(priv, link, priv->ndev, "missing PHY\n");
1619 			ret = -ENODEV;
1620 			goto err3;
1621 		}
1622 	} else {
1623 		ret = phy_connect_direct(priv->ndev, priv->phydev,
1624 					 netsec_phy_adjust_link,
1625 					 priv->phy_interface);
1626 		if (ret) {
1627 			netif_err(priv, link, priv->ndev,
1628 				  "phy_connect_direct() failed (%d)\n", ret);
1629 			goto err3;
1630 		}
1631 	}
1632 
1633 	phy_start(ndev->phydev);
1634 
1635 	netsec_start_gmac(priv);
1636 	napi_enable(&priv->napi);
1637 	netif_start_queue(ndev);
1638 
1639 	/* Enable TX+RX intr. */
1640 	netsec_write(priv, NETSEC_REG_INTEN_SET, NETSEC_IRQ_RX | NETSEC_IRQ_TX);
1641 
1642 	return 0;
1643 err3:
1644 	free_irq(priv->ndev->irq, priv);
1645 err2:
1646 	netsec_uninit_pkt_dring(priv, NETSEC_RING_RX);
1647 err1:
1648 	pm_runtime_put_sync(priv->dev);
1649 	return ret;
1650 }
1651 
1652 static int netsec_netdev_stop(struct net_device *ndev)
1653 {
1654 	int ret;
1655 	struct netsec_priv *priv = netdev_priv(ndev);
1656 
1657 	netif_stop_queue(priv->ndev);
1658 	dma_wmb();
1659 
1660 	napi_disable(&priv->napi);
1661 
1662 	netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0);
1663 	netsec_stop_gmac(priv);
1664 
1665 	free_irq(priv->ndev->irq, priv);
1666 
1667 	netsec_uninit_pkt_dring(priv, NETSEC_RING_TX);
1668 	netsec_uninit_pkt_dring(priv, NETSEC_RING_RX);
1669 
1670 	phy_stop(ndev->phydev);
1671 	phy_disconnect(ndev->phydev);
1672 
1673 	ret = netsec_reset_hardware(priv, false);
1674 
1675 	pm_runtime_put_sync(priv->dev);
1676 
1677 	return ret;
1678 }
1679 
1680 static int netsec_netdev_init(struct net_device *ndev)
1681 {
1682 	struct netsec_priv *priv = netdev_priv(ndev);
1683 	int ret;
1684 	u16 data;
1685 
1686 	BUILD_BUG_ON_NOT_POWER_OF_2(DESC_NUM);
1687 
1688 	ret = netsec_alloc_dring(priv, NETSEC_RING_TX);
1689 	if (ret)
1690 		return ret;
1691 
1692 	ret = netsec_alloc_dring(priv, NETSEC_RING_RX);
1693 	if (ret)
1694 		goto err1;
1695 
1696 	/* set phy power down */
1697 	data = netsec_phy_read(priv->mii_bus, priv->phy_addr, MII_BMCR) |
1698 		BMCR_PDOWN;
1699 	netsec_phy_write(priv->mii_bus, priv->phy_addr, MII_BMCR, data);
1700 
1701 	ret = netsec_reset_hardware(priv, true);
1702 	if (ret)
1703 		goto err2;
1704 
1705 	spin_lock_init(&priv->desc_ring[NETSEC_RING_TX].lock);
1706 	spin_lock_init(&priv->desc_ring[NETSEC_RING_RX].lock);
1707 
1708 	return 0;
1709 err2:
1710 	netsec_free_dring(priv, NETSEC_RING_RX);
1711 err1:
1712 	netsec_free_dring(priv, NETSEC_RING_TX);
1713 	return ret;
1714 }
1715 
1716 static void netsec_netdev_uninit(struct net_device *ndev)
1717 {
1718 	struct netsec_priv *priv = netdev_priv(ndev);
1719 
1720 	netsec_free_dring(priv, NETSEC_RING_RX);
1721 	netsec_free_dring(priv, NETSEC_RING_TX);
1722 }
1723 
1724 static int netsec_netdev_set_features(struct net_device *ndev,
1725 				      netdev_features_t features)
1726 {
1727 	struct netsec_priv *priv = netdev_priv(ndev);
1728 
1729 	priv->rx_cksum_offload_flag = !!(features & NETIF_F_RXCSUM);
1730 
1731 	return 0;
1732 }
1733 
1734 static int netsec_netdev_ioctl(struct net_device *ndev, struct ifreq *ifr,
1735 			       int cmd)
1736 {
1737 	return phy_mii_ioctl(ndev->phydev, ifr, cmd);
1738 }
1739 
1740 static int netsec_xdp_xmit(struct net_device *ndev, int n,
1741 			   struct xdp_frame **frames, u32 flags)
1742 {
1743 	struct netsec_priv *priv = netdev_priv(ndev);
1744 	struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX];
1745 	int drops = 0;
1746 	int i;
1747 
1748 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1749 		return -EINVAL;
1750 
1751 	spin_lock(&tx_ring->lock);
1752 	for (i = 0; i < n; i++) {
1753 		struct xdp_frame *xdpf = frames[i];
1754 		int err;
1755 
1756 		err = netsec_xdp_queue_one(priv, xdpf, true);
1757 		if (err != NETSEC_XDP_TX) {
1758 			xdp_return_frame_rx_napi(xdpf);
1759 			drops++;
1760 		} else {
1761 			tx_ring->xdp_xmit++;
1762 		}
1763 	}
1764 	spin_unlock(&tx_ring->lock);
1765 
1766 	if (unlikely(flags & XDP_XMIT_FLUSH)) {
1767 		netsec_xdp_ring_tx_db(priv, tx_ring->xdp_xmit);
1768 		tx_ring->xdp_xmit = 0;
1769 	}
1770 
1771 	return n - drops;
1772 }
1773 
1774 static int netsec_xdp_setup(struct netsec_priv *priv, struct bpf_prog *prog,
1775 			    struct netlink_ext_ack *extack)
1776 {
1777 	struct net_device *dev = priv->ndev;
1778 	struct bpf_prog *old_prog;
1779 
1780 	/* For now just support only the usual MTU sized frames */
1781 	if (prog && dev->mtu > 1500) {
1782 		NL_SET_ERR_MSG_MOD(extack, "Jumbo frames not supported on XDP");
1783 		return -EOPNOTSUPP;
1784 	}
1785 
1786 	if (netif_running(dev))
1787 		netsec_netdev_stop(dev);
1788 
1789 	/* Detach old prog, if any */
1790 	old_prog = xchg(&priv->xdp_prog, prog);
1791 	if (old_prog)
1792 		bpf_prog_put(old_prog);
1793 
1794 	if (netif_running(dev))
1795 		netsec_netdev_open(dev);
1796 
1797 	return 0;
1798 }
1799 
1800 static int netsec_xdp(struct net_device *ndev, struct netdev_bpf *xdp)
1801 {
1802 	struct netsec_priv *priv = netdev_priv(ndev);
1803 
1804 	switch (xdp->command) {
1805 	case XDP_SETUP_PROG:
1806 		return netsec_xdp_setup(priv, xdp->prog, xdp->extack);
1807 	case XDP_QUERY_PROG:
1808 		xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1809 		return 0;
1810 	default:
1811 		return -EINVAL;
1812 	}
1813 }
1814 
1815 static const struct net_device_ops netsec_netdev_ops = {
1816 	.ndo_init		= netsec_netdev_init,
1817 	.ndo_uninit		= netsec_netdev_uninit,
1818 	.ndo_open		= netsec_netdev_open,
1819 	.ndo_stop		= netsec_netdev_stop,
1820 	.ndo_start_xmit		= netsec_netdev_start_xmit,
1821 	.ndo_set_features	= netsec_netdev_set_features,
1822 	.ndo_set_mac_address    = eth_mac_addr,
1823 	.ndo_validate_addr	= eth_validate_addr,
1824 	.ndo_do_ioctl		= netsec_netdev_ioctl,
1825 	.ndo_xdp_xmit		= netsec_xdp_xmit,
1826 	.ndo_bpf		= netsec_xdp,
1827 };
1828 
1829 static int netsec_of_probe(struct platform_device *pdev,
1830 			   struct netsec_priv *priv, u32 *phy_addr)
1831 {
1832 	priv->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
1833 	if (!priv->phy_np) {
1834 		dev_err(&pdev->dev, "missing required property 'phy-handle'\n");
1835 		return -EINVAL;
1836 	}
1837 
1838 	*phy_addr = of_mdio_parse_addr(&pdev->dev, priv->phy_np);
1839 
1840 	priv->clk = devm_clk_get(&pdev->dev, NULL); /* get by 'phy_ref_clk' */
1841 	if (IS_ERR(priv->clk)) {
1842 		dev_err(&pdev->dev, "phy_ref_clk not found\n");
1843 		return PTR_ERR(priv->clk);
1844 	}
1845 	priv->freq = clk_get_rate(priv->clk);
1846 
1847 	return 0;
1848 }
1849 
1850 static int netsec_acpi_probe(struct platform_device *pdev,
1851 			     struct netsec_priv *priv, u32 *phy_addr)
1852 {
1853 	int ret;
1854 
1855 	if (!IS_ENABLED(CONFIG_ACPI))
1856 		return -ENODEV;
1857 
1858 	ret = device_property_read_u32(&pdev->dev, "phy-channel", phy_addr);
1859 	if (ret) {
1860 		dev_err(&pdev->dev,
1861 			"missing required property 'phy-channel'\n");
1862 		return ret;
1863 	}
1864 
1865 	ret = device_property_read_u32(&pdev->dev,
1866 				       "socionext,phy-clock-frequency",
1867 				       &priv->freq);
1868 	if (ret)
1869 		dev_err(&pdev->dev,
1870 			"missing required property 'socionext,phy-clock-frequency'\n");
1871 	return ret;
1872 }
1873 
1874 static void netsec_unregister_mdio(struct netsec_priv *priv)
1875 {
1876 	struct phy_device *phydev = priv->phydev;
1877 
1878 	if (!dev_of_node(priv->dev) && phydev) {
1879 		phy_device_remove(phydev);
1880 		phy_device_free(phydev);
1881 	}
1882 
1883 	mdiobus_unregister(priv->mii_bus);
1884 }
1885 
1886 static int netsec_register_mdio(struct netsec_priv *priv, u32 phy_addr)
1887 {
1888 	struct mii_bus *bus;
1889 	int ret;
1890 
1891 	bus = devm_mdiobus_alloc(priv->dev);
1892 	if (!bus)
1893 		return -ENOMEM;
1894 
1895 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(priv->dev));
1896 	bus->priv = priv;
1897 	bus->name = "SNI NETSEC MDIO";
1898 	bus->read = netsec_phy_read;
1899 	bus->write = netsec_phy_write;
1900 	bus->parent = priv->dev;
1901 	priv->mii_bus = bus;
1902 
1903 	if (dev_of_node(priv->dev)) {
1904 		struct device_node *mdio_node, *parent = dev_of_node(priv->dev);
1905 
1906 		mdio_node = of_get_child_by_name(parent, "mdio");
1907 		if (mdio_node) {
1908 			parent = mdio_node;
1909 		} else {
1910 			/* older f/w doesn't populate the mdio subnode,
1911 			 * allow relaxed upgrade of f/w in due time.
1912 			 */
1913 			dev_info(priv->dev, "Upgrade f/w for mdio subnode!\n");
1914 		}
1915 
1916 		ret = of_mdiobus_register(bus, parent);
1917 		of_node_put(mdio_node);
1918 
1919 		if (ret) {
1920 			dev_err(priv->dev, "mdiobus register err(%d)\n", ret);
1921 			return ret;
1922 		}
1923 	} else {
1924 		/* Mask out all PHYs from auto probing. */
1925 		bus->phy_mask = ~0;
1926 		ret = mdiobus_register(bus);
1927 		if (ret) {
1928 			dev_err(priv->dev, "mdiobus register err(%d)\n", ret);
1929 			return ret;
1930 		}
1931 
1932 		priv->phydev = get_phy_device(bus, phy_addr, false);
1933 		if (IS_ERR(priv->phydev)) {
1934 			ret = PTR_ERR(priv->phydev);
1935 			dev_err(priv->dev, "get_phy_device err(%d)\n", ret);
1936 			priv->phydev = NULL;
1937 			return -ENODEV;
1938 		}
1939 
1940 		ret = phy_device_register(priv->phydev);
1941 		if (ret) {
1942 			mdiobus_unregister(bus);
1943 			dev_err(priv->dev,
1944 				"phy_device_register err(%d)\n", ret);
1945 		}
1946 	}
1947 
1948 	return ret;
1949 }
1950 
1951 static int netsec_probe(struct platform_device *pdev)
1952 {
1953 	struct resource *mmio_res, *eeprom_res, *irq_res;
1954 	u8 *mac, macbuf[ETH_ALEN];
1955 	struct netsec_priv *priv;
1956 	u32 hw_ver, phy_addr = 0;
1957 	struct net_device *ndev;
1958 	int ret;
1959 
1960 	mmio_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1961 	if (!mmio_res) {
1962 		dev_err(&pdev->dev, "No MMIO resource found.\n");
1963 		return -ENODEV;
1964 	}
1965 
1966 	eeprom_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1967 	if (!eeprom_res) {
1968 		dev_info(&pdev->dev, "No EEPROM resource found.\n");
1969 		return -ENODEV;
1970 	}
1971 
1972 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1973 	if (!irq_res) {
1974 		dev_err(&pdev->dev, "No IRQ resource found.\n");
1975 		return -ENODEV;
1976 	}
1977 
1978 	ndev = alloc_etherdev(sizeof(*priv));
1979 	if (!ndev)
1980 		return -ENOMEM;
1981 
1982 	priv = netdev_priv(ndev);
1983 
1984 	spin_lock_init(&priv->reglock);
1985 	SET_NETDEV_DEV(ndev, &pdev->dev);
1986 	platform_set_drvdata(pdev, priv);
1987 	ndev->irq = irq_res->start;
1988 	priv->dev = &pdev->dev;
1989 	priv->ndev = ndev;
1990 
1991 	priv->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV |
1992 			   NETIF_MSG_LINK | NETIF_MSG_PROBE;
1993 
1994 	priv->phy_interface = device_get_phy_mode(&pdev->dev);
1995 	if ((int)priv->phy_interface < 0) {
1996 		dev_err(&pdev->dev, "missing required property 'phy-mode'\n");
1997 		ret = -ENODEV;
1998 		goto free_ndev;
1999 	}
2000 
2001 	priv->ioaddr = devm_ioremap(&pdev->dev, mmio_res->start,
2002 				    resource_size(mmio_res));
2003 	if (!priv->ioaddr) {
2004 		dev_err(&pdev->dev, "devm_ioremap() failed\n");
2005 		ret = -ENXIO;
2006 		goto free_ndev;
2007 	}
2008 
2009 	priv->eeprom_base = devm_ioremap(&pdev->dev, eeprom_res->start,
2010 					 resource_size(eeprom_res));
2011 	if (!priv->eeprom_base) {
2012 		dev_err(&pdev->dev, "devm_ioremap() failed for EEPROM\n");
2013 		ret = -ENXIO;
2014 		goto free_ndev;
2015 	}
2016 
2017 	mac = device_get_mac_address(&pdev->dev, macbuf, sizeof(macbuf));
2018 	if (mac)
2019 		ether_addr_copy(ndev->dev_addr, mac);
2020 
2021 	if (priv->eeprom_base &&
2022 	    (!mac || !is_valid_ether_addr(ndev->dev_addr))) {
2023 		void __iomem *macp = priv->eeprom_base +
2024 					NETSEC_EEPROM_MAC_ADDRESS;
2025 
2026 		ndev->dev_addr[0] = readb(macp + 3);
2027 		ndev->dev_addr[1] = readb(macp + 2);
2028 		ndev->dev_addr[2] = readb(macp + 1);
2029 		ndev->dev_addr[3] = readb(macp + 0);
2030 		ndev->dev_addr[4] = readb(macp + 7);
2031 		ndev->dev_addr[5] = readb(macp + 6);
2032 	}
2033 
2034 	if (!is_valid_ether_addr(ndev->dev_addr)) {
2035 		dev_warn(&pdev->dev, "No MAC address found, using random\n");
2036 		eth_hw_addr_random(ndev);
2037 	}
2038 
2039 	if (dev_of_node(&pdev->dev))
2040 		ret = netsec_of_probe(pdev, priv, &phy_addr);
2041 	else
2042 		ret = netsec_acpi_probe(pdev, priv, &phy_addr);
2043 	if (ret)
2044 		goto free_ndev;
2045 
2046 	priv->phy_addr = phy_addr;
2047 
2048 	if (!priv->freq) {
2049 		dev_err(&pdev->dev, "missing PHY reference clock frequency\n");
2050 		ret = -ENODEV;
2051 		goto free_ndev;
2052 	}
2053 
2054 	/* default for throughput */
2055 	priv->et_coalesce.rx_coalesce_usecs = 500;
2056 	priv->et_coalesce.rx_max_coalesced_frames = 8;
2057 	priv->et_coalesce.tx_coalesce_usecs = 500;
2058 	priv->et_coalesce.tx_max_coalesced_frames = 8;
2059 
2060 	ret = device_property_read_u32(&pdev->dev, "max-frame-size",
2061 				       &ndev->max_mtu);
2062 	if (ret < 0)
2063 		ndev->max_mtu = ETH_DATA_LEN;
2064 
2065 	/* runtime_pm coverage just for probe, open/close also cover it */
2066 	pm_runtime_enable(&pdev->dev);
2067 	pm_runtime_get_sync(&pdev->dev);
2068 
2069 	hw_ver = netsec_read(priv, NETSEC_REG_F_TAIKI_VER);
2070 	/* this driver only supports F_TAIKI style NETSEC */
2071 	if (NETSEC_F_NETSEC_VER_MAJOR_NUM(hw_ver) !=
2072 	    NETSEC_F_NETSEC_VER_MAJOR_NUM(NETSEC_REG_NETSEC_VER_F_TAIKI)) {
2073 		ret = -ENODEV;
2074 		goto pm_disable;
2075 	}
2076 
2077 	dev_info(&pdev->dev, "hardware revision %d.%d\n",
2078 		 hw_ver >> 16, hw_ver & 0xffff);
2079 
2080 	netif_napi_add(ndev, &priv->napi, netsec_napi_poll, NAPI_POLL_WEIGHT);
2081 
2082 	ndev->netdev_ops = &netsec_netdev_ops;
2083 	ndev->ethtool_ops = &netsec_ethtool_ops;
2084 
2085 	ndev->features |= NETIF_F_HIGHDMA | NETIF_F_RXCSUM | NETIF_F_GSO |
2086 				NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2087 	ndev->hw_features = ndev->features;
2088 
2089 	priv->rx_cksum_offload_flag = true;
2090 
2091 	ret = netsec_register_mdio(priv, phy_addr);
2092 	if (ret)
2093 		goto unreg_napi;
2094 
2095 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)))
2096 		dev_warn(&pdev->dev, "Failed to set DMA mask\n");
2097 
2098 	ret = register_netdev(ndev);
2099 	if (ret) {
2100 		netif_err(priv, probe, ndev, "register_netdev() failed\n");
2101 		goto unreg_mii;
2102 	}
2103 
2104 	pm_runtime_put_sync(&pdev->dev);
2105 	return 0;
2106 
2107 unreg_mii:
2108 	netsec_unregister_mdio(priv);
2109 unreg_napi:
2110 	netif_napi_del(&priv->napi);
2111 pm_disable:
2112 	pm_runtime_put_sync(&pdev->dev);
2113 	pm_runtime_disable(&pdev->dev);
2114 free_ndev:
2115 	free_netdev(ndev);
2116 	dev_err(&pdev->dev, "init failed\n");
2117 
2118 	return ret;
2119 }
2120 
2121 static int netsec_remove(struct platform_device *pdev)
2122 {
2123 	struct netsec_priv *priv = platform_get_drvdata(pdev);
2124 
2125 	unregister_netdev(priv->ndev);
2126 
2127 	netsec_unregister_mdio(priv);
2128 
2129 	netif_napi_del(&priv->napi);
2130 
2131 	pm_runtime_disable(&pdev->dev);
2132 	free_netdev(priv->ndev);
2133 
2134 	return 0;
2135 }
2136 
2137 #ifdef CONFIG_PM
2138 static int netsec_runtime_suspend(struct device *dev)
2139 {
2140 	struct netsec_priv *priv = dev_get_drvdata(dev);
2141 
2142 	netsec_write(priv, NETSEC_REG_CLK_EN, 0);
2143 
2144 	clk_disable_unprepare(priv->clk);
2145 
2146 	return 0;
2147 }
2148 
2149 static int netsec_runtime_resume(struct device *dev)
2150 {
2151 	struct netsec_priv *priv = dev_get_drvdata(dev);
2152 
2153 	clk_prepare_enable(priv->clk);
2154 
2155 	netsec_write(priv, NETSEC_REG_CLK_EN, NETSEC_CLK_EN_REG_DOM_D |
2156 					       NETSEC_CLK_EN_REG_DOM_C |
2157 					       NETSEC_CLK_EN_REG_DOM_G);
2158 	return 0;
2159 }
2160 #endif
2161 
2162 static const struct dev_pm_ops netsec_pm_ops = {
2163 	SET_RUNTIME_PM_OPS(netsec_runtime_suspend, netsec_runtime_resume, NULL)
2164 };
2165 
2166 static const struct of_device_id netsec_dt_ids[] = {
2167 	{ .compatible = "socionext,synquacer-netsec" },
2168 	{ }
2169 };
2170 MODULE_DEVICE_TABLE(of, netsec_dt_ids);
2171 
2172 #ifdef CONFIG_ACPI
2173 static const struct acpi_device_id netsec_acpi_ids[] = {
2174 	{ "SCX0001" },
2175 	{ }
2176 };
2177 MODULE_DEVICE_TABLE(acpi, netsec_acpi_ids);
2178 #endif
2179 
2180 static struct platform_driver netsec_driver = {
2181 	.probe	= netsec_probe,
2182 	.remove	= netsec_remove,
2183 	.driver = {
2184 		.name = "netsec",
2185 		.pm = &netsec_pm_ops,
2186 		.of_match_table = netsec_dt_ids,
2187 		.acpi_match_table = ACPI_PTR(netsec_acpi_ids),
2188 	},
2189 };
2190 module_platform_driver(netsec_driver);
2191 
2192 MODULE_AUTHOR("Jassi Brar <jaswinder.singh@linaro.org>");
2193 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
2194 MODULE_DESCRIPTION("NETSEC Ethernet driver");
2195 MODULE_LICENSE("GPL");
2196