Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /****************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Driver for Solarflare network controllers and boards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright 2011-2013 Solarflare Communications Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) /* Theory of operation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * PTP support is assisted by firmware running on the MC, which provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * the hardware timestamping capabilities.  Both transmitted and received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * PTP event packets are queued onto internal queues for subsequent processing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * this is because the MC operations are relatively long and would block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * block NAPI/interrupt operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * Receive event processing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *	The event contains the packet's UUID and sequence number, together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *	with the hardware timestamp.  The PTP receive packet queue is searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *	for this UUID/sequence number and, if found, put on a pending queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *	Packets not matching are delivered without timestamps (MCDI events will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *	always arrive after the actual packet).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *	It is important for the operation of the PTP protocol that the ordering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *	of packets between the event and general port is maintained.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * Work queue processing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *	If work waiting, synchronise host/hardware time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  *	Transmit: send packet through MC, which returns the transmission time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *	that is converted to an appropriate timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *	Receive: the packet's reception time is converted to an appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *	timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/ktime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/pps_kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/ptp_clock_kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "net_driver.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include "efx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "mcdi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include "mcdi_pcol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include "io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include "farch_regs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include "tx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include "nic.h" /* indirectly includes ptp.h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) /* Maximum number of events expected to make up a PTP event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define	MAX_EVENT_FRAGS			3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) /* Maximum delay, ms, to begin synchronisation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define	MAX_SYNCHRONISE_WAIT_MS		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) /* How long, at most, to spend synchronising */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define	SYNCHRONISE_PERIOD_NS		250000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) /* How often to update the shared memory time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define	SYNCHRONISATION_GRANULARITY_NS	200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) /* Minimum permitted length of a (corrected) synchronisation time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define	DEFAULT_MIN_SYNCHRONISATION_NS	120
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) /* Maximum permitted length of a (corrected) synchronisation time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define	MAX_SYNCHRONISATION_NS		1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) /* How many (MC) receive events that can be queued */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define	MAX_RECEIVE_EVENTS		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) /* Length of (modified) moving average. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define	AVERAGE_LENGTH			16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) /* How long an unmatched event or packet can be held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #define PKT_EVENT_LIFETIME_MS		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) /* Offsets into PTP packet for identification.  These offsets are from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * start of the IP header, not the MAC header.  Note that neither PTP V1 nor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * PTP V2 permit the use of IPV4 options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define PTP_DPORT_OFFSET	22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define PTP_V1_VERSION_LENGTH	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define PTP_V1_VERSION_OFFSET	28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define PTP_V1_UUID_LENGTH	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define PTP_V1_UUID_OFFSET	50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define PTP_V1_SEQUENCE_LENGTH	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define PTP_V1_SEQUENCE_OFFSET	58
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) /* The minimum length of a PTP V1 packet for offsets, etc. to be valid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * includes IP header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #define	PTP_V1_MIN_LENGTH	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define PTP_V2_VERSION_LENGTH	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define PTP_V2_VERSION_OFFSET	29
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define PTP_V2_UUID_LENGTH	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #define PTP_V2_UUID_OFFSET	48
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) /* Although PTP V2 UUIDs are comprised a ClockIdentity (8) and PortNumber (2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * the MC only captures the last six bytes of the clock identity. These values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * reflect those, not the ones used in the standard.  The standard permits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * mapping of V1 UUIDs to V2 UUIDs with these same values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #define PTP_V2_MC_UUID_LENGTH	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #define PTP_V2_MC_UUID_OFFSET	50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #define PTP_V2_SEQUENCE_LENGTH	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #define PTP_V2_SEQUENCE_OFFSET	58
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) /* The minimum length of a PTP V2 packet for offsets, etc. to be valid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * includes IP header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define	PTP_V2_MIN_LENGTH	63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define	PTP_MIN_LENGTH		63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define PTP_ADDRESS		0xe0000181	/* 224.0.1.129 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define PTP_EVENT_PORT		319
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define PTP_GENERAL_PORT	320
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) /* Annoyingly the format of the version numbers are different between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * versions 1 and 2 so it isn't possible to simply look for 1 or 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) #define	PTP_VERSION_V1		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define	PTP_VERSION_V2		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define	PTP_VERSION_V2_MASK	0x0f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) enum ptp_packet_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	PTP_PACKET_STATE_UNMATCHED = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	PTP_PACKET_STATE_MATCHED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	PTP_PACKET_STATE_TIMED_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	PTP_PACKET_STATE_MATCH_UNWANTED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) /* NIC synchronised with single word of time only comprising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  * partial seconds and full nanoseconds: 10^9 ~ 2^30 so 2 bits for seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) #define	MC_NANOSECOND_BITS	30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) #define	MC_NANOSECOND_MASK	((1 << MC_NANOSECOND_BITS) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #define	MC_SECOND_MASK		((1 << (32 - MC_NANOSECOND_BITS)) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) /* Maximum parts-per-billion adjustment that is acceptable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) #define MAX_PPB			1000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) /* Precalculate scale word to avoid long long division at runtime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) /* This is equivalent to 2^66 / 10^9. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) #define PPB_SCALE_WORD  ((1LL << (57)) / 1953125LL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) /* How much to shift down after scaling to convert to FP40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define PPB_SHIFT_FP40		26
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) /* ... and FP44. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #define PPB_SHIFT_FP44		22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) #define PTP_SYNC_ATTEMPTS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  * struct efx_ptp_match - Matching structure, stored in sk_buff's cb area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * @words: UUID and (partial) sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * @expiry: Time after which the packet should be delivered irrespective of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  *            event arrival.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * @state: The state of the packet - whether it is ready for processing or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  *         whether that is of no interest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) struct efx_ptp_match {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	unsigned long expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	enum ptp_packet_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * struct efx_ptp_event_rx - A PTP receive event (from MC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * @link: list of events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * @seq0: First part of (PTP) UUID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  * @seq1: Second part of (PTP) UUID and sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * @hwtimestamp: Event timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * @expiry: Time which the packet arrived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) struct efx_ptp_event_rx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	struct list_head link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	u32 seq0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	u32 seq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	ktime_t hwtimestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	unsigned long expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  * struct efx_ptp_timeset - Synchronisation between host and MC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  * @host_start: Host time immediately before hardware timestamp taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * @major: Hardware timestamp, major
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * @minor: Hardware timestamp, minor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  * @host_end: Host time immediately after hardware timestamp taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * @wait: Number of NIC clock ticks between hardware timestamp being read and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  *          host end time being seen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * @window: Difference of host_end and host_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * @valid: Whether this timeset is valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) struct efx_ptp_timeset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	u32 host_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	u32 major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	u32 minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	u32 host_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	u32 wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	u32 window;	/* Derived: end - start, allowing for wrap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * struct efx_ptp_data - Precision Time Protocol (PTP) state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * @efx: The NIC context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * @channel: The PTP channel (Siena only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  *	separate events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * @rxq: Receive SKB queue (awaiting timestamps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * @txq: Transmit SKB queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  * @evt_list: List of MC receive events awaiting packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  * @evt_free_list: List of free events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220)  * @evt_lock: Lock for manipulating evt_list and evt_free_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221)  * @rx_evts: Instantiated events (on evt_list and evt_free_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  * @workwq: Work queue for processing pending PTP operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223)  * @work: Work task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  * @reset_required: A serious error has occurred and the PTP task needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  *                  reset (disable, enable).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  * @rxfilter_event: Receive filter when operating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * @rxfilter_general: Receive filter when operating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  * @rxfilter_installed: Receive filter installed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229)  * @config: Current timestamp configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230)  * @enabled: PTP operation enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  * @mode: Mode in which PTP operating (PTP version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233)  * @nic_to_kernel_time: Function to convert from NIC to kernel time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234)  * @nic_time: contains time details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235)  * @nic_time.minor_max: Wrap point for NIC minor times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236)  * @nic_time.sync_event_diff_min: Minimum acceptable difference between time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237)  * in packet prefix and last MCDI time sync event i.e. how much earlier than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238)  * the last sync event time a packet timestamp can be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239)  * @nic_time.sync_event_diff_max: Maximum acceptable difference between time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240)  * in packet prefix and last MCDI time sync event i.e. how much later than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241)  * the last sync event time a packet timestamp can be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242)  * @nic_time.sync_event_minor_shift: Shift required to make minor time from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243)  * field in MCDI time sync event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244)  * @min_synchronisation_ns: Minimum acceptable corrected sync window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245)  * @capabilities: Capabilities flags from the NIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * @ts_corrections: contains corrections details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  * @ts_corrections.ptp_tx: Required driver correction of PTP packet transmit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  *                         timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  * @ts_corrections.ptp_rx: Required driver correction of PTP packet receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  *                         timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  * @ts_corrections.pps_out: PPS output error (information only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253)  * @ts_corrections.general_tx: Required driver correction of general packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  *                             transmit timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  * @ts_corrections.general_rx: Required driver correction of general packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  *                             receive timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  * @evt_frags: Partly assembled PTP events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  * @evt_frag_idx: Current fragment number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  * @evt_code: Last event code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  * @start: Address at which MC indicates ready for synchronisation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * @host_time_pps: Host time at last PPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  * @adjfreq_ppb_shift: Shift required to convert scaled parts-per-billion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263)  * frequency adjustment into a fixed point fractional nanosecond format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  * @current_adjfreq: Current ppb adjustment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * @phc_clock: Pointer to registered phc device (if primary function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * @phc_clock_info: Registration structure for phc device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  * @pps_work: pps work task for handling pps events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268)  * @pps_workwq: pps work queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269)  * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270)  * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  *         allocations in main data path).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * @good_syncs: Number of successful synchronisations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  * @fast_syncs: Number of synchronisations requiring short delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  * @bad_syncs: Number of failed synchronisations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  * @sync_timeouts: Number of synchronisation timeouts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * @no_time_syncs: Number of synchronisations with no good times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  * @invalid_sync_windows: Number of sync windows with bad durations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  * @undersize_sync_windows: Number of corrected sync windows that are too small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279)  * @oversize_sync_windows: Number of corrected sync windows that are too large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  * @rx_no_timestamp: Number of packets received without a timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  * @timeset: Last set of synchronisation statistics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  * @xmit_skb: Transmit SKB function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) struct efx_ptp_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct efx_nic *efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct efx_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	bool rx_ts_inline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	struct sk_buff_head rxq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	struct sk_buff_head txq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	struct list_head evt_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	struct list_head evt_free_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	spinlock_t evt_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct workqueue_struct *workwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	bool reset_required;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	u32 rxfilter_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	u32 rxfilter_general;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	bool rxfilter_installed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	struct hwtstamp_config config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	unsigned int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 				      s32 correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		u32 minor_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		u32 sync_event_diff_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		u32 sync_event_diff_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		unsigned int sync_event_minor_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	} nic_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	unsigned int min_synchronisation_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	unsigned int capabilities;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		s32 ptp_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		s32 ptp_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		s32 pps_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		s32 pps_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		s32 general_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		s32 general_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	} ts_corrections;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	efx_qword_t evt_frags[MAX_EVENT_FRAGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	int evt_frag_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	int evt_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	struct efx_buffer start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	struct pps_event_time host_time_pps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	unsigned int adjfreq_ppb_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	s64 current_adjfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	struct ptp_clock *phc_clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	struct ptp_clock_info phc_clock_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	struct work_struct pps_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	struct workqueue_struct *pps_workwq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	bool nic_ts_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	efx_dword_t txbuf[MCDI_TX_BUF_LEN(MC_CMD_PTP_IN_TRANSMIT_LENMAX)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	unsigned int good_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	unsigned int fast_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	unsigned int bad_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	unsigned int sync_timeouts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	unsigned int no_time_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	unsigned int invalid_sync_windows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	unsigned int undersize_sync_windows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	unsigned int oversize_sync_windows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	unsigned int rx_no_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	struct efx_ptp_timeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	void (*xmit_skb)(struct efx_nic *efx, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) static int efx_phc_settime(struct ptp_clock_info *ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			   const struct timespec64 *e_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) static int efx_phc_enable(struct ptp_clock_info *ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			  struct ptp_clock_request *request, int on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) bool efx_ptp_use_mac_tx_timestamps(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	return efx_has_cap(efx, TX_MAC_TIMESTAMPING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) /* PTP 'extra' channel is still a traffic channel, but we only create TX queues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  * if PTP uses MAC TX timestamps, not if PTP uses the MC directly to transmit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static bool efx_ptp_want_txqs(struct efx_channel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	return efx_ptp_use_mac_tx_timestamps(channel->efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) #define PTP_SW_STAT(ext_name, field_name)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	{ #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) #define PTP_MC_STAT(ext_name, mcdi_name)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	{ #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	PTP_SW_STAT(ptp_good_syncs, good_syncs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	PTP_MC_STAT(ptp_timestamp_packets, TS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	PTP_MC_STAT(ptp_filter_matches, FM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	PTP_MC_STAT(ptp_non_filter_matches, NFM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) #define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) static const unsigned long efx_ptp_stat_mask[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	[0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (!efx->ptp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 				      efx_ptp_stat_mask, strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	if (!efx->ptp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	/* Copy software statistics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	for (i = 0; i < PTP_STAT_COUNT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		if (efx_ptp_stat_desc[i].dma_width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		stats[i] = *(unsigned int *)((char *)efx->ptp_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 					     efx_ptp_stat_desc[i].offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	/* Fetch MC statistics.  We *must* fill in all statistics or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	 * risk leaking kernel memory to userland, so if the MCDI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	 * request fails we pretend we got zeroes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			  outbuf, sizeof(outbuf), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		memset(outbuf, 0, sizeof(outbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			     efx_ptp_stat_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			     stats, _MCDI_PTR(outbuf, 0), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	return PTP_STAT_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) /* For Siena platforms NIC time is s and ns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	struct timespec64 ts = ns_to_timespec64(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	*nic_major = (u32)ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	*nic_minor = ts.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 						s32 correction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	ktime_t kt = ktime_set(nic_major, nic_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (correction >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		kt = ktime_add_ns(kt, (u64)correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		kt = ktime_sub_ns(kt, (u64)-correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	return kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) /* To convert from s27 format to ns we multiply then divide by a power of 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460)  * For the conversion from ns to s27, the operation is also converted to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461)  * multiply and shift.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) #define S27_TO_NS_SHIFT	(27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) #define NS_TO_S27_MULT	(((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) #define NS_TO_S27_SHIFT	(63 - S27_TO_NS_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) #define S27_MINOR_MAX	(1 << S27_TO_NS_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) /* For Huntington platforms NIC time is in seconds and fractions of a second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469)  * where the minor register only uses 27 bits in units of 2^-27s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	struct timespec64 ts = ns_to_timespec64(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	u32 maj = (u32)ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	/* The conversion can result in the minor value exceeding the maximum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	 * In this case, round up to the next second.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (min >= S27_MINOR_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		min -= S27_MINOR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		maj++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	*nic_major = maj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	*nic_minor = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			(1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	return ktime_set(nic_major, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 					       s32 correction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	/* Apply the correction and deal with carry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	nic_minor += correction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	if ((s32)nic_minor < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		nic_minor += S27_MINOR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		nic_major--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	} else if (nic_minor >= S27_MINOR_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		nic_minor -= S27_MINOR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		nic_major++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	return efx_ptp_s27_to_ktime(nic_major, nic_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) /* For Medford2 platforms the time is in seconds and quarter nanoseconds. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) static void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	struct timespec64 ts = ns_to_timespec64(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	*nic_major = (u32)ts.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	*nic_minor = ts.tv_nsec * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) static ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 						 s32 correction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	ktime_t kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	correction = DIV_ROUND_CLOSEST(correction, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	kt = ktime_set(nic_major, nic_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (correction >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		kt = ktime_add_ns(kt, (u64)correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		kt = ktime_sub_ns(kt, (u64)-correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	return kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) struct efx_channel *efx_ptp_channel(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	return efx->ptp_data ? efx->ptp_data->channel : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) static u32 last_sync_timestamp_major(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	struct efx_channel *channel = efx_ptp_channel(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	u32 major = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	if (channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		major = channel->sync_timestamp_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	return major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) /* The 8000 series and later can provide the time from the MAC, which is only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555)  * 48 bits long and provides meta-information in the top 2 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) static ktime_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 				    struct efx_ptp_data *ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 				    u32 nic_major, u32 nic_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 				    s32 correction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	u32 sync_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	ktime_t kt = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	s16 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (!(nic_major & 0x80000000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		WARN_ON_ONCE(nic_major >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		/* Medford provides 48 bits of timestamp, so we must get the top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		 * 16 bits from the timesync event state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		 * We only have the lower 16 bits of the time now, but we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		 * have a full resolution timestamp at some point in past. As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		 * long as the difference between the (real) now and the sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		 * is less than 2^15, then we can reconstruct the difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		 * between those two numbers using only the lower 16 bits of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		 * each.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		 * Put another way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		 * a - b = ((a mod k) - b) mod k
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		 * when -k/2 < (a-b) < k/2. In our case k is 2^16. We know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		 * (a mod k) and b, so can calculate the delta, a - b.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		sync_timestamp = last_sync_timestamp_major(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		/* Because delta is s16 this does an implicit mask down to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		 * 16 bits which is what we need, assuming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		 * MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		 * we can deal with the (unlikely) case of sync timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		 * arriving from the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		delta = nic_major - sync_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		/* Recover the fully specified time now, by applying the offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		 * to the (fully specified) sync time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		nic_major = sync_timestamp + delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 					     correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	return kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	struct efx_nic *efx = tx_queue->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	ktime_t kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (efx_ptp_use_mac_tx_timestamps(efx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				tx_queue->completed_timestamp_major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 				tx_queue->completed_timestamp_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 				ptp->ts_corrections.general_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		kt = ptp->nic_to_kernel_time(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 				tx_queue->completed_timestamp_major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 				tx_queue->completed_timestamp_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 				ptp->ts_corrections.general_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	return kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) /* Get PTP attributes and set up time conversions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) static int efx_ptp_get_attributes(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	u32 fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	size_t out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	/* Get the PTP attributes. If the NIC doesn't support the operation we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	 * use the default format for compatibility with older NICs i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	 * seconds and nanoseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 				outbuf, sizeof(outbuf), &out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	} else if (rc == -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	} else if (rc == -EPERM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		pci_info(efx->pci_dev, "no PTP support\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 				       outbuf, sizeof(outbuf), rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	switch (fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		ptp->nic_time.minor_max = 1 << 27;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		ptp->nic_time.sync_event_minor_shift = 19;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		ptp->nic_time.minor_max = 1000000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		ptp->nic_time.sync_event_minor_shift = 22;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		ptp->nic_time.minor_max = 4000000000UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		ptp->nic_time.sync_event_minor_shift = 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	/* Precalculate acceptable difference between the minor time in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	 * packet prefix and the last MCDI time sync event. We expect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	 * packet prefix timestamp to be after of sync event by up to one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	 * sync event interval (0.25s) but we allow it to exceed this by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	 * fuzz factor of (0.1s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		- (ptp->nic_time.minor_max / 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		+ (ptp->nic_time.minor_max / 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	/* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 * a value to use for the minimum acceptable corrected synchronization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	 * window and may return further capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	 * If we have the extra information store it. For older firmware that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	 * does not implement the extended command use the default value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (rc == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	    out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		ptp->min_synchronisation_ns =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			MCDI_DWORD(outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 				   PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (rc == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	    out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		ptp->capabilities = MCDI_DWORD(outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 					PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		ptp->capabilities = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	/* Set up the shift for conversion between frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	 * adjustments in parts-per-billion and the fixed-point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	 * fractional ns format that the adapter uses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) /* Get PTP timestamp corrections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	size_t out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	/* Get the timestamp corrections from the NIC. If this operation is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	 * not supported (older NICs) then no correction is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		       MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 				outbuf, sizeof(outbuf), &out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 			PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 				outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 				PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 				outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 				PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			efx->ptp_data->ts_corrections.general_tx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 				efx->ptp_data->ts_corrections.ptp_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			efx->ptp_data->ts_corrections.general_rx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 				efx->ptp_data->ts_corrections.ptp_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	} else if (rc == -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		efx->ptp_data->ts_corrections.ptp_tx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		efx->ptp_data->ts_corrections.ptp_rx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		efx->ptp_data->ts_corrections.pps_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		efx->ptp_data->ts_corrections.pps_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		efx->ptp_data->ts_corrections.general_tx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		efx->ptp_data->ts_corrections.general_rx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 				       sizeof(outbuf), rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /* Enable MCDI PTP support. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static int efx_ptp_enable(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	MCDI_DECLARE_BUF_ERR(outbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		       efx->ptp_data->channel ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		       efx->ptp_data->channel->channel : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 				outbuf, sizeof(outbuf), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	rc = (rc == -EALREADY) ? 0 : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		efx_mcdi_display_error(efx, MC_CMD_PTP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 				       MC_CMD_PTP_IN_ENABLE_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 				       outbuf, sizeof(outbuf), rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) /* Disable MCDI PTP support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * Note that this function should never rely on the presence of ptp_data -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  * may be called before that exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) static int efx_ptp_disable(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	MCDI_DECLARE_BUF_ERR(outbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 				outbuf, sizeof(outbuf), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	rc = (rc == -EALREADY) ? 0 : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	/* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	 * should only have been called during probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	if (rc == -ENOSYS || rc == -EPERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		pci_info(efx->pci_dev, "no PTP support\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		efx_mcdi_display_error(efx, MC_CMD_PTP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 				       MC_CMD_PTP_IN_DISABLE_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 				       outbuf, sizeof(outbuf), rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	while ((skb = skb_dequeue(q))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		netif_receive_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) static void efx_ptp_handle_no_channel(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	netif_err(efx, drv, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		  "ERROR: PTP requires MSI-X and 1 additional interrupt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		  "vector. PTP disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) /* Repeatedly send the host time to the MC which will capture the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854)  * time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) static void efx_ptp_send_times(struct efx_nic *efx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			       struct pps_event_time *last_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	struct pps_event_time now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	struct timespec64 limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	int *mc_running = ptp->start.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	pps_get_ts(&now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	limit = now.ts_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	/* Write host time for specified period or until MC is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	       READ_ONCE(*mc_running)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		struct timespec64 update_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		unsigned int host_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		/* Don't update continuously to avoid saturating the PCIe bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		update_time = now.ts_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			pps_get_ts(&now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		} while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			 READ_ONCE(*mc_running));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		/* Synchronise NIC with single word of time only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			     now.ts_real.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		/* Update host time in NIC memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		efx->type->ptp_write_host_time(efx, host_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	*last_time = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) /* Read a timeset from the MC's results and partial process. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 				 struct efx_ptp_timeset *timeset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	unsigned start_ns, end_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	/* Ignore seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	start_ns = timeset->host_start & MC_NANOSECOND_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	end_ns = timeset->host_end & MC_NANOSECOND_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	/* Allow for rollover */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (end_ns < start_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		end_ns += NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	/* Determine duration of operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	timeset->window = end_ns - start_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) /* Process times received from MC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * Extract times from returned results, and establish the minimum value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  * seen.  The minimum value represents the "best" possible time and events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  * too much greater than this are rejected - the machine is, perhaps, too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918)  * busy. A number of readings are taken so that, hopefully, at least one good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  * synchronisation will be seen in the results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		      size_t response_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		      const struct pps_event_time *last_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	unsigned number_readings =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		MCDI_VAR_ARRAY_LEN(response_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 				   PTP_OUT_SYNCHRONIZE_TIMESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	unsigned ngood = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	unsigned last_good = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	u32 last_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	u32 start_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	struct timespec64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	ktime_t mc_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	if (number_readings == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	/* Read the set of results and find the last good host-MC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	 * synchronization result. The MC times when it finishes reading the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	 * host time so the corrected window time should be fairly constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	 * for a given platform. Increment stats for any results that appear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	 * to be erroneous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	for (i = 0; i < number_readings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		s32 window, corrected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		struct timespec64 wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		efx_ptp_read_timeset(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			MCDI_ARRAY_STRUCT_PTR(synch_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 					      PTP_OUT_SYNCHRONIZE_TIMESET, i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			&ptp->timeset[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		wait = ktime_to_timespec64(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		window = ptp->timeset[i].window;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		corrected = window - wait.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		/* We expect the uncorrected synchronization window to be at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		 * least as large as the interval between host start and end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		 * times. If it is smaller than this then this is mostly likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		 * to be a consequence of the host's time being adjusted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		 * Check that the corrected sync window is in a reasonable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		 * range. If it is out of range it is likely to be because an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		 * interrupt or other delay occurred between reading the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		 * time and writing it to MC memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		if (window < SYNCHRONISATION_GRANULARITY_NS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			++ptp->invalid_sync_windows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		} else if (corrected >= MAX_SYNCHRONISATION_NS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			++ptp->oversize_sync_windows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		} else if (corrected < ptp->min_synchronisation_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			++ptp->undersize_sync_windows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			ngood++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			last_good = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (ngood == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		netif_warn(efx, drv, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			   "PTP no suitable synchronisations\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	/* Calculate delay from last good sync (host time) to last_time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 * It is possible that the seconds rolled over between taking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	 * the start reading and the last value written by the host.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	 * timescales are such that a gap of more than one second is never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	 * expected.  delta is *not* normalised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (start_sec != last_sec &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	    ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		netif_warn(efx, hw, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			   "PTP bad synchronisation seconds\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	delta.tv_sec = (last_sec - start_sec) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	delta.tv_nsec =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		last_time->ts_real.tv_nsec -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		(ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	/* Convert the NIC time at last good sync into kernel time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	 * No correction is required - this time is the output of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	 * firmware process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 					  ptp->timeset[last_good].minor, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	/* Calculate delay from NIC top of second to last_time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	/* Set PPS timestamp to match NIC top of second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	ptp->host_time_pps = *last_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	pps_sub_ts(&ptp->host_time_pps, delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) /* Synchronize times between the host and the MC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	size_t response_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	struct pps_event_time last_time = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	unsigned int loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	int *start = ptp->start.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		       num_readings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		       ptp->start.dma_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	/* Clear flag that signals MC ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	WRITE_ONCE(*start, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 				MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	EFX_WARN_ON_ONCE_PARANOID(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	/* Wait for start from MCDI (or timeout) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		udelay(20);	/* Usually start MCDI execution quickly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		loops++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (loops <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		++ptp->fast_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (!time_before(jiffies, timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		++ptp->sync_timeouts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (READ_ONCE(*start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		efx_ptp_send_times(efx, &last_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	/* Collect results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 				 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 				 synch_buf, sizeof(synch_buf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 				 &response_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		rc = efx_ptp_process_times(efx, synch_buf, response_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 					   &last_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			++ptp->good_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			++ptp->no_time_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	/* Increment the bad syncs counter if the synchronize fails, whatever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	 * the reason.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		++ptp->bad_syncs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) /* Transmit a PTP packet via the dedicated hardware timestamped queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	struct efx_ptp_data *ptp_data = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	u8 type = efx_tx_csum_type_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	struct efx_tx_queue *tx_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	tx_queue = efx_channel_get_tx_queue(ptp_data->channel, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if (tx_queue && tx_queue->timestamping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		efx_enqueue_skb(tx_queue, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) /* Transmit a PTP packet, via the MCDI interface, to the wire. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	struct efx_ptp_data *ptp_data = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	struct skb_shared_hwtstamps timestamps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	int rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (skb_shinfo(skb)->nr_frags != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		rc = skb_linearize(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		rc = skb_checksum_help(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	skb_copy_from_linear_data(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 				  MCDI_PTR(ptp_data->txbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 					   PTP_IN_TRANSMIT_PACKET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				  skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 			  ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 			  txtime, sizeof(txtime), &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	memset(&timestamps, 0, sizeof(timestamps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		ptp_data->ts_corrections.ptp_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	skb_tstamp_tx(skb, &timestamps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	struct list_head *cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct list_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	if (ptp->rx_ts_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	/* Drop time-expired events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	spin_lock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	list_for_each_safe(cursor, next, &ptp->evt_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		struct efx_ptp_event_rx *evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		evt = list_entry(cursor, struct efx_ptp_event_rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 				 link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		if (time_after(jiffies, evt->expiry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			list_move(&evt->link, &ptp->evt_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			netif_warn(efx, hw, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				   "PTP rx event dropped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	spin_unlock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 					      struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	bool evts_waiting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	struct list_head *cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	struct list_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	struct efx_ptp_match *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	WARN_ON_ONCE(ptp->rx_ts_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	spin_lock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	evts_waiting = !list_empty(&ptp->evt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	spin_unlock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	if (!evts_waiting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		return PTP_PACKET_STATE_UNMATCHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	match = (struct efx_ptp_match *)skb->cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/* Look for a matching timestamp in the event queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	spin_lock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	list_for_each_safe(cursor, next, &ptp->evt_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		struct efx_ptp_event_rx *evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		evt = list_entry(cursor, struct efx_ptp_event_rx, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		if ((evt->seq0 == match->words[0]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		    (evt->seq1 == match->words[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 			struct skb_shared_hwtstamps *timestamps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			/* Match - add in hardware timestamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			timestamps = skb_hwtstamps(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			timestamps->hwtstamp = evt->hwtimestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 			match->state = PTP_PACKET_STATE_MATCHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			rc = PTP_PACKET_STATE_MATCHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			list_move(&evt->link, &ptp->evt_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	spin_unlock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) /* Process any queued receive events and corresponding packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)  * q is returned with all the packets that are ready for delivery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	while ((skb = skb_dequeue(&ptp->rxq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		struct efx_ptp_match *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		match = (struct efx_ptp_match *)skb->cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			__skb_queue_tail(q, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		} else if (efx_ptp_match_rx(efx, skb) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			   PTP_PACKET_STATE_MATCHED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			__skb_queue_tail(q, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		} else if (time_after(jiffies, match->expiry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			match->state = PTP_PACKET_STATE_TIMED_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			++ptp->rx_no_timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			__skb_queue_tail(q, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			/* Replace unprocessed entry and stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			skb_queue_head(&ptp->rxq, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) /* Complete processing of a received packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	netif_receive_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	if (ptp->rxfilter_installed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 					  ptp->rxfilter_general);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 					  ptp->rxfilter_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		ptp->rxfilter_installed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	struct efx_filter_spec rxfilter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	if (!ptp->channel || ptp->rxfilter_installed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	/* Must filter on both event and general ports to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	 * that there is no packet re-ordering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 			   efx_rx_queue_index(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 				   efx_channel_get_rx_queue(ptp->channel)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 				       htonl(PTP_ADDRESS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				       htons(PTP_EVENT_PORT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	rc = efx_filter_insert_filter(efx, &rxfilter, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	ptp->rxfilter_event = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 			   efx_rx_queue_index(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 				   efx_channel_get_rx_queue(ptp->channel)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 				       htonl(PTP_ADDRESS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 				       htons(PTP_GENERAL_PORT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	rc = efx_filter_insert_filter(efx, &rxfilter, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	ptp->rxfilter_general = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	ptp->rxfilter_installed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 				  ptp->rxfilter_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) static int efx_ptp_start(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	ptp->reset_required = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	rc = efx_ptp_insert_multicast_filters(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	rc = efx_ptp_enable(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	ptp->evt_frag_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	ptp->current_adjfreq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	efx_ptp_remove_multicast_filters(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) static int efx_ptp_stop(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	struct list_head *cursor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	struct list_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (ptp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	rc = efx_ptp_disable(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	efx_ptp_remove_multicast_filters(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	/* Make sure RX packets are really delivered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	skb_queue_purge(&efx->ptp_data->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	/* Drop any pending receive events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	spin_lock_bh(&efx->ptp_data->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		list_move(cursor, &efx->ptp_data->evt_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	spin_unlock_bh(&efx->ptp_data->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) static int efx_ptp_restart(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	if (efx->ptp_data && efx->ptp_data->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		return efx_ptp_start(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) static void efx_ptp_pps_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	struct efx_ptp_data *ptp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		container_of(work, struct efx_ptp_data, pps_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	struct efx_nic *efx = ptp->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	struct ptp_clock_event ptp_evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	ptp_evt.type = PTP_CLOCK_PPSUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	ptp_evt.pps_times = ptp->host_time_pps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	ptp_clock_event(ptp->phc_clock, &ptp_evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) static void efx_ptp_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	struct efx_ptp_data *ptp_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		container_of(work, struct efx_ptp_data, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	struct efx_nic *efx = ptp_data->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	struct sk_buff_head tempq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (ptp_data->reset_required) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		efx_ptp_stop(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		efx_ptp_start(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	efx_ptp_drop_time_expired_events(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	__skb_queue_head_init(&tempq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	efx_ptp_process_events(efx, &tempq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	while ((skb = skb_dequeue(&ptp_data->txq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		ptp_data->xmit_skb(efx, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	while ((skb = __skb_dequeue(&tempq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		efx_ptp_process_rx(efx, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) static const struct ptp_clock_info efx_phc_clock_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	.name		= "sfc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	.max_adj	= MAX_PPB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	.n_alarm	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	.n_ext_ts	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	.n_per_out	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	.n_pins		= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	.pps		= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	.adjfreq	= efx_phc_adjfreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	.adjtime	= efx_phc_adjtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	.gettime64	= efx_phc_gettime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	.settime64	= efx_phc_settime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	.enable		= efx_phc_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) /* Initialise PTP state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	struct efx_ptp_data *ptp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	unsigned int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	efx->ptp_data = ptp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (!efx->ptp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	ptp->efx = efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	ptp->channel = channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	skb_queue_head_init(&ptp->rxq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	skb_queue_head_init(&ptp->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	ptp->workwq = create_singlethread_workqueue("sfc_ptp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	if (!ptp->workwq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (efx_ptp_use_mac_tx_timestamps(efx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		ptp->xmit_skb = efx_ptp_xmit_skb_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		/* Request sync events on this channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		ptp->xmit_skb = efx_ptp_xmit_skb_mc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	INIT_WORK(&ptp->work, efx_ptp_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	ptp->config.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	ptp->config.tx_type = HWTSTAMP_TX_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	INIT_LIST_HEAD(&ptp->evt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	INIT_LIST_HEAD(&ptp->evt_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	spin_lock_init(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	/* Get the NIC PTP attributes and set up time conversions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	rc = efx_ptp_get_attributes(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	/* Get the timestamp corrections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	rc = efx_ptp_get_timestamp_corrections(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (efx->mcdi->fn_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	    (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		ptp->phc_clock_info = efx_phc_clock_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 						    &efx->pci_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		if (IS_ERR(ptp->phc_clock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 			rc = PTR_ERR(ptp->phc_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 			goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		} else if (ptp->phc_clock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 			ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 			if (!ptp->pps_workwq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 				rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 				goto fail4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	ptp->nic_ts_enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) fail4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	ptp_clock_unregister(efx->ptp_data->phc_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) fail3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	destroy_workqueue(efx->ptp_data->workwq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) fail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	efx_nic_free_buffer(efx, &ptp->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) fail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	kfree(efx->ptp_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	efx->ptp_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) /* Initialise PTP channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533)  * Setting core_index to zero causes the queue to be initialised and doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)  * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) static int efx_ptp_probe_channel(struct efx_channel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	struct efx_nic *efx = channel->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	channel->irq_moderation_us = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	channel->rx_queue.core_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	rc = efx_ptp_probe(efx, channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	/* Failure to probe PTP is not fatal; this channel will just not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	 * used for anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	 * In the case of EPERM, efx_ptp_probe will print its own message (in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	 * efx_ptp_get_attributes()), so we don't need to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	if (rc && rc != -EPERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		netif_warn(efx, drv, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			   "Failed to probe PTP, rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) void efx_ptp_remove(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	if (!efx->ptp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	(void)efx_ptp_disable(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	cancel_work_sync(&efx->ptp_data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	if (efx->ptp_data->pps_workwq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		cancel_work_sync(&efx->ptp_data->pps_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	skb_queue_purge(&efx->ptp_data->rxq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	skb_queue_purge(&efx->ptp_data->txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	if (efx->ptp_data->phc_clock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		destroy_workqueue(efx->ptp_data->pps_workwq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		ptp_clock_unregister(efx->ptp_data->phc_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	destroy_workqueue(efx->ptp_data->workwq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	efx_nic_free_buffer(efx, &efx->ptp_data->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	kfree(efx->ptp_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	efx->ptp_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) static void efx_ptp_remove_channel(struct efx_channel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	efx_ptp_remove(channel->efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) static void efx_ptp_get_channel_name(struct efx_channel *channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 				     char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	snprintf(buf, len, "%s-ptp", channel->efx->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) /* Determine whether this packet should be processed by the PTP module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)  * or transmitted conventionally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	return efx->ptp_data &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		efx->ptp_data->enabled &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 		skb->len >= PTP_MIN_LENGTH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		likely(skb->protocol == htons(ETH_P_IP)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		skb_transport_header_was_set(skb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		skb_network_header_len(skb) >= sizeof(struct iphdr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		ip_hdr(skb)->protocol == IPPROTO_UDP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		skb_headlen(skb) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		skb_transport_offset(skb) + sizeof(struct udphdr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) /* Receive a PTP packet.  Packets are queued until the arrival of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)  * the receive timestamp from the MC - this will probably occur after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)  * packet arrival because of the processing in the MC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	struct efx_nic *efx = channel->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	u8 *match_data_012, *match_data_345;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	unsigned int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	/* Correct version? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	if (ptp->mode == MC_CMD_PTP_MODE_V1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		data = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		if (version != PTP_VERSION_V1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		/* PTP V1 uses all six bytes of the UUID to match the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		 * to the timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		match_data_012 = data + PTP_V1_UUID_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		data = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		version = data[PTP_V2_VERSION_OFFSET];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		/* The original V2 implementation uses bytes 2-7 of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		 * the UUID to match the packet to the timestamp. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		 * discards two of the bytes of the MAC address used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		 * to create the UUID (SF bug 33070).  The PTP V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		 * enhanced mode fixes this issue and uses bytes 0-2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		 * and byte 5-7 of the UUID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		if (ptp->mode == MC_CMD_PTP_MODE_V2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 			match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 			BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	/* Does this packet require timestamping? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		match->state = PTP_PACKET_STATE_UNMATCHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		/* We expect the sequence number to be in the same position in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		 * the packet for PTP V1 and V2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		/* Extract UUID/Sequence information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		match->words[0] = (match_data_012[0]         |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 				   (match_data_012[1] << 8)  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 				   (match_data_012[2] << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 				   (match_data_345[0] << 24));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		match->words[1] = (match_data_345[1]         |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 				   (match_data_345[2] << 8)  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 				   (data[PTP_V1_SEQUENCE_OFFSET +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 					 PTP_V1_SEQUENCE_LENGTH - 1] <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 				    16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	skb_queue_tail(&ptp->rxq, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	queue_work(ptp->workwq, &ptp->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) /* Transmit a PTP packet.  This has to be transmitted by the MC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)  * itself, through an MCDI call.  MCDI calls aren't permitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700)  * in the transmit path so defer the actual transmission to a suitable worker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	skb_queue_tail(&ptp->txq, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	    (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		efx_xmit_hwtstamp_pending(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	queue_work(ptp->workwq, &ptp->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) int efx_ptp_get_mode(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	return efx->ptp_data->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 			unsigned int new_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	if ((enable_wanted != efx->ptp_data->enabled) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	    (enable_wanted && (efx->ptp_data->mode != new_mode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		if (enable_wanted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 			/* Change of mode requires disable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 			if (efx->ptp_data->enabled &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 			    (efx->ptp_data->mode != new_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 				efx->ptp_data->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 				rc = efx_ptp_stop(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 				if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 					return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			/* Set new operating mode and establish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			 * baseline synchronisation, which must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			 * succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			efx->ptp_data->mode = new_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			if (netif_running(efx->net_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 				rc = efx_ptp_start(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 			if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 				rc = efx_ptp_synchronize(efx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 							 PTP_SYNC_ATTEMPTS * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 				if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 					efx_ptp_stop(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 			rc = efx_ptp_stop(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		efx->ptp_data->enabled = enable_wanted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	if (init->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	if ((init->tx_type != HWTSTAMP_TX_OFF) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	    (init->tx_type != HWTSTAMP_TX_ON))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	rc = efx->type->ptp_set_ts_config(efx, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	efx->ptp_data->config = *init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	struct efx_nic *primary = efx->primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	if (!ptp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 				     SOF_TIMESTAMPING_RX_HARDWARE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 				     SOF_TIMESTAMPING_RAW_HARDWARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	/* Check licensed features.  If we don't have the license for TX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	 * timestamps, the NIC will not support them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	if (efx_ptp_use_mac_tx_timestamps(efx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		struct efx_ef10_nic_data *nic_data = efx->nic_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		if (!(nic_data->licensed_features &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		      (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			ts_info->so_timestamping &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 				~SOF_TIMESTAMPING_TX_HARDWARE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		ts_info->phc_index =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			ptp_clock_index(primary->ptp_data->phc_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	struct hwtstamp_config config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	/* Not a PTP enabled port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	if (!efx->ptp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	rc = efx_ptp_ts_init(efx, &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	return copy_to_user(ifr->ifr_data, &config, sizeof(config))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	if (!efx->ptp_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 			    sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	netif_err(efx, hw, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		"PTP unexpected event length: got %d expected %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		ptp->evt_frag_idx, expected_frag_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	ptp->reset_required = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	queue_work(ptp->workwq, &ptp->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) /* Process a completed receive event.  Put it on the event queue and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855)  * start worker thread.  This is required because event and their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)  * correspoding packets may come in either order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	struct efx_ptp_event_rx *evt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	if (WARN_ON_ONCE(ptp->rx_ts_inline))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	if (ptp->evt_frag_idx != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		ptp_event_failure(efx, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	spin_lock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	if (!list_empty(&ptp->evt_free_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		evt = list_first_entry(&ptp->evt_free_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 				       struct efx_ptp_event_rx, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		list_del(&evt->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 					     MCDI_EVENT_SRC)        |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 			     (EFX_QWORD_FIELD(ptp->evt_frags[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 					      MCDI_EVENT_SRC) << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			     (EFX_QWORD_FIELD(ptp->evt_frags[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 					      MCDI_EVENT_SRC) << 16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 			EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 			EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			ptp->ts_corrections.ptp_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		list_add_tail(&evt->link, &ptp->evt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		queue_work(ptp->workwq, &ptp->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	} else if (net_ratelimit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		/* Log a rate-limited warning message. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	spin_unlock_bh(&ptp->evt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	if (ptp->evt_frag_idx != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		ptp_event_failure(efx, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	if (ptp->nic_ts_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		queue_work(ptp->pps_workwq, &ptp->pps_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	if (!ptp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		if (!efx->ptp_warned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 			netif_warn(efx, drv, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 				   "Received PTP event but PTP not set up\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 			efx->ptp_warned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	if (!ptp->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	if (ptp->evt_frag_idx == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		ptp->evt_code = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	} else if (ptp->evt_code != code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		netif_err(efx, hw, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			  "PTP out of sequence event %d\n", code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		ptp->evt_frag_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	if (!MCDI_EVENT_FIELD(*ev, CONT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		/* Process resulting event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		case MCDI_EVENT_CODE_PTP_RX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 			ptp_event_rx(efx, ptp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		case MCDI_EVENT_CODE_PTP_FAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			ptp_event_fault(efx, ptp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		case MCDI_EVENT_CODE_PTP_PPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 			ptp_event_pps(efx, ptp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 			netif_err(efx, hw, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 				  "PTP unknown event %d\n", code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		ptp->evt_frag_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	} else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		netif_err(efx, hw, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 			  "PTP too many event fragments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		ptp->evt_frag_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	struct efx_nic *efx = channel->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	/* When extracting the sync timestamp minor value, we should discard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	 * the least significant two bits. These are not required in order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	 * to reconstruct full-range timestamps and they are optionally used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	 * to report status depending on the options supplied when subscribing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	 * for sync events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	channel->sync_timestamp_minor =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		(MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 			<< ptp->nic_time.sync_event_minor_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	/* if sync events have been disabled then we want to silently ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	 * this event, so throw away result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	(void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		       SYNC_EVENTS_VALID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 	return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	const u8 *data = eh + efx->rx_packet_ts_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	return (u32)data[0]       |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	       (u32)data[1] << 8  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	       (u32)data[2] << 16 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	       (u32)data[3] << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 				   struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	struct efx_nic *efx = channel->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	struct efx_ptp_data *ptp = efx->ptp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	u32 pkt_timestamp_major, pkt_timestamp_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	u32 diff, carry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	struct skb_shared_hwtstamps *timestamps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	if (channel->sync_events_state != SYNC_EVENTS_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	/* get the difference between the packet and sync timestamps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	 * modulo one second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	if (pkt_timestamp_minor < channel->sync_timestamp_minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		diff += ptp->nic_time.minor_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	/* do we roll over a second boundary and need to carry the one? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 		1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	if (diff <= ptp->nic_time.sync_event_diff_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		/* packet is ahead of the sync event by a quarter of a second or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		 * less (allowing for fuzz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 		pkt_timestamp_major = channel->sync_timestamp_major + carry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	} else if (diff >= ptp->nic_time.sync_event_diff_min) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		/* packet is behind the sync event but within the fuzz factor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 		 * This means the RX packet and sync event crossed as they were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		 * placed on the event queue, which can sometimes happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		/* it's outside tolerance in both directions. this might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		 * indicative of us missing sync events for some reason, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		 * we'll call it an error rather than risk giving a bogus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		 * timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		netif_vdbg(efx, drv, efx->net_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			  "packet timestamp %x too far from sync event %x:%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			  pkt_timestamp_minor, channel->sync_timestamp_major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 			  channel->sync_timestamp_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	/* attach the timestamps to the skb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	timestamps = skb_hwtstamps(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	timestamps->hwtstamp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		ptp->nic_to_kernel_time(pkt_timestamp_major,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 					pkt_timestamp_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 					ptp->ts_corrections.general_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	struct efx_ptp_data *ptp_data = container_of(ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 						     struct efx_ptp_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 						     phc_clock_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	struct efx_nic *efx = ptp_data->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	s64 adjustment_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	if (delta > MAX_PPB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 		delta = MAX_PPB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	else if (delta < -MAX_PPB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 		delta = -MAX_PPB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	/* Convert ppb to fixed point ns taking care to round correctly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			 (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			ptp_data->adjfreq_ppb_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 			  NULL, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	ptp_data->current_adjfreq = adjustment_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	u32 nic_major, nic_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	struct efx_ptp_data *ptp_data = container_of(ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 						     struct efx_ptp_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 						     phc_clock_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	struct efx_nic *efx = ptp_data->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 			    NULL, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	struct efx_ptp_data *ptp_data = container_of(ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 						     struct efx_ptp_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 						     phc_clock_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	struct efx_nic *efx = ptp_data->efx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	ktime_t kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 			  outbuf, sizeof(outbuf), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	kt = ptp_data->nic_to_kernel_time(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	*ts = ktime_to_timespec64(kt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) static int efx_phc_settime(struct ptp_clock_info *ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 			   const struct timespec64 *e_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	/* Get the current NIC time, efx_phc_gettime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	 * Subtract from the desired time to get the offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	 * call efx_phc_adjtime with the offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	struct timespec64 time_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	struct timespec64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	rc = efx_phc_gettime(ptp, &time_now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	delta = timespec64_sub(*e_ts, time_now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) static int efx_phc_enable(struct ptp_clock_info *ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 			  struct ptp_clock_request *request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 			  int enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	struct efx_ptp_data *ptp_data = container_of(ptp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 						     struct efx_ptp_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 						     phc_clock_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	if (request->type != PTP_CLK_REQ_PPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	ptp_data->nic_ts_enabled = !!enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) static const struct efx_channel_type efx_ptp_channel_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	.handle_no_channel	= efx_ptp_handle_no_channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	.pre_probe		= efx_ptp_probe_channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	.post_remove		= efx_ptp_remove_channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	.get_name		= efx_ptp_get_channel_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	/* no copy operation; there is no need to reallocate this channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	.receive_skb		= efx_ptp_rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	.want_txqs		= efx_ptp_want_txqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	.keep_eventq		= false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	/* Check whether PTP is implemented on this NIC.  The DISABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	 * operation will succeed if and only if it is implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	if (efx_ptp_disable(efx) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 			&efx_ptp_channel_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) void efx_ptp_start_datapath(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	if (efx_ptp_restart(efx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	/* re-enable timestamping if it was previously enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	if (efx->type->ptp_set_ts_sync_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		efx->type->ptp_set_ts_sync_events(efx, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) void efx_ptp_stop_datapath(struct efx_nic *efx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	/* temporarily disable timestamping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	if (efx->type->ptp_set_ts_sync_events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 		efx->type->ptp_set_ts_sync_events(efx, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	efx_ptp_stop(efx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) }