Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 4e3b0468e6d7 ("net: mscc: PTP Hardware Clock (PHC) support")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Vladimir Oltean and committed by
Jakub Kicinski
52849bcf c57fe003

+40 -9
+5 -1
drivers/net/dsa/ocelot/felix.c
··· 1291 1291 if (!ocelot->ptp) 1292 1292 return; 1293 1293 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); 1295 1298 return; 1299 + } 1296 1300 1297 1301 if (clone) 1298 1302 OCELOT_SKB_CB(skb)->clone = clone;
+30 -7
drivers/net/ethernet/mscc/ocelot.c
··· 569 569 } 570 570 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up); 571 571 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) 574 574 { 575 575 struct ocelot_port *ocelot_port = ocelot->ports[port]; 576 + unsigned long flags; 576 577 577 - spin_lock(&ocelot_port->ts_id_lock); 578 + spin_lock_irqsave(&ocelot->ts_id_lock, flags); 579 + 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 + } 578 585 579 586 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 580 587 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */ 581 588 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id; 589 + 582 590 ocelot_port->ts_id++; 583 591 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID) 584 592 ocelot_port->ts_id = 0; 593 + 594 + ocelot_port->ptp_skbs_in_flight++; 595 + ocelot->ptp_skbs_in_flight++; 596 + 585 597 skb_queue_tail(&ocelot_port->tx_skbs, clone); 586 598 587 - spin_unlock(&ocelot_port->ts_id_lock); 599 + spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); 600 + 601 + return 0; 588 602 } 589 603 590 604 u32 ocelot_ptp_rew_op(struct sk_buff *skb) ··· 647 633 { 648 634 struct ocelot_port *ocelot_port = ocelot->ports[port]; 649 635 u8 ptp_cmd = ocelot_port->ptp_cmd; 636 + int err; 650 637 651 638 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */ 652 639 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { ··· 665 650 if (!(*clone)) 666 651 return -ENOMEM; 667 652 668 - ocelot_port_add_txtstamp_skb(ocelot, port, *clone); 653 + err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone); 654 + if (err) 655 + return err; 656 + 669 657 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; 670 658 } 671 659 ··· 727 709 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); 728 710 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); 729 711 730 - /* Retrieve its associated skb */ 731 712 port = ocelot->ports[txport]; 732 713 714 + spin_lock(&ocelot->ts_id_lock); 715 + port->ptp_skbs_in_flight--; 716 + ocelot->ptp_skbs_in_flight--; 717 + spin_unlock(&ocelot->ts_id_lock); 718 + 719 + /* Retrieve its associated skb */ 733 720 spin_lock_irqsave(&port->tx_skbs.lock, flags); 734 721 735 722 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { ··· 1973 1950 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1974 1951 1975 1952 skb_queue_head_init(&ocelot_port->tx_skbs); 1976 - spin_lock_init(&ocelot_port->ts_id_lock); 1977 1953 1978 1954 /* Basic L2 initialization */ 1979 1955 ··· 2105 2083 mutex_init(&ocelot->stats_lock); 2106 2084 mutex_init(&ocelot->ptp_lock); 2107 2085 spin_lock_init(&ocelot->ptp_clock_lock); 2086 + spin_lock_init(&ocelot->ts_id_lock); 2108 2087 snprintf(queue_name, sizeof(queue_name), "%s-stats", 2109 2088 dev_name(ocelot->dev)); 2110 2089 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
+4 -1
include/soc/mscc/ocelot.h
··· 603 603 /* The VLAN ID that will be transmitted as untagged, on egress */ 604 604 struct ocelot_vlan native_vlan; 605 605 606 + unsigned int ptp_skbs_in_flight; 606 607 u8 ptp_cmd; 607 608 struct sk_buff_head tx_skbs; 608 609 u8 ts_id; 609 - spinlock_t ts_id_lock; 610 610 611 611 phy_interface_t phy_mode; 612 612 ··· 680 680 struct ptp_clock *ptp_clock; 681 681 struct ptp_clock_info ptp_info; 682 682 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; 683 686 /* Protects the PTP interface state */ 684 687 struct mutex ptp_lock; 685 688 /* Protects the PTP clock */
+1
include/soc/mscc/ocelot_ptp.h
··· 14 14 #include <soc/mscc/ocelot.h> 15 15 16 16 #define OCELOT_MAX_PTP_ID 63 17 + #define OCELOT_PTP_FIFO_SIZE 128 17 18 18 19 #define PTP_PIN_CFG_RSZ 0x20 19 20 #define PTP_PIN_TOD_SEC_MSB_RSZ PTP_PIN_CFG_RSZ