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