sfc: Remove unnecessary condition for processing the TX timestamp queue
[deliverable/linux.git] / drivers / net / ethernet / sfc / ptp.c
1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2011-2013 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"
49 #include "farch_regs.h"
50 #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 DEFAULT_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
102 #define PTP_V2_UUID_LENGTH 8
103 #define PTP_V2_UUID_OFFSET 48
104
105 /* 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
135 enum 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 */
175 struct 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 */
187 struct 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 * @major: Hardware timestamp, major
199 * @minor: Hardware timestamp, minor
200 * @host_end: Host time immediately after hardware timestamp taken
201 * @wait: Number of NIC clock ticks 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 */
206 struct efx_ptp_timeset {
207 u32 host_start;
208 u32 major;
209 u32 minor;
210 u32 host_end;
211 u32 wait;
212 u32 window; /* Derived: end - start, allowing for wrap */
213 };
214
215 /**
216 * struct efx_ptp_data - Precision Time Protocol (PTP) state
217 * @efx: The NIC context
218 * @channel: The PTP channel (Siena only)
219 * @rx_ts_inline: Flag for whether RX timestamps are inline (else they are
220 * separate events)
221 * @rxq: Receive queue (awaiting timestamps)
222 * @txq: Transmit queue
223 * @evt_list: List of MC receive events awaiting packets
224 * @evt_free_list: List of free events
225 * @evt_lock: Lock for manipulating evt_list and evt_free_list
226 * @evt_overflow: Boolean indicating that event list has overflowed
227 * @rx_evts: Instantiated events (on evt_list and evt_free_list)
228 * @workwq: Work queue for processing pending PTP operations
229 * @work: Work task
230 * @reset_required: A serious error has occurred and the PTP task needs to be
231 * reset (disable, enable).
232 * @rxfilter_event: Receive filter when operating
233 * @rxfilter_general: Receive filter when operating
234 * @config: Current timestamp configuration
235 * @enabled: PTP operation enabled
236 * @mode: Mode in which PTP operating (PTP version)
237 * @time_format: Time format supported by this NIC
238 * @ns_to_nic_time: Function to convert from scalar nanoseconds to NIC time
239 * @nic_to_kernel_time: Function to convert from NIC to kernel time
240 * @min_synchronisation_ns: Minimum acceptable corrected sync window
241 * @ts_corrections.tx: Required driver correction of transmit timestamps
242 * @ts_corrections.rx: Required driver correction of receive timestamps
243 * @ts_corrections.pps_out: PPS output error (information only)
244 * @ts_corrections.pps_in: Required driver correction of PPS input timestamps
245 * @evt_frags: Partly assembled PTP events
246 * @evt_frag_idx: Current fragment number
247 * @evt_code: Last event code
248 * @start: Address at which MC indicates ready for synchronisation
249 * @host_time_pps: Host time at last PPS
250 * @current_adjfreq: Current ppb adjustment.
251 * @phc_clock: Pointer to registered phc device (if primary function)
252 * @phc_clock_info: Registration structure for phc device
253 * @pps_work: pps work task for handling pps events
254 * @pps_workwq: pps work queue
255 * @nic_ts_enabled: Flag indicating if NIC generated TS events are handled
256 * @txbuf: Buffer for use when transmitting (PTP) packets to MC (avoids
257 * allocations in main data path).
258 * @timeset: Last set of synchronisation statistics.
259 */
260 struct efx_ptp_data {
261 struct efx_nic *efx;
262 struct efx_channel *channel;
263 bool rx_ts_inline;
264 struct sk_buff_head rxq;
265 struct sk_buff_head txq;
266 struct list_head evt_list;
267 struct list_head evt_free_list;
268 spinlock_t evt_lock;
269 bool evt_overflow;
270 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
271 struct workqueue_struct *workwq;
272 struct work_struct work;
273 bool reset_required;
274 u32 rxfilter_event;
275 u32 rxfilter_general;
276 bool rxfilter_installed;
277 struct hwtstamp_config config;
278 bool enabled;
279 unsigned int mode;
280 unsigned int time_format;
281 void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
282 ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
283 s32 correction);
284 unsigned int min_synchronisation_ns;
285 struct {
286 s32 tx;
287 s32 rx;
288 s32 pps_out;
289 s32 pps_in;
290 } ts_corrections;
291 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
292 int evt_frag_idx;
293 int evt_code;
294 struct efx_buffer start;
295 struct pps_event_time host_time_pps;
296 s64 current_adjfreq;
297 struct ptp_clock *phc_clock;
298 struct ptp_clock_info phc_clock_info;
299 struct work_struct pps_work;
300 struct workqueue_struct *pps_workwq;
301 bool nic_ts_enabled;
302 MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
303 struct efx_ptp_timeset
304 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
305 };
306
307 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
308 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
309 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts);
310 static int efx_phc_settime(struct ptp_clock_info *ptp,
311 const struct timespec *e_ts);
312 static int efx_phc_enable(struct ptp_clock_info *ptp,
313 struct ptp_clock_request *request, int on);
314
315 /* For Siena platforms NIC time is s and ns */
316 static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
317 {
318 struct timespec ts = ns_to_timespec(ns);
319 *nic_major = ts.tv_sec;
320 *nic_minor = ts.tv_nsec;
321 }
322
323 static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
324 s32 correction)
325 {
326 ktime_t kt = ktime_set(nic_major, nic_minor);
327 if (correction >= 0)
328 kt = ktime_add_ns(kt, (u64)correction);
329 else
330 kt = ktime_sub_ns(kt, (u64)-correction);
331 return kt;
332 }
333
334 /* To convert from s27 format to ns we multiply then divide by a power of 2.
335 * For the conversion from ns to s27, the operation is also converted to a
336 * multiply and shift.
337 */
338 #define S27_TO_NS_SHIFT (27)
339 #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
340 #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
341 #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
342
343 /* For Huntington platforms NIC time is in seconds and fractions of a second
344 * where the minor register only uses 27 bits in units of 2^-27s.
345 */
346 static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
347 {
348 struct timespec ts = ns_to_timespec(ns);
349 u32 maj = ts.tv_sec;
350 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
351 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
352
353 /* The conversion can result in the minor value exceeding the maximum.
354 * In this case, round up to the next second.
355 */
356 if (min >= S27_MINOR_MAX) {
357 min -= S27_MINOR_MAX;
358 maj++;
359 }
360
361 *nic_major = maj;
362 *nic_minor = min;
363 }
364
365 static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
366 {
367 u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
368 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
369 return ktime_set(nic_major, ns);
370 }
371
372 static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
373 s32 correction)
374 {
375 /* Apply the correction and deal with carry */
376 nic_minor += correction;
377 if ((s32)nic_minor < 0) {
378 nic_minor += S27_MINOR_MAX;
379 nic_major--;
380 } else if (nic_minor >= S27_MINOR_MAX) {
381 nic_minor -= S27_MINOR_MAX;
382 nic_major++;
383 }
384
385 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
386 }
387
388 /* Get PTP attributes and set up time conversions */
389 static int efx_ptp_get_attributes(struct efx_nic *efx)
390 {
391 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
392 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
393 struct efx_ptp_data *ptp = efx->ptp_data;
394 int rc;
395 u32 fmt;
396 size_t out_len;
397
398 /* Get the PTP attributes. If the NIC doesn't support the operation we
399 * use the default format for compatibility with older NICs i.e.
400 * seconds and nanoseconds.
401 */
402 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
403 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
404 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
405 outbuf, sizeof(outbuf), &out_len);
406 if (rc == 0)
407 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
408 else if (rc == -EINVAL)
409 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
410 else
411 return rc;
412
413 if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION) {
414 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
415 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
416 } else if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS) {
417 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
418 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
419 } else {
420 return -ERANGE;
421 }
422
423 ptp->time_format = fmt;
424
425 /* MC_CMD_PTP_OP_GET_ATTRIBUTES is an extended version of an older
426 * operation MC_CMD_PTP_OP_GET_TIME_FORMAT that also returns a value
427 * to use for the minimum acceptable corrected synchronization window.
428 * If we have the extra information store it. For older firmware that
429 * does not implement the extended command use the default value.
430 */
431 if (rc == 0 && out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
432 ptp->min_synchronisation_ns =
433 MCDI_DWORD(outbuf,
434 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
435 else
436 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
437
438 return 0;
439 }
440
441 /* Get PTP timestamp corrections */
442 static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
443 {
444 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
445 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_LEN);
446 int rc;
447
448 /* Get the timestamp corrections from the NIC. If this operation is
449 * not supported (older NICs) then no correction is required.
450 */
451 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
452 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
453 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
454
455 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
456 outbuf, sizeof(outbuf), NULL);
457 if (rc == 0) {
458 efx->ptp_data->ts_corrections.tx = MCDI_DWORD(outbuf,
459 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
460 efx->ptp_data->ts_corrections.rx = MCDI_DWORD(outbuf,
461 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
462 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
463 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
464 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
465 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
466 } else if (rc == -EINVAL) {
467 efx->ptp_data->ts_corrections.tx = 0;
468 efx->ptp_data->ts_corrections.rx = 0;
469 efx->ptp_data->ts_corrections.pps_out = 0;
470 efx->ptp_data->ts_corrections.pps_in = 0;
471 } else {
472 return rc;
473 }
474
475 return 0;
476 }
477
478 /* Enable MCDI PTP support. */
479 static int efx_ptp_enable(struct efx_nic *efx)
480 {
481 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
482 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
483 int rc;
484
485 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
486 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
487 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
488 efx->ptp_data->channel ?
489 efx->ptp_data->channel->channel : 0);
490 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
491
492 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
493 outbuf, sizeof(outbuf), NULL);
494 rc = (rc == -EALREADY) ? 0 : rc;
495 if (rc)
496 efx_mcdi_display_error(efx, MC_CMD_PTP,
497 MC_CMD_PTP_IN_ENABLE_LEN,
498 outbuf, sizeof(outbuf), rc);
499 return rc;
500 }
501
502 /* Disable MCDI PTP support.
503 *
504 * Note that this function should never rely on the presence of ptp_data -
505 * may be called before that exists.
506 */
507 static int efx_ptp_disable(struct efx_nic *efx)
508 {
509 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
510 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
511 int rc;
512
513 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
514 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
515 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
516 outbuf, sizeof(outbuf), NULL);
517 rc = (rc == -EALREADY) ? 0 : rc;
518 if (rc)
519 efx_mcdi_display_error(efx, MC_CMD_PTP,
520 MC_CMD_PTP_IN_DISABLE_LEN,
521 outbuf, sizeof(outbuf), rc);
522 return rc;
523 }
524
525 static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
526 {
527 struct sk_buff *skb;
528
529 while ((skb = skb_dequeue(q))) {
530 local_bh_disable();
531 netif_receive_skb(skb);
532 local_bh_enable();
533 }
534 }
535
536 static void efx_ptp_handle_no_channel(struct efx_nic *efx)
537 {
538 netif_err(efx, drv, efx->net_dev,
539 "ERROR: PTP requires MSI-X and 1 additional interrupt"
540 "vector. PTP disabled\n");
541 }
542
543 /* Repeatedly send the host time to the MC which will capture the hardware
544 * time.
545 */
546 static void efx_ptp_send_times(struct efx_nic *efx,
547 struct pps_event_time *last_time)
548 {
549 struct pps_event_time now;
550 struct timespec limit;
551 struct efx_ptp_data *ptp = efx->ptp_data;
552 struct timespec start;
553 int *mc_running = ptp->start.addr;
554
555 pps_get_ts(&now);
556 start = now.ts_real;
557 limit = now.ts_real;
558 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
559
560 /* Write host time for specified period or until MC is done */
561 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
562 ACCESS_ONCE(*mc_running)) {
563 struct timespec update_time;
564 unsigned int host_time;
565
566 /* Don't update continuously to avoid saturating the PCIe bus */
567 update_time = now.ts_real;
568 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
569 do {
570 pps_get_ts(&now);
571 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
572 ACCESS_ONCE(*mc_running));
573
574 /* Synchronise NIC with single word of time only */
575 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
576 now.ts_real.tv_nsec);
577 /* Update host time in NIC memory */
578 efx->type->ptp_write_host_time(efx, host_time);
579 }
580 *last_time = now;
581 }
582
583 /* Read a timeset from the MC's results and partial process. */
584 static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
585 struct efx_ptp_timeset *timeset)
586 {
587 unsigned start_ns, end_ns;
588
589 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
590 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
591 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
592 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
593 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
594
595 /* Ignore seconds */
596 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
597 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
598 /* Allow for rollover */
599 if (end_ns < start_ns)
600 end_ns += NSEC_PER_SEC;
601 /* Determine duration of operation */
602 timeset->window = end_ns - start_ns;
603 }
604
605 /* Process times received from MC.
606 *
607 * Extract times from returned results, and establish the minimum value
608 * seen. The minimum value represents the "best" possible time and events
609 * too much greater than this are rejected - the machine is, perhaps, too
610 * busy. A number of readings are taken so that, hopefully, at least one good
611 * synchronisation will be seen in the results.
612 */
613 static int
614 efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
615 size_t response_length,
616 const struct pps_event_time *last_time)
617 {
618 unsigned number_readings =
619 MCDI_VAR_ARRAY_LEN(response_length,
620 PTP_OUT_SYNCHRONIZE_TIMESET);
621 unsigned i;
622 unsigned ngood = 0;
623 unsigned last_good = 0;
624 struct efx_ptp_data *ptp = efx->ptp_data;
625 u32 last_sec;
626 u32 start_sec;
627 struct timespec delta;
628 ktime_t mc_time;
629
630 if (number_readings == 0)
631 return -EAGAIN;
632
633 /* Read the set of results and find the last good host-MC
634 * synchronization result. The MC times when it finishes reading the
635 * host time so the corrected window time should be fairly constant
636 * for a given platform.
637 */
638 for (i = 0; i < number_readings; i++) {
639 s32 window, corrected;
640 struct timespec wait;
641
642 efx_ptp_read_timeset(
643 MCDI_ARRAY_STRUCT_PTR(synch_buf,
644 PTP_OUT_SYNCHRONIZE_TIMESET, i),
645 &ptp->timeset[i]);
646
647 wait = ktime_to_timespec(
648 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
649 window = ptp->timeset[i].window;
650 corrected = window - wait.tv_nsec;
651
652 /* We expect the uncorrected synchronization window to be at
653 * least as large as the interval between host start and end
654 * times. If it is smaller than this then this is mostly likely
655 * to be a consequence of the host's time being adjusted.
656 * Check that the corrected sync window is in a reasonable
657 * range. If it is out of range it is likely to be because an
658 * interrupt or other delay occurred between reading the system
659 * time and writing it to MC memory.
660 */
661 if (window >= SYNCHRONISATION_GRANULARITY_NS &&
662 corrected < MAX_SYNCHRONISATION_NS &&
663 corrected >= ptp->min_synchronisation_ns) {
664 ngood++;
665 last_good = i;
666 }
667 }
668
669 if (ngood == 0) {
670 netif_warn(efx, drv, efx->net_dev,
671 "PTP no suitable synchronisations\n");
672 return -EAGAIN;
673 }
674
675 /* Convert the NIC time into kernel time. No correction is required-
676 * this time is the output of a firmware process.
677 */
678 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
679 ptp->timeset[last_good].minor, 0);
680
681 /* Calculate delay from actual PPS to last_time */
682 delta = ktime_to_timespec(mc_time);
683 delta.tv_nsec +=
684 last_time->ts_real.tv_nsec -
685 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
686
687 /* It is possible that the seconds rolled over between taking
688 * the start reading and the last value written by the host. The
689 * timescales are such that a gap of more than one second is never
690 * expected.
691 */
692 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
693 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
694 if (start_sec != last_sec) {
695 if (((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
696 netif_warn(efx, hw, efx->net_dev,
697 "PTP bad synchronisation seconds\n");
698 return -EAGAIN;
699 } else {
700 delta.tv_sec = 1;
701 }
702 } else {
703 delta.tv_sec = 0;
704 }
705
706 ptp->host_time_pps = *last_time;
707 pps_sub_ts(&ptp->host_time_pps, delta);
708
709 return 0;
710 }
711
712 /* Synchronize times between the host and the MC */
713 static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
714 {
715 struct efx_ptp_data *ptp = efx->ptp_data;
716 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
717 size_t response_length;
718 int rc;
719 unsigned long timeout;
720 struct pps_event_time last_time = {};
721 unsigned int loops = 0;
722 int *start = ptp->start.addr;
723
724 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
725 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
726 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
727 num_readings);
728 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
729 ptp->start.dma_addr);
730
731 /* Clear flag that signals MC ready */
732 ACCESS_ONCE(*start) = 0;
733 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
734 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
735 EFX_BUG_ON_PARANOID(rc);
736
737 /* Wait for start from MCDI (or timeout) */
738 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
739 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
740 udelay(20); /* Usually start MCDI execution quickly */
741 loops++;
742 }
743
744 if (ACCESS_ONCE(*start))
745 efx_ptp_send_times(efx, &last_time);
746
747 /* Collect results */
748 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
749 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
750 synch_buf, sizeof(synch_buf),
751 &response_length);
752 if (rc == 0)
753 rc = efx_ptp_process_times(efx, synch_buf, response_length,
754 &last_time);
755
756 return rc;
757 }
758
759 /* Transmit a PTP packet, via the MCDI interface, to the wire. */
760 static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
761 {
762 struct efx_ptp_data *ptp_data = efx->ptp_data;
763 struct skb_shared_hwtstamps timestamps;
764 int rc = -EIO;
765 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
766 size_t len;
767
768 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
769 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
770 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
771 if (skb_shinfo(skb)->nr_frags != 0) {
772 rc = skb_linearize(skb);
773 if (rc != 0)
774 goto fail;
775 }
776
777 if (skb->ip_summed == CHECKSUM_PARTIAL) {
778 rc = skb_checksum_help(skb);
779 if (rc != 0)
780 goto fail;
781 }
782 skb_copy_from_linear_data(skb,
783 MCDI_PTR(ptp_data->txbuf,
784 PTP_IN_TRANSMIT_PACKET),
785 skb->len);
786 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
787 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
788 txtime, sizeof(txtime), &len);
789 if (rc != 0)
790 goto fail;
791
792 memset(&timestamps, 0, sizeof(timestamps));
793 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
794 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
795 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
796 ptp_data->ts_corrections.tx);
797
798 skb_tstamp_tx(skb, &timestamps);
799
800 rc = 0;
801
802 fail:
803 dev_kfree_skb(skb);
804
805 return rc;
806 }
807
808 static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
809 {
810 struct efx_ptp_data *ptp = efx->ptp_data;
811 struct list_head *cursor;
812 struct list_head *next;
813
814 if (ptp->rx_ts_inline)
815 return;
816
817 /* Drop time-expired events */
818 spin_lock_bh(&ptp->evt_lock);
819 if (!list_empty(&ptp->evt_list)) {
820 list_for_each_safe(cursor, next, &ptp->evt_list) {
821 struct efx_ptp_event_rx *evt;
822
823 evt = list_entry(cursor, struct efx_ptp_event_rx,
824 link);
825 if (time_after(jiffies, evt->expiry)) {
826 list_move(&evt->link, &ptp->evt_free_list);
827 netif_warn(efx, hw, efx->net_dev,
828 "PTP rx event dropped\n");
829 }
830 }
831 }
832 /* If the event overflow flag is set and the event list is now empty
833 * clear the flag to re-enable the overflow warning message.
834 */
835 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
836 ptp->evt_overflow = false;
837 spin_unlock_bh(&ptp->evt_lock);
838 }
839
840 static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
841 struct sk_buff *skb)
842 {
843 struct efx_ptp_data *ptp = efx->ptp_data;
844 bool evts_waiting;
845 struct list_head *cursor;
846 struct list_head *next;
847 struct efx_ptp_match *match;
848 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
849
850 WARN_ON_ONCE(ptp->rx_ts_inline);
851
852 spin_lock_bh(&ptp->evt_lock);
853 evts_waiting = !list_empty(&ptp->evt_list);
854 spin_unlock_bh(&ptp->evt_lock);
855
856 if (!evts_waiting)
857 return PTP_PACKET_STATE_UNMATCHED;
858
859 match = (struct efx_ptp_match *)skb->cb;
860 /* Look for a matching timestamp in the event queue */
861 spin_lock_bh(&ptp->evt_lock);
862 list_for_each_safe(cursor, next, &ptp->evt_list) {
863 struct efx_ptp_event_rx *evt;
864
865 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
866 if ((evt->seq0 == match->words[0]) &&
867 (evt->seq1 == match->words[1])) {
868 struct skb_shared_hwtstamps *timestamps;
869
870 /* Match - add in hardware timestamp */
871 timestamps = skb_hwtstamps(skb);
872 timestamps->hwtstamp = evt->hwtimestamp;
873
874 match->state = PTP_PACKET_STATE_MATCHED;
875 rc = PTP_PACKET_STATE_MATCHED;
876 list_move(&evt->link, &ptp->evt_free_list);
877 break;
878 }
879 }
880 /* If the event overflow flag is set and the event list is now empty
881 * clear the flag to re-enable the overflow warning message.
882 */
883 if (ptp->evt_overflow && list_empty(&ptp->evt_list))
884 ptp->evt_overflow = false;
885 spin_unlock_bh(&ptp->evt_lock);
886
887 return rc;
888 }
889
890 /* Process any queued receive events and corresponding packets
891 *
892 * q is returned with all the packets that are ready for delivery.
893 */
894 static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
895 {
896 struct efx_ptp_data *ptp = efx->ptp_data;
897 struct sk_buff *skb;
898
899 while ((skb = skb_dequeue(&ptp->rxq))) {
900 struct efx_ptp_match *match;
901
902 match = (struct efx_ptp_match *)skb->cb;
903 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
904 __skb_queue_tail(q, skb);
905 } else if (efx_ptp_match_rx(efx, skb) ==
906 PTP_PACKET_STATE_MATCHED) {
907 __skb_queue_tail(q, skb);
908 } else if (time_after(jiffies, match->expiry)) {
909 match->state = PTP_PACKET_STATE_TIMED_OUT;
910 if (net_ratelimit())
911 netif_warn(efx, rx_err, efx->net_dev,
912 "PTP packet - no timestamp seen\n");
913 __skb_queue_tail(q, skb);
914 } else {
915 /* Replace unprocessed entry and stop */
916 skb_queue_head(&ptp->rxq, skb);
917 break;
918 }
919 }
920 }
921
922 /* Complete processing of a received packet */
923 static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
924 {
925 local_bh_disable();
926 netif_receive_skb(skb);
927 local_bh_enable();
928 }
929
930 static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
931 {
932 struct efx_ptp_data *ptp = efx->ptp_data;
933
934 if (ptp->rxfilter_installed) {
935 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
936 ptp->rxfilter_general);
937 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
938 ptp->rxfilter_event);
939 ptp->rxfilter_installed = false;
940 }
941 }
942
943 static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
944 {
945 struct efx_ptp_data *ptp = efx->ptp_data;
946 struct efx_filter_spec rxfilter;
947 int rc;
948
949 if (!ptp->channel || ptp->rxfilter_installed)
950 return 0;
951
952 /* Must filter on both event and general ports to ensure
953 * that there is no packet re-ordering.
954 */
955 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
956 efx_rx_queue_index(
957 efx_channel_get_rx_queue(ptp->channel)));
958 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
959 htonl(PTP_ADDRESS),
960 htons(PTP_EVENT_PORT));
961 if (rc != 0)
962 return rc;
963
964 rc = efx_filter_insert_filter(efx, &rxfilter, true);
965 if (rc < 0)
966 return rc;
967 ptp->rxfilter_event = rc;
968
969 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
970 efx_rx_queue_index(
971 efx_channel_get_rx_queue(ptp->channel)));
972 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
973 htonl(PTP_ADDRESS),
974 htons(PTP_GENERAL_PORT));
975 if (rc != 0)
976 goto fail;
977
978 rc = efx_filter_insert_filter(efx, &rxfilter, true);
979 if (rc < 0)
980 goto fail;
981 ptp->rxfilter_general = rc;
982
983 ptp->rxfilter_installed = true;
984 return 0;
985
986 fail:
987 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
988 ptp->rxfilter_event);
989 return rc;
990 }
991
992 static int efx_ptp_start(struct efx_nic *efx)
993 {
994 struct efx_ptp_data *ptp = efx->ptp_data;
995 int rc;
996
997 ptp->reset_required = false;
998
999 rc = efx_ptp_insert_multicast_filters(efx);
1000 if (rc)
1001 return rc;
1002
1003 rc = efx_ptp_enable(efx);
1004 if (rc != 0)
1005 goto fail;
1006
1007 ptp->evt_frag_idx = 0;
1008 ptp->current_adjfreq = 0;
1009
1010 return 0;
1011
1012 fail:
1013 efx_ptp_remove_multicast_filters(efx);
1014 return rc;
1015 }
1016
1017 static int efx_ptp_stop(struct efx_nic *efx)
1018 {
1019 struct efx_ptp_data *ptp = efx->ptp_data;
1020 struct list_head *cursor;
1021 struct list_head *next;
1022 int rc;
1023
1024 if (ptp == NULL)
1025 return 0;
1026
1027 rc = efx_ptp_disable(efx);
1028
1029 efx_ptp_remove_multicast_filters(efx);
1030
1031 /* Make sure RX packets are really delivered */
1032 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1033 skb_queue_purge(&efx->ptp_data->txq);
1034
1035 /* Drop any pending receive events */
1036 spin_lock_bh(&efx->ptp_data->evt_lock);
1037 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
1038 list_move(cursor, &efx->ptp_data->evt_free_list);
1039 }
1040 ptp->evt_overflow = false;
1041 spin_unlock_bh(&efx->ptp_data->evt_lock);
1042
1043 return rc;
1044 }
1045
1046 static int efx_ptp_restart(struct efx_nic *efx)
1047 {
1048 if (efx->ptp_data && efx->ptp_data->enabled)
1049 return efx_ptp_start(efx);
1050 return 0;
1051 }
1052
1053 static void efx_ptp_pps_worker(struct work_struct *work)
1054 {
1055 struct efx_ptp_data *ptp =
1056 container_of(work, struct efx_ptp_data, pps_work);
1057 struct efx_nic *efx = ptp->efx;
1058 struct ptp_clock_event ptp_evt;
1059
1060 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1061 return;
1062
1063 ptp_evt.type = PTP_CLOCK_PPSUSR;
1064 ptp_evt.pps_times = ptp->host_time_pps;
1065 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1066 }
1067
1068 static void efx_ptp_worker(struct work_struct *work)
1069 {
1070 struct efx_ptp_data *ptp_data =
1071 container_of(work, struct efx_ptp_data, work);
1072 struct efx_nic *efx = ptp_data->efx;
1073 struct sk_buff *skb;
1074 struct sk_buff_head tempq;
1075
1076 if (ptp_data->reset_required) {
1077 efx_ptp_stop(efx);
1078 efx_ptp_start(efx);
1079 return;
1080 }
1081
1082 efx_ptp_drop_time_expired_events(efx);
1083
1084 __skb_queue_head_init(&tempq);
1085 efx_ptp_process_events(efx, &tempq);
1086
1087 while ((skb = skb_dequeue(&ptp_data->txq)))
1088 efx_ptp_xmit_skb(efx, skb);
1089
1090 while ((skb = __skb_dequeue(&tempq)))
1091 efx_ptp_process_rx(efx, skb);
1092 }
1093
1094 static const struct ptp_clock_info efx_phc_clock_info = {
1095 .owner = THIS_MODULE,
1096 .name = "sfc",
1097 .max_adj = MAX_PPB,
1098 .n_alarm = 0,
1099 .n_ext_ts = 0,
1100 .n_per_out = 0,
1101 .pps = 1,
1102 .adjfreq = efx_phc_adjfreq,
1103 .adjtime = efx_phc_adjtime,
1104 .gettime = efx_phc_gettime,
1105 .settime = efx_phc_settime,
1106 .enable = efx_phc_enable,
1107 };
1108
1109 /* Initialise PTP state. */
1110 int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
1111 {
1112 struct efx_ptp_data *ptp;
1113 int rc = 0;
1114 unsigned int pos;
1115
1116 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1117 efx->ptp_data = ptp;
1118 if (!efx->ptp_data)
1119 return -ENOMEM;
1120
1121 ptp->efx = efx;
1122 ptp->channel = channel;
1123 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1124
1125 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
1126 if (rc != 0)
1127 goto fail1;
1128
1129 skb_queue_head_init(&ptp->rxq);
1130 skb_queue_head_init(&ptp->txq);
1131 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1132 if (!ptp->workwq) {
1133 rc = -ENOMEM;
1134 goto fail2;
1135 }
1136
1137 INIT_WORK(&ptp->work, efx_ptp_worker);
1138 ptp->config.flags = 0;
1139 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1140 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1141 INIT_LIST_HEAD(&ptp->evt_list);
1142 INIT_LIST_HEAD(&ptp->evt_free_list);
1143 spin_lock_init(&ptp->evt_lock);
1144 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1145 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1146 ptp->evt_overflow = false;
1147
1148 /* Get the NIC PTP attributes and set up time conversions */
1149 rc = efx_ptp_get_attributes(efx);
1150 if (rc < 0)
1151 goto fail3;
1152
1153 /* Get the timestamp corrections */
1154 rc = efx_ptp_get_timestamp_corrections(efx);
1155 if (rc < 0)
1156 goto fail3;
1157
1158 if (efx->mcdi->fn_flags &
1159 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1160 ptp->phc_clock_info = efx_phc_clock_info;
1161 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1162 &efx->pci_dev->dev);
1163 if (IS_ERR(ptp->phc_clock)) {
1164 rc = PTR_ERR(ptp->phc_clock);
1165 goto fail3;
1166 }
1167
1168 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1169 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1170 if (!ptp->pps_workwq) {
1171 rc = -ENOMEM;
1172 goto fail4;
1173 }
1174 }
1175 ptp->nic_ts_enabled = false;
1176
1177 return 0;
1178 fail4:
1179 ptp_clock_unregister(efx->ptp_data->phc_clock);
1180
1181 fail3:
1182 destroy_workqueue(efx->ptp_data->workwq);
1183
1184 fail2:
1185 efx_nic_free_buffer(efx, &ptp->start);
1186
1187 fail1:
1188 kfree(efx->ptp_data);
1189 efx->ptp_data = NULL;
1190
1191 return rc;
1192 }
1193
1194 /* Initialise PTP channel.
1195 *
1196 * Setting core_index to zero causes the queue to be initialised and doesn't
1197 * overlap with 'rxq0' because ptp.c doesn't use skb_record_rx_queue.
1198 */
1199 static int efx_ptp_probe_channel(struct efx_channel *channel)
1200 {
1201 struct efx_nic *efx = channel->efx;
1202
1203 channel->irq_moderation = 0;
1204 channel->rx_queue.core_index = 0;
1205
1206 return efx_ptp_probe(efx, channel);
1207 }
1208
1209 void efx_ptp_remove(struct efx_nic *efx)
1210 {
1211 if (!efx->ptp_data)
1212 return;
1213
1214 (void)efx_ptp_disable(efx);
1215
1216 cancel_work_sync(&efx->ptp_data->work);
1217 cancel_work_sync(&efx->ptp_data->pps_work);
1218
1219 skb_queue_purge(&efx->ptp_data->rxq);
1220 skb_queue_purge(&efx->ptp_data->txq);
1221
1222 if (efx->ptp_data->phc_clock) {
1223 destroy_workqueue(efx->ptp_data->pps_workwq);
1224 ptp_clock_unregister(efx->ptp_data->phc_clock);
1225 }
1226
1227 destroy_workqueue(efx->ptp_data->workwq);
1228
1229 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1230 kfree(efx->ptp_data);
1231 }
1232
1233 static void efx_ptp_remove_channel(struct efx_channel *channel)
1234 {
1235 efx_ptp_remove(channel->efx);
1236 }
1237
1238 static void efx_ptp_get_channel_name(struct efx_channel *channel,
1239 char *buf, size_t len)
1240 {
1241 snprintf(buf, len, "%s-ptp", channel->efx->name);
1242 }
1243
1244 /* Determine whether this packet should be processed by the PTP module
1245 * or transmitted conventionally.
1246 */
1247 bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1248 {
1249 return efx->ptp_data &&
1250 efx->ptp_data->enabled &&
1251 skb->len >= PTP_MIN_LENGTH &&
1252 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1253 likely(skb->protocol == htons(ETH_P_IP)) &&
1254 skb_transport_header_was_set(skb) &&
1255 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
1256 ip_hdr(skb)->protocol == IPPROTO_UDP &&
1257 skb_headlen(skb) >=
1258 skb_transport_offset(skb) + sizeof(struct udphdr) &&
1259 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1260 }
1261
1262 /* Receive a PTP packet. Packets are queued until the arrival of
1263 * the receive timestamp from the MC - this will probably occur after the
1264 * packet arrival because of the processing in the MC.
1265 */
1266 static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
1267 {
1268 struct efx_nic *efx = channel->efx;
1269 struct efx_ptp_data *ptp = efx->ptp_data;
1270 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
1271 u8 *match_data_012, *match_data_345;
1272 unsigned int version;
1273
1274 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1275
1276 /* Correct version? */
1277 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
1278 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
1279 return false;
1280 }
1281 version = ntohs(*(__be16 *)&skb->data[PTP_V1_VERSION_OFFSET]);
1282 if (version != PTP_VERSION_V1) {
1283 return false;
1284 }
1285
1286 /* PTP V1 uses all six bytes of the UUID to match the packet
1287 * to the timestamp
1288 */
1289 match_data_012 = skb->data + PTP_V1_UUID_OFFSET;
1290 match_data_345 = skb->data + PTP_V1_UUID_OFFSET + 3;
1291 } else {
1292 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
1293 return false;
1294 }
1295 version = skb->data[PTP_V2_VERSION_OFFSET];
1296 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
1297 return false;
1298 }
1299
1300 /* The original V2 implementation uses bytes 2-7 of
1301 * the UUID to match the packet to the timestamp. This
1302 * discards two of the bytes of the MAC address used
1303 * to create the UUID (SF bug 33070). The PTP V2
1304 * enhanced mode fixes this issue and uses bytes 0-2
1305 * and byte 5-7 of the UUID.
1306 */
1307 match_data_345 = skb->data + PTP_V2_UUID_OFFSET + 5;
1308 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1309 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 2;
1310 } else {
1311 match_data_012 = skb->data + PTP_V2_UUID_OFFSET + 0;
1312 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1313 }
1314 }
1315
1316 /* Does this packet require timestamping? */
1317 if (ntohs(*(__be16 *)&skb->data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1318 match->state = PTP_PACKET_STATE_UNMATCHED;
1319
1320 /* We expect the sequence number to be in the same position in
1321 * the packet for PTP V1 and V2
1322 */
1323 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1324 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1325
1326 /* Extract UUID/Sequence information */
1327 match->words[0] = (match_data_012[0] |
1328 (match_data_012[1] << 8) |
1329 (match_data_012[2] << 16) |
1330 (match_data_345[0] << 24));
1331 match->words[1] = (match_data_345[1] |
1332 (match_data_345[2] << 8) |
1333 (skb->data[PTP_V1_SEQUENCE_OFFSET +
1334 PTP_V1_SEQUENCE_LENGTH - 1] <<
1335 16));
1336 } else {
1337 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1338 }
1339
1340 skb_queue_tail(&ptp->rxq, skb);
1341 queue_work(ptp->workwq, &ptp->work);
1342
1343 return true;
1344 }
1345
1346 /* Transmit a PTP packet. This has to be transmitted by the MC
1347 * itself, through an MCDI call. MCDI calls aren't permitted
1348 * in the transmit path so defer the actual transmission to a suitable worker.
1349 */
1350 int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1351 {
1352 struct efx_ptp_data *ptp = efx->ptp_data;
1353
1354 skb_queue_tail(&ptp->txq, skb);
1355
1356 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1357 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1358 efx_xmit_hwtstamp_pending(skb);
1359 queue_work(ptp->workwq, &ptp->work);
1360
1361 return NETDEV_TX_OK;
1362 }
1363
1364 int efx_ptp_get_mode(struct efx_nic *efx)
1365 {
1366 return efx->ptp_data->mode;
1367 }
1368
1369 int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1370 unsigned int new_mode)
1371 {
1372 if ((enable_wanted != efx->ptp_data->enabled) ||
1373 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1374 int rc = 0;
1375
1376 if (enable_wanted) {
1377 /* Change of mode requires disable */
1378 if (efx->ptp_data->enabled &&
1379 (efx->ptp_data->mode != new_mode)) {
1380 efx->ptp_data->enabled = false;
1381 rc = efx_ptp_stop(efx);
1382 if (rc != 0)
1383 return rc;
1384 }
1385
1386 /* Set new operating mode and establish
1387 * baseline synchronisation, which must
1388 * succeed.
1389 */
1390 efx->ptp_data->mode = new_mode;
1391 if (netif_running(efx->net_dev))
1392 rc = efx_ptp_start(efx);
1393 if (rc == 0) {
1394 rc = efx_ptp_synchronize(efx,
1395 PTP_SYNC_ATTEMPTS * 2);
1396 if (rc != 0)
1397 efx_ptp_stop(efx);
1398 }
1399 } else {
1400 rc = efx_ptp_stop(efx);
1401 }
1402
1403 if (rc != 0)
1404 return rc;
1405
1406 efx->ptp_data->enabled = enable_wanted;
1407 }
1408
1409 return 0;
1410 }
1411
1412 static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1413 {
1414 int rc;
1415
1416 if (init->flags)
1417 return -EINVAL;
1418
1419 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1420 (init->tx_type != HWTSTAMP_TX_ON))
1421 return -ERANGE;
1422
1423 rc = efx->type->ptp_set_ts_config(efx, init);
1424 if (rc)
1425 return rc;
1426
1427 efx->ptp_data->config = *init;
1428 return 0;
1429 }
1430
1431 void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
1432 {
1433 struct efx_ptp_data *ptp = efx->ptp_data;
1434 struct efx_nic *primary = efx->primary;
1435
1436 ASSERT_RTNL();
1437
1438 if (!ptp)
1439 return;
1440
1441 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1442 SOF_TIMESTAMPING_RX_HARDWARE |
1443 SOF_TIMESTAMPING_RAW_HARDWARE);
1444 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1445 ts_info->phc_index =
1446 ptp_clock_index(primary->ptp_data->phc_clock);
1447 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1448 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
1449 }
1450
1451 int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1452 {
1453 struct hwtstamp_config config;
1454 int rc;
1455
1456 /* Not a PTP enabled port */
1457 if (!efx->ptp_data)
1458 return -EOPNOTSUPP;
1459
1460 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1461 return -EFAULT;
1462
1463 rc = efx_ptp_ts_init(efx, &config);
1464 if (rc != 0)
1465 return rc;
1466
1467 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1468 ? -EFAULT : 0;
1469 }
1470
1471 int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1472 {
1473 if (!efx->ptp_data)
1474 return -EOPNOTSUPP;
1475
1476 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1477 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1478 }
1479
1480 static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1481 {
1482 struct efx_ptp_data *ptp = efx->ptp_data;
1483
1484 netif_err(efx, hw, efx->net_dev,
1485 "PTP unexpected event length: got %d expected %d\n",
1486 ptp->evt_frag_idx, expected_frag_len);
1487 ptp->reset_required = true;
1488 queue_work(ptp->workwq, &ptp->work);
1489 }
1490
1491 /* Process a completed receive event. Put it on the event queue and
1492 * start worker thread. This is required because event and their
1493 * correspoding packets may come in either order.
1494 */
1495 static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1496 {
1497 struct efx_ptp_event_rx *evt = NULL;
1498
1499 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1500 return;
1501
1502 if (ptp->evt_frag_idx != 3) {
1503 ptp_event_failure(efx, 3);
1504 return;
1505 }
1506
1507 spin_lock_bh(&ptp->evt_lock);
1508 if (!list_empty(&ptp->evt_free_list)) {
1509 evt = list_first_entry(&ptp->evt_free_list,
1510 struct efx_ptp_event_rx, link);
1511 list_del(&evt->link);
1512
1513 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1514 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1515 MCDI_EVENT_SRC) |
1516 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1517 MCDI_EVENT_SRC) << 8) |
1518 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1519 MCDI_EVENT_SRC) << 16));
1520 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
1521 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1522 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1523 ptp->ts_corrections.rx);
1524 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1525 list_add_tail(&evt->link, &ptp->evt_list);
1526
1527 queue_work(ptp->workwq, &ptp->work);
1528 } else if (!ptp->evt_overflow) {
1529 /* Log a warning message and set the event overflow flag.
1530 * The message won't be logged again until the event queue
1531 * becomes empty.
1532 */
1533 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1534 ptp->evt_overflow = true;
1535 }
1536 spin_unlock_bh(&ptp->evt_lock);
1537 }
1538
1539 static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1540 {
1541 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1542 if (ptp->evt_frag_idx != 1) {
1543 ptp_event_failure(efx, 1);
1544 return;
1545 }
1546
1547 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1548 }
1549
1550 static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1551 {
1552 if (ptp->nic_ts_enabled)
1553 queue_work(ptp->pps_workwq, &ptp->pps_work);
1554 }
1555
1556 void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1557 {
1558 struct efx_ptp_data *ptp = efx->ptp_data;
1559 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1560
1561 if (!ptp->enabled)
1562 return;
1563
1564 if (ptp->evt_frag_idx == 0) {
1565 ptp->evt_code = code;
1566 } else if (ptp->evt_code != code) {
1567 netif_err(efx, hw, efx->net_dev,
1568 "PTP out of sequence event %d\n", code);
1569 ptp->evt_frag_idx = 0;
1570 }
1571
1572 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1573 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1574 /* Process resulting event */
1575 switch (code) {
1576 case MCDI_EVENT_CODE_PTP_RX:
1577 ptp_event_rx(efx, ptp);
1578 break;
1579 case MCDI_EVENT_CODE_PTP_FAULT:
1580 ptp_event_fault(efx, ptp);
1581 break;
1582 case MCDI_EVENT_CODE_PTP_PPS:
1583 ptp_event_pps(efx, ptp);
1584 break;
1585 default:
1586 netif_err(efx, hw, efx->net_dev,
1587 "PTP unknown event %d\n", code);
1588 break;
1589 }
1590 ptp->evt_frag_idx = 0;
1591 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1592 netif_err(efx, hw, efx->net_dev,
1593 "PTP too many event fragments\n");
1594 ptp->evt_frag_idx = 0;
1595 }
1596 }
1597
1598 void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1599 {
1600 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1601 channel->sync_timestamp_minor =
1602 MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_26_19) << 19;
1603 /* if sync events have been disabled then we want to silently ignore
1604 * this event, so throw away result.
1605 */
1606 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1607 SYNC_EVENTS_VALID);
1608 }
1609
1610 /* make some assumptions about the time representation rather than abstract it,
1611 * since we currently only support one type of inline timestamping and only on
1612 * EF10.
1613 */
1614 #define MINOR_TICKS_PER_SECOND 0x8000000
1615 /* Fuzz factor for sync events to be out of order with RX events */
1616 #define FUZZ (MINOR_TICKS_PER_SECOND / 10)
1617 #define EXPECTED_SYNC_EVENTS_PER_SECOND 4
1618
1619 static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1620 {
1621 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1622 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1623 #else
1624 const u8 *data = eh + efx->rx_packet_ts_offset;
1625 return (u32)data[0] |
1626 (u32)data[1] << 8 |
1627 (u32)data[2] << 16 |
1628 (u32)data[3] << 24;
1629 #endif
1630 }
1631
1632 void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
1633 struct sk_buff *skb)
1634 {
1635 struct efx_nic *efx = channel->efx;
1636 u32 pkt_timestamp_major, pkt_timestamp_minor;
1637 u32 diff, carry;
1638 struct skb_shared_hwtstamps *timestamps;
1639
1640 pkt_timestamp_minor = (efx_rx_buf_timestamp_minor(efx,
1641 skb_mac_header(skb)) +
1642 (u32) efx->ptp_data->ts_corrections.rx) &
1643 (MINOR_TICKS_PER_SECOND - 1);
1644
1645 /* get the difference between the packet and sync timestamps,
1646 * modulo one second
1647 */
1648 diff = (pkt_timestamp_minor - channel->sync_timestamp_minor) &
1649 (MINOR_TICKS_PER_SECOND - 1);
1650 /* do we roll over a second boundary and need to carry the one? */
1651 carry = channel->sync_timestamp_minor + diff > MINOR_TICKS_PER_SECOND ?
1652 1 : 0;
1653
1654 if (diff <= MINOR_TICKS_PER_SECOND / EXPECTED_SYNC_EVENTS_PER_SECOND +
1655 FUZZ) {
1656 /* packet is ahead of the sync event by a quarter of a second or
1657 * less (allowing for fuzz)
1658 */
1659 pkt_timestamp_major = channel->sync_timestamp_major + carry;
1660 } else if (diff >= MINOR_TICKS_PER_SECOND - FUZZ) {
1661 /* packet is behind the sync event but within the fuzz factor.
1662 * This means the RX packet and sync event crossed as they were
1663 * placed on the event queue, which can sometimes happen.
1664 */
1665 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
1666 } else {
1667 /* it's outside tolerance in both directions. this might be
1668 * indicative of us missing sync events for some reason, so
1669 * we'll call it an error rather than risk giving a bogus
1670 * timestamp.
1671 */
1672 netif_vdbg(efx, drv, efx->net_dev,
1673 "packet timestamp %x too far from sync event %x:%x\n",
1674 pkt_timestamp_minor, channel->sync_timestamp_major,
1675 channel->sync_timestamp_minor);
1676 return;
1677 }
1678
1679 /* attach the timestamps to the skb */
1680 timestamps = skb_hwtstamps(skb);
1681 timestamps->hwtstamp =
1682 efx_ptp_s27_to_ktime(pkt_timestamp_major, pkt_timestamp_minor);
1683 }
1684
1685 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1686 {
1687 struct efx_ptp_data *ptp_data = container_of(ptp,
1688 struct efx_ptp_data,
1689 phc_clock_info);
1690 struct efx_nic *efx = ptp_data->efx;
1691 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
1692 s64 adjustment_ns;
1693 int rc;
1694
1695 if (delta > MAX_PPB)
1696 delta = MAX_PPB;
1697 else if (delta < -MAX_PPB)
1698 delta = -MAX_PPB;
1699
1700 /* Convert ppb to fixed point ns. */
1701 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1702 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1703
1704 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
1705 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
1706 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
1707 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1708 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1709 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1710 NULL, 0, NULL);
1711 if (rc != 0)
1712 return rc;
1713
1714 ptp_data->current_adjfreq = adjustment_ns;
1715 return 0;
1716 }
1717
1718 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1719 {
1720 u32 nic_major, nic_minor;
1721 struct efx_ptp_data *ptp_data = container_of(ptp,
1722 struct efx_ptp_data,
1723 phc_clock_info);
1724 struct efx_nic *efx = ptp_data->efx;
1725 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
1726
1727 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
1728
1729 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
1730 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
1731 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
1732 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
1733 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
1734 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1735 NULL, 0, NULL);
1736 }
1737
1738 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1739 {
1740 struct efx_ptp_data *ptp_data = container_of(ptp,
1741 struct efx_ptp_data,
1742 phc_clock_info);
1743 struct efx_nic *efx = ptp_data->efx;
1744 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1745 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
1746 int rc;
1747 ktime_t kt;
1748
1749 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
1750 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
1751
1752 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1753 outbuf, sizeof(outbuf), NULL);
1754 if (rc != 0)
1755 return rc;
1756
1757 kt = ptp_data->nic_to_kernel_time(
1758 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
1759 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
1760 *ts = ktime_to_timespec(kt);
1761 return 0;
1762 }
1763
1764 static int efx_phc_settime(struct ptp_clock_info *ptp,
1765 const struct timespec *e_ts)
1766 {
1767 /* Get the current NIC time, efx_phc_gettime.
1768 * Subtract from the desired time to get the offset
1769 * call efx_phc_adjtime with the offset
1770 */
1771 int rc;
1772 struct timespec time_now;
1773 struct timespec delta;
1774
1775 rc = efx_phc_gettime(ptp, &time_now);
1776 if (rc != 0)
1777 return rc;
1778
1779 delta = timespec_sub(*e_ts, time_now);
1780
1781 rc = efx_phc_adjtime(ptp, timespec_to_ns(&delta));
1782 if (rc != 0)
1783 return rc;
1784
1785 return 0;
1786 }
1787
1788 static int efx_phc_enable(struct ptp_clock_info *ptp,
1789 struct ptp_clock_request *request,
1790 int enable)
1791 {
1792 struct efx_ptp_data *ptp_data = container_of(ptp,
1793 struct efx_ptp_data,
1794 phc_clock_info);
1795 if (request->type != PTP_CLK_REQ_PPS)
1796 return -EOPNOTSUPP;
1797
1798 ptp_data->nic_ts_enabled = !!enable;
1799 return 0;
1800 }
1801
1802 static const struct efx_channel_type efx_ptp_channel_type = {
1803 .handle_no_channel = efx_ptp_handle_no_channel,
1804 .pre_probe = efx_ptp_probe_channel,
1805 .post_remove = efx_ptp_remove_channel,
1806 .get_name = efx_ptp_get_channel_name,
1807 /* no copy operation; there is no need to reallocate this channel */
1808 .receive_skb = efx_ptp_rx,
1809 .keep_eventq = false,
1810 };
1811
1812 void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
1813 {
1814 /* Check whether PTP is implemented on this NIC. The DISABLE
1815 * operation will succeed if and only if it is implemented.
1816 */
1817 if (efx_ptp_disable(efx) == 0)
1818 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1819 &efx_ptp_channel_type;
1820 }
1821
1822 void efx_ptp_start_datapath(struct efx_nic *efx)
1823 {
1824 if (efx_ptp_restart(efx))
1825 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
1826 /* re-enable timestamping if it was previously enabled */
1827 if (efx->type->ptp_set_ts_sync_events)
1828 efx->type->ptp_set_ts_sync_events(efx, true, true);
1829 }
1830
1831 void efx_ptp_stop_datapath(struct efx_nic *efx)
1832 {
1833 /* temporarily disable timestamping */
1834 if (efx->type->ptp_set_ts_sync_events)
1835 efx->type->ptp_set_ts_sync_events(efx, false, true);
1836 efx_ptp_stop(efx);
1837 }
This page took 0.098091 seconds and 5 git commands to generate.