1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
7 #define E810_OUT_PROP_DELAY_NS 1
10 * ice_set_tx_tstamp - Enable or disable Tx timestamping
11 * @pf: The PF pointer to search in
12 * @on: bool value for whether timestamps are enabled or disabled
14 static void ice_set_tx_tstamp(struct ice_pf *pf, bool on)
20 vsi = ice_get_main_vsi(pf);
24 /* Set the timestamp enable flag for all the Tx rings */
25 ice_for_each_rxq(vsi, i) {
26 if (!vsi->tx_rings[i])
28 vsi->tx_rings[i]->ptp_tx = on;
31 /* Configure the Tx timestamp interrupt */
32 val = rd32(&pf->hw, PFINT_OICR_ENA);
34 val |= PFINT_OICR_TSYN_TX_M;
36 val &= ~PFINT_OICR_TSYN_TX_M;
37 wr32(&pf->hw, PFINT_OICR_ENA, val);
41 * ice_set_rx_tstamp - Enable or disable Rx timestamping
42 * @pf: The PF pointer to search in
43 * @on: bool value for whether timestamps are enabled or disabled
45 static void ice_set_rx_tstamp(struct ice_pf *pf, bool on)
50 vsi = ice_get_main_vsi(pf);
54 /* Set the timestamp flag for all the Rx rings */
55 ice_for_each_rxq(vsi, i) {
56 if (!vsi->rx_rings[i])
58 vsi->rx_rings[i]->ptp_rx = on;
63 * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit
64 * @pf: Board private structure
65 * @ena: bool value to enable or disable time stamp
67 * This function will configure timestamping during PTP initialization
68 * and deinitialization
70 static void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena)
72 ice_set_tx_tstamp(pf, ena);
73 ice_set_rx_tstamp(pf, ena);
76 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL;
77 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON;
79 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
80 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF;
85 * ice_get_ptp_clock_index - Get the PTP clock index
88 * Determine the clock index of the PTP clock associated with this device. If
89 * this is the PF controlling the clock, just use the local access to the
90 * clock device pointer.
92 * Otherwise, read from the driver shared parameters to determine the clock
95 * Returns: the index of the PTP clock associated with this device, or -1 if
96 * there is no associated clock.
98 int ice_get_ptp_clock_index(struct ice_pf *pf)
100 struct device *dev = ice_pf_to_dev(pf);
101 enum ice_aqc_driver_params param_idx;
102 struct ice_hw *hw = &pf->hw;
107 /* Use the ptp_clock structure if we're the main PF */
109 return ptp_clock_index(pf->ptp.clock);
111 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
113 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0;
115 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1;
117 err = ice_aq_get_driver_param(hw, param_idx, &value, NULL);
119 dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n",
120 err, ice_aq_str(hw->adminq.sq_last_status));
124 /* The PTP clock index is an integer, and will be between 0 and
125 * INT_MAX. The highest bit of the driver shared parameter is used to
126 * indicate whether or not the currently stored clock index is valid.
128 if (!(value & PTP_SHARED_CLK_IDX_VALID))
131 return value & ~PTP_SHARED_CLK_IDX_VALID;
135 * ice_set_ptp_clock_index - Set the PTP clock index
136 * @pf: the PF pointer
138 * Set the PTP clock index for this device into the shared driver parameters,
139 * so that other PFs associated with this device can read it.
141 * If the PF is unable to store the clock index, it will log an error, but
142 * will continue operating PTP.
144 static void ice_set_ptp_clock_index(struct ice_pf *pf)
146 struct device *dev = ice_pf_to_dev(pf);
147 enum ice_aqc_driver_params param_idx;
148 struct ice_hw *hw = &pf->hw;
156 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
158 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0;
160 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1;
162 value = (u32)ptp_clock_index(pf->ptp.clock);
163 if (value > INT_MAX) {
164 dev_err(dev, "PTP Clock index is too large to store\n");
167 value |= PTP_SHARED_CLK_IDX_VALID;
169 err = ice_aq_set_driver_param(hw, param_idx, value, NULL);
171 dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n",
172 err, ice_aq_str(hw->adminq.sq_last_status));
177 * ice_clear_ptp_clock_index - Clear the PTP clock index
178 * @pf: the PF pointer
180 * Clear the PTP clock index for this device. Must be called when
181 * unregistering the PTP clock, in order to ensure other PFs stop reporting
182 * a clock object that no longer exists.
184 static void ice_clear_ptp_clock_index(struct ice_pf *pf)
186 struct device *dev = ice_pf_to_dev(pf);
187 enum ice_aqc_driver_params param_idx;
188 struct ice_hw *hw = &pf->hw;
192 /* Do not clear the index if we don't own the timer */
193 if (!hw->func_caps.ts_func_info.src_tmr_owned)
196 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
198 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0;
200 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1;
202 err = ice_aq_set_driver_param(hw, param_idx, 0, NULL);
204 dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n",
205 err, ice_aq_str(hw->adminq.sq_last_status));
210 * ice_ptp_read_src_clk_reg - Read the source clock register
211 * @pf: Board private structure
212 * @sts: Optional parameter for holding a pair of system timestamps from
213 * the system clock. Will be ignored if NULL is given.
216 ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts)
218 struct ice_hw *hw = &pf->hw;
222 tmr_idx = ice_get_ptp_src_clock_index(hw);
223 /* Read the system timestamp pre PHC read */
224 ptp_read_system_prets(sts);
226 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
228 /* Read the system timestamp post PHC read */
229 ptp_read_system_postts(sts);
231 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
232 lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx));
235 /* if TIME_L rolled over read TIME_L again and update
238 ptp_read_system_prets(sts);
239 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
240 ptp_read_system_postts(sts);
241 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
244 return ((u64)hi << 32) | lo;
248 * ice_ptp_update_cached_phctime - Update the cached PHC time values
249 * @pf: Board specific private structure
251 * This function updates the system time values which are cached in the PF
252 * structure and the Rx rings.
254 * This function must be called periodically to ensure that the cached value
255 * is never more than 2 seconds old. It must also be called whenever the PHC
256 * time has been changed.
258 static void ice_ptp_update_cached_phctime(struct ice_pf *pf)
263 /* Read the current PHC time */
264 systime = ice_ptp_read_src_clk_reg(pf, NULL);
266 /* Update the cached PHC time stored in the PF structure */
267 WRITE_ONCE(pf->ptp.cached_phc_time, systime);
269 ice_for_each_vsi(pf, i) {
270 struct ice_vsi *vsi = pf->vsi[i];
276 if (vsi->type != ICE_VSI_PF)
279 ice_for_each_rxq(vsi, j) {
280 if (!vsi->rx_rings[j])
282 WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
288 * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
289 * @cached_phc_time: recently cached copy of PHC time
290 * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
292 * Hardware captures timestamps which contain only 32 bits of nominal
293 * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
294 * Note that the captured timestamp values may be 40 bits, but the lower
295 * 8 bits are sub-nanoseconds and generally discarded.
297 * Extend the 32bit nanosecond timestamp using the following algorithm and
300 * 1) have a recently cached copy of the PHC time
301 * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1
302 * seconds) before or after the PHC time was captured.
303 * 3) calculate the delta between the cached time and the timestamp
304 * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was
305 * captured after the PHC time. In this case, the full timestamp is just
306 * the cached PHC time plus the delta.
307 * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the
308 * timestamp was captured *before* the PHC time, i.e. because the PHC
309 * cache was updated after the timestamp was captured by hardware. In this
310 * case, the full timestamp is the cached time minus the inverse delta.
312 * This algorithm works even if the PHC time was updated after a Tx timestamp
313 * was requested, but before the Tx timestamp event was reported from
316 * This calculation primarily relies on keeping the cached PHC time up to
317 * date. If the timestamp was captured more than 2^31 nanoseconds after the
318 * PHC time, it is possible that the lower 32bits of PHC time have
319 * overflowed more than once, and we might generate an incorrect timestamp.
321 * This is prevented by (a) periodically updating the cached PHC time once
322 * a second, and (b) discarding any Tx timestamp packet if it has waited for
323 * a timestamp for more than one second.
325 static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
327 u32 delta, phc_time_lo;
330 /* Extract the lower 32 bits of the PHC time */
331 phc_time_lo = (u32)cached_phc_time;
333 /* Calculate the delta between the lower 32bits of the cached PHC
334 * time and the in_tstamp value
336 delta = (in_tstamp - phc_time_lo);
338 /* Do not assume that the in_tstamp is always more recent than the
339 * cached PHC time. If the delta is large, it indicates that the
340 * in_tstamp was taken in the past, and should be converted
343 if (delta > (U32_MAX / 2)) {
344 /* reverse the delta calculation here */
345 delta = (phc_time_lo - in_tstamp);
346 ns = cached_phc_time - delta;
348 ns = cached_phc_time + delta;
355 * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds
356 * @pf: Board private structure
357 * @in_tstamp: Ingress/egress 40b timestamp value
359 * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal
360 * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit.
362 * *--------------------------------------------------------------*
363 * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v |
364 * *--------------------------------------------------------------*
366 * The low bit is an indicator of whether the timestamp is valid. The next
367 * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow,
368 * and the remaining 32 bits are the lower 32 bits of the PHC timer.
370 * It is assumed that the caller verifies the timestamp is valid prior to
371 * calling this function.
373 * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
374 * time stored in the device private PTP structure as the basis for timestamp
377 * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
380 static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
382 const u64 mask = GENMASK_ULL(31, 0);
384 return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time,
385 (in_tstamp >> 8) & mask);
389 * ice_ptp_read_time - Read the time from the device
390 * @pf: Board private structure
391 * @ts: timespec structure to hold the current time value
392 * @sts: Optional parameter for holding a pair of system timestamps from
393 * the system clock. Will be ignored if NULL is given.
395 * This function reads the source clock registers and stores them in a timespec.
396 * However, since the registers are 64 bits of nanoseconds, we must convert the
397 * result to a timespec before we can return.
400 ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts,
401 struct ptp_system_timestamp *sts)
403 u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
405 *ts = ns_to_timespec64(time_ns);
409 * ice_ptp_write_init - Set PHC time to provided value
410 * @pf: Board private structure
411 * @ts: timespec structure that holds the new time value
413 * Set the PHC time to the specified time provided in the timespec.
415 static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
417 u64 ns = timespec64_to_ns(ts);
418 struct ice_hw *hw = &pf->hw;
420 return ice_ptp_init_time(hw, ns);
424 * ice_ptp_write_adj - Adjust PHC clock time atomically
425 * @pf: Board private structure
426 * @adj: Adjustment in nanoseconds
428 * Perform an atomic adjustment of the PHC time by the specified number of
431 static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
433 struct ice_hw *hw = &pf->hw;
435 return ice_ptp_adj_clock(hw, adj);
439 * ice_ptp_adjfine - Adjust clock increment rate
440 * @info: the driver's PTP info structure
441 * @scaled_ppm: Parts per million with 16-bit fractional field
443 * Adjust the frequency of the clock by the indicated scaled ppm from the
446 static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
448 struct ice_pf *pf = ptp_info_to_pf(info);
449 u64 freq, divisor = 1000000ULL;
450 struct ice_hw *hw = &pf->hw;
455 incval = ICE_PTP_NOMINAL_INCVAL_E810;
457 if (scaled_ppm < 0) {
459 scaled_ppm = -scaled_ppm;
462 while ((u64)scaled_ppm > div_u64(U64_MAX, incval)) {
463 /* handle overflow by scaling down the scaled_ppm and
464 * the divisor, losing some precision
470 freq = (incval * (u64)scaled_ppm) >> 16;
471 diff = div_u64(freq, divisor);
478 err = ice_ptp_write_incval_locked(hw, incval);
480 dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
489 * ice_ptp_extts_work - Workqueue task function
490 * @work: external timestamp work structure
492 * Service for PTP external clock event
494 static void ice_ptp_extts_work(struct kthread_work *work)
496 struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work);
497 struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
498 struct ptp_clock_event event;
499 struct ice_hw *hw = &pf->hw;
503 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
504 /* Event time is captured by one of the two matched registers
505 * GLTSYN_EVNT_L: 32 LSB of sampled time event
506 * GLTSYN_EVNT_H: 32 MSB of sampled time event
507 * Event is defined in GLTSYN_EVNT_0 register
509 for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
510 /* Check if channel is enabled */
511 if (pf->ptp.ext_ts_irq & (1 << chan)) {
512 lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
513 hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
514 event.timestamp = (((u64)hi) << 32) | lo;
515 event.type = PTP_CLOCK_EXTTS;
519 ptp_clock_event(pf->ptp.clock, &event);
520 pf->ptp.ext_ts_irq &= ~(1 << chan);
526 * ice_ptp_cfg_extts - Configure EXTTS pin and channel
527 * @pf: Board private structure
528 * @ena: true to enable; false to disable
529 * @chan: GPIO channel (0-3)
530 * @gpio_pin: GPIO pin
531 * @extts_flags: request flags from the ptp_extts_request.flags
534 ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin,
535 unsigned int extts_flags)
537 u32 func, aux_reg, gpio_reg, irq_reg;
538 struct ice_hw *hw = &pf->hw;
541 if (chan > (unsigned int)pf->ptp.info.n_ext_ts)
544 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
546 irq_reg = rd32(hw, PFINT_OICR_ENA);
549 /* Enable the interrupt */
550 irq_reg |= PFINT_OICR_TSYN_EVNT_M;
551 aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
553 #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0)
554 #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1)
556 /* set event level to requested edge */
557 if (extts_flags & PTP_FALLING_EDGE)
558 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
559 if (extts_flags & PTP_RISING_EDGE)
560 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
562 /* Write GPIO CTL reg.
563 * 0x1 is input sampled by EVENT register(channel)
564 * + num_in_channels * tmr_idx
566 func = 1 + chan + (tmr_idx * 3);
567 gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) &
568 GLGEN_GPIO_CTL_PIN_FUNC_M);
569 pf->ptp.ext_ts_chan |= (1 << chan);
571 /* clear the values we set to reset defaults */
574 pf->ptp.ext_ts_chan &= ~(1 << chan);
575 if (!pf->ptp.ext_ts_chan)
576 irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
579 wr32(hw, PFINT_OICR_ENA, irq_reg);
580 wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
581 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg);
587 * ice_ptp_cfg_clkout - Configure clock to generate periodic wave
588 * @pf: Board private structure
589 * @chan: GPIO channel (0-3)
590 * @config: desired periodic clk configuration. NULL will disable channel
591 * @store: If set to true the values will be stored
593 * Configure the internal clock generator modules to generate the clock wave of
596 static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan,
597 struct ice_perout_channel *config, bool store)
599 u64 current_time, period, start_time, phase;
600 struct ice_hw *hw = &pf->hw;
601 u32 func, val, gpio_pin;
604 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
606 /* 0. Reset mode & out_en in AUX_OUT */
607 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
609 /* If we're disabling the output, clear out CLKO and TGT and keep
612 if (!config || !config->ena) {
613 wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0);
614 wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0);
615 wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0);
617 val = GLGEN_GPIO_CTL_PIN_DIR_M;
618 gpio_pin = pf->ptp.perout_channels[chan].gpio_pin;
619 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
621 /* Store the value if requested */
623 memset(&pf->ptp.perout_channels[chan], 0,
624 sizeof(struct ice_perout_channel));
628 period = config->period;
629 start_time = config->start_time;
630 div64_u64_rem(start_time, period, &phase);
631 gpio_pin = config->gpio_pin;
633 /* 1. Write clkout with half of required period value */
635 dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
641 /* For proper operation, the GLTSYN_CLKO must be larger than clock tick
644 if (period <= MIN_PULSE || period > U32_MAX) {
645 dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33",
650 wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
652 /* Allow time for programming before start_time is hit */
653 current_time = ice_ptp_read_src_clk_reg(pf, NULL);
655 /* if start time is in the past start the timer at the nearest second
658 if (start_time < current_time)
659 start_time = div64_u64(current_time + NSEC_PER_MSEC - 1,
660 NSEC_PER_SEC) * NSEC_PER_SEC + phase;
662 start_time -= E810_OUT_PROP_DELAY_NS;
664 /* 2. Write TARGET time */
665 wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time));
666 wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time));
668 /* 3. Write AUX_OUT register */
669 val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
670 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
672 /* 4. write GPIO CTL reg */
673 func = 8 + chan + (tmr_idx * 4);
674 val = GLGEN_GPIO_CTL_PIN_DIR_M |
675 ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M);
676 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
678 /* Store the value if requested */
680 memcpy(&pf->ptp.perout_channels[chan], config,
681 sizeof(struct ice_perout_channel));
682 pf->ptp.perout_channels[chan].start_time = phase;
687 dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n");
692 * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC
693 * @info: the driver's PTP info structure
694 * @rq: The requested feature to change
695 * @on: Enable/disable flag
698 ice_ptp_gpio_enable_e810(struct ptp_clock_info *info,
699 struct ptp_clock_request *rq, int on)
701 struct ice_pf *pf = ptp_info_to_pf(info);
702 struct ice_perout_channel clk_cfg = {0};
708 case PTP_CLK_REQ_PEROUT:
709 chan = rq->perout.index;
710 if (chan == PPS_CLK_GEN_CHAN)
711 clk_cfg.gpio_pin = PPS_PIN_INDEX;
713 clk_cfg.gpio_pin = chan;
715 clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) +
716 rq->perout.period.nsec);
717 clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) +
718 rq->perout.start.nsec);
721 err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true);
723 case PTP_CLK_REQ_EXTTS:
724 chan = rq->extts.index;
727 err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin,
738 * ice_ptp_gettimex64 - Get the time of the clock
739 * @info: the driver's PTP info structure
740 * @ts: timespec64 structure to hold the current time value
741 * @sts: Optional parameter for holding a pair of system timestamps from
742 * the system clock. Will be ignored if NULL is given.
744 * Read the device clock and return the correct value on ns, after converting it
745 * into a timespec struct.
748 ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
749 struct ptp_system_timestamp *sts)
751 struct ice_pf *pf = ptp_info_to_pf(info);
752 struct ice_hw *hw = &pf->hw;
754 if (!ice_ptp_lock(hw)) {
755 dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n");
759 ice_ptp_read_time(pf, ts, sts);
766 * ice_ptp_settime64 - Set the time of the clock
767 * @info: the driver's PTP info structure
768 * @ts: timespec64 structure that holds the new time value
770 * Set the device clock to the user input value. The conversion from timespec
771 * to ns happens in the write function.
774 ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
776 struct ice_pf *pf = ptp_info_to_pf(info);
777 struct timespec64 ts64 = *ts;
778 struct ice_hw *hw = &pf->hw;
781 if (!ice_ptp_lock(hw)) {
786 err = ice_ptp_write_init(pf, &ts64);
790 ice_ptp_update_cached_phctime(pf);
794 dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
802 * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
803 * @info: the driver's PTP info structure
804 * @delta: Offset in nanoseconds to adjust the time by
806 static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
808 struct timespec64 now, then;
810 then = ns_to_timespec64(delta);
811 ice_ptp_gettimex64(info, &now, NULL);
812 now = timespec64_add(now, then);
814 return ice_ptp_settime64(info, (const struct timespec64 *)&now);
818 * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
819 * @info: the driver's PTP info structure
820 * @delta: Offset in nanoseconds to adjust the time by
822 static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
824 struct ice_pf *pf = ptp_info_to_pf(info);
825 struct ice_hw *hw = &pf->hw;
829 dev = ice_pf_to_dev(pf);
831 /* Hardware only supports atomic adjustments using signed 32-bit
832 * integers. For any adjustment outside this range, perform
833 * a non-atomic get->adjust->set flow.
835 if (delta > S32_MAX || delta < S32_MIN) {
836 dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
837 return ice_ptp_adjtime_nonatomic(info, delta);
840 if (!ice_ptp_lock(hw)) {
841 dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
845 err = ice_ptp_write_adj(pf, delta);
850 dev_err(dev, "PTP failed to adjust time, err %d\n", err);
854 ice_ptp_update_cached_phctime(pf);
860 * ice_ptp_get_ts_config - ioctl interface to read the timestamping config
861 * @pf: Board private structure
864 * Copy the timestamping config to user buffer
866 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
868 struct hwtstamp_config *config;
870 if (!test_bit(ICE_FLAG_PTP, pf->flags))
873 config = &pf->ptp.tstamp_config;
875 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
880 * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
881 * @pf: Board private structure
882 * @config: hwtstamp settings requested or saved
885 ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config)
887 /* Reserved for future extensions. */
891 switch (config->tx_type) {
892 case HWTSTAMP_TX_OFF:
893 ice_set_tx_tstamp(pf, false);
896 ice_set_tx_tstamp(pf, true);
902 switch (config->rx_filter) {
903 case HWTSTAMP_FILTER_NONE:
904 ice_set_rx_tstamp(pf, false);
906 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
907 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
908 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
909 case HWTSTAMP_FILTER_PTP_V2_EVENT:
910 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
911 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
912 case HWTSTAMP_FILTER_PTP_V2_SYNC:
913 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
914 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
915 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
916 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
917 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
918 case HWTSTAMP_FILTER_NTP_ALL:
919 case HWTSTAMP_FILTER_ALL:
920 config->rx_filter = HWTSTAMP_FILTER_ALL;
921 ice_set_rx_tstamp(pf, true);
931 * ice_ptp_set_ts_config - ioctl interface to control the timestamping
932 * @pf: Board private structure
935 * Get the user config and store it
937 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
939 struct hwtstamp_config config;
942 if (!test_bit(ICE_FLAG_PTP, pf->flags))
945 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
948 err = ice_ptp_set_timestamp_mode(pf, &config);
952 /* Save these settings for future reference */
953 pf->ptp.tstamp_config = config;
955 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
960 * ice_ptp_rx_hwtstamp - Check for an Rx timestamp
961 * @rx_ring: Ring to get the VSI info
962 * @rx_desc: Receive descriptor
963 * @skb: Particular skb to send timestamp with
965 * The driver receives a notification in the receive descriptor with timestamp.
966 * The timestamp is in ns, so we must convert the result first.
969 ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring,
970 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb)
975 /* Populate timesync data into skb */
976 if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) {
977 struct skb_shared_hwtstamps *hwtstamps;
979 /* Use ice_ptp_extend_32b_ts directly, using the ring-specific
980 * cached PHC value, rather than accessing the PF. This also
981 * allows us to simply pass the upper 32bits of nanoseconds
982 * directly. Calling ice_ptp_extend_40b_ts is unnecessary as
983 * it would just discard these bits itself.
985 ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
986 ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high);
988 hwtstamps = skb_hwtstamps(skb);
989 memset(hwtstamps, 0, sizeof(*hwtstamps));
990 hwtstamps->hwtstamp = ns_to_ktime(ts_ns);
995 * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs
996 * @info: PTP clock capabilities
998 static void ice_ptp_setup_pins_e810(struct ptp_clock_info *info)
1000 info->n_per_out = E810_N_PER_OUT;
1001 info->n_ext_ts = E810_N_EXT_TS;
1005 * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
1006 * @pf: Board private structure
1007 * @info: PTP info to fill
1009 * Assign functions to the PTP capabiltiies structure for E810 devices.
1010 * Functions which operate across all device families should be set directly
1011 * in ice_ptp_set_caps. Only add functions here which are distinct for e810
1015 ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info)
1017 info->enable = ice_ptp_gpio_enable_e810;
1019 ice_ptp_setup_pins_e810(info);
1023 * ice_ptp_set_caps - Set PTP capabilities
1024 * @pf: Board private structure
1026 static void ice_ptp_set_caps(struct ice_pf *pf)
1028 struct ptp_clock_info *info = &pf->ptp.info;
1029 struct device *dev = ice_pf_to_dev(pf);
1031 snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
1032 dev_driver_string(dev), dev_name(dev));
1033 info->owner = THIS_MODULE;
1034 info->max_adj = 999999999;
1035 info->adjtime = ice_ptp_adjtime;
1036 info->adjfine = ice_ptp_adjfine;
1037 info->gettimex64 = ice_ptp_gettimex64;
1038 info->settime64 = ice_ptp_settime64;
1040 ice_ptp_set_funcs_e810(pf, info);
1044 * ice_ptp_create_clock - Create PTP clock device for userspace
1045 * @pf: Board private structure
1047 * This function creates a new PTP clock device. It only creates one if we
1048 * don't already have one. Will return error if it can't create one, but success
1049 * if we already have a device. Should be used by ice_ptp_init to create clock
1050 * initially, and prevent global resets from creating new clock devices.
1052 static long ice_ptp_create_clock(struct ice_pf *pf)
1054 struct ptp_clock_info *info;
1055 struct ptp_clock *clock;
1058 /* No need to create a clock device if we already have one */
1062 ice_ptp_set_caps(pf);
1064 info = &pf->ptp.info;
1065 dev = ice_pf_to_dev(pf);
1067 /* Allocate memory for kernel pins interface */
1069 info->pin_config = devm_kcalloc(dev, info->n_pins,
1070 sizeof(*info->pin_config),
1072 if (!info->pin_config) {
1078 /* Attempt to register the clock before enabling the hardware. */
1079 clock = ptp_clock_register(info, dev);
1081 return PTR_ERR(clock);
1083 pf->ptp.clock = clock;
1089 * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port
1090 * @work: pointer to the kthread_work struct
1092 * Process timestamps captured by the PHY associated with this port. To do
1093 * this, loop over each index with a waiting skb.
1095 * If a given index has a valid timestamp, perform the following steps:
1097 * 1) copy the timestamp out of the PHY register
1098 * 4) clear the timestamp valid bit in the PHY register
1099 * 5) unlock the index by clearing the associated in_use bit.
1100 * 2) extend the 40b timestamp value to get a 64bit timestamp
1101 * 3) send that timestamp to the stack
1103 * After looping, if we still have waiting SKBs, then re-queue the work. This
1104 * may cause us effectively poll even when not strictly necessary. We do this
1105 * because it's possible a new timestamp was requested around the same time as
1106 * the interrupt. In some cases hardware might not interrupt us again when the
1107 * timestamp is captured.
1109 * Note that we only take the tracking lock when clearing the bit and when
1110 * checking if we need to re-queue this task. The only place where bits can be
1111 * set is the hard xmit routine where an SKB has a request flag set. The only
1112 * places where we clear bits are this work function, or the periodic cleanup
1113 * thread. If the cleanup thread clears a bit we're processing we catch it
1114 * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread
1115 * starts a new timestamp, we might not begin processing it right away but we
1116 * will notice it at the end when we re-queue the work item. If a Tx thread
1117 * starts a new timestamp just after this function exits without re-queuing,
1118 * the interrupt when the timestamp finishes should trigger. Avoiding holding
1119 * the lock for the entire function is important in order to ensure that Tx
1120 * threads do not get blocked while waiting for the lock.
1122 static void ice_ptp_tx_tstamp_work(struct kthread_work *work)
1124 struct ice_ptp_port *ptp_port;
1125 struct ice_ptp_tx *tx;
1130 tx = container_of(work, struct ice_ptp_tx, work);
1134 ptp_port = container_of(tx, struct ice_ptp_port, tx);
1135 pf = ptp_port_to_pf(ptp_port);
1138 for_each_set_bit(idx, tx->in_use, tx->len) {
1139 struct skb_shared_hwtstamps shhwtstamps = {};
1140 u8 phy_idx = idx + tx->quad_offset;
1141 u64 raw_tstamp, tstamp;
1142 struct sk_buff *skb;
1145 err = ice_read_phy_tstamp(hw, tx->quad, phy_idx,
1150 /* Check if the timestamp is valid */
1151 if (!(raw_tstamp & ICE_PTP_TS_VALID))
1154 /* clear the timestamp register, so that it won't show valid
1155 * again when re-used.
1157 ice_clear_phy_tstamp(hw, tx->quad, phy_idx);
1159 /* The timestamp is valid, so we'll go ahead and clear this
1160 * index and then send the timestamp up to the stack.
1162 spin_lock(&tx->lock);
1163 clear_bit(idx, tx->in_use);
1164 skb = tx->tstamps[idx].skb;
1165 tx->tstamps[idx].skb = NULL;
1166 spin_unlock(&tx->lock);
1168 /* it's (unlikely but) possible we raced with the cleanup
1169 * thread for discarding old timestamp requests.
1174 /* Extend the timestamp using cached PHC time */
1175 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
1176 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
1178 skb_tstamp_tx(skb, &shhwtstamps);
1179 dev_kfree_skb_any(skb);
1182 /* Check if we still have work to do. If so, re-queue this task to
1183 * poll for remaining timestamps.
1185 spin_lock(&tx->lock);
1186 if (!bitmap_empty(tx->in_use, tx->len))
1187 kthread_queue_work(pf->ptp.kworker, &tx->work);
1188 spin_unlock(&tx->lock);
1192 * ice_ptp_request_ts - Request an available Tx timestamp index
1193 * @tx: the PTP Tx timestamp tracker to request from
1194 * @skb: the SKB to associate with this timestamp request
1196 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
1200 /* Check if this tracker is initialized */
1204 spin_lock(&tx->lock);
1205 /* Find and set the first available index */
1206 idx = find_first_zero_bit(tx->in_use, tx->len);
1207 if (idx < tx->len) {
1208 /* We got a valid index that no other thread could have set. Store
1209 * a reference to the skb and the start time to allow discarding old
1212 set_bit(idx, tx->in_use);
1213 tx->tstamps[idx].start = jiffies;
1214 tx->tstamps[idx].skb = skb_get(skb);
1215 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1218 spin_unlock(&tx->lock);
1220 /* return the appropriate PHY timestamp register index, -1 if no
1221 * indexes were available.
1226 return idx + tx->quad_offset;
1230 * ice_ptp_process_ts - Spawn kthread work to handle timestamps
1231 * @pf: Board private structure
1233 * Queue work required to process the PTP Tx timestamps outside of interrupt
1236 void ice_ptp_process_ts(struct ice_pf *pf)
1238 if (pf->ptp.port.tx.init)
1239 kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work);
1243 * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
1244 * @tx: Tx tracking structure to initialize
1246 * Assumes that the length has already been initialized. Do not call directly,
1247 * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead.
1250 ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
1252 tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL);
1256 tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
1263 spin_lock_init(&tx->lock);
1264 kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work);
1272 * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
1273 * @pf: Board private structure
1274 * @tx: the tracker to flush
1277 ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
1281 for (idx = 0; idx < tx->len; idx++) {
1282 u8 phy_idx = idx + tx->quad_offset;
1284 /* Clear any potential residual timestamp in the PHY block */
1285 if (!pf->hw.reset_ongoing)
1286 ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx);
1288 if (tx->tstamps[idx].skb) {
1289 dev_kfree_skb_any(tx->tstamps[idx].skb);
1290 tx->tstamps[idx].skb = NULL;
1296 * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
1297 * @pf: Board private structure
1298 * @tx: Tx tracking structure to release
1300 * Free memory associated with the Tx timestamp tracker.
1303 ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
1307 kthread_cancel_work_sync(&tx->work);
1309 ice_ptp_flush_tx_tracker(pf, tx);
1321 * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps
1322 * @pf: Board private structure
1323 * @tx: the Tx tracking structure to initialize
1325 * Initialize the Tx timestamp tracker for this PF. For E810 devices, each
1326 * port has its own block of timestamps, independent of the other ports.
1329 ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx)
1331 tx->quad = pf->hw.port_info->lport;
1332 tx->quad_offset = 0;
1333 tx->len = INDEX_PER_QUAD;
1335 return ice_ptp_alloc_tx_tracker(tx);
1339 * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped
1340 * @tx: PTP Tx tracker to clean up
1342 * Loop through the Tx timestamp requests and see if any of them have been
1343 * waiting for a long time. Discard any SKBs that have been waiting for more
1344 * than 2 seconds. This is long enough to be reasonably sure that the
1345 * timestamp will never be captured. This might happen if the packet gets
1346 * discarded before it reaches the PHY timestamping block.
1348 static void ice_ptp_tx_tstamp_cleanup(struct ice_ptp_tx *tx)
1355 for_each_set_bit(idx, tx->in_use, tx->len) {
1356 struct sk_buff *skb;
1358 /* Check if this SKB has been waiting for too long */
1359 if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ))
1362 spin_lock(&tx->lock);
1363 skb = tx->tstamps[idx].skb;
1364 tx->tstamps[idx].skb = NULL;
1365 clear_bit(idx, tx->in_use);
1366 spin_unlock(&tx->lock);
1368 /* Free the SKB after we've cleared the bit */
1369 dev_kfree_skb_any(skb);
1373 static void ice_ptp_periodic_work(struct kthread_work *work)
1375 struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
1376 struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
1378 if (!test_bit(ICE_FLAG_PTP, pf->flags))
1381 ice_ptp_update_cached_phctime(pf);
1383 ice_ptp_tx_tstamp_cleanup(&pf->ptp.port.tx);
1385 /* Run twice a second */
1386 kthread_queue_delayed_work(ptp->kworker, &ptp->work,
1387 msecs_to_jiffies(500));
1391 * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
1392 * @pf: Board private structure
1394 * Setup and initialize a PTP clock device that represents the device hardware
1395 * clock. Save the clock index for other functions connected to the same
1396 * hardware resource.
1398 static int ice_ptp_init_owner(struct ice_pf *pf)
1400 struct device *dev = ice_pf_to_dev(pf);
1401 struct ice_hw *hw = &pf->hw;
1402 struct timespec64 ts;
1406 wr32(hw, GLTSYN_SYNC_DLAY, 0);
1408 /* Clear some HW residue and enable source clock */
1409 src_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1411 /* Enable source clocks */
1412 wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M);
1414 /* Enable PHY time sync */
1415 err = ice_ptp_init_phy_e810(hw);
1419 /* Clear event status indications for auxiliary pins */
1420 (void)rd32(hw, GLTSYN_STAT(src_idx));
1422 /* Acquire the global hardware lock */
1423 if (!ice_ptp_lock(hw)) {
1428 /* Write the increment time value to PHY and LAN */
1429 err = ice_ptp_write_incval(hw, ICE_PTP_NOMINAL_INCVAL_E810);
1435 ts = ktime_to_timespec64(ktime_get_real());
1436 /* Write the initial Time value to PHY and LAN */
1437 err = ice_ptp_write_init(pf, &ts);
1443 /* Release the global hardware lock */
1446 /* Ensure we have a clock device */
1447 err = ice_ptp_create_clock(pf);
1451 /* Store the PTP clock index for other PFs */
1452 ice_set_ptp_clock_index(pf);
1457 pf->ptp.clock = NULL;
1459 dev_err(dev, "PTP failed to register clock, err %d\n", err);
1465 * ice_ptp_init - Initialize the PTP support after device probe or reset
1466 * @pf: Board private structure
1468 * This function sets device up for PTP support. The first time it is run, it
1469 * will create a clock device. It does not create a clock device if one
1470 * already exists. It also reconfigures the device after a reset.
1472 void ice_ptp_init(struct ice_pf *pf)
1474 struct device *dev = ice_pf_to_dev(pf);
1475 struct kthread_worker *kworker;
1476 struct ice_hw *hw = &pf->hw;
1479 /* PTP is currently only supported on E810 devices */
1480 if (!ice_is_e810(hw))
1483 /* Check if this PF owns the source timer */
1484 if (hw->func_caps.ts_func_info.src_tmr_owned) {
1485 err = ice_ptp_init_owner(pf);
1490 /* Disable timestamping for both Tx and Rx */
1491 ice_ptp_cfg_timestamp(pf, false);
1493 /* Initialize the PTP port Tx timestamp tracker */
1494 ice_ptp_init_tx_e810(pf, &pf->ptp.port.tx);
1496 /* Initialize work functions */
1497 kthread_init_delayed_work(&pf->ptp.work, ice_ptp_periodic_work);
1498 kthread_init_work(&pf->ptp.extts_work, ice_ptp_extts_work);
1500 /* Allocate a kworker for handling work required for the ports
1501 * connected to the PTP hardware clock.
1503 kworker = kthread_create_worker(0, "ice-ptp-%s", dev_name(dev));
1504 if (IS_ERR(kworker)) {
1505 err = PTR_ERR(kworker);
1508 pf->ptp.kworker = kworker;
1510 set_bit(ICE_FLAG_PTP, pf->flags);
1512 /* Start periodic work going */
1513 kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 0);
1515 dev_info(dev, "PTP init successful\n");
1519 /* If we registered a PTP clock, release it */
1520 if (pf->ptp.clock) {
1521 ptp_clock_unregister(pf->ptp.clock);
1522 pf->ptp.clock = NULL;
1524 dev_err(dev, "PTP failed %d\n", err);
1528 * ice_ptp_release - Disable the driver/HW support and unregister the clock
1529 * @pf: Board private structure
1531 * This function handles the cleanup work required from the initialization by
1532 * clearing out the important information and unregistering the clock
1534 void ice_ptp_release(struct ice_pf *pf)
1536 /* Disable timestamping for both Tx and Rx */
1537 ice_ptp_cfg_timestamp(pf, false);
1539 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
1541 clear_bit(ICE_FLAG_PTP, pf->flags);
1543 kthread_cancel_delayed_work_sync(&pf->ptp.work);
1545 if (pf->ptp.kworker) {
1546 kthread_destroy_worker(pf->ptp.kworker);
1547 pf->ptp.kworker = NULL;
1553 ice_clear_ptp_clock_index(pf);
1554 ptp_clock_unregister(pf->ptp.clock);
1555 pf->ptp.clock = NULL;
1557 dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");