162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/**************************************************************************** 362306a36Sopenharmony_ci * Driver for Solarflare network controllers and boards 462306a36Sopenharmony_ci * Copyright 2011-2013 Solarflare Communications Inc. 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci/* Theory of operation: 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * PTP support is assisted by firmware running on the MC, which provides 1062306a36Sopenharmony_ci * the hardware timestamping capabilities. Both transmitted and received 1162306a36Sopenharmony_ci * PTP event packets are queued onto internal queues for subsequent processing; 1262306a36Sopenharmony_ci * this is because the MC operations are relatively long and would block 1362306a36Sopenharmony_ci * block NAPI/interrupt operation. 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * Receive event processing: 1662306a36Sopenharmony_ci * The event contains the packet's UUID and sequence number, together 1762306a36Sopenharmony_ci * with the hardware timestamp. The PTP receive packet queue is searched 1862306a36Sopenharmony_ci * for this UUID/sequence number and, if found, put on a pending queue. 1962306a36Sopenharmony_ci * Packets not matching are delivered without timestamps (MCDI events will 2062306a36Sopenharmony_ci * always arrive after the actual packet). 2162306a36Sopenharmony_ci * It is important for the operation of the PTP protocol that the ordering 2262306a36Sopenharmony_ci * of packets between the event and general port is maintained. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * Work queue processing: 2562306a36Sopenharmony_ci * If work waiting, synchronise host/hardware time 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * Transmit: send packet through MC, which returns the transmission time 2862306a36Sopenharmony_ci * that is converted to an appropriate timestamp. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * Receive: the packet's reception time is converted to an appropriate 3162306a36Sopenharmony_ci * timestamp. 3262306a36Sopenharmony_ci */ 3362306a36Sopenharmony_ci#include <linux/ip.h> 3462306a36Sopenharmony_ci#include <linux/udp.h> 3562306a36Sopenharmony_ci#include <linux/time.h> 3662306a36Sopenharmony_ci#include <linux/ktime.h> 3762306a36Sopenharmony_ci#include <linux/module.h> 3862306a36Sopenharmony_ci#include <linux/pps_kernel.h> 3962306a36Sopenharmony_ci#include <linux/ptp_clock_kernel.h> 4062306a36Sopenharmony_ci#include "net_driver.h" 4162306a36Sopenharmony_ci#include "efx.h" 4262306a36Sopenharmony_ci#include "mcdi.h" 4362306a36Sopenharmony_ci#include "mcdi_pcol.h" 4462306a36Sopenharmony_ci#include "io.h" 4562306a36Sopenharmony_ci#include "farch_regs.h" 4662306a36Sopenharmony_ci#include "tx.h" 4762306a36Sopenharmony_ci#include "nic.h" /* indirectly includes ptp.h */ 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci/* Maximum number of events expected to make up a PTP event */ 5062306a36Sopenharmony_ci#define MAX_EVENT_FRAGS 3 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/* Maximum delay, ms, to begin synchronisation */ 5362306a36Sopenharmony_ci#define MAX_SYNCHRONISE_WAIT_MS 2 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/* How long, at most, to spend synchronising */ 5662306a36Sopenharmony_ci#define SYNCHRONISE_PERIOD_NS 250000 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* How often to update the shared memory time */ 5962306a36Sopenharmony_ci#define SYNCHRONISATION_GRANULARITY_NS 200 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci/* Minimum permitted length of a (corrected) synchronisation time */ 6262306a36Sopenharmony_ci#define DEFAULT_MIN_SYNCHRONISATION_NS 120 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci/* Maximum permitted length of a (corrected) synchronisation time */ 6562306a36Sopenharmony_ci#define MAX_SYNCHRONISATION_NS 1000 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci/* How many (MC) receive events that can be queued */ 6862306a36Sopenharmony_ci#define MAX_RECEIVE_EVENTS 8 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/* Length of (modified) moving average. */ 7162306a36Sopenharmony_ci#define AVERAGE_LENGTH 16 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci/* How long an unmatched event or packet can be held */ 7462306a36Sopenharmony_ci#define PKT_EVENT_LIFETIME_MS 10 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci/* Offsets into PTP packet for identification. These offsets are from the 7762306a36Sopenharmony_ci * start of the IP header, not the MAC header. Note that neither PTP V1 nor 7862306a36Sopenharmony_ci * PTP V2 permit the use of IPV4 options. 7962306a36Sopenharmony_ci */ 8062306a36Sopenharmony_ci#define PTP_DPORT_OFFSET 22 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci#define PTP_V1_VERSION_LENGTH 2 8362306a36Sopenharmony_ci#define PTP_V1_VERSION_OFFSET 28 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci#define PTP_V1_UUID_LENGTH 6 8662306a36Sopenharmony_ci#define PTP_V1_UUID_OFFSET 50 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci#define PTP_V1_SEQUENCE_LENGTH 2 8962306a36Sopenharmony_ci#define PTP_V1_SEQUENCE_OFFSET 58 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* The minimum length of a PTP V1 packet for offsets, etc. to be valid: 9262306a36Sopenharmony_ci * includes IP header. 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_ci#define PTP_V1_MIN_LENGTH 64 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci#define PTP_V2_VERSION_LENGTH 1 9762306a36Sopenharmony_ci#define PTP_V2_VERSION_OFFSET 29 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci#define PTP_V2_UUID_LENGTH 8 10062306a36Sopenharmony_ci#define PTP_V2_UUID_OFFSET 48 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci/* Although PTP V2 UUIDs are comprised a ClockIdentity (8) and PortNumber (2), 10362306a36Sopenharmony_ci * the MC only captures the last six bytes of the clock identity. These values 10462306a36Sopenharmony_ci * reflect those, not the ones used in the standard. The standard permits 10562306a36Sopenharmony_ci * mapping of V1 UUIDs to V2 UUIDs with these same values. 10662306a36Sopenharmony_ci */ 10762306a36Sopenharmony_ci#define PTP_V2_MC_UUID_LENGTH 6 10862306a36Sopenharmony_ci#define PTP_V2_MC_UUID_OFFSET 50 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci#define PTP_V2_SEQUENCE_LENGTH 2 11162306a36Sopenharmony_ci#define PTP_V2_SEQUENCE_OFFSET 58 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci/* The minimum length of a PTP V2 packet for offsets, etc. to be valid: 11462306a36Sopenharmony_ci * includes IP header. 11562306a36Sopenharmony_ci */ 11662306a36Sopenharmony_ci#define PTP_V2_MIN_LENGTH 63 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci#define PTP_MIN_LENGTH 63 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci#define PTP_ADDRESS 0xe0000181 /* 224.0.1.129 */ 12162306a36Sopenharmony_ci#define PTP_EVENT_PORT 319 12262306a36Sopenharmony_ci#define PTP_GENERAL_PORT 320 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci/* Annoyingly the format of the version numbers are different between 12562306a36Sopenharmony_ci * versions 1 and 2 so it isn't possible to simply look for 1 or 2. 12662306a36Sopenharmony_ci */ 12762306a36Sopenharmony_ci#define PTP_VERSION_V1 1 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci#define PTP_VERSION_V2 2 13062306a36Sopenharmony_ci#define PTP_VERSION_V2_MASK 0x0f 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_cienum ptp_packet_state { 13362306a36Sopenharmony_ci PTP_PACKET_STATE_UNMATCHED = 0, 13462306a36Sopenharmony_ci PTP_PACKET_STATE_MATCHED, 13562306a36Sopenharmony_ci PTP_PACKET_STATE_TIMED_OUT, 13662306a36Sopenharmony_ci PTP_PACKET_STATE_MATCH_UNWANTED 13762306a36Sopenharmony_ci}; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/* NIC synchronised with single word of time only comprising 14062306a36Sopenharmony_ci * partial seconds and full nanoseconds: 10^9 ~ 2^30 so 2 bits for seconds. 14162306a36Sopenharmony_ci */ 14262306a36Sopenharmony_ci#define MC_NANOSECOND_BITS 30 14362306a36Sopenharmony_ci#define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1) 14462306a36Sopenharmony_ci#define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1) 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci/* Maximum parts-per-billion adjustment that is acceptable */ 14762306a36Sopenharmony_ci#define MAX_PPB 1000000 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* Precalculate scale word to avoid long long division at runtime */ 15062306a36Sopenharmony_ci/* This is equivalent to 2^66 / 10^9. */ 15162306a36Sopenharmony_ci#define PPB_SCALE_WORD ((1LL << (57)) / 1953125LL) 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci/* How much to shift down after scaling to convert to FP40 */ 15462306a36Sopenharmony_ci#define PPB_SHIFT_FP40 26 15562306a36Sopenharmony_ci/* ... and FP44. */ 15662306a36Sopenharmony_ci#define PPB_SHIFT_FP44 22 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci#define PTP_SYNC_ATTEMPTS 4 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci/** 16162306a36Sopenharmony_ci * struct efx_ptp_match - Matching structure, stored in sk_buff's cb area. 16262306a36Sopenharmony_ci * @words: UUID and (partial) sequence number 16362306a36Sopenharmony_ci * @expiry: Time after which the packet should be delivered irrespective of 16462306a36Sopenharmony_ci * event arrival. 16562306a36Sopenharmony_ci * @state: The state of the packet - whether it is ready for processing or 16662306a36Sopenharmony_ci * whether that is of no interest. 16762306a36Sopenharmony_ci */ 16862306a36Sopenharmony_cistruct efx_ptp_match { 16962306a36Sopenharmony_ci u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)]; 17062306a36Sopenharmony_ci unsigned long expiry; 17162306a36Sopenharmony_ci enum ptp_packet_state state; 17262306a36Sopenharmony_ci}; 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci/** 17562306a36Sopenharmony_ci * struct efx_ptp_event_rx - A PTP receive event (from MC) 17662306a36Sopenharmony_ci * @link: list of events 17762306a36Sopenharmony_ci * @seq0: First part of (PTP) UUID 17862306a36Sopenharmony_ci * @seq1: Second part of (PTP) UUID and sequence number 17962306a36Sopenharmony_ci * @hwtimestamp: Event timestamp 18062306a36Sopenharmony_ci * @expiry: Time which the packet arrived 18162306a36Sopenharmony_ci */ 18262306a36Sopenharmony_cistruct efx_ptp_event_rx { 18362306a36Sopenharmony_ci struct list_head link; 18462306a36Sopenharmony_ci u32 seq0; 18562306a36Sopenharmony_ci u32 seq1; 18662306a36Sopenharmony_ci ktime_t hwtimestamp; 18762306a36Sopenharmony_ci unsigned long expiry; 18862306a36Sopenharmony_ci}; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci/** 19162306a36Sopenharmony_ci * struct efx_ptp_timeset - Synchronisation between host and MC 19262306a36Sopenharmony_ci * @host_start: Host time immediately before hardware timestamp taken 19362306a36Sopenharmony_ci * @major: Hardware timestamp, major 19462306a36Sopenharmony_ci * @minor: Hardware timestamp, minor 19562306a36Sopenharmony_ci * @host_end: Host time immediately after hardware timestamp taken 19662306a36Sopenharmony_ci * @wait: Number of NIC clock ticks between hardware timestamp being read and 19762306a36Sopenharmony_ci * host end time being seen 19862306a36Sopenharmony_ci * @window: Difference of host_end and host_start 19962306a36Sopenharmony_ci * @valid: Whether this timeset is valid 20062306a36Sopenharmony_ci */ 20162306a36Sopenharmony_cistruct efx_ptp_timeset { 20262306a36Sopenharmony_ci u32 host_start; 20362306a36Sopenharmony_ci u32 major; 20462306a36Sopenharmony_ci u32 minor; 20562306a36Sopenharmony_ci u32 host_end; 20662306a36Sopenharmony_ci u32 wait; 20762306a36Sopenharmony_ci u32 window; /* Derived: end - start, allowing for wrap */ 20862306a36Sopenharmony_ci}; 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci/** 21162306a36Sopenharmony_ci * struct efx_ptp_data - Precision Time Protocol (PTP) state 21262306a36Sopenharmony_ci * @efx: The NIC context 21362306a36Sopenharmony_ci * @channel: The PTP channel (Siena only) 21462306a36Sopenharmony_ci * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are 21562306a36Sopenharmony_ci * separate events) 21662306a36Sopenharmony_ci * @rxq: Receive SKB queue (awaiting timestamps) 21762306a36Sopenharmony_ci * @txq: Transmit SKB queue 21862306a36Sopenharmony_ci * @evt_list: List of MC receive events awaiting packets 21962306a36Sopenharmony_ci * @evt_free_list: List of free events 22062306a36Sopenharmony_ci * @evt_lock: Lock for manipulating evt_list and evt_free_list 22162306a36Sopenharmony_ci * @rx_evts: Instantiated events (on evt_list and evt_free_list) 22262306a36Sopenharmony_ci * @workwq: Work queue for processing pending PTP operations 22362306a36Sopenharmony_ci * @work: Work task 22462306a36Sopenharmony_ci * @reset_required: A serious error has occurred and the PTP task needs to be 22562306a36Sopenharmony_ci * reset (disable, enable). 22662306a36Sopenharmony_ci * @rxfilter_event: Receive filter when operating 22762306a36Sopenharmony_ci * @rxfilter_general: Receive filter when operating 22862306a36Sopenharmony_ci * @rxfilter_installed: Receive filter installed 22962306a36Sopenharmony_ci * @config: Current timestamp configuration 23062306a36Sopenharmony_ci * @enabled: PTP operation enabled 23162306a36Sopenharmony_ci * @mode: Mode in which PTP operating (PTP version) 23262306a36Sopenharmony_ci * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time 23362306a36Sopenharmony_ci * @nic_to_kernel_time: Function to convert from NIC to kernel time 23462306a36Sopenharmony_ci * @nic_time: contains time details 23562306a36Sopenharmony_ci * @nic_time.minor_max: Wrap point for NIC minor times 23662306a36Sopenharmony_ci * @nic_time.sync_event_diff_min: Minimum acceptable difference between time 23762306a36Sopenharmony_ci * in packet prefix and last MCDI time sync event i.e. how much earlier than 23862306a36Sopenharmony_ci * the last sync event time a packet timestamp can be. 23962306a36Sopenharmony_ci * @nic_time.sync_event_diff_max: Maximum acceptable difference between time 24062306a36Sopenharmony_ci * in packet prefix and last MCDI time sync event i.e. how much later than 24162306a36Sopenharmony_ci * the last sync event time a packet timestamp can be. 24262306a36Sopenharmony_ci * @nic_time.sync_event_minor_shift: Shift required to make minor time from 24362306a36Sopenharmony_ci * field in MCDI time sync event. 24462306a36Sopenharmony_ci * @min_synchronisation_ns: Minimum acceptable corrected sync window 24562306a36Sopenharmony_ci * @capabilities: Capabilities flags from the NIC 24662306a36Sopenharmony_ci * @ts_corrections: contains corrections details 24762306a36Sopenharmony_ci * @ts_corrections.ptp_tx: Required driver correction of PTP packet transmit 24862306a36Sopenharmony_ci * timestamps 24962306a36Sopenharmony_ci * @ts_corrections.ptp_rx: Required driver correction of PTP packet receive 25062306a36Sopenharmony_ci * timestamps 25162306a36Sopenharmony_ci * @ts_corrections.pps_out: PPS output error (information only) 25262306a36Sopenharmony_ci * @ts_corrections.pps_in: Required driver correction of PPS input timestamps 25362306a36Sopenharmony_ci * @ts_corrections.general_tx: Required driver correction of general packet 25462306a36Sopenharmony_ci * transmit timestamps 25562306a36Sopenharmony_ci * @ts_corrections.general_rx: Required driver correction of general packet 25662306a36Sopenharmony_ci * receive timestamps 25762306a36Sopenharmony_ci * @evt_frags: Partly assembled PTP events 25862306a36Sopenharmony_ci * @evt_frag_idx: Current fragment number 25962306a36Sopenharmony_ci * @evt_code: Last event code 26062306a36Sopenharmony_ci * @start: Address at which MC indicates ready for synchronisation 26162306a36Sopenharmony_ci * @host_time_pps: Host time at last PPS 26262306a36Sopenharmony_ci * @adjfreq_ppb_shift: Shift required to convert scaled parts-per-billion 26362306a36Sopenharmony_ci * frequency adjustment into a fixed point fractional nanosecond format. 26462306a36Sopenharmony_ci * @current_adjfreq: Current ppb adjustment. 26562306a36Sopenharmony_ci * @phc_clock: Pointer to registered phc device (if primary function) 26662306a36Sopenharmony_ci * @phc_clock_info: Registration structure for phc device 26762306a36Sopenharmony_ci * @pps_work: pps work task for handling pps events 26862306a36Sopenharmony_ci * @pps_workwq: pps work queue 26962306a36Sopenharmony_ci * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled 27062306a36Sopenharmony_ci * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids 27162306a36Sopenharmony_ci * allocations in main data path). 27262306a36Sopenharmony_ci * @good_syncs: Number of successful synchronisations. 27362306a36Sopenharmony_ci * @fast_syncs: Number of synchronisations requiring short delay 27462306a36Sopenharmony_ci * @bad_syncs: Number of failed synchronisations. 27562306a36Sopenharmony_ci * @sync_timeouts: Number of synchronisation timeouts 27662306a36Sopenharmony_ci * @no_time_syncs: Number of synchronisations with no good times. 27762306a36Sopenharmony_ci * @invalid_sync_windows: Number of sync windows with bad durations. 27862306a36Sopenharmony_ci * @undersize_sync_windows: Number of corrected sync windows that are too small 27962306a36Sopenharmony_ci * @oversize_sync_windows: Number of corrected sync windows that are too large 28062306a36Sopenharmony_ci * @rx_no_timestamp: Number of packets received without a timestamp. 28162306a36Sopenharmony_ci * @timeset: Last set of synchronisation statistics. 28262306a36Sopenharmony_ci * @xmit_skb: Transmit SKB function. 28362306a36Sopenharmony_ci */ 28462306a36Sopenharmony_cistruct efx_ptp_data { 28562306a36Sopenharmony_ci struct efx_nic *efx; 28662306a36Sopenharmony_ci struct efx_channel *channel; 28762306a36Sopenharmony_ci bool rx_ts_inline; 28862306a36Sopenharmony_ci struct sk_buff_head rxq; 28962306a36Sopenharmony_ci struct sk_buff_head txq; 29062306a36Sopenharmony_ci struct list_head evt_list; 29162306a36Sopenharmony_ci struct list_head evt_free_list; 29262306a36Sopenharmony_ci spinlock_t evt_lock; 29362306a36Sopenharmony_ci struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS]; 29462306a36Sopenharmony_ci struct workqueue_struct *workwq; 29562306a36Sopenharmony_ci struct work_struct work; 29662306a36Sopenharmony_ci bool reset_required; 29762306a36Sopenharmony_ci u32 rxfilter_event; 29862306a36Sopenharmony_ci u32 rxfilter_general; 29962306a36Sopenharmony_ci bool rxfilter_installed; 30062306a36Sopenharmony_ci struct hwtstamp_config config; 30162306a36Sopenharmony_ci bool enabled; 30262306a36Sopenharmony_ci unsigned int mode; 30362306a36Sopenharmony_ci void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor); 30462306a36Sopenharmony_ci ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor, 30562306a36Sopenharmony_ci s32 correction); 30662306a36Sopenharmony_ci struct { 30762306a36Sopenharmony_ci u32 minor_max; 30862306a36Sopenharmony_ci u32 sync_event_diff_min; 30962306a36Sopenharmony_ci u32 sync_event_diff_max; 31062306a36Sopenharmony_ci unsigned int sync_event_minor_shift; 31162306a36Sopenharmony_ci } nic_time; 31262306a36Sopenharmony_ci unsigned int min_synchronisation_ns; 31362306a36Sopenharmony_ci unsigned int capabilities; 31462306a36Sopenharmony_ci struct { 31562306a36Sopenharmony_ci s32 ptp_tx; 31662306a36Sopenharmony_ci s32 ptp_rx; 31762306a36Sopenharmony_ci s32 pps_out; 31862306a36Sopenharmony_ci s32 pps_in; 31962306a36Sopenharmony_ci s32 general_tx; 32062306a36Sopenharmony_ci s32 general_rx; 32162306a36Sopenharmony_ci } ts_corrections; 32262306a36Sopenharmony_ci efx_qword_t evt_frags[MAX_EVENT_FRAGS]; 32362306a36Sopenharmony_ci int evt_frag_idx; 32462306a36Sopenharmony_ci int evt_code; 32562306a36Sopenharmony_ci struct efx_buffer start; 32662306a36Sopenharmony_ci struct pps_event_time host_time_pps; 32762306a36Sopenharmony_ci unsigned int adjfreq_ppb_shift; 32862306a36Sopenharmony_ci s64 current_adjfreq; 32962306a36Sopenharmony_ci struct ptp_clock *phc_clock; 33062306a36Sopenharmony_ci struct ptp_clock_info phc_clock_info; 33162306a36Sopenharmony_ci struct work_struct pps_work; 33262306a36Sopenharmony_ci struct workqueue_struct *pps_workwq; 33362306a36Sopenharmony_ci bool nic_ts_enabled; 33462306a36Sopenharmony_ci efx_dword_t txbuf[MCDI_TX_BUF_LEN(MC_CMD_PTP_IN_TRANSMIT_LENMAX)]; 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ci unsigned int good_syncs; 33762306a36Sopenharmony_ci unsigned int fast_syncs; 33862306a36Sopenharmony_ci unsigned int bad_syncs; 33962306a36Sopenharmony_ci unsigned int sync_timeouts; 34062306a36Sopenharmony_ci unsigned int no_time_syncs; 34162306a36Sopenharmony_ci unsigned int invalid_sync_windows; 34262306a36Sopenharmony_ci unsigned int undersize_sync_windows; 34362306a36Sopenharmony_ci unsigned int oversize_sync_windows; 34462306a36Sopenharmony_ci unsigned int rx_no_timestamp; 34562306a36Sopenharmony_ci struct efx_ptp_timeset 34662306a36Sopenharmony_ci timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM]; 34762306a36Sopenharmony_ci void (*xmit_skb)(struct efx_nic *efx, struct sk_buff *skb); 34862306a36Sopenharmony_ci}; 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_cistatic int efx_phc_adjfine(struct ptp_clock_info *ptp, long scaled_ppm); 35162306a36Sopenharmony_cistatic int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta); 35262306a36Sopenharmony_cistatic int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts); 35362306a36Sopenharmony_cistatic int efx_phc_settime(struct ptp_clock_info *ptp, 35462306a36Sopenharmony_ci const struct timespec64 *e_ts); 35562306a36Sopenharmony_cistatic int efx_phc_enable(struct ptp_clock_info *ptp, 35662306a36Sopenharmony_ci struct ptp_clock_request *request, int on); 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_cibool efx_siena_ptp_use_mac_tx_timestamps(struct efx_nic *efx) 35962306a36Sopenharmony_ci{ 36062306a36Sopenharmony_ci return efx_has_cap(efx, TX_MAC_TIMESTAMPING); 36162306a36Sopenharmony_ci} 36262306a36Sopenharmony_ci 36362306a36Sopenharmony_ci/* PTP 'extra' channel is still a traffic channel, but we only create TX queues 36462306a36Sopenharmony_ci * if PTP uses MAC TX timestamps, not if PTP uses the MC directly to transmit. 36562306a36Sopenharmony_ci */ 36662306a36Sopenharmony_cistatic bool efx_ptp_want_txqs(struct efx_channel *channel) 36762306a36Sopenharmony_ci{ 36862306a36Sopenharmony_ci return efx_siena_ptp_use_mac_tx_timestamps(channel->efx); 36962306a36Sopenharmony_ci} 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci#define PTP_SW_STAT(ext_name, field_name) \ 37262306a36Sopenharmony_ci { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) } 37362306a36Sopenharmony_ci#define PTP_MC_STAT(ext_name, mcdi_name) \ 37462306a36Sopenharmony_ci { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST } 37562306a36Sopenharmony_cistatic const struct efx_hw_stat_desc efx_ptp_stat_desc[] = { 37662306a36Sopenharmony_ci PTP_SW_STAT(ptp_good_syncs, good_syncs), 37762306a36Sopenharmony_ci PTP_SW_STAT(ptp_fast_syncs, fast_syncs), 37862306a36Sopenharmony_ci PTP_SW_STAT(ptp_bad_syncs, bad_syncs), 37962306a36Sopenharmony_ci PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts), 38062306a36Sopenharmony_ci PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs), 38162306a36Sopenharmony_ci PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows), 38262306a36Sopenharmony_ci PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows), 38362306a36Sopenharmony_ci PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows), 38462306a36Sopenharmony_ci PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp), 38562306a36Sopenharmony_ci PTP_MC_STAT(ptp_tx_timestamp_packets, TX), 38662306a36Sopenharmony_ci PTP_MC_STAT(ptp_rx_timestamp_packets, RX), 38762306a36Sopenharmony_ci PTP_MC_STAT(ptp_timestamp_packets, TS), 38862306a36Sopenharmony_ci PTP_MC_STAT(ptp_filter_matches, FM), 38962306a36Sopenharmony_ci PTP_MC_STAT(ptp_non_filter_matches, NFM), 39062306a36Sopenharmony_ci}; 39162306a36Sopenharmony_ci#define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc) 39262306a36Sopenharmony_cistatic const unsigned long efx_ptp_stat_mask[] = { 39362306a36Sopenharmony_ci [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL, 39462306a36Sopenharmony_ci}; 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_cisize_t efx_siena_ptp_describe_stats(struct efx_nic *efx, u8 *strings) 39762306a36Sopenharmony_ci{ 39862306a36Sopenharmony_ci if (!efx->ptp_data) 39962306a36Sopenharmony_ci return 0; 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci return efx_siena_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT, 40262306a36Sopenharmony_ci efx_ptp_stat_mask, strings); 40362306a36Sopenharmony_ci} 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_cisize_t efx_siena_ptp_update_stats(struct efx_nic *efx, u64 *stats) 40662306a36Sopenharmony_ci{ 40762306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN); 40862306a36Sopenharmony_ci MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN); 40962306a36Sopenharmony_ci size_t i; 41062306a36Sopenharmony_ci int rc; 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci if (!efx->ptp_data) 41362306a36Sopenharmony_ci return 0; 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ci /* Copy software statistics */ 41662306a36Sopenharmony_ci for (i = 0; i < PTP_STAT_COUNT; i++) { 41762306a36Sopenharmony_ci if (efx_ptp_stat_desc[i].dma_width) 41862306a36Sopenharmony_ci continue; 41962306a36Sopenharmony_ci stats[i] = *(unsigned int *)((char *)efx->ptp_data + 42062306a36Sopenharmony_ci efx_ptp_stat_desc[i].offset); 42162306a36Sopenharmony_ci } 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci /* Fetch MC statistics. We *must* fill in all statistics or 42462306a36Sopenharmony_ci * risk leaking kernel memory to userland, so if the MCDI 42562306a36Sopenharmony_ci * request fails we pretend we got zeroes. 42662306a36Sopenharmony_ci */ 42762306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS); 42862306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 42962306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 43062306a36Sopenharmony_ci outbuf, sizeof(outbuf), NULL); 43162306a36Sopenharmony_ci if (rc) 43262306a36Sopenharmony_ci memset(outbuf, 0, sizeof(outbuf)); 43362306a36Sopenharmony_ci efx_siena_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT, 43462306a36Sopenharmony_ci efx_ptp_stat_mask, 43562306a36Sopenharmony_ci stats, _MCDI_PTR(outbuf, 0), false); 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci return PTP_STAT_COUNT; 43862306a36Sopenharmony_ci} 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci/* For Siena platforms NIC time is s and ns */ 44162306a36Sopenharmony_cistatic void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor) 44262306a36Sopenharmony_ci{ 44362306a36Sopenharmony_ci struct timespec64 ts = ns_to_timespec64(ns); 44462306a36Sopenharmony_ci *nic_major = (u32)ts.tv_sec; 44562306a36Sopenharmony_ci *nic_minor = ts.tv_nsec; 44662306a36Sopenharmony_ci} 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_cistatic ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor, 44962306a36Sopenharmony_ci s32 correction) 45062306a36Sopenharmony_ci{ 45162306a36Sopenharmony_ci ktime_t kt = ktime_set(nic_major, nic_minor); 45262306a36Sopenharmony_ci if (correction >= 0) 45362306a36Sopenharmony_ci kt = ktime_add_ns(kt, (u64)correction); 45462306a36Sopenharmony_ci else 45562306a36Sopenharmony_ci kt = ktime_sub_ns(kt, (u64)-correction); 45662306a36Sopenharmony_ci return kt; 45762306a36Sopenharmony_ci} 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci/* To convert from s27 format to ns we multiply then divide by a power of 2. 46062306a36Sopenharmony_ci * For the conversion from ns to s27, the operation is also converted to a 46162306a36Sopenharmony_ci * multiply and shift. 46262306a36Sopenharmony_ci */ 46362306a36Sopenharmony_ci#define S27_TO_NS_SHIFT (27) 46462306a36Sopenharmony_ci#define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC) 46562306a36Sopenharmony_ci#define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT) 46662306a36Sopenharmony_ci#define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT) 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci/* For Huntington platforms NIC time is in seconds and fractions of a second 46962306a36Sopenharmony_ci * where the minor register only uses 27 bits in units of 2^-27s. 47062306a36Sopenharmony_ci */ 47162306a36Sopenharmony_cistatic void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor) 47262306a36Sopenharmony_ci{ 47362306a36Sopenharmony_ci struct timespec64 ts = ns_to_timespec64(ns); 47462306a36Sopenharmony_ci u32 maj = (u32)ts.tv_sec; 47562306a36Sopenharmony_ci u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT + 47662306a36Sopenharmony_ci (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT); 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci /* The conversion can result in the minor value exceeding the maximum. 47962306a36Sopenharmony_ci * In this case, round up to the next second. 48062306a36Sopenharmony_ci */ 48162306a36Sopenharmony_ci if (min >= S27_MINOR_MAX) { 48262306a36Sopenharmony_ci min -= S27_MINOR_MAX; 48362306a36Sopenharmony_ci maj++; 48462306a36Sopenharmony_ci } 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci *nic_major = maj; 48762306a36Sopenharmony_ci *nic_minor = min; 48862306a36Sopenharmony_ci} 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_cistatic inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor) 49162306a36Sopenharmony_ci{ 49262306a36Sopenharmony_ci u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC + 49362306a36Sopenharmony_ci (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT); 49462306a36Sopenharmony_ci return ktime_set(nic_major, ns); 49562306a36Sopenharmony_ci} 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_cistatic ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor, 49862306a36Sopenharmony_ci s32 correction) 49962306a36Sopenharmony_ci{ 50062306a36Sopenharmony_ci /* Apply the correction and deal with carry */ 50162306a36Sopenharmony_ci nic_minor += correction; 50262306a36Sopenharmony_ci if ((s32)nic_minor < 0) { 50362306a36Sopenharmony_ci nic_minor += S27_MINOR_MAX; 50462306a36Sopenharmony_ci nic_major--; 50562306a36Sopenharmony_ci } else if (nic_minor >= S27_MINOR_MAX) { 50662306a36Sopenharmony_ci nic_minor -= S27_MINOR_MAX; 50762306a36Sopenharmony_ci nic_major++; 50862306a36Sopenharmony_ci } 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci return efx_ptp_s27_to_ktime(nic_major, nic_minor); 51162306a36Sopenharmony_ci} 51262306a36Sopenharmony_ci 51362306a36Sopenharmony_ci/* For Medford2 platforms the time is in seconds and quarter nanoseconds. */ 51462306a36Sopenharmony_cistatic void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor) 51562306a36Sopenharmony_ci{ 51662306a36Sopenharmony_ci struct timespec64 ts = ns_to_timespec64(ns); 51762306a36Sopenharmony_ci 51862306a36Sopenharmony_ci *nic_major = (u32)ts.tv_sec; 51962306a36Sopenharmony_ci *nic_minor = ts.tv_nsec * 4; 52062306a36Sopenharmony_ci} 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_cistatic ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor, 52362306a36Sopenharmony_ci s32 correction) 52462306a36Sopenharmony_ci{ 52562306a36Sopenharmony_ci ktime_t kt; 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4); 52862306a36Sopenharmony_ci correction = DIV_ROUND_CLOSEST(correction, 4); 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci kt = ktime_set(nic_major, nic_minor); 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_ci if (correction >= 0) 53362306a36Sopenharmony_ci kt = ktime_add_ns(kt, (u64)correction); 53462306a36Sopenharmony_ci else 53562306a36Sopenharmony_ci kt = ktime_sub_ns(kt, (u64)-correction); 53662306a36Sopenharmony_ci return kt; 53762306a36Sopenharmony_ci} 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_cistruct efx_channel *efx_siena_ptp_channel(struct efx_nic *efx) 54062306a36Sopenharmony_ci{ 54162306a36Sopenharmony_ci return efx->ptp_data ? efx->ptp_data->channel : NULL; 54262306a36Sopenharmony_ci} 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_cistatic u32 last_sync_timestamp_major(struct efx_nic *efx) 54562306a36Sopenharmony_ci{ 54662306a36Sopenharmony_ci struct efx_channel *channel = efx_siena_ptp_channel(efx); 54762306a36Sopenharmony_ci u32 major = 0; 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci if (channel) 55062306a36Sopenharmony_ci major = channel->sync_timestamp_major; 55162306a36Sopenharmony_ci return major; 55262306a36Sopenharmony_ci} 55362306a36Sopenharmony_ci 55462306a36Sopenharmony_ci/* The 8000 series and later can provide the time from the MAC, which is only 55562306a36Sopenharmony_ci * 48 bits long and provides meta-information in the top 2 bits. 55662306a36Sopenharmony_ci */ 55762306a36Sopenharmony_cistatic ktime_t 55862306a36Sopenharmony_ciefx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx, 55962306a36Sopenharmony_ci struct efx_ptp_data *ptp, 56062306a36Sopenharmony_ci u32 nic_major, u32 nic_minor, 56162306a36Sopenharmony_ci s32 correction) 56262306a36Sopenharmony_ci{ 56362306a36Sopenharmony_ci u32 sync_timestamp; 56462306a36Sopenharmony_ci ktime_t kt = { 0 }; 56562306a36Sopenharmony_ci s16 delta; 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_ci if (!(nic_major & 0x80000000)) { 56862306a36Sopenharmony_ci WARN_ON_ONCE(nic_major >> 16); 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci /* Medford provides 48 bits of timestamp, so we must get the top 57162306a36Sopenharmony_ci * 16 bits from the timesync event state. 57262306a36Sopenharmony_ci * 57362306a36Sopenharmony_ci * We only have the lower 16 bits of the time now, but we do 57462306a36Sopenharmony_ci * have a full resolution timestamp at some point in past. As 57562306a36Sopenharmony_ci * long as the difference between the (real) now and the sync 57662306a36Sopenharmony_ci * is less than 2^15, then we can reconstruct the difference 57762306a36Sopenharmony_ci * between those two numbers using only the lower 16 bits of 57862306a36Sopenharmony_ci * each. 57962306a36Sopenharmony_ci * 58062306a36Sopenharmony_ci * Put another way 58162306a36Sopenharmony_ci * 58262306a36Sopenharmony_ci * a - b = ((a mod k) - b) mod k 58362306a36Sopenharmony_ci * 58462306a36Sopenharmony_ci * when -k/2 < (a-b) < k/2. In our case k is 2^16. We know 58562306a36Sopenharmony_ci * (a mod k) and b, so can calculate the delta, a - b. 58662306a36Sopenharmony_ci * 58762306a36Sopenharmony_ci */ 58862306a36Sopenharmony_ci sync_timestamp = last_sync_timestamp_major(efx); 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci /* Because delta is s16 this does an implicit mask down to 59162306a36Sopenharmony_ci * 16 bits which is what we need, assuming 59262306a36Sopenharmony_ci * MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that 59362306a36Sopenharmony_ci * we can deal with the (unlikely) case of sync timestamps 59462306a36Sopenharmony_ci * arriving from the future. 59562306a36Sopenharmony_ci */ 59662306a36Sopenharmony_ci delta = nic_major - sync_timestamp; 59762306a36Sopenharmony_ci 59862306a36Sopenharmony_ci /* Recover the fully specified time now, by applying the offset 59962306a36Sopenharmony_ci * to the (fully specified) sync time. 60062306a36Sopenharmony_ci */ 60162306a36Sopenharmony_ci nic_major = sync_timestamp + delta; 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci kt = ptp->nic_to_kernel_time(nic_major, nic_minor, 60462306a36Sopenharmony_ci correction); 60562306a36Sopenharmony_ci } 60662306a36Sopenharmony_ci return kt; 60762306a36Sopenharmony_ci} 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_ciktime_t efx_siena_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue) 61062306a36Sopenharmony_ci{ 61162306a36Sopenharmony_ci struct efx_nic *efx = tx_queue->efx; 61262306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 61362306a36Sopenharmony_ci ktime_t kt; 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_ci if (efx_siena_ptp_use_mac_tx_timestamps(efx)) 61662306a36Sopenharmony_ci kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp, 61762306a36Sopenharmony_ci tx_queue->completed_timestamp_major, 61862306a36Sopenharmony_ci tx_queue->completed_timestamp_minor, 61962306a36Sopenharmony_ci ptp->ts_corrections.general_tx); 62062306a36Sopenharmony_ci else 62162306a36Sopenharmony_ci kt = ptp->nic_to_kernel_time( 62262306a36Sopenharmony_ci tx_queue->completed_timestamp_major, 62362306a36Sopenharmony_ci tx_queue->completed_timestamp_minor, 62462306a36Sopenharmony_ci ptp->ts_corrections.general_tx); 62562306a36Sopenharmony_ci return kt; 62662306a36Sopenharmony_ci} 62762306a36Sopenharmony_ci 62862306a36Sopenharmony_ci/* Get PTP attributes and set up time conversions */ 62962306a36Sopenharmony_cistatic int efx_ptp_get_attributes(struct efx_nic *efx) 63062306a36Sopenharmony_ci{ 63162306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN); 63262306a36Sopenharmony_ci MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN); 63362306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 63462306a36Sopenharmony_ci int rc; 63562306a36Sopenharmony_ci u32 fmt; 63662306a36Sopenharmony_ci size_t out_len; 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci /* Get the PTP attributes. If the NIC doesn't support the operation we 63962306a36Sopenharmony_ci * use the default format for compatibility with older NICs i.e. 64062306a36Sopenharmony_ci * seconds and nanoseconds. 64162306a36Sopenharmony_ci */ 64262306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES); 64362306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 64462306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 64562306a36Sopenharmony_ci outbuf, sizeof(outbuf), &out_len); 64662306a36Sopenharmony_ci if (rc == 0) { 64762306a36Sopenharmony_ci fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT); 64862306a36Sopenharmony_ci } else if (rc == -EINVAL) { 64962306a36Sopenharmony_ci fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS; 65062306a36Sopenharmony_ci } else if (rc == -EPERM) { 65162306a36Sopenharmony_ci pci_info(efx->pci_dev, "no PTP support\n"); 65262306a36Sopenharmony_ci return rc; 65362306a36Sopenharmony_ci } else { 65462306a36Sopenharmony_ci efx_siena_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), 65562306a36Sopenharmony_ci outbuf, sizeof(outbuf), rc); 65662306a36Sopenharmony_ci return rc; 65762306a36Sopenharmony_ci } 65862306a36Sopenharmony_ci 65962306a36Sopenharmony_ci switch (fmt) { 66062306a36Sopenharmony_ci case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION: 66162306a36Sopenharmony_ci ptp->ns_to_nic_time = efx_ptp_ns_to_s27; 66262306a36Sopenharmony_ci ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction; 66362306a36Sopenharmony_ci ptp->nic_time.minor_max = 1 << 27; 66462306a36Sopenharmony_ci ptp->nic_time.sync_event_minor_shift = 19; 66562306a36Sopenharmony_ci break; 66662306a36Sopenharmony_ci case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS: 66762306a36Sopenharmony_ci ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns; 66862306a36Sopenharmony_ci ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction; 66962306a36Sopenharmony_ci ptp->nic_time.minor_max = 1000000000; 67062306a36Sopenharmony_ci ptp->nic_time.sync_event_minor_shift = 22; 67162306a36Sopenharmony_ci break; 67262306a36Sopenharmony_ci case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS: 67362306a36Sopenharmony_ci ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns; 67462306a36Sopenharmony_ci ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction; 67562306a36Sopenharmony_ci ptp->nic_time.minor_max = 4000000000UL; 67662306a36Sopenharmony_ci ptp->nic_time.sync_event_minor_shift = 24; 67762306a36Sopenharmony_ci break; 67862306a36Sopenharmony_ci default: 67962306a36Sopenharmony_ci return -ERANGE; 68062306a36Sopenharmony_ci } 68162306a36Sopenharmony_ci 68262306a36Sopenharmony_ci /* Precalculate acceptable difference between the minor time in the 68362306a36Sopenharmony_ci * packet prefix and the last MCDI time sync event. We expect the 68462306a36Sopenharmony_ci * packet prefix timestamp to be after of sync event by up to one 68562306a36Sopenharmony_ci * sync event interval (0.25s) but we allow it to exceed this by a 68662306a36Sopenharmony_ci * fuzz factor of (0.1s) 68762306a36Sopenharmony_ci */ 68862306a36Sopenharmony_ci ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max 68962306a36Sopenharmony_ci - (ptp->nic_time.minor_max / 10); 69062306a36Sopenharmony_ci ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4) 69162306a36Sopenharmony_ci + (ptp->nic_time.minor_max / 10); 69262306a36Sopenharmony_ci 69362306a36Sopenharmony_ci /* MC_CMD_PTP_OP_GET_ATTRIBUTES has been extended twice from an older 69462306a36Sopenharmony_ci * operation MC_CMD_PTP_OP_GET_TIME_FORMAT. The function now may return 69562306a36Sopenharmony_ci * a value to use for the minimum acceptable corrected synchronization 69662306a36Sopenharmony_ci * window and may return further capabilities. 69762306a36Sopenharmony_ci * If we have the extra information store it. For older firmware that 69862306a36Sopenharmony_ci * does not implement the extended command use the default value. 69962306a36Sopenharmony_ci */ 70062306a36Sopenharmony_ci if (rc == 0 && 70162306a36Sopenharmony_ci out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST) 70262306a36Sopenharmony_ci ptp->min_synchronisation_ns = 70362306a36Sopenharmony_ci MCDI_DWORD(outbuf, 70462306a36Sopenharmony_ci PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN); 70562306a36Sopenharmony_ci else 70662306a36Sopenharmony_ci ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS; 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_ci if (rc == 0 && 70962306a36Sopenharmony_ci out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN) 71062306a36Sopenharmony_ci ptp->capabilities = MCDI_DWORD(outbuf, 71162306a36Sopenharmony_ci PTP_OUT_GET_ATTRIBUTES_CAPABILITIES); 71262306a36Sopenharmony_ci else 71362306a36Sopenharmony_ci ptp->capabilities = 0; 71462306a36Sopenharmony_ci 71562306a36Sopenharmony_ci /* Set up the shift for conversion between frequency 71662306a36Sopenharmony_ci * adjustments in parts-per-billion and the fixed-point 71762306a36Sopenharmony_ci * fractional ns format that the adapter uses. 71862306a36Sopenharmony_ci */ 71962306a36Sopenharmony_ci if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN)) 72062306a36Sopenharmony_ci ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44; 72162306a36Sopenharmony_ci else 72262306a36Sopenharmony_ci ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40; 72362306a36Sopenharmony_ci 72462306a36Sopenharmony_ci return 0; 72562306a36Sopenharmony_ci} 72662306a36Sopenharmony_ci 72762306a36Sopenharmony_ci/* Get PTP timestamp corrections */ 72862306a36Sopenharmony_cistatic int efx_ptp_get_timestamp_corrections(struct efx_nic *efx) 72962306a36Sopenharmony_ci{ 73062306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN); 73162306a36Sopenharmony_ci MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN); 73262306a36Sopenharmony_ci int rc; 73362306a36Sopenharmony_ci size_t out_len; 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_ci /* Get the timestamp corrections from the NIC. If this operation is 73662306a36Sopenharmony_ci * not supported (older NICs) then no correction is required. 73762306a36Sopenharmony_ci */ 73862306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, 73962306a36Sopenharmony_ci MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS); 74062306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 74162306a36Sopenharmony_ci 74262306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 74362306a36Sopenharmony_ci outbuf, sizeof(outbuf), &out_len); 74462306a36Sopenharmony_ci if (rc == 0) { 74562306a36Sopenharmony_ci efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf, 74662306a36Sopenharmony_ci PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT); 74762306a36Sopenharmony_ci efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf, 74862306a36Sopenharmony_ci PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE); 74962306a36Sopenharmony_ci efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf, 75062306a36Sopenharmony_ci PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT); 75162306a36Sopenharmony_ci efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf, 75262306a36Sopenharmony_ci PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN); 75362306a36Sopenharmony_ci 75462306a36Sopenharmony_ci if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) { 75562306a36Sopenharmony_ci efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD( 75662306a36Sopenharmony_ci outbuf, 75762306a36Sopenharmony_ci PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX); 75862306a36Sopenharmony_ci efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD( 75962306a36Sopenharmony_ci outbuf, 76062306a36Sopenharmony_ci PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX); 76162306a36Sopenharmony_ci } else { 76262306a36Sopenharmony_ci efx->ptp_data->ts_corrections.general_tx = 76362306a36Sopenharmony_ci efx->ptp_data->ts_corrections.ptp_tx; 76462306a36Sopenharmony_ci efx->ptp_data->ts_corrections.general_rx = 76562306a36Sopenharmony_ci efx->ptp_data->ts_corrections.ptp_rx; 76662306a36Sopenharmony_ci } 76762306a36Sopenharmony_ci } else if (rc == -EINVAL) { 76862306a36Sopenharmony_ci efx->ptp_data->ts_corrections.ptp_tx = 0; 76962306a36Sopenharmony_ci efx->ptp_data->ts_corrections.ptp_rx = 0; 77062306a36Sopenharmony_ci efx->ptp_data->ts_corrections.pps_out = 0; 77162306a36Sopenharmony_ci efx->ptp_data->ts_corrections.pps_in = 0; 77262306a36Sopenharmony_ci efx->ptp_data->ts_corrections.general_tx = 0; 77362306a36Sopenharmony_ci efx->ptp_data->ts_corrections.general_rx = 0; 77462306a36Sopenharmony_ci } else { 77562306a36Sopenharmony_ci efx_siena_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), 77662306a36Sopenharmony_ci outbuf, sizeof(outbuf), rc); 77762306a36Sopenharmony_ci return rc; 77862306a36Sopenharmony_ci } 77962306a36Sopenharmony_ci 78062306a36Sopenharmony_ci return 0; 78162306a36Sopenharmony_ci} 78262306a36Sopenharmony_ci 78362306a36Sopenharmony_ci/* Enable MCDI PTP support. */ 78462306a36Sopenharmony_cistatic int efx_ptp_enable(struct efx_nic *efx) 78562306a36Sopenharmony_ci{ 78662306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN); 78762306a36Sopenharmony_ci MCDI_DECLARE_BUF_ERR(outbuf); 78862306a36Sopenharmony_ci int rc; 78962306a36Sopenharmony_ci 79062306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE); 79162306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 79262306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE, 79362306a36Sopenharmony_ci efx->ptp_data->channel ? 79462306a36Sopenharmony_ci efx->ptp_data->channel->channel : 0); 79562306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode); 79662306a36Sopenharmony_ci 79762306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 79862306a36Sopenharmony_ci outbuf, sizeof(outbuf), NULL); 79962306a36Sopenharmony_ci rc = (rc == -EALREADY) ? 0 : rc; 80062306a36Sopenharmony_ci if (rc) 80162306a36Sopenharmony_ci efx_siena_mcdi_display_error(efx, MC_CMD_PTP, 80262306a36Sopenharmony_ci MC_CMD_PTP_IN_ENABLE_LEN, 80362306a36Sopenharmony_ci outbuf, sizeof(outbuf), rc); 80462306a36Sopenharmony_ci return rc; 80562306a36Sopenharmony_ci} 80662306a36Sopenharmony_ci 80762306a36Sopenharmony_ci/* Disable MCDI PTP support. 80862306a36Sopenharmony_ci * 80962306a36Sopenharmony_ci * Note that this function should never rely on the presence of ptp_data - 81062306a36Sopenharmony_ci * may be called before that exists. 81162306a36Sopenharmony_ci */ 81262306a36Sopenharmony_cistatic int efx_ptp_disable(struct efx_nic *efx) 81362306a36Sopenharmony_ci{ 81462306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN); 81562306a36Sopenharmony_ci MCDI_DECLARE_BUF_ERR(outbuf); 81662306a36Sopenharmony_ci int rc; 81762306a36Sopenharmony_ci 81862306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE); 81962306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 82062306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 82162306a36Sopenharmony_ci outbuf, sizeof(outbuf), NULL); 82262306a36Sopenharmony_ci rc = (rc == -EALREADY) ? 0 : rc; 82362306a36Sopenharmony_ci /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function 82462306a36Sopenharmony_ci * should only have been called during probe. 82562306a36Sopenharmony_ci */ 82662306a36Sopenharmony_ci if (rc == -ENOSYS || rc == -EPERM) 82762306a36Sopenharmony_ci pci_info(efx->pci_dev, "no PTP support\n"); 82862306a36Sopenharmony_ci else if (rc) 82962306a36Sopenharmony_ci efx_siena_mcdi_display_error(efx, MC_CMD_PTP, 83062306a36Sopenharmony_ci MC_CMD_PTP_IN_DISABLE_LEN, 83162306a36Sopenharmony_ci outbuf, sizeof(outbuf), rc); 83262306a36Sopenharmony_ci return rc; 83362306a36Sopenharmony_ci} 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_cistatic void efx_ptp_deliver_rx_queue(struct sk_buff_head *q) 83662306a36Sopenharmony_ci{ 83762306a36Sopenharmony_ci struct sk_buff *skb; 83862306a36Sopenharmony_ci 83962306a36Sopenharmony_ci while ((skb = skb_dequeue(q))) { 84062306a36Sopenharmony_ci local_bh_disable(); 84162306a36Sopenharmony_ci netif_receive_skb(skb); 84262306a36Sopenharmony_ci local_bh_enable(); 84362306a36Sopenharmony_ci } 84462306a36Sopenharmony_ci} 84562306a36Sopenharmony_ci 84662306a36Sopenharmony_cistatic void efx_ptp_handle_no_channel(struct efx_nic *efx) 84762306a36Sopenharmony_ci{ 84862306a36Sopenharmony_ci netif_err(efx, drv, efx->net_dev, 84962306a36Sopenharmony_ci "ERROR: PTP requires MSI-X and 1 additional interrupt" 85062306a36Sopenharmony_ci "vector. PTP disabled\n"); 85162306a36Sopenharmony_ci} 85262306a36Sopenharmony_ci 85362306a36Sopenharmony_ci/* Repeatedly send the host time to the MC which will capture the hardware 85462306a36Sopenharmony_ci * time. 85562306a36Sopenharmony_ci */ 85662306a36Sopenharmony_cistatic void efx_ptp_send_times(struct efx_nic *efx, 85762306a36Sopenharmony_ci struct pps_event_time *last_time) 85862306a36Sopenharmony_ci{ 85962306a36Sopenharmony_ci struct pps_event_time now; 86062306a36Sopenharmony_ci struct timespec64 limit; 86162306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 86262306a36Sopenharmony_ci int *mc_running = ptp->start.addr; 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci pps_get_ts(&now); 86562306a36Sopenharmony_ci limit = now.ts_real; 86662306a36Sopenharmony_ci timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS); 86762306a36Sopenharmony_ci 86862306a36Sopenharmony_ci /* Write host time for specified period or until MC is done */ 86962306a36Sopenharmony_ci while ((timespec64_compare(&now.ts_real, &limit) < 0) && 87062306a36Sopenharmony_ci READ_ONCE(*mc_running)) { 87162306a36Sopenharmony_ci struct timespec64 update_time; 87262306a36Sopenharmony_ci unsigned int host_time; 87362306a36Sopenharmony_ci 87462306a36Sopenharmony_ci /* Don't update continuously to avoid saturating the PCIe bus */ 87562306a36Sopenharmony_ci update_time = now.ts_real; 87662306a36Sopenharmony_ci timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS); 87762306a36Sopenharmony_ci do { 87862306a36Sopenharmony_ci pps_get_ts(&now); 87962306a36Sopenharmony_ci } while ((timespec64_compare(&now.ts_real, &update_time) < 0) && 88062306a36Sopenharmony_ci READ_ONCE(*mc_running)); 88162306a36Sopenharmony_ci 88262306a36Sopenharmony_ci /* Synchronise NIC with single word of time only */ 88362306a36Sopenharmony_ci host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS | 88462306a36Sopenharmony_ci now.ts_real.tv_nsec); 88562306a36Sopenharmony_ci /* Update host time in NIC memory */ 88662306a36Sopenharmony_ci efx->type->ptp_write_host_time(efx, host_time); 88762306a36Sopenharmony_ci } 88862306a36Sopenharmony_ci *last_time = now; 88962306a36Sopenharmony_ci} 89062306a36Sopenharmony_ci 89162306a36Sopenharmony_ci/* Read a timeset from the MC's results and partial process. */ 89262306a36Sopenharmony_cistatic void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data), 89362306a36Sopenharmony_ci struct efx_ptp_timeset *timeset) 89462306a36Sopenharmony_ci{ 89562306a36Sopenharmony_ci unsigned start_ns, end_ns; 89662306a36Sopenharmony_ci 89762306a36Sopenharmony_ci timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART); 89862306a36Sopenharmony_ci timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR); 89962306a36Sopenharmony_ci timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR); 90062306a36Sopenharmony_ci timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND), 90162306a36Sopenharmony_ci timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS); 90262306a36Sopenharmony_ci 90362306a36Sopenharmony_ci /* Ignore seconds */ 90462306a36Sopenharmony_ci start_ns = timeset->host_start & MC_NANOSECOND_MASK; 90562306a36Sopenharmony_ci end_ns = timeset->host_end & MC_NANOSECOND_MASK; 90662306a36Sopenharmony_ci /* Allow for rollover */ 90762306a36Sopenharmony_ci if (end_ns < start_ns) 90862306a36Sopenharmony_ci end_ns += NSEC_PER_SEC; 90962306a36Sopenharmony_ci /* Determine duration of operation */ 91062306a36Sopenharmony_ci timeset->window = end_ns - start_ns; 91162306a36Sopenharmony_ci} 91262306a36Sopenharmony_ci 91362306a36Sopenharmony_ci/* Process times received from MC. 91462306a36Sopenharmony_ci * 91562306a36Sopenharmony_ci * Extract times from returned results, and establish the minimum value 91662306a36Sopenharmony_ci * seen. The minimum value represents the "best" possible time and events 91762306a36Sopenharmony_ci * too much greater than this are rejected - the machine is, perhaps, too 91862306a36Sopenharmony_ci * busy. A number of readings are taken so that, hopefully, at least one good 91962306a36Sopenharmony_ci * synchronisation will be seen in the results. 92062306a36Sopenharmony_ci */ 92162306a36Sopenharmony_cistatic int 92262306a36Sopenharmony_ciefx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf), 92362306a36Sopenharmony_ci size_t response_length, 92462306a36Sopenharmony_ci const struct pps_event_time *last_time) 92562306a36Sopenharmony_ci{ 92662306a36Sopenharmony_ci unsigned number_readings = 92762306a36Sopenharmony_ci MCDI_VAR_ARRAY_LEN(response_length, 92862306a36Sopenharmony_ci PTP_OUT_SYNCHRONIZE_TIMESET); 92962306a36Sopenharmony_ci unsigned i; 93062306a36Sopenharmony_ci unsigned ngood = 0; 93162306a36Sopenharmony_ci unsigned last_good = 0; 93262306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 93362306a36Sopenharmony_ci u32 last_sec; 93462306a36Sopenharmony_ci u32 start_sec; 93562306a36Sopenharmony_ci struct timespec64 delta; 93662306a36Sopenharmony_ci ktime_t mc_time; 93762306a36Sopenharmony_ci 93862306a36Sopenharmony_ci if (number_readings == 0) 93962306a36Sopenharmony_ci return -EAGAIN; 94062306a36Sopenharmony_ci 94162306a36Sopenharmony_ci /* Read the set of results and find the last good host-MC 94262306a36Sopenharmony_ci * synchronization result. The MC times when it finishes reading the 94362306a36Sopenharmony_ci * host time so the corrected window time should be fairly constant 94462306a36Sopenharmony_ci * for a given platform. Increment stats for any results that appear 94562306a36Sopenharmony_ci * to be erroneous. 94662306a36Sopenharmony_ci */ 94762306a36Sopenharmony_ci for (i = 0; i < number_readings; i++) { 94862306a36Sopenharmony_ci s32 window, corrected; 94962306a36Sopenharmony_ci struct timespec64 wait; 95062306a36Sopenharmony_ci 95162306a36Sopenharmony_ci efx_ptp_read_timeset( 95262306a36Sopenharmony_ci MCDI_ARRAY_STRUCT_PTR(synch_buf, 95362306a36Sopenharmony_ci PTP_OUT_SYNCHRONIZE_TIMESET, i), 95462306a36Sopenharmony_ci &ptp->timeset[i]); 95562306a36Sopenharmony_ci 95662306a36Sopenharmony_ci wait = ktime_to_timespec64( 95762306a36Sopenharmony_ci ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0)); 95862306a36Sopenharmony_ci window = ptp->timeset[i].window; 95962306a36Sopenharmony_ci corrected = window - wait.tv_nsec; 96062306a36Sopenharmony_ci 96162306a36Sopenharmony_ci /* We expect the uncorrected synchronization window to be at 96262306a36Sopenharmony_ci * least as large as the interval between host start and end 96362306a36Sopenharmony_ci * times. If it is smaller than this then this is mostly likely 96462306a36Sopenharmony_ci * to be a consequence of the host's time being adjusted. 96562306a36Sopenharmony_ci * Check that the corrected sync window is in a reasonable 96662306a36Sopenharmony_ci * range. If it is out of range it is likely to be because an 96762306a36Sopenharmony_ci * interrupt or other delay occurred between reading the system 96862306a36Sopenharmony_ci * time and writing it to MC memory. 96962306a36Sopenharmony_ci */ 97062306a36Sopenharmony_ci if (window < SYNCHRONISATION_GRANULARITY_NS) { 97162306a36Sopenharmony_ci ++ptp->invalid_sync_windows; 97262306a36Sopenharmony_ci } else if (corrected >= MAX_SYNCHRONISATION_NS) { 97362306a36Sopenharmony_ci ++ptp->oversize_sync_windows; 97462306a36Sopenharmony_ci } else if (corrected < ptp->min_synchronisation_ns) { 97562306a36Sopenharmony_ci ++ptp->undersize_sync_windows; 97662306a36Sopenharmony_ci } else { 97762306a36Sopenharmony_ci ngood++; 97862306a36Sopenharmony_ci last_good = i; 97962306a36Sopenharmony_ci } 98062306a36Sopenharmony_ci } 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci if (ngood == 0) { 98362306a36Sopenharmony_ci netif_warn(efx, drv, efx->net_dev, 98462306a36Sopenharmony_ci "PTP no suitable synchronisations\n"); 98562306a36Sopenharmony_ci return -EAGAIN; 98662306a36Sopenharmony_ci } 98762306a36Sopenharmony_ci 98862306a36Sopenharmony_ci /* Calculate delay from last good sync (host time) to last_time. 98962306a36Sopenharmony_ci * It is possible that the seconds rolled over between taking 99062306a36Sopenharmony_ci * the start reading and the last value written by the host. The 99162306a36Sopenharmony_ci * timescales are such that a gap of more than one second is never 99262306a36Sopenharmony_ci * expected. delta is *not* normalised. 99362306a36Sopenharmony_ci */ 99462306a36Sopenharmony_ci start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS; 99562306a36Sopenharmony_ci last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK; 99662306a36Sopenharmony_ci if (start_sec != last_sec && 99762306a36Sopenharmony_ci ((start_sec + 1) & MC_SECOND_MASK) != last_sec) { 99862306a36Sopenharmony_ci netif_warn(efx, hw, efx->net_dev, 99962306a36Sopenharmony_ci "PTP bad synchronisation seconds\n"); 100062306a36Sopenharmony_ci return -EAGAIN; 100162306a36Sopenharmony_ci } 100262306a36Sopenharmony_ci delta.tv_sec = (last_sec - start_sec) & 1; 100362306a36Sopenharmony_ci delta.tv_nsec = 100462306a36Sopenharmony_ci last_time->ts_real.tv_nsec - 100562306a36Sopenharmony_ci (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK); 100662306a36Sopenharmony_ci 100762306a36Sopenharmony_ci /* Convert the NIC time at last good sync into kernel time. 100862306a36Sopenharmony_ci * No correction is required - this time is the output of a 100962306a36Sopenharmony_ci * firmware process. 101062306a36Sopenharmony_ci */ 101162306a36Sopenharmony_ci mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major, 101262306a36Sopenharmony_ci ptp->timeset[last_good].minor, 0); 101362306a36Sopenharmony_ci 101462306a36Sopenharmony_ci /* Calculate delay from NIC top of second to last_time */ 101562306a36Sopenharmony_ci delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec; 101662306a36Sopenharmony_ci 101762306a36Sopenharmony_ci /* Set PPS timestamp to match NIC top of second */ 101862306a36Sopenharmony_ci ptp->host_time_pps = *last_time; 101962306a36Sopenharmony_ci pps_sub_ts(&ptp->host_time_pps, delta); 102062306a36Sopenharmony_ci 102162306a36Sopenharmony_ci return 0; 102262306a36Sopenharmony_ci} 102362306a36Sopenharmony_ci 102462306a36Sopenharmony_ci/* Synchronize times between the host and the MC */ 102562306a36Sopenharmony_cistatic int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings) 102662306a36Sopenharmony_ci{ 102762306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 102862306a36Sopenharmony_ci MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX); 102962306a36Sopenharmony_ci size_t response_length; 103062306a36Sopenharmony_ci int rc; 103162306a36Sopenharmony_ci unsigned long timeout; 103262306a36Sopenharmony_ci struct pps_event_time last_time = {}; 103362306a36Sopenharmony_ci unsigned int loops = 0; 103462306a36Sopenharmony_ci int *start = ptp->start.addr; 103562306a36Sopenharmony_ci 103662306a36Sopenharmony_ci MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE); 103762306a36Sopenharmony_ci MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0); 103862306a36Sopenharmony_ci MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS, 103962306a36Sopenharmony_ci num_readings); 104062306a36Sopenharmony_ci MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR, 104162306a36Sopenharmony_ci ptp->start.dma_addr); 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci /* Clear flag that signals MC ready */ 104462306a36Sopenharmony_ci WRITE_ONCE(*start, 0); 104562306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf, 104662306a36Sopenharmony_ci MC_CMD_PTP_IN_SYNCHRONIZE_LEN); 104762306a36Sopenharmony_ci EFX_WARN_ON_ONCE_PARANOID(rc); 104862306a36Sopenharmony_ci 104962306a36Sopenharmony_ci /* Wait for start from MCDI (or timeout) */ 105062306a36Sopenharmony_ci timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS); 105162306a36Sopenharmony_ci while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) { 105262306a36Sopenharmony_ci udelay(20); /* Usually start MCDI execution quickly */ 105362306a36Sopenharmony_ci loops++; 105462306a36Sopenharmony_ci } 105562306a36Sopenharmony_ci 105662306a36Sopenharmony_ci if (loops <= 1) 105762306a36Sopenharmony_ci ++ptp->fast_syncs; 105862306a36Sopenharmony_ci if (!time_before(jiffies, timeout)) 105962306a36Sopenharmony_ci ++ptp->sync_timeouts; 106062306a36Sopenharmony_ci 106162306a36Sopenharmony_ci if (READ_ONCE(*start)) 106262306a36Sopenharmony_ci efx_ptp_send_times(efx, &last_time); 106362306a36Sopenharmony_ci 106462306a36Sopenharmony_ci /* Collect results */ 106562306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc_finish(efx, MC_CMD_PTP, 106662306a36Sopenharmony_ci MC_CMD_PTP_IN_SYNCHRONIZE_LEN, 106762306a36Sopenharmony_ci synch_buf, sizeof(synch_buf), 106862306a36Sopenharmony_ci &response_length); 106962306a36Sopenharmony_ci if (rc == 0) { 107062306a36Sopenharmony_ci rc = efx_ptp_process_times(efx, synch_buf, response_length, 107162306a36Sopenharmony_ci &last_time); 107262306a36Sopenharmony_ci if (rc == 0) 107362306a36Sopenharmony_ci ++ptp->good_syncs; 107462306a36Sopenharmony_ci else 107562306a36Sopenharmony_ci ++ptp->no_time_syncs; 107662306a36Sopenharmony_ci } 107762306a36Sopenharmony_ci 107862306a36Sopenharmony_ci /* Increment the bad syncs counter if the synchronize fails, whatever 107962306a36Sopenharmony_ci * the reason. 108062306a36Sopenharmony_ci */ 108162306a36Sopenharmony_ci if (rc != 0) 108262306a36Sopenharmony_ci ++ptp->bad_syncs; 108362306a36Sopenharmony_ci 108462306a36Sopenharmony_ci return rc; 108562306a36Sopenharmony_ci} 108662306a36Sopenharmony_ci 108762306a36Sopenharmony_ci/* Transmit a PTP packet via the dedicated hardware timestamped queue. */ 108862306a36Sopenharmony_cistatic void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb) 108962306a36Sopenharmony_ci{ 109062306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = efx->ptp_data; 109162306a36Sopenharmony_ci u8 type = efx_tx_csum_type_skb(skb); 109262306a36Sopenharmony_ci struct efx_tx_queue *tx_queue; 109362306a36Sopenharmony_ci 109462306a36Sopenharmony_ci tx_queue = efx_channel_get_tx_queue(ptp_data->channel, type); 109562306a36Sopenharmony_ci if (tx_queue && tx_queue->timestamping) { 109662306a36Sopenharmony_ci efx_enqueue_skb(tx_queue, skb); 109762306a36Sopenharmony_ci } else { 109862306a36Sopenharmony_ci WARN_ONCE(1, "PTP channel has no timestamped tx queue\n"); 109962306a36Sopenharmony_ci dev_kfree_skb_any(skb); 110062306a36Sopenharmony_ci } 110162306a36Sopenharmony_ci} 110262306a36Sopenharmony_ci 110362306a36Sopenharmony_ci/* Transmit a PTP packet, via the MCDI interface, to the wire. */ 110462306a36Sopenharmony_cistatic void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb) 110562306a36Sopenharmony_ci{ 110662306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = efx->ptp_data; 110762306a36Sopenharmony_ci struct skb_shared_hwtstamps timestamps; 110862306a36Sopenharmony_ci int rc = -EIO; 110962306a36Sopenharmony_ci MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN); 111062306a36Sopenharmony_ci size_t len; 111162306a36Sopenharmony_ci 111262306a36Sopenharmony_ci MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT); 111362306a36Sopenharmony_ci MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0); 111462306a36Sopenharmony_ci MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len); 111562306a36Sopenharmony_ci if (skb_shinfo(skb)->nr_frags != 0) { 111662306a36Sopenharmony_ci rc = skb_linearize(skb); 111762306a36Sopenharmony_ci if (rc != 0) 111862306a36Sopenharmony_ci goto fail; 111962306a36Sopenharmony_ci } 112062306a36Sopenharmony_ci 112162306a36Sopenharmony_ci if (skb->ip_summed == CHECKSUM_PARTIAL) { 112262306a36Sopenharmony_ci rc = skb_checksum_help(skb); 112362306a36Sopenharmony_ci if (rc != 0) 112462306a36Sopenharmony_ci goto fail; 112562306a36Sopenharmony_ci } 112662306a36Sopenharmony_ci skb_copy_from_linear_data(skb, 112762306a36Sopenharmony_ci MCDI_PTR(ptp_data->txbuf, 112862306a36Sopenharmony_ci PTP_IN_TRANSMIT_PACKET), 112962306a36Sopenharmony_ci skb->len); 113062306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc(efx, MC_CMD_PTP, ptp_data->txbuf, 113162306a36Sopenharmony_ci MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len), txtime, 113262306a36Sopenharmony_ci sizeof(txtime), &len); 113362306a36Sopenharmony_ci if (rc != 0) 113462306a36Sopenharmony_ci goto fail; 113562306a36Sopenharmony_ci 113662306a36Sopenharmony_ci memset(×tamps, 0, sizeof(timestamps)); 113762306a36Sopenharmony_ci timestamps.hwtstamp = ptp_data->nic_to_kernel_time( 113862306a36Sopenharmony_ci MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR), 113962306a36Sopenharmony_ci MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR), 114062306a36Sopenharmony_ci ptp_data->ts_corrections.ptp_tx); 114162306a36Sopenharmony_ci 114262306a36Sopenharmony_ci skb_tstamp_tx(skb, ×tamps); 114362306a36Sopenharmony_ci 114462306a36Sopenharmony_ci rc = 0; 114562306a36Sopenharmony_ci 114662306a36Sopenharmony_cifail: 114762306a36Sopenharmony_ci dev_kfree_skb_any(skb); 114862306a36Sopenharmony_ci 114962306a36Sopenharmony_ci return; 115062306a36Sopenharmony_ci} 115162306a36Sopenharmony_ci 115262306a36Sopenharmony_cistatic void efx_ptp_drop_time_expired_events(struct efx_nic *efx) 115362306a36Sopenharmony_ci{ 115462306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 115562306a36Sopenharmony_ci struct list_head *cursor; 115662306a36Sopenharmony_ci struct list_head *next; 115762306a36Sopenharmony_ci 115862306a36Sopenharmony_ci if (ptp->rx_ts_inline) 115962306a36Sopenharmony_ci return; 116062306a36Sopenharmony_ci 116162306a36Sopenharmony_ci /* Drop time-expired events */ 116262306a36Sopenharmony_ci spin_lock_bh(&ptp->evt_lock); 116362306a36Sopenharmony_ci list_for_each_safe(cursor, next, &ptp->evt_list) { 116462306a36Sopenharmony_ci struct efx_ptp_event_rx *evt; 116562306a36Sopenharmony_ci 116662306a36Sopenharmony_ci evt = list_entry(cursor, struct efx_ptp_event_rx, 116762306a36Sopenharmony_ci link); 116862306a36Sopenharmony_ci if (time_after(jiffies, evt->expiry)) { 116962306a36Sopenharmony_ci list_move(&evt->link, &ptp->evt_free_list); 117062306a36Sopenharmony_ci netif_warn(efx, hw, efx->net_dev, 117162306a36Sopenharmony_ci "PTP rx event dropped\n"); 117262306a36Sopenharmony_ci } 117362306a36Sopenharmony_ci } 117462306a36Sopenharmony_ci spin_unlock_bh(&ptp->evt_lock); 117562306a36Sopenharmony_ci} 117662306a36Sopenharmony_ci 117762306a36Sopenharmony_cistatic enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx, 117862306a36Sopenharmony_ci struct sk_buff *skb) 117962306a36Sopenharmony_ci{ 118062306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 118162306a36Sopenharmony_ci bool evts_waiting; 118262306a36Sopenharmony_ci struct list_head *cursor; 118362306a36Sopenharmony_ci struct list_head *next; 118462306a36Sopenharmony_ci struct efx_ptp_match *match; 118562306a36Sopenharmony_ci enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED; 118662306a36Sopenharmony_ci 118762306a36Sopenharmony_ci WARN_ON_ONCE(ptp->rx_ts_inline); 118862306a36Sopenharmony_ci 118962306a36Sopenharmony_ci spin_lock_bh(&ptp->evt_lock); 119062306a36Sopenharmony_ci evts_waiting = !list_empty(&ptp->evt_list); 119162306a36Sopenharmony_ci spin_unlock_bh(&ptp->evt_lock); 119262306a36Sopenharmony_ci 119362306a36Sopenharmony_ci if (!evts_waiting) 119462306a36Sopenharmony_ci return PTP_PACKET_STATE_UNMATCHED; 119562306a36Sopenharmony_ci 119662306a36Sopenharmony_ci match = (struct efx_ptp_match *)skb->cb; 119762306a36Sopenharmony_ci /* Look for a matching timestamp in the event queue */ 119862306a36Sopenharmony_ci spin_lock_bh(&ptp->evt_lock); 119962306a36Sopenharmony_ci list_for_each_safe(cursor, next, &ptp->evt_list) { 120062306a36Sopenharmony_ci struct efx_ptp_event_rx *evt; 120162306a36Sopenharmony_ci 120262306a36Sopenharmony_ci evt = list_entry(cursor, struct efx_ptp_event_rx, link); 120362306a36Sopenharmony_ci if ((evt->seq0 == match->words[0]) && 120462306a36Sopenharmony_ci (evt->seq1 == match->words[1])) { 120562306a36Sopenharmony_ci struct skb_shared_hwtstamps *timestamps; 120662306a36Sopenharmony_ci 120762306a36Sopenharmony_ci /* Match - add in hardware timestamp */ 120862306a36Sopenharmony_ci timestamps = skb_hwtstamps(skb); 120962306a36Sopenharmony_ci timestamps->hwtstamp = evt->hwtimestamp; 121062306a36Sopenharmony_ci 121162306a36Sopenharmony_ci match->state = PTP_PACKET_STATE_MATCHED; 121262306a36Sopenharmony_ci rc = PTP_PACKET_STATE_MATCHED; 121362306a36Sopenharmony_ci list_move(&evt->link, &ptp->evt_free_list); 121462306a36Sopenharmony_ci break; 121562306a36Sopenharmony_ci } 121662306a36Sopenharmony_ci } 121762306a36Sopenharmony_ci spin_unlock_bh(&ptp->evt_lock); 121862306a36Sopenharmony_ci 121962306a36Sopenharmony_ci return rc; 122062306a36Sopenharmony_ci} 122162306a36Sopenharmony_ci 122262306a36Sopenharmony_ci/* Process any queued receive events and corresponding packets 122362306a36Sopenharmony_ci * 122462306a36Sopenharmony_ci * q is returned with all the packets that are ready for delivery. 122562306a36Sopenharmony_ci */ 122662306a36Sopenharmony_cistatic void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q) 122762306a36Sopenharmony_ci{ 122862306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 122962306a36Sopenharmony_ci struct sk_buff *skb; 123062306a36Sopenharmony_ci 123162306a36Sopenharmony_ci while ((skb = skb_dequeue(&ptp->rxq))) { 123262306a36Sopenharmony_ci struct efx_ptp_match *match; 123362306a36Sopenharmony_ci 123462306a36Sopenharmony_ci match = (struct efx_ptp_match *)skb->cb; 123562306a36Sopenharmony_ci if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) { 123662306a36Sopenharmony_ci __skb_queue_tail(q, skb); 123762306a36Sopenharmony_ci } else if (efx_ptp_match_rx(efx, skb) == 123862306a36Sopenharmony_ci PTP_PACKET_STATE_MATCHED) { 123962306a36Sopenharmony_ci __skb_queue_tail(q, skb); 124062306a36Sopenharmony_ci } else if (time_after(jiffies, match->expiry)) { 124162306a36Sopenharmony_ci match->state = PTP_PACKET_STATE_TIMED_OUT; 124262306a36Sopenharmony_ci ++ptp->rx_no_timestamp; 124362306a36Sopenharmony_ci __skb_queue_tail(q, skb); 124462306a36Sopenharmony_ci } else { 124562306a36Sopenharmony_ci /* Replace unprocessed entry and stop */ 124662306a36Sopenharmony_ci skb_queue_head(&ptp->rxq, skb); 124762306a36Sopenharmony_ci break; 124862306a36Sopenharmony_ci } 124962306a36Sopenharmony_ci } 125062306a36Sopenharmony_ci} 125162306a36Sopenharmony_ci 125262306a36Sopenharmony_ci/* Complete processing of a received packet */ 125362306a36Sopenharmony_cistatic inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb) 125462306a36Sopenharmony_ci{ 125562306a36Sopenharmony_ci local_bh_disable(); 125662306a36Sopenharmony_ci netif_receive_skb(skb); 125762306a36Sopenharmony_ci local_bh_enable(); 125862306a36Sopenharmony_ci} 125962306a36Sopenharmony_ci 126062306a36Sopenharmony_cistatic void efx_ptp_remove_multicast_filters(struct efx_nic *efx) 126162306a36Sopenharmony_ci{ 126262306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 126362306a36Sopenharmony_ci 126462306a36Sopenharmony_ci if (ptp->rxfilter_installed) { 126562306a36Sopenharmony_ci efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, 126662306a36Sopenharmony_ci ptp->rxfilter_general); 126762306a36Sopenharmony_ci efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, 126862306a36Sopenharmony_ci ptp->rxfilter_event); 126962306a36Sopenharmony_ci ptp->rxfilter_installed = false; 127062306a36Sopenharmony_ci } 127162306a36Sopenharmony_ci} 127262306a36Sopenharmony_ci 127362306a36Sopenharmony_cistatic int efx_ptp_insert_multicast_filters(struct efx_nic *efx) 127462306a36Sopenharmony_ci{ 127562306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 127662306a36Sopenharmony_ci struct efx_filter_spec rxfilter; 127762306a36Sopenharmony_ci int rc; 127862306a36Sopenharmony_ci 127962306a36Sopenharmony_ci if (!ptp->channel || ptp->rxfilter_installed) 128062306a36Sopenharmony_ci return 0; 128162306a36Sopenharmony_ci 128262306a36Sopenharmony_ci /* Must filter on both event and general ports to ensure 128362306a36Sopenharmony_ci * that there is no packet re-ordering. 128462306a36Sopenharmony_ci */ 128562306a36Sopenharmony_ci efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0, 128662306a36Sopenharmony_ci efx_rx_queue_index( 128762306a36Sopenharmony_ci efx_channel_get_rx_queue(ptp->channel))); 128862306a36Sopenharmony_ci rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP, 128962306a36Sopenharmony_ci htonl(PTP_ADDRESS), 129062306a36Sopenharmony_ci htons(PTP_EVENT_PORT)); 129162306a36Sopenharmony_ci if (rc != 0) 129262306a36Sopenharmony_ci return rc; 129362306a36Sopenharmony_ci 129462306a36Sopenharmony_ci rc = efx_filter_insert_filter(efx, &rxfilter, true); 129562306a36Sopenharmony_ci if (rc < 0) 129662306a36Sopenharmony_ci return rc; 129762306a36Sopenharmony_ci ptp->rxfilter_event = rc; 129862306a36Sopenharmony_ci 129962306a36Sopenharmony_ci efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0, 130062306a36Sopenharmony_ci efx_rx_queue_index( 130162306a36Sopenharmony_ci efx_channel_get_rx_queue(ptp->channel))); 130262306a36Sopenharmony_ci rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP, 130362306a36Sopenharmony_ci htonl(PTP_ADDRESS), 130462306a36Sopenharmony_ci htons(PTP_GENERAL_PORT)); 130562306a36Sopenharmony_ci if (rc != 0) 130662306a36Sopenharmony_ci goto fail; 130762306a36Sopenharmony_ci 130862306a36Sopenharmony_ci rc = efx_filter_insert_filter(efx, &rxfilter, true); 130962306a36Sopenharmony_ci if (rc < 0) 131062306a36Sopenharmony_ci goto fail; 131162306a36Sopenharmony_ci ptp->rxfilter_general = rc; 131262306a36Sopenharmony_ci 131362306a36Sopenharmony_ci ptp->rxfilter_installed = true; 131462306a36Sopenharmony_ci return 0; 131562306a36Sopenharmony_ci 131662306a36Sopenharmony_cifail: 131762306a36Sopenharmony_ci efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, 131862306a36Sopenharmony_ci ptp->rxfilter_event); 131962306a36Sopenharmony_ci return rc; 132062306a36Sopenharmony_ci} 132162306a36Sopenharmony_ci 132262306a36Sopenharmony_cistatic int efx_ptp_start(struct efx_nic *efx) 132362306a36Sopenharmony_ci{ 132462306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 132562306a36Sopenharmony_ci int rc; 132662306a36Sopenharmony_ci 132762306a36Sopenharmony_ci ptp->reset_required = false; 132862306a36Sopenharmony_ci 132962306a36Sopenharmony_ci rc = efx_ptp_insert_multicast_filters(efx); 133062306a36Sopenharmony_ci if (rc) 133162306a36Sopenharmony_ci return rc; 133262306a36Sopenharmony_ci 133362306a36Sopenharmony_ci rc = efx_ptp_enable(efx); 133462306a36Sopenharmony_ci if (rc != 0) 133562306a36Sopenharmony_ci goto fail; 133662306a36Sopenharmony_ci 133762306a36Sopenharmony_ci ptp->evt_frag_idx = 0; 133862306a36Sopenharmony_ci ptp->current_adjfreq = 0; 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ci return 0; 134162306a36Sopenharmony_ci 134262306a36Sopenharmony_cifail: 134362306a36Sopenharmony_ci efx_ptp_remove_multicast_filters(efx); 134462306a36Sopenharmony_ci return rc; 134562306a36Sopenharmony_ci} 134662306a36Sopenharmony_ci 134762306a36Sopenharmony_cistatic int efx_ptp_stop(struct efx_nic *efx) 134862306a36Sopenharmony_ci{ 134962306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 135062306a36Sopenharmony_ci struct list_head *cursor; 135162306a36Sopenharmony_ci struct list_head *next; 135262306a36Sopenharmony_ci int rc; 135362306a36Sopenharmony_ci 135462306a36Sopenharmony_ci if (ptp == NULL) 135562306a36Sopenharmony_ci return 0; 135662306a36Sopenharmony_ci 135762306a36Sopenharmony_ci rc = efx_ptp_disable(efx); 135862306a36Sopenharmony_ci 135962306a36Sopenharmony_ci efx_ptp_remove_multicast_filters(efx); 136062306a36Sopenharmony_ci 136162306a36Sopenharmony_ci /* Make sure RX packets are really delivered */ 136262306a36Sopenharmony_ci efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq); 136362306a36Sopenharmony_ci skb_queue_purge(&efx->ptp_data->txq); 136462306a36Sopenharmony_ci 136562306a36Sopenharmony_ci /* Drop any pending receive events */ 136662306a36Sopenharmony_ci spin_lock_bh(&efx->ptp_data->evt_lock); 136762306a36Sopenharmony_ci list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) { 136862306a36Sopenharmony_ci list_move(cursor, &efx->ptp_data->evt_free_list); 136962306a36Sopenharmony_ci } 137062306a36Sopenharmony_ci spin_unlock_bh(&efx->ptp_data->evt_lock); 137162306a36Sopenharmony_ci 137262306a36Sopenharmony_ci return rc; 137362306a36Sopenharmony_ci} 137462306a36Sopenharmony_ci 137562306a36Sopenharmony_cistatic int efx_ptp_restart(struct efx_nic *efx) 137662306a36Sopenharmony_ci{ 137762306a36Sopenharmony_ci if (efx->ptp_data && efx->ptp_data->enabled) 137862306a36Sopenharmony_ci return efx_ptp_start(efx); 137962306a36Sopenharmony_ci return 0; 138062306a36Sopenharmony_ci} 138162306a36Sopenharmony_ci 138262306a36Sopenharmony_cistatic void efx_ptp_pps_worker(struct work_struct *work) 138362306a36Sopenharmony_ci{ 138462306a36Sopenharmony_ci struct efx_ptp_data *ptp = 138562306a36Sopenharmony_ci container_of(work, struct efx_ptp_data, pps_work); 138662306a36Sopenharmony_ci struct efx_nic *efx = ptp->efx; 138762306a36Sopenharmony_ci struct ptp_clock_event ptp_evt; 138862306a36Sopenharmony_ci 138962306a36Sopenharmony_ci if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS)) 139062306a36Sopenharmony_ci return; 139162306a36Sopenharmony_ci 139262306a36Sopenharmony_ci ptp_evt.type = PTP_CLOCK_PPSUSR; 139362306a36Sopenharmony_ci ptp_evt.pps_times = ptp->host_time_pps; 139462306a36Sopenharmony_ci ptp_clock_event(ptp->phc_clock, &ptp_evt); 139562306a36Sopenharmony_ci} 139662306a36Sopenharmony_ci 139762306a36Sopenharmony_cistatic void efx_ptp_worker(struct work_struct *work) 139862306a36Sopenharmony_ci{ 139962306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = 140062306a36Sopenharmony_ci container_of(work, struct efx_ptp_data, work); 140162306a36Sopenharmony_ci struct efx_nic *efx = ptp_data->efx; 140262306a36Sopenharmony_ci struct sk_buff *skb; 140362306a36Sopenharmony_ci struct sk_buff_head tempq; 140462306a36Sopenharmony_ci 140562306a36Sopenharmony_ci if (ptp_data->reset_required) { 140662306a36Sopenharmony_ci efx_ptp_stop(efx); 140762306a36Sopenharmony_ci efx_ptp_start(efx); 140862306a36Sopenharmony_ci return; 140962306a36Sopenharmony_ci } 141062306a36Sopenharmony_ci 141162306a36Sopenharmony_ci efx_ptp_drop_time_expired_events(efx); 141262306a36Sopenharmony_ci 141362306a36Sopenharmony_ci __skb_queue_head_init(&tempq); 141462306a36Sopenharmony_ci efx_ptp_process_events(efx, &tempq); 141562306a36Sopenharmony_ci 141662306a36Sopenharmony_ci while ((skb = skb_dequeue(&ptp_data->txq))) 141762306a36Sopenharmony_ci ptp_data->xmit_skb(efx, skb); 141862306a36Sopenharmony_ci 141962306a36Sopenharmony_ci while ((skb = __skb_dequeue(&tempq))) 142062306a36Sopenharmony_ci efx_ptp_process_rx(efx, skb); 142162306a36Sopenharmony_ci} 142262306a36Sopenharmony_ci 142362306a36Sopenharmony_cistatic const struct ptp_clock_info efx_phc_clock_info = { 142462306a36Sopenharmony_ci .owner = THIS_MODULE, 142562306a36Sopenharmony_ci .name = "sfc_siena", 142662306a36Sopenharmony_ci .max_adj = MAX_PPB, 142762306a36Sopenharmony_ci .n_alarm = 0, 142862306a36Sopenharmony_ci .n_ext_ts = 0, 142962306a36Sopenharmony_ci .n_per_out = 0, 143062306a36Sopenharmony_ci .n_pins = 0, 143162306a36Sopenharmony_ci .pps = 1, 143262306a36Sopenharmony_ci .adjfine = efx_phc_adjfine, 143362306a36Sopenharmony_ci .adjtime = efx_phc_adjtime, 143462306a36Sopenharmony_ci .gettime64 = efx_phc_gettime, 143562306a36Sopenharmony_ci .settime64 = efx_phc_settime, 143662306a36Sopenharmony_ci .enable = efx_phc_enable, 143762306a36Sopenharmony_ci}; 143862306a36Sopenharmony_ci 143962306a36Sopenharmony_ci/* Initialise PTP state. */ 144062306a36Sopenharmony_cistatic int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel) 144162306a36Sopenharmony_ci{ 144262306a36Sopenharmony_ci struct efx_ptp_data *ptp; 144362306a36Sopenharmony_ci int rc = 0; 144462306a36Sopenharmony_ci unsigned int pos; 144562306a36Sopenharmony_ci 144662306a36Sopenharmony_ci ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL); 144762306a36Sopenharmony_ci efx->ptp_data = ptp; 144862306a36Sopenharmony_ci if (!efx->ptp_data) 144962306a36Sopenharmony_ci return -ENOMEM; 145062306a36Sopenharmony_ci 145162306a36Sopenharmony_ci ptp->efx = efx; 145262306a36Sopenharmony_ci ptp->channel = channel; 145362306a36Sopenharmony_ci ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0; 145462306a36Sopenharmony_ci 145562306a36Sopenharmony_ci rc = efx_siena_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL); 145662306a36Sopenharmony_ci if (rc != 0) 145762306a36Sopenharmony_ci goto fail1; 145862306a36Sopenharmony_ci 145962306a36Sopenharmony_ci skb_queue_head_init(&ptp->rxq); 146062306a36Sopenharmony_ci skb_queue_head_init(&ptp->txq); 146162306a36Sopenharmony_ci ptp->workwq = create_singlethread_workqueue("sfc_siena_ptp"); 146262306a36Sopenharmony_ci if (!ptp->workwq) { 146362306a36Sopenharmony_ci rc = -ENOMEM; 146462306a36Sopenharmony_ci goto fail2; 146562306a36Sopenharmony_ci } 146662306a36Sopenharmony_ci 146762306a36Sopenharmony_ci if (efx_siena_ptp_use_mac_tx_timestamps(efx)) { 146862306a36Sopenharmony_ci ptp->xmit_skb = efx_ptp_xmit_skb_queue; 146962306a36Sopenharmony_ci /* Request sync events on this channel. */ 147062306a36Sopenharmony_ci channel->sync_events_state = SYNC_EVENTS_QUIESCENT; 147162306a36Sopenharmony_ci } else { 147262306a36Sopenharmony_ci ptp->xmit_skb = efx_ptp_xmit_skb_mc; 147362306a36Sopenharmony_ci } 147462306a36Sopenharmony_ci 147562306a36Sopenharmony_ci INIT_WORK(&ptp->work, efx_ptp_worker); 147662306a36Sopenharmony_ci ptp->config.flags = 0; 147762306a36Sopenharmony_ci ptp->config.tx_type = HWTSTAMP_TX_OFF; 147862306a36Sopenharmony_ci ptp->config.rx_filter = HWTSTAMP_FILTER_NONE; 147962306a36Sopenharmony_ci INIT_LIST_HEAD(&ptp->evt_list); 148062306a36Sopenharmony_ci INIT_LIST_HEAD(&ptp->evt_free_list); 148162306a36Sopenharmony_ci spin_lock_init(&ptp->evt_lock); 148262306a36Sopenharmony_ci for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++) 148362306a36Sopenharmony_ci list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list); 148462306a36Sopenharmony_ci 148562306a36Sopenharmony_ci /* Get the NIC PTP attributes and set up time conversions */ 148662306a36Sopenharmony_ci rc = efx_ptp_get_attributes(efx); 148762306a36Sopenharmony_ci if (rc < 0) 148862306a36Sopenharmony_ci goto fail3; 148962306a36Sopenharmony_ci 149062306a36Sopenharmony_ci /* Get the timestamp corrections */ 149162306a36Sopenharmony_ci rc = efx_ptp_get_timestamp_corrections(efx); 149262306a36Sopenharmony_ci if (rc < 0) 149362306a36Sopenharmony_ci goto fail3; 149462306a36Sopenharmony_ci 149562306a36Sopenharmony_ci if (efx->mcdi->fn_flags & 149662306a36Sopenharmony_ci (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) { 149762306a36Sopenharmony_ci ptp->phc_clock_info = efx_phc_clock_info; 149862306a36Sopenharmony_ci ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info, 149962306a36Sopenharmony_ci &efx->pci_dev->dev); 150062306a36Sopenharmony_ci if (IS_ERR(ptp->phc_clock)) { 150162306a36Sopenharmony_ci rc = PTR_ERR(ptp->phc_clock); 150262306a36Sopenharmony_ci goto fail3; 150362306a36Sopenharmony_ci } else if (ptp->phc_clock) { 150462306a36Sopenharmony_ci INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker); 150562306a36Sopenharmony_ci ptp->pps_workwq = 150662306a36Sopenharmony_ci create_singlethread_workqueue("sfc_siena_pps"); 150762306a36Sopenharmony_ci if (!ptp->pps_workwq) { 150862306a36Sopenharmony_ci rc = -ENOMEM; 150962306a36Sopenharmony_ci goto fail4; 151062306a36Sopenharmony_ci } 151162306a36Sopenharmony_ci } 151262306a36Sopenharmony_ci } 151362306a36Sopenharmony_ci ptp->nic_ts_enabled = false; 151462306a36Sopenharmony_ci 151562306a36Sopenharmony_ci return 0; 151662306a36Sopenharmony_cifail4: 151762306a36Sopenharmony_ci ptp_clock_unregister(efx->ptp_data->phc_clock); 151862306a36Sopenharmony_ci 151962306a36Sopenharmony_cifail3: 152062306a36Sopenharmony_ci destroy_workqueue(efx->ptp_data->workwq); 152162306a36Sopenharmony_ci 152262306a36Sopenharmony_cifail2: 152362306a36Sopenharmony_ci efx_siena_free_buffer(efx, &ptp->start); 152462306a36Sopenharmony_ci 152562306a36Sopenharmony_cifail1: 152662306a36Sopenharmony_ci kfree(efx->ptp_data); 152762306a36Sopenharmony_ci efx->ptp_data = NULL; 152862306a36Sopenharmony_ci 152962306a36Sopenharmony_ci return rc; 153062306a36Sopenharmony_ci} 153162306a36Sopenharmony_ci 153262306a36Sopenharmony_ci/* Initialise PTP channel. 153362306a36Sopenharmony_ci * 153462306a36Sopenharmony_ci * Setting core_index to zero causes the queue to be initialised and doesn't 153562306a36Sopenharmony_ci * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue. 153662306a36Sopenharmony_ci */ 153762306a36Sopenharmony_cistatic int efx_ptp_probe_channel(struct efx_channel *channel) 153862306a36Sopenharmony_ci{ 153962306a36Sopenharmony_ci struct efx_nic *efx = channel->efx; 154062306a36Sopenharmony_ci int rc; 154162306a36Sopenharmony_ci 154262306a36Sopenharmony_ci channel->irq_moderation_us = 0; 154362306a36Sopenharmony_ci channel->rx_queue.core_index = 0; 154462306a36Sopenharmony_ci 154562306a36Sopenharmony_ci rc = efx_ptp_probe(efx, channel); 154662306a36Sopenharmony_ci /* Failure to probe PTP is not fatal; this channel will just not be 154762306a36Sopenharmony_ci * used for anything. 154862306a36Sopenharmony_ci * In the case of EPERM, efx_ptp_probe will print its own message (in 154962306a36Sopenharmony_ci * efx_ptp_get_attributes()), so we don't need to. 155062306a36Sopenharmony_ci */ 155162306a36Sopenharmony_ci if (rc && rc != -EPERM) 155262306a36Sopenharmony_ci netif_warn(efx, drv, efx->net_dev, 155362306a36Sopenharmony_ci "Failed to probe PTP, rc=%d\n", rc); 155462306a36Sopenharmony_ci return 0; 155562306a36Sopenharmony_ci} 155662306a36Sopenharmony_ci 155762306a36Sopenharmony_cistatic void efx_ptp_remove(struct efx_nic *efx) 155862306a36Sopenharmony_ci{ 155962306a36Sopenharmony_ci if (!efx->ptp_data) 156062306a36Sopenharmony_ci return; 156162306a36Sopenharmony_ci 156262306a36Sopenharmony_ci (void)efx_ptp_disable(efx); 156362306a36Sopenharmony_ci 156462306a36Sopenharmony_ci cancel_work_sync(&efx->ptp_data->work); 156562306a36Sopenharmony_ci if (efx->ptp_data->pps_workwq) 156662306a36Sopenharmony_ci cancel_work_sync(&efx->ptp_data->pps_work); 156762306a36Sopenharmony_ci 156862306a36Sopenharmony_ci skb_queue_purge(&efx->ptp_data->rxq); 156962306a36Sopenharmony_ci skb_queue_purge(&efx->ptp_data->txq); 157062306a36Sopenharmony_ci 157162306a36Sopenharmony_ci if (efx->ptp_data->phc_clock) { 157262306a36Sopenharmony_ci destroy_workqueue(efx->ptp_data->pps_workwq); 157362306a36Sopenharmony_ci ptp_clock_unregister(efx->ptp_data->phc_clock); 157462306a36Sopenharmony_ci } 157562306a36Sopenharmony_ci 157662306a36Sopenharmony_ci destroy_workqueue(efx->ptp_data->workwq); 157762306a36Sopenharmony_ci 157862306a36Sopenharmony_ci efx_siena_free_buffer(efx, &efx->ptp_data->start); 157962306a36Sopenharmony_ci kfree(efx->ptp_data); 158062306a36Sopenharmony_ci efx->ptp_data = NULL; 158162306a36Sopenharmony_ci} 158262306a36Sopenharmony_ci 158362306a36Sopenharmony_cistatic void efx_ptp_remove_channel(struct efx_channel *channel) 158462306a36Sopenharmony_ci{ 158562306a36Sopenharmony_ci efx_ptp_remove(channel->efx); 158662306a36Sopenharmony_ci} 158762306a36Sopenharmony_ci 158862306a36Sopenharmony_cistatic void efx_ptp_get_channel_name(struct efx_channel *channel, 158962306a36Sopenharmony_ci char *buf, size_t len) 159062306a36Sopenharmony_ci{ 159162306a36Sopenharmony_ci snprintf(buf, len, "%s-ptp", channel->efx->name); 159262306a36Sopenharmony_ci} 159362306a36Sopenharmony_ci 159462306a36Sopenharmony_ci/* Determine whether this packet should be processed by the PTP module 159562306a36Sopenharmony_ci * or transmitted conventionally. 159662306a36Sopenharmony_ci */ 159762306a36Sopenharmony_cibool efx_siena_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb) 159862306a36Sopenharmony_ci{ 159962306a36Sopenharmony_ci return efx->ptp_data && 160062306a36Sopenharmony_ci efx->ptp_data->enabled && 160162306a36Sopenharmony_ci skb->len >= PTP_MIN_LENGTH && 160262306a36Sopenharmony_ci skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM && 160362306a36Sopenharmony_ci likely(skb->protocol == htons(ETH_P_IP)) && 160462306a36Sopenharmony_ci skb_transport_header_was_set(skb) && 160562306a36Sopenharmony_ci skb_network_header_len(skb) >= sizeof(struct iphdr) && 160662306a36Sopenharmony_ci ip_hdr(skb)->protocol == IPPROTO_UDP && 160762306a36Sopenharmony_ci skb_headlen(skb) >= 160862306a36Sopenharmony_ci skb_transport_offset(skb) + sizeof(struct udphdr) && 160962306a36Sopenharmony_ci udp_hdr(skb)->dest == htons(PTP_EVENT_PORT); 161062306a36Sopenharmony_ci} 161162306a36Sopenharmony_ci 161262306a36Sopenharmony_ci/* Receive a PTP packet. Packets are queued until the arrival of 161362306a36Sopenharmony_ci * the receive timestamp from the MC - this will probably occur after the 161462306a36Sopenharmony_ci * packet arrival because of the processing in the MC. 161562306a36Sopenharmony_ci */ 161662306a36Sopenharmony_cistatic bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb) 161762306a36Sopenharmony_ci{ 161862306a36Sopenharmony_ci struct efx_nic *efx = channel->efx; 161962306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 162062306a36Sopenharmony_ci struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb; 162162306a36Sopenharmony_ci u8 *match_data_012, *match_data_345; 162262306a36Sopenharmony_ci unsigned int version; 162362306a36Sopenharmony_ci u8 *data; 162462306a36Sopenharmony_ci 162562306a36Sopenharmony_ci match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS); 162662306a36Sopenharmony_ci 162762306a36Sopenharmony_ci /* Correct version? */ 162862306a36Sopenharmony_ci if (ptp->mode == MC_CMD_PTP_MODE_V1) { 162962306a36Sopenharmony_ci if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) { 163062306a36Sopenharmony_ci return false; 163162306a36Sopenharmony_ci } 163262306a36Sopenharmony_ci data = skb->data; 163362306a36Sopenharmony_ci version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]); 163462306a36Sopenharmony_ci if (version != PTP_VERSION_V1) { 163562306a36Sopenharmony_ci return false; 163662306a36Sopenharmony_ci } 163762306a36Sopenharmony_ci 163862306a36Sopenharmony_ci /* PTP V1 uses all six bytes of the UUID to match the packet 163962306a36Sopenharmony_ci * to the timestamp 164062306a36Sopenharmony_ci */ 164162306a36Sopenharmony_ci match_data_012 = data + PTP_V1_UUID_OFFSET; 164262306a36Sopenharmony_ci match_data_345 = data + PTP_V1_UUID_OFFSET + 3; 164362306a36Sopenharmony_ci } else { 164462306a36Sopenharmony_ci if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) { 164562306a36Sopenharmony_ci return false; 164662306a36Sopenharmony_ci } 164762306a36Sopenharmony_ci data = skb->data; 164862306a36Sopenharmony_ci version = data[PTP_V2_VERSION_OFFSET]; 164962306a36Sopenharmony_ci if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) { 165062306a36Sopenharmony_ci return false; 165162306a36Sopenharmony_ci } 165262306a36Sopenharmony_ci 165362306a36Sopenharmony_ci /* The original V2 implementation uses bytes 2-7 of 165462306a36Sopenharmony_ci * the UUID to match the packet to the timestamp. This 165562306a36Sopenharmony_ci * discards two of the bytes of the MAC address used 165662306a36Sopenharmony_ci * to create the UUID (SF bug 33070). The PTP V2 165762306a36Sopenharmony_ci * enhanced mode fixes this issue and uses bytes 0-2 165862306a36Sopenharmony_ci * and byte 5-7 of the UUID. 165962306a36Sopenharmony_ci */ 166062306a36Sopenharmony_ci match_data_345 = data + PTP_V2_UUID_OFFSET + 5; 166162306a36Sopenharmony_ci if (ptp->mode == MC_CMD_PTP_MODE_V2) { 166262306a36Sopenharmony_ci match_data_012 = data + PTP_V2_UUID_OFFSET + 2; 166362306a36Sopenharmony_ci } else { 166462306a36Sopenharmony_ci match_data_012 = data + PTP_V2_UUID_OFFSET + 0; 166562306a36Sopenharmony_ci BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED); 166662306a36Sopenharmony_ci } 166762306a36Sopenharmony_ci } 166862306a36Sopenharmony_ci 166962306a36Sopenharmony_ci /* Does this packet require timestamping? */ 167062306a36Sopenharmony_ci if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) { 167162306a36Sopenharmony_ci match->state = PTP_PACKET_STATE_UNMATCHED; 167262306a36Sopenharmony_ci 167362306a36Sopenharmony_ci /* We expect the sequence number to be in the same position in 167462306a36Sopenharmony_ci * the packet for PTP V1 and V2 167562306a36Sopenharmony_ci */ 167662306a36Sopenharmony_ci BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET); 167762306a36Sopenharmony_ci BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH); 167862306a36Sopenharmony_ci 167962306a36Sopenharmony_ci /* Extract UUID/Sequence information */ 168062306a36Sopenharmony_ci match->words[0] = (match_data_012[0] | 168162306a36Sopenharmony_ci (match_data_012[1] << 8) | 168262306a36Sopenharmony_ci (match_data_012[2] << 16) | 168362306a36Sopenharmony_ci (match_data_345[0] << 24)); 168462306a36Sopenharmony_ci match->words[1] = (match_data_345[1] | 168562306a36Sopenharmony_ci (match_data_345[2] << 8) | 168662306a36Sopenharmony_ci (data[PTP_V1_SEQUENCE_OFFSET + 168762306a36Sopenharmony_ci PTP_V1_SEQUENCE_LENGTH - 1] << 168862306a36Sopenharmony_ci 16)); 168962306a36Sopenharmony_ci } else { 169062306a36Sopenharmony_ci match->state = PTP_PACKET_STATE_MATCH_UNWANTED; 169162306a36Sopenharmony_ci } 169262306a36Sopenharmony_ci 169362306a36Sopenharmony_ci skb_queue_tail(&ptp->rxq, skb); 169462306a36Sopenharmony_ci queue_work(ptp->workwq, &ptp->work); 169562306a36Sopenharmony_ci 169662306a36Sopenharmony_ci return true; 169762306a36Sopenharmony_ci} 169862306a36Sopenharmony_ci 169962306a36Sopenharmony_ci/* Transmit a PTP packet. This has to be transmitted by the MC 170062306a36Sopenharmony_ci * itself, through an MCDI call. MCDI calls aren't permitted 170162306a36Sopenharmony_ci * in the transmit path so defer the actual transmission to a suitable worker. 170262306a36Sopenharmony_ci */ 170362306a36Sopenharmony_ciint efx_siena_ptp_tx(struct efx_nic *efx, struct sk_buff *skb) 170462306a36Sopenharmony_ci{ 170562306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 170662306a36Sopenharmony_ci 170762306a36Sopenharmony_ci skb_queue_tail(&ptp->txq, skb); 170862306a36Sopenharmony_ci 170962306a36Sopenharmony_ci if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) && 171062306a36Sopenharmony_ci (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM)) 171162306a36Sopenharmony_ci efx_xmit_hwtstamp_pending(skb); 171262306a36Sopenharmony_ci queue_work(ptp->workwq, &ptp->work); 171362306a36Sopenharmony_ci 171462306a36Sopenharmony_ci return NETDEV_TX_OK; 171562306a36Sopenharmony_ci} 171662306a36Sopenharmony_ci 171762306a36Sopenharmony_ciint efx_siena_ptp_get_mode(struct efx_nic *efx) 171862306a36Sopenharmony_ci{ 171962306a36Sopenharmony_ci return efx->ptp_data->mode; 172062306a36Sopenharmony_ci} 172162306a36Sopenharmony_ci 172262306a36Sopenharmony_ciint efx_siena_ptp_change_mode(struct efx_nic *efx, bool enable_wanted, 172362306a36Sopenharmony_ci unsigned int new_mode) 172462306a36Sopenharmony_ci{ 172562306a36Sopenharmony_ci if ((enable_wanted != efx->ptp_data->enabled) || 172662306a36Sopenharmony_ci (enable_wanted && (efx->ptp_data->mode != new_mode))) { 172762306a36Sopenharmony_ci int rc = 0; 172862306a36Sopenharmony_ci 172962306a36Sopenharmony_ci if (enable_wanted) { 173062306a36Sopenharmony_ci /* Change of mode requires disable */ 173162306a36Sopenharmony_ci if (efx->ptp_data->enabled && 173262306a36Sopenharmony_ci (efx->ptp_data->mode != new_mode)) { 173362306a36Sopenharmony_ci efx->ptp_data->enabled = false; 173462306a36Sopenharmony_ci rc = efx_ptp_stop(efx); 173562306a36Sopenharmony_ci if (rc != 0) 173662306a36Sopenharmony_ci return rc; 173762306a36Sopenharmony_ci } 173862306a36Sopenharmony_ci 173962306a36Sopenharmony_ci /* Set new operating mode and establish 174062306a36Sopenharmony_ci * baseline synchronisation, which must 174162306a36Sopenharmony_ci * succeed. 174262306a36Sopenharmony_ci */ 174362306a36Sopenharmony_ci efx->ptp_data->mode = new_mode; 174462306a36Sopenharmony_ci if (netif_running(efx->net_dev)) 174562306a36Sopenharmony_ci rc = efx_ptp_start(efx); 174662306a36Sopenharmony_ci if (rc == 0) { 174762306a36Sopenharmony_ci rc = efx_ptp_synchronize(efx, 174862306a36Sopenharmony_ci PTP_SYNC_ATTEMPTS * 2); 174962306a36Sopenharmony_ci if (rc != 0) 175062306a36Sopenharmony_ci efx_ptp_stop(efx); 175162306a36Sopenharmony_ci } 175262306a36Sopenharmony_ci } else { 175362306a36Sopenharmony_ci rc = efx_ptp_stop(efx); 175462306a36Sopenharmony_ci } 175562306a36Sopenharmony_ci 175662306a36Sopenharmony_ci if (rc != 0) 175762306a36Sopenharmony_ci return rc; 175862306a36Sopenharmony_ci 175962306a36Sopenharmony_ci efx->ptp_data->enabled = enable_wanted; 176062306a36Sopenharmony_ci } 176162306a36Sopenharmony_ci 176262306a36Sopenharmony_ci return 0; 176362306a36Sopenharmony_ci} 176462306a36Sopenharmony_ci 176562306a36Sopenharmony_cistatic int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init) 176662306a36Sopenharmony_ci{ 176762306a36Sopenharmony_ci int rc; 176862306a36Sopenharmony_ci 176962306a36Sopenharmony_ci if ((init->tx_type != HWTSTAMP_TX_OFF) && 177062306a36Sopenharmony_ci (init->tx_type != HWTSTAMP_TX_ON)) 177162306a36Sopenharmony_ci return -ERANGE; 177262306a36Sopenharmony_ci 177362306a36Sopenharmony_ci rc = efx->type->ptp_set_ts_config(efx, init); 177462306a36Sopenharmony_ci if (rc) 177562306a36Sopenharmony_ci return rc; 177662306a36Sopenharmony_ci 177762306a36Sopenharmony_ci efx->ptp_data->config = *init; 177862306a36Sopenharmony_ci return 0; 177962306a36Sopenharmony_ci} 178062306a36Sopenharmony_ci 178162306a36Sopenharmony_civoid efx_siena_ptp_get_ts_info(struct efx_nic *efx, 178262306a36Sopenharmony_ci struct ethtool_ts_info *ts_info) 178362306a36Sopenharmony_ci{ 178462306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 178562306a36Sopenharmony_ci struct efx_nic *primary = efx->primary; 178662306a36Sopenharmony_ci 178762306a36Sopenharmony_ci ASSERT_RTNL(); 178862306a36Sopenharmony_ci 178962306a36Sopenharmony_ci if (!ptp) 179062306a36Sopenharmony_ci return; 179162306a36Sopenharmony_ci 179262306a36Sopenharmony_ci ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE | 179362306a36Sopenharmony_ci SOF_TIMESTAMPING_RX_HARDWARE | 179462306a36Sopenharmony_ci SOF_TIMESTAMPING_RAW_HARDWARE); 179562306a36Sopenharmony_ci if (primary && primary->ptp_data && primary->ptp_data->phc_clock) 179662306a36Sopenharmony_ci ts_info->phc_index = 179762306a36Sopenharmony_ci ptp_clock_index(primary->ptp_data->phc_clock); 179862306a36Sopenharmony_ci ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON; 179962306a36Sopenharmony_ci ts_info->rx_filters = ptp->efx->type->hwtstamp_filters; 180062306a36Sopenharmony_ci} 180162306a36Sopenharmony_ci 180262306a36Sopenharmony_ciint efx_siena_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr) 180362306a36Sopenharmony_ci{ 180462306a36Sopenharmony_ci struct hwtstamp_config config; 180562306a36Sopenharmony_ci int rc; 180662306a36Sopenharmony_ci 180762306a36Sopenharmony_ci /* Not a PTP enabled port */ 180862306a36Sopenharmony_ci if (!efx->ptp_data) 180962306a36Sopenharmony_ci return -EOPNOTSUPP; 181062306a36Sopenharmony_ci 181162306a36Sopenharmony_ci if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 181262306a36Sopenharmony_ci return -EFAULT; 181362306a36Sopenharmony_ci 181462306a36Sopenharmony_ci rc = efx_ptp_ts_init(efx, &config); 181562306a36Sopenharmony_ci if (rc != 0) 181662306a36Sopenharmony_ci return rc; 181762306a36Sopenharmony_ci 181862306a36Sopenharmony_ci return copy_to_user(ifr->ifr_data, &config, sizeof(config)) 181962306a36Sopenharmony_ci ? -EFAULT : 0; 182062306a36Sopenharmony_ci} 182162306a36Sopenharmony_ci 182262306a36Sopenharmony_ciint efx_siena_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr) 182362306a36Sopenharmony_ci{ 182462306a36Sopenharmony_ci if (!efx->ptp_data) 182562306a36Sopenharmony_ci return -EOPNOTSUPP; 182662306a36Sopenharmony_ci 182762306a36Sopenharmony_ci return copy_to_user(ifr->ifr_data, &efx->ptp_data->config, 182862306a36Sopenharmony_ci sizeof(efx->ptp_data->config)) ? -EFAULT : 0; 182962306a36Sopenharmony_ci} 183062306a36Sopenharmony_ci 183162306a36Sopenharmony_cistatic void ptp_event_failure(struct efx_nic *efx, int expected_frag_len) 183262306a36Sopenharmony_ci{ 183362306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 183462306a36Sopenharmony_ci 183562306a36Sopenharmony_ci netif_err(efx, hw, efx->net_dev, 183662306a36Sopenharmony_ci "PTP unexpected event length: got %d expected %d\n", 183762306a36Sopenharmony_ci ptp->evt_frag_idx, expected_frag_len); 183862306a36Sopenharmony_ci ptp->reset_required = true; 183962306a36Sopenharmony_ci queue_work(ptp->workwq, &ptp->work); 184062306a36Sopenharmony_ci} 184162306a36Sopenharmony_ci 184262306a36Sopenharmony_ci/* Process a completed receive event. Put it on the event queue and 184362306a36Sopenharmony_ci * start worker thread. This is required because event and their 184462306a36Sopenharmony_ci * correspoding packets may come in either order. 184562306a36Sopenharmony_ci */ 184662306a36Sopenharmony_cistatic void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp) 184762306a36Sopenharmony_ci{ 184862306a36Sopenharmony_ci struct efx_ptp_event_rx *evt = NULL; 184962306a36Sopenharmony_ci 185062306a36Sopenharmony_ci if (WARN_ON_ONCE(ptp->rx_ts_inline)) 185162306a36Sopenharmony_ci return; 185262306a36Sopenharmony_ci 185362306a36Sopenharmony_ci if (ptp->evt_frag_idx != 3) { 185462306a36Sopenharmony_ci ptp_event_failure(efx, 3); 185562306a36Sopenharmony_ci return; 185662306a36Sopenharmony_ci } 185762306a36Sopenharmony_ci 185862306a36Sopenharmony_ci spin_lock_bh(&ptp->evt_lock); 185962306a36Sopenharmony_ci if (!list_empty(&ptp->evt_free_list)) { 186062306a36Sopenharmony_ci evt = list_first_entry(&ptp->evt_free_list, 186162306a36Sopenharmony_ci struct efx_ptp_event_rx, link); 186262306a36Sopenharmony_ci list_del(&evt->link); 186362306a36Sopenharmony_ci 186462306a36Sopenharmony_ci evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA); 186562306a36Sopenharmony_ci evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2], 186662306a36Sopenharmony_ci MCDI_EVENT_SRC) | 186762306a36Sopenharmony_ci (EFX_QWORD_FIELD(ptp->evt_frags[1], 186862306a36Sopenharmony_ci MCDI_EVENT_SRC) << 8) | 186962306a36Sopenharmony_ci (EFX_QWORD_FIELD(ptp->evt_frags[0], 187062306a36Sopenharmony_ci MCDI_EVENT_SRC) << 16)); 187162306a36Sopenharmony_ci evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time( 187262306a36Sopenharmony_ci EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA), 187362306a36Sopenharmony_ci EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA), 187462306a36Sopenharmony_ci ptp->ts_corrections.ptp_rx); 187562306a36Sopenharmony_ci evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS); 187662306a36Sopenharmony_ci list_add_tail(&evt->link, &ptp->evt_list); 187762306a36Sopenharmony_ci 187862306a36Sopenharmony_ci queue_work(ptp->workwq, &ptp->work); 187962306a36Sopenharmony_ci } else if (net_ratelimit()) { 188062306a36Sopenharmony_ci /* Log a rate-limited warning message. */ 188162306a36Sopenharmony_ci netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n"); 188262306a36Sopenharmony_ci } 188362306a36Sopenharmony_ci spin_unlock_bh(&ptp->evt_lock); 188462306a36Sopenharmony_ci} 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_cistatic void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp) 188762306a36Sopenharmony_ci{ 188862306a36Sopenharmony_ci int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA); 188962306a36Sopenharmony_ci if (ptp->evt_frag_idx != 1) { 189062306a36Sopenharmony_ci ptp_event_failure(efx, 1); 189162306a36Sopenharmony_ci return; 189262306a36Sopenharmony_ci } 189362306a36Sopenharmony_ci 189462306a36Sopenharmony_ci netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code); 189562306a36Sopenharmony_ci} 189662306a36Sopenharmony_ci 189762306a36Sopenharmony_cistatic void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp) 189862306a36Sopenharmony_ci{ 189962306a36Sopenharmony_ci if (ptp->nic_ts_enabled) 190062306a36Sopenharmony_ci queue_work(ptp->pps_workwq, &ptp->pps_work); 190162306a36Sopenharmony_ci} 190262306a36Sopenharmony_ci 190362306a36Sopenharmony_civoid efx_siena_ptp_event(struct efx_nic *efx, efx_qword_t *ev) 190462306a36Sopenharmony_ci{ 190562306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 190662306a36Sopenharmony_ci int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE); 190762306a36Sopenharmony_ci 190862306a36Sopenharmony_ci if (!ptp) { 190962306a36Sopenharmony_ci if (!efx->ptp_warned) { 191062306a36Sopenharmony_ci netif_warn(efx, drv, efx->net_dev, 191162306a36Sopenharmony_ci "Received PTP event but PTP not set up\n"); 191262306a36Sopenharmony_ci efx->ptp_warned = true; 191362306a36Sopenharmony_ci } 191462306a36Sopenharmony_ci return; 191562306a36Sopenharmony_ci } 191662306a36Sopenharmony_ci 191762306a36Sopenharmony_ci if (!ptp->enabled) 191862306a36Sopenharmony_ci return; 191962306a36Sopenharmony_ci 192062306a36Sopenharmony_ci if (ptp->evt_frag_idx == 0) { 192162306a36Sopenharmony_ci ptp->evt_code = code; 192262306a36Sopenharmony_ci } else if (ptp->evt_code != code) { 192362306a36Sopenharmony_ci netif_err(efx, hw, efx->net_dev, 192462306a36Sopenharmony_ci "PTP out of sequence event %d\n", code); 192562306a36Sopenharmony_ci ptp->evt_frag_idx = 0; 192662306a36Sopenharmony_ci } 192762306a36Sopenharmony_ci 192862306a36Sopenharmony_ci ptp->evt_frags[ptp->evt_frag_idx++] = *ev; 192962306a36Sopenharmony_ci if (!MCDI_EVENT_FIELD(*ev, CONT)) { 193062306a36Sopenharmony_ci /* Process resulting event */ 193162306a36Sopenharmony_ci switch (code) { 193262306a36Sopenharmony_ci case MCDI_EVENT_CODE_PTP_RX: 193362306a36Sopenharmony_ci ptp_event_rx(efx, ptp); 193462306a36Sopenharmony_ci break; 193562306a36Sopenharmony_ci case MCDI_EVENT_CODE_PTP_FAULT: 193662306a36Sopenharmony_ci ptp_event_fault(efx, ptp); 193762306a36Sopenharmony_ci break; 193862306a36Sopenharmony_ci case MCDI_EVENT_CODE_PTP_PPS: 193962306a36Sopenharmony_ci ptp_event_pps(efx, ptp); 194062306a36Sopenharmony_ci break; 194162306a36Sopenharmony_ci default: 194262306a36Sopenharmony_ci netif_err(efx, hw, efx->net_dev, 194362306a36Sopenharmony_ci "PTP unknown event %d\n", code); 194462306a36Sopenharmony_ci break; 194562306a36Sopenharmony_ci } 194662306a36Sopenharmony_ci ptp->evt_frag_idx = 0; 194762306a36Sopenharmony_ci } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) { 194862306a36Sopenharmony_ci netif_err(efx, hw, efx->net_dev, 194962306a36Sopenharmony_ci "PTP too many event fragments\n"); 195062306a36Sopenharmony_ci ptp->evt_frag_idx = 0; 195162306a36Sopenharmony_ci } 195262306a36Sopenharmony_ci} 195362306a36Sopenharmony_ci 195462306a36Sopenharmony_civoid efx_siena_time_sync_event(struct efx_channel *channel, efx_qword_t *ev) 195562306a36Sopenharmony_ci{ 195662306a36Sopenharmony_ci struct efx_nic *efx = channel->efx; 195762306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 195862306a36Sopenharmony_ci 195962306a36Sopenharmony_ci /* When extracting the sync timestamp minor value, we should discard 196062306a36Sopenharmony_ci * the least significant two bits. These are not required in order 196162306a36Sopenharmony_ci * to reconstruct full-range timestamps and they are optionally used 196262306a36Sopenharmony_ci * to report status depending on the options supplied when subscribing 196362306a36Sopenharmony_ci * for sync events. 196462306a36Sopenharmony_ci */ 196562306a36Sopenharmony_ci channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR); 196662306a36Sopenharmony_ci channel->sync_timestamp_minor = 196762306a36Sopenharmony_ci (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC) 196862306a36Sopenharmony_ci << ptp->nic_time.sync_event_minor_shift; 196962306a36Sopenharmony_ci 197062306a36Sopenharmony_ci /* if sync events have been disabled then we want to silently ignore 197162306a36Sopenharmony_ci * this event, so throw away result. 197262306a36Sopenharmony_ci */ 197362306a36Sopenharmony_ci (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED, 197462306a36Sopenharmony_ci SYNC_EVENTS_VALID); 197562306a36Sopenharmony_ci} 197662306a36Sopenharmony_ci 197762306a36Sopenharmony_cistatic inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh) 197862306a36Sopenharmony_ci{ 197962306a36Sopenharmony_ci#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 198062306a36Sopenharmony_ci return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset)); 198162306a36Sopenharmony_ci#else 198262306a36Sopenharmony_ci const u8 *data = eh + efx->rx_packet_ts_offset; 198362306a36Sopenharmony_ci return (u32)data[0] | 198462306a36Sopenharmony_ci (u32)data[1] << 8 | 198562306a36Sopenharmony_ci (u32)data[2] << 16 | 198662306a36Sopenharmony_ci (u32)data[3] << 24; 198762306a36Sopenharmony_ci#endif 198862306a36Sopenharmony_ci} 198962306a36Sopenharmony_ci 199062306a36Sopenharmony_civoid __efx_siena_rx_skb_attach_timestamp(struct efx_channel *channel, 199162306a36Sopenharmony_ci struct sk_buff *skb) 199262306a36Sopenharmony_ci{ 199362306a36Sopenharmony_ci struct efx_nic *efx = channel->efx; 199462306a36Sopenharmony_ci struct efx_ptp_data *ptp = efx->ptp_data; 199562306a36Sopenharmony_ci u32 pkt_timestamp_major, pkt_timestamp_minor; 199662306a36Sopenharmony_ci u32 diff, carry; 199762306a36Sopenharmony_ci struct skb_shared_hwtstamps *timestamps; 199862306a36Sopenharmony_ci 199962306a36Sopenharmony_ci if (channel->sync_events_state != SYNC_EVENTS_VALID) 200062306a36Sopenharmony_ci return; 200162306a36Sopenharmony_ci 200262306a36Sopenharmony_ci pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb)); 200362306a36Sopenharmony_ci 200462306a36Sopenharmony_ci /* get the difference between the packet and sync timestamps, 200562306a36Sopenharmony_ci * modulo one second 200662306a36Sopenharmony_ci */ 200762306a36Sopenharmony_ci diff = pkt_timestamp_minor - channel->sync_timestamp_minor; 200862306a36Sopenharmony_ci if (pkt_timestamp_minor < channel->sync_timestamp_minor) 200962306a36Sopenharmony_ci diff += ptp->nic_time.minor_max; 201062306a36Sopenharmony_ci 201162306a36Sopenharmony_ci /* do we roll over a second boundary and need to carry the one? */ 201262306a36Sopenharmony_ci carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ? 201362306a36Sopenharmony_ci 1 : 0; 201462306a36Sopenharmony_ci 201562306a36Sopenharmony_ci if (diff <= ptp->nic_time.sync_event_diff_max) { 201662306a36Sopenharmony_ci /* packet is ahead of the sync event by a quarter of a second or 201762306a36Sopenharmony_ci * less (allowing for fuzz) 201862306a36Sopenharmony_ci */ 201962306a36Sopenharmony_ci pkt_timestamp_major = channel->sync_timestamp_major + carry; 202062306a36Sopenharmony_ci } else if (diff >= ptp->nic_time.sync_event_diff_min) { 202162306a36Sopenharmony_ci /* packet is behind the sync event but within the fuzz factor. 202262306a36Sopenharmony_ci * This means the RX packet and sync event crossed as they were 202362306a36Sopenharmony_ci * placed on the event queue, which can sometimes happen. 202462306a36Sopenharmony_ci */ 202562306a36Sopenharmony_ci pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry; 202662306a36Sopenharmony_ci } else { 202762306a36Sopenharmony_ci /* it's outside tolerance in both directions. this might be 202862306a36Sopenharmony_ci * indicative of us missing sync events for some reason, so 202962306a36Sopenharmony_ci * we'll call it an error rather than risk giving a bogus 203062306a36Sopenharmony_ci * timestamp. 203162306a36Sopenharmony_ci */ 203262306a36Sopenharmony_ci netif_vdbg(efx, drv, efx->net_dev, 203362306a36Sopenharmony_ci "packet timestamp %x too far from sync event %x:%x\n", 203462306a36Sopenharmony_ci pkt_timestamp_minor, channel->sync_timestamp_major, 203562306a36Sopenharmony_ci channel->sync_timestamp_minor); 203662306a36Sopenharmony_ci return; 203762306a36Sopenharmony_ci } 203862306a36Sopenharmony_ci 203962306a36Sopenharmony_ci /* attach the timestamps to the skb */ 204062306a36Sopenharmony_ci timestamps = skb_hwtstamps(skb); 204162306a36Sopenharmony_ci timestamps->hwtstamp = 204262306a36Sopenharmony_ci ptp->nic_to_kernel_time(pkt_timestamp_major, 204362306a36Sopenharmony_ci pkt_timestamp_minor, 204462306a36Sopenharmony_ci ptp->ts_corrections.general_rx); 204562306a36Sopenharmony_ci} 204662306a36Sopenharmony_ci 204762306a36Sopenharmony_cistatic int efx_phc_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 204862306a36Sopenharmony_ci{ 204962306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = container_of(ptp, 205062306a36Sopenharmony_ci struct efx_ptp_data, 205162306a36Sopenharmony_ci phc_clock_info); 205262306a36Sopenharmony_ci s32 delta = scaled_ppm_to_ppb(scaled_ppm); 205362306a36Sopenharmony_ci struct efx_nic *efx = ptp_data->efx; 205462306a36Sopenharmony_ci MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN); 205562306a36Sopenharmony_ci s64 adjustment_ns; 205662306a36Sopenharmony_ci int rc; 205762306a36Sopenharmony_ci 205862306a36Sopenharmony_ci if (delta > MAX_PPB) 205962306a36Sopenharmony_ci delta = MAX_PPB; 206062306a36Sopenharmony_ci else if (delta < -MAX_PPB) 206162306a36Sopenharmony_ci delta = -MAX_PPB; 206262306a36Sopenharmony_ci 206362306a36Sopenharmony_ci /* Convert ppb to fixed point ns taking care to round correctly. */ 206462306a36Sopenharmony_ci adjustment_ns = ((s64)delta * PPB_SCALE_WORD + 206562306a36Sopenharmony_ci (1 << (ptp_data->adjfreq_ppb_shift - 1))) >> 206662306a36Sopenharmony_ci ptp_data->adjfreq_ppb_shift; 206762306a36Sopenharmony_ci 206862306a36Sopenharmony_ci MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST); 206962306a36Sopenharmony_ci MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0); 207062306a36Sopenharmony_ci MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns); 207162306a36Sopenharmony_ci MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0); 207262306a36Sopenharmony_ci MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0); 207362306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj), 207462306a36Sopenharmony_ci NULL, 0, NULL); 207562306a36Sopenharmony_ci if (rc != 0) 207662306a36Sopenharmony_ci return rc; 207762306a36Sopenharmony_ci 207862306a36Sopenharmony_ci ptp_data->current_adjfreq = adjustment_ns; 207962306a36Sopenharmony_ci return 0; 208062306a36Sopenharmony_ci} 208162306a36Sopenharmony_ci 208262306a36Sopenharmony_cistatic int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta) 208362306a36Sopenharmony_ci{ 208462306a36Sopenharmony_ci u32 nic_major, nic_minor; 208562306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = container_of(ptp, 208662306a36Sopenharmony_ci struct efx_ptp_data, 208762306a36Sopenharmony_ci phc_clock_info); 208862306a36Sopenharmony_ci struct efx_nic *efx = ptp_data->efx; 208962306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN); 209062306a36Sopenharmony_ci 209162306a36Sopenharmony_ci efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor); 209262306a36Sopenharmony_ci 209362306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST); 209462306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 209562306a36Sopenharmony_ci MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq); 209662306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major); 209762306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor); 209862306a36Sopenharmony_ci return efx_siena_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 209962306a36Sopenharmony_ci NULL, 0, NULL); 210062306a36Sopenharmony_ci} 210162306a36Sopenharmony_ci 210262306a36Sopenharmony_cistatic int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) 210362306a36Sopenharmony_ci{ 210462306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = container_of(ptp, 210562306a36Sopenharmony_ci struct efx_ptp_data, 210662306a36Sopenharmony_ci phc_clock_info); 210762306a36Sopenharmony_ci struct efx_nic *efx = ptp_data->efx; 210862306a36Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN); 210962306a36Sopenharmony_ci MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN); 211062306a36Sopenharmony_ci int rc; 211162306a36Sopenharmony_ci ktime_t kt; 211262306a36Sopenharmony_ci 211362306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME); 211462306a36Sopenharmony_ci MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); 211562306a36Sopenharmony_ci 211662306a36Sopenharmony_ci rc = efx_siena_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), 211762306a36Sopenharmony_ci outbuf, sizeof(outbuf), NULL); 211862306a36Sopenharmony_ci if (rc != 0) 211962306a36Sopenharmony_ci return rc; 212062306a36Sopenharmony_ci 212162306a36Sopenharmony_ci kt = ptp_data->nic_to_kernel_time( 212262306a36Sopenharmony_ci MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR), 212362306a36Sopenharmony_ci MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0); 212462306a36Sopenharmony_ci *ts = ktime_to_timespec64(kt); 212562306a36Sopenharmony_ci return 0; 212662306a36Sopenharmony_ci} 212762306a36Sopenharmony_ci 212862306a36Sopenharmony_cistatic int efx_phc_settime(struct ptp_clock_info *ptp, 212962306a36Sopenharmony_ci const struct timespec64 *e_ts) 213062306a36Sopenharmony_ci{ 213162306a36Sopenharmony_ci /* Get the current NIC time, efx_phc_gettime. 213262306a36Sopenharmony_ci * Subtract from the desired time to get the offset 213362306a36Sopenharmony_ci * call efx_phc_adjtime with the offset 213462306a36Sopenharmony_ci */ 213562306a36Sopenharmony_ci int rc; 213662306a36Sopenharmony_ci struct timespec64 time_now; 213762306a36Sopenharmony_ci struct timespec64 delta; 213862306a36Sopenharmony_ci 213962306a36Sopenharmony_ci rc = efx_phc_gettime(ptp, &time_now); 214062306a36Sopenharmony_ci if (rc != 0) 214162306a36Sopenharmony_ci return rc; 214262306a36Sopenharmony_ci 214362306a36Sopenharmony_ci delta = timespec64_sub(*e_ts, time_now); 214462306a36Sopenharmony_ci 214562306a36Sopenharmony_ci rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta)); 214662306a36Sopenharmony_ci if (rc != 0) 214762306a36Sopenharmony_ci return rc; 214862306a36Sopenharmony_ci 214962306a36Sopenharmony_ci return 0; 215062306a36Sopenharmony_ci} 215162306a36Sopenharmony_ci 215262306a36Sopenharmony_cistatic int efx_phc_enable(struct ptp_clock_info *ptp, 215362306a36Sopenharmony_ci struct ptp_clock_request *request, 215462306a36Sopenharmony_ci int enable) 215562306a36Sopenharmony_ci{ 215662306a36Sopenharmony_ci struct efx_ptp_data *ptp_data = container_of(ptp, 215762306a36Sopenharmony_ci struct efx_ptp_data, 215862306a36Sopenharmony_ci phc_clock_info); 215962306a36Sopenharmony_ci if (request->type != PTP_CLK_REQ_PPS) 216062306a36Sopenharmony_ci return -EOPNOTSUPP; 216162306a36Sopenharmony_ci 216262306a36Sopenharmony_ci ptp_data->nic_ts_enabled = !!enable; 216362306a36Sopenharmony_ci return 0; 216462306a36Sopenharmony_ci} 216562306a36Sopenharmony_ci 216662306a36Sopenharmony_cistatic const struct efx_channel_type efx_ptp_channel_type = { 216762306a36Sopenharmony_ci .handle_no_channel = efx_ptp_handle_no_channel, 216862306a36Sopenharmony_ci .pre_probe = efx_ptp_probe_channel, 216962306a36Sopenharmony_ci .post_remove = efx_ptp_remove_channel, 217062306a36Sopenharmony_ci .get_name = efx_ptp_get_channel_name, 217162306a36Sopenharmony_ci /* no copy operation; there is no need to reallocate this channel */ 217262306a36Sopenharmony_ci .receive_skb = efx_ptp_rx, 217362306a36Sopenharmony_ci .want_txqs = efx_ptp_want_txqs, 217462306a36Sopenharmony_ci .keep_eventq = false, 217562306a36Sopenharmony_ci}; 217662306a36Sopenharmony_ci 217762306a36Sopenharmony_civoid efx_siena_ptp_defer_probe_with_channel(struct efx_nic *efx) 217862306a36Sopenharmony_ci{ 217962306a36Sopenharmony_ci /* Check whether PTP is implemented on this NIC. The DISABLE 218062306a36Sopenharmony_ci * operation will succeed if and only if it is implemented. 218162306a36Sopenharmony_ci */ 218262306a36Sopenharmony_ci if (efx_ptp_disable(efx) == 0) 218362306a36Sopenharmony_ci efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] = 218462306a36Sopenharmony_ci &efx_ptp_channel_type; 218562306a36Sopenharmony_ci} 218662306a36Sopenharmony_ci 218762306a36Sopenharmony_civoid efx_siena_ptp_start_datapath(struct efx_nic *efx) 218862306a36Sopenharmony_ci{ 218962306a36Sopenharmony_ci if (efx_ptp_restart(efx)) 219062306a36Sopenharmony_ci netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n"); 219162306a36Sopenharmony_ci /* re-enable timestamping if it was previously enabled */ 219262306a36Sopenharmony_ci if (efx->type->ptp_set_ts_sync_events) 219362306a36Sopenharmony_ci efx->type->ptp_set_ts_sync_events(efx, true, true); 219462306a36Sopenharmony_ci} 219562306a36Sopenharmony_ci 219662306a36Sopenharmony_civoid efx_siena_ptp_stop_datapath(struct efx_nic *efx) 219762306a36Sopenharmony_ci{ 219862306a36Sopenharmony_ci /* temporarily disable timestamping */ 219962306a36Sopenharmony_ci if (efx->type->ptp_set_ts_sync_events) 220062306a36Sopenharmony_ci efx->type->ptp_set_ts_sync_events(efx, false, true); 220162306a36Sopenharmony_ci efx_ptp_stop(efx); 220262306a36Sopenharmony_ci} 2203