blob: 52be63dc68cf9220194677076da6d8153e247c8d [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);
326static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts);
327static int efx_phc_settime(struct ptp_clock_info *ptp,
328 const struct timespec *e_ts);
329static 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;
607 if (rc)
608 efx_mcdi_display_error(efx, MC_CMD_PTP,
609 MC_CMD_PTP_IN_DISABLE_LEN,
610 outbuf, sizeof(outbuf), rc);
611 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100612}
613
614static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
615{
616 struct sk_buff *skb;
617
618 while ((skb = skb_dequeue(q))) {
619 local_bh_disable();
620 netif_receive_skb(skb);
621 local_bh_enable();
622 }
623}
624
625static void efx_ptp_handle_no_channel(struct efx_nic *efx)
626{
627 netif_err(efx, drv, efx->net_dev,
628 "ERROR: PTP requires MSI-X and 1 additional interrupt"
629 "vector. PTP disabled\n");
630}
631
632/* Repeatedly send the host time to the MC which will capture the hardware
633 * time.
634 */
635static void efx_ptp_send_times(struct efx_nic *efx,
636 struct pps_event_time *last_time)
637{
638 struct pps_event_time now;
639 struct timespec limit;
640 struct efx_ptp_data *ptp = efx->ptp_data;
641 struct timespec start;
642 int *mc_running = ptp->start.addr;
643
644 pps_get_ts(&now);
645 start = now.ts_real;
646 limit = now.ts_real;
647 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
648
649 /* Write host time for specified period or until MC is done */
650 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
651 ACCESS_ONCE(*mc_running)) {
652 struct timespec update_time;
653 unsigned int host_time;
654
655 /* Don't update continuously to avoid saturating the PCIe bus */
656 update_time = now.ts_real;
657 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
658 do {
659 pps_get_ts(&now);
660 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
661 ACCESS_ONCE(*mc_running));
662
663 /* Synchronise NIC with single word of time only */
664 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
665 now.ts_real.tv_nsec);
666 /* Update host time in NIC memory */
Laurence Evans977a5d52013-03-07 11:46:58 +0000667 efx->type->ptp_write_host_time(efx, host_time);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100668 }
669 *last_time = now;
670}
671
672/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100673static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
674 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100675{
676 unsigned start_ns, end_ns;
677
678 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
Laurence Evansa6f73462013-12-04 23:47:56 +0000679 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
680 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100681 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
Laurence Evansa6f73462013-12-04 23:47:56 +0000682 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100683
684 /* Ignore seconds */
685 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
686 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
687 /* Allow for rollover */
688 if (end_ns < start_ns)
689 end_ns += NSEC_PER_SEC;
690 /* Determine duration of operation */
691 timeset->window = end_ns - start_ns;
692}
693
694/* Process times received from MC.
695 *
696 * Extract times from returned results, and establish the minimum value
697 * seen. The minimum value represents the "best" possible time and events
698 * too much greater than this are rejected - the machine is, perhaps, too
699 * busy. A number of readings are taken so that, hopefully, at least one good
700 * synchronisation will be seen in the results.
701 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100702static int
703efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
704 size_t response_length,
705 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100706{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100707 unsigned number_readings =
708 MCDI_VAR_ARRAY_LEN(response_length,
709 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100710 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100711 unsigned ngood = 0;
712 unsigned last_good = 0;
713 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100714 u32 last_sec;
715 u32 start_sec;
716 struct timespec delta;
Laurence Evansa6f73462013-12-04 23:47:56 +0000717 ktime_t mc_time;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100718
719 if (number_readings == 0)
720 return -EAGAIN;
721
Laurence Evansdfd8d582013-11-21 10:38:24 +0000722 /* Read the set of results and find the last good host-MC
723 * synchronization result. The MC times when it finishes reading the
724 * host time so the corrected window time should be fairly constant
Ben Hutchings99691c42013-12-11 02:36:08 +0000725 * for a given platform. Increment stats for any results that appear
726 * to be erroneous.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100727 */
728 for (i = 0; i < number_readings; i++) {
Laurence Evansdfd8d582013-11-21 10:38:24 +0000729 s32 window, corrected;
Laurence Evansa6f73462013-12-04 23:47:56 +0000730 struct timespec wait;
Laurence Evansdfd8d582013-11-21 10:38:24 +0000731
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100732 efx_ptp_read_timeset(
733 MCDI_ARRAY_STRUCT_PTR(synch_buf,
734 PTP_OUT_SYNCHRONIZE_TIMESET, i),
735 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100736
Laurence Evansa6f73462013-12-04 23:47:56 +0000737 wait = ktime_to_timespec(
738 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
Laurence Evansdfd8d582013-11-21 10:38:24 +0000739 window = ptp->timeset[i].window;
Laurence Evansa6f73462013-12-04 23:47:56 +0000740 corrected = window - wait.tv_nsec;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100741
Laurence Evansdfd8d582013-11-21 10:38:24 +0000742 /* We expect the uncorrected synchronization window to be at
743 * least as large as the interval between host start and end
744 * times. If it is smaller than this then this is mostly likely
745 * to be a consequence of the host's time being adjusted.
746 * Check that the corrected sync window is in a reasonable
747 * range. If it is out of range it is likely to be because an
748 * interrupt or other delay occurred between reading the system
749 * time and writing it to MC memory.
750 */
Ben Hutchings99691c42013-12-11 02:36:08 +0000751 if (window < SYNCHRONISATION_GRANULARITY_NS) {
752 ++ptp->invalid_sync_windows;
753 } else if (corrected >= MAX_SYNCHRONISATION_NS) {
Ben Hutchings99691c42013-12-11 02:36:08 +0000754 ++ptp->oversize_sync_windows;
Ben Hutchings13c92e82014-01-17 19:48:17 +0000755 } else if (corrected < ptp->min_synchronisation_ns) {
756 ++ptp->undersize_sync_windows;
Ben Hutchings99691c42013-12-11 02:36:08 +0000757 } else {
Laurence Evansdfd8d582013-11-21 10:38:24 +0000758 ngood++;
759 last_good = i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100760 }
Laurence Evansdfd8d582013-11-21 10:38:24 +0000761 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100762
763 if (ngood == 0) {
764 netif_warn(efx, drv, efx->net_dev,
Laurence Evans94cd60d2013-11-21 10:38:24 +0000765 "PTP no suitable synchronisations\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100766 return -EAGAIN;
767 }
768
Laurence Evansa6f73462013-12-04 23:47:56 +0000769 /* Convert the NIC time into kernel time. No correction is required-
770 * this time is the output of a firmware process.
771 */
772 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
773 ptp->timeset[last_good].minor, 0);
774
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100775 /* Calculate delay from actual PPS to last_time */
Laurence Evansa6f73462013-12-04 23:47:56 +0000776 delta = ktime_to_timespec(mc_time);
777 delta.tv_nsec +=
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100778 last_time->ts_real.tv_nsec -
779 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
780
781 /* It is possible that the seconds rolled over between taking
782 * the start reading and the last value written by the host. The
783 * timescales are such that a gap of more than one second is never
784 * expected.
785 */
786 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
787 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
788 if (start_sec != last_sec) {
789 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
790 netif_warn(efx, hw, efx->net_dev,
791 "PTP bad synchronisation seconds\n");
792 return -EAGAIN;
793 } else {
794 delta.tv_sec = 1;
795 }
796 } else {
797 delta.tv_sec = 0;
798 }
799
800 ptp->host_time_pps = *last_time;
801 pps_sub_ts(&ptp->host_time_pps, delta);
802
803 return 0;
804}
805
806/* Synchronize times between the host and the MC */
807static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
808{
809 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100810 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100811 size_t response_length;
812 int rc;
813 unsigned long timeout;
814 struct pps_event_time last_time = {};
815 unsigned int loops = 0;
816 int *start = ptp->start.addr;
817
818 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000819 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100820 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
821 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100822 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
823 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100824
825 /* Clear flag that signals MC ready */
826 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100827 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
828 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
829 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100830
831 /* Wait for start from MCDI (or timeout) */
832 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
833 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
834 udelay(20); /* Usually start MCDI execution quickly */
835 loops++;
836 }
837
Ben Hutchings99691c42013-12-11 02:36:08 +0000838 if (loops <= 1)
839 ++ptp->fast_syncs;
840 if (!time_before(jiffies, timeout))
841 ++ptp->sync_timeouts;
842
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100843 if (ACCESS_ONCE(*start))
844 efx_ptp_send_times(efx, &last_time);
845
846 /* Collect results */
847 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
848 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
849 synch_buf, sizeof(synch_buf),
850 &response_length);
Ben Hutchings99691c42013-12-11 02:36:08 +0000851 if (rc == 0) {
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100852 rc = efx_ptp_process_times(efx, synch_buf, response_length,
853 &last_time);
Ben Hutchings99691c42013-12-11 02:36:08 +0000854 if (rc == 0)
855 ++ptp->good_syncs;
856 else
857 ++ptp->no_time_syncs;
858 }
859
860 /* Increment the bad syncs counter if the synchronize fails, whatever
861 * the reason.
862 */
863 if (rc != 0)
864 ++ptp->bad_syncs;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100865
866 return rc;
867}
868
869/* Transmit a PTP packet, via the MCDI interface, to the wire. */
870static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
871{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100872 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100873 struct skb_shared_hwtstamps timestamps;
874 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100875 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100876 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100877
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100878 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000879 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100880 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100881 if (skb_shinfo(skb)->nr_frags != 0) {
882 rc = skb_linearize(skb);
883 if (rc != 0)
884 goto fail;
885 }
886
887 if (skb->ip_summed == CHECKSUM_PARTIAL) {
888 rc = skb_checksum_help(skb);
889 if (rc != 0)
890 goto fail;
891 }
892 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100893 MCDI_PTR(ptp_data->txbuf,
894 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100895 skb->len);
896 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
897 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
898 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100899 if (rc != 0)
900 goto fail;
901
902 memset(&timestamps, 0, sizeof(timestamps));
Laurence Evansa6f73462013-12-04 23:47:56 +0000903 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
904 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
905 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
906 ptp_data->ts_corrections.tx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100907
908 skb_tstamp_tx(skb, &timestamps);
909
910 rc = 0;
911
912fail:
913 dev_kfree_skb(skb);
914
915 return rc;
916}
917
918static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
919{
920 struct efx_ptp_data *ptp = efx->ptp_data;
921 struct list_head *cursor;
922 struct list_head *next;
923
Jon Cooperbd9a2652013-11-18 12:54:41 +0000924 if (ptp->rx_ts_inline)
925 return;
926
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100927 /* Drop time-expired events */
928 spin_lock_bh(&ptp->evt_lock);
929 if (!list_empty(&ptp->evt_list)) {
930 list_for_each_safe(cursor, next, &ptp->evt_list) {
931 struct efx_ptp_event_rx *evt;
932
933 evt = list_entry(cursor, struct efx_ptp_event_rx,
934 link);
935 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000936 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100937 netif_warn(efx, hw, efx->net_dev,
938 "PTP rx event dropped\n");
939 }
940 }
941 }
942 spin_unlock_bh(&ptp->evt_lock);
943}
944
945static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
946 struct sk_buff *skb)
947{
948 struct efx_ptp_data *ptp = efx->ptp_data;
949 bool evts_waiting;
950 struct list_head *cursor;
951 struct list_head *next;
952 struct efx_ptp_match *match;
953 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
954
Jon Cooperbd9a2652013-11-18 12:54:41 +0000955 WARN_ON_ONCE(ptp->rx_ts_inline);
956
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100957 spin_lock_bh(&ptp->evt_lock);
958 evts_waiting = !list_empty(&ptp->evt_list);
959 spin_unlock_bh(&ptp->evt_lock);
960
961 if (!evts_waiting)
962 return PTP_PACKET_STATE_UNMATCHED;
963
964 match = (struct efx_ptp_match *)skb->cb;
965 /* Look for a matching timestamp in the event queue */
966 spin_lock_bh(&ptp->evt_lock);
967 list_for_each_safe(cursor, next, &ptp->evt_list) {
968 struct efx_ptp_event_rx *evt;
969
970 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
971 if ((evt->seq0 == match->words[0]) &&
972 (evt->seq1 == match->words[1])) {
973 struct skb_shared_hwtstamps *timestamps;
974
975 /* Match - add in hardware timestamp */
976 timestamps = skb_hwtstamps(skb);
977 timestamps->hwtstamp = evt->hwtimestamp;
978
979 match->state = PTP_PACKET_STATE_MATCHED;
980 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000981 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100982 break;
983 }
984 }
985 spin_unlock_bh(&ptp->evt_lock);
986
987 return rc;
988}
989
990/* Process any queued receive events and corresponding packets
991 *
992 * q is returned with all the packets that are ready for delivery.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100993 */
Ben Hutchingsbbbe7142013-11-28 18:58:12 +0000994static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100995{
996 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100997 struct sk_buff *skb;
998
999 while ((skb = skb_dequeue(&ptp->rxq))) {
1000 struct efx_ptp_match *match;
1001
1002 match = (struct efx_ptp_match *)skb->cb;
1003 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1004 __skb_queue_tail(q, skb);
1005 } else if (efx_ptp_match_rx(efx, skb) ==
1006 PTP_PACKET_STATE_MATCHED) {
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001007 __skb_queue_tail(q, skb);
1008 } else if (time_after(jiffies, match->expiry)) {
1009 match->state = PTP_PACKET_STATE_TIMED_OUT;
Ben Hutchings99691c42013-12-11 02:36:08 +00001010 ++ptp->rx_no_timestamp;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001011 __skb_queue_tail(q, skb);
1012 } else {
1013 /* Replace unprocessed entry and stop */
1014 skb_queue_head(&ptp->rxq, skb);
1015 break;
1016 }
1017 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001018}
1019
1020/* Complete processing of a received packet */
1021static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1022{
1023 local_bh_disable();
1024 netif_receive_skb(skb);
1025 local_bh_enable();
1026}
1027
Ben Hutchings62a1c702013-10-15 17:54:56 +01001028static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1029{
1030 struct efx_ptp_data *ptp = efx->ptp_data;
1031
1032 if (ptp->rxfilter_installed) {
1033 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1034 ptp->rxfilter_general);
1035 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1036 ptp->rxfilter_event);
1037 ptp->rxfilter_installed = false;
1038 }
1039}
1040
1041static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001042{
1043 struct efx_ptp_data *ptp = efx->ptp_data;
1044 struct efx_filter_spec rxfilter;
1045 int rc;
1046
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001047 if (!ptp->channel || ptp->rxfilter_installed)
Ben Hutchings62a1c702013-10-15 17:54:56 +01001048 return 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001049
1050 /* Must filter on both event and general ports to ensure
1051 * that there is no packet re-ordering.
1052 */
1053 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1054 efx_rx_queue_index(
1055 efx_channel_get_rx_queue(ptp->channel)));
1056 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1057 htonl(PTP_ADDRESS),
1058 htons(PTP_EVENT_PORT));
1059 if (rc != 0)
1060 return rc;
1061
1062 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1063 if (rc < 0)
1064 return rc;
1065 ptp->rxfilter_event = rc;
1066
1067 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1068 efx_rx_queue_index(
1069 efx_channel_get_rx_queue(ptp->channel)));
1070 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1071 htonl(PTP_ADDRESS),
1072 htons(PTP_GENERAL_PORT));
1073 if (rc != 0)
1074 goto fail;
1075
1076 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1077 if (rc < 0)
1078 goto fail;
1079 ptp->rxfilter_general = rc;
1080
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001081 ptp->rxfilter_installed = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001082 return 0;
1083
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001084fail:
1085 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1086 ptp->rxfilter_event);
Ben Hutchings62a1c702013-10-15 17:54:56 +01001087 return rc;
1088}
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001089
Ben Hutchings62a1c702013-10-15 17:54:56 +01001090static int efx_ptp_start(struct efx_nic *efx)
1091{
1092 struct efx_ptp_data *ptp = efx->ptp_data;
1093 int rc;
1094
1095 ptp->reset_required = false;
1096
1097 rc = efx_ptp_insert_multicast_filters(efx);
1098 if (rc)
1099 return rc;
1100
1101 rc = efx_ptp_enable(efx);
1102 if (rc != 0)
1103 goto fail;
1104
1105 ptp->evt_frag_idx = 0;
1106 ptp->current_adjfreq = 0;
1107
1108 return 0;
1109
1110fail:
1111 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001112 return rc;
1113}
1114
1115static int efx_ptp_stop(struct efx_nic *efx)
1116{
1117 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001118 struct list_head *cursor;
1119 struct list_head *next;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001120 int rc;
1121
1122 if (ptp == NULL)
1123 return 0;
1124
1125 rc = efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001126
Ben Hutchings62a1c702013-10-15 17:54:56 +01001127 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001128
1129 /* Make sure RX packets are really delivered */
1130 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1131 skb_queue_purge(&efx->ptp_data->txq);
1132
1133 /* Drop any pending receive events */
1134 spin_lock_bh(&efx->ptp_data->evt_lock);
1135 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +00001136 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001137 }
1138 spin_unlock_bh(&efx->ptp_data->evt_lock);
1139
1140 return rc;
1141}
1142
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001143static int efx_ptp_restart(struct efx_nic *efx)
1144{
1145 if (efx->ptp_data && efx->ptp_data->enabled)
1146 return efx_ptp_start(efx);
1147 return 0;
1148}
1149
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001150static void efx_ptp_pps_worker(struct work_struct *work)
1151{
1152 struct efx_ptp_data *ptp =
1153 container_of(work, struct efx_ptp_data, pps_work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001154 struct efx_nic *efx = ptp->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001155 struct ptp_clock_event ptp_evt;
1156
1157 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1158 return;
1159
1160 ptp_evt.type = PTP_CLOCK_PPSUSR;
1161 ptp_evt.pps_times = ptp->host_time_pps;
1162 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1163}
1164
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001165static void efx_ptp_worker(struct work_struct *work)
1166{
1167 struct efx_ptp_data *ptp_data =
1168 container_of(work, struct efx_ptp_data, work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001169 struct efx_nic *efx = ptp_data->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001170 struct sk_buff *skb;
1171 struct sk_buff_head tempq;
1172
1173 if (ptp_data->reset_required) {
1174 efx_ptp_stop(efx);
1175 efx_ptp_start(efx);
1176 return;
1177 }
1178
1179 efx_ptp_drop_time_expired_events(efx);
1180
1181 __skb_queue_head_init(&tempq);
Ben Hutchingsbbbe7142013-11-28 18:58:12 +00001182 efx_ptp_process_events(efx, &tempq);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001183
Ben Hutchingsbbbe7142013-11-28 18:58:12 +00001184 while ((skb = skb_dequeue(&ptp_data->txq)))
1185 efx_ptp_xmit_skb(efx, skb);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001186
1187 while ((skb = __skb_dequeue(&tempq)))
1188 efx_ptp_process_rx(efx, skb);
1189}
1190
Ben Hutchings5d0dab02013-10-16 18:32:41 +01001191static const struct ptp_clock_info efx_phc_clock_info = {
1192 .owner = THIS_MODULE,
1193 .name = "sfc",
1194 .max_adj = MAX_PPB,
1195 .n_alarm = 0,
1196 .n_ext_ts = 0,
1197 .n_per_out = 0,
1198 .pps = 1,
1199 .adjfreq = efx_phc_adjfreq,
1200 .adjtime = efx_phc_adjtime,
1201 .gettime = efx_phc_gettime,
1202 .settime = efx_phc_settime,
1203 .enable = efx_phc_enable,
1204};
1205
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001206/* Initialise PTP state. */
1207int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001208{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001209 struct efx_ptp_data *ptp;
1210 int rc = 0;
1211 unsigned int pos;
1212
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001213 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1214 efx->ptp_data = ptp;
1215 if (!efx->ptp_data)
1216 return -ENOMEM;
1217
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001218 ptp->efx = efx;
1219 ptp->channel = channel;
Jon Cooperbd9a2652013-11-18 12:54:41 +00001220 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001221
Ben Hutchings0d19a542012-09-18 21:59:52 +01001222 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001223 if (rc != 0)
1224 goto fail1;
1225
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001226 skb_queue_head_init(&ptp->rxq);
1227 skb_queue_head_init(&ptp->txq);
1228 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1229 if (!ptp->workwq) {
1230 rc = -ENOMEM;
1231 goto fail2;
1232 }
1233
1234 INIT_WORK(&ptp->work, efx_ptp_worker);
1235 ptp->config.flags = 0;
1236 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1237 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1238 INIT_LIST_HEAD(&ptp->evt_list);
1239 INIT_LIST_HEAD(&ptp->evt_free_list);
1240 spin_lock_init(&ptp->evt_lock);
1241 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1242 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1243
Laurence Evansa6f73462013-12-04 23:47:56 +00001244 /* Get the NIC PTP attributes and set up time conversions */
1245 rc = efx_ptp_get_attributes(efx);
1246 if (rc < 0)
1247 goto fail3;
1248
1249 /* Get the timestamp corrections */
1250 rc = efx_ptp_get_timestamp_corrections(efx);
1251 if (rc < 0)
1252 goto fail3;
1253
Ben Hutchings9aecda92013-12-05 21:28:42 +00001254 if (efx->mcdi->fn_flags &
1255 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1256 ptp->phc_clock_info = efx_phc_clock_info;
1257 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1258 &efx->pci_dev->dev);
1259 if (IS_ERR(ptp->phc_clock)) {
1260 rc = PTR_ERR(ptp->phc_clock);
1261 goto fail3;
1262 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001263
Ben Hutchings9aecda92013-12-05 21:28:42 +00001264 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1265 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1266 if (!ptp->pps_workwq) {
1267 rc = -ENOMEM;
1268 goto fail4;
1269 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001270 }
1271 ptp->nic_ts_enabled = false;
1272
1273 return 0;
1274fail4:
1275 ptp_clock_unregister(efx->ptp_data->phc_clock);
1276
1277fail3:
1278 destroy_workqueue(efx->ptp_data->workwq);
1279
1280fail2:
1281 efx_nic_free_buffer(efx, &ptp->start);
1282
1283fail1:
1284 kfree(efx->ptp_data);
1285 efx->ptp_data = NULL;
1286
1287 return rc;
1288}
1289
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001290/* Initialise PTP channel.
1291 *
1292 * Setting core_index to zero causes the queue to be initialised and doesn't
1293 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1294 */
1295static int efx_ptp_probe_channel(struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001296{
1297 struct efx_nic *efx = channel->efx;
1298
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001299 channel->irq_moderation = 0;
1300 channel->rx_queue.core_index = 0;
1301
1302 return efx_ptp_probe(efx, channel);
1303}
1304
1305void efx_ptp_remove(struct efx_nic *efx)
1306{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001307 if (!efx->ptp_data)
1308 return;
1309
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001310 (void)efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001311
1312 cancel_work_sync(&efx->ptp_data->work);
1313 cancel_work_sync(&efx->ptp_data->pps_work);
1314
1315 skb_queue_purge(&efx->ptp_data->rxq);
1316 skb_queue_purge(&efx->ptp_data->txq);
1317
Ben Hutchings9aecda92013-12-05 21:28:42 +00001318 if (efx->ptp_data->phc_clock) {
1319 destroy_workqueue(efx->ptp_data->pps_workwq);
1320 ptp_clock_unregister(efx->ptp_data->phc_clock);
1321 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001322
1323 destroy_workqueue(efx->ptp_data->workwq);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001324
1325 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1326 kfree(efx->ptp_data);
1327}
1328
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001329static void efx_ptp_remove_channel(struct efx_channel *channel)
1330{
1331 efx_ptp_remove(channel->efx);
1332}
1333
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001334static void efx_ptp_get_channel_name(struct efx_channel *channel,
1335 char *buf, size_t len)
1336{
1337 snprintf(buf, len, "%s-ptp", channel->efx->name);
1338}
1339
1340/* Determine whether this packet should be processed by the PTP module
1341 * or transmitted conventionally.
1342 */
1343bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1344{
1345 return efx->ptp_data &&
1346 efx->ptp_data->enabled &&
1347 skb->len >= PTP_MIN_LENGTH &&
1348 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1349 likely(skb->protocol == htons(ETH_P_IP)) &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001350 skb_transport_header_was_set(skb) &&
1351 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001352 ip_hdr(skb)->protocol == IPPROTO_UDP &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001353 skb_headlen(skb) >=
1354 skb_transport_offset(skb) + sizeof(struct udphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001355 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1356}
1357
1358/* Receive a PTP packet. Packets are queued until the arrival of
1359 * the receive timestamp from the MC - this will probably occur after the
1360 * packet arrival because of the processing in the MC.
1361 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001362static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001363{
1364 struct efx_nic *efx = channel->efx;
1365 struct efx_ptp_data *ptp = efx->ptp_data;
1366 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001367 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001368 unsigned int version;
1369
1370 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1371
1372 /* Correct version? */
1373 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001374 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001375 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001376 }
1377 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1378 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001379 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001380 }
Laurence Evansc939a312012-11-15 10:56:07 +00001381
1382 /* PTP V1 uses all six bytes of the UUID to match the packet
1383 * to the timestamp
1384 */
1385 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1386 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001387 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001388 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001389 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001390 }
1391 version = skb->data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001392 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001393 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001394 }
Laurence Evansc939a312012-11-15 10:56:07 +00001395
1396 /* The original V2 implementation uses bytes 2-7 of
1397 * the UUID to match the packet to the timestamp. This
1398 * discards two of the bytes of the MAC address used
1399 * to create the UUID (SF bug 33070). The PTP V2
1400 * enhanced mode fixes this issue and uses bytes 0-2
1401 * and byte 5-7 of the UUID.
1402 */
1403 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1404 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1405 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1406 } else {
1407 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1408 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1409 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001410 }
1411
1412 /* Does this packet require timestamping? */
1413 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001414 match->state = PTP_PACKET_STATE_UNMATCHED;
1415
Laurence Evansc939a312012-11-15 10:56:07 +00001416 /* We expect the sequence number to be in the same position in
1417 * the packet for PTP V1 and V2
1418 */
1419 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1420 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1421
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001422 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001423 match->words[0] = (match_data_012[0] |
1424 (match_data_012[1] << 8) |
1425 (match_data_012[2] << 16) |
1426 (match_data_345[0] << 24));
1427 match->words[1] = (match_data_345[1] |
1428 (match_data_345[2] << 8) |
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001429 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1430 PTP_V1_SEQUENCE_LENGTH - 1] <<
1431 16));
1432 } else {
1433 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1434 }
1435
1436 skb_queue_tail(&ptp->rxq, skb);
1437 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001438
1439 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001440}
1441
1442/* Transmit a PTP packet. This has to be transmitted by the MC
1443 * itself, through an MCDI call. MCDI calls aren't permitted
1444 * in the transmit path so defer the actual transmission to a suitable worker.
1445 */
1446int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1447{
1448 struct efx_ptp_data *ptp = efx->ptp_data;
1449
1450 skb_queue_tail(&ptp->txq, skb);
1451
1452 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1453 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1454 efx_xmit_hwtstamp_pending(skb);
1455 queue_work(ptp->workwq, &ptp->work);
1456
1457 return NETDEV_TX_OK;
1458}
1459
Daniel Pieczko9ec06592013-11-21 17:11:25 +00001460int efx_ptp_get_mode(struct efx_nic *efx)
1461{
1462 return efx->ptp_data->mode;
1463}
1464
1465int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1466 unsigned int new_mode)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001467{
1468 if ((enable_wanted != efx->ptp_data->enabled) ||
1469 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001470 int rc = 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001471
1472 if (enable_wanted) {
1473 /* Change of mode requires disable */
1474 if (efx->ptp_data->enabled &&
1475 (efx->ptp_data->mode != new_mode)) {
1476 efx->ptp_data->enabled = false;
1477 rc = efx_ptp_stop(efx);
1478 if (rc != 0)
1479 return rc;
1480 }
1481
1482 /* Set new operating mode and establish
1483 * baseline synchronisation, which must
1484 * succeed.
1485 */
1486 efx->ptp_data->mode = new_mode;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001487 if (netif_running(efx->net_dev))
1488 rc = efx_ptp_start(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001489 if (rc == 0) {
1490 rc = efx_ptp_synchronize(efx,
1491 PTP_SYNC_ATTEMPTS * 2);
1492 if (rc != 0)
1493 efx_ptp_stop(efx);
1494 }
1495 } else {
1496 rc = efx_ptp_stop(efx);
1497 }
1498
1499 if (rc != 0)
1500 return rc;
1501
1502 efx->ptp_data->enabled = enable_wanted;
1503 }
1504
1505 return 0;
1506}
1507
1508static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1509{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001510 int rc;
1511
1512 if (init->flags)
1513 return -EINVAL;
1514
1515 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1516 (init->tx_type != HWTSTAMP_TX_ON))
1517 return -ERANGE;
1518
Daniel Pieczko9ec06592013-11-21 17:11:25 +00001519 rc = efx->type->ptp_set_ts_config(efx, init);
1520 if (rc)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001521 return rc;
1522
1523 efx->ptp_data->config = *init;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001524 return 0;
1525}
1526
Ben Hutchings62ebac92013-04-08 17:34:58 +01001527void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001528{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001529 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings9aecda92013-12-05 21:28:42 +00001530 struct efx_nic *primary = efx->primary;
1531
1532 ASSERT_RTNL();
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001533
1534 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001535 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001536
Ben Hutchings62ebac92013-04-08 17:34:58 +01001537 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1538 SOF_TIMESTAMPING_RX_HARDWARE |
1539 SOF_TIMESTAMPING_RAW_HARDWARE);
Ben Hutchings9aecda92013-12-05 21:28:42 +00001540 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1541 ts_info->phc_index =
1542 ptp_clock_index(primary->ptp_data->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001543 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
Daniel Pieczko9ec06592013-11-21 17:11:25 +00001544 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001545}
1546
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001547int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001548{
1549 struct hwtstamp_config config;
1550 int rc;
1551
1552 /* Not a PTP enabled port */
1553 if (!efx->ptp_data)
1554 return -EOPNOTSUPP;
1555
1556 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1557 return -EFAULT;
1558
1559 rc = efx_ptp_ts_init(efx, &config);
1560 if (rc != 0)
1561 return rc;
1562
1563 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1564 ? -EFAULT : 0;
1565}
1566
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001567int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1568{
1569 if (!efx->ptp_data)
1570 return -EOPNOTSUPP;
1571
1572 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1573 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1574}
1575
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001576static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1577{
1578 struct efx_ptp_data *ptp = efx->ptp_data;
1579
1580 netif_err(efx, hw, efx->net_dev,
1581 "PTP unexpected event length: got %d expected %d\n",
1582 ptp->evt_frag_idx, expected_frag_len);
1583 ptp->reset_required = true;
1584 queue_work(ptp->workwq, &ptp->work);
1585}
1586
1587/* Process a completed receive event. Put it on the event queue and
1588 * start worker thread. This is required because event and their
1589 * correspoding packets may come in either order.
1590 */
1591static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1592{
1593 struct efx_ptp_event_rx *evt = NULL;
1594
Jon Cooperbd9a2652013-11-18 12:54:41 +00001595 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1596 return;
1597
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001598 if (ptp->evt_frag_idx != 3) {
1599 ptp_event_failure(efx, 3);
1600 return;
1601 }
1602
1603 spin_lock_bh(&ptp->evt_lock);
1604 if (!list_empty(&ptp->evt_free_list)) {
1605 evt = list_first_entry(&ptp->evt_free_list,
1606 struct efx_ptp_event_rx, link);
1607 list_del(&evt->link);
1608
1609 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1610 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1611 MCDI_EVENT_SRC) |
1612 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1613 MCDI_EVENT_SRC) << 8) |
1614 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1615 MCDI_EVENT_SRC) << 16));
Laurence Evansa6f73462013-12-04 23:47:56 +00001616 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001617 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
Laurence Evansa6f73462013-12-04 23:47:56 +00001618 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1619 ptp->ts_corrections.rx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001620 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1621 list_add_tail(&evt->link, &ptp->evt_list);
1622
1623 queue_work(ptp->workwq, &ptp->work);
Laurence Evansf9fd7ec72014-02-12 18:58:24 +00001624 } else if (net_ratelimit()) {
1625 /* Log a rate-limited warning message. */
Laurence Evansf3211602013-01-28 14:51:17 +00001626 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001627 }
1628 spin_unlock_bh(&ptp->evt_lock);
1629}
1630
1631static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1632{
1633 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1634 if (ptp->evt_frag_idx != 1) {
1635 ptp_event_failure(efx, 1);
1636 return;
1637 }
1638
1639 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1640}
1641
1642static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1643{
1644 if (ptp->nic_ts_enabled)
1645 queue_work(ptp->pps_workwq, &ptp->pps_work);
1646}
1647
1648void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1649{
1650 struct efx_ptp_data *ptp = efx->ptp_data;
1651 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1652
1653 if (!ptp->enabled)
1654 return;
1655
1656 if (ptp->evt_frag_idx == 0) {
1657 ptp->evt_code = code;
1658 } else if (ptp->evt_code != code) {
1659 netif_err(efx, hw, efx->net_dev,
1660 "PTP out of sequence event %d\n", code);
1661 ptp->evt_frag_idx = 0;
1662 }
1663
1664 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1665 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1666 /* Process resulting event */
1667 switch (code) {
1668 case MCDI_EVENT_CODE_PTP_RX:
1669 ptp_event_rx(efx, ptp);
1670 break;
1671 case MCDI_EVENT_CODE_PTP_FAULT:
1672 ptp_event_fault(efx, ptp);
1673 break;
1674 case MCDI_EVENT_CODE_PTP_PPS:
1675 ptp_event_pps(efx, ptp);
1676 break;
1677 default:
1678 netif_err(efx, hw, efx->net_dev,
1679 "PTP unknown event %d\n", code);
1680 break;
1681 }
1682 ptp->evt_frag_idx = 0;
1683 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1684 netif_err(efx, hw, efx->net_dev,
1685 "PTP too many event fragments\n");
1686 ptp->evt_frag_idx = 0;
1687 }
1688}
1689
Jon Cooperbd9a2652013-11-18 12:54:41 +00001690void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1691{
1692 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1693 channel->sync_timestamp_minor =
1694 MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_26_19) << 19;
1695 /* if sync events have been disabled then we want to silently ignore
1696 * this event, so throw away result.
1697 */
1698 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1699 SYNC_EVENTS_VALID);
1700}
1701
1702/* make some assumptions about the time representation rather than abstract it,
1703 * since we currently only support one type of inline timestamping and only on
1704 * EF10.
1705 */
1706#define MINOR_TICKS_PER_SECOND 0x8000000
1707/* Fuzz factor for sync events to be out of order with RX events */
1708#define FUZZ (MINOR_TICKS_PER_SECOND / 10)
1709#define EXPECTED_SYNC_EVENTS_PER_SECOND 4
1710
1711static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1712{
1713#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1714 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1715#else
1716 const u8 *data = eh + efx->rx_packet_ts_offset;
1717 return (u32)data[0] |
1718 (u32)data[1] << 8 |
1719 (u32)data[2] << 16 |
1720 (u32)data[3] << 24;
1721#endif
1722}
1723
1724void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
1725 struct sk_buff *skb)
1726{
1727 struct efx_nic *efx = channel->efx;
1728 u32 pkt_timestamp_major, pkt_timestamp_minor;
1729 u32 diff, carry;
1730 struct skb_shared_hwtstamps *timestamps;
1731
1732 pkt_timestamp_minor = (efx_rx_buf_timestamp_minor(efx,
1733 skb_mac_header(skb)) +
1734 (u32) efx->ptp_data->ts_corrections.rx) &
1735 (MINOR_TICKS_PER_SECOND - 1);
1736
1737 /* get the difference between the packet and sync timestamps,
1738 * modulo one second
1739 */
1740 diff = (pkt_timestamp_minor - channel->sync_timestamp_minor) &
1741 (MINOR_TICKS_PER_SECOND - 1);
1742 /* do we roll over a second boundary and need to carry the one? */
1743 carry = channel->sync_timestamp_minor + diff > MINOR_TICKS_PER_SECOND ?
1744 1 : 0;
1745
1746 if (diff <= MINOR_TICKS_PER_SECOND / EXPECTED_SYNC_EVENTS_PER_SECOND +
1747 FUZZ) {
1748 /* packet is ahead of the sync event by a quarter of a second or
1749 * less (allowing for fuzz)
1750 */
1751 pkt_timestamp_major = channel->sync_timestamp_major + carry;
1752 } else if (diff >= MINOR_TICKS_PER_SECOND - FUZZ) {
1753 /* packet is behind the sync event but within the fuzz factor.
1754 * This means the RX packet and sync event crossed as they were
1755 * placed on the event queue, which can sometimes happen.
1756 */
1757 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
1758 } else {
1759 /* it's outside tolerance in both directions. this might be
1760 * indicative of us missing sync events for some reason, so
1761 * we'll call it an error rather than risk giving a bogus
1762 * timestamp.
1763 */
1764 netif_vdbg(efx, drv, efx->net_dev,
1765 "packet timestamp %x too far from sync event %x:%x\n",
1766 pkt_timestamp_minor, channel->sync_timestamp_major,
1767 channel->sync_timestamp_minor);
1768 return;
1769 }
1770
1771 /* attach the timestamps to the skb */
1772 timestamps = skb_hwtstamps(skb);
1773 timestamps->hwtstamp =
1774 efx_ptp_s27_to_ktime(pkt_timestamp_major, pkt_timestamp_minor);
1775}
1776
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001777static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1778{
1779 struct efx_ptp_data *ptp_data = container_of(ptp,
1780 struct efx_ptp_data,
1781 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001782 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001783 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001784 s64 adjustment_ns;
1785 int rc;
1786
1787 if (delta > MAX_PPB)
1788 delta = MAX_PPB;
1789 else if (delta < -MAX_PPB)
1790 delta = -MAX_PPB;
1791
1792 /* Convert ppb to fixed point ns. */
1793 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1794 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1795
1796 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001797 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001798 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001799 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1800 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1801 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1802 NULL, 0, NULL);
1803 if (rc != 0)
1804 return rc;
1805
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001806 ptp_data->current_adjfreq = adjustment_ns;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001807 return 0;
1808}
1809
1810static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1811{
Laurence Evansa6f73462013-12-04 23:47:56 +00001812 u32 nic_major, nic_minor;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001813 struct efx_ptp_data *ptp_data = container_of(ptp,
1814 struct efx_ptp_data,
1815 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001816 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001817 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001818
Laurence Evansa6f73462013-12-04 23:47:56 +00001819 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
1820
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001821 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001822 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001823 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
Laurence Evansa6f73462013-12-04 23:47:56 +00001824 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
1825 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001826 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1827 NULL, 0, NULL);
1828}
1829
1830static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1831{
1832 struct efx_ptp_data *ptp_data = container_of(ptp,
1833 struct efx_ptp_data,
1834 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001835 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001836 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1837 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001838 int rc;
Laurence Evansa6f73462013-12-04 23:47:56 +00001839 ktime_t kt;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001840
1841 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001842 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001843
1844 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1845 outbuf, sizeof(outbuf), NULL);
1846 if (rc != 0)
1847 return rc;
1848
Laurence Evansa6f73462013-12-04 23:47:56 +00001849 kt = ptp_data->nic_to_kernel_time(
1850 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
1851 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
1852 *ts = ktime_to_timespec(kt);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001853 return 0;
1854}
1855
1856static int efx_phc_settime(struct ptp_clock_info *ptp,
1857 const struct timespec *e_ts)
1858{
1859 /* Get the current NIC time, efx_phc_gettime.
1860 * Subtract from the desired time to get the offset
1861 * call efx_phc_adjtime with the offset
1862 */
1863 int rc;
1864 struct timespec time_now;
1865 struct timespec delta;
1866
1867 rc = efx_phc_gettime(ptp, &time_now);
1868 if (rc != 0)
1869 return rc;
1870
1871 delta = timespec_sub(*e_ts, time_now);
1872
Julia Lawall56567c62013-01-21 03:02:48 +00001873 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001874 if (rc != 0)
1875 return rc;
1876
1877 return 0;
1878}
1879
1880static int efx_phc_enable(struct ptp_clock_info *ptp,
1881 struct ptp_clock_request *request,
1882 int enable)
1883{
1884 struct efx_ptp_data *ptp_data = container_of(ptp,
1885 struct efx_ptp_data,
1886 phc_clock_info);
1887 if (request->type != PTP_CLK_REQ_PPS)
1888 return -EOPNOTSUPP;
1889
1890 ptp_data->nic_ts_enabled = !!enable;
1891 return 0;
1892}
1893
1894static const struct efx_channel_type efx_ptp_channel_type = {
1895 .handle_no_channel = efx_ptp_handle_no_channel,
1896 .pre_probe = efx_ptp_probe_channel,
1897 .post_remove = efx_ptp_remove_channel,
1898 .get_name = efx_ptp_get_channel_name,
1899 /* no copy operation; there is no need to reallocate this channel */
1900 .receive_skb = efx_ptp_rx,
1901 .keep_eventq = false,
1902};
1903
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001904void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001905{
1906 /* Check whether PTP is implemented on this NIC. The DISABLE
1907 * operation will succeed if and only if it is implemented.
1908 */
1909 if (efx_ptp_disable(efx) == 0)
1910 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1911 &efx_ptp_channel_type;
1912}
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001913
1914void efx_ptp_start_datapath(struct efx_nic *efx)
1915{
1916 if (efx_ptp_restart(efx))
1917 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
Jon Cooperbd9a2652013-11-18 12:54:41 +00001918 /* re-enable timestamping if it was previously enabled */
1919 if (efx->type->ptp_set_ts_sync_events)
1920 efx->type->ptp_set_ts_sync_events(efx, true, true);
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001921}
1922
1923void efx_ptp_stop_datapath(struct efx_nic *efx)
1924{
Jon Cooperbd9a2652013-11-18 12:54:41 +00001925 /* temporarily disable timestamping */
1926 if (efx->type->ptp_set_ts_sync_events)
1927 efx->type->ptp_set_ts_sync_events(efx, false, true);
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001928 efx_ptp_stop(efx);
1929}