Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
Ben Hutchings | f7a6d2c | 2013-08-29 23:32:48 +0100 | [diff] [blame] | 2 | * Driver for Solarflare network controllers and boards |
| 3 | * Copyright 2011-2013 Solarflare Communications Inc. |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 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" |
Ben Hutchings | 8b8a95a | 2012-09-18 01:57:07 +0100 | [diff] [blame] | 49 | #include "farch_regs.h" |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 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 */ |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 65 | #define DEFAULT_MIN_SYNCHRONISATION_NS 120 |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 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 | |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 102 | #define PTP_V2_UUID_LENGTH 8 |
| 103 | #define PTP_V2_UUID_OFFSET 48 |
| 104 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 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 |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 198 | * @major: Hardware timestamp, major |
| 199 | * @minor: Hardware timestamp, minor |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 200 | * @host_end: Host time immediately after hardware timestamp taken |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 201 | * @wait: Number of NIC clock ticks between hardware timestamp being read and |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 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; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 208 | u32 major; |
| 209 | u32 minor; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 210 | u32 host_end; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 211 | u32 wait; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 212 | u32 window; /* Derived: end - start, allowing for wrap */ |
| 213 | }; |
| 214 | |
| 215 | /** |
| 216 | * struct efx_ptp_data - Precision Time Protocol (PTP) state |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 217 | * @efx: The NIC context |
| 218 | * @channel: The PTP channel (Siena only) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 219 | * @rxq: Receive queue (awaiting timestamps) |
| 220 | * @txq: Transmit queue |
| 221 | * @evt_list: List of MC receive events awaiting packets |
| 222 | * @evt_free_list: List of free events |
| 223 | * @evt_lock: Lock for manipulating evt_list and evt_free_list |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 224 | * @evt_overflow: Boolean indicating that event list has overflowed |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 225 | * @rx_evts: Instantiated events (on evt_list and evt_free_list) |
| 226 | * @workwq: Work queue for processing pending PTP operations |
| 227 | * @work: Work task |
| 228 | * @reset_required: A serious error has occurred and the PTP task needs to be |
| 229 | * reset (disable, enable). |
| 230 | * @rxfilter_event: Receive filter when operating |
| 231 | * @rxfilter_general: Receive filter when operating |
| 232 | * @config: Current timestamp configuration |
| 233 | * @enabled: PTP operation enabled |
| 234 | * @mode: Mode in which PTP operating (PTP version) |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 235 | * @time_format: Time format supported by this NIC |
| 236 | * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time |
| 237 | * @nic_to_kernel_time: Function to convert from NIC to kernel time |
| 238 | * @min_synchronisation_ns: Minimum acceptable corrected sync window |
| 239 | * @ts_corrections.tx: Required driver correction of transmit timestamps |
| 240 | * @ts_corrections.rx: Required driver correction of receive timestamps |
| 241 | * @ts_corrections.pps_out: PPS output error (information only) |
| 242 | * @ts_corrections.pps_in: Required driver correction of PPS input timestamps |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 243 | * @evt_frags: Partly assembled PTP events |
| 244 | * @evt_frag_idx: Current fragment number |
| 245 | * @evt_code: Last event code |
| 246 | * @start: Address at which MC indicates ready for synchronisation |
| 247 | * @host_time_pps: Host time at last PPS |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 248 | * @current_adjfreq: Current ppb adjustment. |
| 249 | * @phc_clock: Pointer to registered phc device |
| 250 | * @phc_clock_info: Registration structure for phc device |
| 251 | * @pps_work: pps work task for handling pps events |
| 252 | * @pps_workwq: pps work queue |
| 253 | * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled |
| 254 | * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids |
| 255 | * allocations in main data path). |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 256 | * @timeset: Last set of synchronisation statistics. |
| 257 | */ |
| 258 | struct efx_ptp_data { |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 259 | struct efx_nic *efx; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 260 | struct efx_channel *channel; |
| 261 | struct sk_buff_head rxq; |
| 262 | struct sk_buff_head txq; |
| 263 | struct list_head evt_list; |
| 264 | struct list_head evt_free_list; |
| 265 | spinlock_t evt_lock; |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 266 | bool evt_overflow; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 267 | struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS]; |
| 268 | struct workqueue_struct *workwq; |
| 269 | struct work_struct work; |
| 270 | bool reset_required; |
| 271 | u32 rxfilter_event; |
| 272 | u32 rxfilter_general; |
| 273 | bool rxfilter_installed; |
| 274 | struct hwtstamp_config config; |
| 275 | bool enabled; |
| 276 | unsigned int mode; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 277 | unsigned int time_format; |
| 278 | void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor); |
| 279 | ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor, |
| 280 | s32 correction); |
| 281 | unsigned int min_synchronisation_ns; |
| 282 | struct { |
| 283 | s32 tx; |
| 284 | s32 rx; |
| 285 | s32 pps_out; |
| 286 | s32 pps_in; |
| 287 | } ts_corrections; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 288 | efx_qword_t evt_frags[MAX_EVENT_FRAGS]; |
| 289 | int evt_frag_idx; |
| 290 | int evt_code; |
| 291 | struct efx_buffer start; |
| 292 | struct pps_event_time host_time_pps; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 293 | s64 current_adjfreq; |
| 294 | struct ptp_clock *phc_clock; |
| 295 | struct ptp_clock_info phc_clock_info; |
| 296 | struct work_struct pps_work; |
| 297 | struct workqueue_struct *pps_workwq; |
| 298 | bool nic_ts_enabled; |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 299 | MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 300 | struct efx_ptp_timeset |
| 301 | timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM]; |
| 302 | }; |
| 303 | |
| 304 | static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta); |
| 305 | static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta); |
| 306 | static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts); |
| 307 | static int efx_phc_settime(struct ptp_clock_info *ptp, |
| 308 | const struct timespec *e_ts); |
| 309 | static int efx_phc_enable(struct ptp_clock_info *ptp, |
| 310 | struct ptp_clock_request *request, int on); |
| 311 | |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 312 | /* For Siena platforms NIC time is s and ns */ |
| 313 | static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor) |
| 314 | { |
| 315 | struct timespec ts = ns_to_timespec(ns); |
| 316 | *nic_major = ts.tv_sec; |
| 317 | *nic_minor = ts.tv_nsec; |
| 318 | } |
| 319 | |
| 320 | static ktime_t efx_ptp_s_ns_to_ktime(u32 nic_major, u32 nic_minor, |
| 321 | s32 correction) |
| 322 | { |
| 323 | ktime_t kt = ktime_set(nic_major, nic_minor); |
| 324 | if (correction >= 0) |
| 325 | kt = ktime_add_ns(kt, (u64)correction); |
| 326 | else |
| 327 | kt = ktime_sub_ns(kt, (u64)-correction); |
| 328 | return kt; |
| 329 | } |
| 330 | |
| 331 | /* To convert from s27 format to ns we multiply then divide by a power of 2. |
| 332 | * For the conversion from ns to s27, the operation is also converted to a |
| 333 | * multiply and shift. |
| 334 | */ |
| 335 | #define S27_TO_NS_SHIFT (27) |
| 336 | #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC) |
| 337 | #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT) |
| 338 | #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT) |
| 339 | |
| 340 | /* For Huntington platforms NIC time is in seconds and fractions of a second |
| 341 | * where the minor register only uses 27 bits in units of 2^-27s. |
| 342 | */ |
| 343 | static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor) |
| 344 | { |
| 345 | struct timespec ts = ns_to_timespec(ns); |
| 346 | u32 maj = ts.tv_sec; |
| 347 | u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT + |
| 348 | (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT); |
| 349 | |
| 350 | /* The conversion can result in the minor value exceeding the maximum. |
| 351 | * In this case, round up to the next second. |
| 352 | */ |
| 353 | if (min >= S27_MINOR_MAX) { |
| 354 | min -= S27_MINOR_MAX; |
| 355 | maj++; |
| 356 | } |
| 357 | |
| 358 | *nic_major = maj; |
| 359 | *nic_minor = min; |
| 360 | } |
| 361 | |
| 362 | static ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor, |
| 363 | s32 correction) |
| 364 | { |
| 365 | u32 ns; |
| 366 | |
| 367 | /* Apply the correction and deal with carry */ |
| 368 | nic_minor += correction; |
| 369 | if ((s32)nic_minor < 0) { |
| 370 | nic_minor += S27_MINOR_MAX; |
| 371 | nic_major--; |
| 372 | } else if (nic_minor >= S27_MINOR_MAX) { |
| 373 | nic_minor -= S27_MINOR_MAX; |
| 374 | nic_major++; |
| 375 | } |
| 376 | |
| 377 | ns = (u32)(((u64)nic_minor * NSEC_PER_SEC + |
| 378 | (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT); |
| 379 | |
| 380 | return ktime_set(nic_major, ns); |
| 381 | } |
| 382 | |
| 383 | /* Get PTP attributes and set up time conversions */ |
| 384 | static int efx_ptp_get_attributes(struct efx_nic *efx) |
| 385 | { |
| 386 | MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN); |
| 387 | MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN); |
| 388 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 389 | int rc; |
| 390 | u32 fmt; |
| 391 | size_t out_len; |
| 392 | |
| 393 | /* Get the PTP attributes. If the NIC doesn't support the operation we |
| 394 | * use the default format for compatibility with older NICs i.e. |
| 395 | * seconds and nanoseconds. |
| 396 | */ |
| 397 | MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES); |
| 398 | MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); |
| 399 | rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), |
| 400 | outbuf, sizeof(outbuf), &out_len); |
| 401 | if (rc == 0) |
| 402 | fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT); |
| 403 | else if (rc == -EINVAL) |
| 404 | fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS; |
| 405 | else |
| 406 | return rc; |
| 407 | |
| 408 | if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION) { |
| 409 | ptp->ns_to_nic_time = efx_ptp_ns_to_s27; |
| 410 | ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime; |
| 411 | } else if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS) { |
| 412 | ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns; |
| 413 | ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime; |
| 414 | } else { |
| 415 | return -ERANGE; |
| 416 | } |
| 417 | |
| 418 | ptp->time_format = fmt; |
| 419 | |
| 420 | /* MC_CMD_PTP_OP_GET_ATTRIBUTES is an extended version of an older |
| 421 | * operation MC_CMD_PTP_OP_GET_TIME_FORMAT that also returns a value |
| 422 | * to use for the minimum acceptable corrected synchronization window. |
| 423 | * If we have the extra information store it. For older firmware that |
| 424 | * does not implement the extended command use the default value. |
| 425 | */ |
| 426 | if (rc == 0 && out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN) |
| 427 | ptp->min_synchronisation_ns = |
| 428 | MCDI_DWORD(outbuf, |
| 429 | PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN); |
| 430 | else |
| 431 | ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS; |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* Get PTP timestamp corrections */ |
| 437 | static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx) |
| 438 | { |
| 439 | MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN); |
| 440 | MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_LEN); |
| 441 | int rc; |
| 442 | |
| 443 | /* Get the timestamp corrections from the NIC. If this operation is |
| 444 | * not supported (older NICs) then no correction is required. |
| 445 | */ |
| 446 | MCDI_SET_DWORD(inbuf, PTP_IN_OP, |
| 447 | MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS); |
| 448 | MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); |
| 449 | |
| 450 | rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), |
| 451 | outbuf, sizeof(outbuf), NULL); |
| 452 | if (rc == 0) { |
| 453 | efx->ptp_data->ts_corrections.tx = MCDI_DWORD(outbuf, |
| 454 | PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT); |
| 455 | efx->ptp_data->ts_corrections.rx = MCDI_DWORD(outbuf, |
| 456 | PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE); |
| 457 | efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf, |
| 458 | PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT); |
| 459 | efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf, |
| 460 | PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN); |
| 461 | } else if (rc == -EINVAL) { |
| 462 | efx->ptp_data->ts_corrections.tx = 0; |
| 463 | efx->ptp_data->ts_corrections.rx = 0; |
| 464 | efx->ptp_data->ts_corrections.pps_out = 0; |
| 465 | efx->ptp_data->ts_corrections.pps_in = 0; |
| 466 | } else { |
| 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 473 | /* Enable MCDI PTP support. */ |
| 474 | static int efx_ptp_enable(struct efx_nic *efx) |
| 475 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 476 | MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN); |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 477 | MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0); |
| 478 | int rc; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 479 | |
| 480 | MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 481 | MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 482 | MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE, |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 483 | efx->ptp_data->channel ? |
| 484 | efx->ptp_data->channel->channel : 0); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 485 | MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode); |
| 486 | |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 487 | rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), |
| 488 | outbuf, sizeof(outbuf), NULL); |
| 489 | rc = (rc == -EALREADY) ? 0 : rc; |
| 490 | if (rc) |
| 491 | efx_mcdi_display_error(efx, MC_CMD_PTP, |
| 492 | MC_CMD_PTP_IN_ENABLE_LEN, |
| 493 | outbuf, sizeof(outbuf), rc); |
| 494 | return rc; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* Disable MCDI PTP support. |
| 498 | * |
| 499 | * Note that this function should never rely on the presence of ptp_data - |
| 500 | * may be called before that exists. |
| 501 | */ |
| 502 | static int efx_ptp_disable(struct efx_nic *efx) |
| 503 | { |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 504 | MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN); |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 505 | MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0); |
| 506 | int rc; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 507 | |
| 508 | MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 509 | MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); |
Edward Cree | 1e0b812 | 2013-05-31 18:36:12 +0100 | [diff] [blame] | 510 | rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), |
| 511 | outbuf, sizeof(outbuf), NULL); |
| 512 | rc = (rc == -EALREADY) ? 0 : rc; |
| 513 | if (rc) |
| 514 | efx_mcdi_display_error(efx, MC_CMD_PTP, |
| 515 | MC_CMD_PTP_IN_DISABLE_LEN, |
| 516 | outbuf, sizeof(outbuf), rc); |
| 517 | return rc; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q) |
| 521 | { |
| 522 | struct sk_buff *skb; |
| 523 | |
| 524 | while ((skb = skb_dequeue(q))) { |
| 525 | local_bh_disable(); |
| 526 | netif_receive_skb(skb); |
| 527 | local_bh_enable(); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static void efx_ptp_handle_no_channel(struct efx_nic *efx) |
| 532 | { |
| 533 | netif_err(efx, drv, efx->net_dev, |
| 534 | "ERROR: PTP requires MSI-X and 1 additional interrupt" |
| 535 | "vector. PTP disabled\n"); |
| 536 | } |
| 537 | |
| 538 | /* Repeatedly send the host time to the MC which will capture the hardware |
| 539 | * time. |
| 540 | */ |
| 541 | static void efx_ptp_send_times(struct efx_nic *efx, |
| 542 | struct pps_event_time *last_time) |
| 543 | { |
| 544 | struct pps_event_time now; |
| 545 | struct timespec limit; |
| 546 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 547 | struct timespec start; |
| 548 | int *mc_running = ptp->start.addr; |
| 549 | |
| 550 | pps_get_ts(&now); |
| 551 | start = now.ts_real; |
| 552 | limit = now.ts_real; |
| 553 | timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS); |
| 554 | |
| 555 | /* Write host time for specified period or until MC is done */ |
| 556 | while ((timespec_compare(&now.ts_real, &limit) < 0) && |
| 557 | ACCESS_ONCE(*mc_running)) { |
| 558 | struct timespec update_time; |
| 559 | unsigned int host_time; |
| 560 | |
| 561 | /* Don't update continuously to avoid saturating the PCIe bus */ |
| 562 | update_time = now.ts_real; |
| 563 | timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS); |
| 564 | do { |
| 565 | pps_get_ts(&now); |
| 566 | } while ((timespec_compare(&now.ts_real, &update_time) < 0) && |
| 567 | ACCESS_ONCE(*mc_running)); |
| 568 | |
| 569 | /* Synchronise NIC with single word of time only */ |
| 570 | host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS | |
| 571 | now.ts_real.tv_nsec); |
| 572 | /* Update host time in NIC memory */ |
Laurence Evans | 977a5d5 | 2013-03-07 11:46:58 +0000 | [diff] [blame] | 573 | efx->type->ptp_write_host_time(efx, host_time); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 574 | } |
| 575 | *last_time = now; |
| 576 | } |
| 577 | |
| 578 | /* Read a timeset from the MC's results and partial process. */ |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 579 | static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data), |
| 580 | struct efx_ptp_timeset *timeset) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 581 | { |
| 582 | unsigned start_ns, end_ns; |
| 583 | |
| 584 | timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART); |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 585 | timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR); |
| 586 | timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 587 | timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND), |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 588 | timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 589 | |
| 590 | /* Ignore seconds */ |
| 591 | start_ns = timeset->host_start & MC_NANOSECOND_MASK; |
| 592 | end_ns = timeset->host_end & MC_NANOSECOND_MASK; |
| 593 | /* Allow for rollover */ |
| 594 | if (end_ns < start_ns) |
| 595 | end_ns += NSEC_PER_SEC; |
| 596 | /* Determine duration of operation */ |
| 597 | timeset->window = end_ns - start_ns; |
| 598 | } |
| 599 | |
| 600 | /* Process times received from MC. |
| 601 | * |
| 602 | * Extract times from returned results, and establish the minimum value |
| 603 | * seen. The minimum value represents the "best" possible time and events |
| 604 | * too much greater than this are rejected - the machine is, perhaps, too |
| 605 | * busy. A number of readings are taken so that, hopefully, at least one good |
| 606 | * synchronisation will be seen in the results. |
| 607 | */ |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 608 | static int |
| 609 | efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf), |
| 610 | size_t response_length, |
| 611 | const struct pps_event_time *last_time) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 612 | { |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 613 | unsigned number_readings = |
| 614 | MCDI_VAR_ARRAY_LEN(response_length, |
| 615 | PTP_OUT_SYNCHRONIZE_TIMESET); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 616 | unsigned i; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 617 | unsigned ngood = 0; |
| 618 | unsigned last_good = 0; |
| 619 | struct efx_ptp_data *ptp = efx->ptp_data; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 620 | u32 last_sec; |
| 621 | u32 start_sec; |
| 622 | struct timespec delta; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 623 | ktime_t mc_time; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 624 | |
| 625 | if (number_readings == 0) |
| 626 | return -EAGAIN; |
| 627 | |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 628 | /* Read the set of results and find the last good host-MC |
| 629 | * synchronization result. The MC times when it finishes reading the |
| 630 | * host time so the corrected window time should be fairly constant |
| 631 | * for a given platform. |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 632 | */ |
| 633 | for (i = 0; i < number_readings; i++) { |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 634 | s32 window, corrected; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 635 | struct timespec wait; |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 636 | |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 637 | efx_ptp_read_timeset( |
| 638 | MCDI_ARRAY_STRUCT_PTR(synch_buf, |
| 639 | PTP_OUT_SYNCHRONIZE_TIMESET, i), |
| 640 | &ptp->timeset[i]); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 641 | |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 642 | wait = ktime_to_timespec( |
| 643 | ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0)); |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 644 | window = ptp->timeset[i].window; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 645 | corrected = window - wait.tv_nsec; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 646 | |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 647 | /* We expect the uncorrected synchronization window to be at |
| 648 | * least as large as the interval between host start and end |
| 649 | * times. If it is smaller than this then this is mostly likely |
| 650 | * to be a consequence of the host's time being adjusted. |
| 651 | * Check that the corrected sync window is in a reasonable |
| 652 | * range. If it is out of range it is likely to be because an |
| 653 | * interrupt or other delay occurred between reading the system |
| 654 | * time and writing it to MC memory. |
| 655 | */ |
| 656 | if (window >= SYNCHRONISATION_GRANULARITY_NS && |
| 657 | corrected < MAX_SYNCHRONISATION_NS && |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 658 | corrected >= ptp->min_synchronisation_ns) { |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 659 | ngood++; |
| 660 | last_good = i; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 661 | } |
Laurence Evans | dfd8d58 | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 662 | } |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 663 | |
| 664 | if (ngood == 0) { |
| 665 | netif_warn(efx, drv, efx->net_dev, |
Laurence Evans | 94cd60d | 2013-11-21 10:38:24 +0000 | [diff] [blame] | 666 | "PTP no suitable synchronisations\n"); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 667 | return -EAGAIN; |
| 668 | } |
| 669 | |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 670 | /* Convert the NIC time into kernel time. No correction is required- |
| 671 | * this time is the output of a firmware process. |
| 672 | */ |
| 673 | mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major, |
| 674 | ptp->timeset[last_good].minor, 0); |
| 675 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 676 | /* Calculate delay from actual PPS to last_time */ |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 677 | delta = ktime_to_timespec(mc_time); |
| 678 | delta.tv_nsec += |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 679 | last_time->ts_real.tv_nsec - |
| 680 | (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK); |
| 681 | |
| 682 | /* It is possible that the seconds rolled over between taking |
| 683 | * the start reading and the last value written by the host. The |
| 684 | * timescales are such that a gap of more than one second is never |
| 685 | * expected. |
| 686 | */ |
| 687 | start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS; |
| 688 | last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK; |
| 689 | if (start_sec != last_sec) { |
| 690 | if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) { |
| 691 | netif_warn(efx, hw, efx->net_dev, |
| 692 | "PTP bad synchronisation seconds\n"); |
| 693 | return -EAGAIN; |
| 694 | } else { |
| 695 | delta.tv_sec = 1; |
| 696 | } |
| 697 | } else { |
| 698 | delta.tv_sec = 0; |
| 699 | } |
| 700 | |
| 701 | ptp->host_time_pps = *last_time; |
| 702 | pps_sub_ts(&ptp->host_time_pps, delta); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | /* Synchronize times between the host and the MC */ |
| 708 | static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings) |
| 709 | { |
| 710 | struct efx_ptp_data *ptp = efx->ptp_data; |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 711 | MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 712 | size_t response_length; |
| 713 | int rc; |
| 714 | unsigned long timeout; |
| 715 | struct pps_event_time last_time = {}; |
| 716 | unsigned int loops = 0; |
| 717 | int *start = ptp->start.addr; |
| 718 | |
| 719 | MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 720 | MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 721 | MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS, |
| 722 | num_readings); |
Ben Hutchings | 338f74d | 2012-10-10 23:20:17 +0100 | [diff] [blame] | 723 | MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR, |
| 724 | ptp->start.dma_addr); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 725 | |
| 726 | /* Clear flag that signals MC ready */ |
| 727 | ACCESS_ONCE(*start) = 0; |
Ben Hutchings | df2cd8a | 2012-09-19 00:56:18 +0100 | [diff] [blame] | 728 | rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf, |
| 729 | MC_CMD_PTP_IN_SYNCHRONIZE_LEN); |
| 730 | EFX_BUG_ON_PARANOID(rc); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 731 | |
| 732 | /* Wait for start from MCDI (or timeout) */ |
| 733 | timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS); |
| 734 | while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) { |
| 735 | udelay(20); /* Usually start MCDI execution quickly */ |
| 736 | loops++; |
| 737 | } |
| 738 | |
| 739 | if (ACCESS_ONCE(*start)) |
| 740 | efx_ptp_send_times(efx, &last_time); |
| 741 | |
| 742 | /* Collect results */ |
| 743 | rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP, |
| 744 | MC_CMD_PTP_IN_SYNCHRONIZE_LEN, |
| 745 | synch_buf, sizeof(synch_buf), |
| 746 | &response_length); |
| 747 | if (rc == 0) |
| 748 | rc = efx_ptp_process_times(efx, synch_buf, response_length, |
| 749 | &last_time); |
| 750 | |
| 751 | return rc; |
| 752 | } |
| 753 | |
| 754 | /* Transmit a PTP packet, via the MCDI interface, to the wire. */ |
| 755 | static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb) |
| 756 | { |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 757 | struct efx_ptp_data *ptp_data = efx->ptp_data; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 758 | struct skb_shared_hwtstamps timestamps; |
| 759 | int rc = -EIO; |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 760 | MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN); |
Ben Hutchings | 9528b92 | 2012-09-14 17:31:41 +0100 | [diff] [blame] | 761 | size_t len; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 762 | |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 763 | MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 764 | MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0); |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 765 | MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 766 | if (skb_shinfo(skb)->nr_frags != 0) { |
| 767 | rc = skb_linearize(skb); |
| 768 | if (rc != 0) |
| 769 | goto fail; |
| 770 | } |
| 771 | |
| 772 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 773 | rc = skb_checksum_help(skb); |
| 774 | if (rc != 0) |
| 775 | goto fail; |
| 776 | } |
| 777 | skb_copy_from_linear_data(skb, |
Ben Hutchings | c5bb0e9 | 2012-09-14 17:31:33 +0100 | [diff] [blame] | 778 | MCDI_PTR(ptp_data->txbuf, |
| 779 | PTP_IN_TRANSMIT_PACKET), |
Ben Hutchings | 9528b92 | 2012-09-14 17:31:41 +0100 | [diff] [blame] | 780 | skb->len); |
| 781 | rc = efx_mcdi_rpc(efx, MC_CMD_PTP, |
| 782 | ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len), |
| 783 | txtime, sizeof(txtime), &len); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 784 | if (rc != 0) |
| 785 | goto fail; |
| 786 | |
| 787 | memset(×tamps, 0, sizeof(timestamps)); |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 788 | timestamps.hwtstamp = ptp_data->nic_to_kernel_time( |
| 789 | MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR), |
| 790 | MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR), |
| 791 | ptp_data->ts_corrections.tx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 792 | |
| 793 | skb_tstamp_tx(skb, ×tamps); |
| 794 | |
| 795 | rc = 0; |
| 796 | |
| 797 | fail: |
| 798 | dev_kfree_skb(skb); |
| 799 | |
| 800 | return rc; |
| 801 | } |
| 802 | |
| 803 | static void efx_ptp_drop_time_expired_events(struct efx_nic *efx) |
| 804 | { |
| 805 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 806 | struct list_head *cursor; |
| 807 | struct list_head *next; |
| 808 | |
| 809 | /* Drop time-expired events */ |
| 810 | spin_lock_bh(&ptp->evt_lock); |
| 811 | if (!list_empty(&ptp->evt_list)) { |
| 812 | list_for_each_safe(cursor, next, &ptp->evt_list) { |
| 813 | struct efx_ptp_event_rx *evt; |
| 814 | |
| 815 | evt = list_entry(cursor, struct efx_ptp_event_rx, |
| 816 | link); |
| 817 | if (time_after(jiffies, evt->expiry)) { |
Wei Yongjun | 9545f4e | 2012-10-07 03:41:50 +0000 | [diff] [blame] | 818 | list_move(&evt->link, &ptp->evt_free_list); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 819 | netif_warn(efx, hw, efx->net_dev, |
| 820 | "PTP rx event dropped\n"); |
| 821 | } |
| 822 | } |
| 823 | } |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 824 | /* If the event overflow flag is set and the event list is now empty |
| 825 | * clear the flag to re-enable the overflow warning message. |
| 826 | */ |
| 827 | if (ptp->evt_overflow && list_empty(&ptp->evt_list)) |
| 828 | ptp->evt_overflow = false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 829 | spin_unlock_bh(&ptp->evt_lock); |
| 830 | } |
| 831 | |
| 832 | static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx, |
| 833 | struct sk_buff *skb) |
| 834 | { |
| 835 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 836 | bool evts_waiting; |
| 837 | struct list_head *cursor; |
| 838 | struct list_head *next; |
| 839 | struct efx_ptp_match *match; |
| 840 | enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED; |
| 841 | |
| 842 | spin_lock_bh(&ptp->evt_lock); |
| 843 | evts_waiting = !list_empty(&ptp->evt_list); |
| 844 | spin_unlock_bh(&ptp->evt_lock); |
| 845 | |
| 846 | if (!evts_waiting) |
| 847 | return PTP_PACKET_STATE_UNMATCHED; |
| 848 | |
| 849 | match = (struct efx_ptp_match *)skb->cb; |
| 850 | /* Look for a matching timestamp in the event queue */ |
| 851 | spin_lock_bh(&ptp->evt_lock); |
| 852 | list_for_each_safe(cursor, next, &ptp->evt_list) { |
| 853 | struct efx_ptp_event_rx *evt; |
| 854 | |
| 855 | evt = list_entry(cursor, struct efx_ptp_event_rx, link); |
| 856 | if ((evt->seq0 == match->words[0]) && |
| 857 | (evt->seq1 == match->words[1])) { |
| 858 | struct skb_shared_hwtstamps *timestamps; |
| 859 | |
| 860 | /* Match - add in hardware timestamp */ |
| 861 | timestamps = skb_hwtstamps(skb); |
| 862 | timestamps->hwtstamp = evt->hwtimestamp; |
| 863 | |
| 864 | match->state = PTP_PACKET_STATE_MATCHED; |
| 865 | rc = PTP_PACKET_STATE_MATCHED; |
Wei Yongjun | 9545f4e | 2012-10-07 03:41:50 +0000 | [diff] [blame] | 866 | list_move(&evt->link, &ptp->evt_free_list); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | } |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 870 | /* If the event overflow flag is set and the event list is now empty |
| 871 | * clear the flag to re-enable the overflow warning message. |
| 872 | */ |
| 873 | if (ptp->evt_overflow && list_empty(&ptp->evt_list)) |
| 874 | ptp->evt_overflow = false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 875 | spin_unlock_bh(&ptp->evt_lock); |
| 876 | |
| 877 | return rc; |
| 878 | } |
| 879 | |
| 880 | /* Process any queued receive events and corresponding packets |
| 881 | * |
| 882 | * q is returned with all the packets that are ready for delivery. |
| 883 | * true is returned if at least one of those packets requires |
| 884 | * synchronisation. |
| 885 | */ |
| 886 | static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q) |
| 887 | { |
| 888 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 889 | bool rc = false; |
| 890 | struct sk_buff *skb; |
| 891 | |
| 892 | while ((skb = skb_dequeue(&ptp->rxq))) { |
| 893 | struct efx_ptp_match *match; |
| 894 | |
| 895 | match = (struct efx_ptp_match *)skb->cb; |
| 896 | if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) { |
| 897 | __skb_queue_tail(q, skb); |
| 898 | } else if (efx_ptp_match_rx(efx, skb) == |
| 899 | PTP_PACKET_STATE_MATCHED) { |
| 900 | rc = true; |
| 901 | __skb_queue_tail(q, skb); |
| 902 | } else if (time_after(jiffies, match->expiry)) { |
| 903 | match->state = PTP_PACKET_STATE_TIMED_OUT; |
Ben Hutchings | 35f9a7a | 2013-12-06 22:10:46 +0000 | [diff] [blame] | 904 | if (net_ratelimit()) |
| 905 | netif_warn(efx, rx_err, efx->net_dev, |
| 906 | "PTP packet - no timestamp seen\n"); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 907 | __skb_queue_tail(q, skb); |
| 908 | } else { |
| 909 | /* Replace unprocessed entry and stop */ |
| 910 | skb_queue_head(&ptp->rxq, skb); |
| 911 | break; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | return rc; |
| 916 | } |
| 917 | |
| 918 | /* Complete processing of a received packet */ |
| 919 | static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb) |
| 920 | { |
| 921 | local_bh_disable(); |
| 922 | netif_receive_skb(skb); |
| 923 | local_bh_enable(); |
| 924 | } |
| 925 | |
Ben Hutchings | 62a1c70 | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 926 | static void efx_ptp_remove_multicast_filters(struct efx_nic *efx) |
| 927 | { |
| 928 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 929 | |
| 930 | if (ptp->rxfilter_installed) { |
| 931 | efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, |
| 932 | ptp->rxfilter_general); |
| 933 | efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, |
| 934 | ptp->rxfilter_event); |
| 935 | ptp->rxfilter_installed = false; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | static int efx_ptp_insert_multicast_filters(struct efx_nic *efx) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 940 | { |
| 941 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 942 | struct efx_filter_spec rxfilter; |
| 943 | int rc; |
| 944 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 945 | if (!ptp->channel || ptp->rxfilter_installed) |
Ben Hutchings | 62a1c70 | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 946 | return 0; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 947 | |
| 948 | /* Must filter on both event and general ports to ensure |
| 949 | * that there is no packet re-ordering. |
| 950 | */ |
| 951 | efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0, |
| 952 | efx_rx_queue_index( |
| 953 | efx_channel_get_rx_queue(ptp->channel))); |
| 954 | rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP, |
| 955 | htonl(PTP_ADDRESS), |
| 956 | htons(PTP_EVENT_PORT)); |
| 957 | if (rc != 0) |
| 958 | return rc; |
| 959 | |
| 960 | rc = efx_filter_insert_filter(efx, &rxfilter, true); |
| 961 | if (rc < 0) |
| 962 | return rc; |
| 963 | ptp->rxfilter_event = rc; |
| 964 | |
| 965 | efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0, |
| 966 | efx_rx_queue_index( |
| 967 | efx_channel_get_rx_queue(ptp->channel))); |
| 968 | rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP, |
| 969 | htonl(PTP_ADDRESS), |
| 970 | htons(PTP_GENERAL_PORT)); |
| 971 | if (rc != 0) |
| 972 | goto fail; |
| 973 | |
| 974 | rc = efx_filter_insert_filter(efx, &rxfilter, true); |
| 975 | if (rc < 0) |
| 976 | goto fail; |
| 977 | ptp->rxfilter_general = rc; |
| 978 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 979 | ptp->rxfilter_installed = true; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 980 | return 0; |
| 981 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 982 | fail: |
| 983 | efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, |
| 984 | ptp->rxfilter_event); |
Ben Hutchings | 62a1c70 | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 985 | return rc; |
| 986 | } |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 987 | |
Ben Hutchings | 62a1c70 | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 988 | static int efx_ptp_start(struct efx_nic *efx) |
| 989 | { |
| 990 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 991 | int rc; |
| 992 | |
| 993 | ptp->reset_required = false; |
| 994 | |
| 995 | rc = efx_ptp_insert_multicast_filters(efx); |
| 996 | if (rc) |
| 997 | return rc; |
| 998 | |
| 999 | rc = efx_ptp_enable(efx); |
| 1000 | if (rc != 0) |
| 1001 | goto fail; |
| 1002 | |
| 1003 | ptp->evt_frag_idx = 0; |
| 1004 | ptp->current_adjfreq = 0; |
| 1005 | |
| 1006 | return 0; |
| 1007 | |
| 1008 | fail: |
| 1009 | efx_ptp_remove_multicast_filters(efx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1010 | return rc; |
| 1011 | } |
| 1012 | |
| 1013 | static int efx_ptp_stop(struct efx_nic *efx) |
| 1014 | { |
| 1015 | struct efx_ptp_data *ptp = efx->ptp_data; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1016 | struct list_head *cursor; |
| 1017 | struct list_head *next; |
Alexandre Rames | 2ea4dc2 | 2013-11-08 10:20:31 +0000 | [diff] [blame] | 1018 | int rc; |
| 1019 | |
| 1020 | if (ptp == NULL) |
| 1021 | return 0; |
| 1022 | |
| 1023 | rc = efx_ptp_disable(efx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1024 | |
Ben Hutchings | 62a1c70 | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1025 | efx_ptp_remove_multicast_filters(efx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1026 | |
| 1027 | /* Make sure RX packets are really delivered */ |
| 1028 | efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq); |
| 1029 | skb_queue_purge(&efx->ptp_data->txq); |
| 1030 | |
| 1031 | /* Drop any pending receive events */ |
| 1032 | spin_lock_bh(&efx->ptp_data->evt_lock); |
| 1033 | list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) { |
Wei Yongjun | 9545f4e | 2012-10-07 03:41:50 +0000 | [diff] [blame] | 1034 | list_move(cursor, &efx->ptp_data->evt_free_list); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1035 | } |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 1036 | ptp->evt_overflow = false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1037 | spin_unlock_bh(&efx->ptp_data->evt_lock); |
| 1038 | |
| 1039 | return rc; |
| 1040 | } |
| 1041 | |
Alexandre Rames | 2ea4dc2 | 2013-11-08 10:20:31 +0000 | [diff] [blame] | 1042 | static int efx_ptp_restart(struct efx_nic *efx) |
| 1043 | { |
| 1044 | if (efx->ptp_data && efx->ptp_data->enabled) |
| 1045 | return efx_ptp_start(efx); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1049 | static void efx_ptp_pps_worker(struct work_struct *work) |
| 1050 | { |
| 1051 | struct efx_ptp_data *ptp = |
| 1052 | container_of(work, struct efx_ptp_data, pps_work); |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1053 | struct efx_nic *efx = ptp->efx; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1054 | struct ptp_clock_event ptp_evt; |
| 1055 | |
| 1056 | if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS)) |
| 1057 | return; |
| 1058 | |
| 1059 | ptp_evt.type = PTP_CLOCK_PPSUSR; |
| 1060 | ptp_evt.pps_times = ptp->host_time_pps; |
| 1061 | ptp_clock_event(ptp->phc_clock, &ptp_evt); |
| 1062 | } |
| 1063 | |
| 1064 | /* Process any pending transmissions and timestamp any received packets. |
| 1065 | */ |
| 1066 | static void efx_ptp_worker(struct work_struct *work) |
| 1067 | { |
| 1068 | struct efx_ptp_data *ptp_data = |
| 1069 | container_of(work, struct efx_ptp_data, work); |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1070 | struct efx_nic *efx = ptp_data->efx; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1071 | struct sk_buff *skb; |
| 1072 | struct sk_buff_head tempq; |
| 1073 | |
| 1074 | if (ptp_data->reset_required) { |
| 1075 | efx_ptp_stop(efx); |
| 1076 | efx_ptp_start(efx); |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | efx_ptp_drop_time_expired_events(efx); |
| 1081 | |
| 1082 | __skb_queue_head_init(&tempq); |
| 1083 | if (efx_ptp_process_events(efx, &tempq) || |
| 1084 | !skb_queue_empty(&ptp_data->txq)) { |
| 1085 | |
| 1086 | while ((skb = skb_dequeue(&ptp_data->txq))) |
| 1087 | efx_ptp_xmit_skb(efx, skb); |
| 1088 | } |
| 1089 | |
| 1090 | while ((skb = __skb_dequeue(&tempq))) |
| 1091 | efx_ptp_process_rx(efx, skb); |
| 1092 | } |
| 1093 | |
Ben Hutchings | 5d0dab0 | 2013-10-16 18:32:41 +0100 | [diff] [blame] | 1094 | static const struct ptp_clock_info efx_phc_clock_info = { |
| 1095 | .owner = THIS_MODULE, |
| 1096 | .name = "sfc", |
| 1097 | .max_adj = MAX_PPB, |
| 1098 | .n_alarm = 0, |
| 1099 | .n_ext_ts = 0, |
| 1100 | .n_per_out = 0, |
| 1101 | .pps = 1, |
| 1102 | .adjfreq = efx_phc_adjfreq, |
| 1103 | .adjtime = efx_phc_adjtime, |
| 1104 | .gettime = efx_phc_gettime, |
| 1105 | .settime = efx_phc_settime, |
| 1106 | .enable = efx_phc_enable, |
| 1107 | }; |
| 1108 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1109 | /* Initialise PTP state. */ |
| 1110 | int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1111 | { |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1112 | struct efx_ptp_data *ptp; |
| 1113 | int rc = 0; |
| 1114 | unsigned int pos; |
| 1115 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1116 | ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL); |
| 1117 | efx->ptp_data = ptp; |
| 1118 | if (!efx->ptp_data) |
| 1119 | return -ENOMEM; |
| 1120 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1121 | ptp->efx = efx; |
| 1122 | ptp->channel = channel; |
| 1123 | |
Ben Hutchings | 0d19a54 | 2012-09-18 21:59:52 +0100 | [diff] [blame] | 1124 | rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1125 | if (rc != 0) |
| 1126 | goto fail1; |
| 1127 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1128 | skb_queue_head_init(&ptp->rxq); |
| 1129 | skb_queue_head_init(&ptp->txq); |
| 1130 | ptp->workwq = create_singlethread_workqueue("sfc_ptp"); |
| 1131 | if (!ptp->workwq) { |
| 1132 | rc = -ENOMEM; |
| 1133 | goto fail2; |
| 1134 | } |
| 1135 | |
| 1136 | INIT_WORK(&ptp->work, efx_ptp_worker); |
| 1137 | ptp->config.flags = 0; |
| 1138 | ptp->config.tx_type = HWTSTAMP_TX_OFF; |
| 1139 | ptp->config.rx_filter = HWTSTAMP_FILTER_NONE; |
| 1140 | INIT_LIST_HEAD(&ptp->evt_list); |
| 1141 | INIT_LIST_HEAD(&ptp->evt_free_list); |
| 1142 | spin_lock_init(&ptp->evt_lock); |
| 1143 | for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++) |
| 1144 | list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list); |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 1145 | ptp->evt_overflow = false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1146 | |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1147 | /* Get the NIC PTP attributes and set up time conversions */ |
| 1148 | rc = efx_ptp_get_attributes(efx); |
| 1149 | if (rc < 0) |
| 1150 | goto fail3; |
| 1151 | |
| 1152 | /* Get the timestamp corrections */ |
| 1153 | rc = efx_ptp_get_timestamp_corrections(efx); |
| 1154 | if (rc < 0) |
| 1155 | goto fail3; |
| 1156 | |
Ben Hutchings | 5d0dab0 | 2013-10-16 18:32:41 +0100 | [diff] [blame] | 1157 | ptp->phc_clock_info = efx_phc_clock_info; |
Richard Cochran | 1ef7615 | 2012-09-22 07:02:03 +0000 | [diff] [blame] | 1158 | ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info, |
| 1159 | &efx->pci_dev->dev); |
Wei Yongjun | 155d940 | 2013-05-07 02:19:25 +0000 | [diff] [blame] | 1160 | if (IS_ERR(ptp->phc_clock)) { |
| 1161 | rc = PTR_ERR(ptp->phc_clock); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1162 | goto fail3; |
Wei Yongjun | 155d940 | 2013-05-07 02:19:25 +0000 | [diff] [blame] | 1163 | } |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1164 | |
| 1165 | INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker); |
| 1166 | ptp->pps_workwq = create_singlethread_workqueue("sfc_pps"); |
| 1167 | if (!ptp->pps_workwq) { |
| 1168 | rc = -ENOMEM; |
| 1169 | goto fail4; |
| 1170 | } |
| 1171 | ptp->nic_ts_enabled = false; |
| 1172 | |
| 1173 | return 0; |
| 1174 | fail4: |
| 1175 | ptp_clock_unregister(efx->ptp_data->phc_clock); |
| 1176 | |
| 1177 | fail3: |
| 1178 | destroy_workqueue(efx->ptp_data->workwq); |
| 1179 | |
| 1180 | fail2: |
| 1181 | efx_nic_free_buffer(efx, &ptp->start); |
| 1182 | |
| 1183 | fail1: |
| 1184 | kfree(efx->ptp_data); |
| 1185 | efx->ptp_data = NULL; |
| 1186 | |
| 1187 | return rc; |
| 1188 | } |
| 1189 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1190 | /* Initialise PTP channel. |
| 1191 | * |
| 1192 | * Setting core_index to zero causes the queue to be initialised and doesn't |
| 1193 | * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue. |
| 1194 | */ |
| 1195 | static int efx_ptp_probe_channel(struct efx_channel *channel) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1196 | { |
| 1197 | struct efx_nic *efx = channel->efx; |
| 1198 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1199 | channel->irq_moderation = 0; |
| 1200 | channel->rx_queue.core_index = 0; |
| 1201 | |
| 1202 | return efx_ptp_probe(efx, channel); |
| 1203 | } |
| 1204 | |
| 1205 | void efx_ptp_remove(struct efx_nic *efx) |
| 1206 | { |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1207 | if (!efx->ptp_data) |
| 1208 | return; |
| 1209 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1210 | (void)efx_ptp_disable(efx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1211 | |
| 1212 | cancel_work_sync(&efx->ptp_data->work); |
| 1213 | cancel_work_sync(&efx->ptp_data->pps_work); |
| 1214 | |
| 1215 | skb_queue_purge(&efx->ptp_data->rxq); |
| 1216 | skb_queue_purge(&efx->ptp_data->txq); |
| 1217 | |
| 1218 | ptp_clock_unregister(efx->ptp_data->phc_clock); |
| 1219 | |
| 1220 | destroy_workqueue(efx->ptp_data->workwq); |
| 1221 | destroy_workqueue(efx->ptp_data->pps_workwq); |
| 1222 | |
| 1223 | efx_nic_free_buffer(efx, &efx->ptp_data->start); |
| 1224 | kfree(efx->ptp_data); |
| 1225 | } |
| 1226 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1227 | static void efx_ptp_remove_channel(struct efx_channel *channel) |
| 1228 | { |
| 1229 | efx_ptp_remove(channel->efx); |
| 1230 | } |
| 1231 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1232 | static void efx_ptp_get_channel_name(struct efx_channel *channel, |
| 1233 | char *buf, size_t len) |
| 1234 | { |
| 1235 | snprintf(buf, len, "%s-ptp", channel->efx->name); |
| 1236 | } |
| 1237 | |
| 1238 | /* Determine whether this packet should be processed by the PTP module |
| 1239 | * or transmitted conventionally. |
| 1240 | */ |
| 1241 | bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb) |
| 1242 | { |
| 1243 | return efx->ptp_data && |
| 1244 | efx->ptp_data->enabled && |
| 1245 | skb->len >= PTP_MIN_LENGTH && |
| 1246 | skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM && |
| 1247 | likely(skb->protocol == htons(ETH_P_IP)) && |
Ben Hutchings | e5a498e | 2013-12-06 19:26:40 +0000 | [diff] [blame] | 1248 | skb_transport_header_was_set(skb) && |
| 1249 | skb_network_header_len(skb) >= sizeof(struct iphdr) && |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1250 | ip_hdr(skb)->protocol == IPPROTO_UDP && |
Ben Hutchings | e5a498e | 2013-12-06 19:26:40 +0000 | [diff] [blame] | 1251 | skb_headlen(skb) >= |
| 1252 | skb_transport_offset(skb) + sizeof(struct udphdr) && |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1253 | udp_hdr(skb)->dest == htons(PTP_EVENT_PORT); |
| 1254 | } |
| 1255 | |
| 1256 | /* Receive a PTP packet. Packets are queued until the arrival of |
| 1257 | * the receive timestamp from the MC - this will probably occur after the |
| 1258 | * packet arrival because of the processing in the MC. |
| 1259 | */ |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 1260 | static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1261 | { |
| 1262 | struct efx_nic *efx = channel->efx; |
| 1263 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 1264 | struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb; |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1265 | u8 *match_data_012, *match_data_345; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1266 | unsigned int version; |
| 1267 | |
| 1268 | match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS); |
| 1269 | |
| 1270 | /* Correct version? */ |
| 1271 | if (ptp->mode == MC_CMD_PTP_MODE_V1) { |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 1272 | if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) { |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 1273 | return false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1274 | } |
| 1275 | version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]); |
| 1276 | if (version != PTP_VERSION_V1) { |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 1277 | return false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1278 | } |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1279 | |
| 1280 | /* PTP V1 uses all six bytes of the UUID to match the packet |
| 1281 | * to the timestamp |
| 1282 | */ |
| 1283 | match_data_012 = skb->data + PTP_V1_UUID_OFFSET; |
| 1284 | match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1285 | } else { |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 1286 | if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) { |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 1287 | return false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1288 | } |
| 1289 | version = skb->data[PTP_V2_VERSION_OFFSET]; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1290 | if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) { |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 1291 | return false; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1292 | } |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1293 | |
| 1294 | /* The original V2 implementation uses bytes 2-7 of |
| 1295 | * the UUID to match the packet to the timestamp. This |
| 1296 | * discards two of the bytes of the MAC address used |
| 1297 | * to create the UUID (SF bug 33070). The PTP V2 |
| 1298 | * enhanced mode fixes this issue and uses bytes 0-2 |
| 1299 | * and byte 5-7 of the UUID. |
| 1300 | */ |
| 1301 | match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5; |
| 1302 | if (ptp->mode == MC_CMD_PTP_MODE_V2) { |
| 1303 | match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2; |
| 1304 | } else { |
| 1305 | match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0; |
| 1306 | BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED); |
| 1307 | } |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | /* Does this packet require timestamping? */ |
| 1311 | if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) { |
| 1312 | struct skb_shared_hwtstamps *timestamps; |
| 1313 | |
| 1314 | match->state = PTP_PACKET_STATE_UNMATCHED; |
| 1315 | |
| 1316 | /* Clear all timestamps held: filled in later */ |
| 1317 | timestamps = skb_hwtstamps(skb); |
| 1318 | memset(timestamps, 0, sizeof(*timestamps)); |
| 1319 | |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1320 | /* We expect the sequence number to be in the same position in |
| 1321 | * the packet for PTP V1 and V2 |
| 1322 | */ |
| 1323 | BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET); |
| 1324 | BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH); |
| 1325 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1326 | /* Extract UUID/Sequence information */ |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1327 | match->words[0] = (match_data_012[0] | |
| 1328 | (match_data_012[1] << 8) | |
| 1329 | (match_data_012[2] << 16) | |
| 1330 | (match_data_345[0] << 24)); |
| 1331 | match->words[1] = (match_data_345[1] | |
| 1332 | (match_data_345[2] << 8) | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1333 | (skb->data[PTP_V1_SEQUENCE_OFFSET + |
| 1334 | PTP_V1_SEQUENCE_LENGTH - 1] << |
| 1335 | 16)); |
| 1336 | } else { |
| 1337 | match->state = PTP_PACKET_STATE_MATCH_UNWANTED; |
| 1338 | } |
| 1339 | |
| 1340 | skb_queue_tail(&ptp->rxq, skb); |
| 1341 | queue_work(ptp->workwq, &ptp->work); |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 1342 | |
| 1343 | return true; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* Transmit a PTP packet. This has to be transmitted by the MC |
| 1347 | * itself, through an MCDI call. MCDI calls aren't permitted |
| 1348 | * in the transmit path so defer the actual transmission to a suitable worker. |
| 1349 | */ |
| 1350 | int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb) |
| 1351 | { |
| 1352 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 1353 | |
| 1354 | skb_queue_tail(&ptp->txq, skb); |
| 1355 | |
| 1356 | if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) && |
| 1357 | (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM)) |
| 1358 | efx_xmit_hwtstamp_pending(skb); |
| 1359 | queue_work(ptp->workwq, &ptp->work); |
| 1360 | |
| 1361 | return NETDEV_TX_OK; |
| 1362 | } |
| 1363 | |
| 1364 | static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted, |
| 1365 | unsigned int new_mode) |
| 1366 | { |
| 1367 | if ((enable_wanted != efx->ptp_data->enabled) || |
| 1368 | (enable_wanted && (efx->ptp_data->mode != new_mode))) { |
Alexandre Rames | 2ea4dc2 | 2013-11-08 10:20:31 +0000 | [diff] [blame] | 1369 | int rc = 0; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1370 | |
| 1371 | if (enable_wanted) { |
| 1372 | /* Change of mode requires disable */ |
| 1373 | if (efx->ptp_data->enabled && |
| 1374 | (efx->ptp_data->mode != new_mode)) { |
| 1375 | efx->ptp_data->enabled = false; |
| 1376 | rc = efx_ptp_stop(efx); |
| 1377 | if (rc != 0) |
| 1378 | return rc; |
| 1379 | } |
| 1380 | |
| 1381 | /* Set new operating mode and establish |
| 1382 | * baseline synchronisation, which must |
| 1383 | * succeed. |
| 1384 | */ |
| 1385 | efx->ptp_data->mode = new_mode; |
Alexandre Rames | 2ea4dc2 | 2013-11-08 10:20:31 +0000 | [diff] [blame] | 1386 | if (netif_running(efx->net_dev)) |
| 1387 | rc = efx_ptp_start(efx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1388 | if (rc == 0) { |
| 1389 | rc = efx_ptp_synchronize(efx, |
| 1390 | PTP_SYNC_ATTEMPTS * 2); |
| 1391 | if (rc != 0) |
| 1392 | efx_ptp_stop(efx); |
| 1393 | } |
| 1394 | } else { |
| 1395 | rc = efx_ptp_stop(efx); |
| 1396 | } |
| 1397 | |
| 1398 | if (rc != 0) |
| 1399 | return rc; |
| 1400 | |
| 1401 | efx->ptp_data->enabled = enable_wanted; |
| 1402 | } |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init) |
| 1408 | { |
| 1409 | bool enable_wanted = false; |
| 1410 | unsigned int new_mode; |
| 1411 | int rc; |
| 1412 | |
| 1413 | if (init->flags) |
| 1414 | return -EINVAL; |
| 1415 | |
| 1416 | if ((init->tx_type != HWTSTAMP_TX_OFF) && |
| 1417 | (init->tx_type != HWTSTAMP_TX_ON)) |
| 1418 | return -ERANGE; |
| 1419 | |
| 1420 | new_mode = efx->ptp_data->mode; |
| 1421 | /* Determine whether any PTP HW operations are required */ |
| 1422 | switch (init->rx_filter) { |
| 1423 | case HWTSTAMP_FILTER_NONE: |
| 1424 | break; |
| 1425 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
| 1426 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
| 1427 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: |
| 1428 | init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; |
| 1429 | new_mode = MC_CMD_PTP_MODE_V1; |
| 1430 | enable_wanted = true; |
| 1431 | break; |
| 1432 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 1433 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 1434 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
| 1435 | /* Although these three are accepted only IPV4 packets will be |
| 1436 | * timestamped |
| 1437 | */ |
| 1438 | init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1439 | new_mode = MC_CMD_PTP_MODE_V2_ENHANCED; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1440 | enable_wanted = true; |
| 1441 | break; |
| 1442 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 1443 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 1444 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
| 1445 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 1446 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 1447 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
| 1448 | /* Non-IP + IPv6 timestamping not supported */ |
| 1449 | return -ERANGE; |
| 1450 | break; |
| 1451 | default: |
| 1452 | return -ERANGE; |
| 1453 | } |
| 1454 | |
| 1455 | if (init->tx_type != HWTSTAMP_TX_OFF) |
| 1456 | enable_wanted = true; |
| 1457 | |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1458 | /* Old versions of the firmware do not support the improved |
| 1459 | * UUID filtering option (SF bug 33070). If the firmware does |
| 1460 | * not accept the enhanced mode, fall back to the standard PTP |
| 1461 | * v2 UUID filtering. |
| 1462 | */ |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1463 | rc = efx_ptp_change_mode(efx, enable_wanted, new_mode); |
Laurence Evans | c939a31 | 2012-11-15 10:56:07 +0000 | [diff] [blame] | 1464 | if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED)) |
| 1465 | rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1466 | if (rc != 0) |
| 1467 | return rc; |
| 1468 | |
| 1469 | efx->ptp_data->config = *init; |
| 1470 | |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Ben Hutchings | 62ebac9 | 2013-04-08 17:34:58 +0100 | [diff] [blame] | 1474 | void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1475 | { |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1476 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 1477 | |
| 1478 | if (!ptp) |
Ben Hutchings | 62ebac9 | 2013-04-08 17:34:58 +0100 | [diff] [blame] | 1479 | return; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1480 | |
Ben Hutchings | 62ebac9 | 2013-04-08 17:34:58 +0100 | [diff] [blame] | 1481 | ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE | |
| 1482 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1483 | SOF_TIMESTAMPING_RAW_HARDWARE); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1484 | ts_info->phc_index = ptp_clock_index(ptp->phc_clock); |
| 1485 | ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON; |
| 1486 | ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE | |
| 1487 | 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT | |
| 1488 | 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC | |
| 1489 | 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ | |
| 1490 | 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT | |
| 1491 | 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC | |
| 1492 | 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
Ben Hutchings | 433dc9b | 2013-11-14 01:26:21 +0000 | [diff] [blame] | 1495 | int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1496 | { |
| 1497 | struct hwtstamp_config config; |
| 1498 | int rc; |
| 1499 | |
| 1500 | /* Not a PTP enabled port */ |
| 1501 | if (!efx->ptp_data) |
| 1502 | return -EOPNOTSUPP; |
| 1503 | |
| 1504 | if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) |
| 1505 | return -EFAULT; |
| 1506 | |
| 1507 | rc = efx_ptp_ts_init(efx, &config); |
| 1508 | if (rc != 0) |
| 1509 | return rc; |
| 1510 | |
| 1511 | return copy_to_user(ifr->ifr_data, &config, sizeof(config)) |
| 1512 | ? -EFAULT : 0; |
| 1513 | } |
| 1514 | |
Ben Hutchings | 433dc9b | 2013-11-14 01:26:21 +0000 | [diff] [blame] | 1515 | int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr) |
| 1516 | { |
| 1517 | if (!efx->ptp_data) |
| 1518 | return -EOPNOTSUPP; |
| 1519 | |
| 1520 | return copy_to_user(ifr->ifr_data, &efx->ptp_data->config, |
| 1521 | sizeof(efx->ptp_data->config)) ? -EFAULT : 0; |
| 1522 | } |
| 1523 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1524 | static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len) |
| 1525 | { |
| 1526 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 1527 | |
| 1528 | netif_err(efx, hw, efx->net_dev, |
| 1529 | "PTP unexpected event length: got %d expected %d\n", |
| 1530 | ptp->evt_frag_idx, expected_frag_len); |
| 1531 | ptp->reset_required = true; |
| 1532 | queue_work(ptp->workwq, &ptp->work); |
| 1533 | } |
| 1534 | |
| 1535 | /* Process a completed receive event. Put it on the event queue and |
| 1536 | * start worker thread. This is required because event and their |
| 1537 | * correspoding packets may come in either order. |
| 1538 | */ |
| 1539 | static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp) |
| 1540 | { |
| 1541 | struct efx_ptp_event_rx *evt = NULL; |
| 1542 | |
| 1543 | if (ptp->evt_frag_idx != 3) { |
| 1544 | ptp_event_failure(efx, 3); |
| 1545 | return; |
| 1546 | } |
| 1547 | |
| 1548 | spin_lock_bh(&ptp->evt_lock); |
| 1549 | if (!list_empty(&ptp->evt_free_list)) { |
| 1550 | evt = list_first_entry(&ptp->evt_free_list, |
| 1551 | struct efx_ptp_event_rx, link); |
| 1552 | list_del(&evt->link); |
| 1553 | |
| 1554 | evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA); |
| 1555 | evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2], |
| 1556 | MCDI_EVENT_SRC) | |
| 1557 | (EFX_QWORD_FIELD(ptp->evt_frags[1], |
| 1558 | MCDI_EVENT_SRC) << 8) | |
| 1559 | (EFX_QWORD_FIELD(ptp->evt_frags[0], |
| 1560 | MCDI_EVENT_SRC) << 16)); |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1561 | evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time( |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1562 | EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA), |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1563 | EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA), |
| 1564 | ptp->ts_corrections.rx); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1565 | evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS); |
| 1566 | list_add_tail(&evt->link, &ptp->evt_list); |
| 1567 | |
| 1568 | queue_work(ptp->workwq, &ptp->work); |
Laurence Evans | f321160 | 2013-01-28 14:51:17 +0000 | [diff] [blame] | 1569 | } else if (!ptp->evt_overflow) { |
| 1570 | /* Log a warning message and set the event overflow flag. |
| 1571 | * The message won't be logged again until the event queue |
| 1572 | * becomes empty. |
| 1573 | */ |
| 1574 | netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n"); |
| 1575 | ptp->evt_overflow = true; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1576 | } |
| 1577 | spin_unlock_bh(&ptp->evt_lock); |
| 1578 | } |
| 1579 | |
| 1580 | static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp) |
| 1581 | { |
| 1582 | int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA); |
| 1583 | if (ptp->evt_frag_idx != 1) { |
| 1584 | ptp_event_failure(efx, 1); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code); |
| 1589 | } |
| 1590 | |
| 1591 | static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp) |
| 1592 | { |
| 1593 | if (ptp->nic_ts_enabled) |
| 1594 | queue_work(ptp->pps_workwq, &ptp->pps_work); |
| 1595 | } |
| 1596 | |
| 1597 | void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev) |
| 1598 | { |
| 1599 | struct efx_ptp_data *ptp = efx->ptp_data; |
| 1600 | int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE); |
| 1601 | |
| 1602 | if (!ptp->enabled) |
| 1603 | return; |
| 1604 | |
| 1605 | if (ptp->evt_frag_idx == 0) { |
| 1606 | ptp->evt_code = code; |
| 1607 | } else if (ptp->evt_code != code) { |
| 1608 | netif_err(efx, hw, efx->net_dev, |
| 1609 | "PTP out of sequence event %d\n", code); |
| 1610 | ptp->evt_frag_idx = 0; |
| 1611 | } |
| 1612 | |
| 1613 | ptp->evt_frags[ptp->evt_frag_idx++] = *ev; |
| 1614 | if (!MCDI_EVENT_FIELD(*ev, CONT)) { |
| 1615 | /* Process resulting event */ |
| 1616 | switch (code) { |
| 1617 | case MCDI_EVENT_CODE_PTP_RX: |
| 1618 | ptp_event_rx(efx, ptp); |
| 1619 | break; |
| 1620 | case MCDI_EVENT_CODE_PTP_FAULT: |
| 1621 | ptp_event_fault(efx, ptp); |
| 1622 | break; |
| 1623 | case MCDI_EVENT_CODE_PTP_PPS: |
| 1624 | ptp_event_pps(efx, ptp); |
| 1625 | break; |
| 1626 | default: |
| 1627 | netif_err(efx, hw, efx->net_dev, |
| 1628 | "PTP unknown event %d\n", code); |
| 1629 | break; |
| 1630 | } |
| 1631 | ptp->evt_frag_idx = 0; |
| 1632 | } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) { |
| 1633 | netif_err(efx, hw, efx->net_dev, |
| 1634 | "PTP too many event fragments\n"); |
| 1635 | ptp->evt_frag_idx = 0; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta) |
| 1640 | { |
| 1641 | struct efx_ptp_data *ptp_data = container_of(ptp, |
| 1642 | struct efx_ptp_data, |
| 1643 | phc_clock_info); |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1644 | struct efx_nic *efx = ptp_data->efx; |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1645 | MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1646 | s64 adjustment_ns; |
| 1647 | int rc; |
| 1648 | |
| 1649 | if (delta > MAX_PPB) |
| 1650 | delta = MAX_PPB; |
| 1651 | else if (delta < -MAX_PPB) |
| 1652 | delta = -MAX_PPB; |
| 1653 | |
| 1654 | /* Convert ppb to fixed point ns. */ |
| 1655 | adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >> |
| 1656 | (PPB_EXTRA_BITS + MAX_PPB_BITS)); |
| 1657 | |
| 1658 | MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 1659 | MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0); |
Ben Hutchings | 338f74d | 2012-10-10 23:20:17 +0100 | [diff] [blame] | 1660 | MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1661 | MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0); |
| 1662 | MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0); |
| 1663 | rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj), |
| 1664 | NULL, 0, NULL); |
| 1665 | if (rc != 0) |
| 1666 | return rc; |
| 1667 | |
Ben Hutchings | cd6fe65 | 2013-12-05 17:24:06 +0000 | [diff] [blame] | 1668 | ptp_data->current_adjfreq = adjustment_ns; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta) |
| 1673 | { |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1674 | u32 nic_major, nic_minor; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1675 | struct efx_ptp_data *ptp_data = container_of(ptp, |
| 1676 | struct efx_ptp_data, |
| 1677 | phc_clock_info); |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1678 | struct efx_nic *efx = ptp_data->efx; |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1679 | MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1680 | |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1681 | efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor); |
| 1682 | |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1683 | MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 1684 | MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); |
Ben Hutchings | cd6fe65 | 2013-12-05 17:24:06 +0000 | [diff] [blame] | 1685 | MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq); |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1686 | MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major); |
| 1687 | MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1688 | return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), |
| 1689 | NULL, 0, NULL); |
| 1690 | } |
| 1691 | |
| 1692 | static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts) |
| 1693 | { |
| 1694 | struct efx_ptp_data *ptp_data = container_of(ptp, |
| 1695 | struct efx_ptp_data, |
| 1696 | phc_clock_info); |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1697 | struct efx_nic *efx = ptp_data->efx; |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 1698 | MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN); |
| 1699 | MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1700 | int rc; |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1701 | ktime_t kt; |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1702 | |
| 1703 | MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME); |
Laurence Evans | c1d828b | 2013-03-06 15:33:17 +0000 | [diff] [blame] | 1704 | MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1705 | |
| 1706 | rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf), |
| 1707 | outbuf, sizeof(outbuf), NULL); |
| 1708 | if (rc != 0) |
| 1709 | return rc; |
| 1710 | |
Laurence Evans | a6f7346 | 2013-12-04 23:47:56 +0000 | [diff] [blame^] | 1711 | kt = ptp_data->nic_to_kernel_time( |
| 1712 | MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR), |
| 1713 | MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0); |
| 1714 | *ts = ktime_to_timespec(kt); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | static int efx_phc_settime(struct ptp_clock_info *ptp, |
| 1719 | const struct timespec *e_ts) |
| 1720 | { |
| 1721 | /* Get the current NIC time, efx_phc_gettime. |
| 1722 | * Subtract from the desired time to get the offset |
| 1723 | * call efx_phc_adjtime with the offset |
| 1724 | */ |
| 1725 | int rc; |
| 1726 | struct timespec time_now; |
| 1727 | struct timespec delta; |
| 1728 | |
| 1729 | rc = efx_phc_gettime(ptp, &time_now); |
| 1730 | if (rc != 0) |
| 1731 | return rc; |
| 1732 | |
| 1733 | delta = timespec_sub(*e_ts, time_now); |
| 1734 | |
Julia Lawall | 56567c6 | 2013-01-21 03:02:48 +0000 | [diff] [blame] | 1735 | rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta)); |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1736 | if (rc != 0) |
| 1737 | return rc; |
| 1738 | |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static int efx_phc_enable(struct ptp_clock_info *ptp, |
| 1743 | struct ptp_clock_request *request, |
| 1744 | int enable) |
| 1745 | { |
| 1746 | struct efx_ptp_data *ptp_data = container_of(ptp, |
| 1747 | struct efx_ptp_data, |
| 1748 | phc_clock_info); |
| 1749 | if (request->type != PTP_CLK_REQ_PPS) |
| 1750 | return -EOPNOTSUPP; |
| 1751 | |
| 1752 | ptp_data->nic_ts_enabled = !!enable; |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | static const struct efx_channel_type efx_ptp_channel_type = { |
| 1757 | .handle_no_channel = efx_ptp_handle_no_channel, |
| 1758 | .pre_probe = efx_ptp_probe_channel, |
| 1759 | .post_remove = efx_ptp_remove_channel, |
| 1760 | .get_name = efx_ptp_get_channel_name, |
| 1761 | /* no copy operation; there is no need to reallocate this channel */ |
| 1762 | .receive_skb = efx_ptp_rx, |
| 1763 | .keep_eventq = false, |
| 1764 | }; |
| 1765 | |
Ben Hutchings | ac36baf | 2013-10-15 17:54:56 +0100 | [diff] [blame] | 1766 | void efx_ptp_defer_probe_with_channel(struct efx_nic *efx) |
Stuart Hodgson | 7c236c4 | 2012-09-03 11:09:36 +0100 | [diff] [blame] | 1767 | { |
| 1768 | /* Check whether PTP is implemented on this NIC. The DISABLE |
| 1769 | * operation will succeed if and only if it is implemented. |
| 1770 | */ |
| 1771 | if (efx_ptp_disable(efx) == 0) |
| 1772 | efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] = |
| 1773 | &efx_ptp_channel_type; |
| 1774 | } |
Alexandre Rames | 2ea4dc2 | 2013-11-08 10:20:31 +0000 | [diff] [blame] | 1775 | |
| 1776 | void efx_ptp_start_datapath(struct efx_nic *efx) |
| 1777 | { |
| 1778 | if (efx_ptp_restart(efx)) |
| 1779 | netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n"); |
| 1780 | } |
| 1781 | |
| 1782 | void efx_ptp_stop_datapath(struct efx_nic *efx) |
| 1783 | { |
| 1784 | efx_ptp_stop(efx); |
| 1785 | } |