xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw_new.c (revision 7f877908)
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/timer.h>
11 #include <linux/module.h>
12 #include <linux/irqreturn.h>
13 #include <linux/interrupt.h>
14 #include <linux/if_ether.h>
15 #include <linux/etherdevice.h>
16 #include <linux/net_tstamp.h>
17 #include <linux/phy.h>
18 #include <linux/phy/phy.h>
19 #include <linux/delay.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of.h>
23 #include <linux/of_mdio.h>
24 #include <linux/of_net.h>
25 #include <linux/of_device.h>
26 #include <linux/if_vlan.h>
27 #include <linux/kmemleak.h>
28 #include <linux/sys_soc.h>
29 
30 #include <net/page_pool.h>
31 #include <net/pkt_cls.h>
32 #include <net/devlink.h>
33 
34 #include "cpsw.h"
35 #include "cpsw_ale.h"
36 #include "cpsw_priv.h"
37 #include "cpsw_sl.h"
38 #include "cpsw_switchdev.h"
39 #include "cpts.h"
40 #include "davinci_cpdma.h"
41 
42 #include <net/pkt_sched.h>
43 
44 static int debug_level;
45 static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT;
46 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
47 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
48 
49 struct cpsw_devlink {
50 	struct cpsw_common *cpsw;
51 };
52 
53 enum cpsw_devlink_param_id {
54 	CPSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
55 	CPSW_DL_PARAM_SWITCH_MODE,
56 	CPSW_DL_PARAM_ALE_BYPASS,
57 };
58 
59 /* struct cpsw_common is not needed, kept here for compatibility
60  * reasons witrh the old driver
61  */
62 static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
63 				 struct cpsw_priv *priv)
64 {
65 	if (priv->emac_port == HOST_PORT_NUM)
66 		return -1;
67 
68 	return priv->emac_port - 1;
69 }
70 
71 static bool cpsw_is_switch_en(struct cpsw_common *cpsw)
72 {
73 	return !cpsw->data.dual_emac;
74 }
75 
76 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
77 {
78 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
79 	bool enable_uni = false;
80 	int i;
81 
82 	if (cpsw_is_switch_en(cpsw))
83 		return;
84 
85 	/* Enabling promiscuous mode for one interface will be
86 	 * common for both the interface as the interface shares
87 	 * the same hardware resource.
88 	 */
89 	for (i = 0; i < cpsw->data.slaves; i++)
90 		if (cpsw->slaves[i].ndev &&
91 		    (cpsw->slaves[i].ndev->flags & IFF_PROMISC))
92 			enable_uni = true;
93 
94 	if (!enable && enable_uni) {
95 		enable = enable_uni;
96 		dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
97 	}
98 
99 	if (enable) {
100 		/* Enable unknown unicast, reg/unreg mcast */
101 		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
102 				     ALE_P0_UNI_FLOOD, 1);
103 
104 		dev_dbg(cpsw->dev, "promiscuity enabled\n");
105 	} else {
106 		/* Disable unknown unicast */
107 		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
108 				     ALE_P0_UNI_FLOOD, 0);
109 		dev_dbg(cpsw->dev, "promiscuity disabled\n");
110 	}
111 }
112 
113 /**
114  * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
115  * if it's not deleted
116  * @ndev: device to sync
117  * @addr: address to be added or deleted
118  * @vid: vlan id, if vid < 0 set/unset address for real device
119  * @add: add address if the flag is set or remove otherwise
120  */
121 static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
122 		       int vid, int add)
123 {
124 	struct cpsw_priv *priv = netdev_priv(ndev);
125 	struct cpsw_common *cpsw = priv->cpsw;
126 	int mask, flags, ret, slave_no;
127 
128 	slave_no = cpsw_slave_index(cpsw, priv);
129 	if (vid < 0)
130 		vid = cpsw->slaves[slave_no].port_vlan;
131 
132 	mask =  ALE_PORT_HOST;
133 	flags = vid ? ALE_VLAN : 0;
134 
135 	if (add)
136 		ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
137 	else
138 		ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
139 
140 	return ret;
141 }
142 
143 static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
144 {
145 	struct addr_sync_ctx *sync_ctx = ctx;
146 	struct netdev_hw_addr *ha;
147 	int found = 0, ret = 0;
148 
149 	if (!vdev || !(vdev->flags & IFF_UP))
150 		return 0;
151 
152 	/* vlan address is relevant if its sync_cnt != 0 */
153 	netdev_for_each_mc_addr(ha, vdev) {
154 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
155 			found = ha->sync_cnt;
156 			break;
157 		}
158 	}
159 
160 	if (found)
161 		sync_ctx->consumed++;
162 
163 	if (sync_ctx->flush) {
164 		if (!found)
165 			cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
166 		return 0;
167 	}
168 
169 	if (found)
170 		ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
171 
172 	return ret;
173 }
174 
175 static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
176 {
177 	struct addr_sync_ctx sync_ctx;
178 	int ret;
179 
180 	sync_ctx.consumed = 0;
181 	sync_ctx.addr = addr;
182 	sync_ctx.ndev = ndev;
183 	sync_ctx.flush = 0;
184 
185 	ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
186 	if (sync_ctx.consumed < num && !ret)
187 		ret = cpsw_set_mc(ndev, addr, -1, 1);
188 
189 	return ret;
190 }
191 
192 static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
193 {
194 	struct addr_sync_ctx sync_ctx;
195 
196 	sync_ctx.consumed = 0;
197 	sync_ctx.addr = addr;
198 	sync_ctx.ndev = ndev;
199 	sync_ctx.flush = 1;
200 
201 	vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
202 	if (sync_ctx.consumed == num)
203 		cpsw_set_mc(ndev, addr, -1, 0);
204 
205 	return 0;
206 }
207 
208 static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
209 {
210 	struct addr_sync_ctx *sync_ctx = ctx;
211 	struct netdev_hw_addr *ha;
212 	int found = 0;
213 
214 	if (!vdev || !(vdev->flags & IFF_UP))
215 		return 0;
216 
217 	/* vlan address is relevant if its sync_cnt != 0 */
218 	netdev_for_each_mc_addr(ha, vdev) {
219 		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
220 			found = ha->sync_cnt;
221 			break;
222 		}
223 	}
224 
225 	if (!found)
226 		return 0;
227 
228 	sync_ctx->consumed++;
229 	cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
230 	return 0;
231 }
232 
233 static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
234 {
235 	struct addr_sync_ctx sync_ctx;
236 
237 	sync_ctx.addr = addr;
238 	sync_ctx.ndev = ndev;
239 	sync_ctx.consumed = 0;
240 
241 	vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
242 	if (sync_ctx.consumed < num)
243 		cpsw_set_mc(ndev, addr, -1, 0);
244 
245 	return 0;
246 }
247 
248 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
249 {
250 	struct cpsw_priv *priv = netdev_priv(ndev);
251 	struct cpsw_common *cpsw = priv->cpsw;
252 
253 	if (ndev->flags & IFF_PROMISC) {
254 		/* Enable promiscuous mode */
255 		cpsw_set_promiscious(ndev, true);
256 		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, priv->emac_port);
257 		return;
258 	}
259 
260 	/* Disable promiscuous mode */
261 	cpsw_set_promiscious(ndev, false);
262 
263 	/* Restore allmulti on vlans if necessary */
264 	cpsw_ale_set_allmulti(cpsw->ale,
265 			      ndev->flags & IFF_ALLMULTI, priv->emac_port);
266 
267 	/* add/remove mcast address either for real netdev or for vlan */
268 	__hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
269 			       cpsw_del_mc_addr);
270 }
271 
272 static unsigned int cpsw_rxbuf_total_len(unsigned int len)
273 {
274 	len += CPSW_HEADROOM;
275 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
276 
277 	return SKB_DATA_ALIGN(len);
278 }
279 
280 static void cpsw_rx_handler(void *token, int len, int status)
281 {
282 	struct page *new_page, *page = token;
283 	void *pa = page_address(page);
284 	int headroom = CPSW_HEADROOM;
285 	struct cpsw_meta_xdp *xmeta;
286 	struct cpsw_common *cpsw;
287 	struct net_device *ndev;
288 	int port, ch, pkt_size;
289 	struct cpsw_priv *priv;
290 	struct page_pool *pool;
291 	struct sk_buff *skb;
292 	struct xdp_buff xdp;
293 	int ret = 0;
294 	dma_addr_t dma;
295 
296 	xmeta = pa + CPSW_XMETA_OFFSET;
297 	cpsw = ndev_to_cpsw(xmeta->ndev);
298 	ndev = xmeta->ndev;
299 	pkt_size = cpsw->rx_packet_max;
300 	ch = xmeta->ch;
301 
302 	if (status >= 0) {
303 		port = CPDMA_RX_SOURCE_PORT(status);
304 		if (port)
305 			ndev = cpsw->slaves[--port].ndev;
306 	}
307 
308 	priv = netdev_priv(ndev);
309 	pool = cpsw->page_pool[ch];
310 
311 	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
312 		/* In dual emac mode check for all interfaces */
313 		if (cpsw->usage_count && status >= 0) {
314 			/* The packet received is for the interface which
315 			 * is already down and the other interface is up
316 			 * and running, instead of freeing which results
317 			 * in reducing of the number of rx descriptor in
318 			 * DMA engine, requeue page back to cpdma.
319 			 */
320 			new_page = page;
321 			goto requeue;
322 		}
323 
324 		/* the interface is going down, pages are purged */
325 		page_pool_recycle_direct(pool, page);
326 		return;
327 	}
328 
329 	new_page = page_pool_dev_alloc_pages(pool);
330 	if (unlikely(!new_page)) {
331 		new_page = page;
332 		ndev->stats.rx_dropped++;
333 		goto requeue;
334 	}
335 
336 	if (priv->xdp_prog) {
337 		if (status & CPDMA_RX_VLAN_ENCAP) {
338 			xdp.data = pa + CPSW_HEADROOM +
339 				   CPSW_RX_VLAN_ENCAP_HDR_SIZE;
340 			xdp.data_end = xdp.data + len -
341 				       CPSW_RX_VLAN_ENCAP_HDR_SIZE;
342 		} else {
343 			xdp.data = pa + CPSW_HEADROOM;
344 			xdp.data_end = xdp.data + len;
345 		}
346 
347 		xdp_set_data_meta_invalid(&xdp);
348 
349 		xdp.data_hard_start = pa;
350 		xdp.rxq = &priv->xdp_rxq[ch];
351 
352 		ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port);
353 		if (ret != CPSW_XDP_PASS)
354 			goto requeue;
355 
356 		/* XDP prog might have changed packet data and boundaries */
357 		len = xdp.data_end - xdp.data;
358 		headroom = xdp.data - xdp.data_hard_start;
359 
360 		/* XDP prog can modify vlan tag, so can't use encap header */
361 		status &= ~CPDMA_RX_VLAN_ENCAP;
362 	}
363 
364 	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
365 	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
366 	if (!skb) {
367 		ndev->stats.rx_dropped++;
368 		page_pool_recycle_direct(pool, page);
369 		goto requeue;
370 	}
371 
372 	skb->offload_fwd_mark = priv->offload_fwd_mark;
373 	skb_reserve(skb, headroom);
374 	skb_put(skb, len);
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 	/* unmap page as no netstack skb page recycling */
383 	page_pool_release_page(pool, page);
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;
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_get_sync(cpsw->dev);
458 	if (ret < 0) {
459 		pm_runtime_put_noidle(cpsw->dev);
460 		return ret;
461 	}
462 
463 	/* In dual EMAC, reserved VLAN id should not be used for
464 	 * creating VLAN interfaces as this can break the dual
465 	 * EMAC port separation
466 	 */
467 	for (i = 0; i < cpsw->data.slaves; i++) {
468 		if (cpsw->slaves[i].ndev &&
469 		    vid == cpsw->slaves[i].port_vlan) {
470 			ret = -EINVAL;
471 			goto err;
472 		}
473 	}
474 
475 	dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
476 	ret = cpsw_add_vlan_ale_entry(priv, vid);
477 err:
478 	pm_runtime_put(cpsw->dev);
479 	return ret;
480 }
481 
482 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
483 {
484 	struct cpsw_priv *priv = arg;
485 
486 	if (!vdev || !vid)
487 		return 0;
488 
489 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
490 	return 0;
491 }
492 
493 /* restore resources after port reset */
494 static void cpsw_restore(struct cpsw_priv *priv)
495 {
496 	struct cpsw_common *cpsw = priv->cpsw;
497 
498 	/* restore vlan configurations */
499 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
500 
501 	/* restore MQPRIO offload */
502 	cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
503 
504 	/* restore CBS offload */
505 	cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
506 }
507 
508 static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
509 {
510 	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 unaware 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 	slave->phy = phy;
781 
782 	phy_attached_info(slave->phy);
783 
784 	phy_start(slave->phy);
785 
786 	/* Configure GMII_SEL register */
787 	phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
788 			 slave->data->phy_if);
789 }
790 
791 static int cpsw_ndo_stop(struct net_device *ndev)
792 {
793 	struct cpsw_priv *priv = netdev_priv(ndev);
794 	struct cpsw_common *cpsw = priv->cpsw;
795 	struct cpsw_slave *slave;
796 
797 	cpsw_info(priv, ifdown, "shutting down ndev\n");
798 	slave = &cpsw->slaves[priv->emac_port - 1];
799 	if (slave->phy)
800 		phy_stop(slave->phy);
801 
802 	netif_tx_stop_all_queues(priv->ndev);
803 
804 	if (slave->phy) {
805 		phy_disconnect(slave->phy);
806 		slave->phy = NULL;
807 	}
808 
809 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
810 
811 	if (cpsw->usage_count <= 1) {
812 		napi_disable(&cpsw->napi_rx);
813 		napi_disable(&cpsw->napi_tx);
814 		cpts_unregister(cpsw->cpts);
815 		cpsw_intr_disable(cpsw);
816 		cpdma_ctlr_stop(cpsw->dma);
817 		cpsw_ale_stop(cpsw->ale);
818 		cpsw_destroy_xdp_rxqs(cpsw);
819 	}
820 
821 	if (cpsw_need_resplit(cpsw))
822 		cpsw_split_res(cpsw);
823 
824 	cpsw->usage_count--;
825 	pm_runtime_put_sync(cpsw->dev);
826 	return 0;
827 }
828 
829 static int cpsw_ndo_open(struct net_device *ndev)
830 {
831 	struct cpsw_priv *priv = netdev_priv(ndev);
832 	struct cpsw_common *cpsw = priv->cpsw;
833 	int ret;
834 
835 	dev_info(priv->dev, "starting ndev. mode: %s\n",
836 		 cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac");
837 	ret = pm_runtime_get_sync(cpsw->dev);
838 	if (ret < 0) {
839 		pm_runtime_put_noidle(cpsw->dev);
840 		return ret;
841 	}
842 
843 	/* Notify the stack of the actual queue counts. */
844 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
845 	if (ret) {
846 		dev_err(priv->dev, "cannot set real number of tx queues\n");
847 		goto pm_cleanup;
848 	}
849 
850 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
851 	if (ret) {
852 		dev_err(priv->dev, "cannot set real number of rx queues\n");
853 		goto pm_cleanup;
854 	}
855 
856 	/* Initialize host and slave ports */
857 	if (!cpsw->usage_count)
858 		cpsw_init_host_port(priv);
859 	cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv);
860 
861 	/* initialize shared resources for every ndev */
862 	if (!cpsw->usage_count) {
863 		/* create rxqs for both infs in dual mac as they use same pool
864 		 * and must be destroyed together when no users.
865 		 */
866 		ret = cpsw_create_xdp_rxqs(cpsw);
867 		if (ret < 0)
868 			goto err_cleanup;
869 
870 		ret = cpsw_fill_rx_channels(priv);
871 		if (ret < 0)
872 			goto err_cleanup;
873 
874 		if (cpts_register(cpsw->cpts))
875 			dev_err(priv->dev, "error registering cpts device\n");
876 
877 		napi_enable(&cpsw->napi_rx);
878 		napi_enable(&cpsw->napi_tx);
879 
880 		if (cpsw->tx_irq_disabled) {
881 			cpsw->tx_irq_disabled = false;
882 			enable_irq(cpsw->irqs_table[1]);
883 		}
884 
885 		if (cpsw->rx_irq_disabled) {
886 			cpsw->rx_irq_disabled = false;
887 			enable_irq(cpsw->irqs_table[0]);
888 		}
889 	}
890 
891 	cpsw_restore(priv);
892 
893 	/* Enable Interrupt pacing if configured */
894 	if (cpsw->coal_intvl != 0) {
895 		struct ethtool_coalesce coal;
896 
897 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
898 		cpsw_set_coalesce(ndev, &coal);
899 	}
900 
901 	cpdma_ctlr_start(cpsw->dma);
902 	cpsw_intr_enable(cpsw);
903 	cpsw->usage_count++;
904 
905 	return 0;
906 
907 err_cleanup:
908 	cpsw_ndo_stop(ndev);
909 
910 pm_cleanup:
911 	pm_runtime_put_sync(cpsw->dev);
912 	return ret;
913 }
914 
915 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
916 				       struct net_device *ndev)
917 {
918 	struct cpsw_priv *priv = netdev_priv(ndev);
919 	struct cpsw_common *cpsw = priv->cpsw;
920 	struct cpts *cpts = cpsw->cpts;
921 	struct netdev_queue *txq;
922 	struct cpdma_chan *txch;
923 	int ret, q_idx;
924 
925 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
926 		cpsw_err(priv, tx_err, "packet pad failed\n");
927 		ndev->stats.tx_dropped++;
928 		return NET_XMIT_DROP;
929 	}
930 
931 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
932 	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
933 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
934 
935 	q_idx = skb_get_queue_mapping(skb);
936 	if (q_idx >= cpsw->tx_ch_num)
937 		q_idx = q_idx % cpsw->tx_ch_num;
938 
939 	txch = cpsw->txv[q_idx].ch;
940 	txq = netdev_get_tx_queue(ndev, q_idx);
941 	skb_tx_timestamp(skb);
942 	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
943 				priv->emac_port);
944 	if (unlikely(ret != 0)) {
945 		cpsw_err(priv, tx_err, "desc submit failed\n");
946 		goto fail;
947 	}
948 
949 	/* If there is no more tx desc left free then we need to
950 	 * tell the kernel to stop sending us tx frames.
951 	 */
952 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
953 		netif_tx_stop_queue(txq);
954 
955 		/* Barrier, so that stop_queue visible to other cpus */
956 		smp_mb__after_atomic();
957 
958 		if (cpdma_check_free_tx_desc(txch))
959 			netif_tx_wake_queue(txq);
960 	}
961 
962 	return NETDEV_TX_OK;
963 fail:
964 	ndev->stats.tx_dropped++;
965 	netif_tx_stop_queue(txq);
966 
967 	/* Barrier, so that stop_queue visible to other cpus */
968 	smp_mb__after_atomic();
969 
970 	if (cpdma_check_free_tx_desc(txch))
971 		netif_tx_wake_queue(txq);
972 
973 	return NETDEV_TX_BUSY;
974 }
975 
976 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
977 {
978 	struct sockaddr *addr = (struct sockaddr *)p;
979 	struct cpsw_priv *priv = netdev_priv(ndev);
980 	struct cpsw_common *cpsw = priv->cpsw;
981 	int ret, slave_no;
982 	int flags = 0;
983 	u16 vid = 0;
984 
985 	slave_no = cpsw_slave_index(cpsw, priv);
986 	if (!is_valid_ether_addr(addr->sa_data))
987 		return -EADDRNOTAVAIL;
988 
989 	ret = pm_runtime_get_sync(cpsw->dev);
990 	if (ret < 0) {
991 		pm_runtime_put_noidle(cpsw->dev);
992 		return ret;
993 	}
994 
995 	vid = cpsw->slaves[slave_no].port_vlan;
996 	flags = ALE_VLAN | ALE_SECURE;
997 
998 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
999 			   flags, vid);
1000 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1001 			   flags, vid);
1002 
1003 	ether_addr_copy(priv->mac_addr, addr->sa_data);
1004 	ether_addr_copy(ndev->dev_addr, priv->mac_addr);
1005 	cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv);
1006 
1007 	pm_runtime_put(cpsw->dev);
1008 
1009 	return 0;
1010 }
1011 
1012 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1013 				     __be16 proto, u16 vid)
1014 {
1015 	struct cpsw_priv *priv = netdev_priv(ndev);
1016 	struct cpsw_common *cpsw = priv->cpsw;
1017 	int ret;
1018 	int i;
1019 
1020 	if (cpsw_is_switch_en(cpsw)) {
1021 		dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
1022 		return 0;
1023 	}
1024 
1025 	if (vid == cpsw->data.default_vlan)
1026 		return 0;
1027 
1028 	ret = pm_runtime_get_sync(cpsw->dev);
1029 	if (ret < 0) {
1030 		pm_runtime_put_noidle(cpsw->dev);
1031 		return ret;
1032 	}
1033 
1034 	for (i = 0; i < cpsw->data.slaves; i++) {
1035 		if (cpsw->slaves[i].ndev &&
1036 		    vid == cpsw->slaves[i].port_vlan)
1037 			goto err;
1038 	}
1039 
1040 	dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1041 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1042 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1043 			   HOST_PORT_NUM, ALE_VLAN, vid);
1044 	cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1045 			   0, ALE_VLAN, vid);
1046 	cpsw_ale_flush_multicast(cpsw->ale, 0, vid);
1047 err:
1048 	pm_runtime_put(cpsw->dev);
1049 	return ret;
1050 }
1051 
1052 static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
1053 				       size_t len)
1054 {
1055 	struct cpsw_priv *priv = netdev_priv(ndev);
1056 	int err;
1057 
1058 	err = snprintf(name, len, "p%d", priv->emac_port);
1059 
1060 	if (err >= len)
1061 		return -EINVAL;
1062 
1063 	return 0;
1064 }
1065 
1066 #ifdef CONFIG_NET_POLL_CONTROLLER
1067 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1068 {
1069 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1070 
1071 	cpsw_intr_disable(cpsw);
1072 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1073 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1074 	cpsw_intr_enable(cpsw);
1075 }
1076 #endif
1077 
1078 static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1079 			     struct xdp_frame **frames, u32 flags)
1080 {
1081 	struct cpsw_priv *priv = netdev_priv(ndev);
1082 	struct xdp_frame *xdpf;
1083 	int i, drops = 0;
1084 
1085 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1086 		return -EINVAL;
1087 
1088 	for (i = 0; i < n; i++) {
1089 		xdpf = frames[i];
1090 		if (xdpf->len < CPSW_MIN_PACKET_SIZE) {
1091 			xdp_return_frame_rx_napi(xdpf);
1092 			drops++;
1093 			continue;
1094 		}
1095 
1096 		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
1097 			drops++;
1098 	}
1099 
1100 	return n - drops;
1101 }
1102 
1103 static int cpsw_get_port_parent_id(struct net_device *ndev,
1104 				   struct netdev_phys_item_id *ppid)
1105 {
1106 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1107 
1108 	ppid->id_len = sizeof(cpsw->base_mac);
1109 	memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len);
1110 
1111 	return 0;
1112 }
1113 
1114 static const struct net_device_ops cpsw_netdev_ops = {
1115 	.ndo_open		= cpsw_ndo_open,
1116 	.ndo_stop		= cpsw_ndo_stop,
1117 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
1118 	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
1119 	.ndo_do_ioctl		= cpsw_ndo_ioctl,
1120 	.ndo_validate_addr	= eth_validate_addr,
1121 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
1122 	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
1123 	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
1124 #ifdef CONFIG_NET_POLL_CONTROLLER
1125 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
1126 #endif
1127 	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
1128 	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
1129 	.ndo_setup_tc           = cpsw_ndo_setup_tc,
1130 	.ndo_get_phys_port_name = cpsw_ndo_get_phys_port_name,
1131 	.ndo_bpf		= cpsw_ndo_bpf,
1132 	.ndo_xdp_xmit		= cpsw_ndo_xdp_xmit,
1133 	.ndo_get_port_parent_id	= cpsw_get_port_parent_id,
1134 };
1135 
1136 static void cpsw_get_drvinfo(struct net_device *ndev,
1137 			     struct ethtool_drvinfo *info)
1138 {
1139 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1140 	struct platform_device *pdev;
1141 
1142 	pdev = to_platform_device(cpsw->dev);
1143 	strlcpy(info->driver, "cpsw-switch", sizeof(info->driver));
1144 	strlcpy(info->version, "2.0", sizeof(info->version));
1145 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1146 }
1147 
1148 static int cpsw_set_pauseparam(struct net_device *ndev,
1149 			       struct ethtool_pauseparam *pause)
1150 {
1151 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1152 	struct cpsw_priv *priv = netdev_priv(ndev);
1153 	int slave_no;
1154 
1155 	slave_no = cpsw_slave_index(cpsw, priv);
1156 	if (!cpsw->slaves[slave_no].phy)
1157 		return -EINVAL;
1158 
1159 	if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
1160 		return -EINVAL;
1161 
1162 	priv->rx_pause = pause->rx_pause ? true : false;
1163 	priv->tx_pause = pause->tx_pause ? true : false;
1164 
1165 	phy_set_asym_pause(cpsw->slaves[slave_no].phy,
1166 			   priv->rx_pause, priv->tx_pause);
1167 
1168 	return 0;
1169 }
1170 
1171 static int cpsw_set_channels(struct net_device *ndev,
1172 			     struct ethtool_channels *chs)
1173 {
1174 	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1175 }
1176 
1177 static const struct ethtool_ops cpsw_ethtool_ops = {
1178 	.get_drvinfo		= cpsw_get_drvinfo,
1179 	.get_msglevel		= cpsw_get_msglevel,
1180 	.set_msglevel		= cpsw_set_msglevel,
1181 	.get_link		= ethtool_op_get_link,
1182 	.get_ts_info		= cpsw_get_ts_info,
1183 	.get_coalesce		= cpsw_get_coalesce,
1184 	.set_coalesce		= cpsw_set_coalesce,
1185 	.get_sset_count		= cpsw_get_sset_count,
1186 	.get_strings		= cpsw_get_strings,
1187 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
1188 	.get_pauseparam		= cpsw_get_pauseparam,
1189 	.set_pauseparam		= cpsw_set_pauseparam,
1190 	.get_wol		= cpsw_get_wol,
1191 	.set_wol		= cpsw_set_wol,
1192 	.get_regs_len		= cpsw_get_regs_len,
1193 	.get_regs		= cpsw_get_regs,
1194 	.begin			= cpsw_ethtool_op_begin,
1195 	.complete		= cpsw_ethtool_op_complete,
1196 	.get_channels		= cpsw_get_channels,
1197 	.set_channels		= cpsw_set_channels,
1198 	.get_link_ksettings	= cpsw_get_link_ksettings,
1199 	.set_link_ksettings	= cpsw_set_link_ksettings,
1200 	.get_eee		= cpsw_get_eee,
1201 	.set_eee		= cpsw_set_eee,
1202 	.nway_reset		= cpsw_nway_reset,
1203 	.get_ringparam		= cpsw_get_ringparam,
1204 	.set_ringparam		= cpsw_set_ringparam,
1205 };
1206 
1207 static int cpsw_probe_dt(struct cpsw_common *cpsw)
1208 {
1209 	struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np;
1210 	struct cpsw_platform_data *data = &cpsw->data;
1211 	struct device *dev = cpsw->dev;
1212 	int ret;
1213 	u32 prop;
1214 
1215 	if (!node)
1216 		return -EINVAL;
1217 
1218 	tmp_node = of_get_child_by_name(node, "ethernet-ports");
1219 	if (!tmp_node)
1220 		return -ENOENT;
1221 	data->slaves = of_get_child_count(tmp_node);
1222 	if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
1223 		of_node_put(tmp_node);
1224 		return -ENOENT;
1225 	}
1226 
1227 	data->active_slave = 0;
1228 	data->channels = CPSW_MAX_QUEUES;
1229 	data->ale_entries = CPSW_ALE_NUM_ENTRIES;
1230 	data->dual_emac = 1;
1231 	data->bd_ram_size = CPSW_BD_RAM_SIZE;
1232 	data->mac_control = 0;
1233 
1234 	data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM,
1235 					sizeof(struct cpsw_slave_data),
1236 					GFP_KERNEL);
1237 	if (!data->slave_data)
1238 		return -ENOMEM;
1239 
1240 	/* Populate all the child nodes here...
1241 	 */
1242 	ret = devm_of_platform_populate(dev);
1243 	/* We do not want to force this, as in some cases may not have child */
1244 	if (ret)
1245 		dev_warn(dev, "Doesn't have any child node\n");
1246 
1247 	for_each_child_of_node(tmp_node, port_np) {
1248 		struct cpsw_slave_data *slave_data;
1249 		const void *mac_addr;
1250 		u32 port_id;
1251 
1252 		ret = of_property_read_u32(port_np, "reg", &port_id);
1253 		if (ret < 0) {
1254 			dev_err(dev, "%pOF error reading port_id %d\n",
1255 				port_np, ret);
1256 			goto err_node_put;
1257 		}
1258 
1259 		if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
1260 			dev_err(dev, "%pOF has invalid port_id %u\n",
1261 				port_np, port_id);
1262 			ret = -EINVAL;
1263 			goto err_node_put;
1264 		}
1265 
1266 		slave_data = &data->slave_data[port_id - 1];
1267 
1268 		slave_data->disabled = !of_device_is_available(port_np);
1269 		if (slave_data->disabled)
1270 			continue;
1271 
1272 		slave_data->slave_node = port_np;
1273 		slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL);
1274 		if (IS_ERR(slave_data->ifphy)) {
1275 			ret = PTR_ERR(slave_data->ifphy);
1276 			dev_err(dev, "%pOF: Error retrieving port phy: %d\n",
1277 				port_np, ret);
1278 			goto err_node_put;
1279 		}
1280 
1281 		if (of_phy_is_fixed_link(port_np)) {
1282 			ret = of_phy_register_fixed_link(port_np);
1283 			if (ret) {
1284 				if (ret != -EPROBE_DEFER)
1285 					dev_err(dev, "%pOF failed to register fixed-link phy: %d\n",
1286 						port_np, ret);
1287 				goto err_node_put;
1288 			}
1289 			slave_data->phy_node = of_node_get(port_np);
1290 		} else {
1291 			slave_data->phy_node =
1292 				of_parse_phandle(port_np, "phy-handle", 0);
1293 		}
1294 
1295 		if (!slave_data->phy_node) {
1296 			dev_err(dev, "%pOF no phy found\n", port_np);
1297 			ret = -ENODEV;
1298 			goto err_node_put;
1299 		}
1300 
1301 		ret = of_get_phy_mode(port_np, &slave_data->phy_if);
1302 		if (ret) {
1303 			dev_err(dev, "%pOF read phy-mode err %d\n",
1304 				port_np, ret);
1305 			goto err_node_put;
1306 		}
1307 
1308 		mac_addr = of_get_mac_address(port_np);
1309 		if (!IS_ERR(mac_addr)) {
1310 			ether_addr_copy(slave_data->mac_addr, mac_addr);
1311 		} else {
1312 			ret = ti_cm_get_macid(dev, port_id - 1,
1313 					      slave_data->mac_addr);
1314 			if (ret)
1315 				goto err_node_put;
1316 		}
1317 
1318 		if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
1319 					 &prop)) {
1320 			dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
1321 				port_np);
1322 			slave_data->dual_emac_res_vlan = port_id;
1323 			dev_err(dev, "%pOF Using %d as Reserved VLAN\n",
1324 				port_np, slave_data->dual_emac_res_vlan);
1325 		} else {
1326 			slave_data->dual_emac_res_vlan = prop;
1327 		}
1328 	}
1329 
1330 	of_node_put(tmp_node);
1331 	return 0;
1332 
1333 err_node_put:
1334 	of_node_put(port_np);
1335 	return ret;
1336 }
1337 
1338 static void cpsw_remove_dt(struct cpsw_common *cpsw)
1339 {
1340 	struct cpsw_platform_data *data = &cpsw->data;
1341 	int i = 0;
1342 
1343 	for (i = 0; i < cpsw->data.slaves; i++) {
1344 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1345 		struct device_node *port_np = slave_data->phy_node;
1346 
1347 		if (port_np) {
1348 			if (of_phy_is_fixed_link(port_np))
1349 				of_phy_deregister_fixed_link(port_np);
1350 
1351 			of_node_put(port_np);
1352 		}
1353 	}
1354 }
1355 
1356 static int cpsw_create_ports(struct cpsw_common *cpsw)
1357 {
1358 	struct cpsw_platform_data *data = &cpsw->data;
1359 	struct net_device *ndev, *napi_ndev = NULL;
1360 	struct device *dev = cpsw->dev;
1361 	struct cpsw_priv *priv;
1362 	int ret = 0, i = 0;
1363 
1364 	for (i = 0; i < cpsw->data.slaves; i++) {
1365 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1366 
1367 		if (slave_data->disabled)
1368 			continue;
1369 
1370 		ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
1371 					       CPSW_MAX_QUEUES,
1372 					       CPSW_MAX_QUEUES);
1373 		if (!ndev) {
1374 			dev_err(dev, "error allocating net_device\n");
1375 			return -ENOMEM;
1376 		}
1377 
1378 		priv = netdev_priv(ndev);
1379 		priv->cpsw = cpsw;
1380 		priv->ndev = ndev;
1381 		priv->dev  = dev;
1382 		priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1383 		priv->emac_port = i + 1;
1384 
1385 		if (is_valid_ether_addr(slave_data->mac_addr)) {
1386 			ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1387 			dev_info(cpsw->dev, "Detected MACID = %pM\n",
1388 				 priv->mac_addr);
1389 		} else {
1390 			eth_random_addr(slave_data->mac_addr);
1391 			dev_info(cpsw->dev, "Random MACID = %pM\n",
1392 				 priv->mac_addr);
1393 		}
1394 		ether_addr_copy(ndev->dev_addr, slave_data->mac_addr);
1395 		ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1396 
1397 		cpsw->slaves[i].ndev = ndev;
1398 
1399 		ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
1400 				  NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_NETNS_LOCAL;
1401 
1402 		ndev->netdev_ops = &cpsw_netdev_ops;
1403 		ndev->ethtool_ops = &cpsw_ethtool_ops;
1404 		SET_NETDEV_DEV(ndev, dev);
1405 
1406 		if (!napi_ndev) {
1407 			/* CPSW Host port CPDMA interface is shared between
1408 			 * ports and there is only one TX and one RX IRQs
1409 			 * available for all possible TX and RX channels
1410 			 * accordingly.
1411 			 */
1412 			netif_napi_add(ndev, &cpsw->napi_rx,
1413 				       cpsw->quirk_irq ?
1414 				       cpsw_rx_poll : cpsw_rx_mq_poll,
1415 				       CPSW_POLL_WEIGHT);
1416 			netif_tx_napi_add(ndev, &cpsw->napi_tx,
1417 					  cpsw->quirk_irq ?
1418 					  cpsw_tx_poll : cpsw_tx_mq_poll,
1419 					  CPSW_POLL_WEIGHT);
1420 		}
1421 
1422 		napi_ndev = ndev;
1423 	}
1424 
1425 	return ret;
1426 }
1427 
1428 static void cpsw_unregister_ports(struct cpsw_common *cpsw)
1429 {
1430 	int i = 0;
1431 
1432 	for (i = 0; i < cpsw->data.slaves; i++) {
1433 		if (!cpsw->slaves[i].ndev)
1434 			continue;
1435 
1436 		unregister_netdev(cpsw->slaves[i].ndev);
1437 	}
1438 }
1439 
1440 static int cpsw_register_ports(struct cpsw_common *cpsw)
1441 {
1442 	int ret = 0, i = 0;
1443 
1444 	for (i = 0; i < cpsw->data.slaves; i++) {
1445 		if (!cpsw->slaves[i].ndev)
1446 			continue;
1447 
1448 		/* register the network device */
1449 		ret = register_netdev(cpsw->slaves[i].ndev);
1450 		if (ret) {
1451 			dev_err(cpsw->dev,
1452 				"cpsw: err registering net device%d\n", i);
1453 			cpsw->slaves[i].ndev = NULL;
1454 			break;
1455 		}
1456 	}
1457 
1458 	if (ret)
1459 		cpsw_unregister_ports(cpsw);
1460 	return ret;
1461 }
1462 
1463 bool cpsw_port_dev_check(const struct net_device *ndev)
1464 {
1465 	if (ndev->netdev_ops == &cpsw_netdev_ops) {
1466 		struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1467 
1468 		return !cpsw->data.dual_emac;
1469 	}
1470 
1471 	return false;
1472 }
1473 
1474 static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
1475 {
1476 	int set_val = 0;
1477 	int i;
1478 
1479 	if (!cpsw->ale_bypass &&
1480 	    (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
1481 		set_val = 1;
1482 
1483 	dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
1484 
1485 	for (i = 0; i < cpsw->data.slaves; i++) {
1486 		struct net_device *sl_ndev = cpsw->slaves[i].ndev;
1487 		struct cpsw_priv *priv = netdev_priv(sl_ndev);
1488 
1489 		priv->offload_fwd_mark = set_val;
1490 	}
1491 }
1492 
1493 static int cpsw_netdevice_port_link(struct net_device *ndev,
1494 				    struct net_device *br_ndev)
1495 {
1496 	struct cpsw_priv *priv = netdev_priv(ndev);
1497 	struct cpsw_common *cpsw = priv->cpsw;
1498 
1499 	if (!cpsw->br_members) {
1500 		cpsw->hw_bridge_dev = br_ndev;
1501 	} else {
1502 		/* This is adding the port to a second bridge, this is
1503 		 * unsupported
1504 		 */
1505 		if (cpsw->hw_bridge_dev != br_ndev)
1506 			return -EOPNOTSUPP;
1507 	}
1508 
1509 	cpsw->br_members |= BIT(priv->emac_port);
1510 
1511 	cpsw_port_offload_fwd_mark_update(cpsw);
1512 
1513 	return NOTIFY_DONE;
1514 }
1515 
1516 static void cpsw_netdevice_port_unlink(struct net_device *ndev)
1517 {
1518 	struct cpsw_priv *priv = netdev_priv(ndev);
1519 	struct cpsw_common *cpsw = priv->cpsw;
1520 
1521 	cpsw->br_members &= ~BIT(priv->emac_port);
1522 
1523 	cpsw_port_offload_fwd_mark_update(cpsw);
1524 
1525 	if (!cpsw->br_members)
1526 		cpsw->hw_bridge_dev = NULL;
1527 }
1528 
1529 /* netdev notifier */
1530 static int cpsw_netdevice_event(struct notifier_block *unused,
1531 				unsigned long event, void *ptr)
1532 {
1533 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1534 	struct netdev_notifier_changeupper_info *info;
1535 	int ret = NOTIFY_DONE;
1536 
1537 	if (!cpsw_port_dev_check(ndev))
1538 		return NOTIFY_DONE;
1539 
1540 	switch (event) {
1541 	case NETDEV_CHANGEUPPER:
1542 		info = ptr;
1543 
1544 		if (netif_is_bridge_master(info->upper_dev)) {
1545 			if (info->linking)
1546 				ret = cpsw_netdevice_port_link(ndev,
1547 							       info->upper_dev);
1548 			else
1549 				cpsw_netdevice_port_unlink(ndev);
1550 		}
1551 		break;
1552 	default:
1553 		return NOTIFY_DONE;
1554 	}
1555 
1556 	return notifier_from_errno(ret);
1557 }
1558 
1559 static struct notifier_block cpsw_netdevice_nb __read_mostly = {
1560 	.notifier_call = cpsw_netdevice_event,
1561 };
1562 
1563 static int cpsw_register_notifiers(struct cpsw_common *cpsw)
1564 {
1565 	int ret = 0;
1566 
1567 	ret = register_netdevice_notifier(&cpsw_netdevice_nb);
1568 	if (ret) {
1569 		dev_err(cpsw->dev, "can't register netdevice notifier\n");
1570 		return ret;
1571 	}
1572 
1573 	ret = cpsw_switchdev_register_notifiers(cpsw);
1574 	if (ret)
1575 		unregister_netdevice_notifier(&cpsw_netdevice_nb);
1576 
1577 	return ret;
1578 }
1579 
1580 static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
1581 {
1582 	cpsw_switchdev_unregister_notifiers(cpsw);
1583 	unregister_netdevice_notifier(&cpsw_netdevice_nb);
1584 }
1585 
1586 static const struct devlink_ops cpsw_devlink_ops = {
1587 };
1588 
1589 static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
1590 				   struct devlink_param_gset_ctx *ctx)
1591 {
1592 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1593 	struct cpsw_common *cpsw = dl_priv->cpsw;
1594 
1595 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1596 
1597 	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1598 		return  -EOPNOTSUPP;
1599 
1600 	ctx->val.vbool = !cpsw->data.dual_emac;
1601 
1602 	return 0;
1603 }
1604 
1605 static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
1606 				   struct devlink_param_gset_ctx *ctx)
1607 {
1608 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1609 	struct cpsw_common *cpsw = dl_priv->cpsw;
1610 	int vlan = cpsw->data.default_vlan;
1611 	bool switch_en = ctx->val.vbool;
1612 	bool if_running = false;
1613 	int i;
1614 
1615 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1616 
1617 	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1618 		return  -EOPNOTSUPP;
1619 
1620 	if (switch_en == !cpsw->data.dual_emac)
1621 		return 0;
1622 
1623 	if (!switch_en && cpsw->br_members) {
1624 		dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
1625 		return -EINVAL;
1626 	}
1627 
1628 	rtnl_lock();
1629 
1630 	for (i = 0; i < cpsw->data.slaves; i++) {
1631 		struct cpsw_slave *slave = &cpsw->slaves[i];
1632 		struct net_device *sl_ndev = slave->ndev;
1633 
1634 		if (!sl_ndev || !netif_running(sl_ndev))
1635 			continue;
1636 
1637 		if_running = true;
1638 	}
1639 
1640 	if (!if_running) {
1641 		/* all ndevs are down */
1642 		cpsw->data.dual_emac = !switch_en;
1643 		for (i = 0; i < cpsw->data.slaves; i++) {
1644 			struct cpsw_slave *slave = &cpsw->slaves[i];
1645 			struct net_device *sl_ndev = slave->ndev;
1646 			struct cpsw_priv *priv;
1647 
1648 			if (!sl_ndev)
1649 				continue;
1650 
1651 			priv = netdev_priv(sl_ndev);
1652 			if (switch_en)
1653 				vlan = cpsw->data.default_vlan;
1654 			else
1655 				vlan = slave->data->dual_emac_res_vlan;
1656 			slave->port_vlan = vlan;
1657 		}
1658 		goto exit;
1659 	}
1660 
1661 	if (switch_en) {
1662 		dev_info(cpsw->dev, "Enable switch mode\n");
1663 
1664 		/* enable bypass - no forwarding; all traffic goes to Host */
1665 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1666 
1667 		/* clean up ALE table */
1668 		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1669 		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1670 
1671 		cpsw_init_host_port_switch(cpsw);
1672 
1673 		for (i = 0; i < cpsw->data.slaves; i++) {
1674 			struct cpsw_slave *slave = &cpsw->slaves[i];
1675 			struct net_device *sl_ndev = slave->ndev;
1676 			struct cpsw_priv *priv;
1677 
1678 			if (!sl_ndev)
1679 				continue;
1680 
1681 			priv = netdev_priv(sl_ndev);
1682 			slave->port_vlan = vlan;
1683 			if (netif_running(sl_ndev))
1684 				cpsw_port_add_switch_def_ale_entries(priv,
1685 								     slave);
1686 		}
1687 
1688 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1689 		cpsw->data.dual_emac = false;
1690 	} else {
1691 		dev_info(cpsw->dev, "Disable switch mode\n");
1692 
1693 		/* enable bypass - no forwarding; all traffic goes to Host */
1694 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1695 
1696 		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1697 		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1698 
1699 		cpsw_init_host_port_dual_mac(cpsw);
1700 
1701 		for (i = 0; i < cpsw->data.slaves; i++) {
1702 			struct cpsw_slave *slave = &cpsw->slaves[i];
1703 			struct net_device *sl_ndev = slave->ndev;
1704 			struct cpsw_priv *priv;
1705 
1706 			if (!sl_ndev)
1707 				continue;
1708 
1709 			priv = netdev_priv(slave->ndev);
1710 			slave->port_vlan = slave->data->dual_emac_res_vlan;
1711 			cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
1712 		}
1713 
1714 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1715 		cpsw->data.dual_emac = true;
1716 	}
1717 exit:
1718 	rtnl_unlock();
1719 
1720 	return 0;
1721 }
1722 
1723 static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
1724 				struct devlink_param_gset_ctx *ctx)
1725 {
1726 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1727 	struct cpsw_common *cpsw = dl_priv->cpsw;
1728 
1729 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1730 
1731 	switch (id) {
1732 	case CPSW_DL_PARAM_ALE_BYPASS:
1733 		ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
1734 		break;
1735 	default:
1736 		return -EOPNOTSUPP;
1737 	}
1738 
1739 	return 0;
1740 }
1741 
1742 static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
1743 				struct devlink_param_gset_ctx *ctx)
1744 {
1745 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1746 	struct cpsw_common *cpsw = dl_priv->cpsw;
1747 	int ret = -EOPNOTSUPP;
1748 
1749 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1750 
1751 	switch (id) {
1752 	case CPSW_DL_PARAM_ALE_BYPASS:
1753 		ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
1754 					   ctx->val.vbool);
1755 		if (!ret) {
1756 			cpsw->ale_bypass = ctx->val.vbool;
1757 			cpsw_port_offload_fwd_mark_update(cpsw);
1758 		}
1759 		break;
1760 	default:
1761 		return -EOPNOTSUPP;
1762 	}
1763 
1764 	return 0;
1765 }
1766 
1767 static const struct devlink_param cpsw_devlink_params[] = {
1768 	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
1769 			     "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
1770 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1771 			     cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
1772 			     NULL),
1773 	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
1774 			     "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
1775 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1776 			     cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
1777 };
1778 
1779 static int cpsw_register_devlink(struct cpsw_common *cpsw)
1780 {
1781 	struct device *dev = cpsw->dev;
1782 	struct cpsw_devlink *dl_priv;
1783 	int ret = 0;
1784 
1785 	cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv));
1786 	if (!cpsw->devlink)
1787 		return -ENOMEM;
1788 
1789 	dl_priv = devlink_priv(cpsw->devlink);
1790 	dl_priv->cpsw = cpsw;
1791 
1792 	ret = devlink_register(cpsw->devlink, dev);
1793 	if (ret) {
1794 		dev_err(dev, "DL reg fail ret:%d\n", ret);
1795 		goto dl_free;
1796 	}
1797 
1798 	ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
1799 				      ARRAY_SIZE(cpsw_devlink_params));
1800 	if (ret) {
1801 		dev_err(dev, "DL params reg fail ret:%d\n", ret);
1802 		goto dl_unreg;
1803 	}
1804 
1805 	devlink_params_publish(cpsw->devlink);
1806 	return ret;
1807 
1808 dl_unreg:
1809 	devlink_unregister(cpsw->devlink);
1810 dl_free:
1811 	devlink_free(cpsw->devlink);
1812 	return ret;
1813 }
1814 
1815 static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
1816 {
1817 	devlink_params_unpublish(cpsw->devlink);
1818 	devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
1819 				  ARRAY_SIZE(cpsw_devlink_params));
1820 	devlink_unregister(cpsw->devlink);
1821 	devlink_free(cpsw->devlink);
1822 }
1823 
1824 static const struct of_device_id cpsw_of_mtable[] = {
1825 	{ .compatible = "ti,cpsw-switch"},
1826 	{ .compatible = "ti,am335x-cpsw-switch"},
1827 	{ .compatible = "ti,am4372-cpsw-switch"},
1828 	{ .compatible = "ti,dra7-cpsw-switch"},
1829 	{ /* sentinel */ },
1830 };
1831 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
1832 
1833 static const struct soc_device_attribute cpsw_soc_devices[] = {
1834 	{ .family = "AM33xx", .revision = "ES1.0"},
1835 	{ /* sentinel */ }
1836 };
1837 
1838 static int cpsw_probe(struct platform_device *pdev)
1839 {
1840 	const struct soc_device_attribute *soc;
1841 	struct device *dev = &pdev->dev;
1842 	struct cpsw_common *cpsw;
1843 	struct resource *ss_res;
1844 	struct gpio_descs *mode;
1845 	void __iomem *ss_regs;
1846 	int ret = 0, ch;
1847 	struct clk *clk;
1848 	int irq;
1849 
1850 	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
1851 	if (!cpsw)
1852 		return -ENOMEM;
1853 
1854 	cpsw_slave_index = cpsw_slave_index_priv;
1855 
1856 	cpsw->dev = dev;
1857 
1858 	cpsw->slaves = devm_kcalloc(dev,
1859 				    CPSW_SLAVE_PORTS_NUM,
1860 				    sizeof(struct cpsw_slave),
1861 				    GFP_KERNEL);
1862 	if (!cpsw->slaves)
1863 		return -ENOMEM;
1864 
1865 	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
1866 	if (IS_ERR(mode)) {
1867 		ret = PTR_ERR(mode);
1868 		dev_err(dev, "gpio request failed, ret %d\n", ret);
1869 		return ret;
1870 	}
1871 
1872 	clk = devm_clk_get(dev, "fck");
1873 	if (IS_ERR(clk)) {
1874 		ret = PTR_ERR(clk);
1875 		dev_err(dev, "fck is not found %d\n", ret);
1876 		return ret;
1877 	}
1878 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
1879 
1880 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1881 	ss_regs = devm_ioremap_resource(dev, ss_res);
1882 	if (IS_ERR(ss_regs)) {
1883 		ret = PTR_ERR(ss_regs);
1884 		return ret;
1885 	}
1886 	cpsw->regs = ss_regs;
1887 
1888 	irq = platform_get_irq_byname(pdev, "rx");
1889 	if (irq < 0)
1890 		return irq;
1891 	cpsw->irqs_table[0] = irq;
1892 
1893 	irq = platform_get_irq_byname(pdev, "tx");
1894 	if (irq < 0)
1895 		return irq;
1896 	cpsw->irqs_table[1] = irq;
1897 
1898 	platform_set_drvdata(pdev, cpsw);
1899 	/* This may be required here for child devices. */
1900 	pm_runtime_enable(dev);
1901 
1902 	/* Need to enable clocks with runtime PM api to access module
1903 	 * registers
1904 	 */
1905 	ret = pm_runtime_get_sync(dev);
1906 	if (ret < 0) {
1907 		pm_runtime_put_noidle(dev);
1908 		pm_runtime_disable(dev);
1909 		return ret;
1910 	}
1911 
1912 	ret = cpsw_probe_dt(cpsw);
1913 	if (ret)
1914 		goto clean_dt_ret;
1915 
1916 	soc = soc_device_match(cpsw_soc_devices);
1917 	if (soc)
1918 		cpsw->quirk_irq = 1;
1919 
1920 	cpsw->rx_packet_max = rx_packet_max;
1921 	cpsw->descs_pool_size = descs_pool_size;
1922 	eth_random_addr(cpsw->base_mac);
1923 
1924 	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1925 			       (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
1926 			       descs_pool_size);
1927 	if (ret)
1928 		goto clean_dt_ret;
1929 
1930 	cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
1931 			ss_regs + CPSW1_WR_OFFSET :
1932 			ss_regs + CPSW2_WR_OFFSET;
1933 
1934 	ch = cpsw->quirk_irq ? 0 : 7;
1935 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
1936 	if (IS_ERR(cpsw->txv[0].ch)) {
1937 		dev_err(dev, "error initializing tx dma channel\n");
1938 		ret = PTR_ERR(cpsw->txv[0].ch);
1939 		goto clean_cpts;
1940 	}
1941 
1942 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
1943 	if (IS_ERR(cpsw->rxv[0].ch)) {
1944 		dev_err(dev, "error initializing rx dma channel\n");
1945 		ret = PTR_ERR(cpsw->rxv[0].ch);
1946 		goto clean_cpts;
1947 	}
1948 	cpsw_split_res(cpsw);
1949 
1950 	/* setup netdevs */
1951 	ret = cpsw_create_ports(cpsw);
1952 	if (ret)
1953 		goto clean_unregister_netdev;
1954 
1955 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1956 	 * MISC IRQs which are always kept disabled with this driver so
1957 	 * we will not request them.
1958 	 *
1959 	 * If anyone wants to implement support for those, make sure to
1960 	 * first request and append them to irqs_table array.
1961 	 */
1962 
1963 	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1964 			       0, dev_name(dev), cpsw);
1965 	if (ret < 0) {
1966 		dev_err(dev, "error attaching irq (%d)\n", ret);
1967 		goto clean_unregister_netdev;
1968 	}
1969 
1970 	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1971 			       0, dev_name(dev), cpsw);
1972 	if (ret < 0) {
1973 		dev_err(dev, "error attaching irq (%d)\n", ret);
1974 		goto clean_unregister_netdev;
1975 	}
1976 
1977 	ret = cpsw_register_notifiers(cpsw);
1978 	if (ret)
1979 		goto clean_unregister_netdev;
1980 
1981 	ret = cpsw_register_devlink(cpsw);
1982 	if (ret)
1983 		goto clean_unregister_notifiers;
1984 
1985 	ret = cpsw_register_ports(cpsw);
1986 	if (ret)
1987 		goto clean_unregister_notifiers;
1988 
1989 	dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
1990 		   &ss_res->start, descs_pool_size,
1991 		   cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
1992 		   CPSW_MINOR_VERSION(cpsw->version),
1993 		   CPSW_RTL_VERSION(cpsw->version));
1994 
1995 	pm_runtime_put(dev);
1996 
1997 	return 0;
1998 
1999 clean_unregister_notifiers:
2000 	cpsw_unregister_notifiers(cpsw);
2001 clean_unregister_netdev:
2002 	cpsw_unregister_ports(cpsw);
2003 clean_cpts:
2004 	cpts_release(cpsw->cpts);
2005 	cpdma_ctlr_destroy(cpsw->dma);
2006 clean_dt_ret:
2007 	cpsw_remove_dt(cpsw);
2008 	pm_runtime_put_sync(dev);
2009 	pm_runtime_disable(dev);
2010 	return ret;
2011 }
2012 
2013 static int cpsw_remove(struct platform_device *pdev)
2014 {
2015 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
2016 	int ret;
2017 
2018 	ret = pm_runtime_get_sync(&pdev->dev);
2019 	if (ret < 0) {
2020 		pm_runtime_put_noidle(&pdev->dev);
2021 		return ret;
2022 	}
2023 
2024 	cpsw_unregister_notifiers(cpsw);
2025 	cpsw_unregister_devlink(cpsw);
2026 	cpsw_unregister_ports(cpsw);
2027 
2028 	cpts_release(cpsw->cpts);
2029 	cpdma_ctlr_destroy(cpsw->dma);
2030 	cpsw_remove_dt(cpsw);
2031 	pm_runtime_put_sync(&pdev->dev);
2032 	pm_runtime_disable(&pdev->dev);
2033 	return 0;
2034 }
2035 
2036 static struct platform_driver cpsw_driver = {
2037 	.driver = {
2038 		.name	 = "cpsw-switch",
2039 		.of_match_table = cpsw_of_mtable,
2040 	},
2041 	.probe = cpsw_probe,
2042 	.remove = cpsw_remove,
2043 };
2044 
2045 module_platform_driver(cpsw_driver);
2046 
2047 MODULE_LICENSE("GPL");
2048 MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");
2049