blob: cf2ae11b13f167fc6ff318c196ad84763961f900 [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)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100219 * @rxq: Receive queue (awaiting timestamps)
220 * @txq: Transmit queue
221 * @evt_list: List of MC receive events awaiting packets
222 * @evt_free_list: List of free events
223 * @evt_lock: Lock for manipulating evt_list and evt_free_list
Laurence Evansf3211602013-01-28 14:51:17 +0000224 * @evt_overflow: Boolean indicating that event list has overflowed
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100225 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
226 * @workwq: Work queue for processing pending PTP operations
227 * @work: Work task
228 * @reset_required: A serious error has occurred and the PTP task needs to be
229 * reset (disable, enable).
230 * @rxfilter_event: Receive filter when operating
231 * @rxfilter_general: Receive filter when operating
232 * @config: Current timestamp configuration
233 * @enabled: PTP operation enabled
234 * @mode: Mode in which PTP operating (PTP version)
Laurence Evansa6f73462013-12-04 23:47:56 +0000235 * @time_format: Time format supported by this NIC
236 * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
237 * @nic_to_kernel_time: Function to convert from NIC to kernel time
238 * @min_synchronisation_ns: Minimum acceptable corrected sync window
239 * @ts_corrections.tx: Required driver correction of transmit timestamps
240 * @ts_corrections.rx: Required driver correction of receive timestamps
241 * @ts_corrections.pps_out: PPS output error (information only)
242 * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100243 * @evt_frags: Partly assembled PTP events
244 * @evt_frag_idx: Current fragment number
245 * @evt_code: Last event code
246 * @start: Address at which MC indicates ready for synchronisation
247 * @host_time_pps: Host time at last PPS
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100248 * @current_adjfreq: Current ppb adjustment.
249 * @phc_clock: Pointer to registered phc device
250 * @phc_clock_info: Registration structure for phc device
251 * @pps_work: pps work task for handling pps events
252 * @pps_workwq: pps work queue
253 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
254 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
255 * allocations in main data path).
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100256 * @timeset: Last set of synchronisation statistics.
257 */
258struct efx_ptp_data {
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100259 struct efx_nic *efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100260 struct efx_channel *channel;
261 struct sk_buff_head rxq;
262 struct sk_buff_head txq;
263 struct list_head evt_list;
264 struct list_head evt_free_list;
265 spinlock_t evt_lock;
Laurence Evansf3211602013-01-28 14:51:17 +0000266 bool evt_overflow;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100267 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
268 struct workqueue_struct *workwq;
269 struct work_struct work;
270 bool reset_required;
271 u32 rxfilter_event;
272 u32 rxfilter_general;
273 bool rxfilter_installed;
274 struct hwtstamp_config config;
275 bool enabled;
276 unsigned int mode;
Laurence Evansa6f73462013-12-04 23:47:56 +0000277 unsigned int time_format;
278 void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
279 ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
280 s32 correction);
281 unsigned int min_synchronisation_ns;
282 struct {
283 s32 tx;
284 s32 rx;
285 s32 pps_out;
286 s32 pps_in;
287 } ts_corrections;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100288 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
289 int evt_frag_idx;
290 int evt_code;
291 struct efx_buffer start;
292 struct pps_event_time host_time_pps;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100293 s64 current_adjfreq;
294 struct ptp_clock *phc_clock;
295 struct ptp_clock_info phc_clock_info;
296 struct work_struct pps_work;
297 struct workqueue_struct *pps_workwq;
298 bool nic_ts_enabled;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100299 MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100300 struct efx_ptp_timeset
301 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
302};
303
304static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
305static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
306static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts);
307static int efx_phc_settime(struct ptp_clock_info *ptp,
308 const struct timespec *e_ts);
309static int efx_phc_enable(struct ptp_clock_info *ptp,
310 struct ptp_clock_request *request, int on);
311
Laurence Evansa6f73462013-12-04 23:47:56 +0000312/* For Siena platforms NIC time is s and ns */
313static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
314{
315 struct timespec ts = ns_to_timespec(ns);
316 *nic_major = ts.tv_sec;
317 *nic_minor = ts.tv_nsec;
318}
319
320static ktime_t efx_ptp_s_ns_to_ktime(u32 nic_major, u32 nic_minor,
321 s32 correction)
322{
323 ktime_t kt = ktime_set(nic_major, nic_minor);
324 if (correction >= 0)
325 kt = ktime_add_ns(kt, (u64)correction);
326 else
327 kt = ktime_sub_ns(kt, (u64)-correction);
328 return kt;
329}
330
331/* To convert from s27 format to ns we multiply then divide by a power of 2.
332 * For the conversion from ns to s27, the operation is also converted to a
333 * multiply and shift.
334 */
335#define S27_TO_NS_SHIFT (27)
336#define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
337#define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
338#define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
339
340/* For Huntington platforms NIC time is in seconds and fractions of a second
341 * where the minor register only uses 27 bits in units of 2^-27s.
342 */
343static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
344{
345 struct timespec ts = ns_to_timespec(ns);
346 u32 maj = ts.tv_sec;
347 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
348 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
349
350 /* The conversion can result in the minor value exceeding the maximum.
351 * In this case, round up to the next second.
352 */
353 if (min >= S27_MINOR_MAX) {
354 min -= S27_MINOR_MAX;
355 maj++;
356 }
357
358 *nic_major = maj;
359 *nic_minor = min;
360}
361
362static ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor,
363 s32 correction)
364{
365 u32 ns;
366
367 /* Apply the correction and deal with carry */
368 nic_minor += correction;
369 if ((s32)nic_minor < 0) {
370 nic_minor += S27_MINOR_MAX;
371 nic_major--;
372 } else if (nic_minor >= S27_MINOR_MAX) {
373 nic_minor -= S27_MINOR_MAX;
374 nic_major++;
375 }
376
377 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
378 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
379
380 return ktime_set(nic_major, ns);
381}
382
383/* Get PTP attributes and set up time conversions */
384static int efx_ptp_get_attributes(struct efx_nic *efx)
385{
386 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
387 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
388 struct efx_ptp_data *ptp = efx->ptp_data;
389 int rc;
390 u32 fmt;
391 size_t out_len;
392
393 /* Get the PTP attributes. If the NIC doesn't support the operation we
394 * use the default format for compatibility with older NICs i.e.
395 * seconds and nanoseconds.
396 */
397 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
398 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
399 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
400 outbuf, sizeof(outbuf), &out_len);
401 if (rc == 0)
402 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
403 else if (rc == -EINVAL)
404 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
405 else
406 return rc;
407
408 if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION) {
409 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
410 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime;
411 } else if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS) {
412 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
413 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime;
414 } else {
415 return -ERANGE;
416 }
417
418 ptp->time_format = fmt;
419
420 /* MC_CMD_PTP_OP_GET_ATTRIBUTES is an extended version of an older
421 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT that also returns a value
422 * to use for the minimum acceptable corrected synchronization window.
423 * If we have the extra information store it. For older firmware that
424 * does not implement the extended command use the default value.
425 */
426 if (rc == 0 && out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
427 ptp->min_synchronisation_ns =
428 MCDI_DWORD(outbuf,
429 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
430 else
431 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
432
433 return 0;
434}
435
436/* Get PTP timestamp corrections */
437static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
438{
439 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
440 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_LEN);
441 int rc;
442
443 /* Get the timestamp corrections from the NIC. If this operation is
444 * not supported (older NICs) then no correction is required.
445 */
446 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
447 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
448 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
449
450 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
451 outbuf, sizeof(outbuf), NULL);
452 if (rc == 0) {
453 efx->ptp_data->ts_corrections.tx = MCDI_DWORD(outbuf,
454 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
455 efx->ptp_data->ts_corrections.rx = MCDI_DWORD(outbuf,
456 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
457 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
458 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
459 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
460 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
461 } else if (rc == -EINVAL) {
462 efx->ptp_data->ts_corrections.tx = 0;
463 efx->ptp_data->ts_corrections.rx = 0;
464 efx->ptp_data->ts_corrections.pps_out = 0;
465 efx->ptp_data->ts_corrections.pps_in = 0;
466 } else {
467 return rc;
468 }
469
470 return 0;
471}
472
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100473/* Enable MCDI PTP support. */
474static int efx_ptp_enable(struct efx_nic *efx)
475{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100476 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100477 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
478 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100479
480 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000481 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100482 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100483 efx->ptp_data->channel ?
484 efx->ptp_data->channel->channel : 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100485 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
486
Edward Cree1e0b8122013-05-31 18:36:12 +0100487 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
488 outbuf, sizeof(outbuf), NULL);
489 rc = (rc == -EALREADY) ? 0 : rc;
490 if (rc)
491 efx_mcdi_display_error(efx, MC_CMD_PTP,
492 MC_CMD_PTP_IN_ENABLE_LEN,
493 outbuf, sizeof(outbuf), rc);
494 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100495}
496
497/* Disable MCDI PTP support.
498 *
499 * Note that this function should never rely on the presence of ptp_data -
500 * may be called before that exists.
501 */
502static int efx_ptp_disable(struct efx_nic *efx)
503{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100504 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100505 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
506 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100507
508 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000509 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Edward Cree1e0b8122013-05-31 18:36:12 +0100510 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
511 outbuf, sizeof(outbuf), NULL);
512 rc = (rc == -EALREADY) ? 0 : rc;
513 if (rc)
514 efx_mcdi_display_error(efx, MC_CMD_PTP,
515 MC_CMD_PTP_IN_DISABLE_LEN,
516 outbuf, sizeof(outbuf), rc);
517 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100518}
519
520static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
521{
522 struct sk_buff *skb;
523
524 while ((skb = skb_dequeue(q))) {
525 local_bh_disable();
526 netif_receive_skb(skb);
527 local_bh_enable();
528 }
529}
530
531static void efx_ptp_handle_no_channel(struct efx_nic *efx)
532{
533 netif_err(efx, drv, efx->net_dev,
534 "ERROR: PTP requires MSI-X and 1 additional interrupt"
535 "vector. PTP disabled\n");
536}
537
538/* Repeatedly send the host time to the MC which will capture the hardware
539 * time.
540 */
541static void efx_ptp_send_times(struct efx_nic *efx,
542 struct pps_event_time *last_time)
543{
544 struct pps_event_time now;
545 struct timespec limit;
546 struct efx_ptp_data *ptp = efx->ptp_data;
547 struct timespec start;
548 int *mc_running = ptp->start.addr;
549
550 pps_get_ts(&now);
551 start = now.ts_real;
552 limit = now.ts_real;
553 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
554
555 /* Write host time for specified period or until MC is done */
556 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
557 ACCESS_ONCE(*mc_running)) {
558 struct timespec update_time;
559 unsigned int host_time;
560
561 /* Don't update continuously to avoid saturating the PCIe bus */
562 update_time = now.ts_real;
563 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
564 do {
565 pps_get_ts(&now);
566 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
567 ACCESS_ONCE(*mc_running));
568
569 /* Synchronise NIC with single word of time only */
570 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
571 now.ts_real.tv_nsec);
572 /* Update host time in NIC memory */
Laurence Evans977a5d52013-03-07 11:46:58 +0000573 efx->type->ptp_write_host_time(efx, host_time);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100574 }
575 *last_time = now;
576}
577
578/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100579static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
580 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100581{
582 unsigned start_ns, end_ns;
583
584 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
Laurence Evansa6f73462013-12-04 23:47:56 +0000585 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
586 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100587 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
Laurence Evansa6f73462013-12-04 23:47:56 +0000588 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100589
590 /* Ignore seconds */
591 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
592 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
593 /* Allow for rollover */
594 if (end_ns < start_ns)
595 end_ns += NSEC_PER_SEC;
596 /* Determine duration of operation */
597 timeset->window = end_ns - start_ns;
598}
599
600/* Process times received from MC.
601 *
602 * Extract times from returned results, and establish the minimum value
603 * seen. The minimum value represents the "best" possible time and events
604 * too much greater than this are rejected - the machine is, perhaps, too
605 * busy. A number of readings are taken so that, hopefully, at least one good
606 * synchronisation will be seen in the results.
607 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100608static int
609efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
610 size_t response_length,
611 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100612{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100613 unsigned number_readings =
614 MCDI_VAR_ARRAY_LEN(response_length,
615 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100616 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100617 unsigned ngood = 0;
618 unsigned last_good = 0;
619 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100620 u32 last_sec;
621 u32 start_sec;
622 struct timespec delta;
Laurence Evansa6f73462013-12-04 23:47:56 +0000623 ktime_t mc_time;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100624
625 if (number_readings == 0)
626 return -EAGAIN;
627
Laurence Evansdfd8d582013-11-21 10:38:24 +0000628 /* Read the set of results and find the last good host-MC
629 * synchronization result. The MC times when it finishes reading the
630 * host time so the corrected window time should be fairly constant
631 * for a given platform.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100632 */
633 for (i = 0; i < number_readings; i++) {
Laurence Evansdfd8d582013-11-21 10:38:24 +0000634 s32 window, corrected;
Laurence Evansa6f73462013-12-04 23:47:56 +0000635 struct timespec wait;
Laurence Evansdfd8d582013-11-21 10:38:24 +0000636
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100637 efx_ptp_read_timeset(
638 MCDI_ARRAY_STRUCT_PTR(synch_buf,
639 PTP_OUT_SYNCHRONIZE_TIMESET, i),
640 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100641
Laurence Evansa6f73462013-12-04 23:47:56 +0000642 wait = ktime_to_timespec(
643 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
Laurence Evansdfd8d582013-11-21 10:38:24 +0000644 window = ptp->timeset[i].window;
Laurence Evansa6f73462013-12-04 23:47:56 +0000645 corrected = window - wait.tv_nsec;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100646
Laurence Evansdfd8d582013-11-21 10:38:24 +0000647 /* We expect the uncorrected synchronization window to be at
648 * least as large as the interval between host start and end
649 * times. If it is smaller than this then this is mostly likely
650 * to be a consequence of the host's time being adjusted.
651 * Check that the corrected sync window is in a reasonable
652 * range. If it is out of range it is likely to be because an
653 * interrupt or other delay occurred between reading the system
654 * time and writing it to MC memory.
655 */
656 if (window >= SYNCHRONISATION_GRANULARITY_NS &&
657 corrected < MAX_SYNCHRONISATION_NS &&
Laurence Evansa6f73462013-12-04 23:47:56 +0000658 corrected >= ptp->min_synchronisation_ns) {
Laurence Evansdfd8d582013-11-21 10:38:24 +0000659 ngood++;
660 last_good = i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100661 }
Laurence Evansdfd8d582013-11-21 10:38:24 +0000662 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100663
664 if (ngood == 0) {
665 netif_warn(efx, drv, efx->net_dev,
Laurence Evans94cd60d2013-11-21 10:38:24 +0000666 "PTP no suitable synchronisations\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100667 return -EAGAIN;
668 }
669
Laurence Evansa6f73462013-12-04 23:47:56 +0000670 /* Convert the NIC time into kernel time. No correction is required-
671 * this time is the output of a firmware process.
672 */
673 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
674 ptp->timeset[last_good].minor, 0);
675
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100676 /* Calculate delay from actual PPS to last_time */
Laurence Evansa6f73462013-12-04 23:47:56 +0000677 delta = ktime_to_timespec(mc_time);
678 delta.tv_nsec +=
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100679 last_time->ts_real.tv_nsec -
680 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
681
682 /* It is possible that the seconds rolled over between taking
683 * the start reading and the last value written by the host. The
684 * timescales are such that a gap of more than one second is never
685 * expected.
686 */
687 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
688 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
689 if (start_sec != last_sec) {
690 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
691 netif_warn(efx, hw, efx->net_dev,
692 "PTP bad synchronisation seconds\n");
693 return -EAGAIN;
694 } else {
695 delta.tv_sec = 1;
696 }
697 } else {
698 delta.tv_sec = 0;
699 }
700
701 ptp->host_time_pps = *last_time;
702 pps_sub_ts(&ptp->host_time_pps, delta);
703
704 return 0;
705}
706
707/* Synchronize times between the host and the MC */
708static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
709{
710 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100711 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100712 size_t response_length;
713 int rc;
714 unsigned long timeout;
715 struct pps_event_time last_time = {};
716 unsigned int loops = 0;
717 int *start = ptp->start.addr;
718
719 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000720 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100721 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
722 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100723 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
724 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100725
726 /* Clear flag that signals MC ready */
727 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100728 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
729 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
730 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100731
732 /* Wait for start from MCDI (or timeout) */
733 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
734 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
735 udelay(20); /* Usually start MCDI execution quickly */
736 loops++;
737 }
738
739 if (ACCESS_ONCE(*start))
740 efx_ptp_send_times(efx, &last_time);
741
742 /* Collect results */
743 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
744 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
745 synch_buf, sizeof(synch_buf),
746 &response_length);
747 if (rc == 0)
748 rc = efx_ptp_process_times(efx, synch_buf, response_length,
749 &last_time);
750
751 return rc;
752}
753
754/* Transmit a PTP packet, via the MCDI interface, to the wire. */
755static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
756{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100757 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100758 struct skb_shared_hwtstamps timestamps;
759 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100760 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100761 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100762
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100763 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000764 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100765 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100766 if (skb_shinfo(skb)->nr_frags != 0) {
767 rc = skb_linearize(skb);
768 if (rc != 0)
769 goto fail;
770 }
771
772 if (skb->ip_summed == CHECKSUM_PARTIAL) {
773 rc = skb_checksum_help(skb);
774 if (rc != 0)
775 goto fail;
776 }
777 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100778 MCDI_PTR(ptp_data->txbuf,
779 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100780 skb->len);
781 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
782 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
783 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100784 if (rc != 0)
785 goto fail;
786
787 memset(&timestamps, 0, sizeof(timestamps));
Laurence Evansa6f73462013-12-04 23:47:56 +0000788 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
789 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
790 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
791 ptp_data->ts_corrections.tx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100792
793 skb_tstamp_tx(skb, &timestamps);
794
795 rc = 0;
796
797fail:
798 dev_kfree_skb(skb);
799
800 return rc;
801}
802
803static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
804{
805 struct efx_ptp_data *ptp = efx->ptp_data;
806 struct list_head *cursor;
807 struct list_head *next;
808
809 /* Drop time-expired events */
810 spin_lock_bh(&ptp->evt_lock);
811 if (!list_empty(&ptp->evt_list)) {
812 list_for_each_safe(cursor, next, &ptp->evt_list) {
813 struct efx_ptp_event_rx *evt;
814
815 evt = list_entry(cursor, struct efx_ptp_event_rx,
816 link);
817 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000818 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100819 netif_warn(efx, hw, efx->net_dev,
820 "PTP rx event dropped\n");
821 }
822 }
823 }
Laurence Evansf3211602013-01-28 14:51:17 +0000824 /* If the event overflow flag is set and the event list is now empty
825 * clear the flag to re-enable the overflow warning message.
826 */
827 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
828 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100829 spin_unlock_bh(&ptp->evt_lock);
830}
831
832static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
833 struct sk_buff *skb)
834{
835 struct efx_ptp_data *ptp = efx->ptp_data;
836 bool evts_waiting;
837 struct list_head *cursor;
838 struct list_head *next;
839 struct efx_ptp_match *match;
840 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
841
842 spin_lock_bh(&ptp->evt_lock);
843 evts_waiting = !list_empty(&ptp->evt_list);
844 spin_unlock_bh(&ptp->evt_lock);
845
846 if (!evts_waiting)
847 return PTP_PACKET_STATE_UNMATCHED;
848
849 match = (struct efx_ptp_match *)skb->cb;
850 /* Look for a matching timestamp in the event queue */
851 spin_lock_bh(&ptp->evt_lock);
852 list_for_each_safe(cursor, next, &ptp->evt_list) {
853 struct efx_ptp_event_rx *evt;
854
855 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
856 if ((evt->seq0 == match->words[0]) &&
857 (evt->seq1 == match->words[1])) {
858 struct skb_shared_hwtstamps *timestamps;
859
860 /* Match - add in hardware timestamp */
861 timestamps = skb_hwtstamps(skb);
862 timestamps->hwtstamp = evt->hwtimestamp;
863
864 match->state = PTP_PACKET_STATE_MATCHED;
865 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000866 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100867 break;
868 }
869 }
Laurence Evansf3211602013-01-28 14:51:17 +0000870 /* If the event overflow flag is set and the event list is now empty
871 * clear the flag to re-enable the overflow warning message.
872 */
873 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
874 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100875 spin_unlock_bh(&ptp->evt_lock);
876
877 return rc;
878}
879
880/* Process any queued receive events and corresponding packets
881 *
882 * q is returned with all the packets that are ready for delivery.
883 * true is returned if at least one of those packets requires
884 * synchronisation.
885 */
886static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
887{
888 struct efx_ptp_data *ptp = efx->ptp_data;
889 bool rc = false;
890 struct sk_buff *skb;
891
892 while ((skb = skb_dequeue(&ptp->rxq))) {
893 struct efx_ptp_match *match;
894
895 match = (struct efx_ptp_match *)skb->cb;
896 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
897 __skb_queue_tail(q, skb);
898 } else if (efx_ptp_match_rx(efx, skb) ==
899 PTP_PACKET_STATE_MATCHED) {
900 rc = true;
901 __skb_queue_tail(q, skb);
902 } else if (time_after(jiffies, match->expiry)) {
903 match->state = PTP_PACKET_STATE_TIMED_OUT;
Ben Hutchings35f9a7a2013-12-06 22:10:46 +0000904 if (net_ratelimit())
905 netif_warn(efx, rx_err, efx->net_dev,
906 "PTP packet - no timestamp seen\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100907 __skb_queue_tail(q, skb);
908 } else {
909 /* Replace unprocessed entry and stop */
910 skb_queue_head(&ptp->rxq, skb);
911 break;
912 }
913 }
914
915 return rc;
916}
917
918/* Complete processing of a received packet */
919static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
920{
921 local_bh_disable();
922 netif_receive_skb(skb);
923 local_bh_enable();
924}
925
Ben Hutchings62a1c702013-10-15 17:54:56 +0100926static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
927{
928 struct efx_ptp_data *ptp = efx->ptp_data;
929
930 if (ptp->rxfilter_installed) {
931 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
932 ptp->rxfilter_general);
933 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
934 ptp->rxfilter_event);
935 ptp->rxfilter_installed = false;
936 }
937}
938
939static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100940{
941 struct efx_ptp_data *ptp = efx->ptp_data;
942 struct efx_filter_spec rxfilter;
943 int rc;
944
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100945 if (!ptp->channel || ptp->rxfilter_installed)
Ben Hutchings62a1c702013-10-15 17:54:56 +0100946 return 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100947
948 /* Must filter on both event and general ports to ensure
949 * that there is no packet re-ordering.
950 */
951 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
952 efx_rx_queue_index(
953 efx_channel_get_rx_queue(ptp->channel)));
954 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
955 htonl(PTP_ADDRESS),
956 htons(PTP_EVENT_PORT));
957 if (rc != 0)
958 return rc;
959
960 rc = efx_filter_insert_filter(efx, &rxfilter, true);
961 if (rc < 0)
962 return rc;
963 ptp->rxfilter_event = rc;
964
965 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
966 efx_rx_queue_index(
967 efx_channel_get_rx_queue(ptp->channel)));
968 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
969 htonl(PTP_ADDRESS),
970 htons(PTP_GENERAL_PORT));
971 if (rc != 0)
972 goto fail;
973
974 rc = efx_filter_insert_filter(efx, &rxfilter, true);
975 if (rc < 0)
976 goto fail;
977 ptp->rxfilter_general = rc;
978
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100979 ptp->rxfilter_installed = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100980 return 0;
981
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100982fail:
983 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
984 ptp->rxfilter_event);
Ben Hutchings62a1c702013-10-15 17:54:56 +0100985 return rc;
986}
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100987
Ben Hutchings62a1c702013-10-15 17:54:56 +0100988static int efx_ptp_start(struct efx_nic *efx)
989{
990 struct efx_ptp_data *ptp = efx->ptp_data;
991 int rc;
992
993 ptp->reset_required = false;
994
995 rc = efx_ptp_insert_multicast_filters(efx);
996 if (rc)
997 return rc;
998
999 rc = efx_ptp_enable(efx);
1000 if (rc != 0)
1001 goto fail;
1002
1003 ptp->evt_frag_idx = 0;
1004 ptp->current_adjfreq = 0;
1005
1006 return 0;
1007
1008fail:
1009 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001010 return rc;
1011}
1012
1013static int efx_ptp_stop(struct efx_nic *efx)
1014{
1015 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001016 struct list_head *cursor;
1017 struct list_head *next;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001018 int rc;
1019
1020 if (ptp == NULL)
1021 return 0;
1022
1023 rc = efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001024
Ben Hutchings62a1c702013-10-15 17:54:56 +01001025 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001026
1027 /* Make sure RX packets are really delivered */
1028 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1029 skb_queue_purge(&efx->ptp_data->txq);
1030
1031 /* Drop any pending receive events */
1032 spin_lock_bh(&efx->ptp_data->evt_lock);
1033 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +00001034 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001035 }
Laurence Evansf3211602013-01-28 14:51:17 +00001036 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001037 spin_unlock_bh(&efx->ptp_data->evt_lock);
1038
1039 return rc;
1040}
1041
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001042static int efx_ptp_restart(struct efx_nic *efx)
1043{
1044 if (efx->ptp_data && efx->ptp_data->enabled)
1045 return efx_ptp_start(efx);
1046 return 0;
1047}
1048
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001049static void efx_ptp_pps_worker(struct work_struct *work)
1050{
1051 struct efx_ptp_data *ptp =
1052 container_of(work, struct efx_ptp_data, pps_work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001053 struct efx_nic *efx = ptp->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001054 struct ptp_clock_event ptp_evt;
1055
1056 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1057 return;
1058
1059 ptp_evt.type = PTP_CLOCK_PPSUSR;
1060 ptp_evt.pps_times = ptp->host_time_pps;
1061 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1062}
1063
1064/* Process any pending transmissions and timestamp any received packets.
1065 */
1066static void efx_ptp_worker(struct work_struct *work)
1067{
1068 struct efx_ptp_data *ptp_data =
1069 container_of(work, struct efx_ptp_data, work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001070 struct efx_nic *efx = ptp_data->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001071 struct sk_buff *skb;
1072 struct sk_buff_head tempq;
1073
1074 if (ptp_data->reset_required) {
1075 efx_ptp_stop(efx);
1076 efx_ptp_start(efx);
1077 return;
1078 }
1079
1080 efx_ptp_drop_time_expired_events(efx);
1081
1082 __skb_queue_head_init(&tempq);
1083 if (efx_ptp_process_events(efx, &tempq) ||
1084 !skb_queue_empty(&ptp_data->txq)) {
1085
1086 while ((skb = skb_dequeue(&ptp_data->txq)))
1087 efx_ptp_xmit_skb(efx, skb);
1088 }
1089
1090 while ((skb = __skb_dequeue(&tempq)))
1091 efx_ptp_process_rx(efx, skb);
1092}
1093
Ben Hutchings5d0dab02013-10-16 18:32:41 +01001094static const struct ptp_clock_info efx_phc_clock_info = {
1095 .owner = THIS_MODULE,
1096 .name = "sfc",
1097 .max_adj = MAX_PPB,
1098 .n_alarm = 0,
1099 .n_ext_ts = 0,
1100 .n_per_out = 0,
1101 .pps = 1,
1102 .adjfreq = efx_phc_adjfreq,
1103 .adjtime = efx_phc_adjtime,
1104 .gettime = efx_phc_gettime,
1105 .settime = efx_phc_settime,
1106 .enable = efx_phc_enable,
1107};
1108
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001109/* Initialise PTP state. */
1110int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001111{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001112 struct efx_ptp_data *ptp;
1113 int rc = 0;
1114 unsigned int pos;
1115
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001116 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1117 efx->ptp_data = ptp;
1118 if (!efx->ptp_data)
1119 return -ENOMEM;
1120
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001121 ptp->efx = efx;
1122 ptp->channel = channel;
1123
Ben Hutchings0d19a542012-09-18 21:59:52 +01001124 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001125 if (rc != 0)
1126 goto fail1;
1127
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001128 skb_queue_head_init(&ptp->rxq);
1129 skb_queue_head_init(&ptp->txq);
1130 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1131 if (!ptp->workwq) {
1132 rc = -ENOMEM;
1133 goto fail2;
1134 }
1135
1136 INIT_WORK(&ptp->work, efx_ptp_worker);
1137 ptp->config.flags = 0;
1138 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1139 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1140 INIT_LIST_HEAD(&ptp->evt_list);
1141 INIT_LIST_HEAD(&ptp->evt_free_list);
1142 spin_lock_init(&ptp->evt_lock);
1143 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1144 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
Laurence Evansf3211602013-01-28 14:51:17 +00001145 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001146
Laurence Evansa6f73462013-12-04 23:47:56 +00001147 /* Get the NIC PTP attributes and set up time conversions */
1148 rc = efx_ptp_get_attributes(efx);
1149 if (rc < 0)
1150 goto fail3;
1151
1152 /* Get the timestamp corrections */
1153 rc = efx_ptp_get_timestamp_corrections(efx);
1154 if (rc < 0)
1155 goto fail3;
1156
Ben Hutchings5d0dab02013-10-16 18:32:41 +01001157 ptp->phc_clock_info = efx_phc_clock_info;
Richard Cochran1ef76152012-09-22 07:02:03 +00001158 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1159 &efx->pci_dev->dev);
Wei Yongjun155d9402013-05-07 02:19:25 +00001160 if (IS_ERR(ptp->phc_clock)) {
1161 rc = PTR_ERR(ptp->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001162 goto fail3;
Wei Yongjun155d9402013-05-07 02:19:25 +00001163 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001164
1165 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1166 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1167 if (!ptp->pps_workwq) {
1168 rc = -ENOMEM;
1169 goto fail4;
1170 }
1171 ptp->nic_ts_enabled = false;
1172
1173 return 0;
1174fail4:
1175 ptp_clock_unregister(efx->ptp_data->phc_clock);
1176
1177fail3:
1178 destroy_workqueue(efx->ptp_data->workwq);
1179
1180fail2:
1181 efx_nic_free_buffer(efx, &ptp->start);
1182
1183fail1:
1184 kfree(efx->ptp_data);
1185 efx->ptp_data = NULL;
1186
1187 return rc;
1188}
1189
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001190/* Initialise PTP channel.
1191 *
1192 * Setting core_index to zero causes the queue to be initialised and doesn't
1193 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1194 */
1195static int efx_ptp_probe_channel(struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001196{
1197 struct efx_nic *efx = channel->efx;
1198
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001199 channel->irq_moderation = 0;
1200 channel->rx_queue.core_index = 0;
1201
1202 return efx_ptp_probe(efx, channel);
1203}
1204
1205void efx_ptp_remove(struct efx_nic *efx)
1206{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001207 if (!efx->ptp_data)
1208 return;
1209
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001210 (void)efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001211
1212 cancel_work_sync(&efx->ptp_data->work);
1213 cancel_work_sync(&efx->ptp_data->pps_work);
1214
1215 skb_queue_purge(&efx->ptp_data->rxq);
1216 skb_queue_purge(&efx->ptp_data->txq);
1217
1218 ptp_clock_unregister(efx->ptp_data->phc_clock);
1219
1220 destroy_workqueue(efx->ptp_data->workwq);
1221 destroy_workqueue(efx->ptp_data->pps_workwq);
1222
1223 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1224 kfree(efx->ptp_data);
1225}
1226
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001227static void efx_ptp_remove_channel(struct efx_channel *channel)
1228{
1229 efx_ptp_remove(channel->efx);
1230}
1231
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001232static void efx_ptp_get_channel_name(struct efx_channel *channel,
1233 char *buf, size_t len)
1234{
1235 snprintf(buf, len, "%s-ptp", channel->efx->name);
1236}
1237
1238/* Determine whether this packet should be processed by the PTP module
1239 * or transmitted conventionally.
1240 */
1241bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1242{
1243 return efx->ptp_data &&
1244 efx->ptp_data->enabled &&
1245 skb->len >= PTP_MIN_LENGTH &&
1246 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1247 likely(skb->protocol == htons(ETH_P_IP)) &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001248 skb_transport_header_was_set(skb) &&
1249 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001250 ip_hdr(skb)->protocol == IPPROTO_UDP &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001251 skb_headlen(skb) >=
1252 skb_transport_offset(skb) + sizeof(struct udphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001253 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1254}
1255
1256/* Receive a PTP packet. Packets are queued until the arrival of
1257 * the receive timestamp from the MC - this will probably occur after the
1258 * packet arrival because of the processing in the MC.
1259 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001260static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001261{
1262 struct efx_nic *efx = channel->efx;
1263 struct efx_ptp_data *ptp = efx->ptp_data;
1264 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001265 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001266 unsigned int version;
1267
1268 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1269
1270 /* Correct version? */
1271 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001272 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001273 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001274 }
1275 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1276 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001277 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001278 }
Laurence Evansc939a312012-11-15 10:56:07 +00001279
1280 /* PTP V1 uses all six bytes of the UUID to match the packet
1281 * to the timestamp
1282 */
1283 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1284 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001285 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001286 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001287 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001288 }
1289 version = skb->data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001290 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001291 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001292 }
Laurence Evansc939a312012-11-15 10:56:07 +00001293
1294 /* The original V2 implementation uses bytes 2-7 of
1295 * the UUID to match the packet to the timestamp. This
1296 * discards two of the bytes of the MAC address used
1297 * to create the UUID (SF bug 33070). The PTP V2
1298 * enhanced mode fixes this issue and uses bytes 0-2
1299 * and byte 5-7 of the UUID.
1300 */
1301 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1302 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1303 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1304 } else {
1305 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1306 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1307 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001308 }
1309
1310 /* Does this packet require timestamping? */
1311 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1312 struct skb_shared_hwtstamps *timestamps;
1313
1314 match->state = PTP_PACKET_STATE_UNMATCHED;
1315
1316 /* Clear all timestamps held: filled in later */
1317 timestamps = skb_hwtstamps(skb);
1318 memset(timestamps, 0, sizeof(*timestamps));
1319
Laurence Evansc939a312012-11-15 10:56:07 +00001320 /* We expect the sequence number to be in the same position in
1321 * the packet for PTP V1 and V2
1322 */
1323 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1324 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1325
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001326 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001327 match->words[0] = (match_data_012[0] |
1328 (match_data_012[1] << 8) |
1329 (match_data_012[2] << 16) |
1330 (match_data_345[0] << 24));
1331 match->words[1] = (match_data_345[1] |
1332 (match_data_345[2] << 8) |
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001333 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1334 PTP_V1_SEQUENCE_LENGTH - 1] <<
1335 16));
1336 } else {
1337 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1338 }
1339
1340 skb_queue_tail(&ptp->rxq, skb);
1341 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001342
1343 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001344}
1345
1346/* Transmit a PTP packet. This has to be transmitted by the MC
1347 * itself, through an MCDI call. MCDI calls aren't permitted
1348 * in the transmit path so defer the actual transmission to a suitable worker.
1349 */
1350int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1351{
1352 struct efx_ptp_data *ptp = efx->ptp_data;
1353
1354 skb_queue_tail(&ptp->txq, skb);
1355
1356 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1357 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1358 efx_xmit_hwtstamp_pending(skb);
1359 queue_work(ptp->workwq, &ptp->work);
1360
1361 return NETDEV_TX_OK;
1362}
1363
1364static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1365 unsigned int new_mode)
1366{
1367 if ((enable_wanted != efx->ptp_data->enabled) ||
1368 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001369 int rc = 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001370
1371 if (enable_wanted) {
1372 /* Change of mode requires disable */
1373 if (efx->ptp_data->enabled &&
1374 (efx->ptp_data->mode != new_mode)) {
1375 efx->ptp_data->enabled = false;
1376 rc = efx_ptp_stop(efx);
1377 if (rc != 0)
1378 return rc;
1379 }
1380
1381 /* Set new operating mode and establish
1382 * baseline synchronisation, which must
1383 * succeed.
1384 */
1385 efx->ptp_data->mode = new_mode;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001386 if (netif_running(efx->net_dev))
1387 rc = efx_ptp_start(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001388 if (rc == 0) {
1389 rc = efx_ptp_synchronize(efx,
1390 PTP_SYNC_ATTEMPTS * 2);
1391 if (rc != 0)
1392 efx_ptp_stop(efx);
1393 }
1394 } else {
1395 rc = efx_ptp_stop(efx);
1396 }
1397
1398 if (rc != 0)
1399 return rc;
1400
1401 efx->ptp_data->enabled = enable_wanted;
1402 }
1403
1404 return 0;
1405}
1406
1407static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1408{
1409 bool enable_wanted = false;
1410 unsigned int new_mode;
1411 int rc;
1412
1413 if (init->flags)
1414 return -EINVAL;
1415
1416 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1417 (init->tx_type != HWTSTAMP_TX_ON))
1418 return -ERANGE;
1419
1420 new_mode = efx->ptp_data->mode;
1421 /* Determine whether any PTP HW operations are required */
1422 switch (init->rx_filter) {
1423 case HWTSTAMP_FILTER_NONE:
1424 break;
1425 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1426 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1427 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1428 init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1429 new_mode = MC_CMD_PTP_MODE_V1;
1430 enable_wanted = true;
1431 break;
1432 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1433 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1434 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1435 /* Although these three are accepted only IPV4 packets will be
1436 * timestamped
1437 */
1438 init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
Laurence Evansc939a312012-11-15 10:56:07 +00001439 new_mode = MC_CMD_PTP_MODE_V2_ENHANCED;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001440 enable_wanted = true;
1441 break;
1442 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1443 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1444 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1445 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1446 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1447 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1448 /* Non-IP + IPv6 timestamping not supported */
1449 return -ERANGE;
1450 break;
1451 default:
1452 return -ERANGE;
1453 }
1454
1455 if (init->tx_type != HWTSTAMP_TX_OFF)
1456 enable_wanted = true;
1457
Laurence Evansc939a312012-11-15 10:56:07 +00001458 /* Old versions of the firmware do not support the improved
1459 * UUID filtering option (SF bug 33070). If the firmware does
1460 * not accept the enhanced mode, fall back to the standard PTP
1461 * v2 UUID filtering.
1462 */
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001463 rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
Laurence Evansc939a312012-11-15 10:56:07 +00001464 if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED))
1465 rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001466 if (rc != 0)
1467 return rc;
1468
1469 efx->ptp_data->config = *init;
1470
1471 return 0;
1472}
1473
Ben Hutchings62ebac92013-04-08 17:34:58 +01001474void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001475{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001476 struct efx_ptp_data *ptp = efx->ptp_data;
1477
1478 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001479 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001480
Ben Hutchings62ebac92013-04-08 17:34:58 +01001481 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1482 SOF_TIMESTAMPING_RX_HARDWARE |
1483 SOF_TIMESTAMPING_RAW_HARDWARE);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001484 ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
1485 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1486 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
1487 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
1488 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
1489 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
1490 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
1491 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
1492 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001493}
1494
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001495int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001496{
1497 struct hwtstamp_config config;
1498 int rc;
1499
1500 /* Not a PTP enabled port */
1501 if (!efx->ptp_data)
1502 return -EOPNOTSUPP;
1503
1504 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1505 return -EFAULT;
1506
1507 rc = efx_ptp_ts_init(efx, &config);
1508 if (rc != 0)
1509 return rc;
1510
1511 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1512 ? -EFAULT : 0;
1513}
1514
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001515int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1516{
1517 if (!efx->ptp_data)
1518 return -EOPNOTSUPP;
1519
1520 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1521 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1522}
1523
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001524static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1525{
1526 struct efx_ptp_data *ptp = efx->ptp_data;
1527
1528 netif_err(efx, hw, efx->net_dev,
1529 "PTP unexpected event length: got %d expected %d\n",
1530 ptp->evt_frag_idx, expected_frag_len);
1531 ptp->reset_required = true;
1532 queue_work(ptp->workwq, &ptp->work);
1533}
1534
1535/* Process a completed receive event. Put it on the event queue and
1536 * start worker thread. This is required because event and their
1537 * correspoding packets may come in either order.
1538 */
1539static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1540{
1541 struct efx_ptp_event_rx *evt = NULL;
1542
1543 if (ptp->evt_frag_idx != 3) {
1544 ptp_event_failure(efx, 3);
1545 return;
1546 }
1547
1548 spin_lock_bh(&ptp->evt_lock);
1549 if (!list_empty(&ptp->evt_free_list)) {
1550 evt = list_first_entry(&ptp->evt_free_list,
1551 struct efx_ptp_event_rx, link);
1552 list_del(&evt->link);
1553
1554 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1555 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1556 MCDI_EVENT_SRC) |
1557 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1558 MCDI_EVENT_SRC) << 8) |
1559 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1560 MCDI_EVENT_SRC) << 16));
Laurence Evansa6f73462013-12-04 23:47:56 +00001561 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001562 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
Laurence Evansa6f73462013-12-04 23:47:56 +00001563 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1564 ptp->ts_corrections.rx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001565 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1566 list_add_tail(&evt->link, &ptp->evt_list);
1567
1568 queue_work(ptp->workwq, &ptp->work);
Laurence Evansf3211602013-01-28 14:51:17 +00001569 } else if (!ptp->evt_overflow) {
1570 /* Log a warning message and set the event overflow flag.
1571 * The message won't be logged again until the event queue
1572 * becomes empty.
1573 */
1574 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1575 ptp->evt_overflow = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001576 }
1577 spin_unlock_bh(&ptp->evt_lock);
1578}
1579
1580static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1581{
1582 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1583 if (ptp->evt_frag_idx != 1) {
1584 ptp_event_failure(efx, 1);
1585 return;
1586 }
1587
1588 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1589}
1590
1591static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1592{
1593 if (ptp->nic_ts_enabled)
1594 queue_work(ptp->pps_workwq, &ptp->pps_work);
1595}
1596
1597void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1598{
1599 struct efx_ptp_data *ptp = efx->ptp_data;
1600 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1601
1602 if (!ptp->enabled)
1603 return;
1604
1605 if (ptp->evt_frag_idx == 0) {
1606 ptp->evt_code = code;
1607 } else if (ptp->evt_code != code) {
1608 netif_err(efx, hw, efx->net_dev,
1609 "PTP out of sequence event %d\n", code);
1610 ptp->evt_frag_idx = 0;
1611 }
1612
1613 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1614 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1615 /* Process resulting event */
1616 switch (code) {
1617 case MCDI_EVENT_CODE_PTP_RX:
1618 ptp_event_rx(efx, ptp);
1619 break;
1620 case MCDI_EVENT_CODE_PTP_FAULT:
1621 ptp_event_fault(efx, ptp);
1622 break;
1623 case MCDI_EVENT_CODE_PTP_PPS:
1624 ptp_event_pps(efx, ptp);
1625 break;
1626 default:
1627 netif_err(efx, hw, efx->net_dev,
1628 "PTP unknown event %d\n", code);
1629 break;
1630 }
1631 ptp->evt_frag_idx = 0;
1632 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1633 netif_err(efx, hw, efx->net_dev,
1634 "PTP too many event fragments\n");
1635 ptp->evt_frag_idx = 0;
1636 }
1637}
1638
1639static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1640{
1641 struct efx_ptp_data *ptp_data = container_of(ptp,
1642 struct efx_ptp_data,
1643 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001644 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001645 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001646 s64 adjustment_ns;
1647 int rc;
1648
1649 if (delta > MAX_PPB)
1650 delta = MAX_PPB;
1651 else if (delta < -MAX_PPB)
1652 delta = -MAX_PPB;
1653
1654 /* Convert ppb to fixed point ns. */
1655 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1656 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1657
1658 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001659 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001660 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001661 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1662 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1663 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1664 NULL, 0, NULL);
1665 if (rc != 0)
1666 return rc;
1667
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001668 ptp_data->current_adjfreq = adjustment_ns;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001669 return 0;
1670}
1671
1672static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1673{
Laurence Evansa6f73462013-12-04 23:47:56 +00001674 u32 nic_major, nic_minor;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001675 struct efx_ptp_data *ptp_data = container_of(ptp,
1676 struct efx_ptp_data,
1677 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001678 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001679 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001680
Laurence Evansa6f73462013-12-04 23:47:56 +00001681 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
1682
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001683 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001684 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001685 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
Laurence Evansa6f73462013-12-04 23:47:56 +00001686 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
1687 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001688 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1689 NULL, 0, NULL);
1690}
1691
1692static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1693{
1694 struct efx_ptp_data *ptp_data = container_of(ptp,
1695 struct efx_ptp_data,
1696 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001697 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001698 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1699 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001700 int rc;
Laurence Evansa6f73462013-12-04 23:47:56 +00001701 ktime_t kt;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001702
1703 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001704 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001705
1706 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1707 outbuf, sizeof(outbuf), NULL);
1708 if (rc != 0)
1709 return rc;
1710
Laurence Evansa6f73462013-12-04 23:47:56 +00001711 kt = ptp_data->nic_to_kernel_time(
1712 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
1713 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
1714 *ts = ktime_to_timespec(kt);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001715 return 0;
1716}
1717
1718static int efx_phc_settime(struct ptp_clock_info *ptp,
1719 const struct timespec *e_ts)
1720{
1721 /* Get the current NIC time, efx_phc_gettime.
1722 * Subtract from the desired time to get the offset
1723 * call efx_phc_adjtime with the offset
1724 */
1725 int rc;
1726 struct timespec time_now;
1727 struct timespec delta;
1728
1729 rc = efx_phc_gettime(ptp, &time_now);
1730 if (rc != 0)
1731 return rc;
1732
1733 delta = timespec_sub(*e_ts, time_now);
1734
Julia Lawall56567c62013-01-21 03:02:48 +00001735 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001736 if (rc != 0)
1737 return rc;
1738
1739 return 0;
1740}
1741
1742static int efx_phc_enable(struct ptp_clock_info *ptp,
1743 struct ptp_clock_request *request,
1744 int enable)
1745{
1746 struct efx_ptp_data *ptp_data = container_of(ptp,
1747 struct efx_ptp_data,
1748 phc_clock_info);
1749 if (request->type != PTP_CLK_REQ_PPS)
1750 return -EOPNOTSUPP;
1751
1752 ptp_data->nic_ts_enabled = !!enable;
1753 return 0;
1754}
1755
1756static const struct efx_channel_type efx_ptp_channel_type = {
1757 .handle_no_channel = efx_ptp_handle_no_channel,
1758 .pre_probe = efx_ptp_probe_channel,
1759 .post_remove = efx_ptp_remove_channel,
1760 .get_name = efx_ptp_get_channel_name,
1761 /* no copy operation; there is no need to reallocate this channel */
1762 .receive_skb = efx_ptp_rx,
1763 .keep_eventq = false,
1764};
1765
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001766void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001767{
1768 /* Check whether PTP is implemented on this NIC. The DISABLE
1769 * operation will succeed if and only if it is implemented.
1770 */
1771 if (efx_ptp_disable(efx) == 0)
1772 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1773 &efx_ptp_channel_type;
1774}
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001775
1776void efx_ptp_start_datapath(struct efx_nic *efx)
1777{
1778 if (efx_ptp_restart(efx))
1779 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
1780}
1781
1782void efx_ptp_stop_datapath(struct efx_nic *efx)
1783{
1784 efx_ptp_stop(efx);
1785}