Re: [PATCH net-next 6/8] net: phy: mscc: timestamping and PHC support

From: Richard Cochran
Date: Thu May 28 2020 - 10:34:50 EST


On Wed, May 27, 2020 at 06:41:56PM +0200, Antoine Tenart wrote:

> +static struct vsc85xx_ptphdr *get_ptp_header(struct sk_buff *skb)
> +{
> + struct ethhdr *ethhdr = eth_hdr(skb);
> + struct iphdr *iphdr = ip_hdr(skb);
> + struct udphdr *udphdr;
> + __u8 proto;
> +
> + if (ethhdr->h_proto == htons(ETH_P_1588))
> + return (struct vsc85xx_ptphdr *)(((unsigned char *)ethhdr) +
> + skb_mac_header_len(skb));
> +
> + if (ethhdr->h_proto != htons(ETH_P_IP))
> + return NULL;
> +
> + proto = iphdr->protocol;
> + if (proto != IPPROTO_UDP)
> + return NULL;
> +
> + udphdr = udp_hdr(skb);
> +
> + if (udphdr->source != ntohs(PTP_EV_PORT) ||
> + udphdr->dest != ntohs(PTP_EV_PORT))
> + return NULL;
> +
> + return (struct vsc85xx_ptphdr *)(((unsigned char *)udphdr) + UDP_HLEN);
> +}

This looks a lot like get_ptp_header_rx() below. Are you sure you
need two almost identical methods?

> +static void vsc85xx_get_tx_ts(struct vsc85xx_ptp *ptp)
> +{
> + struct skb_shared_hwtstamps shhwtstamps;
> + struct sk_buff *skb, *first_skb = NULL;
> + struct vsc85xx_ts_fifo fifo;
> + u8 i, skb_sig[16], *p;
> + unsigned long ns;
> + s64 secs;
> + u32 reg;
> +
> +next_in_fifo:
> + memset(&fifo, 0, sizeof(fifo));
> + p = (u8 *)&fifo;
> +
> + reg = vsc85xx_ts_read_csr(ptp->phydev, PROCESSOR,
> + MSCC_PHY_PTP_EGR_TS_FIFO(0));
> + if (reg & PTP_EGR_TS_FIFO_EMPTY)
> + goto out;
> +
> + *p++ = reg & 0xff;
> + *p++ = (reg >> 8) & 0xff;
> +
> + /* Reading FIFO6 pops the FIFO item */
> + for (i = 1; i < 7; i++) {
> + reg = vsc85xx_ts_read_csr(ptp->phydev, PROCESSOR,
> + MSCC_PHY_PTP_EGR_TS_FIFO(i));
> + *p++ = reg & 0xff;
> + *p++ = (reg >> 8) & 0xff;
> + *p++ = (reg >> 16) & 0xff;
> + *p++ = (reg >> 24) & 0xff;
> + }
> +
> +next_in_queue:
> + skb = skb_dequeue(&ptp->tx_queue);
> + if (!skb || skb == first_skb)
> + goto out;
> +
> + /* Keep the first skb to avoid looping over it again. */
> + if (!first_skb)
> + first_skb = skb;
> +
> + /* Can't get the signature of the packet, won't ever
> + * be able to have one so let's dequeue the packet.
> + */
> + if (get_sig(skb, skb_sig) < 0)
> + goto next_in_queue;
> +
> + /* Valid signature but does not match the one of the
> + * packet in the FIFO right now, reschedule it for later
> + * packets.
> + */
> + if (memcmp(skb_sig, fifo.sig, sizeof(fifo.sig))) {
> + skb_queue_tail(&ptp->tx_queue, skb);
> + goto next_in_queue;
> + }
> +
> + ns = fifo.ns;
> + secs = fifo.secs;
> +
> + memset(&shhwtstamps, 0, sizeof(shhwtstamps));
> + shhwtstamps.hwtstamp = ktime_set(secs, ns);
> + skb_complete_tx_timestamp(skb, &shhwtstamps);
> +
> +out:
> + /* If other timestamps are available in the FIFO, process them. */
> + reg = vsc85xx_ts_read_csr(ptp->phydev, PROCESSOR,
> + MSCC_PHY_PTP_EGR_TS_FIFO_CTRL);
> + if (PTP_EGR_FIFO_LEVEL_LAST_READ(reg) > 1)
> + goto next_in_fifo;
> +}

AFAICT, there is no need for labels and jumps here. Two nested 'for'
loops will do nicely. The inner skb loop can be in a helper function
for clarity. Be sure to use the "safe" iterator over the skbs.


> +static void vsc85xx_txtstamp(struct mii_timestamper *mii_ts,
> + struct sk_buff *skb, int type)
> +{
> + struct vsc8531_private *vsc8531 =
> + container_of(mii_ts, struct vsc8531_private, mii_ts);
> +
> + if (!skb || !vsc8531->ptp->configured)

The skb cannot be NULL here. See net/core/timestamping.c

> + return;
> +
> + if (vsc8531->ptp->tx_type == HWTSTAMP_TX_OFF) {
> + kfree_skb(skb);
> + return;
> + }
> +
> + skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
> + skb_queue_tail(&vsc8531->ptp->tx_queue, skb);
> + /* Scheduling the work for the TS FIFO is handled by the IRQ routine */
> +}
> +
> +static bool vsc85xx_rxtstamp(struct mii_timestamper *mii_ts,
> + struct sk_buff *skb, int type)
> +{
> + struct vsc8531_private *vsc8531 =
> + container_of(mii_ts, struct vsc8531_private, mii_ts);
> + struct skb_shared_hwtstamps *shhwtstamps = NULL;
> + struct vsc85xx_ptphdr *ptphdr;
> + struct timespec64 ts;
> + unsigned long ns;
> +
> + if (!skb || !vsc8531->ptp->configured)

Again, skb can't be null.

Thanks,
Richard