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