blob: 4de2f8febd596234501444c16f0bfc73e6a1dc17 [file] [log] [blame]
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2011 Solarflare Communications Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation, incorporated herein by reference.
8 */
9
10/* Theory of operation:
11 *
12 * PTP support is assisted by firmware running on the MC, which provides
13 * the hardware timestamping capabilities. Both transmitted and received
14 * PTP event packets are queued onto internal queues for subsequent processing;
15 * this is because the MC operations are relatively long and would block
16 * block NAPI/interrupt operation.
17 *
18 * Receive event processing:
19 * The event contains the packet's UUID and sequence number, together
20 * with the hardware timestamp. The PTP receive packet queue is searched
21 * for this UUID/sequence number and, if found, put on a pending queue.
22 * Packets not matching are delivered without timestamps (MCDI events will
23 * always arrive after the actual packet).
24 * It is important for the operation of the PTP protocol that the ordering
25 * of packets between the event and general port is maintained.
26 *
27 * Work queue processing:
28 * If work waiting, synchronise host/hardware time
29 *
30 * Transmit: send packet through MC, which returns the transmission time
31 * that is converted to an appropriate timestamp.
32 *
33 * Receive: the packet's reception time is converted to an appropriate
34 * timestamp.
35 */
36#include <linux/ip.h>
37#include <linux/udp.h>
38#include <linux/time.h>
39#include <linux/ktime.h>
40#include <linux/module.h>
41#include <linux/net_tstamp.h>
42#include <linux/pps_kernel.h>
43#include <linux/ptp_clock_kernel.h>
44#include "net_driver.h"
45#include "efx.h"
46#include "mcdi.h"
47#include "mcdi_pcol.h"
48#include "io.h"
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
223 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
224 * @workwq: Work queue for processing pending PTP operations
225 * @work: Work task
226 * @reset_required: A serious error has occurred and the PTP task needs to be
227 * reset (disable, enable).
228 * @rxfilter_event: Receive filter when operating
229 * @rxfilter_general: Receive filter when operating
230 * @config: Current timestamp configuration
231 * @enabled: PTP operation enabled
232 * @mode: Mode in which PTP operating (PTP version)
233 * @evt_frags: Partly assembled PTP events
234 * @evt_frag_idx: Current fragment number
235 * @evt_code: Last event code
236 * @start: Address at which MC indicates ready for synchronisation
237 * @host_time_pps: Host time at last PPS
238 * @last_sync_ns: Last number of nanoseconds between readings when synchronising
239 * @base_sync_ns: Number of nanoseconds for last synchronisation.
240 * @base_sync_valid: Whether base_sync_time is valid.
241 * @current_adjfreq: Current ppb adjustment.
242 * @phc_clock: Pointer to registered phc device
243 * @phc_clock_info: Registration structure for phc device
244 * @pps_work: pps work task for handling pps events
245 * @pps_workwq: pps work queue
246 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
247 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
248 * allocations in main data path).
249 * @debug_ptp_dir: PTP debugfs directory
250 * @missed_rx_sync: Number of packets received without syncrhonisation.
251 * @good_syncs: Number of successful synchronisations.
252 * @no_time_syncs: Number of synchronisations with no good times.
253 * @bad_sync_durations: Number of synchronisations with bad durations.
254 * @bad_syncs: Number of failed synchronisations.
255 * @last_sync_time: Number of nanoseconds for last synchronisation.
256 * @sync_timeouts: Number of synchronisation timeouts
257 * @fast_syncs: Number of synchronisations requiring short delay
258 * @min_sync_delta: Minimum time between event and synchronisation
259 * @max_sync_delta: Maximum time between event and synchronisation
260 * @average_sync_delta: Average time between event and synchronisation.
261 * Modified moving average.
262 * @last_sync_delta: Last time between event and synchronisation
263 * @mc_stats: Context value for MC statistics
264 * @timeset: Last set of synchronisation statistics.
265 */
266struct efx_ptp_data {
267 struct efx_channel *channel;
268 struct sk_buff_head rxq;
269 struct sk_buff_head txq;
270 struct list_head evt_list;
271 struct list_head evt_free_list;
272 spinlock_t evt_lock;
273 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
274 struct workqueue_struct *workwq;
275 struct work_struct work;
276 bool reset_required;
277 u32 rxfilter_event;
278 u32 rxfilter_general;
279 bool rxfilter_installed;
280 struct hwtstamp_config config;
281 bool enabled;
282 unsigned int mode;
283 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
284 int evt_frag_idx;
285 int evt_code;
286 struct efx_buffer start;
287 struct pps_event_time host_time_pps;
288 unsigned last_sync_ns;
289 unsigned base_sync_ns;
290 bool base_sync_valid;
291 s64 current_adjfreq;
292 struct ptp_clock *phc_clock;
293 struct ptp_clock_info phc_clock_info;
294 struct work_struct pps_work;
295 struct workqueue_struct *pps_workwq;
296 bool nic_ts_enabled;
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100297 MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100298 struct efx_ptp_timeset
299 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
300};
301
302static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
303static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
304static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts);
305static int efx_phc_settime(struct ptp_clock_info *ptp,
306 const struct timespec *e_ts);
307static int efx_phc_enable(struct ptp_clock_info *ptp,
308 struct ptp_clock_request *request, int on);
309
310/* Enable MCDI PTP support. */
311static int efx_ptp_enable(struct efx_nic *efx)
312{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100313 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100314
315 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000316 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100317 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
318 efx->ptp_data->channel->channel);
319 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
320
321 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
322 NULL, 0, NULL);
323}
324
325/* Disable MCDI PTP support.
326 *
327 * Note that this function should never rely on the presence of ptp_data -
328 * may be called before that exists.
329 */
330static int efx_ptp_disable(struct efx_nic *efx)
331{
Ben Hutchings59cfc472012-09-14 17:30:10 +0100332 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100333
334 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000335 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100336 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
337 NULL, 0, NULL);
338}
339
340static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
341{
342 struct sk_buff *skb;
343
344 while ((skb = skb_dequeue(q))) {
345 local_bh_disable();
346 netif_receive_skb(skb);
347 local_bh_enable();
348 }
349}
350
351static void efx_ptp_handle_no_channel(struct efx_nic *efx)
352{
353 netif_err(efx, drv, efx->net_dev,
354 "ERROR: PTP requires MSI-X and 1 additional interrupt"
355 "vector. PTP disabled\n");
356}
357
358/* Repeatedly send the host time to the MC which will capture the hardware
359 * time.
360 */
361static void efx_ptp_send_times(struct efx_nic *efx,
362 struct pps_event_time *last_time)
363{
364 struct pps_event_time now;
365 struct timespec limit;
366 struct efx_ptp_data *ptp = efx->ptp_data;
367 struct timespec start;
368 int *mc_running = ptp->start.addr;
369
370 pps_get_ts(&now);
371 start = now.ts_real;
372 limit = now.ts_real;
373 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
374
375 /* Write host time for specified period or until MC is done */
376 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
377 ACCESS_ONCE(*mc_running)) {
378 struct timespec update_time;
379 unsigned int host_time;
380
381 /* Don't update continuously to avoid saturating the PCIe bus */
382 update_time = now.ts_real;
383 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
384 do {
385 pps_get_ts(&now);
386 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
387 ACCESS_ONCE(*mc_running));
388
389 /* Synchronise NIC with single word of time only */
390 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
391 now.ts_real.tv_nsec);
392 /* Update host time in NIC memory */
393 _efx_writed(efx, cpu_to_le32(host_time),
394 FR_CZ_MC_TREG_SMEM + MC_SMEM_P0_PTP_TIME_OFST);
395 }
396 *last_time = now;
397}
398
399/* Read a timeset from the MC's results and partial process. */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100400static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
401 struct efx_ptp_timeset *timeset)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100402{
403 unsigned start_ns, end_ns;
404
405 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
406 timeset->seconds = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_SECONDS);
407 timeset->nanoseconds = MCDI_DWORD(data,
408 PTP_OUT_SYNCHRONIZE_NANOSECONDS);
409 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
410 timeset->waitns = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
411
412 /* Ignore seconds */
413 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
414 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
415 /* Allow for rollover */
416 if (end_ns < start_ns)
417 end_ns += NSEC_PER_SEC;
418 /* Determine duration of operation */
419 timeset->window = end_ns - start_ns;
420}
421
422/* Process times received from MC.
423 *
424 * Extract times from returned results, and establish the minimum value
425 * seen. The minimum value represents the "best" possible time and events
426 * too much greater than this are rejected - the machine is, perhaps, too
427 * busy. A number of readings are taken so that, hopefully, at least one good
428 * synchronisation will be seen in the results.
429 */
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100430static int
431efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
432 size_t response_length,
433 const struct pps_event_time *last_time)
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100434{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100435 unsigned number_readings =
436 MCDI_VAR_ARRAY_LEN(response_length,
437 PTP_OUT_SYNCHRONIZE_TIMESET);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100438 unsigned i;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100439 unsigned total;
440 unsigned ngood = 0;
441 unsigned last_good = 0;
442 struct efx_ptp_data *ptp = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100443 u32 last_sec;
444 u32 start_sec;
445 struct timespec delta;
446
447 if (number_readings == 0)
448 return -EAGAIN;
449
Laurence Evans92304512013-02-11 13:55:08 +0000450 /* Read the set of results and increment stats for any results that
451 * appera to be erroneous.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100452 */
453 for (i = 0; i < number_readings; i++) {
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100454 efx_ptp_read_timeset(
455 MCDI_ARRAY_STRUCT_PTR(synch_buf,
456 PTP_OUT_SYNCHRONIZE_TIMESET, i),
457 &ptp->timeset[i]);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100458 }
459
Laurence Evans92304512013-02-11 13:55:08 +0000460 /* Find the last good host-MC synchronization result. The MC times
461 * when it finishes reading the host time so the corrected window time
462 * should be fairly constant for a given platform.
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100463 */
464 total = 0;
465 for (i = 0; i < number_readings; i++)
466 if (ptp->timeset[i].window > ptp->timeset[i].waitns) {
467 unsigned win;
468
469 win = ptp->timeset[i].window - ptp->timeset[i].waitns;
470 if (win >= MIN_SYNCHRONISATION_NS &&
471 win < MAX_SYNCHRONISATION_NS) {
472 total += ptp->timeset[i].window;
473 ngood++;
474 last_good = i;
475 }
476 }
477
478 if (ngood == 0) {
479 netif_warn(efx, drv, efx->net_dev,
Laurence Evans92304512013-02-11 13:55:08 +0000480 "PTP no suitable synchronisations %dns\n",
481 ptp->base_sync_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100482 return -EAGAIN;
483 }
484
485 /* Average minimum this synchronisation */
486 ptp->last_sync_ns = DIV_ROUND_UP(total, ngood);
487 if (!ptp->base_sync_valid || (ptp->last_sync_ns < ptp->base_sync_ns)) {
488 ptp->base_sync_valid = true;
489 ptp->base_sync_ns = ptp->last_sync_ns;
490 }
491
492 /* Calculate delay from actual PPS to last_time */
493 delta.tv_nsec =
494 ptp->timeset[last_good].nanoseconds +
495 last_time->ts_real.tv_nsec -
496 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
497
498 /* It is possible that the seconds rolled over between taking
499 * the start reading and the last value written by the host. The
500 * timescales are such that a gap of more than one second is never
501 * expected.
502 */
503 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
504 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
505 if (start_sec != last_sec) {
506 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
507 netif_warn(efx, hw, efx->net_dev,
508 "PTP bad synchronisation seconds\n");
509 return -EAGAIN;
510 } else {
511 delta.tv_sec = 1;
512 }
513 } else {
514 delta.tv_sec = 0;
515 }
516
517 ptp->host_time_pps = *last_time;
518 pps_sub_ts(&ptp->host_time_pps, delta);
519
520 return 0;
521}
522
523/* Synchronize times between the host and the MC */
524static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
525{
526 struct efx_ptp_data *ptp = efx->ptp_data;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100527 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100528 size_t response_length;
529 int rc;
530 unsigned long timeout;
531 struct pps_event_time last_time = {};
532 unsigned int loops = 0;
533 int *start = ptp->start.addr;
534
535 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000536 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100537 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
538 num_readings);
Ben Hutchings338f74d2012-10-10 23:20:17 +0100539 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
540 ptp->start.dma_addr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100541
542 /* Clear flag that signals MC ready */
543 ACCESS_ONCE(*start) = 0;
Ben Hutchingsdf2cd8a2012-09-19 00:56:18 +0100544 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
545 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
546 EFX_BUG_ON_PARANOID(rc);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100547
548 /* Wait for start from MCDI (or timeout) */
549 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
550 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
551 udelay(20); /* Usually start MCDI execution quickly */
552 loops++;
553 }
554
555 if (ACCESS_ONCE(*start))
556 efx_ptp_send_times(efx, &last_time);
557
558 /* Collect results */
559 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
560 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
561 synch_buf, sizeof(synch_buf),
562 &response_length);
563 if (rc == 0)
564 rc = efx_ptp_process_times(efx, synch_buf, response_length,
565 &last_time);
566
567 return rc;
568}
569
570/* Transmit a PTP packet, via the MCDI interface, to the wire. */
571static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
572{
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100573 struct efx_ptp_data *ptp_data = efx->ptp_data;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100574 struct skb_shared_hwtstamps timestamps;
575 int rc = -EIO;
Ben Hutchings59cfc472012-09-14 17:30:10 +0100576 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
Ben Hutchings9528b922012-09-14 17:31:41 +0100577 size_t len;
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100578
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100579 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
Laurence Evansc1d828b2013-03-06 15:33:17 +0000580 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100581 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100582 if (skb_shinfo(skb)->nr_frags != 0) {
583 rc = skb_linearize(skb);
584 if (rc != 0)
585 goto fail;
586 }
587
588 if (skb->ip_summed == CHECKSUM_PARTIAL) {
589 rc = skb_checksum_help(skb);
590 if (rc != 0)
591 goto fail;
592 }
593 skb_copy_from_linear_data(skb,
Ben Hutchingsc5bb0e92012-09-14 17:31:33 +0100594 MCDI_PTR(ptp_data->txbuf,
595 PTP_IN_TRANSMIT_PACKET),
Ben Hutchings9528b922012-09-14 17:31:41 +0100596 skb->len);
597 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
598 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
599 txtime, sizeof(txtime), &len);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100600 if (rc != 0)
601 goto fail;
602
603 memset(&timestamps, 0, sizeof(timestamps));
604 timestamps.hwtstamp = ktime_set(
605 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_SECONDS),
606 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_NANOSECONDS));
607
608 skb_tstamp_tx(skb, &timestamps);
609
610 rc = 0;
611
612fail:
613 dev_kfree_skb(skb);
614
615 return rc;
616}
617
618static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
619{
620 struct efx_ptp_data *ptp = efx->ptp_data;
621 struct list_head *cursor;
622 struct list_head *next;
623
624 /* Drop time-expired events */
625 spin_lock_bh(&ptp->evt_lock);
626 if (!list_empty(&ptp->evt_list)) {
627 list_for_each_safe(cursor, next, &ptp->evt_list) {
628 struct efx_ptp_event_rx *evt;
629
630 evt = list_entry(cursor, struct efx_ptp_event_rx,
631 link);
632 if (time_after(jiffies, evt->expiry)) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000633 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100634 netif_warn(efx, hw, efx->net_dev,
635 "PTP rx event dropped\n");
636 }
637 }
638 }
639 spin_unlock_bh(&ptp->evt_lock);
640}
641
642static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
643 struct sk_buff *skb)
644{
645 struct efx_ptp_data *ptp = efx->ptp_data;
646 bool evts_waiting;
647 struct list_head *cursor;
648 struct list_head *next;
649 struct efx_ptp_match *match;
650 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
651
652 spin_lock_bh(&ptp->evt_lock);
653 evts_waiting = !list_empty(&ptp->evt_list);
654 spin_unlock_bh(&ptp->evt_lock);
655
656 if (!evts_waiting)
657 return PTP_PACKET_STATE_UNMATCHED;
658
659 match = (struct efx_ptp_match *)skb->cb;
660 /* Look for a matching timestamp in the event queue */
661 spin_lock_bh(&ptp->evt_lock);
662 list_for_each_safe(cursor, next, &ptp->evt_list) {
663 struct efx_ptp_event_rx *evt;
664
665 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
666 if ((evt->seq0 == match->words[0]) &&
667 (evt->seq1 == match->words[1])) {
668 struct skb_shared_hwtstamps *timestamps;
669
670 /* Match - add in hardware timestamp */
671 timestamps = skb_hwtstamps(skb);
672 timestamps->hwtstamp = evt->hwtimestamp;
673
674 match->state = PTP_PACKET_STATE_MATCHED;
675 rc = PTP_PACKET_STATE_MATCHED;
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000676 list_move(&evt->link, &ptp->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100677 break;
678 }
679 }
680 spin_unlock_bh(&ptp->evt_lock);
681
682 return rc;
683}
684
685/* Process any queued receive events and corresponding packets
686 *
687 * q is returned with all the packets that are ready for delivery.
688 * true is returned if at least one of those packets requires
689 * synchronisation.
690 */
691static bool efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
692{
693 struct efx_ptp_data *ptp = efx->ptp_data;
694 bool rc = false;
695 struct sk_buff *skb;
696
697 while ((skb = skb_dequeue(&ptp->rxq))) {
698 struct efx_ptp_match *match;
699
700 match = (struct efx_ptp_match *)skb->cb;
701 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
702 __skb_queue_tail(q, skb);
703 } else if (efx_ptp_match_rx(efx, skb) ==
704 PTP_PACKET_STATE_MATCHED) {
705 rc = true;
706 __skb_queue_tail(q, skb);
707 } else if (time_after(jiffies, match->expiry)) {
708 match->state = PTP_PACKET_STATE_TIMED_OUT;
709 netif_warn(efx, rx_err, efx->net_dev,
710 "PTP packet - no timestamp seen\n");
711 __skb_queue_tail(q, skb);
712 } else {
713 /* Replace unprocessed entry and stop */
714 skb_queue_head(&ptp->rxq, skb);
715 break;
716 }
717 }
718
719 return rc;
720}
721
722/* Complete processing of a received packet */
723static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
724{
725 local_bh_disable();
726 netif_receive_skb(skb);
727 local_bh_enable();
728}
729
730static int efx_ptp_start(struct efx_nic *efx)
731{
732 struct efx_ptp_data *ptp = efx->ptp_data;
733 struct efx_filter_spec rxfilter;
734 int rc;
735
736 ptp->reset_required = false;
737
738 /* Must filter on both event and general ports to ensure
739 * that there is no packet re-ordering.
740 */
741 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
742 efx_rx_queue_index(
743 efx_channel_get_rx_queue(ptp->channel)));
744 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
745 htonl(PTP_ADDRESS),
746 htons(PTP_EVENT_PORT));
747 if (rc != 0)
748 return rc;
749
750 rc = efx_filter_insert_filter(efx, &rxfilter, true);
751 if (rc < 0)
752 return rc;
753 ptp->rxfilter_event = rc;
754
755 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
756 efx_rx_queue_index(
757 efx_channel_get_rx_queue(ptp->channel)));
758 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
759 htonl(PTP_ADDRESS),
760 htons(PTP_GENERAL_PORT));
761 if (rc != 0)
762 goto fail;
763
764 rc = efx_filter_insert_filter(efx, &rxfilter, true);
765 if (rc < 0)
766 goto fail;
767 ptp->rxfilter_general = rc;
768
769 rc = efx_ptp_enable(efx);
770 if (rc != 0)
771 goto fail2;
772
773 ptp->evt_frag_idx = 0;
774 ptp->current_adjfreq = 0;
775 ptp->rxfilter_installed = true;
776
777 return 0;
778
779fail2:
780 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
781 ptp->rxfilter_general);
782fail:
783 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
784 ptp->rxfilter_event);
785
786 return rc;
787}
788
789static int efx_ptp_stop(struct efx_nic *efx)
790{
791 struct efx_ptp_data *ptp = efx->ptp_data;
792 int rc = efx_ptp_disable(efx);
793 struct list_head *cursor;
794 struct list_head *next;
795
796 if (ptp->rxfilter_installed) {
797 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
798 ptp->rxfilter_general);
799 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
800 ptp->rxfilter_event);
801 ptp->rxfilter_installed = false;
802 }
803
804 /* Make sure RX packets are really delivered */
805 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
806 skb_queue_purge(&efx->ptp_data->txq);
807
808 /* Drop any pending receive events */
809 spin_lock_bh(&efx->ptp_data->evt_lock);
810 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
Wei Yongjun9545f4e2012-10-07 03:41:50 +0000811 list_move(cursor, &efx->ptp_data->evt_free_list);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100812 }
813 spin_unlock_bh(&efx->ptp_data->evt_lock);
814
815 return rc;
816}
817
818static void efx_ptp_pps_worker(struct work_struct *work)
819{
820 struct efx_ptp_data *ptp =
821 container_of(work, struct efx_ptp_data, pps_work);
822 struct efx_nic *efx = ptp->channel->efx;
823 struct ptp_clock_event ptp_evt;
824
825 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
826 return;
827
828 ptp_evt.type = PTP_CLOCK_PPSUSR;
829 ptp_evt.pps_times = ptp->host_time_pps;
830 ptp_clock_event(ptp->phc_clock, &ptp_evt);
831}
832
833/* Process any pending transmissions and timestamp any received packets.
834 */
835static void efx_ptp_worker(struct work_struct *work)
836{
837 struct efx_ptp_data *ptp_data =
838 container_of(work, struct efx_ptp_data, work);
839 struct efx_nic *efx = ptp_data->channel->efx;
840 struct sk_buff *skb;
841 struct sk_buff_head tempq;
842
843 if (ptp_data->reset_required) {
844 efx_ptp_stop(efx);
845 efx_ptp_start(efx);
846 return;
847 }
848
849 efx_ptp_drop_time_expired_events(efx);
850
851 __skb_queue_head_init(&tempq);
852 if (efx_ptp_process_events(efx, &tempq) ||
853 !skb_queue_empty(&ptp_data->txq)) {
854
855 while ((skb = skb_dequeue(&ptp_data->txq)))
856 efx_ptp_xmit_skb(efx, skb);
857 }
858
859 while ((skb = __skb_dequeue(&tempq)))
860 efx_ptp_process_rx(efx, skb);
861}
862
863/* Initialise PTP channel and state.
864 *
865 * Setting core_index to zero causes the queue to be initialised and doesn't
866 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
867 */
868static int efx_ptp_probe_channel(struct efx_channel *channel)
869{
870 struct efx_nic *efx = channel->efx;
871 struct efx_ptp_data *ptp;
872 int rc = 0;
873 unsigned int pos;
874
875 channel->irq_moderation = 0;
876 channel->rx_queue.core_index = 0;
877
878 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
879 efx->ptp_data = ptp;
880 if (!efx->ptp_data)
881 return -ENOMEM;
882
Ben Hutchings0d19a542012-09-18 21:59:52 +0100883 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100884 if (rc != 0)
885 goto fail1;
886
887 ptp->channel = channel;
888 skb_queue_head_init(&ptp->rxq);
889 skb_queue_head_init(&ptp->txq);
890 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
891 if (!ptp->workwq) {
892 rc = -ENOMEM;
893 goto fail2;
894 }
895
896 INIT_WORK(&ptp->work, efx_ptp_worker);
897 ptp->config.flags = 0;
898 ptp->config.tx_type = HWTSTAMP_TX_OFF;
899 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
900 INIT_LIST_HEAD(&ptp->evt_list);
901 INIT_LIST_HEAD(&ptp->evt_free_list);
902 spin_lock_init(&ptp->evt_lock);
903 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
904 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
905
906 ptp->phc_clock_info.owner = THIS_MODULE;
907 snprintf(ptp->phc_clock_info.name,
908 sizeof(ptp->phc_clock_info.name),
909 "%pm", efx->net_dev->perm_addr);
910 ptp->phc_clock_info.max_adj = MAX_PPB;
911 ptp->phc_clock_info.n_alarm = 0;
912 ptp->phc_clock_info.n_ext_ts = 0;
913 ptp->phc_clock_info.n_per_out = 0;
914 ptp->phc_clock_info.pps = 1;
915 ptp->phc_clock_info.adjfreq = efx_phc_adjfreq;
916 ptp->phc_clock_info.adjtime = efx_phc_adjtime;
917 ptp->phc_clock_info.gettime = efx_phc_gettime;
918 ptp->phc_clock_info.settime = efx_phc_settime;
919 ptp->phc_clock_info.enable = efx_phc_enable;
920
Richard Cochran1ef76152012-09-22 07:02:03 +0000921 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
922 &efx->pci_dev->dev);
Wei Yongjun155d9402013-05-07 02:19:25 +0000923 if (IS_ERR(ptp->phc_clock)) {
924 rc = PTR_ERR(ptp->phc_clock);
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100925 goto fail3;
Wei Yongjun155d9402013-05-07 02:19:25 +0000926 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100927
928 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
929 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
930 if (!ptp->pps_workwq) {
931 rc = -ENOMEM;
932 goto fail4;
933 }
934 ptp->nic_ts_enabled = false;
935
936 return 0;
937fail4:
938 ptp_clock_unregister(efx->ptp_data->phc_clock);
939
940fail3:
941 destroy_workqueue(efx->ptp_data->workwq);
942
943fail2:
944 efx_nic_free_buffer(efx, &ptp->start);
945
946fail1:
947 kfree(efx->ptp_data);
948 efx->ptp_data = NULL;
949
950 return rc;
951}
952
953static void efx_ptp_remove_channel(struct efx_channel *channel)
954{
955 struct efx_nic *efx = channel->efx;
956
957 if (!efx->ptp_data)
958 return;
959
960 (void)efx_ptp_disable(channel->efx);
961
962 cancel_work_sync(&efx->ptp_data->work);
963 cancel_work_sync(&efx->ptp_data->pps_work);
964
965 skb_queue_purge(&efx->ptp_data->rxq);
966 skb_queue_purge(&efx->ptp_data->txq);
967
968 ptp_clock_unregister(efx->ptp_data->phc_clock);
969
970 destroy_workqueue(efx->ptp_data->workwq);
971 destroy_workqueue(efx->ptp_data->pps_workwq);
972
973 efx_nic_free_buffer(efx, &efx->ptp_data->start);
974 kfree(efx->ptp_data);
975}
976
977static void efx_ptp_get_channel_name(struct efx_channel *channel,
978 char *buf, size_t len)
979{
980 snprintf(buf, len, "%s-ptp", channel->efx->name);
981}
982
983/* Determine whether this packet should be processed by the PTP module
984 * or transmitted conventionally.
985 */
986bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
987{
988 return efx->ptp_data &&
989 efx->ptp_data->enabled &&
990 skb->len >= PTP_MIN_LENGTH &&
991 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
992 likely(skb->protocol == htons(ETH_P_IP)) &&
993 ip_hdr(skb)->protocol == IPPROTO_UDP &&
994 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
995}
996
997/* Receive a PTP packet. Packets are queued until the arrival of
998 * the receive timestamp from the MC - this will probably occur after the
999 * packet arrival because of the processing in the MC.
1000 */
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001001static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001002{
1003 struct efx_nic *efx = channel->efx;
1004 struct efx_ptp_data *ptp = efx->ptp_data;
1005 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
Laurence Evansc939a312012-11-15 10:56:07 +00001006 u8 *match_data_012, *match_data_345;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001007 unsigned int version;
1008
1009 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1010
1011 /* Correct version? */
1012 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001013 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001014 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001015 }
1016 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1017 if (version != PTP_VERSION_V1) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001018 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001019 }
Laurence Evansc939a312012-11-15 10:56:07 +00001020
1021 /* PTP V1 uses all six bytes of the UUID to match the packet
1022 * to the timestamp
1023 */
1024 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1025 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001026 } else {
Alexandre Rames97d48a12013-01-11 12:26:21 +00001027 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001028 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001029 }
1030 version = skb->data[PTP_V2_VERSION_OFFSET];
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001031 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001032 return false;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001033 }
Laurence Evansc939a312012-11-15 10:56:07 +00001034
1035 /* The original V2 implementation uses bytes 2-7 of
1036 * the UUID to match the packet to the timestamp. This
1037 * discards two of the bytes of the MAC address used
1038 * to create the UUID (SF bug 33070). The PTP V2
1039 * enhanced mode fixes this issue and uses bytes 0-2
1040 * and byte 5-7 of the UUID.
1041 */
1042 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1043 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1044 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1045 } else {
1046 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1047 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1048 }
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001049 }
1050
1051 /* Does this packet require timestamping? */
1052 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1053 struct skb_shared_hwtstamps *timestamps;
1054
1055 match->state = PTP_PACKET_STATE_UNMATCHED;
1056
1057 /* Clear all timestamps held: filled in later */
1058 timestamps = skb_hwtstamps(skb);
1059 memset(timestamps, 0, sizeof(*timestamps));
1060
Laurence Evansc939a312012-11-15 10:56:07 +00001061 /* We expect the sequence number to be in the same position in
1062 * the packet for PTP V1 and V2
1063 */
1064 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1065 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1066
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001067 /* Extract UUID/Sequence information */
Laurence Evansc939a312012-11-15 10:56:07 +00001068 match->words[0] = (match_data_012[0] |
1069 (match_data_012[1] << 8) |
1070 (match_data_012[2] << 16) |
1071 (match_data_345[0] << 24));
1072 match->words[1] = (match_data_345[1] |
1073 (match_data_345[2] << 8) |
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001074 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1075 PTP_V1_SEQUENCE_LENGTH - 1] <<
1076 16));
1077 } else {
1078 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1079 }
1080
1081 skb_queue_tail(&ptp->rxq, skb);
1082 queue_work(ptp->workwq, &ptp->work);
Ben Hutchings4a74dc62013-03-05 20:13:54 +00001083
1084 return true;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001085}
1086
1087/* Transmit a PTP packet. This has to be transmitted by the MC
1088 * itself, through an MCDI call. MCDI calls aren't permitted
1089 * in the transmit path so defer the actual transmission to a suitable worker.
1090 */
1091int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1092{
1093 struct efx_ptp_data *ptp = efx->ptp_data;
1094
1095 skb_queue_tail(&ptp->txq, skb);
1096
1097 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1098 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1099 efx_xmit_hwtstamp_pending(skb);
1100 queue_work(ptp->workwq, &ptp->work);
1101
1102 return NETDEV_TX_OK;
1103}
1104
1105static int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1106 unsigned int new_mode)
1107{
1108 if ((enable_wanted != efx->ptp_data->enabled) ||
1109 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1110 int rc;
1111
1112 if (enable_wanted) {
1113 /* Change of mode requires disable */
1114 if (efx->ptp_data->enabled &&
1115 (efx->ptp_data->mode != new_mode)) {
1116 efx->ptp_data->enabled = false;
1117 rc = efx_ptp_stop(efx);
1118 if (rc != 0)
1119 return rc;
1120 }
1121
1122 /* Set new operating mode and establish
1123 * baseline synchronisation, which must
1124 * succeed.
1125 */
1126 efx->ptp_data->mode = new_mode;
1127 rc = efx_ptp_start(efx);
1128 if (rc == 0) {
1129 rc = efx_ptp_synchronize(efx,
1130 PTP_SYNC_ATTEMPTS * 2);
1131 if (rc != 0)
1132 efx_ptp_stop(efx);
1133 }
1134 } else {
1135 rc = efx_ptp_stop(efx);
1136 }
1137
1138 if (rc != 0)
1139 return rc;
1140
1141 efx->ptp_data->enabled = enable_wanted;
1142 }
1143
1144 return 0;
1145}
1146
1147static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1148{
1149 bool enable_wanted = false;
1150 unsigned int new_mode;
1151 int rc;
1152
1153 if (init->flags)
1154 return -EINVAL;
1155
1156 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1157 (init->tx_type != HWTSTAMP_TX_ON))
1158 return -ERANGE;
1159
1160 new_mode = efx->ptp_data->mode;
1161 /* Determine whether any PTP HW operations are required */
1162 switch (init->rx_filter) {
1163 case HWTSTAMP_FILTER_NONE:
1164 break;
1165 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1166 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1167 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1168 init->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
1169 new_mode = MC_CMD_PTP_MODE_V1;
1170 enable_wanted = true;
1171 break;
1172 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1173 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1174 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1175 /* Although these three are accepted only IPV4 packets will be
1176 * timestamped
1177 */
1178 init->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
Laurence Evansc939a312012-11-15 10:56:07 +00001179 new_mode = MC_CMD_PTP_MODE_V2_ENHANCED;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001180 enable_wanted = true;
1181 break;
1182 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1183 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1184 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1185 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1186 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1187 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1188 /* Non-IP + IPv6 timestamping not supported */
1189 return -ERANGE;
1190 break;
1191 default:
1192 return -ERANGE;
1193 }
1194
1195 if (init->tx_type != HWTSTAMP_TX_OFF)
1196 enable_wanted = true;
1197
Laurence Evansc939a312012-11-15 10:56:07 +00001198 /* Old versions of the firmware do not support the improved
1199 * UUID filtering option (SF bug 33070). If the firmware does
1200 * not accept the enhanced mode, fall back to the standard PTP
1201 * v2 UUID filtering.
1202 */
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001203 rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
Laurence Evansc939a312012-11-15 10:56:07 +00001204 if ((rc != 0) && (new_mode == MC_CMD_PTP_MODE_V2_ENHANCED))
1205 rc = efx_ptp_change_mode(efx, enable_wanted, MC_CMD_PTP_MODE_V2);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001206 if (rc != 0)
1207 return rc;
1208
1209 efx->ptp_data->config = *init;
1210
1211 return 0;
1212}
1213
Ben Hutchings62ebac92013-04-08 17:34:58 +01001214void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001215{
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001216 struct efx_ptp_data *ptp = efx->ptp_data;
1217
1218 if (!ptp)
Ben Hutchings62ebac92013-04-08 17:34:58 +01001219 return;
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001220
Ben Hutchings62ebac92013-04-08 17:34:58 +01001221 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1222 SOF_TIMESTAMPING_RX_HARDWARE |
1223 SOF_TIMESTAMPING_RAW_HARDWARE);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001224 ts_info->phc_index = ptp_clock_index(ptp->phc_clock);
1225 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1226 ts_info->rx_filters = (1 << HWTSTAMP_FILTER_NONE |
1227 1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT |
1228 1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC |
1229 1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ |
1230 1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT |
1231 1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC |
1232 1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001233}
1234
1235int efx_ptp_ioctl(struct efx_nic *efx, struct ifreq *ifr, int cmd)
1236{
1237 struct hwtstamp_config config;
1238 int rc;
1239
1240 /* Not a PTP enabled port */
1241 if (!efx->ptp_data)
1242 return -EOPNOTSUPP;
1243
1244 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1245 return -EFAULT;
1246
1247 rc = efx_ptp_ts_init(efx, &config);
1248 if (rc != 0)
1249 return rc;
1250
1251 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1252 ? -EFAULT : 0;
1253}
1254
1255static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1256{
1257 struct efx_ptp_data *ptp = efx->ptp_data;
1258
1259 netif_err(efx, hw, efx->net_dev,
1260 "PTP unexpected event length: got %d expected %d\n",
1261 ptp->evt_frag_idx, expected_frag_len);
1262 ptp->reset_required = true;
1263 queue_work(ptp->workwq, &ptp->work);
1264}
1265
1266/* Process a completed receive event. Put it on the event queue and
1267 * start worker thread. This is required because event and their
1268 * correspoding packets may come in either order.
1269 */
1270static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1271{
1272 struct efx_ptp_event_rx *evt = NULL;
1273
1274 if (ptp->evt_frag_idx != 3) {
1275 ptp_event_failure(efx, 3);
1276 return;
1277 }
1278
1279 spin_lock_bh(&ptp->evt_lock);
1280 if (!list_empty(&ptp->evt_free_list)) {
1281 evt = list_first_entry(&ptp->evt_free_list,
1282 struct efx_ptp_event_rx, link);
1283 list_del(&evt->link);
1284
1285 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1286 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1287 MCDI_EVENT_SRC) |
1288 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1289 MCDI_EVENT_SRC) << 8) |
1290 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1291 MCDI_EVENT_SRC) << 16));
1292 evt->hwtimestamp = ktime_set(
1293 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1294 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA));
1295 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1296 list_add_tail(&evt->link, &ptp->evt_list);
1297
1298 queue_work(ptp->workwq, &ptp->work);
1299 } else {
1300 netif_err(efx, rx_err, efx->net_dev, "No free PTP event");
1301 }
1302 spin_unlock_bh(&ptp->evt_lock);
1303}
1304
1305static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1306{
1307 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1308 if (ptp->evt_frag_idx != 1) {
1309 ptp_event_failure(efx, 1);
1310 return;
1311 }
1312
1313 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1314}
1315
1316static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1317{
1318 if (ptp->nic_ts_enabled)
1319 queue_work(ptp->pps_workwq, &ptp->pps_work);
1320}
1321
1322void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1323{
1324 struct efx_ptp_data *ptp = efx->ptp_data;
1325 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1326
1327 if (!ptp->enabled)
1328 return;
1329
1330 if (ptp->evt_frag_idx == 0) {
1331 ptp->evt_code = code;
1332 } else if (ptp->evt_code != code) {
1333 netif_err(efx, hw, efx->net_dev,
1334 "PTP out of sequence event %d\n", code);
1335 ptp->evt_frag_idx = 0;
1336 }
1337
1338 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1339 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1340 /* Process resulting event */
1341 switch (code) {
1342 case MCDI_EVENT_CODE_PTP_RX:
1343 ptp_event_rx(efx, ptp);
1344 break;
1345 case MCDI_EVENT_CODE_PTP_FAULT:
1346 ptp_event_fault(efx, ptp);
1347 break;
1348 case MCDI_EVENT_CODE_PTP_PPS:
1349 ptp_event_pps(efx, ptp);
1350 break;
1351 default:
1352 netif_err(efx, hw, efx->net_dev,
1353 "PTP unknown event %d\n", code);
1354 break;
1355 }
1356 ptp->evt_frag_idx = 0;
1357 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1358 netif_err(efx, hw, efx->net_dev,
1359 "PTP too many event fragments\n");
1360 ptp->evt_frag_idx = 0;
1361 }
1362}
1363
1364static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1365{
1366 struct efx_ptp_data *ptp_data = container_of(ptp,
1367 struct efx_ptp_data,
1368 phc_clock_info);
1369 struct efx_nic *efx = ptp_data->channel->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001370 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001371 s64 adjustment_ns;
1372 int rc;
1373
1374 if (delta > MAX_PPB)
1375 delta = MAX_PPB;
1376 else if (delta < -MAX_PPB)
1377 delta = -MAX_PPB;
1378
1379 /* Convert ppb to fixed point ns. */
1380 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1381 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1382
1383 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001384 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001385 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001386 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1387 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1388 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1389 NULL, 0, NULL);
1390 if (rc != 0)
1391 return rc;
1392
1393 ptp_data->current_adjfreq = delta;
1394 return 0;
1395}
1396
1397static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1398{
1399 struct efx_ptp_data *ptp_data = container_of(ptp,
1400 struct efx_ptp_data,
1401 phc_clock_info);
1402 struct efx_nic *efx = ptp_data->channel->efx;
1403 struct timespec delta_ts = ns_to_timespec(delta);
Ben Hutchings59cfc472012-09-14 17:30:10 +01001404 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001405
1406 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001407 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Ben Hutchings338f74d2012-10-10 23:20:17 +01001408 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001409 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_SECONDS, (u32)delta_ts.tv_sec);
1410 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_NANOSECONDS, (u32)delta_ts.tv_nsec);
1411 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1412 NULL, 0, NULL);
1413}
1414
1415static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1416{
1417 struct efx_ptp_data *ptp_data = container_of(ptp,
1418 struct efx_ptp_data,
1419 phc_clock_info);
1420 struct efx_nic *efx = ptp_data->channel->efx;
Ben Hutchings59cfc472012-09-14 17:30:10 +01001421 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1422 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001423 int rc;
1424
1425 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
Laurence Evansc1d828b2013-03-06 15:33:17 +00001426 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001427
1428 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1429 outbuf, sizeof(outbuf), NULL);
1430 if (rc != 0)
1431 return rc;
1432
1433 ts->tv_sec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_SECONDS);
1434 ts->tv_nsec = MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_NANOSECONDS);
1435 return 0;
1436}
1437
1438static int efx_phc_settime(struct ptp_clock_info *ptp,
1439 const struct timespec *e_ts)
1440{
1441 /* Get the current NIC time, efx_phc_gettime.
1442 * Subtract from the desired time to get the offset
1443 * call efx_phc_adjtime with the offset
1444 */
1445 int rc;
1446 struct timespec time_now;
1447 struct timespec delta;
1448
1449 rc = efx_phc_gettime(ptp, &time_now);
1450 if (rc != 0)
1451 return rc;
1452
1453 delta = timespec_sub(*e_ts, time_now);
1454
Julia Lawall56567c62013-01-21 03:02:48 +00001455 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001456 if (rc != 0)
1457 return rc;
1458
1459 return 0;
1460}
1461
1462static int efx_phc_enable(struct ptp_clock_info *ptp,
1463 struct ptp_clock_request *request,
1464 int enable)
1465{
1466 struct efx_ptp_data *ptp_data = container_of(ptp,
1467 struct efx_ptp_data,
1468 phc_clock_info);
1469 if (request->type != PTP_CLK_REQ_PPS)
1470 return -EOPNOTSUPP;
1471
1472 ptp_data->nic_ts_enabled = !!enable;
1473 return 0;
1474}
1475
1476static const struct efx_channel_type efx_ptp_channel_type = {
1477 .handle_no_channel = efx_ptp_handle_no_channel,
1478 .pre_probe = efx_ptp_probe_channel,
1479 .post_remove = efx_ptp_remove_channel,
1480 .get_name = efx_ptp_get_channel_name,
1481 /* no copy operation; there is no need to reallocate this channel */
1482 .receive_skb = efx_ptp_rx,
1483 .keep_eventq = false,
1484};
1485
1486void efx_ptp_probe(struct efx_nic *efx)
1487{
1488 /* Check whether PTP is implemented on this NIC. The DISABLE
1489 * operation will succeed if and only if it is implemented.
1490 */
1491 if (efx_ptp_disable(efx) == 0)
1492 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1493 &efx_ptp_channel_type;
1494}