blob: 47d10ad39c180532645eeadc92a914d82e3c45b1 [file] [log] [blame]
Jeff Kirsherae06c702018-03-22 10:08:48 -07001// SPDX-License-Identifier: GPL-2.0
Jeff Kirsher51dce242018-04-26 08:08:09 -07002/* Copyright(c) 2013 - 2018 Intel Corporation. */
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00003
4#include "i40e.h"
Jacob Kellerbeb0dff2014-01-11 05:43:19 +00005#include <linux/ptp_classify.h>
6
7/* The XL710 timesync is very much like Intel's 82599 design when it comes to
8 * the fundamental clock design. However, the clock operations are much simpler
9 * in the XL710 because the device supports a full 64 bits of nanoseconds.
10 * Because the field is so wide, we can forgo the cycle counter and just
11 * operate with the nanosecond field directly without fear of overflow.
12 *
13 * Much like the 82599, the update period is dependent upon the link speed:
14 * At 40Gb link or no link, the period is 1.6ns.
15 * At 10Gb link, the period is multiplied by 2. (3.2ns)
16 * At 1Gb link, the period is multiplied by 20. (32ns)
17 * 1588 functionality is not supported at 100Mbps.
18 */
19#define I40E_PTP_40GB_INCVAL 0x0199999999ULL
20#define I40E_PTP_10GB_INCVAL 0x0333333333ULL
21#define I40E_PTP_1GB_INCVAL 0x2000000000ULL
22
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040023#define I40E_PRTTSYN_CTL1_TSYNTYPE_V1 BIT(I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
24#define I40E_PRTTSYN_CTL1_TSYNTYPE_V2 (2 << \
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000025 I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000026
27/**
28 * i40e_ptp_read - Read the PHC time from the device
29 * @pf: Board private structure
30 * @ts: timespec structure to hold the current time value
31 *
32 * This function reads the PRTTSYN_TIME registers and stores them in a
33 * timespec. However, since the registers are 64 bits of nanoseconds, we must
34 * convert the result to a timespec before we can return.
35 **/
Richard Cochran6f7a9b82015-03-29 23:12:02 +020036static void i40e_ptp_read(struct i40e_pf *pf, struct timespec64 *ts)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000037{
38 struct i40e_hw *hw = &pf->hw;
39 u32 hi, lo;
40 u64 ns;
41
42 /* The timer latches on the lowest register read. */
43 lo = rd32(hw, I40E_PRTTSYN_TIME_L);
44 hi = rd32(hw, I40E_PRTTSYN_TIME_H);
45
46 ns = (((u64)hi) << 32) | lo;
47
Richard Cochran6f7a9b82015-03-29 23:12:02 +020048 *ts = ns_to_timespec64(ns);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000049}
50
51/**
52 * i40e_ptp_write - Write the PHC time to the device
53 * @pf: Board private structure
54 * @ts: timespec structure that holds the new time value
55 *
56 * This function writes the PRTTSYN_TIME registers with the user value. Since
57 * we receive a timespec from the stack, we must convert that timespec into
58 * nanoseconds before programming the registers.
59 **/
Richard Cochran6f7a9b82015-03-29 23:12:02 +020060static void i40e_ptp_write(struct i40e_pf *pf, const struct timespec64 *ts)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000061{
62 struct i40e_hw *hw = &pf->hw;
Richard Cochran6f7a9b82015-03-29 23:12:02 +020063 u64 ns = timespec64_to_ns(ts);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +000064
65 /* The timer will not update until the high register is written, so
66 * write the low register first.
67 */
68 wr32(hw, I40E_PRTTSYN_TIME_L, ns & 0xFFFFFFFF);
69 wr32(hw, I40E_PRTTSYN_TIME_H, ns >> 32);
70}
71
72/**
73 * i40e_ptp_convert_to_hwtstamp - Convert device clock to system time
74 * @hwtstamps: Timestamp structure to update
75 * @timestamp: Timestamp from the hardware
76 *
77 * We need to convert the NIC clock value into a hwtstamp which can be used by
78 * the upper level timestamping functions. Since the timestamp is simply a 64-
79 * bit nanosecond value, we can call ns_to_ktime directly to handle this.
80 **/
81static void i40e_ptp_convert_to_hwtstamp(struct skb_shared_hwtstamps *hwtstamps,
82 u64 timestamp)
83{
84 memset(hwtstamps, 0, sizeof(*hwtstamps));
85
86 hwtstamps->hwtstamp = ns_to_ktime(timestamp);
87}
88
89/**
90 * i40e_ptp_adjfreq - Adjust the PHC frequency
91 * @ptp: The PTP clock structure
92 * @ppb: Parts per billion adjustment from the base
93 *
94 * Adjust the frequency of the PHC by the indicated parts per billion from the
95 * base frequency.
96 **/
97static int i40e_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
98{
99 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
100 struct i40e_hw *hw = &pf->hw;
101 u64 adj, freq, diff;
102 int neg_adj = 0;
103
104 if (ppb < 0) {
105 neg_adj = 1;
106 ppb = -ppb;
107 }
108
109 smp_mb(); /* Force any pending update before accessing. */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700110 adj = READ_ONCE(pf->ptp_base_adj);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000111
112 freq = adj;
113 freq *= ppb;
114 diff = div_u64(freq, 1000000000ULL);
115
116 if (neg_adj)
117 adj -= diff;
118 else
119 adj += diff;
120
121 wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
122 wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
123
124 return 0;
125}
126
127/**
128 * i40e_ptp_adjtime - Adjust the PHC time
129 * @ptp: The PTP clock structure
130 * @delta: Offset in nanoseconds to adjust the PHC time by
131 *
132 * Adjust the frequency of the PHC by the indicated parts per billion from the
133 * base frequency.
134 **/
135static int i40e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
136{
137 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
Jesse Brandeburg0ac30ce2017-06-20 15:16:56 -0700138 struct timespec64 now;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000139
Jacob Keller19551262016-10-05 09:30:43 -0700140 mutex_lock(&pf->tmreg_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000141
142 i40e_ptp_read(pf, &now);
Jesse Brandeburg0ac30ce2017-06-20 15:16:56 -0700143 timespec64_add_ns(&now, delta);
Richard Cochran6f7a9b82015-03-29 23:12:02 +0200144 i40e_ptp_write(pf, (const struct timespec64 *)&now);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000145
Jacob Keller19551262016-10-05 09:30:43 -0700146 mutex_unlock(&pf->tmreg_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000147
148 return 0;
149}
150
151/**
152 * i40e_ptp_gettime - Get the time of the PHC
153 * @ptp: The PTP clock structure
154 * @ts: timespec structure to hold the current time value
155 *
156 * Read the device clock and return the correct value on ns, after converting it
157 * into a timespec struct.
158 **/
Richard Cochran6f7a9b82015-03-29 23:12:02 +0200159static int i40e_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000160{
161 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000162
Jacob Keller19551262016-10-05 09:30:43 -0700163 mutex_lock(&pf->tmreg_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000164 i40e_ptp_read(pf, ts);
Jacob Keller19551262016-10-05 09:30:43 -0700165 mutex_unlock(&pf->tmreg_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000166
167 return 0;
168}
169
170/**
171 * i40e_ptp_settime - Set the time of the PHC
172 * @ptp: The PTP clock structure
173 * @ts: timespec structure that holds the new time value
174 *
175 * Set the device clock to the user input value. The conversion from timespec
176 * to ns happens in the write function.
177 **/
178static int i40e_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochran6f7a9b82015-03-29 23:12:02 +0200179 const struct timespec64 *ts)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000180{
181 struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000182
Jacob Keller19551262016-10-05 09:30:43 -0700183 mutex_lock(&pf->tmreg_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000184 i40e_ptp_write(pf, ts);
Jacob Keller19551262016-10-05 09:30:43 -0700185 mutex_unlock(&pf->tmreg_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000186
187 return 0;
188}
189
190/**
Jacob Keller69d1a70c2014-06-04 04:22:42 +0000191 * i40e_ptp_feature_enable - Enable/disable ancillary features of the PHC subsystem
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000192 * @ptp: The PTP clock structure
193 * @rq: The requested feature to change
194 * @on: Enable/disable flag
195 *
196 * The XL710 does not support any of the ancillary features of the PHC
197 * subsystem, so this function may just return.
198 **/
Jacob Keller69d1a70c2014-06-04 04:22:42 +0000199static int i40e_ptp_feature_enable(struct ptp_clock_info *ptp,
200 struct ptp_clock_request *rq, int on)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000201{
202 return -EOPNOTSUPP;
203}
204
205/**
Jacob Keller12490502016-10-05 09:30:44 -0700206 * i40e_ptp_update_latch_events - Read I40E_PRTTSYN_STAT_1 and latch events
207 * @pf: the PF data structure
208 *
209 * This function reads I40E_PRTTSYN_STAT_1 and updates the corresponding timers
210 * for noticed latch events. This allows the driver to keep track of the first
211 * time a latch event was noticed which will be used to help clear out Rx
212 * timestamps for packets that got dropped or lost.
213 *
214 * This function will return the current value of I40E_PRTTSYN_STAT_1 and is
215 * expected to be called only while under the ptp_rx_lock.
216 **/
217static u32 i40e_ptp_get_rx_events(struct i40e_pf *pf)
218{
219 struct i40e_hw *hw = &pf->hw;
220 u32 prttsyn_stat, new_latch_events;
221 int i;
222
223 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_1);
224 new_latch_events = prttsyn_stat & ~pf->latch_event_flags;
225
226 /* Update the jiffies time for any newly latched timestamp. This
227 * ensures that we store the time that we first discovered a timestamp
228 * was latched by the hardware. The service task will later determine
229 * if we should free the latch and drop that timestamp should too much
230 * time pass. This flow ensures that we only update jiffies for new
231 * events latched since the last time we checked, and not all events
232 * currently latched, so that the service task accounting remains
233 * accurate.
234 */
235 for (i = 0; i < 4; i++) {
236 if (new_latch_events & BIT(i))
237 pf->latch_events[i] = jiffies;
238 }
239
240 /* Finally, we store the current status of the Rx timestamp latches */
241 pf->latch_event_flags = prttsyn_stat;
242
243 return prttsyn_stat;
244}
245
246/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000247 * i40e_ptp_rx_hang - Detect error case when Rx timestamp registers are hung
Jacob Keller61189552017-05-03 10:29:01 -0700248 * @pf: The PF private data structure
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000249 * @vsi: The VSI with the rings relevant to 1588
250 *
251 * This watchdog task is scheduled to detect error case where hardware has
252 * dropped an Rx packet that was timestamped when the ring is full. The
253 * particular error is rare but leaves the device in a state unable to timestamp
254 * any future packets.
255 **/
Jacob Keller61189552017-05-03 10:29:01 -0700256void i40e_ptp_rx_hang(struct i40e_pf *pf)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000257{
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000258 struct i40e_hw *hw = &pf->hw;
Jacob Kellere6e3fc22016-12-02 12:32:58 -0800259 unsigned int i, cleared = 0;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000260
Jacob Kellerb535a012014-12-14 01:55:14 +0000261 /* Since we cannot turn off the Rx timestamp logic if the device is
262 * configured for Tx timestamping, we check if Rx timestamping is
263 * configured. We don't want to spuriously warn about Rx timestamp
264 * hangs if we don't care about the timestamps.
265 */
266 if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000267 return;
268
Jacob Keller12490502016-10-05 09:30:44 -0700269 spin_lock_bh(&pf->ptp_rx_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000270
Jacob Keller12490502016-10-05 09:30:44 -0700271 /* Update current latch times for Rx events */
272 i40e_ptp_get_rx_events(pf);
273
274 /* Check all the currently latched Rx events and see whether they have
275 * been latched for over a second. It is assumed that any timestamp
276 * should have been cleared within this time, or else it was captured
277 * for a dropped frame that the driver never received. Thus, we will
278 * clear any timestamp that has been latched for over 1 second.
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000279 */
Jacob Keller12490502016-10-05 09:30:44 -0700280 for (i = 0; i < 4; i++) {
281 if ((pf->latch_event_flags & BIT(i)) &&
282 time_is_before_jiffies(pf->latch_events[i] + HZ)) {
283 rd32(hw, I40E_PRTTSYN_RXTIME_H(i));
284 pf->latch_event_flags &= ~BIT(i);
Jacob Kellere6e3fc22016-12-02 12:32:58 -0800285 cleared++;
Jacob Keller12490502016-10-05 09:30:44 -0700286 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000287 }
288
Jacob Keller12490502016-10-05 09:30:44 -0700289 spin_unlock_bh(&pf->ptp_rx_lock);
Jacob Kellere6e3fc22016-12-02 12:32:58 -0800290
291 /* Log a warning if more than 2 timestamps got dropped in the same
292 * check. We don't want to warn about all drops because it can occur
293 * in normal scenarios such as PTP frames on multicast addresses we
294 * aren't listening to. However, administrator should know if this is
295 * the reason packets aren't receiving timestamps.
296 */
297 if (cleared > 2)
298 dev_dbg(&pf->pdev->dev,
299 "Dropped %d missed RXTIME timestamp events\n",
300 cleared);
301
302 /* Finally, update the rx_hwtstamp_cleared counter */
303 pf->rx_hwtstamp_cleared += cleared;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000304}
305
306/**
Jacob Keller0bc07062017-05-03 10:29:02 -0700307 * i40e_ptp_tx_hang - Detect error case when Tx timestamp register is hung
308 * @pf: The PF private data structure
309 *
310 * This watchdog task is run periodically to make sure that we clear the Tx
311 * timestamp logic if we don't obtain a timestamp in a reasonable amount of
312 * time. It is unexpected in the normal case but if it occurs it results in
313 * permanently prevent timestamps of future packets
314 **/
315void i40e_ptp_tx_hang(struct i40e_pf *pf)
316{
317 if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
318 return;
319
320 /* Nothing to do if we're not already waiting for a timestamp */
321 if (!test_bit(__I40E_PTP_TX_IN_PROGRESS, pf->state))
322 return;
323
324 /* We already have a handler routine which is run when we are notified
325 * of a Tx timestamp in the hardware. If we don't get an interrupt
326 * within a second it is reasonable to assume that we never will.
327 */
328 if (time_is_before_jiffies(pf->ptp_tx_start + HZ)) {
329 dev_kfree_skb_any(pf->ptp_tx_skb);
330 pf->ptp_tx_skb = NULL;
331 clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
332 pf->tx_hwtstamp_timeouts++;
333 }
334}
335
336/**
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000337 * i40e_ptp_tx_hwtstamp - Utility function which returns the Tx timestamp
338 * @pf: Board private structure
339 *
340 * Read the value of the Tx timestamp from the registers, convert it into a
341 * value consumable by the stack, and store that result into the shhwtstamps
342 * struct before returning it up the stack.
343 **/
344void i40e_ptp_tx_hwtstamp(struct i40e_pf *pf)
345{
346 struct skb_shared_hwtstamps shhwtstamps;
Jacob Kellerbbc4e7d2017-05-03 10:28:51 -0700347 struct sk_buff *skb = pf->ptp_tx_skb;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000348 struct i40e_hw *hw = &pf->hw;
349 u32 hi, lo;
350 u64 ns;
351
Jacob Keller22b47772014-12-14 01:55:09 +0000352 if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
353 return;
354
355 /* don't attempt to timestamp if we don't have an skb */
356 if (!pf->ptp_tx_skb)
357 return;
358
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000359 lo = rd32(hw, I40E_PRTTSYN_TXTIME_L);
360 hi = rd32(hw, I40E_PRTTSYN_TXTIME_H);
361
362 ns = (((u64)hi) << 32) | lo;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000363 i40e_ptp_convert_to_hwtstamp(&shhwtstamps, ns);
Jacob Kellerbbc4e7d2017-05-03 10:28:51 -0700364
365 /* Clear the bit lock as soon as possible after reading the register,
366 * and prior to notifying the stack via skb_tstamp_tx(). Otherwise
367 * applications might wake up and attempt to request another transmit
368 * timestamp prior to the bit lock being cleared.
369 */
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000370 pf->ptp_tx_skb = NULL;
Jacob Keller0da36b92017-04-19 09:25:55 -0400371 clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
Jacob Kellerbbc4e7d2017-05-03 10:28:51 -0700372
373 /* Notify the stack and free the skb after we've unlocked */
374 skb_tstamp_tx(skb, &shhwtstamps);
375 dev_kfree_skb_any(skb);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000376}
377
378/**
379 * i40e_ptp_rx_hwtstamp - Utility function which checks for an Rx timestamp
380 * @pf: Board private structure
381 * @skb: Particular skb to send timestamp with
382 * @index: Index into the receive timestamp registers for the timestamp
383 *
384 * The XL710 receives a notification in the receive descriptor with an offset
385 * into the set of RXTIME registers where the timestamp is for that skb. This
386 * function goes and fetches the receive timestamp from that offset, if a valid
387 * one exists. The RXTIME registers are in ns, so we must convert the result
388 * first.
389 **/
390void i40e_ptp_rx_hwtstamp(struct i40e_pf *pf, struct sk_buff *skb, u8 index)
391{
392 u32 prttsyn_stat, hi, lo;
393 struct i40e_hw *hw;
394 u64 ns;
395
396 /* Since we cannot turn off the Rx timestamp logic if the device is
397 * doing Tx timestamping, check if Rx timestamping is configured.
398 */
Jacob Keller22b47772014-12-14 01:55:09 +0000399 if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000400 return;
401
402 hw = &pf->hw;
403
Jacob Keller12490502016-10-05 09:30:44 -0700404 spin_lock_bh(&pf->ptp_rx_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000405
Jacob Keller12490502016-10-05 09:30:44 -0700406 /* Get current Rx events and update latch times */
407 prttsyn_stat = i40e_ptp_get_rx_events(pf);
408
409 /* TODO: Should we warn about missing Rx timestamp event? */
410 if (!(prttsyn_stat & BIT(index))) {
411 spin_unlock_bh(&pf->ptp_rx_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000412 return;
Jacob Keller12490502016-10-05 09:30:44 -0700413 }
414
415 /* Clear the latched event since we're about to read its register */
416 pf->latch_event_flags &= ~BIT(index);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000417
418 lo = rd32(hw, I40E_PRTTSYN_RXTIME_L(index));
419 hi = rd32(hw, I40E_PRTTSYN_RXTIME_H(index));
420
Jacob Keller12490502016-10-05 09:30:44 -0700421 spin_unlock_bh(&pf->ptp_rx_lock);
422
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000423 ns = (((u64)hi) << 32) | lo;
424
425 i40e_ptp_convert_to_hwtstamp(skb_hwtstamps(skb), ns);
426}
427
428/**
429 * i40e_ptp_set_increment - Utility function to update clock increment rate
430 * @pf: Board private structure
431 *
432 * During a link change, the DMA frequency that drives the 1588 logic will
433 * change. In order to keep the PRTTSYN_TIME registers in units of nanoseconds,
434 * we must update the increment value per clock tick.
435 **/
436void i40e_ptp_set_increment(struct i40e_pf *pf)
437{
438 struct i40e_link_status *hw_link_info;
439 struct i40e_hw *hw = &pf->hw;
440 u64 incval;
441
442 hw_link_info = &hw->phy.link_info;
443
444 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
445
446 switch (hw_link_info->link_speed) {
447 case I40E_LINK_SPEED_10GB:
448 incval = I40E_PTP_10GB_INCVAL;
449 break;
450 case I40E_LINK_SPEED_1GB:
451 incval = I40E_PTP_1GB_INCVAL;
452 break;
453 case I40E_LINK_SPEED_100MB:
Shannon Nelsone684fa32014-11-11 03:15:03 +0000454 {
455 static int warn_once;
456
457 if (!warn_once) {
458 dev_warn(&pf->pdev->dev,
459 "1588 functionality is not supported at 100 Mbps. Stopping the PHC.\n");
460 warn_once++;
461 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000462 incval = 0;
463 break;
Shannon Nelsone684fa32014-11-11 03:15:03 +0000464 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000465 case I40E_LINK_SPEED_40GB:
466 default:
467 incval = I40E_PTP_40GB_INCVAL;
468 break;
469 }
470
471 /* Write the new increment value into the increment register. The
472 * hardware will not update the clock until both registers have been
473 * written.
474 */
475 wr32(hw, I40E_PRTTSYN_INC_L, incval & 0xFFFFFFFF);
476 wr32(hw, I40E_PRTTSYN_INC_H, incval >> 32);
477
478 /* Update the base adjustement value. */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700479 WRITE_ONCE(pf->ptp_base_adj, incval);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000480 smp_mb(); /* Force the above update. */
481}
482
483/**
484 * i40e_ptp_get_ts_config - ioctl interface to read the HW timestamping
485 * @pf: Board private structure
486 * @ifreq: ioctl data
487 *
488 * Obtain the current hardware timestamping settigs as requested. To do this,
489 * keep a shadow copy of the timestamp settings rather than attempting to
490 * deconstruct it from the registers.
491 **/
492int i40e_ptp_get_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
493{
494 struct hwtstamp_config *config = &pf->tstamp_config;
495
Jacob Kellerfe88bda2014-11-11 20:05:58 +0000496 if (!(pf->flags & I40E_FLAG_PTP))
497 return -EOPNOTSUPP;
498
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000499 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
500 -EFAULT : 0;
501}
502
503/**
Jacob Keller18946452014-06-04 06:08:29 +0000504 * i40e_ptp_set_timestamp_mode - setup hardware for requested timestamp mode
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000505 * @pf: Board private structure
Jacob Keller18946452014-06-04 06:08:29 +0000506 * @config: hwtstamp settings requested or saved
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000507 *
Jacob Keller18946452014-06-04 06:08:29 +0000508 * Control hardware registers to enter the specific mode requested by the
509 * user. Also used during reset path to ensure that timestamp settings are
510 * maintained.
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000511 *
Jacob Keller18946452014-06-04 06:08:29 +0000512 * Note: modifies config in place, and may update the requested mode to be
513 * more broad if the specific filter is not directly supported.
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000514 **/
Jacob Keller18946452014-06-04 06:08:29 +0000515static int i40e_ptp_set_timestamp_mode(struct i40e_pf *pf,
516 struct hwtstamp_config *config)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000517{
518 struct i40e_hw *hw = &pf->hw;
Jacob Kellerfe88bda2014-11-11 20:05:58 +0000519 u32 tsyntype, regval;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000520
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000521 /* Reserved for future extensions. */
522 if (config->flags)
523 return -EINVAL;
524
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000525 switch (config->tx_type) {
526 case HWTSTAMP_TX_OFF:
527 pf->ptp_tx = false;
528 break;
529 case HWTSTAMP_TX_ON:
530 pf->ptp_tx = true;
531 break;
532 default:
533 return -ERANGE;
534 }
535
536 switch (config->rx_filter) {
537 case HWTSTAMP_FILTER_NONE:
538 pf->ptp_rx = false;
Jacob Keller4fda14c2014-12-14 01:55:15 +0000539 /* We set the type to V1, but do not enable UDP packet
540 * recognition. In this way, we should be as close to
541 * disabling PTP Rx timestamps as possible since V1 packets
542 * are always UDP, since L2 packets are a V2 feature.
543 */
544 tsyntype = I40E_PRTTSYN_CTL1_TSYNTYPE_V1;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000545 break;
546 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
547 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
548 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Jacob Kellerd36e41d2017-06-23 04:24:46 -0400549 if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
Jacob Keller1e28e862016-11-11 12:39:25 -0800550 return -ERANGE;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000551 pf->ptp_rx = true;
552 tsyntype = I40E_PRTTSYN_CTL1_V1MESSTYPE0_MASK |
553 I40E_PRTTSYN_CTL1_TSYNTYPE_V1 |
554 I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
555 config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
556 break;
557 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000558 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
559 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000560 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
561 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000562 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Jacob Kellerd36e41d2017-06-23 04:24:46 -0400563 if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
Jacob Keller1e28e862016-11-11 12:39:25 -0800564 return -ERANGE;
565 /* fall through */
566 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
567 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
568 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000569 pf->ptp_rx = true;
570 tsyntype = I40E_PRTTSYN_CTL1_V2MESSTYPE0_MASK |
Jacob Keller1e28e862016-11-11 12:39:25 -0800571 I40E_PRTTSYN_CTL1_TSYNTYPE_V2;
Jacob Kellerd36e41d2017-06-23 04:24:46 -0400572 if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE) {
Jacob Keller1e28e862016-11-11 12:39:25 -0800573 tsyntype |= I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
574 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
575 } else {
576 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
577 }
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000578 break;
Miroslav Lichvare3412572017-05-19 17:52:36 +0200579 case HWTSTAMP_FILTER_NTP_ALL:
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000580 case HWTSTAMP_FILTER_ALL:
581 default:
582 return -ERANGE;
583 }
584
585 /* Clear out all 1588-related registers to clear and unlatch them. */
Jacob Keller12490502016-10-05 09:30:44 -0700586 spin_lock_bh(&pf->ptp_rx_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000587 rd32(hw, I40E_PRTTSYN_STAT_0);
588 rd32(hw, I40E_PRTTSYN_TXTIME_H);
589 rd32(hw, I40E_PRTTSYN_RXTIME_H(0));
590 rd32(hw, I40E_PRTTSYN_RXTIME_H(1));
591 rd32(hw, I40E_PRTTSYN_RXTIME_H(2));
592 rd32(hw, I40E_PRTTSYN_RXTIME_H(3));
Jacob Keller12490502016-10-05 09:30:44 -0700593 pf->latch_event_flags = 0;
594 spin_unlock_bh(&pf->ptp_rx_lock);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000595
596 /* Enable/disable the Tx timestamp interrupt based on user input. */
597 regval = rd32(hw, I40E_PRTTSYN_CTL0);
598 if (pf->ptp_tx)
599 regval |= I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
600 else
601 regval &= ~I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
602 wr32(hw, I40E_PRTTSYN_CTL0, regval);
603
604 regval = rd32(hw, I40E_PFINT_ICR0_ENA);
605 if (pf->ptp_tx)
606 regval |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
607 else
608 regval &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
609 wr32(hw, I40E_PFINT_ICR0_ENA, regval);
610
Jacob Keller4fda14c2014-12-14 01:55:15 +0000611 /* Although there is no simple on/off switch for Rx, we "disable" Rx
612 * timestamps by setting to V1 only mode and clear the UDP
613 * recognition. This ought to disable all PTP Rx timestamps as V1
614 * packets are always over UDP. Note that software is configured to
615 * ignore Rx timestamps via the pf->ptp_rx flag.
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000616 */
Jacob Keller4fda14c2014-12-14 01:55:15 +0000617 regval = rd32(hw, I40E_PRTTSYN_CTL1);
618 /* clear everything but the enable bit */
619 regval &= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
620 /* now enable bits for desired Rx timestamps */
621 regval |= tsyntype;
622 wr32(hw, I40E_PRTTSYN_CTL1, regval);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000623
Jacob Keller18946452014-06-04 06:08:29 +0000624 return 0;
625}
626
627/**
628 * i40e_ptp_set_ts_config - ioctl interface to control the HW timestamping
629 * @pf: Board private structure
630 * @ifreq: ioctl data
631 *
632 * Respond to the user filter requests and make the appropriate hardware
633 * changes here. The XL710 cannot support splitting of the Tx/Rx timestamping
634 * logic, so keep track in software of whether to indicate these timestamps
635 * or not.
636 *
637 * It is permissible to "upgrade" the user request to a broader filter, as long
638 * as the user receives the timestamps they care about and the user is notified
639 * the filter has been broadened.
640 **/
641int i40e_ptp_set_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
642{
Jacob Kellerd19af2a2014-06-04 04:22:44 +0000643 struct hwtstamp_config config;
Jacob Keller18946452014-06-04 06:08:29 +0000644 int err;
645
Jacob Kellerfe88bda2014-11-11 20:05:58 +0000646 if (!(pf->flags & I40E_FLAG_PTP))
647 return -EOPNOTSUPP;
648
Jacob Kellerd19af2a2014-06-04 04:22:44 +0000649 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
Jacob Keller18946452014-06-04 06:08:29 +0000650 return -EFAULT;
651
Jacob Kellerd19af2a2014-06-04 04:22:44 +0000652 err = i40e_ptp_set_timestamp_mode(pf, &config);
Jacob Keller18946452014-06-04 06:08:29 +0000653 if (err)
654 return err;
655
Jacob Kellerd19af2a2014-06-04 04:22:44 +0000656 /* save these settings for future reference */
657 pf->tstamp_config = config;
658
659 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000660 -EFAULT : 0;
661}
662
663/**
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000664 * i40e_ptp_create_clock - Create PTP clock device for userspace
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000665 * @pf: Board private structure
666 *
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000667 * This function creates a new PTP clock device. It only creates one if we
668 * don't already have one, so it is safe to call. Will return error if it
669 * can't create one, but success if we already have a device. Should be used
670 * by i40e_ptp_init to create clock initially, and prevent global resets from
671 * creating new clock devices.
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000672 **/
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000673static long i40e_ptp_create_clock(struct i40e_pf *pf)
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000674{
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000675 /* no need to create a clock device if we already have one */
676 if (!IS_ERR_OR_NULL(pf->ptp_clock))
677 return 0;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000678
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000679 strncpy(pf->ptp_caps.name, i40e_driver_name, sizeof(pf->ptp_caps.name));
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000680 pf->ptp_caps.owner = THIS_MODULE;
681 pf->ptp_caps.max_adj = 999999999;
682 pf->ptp_caps.n_ext_ts = 0;
683 pf->ptp_caps.pps = 0;
684 pf->ptp_caps.adjfreq = i40e_ptp_adjfreq;
685 pf->ptp_caps.adjtime = i40e_ptp_adjtime;
Richard Cochran6f7a9b82015-03-29 23:12:02 +0200686 pf->ptp_caps.gettime64 = i40e_ptp_gettime;
687 pf->ptp_caps.settime64 = i40e_ptp_settime;
Jacob Keller69d1a70c2014-06-04 04:22:42 +0000688 pf->ptp_caps.enable = i40e_ptp_feature_enable;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000689
690 /* Attempt to register the clock before enabling the hardware. */
691 pf->ptp_clock = ptp_clock_register(&pf->ptp_caps, &pf->pdev->dev);
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400692 if (IS_ERR(pf->ptp_clock))
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000693 return PTR_ERR(pf->ptp_clock);
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000694
695 /* clear the hwtstamp settings here during clock create, instead of
696 * during regular init, so that we can maintain settings across a
697 * reset or suspend.
698 */
699 pf->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
700 pf->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
701
702 return 0;
703}
704
705/**
706 * i40e_ptp_init - Initialize the 1588 support after device probe or reset
707 * @pf: Board private structure
708 *
709 * This function sets device up for 1588 support. The first time it is run, it
710 * will create a PHC clock device. It does not create a clock device if one
711 * already exists. It also reconfigures the device after a reset.
712 **/
713void i40e_ptp_init(struct i40e_pf *pf)
714{
715 struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
716 struct i40e_hw *hw = &pf->hw;
Jacob Kellerfe88bda2014-11-11 20:05:58 +0000717 u32 pf_id;
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000718 long err;
719
Jacob Kellerfe88bda2014-11-11 20:05:58 +0000720 /* Only one PF is assigned to control 1588 logic per port. Do not
721 * enable any support for PFs not assigned via PRTTSYN_CTL0.PF_ID
722 */
723 pf_id = (rd32(hw, I40E_PRTTSYN_CTL0) & I40E_PRTTSYN_CTL0_PF_ID_MASK) >>
724 I40E_PRTTSYN_CTL0_PF_ID_SHIFT;
725 if (hw->pf_id != pf_id) {
726 pf->flags &= ~I40E_FLAG_PTP;
727 dev_info(&pf->pdev->dev, "%s: PTP not supported on %s\n",
728 __func__,
729 netdev->name);
730 return;
731 }
732
Jacob Keller19551262016-10-05 09:30:43 -0700733 mutex_init(&pf->tmreg_lock);
Jacob Keller12490502016-10-05 09:30:44 -0700734 spin_lock_init(&pf->ptp_rx_lock);
Jacob Kellerfbd5e2d2014-06-04 04:22:45 +0000735
736 /* ensure we have a clock device */
737 err = i40e_ptp_create_clock(pf);
738 if (err) {
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000739 pf->ptp_clock = NULL;
740 dev_err(&pf->pdev->dev, "%s: ptp_clock_register failed\n",
741 __func__);
Nicolas Pitreefee95f2016-09-20 19:25:58 -0400742 } else if (pf->ptp_clock) {
Richard Cochran6f7a9b82015-03-29 23:12:02 +0200743 struct timespec64 ts;
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000744 u32 regval;
745
Shannon Nelson6dec1012015-09-28 14:12:30 -0400746 if (pf->hw.debug_mask & I40E_DEBUG_LAN)
747 dev_info(&pf->pdev->dev, "PHC enabled\n");
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000748 pf->flags |= I40E_FLAG_PTP;
749
750 /* Ensure the clocks are running. */
751 regval = rd32(hw, I40E_PRTTSYN_CTL0);
752 regval |= I40E_PRTTSYN_CTL0_TSYNENA_MASK;
753 wr32(hw, I40E_PRTTSYN_CTL0, regval);
754 regval = rd32(hw, I40E_PRTTSYN_CTL1);
755 regval |= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
756 wr32(hw, I40E_PRTTSYN_CTL1, regval);
757
758 /* Set the increment value per clock tick. */
759 i40e_ptp_set_increment(pf);
760
Jacob Keller18946452014-06-04 06:08:29 +0000761 /* reset timestamping mode */
762 i40e_ptp_set_timestamp_mode(pf, &pf->tstamp_config);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000763
764 /* Set the clock value. */
Richard Cochran6f7a9b82015-03-29 23:12:02 +0200765 ts = ktime_to_timespec64(ktime_get_real());
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000766 i40e_ptp_settime(&pf->ptp_caps, &ts);
767 }
768}
769
770/**
771 * i40e_ptp_stop - Disable the driver/hardware support and unregister the PHC
772 * @pf: Board private structure
773 *
774 * This function handles the cleanup work required from the initialization by
775 * clearing out the important information and unregistering the PHC.
776 **/
777void i40e_ptp_stop(struct i40e_pf *pf)
778{
779 pf->flags &= ~I40E_FLAG_PTP;
780 pf->ptp_tx = false;
781 pf->ptp_rx = false;
782
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000783 if (pf->ptp_tx_skb) {
784 dev_kfree_skb_any(pf->ptp_tx_skb);
785 pf->ptp_tx_skb = NULL;
Jacob Keller0da36b92017-04-19 09:25:55 -0400786 clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
Jacob Kellerbeb0dff2014-01-11 05:43:19 +0000787 }
788
789 if (pf->ptp_clock) {
790 ptp_clock_unregister(pf->ptp_clock);
791 pf->ptp_clock = NULL;
792 dev_info(&pf->pdev->dev, "%s: removed PHC on %s\n", __func__,
793 pf->vsi[pf->lan_vsi]->netdev->name);
794 }
795}