Skip to content

Commit 52849bc

Browse files
vladimirolteankuba-moo
authored andcommitted
net: mscc: ocelot: avoid overflowing the PTP timestamp FIFO
PTP packets with 2-step TX timestamp requests are matched to packets based on the egress port number and a 6-bit timestamp identifier. All PTP timestamps are held in a common FIFO that is 128 entry deep. This patch ensures that back-to-back timestamping requests cannot exceed the hardware FIFO capacity. If that happens, simply send the packets without requesting a TX timestamp to be taken (in the case of felix, since the DSA API has a void return code in ds->ops->port_txtstamp) or drop them (in the case of ocelot). I've moved the ts_id_lock from a per-port basis to a per-switch basis, because we need separate accounting for both numbers of PTP frames in flight. And since we need locking to inc/dec the per-switch counter, that also offers protection for the per-port counter and hence there is no reason to have a per-port counter anymore. Fixes: 4e3b046 ("net: mscc: PTP Hardware Clock (PHC) support") Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent c57fe00 commit 52849bc

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

drivers/net/dsa/ocelot/felix.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,8 +1291,12 @@ static void felix_txtstamp(struct dsa_switch *ds, int port,
12911291
if (!ocelot->ptp)
12921292
return;
12931293

1294-
if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone))
1294+
if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
1295+
dev_err_ratelimited(ds->dev,
1296+
"port %d delivering skb without TX timestamp\n",
1297+
port);
12951298
return;
1299+
}
12961300

12971301
if (clone)
12981302
OCELOT_SKB_CB(skb)->clone = clone;

drivers/net/ethernet/mscc/ocelot.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,22 +569,36 @@ void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
569569
}
570570
EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
571571

572-
static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
573-
struct sk_buff *clone)
572+
static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
573+
struct sk_buff *clone)
574574
{
575575
struct ocelot_port *ocelot_port = ocelot->ports[port];
576+
unsigned long flags;
577+
578+
spin_lock_irqsave(&ocelot->ts_id_lock, flags);
576579

577-
spin_lock(&ocelot_port->ts_id_lock);
580+
if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
581+
ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
582+
spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
583+
return -EBUSY;
584+
}
578585

579586
skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
580587
/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
581588
OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
589+
582590
ocelot_port->ts_id++;
583591
if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
584592
ocelot_port->ts_id = 0;
593+
594+
ocelot_port->ptp_skbs_in_flight++;
595+
ocelot->ptp_skbs_in_flight++;
596+
585597
skb_queue_tail(&ocelot_port->tx_skbs, clone);
586598

587-
spin_unlock(&ocelot_port->ts_id_lock);
599+
spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
600+
601+
return 0;
588602
}
589603

590604
u32 ocelot_ptp_rew_op(struct sk_buff *skb)
@@ -633,6 +647,7 @@ int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
633647
{
634648
struct ocelot_port *ocelot_port = ocelot->ports[port];
635649
u8 ptp_cmd = ocelot_port->ptp_cmd;
650+
int err;
636651

637652
/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
638653
if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
@@ -650,7 +665,10 @@ int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
650665
if (!(*clone))
651666
return -ENOMEM;
652667

653-
ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
668+
err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
669+
if (err)
670+
return err;
671+
654672
OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
655673
}
656674

@@ -709,9 +727,14 @@ void ocelot_get_txtstamp(struct ocelot *ocelot)
709727
id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
710728
txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
711729

712-
/* Retrieve its associated skb */
713730
port = ocelot->ports[txport];
714731

732+
spin_lock(&ocelot->ts_id_lock);
733+
port->ptp_skbs_in_flight--;
734+
ocelot->ptp_skbs_in_flight--;
735+
spin_unlock(&ocelot->ts_id_lock);
736+
737+
/* Retrieve its associated skb */
715738
spin_lock_irqsave(&port->tx_skbs.lock, flags);
716739

717740
skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
@@ -1950,7 +1973,6 @@ void ocelot_init_port(struct ocelot *ocelot, int port)
19501973
struct ocelot_port *ocelot_port = ocelot->ports[port];
19511974

19521975
skb_queue_head_init(&ocelot_port->tx_skbs);
1953-
spin_lock_init(&ocelot_port->ts_id_lock);
19541976

19551977
/* Basic L2 initialization */
19561978

@@ -2083,6 +2105,7 @@ int ocelot_init(struct ocelot *ocelot)
20832105
mutex_init(&ocelot->stats_lock);
20842106
mutex_init(&ocelot->ptp_lock);
20852107
spin_lock_init(&ocelot->ptp_clock_lock);
2108+
spin_lock_init(&ocelot->ts_id_lock);
20862109
snprintf(queue_name, sizeof(queue_name), "%s-stats",
20872110
dev_name(ocelot->dev));
20882111
ocelot->stats_queue = create_singlethread_workqueue(queue_name);

include/soc/mscc/ocelot.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,10 @@ struct ocelot_port {
603603
/* The VLAN ID that will be transmitted as untagged, on egress */
604604
struct ocelot_vlan native_vlan;
605605

606+
unsigned int ptp_skbs_in_flight;
606607
u8 ptp_cmd;
607608
struct sk_buff_head tx_skbs;
608609
u8 ts_id;
609-
spinlock_t ts_id_lock;
610610

611611
phy_interface_t phy_mode;
612612

@@ -680,6 +680,9 @@ struct ocelot {
680680
struct ptp_clock *ptp_clock;
681681
struct ptp_clock_info ptp_info;
682682
struct hwtstamp_config hwtstamp_config;
683+
unsigned int ptp_skbs_in_flight;
684+
/* Protects the 2-step TX timestamp ID logic */
685+
spinlock_t ts_id_lock;
683686
/* Protects the PTP interface state */
684687
struct mutex ptp_lock;
685688
/* Protects the PTP clock */

include/soc/mscc/ocelot_ptp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <soc/mscc/ocelot.h>
1515

1616
#define OCELOT_MAX_PTP_ID 63
17+
#define OCELOT_PTP_FIFO_SIZE 128
1718

1819
#define PTP_PIN_CFG_RSZ 0x20
1920
#define PTP_PIN_TOD_SEC_MSB_RSZ PTP_PIN_CFG_RSZ

0 commit comments

Comments
 (0)