1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/etherdevice.h>
8 #include <linux/ethtool.h>
9 #include <linux/if_bridge.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/phy.h>
17 #include <linux/skbuff.h>
19 #include <net/netevent.h>
20 #include <net/rtnetlink.h>
21 #include <net/switchdev.h>
25 /* MAC table entry types.
26 * ENTRYTYPE_NORMAL is subject to aging.
27 * ENTRYTYPE_LOCKED is not subject to aging.
28 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
29 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
31 enum macaccess_entry_type {
38 struct ocelot_mact_entry {
41 enum macaccess_entry_type type;
44 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
46 unsigned int val, timeout = 10;
48 /* Wait for the issued mac table command to be completed, or timeout.
49 * When the command read from ANA_TABLES_MACACCESS is
50 * MACACCESS_CMD_IDLE, the issued command completed successfully.
53 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
54 val &= ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M;
55 } while (val != MACACCESS_CMD_IDLE && timeout--);
63 static void ocelot_mact_select(struct ocelot *ocelot,
64 const unsigned char mac[ETH_ALEN],
67 u32 macl = 0, mach = 0;
69 /* Set the MAC address to handle and the vlan associated in a format
70 * understood by the hardware.
80 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
81 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
85 static int ocelot_mact_learn(struct ocelot *ocelot, int port,
86 const unsigned char mac[ETH_ALEN],
88 enum macaccess_entry_type type)
90 ocelot_mact_select(ocelot, mac, vid);
92 /* Issue a write command */
93 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
94 ANA_TABLES_MACACCESS_DEST_IDX(port) |
95 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
96 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
97 ANA_TABLES_MACACCESS);
99 return ocelot_mact_wait_for_completion(ocelot);
102 static int ocelot_mact_forget(struct ocelot *ocelot,
103 const unsigned char mac[ETH_ALEN],
106 ocelot_mact_select(ocelot, mac, vid);
108 /* Issue a forget command */
110 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
111 ANA_TABLES_MACACCESS);
113 return ocelot_mact_wait_for_completion(ocelot);
116 static void ocelot_mact_init(struct ocelot *ocelot)
118 /* Configure the learning mode entries attributes:
119 * - Do not copy the frame to the CPU extraction queues.
120 * - Use the vlan and mac_cpoy for dmac lookup.
122 ocelot_rmw(ocelot, 0,
123 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
124 | ANA_AGENCTRL_LEARN_FWD_KILL
125 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
128 /* Clear the MAC table */
129 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
132 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
134 unsigned int val, timeout = 10;
136 /* Wait for the issued mac table command to be completed, or timeout.
137 * When the command read from ANA_TABLES_MACACCESS is
138 * MACACCESS_CMD_IDLE, the issued command completed successfully.
141 val = ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
142 val &= ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M;
143 } while (val != ANA_TABLES_VLANACCESS_CMD_IDLE && timeout--);
151 static void ocelot_vlan_init(struct ocelot *ocelot)
153 /* Clear VLAN table, by default all ports are members of all VLANs */
154 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
155 ANA_TABLES_VLANACCESS);
156 ocelot_vlant_wait_for_completion(ocelot);
160 * Bit 8: Unit; 0:1, 1:16
161 * Bit 7-0: Value to be multiplied with unit
163 static u16 ocelot_wm_enc(u16 value)
166 return BIT(8) | (value / 16);
171 static void ocelot_port_adjust_link(struct net_device *dev)
173 struct ocelot_port *port = netdev_priv(dev);
174 struct ocelot *ocelot = port->ocelot;
175 u8 p = port->chip_port;
176 int speed, atop_wm, mode = 0;
178 switch (dev->phydev->speed) {
180 speed = OCELOT_SPEED_10;
183 speed = OCELOT_SPEED_100;
186 speed = OCELOT_SPEED_1000;
187 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
190 speed = OCELOT_SPEED_2500;
191 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
194 netdev_err(dev, "Unsupported PHY speed: %d\n",
199 phy_print_status(dev->phydev);
201 if (!dev->phydev->link)
204 /* Only full duplex supported for now */
205 ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
206 mode, DEV_MAC_MODE_CFG);
209 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
210 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
212 ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
214 /* Load seed (0) and set MAC HDX late collision */
215 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
216 DEV_MAC_HDX_CFG_SEED_LOAD,
219 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
222 /* Disable HDX fast control */
223 ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
225 /* SGMII only for now */
226 ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
227 ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
230 ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
232 /* No aneg on SGMII */
233 ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
236 ocelot_port_writel(port, 0, PCS1G_LB_CFG);
238 /* Set Max Length and maximum tags allowed */
239 ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
240 ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
241 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
242 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
245 /* Enable MAC module */
246 ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
247 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
249 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
251 ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
254 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
255 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
256 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
259 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
262 /* Set Pause WM hysteresis
263 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
264 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
266 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
267 SYS_PAUSE_CFG_PAUSE_STOP(101) |
268 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
270 /* Core: Enable port for frame transfer */
271 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
272 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
273 QSYS_SWITCH_PORT_MODE_PORT_ENA,
274 QSYS_SWITCH_PORT_MODE, p);
277 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
278 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
279 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
280 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
281 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
283 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
285 /* Tail dropping watermark */
286 atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
287 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
289 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
292 static int ocelot_port_open(struct net_device *dev)
294 struct ocelot_port *port = netdev_priv(dev);
295 struct ocelot *ocelot = port->ocelot;
298 /* Enable receiving frames on the port, and activate auto-learning of
301 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
302 ANA_PORT_PORT_CFG_RECV_ENA |
303 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
304 ANA_PORT_PORT_CFG, port->chip_port);
306 err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
307 PHY_INTERFACE_MODE_NA);
309 netdev_err(dev, "Could not attach to PHY\n");
313 dev->phydev = port->phy;
315 phy_attached_info(port->phy);
316 phy_start(port->phy);
320 static int ocelot_port_stop(struct net_device *dev)
322 struct ocelot_port *port = netdev_priv(dev);
324 phy_disconnect(port->phy);
328 ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
329 ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
330 QSYS_SWITCH_PORT_MODE, port->chip_port);
334 /* Generate the IFH for frame injection
336 * The IFH is a 128bit-value
337 * bit 127: bypass the analyzer processing
338 * bit 56-67: destination mask
339 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
340 * bit 20-27: cpu extraction queue mask
341 * bit 16: tag type 0: C-tag, 1: S-tag
344 static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
346 ifh[0] = IFH_INJ_BYPASS;
347 ifh[1] = (0xff00 & info->port) >> 8;
348 ifh[2] = (0xff & info->port) << 24;
349 ifh[3] = IFH_INJ_POP_CNT_DISABLE | (info->cpuq << 20) |
350 (info->tag_type << 16) | info->vid;
355 static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
357 struct ocelot_port *port = netdev_priv(dev);
358 struct ocelot *ocelot = port->ocelot;
359 u32 val, ifh[IFH_LEN];
360 struct frame_info info = {};
361 u8 grp = 0; /* Send everything on CPU group 0 */
362 unsigned int i, count, last;
364 val = ocelot_read(ocelot, QS_INJ_STATUS);
365 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
366 (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
367 return NETDEV_TX_BUSY;
369 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
370 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
372 info.port = BIT(port->chip_port);
374 ocelot_gen_ifh(ifh, &info);
376 for (i = 0; i < IFH_LEN; i++)
377 ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
379 count = (skb->len + 3) / 4;
381 for (i = 0; i < count; i++) {
382 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
386 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
387 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
391 /* Indicate EOF and valid bytes in last word */
392 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
393 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
398 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
399 skb_tx_timestamp(skb);
401 dev->stats.tx_packets++;
402 dev->stats.tx_bytes += skb->len;
403 dev_kfree_skb_any(skb);
408 static void ocelot_mact_mc_reset(struct ocelot_port *port)
410 struct ocelot *ocelot = port->ocelot;
411 struct netdev_hw_addr *ha, *n;
413 /* Free and forget all the MAC addresses stored in the port private mc
414 * list. These are mc addresses that were previously added by calling
415 * ocelot_mact_mc_add().
417 list_for_each_entry_safe(ha, n, &port->mc, list) {
418 ocelot_mact_forget(ocelot, ha->addr, port->pvid);
424 static int ocelot_mact_mc_add(struct ocelot_port *port,
425 struct netdev_hw_addr *hw_addr)
427 struct ocelot *ocelot = port->ocelot;
428 struct netdev_hw_addr *ha = kzalloc(sizeof(*ha), GFP_KERNEL);
433 memcpy(ha, hw_addr, sizeof(*ha));
434 list_add_tail(&ha->list, &port->mc);
436 ocelot_mact_learn(ocelot, PGID_CPU, ha->addr, port->pvid,
442 static void ocelot_set_rx_mode(struct net_device *dev)
444 struct ocelot_port *port = netdev_priv(dev);
445 struct ocelot *ocelot = port->ocelot;
446 struct netdev_hw_addr *ha;
450 /* This doesn't handle promiscuous mode because the bridge core is
451 * setting IFF_PROMISC on all slave interfaces and all frames would be
452 * forwarded to the CPU port.
454 val = GENMASK(ocelot->num_phys_ports - 1, 0);
455 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
456 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
458 /* Handle the device multicast addresses. First remove all the
459 * previously installed addresses and then add the latest ones to the
462 ocelot_mact_mc_reset(port);
463 netdev_for_each_mc_addr(ha, dev)
464 ocelot_mact_mc_add(port, ha);
467 static int ocelot_port_get_phys_port_name(struct net_device *dev,
468 char *buf, size_t len)
470 struct ocelot_port *port = netdev_priv(dev);
473 ret = snprintf(buf, len, "p%d", port->chip_port);
480 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
482 struct ocelot_port *port = netdev_priv(dev);
483 struct ocelot *ocelot = port->ocelot;
484 const struct sockaddr *addr = p;
486 /* Learn the new net device MAC address in the mac table. */
487 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
489 /* Then forget the previous one. */
490 ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
492 ether_addr_copy(dev->dev_addr, addr->sa_data);
496 static void ocelot_get_stats64(struct net_device *dev,
497 struct rtnl_link_stats64 *stats)
499 struct ocelot_port *port = netdev_priv(dev);
500 struct ocelot *ocelot = port->ocelot;
502 /* Configure the port to read the stats from */
503 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
507 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
508 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
509 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
510 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
511 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
512 ocelot_read(ocelot, SYS_COUNT_RX_64) +
513 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
514 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
515 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
516 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
517 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
518 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
519 stats->rx_dropped = dev->stats.rx_dropped;
522 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
523 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
524 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
525 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
526 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
527 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
528 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
529 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
530 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
531 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
534 static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
535 struct net_device *dev, const unsigned char *addr,
538 struct ocelot_port *port = netdev_priv(dev);
539 struct ocelot *ocelot = port->ocelot;
541 return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
545 static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
546 struct net_device *dev,
547 const unsigned char *addr, u16 vid)
549 struct ocelot_port *port = netdev_priv(dev);
550 struct ocelot *ocelot = port->ocelot;
552 return ocelot_mact_forget(ocelot, addr, vid);
555 struct ocelot_dump_ctx {
556 struct net_device *dev;
558 struct netlink_callback *cb;
562 static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
563 struct ocelot_dump_ctx *dump)
565 u32 portid = NETLINK_CB(dump->cb->skb).portid;
566 u32 seq = dump->cb->nlh->nlmsg_seq;
567 struct nlmsghdr *nlh;
570 if (dump->idx < dump->cb->args[2])
573 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
574 sizeof(*ndm), NLM_F_MULTI);
578 ndm = nlmsg_data(nlh);
579 ndm->ndm_family = AF_BRIDGE;
582 ndm->ndm_flags = NTF_SELF;
584 ndm->ndm_ifindex = dump->dev->ifindex;
585 ndm->ndm_state = NUD_REACHABLE;
587 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
588 goto nla_put_failure;
590 if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
591 goto nla_put_failure;
593 nlmsg_end(dump->skb, nlh);
600 nlmsg_cancel(dump->skb, nlh);
604 static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
605 struct ocelot_mact_entry *entry)
607 struct ocelot *ocelot = port->ocelot;
609 u32 val, dst, macl, mach;
611 /* Set row and column to read from */
612 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
613 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
615 /* Issue a read command */
617 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
618 ANA_TABLES_MACACCESS);
620 if (ocelot_mact_wait_for_completion(ocelot))
623 /* Read the entry flags */
624 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
625 if (!(val & ANA_TABLES_MACACCESS_VALID))
628 /* If the entry read has another port configured as its destination,
631 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
632 if (dst != port->chip_port)
635 /* Get the entry's MAC address and VLAN id */
636 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
637 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
639 mac[0] = (mach >> 8) & 0xff;
640 mac[1] = (mach >> 0) & 0xff;
641 mac[2] = (macl >> 24) & 0xff;
642 mac[3] = (macl >> 16) & 0xff;
643 mac[4] = (macl >> 8) & 0xff;
644 mac[5] = (macl >> 0) & 0xff;
646 entry->vid = (mach >> 16) & 0xfff;
647 ether_addr_copy(entry->mac, mac);
652 static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
653 struct net_device *dev,
654 struct net_device *filter_dev, int *idx)
656 struct ocelot_port *port = netdev_priv(dev);
658 struct ocelot_dump_ctx dump = {
665 struct ocelot_mact_entry entry;
667 /* Loop through all the mac tables entries. There are 1024 rows of 4
670 for (i = 0; i < 1024; i++) {
671 for (j = 0; j < 4; j++) {
672 ret = ocelot_mact_read(port, i, j, &entry);
673 /* If the entry is invalid (wrong port, invalid...),
681 ret = ocelot_fdb_do_dump(&entry, &dump);
692 static const struct net_device_ops ocelot_port_netdev_ops = {
693 .ndo_open = ocelot_port_open,
694 .ndo_stop = ocelot_port_stop,
695 .ndo_start_xmit = ocelot_port_xmit,
696 .ndo_set_rx_mode = ocelot_set_rx_mode,
697 .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
698 .ndo_set_mac_address = ocelot_port_set_mac_address,
699 .ndo_get_stats64 = ocelot_get_stats64,
700 .ndo_fdb_add = ocelot_fdb_add,
701 .ndo_fdb_del = ocelot_fdb_del,
702 .ndo_fdb_dump = ocelot_fdb_dump,
705 static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
707 struct ocelot_port *port = netdev_priv(netdev);
708 struct ocelot *ocelot = port->ocelot;
711 if (sset != ETH_SS_STATS)
714 for (i = 0; i < ocelot->num_stats; i++)
715 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
719 static void ocelot_check_stats(struct work_struct *work)
721 struct delayed_work *del_work = to_delayed_work(work);
722 struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work);
725 mutex_lock(&ocelot->stats_lock);
727 for (i = 0; i < ocelot->num_phys_ports; i++) {
728 /* Configure the port to read the stats from */
729 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
731 for (j = 0; j < ocelot->num_stats; j++) {
733 unsigned int idx = i * ocelot->num_stats + j;
735 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
736 ocelot->stats_layout[j].offset);
738 if (val < (ocelot->stats[idx] & U32_MAX))
739 ocelot->stats[idx] += (u64)1 << 32;
741 ocelot->stats[idx] = (ocelot->stats[idx] &
742 ~(u64)U32_MAX) + val;
746 cancel_delayed_work(&ocelot->stats_work);
747 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
748 OCELOT_STATS_CHECK_DELAY);
750 mutex_unlock(&ocelot->stats_lock);
753 static void ocelot_get_ethtool_stats(struct net_device *dev,
754 struct ethtool_stats *stats, u64 *data)
756 struct ocelot_port *port = netdev_priv(dev);
757 struct ocelot *ocelot = port->ocelot;
760 /* check and update now */
761 ocelot_check_stats(&ocelot->stats_work.work);
763 /* Copy all counters */
764 for (i = 0; i < ocelot->num_stats; i++)
765 *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
768 static int ocelot_get_sset_count(struct net_device *dev, int sset)
770 struct ocelot_port *port = netdev_priv(dev);
771 struct ocelot *ocelot = port->ocelot;
773 if (sset != ETH_SS_STATS)
775 return ocelot->num_stats;
778 static const struct ethtool_ops ocelot_ethtool_ops = {
779 .get_strings = ocelot_get_strings,
780 .get_ethtool_stats = ocelot_get_ethtool_stats,
781 .get_sset_count = ocelot_get_sset_count,
784 static int ocelot_port_attr_get(struct net_device *dev,
785 struct switchdev_attr *attr)
787 struct ocelot_port *ocelot_port = netdev_priv(dev);
788 struct ocelot *ocelot = ocelot_port->ocelot;
791 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
792 attr->u.ppid.id_len = sizeof(ocelot->base_mac);
793 memcpy(&attr->u.ppid.id, &ocelot->base_mac,
794 attr->u.ppid.id_len);
803 static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
804 struct switchdev_trans *trans,
807 struct ocelot *ocelot = ocelot_port->ocelot;
811 if (switchdev_trans_ph_prepare(trans))
814 if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
817 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
818 ocelot_port->chip_port);
821 case BR_STATE_FORWARDING:
822 ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
824 case BR_STATE_LEARNING:
825 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
829 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
830 ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
834 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
835 ocelot_port->chip_port);
837 /* Apply FWD mask. The loop is needed to add/remove the current port as
838 * a source for the other ports.
840 for (port = 0; port < ocelot->num_phys_ports; port++) {
841 if (ocelot->bridge_fwd_mask & BIT(port)) {
842 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
844 for (i = 0; i < ocelot->num_phys_ports; i++) {
845 unsigned long bond_mask = ocelot->lags[i];
850 if (bond_mask & BIT(port)) {
856 ocelot_write_rix(ocelot,
857 BIT(ocelot->num_phys_ports) | mask,
858 ANA_PGID_PGID, PGID_SRC + port);
860 /* Only the CPU port, this is compatible with link
863 ocelot_write_rix(ocelot,
864 BIT(ocelot->num_phys_ports),
865 ANA_PGID_PGID, PGID_SRC + port);
872 static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
873 unsigned long ageing_clock_t)
875 struct ocelot *ocelot = ocelot_port->ocelot;
876 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
877 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
879 ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
883 static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
885 struct ocelot *ocelot = port->ocelot;
886 u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
890 val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
891 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
892 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
894 val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
895 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
896 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
898 ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
901 static int ocelot_port_attr_set(struct net_device *dev,
902 const struct switchdev_attr *attr,
903 struct switchdev_trans *trans)
905 struct ocelot_port *ocelot_port = netdev_priv(dev);
909 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
910 ocelot_port_attr_stp_state_set(ocelot_port, trans,
913 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
914 ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
916 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
917 ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
927 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
928 const unsigned char *addr,
931 struct ocelot_multicast *mc;
933 list_for_each_entry(mc, &ocelot->multicast, list) {
934 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
941 static int ocelot_port_obj_add_mdb(struct net_device *dev,
942 const struct switchdev_obj_port_mdb *mdb,
943 struct switchdev_trans *trans)
945 struct ocelot_port *port = netdev_priv(dev);
946 struct ocelot *ocelot = port->ocelot;
947 struct ocelot_multicast *mc;
948 unsigned char addr[ETH_ALEN];
955 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
957 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
961 memcpy(mc->addr, mdb->addr, ETH_ALEN);
964 list_add_tail(&mc->list, &ocelot->multicast);
968 memcpy(addr, mc->addr, ETH_ALEN);
972 addr[2] = mc->ports << 0;
973 addr[1] = mc->ports << 8;
974 ocelot_mact_forget(ocelot, addr, vid);
977 mc->ports |= BIT(port->chip_port);
978 addr[2] = mc->ports << 0;
979 addr[1] = mc->ports << 8;
981 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
984 static int ocelot_port_obj_del_mdb(struct net_device *dev,
985 const struct switchdev_obj_port_mdb *mdb)
987 struct ocelot_port *port = netdev_priv(dev);
988 struct ocelot *ocelot = port->ocelot;
989 struct ocelot_multicast *mc;
990 unsigned char addr[ETH_ALEN];
996 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1000 memcpy(addr, mc->addr, ETH_ALEN);
1001 addr[2] = mc->ports << 0;
1002 addr[1] = mc->ports << 8;
1004 ocelot_mact_forget(ocelot, addr, vid);
1006 mc->ports &= ~BIT(port->chip_port);
1008 list_del(&mc->list);
1009 devm_kfree(ocelot->dev, mc);
1013 addr[2] = mc->ports << 0;
1014 addr[1] = mc->ports << 8;
1016 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1019 static int ocelot_port_obj_add(struct net_device *dev,
1020 const struct switchdev_obj *obj,
1021 struct switchdev_trans *trans)
1026 case SWITCHDEV_OBJ_ID_PORT_MDB:
1027 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1037 static int ocelot_port_obj_del(struct net_device *dev,
1038 const struct switchdev_obj *obj)
1043 case SWITCHDEV_OBJ_ID_PORT_MDB:
1044 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1053 static const struct switchdev_ops ocelot_port_switchdev_ops = {
1054 .switchdev_port_attr_get = ocelot_port_attr_get,
1055 .switchdev_port_attr_set = ocelot_port_attr_set,
1056 .switchdev_port_obj_add = ocelot_port_obj_add,
1057 .switchdev_port_obj_del = ocelot_port_obj_del,
1060 static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1061 struct net_device *bridge)
1063 struct ocelot *ocelot = ocelot_port->ocelot;
1065 if (!ocelot->bridge_mask) {
1066 ocelot->hw_bridge_dev = bridge;
1068 if (ocelot->hw_bridge_dev != bridge)
1069 /* This is adding the port to a second bridge, this is
1074 ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1079 static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1080 struct net_device *bridge)
1082 struct ocelot *ocelot = ocelot_port->ocelot;
1084 ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1086 if (!ocelot->bridge_mask)
1087 ocelot->hw_bridge_dev = NULL;
1090 /* Checks if the net_device instance given to us originate from our driver. */
1091 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1093 return dev->netdev_ops == &ocelot_port_netdev_ops;
1096 static int ocelot_netdevice_port_event(struct net_device *dev,
1097 unsigned long event,
1098 struct netdev_notifier_changeupper_info *info)
1100 struct ocelot_port *ocelot_port = netdev_priv(dev);
1103 if (!ocelot_netdevice_dev_check(dev))
1107 case NETDEV_CHANGEUPPER:
1108 if (netif_is_bridge_master(info->upper_dev)) {
1110 err = ocelot_port_bridge_join(ocelot_port,
1113 ocelot_port_bridge_leave(ocelot_port,
1124 static int ocelot_netdevice_event(struct notifier_block *unused,
1125 unsigned long event, void *ptr)
1127 struct netdev_notifier_changeupper_info *info = ptr;
1128 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1131 if (netif_is_lag_master(dev)) {
1132 struct net_device *slave;
1133 struct list_head *iter;
1135 netdev_for_each_lower_dev(dev, slave, iter) {
1136 ret = ocelot_netdevice_port_event(slave, event, info);
1141 ret = ocelot_netdevice_port_event(dev, event, info);
1145 return notifier_from_errno(ret);
1148 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1149 .notifier_call = ocelot_netdevice_event,
1151 EXPORT_SYMBOL(ocelot_netdevice_nb);
1153 int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1155 struct phy_device *phy)
1157 struct ocelot_port *ocelot_port;
1158 struct net_device *dev;
1161 dev = alloc_etherdev(sizeof(struct ocelot_port));
1164 SET_NETDEV_DEV(dev, ocelot->dev);
1165 ocelot_port = netdev_priv(dev);
1166 ocelot_port->dev = dev;
1167 ocelot_port->ocelot = ocelot;
1168 ocelot_port->regs = regs;
1169 ocelot_port->chip_port = port;
1170 ocelot_port->phy = phy;
1171 INIT_LIST_HEAD(&ocelot_port->mc);
1172 ocelot->ports[port] = ocelot_port;
1174 dev->netdev_ops = &ocelot_port_netdev_ops;
1175 dev->ethtool_ops = &ocelot_ethtool_ops;
1176 dev->switchdev_ops = &ocelot_port_switchdev_ops;
1178 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1179 dev->dev_addr[ETH_ALEN - 1] += port;
1180 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
1183 err = register_netdev(dev);
1185 dev_err(ocelot->dev, "register_netdev failed\n");
1186 goto err_register_netdev;
1191 err_register_netdev:
1195 EXPORT_SYMBOL(ocelot_probe_port);
1197 int ocelot_init(struct ocelot *ocelot)
1200 int i, cpu = ocelot->num_phys_ports;
1201 char queue_name[32];
1203 ocelot->stats = devm_kcalloc(ocelot->dev,
1204 ocelot->num_phys_ports * ocelot->num_stats,
1205 sizeof(u64), GFP_KERNEL);
1209 mutex_init(&ocelot->stats_lock);
1210 snprintf(queue_name, sizeof(queue_name), "%s-stats",
1211 dev_name(ocelot->dev));
1212 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1213 if (!ocelot->stats_queue)
1216 ocelot_mact_init(ocelot);
1217 ocelot_vlan_init(ocelot);
1219 for (port = 0; port < ocelot->num_phys_ports; port++) {
1220 /* Clear all counters (5 groups) */
1221 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1222 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1226 /* Only use S-Tag */
1227 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1229 /* Aggregation mode */
1230 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1231 ANA_AGGR_CFG_AC_DMAC_ENA |
1232 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1233 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1235 /* Set MAC age time to default value. The entry is aged after
1238 ocelot_write(ocelot,
1239 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1242 /* Disable learning for frames discarded by VLAN ingress filtering */
1243 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1245 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1246 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1247 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1249 /* Setup flooding PGIDs */
1250 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1251 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1252 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1254 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1255 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1256 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1257 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1260 for (port = 0; port < ocelot->num_phys_ports; port++) {
1261 /* Transmit the frame to the local port. */
1262 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1263 /* Do not forward BPDU frames to the front ports. */
1264 ocelot_write_gix(ocelot,
1265 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1266 ANA_PORT_CPU_FWD_BPDU_CFG,
1268 /* Ensure bridging is disabled */
1269 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1272 /* Configure and enable the CPU port. */
1273 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1274 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1275 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1276 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1277 ANA_PORT_PORT_CFG, cpu);
1279 /* Allow broadcast MAC frames. */
1280 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1281 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1283 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1285 ocelot_write_rix(ocelot,
1286 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1287 ANA_PGID_PGID, PGID_MC);
1288 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1289 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1291 /* CPU port Injection/Extraction configuration */
1292 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1293 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1294 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1295 QSYS_SWITCH_PORT_MODE, cpu);
1296 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
1297 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
1298 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1299 * registers endianness.
1301 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1302 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1303 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1304 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1305 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1306 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1307 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1308 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1309 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1310 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1311 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1312 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1313 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1314 for (i = 0; i < 16; i++)
1315 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1316 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1317 ANA_CPUQ_8021_CFG, i);
1319 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats);
1320 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1321 OCELOT_STATS_CHECK_DELAY);
1324 EXPORT_SYMBOL(ocelot_init);
1326 void ocelot_deinit(struct ocelot *ocelot)
1328 destroy_workqueue(ocelot->stats_queue);
1329 mutex_destroy(&ocelot->stats_lock);
1331 EXPORT_SYMBOL(ocelot_deinit);
1333 MODULE_LICENSE("Dual MIT/GPL");