mbox series

[RFC,v2,0/2] AT8031 PHY timestamping support

Message ID 20200228180226.22986-1-michael@walle.cc
Headers show
Series AT8031 PHY timestamping support | expand

Message

Michael Walle Feb. 28, 2020, 6:02 p.m. UTC
I've put some additional time into figuring out how the PHY works since my
last RFC. My previous comments on the interrupt handling was caused by my
broken setup.

This patchset is the current state of my work for adding PHY
timestamping support. I just wanted to post this to the mailinglist
before I never do it. Maybe its a starting point for other people. That
being said, I wouldn't mind comments ;) Also I like to share my findings
about the PHY. The PHY has three major caveats which IMHO makes it really
hard to work properly.

 (1) The PHY doesn't support atomic reading of the (timestamp,
     messageType, sequenceId) tuple. The workaround is to read the
     timestamp again and check if it has changed. Actually, you'd have
     to read the complete tuple again.
 (2) The PHY generates an interrupt on every PTP packet not only on
     event messages. Thus the interrupt handler may read the capture
     registers and then determine that nothing has changed. This also
     means we have to remember the last read timestamp. I make the
     assumption that the timestamp is unique.
 (3) The biggest drawback is that the PHY only provide one set of RX and
     TX capture registers. It is possible that the timestamp will change
     when another PTP event message is received while we are still
     reading the timestamp of the previously received packet.

Example A
The driver basically works when there is low PTP traffic. Eg. the
following works pretty good.

ptp4l -m -2 -i eth0 -f ptp.cfg -s

ptp.cfg:
  [global]
  tx_timestamp_timeout 100

Example B
But if you're using a P2P clock with peer delay requests this whole
thing falls apart because of caveat (3). You'll often see messages like
  received SYNC without timestamp
or
 received PDELAY_RESP without timestamp
in linuxptp. Sometimes it working for some time and then it starts to
loosing packets. I suspect this depends on how the PDELAY messages are
interleaved with the SYNC message. If there is not enough time to until
the next event message is received either of these two messages won't
have a timestamp.

ptp4l -m -f gPTP.cfg -i eth0 -s

gPTP.cfg is the one from stock linuxptp with tx_timestamp_timeout set to
100.

The PHY also supports appending the timestamp to the actual ethernet frame,
but this seems to only work when the PHY is connected via RGMII. I've never
get it to work with a SGMII connection.

The first patch might actually be useful outside of this series. See also
  https://lore.kernel.org/netdev/bd47f8e1ebc04fa98856ed8d89b91419@walle.cc/

Changes since RFC v1:
net: phy: let the driver register its own IRQ handler
 - fixed mistake for calling phy_request_interrupt(). Thanks Heiner.
 - removed phy_drv_interrupt(). just calling phy_mac_interrupt()

net: phy: at803x: add PTP support for AR8031
 - moved rereading the timestamp out of at8031_read_ts() to
   at8031_get_rxts()/at8031_get_txts().
 - call phy_mac_interrupt() instead of phy_drv_interrupt()

Please note that Heiner suggested that I should use handle_interrupt()
instead of registering my own handler. I've still included my old patch
here because the discussion is still ongoing and the proposed fix is only
working for this use case. See also
  https://lore.kernel.org/netdev/2e4371354d84231abf3a63deae1a0d04@walle.cc/

-michael

Michael Walle (2):
  net: phy: let the driver register its own IRQ handler
  net: phy: at803x: add PTP support for AR8031

 drivers/net/phy/Kconfig      |  17 +
 drivers/net/phy/at803x.c     | 855 ++++++++++++++++++++++++++++++++++-
 drivers/net/phy/phy_device.c |   6 +-
 include/linux/phy.h          |   1 +
 4 files changed, 852 insertions(+), 27 deletions(-)

Comments

Richard Cochran Feb. 28, 2020, 6:15 p.m. UTC | #1
On Fri, Feb 28, 2020 at 07:02:24PM +0100, Michael Walle wrote:
>  (1) The PHY doesn't support atomic reading of the (timestamp,
>      messageType, sequenceId) tuple. The workaround is to read the
>      timestamp again and check if it has changed. Actually, you'd have
>      to read the complete tuple again.

This HW is broken by design :(

> But if you're using a P2P clock with peer delay requests this whole
> thing falls apart because of caveat (3). You'll often see messages like
>   received SYNC without timestamp
> or
>  received PDELAY_RESP without timestamp
> in linuxptp. Sometimes it working for some time and then it starts to
> loosing packets. I suspect this depends on how the PDELAY messages are
> interleaved with the SYNC message. If there is not enough time to until
> the next event message is received either of these two messages won't
> have a timestamp.

And even the case where a Sync and a DelayResp arrive at nearly the
same time will fail.

> The PHY also supports appending the timestamp to the actual ethernet frame,
> but this seems to only work when the PHY is connected via RGMII. I've never
> get it to work with a SGMII connection.

This is the way to go.  I would try to get the vendor's help in making
this work.

Thanks,
Richard
Michael Walle Feb. 28, 2020, 7:43 p.m. UTC | #2
Hi Richard,

Am 2020-02-28 19:15, schrieb Richard Cochran:
> On Fri, Feb 28, 2020 at 07:02:24PM +0100, Michael Walle wrote:
>>  (1) The PHY doesn't support atomic reading of the (timestamp,
>>      messageType, sequenceId) tuple. The workaround is to read the
>>      timestamp again and check if it has changed. Actually, you'd have
>>      to read the complete tuple again.
> 
> This HW is broken by design :(

Yeah, I know. And actually I don't think I'll pursue this further. Like 
I
said, I just wanted to my current work. Maybe it will be useful in the
future who knows.

>> But if you're using a P2P clock with peer delay requests this whole
>> thing falls apart because of caveat (3). You'll often see messages 
>> like
>>   received SYNC without timestamp
>> or
>>  received PDELAY_RESP without timestamp
>> in linuxptp. Sometimes it working for some time and then it starts to
>> loosing packets. I suspect this depends on how the PDELAY messages are
>> interleaved with the SYNC message. If there is not enough time to 
>> until
>> the next event message is received either of these two messages won't
>> have a timestamp.
> 
> And even the case where a Sync and a DelayResp arrive at nearly the
> same time will fail.
> 
>> The PHY also supports appending the timestamp to the actual ethernet 
>> frame,
>> but this seems to only work when the PHY is connected via RGMII. I've 
>> never
>> get it to work with a SGMII connection.
> 
> This is the way to go.  I would try to get the vendor's help in making
> this work.

Like I said, our FAE is pretty unresponsive. But I'll at least try to 
find
out if my guess is correct (that it only works with RGMII). But even 
then,
how should the outgoing timestamping work. There are two possibilities:

  (1) According to the datasheet, the PHY will attach the TX timestamp to
      the corresponding RX packet; whatever that means. Lets assume there
      is such a "corresponding packet", then we would be at the mercy of 
the
      peer to actually send such a packet, let alone in a timely manner.
  (2) Mixing both methods. Use attached timestamps for RX packets, read 
the
      timestamp via PHY registers for TX packets. Theoretically, we could
      control how the packets are send and make sure, we fetch the TX
      timestamp before sending another PTP packet. But well.. sounds 
really
      hacky to me.

-michael
Richard Cochran Feb. 29, 2020, 2:48 p.m. UTC | #3
On Fri, Feb 28, 2020 at 08:43:05PM +0100, Michael Walle wrote:
> 
> Yeah, I know. And actually I don't think I'll pursue this further. Like I
> said, I just wanted to my current work. Maybe it will be useful in the
> future who knows.

I appreciate your publishing this work.  It is a good starting place.
Maybe the vendor will wake up and help this along.  One can always hope.
 
> Like I said, our FAE is pretty unresponsive. But I'll at least try to find
> out if my guess is correct (that it only works with RGMII). But even then,
> how should the outgoing timestamping work. There are two possibilities:
> 
>  (1) According to the datasheet, the PHY will attach the TX timestamp to
>      the corresponding RX packet; whatever that means. Lets assume there
>      is such a "corresponding packet", then we would be at the mercy of the
>      peer to actually send such a packet, let alone in a timely manner.

I see.  Mysterious.  For a Sync frame, I can't think of any
"corresponding RX packet".

>  (2) Mixing both methods. Use attached timestamps for RX packets, read the
>      timestamp via PHY registers for TX packets. Theoretically, we could
>      control how the packets are send and make sure, we fetch the TX
>      timestamp before sending another PTP packet. But well.. sounds really
>      hacky to me.

It would not be that bad.  Some of the MAC cards can only buffer one
Tx time stamp, and for this reason, the ptp4l program always fetches
the time stamp immediately after sending.

Thanks,
Richard