blob: 8c5c96acecc92be9e9c853e71ba7d60b0e52144a [file] [log] [blame]
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
3 * Copyright 2011-2013 Solarflare Communications Inc.
Stuart Hodgson7c236c42012-09-03 11:09:36 +01004 *
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 Hutchings8b8a95a2012-09-18 01:57:07 +010049#include "farch_regs.h"
Stuart Hodgson7c236c42012-09-03 11:09:36 +010050#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 Evansa6f73462013-12-04 23:47:56 +000065#define DEFAULT_MIN_SYNCHRONISATION_NS 120
Stuart Hodgson7c236c42012-09-03 11:09:36 +010066
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 Evansc939a312012-11-15 10:56:07 +0000102#define PTP_V2_UUID_LENGTH 8
103#define PTP_V2_UUID_OFFSET 48
104
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100105/* 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
135enum 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 */
175struct 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 */
187struct 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 Evansa6f73462013-12-04 23:47:56 +0000198 * @major: Hardware timestamp, major
199 * @minor: Hardware timestamp, minor
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100200 * @host_end: Host time immediately after hardware timestamp taken
Laurence Evansa6f73462013-12-04 23:47:56 +0000201 * @wait: Number of NIC clock ticks between hardware timestamp being read and
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100202 * host end time being seen
203 * @window: Difference of host_end and host_start
204 * @valid: Whether this timeset is valid
205 */
206struct efx_ptp_timeset {
207 u32 host_start;
Laurence Evansa6f73462013-12-04 23:47:56 +0000208 u32 major;
209 u32 minor;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100210 u32 host_end;
Laurence Evansa6f73462013-12-04 23:47:56 +0000211 u32 wait;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100212 u32 window; /* Derived: end - start, allowing for wrap */
213};
214
215/**
216 * struct efx_ptp_data - Precision Time Protocol (PTP) state
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100217 * @efx: The NIC context
218 * @channel: The PTP channel (Siena only)
Jon Cooperbd9a2652013-11-18 12:54:41 +0000219 * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are
220 * separate events)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100221 * @rxq: Receive queue (awaiting timestamps)
222 * @txq: Transmit queue
223 * @evt_list: List of MC receive events awaiting packets
224 * @evt_free_list: List of free events
225 * @evt_lock: Lock for manipulating evt_list and evt_free_list
226 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
227 * @workwq: Work queue for processing pending PTP operations
228 * @work: Work task
229 * @reset_required: A serious error has occurred and the PTP task needs to be
230 * reset (disable, enable).
231 * @rxfilter_event: Receive filter when operating
232 * @rxfilter_general: Receive filter when operating
233 * @config: Current timestamp configuration
234 * @enabled: PTP operation enabled
235 * @mode: Mode in which PTP operating (PTP version)
Laurence Evansa6f73462013-12-04 23:47:56 +0000236 * @time_format: Time format supported by this NIC
237 * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
238 * @nic_to_kernel_time: Function to convert from NIC to kernel time
239 * @min_synchronisation_ns: Minimum acceptable corrected sync window
240 * @ts_corrections.tx: Required driver correction of transmit timestamps
241 * @ts_corrections.rx: Required driver correction of receive timestamps
242 * @ts_corrections.pps_out: PPS output error (information only)
243 * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100244 * @evt_frags: Partly assembled PTP events
245 * @evt_frag_idx: Current fragment number
246 * @evt_code: Last event code
247 * @start: Address at which MC indicates ready for synchronisation
248 * @host_time_pps: Host time at last PPS
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100249 * @current_adjfreq: Current ppb adjustment.
Ben Hutchings9aecda92013-12-05 21:28:42 +0000250 * @phc_clock: Pointer to registered phc device (if primary function)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100251 * @phc_clock_info: Registration structure for phc device
252 * @pps_work: pps work task for handling pps events
253 * @pps_workwq: pps work queue
254 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
255 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
256 * allocations in main data path).
Ben Hutchings99691c42013-12-11 02:36:08 +0000257 * @good_syncs: Number of successful synchronisations.
258 * @fast_syncs: Number of synchronisations requiring short delay
259 * @bad_syncs: Number of failed synchronisations.
260 * @sync_timeouts: Number of synchronisation timeouts
261 * @no_time_syncs: Number of synchronisations with no good times.
262 * @invalid_sync_windows: Number of sync windows with bad durations.
263 * @undersize_sync_windows: Number of corrected sync windows that are too small
264 * @oversize_sync_windows: Number of corrected sync windows that are too large
265 * @rx_no_timestamp: Number of packets received without a timestamp.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100266 * @timeset: Last set of synchronisation statistics.
267 */
268struct efx_ptp_data {
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100269 struct efx_nic *efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100270 struct efx_channel *channel;
Jon Cooperbd9a2652013-11-18 12:54:41 +0000271 bool rx_ts_inline;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100272 struct sk_buff_head rxq;
273 struct sk_buff_head txq;
274 struct list_head evt_list;
275 struct list_head evt_free_list;
276 spinlock_t evt_lock;
277 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
278 struct workqueue_struct *workwq;
279 struct work_struct work;
280 bool reset_required;
281 u32 rxfilter_event;
282 u32 rxfilter_general;
283 bool rxfilter_installed;
284 struct hwtstamp_config config;
285 bool enabled;
286 unsigned int mode;
Laurence Evansa6f73462013-12-04 23:47:56 +0000287 unsigned int time_format;
288 void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
289 ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
290 s32 correction);
291 unsigned int min_synchronisation_ns;
292 struct {
293 s32 tx;
294 s32 rx;
295 s32 pps_out;
296 s32 pps_in;
297 } ts_corrections;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100298 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
299 int evt_frag_idx;
300 int evt_code;
301 struct efx_buffer start;
302 struct pps_event_time host_time_pps;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100303 s64 current_adjfreq;
304 struct ptp_clock *phc_clock;
305 struct ptp_clock_info phc_clock_info;
306 struct work_struct pps_work;
307 struct workqueue_struct *pps_workwq;
308 bool nic_ts_enabled;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100309 MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
Ben Hutchings99691c42013-12-11 02:36:08 +0000310
311 unsigned int good_syncs;
312 unsigned int fast_syncs;
313 unsigned int bad_syncs;
314 unsigned int sync_timeouts;
315 unsigned int no_time_syncs;
316 unsigned int invalid_sync_windows;
317 unsigned int undersize_sync_windows;
318 unsigned int oversize_sync_windows;
319 unsigned int rx_no_timestamp;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100320 struct efx_ptp_timeset
321 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
322};
323
324static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
325static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
Richard Cochran0fcb5c762015-03-29 23:12:06 +0200326static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100327static int efx_phc_settime(struct ptp_clock_info *ptp,
Richard Cochran0fcb5c762015-03-29 23:12:06 +0200328 const struct timespec64 *e_ts);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100329static int efx_phc_enable(struct ptp_clock_info *ptp,
330 struct ptp_clock_request *request, int on);
331
Ben Hutchings99691c42013-12-11 02:36:08 +0000332#define PTP_SW_STAT(ext_name, field_name) \
333 { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
334#define PTP_MC_STAT(ext_name, mcdi_name) \
335 { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
336static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
337 PTP_SW_STAT(ptp_good_syncs, good_syncs),
338 PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
339 PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
340 PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
341 PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
342 PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
343 PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
344 PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
345 PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
346 PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
347 PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
348 PTP_MC_STAT(ptp_timestamp_packets, TS),
349 PTP_MC_STAT(ptp_filter_matches, FM),
350 PTP_MC_STAT(ptp_non_filter_matches, NFM),
351};
352#define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
353static const unsigned long efx_ptp_stat_mask[] = {
354 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
355};
356
357size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
358{
359 if (!efx->ptp_data)
360 return 0;
361
362 return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
363 efx_ptp_stat_mask, strings);
364}
365
366size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
367{
368 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
369 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
370 size_t i;
371 int rc;
372
373 if (!efx->ptp_data)
374 return 0;
375
376 /* Copy software statistics */
377 for (i = 0; i < PTP_STAT_COUNT; i++) {
378 if (efx_ptp_stat_desc[i].dma_width)
379 continue;
380 stats[i] = *(unsigned int *)((char *)efx->ptp_data +
381 efx_ptp_stat_desc[i].offset);
382 }
383
384 /* Fetch MC statistics. We *must* fill in all statistics or
385 * risk leaking kernel memory to userland, so if the MCDI
386 * request fails we pretend we got zeroes.
387 */
388 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
389 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
390 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
391 outbuf, sizeof(outbuf), NULL);
392 if (rc) {
393 netif_err(efx, hw, efx->net_dev,
394 "MC_CMD_PTP_OP_STATUS failed (%d)\n", rc);
395 memset(outbuf, 0, sizeof(outbuf));
396 }
397 efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
398 efx_ptp_stat_mask,
399 stats, _MCDI_PTR(outbuf, 0), false);
400
401 return PTP_STAT_COUNT;
402}
403
Laurence Evansa6f73462013-12-04 23:47:56 +0000404/* For Siena platforms NIC time is s and ns */
405static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
406{
407 struct timespec ts = ns_to_timespec(ns);
408 *nic_major = ts.tv_sec;
409 *nic_minor = ts.tv_nsec;
410}
411
Jon Cooperbd9a2652013-11-18 12:54:41 +0000412static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
413 s32 correction)
Laurence Evansa6f73462013-12-04 23:47:56 +0000414{
415 ktime_t kt = ktime_set(nic_major, nic_minor);
416 if (correction >= 0)
417 kt = ktime_add_ns(kt, (u64)correction);
418 else
419 kt = ktime_sub_ns(kt, (u64)-correction);
420 return kt;
421}
422
423/* To convert from s27 format to ns we multiply then divide by a power of 2.
424 * For the conversion from ns to s27, the operation is also converted to a
425 * multiply and shift.
426 */
427#define S27_TO_NS_SHIFT (27)
428#define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
429#define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
430#define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
431
432/* For Huntington platforms NIC time is in seconds and fractions of a second
433 * where the minor register only uses 27 bits in units of 2^-27s.
434 */
435static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
436{
437 struct timespec ts = ns_to_timespec(ns);
438 u32 maj = ts.tv_sec;
439 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
440 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
441
442 /* The conversion can result in the minor value exceeding the maximum.
443 * In this case, round up to the next second.
444 */
445 if (min >= S27_MINOR_MAX) {
446 min -= S27_MINOR_MAX;
447 maj++;
448 }
449
450 *nic_major = maj;
451 *nic_minor = min;
452}
453
Jon Cooperbd9a2652013-11-18 12:54:41 +0000454static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
Laurence Evansa6f73462013-12-04 23:47:56 +0000455{
Jon Cooperbd9a2652013-11-18 12:54:41 +0000456 u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
457 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
458 return ktime_set(nic_major, ns);
459}
Laurence Evansa6f73462013-12-04 23:47:56 +0000460
Jon Cooperbd9a2652013-11-18 12:54:41 +0000461static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
462 s32 correction)
463{
Laurence Evansa6f73462013-12-04 23:47:56 +0000464 /* Apply the correction and deal with carry */
465 nic_minor += correction;
466 if ((s32)nic_minor < 0) {
467 nic_minor += S27_MINOR_MAX;
468 nic_major--;
469 } else if (nic_minor >= S27_MINOR_MAX) {
470 nic_minor -= S27_MINOR_MAX;
471 nic_major++;
472 }
473
Jon Cooperbd9a2652013-11-18 12:54:41 +0000474 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
Laurence Evansa6f73462013-12-04 23:47:56 +0000475}
476
477/* Get PTP attributes and set up time conversions */
478static int efx_ptp_get_attributes(struct efx_nic *efx)
479{
480 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
481 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
482 struct efx_ptp_data *ptp = efx->ptp_data;
483 int rc;
484 u32 fmt;
485 size_t out_len;
486
487 /* Get the PTP attributes. If the NIC doesn't support the operation we
488 * use the default format for compatibility with older NICs i.e.
489 * seconds and nanoseconds.
490 */
491 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
492 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
493 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
494 outbuf, sizeof(outbuf), &out_len);
495 if (rc == 0)
496 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
497 else if (rc == -EINVAL)
498 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
499 else
500 return rc;
501
502 if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION) {
503 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
Jon Cooperbd9a2652013-11-18 12:54:41 +0000504 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
Laurence Evansa6f73462013-12-04 23:47:56 +0000505 } else if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS) {
506 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
Jon Cooperbd9a2652013-11-18 12:54:41 +0000507 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
Laurence Evansa6f73462013-12-04 23:47:56 +0000508 } else {
509 return -ERANGE;
510 }
511
512 ptp->time_format = fmt;
513
514 /* MC_CMD_PTP_OP_GET_ATTRIBUTES is an extended version of an older
515 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT that also returns a value
516 * to use for the minimum acceptable corrected synchronization window.
517 * If we have the extra information store it. For older firmware that
518 * does not implement the extended command use the default value.
519 */
520 if (rc == 0 && out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
521 ptp->min_synchronisation_ns =
522 MCDI_DWORD(outbuf,
523 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
524 else
525 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
526
527 return 0;
528}
529
530/* Get PTP timestamp corrections */
531static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
532{
533 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
534 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_LEN);
535 int rc;
536
537 /* Get the timestamp corrections from the NIC. If this operation is
538 * not supported (older NICs) then no correction is required.
539 */
540 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
541 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
542 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
543
544 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
545 outbuf, sizeof(outbuf), NULL);
546 if (rc == 0) {
547 efx->ptp_data->ts_corrections.tx = MCDI_DWORD(outbuf,
548 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
549 efx->ptp_data->ts_corrections.rx = MCDI_DWORD(outbuf,
550 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
551 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
552 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
553 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
554 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
555 } else if (rc == -EINVAL) {
556 efx->ptp_data->ts_corrections.tx = 0;
557 efx->ptp_data->ts_corrections.rx = 0;
558 efx->ptp_data->ts_corrections.pps_out = 0;
559 efx->ptp_data->ts_corrections.pps_in = 0;
560 } else {
561 return rc;
562 }
563
564 return 0;
565}
566
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100567/* Enable MCDI PTP support. */
568static int efx_ptp_enable(struct efx_nic *efx)
569{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100570 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100571 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
572 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100573
574 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000575 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100576 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100577 efx->ptp_data->channel ?
578 efx->ptp_data->channel->channel : 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100579 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
580
Edward Cree1e0b8122013-05-31 18:36:12 +0100581 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
582 outbuf, sizeof(outbuf), NULL);
583 rc = (rc == -EALREADY) ? 0 : rc;
584 if (rc)
585 efx_mcdi_display_error(efx, MC_CMD_PTP,
586 MC_CMD_PTP_IN_ENABLE_LEN,
587 outbuf, sizeof(outbuf), rc);
588 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100589}
590
591/* Disable MCDI PTP support.
592 *
593 * Note that this function should never rely on the presence of ptp_data -
594 * may be called before that exists.
595 */
596static int efx_ptp_disable(struct efx_nic *efx)
597{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100598 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100599 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
600 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100601
602 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000603 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Edward Cree1e0b8122013-05-31 18:36:12 +0100604 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
605 outbuf, sizeof(outbuf), NULL);
606 rc = (rc == -EALREADY) ? 0 : rc;
Edward Creeb1336382015-05-12 13:04:52 +0100607 /* If we get ENOSYS, the NIC doesn't support PTP, and thus this function
608 * should only have been called during probe.
609 */
610 if (rc == -ENOSYS || rc == -EPERM)
611 netif_info(efx, probe, efx->net_dev, "no PTP support\n");
612 else if (rc)
Edward Cree1e0b8122013-05-31 18:36:12 +0100613 efx_mcdi_display_error(efx, MC_CMD_PTP,
614 MC_CMD_PTP_IN_DISABLE_LEN,
615 outbuf, sizeof(outbuf), rc);
616 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100617}
618
619static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
620{
621 struct sk_buff *skb;
622
623 while ((skb = skb_dequeue(q))) {
624 local_bh_disable();
625 netif_receive_skb(skb);
626 local_bh_enable();
627 }
628}
629
630static void efx_ptp_handle_no_channel(struct efx_nic *efx)
631{
632 netif_err(efx, drv, efx->net_dev,
633 "ERROR: PTP requires MSI-X and 1 additional interrupt"
634 "vector. PTP disabled\n");
635}
636
637/* Repeatedly send the host time to the MC which will capture the hardware
638 * time.
639 */
640static void efx_ptp_send_times(struct efx_nic *efx,
641 struct pps_event_time *last_time)
642{
643 struct pps_event_time now;
644 struct timespec limit;
645 struct efx_ptp_data *ptp = efx->ptp_data;
646 struct timespec start;
647 int *mc_running = ptp->start.addr;
648
649 pps_get_ts(&now);
650 start = now.ts_real;
651 limit = now.ts_real;
652 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
653
654 /* Write host time for specified period or until MC is done */
655 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
656 ACCESS_ONCE(*mc_running)) {
657 struct timespec update_time;
658 unsigned int host_time;
659
660 /* Don't update continuously to avoid saturating the PCIe bus */
661 update_time = now.ts_real;
662 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
663 do {
664 pps_get_ts(&now);
665 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
666 ACCESS_ONCE(*mc_running));
667
668 /* Synchronise NIC with single word of time only */
669 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
670 now.ts_real.tv_nsec);
671 /* Update host time in NIC memory */
Laurence Evans977a5d52013-03-07 11:46:58 +0000672 efx->type->ptp_write_host_time(efx, host_time);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100673 }
674 *last_time = now;
675}
676
677/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100678static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
679 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100680{
681 unsigned start_ns, end_ns;
682
683 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
Laurence Evansa6f73462013-12-04 23:47:56 +0000684 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
685 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100686 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
Laurence Evansa6f73462013-12-04 23:47:56 +0000687 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100688
689 /* Ignore seconds */
690 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
691 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
692 /* Allow for rollover */
693 if (end_ns < start_ns)
694 end_ns += NSEC_PER_SEC;
695 /* Determine duration of operation */
696 timeset->window = end_ns - start_ns;
697}
698
699/* Process times received from MC.
700 *
701 * Extract times from returned results, and establish the minimum value
702 * seen. The minimum value represents the "best" possible time and events
703 * too much greater than this are rejected - the machine is, perhaps, too
704 * busy. A number of readings are taken so that, hopefully, at least one good
705 * synchronisation will be seen in the results.
706 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100707static int
708efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
709 size_t response_length,
710 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100711{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100712 unsigned number_readings =
713 MCDI_VAR_ARRAY_LEN(response_length,
714 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100715 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100716 unsigned ngood = 0;
717 unsigned last_good = 0;
718 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100719 u32 last_sec;
720 u32 start_sec;
721 struct timespec delta;
Laurence Evansa6f73462013-12-04 23:47:56 +0000722 ktime_t mc_time;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100723
724 if (number_readings == 0)
725 return -EAGAIN;
726
Laurence Evansdfd8d582013-11-21 10:38:24 +0000727 /* Read the set of results and find the last good host-MC
728 * synchronization result. The MC times when it finishes reading the
729 * host time so the corrected window time should be fairly constant
Ben Hutchings99691c42013-12-11 02:36:08 +0000730 * for a given platform. Increment stats for any results that appear
731 * to be erroneous.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100732 */
733 for (i = 0; i < number_readings; i++) {
Laurence Evansdfd8d582013-11-21 10:38:24 +0000734 s32 window, corrected;
Laurence Evansa6f73462013-12-04 23:47:56 +0000735 struct timespec wait;
Laurence Evansdfd8d582013-11-21 10:38:24 +0000736
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100737 efx_ptp_read_timeset(
738 MCDI_ARRAY_STRUCT_PTR(synch_buf,
739 PTP_OUT_SYNCHRONIZE_TIMESET, i),
740 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100741
Laurence Evansa6f73462013-12-04 23:47:56 +0000742 wait = ktime_to_timespec(
743 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
Laurence Evansdfd8d582013-11-21 10:38:24 +0000744 window = ptp->timeset[i].window;
Laurence Evansa6f73462013-12-04 23:47:56 +0000745 corrected = window - wait.tv_nsec;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100746
Laurence Evansdfd8d582013-11-21 10:38:24 +0000747 /* We expect the uncorrected synchronization window to be at
748 * least as large as the interval between host start and end
749 * times. If it is smaller than this then this is mostly likely
750 * to be a consequence of the host's time being adjusted.
751 * Check that the corrected sync window is in a reasonable
752 * range. If it is out of range it is likely to be because an
753 * interrupt or other delay occurred between reading the system
754 * time and writing it to MC memory.
755 */
Ben Hutchings99691c42013-12-11 02:36:08 +0000756 if (window < SYNCHRONISATION_GRANULARITY_NS) {
757 ++ptp->invalid_sync_windows;
758 } else if (corrected >= MAX_SYNCHRONISATION_NS) {
Ben Hutchings99691c42013-12-11 02:36:08 +0000759 ++ptp->oversize_sync_windows;
Ben Hutchings13c92e82014-01-17 19:48:17 +0000760 } else if (corrected < ptp->min_synchronisation_ns) {
761 ++ptp->undersize_sync_windows;
Ben Hutchings99691c42013-12-11 02:36:08 +0000762 } else {
Laurence Evansdfd8d582013-11-21 10:38:24 +0000763 ngood++;
764 last_good = i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100765 }
Laurence Evansdfd8d582013-11-21 10:38:24 +0000766 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100767
768 if (ngood == 0) {
769 netif_warn(efx, drv, efx->net_dev,
Laurence Evans94cd60d2013-11-21 10:38:24 +0000770 "PTP no suitable synchronisations\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100771 return -EAGAIN;
772 }
773
Ben Hutchings92d8f762014-02-12 18:58:46 +0000774 /* Calculate delay from last good sync (host time) to last_time.
775 * It is possible that the seconds rolled over between taking
776 * the start reading and the last value written by the host. The
777 * timescales are such that a gap of more than one second is never
778 * expected. delta is *not* normalised.
779 */
780 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
781 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
782 if (start_sec != last_sec &&
783 ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
784 netif_warn(efx, hw, efx->net_dev,
785 "PTP bad synchronisation seconds\n");
786 return -EAGAIN;
787 }
788 delta.tv_sec = (last_sec - start_sec) & 1;
789 delta.tv_nsec =
790 last_time->ts_real.tv_nsec -
791 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
792
793 /* Convert the NIC time at last good sync into kernel time.
794 * No correction is required - this time is the output of a
795 * firmware process.
Laurence Evansa6f73462013-12-04 23:47:56 +0000796 */
797 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
798 ptp->timeset[last_good].minor, 0);
799
Ben Hutchings92d8f762014-02-12 18:58:46 +0000800 /* Calculate delay from NIC top of second to last_time */
801 delta.tv_nsec += ktime_to_timespec(mc_time).tv_nsec;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100802
Ben Hutchings92d8f762014-02-12 18:58:46 +0000803 /* Set PPS timestamp to match NIC top of second */
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100804 ptp->host_time_pps = *last_time;
805 pps_sub_ts(&ptp->host_time_pps, delta);
806
807 return 0;
808}
809
810/* Synchronize times between the host and the MC */
811static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
812{
813 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100814 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100815 size_t response_length;
816 int rc;
817 unsigned long timeout;
818 struct pps_event_time last_time = {};
819 unsigned int loops = 0;
820 int *start = ptp->start.addr;
821
822 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000823 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100824 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
825 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100826 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
827 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100828
829 /* Clear flag that signals MC ready */
830 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100831 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
832 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
833 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100834
835 /* Wait for start from MCDI (or timeout) */
836 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
837 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
838 udelay(20); /* Usually start MCDI execution quickly */
839 loops++;
840 }
841
Ben Hutchings99691c42013-12-11 02:36:08 +0000842 if (loops <= 1)
843 ++ptp->fast_syncs;
844 if (!time_before(jiffies, timeout))
845 ++ptp->sync_timeouts;
846
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100847 if (ACCESS_ONCE(*start))
848 efx_ptp_send_times(efx, &last_time);
849
850 /* Collect results */
851 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
852 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
853 synch_buf, sizeof(synch_buf),
854 &response_length);
Ben Hutchings99691c42013-12-11 02:36:08 +0000855 if (rc == 0) {
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100856 rc = efx_ptp_process_times(efx, synch_buf, response_length,
857 &last_time);
Ben Hutchings99691c42013-12-11 02:36:08 +0000858 if (rc == 0)
859 ++ptp->good_syncs;
860 else
861 ++ptp->no_time_syncs;
862 }
863
864 /* Increment the bad syncs counter if the synchronize fails, whatever
865 * the reason.
866 */
867 if (rc != 0)
868 ++ptp->bad_syncs;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100869
870 return rc;
871}
872
873/* Transmit a PTP packet, via the MCDI interface, to the wire. */
874static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
875{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100876 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100877 struct skb_shared_hwtstamps timestamps;
878 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100879 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100880 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100881
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100882 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000883 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100884 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100885 if (skb_shinfo(skb)->nr_frags != 0) {
886 rc = skb_linearize(skb);
887 if (rc != 0)
888 goto fail;
889 }
890
891 if (skb->ip_summed == CHECKSUM_PARTIAL) {
892 rc = skb_checksum_help(skb);
893 if (rc != 0)
894 goto fail;
895 }
896 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100897 MCDI_PTR(ptp_data->txbuf,
898 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100899 skb->len);
900 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
901 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
902 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100903 if (rc != 0)
904 goto fail;
905
906 memset(&timestamps, 0, sizeof(timestamps));
Laurence Evansa6f73462013-12-04 23:47:56 +0000907 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
908 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
909 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
910 ptp_data->ts_corrections.tx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100911
912 skb_tstamp_tx(skb, &timestamps);
913
914 rc = 0;
915
916fail:
917 dev_kfree_skb(skb);
918
919 return rc;
920}
921
922static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
923{
924 struct efx_ptp_data *ptp = efx->ptp_data;
925 struct list_head *cursor;
926 struct list_head *next;
927
Jon Cooperbd9a2652013-11-18 12:54:41 +0000928 if (ptp->rx_ts_inline)
929 return;
930
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100931 /* Drop time-expired events */
932 spin_lock_bh(&ptp->evt_lock);
933 if (!list_empty(&ptp->evt_list)) {
934 list_for_each_safe(cursor, next, &ptp->evt_list) {
935 struct efx_ptp_event_rx *evt;
936
937 evt = list_entry(cursor, struct efx_ptp_event_rx,
938 link);
939 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000940 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100941 netif_warn(efx, hw, efx->net_dev,
942 "PTP rx event dropped\n");
943 }
944 }
945 }
946 spin_unlock_bh(&ptp->evt_lock);
947}
948
949static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
950 struct sk_buff *skb)
951{
952 struct efx_ptp_data *ptp = efx->ptp_data;
953 bool evts_waiting;
954 struct list_head *cursor;
955 struct list_head *next;
956 struct efx_ptp_match *match;
957 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
958
Jon Cooperbd9a2652013-11-18 12:54:41 +0000959 WARN_ON_ONCE(ptp->rx_ts_inline);
960
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100961 spin_lock_bh(&ptp->evt_lock);
962 evts_waiting = !list_empty(&ptp->evt_list);
963 spin_unlock_bh(&ptp->evt_lock);
964
965 if (!evts_waiting)
966 return PTP_PACKET_STATE_UNMATCHED;
967
968 match = (struct efx_ptp_match *)skb->cb;
969 /* Look for a matching timestamp in the event queue */
970 spin_lock_bh(&ptp->evt_lock);
971 list_for_each_safe(cursor, next, &ptp->evt_list) {
972 struct efx_ptp_event_rx *evt;
973
974 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
975 if ((evt->seq0 == match->words[0]) &&
976 (evt->seq1 == match->words[1])) {
977 struct skb_shared_hwtstamps *timestamps;
978
979 /* Match - add in hardware timestamp */
980 timestamps = skb_hwtstamps(skb);
981 timestamps->hwtstamp = evt->hwtimestamp;
982
983 match->state = PTP_PACKET_STATE_MATCHED;
984 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000985 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100986 break;
987 }
988 }
989 spin_unlock_bh(&ptp->evt_lock);
990
991 return rc;
992}
993
994/* Process any queued receive events and corresponding packets
995 *
996 * q is returned with all the packets that are ready for delivery.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100997 */
Ben Hutchingsbbbe7142013-11-28 18:58:12 +0000998static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100999{
1000 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001001 struct sk_buff *skb;
1002
1003 while ((skb = skb_dequeue(&ptp->rxq))) {
1004 struct efx_ptp_match *match;
1005
1006 match = (struct efx_ptp_match *)skb->cb;
1007 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1008 __skb_queue_tail(q, skb);
1009 } else if (efx_ptp_match_rx(efx, skb) ==
1010 PTP_PACKET_STATE_MATCHED) {
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001011 __skb_queue_tail(q, skb);
1012 } else if (time_after(jiffies, match->expiry)) {
1013 match->state = PTP_PACKET_STATE_TIMED_OUT;
Ben Hutchings99691c42013-12-11 02:36:08 +00001014 ++ptp->rx_no_timestamp;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001015 __skb_queue_tail(q, skb);
1016 } else {
1017 /* Replace unprocessed entry and stop */
1018 skb_queue_head(&ptp->rxq, skb);
1019 break;
1020 }
1021 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001022}
1023
1024/* Complete processing of a received packet */
1025static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1026{
1027 local_bh_disable();
1028 netif_receive_skb(skb);
1029 local_bh_enable();
1030}
1031
Ben Hutchings62a1c702013-10-15 17:54:56 +01001032static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1033{
1034 struct efx_ptp_data *ptp = efx->ptp_data;
1035
1036 if (ptp->rxfilter_installed) {
1037 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1038 ptp->rxfilter_general);
1039 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1040 ptp->rxfilter_event);
1041 ptp->rxfilter_installed = false;
1042 }
1043}
1044
1045static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001046{
1047 struct efx_ptp_data *ptp = efx->ptp_data;
1048 struct efx_filter_spec rxfilter;
1049 int rc;
1050
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001051 if (!ptp->channel || ptp->rxfilter_installed)
Ben Hutchings62a1c702013-10-15 17:54:56 +01001052 return 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001053
1054 /* Must filter on both event and general ports to ensure
1055 * that there is no packet re-ordering.
1056 */
1057 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1058 efx_rx_queue_index(
1059 efx_channel_get_rx_queue(ptp->channel)));
1060 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1061 htonl(PTP_ADDRESS),
1062 htons(PTP_EVENT_PORT));
1063 if (rc != 0)
1064 return rc;
1065
1066 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1067 if (rc < 0)
1068 return rc;
1069 ptp->rxfilter_event = rc;
1070
1071 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1072 efx_rx_queue_index(
1073 efx_channel_get_rx_queue(ptp->channel)));
1074 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1075 htonl(PTP_ADDRESS),
1076 htons(PTP_GENERAL_PORT));
1077 if (rc != 0)
1078 goto fail;
1079
1080 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1081 if (rc < 0)
1082 goto fail;
1083 ptp->rxfilter_general = rc;
1084
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001085 ptp->rxfilter_installed = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001086 return 0;
1087
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001088fail:
1089 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1090 ptp->rxfilter_event);
Ben Hutchings62a1c702013-10-15 17:54:56 +01001091 return rc;
1092}
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001093
Ben Hutchings62a1c702013-10-15 17:54:56 +01001094static int efx_ptp_start(struct efx_nic *efx)
1095{
1096 struct efx_ptp_data *ptp = efx->ptp_data;
1097 int rc;
1098
1099 ptp->reset_required = false;
1100
1101 rc = efx_ptp_insert_multicast_filters(efx);
1102 if (rc)
1103 return rc;
1104
1105 rc = efx_ptp_enable(efx);
1106 if (rc != 0)
1107 goto fail;
1108
1109 ptp->evt_frag_idx = 0;
1110 ptp->current_adjfreq = 0;
1111
1112 return 0;
1113
1114fail:
1115 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001116 return rc;
1117}
1118
1119static int efx_ptp_stop(struct efx_nic *efx)
1120{
1121 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001122 struct list_head *cursor;
1123 struct list_head *next;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001124 int rc;
1125
1126 if (ptp == NULL)
1127 return 0;
1128
1129 rc = efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001130
Ben Hutchings62a1c702013-10-15 17:54:56 +01001131 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001132
1133 /* Make sure RX packets are really delivered */
1134 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1135 skb_queue_purge(&efx->ptp_data->txq);
1136
1137 /* Drop any pending receive events */
1138 spin_lock_bh(&efx->ptp_data->evt_lock);
1139 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +00001140 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001141 }
1142 spin_unlock_bh(&efx->ptp_data->evt_lock);
1143
1144 return rc;
1145}
1146
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001147static int efx_ptp_restart(struct efx_nic *efx)
1148{
1149 if (efx->ptp_data && efx->ptp_data->enabled)
1150 return efx_ptp_start(efx);
1151 return 0;
1152}
1153
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001154static void efx_ptp_pps_worker(struct work_struct *work)
1155{
1156 struct efx_ptp_data *ptp =
1157 container_of(work, struct efx_ptp_data, pps_work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001158 struct efx_nic *efx = ptp->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001159 struct ptp_clock_event ptp_evt;
1160
1161 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1162 return;
1163
1164 ptp_evt.type = PTP_CLOCK_PPSUSR;
1165 ptp_evt.pps_times = ptp->host_time_pps;
1166 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1167}
1168
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001169static void efx_ptp_worker(struct work_struct *work)
1170{
1171 struct efx_ptp_data *ptp_data =
1172 container_of(work, struct efx_ptp_data, work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001173 struct efx_nic *efx = ptp_data->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001174 struct sk_buff *skb;
1175 struct sk_buff_head tempq;
1176
1177 if (ptp_data->reset_required) {
1178 efx_ptp_stop(efx);
1179 efx_ptp_start(efx);
1180 return;
1181 }
1182
1183 efx_ptp_drop_time_expired_events(efx);
1184
1185 __skb_queue_head_init(&tempq);
Ben Hutchingsbbbe7142013-11-28 18:58:12 +00001186 efx_ptp_process_events(efx, &tempq);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001187
Ben Hutchingsbbbe7142013-11-28 18:58:12 +00001188 while ((skb = skb_dequeue(&ptp_data->txq)))
1189 efx_ptp_xmit_skb(efx, skb);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001190
1191 while ((skb = __skb_dequeue(&tempq)))
1192 efx_ptp_process_rx(efx, skb);
1193}
1194
Ben Hutchings5d0dab02013-10-16 18:32:41 +01001195static const struct ptp_clock_info efx_phc_clock_info = {
1196 .owner = THIS_MODULE,
1197 .name = "sfc",
1198 .max_adj = MAX_PPB,
1199 .n_alarm = 0,
1200 .n_ext_ts = 0,
1201 .n_per_out = 0,
Richard Cochran4986b4f02014-03-20 22:21:55 +01001202 .n_pins = 0,
Ben Hutchings5d0dab02013-10-16 18:32:41 +01001203 .pps = 1,
1204 .adjfreq = efx_phc_adjfreq,
1205 .adjtime = efx_phc_adjtime,
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001206 .gettime64 = efx_phc_gettime,
1207 .settime64 = efx_phc_settime,
Ben Hutchings5d0dab02013-10-16 18:32:41 +01001208 .enable = efx_phc_enable,
1209};
1210
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001211/* Initialise PTP state. */
1212int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001213{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001214 struct efx_ptp_data *ptp;
1215 int rc = 0;
1216 unsigned int pos;
1217
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001218 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1219 efx->ptp_data = ptp;
1220 if (!efx->ptp_data)
1221 return -ENOMEM;
1222
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001223 ptp->efx = efx;
1224 ptp->channel = channel;
Jon Cooperbd9a2652013-11-18 12:54:41 +00001225 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001226
Ben Hutchings0d19a542012-09-18 21:59:52 +01001227 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001228 if (rc != 0)
1229 goto fail1;
1230
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001231 skb_queue_head_init(&ptp->rxq);
1232 skb_queue_head_init(&ptp->txq);
1233 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1234 if (!ptp->workwq) {
1235 rc = -ENOMEM;
1236 goto fail2;
1237 }
1238
1239 INIT_WORK(&ptp->work, efx_ptp_worker);
1240 ptp->config.flags = 0;
1241 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1242 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1243 INIT_LIST_HEAD(&ptp->evt_list);
1244 INIT_LIST_HEAD(&ptp->evt_free_list);
1245 spin_lock_init(&ptp->evt_lock);
1246 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1247 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1248
Laurence Evansa6f73462013-12-04 23:47:56 +00001249 /* Get the NIC PTP attributes and set up time conversions */
1250 rc = efx_ptp_get_attributes(efx);
1251 if (rc < 0)
1252 goto fail3;
1253
1254 /* Get the timestamp corrections */
1255 rc = efx_ptp_get_timestamp_corrections(efx);
1256 if (rc < 0)
1257 goto fail3;
1258
Ben Hutchings9aecda92013-12-05 21:28:42 +00001259 if (efx->mcdi->fn_flags &
1260 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1261 ptp->phc_clock_info = efx_phc_clock_info;
1262 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1263 &efx->pci_dev->dev);
1264 if (IS_ERR(ptp->phc_clock)) {
1265 rc = PTR_ERR(ptp->phc_clock);
1266 goto fail3;
1267 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001268
Ben Hutchings9aecda92013-12-05 21:28:42 +00001269 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1270 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1271 if (!ptp->pps_workwq) {
1272 rc = -ENOMEM;
1273 goto fail4;
1274 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001275 }
1276 ptp->nic_ts_enabled = false;
1277
1278 return 0;
1279fail4:
1280 ptp_clock_unregister(efx->ptp_data->phc_clock);
1281
1282fail3:
1283 destroy_workqueue(efx->ptp_data->workwq);
1284
1285fail2:
1286 efx_nic_free_buffer(efx, &ptp->start);
1287
1288fail1:
1289 kfree(efx->ptp_data);
1290 efx->ptp_data = NULL;
1291
1292 return rc;
1293}
1294
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001295/* Initialise PTP channel.
1296 *
1297 * Setting core_index to zero causes the queue to be initialised and doesn't
1298 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1299 */
1300static int efx_ptp_probe_channel(struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001301{
1302 struct efx_nic *efx = channel->efx;
1303
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001304 channel->irq_moderation = 0;
1305 channel->rx_queue.core_index = 0;
1306
1307 return efx_ptp_probe(efx, channel);
1308}
1309
1310void efx_ptp_remove(struct efx_nic *efx)
1311{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001312 if (!efx->ptp_data)
1313 return;
1314
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001315 (void)efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001316
1317 cancel_work_sync(&efx->ptp_data->work);
1318 cancel_work_sync(&efx->ptp_data->pps_work);
1319
1320 skb_queue_purge(&efx->ptp_data->rxq);
1321 skb_queue_purge(&efx->ptp_data->txq);
1322
Ben Hutchings9aecda92013-12-05 21:28:42 +00001323 if (efx->ptp_data->phc_clock) {
1324 destroy_workqueue(efx->ptp_data->pps_workwq);
1325 ptp_clock_unregister(efx->ptp_data->phc_clock);
1326 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001327
1328 destroy_workqueue(efx->ptp_data->workwq);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001329
1330 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1331 kfree(efx->ptp_data);
1332}
1333
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001334static void efx_ptp_remove_channel(struct efx_channel *channel)
1335{
1336 efx_ptp_remove(channel->efx);
1337}
1338
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001339static void efx_ptp_get_channel_name(struct efx_channel *channel,
1340 char *buf, size_t len)
1341{
1342 snprintf(buf, len, "%s-ptp", channel->efx->name);
1343}
1344
1345/* Determine whether this packet should be processed by the PTP module
1346 * or transmitted conventionally.
1347 */
1348bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1349{
1350 return efx->ptp_data &&
1351 efx->ptp_data->enabled &&
1352 skb->len >= PTP_MIN_LENGTH &&
1353 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1354 likely(skb->protocol == htons(ETH_P_IP)) &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001355 skb_transport_header_was_set(skb) &&
1356 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001357 ip_hdr(skb)->protocol == IPPROTO_UDP &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001358 skb_headlen(skb) >=
1359 skb_transport_offset(skb) + sizeof(struct udphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001360 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1361}
1362
1363/* Receive a PTP packet. Packets are queued until the arrival of
1364 * the receive timestamp from the MC - this will probably occur after the
1365 * packet arrival because of the processing in the MC.
1366 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001367static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001368{
1369 struct efx_nic *efx = channel->efx;
1370 struct efx_ptp_data *ptp = efx->ptp_data;
1371 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001372 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001373 unsigned int version;
Ben Hutchingsce320f42014-02-12 18:58:34 +00001374 u8 *data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001375
1376 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1377
1378 /* Correct version? */
1379 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001380 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001381 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001382 }
Ben Hutchingsce320f42014-02-12 18:58:34 +00001383 data = skb->data;
1384 version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001385 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001386 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001387 }
Laurence Evansc939a312012-11-15 10:56:07 +00001388
1389 /* PTP V1 uses all six bytes of the UUID to match the packet
1390 * to the timestamp
1391 */
Ben Hutchingsce320f42014-02-12 18:58:34 +00001392 match_data_012 = data + PTP_V1_UUID_OFFSET;
1393 match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001394 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001395 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001396 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001397 }
Ben Hutchingsce320f42014-02-12 18:58:34 +00001398 data = skb->data;
1399 version = data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001400 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001401 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001402 }
Laurence Evansc939a312012-11-15 10:56:07 +00001403
1404 /* The original V2 implementation uses bytes 2-7 of
1405 * the UUID to match the packet to the timestamp. This
1406 * discards two of the bytes of the MAC address used
1407 * to create the UUID (SF bug 33070). The PTP V2
1408 * enhanced mode fixes this issue and uses bytes 0-2
1409 * and byte 5-7 of the UUID.
1410 */
Ben Hutchingsce320f42014-02-12 18:58:34 +00001411 match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
Laurence Evansc939a312012-11-15 10:56:07 +00001412 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
Ben Hutchingsce320f42014-02-12 18:58:34 +00001413 match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
Laurence Evansc939a312012-11-15 10:56:07 +00001414 } else {
Ben Hutchingsce320f42014-02-12 18:58:34 +00001415 match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
Laurence Evansc939a312012-11-15 10:56:07 +00001416 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1417 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001418 }
1419
1420 /* Does this packet require timestamping? */
Ben Hutchingsce320f42014-02-12 18:58:34 +00001421 if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001422 match->state = PTP_PACKET_STATE_UNMATCHED;
1423
Laurence Evansc939a312012-11-15 10:56:07 +00001424 /* We expect the sequence number to be in the same position in
1425 * the packet for PTP V1 and V2
1426 */
1427 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1428 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1429
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001430 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001431 match->words[0] = (match_data_012[0] |
1432 (match_data_012[1] << 8) |
1433 (match_data_012[2] << 16) |
1434 (match_data_345[0] << 24));
1435 match->words[1] = (match_data_345[1] |
1436 (match_data_345[2] << 8) |
Ben Hutchingsce320f42014-02-12 18:58:34 +00001437 (data[PTP_V1_SEQUENCE_OFFSET +
1438 PTP_V1_SEQUENCE_LENGTH - 1] <<
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001439 16));
1440 } else {
1441 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1442 }
1443
1444 skb_queue_tail(&ptp->rxq, skb);
1445 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001446
1447 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001448}
1449
1450/* Transmit a PTP packet. This has to be transmitted by the MC
1451 * itself, through an MCDI call. MCDI calls aren't permitted
1452 * in the transmit path so defer the actual transmission to a suitable worker.
1453 */
1454int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1455{
1456 struct efx_ptp_data *ptp = efx->ptp_data;
1457
1458 skb_queue_tail(&ptp->txq, skb);
1459
1460 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1461 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1462 efx_xmit_hwtstamp_pending(skb);
1463 queue_work(ptp->workwq, &ptp->work);
1464
1465 return NETDEV_TX_OK;
1466}
1467
Daniel Pieczko9ec06592013-11-21 17:11:25 +00001468int efx_ptp_get_mode(struct efx_nic *efx)
1469{
1470 return efx->ptp_data->mode;
1471}
1472
1473int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1474 unsigned int new_mode)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001475{
1476 if ((enable_wanted != efx->ptp_data->enabled) ||
1477 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001478 int rc = 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001479
1480 if (enable_wanted) {
1481 /* Change of mode requires disable */
1482 if (efx->ptp_data->enabled &&
1483 (efx->ptp_data->mode != new_mode)) {
1484 efx->ptp_data->enabled = false;
1485 rc = efx_ptp_stop(efx);
1486 if (rc != 0)
1487 return rc;
1488 }
1489
1490 /* Set new operating mode and establish
1491 * baseline synchronisation, which must
1492 * succeed.
1493 */
1494 efx->ptp_data->mode = new_mode;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001495 if (netif_running(efx->net_dev))
1496 rc = efx_ptp_start(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001497 if (rc == 0) {
1498 rc = efx_ptp_synchronize(efx,
1499 PTP_SYNC_ATTEMPTS * 2);
1500 if (rc != 0)
1501 efx_ptp_stop(efx);
1502 }
1503 } else {
1504 rc = efx_ptp_stop(efx);
1505 }
1506
1507 if (rc != 0)
1508 return rc;
1509
1510 efx->ptp_data->enabled = enable_wanted;
1511 }
1512
1513 return 0;
1514}
1515
1516static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1517{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001518 int rc;
1519
1520 if (init->flags)
1521 return -EINVAL;
1522
1523 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1524 (init->tx_type != HWTSTAMP_TX_ON))
1525 return -ERANGE;
1526
Daniel Pieczko9ec06592013-11-21 17:11:25 +00001527 rc = efx->type->ptp_set_ts_config(efx, init);
1528 if (rc)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001529 return rc;
1530
1531 efx->ptp_data->config = *init;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001532 return 0;
1533}
1534
Ben Hutchings62ebac92013-04-08 17:34:58 +01001535void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001536{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001537 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings9aecda92013-12-05 21:28:42 +00001538 struct efx_nic *primary = efx->primary;
1539
1540 ASSERT_RTNL();
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001541
1542 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001543 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001544
Ben Hutchings62ebac92013-04-08 17:34:58 +01001545 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1546 SOF_TIMESTAMPING_RX_HARDWARE |
1547 SOF_TIMESTAMPING_RAW_HARDWARE);
Ben Hutchings9aecda92013-12-05 21:28:42 +00001548 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1549 ts_info->phc_index =
1550 ptp_clock_index(primary->ptp_data->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001551 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
Daniel Pieczko9ec06592013-11-21 17:11:25 +00001552 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001553}
1554
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001555int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001556{
1557 struct hwtstamp_config config;
1558 int rc;
1559
1560 /* Not a PTP enabled port */
1561 if (!efx->ptp_data)
1562 return -EOPNOTSUPP;
1563
1564 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1565 return -EFAULT;
1566
1567 rc = efx_ptp_ts_init(efx, &config);
1568 if (rc != 0)
1569 return rc;
1570
1571 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1572 ? -EFAULT : 0;
1573}
1574
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001575int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1576{
1577 if (!efx->ptp_data)
1578 return -EOPNOTSUPP;
1579
1580 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1581 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1582}
1583
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001584static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1585{
1586 struct efx_ptp_data *ptp = efx->ptp_data;
1587
1588 netif_err(efx, hw, efx->net_dev,
1589 "PTP unexpected event length: got %d expected %d\n",
1590 ptp->evt_frag_idx, expected_frag_len);
1591 ptp->reset_required = true;
1592 queue_work(ptp->workwq, &ptp->work);
1593}
1594
1595/* Process a completed receive event. Put it on the event queue and
1596 * start worker thread. This is required because event and their
1597 * correspoding packets may come in either order.
1598 */
1599static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1600{
1601 struct efx_ptp_event_rx *evt = NULL;
1602
Jon Cooperbd9a2652013-11-18 12:54:41 +00001603 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1604 return;
1605
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001606 if (ptp->evt_frag_idx != 3) {
1607 ptp_event_failure(efx, 3);
1608 return;
1609 }
1610
1611 spin_lock_bh(&ptp->evt_lock);
1612 if (!list_empty(&ptp->evt_free_list)) {
1613 evt = list_first_entry(&ptp->evt_free_list,
1614 struct efx_ptp_event_rx, link);
1615 list_del(&evt->link);
1616
1617 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1618 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1619 MCDI_EVENT_SRC) |
1620 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1621 MCDI_EVENT_SRC) << 8) |
1622 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1623 MCDI_EVENT_SRC) << 16));
Laurence Evansa6f73462013-12-04 23:47:56 +00001624 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001625 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
Laurence Evansa6f73462013-12-04 23:47:56 +00001626 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1627 ptp->ts_corrections.rx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001628 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1629 list_add_tail(&evt->link, &ptp->evt_list);
1630
1631 queue_work(ptp->workwq, &ptp->work);
Laurence Evansf9fd7ec72014-02-12 18:58:24 +00001632 } else if (net_ratelimit()) {
1633 /* Log a rate-limited warning message. */
Laurence Evansf3211602013-01-28 14:51:17 +00001634 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001635 }
1636 spin_unlock_bh(&ptp->evt_lock);
1637}
1638
1639static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1640{
1641 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1642 if (ptp->evt_frag_idx != 1) {
1643 ptp_event_failure(efx, 1);
1644 return;
1645 }
1646
1647 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1648}
1649
1650static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1651{
1652 if (ptp->nic_ts_enabled)
1653 queue_work(ptp->pps_workwq, &ptp->pps_work);
1654}
1655
1656void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1657{
1658 struct efx_ptp_data *ptp = efx->ptp_data;
1659 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1660
Edward Cree8f355e52014-02-25 13:17:59 +00001661 if (!ptp) {
1662 if (net_ratelimit())
1663 netif_warn(efx, drv, efx->net_dev,
1664 "Received PTP event but PTP not set up\n");
1665 return;
1666 }
1667
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001668 if (!ptp->enabled)
1669 return;
1670
1671 if (ptp->evt_frag_idx == 0) {
1672 ptp->evt_code = code;
1673 } else if (ptp->evt_code != code) {
1674 netif_err(efx, hw, efx->net_dev,
1675 "PTP out of sequence event %d\n", code);
1676 ptp->evt_frag_idx = 0;
1677 }
1678
1679 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1680 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1681 /* Process resulting event */
1682 switch (code) {
1683 case MCDI_EVENT_CODE_PTP_RX:
1684 ptp_event_rx(efx, ptp);
1685 break;
1686 case MCDI_EVENT_CODE_PTP_FAULT:
1687 ptp_event_fault(efx, ptp);
1688 break;
1689 case MCDI_EVENT_CODE_PTP_PPS:
1690 ptp_event_pps(efx, ptp);
1691 break;
1692 default:
1693 netif_err(efx, hw, efx->net_dev,
1694 "PTP unknown event %d\n", code);
1695 break;
1696 }
1697 ptp->evt_frag_idx = 0;
1698 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1699 netif_err(efx, hw, efx->net_dev,
1700 "PTP too many event fragments\n");
1701 ptp->evt_frag_idx = 0;
1702 }
1703}
1704
Jon Cooperbd9a2652013-11-18 12:54:41 +00001705void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1706{
1707 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1708 channel->sync_timestamp_minor =
1709 MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_26_19) << 19;
1710 /* if sync events have been disabled then we want to silently ignore
1711 * this event, so throw away result.
1712 */
1713 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1714 SYNC_EVENTS_VALID);
1715}
1716
1717/* make some assumptions about the time representation rather than abstract it,
1718 * since we currently only support one type of inline timestamping and only on
1719 * EF10.
1720 */
1721#define MINOR_TICKS_PER_SECOND 0x8000000
1722/* Fuzz factor for sync events to be out of order with RX events */
1723#define FUZZ (MINOR_TICKS_PER_SECOND / 10)
1724#define EXPECTED_SYNC_EVENTS_PER_SECOND 4
1725
1726static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1727{
1728#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1729 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1730#else
1731 const u8 *data = eh + efx->rx_packet_ts_offset;
1732 return (u32)data[0] |
1733 (u32)data[1] << 8 |
1734 (u32)data[2] << 16 |
1735 (u32)data[3] << 24;
1736#endif
1737}
1738
1739void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
1740 struct sk_buff *skb)
1741{
1742 struct efx_nic *efx = channel->efx;
1743 u32 pkt_timestamp_major, pkt_timestamp_minor;
1744 u32 diff, carry;
1745 struct skb_shared_hwtstamps *timestamps;
1746
1747 pkt_timestamp_minor = (efx_rx_buf_timestamp_minor(efx,
1748 skb_mac_header(skb)) +
1749 (u32) efx->ptp_data->ts_corrections.rx) &
1750 (MINOR_TICKS_PER_SECOND - 1);
1751
1752 /* get the difference between the packet and sync timestamps,
1753 * modulo one second
1754 */
1755 diff = (pkt_timestamp_minor - channel->sync_timestamp_minor) &
1756 (MINOR_TICKS_PER_SECOND - 1);
1757 /* do we roll over a second boundary and need to carry the one? */
1758 carry = channel->sync_timestamp_minor + diff > MINOR_TICKS_PER_SECOND ?
1759 1 : 0;
1760
1761 if (diff <= MINOR_TICKS_PER_SECOND / EXPECTED_SYNC_EVENTS_PER_SECOND +
1762 FUZZ) {
1763 /* packet is ahead of the sync event by a quarter of a second or
1764 * less (allowing for fuzz)
1765 */
1766 pkt_timestamp_major = channel->sync_timestamp_major + carry;
1767 } else if (diff >= MINOR_TICKS_PER_SECOND - FUZZ) {
1768 /* packet is behind the sync event but within the fuzz factor.
1769 * This means the RX packet and sync event crossed as they were
1770 * placed on the event queue, which can sometimes happen.
1771 */
1772 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
1773 } else {
1774 /* it's outside tolerance in both directions. this might be
1775 * indicative of us missing sync events for some reason, so
1776 * we'll call it an error rather than risk giving a bogus
1777 * timestamp.
1778 */
1779 netif_vdbg(efx, drv, efx->net_dev,
1780 "packet timestamp %x too far from sync event %x:%x\n",
1781 pkt_timestamp_minor, channel->sync_timestamp_major,
1782 channel->sync_timestamp_minor);
1783 return;
1784 }
1785
1786 /* attach the timestamps to the skb */
1787 timestamps = skb_hwtstamps(skb);
1788 timestamps->hwtstamp =
1789 efx_ptp_s27_to_ktime(pkt_timestamp_major, pkt_timestamp_minor);
1790}
1791
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001792static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1793{
1794 struct efx_ptp_data *ptp_data = container_of(ptp,
1795 struct efx_ptp_data,
1796 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001797 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001798 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001799 s64 adjustment_ns;
1800 int rc;
1801
1802 if (delta > MAX_PPB)
1803 delta = MAX_PPB;
1804 else if (delta < -MAX_PPB)
1805 delta = -MAX_PPB;
1806
1807 /* Convert ppb to fixed point ns. */
1808 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1809 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1810
1811 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001812 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001813 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001814 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1815 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1816 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1817 NULL, 0, NULL);
1818 if (rc != 0)
1819 return rc;
1820
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001821 ptp_data->current_adjfreq = adjustment_ns;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001822 return 0;
1823}
1824
1825static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1826{
Laurence Evansa6f73462013-12-04 23:47:56 +00001827 u32 nic_major, nic_minor;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001828 struct efx_ptp_data *ptp_data = container_of(ptp,
1829 struct efx_ptp_data,
1830 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001831 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001832 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001833
Laurence Evansa6f73462013-12-04 23:47:56 +00001834 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
1835
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001836 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001837 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001838 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
Laurence Evansa6f73462013-12-04 23:47:56 +00001839 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
1840 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001841 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1842 NULL, 0, NULL);
1843}
1844
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001845static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001846{
1847 struct efx_ptp_data *ptp_data = container_of(ptp,
1848 struct efx_ptp_data,
1849 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001850 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001851 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1852 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001853 int rc;
Laurence Evansa6f73462013-12-04 23:47:56 +00001854 ktime_t kt;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001855
1856 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001857 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001858
1859 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1860 outbuf, sizeof(outbuf), NULL);
1861 if (rc != 0)
1862 return rc;
1863
Laurence Evansa6f73462013-12-04 23:47:56 +00001864 kt = ptp_data->nic_to_kernel_time(
1865 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
1866 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001867 *ts = ktime_to_timespec64(kt);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001868 return 0;
1869}
1870
1871static int efx_phc_settime(struct ptp_clock_info *ptp,
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001872 const struct timespec64 *e_ts)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001873{
1874 /* Get the current NIC time, efx_phc_gettime.
1875 * Subtract from the desired time to get the offset
1876 * call efx_phc_adjtime with the offset
1877 */
1878 int rc;
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001879 struct timespec64 time_now;
1880 struct timespec64 delta;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001881
1882 rc = efx_phc_gettime(ptp, &time_now);
1883 if (rc != 0)
1884 return rc;
1885
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001886 delta = timespec64_sub(*e_ts, time_now);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001887
Richard Cochran0fcb5c762015-03-29 23:12:06 +02001888 rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001889 if (rc != 0)
1890 return rc;
1891
1892 return 0;
1893}
1894
1895static int efx_phc_enable(struct ptp_clock_info *ptp,
1896 struct ptp_clock_request *request,
1897 int enable)
1898{
1899 struct efx_ptp_data *ptp_data = container_of(ptp,
1900 struct efx_ptp_data,
1901 phc_clock_info);
1902 if (request->type != PTP_CLK_REQ_PPS)
1903 return -EOPNOTSUPP;
1904
1905 ptp_data->nic_ts_enabled = !!enable;
1906 return 0;
1907}
1908
1909static const struct efx_channel_type efx_ptp_channel_type = {
1910 .handle_no_channel = efx_ptp_handle_no_channel,
1911 .pre_probe = efx_ptp_probe_channel,
1912 .post_remove = efx_ptp_remove_channel,
1913 .get_name = efx_ptp_get_channel_name,
1914 /* no copy operation; there is no need to reallocate this channel */
1915 .receive_skb = efx_ptp_rx,
1916 .keep_eventq = false,
1917};
1918
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001919void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001920{
1921 /* Check whether PTP is implemented on this NIC. The DISABLE
1922 * operation will succeed if and only if it is implemented.
1923 */
1924 if (efx_ptp_disable(efx) == 0)
1925 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1926 &efx_ptp_channel_type;
1927}
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001928
1929void efx_ptp_start_datapath(struct efx_nic *efx)
1930{
1931 if (efx_ptp_restart(efx))
1932 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
Jon Cooperbd9a2652013-11-18 12:54:41 +00001933 /* re-enable timestamping if it was previously enabled */
1934 if (efx->type->ptp_set_ts_sync_events)
1935 efx->type->ptp_set_ts_sync_events(efx, true, true);
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001936}
1937
1938void efx_ptp_stop_datapath(struct efx_nic *efx)
1939{
Jon Cooperbd9a2652013-11-18 12:54:41 +00001940 /* temporarily disable timestamping */
1941 if (efx->type->ptp_set_ts_sync_events)
1942 efx->type->ptp_set_ts_sync_events(efx, false, true);
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001943 efx_ptp_stop(efx);
1944}