blob: 8b2cf783217cacd2d035545b90acd5712f4d2bc6 [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);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100316
317 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000318 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100319 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
320 efx->ptp_data->channel->channel);
321 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
322
323 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
324 NULL, 0, NULL);
325}
326
327/* Disable MCDI PTP support.
328 *
329 * Note that this function should never rely on the presence of ptp_data -
330 * may be called before that exists.
331 */
332static int efx_ptp_disable(struct efx_nic *efx)
333{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100334 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100335
336 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000337 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100338 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
339 NULL, 0, NULL);
340}
341
342static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
343{
344 struct sk_buff *skb;
345
346 while ((skb = skb_dequeue(q))) {
347 local_bh_disable();
348 netif_receive_skb(skb);
349 local_bh_enable();
350 }
351}
352
353static void efx_ptp_handle_no_channel(struct efx_nic *efx)
354{
355 netif_err(efx, drv, efx->net_dev,
356 "ERROR: PTP requires MSI-X and 1 additional interrupt"
357 "vector. PTP disabled\n");
358}
359
360/* Repeatedly send the host time to the MC which will capture the hardware
361 * time.
362 */
363static void efx_ptp_send_times(struct efx_nic *efx,
364 struct pps_event_time *last_time)
365{
366 struct pps_event_time now;
367 struct timespec limit;
368 struct efx_ptp_data *ptp = efx->ptp_data;
369 struct timespec start;
370 int *mc_running = ptp->start.addr;
371
372 pps_get_ts(&now);
373 start = now.ts_real;
374 limit = now.ts_real;
375 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
376
377 /* Write host time for specified period or until MC is done */
378 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
379 ACCESS_ONCE(*mc_running)) {
380 struct timespec update_time;
381 unsigned int host_time;
382
383 /* Don't update continuously to avoid saturating the PCIe bus */
384 update_time = now.ts_real;
385 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
386 do {
387 pps_get_ts(&now);
388 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
389 ACCESS_ONCE(*mc_running));
390
391 /* Synchronise NIC with single word of time only */
392 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
393 now.ts_real.tv_nsec);
394 /* Update host time in NIC memory */
Laurence Evans977a5d52013-03-07 11:46:58 +0000395 efx->type->ptp_write_host_time(efx, host_time);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100396 }
397 *last_time = now;
398}
399
400/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100401static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
402 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100403{
404 unsigned start_ns, end_ns;
405
406 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
407 timeset->seconds = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_SECONDS);
408 timeset->nanoseconds = MCDI_DWORD(data,
409 PTP_OUT_SYNCHRONIZE_NANOSECONDS);
410 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
411 timeset->waitns = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
412
413 /* Ignore seconds */
414 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
415 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
416 /* Allow for rollover */
417 if (end_ns < start_ns)
418 end_ns += NSEC_PER_SEC;
419 /* Determine duration of operation */
420 timeset->window = end_ns - start_ns;
421}
422
423/* Process times received from MC.
424 *
425 * Extract times from returned results, and establish the minimum value
426 * seen. The minimum value represents the "best" possible time and events
427 * too much greater than this are rejected - the machine is, perhaps, too
428 * busy. A number of readings are taken so that, hopefully, at least one good
429 * synchronisation will be seen in the results.
430 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100431static int
432efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
433 size_t response_length,
434 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100435{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100436 unsigned number_readings =
437 MCDI_VAR_ARRAY_LEN(response_length,
438 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100439 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100440 unsigned total;
441 unsigned ngood = 0;
442 unsigned last_good = 0;
443 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100444 u32 last_sec;
445 u32 start_sec;
446 struct timespec delta;
447
448 if (number_readings == 0)
449 return -EAGAIN;
450
Laurence Evans92304512013-02-11 13:55:08 +0000451 /* Read the set of results and increment stats for any results that
452 * appera to be erroneous.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100453 */
454 for (i = 0; i < number_readings; i++) {
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100455 efx_ptp_read_timeset(
456 MCDI_ARRAY_STRUCT_PTR(synch_buf,
457 PTP_OUT_SYNCHRONIZE_TIMESET, i),
458 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100459 }
460
Laurence Evans92304512013-02-11 13:55:08 +0000461 /* Find the last good host-MC synchronization result. The MC times
462 * when it finishes reading the host time so the corrected window time
463 * should be fairly constant for a given platform.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100464 */
465 total = 0;
466 for (i = 0; i < number_readings; i++)
467 if (ptp->timeset[i].window > ptp->timeset[i].waitns) {
468 unsigned win;
469
470 win = ptp->timeset[i].window - ptp->timeset[i].waitns;
471 if (win >= MIN_SYNCHRONISATION_NS &&
472 win < MAX_SYNCHRONISATION_NS) {
473 total += ptp->timeset[i].window;
474 ngood++;
475 last_good = i;
476 }
477 }
478
479 if (ngood == 0) {
480 netif_warn(efx, drv, efx->net_dev,
Laurence Evans92304512013-02-11 13:55:08 +0000481 "PTP no suitable synchronisations %dns\n",
482 ptp->base_sync_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100483 return -EAGAIN;
484 }
485
486 /* Average minimum this synchronisation */
487 ptp->last_sync_ns = DIV_ROUND_UP(total, ngood);
488 if (!ptp->base_sync_valid || (ptp->last_sync_ns < ptp->base_sync_ns)) {
489 ptp->base_sync_valid = true;
490 ptp->base_sync_ns = ptp->last_sync_ns;
491 }
492
493 /* Calculate delay from actual PPS to last_time */
494 delta.tv_nsec =
495 ptp->timeset[last_good].nanoseconds +
496 last_time->ts_real.tv_nsec -
497 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
498
499 /* It is possible that the seconds rolled over between taking
500 * the start reading and the last value written by the host. The
501 * timescales are such that a gap of more than one second is never
502 * expected.
503 */
504 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
505 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
506 if (start_sec != last_sec) {
507 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
508 netif_warn(efx, hw, efx->net_dev,
509 "PTP bad synchronisation seconds\n");
510 return -EAGAIN;
511 } else {
512 delta.tv_sec = 1;
513 }
514 } else {
515 delta.tv_sec = 0;
516 }
517
518 ptp->host_time_pps = *last_time;
519 pps_sub_ts(&ptp->host_time_pps, delta);
520
521 return 0;
522}
523
524/* Synchronize times between the host and the MC */
525static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
526{
527 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100528 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100529 size_t response_length;
530 int rc;
531 unsigned long timeout;
532 struct pps_event_time last_time = {};
533 unsigned int loops = 0;
534 int *start = ptp->start.addr;
535
536 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000537 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100538 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
539 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100540 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
541 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100542
543 /* Clear flag that signals MC ready */
544 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100545 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
546 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
547 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100548
549 /* Wait for start from MCDI (or timeout) */
550 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
551 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
552 udelay(20); /* Usually start MCDI execution quickly */
553 loops++;
554 }
555
556 if (ACCESS_ONCE(*start))
557 efx_ptp_send_times(efx, &last_time);
558
559 /* Collect results */
560 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
561 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
562 synch_buf, sizeof(synch_buf),
563 &response_length);
564 if (rc == 0)
565 rc = efx_ptp_process_times(efx, synch_buf, response_length,
566 &last_time);
567
568 return rc;
569}
570
571/* Transmit a PTP packet, via the MCDI interface, to the wire. */
572static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
573{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100574 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100575 struct skb_shared_hwtstamps timestamps;
576 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100577 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100578 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100579
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100580 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000581 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100582 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100583 if (skb_shinfo(skb)->nr_frags != 0) {
584 rc = skb_linearize(skb);
585 if (rc != 0)
586 goto fail;
587 }
588
589 if (skb->ip_summed == CHECKSUM_PARTIAL) {
590 rc = skb_checksum_help(skb);
591 if (rc != 0)
592 goto fail;
593 }
594 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100595 MCDI_PTR(ptp_data->txbuf,
596 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100597 skb->len);
598 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
599 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
600 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100601 if (rc != 0)
602 goto fail;
603
604 memset(&timestamps, 0, sizeof(timestamps));
605 timestamps.hwtstamp = ktime_set(
606 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_SECONDS),
607 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_NANOSECONDS));
608
609 skb_tstamp_tx(skb, &timestamps);
610
611 rc = 0;
612
613fail:
614 dev_kfree_skb(skb);
615
616 return rc;
617}
618
619static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
620{
621 struct efx_ptp_data *ptp = efx->ptp_data;
622 struct list_head *cursor;
623 struct list_head *next;
624
625 /* Drop time-expired events */
626 spin_lock_bh(&ptp->evt_lock);
627 if (!list_empty(&ptp->evt_list)) {
628 list_for_each_safe(cursor, next, &ptp->evt_list) {
629 struct efx_ptp_event_rx *evt;
630
631 evt = list_entry(cursor, struct efx_ptp_event_rx,
632 link);
633 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000634 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100635 netif_warn(efx, hw, efx->net_dev,
636 "PTP rx event dropped\n");
637 }
638 }
639 }
Laurence Evansf3211602013-01-28 14:51:17 +0000640 /* If the event overflow flag is set and the event list is now empty
641 * clear the flag to re-enable the overflow warning message.
642 */
643 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
644 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100645 spin_unlock_bh(&ptp->evt_lock);
646}
647
648static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
649 struct sk_buff *skb)
650{
651 struct efx_ptp_data *ptp = efx->ptp_data;
652 bool evts_waiting;
653 struct list_head *cursor;
654 struct list_head *next;
655 struct efx_ptp_match *match;
656 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
657
658 spin_lock_bh(&ptp->evt_lock);
659 evts_waiting = !list_empty(&ptp->evt_list);
660 spin_unlock_bh(&ptp->evt_lock);
661
662 if (!evts_waiting)
663 return PTP_PACKET_STATE_UNMATCHED;
664
665 match = (struct efx_ptp_match *)skb->cb;
666 /* Look for a matching timestamp in the event queue */
667 spin_lock_bh(&ptp->evt_lock);
668 list_for_each_safe(cursor, next, &ptp->evt_list) {
669 struct efx_ptp_event_rx *evt;
670
671 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
672 if ((evt->seq0 == match->words[0]) &&
673 (evt->seq1 == match->words[1])) {
674 struct skb_shared_hwtstamps *timestamps;
675
676 /* Match - add in hardware timestamp */
677 timestamps = skb_hwtstamps(skb);
678 timestamps->hwtstamp = evt->hwtimestamp;
679
680 match->state = PTP_PACKET_STATE_MATCHED;
681 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000682 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100683 break;
684 }
685 }
Laurence Evansf3211602013-01-28 14:51:17 +0000686 /* If the event overflow flag is set and the event list is now empty
687 * clear the flag to re-enable the overflow warning message.
688 */
689 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
690 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100691 spin_unlock_bh(&ptp->evt_lock);
692
693 return rc;
694}
695
696/* Process any queued receive events and corresponding packets
697 *
698 * q is returned with all the packets that are ready for delivery.
699 * true is returned if at least one of those packets requires
700 * synchronisation.
701 */
702static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
703{
704 struct efx_ptp_data *ptp = efx->ptp_data;
705 bool rc = false;
706 struct sk_buff *skb;
707
708 while ((skb = skb_dequeue(&ptp->rxq))) {
709 struct efx_ptp_match *match;
710
711 match = (struct efx_ptp_match *)skb->cb;
712 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
713 __skb_queue_tail(q, skb);
714 } else if (efx_ptp_match_rx(efx, skb) ==
715 PTP_PACKET_STATE_MATCHED) {
716 rc = true;
717 __skb_queue_tail(q, skb);
718 } else if (time_after(jiffies, match->expiry)) {
719 match->state = PTP_PACKET_STATE_TIMED_OUT;
720 netif_warn(efx, rx_err, efx->net_dev,
721 "PTP packet - no timestamp seen\n");
722 __skb_queue_tail(q, skb);
723 } else {
724 /* Replace unprocessed entry and stop */
725 skb_queue_head(&ptp->rxq, skb);
726 break;
727 }
728 }
729
730 return rc;
731}
732
733/* Complete processing of a received packet */
734static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
735{
736 local_bh_disable();
737 netif_receive_skb(skb);
738 local_bh_enable();
739}
740
741static int efx_ptp_start(struct efx_nic *efx)
742{
743 struct efx_ptp_data *ptp = efx->ptp_data;
744 struct efx_filter_spec rxfilter;
745 int rc;
746
747 ptp->reset_required = false;
748
749 /* Must filter on both event and general ports to ensure
750 * that there is no packet re-ordering.
751 */
752 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
753 efx_rx_queue_index(
754 efx_channel_get_rx_queue(ptp->channel)));
755 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
756 htonl(PTP_ADDRESS),
757 htons(PTP_EVENT_PORT));
758 if (rc != 0)
759 return rc;
760
761 rc = efx_filter_insert_filter(efx, &rxfilter, true);
762 if (rc < 0)
763 return rc;
764 ptp->rxfilter_event = rc;
765
766 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
767 efx_rx_queue_index(
768 efx_channel_get_rx_queue(ptp->channel)));
769 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
770 htonl(PTP_ADDRESS),
771 htons(PTP_GENERAL_PORT));
772 if (rc != 0)
773 goto fail;
774
775 rc = efx_filter_insert_filter(efx, &rxfilter, true);
776 if (rc < 0)
777 goto fail;
778 ptp->rxfilter_general = rc;
779
780 rc = efx_ptp_enable(efx);
781 if (rc != 0)
782 goto fail2;
783
784 ptp->evt_frag_idx = 0;
785 ptp->current_adjfreq = 0;
786 ptp->rxfilter_installed = true;
787
788 return 0;
789
790fail2:
791 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
792 ptp->rxfilter_general);
793fail:
794 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
795 ptp->rxfilter_event);
796
797 return rc;
798}
799
800static int efx_ptp_stop(struct efx_nic *efx)
801{
802 struct efx_ptp_data *ptp = efx->ptp_data;
803 int rc = efx_ptp_disable(efx);
804 struct list_head *cursor;
805 struct list_head *next;
806
807 if (ptp->rxfilter_installed) {
808 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
809 ptp->rxfilter_general);
810 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
811 ptp->rxfilter_event);
812 ptp->rxfilter_installed = false;
813 }
814
815 /* Make sure RX packets are really delivered */
816 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
817 skb_queue_purge(&efx->ptp_data->txq);
818
819 /* Drop any pending receive events */
820 spin_lock_bh(&efx->ptp_data->evt_lock);
821 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000822 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100823 }
Laurence Evansf3211602013-01-28 14:51:17 +0000824 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100825 spin_unlock_bh(&efx->ptp_data->evt_lock);
826
827 return rc;
828}
829
830static void efx_ptp_pps_worker(struct work_struct *work)
831{
832 struct efx_ptp_data *ptp =
833 container_of(work, struct efx_ptp_data, pps_work);
834 struct efx_nic *efx = ptp->channel->efx;
835 struct ptp_clock_event ptp_evt;
836
837 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
838 return;
839
840 ptp_evt.type = PTP_CLOCK_PPSUSR;
841 ptp_evt.pps_times = ptp->host_time_pps;
842 ptp_clock_event(ptp->phc_clock, &ptp_evt);
843}
844
845/* Process any pending transmissions and timestamp any received packets.
846 */
847static void efx_ptp_worker(struct work_struct *work)
848{
849 struct efx_ptp_data *ptp_data =
850 container_of(work, struct efx_ptp_data, work);
851 struct efx_nic *efx = ptp_data->channel->efx;
852 struct sk_buff *skb;
853 struct sk_buff_head tempq;
854
855 if (ptp_data->reset_required) {
856 efx_ptp_stop(efx);
857 efx_ptp_start(efx);
858 return;
859 }
860
861 efx_ptp_drop_time_expired_events(efx);
862
863 __skb_queue_head_init(&tempq);
864 if (efx_ptp_process_events(efx, &tempq) ||
865 !skb_queue_empty(&ptp_data->txq)) {
866
867 while ((skb = skb_dequeue(&ptp_data->txq)))
868 efx_ptp_xmit_skb(efx, skb);
869 }
870
871 while ((skb = __skb_dequeue(&tempq)))
872 efx_ptp_process_rx(efx, skb);
873}
874
875/* Initialise PTP channel and state.
876 *
877 * Setting core_index to zero causes the queue to be initialised and doesn't
878 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
879 */
880static int efx_ptp_probe_channel(struct efx_channel *channel)
881{
882 struct efx_nic *efx = channel->efx;
883 struct efx_ptp_data *ptp;
884 int rc = 0;
885 unsigned int pos;
886
887 channel->irq_moderation = 0;
888 channel->rx_queue.core_index = 0;
889
890 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
891 efx->ptp_data = ptp;
892 if (!efx->ptp_data)
893 return -ENOMEM;
894
Ben Hutchings0d19a542012-09-18 21:59:52 +0100895 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100896 if (rc != 0)
897 goto fail1;
898
899 ptp->channel = channel;
900 skb_queue_head_init(&ptp->rxq);
901 skb_queue_head_init(&ptp->txq);
902 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
903 if (!ptp->workwq) {
904 rc = -ENOMEM;
905 goto fail2;
906 }
907
908 INIT_WORK(&ptp->work, efx_ptp_worker);
909 ptp->config.flags = 0;
910 ptp->config.tx_type = HWTSTAMP_TX_OFF;
911 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
912 INIT_LIST_HEAD(&ptp->evt_list);
913 INIT_LIST_HEAD(&ptp->evt_free_list);
914 spin_lock_init(&ptp->evt_lock);
915 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
916 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
Laurence Evansf3211602013-01-28 14:51:17 +0000917 ptp->evt_overflow = false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100918
919 ptp->phc_clock_info.owner = THIS_MODULE;
920 snprintf(ptp->phc_clock_info.name,
921 sizeof(ptp->phc_clock_info.name),
922 "%pm", efx->net_dev->perm_addr);
923 ptp->phc_clock_info.max_adj = MAX_PPB;
924 ptp->phc_clock_info.n_alarm = 0;
925 ptp->phc_clock_info.n_ext_ts = 0;
926 ptp->phc_clock_info.n_per_out = 0;
927 ptp->phc_clock_info.pps = 1;
928 ptp->phc_clock_info.adjfreq = efx_phc_adjfreq;
929 ptp->phc_clock_info.adjtime = efx_phc_adjtime;
930 ptp->phc_clock_info.gettime = efx_phc_gettime;
931 ptp->phc_clock_info.settime = efx_phc_settime;
932 ptp->phc_clock_info.enable = efx_phc_enable;
933
Richard Cochran1ef76152012-09-22 07:02:03 +0000934 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
935 &efx->pci_dev->dev);
Wei Yongjun155d9402013-05-07 02:19:25 +0000936 if (IS_ERR(ptp->phc_clock)) {
937 rc = PTR_ERR(ptp->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100938 goto fail3;
Wei Yongjun155d9402013-05-07 02:19:25 +0000939 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100940
941 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
942 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
943 if (!ptp->pps_workwq) {
944 rc = -ENOMEM;
945 goto fail4;
946 }
947 ptp->nic_ts_enabled = false;
948
949 return 0;
950fail4:
951 ptp_clock_unregister(efx->ptp_data->phc_clock);
952
953fail3:
954 destroy_workqueue(efx->ptp_data->workwq);
955
956fail2:
957 efx_nic_free_buffer(efx, &ptp->start);
958
959fail1:
960 kfree(efx->ptp_data);
961 efx->ptp_data = NULL;
962
963 return rc;
964}
965
966static void efx_ptp_remove_channel(struct efx_channel *channel)
967{
968 struct efx_nic *efx = channel->efx;
969
970 if (!efx->ptp_data)
971 return;
972
973 (void)efx_ptp_disable(channel->efx);
974
975 cancel_work_sync(&efx->ptp_data->work);
976 cancel_work_sync(&efx->ptp_data->pps_work);
977
978 skb_queue_purge(&efx->ptp_data->rxq);
979 skb_queue_purge(&efx->ptp_data->txq);
980
981 ptp_clock_unregister(efx->ptp_data->phc_clock);
982
983 destroy_workqueue(efx->ptp_data->workwq);
984 destroy_workqueue(efx->ptp_data->pps_workwq);
985
986 efx_nic_free_buffer(efx, &efx->ptp_data->start);
987 kfree(efx->ptp_data);
988}
989
990static void efx_ptp_get_channel_name(struct efx_channel *channel,
991 char *buf, size_t len)
992{
993 snprintf(buf, len, "%s-ptp", channel->efx->name);
994}
995
996/* Determine whether this packet should be processed by the PTP module
997 * or transmitted conventionally.
998 */
999bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1000{
1001 return efx->ptp_data &&
1002 efx->ptp_data->enabled &&
1003 skb->len >= PTP_MIN_LENGTH &&
1004 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1005 likely(skb->protocol == htons(ETH_P_IP)) &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001006 skb_transport_header_was_set(skb) &&
1007 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001008 ip_hdr(skb)->protocol == IPPROTO_UDP &&
Ben Hutchingse5a498e2013-12-06 19:26:40 +00001009 skb_headlen(skb) >=
1010 skb_transport_offset(skb) + sizeof(struct udphdr) &&
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001011 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1012}
1013
1014/* Receive a PTP packet. Packets are queued until the arrival of
1015 * the receive timestamp from the MC - this will probably occur after the
1016 * packet arrival because of the processing in the MC.
1017 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001018static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001019{
1020 struct efx_nic *efx = channel->efx;
1021 struct efx_ptp_data *ptp = efx->ptp_data;
1022 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001023 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001024 unsigned int version;
1025
1026 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1027
1028 /* Correct version? */
1029 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001030 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001031 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001032 }
1033 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1034 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001035 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001036 }
Laurence Evansc939a312012-11-15 10:56:07 +00001037
1038 /* PTP V1 uses all six bytes of the UUID to match the packet
1039 * to the timestamp
1040 */
1041 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1042 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001043 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001044 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001045 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001046 }
1047 version = skb->data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001048 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001049 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001050 }
Laurence Evansc939a312012-11-15 10:56:07 +00001051
1052 /* The original V2 implementation uses bytes 2-7 of
1053 * the UUID to match the packet to the timestamp. This
1054 * discards two of the bytes of the MAC address used
1055 * to create the UUID (SF bug 33070). The PTP V2
1056 * enhanced mode fixes this issue and uses bytes 0-2
1057 * and byte 5-7 of the UUID.
1058 */
1059 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1060 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1061 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1062 } else {
1063 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1064 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1065 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001066 }
1067
1068 /* Does this packet require timestamping? */
1069 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1070 struct skb_shared_hwtstamps *timestamps;
1071
1072 match->state = PTP_PACKET_STATE_UNMATCHED;
1073
1074 /* Clear all timestamps held: filled in later */
1075 timestamps = skb_hwtstamps(skb);
1076 memset(timestamps, 0, sizeof(*timestamps));
1077
Laurence Evansc939a312012-11-15 10:56:07 +00001078 /* We expect the sequence number to be in the same position in
1079 * the packet for PTP V1 and V2
1080 */
1081 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1082 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1083
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001084 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001085 match->words[0] = (match_data_012[0] |
1086 (match_data_012[1] << 8) |
1087 (match_data_012[2] << 16) |
1088 (match_data_345[0] << 24));
1089 match->words[1] = (match_data_345[1] |
1090 (match_data_345[2] << 8) |
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001091 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1092 PTP_V1_SEQUENCE_LENGTH - 1] <<
1093 16));
1094 } else {
1095 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1096 }
1097
1098 skb_queue_tail(&ptp->rxq, skb);
1099 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001100
1101 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001102}
1103
1104/* Transmit a PTP packet. This has to be transmitted by the MC
1105 * itself, through an MCDI call. MCDI calls aren't permitted
1106 * in the transmit path so defer the actual transmission to a suitable worker.
1107 */
1108int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1109{
1110 struct efx_ptp_data *ptp = efx->ptp_data;
1111
1112 skb_queue_tail(&ptp->txq, skb);
1113
1114 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1115 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1116 efx_xmit_hwtstamp_pending(skb);
1117 queue_work(ptp->workwq, &ptp->work);
1118
1119 return NETDEV_TX_OK;
1120}
1121
1122static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1123 unsigned int new_mode)
1124{
1125 if ((enable_wanted != efx->ptp_data->enabled) ||
1126 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1127 int rc;
1128
1129 if (enable_wanted) {
1130 /* Change of mode requires disable */
1131 if (efx->ptp_data->enabled &&
1132 (efx->ptp_data->mode != new_mode)) {
1133 efx->ptp_data->enabled = false;
1134 rc = efx_ptp_stop(efx);
1135 if (rc != 0)
1136 return rc;
1137 }
1138
1139 /* Set new operating mode and establish
1140 * baseline synchronisation, which must
1141 * succeed.
1142 */
1143 efx->ptp_data->mode = new_mode;
1144 rc = efx_ptp_start(efx);
1145 if (rc == 0) {
1146 rc = efx_ptp_synchronize(efx,
1147 PTP_SYNC_ATTEMPTS * 2);
1148 if (rc != 0)
1149 efx_ptp_stop(efx);
1150 }
1151 } else {
1152 rc = efx_ptp_stop(efx);
1153 }
1154
1155 if (rc != 0)
1156 return rc;
1157
1158 efx->ptp_data->enabled = enable_wanted;
1159 }
1160
1161 return 0;
1162}
1163
1164static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1165{
1166 bool enable_wanted = false;
1167 unsigned int new_mode;
1168 int rc;
1169
1170 if (init->flags)
1171 return -EINVAL;
1172
1173 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1174 (init->tx_type != HWTSTAMP_TX_ON))
1175 return -ERANGE;
1176
1177 new_mode = efx->ptp_data->mode;
1178 /* Determine whether any PTP HW operations are required */
1179 switch (init->rx_filter) {
1180 case HWTSTAMP_FILTER_NONE:
1181 break;
1182 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1183 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1184 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1185 init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1186 new_mode = MC_CMD_PTP_MODE_V1;
1187 enable_wanted = true;
1188 break;
1189 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1190 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1191 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1192 /* Although these three are accepted only IPV4 packets will be
1193 * timestamped
1194 */
1195 init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
Laurence Evansc939a312012-11-15 10:56:07 +00001196 new_mode = MC_CMD_PTP_MODE_V2_ENHANCED;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001197 enable_wanted = true;
1198 break;
1199 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1200 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1201 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1202 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1203 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1204 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1205 /* Non-IP + IPv6 timestamping not supported */
1206 return -ERANGE;
1207 break;
1208 default:
1209 return -ERANGE;
1210 }
1211
1212 if (init->tx_type != HWTSTAMP_TX_OFF)
1213 enable_wanted = true;
1214
Laurence Evansc939a312012-11-15 10:56:07 +00001215 /* Old versions of the firmware do not support the improved
1216 * UUID filtering option (SF bug 33070). If the firmware does
1217 * not accept the enhanced mode, fall back to the standard PTP
1218 * v2 UUID filtering.
1219 */
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001220 rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
Laurence Evansc939a312012-11-15 10:56:07 +00001221 if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED))
1222 rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001223 if (rc != 0)
1224 return rc;
1225
1226 efx->ptp_data->config = *init;
1227
1228 return 0;
1229}
1230
Ben Hutchings62ebac92013-04-08 17:34:58 +01001231void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001232{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001233 struct efx_ptp_data *ptp = efx->ptp_data;
1234
1235 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001236 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001237
Ben Hutchings62ebac92013-04-08 17:34:58 +01001238 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1239 SOF_TIMESTAMPING_RX_HARDWARE |
1240 SOF_TIMESTAMPING_RAW_HARDWARE);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001241 ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
1242 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1243 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
1244 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
1245 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
1246 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
1247 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
1248 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
1249 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001250}
1251
1252int efx_ptp_ioctl(struct efx_nic *efx, struct ifreq *ifr, int cmd)
1253{
1254 struct hwtstamp_config config;
1255 int rc;
1256
1257 /* Not a PTP enabled port */
1258 if (!efx->ptp_data)
1259 return -EOPNOTSUPP;
1260
1261 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1262 return -EFAULT;
1263
1264 rc = efx_ptp_ts_init(efx, &config);
1265 if (rc != 0)
1266 return rc;
1267
1268 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1269 ? -EFAULT : 0;
1270}
1271
1272static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1273{
1274 struct efx_ptp_data *ptp = efx->ptp_data;
1275
1276 netif_err(efx, hw, efx->net_dev,
1277 "PTP unexpected event length: got %d expected %d\n",
1278 ptp->evt_frag_idx, expected_frag_len);
1279 ptp->reset_required = true;
1280 queue_work(ptp->workwq, &ptp->work);
1281}
1282
1283/* Process a completed receive event. Put it on the event queue and
1284 * start worker thread. This is required because event and their
1285 * correspoding packets may come in either order.
1286 */
1287static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1288{
1289 struct efx_ptp_event_rx *evt = NULL;
1290
1291 if (ptp->evt_frag_idx != 3) {
1292 ptp_event_failure(efx, 3);
1293 return;
1294 }
1295
1296 spin_lock_bh(&ptp->evt_lock);
1297 if (!list_empty(&ptp->evt_free_list)) {
1298 evt = list_first_entry(&ptp->evt_free_list,
1299 struct efx_ptp_event_rx, link);
1300 list_del(&evt->link);
1301
1302 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1303 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1304 MCDI_EVENT_SRC) |
1305 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1306 MCDI_EVENT_SRC) << 8) |
1307 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1308 MCDI_EVENT_SRC) << 16));
1309 evt->hwtimestamp = ktime_set(
1310 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1311 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA));
1312 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1313 list_add_tail(&evt->link, &ptp->evt_list);
1314
1315 queue_work(ptp->workwq, &ptp->work);
Laurence Evansf3211602013-01-28 14:51:17 +00001316 } else if (!ptp->evt_overflow) {
1317 /* Log a warning message and set the event overflow flag.
1318 * The message won't be logged again until the event queue
1319 * becomes empty.
1320 */
1321 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1322 ptp->evt_overflow = true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001323 }
1324 spin_unlock_bh(&ptp->evt_lock);
1325}
1326
1327static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1328{
1329 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1330 if (ptp->evt_frag_idx != 1) {
1331 ptp_event_failure(efx, 1);
1332 return;
1333 }
1334
1335 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1336}
1337
1338static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1339{
1340 if (ptp->nic_ts_enabled)
1341 queue_work(ptp->pps_workwq, &ptp->pps_work);
1342}
1343
1344void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1345{
1346 struct efx_ptp_data *ptp = efx->ptp_data;
1347 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1348
1349 if (!ptp->enabled)
1350 return;
1351
1352 if (ptp->evt_frag_idx == 0) {
1353 ptp->evt_code = code;
1354 } else if (ptp->evt_code != code) {
1355 netif_err(efx, hw, efx->net_dev,
1356 "PTP out of sequence event %d\n", code);
1357 ptp->evt_frag_idx = 0;
1358 }
1359
1360 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1361 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1362 /* Process resulting event */
1363 switch (code) {
1364 case MCDI_EVENT_CODE_PTP_RX:
1365 ptp_event_rx(efx, ptp);
1366 break;
1367 case MCDI_EVENT_CODE_PTP_FAULT:
1368 ptp_event_fault(efx, ptp);
1369 break;
1370 case MCDI_EVENT_CODE_PTP_PPS:
1371 ptp_event_pps(efx, ptp);
1372 break;
1373 default:
1374 netif_err(efx, hw, efx->net_dev,
1375 "PTP unknown event %d\n", code);
1376 break;
1377 }
1378 ptp->evt_frag_idx = 0;
1379 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1380 netif_err(efx, hw, efx->net_dev,
1381 "PTP too many event fragments\n");
1382 ptp->evt_frag_idx = 0;
1383 }
1384}
1385
1386static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1387{
1388 struct efx_ptp_data *ptp_data = container_of(ptp,
1389 struct efx_ptp_data,
1390 phc_clock_info);
1391 struct efx_nic *efx = ptp_data->channel->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001392 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001393 s64 adjustment_ns;
1394 int rc;
1395
1396 if (delta > MAX_PPB)
1397 delta = MAX_PPB;
1398 else if (delta < -MAX_PPB)
1399 delta = -MAX_PPB;
1400
1401 /* Convert ppb to fixed point ns. */
1402 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1403 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1404
1405 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001406 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001407 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001408 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1409 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1410 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1411 NULL, 0, NULL);
1412 if (rc != 0)
1413 return rc;
1414
1415 ptp_data->current_adjfreq = delta;
1416 return 0;
1417}
1418
1419static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1420{
1421 struct efx_ptp_data *ptp_data = container_of(ptp,
1422 struct efx_ptp_data,
1423 phc_clock_info);
1424 struct efx_nic *efx = ptp_data->channel->efx;
1425 struct timespec delta_ts = ns_to_timespec(delta);
Ben Hutchings59cfc472012-09-14 17:30:10 +01001426 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001427
1428 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001429 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001430 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001431 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_SECONDS, (u32)delta_ts.tv_sec);
1432 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_NANOSECONDS, (u32)delta_ts.tv_nsec);
1433 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1434 NULL, 0, NULL);
1435}
1436
1437static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1438{
1439 struct efx_ptp_data *ptp_data = container_of(ptp,
1440 struct efx_ptp_data,
1441 phc_clock_info);
1442 struct efx_nic *efx = ptp_data->channel->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001443 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1444 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001445 int rc;
1446
1447 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001448 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001449
1450 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1451 outbuf, sizeof(outbuf), NULL);
1452 if (rc != 0)
1453 return rc;
1454
1455 ts->tv_sec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_SECONDS);
1456 ts->tv_nsec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_NANOSECONDS);
1457 return 0;
1458}
1459
1460static int efx_phc_settime(struct ptp_clock_info *ptp,
1461 const struct timespec *e_ts)
1462{
1463 /* Get the current NIC time, efx_phc_gettime.
1464 * Subtract from the desired time to get the offset
1465 * call efx_phc_adjtime with the offset
1466 */
1467 int rc;
1468 struct timespec time_now;
1469 struct timespec delta;
1470
1471 rc = efx_phc_gettime(ptp, &time_now);
1472 if (rc != 0)
1473 return rc;
1474
1475 delta = timespec_sub(*e_ts, time_now);
1476
Julia Lawall56567c62013-01-21 03:02:48 +00001477 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001478 if (rc != 0)
1479 return rc;
1480
1481 return 0;
1482}
1483
1484static int efx_phc_enable(struct ptp_clock_info *ptp,
1485 struct ptp_clock_request *request,
1486 int enable)
1487{
1488 struct efx_ptp_data *ptp_data = container_of(ptp,
1489 struct efx_ptp_data,
1490 phc_clock_info);
1491 if (request->type != PTP_CLK_REQ_PPS)
1492 return -EOPNOTSUPP;
1493
1494 ptp_data->nic_ts_enabled = !!enable;
1495 return 0;
1496}
1497
1498static const struct efx_channel_type efx_ptp_channel_type = {
1499 .handle_no_channel = efx_ptp_handle_no_channel,
1500 .pre_probe = efx_ptp_probe_channel,
1501 .post_remove = efx_ptp_remove_channel,
1502 .get_name = efx_ptp_get_channel_name,
1503 /* no copy operation; there is no need to reallocate this channel */
1504 .receive_skb = efx_ptp_rx,
1505 .keep_eventq = false,
1506};
1507
1508void efx_ptp_probe(struct efx_nic *efx)
1509{
1510 /* Check whether PTP is implemented on this NIC. The DISABLE
1511 * operation will succeed if and only if it is implemented.
1512 */
1513 if (efx_ptp_disable(efx) == 0)
1514 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1515 &efx_ptp_channel_type;
1516}