0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include <linux/ip.h>
0034 #include <linux/udp.h>
0035 #include <linux/time.h>
0036 #include <linux/ktime.h>
0037 #include <linux/module.h>
0038 #include <linux/pps_kernel.h>
0039 #include <linux/ptp_clock_kernel.h>
0040 #include "net_driver.h"
0041 #include "efx.h"
0042 #include "mcdi.h"
0043 #include "mcdi_pcol.h"
0044 #include "io.h"
0045 #include "farch_regs.h"
0046 #include "tx.h"
0047 #include "nic.h" /* indirectly includes ptp.h */
0048 #include "efx_channels.h"
0049
0050
0051 #define MAX_EVENT_FRAGS 3
0052
0053
0054 #define MAX_SYNCHRONISE_WAIT_MS 2
0055
0056
0057 #define SYNCHRONISE_PERIOD_NS 250000
0058
0059
0060 #define SYNCHRONISATION_GRANULARITY_NS 200
0061
0062
0063 #define DEFAULT_MIN_SYNCHRONISATION_NS 120
0064
0065
0066 #define MAX_SYNCHRONISATION_NS 1000
0067
0068
0069 #define MAX_RECEIVE_EVENTS 8
0070
0071
0072 #define AVERAGE_LENGTH 16
0073
0074
0075 #define PKT_EVENT_LIFETIME_MS 10
0076
0077
0078
0079
0080
0081 #define PTP_DPORT_OFFSET 22
0082
0083 #define PTP_V1_VERSION_LENGTH 2
0084 #define PTP_V1_VERSION_OFFSET 28
0085
0086 #define PTP_V1_UUID_LENGTH 6
0087 #define PTP_V1_UUID_OFFSET 50
0088
0089 #define PTP_V1_SEQUENCE_LENGTH 2
0090 #define PTP_V1_SEQUENCE_OFFSET 58
0091
0092
0093
0094
0095 #define PTP_V1_MIN_LENGTH 64
0096
0097 #define PTP_V2_VERSION_LENGTH 1
0098 #define PTP_V2_VERSION_OFFSET 29
0099
0100 #define PTP_V2_UUID_LENGTH 8
0101 #define PTP_V2_UUID_OFFSET 48
0102
0103
0104
0105
0106
0107
0108 #define PTP_V2_MC_UUID_LENGTH 6
0109 #define PTP_V2_MC_UUID_OFFSET 50
0110
0111 #define PTP_V2_SEQUENCE_LENGTH 2
0112 #define PTP_V2_SEQUENCE_OFFSET 58
0113
0114
0115
0116
0117 #define PTP_V2_MIN_LENGTH 63
0118
0119 #define PTP_MIN_LENGTH 63
0120
0121 #define PTP_ADDRESS 0xe0000181
0122 #define PTP_EVENT_PORT 319
0123 #define PTP_GENERAL_PORT 320
0124
0125
0126
0127
0128 #define PTP_VERSION_V1 1
0129
0130 #define PTP_VERSION_V2 2
0131 #define PTP_VERSION_V2_MASK 0x0f
0132
0133 enum ptp_packet_state {
0134 PTP_PACKET_STATE_UNMATCHED = 0,
0135 PTP_PACKET_STATE_MATCHED,
0136 PTP_PACKET_STATE_TIMED_OUT,
0137 PTP_PACKET_STATE_MATCH_UNWANTED
0138 };
0139
0140
0141
0142
0143 #define MC_NANOSECOND_BITS 30
0144 #define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1)
0145 #define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1)
0146
0147
0148 #define MAX_PPB 1000000
0149
0150
0151
0152 #define PPB_SCALE_WORD ((1LL << (57)) / 1953125LL)
0153
0154
0155 #define PPB_SHIFT_FP40 26
0156
0157 #define PPB_SHIFT_FP44 22
0158
0159 #define PTP_SYNC_ATTEMPTS 4
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169 struct efx_ptp_match {
0170 u32 words[DIV_ROUND_UP(PTP_V1_UUID_LENGTH, 4)];
0171 unsigned long expiry;
0172 enum ptp_packet_state state;
0173 };
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 struct efx_ptp_event_rx {
0184 struct list_head link;
0185 u32 seq0;
0186 u32 seq1;
0187 ktime_t hwtimestamp;
0188 unsigned long expiry;
0189 };
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 struct efx_ptp_timeset {
0203 u32 host_start;
0204 u32 major;
0205 u32 minor;
0206 u32 host_end;
0207 u32 wait;
0208 u32 window;
0209 };
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285 struct efx_ptp_data {
0286 struct efx_nic *efx;
0287 struct efx_channel *channel;
0288 bool rx_ts_inline;
0289 struct sk_buff_head rxq;
0290 struct sk_buff_head txq;
0291 struct list_head evt_list;
0292 struct list_head evt_free_list;
0293 spinlock_t evt_lock;
0294 struct efx_ptp_event_rx rx_evts[MAX_RECEIVE_EVENTS];
0295 struct workqueue_struct *workwq;
0296 struct work_struct work;
0297 bool reset_required;
0298 u32 rxfilter_event;
0299 u32 rxfilter_general;
0300 bool rxfilter_installed;
0301 struct hwtstamp_config config;
0302 bool enabled;
0303 unsigned int mode;
0304 void (*ns_to_nic_time)(s64 ns, u32 *nic_major, u32 *nic_minor);
0305 ktime_t (*nic_to_kernel_time)(u32 nic_major, u32 nic_minor,
0306 s32 correction);
0307 struct {
0308 u32 minor_max;
0309 u32 sync_event_diff_min;
0310 u32 sync_event_diff_max;
0311 unsigned int sync_event_minor_shift;
0312 } nic_time;
0313 unsigned int min_synchronisation_ns;
0314 unsigned int capabilities;
0315 struct {
0316 s32 ptp_tx;
0317 s32 ptp_rx;
0318 s32 pps_out;
0319 s32 pps_in;
0320 s32 general_tx;
0321 s32 general_rx;
0322 } ts_corrections;
0323 efx_qword_t evt_frags[MAX_EVENT_FRAGS];
0324 int evt_frag_idx;
0325 int evt_code;
0326 struct efx_buffer start;
0327 struct pps_event_time host_time_pps;
0328 unsigned int adjfreq_ppb_shift;
0329 s64 current_adjfreq;
0330 struct ptp_clock *phc_clock;
0331 struct ptp_clock_info phc_clock_info;
0332 struct work_struct pps_work;
0333 struct workqueue_struct *pps_workwq;
0334 bool nic_ts_enabled;
0335 efx_dword_t txbuf[MCDI_TX_BUF_LEN(MC_CMD_PTP_IN_TRANSMIT_LENMAX)];
0336
0337 unsigned int good_syncs;
0338 unsigned int fast_syncs;
0339 unsigned int bad_syncs;
0340 unsigned int sync_timeouts;
0341 unsigned int no_time_syncs;
0342 unsigned int invalid_sync_windows;
0343 unsigned int undersize_sync_windows;
0344 unsigned int oversize_sync_windows;
0345 unsigned int rx_no_timestamp;
0346 struct efx_ptp_timeset
0347 timeset[MC_CMD_PTP_OUT_SYNCHRONIZE_TIMESET_MAXNUM];
0348 void (*xmit_skb)(struct efx_nic *efx, struct sk_buff *skb);
0349 };
0350
0351 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta);
0352 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta);
0353 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts);
0354 static int efx_phc_settime(struct ptp_clock_info *ptp,
0355 const struct timespec64 *e_ts);
0356 static int efx_phc_enable(struct ptp_clock_info *ptp,
0357 struct ptp_clock_request *request, int on);
0358
0359 bool efx_ptp_use_mac_tx_timestamps(struct efx_nic *efx)
0360 {
0361 return efx_has_cap(efx, TX_MAC_TIMESTAMPING);
0362 }
0363
0364
0365
0366
0367 static bool efx_ptp_want_txqs(struct efx_channel *channel)
0368 {
0369 return efx_ptp_use_mac_tx_timestamps(channel->efx);
0370 }
0371
0372 #define PTP_SW_STAT(ext_name, field_name) \
0373 { #ext_name, 0, offsetof(struct efx_ptp_data, field_name) }
0374 #define PTP_MC_STAT(ext_name, mcdi_name) \
0375 { #ext_name, 32, MC_CMD_PTP_OUT_STATUS_STATS_ ## mcdi_name ## _OFST }
0376 static const struct efx_hw_stat_desc efx_ptp_stat_desc[] = {
0377 PTP_SW_STAT(ptp_good_syncs, good_syncs),
0378 PTP_SW_STAT(ptp_fast_syncs, fast_syncs),
0379 PTP_SW_STAT(ptp_bad_syncs, bad_syncs),
0380 PTP_SW_STAT(ptp_sync_timeouts, sync_timeouts),
0381 PTP_SW_STAT(ptp_no_time_syncs, no_time_syncs),
0382 PTP_SW_STAT(ptp_invalid_sync_windows, invalid_sync_windows),
0383 PTP_SW_STAT(ptp_undersize_sync_windows, undersize_sync_windows),
0384 PTP_SW_STAT(ptp_oversize_sync_windows, oversize_sync_windows),
0385 PTP_SW_STAT(ptp_rx_no_timestamp, rx_no_timestamp),
0386 PTP_MC_STAT(ptp_tx_timestamp_packets, TX),
0387 PTP_MC_STAT(ptp_rx_timestamp_packets, RX),
0388 PTP_MC_STAT(ptp_timestamp_packets, TS),
0389 PTP_MC_STAT(ptp_filter_matches, FM),
0390 PTP_MC_STAT(ptp_non_filter_matches, NFM),
0391 };
0392 #define PTP_STAT_COUNT ARRAY_SIZE(efx_ptp_stat_desc)
0393 static const unsigned long efx_ptp_stat_mask[] = {
0394 [0 ... BITS_TO_LONGS(PTP_STAT_COUNT) - 1] = ~0UL,
0395 };
0396
0397 size_t efx_ptp_describe_stats(struct efx_nic *efx, u8 *strings)
0398 {
0399 if (!efx->ptp_data)
0400 return 0;
0401
0402 return efx_nic_describe_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
0403 efx_ptp_stat_mask, strings);
0404 }
0405
0406 size_t efx_ptp_update_stats(struct efx_nic *efx, u64 *stats)
0407 {
0408 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_STATUS_LEN);
0409 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_STATUS_LEN);
0410 size_t i;
0411 int rc;
0412
0413 if (!efx->ptp_data)
0414 return 0;
0415
0416
0417 for (i = 0; i < PTP_STAT_COUNT; i++) {
0418 if (efx_ptp_stat_desc[i].dma_width)
0419 continue;
0420 stats[i] = *(unsigned int *)((char *)efx->ptp_data +
0421 efx_ptp_stat_desc[i].offset);
0422 }
0423
0424
0425
0426
0427
0428 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_STATUS);
0429 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
0430 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
0431 outbuf, sizeof(outbuf), NULL);
0432 if (rc)
0433 memset(outbuf, 0, sizeof(outbuf));
0434 efx_nic_update_stats(efx_ptp_stat_desc, PTP_STAT_COUNT,
0435 efx_ptp_stat_mask,
0436 stats, _MCDI_PTR(outbuf, 0), false);
0437
0438 return PTP_STAT_COUNT;
0439 }
0440
0441
0442 static void efx_ptp_ns_to_s_ns(s64 ns, u32 *nic_major, u32 *nic_minor)
0443 {
0444 struct timespec64 ts = ns_to_timespec64(ns);
0445 *nic_major = (u32)ts.tv_sec;
0446 *nic_minor = ts.tv_nsec;
0447 }
0448
0449 static ktime_t efx_ptp_s_ns_to_ktime_correction(u32 nic_major, u32 nic_minor,
0450 s32 correction)
0451 {
0452 ktime_t kt = ktime_set(nic_major, nic_minor);
0453 if (correction >= 0)
0454 kt = ktime_add_ns(kt, (u64)correction);
0455 else
0456 kt = ktime_sub_ns(kt, (u64)-correction);
0457 return kt;
0458 }
0459
0460
0461
0462
0463
0464 #define S27_TO_NS_SHIFT (27)
0465 #define NS_TO_S27_MULT (((1ULL << 63) + NSEC_PER_SEC / 2) / NSEC_PER_SEC)
0466 #define NS_TO_S27_SHIFT (63 - S27_TO_NS_SHIFT)
0467 #define S27_MINOR_MAX (1 << S27_TO_NS_SHIFT)
0468
0469
0470
0471
0472 static void efx_ptp_ns_to_s27(s64 ns, u32 *nic_major, u32 *nic_minor)
0473 {
0474 struct timespec64 ts = ns_to_timespec64(ns);
0475 u32 maj = (u32)ts.tv_sec;
0476 u32 min = (u32)(((u64)ts.tv_nsec * NS_TO_S27_MULT +
0477 (1ULL << (NS_TO_S27_SHIFT - 1))) >> NS_TO_S27_SHIFT);
0478
0479
0480
0481
0482 if (min >= S27_MINOR_MAX) {
0483 min -= S27_MINOR_MAX;
0484 maj++;
0485 }
0486
0487 *nic_major = maj;
0488 *nic_minor = min;
0489 }
0490
0491 static inline ktime_t efx_ptp_s27_to_ktime(u32 nic_major, u32 nic_minor)
0492 {
0493 u32 ns = (u32)(((u64)nic_minor * NSEC_PER_SEC +
0494 (1ULL << (S27_TO_NS_SHIFT - 1))) >> S27_TO_NS_SHIFT);
0495 return ktime_set(nic_major, ns);
0496 }
0497
0498 static ktime_t efx_ptp_s27_to_ktime_correction(u32 nic_major, u32 nic_minor,
0499 s32 correction)
0500 {
0501
0502 nic_minor += correction;
0503 if ((s32)nic_minor < 0) {
0504 nic_minor += S27_MINOR_MAX;
0505 nic_major--;
0506 } else if (nic_minor >= S27_MINOR_MAX) {
0507 nic_minor -= S27_MINOR_MAX;
0508 nic_major++;
0509 }
0510
0511 return efx_ptp_s27_to_ktime(nic_major, nic_minor);
0512 }
0513
0514
0515 static void efx_ptp_ns_to_s_qns(s64 ns, u32 *nic_major, u32 *nic_minor)
0516 {
0517 struct timespec64 ts = ns_to_timespec64(ns);
0518
0519 *nic_major = (u32)ts.tv_sec;
0520 *nic_minor = ts.tv_nsec * 4;
0521 }
0522
0523 static ktime_t efx_ptp_s_qns_to_ktime_correction(u32 nic_major, u32 nic_minor,
0524 s32 correction)
0525 {
0526 ktime_t kt;
0527
0528 nic_minor = DIV_ROUND_CLOSEST(nic_minor, 4);
0529 correction = DIV_ROUND_CLOSEST(correction, 4);
0530
0531 kt = ktime_set(nic_major, nic_minor);
0532
0533 if (correction >= 0)
0534 kt = ktime_add_ns(kt, (u64)correction);
0535 else
0536 kt = ktime_sub_ns(kt, (u64)-correction);
0537 return kt;
0538 }
0539
0540 struct efx_channel *efx_ptp_channel(struct efx_nic *efx)
0541 {
0542 return efx->ptp_data ? efx->ptp_data->channel : NULL;
0543 }
0544
0545 void efx_ptp_update_channel(struct efx_nic *efx, struct efx_channel *channel)
0546 {
0547 if (efx->ptp_data)
0548 efx->ptp_data->channel = channel;
0549 }
0550
0551 static u32 last_sync_timestamp_major(struct efx_nic *efx)
0552 {
0553 struct efx_channel *channel = efx_ptp_channel(efx);
0554 u32 major = 0;
0555
0556 if (channel)
0557 major = channel->sync_timestamp_major;
0558 return major;
0559 }
0560
0561
0562
0563
0564 static ktime_t
0565 efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
0566 struct efx_ptp_data *ptp,
0567 u32 nic_major, u32 nic_minor,
0568 s32 correction)
0569 {
0570 u32 sync_timestamp;
0571 ktime_t kt = { 0 };
0572 s16 delta;
0573
0574 if (!(nic_major & 0x80000000)) {
0575 WARN_ON_ONCE(nic_major >> 16);
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595 sync_timestamp = last_sync_timestamp_major(efx);
0596
0597
0598
0599
0600
0601
0602
0603 delta = nic_major - sync_timestamp;
0604
0605
0606
0607
0608 nic_major = sync_timestamp + delta;
0609
0610 kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
0611 correction);
0612 }
0613 return kt;
0614 }
0615
0616 ktime_t efx_ptp_nic_to_kernel_time(struct efx_tx_queue *tx_queue)
0617 {
0618 struct efx_nic *efx = tx_queue->efx;
0619 struct efx_ptp_data *ptp = efx->ptp_data;
0620 ktime_t kt;
0621
0622 if (efx_ptp_use_mac_tx_timestamps(efx))
0623 kt = efx_ptp_mac_nic_to_ktime_correction(efx, ptp,
0624 tx_queue->completed_timestamp_major,
0625 tx_queue->completed_timestamp_minor,
0626 ptp->ts_corrections.general_tx);
0627 else
0628 kt = ptp->nic_to_kernel_time(
0629 tx_queue->completed_timestamp_major,
0630 tx_queue->completed_timestamp_minor,
0631 ptp->ts_corrections.general_tx);
0632 return kt;
0633 }
0634
0635
0636 static int efx_ptp_get_attributes(struct efx_nic *efx)
0637 {
0638 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_ATTRIBUTES_LEN);
0639 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN);
0640 struct efx_ptp_data *ptp = efx->ptp_data;
0641 int rc;
0642 u32 fmt;
0643 size_t out_len;
0644
0645
0646
0647
0648
0649 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_GET_ATTRIBUTES);
0650 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
0651 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
0652 outbuf, sizeof(outbuf), &out_len);
0653 if (rc == 0) {
0654 fmt = MCDI_DWORD(outbuf, PTP_OUT_GET_ATTRIBUTES_TIME_FORMAT);
0655 } else if (rc == -EINVAL) {
0656 fmt = MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS;
0657 } else if (rc == -EPERM) {
0658 pci_info(efx->pci_dev, "no PTP support\n");
0659 return rc;
0660 } else {
0661 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf),
0662 outbuf, sizeof(outbuf), rc);
0663 return rc;
0664 }
0665
0666 switch (fmt) {
0667 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_27FRACTION:
0668 ptp->ns_to_nic_time = efx_ptp_ns_to_s27;
0669 ptp->nic_to_kernel_time = efx_ptp_s27_to_ktime_correction;
0670 ptp->nic_time.minor_max = 1 << 27;
0671 ptp->nic_time.sync_event_minor_shift = 19;
0672 break;
0673 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_NANOSECONDS:
0674 ptp->ns_to_nic_time = efx_ptp_ns_to_s_ns;
0675 ptp->nic_to_kernel_time = efx_ptp_s_ns_to_ktime_correction;
0676 ptp->nic_time.minor_max = 1000000000;
0677 ptp->nic_time.sync_event_minor_shift = 22;
0678 break;
0679 case MC_CMD_PTP_OUT_GET_ATTRIBUTES_SECONDS_QTR_NANOSECONDS:
0680 ptp->ns_to_nic_time = efx_ptp_ns_to_s_qns;
0681 ptp->nic_to_kernel_time = efx_ptp_s_qns_to_ktime_correction;
0682 ptp->nic_time.minor_max = 4000000000UL;
0683 ptp->nic_time.sync_event_minor_shift = 24;
0684 break;
0685 default:
0686 return -ERANGE;
0687 }
0688
0689
0690
0691
0692
0693
0694
0695 ptp->nic_time.sync_event_diff_min = ptp->nic_time.minor_max
0696 - (ptp->nic_time.minor_max / 10);
0697 ptp->nic_time.sync_event_diff_max = (ptp->nic_time.minor_max / 4)
0698 + (ptp->nic_time.minor_max / 10);
0699
0700
0701
0702
0703
0704
0705
0706
0707 if (rc == 0 &&
0708 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_CAPABILITIES_OFST)
0709 ptp->min_synchronisation_ns =
0710 MCDI_DWORD(outbuf,
0711 PTP_OUT_GET_ATTRIBUTES_SYNC_WINDOW_MIN);
0712 else
0713 ptp->min_synchronisation_ns = DEFAULT_MIN_SYNCHRONISATION_NS;
0714
0715 if (rc == 0 &&
0716 out_len >= MC_CMD_PTP_OUT_GET_ATTRIBUTES_LEN)
0717 ptp->capabilities = MCDI_DWORD(outbuf,
0718 PTP_OUT_GET_ATTRIBUTES_CAPABILITIES);
0719 else
0720 ptp->capabilities = 0;
0721
0722
0723
0724
0725
0726 if (ptp->capabilities & (1 << MC_CMD_PTP_OUT_GET_ATTRIBUTES_FP44_FREQ_ADJ_LBN))
0727 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP44;
0728 else
0729 ptp->adjfreq_ppb_shift = PPB_SHIFT_FP40;
0730
0731 return 0;
0732 }
0733
0734
0735 static int efx_ptp_get_timestamp_corrections(struct efx_nic *efx)
0736 {
0737 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_GET_TIMESTAMP_CORRECTIONS_LEN);
0738 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN);
0739 int rc;
0740 size_t out_len;
0741
0742
0743
0744
0745 MCDI_SET_DWORD(inbuf, PTP_IN_OP,
0746 MC_CMD_PTP_OP_GET_TIMESTAMP_CORRECTIONS);
0747 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
0748
0749 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
0750 outbuf, sizeof(outbuf), &out_len);
0751 if (rc == 0) {
0752 efx->ptp_data->ts_corrections.ptp_tx = MCDI_DWORD(outbuf,
0753 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_TRANSMIT);
0754 efx->ptp_data->ts_corrections.ptp_rx = MCDI_DWORD(outbuf,
0755 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_RECEIVE);
0756 efx->ptp_data->ts_corrections.pps_out = MCDI_DWORD(outbuf,
0757 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_OUT);
0758 efx->ptp_data->ts_corrections.pps_in = MCDI_DWORD(outbuf,
0759 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_PPS_IN);
0760
0761 if (out_len >= MC_CMD_PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_LEN) {
0762 efx->ptp_data->ts_corrections.general_tx = MCDI_DWORD(
0763 outbuf,
0764 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_TX);
0765 efx->ptp_data->ts_corrections.general_rx = MCDI_DWORD(
0766 outbuf,
0767 PTP_OUT_GET_TIMESTAMP_CORRECTIONS_V2_GENERAL_RX);
0768 } else {
0769 efx->ptp_data->ts_corrections.general_tx =
0770 efx->ptp_data->ts_corrections.ptp_tx;
0771 efx->ptp_data->ts_corrections.general_rx =
0772 efx->ptp_data->ts_corrections.ptp_rx;
0773 }
0774 } else if (rc == -EINVAL) {
0775 efx->ptp_data->ts_corrections.ptp_tx = 0;
0776 efx->ptp_data->ts_corrections.ptp_rx = 0;
0777 efx->ptp_data->ts_corrections.pps_out = 0;
0778 efx->ptp_data->ts_corrections.pps_in = 0;
0779 efx->ptp_data->ts_corrections.general_tx = 0;
0780 efx->ptp_data->ts_corrections.general_rx = 0;
0781 } else {
0782 efx_mcdi_display_error(efx, MC_CMD_PTP, sizeof(inbuf), outbuf,
0783 sizeof(outbuf), rc);
0784 return rc;
0785 }
0786
0787 return 0;
0788 }
0789
0790
0791 static int efx_ptp_enable(struct efx_nic *efx)
0792 {
0793 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ENABLE_LEN);
0794 MCDI_DECLARE_BUF_ERR(outbuf);
0795 int rc;
0796
0797 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ENABLE);
0798 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
0799 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_QUEUE,
0800 efx->ptp_data->channel ?
0801 efx->ptp_data->channel->channel : 0);
0802 MCDI_SET_DWORD(inbuf, PTP_IN_ENABLE_MODE, efx->ptp_data->mode);
0803
0804 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
0805 outbuf, sizeof(outbuf), NULL);
0806 rc = (rc == -EALREADY) ? 0 : rc;
0807 if (rc)
0808 efx_mcdi_display_error(efx, MC_CMD_PTP,
0809 MC_CMD_PTP_IN_ENABLE_LEN,
0810 outbuf, sizeof(outbuf), rc);
0811 return rc;
0812 }
0813
0814
0815
0816
0817
0818
0819 static int efx_ptp_disable(struct efx_nic *efx)
0820 {
0821 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_DISABLE_LEN);
0822 MCDI_DECLARE_BUF_ERR(outbuf);
0823 int rc;
0824
0825 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_DISABLE);
0826 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
0827 rc = efx_mcdi_rpc_quiet(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
0828 outbuf, sizeof(outbuf), NULL);
0829 rc = (rc == -EALREADY) ? 0 : rc;
0830
0831
0832
0833 if (rc == -ENOSYS || rc == -EPERM)
0834 pci_info(efx->pci_dev, "no PTP support\n");
0835 else if (rc)
0836 efx_mcdi_display_error(efx, MC_CMD_PTP,
0837 MC_CMD_PTP_IN_DISABLE_LEN,
0838 outbuf, sizeof(outbuf), rc);
0839 return rc;
0840 }
0841
0842 static void efx_ptp_deliver_rx_queue(struct sk_buff_head *q)
0843 {
0844 struct sk_buff *skb;
0845
0846 while ((skb = skb_dequeue(q))) {
0847 local_bh_disable();
0848 netif_receive_skb(skb);
0849 local_bh_enable();
0850 }
0851 }
0852
0853 static void efx_ptp_handle_no_channel(struct efx_nic *efx)
0854 {
0855 netif_err(efx, drv, efx->net_dev,
0856 "ERROR: PTP requires MSI-X and 1 additional interrupt"
0857 "vector. PTP disabled\n");
0858 }
0859
0860
0861
0862
0863 static void efx_ptp_send_times(struct efx_nic *efx,
0864 struct pps_event_time *last_time)
0865 {
0866 struct pps_event_time now;
0867 struct timespec64 limit;
0868 struct efx_ptp_data *ptp = efx->ptp_data;
0869 int *mc_running = ptp->start.addr;
0870
0871 pps_get_ts(&now);
0872 limit = now.ts_real;
0873 timespec64_add_ns(&limit, SYNCHRONISE_PERIOD_NS);
0874
0875
0876 while ((timespec64_compare(&now.ts_real, &limit) < 0) &&
0877 READ_ONCE(*mc_running)) {
0878 struct timespec64 update_time;
0879 unsigned int host_time;
0880
0881
0882 update_time = now.ts_real;
0883 timespec64_add_ns(&update_time, SYNCHRONISATION_GRANULARITY_NS);
0884 do {
0885 pps_get_ts(&now);
0886 } while ((timespec64_compare(&now.ts_real, &update_time) < 0) &&
0887 READ_ONCE(*mc_running));
0888
0889
0890 host_time = (now.ts_real.tv_sec << MC_NANOSECOND_BITS |
0891 now.ts_real.tv_nsec);
0892
0893 efx->type->ptp_write_host_time(efx, host_time);
0894 }
0895 *last_time = now;
0896 }
0897
0898
0899 static void efx_ptp_read_timeset(MCDI_DECLARE_STRUCT_PTR(data),
0900 struct efx_ptp_timeset *timeset)
0901 {
0902 unsigned start_ns, end_ns;
0903
0904 timeset->host_start = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTSTART);
0905 timeset->major = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MAJOR);
0906 timeset->minor = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_MINOR);
0907 timeset->host_end = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_HOSTEND),
0908 timeset->wait = MCDI_DWORD(data, PTP_OUT_SYNCHRONIZE_WAITNS);
0909
0910
0911 start_ns = timeset->host_start & MC_NANOSECOND_MASK;
0912 end_ns = timeset->host_end & MC_NANOSECOND_MASK;
0913
0914 if (end_ns < start_ns)
0915 end_ns += NSEC_PER_SEC;
0916
0917 timeset->window = end_ns - start_ns;
0918 }
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928 static int
0929 efx_ptp_process_times(struct efx_nic *efx, MCDI_DECLARE_STRUCT_PTR(synch_buf),
0930 size_t response_length,
0931 const struct pps_event_time *last_time)
0932 {
0933 unsigned number_readings =
0934 MCDI_VAR_ARRAY_LEN(response_length,
0935 PTP_OUT_SYNCHRONIZE_TIMESET);
0936 unsigned i;
0937 unsigned ngood = 0;
0938 unsigned last_good = 0;
0939 struct efx_ptp_data *ptp = efx->ptp_data;
0940 u32 last_sec;
0941 u32 start_sec;
0942 struct timespec64 delta;
0943 ktime_t mc_time;
0944
0945 if (number_readings == 0)
0946 return -EAGAIN;
0947
0948
0949
0950
0951
0952
0953
0954 for (i = 0; i < number_readings; i++) {
0955 s32 window, corrected;
0956 struct timespec64 wait;
0957
0958 efx_ptp_read_timeset(
0959 MCDI_ARRAY_STRUCT_PTR(synch_buf,
0960 PTP_OUT_SYNCHRONIZE_TIMESET, i),
0961 &ptp->timeset[i]);
0962
0963 wait = ktime_to_timespec64(
0964 ptp->nic_to_kernel_time(0, ptp->timeset[i].wait, 0));
0965 window = ptp->timeset[i].window;
0966 corrected = window - wait.tv_nsec;
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977 if (window < SYNCHRONISATION_GRANULARITY_NS) {
0978 ++ptp->invalid_sync_windows;
0979 } else if (corrected >= MAX_SYNCHRONISATION_NS) {
0980 ++ptp->oversize_sync_windows;
0981 } else if (corrected < ptp->min_synchronisation_ns) {
0982 ++ptp->undersize_sync_windows;
0983 } else {
0984 ngood++;
0985 last_good = i;
0986 }
0987 }
0988
0989 if (ngood == 0) {
0990 netif_warn(efx, drv, efx->net_dev,
0991 "PTP no suitable synchronisations\n");
0992 return -EAGAIN;
0993 }
0994
0995
0996
0997
0998
0999
1000
1001 start_sec = ptp->timeset[last_good].host_start >> MC_NANOSECOND_BITS;
1002 last_sec = last_time->ts_real.tv_sec & MC_SECOND_MASK;
1003 if (start_sec != last_sec &&
1004 ((start_sec + 1) & MC_SECOND_MASK) != last_sec) {
1005 netif_warn(efx, hw, efx->net_dev,
1006 "PTP bad synchronisation seconds\n");
1007 return -EAGAIN;
1008 }
1009 delta.tv_sec = (last_sec - start_sec) & 1;
1010 delta.tv_nsec =
1011 last_time->ts_real.tv_nsec -
1012 (ptp->timeset[last_good].host_start & MC_NANOSECOND_MASK);
1013
1014
1015
1016
1017
1018 mc_time = ptp->nic_to_kernel_time(ptp->timeset[last_good].major,
1019 ptp->timeset[last_good].minor, 0);
1020
1021
1022 delta.tv_nsec += ktime_to_timespec64(mc_time).tv_nsec;
1023
1024
1025 ptp->host_time_pps = *last_time;
1026 pps_sub_ts(&ptp->host_time_pps, delta);
1027
1028 return 0;
1029 }
1030
1031
1032 static int efx_ptp_synchronize(struct efx_nic *efx, unsigned int num_readings)
1033 {
1034 struct efx_ptp_data *ptp = efx->ptp_data;
1035 MCDI_DECLARE_BUF(synch_buf, MC_CMD_PTP_OUT_SYNCHRONIZE_LENMAX);
1036 size_t response_length;
1037 int rc;
1038 unsigned long timeout;
1039 struct pps_event_time last_time = {};
1040 unsigned int loops = 0;
1041 int *start = ptp->start.addr;
1042
1043 MCDI_SET_DWORD(synch_buf, PTP_IN_OP, MC_CMD_PTP_OP_SYNCHRONIZE);
1044 MCDI_SET_DWORD(synch_buf, PTP_IN_PERIPH_ID, 0);
1045 MCDI_SET_DWORD(synch_buf, PTP_IN_SYNCHRONIZE_NUMTIMESETS,
1046 num_readings);
1047 MCDI_SET_QWORD(synch_buf, PTP_IN_SYNCHRONIZE_START_ADDR,
1048 ptp->start.dma_addr);
1049
1050
1051 WRITE_ONCE(*start, 0);
1052 rc = efx_mcdi_rpc_start(efx, MC_CMD_PTP, synch_buf,
1053 MC_CMD_PTP_IN_SYNCHRONIZE_LEN);
1054 EFX_WARN_ON_ONCE_PARANOID(rc);
1055
1056
1057 timeout = jiffies + msecs_to_jiffies(MAX_SYNCHRONISE_WAIT_MS);
1058 while (!READ_ONCE(*start) && (time_before(jiffies, timeout))) {
1059 udelay(20);
1060 loops++;
1061 }
1062
1063 if (loops <= 1)
1064 ++ptp->fast_syncs;
1065 if (!time_before(jiffies, timeout))
1066 ++ptp->sync_timeouts;
1067
1068 if (READ_ONCE(*start))
1069 efx_ptp_send_times(efx, &last_time);
1070
1071
1072 rc = efx_mcdi_rpc_finish(efx, MC_CMD_PTP,
1073 MC_CMD_PTP_IN_SYNCHRONIZE_LEN,
1074 synch_buf, sizeof(synch_buf),
1075 &response_length);
1076 if (rc == 0) {
1077 rc = efx_ptp_process_times(efx, synch_buf, response_length,
1078 &last_time);
1079 if (rc == 0)
1080 ++ptp->good_syncs;
1081 else
1082 ++ptp->no_time_syncs;
1083 }
1084
1085
1086
1087
1088 if (rc != 0)
1089 ++ptp->bad_syncs;
1090
1091 return rc;
1092 }
1093
1094
1095 static void efx_ptp_xmit_skb_queue(struct efx_nic *efx, struct sk_buff *skb)
1096 {
1097 struct efx_ptp_data *ptp_data = efx->ptp_data;
1098 u8 type = efx_tx_csum_type_skb(skb);
1099 struct efx_tx_queue *tx_queue;
1100
1101 tx_queue = efx_channel_get_tx_queue(ptp_data->channel, type);
1102 if (tx_queue && tx_queue->timestamping) {
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123 local_bh_disable();
1124 efx_enqueue_skb(tx_queue, skb);
1125 local_bh_enable();
1126 } else {
1127 WARN_ONCE(1, "PTP channel has no timestamped tx queue\n");
1128 dev_kfree_skb_any(skb);
1129 }
1130 }
1131
1132
1133 static void efx_ptp_xmit_skb_mc(struct efx_nic *efx, struct sk_buff *skb)
1134 {
1135 struct efx_ptp_data *ptp_data = efx->ptp_data;
1136 struct skb_shared_hwtstamps timestamps;
1137 int rc = -EIO;
1138 MCDI_DECLARE_BUF(txtime, MC_CMD_PTP_OUT_TRANSMIT_LEN);
1139 size_t len;
1140
1141 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_OP, MC_CMD_PTP_OP_TRANSMIT);
1142 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_PERIPH_ID, 0);
1143 MCDI_SET_DWORD(ptp_data->txbuf, PTP_IN_TRANSMIT_LENGTH, skb->len);
1144 if (skb_shinfo(skb)->nr_frags != 0) {
1145 rc = skb_linearize(skb);
1146 if (rc != 0)
1147 goto fail;
1148 }
1149
1150 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1151 rc = skb_checksum_help(skb);
1152 if (rc != 0)
1153 goto fail;
1154 }
1155 skb_copy_from_linear_data(skb,
1156 MCDI_PTR(ptp_data->txbuf,
1157 PTP_IN_TRANSMIT_PACKET),
1158 skb->len);
1159 rc = efx_mcdi_rpc(efx, MC_CMD_PTP,
1160 ptp_data->txbuf, MC_CMD_PTP_IN_TRANSMIT_LEN(skb->len),
1161 txtime, sizeof(txtime), &len);
1162 if (rc != 0)
1163 goto fail;
1164
1165 memset(×tamps, 0, sizeof(timestamps));
1166 timestamps.hwtstamp = ptp_data->nic_to_kernel_time(
1167 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MAJOR),
1168 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_MINOR),
1169 ptp_data->ts_corrections.ptp_tx);
1170
1171 skb_tstamp_tx(skb, ×tamps);
1172
1173 rc = 0;
1174
1175 fail:
1176 dev_kfree_skb_any(skb);
1177
1178 return;
1179 }
1180
1181 static void efx_ptp_drop_time_expired_events(struct efx_nic *efx)
1182 {
1183 struct efx_ptp_data *ptp = efx->ptp_data;
1184 struct list_head *cursor;
1185 struct list_head *next;
1186
1187 if (ptp->rx_ts_inline)
1188 return;
1189
1190
1191 spin_lock_bh(&ptp->evt_lock);
1192 list_for_each_safe(cursor, next, &ptp->evt_list) {
1193 struct efx_ptp_event_rx *evt;
1194
1195 evt = list_entry(cursor, struct efx_ptp_event_rx,
1196 link);
1197 if (time_after(jiffies, evt->expiry)) {
1198 list_move(&evt->link, &ptp->evt_free_list);
1199 netif_warn(efx, hw, efx->net_dev,
1200 "PTP rx event dropped\n");
1201 }
1202 }
1203 spin_unlock_bh(&ptp->evt_lock);
1204 }
1205
1206 static enum ptp_packet_state efx_ptp_match_rx(struct efx_nic *efx,
1207 struct sk_buff *skb)
1208 {
1209 struct efx_ptp_data *ptp = efx->ptp_data;
1210 bool evts_waiting;
1211 struct list_head *cursor;
1212 struct list_head *next;
1213 struct efx_ptp_match *match;
1214 enum ptp_packet_state rc = PTP_PACKET_STATE_UNMATCHED;
1215
1216 WARN_ON_ONCE(ptp->rx_ts_inline);
1217
1218 spin_lock_bh(&ptp->evt_lock);
1219 evts_waiting = !list_empty(&ptp->evt_list);
1220 spin_unlock_bh(&ptp->evt_lock);
1221
1222 if (!evts_waiting)
1223 return PTP_PACKET_STATE_UNMATCHED;
1224
1225 match = (struct efx_ptp_match *)skb->cb;
1226
1227 spin_lock_bh(&ptp->evt_lock);
1228 list_for_each_safe(cursor, next, &ptp->evt_list) {
1229 struct efx_ptp_event_rx *evt;
1230
1231 evt = list_entry(cursor, struct efx_ptp_event_rx, link);
1232 if ((evt->seq0 == match->words[0]) &&
1233 (evt->seq1 == match->words[1])) {
1234 struct skb_shared_hwtstamps *timestamps;
1235
1236
1237 timestamps = skb_hwtstamps(skb);
1238 timestamps->hwtstamp = evt->hwtimestamp;
1239
1240 match->state = PTP_PACKET_STATE_MATCHED;
1241 rc = PTP_PACKET_STATE_MATCHED;
1242 list_move(&evt->link, &ptp->evt_free_list);
1243 break;
1244 }
1245 }
1246 spin_unlock_bh(&ptp->evt_lock);
1247
1248 return rc;
1249 }
1250
1251
1252
1253
1254
1255 static void efx_ptp_process_events(struct efx_nic *efx, struct sk_buff_head *q)
1256 {
1257 struct efx_ptp_data *ptp = efx->ptp_data;
1258 struct sk_buff *skb;
1259
1260 while ((skb = skb_dequeue(&ptp->rxq))) {
1261 struct efx_ptp_match *match;
1262
1263 match = (struct efx_ptp_match *)skb->cb;
1264 if (match->state == PTP_PACKET_STATE_MATCH_UNWANTED) {
1265 __skb_queue_tail(q, skb);
1266 } else if (efx_ptp_match_rx(efx, skb) ==
1267 PTP_PACKET_STATE_MATCHED) {
1268 __skb_queue_tail(q, skb);
1269 } else if (time_after(jiffies, match->expiry)) {
1270 match->state = PTP_PACKET_STATE_TIMED_OUT;
1271 ++ptp->rx_no_timestamp;
1272 __skb_queue_tail(q, skb);
1273 } else {
1274
1275 skb_queue_head(&ptp->rxq, skb);
1276 break;
1277 }
1278 }
1279 }
1280
1281
1282 static inline void efx_ptp_process_rx(struct efx_nic *efx, struct sk_buff *skb)
1283 {
1284 local_bh_disable();
1285 netif_receive_skb(skb);
1286 local_bh_enable();
1287 }
1288
1289 static void efx_ptp_remove_multicast_filters(struct efx_nic *efx)
1290 {
1291 struct efx_ptp_data *ptp = efx->ptp_data;
1292
1293 if (ptp->rxfilter_installed) {
1294 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1295 ptp->rxfilter_general);
1296 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1297 ptp->rxfilter_event);
1298 ptp->rxfilter_installed = false;
1299 }
1300 }
1301
1302 static int efx_ptp_insert_multicast_filters(struct efx_nic *efx)
1303 {
1304 struct efx_ptp_data *ptp = efx->ptp_data;
1305 struct efx_filter_spec rxfilter;
1306 int rc;
1307
1308 if (!ptp->channel || ptp->rxfilter_installed)
1309 return 0;
1310
1311
1312
1313
1314 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1315 efx_rx_queue_index(
1316 efx_channel_get_rx_queue(ptp->channel)));
1317 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1318 htonl(PTP_ADDRESS),
1319 htons(PTP_EVENT_PORT));
1320 if (rc != 0)
1321 return rc;
1322
1323 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1324 if (rc < 0)
1325 return rc;
1326 ptp->rxfilter_event = rc;
1327
1328 efx_filter_init_rx(&rxfilter, EFX_FILTER_PRI_REQUIRED, 0,
1329 efx_rx_queue_index(
1330 efx_channel_get_rx_queue(ptp->channel)));
1331 rc = efx_filter_set_ipv4_local(&rxfilter, IPPROTO_UDP,
1332 htonl(PTP_ADDRESS),
1333 htons(PTP_GENERAL_PORT));
1334 if (rc != 0)
1335 goto fail;
1336
1337 rc = efx_filter_insert_filter(efx, &rxfilter, true);
1338 if (rc < 0)
1339 goto fail;
1340 ptp->rxfilter_general = rc;
1341
1342 ptp->rxfilter_installed = true;
1343 return 0;
1344
1345 fail:
1346 efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED,
1347 ptp->rxfilter_event);
1348 return rc;
1349 }
1350
1351 static int efx_ptp_start(struct efx_nic *efx)
1352 {
1353 struct efx_ptp_data *ptp = efx->ptp_data;
1354 int rc;
1355
1356 ptp->reset_required = false;
1357
1358 rc = efx_ptp_insert_multicast_filters(efx);
1359 if (rc)
1360 return rc;
1361
1362 rc = efx_ptp_enable(efx);
1363 if (rc != 0)
1364 goto fail;
1365
1366 ptp->evt_frag_idx = 0;
1367 ptp->current_adjfreq = 0;
1368
1369 return 0;
1370
1371 fail:
1372 efx_ptp_remove_multicast_filters(efx);
1373 return rc;
1374 }
1375
1376 static int efx_ptp_stop(struct efx_nic *efx)
1377 {
1378 struct efx_ptp_data *ptp = efx->ptp_data;
1379 struct list_head *cursor;
1380 struct list_head *next;
1381 int rc;
1382
1383 if (ptp == NULL)
1384 return 0;
1385
1386 rc = efx_ptp_disable(efx);
1387
1388 efx_ptp_remove_multicast_filters(efx);
1389
1390
1391 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
1392 skb_queue_purge(&efx->ptp_data->txq);
1393
1394
1395 spin_lock_bh(&efx->ptp_data->evt_lock);
1396 list_for_each_safe(cursor, next, &efx->ptp_data->evt_list) {
1397 list_move(cursor, &efx->ptp_data->evt_free_list);
1398 }
1399 spin_unlock_bh(&efx->ptp_data->evt_lock);
1400
1401 return rc;
1402 }
1403
1404 static int efx_ptp_restart(struct efx_nic *efx)
1405 {
1406 if (efx->ptp_data && efx->ptp_data->enabled)
1407 return efx_ptp_start(efx);
1408 return 0;
1409 }
1410
1411 static void efx_ptp_pps_worker(struct work_struct *work)
1412 {
1413 struct efx_ptp_data *ptp =
1414 container_of(work, struct efx_ptp_data, pps_work);
1415 struct efx_nic *efx = ptp->efx;
1416 struct ptp_clock_event ptp_evt;
1417
1418 if (efx_ptp_synchronize(efx, PTP_SYNC_ATTEMPTS))
1419 return;
1420
1421 ptp_evt.type = PTP_CLOCK_PPSUSR;
1422 ptp_evt.pps_times = ptp->host_time_pps;
1423 ptp_clock_event(ptp->phc_clock, &ptp_evt);
1424 }
1425
1426 static void efx_ptp_worker(struct work_struct *work)
1427 {
1428 struct efx_ptp_data *ptp_data =
1429 container_of(work, struct efx_ptp_data, work);
1430 struct efx_nic *efx = ptp_data->efx;
1431 struct sk_buff *skb;
1432 struct sk_buff_head tempq;
1433
1434 if (ptp_data->reset_required) {
1435 efx_ptp_stop(efx);
1436 efx_ptp_start(efx);
1437 return;
1438 }
1439
1440 efx_ptp_drop_time_expired_events(efx);
1441
1442 __skb_queue_head_init(&tempq);
1443 efx_ptp_process_events(efx, &tempq);
1444
1445 while ((skb = skb_dequeue(&ptp_data->txq)))
1446 ptp_data->xmit_skb(efx, skb);
1447
1448 while ((skb = __skb_dequeue(&tempq)))
1449 efx_ptp_process_rx(efx, skb);
1450 }
1451
1452 static const struct ptp_clock_info efx_phc_clock_info = {
1453 .owner = THIS_MODULE,
1454 .name = "sfc",
1455 .max_adj = MAX_PPB,
1456 .n_alarm = 0,
1457 .n_ext_ts = 0,
1458 .n_per_out = 0,
1459 .n_pins = 0,
1460 .pps = 1,
1461 .adjfreq = efx_phc_adjfreq,
1462 .adjtime = efx_phc_adjtime,
1463 .gettime64 = efx_phc_gettime,
1464 .settime64 = efx_phc_settime,
1465 .enable = efx_phc_enable,
1466 };
1467
1468
1469 int efx_ptp_probe(struct efx_nic *efx, struct efx_channel *channel)
1470 {
1471 struct efx_ptp_data *ptp;
1472 int rc = 0;
1473 unsigned int pos;
1474
1475 if (efx->ptp_data) {
1476 efx->ptp_data->channel = channel;
1477 return 0;
1478 }
1479
1480 ptp = kzalloc(sizeof(struct efx_ptp_data), GFP_KERNEL);
1481 efx->ptp_data = ptp;
1482 if (!efx->ptp_data)
1483 return -ENOMEM;
1484
1485 ptp->efx = efx;
1486 ptp->channel = channel;
1487 ptp->rx_ts_inline = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1488
1489 rc = efx_nic_alloc_buffer(efx, &ptp->start, sizeof(int), GFP_KERNEL);
1490 if (rc != 0)
1491 goto fail1;
1492
1493 skb_queue_head_init(&ptp->rxq);
1494 skb_queue_head_init(&ptp->txq);
1495 ptp->workwq = create_singlethread_workqueue("sfc_ptp");
1496 if (!ptp->workwq) {
1497 rc = -ENOMEM;
1498 goto fail2;
1499 }
1500
1501 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1502 ptp->xmit_skb = efx_ptp_xmit_skb_queue;
1503
1504 channel->sync_events_state = SYNC_EVENTS_QUIESCENT;
1505 } else {
1506 ptp->xmit_skb = efx_ptp_xmit_skb_mc;
1507 }
1508
1509 INIT_WORK(&ptp->work, efx_ptp_worker);
1510 ptp->config.flags = 0;
1511 ptp->config.tx_type = HWTSTAMP_TX_OFF;
1512 ptp->config.rx_filter = HWTSTAMP_FILTER_NONE;
1513 INIT_LIST_HEAD(&ptp->evt_list);
1514 INIT_LIST_HEAD(&ptp->evt_free_list);
1515 spin_lock_init(&ptp->evt_lock);
1516 for (pos = 0; pos < MAX_RECEIVE_EVENTS; pos++)
1517 list_add(&ptp->rx_evts[pos].link, &ptp->evt_free_list);
1518
1519
1520 rc = efx_ptp_get_attributes(efx);
1521 if (rc < 0)
1522 goto fail3;
1523
1524
1525 rc = efx_ptp_get_timestamp_corrections(efx);
1526 if (rc < 0)
1527 goto fail3;
1528
1529 if (efx->mcdi->fn_flags &
1530 (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY)) {
1531 ptp->phc_clock_info = efx_phc_clock_info;
1532 ptp->phc_clock = ptp_clock_register(&ptp->phc_clock_info,
1533 &efx->pci_dev->dev);
1534 if (IS_ERR(ptp->phc_clock)) {
1535 rc = PTR_ERR(ptp->phc_clock);
1536 goto fail3;
1537 } else if (ptp->phc_clock) {
1538 INIT_WORK(&ptp->pps_work, efx_ptp_pps_worker);
1539 ptp->pps_workwq = create_singlethread_workqueue("sfc_pps");
1540 if (!ptp->pps_workwq) {
1541 rc = -ENOMEM;
1542 goto fail4;
1543 }
1544 }
1545 }
1546 ptp->nic_ts_enabled = false;
1547
1548 return 0;
1549 fail4:
1550 ptp_clock_unregister(efx->ptp_data->phc_clock);
1551
1552 fail3:
1553 destroy_workqueue(efx->ptp_data->workwq);
1554
1555 fail2:
1556 efx_nic_free_buffer(efx, &ptp->start);
1557
1558 fail1:
1559 kfree(efx->ptp_data);
1560 efx->ptp_data = NULL;
1561
1562 return rc;
1563 }
1564
1565
1566
1567
1568
1569
1570 static int efx_ptp_probe_channel(struct efx_channel *channel)
1571 {
1572 struct efx_nic *efx = channel->efx;
1573 int rc;
1574
1575 channel->irq_moderation_us = 0;
1576 channel->rx_queue.core_index = 0;
1577
1578 rc = efx_ptp_probe(efx, channel);
1579
1580
1581
1582
1583
1584 if (rc && rc != -EPERM)
1585 netif_warn(efx, drv, efx->net_dev,
1586 "Failed to probe PTP, rc=%d\n", rc);
1587 return 0;
1588 }
1589
1590 void efx_ptp_remove(struct efx_nic *efx)
1591 {
1592 if (!efx->ptp_data)
1593 return;
1594
1595 (void)efx_ptp_disable(efx);
1596
1597 cancel_work_sync(&efx->ptp_data->work);
1598 if (efx->ptp_data->pps_workwq)
1599 cancel_work_sync(&efx->ptp_data->pps_work);
1600
1601 skb_queue_purge(&efx->ptp_data->rxq);
1602 skb_queue_purge(&efx->ptp_data->txq);
1603
1604 if (efx->ptp_data->phc_clock) {
1605 destroy_workqueue(efx->ptp_data->pps_workwq);
1606 ptp_clock_unregister(efx->ptp_data->phc_clock);
1607 }
1608
1609 destroy_workqueue(efx->ptp_data->workwq);
1610
1611 efx_nic_free_buffer(efx, &efx->ptp_data->start);
1612 kfree(efx->ptp_data);
1613 efx->ptp_data = NULL;
1614 }
1615
1616 static void efx_ptp_remove_channel(struct efx_channel *channel)
1617 {
1618 efx_ptp_remove(channel->efx);
1619 }
1620
1621 static void efx_ptp_get_channel_name(struct efx_channel *channel,
1622 char *buf, size_t len)
1623 {
1624 snprintf(buf, len, "%s-ptp", channel->efx->name);
1625 }
1626
1627
1628
1629
1630 bool efx_ptp_is_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1631 {
1632 return efx->ptp_data &&
1633 efx->ptp_data->enabled &&
1634 skb->len >= PTP_MIN_LENGTH &&
1635 skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM &&
1636 likely(skb->protocol == htons(ETH_P_IP)) &&
1637 skb_transport_header_was_set(skb) &&
1638 skb_network_header_len(skb) >= sizeof(struct iphdr) &&
1639 ip_hdr(skb)->protocol == IPPROTO_UDP &&
1640 skb_headlen(skb) >=
1641 skb_transport_offset(skb) + sizeof(struct udphdr) &&
1642 udp_hdr(skb)->dest == htons(PTP_EVENT_PORT);
1643 }
1644
1645
1646
1647
1648
1649 static bool efx_ptp_rx(struct efx_channel *channel, struct sk_buff *skb)
1650 {
1651 struct efx_nic *efx = channel->efx;
1652 struct efx_ptp_data *ptp = efx->ptp_data;
1653 struct efx_ptp_match *match = (struct efx_ptp_match *)skb->cb;
1654 u8 *match_data_012, *match_data_345;
1655 unsigned int version;
1656 u8 *data;
1657
1658 match->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1659
1660
1661 if (ptp->mode == MC_CMD_PTP_MODE_V1) {
1662 if (!pskb_may_pull(skb, PTP_V1_MIN_LENGTH)) {
1663 return false;
1664 }
1665 data = skb->data;
1666 version = ntohs(*(__be16 *)&data[PTP_V1_VERSION_OFFSET]);
1667 if (version != PTP_VERSION_V1) {
1668 return false;
1669 }
1670
1671
1672
1673
1674 match_data_012 = data + PTP_V1_UUID_OFFSET;
1675 match_data_345 = data + PTP_V1_UUID_OFFSET + 3;
1676 } else {
1677 if (!pskb_may_pull(skb, PTP_V2_MIN_LENGTH)) {
1678 return false;
1679 }
1680 data = skb->data;
1681 version = data[PTP_V2_VERSION_OFFSET];
1682 if ((version & PTP_VERSION_V2_MASK) != PTP_VERSION_V2) {
1683 return false;
1684 }
1685
1686
1687
1688
1689
1690
1691
1692
1693 match_data_345 = data + PTP_V2_UUID_OFFSET + 5;
1694 if (ptp->mode == MC_CMD_PTP_MODE_V2) {
1695 match_data_012 = data + PTP_V2_UUID_OFFSET + 2;
1696 } else {
1697 match_data_012 = data + PTP_V2_UUID_OFFSET + 0;
1698 BUG_ON(ptp->mode != MC_CMD_PTP_MODE_V2_ENHANCED);
1699 }
1700 }
1701
1702
1703 if (ntohs(*(__be16 *)&data[PTP_DPORT_OFFSET]) == PTP_EVENT_PORT) {
1704 match->state = PTP_PACKET_STATE_UNMATCHED;
1705
1706
1707
1708
1709 BUILD_BUG_ON(PTP_V1_SEQUENCE_OFFSET != PTP_V2_SEQUENCE_OFFSET);
1710 BUILD_BUG_ON(PTP_V1_SEQUENCE_LENGTH != PTP_V2_SEQUENCE_LENGTH);
1711
1712
1713 match->words[0] = (match_data_012[0] |
1714 (match_data_012[1] << 8) |
1715 (match_data_012[2] << 16) |
1716 (match_data_345[0] << 24));
1717 match->words[1] = (match_data_345[1] |
1718 (match_data_345[2] << 8) |
1719 (data[PTP_V1_SEQUENCE_OFFSET +
1720 PTP_V1_SEQUENCE_LENGTH - 1] <<
1721 16));
1722 } else {
1723 match->state = PTP_PACKET_STATE_MATCH_UNWANTED;
1724 }
1725
1726 skb_queue_tail(&ptp->rxq, skb);
1727 queue_work(ptp->workwq, &ptp->work);
1728
1729 return true;
1730 }
1731
1732
1733
1734
1735
1736 int efx_ptp_tx(struct efx_nic *efx, struct sk_buff *skb)
1737 {
1738 struct efx_ptp_data *ptp = efx->ptp_data;
1739
1740 skb_queue_tail(&ptp->txq, skb);
1741
1742 if ((udp_hdr(skb)->dest == htons(PTP_EVENT_PORT)) &&
1743 (skb->len <= MC_CMD_PTP_IN_TRANSMIT_PACKET_MAXNUM))
1744 efx_xmit_hwtstamp_pending(skb);
1745 queue_work(ptp->workwq, &ptp->work);
1746
1747 return NETDEV_TX_OK;
1748 }
1749
1750 int efx_ptp_get_mode(struct efx_nic *efx)
1751 {
1752 return efx->ptp_data->mode;
1753 }
1754
1755 int efx_ptp_change_mode(struct efx_nic *efx, bool enable_wanted,
1756 unsigned int new_mode)
1757 {
1758 if ((enable_wanted != efx->ptp_data->enabled) ||
1759 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1760 int rc = 0;
1761
1762 if (enable_wanted) {
1763
1764 if (efx->ptp_data->enabled &&
1765 (efx->ptp_data->mode != new_mode)) {
1766 efx->ptp_data->enabled = false;
1767 rc = efx_ptp_stop(efx);
1768 if (rc != 0)
1769 return rc;
1770 }
1771
1772
1773
1774
1775
1776 efx->ptp_data->mode = new_mode;
1777 if (netif_running(efx->net_dev))
1778 rc = efx_ptp_start(efx);
1779 if (rc == 0) {
1780 rc = efx_ptp_synchronize(efx,
1781 PTP_SYNC_ATTEMPTS * 2);
1782 if (rc != 0)
1783 efx_ptp_stop(efx);
1784 }
1785 } else {
1786 rc = efx_ptp_stop(efx);
1787 }
1788
1789 if (rc != 0)
1790 return rc;
1791
1792 efx->ptp_data->enabled = enable_wanted;
1793 }
1794
1795 return 0;
1796 }
1797
1798 static int efx_ptp_ts_init(struct efx_nic *efx, struct hwtstamp_config *init)
1799 {
1800 int rc;
1801
1802 if ((init->tx_type != HWTSTAMP_TX_OFF) &&
1803 (init->tx_type != HWTSTAMP_TX_ON))
1804 return -ERANGE;
1805
1806 rc = efx->type->ptp_set_ts_config(efx, init);
1807 if (rc)
1808 return rc;
1809
1810 efx->ptp_data->config = *init;
1811 return 0;
1812 }
1813
1814 void efx_ptp_get_ts_info(struct efx_nic *efx, struct ethtool_ts_info *ts_info)
1815 {
1816 struct efx_ptp_data *ptp = efx->ptp_data;
1817 struct efx_nic *primary = efx->primary;
1818
1819 ASSERT_RTNL();
1820
1821 if (!ptp)
1822 return;
1823
1824 ts_info->so_timestamping |= (SOF_TIMESTAMPING_TX_HARDWARE |
1825 SOF_TIMESTAMPING_RX_HARDWARE |
1826 SOF_TIMESTAMPING_RAW_HARDWARE);
1827
1828
1829
1830 if (efx_ptp_use_mac_tx_timestamps(efx)) {
1831 struct efx_ef10_nic_data *nic_data = efx->nic_data;
1832
1833 if (!(nic_data->licensed_features &
1834 (1 << LICENSED_V3_FEATURES_TX_TIMESTAMPS_LBN)))
1835 ts_info->so_timestamping &=
1836 ~SOF_TIMESTAMPING_TX_HARDWARE;
1837 }
1838 if (primary && primary->ptp_data && primary->ptp_data->phc_clock)
1839 ts_info->phc_index =
1840 ptp_clock_index(primary->ptp_data->phc_clock);
1841 ts_info->tx_types = 1 << HWTSTAMP_TX_OFF | 1 << HWTSTAMP_TX_ON;
1842 ts_info->rx_filters = ptp->efx->type->hwtstamp_filters;
1843 }
1844
1845 int efx_ptp_set_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1846 {
1847 struct hwtstamp_config config;
1848 int rc;
1849
1850
1851 if (!efx->ptp_data)
1852 return -EOPNOTSUPP;
1853
1854 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1855 return -EFAULT;
1856
1857 rc = efx_ptp_ts_init(efx, &config);
1858 if (rc != 0)
1859 return rc;
1860
1861 return copy_to_user(ifr->ifr_data, &config, sizeof(config))
1862 ? -EFAULT : 0;
1863 }
1864
1865 int efx_ptp_get_ts_config(struct efx_nic *efx, struct ifreq *ifr)
1866 {
1867 if (!efx->ptp_data)
1868 return -EOPNOTSUPP;
1869
1870 return copy_to_user(ifr->ifr_data, &efx->ptp_data->config,
1871 sizeof(efx->ptp_data->config)) ? -EFAULT : 0;
1872 }
1873
1874 static void ptp_event_failure(struct efx_nic *efx, int expected_frag_len)
1875 {
1876 struct efx_ptp_data *ptp = efx->ptp_data;
1877
1878 netif_err(efx, hw, efx->net_dev,
1879 "PTP unexpected event length: got %d expected %d\n",
1880 ptp->evt_frag_idx, expected_frag_len);
1881 ptp->reset_required = true;
1882 queue_work(ptp->workwq, &ptp->work);
1883 }
1884
1885
1886
1887
1888
1889 static void ptp_event_rx(struct efx_nic *efx, struct efx_ptp_data *ptp)
1890 {
1891 struct efx_ptp_event_rx *evt = NULL;
1892
1893 if (WARN_ON_ONCE(ptp->rx_ts_inline))
1894 return;
1895
1896 if (ptp->evt_frag_idx != 3) {
1897 ptp_event_failure(efx, 3);
1898 return;
1899 }
1900
1901 spin_lock_bh(&ptp->evt_lock);
1902 if (!list_empty(&ptp->evt_free_list)) {
1903 evt = list_first_entry(&ptp->evt_free_list,
1904 struct efx_ptp_event_rx, link);
1905 list_del(&evt->link);
1906
1907 evt->seq0 = EFX_QWORD_FIELD(ptp->evt_frags[2], MCDI_EVENT_DATA);
1908 evt->seq1 = (EFX_QWORD_FIELD(ptp->evt_frags[2],
1909 MCDI_EVENT_SRC) |
1910 (EFX_QWORD_FIELD(ptp->evt_frags[1],
1911 MCDI_EVENT_SRC) << 8) |
1912 (EFX_QWORD_FIELD(ptp->evt_frags[0],
1913 MCDI_EVENT_SRC) << 16));
1914 evt->hwtimestamp = efx->ptp_data->nic_to_kernel_time(
1915 EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA),
1916 EFX_QWORD_FIELD(ptp->evt_frags[1], MCDI_EVENT_DATA),
1917 ptp->ts_corrections.ptp_rx);
1918 evt->expiry = jiffies + msecs_to_jiffies(PKT_EVENT_LIFETIME_MS);
1919 list_add_tail(&evt->link, &ptp->evt_list);
1920
1921 queue_work(ptp->workwq, &ptp->work);
1922 } else if (net_ratelimit()) {
1923
1924 netif_err(efx, rx_err, efx->net_dev, "PTP event queue overflow\n");
1925 }
1926 spin_unlock_bh(&ptp->evt_lock);
1927 }
1928
1929 static void ptp_event_fault(struct efx_nic *efx, struct efx_ptp_data *ptp)
1930 {
1931 int code = EFX_QWORD_FIELD(ptp->evt_frags[0], MCDI_EVENT_DATA);
1932 if (ptp->evt_frag_idx != 1) {
1933 ptp_event_failure(efx, 1);
1934 return;
1935 }
1936
1937 netif_err(efx, hw, efx->net_dev, "PTP error %d\n", code);
1938 }
1939
1940 static void ptp_event_pps(struct efx_nic *efx, struct efx_ptp_data *ptp)
1941 {
1942 if (ptp->nic_ts_enabled)
1943 queue_work(ptp->pps_workwq, &ptp->pps_work);
1944 }
1945
1946 void efx_ptp_event(struct efx_nic *efx, efx_qword_t *ev)
1947 {
1948 struct efx_ptp_data *ptp = efx->ptp_data;
1949 int code = EFX_QWORD_FIELD(*ev, MCDI_EVENT_CODE);
1950
1951 if (!ptp) {
1952 if (!efx->ptp_warned) {
1953 netif_warn(efx, drv, efx->net_dev,
1954 "Received PTP event but PTP not set up\n");
1955 efx->ptp_warned = true;
1956 }
1957 return;
1958 }
1959
1960 if (!ptp->enabled)
1961 return;
1962
1963 if (ptp->evt_frag_idx == 0) {
1964 ptp->evt_code = code;
1965 } else if (ptp->evt_code != code) {
1966 netif_err(efx, hw, efx->net_dev,
1967 "PTP out of sequence event %d\n", code);
1968 ptp->evt_frag_idx = 0;
1969 }
1970
1971 ptp->evt_frags[ptp->evt_frag_idx++] = *ev;
1972 if (!MCDI_EVENT_FIELD(*ev, CONT)) {
1973
1974 switch (code) {
1975 case MCDI_EVENT_CODE_PTP_RX:
1976 ptp_event_rx(efx, ptp);
1977 break;
1978 case MCDI_EVENT_CODE_PTP_FAULT:
1979 ptp_event_fault(efx, ptp);
1980 break;
1981 case MCDI_EVENT_CODE_PTP_PPS:
1982 ptp_event_pps(efx, ptp);
1983 break;
1984 default:
1985 netif_err(efx, hw, efx->net_dev,
1986 "PTP unknown event %d\n", code);
1987 break;
1988 }
1989 ptp->evt_frag_idx = 0;
1990 } else if (MAX_EVENT_FRAGS == ptp->evt_frag_idx) {
1991 netif_err(efx, hw, efx->net_dev,
1992 "PTP too many event fragments\n");
1993 ptp->evt_frag_idx = 0;
1994 }
1995 }
1996
1997 void efx_time_sync_event(struct efx_channel *channel, efx_qword_t *ev)
1998 {
1999 struct efx_nic *efx = channel->efx;
2000 struct efx_ptp_data *ptp = efx->ptp_data;
2001
2002
2003
2004
2005
2006
2007
2008 channel->sync_timestamp_major = MCDI_EVENT_FIELD(*ev, PTP_TIME_MAJOR);
2009 channel->sync_timestamp_minor =
2010 (MCDI_EVENT_FIELD(*ev, PTP_TIME_MINOR_MS_8BITS) & 0xFC)
2011 << ptp->nic_time.sync_event_minor_shift;
2012
2013
2014
2015
2016 (void) cmpxchg(&channel->sync_events_state, SYNC_EVENTS_REQUESTED,
2017 SYNC_EVENTS_VALID);
2018 }
2019
2020 static inline u32 efx_rx_buf_timestamp_minor(struct efx_nic *efx, const u8 *eh)
2021 {
2022 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
2023 return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_ts_offset));
2024 #else
2025 const u8 *data = eh + efx->rx_packet_ts_offset;
2026 return (u32)data[0] |
2027 (u32)data[1] << 8 |
2028 (u32)data[2] << 16 |
2029 (u32)data[3] << 24;
2030 #endif
2031 }
2032
2033 void __efx_rx_skb_attach_timestamp(struct efx_channel *channel,
2034 struct sk_buff *skb)
2035 {
2036 struct efx_nic *efx = channel->efx;
2037 struct efx_ptp_data *ptp = efx->ptp_data;
2038 u32 pkt_timestamp_major, pkt_timestamp_minor;
2039 u32 diff, carry;
2040 struct skb_shared_hwtstamps *timestamps;
2041
2042 if (channel->sync_events_state != SYNC_EVENTS_VALID)
2043 return;
2044
2045 pkt_timestamp_minor = efx_rx_buf_timestamp_minor(efx, skb_mac_header(skb));
2046
2047
2048
2049
2050 diff = pkt_timestamp_minor - channel->sync_timestamp_minor;
2051 if (pkt_timestamp_minor < channel->sync_timestamp_minor)
2052 diff += ptp->nic_time.minor_max;
2053
2054
2055 carry = (channel->sync_timestamp_minor >= ptp->nic_time.minor_max - diff) ?
2056 1 : 0;
2057
2058 if (diff <= ptp->nic_time.sync_event_diff_max) {
2059
2060
2061
2062 pkt_timestamp_major = channel->sync_timestamp_major + carry;
2063 } else if (diff >= ptp->nic_time.sync_event_diff_min) {
2064
2065
2066
2067
2068 pkt_timestamp_major = channel->sync_timestamp_major - 1 + carry;
2069 } else {
2070
2071
2072
2073
2074
2075 netif_vdbg(efx, drv, efx->net_dev,
2076 "packet timestamp %x too far from sync event %x:%x\n",
2077 pkt_timestamp_minor, channel->sync_timestamp_major,
2078 channel->sync_timestamp_minor);
2079 return;
2080 }
2081
2082
2083 timestamps = skb_hwtstamps(skb);
2084 timestamps->hwtstamp =
2085 ptp->nic_to_kernel_time(pkt_timestamp_major,
2086 pkt_timestamp_minor,
2087 ptp->ts_corrections.general_rx);
2088 }
2089
2090 static int efx_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
2091 {
2092 struct efx_ptp_data *ptp_data = container_of(ptp,
2093 struct efx_ptp_data,
2094 phc_clock_info);
2095 struct efx_nic *efx = ptp_data->efx;
2096 MCDI_DECLARE_BUF(inadj, MC_CMD_PTP_IN_ADJUST_LEN);
2097 s64 adjustment_ns;
2098 int rc;
2099
2100 if (delta > MAX_PPB)
2101 delta = MAX_PPB;
2102 else if (delta < -MAX_PPB)
2103 delta = -MAX_PPB;
2104
2105
2106 adjustment_ns = ((s64)delta * PPB_SCALE_WORD +
2107 (1 << (ptp_data->adjfreq_ppb_shift - 1))) >>
2108 ptp_data->adjfreq_ppb_shift;
2109
2110 MCDI_SET_DWORD(inadj, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2111 MCDI_SET_DWORD(inadj, PTP_IN_PERIPH_ID, 0);
2112 MCDI_SET_QWORD(inadj, PTP_IN_ADJUST_FREQ, adjustment_ns);
2113 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_SECONDS, 0);
2114 MCDI_SET_DWORD(inadj, PTP_IN_ADJUST_NANOSECONDS, 0);
2115 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inadj, sizeof(inadj),
2116 NULL, 0, NULL);
2117 if (rc != 0)
2118 return rc;
2119
2120 ptp_data->current_adjfreq = adjustment_ns;
2121 return 0;
2122 }
2123
2124 static int efx_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
2125 {
2126 u32 nic_major, nic_minor;
2127 struct efx_ptp_data *ptp_data = container_of(ptp,
2128 struct efx_ptp_data,
2129 phc_clock_info);
2130 struct efx_nic *efx = ptp_data->efx;
2131 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_ADJUST_LEN);
2132
2133 efx->ptp_data->ns_to_nic_time(delta, &nic_major, &nic_minor);
2134
2135 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_ADJUST);
2136 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2137 MCDI_SET_QWORD(inbuf, PTP_IN_ADJUST_FREQ, ptp_data->current_adjfreq);
2138 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MAJOR, nic_major);
2139 MCDI_SET_DWORD(inbuf, PTP_IN_ADJUST_MINOR, nic_minor);
2140 return efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2141 NULL, 0, NULL);
2142 }
2143
2144 static int efx_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
2145 {
2146 struct efx_ptp_data *ptp_data = container_of(ptp,
2147 struct efx_ptp_data,
2148 phc_clock_info);
2149 struct efx_nic *efx = ptp_data->efx;
2150 MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_READ_NIC_TIME_LEN);
2151 MCDI_DECLARE_BUF(outbuf, MC_CMD_PTP_OUT_READ_NIC_TIME_LEN);
2152 int rc;
2153 ktime_t kt;
2154
2155 MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_READ_NIC_TIME);
2156 MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
2157
2158 rc = efx_mcdi_rpc(efx, MC_CMD_PTP, inbuf, sizeof(inbuf),
2159 outbuf, sizeof(outbuf), NULL);
2160 if (rc != 0)
2161 return rc;
2162
2163 kt = ptp_data->nic_to_kernel_time(
2164 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MAJOR),
2165 MCDI_DWORD(outbuf, PTP_OUT_READ_NIC_TIME_MINOR), 0);
2166 *ts = ktime_to_timespec64(kt);
2167 return 0;
2168 }
2169
2170 static int efx_phc_settime(struct ptp_clock_info *ptp,
2171 const struct timespec64 *e_ts)
2172 {
2173
2174
2175
2176
2177 int rc;
2178 struct timespec64 time_now;
2179 struct timespec64 delta;
2180
2181 rc = efx_phc_gettime(ptp, &time_now);
2182 if (rc != 0)
2183 return rc;
2184
2185 delta = timespec64_sub(*e_ts, time_now);
2186
2187 rc = efx_phc_adjtime(ptp, timespec64_to_ns(&delta));
2188 if (rc != 0)
2189 return rc;
2190
2191 return 0;
2192 }
2193
2194 static int efx_phc_enable(struct ptp_clock_info *ptp,
2195 struct ptp_clock_request *request,
2196 int enable)
2197 {
2198 struct efx_ptp_data *ptp_data = container_of(ptp,
2199 struct efx_ptp_data,
2200 phc_clock_info);
2201 if (request->type != PTP_CLK_REQ_PPS)
2202 return -EOPNOTSUPP;
2203
2204 ptp_data->nic_ts_enabled = !!enable;
2205 return 0;
2206 }
2207
2208 static const struct efx_channel_type efx_ptp_channel_type = {
2209 .handle_no_channel = efx_ptp_handle_no_channel,
2210 .pre_probe = efx_ptp_probe_channel,
2211 .post_remove = efx_ptp_remove_channel,
2212 .get_name = efx_ptp_get_channel_name,
2213 .copy = efx_copy_channel,
2214 .receive_skb = efx_ptp_rx,
2215 .want_txqs = efx_ptp_want_txqs,
2216 .keep_eventq = false,
2217 };
2218
2219 void efx_ptp_defer_probe_with_channel(struct efx_nic *efx)
2220 {
2221
2222
2223
2224 if (efx_ptp_disable(efx) == 0)
2225 efx->extra_channel_type[EFX_EXTRA_CHANNEL_PTP] =
2226 &efx_ptp_channel_type;
2227 }
2228
2229 void efx_ptp_start_datapath(struct efx_nic *efx)
2230 {
2231 if (efx_ptp_restart(efx))
2232 netif_err(efx, drv, efx->net_dev, "Failed to restart PTP.\n");
2233
2234 if (efx->type->ptp_set_ts_sync_events)
2235 efx->type->ptp_set_ts_sync_events(efx, true, true);
2236 }
2237
2238 void efx_ptp_stop_datapath(struct efx_nic *efx)
2239 {
2240
2241 if (efx->type->ptp_set_ts_sync_events)
2242 efx->type->ptp_set_ts_sync_events(efx, false, true);
2243 efx_ptp_stop(efx);
2244 }