1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/dsa/ocelot.h>
8 #include <linux/if_bridge.h>
9 #include <linux/ptp_classify.h>
10 #include <soc/mscc/ocelot_vcap.h>
12 #include "ocelot_vcap.h"
14 #define TABLE_UPDATE_SLEEP_US 10
15 #define TABLE_UPDATE_TIMEOUT_US 100000
17 struct ocelot_mact_entry {
20 enum macaccess_entry_type type;
23 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
25 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
28 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
32 return readx_poll_timeout(ocelot_mact_read_macaccess,
34 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
36 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
39 static void ocelot_mact_select(struct ocelot *ocelot,
40 const unsigned char mac[ETH_ALEN],
43 u32 macl = 0, mach = 0;
45 /* Set the MAC address to handle and the vlan associated in a format
46 * understood by the hardware.
56 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
57 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
61 int ocelot_mact_learn(struct ocelot *ocelot, int port,
62 const unsigned char mac[ETH_ALEN],
63 unsigned int vid, enum macaccess_entry_type type)
65 u32 cmd = ANA_TABLES_MACACCESS_VALID |
66 ANA_TABLES_MACACCESS_DEST_IDX(port) |
67 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
69 unsigned int mc_ports;
71 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
72 if (type == ENTRYTYPE_MACv4)
73 mc_ports = (mac[1] << 8) | mac[2];
74 else if (type == ENTRYTYPE_MACv6)
75 mc_ports = (mac[0] << 8) | mac[1];
79 if (mc_ports & BIT(ocelot->num_phys_ports))
80 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
82 ocelot_mact_select(ocelot, mac, vid);
84 /* Issue a write command */
85 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
87 return ocelot_mact_wait_for_completion(ocelot);
89 EXPORT_SYMBOL(ocelot_mact_learn);
91 int ocelot_mact_forget(struct ocelot *ocelot,
92 const unsigned char mac[ETH_ALEN], unsigned int vid)
94 ocelot_mact_select(ocelot, mac, vid);
96 /* Issue a forget command */
98 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
99 ANA_TABLES_MACACCESS);
101 return ocelot_mact_wait_for_completion(ocelot);
103 EXPORT_SYMBOL(ocelot_mact_forget);
105 static void ocelot_mact_init(struct ocelot *ocelot)
107 /* Configure the learning mode entries attributes:
108 * - Do not copy the frame to the CPU extraction queues.
109 * - Use the vlan and mac_cpoy for dmac lookup.
111 ocelot_rmw(ocelot, 0,
112 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
113 | ANA_AGENCTRL_LEARN_FWD_KILL
114 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
117 /* Clear the MAC table */
118 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
121 static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
123 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
124 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
125 ANA_PORT_VCAP_S2_CFG, port);
127 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
128 ANA_PORT_VCAP_CFG, port);
130 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
135 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
137 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
140 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
144 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
147 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
148 ANA_TABLES_VLANACCESS_CMD_IDLE,
149 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
152 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
154 /* Select the VID to configure */
155 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
156 ANA_TABLES_VLANTIDX);
157 /* Set the vlan port members mask and issue a write command */
158 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
159 ANA_TABLES_VLANACCESS_CMD_WRITE,
160 ANA_TABLES_VLANACCESS);
162 return ocelot_vlant_wait_for_completion(ocelot);
165 static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
166 struct ocelot_vlan native_vlan)
168 struct ocelot_port *ocelot_port = ocelot->ports[port];
171 ocelot_port->native_vlan = native_vlan;
173 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
174 REW_PORT_VLAN_CFG_PORT_VID_M,
175 REW_PORT_VLAN_CFG, port);
177 if (ocelot_port->vlan_aware) {
178 if (native_vlan.valid)
179 /* Tag all frames except when VID == DEFAULT_VLAN */
180 val = REW_TAG_CFG_TAG_CFG(1);
183 val = REW_TAG_CFG_TAG_CFG(3);
185 /* Port tagging disabled. */
186 val = REW_TAG_CFG_TAG_CFG(0);
188 ocelot_rmw_gix(ocelot, val,
189 REW_TAG_CFG_TAG_CFG_M,
193 /* Default vlan to clasify for untagged frames (may be zero) */
194 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
195 struct ocelot_vlan pvid_vlan)
197 struct ocelot_port *ocelot_port = ocelot->ports[port];
200 ocelot_port->pvid_vlan = pvid_vlan;
202 if (!ocelot_port->vlan_aware)
205 ocelot_rmw_gix(ocelot,
206 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
207 ANA_PORT_VLAN_CFG_VLAN_VID_M,
208 ANA_PORT_VLAN_CFG, port);
210 /* If there's no pvid, we should drop not only untagged traffic (which
211 * happens automatically), but also 802.1p traffic which gets
212 * classified to VLAN 0, but that is always in our RX filter, so it
213 * would get accepted were it not for this setting.
215 if (!pvid_vlan.valid && ocelot_port->vlan_aware)
216 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
217 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
219 ocelot_rmw_gix(ocelot, val,
220 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
221 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
222 ANA_PORT_DROP_CFG, port);
225 static int ocelot_vlan_member_set(struct ocelot *ocelot, u32 vlan_mask, u16 vid)
229 err = ocelot_vlant_set_mask(ocelot, vid, vlan_mask);
233 ocelot->vlan_mask[vid] = vlan_mask;
238 static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid)
240 return ocelot_vlan_member_set(ocelot,
241 ocelot->vlan_mask[vid] | BIT(port),
245 static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
247 return ocelot_vlan_member_set(ocelot,
248 ocelot->vlan_mask[vid] & ~BIT(port),
252 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
253 bool vlan_aware, struct netlink_ext_ack *extack)
255 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
256 struct ocelot_port *ocelot_port = ocelot->ports[port];
257 struct ocelot_vcap_filter *filter;
260 list_for_each_entry(filter, &block->rules, list) {
261 if (filter->ingress_port_mask & BIT(port) &&
262 filter->action.vid_replace_ena) {
263 NL_SET_ERR_MSG_MOD(extack,
264 "Cannot change VLAN state with vlan modify rules active");
269 ocelot_port->vlan_aware = vlan_aware;
272 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
273 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
276 ocelot_rmw_gix(ocelot, val,
277 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
278 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
279 ANA_PORT_VLAN_CFG, port);
281 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
282 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
286 EXPORT_SYMBOL(ocelot_port_vlan_filtering);
288 int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
289 bool untagged, struct netlink_ext_ack *extack)
291 struct ocelot_port *ocelot_port = ocelot->ports[port];
293 /* Deny changing the native VLAN, but always permit deleting it */
294 if (untagged && ocelot_port->native_vlan.vid != vid &&
295 ocelot_port->native_vlan.valid) {
296 NL_SET_ERR_MSG_MOD(extack,
297 "Port already has a native VLAN");
303 EXPORT_SYMBOL(ocelot_vlan_prepare);
305 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
310 err = ocelot_vlan_member_add(ocelot, port, vid);
314 /* Default ingress vlan classification */
316 struct ocelot_vlan pvid_vlan;
319 pvid_vlan.valid = true;
320 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
323 /* Untagged egress vlan clasification */
325 struct ocelot_vlan native_vlan;
327 native_vlan.vid = vid;
328 native_vlan.valid = true;
329 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
334 EXPORT_SYMBOL(ocelot_vlan_add);
336 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
338 struct ocelot_port *ocelot_port = ocelot->ports[port];
341 err = ocelot_vlan_member_del(ocelot, port, vid);
346 if (ocelot_port->pvid_vlan.vid == vid) {
347 struct ocelot_vlan pvid_vlan = {0};
349 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
353 if (ocelot_port->native_vlan.vid == vid) {
354 struct ocelot_vlan native_vlan = {0};
356 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
361 EXPORT_SYMBOL(ocelot_vlan_del);
363 static void ocelot_vlan_init(struct ocelot *ocelot)
365 unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
368 /* Clear VLAN table, by default all ports are members of all VLANs */
369 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
370 ANA_TABLES_VLANACCESS);
371 ocelot_vlant_wait_for_completion(ocelot);
373 /* Configure the port VLAN memberships */
374 for (vid = 1; vid < VLAN_N_VID; vid++)
375 ocelot_vlan_member_set(ocelot, 0, vid);
377 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
378 * traffic. It is added automatically if 8021q module is loaded, but
379 * we can't rely on it since module may be not loaded.
381 ocelot_vlan_member_set(ocelot, all_ports, 0);
383 /* Set vlan ingress filter mask to all ports but the CPU port by
386 ocelot_write(ocelot, all_ports, ANA_VLANMASK);
388 for (port = 0; port < ocelot->num_phys_ports; port++) {
389 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
390 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
394 static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
396 return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
399 static int ocelot_port_flush(struct ocelot *ocelot, int port)
401 unsigned int pause_ena;
404 /* Disable dequeuing from the egress queues */
405 ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
406 QSYS_PORT_MODE_DEQUEUE_DIS,
407 QSYS_PORT_MODE, port);
409 /* Disable flow control */
410 ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
411 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
413 /* Disable priority flow control */
414 ocelot_fields_write(ocelot, port,
415 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
417 /* Wait at least the time it takes to receive a frame of maximum length
419 * Worst-case delays for 10 kilobyte jumbo frames are:
421 * 800 μs on a 100M port
422 * 80 μs on a 1G port
423 * 32 μs on a 2.5G port
425 usleep_range(8000, 10000);
427 /* Disable half duplex backpressure. */
428 ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
429 SYS_FRONT_PORT_MODE, port);
431 /* Flush the queues associated with the port. */
432 ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
435 /* Enable dequeuing from the egress queues. */
436 ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
439 /* Wait until flushing is complete. */
440 err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
441 100, 2000000, false, ocelot, port);
443 /* Clear flushing again. */
444 ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
446 /* Re-enable flow control */
447 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
452 void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
453 unsigned int link_an_mode,
454 phy_interface_t interface,
455 unsigned long quirks)
457 struct ocelot_port *ocelot_port = ocelot->ports[port];
460 ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
463 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
465 err = ocelot_port_flush(ocelot, port);
467 dev_err(ocelot->dev, "failed to flush port %d: %d\n",
470 /* Put the port in reset. */
471 if (interface != PHY_INTERFACE_MODE_QSGMII ||
472 !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
473 ocelot_port_rmwl(ocelot_port,
474 DEV_CLOCK_CFG_MAC_TX_RST |
475 DEV_CLOCK_CFG_MAC_RX_RST,
476 DEV_CLOCK_CFG_MAC_TX_RST |
477 DEV_CLOCK_CFG_MAC_RX_RST,
480 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
482 void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
483 struct phy_device *phydev,
484 unsigned int link_an_mode,
485 phy_interface_t interface,
486 int speed, int duplex,
487 bool tx_pause, bool rx_pause,
488 unsigned long quirks)
490 struct ocelot_port *ocelot_port = ocelot->ports[port];
491 int mac_speed, mode = 0;
494 /* The MAC might be integrated in systems where the MAC speed is fixed
495 * and it's the PCS who is performing the rate adaptation, so we have
496 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
497 * (which is also its default value).
499 if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
500 speed == SPEED_1000) {
501 mac_speed = OCELOT_SPEED_1000;
502 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
503 } else if (speed == SPEED_2500) {
504 mac_speed = OCELOT_SPEED_2500;
505 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
506 } else if (speed == SPEED_100) {
507 mac_speed = OCELOT_SPEED_100;
509 mac_speed = OCELOT_SPEED_10;
512 if (duplex == DUPLEX_FULL)
513 mode |= DEV_MAC_MODE_CFG_FDX_ENA;
515 ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
517 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
518 * PORT_RST bits in DEV_CLOCK_CFG.
520 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
525 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
528 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
532 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
535 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
540 /* Handle RX pause in all cases, with 2500base-X this is used for rate
543 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
546 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
547 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
548 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
549 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
551 /* Flow control. Link speed is only used here to evaluate the time
552 * specification in incoming pause frames.
554 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
556 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
558 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
560 /* Undo the effects of ocelot_phylink_mac_link_down:
563 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
564 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
566 /* Core: Enable port for frame transfer */
567 ocelot_fields_write(ocelot, port,
568 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
570 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
572 static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
573 struct sk_buff *clone)
575 struct ocelot_port *ocelot_port = ocelot->ports[port];
578 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
580 if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
581 ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
582 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
586 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
587 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
588 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
590 ocelot_port->ts_id++;
591 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
592 ocelot_port->ts_id = 0;
594 ocelot_port->ptp_skbs_in_flight++;
595 ocelot->ptp_skbs_in_flight++;
597 skb_queue_tail(&ocelot_port->tx_skbs, clone);
599 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
604 u32 ocelot_ptp_rew_op(struct sk_buff *skb)
606 struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
607 u8 ptp_cmd = OCELOT_SKB_CB(skb)->ptp_cmd;
610 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP && clone) {
612 rew_op |= OCELOT_SKB_CB(clone)->ts_id << 3;
613 } else if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
619 EXPORT_SYMBOL(ocelot_ptp_rew_op);
621 static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb)
623 struct ptp_header *hdr;
624 unsigned int ptp_class;
627 ptp_class = ptp_classify_raw(skb);
628 if (ptp_class == PTP_CLASS_NONE)
631 hdr = ptp_parse_header(skb, ptp_class);
635 msgtype = ptp_get_msgtype(hdr, ptp_class);
636 twostep = hdr->flag_field[0] & 0x2;
638 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
644 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
646 struct sk_buff **clone)
648 struct ocelot_port *ocelot_port = ocelot->ports[port];
649 u8 ptp_cmd = ocelot_port->ptp_cmd;
652 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
653 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
654 if (ocelot_ptp_is_onestep_sync(skb)) {
655 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
659 /* Fall back to two-step timestamping */
660 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
663 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
664 *clone = skb_clone_sk(skb);
668 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
672 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
677 EXPORT_SYMBOL(ocelot_port_txtstamp_request);
679 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
680 struct timespec64 *ts)
685 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
687 /* Read current PTP time to get seconds */
688 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
690 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
691 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
692 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
693 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
695 /* Read packet HW timestamp from FIFO */
696 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
697 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
699 /* Sec has incremented since the ts was registered */
700 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
703 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
706 void ocelot_get_txtstamp(struct ocelot *ocelot)
708 int budget = OCELOT_PTP_QUEUE_SZ;
711 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
712 struct skb_shared_hwtstamps shhwtstamps;
713 struct ocelot_port *port;
714 struct timespec64 ts;
718 val = ocelot_read(ocelot, SYS_PTP_STATUS);
720 /* Check if a timestamp can be retrieved */
721 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
724 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
726 /* Retrieve the ts ID and Tx port */
727 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
728 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
730 port = ocelot->ports[txport];
732 spin_lock(&ocelot->ts_id_lock);
733 port->ptp_skbs_in_flight--;
734 ocelot->ptp_skbs_in_flight--;
735 spin_unlock(&ocelot->ts_id_lock);
737 /* Retrieve its associated skb */
738 spin_lock_irqsave(&port->tx_skbs.lock, flags);
740 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
741 if (OCELOT_SKB_CB(skb)->ts_id != id)
743 __skb_unlink(skb, &port->tx_skbs);
748 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
750 if (WARN_ON(!skb_match))
753 /* Get the h/w timestamp */
754 ocelot_get_hwtimestamp(ocelot, &ts);
756 /* Set the timestamp into the skb */
757 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
758 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
759 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
762 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
765 EXPORT_SYMBOL(ocelot_get_txtstamp);
767 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
770 u32 bytes_valid, val;
772 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
773 if (val == XTR_NOT_READY) {
778 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
779 } while (val == XTR_NOT_READY);
790 bytes_valid = XTR_VALID_BYTES(val);
791 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
792 if (val == XTR_ESCAPE)
793 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
799 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
809 static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
813 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
814 err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
816 return (err < 0) ? err : -EIO;
822 int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
824 struct skb_shared_hwtstamps *shhwtstamps;
825 u64 tod_in_ns, full_ts_in_ns;
826 u64 timestamp, src_port, len;
827 u32 xfh[OCELOT_TAG_LEN / 4];
828 struct net_device *dev;
829 struct timespec64 ts;
835 err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
839 ocelot_xfh_get_src_port(xfh, &src_port);
840 ocelot_xfh_get_len(xfh, &len);
841 ocelot_xfh_get_rew_val(xfh, ×tamp);
843 if (WARN_ON(src_port >= ocelot->num_phys_ports))
846 dev = ocelot->ops->port_to_netdev(ocelot, src_port);
850 skb = netdev_alloc_skb(dev, len);
851 if (unlikely(!skb)) {
852 netdev_err(dev, "Unable to allocate sk_buff\n");
856 buf_len = len - ETH_FCS_LEN;
857 buf = (u32 *)skb_put(skb, buf_len);
861 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
868 } while (len < buf_len);
871 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
877 /* Update the statistics if part of the FCS was read before */
878 len -= ETH_FCS_LEN - sz;
880 if (unlikely(dev->features & NETIF_F_RXFCS)) {
881 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
886 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
888 tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
889 if ((tod_in_ns & 0xffffffff) < timestamp)
890 full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
893 full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
896 shhwtstamps = skb_hwtstamps(skb);
897 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
898 shhwtstamps->hwtstamp = full_ts_in_ns;
901 /* Everything we see on an interface that is in the HW bridge
902 * has already been forwarded.
904 if (ocelot->ports[src_port]->bridge)
905 skb->offload_fwd_mark = 1;
907 skb->protocol = eth_type_trans(skb, dev);
917 EXPORT_SYMBOL(ocelot_xtr_poll_frame);
919 bool ocelot_can_inject(struct ocelot *ocelot, int grp)
921 u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
923 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
925 if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
930 EXPORT_SYMBOL(ocelot_can_inject);
932 void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
933 u32 rew_op, struct sk_buff *skb)
935 u32 ifh[OCELOT_TAG_LEN / 4] = {0};
936 unsigned int i, count, last;
938 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
939 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
941 ocelot_ifh_set_bypass(ifh, 1);
942 ocelot_ifh_set_dest(ifh, BIT_ULL(port));
943 ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
944 ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
945 ocelot_ifh_set_rew_op(ifh, rew_op);
947 for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
948 ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
950 count = DIV_ROUND_UP(skb->len, 4);
952 for (i = 0; i < count; i++)
953 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
956 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
957 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
961 /* Indicate EOF and valid bytes in last word */
962 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
963 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
968 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
969 skb_tx_timestamp(skb);
971 skb->dev->stats.tx_packets++;
972 skb->dev->stats.tx_bytes += skb->len;
974 EXPORT_SYMBOL(ocelot_port_inject_frame);
976 void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
978 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
979 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
981 EXPORT_SYMBOL(ocelot_drain_cpu_queue);
983 int ocelot_fdb_add(struct ocelot *ocelot, int port,
984 const unsigned char *addr, u16 vid)
988 if (port == ocelot->npi)
991 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
993 EXPORT_SYMBOL(ocelot_fdb_add);
995 int ocelot_fdb_del(struct ocelot *ocelot, int port,
996 const unsigned char *addr, u16 vid)
998 return ocelot_mact_forget(ocelot, addr, vid);
1000 EXPORT_SYMBOL(ocelot_fdb_del);
1002 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1003 bool is_static, void *data)
1005 struct ocelot_dump_ctx *dump = data;
1006 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1007 u32 seq = dump->cb->nlh->nlmsg_seq;
1008 struct nlmsghdr *nlh;
1011 if (dump->idx < dump->cb->args[2])
1014 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1015 sizeof(*ndm), NLM_F_MULTI);
1019 ndm = nlmsg_data(nlh);
1020 ndm->ndm_family = AF_BRIDGE;
1023 ndm->ndm_flags = NTF_SELF;
1025 ndm->ndm_ifindex = dump->dev->ifindex;
1026 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
1028 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1029 goto nla_put_failure;
1031 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1032 goto nla_put_failure;
1034 nlmsg_end(dump->skb, nlh);
1041 nlmsg_cancel(dump->skb, nlh);
1044 EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1046 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1047 struct ocelot_mact_entry *entry)
1049 u32 val, dst, macl, mach;
1052 /* Set row and column to read from */
1053 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1054 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1056 /* Issue a read command */
1057 ocelot_write(ocelot,
1058 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1059 ANA_TABLES_MACACCESS);
1061 if (ocelot_mact_wait_for_completion(ocelot))
1064 /* Read the entry flags */
1065 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1066 if (!(val & ANA_TABLES_MACACCESS_VALID))
1069 /* If the entry read has another port configured as its destination,
1072 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1076 /* Get the entry's MAC address and VLAN id */
1077 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1078 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1080 mac[0] = (mach >> 8) & 0xff;
1081 mac[1] = (mach >> 0) & 0xff;
1082 mac[2] = (macl >> 24) & 0xff;
1083 mac[3] = (macl >> 16) & 0xff;
1084 mac[4] = (macl >> 8) & 0xff;
1085 mac[5] = (macl >> 0) & 0xff;
1087 entry->vid = (mach >> 16) & 0xfff;
1088 ether_addr_copy(entry->mac, mac);
1093 int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1094 dsa_fdb_dump_cb_t *cb, void *data)
1098 /* Loop through all the mac tables entries. */
1099 for (i = 0; i < ocelot->num_mact_rows; i++) {
1100 for (j = 0; j < 4; j++) {
1101 struct ocelot_mact_entry entry;
1105 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1106 /* If the entry is invalid (wrong port, invalid...),
1114 is_static = (entry.type == ENTRYTYPE_LOCKED);
1116 ret = cb(entry.mac, entry.vid, is_static, data);
1124 EXPORT_SYMBOL(ocelot_fdb_dump);
1126 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
1128 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1129 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1131 EXPORT_SYMBOL(ocelot_hwstamp_get);
1133 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
1135 struct ocelot_port *ocelot_port = ocelot->ports[port];
1136 struct hwtstamp_config cfg;
1138 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1141 /* reserved for future extensions */
1145 /* Tx type sanity check */
1146 switch (cfg.tx_type) {
1147 case HWTSTAMP_TX_ON:
1148 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
1150 case HWTSTAMP_TX_ONESTEP_SYNC:
1151 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1152 * need to update the origin time.
1154 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
1156 case HWTSTAMP_TX_OFF:
1157 ocelot_port->ptp_cmd = 0;
1163 mutex_lock(&ocelot->ptp_lock);
1165 switch (cfg.rx_filter) {
1166 case HWTSTAMP_FILTER_NONE:
1168 case HWTSTAMP_FILTER_ALL:
1169 case HWTSTAMP_FILTER_SOME:
1170 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1171 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1172 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1173 case HWTSTAMP_FILTER_NTP_ALL:
1174 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1175 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1176 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1177 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1178 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1179 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1180 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1181 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1182 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1183 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1186 mutex_unlock(&ocelot->ptp_lock);
1190 /* Commit back the result & save it */
1191 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1192 mutex_unlock(&ocelot->ptp_lock);
1194 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1196 EXPORT_SYMBOL(ocelot_hwstamp_set);
1198 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1202 if (sset != ETH_SS_STATS)
1205 for (i = 0; i < ocelot->num_stats; i++)
1206 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1209 EXPORT_SYMBOL(ocelot_get_strings);
1211 static void ocelot_update_stats(struct ocelot *ocelot)
1215 mutex_lock(&ocelot->stats_lock);
1217 for (i = 0; i < ocelot->num_phys_ports; i++) {
1218 /* Configure the port to read the stats from */
1219 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1221 for (j = 0; j < ocelot->num_stats; j++) {
1223 unsigned int idx = i * ocelot->num_stats + j;
1225 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1226 ocelot->stats_layout[j].offset);
1228 if (val < (ocelot->stats[idx] & U32_MAX))
1229 ocelot->stats[idx] += (u64)1 << 32;
1231 ocelot->stats[idx] = (ocelot->stats[idx] &
1232 ~(u64)U32_MAX) + val;
1236 mutex_unlock(&ocelot->stats_lock);
1239 static void ocelot_check_stats_work(struct work_struct *work)
1241 struct delayed_work *del_work = to_delayed_work(work);
1242 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1245 ocelot_update_stats(ocelot);
1247 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1248 OCELOT_STATS_CHECK_DELAY);
1251 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1255 /* check and update now */
1256 ocelot_update_stats(ocelot);
1258 /* Copy all counters */
1259 for (i = 0; i < ocelot->num_stats; i++)
1260 *data++ = ocelot->stats[port * ocelot->num_stats + i];
1262 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1264 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1266 if (sset != ETH_SS_STATS)
1269 return ocelot->num_stats;
1271 EXPORT_SYMBOL(ocelot_get_sset_count);
1273 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1274 struct ethtool_ts_info *info)
1276 info->phc_index = ocelot->ptp_clock ?
1277 ptp_clock_index(ocelot->ptp_clock) : -1;
1278 if (info->phc_index == -1) {
1279 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1280 SOF_TIMESTAMPING_RX_SOFTWARE |
1281 SOF_TIMESTAMPING_SOFTWARE;
1284 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1285 SOF_TIMESTAMPING_RX_SOFTWARE |
1286 SOF_TIMESTAMPING_SOFTWARE |
1287 SOF_TIMESTAMPING_TX_HARDWARE |
1288 SOF_TIMESTAMPING_RX_HARDWARE |
1289 SOF_TIMESTAMPING_RAW_HARDWARE;
1290 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1291 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1292 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1296 EXPORT_SYMBOL(ocelot_get_ts_info);
1298 static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
1299 bool only_active_ports)
1304 for (port = 0; port < ocelot->num_phys_ports; port++) {
1305 struct ocelot_port *ocelot_port = ocelot->ports[port];
1310 if (ocelot_port->bond == bond) {
1311 if (only_active_ports && !ocelot_port->lag_tx_active)
1321 static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port,
1322 struct net_device *bridge)
1324 struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1328 if (!ocelot_port || ocelot_port->bridge != bridge ||
1329 ocelot_port->stp_state != BR_STATE_FORWARDING)
1332 for (port = 0; port < ocelot->num_phys_ports; port++) {
1333 ocelot_port = ocelot->ports[port];
1338 if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1339 ocelot_port->bridge == bridge)
1346 static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
1351 for (port = 0; port < ocelot->num_phys_ports; port++) {
1352 struct ocelot_port *ocelot_port = ocelot->ports[port];
1357 if (ocelot_port->is_dsa_8021q_cpu)
1364 void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1366 unsigned long cpu_fwd_mask;
1369 /* If a DSA tag_8021q CPU exists, it needs to be included in the
1370 * regular forwarding path of the front ports regardless of whether
1371 * those are bridged or standalone.
1372 * If DSA tag_8021q is not used, this returns 0, which is fine because
1373 * the hardware-based CPU port module can be a destination for packets
1374 * even if it isn't part of PGID_SRC.
1376 cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1378 /* Apply FWD mask. The loop is needed to add/remove the current port as
1379 * a source for the other ports.
1381 for (port = 0; port < ocelot->num_phys_ports; port++) {
1382 struct ocelot_port *ocelot_port = ocelot->ports[port];
1386 /* Unused ports can't send anywhere */
1388 } else if (ocelot_port->is_dsa_8021q_cpu) {
1389 /* The DSA tag_8021q CPU ports need to be able to
1390 * forward packets to all other ports except for
1393 mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1394 mask &= ~cpu_fwd_mask;
1395 } else if (ocelot_port->bridge) {
1396 struct net_device *bridge = ocelot_port->bridge;
1397 struct net_device *bond = ocelot_port->bond;
1399 mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge);
1400 mask |= cpu_fwd_mask;
1403 mask &= ~ocelot_get_bond_mask(ocelot, bond,
1407 /* Standalone ports forward only to DSA tag_8021q CPU
1408 * ports (if those exist), or to the hardware CPU port
1411 mask = cpu_fwd_mask;
1414 ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
1417 EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
1419 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1421 struct ocelot_port *ocelot_port = ocelot->ports[port];
1424 ocelot_port->stp_state = state;
1426 if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1427 ocelot_port->learn_ena)
1428 learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1430 ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1431 ANA_PORT_PORT_CFG, port);
1433 ocelot_apply_bridge_fwd_mask(ocelot);
1435 EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1437 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
1439 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1441 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1442 * which is clearly not what our intention is. So avoid that.
1447 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1449 EXPORT_SYMBOL(ocelot_set_ageing_time);
1451 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1452 const unsigned char *addr,
1455 struct ocelot_multicast *mc;
1457 list_for_each_entry(mc, &ocelot->multicast, list) {
1458 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1465 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
1467 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
1468 return ENTRYTYPE_MACv4;
1469 if (addr[0] == 0x33 && addr[1] == 0x33)
1470 return ENTRYTYPE_MACv6;
1471 return ENTRYTYPE_LOCKED;
1474 static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1475 unsigned long ports)
1477 struct ocelot_pgid *pgid;
1479 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1481 return ERR_PTR(-ENOMEM);
1483 pgid->ports = ports;
1484 pgid->index = index;
1485 refcount_set(&pgid->refcount, 1);
1486 list_add_tail(&pgid->list, &ocelot->pgids);
1491 static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1493 if (!refcount_dec_and_test(&pgid->refcount))
1496 list_del(&pgid->list);
1500 static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1501 const struct ocelot_multicast *mc)
1503 struct ocelot_pgid *pgid;
1506 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1507 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1508 * destination mask table (PGID), the destination set is programmed as
1509 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1511 if (mc->entry_type == ENTRYTYPE_MACv4 ||
1512 mc->entry_type == ENTRYTYPE_MACv6)
1513 return ocelot_pgid_alloc(ocelot, 0, mc->ports);
1515 list_for_each_entry(pgid, &ocelot->pgids, list) {
1516 /* When searching for a nonreserved multicast PGID, ignore the
1517 * dummy PGID of zero that we have for MACv4/MACv6 entries
1519 if (pgid->index && pgid->ports == mc->ports) {
1520 refcount_inc(&pgid->refcount);
1525 /* Search for a free index in the nonreserved multicast PGID area */
1526 for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
1529 list_for_each_entry(pgid, &ocelot->pgids, list) {
1530 if (pgid->index == index) {
1537 return ocelot_pgid_alloc(ocelot, index, mc->ports);
1540 return ERR_PTR(-ENOSPC);
1543 static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1544 struct ocelot_multicast *mc)
1546 ether_addr_copy(addr, mc->addr);
1548 if (mc->entry_type == ENTRYTYPE_MACv4) {
1550 addr[1] = mc->ports >> 8;
1551 addr[2] = mc->ports & 0xff;
1552 } else if (mc->entry_type == ENTRYTYPE_MACv6) {
1553 addr[0] = mc->ports >> 8;
1554 addr[1] = mc->ports & 0xff;
1558 int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1559 const struct switchdev_obj_port_mdb *mdb)
1561 unsigned char addr[ETH_ALEN];
1562 struct ocelot_multicast *mc;
1563 struct ocelot_pgid *pgid;
1566 if (port == ocelot->npi)
1567 port = ocelot->num_phys_ports;
1569 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1572 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1576 mc->entry_type = ocelot_classify_mdb(mdb->addr);
1577 ether_addr_copy(mc->addr, mdb->addr);
1580 list_add_tail(&mc->list, &ocelot->multicast);
1582 /* Existing entry. Clean up the current port mask from
1583 * hardware now, because we'll be modifying it.
1585 ocelot_pgid_free(ocelot, mc->pgid);
1586 ocelot_encode_ports_to_mdb(addr, mc);
1587 ocelot_mact_forget(ocelot, addr, vid);
1590 mc->ports |= BIT(port);
1592 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1594 dev_err(ocelot->dev,
1595 "Cannot allocate PGID for mdb %pM vid %d\n",
1597 devm_kfree(ocelot->dev, mc);
1598 return PTR_ERR(pgid);
1602 ocelot_encode_ports_to_mdb(addr, mc);
1604 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1605 mc->entry_type != ENTRYTYPE_MACv6)
1606 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1609 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1612 EXPORT_SYMBOL(ocelot_port_mdb_add);
1614 int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1615 const struct switchdev_obj_port_mdb *mdb)
1617 unsigned char addr[ETH_ALEN];
1618 struct ocelot_multicast *mc;
1619 struct ocelot_pgid *pgid;
1622 if (port == ocelot->npi)
1623 port = ocelot->num_phys_ports;
1625 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1629 ocelot_encode_ports_to_mdb(addr, mc);
1630 ocelot_mact_forget(ocelot, addr, vid);
1632 ocelot_pgid_free(ocelot, mc->pgid);
1633 mc->ports &= ~BIT(port);
1635 list_del(&mc->list);
1636 devm_kfree(ocelot->dev, mc);
1640 /* We have a PGID with fewer ports now */
1641 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1643 return PTR_ERR(pgid);
1646 ocelot_encode_ports_to_mdb(addr, mc);
1648 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1649 mc->entry_type != ENTRYTYPE_MACv6)
1650 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1653 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1656 EXPORT_SYMBOL(ocelot_port_mdb_del);
1658 void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1659 struct net_device *bridge)
1661 struct ocelot_port *ocelot_port = ocelot->ports[port];
1663 ocelot_port->bridge = bridge;
1665 ocelot_apply_bridge_fwd_mask(ocelot);
1667 EXPORT_SYMBOL(ocelot_port_bridge_join);
1669 void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1670 struct net_device *bridge)
1672 struct ocelot_port *ocelot_port = ocelot->ports[port];
1673 struct ocelot_vlan pvid = {0}, native_vlan = {0};
1675 ocelot_port->bridge = NULL;
1677 ocelot_port_set_pvid(ocelot, port, pvid);
1678 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1679 ocelot_apply_bridge_fwd_mask(ocelot);
1681 EXPORT_SYMBOL(ocelot_port_bridge_leave);
1683 static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1685 unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1688 /* Reset destination and aggregation PGIDS */
1689 for_each_unicast_dest_pgid(ocelot, port)
1690 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1692 for_each_aggr_pgid(ocelot, i)
1693 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1696 /* The visited ports bitmask holds the list of ports offloading any
1697 * bonding interface. Initially we mark all these ports as unvisited,
1698 * then every time we visit a port in this bitmask, we know that it is
1699 * the lowest numbered port, i.e. the one whose logical ID == physical
1700 * port ID == LAG ID. So we mark as visited all further ports in the
1701 * bitmask that are offloading the same bonding interface. This way,
1702 * we set up the aggregation PGIDs only once per bonding interface.
1704 for (port = 0; port < ocelot->num_phys_ports; port++) {
1705 struct ocelot_port *ocelot_port = ocelot->ports[port];
1707 if (!ocelot_port || !ocelot_port->bond)
1710 visited &= ~BIT(port);
1713 /* Now, set PGIDs for each active LAG */
1714 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1715 struct net_device *bond = ocelot->ports[lag]->bond;
1716 int num_active_ports = 0;
1717 unsigned long bond_mask;
1720 if (!bond || (visited & BIT(lag)))
1723 bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1725 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1727 ocelot_write_rix(ocelot, bond_mask,
1728 ANA_PGID_PGID, port);
1729 aggr_idx[num_active_ports++] = port;
1732 for_each_aggr_pgid(ocelot, i) {
1735 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1737 /* Don't do division by zero if there was no active
1738 * port. Just make all aggregation codes zero.
1740 if (num_active_ports)
1741 ac |= BIT(aggr_idx[i % num_active_ports]);
1742 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1745 /* Mark all ports in the same LAG as visited to avoid applying
1746 * the same config again.
1748 for (port = lag; port < ocelot->num_phys_ports; port++) {
1749 struct ocelot_port *ocelot_port = ocelot->ports[port];
1754 if (ocelot_port->bond == bond)
1755 visited |= BIT(port);
1760 /* When offloading a bonding interface, the switch ports configured under the
1761 * same bond must have the same logical port ID, equal to the physical port ID
1762 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
1763 * bridged mode, each port has a logical port ID equal to its physical port ID.
1765 static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1769 for (port = 0; port < ocelot->num_phys_ports; port++) {
1770 struct ocelot_port *ocelot_port = ocelot->ports[port];
1771 struct net_device *bond;
1776 bond = ocelot_port->bond;
1778 int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
1781 ocelot_rmw_gix(ocelot,
1782 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1783 ANA_PORT_PORT_CFG_PORTID_VAL_M,
1784 ANA_PORT_PORT_CFG, port);
1786 ocelot_rmw_gix(ocelot,
1787 ANA_PORT_PORT_CFG_PORTID_VAL(port),
1788 ANA_PORT_PORT_CFG_PORTID_VAL_M,
1789 ANA_PORT_PORT_CFG, port);
1794 int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1795 struct net_device *bond,
1796 struct netdev_lag_upper_info *info)
1798 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1801 ocelot->ports[port]->bond = bond;
1803 ocelot_setup_logical_port_ids(ocelot);
1804 ocelot_apply_bridge_fwd_mask(ocelot);
1805 ocelot_set_aggr_pgids(ocelot);
1809 EXPORT_SYMBOL(ocelot_port_lag_join);
1811 void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1812 struct net_device *bond)
1814 ocelot->ports[port]->bond = NULL;
1816 ocelot_setup_logical_port_ids(ocelot);
1817 ocelot_apply_bridge_fwd_mask(ocelot);
1818 ocelot_set_aggr_pgids(ocelot);
1820 EXPORT_SYMBOL(ocelot_port_lag_leave);
1822 void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
1824 struct ocelot_port *ocelot_port = ocelot->ports[port];
1826 ocelot_port->lag_tx_active = lag_tx_active;
1828 /* Rebalance the LAGs */
1829 ocelot_set_aggr_pgids(ocelot);
1831 EXPORT_SYMBOL(ocelot_port_lag_change);
1833 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1834 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1835 * In the special case that it's the NPI port that we're configuring, the
1836 * length of the tag and optional prefix needs to be accounted for privately,
1837 * in order to be able to sustain communication at the requested @sdu.
1839 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
1841 struct ocelot_port *ocelot_port = ocelot->ports[port];
1842 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1843 int pause_start, pause_stop;
1846 if (port == ocelot->npi) {
1847 maxlen += OCELOT_TAG_LEN;
1849 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1850 maxlen += OCELOT_SHORT_PREFIX_LEN;
1851 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
1852 maxlen += OCELOT_LONG_PREFIX_LEN;
1855 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1857 /* Set Pause watermark hysteresis */
1858 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1859 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1860 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1862 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1865 /* Tail dropping watermarks */
1866 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1867 OCELOT_BUFFER_CELL_SZ;
1868 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1869 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1870 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1872 EXPORT_SYMBOL(ocelot_port_set_maxlen);
1874 int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1876 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1878 if (port == ocelot->npi) {
1879 max_mtu -= OCELOT_TAG_LEN;
1881 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1882 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1883 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
1884 max_mtu -= OCELOT_LONG_PREFIX_LEN;
1889 EXPORT_SYMBOL(ocelot_get_max_mtu);
1891 static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1894 struct ocelot_port *ocelot_port = ocelot->ports[port];
1898 val = ANA_PORT_PORT_CFG_LEARN_ENA;
1900 ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1901 ANA_PORT_PORT_CFG, port);
1903 ocelot_port->learn_ena = enabled;
1906 static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1914 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1917 static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1925 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1928 static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1936 ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1939 int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1940 struct switchdev_brport_flags flags)
1942 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1948 EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1950 void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1951 struct switchdev_brport_flags flags)
1953 if (flags.mask & BR_LEARNING)
1954 ocelot_port_set_learning(ocelot, port,
1955 !!(flags.val & BR_LEARNING));
1957 if (flags.mask & BR_FLOOD)
1958 ocelot_port_set_ucast_flood(ocelot, port,
1959 !!(flags.val & BR_FLOOD));
1961 if (flags.mask & BR_MCAST_FLOOD)
1962 ocelot_port_set_mcast_flood(ocelot, port,
1963 !!(flags.val & BR_MCAST_FLOOD));
1965 if (flags.mask & BR_BCAST_FLOOD)
1966 ocelot_port_set_bcast_flood(ocelot, port,
1967 !!(flags.val & BR_BCAST_FLOOD));
1969 EXPORT_SYMBOL(ocelot_port_bridge_flags);
1971 void ocelot_init_port(struct ocelot *ocelot, int port)
1973 struct ocelot_port *ocelot_port = ocelot->ports[port];
1975 skb_queue_head_init(&ocelot_port->tx_skbs);
1977 /* Basic L2 initialization */
1980 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1981 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1983 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1986 /* Load seed (0) and set MAC HDX late collision */
1987 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1988 DEV_MAC_HDX_CFG_SEED_LOAD,
1991 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1994 /* Set Max Length and maximum tags allowed */
1995 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
1996 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
1997 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1998 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
1999 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2002 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2003 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2004 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2006 /* Enable transmission of pause frames */
2007 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2009 /* Drop frames with multicast source address */
2010 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2011 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2012 ANA_PORT_DROP_CFG, port);
2014 /* Set default VLAN and tag type to 8021Q. */
2015 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2016 REW_PORT_VLAN_CFG_PORT_TPID_M,
2017 REW_PORT_VLAN_CFG, port);
2019 /* Disable source address learning for standalone mode */
2020 ocelot_port_set_learning(ocelot, port, false);
2022 /* Set the port's initial logical port ID value, enable receiving
2023 * frames on it, and configure the MAC address learning type to
2026 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
2027 ANA_PORT_PORT_CFG_RECV_ENA |
2028 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2029 ANA_PORT_PORT_CFG, port);
2031 /* Enable vcap lookups */
2032 ocelot_vcap_enable(ocelot, port);
2034 EXPORT_SYMBOL(ocelot_init_port);
2036 /* Configure and enable the CPU port module, which is a set of queues
2037 * accessible through register MMIO, frame DMA or Ethernet (in case
2038 * NPI mode is used).
2040 static void ocelot_cpu_port_init(struct ocelot *ocelot)
2042 int cpu = ocelot->num_phys_ports;
2044 /* The unicast destination PGID for the CPU port module is unused */
2045 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2046 /* Instead set up a multicast destination PGID for traffic copied to
2047 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2048 * addresses will be copied to the CPU via this PGID.
2050 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2051 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2052 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2053 ANA_PORT_PORT_CFG, cpu);
2055 /* Enable CPU port module */
2056 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
2057 /* CPU port Injection/Extraction configuration */
2058 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2059 OCELOT_TAG_PREFIX_NONE);
2060 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2061 OCELOT_TAG_PREFIX_NONE);
2063 /* Configure the CPU port to be VLAN aware */
2064 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2065 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2066 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2067 ANA_PORT_VLAN_CFG, cpu);
2070 static void ocelot_detect_features(struct ocelot *ocelot)
2074 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2075 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2076 * 192 bytes as the documentation incorrectly says.
2078 mmgt = ocelot_read(ocelot, SYS_MMGT);
2079 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2081 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2082 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2085 int ocelot_init(struct ocelot *ocelot)
2087 char queue_name[32];
2091 if (ocelot->ops->reset) {
2092 ret = ocelot->ops->reset(ocelot);
2094 dev_err(ocelot->dev, "Switch reset failed\n");
2099 ocelot->stats = devm_kcalloc(ocelot->dev,
2100 ocelot->num_phys_ports * ocelot->num_stats,
2101 sizeof(u64), GFP_KERNEL);
2105 mutex_init(&ocelot->stats_lock);
2106 mutex_init(&ocelot->ptp_lock);
2107 spin_lock_init(&ocelot->ptp_clock_lock);
2108 spin_lock_init(&ocelot->ts_id_lock);
2109 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2110 dev_name(ocelot->dev));
2111 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2112 if (!ocelot->stats_queue)
2115 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2117 destroy_workqueue(ocelot->stats_queue);
2121 INIT_LIST_HEAD(&ocelot->multicast);
2122 INIT_LIST_HEAD(&ocelot->pgids);
2123 ocelot_detect_features(ocelot);
2124 ocelot_mact_init(ocelot);
2125 ocelot_vlan_init(ocelot);
2126 ocelot_vcap_init(ocelot);
2127 ocelot_cpu_port_init(ocelot);
2129 for (port = 0; port < ocelot->num_phys_ports; port++) {
2130 /* Clear all counters (5 groups) */
2131 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2132 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2136 /* Only use S-Tag */
2137 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2139 /* Aggregation mode */
2140 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2141 ANA_AGGR_CFG_AC_DMAC_ENA |
2142 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2143 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2144 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2145 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2148 /* Set MAC age time to default value. The entry is aged after
2151 ocelot_write(ocelot,
2152 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2155 /* Disable learning for frames discarded by VLAN ingress filtering */
2156 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2158 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2159 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2160 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2162 /* Setup flooding PGIDs */
2163 for (i = 0; i < ocelot->num_flooding_pgids; i++)
2164 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2165 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2166 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2168 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2169 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2170 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2171 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2174 for (port = 0; port < ocelot->num_phys_ports; port++) {
2175 /* Transmit the frame to the local port. */
2176 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2177 /* Do not forward BPDU frames to the front ports. */
2178 ocelot_write_gix(ocelot,
2179 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2180 ANA_PORT_CPU_FWD_BPDU_CFG,
2182 /* Ensure bridging is disabled */
2183 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2186 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2187 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2189 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2192 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2194 /* Allow broadcast and unknown L2 multicast to the CPU. */
2195 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2196 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2197 ANA_PGID_PGID, PGID_MC);
2198 ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2199 ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2200 ANA_PGID_PGID, PGID_BC);
2201 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2202 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2204 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2205 * registers endianness.
2207 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2208 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2209 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2210 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2211 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2212 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2213 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2214 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2215 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2216 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2217 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2218 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2219 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2220 for (i = 0; i < 16; i++)
2221 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2222 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2223 ANA_CPUQ_8021_CFG, i);
2225 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2226 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2227 OCELOT_STATS_CHECK_DELAY);
2231 EXPORT_SYMBOL(ocelot_init);
2233 void ocelot_deinit(struct ocelot *ocelot)
2235 cancel_delayed_work(&ocelot->stats_work);
2236 destroy_workqueue(ocelot->stats_queue);
2237 destroy_workqueue(ocelot->owq);
2238 mutex_destroy(&ocelot->stats_lock);
2240 EXPORT_SYMBOL(ocelot_deinit);
2242 void ocelot_deinit_port(struct ocelot *ocelot, int port)
2244 struct ocelot_port *ocelot_port = ocelot->ports[port];
2246 skb_queue_purge(&ocelot_port->tx_skbs);
2248 EXPORT_SYMBOL(ocelot_deinit_port);
2250 MODULE_LICENSE("Dual MIT/GPL");