1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Texas Instruments Ethernet Switch Driver
4  *
5  * Copyright (C) 2019 Texas Instruments
6  */
7 
8 #include <linux/io.h>
9 #include <linux/clk.h>
10 #include <linux/platform_device.h>
11 #include <linux/timer.h>
12 #include <linux/module.h>
13 #include <linux/irqreturn.h>
14 #include <linux/interrupt.h>
15 #include <linux/if_ether.h>
16 #include <linux/etherdevice.h>
17 #include <linux/net_tstamp.h>
18 #include <linux/phy.h>
19 #include <linux/phy/phy.h>
20 #include <linux/delay.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/of.h>
25 #include <linux/of_mdio.h>
26 #include <linux/of_net.h>
27 #include <linux/of_platform.h>
28 #include <linux/if_vlan.h>
29 #include <linux/kmemleak.h>
30 #include <linux/sys_soc.h>
31 
32 #include <net/switchdev.h>
33 #include <net/page_pool/helpers.h>
34 #include <net/pkt_cls.h>
35 #include <net/devlink.h>
36 
37 #include "cpsw.h"
38 #include "cpsw_ale.h"
39 #include "cpsw_priv.h"
40 #include "cpsw_sl.h"
41 #include "cpsw_switchdev.h"
42 #include "cpts.h"
43 #include "davinci_cpdma.h"
44 
45 #include <net/pkt_sched.h>
46 
47 static int debug_level;
48 static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT;
49 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
50 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
51 
52 struct cpsw_devlink {
53 	struct cpsw_common *cpsw;
54 };
55 
56 enum cpsw_devlink_param_id {
57 	CPSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
58 	CPSW_DL_PARAM_SWITCH_MODE,
59 	CPSW_DL_PARAM_ALE_BYPASS,
60 };
61 
62 /* struct cpsw_common is not needed, kept here for compatibility
63  * reasons witrh the old driver
64  */
65 static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
66 				 struct cpsw_priv *priv)
67 {
68 	if (priv->emac_port == HOST_PORT_NUM)
69 		return -1;
70 
71 	return priv->emac_port - 1;
72 }
73 
74 static bool cpsw_is_switch_en(struct cpsw_common *cpsw)
75 {
76 	return !cpsw->data.dual_emac;
77 }
78 
79 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
80 {
81 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
82 	bool enable_uni = false;
83 	int i;
84 
85 	if (cpsw_is_switch_en(cpsw))
86 		return;
87 
88 	/* Enabling promiscuous mode for one interface will be
89 	 * common for both the interface as the interface shares
90 	 * the same hardware resource.
91 	 */
92 	for (i = 0; i < cpsw->data.slaves; i++)
93 		if (cpsw->slaves[i].ndev &&
94 		    (cpsw->slaves[i].ndev->flags & IFF_PROMISC))
95 			enable_uni = true;
96 
97 	if (!enable && enable_uni) {
98 		enable = enable_uni;
99 		dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
100 	}
101 
102 	if (enable) {
103 		/* Enable unknown unicast, reg/unreg mcast */
104 		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
105 				     ALE_P0_UNI_FLOOD, 1);
106 
107 		dev_dbg(cpsw->dev, "promiscuity enabled\n");
108 	} else {
109 		/* Disable unknown unicast */
110 		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
111 				     ALE_P0_UNI_FLOOD, 0);
112 		dev_dbg(cpsw->dev, "promiscuity disabled\n");
113 	}
114 }
115 
116 /**
117  * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
118  * if it's not deleted
119  * @ndev: device to sync
120  * @addr: address to be added or deleted
121  * @vid: vlan id, if vid < 0 set/unset address for real device
122  * @add: add address if the flag is set or remove otherwise
123  */
124 static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
125 		       int vid, int add)
126 {
127 	struct cpsw_priv *priv = netdev_priv(ndev);
128 	struct cpsw_common *cpsw = priv->cpsw;
129 	int mask, flags, ret, slave_no;
130 
131 	slave_no = cpsw_slave_index(cpsw, priv);
132 	if (vid < 0)
133 		vid = cpsw->slaves[slave_no].port_vlan;
134 
135 	mask =  ALE_PORT_HOST;
136 	flags = vid ? ALE_VLAN : 0;
137 
138 	if (add)
139 		ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
140 	else
141 		ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
142 
143 	return ret;
144 }
145 
146 static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
147 {
148 	struct addr_sync_ctx *sync_ctx = ctx;
149 	struct netdev_hw_addr *ha;
150 	int found = 0, ret = 0;
151 
152 	if (!vdev || !(vdev->flags & IFF_UP))
153 		return 0;
154 
155 	/* vlan address is relevant if its sync_cnt != 0 */
156 	netdev_for_each_mc_addr(ha, vdev) {
157 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
158 			found = ha->sync_cnt;
159 			break;
160 		}
161 	}
162 
163 	if (found)
164 		sync_ctx->consumed++;
165 
166 	if (sync_ctx->flush) {
167 		if (!found)
168 			cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
169 		return 0;
170 	}
171 
172 	if (found)
173 		ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
174 
175 	return ret;
176 }
177 
178 static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
179 {
180 	struct addr_sync_ctx sync_ctx;
181 	int ret;
182 
183 	sync_ctx.consumed = 0;
184 	sync_ctx.addr = addr;
185 	sync_ctx.ndev = ndev;
186 	sync_ctx.flush = 0;
187 
188 	ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
189 	if (sync_ctx.consumed < num && !ret)
190 		ret = cpsw_set_mc(ndev, addr, -1, 1);
191 
192 	return ret;
193 }
194 
195 static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
196 {
197 	struct addr_sync_ctx sync_ctx;
198 
199 	sync_ctx.consumed = 0;
200 	sync_ctx.addr = addr;
201 	sync_ctx.ndev = ndev;
202 	sync_ctx.flush = 1;
203 
204 	vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
205 	if (sync_ctx.consumed == num)
206 		cpsw_set_mc(ndev, addr, -1, 0);
207 
208 	return 0;
209 }
210 
211 static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
212 {
213 	struct addr_sync_ctx *sync_ctx = ctx;
214 	struct netdev_hw_addr *ha;
215 	int found = 0;
216 
217 	if (!vdev || !(vdev->flags & IFF_UP))
218 		return 0;
219 
220 	/* vlan address is relevant if its sync_cnt != 0 */
221 	netdev_for_each_mc_addr(ha, vdev) {
222 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
223 			found = ha->sync_cnt;
224 			break;
225 		}
226 	}
227 
228 	if (!found)
229 		return 0;
230 
231 	sync_ctx->consumed++;
232 	cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
233 	return 0;
234 }
235 
236 static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
237 {
238 	struct addr_sync_ctx sync_ctx;
239 
240 	sync_ctx.addr = addr;
241 	sync_ctx.ndev = ndev;
242 	sync_ctx.consumed = 0;
243 
244 	vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
245 	if (sync_ctx.consumed < num)
246 		cpsw_set_mc(ndev, addr, -1, 0);
247 
248 	return 0;
249 }
250 
251 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
252 {
253 	struct cpsw_priv *priv = netdev_priv(ndev);
254 	struct cpsw_common *cpsw = priv->cpsw;
255 
256 	if (ndev->flags & IFF_PROMISC) {
257 		/* Enable promiscuous mode */
258 		cpsw_set_promiscious(ndev, true);
259 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, priv->emac_port);
260 		return;
261 	}
262 
263 	/* Disable promiscuous mode */
264 	cpsw_set_promiscious(ndev, false);
265 
266 	/* Restore allmulti on vlans if necessary */
267 	cpsw_ale_set_allmulti(cpsw->ale,
268 			      ndev->flags & IFF_ALLMULTI, priv->emac_port);
269 
270 	/* add/remove mcast address either for real netdev or for vlan */
271 	__hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
272 			       cpsw_del_mc_addr);
273 }
274 
275 static unsigned int cpsw_rxbuf_total_len(unsigned int len)
276 {
277 	len += CPSW_HEADROOM_NA;
278 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
279 
280 	return SKB_DATA_ALIGN(len);
281 }
282 
283 static void cpsw_rx_handler(void *token, int len, int status)
284 {
285 	struct page *new_page, *page = token;
286 	void *pa = page_address(page);
287 	int headroom = CPSW_HEADROOM_NA;
288 	struct cpsw_meta_xdp *xmeta;
289 	struct cpsw_common *cpsw;
290 	struct net_device *ndev;
291 	int port, ch, pkt_size;
292 	struct cpsw_priv *priv;
293 	struct page_pool *pool;
294 	struct sk_buff *skb;
295 	struct xdp_buff xdp;
296 	u32 metasize = 0;
297 	int ret = 0;
298 	dma_addr_t dma;
299 
300 	xmeta = pa + CPSW_XMETA_OFFSET;
301 	cpsw = ndev_to_cpsw(xmeta->ndev);
302 	ndev = xmeta->ndev;
303 	pkt_size = cpsw->rx_packet_max;
304 	ch = xmeta->ch;
305 
306 	if (status >= 0) {
307 		port = CPDMA_RX_SOURCE_PORT(status);
308 		if (port)
309 			ndev = cpsw->slaves[--port].ndev;
310 	}
311 
312 	priv = netdev_priv(ndev);
313 	pool = cpsw->page_pool[ch];
314 
315 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
316 		/* In dual emac mode check for all interfaces */
317 		if (cpsw->usage_count && status >= 0) {
318 			/* The packet received is for the interface which
319 			 * is already down and the other interface is up
320 			 * and running, instead of freeing which results
321 			 * in reducing of the number of rx descriptor in
322 			 * DMA engine, requeue page back to cpdma.
323 			 */
324 			new_page = page;
325 			goto requeue;
326 		}
327 
328 		/* the interface is going down, pages are purged */
329 		page_pool_recycle_direct(pool, page);
330 		return;
331 	}
332 
333 	new_page = page_pool_dev_alloc_pages(pool);
334 	if (unlikely(!new_page)) {
335 		new_page = page;
336 		ndev->stats.rx_dropped++;
337 		goto requeue;
338 	}
339 
340 	if (priv->xdp_prog) {
341 		int size = len;
342 
343 		xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
344 		if (status & CPDMA_RX_VLAN_ENCAP) {
345 			headroom += CPSW_RX_VLAN_ENCAP_HDR_SIZE;
346 			size -= CPSW_RX_VLAN_ENCAP_HDR_SIZE;
347 		}
348 
349 		xdp_prepare_buff(&xdp, pa, headroom, size, true);
350 
351 		ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port, &len);
352 		if (ret != CPSW_XDP_PASS)
353 			goto requeue;
354 
355 		headroom = xdp.data - xdp.data_hard_start;
356 		metasize = xdp.data - xdp.data_meta;
357 
358 		/* XDP prog can modify vlan tag, so can't use encap header */
359 		status &= ~CPDMA_RX_VLAN_ENCAP;
360 	}
361 
362 	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
363 	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
364 	if (!skb) {
365 		ndev->stats.rx_dropped++;
366 		page_pool_recycle_direct(pool, page);
367 		goto requeue;
368 	}
369 
370 	skb->offload_fwd_mark = priv->offload_fwd_mark;
371 	skb_reserve(skb, headroom);
372 	skb_put(skb, len);
373 	if (metasize)
374 		skb_metadata_set(skb, metasize);
375 	skb->dev = ndev;
376 	if (status & CPDMA_RX_VLAN_ENCAP)
377 		cpsw_rx_vlan_encap(skb);
378 	if (priv->rx_ts_enabled)
379 		cpts_rx_timestamp(cpsw->cpts, skb);
380 	skb->protocol = eth_type_trans(skb, ndev);
381 
382 	/* mark skb for recycling */
383 	skb_mark_for_recycle(skb);
384 	netif_receive_skb(skb);
385 
386 	ndev->stats.rx_bytes += len;
387 	ndev->stats.rx_packets++;
388 
389 requeue:
390 	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
391 	xmeta->ndev = ndev;
392 	xmeta->ch = ch;
393 
394 	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
395 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
396 				       pkt_size, 0);
397 	if (ret < 0) {
398 		WARN_ON(ret == -ENOMEM);
399 		page_pool_recycle_direct(pool, new_page);
400 	}
401 }
402 
403 static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
404 				   unsigned short vid)
405 {
406 	struct cpsw_common *cpsw = priv->cpsw;
407 	int unreg_mcast_mask = 0;
408 	int mcast_mask;
409 	u32 port_mask;
410 	int ret;
411 
412 	port_mask = (1 << priv->emac_port) | ALE_PORT_HOST;
413 
414 	mcast_mask = ALE_PORT_HOST;
415 	if (priv->ndev->flags & IFF_ALLMULTI)
416 		unreg_mcast_mask = mcast_mask;
417 
418 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
419 				unreg_mcast_mask);
420 	if (ret != 0)
421 		return ret;
422 
423 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
424 				 HOST_PORT_NUM, ALE_VLAN, vid);
425 	if (ret != 0)
426 		goto clean_vid;
427 
428 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
429 				 mcast_mask, ALE_VLAN, vid, 0);
430 	if (ret != 0)
431 		goto clean_vlan_ucast;
432 	return 0;
433 
434 clean_vlan_ucast:
435 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
436 			   HOST_PORT_NUM, ALE_VLAN, vid);
437 clean_vid:
438 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
439 	return ret;
440 }
441 
442 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
443 				    __be16 proto, u16 vid)
444 {
445 	struct cpsw_priv *priv = netdev_priv(ndev);
446 	struct cpsw_common *cpsw = priv->cpsw;
447 	int ret, i;
448 
449 	if (cpsw_is_switch_en(cpsw)) {
450 		dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n");
451 		return 0;
452 	}
453 
454 	if (vid == cpsw->data.default_vlan)
455 		return 0;
456 
457 	ret = pm_runtime_resume_and_get(cpsw->dev);
458 	if (ret < 0)
459 		return ret;
460 
461 	/* In dual EMAC, reserved VLAN id should not be used for
462 	 * creating VLAN interfaces as this can break the dual
463 	 * EMAC port separation
464 	 */
465 	for (i = 0; i < cpsw->data.slaves; i++) {
466 		if (cpsw->slaves[i].ndev &&
467 		    vid == cpsw->slaves[i].port_vlan) {
468 			ret = -EINVAL;
469 			goto err;
470 		}
471 	}
472 
473 	dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
474 	ret = cpsw_add_vlan_ale_entry(priv, vid);
475 err:
476 	pm_runtime_put(cpsw->dev);
477 	return ret;
478 }
479 
480 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
481 {
482 	struct cpsw_priv *priv = arg;
483 
484 	if (!vdev || !vid)
485 		return 0;
486 
487 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
488 	return 0;
489 }
490 
491 /* restore resources after port reset */
492 static void cpsw_restore(struct cpsw_priv *priv)
493 {
494 	struct cpsw_common *cpsw = priv->cpsw;
495 
496 	/* restore vlan configurations */
497 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
498 
499 	/* restore MQPRIO offload */
500 	cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
501 
502 	/* restore CBS offload */
503 	cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
504 
505 	cpsw_qos_clsflower_resume(priv);
506 }
507 
508 static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
509 {
510 	static const char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0};
511 
512 	cpsw_ale_add_mcast(cpsw->ale, stpa,
513 			   ALE_PORT_HOST, ALE_SUPER, 0,
514 			   ALE_MCAST_BLOCK_LEARN_FWD);
515 }
516 
517 static void cpsw_init_host_port_switch(struct cpsw_common *cpsw)
518 {
519 	int vlan = cpsw->data.default_vlan;
520 
521 	writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl);
522 
523 	writel(vlan, &cpsw->host_port_regs->port_vlan);
524 
525 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
526 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
527 			  ALE_PORT_1 | ALE_PORT_2);
528 
529 	cpsw_init_stp_ale_entry(cpsw);
530 
531 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
532 	dev_dbg(cpsw->dev, "Set P0_UNI_FLOOD\n");
533 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
534 }
535 
536 static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw)
537 {
538 	int vlan = cpsw->data.default_vlan;
539 
540 	writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl);
541 
542 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
543 	dev_dbg(cpsw->dev, "unset P0_UNI_FLOOD\n");
544 
545 	writel(vlan, &cpsw->host_port_regs->port_vlan);
546 
547 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
548 	/* learning make no sense in dual_mac mode */
549 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
550 }
551 
552 static void cpsw_init_host_port(struct cpsw_priv *priv)
553 {
554 	struct cpsw_common *cpsw = priv->cpsw;
555 	u32 control_reg;
556 
557 	/* soft reset the controller and initialize ale */
558 	soft_reset("cpsw", &cpsw->regs->soft_reset);
559 	cpsw_ale_start(cpsw->ale);
560 
561 	/* switch to vlan aware mode */
562 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
563 			     CPSW_ALE_VLAN_AWARE);
564 	control_reg = readl(&cpsw->regs->control);
565 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
566 	writel(control_reg, &cpsw->regs->control);
567 
568 	/* setup host port priority mapping */
569 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
570 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
571 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
572 
573 	/* disable priority elevation */
574 	writel_relaxed(0, &cpsw->regs->ptype);
575 
576 	/* enable statistics collection only on all ports */
577 	writel_relaxed(0x7, &cpsw->regs->stat_port_en);
578 
579 	/* Enable internal fifo flow control */
580 	writel(0x7, &cpsw->regs->flow_control);
581 
582 	if (cpsw_is_switch_en(cpsw))
583 		cpsw_init_host_port_switch(cpsw);
584 	else
585 		cpsw_init_host_port_dual_mac(cpsw);
586 
587 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
588 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
589 }
590 
591 static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv,
592 						    struct cpsw_slave *slave)
593 {
594 	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
595 	struct cpsw_common *cpsw = priv->cpsw;
596 	u32 reg;
597 
598 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
599 	       CPSW2_PORT_VLAN;
600 	slave_write(slave, slave->port_vlan, reg);
601 
602 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
603 			  port_mask, port_mask, 0);
604 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
605 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan,
606 			   ALE_MCAST_FWD);
607 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
608 			   HOST_PORT_NUM, ALE_VLAN |
609 			   ALE_SECURE, slave->port_vlan);
610 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
611 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
612 	/* learning make no sense in dual_mac mode */
613 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
614 			     ALE_PORT_NOLEARN, 1);
615 }
616 
617 static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv,
618 						 struct cpsw_slave *slave)
619 {
620 	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
621 	struct cpsw_common *cpsw = priv->cpsw;
622 	u32 reg;
623 
624 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
625 			     ALE_PORT_DROP_UNKNOWN_VLAN, 0);
626 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
627 			     ALE_PORT_NOLEARN, 0);
628 	/* disabling SA_UPDATE required to make stp work, without this setting
629 	 * Host MAC addresses will jump between ports.
630 	 * As per TRM MAC address can be defined as unicast supervisory (super)
631 	 * by setting both (ALE_BLOCKED | ALE_SECURE) which should prevent
632 	 * SA_UPDATE, but HW seems works incorrectly and setting ALE_SECURE
633 	 * causes STP packets to be dropped due to ingress filter
634 	 *	if (source address found) and (secure) and
635 	 *	   (receive port number != port_number))
636 	 *	   then discard the packet
637 	 */
638 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
639 			     ALE_PORT_NO_SA_UPDATE, 1);
640 
641 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
642 			   port_mask, ALE_VLAN, slave->port_vlan,
643 			   ALE_MCAST_FWD_2);
644 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
645 			   HOST_PORT_NUM, ALE_VLAN, slave->port_vlan);
646 
647 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
648 	       CPSW2_PORT_VLAN;
649 	slave_write(slave, slave->port_vlan, reg);
650 }
651 
652 static void cpsw_adjust_link(struct net_device *ndev)
653 {
654 	struct cpsw_priv *priv = netdev_priv(ndev);
655 	struct cpsw_common *cpsw = priv->cpsw;
656 	struct cpsw_slave *slave;
657 	struct phy_device *phy;
658 	u32 mac_control = 0;
659 
660 	slave = &cpsw->slaves[priv->emac_port - 1];
661 	phy = slave->phy;
662 
663 	if (!phy)
664 		return;
665 
666 	if (phy->link) {
667 		mac_control = CPSW_SL_CTL_GMII_EN;
668 
669 		if (phy->speed == 1000)
670 			mac_control |= CPSW_SL_CTL_GIG;
671 		if (phy->duplex)
672 			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
673 
674 		/* set speed_in input in case RMII mode is used in 100Mbps */
675 		if (phy->speed == 100)
676 			mac_control |= CPSW_SL_CTL_IFCTL_A;
677 		/* in band mode only works in 10Mbps RGMII mode */
678 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
679 			mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
680 
681 		if (priv->rx_pause)
682 			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
683 
684 		if (priv->tx_pause)
685 			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
686 
687 		if (mac_control != slave->mac_control)
688 			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
689 
690 		/* enable forwarding */
691 		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
692 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
693 
694 		netif_tx_wake_all_queues(ndev);
695 
696 		if (priv->shp_cfg_speed &&
697 		    priv->shp_cfg_speed != slave->phy->speed &&
698 		    !cpsw_shp_is_off(priv))
699 			dev_warn(priv->dev, "Speed was changed, CBS shaper speeds are changed!");
700 	} else {
701 		netif_tx_stop_all_queues(ndev);
702 
703 		mac_control = 0;
704 		/* disable forwarding */
705 		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
706 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
707 
708 		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
709 
710 		cpsw_sl_ctl_reset(slave->mac_sl);
711 	}
712 
713 	if (mac_control != slave->mac_control)
714 		phy_print_status(phy);
715 
716 	slave->mac_control = mac_control;
717 
718 	if (phy->link && cpsw_need_resplit(cpsw))
719 		cpsw_split_res(cpsw);
720 }
721 
722 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
723 {
724 	struct cpsw_common *cpsw = priv->cpsw;
725 	struct phy_device *phy;
726 
727 	cpsw_sl_reset(slave->mac_sl, 100);
728 	cpsw_sl_ctl_reset(slave->mac_sl);
729 
730 	/* setup priority mapping */
731 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
732 			  RX_PRIORITY_MAPPING);
733 
734 	switch (cpsw->version) {
735 	case CPSW_VERSION_1:
736 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
737 		/* Increase RX FIFO size to 5 for supporting fullduplex
738 		 * flow control mode
739 		 */
740 		slave_write(slave,
741 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
742 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
743 		break;
744 	case CPSW_VERSION_2:
745 	case CPSW_VERSION_3:
746 	case CPSW_VERSION_4:
747 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
748 		/* Increase RX FIFO size to 5 for supporting fullduplex
749 		 * flow control mode
750 		 */
751 		slave_write(slave,
752 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
753 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
754 		break;
755 	}
756 
757 	/* setup max packet size, and mac address */
758 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
759 			  cpsw->rx_packet_max);
760 	cpsw_set_slave_mac(slave, priv);
761 
762 	slave->mac_control = 0;	/* no link yet */
763 
764 	if (cpsw_is_switch_en(cpsw))
765 		cpsw_port_add_switch_def_ale_entries(priv, slave);
766 	else
767 		cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
768 
769 	if (!slave->data->phy_node)
770 		dev_err(priv->dev, "no phy found on slave %d\n",
771 			slave->slave_num);
772 	phy = of_phy_connect(priv->ndev, slave->data->phy_node,
773 			     &cpsw_adjust_link, 0, slave->data->phy_if);
774 	if (!phy) {
775 		dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
776 			slave->data->phy_node,
777 			slave->slave_num);
778 		return;
779 	}
780 
781 	phy->mac_managed_pm = true;
782 
783 	slave->phy = phy;
784 
785 	phy_disable_eee(slave->phy);
786 
787 	phy_attached_info(slave->phy);
788 
789 	phy_start(slave->phy);
790 
791 	/* Configure GMII_SEL register */
792 	phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
793 			 slave->data->phy_if);
794 }
795 
796 static int cpsw_ndo_stop(struct net_device *ndev)
797 {
798 	struct cpsw_priv *priv = netdev_priv(ndev);
799 	struct cpsw_common *cpsw = priv->cpsw;
800 	struct cpsw_slave *slave;
801 
802 	cpsw_info(priv, ifdown, "shutting down ndev\n");
803 	slave = &cpsw->slaves[priv->emac_port - 1];
804 	if (slave->phy)
805 		phy_stop(slave->phy);
806 
807 	netif_tx_stop_all_queues(priv->ndev);
808 
809 	if (slave->phy) {
810 		phy_disconnect(slave->phy);
811 		slave->phy = NULL;
812 	}
813 
814 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
815 
816 	if (cpsw->usage_count <= 1) {
817 		napi_disable(&cpsw->napi_rx);
818 		napi_disable(&cpsw->napi_tx);
819 		cpts_unregister(cpsw->cpts);
820 		cpsw_intr_disable(cpsw);
821 		cpdma_ctlr_stop(cpsw->dma);
822 		cpsw_ale_stop(cpsw->ale);
823 		cpsw_destroy_xdp_rxqs(cpsw);
824 	}
825 
826 	if (cpsw_need_resplit(cpsw))
827 		cpsw_split_res(cpsw);
828 
829 	cpsw->usage_count--;
830 	pm_runtime_put_sync(cpsw->dev);
831 	return 0;
832 }
833 
834 static int cpsw_ndo_open(struct net_device *ndev)
835 {
836 	struct cpsw_priv *priv = netdev_priv(ndev);
837 	struct cpsw_common *cpsw = priv->cpsw;
838 	int ret;
839 
840 	dev_info(priv->dev, "starting ndev. mode: %s\n",
841 		 cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac");
842 	ret = pm_runtime_resume_and_get(cpsw->dev);
843 	if (ret < 0)
844 		return ret;
845 
846 	/* Notify the stack of the actual queue counts. */
847 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
848 	if (ret) {
849 		dev_err(priv->dev, "cannot set real number of tx queues\n");
850 		goto pm_cleanup;
851 	}
852 
853 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
854 	if (ret) {
855 		dev_err(priv->dev, "cannot set real number of rx queues\n");
856 		goto pm_cleanup;
857 	}
858 
859 	/* Initialize host and slave ports */
860 	if (!cpsw->usage_count)
861 		cpsw_init_host_port(priv);
862 	cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv);
863 
864 	/* initialize shared resources for every ndev */
865 	if (!cpsw->usage_count) {
866 		/* create rxqs for both infs in dual mac as they use same pool
867 		 * and must be destroyed together when no users.
868 		 */
869 		ret = cpsw_create_xdp_rxqs(cpsw);
870 		if (ret < 0)
871 			goto err_cleanup;
872 
873 		ret = cpsw_fill_rx_channels(priv);
874 		if (ret < 0)
875 			goto err_cleanup;
876 
877 		if (cpsw->cpts) {
878 			if (cpts_register(cpsw->cpts))
879 				dev_err(priv->dev, "error registering cpts device\n");
880 			else
881 				writel(0x10, &cpsw->wr_regs->misc_en);
882 		}
883 
884 		napi_enable(&cpsw->napi_rx);
885 		napi_enable(&cpsw->napi_tx);
886 
887 		if (cpsw->tx_irq_disabled) {
888 			cpsw->tx_irq_disabled = false;
889 			enable_irq(cpsw->irqs_table[1]);
890 		}
891 
892 		if (cpsw->rx_irq_disabled) {
893 			cpsw->rx_irq_disabled = false;
894 			enable_irq(cpsw->irqs_table[0]);
895 		}
896 	}
897 
898 	cpsw_restore(priv);
899 
900 	/* Enable Interrupt pacing if configured */
901 	if (cpsw->coal_intvl != 0) {
902 		struct ethtool_coalesce coal;
903 
904 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
905 		cpsw_set_coalesce(ndev, &coal, NULL, NULL);
906 	}
907 
908 	cpdma_ctlr_start(cpsw->dma);
909 	cpsw_intr_enable(cpsw);
910 	cpsw->usage_count++;
911 
912 	return 0;
913 
914 err_cleanup:
915 	cpsw_ndo_stop(ndev);
916 
917 pm_cleanup:
918 	pm_runtime_put_sync(cpsw->dev);
919 	return ret;
920 }
921 
922 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
923 				       struct net_device *ndev)
924 {
925 	struct cpsw_priv *priv = netdev_priv(ndev);
926 	struct cpsw_common *cpsw = priv->cpsw;
927 	struct cpts *cpts = cpsw->cpts;
928 	struct netdev_queue *txq;
929 	struct cpdma_chan *txch;
930 	int ret, q_idx;
931 
932 	if (skb_put_padto(skb, READ_ONCE(priv->tx_packet_min))) {
933 		cpsw_err(priv, tx_err, "packet pad failed\n");
934 		ndev->stats.tx_dropped++;
935 		return NET_XMIT_DROP;
936 	}
937 
938 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
939 	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
940 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
941 
942 	q_idx = skb_get_queue_mapping(skb);
943 	if (q_idx >= cpsw->tx_ch_num)
944 		q_idx = q_idx % cpsw->tx_ch_num;
945 
946 	txch = cpsw->txv[q_idx].ch;
947 	txq = netdev_get_tx_queue(ndev, q_idx);
948 	skb_tx_timestamp(skb);
949 	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
950 				priv->emac_port);
951 	if (unlikely(ret != 0)) {
952 		cpsw_err(priv, tx_err, "desc submit failed\n");
953 		goto fail;
954 	}
955 
956 	/* If there is no more tx desc left free then we need to
957 	 * tell the kernel to stop sending us tx frames.
958 	 */
959 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
960 		netif_tx_stop_queue(txq);
961 
962 		/* Barrier, so that stop_queue visible to other cpus */
963 		smp_mb__after_atomic();
964 
965 		if (cpdma_check_free_tx_desc(txch))
966 			netif_tx_wake_queue(txq);
967 	}
968 
969 	return NETDEV_TX_OK;
970 fail:
971 	ndev->stats.tx_dropped++;
972 	netif_tx_stop_queue(txq);
973 
974 	/* Barrier, so that stop_queue visible to other cpus */
975 	smp_mb__after_atomic();
976 
977 	if (cpdma_check_free_tx_desc(txch))
978 		netif_tx_wake_queue(txq);
979 
980 	return NETDEV_TX_BUSY;
981 }
982 
983 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
984 {
985 	struct sockaddr *addr = (struct sockaddr *)p;
986 	struct cpsw_priv *priv = netdev_priv(ndev);
987 	struct cpsw_common *cpsw = priv->cpsw;
988 	int ret, slave_no;
989 	int flags = 0;
990 	u16 vid = 0;
991 
992 	slave_no = cpsw_slave_index(cpsw, priv);
993 	if (!is_valid_ether_addr(addr->sa_data))
994 		return -EADDRNOTAVAIL;
995 
996 	ret = pm_runtime_resume_and_get(cpsw->dev);
997 	if (ret < 0)
998 		return ret;
999 
1000 	vid = cpsw->slaves[slave_no].port_vlan;
1001 	flags = ALE_VLAN | ALE_SECURE;
1002 
1003 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1004 			   flags, vid);
1005 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1006 			   flags, vid);
1007 
1008 	ether_addr_copy(priv->mac_addr, addr->sa_data);
1009 	eth_hw_addr_set(ndev, priv->mac_addr);
1010 	cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv);
1011 
1012 	pm_runtime_put(cpsw->dev);
1013 
1014 	return 0;
1015 }
1016 
1017 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1018 				     __be16 proto, u16 vid)
1019 {
1020 	struct cpsw_priv *priv = netdev_priv(ndev);
1021 	struct cpsw_common *cpsw = priv->cpsw;
1022 	int ret;
1023 	int i;
1024 
1025 	if (cpsw_is_switch_en(cpsw)) {
1026 		dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
1027 		return 0;
1028 	}
1029 
1030 	if (vid == cpsw->data.default_vlan)
1031 		return 0;
1032 
1033 	ret = pm_runtime_resume_and_get(cpsw->dev);
1034 	if (ret < 0)
1035 		return ret;
1036 
1037 	/* reset the return code as pm_runtime_get_sync() can return
1038 	 * non zero values as well.
1039 	 */
1040 	ret = 0;
1041 	for (i = 0; i < cpsw->data.slaves; i++) {
1042 		if (cpsw->slaves[i].ndev &&
1043 		    vid == cpsw->slaves[i].port_vlan) {
1044 			ret = -EINVAL;
1045 			goto err;
1046 		}
1047 	}
1048 
1049 	dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1050 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1051 	if (ret)
1052 		dev_err(priv->dev, "cpsw_ale_del_vlan() failed: ret %d\n", ret);
1053 	ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1054 				 HOST_PORT_NUM, ALE_VLAN, vid);
1055 	if (ret)
1056 		dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n",
1057 			ret);
1058 	ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1059 				 0, ALE_VLAN, vid);
1060 	if (ret)
1061 		dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n",
1062 			ret);
1063 	cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1064 	ret = 0;
1065 err:
1066 	pm_runtime_put(cpsw->dev);
1067 	return ret;
1068 }
1069 
1070 static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
1071 				       size_t len)
1072 {
1073 	struct cpsw_priv *priv = netdev_priv(ndev);
1074 	int err;
1075 
1076 	err = snprintf(name, len, "p%d", priv->emac_port);
1077 
1078 	if (err >= len)
1079 		return -EINVAL;
1080 
1081 	return 0;
1082 }
1083 
1084 #ifdef CONFIG_NET_POLL_CONTROLLER
1085 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1086 {
1087 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1088 
1089 	cpsw_intr_disable(cpsw);
1090 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1091 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1092 	cpsw_intr_enable(cpsw);
1093 }
1094 #endif
1095 
1096 static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1097 			     struct xdp_frame **frames, u32 flags)
1098 {
1099 	struct cpsw_priv *priv = netdev_priv(ndev);
1100 	struct xdp_frame *xdpf;
1101 	int i, nxmit = 0;
1102 
1103 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1104 		return -EINVAL;
1105 
1106 	for (i = 0; i < n; i++) {
1107 		xdpf = frames[i];
1108 		if (xdpf->len < READ_ONCE(priv->tx_packet_min))
1109 			break;
1110 
1111 		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
1112 			break;
1113 		nxmit++;
1114 	}
1115 
1116 	return nxmit;
1117 }
1118 
1119 static int cpsw_get_port_parent_id(struct net_device *ndev,
1120 				   struct netdev_phys_item_id *ppid)
1121 {
1122 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1123 
1124 	ppid->id_len = sizeof(cpsw->base_mac);
1125 	memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len);
1126 
1127 	return 0;
1128 }
1129 
1130 static const struct net_device_ops cpsw_netdev_ops = {
1131 	.ndo_open		= cpsw_ndo_open,
1132 	.ndo_stop		= cpsw_ndo_stop,
1133 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
1134 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
1135 	.ndo_eth_ioctl		= phy_do_ioctl_running,
1136 	.ndo_validate_addr	= eth_validate_addr,
1137 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
1138 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
1139 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
1140 #ifdef CONFIG_NET_POLL_CONTROLLER
1141 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
1142 #endif
1143 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
1144 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
1145 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
1146 	.ndo_get_phys_port_name = cpsw_ndo_get_phys_port_name,
1147 	.ndo_bpf		= cpsw_ndo_bpf,
1148 	.ndo_xdp_xmit		= cpsw_ndo_xdp_xmit,
1149 	.ndo_get_port_parent_id	= cpsw_get_port_parent_id,
1150 	.ndo_hwtstamp_get	= cpsw_hwtstamp_get,
1151 	.ndo_hwtstamp_set	= cpsw_hwtstamp_set,
1152 };
1153 
1154 static void cpsw_get_drvinfo(struct net_device *ndev,
1155 			     struct ethtool_drvinfo *info)
1156 {
1157 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1158 	struct platform_device *pdev;
1159 
1160 	pdev = to_platform_device(cpsw->dev);
1161 	strscpy(info->driver, "cpsw-switch", sizeof(info->driver));
1162 	strscpy(info->version, "2.0", sizeof(info->version));
1163 	strscpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1164 }
1165 
1166 static int cpsw_set_pauseparam(struct net_device *ndev,
1167 			       struct ethtool_pauseparam *pause)
1168 {
1169 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1170 	struct cpsw_priv *priv = netdev_priv(ndev);
1171 	int slave_no;
1172 
1173 	slave_no = cpsw_slave_index(cpsw, priv);
1174 	if (!cpsw->slaves[slave_no].phy)
1175 		return -EINVAL;
1176 
1177 	if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
1178 		return -EINVAL;
1179 
1180 	priv->rx_pause = pause->rx_pause ? true : false;
1181 	priv->tx_pause = pause->tx_pause ? true : false;
1182 
1183 	phy_set_asym_pause(cpsw->slaves[slave_no].phy,
1184 			   priv->rx_pause, priv->tx_pause);
1185 
1186 	return 0;
1187 }
1188 
1189 static int cpsw_set_channels(struct net_device *ndev,
1190 			     struct ethtool_channels *chs)
1191 {
1192 	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1193 }
1194 
1195 static const struct ethtool_ops cpsw_ethtool_ops = {
1196 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1197 	.get_drvinfo		= cpsw_get_drvinfo,
1198 	.get_msglevel		= cpsw_get_msglevel,
1199 	.set_msglevel		= cpsw_set_msglevel,
1200 	.get_link		= ethtool_op_get_link,
1201 	.get_ts_info		= cpsw_get_ts_info,
1202 	.get_coalesce		= cpsw_get_coalesce,
1203 	.set_coalesce		= cpsw_set_coalesce,
1204 	.get_sset_count		= cpsw_get_sset_count,
1205 	.get_strings		= cpsw_get_strings,
1206 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
1207 	.get_pauseparam		= cpsw_get_pauseparam,
1208 	.set_pauseparam		= cpsw_set_pauseparam,
1209 	.get_wol		= cpsw_get_wol,
1210 	.set_wol		= cpsw_set_wol,
1211 	.get_regs_len		= cpsw_get_regs_len,
1212 	.get_regs		= cpsw_get_regs,
1213 	.begin			= cpsw_ethtool_op_begin,
1214 	.complete		= cpsw_ethtool_op_complete,
1215 	.get_channels		= cpsw_get_channels,
1216 	.set_channels		= cpsw_set_channels,
1217 	.get_link_ksettings	= cpsw_get_link_ksettings,
1218 	.set_link_ksettings	= cpsw_set_link_ksettings,
1219 	.get_eee		= cpsw_get_eee,
1220 	.nway_reset		= cpsw_nway_reset,
1221 	.get_ringparam		= cpsw_get_ringparam,
1222 	.set_ringparam		= cpsw_set_ringparam,
1223 };
1224 
1225 static int cpsw_probe_dt(struct cpsw_common *cpsw)
1226 {
1227 	struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np;
1228 	struct cpsw_platform_data *data = &cpsw->data;
1229 	struct device *dev = cpsw->dev;
1230 	int ret;
1231 	u32 prop;
1232 
1233 	if (!node)
1234 		return -EINVAL;
1235 
1236 	tmp_node = of_get_child_by_name(node, "ethernet-ports");
1237 	if (!tmp_node)
1238 		return -ENOENT;
1239 	data->slaves = of_get_child_count(tmp_node);
1240 	if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
1241 		of_node_put(tmp_node);
1242 		return -ENOENT;
1243 	}
1244 
1245 	data->active_slave = 0;
1246 	data->channels = CPSW_MAX_QUEUES;
1247 	data->dual_emac = true;
1248 	data->bd_ram_size = CPSW_BD_RAM_SIZE;
1249 	data->mac_control = 0;
1250 
1251 	data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM,
1252 					sizeof(struct cpsw_slave_data),
1253 					GFP_KERNEL);
1254 	if (!data->slave_data) {
1255 		of_node_put(tmp_node);
1256 		return -ENOMEM;
1257 	}
1258 
1259 	/* Populate all the child nodes here...
1260 	 */
1261 	ret = devm_of_platform_populate(dev);
1262 	/* We do not want to force this, as in some cases may not have child */
1263 	if (ret)
1264 		dev_warn(dev, "Doesn't have any child node\n");
1265 
1266 	for_each_child_of_node(tmp_node, port_np) {
1267 		struct cpsw_slave_data *slave_data;
1268 		u32 port_id;
1269 
1270 		ret = of_property_read_u32(port_np, "reg", &port_id);
1271 		if (ret < 0) {
1272 			dev_err(dev, "%pOF error reading port_id %d\n",
1273 				port_np, ret);
1274 			goto err_node_put;
1275 		}
1276 
1277 		if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
1278 			dev_err(dev, "%pOF has invalid port_id %u\n",
1279 				port_np, port_id);
1280 			ret = -EINVAL;
1281 			goto err_node_put;
1282 		}
1283 
1284 		slave_data = &data->slave_data[port_id - 1];
1285 
1286 		slave_data->disabled = !of_device_is_available(port_np);
1287 		if (slave_data->disabled)
1288 			continue;
1289 
1290 		slave_data->slave_node = port_np;
1291 		slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL);
1292 		if (IS_ERR(slave_data->ifphy)) {
1293 			ret = PTR_ERR(slave_data->ifphy);
1294 			dev_err(dev, "%pOF: Error retrieving port phy: %d\n",
1295 				port_np, ret);
1296 			goto err_node_put;
1297 		}
1298 
1299 		if (of_phy_is_fixed_link(port_np)) {
1300 			ret = of_phy_register_fixed_link(port_np);
1301 			if (ret) {
1302 				dev_err_probe(dev, ret, "%pOF failed to register fixed-link phy\n",
1303 					      port_np);
1304 				goto err_node_put;
1305 			}
1306 			slave_data->phy_node = of_node_get(port_np);
1307 		} else {
1308 			slave_data->phy_node =
1309 				of_parse_phandle(port_np, "phy-handle", 0);
1310 		}
1311 
1312 		if (!slave_data->phy_node) {
1313 			dev_err(dev, "%pOF no phy found\n", port_np);
1314 			ret = -ENODEV;
1315 			goto err_node_put;
1316 		}
1317 
1318 		ret = of_get_phy_mode(port_np, &slave_data->phy_if);
1319 		if (ret) {
1320 			dev_err(dev, "%pOF read phy-mode err %d\n",
1321 				port_np, ret);
1322 			goto err_node_put;
1323 		}
1324 
1325 		ret = of_get_mac_address(port_np, slave_data->mac_addr);
1326 		if (ret) {
1327 			ret = ti_cm_get_macid(dev, port_id - 1,
1328 					      slave_data->mac_addr);
1329 			if (ret)
1330 				goto err_node_put;
1331 		}
1332 
1333 		if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
1334 					 &prop)) {
1335 			dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
1336 				port_np);
1337 			slave_data->dual_emac_res_vlan = port_id;
1338 			dev_err(dev, "%pOF Using %d as Reserved VLAN\n",
1339 				port_np, slave_data->dual_emac_res_vlan);
1340 		} else {
1341 			slave_data->dual_emac_res_vlan = prop;
1342 		}
1343 	}
1344 
1345 	of_node_put(tmp_node);
1346 	return 0;
1347 
1348 err_node_put:
1349 	of_node_put(port_np);
1350 	of_node_put(tmp_node);
1351 	return ret;
1352 }
1353 
1354 static void cpsw_remove_dt(struct cpsw_common *cpsw)
1355 {
1356 	struct cpsw_platform_data *data = &cpsw->data;
1357 	int i = 0;
1358 
1359 	for (i = 0; i < cpsw->data.slaves; i++) {
1360 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1361 		struct device_node *port_np = slave_data->phy_node;
1362 
1363 		if (port_np) {
1364 			if (of_phy_is_fixed_link(port_np))
1365 				of_phy_deregister_fixed_link(port_np);
1366 
1367 			of_node_put(port_np);
1368 		}
1369 	}
1370 }
1371 
1372 static int cpsw_create_ports(struct cpsw_common *cpsw)
1373 {
1374 	struct cpsw_platform_data *data = &cpsw->data;
1375 	struct net_device *ndev, *napi_ndev = NULL;
1376 	struct device *dev = cpsw->dev;
1377 	struct cpsw_priv *priv;
1378 	int ret = 0, i = 0;
1379 
1380 	for (i = 0; i < cpsw->data.slaves; i++) {
1381 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1382 
1383 		if (slave_data->disabled)
1384 			continue;
1385 
1386 		ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
1387 					       CPSW_MAX_QUEUES,
1388 					       CPSW_MAX_QUEUES);
1389 		if (!ndev) {
1390 			dev_err(dev, "error allocating net_device\n");
1391 			return -ENOMEM;
1392 		}
1393 
1394 		priv = netdev_priv(ndev);
1395 		priv->cpsw = cpsw;
1396 		priv->ndev = ndev;
1397 		priv->dev  = dev;
1398 		priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1399 		priv->emac_port = i + 1;
1400 		priv->tx_packet_min = CPSW_MIN_PACKET_SIZE;
1401 
1402 		if (is_valid_ether_addr(slave_data->mac_addr)) {
1403 			ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1404 			dev_info(cpsw->dev, "Detected MACID = %pM\n",
1405 				 priv->mac_addr);
1406 		} else {
1407 			eth_random_addr(slave_data->mac_addr);
1408 			dev_info(cpsw->dev, "Random MACID = %pM\n",
1409 				 priv->mac_addr);
1410 		}
1411 		eth_hw_addr_set(ndev, slave_data->mac_addr);
1412 		ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1413 
1414 		cpsw->slaves[i].ndev = ndev;
1415 
1416 		ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
1417 				  NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_TC;
1418 		ndev->netns_immutable = true;
1419 
1420 		ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
1421 				     NETDEV_XDP_ACT_REDIRECT |
1422 				     NETDEV_XDP_ACT_NDO_XMIT;
1423 
1424 		ndev->netdev_ops = &cpsw_netdev_ops;
1425 		ndev->ethtool_ops = &cpsw_ethtool_ops;
1426 		SET_NETDEV_DEV(ndev, dev);
1427 		ndev->dev.of_node = slave_data->slave_node;
1428 
1429 		if (!napi_ndev) {
1430 			/* CPSW Host port CPDMA interface is shared between
1431 			 * ports and there is only one TX and one RX IRQs
1432 			 * available for all possible TX and RX channels
1433 			 * accordingly.
1434 			 */
1435 			netif_napi_add(ndev, &cpsw->napi_rx,
1436 				       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll);
1437 			netif_napi_add_tx(ndev, &cpsw->napi_tx,
1438 					  cpsw->quirk_irq ?
1439 					  cpsw_tx_poll : cpsw_tx_mq_poll);
1440 		}
1441 
1442 		napi_ndev = ndev;
1443 	}
1444 
1445 	return ret;
1446 }
1447 
1448 static void cpsw_unregister_ports(struct cpsw_common *cpsw)
1449 {
1450 	int i = 0;
1451 
1452 	for (i = 0; i < cpsw->data.slaves; i++) {
1453 		if (!cpsw->slaves[i].ndev)
1454 			continue;
1455 
1456 		unregister_netdev(cpsw->slaves[i].ndev);
1457 	}
1458 }
1459 
1460 static int cpsw_register_ports(struct cpsw_common *cpsw)
1461 {
1462 	int ret = 0, i = 0;
1463 
1464 	for (i = 0; i < cpsw->data.slaves; i++) {
1465 		if (!cpsw->slaves[i].ndev)
1466 			continue;
1467 
1468 		/* register the network device */
1469 		ret = register_netdev(cpsw->slaves[i].ndev);
1470 		if (ret) {
1471 			dev_err(cpsw->dev,
1472 				"cpsw: err registering net device%d\n", i);
1473 			cpsw->slaves[i].ndev = NULL;
1474 			break;
1475 		}
1476 	}
1477 
1478 	if (ret)
1479 		cpsw_unregister_ports(cpsw);
1480 	return ret;
1481 }
1482 
1483 bool cpsw_port_dev_check(const struct net_device *ndev)
1484 {
1485 	if (ndev->netdev_ops == &cpsw_netdev_ops) {
1486 		struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1487 
1488 		return !cpsw->data.dual_emac;
1489 	}
1490 
1491 	return false;
1492 }
1493 
1494 static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
1495 {
1496 	int set_val = 0;
1497 	int i;
1498 
1499 	if (!cpsw->ale_bypass &&
1500 	    (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
1501 		set_val = 1;
1502 
1503 	dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
1504 
1505 	for (i = 0; i < cpsw->data.slaves; i++) {
1506 		struct net_device *sl_ndev = cpsw->slaves[i].ndev;
1507 		struct cpsw_priv *priv = netdev_priv(sl_ndev);
1508 
1509 		priv->offload_fwd_mark = set_val;
1510 	}
1511 }
1512 
1513 static int cpsw_netdevice_port_link(struct net_device *ndev,
1514 				    struct net_device *br_ndev,
1515 				    struct netlink_ext_ack *extack)
1516 {
1517 	struct cpsw_priv *priv = netdev_priv(ndev);
1518 	struct cpsw_common *cpsw = priv->cpsw;
1519 	int err;
1520 
1521 	if (!cpsw->br_members) {
1522 		cpsw->hw_bridge_dev = br_ndev;
1523 	} else {
1524 		/* This is adding the port to a second bridge, this is
1525 		 * unsupported
1526 		 */
1527 		if (cpsw->hw_bridge_dev != br_ndev)
1528 			return -EOPNOTSUPP;
1529 	}
1530 
1531 	err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
1532 					    false, extack);
1533 	if (err)
1534 		return err;
1535 
1536 	cpsw->br_members |= BIT(priv->emac_port);
1537 
1538 	cpsw_port_offload_fwd_mark_update(cpsw);
1539 
1540 	return NOTIFY_DONE;
1541 }
1542 
1543 static void cpsw_netdevice_port_unlink(struct net_device *ndev)
1544 {
1545 	struct cpsw_priv *priv = netdev_priv(ndev);
1546 	struct cpsw_common *cpsw = priv->cpsw;
1547 
1548 	switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
1549 
1550 	cpsw->br_members &= ~BIT(priv->emac_port);
1551 
1552 	cpsw_port_offload_fwd_mark_update(cpsw);
1553 
1554 	if (!cpsw->br_members)
1555 		cpsw->hw_bridge_dev = NULL;
1556 }
1557 
1558 /* netdev notifier */
1559 static int cpsw_netdevice_event(struct notifier_block *unused,
1560 				unsigned long event, void *ptr)
1561 {
1562 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
1563 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1564 	struct netdev_notifier_changeupper_info *info;
1565 	int ret = NOTIFY_DONE;
1566 
1567 	if (!cpsw_port_dev_check(ndev))
1568 		return NOTIFY_DONE;
1569 
1570 	switch (event) {
1571 	case NETDEV_CHANGEUPPER:
1572 		info = ptr;
1573 
1574 		if (netif_is_bridge_master(info->upper_dev)) {
1575 			if (info->linking)
1576 				ret = cpsw_netdevice_port_link(ndev,
1577 							       info->upper_dev,
1578 							       extack);
1579 			else
1580 				cpsw_netdevice_port_unlink(ndev);
1581 		}
1582 		break;
1583 	default:
1584 		return NOTIFY_DONE;
1585 	}
1586 
1587 	return notifier_from_errno(ret);
1588 }
1589 
1590 static struct notifier_block cpsw_netdevice_nb __read_mostly = {
1591 	.notifier_call = cpsw_netdevice_event,
1592 };
1593 
1594 static int cpsw_register_notifiers(struct cpsw_common *cpsw)
1595 {
1596 	int ret = 0;
1597 
1598 	ret = register_netdevice_notifier(&cpsw_netdevice_nb);
1599 	if (ret) {
1600 		dev_err(cpsw->dev, "can't register netdevice notifier\n");
1601 		return ret;
1602 	}
1603 
1604 	ret = cpsw_switchdev_register_notifiers(cpsw);
1605 	if (ret)
1606 		unregister_netdevice_notifier(&cpsw_netdevice_nb);
1607 
1608 	return ret;
1609 }
1610 
1611 static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
1612 {
1613 	cpsw_switchdev_unregister_notifiers(cpsw);
1614 	unregister_netdevice_notifier(&cpsw_netdevice_nb);
1615 }
1616 
1617 static const struct devlink_ops cpsw_devlink_ops = {
1618 };
1619 
1620 static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
1621 				   struct devlink_param_gset_ctx *ctx)
1622 {
1623 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1624 	struct cpsw_common *cpsw = dl_priv->cpsw;
1625 
1626 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1627 
1628 	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1629 		return  -EOPNOTSUPP;
1630 
1631 	ctx->val.vbool = !cpsw->data.dual_emac;
1632 
1633 	return 0;
1634 }
1635 
1636 static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
1637 				   struct devlink_param_gset_ctx *ctx,
1638 				   struct netlink_ext_ack *extack)
1639 {
1640 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1641 	struct cpsw_common *cpsw = dl_priv->cpsw;
1642 	int vlan = cpsw->data.default_vlan;
1643 	bool switch_en = ctx->val.vbool;
1644 	bool if_running = false;
1645 	int i;
1646 
1647 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1648 
1649 	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1650 		return  -EOPNOTSUPP;
1651 
1652 	if (switch_en == !cpsw->data.dual_emac)
1653 		return 0;
1654 
1655 	if (!switch_en && cpsw->br_members) {
1656 		dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
1657 		return -EINVAL;
1658 	}
1659 
1660 	rtnl_lock();
1661 
1662 	for (i = 0; i < cpsw->data.slaves; i++) {
1663 		struct cpsw_slave *slave = &cpsw->slaves[i];
1664 		struct net_device *sl_ndev = slave->ndev;
1665 
1666 		if (!sl_ndev || !netif_running(sl_ndev))
1667 			continue;
1668 
1669 		if_running = true;
1670 	}
1671 
1672 	if (!if_running) {
1673 		/* all ndevs are down */
1674 		cpsw->data.dual_emac = !switch_en;
1675 		for (i = 0; i < cpsw->data.slaves; i++) {
1676 			struct cpsw_slave *slave = &cpsw->slaves[i];
1677 			struct net_device *sl_ndev = slave->ndev;
1678 
1679 			if (!sl_ndev)
1680 				continue;
1681 
1682 			if (switch_en)
1683 				vlan = cpsw->data.default_vlan;
1684 			else
1685 				vlan = slave->data->dual_emac_res_vlan;
1686 			slave->port_vlan = vlan;
1687 		}
1688 		goto exit;
1689 	}
1690 
1691 	if (switch_en) {
1692 		dev_info(cpsw->dev, "Enable switch mode\n");
1693 
1694 		/* enable bypass - no forwarding; all traffic goes to Host */
1695 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1696 
1697 		/* clean up ALE table */
1698 		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1699 		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1700 
1701 		cpsw_init_host_port_switch(cpsw);
1702 
1703 		for (i = 0; i < cpsw->data.slaves; i++) {
1704 			struct cpsw_slave *slave = &cpsw->slaves[i];
1705 			struct net_device *sl_ndev = slave->ndev;
1706 			struct cpsw_priv *priv;
1707 
1708 			if (!sl_ndev)
1709 				continue;
1710 
1711 			priv = netdev_priv(sl_ndev);
1712 			slave->port_vlan = vlan;
1713 			WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE_VLAN);
1714 			if (netif_running(sl_ndev))
1715 				cpsw_port_add_switch_def_ale_entries(priv,
1716 								     slave);
1717 		}
1718 
1719 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1720 		cpsw->data.dual_emac = false;
1721 	} else {
1722 		dev_info(cpsw->dev, "Disable switch mode\n");
1723 
1724 		/* enable bypass - no forwarding; all traffic goes to Host */
1725 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1726 
1727 		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1728 		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1729 
1730 		cpsw_init_host_port_dual_mac(cpsw);
1731 
1732 		for (i = 0; i < cpsw->data.slaves; i++) {
1733 			struct cpsw_slave *slave = &cpsw->slaves[i];
1734 			struct net_device *sl_ndev = slave->ndev;
1735 			struct cpsw_priv *priv;
1736 
1737 			if (!sl_ndev)
1738 				continue;
1739 
1740 			priv = netdev_priv(slave->ndev);
1741 			slave->port_vlan = slave->data->dual_emac_res_vlan;
1742 			WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE);
1743 			cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
1744 		}
1745 
1746 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1747 		cpsw->data.dual_emac = true;
1748 	}
1749 exit:
1750 	rtnl_unlock();
1751 
1752 	return 0;
1753 }
1754 
1755 static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
1756 				struct devlink_param_gset_ctx *ctx)
1757 {
1758 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1759 	struct cpsw_common *cpsw = dl_priv->cpsw;
1760 
1761 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1762 
1763 	switch (id) {
1764 	case CPSW_DL_PARAM_ALE_BYPASS:
1765 		ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
1766 		break;
1767 	default:
1768 		return -EOPNOTSUPP;
1769 	}
1770 
1771 	return 0;
1772 }
1773 
1774 static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
1775 				struct devlink_param_gset_ctx *ctx,
1776 				struct netlink_ext_ack *extack)
1777 {
1778 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1779 	struct cpsw_common *cpsw = dl_priv->cpsw;
1780 	int ret = -EOPNOTSUPP;
1781 
1782 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1783 
1784 	switch (id) {
1785 	case CPSW_DL_PARAM_ALE_BYPASS:
1786 		ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
1787 					   ctx->val.vbool);
1788 		if (!ret) {
1789 			cpsw->ale_bypass = ctx->val.vbool;
1790 			cpsw_port_offload_fwd_mark_update(cpsw);
1791 		}
1792 		break;
1793 	default:
1794 		return -EOPNOTSUPP;
1795 	}
1796 
1797 	return 0;
1798 }
1799 
1800 static const struct devlink_param cpsw_devlink_params[] = {
1801 	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
1802 			     "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
1803 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1804 			     cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
1805 			     NULL),
1806 	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
1807 			     "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
1808 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1809 			     cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
1810 };
1811 
1812 static int cpsw_register_devlink(struct cpsw_common *cpsw)
1813 {
1814 	struct device *dev = cpsw->dev;
1815 	struct cpsw_devlink *dl_priv;
1816 	int ret = 0;
1817 
1818 	cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv), dev);
1819 	if (!cpsw->devlink)
1820 		return -ENOMEM;
1821 
1822 	dl_priv = devlink_priv(cpsw->devlink);
1823 	dl_priv->cpsw = cpsw;
1824 
1825 	ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
1826 				      ARRAY_SIZE(cpsw_devlink_params));
1827 	if (ret) {
1828 		dev_err(dev, "DL params reg fail ret:%d\n", ret);
1829 		goto dl_unreg;
1830 	}
1831 
1832 	devlink_register(cpsw->devlink);
1833 	return ret;
1834 
1835 dl_unreg:
1836 	devlink_free(cpsw->devlink);
1837 	return ret;
1838 }
1839 
1840 static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
1841 {
1842 	devlink_unregister(cpsw->devlink);
1843 	devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
1844 				  ARRAY_SIZE(cpsw_devlink_params));
1845 	devlink_free(cpsw->devlink);
1846 }
1847 
1848 static const struct of_device_id cpsw_of_mtable[] = {
1849 	{ .compatible = "ti,cpsw-switch"},
1850 	{ .compatible = "ti,am335x-cpsw-switch"},
1851 	{ .compatible = "ti,am4372-cpsw-switch"},
1852 	{ .compatible = "ti,dra7-cpsw-switch"},
1853 	{ /* sentinel */ },
1854 };
1855 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
1856 
1857 static const struct soc_device_attribute cpsw_soc_devices[] = {
1858 	{ .family = "AM33xx", .revision = "ES1.0"},
1859 	{ /* sentinel */ }
1860 };
1861 
1862 static int cpsw_probe(struct platform_device *pdev)
1863 {
1864 	const struct soc_device_attribute *soc;
1865 	struct device *dev = &pdev->dev;
1866 	struct cpsw_common *cpsw;
1867 	struct resource *ss_res;
1868 	struct gpio_descs *mode;
1869 	void __iomem *ss_regs;
1870 	int ret = 0, ch;
1871 	struct clk *clk;
1872 	int irq;
1873 
1874 	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
1875 	if (!cpsw)
1876 		return -ENOMEM;
1877 
1878 	cpsw_slave_index = cpsw_slave_index_priv;
1879 
1880 	cpsw->dev = dev;
1881 
1882 	cpsw->slaves = devm_kcalloc(dev,
1883 				    CPSW_SLAVE_PORTS_NUM,
1884 				    sizeof(struct cpsw_slave),
1885 				    GFP_KERNEL);
1886 	if (!cpsw->slaves)
1887 		return -ENOMEM;
1888 
1889 	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
1890 	if (IS_ERR(mode)) {
1891 		ret = PTR_ERR(mode);
1892 		dev_err(dev, "gpio request failed, ret %d\n", ret);
1893 		return ret;
1894 	}
1895 
1896 	clk = devm_clk_get(dev, "fck");
1897 	if (IS_ERR(clk)) {
1898 		ret = PTR_ERR(clk);
1899 		dev_err(dev, "fck is not found %d\n", ret);
1900 		return ret;
1901 	}
1902 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
1903 
1904 	ss_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ss_res);
1905 	if (IS_ERR(ss_regs)) {
1906 		ret = PTR_ERR(ss_regs);
1907 		return ret;
1908 	}
1909 	cpsw->regs = ss_regs;
1910 
1911 	irq = platform_get_irq_byname(pdev, "rx");
1912 	if (irq < 0)
1913 		return irq;
1914 	cpsw->irqs_table[0] = irq;
1915 
1916 	irq = platform_get_irq_byname(pdev, "tx");
1917 	if (irq < 0)
1918 		return irq;
1919 	cpsw->irqs_table[1] = irq;
1920 
1921 	irq = platform_get_irq_byname(pdev, "misc");
1922 	if (irq <= 0)
1923 		return irq;
1924 	cpsw->misc_irq = irq;
1925 
1926 	platform_set_drvdata(pdev, cpsw);
1927 	/* This may be required here for child devices. */
1928 	pm_runtime_enable(dev);
1929 
1930 	/* Need to enable clocks with runtime PM api to access module
1931 	 * registers
1932 	 */
1933 	ret = pm_runtime_resume_and_get(dev);
1934 	if (ret < 0) {
1935 		pm_runtime_disable(dev);
1936 		return ret;
1937 	}
1938 
1939 	ret = cpsw_probe_dt(cpsw);
1940 	if (ret)
1941 		goto clean_dt_ret;
1942 
1943 	soc = soc_device_match(cpsw_soc_devices);
1944 	if (soc)
1945 		cpsw->quirk_irq = true;
1946 
1947 	cpsw->rx_packet_max = rx_packet_max;
1948 	cpsw->descs_pool_size = descs_pool_size;
1949 	eth_random_addr(cpsw->base_mac);
1950 
1951 	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1952 			       (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
1953 			       descs_pool_size);
1954 	if (ret)
1955 		goto clean_dt_ret;
1956 
1957 	cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
1958 			ss_regs + CPSW1_WR_OFFSET :
1959 			ss_regs + CPSW2_WR_OFFSET;
1960 
1961 	ch = cpsw->quirk_irq ? 0 : 7;
1962 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
1963 	if (IS_ERR(cpsw->txv[0].ch)) {
1964 		dev_err(dev, "error initializing tx dma channel\n");
1965 		ret = PTR_ERR(cpsw->txv[0].ch);
1966 		goto clean_cpts;
1967 	}
1968 
1969 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
1970 	if (IS_ERR(cpsw->rxv[0].ch)) {
1971 		dev_err(dev, "error initializing rx dma channel\n");
1972 		ret = PTR_ERR(cpsw->rxv[0].ch);
1973 		goto clean_cpts;
1974 	}
1975 	cpsw_split_res(cpsw);
1976 
1977 	/* setup netdevs */
1978 	ret = cpsw_create_ports(cpsw);
1979 	if (ret)
1980 		goto clean_unregister_netdev;
1981 
1982 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1983 	 * MISC IRQs which are always kept disabled with this driver so
1984 	 * we will not request them.
1985 	 *
1986 	 * If anyone wants to implement support for those, make sure to
1987 	 * first request and append them to irqs_table array.
1988 	 */
1989 
1990 	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1991 			       0, dev_name(dev), cpsw);
1992 	if (ret < 0) {
1993 		dev_err(dev, "error attaching irq (%d)\n", ret);
1994 		goto clean_unregister_netdev;
1995 	}
1996 
1997 	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1998 			       0, dev_name(dev), cpsw);
1999 	if (ret < 0) {
2000 		dev_err(dev, "error attaching irq (%d)\n", ret);
2001 		goto clean_unregister_netdev;
2002 	}
2003 
2004 	if (!cpsw->cpts)
2005 		goto skip_cpts;
2006 
2007 	ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt,
2008 			       0, dev_name(&pdev->dev), cpsw);
2009 	if (ret < 0) {
2010 		dev_err(dev, "error attaching misc irq (%d)\n", ret);
2011 		goto clean_unregister_netdev;
2012 	}
2013 
2014 	/* Enable misc CPTS evnt_pend IRQ */
2015 	cpts_set_irqpoll(cpsw->cpts, false);
2016 
2017 skip_cpts:
2018 	ret = cpsw_register_notifiers(cpsw);
2019 	if (ret)
2020 		goto clean_unregister_netdev;
2021 
2022 	ret = cpsw_register_devlink(cpsw);
2023 	if (ret)
2024 		goto clean_unregister_notifiers;
2025 
2026 	ret = cpsw_register_ports(cpsw);
2027 	if (ret)
2028 		goto clean_unregister_notifiers;
2029 
2030 	dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
2031 		   &ss_res->start, descs_pool_size,
2032 		   cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
2033 		   CPSW_MINOR_VERSION(cpsw->version),
2034 		   CPSW_RTL_VERSION(cpsw->version));
2035 
2036 	pm_runtime_put(dev);
2037 
2038 	return 0;
2039 
2040 clean_unregister_notifiers:
2041 	cpsw_unregister_notifiers(cpsw);
2042 clean_unregister_netdev:
2043 	cpsw_unregister_ports(cpsw);
2044 clean_cpts:
2045 	cpts_release(cpsw->cpts);
2046 	cpdma_ctlr_destroy(cpsw->dma);
2047 clean_dt_ret:
2048 	cpsw_remove_dt(cpsw);
2049 	pm_runtime_put_sync(dev);
2050 	pm_runtime_disable(dev);
2051 	return ret;
2052 }
2053 
2054 static void cpsw_remove(struct platform_device *pdev)
2055 {
2056 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
2057 	int ret;
2058 
2059 	ret = pm_runtime_resume_and_get(&pdev->dev);
2060 	if (ret < 0) {
2061 		/* Note, if this error path is taken, we're leaking some
2062 		 * resources.
2063 		 */
2064 		dev_err(&pdev->dev, "Failed to resume device (%pe)\n",
2065 			ERR_PTR(ret));
2066 		return;
2067 	}
2068 
2069 	cpsw_unregister_notifiers(cpsw);
2070 	cpsw_unregister_devlink(cpsw);
2071 	cpsw_unregister_ports(cpsw);
2072 
2073 	cpts_release(cpsw->cpts);
2074 	cpdma_ctlr_destroy(cpsw->dma);
2075 	cpsw_remove_dt(cpsw);
2076 	pm_runtime_put_sync(&pdev->dev);
2077 	pm_runtime_disable(&pdev->dev);
2078 }
2079 
2080 static int __maybe_unused cpsw_suspend(struct device *dev)
2081 {
2082 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
2083 	int i;
2084 
2085 	rtnl_lock();
2086 
2087 	for (i = 0; i < cpsw->data.slaves; i++) {
2088 		struct net_device *ndev = cpsw->slaves[i].ndev;
2089 
2090 		if (!(ndev && netif_running(ndev)))
2091 			continue;
2092 
2093 		cpsw_ndo_stop(ndev);
2094 	}
2095 
2096 	rtnl_unlock();
2097 
2098 	/* Select sleep pin state */
2099 	pinctrl_pm_select_sleep_state(dev);
2100 
2101 	return 0;
2102 }
2103 
2104 static int __maybe_unused cpsw_resume(struct device *dev)
2105 {
2106 	struct cpsw_common *cpsw = dev_get_drvdata(dev);
2107 	int i;
2108 
2109 	/* Select default pin state */
2110 	pinctrl_pm_select_default_state(dev);
2111 
2112 	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
2113 	rtnl_lock();
2114 
2115 	for (i = 0; i < cpsw->data.slaves; i++) {
2116 		struct net_device *ndev = cpsw->slaves[i].ndev;
2117 
2118 		if (!(ndev && netif_running(ndev)))
2119 			continue;
2120 
2121 		cpsw_ndo_open(ndev);
2122 	}
2123 
2124 	rtnl_unlock();
2125 
2126 	return 0;
2127 }
2128 
2129 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
2130 
2131 static struct platform_driver cpsw_driver = {
2132 	.driver = {
2133 		.name	 = "cpsw-switch",
2134 		.pm	 = &cpsw_pm_ops,
2135 		.of_match_table = cpsw_of_mtable,
2136 	},
2137 	.probe = cpsw_probe,
2138 	.remove = cpsw_remove,
2139 };
2140 
2141 module_platform_driver(cpsw_driver);
2142 
2143 MODULE_LICENSE("GPL");
2144 MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");
2145