xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw_new.c (revision f95fc014)
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 		xdp.frame_sz = PAGE_SIZE;
352 
353 		ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port);
354 		if (ret != CPSW_XDP_PASS)
355 			goto requeue;
356 
357 		/* XDP prog might have changed packet data and boundaries */
358 		len = xdp.data_end - xdp.data;
359 		headroom = xdp.data - xdp.data_hard_start;
360 
361 		/* XDP prog can modify vlan tag, so can't use encap header */
362 		status &= ~CPDMA_RX_VLAN_ENCAP;
363 	}
364 
365 	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
366 	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
367 	if (!skb) {
368 		ndev->stats.rx_dropped++;
369 		page_pool_recycle_direct(pool, page);
370 		goto requeue;
371 	}
372 
373 	skb->offload_fwd_mark = priv->offload_fwd_mark;
374 	skb_reserve(skb, headroom);
375 	skb_put(skb, len);
376 	skb->dev = ndev;
377 	if (status & CPDMA_RX_VLAN_ENCAP)
378 		cpsw_rx_vlan_encap(skb);
379 	if (priv->rx_ts_enabled)
380 		cpts_rx_timestamp(cpsw->cpts, skb);
381 	skb->protocol = eth_type_trans(skb, ndev);
382 
383 	/* unmap page as no netstack skb page recycling */
384 	page_pool_release_page(pool, page);
385 	netif_receive_skb(skb);
386 
387 	ndev->stats.rx_bytes += len;
388 	ndev->stats.rx_packets++;
389 
390 requeue:
391 	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
392 	xmeta->ndev = ndev;
393 	xmeta->ch = ch;
394 
395 	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
396 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
397 				       pkt_size, 0);
398 	if (ret < 0) {
399 		WARN_ON(ret == -ENOMEM);
400 		page_pool_recycle_direct(pool, new_page);
401 	}
402 }
403 
404 static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
405 				   unsigned short vid)
406 {
407 	struct cpsw_common *cpsw = priv->cpsw;
408 	int unreg_mcast_mask = 0;
409 	int mcast_mask;
410 	u32 port_mask;
411 	int ret;
412 
413 	port_mask = (1 << priv->emac_port) | ALE_PORT_HOST;
414 
415 	mcast_mask = ALE_PORT_HOST;
416 	if (priv->ndev->flags & IFF_ALLMULTI)
417 		unreg_mcast_mask = mcast_mask;
418 
419 	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
420 				unreg_mcast_mask);
421 	if (ret != 0)
422 		return ret;
423 
424 	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
425 				 HOST_PORT_NUM, ALE_VLAN, vid);
426 	if (ret != 0)
427 		goto clean_vid;
428 
429 	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
430 				 mcast_mask, ALE_VLAN, vid, 0);
431 	if (ret != 0)
432 		goto clean_vlan_ucast;
433 	return 0;
434 
435 clean_vlan_ucast:
436 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
437 			   HOST_PORT_NUM, ALE_VLAN, vid);
438 clean_vid:
439 	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
440 	return ret;
441 }
442 
443 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
444 				    __be16 proto, u16 vid)
445 {
446 	struct cpsw_priv *priv = netdev_priv(ndev);
447 	struct cpsw_common *cpsw = priv->cpsw;
448 	int ret, i;
449 
450 	if (cpsw_is_switch_en(cpsw)) {
451 		dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n");
452 		return 0;
453 	}
454 
455 	if (vid == cpsw->data.default_vlan)
456 		return 0;
457 
458 	ret = pm_runtime_get_sync(cpsw->dev);
459 	if (ret < 0) {
460 		pm_runtime_put_noidle(cpsw->dev);
461 		return ret;
462 	}
463 
464 	/* In dual EMAC, reserved VLAN id should not be used for
465 	 * creating VLAN interfaces as this can break the dual
466 	 * EMAC port separation
467 	 */
468 	for (i = 0; i < cpsw->data.slaves; i++) {
469 		if (cpsw->slaves[i].ndev &&
470 		    vid == cpsw->slaves[i].port_vlan) {
471 			ret = -EINVAL;
472 			goto err;
473 		}
474 	}
475 
476 	dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
477 	ret = cpsw_add_vlan_ale_entry(priv, vid);
478 err:
479 	pm_runtime_put(cpsw->dev);
480 	return ret;
481 }
482 
483 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
484 {
485 	struct cpsw_priv *priv = arg;
486 
487 	if (!vdev || !vid)
488 		return 0;
489 
490 	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
491 	return 0;
492 }
493 
494 /* restore resources after port reset */
495 static void cpsw_restore(struct cpsw_priv *priv)
496 {
497 	struct cpsw_common *cpsw = priv->cpsw;
498 
499 	/* restore vlan configurations */
500 	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
501 
502 	/* restore MQPRIO offload */
503 	cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
504 
505 	/* restore CBS offload */
506 	cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
507 }
508 
509 static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
510 {
511 	char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0};
512 
513 	cpsw_ale_add_mcast(cpsw->ale, stpa,
514 			   ALE_PORT_HOST, ALE_SUPER, 0,
515 			   ALE_MCAST_BLOCK_LEARN_FWD);
516 }
517 
518 static void cpsw_init_host_port_switch(struct cpsw_common *cpsw)
519 {
520 	int vlan = cpsw->data.default_vlan;
521 
522 	writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl);
523 
524 	writel(vlan, &cpsw->host_port_regs->port_vlan);
525 
526 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
527 			  ALE_ALL_PORTS, ALE_ALL_PORTS,
528 			  ALE_PORT_1 | ALE_PORT_2);
529 
530 	cpsw_init_stp_ale_entry(cpsw);
531 
532 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
533 	dev_dbg(cpsw->dev, "Set P0_UNI_FLOOD\n");
534 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
535 }
536 
537 static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw)
538 {
539 	int vlan = cpsw->data.default_vlan;
540 
541 	writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl);
542 
543 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
544 	dev_dbg(cpsw->dev, "unset P0_UNI_FLOOD\n");
545 
546 	writel(vlan, &cpsw->host_port_regs->port_vlan);
547 
548 	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
549 	/* learning make no sense in dual_mac mode */
550 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
551 }
552 
553 static void cpsw_init_host_port(struct cpsw_priv *priv)
554 {
555 	struct cpsw_common *cpsw = priv->cpsw;
556 	u32 control_reg;
557 
558 	/* soft reset the controller and initialize ale */
559 	soft_reset("cpsw", &cpsw->regs->soft_reset);
560 	cpsw_ale_start(cpsw->ale);
561 
562 	/* switch to vlan unaware mode */
563 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
564 			     CPSW_ALE_VLAN_AWARE);
565 	control_reg = readl(&cpsw->regs->control);
566 	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
567 	writel(control_reg, &cpsw->regs->control);
568 
569 	/* setup host port priority mapping */
570 	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
571 		       &cpsw->host_port_regs->cpdma_tx_pri_map);
572 	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
573 
574 	/* disable priority elevation */
575 	writel_relaxed(0, &cpsw->regs->ptype);
576 
577 	/* enable statistics collection only on all ports */
578 	writel_relaxed(0x7, &cpsw->regs->stat_port_en);
579 
580 	/* Enable internal fifo flow control */
581 	writel(0x7, &cpsw->regs->flow_control);
582 
583 	if (cpsw_is_switch_en(cpsw))
584 		cpsw_init_host_port_switch(cpsw);
585 	else
586 		cpsw_init_host_port_dual_mac(cpsw);
587 
588 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
589 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
590 }
591 
592 static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv,
593 						    struct cpsw_slave *slave)
594 {
595 	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
596 	struct cpsw_common *cpsw = priv->cpsw;
597 	u32 reg;
598 
599 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
600 	       CPSW2_PORT_VLAN;
601 	slave_write(slave, slave->port_vlan, reg);
602 
603 	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
604 			  port_mask, port_mask, 0);
605 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
606 			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan,
607 			   ALE_MCAST_FWD);
608 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
609 			   HOST_PORT_NUM, ALE_VLAN |
610 			   ALE_SECURE, slave->port_vlan);
611 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
612 			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
613 	/* learning make no sense in dual_mac mode */
614 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
615 			     ALE_PORT_NOLEARN, 1);
616 }
617 
618 static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv,
619 						 struct cpsw_slave *slave)
620 {
621 	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
622 	struct cpsw_common *cpsw = priv->cpsw;
623 	u32 reg;
624 
625 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
626 			     ALE_PORT_DROP_UNKNOWN_VLAN, 0);
627 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
628 			     ALE_PORT_NOLEARN, 0);
629 	/* disabling SA_UPDATE required to make stp work, without this setting
630 	 * Host MAC addresses will jump between ports.
631 	 * As per TRM MAC address can be defined as unicast supervisory (super)
632 	 * by setting both (ALE_BLOCKED | ALE_SECURE) which should prevent
633 	 * SA_UPDATE, but HW seems works incorrectly and setting ALE_SECURE
634 	 * causes STP packets to be dropped due to ingress filter
635 	 *	if (source address found) and (secure) and
636 	 *	   (receive port number != port_number))
637 	 *	   then discard the packet
638 	 */
639 	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
640 			     ALE_PORT_NO_SA_UPDATE, 1);
641 
642 	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
643 			   port_mask, ALE_VLAN, slave->port_vlan,
644 			   ALE_MCAST_FWD_2);
645 	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
646 			   HOST_PORT_NUM, ALE_VLAN, slave->port_vlan);
647 
648 	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
649 	       CPSW2_PORT_VLAN;
650 	slave_write(slave, slave->port_vlan, reg);
651 }
652 
653 static void cpsw_adjust_link(struct net_device *ndev)
654 {
655 	struct cpsw_priv *priv = netdev_priv(ndev);
656 	struct cpsw_common *cpsw = priv->cpsw;
657 	struct cpsw_slave *slave;
658 	struct phy_device *phy;
659 	u32 mac_control = 0;
660 
661 	slave = &cpsw->slaves[priv->emac_port - 1];
662 	phy = slave->phy;
663 
664 	if (!phy)
665 		return;
666 
667 	if (phy->link) {
668 		mac_control = CPSW_SL_CTL_GMII_EN;
669 
670 		if (phy->speed == 1000)
671 			mac_control |= CPSW_SL_CTL_GIG;
672 		if (phy->duplex)
673 			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
674 
675 		/* set speed_in input in case RMII mode is used in 100Mbps */
676 		if (phy->speed == 100)
677 			mac_control |= CPSW_SL_CTL_IFCTL_A;
678 		/* in band mode only works in 10Mbps RGMII mode */
679 		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
680 			mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
681 
682 		if (priv->rx_pause)
683 			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
684 
685 		if (priv->tx_pause)
686 			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
687 
688 		if (mac_control != slave->mac_control)
689 			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
690 
691 		/* enable forwarding */
692 		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
693 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
694 
695 		netif_tx_wake_all_queues(ndev);
696 
697 		if (priv->shp_cfg_speed &&
698 		    priv->shp_cfg_speed != slave->phy->speed &&
699 		    !cpsw_shp_is_off(priv))
700 			dev_warn(priv->dev, "Speed was changed, CBS shaper speeds are changed!");
701 	} else {
702 		netif_tx_stop_all_queues(ndev);
703 
704 		mac_control = 0;
705 		/* disable forwarding */
706 		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
707 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
708 
709 		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
710 
711 		cpsw_sl_ctl_reset(slave->mac_sl);
712 	}
713 
714 	if (mac_control != slave->mac_control)
715 		phy_print_status(phy);
716 
717 	slave->mac_control = mac_control;
718 
719 	if (phy->link && cpsw_need_resplit(cpsw))
720 		cpsw_split_res(cpsw);
721 }
722 
723 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
724 {
725 	struct cpsw_common *cpsw = priv->cpsw;
726 	struct phy_device *phy;
727 
728 	cpsw_sl_reset(slave->mac_sl, 100);
729 	cpsw_sl_ctl_reset(slave->mac_sl);
730 
731 	/* setup priority mapping */
732 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
733 			  RX_PRIORITY_MAPPING);
734 
735 	switch (cpsw->version) {
736 	case CPSW_VERSION_1:
737 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
738 		/* Increase RX FIFO size to 5 for supporting fullduplex
739 		 * flow control mode
740 		 */
741 		slave_write(slave,
742 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
743 			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
744 		break;
745 	case CPSW_VERSION_2:
746 	case CPSW_VERSION_3:
747 	case CPSW_VERSION_4:
748 		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
749 		/* Increase RX FIFO size to 5 for supporting fullduplex
750 		 * flow control mode
751 		 */
752 		slave_write(slave,
753 			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
754 			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
755 		break;
756 	}
757 
758 	/* setup max packet size, and mac address */
759 	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
760 			  cpsw->rx_packet_max);
761 	cpsw_set_slave_mac(slave, priv);
762 
763 	slave->mac_control = 0;	/* no link yet */
764 
765 	if (cpsw_is_switch_en(cpsw))
766 		cpsw_port_add_switch_def_ale_entries(priv, slave);
767 	else
768 		cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
769 
770 	if (!slave->data->phy_node)
771 		dev_err(priv->dev, "no phy found on slave %d\n",
772 			slave->slave_num);
773 	phy = of_phy_connect(priv->ndev, slave->data->phy_node,
774 			     &cpsw_adjust_link, 0, slave->data->phy_if);
775 	if (!phy) {
776 		dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
777 			slave->data->phy_node,
778 			slave->slave_num);
779 		return;
780 	}
781 	slave->phy = phy;
782 
783 	phy_attached_info(slave->phy);
784 
785 	phy_start(slave->phy);
786 
787 	/* Configure GMII_SEL register */
788 	phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
789 			 slave->data->phy_if);
790 }
791 
792 static int cpsw_ndo_stop(struct net_device *ndev)
793 {
794 	struct cpsw_priv *priv = netdev_priv(ndev);
795 	struct cpsw_common *cpsw = priv->cpsw;
796 	struct cpsw_slave *slave;
797 
798 	cpsw_info(priv, ifdown, "shutting down ndev\n");
799 	slave = &cpsw->slaves[priv->emac_port - 1];
800 	if (slave->phy)
801 		phy_stop(slave->phy);
802 
803 	netif_tx_stop_all_queues(priv->ndev);
804 
805 	if (slave->phy) {
806 		phy_disconnect(slave->phy);
807 		slave->phy = NULL;
808 	}
809 
810 	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
811 
812 	if (cpsw->usage_count <= 1) {
813 		napi_disable(&cpsw->napi_rx);
814 		napi_disable(&cpsw->napi_tx);
815 		cpts_unregister(cpsw->cpts);
816 		cpsw_intr_disable(cpsw);
817 		cpdma_ctlr_stop(cpsw->dma);
818 		cpsw_ale_stop(cpsw->ale);
819 		cpsw_destroy_xdp_rxqs(cpsw);
820 	}
821 
822 	if (cpsw_need_resplit(cpsw))
823 		cpsw_split_res(cpsw);
824 
825 	cpsw->usage_count--;
826 	pm_runtime_put_sync(cpsw->dev);
827 	return 0;
828 }
829 
830 static int cpsw_ndo_open(struct net_device *ndev)
831 {
832 	struct cpsw_priv *priv = netdev_priv(ndev);
833 	struct cpsw_common *cpsw = priv->cpsw;
834 	int ret;
835 
836 	dev_info(priv->dev, "starting ndev. mode: %s\n",
837 		 cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac");
838 	ret = pm_runtime_get_sync(cpsw->dev);
839 	if (ret < 0) {
840 		pm_runtime_put_noidle(cpsw->dev);
841 		return ret;
842 	}
843 
844 	/* Notify the stack of the actual queue counts. */
845 	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
846 	if (ret) {
847 		dev_err(priv->dev, "cannot set real number of tx queues\n");
848 		goto pm_cleanup;
849 	}
850 
851 	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
852 	if (ret) {
853 		dev_err(priv->dev, "cannot set real number of rx queues\n");
854 		goto pm_cleanup;
855 	}
856 
857 	/* Initialize host and slave ports */
858 	if (!cpsw->usage_count)
859 		cpsw_init_host_port(priv);
860 	cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv);
861 
862 	/* initialize shared resources for every ndev */
863 	if (!cpsw->usage_count) {
864 		/* create rxqs for both infs in dual mac as they use same pool
865 		 * and must be destroyed together when no users.
866 		 */
867 		ret = cpsw_create_xdp_rxqs(cpsw);
868 		if (ret < 0)
869 			goto err_cleanup;
870 
871 		ret = cpsw_fill_rx_channels(priv);
872 		if (ret < 0)
873 			goto err_cleanup;
874 
875 		if (cpts_register(cpsw->cpts))
876 			dev_err(priv->dev, "error registering cpts device\n");
877 
878 		napi_enable(&cpsw->napi_rx);
879 		napi_enable(&cpsw->napi_tx);
880 
881 		if (cpsw->tx_irq_disabled) {
882 			cpsw->tx_irq_disabled = false;
883 			enable_irq(cpsw->irqs_table[1]);
884 		}
885 
886 		if (cpsw->rx_irq_disabled) {
887 			cpsw->rx_irq_disabled = false;
888 			enable_irq(cpsw->irqs_table[0]);
889 		}
890 	}
891 
892 	cpsw_restore(priv);
893 
894 	/* Enable Interrupt pacing if configured */
895 	if (cpsw->coal_intvl != 0) {
896 		struct ethtool_coalesce coal;
897 
898 		coal.rx_coalesce_usecs = cpsw->coal_intvl;
899 		cpsw_set_coalesce(ndev, &coal);
900 	}
901 
902 	cpdma_ctlr_start(cpsw->dma);
903 	cpsw_intr_enable(cpsw);
904 	cpsw->usage_count++;
905 
906 	return 0;
907 
908 err_cleanup:
909 	cpsw_ndo_stop(ndev);
910 
911 pm_cleanup:
912 	pm_runtime_put_sync(cpsw->dev);
913 	return ret;
914 }
915 
916 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
917 				       struct net_device *ndev)
918 {
919 	struct cpsw_priv *priv = netdev_priv(ndev);
920 	struct cpsw_common *cpsw = priv->cpsw;
921 	struct cpts *cpts = cpsw->cpts;
922 	struct netdev_queue *txq;
923 	struct cpdma_chan *txch;
924 	int ret, q_idx;
925 
926 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
927 		cpsw_err(priv, tx_err, "packet pad failed\n");
928 		ndev->stats.tx_dropped++;
929 		return NET_XMIT_DROP;
930 	}
931 
932 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
933 	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
934 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
935 
936 	q_idx = skb_get_queue_mapping(skb);
937 	if (q_idx >= cpsw->tx_ch_num)
938 		q_idx = q_idx % cpsw->tx_ch_num;
939 
940 	txch = cpsw->txv[q_idx].ch;
941 	txq = netdev_get_tx_queue(ndev, q_idx);
942 	skb_tx_timestamp(skb);
943 	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
944 				priv->emac_port);
945 	if (unlikely(ret != 0)) {
946 		cpsw_err(priv, tx_err, "desc submit failed\n");
947 		goto fail;
948 	}
949 
950 	/* If there is no more tx desc left free then we need to
951 	 * tell the kernel to stop sending us tx frames.
952 	 */
953 	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
954 		netif_tx_stop_queue(txq);
955 
956 		/* Barrier, so that stop_queue visible to other cpus */
957 		smp_mb__after_atomic();
958 
959 		if (cpdma_check_free_tx_desc(txch))
960 			netif_tx_wake_queue(txq);
961 	}
962 
963 	return NETDEV_TX_OK;
964 fail:
965 	ndev->stats.tx_dropped++;
966 	netif_tx_stop_queue(txq);
967 
968 	/* Barrier, so that stop_queue visible to other cpus */
969 	smp_mb__after_atomic();
970 
971 	if (cpdma_check_free_tx_desc(txch))
972 		netif_tx_wake_queue(txq);
973 
974 	return NETDEV_TX_BUSY;
975 }
976 
977 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
978 {
979 	struct sockaddr *addr = (struct sockaddr *)p;
980 	struct cpsw_priv *priv = netdev_priv(ndev);
981 	struct cpsw_common *cpsw = priv->cpsw;
982 	int ret, slave_no;
983 	int flags = 0;
984 	u16 vid = 0;
985 
986 	slave_no = cpsw_slave_index(cpsw, priv);
987 	if (!is_valid_ether_addr(addr->sa_data))
988 		return -EADDRNOTAVAIL;
989 
990 	ret = pm_runtime_get_sync(cpsw->dev);
991 	if (ret < 0) {
992 		pm_runtime_put_noidle(cpsw->dev);
993 		return ret;
994 	}
995 
996 	vid = cpsw->slaves[slave_no].port_vlan;
997 	flags = ALE_VLAN | ALE_SECURE;
998 
999 	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1000 			   flags, vid);
1001 	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1002 			   flags, vid);
1003 
1004 	ether_addr_copy(priv->mac_addr, addr->sa_data);
1005 	ether_addr_copy(ndev->dev_addr, priv->mac_addr);
1006 	cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv);
1007 
1008 	pm_runtime_put(cpsw->dev);
1009 
1010 	return 0;
1011 }
1012 
1013 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1014 				     __be16 proto, u16 vid)
1015 {
1016 	struct cpsw_priv *priv = netdev_priv(ndev);
1017 	struct cpsw_common *cpsw = priv->cpsw;
1018 	int ret;
1019 	int i;
1020 
1021 	if (cpsw_is_switch_en(cpsw)) {
1022 		dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
1023 		return 0;
1024 	}
1025 
1026 	if (vid == cpsw->data.default_vlan)
1027 		return 0;
1028 
1029 	ret = pm_runtime_get_sync(cpsw->dev);
1030 	if (ret < 0) {
1031 		pm_runtime_put_noidle(cpsw->dev);
1032 		return ret;
1033 	}
1034 
1035 	/* reset the return code as pm_runtime_get_sync() can return
1036 	 * non zero values as well.
1037 	 */
1038 	ret = 0;
1039 	for (i = 0; i < cpsw->data.slaves; i++) {
1040 		if (cpsw->slaves[i].ndev &&
1041 		    vid == cpsw->slaves[i].port_vlan) {
1042 			ret = -EINVAL;
1043 			goto err;
1044 		}
1045 	}
1046 
1047 	dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1048 	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1049 	if (ret)
1050 		dev_err(priv->dev, "cpsw_ale_del_vlan() failed: ret %d\n", ret);
1051 	ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1052 				 HOST_PORT_NUM, ALE_VLAN, vid);
1053 	if (ret)
1054 		dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n",
1055 			ret);
1056 	ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1057 				 0, ALE_VLAN, vid);
1058 	if (ret)
1059 		dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n",
1060 			ret);
1061 	cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1062 	ret = 0;
1063 err:
1064 	pm_runtime_put(cpsw->dev);
1065 	return ret;
1066 }
1067 
1068 static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
1069 				       size_t len)
1070 {
1071 	struct cpsw_priv *priv = netdev_priv(ndev);
1072 	int err;
1073 
1074 	err = snprintf(name, len, "p%d", priv->emac_port);
1075 
1076 	if (err >= len)
1077 		return -EINVAL;
1078 
1079 	return 0;
1080 }
1081 
1082 #ifdef CONFIG_NET_POLL_CONTROLLER
1083 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1084 {
1085 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1086 
1087 	cpsw_intr_disable(cpsw);
1088 	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1089 	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1090 	cpsw_intr_enable(cpsw);
1091 }
1092 #endif
1093 
1094 static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1095 			     struct xdp_frame **frames, u32 flags)
1096 {
1097 	struct cpsw_priv *priv = netdev_priv(ndev);
1098 	struct xdp_frame *xdpf;
1099 	int i, drops = 0;
1100 
1101 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1102 		return -EINVAL;
1103 
1104 	for (i = 0; i < n; i++) {
1105 		xdpf = frames[i];
1106 		if (xdpf->len < CPSW_MIN_PACKET_SIZE) {
1107 			xdp_return_frame_rx_napi(xdpf);
1108 			drops++;
1109 			continue;
1110 		}
1111 
1112 		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
1113 			drops++;
1114 	}
1115 
1116 	return n - drops;
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_do_ioctl		= cpsw_ndo_ioctl,
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 };
1151 
1152 static void cpsw_get_drvinfo(struct net_device *ndev,
1153 			     struct ethtool_drvinfo *info)
1154 {
1155 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1156 	struct platform_device *pdev;
1157 
1158 	pdev = to_platform_device(cpsw->dev);
1159 	strlcpy(info->driver, "cpsw-switch", sizeof(info->driver));
1160 	strlcpy(info->version, "2.0", sizeof(info->version));
1161 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1162 }
1163 
1164 static int cpsw_set_pauseparam(struct net_device *ndev,
1165 			       struct ethtool_pauseparam *pause)
1166 {
1167 	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1168 	struct cpsw_priv *priv = netdev_priv(ndev);
1169 	int slave_no;
1170 
1171 	slave_no = cpsw_slave_index(cpsw, priv);
1172 	if (!cpsw->slaves[slave_no].phy)
1173 		return -EINVAL;
1174 
1175 	if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
1176 		return -EINVAL;
1177 
1178 	priv->rx_pause = pause->rx_pause ? true : false;
1179 	priv->tx_pause = pause->tx_pause ? true : false;
1180 
1181 	phy_set_asym_pause(cpsw->slaves[slave_no].phy,
1182 			   priv->rx_pause, priv->tx_pause);
1183 
1184 	return 0;
1185 }
1186 
1187 static int cpsw_set_channels(struct net_device *ndev,
1188 			     struct ethtool_channels *chs)
1189 {
1190 	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1191 }
1192 
1193 static const struct ethtool_ops cpsw_ethtool_ops = {
1194 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1195 	.get_drvinfo		= cpsw_get_drvinfo,
1196 	.get_msglevel		= cpsw_get_msglevel,
1197 	.set_msglevel		= cpsw_set_msglevel,
1198 	.get_link		= ethtool_op_get_link,
1199 	.get_ts_info		= cpsw_get_ts_info,
1200 	.get_coalesce		= cpsw_get_coalesce,
1201 	.set_coalesce		= cpsw_set_coalesce,
1202 	.get_sset_count		= cpsw_get_sset_count,
1203 	.get_strings		= cpsw_get_strings,
1204 	.get_ethtool_stats	= cpsw_get_ethtool_stats,
1205 	.get_pauseparam		= cpsw_get_pauseparam,
1206 	.set_pauseparam		= cpsw_set_pauseparam,
1207 	.get_wol		= cpsw_get_wol,
1208 	.set_wol		= cpsw_set_wol,
1209 	.get_regs_len		= cpsw_get_regs_len,
1210 	.get_regs		= cpsw_get_regs,
1211 	.begin			= cpsw_ethtool_op_begin,
1212 	.complete		= cpsw_ethtool_op_complete,
1213 	.get_channels		= cpsw_get_channels,
1214 	.set_channels		= cpsw_set_channels,
1215 	.get_link_ksettings	= cpsw_get_link_ksettings,
1216 	.set_link_ksettings	= cpsw_set_link_ksettings,
1217 	.get_eee		= cpsw_get_eee,
1218 	.set_eee		= cpsw_set_eee,
1219 	.nway_reset		= cpsw_nway_reset,
1220 	.get_ringparam		= cpsw_get_ringparam,
1221 	.set_ringparam		= cpsw_set_ringparam,
1222 };
1223 
1224 static int cpsw_probe_dt(struct cpsw_common *cpsw)
1225 {
1226 	struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np;
1227 	struct cpsw_platform_data *data = &cpsw->data;
1228 	struct device *dev = cpsw->dev;
1229 	int ret;
1230 	u32 prop;
1231 
1232 	if (!node)
1233 		return -EINVAL;
1234 
1235 	tmp_node = of_get_child_by_name(node, "ethernet-ports");
1236 	if (!tmp_node)
1237 		return -ENOENT;
1238 	data->slaves = of_get_child_count(tmp_node);
1239 	if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
1240 		of_node_put(tmp_node);
1241 		return -ENOENT;
1242 	}
1243 
1244 	data->active_slave = 0;
1245 	data->channels = CPSW_MAX_QUEUES;
1246 	data->ale_entries = CPSW_ALE_NUM_ENTRIES;
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 		return -ENOMEM;
1256 
1257 	/* Populate all the child nodes here...
1258 	 */
1259 	ret = devm_of_platform_populate(dev);
1260 	/* We do not want to force this, as in some cases may not have child */
1261 	if (ret)
1262 		dev_warn(dev, "Doesn't have any child node\n");
1263 
1264 	for_each_child_of_node(tmp_node, port_np) {
1265 		struct cpsw_slave_data *slave_data;
1266 		const void *mac_addr;
1267 		u32 port_id;
1268 
1269 		ret = of_property_read_u32(port_np, "reg", &port_id);
1270 		if (ret < 0) {
1271 			dev_err(dev, "%pOF error reading port_id %d\n",
1272 				port_np, ret);
1273 			goto err_node_put;
1274 		}
1275 
1276 		if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
1277 			dev_err(dev, "%pOF has invalid port_id %u\n",
1278 				port_np, port_id);
1279 			ret = -EINVAL;
1280 			goto err_node_put;
1281 		}
1282 
1283 		slave_data = &data->slave_data[port_id - 1];
1284 
1285 		slave_data->disabled = !of_device_is_available(port_np);
1286 		if (slave_data->disabled)
1287 			continue;
1288 
1289 		slave_data->slave_node = port_np;
1290 		slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL);
1291 		if (IS_ERR(slave_data->ifphy)) {
1292 			ret = PTR_ERR(slave_data->ifphy);
1293 			dev_err(dev, "%pOF: Error retrieving port phy: %d\n",
1294 				port_np, ret);
1295 			goto err_node_put;
1296 		}
1297 
1298 		if (of_phy_is_fixed_link(port_np)) {
1299 			ret = of_phy_register_fixed_link(port_np);
1300 			if (ret) {
1301 				if (ret != -EPROBE_DEFER)
1302 					dev_err(dev, "%pOF failed to register fixed-link phy: %d\n",
1303 						port_np, ret);
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 		mac_addr = of_get_mac_address(port_np);
1326 		if (!IS_ERR(mac_addr)) {
1327 			ether_addr_copy(slave_data->mac_addr, mac_addr);
1328 		} else {
1329 			ret = ti_cm_get_macid(dev, port_id - 1,
1330 					      slave_data->mac_addr);
1331 			if (ret)
1332 				goto err_node_put;
1333 		}
1334 
1335 		if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
1336 					 &prop)) {
1337 			dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
1338 				port_np);
1339 			slave_data->dual_emac_res_vlan = port_id;
1340 			dev_err(dev, "%pOF Using %d as Reserved VLAN\n",
1341 				port_np, slave_data->dual_emac_res_vlan);
1342 		} else {
1343 			slave_data->dual_emac_res_vlan = prop;
1344 		}
1345 	}
1346 
1347 	of_node_put(tmp_node);
1348 	return 0;
1349 
1350 err_node_put:
1351 	of_node_put(port_np);
1352 	return ret;
1353 }
1354 
1355 static void cpsw_remove_dt(struct cpsw_common *cpsw)
1356 {
1357 	struct cpsw_platform_data *data = &cpsw->data;
1358 	int i = 0;
1359 
1360 	for (i = 0; i < cpsw->data.slaves; i++) {
1361 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1362 		struct device_node *port_np = slave_data->phy_node;
1363 
1364 		if (port_np) {
1365 			if (of_phy_is_fixed_link(port_np))
1366 				of_phy_deregister_fixed_link(port_np);
1367 
1368 			of_node_put(port_np);
1369 		}
1370 	}
1371 }
1372 
1373 static int cpsw_create_ports(struct cpsw_common *cpsw)
1374 {
1375 	struct cpsw_platform_data *data = &cpsw->data;
1376 	struct net_device *ndev, *napi_ndev = NULL;
1377 	struct device *dev = cpsw->dev;
1378 	struct cpsw_priv *priv;
1379 	int ret = 0, i = 0;
1380 
1381 	for (i = 0; i < cpsw->data.slaves; i++) {
1382 		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1383 
1384 		if (slave_data->disabled)
1385 			continue;
1386 
1387 		ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
1388 					       CPSW_MAX_QUEUES,
1389 					       CPSW_MAX_QUEUES);
1390 		if (!ndev) {
1391 			dev_err(dev, "error allocating net_device\n");
1392 			return -ENOMEM;
1393 		}
1394 
1395 		priv = netdev_priv(ndev);
1396 		priv->cpsw = cpsw;
1397 		priv->ndev = ndev;
1398 		priv->dev  = dev;
1399 		priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1400 		priv->emac_port = i + 1;
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 		ether_addr_copy(ndev->dev_addr, 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_NETNS_LOCAL;
1418 
1419 		ndev->netdev_ops = &cpsw_netdev_ops;
1420 		ndev->ethtool_ops = &cpsw_ethtool_ops;
1421 		SET_NETDEV_DEV(ndev, dev);
1422 
1423 		if (!napi_ndev) {
1424 			/* CPSW Host port CPDMA interface is shared between
1425 			 * ports and there is only one TX and one RX IRQs
1426 			 * available for all possible TX and RX channels
1427 			 * accordingly.
1428 			 */
1429 			netif_napi_add(ndev, &cpsw->napi_rx,
1430 				       cpsw->quirk_irq ?
1431 				       cpsw_rx_poll : cpsw_rx_mq_poll,
1432 				       CPSW_POLL_WEIGHT);
1433 			netif_tx_napi_add(ndev, &cpsw->napi_tx,
1434 					  cpsw->quirk_irq ?
1435 					  cpsw_tx_poll : cpsw_tx_mq_poll,
1436 					  CPSW_POLL_WEIGHT);
1437 		}
1438 
1439 		napi_ndev = ndev;
1440 	}
1441 
1442 	return ret;
1443 }
1444 
1445 static void cpsw_unregister_ports(struct cpsw_common *cpsw)
1446 {
1447 	int i = 0;
1448 
1449 	for (i = 0; i < cpsw->data.slaves; i++) {
1450 		if (!cpsw->slaves[i].ndev)
1451 			continue;
1452 
1453 		unregister_netdev(cpsw->slaves[i].ndev);
1454 	}
1455 }
1456 
1457 static int cpsw_register_ports(struct cpsw_common *cpsw)
1458 {
1459 	int ret = 0, i = 0;
1460 
1461 	for (i = 0; i < cpsw->data.slaves; i++) {
1462 		if (!cpsw->slaves[i].ndev)
1463 			continue;
1464 
1465 		/* register the network device */
1466 		ret = register_netdev(cpsw->slaves[i].ndev);
1467 		if (ret) {
1468 			dev_err(cpsw->dev,
1469 				"cpsw: err registering net device%d\n", i);
1470 			cpsw->slaves[i].ndev = NULL;
1471 			break;
1472 		}
1473 	}
1474 
1475 	if (ret)
1476 		cpsw_unregister_ports(cpsw);
1477 	return ret;
1478 }
1479 
1480 bool cpsw_port_dev_check(const struct net_device *ndev)
1481 {
1482 	if (ndev->netdev_ops == &cpsw_netdev_ops) {
1483 		struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1484 
1485 		return !cpsw->data.dual_emac;
1486 	}
1487 
1488 	return false;
1489 }
1490 
1491 static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
1492 {
1493 	int set_val = 0;
1494 	int i;
1495 
1496 	if (!cpsw->ale_bypass &&
1497 	    (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
1498 		set_val = 1;
1499 
1500 	dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
1501 
1502 	for (i = 0; i < cpsw->data.slaves; i++) {
1503 		struct net_device *sl_ndev = cpsw->slaves[i].ndev;
1504 		struct cpsw_priv *priv = netdev_priv(sl_ndev);
1505 
1506 		priv->offload_fwd_mark = set_val;
1507 	}
1508 }
1509 
1510 static int cpsw_netdevice_port_link(struct net_device *ndev,
1511 				    struct net_device *br_ndev)
1512 {
1513 	struct cpsw_priv *priv = netdev_priv(ndev);
1514 	struct cpsw_common *cpsw = priv->cpsw;
1515 
1516 	if (!cpsw->br_members) {
1517 		cpsw->hw_bridge_dev = br_ndev;
1518 	} else {
1519 		/* This is adding the port to a second bridge, this is
1520 		 * unsupported
1521 		 */
1522 		if (cpsw->hw_bridge_dev != br_ndev)
1523 			return -EOPNOTSUPP;
1524 	}
1525 
1526 	cpsw->br_members |= BIT(priv->emac_port);
1527 
1528 	cpsw_port_offload_fwd_mark_update(cpsw);
1529 
1530 	return NOTIFY_DONE;
1531 }
1532 
1533 static void cpsw_netdevice_port_unlink(struct net_device *ndev)
1534 {
1535 	struct cpsw_priv *priv = netdev_priv(ndev);
1536 	struct cpsw_common *cpsw = priv->cpsw;
1537 
1538 	cpsw->br_members &= ~BIT(priv->emac_port);
1539 
1540 	cpsw_port_offload_fwd_mark_update(cpsw);
1541 
1542 	if (!cpsw->br_members)
1543 		cpsw->hw_bridge_dev = NULL;
1544 }
1545 
1546 /* netdev notifier */
1547 static int cpsw_netdevice_event(struct notifier_block *unused,
1548 				unsigned long event, void *ptr)
1549 {
1550 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1551 	struct netdev_notifier_changeupper_info *info;
1552 	int ret = NOTIFY_DONE;
1553 
1554 	if (!cpsw_port_dev_check(ndev))
1555 		return NOTIFY_DONE;
1556 
1557 	switch (event) {
1558 	case NETDEV_CHANGEUPPER:
1559 		info = ptr;
1560 
1561 		if (netif_is_bridge_master(info->upper_dev)) {
1562 			if (info->linking)
1563 				ret = cpsw_netdevice_port_link(ndev,
1564 							       info->upper_dev);
1565 			else
1566 				cpsw_netdevice_port_unlink(ndev);
1567 		}
1568 		break;
1569 	default:
1570 		return NOTIFY_DONE;
1571 	}
1572 
1573 	return notifier_from_errno(ret);
1574 }
1575 
1576 static struct notifier_block cpsw_netdevice_nb __read_mostly = {
1577 	.notifier_call = cpsw_netdevice_event,
1578 };
1579 
1580 static int cpsw_register_notifiers(struct cpsw_common *cpsw)
1581 {
1582 	int ret = 0;
1583 
1584 	ret = register_netdevice_notifier(&cpsw_netdevice_nb);
1585 	if (ret) {
1586 		dev_err(cpsw->dev, "can't register netdevice notifier\n");
1587 		return ret;
1588 	}
1589 
1590 	ret = cpsw_switchdev_register_notifiers(cpsw);
1591 	if (ret)
1592 		unregister_netdevice_notifier(&cpsw_netdevice_nb);
1593 
1594 	return ret;
1595 }
1596 
1597 static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
1598 {
1599 	cpsw_switchdev_unregister_notifiers(cpsw);
1600 	unregister_netdevice_notifier(&cpsw_netdevice_nb);
1601 }
1602 
1603 static const struct devlink_ops cpsw_devlink_ops = {
1604 };
1605 
1606 static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
1607 				   struct devlink_param_gset_ctx *ctx)
1608 {
1609 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1610 	struct cpsw_common *cpsw = dl_priv->cpsw;
1611 
1612 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1613 
1614 	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1615 		return  -EOPNOTSUPP;
1616 
1617 	ctx->val.vbool = !cpsw->data.dual_emac;
1618 
1619 	return 0;
1620 }
1621 
1622 static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
1623 				   struct devlink_param_gset_ctx *ctx)
1624 {
1625 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1626 	struct cpsw_common *cpsw = dl_priv->cpsw;
1627 	int vlan = cpsw->data.default_vlan;
1628 	bool switch_en = ctx->val.vbool;
1629 	bool if_running = false;
1630 	int i;
1631 
1632 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1633 
1634 	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1635 		return  -EOPNOTSUPP;
1636 
1637 	if (switch_en == !cpsw->data.dual_emac)
1638 		return 0;
1639 
1640 	if (!switch_en && cpsw->br_members) {
1641 		dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
1642 		return -EINVAL;
1643 	}
1644 
1645 	rtnl_lock();
1646 
1647 	for (i = 0; i < cpsw->data.slaves; i++) {
1648 		struct cpsw_slave *slave = &cpsw->slaves[i];
1649 		struct net_device *sl_ndev = slave->ndev;
1650 
1651 		if (!sl_ndev || !netif_running(sl_ndev))
1652 			continue;
1653 
1654 		if_running = true;
1655 	}
1656 
1657 	if (!if_running) {
1658 		/* all ndevs are down */
1659 		cpsw->data.dual_emac = !switch_en;
1660 		for (i = 0; i < cpsw->data.slaves; i++) {
1661 			struct cpsw_slave *slave = &cpsw->slaves[i];
1662 			struct net_device *sl_ndev = slave->ndev;
1663 			struct cpsw_priv *priv;
1664 
1665 			if (!sl_ndev)
1666 				continue;
1667 
1668 			priv = netdev_priv(sl_ndev);
1669 			if (switch_en)
1670 				vlan = cpsw->data.default_vlan;
1671 			else
1672 				vlan = slave->data->dual_emac_res_vlan;
1673 			slave->port_vlan = vlan;
1674 		}
1675 		goto exit;
1676 	}
1677 
1678 	if (switch_en) {
1679 		dev_info(cpsw->dev, "Enable switch mode\n");
1680 
1681 		/* enable bypass - no forwarding; all traffic goes to Host */
1682 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1683 
1684 		/* clean up ALE table */
1685 		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1686 		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1687 
1688 		cpsw_init_host_port_switch(cpsw);
1689 
1690 		for (i = 0; i < cpsw->data.slaves; i++) {
1691 			struct cpsw_slave *slave = &cpsw->slaves[i];
1692 			struct net_device *sl_ndev = slave->ndev;
1693 			struct cpsw_priv *priv;
1694 
1695 			if (!sl_ndev)
1696 				continue;
1697 
1698 			priv = netdev_priv(sl_ndev);
1699 			slave->port_vlan = vlan;
1700 			if (netif_running(sl_ndev))
1701 				cpsw_port_add_switch_def_ale_entries(priv,
1702 								     slave);
1703 		}
1704 
1705 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1706 		cpsw->data.dual_emac = false;
1707 	} else {
1708 		dev_info(cpsw->dev, "Disable switch mode\n");
1709 
1710 		/* enable bypass - no forwarding; all traffic goes to Host */
1711 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1712 
1713 		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1714 		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1715 
1716 		cpsw_init_host_port_dual_mac(cpsw);
1717 
1718 		for (i = 0; i < cpsw->data.slaves; i++) {
1719 			struct cpsw_slave *slave = &cpsw->slaves[i];
1720 			struct net_device *sl_ndev = slave->ndev;
1721 			struct cpsw_priv *priv;
1722 
1723 			if (!sl_ndev)
1724 				continue;
1725 
1726 			priv = netdev_priv(slave->ndev);
1727 			slave->port_vlan = slave->data->dual_emac_res_vlan;
1728 			cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
1729 		}
1730 
1731 		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1732 		cpsw->data.dual_emac = true;
1733 	}
1734 exit:
1735 	rtnl_unlock();
1736 
1737 	return 0;
1738 }
1739 
1740 static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
1741 				struct devlink_param_gset_ctx *ctx)
1742 {
1743 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1744 	struct cpsw_common *cpsw = dl_priv->cpsw;
1745 
1746 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1747 
1748 	switch (id) {
1749 	case CPSW_DL_PARAM_ALE_BYPASS:
1750 		ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
1751 		break;
1752 	default:
1753 		return -EOPNOTSUPP;
1754 	}
1755 
1756 	return 0;
1757 }
1758 
1759 static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
1760 				struct devlink_param_gset_ctx *ctx)
1761 {
1762 	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1763 	struct cpsw_common *cpsw = dl_priv->cpsw;
1764 	int ret = -EOPNOTSUPP;
1765 
1766 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1767 
1768 	switch (id) {
1769 	case CPSW_DL_PARAM_ALE_BYPASS:
1770 		ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
1771 					   ctx->val.vbool);
1772 		if (!ret) {
1773 			cpsw->ale_bypass = ctx->val.vbool;
1774 			cpsw_port_offload_fwd_mark_update(cpsw);
1775 		}
1776 		break;
1777 	default:
1778 		return -EOPNOTSUPP;
1779 	}
1780 
1781 	return 0;
1782 }
1783 
1784 static const struct devlink_param cpsw_devlink_params[] = {
1785 	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
1786 			     "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
1787 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1788 			     cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
1789 			     NULL),
1790 	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
1791 			     "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
1792 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1793 			     cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
1794 };
1795 
1796 static int cpsw_register_devlink(struct cpsw_common *cpsw)
1797 {
1798 	struct device *dev = cpsw->dev;
1799 	struct cpsw_devlink *dl_priv;
1800 	int ret = 0;
1801 
1802 	cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv));
1803 	if (!cpsw->devlink)
1804 		return -ENOMEM;
1805 
1806 	dl_priv = devlink_priv(cpsw->devlink);
1807 	dl_priv->cpsw = cpsw;
1808 
1809 	ret = devlink_register(cpsw->devlink, dev);
1810 	if (ret) {
1811 		dev_err(dev, "DL reg fail ret:%d\n", ret);
1812 		goto dl_free;
1813 	}
1814 
1815 	ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
1816 				      ARRAY_SIZE(cpsw_devlink_params));
1817 	if (ret) {
1818 		dev_err(dev, "DL params reg fail ret:%d\n", ret);
1819 		goto dl_unreg;
1820 	}
1821 
1822 	devlink_params_publish(cpsw->devlink);
1823 	return ret;
1824 
1825 dl_unreg:
1826 	devlink_unregister(cpsw->devlink);
1827 dl_free:
1828 	devlink_free(cpsw->devlink);
1829 	return ret;
1830 }
1831 
1832 static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
1833 {
1834 	devlink_params_unpublish(cpsw->devlink);
1835 	devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
1836 				  ARRAY_SIZE(cpsw_devlink_params));
1837 	devlink_unregister(cpsw->devlink);
1838 	devlink_free(cpsw->devlink);
1839 }
1840 
1841 static const struct of_device_id cpsw_of_mtable[] = {
1842 	{ .compatible = "ti,cpsw-switch"},
1843 	{ .compatible = "ti,am335x-cpsw-switch"},
1844 	{ .compatible = "ti,am4372-cpsw-switch"},
1845 	{ .compatible = "ti,dra7-cpsw-switch"},
1846 	{ /* sentinel */ },
1847 };
1848 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
1849 
1850 static const struct soc_device_attribute cpsw_soc_devices[] = {
1851 	{ .family = "AM33xx", .revision = "ES1.0"},
1852 	{ /* sentinel */ }
1853 };
1854 
1855 static int cpsw_probe(struct platform_device *pdev)
1856 {
1857 	const struct soc_device_attribute *soc;
1858 	struct device *dev = &pdev->dev;
1859 	struct cpsw_common *cpsw;
1860 	struct resource *ss_res;
1861 	struct gpio_descs *mode;
1862 	void __iomem *ss_regs;
1863 	int ret = 0, ch;
1864 	struct clk *clk;
1865 	int irq;
1866 
1867 	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
1868 	if (!cpsw)
1869 		return -ENOMEM;
1870 
1871 	cpsw_slave_index = cpsw_slave_index_priv;
1872 
1873 	cpsw->dev = dev;
1874 
1875 	cpsw->slaves = devm_kcalloc(dev,
1876 				    CPSW_SLAVE_PORTS_NUM,
1877 				    sizeof(struct cpsw_slave),
1878 				    GFP_KERNEL);
1879 	if (!cpsw->slaves)
1880 		return -ENOMEM;
1881 
1882 	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
1883 	if (IS_ERR(mode)) {
1884 		ret = PTR_ERR(mode);
1885 		dev_err(dev, "gpio request failed, ret %d\n", ret);
1886 		return ret;
1887 	}
1888 
1889 	clk = devm_clk_get(dev, "fck");
1890 	if (IS_ERR(clk)) {
1891 		ret = PTR_ERR(clk);
1892 		dev_err(dev, "fck is not found %d\n", ret);
1893 		return ret;
1894 	}
1895 	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
1896 
1897 	ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1898 	ss_regs = devm_ioremap_resource(dev, ss_res);
1899 	if (IS_ERR(ss_regs)) {
1900 		ret = PTR_ERR(ss_regs);
1901 		return ret;
1902 	}
1903 	cpsw->regs = ss_regs;
1904 
1905 	irq = platform_get_irq_byname(pdev, "rx");
1906 	if (irq < 0)
1907 		return irq;
1908 	cpsw->irqs_table[0] = irq;
1909 
1910 	irq = platform_get_irq_byname(pdev, "tx");
1911 	if (irq < 0)
1912 		return irq;
1913 	cpsw->irqs_table[1] = irq;
1914 
1915 	irq = platform_get_irq_byname(pdev, "misc");
1916 	if (irq <= 0)
1917 		return irq;
1918 	cpsw->misc_irq = irq;
1919 
1920 	platform_set_drvdata(pdev, cpsw);
1921 	/* This may be required here for child devices. */
1922 	pm_runtime_enable(dev);
1923 
1924 	/* Need to enable clocks with runtime PM api to access module
1925 	 * registers
1926 	 */
1927 	ret = pm_runtime_get_sync(dev);
1928 	if (ret < 0) {
1929 		pm_runtime_put_noidle(dev);
1930 		pm_runtime_disable(dev);
1931 		return ret;
1932 	}
1933 
1934 	ret = cpsw_probe_dt(cpsw);
1935 	if (ret)
1936 		goto clean_dt_ret;
1937 
1938 	soc = soc_device_match(cpsw_soc_devices);
1939 	if (soc)
1940 		cpsw->quirk_irq = true;
1941 
1942 	cpsw->rx_packet_max = rx_packet_max;
1943 	cpsw->descs_pool_size = descs_pool_size;
1944 	eth_random_addr(cpsw->base_mac);
1945 
1946 	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1947 			       (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
1948 			       descs_pool_size);
1949 	if (ret)
1950 		goto clean_dt_ret;
1951 
1952 	cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
1953 			ss_regs + CPSW1_WR_OFFSET :
1954 			ss_regs + CPSW2_WR_OFFSET;
1955 
1956 	ch = cpsw->quirk_irq ? 0 : 7;
1957 	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
1958 	if (IS_ERR(cpsw->txv[0].ch)) {
1959 		dev_err(dev, "error initializing tx dma channel\n");
1960 		ret = PTR_ERR(cpsw->txv[0].ch);
1961 		goto clean_cpts;
1962 	}
1963 
1964 	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
1965 	if (IS_ERR(cpsw->rxv[0].ch)) {
1966 		dev_err(dev, "error initializing rx dma channel\n");
1967 		ret = PTR_ERR(cpsw->rxv[0].ch);
1968 		goto clean_cpts;
1969 	}
1970 	cpsw_split_res(cpsw);
1971 
1972 	/* setup netdevs */
1973 	ret = cpsw_create_ports(cpsw);
1974 	if (ret)
1975 		goto clean_unregister_netdev;
1976 
1977 	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1978 	 * MISC IRQs which are always kept disabled with this driver so
1979 	 * we will not request them.
1980 	 *
1981 	 * If anyone wants to implement support for those, make sure to
1982 	 * first request and append them to irqs_table array.
1983 	 */
1984 
1985 	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1986 			       0, dev_name(dev), cpsw);
1987 	if (ret < 0) {
1988 		dev_err(dev, "error attaching irq (%d)\n", ret);
1989 		goto clean_unregister_netdev;
1990 	}
1991 
1992 	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1993 			       0, dev_name(dev), cpsw);
1994 	if (ret < 0) {
1995 		dev_err(dev, "error attaching irq (%d)\n", ret);
1996 		goto clean_unregister_netdev;
1997 	}
1998 
1999 	if (!cpsw->cpts)
2000 		goto skip_cpts;
2001 
2002 	ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt,
2003 			       0, dev_name(&pdev->dev), cpsw);
2004 	if (ret < 0) {
2005 		dev_err(dev, "error attaching misc irq (%d)\n", ret);
2006 		goto clean_unregister_netdev;
2007 	}
2008 
2009 	/* Enable misc CPTS evnt_pend IRQ */
2010 	cpts_set_irqpoll(cpsw->cpts, false);
2011 	writel(0x10, &cpsw->wr_regs->misc_en);
2012 
2013 skip_cpts:
2014 	ret = cpsw_register_notifiers(cpsw);
2015 	if (ret)
2016 		goto clean_unregister_netdev;
2017 
2018 	ret = cpsw_register_devlink(cpsw);
2019 	if (ret)
2020 		goto clean_unregister_notifiers;
2021 
2022 	ret = cpsw_register_ports(cpsw);
2023 	if (ret)
2024 		goto clean_unregister_notifiers;
2025 
2026 	dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
2027 		   &ss_res->start, descs_pool_size,
2028 		   cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
2029 		   CPSW_MINOR_VERSION(cpsw->version),
2030 		   CPSW_RTL_VERSION(cpsw->version));
2031 
2032 	pm_runtime_put(dev);
2033 
2034 	return 0;
2035 
2036 clean_unregister_notifiers:
2037 	cpsw_unregister_notifiers(cpsw);
2038 clean_unregister_netdev:
2039 	cpsw_unregister_ports(cpsw);
2040 clean_cpts:
2041 	cpts_release(cpsw->cpts);
2042 	cpdma_ctlr_destroy(cpsw->dma);
2043 clean_dt_ret:
2044 	cpsw_remove_dt(cpsw);
2045 	pm_runtime_put_sync(dev);
2046 	pm_runtime_disable(dev);
2047 	return ret;
2048 }
2049 
2050 static int cpsw_remove(struct platform_device *pdev)
2051 {
2052 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
2053 	int ret;
2054 
2055 	ret = pm_runtime_get_sync(&pdev->dev);
2056 	if (ret < 0) {
2057 		pm_runtime_put_noidle(&pdev->dev);
2058 		return ret;
2059 	}
2060 
2061 	cpsw_unregister_notifiers(cpsw);
2062 	cpsw_unregister_devlink(cpsw);
2063 	cpsw_unregister_ports(cpsw);
2064 
2065 	cpts_release(cpsw->cpts);
2066 	cpdma_ctlr_destroy(cpsw->dma);
2067 	cpsw_remove_dt(cpsw);
2068 	pm_runtime_put_sync(&pdev->dev);
2069 	pm_runtime_disable(&pdev->dev);
2070 	return 0;
2071 }
2072 
2073 static struct platform_driver cpsw_driver = {
2074 	.driver = {
2075 		.name	 = "cpsw-switch",
2076 		.of_match_table = cpsw_of_mtable,
2077 	},
2078 	.probe = cpsw_probe,
2079 	.remove = cpsw_remove,
2080 };
2081 
2082 module_platform_driver(cpsw_driver);
2083 
2084 MODULE_LICENSE("GPL");
2085 MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");
2086