1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 - 2014 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
21 * Contact Information:
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 ******************************************************************************/
28 #include <linux/export.h>
29 #include <linux/ptp_classify.h>
31 /* The XL710 timesync is very much like Intel's 82599 design when it comes to
32 * the fundamental clock design. However, the clock operations are much simpler
33 * in the XL710 because the device supports a full 64 bits of nanoseconds.
34 * Because the field is so wide, we can forgo the cycle counter and just
35 * operate with the nanosecond field directly without fear of overflow.
37 * Much like the 82599, the update period is dependent upon the link speed:
38 * At 40Gb link or no link, the period is 1.6ns.
39 * At 10Gb link, the period is multiplied by 2. (3.2ns)
40 * At 1Gb link, the period is multiplied by 20. (32ns)
41 * 1588 functionality is not supported at 100Mbps.
43 #define I40E_PTP_40GB_INCVAL 0x0199999999ULL
44 #define I40E_PTP_10GB_INCVAL 0x0333333333ULL
45 #define I40E_PTP_1GB_INCVAL 0x2000000000ULL
47 #define I40E_PRTTSYN_CTL1_TSYNTYPE_V1 (0x1 << \
48 I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
49 #define I40E_PRTTSYN_CTL1_TSYNTYPE_V2 (0x2 << \
50 I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
51 #define I40E_PTP_TX_TIMEOUT (HZ * 15)
54 * i40e_ptp_read - Read the PHC time from the device
55 * @pf: Board private structure
56 * @ts: timespec structure to hold the current time value
58 * This function reads the PRTTSYN_TIME registers and stores them in a
59 * timespec. However, since the registers are 64 bits of nanoseconds, we must
60 * convert the result to a timespec before we can return.
62 static void i40e_ptp_read(struct i40e_pf *pf, struct timespec *ts)
64 struct i40e_hw *hw = &pf->hw;
68 /* The timer latches on the lowest register read. */
69 lo = rd32(hw, I40E_PRTTSYN_TIME_L);
70 hi = rd32(hw, I40E_PRTTSYN_TIME_H);
72 ns = (((u64)hi) << 32) | lo;
74 *ts = ns_to_timespec(ns);
78 * i40e_ptp_write - Write the PHC time to the device
79 * @pf: Board private structure
80 * @ts: timespec structure that holds the new time value
82 * This function writes the PRTTSYN_TIME registers with the user value. Since
83 * we receive a timespec from the stack, we must convert that timespec into
84 * nanoseconds before programming the registers.
86 static void i40e_ptp_write(struct i40e_pf *pf, const struct timespec *ts)
88 struct i40e_hw *hw = &pf->hw;
89 u64 ns = timespec_to_ns(ts);
91 /* The timer will not update until the high register is written, so
92 * write the low register first.
94 wr32(hw, I40E_PRTTSYN_TIME_L, ns & 0xFFFFFFFF);
95 wr32(hw, I40E_PRTTSYN_TIME_H, ns >> 32);
99 * i40e_ptp_convert_to_hwtstamp - Convert device clock to system time
100 * @hwtstamps: Timestamp structure to update
101 * @timestamp: Timestamp from the hardware
103 * We need to convert the NIC clock value into a hwtstamp which can be used by
104 * the upper level timestamping functions. Since the timestamp is simply a 64-
105 * bit nanosecond value, we can call ns_to_ktime directly to handle this.
107 static void i40e_ptp_convert_to_hwtstamp(struct skb_shared_hwtstamps *hwtstamps,
110 memset(hwtstamps, 0, sizeof(*hwtstamps));
112 hwtstamps->hwtstamp = ns_to_ktime(timestamp);
116 * i40e_ptp_adjfreq - Adjust the PHC frequency
117 * @ptp: The PTP clock structure
118 * @ppb: Parts per billion adjustment from the base
120 * Adjust the frequency of the PHC by the indicated parts per billion from the
123 static int i40e_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
125 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
126 struct i40e_hw *hw = &pf->hw;
135 smp_mb(); /* Force any pending update before accessing. */
136 adj = ACCESS_ONCE(pf->ptp_base_adj);
140 diff = div_u64(freq, 1000000000ULL);
147 wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
148 wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
154 * i40e_ptp_adjtime - Adjust the PHC time
155 * @ptp: The PTP clock structure
156 * @delta: Offset in nanoseconds to adjust the PHC time by
158 * Adjust the frequency of the PHC by the indicated parts per billion from the
161 static int i40e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
163 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
164 struct timespec now, then = ns_to_timespec(delta);
167 spin_lock_irqsave(&pf->tmreg_lock, flags);
169 i40e_ptp_read(pf, &now);
170 now = timespec_add(now, then);
171 i40e_ptp_write(pf, (const struct timespec *)&now);
173 spin_unlock_irqrestore(&pf->tmreg_lock, flags);
179 * i40e_ptp_gettime - Get the time of the PHC
180 * @ptp: The PTP clock structure
181 * @ts: timespec structure to hold the current time value
183 * Read the device clock and return the correct value on ns, after converting it
184 * into a timespec struct.
186 static int i40e_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
188 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
191 spin_lock_irqsave(&pf->tmreg_lock, flags);
192 i40e_ptp_read(pf, ts);
193 spin_unlock_irqrestore(&pf->tmreg_lock, flags);
199 * i40e_ptp_settime - Set the time of the PHC
200 * @ptp: The PTP clock structure
201 * @ts: timespec structure that holds the new time value
203 * Set the device clock to the user input value. The conversion from timespec
204 * to ns happens in the write function.
206 static int i40e_ptp_settime(struct ptp_clock_info *ptp,
207 const struct timespec *ts)
209 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
212 spin_lock_irqsave(&pf->tmreg_lock, flags);
213 i40e_ptp_write(pf, ts);
214 spin_unlock_irqrestore(&pf->tmreg_lock, flags);
221 * @work: pointer to work struct
223 * This work function polls the PRTTSYN_STAT_0.TXTIME bit to determine when a
224 * Tx timestamp event has occurred, in order to pass the Tx timestamp value up
225 * the stack in the skb.
227 static void i40e_ptp_tx_work(struct work_struct *work)
229 struct i40e_pf *pf = container_of(work, struct i40e_pf,
231 struct i40e_hw *hw = &pf->hw;
237 if (time_is_before_jiffies(pf->ptp_tx_start +
238 I40E_PTP_TX_TIMEOUT)) {
239 dev_kfree_skb_any(pf->ptp_tx_skb);
240 pf->ptp_tx_skb = NULL;
241 pf->tx_hwtstamp_timeouts++;
242 dev_warn(&pf->pdev->dev, "clearing Tx timestamp hang");
246 prttsyn_stat_0 = rd32(hw, I40E_PRTTSYN_STAT_0);
247 if (prttsyn_stat_0 & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
248 i40e_ptp_tx_hwtstamp(pf);
250 schedule_work(&pf->ptp_tx_work);
254 * i40e_ptp_enable - Enable/disable ancillary features of the PHC subsystem
255 * @ptp: The PTP clock structure
256 * @rq: The requested feature to change
257 * @on: Enable/disable flag
259 * The XL710 does not support any of the ancillary features of the PHC
260 * subsystem, so this function may just return.
262 static int i40e_ptp_enable(struct ptp_clock_info *ptp,
263 struct ptp_clock_request *rq, int on)
269 * i40e_ptp_rx_hang - Detect error case when Rx timestamp registers are hung
270 * @vsi: The VSI with the rings relevant to 1588
272 * This watchdog task is scheduled to detect error case where hardware has
273 * dropped an Rx packet that was timestamped when the ring is full. The
274 * particular error is rare but leaves the device in a state unable to timestamp
275 * any future packets.
277 void i40e_ptp_rx_hang(struct i40e_vsi *vsi)
279 struct i40e_pf *pf = vsi->back;
280 struct i40e_hw *hw = &pf->hw;
281 struct i40e_ring *rx_ring;
282 unsigned long rx_event;
286 if (pf->flags & I40E_FLAG_PTP)
289 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_1);
291 /* Unless all four receive timestamp registers are latched, we are not
292 * concerned about a possible PTP Rx hang, so just update the timeout
295 if (!(prttsyn_stat & ((I40E_PRTTSYN_STAT_1_RXT0_MASK <<
296 I40E_PRTTSYN_STAT_1_RXT0_SHIFT) |
297 (I40E_PRTTSYN_STAT_1_RXT1_MASK <<
298 I40E_PRTTSYN_STAT_1_RXT1_SHIFT) |
299 (I40E_PRTTSYN_STAT_1_RXT2_MASK <<
300 I40E_PRTTSYN_STAT_1_RXT2_SHIFT) |
301 (I40E_PRTTSYN_STAT_1_RXT3_MASK <<
302 I40E_PRTTSYN_STAT_1_RXT3_SHIFT)))) {
303 pf->last_rx_ptp_check = jiffies;
307 /* Determine the most recent watchdog or rx_timestamp event. */
308 rx_event = pf->last_rx_ptp_check;
309 for (n = 0; n < vsi->num_queue_pairs; n++) {
310 rx_ring = vsi->rx_rings[n];
311 if (time_after(rx_ring->last_rx_timestamp, rx_event))
312 rx_event = rx_ring->last_rx_timestamp;
315 /* Only need to read the high RXSTMP register to clear the lock */
316 if (time_is_before_jiffies(rx_event + 5 * HZ)) {
317 rd32(hw, I40E_PRTTSYN_RXTIME_H(0));
318 rd32(hw, I40E_PRTTSYN_RXTIME_H(1));
319 rd32(hw, I40E_PRTTSYN_RXTIME_H(2));
320 rd32(hw, I40E_PRTTSYN_RXTIME_H(3));
321 pf->last_rx_ptp_check = jiffies;
322 pf->rx_hwtstamp_cleared++;
323 dev_warn(&vsi->back->pdev->dev,
324 "%s: clearing Rx timestamp hang",
330 * i40e_ptp_tx_hwtstamp - Utility function which returns the Tx timestamp
331 * @pf: Board private structure
333 * Read the value of the Tx timestamp from the registers, convert it into a
334 * value consumable by the stack, and store that result into the shhwtstamps
335 * struct before returning it up the stack.
337 void i40e_ptp_tx_hwtstamp(struct i40e_pf *pf)
339 struct skb_shared_hwtstamps shhwtstamps;
340 struct i40e_hw *hw = &pf->hw;
344 lo = rd32(hw, I40E_PRTTSYN_TXTIME_L);
345 hi = rd32(hw, I40E_PRTTSYN_TXTIME_H);
347 ns = (((u64)hi) << 32) | lo;
349 i40e_ptp_convert_to_hwtstamp(&shhwtstamps, ns);
350 skb_tstamp_tx(pf->ptp_tx_skb, &shhwtstamps);
351 dev_kfree_skb_any(pf->ptp_tx_skb);
352 pf->ptp_tx_skb = NULL;
356 * i40e_ptp_rx_hwtstamp - Utility function which checks for an Rx timestamp
357 * @pf: Board private structure
358 * @skb: Particular skb to send timestamp with
359 * @index: Index into the receive timestamp registers for the timestamp
361 * The XL710 receives a notification in the receive descriptor with an offset
362 * into the set of RXTIME registers where the timestamp is for that skb. This
363 * function goes and fetches the receive timestamp from that offset, if a valid
364 * one exists. The RXTIME registers are in ns, so we must convert the result
367 void i40e_ptp_rx_hwtstamp(struct i40e_pf *pf, struct sk_buff *skb, u8 index)
369 u32 prttsyn_stat, hi, lo;
373 /* Since we cannot turn off the Rx timestamp logic if the device is
374 * doing Tx timestamping, check if Rx timestamping is configured.
381 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_1);
383 if (!(prttsyn_stat & (1 << index)))
386 lo = rd32(hw, I40E_PRTTSYN_RXTIME_L(index));
387 hi = rd32(hw, I40E_PRTTSYN_RXTIME_H(index));
389 ns = (((u64)hi) << 32) | lo;
391 i40e_ptp_convert_to_hwtstamp(skb_hwtstamps(skb), ns);
395 * i40e_ptp_set_increment - Utility function to update clock increment rate
396 * @pf: Board private structure
398 * During a link change, the DMA frequency that drives the 1588 logic will
399 * change. In order to keep the PRTTSYN_TIME registers in units of nanoseconds,
400 * we must update the increment value per clock tick.
402 void i40e_ptp_set_increment(struct i40e_pf *pf)
404 struct i40e_link_status *hw_link_info;
405 struct i40e_hw *hw = &pf->hw;
408 hw_link_info = &hw->phy.link_info;
410 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
412 switch (hw_link_info->link_speed) {
413 case I40E_LINK_SPEED_10GB:
414 incval = I40E_PTP_10GB_INCVAL;
416 case I40E_LINK_SPEED_1GB:
417 incval = I40E_PTP_1GB_INCVAL;
419 case I40E_LINK_SPEED_100MB:
420 dev_warn(&pf->pdev->dev,
421 "%s: 1588 functionality is not supported at 100 Mbps. Stopping the PHC.\n",
425 case I40E_LINK_SPEED_40GB:
427 incval = I40E_PTP_40GB_INCVAL;
431 /* Write the new increment value into the increment register. The
432 * hardware will not update the clock until both registers have been
435 wr32(hw, I40E_PRTTSYN_INC_L, incval & 0xFFFFFFFF);
436 wr32(hw, I40E_PRTTSYN_INC_H, incval >> 32);
438 /* Update the base adjustement value. */
439 ACCESS_ONCE(pf->ptp_base_adj) = incval;
440 smp_mb(); /* Force the above update. */
444 * i40e_ptp_get_ts_config - ioctl interface to read the HW timestamping
445 * @pf: Board private structure
448 * Obtain the current hardware timestamping settigs as requested. To do this,
449 * keep a shadow copy of the timestamp settings rather than attempting to
450 * deconstruct it from the registers.
452 int i40e_ptp_get_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
454 struct hwtstamp_config *config = &pf->tstamp_config;
456 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
461 * i40e_ptp_set_ts_config - ioctl interface to control the HW timestamping
462 * @pf: Board private structure
465 * Respond to the user filter requests and make the appropriate hardware
466 * changes here. The XL710 cannot support splitting of the Tx/Rx timestamping
467 * logic, so keep track in software of whether to indicate these timestamps
470 * It is permissible to "upgrade" the user request to a broader filter, as long
471 * as the user receives the timestamps they care about and the user is notified
472 * the filter has been broadened.
474 int i40e_ptp_set_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
476 struct i40e_hw *hw = &pf->hw;
477 struct hwtstamp_config *config = &pf->tstamp_config;
478 u32 pf_id, tsyntype, regval;
480 if (copy_from_user(config, ifr->ifr_data, sizeof(*config)))
483 /* Reserved for future extensions. */
487 /* Confirm that 1588 is supported on this PF. */
488 pf_id = (rd32(hw, I40E_PRTTSYN_CTL0) & I40E_PRTTSYN_CTL0_PF_ID_MASK) >>
489 I40E_PRTTSYN_CTL0_PF_ID_SHIFT;
490 if (hw->pf_id != pf_id)
493 switch (config->tx_type) {
494 case HWTSTAMP_TX_OFF:
504 switch (config->rx_filter) {
505 case HWTSTAMP_FILTER_NONE:
509 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
510 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
511 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
513 tsyntype = I40E_PRTTSYN_CTL1_V1MESSTYPE0_MASK |
514 I40E_PRTTSYN_CTL1_TSYNTYPE_V1 |
515 I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
516 config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
518 case HWTSTAMP_FILTER_PTP_V2_EVENT:
519 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
520 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
521 case HWTSTAMP_FILTER_PTP_V2_SYNC:
522 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
523 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
524 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
525 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
526 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
528 tsyntype = I40E_PRTTSYN_CTL1_V2MESSTYPE0_MASK |
529 I40E_PRTTSYN_CTL1_TSYNTYPE_V2 |
530 I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
531 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
533 case HWTSTAMP_FILTER_ALL:
538 /* Clear out all 1588-related registers to clear and unlatch them. */
539 rd32(hw, I40E_PRTTSYN_STAT_0);
540 rd32(hw, I40E_PRTTSYN_TXTIME_H);
541 rd32(hw, I40E_PRTTSYN_RXTIME_H(0));
542 rd32(hw, I40E_PRTTSYN_RXTIME_H(1));
543 rd32(hw, I40E_PRTTSYN_RXTIME_H(2));
544 rd32(hw, I40E_PRTTSYN_RXTIME_H(3));
546 /* Enable/disable the Tx timestamp interrupt based on user input. */
547 regval = rd32(hw, I40E_PRTTSYN_CTL0);
549 regval |= I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
551 regval &= ~I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
552 wr32(hw, I40E_PRTTSYN_CTL0, regval);
554 regval = rd32(hw, I40E_PFINT_ICR0_ENA);
556 regval |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
558 regval &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
559 wr32(hw, I40E_PFINT_ICR0_ENA, regval);
561 /* There is no simple on/off switch for Rx. To "disable" Rx support,
562 * ignore any received timestamps, rather than turn off the clock.
565 regval = rd32(hw, I40E_PRTTSYN_CTL1);
566 /* clear everything but the enable bit */
567 regval &= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
568 /* now enable bits for desired Rx timestamps */
570 wr32(hw, I40E_PRTTSYN_CTL1, regval);
573 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
578 * i40e_ptp_init - Initialize the 1588 support and register the PHC
579 * @pf: Board private structure
581 * This function registers the device clock as a PHC. If it is successful, it
582 * starts the clock in the hardware.
584 void i40e_ptp_init(struct i40e_pf *pf)
586 struct i40e_hw *hw = &pf->hw;
587 struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
589 strncpy(pf->ptp_caps.name, "i40e", sizeof(pf->ptp_caps.name));
590 pf->ptp_caps.owner = THIS_MODULE;
591 pf->ptp_caps.max_adj = 999999999;
592 pf->ptp_caps.n_ext_ts = 0;
593 pf->ptp_caps.pps = 0;
594 pf->ptp_caps.adjfreq = i40e_ptp_adjfreq;
595 pf->ptp_caps.adjtime = i40e_ptp_adjtime;
596 pf->ptp_caps.gettime = i40e_ptp_gettime;
597 pf->ptp_caps.settime = i40e_ptp_settime;
598 pf->ptp_caps.enable = i40e_ptp_enable;
600 /* Attempt to register the clock before enabling the hardware. */
601 pf->ptp_clock = ptp_clock_register(&pf->ptp_caps, &pf->pdev->dev);
602 if (IS_ERR(pf->ptp_clock)) {
603 pf->ptp_clock = NULL;
604 dev_err(&pf->pdev->dev, "%s: ptp_clock_register failed\n",
610 spin_lock_init(&pf->tmreg_lock);
611 INIT_WORK(&pf->ptp_tx_work, i40e_ptp_tx_work);
613 dev_info(&pf->pdev->dev, "%s: added PHC on %s\n", __func__,
615 pf->flags |= I40E_FLAG_PTP;
617 /* Ensure the clocks are running. */
618 regval = rd32(hw, I40E_PRTTSYN_CTL0);
619 regval |= I40E_PRTTSYN_CTL0_TSYNENA_MASK;
620 wr32(hw, I40E_PRTTSYN_CTL0, regval);
621 regval = rd32(hw, I40E_PRTTSYN_CTL1);
622 regval |= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
623 wr32(hw, I40E_PRTTSYN_CTL1, regval);
625 /* Set the increment value per clock tick. */
626 i40e_ptp_set_increment(pf);
628 /* reset the tstamp_config */
629 memset(&pf->tstamp_config, 0, sizeof(pf->tstamp_config));
631 /* Set the clock value. */
632 ts = ktime_to_timespec(ktime_get_real());
633 i40e_ptp_settime(&pf->ptp_caps, &ts);
638 * i40e_ptp_stop - Disable the driver/hardware support and unregister the PHC
639 * @pf: Board private structure
641 * This function handles the cleanup work required from the initialization by
642 * clearing out the important information and unregistering the PHC.
644 void i40e_ptp_stop(struct i40e_pf *pf)
646 pf->flags &= ~I40E_FLAG_PTP;
650 cancel_work_sync(&pf->ptp_tx_work);
651 if (pf->ptp_tx_skb) {
652 dev_kfree_skb_any(pf->ptp_tx_skb);
653 pf->ptp_tx_skb = NULL;
657 ptp_clock_unregister(pf->ptp_clock);
658 pf->ptp_clock = NULL;
659 dev_info(&pf->pdev->dev, "%s: removed PHC on %s\n", __func__,
660 pf->vsi[pf->lan_vsi]->netdev->name);