blob: 24bd2e3040f8d6ecd7f0e0ec333dba7fdb52c6ae [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
217 * @channel: The PTP channel
218 * @rxq: Receive queue (awaiting timestamps)
219 * @txq: Transmit queue
220 * @evt_list: List of MC receive events awaiting packets
221 * @evt_free_list: List of free events
222 * @evt_lock: Lock for manipulating evt_list and evt_free_list
Laurence Evansf3211602013-01-28 14:51:17 +0000223 * @evt_overflow: Boolean indicating that event list has overflowed
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100224 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
225 * @workwq: Work queue for processing pending PTP operations
226 * @work: Work task
227 * @reset_required: A serious error has occurred and the PTP task needs to be
228 * reset (disable, enable).
229 * @rxfilter_event: Receive filter when operating
230 * @rxfilter_general: Receive filter when operating
231 * @config: Current timestamp configuration
232 * @enabled: PTP operation enabled
233 * @mode: Mode in which PTP operating (PTP version)
234 * @evt_frags: Partly assembled PTP events
235 * @evt_frag_idx: Current fragment number
236 * @evt_code: Last event code
237 * @start: Address at which MC indicates ready for synchronisation
238 * @host_time_pps: Host time at last PPS
239 * @last_sync_ns: Last number of nanoseconds between readings when synchronising
240 * @base_sync_ns: Number of nanoseconds for last synchronisation.
241 * @base_sync_valid: Whether base_sync_time is valid.
242 * @current_adjfreq: Current ppb adjustment.
243 * @phc_clock: Pointer to registered phc device
244 * @phc_clock_info: Registration structure for phc device
245 * @pps_work: pps work task for handling pps events
246 * @pps_workwq: pps work queue
247 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
248 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
249 * allocations in main data path).
250 * @debug_ptp_dir: PTP debugfs directory
251 * @missed_rx_sync: Number of packets received without syncrhonisation.
252 * @good_syncs: Number of successful synchronisations.
253 * @no_time_syncs: Number of synchronisations with no good times.
254 * @bad_sync_durations: Number of synchronisations with bad durations.
255 * @bad_syncs: Number of failed synchronisations.
256 * @last_sync_time: Number of nanoseconds for last synchronisation.
257 * @sync_timeouts: Number of synchronisation timeouts
258 * @fast_syncs: Number of synchronisations requiring short delay
259 * @min_sync_delta: Minimum time between event and synchronisation
260 * @max_sync_delta: Maximum time between event and synchronisation
261 * @average_sync_delta: Average time between event and synchronisation.
262 * Modified moving average.
263 * @last_sync_delta: Last time between event and synchronisation
264 * @mc_stats: Context value for MC statistics
265 * @timeset: Last set of synchronisation statistics.
266 */
267struct efx_ptp_data {
268 struct efx_channel *channel;
269 struct sk_buff_head rxq;
270 struct sk_buff_head txq;
271 struct list_head evt_list;
272 struct list_head evt_free_list;
273 spinlock_t evt_lock;
Laurence Evansf3211602013-01-28 14:51:17 +0000274 bool evt_overflow;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100275 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
276 struct workqueue_struct *workwq;
277 struct work_struct work;
278 bool reset_required;
279 u32 rxfilter_event;
280 u32 rxfilter_general;
281 bool rxfilter_installed;
282 struct hwtstamp_config config;
283 bool enabled;
284 unsigned int mode;
285 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
286 int evt_frag_idx;
287 int evt_code;
288 struct efx_buffer start;
289 struct pps_event_time host_time_pps;
290 unsigned last_sync_ns;
291 unsigned base_sync_ns;
292 bool base_sync_valid;
293 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
312/* Enable MCDI PTP support. */
313static int efx_ptp_enable(struct efx_nic *efx)
314{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100315 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100316 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
317 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100318
319 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000320 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100321 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
322 efx->ptp_data->channel->channel);
323 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
324
Edward Cree1e0b8122013-05-31 18:36:12 +0100325 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
326 outbuf, sizeof(outbuf), NULL);
327 rc = (rc == -EALREADY) ? 0 : rc;
328 if (rc)
329 efx_mcdi_display_error(efx, MC_CMD_PTP,
330 MC_CMD_PTP_IN_ENABLE_LEN,
331 outbuf, sizeof(outbuf), rc);
332 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100333}
334
335/* Disable MCDI PTP support.
336 *
337 * Note that this function should never rely on the presence of ptp_data -
338 * may be called before that exists.
339 */
340static int efx_ptp_disable(struct efx_nic *efx)
341{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100342 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
Edward Cree1e0b8122013-05-31 18:36:12 +0100343 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
344 int rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100345
346 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000347 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Edward Cree1e0b8122013-05-31 18:36:12 +0100348 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
349 outbuf, sizeof(outbuf), NULL);
350 rc = (rc == -EALREADY) ? 0 : rc;
351 if (rc)
352 efx_mcdi_display_error(efx, MC_CMD_PTP,
353 MC_CMD_PTP_IN_DISABLE_LEN,
354 outbuf, sizeof(outbuf), rc);
355 return rc;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100356}
357
358static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
359{
360 struct sk_buff *skb;
361
362 while ((skb = skb_dequeue(q))) {
363 local_bh_disable();
364 netif_receive_skb(skb);
365 local_bh_enable();
366 }
367}
368
369static void efx_ptp_handle_no_channel(struct efx_nic *efx)
370{
371 netif_err(efx, drv, efx->net_dev,
372 "ERROR: PTP requires MSI-X and 1 additional interrupt"
373 "vector. PTP disabled\n");
374}
375
376/* Repeatedly send the host time to the MC which will capture the hardware
377 * time.
378 */
379static void efx_ptp_send_times(struct efx_nic *efx,
380 struct pps_event_time *last_time)
381{
382 struct pps_event_time now;
383 struct timespec limit;
384 struct efx_ptp_data *ptp = efx->ptp_data;
385 struct timespec start;
386 int *mc_running = ptp->start.addr;
387
388 pps_get_ts(&now);
389 start = now.ts_real;
390 limit = now.ts_real;
391 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
392
393 /* Write host time for specified period or until MC is done */
394 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
395 ACCESS_ONCE(*mc_running)) {
396 struct timespec update_time;
397 unsigned int host_time;
398
399 /* Don't update continuously to avoid saturating the PCIe bus */
400 update_time = now.ts_real;
401 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
402 do {
403 pps_get_ts(&now);
404 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
405 ACCESS_ONCE(*mc_running));
406
407 /* Synchronise NIC with single word of time only */
408 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
409 now.ts_real.tv_nsec);
410 /* Update host time in NIC memory */
Laurence Evans977a5d52013-03-07 11:46:58 +0000411 efx->type->ptp_write_host_time(efx, host_time);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100412 }
413 *last_time = now;
414}
415
416/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100417static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
418 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100419{
420 unsigned start_ns, end_ns;
421
422 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
423 timeset->seconds = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_SECONDS);
424 timeset->nanoseconds = MCDI_DWORD(data,
425 PTP_OUT_SYNCHRONIZE_NANOSECONDS);
426 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
427 timeset->waitns = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
428
429 /* Ignore seconds */
430 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
431 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
432 /* Allow for rollover */
433 if (end_ns < start_ns)
434 end_ns += NSEC_PER_SEC;
435 /* Determine duration of operation */
436 timeset->window = end_ns - start_ns;
437}
438
439/* Process times received from MC.
440 *
441 * Extract times from returned results, and establish the minimum value
442 * seen. The minimum value represents the "best" possible time and events
443 * too much greater than this are rejected - the machine is, perhaps, too
444 * busy. A number of readings are taken so that, hopefully, at least one good
445 * synchronisation will be seen in the results.
446 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100447static int
448efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
449 size_t response_length,
450 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100451{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100452 unsigned number_readings =
453 MCDI_VAR_ARRAY_LEN(response_length,
454 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100455 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100456 unsigned total;
457 unsigned ngood = 0;
458 unsigned last_good = 0;
459 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100460 u32 last_sec;
461 u32 start_sec;
462 struct timespec delta;
463
464 if (number_readings == 0)
465 return -EAGAIN;
466
Laurence Evans92304512013-02-11 13:55:08 +0000467 /* Read the set of results and increment stats for any results that
468 * appera to be erroneous.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100469 */
470 for (i = 0; i < number_readings; i++) {
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100471 efx_ptp_read_timeset(
472 MCDI_ARRAY_STRUCT_PTR(synch_buf,
473 PTP_OUT_SYNCHRONIZE_TIMESET, i),
474 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100475 }
476
Laurence Evans92304512013-02-11 13:55:08 +0000477 /* Find the last good host-MC synchronization result. The MC times
478 * when it finishes reading the host time so the corrected window time
479 * should be fairly constant for a given platform.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100480 */
481 total = 0;
482 for (i = 0; i < number_readings; i++)
483 if (ptp->timeset[i].window > ptp->timeset[i].waitns) {
484 unsigned win;
485
486 win = ptp->timeset[i].window - ptp->timeset[i].waitns;
487 if (win >= MIN_SYNCHRONISATION_NS &&
488 win < MAX_SYNCHRONISATION_NS) {
489 total += ptp->timeset[i].window;
490 ngood++;
491 last_good = i;
492 }
493 }
494
495 if (ngood == 0) {
496 netif_warn(efx, drv, efx->net_dev,
Laurence Evans92304512013-02-11 13:55:08 +0000497 "PTP no suitable synchronisations %dns\n",
498 ptp->base_sync_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100499 return -EAGAIN;
500 }
501
502 /* Average minimum this synchronisation */
503 ptp->last_sync_ns = DIV_ROUND_UP(total, ngood);
504 if (!ptp->base_sync_valid || (ptp->last_sync_ns < ptp->base_sync_ns)) {
505 ptp->base_sync_valid = true;
506 ptp->base_sync_ns = ptp->last_sync_ns;
507 }
508
509 /* Calculate delay from actual PPS to last_time */
510 delta.tv_nsec =
511 ptp->timeset[last_good].nanoseconds +
512 last_time->ts_real.tv_nsec -
513 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
514
515 /* It is possible that the seconds rolled over between taking
516 * the start reading and the last value written by the host. The
517 * timescales are such that a gap of more than one second is never
518 * expected.
519 */
520 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
521 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
522 if (start_sec != last_sec) {
523 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
524 netif_warn(efx, hw, efx->net_dev,
525 "PTP bad synchronisation seconds\n");
526 return -EAGAIN;
527 } else {
528 delta.tv_sec = 1;
529 }
530 } else {
531 delta.tv_sec = 0;
532 }
533
534 ptp->host_time_pps = *last_time;
535 pps_sub_ts(&ptp->host_time_pps, delta);
536
537 return 0;
538}
539
540/* Synchronize times between the host and the MC */
541static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
542{
543 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100544 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100545 size_t response_length;
546 int rc;
547 unsigned long timeout;
548 struct pps_event_time last_time = {};
549 unsigned int loops = 0;
550 int *start = ptp->start.addr;
551
552 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000553 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100554 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
555 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100556 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
557 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100558
559 /* Clear flag that signals MC ready */
560 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100561 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
562 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
563 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100564
565 /* Wait for start from MCDI (or timeout) */
566 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
567 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
568 udelay(20); /* Usually start MCDI execution quickly */
569 loops++;
570 }
571
572 if (ACCESS_ONCE(*start))
573 efx_ptp_send_times(efx, &last_time);
574
575 /* Collect results */
576 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
577 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
578 synch_buf, sizeof(synch_buf),
579 &response_length);
580 if (rc == 0)
581 rc = efx_ptp_process_times(efx, synch_buf, response_length,
582 &last_time);
583
584 return rc;
585}
586
587/* Transmit a PTP packet, via the MCDI interface, to the wire. */
588static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
589{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100590 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100591 struct skb_shared_hwtstamps timestamps;
592 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100593 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100594 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100595
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100596 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000597 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100598 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100599 if (skb_shinfo(skb)->nr_frags != 0) {
600 rc = skb_linearize(skb);
601 if (rc != 0)
602 goto fail;
603 }
604
605 if (skb->ip_summed == CHECKSUM_PARTIAL) {
606 rc = skb_checksum_help(skb);
607 if (rc != 0)
608 goto fail;
609 }
610 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100611 MCDI_PTR(ptp_data->txbuf,
612 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100613 skb->len);
614 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
615 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
616 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100617 if (rc != 0)
618 goto fail;
619
620 memset(&timestamps, 0, sizeof(timestamps));
621 timestamps.hwtstamp = ktime_set(
622 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_SECONDS),
623 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_NANOSECONDS));
624
625 skb_tstamp_tx(skb, &timestamps);
626
627 rc = 0;
628
629fail:
630 dev_kfree_skb(skb);
631
632 return rc;
633}
634
635static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
636{
637 struct efx_ptp_data *ptp = efx->ptp_data;
638 struct list_head *cursor;
639 struct list_head *next;
640
641 /* Drop time-expired events */
642 spin_lock_bh(&ptp->evt_lock);
643 if (!list_empty(&ptp->evt_list)) {
644 list_for_each_safe(cursor, next, &ptp->evt_list) {
645 struct efx_ptp_event_rx *evt;
646
647 evt = list_entry(cursor, struct efx_ptp_event_rx,
648 link);
649 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000650 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100651 netif_warn(efx, hw, efx->net_dev,
652 "PTP rx event dropped\n");
653 }
654 }
655 }
Laurence Evansf3211602013-01-28 14:51:17 +0000656 /* If the event overflow flag is set and the event list is now empty
657 * clear the flag to re-enable the overflow warning message.
658 */
659 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
660 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100661 spin_unlock_bh(&ptp->evt_lock);
662}
663
664static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
665 struct sk_buff *skb)
666{
667 struct efx_ptp_data *ptp = efx->ptp_data;
668 bool evts_waiting;
669 struct list_head *cursor;
670 struct list_head *next;
671 struct efx_ptp_match *match;
672 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
673
674 spin_lock_bh(&ptp->evt_lock);
675 evts_waiting = !list_empty(&ptp->evt_list);
676 spin_unlock_bh(&ptp->evt_lock);
677
678 if (!evts_waiting)
679 return PTP_PACKET_STATE_UNMATCHED;
680
681 match = (struct efx_ptp_match *)skb->cb;
682 /* Look for a matching timestamp in the event queue */
683 spin_lock_bh(&ptp->evt_lock);
684 list_for_each_safe(cursor, next, &ptp->evt_list) {
685 struct efx_ptp_event_rx *evt;
686
687 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
688 if ((evt->seq0 == match->words[0]) &&
689 (evt->seq1 == match->words[1])) {
690 struct skb_shared_hwtstamps *timestamps;
691
692 /* Match - add in hardware timestamp */
693 timestamps = skb_hwtstamps(skb);
694 timestamps->hwtstamp = evt->hwtimestamp;
695
696 match->state = PTP_PACKET_STATE_MATCHED;
697 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000698 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100699 break;
700 }
701 }
Laurence Evansf3211602013-01-28 14:51:17 +0000702 /* If the event overflow flag is set and the event list is now empty
703 * clear the flag to re-enable the overflow warning message.
704 */
705 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
706 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100707 spin_unlock_bh(&ptp->evt_lock);
708
709 return rc;
710}
711
712/* Process any queued receive events and corresponding packets
713 *
714 * q is returned with all the packets that are ready for delivery.
715 * true is returned if at least one of those packets requires
716 * synchronisation.
717 */
718static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
719{
720 struct efx_ptp_data *ptp = efx->ptp_data;
721 bool rc = false;
722 struct sk_buff *skb;
723
724 while ((skb = skb_dequeue(&ptp->rxq))) {
725 struct efx_ptp_match *match;
726
727 match = (struct efx_ptp_match *)skb->cb;
728 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
729 __skb_queue_tail(q, skb);
730 } else if (efx_ptp_match_rx(efx, skb) ==
731 PTP_PACKET_STATE_MATCHED) {
732 rc = true;
733 __skb_queue_tail(q, skb);
734 } else if (time_after(jiffies, match->expiry)) {
735 match->state = PTP_PACKET_STATE_TIMED_OUT;
Ben Hutchings35f9a7a2013-12-06 22:10:46 +0000736 if (net_ratelimit())
737 netif_warn(efx, rx_err, efx->net_dev,
738 "PTP packet - no timestamp seen\n");
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100739 __skb_queue_tail(q, skb);
740 } else {
741 /* Replace unprocessed entry and stop */
742 skb_queue_head(&ptp->rxq, skb);
743 break;
744 }
745 }
746
747 return rc;
748}
749
750/* Complete processing of a received packet */
751static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
752{
753 local_bh_disable();
754 netif_receive_skb(skb);
755 local_bh_enable();
756}
757
758static int efx_ptp_start(struct efx_nic *efx)
759{
760 struct efx_ptp_data *ptp = efx->ptp_data;
761 struct efx_filter_spec rxfilter;
762 int rc;
763
764 ptp->reset_required = false;
765
766 /* Must filter on both event and general ports to ensure
767 * that there is no packet re-ordering.
768 */
769 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
770 efx_rx_queue_index(
771 efx_channel_get_rx_queue(ptp->channel)));
772 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
773 htonl(PTP_ADDRESS),
774 htons(PTP_EVENT_PORT));
775 if (rc != 0)
776 return rc;
777
778 rc = efx_filter_insert_filter(efx, &rxfilter, true);
779 if (rc < 0)
780 return rc;
781 ptp->rxfilter_event = rc;
782
783 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
784 efx_rx_queue_index(
785 efx_channel_get_rx_queue(ptp->channel)));
786 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
787 htonl(PTP_ADDRESS),
788 htons(PTP_GENERAL_PORT));
789 if (rc != 0)
790 goto fail;
791
792 rc = efx_filter_insert_filter(efx, &rxfilter, true);
793 if (rc < 0)
794 goto fail;
795 ptp->rxfilter_general = rc;
796
797 rc = efx_ptp_enable(efx);
798 if (rc != 0)
799 goto fail2;
800
801 ptp->evt_frag_idx = 0;
802 ptp->current_adjfreq = 0;
803 ptp->rxfilter_installed = true;
804
805 return 0;
806
807fail2:
808 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
809 ptp->rxfilter_general);
810fail:
811 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
812 ptp->rxfilter_event);
813
814 return rc;
815}
816
817static int efx_ptp_stop(struct efx_nic *efx)
818{
819 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100820 struct list_head *cursor;
821 struct list_head *next;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000822 int rc;
823
824 if (ptp == NULL)
825 return 0;
826
827 rc = efx_ptp_disable(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100828
829 if (ptp->rxfilter_installed) {
830 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
831 ptp->rxfilter_general);
832 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
833 ptp->rxfilter_event);
834 ptp->rxfilter_installed = false;
835 }
836
837 /* Make sure RX packets are really delivered */
838 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
839 skb_queue_purge(&efx->ptp_data->txq);
840
841 /* Drop any pending receive events */
842 spin_lock_bh(&efx->ptp_data->evt_lock);
843 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000844 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100845 }
Laurence Evansf3211602013-01-28 14:51:17 +0000846 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100847 spin_unlock_bh(&efx->ptp_data->evt_lock);
848
849 return rc;
850}
851
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000852static int efx_ptp_restart(struct efx_nic *efx)
853{
854 if (efx->ptp_data && efx->ptp_data->enabled)
855 return efx_ptp_start(efx);
856 return 0;
857}
858
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100859static void efx_ptp_pps_worker(struct work_struct *work)
860{
861 struct efx_ptp_data *ptp =
862 container_of(work, struct efx_ptp_data, pps_work);
863 struct efx_nic *efx = ptp->channel->efx;
864 struct ptp_clock_event ptp_evt;
865
866 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
867 return;
868
869 ptp_evt.type = PTP_CLOCK_PPSUSR;
870 ptp_evt.pps_times = ptp->host_time_pps;
871 ptp_clock_event(ptp->phc_clock, &ptp_evt);
872}
873
874/* Process any pending transmissions and timestamp any received packets.
875 */
876static void efx_ptp_worker(struct work_struct *work)
877{
878 struct efx_ptp_data *ptp_data =
879 container_of(work, struct efx_ptp_data, work);
880 struct efx_nic *efx = ptp_data->channel->efx;
881 struct sk_buff *skb;
882 struct sk_buff_head tempq;
883
884 if (ptp_data->reset_required) {
885 efx_ptp_stop(efx);
886 efx_ptp_start(efx);
887 return;
888 }
889
890 efx_ptp_drop_time_expired_events(efx);
891
892 __skb_queue_head_init(&tempq);
893 if (efx_ptp_process_events(efx, &tempq) ||
894 !skb_queue_empty(&ptp_data->txq)) {
895
896 while ((skb = skb_dequeue(&ptp_data->txq)))
897 efx_ptp_xmit_skb(efx, skb);
898 }
899
900 while ((skb = __skb_dequeue(&tempq)))
901 efx_ptp_process_rx(efx, skb);
902}
903
904/* Initialise PTP channel and state.
905 *
906 * Setting core_index to zero causes the queue to be initialised and doesn't
907 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
908 */
909static int efx_ptp_probe_channel(struct efx_channel *channel)
910{
911 struct efx_nic *efx = channel->efx;
912 struct efx_ptp_data *ptp;
913 int rc = 0;
914 unsigned int pos;
915
916 channel->irq_moderation = 0;
917 channel->rx_queue.core_index = 0;
918
919 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
920 efx->ptp_data = ptp;
921 if (!efx->ptp_data)
922 return -ENOMEM;
923
Ben Hutchings0d19a542012-09-18 21:59:52 +0100924 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100925 if (rc != 0)
926 goto fail1;
927
928 ptp->channel = channel;
929 skb_queue_head_init(&ptp->rxq);
930 skb_queue_head_init(&ptp->txq);
931 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
932 if (!ptp->workwq) {
933 rc = -ENOMEM;
934 goto fail2;
935 }
936
937 INIT_WORK(&ptp->work, efx_ptp_worker);
938 ptp->config.flags = 0;
939 ptp->config.tx_type = HWTSTAMP_TX_OFF;
940 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
941 INIT_LIST_HEAD(&ptp->evt_list);
942 INIT_LIST_HEAD(&ptp->evt_free_list);
943 spin_lock_init(&ptp->evt_lock);
944 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
945 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
Laurence Evansf3211602013-01-28 14:51:17 +0000946 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100947
948 ptp->phc_clock_info.owner = THIS_MODULE;
949 snprintf(ptp->phc_clock_info.name,
950 sizeof(ptp->phc_clock_info.name),
951 "%pm", efx->net_dev->perm_addr);
952 ptp->phc_clock_info.max_adj = MAX_PPB;
953 ptp->phc_clock_info.n_alarm = 0;
954 ptp->phc_clock_info.n_ext_ts = 0;
955 ptp->phc_clock_info.n_per_out = 0;
956 ptp->phc_clock_info.pps = 1;
957 ptp->phc_clock_info.adjfreq = efx_phc_adjfreq;
958 ptp->phc_clock_info.adjtime = efx_phc_adjtime;
959 ptp->phc_clock_info.gettime = efx_phc_gettime;
960 ptp->phc_clock_info.settime = efx_phc_settime;
961 ptp->phc_clock_info.enable = efx_phc_enable;
962
Richard Cochran1ef76152012-09-22 07:02:03 +0000963 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
964 &efx->pci_dev->dev);
Wei Yongjun155d9402013-05-07 02:19:25 +0000965 if (IS_ERR(ptp->phc_clock)) {
966 rc = PTR_ERR(ptp->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100967 goto fail3;
Wei Yongjun155d9402013-05-07 02:19:25 +0000968 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100969
970 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
971 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
972 if (!ptp->pps_workwq) {
973 rc = -ENOMEM;
974 goto fail4;
975 }
976 ptp->nic_ts_enabled = false;
977
978 return 0;
979fail4:
980 ptp_clock_unregister(efx->ptp_data->phc_clock);
981
982fail3:
983 destroy_workqueue(efx->ptp_data->workwq);
984
985fail2:
986 efx_nic_free_buffer(efx, &ptp->start);
987
988fail1:
989 kfree(efx->ptp_data);
990 efx->ptp_data = NULL;
991
992 return rc;
993}
994
995static void efx_ptp_remove_channel(struct efx_channel *channel)
996{
997 struct efx_nic *efx = channel->efx;
998
999 if (!efx->ptp_data)
1000 return;
1001
1002 (void)efx_ptp_disable(channel->efx);
1003
1004 cancel_work_sync(&efx->ptp_data->work);
1005 cancel_work_sync(&efx->ptp_data->pps_work);
1006
1007 skb_queue_purge(&efx->ptp_data->rxq);
1008 skb_queue_purge(&efx->ptp_data->txq);
1009
1010 ptp_clock_unregister(efx->ptp_data->phc_clock);
1011
1012 destroy_workqueue(efx->ptp_data->workwq);
1013 destroy_workqueue(efx->ptp_data->pps_workwq);
1014
1015 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1016 kfree(efx->ptp_data);
1017}
1018
1019static void efx_ptp_get_channel_name(struct efx_channel *channel,
1020 char *buf, size_t len)
1021{
1022 snprintf(buf, len, "%s-ptp", channel->efx->name);
1023}
1024
1025/* Determine whether this packet should be processed by the PTP module
1026 * or transmitted conventionally.
1027 */
1028bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1029{
1030 return efx->ptp_data &&
1031 efx->ptp_data->enabled &&
1032 skb->len >= PTP_MIN_LENGTH &&
1033 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1034 likely(skb->protocol == htons(ETH_P_IP)) &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001035 skb_transport_header_was_set(skb) &&
1036 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001037 ip_hdr(skb)->protocol == IPPROTO_UDP &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001038 skb_headlen(skb) >=
1039 skb_transport_offset(skb) + sizeof(struct udphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001040 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1041}
1042
1043/* Receive a PTP packet. Packets are queued until the arrival of
1044 * the receive timestamp from the MC - this will probably occur after the
1045 * packet arrival because of the processing in the MC.
1046 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001047static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001048{
1049 struct efx_nic *efx = channel->efx;
1050 struct efx_ptp_data *ptp = efx->ptp_data;
1051 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001052 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001053 unsigned int version;
1054
1055 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1056
1057 /* Correct version? */
1058 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001059 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001060 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001061 }
1062 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1063 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001064 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001065 }
Laurence Evansc939a312012-11-15 10:56:07 +00001066
1067 /* PTP V1 uses all six bytes of the UUID to match the packet
1068 * to the timestamp
1069 */
1070 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1071 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001072 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001073 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001074 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001075 }
1076 version = skb->data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001077 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001078 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001079 }
Laurence Evansc939a312012-11-15 10:56:07 +00001080
1081 /* The original V2 implementation uses bytes 2-7 of
1082 * the UUID to match the packet to the timestamp. This
1083 * discards two of the bytes of the MAC address used
1084 * to create the UUID (SF bug 33070). The PTP V2
1085 * enhanced mode fixes this issue and uses bytes 0-2
1086 * and byte 5-7 of the UUID.
1087 */
1088 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1089 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1090 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1091 } else {
1092 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1093 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1094 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001095 }
1096
1097 /* Does this packet require timestamping? */
1098 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1099 struct skb_shared_hwtstamps *timestamps;
1100
1101 match->state = PTP_PACKET_STATE_UNMATCHED;
1102
1103 /* Clear all timestamps held: filled in later */
1104 timestamps = skb_hwtstamps(skb);
1105 memset(timestamps, 0, sizeof(*timestamps));
1106
Laurence Evansc939a312012-11-15 10:56:07 +00001107 /* We expect the sequence number to be in the same position in
1108 * the packet for PTP V1 and V2
1109 */
1110 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1111 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1112
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001113 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001114 match->words[0] = (match_data_012[0] |
1115 (match_data_012[1] << 8) |
1116 (match_data_012[2] << 16) |
1117 (match_data_345[0] << 24));
1118 match->words[1] = (match_data_345[1] |
1119 (match_data_345[2] << 8) |
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001120 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1121 PTP_V1_SEQUENCE_LENGTH - 1] <<
1122 16));
1123 } else {
1124 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1125 }
1126
1127 skb_queue_tail(&ptp->rxq, skb);
1128 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001129
1130 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001131}
1132
1133/* Transmit a PTP packet. This has to be transmitted by the MC
1134 * itself, through an MCDI call. MCDI calls aren't permitted
1135 * in the transmit path so defer the actual transmission to a suitable worker.
1136 */
1137int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1138{
1139 struct efx_ptp_data *ptp = efx->ptp_data;
1140
1141 skb_queue_tail(&ptp->txq, skb);
1142
1143 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1144 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1145 efx_xmit_hwtstamp_pending(skb);
1146 queue_work(ptp->workwq, &ptp->work);
1147
1148 return NETDEV_TX_OK;
1149}
1150
1151static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1152 unsigned int new_mode)
1153{
1154 if ((enable_wanted != efx->ptp_data->enabled) ||
1155 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001156 int rc = 0;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001157
1158 if (enable_wanted) {
1159 /* Change of mode requires disable */
1160 if (efx->ptp_data->enabled &&
1161 (efx->ptp_data->mode != new_mode)) {
1162 efx->ptp_data->enabled = false;
1163 rc = efx_ptp_stop(efx);
1164 if (rc != 0)
1165 return rc;
1166 }
1167
1168 /* Set new operating mode and establish
1169 * baseline synchronisation, which must
1170 * succeed.
1171 */
1172 efx->ptp_data->mode = new_mode;
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001173 if (netif_running(efx->net_dev))
1174 rc = efx_ptp_start(efx);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001175 if (rc == 0) {
1176 rc = efx_ptp_synchronize(efx,
1177 PTP_SYNC_ATTEMPTS * 2);
1178 if (rc != 0)
1179 efx_ptp_stop(efx);
1180 }
1181 } else {
1182 rc = efx_ptp_stop(efx);
1183 }
1184
1185 if (rc != 0)
1186 return rc;
1187
1188 efx->ptp_data->enabled = enable_wanted;
1189 }
1190
1191 return 0;
1192}
1193
1194static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1195{
1196 bool enable_wanted = false;
1197 unsigned int new_mode;
1198 int rc;
1199
1200 if (init->flags)
1201 return -EINVAL;
1202
1203 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1204 (init->tx_type != HWTSTAMP_TX_ON))
1205 return -ERANGE;
1206
1207 new_mode = efx->ptp_data->mode;
1208 /* Determine whether any PTP HW operations are required */
1209 switch (init->rx_filter) {
1210 case HWTSTAMP_FILTER_NONE:
1211 break;
1212 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1213 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1214 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1215 init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1216 new_mode = MC_CMD_PTP_MODE_V1;
1217 enable_wanted = true;
1218 break;
1219 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1220 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1221 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1222 /* Although these three are accepted only IPV4 packets will be
1223 * timestamped
1224 */
1225 init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
Laurence Evansc939a312012-11-15 10:56:07 +00001226 new_mode = MC_CMD_PTP_MODE_V2_ENHANCED;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001227 enable_wanted = true;
1228 break;
1229 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1230 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1231 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1232 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1233 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1234 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1235 /* Non-IP + IPv6 timestamping not supported */
1236 return -ERANGE;
1237 break;
1238 default:
1239 return -ERANGE;
1240 }
1241
1242 if (init->tx_type != HWTSTAMP_TX_OFF)
1243 enable_wanted = true;
1244
Laurence Evansc939a312012-11-15 10:56:07 +00001245 /* Old versions of the firmware do not support the improved
1246 * UUID filtering option (SF bug 33070). If the firmware does
1247 * not accept the enhanced mode, fall back to the standard PTP
1248 * v2 UUID filtering.
1249 */
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001250 rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
Laurence Evansc939a312012-11-15 10:56:07 +00001251 if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED))
1252 rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001253 if (rc != 0)
1254 return rc;
1255
1256 efx->ptp_data->config = *init;
1257
1258 return 0;
1259}
1260
Ben Hutchings62ebac92013-04-08 17:34:58 +01001261void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001262{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001263 struct efx_ptp_data *ptp = efx->ptp_data;
1264
1265 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001266 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001267
Ben Hutchings62ebac92013-04-08 17:34:58 +01001268 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1269 SOF_TIMESTAMPING_RX_HARDWARE |
1270 SOF_TIMESTAMPING_RAW_HARDWARE);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001271 ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
1272 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1273 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
1274 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
1275 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
1276 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
1277 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
1278 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
1279 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001280}
1281
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001282int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001283{
1284 struct hwtstamp_config config;
1285 int rc;
1286
1287 /* Not a PTP enabled port */
1288 if (!efx->ptp_data)
1289 return -EOPNOTSUPP;
1290
1291 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1292 return -EFAULT;
1293
1294 rc = efx_ptp_ts_init(efx, &config);
1295 if (rc != 0)
1296 return rc;
1297
1298 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1299 ? -EFAULT : 0;
1300}
1301
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001302int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1303{
1304 if (!efx->ptp_data)
1305 return -EOPNOTSUPP;
1306
1307 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1308 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1309}
1310
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001311static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1312{
1313 struct efx_ptp_data *ptp = efx->ptp_data;
1314
1315 netif_err(efx, hw, efx->net_dev,
1316 "PTP unexpected event length: got %d expected %d\n",
1317 ptp->evt_frag_idx, expected_frag_len);
1318 ptp->reset_required = true;
1319 queue_work(ptp->workwq, &ptp->work);
1320}
1321
1322/* Process a completed receive event. Put it on the event queue and
1323 * start worker thread. This is required because event and their
1324 * correspoding packets may come in either order.
1325 */
1326static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1327{
1328 struct efx_ptp_event_rx *evt = NULL;
1329
1330 if (ptp->evt_frag_idx != 3) {
1331 ptp_event_failure(efx, 3);
1332 return;
1333 }
1334
1335 spin_lock_bh(&ptp->evt_lock);
1336 if (!list_empty(&ptp->evt_free_list)) {
1337 evt = list_first_entry(&ptp->evt_free_list,
1338 struct efx_ptp_event_rx, link);
1339 list_del(&evt->link);
1340
1341 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1342 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1343 MCDI_EVENT_SRC) |
1344 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1345 MCDI_EVENT_SRC) << 8) |
1346 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1347 MCDI_EVENT_SRC) << 16));
1348 evt->hwtimestamp = ktime_set(
1349 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1350 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA));
1351 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1352 list_add_tail(&evt->link, &ptp->evt_list);
1353
1354 queue_work(ptp->workwq, &ptp->work);
Laurence Evansf3211602013-01-28 14:51:17 +00001355 } else if (!ptp->evt_overflow) {
1356 /* Log a warning message and set the event overflow flag.
1357 * The message won't be logged again until the event queue
1358 * becomes empty.
1359 */
1360 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1361 ptp->evt_overflow = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001362 }
1363 spin_unlock_bh(&ptp->evt_lock);
1364}
1365
1366static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1367{
1368 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1369 if (ptp->evt_frag_idx != 1) {
1370 ptp_event_failure(efx, 1);
1371 return;
1372 }
1373
1374 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1375}
1376
1377static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1378{
1379 if (ptp->nic_ts_enabled)
1380 queue_work(ptp->pps_workwq, &ptp->pps_work);
1381}
1382
1383void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1384{
1385 struct efx_ptp_data *ptp = efx->ptp_data;
1386 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1387
1388 if (!ptp->enabled)
1389 return;
1390
1391 if (ptp->evt_frag_idx == 0) {
1392 ptp->evt_code = code;
1393 } else if (ptp->evt_code != code) {
1394 netif_err(efx, hw, efx->net_dev,
1395 "PTP out of sequence event %d\n", code);
1396 ptp->evt_frag_idx = 0;
1397 }
1398
1399 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1400 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1401 /* Process resulting event */
1402 switch (code) {
1403 case MCDI_EVENT_CODE_PTP_RX:
1404 ptp_event_rx(efx, ptp);
1405 break;
1406 case MCDI_EVENT_CODE_PTP_FAULT:
1407 ptp_event_fault(efx, ptp);
1408 break;
1409 case MCDI_EVENT_CODE_PTP_PPS:
1410 ptp_event_pps(efx, ptp);
1411 break;
1412 default:
1413 netif_err(efx, hw, efx->net_dev,
1414 "PTP unknown event %d\n", code);
1415 break;
1416 }
1417 ptp->evt_frag_idx = 0;
1418 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1419 netif_err(efx, hw, efx->net_dev,
1420 "PTP too many event fragments\n");
1421 ptp->evt_frag_idx = 0;
1422 }
1423}
1424
1425static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1426{
1427 struct efx_ptp_data *ptp_data = container_of(ptp,
1428 struct efx_ptp_data,
1429 phc_clock_info);
1430 struct efx_nic *efx = ptp_data->channel->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001431 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001432 s64 adjustment_ns;
1433 int rc;
1434
1435 if (delta > MAX_PPB)
1436 delta = MAX_PPB;
1437 else if (delta < -MAX_PPB)
1438 delta = -MAX_PPB;
1439
1440 /* Convert ppb to fixed point ns. */
1441 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1442 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1443
1444 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001445 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001446 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001447 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1448 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1449 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1450 NULL, 0, NULL);
1451 if (rc != 0)
1452 return rc;
1453
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001454 ptp_data->current_adjfreq = adjustment_ns;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001455 return 0;
1456}
1457
1458static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1459{
1460 struct efx_ptp_data *ptp_data = container_of(ptp,
1461 struct efx_ptp_data,
1462 phc_clock_info);
1463 struct efx_nic *efx = ptp_data->channel->efx;
1464 struct timespec delta_ts = ns_to_timespec(delta);
Ben Hutchings59cfc472012-09-14 17:30:10 +01001465 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001466
1467 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001468 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingscd6fe652013-12-05 17:24:06 +00001469 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001470 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_SECONDS, (u32)delta_ts.tv_sec);
1471 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_NANOSECONDS, (u32)delta_ts.tv_nsec);
1472 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1473 NULL, 0, NULL);
1474}
1475
1476static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1477{
1478 struct efx_ptp_data *ptp_data = container_of(ptp,
1479 struct efx_ptp_data,
1480 phc_clock_info);
1481 struct efx_nic *efx = ptp_data->channel->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001482 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1483 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001484 int rc;
1485
1486 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001487 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001488
1489 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1490 outbuf, sizeof(outbuf), NULL);
1491 if (rc != 0)
1492 return rc;
1493
1494 ts->tv_sec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_SECONDS);
1495 ts->tv_nsec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_NANOSECONDS);
1496 return 0;
1497}
1498
1499static int efx_phc_settime(struct ptp_clock_info *ptp,
1500 const struct timespec *e_ts)
1501{
1502 /* Get the current NIC time, efx_phc_gettime.
1503 * Subtract from the desired time to get the offset
1504 * call efx_phc_adjtime with the offset
1505 */
1506 int rc;
1507 struct timespec time_now;
1508 struct timespec delta;
1509
1510 rc = efx_phc_gettime(ptp, &time_now);
1511 if (rc != 0)
1512 return rc;
1513
1514 delta = timespec_sub(*e_ts, time_now);
1515
Julia Lawall56567c62013-01-21 03:02:48 +00001516 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001517 if (rc != 0)
1518 return rc;
1519
1520 return 0;
1521}
1522
1523static int efx_phc_enable(struct ptp_clock_info *ptp,
1524 struct ptp_clock_request *request,
1525 int enable)
1526{
1527 struct efx_ptp_data *ptp_data = container_of(ptp,
1528 struct efx_ptp_data,
1529 phc_clock_info);
1530 if (request->type != PTP_CLK_REQ_PPS)
1531 return -EOPNOTSUPP;
1532
1533 ptp_data->nic_ts_enabled = !!enable;
1534 return 0;
1535}
1536
1537static const struct efx_channel_type efx_ptp_channel_type = {
1538 .handle_no_channel = efx_ptp_handle_no_channel,
1539 .pre_probe = efx_ptp_probe_channel,
1540 .post_remove = efx_ptp_remove_channel,
1541 .get_name = efx_ptp_get_channel_name,
1542 /* no copy operation; there is no need to reallocate this channel */
1543 .receive_skb = efx_ptp_rx,
1544 .keep_eventq = false,
1545};
1546
1547void efx_ptp_probe(struct efx_nic *efx)
1548{
1549 /* Check whether PTP is implemented on this NIC. The DISABLE
1550 * operation will succeed if and only if it is implemented.
1551 */
1552 if (efx_ptp_disable(efx) == 0)
1553 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1554 &efx_ptp_channel_type;
1555}
Alexandre Rames2ea4dc22013-11-08 10:20:31 +00001556
1557void efx_ptp_start_datapath(struct efx_nic *efx)
1558{
1559 if (efx_ptp_restart(efx))
1560 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
1561}
1562
1563void efx_ptp_stop_datapath(struct efx_nic *efx)
1564{
1565 efx_ptp_stop(efx);
1566}