Re: [Patch net-next v3 07/13] net: dsa: microchip: ptp: add packet reception timestamping

From: Vladimir Oltean
Date: Fri Dec 09 2022 - 10:08:56 EST


On Fri, Dec 09, 2022 at 12:54:31PM +0530, Arun Ramadoss wrote:
> From: Christian Eggers <ceggers@xxxxxxx>
>
> Rx Timestamping is done through 4 additional bytes in tail tag.
> Whenever the ptp packet is received, the 4 byte hardware time stamped
> value is added before 1 byte tail tag. Also, bit 7 in tail tag indicates
> it as PTP frame. This 4 byte value is extracted from the tail tag and
> reconstructed to absolute time and assigned to skb hwtstamp.
> If the packet received in PDelay_Resp, then partial ingress timestamp
> is subtracted from the correction field. Since user space tools expects
> to be done in hardware.
>
> Signed-off-by: Christian Eggers <ceggers@xxxxxxx>
> Co-developed-by: Arun Ramadoss <arun.ramadoss@xxxxxxxxxxxxx>
> Signed-off-by: Arun Ramadoss <arun.ramadoss@xxxxxxxxxxxxx>
>
> ---

Reviewed-by: Vladimir Oltean <olteanv@xxxxxxxxx>

> +bool ksz_port_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb,
> + unsigned int type)
> +{
> + struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
> + struct ksz_device *dev = ds->priv;
> + struct ptp_header *ptp_hdr;
> + u8 ptp_msg_type;
> + ktime_t tstamp;
> + s64 correction;
> +
> + tstamp = KSZ_SKB_CB(skb)->tstamp;
> + memset(hwtstamps, 0, sizeof(*hwtstamps));
> + hwtstamps->hwtstamp = ksz_tstamp_reconstruct(dev, tstamp);
> +
> + ptp_hdr = ptp_parse_header(skb, type);
> + if (!ptp_hdr)
> + goto out;
> +
> + ptp_msg_type = ptp_get_msgtype(ptp_hdr, type);
> + if (ptp_msg_type != PTP_MSGTYPE_PDELAY_REQ)
> + goto out;
> +
> + /* Only subtract the partial time stamp from the correction field. When
> + * the hardware adds the egress time stamp to the correction field of
> + * the PDelay_Resp message on tx, also only the partial time stamp will
> + * be added.
> + */
> + correction = (s64)get_unaligned_be64(&ptp_hdr->correction);
> + correction -= ktime_to_ns(tstamp) << 16;
> +
> + ptp_header_update_correction(skb, type, ptp_hdr, correction);
> +
> +out:
> + return 0;

Nitpick: port_rxtstamp() returns "bool" as in "should reception of this
packet be deferred until the full RX timestamp is available?". The
answer of 0 is coincidentally correct, but should have been "false".

> +}
> +
> static int _ksz_ptp_gettime(struct ksz_device *dev, struct timespec64 *ts)
> {
> u32 nanoseconds;