1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/if_bridge.h>
8 #include <soc/mscc/ocelot_vcap.h>
10 #include "ocelot_vcap.h"
12 #define TABLE_UPDATE_SLEEP_US 10
13 #define TABLE_UPDATE_TIMEOUT_US 100000
15 struct ocelot_mact_entry {
18 enum macaccess_entry_type type;
21 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
23 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
26 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
30 return readx_poll_timeout(ocelot_mact_read_macaccess,
32 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
34 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
37 static void ocelot_mact_select(struct ocelot *ocelot,
38 const unsigned char mac[ETH_ALEN],
41 u32 macl = 0, mach = 0;
43 /* Set the MAC address to handle and the vlan associated in a format
44 * understood by the hardware.
54 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
55 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
59 int ocelot_mact_learn(struct ocelot *ocelot, int port,
60 const unsigned char mac[ETH_ALEN],
61 unsigned int vid, enum macaccess_entry_type type)
63 u32 cmd = ANA_TABLES_MACACCESS_VALID |
64 ANA_TABLES_MACACCESS_DEST_IDX(port) |
65 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
66 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
67 unsigned int mc_ports;
69 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
70 if (type == ENTRYTYPE_MACv4)
71 mc_ports = (mac[1] << 8) | mac[2];
72 else if (type == ENTRYTYPE_MACv6)
73 mc_ports = (mac[0] << 8) | mac[1];
77 if (mc_ports & BIT(ocelot->num_phys_ports))
78 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
80 ocelot_mact_select(ocelot, mac, vid);
82 /* Issue a write command */
83 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
85 return ocelot_mact_wait_for_completion(ocelot);
87 EXPORT_SYMBOL(ocelot_mact_learn);
89 int ocelot_mact_forget(struct ocelot *ocelot,
90 const unsigned char mac[ETH_ALEN], unsigned int vid)
92 ocelot_mact_select(ocelot, mac, vid);
94 /* Issue a forget command */
96 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
97 ANA_TABLES_MACACCESS);
99 return ocelot_mact_wait_for_completion(ocelot);
101 EXPORT_SYMBOL(ocelot_mact_forget);
103 static void ocelot_mact_init(struct ocelot *ocelot)
105 /* Configure the learning mode entries attributes:
106 * - Do not copy the frame to the CPU extraction queues.
107 * - Use the vlan and mac_cpoy for dmac lookup.
109 ocelot_rmw(ocelot, 0,
110 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
111 | ANA_AGENCTRL_LEARN_FWD_KILL
112 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
115 /* Clear the MAC table */
116 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
119 static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
121 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
122 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
123 ANA_PORT_VCAP_S2_CFG, port);
125 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
126 ANA_PORT_VCAP_CFG, port);
128 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
133 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
135 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
138 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
142 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
145 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
146 ANA_TABLES_VLANACCESS_CMD_IDLE,
147 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
150 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
152 /* Select the VID to configure */
153 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
154 ANA_TABLES_VLANTIDX);
155 /* Set the vlan port members mask and issue a write command */
156 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
157 ANA_TABLES_VLANACCESS_CMD_WRITE,
158 ANA_TABLES_VLANACCESS);
160 return ocelot_vlant_wait_for_completion(ocelot);
163 static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
166 struct ocelot_port *ocelot_port = ocelot->ports[port];
169 if (ocelot_port->vid != vid) {
170 /* Always permit deleting the native VLAN (vid = 0) */
171 if (ocelot_port->vid && vid) {
173 "Port already has a native VLAN: %d\n",
177 ocelot_port->vid = vid;
180 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
181 REW_PORT_VLAN_CFG_PORT_VID_M,
182 REW_PORT_VLAN_CFG, port);
184 if (ocelot_port->vlan_aware && !ocelot_port->vid)
185 /* If port is vlan-aware and tagged, drop untagged and priority
188 val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
189 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
190 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
191 ocelot_rmw_gix(ocelot, val,
192 ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
193 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
194 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
195 ANA_PORT_DROP_CFG, port);
197 if (ocelot_port->vlan_aware) {
198 if (ocelot_port->vid)
199 /* Tag all frames except when VID == DEFAULT_VLAN */
200 val = REW_TAG_CFG_TAG_CFG(1);
203 val = REW_TAG_CFG_TAG_CFG(3);
205 /* Port tagging disabled. */
206 val = REW_TAG_CFG_TAG_CFG(0);
208 ocelot_rmw_gix(ocelot, val,
209 REW_TAG_CFG_TAG_CFG_M,
215 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
216 bool vlan_aware, struct switchdev_trans *trans)
218 struct ocelot_port *ocelot_port = ocelot->ports[port];
221 if (switchdev_trans_ph_prepare(trans)) {
222 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
223 struct ocelot_vcap_filter *filter;
225 list_for_each_entry(filter, &block->rules, list) {
226 if (filter->ingress_port_mask & BIT(port) &&
227 filter->action.vid_replace_ena) {
229 "Cannot change VLAN state with vlan modify rules active\n");
237 ocelot_port->vlan_aware = vlan_aware;
240 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
241 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
244 ocelot_rmw_gix(ocelot, val,
245 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
246 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
247 ANA_PORT_VLAN_CFG, port);
249 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
253 EXPORT_SYMBOL(ocelot_port_vlan_filtering);
255 /* Default vlan to clasify for untagged frames (may be zero) */
256 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
258 struct ocelot_port *ocelot_port = ocelot->ports[port];
260 ocelot_rmw_gix(ocelot,
261 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
262 ANA_PORT_VLAN_CFG_VLAN_VID_M,
263 ANA_PORT_VLAN_CFG, port);
265 ocelot_port->pvid = pvid;
268 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
273 /* Make the port a member of the VLAN */
274 ocelot->vlan_mask[vid] |= BIT(port);
275 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
279 /* Default ingress vlan classification */
281 ocelot_port_set_pvid(ocelot, port, vid);
283 /* Untagged egress vlan clasification */
285 ret = ocelot_port_set_native_vlan(ocelot, port, vid);
292 EXPORT_SYMBOL(ocelot_vlan_add);
294 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
296 struct ocelot_port *ocelot_port = ocelot->ports[port];
299 /* Stop the port from being a member of the vlan */
300 ocelot->vlan_mask[vid] &= ~BIT(port);
301 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
306 if (ocelot_port->pvid == vid)
307 ocelot_port_set_pvid(ocelot, port, 0);
310 if (ocelot_port->vid == vid)
311 ocelot_port_set_native_vlan(ocelot, port, 0);
315 EXPORT_SYMBOL(ocelot_vlan_del);
317 static void ocelot_vlan_init(struct ocelot *ocelot)
321 /* Clear VLAN table, by default all ports are members of all VLANs */
322 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
323 ANA_TABLES_VLANACCESS);
324 ocelot_vlant_wait_for_completion(ocelot);
326 /* Configure the port VLAN memberships */
327 for (vid = 1; vid < VLAN_N_VID; vid++) {
328 ocelot->vlan_mask[vid] = 0;
329 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
332 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
333 * traffic. It is added automatically if 8021q module is loaded, but
334 * we can't rely on it since module may be not loaded.
336 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
337 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
339 /* Set vlan ingress filter mask to all ports but the CPU port by
342 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
345 for (port = 0; port < ocelot->num_phys_ports; port++) {
346 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
347 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
351 static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
353 return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
356 int ocelot_port_flush(struct ocelot *ocelot, int port)
358 unsigned int pause_ena;
361 /* Disable dequeuing from the egress queues */
362 ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
363 QSYS_PORT_MODE_DEQUEUE_DIS,
364 QSYS_PORT_MODE, port);
366 /* Disable flow control */
367 ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
368 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
370 /* Disable priority flow control */
371 ocelot_fields_write(ocelot, port,
372 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
374 /* Wait at least the time it takes to receive a frame of maximum length
376 * Worst-case delays for 10 kilobyte jumbo frames are:
378 * 800 μs on a 100M port
379 * 80 μs on a 1G port
380 * 32 μs on a 2.5G port
382 usleep_range(8000, 10000);
384 /* Disable half duplex backpressure. */
385 ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
386 SYS_FRONT_PORT_MODE, port);
388 /* Flush the queues associated with the port. */
389 ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
392 /* Enable dequeuing from the egress queues. */
393 ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
396 /* Wait until flushing is complete. */
397 err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
398 100, 2000000, false, ocelot, port);
400 /* Clear flushing again. */
401 ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
403 /* Re-enable flow control */
404 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
408 EXPORT_SYMBOL(ocelot_port_flush);
410 void ocelot_adjust_link(struct ocelot *ocelot, int port,
411 struct phy_device *phydev)
413 struct ocelot_port *ocelot_port = ocelot->ports[port];
416 switch (phydev->speed) {
418 speed = OCELOT_SPEED_10;
421 speed = OCELOT_SPEED_100;
424 speed = OCELOT_SPEED_1000;
425 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
428 speed = OCELOT_SPEED_2500;
429 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
432 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
433 port, phydev->speed);
437 phy_print_status(phydev);
442 /* Only full duplex supported for now */
443 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
444 mode, DEV_MAC_MODE_CFG);
446 /* Disable HDX fast control */
447 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
450 /* SGMII only for now */
451 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
453 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
456 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
458 /* No aneg on SGMII */
459 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
462 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
464 /* Enable MAC module */
465 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
466 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
468 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
470 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
474 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
475 ANA_PFC_PFC_CFG, port);
477 /* Core: Enable port for frame transfer */
478 ocelot_fields_write(ocelot, port,
479 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
482 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
483 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
484 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
485 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
486 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
487 SYS_MAC_FC_CFG, port);
488 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
490 EXPORT_SYMBOL(ocelot_adjust_link);
492 void ocelot_port_enable(struct ocelot *ocelot, int port,
493 struct phy_device *phy)
495 /* Enable receiving frames on the port, and activate auto-learning of
498 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
499 ANA_PORT_PORT_CFG_RECV_ENA |
500 ANA_PORT_PORT_CFG_PORTID_VAL(port),
501 ANA_PORT_PORT_CFG, port);
503 EXPORT_SYMBOL(ocelot_port_enable);
505 void ocelot_port_disable(struct ocelot *ocelot, int port)
507 struct ocelot_port *ocelot_port = ocelot->ports[port];
509 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
510 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
512 EXPORT_SYMBOL(ocelot_port_disable);
514 void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
515 struct sk_buff *clone)
517 struct ocelot_port *ocelot_port = ocelot->ports[port];
519 spin_lock(&ocelot_port->ts_id_lock);
521 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
522 /* Store timestamp ID in cb[0] of sk_buff */
523 clone->cb[0] = ocelot_port->ts_id;
524 ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
525 skb_queue_tail(&ocelot_port->tx_skbs, clone);
527 spin_unlock(&ocelot_port->ts_id_lock);
529 EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
531 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
532 struct timespec64 *ts)
537 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
539 /* Read current PTP time to get seconds */
540 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
542 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
543 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
544 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
545 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
547 /* Read packet HW timestamp from FIFO */
548 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
549 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
551 /* Sec has incremented since the ts was registered */
552 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
555 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
558 void ocelot_get_txtstamp(struct ocelot *ocelot)
560 int budget = OCELOT_PTP_QUEUE_SZ;
563 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
564 struct skb_shared_hwtstamps shhwtstamps;
565 struct ocelot_port *port;
566 struct timespec64 ts;
570 val = ocelot_read(ocelot, SYS_PTP_STATUS);
572 /* Check if a timestamp can be retrieved */
573 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
576 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
578 /* Retrieve the ts ID and Tx port */
579 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
580 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
582 /* Retrieve its associated skb */
583 port = ocelot->ports[txport];
585 spin_lock_irqsave(&port->tx_skbs.lock, flags);
587 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
588 if (skb->cb[0] != id)
590 __skb_unlink(skb, &port->tx_skbs);
595 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
597 if (WARN_ON(!skb_match))
600 /* Get the h/w timestamp */
601 ocelot_get_hwtimestamp(ocelot, &ts);
603 /* Set the timestamp into the skb */
604 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
605 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
606 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
609 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
612 EXPORT_SYMBOL(ocelot_get_txtstamp);
614 int ocelot_fdb_add(struct ocelot *ocelot, int port,
615 const unsigned char *addr, u16 vid)
617 struct ocelot_port *ocelot_port = ocelot->ports[port];
620 if (port == ocelot->npi)
624 if (!ocelot_port->vlan_aware)
625 /* If the bridge is not VLAN aware and no VID was
626 * provided, set it to pvid to ensure the MAC entry
627 * matches incoming untagged packets
629 vid = ocelot_port->pvid;
631 /* If the bridge is VLAN aware a VID must be provided as
632 * otherwise the learnt entry wouldn't match any frame.
637 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
639 EXPORT_SYMBOL(ocelot_fdb_add);
641 int ocelot_fdb_del(struct ocelot *ocelot, int port,
642 const unsigned char *addr, u16 vid)
644 return ocelot_mact_forget(ocelot, addr, vid);
646 EXPORT_SYMBOL(ocelot_fdb_del);
648 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
649 bool is_static, void *data)
651 struct ocelot_dump_ctx *dump = data;
652 u32 portid = NETLINK_CB(dump->cb->skb).portid;
653 u32 seq = dump->cb->nlh->nlmsg_seq;
654 struct nlmsghdr *nlh;
657 if (dump->idx < dump->cb->args[2])
660 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
661 sizeof(*ndm), NLM_F_MULTI);
665 ndm = nlmsg_data(nlh);
666 ndm->ndm_family = AF_BRIDGE;
669 ndm->ndm_flags = NTF_SELF;
671 ndm->ndm_ifindex = dump->dev->ifindex;
672 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
674 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
675 goto nla_put_failure;
677 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
678 goto nla_put_failure;
680 nlmsg_end(dump->skb, nlh);
687 nlmsg_cancel(dump->skb, nlh);
690 EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
692 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
693 struct ocelot_mact_entry *entry)
695 u32 val, dst, macl, mach;
698 /* Set row and column to read from */
699 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
700 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
702 /* Issue a read command */
704 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
705 ANA_TABLES_MACACCESS);
707 if (ocelot_mact_wait_for_completion(ocelot))
710 /* Read the entry flags */
711 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
712 if (!(val & ANA_TABLES_MACACCESS_VALID))
715 /* If the entry read has another port configured as its destination,
718 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
722 /* Get the entry's MAC address and VLAN id */
723 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
724 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
726 mac[0] = (mach >> 8) & 0xff;
727 mac[1] = (mach >> 0) & 0xff;
728 mac[2] = (macl >> 24) & 0xff;
729 mac[3] = (macl >> 16) & 0xff;
730 mac[4] = (macl >> 8) & 0xff;
731 mac[5] = (macl >> 0) & 0xff;
733 entry->vid = (mach >> 16) & 0xfff;
734 ether_addr_copy(entry->mac, mac);
739 int ocelot_fdb_dump(struct ocelot *ocelot, int port,
740 dsa_fdb_dump_cb_t *cb, void *data)
744 /* Loop through all the mac tables entries. */
745 for (i = 0; i < ocelot->num_mact_rows; i++) {
746 for (j = 0; j < 4; j++) {
747 struct ocelot_mact_entry entry;
751 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
752 /* If the entry is invalid (wrong port, invalid...),
760 is_static = (entry.type == ENTRYTYPE_LOCKED);
762 ret = cb(entry.mac, entry.vid, is_static, data);
770 EXPORT_SYMBOL(ocelot_fdb_dump);
772 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
774 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
775 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
777 EXPORT_SYMBOL(ocelot_hwstamp_get);
779 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
781 struct ocelot_port *ocelot_port = ocelot->ports[port];
782 struct hwtstamp_config cfg;
784 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
787 /* reserved for future extensions */
791 /* Tx type sanity check */
792 switch (cfg.tx_type) {
794 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
796 case HWTSTAMP_TX_ONESTEP_SYNC:
797 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
798 * need to update the origin time.
800 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
802 case HWTSTAMP_TX_OFF:
803 ocelot_port->ptp_cmd = 0;
809 mutex_lock(&ocelot->ptp_lock);
811 switch (cfg.rx_filter) {
812 case HWTSTAMP_FILTER_NONE:
814 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
815 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
816 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
817 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
818 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
819 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
820 case HWTSTAMP_FILTER_PTP_V2_EVENT:
821 case HWTSTAMP_FILTER_PTP_V2_SYNC:
822 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
823 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
826 mutex_unlock(&ocelot->ptp_lock);
830 /* Commit back the result & save it */
831 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
832 mutex_unlock(&ocelot->ptp_lock);
834 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
836 EXPORT_SYMBOL(ocelot_hwstamp_set);
838 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
842 if (sset != ETH_SS_STATS)
845 for (i = 0; i < ocelot->num_stats; i++)
846 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
849 EXPORT_SYMBOL(ocelot_get_strings);
851 /* Caller must hold &ocelot->stats_lock */
852 static void ocelot_update_stats(struct ocelot *ocelot)
856 for (i = 0; i < ocelot->num_phys_ports; i++) {
857 /* Configure the port to read the stats from */
858 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
860 for (j = 0; j < ocelot->num_stats; j++) {
862 unsigned int idx = i * ocelot->num_stats + j;
864 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
865 ocelot->stats_layout[j].offset);
867 if (val < (ocelot->stats[idx] & U32_MAX))
868 ocelot->stats[idx] += (u64)1 << 32;
870 ocelot->stats[idx] = (ocelot->stats[idx] &
871 ~(u64)U32_MAX) + val;
876 static void ocelot_check_stats_work(struct work_struct *work)
878 struct delayed_work *del_work = to_delayed_work(work);
879 struct ocelot *ocelot = container_of(del_work, struct ocelot,
882 mutex_lock(&ocelot->stats_lock);
883 ocelot_update_stats(ocelot);
884 mutex_unlock(&ocelot->stats_lock);
886 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
887 OCELOT_STATS_CHECK_DELAY);
890 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
894 mutex_lock(&ocelot->stats_lock);
896 /* check and update now */
897 ocelot_update_stats(ocelot);
899 /* Copy all counters */
900 for (i = 0; i < ocelot->num_stats; i++)
901 *data++ = ocelot->stats[port * ocelot->num_stats + i];
903 mutex_unlock(&ocelot->stats_lock);
905 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
907 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
909 if (sset != ETH_SS_STATS)
912 return ocelot->num_stats;
914 EXPORT_SYMBOL(ocelot_get_sset_count);
916 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
917 struct ethtool_ts_info *info)
919 info->phc_index = ocelot->ptp_clock ?
920 ptp_clock_index(ocelot->ptp_clock) : -1;
921 if (info->phc_index == -1) {
922 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
923 SOF_TIMESTAMPING_RX_SOFTWARE |
924 SOF_TIMESTAMPING_SOFTWARE;
927 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
928 SOF_TIMESTAMPING_RX_SOFTWARE |
929 SOF_TIMESTAMPING_SOFTWARE |
930 SOF_TIMESTAMPING_TX_HARDWARE |
931 SOF_TIMESTAMPING_RX_HARDWARE |
932 SOF_TIMESTAMPING_RAW_HARDWARE;
933 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
934 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
935 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
936 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
937 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
938 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
942 EXPORT_SYMBOL(ocelot_get_ts_info);
944 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
949 if (!(BIT(port) & ocelot->bridge_mask))
952 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
955 case BR_STATE_FORWARDING:
956 ocelot->bridge_fwd_mask |= BIT(port);
958 case BR_STATE_LEARNING:
959 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
963 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
964 ocelot->bridge_fwd_mask &= ~BIT(port);
968 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
970 /* Apply FWD mask. The loop is needed to add/remove the current port as
971 * a source for the other ports.
973 for (p = 0; p < ocelot->num_phys_ports; p++) {
974 if (ocelot->bridge_fwd_mask & BIT(p)) {
975 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
977 for (i = 0; i < ocelot->num_phys_ports; i++) {
978 unsigned long bond_mask = ocelot->lags[i];
983 if (bond_mask & BIT(p)) {
989 ocelot_write_rix(ocelot, mask,
990 ANA_PGID_PGID, PGID_SRC + p);
992 ocelot_write_rix(ocelot, 0,
993 ANA_PGID_PGID, PGID_SRC + p);
997 EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
999 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
1001 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1003 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1004 * which is clearly not what our intention is. So avoid that.
1009 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1011 EXPORT_SYMBOL(ocelot_set_ageing_time);
1013 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1014 const unsigned char *addr,
1017 struct ocelot_multicast *mc;
1019 list_for_each_entry(mc, &ocelot->multicast, list) {
1020 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1027 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
1029 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
1030 return ENTRYTYPE_MACv4;
1031 if (addr[0] == 0x33 && addr[1] == 0x33)
1032 return ENTRYTYPE_MACv6;
1033 return ENTRYTYPE_NORMAL;
1036 static int ocelot_mdb_get_pgid(struct ocelot *ocelot,
1037 enum macaccess_entry_type entry_type)
1041 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1042 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1043 * destination mask table (PGID), the destination set is programmed as
1044 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1046 if (entry_type == ENTRYTYPE_MACv4 ||
1047 entry_type == ENTRYTYPE_MACv6)
1050 for_each_nonreserved_multicast_dest_pgid(ocelot, pgid) {
1051 struct ocelot_multicast *mc;
1054 list_for_each_entry(mc, &ocelot->multicast, list) {
1055 if (mc->pgid == pgid) {
1068 static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1069 struct ocelot_multicast *mc,
1070 enum macaccess_entry_type entry_type)
1072 memcpy(addr, mc->addr, ETH_ALEN);
1074 if (entry_type == ENTRYTYPE_MACv4) {
1076 addr[1] = mc->ports >> 8;
1077 addr[2] = mc->ports & 0xff;
1078 } else if (entry_type == ENTRYTYPE_MACv6) {
1079 addr[0] = mc->ports >> 8;
1080 addr[1] = mc->ports & 0xff;
1084 int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1085 const struct switchdev_obj_port_mdb *mdb)
1087 struct ocelot_port *ocelot_port = ocelot->ports[port];
1088 enum macaccess_entry_type entry_type;
1089 unsigned char addr[ETH_ALEN];
1090 struct ocelot_multicast *mc;
1094 if (port == ocelot->npi)
1095 port = ocelot->num_phys_ports;
1098 vid = ocelot_port->pvid;
1100 entry_type = ocelot_classify_mdb(mdb->addr);
1102 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1104 int pgid = ocelot_mdb_get_pgid(ocelot, entry_type);
1107 dev_err(ocelot->dev,
1108 "No more PGIDs available for mdb %pM vid %d\n",
1113 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1117 memcpy(mc->addr, mdb->addr, ETH_ALEN);
1121 list_add_tail(&mc->list, &ocelot->multicast);
1126 ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1127 ocelot_mact_forget(ocelot, addr, vid);
1130 mc->ports |= BIT(port);
1131 ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1133 return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type);
1135 EXPORT_SYMBOL(ocelot_port_mdb_add);
1137 int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1138 const struct switchdev_obj_port_mdb *mdb)
1140 struct ocelot_port *ocelot_port = ocelot->ports[port];
1141 enum macaccess_entry_type entry_type;
1142 unsigned char addr[ETH_ALEN];
1143 struct ocelot_multicast *mc;
1146 if (port == ocelot->npi)
1147 port = ocelot->num_phys_ports;
1150 vid = ocelot_port->pvid;
1152 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1156 entry_type = ocelot_classify_mdb(mdb->addr);
1158 ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1159 ocelot_mact_forget(ocelot, addr, vid);
1161 mc->ports &= ~BIT(port);
1163 list_del(&mc->list);
1164 devm_kfree(ocelot->dev, mc);
1168 ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1170 return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type);
1172 EXPORT_SYMBOL(ocelot_port_mdb_del);
1174 int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1175 struct net_device *bridge)
1177 if (!ocelot->bridge_mask) {
1178 ocelot->hw_bridge_dev = bridge;
1180 if (ocelot->hw_bridge_dev != bridge)
1181 /* This is adding the port to a second bridge, this is
1186 ocelot->bridge_mask |= BIT(port);
1190 EXPORT_SYMBOL(ocelot_port_bridge_join);
1192 int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1193 struct net_device *bridge)
1195 struct switchdev_trans trans;
1198 ocelot->bridge_mask &= ~BIT(port);
1200 if (!ocelot->bridge_mask)
1201 ocelot->hw_bridge_dev = NULL;
1203 trans.ph_prepare = true;
1204 ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
1208 trans.ph_prepare = false;
1209 ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
1213 ocelot_port_set_pvid(ocelot, port, 0);
1214 return ocelot_port_set_native_vlan(ocelot, port, 0);
1216 EXPORT_SYMBOL(ocelot_port_bridge_leave);
1218 static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1222 /* Reset destination and aggregation PGIDS */
1223 for_each_unicast_dest_pgid(ocelot, port)
1224 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1226 for_each_aggr_pgid(ocelot, i)
1227 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1230 /* Now, set PGIDs for each LAG */
1231 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1232 unsigned long bond_mask;
1236 bond_mask = ocelot->lags[lag];
1240 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1242 ocelot_write_rix(ocelot, bond_mask,
1243 ANA_PGID_PGID, port);
1244 aggr_idx[aggr_count] = port;
1248 for_each_aggr_pgid(ocelot, i) {
1251 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1253 ac |= BIT(aggr_idx[i % aggr_count]);
1254 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1259 static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1261 unsigned long bond_mask = ocelot->lags[lag];
1264 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1265 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1267 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1269 /* Use lag port as logical port for port i */
1270 ocelot_write_gix(ocelot, port_cfg |
1271 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1272 ANA_PORT_PORT_CFG, p);
1276 int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1277 struct net_device *bond)
1279 struct net_device *ndev;
1284 for_each_netdev_in_bond_rcu(bond, ndev) {
1285 struct ocelot_port_private *priv = netdev_priv(ndev);
1287 bond_mask |= BIT(priv->chip_port);
1291 lp = __ffs(bond_mask);
1293 /* If the new port is the lowest one, use it as the logical port from
1298 ocelot->lags[port] = bond_mask;
1299 bond_mask &= ~BIT(port);
1301 lp = __ffs(bond_mask);
1302 ocelot->lags[lp] = 0;
1306 ocelot->lags[lp] |= BIT(port);
1309 ocelot_setup_lag(ocelot, lag);
1310 ocelot_set_aggr_pgids(ocelot);
1314 EXPORT_SYMBOL(ocelot_port_lag_join);
1316 void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1317 struct net_device *bond)
1322 /* Remove port from any lag */
1323 for (i = 0; i < ocelot->num_phys_ports; i++)
1324 ocelot->lags[i] &= ~BIT(port);
1326 /* if it was the logical port of the lag, move the lag config to the
1329 if (ocelot->lags[port]) {
1330 int n = __ffs(ocelot->lags[port]);
1332 ocelot->lags[n] = ocelot->lags[port];
1333 ocelot->lags[port] = 0;
1335 ocelot_setup_lag(ocelot, n);
1338 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1339 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1340 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1341 ANA_PORT_PORT_CFG, port);
1343 ocelot_set_aggr_pgids(ocelot);
1345 EXPORT_SYMBOL(ocelot_port_lag_leave);
1347 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1348 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1349 * In the special case that it's the NPI port that we're configuring, the
1350 * length of the tag and optional prefix needs to be accounted for privately,
1351 * in order to be able to sustain communication at the requested @sdu.
1353 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
1355 struct ocelot_port *ocelot_port = ocelot->ports[port];
1356 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1357 int pause_start, pause_stop;
1360 if (port == ocelot->npi) {
1361 maxlen += OCELOT_TAG_LEN;
1363 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1364 maxlen += OCELOT_SHORT_PREFIX_LEN;
1365 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1366 maxlen += OCELOT_LONG_PREFIX_LEN;
1369 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1371 /* Set Pause watermark hysteresis */
1372 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1373 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1374 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1376 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1379 /* Tail dropping watermarks */
1380 atop_tot = (ocelot->shared_queue_sz - 9 * maxlen) /
1381 OCELOT_BUFFER_CELL_SZ;
1382 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1383 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1384 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1386 EXPORT_SYMBOL(ocelot_port_set_maxlen);
1388 int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1390 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1392 if (port == ocelot->npi) {
1393 max_mtu -= OCELOT_TAG_LEN;
1395 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1396 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1397 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1398 max_mtu -= OCELOT_LONG_PREFIX_LEN;
1403 EXPORT_SYMBOL(ocelot_get_max_mtu);
1405 void ocelot_init_port(struct ocelot *ocelot, int port)
1407 struct ocelot_port *ocelot_port = ocelot->ports[port];
1409 skb_queue_head_init(&ocelot_port->tx_skbs);
1410 spin_lock_init(&ocelot_port->ts_id_lock);
1412 /* Basic L2 initialization */
1415 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1416 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1418 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1421 /* Load seed (0) and set MAC HDX late collision */
1422 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1423 DEV_MAC_HDX_CFG_SEED_LOAD,
1426 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1429 /* Set Max Length and maximum tags allowed */
1430 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
1431 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
1432 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1433 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
1434 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
1437 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1438 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
1439 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
1441 /* Enable transmission of pause frames */
1442 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1444 /* Drop frames with multicast source address */
1445 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1446 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1447 ANA_PORT_DROP_CFG, port);
1449 /* Set default VLAN and tag type to 8021Q. */
1450 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
1451 REW_PORT_VLAN_CFG_PORT_TPID_M,
1452 REW_PORT_VLAN_CFG, port);
1454 /* Enable vcap lookups */
1455 ocelot_vcap_enable(ocelot, port);
1457 EXPORT_SYMBOL(ocelot_init_port);
1459 /* Configure and enable the CPU port module, which is a set of queues
1460 * accessible through register MMIO, frame DMA or Ethernet (in case
1461 * NPI mode is used).
1463 static void ocelot_cpu_port_init(struct ocelot *ocelot)
1465 int cpu = ocelot->num_phys_ports;
1467 /* The unicast destination PGID for the CPU port module is unused */
1468 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1469 /* Instead set up a multicast destination PGID for traffic copied to
1470 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1471 * addresses will be copied to the CPU via this PGID.
1473 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1474 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1475 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1476 ANA_PORT_PORT_CFG, cpu);
1478 /* Enable CPU port module */
1479 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
1480 /* CPU port Injection/Extraction configuration */
1481 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1482 ocelot->xtr_prefix);
1483 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1484 ocelot->inj_prefix);
1486 /* Configure the CPU port to be VLAN aware */
1487 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1488 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1489 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1490 ANA_PORT_VLAN_CFG, cpu);
1493 int ocelot_init(struct ocelot *ocelot)
1495 char queue_name[32];
1499 if (ocelot->ops->reset) {
1500 ret = ocelot->ops->reset(ocelot);
1502 dev_err(ocelot->dev, "Switch reset failed\n");
1507 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1508 sizeof(u32), GFP_KERNEL);
1512 ocelot->stats = devm_kcalloc(ocelot->dev,
1513 ocelot->num_phys_ports * ocelot->num_stats,
1514 sizeof(u64), GFP_KERNEL);
1518 mutex_init(&ocelot->stats_lock);
1519 mutex_init(&ocelot->ptp_lock);
1520 spin_lock_init(&ocelot->ptp_clock_lock);
1521 snprintf(queue_name, sizeof(queue_name), "%s-stats",
1522 dev_name(ocelot->dev));
1523 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1524 if (!ocelot->stats_queue)
1527 INIT_LIST_HEAD(&ocelot->multicast);
1528 ocelot_mact_init(ocelot);
1529 ocelot_vlan_init(ocelot);
1530 ocelot_vcap_init(ocelot);
1531 ocelot_cpu_port_init(ocelot);
1533 for (port = 0; port < ocelot->num_phys_ports; port++) {
1534 /* Clear all counters (5 groups) */
1535 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1536 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1540 /* Only use S-Tag */
1541 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1543 /* Aggregation mode */
1544 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1545 ANA_AGGR_CFG_AC_DMAC_ENA |
1546 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1547 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1549 /* Set MAC age time to default value. The entry is aged after
1552 ocelot_write(ocelot,
1553 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1556 /* Disable learning for frames discarded by VLAN ingress filtering */
1557 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1559 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1560 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1561 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1563 /* Setup flooding PGIDs */
1564 for (i = 0; i < ocelot->num_flooding_pgids; i++)
1565 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1566 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1567 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1569 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1570 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1571 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1572 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1575 for (port = 0; port < ocelot->num_phys_ports; port++) {
1576 /* Transmit the frame to the local port. */
1577 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1578 /* Do not forward BPDU frames to the front ports. */
1579 ocelot_write_gix(ocelot,
1580 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1581 ANA_PORT_CPU_FWD_BPDU_CFG,
1583 /* Ensure bridging is disabled */
1584 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1587 /* Allow broadcast MAC frames. */
1588 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1589 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1591 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1593 ocelot_write_rix(ocelot,
1594 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1595 ANA_PGID_PGID, PGID_MC);
1596 ocelot_write_rix(ocelot,
1597 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1598 ANA_PGID_PGID, PGID_MCIPV4);
1599 ocelot_write_rix(ocelot,
1600 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1601 ANA_PGID_PGID, PGID_MCIPV6);
1603 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1604 * registers endianness.
1606 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1607 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1608 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1609 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1610 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1611 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1612 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1613 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1614 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1615 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1616 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1617 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1618 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1619 for (i = 0; i < 16; i++)
1620 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1621 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1622 ANA_CPUQ_8021_CFG, i);
1624 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1625 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1626 OCELOT_STATS_CHECK_DELAY);
1630 EXPORT_SYMBOL(ocelot_init);
1632 void ocelot_deinit(struct ocelot *ocelot)
1634 cancel_delayed_work(&ocelot->stats_work);
1635 destroy_workqueue(ocelot->stats_queue);
1636 mutex_destroy(&ocelot->stats_lock);
1638 EXPORT_SYMBOL(ocelot_deinit);
1640 void ocelot_deinit_port(struct ocelot *ocelot, int port)
1642 struct ocelot_port *ocelot_port = ocelot->ports[port];
1644 skb_queue_purge(&ocelot_port->tx_skbs);
1646 EXPORT_SYMBOL(ocelot_deinit_port);
1648 MODULE_LICENSE("Dual MIT/GPL");