blob: 82d8e27e8a5847521ce2bbb0360d3b68eb6acd57 [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 */
65#define MIN_SYNCHRONISATION_NS 120
66
67/* Maximum permitted length of a (corrected) synchronisation time */
68#define MAX_SYNCHRONISATION_NS 1000
69
70/* How many (MC) receive events that can be queued */
71#define MAX_RECEIVE_EVENTS 8
72
73/* Length of (modified) moving average. */
74#define AVERAGE_LENGTH 16
75
76/* How long an unmatched event or packet can be held */
77#define PKT_EVENT_LIFETIME_MS 10
78
79/* Offsets into PTP packet for identification. These offsets are from the
80 * start of the IP header, not the MAC header. Note that neither PTP V1 nor
81 * PTP V2 permit the use of IPV4 options.
82 */
83#define PTP_DPORT_OFFSET 22
84
85#define PTP_V1_VERSION_LENGTH 2
86#define PTP_V1_VERSION_OFFSET 28
87
88#define PTP_V1_UUID_LENGTH 6
89#define PTP_V1_UUID_OFFSET 50
90
91#define PTP_V1_SEQUENCE_LENGTH 2
92#define PTP_V1_SEQUENCE_OFFSET 58
93
94/* The minimum length of a PTP V1 packet for offsets, etc. to be valid:
95 * includes IP header.
96 */
97#define PTP_V1_MIN_LENGTH 64
98
99#define PTP_V2_VERSION_LENGTH 1
100#define PTP_V2_VERSION_OFFSET 29
101
Laurence 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
198 * @seconds: Hardware timestamp, seconds
199 * @nanoseconds: Hardware timestamp, nanoseconds
200 * @host_end: Host time immediately after hardware timestamp taken
201 * @waitns: Number of nanoseconds between hardware timestamp being read and
202 * host end time being seen
203 * @window: Difference of host_end and host_start
204 * @valid: Whether this timeset is valid
205 */
206struct efx_ptp_timeset {
207 u32 host_start;
208 u32 seconds;
209 u32 nanoseconds;
210 u32 host_end;
211 u32 waitns;
212 u32 window; /* Derived: end - start, allowing for wrap */
213};
214
215/**
216 * struct efx_ptp_data - Precision Time Protocol (PTP) state
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)
235 * @evt_frags: Partly assembled PTP events
236 * @evt_frag_idx: Current fragment number
237 * @evt_code: Last event code
238 * @start: Address at which MC indicates ready for synchronisation
239 * @host_time_pps: Host time at last PPS
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100240 * @current_adjfreq: Current ppb adjustment.
241 * @phc_clock: Pointer to registered phc device
242 * @phc_clock_info: Registration structure for phc device
243 * @pps_work: pps work task for handling pps events
244 * @pps_workwq: pps work queue
245 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
246 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
247 * allocations in main data path).
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100248 * @timeset: Last set of synchronisation statistics.
249 */
250struct efx_ptp_data {
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100251 struct efx_nic *efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100252 struct efx_channel *channel;
253 struct sk_buff_head rxq;
254 struct sk_buff_head txq;
255 struct list_head evt_list;
256 struct list_head evt_free_list;
257 spinlock_t evt_lock;
Laurence Evansf3211602013-01-28 14:51:17 +0000258 bool evt_overflow;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100259 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
260 struct workqueue_struct *workwq;
261 struct work_struct work;
262 bool reset_required;
263 u32 rxfilter_event;
264 u32 rxfilter_general;
265 bool rxfilter_installed;
266 struct hwtstamp_config config;
267 bool enabled;
268 unsigned int mode;
269 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
270 int evt_frag_idx;
271 int evt_code;
272 struct efx_buffer start;
273 struct pps_event_time host_time_pps;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100274 s64 current_adjfreq;
275 struct ptp_clock *phc_clock;
276 struct ptp_clock_info phc_clock_info;
277 struct work_struct pps_work;
278 struct workqueue_struct *pps_workwq;
279 bool nic_ts_enabled;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100280 MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100281 struct efx_ptp_timeset
282 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
283};
284
285static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
286static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
287static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts);
288static int efx_phc_settime(struct ptp_clock_info *ptp,
289 const struct timespec *e_ts);
290static int efx_phc_enable(struct ptp_clock_info *ptp,
291 struct ptp_clock_request *request, int on);
292
293/* Enable MCDI PTP support. */
294static int efx_ptp_enable(struct efx_nic *efx)
295{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100296 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100297 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
298 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100299
300 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000301 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100302 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100303 efx->ptp_data->channel ?
304 efx->ptp_data->channel->channel : 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100305 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
306
Edward Cree1e0b8122013-05-31 18:36:12 +0100307 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
308 outbuf, sizeof(outbuf), NULL);
309 rc = (rc == -EALREADY) ? 0 : rc;
310 if (rc)
311 efx_mcdi_display_error(efx, MC_CMD_PTP,
312 MC_CMD_PTP_IN_ENABLE_LEN,
313 outbuf, sizeof(outbuf), rc);
314 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100315}
316
317/* Disable MCDI PTP support.
318 *
319 * Note that this function should never rely on the presence of ptp_data -
320 * may be called before that exists.
321 */
322static int efx_ptp_disable(struct efx_nic *efx)
323{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100324 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100325 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
326 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100327
328 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000329 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Edward Cree1e0b8122013-05-31 18:36:12 +0100330 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
331 outbuf, sizeof(outbuf), NULL);
332 rc = (rc == -EALREADY) ? 0 : rc;
333 if (rc)
334 efx_mcdi_display_error(efx, MC_CMD_PTP,
335 MC_CMD_PTP_IN_DISABLE_LEN,
336 outbuf, sizeof(outbuf), rc);
337 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100338}
339
340static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
341{
342 struct sk_buff *skb;
343
344 while ((skb = skb_dequeue(q))) {
345 local_bh_disable();
346 netif_receive_skb(skb);
347 local_bh_enable();
348 }
349}
350
351static void efx_ptp_handle_no_channel(struct efx_nic *efx)
352{
353 netif_err(efx, drv, efx->net_dev,
354 "ERROR: PTP requires MSI-X and 1 additional interrupt"
355 "vector. PTP disabled\n");
356}
357
358/* Repeatedly send the host time to the MC which will capture the hardware
359 * time.
360 */
361static void efx_ptp_send_times(struct efx_nic *efx,
362 struct pps_event_time *last_time)
363{
364 struct pps_event_time now;
365 struct timespec limit;
366 struct efx_ptp_data *ptp = efx->ptp_data;
367 struct timespec start;
368 int *mc_running = ptp->start.addr;
369
370 pps_get_ts(&now);
371 start = now.ts_real;
372 limit = now.ts_real;
373 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
374
375 /* Write host time for specified period or until MC is done */
376 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
377 ACCESS_ONCE(*mc_running)) {
378 struct timespec update_time;
379 unsigned int host_time;
380
381 /* Don't update continuously to avoid saturating the PCIe bus */
382 update_time = now.ts_real;
383 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
384 do {
385 pps_get_ts(&now);
386 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
387 ACCESS_ONCE(*mc_running));
388
389 /* Synchronise NIC with single word of time only */
390 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
391 now.ts_real.tv_nsec);
392 /* Update host time in NIC memory */
Laurence Evans977a5d52013-03-07 11:46:58 +0000393 efx->type->ptp_write_host_time(efx, host_time);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100394 }
395 *last_time = now;
396}
397
398/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100399static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
400 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100401{
402 unsigned start_ns, end_ns;
403
404 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
405 timeset->seconds = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_SECONDS);
406 timeset->nanoseconds = MCDI_DWORD(data,
407 PTP_OUT_SYNCHRONIZE_NANOSECONDS);
408 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
409 timeset->waitns = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
410
411 /* Ignore seconds */
412 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
413 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
414 /* Allow for rollover */
415 if (end_ns < start_ns)
416 end_ns += NSEC_PER_SEC;
417 /* Determine duration of operation */
418 timeset->window = end_ns - start_ns;
419}
420
421/* Process times received from MC.
422 *
423 * Extract times from returned results, and establish the minimum value
424 * seen. The minimum value represents the "best" possible time and events
425 * too much greater than this are rejected - the machine is, perhaps, too
426 * busy. A number of readings are taken so that, hopefully, at least one good
427 * synchronisation will be seen in the results.
428 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100429static int
430efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
431 size_t response_length,
432 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100433{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100434 unsigned number_readings =
435 MCDI_VAR_ARRAY_LEN(response_length,
436 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100437 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100438 unsigned total;
439 unsigned ngood = 0;
440 unsigned last_good = 0;
441 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100442 u32 last_sec;
443 u32 start_sec;
444 struct timespec delta;
445
446 if (number_readings == 0)
447 return -EAGAIN;
448
Laurence Evans92304512013-02-11 13:55:08 +0000449 /* Read the set of results and increment stats for any results that
450 * appera to be erroneous.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100451 */
452 for (i = 0; i < number_readings; i++) {
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100453 efx_ptp_read_timeset(
454 MCDI_ARRAY_STRUCT_PTR(synch_buf,
455 PTP_OUT_SYNCHRONIZE_TIMESET, i),
456 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100457 }
458
Laurence Evans92304512013-02-11 13:55:08 +0000459 /* Find the last good host-MC synchronization result. The MC times
460 * when it finishes reading the host time so the corrected window time
461 * should be fairly constant for a given platform.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100462 */
463 total = 0;
464 for (i = 0; i < number_readings; i++)
465 if (ptp->timeset[i].window > ptp->timeset[i].waitns) {
466 unsigned win;
467
468 win = ptp->timeset[i].window - ptp->timeset[i].waitns;
469 if (win >= MIN_SYNCHRONISATION_NS &&
470 win < MAX_SYNCHRONISATION_NS) {
471 total += ptp->timeset[i].window;
472 ngood++;
473 last_good = i;
474 }
475 }
476
477 if (ngood == 0) {
478 netif_warn(efx, drv, efx->net_dev,
Laurence Evans94cd60d2013-11-21 10:38:24 +0000479 "PTP no suitable synchronisations\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100480 return -EAGAIN;
481 }
482
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100483 /* Calculate delay from actual PPS to last_time */
484 delta.tv_nsec =
485 ptp->timeset[last_good].nanoseconds +
486 last_time->ts_real.tv_nsec -
487 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
488
489 /* It is possible that the seconds rolled over between taking
490 * the start reading and the last value written by the host. The
491 * timescales are such that a gap of more than one second is never
492 * expected.
493 */
494 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
495 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
496 if (start_sec != last_sec) {
497 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
498 netif_warn(efx, hw, efx->net_dev,
499 "PTP bad synchronisation seconds\n");
500 return -EAGAIN;
501 } else {
502 delta.tv_sec = 1;
503 }
504 } else {
505 delta.tv_sec = 0;
506 }
507
508 ptp->host_time_pps = *last_time;
509 pps_sub_ts(&ptp->host_time_pps, delta);
510
511 return 0;
512}
513
514/* Synchronize times between the host and the MC */
515static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
516{
517 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100518 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100519 size_t response_length;
520 int rc;
521 unsigned long timeout;
522 struct pps_event_time last_time = {};
523 unsigned int loops = 0;
524 int *start = ptp->start.addr;
525
526 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000527 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100528 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
529 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100530 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
531 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100532
533 /* Clear flag that signals MC ready */
534 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100535 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
536 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
537 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100538
539 /* Wait for start from MCDI (or timeout) */
540 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
541 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
542 udelay(20); /* Usually start MCDI execution quickly */
543 loops++;
544 }
545
546 if (ACCESS_ONCE(*start))
547 efx_ptp_send_times(efx, &last_time);
548
549 /* Collect results */
550 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
551 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
552 synch_buf, sizeof(synch_buf),
553 &response_length);
554 if (rc == 0)
555 rc = efx_ptp_process_times(efx, synch_buf, response_length,
556 &last_time);
557
558 return rc;
559}
560
561/* Transmit a PTP packet, via the MCDI interface, to the wire. */
562static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
563{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100564 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100565 struct skb_shared_hwtstamps timestamps;
566 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100567 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100568 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100569
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100570 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000571 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100572 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100573 if (skb_shinfo(skb)->nr_frags != 0) {
574 rc = skb_linearize(skb);
575 if (rc != 0)
576 goto fail;
577 }
578
579 if (skb->ip_summed == CHECKSUM_PARTIAL) {
580 rc = skb_checksum_help(skb);
581 if (rc != 0)
582 goto fail;
583 }
584 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100585 MCDI_PTR(ptp_data->txbuf,
586 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100587 skb->len);
588 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
589 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
590 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100591 if (rc != 0)
592 goto fail;
593
594 memset(&timestamps, 0, sizeof(timestamps));
595 timestamps.hwtstamp = ktime_set(
596 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_SECONDS),
597 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_NANOSECONDS));
598
599 skb_tstamp_tx(skb, &timestamps);
600
601 rc = 0;
602
603fail:
604 dev_kfree_skb(skb);
605
606 return rc;
607}
608
609static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
610{
611 struct efx_ptp_data *ptp = efx->ptp_data;
612 struct list_head *cursor;
613 struct list_head *next;
614
615 /* Drop time-expired events */
616 spin_lock_bh(&ptp->evt_lock);
617 if (!list_empty(&ptp->evt_list)) {
618 list_for_each_safe(cursor, next, &ptp->evt_list) {
619 struct efx_ptp_event_rx *evt;
620
621 evt = list_entry(cursor, struct efx_ptp_event_rx,
622 link);
623 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000624 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100625 netif_warn(efx, hw, efx->net_dev,
626 "PTP rx event dropped\n");
627 }
628 }
629 }
Laurence Evansf3211602013-01-28 14:51:17 +0000630 /* If the event overflow flag is set and the event list is now empty
631 * clear the flag to re-enable the overflow warning message.
632 */
633 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
634 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100635 spin_unlock_bh(&ptp->evt_lock);
636}
637
638static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
639 struct sk_buff *skb)
640{
641 struct efx_ptp_data *ptp = efx->ptp_data;
642 bool evts_waiting;
643 struct list_head *cursor;
644 struct list_head *next;
645 struct efx_ptp_match *match;
646 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
647
648 spin_lock_bh(&ptp->evt_lock);
649 evts_waiting = !list_empty(&ptp->evt_list);
650 spin_unlock_bh(&ptp->evt_lock);
651
652 if (!evts_waiting)
653 return PTP_PACKET_STATE_UNMATCHED;
654
655 match = (struct efx_ptp_match *)skb->cb;
656 /* Look for a matching timestamp in the event queue */
657 spin_lock_bh(&ptp->evt_lock);
658 list_for_each_safe(cursor, next, &ptp->evt_list) {
659 struct efx_ptp_event_rx *evt;
660
661 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
662 if ((evt->seq0 == match->words[0]) &&
663 (evt->seq1 == match->words[1])) {
664 struct skb_shared_hwtstamps *timestamps;
665
666 /* Match - add in hardware timestamp */
667 timestamps = skb_hwtstamps(skb);
668 timestamps->hwtstamp = evt->hwtimestamp;
669
670 match->state = PTP_PACKET_STATE_MATCHED;
671 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000672 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100673 break;
674 }
675 }
Laurence Evansf3211602013-01-28 14:51:17 +0000676 /* If the event overflow flag is set and the event list is now empty
677 * clear the flag to re-enable the overflow warning message.
678 */
679 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
680 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100681 spin_unlock_bh(&ptp->evt_lock);
682
683 return rc;
684}
685
686/* Process any queued receive events and corresponding packets
687 *
688 * q is returned with all the packets that are ready for delivery.
689 * true is returned if at least one of those packets requires
690 * synchronisation.
691 */
692static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
693{
694 struct efx_ptp_data *ptp = efx->ptp_data;
695 bool rc = false;
696 struct sk_buff *skb;
697
698 while ((skb = skb_dequeue(&ptp->rxq))) {
699 struct efx_ptp_match *match;
700
701 match = (struct efx_ptp_match *)skb->cb;
702 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
703 __skb_queue_tail(q, skb);
704 } else if (efx_ptp_match_rx(efx, skb) ==
705 PTP_PACKET_STATE_MATCHED) {
706 rc = true;
707 __skb_queue_tail(q, skb);
708 } else if (time_after(jiffies, match->expiry)) {
709 match->state = PTP_PACKET_STATE_TIMED_OUT;
Ben Hutchings35f9a7a2013-12-06 22:10:46 +0000710 if (net_ratelimit())
711 netif_warn(efx, rx_err, efx->net_dev,
712 "PTP packet - no timestamp seen\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100713 __skb_queue_tail(q, skb);
714 } else {
715 /* Replace unprocessed entry and stop */
716 skb_queue_head(&ptp->rxq, skb);
717 break;
718 }
719 }
720
721 return rc;
722}
723
724/* Complete processing of a received packet */
725static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
726{
727 local_bh_disable();
728 netif_receive_skb(skb);
729 local_bh_enable();
730}
731
Ben Hutchings62a1c702013-10-15 17:54:56 +0100732static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
733{
734 struct efx_ptp_data *ptp = efx->ptp_data;
735
736 if (ptp->rxfilter_installed) {
737 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
738 ptp->rxfilter_general);
739 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
740 ptp->rxfilter_event);
741 ptp->rxfilter_installed = false;
742 }
743}
744
745static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100746{
747 struct efx_ptp_data *ptp = efx->ptp_data;
748 struct efx_filter_spec rxfilter;
749 int rc;
750
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100751 if (!ptp->channel || ptp->rxfilter_installed)
Ben Hutchings62a1c702013-10-15 17:54:56 +0100752 return 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100753
754 /* Must filter on both event and general ports to ensure
755 * that there is no packet re-ordering.
756 */
757 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
758 efx_rx_queue_index(
759 efx_channel_get_rx_queue(ptp->channel)));
760 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
761 htonl(PTP_ADDRESS),
762 htons(PTP_EVENT_PORT));
763 if (rc != 0)
764 return rc;
765
766 rc = efx_filter_insert_filter(efx, &rxfilter, true);
767 if (rc < 0)
768 return rc;
769 ptp->rxfilter_event = rc;
770
771 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
772 efx_rx_queue_index(
773 efx_channel_get_rx_queue(ptp->channel)));
774 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
775 htonl(PTP_ADDRESS),
776 htons(PTP_GENERAL_PORT));
777 if (rc != 0)
778 goto fail;
779
780 rc = efx_filter_insert_filter(efx, &rxfilter, true);
781 if (rc < 0)
782 goto fail;
783 ptp->rxfilter_general = rc;
784
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100785 ptp->rxfilter_installed = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100786 return 0;
787
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100788fail:
789 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
790 ptp->rxfilter_event);
Ben Hutchings62a1c702013-10-15 17:54:56 +0100791 return rc;
792}
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100793
Ben Hutchings62a1c702013-10-15 17:54:56 +0100794static int efx_ptp_start(struct efx_nic *efx)
795{
796 struct efx_ptp_data *ptp = efx->ptp_data;
797 int rc;
798
799 ptp->reset_required = false;
800
801 rc = efx_ptp_insert_multicast_filters(efx);
802 if (rc)
803 return rc;
804
805 rc = efx_ptp_enable(efx);
806 if (rc != 0)
807 goto fail;
808
809 ptp->evt_frag_idx = 0;
810 ptp->current_adjfreq = 0;
811
812 return 0;
813
814fail:
815 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100816 return rc;
817}
818
819static int efx_ptp_stop(struct efx_nic *efx)
820{
821 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100822 struct list_head *cursor;
823 struct list_head *next;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000824 int rc;
825
826 if (ptp == NULL)
827 return 0;
828
829 rc = efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100830
Ben Hutchings62a1c702013-10-15 17:54:56 +0100831 efx_ptp_remove_multicast_filters(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100832
833 /* Make sure RX packets are really delivered */
834 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
835 skb_queue_purge(&efx->ptp_data->txq);
836
837 /* Drop any pending receive events */
838 spin_lock_bh(&efx->ptp_data->evt_lock);
839 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000840 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100841 }
Laurence Evansf3211602013-01-28 14:51:17 +0000842 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100843 spin_unlock_bh(&efx->ptp_data->evt_lock);
844
845 return rc;
846}
847
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000848static int efx_ptp_restart(struct efx_nic *efx)
849{
850 if (efx->ptp_data && efx->ptp_data->enabled)
851 return efx_ptp_start(efx);
852 return 0;
853}
854
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100855static void efx_ptp_pps_worker(struct work_struct *work)
856{
857 struct efx_ptp_data *ptp =
858 container_of(work, struct efx_ptp_data, pps_work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100859 struct efx_nic *efx = ptp->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100860 struct ptp_clock_event ptp_evt;
861
862 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
863 return;
864
865 ptp_evt.type = PTP_CLOCK_PPSUSR;
866 ptp_evt.pps_times = ptp->host_time_pps;
867 ptp_clock_event(ptp->phc_clock, &ptp_evt);
868}
869
870/* Process any pending transmissions and timestamp any received packets.
871 */
872static void efx_ptp_worker(struct work_struct *work)
873{
874 struct efx_ptp_data *ptp_data =
875 container_of(work, struct efx_ptp_data, work);
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100876 struct efx_nic *efx = ptp_data->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100877 struct sk_buff *skb;
878 struct sk_buff_head tempq;
879
880 if (ptp_data->reset_required) {
881 efx_ptp_stop(efx);
882 efx_ptp_start(efx);
883 return;
884 }
885
886 efx_ptp_drop_time_expired_events(efx);
887
888 __skb_queue_head_init(&tempq);
889 if (efx_ptp_process_events(efx, &tempq) ||
890 !skb_queue_empty(&ptp_data->txq)) {
891
892 while ((skb = skb_dequeue(&ptp_data->txq)))
893 efx_ptp_xmit_skb(efx, skb);
894 }
895
896 while ((skb = __skb_dequeue(&tempq)))
897 efx_ptp_process_rx(efx, skb);
898}
899
Ben Hutchings5d0dab02013-10-16 18:32:41 +0100900static const struct ptp_clock_info efx_phc_clock_info = {
901 .owner = THIS_MODULE,
902 .name = "sfc",
903 .max_adj = MAX_PPB,
904 .n_alarm = 0,
905 .n_ext_ts = 0,
906 .n_per_out = 0,
907 .pps = 1,
908 .adjfreq = efx_phc_adjfreq,
909 .adjtime = efx_phc_adjtime,
910 .gettime = efx_phc_gettime,
911 .settime = efx_phc_settime,
912 .enable = efx_phc_enable,
913};
914
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100915/* Initialise PTP state. */
916int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100917{
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100918 struct efx_ptp_data *ptp;
919 int rc = 0;
920 unsigned int pos;
921
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100922 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
923 efx->ptp_data = ptp;
924 if (!efx->ptp_data)
925 return -ENOMEM;
926
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100927 ptp->efx = efx;
928 ptp->channel = channel;
929
Ben Hutchings0d19a542012-09-18 21:59:52 +0100930 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100931 if (rc != 0)
932 goto fail1;
933
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100934 skb_queue_head_init(&ptp->rxq);
935 skb_queue_head_init(&ptp->txq);
936 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
937 if (!ptp->workwq) {
938 rc = -ENOMEM;
939 goto fail2;
940 }
941
942 INIT_WORK(&ptp->work, efx_ptp_worker);
943 ptp->config.flags = 0;
944 ptp->config.tx_type = HWTSTAMP_TX_OFF;
945 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
946 INIT_LIST_HEAD(&ptp->evt_list);
947 INIT_LIST_HEAD(&ptp->evt_free_list);
948 spin_lock_init(&ptp->evt_lock);
949 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
950 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
Laurence Evansf3211602013-01-28 14:51:17 +0000951 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100952
Ben Hutchings5d0dab02013-10-16 18:32:41 +0100953 ptp->phc_clock_info = efx_phc_clock_info;
Richard Cochran1ef76152012-09-22 07:02:03 +0000954 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
955 &efx->pci_dev->dev);
Wei Yongjun155d9402013-05-07 02:19:25 +0000956 if (IS_ERR(ptp->phc_clock)) {
957 rc = PTR_ERR(ptp->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100958 goto fail3;
Wei Yongjun155d9402013-05-07 02:19:25 +0000959 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100960
961 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
962 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
963 if (!ptp->pps_workwq) {
964 rc = -ENOMEM;
965 goto fail4;
966 }
967 ptp->nic_ts_enabled = false;
968
969 return 0;
970fail4:
971 ptp_clock_unregister(efx->ptp_data->phc_clock);
972
973fail3:
974 destroy_workqueue(efx->ptp_data->workwq);
975
976fail2:
977 efx_nic_free_buffer(efx, &ptp->start);
978
979fail1:
980 kfree(efx->ptp_data);
981 efx->ptp_data = NULL;
982
983 return rc;
984}
985
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100986/* Initialise PTP channel.
987 *
988 * Setting core_index to zero causes the queue to be initialised and doesn't
989 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
990 */
991static int efx_ptp_probe_channel(struct efx_channel *channel)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100992{
993 struct efx_nic *efx = channel->efx;
994
Ben Hutchingsac36baf2013-10-15 17:54:56 +0100995 channel->irq_moderation = 0;
996 channel->rx_queue.core_index = 0;
997
998 return efx_ptp_probe(efx, channel);
999}
1000
1001void efx_ptp_remove(struct efx_nic *efx)
1002{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001003 if (!efx->ptp_data)
1004 return;
1005
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001006 (void)efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001007
1008 cancel_work_sync(&efx->ptp_data->work);
1009 cancel_work_sync(&efx->ptp_data->pps_work);
1010
1011 skb_queue_purge(&efx->ptp_data->rxq);
1012 skb_queue_purge(&efx->ptp_data->txq);
1013
1014 ptp_clock_unregister(efx->ptp_data->phc_clock);
1015
1016 destroy_workqueue(efx->ptp_data->workwq);
1017 destroy_workqueue(efx->ptp_data->pps_workwq);
1018
1019 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1020 kfree(efx->ptp_data);
1021}
1022
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001023static void efx_ptp_remove_channel(struct efx_channel *channel)
1024{
1025 efx_ptp_remove(channel->efx);
1026}
1027
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001028static void efx_ptp_get_channel_name(struct efx_channel *channel,
1029 char *buf, size_t len)
1030{
1031 snprintf(buf, len, "%s-ptp", channel->efx->name);
1032}
1033
1034/* Determine whether this packet should be processed by the PTP module
1035 * or transmitted conventionally.
1036 */
1037bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1038{
1039 return efx->ptp_data &&
1040 efx->ptp_data->enabled &&
1041 skb->len >= PTP_MIN_LENGTH &&
1042 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1043 likely(skb->protocol == htons(ETH_P_IP)) &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001044 skb_transport_header_was_set(skb) &&
1045 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001046 ip_hdr(skb)->protocol == IPPROTO_UDP &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001047 skb_headlen(skb) >=
1048 skb_transport_offset(skb) + sizeof(struct udphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001049 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1050}
1051
1052/* Receive a PTP packet. Packets are queued until the arrival of
1053 * the receive timestamp from the MC - this will probably occur after the
1054 * packet arrival because of the processing in the MC.
1055 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001056static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001057{
1058 struct efx_nic *efx = channel->efx;
1059 struct efx_ptp_data *ptp = efx->ptp_data;
1060 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001061 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001062 unsigned int version;
1063
1064 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1065
1066 /* Correct version? */
1067 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001068 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001069 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001070 }
1071 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1072 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001073 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001074 }
Laurence Evansc939a312012-11-15 10:56:07 +00001075
1076 /* PTP V1 uses all six bytes of the UUID to match the packet
1077 * to the timestamp
1078 */
1079 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1080 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001081 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001082 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001083 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001084 }
1085 version = skb->data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001086 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001087 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001088 }
Laurence Evansc939a312012-11-15 10:56:07 +00001089
1090 /* The original V2 implementation uses bytes 2-7 of
1091 * the UUID to match the packet to the timestamp. This
1092 * discards two of the bytes of the MAC address used
1093 * to create the UUID (SF bug 33070). The PTP V2
1094 * enhanced mode fixes this issue and uses bytes 0-2
1095 * and byte 5-7 of the UUID.
1096 */
1097 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1098 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1099 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1100 } else {
1101 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1102 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1103 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001104 }
1105
1106 /* Does this packet require timestamping? */
1107 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1108 struct skb_shared_hwtstamps *timestamps;
1109
1110 match->state = PTP_PACKET_STATE_UNMATCHED;
1111
1112 /* Clear all timestamps held: filled in later */
1113 timestamps = skb_hwtstamps(skb);
1114 memset(timestamps, 0, sizeof(*timestamps));
1115
Laurence Evansc939a312012-11-15 10:56:07 +00001116 /* We expect the sequence number to be in the same position in
1117 * the packet for PTP V1 and V2
1118 */
1119 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1120 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1121
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001122 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001123 match->words[0] = (match_data_012[0] |
1124 (match_data_012[1] << 8) |
1125 (match_data_012[2] << 16) |
1126 (match_data_345[0] << 24));
1127 match->words[1] = (match_data_345[1] |
1128 (match_data_345[2] << 8) |
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001129 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1130 PTP_V1_SEQUENCE_LENGTH - 1] <<
1131 16));
1132 } else {
1133 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1134 }
1135
1136 skb_queue_tail(&ptp->rxq, skb);
1137 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001138
1139 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001140}
1141
1142/* Transmit a PTP packet. This has to be transmitted by the MC
1143 * itself, through an MCDI call. MCDI calls aren't permitted
1144 * in the transmit path so defer the actual transmission to a suitable worker.
1145 */
1146int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1147{
1148 struct efx_ptp_data *ptp = efx->ptp_data;
1149
1150 skb_queue_tail(&ptp->txq, skb);
1151
1152 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1153 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1154 efx_xmit_hwtstamp_pending(skb);
1155 queue_work(ptp->workwq, &ptp->work);
1156
1157 return NETDEV_TX_OK;
1158}
1159
1160static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1161 unsigned int new_mode)
1162{
1163 if ((enable_wanted != efx->ptp_data->enabled) ||
1164 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001165 int rc = 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001166
1167 if (enable_wanted) {
1168 /* Change of mode requires disable */
1169 if (efx->ptp_data->enabled &&
1170 (efx->ptp_data->mode != new_mode)) {
1171 efx->ptp_data->enabled = false;
1172 rc = efx_ptp_stop(efx);
1173 if (rc != 0)
1174 return rc;
1175 }
1176
1177 /* Set new operating mode and establish
1178 * baseline synchronisation, which must
1179 * succeed.
1180 */
1181 efx->ptp_data->mode = new_mode;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001182 if (netif_running(efx->net_dev))
1183 rc = efx_ptp_start(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001184 if (rc == 0) {
1185 rc = efx_ptp_synchronize(efx,
1186 PTP_SYNC_ATTEMPTS * 2);
1187 if (rc != 0)
1188 efx_ptp_stop(efx);
1189 }
1190 } else {
1191 rc = efx_ptp_stop(efx);
1192 }
1193
1194 if (rc != 0)
1195 return rc;
1196
1197 efx->ptp_data->enabled = enable_wanted;
1198 }
1199
1200 return 0;
1201}
1202
1203static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1204{
1205 bool enable_wanted = false;
1206 unsigned int new_mode;
1207 int rc;
1208
1209 if (init->flags)
1210 return -EINVAL;
1211
1212 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1213 (init->tx_type != HWTSTAMP_TX_ON))
1214 return -ERANGE;
1215
1216 new_mode = efx->ptp_data->mode;
1217 /* Determine whether any PTP HW operations are required */
1218 switch (init->rx_filter) {
1219 case HWTSTAMP_FILTER_NONE:
1220 break;
1221 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1222 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1223 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1224 init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1225 new_mode = MC_CMD_PTP_MODE_V1;
1226 enable_wanted = true;
1227 break;
1228 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1229 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1230 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1231 /* Although these three are accepted only IPV4 packets will be
1232 * timestamped
1233 */
1234 init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
Laurence Evansc939a312012-11-15 10:56:07 +00001235 new_mode = MC_CMD_PTP_MODE_V2_ENHANCED;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001236 enable_wanted = true;
1237 break;
1238 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1239 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1240 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1241 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1242 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1243 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1244 /* Non-IP + IPv6 timestamping not supported */
1245 return -ERANGE;
1246 break;
1247 default:
1248 return -ERANGE;
1249 }
1250
1251 if (init->tx_type != HWTSTAMP_TX_OFF)
1252 enable_wanted = true;
1253
Laurence Evansc939a312012-11-15 10:56:07 +00001254 /* Old versions of the firmware do not support the improved
1255 * UUID filtering option (SF bug 33070). If the firmware does
1256 * not accept the enhanced mode, fall back to the standard PTP
1257 * v2 UUID filtering.
1258 */
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001259 rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
Laurence Evansc939a312012-11-15 10:56:07 +00001260 if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED))
1261 rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001262 if (rc != 0)
1263 return rc;
1264
1265 efx->ptp_data->config = *init;
1266
1267 return 0;
1268}
1269
Ben Hutchings62ebac92013-04-08 17:34:58 +01001270void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001271{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001272 struct efx_ptp_data *ptp = efx->ptp_data;
1273
1274 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001275 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001276
Ben Hutchings62ebac92013-04-08 17:34:58 +01001277 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1278 SOF_TIMESTAMPING_RX_HARDWARE |
1279 SOF_TIMESTAMPING_RAW_HARDWARE);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001280 ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
1281 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1282 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
1283 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
1284 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
1285 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
1286 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
1287 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
1288 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001289}
1290
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001291int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001292{
1293 struct hwtstamp_config config;
1294 int rc;
1295
1296 /* Not a PTP enabled port */
1297 if (!efx->ptp_data)
1298 return -EOPNOTSUPP;
1299
1300 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1301 return -EFAULT;
1302
1303 rc = efx_ptp_ts_init(efx, &config);
1304 if (rc != 0)
1305 return rc;
1306
1307 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1308 ? -EFAULT : 0;
1309}
1310
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001311int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1312{
1313 if (!efx->ptp_data)
1314 return -EOPNOTSUPP;
1315
1316 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1317 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1318}
1319
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001320static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1321{
1322 struct efx_ptp_data *ptp = efx->ptp_data;
1323
1324 netif_err(efx, hw, efx->net_dev,
1325 "PTP unexpected event length: got %d expected %d\n",
1326 ptp->evt_frag_idx, expected_frag_len);
1327 ptp->reset_required = true;
1328 queue_work(ptp->workwq, &ptp->work);
1329}
1330
1331/* Process a completed receive event. Put it on the event queue and
1332 * start worker thread. This is required because event and their
1333 * correspoding packets may come in either order.
1334 */
1335static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1336{
1337 struct efx_ptp_event_rx *evt = NULL;
1338
1339 if (ptp->evt_frag_idx != 3) {
1340 ptp_event_failure(efx, 3);
1341 return;
1342 }
1343
1344 spin_lock_bh(&ptp->evt_lock);
1345 if (!list_empty(&ptp->evt_free_list)) {
1346 evt = list_first_entry(&ptp->evt_free_list,
1347 struct efx_ptp_event_rx, link);
1348 list_del(&evt->link);
1349
1350 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1351 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1352 MCDI_EVENT_SRC) |
1353 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1354 MCDI_EVENT_SRC) << 8) |
1355 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1356 MCDI_EVENT_SRC) << 16));
1357 evt->hwtimestamp = ktime_set(
1358 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1359 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA));
1360 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1361 list_add_tail(&evt->link, &ptp->evt_list);
1362
1363 queue_work(ptp->workwq, &ptp->work);
Laurence Evansf3211602013-01-28 14:51:17 +00001364 } else if (!ptp->evt_overflow) {
1365 /* Log a warning message and set the event overflow flag.
1366 * The message won't be logged again until the event queue
1367 * becomes empty.
1368 */
1369 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1370 ptp->evt_overflow = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001371 }
1372 spin_unlock_bh(&ptp->evt_lock);
1373}
1374
1375static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1376{
1377 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1378 if (ptp->evt_frag_idx != 1) {
1379 ptp_event_failure(efx, 1);
1380 return;
1381 }
1382
1383 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1384}
1385
1386static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1387{
1388 if (ptp->nic_ts_enabled)
1389 queue_work(ptp->pps_workwq, &ptp->pps_work);
1390}
1391
1392void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1393{
1394 struct efx_ptp_data *ptp = efx->ptp_data;
1395 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1396
1397 if (!ptp->enabled)
1398 return;
1399
1400 if (ptp->evt_frag_idx == 0) {
1401 ptp->evt_code = code;
1402 } else if (ptp->evt_code != code) {
1403 netif_err(efx, hw, efx->net_dev,
1404 "PTP out of sequence event %d\n", code);
1405 ptp->evt_frag_idx = 0;
1406 }
1407
1408 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1409 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1410 /* Process resulting event */
1411 switch (code) {
1412 case MCDI_EVENT_CODE_PTP_RX:
1413 ptp_event_rx(efx, ptp);
1414 break;
1415 case MCDI_EVENT_CODE_PTP_FAULT:
1416 ptp_event_fault(efx, ptp);
1417 break;
1418 case MCDI_EVENT_CODE_PTP_PPS:
1419 ptp_event_pps(efx, ptp);
1420 break;
1421 default:
1422 netif_err(efx, hw, efx->net_dev,
1423 "PTP unknown event %d\n", code);
1424 break;
1425 }
1426 ptp->evt_frag_idx = 0;
1427 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1428 netif_err(efx, hw, efx->net_dev,
1429 "PTP too many event fragments\n");
1430 ptp->evt_frag_idx = 0;
1431 }
1432}
1433
1434static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1435{
1436 struct efx_ptp_data *ptp_data = container_of(ptp,
1437 struct efx_ptp_data,
1438 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001439 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001440 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001441 s64 adjustment_ns;
1442 int rc;
1443
1444 if (delta > MAX_PPB)
1445 delta = MAX_PPB;
1446 else if (delta < -MAX_PPB)
1447 delta = -MAX_PPB;
1448
1449 /* Convert ppb to fixed point ns. */
1450 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1451 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1452
1453 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001454 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001455 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001456 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1457 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1458 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1459 NULL, 0, NULL);
1460 if (rc != 0)
1461 return rc;
1462
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001463 ptp_data->current_adjfreq = adjustment_ns;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001464 return 0;
1465}
1466
1467static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1468{
1469 struct efx_ptp_data *ptp_data = container_of(ptp,
1470 struct efx_ptp_data,
1471 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001472 struct efx_nic *efx = ptp_data->efx;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001473 struct timespec delta_ts = ns_to_timespec(delta);
Ben Hutchings59cfc472012-09-14 17:30:10 +01001474 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001475
1476 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001477 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001478 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001479 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_SECONDS, (u32)delta_ts.tv_sec);
1480 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_NANOSECONDS, (u32)delta_ts.tv_nsec);
1481 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1482 NULL, 0, NULL);
1483}
1484
1485static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1486{
1487 struct efx_ptp_data *ptp_data = container_of(ptp,
1488 struct efx_ptp_data,
1489 phc_clock_info);
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001490 struct efx_nic *efx = ptp_data->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001491 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1492 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001493 int rc;
1494
1495 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001496 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001497
1498 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1499 outbuf, sizeof(outbuf), NULL);
1500 if (rc != 0)
1501 return rc;
1502
1503 ts->tv_sec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_SECONDS);
1504 ts->tv_nsec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_NANOSECONDS);
1505 return 0;
1506}
1507
1508static int efx_phc_settime(struct ptp_clock_info *ptp,
1509 const struct timespec *e_ts)
1510{
1511 /* Get the current NIC time, efx_phc_gettime.
1512 * Subtract from the desired time to get the offset
1513 * call efx_phc_adjtime with the offset
1514 */
1515 int rc;
1516 struct timespec time_now;
1517 struct timespec delta;
1518
1519 rc = efx_phc_gettime(ptp, &time_now);
1520 if (rc != 0)
1521 return rc;
1522
1523 delta = timespec_sub(*e_ts, time_now);
1524
Julia Lawall56567c62013-01-21 03:02:48 +00001525 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001526 if (rc != 0)
1527 return rc;
1528
1529 return 0;
1530}
1531
1532static int efx_phc_enable(struct ptp_clock_info *ptp,
1533 struct ptp_clock_request *request,
1534 int enable)
1535{
1536 struct efx_ptp_data *ptp_data = container_of(ptp,
1537 struct efx_ptp_data,
1538 phc_clock_info);
1539 if (request->type != PTP_CLK_REQ_PPS)
1540 return -EOPNOTSUPP;
1541
1542 ptp_data->nic_ts_enabled = !!enable;
1543 return 0;
1544}
1545
1546static const struct efx_channel_type efx_ptp_channel_type = {
1547 .handle_no_channel = efx_ptp_handle_no_channel,
1548 .pre_probe = efx_ptp_probe_channel,
1549 .post_remove = efx_ptp_remove_channel,
1550 .get_name = efx_ptp_get_channel_name,
1551 /* no copy operation; there is no need to reallocate this channel */
1552 .receive_skb = efx_ptp_rx,
1553 .keep_eventq = false,
1554};
1555
Ben Hutchingsac36baf2013-10-15 17:54:56 +01001556void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001557{
1558 /* Check whether PTP is implemented on this NIC. The DISABLE
1559 * operation will succeed if and only if it is implemented.
1560 */
1561 if (efx_ptp_disable(efx) == 0)
1562 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1563 &efx_ptp_channel_type;
1564}
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001565
1566void efx_ptp_start_datapath(struct efx_nic *efx)
1567{
1568 if (efx_ptp_restart(efx))
1569 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
1570}
1571
1572void efx_ptp_stop_datapath(struct efx_nic *efx)
1573{
1574 efx_ptp_stop(efx);
1575}