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