ixgbe: Hardware Timestamping + PTP Hardware Clock (PHC)
[linux-2.6-block.git] / drivers / net / ethernet / intel / ixgbe / ixgbe_ptp.c
CommitLineData
3a6a4eda
JK
1/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
4 Copyright(c) 1999 - 2012 Intel Corporation.
5
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.
9
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
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27#include "ixgbe.h"
28#include <linux/export.h>
29
30/*
31 * The 82599 and the X540 do not have true 64bit nanosecond scale
32 * counter registers. Instead, SYSTIME is defined by a fixed point
33 * system which allows the user to define the scale counter increment
34 * value at every level change of the oscillator driving the SYSTIME
35 * value. For both devices the TIMINCA:IV field defines this
36 * increment. On the X540 device, 31 bits are provided. However on the
37 * 82599 only provides 24 bits. The time unit is determined by the
38 * clock frequency of the oscillator in combination with the TIMINCA
39 * register. When these devices link at 10Gb the oscillator has a
40 * period of 6.4ns. In order to convert the scale counter into
41 * nanoseconds the cyclecounter and timecounter structures are
42 * used. The SYSTIME registers need to be converted to ns values by use
43 * of only a right shift (division by power of 2). The following math
44 * determines the largest incvalue that will fit into the available
45 * bits in the TIMINCA register.
46 *
47 * PeriodWidth: Number of bits to store the clock period
48 * MaxWidth: The maximum width value of the TIMINCA register
49 * Period: The clock period for the oscillator
50 * round(): discard the fractional portion of the calculation
51 *
52 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ]
53 *
54 * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns
55 * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns
56 *
57 * The period also changes based on the link speed:
58 * At 10Gb link or no link, the period remains the same.
59 * At 1Gb link, the period is multiplied by 10. (64ns)
60 * At 100Mb link, the period is multiplied by 100. (640ns)
61 *
62 * The calculated value allows us to right shift the SYSTIME register
63 * value in order to quickly convert it into a nanosecond clock,
64 * while allowing for the maximum possible adjustment value.
65 *
66 * These diagrams are only for the 10Gb link period
67 *
68 * SYSTIMEH SYSTIMEL
69 * +--------------+ +--------------+
70 * X540 | 32 | | 1 | 3 | 28 |
71 * *--------------+ +--------------+
72 * \________ 36 bits ______/ fract
73 *
74 * +--------------+ +--------------+
75 * 82599 | 32 | | 8 | 3 | 21 |
76 * *--------------+ +--------------+
77 * \________ 43 bits ______/ fract
78 *
79 * The 36 bit X540 SYSTIME overflows every
80 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds
81 *
82 * The 43 bit 82599 SYSTIME overflows every
83 * 2^43 * 10^-9 / 3600 = 2.4 hours
84 */
85#define IXGBE_INCVAL_10GB 0x66666666
86#define IXGBE_INCVAL_1GB 0x40000000
87#define IXGBE_INCVAL_100 0x50000000
88
89#define IXGBE_INCVAL_SHIFT_10GB 28
90#define IXGBE_INCVAL_SHIFT_1GB 24
91#define IXGBE_INCVAL_SHIFT_100 21
92
93#define IXGBE_INCVAL_SHIFT_82599 7
94#define IXGBE_INCPER_SHIFT_82599 24
95#define IXGBE_MAX_TIMEADJ_VALUE 0x7FFFFFFFFFFFFFFFULL
96
97#define IXGBE_OVERFLOW_PERIOD (HZ * 30)
98
99/**
100 * ixgbe_ptp_read - read raw cycle counter (to be used by time counter)
101 * @cc - the cyclecounter structure
102 *
103 * this function reads the cyclecounter registers and is called by the
104 * cyclecounter structure used to construct a ns counter from the
105 * arbitrary fixed point registers
106 */
107static cycle_t ixgbe_ptp_read(const struct cyclecounter *cc)
108{
109 struct ixgbe_adapter *adapter =
110 container_of(cc, struct ixgbe_adapter, cc);
111 struct ixgbe_hw *hw = &adapter->hw;
112 u64 stamp = 0;
113
114 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
115 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32;
116
117 return stamp;
118}
119
120/**
121 * ixgbe_ptp_adjfreq
122 * @ptp - the ptp clock structure
123 * @ppb - parts per billion adjustment from base
124 *
125 * adjust the frequency of the ptp cycle counter by the
126 * indicated ppb from the base frequency.
127 */
128static int ixgbe_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
129{
130 struct ixgbe_adapter *adapter =
131 container_of(ptp, struct ixgbe_adapter, ptp_caps);
132 struct ixgbe_hw *hw = &adapter->hw;
133 u64 freq;
134 u32 diff, incval;
135 int neg_adj = 0;
136
137 if (ppb < 0) {
138 neg_adj = 1;
139 ppb = -ppb;
140 }
141
142 smp_mb();
143 incval = ACCESS_ONCE(adapter->base_incval);
144
145 freq = incval;
146 freq *= ppb;
147 diff = div_u64(freq, 1000000000ULL);
148
149 incval = neg_adj ? (incval - diff) : (incval + diff);
150
151 switch (hw->mac.type) {
152 case ixgbe_mac_X540:
153 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
154 break;
155 case ixgbe_mac_82599EB:
156 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
157 (1 << IXGBE_INCPER_SHIFT_82599) |
158 incval);
159 break;
160 default:
161 break;
162 }
163
164 return 0;
165}
166
167/**
168 * ixgbe_ptp_adjtime
169 * @ptp - the ptp clock structure
170 * @delta - offset to adjust the cycle counter by
171 *
172 * adjust the timer by resetting the timecounter structure.
173 */
174static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
175{
176 struct ixgbe_adapter *adapter =
177 container_of(ptp, struct ixgbe_adapter, ptp_caps);
178 unsigned long flags;
179 u64 now;
180
181 spin_lock_irqsave(&adapter->tmreg_lock, flags);
182
183 now = timecounter_read(&adapter->tc);
184 now += delta;
185
186 /* reset the timecounter */
187 timecounter_init(&adapter->tc,
188 &adapter->cc,
189 now);
190
191 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
192 return 0;
193}
194
195/**
196 * ixgbe_ptp_gettime
197 * @ptp - the ptp clock structure
198 * @ts - timespec structure to hold the current time value
199 *
200 * read the timecounter and return the correct value on ns,
201 * after converting it into a struct timespec.
202 */
203static int ixgbe_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
204{
205 struct ixgbe_adapter *adapter =
206 container_of(ptp, struct ixgbe_adapter, ptp_caps);
207 u64 ns;
208 u32 remainder;
209 unsigned long flags;
210
211 spin_lock_irqsave(&adapter->tmreg_lock, flags);
212 ns = timecounter_read(&adapter->tc);
213 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
214
215 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
216 ts->tv_nsec = remainder;
217
218 return 0;
219}
220
221/**
222 * ixgbe_ptp_settime
223 * @ptp - the ptp clock structure
224 * @ts - the timespec containing the new time for the cycle counter
225 *
226 * reset the timecounter to use a new base value instead of the kernel
227 * wall timer value.
228 */
229static int ixgbe_ptp_settime(struct ptp_clock_info *ptp,
230 const struct timespec *ts)
231{
232 struct ixgbe_adapter *adapter =
233 container_of(ptp, struct ixgbe_adapter, ptp_caps);
234 u64 ns;
235 unsigned long flags;
236
237 ns = ts->tv_sec * 1000000000ULL;
238 ns += ts->tv_nsec;
239
240 /* reset the timecounter */
241 spin_lock_irqsave(&adapter->tmreg_lock, flags);
242 timecounter_init(&adapter->tc, &adapter->cc, ns);
243 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
244
245 return 0;
246}
247
248/**
249 * ixgbe_ptp_enable
250 * @ptp - the ptp clock structure
251 * @rq - the requested feature to change
252 * @on - whether to enable or disable the feature
253 *
254 * enable (or disable) ancillary features of the phc subsystem.
255 * our driver does not support any of these features
256 */
257static int ixgbe_ptp_enable(struct ptp_clock_info *ptp,
258 struct ptp_clock_request *rq, int on)
259{
260 return -ENOTSUPP;
261}
262
263/**
264 * ixgbe_ptp_overflow_check - delayed work to detect SYSTIME overflow
265 * @work: structure containing information about this work task
266 *
267 * this work function is scheduled to continue reading the timecounter
268 * in order to prevent missing when the system time registers wrap
269 * around. This needs to be run approximately twice a minute when no
270 * PTP activity is occurring.
271 */
272void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter)
273{
274 unsigned long elapsed_jiffies = adapter->last_overflow_check - jiffies;
275 struct timespec ts;
276
277 if ((adapter->flags2 & IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED) &&
278 (elapsed_jiffies >= IXGBE_OVERFLOW_PERIOD)) {
279 ixgbe_ptp_gettime(&adapter->ptp_caps, &ts);
280 adapter->last_overflow_check = jiffies;
281 }
282}
283
284/**
285 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp
286 * @q_vector: structure containing interrupt and ring information
287 * @skb: particular skb to send timestamp with
288 *
289 * if the timestamp is valid, we convert it into the timecounter ns
290 * value, then store that result into the shhwtstamps structure which
291 * is passed up the network stack
292 */
293void ixgbe_ptp_tx_hwtstamp(struct ixgbe_q_vector *q_vector,
294 struct sk_buff *skb)
295{
296 struct ixgbe_adapter *adapter;
297 struct ixgbe_hw *hw;
298 struct skb_shared_hwtstamps shhwtstamps;
299 u64 regval = 0, ns;
300 u32 tsynctxctl;
301 unsigned long flags;
302
303 /* we cannot process timestamps on a ring without a q_vector */
304 if (!q_vector || !q_vector->adapter)
305 return;
306
307 adapter = q_vector->adapter;
308 hw = &adapter->hw;
309
310 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
311 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
312 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32;
313
314 /*
315 * if TX timestamp is not valid, exit after clearing the
316 * timestamp registers
317 */
318 if (!(tsynctxctl & IXGBE_TSYNCTXCTL_VALID))
319 return;
320
321 spin_lock_irqsave(&adapter->tmreg_lock, flags);
322 ns = timecounter_cyc2time(&adapter->tc, regval);
323 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
324
325 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
326 shhwtstamps.hwtstamp = ns_to_ktime(ns);
327 skb_tstamp_tx(skb, &shhwtstamps);
328}
329
330/**
331 * ixgbe_ptp_rx_hwtstamp - utility function which checks for RX time stamp
332 * @q_vector: structure containing interrupt and ring information
333 * @skb: particular skb to send timestamp with
334 *
335 * if the timestamp is valid, we convert it into the timecounter ns
336 * value, then store that result into the shhwtstamps structure which
337 * is passed up the network stack
338 */
339void ixgbe_ptp_rx_hwtstamp(struct ixgbe_q_vector *q_vector,
340 struct sk_buff *skb)
341{
342 struct ixgbe_adapter *adapter;
343 struct ixgbe_hw *hw;
344 struct skb_shared_hwtstamps *shhwtstamps;
345 u64 regval = 0, ns;
346 u32 tsyncrxctl;
347 unsigned long flags;
348
349 /* we cannot process timestamps on a ring without a q_vector */
350 if (!q_vector || !q_vector->adapter)
351 return;
352
353 adapter = q_vector->adapter;
354 hw = &adapter->hw;
355
356 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
357 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
358 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32;
359
360 /*
361 * If this bit is set, then the RX registers contain the time stamp. No
362 * other packet will be time stamped until we read these registers, so
363 * read the registers to make them available again. Because only one
364 * packet can be time stamped at a time, we know that the register
365 * values must belong to this one here and therefore we don't need to
366 * compare any of the additional attributes stored for it.
367 *
368 * If nothing went wrong, then it should have a skb_shared_tx that we
369 * can turn into a skb_shared_hwtstamps.
370 */
371 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID))
372 return;
373
374 spin_lock_irqsave(&adapter->tmreg_lock, flags);
375 ns = timecounter_cyc2time(&adapter->tc, regval);
376 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
377
378 shhwtstamps = skb_hwtstamps(skb);
379 shhwtstamps->hwtstamp = ns_to_ktime(ns);
380}
381
382/**
383 * ixgbe_ptp_hwtstamp_ioctl - control hardware time stamping
384 * @adapter: pointer to adapter struct
385 * @ifreq: ioctl data
386 * @cmd: particular ioctl requested
387 *
388 * Outgoing time stamping can be enabled and disabled. Play nice and
389 * disable it when requested, although it shouldn't case any overhead
390 * when no packet needs it. At most one packet in the queue may be
391 * marked for time stamping, otherwise it would be impossible to tell
392 * for sure to which packet the hardware time stamp belongs.
393 *
394 * Incoming time stamping has to be configured via the hardware
395 * filters. Not all combinations are supported, in particular event
396 * type has to be specified. Matching the kind of event packet is
397 * not supported, with the exception of "all V2 events regardless of
398 * level 2 or 4".
399 */
400int ixgbe_ptp_hwtstamp_ioctl(struct ixgbe_adapter *adapter,
401 struct ifreq *ifr, int cmd)
402{
403 struct ixgbe_hw *hw = &adapter->hw;
404 struct hwtstamp_config config;
405 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED;
406 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED;
407 u32 tsync_rx_mtrl = 0;
408 bool is_l4 = false;
409 bool is_l2 = false;
410 u32 regval;
411
412 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
413 return -EFAULT;
414
415 /* reserved for future extensions */
416 if (config.flags)
417 return -EINVAL;
418
419 switch (config.tx_type) {
420 case HWTSTAMP_TX_OFF:
421 tsync_tx_ctl = 0;
422 case HWTSTAMP_TX_ON:
423 break;
424 default:
425 return -ERANGE;
426 }
427
428 switch (config.rx_filter) {
429 case HWTSTAMP_FILTER_NONE:
430 tsync_rx_ctl = 0;
431 break;
432 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
433 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
434 tsync_rx_mtrl = IXGBE_RXMTRL_V1_SYNC_MSG;
435 is_l4 = true;
436 break;
437 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
438 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1;
439 tsync_rx_mtrl = IXGBE_RXMTRL_V1_DELAY_REQ_MSG;
440 is_l4 = true;
441 break;
442 case HWTSTAMP_FILTER_PTP_V2_SYNC:
443 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
444 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
445 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L2_L4_V2;
446 tsync_rx_mtrl = IXGBE_RXMTRL_V2_SYNC_MSG;
447 is_l2 = true;
448 is_l4 = true;
449 config.rx_filter = HWTSTAMP_FILTER_SOME;
450 break;
451 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
452 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
453 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
454 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L2_L4_V2;
455 tsync_rx_mtrl = IXGBE_RXMTRL_V2_DELAY_REQ_MSG;
456 is_l2 = true;
457 is_l4 = true;
458 config.rx_filter = HWTSTAMP_FILTER_SOME;
459 break;
460 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
461 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
462 case HWTSTAMP_FILTER_PTP_V2_EVENT:
463 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2;
464 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
465 is_l2 = true;
466 is_l4 = true;
467 break;
468 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
469 case HWTSTAMP_FILTER_ALL:
470 default:
471 /*
472 * register RXMTRL must be set, therefore it is not
473 * possible to time stamp both V1 Sync and Delay_Req messages
474 * and hardware does not support timestamping all packets
475 * => return error
476 */
477 return -ERANGE;
478 }
479
480 if (hw->mac.type == ixgbe_mac_82598EB) {
481 if (tsync_rx_ctl | tsync_tx_ctl)
482 return -ERANGE;
483 return 0;
484 }
485
486 /* define ethertype filter for timestamped packets */
487 if (is_l2)
488 IXGBE_WRITE_REG(hw, IXGBE_ETQF(3),
489 (IXGBE_ETQF_FILTER_EN | /* enable filter */
490 IXGBE_ETQF_1588 | /* enable timestamping */
491 ETH_P_1588)); /* 1588 eth protocol type */
492 else
493 IXGBE_WRITE_REG(hw, IXGBE_ETQF(3), 0);
494
495#define PTP_PORT 319
496 /* L4 Queue Filter[3]: filter by destination port and protocol */
497 if (is_l4) {
498 u32 ftqf = (IXGBE_FTQF_PROTOCOL_UDP /* UDP */
499 | IXGBE_FTQF_POOL_MASK_EN /* Pool not compared */
500 | IXGBE_FTQF_QUEUE_ENABLE);
501
502 ftqf |= ((IXGBE_FTQF_PROTOCOL_COMP_MASK /* protocol check */
503 & IXGBE_FTQF_DEST_PORT_MASK /* dest check */
504 & IXGBE_FTQF_SOURCE_PORT_MASK) /* source check */
505 << IXGBE_FTQF_5TUPLE_MASK_SHIFT);
506
507 IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(3),
508 (3 << IXGBE_IMIR_RX_QUEUE_SHIFT_82599 |
509 IXGBE_IMIR_SIZE_BP_82599));
510
511 /* enable port check */
512 IXGBE_WRITE_REG(hw, IXGBE_SDPQF(3),
513 (htons(PTP_PORT) |
514 htons(PTP_PORT) << 16));
515
516 IXGBE_WRITE_REG(hw, IXGBE_FTQF(3), ftqf);
517
518 tsync_rx_mtrl |= PTP_PORT << 16;
519 } else {
520 IXGBE_WRITE_REG(hw, IXGBE_FTQF(3), 0);
521 }
522
523 /* enable/disable TX */
524 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
525 regval &= ~IXGBE_TSYNCTXCTL_ENABLED;
526 regval |= tsync_tx_ctl;
527 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval);
528
529 /* enable/disable RX */
530 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
531 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK);
532 regval |= tsync_rx_ctl;
533 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval);
534
535 /* define which PTP packets are time stamped */
536 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl);
537
538 IXGBE_WRITE_FLUSH(hw);
539
540 /* clear TX/RX time stamp registers, just to be sure */
541 regval = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
542 regval = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
543
544 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
545 -EFAULT : 0;
546}
547
548/**
549 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw
550 * @adapter - pointer to the adapter structure
551 *
552 * this function initializes the timecounter and cyclecounter
553 * structures for use in generated a ns counter from the arbitrary
554 * fixed point cycles registers in the hardware.
555 *
556 * A change in link speed impacts the frequency of the DMA clock on
557 * the device, which is used to generate the cycle counter
558 * registers. Therefor this function is called whenever the link speed
559 * changes.
560 */
561void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter)
562{
563 struct ixgbe_hw *hw = &adapter->hw;
564 u32 incval = 0;
565 u32 shift = 0;
566 u32 cycle_speed;
567 unsigned long flags;
568
569 /**
570 * Determine what speed we need to set the cyclecounter
571 * for. It should be different for 100Mb, 1Gb, and 10Gb. Treat
572 * unknown speeds as 10Gb. (Hence why we can't just copy the
573 * link_speed.
574 */
575 switch (adapter->link_speed) {
576 case IXGBE_LINK_SPEED_100_FULL:
577 case IXGBE_LINK_SPEED_1GB_FULL:
578 case IXGBE_LINK_SPEED_10GB_FULL:
579 cycle_speed = adapter->link_speed;
580 break;
581 default:
582 /* cycle speed should be 10Gb when there is no link */
583 cycle_speed = IXGBE_LINK_SPEED_10GB_FULL;
584 break;
585 }
586
587 /* Bail if the cycle speed didn't change */
588 if (adapter->cycle_speed == cycle_speed)
589 return;
590
591 /**
592 * Scale the NIC cycle counter by a large factor so that
593 * relatively small corrections to the frequency can be added
594 * or subtracted. The drawbacks of a large factor include
595 * (a) the clock register overflows more quickly, (b) the cycle
596 * counter structure must be able to convert the systime value
597 * to nanoseconds using only a multiplier and a right-shift,
598 * and (c) the value must fit within the timinca register space
599 * => math based on internal DMA clock rate and available bits
600 */
601 switch (cycle_speed) {
602 case IXGBE_LINK_SPEED_100_FULL:
603 incval = IXGBE_INCVAL_100;
604 shift = IXGBE_INCVAL_SHIFT_100;
605 break;
606 case IXGBE_LINK_SPEED_1GB_FULL:
607 incval = IXGBE_INCVAL_1GB;
608 shift = IXGBE_INCVAL_SHIFT_1GB;
609 break;
610 case IXGBE_LINK_SPEED_10GB_FULL:
611 incval = IXGBE_INCVAL_10GB;
612 shift = IXGBE_INCVAL_SHIFT_10GB;
613 break;
614 }
615
616 /**
617 * Modify the calculated values to fit within the correct
618 * number of bits specified by the hardware. The 82599 doesn't
619 * have the same space as the X540, so bitshift the calculated
620 * values to fit.
621 */
622 switch (hw->mac.type) {
623 case ixgbe_mac_X540:
624 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
625 break;
626 case ixgbe_mac_82599EB:
627 incval >>= IXGBE_INCVAL_SHIFT_82599;
628 shift -= IXGBE_INCVAL_SHIFT_82599;
629 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
630 (1 << IXGBE_INCPER_SHIFT_82599) |
631 incval);
632 break;
633 default:
634 /* other devices aren't supported */
635 return;
636 }
637
638 /* reset the system time registers */
639 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x00000000);
640 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x00000000);
641 IXGBE_WRITE_FLUSH(hw);
642
643 /* store the new cycle speed */
644 adapter->cycle_speed = cycle_speed;
645
646 ACCESS_ONCE(adapter->base_incval) = incval;
647 smp_mb();
648
649 /* grab the ptp lock */
650 spin_lock_irqsave(&adapter->tmreg_lock, flags);
651
652 memset(&adapter->cc, 0, sizeof(adapter->cc));
653 adapter->cc.read = ixgbe_ptp_read;
654 adapter->cc.mask = CLOCKSOURCE_MASK(64);
655 adapter->cc.shift = shift;
656 adapter->cc.mult = 1;
657
658 /* reset the ns time counter */
659 timecounter_init(&adapter->tc, &adapter->cc,
660 ktime_to_ns(ktime_get_real()));
661
662 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
663}
664
665/**
666 * ixgbe_ptp_init
667 * @adapter - the ixgbe private adapter structure
668 *
669 * This function performs the required steps for enabling ptp
670 * support. If ptp support has already been loaded it simply calls the
671 * cyclecounter init routine and exits.
672 */
673void ixgbe_ptp_init(struct ixgbe_adapter *adapter)
674{
675 struct net_device *netdev = adapter->netdev;
676
677 switch (adapter->hw.mac.type) {
678 case ixgbe_mac_X540:
679 case ixgbe_mac_82599EB:
680 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
681 adapter->ptp_caps.owner = THIS_MODULE;
682 adapter->ptp_caps.max_adj = 250000000;
683 adapter->ptp_caps.n_alarm = 0;
684 adapter->ptp_caps.n_ext_ts = 0;
685 adapter->ptp_caps.n_per_out = 0;
686 adapter->ptp_caps.pps = 0;
687 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq;
688 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime;
689 adapter->ptp_caps.gettime = ixgbe_ptp_gettime;
690 adapter->ptp_caps.settime = ixgbe_ptp_settime;
691 adapter->ptp_caps.enable = ixgbe_ptp_enable;
692 break;
693 default:
694 adapter->ptp_clock = NULL;
695 return;
696 }
697
698 spin_lock_init(&adapter->tmreg_lock);
699
700 ixgbe_ptp_start_cyclecounter(adapter);
701
702 /* (Re)start the overflow check */
703 adapter->flags2 |= IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED;
704
705 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps);
706 if (IS_ERR(adapter->ptp_clock)) {
707 adapter->ptp_clock = NULL;
708 e_dev_err("ptp_clock_register failed\n");
709 } else
710 e_dev_info("registered PHC device on %s\n", netdev->name);
711
712 return;
713}
714
715/**
716 * ixgbe_ptp_stop - disable ptp device and stop the overflow check
717 * @adapter: pointer to adapter struct
718 *
719 * this function stops the ptp support, and cancels the delayed work.
720 */
721void ixgbe_ptp_stop(struct ixgbe_adapter *adapter)
722{
723 /* stop the overflow check task */
724 adapter->flags2 &= ~IXGBE_FLAG2_OVERFLOW_CHECK_ENABLED;
725
726 if (adapter->ptp_clock) {
727 ptp_clock_unregister(adapter->ptp_clock);
728 adapter->ptp_clock = NULL;
729 e_dev_info("removed PHC on %s\n",
730 adapter->netdev->name);
731 }
732}