1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
53#define MAX_EVENT_FRAGS 3
54
55
56#define MAX_SYNCHRONISE_WAIT_MS 2
57
58
59#define SYNCHRONISE_PERIOD_NS 250000
60
61
62#define SYNCHRONISATION_GRANULARITY_NS 200
63
64
65#define DEFAULT_MIN_SYNCHRONISATION_NS 120
66
67
68#define MAX_SYNCHRONISATION_NS 1000
69
70
71#define MAX_RECEIVE_EVENTS 8
72
73
74#define AVERAGE_LENGTH 16
75
76
77#define PKT_EVENT_LIFETIME_MS 10
78
79
80
81
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
95
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
106
107
108
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
117
118
119#define PTP_V2_MIN_LENGTH 63
120
121#define PTP_MIN_LENGTH 63
122
123#define PTP_ADDRESS 0xe0000181
124#define PTP_EVENT_PORT 319
125#define PTP_GENERAL_PORT 320
126
127
128
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
143
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
150#define MAX_PPB 1000000
151
152
153#define MAX_PPB_BITS 20
154
155
156
157
158
159#define PPB_EXTRA_BITS 2
160
161
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
169
170
171
172
173
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
183
184
185
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
197
198
199
200
201
202
203
204
205
206struct efx_ptp_timeset {
207 u32 host_start;
208 u32 major;
209 u32 minor;
210 u32 host_end;
211 u32 wait;
212 u32 window;
213};
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268struct efx_ptp_data {
269 struct efx_nic *efx;
270 struct efx_channel *channel;
271 bool rx_ts_inline;
272 struct sk_buff_head rxq;
273 struct sk_buff_head txq;
274 struct list_head evt_list;
275 struct list_head evt_free_list;
276 spinlock_t evt_lock;
277 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
278 struct workqueue_struct *workwq;
279 struct work_struct work;
280 bool reset_required;
281 u32 rxfilter_event;
282 u32 rxfilter_general;
283 bool rxfilter_installed;
284 struct hwtstamp_config config;
285 bool enabled;
286 unsigned int mode;
287 unsigned int time_format;
288 void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
289 ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
290 s32 correction);
291 unsigned int min_synchronisation_ns;
292 struct {
293 s32 tx;
294 s32 rx;
295 s32 pps_out;
296 s32 pps_in;
297 } ts_corrections;
298 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
299 int evt_frag_idx;
300 int evt_code;
301 struct efx_buffer start;
302 struct pps_event_time host_time_pps;
303 s64 current_adjfreq;
304 struct ptp_clock *phc_clock;
305 struct ptp_clock_info phc_clock_info;
306 struct work_struct pps_work;
307 struct workqueue_struct *pps_workwq;
308 bool nic_ts_enabled;
309 MCDI_DECLARE_BUF(txbuf, MC_CMD_PTP_IN_TRANSMIT_LENMAX);
310
311 unsigned int good_syncs;
312 unsigned int fast_syncs;
313 unsigned int bad_syncs;
314 unsigned int sync_timeouts;
315 unsigned int no_time_syncs;
316 unsigned int invalid_sync_windows;
317 unsigned int undersize_sync_windows;
318 unsigned int oversize_sync_windows;
319 unsigned int rx_no_timestamp;
320 struct efx_ptp_timeset
321 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
322};
323
324static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
325static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
326static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
327static int efx_phc_settime(struct ptp_clock_info *ptp,
328 const struct timespec64 *e_ts);
329static int efx_phc_enable(struct ptp_clock_info *ptp,
330 struct ptp_clock_request *request, int on);
331
332#define PTP_SW_STAT(ext_name, field_name) \
333 { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
334#define PTP_MC_STAT(ext_name, mcdi_name) \
335 { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
336static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
337 PTP_SW_STAT(ptp_good_syncs, good_syncs),
338 PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
339 PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
340 PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
341 PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
342 PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
343 PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
344 PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
345 PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
346 PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
347 PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
348 PTP_MC_STAT(ptp_timestamp_packets, TS),
349 PTP_MC_STAT(ptp_filter_matches, FM),
350 PTP_MC_STAT(ptp_non_filter_matches, NFM),
351};
352#define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
353static const unsigned long efx_ptp_stat_mask[] = {
354 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
355};
356
357size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
358{
359 if (!efx->ptp_data)
360 return 0;
361
362 return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
363 efx_ptp_stat_mask, strings);
364}
365
366size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
367{
368 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
369 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
370 size_t i;
371 int rc;
372
373 if (!efx->ptp_data)
374 return 0;
375
376
377 for (i = 0; i < PTP_STAT_COUNT; i++) {
378 if (efx_ptp_stat_desc[i].dma_width)
379 continue;
380 stats[i] = *(unsigned int *)((char *)efx->ptp_data +
381 efx_ptp_stat_desc[i].offset);
382 }
383
384
385
386
387
388 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
389 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
390 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
391 outbuf, sizeof(outbuf), NULL);
392 if (rc) {
393 netif_err(efx, hw, efx->net_dev,
394 "MC_CMD_PTP_OP_STATUS failed (%d)\n", rc);
395 memset(outbuf, 0, sizeof(outbuf));
396 }
397 efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
398 efx_ptp_stat_mask,
399 stats, _MCDI_PTR(outbuf, 0), false);
400
401 return PTP_STAT_COUNT;
402}
403
404
405static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
406{
407 struct timespec ts = ns_to_timespec(ns);
408 *nic_major = ts.tv_sec;
409 *nic_minor = ts.tv_nsec;
410}
411
412static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
413 s32 correction)
414{
415 ktime_t kt = ktime_set(nic_major, nic_minor);
416 if (correction >= 0)
417 kt = ktime_add_ns(kt, (u64)correction);
418 else
419 kt = ktime_sub_ns(kt, (u64)-correction);
420 return kt;
421}
422
423
424
425
426
427#define S27_TO_NS_SHIFT (27)
428#define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
429#define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
430#define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
431
432
433
434
435static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
436{
437 struct timespec ts = ns_to_timespec(ns);
438 u32 maj = ts.tv_sec;
439 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
440 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
441
442
443
444
445 if (min >= S27_MINOR_MAX) {
446 min -= S27_MINOR_MAX;
447 maj++;
448 }
449
450 *nic_major = maj;
451 *nic_minor = min;
452}
453
454static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
455{
456 u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
457 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
458 return ktime_set(nic_major, ns);
459}
460
461static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
462 s32 correction)
463{
464
465 nic_minor += correction;
466 if ((s32)nic_minor < 0) {
467 nic_minor += S27_MINOR_MAX;
468 nic_major--;
469 } else if (nic_minor >= S27_MINOR_MAX) {
470 nic_minor -= S27_MINOR_MAX;
471 nic_major++;
472 }
473
474 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
475}
476
477
478static int efx_ptp_get_attributes(struct efx_nic *efx)
479{
480 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
481 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
482 struct efx_ptp_data *ptp = efx->ptp_data;
483 int rc;
484 u32 fmt;
485 size_t out_len;
486
487
488
489
490
491 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
492 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
493 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
494 outbuf, sizeof(outbuf), &out_len);
495 if (rc == 0)
496 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
497 else if (rc == -EINVAL)
498 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
499 else
500 return rc;
501
502 if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION) {
503 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
504 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
505 } else if (fmt == MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS) {
506 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
507 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
508 } else {
509 return -ERANGE;
510 }
511
512 ptp->time_format = fmt;
513
514
515
516
517
518
519
520 if (rc == 0 && out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
521 ptp->min_synchronisation_ns =
522 MCDI_DWORD(outbuf,
523 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
524 else
525 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
526
527 return 0;
528}
529
530
531static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
532{
533 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
534 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_LEN);
535 int rc;
536
537
538
539
540 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
541 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
542 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
543
544 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
545 outbuf, sizeof(outbuf), NULL);
546 if (rc == 0) {
547 efx->ptp_data->ts_corrections.tx = MCDI_DWORD(outbuf,
548 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
549 efx->ptp_data->ts_corrections.rx = MCDI_DWORD(outbuf,
550 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
551 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
552 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
553 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
554 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
555 } else if (rc == -EINVAL) {
556 efx->ptp_data->ts_corrections.tx = 0;
557 efx->ptp_data->ts_corrections.rx = 0;
558 efx->ptp_data->ts_corrections.pps_out = 0;
559 efx->ptp_data->ts_corrections.pps_in = 0;
560 } else {
561 return rc;
562 }
563
564 return 0;
565}
566
567
568static int efx_ptp_enable(struct efx_nic *efx)
569{
570 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
571 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
572 int rc;
573
574 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
575 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
576 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
577 efx->ptp_data->channel ?
578 efx->ptp_data->channel->channel : 0);
579 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
580
581 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
582 outbuf, sizeof(outbuf), NULL);
583 rc = (rc == -EALREADY) ? 0 : rc;
584 if (rc)
585 efx_mcdi_display_error(efx, MC_CMD_PTP,
586 MC_CMD_PTP_IN_ENABLE_LEN,
587 outbuf, sizeof(outbuf), rc);
588 return rc;
589}
590
591
592
593
594
595
596static int efx_ptp_disable(struct efx_nic *efx)
597{
598 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
599 MCDI_DECLARE_BUF_OUT_OR_ERR(outbuf, 0);
600 int rc;
601
602 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
603 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
604 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
605 outbuf, sizeof(outbuf), NULL);
606 rc = (rc == -EALREADY) ? 0 : rc;
607 if (rc)
608 efx_mcdi_display_error(efx, MC_CMD_PTP,
609 MC_CMD_PTP_IN_DISABLE_LEN,
610 outbuf, sizeof(outbuf), rc);
611 return rc;
612}
613
614static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
615{
616 struct sk_buff *skb;
617
618 while ((skb = skb_dequeue(q))) {
619 local_bh_disable();
620 netif_receive_skb(skb);
621 local_bh_enable();
622 }
623}
624
625static void efx_ptp_handle_no_channel(struct efx_nic *efx)
626{
627 netif_err(efx, drv, efx->net_dev,
628 "ERROR: PTP requires MSI-X and 1 additional interrupt"
629 "vector. PTP disabled\n");
630}
631
632
633
634
635static void efx_ptp_send_times(struct efx_nic *efx,
636 struct pps_event_time *last_time)
637{
638 struct pps_event_time now;
639 struct timespec limit;
640 struct efx_ptp_data *ptp = efx->ptp_data;
641 struct timespec start;
642 int *mc_running = ptp->start.addr;
643
644 pps_get_ts(&now);
645 start = now.ts_real;
646 limit = now.ts_real;
647 timespec_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
648
649
650 while ((timespec_compare(&now.ts_real, &limit) < 0) &&
651 ACCESS_ONCE(*mc_running)) {
652 struct timespec update_time;
653 unsigned int host_time;
654
655
656 update_time = now.ts_real;
657 timespec_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
658 do {
659 pps_get_ts(&now);
660 } while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
661 ACCESS_ONCE(*mc_running));
662
663
664 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
665 now.ts_real.tv_nsec);
666
667 efx->type->ptp_write_host_time(efx, host_time);
668 }
669 *last_time = now;
670}
671
672
673static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
674 struct efx_ptp_timeset *timeset)
675{
676 unsigned start_ns, end_ns;
677
678 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
679 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
680 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
681 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
682 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
683
684
685 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
686 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
687
688 if (end_ns < start_ns)
689 end_ns += NSEC_PER_SEC;
690
691 timeset->window = end_ns - start_ns;
692}
693
694
695
696
697
698
699
700
701
702static int
703efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
704 size_t response_length,
705 const struct pps_event_time *last_time)
706{
707 unsigned number_readings =
708 MCDI_VAR_ARRAY_LEN(response_length,
709 PTP_OUT_SYNCHRONIZE_TIMESET);
710 unsigned i;
711 unsigned ngood = 0;
712 unsigned last_good = 0;
713 struct efx_ptp_data *ptp = efx->ptp_data;
714 u32 last_sec;
715 u32 start_sec;
716 struct timespec delta;
717 ktime_t mc_time;
718
719 if (number_readings == 0)
720 return -EAGAIN;
721
722
723
724
725
726
727
728 for (i = 0; i < number_readings; i++) {
729 s32 window, corrected;
730 struct timespec wait;
731
732 efx_ptp_read_timeset(
733 MCDI_ARRAY_STRUCT_PTR(synch_buf,
734 PTP_OUT_SYNCHRONIZE_TIMESET, i),
735 &ptp->timeset[i]);
736
737 wait = ktime_to_timespec(
738 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
739 window = ptp->timeset[i].window;
740 corrected = window - wait.tv_nsec;
741
742
743
744
745
746
747
748
749
750
751 if (window < SYNCHRONISATION_GRANULARITY_NS) {
752 ++ptp->invalid_sync_windows;
753 } else if (corrected >= MAX_SYNCHRONISATION_NS) {
754 ++ptp->oversize_sync_windows;
755 } else if (corrected < ptp->min_synchronisation_ns) {
756 ++ptp->undersize_sync_windows;
757 } else {
758 ngood++;
759 last_good = i;
760 }
761 }
762
763 if (ngood == 0) {
764 netif_warn(efx, drv, efx->net_dev,
765 "PTP no suitable synchronisations\n");
766 return -EAGAIN;
767 }
768
769
770
771
772
773
774
775 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
776 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
777 if (start_sec != last_sec &&
778 ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
779 netif_warn(efx, hw, efx->net_dev,
780 "PTP bad synchronisation seconds\n");
781 return -EAGAIN;
782 }
783 delta.tv_sec = (last_sec - start_sec) & 1;
784 delta.tv_nsec =
785 last_time->ts_real.tv_nsec -
786 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
787
788
789
790
791
792 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
793 ptp->timeset[last_good].minor, 0);
794
795
796 delta.tv_nsec += ktime_to_timespec(mc_time).tv_nsec;
797
798
799 ptp->host_time_pps = *last_time;
800 pps_sub_ts(&ptp->host_time_pps, delta);
801
802 return 0;
803}
804
805
806static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
807{
808 struct efx_ptp_data *ptp = efx->ptp_data;
809 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
810 size_t response_length;
811 int rc;
812 unsigned long timeout;
813 struct pps_event_time last_time = {};
814 unsigned int loops = 0;
815 int *start = ptp->start.addr;
816
817 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
818 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
819 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
820 num_readings);
821 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
822 ptp->start.dma_addr);
823
824
825 ACCESS_ONCE(*start) = 0;
826 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
827 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
828 EFX_BUG_ON_PARANOID(rc);
829
830
831 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
832 while (!ACCESS_ONCE(*start) && (time_before(jiffies, timeout))) {
833 udelay(20);
834 loops++;
835 }
836
837 if (loops <= 1)
838 ++ptp->fast_syncs;
839 if (!time_before(jiffies, timeout))
840 ++ptp->sync_timeouts;
841
842 if (ACCESS_ONCE(*start))
843 efx_ptp_send_times(efx, &last_time);
844
845
846 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
847 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
848 synch_buf, sizeof(synch_buf),
849 &response_length);
850 if (rc == 0) {
851 rc = efx_ptp_process_times(efx, synch_buf, response_length,
852 &last_time);
853 if (rc == 0)
854 ++ptp->good_syncs;
855 else
856 ++ptp->no_time_syncs;
857 }
858
859
860
861
862 if (rc != 0)
863 ++ptp->bad_syncs;
864
865 return rc;
866}
867
868
869static int efx_ptp_xmit_skb(struct efx_nic *efx, struct sk_buff *skb)
870{
871 struct efx_ptp_data *ptp_data = efx->ptp_data;
872 struct skb_shared_hwtstamps timestamps;
873 int rc = -EIO;
874 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
875 size_t len;
876
877 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
878 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
879 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
880 if (skb_shinfo(skb)->nr_frags != 0) {
881 rc = skb_linearize(skb);
882 if (rc != 0)
883 goto fail;
884 }
885
886 if (skb->ip_summed == CHECKSUM_PARTIAL) {
887 rc = skb_checksum_help(skb);
888 if (rc != 0)
889 goto fail;
890 }
891 skb_copy_from_linear_data(skb,
892 MCDI_PTR(ptp_data->txbuf,
893 PTP_IN_TRANSMIT_PACKET),
894 skb->len);
895 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
896 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
897 txtime, sizeof(txtime), &len);
898 if (rc != 0)
899 goto fail;
900
901 memset(×tamps, 0, sizeof(timestamps));
902 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
903 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
904 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
905 ptp_data->ts_corrections.tx);
906
907 skb_tstamp_tx(skb, ×tamps);
908
909 rc = 0;
910
911fail:
912 dev_kfree_skb(skb);
913
914 return rc;
915}
916
917static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
918{
919 struct efx_ptp_data *ptp = efx->ptp_data;
920 struct list_head *cursor;
921 struct list_head *next;
922
923 if (ptp->rx_ts_inline)
924 return;
925
926
927 spin_lock_bh(&ptp->evt_lock);
928 if (!list_empty(&ptp->evt_list)) {
929 list_for_each_safe(cursor, next, &ptp->evt_list) {
930 struct efx_ptp_event_rx *evt;
931
932 evt = list_entry(cursor, struct efx_ptp_event_rx,
933 link);
934 if (time_after(jiffies, evt->expiry)) {
935 list_move(&evt->link, &ptp->evt_free_list);
936 netif_warn(efx, hw, efx->net_dev,
937 "PTP rx event dropped\n");
938 }
939 }
940 }
941 spin_unlock_bh(&ptp->evt_lock);
942}
943
944static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
945 struct sk_buff *skb)
946{
947 struct efx_ptp_data *ptp = efx->ptp_data;
948 bool evts_waiting;
949 struct list_head *cursor;
950 struct list_head *next;
951 struct efx_ptp_match *match;
952 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
953
954 WARN_ON_ONCE(ptp->rx_ts_inline);
955
956 spin_lock_bh(&ptp->evt_lock);
957 evts_waiting = !list_empty(&ptp->evt_list);
958 spin_unlock_bh(&ptp->evt_lock);
959
960 if (!evts_waiting)
961 return PTP_PACKET_STATE_UNMATCHED;
962
963 match = (struct efx_ptp_match *)skb->cb;
964
965 spin_lock_bh(&ptp->evt_lock);
966 list_for_each_safe(cursor, next, &ptp->evt_list) {
967 struct efx_ptp_event_rx *evt;
968
969 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
970 if ((evt->seq0 == match->words[0]) &&
971 (evt->seq1 == match->words[1])) {
972 struct skb_shared_hwtstamps *timestamps;
973
974
975 timestamps = skb_hwtstamps(skb);
976 timestamps->hwtstamp = evt->hwtimestamp;
977
978 match->state = PTP_PACKET_STATE_MATCHED;
979 rc = PTP_PACKET_STATE_MATCHED;
980 list_move(&evt->link, &ptp->evt_free_list);
981 break;
982 }
983 }
984 spin_unlock_bh(&ptp->evt_lock);
985
986 return rc;
987}
988
989
990
991
992
993static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
994{
995 struct efx_ptp_data *ptp = efx->ptp_data;
996 struct sk_buff *skb;
997
998 while ((skb = skb_dequeue(&ptp->rxq))) {
999 struct efx_ptp_match *match;
1000
1001 match = (struct efx_ptp_match *)skb->cb;
1002 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1003 __skb_queue_tail(q, skb);
1004 } else if (efx_ptp_match_rx(efx, skb) ==
1005 PTP_PACKET_STATE_MATCHED) {
1006 __skb_queue_tail(q, skb);
1007 } else if (time_after(jiffies, match->expiry)) {
1008 match->state = PTP_PACKET_STATE_TIMED_OUT;
1009 ++ptp->rx_no_timestamp;
1010 __skb_queue_tail(q, skb);
1011 } else {
1012
1013 skb_queue_head(&ptp->rxq, skb);
1014 break;
1015 }
1016 }
1017}
1018
1019
1020static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1021{
1022 local_bh_disable();
1023 netif_receive_skb(skb);
1024 local_bh_enable();
1025}
1026
1027static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1028{
1029 struct efx_ptp_data *ptp = efx->ptp_data;
1030
1031 if (ptp->rxfilter_installed) {
1032 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1033 ptp->rxfilter_general);
1034 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1035 ptp->rxfilter_event);
1036 ptp->rxfilter_installed = false;
1037 }
1038}
1039
1040static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
1041{
1042 struct efx_ptp_data *ptp = efx->ptp_data;
1043 struct efx_filter_spec rxfilter;
1044 int rc;
1045
1046 if (!ptp->channel || ptp->rxfilter_installed)
1047 return 0;
1048
1049
1050
1051
1052 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1053 efx_rx_queue_index(
1054 efx_channel_get_rx_queue(ptp->channel)));
1055 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1056 htonl(PTP_ADDRESS),
1057 htons(PTP_EVENT_PORT));
1058 if (rc != 0)
1059 return rc;
1060
1061 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1062 if (rc < 0)
1063 return rc;
1064 ptp->rxfilter_event = rc;
1065
1066 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1067 efx_rx_queue_index(
1068 efx_channel_get_rx_queue(ptp->channel)));
1069 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1070 htonl(PTP_ADDRESS),
1071 htons(PTP_GENERAL_PORT));
1072 if (rc != 0)
1073 goto fail;
1074
1075 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1076 if (rc < 0)
1077 goto fail;
1078 ptp->rxfilter_general = rc;
1079
1080 ptp->rxfilter_installed = true;
1081 return 0;
1082
1083fail:
1084 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1085 ptp->rxfilter_event);
1086 return rc;
1087}
1088
1089static int efx_ptp_start(struct efx_nic *efx)
1090{
1091 struct efx_ptp_data *ptp = efx->ptp_data;
1092 int rc;
1093
1094 ptp->reset_required = false;
1095
1096 rc = efx_ptp_insert_multicast_filters(efx);
1097 if (rc)
1098 return rc;
1099
1100 rc = efx_ptp_enable(efx);
1101 if (rc != 0)
1102 goto fail;
1103
1104 ptp->evt_frag_idx = 0;
1105 ptp->current_adjfreq = 0;
1106
1107 return 0;
1108
1109fail:
1110 efx_ptp_remove_multicast_filters(efx);
1111 return rc;
1112}
1113
1114static int efx_ptp_stop(struct efx_nic *efx)
1115{
1116 struct efx_ptp_data *ptp = efx->ptp_data;
1117 struct list_head *cursor;
1118 struct list_head *next;
1119 int rc;
1120
1121 if (ptp == NULL)
1122 return 0;
1123
1124 rc = efx_ptp_disable(efx);
1125
1126 efx_ptp_remove_multicast_filters(efx);
1127
1128
1129 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1130 skb_queue_purge(&efx->ptp_data->txq);
1131
1132
1133 spin_lock_bh(&efx->ptp_data->evt_lock);
1134 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
1135 list_move(cursor, &efx->ptp_data->evt_free_list);
1136 }
1137 spin_unlock_bh(&efx->ptp_data->evt_lock);
1138
1139 return rc;
1140}
1141
1142static int efx_ptp_restart(struct efx_nic *efx)
1143{
1144 if (efx->ptp_data && efx->ptp_data->enabled)
1145 return efx_ptp_start(efx);
1146 return 0;
1147}
1148
1149static void efx_ptp_pps_worker(struct work_struct *work)
1150{
1151 struct efx_ptp_data *ptp =
1152 container_of(work, struct efx_ptp_data, pps_work);
1153 struct efx_nic *efx = ptp->efx;
1154 struct ptp_clock_event ptp_evt;
1155
1156 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1157 return;
1158
1159 ptp_evt.type = PTP_CLOCK_PPSUSR;
1160 ptp_evt.pps_times = ptp->host_time_pps;
1161 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1162}
1163
1164static void efx_ptp_worker(struct work_struct *work)
1165{
1166 struct efx_ptp_data *ptp_data =
1167 container_of(work, struct efx_ptp_data, work);
1168 struct efx_nic *efx = ptp_data->efx;
1169 struct sk_buff *skb;
1170 struct sk_buff_head tempq;
1171
1172 if (ptp_data->reset_required) {
1173 efx_ptp_stop(efx);
1174 efx_ptp_start(efx);
1175 return;
1176 }
1177
1178 efx_ptp_drop_time_expired_events(efx);
1179
1180 __skb_queue_head_init(&tempq);
1181 efx_ptp_process_events(efx, &tempq);
1182
1183 while ((skb = skb_dequeue(&ptp_data->txq)))
1184 efx_ptp_xmit_skb(efx, skb);
1185
1186 while ((skb = __skb_dequeue(&tempq)))
1187 efx_ptp_process_rx(efx, skb);
1188}
1189
1190static const struct ptp_clock_info efx_phc_clock_info = {
1191 .owner = THIS_MODULE,
1192 .name = "sfc",
1193 .max_adj = MAX_PPB,
1194 .n_alarm = 0,
1195 .n_ext_ts = 0,
1196 .n_per_out = 0,
1197 .n_pins = 0,
1198 .pps = 1,
1199 .adjfreq = efx_phc_adjfreq,
1200 .adjtime = efx_phc_adjtime,
1201 .gettime64 = efx_phc_gettime,
1202 .settime64 = efx_phc_settime,
1203 .enable = efx_phc_enable,
1204};
1205
1206
1207int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
1208{
1209 struct efx_ptp_data *ptp;
1210 int rc = 0;
1211 unsigned int pos;
1212
1213 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1214 efx->ptp_data = ptp;
1215 if (!efx->ptp_data)
1216 return -ENOMEM;
1217
1218 ptp->efx = efx;
1219 ptp->channel = channel;
1220 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1221
1222 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
1223 if (rc != 0)
1224 goto fail1;
1225
1226 skb_queue_head_init(&ptp->rxq);
1227 skb_queue_head_init(&ptp->txq);
1228 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1229 if (!ptp->workwq) {
1230 rc = -ENOMEM;
1231 goto fail2;
1232 }
1233
1234 INIT_WORK(&ptp->work, efx_ptp_worker);
1235 ptp->config.flags = 0;
1236 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1237 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1238 INIT_LIST_HEAD(&ptp->evt_list);
1239 INIT_LIST_HEAD(&ptp->evt_free_list);
1240 spin_lock_init(&ptp->evt_lock);
1241 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1242 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1243
1244
1245 rc = efx_ptp_get_attributes(efx);
1246 if (rc < 0)
1247 goto fail3;
1248
1249
1250 rc = efx_ptp_get_timestamp_corrections(efx);
1251 if (rc < 0)
1252 goto fail3;
1253
1254 if (efx->mcdi->fn_flags &
1255 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1256 ptp->phc_clock_info = efx_phc_clock_info;
1257 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1258 &efx->pci_dev->dev);
1259 if (IS_ERR(ptp->phc_clock)) {
1260 rc = PTR_ERR(ptp->phc_clock);
1261 goto fail3;
1262 }
1263
1264 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1265 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1266 if (!ptp->pps_workwq) {
1267 rc = -ENOMEM;
1268 goto fail4;
1269 }
1270 }
1271 ptp->nic_ts_enabled = false;
1272
1273 return 0;
1274fail4:
1275 ptp_clock_unregister(efx->ptp_data->phc_clock);
1276
1277fail3:
1278 destroy_workqueue(efx->ptp_data->workwq);
1279
1280fail2:
1281 efx_nic_free_buffer(efx, &ptp->start);
1282
1283fail1:
1284 kfree(efx->ptp_data);
1285 efx->ptp_data = NULL;
1286
1287 return rc;
1288}
1289
1290
1291
1292
1293
1294
1295static int efx_ptp_probe_channel(struct efx_channel *channel)
1296{
1297 struct efx_nic *efx = channel->efx;
1298
1299 channel->irq_moderation = 0;
1300 channel->rx_queue.core_index = 0;
1301
1302 return efx_ptp_probe(efx, channel);
1303}
1304
1305void efx_ptp_remove(struct efx_nic *efx)
1306{
1307 if (!efx->ptp_data)
1308 return;
1309
1310 (void)efx_ptp_disable(efx);
1311
1312 cancel_work_sync(&efx->ptp_data->work);
1313 cancel_work_sync(&efx->ptp_data->pps_work);
1314
1315 skb_queue_purge(&efx->ptp_data->rxq);
1316 skb_queue_purge(&efx->ptp_data->txq);
1317
1318 if (efx->ptp_data->phc_clock) {
1319 destroy_workqueue(efx->ptp_data->pps_workwq);
1320 ptp_clock_unregister(efx->ptp_data->phc_clock);
1321 }
1322
1323 destroy_workqueue(efx->ptp_data->workwq);
1324
1325 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1326 kfree(efx->ptp_data);
1327}
1328
1329static void efx_ptp_remove_channel(struct efx_channel *channel)
1330{
1331 efx_ptp_remove(channel->efx);
1332}
1333
1334static void efx_ptp_get_channel_name(struct efx_channel *channel,
1335 char *buf, size_t len)
1336{
1337 snprintf(buf, len, "%s-ptp", channel->efx->name);
1338}
1339
1340
1341
1342
1343bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1344{
1345 return efx->ptp_data &&
1346 efx->ptp_data->enabled &&
1347 skb->len >= PTP_MIN_LENGTH &&
1348 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1349 likely(skb->protocol == htons(ETH_P_IP)) &&
1350 skb_transport_header_was_set(skb) &&
1351 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
1352 ip_hdr(skb)->protocol == IPPROTO_UDP &&
1353 skb_headlen(skb) >=
1354 skb_transport_offset(skb) + sizeof(struct udphdr) &&
1355 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1356}
1357
1358
1359
1360
1361
1362static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
1363{
1364 struct efx_nic *efx = channel->efx;
1365 struct efx_ptp_data *ptp = efx->ptp_data;
1366 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
1367 u8 *match_data_012, *match_data_345;
1368 unsigned int version;
1369 u8 *data;
1370
1371 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1372
1373
1374 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
1375 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
1376 return false;
1377 }
1378 data = skb->data;
1379 version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
1380 if (version != PTP_VERSION_V1) {
1381 return false;
1382 }
1383
1384
1385
1386
1387 match_data_012 = data + PTP_V1_UUID_OFFSET;
1388 match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
1389 } else {
1390 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
1391 return false;
1392 }
1393 data = skb->data;
1394 version = data[PTP_V2_VERSION_OFFSET];
1395 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
1396 return false;
1397 }
1398
1399
1400
1401
1402
1403
1404
1405
1406 match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
1407 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1408 match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
1409 } else {
1410 match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
1411 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1412 }
1413 }
1414
1415
1416 if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1417 match->state = PTP_PACKET_STATE_UNMATCHED;
1418
1419
1420
1421
1422 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1423 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1424
1425
1426 match->words[0] = (match_data_012[0] |
1427 (match_data_012[1] << 8) |
1428 (match_data_012[2] << 16) |
1429 (match_data_345[0] << 24));
1430 match->words[1] = (match_data_345[1] |
1431 (match_data_345[2] << 8) |
1432 (data[PTP_V1_SEQUENCE_OFFSET +
1433 PTP_V1_SEQUENCE_LENGTH - 1] <<
1434 16));
1435 } else {
1436 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1437 }
1438
1439 skb_queue_tail(&ptp->rxq, skb);
1440 queue_work(ptp->workwq, &ptp->work);
1441
1442 return true;
1443}
1444
1445
1446
1447
1448
1449int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1450{
1451 struct efx_ptp_data *ptp = efx->ptp_data;
1452
1453 skb_queue_tail(&ptp->txq, skb);
1454
1455 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1456 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1457 efx_xmit_hwtstamp_pending(skb);
1458 queue_work(ptp->workwq, &ptp->work);
1459
1460 return NETDEV_TX_OK;
1461}
1462
1463int efx_ptp_get_mode(struct efx_nic *efx)
1464{
1465 return efx->ptp_data->mode;
1466}
1467
1468int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1469 unsigned int new_mode)
1470{
1471 if ((enable_wanted != efx->ptp_data->enabled) ||
1472 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1473 int rc = 0;
1474
1475 if (enable_wanted) {
1476
1477 if (efx->ptp_data->enabled &&
1478 (efx->ptp_data->mode != new_mode)) {
1479 efx->ptp_data->enabled = false;
1480 rc = efx_ptp_stop(efx);
1481 if (rc != 0)
1482 return rc;
1483 }
1484
1485
1486
1487
1488
1489 efx->ptp_data->mode = new_mode;
1490 if (netif_running(efx->net_dev))
1491 rc = efx_ptp_start(efx);
1492 if (rc == 0) {
1493 rc = efx_ptp_synchronize(efx,
1494 PTP_SYNC_ATTEMPTS * 2);
1495 if (rc != 0)
1496 efx_ptp_stop(efx);
1497 }
1498 } else {
1499 rc = efx_ptp_stop(efx);
1500 }
1501
1502 if (rc != 0)
1503 return rc;
1504
1505 efx->ptp_data->enabled = enable_wanted;
1506 }
1507
1508 return 0;
1509}
1510
1511static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1512{
1513 int rc;
1514
1515 if (init->flags)
1516 return -EINVAL;
1517
1518 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1519 (init->tx_type != HWTSTAMP_TX_ON))
1520 return -ERANGE;
1521
1522 rc = efx->type->ptp_set_ts_config(efx, init);
1523 if (rc)
1524 return rc;
1525
1526 efx->ptp_data->config = *init;
1527 return 0;
1528}
1529
1530void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
1531{
1532 struct efx_ptp_data *ptp = efx->ptp_data;
1533 struct efx_nic *primary = efx->primary;
1534
1535 ASSERT_RTNL();
1536
1537 if (!ptp)
1538 return;
1539
1540 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1541 SOF_TIMESTAMPING_RX_HARDWARE |
1542 SOF_TIMESTAMPING_RAW_HARDWARE);
1543 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1544 ts_info->phc_index =
1545 ptp_clock_index(primary->ptp_data->phc_clock);
1546 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1547 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
1548}
1549
1550int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1551{
1552 struct hwtstamp_config config;
1553 int rc;
1554
1555
1556 if (!efx->ptp_data)
1557 return -EOPNOTSUPP;
1558
1559 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1560 return -EFAULT;
1561
1562 rc = efx_ptp_ts_init(efx, &config);
1563 if (rc != 0)
1564 return rc;
1565
1566 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1567 ? -EFAULT : 0;
1568}
1569
1570int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1571{
1572 if (!efx->ptp_data)
1573 return -EOPNOTSUPP;
1574
1575 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1576 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1577}
1578
1579static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1580{
1581 struct efx_ptp_data *ptp = efx->ptp_data;
1582
1583 netif_err(efx, hw, efx->net_dev,
1584 "PTP unexpected event length: got %d expected %d\n",
1585 ptp->evt_frag_idx, expected_frag_len);
1586 ptp->reset_required = true;
1587 queue_work(ptp->workwq, &ptp->work);
1588}
1589
1590
1591
1592
1593
1594static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1595{
1596 struct efx_ptp_event_rx *evt = NULL;
1597
1598 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1599 return;
1600
1601 if (ptp->evt_frag_idx != 3) {
1602 ptp_event_failure(efx, 3);
1603 return;
1604 }
1605
1606 spin_lock_bh(&ptp->evt_lock);
1607 if (!list_empty(&ptp->evt_free_list)) {
1608 evt = list_first_entry(&ptp->evt_free_list,
1609 struct efx_ptp_event_rx, link);
1610 list_del(&evt->link);
1611
1612 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1613 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1614 MCDI_EVENT_SRC) |
1615 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1616 MCDI_EVENT_SRC) << 8) |
1617 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1618 MCDI_EVENT_SRC) << 16));
1619 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
1620 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1621 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1622 ptp->ts_corrections.rx);
1623 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1624 list_add_tail(&evt->link, &ptp->evt_list);
1625
1626 queue_work(ptp->workwq, &ptp->work);
1627 } else if (net_ratelimit()) {
1628
1629 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1630 }
1631 spin_unlock_bh(&ptp->evt_lock);
1632}
1633
1634static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1635{
1636 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1637 if (ptp->evt_frag_idx != 1) {
1638 ptp_event_failure(efx, 1);
1639 return;
1640 }
1641
1642 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1643}
1644
1645static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1646{
1647 if (ptp->nic_ts_enabled)
1648 queue_work(ptp->pps_workwq, &ptp->pps_work);
1649}
1650
1651void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1652{
1653 struct efx_ptp_data *ptp = efx->ptp_data;
1654 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1655
1656 if (!ptp) {
1657 if (net_ratelimit())
1658 netif_warn(efx, drv, efx->net_dev,
1659 "Received PTP event but PTP not set up\n");
1660 return;
1661 }
1662
1663 if (!ptp->enabled)
1664 return;
1665
1666 if (ptp->evt_frag_idx == 0) {
1667 ptp->evt_code = code;
1668 } else if (ptp->evt_code != code) {
1669 netif_err(efx, hw, efx->net_dev,
1670 "PTP out of sequence event %d\n", code);
1671 ptp->evt_frag_idx = 0;
1672 }
1673
1674 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1675 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1676
1677 switch (code) {
1678 case MCDI_EVENT_CODE_PTP_RX:
1679 ptp_event_rx(efx, ptp);
1680 break;
1681 case MCDI_EVENT_CODE_PTP_FAULT:
1682 ptp_event_fault(efx, ptp);
1683 break;
1684 case MCDI_EVENT_CODE_PTP_PPS:
1685 ptp_event_pps(efx, ptp);
1686 break;
1687 default:
1688 netif_err(efx, hw, efx->net_dev,
1689 "PTP unknown event %d\n", code);
1690 break;
1691 }
1692 ptp->evt_frag_idx = 0;
1693 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1694 netif_err(efx, hw, efx->net_dev,
1695 "PTP too many event fragments\n");
1696 ptp->evt_frag_idx = 0;
1697 }
1698}
1699
1700void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1701{
1702 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
1703 channel->sync_timestamp_minor =
1704 MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_26_19) << 19;
1705
1706
1707
1708 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
1709 SYNC_EVENTS_VALID);
1710}
1711
1712
1713
1714
1715
1716#define MINOR_TICKS_PER_SECOND 0x8000000
1717
1718#define FUZZ (MINOR_TICKS_PER_SECOND / 10)
1719#define EXPECTED_SYNC_EVENTS_PER_SECOND 4
1720
1721static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
1722{
1723#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1724 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
1725#else
1726 const u8 *data = eh + efx->rx_packet_ts_offset;
1727 return (u32)data[0] |
1728 (u32)data[1] << 8 |
1729 (u32)data[2] << 16 |
1730 (u32)data[3] << 24;
1731#endif
1732}
1733
1734void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
1735 struct sk_buff *skb)
1736{
1737 struct efx_nic *efx = channel->efx;
1738 u32 pkt_timestamp_major, pkt_timestamp_minor;
1739 u32 diff, carry;
1740 struct skb_shared_hwtstamps *timestamps;
1741
1742 pkt_timestamp_minor = (efx_rx_buf_timestamp_minor(efx,
1743 skb_mac_header(skb)) +
1744 (u32) efx->ptp_data->ts_corrections.rx) &
1745 (MINOR_TICKS_PER_SECOND - 1);
1746
1747
1748
1749
1750 diff = (pkt_timestamp_minor - channel->sync_timestamp_minor) &
1751 (MINOR_TICKS_PER_SECOND - 1);
1752
1753 carry = channel->sync_timestamp_minor + diff > MINOR_TICKS_PER_SECOND ?
1754 1 : 0;
1755
1756 if (diff <= MINOR_TICKS_PER_SECOND / EXPECTED_SYNC_EVENTS_PER_SECOND +
1757 FUZZ) {
1758
1759
1760
1761 pkt_timestamp_major = channel->sync_timestamp_major + carry;
1762 } else if (diff >= MINOR_TICKS_PER_SECOND - FUZZ) {
1763
1764
1765
1766
1767 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
1768 } else {
1769
1770
1771
1772
1773
1774 netif_vdbg(efx, drv, efx->net_dev,
1775 "packet timestamp %x too far from sync event %x:%x\n",
1776 pkt_timestamp_minor, channel->sync_timestamp_major,
1777 channel->sync_timestamp_minor);
1778 return;
1779 }
1780
1781
1782 timestamps = skb_hwtstamps(skb);
1783 timestamps->hwtstamp =
1784 efx_ptp_s27_to_ktime(pkt_timestamp_major, pkt_timestamp_minor);
1785}
1786
1787static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
1788{
1789 struct efx_ptp_data *ptp_data = container_of(ptp,
1790 struct efx_ptp_data,
1791 phc_clock_info);
1792 struct efx_nic *efx = ptp_data->efx;
1793 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
1794 s64 adjustment_ns;
1795 int rc;
1796
1797 if (delta > MAX_PPB)
1798 delta = MAX_PPB;
1799 else if (delta < -MAX_PPB)
1800 delta = -MAX_PPB;
1801
1802
1803 adjustment_ns = (((s64)delta * PPB_SCALE_WORD) >>
1804 (PPB_EXTRA_BITS + MAX_PPB_BITS));
1805
1806 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
1807 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
1808 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
1809 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
1810 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
1811 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
1812 NULL, 0, NULL);
1813 if (rc != 0)
1814 return rc;
1815
1816 ptp_data->current_adjfreq = adjustment_ns;
1817 return 0;
1818}
1819
1820static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
1821{
1822 u32 nic_major, nic_minor;
1823 struct efx_ptp_data *ptp_data = container_of(ptp,
1824 struct efx_ptp_data,
1825 phc_clock_info);
1826 struct efx_nic *efx = ptp_data->efx;
1827 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
1828
1829 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
1830
1831 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
1832 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
1833 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
1834 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
1835 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
1836 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1837 NULL, 0, NULL);
1838}
1839
1840static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1841{
1842 struct efx_ptp_data *ptp_data = container_of(ptp,
1843 struct efx_ptp_data,
1844 phc_clock_info);
1845 struct efx_nic *efx = ptp_data->efx;
1846 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
1847 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
1848 int rc;
1849 ktime_t kt;
1850
1851 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
1852 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
1853
1854 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
1855 outbuf, sizeof(outbuf), NULL);
1856 if (rc != 0)
1857 return rc;
1858
1859 kt = ptp_data->nic_to_kernel_time(
1860 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
1861 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
1862 *ts = ktime_to_timespec64(kt);
1863 return 0;
1864}
1865
1866static int efx_phc_settime(struct ptp_clock_info *ptp,
1867 const struct timespec64 *e_ts)
1868{
1869
1870
1871
1872
1873 int rc;
1874 struct timespec64 time_now;
1875 struct timespec64 delta;
1876
1877 rc = efx_phc_gettime(ptp, &time_now);
1878 if (rc != 0)
1879 return rc;
1880
1881 delta = timespec64_sub(*e_ts, time_now);
1882
1883 rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
1884 if (rc != 0)
1885 return rc;
1886
1887 return 0;
1888}
1889
1890static int efx_phc_enable(struct ptp_clock_info *ptp,
1891 struct ptp_clock_request *request,
1892 int enable)
1893{
1894 struct efx_ptp_data *ptp_data = container_of(ptp,
1895 struct efx_ptp_data,
1896 phc_clock_info);
1897 if (request->type != PTP_CLK_REQ_PPS)
1898 return -EOPNOTSUPP;
1899
1900 ptp_data->nic_ts_enabled = !!enable;
1901 return 0;
1902}
1903
1904static const struct efx_channel_type efx_ptp_channel_type = {
1905 .handle_no_channel = efx_ptp_handle_no_channel,
1906 .pre_probe = efx_ptp_probe_channel,
1907 .post_remove = efx_ptp_remove_channel,
1908 .get_name = efx_ptp_get_channel_name,
1909
1910 .receive_skb = efx_ptp_rx,
1911 .keep_eventq = false,
1912};
1913
1914void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
1915{
1916
1917
1918
1919 if (efx_ptp_disable(efx) == 0)
1920 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
1921 &efx_ptp_channel_type;
1922}
1923
1924void efx_ptp_start_datapath(struct efx_nic *efx)
1925{
1926 if (efx_ptp_restart(efx))
1927 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
1928
1929 if (efx->type->ptp_set_ts_sync_events)
1930 efx->type->ptp_set_ts_sync_events(efx, true, true);
1931}
1932
1933void efx_ptp_stop_datapath(struct efx_nic *efx)
1934{
1935
1936 if (efx->type->ptp_set_ts_sync_events)
1937 efx->type->ptp_set_ts_sync_events(efx, false, true);
1938 efx_ptp_stop(efx);
1939}
1940