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 int ocelot_port_vlan_filtering(struct ocelot
*ocelot
, int port
,
228 struct ocelot_vcap_block
*block
= &ocelot
->block
[VCAP_IS1
];
229 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
230 struct ocelot_vcap_filter
*filter
;
233 list_for_each_entry(filter
, &block
->rules
, list
) {
234 if (filter
->ingress_port_mask
& BIT(port
) &&
235 filter
->action
.vid_replace_ena
) {
237 "Cannot change VLAN state with vlan modify rules active\n");
242 ocelot_port
->vlan_aware
= vlan_aware
;
245 val
= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
246 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
249 ocelot_rmw_gix(ocelot
, val
,
250 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
251 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M
,
252 ANA_PORT_VLAN_CFG
, port
);
254 ocelot_port_set_pvid(ocelot
, port
, ocelot_port
->pvid_vlan
);
255 ocelot_port_set_native_vlan(ocelot
, port
, ocelot_port
->native_vlan
);
259 EXPORT_SYMBOL(ocelot_port_vlan_filtering
);
261 int ocelot_vlan_prepare(struct ocelot
*ocelot
, int port
, u16 vid
, bool pvid
,
264 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
266 /* Deny changing the native VLAN, but always permit deleting it */
267 if (untagged
&& ocelot_port
->native_vlan
.vid
!= vid
&&
268 ocelot_port
->native_vlan
.valid
) {
270 "Port already has a native VLAN: %d\n",
271 ocelot_port
->native_vlan
.vid
);
277 EXPORT_SYMBOL(ocelot_vlan_prepare
);
279 int ocelot_vlan_add(struct ocelot
*ocelot
, int port
, u16 vid
, bool pvid
,
284 /* Make the port a member of the VLAN */
285 ocelot
->vlan_mask
[vid
] |= BIT(port
);
286 ret
= ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
290 /* Default ingress vlan classification */
292 struct ocelot_vlan pvid_vlan
;
295 pvid_vlan
.valid
= true;
296 ocelot_port_set_pvid(ocelot
, port
, pvid_vlan
);
299 /* Untagged egress vlan clasification */
301 struct ocelot_vlan native_vlan
;
303 native_vlan
.vid
= vid
;
304 native_vlan
.valid
= true;
305 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
310 EXPORT_SYMBOL(ocelot_vlan_add
);
312 int ocelot_vlan_del(struct ocelot
*ocelot
, int port
, u16 vid
)
314 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
317 /* Stop the port from being a member of the vlan */
318 ocelot
->vlan_mask
[vid
] &= ~BIT(port
);
319 ret
= ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
324 if (ocelot_port
->pvid_vlan
.vid
== vid
) {
325 struct ocelot_vlan pvid_vlan
= {0};
327 ocelot_port_set_pvid(ocelot
, port
, pvid_vlan
);
331 if (ocelot_port
->native_vlan
.vid
== vid
) {
332 struct ocelot_vlan native_vlan
= {0};
334 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
339 EXPORT_SYMBOL(ocelot_vlan_del
);
341 static void ocelot_vlan_init(struct ocelot
*ocelot
)
345 /* Clear VLAN table, by default all ports are members of all VLANs */
346 ocelot_write(ocelot
, ANA_TABLES_VLANACCESS_CMD_INIT
,
347 ANA_TABLES_VLANACCESS
);
348 ocelot_vlant_wait_for_completion(ocelot
);
350 /* Configure the port VLAN memberships */
351 for (vid
= 1; vid
< VLAN_N_VID
; vid
++) {
352 ocelot
->vlan_mask
[vid
] = 0;
353 ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
356 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
357 * traffic. It is added automatically if 8021q module is loaded, but
358 * we can't rely on it since module may be not loaded.
360 ocelot
->vlan_mask
[0] = GENMASK(ocelot
->num_phys_ports
- 1, 0);
361 ocelot_vlant_set_mask(ocelot
, 0, ocelot
->vlan_mask
[0]);
363 /* Set vlan ingress filter mask to all ports but the CPU port by
366 ocelot_write(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
369 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
370 ocelot_write_gix(ocelot
, 0, REW_PORT_VLAN_CFG
, port
);
371 ocelot_write_gix(ocelot
, 0, REW_TAG_CFG
, port
);
375 static u32
ocelot_read_eq_avail(struct ocelot
*ocelot
, int port
)
377 return ocelot_read_rix(ocelot
, QSYS_SW_STATUS
, port
);
380 int ocelot_port_flush(struct ocelot
*ocelot
, int port
)
382 unsigned int pause_ena
;
385 /* Disable dequeuing from the egress queues */
386 ocelot_rmw_rix(ocelot
, QSYS_PORT_MODE_DEQUEUE_DIS
,
387 QSYS_PORT_MODE_DEQUEUE_DIS
,
388 QSYS_PORT_MODE
, port
);
390 /* Disable flow control */
391 ocelot_fields_read(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_ENA
, &pause_ena
);
392 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_ENA
, 0);
394 /* Disable priority flow control */
395 ocelot_fields_write(ocelot
, port
,
396 QSYS_SWITCH_PORT_MODE_TX_PFC_ENA
, 0);
398 /* Wait at least the time it takes to receive a frame of maximum length
400 * Worst-case delays for 10 kilobyte jumbo frames are:
402 * 800 μs on a 100M port
403 * 80 μs on a 1G port
404 * 32 μs on a 2.5G port
406 usleep_range(8000, 10000);
408 /* Disable half duplex backpressure. */
409 ocelot_rmw_rix(ocelot
, 0, SYS_FRONT_PORT_MODE_HDX_MODE
,
410 SYS_FRONT_PORT_MODE
, port
);
412 /* Flush the queues associated with the port. */
413 ocelot_rmw_gix(ocelot
, REW_PORT_CFG_FLUSH_ENA
, REW_PORT_CFG_FLUSH_ENA
,
416 /* Enable dequeuing from the egress queues. */
417 ocelot_rmw_rix(ocelot
, 0, QSYS_PORT_MODE_DEQUEUE_DIS
, QSYS_PORT_MODE
,
420 /* Wait until flushing is complete. */
421 err
= read_poll_timeout(ocelot_read_eq_avail
, val
, !val
,
422 100, 2000000, false, ocelot
, port
);
424 /* Clear flushing again. */
425 ocelot_rmw_gix(ocelot
, 0, REW_PORT_CFG_FLUSH_ENA
, REW_PORT_CFG
, port
);
427 /* Re-enable flow control */
428 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_ENA
, pause_ena
);
432 EXPORT_SYMBOL(ocelot_port_flush
);
434 void ocelot_adjust_link(struct ocelot
*ocelot
, int port
,
435 struct phy_device
*phydev
)
437 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
440 switch (phydev
->speed
) {
442 speed
= OCELOT_SPEED_10
;
445 speed
= OCELOT_SPEED_100
;
448 speed
= OCELOT_SPEED_1000
;
449 mode
= DEV_MAC_MODE_CFG_GIGA_MODE_ENA
;
452 speed
= OCELOT_SPEED_2500
;
453 mode
= DEV_MAC_MODE_CFG_GIGA_MODE_ENA
;
456 dev_err(ocelot
->dev
, "Unsupported PHY speed on port %d: %d\n",
457 port
, phydev
->speed
);
461 phy_print_status(phydev
);
466 /* Only full duplex supported for now */
467 ocelot_port_writel(ocelot_port
, DEV_MAC_MODE_CFG_FDX_ENA
|
468 mode
, DEV_MAC_MODE_CFG
);
470 /* Disable HDX fast control */
471 ocelot_port_writel(ocelot_port
, DEV_PORT_MISC_HDX_FAST_DIS
,
474 /* SGMII only for now */
475 ocelot_port_writel(ocelot_port
, PCS1G_MODE_CFG_SGMII_MODE_ENA
,
477 ocelot_port_writel(ocelot_port
, PCS1G_SD_CFG_SD_SEL
, PCS1G_SD_CFG
);
480 ocelot_port_writel(ocelot_port
, PCS1G_CFG_PCS_ENA
, PCS1G_CFG
);
482 /* No aneg on SGMII */
483 ocelot_port_writel(ocelot_port
, 0, PCS1G_ANEG_CFG
);
486 ocelot_port_writel(ocelot_port
, 0, PCS1G_LB_CFG
);
488 /* Enable MAC module */
489 ocelot_port_writel(ocelot_port
, DEV_MAC_ENA_CFG_RX_ENA
|
490 DEV_MAC_ENA_CFG_TX_ENA
, DEV_MAC_ENA_CFG
);
492 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
495 ocelot_port_writel(ocelot_port
, DEV_CLOCK_CFG_LINK_SPEED(speed
),
499 ocelot_write_gix(ocelot
, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed
),
500 ANA_PFC_PFC_CFG
, port
);
502 /* Core: Enable port for frame transfer */
503 ocelot_fields_write(ocelot
, port
,
504 QSYS_SWITCH_PORT_MODE_PORT_ENA
, 1);
507 ocelot_write_rix(ocelot
, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
508 SYS_MAC_FC_CFG_RX_FC_ENA
| SYS_MAC_FC_CFG_TX_FC_ENA
|
509 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA
|
510 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
511 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed
),
512 SYS_MAC_FC_CFG
, port
);
513 ocelot_write_rix(ocelot
, 0, ANA_POL_FLOWC
, port
);
515 EXPORT_SYMBOL(ocelot_adjust_link
);
517 void ocelot_port_enable(struct ocelot
*ocelot
, int port
,
518 struct phy_device
*phy
)
520 /* Enable receiving frames on the port, and activate auto-learning of
523 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_LEARNAUTO
|
524 ANA_PORT_PORT_CFG_RECV_ENA
|
525 ANA_PORT_PORT_CFG_PORTID_VAL(port
),
526 ANA_PORT_PORT_CFG
, port
);
528 EXPORT_SYMBOL(ocelot_port_enable
);
530 void ocelot_port_disable(struct ocelot
*ocelot
, int port
)
532 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
534 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_ENA_CFG
);
535 ocelot_fields_write(ocelot
, port
, QSYS_SWITCH_PORT_MODE_PORT_ENA
, 0);
537 EXPORT_SYMBOL(ocelot_port_disable
);
539 static void ocelot_port_add_txtstamp_skb(struct ocelot
*ocelot
, int port
,
540 struct sk_buff
*clone
)
542 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
544 spin_lock(&ocelot_port
->ts_id_lock
);
546 skb_shinfo(clone
)->tx_flags
|= SKBTX_IN_PROGRESS
;
547 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
548 OCELOT_SKB_CB(clone
)->ts_id
= ocelot_port
->ts_id
;
549 ocelot_port
->ts_id
= (ocelot_port
->ts_id
+ 1) % 4;
550 skb_queue_tail(&ocelot_port
->tx_skbs
, clone
);
552 spin_unlock(&ocelot_port
->ts_id_lock
);
555 u32
ocelot_ptp_rew_op(struct sk_buff
*skb
)
557 struct sk_buff
*clone
= OCELOT_SKB_CB(skb
)->clone
;
558 u8 ptp_cmd
= OCELOT_SKB_CB(skb
)->ptp_cmd
;
561 if (ptp_cmd
== IFH_REW_OP_TWO_STEP_PTP
&& clone
) {
563 rew_op
|= OCELOT_SKB_CB(clone
)->ts_id
<< 3;
564 } else if (ptp_cmd
== IFH_REW_OP_ORIGIN_PTP
) {
570 EXPORT_SYMBOL(ocelot_ptp_rew_op
);
572 static bool ocelot_ptp_is_onestep_sync(struct sk_buff
*skb
)
574 struct ptp_header
*hdr
;
575 unsigned int ptp_class
;
578 ptp_class
= ptp_classify_raw(skb
);
579 if (ptp_class
== PTP_CLASS_NONE
)
582 hdr
= ptp_parse_header(skb
, ptp_class
);
586 msgtype
= ptp_get_msgtype(hdr
, ptp_class
);
587 twostep
= hdr
->flag_field
[0] & 0x2;
589 if (msgtype
== PTP_MSGTYPE_SYNC
&& twostep
== 0)
595 int ocelot_port_txtstamp_request(struct ocelot
*ocelot
, int port
,
597 struct sk_buff
**clone
)
599 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
600 u8 ptp_cmd
= ocelot_port
->ptp_cmd
;
602 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
603 if (ptp_cmd
== IFH_REW_OP_ORIGIN_PTP
) {
604 if (ocelot_ptp_is_onestep_sync(skb
)) {
605 OCELOT_SKB_CB(skb
)->ptp_cmd
= ptp_cmd
;
609 /* Fall back to two-step timestamping */
610 ptp_cmd
= IFH_REW_OP_TWO_STEP_PTP
;
613 if (ptp_cmd
== IFH_REW_OP_TWO_STEP_PTP
) {
614 *clone
= skb_clone_sk(skb
);
618 ocelot_port_add_txtstamp_skb(ocelot
, port
, *clone
);
619 OCELOT_SKB_CB(skb
)->ptp_cmd
= ptp_cmd
;
624 EXPORT_SYMBOL(ocelot_port_txtstamp_request
);
626 static void ocelot_get_hwtimestamp(struct ocelot
*ocelot
,
627 struct timespec64
*ts
)
632 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
634 /* Read current PTP time to get seconds */
635 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
637 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
638 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE
);
639 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
640 ts
->tv_sec
= ocelot_read_rix(ocelot
, PTP_PIN_TOD_SEC_LSB
, TOD_ACC_PIN
);
642 /* Read packet HW timestamp from FIFO */
643 val
= ocelot_read(ocelot
, SYS_PTP_TXSTAMP
);
644 ts
->tv_nsec
= SYS_PTP_TXSTAMP_PTP_TXSTAMP(val
);
646 /* Sec has incremented since the ts was registered */
647 if ((ts
->tv_sec
& 0x1) != !!(val
& SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC
))
650 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
653 void ocelot_get_txtstamp(struct ocelot
*ocelot
)
655 int budget
= OCELOT_PTP_QUEUE_SZ
;
658 struct sk_buff
*skb
, *skb_tmp
, *skb_match
= NULL
;
659 struct skb_shared_hwtstamps shhwtstamps
;
660 struct ocelot_port
*port
;
661 struct timespec64 ts
;
665 val
= ocelot_read(ocelot
, SYS_PTP_STATUS
);
667 /* Check if a timestamp can be retrieved */
668 if (!(val
& SYS_PTP_STATUS_PTP_MESS_VLD
))
671 WARN_ON(val
& SYS_PTP_STATUS_PTP_OVFL
);
673 /* Retrieve the ts ID and Tx port */
674 id
= SYS_PTP_STATUS_PTP_MESS_ID_X(val
);
675 txport
= SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val
);
677 /* Retrieve its associated skb */
678 port
= ocelot
->ports
[txport
];
680 spin_lock_irqsave(&port
->tx_skbs
.lock
, flags
);
682 skb_queue_walk_safe(&port
->tx_skbs
, skb
, skb_tmp
) {
683 if (OCELOT_SKB_CB(skb
)->ts_id
!= id
)
685 __skb_unlink(skb
, &port
->tx_skbs
);
690 spin_unlock_irqrestore(&port
->tx_skbs
.lock
, flags
);
692 /* Get the h/w timestamp */
693 ocelot_get_hwtimestamp(ocelot
, &ts
);
695 if (unlikely(!skb_match
))
698 /* Set the timestamp into the skb */
699 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
700 shhwtstamps
.hwtstamp
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
701 skb_complete_tx_timestamp(skb_match
, &shhwtstamps
);
704 ocelot_write(ocelot
, SYS_PTP_NXT_PTP_NXT
, SYS_PTP_NXT
);
707 EXPORT_SYMBOL(ocelot_get_txtstamp
);
709 static int ocelot_rx_frame_word(struct ocelot
*ocelot
, u8 grp
, bool ifh
,
712 u32 bytes_valid
, val
;
714 val
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
715 if (val
== XTR_NOT_READY
) {
720 val
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
721 } while (val
== XTR_NOT_READY
);
732 bytes_valid
= XTR_VALID_BYTES(val
);
733 val
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
734 if (val
== XTR_ESCAPE
)
735 *rval
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
741 *rval
= ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
751 static int ocelot_xtr_poll_xfh(struct ocelot
*ocelot
, int grp
, u32
*xfh
)
755 for (i
= 0; i
< OCELOT_TAG_LEN
/ 4; i
++) {
756 err
= ocelot_rx_frame_word(ocelot
, grp
, true, &xfh
[i
]);
758 return (err
< 0) ? err
: -EIO
;
764 int ocelot_xtr_poll_frame(struct ocelot
*ocelot
, int grp
, struct sk_buff
**nskb
)
766 struct skb_shared_hwtstamps
*shhwtstamps
;
767 u64 tod_in_ns
, full_ts_in_ns
;
768 u64 timestamp
, src_port
, len
;
769 u32 xfh
[OCELOT_TAG_LEN
/ 4];
770 struct net_device
*dev
;
771 struct timespec64 ts
;
777 err
= ocelot_xtr_poll_xfh(ocelot
, grp
, xfh
);
781 ocelot_xfh_get_src_port(xfh
, &src_port
);
782 ocelot_xfh_get_len(xfh
, &len
);
783 ocelot_xfh_get_rew_val(xfh
, ×tamp
);
785 if (WARN_ON(src_port
>= ocelot
->num_phys_ports
))
788 dev
= ocelot
->ops
->port_to_netdev(ocelot
, src_port
);
792 skb
= netdev_alloc_skb(dev
, len
);
793 if (unlikely(!skb
)) {
794 netdev_err(dev
, "Unable to allocate sk_buff\n");
798 buf_len
= len
- ETH_FCS_LEN
;
799 buf
= (u32
*)skb_put(skb
, buf_len
);
803 sz
= ocelot_rx_frame_word(ocelot
, grp
, false, &val
);
810 } while (len
< buf_len
);
813 sz
= ocelot_rx_frame_word(ocelot
, grp
, false, &val
);
819 /* Update the statistics if part of the FCS was read before */
820 len
-= ETH_FCS_LEN
- sz
;
822 if (unlikely(dev
->features
& NETIF_F_RXFCS
)) {
823 buf
= (u32
*)skb_put(skb
, ETH_FCS_LEN
);
828 ocelot_ptp_gettime64(&ocelot
->ptp_info
, &ts
);
830 tod_in_ns
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
831 if ((tod_in_ns
& 0xffffffff) < timestamp
)
832 full_ts_in_ns
= (((tod_in_ns
>> 32) - 1) << 32) |
835 full_ts_in_ns
= (tod_in_ns
& GENMASK_ULL(63, 32)) |
838 shhwtstamps
= skb_hwtstamps(skb
);
839 memset(shhwtstamps
, 0, sizeof(struct skb_shared_hwtstamps
));
840 shhwtstamps
->hwtstamp
= full_ts_in_ns
;
843 /* Everything we see on an interface that is in the HW bridge
844 * has already been forwarded.
846 if (ocelot
->ports
[src_port
]->bridge
)
847 skb
->offload_fwd_mark
= 1;
849 skb
->protocol
= eth_type_trans(skb
, dev
);
859 EXPORT_SYMBOL(ocelot_xtr_poll_frame
);
861 bool ocelot_can_inject(struct ocelot
*ocelot
, int grp
)
863 u32 val
= ocelot_read(ocelot
, QS_INJ_STATUS
);
865 if (!(val
& QS_INJ_STATUS_FIFO_RDY(BIT(grp
))))
867 if (val
& QS_INJ_STATUS_WMARK_REACHED(BIT(grp
)))
872 EXPORT_SYMBOL(ocelot_can_inject
);
874 void ocelot_port_inject_frame(struct ocelot
*ocelot
, int port
, int grp
,
875 u32 rew_op
, struct sk_buff
*skb
)
877 u32 ifh
[OCELOT_TAG_LEN
/ 4] = {0};
878 unsigned int i
, count
, last
;
880 ocelot_write_rix(ocelot
, QS_INJ_CTRL_GAP_SIZE(1) |
881 QS_INJ_CTRL_SOF
, QS_INJ_CTRL
, grp
);
883 ocelot_ifh_set_bypass(ifh
, 1);
884 ocelot_ifh_set_dest(ifh
, BIT_ULL(port
));
885 ocelot_ifh_set_tag_type(ifh
, IFH_TAG_TYPE_C
);
886 ocelot_ifh_set_vid(ifh
, skb_vlan_tag_get(skb
));
887 ocelot_ifh_set_rew_op(ifh
, rew_op
);
889 for (i
= 0; i
< OCELOT_TAG_LEN
/ 4; i
++)
890 ocelot_write_rix(ocelot
, ifh
[i
], QS_INJ_WR
, grp
);
892 count
= DIV_ROUND_UP(skb
->len
, 4);
894 for (i
= 0; i
< count
; i
++)
895 ocelot_write_rix(ocelot
, ((u32
*)skb
->data
)[i
], QS_INJ_WR
, grp
);
898 while (i
< (OCELOT_BUFFER_CELL_SZ
/ 4)) {
899 ocelot_write_rix(ocelot
, 0, QS_INJ_WR
, grp
);
903 /* Indicate EOF and valid bytes in last word */
904 ocelot_write_rix(ocelot
, QS_INJ_CTRL_GAP_SIZE(1) |
905 QS_INJ_CTRL_VLD_BYTES(skb
->len
< OCELOT_BUFFER_CELL_SZ
? 0 : last
) |
910 ocelot_write_rix(ocelot
, 0, QS_INJ_WR
, grp
);
911 skb_tx_timestamp(skb
);
913 skb
->dev
->stats
.tx_packets
++;
914 skb
->dev
->stats
.tx_bytes
+= skb
->len
;
916 EXPORT_SYMBOL(ocelot_port_inject_frame
);
918 void ocelot_drain_cpu_queue(struct ocelot
*ocelot
, int grp
)
920 while (ocelot_read(ocelot
, QS_XTR_DATA_PRESENT
) & BIT(grp
))
921 ocelot_read_rix(ocelot
, QS_XTR_RD
, grp
);
923 EXPORT_SYMBOL(ocelot_drain_cpu_queue
);
925 int ocelot_fdb_add(struct ocelot
*ocelot
, int port
,
926 const unsigned char *addr
, u16 vid
)
930 if (port
== ocelot
->npi
)
933 return ocelot_mact_learn(ocelot
, pgid
, addr
, vid
, ENTRYTYPE_LOCKED
);
935 EXPORT_SYMBOL(ocelot_fdb_add
);
937 int ocelot_fdb_del(struct ocelot
*ocelot
, int port
,
938 const unsigned char *addr
, u16 vid
)
940 return ocelot_mact_forget(ocelot
, addr
, vid
);
942 EXPORT_SYMBOL(ocelot_fdb_del
);
944 int ocelot_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
945 bool is_static
, void *data
)
947 struct ocelot_dump_ctx
*dump
= data
;
948 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
949 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
950 struct nlmsghdr
*nlh
;
953 if (dump
->idx
< dump
->cb
->args
[2])
956 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
957 sizeof(*ndm
), NLM_F_MULTI
);
961 ndm
= nlmsg_data(nlh
);
962 ndm
->ndm_family
= AF_BRIDGE
;
965 ndm
->ndm_flags
= NTF_SELF
;
967 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
968 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
970 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
971 goto nla_put_failure
;
973 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
974 goto nla_put_failure
;
976 nlmsg_end(dump
->skb
, nlh
);
983 nlmsg_cancel(dump
->skb
, nlh
);
986 EXPORT_SYMBOL(ocelot_port_fdb_do_dump
);
988 static int ocelot_mact_read(struct ocelot
*ocelot
, int port
, int row
, int col
,
989 struct ocelot_mact_entry
*entry
)
991 u32 val
, dst
, macl
, mach
;
994 /* Set row and column to read from */
995 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_M_INDEX
, row
);
996 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_BUCKET
, col
);
998 /* Issue a read command */
1000 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ
),
1001 ANA_TABLES_MACACCESS
);
1003 if (ocelot_mact_wait_for_completion(ocelot
))
1006 /* Read the entry flags */
1007 val
= ocelot_read(ocelot
, ANA_TABLES_MACACCESS
);
1008 if (!(val
& ANA_TABLES_MACACCESS_VALID
))
1011 /* If the entry read has another port configured as its destination,
1014 dst
= (val
& ANA_TABLES_MACACCESS_DEST_IDX_M
) >> 3;
1018 /* Get the entry's MAC address and VLAN id */
1019 macl
= ocelot_read(ocelot
, ANA_TABLES_MACLDATA
);
1020 mach
= ocelot_read(ocelot
, ANA_TABLES_MACHDATA
);
1022 mac
[0] = (mach
>> 8) & 0xff;
1023 mac
[1] = (mach
>> 0) & 0xff;
1024 mac
[2] = (macl
>> 24) & 0xff;
1025 mac
[3] = (macl
>> 16) & 0xff;
1026 mac
[4] = (macl
>> 8) & 0xff;
1027 mac
[5] = (macl
>> 0) & 0xff;
1029 entry
->vid
= (mach
>> 16) & 0xfff;
1030 ether_addr_copy(entry
->mac
, mac
);
1035 int ocelot_fdb_dump(struct ocelot
*ocelot
, int port
,
1036 dsa_fdb_dump_cb_t
*cb
, void *data
)
1040 /* Loop through all the mac tables entries. */
1041 for (i
= 0; i
< ocelot
->num_mact_rows
; i
++) {
1042 for (j
= 0; j
< 4; j
++) {
1043 struct ocelot_mact_entry entry
;
1047 ret
= ocelot_mact_read(ocelot
, port
, i
, j
, &entry
);
1048 /* If the entry is invalid (wrong port, invalid...),
1056 is_static
= (entry
.type
== ENTRYTYPE_LOCKED
);
1058 ret
= cb(entry
.mac
, entry
.vid
, is_static
, data
);
1066 EXPORT_SYMBOL(ocelot_fdb_dump
);
1068 int ocelot_hwstamp_get(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
1070 return copy_to_user(ifr
->ifr_data
, &ocelot
->hwtstamp_config
,
1071 sizeof(ocelot
->hwtstamp_config
)) ? -EFAULT
: 0;
1073 EXPORT_SYMBOL(ocelot_hwstamp_get
);
1075 int ocelot_hwstamp_set(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
1077 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1078 struct hwtstamp_config cfg
;
1080 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
1083 /* reserved for future extensions */
1087 /* Tx type sanity check */
1088 switch (cfg
.tx_type
) {
1089 case HWTSTAMP_TX_ON
:
1090 ocelot_port
->ptp_cmd
= IFH_REW_OP_TWO_STEP_PTP
;
1092 case HWTSTAMP_TX_ONESTEP_SYNC
:
1093 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1094 * need to update the origin time.
1096 ocelot_port
->ptp_cmd
= IFH_REW_OP_ORIGIN_PTP
;
1098 case HWTSTAMP_TX_OFF
:
1099 ocelot_port
->ptp_cmd
= 0;
1105 mutex_lock(&ocelot
->ptp_lock
);
1107 switch (cfg
.rx_filter
) {
1108 case HWTSTAMP_FILTER_NONE
:
1110 case HWTSTAMP_FILTER_ALL
:
1111 case HWTSTAMP_FILTER_SOME
:
1112 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
1113 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
1114 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
1115 case HWTSTAMP_FILTER_NTP_ALL
:
1116 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
1117 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
1118 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
1119 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1120 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
1121 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
1122 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1123 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
1124 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
1125 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
1128 mutex_unlock(&ocelot
->ptp_lock
);
1132 /* Commit back the result & save it */
1133 memcpy(&ocelot
->hwtstamp_config
, &cfg
, sizeof(cfg
));
1134 mutex_unlock(&ocelot
->ptp_lock
);
1136 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
1138 EXPORT_SYMBOL(ocelot_hwstamp_set
);
1140 void ocelot_get_strings(struct ocelot
*ocelot
, int port
, u32 sset
, u8
*data
)
1144 if (sset
!= ETH_SS_STATS
)
1147 for (i
= 0; i
< ocelot
->num_stats
; i
++)
1148 memcpy(data
+ i
* ETH_GSTRING_LEN
, ocelot
->stats_layout
[i
].name
,
1151 EXPORT_SYMBOL(ocelot_get_strings
);
1153 static void ocelot_update_stats(struct ocelot
*ocelot
)
1157 mutex_lock(&ocelot
->stats_lock
);
1159 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
1160 /* Configure the port to read the stats from */
1161 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(i
), SYS_STAT_CFG
);
1163 for (j
= 0; j
< ocelot
->num_stats
; j
++) {
1165 unsigned int idx
= i
* ocelot
->num_stats
+ j
;
1167 val
= ocelot_read_rix(ocelot
, SYS_COUNT_RX_OCTETS
,
1168 ocelot
->stats_layout
[j
].offset
);
1170 if (val
< (ocelot
->stats
[idx
] & U32_MAX
))
1171 ocelot
->stats
[idx
] += (u64
)1 << 32;
1173 ocelot
->stats
[idx
] = (ocelot
->stats
[idx
] &
1174 ~(u64
)U32_MAX
) + val
;
1178 mutex_unlock(&ocelot
->stats_lock
);
1181 static void ocelot_check_stats_work(struct work_struct
*work
)
1183 struct delayed_work
*del_work
= to_delayed_work(work
);
1184 struct ocelot
*ocelot
= container_of(del_work
, struct ocelot
,
1187 ocelot_update_stats(ocelot
);
1189 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
1190 OCELOT_STATS_CHECK_DELAY
);
1193 void ocelot_get_ethtool_stats(struct ocelot
*ocelot
, int port
, u64
*data
)
1197 /* check and update now */
1198 ocelot_update_stats(ocelot
);
1200 /* Copy all counters */
1201 for (i
= 0; i
< ocelot
->num_stats
; i
++)
1202 *data
++ = ocelot
->stats
[port
* ocelot
->num_stats
+ i
];
1204 EXPORT_SYMBOL(ocelot_get_ethtool_stats
);
1206 int ocelot_get_sset_count(struct ocelot
*ocelot
, int port
, int sset
)
1208 if (sset
!= ETH_SS_STATS
)
1211 return ocelot
->num_stats
;
1213 EXPORT_SYMBOL(ocelot_get_sset_count
);
1215 int ocelot_get_ts_info(struct ocelot
*ocelot
, int port
,
1216 struct ethtool_ts_info
*info
)
1218 info
->phc_index
= ocelot
->ptp_clock
?
1219 ptp_clock_index(ocelot
->ptp_clock
) : -1;
1220 if (info
->phc_index
== -1) {
1221 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
1222 SOF_TIMESTAMPING_RX_SOFTWARE
|
1223 SOF_TIMESTAMPING_SOFTWARE
;
1226 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
1227 SOF_TIMESTAMPING_RX_SOFTWARE
|
1228 SOF_TIMESTAMPING_SOFTWARE
|
1229 SOF_TIMESTAMPING_TX_HARDWARE
|
1230 SOF_TIMESTAMPING_RX_HARDWARE
|
1231 SOF_TIMESTAMPING_RAW_HARDWARE
;
1232 info
->tx_types
= BIT(HWTSTAMP_TX_OFF
) | BIT(HWTSTAMP_TX_ON
) |
1233 BIT(HWTSTAMP_TX_ONESTEP_SYNC
);
1234 info
->rx_filters
= BIT(HWTSTAMP_FILTER_NONE
) | BIT(HWTSTAMP_FILTER_ALL
);
1238 EXPORT_SYMBOL(ocelot_get_ts_info
);
1240 static u32
ocelot_get_bond_mask(struct ocelot
*ocelot
, struct net_device
*bond
,
1241 bool only_active_ports
)
1246 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1247 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1252 if (ocelot_port
->bond
== bond
) {
1253 if (only_active_ports
&& !ocelot_port
->lag_tx_active
)
1263 static u32
ocelot_get_bridge_fwd_mask(struct ocelot
*ocelot
,
1264 struct net_device
*bridge
)
1269 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1270 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1275 if (ocelot_port
->stp_state
== BR_STATE_FORWARDING
&&
1276 ocelot_port
->bridge
== bridge
)
1283 static u32
ocelot_get_dsa_8021q_cpu_mask(struct ocelot
*ocelot
)
1288 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1289 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1294 if (ocelot_port
->is_dsa_8021q_cpu
)
1301 void ocelot_apply_bridge_fwd_mask(struct ocelot
*ocelot
)
1303 unsigned long cpu_fwd_mask
;
1306 /* If a DSA tag_8021q CPU exists, it needs to be included in the
1307 * regular forwarding path of the front ports regardless of whether
1308 * those are bridged or standalone.
1309 * If DSA tag_8021q is not used, this returns 0, which is fine because
1310 * the hardware-based CPU port module can be a destination for packets
1311 * even if it isn't part of PGID_SRC.
1313 cpu_fwd_mask
= ocelot_get_dsa_8021q_cpu_mask(ocelot
);
1315 /* Apply FWD mask. The loop is needed to add/remove the current port as
1316 * a source for the other ports.
1318 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1319 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1323 /* Unused ports can't send anywhere */
1325 } else if (ocelot_port
->is_dsa_8021q_cpu
) {
1326 /* The DSA tag_8021q CPU ports need to be able to
1327 * forward packets to all other ports except for
1330 mask
= GENMASK(ocelot
->num_phys_ports
- 1, 0);
1331 mask
&= ~cpu_fwd_mask
;
1332 } else if (ocelot_port
->bridge
) {
1333 struct net_device
*bridge
= ocelot_port
->bridge
;
1334 struct net_device
*bond
= ocelot_port
->bond
;
1336 mask
= ocelot_get_bridge_fwd_mask(ocelot
, bridge
);
1339 mask
&= ~ocelot_get_bond_mask(ocelot
, bond
,
1343 /* Standalone ports forward only to DSA tag_8021q CPU
1344 * ports (if those exist), or to the hardware CPU port
1347 mask
= cpu_fwd_mask
;
1350 ocelot_write_rix(ocelot
, mask
, ANA_PGID_PGID
, PGID_SRC
+ port
);
1353 EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask
);
1355 void ocelot_bridge_stp_state_set(struct ocelot
*ocelot
, int port
, u8 state
)
1357 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1360 ocelot_port
->stp_state
= state
;
1362 if ((state
== BR_STATE_LEARNING
|| state
== BR_STATE_FORWARDING
) &&
1363 ocelot_port
->learn_ena
)
1364 learn_ena
= ANA_PORT_PORT_CFG_LEARN_ENA
;
1366 ocelot_rmw_gix(ocelot
, learn_ena
, ANA_PORT_PORT_CFG_LEARN_ENA
,
1367 ANA_PORT_PORT_CFG
, port
);
1369 ocelot_apply_bridge_fwd_mask(ocelot
);
1371 EXPORT_SYMBOL(ocelot_bridge_stp_state_set
);
1373 void ocelot_set_ageing_time(struct ocelot
*ocelot
, unsigned int msecs
)
1375 unsigned int age_period
= ANA_AUTOAGE_AGE_PERIOD(msecs
/ 2000);
1377 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1378 * which is clearly not what our intention is. So avoid that.
1383 ocelot_rmw(ocelot
, age_period
, ANA_AUTOAGE_AGE_PERIOD_M
, ANA_AUTOAGE
);
1385 EXPORT_SYMBOL(ocelot_set_ageing_time
);
1387 static struct ocelot_multicast
*ocelot_multicast_get(struct ocelot
*ocelot
,
1388 const unsigned char *addr
,
1391 struct ocelot_multicast
*mc
;
1393 list_for_each_entry(mc
, &ocelot
->multicast
, list
) {
1394 if (ether_addr_equal(mc
->addr
, addr
) && mc
->vid
== vid
)
1401 static enum macaccess_entry_type
ocelot_classify_mdb(const unsigned char *addr
)
1403 if (addr
[0] == 0x01 && addr
[1] == 0x00 && addr
[2] == 0x5e)
1404 return ENTRYTYPE_MACv4
;
1405 if (addr
[0] == 0x33 && addr
[1] == 0x33)
1406 return ENTRYTYPE_MACv6
;
1407 return ENTRYTYPE_LOCKED
;
1410 static struct ocelot_pgid
*ocelot_pgid_alloc(struct ocelot
*ocelot
, int index
,
1411 unsigned long ports
)
1413 struct ocelot_pgid
*pgid
;
1415 pgid
= kzalloc(sizeof(*pgid
), GFP_KERNEL
);
1417 return ERR_PTR(-ENOMEM
);
1419 pgid
->ports
= ports
;
1420 pgid
->index
= index
;
1421 refcount_set(&pgid
->refcount
, 1);
1422 list_add_tail(&pgid
->list
, &ocelot
->pgids
);
1427 static void ocelot_pgid_free(struct ocelot
*ocelot
, struct ocelot_pgid
*pgid
)
1429 if (!refcount_dec_and_test(&pgid
->refcount
))
1432 list_del(&pgid
->list
);
1436 static struct ocelot_pgid
*ocelot_mdb_get_pgid(struct ocelot
*ocelot
,
1437 const struct ocelot_multicast
*mc
)
1439 struct ocelot_pgid
*pgid
;
1442 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1443 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1444 * destination mask table (PGID), the destination set is programmed as
1445 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1447 if (mc
->entry_type
== ENTRYTYPE_MACv4
||
1448 mc
->entry_type
== ENTRYTYPE_MACv6
)
1449 return ocelot_pgid_alloc(ocelot
, 0, mc
->ports
);
1451 list_for_each_entry(pgid
, &ocelot
->pgids
, list
) {
1452 /* When searching for a nonreserved multicast PGID, ignore the
1453 * dummy PGID of zero that we have for MACv4/MACv6 entries
1455 if (pgid
->index
&& pgid
->ports
== mc
->ports
) {
1456 refcount_inc(&pgid
->refcount
);
1461 /* Search for a free index in the nonreserved multicast PGID area */
1462 for_each_nonreserved_multicast_dest_pgid(ocelot
, index
) {
1465 list_for_each_entry(pgid
, &ocelot
->pgids
, list
) {
1466 if (pgid
->index
== index
) {
1473 return ocelot_pgid_alloc(ocelot
, index
, mc
->ports
);
1476 return ERR_PTR(-ENOSPC
);
1479 static void ocelot_encode_ports_to_mdb(unsigned char *addr
,
1480 struct ocelot_multicast
*mc
)
1482 ether_addr_copy(addr
, mc
->addr
);
1484 if (mc
->entry_type
== ENTRYTYPE_MACv4
) {
1486 addr
[1] = mc
->ports
>> 8;
1487 addr
[2] = mc
->ports
& 0xff;
1488 } else if (mc
->entry_type
== ENTRYTYPE_MACv6
) {
1489 addr
[0] = mc
->ports
>> 8;
1490 addr
[1] = mc
->ports
& 0xff;
1494 int ocelot_port_mdb_add(struct ocelot
*ocelot
, int port
,
1495 const struct switchdev_obj_port_mdb
*mdb
)
1497 unsigned char addr
[ETH_ALEN
];
1498 struct ocelot_multicast
*mc
;
1499 struct ocelot_pgid
*pgid
;
1502 if (port
== ocelot
->npi
)
1503 port
= ocelot
->num_phys_ports
;
1505 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1508 mc
= devm_kzalloc(ocelot
->dev
, sizeof(*mc
), GFP_KERNEL
);
1512 mc
->entry_type
= ocelot_classify_mdb(mdb
->addr
);
1513 ether_addr_copy(mc
->addr
, mdb
->addr
);
1516 list_add_tail(&mc
->list
, &ocelot
->multicast
);
1518 /* Existing entry. Clean up the current port mask from
1519 * hardware now, because we'll be modifying it.
1521 ocelot_pgid_free(ocelot
, mc
->pgid
);
1522 ocelot_encode_ports_to_mdb(addr
, mc
);
1523 ocelot_mact_forget(ocelot
, addr
, vid
);
1526 mc
->ports
|= BIT(port
);
1528 pgid
= ocelot_mdb_get_pgid(ocelot
, mc
);
1530 dev_err(ocelot
->dev
,
1531 "Cannot allocate PGID for mdb %pM vid %d\n",
1533 devm_kfree(ocelot
->dev
, mc
);
1534 return PTR_ERR(pgid
);
1538 ocelot_encode_ports_to_mdb(addr
, mc
);
1540 if (mc
->entry_type
!= ENTRYTYPE_MACv4
&&
1541 mc
->entry_type
!= ENTRYTYPE_MACv6
)
1542 ocelot_write_rix(ocelot
, pgid
->ports
, ANA_PGID_PGID
,
1545 return ocelot_mact_learn(ocelot
, pgid
->index
, addr
, vid
,
1548 EXPORT_SYMBOL(ocelot_port_mdb_add
);
1550 int ocelot_port_mdb_del(struct ocelot
*ocelot
, int port
,
1551 const struct switchdev_obj_port_mdb
*mdb
)
1553 unsigned char addr
[ETH_ALEN
];
1554 struct ocelot_multicast
*mc
;
1555 struct ocelot_pgid
*pgid
;
1558 if (port
== ocelot
->npi
)
1559 port
= ocelot
->num_phys_ports
;
1561 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1565 ocelot_encode_ports_to_mdb(addr
, mc
);
1566 ocelot_mact_forget(ocelot
, addr
, vid
);
1568 ocelot_pgid_free(ocelot
, mc
->pgid
);
1569 mc
->ports
&= ~BIT(port
);
1571 list_del(&mc
->list
);
1572 devm_kfree(ocelot
->dev
, mc
);
1576 /* We have a PGID with fewer ports now */
1577 pgid
= ocelot_mdb_get_pgid(ocelot
, mc
);
1579 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_del
);
1594 void ocelot_port_bridge_join(struct ocelot
*ocelot
, int port
,
1595 struct net_device
*bridge
)
1597 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1599 ocelot_port
->bridge
= bridge
;
1601 ocelot_apply_bridge_fwd_mask(ocelot
);
1603 EXPORT_SYMBOL(ocelot_port_bridge_join
);
1605 void ocelot_port_bridge_leave(struct ocelot
*ocelot
, int port
,
1606 struct net_device
*bridge
)
1608 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1609 struct ocelot_vlan pvid
= {0}, native_vlan
= {0};
1611 ocelot_port
->bridge
= NULL
;
1613 ocelot_port_set_pvid(ocelot
, port
, pvid
);
1614 ocelot_port_set_native_vlan(ocelot
, port
, native_vlan
);
1615 ocelot_apply_bridge_fwd_mask(ocelot
);
1617 EXPORT_SYMBOL(ocelot_port_bridge_leave
);
1619 static void ocelot_set_aggr_pgids(struct ocelot
*ocelot
)
1621 unsigned long visited
= GENMASK(ocelot
->num_phys_ports
- 1, 0);
1624 /* Reset destination and aggregation PGIDS */
1625 for_each_unicast_dest_pgid(ocelot
, port
)
1626 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
1628 for_each_aggr_pgid(ocelot
, i
)
1629 ocelot_write_rix(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
1632 /* The visited ports bitmask holds the list of ports offloading any
1633 * bonding interface. Initially we mark all these ports as unvisited,
1634 * then every time we visit a port in this bitmask, we know that it is
1635 * the lowest numbered port, i.e. the one whose logical ID == physical
1636 * port ID == LAG ID. So we mark as visited all further ports in the
1637 * bitmask that are offloading the same bonding interface. This way,
1638 * we set up the aggregation PGIDs only once per bonding interface.
1640 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1641 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1643 if (!ocelot_port
|| !ocelot_port
->bond
)
1646 visited
&= ~BIT(port
);
1649 /* Now, set PGIDs for each active LAG */
1650 for (lag
= 0; lag
< ocelot
->num_phys_ports
; lag
++) {
1651 struct net_device
*bond
= ocelot
->ports
[lag
]->bond
;
1652 int num_active_ports
= 0;
1653 unsigned long bond_mask
;
1656 if (!bond
|| (visited
& BIT(lag
)))
1659 bond_mask
= ocelot_get_bond_mask(ocelot
, bond
, true);
1661 for_each_set_bit(port
, &bond_mask
, ocelot
->num_phys_ports
) {
1663 ocelot_write_rix(ocelot
, bond_mask
,
1664 ANA_PGID_PGID
, port
);
1665 aggr_idx
[num_active_ports
++] = port
;
1668 for_each_aggr_pgid(ocelot
, i
) {
1671 ac
= ocelot_read_rix(ocelot
, ANA_PGID_PGID
, i
);
1673 /* Don't do division by zero if there was no active
1674 * port. Just make all aggregation codes zero.
1676 if (num_active_ports
)
1677 ac
|= BIT(aggr_idx
[i
% num_active_ports
]);
1678 ocelot_write_rix(ocelot
, ac
, ANA_PGID_PGID
, i
);
1681 /* Mark all ports in the same LAG as visited to avoid applying
1682 * the same config again.
1684 for (port
= lag
; port
< ocelot
->num_phys_ports
; port
++) {
1685 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1690 if (ocelot_port
->bond
== bond
)
1691 visited
|= BIT(port
);
1696 /* When offloading a bonding interface, the switch ports configured under the
1697 * same bond must have the same logical port ID, equal to the physical port ID
1698 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
1699 * bridged mode, each port has a logical port ID equal to its physical port ID.
1701 static void ocelot_setup_logical_port_ids(struct ocelot
*ocelot
)
1705 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
1706 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1707 struct net_device
*bond
;
1712 bond
= ocelot_port
->bond
;
1714 int lag
= __ffs(ocelot_get_bond_mask(ocelot
, bond
,
1717 ocelot_rmw_gix(ocelot
,
1718 ANA_PORT_PORT_CFG_PORTID_VAL(lag
),
1719 ANA_PORT_PORT_CFG_PORTID_VAL_M
,
1720 ANA_PORT_PORT_CFG
, port
);
1722 ocelot_rmw_gix(ocelot
,
1723 ANA_PORT_PORT_CFG_PORTID_VAL(port
),
1724 ANA_PORT_PORT_CFG_PORTID_VAL_M
,
1725 ANA_PORT_PORT_CFG
, port
);
1730 int ocelot_port_lag_join(struct ocelot
*ocelot
, int port
,
1731 struct net_device
*bond
,
1732 struct netdev_lag_upper_info
*info
)
1734 if (info
->tx_type
!= NETDEV_LAG_TX_TYPE_HASH
)
1737 ocelot
->ports
[port
]->bond
= bond
;
1739 ocelot_setup_logical_port_ids(ocelot
);
1740 ocelot_apply_bridge_fwd_mask(ocelot
);
1741 ocelot_set_aggr_pgids(ocelot
);
1745 EXPORT_SYMBOL(ocelot_port_lag_join
);
1747 void ocelot_port_lag_leave(struct ocelot
*ocelot
, int port
,
1748 struct net_device
*bond
)
1750 ocelot
->ports
[port
]->bond
= NULL
;
1752 ocelot_setup_logical_port_ids(ocelot
);
1753 ocelot_apply_bridge_fwd_mask(ocelot
);
1754 ocelot_set_aggr_pgids(ocelot
);
1756 EXPORT_SYMBOL(ocelot_port_lag_leave
);
1758 void ocelot_port_lag_change(struct ocelot
*ocelot
, int port
, bool lag_tx_active
)
1760 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1762 ocelot_port
->lag_tx_active
= lag_tx_active
;
1764 /* Rebalance the LAGs */
1765 ocelot_set_aggr_pgids(ocelot
);
1767 EXPORT_SYMBOL(ocelot_port_lag_change
);
1769 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1770 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1771 * In the special case that it's the NPI port that we're configuring, the
1772 * length of the tag and optional prefix needs to be accounted for privately,
1773 * in order to be able to sustain communication at the requested @sdu.
1775 void ocelot_port_set_maxlen(struct ocelot
*ocelot
, int port
, size_t sdu
)
1777 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1778 int maxlen
= sdu
+ ETH_HLEN
+ ETH_FCS_LEN
;
1779 int pause_start
, pause_stop
;
1782 if (port
== ocelot
->npi
) {
1783 maxlen
+= OCELOT_TAG_LEN
;
1785 if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_SHORT
)
1786 maxlen
+= OCELOT_SHORT_PREFIX_LEN
;
1787 else if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_LONG
)
1788 maxlen
+= OCELOT_LONG_PREFIX_LEN
;
1791 ocelot_port_writel(ocelot_port
, maxlen
, DEV_MAC_MAXLEN_CFG
);
1793 /* Set Pause watermark hysteresis */
1794 pause_start
= 6 * maxlen
/ OCELOT_BUFFER_CELL_SZ
;
1795 pause_stop
= 4 * maxlen
/ OCELOT_BUFFER_CELL_SZ
;
1796 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_START
,
1798 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_STOP
,
1801 /* Tail dropping watermarks */
1802 atop_tot
= (ocelot
->packet_buffer_size
- 9 * maxlen
) /
1803 OCELOT_BUFFER_CELL_SZ
;
1804 atop
= (9 * maxlen
) / OCELOT_BUFFER_CELL_SZ
;
1805 ocelot_write_rix(ocelot
, ocelot
->ops
->wm_enc(atop
), SYS_ATOP
, port
);
1806 ocelot_write(ocelot
, ocelot
->ops
->wm_enc(atop_tot
), SYS_ATOP_TOT_CFG
);
1808 EXPORT_SYMBOL(ocelot_port_set_maxlen
);
1810 int ocelot_get_max_mtu(struct ocelot
*ocelot
, int port
)
1812 int max_mtu
= 65535 - ETH_HLEN
- ETH_FCS_LEN
;
1814 if (port
== ocelot
->npi
) {
1815 max_mtu
-= OCELOT_TAG_LEN
;
1817 if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_SHORT
)
1818 max_mtu
-= OCELOT_SHORT_PREFIX_LEN
;
1819 else if (ocelot
->npi_inj_prefix
== OCELOT_TAG_PREFIX_LONG
)
1820 max_mtu
-= OCELOT_LONG_PREFIX_LEN
;
1825 EXPORT_SYMBOL(ocelot_get_max_mtu
);
1827 static void ocelot_port_set_learning(struct ocelot
*ocelot
, int port
,
1830 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1834 val
= ANA_PORT_PORT_CFG_LEARN_ENA
;
1836 ocelot_rmw_gix(ocelot
, val
, ANA_PORT_PORT_CFG_LEARN_ENA
,
1837 ANA_PORT_PORT_CFG
, port
);
1839 ocelot_port
->learn_ena
= enabled
;
1842 static void ocelot_port_set_ucast_flood(struct ocelot
*ocelot
, int port
,
1850 ocelot_rmw_rix(ocelot
, val
, BIT(port
), ANA_PGID_PGID
, PGID_UC
);
1853 static void ocelot_port_set_mcast_flood(struct ocelot
*ocelot
, int port
,
1861 ocelot_rmw_rix(ocelot
, val
, BIT(port
), ANA_PGID_PGID
, PGID_MC
);
1864 static void ocelot_port_set_bcast_flood(struct ocelot
*ocelot
, int port
,
1872 ocelot_rmw_rix(ocelot
, val
, BIT(port
), ANA_PGID_PGID
, PGID_BC
);
1875 int ocelot_port_pre_bridge_flags(struct ocelot
*ocelot
, int port
,
1876 struct switchdev_brport_flags flags
)
1878 if (flags
.mask
& ~(BR_LEARNING
| BR_FLOOD
| BR_MCAST_FLOOD
|
1884 EXPORT_SYMBOL(ocelot_port_pre_bridge_flags
);
1886 void ocelot_port_bridge_flags(struct ocelot
*ocelot
, int port
,
1887 struct switchdev_brport_flags flags
)
1889 if (flags
.mask
& BR_LEARNING
)
1890 ocelot_port_set_learning(ocelot
, port
,
1891 !!(flags
.val
& BR_LEARNING
));
1893 if (flags
.mask
& BR_FLOOD
)
1894 ocelot_port_set_ucast_flood(ocelot
, port
,
1895 !!(flags
.val
& BR_FLOOD
));
1897 if (flags
.mask
& BR_MCAST_FLOOD
)
1898 ocelot_port_set_mcast_flood(ocelot
, port
,
1899 !!(flags
.val
& BR_MCAST_FLOOD
));
1901 if (flags
.mask
& BR_BCAST_FLOOD
)
1902 ocelot_port_set_bcast_flood(ocelot
, port
,
1903 !!(flags
.val
& BR_BCAST_FLOOD
));
1905 EXPORT_SYMBOL(ocelot_port_bridge_flags
);
1907 void ocelot_init_port(struct ocelot
*ocelot
, int port
)
1909 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1911 skb_queue_head_init(&ocelot_port
->tx_skbs
);
1912 spin_lock_init(&ocelot_port
->ts_id_lock
);
1914 /* Basic L2 initialization */
1917 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1918 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1920 ocelot_port_writel(ocelot_port
, DEV_MAC_IFG_CFG_TX_IFG(5),
1923 /* Load seed (0) and set MAC HDX late collision */
1924 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1925 DEV_MAC_HDX_CFG_SEED_LOAD
,
1928 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1931 /* Set Max Length and maximum tags allowed */
1932 ocelot_port_set_maxlen(ocelot
, port
, ETH_DATA_LEN
);
1933 ocelot_port_writel(ocelot_port
, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD
) |
1934 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA
|
1935 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA
|
1936 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA
,
1939 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1940 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_HIGH_CFG
);
1941 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_LOW_CFG
);
1943 /* Enable transmission of pause frames */
1944 ocelot_fields_write(ocelot
, port
, SYS_PAUSE_CFG_PAUSE_ENA
, 1);
1946 /* Drop frames with multicast source address */
1947 ocelot_rmw_gix(ocelot
, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
1948 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
1949 ANA_PORT_DROP_CFG
, port
);
1951 /* Set default VLAN and tag type to 8021Q. */
1952 ocelot_rmw_gix(ocelot
, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q
),
1953 REW_PORT_VLAN_CFG_PORT_TPID_M
,
1954 REW_PORT_VLAN_CFG
, port
);
1956 /* Disable source address learning for standalone mode */
1957 ocelot_port_set_learning(ocelot
, port
, false);
1959 /* Enable vcap lookups */
1960 ocelot_vcap_enable(ocelot
, port
);
1962 EXPORT_SYMBOL(ocelot_init_port
);
1964 /* Configure and enable the CPU port module, which is a set of queues
1965 * accessible through register MMIO, frame DMA or Ethernet (in case
1966 * NPI mode is used).
1968 static void ocelot_cpu_port_init(struct ocelot
*ocelot
)
1970 int cpu
= ocelot
->num_phys_ports
;
1972 /* The unicast destination PGID for the CPU port module is unused */
1973 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, cpu
);
1974 /* Instead set up a multicast destination PGID for traffic copied to
1975 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1976 * addresses will be copied to the CPU via this PGID.
1978 ocelot_write_rix(ocelot
, BIT(cpu
), ANA_PGID_PGID
, PGID_CPU
);
1979 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_RECV_ENA
|
1980 ANA_PORT_PORT_CFG_PORTID_VAL(cpu
),
1981 ANA_PORT_PORT_CFG
, cpu
);
1983 /* Enable CPU port module */
1984 ocelot_fields_write(ocelot
, cpu
, QSYS_SWITCH_PORT_MODE_PORT_ENA
, 1);
1985 /* CPU port Injection/Extraction configuration */
1986 ocelot_fields_write(ocelot
, cpu
, SYS_PORT_MODE_INCL_XTR_HDR
,
1987 OCELOT_TAG_PREFIX_NONE
);
1988 ocelot_fields_write(ocelot
, cpu
, SYS_PORT_MODE_INCL_INJ_HDR
,
1989 OCELOT_TAG_PREFIX_NONE
);
1991 /* Configure the CPU port to be VLAN aware */
1992 ocelot_write_gix(ocelot
, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1993 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
1994 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1995 ANA_PORT_VLAN_CFG
, cpu
);
1998 static void ocelot_detect_features(struct ocelot
*ocelot
)
2002 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2003 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2004 * 192 bytes as the documentation incorrectly says.
2006 mmgt
= ocelot_read(ocelot
, SYS_MMGT
);
2007 ocelot
->packet_buffer_size
= 240 * SYS_MMGT_FREECNT(mmgt
);
2009 eq_ctrl
= ocelot_read(ocelot
, QSYS_EQ_CTRL
);
2010 ocelot
->num_frame_refs
= QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl
);
2013 int ocelot_init(struct ocelot
*ocelot
)
2015 char queue_name
[32];
2019 if (ocelot
->ops
->reset
) {
2020 ret
= ocelot
->ops
->reset(ocelot
);
2022 dev_err(ocelot
->dev
, "Switch reset failed\n");
2027 ocelot
->stats
= devm_kcalloc(ocelot
->dev
,
2028 ocelot
->num_phys_ports
* ocelot
->num_stats
,
2029 sizeof(u64
), GFP_KERNEL
);
2033 mutex_init(&ocelot
->stats_lock
);
2034 mutex_init(&ocelot
->ptp_lock
);
2035 spin_lock_init(&ocelot
->ptp_clock_lock
);
2036 snprintf(queue_name
, sizeof(queue_name
), "%s-stats",
2037 dev_name(ocelot
->dev
));
2038 ocelot
->stats_queue
= create_singlethread_workqueue(queue_name
);
2039 if (!ocelot
->stats_queue
)
2042 ocelot
->owq
= alloc_ordered_workqueue("ocelot-owq", 0);
2044 destroy_workqueue(ocelot
->stats_queue
);
2048 INIT_LIST_HEAD(&ocelot
->multicast
);
2049 INIT_LIST_HEAD(&ocelot
->pgids
);
2050 ocelot_detect_features(ocelot
);
2051 ocelot_mact_init(ocelot
);
2052 ocelot_vlan_init(ocelot
);
2053 ocelot_vcap_init(ocelot
);
2054 ocelot_cpu_port_init(ocelot
);
2056 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
2057 /* Clear all counters (5 groups) */
2058 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(port
) |
2059 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2063 /* Only use S-Tag */
2064 ocelot_write(ocelot
, ETH_P_8021AD
, SYS_VLAN_ETYPE_CFG
);
2066 /* Aggregation mode */
2067 ocelot_write(ocelot
, ANA_AGGR_CFG_AC_SMAC_ENA
|
2068 ANA_AGGR_CFG_AC_DMAC_ENA
|
2069 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA
|
2070 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA
|
2071 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA
|
2072 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA
,
2075 /* Set MAC age time to default value. The entry is aged after
2078 ocelot_write(ocelot
,
2079 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME
/ 2 / HZ
),
2082 /* Disable learning for frames discarded by VLAN ingress filtering */
2083 regmap_field_write(ocelot
->regfields
[ANA_ADVLEARN_VLAN_CHK
], 1);
2085 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2086 ocelot_write(ocelot
, SYS_FRM_AGING_AGE_TX_ENA
|
2087 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING
);
2089 /* Setup flooding PGIDs */
2090 for (i
= 0; i
< ocelot
->num_flooding_pgids
; i
++)
2091 ocelot_write_rix(ocelot
, ANA_FLOODING_FLD_MULTICAST(PGID_MC
) |
2092 ANA_FLOODING_FLD_BROADCAST(PGID_BC
) |
2093 ANA_FLOODING_FLD_UNICAST(PGID_UC
),
2095 ocelot_write(ocelot
, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6
) |
2096 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC
) |
2097 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4
) |
2098 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC
),
2101 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
2102 /* Transmit the frame to the local port. */
2103 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
2104 /* Do not forward BPDU frames to the front ports. */
2105 ocelot_write_gix(ocelot
,
2106 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2107 ANA_PORT_CPU_FWD_BPDU_CFG
,
2109 /* Ensure bridging is disabled */
2110 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_SRC
+ port
);
2113 for_each_nonreserved_multicast_dest_pgid(ocelot
, i
) {
2114 u32 val
= ANA_PGID_PGID_PGID(GENMASK(ocelot
->num_phys_ports
- 1, 0));
2116 ocelot_write_rix(ocelot
, val
, ANA_PGID_PGID
, i
);
2119 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_BLACKHOLE
);
2121 /* Allow broadcast and unknown L2 multicast to the CPU. */
2122 ocelot_rmw_rix(ocelot
, ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2123 ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2124 ANA_PGID_PGID
, PGID_MC
);
2125 ocelot_rmw_rix(ocelot
, ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2126 ANA_PGID_PGID_PGID(BIT(ocelot
->num_phys_ports
)),
2127 ANA_PGID_PGID
, PGID_BC
);
2128 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV4
);
2129 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV6
);
2131 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2132 * registers endianness.
2134 ocelot_write_rix(ocelot
, QS_INJ_GRP_CFG_BYTE_SWAP
|
2135 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG
, 0);
2136 ocelot_write_rix(ocelot
, QS_XTR_GRP_CFG_BYTE_SWAP
|
2137 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG
, 0);
2138 ocelot_write(ocelot
, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2139 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2140 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2141 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2142 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2143 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2144 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2145 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2146 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG
);
2147 for (i
= 0; i
< 16; i
++)
2148 ocelot_write_rix(ocelot
, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2149 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2150 ANA_CPUQ_8021_CFG
, i
);
2152 INIT_DELAYED_WORK(&ocelot
->stats_work
, ocelot_check_stats_work
);
2153 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
2154 OCELOT_STATS_CHECK_DELAY
);
2158 EXPORT_SYMBOL(ocelot_init
);
2160 void ocelot_deinit(struct ocelot
*ocelot
)
2162 cancel_delayed_work(&ocelot
->stats_work
);
2163 destroy_workqueue(ocelot
->stats_queue
);
2164 destroy_workqueue(ocelot
->owq
);
2165 mutex_destroy(&ocelot
->stats_lock
);
2167 EXPORT_SYMBOL(ocelot_deinit
);
2169 void ocelot_deinit_port(struct ocelot
*ocelot
, int port
)
2171 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
2173 skb_queue_purge(&ocelot_port
->tx_skbs
);
2175 EXPORT_SYMBOL(ocelot_deinit_port
);
2177 MODULE_LICENSE("Dual MIT/GPL");