37 #include <linux/udp.h>
38 #include <linux/time.h>
40 #include <linux/module.h>
53 #define MAX_EVENT_FRAGS 3
56 #define MAX_SYNCHRONISE_WAIT_MS 2
59 #define SYNCHRONISE_PERIOD_NS 250000
62 #define SYNCHRONISATION_GRANULARITY_NS 200
65 #define MIN_SYNCHRONISATION_NS 120
68 #define MAX_SYNCHRONISATION_NS 1000
71 #define MAX_RECEIVE_EVENTS 8
74 #define AVERAGE_LENGTH 16
77 #define PKT_EVENT_LIFETIME_MS 10
83 #define PTP_DPORT_OFFSET 22
85 #define PTP_V1_VERSION_LENGTH 2
86 #define PTP_V1_VERSION_OFFSET 28
88 #define PTP_V1_UUID_LENGTH 6
89 #define PTP_V1_UUID_OFFSET 50
91 #define PTP_V1_SEQUENCE_LENGTH 2
92 #define PTP_V1_SEQUENCE_OFFSET 58
97 #define PTP_V1_MIN_LENGTH 64
99 #define PTP_V2_VERSION_LENGTH 1
100 #define PTP_V2_VERSION_OFFSET 29
107 #define PTP_V2_MC_UUID_LENGTH 6
108 #define PTP_V2_MC_UUID_OFFSET 50
110 #define PTP_V2_SEQUENCE_LENGTH 2
111 #define PTP_V2_SEQUENCE_OFFSET 58
116 #define PTP_V2_MIN_LENGTH 63
118 #define PTP_MIN_LENGTH 63
120 #define PTP_ADDRESS 0xe0000181
121 #define PTP_EVENT_PORT 319
122 #define PTP_GENERAL_PORT 320
127 #define PTP_VERSION_V1 1
129 #define PTP_VERSION_V2 2
130 #define PTP_VERSION_V2_MASK 0x0f
142 #define MC_NANOSECOND_BITS 30
143 #define MC_NANOSECOND_MASK ((1 << MC_NANOSECOND_BITS) - 1)
144 #define MC_SECOND_MASK ((1 << (32 - MC_NANOSECOND_BITS)) - 1)
147 #define MAX_PPB 1000000
150 #define MAX_PPB_BITS 20
156 #define PPB_EXTRA_BITS 2
159 #define PPB_SCALE_WORD ((1LL << (PPB_EXTRA_BITS + MC_CMD_PTP_IN_ADJUST_BITS +\
160 MAX_PPB_BITS)) / 1000000000LL)
162 #define PTP_SYNC_ATTEMPTS 4
309 static int efx_ptp_enable(
struct efx_nic *efx)
315 efx->ptp_data->
channel->channel);
327 static int efx_ptp_disable(
struct efx_nic *efx)
347 static void efx_ptp_handle_no_channel(
struct efx_nic *efx)
350 "ERROR: PTP requires MSI-X and 1 additional interrupt"
351 "vector. PTP disabled\n");
357 static void efx_ptp_send_times(
struct efx_nic *efx,
364 int *mc_running = ptp->
start.addr;
372 while ((timespec_compare(&now.ts_real, &
limit) < 0) &&
375 unsigned int host_time;
378 update_time = now.ts_real;
382 }
while ((timespec_compare(&now.ts_real, &update_time) < 0) &&
387 now.ts_real.tv_nsec);
398 unsigned start_ns, end_ns;
403 PTP_OUT_SYNCHRONIZE_NANOSECONDS);
411 if (end_ns < start_ns)
414 timeset->
window = end_ns - start_ns;
425 static int efx_ptp_process_times(
struct efx_nic *efx,
u8 *synch_buf,
426 size_t response_length,
429 unsigned number_readings = (response_length /
433 unsigned min_set = 0;
436 unsigned last_good = 0;
438 bool min_valid =
false;
443 if (number_readings == 0)
449 for (i = 0; i < number_readings; i++) {
450 efx_ptp_read_timeset(synch_buf, &ptp->
timeset[i]);
454 if (ptp->
timeset[i].window < min_set)
477 for (i = 0; i < number_readings; i++)
492 "PTP no suitable synchronisations %dns %dns\n",
506 ptp->
timeset[last_good].nanoseconds +
517 if (start_sec != last_sec) {
520 "PTP bad synchronisation seconds\n");
536 static int efx_ptp_synchronize(
struct efx_nic *efx,
unsigned int num_readings)
540 size_t response_length;
544 unsigned int loops = 0;
568 efx_ptp_send_times(efx, &last_time);
573 synch_buf,
sizeof(synch_buf),
576 rc = efx_ptp_process_times(efx, synch_buf, response_length,
583 static int efx_ptp_xmit_skb(
struct efx_nic *efx,
struct sk_buff *skb)
585 u8 *txbuf = efx->ptp_data->txbuf;
594 if (skb_shinfo(skb)->nr_frags != 0) {
595 rc = skb_linearize(skb);
605 skb_copy_from_linear_data(skb,
609 sizeof(txtime), &len);
613 memset(×tamps, 0,
sizeof(timestamps));
614 timestamps.hwtstamp = ktime_set(
616 MCDI_DWORD(txtime, PTP_OUT_TRANSMIT_NANOSECONDS));
628 static void efx_ptp_drop_time_expired_events(
struct efx_nic *efx)
645 "PTP rx event dropped\n");
663 evts_waiting = !list_empty(&ptp->
evt_list);
681 timestamps = skb_hwtstamps(skb);
712 __skb_queue_tail(q, skb);
713 }
else if (efx_ptp_match_rx(efx, skb) ==
716 __skb_queue_tail(q, skb);
720 "PTP packet - no timestamp seen\n");
721 __skb_queue_tail(q, skb);
733 static inline void efx_ptp_process_rx(
struct efx_nic *efx,
struct sk_buff *skb)
740 static int efx_ptp_start(
struct efx_nic *efx)
753 efx_channel_get_rx_queue(ptp->
channel)));
767 efx_channel_get_rx_queue(ptp->
channel)));
779 rc = efx_ptp_enable(efx);
799 static int efx_ptp_stop(
struct efx_nic *efx)
802 int rc = efx_ptp_disable(efx);
815 efx_ptp_deliver_rx_queue(&efx->ptp_data->rxq);
819 spin_lock_bh(&efx->ptp_data->evt_lock);
821 list_move(cursor, &efx->ptp_data->evt_free_list);
823 spin_unlock_bh(&efx->ptp_data->evt_lock);
845 static void efx_ptp_worker(
struct work_struct *work)
859 efx_ptp_drop_time_expired_events(efx);
861 __skb_queue_head_init(&tempq);
862 if (efx_ptp_process_events(efx, &tempq) ||
863 !skb_queue_empty(&ptp_data->
txq)) {
866 efx_ptp_xmit_skb(efx, skb);
869 while ((skb = __skb_dequeue(&tempq)))
870 efx_ptp_process_rx(efx, skb);
898 skb_queue_head_init(&ptp->
rxq);
899 skb_queue_head_init(&ptp->
txq);
919 "%pm", efx->
net_dev->perm_addr);
955 kfree(efx->ptp_data);
956 efx->ptp_data =
NULL;
961 static void efx_ptp_remove_channel(
struct efx_channel *channel)
968 (
void)efx_ptp_disable(channel->
efx);
982 kfree(efx->ptp_data);
985 static void efx_ptp_get_channel_name(
struct efx_channel *channel,
986 char *
buf,
size_t len)
996 return efx->ptp_data &&
997 efx->ptp_data->enabled &&
1056 timestamps = skb_hwtstamps(skb);
1057 memset(timestamps, 0,
sizeof(*timestamps));
1061 match->
words[0] = (data[0] |
1065 match->
words[1] = (data[4] |
1090 efx_xmit_hwtstamp_pending(skb);
1096 static int efx_ptp_change_mode(
struct efx_nic *efx,
bool enable_wanted,
1097 unsigned int new_mode)
1099 if ((enable_wanted != efx->ptp_data->enabled) ||
1100 (enable_wanted && (efx->ptp_data->mode != new_mode))) {
1103 if (enable_wanted) {
1105 if (efx->ptp_data->enabled &&
1106 (efx->ptp_data->mode != new_mode)) {
1107 efx->ptp_data->enabled =
false;
1108 rc = efx_ptp_stop(efx);
1117 efx->ptp_data->mode = new_mode;
1118 rc = efx_ptp_start(efx);
1120 rc = efx_ptp_synchronize(efx,
1126 rc = efx_ptp_stop(efx);
1132 efx->ptp_data->enabled = enable_wanted;
1140 bool enable_wanted =
false;
1141 unsigned int new_mode;
1151 new_mode = efx->ptp_data->mode;
1161 enable_wanted =
true;
1171 enable_wanted =
true;
1187 enable_wanted =
true;
1189 rc = efx_ptp_change_mode(efx, enable_wanted, new_mode);
1193 efx->ptp_data->config = *
init;
1201 struct efx_nic *efx = netdev_priv(net_dev);
1234 rc = efx_ptp_ts_init(efx, &config);
1238 return copy_to_user(ifr->ifr_data, &config,
sizeof(config))
1242 static void ptp_event_failure(
struct efx_nic *efx,
int expected_frag_len)
1247 "PTP unexpected event length: got %d expected %d\n",
1262 ptp_event_failure(efx, 3);
1276 MCDI_EVENT_SRC) << 8) |
1278 MCDI_EVENT_SRC) << 16));
1296 ptp_event_failure(efx, 1);
1319 }
else if (ptp->
evt_code != code) {
1321 "PTP out of sequence event %d\n", code);
1330 ptp_event_rx(efx, ptp);
1333 ptp_event_fault(efx, ptp);
1336 ptp_event_pps(efx, ptp);
1340 "PTP unknown event %d\n", code);
1346 "PTP too many event fragments\n");
1373 (
u32)(adjustment_ns >> 32));
1416 outbuf,
sizeof(outbuf),
NULL);
1436 rc = efx_phc_gettime(ptp, &time_now);
1440 delta = timespec_sub(*e_ts, time_now);
1442 efx_phc_adjtime(ptp, timespec_to_ns(&delta));
1456 if (request->
type != PTP_CLK_REQ_PPS)
1464 .handle_no_channel = efx_ptp_handle_no_channel,
1465 .pre_probe = efx_ptp_probe_channel,
1466 .post_remove = efx_ptp_remove_channel,
1467 .get_name = efx_ptp_get_channel_name,
1469 .receive_skb = efx_ptp_rx,
1470 .keep_eventq =
false,
1478 if (efx_ptp_disable(efx) == 0)
1480 &efx_ptp_channel_type;