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