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