1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/if_bridge.h>
8 #include <soc/mscc/ocelot_vcap.h>
10 #include "ocelot_vcap.h"
12 #define TABLE_UPDATE_SLEEP_US 10
13 #define TABLE_UPDATE_TIMEOUT_US 100000
15 struct ocelot_mact_entry
{
18 enum macaccess_entry_type type
;
21 static inline u32
ocelot_mact_read_macaccess(struct ocelot
*ocelot
)
23 return ocelot_read(ocelot
, ANA_TABLES_MACACCESS
);
26 static inline int ocelot_mact_wait_for_completion(struct ocelot
*ocelot
)
30 return readx_poll_timeout(ocelot_mact_read_macaccess
,
32 (val
& ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M
) ==
34 TABLE_UPDATE_SLEEP_US
, TABLE_UPDATE_TIMEOUT_US
);
37 static void ocelot_mact_select(struct ocelot
*ocelot
,
38 const unsigned char mac
[ETH_ALEN
],
41 u32 macl
= 0, mach
= 0;
43 /* Set the MAC address to handle and the vlan associated in a format
44 * understood by the hardware.
54 ocelot_write(ocelot
, macl
, ANA_TABLES_MACLDATA
);
55 ocelot_write(ocelot
, mach
, ANA_TABLES_MACHDATA
);
59 int ocelot_mact_learn(struct ocelot
*ocelot
, int port
,
60 const unsigned char mac
[ETH_ALEN
],
61 unsigned int vid
, enum macaccess_entry_type type
)
63 ocelot_mact_select(ocelot
, mac
, vid
);
65 /* Issue a write command */
66 ocelot_write(ocelot
, ANA_TABLES_MACACCESS_VALID
|
67 ANA_TABLES_MACACCESS_DEST_IDX(port
) |
68 ANA_TABLES_MACACCESS_ENTRYTYPE(type
) |
69 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN
),
70 ANA_TABLES_MACACCESS
);
72 return ocelot_mact_wait_for_completion(ocelot
);
74 EXPORT_SYMBOL(ocelot_mact_learn
);
76 int ocelot_mact_forget(struct ocelot
*ocelot
,
77 const unsigned char mac
[ETH_ALEN
], unsigned int vid
)
79 ocelot_mact_select(ocelot
, mac
, vid
);
81 /* Issue a forget command */
83 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET
),
84 ANA_TABLES_MACACCESS
);
86 return ocelot_mact_wait_for_completion(ocelot
);
88 EXPORT_SYMBOL(ocelot_mact_forget
);
90 static void ocelot_mact_init(struct ocelot
*ocelot
)
92 /* Configure the learning mode entries attributes:
93 * - Do not copy the frame to the CPU extraction queues.
94 * - Use the vlan and mac_cpoy for dmac lookup.
97 ANA_AGENCTRL_LEARN_CPU_COPY
| ANA_AGENCTRL_IGNORE_DMAC_FLAGS
98 | ANA_AGENCTRL_LEARN_FWD_KILL
99 | ANA_AGENCTRL_LEARN_IGNORE_VLAN
,
102 /* Clear the MAC table */
103 ocelot_write(ocelot
, MACACCESS_CMD_INIT
, ANA_TABLES_MACACCESS
);
106 static void ocelot_vcap_enable(struct ocelot
*ocelot
, int port
)
108 ocelot_write_gix(ocelot
, ANA_PORT_VCAP_S2_CFG_S2_ENA
|
109 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
110 ANA_PORT_VCAP_S2_CFG
, port
);
112 ocelot_write_gix(ocelot
, ANA_PORT_VCAP_CFG_S1_ENA
,
113 ANA_PORT_VCAP_CFG
, port
);
115 ocelot_rmw_gix(ocelot
, REW_PORT_CFG_ES0_EN
,
120 static inline u32
ocelot_vlant_read_vlanaccess(struct ocelot
*ocelot
)
122 return ocelot_read(ocelot
, ANA_TABLES_VLANACCESS
);
125 static inline int ocelot_vlant_wait_for_completion(struct ocelot
*ocelot
)
129 return readx_poll_timeout(ocelot_vlant_read_vlanaccess
,
132 (val
& ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M
) ==
133 ANA_TABLES_VLANACCESS_CMD_IDLE
,
134 TABLE_UPDATE_SLEEP_US
, TABLE_UPDATE_TIMEOUT_US
);
137 static int ocelot_vlant_set_mask(struct ocelot
*ocelot
, u16 vid
, u32 mask
)
139 /* Select the VID to configure */
140 ocelot_write(ocelot
, ANA_TABLES_VLANTIDX_V_INDEX(vid
),
141 ANA_TABLES_VLANTIDX
);
142 /* Set the vlan port members mask and issue a write command */
143 ocelot_write(ocelot
, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask
) |
144 ANA_TABLES_VLANACCESS_CMD_WRITE
,
145 ANA_TABLES_VLANACCESS
);
147 return ocelot_vlant_wait_for_completion(ocelot
);
150 static void ocelot_port_set_native_vlan(struct ocelot
*ocelot
, int port
,
151 struct ocelot_vlan native_vlan
)
153 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
156 ocelot_port
->native_vlan
= native_vlan
;
158 ocelot_rmw_gix(ocelot
, REW_PORT_VLAN_CFG_PORT_VID(native_vlan
.vid
),
159 REW_PORT_VLAN_CFG_PORT_VID_M
,
160 REW_PORT_VLAN_CFG
, port
);
162 if (ocelot_port
->vlan_aware
) {
163 if (native_vlan
.valid
)
164 /* Tag all frames except when VID == DEFAULT_VLAN */
165 val
= REW_TAG_CFG_TAG_CFG(1);
168 val
= REW_TAG_CFG_TAG_CFG(3);
170 /* Port tagging disabled. */
171 val
= REW_TAG_CFG_TAG_CFG(0);
173 ocelot_rmw_gix(ocelot
, val
,
174 REW_TAG_CFG_TAG_CFG_M
,
178 /* Default vlan to clasify for untagged frames (may be zero) */
179 static void ocelot_port_set_pvid(struct ocelot
*ocelot
, int port
,
180 struct ocelot_vlan pvid_vlan
)
182 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
185 ocelot_port
->pvid_vlan
= pvid_vlan
;
187 if (!ocelot_port
->vlan_aware
)
190 ocelot_rmw_gix(ocelot
,
191 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan
.vid
),
192 ANA_PORT_VLAN_CFG_VLAN_VID_M
,
193 ANA_PORT_VLAN_CFG
, port
);
195 /* If there's no pvid, we should drop not only untagged traffic (which
196 * happens automatically), but also 802.1p traffic which gets
197 * classified to VLAN 0, but that is always in our RX filter, so it
198 * would get accepted were it not for this setting.
200 if (!pvid_vlan
.valid
&& ocelot_port
->vlan_aware
)
201 val
= ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA
|
202 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA
;
204 ocelot_rmw_gix(ocelot
, val
,
205 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA
|
206 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA
,
207 ANA_PORT_DROP_CFG
, port
);
210 int ocelot_port_vlan_filtering(struct ocelot
*ocelot
, int port
,
211 bool vlan_aware
, struct switchdev_trans
*trans
)
213 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
216 if (switchdev_trans_ph_prepare(trans
)) {
217 struct ocelot_vcap_block
*block
= &ocelot
->block
[VCAP_IS1
];
218 struct ocelot_vcap_filter
*filter
;
220 list_for_each_entry(filter
, &block
->rules
, list
) {
221 if (filter
->ingress_port_mask
& BIT(port
) &&
222 filter
->action
.vid_replace_ena
) {
224 "Cannot change VLAN state with vlan modify rules active\n");
232 ocelot_port
->vlan_aware
= vlan_aware
;
235 val
= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
236 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
239 ocelot_rmw_gix(ocelot
, val
,
240 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
241 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M
,
242 ANA_PORT_VLAN_CFG
, port
);
244 ocelot_port_set_pvid(ocelot
, port
, ocelot_port
->pvid_vlan
);
245 ocelot_port_set_native_vlan(ocelot
, port
, ocelot_port
->native_vlan
);
249 EXPORT_SYMBOL(ocelot_port_vlan_filtering
);
251 int ocelot_vlan_prepare(struct ocelot
*ocelot
, int port
, u16 vid
, bool pvid
,
254 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
256 /* Deny changing the native VLAN, but always permit deleting it */
257 if (untagged
&& ocelot_port
->native_vlan
.vid
!= vid
&&
258 ocelot_port
->native_vlan
.valid
) {
260 "Port already has a native VLAN: %d\n",
261 ocelot_port
->native_vlan
.vid
);
267 EXPORT_SYMBOL(ocelot_vlan_prepare
);
269 int ocelot_vlan_add(struct ocelot
*ocelot
, int port
, u16 vid
, bool pvid
,
274 /* Make the port a member of the VLAN */
275 ocelot
->vlan_mask
[vid
] |= BIT(port
);
276 ret
= ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
280 /* Default ingress vlan classification */
282 struct ocelot_vlan pvid_vlan
;
285 pvid_vlan
.valid
= true;
286 ocelot_port_set_pvid(ocelot
, port
, pvid_vlan
);
289 /* Untagged egress vlan clasification */
291 struct ocelot_vlan native_vlan
;
293 native_vlan
.vid
= vid
;
294 native_vlan
.valid
= true;
295 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
300 EXPORT_SYMBOL(ocelot_vlan_add
);
302 int ocelot_vlan_del(struct ocelot
*ocelot
, int port
, u16 vid
)
304 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
307 /* Stop the port from being a member of the vlan */
308 ocelot
->vlan_mask
[vid
] &= ~BIT(port
);
309 ret
= ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
314 if (ocelot_port
->pvid_vlan
.vid
== vid
) {
315 struct ocelot_vlan pvid_vlan
= {0};
317 ocelot_port_set_pvid(ocelot
, port
, pvid_vlan
);
321 if (ocelot_port
->native_vlan
.vid
== vid
) {
322 struct ocelot_vlan native_vlan
= {0};
324 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
329 EXPORT_SYMBOL(ocelot_vlan_del
);
331 static void ocelot_vlan_init(struct ocelot
*ocelot
)
335 /* Clear VLAN table, by default all ports are members of all VLANs */
336 ocelot_write(ocelot
, ANA_TABLES_VLANACCESS_CMD_INIT
,
337 ANA_TABLES_VLANACCESS
);
338 ocelot_vlant_wait_for_completion(ocelot
);
340 /* Configure the port VLAN memberships */
341 for (vid
= 1; vid
< VLAN_N_VID
; vid
++) {
342 ocelot
->vlan_mask
[vid
] = 0;
343 ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
346 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
347 * traffic. It is added automatically if 8021q module is loaded, but
348 * we can't rely on it since module may be not loaded.
350 ocelot
->vlan_mask
[0] = GENMASK(ocelot
->num_phys_ports
- 1, 0);
351 ocelot_vlant_set_mask(ocelot
, 0, ocelot
->vlan_mask
[0]);
353 /* Set vlan ingress filter mask to all ports but the CPU port by
356 ocelot_write(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
359 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
360 ocelot_write_gix(ocelot
, 0, REW_PORT_VLAN_CFG
, port
);
361 ocelot_write_gix(ocelot
, 0, REW_TAG_CFG
, port
);
365 void ocelot_adjust_link(struct ocelot
*ocelot
, int port
,
366 struct phy_device
*phydev
)
368 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
371 switch (phydev
->speed
) {
373 speed
= OCELOT_SPEED_10
;
376 speed
= OCELOT_SPEED_100
;
379 speed
= OCELOT_SPEED_1000
;
380 mode
= DEV_MAC_MODE_CFG_GIGA_MODE_ENA
;
383 speed
= OCELOT_SPEED_2500
;
384 mode
= DEV_MAC_MODE_CFG_GIGA_MODE_ENA
;
387 dev_err(ocelot
->dev
, "Unsupported PHY speed on port %d: %d\n",
388 port
, phydev
->speed
);
392 phy_print_status(phydev
);
397 /* Only full duplex supported for now */
398 ocelot_port_writel(ocelot_port
, DEV_MAC_MODE_CFG_FDX_ENA
|
399 mode
, DEV_MAC_MODE_CFG
);
401 /* Disable HDX fast control */
402 ocelot_port_writel(ocelot_port
, DEV_PORT_MISC_HDX_FAST_DIS
,
405 /* SGMII only for now */
406 ocelot_port_writel(ocelot_port
, PCS1G_MODE_CFG_SGMII_MODE_ENA
,
408 ocelot_port_writel(ocelot_port
, PCS1G_SD_CFG_SD_SEL
, PCS1G_SD_CFG
);
411 ocelot_port_writel(ocelot_port
, PCS1G_CFG_PCS_ENA
, PCS1G_CFG
);
413 /* No aneg on SGMII */
414 ocelot_port_writel(ocelot_port
, 0, PCS1G_ANEG_CFG
);
417 ocelot_port_writel(ocelot_port
, 0, PCS1G_LB_CFG
);
419 /* Enable MAC module */
420 ocelot_port_writel(ocelot_port
, DEV_MAC_ENA_CFG_RX_ENA
|
421 DEV_MAC_ENA_CFG_TX_ENA
, DEV_MAC_ENA_CFG
);
423 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
425 ocelot_port_writel(ocelot_port
, DEV_CLOCK_CFG_LINK_SPEED(speed
),
429 ocelot_write_gix(ocelot
, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed
),
430 ANA_PFC_PFC_CFG
, port
);
432 /* Core: Enable port for frame transfer */
433 ocelot_fields_write(ocelot
, port
,
434 QSYS_SWITCH_PORT_MODE_PORT_ENA
, 1);
437 ocelot_write_rix(ocelot
, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
438 SYS_MAC_FC_CFG_RX_FC_ENA
| SYS_MAC_FC_CFG_TX_FC_ENA
|
439 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA
|
440 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
441 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed
),
442 SYS_MAC_FC_CFG
, port
);
443 ocelot_write_rix(ocelot
, 0, ANA_POL_FLOWC
, port
);
445 EXPORT_SYMBOL(ocelot_adjust_link
);
447 void ocelot_port_enable(struct ocelot
*ocelot
, int port
,
448 struct phy_device
*phy
)
450 /* Enable receiving frames on the port, and activate auto-learning of
453 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_LEARNAUTO
|
454 ANA_PORT_PORT_CFG_RECV_ENA
|
455 ANA_PORT_PORT_CFG_PORTID_VAL(port
),
456 ANA_PORT_PORT_CFG
, port
);
458 EXPORT_SYMBOL(ocelot_port_enable
);
460 void ocelot_port_disable(struct ocelot
*ocelot
, int port
)
462 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
464 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_ENA_CFG
);
465 ocelot_fields_write(ocelot
, port
, QSYS_SWITCH_PORT_MODE_PORT_ENA
, 0);
467 EXPORT_SYMBOL(ocelot_port_disable
);
469 void ocelot_port_add_txtstamp_skb(struct ocelot
*ocelot
, int port
,
470 struct sk_buff
*clone
)
472 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
474 spin_lock(&ocelot_port
->ts_id_lock
);
476 skb_shinfo(clone
)->tx_flags
|= SKBTX_IN_PROGRESS
;
477 /* Store timestamp ID in cb[0] of sk_buff */
478 clone
->cb
[0] = ocelot_port
->ts_id
;
479 ocelot_port
->ts_id
= (ocelot_port
->ts_id
+ 1) % 4;
480 skb_queue_tail(&ocelot_port
->tx_skbs
, clone
);
482 spin_unlock(&ocelot_port
->ts_id_lock
);
484 EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb
);
486 static void ocelot_get_hwtimestamp(struct ocelot
*ocelot
,
487 struct timespec64
*ts
)
492 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
494 /* Read current PTP time to get seconds */
495 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
497 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
498 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE
);
499 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
500 ts
->tv_sec
= ocelot_read_rix(ocelot
, PTP_PIN_TOD_SEC_LSB
, TOD_ACC_PIN
);
502 /* Read packet HW timestamp from FIFO */
503 val
= ocelot_read(ocelot
, SYS_PTP_TXSTAMP
);
504 ts
->tv_nsec
= SYS_PTP_TXSTAMP_PTP_TXSTAMP(val
);
506 /* Sec has incremented since the ts was registered */
507 if ((ts
->tv_sec
& 0x1) != !!(val
& SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC
))
510 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
513 void ocelot_get_txtstamp(struct ocelot
*ocelot
)
515 int budget
= OCELOT_PTP_QUEUE_SZ
;
518 struct sk_buff
*skb
, *skb_tmp
, *skb_match
= NULL
;
519 struct skb_shared_hwtstamps shhwtstamps
;
520 struct ocelot_port
*port
;
521 struct timespec64 ts
;
525 val
= ocelot_read(ocelot
, SYS_PTP_STATUS
);
527 /* Check if a timestamp can be retrieved */
528 if (!(val
& SYS_PTP_STATUS_PTP_MESS_VLD
))
531 WARN_ON(val
& SYS_PTP_STATUS_PTP_OVFL
);
533 /* Retrieve the ts ID and Tx port */
534 id
= SYS_PTP_STATUS_PTP_MESS_ID_X(val
);
535 txport
= SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val
);
537 /* Retrieve its associated skb */
538 port
= ocelot
->ports
[txport
];
540 spin_lock_irqsave(&port
->tx_skbs
.lock
, flags
);
542 skb_queue_walk_safe(&port
->tx_skbs
, skb
, skb_tmp
) {
543 if (skb
->cb
[0] != id
)
545 __skb_unlink(skb
, &port
->tx_skbs
);
550 spin_unlock_irqrestore(&port
->tx_skbs
.lock
, flags
);
552 /* Get the h/w timestamp */
553 ocelot_get_hwtimestamp(ocelot
, &ts
);
555 if (unlikely(!skb_match
))
558 /* Set the timestamp into the skb */
559 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
560 shhwtstamps
.hwtstamp
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
561 skb_complete_tx_timestamp(skb_match
, &shhwtstamps
);
564 ocelot_write(ocelot
, SYS_PTP_NXT_PTP_NXT
, SYS_PTP_NXT
);
567 EXPORT_SYMBOL(ocelot_get_txtstamp
);
569 int ocelot_fdb_add(struct ocelot
*ocelot
, int port
,
570 const unsigned char *addr
, u16 vid
)
574 if (port
== ocelot
->npi
)
577 return ocelot_mact_learn(ocelot
, pgid
, addr
, vid
, ENTRYTYPE_LOCKED
);
579 EXPORT_SYMBOL(ocelot_fdb_add
);
581 int ocelot_fdb_del(struct ocelot
*ocelot
, int port
,
582 const unsigned char *addr
, u16 vid
)
584 return ocelot_mact_forget(ocelot
, addr
, vid
);
586 EXPORT_SYMBOL(ocelot_fdb_del
);
588 int ocelot_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
589 bool is_static
, void *data
)
591 struct ocelot_dump_ctx
*dump
= data
;
592 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
593 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
594 struct nlmsghdr
*nlh
;
597 if (dump
->idx
< dump
->cb
->args
[2])
600 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
601 sizeof(*ndm
), NLM_F_MULTI
);
605 ndm
= nlmsg_data(nlh
);
606 ndm
->ndm_family
= AF_BRIDGE
;
609 ndm
->ndm_flags
= NTF_SELF
;
611 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
612 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
614 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
615 goto nla_put_failure
;
617 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
618 goto nla_put_failure
;
620 nlmsg_end(dump
->skb
, nlh
);
627 nlmsg_cancel(dump
->skb
, nlh
);
630 EXPORT_SYMBOL(ocelot_port_fdb_do_dump
);
632 static int ocelot_mact_read(struct ocelot
*ocelot
, int port
, int row
, int col
,
633 struct ocelot_mact_entry
*entry
)
635 u32 val
, dst
, macl
, mach
;
638 /* Set row and column to read from */
639 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_M_INDEX
, row
);
640 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_BUCKET
, col
);
642 /* Issue a read command */
644 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ
),
645 ANA_TABLES_MACACCESS
);
647 if (ocelot_mact_wait_for_completion(ocelot
))
650 /* Read the entry flags */
651 val
= ocelot_read(ocelot
, ANA_TABLES_MACACCESS
);
652 if (!(val
& ANA_TABLES_MACACCESS_VALID
))
655 /* If the entry read has another port configured as its destination,
658 dst
= (val
& ANA_TABLES_MACACCESS_DEST_IDX_M
) >> 3;
662 /* Get the entry's MAC address and VLAN id */
663 macl
= ocelot_read(ocelot
, ANA_TABLES_MACLDATA
);
664 mach
= ocelot_read(ocelot
, ANA_TABLES_MACHDATA
);
666 mac
[0] = (mach
>> 8) & 0xff;
667 mac
[1] = (mach
>> 0) & 0xff;
668 mac
[2] = (macl
>> 24) & 0xff;
669 mac
[3] = (macl
>> 16) & 0xff;
670 mac
[4] = (macl
>> 8) & 0xff;
671 mac
[5] = (macl
>> 0) & 0xff;
673 entry
->vid
= (mach
>> 16) & 0xfff;
674 ether_addr_copy(entry
->mac
, mac
);
679 int ocelot_fdb_dump(struct ocelot
*ocelot
, int port
,
680 dsa_fdb_dump_cb_t
*cb
, void *data
)
684 /* Loop through all the mac tables entries. */
685 for (i
= 0; i
< ocelot
->num_mact_rows
; i
++) {
686 for (j
= 0; j
< 4; j
++) {
687 struct ocelot_mact_entry entry
;
691 ret
= ocelot_mact_read(ocelot
, port
, i
, j
, &entry
);
692 /* If the entry is invalid (wrong port, invalid...),
700 is_static
= (entry
.type
== ENTRYTYPE_LOCKED
);
702 ret
= cb(entry
.mac
, entry
.vid
, is_static
, data
);
710 EXPORT_SYMBOL(ocelot_fdb_dump
);
712 int ocelot_hwstamp_get(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
714 return copy_to_user(ifr
->ifr_data
, &ocelot
->hwtstamp_config
,
715 sizeof(ocelot
->hwtstamp_config
)) ? -EFAULT
: 0;
717 EXPORT_SYMBOL(ocelot_hwstamp_get
);
719 int ocelot_hwstamp_set(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
721 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
722 struct hwtstamp_config cfg
;
724 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
727 /* reserved for future extensions */
731 /* Tx type sanity check */
732 switch (cfg
.tx_type
) {
734 ocelot_port
->ptp_cmd
= IFH_REW_OP_TWO_STEP_PTP
;
736 case HWTSTAMP_TX_ONESTEP_SYNC
:
737 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
738 * need to update the origin time.
740 ocelot_port
->ptp_cmd
= IFH_REW_OP_ORIGIN_PTP
;
742 case HWTSTAMP_TX_OFF
:
743 ocelot_port
->ptp_cmd
= 0;
749 mutex_lock(&ocelot
->ptp_lock
);
751 switch (cfg
.rx_filter
) {
752 case HWTSTAMP_FILTER_NONE
:
754 case HWTSTAMP_FILTER_ALL
:
755 case HWTSTAMP_FILTER_SOME
:
756 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
757 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
758 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
759 case HWTSTAMP_FILTER_NTP_ALL
:
760 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
761 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
762 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
763 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
764 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
765 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
766 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
767 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
768 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
769 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
772 mutex_unlock(&ocelot
->ptp_lock
);
776 /* Commit back the result & save it */
777 memcpy(&ocelot
->hwtstamp_config
, &cfg
, sizeof(cfg
));
778 mutex_unlock(&ocelot
->ptp_lock
);
780 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
782 EXPORT_SYMBOL(ocelot_hwstamp_set
);
784 void ocelot_get_strings(struct ocelot
*ocelot
, int port
, u32 sset
, u8
*data
)
788 if (sset
!= ETH_SS_STATS
)
791 for (i
= 0; i
< ocelot
->num_stats
; i
++)
792 memcpy(data
+ i
* ETH_GSTRING_LEN
, ocelot
->stats_layout
[i
].name
,
795 EXPORT_SYMBOL(ocelot_get_strings
);
797 static void ocelot_update_stats(struct ocelot
*ocelot
)
801 mutex_lock(&ocelot
->stats_lock
);
803 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
804 /* Configure the port to read the stats from */
805 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(i
), SYS_STAT_CFG
);
807 for (j
= 0; j
< ocelot
->num_stats
; j
++) {
809 unsigned int idx
= i
* ocelot
->num_stats
+ j
;
811 val
= ocelot_read_rix(ocelot
, SYS_COUNT_RX_OCTETS
,
812 ocelot
->stats_layout
[j
].offset
);
814 if (val
< (ocelot
->stats
[idx
] & U32_MAX
))
815 ocelot
->stats
[idx
] += (u64
)1 << 32;
817 ocelot
->stats
[idx
] = (ocelot
->stats
[idx
] &
818 ~(u64
)U32_MAX
) + val
;
822 mutex_unlock(&ocelot
->stats_lock
);
825 static void ocelot_check_stats_work(struct work_struct
*work
)
827 struct delayed_work
*del_work
= to_delayed_work(work
);
828 struct ocelot
*ocelot
= container_of(del_work
, struct ocelot
,
831 ocelot_update_stats(ocelot
);
833 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
834 OCELOT_STATS_CHECK_DELAY
);
837 void ocelot_get_ethtool_stats(struct ocelot
*ocelot
, int port
, u64
*data
)
841 /* check and update now */
842 ocelot_update_stats(ocelot
);
844 /* Copy all counters */
845 for (i
= 0; i
< ocelot
->num_stats
; i
++)
846 *data
++ = ocelot
->stats
[port
* ocelot
->num_stats
+ i
];
848 EXPORT_SYMBOL(ocelot_get_ethtool_stats
);
850 int ocelot_get_sset_count(struct ocelot
*ocelot
, int port
, int sset
)
852 if (sset
!= ETH_SS_STATS
)
855 return ocelot
->num_stats
;
857 EXPORT_SYMBOL(ocelot_get_sset_count
);
859 int ocelot_get_ts_info(struct ocelot
*ocelot
, int port
,
860 struct ethtool_ts_info
*info
)
862 info
->phc_index
= ocelot
->ptp_clock
?
863 ptp_clock_index(ocelot
->ptp_clock
) : -1;
864 if (info
->phc_index
== -1) {
865 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
866 SOF_TIMESTAMPING_RX_SOFTWARE
|
867 SOF_TIMESTAMPING_SOFTWARE
;
870 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
871 SOF_TIMESTAMPING_RX_SOFTWARE
|
872 SOF_TIMESTAMPING_SOFTWARE
|
873 SOF_TIMESTAMPING_TX_HARDWARE
|
874 SOF_TIMESTAMPING_RX_HARDWARE
|
875 SOF_TIMESTAMPING_RAW_HARDWARE
;
876 info
->tx_types
= BIT(HWTSTAMP_TX_OFF
) | BIT(HWTSTAMP_TX_ON
) |
877 BIT(HWTSTAMP_TX_ONESTEP_SYNC
);
878 info
->rx_filters
= BIT(HWTSTAMP_FILTER_NONE
) | BIT(HWTSTAMP_FILTER_ALL
);
882 EXPORT_SYMBOL(ocelot_get_ts_info
);
884 void ocelot_bridge_stp_state_set(struct ocelot
*ocelot
, int port
, u8 state
)
889 if (!(BIT(port
) & ocelot
->bridge_mask
))
892 port_cfg
= ocelot_read_gix(ocelot
, ANA_PORT_PORT_CFG
, port
);
895 case BR_STATE_FORWARDING
:
896 ocelot
->bridge_fwd_mask
|= BIT(port
);
898 case BR_STATE_LEARNING
:
899 port_cfg
|= ANA_PORT_PORT_CFG_LEARN_ENA
;
903 port_cfg
&= ~ANA_PORT_PORT_CFG_LEARN_ENA
;
904 ocelot
->bridge_fwd_mask
&= ~BIT(port
);
908 ocelot_write_gix(ocelot
, port_cfg
, ANA_PORT_PORT_CFG
, port
);
910 /* Apply FWD mask. The loop is needed to add/remove the current port as
911 * a source for the other ports.
913 for (p
= 0; p
< ocelot
->num_phys_ports
; p
++) {
914 if (ocelot
->bridge_fwd_mask
& BIT(p
)) {
915 unsigned long mask
= ocelot
->bridge_fwd_mask
& ~BIT(p
);
917 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
918 unsigned long bond_mask
= ocelot
->lags
[i
];
923 if (bond_mask
& BIT(p
)) {
929 ocelot_write_rix(ocelot
, mask
,
930 ANA_PGID_PGID
, PGID_SRC
+ p
);
932 ocelot_write_rix(ocelot
, 0,
933 ANA_PGID_PGID
, PGID_SRC
+ p
);
937 EXPORT_SYMBOL(ocelot_bridge_stp_state_set
);
939 void ocelot_set_ageing_time(struct ocelot
*ocelot
, unsigned int msecs
)
941 unsigned int age_period
= ANA_AUTOAGE_AGE_PERIOD(msecs
/ 2000);
943 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
944 * which is clearly not what our intention is. So avoid that.
949 ocelot_rmw(ocelot
, age_period
, ANA_AUTOAGE_AGE_PERIOD_M
, ANA_AUTOAGE
);
951 EXPORT_SYMBOL(ocelot_set_ageing_time
);
953 static struct ocelot_multicast
*ocelot_multicast_get(struct ocelot
*ocelot
,
954 const unsigned char *addr
,
957 struct ocelot_multicast
*mc
;
959 list_for_each_entry(mc
, &ocelot
->multicast
, list
) {
960 if (ether_addr_equal(mc
->addr
, addr
) && mc
->vid
== vid
)
967 static enum macaccess_entry_type
ocelot_classify_mdb(const unsigned char *addr
)
969 if (addr
[0] == 0x01 && addr
[1] == 0x00 && addr
[2] == 0x5e)
970 return ENTRYTYPE_MACv4
;
971 if (addr
[0] == 0x33 && addr
[1] == 0x33)
972 return ENTRYTYPE_MACv6
;
973 return ENTRYTYPE_LOCKED
;
976 static struct ocelot_pgid
*ocelot_pgid_alloc(struct ocelot
*ocelot
, int index
,
979 struct ocelot_pgid
*pgid
;
981 pgid
= kzalloc(sizeof(*pgid
), GFP_KERNEL
);
983 return ERR_PTR(-ENOMEM
);
987 refcount_set(&pgid
->refcount
, 1);
988 list_add_tail(&pgid
->list
, &ocelot
->pgids
);
993 static void ocelot_pgid_free(struct ocelot
*ocelot
, struct ocelot_pgid
*pgid
)
995 if (!refcount_dec_and_test(&pgid
->refcount
))
998 list_del(&pgid
->list
);
1002 static struct ocelot_pgid
*ocelot_mdb_get_pgid(struct ocelot
*ocelot
,
1003 const struct ocelot_multicast
*mc
)
1005 struct ocelot_pgid
*pgid
;
1008 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1009 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1010 * destination mask table (PGID), the destination set is programmed as
1011 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1013 if (mc
->entry_type
== ENTRYTYPE_MACv4
||
1014 mc
->entry_type
== ENTRYTYPE_MACv6
)
1015 return ocelot_pgid_alloc(ocelot
, 0, mc
->ports
);
1017 list_for_each_entry(pgid
, &ocelot
->pgids
, list
) {
1018 /* When searching for a nonreserved multicast PGID, ignore the
1019 * dummy PGID of zero that we have for MACv4/MACv6 entries
1021 if (pgid
->index
&& pgid
->ports
== mc
->ports
) {
1022 refcount_inc(&pgid
->refcount
);
1027 /* Search for a free index in the nonreserved multicast PGID area */
1028 for_each_nonreserved_multicast_dest_pgid(ocelot
, index
) {
1031 list_for_each_entry(pgid
, &ocelot
->pgids
, list
) {
1032 if (pgid
->index
== index
) {
1039 return ocelot_pgid_alloc(ocelot
, index
, mc
->ports
);
1042 return ERR_PTR(-ENOSPC
);
1045 static void ocelot_encode_ports_to_mdb(unsigned char *addr
,
1046 struct ocelot_multicast
*mc
)
1048 ether_addr_copy(addr
, mc
->addr
);
1050 if (mc
->entry_type
== ENTRYTYPE_MACv4
) {
1052 addr
[1] = mc
->ports
>> 8;
1053 addr
[2] = mc
->ports
& 0xff;
1054 } else if (mc
->entry_type
== ENTRYTYPE_MACv6
) {
1055 addr
[0] = mc
->ports
>> 8;
1056 addr
[1] = mc
->ports
& 0xff;
1060 int ocelot_port_mdb_add(struct ocelot
*ocelot
, int port
,
1061 const struct switchdev_obj_port_mdb
*mdb
)
1063 unsigned char addr
[ETH_ALEN
];
1064 struct ocelot_multicast
*mc
;
1065 struct ocelot_pgid
*pgid
;
1068 if (port
== ocelot
->npi
)
1069 port
= ocelot
->num_phys_ports
;
1071 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1074 mc
= devm_kzalloc(ocelot
->dev
, sizeof(*mc
), GFP_KERNEL
);
1078 mc
->entry_type
= ocelot_classify_mdb(mdb
->addr
);
1079 ether_addr_copy(mc
->addr
, mdb
->addr
);
1082 list_add_tail(&mc
->list
, &ocelot
->multicast
);
1084 /* Existing entry. Clean up the current port mask from
1085 * hardware now, because we'll be modifying it.
1087 ocelot_pgid_free(ocelot
, mc
->pgid
);
1088 ocelot_encode_ports_to_mdb(addr
, mc
);
1089 ocelot_mact_forget(ocelot
, addr
, vid
);
1092 mc
->ports
|= BIT(port
);
1094 pgid
= ocelot_mdb_get_pgid(ocelot
, mc
);
1096 dev_err(ocelot
->dev
,
1097 "Cannot allocate PGID for mdb %pM vid %d\n",
1099 devm_kfree(ocelot
->dev
, mc
);
1100 return PTR_ERR(pgid
);
1104 ocelot_encode_ports_to_mdb(addr
, mc
);
1106 if (mc
->entry_type
!= ENTRYTYPE_MACv4
&&
1107 mc
->entry_type
!= ENTRYTYPE_MACv6
)
1108 ocelot_write_rix(ocelot
, pgid
->ports
, ANA_PGID_PGID
,
1111 return ocelot_mact_learn(ocelot
, pgid
->index
, addr
, vid
,
1114 EXPORT_SYMBOL(ocelot_port_mdb_add
);
1116 int ocelot_port_mdb_del(struct ocelot
*ocelot
, int port
,
1117 const struct switchdev_obj_port_mdb
*mdb
)
1119 unsigned char addr
[ETH_ALEN
];
1120 struct ocelot_multicast
*mc
;
1121 struct ocelot_pgid
*pgid
;
1124 if (port
== ocelot
->npi
)
1125 port
= ocelot
->num_phys_ports
;
1127 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1131 ocelot_encode_ports_to_mdb(addr
, mc
);
1132 ocelot_mact_forget(ocelot
, addr
, vid
);
1134 ocelot_pgid_free(ocelot
, mc
->pgid
);
1135 mc
->ports
&= ~BIT(port
);
1137 list_del(&mc
->list
);
1138 devm_kfree(ocelot
->dev
, mc
);
1142 /* We have a PGID with fewer ports now */
1143 pgid
= ocelot_mdb_get_pgid(ocelot
, mc
);
1145 return PTR_ERR(pgid
);
1148 ocelot_encode_ports_to_mdb(addr
, mc
);
1150 if (mc
->entry_type
!= ENTRYTYPE_MACv4
&&
1151 mc
->entry_type
!= ENTRYTYPE_MACv6
)
1152 ocelot_write_rix(ocelot
, pgid
->ports
, ANA_PGID_PGID
,
1155 return ocelot_mact_learn(ocelot
, pgid
->index
, addr
, vid
,
1158 EXPORT_SYMBOL(ocelot_port_mdb_del
);
1160 int ocelot_port_bridge_join(struct ocelot
*ocelot
, int port
,
1161 struct net_device
*bridge
)
1163 if (!ocelot
->bridge_mask
) {
1164 ocelot
->hw_bridge_dev
= bridge
;
1166 if (ocelot
->hw_bridge_dev
!= bridge
)
1167 /* This is adding the port to a second bridge, this is
1172 ocelot
->bridge_mask
|= BIT(port
);
1176 EXPORT_SYMBOL(ocelot_port_bridge_join
);
1178 int ocelot_port_bridge_leave(struct ocelot
*ocelot
, int port
,
1179 struct net_device
*bridge
)
1181 struct ocelot_vlan pvid
= {0}, native_vlan
= {0};
1182 struct switchdev_trans trans
;
1185 ocelot
->bridge_mask
&= ~BIT(port
);
1187 if (!ocelot
->bridge_mask
)
1188 ocelot
->hw_bridge_dev
= NULL
;
1190 trans
.ph_prepare
= true;
1191 ret
= ocelot_port_vlan_filtering(ocelot
, port
, false, &trans
);
1195 trans
.ph_prepare
= false;
1196 ret
= ocelot_port_vlan_filtering(ocelot
, port
, false, &trans
);
1200 ocelot_port_set_pvid(ocelot
, port
, pvid
);
1201 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
1205 EXPORT_SYMBOL(ocelot_port_bridge_leave
);
1207 static void ocelot_set_aggr_pgids(struct ocelot
*ocelot
)
1211 /* Reset destination and aggregation PGIDS */
1212 for_each_unicast_dest_pgid(ocelot
, port
)
1213 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
1215 for_each_aggr_pgid(ocelot
, i
)
1216 ocelot_write_rix(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
1219 /* Now, set PGIDs for each LAG */
1220 for (lag
= 0; lag
< ocelot
->num_phys_ports
; lag
++) {
1221 unsigned long bond_mask
;
1225 bond_mask
= ocelot
->lags
[lag
];
1229 for_each_set_bit(port
, &bond_mask
, ocelot
->num_phys_ports
) {
1231 ocelot_write_rix(ocelot
, bond_mask
,
1232 ANA_PGID_PGID
, port
);
1233 aggr_idx
[aggr_count
] = port
;
1237 for_each_aggr_pgid(ocelot
, i
) {
1240 ac
= ocelot_read_rix(ocelot
, ANA_PGID_PGID
, i
);
1242 ac
|= BIT(aggr_idx
[i
% aggr_count
]);
1243 ocelot_write_rix(ocelot
, ac
, ANA_PGID_PGID
, i
);
1248 static void ocelot_setup_lag(struct ocelot
*ocelot
, int lag
)
1250 unsigned long bond_mask
= ocelot
->lags
[lag
];
1253 for_each_set_bit(p
, &bond_mask
, ocelot
->num_phys_ports
) {
1254 u32 port_cfg
= ocelot_read_gix(ocelot
, ANA_PORT_PORT_CFG
, p
);
1256 port_cfg
&= ~ANA_PORT_PORT_CFG_PORTID_VAL_M
;
1258 /* Use lag port as logical port for port i */
1259 ocelot_write_gix(ocelot
, port_cfg
|
1260 ANA_PORT_PORT_CFG_PORTID_VAL(lag
),
1261 ANA_PORT_PORT_CFG
, p
);
1265 int ocelot_port_lag_join(struct ocelot
*ocelot
, int port
,
1266 struct net_device
*bond
)
1268 struct net_device
*ndev
;
1273 for_each_netdev_in_bond_rcu(bond
, ndev
) {
1274 struct ocelot_port_private
*priv
= netdev_priv(ndev
);
1276 bond_mask
|= BIT(priv
->chip_port
);
1280 lp
= __ffs(bond_mask
);
1282 /* If the new port is the lowest one, use it as the logical port from
1287 ocelot
->lags
[port
] = bond_mask
;
1288 bond_mask
&= ~BIT(port
);
1290 lp
= __ffs(bond_mask
);
1291 ocelot
->lags
[lp
] = 0;
1295 ocelot
->lags
[lp
] |= BIT(port
);
1298 ocelot_setup_lag(ocelot
, lag
);
1299 ocelot_set_aggr_pgids(ocelot
);
1303 EXPORT_SYMBOL(ocelot_port_lag_join
);
1305 void ocelot_port_lag_leave(struct ocelot
*ocelot
, int port
,
1306 struct net_device
*bond
)
1311 /* Remove port from any lag */
1312 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++)
1313 ocelot
->lags
[i
] &= ~BIT(port
);
1315 /* if it was the logical port of the lag, move the lag config to the
1318 if (ocelot
->lags
[port
]) {
1319 int n
= __ffs(ocelot
->lags
[port
]);
1321 ocelot
->lags
[n
] = ocelot
->lags
[port
];
1322 ocelot
->lags
[port
] = 0;
1324 ocelot_setup_lag(ocelot
, n
);
1327 port_cfg
= ocelot_read_gix(ocelot
, ANA_PORT_PORT_CFG
, port
);
1328 port_cfg
&= ~ANA_PORT_PORT_CFG_PORTID_VAL_M
;
1329 ocelot_write_gix(ocelot
, port_cfg
| ANA_PORT_PORT_CFG_PORTID_VAL(port
),
1330 ANA_PORT_PORT_CFG
, port
);
1332 ocelot_set_aggr_pgids(ocelot
);
1334 EXPORT_SYMBOL(ocelot_port_lag_leave
);
1336 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1337 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1338 * In the special case that it's the NPI port that we're configuring, the
1339 * length of the tag and optional prefix needs to be accounted for privately,
1340 * in order to be able to sustain communication at the requested @sdu.
1342 void ocelot_port_set_maxlen(struct ocelot
*ocelot
, int port
, size_t sdu
)
1344 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1345 int maxlen
= sdu
+ ETH_HLEN
+ ETH_FCS_LEN
;
1346 int pause_start
, pause_stop
;
1349 if (port
== ocelot
->npi
) {
1350 maxlen
+= OCELOT_TAG_LEN
;
1352 if (ocelot
->inj_prefix
== OCELOT_TAG_PREFIX_SHORT
)
1353 maxlen
+= OCELOT_SHORT_PREFIX_LEN
;
1354 else if (ocelot
->inj_prefix
== OCELOT_TAG_PREFIX_LONG
)
1355 maxlen
+= OCELOT_LONG_PREFIX_LEN
;
1358 ocelot_port_writel(ocelot_port
, maxlen
, DEV_MAC_MAXLEN_CFG
);
1360 /* Set Pause watermark hysteresis */
1361 pause_start
= 6 * maxlen
/ OCELOT_BUFFER_CELL_SZ
;
1362 pause_stop
= 4 * maxlen
/ OCELOT_BUFFER_CELL_SZ
;
1363 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_START
,
1365 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_STOP
,
1368 /* Tail dropping watermarks */
1369 atop_tot
= (ocelot
->shared_queue_sz
- 9 * maxlen
) /
1370 OCELOT_BUFFER_CELL_SZ
;
1371 atop
= (9 * maxlen
) / OCELOT_BUFFER_CELL_SZ
;
1372 ocelot_write_rix(ocelot
, ocelot
->ops
->wm_enc(atop
), SYS_ATOP
, port
);
1373 ocelot_write(ocelot
, ocelot
->ops
->wm_enc(atop_tot
), SYS_ATOP_TOT_CFG
);
1375 EXPORT_SYMBOL(ocelot_port_set_maxlen
);
1377 int ocelot_get_max_mtu(struct ocelot
*ocelot
, int port
)
1379 int max_mtu
= 65535 - ETH_HLEN
- ETH_FCS_LEN
;
1381 if (port
== ocelot
->npi
) {
1382 max_mtu
-= OCELOT_TAG_LEN
;
1384 if (ocelot
->inj_prefix
== OCELOT_TAG_PREFIX_SHORT
)
1385 max_mtu
-= OCELOT_SHORT_PREFIX_LEN
;
1386 else if (ocelot
->inj_prefix
== OCELOT_TAG_PREFIX_LONG
)
1387 max_mtu
-= OCELOT_LONG_PREFIX_LEN
;
1392 EXPORT_SYMBOL(ocelot_get_max_mtu
);
1394 void ocelot_init_port(struct ocelot
*ocelot
, int port
)
1396 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1398 skb_queue_head_init(&ocelot_port
->tx_skbs
);
1399 spin_lock_init(&ocelot_port
->ts_id_lock
);
1401 /* Basic L2 initialization */
1404 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1405 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1407 ocelot_port_writel(ocelot_port
, DEV_MAC_IFG_CFG_TX_IFG(5),
1410 /* Load seed (0) and set MAC HDX late collision */
1411 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1412 DEV_MAC_HDX_CFG_SEED_LOAD
,
1415 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1418 /* Set Max Length and maximum tags allowed */
1419 ocelot_port_set_maxlen(ocelot
, port
, ETH_DATA_LEN
);
1420 ocelot_port_writel(ocelot_port
, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD
) |
1421 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA
|
1422 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA
|
1423 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA
,
1426 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1427 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_HIGH_CFG
);
1428 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_LOW_CFG
);
1430 /* Enable transmission of pause frames */
1431 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_ENA
, 1);
1433 /* Drop frames with multicast source address */
1434 ocelot_rmw_gix(ocelot
, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
1435 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
1436 ANA_PORT_DROP_CFG
, port
);
1438 /* Set default VLAN and tag type to 8021Q. */
1439 ocelot_rmw_gix(ocelot
, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q
),
1440 REW_PORT_VLAN_CFG_PORT_TPID_M
,
1441 REW_PORT_VLAN_CFG
, port
);
1443 /* Enable vcap lookups */
1444 ocelot_vcap_enable(ocelot
, port
);
1446 EXPORT_SYMBOL(ocelot_init_port
);
1448 /* Configure and enable the CPU port module, which is a set of queues
1449 * accessible through register MMIO, frame DMA or Ethernet (in case
1450 * NPI mode is used).
1452 static void ocelot_cpu_port_init(struct ocelot
*ocelot
)
1454 int cpu
= ocelot
->num_phys_ports
;
1456 /* The unicast destination PGID for the CPU port module is unused */
1457 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, cpu
);
1458 /* Instead set up a multicast destination PGID for traffic copied to
1459 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1460 * addresses will be copied to the CPU via this PGID.
1462 ocelot_write_rix(ocelot
, BIT(cpu
), ANA_PGID_PGID
, PGID_CPU
);
1463 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_RECV_ENA
|
1464 ANA_PORT_PORT_CFG_PORTID_VAL(cpu
),
1465 ANA_PORT_PORT_CFG
, cpu
);
1467 /* Enable CPU port module */
1468 ocelot_fields_write(ocelot
, cpu
, QSYS_SWITCH_PORT_MODE_PORT_ENA
, 1);
1469 /* CPU port Injection/Extraction configuration */
1470 ocelot_fields_write(ocelot
, cpu
, SYS_PORT_MODE_INCL_XTR_HDR
,
1471 ocelot
->xtr_prefix
);
1472 ocelot_fields_write(ocelot
, cpu
, SYS_PORT_MODE_INCL_INJ_HDR
,
1473 ocelot
->inj_prefix
);
1475 /* Configure the CPU port to be VLAN aware */
1476 ocelot_write_gix(ocelot
, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1477 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
1478 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1479 ANA_PORT_VLAN_CFG
, cpu
);
1482 int ocelot_init(struct ocelot
*ocelot
)
1484 char queue_name
[32];
1488 if (ocelot
->ops
->reset
) {
1489 ret
= ocelot
->ops
->reset(ocelot
);
1491 dev_err(ocelot
->dev
, "Switch reset failed\n");
1496 ocelot
->lags
= devm_kcalloc(ocelot
->dev
, ocelot
->num_phys_ports
,
1497 sizeof(u32
), GFP_KERNEL
);
1501 ocelot
->stats
= devm_kcalloc(ocelot
->dev
,
1502 ocelot
->num_phys_ports
* ocelot
->num_stats
,
1503 sizeof(u64
), GFP_KERNEL
);
1507 mutex_init(&ocelot
->stats_lock
);
1508 mutex_init(&ocelot
->ptp_lock
);
1509 spin_lock_init(&ocelot
->ptp_clock_lock
);
1510 snprintf(queue_name
, sizeof(queue_name
), "%s-stats",
1511 dev_name(ocelot
->dev
));
1512 ocelot
->stats_queue
= create_singlethread_workqueue(queue_name
);
1513 if (!ocelot
->stats_queue
)
1516 ocelot
->owq
= alloc_ordered_workqueue("ocelot-owq", 0);
1518 destroy_workqueue(ocelot
->stats_queue
);
1522 INIT_LIST_HEAD(&ocelot
->multicast
);
1523 INIT_LIST_HEAD(&ocelot
->pgids
);
1524 ocelot_mact_init(ocelot
);
1525 ocelot_vlan_init(ocelot
);
1526 ocelot_vcap_init(ocelot
);
1527 ocelot_cpu_port_init(ocelot
);
1529 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1530 /* Clear all counters (5 groups) */
1531 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(port
) |
1532 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1536 /* Only use S-Tag */
1537 ocelot_write(ocelot
, ETH_P_8021AD
, SYS_VLAN_ETYPE_CFG
);
1539 /* Aggregation mode */
1540 ocelot_write(ocelot
, ANA_AGGR_CFG_AC_SMAC_ENA
|
1541 ANA_AGGR_CFG_AC_DMAC_ENA
|
1542 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA
|
1543 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA
, ANA_AGGR_CFG
);
1545 /* Set MAC age time to default value. The entry is aged after
1548 ocelot_write(ocelot
,
1549 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME
/ 2 / HZ
),
1552 /* Disable learning for frames discarded by VLAN ingress filtering */
1553 regmap_field_write(ocelot
->regfields
[ANA_ADVLEARN_VLAN_CHK
], 1);
1555 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1556 ocelot_write(ocelot
, SYS_FRM_AGING_AGE_TX_ENA
|
1557 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING
);
1559 /* Setup flooding PGIDs */
1560 for (i
= 0; i
< ocelot
->num_flooding_pgids
; i
++)
1561 ocelot_write_rix(ocelot
, ANA_FLOODING_FLD_MULTICAST(PGID_MC
) |
1562 ANA_FLOODING_FLD_BROADCAST(PGID_MC
) |
1563 ANA_FLOODING_FLD_UNICAST(PGID_UC
),
1565 ocelot_write(ocelot
, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6
) |
1566 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC
) |
1567 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4
) |
1568 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC
),
1571 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1572 /* Transmit the frame to the local port. */
1573 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
1574 /* Do not forward BPDU frames to the front ports. */
1575 ocelot_write_gix(ocelot
,
1576 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1577 ANA_PORT_CPU_FWD_BPDU_CFG
,
1579 /* Ensure bridging is disabled */
1580 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_SRC
+ port
);
1583 /* Allow broadcast MAC frames. */
1584 for_each_nonreserved_multicast_dest_pgid(ocelot
, i
) {
1585 u32 val
= ANA_PGID_PGID_PGID(GENMASK(ocelot
->num_phys_ports
- 1, 0));
1587 ocelot_write_rix(ocelot
, val
, ANA_PGID_PGID
, i
);
1589 ocelot_write_rix(ocelot
,
1590 ANA_PGID_PGID_PGID(GENMASK(ocelot
->num_phys_ports
, 0)),
1591 ANA_PGID_PGID
, PGID_MC
);
1592 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV4
);
1593 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV6
);
1595 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1596 * registers endianness.
1598 ocelot_write_rix(ocelot
, QS_INJ_GRP_CFG_BYTE_SWAP
|
1599 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG
, 0);
1600 ocelot_write_rix(ocelot
, QS_XTR_GRP_CFG_BYTE_SWAP
|
1601 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG
, 0);
1602 ocelot_write(ocelot
, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1603 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1604 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1605 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1606 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1607 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1608 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1609 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1610 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG
);
1611 for (i
= 0; i
< 16; i
++)
1612 ocelot_write_rix(ocelot
, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1613 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1614 ANA_CPUQ_8021_CFG
, i
);
1616 INIT_DELAYED_WORK(&ocelot
->stats_work
, ocelot_check_stats_work
);
1617 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
1618 OCELOT_STATS_CHECK_DELAY
);
1622 EXPORT_SYMBOL(ocelot_init
);
1624 void ocelot_deinit(struct ocelot
*ocelot
)
1626 cancel_delayed_work(&ocelot
->stats_work
);
1627 destroy_workqueue(ocelot
->stats_queue
);
1628 destroy_workqueue(ocelot
->owq
);
1629 mutex_destroy(&ocelot
->stats_lock
);
1631 EXPORT_SYMBOL(ocelot_deinit
);
1633 void ocelot_deinit_port(struct ocelot
*ocelot
, int port
)
1635 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1637 skb_queue_purge(&ocelot_port
->tx_skbs
);
1639 EXPORT_SYMBOL(ocelot_deinit_port
);
1641 MODULE_LICENSE("Dual MIT/GPL");