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