1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Intel Corporation */
6 #include <linux/module.h>
7 #include <linux/device.h>
9 #include <linux/ptp_classify.h>
10 #include <linux/clocksource.h>
11 #include <linux/ktime.h>
13 #define INCVALUE_MASK 0x7fffffff
14 #define ISGN 0x80000000
16 #define IGC_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
17 #define IGC_PTP_TX_TIMEOUT (HZ * 15)
19 /* SYSTIM read access for I225 */
20 void igc_ptp_read(struct igc_adapter *adapter, struct timespec64 *ts)
22 struct igc_hw *hw = &adapter->hw;
25 /* The timestamp is latched when SYSTIML is read. */
26 nsec = rd32(IGC_SYSTIML);
27 sec = rd32(IGC_SYSTIMH);
33 static void igc_ptp_write_i225(struct igc_adapter *adapter,
34 const struct timespec64 *ts)
36 struct igc_hw *hw = &adapter->hw;
38 wr32(IGC_SYSTIML, ts->tv_nsec);
39 wr32(IGC_SYSTIMH, ts->tv_sec);
42 static int igc_ptp_adjfine_i225(struct ptp_clock_info *ptp, long scaled_ppm)
44 struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
46 struct igc_hw *hw = &igc->hw;
53 scaled_ppm = -scaled_ppm;
57 rate = div_u64(rate, 78125);
59 inca = rate & INCVALUE_MASK;
63 wr32(IGC_TIMINCA, inca);
68 static int igc_ptp_adjtime_i225(struct ptp_clock_info *ptp, s64 delta)
70 struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
72 struct timespec64 now, then = ns_to_timespec64(delta);
75 spin_lock_irqsave(&igc->tmreg_lock, flags);
77 igc_ptp_read(igc, &now);
78 now = timespec64_add(now, then);
79 igc_ptp_write_i225(igc, (const struct timespec64 *)&now);
81 spin_unlock_irqrestore(&igc->tmreg_lock, flags);
86 static int igc_ptp_gettimex64_i225(struct ptp_clock_info *ptp,
87 struct timespec64 *ts,
88 struct ptp_system_timestamp *sts)
90 struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
92 struct igc_hw *hw = &igc->hw;
95 spin_lock_irqsave(&igc->tmreg_lock, flags);
97 ptp_read_system_prets(sts);
98 ts->tv_nsec = rd32(IGC_SYSTIML);
99 ts->tv_sec = rd32(IGC_SYSTIMH);
100 ptp_read_system_postts(sts);
102 spin_unlock_irqrestore(&igc->tmreg_lock, flags);
107 static int igc_ptp_settime_i225(struct ptp_clock_info *ptp,
108 const struct timespec64 *ts)
110 struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
114 spin_lock_irqsave(&igc->tmreg_lock, flags);
116 igc_ptp_write_i225(igc, ts);
118 spin_unlock_irqrestore(&igc->tmreg_lock, flags);
123 static int igc_ptp_feature_enable_i225(struct ptp_clock_info *ptp,
124 struct ptp_clock_request *rq, int on)
130 * igc_ptp_systim_to_hwtstamp - convert system time value to HW timestamp
131 * @adapter: board private structure
132 * @hwtstamps: timestamp structure to update
133 * @systim: unsigned 64bit system time value
135 * We need to convert the system time value stored in the RX/TXSTMP registers
136 * into a hwtstamp which can be used by the upper level timestamping functions.
138 * Returns 0 on success.
140 static int igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter,
141 struct skb_shared_hwtstamps *hwtstamps,
144 switch (adapter->hw.mac.type) {
146 memset(hwtstamps, 0, sizeof(*hwtstamps));
147 /* Upper 32 bits contain s, lower 32 bits contain ns. */
148 hwtstamps->hwtstamp = ktime_set(systim >> 32,
149 systim & 0xFFFFFFFF);
158 * igc_ptp_rx_pktstamp - Retrieve timestamp from Rx packet buffer
159 * @q_vector: Pointer to interrupt specific structure
160 * @va: Pointer to address containing Rx buffer
161 * @skb: Buffer containing timestamp and packet
163 * This function retrieves the timestamp saved in the beginning of packet
164 * buffer. While two timestamps are available, one in timer0 reference and the
165 * other in timer1 reference, this function considers only the timestamp in
168 void igc_ptp_rx_pktstamp(struct igc_q_vector *q_vector, __le32 *va,
171 struct igc_adapter *adapter = q_vector->adapter;
175 /* Timestamps are saved in little endian at the beginning of the packet
176 * buffer following the layout:
178 * DWORD: | 0 | 1 | 2 | 3 |
179 * Field: | Timer1 SYSTIML | Timer1 SYSTIMH | Timer0 SYSTIML | Timer0 SYSTIMH |
181 * SYSTIML holds the nanoseconds part while SYSTIMH holds the seconds
182 * part of the timestamp.
184 regval = le32_to_cpu(va[2]);
185 regval |= (u64)le32_to_cpu(va[3]) << 32;
186 igc_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
188 /* Adjust timestamp for the RX latency based on link speed */
189 switch (adapter->link_speed) {
191 adjust = IGC_I225_RX_LATENCY_10;
194 adjust = IGC_I225_RX_LATENCY_100;
197 adjust = IGC_I225_RX_LATENCY_1000;
200 adjust = IGC_I225_RX_LATENCY_2500;
204 netdev_warn_once(adapter->netdev, "Imprecise timestamp\n");
207 skb_hwtstamps(skb)->hwtstamp =
208 ktime_sub_ns(skb_hwtstamps(skb)->hwtstamp, adjust);
211 static void igc_ptp_disable_rx_timestamp(struct igc_adapter *adapter)
213 struct igc_hw *hw = &adapter->hw;
217 wr32(IGC_TSYNCRXCTL, 0);
219 for (i = 0; i < adapter->num_rx_queues; i++) {
220 val = rd32(IGC_SRRCTL(i));
221 val &= ~IGC_SRRCTL_TIMESTAMP;
222 wr32(IGC_SRRCTL(i), val);
225 val = rd32(IGC_RXPBS);
226 val &= ~IGC_RXPBS_CFG_TS_EN;
227 wr32(IGC_RXPBS, val);
230 static void igc_ptp_enable_rx_timestamp(struct igc_adapter *adapter)
232 struct igc_hw *hw = &adapter->hw;
236 val = rd32(IGC_RXPBS);
237 val |= IGC_RXPBS_CFG_TS_EN;
238 wr32(IGC_RXPBS, val);
240 for (i = 0; i < adapter->num_rx_queues; i++) {
241 val = rd32(IGC_SRRCTL(i));
242 /* FIXME: For now, only support retrieving RX timestamps from
245 val |= IGC_SRRCTL_TIMER1SEL(0) | IGC_SRRCTL_TIMER0SEL(0) |
246 IGC_SRRCTL_TIMESTAMP;
247 wr32(IGC_SRRCTL(i), val);
250 val = IGC_TSYNCRXCTL_ENABLED | IGC_TSYNCRXCTL_TYPE_ALL |
251 IGC_TSYNCRXCTL_RXSYNSIG;
252 wr32(IGC_TSYNCRXCTL, val);
255 static void igc_ptp_disable_tx_timestamp(struct igc_adapter *adapter)
257 struct igc_hw *hw = &adapter->hw;
259 wr32(IGC_TSYNCTXCTL, 0);
262 static void igc_ptp_enable_tx_timestamp(struct igc_adapter *adapter)
264 struct igc_hw *hw = &adapter->hw;
266 wr32(IGC_TSYNCTXCTL, IGC_TSYNCTXCTL_ENABLED | IGC_TSYNCTXCTL_TXSYNSIG);
268 /* Read TXSTMP registers to discard any timestamp previously stored. */
274 * igc_ptp_set_timestamp_mode - setup hardware for timestamping
275 * @adapter: networking device structure
276 * @config: hwtstamp configuration
278 * Return: 0 in case of success, negative errno code otherwise.
280 static int igc_ptp_set_timestamp_mode(struct igc_adapter *adapter,
281 struct hwtstamp_config *config)
283 /* reserved for future extensions */
287 switch (config->tx_type) {
288 case HWTSTAMP_TX_OFF:
289 igc_ptp_disable_tx_timestamp(adapter);
292 igc_ptp_enable_tx_timestamp(adapter);
298 switch (config->rx_filter) {
299 case HWTSTAMP_FILTER_NONE:
300 igc_ptp_disable_rx_timestamp(adapter);
302 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
303 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
304 case HWTSTAMP_FILTER_PTP_V2_EVENT:
305 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
306 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
307 case HWTSTAMP_FILTER_PTP_V2_SYNC:
308 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
309 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
310 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
311 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
312 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
313 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
314 case HWTSTAMP_FILTER_NTP_ALL:
315 case HWTSTAMP_FILTER_ALL:
316 igc_ptp_enable_rx_timestamp(adapter);
317 config->rx_filter = HWTSTAMP_FILTER_ALL;
326 /* Requires adapter->ptp_tx_lock held by caller. */
327 static void igc_ptp_tx_timeout(struct igc_adapter *adapter)
329 struct igc_hw *hw = &adapter->hw;
331 dev_kfree_skb_any(adapter->ptp_tx_skb);
332 adapter->ptp_tx_skb = NULL;
333 adapter->tx_hwtstamp_timeouts++;
334 /* Clear the tx valid bit in TSYNCTXCTL register to enable interrupt. */
336 netdev_warn(adapter->netdev, "Tx timestamp timeout\n");
339 void igc_ptp_tx_hang(struct igc_adapter *adapter)
343 spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
345 if (!adapter->ptp_tx_skb)
348 if (time_is_after_jiffies(adapter->ptp_tx_start + IGC_PTP_TX_TIMEOUT))
351 igc_ptp_tx_timeout(adapter);
354 spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
358 * igc_ptp_tx_hwtstamp - utility function which checks for TX time stamp
359 * @adapter: Board private structure
361 * If we were asked to do hardware stamping and such a time stamp is
362 * available, then it must have been for this skb here because we only
363 * allow only one such packet into the queue.
365 * Context: Expects adapter->ptp_tx_lock to be held by caller.
367 static void igc_ptp_tx_hwtstamp(struct igc_adapter *adapter)
369 struct sk_buff *skb = adapter->ptp_tx_skb;
370 struct skb_shared_hwtstamps shhwtstamps;
371 struct igc_hw *hw = &adapter->hw;
375 if (WARN_ON_ONCE(!skb))
378 regval = rd32(IGC_TXSTMPL);
379 regval |= (u64)rd32(IGC_TXSTMPH) << 32;
380 if (igc_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval))
383 switch (adapter->link_speed) {
385 adjust = IGC_I225_TX_LATENCY_10;
388 adjust = IGC_I225_TX_LATENCY_100;
391 adjust = IGC_I225_TX_LATENCY_1000;
394 adjust = IGC_I225_TX_LATENCY_2500;
398 shhwtstamps.hwtstamp =
399 ktime_add_ns(shhwtstamps.hwtstamp, adjust);
401 adapter->ptp_tx_skb = NULL;
403 /* Notify the stack and free the skb after we've unlocked */
404 skb_tstamp_tx(skb, &shhwtstamps);
405 dev_kfree_skb_any(skb);
410 * @work: pointer to work struct
412 * This work function checks the TSYNCTXCTL valid bit to determine when
413 * a timestamp has been taken for the current stored skb.
415 static void igc_ptp_tx_work(struct work_struct *work)
417 struct igc_adapter *adapter = container_of(work, struct igc_adapter,
419 struct igc_hw *hw = &adapter->hw;
423 spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
425 if (!adapter->ptp_tx_skb)
428 tsynctxctl = rd32(IGC_TSYNCTXCTL);
429 tsynctxctl &= IGC_TSYNCTXCTL_TXTT_0;
431 WARN_ONCE(1, "Received a TSTAMP interrupt but no TSTAMP is ready.\n");
435 igc_ptp_tx_hwtstamp(adapter);
438 spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
442 * igc_ptp_set_ts_config - set hardware time stamping config
443 * @netdev: network interface device structure
444 * @ifr: interface request data
447 int igc_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
449 struct igc_adapter *adapter = netdev_priv(netdev);
450 struct hwtstamp_config config;
453 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
456 err = igc_ptp_set_timestamp_mode(adapter, &config);
460 /* save these settings for future reference */
461 memcpy(&adapter->tstamp_config, &config,
462 sizeof(adapter->tstamp_config));
464 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
469 * igc_ptp_get_ts_config - get hardware time stamping config
470 * @netdev: network interface device structure
471 * @ifr: interface request data
473 * Get the hwtstamp_config settings to return to the user. Rather than attempt
474 * to deconstruct the settings from the registers, just return a shadow copy
475 * of the last known settings.
477 int igc_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
479 struct igc_adapter *adapter = netdev_priv(netdev);
480 struct hwtstamp_config *config = &adapter->tstamp_config;
482 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
487 * igc_ptp_init - Initialize PTP functionality
488 * @adapter: Board private structure
490 * This function is called at device probe to initialize the PTP
493 void igc_ptp_init(struct igc_adapter *adapter)
495 struct net_device *netdev = adapter->netdev;
496 struct igc_hw *hw = &adapter->hw;
498 switch (hw->mac.type) {
500 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
501 adapter->ptp_caps.owner = THIS_MODULE;
502 adapter->ptp_caps.max_adj = 62499999;
503 adapter->ptp_caps.adjfine = igc_ptp_adjfine_i225;
504 adapter->ptp_caps.adjtime = igc_ptp_adjtime_i225;
505 adapter->ptp_caps.gettimex64 = igc_ptp_gettimex64_i225;
506 adapter->ptp_caps.settime64 = igc_ptp_settime_i225;
507 adapter->ptp_caps.enable = igc_ptp_feature_enable_i225;
510 adapter->ptp_clock = NULL;
514 spin_lock_init(&adapter->ptp_tx_lock);
515 spin_lock_init(&adapter->tmreg_lock);
516 INIT_WORK(&adapter->ptp_tx_work, igc_ptp_tx_work);
518 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
519 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
521 adapter->prev_ptp_time = ktime_to_timespec64(ktime_get_real());
522 adapter->ptp_reset_start = ktime_get();
524 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
525 &adapter->pdev->dev);
526 if (IS_ERR(adapter->ptp_clock)) {
527 adapter->ptp_clock = NULL;
528 netdev_err(netdev, "ptp_clock_register failed\n");
529 } else if (adapter->ptp_clock) {
530 netdev_info(netdev, "PHC added\n");
531 adapter->ptp_flags |= IGC_PTP_ENABLED;
535 static void igc_ptp_time_save(struct igc_adapter *adapter)
537 igc_ptp_read(adapter, &adapter->prev_ptp_time);
538 adapter->ptp_reset_start = ktime_get();
541 static void igc_ptp_time_restore(struct igc_adapter *adapter)
543 struct timespec64 ts = adapter->prev_ptp_time;
546 delta = ktime_sub(ktime_get(), adapter->ptp_reset_start);
548 timespec64_add_ns(&ts, ktime_to_ns(delta));
550 igc_ptp_write_i225(adapter, &ts);
554 * igc_ptp_suspend - Disable PTP work items and prepare for suspend
555 * @adapter: Board private structure
557 * This function stops the overflow check work and PTP Tx timestamp work, and
558 * will prepare the device for OS suspend.
560 void igc_ptp_suspend(struct igc_adapter *adapter)
562 if (!(adapter->ptp_flags & IGC_PTP_ENABLED))
565 cancel_work_sync(&adapter->ptp_tx_work);
566 dev_kfree_skb_any(adapter->ptp_tx_skb);
567 adapter->ptp_tx_skb = NULL;
569 if (pci_device_is_present(adapter->pdev))
570 igc_ptp_time_save(adapter);
574 * igc_ptp_stop - Disable PTP device and stop the overflow check.
575 * @adapter: Board private structure.
577 * This function stops the PTP support and cancels the delayed work.
579 void igc_ptp_stop(struct igc_adapter *adapter)
581 igc_ptp_suspend(adapter);
583 if (adapter->ptp_clock) {
584 ptp_clock_unregister(adapter->ptp_clock);
585 netdev_info(adapter->netdev, "PHC removed\n");
586 adapter->ptp_flags &= ~IGC_PTP_ENABLED;
591 * igc_ptp_reset - Re-enable the adapter for PTP following a reset.
592 * @adapter: Board private structure.
594 * This function handles the reset work required to re-enable the PTP device.
596 void igc_ptp_reset(struct igc_adapter *adapter)
598 struct igc_hw *hw = &adapter->hw;
601 /* reset the tstamp_config */
602 igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
604 spin_lock_irqsave(&adapter->tmreg_lock, flags);
606 switch (adapter->hw.mac.type) {
608 wr32(IGC_TSAUXC, 0x0);
609 wr32(IGC_TSSDP, 0x0);
610 wr32(IGC_TSIM, IGC_TSICR_INTERRUPTS);
611 wr32(IGC_IMS, IGC_IMS_TS);
618 /* Re-initialize the timer. */
619 if (hw->mac.type == igc_i225) {
620 igc_ptp_time_restore(adapter);
622 timecounter_init(&adapter->tc, &adapter->cc,
623 ktime_to_ns(ktime_get_real()));
626 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);