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