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