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