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_TX_RST
,
476 DEV_CLOCK_CFG_MAC_TX_RST
|
477 DEV_CLOCK_CFG_MAC_TX_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 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
569 ocelot_port_writel(ocelot_port
, DEV_CLOCK_CFG_LINK_SPEED(speed
),
573 ocelot_write_gix(ocelot
, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed
),
574 ANA_PFC_PFC_CFG
, port
);
576 /* Core: Enable port for frame transfer */
577 ocelot_fields_write(ocelot
, port
,
578 QSYS_SWITCH_PORT_MODE_PORT_ENA
, 1);
580 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up
);
582 static void ocelot_port_add_txtstamp_skb(struct ocelot
*ocelot
, int port
,
583 struct sk_buff
*clone
)
585 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
587 spin_lock(&ocelot_port
->ts_id_lock
);
589 skb_shinfo(clone
)->tx_flags
|= SKBTX_IN_PROGRESS
;
590 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
591 OCELOT_SKB_CB(clone
)->ts_id
= ocelot_port
->ts_id
;
592 ocelot_port
->ts_id
= (ocelot_port
->ts_id
+ 1) % 4;
593 skb_queue_tail(&ocelot_port
->tx_skbs
, clone
);
595 spin_unlock(&ocelot_port
->ts_id_lock
);
598 u32
ocelot_ptp_rew_op(struct sk_buff
*skb
)
600 struct sk_buff
*clone
= OCELOT_SKB_CB(skb
)->clone
;
601 u8 ptp_cmd
= OCELOT_SKB_CB(skb
)->ptp_cmd
;
604 if (ptp_cmd
== IFH_REW_OP_TWO_STEP_PTP
&& clone
) {
606 rew_op
|= OCELOT_SKB_CB(clone
)->ts_id
<< 3;
607 } else if (ptp_cmd
== IFH_REW_OP_ORIGIN_PTP
) {
613 EXPORT_SYMBOL(ocelot_ptp_rew_op
);
615 static bool ocelot_ptp_is_onestep_sync(struct sk_buff
*skb
)
617 struct ptp_header
*hdr
;
618 unsigned int ptp_class
;
621 ptp_class
= ptp_classify_raw(skb
);
622 if (ptp_class
== PTP_CLASS_NONE
)
625 hdr
= ptp_parse_header(skb
, ptp_class
);
629 msgtype
= ptp_get_msgtype(hdr
, ptp_class
);
630 twostep
= hdr
->flag_field
[0] & 0x2;
632 if (msgtype
== PTP_MSGTYPE_SYNC
&& twostep
== 0)
638 int ocelot_port_txtstamp_request(struct ocelot
*ocelot
, int port
,
640 struct sk_buff
**clone
)
642 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
643 u8 ptp_cmd
= ocelot_port
->ptp_cmd
;
645 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
646 if (ptp_cmd
== IFH_REW_OP_ORIGIN_PTP
) {
647 if (ocelot_ptp_is_onestep_sync(skb
)) {
648 OCELOT_SKB_CB(skb
)->ptp_cmd
= ptp_cmd
;
652 /* Fall back to two-step timestamping */
653 ptp_cmd
= IFH_REW_OP_TWO_STEP_PTP
;
656 if (ptp_cmd
== IFH_REW_OP_TWO_STEP_PTP
) {
657 *clone
= skb_clone_sk(skb
);
661 ocelot_port_add_txtstamp_skb(ocelot
, port
, *clone
);
662 OCELOT_SKB_CB(skb
)->ptp_cmd
= ptp_cmd
;
667 EXPORT_SYMBOL(ocelot_port_txtstamp_request
);
669 static void ocelot_get_hwtimestamp(struct ocelot
*ocelot
,
670 struct timespec64
*ts
)
675 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
677 /* Read current PTP time to get seconds */
678 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
680 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
681 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE
);
682 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
683 ts
->tv_sec
= ocelot_read_rix(ocelot
, PTP_PIN_TOD_SEC_LSB
, TOD_ACC_PIN
);
685 /* Read packet HW timestamp from FIFO */
686 val
= ocelot_read(ocelot
, SYS_PTP_TXSTAMP
);
687 ts
->tv_nsec
= SYS_PTP_TXSTAMP_PTP_TXSTAMP(val
);
689 /* Sec has incremented since the ts was registered */
690 if ((ts
->tv_sec
& 0x1) != !!(val
& SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC
))
693 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
696 void ocelot_get_txtstamp(struct ocelot
*ocelot
)
698 int budget
= OCELOT_PTP_QUEUE_SZ
;
701 struct sk_buff
*skb
, *skb_tmp
, *skb_match
= NULL
;
702 struct skb_shared_hwtstamps shhwtstamps
;
703 struct ocelot_port
*port
;
704 struct timespec64 ts
;
708 val
= ocelot_read(ocelot
, SYS_PTP_STATUS
);
710 /* Check if a timestamp can be retrieved */
711 if (!(val
& SYS_PTP_STATUS_PTP_MESS_VLD
))
714 WARN_ON(val
& SYS_PTP_STATUS_PTP_OVFL
);
716 /* Retrieve the ts ID and Tx port */
717 id
= SYS_PTP_STATUS_PTP_MESS_ID_X(val
);
718 txport
= SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val
);
720 /* Retrieve its associated skb */
721 port
= ocelot
->ports
[txport
];
723 spin_lock_irqsave(&port
->tx_skbs
.lock
, flags
);
725 skb_queue_walk_safe(&port
->tx_skbs
, skb
, skb_tmp
) {
726 if (OCELOT_SKB_CB(skb
)->ts_id
!= id
)
728 __skb_unlink(skb
, &port
->tx_skbs
);
733 spin_unlock_irqrestore(&port
->tx_skbs
.lock
, flags
);
735 /* Get the h/w timestamp */
736 ocelot_get_hwtimestamp(ocelot
, &ts
);
738 if (unlikely(!skb_match
))
741 /* Set the timestamp into the skb */
742 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
743 shhwtstamps
.hwtstamp
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
744 skb_complete_tx_timestamp(skb_match
, &shhwtstamps
);
747 ocelot_write(ocelot
, SYS_PTP_NXT_PTP_NXT
, SYS_PTP_NXT
);
750 EXPORT_SYMBOL(ocelot_get_txtstamp
);
752 static int ocelot_rx_frame_word(struct ocelot
*ocelot
, u8 grp
, bool ifh
,
755 u32 bytes_valid
, val
;
757 val
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
758 if (val
== XTR_NOT_READY
) {
763 val
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
764 } while (val
== XTR_NOT_READY
);
775 bytes_valid
= XTR_VALID_BYTES(val
);
776 val
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
777 if (val
== XTR_ESCAPE
)
778 *rval
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
784 *rval
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
794 static int ocelot_xtr_poll_xfh(struct ocelot
*ocelot
, int grp
, u32
*xfh
)
798 for (i
= 0; i
< OCELOT_TAG_LEN
/ 4; i
++) {
799 err
= ocelot_rx_frame_word(ocelot
, grp
, true, &xfh
[i
]);
801 return (err
< 0) ? err
: -EIO
;
807 int ocelot_xtr_poll_frame(struct ocelot
*ocelot
, int grp
, struct sk_buff
**nskb
)
809 struct skb_shared_hwtstamps
*shhwtstamps
;
810 u64 tod_in_ns
, full_ts_in_ns
;
811 u64 timestamp
, src_port
, len
;
812 u32 xfh
[OCELOT_TAG_LEN
/ 4];
813 struct net_device
*dev
;
814 struct timespec64 ts
;
820 err
= ocelot_xtr_poll_xfh(ocelot
, grp
, xfh
);
824 ocelot_xfh_get_src_port(xfh
, &src_port
);
825 ocelot_xfh_get_len(xfh
, &len
);
826 ocelot_xfh_get_rew_val(xfh
, ×tamp
);
828 if (WARN_ON(src_port
>= ocelot
->num_phys_ports
))
831 dev
= ocelot
->ops
->port_to_netdev(ocelot
, src_port
);
835 skb
= netdev_alloc_skb(dev
, len
);
836 if (unlikely(!skb
)) {
837 netdev_err(dev
, "Unable to allocate sk_buff\n");
841 buf_len
= len
- ETH_FCS_LEN
;
842 buf
= (u32
*)skb_put(skb
, buf_len
);
846 sz
= ocelot_rx_frame_word(ocelot
, grp
, false, &val
);
853 } while (len
< buf_len
);
856 sz
= ocelot_rx_frame_word(ocelot
, grp
, false, &val
);
862 /* Update the statistics if part of the FCS was read before */
863 len
-= ETH_FCS_LEN
- sz
;
865 if (unlikely(dev
->features
& NETIF_F_RXFCS
)) {
866 buf
= (u32
*)skb_put(skb
, ETH_FCS_LEN
);
871 ocelot_ptp_gettime64(&ocelot
->ptp_info
, &ts
);
873 tod_in_ns
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
874 if ((tod_in_ns
& 0xffffffff) < timestamp
)
875 full_ts_in_ns
= (((tod_in_ns
>> 32) - 1) << 32) |
878 full_ts_in_ns
= (tod_in_ns
& GENMASK_ULL(63, 32)) |
881 shhwtstamps
= skb_hwtstamps(skb
);
882 memset(shhwtstamps
, 0, sizeof(struct skb_shared_hwtstamps
));
883 shhwtstamps
->hwtstamp
= full_ts_in_ns
;
886 /* Everything we see on an interface that is in the HW bridge
887 * has already been forwarded.
889 if (ocelot
->ports
[src_port
]->bridge
)
890 skb
->offload_fwd_mark
= 1;
892 skb
->protocol
= eth_type_trans(skb
, dev
);
902 EXPORT_SYMBOL(ocelot_xtr_poll_frame
);
904 bool ocelot_can_inject(struct ocelot
*ocelot
, int grp
)
906 u32 val
= ocelot_read(ocelot
, QS_INJ_STATUS
);
908 if (!(val
& QS_INJ_STATUS_FIFO_RDY(BIT(grp
))))
910 if (val
& QS_INJ_STATUS_WMARK_REACHED(BIT(grp
)))
915 EXPORT_SYMBOL(ocelot_can_inject
);
917 void ocelot_port_inject_frame(struct ocelot
*ocelot
, int port
, int grp
,
918 u32 rew_op
, struct sk_buff
*skb
)
920 u32 ifh
[OCELOT_TAG_LEN
/ 4] = {0};
921 unsigned int i
, count
, last
;
923 ocelot_write_rix(ocelot
, QS_INJ_CTRL_GAP_SIZE(1) |
924 QS_INJ_CTRL_SOF
, QS_INJ_CTRL
, grp
);
926 ocelot_ifh_set_bypass(ifh
, 1);
927 ocelot_ifh_set_dest(ifh
, BIT_ULL(port
));
928 ocelot_ifh_set_tag_type(ifh
, IFH_TAG_TYPE_C
);
929 ocelot_ifh_set_vid(ifh
, skb_vlan_tag_get(skb
));
930 ocelot_ifh_set_rew_op(ifh
, rew_op
);
932 for (i
= 0; i
< OCELOT_TAG_LEN
/ 4; i
++)
933 ocelot_write_rix(ocelot
, ifh
[i
], QS_INJ_WR
, grp
);
935 count
= DIV_ROUND_UP(skb
->len
, 4);
937 for (i
= 0; i
< count
; i
++)
938 ocelot_write_rix(ocelot
, ((u32
*)skb
->data
)[i
], QS_INJ_WR
, grp
);
941 while (i
< (OCELOT_BUFFER_CELL_SZ
/ 4)) {
942 ocelot_write_rix(ocelot
, 0, QS_INJ_WR
, grp
);
946 /* Indicate EOF and valid bytes in last word */
947 ocelot_write_rix(ocelot
, QS_INJ_CTRL_GAP_SIZE(1) |
948 QS_INJ_CTRL_VLD_BYTES(skb
->len
< OCELOT_BUFFER_CELL_SZ
? 0 : last
) |
953 ocelot_write_rix(ocelot
, 0, QS_INJ_WR
, grp
);
954 skb_tx_timestamp(skb
);
956 skb
->dev
->stats
.tx_packets
++;
957 skb
->dev
->stats
.tx_bytes
+= skb
->len
;
959 EXPORT_SYMBOL(ocelot_port_inject_frame
);
961 void ocelot_drain_cpu_queue(struct ocelot
*ocelot
, int grp
)
963 while (ocelot_read(ocelot
, QS_XTR_DATA_PRESENT
) & BIT(grp
))
964 ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
966 EXPORT_SYMBOL(ocelot_drain_cpu_queue
);
968 int ocelot_fdb_add(struct ocelot
*ocelot
, int port
,
969 const unsigned char *addr
, u16 vid
)
973 if (port
== ocelot
->npi
)
976 return ocelot_mact_learn(ocelot
, pgid
, addr
, vid
, ENTRYTYPE_LOCKED
);
978 EXPORT_SYMBOL(ocelot_fdb_add
);
980 int ocelot_fdb_del(struct ocelot
*ocelot
, int port
,
981 const unsigned char *addr
, u16 vid
)
983 return ocelot_mact_forget(ocelot
, addr
, vid
);
985 EXPORT_SYMBOL(ocelot_fdb_del
);
987 int ocelot_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
988 bool is_static
, void *data
)
990 struct ocelot_dump_ctx
*dump
= data
;
991 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
992 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
993 struct nlmsghdr
*nlh
;
996 if (dump
->idx
< dump
->cb
->args
[2])
999 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
1000 sizeof(*ndm
), NLM_F_MULTI
);
1004 ndm
= nlmsg_data(nlh
);
1005 ndm
->ndm_family
= AF_BRIDGE
;
1008 ndm
->ndm_flags
= NTF_SELF
;
1010 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
1011 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
1013 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
1014 goto nla_put_failure
;
1016 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
1017 goto nla_put_failure
;
1019 nlmsg_end(dump
->skb
, nlh
);
1026 nlmsg_cancel(dump
->skb
, nlh
);
1029 EXPORT_SYMBOL(ocelot_port_fdb_do_dump
);
1031 static int ocelot_mact_read(struct ocelot
*ocelot
, int port
, int row
, int col
,
1032 struct ocelot_mact_entry
*entry
)
1034 u32 val
, dst
, macl
, mach
;
1037 /* Set row and column to read from */
1038 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_M_INDEX
, row
);
1039 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_BUCKET
, col
);
1041 /* Issue a read command */
1042 ocelot_write(ocelot
,
1043 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ
),
1044 ANA_TABLES_MACACCESS
);
1046 if (ocelot_mact_wait_for_completion(ocelot
))
1049 /* Read the entry flags */
1050 val
= ocelot_read(ocelot
, ANA_TABLES_MACACCESS
);
1051 if (!(val
& ANA_TABLES_MACACCESS_VALID
))
1054 /* If the entry read has another port configured as its destination,
1057 dst
= (val
& ANA_TABLES_MACACCESS_DEST_IDX_M
) >> 3;
1061 /* Get the entry's MAC address and VLAN id */
1062 macl
= ocelot_read(ocelot
, ANA_TABLES_MACLDATA
);
1063 mach
= ocelot_read(ocelot
, ANA_TABLES_MACHDATA
);
1065 mac
[0] = (mach
>> 8) & 0xff;
1066 mac
[1] = (mach
>> 0) & 0xff;
1067 mac
[2] = (macl
>> 24) & 0xff;
1068 mac
[3] = (macl
>> 16) & 0xff;
1069 mac
[4] = (macl
>> 8) & 0xff;
1070 mac
[5] = (macl
>> 0) & 0xff;
1072 entry
->vid
= (mach
>> 16) & 0xfff;
1073 ether_addr_copy(entry
->mac
, mac
);
1078 int ocelot_fdb_dump(struct ocelot
*ocelot
, int port
,
1079 dsa_fdb_dump_cb_t
*cb
, void *data
)
1083 /* Loop through all the mac tables entries. */
1084 for (i
= 0; i
< ocelot
->num_mact_rows
; i
++) {
1085 for (j
= 0; j
< 4; j
++) {
1086 struct ocelot_mact_entry entry
;
1090 ret
= ocelot_mact_read(ocelot
, port
, i
, j
, &entry
);
1091 /* If the entry is invalid (wrong port, invalid...),
1099 is_static
= (entry
.type
== ENTRYTYPE_LOCKED
);
1101 ret
= cb(entry
.mac
, entry
.vid
, is_static
, data
);
1109 EXPORT_SYMBOL(ocelot_fdb_dump
);
1111 int ocelot_hwstamp_get(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
1113 return copy_to_user(ifr
->ifr_data
, &ocelot
->hwtstamp_config
,
1114 sizeof(ocelot
->hwtstamp_config
)) ? -EFAULT
: 0;
1116 EXPORT_SYMBOL(ocelot_hwstamp_get
);
1118 int ocelot_hwstamp_set(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
1120 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1121 struct hwtstamp_config cfg
;
1123 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
1126 /* reserved for future extensions */
1130 /* Tx type sanity check */
1131 switch (cfg
.tx_type
) {
1132 case HWTSTAMP_TX_ON
:
1133 ocelot_port
->ptp_cmd
= IFH_REW_OP_TWO_STEP_PTP
;
1135 case HWTSTAMP_TX_ONESTEP_SYNC
:
1136 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1137 * need to update the origin time.
1139 ocelot_port
->ptp_cmd
= IFH_REW_OP_ORIGIN_PTP
;
1141 case HWTSTAMP_TX_OFF
:
1142 ocelot_port
->ptp_cmd
= 0;
1148 mutex_lock(&ocelot
->ptp_lock
);
1150 switch (cfg
.rx_filter
) {
1151 case HWTSTAMP_FILTER_NONE
:
1153 case HWTSTAMP_FILTER_ALL
:
1154 case HWTSTAMP_FILTER_SOME
:
1155 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
1156 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
1157 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
1158 case HWTSTAMP_FILTER_NTP_ALL
:
1159 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
1160 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
1161 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
1162 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1163 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
1164 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
1165 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1166 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
1167 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
1168 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
1171 mutex_unlock(&ocelot
->ptp_lock
);
1175 /* Commit back the result & save it */
1176 memcpy(&ocelot
->hwtstamp_config
, &cfg
, sizeof(cfg
));
1177 mutex_unlock(&ocelot
->ptp_lock
);
1179 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
1181 EXPORT_SYMBOL(ocelot_hwstamp_set
);
1183 void ocelot_get_strings(struct ocelot
*ocelot
, int port
, u32 sset
, u8
*data
)
1187 if (sset
!= ETH_SS_STATS
)
1190 for (i
= 0; i
< ocelot
->num_stats
; i
++)
1191 memcpy(data
+ i
* ETH_GSTRING_LEN
, ocelot
->stats_layout
[i
].name
,
1194 EXPORT_SYMBOL(ocelot_get_strings
);
1196 static void ocelot_update_stats(struct ocelot
*ocelot
)
1200 mutex_lock(&ocelot
->stats_lock
);
1202 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
1203 /* Configure the port to read the stats from */
1204 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(i
), SYS_STAT_CFG
);
1206 for (j
= 0; j
< ocelot
->num_stats
; j
++) {
1208 unsigned int idx
= i
* ocelot
->num_stats
+ j
;
1210 val
= ocelot_read_rix(ocelot
, SYS_COUNT_RX_OCTETS
,
1211 ocelot
->stats_layout
[j
].offset
);
1213 if (val
< (ocelot
->stats
[idx
] & U32_MAX
))
1214 ocelot
->stats
[idx
] += (u64
)1 << 32;
1216 ocelot
->stats
[idx
] = (ocelot
->stats
[idx
] &
1217 ~(u64
)U32_MAX
) + val
;
1221 mutex_unlock(&ocelot
->stats_lock
);
1224 static void ocelot_check_stats_work(struct work_struct
*work
)
1226 struct delayed_work
*del_work
= to_delayed_work(work
);
1227 struct ocelot
*ocelot
= container_of(del_work
, struct ocelot
,
1230 ocelot_update_stats(ocelot
);
1232 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
1233 OCELOT_STATS_CHECK_DELAY
);
1236 void ocelot_get_ethtool_stats(struct ocelot
*ocelot
, int port
, u64
*data
)
1240 /* check and update now */
1241 ocelot_update_stats(ocelot
);
1243 /* Copy all counters */
1244 for (i
= 0; i
< ocelot
->num_stats
; i
++)
1245 *data
++ = ocelot
->stats
[port
* ocelot
->num_stats
+ i
];
1247 EXPORT_SYMBOL(ocelot_get_ethtool_stats
);
1249 int ocelot_get_sset_count(struct ocelot
*ocelot
, int port
, int sset
)
1251 if (sset
!= ETH_SS_STATS
)
1254 return ocelot
->num_stats
;
1256 EXPORT_SYMBOL(ocelot_get_sset_count
);
1258 int ocelot_get_ts_info(struct ocelot
*ocelot
, int port
,
1259 struct ethtool_ts_info
*info
)
1261 info
->phc_index
= ocelot
->ptp_clock
?
1262 ptp_clock_index(ocelot
->ptp_clock
) : -1;
1263 if (info
->phc_index
== -1) {
1264 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
1265 SOF_TIMESTAMPING_RX_SOFTWARE
|
1266 SOF_TIMESTAMPING_SOFTWARE
;
1269 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
1270 SOF_TIMESTAMPING_RX_SOFTWARE
|
1271 SOF_TIMESTAMPING_SOFTWARE
|
1272 SOF_TIMESTAMPING_TX_HARDWARE
|
1273 SOF_TIMESTAMPING_RX_HARDWARE
|
1274 SOF_TIMESTAMPING_RAW_HARDWARE
;
1275 info
->tx_types
= BIT(HWTSTAMP_TX_OFF
) | BIT(HWTSTAMP_TX_ON
) |
1276 BIT(HWTSTAMP_TX_ONESTEP_SYNC
);
1277 info
->rx_filters
= BIT(HWTSTAMP_FILTER_NONE
) | BIT(HWTSTAMP_FILTER_ALL
);
1281 EXPORT_SYMBOL(ocelot_get_ts_info
);
1283 static u32
ocelot_get_bond_mask(struct ocelot
*ocelot
, struct net_device
*bond
,
1284 bool only_active_ports
)
1289 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1290 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1295 if (ocelot_port
->bond
== bond
) {
1296 if (only_active_ports
&& !ocelot_port
->lag_tx_active
)
1306 static u32
ocelot_get_bridge_fwd_mask(struct ocelot
*ocelot
,
1307 struct net_device
*bridge
)
1312 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1313 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1318 if (ocelot_port
->stp_state
== BR_STATE_FORWARDING
&&
1319 ocelot_port
->bridge
== bridge
)
1326 static u32
ocelot_get_dsa_8021q_cpu_mask(struct ocelot
*ocelot
)
1331 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1332 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1337 if (ocelot_port
->is_dsa_8021q_cpu
)
1344 void ocelot_apply_bridge_fwd_mask(struct ocelot
*ocelot
)
1346 unsigned long cpu_fwd_mask
;
1349 /* If a DSA tag_8021q CPU exists, it needs to be included in the
1350 * regular forwarding path of the front ports regardless of whether
1351 * those are bridged or standalone.
1352 * If DSA tag_8021q is not used, this returns 0, which is fine because
1353 * the hardware-based CPU port module can be a destination for packets
1354 * even if it isn't part of PGID_SRC.
1356 cpu_fwd_mask
= ocelot_get_dsa_8021q_cpu_mask(ocelot
);
1358 /* Apply FWD mask. The loop is needed to add/remove the current port as
1359 * a source for the other ports.
1361 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1362 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1366 /* Unused ports can't send anywhere */
1368 } else if (ocelot_port
->is_dsa_8021q_cpu
) {
1369 /* The DSA tag_8021q CPU ports need to be able to
1370 * forward packets to all other ports except for
1373 mask
= GENMASK(ocelot
->num_phys_ports
- 1, 0);
1374 mask
&= ~cpu_fwd_mask
;
1375 } else if (ocelot_port
->bridge
) {
1376 struct net_device
*bridge
= ocelot_port
->bridge
;
1377 struct net_device
*bond
= ocelot_port
->bond
;
1379 mask
= ocelot_get_bridge_fwd_mask(ocelot
, bridge
);
1380 mask
|= cpu_fwd_mask
;
1383 mask
&= ~ocelot_get_bond_mask(ocelot
, bond
,
1387 /* Standalone ports forward only to DSA tag_8021q CPU
1388 * ports (if those exist), or to the hardware CPU port
1391 mask
= cpu_fwd_mask
;
1394 ocelot_write_rix(ocelot
, mask
, ANA_PGID_PGID
, PGID_SRC
+ port
);
1397 EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask
);
1399 void ocelot_bridge_stp_state_set(struct ocelot
*ocelot
, int port
, u8 state
)
1401 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1404 ocelot_port
->stp_state
= state
;
1406 if ((state
== BR_STATE_LEARNING
|| state
== BR_STATE_FORWARDING
) &&
1407 ocelot_port
->learn_ena
)
1408 learn_ena
= ANA_PORT_PORT_CFG_LEARN_ENA
;
1410 ocelot_rmw_gix(ocelot
, learn_ena
, ANA_PORT_PORT_CFG_LEARN_ENA
,
1411 ANA_PORT_PORT_CFG
, port
);
1413 ocelot_apply_bridge_fwd_mask(ocelot
);
1415 EXPORT_SYMBOL(ocelot_bridge_stp_state_set
);
1417 void ocelot_set_ageing_time(struct ocelot
*ocelot
, unsigned int msecs
)
1419 unsigned int age_period
= ANA_AUTOAGE_AGE_PERIOD(msecs
/ 2000);
1421 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1422 * which is clearly not what our intention is. So avoid that.
1427 ocelot_rmw(ocelot
, age_period
, ANA_AUTOAGE_AGE_PERIOD_M
, ANA_AUTOAGE
);
1429 EXPORT_SYMBOL(ocelot_set_ageing_time
);
1431 static struct ocelot_multicast
*ocelot_multicast_get(struct ocelot
*ocelot
,
1432 const unsigned char *addr
,
1435 struct ocelot_multicast
*mc
;
1437 list_for_each_entry(mc
, &ocelot
->multicast
, list
) {
1438 if (ether_addr_equal(mc
->addr
, addr
) && mc
->vid
== vid
)
1445 static enum macaccess_entry_type
ocelot_classify_mdb(const unsigned char *addr
)
1447 if (addr
[0] == 0x01 && addr
[1] == 0x00 && addr
[2] == 0x5e)
1448 return ENTRYTYPE_MACv4
;
1449 if (addr
[0] == 0x33 && addr
[1] == 0x33)
1450 return ENTRYTYPE_MACv6
;
1451 return ENTRYTYPE_LOCKED
;
1454 static struct ocelot_pgid
*ocelot_pgid_alloc(struct ocelot
*ocelot
, int index
,
1455 unsigned long ports
)
1457 struct ocelot_pgid
*pgid
;
1459 pgid
= kzalloc(sizeof(*pgid
), GFP_KERNEL
);
1461 return ERR_PTR(-ENOMEM
);
1463 pgid
->ports
= ports
;
1464 pgid
->index
= index
;
1465 refcount_set(&pgid
->refcount
, 1);
1466 list_add_tail(&pgid
->list
, &ocelot
->pgids
);
1471 static void ocelot_pgid_free(struct ocelot
*ocelot
, struct ocelot_pgid
*pgid
)
1473 if (!refcount_dec_and_test(&pgid
->refcount
))
1476 list_del(&pgid
->list
);
1480 static struct ocelot_pgid
*ocelot_mdb_get_pgid(struct ocelot
*ocelot
,
1481 const struct ocelot_multicast
*mc
)
1483 struct ocelot_pgid
*pgid
;
1486 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1487 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1488 * destination mask table (PGID), the destination set is programmed as
1489 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1491 if (mc
->entry_type
== ENTRYTYPE_MACv4
||
1492 mc
->entry_type
== ENTRYTYPE_MACv6
)
1493 return ocelot_pgid_alloc(ocelot
, 0, mc
->ports
);
1495 list_for_each_entry(pgid
, &ocelot
->pgids
, list
) {
1496 /* When searching for a nonreserved multicast PGID, ignore the
1497 * dummy PGID of zero that we have for MACv4/MACv6 entries
1499 if (pgid
->index
&& pgid
->ports
== mc
->ports
) {
1500 refcount_inc(&pgid
->refcount
);
1505 /* Search for a free index in the nonreserved multicast PGID area */
1506 for_each_nonreserved_multicast_dest_pgid(ocelot
, index
) {
1509 list_for_each_entry(pgid
, &ocelot
->pgids
, list
) {
1510 if (pgid
->index
== index
) {
1517 return ocelot_pgid_alloc(ocelot
, index
, mc
->ports
);
1520 return ERR_PTR(-ENOSPC
);
1523 static void ocelot_encode_ports_to_mdb(unsigned char *addr
,
1524 struct ocelot_multicast
*mc
)
1526 ether_addr_copy(addr
, mc
->addr
);
1528 if (mc
->entry_type
== ENTRYTYPE_MACv4
) {
1530 addr
[1] = mc
->ports
>> 8;
1531 addr
[2] = mc
->ports
& 0xff;
1532 } else if (mc
->entry_type
== ENTRYTYPE_MACv6
) {
1533 addr
[0] = mc
->ports
>> 8;
1534 addr
[1] = mc
->ports
& 0xff;
1538 int ocelot_port_mdb_add(struct ocelot
*ocelot
, int port
,
1539 const struct switchdev_obj_port_mdb
*mdb
)
1541 unsigned char addr
[ETH_ALEN
];
1542 struct ocelot_multicast
*mc
;
1543 struct ocelot_pgid
*pgid
;
1546 if (port
== ocelot
->npi
)
1547 port
= ocelot
->num_phys_ports
;
1549 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1552 mc
= devm_kzalloc(ocelot
->dev
, sizeof(*mc
), GFP_KERNEL
);
1556 mc
->entry_type
= ocelot_classify_mdb(mdb
->addr
);
1557 ether_addr_copy(mc
->addr
, mdb
->addr
);
1560 list_add_tail(&mc
->list
, &ocelot
->multicast
);
1562 /* Existing entry. Clean up the current port mask from
1563 * hardware now, because we'll be modifying it.
1565 ocelot_pgid_free(ocelot
, mc
->pgid
);
1566 ocelot_encode_ports_to_mdb(addr
, mc
);
1567 ocelot_mact_forget(ocelot
, addr
, vid
);
1570 mc
->ports
|= BIT(port
);
1572 pgid
= ocelot_mdb_get_pgid(ocelot
, mc
);
1574 dev_err(ocelot
->dev
,
1575 "Cannot allocate PGID for mdb %pM vid %d\n",
1577 devm_kfree(ocelot
->dev
, mc
);
1578 return PTR_ERR(pgid
);
1582 ocelot_encode_ports_to_mdb(addr
, mc
);
1584 if (mc
->entry_type
!= ENTRYTYPE_MACv4
&&
1585 mc
->entry_type
!= ENTRYTYPE_MACv6
)
1586 ocelot_write_rix(ocelot
, pgid
->ports
, ANA_PGID_PGID
,
1589 return ocelot_mact_learn(ocelot
, pgid
->index
, addr
, vid
,
1592 EXPORT_SYMBOL(ocelot_port_mdb_add
);
1594 int ocelot_port_mdb_del(struct ocelot
*ocelot
, int port
,
1595 const struct switchdev_obj_port_mdb
*mdb
)
1597 unsigned char addr
[ETH_ALEN
];
1598 struct ocelot_multicast
*mc
;
1599 struct ocelot_pgid
*pgid
;
1602 if (port
== ocelot
->npi
)
1603 port
= ocelot
->num_phys_ports
;
1605 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1609 ocelot_encode_ports_to_mdb(addr
, mc
);
1610 ocelot_mact_forget(ocelot
, addr
, vid
);
1612 ocelot_pgid_free(ocelot
, mc
->pgid
);
1613 mc
->ports
&= ~BIT(port
);
1615 list_del(&mc
->list
);
1616 devm_kfree(ocelot
->dev
, mc
);
1620 /* We have a PGID with fewer ports now */
1621 pgid
= ocelot_mdb_get_pgid(ocelot
, mc
);
1623 return PTR_ERR(pgid
);
1626 ocelot_encode_ports_to_mdb(addr
, mc
);
1628 if (mc
->entry_type
!= ENTRYTYPE_MACv4
&&
1629 mc
->entry_type
!= ENTRYTYPE_MACv6
)
1630 ocelot_write_rix(ocelot
, pgid
->ports
, ANA_PGID_PGID
,
1633 return ocelot_mact_learn(ocelot
, pgid
->index
, addr
, vid
,
1636 EXPORT_SYMBOL(ocelot_port_mdb_del
);
1638 void ocelot_port_bridge_join(struct ocelot
*ocelot
, int port
,
1639 struct net_device
*bridge
)
1641 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1643 ocelot_port
->bridge
= bridge
;
1645 ocelot_apply_bridge_fwd_mask(ocelot
);
1647 EXPORT_SYMBOL(ocelot_port_bridge_join
);
1649 void ocelot_port_bridge_leave(struct ocelot
*ocelot
, int port
,
1650 struct net_device
*bridge
)
1652 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1653 struct ocelot_vlan pvid
= {0}, native_vlan
= {0};
1655 ocelot_port
->bridge
= NULL
;
1657 ocelot_port_set_pvid(ocelot
, port
, pvid
);
1658 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
1659 ocelot_apply_bridge_fwd_mask(ocelot
);
1661 EXPORT_SYMBOL(ocelot_port_bridge_leave
);
1663 static void ocelot_set_aggr_pgids(struct ocelot
*ocelot
)
1665 unsigned long visited
= GENMASK(ocelot
->num_phys_ports
- 1, 0);
1668 /* Reset destination and aggregation PGIDS */
1669 for_each_unicast_dest_pgid(ocelot
, port
)
1670 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
1672 for_each_aggr_pgid(ocelot
, i
)
1673 ocelot_write_rix(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
1676 /* The visited ports bitmask holds the list of ports offloading any
1677 * bonding interface. Initially we mark all these ports as unvisited,
1678 * then every time we visit a port in this bitmask, we know that it is
1679 * the lowest numbered port, i.e. the one whose logical ID == physical
1680 * port ID == LAG ID. So we mark as visited all further ports in the
1681 * bitmask that are offloading the same bonding interface. This way,
1682 * we set up the aggregation PGIDs only once per bonding interface.
1684 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1685 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1687 if (!ocelot_port
|| !ocelot_port
->bond
)
1690 visited
&= ~BIT(port
);
1693 /* Now, set PGIDs for each active LAG */
1694 for (lag
= 0; lag
< ocelot
->num_phys_ports
; lag
++) {
1695 struct net_device
*bond
= ocelot
->ports
[lag
]->bond
;
1696 int num_active_ports
= 0;
1697 unsigned long bond_mask
;
1700 if (!bond
|| (visited
& BIT(lag
)))
1703 bond_mask
= ocelot_get_bond_mask(ocelot
, bond
, true);
1705 for_each_set_bit(port
, &bond_mask
, ocelot
->num_phys_ports
) {
1707 ocelot_write_rix(ocelot
, bond_mask
,
1708 ANA_PGID_PGID
, port
);
1709 aggr_idx
[num_active_ports
++] = port
;
1712 for_each_aggr_pgid(ocelot
, i
) {
1715 ac
= ocelot_read_rix(ocelot
, ANA_PGID_PGID
, i
);
1717 /* Don't do division by zero if there was no active
1718 * port. Just make all aggregation codes zero.
1720 if (num_active_ports
)
1721 ac
|= BIT(aggr_idx
[i
% num_active_ports
]);
1722 ocelot_write_rix(ocelot
, ac
, ANA_PGID_PGID
, i
);
1725 /* Mark all ports in the same LAG as visited to avoid applying
1726 * the same config again.
1728 for (port
= lag
; port
< ocelot
->num_phys_ports
; port
++) {
1729 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1734 if (ocelot_port
->bond
== bond
)
1735 visited
|= BIT(port
);
1740 /* When offloading a bonding interface, the switch ports configured under the
1741 * same bond must have the same logical port ID, equal to the physical port ID
1742 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
1743 * bridged mode, each port has a logical port ID equal to its physical port ID.
1745 static void ocelot_setup_logical_port_ids(struct ocelot
*ocelot
)
1749 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1750 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1751 struct net_device
*bond
;
1756 bond
= ocelot_port
->bond
;
1758 int lag
= __ffs(ocelot_get_bond_mask(ocelot
, bond
,
1761 ocelot_rmw_gix(ocelot
,
1762 ANA_PORT_PORT_CFG_PORTID_VAL(lag
),
1763 ANA_PORT_PORT_CFG_PORTID_VAL_M
,
1764 ANA_PORT_PORT_CFG
, port
);
1766 ocelot_rmw_gix(ocelot
,
1767 ANA_PORT_PORT_CFG_PORTID_VAL(port
),
1768 ANA_PORT_PORT_CFG_PORTID_VAL_M
,
1769 ANA_PORT_PORT_CFG
, port
);
1774 int ocelot_port_lag_join(struct ocelot
*ocelot
, int port
,
1775 struct net_device
*bond
,
1776 struct netdev_lag_upper_info
*info
)
1778 if (info
->tx_type
!= NETDEV_LAG_TX_TYPE_HASH
)
1781 ocelot
->ports
[port
]->bond
= bond
;
1783 ocelot_setup_logical_port_ids(ocelot
);
1784 ocelot_apply_bridge_fwd_mask(ocelot
);
1785 ocelot_set_aggr_pgids(ocelot
);
1789 EXPORT_SYMBOL(ocelot_port_lag_join
);
1791 void ocelot_port_lag_leave(struct ocelot
*ocelot
, int port
,
1792 struct net_device
*bond
)
1794 ocelot
->ports
[port
]->bond
= NULL
;
1796 ocelot_setup_logical_port_ids(ocelot
);
1797 ocelot_apply_bridge_fwd_mask(ocelot
);
1798 ocelot_set_aggr_pgids(ocelot
);
1800 EXPORT_SYMBOL(ocelot_port_lag_leave
);
1802 void ocelot_port_lag_change(struct ocelot
*ocelot
, int port
, bool lag_tx_active
)
1804 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1806 ocelot_port
->lag_tx_active
= lag_tx_active
;
1808 /* Rebalance the LAGs */
1809 ocelot_set_aggr_pgids(ocelot
);
1811 EXPORT_SYMBOL(ocelot_port_lag_change
);
1813 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1814 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1815 * In the special case that it's the NPI port that we're configuring, the
1816 * length of the tag and optional prefix needs to be accounted for privately,
1817 * in order to be able to sustain communication at the requested @sdu.
1819 void ocelot_port_set_maxlen(struct ocelot
*ocelot
, int port
, size_t sdu
)
1821 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1822 int maxlen
= sdu
+ ETH_HLEN
+ ETH_FCS_LEN
;
1823 int pause_start
, pause_stop
;
1826 if (port
== ocelot
->npi
) {
1827 maxlen
+= OCELOT_TAG_LEN
;
1829 if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_SHORT
)
1830 maxlen
+= OCELOT_SHORT_PREFIX_LEN
;
1831 else if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_LONG
)
1832 maxlen
+= OCELOT_LONG_PREFIX_LEN
;
1835 ocelot_port_writel(ocelot_port
, maxlen
, DEV_MAC_MAXLEN_CFG
);
1837 /* Set Pause watermark hysteresis */
1838 pause_start
= 6 * maxlen
/ OCELOT_BUFFER_CELL_SZ
;
1839 pause_stop
= 4 * maxlen
/ OCELOT_BUFFER_CELL_SZ
;
1840 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_START
,
1842 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_STOP
,
1845 /* Tail dropping watermarks */
1846 atop_tot
= (ocelot
->packet_buffer_size
- 9 * maxlen
) /
1847 OCELOT_BUFFER_CELL_SZ
;
1848 atop
= (9 * maxlen
) / OCELOT_BUFFER_CELL_SZ
;
1849 ocelot_write_rix(ocelot
, ocelot
->ops
->wm_enc(atop
), SYS_ATOP
, port
);
1850 ocelot_write(ocelot
, ocelot
->ops
->wm_enc(atop_tot
), SYS_ATOP_TOT_CFG
);
1852 EXPORT_SYMBOL(ocelot_port_set_maxlen
);
1854 int ocelot_get_max_mtu(struct ocelot
*ocelot
, int port
)
1856 int max_mtu
= 65535 - ETH_HLEN
- ETH_FCS_LEN
;
1858 if (port
== ocelot
->npi
) {
1859 max_mtu
-= OCELOT_TAG_LEN
;
1861 if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_SHORT
)
1862 max_mtu
-= OCELOT_SHORT_PREFIX_LEN
;
1863 else if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_LONG
)
1864 max_mtu
-= OCELOT_LONG_PREFIX_LEN
;
1869 EXPORT_SYMBOL(ocelot_get_max_mtu
);
1871 static void ocelot_port_set_learning(struct ocelot
*ocelot
, int port
,
1874 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1878 val
= ANA_PORT_PORT_CFG_LEARN_ENA
;
1880 ocelot_rmw_gix(ocelot
, val
, ANA_PORT_PORT_CFG_LEARN_ENA
,
1881 ANA_PORT_PORT_CFG
, port
);
1883 ocelot_port
->learn_ena
= enabled
;
1886 static void ocelot_port_set_ucast_flood(struct ocelot
*ocelot
, int port
,
1894 ocelot_rmw_rix(ocelot
, val
, BIT(port
), ANA_PGID_PGID
, PGID_UC
);
1897 static void ocelot_port_set_mcast_flood(struct ocelot
*ocelot
, int port
,
1905 ocelot_rmw_rix(ocelot
, val
, BIT(port
), ANA_PGID_PGID
, PGID_MC
);
1908 static void ocelot_port_set_bcast_flood(struct ocelot
*ocelot
, int port
,
1916 ocelot_rmw_rix(ocelot
, val
, BIT(port
), ANA_PGID_PGID
, PGID_BC
);
1919 int ocelot_port_pre_bridge_flags(struct ocelot
*ocelot
, int port
,
1920 struct switchdev_brport_flags flags
)
1922 if (flags
.mask
& ~(BR_LEARNING
| BR_FLOOD
| BR_MCAST_FLOOD
|
1928 EXPORT_SYMBOL(ocelot_port_pre_bridge_flags
);
1930 void ocelot_port_bridge_flags(struct ocelot
*ocelot
, int port
,
1931 struct switchdev_brport_flags flags
)
1933 if (flags
.mask
& BR_LEARNING
)
1934 ocelot_port_set_learning(ocelot
, port
,
1935 !!(flags
.val
& BR_LEARNING
));
1937 if (flags
.mask
& BR_FLOOD
)
1938 ocelot_port_set_ucast_flood(ocelot
, port
,
1939 !!(flags
.val
& BR_FLOOD
));
1941 if (flags
.mask
& BR_MCAST_FLOOD
)
1942 ocelot_port_set_mcast_flood(ocelot
, port
,
1943 !!(flags
.val
& BR_MCAST_FLOOD
));
1945 if (flags
.mask
& BR_BCAST_FLOOD
)
1946 ocelot_port_set_bcast_flood(ocelot
, port
,
1947 !!(flags
.val
& BR_BCAST_FLOOD
));
1949 EXPORT_SYMBOL(ocelot_port_bridge_flags
);
1951 void ocelot_init_port(struct ocelot
*ocelot
, int port
)
1953 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1955 skb_queue_head_init(&ocelot_port
->tx_skbs
);
1956 spin_lock_init(&ocelot_port
->ts_id_lock
);
1958 /* Basic L2 initialization */
1961 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1962 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1964 ocelot_port_writel(ocelot_port
, DEV_MAC_IFG_CFG_TX_IFG(5),
1967 /* Load seed (0) and set MAC HDX late collision */
1968 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1969 DEV_MAC_HDX_CFG_SEED_LOAD
,
1972 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1975 /* Set Max Length and maximum tags allowed */
1976 ocelot_port_set_maxlen(ocelot
, port
, ETH_DATA_LEN
);
1977 ocelot_port_writel(ocelot_port
, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD
) |
1978 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA
|
1979 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA
|
1980 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA
,
1983 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1984 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_HIGH_CFG
);
1985 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_LOW_CFG
);
1987 /* Enable transmission of pause frames */
1988 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_ENA
, 1);
1990 /* Drop frames with multicast source address */
1991 ocelot_rmw_gix(ocelot
, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
1992 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
1993 ANA_PORT_DROP_CFG
, port
);
1995 /* Set default VLAN and tag type to 8021Q. */
1996 ocelot_rmw_gix(ocelot
, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q
),
1997 REW_PORT_VLAN_CFG_PORT_TPID_M
,
1998 REW_PORT_VLAN_CFG
, port
);
2000 /* Disable source address learning for standalone mode */
2001 ocelot_port_set_learning(ocelot
, port
, false);
2003 /* Set the port's initial logical port ID value, enable receiving
2004 * frames on it, and configure the MAC address learning type to
2007 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_LEARNAUTO
|
2008 ANA_PORT_PORT_CFG_RECV_ENA
|
2009 ANA_PORT_PORT_CFG_PORTID_VAL(port
),
2010 ANA_PORT_PORT_CFG
, port
);
2012 /* Enable vcap lookups */
2013 ocelot_vcap_enable(ocelot
, port
);
2015 EXPORT_SYMBOL(ocelot_init_port
);
2017 /* Configure and enable the CPU port module, which is a set of queues
2018 * accessible through register MMIO, frame DMA or Ethernet (in case
2019 * NPI mode is used).
2021 static void ocelot_cpu_port_init(struct ocelot
*ocelot
)
2023 int cpu
= ocelot
->num_phys_ports
;
2025 /* The unicast destination PGID for the CPU port module is unused */
2026 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, cpu
);
2027 /* Instead set up a multicast destination PGID for traffic copied to
2028 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2029 * addresses will be copied to the CPU via this PGID.
2031 ocelot_write_rix(ocelot
, BIT(cpu
), ANA_PGID_PGID
, PGID_CPU
);
2032 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_RECV_ENA
|
2033 ANA_PORT_PORT_CFG_PORTID_VAL(cpu
),
2034 ANA_PORT_PORT_CFG
, cpu
);
2036 /* Enable CPU port module */
2037 ocelot_fields_write(ocelot
, cpu
, QSYS_SWITCH_PORT_MODE_PORT_ENA
, 1);
2038 /* CPU port Injection/Extraction configuration */
2039 ocelot_fields_write(ocelot
, cpu
, SYS_PORT_MODE_INCL_XTR_HDR
,
2040 OCELOT_TAG_PREFIX_NONE
);
2041 ocelot_fields_write(ocelot
, cpu
, SYS_PORT_MODE_INCL_INJ_HDR
,
2042 OCELOT_TAG_PREFIX_NONE
);
2044 /* Configure the CPU port to be VLAN aware */
2045 ocelot_write_gix(ocelot
, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2046 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
2047 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2048 ANA_PORT_VLAN_CFG
, cpu
);
2051 static void ocelot_detect_features(struct ocelot
*ocelot
)
2055 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2056 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2057 * 192 bytes as the documentation incorrectly says.
2059 mmgt
= ocelot_read(ocelot
, SYS_MMGT
);
2060 ocelot
->packet_buffer_size
= 240 * SYS_MMGT_FREECNT(mmgt
);
2062 eq_ctrl
= ocelot_read(ocelot
, QSYS_EQ_CTRL
);
2063 ocelot
->num_frame_refs
= QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl
);
2066 int ocelot_init(struct ocelot
*ocelot
)
2068 char queue_name
[32];
2072 if (ocelot
->ops
->reset
) {
2073 ret
= ocelot
->ops
->reset(ocelot
);
2075 dev_err(ocelot
->dev
, "Switch reset failed\n");
2080 ocelot
->stats
= devm_kcalloc(ocelot
->dev
,
2081 ocelot
->num_phys_ports
* ocelot
->num_stats
,
2082 sizeof(u64
), GFP_KERNEL
);
2086 mutex_init(&ocelot
->stats_lock
);
2087 mutex_init(&ocelot
->ptp_lock
);
2088 spin_lock_init(&ocelot
->ptp_clock_lock
);
2089 snprintf(queue_name
, sizeof(queue_name
), "%s-stats",
2090 dev_name(ocelot
->dev
));
2091 ocelot
->stats_queue
= create_singlethread_workqueue(queue_name
);
2092 if (!ocelot
->stats_queue
)
2095 ocelot
->owq
= alloc_ordered_workqueue("ocelot-owq", 0);
2097 destroy_workqueue(ocelot
->stats_queue
);
2101 INIT_LIST_HEAD(&ocelot
->multicast
);
2102 INIT_LIST_HEAD(&ocelot
->pgids
);
2103 ocelot_detect_features(ocelot
);
2104 ocelot_mact_init(ocelot
);
2105 ocelot_vlan_init(ocelot
);
2106 ocelot_vcap_init(ocelot
);
2107 ocelot_cpu_port_init(ocelot
);
2109 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
2110 /* Clear all counters (5 groups) */
2111 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(port
) |
2112 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2116 /* Only use S-Tag */
2117 ocelot_write(ocelot
, ETH_P_8021AD
, SYS_VLAN_ETYPE_CFG
);
2119 /* Aggregation mode */
2120 ocelot_write(ocelot
, ANA_AGGR_CFG_AC_SMAC_ENA
|
2121 ANA_AGGR_CFG_AC_DMAC_ENA
|
2122 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA
|
2123 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA
|
2124 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA
|
2125 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA
,
2128 /* Set MAC age time to default value. The entry is aged after
2131 ocelot_write(ocelot
,
2132 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME
/ 2 / HZ
),
2135 /* Disable learning for frames discarded by VLAN ingress filtering */
2136 regmap_field_write(ocelot
->regfields
[ANA_ADVLEARN_VLAN_CHK
], 1);
2138 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2139 ocelot_write(ocelot
, SYS_FRM_AGING_AGE_TX_ENA
|
2140 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING
);
2142 /* Setup flooding PGIDs */
2143 for (i
= 0; i
< ocelot
->num_flooding_pgids
; i
++)
2144 ocelot_write_rix(ocelot
, ANA_FLOODING_FLD_MULTICAST(PGID_MC
) |
2145 ANA_FLOODING_FLD_BROADCAST(PGID_BC
) |
2146 ANA_FLOODING_FLD_UNICAST(PGID_UC
),
2148 ocelot_write(ocelot
, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6
) |
2149 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC
) |
2150 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4
) |
2151 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC
),
2154 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
2155 /* Transmit the frame to the local port. */
2156 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
2157 /* Do not forward BPDU frames to the front ports. */
2158 ocelot_write_gix(ocelot
,
2159 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2160 ANA_PORT_CPU_FWD_BPDU_CFG
,
2162 /* Ensure bridging is disabled */
2163 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_SRC
+ port
);
2166 for_each_nonreserved_multicast_dest_pgid(ocelot
, i
) {
2167 u32 val
= ANA_PGID_PGID_PGID(GENMASK(ocelot
->num_phys_ports
- 1, 0));
2169 ocelot_write_rix(ocelot
, val
, ANA_PGID_PGID
, i
);
2172 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_BLACKHOLE
);
2174 /* Allow broadcast and unknown L2 multicast to the CPU. */
2175 ocelot_rmw_rix(ocelot
, ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2176 ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2177 ANA_PGID_PGID
, PGID_MC
);
2178 ocelot_rmw_rix(ocelot
, ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2179 ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2180 ANA_PGID_PGID
, PGID_BC
);
2181 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV4
);
2182 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV6
);
2184 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2185 * registers endianness.
2187 ocelot_write_rix(ocelot
, QS_INJ_GRP_CFG_BYTE_SWAP
|
2188 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG
, 0);
2189 ocelot_write_rix(ocelot
, QS_XTR_GRP_CFG_BYTE_SWAP
|
2190 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG
, 0);
2191 ocelot_write(ocelot
, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2192 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2193 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2194 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2195 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2196 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2197 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2198 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2199 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG
);
2200 for (i
= 0; i
< 16; i
++)
2201 ocelot_write_rix(ocelot
, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2202 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2203 ANA_CPUQ_8021_CFG
, i
);
2205 INIT_DELAYED_WORK(&ocelot
->stats_work
, ocelot_check_stats_work
);
2206 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
2207 OCELOT_STATS_CHECK_DELAY
);
2211 EXPORT_SYMBOL(ocelot_init
);
2213 void ocelot_deinit(struct ocelot
*ocelot
)
2215 cancel_delayed_work(&ocelot
->stats_work
);
2216 destroy_workqueue(ocelot
->stats_queue
);
2217 destroy_workqueue(ocelot
->owq
);
2218 mutex_destroy(&ocelot
->stats_lock
);
2220 EXPORT_SYMBOL(ocelot_deinit
);
2222 void ocelot_deinit_port(struct ocelot
*ocelot
, int port
)
2224 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
2226 skb_queue_purge(&ocelot_port
->tx_skbs
);
2228 EXPORT_SYMBOL(ocelot_deinit_port
);
2230 MODULE_LICENSE("Dual MIT/GPL");