1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_trace.h"
7 #include "ice_txclk.h"
8
9 static const char ice_pin_names[][64] = {
10 "SDP0",
11 "SDP1",
12 "SDP2",
13 "SDP3",
14 "TIME_SYNC",
15 "1PPS"
16 };
17
18 static const struct ice_ptp_pin_desc ice_pin_desc_e82x[] = {
19 /* name, gpio, delay */
20 { TIME_SYNC, { 4, -1 }, { 0, 0 }},
21 { ONE_PPS, { -1, 5 }, { 0, 11 }},
22 };
23
24 static const struct ice_ptp_pin_desc ice_pin_desc_e825c[] = {
25 /* name, gpio, delay */
26 { SDP0, { 0, 0 }, { 15, 14 }},
27 { SDP1, { 1, 1 }, { 15, 14 }},
28 { SDP2, { 2, 2 }, { 15, 14 }},
29 { SDP3, { 3, 3 }, { 15, 14 }},
30 { TIME_SYNC, { 4, -1 }, { 11, 0 }},
31 { ONE_PPS, { -1, 5 }, { 0, 9 }},
32 };
33
34 static const struct ice_ptp_pin_desc ice_pin_desc_e810[] = {
35 /* name, gpio, delay */
36 { SDP0, { 0, 0 }, { 0, 1 }},
37 { SDP1, { 1, 1 }, { 0, 1 }},
38 { SDP2, { 2, 2 }, { 0, 1 }},
39 { SDP3, { 3, 3 }, { 0, 1 }},
40 { ONE_PPS, { -1, 5 }, { 0, 1 }},
41 };
42
43 static const char ice_pin_names_dpll[][64] = {
44 "SDP20",
45 "SDP21",
46 "SDP22",
47 "SDP23",
48 };
49
50 static const struct ice_ptp_pin_desc ice_pin_desc_dpll[] = {
51 /* name, gpio, delay */
52 { SDP0, { -1, 0 }, { 0, 1 }},
53 { SDP1, { 1, -1 }, { 0, 0 }},
54 { SDP2, { -1, 2 }, { 0, 1 }},
55 { SDP3, { 3, -1 }, { 0, 0 }},
56 };
57
ice_get_ctrl_ptp(struct ice_pf * pf)58 static struct ice_ptp *ice_get_ctrl_ptp(struct ice_pf *pf)
59 {
60 struct ice_pf *ctrl_pf = ice_get_ctrl_pf(pf);
61
62 return !ctrl_pf ? NULL : &ctrl_pf->ptp;
63 }
64
65 /**
66 * ice_ptp_find_pin_idx - Find pin index in ptp_pin_desc
67 * @pf: Board private structure
68 * @func: Pin function
69 * @chan: GPIO channel
70 *
71 * Return: positive pin number when pin is present, -1 otherwise
72 */
ice_ptp_find_pin_idx(struct ice_pf * pf,enum ptp_pin_function func,unsigned int chan)73 static int ice_ptp_find_pin_idx(struct ice_pf *pf, enum ptp_pin_function func,
74 unsigned int chan)
75 {
76 const struct ptp_clock_info *info = &pf->ptp.info;
77 int i;
78
79 for (i = 0; i < info->n_pins; i++) {
80 if (info->pin_config[i].func == func &&
81 info->pin_config[i].chan == chan)
82 return i;
83 }
84
85 return -1;
86 }
87
88 /**
89 * ice_ptp_cfg_tx_interrupt - Configure Tx timestamp interrupt for the device
90 * @pf: Board private structure
91 *
92 * Program the device to respond appropriately to the Tx timestamp interrupt
93 * cause.
94 */
ice_ptp_cfg_tx_interrupt(struct ice_pf * pf)95 static void ice_ptp_cfg_tx_interrupt(struct ice_pf *pf)
96 {
97 struct ice_hw *hw = &pf->hw;
98 bool enable;
99 u32 val;
100
101 switch (pf->ptp.tx_interrupt_mode) {
102 case ICE_PTP_TX_INTERRUPT_ALL:
103 /* React to interrupts across all quads. */
104 wr32(hw, PFINT_TSYN_MSK + (0x4 * hw->pf_id), (u32)0x1f);
105 enable = true;
106 break;
107 case ICE_PTP_TX_INTERRUPT_NONE:
108 /* Do not react to interrupts on any quad. */
109 wr32(hw, PFINT_TSYN_MSK + (0x4 * hw->pf_id), (u32)0x0);
110 enable = false;
111 break;
112 case ICE_PTP_TX_INTERRUPT_SELF:
113 default:
114 enable = pf->ptp.tstamp_config.tx_type == HWTSTAMP_TX_ON;
115 break;
116 }
117
118 /* Configure the Tx timestamp interrupt */
119 val = rd32(hw, PFINT_OICR_ENA);
120 if (enable)
121 val |= PFINT_OICR_TSYN_TX_M;
122 else
123 val &= ~PFINT_OICR_TSYN_TX_M;
124 wr32(hw, PFINT_OICR_ENA, val);
125 }
126
127 /**
128 * ice_set_rx_tstamp - Enable or disable Rx timestamping
129 * @pf: The PF pointer to search in
130 * @on: bool value for whether timestamps are enabled or disabled
131 */
ice_set_rx_tstamp(struct ice_pf * pf,bool on)132 static void ice_set_rx_tstamp(struct ice_pf *pf, bool on)
133 {
134 struct ice_vsi *vsi;
135 u16 i;
136
137 vsi = ice_get_main_vsi(pf);
138 if (!vsi || !vsi->rx_rings)
139 return;
140
141 /* Set the timestamp flag for all the Rx rings */
142 ice_for_each_rxq(vsi, i) {
143 if (!vsi->rx_rings[i])
144 continue;
145 vsi->rx_rings[i]->ptp_rx = on;
146 }
147 }
148
149 /**
150 * ice_ptp_disable_timestamp_mode - Disable current timestamp mode
151 * @pf: Board private structure
152 *
153 * Called during preparation for reset to temporarily disable timestamping on
154 * the device. Called during remove to disable timestamping while cleaning up
155 * driver resources.
156 */
ice_ptp_disable_timestamp_mode(struct ice_pf * pf)157 static void ice_ptp_disable_timestamp_mode(struct ice_pf *pf)
158 {
159 struct ice_hw *hw = &pf->hw;
160 u32 val;
161
162 val = rd32(hw, PFINT_OICR_ENA);
163 val &= ~PFINT_OICR_TSYN_TX_M;
164 wr32(hw, PFINT_OICR_ENA, val);
165
166 ice_set_rx_tstamp(pf, false);
167 }
168
169 /**
170 * ice_ptp_restore_timestamp_mode - Restore timestamp configuration
171 * @pf: Board private structure
172 *
173 * Called at the end of rebuild to restore timestamp configuration after
174 * a device reset.
175 */
ice_ptp_restore_timestamp_mode(struct ice_pf * pf)176 void ice_ptp_restore_timestamp_mode(struct ice_pf *pf)
177 {
178 struct ice_hw *hw = &pf->hw;
179 bool enable_rx;
180
181 ice_ptp_cfg_tx_interrupt(pf);
182
183 enable_rx = pf->ptp.tstamp_config.rx_filter == HWTSTAMP_FILTER_ALL;
184 ice_set_rx_tstamp(pf, enable_rx);
185
186 /* Trigger an immediate software interrupt to ensure that timestamps
187 * which occurred during reset are handled now.
188 */
189 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
190 ice_flush(hw);
191 }
192
193 /**
194 * ice_ptp_read_src_clk_reg - Read the source clock register
195 * @pf: Board private structure
196 * @sts: Optional parameter for holding a pair of system timestamps from
197 * the system clock. Will be ignored if NULL is given.
198 */
ice_ptp_read_src_clk_reg(struct ice_pf * pf,struct ptp_system_timestamp * sts)199 u64 ice_ptp_read_src_clk_reg(struct ice_pf *pf,
200 struct ptp_system_timestamp *sts)
201 {
202 struct ice_hw *hw = &pf->hw;
203 u32 hi, lo, lo2;
204 u8 tmr_idx;
205
206 if (!ice_is_primary(hw))
207 hw = ice_get_primary_hw(pf);
208
209 tmr_idx = ice_get_ptp_src_clock_index(hw);
210 guard(spinlock)(&pf->adapter->ptp_gltsyn_time_lock);
211 /* Read the system timestamp pre PHC read */
212 ptp_read_system_prets(sts);
213
214 if (hw->mac_type == ICE_MAC_E830) {
215 u64 clk_time = rd64(hw, E830_GLTSYN_TIME_L(tmr_idx));
216
217 /* Read the system timestamp post PHC read */
218 ptp_read_system_postts(sts);
219
220 return clk_time;
221 }
222
223 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
224
225 /* Read the system timestamp post PHC read */
226 ptp_read_system_postts(sts);
227
228 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
229 lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx));
230
231 if (lo2 < lo) {
232 /* if TIME_L rolled over read TIME_L again and update
233 * system timestamps
234 */
235 ptp_read_system_prets(sts);
236 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
237 ptp_read_system_postts(sts);
238 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
239 }
240
241 return ((u64)hi << 32) | lo;
242 }
243
244 /**
245 * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
246 * @cached_phc_time: recently cached copy of PHC time
247 * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
248 *
249 * Hardware captures timestamps which contain only 32 bits of nominal
250 * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
251 * Note that the captured timestamp values may be 40 bits, but the lower
252 * 8 bits are sub-nanoseconds and generally discarded.
253 *
254 * Extend the 32bit nanosecond timestamp using the following algorithm and
255 * assumptions:
256 *
257 * 1) have a recently cached copy of the PHC time
258 * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1
259 * seconds) before or after the PHC time was captured.
260 * 3) calculate the delta between the cached time and the timestamp
261 * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was
262 * captured after the PHC time. In this case, the full timestamp is just
263 * the cached PHC time plus the delta.
264 * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the
265 * timestamp was captured *before* the PHC time, i.e. because the PHC
266 * cache was updated after the timestamp was captured by hardware. In this
267 * case, the full timestamp is the cached time minus the inverse delta.
268 *
269 * This algorithm works even if the PHC time was updated after a Tx timestamp
270 * was requested, but before the Tx timestamp event was reported from
271 * hardware.
272 *
273 * This calculation primarily relies on keeping the cached PHC time up to
274 * date. If the timestamp was captured more than 2^31 nanoseconds after the
275 * PHC time, it is possible that the lower 32bits of PHC time have
276 * overflowed more than once, and we might generate an incorrect timestamp.
277 *
278 * This is prevented by (a) periodically updating the cached PHC time once
279 * a second, and (b) discarding any Tx timestamp packet if it has waited for
280 * a timestamp for more than one second.
281 */
ice_ptp_extend_32b_ts(u64 cached_phc_time,u32 in_tstamp)282 static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
283 {
284 u32 delta, phc_time_lo;
285 u64 ns;
286
287 /* Extract the lower 32 bits of the PHC time */
288 phc_time_lo = (u32)cached_phc_time;
289
290 /* Calculate the delta between the lower 32bits of the cached PHC
291 * time and the in_tstamp value
292 */
293 delta = (in_tstamp - phc_time_lo);
294
295 /* Do not assume that the in_tstamp is always more recent than the
296 * cached PHC time. If the delta is large, it indicates that the
297 * in_tstamp was taken in the past, and should be converted
298 * forward.
299 */
300 if (delta > (U32_MAX / 2)) {
301 /* reverse the delta calculation here */
302 delta = (phc_time_lo - in_tstamp);
303 ns = cached_phc_time - delta;
304 } else {
305 ns = cached_phc_time + delta;
306 }
307
308 return ns;
309 }
310
311 /**
312 * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds
313 * @pf: Board private structure
314 * @in_tstamp: Ingress/egress 40b timestamp value
315 *
316 * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal
317 * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit.
318 *
319 * *--------------------------------------------------------------*
320 * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v |
321 * *--------------------------------------------------------------*
322 *
323 * The low bit is an indicator of whether the timestamp is valid. The next
324 * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow,
325 * and the remaining 32 bits are the lower 32 bits of the PHC timer.
326 *
327 * It is assumed that the caller verifies the timestamp is valid prior to
328 * calling this function.
329 *
330 * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
331 * time stored in the device private PTP structure as the basis for timestamp
332 * extension.
333 *
334 * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
335 * algorithm.
336 */
ice_ptp_extend_40b_ts(struct ice_pf * pf,u64 in_tstamp)337 static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
338 {
339 const u64 mask = GENMASK_ULL(31, 0);
340 unsigned long discard_time;
341
342 /* Discard the hardware timestamp if the cached PHC time is too old */
343 discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
344 if (time_is_before_jiffies(discard_time)) {
345 pf->ptp.tx_hwtstamp_discarded++;
346 return 0;
347 }
348
349 return ice_ptp_extend_32b_ts(READ_ONCE(pf->ptp.cached_phc_time),
350 (in_tstamp >> 8) & mask);
351 }
352
353 /**
354 * ice_ptp_is_tx_tracker_up - Check if Tx tracker is ready for new timestamps
355 * @tx: the PTP Tx timestamp tracker to check
356 *
357 * Check that a given PTP Tx timestamp tracker is up, i.e. that it is ready
358 * to accept new timestamp requests.
359 *
360 * Assumes the tx->lock spinlock is already held.
361 */
362 static bool
ice_ptp_is_tx_tracker_up(struct ice_ptp_tx * tx)363 ice_ptp_is_tx_tracker_up(struct ice_ptp_tx *tx)
364 {
365 lockdep_assert_held(&tx->lock);
366
367 return tx->init && !tx->calibrating;
368 }
369
370 /**
371 * ice_ptp_req_tx_single_tstamp - Request Tx timestamp for a port from FW
372 * @tx: the PTP Tx timestamp tracker
373 * @idx: index of the timestamp to request
374 */
ice_ptp_req_tx_single_tstamp(struct ice_ptp_tx * tx,u8 idx)375 void ice_ptp_req_tx_single_tstamp(struct ice_ptp_tx *tx, u8 idx)
376 {
377 struct ice_e810_params *params;
378 struct ice_ptp_port *ptp_port;
379 unsigned long flags;
380 struct sk_buff *skb;
381 struct ice_pf *pf;
382
383 if (!tx->init)
384 return;
385
386 ptp_port = container_of(tx, struct ice_ptp_port, tx);
387 pf = ptp_port_to_pf(ptp_port);
388 params = &pf->hw.ptp.phy.e810;
389
390 /* Drop packets which have waited for more than 2 seconds */
391 if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
392 /* Count the number of Tx timestamps that timed out */
393 pf->ptp.tx_hwtstamp_timeouts++;
394
395 skb = tx->tstamps[idx].skb;
396 tx->tstamps[idx].skb = NULL;
397 clear_bit(idx, tx->in_use);
398
399 dev_kfree_skb_any(skb);
400 return;
401 }
402
403 ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
404
405 spin_lock_irqsave(¶ms->atqbal_wq.lock, flags);
406
407 params->atqbal_flags |= ATQBAL_FLAGS_INTR_IN_PROGRESS;
408
409 /* Write TS index to read to the PF register so the FW can read it */
410 wr32(&pf->hw, REG_LL_PROXY_H,
411 REG_LL_PROXY_H_TS_INTR_ENA | FIELD_PREP(REG_LL_PROXY_H_TS_IDX, idx) |
412 REG_LL_PROXY_H_EXEC);
413 tx->last_ll_ts_idx_read = idx;
414
415 spin_unlock_irqrestore(¶ms->atqbal_wq.lock, flags);
416 }
417
418 /**
419 * ice_ptp_complete_tx_single_tstamp - Complete Tx timestamp for a port
420 * @tx: the PTP Tx timestamp tracker
421 */
ice_ptp_complete_tx_single_tstamp(struct ice_ptp_tx * tx)422 void ice_ptp_complete_tx_single_tstamp(struct ice_ptp_tx *tx)
423 {
424 struct skb_shared_hwtstamps shhwtstamps = {};
425 u8 idx = tx->last_ll_ts_idx_read;
426 struct ice_e810_params *params;
427 struct ice_ptp_port *ptp_port;
428 u64 raw_tstamp, tstamp;
429 bool drop_ts = false;
430 struct sk_buff *skb;
431 unsigned long flags;
432 struct device *dev;
433 struct ice_pf *pf;
434 u32 reg_ll_high;
435
436 if (!tx->init || tx->last_ll_ts_idx_read < 0)
437 return;
438
439 ptp_port = container_of(tx, struct ice_ptp_port, tx);
440 pf = ptp_port_to_pf(ptp_port);
441 dev = ice_pf_to_dev(pf);
442 params = &pf->hw.ptp.phy.e810;
443
444 ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
445
446 spin_lock_irqsave(¶ms->atqbal_wq.lock, flags);
447
448 if (!(params->atqbal_flags & ATQBAL_FLAGS_INTR_IN_PROGRESS))
449 dev_dbg(dev, "%s: low latency interrupt request not in progress?\n",
450 __func__);
451
452 /* Read the low 32 bit value */
453 raw_tstamp = rd32(&pf->hw, REG_LL_PROXY_L);
454 /* Read the status together with high TS part */
455 reg_ll_high = rd32(&pf->hw, REG_LL_PROXY_H);
456
457 /* Wake up threads waiting on low latency interface */
458 params->atqbal_flags &= ~ATQBAL_FLAGS_INTR_IN_PROGRESS;
459
460 wake_up_locked(¶ms->atqbal_wq);
461
462 spin_unlock_irqrestore(¶ms->atqbal_wq.lock, flags);
463
464 /* When the bit is cleared, the TS is ready in the register */
465 if (reg_ll_high & REG_LL_PROXY_H_EXEC) {
466 dev_err(ice_pf_to_dev(pf), "Failed to get the Tx tstamp - FW not ready");
467 return;
468 }
469
470 /* High 8 bit value of the TS is on the bits 16:23 */
471 raw_tstamp |= ((u64)FIELD_GET(REG_LL_PROXY_H_TS_HIGH, reg_ll_high)) << 32;
472
473 /* Devices using this interface always verify the timestamp differs
474 * relative to the last cached timestamp value.
475 */
476 if (raw_tstamp == tx->tstamps[idx].cached_tstamp)
477 return;
478
479 tx->tstamps[idx].cached_tstamp = raw_tstamp;
480 clear_bit(idx, tx->in_use);
481 skb = tx->tstamps[idx].skb;
482 tx->tstamps[idx].skb = NULL;
483 if (test_and_clear_bit(idx, tx->stale))
484 drop_ts = true;
485
486 if (!skb)
487 return;
488
489 if (drop_ts) {
490 dev_kfree_skb_any(skb);
491 return;
492 }
493
494 /* Extend the timestamp using cached PHC time */
495 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
496 if (tstamp) {
497 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
498 ice_trace(tx_tstamp_complete, skb, idx);
499
500 /* Count the number of Tx timestamps that succeeded */
501 pf->ptp.tx_hwtstamp_good++;
502 }
503
504 skb_tstamp_tx(skb, &shhwtstamps);
505 dev_kfree_skb_any(skb);
506 }
507
508 /**
509 * ice_ptp_process_tx_tstamp - Process Tx timestamps for a port
510 * @tx: the PTP Tx timestamp tracker
511 *
512 * Process timestamps captured by the PHY associated with this port. To do
513 * this, loop over each index with a waiting skb.
514 *
515 * If a given index has a valid timestamp, perform the following steps:
516 *
517 * 1) check that the timestamp request is not stale
518 * 2) check that a timestamp is ready and available in the PHY memory bank
519 * 3) read and copy the timestamp out of the PHY register
520 * 4) unlock the index by clearing the associated in_use bit
521 * 5) check if the timestamp is stale, and discard if so
522 * 6) extend the 40 bit timestamp value to get a 64 bit timestamp value
523 * 7) send this 64 bit timestamp to the stack
524 *
525 * Note that we do not hold the tracking lock while reading the Tx timestamp.
526 * This is because reading the timestamp requires taking a mutex that might
527 * sleep.
528 *
529 * The only place where we set in_use is when a new timestamp is initiated
530 * with a slot index. This is only called in the hard xmit routine where an
531 * SKB has a request flag set. The only places where we clear this bit is this
532 * function, or during teardown when the Tx timestamp tracker is being
533 * removed. A timestamp index will never be re-used until the in_use bit for
534 * that index is cleared.
535 *
536 * If a Tx thread starts a new timestamp, we might not begin processing it
537 * right away but we will notice it at the end when we re-queue the task.
538 *
539 * If a Tx thread starts a new timestamp just after this function exits, the
540 * interrupt for that timestamp should re-trigger this function once
541 * a timestamp is ready.
542 *
543 * In cases where the PTP hardware clock was directly adjusted, some
544 * timestamps may not be able to safely use the timestamp extension math. In
545 * this case, software will set the stale bit for any outstanding Tx
546 * timestamps when the clock is adjusted. Then this function will discard
547 * those captured timestamps instead of sending them to the stack.
548 *
549 * If a Tx packet has been waiting for more than 2 seconds, it is not possible
550 * to correctly extend the timestamp using the cached PHC time. It is
551 * extremely unlikely that a packet will ever take this long to timestamp. If
552 * we detect a Tx timestamp request that has waited for this long we assume
553 * the packet will never be sent by hardware and discard it without reading
554 * the timestamp register.
555 */
ice_ptp_process_tx_tstamp(struct ice_ptp_tx * tx)556 static void ice_ptp_process_tx_tstamp(struct ice_ptp_tx *tx)
557 {
558 struct ice_ptp_port *ptp_port;
559 unsigned long flags;
560 u32 tstamp_good = 0;
561 struct ice_pf *pf;
562 struct ice_hw *hw;
563 u64 tstamp_ready;
564 bool link_up;
565 int err;
566 u8 idx;
567
568 ptp_port = container_of(tx, struct ice_ptp_port, tx);
569 pf = ptp_port_to_pf(ptp_port);
570 hw = &pf->hw;
571
572 if (!tx->init)
573 return;
574
575 /* Read the Tx ready status first */
576 if (tx->has_ready_bitmap) {
577 err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
578 if (err)
579 return;
580 }
581
582 /* Drop packets if the link went down */
583 link_up = ptp_port->link_up;
584
585 for_each_set_bit(idx, tx->in_use, tx->len) {
586 struct skb_shared_hwtstamps shhwtstamps = {};
587 u8 phy_idx = idx + tx->offset;
588 u64 raw_tstamp = 0, tstamp;
589 bool drop_ts = !link_up;
590 struct sk_buff *skb;
591
592 /* Drop packets which have waited for more than 2 seconds */
593 if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
594 drop_ts = true;
595
596 /* Count the number of Tx timestamps that timed out */
597 pf->ptp.tx_hwtstamp_timeouts++;
598 }
599
600 /* Only read a timestamp from the PHY if its marked as ready
601 * by the tstamp_ready register. This avoids unnecessary
602 * reading of timestamps which are not yet valid. This is
603 * important as we must read all timestamps which are valid
604 * and only timestamps which are valid during each interrupt.
605 * If we do not, the hardware logic for generating a new
606 * interrupt can get stuck on some devices.
607 */
608 if (tx->has_ready_bitmap &&
609 !(tstamp_ready & BIT_ULL(phy_idx))) {
610 if (drop_ts)
611 goto skip_ts_read;
612
613 continue;
614 }
615
616 ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
617
618 err = ice_read_phy_tstamp(hw, tx->block, phy_idx, &raw_tstamp);
619 if (err && !drop_ts)
620 continue;
621
622 ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
623
624 /* For PHYs which don't implement a proper timestamp ready
625 * bitmap, verify that the timestamp value is different
626 * from the last cached timestamp. If it is not, skip this for
627 * now assuming it hasn't yet been captured by hardware.
628 */
629 if (!drop_ts && !tx->has_ready_bitmap &&
630 raw_tstamp == tx->tstamps[idx].cached_tstamp)
631 continue;
632
633 /* Discard any timestamp value without the valid bit set */
634 if (!(raw_tstamp & ICE_PTP_TS_VALID))
635 drop_ts = true;
636
637 skip_ts_read:
638 spin_lock_irqsave(&tx->lock, flags);
639 if (!tx->has_ready_bitmap && raw_tstamp)
640 tx->tstamps[idx].cached_tstamp = raw_tstamp;
641 clear_bit(idx, tx->in_use);
642 skb = tx->tstamps[idx].skb;
643 tx->tstamps[idx].skb = NULL;
644 if (test_and_clear_bit(idx, tx->stale))
645 drop_ts = true;
646 spin_unlock_irqrestore(&tx->lock, flags);
647
648 /* It is unlikely but possible that the SKB will have been
649 * flushed at this point due to link change or teardown.
650 */
651 if (!skb)
652 continue;
653
654 if (drop_ts) {
655 dev_kfree_skb_any(skb);
656 continue;
657 }
658
659 /* Extend the timestamp using cached PHC time */
660 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
661 if (tstamp) {
662 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
663 ice_trace(tx_tstamp_complete, skb, idx);
664
665 /* Count the number of Tx timestamps that succeeded */
666 tstamp_good++;
667 }
668
669 skb_tstamp_tx(skb, &shhwtstamps);
670 dev_kfree_skb_any(skb);
671 }
672
673 pf->ptp.tx_hwtstamp_good += tstamp_good;
674 }
675
ice_ptp_tx_tstamp_owner(struct ice_pf * pf)676 static void ice_ptp_tx_tstamp_owner(struct ice_pf *pf)
677 {
678 struct ice_ptp_port *port;
679
680 mutex_lock(&pf->adapter->ports.lock);
681 list_for_each_entry(port, &pf->adapter->ports.ports, list_node) {
682 struct ice_ptp_tx *tx = &port->tx;
683
684 if (!tx || !tx->init)
685 continue;
686
687 ice_ptp_process_tx_tstamp(tx);
688 }
689 mutex_unlock(&pf->adapter->ports.lock);
690 }
691
692 /**
693 * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
694 * @tx: Tx tracking structure to initialize
695 *
696 * Assumes that the length has already been initialized. Do not call directly,
697 * use the ice_ptp_init_tx_* instead.
698 */
699 static int
ice_ptp_alloc_tx_tracker(struct ice_ptp_tx * tx)700 ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
701 {
702 unsigned long *in_use, *stale;
703 struct ice_tx_tstamp *tstamps;
704
705 tstamps = kzalloc_objs(*tstamps, tx->len);
706 in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
707 stale = bitmap_zalloc(tx->len, GFP_KERNEL);
708
709 if (!tstamps || !in_use || !stale) {
710 kfree(tstamps);
711 bitmap_free(in_use);
712 bitmap_free(stale);
713
714 return -ENOMEM;
715 }
716
717 tx->tstamps = tstamps;
718 tx->in_use = in_use;
719 tx->stale = stale;
720 tx->init = 1;
721 tx->last_ll_ts_idx_read = -1;
722
723 spin_lock_init(&tx->lock);
724
725 return 0;
726 }
727
728 /**
729 * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
730 * @pf: Board private structure
731 * @tx: the tracker to flush
732 *
733 * Called during teardown when a Tx tracker is being removed.
734 */
735 static void
ice_ptp_flush_tx_tracker(struct ice_pf * pf,struct ice_ptp_tx * tx)736 ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
737 {
738 struct ice_hw *hw = &pf->hw;
739 unsigned long flags;
740 u64 tstamp_ready;
741 int err;
742 u8 idx;
743
744 err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
745 if (err) {
746 dev_dbg(ice_pf_to_dev(pf), "Failed to get the Tx tstamp ready bitmap for block %u, err %d\n",
747 tx->block, err);
748
749 /* If we fail to read the Tx timestamp ready bitmap just
750 * skip clearing the PHY timestamps.
751 */
752 tstamp_ready = 0;
753 }
754
755 for_each_set_bit(idx, tx->in_use, tx->len) {
756 u8 phy_idx = idx + tx->offset;
757 struct sk_buff *skb;
758
759 /* In case this timestamp is ready, we need to clear it. */
760 if (!hw->reset_ongoing && (tstamp_ready & BIT_ULL(phy_idx)))
761 ice_clear_phy_tstamp(hw, tx->block, phy_idx);
762
763 spin_lock_irqsave(&tx->lock, flags);
764 skb = tx->tstamps[idx].skb;
765 tx->tstamps[idx].skb = NULL;
766 clear_bit(idx, tx->in_use);
767 clear_bit(idx, tx->stale);
768 spin_unlock_irqrestore(&tx->lock, flags);
769
770 /* Count the number of Tx timestamps flushed */
771 pf->ptp.tx_hwtstamp_flushed++;
772
773 /* Free the SKB after we've cleared the bit */
774 dev_kfree_skb_any(skb);
775 }
776 }
777
778 /**
779 * ice_ptp_mark_tx_tracker_stale - Mark unfinished timestamps as stale
780 * @tx: the tracker to mark
781 *
782 * Mark currently outstanding Tx timestamps as stale. This prevents sending
783 * their timestamp value to the stack. This is required to prevent extending
784 * the 40bit hardware timestamp incorrectly.
785 *
786 * This should be called when the PTP clock is modified such as after a set
787 * time request.
788 */
789 static void
ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx * tx)790 ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx *tx)
791 {
792 unsigned long flags;
793
794 spin_lock_irqsave(&tx->lock, flags);
795 bitmap_or(tx->stale, tx->stale, tx->in_use, tx->len);
796 spin_unlock_irqrestore(&tx->lock, flags);
797 }
798
799 /**
800 * ice_ptp_flush_all_tx_tracker - Flush all timestamp trackers on this clock
801 * @pf: Board private structure
802 *
803 * Called by the clock owner to flush all the Tx timestamp trackers associated
804 * with the clock.
805 */
806 static void
ice_ptp_flush_all_tx_tracker(struct ice_pf * pf)807 ice_ptp_flush_all_tx_tracker(struct ice_pf *pf)
808 {
809 struct ice_ptp_port *port;
810
811 list_for_each_entry(port, &pf->adapter->ports.ports, list_node)
812 ice_ptp_flush_tx_tracker(ptp_port_to_pf(port), &port->tx);
813 }
814
815 /**
816 * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
817 * @pf: Board private structure
818 * @tx: Tx tracking structure to release
819 *
820 * Free memory associated with the Tx timestamp tracker.
821 */
822 static void
ice_ptp_release_tx_tracker(struct ice_pf * pf,struct ice_ptp_tx * tx)823 ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
824 {
825 unsigned long flags;
826
827 spin_lock_irqsave(&tx->lock, flags);
828 tx->init = 0;
829 spin_unlock_irqrestore(&tx->lock, flags);
830
831 /* wait for potentially outstanding interrupt to complete */
832 synchronize_irq(pf->oicr_irq.virq);
833
834 ice_ptp_flush_tx_tracker(pf, tx);
835
836 kfree(tx->tstamps);
837 tx->tstamps = NULL;
838
839 bitmap_free(tx->in_use);
840 tx->in_use = NULL;
841
842 bitmap_free(tx->stale);
843 tx->stale = NULL;
844
845 tx->len = 0;
846 }
847
848 /**
849 * ice_ptp_init_tx_e82x - Initialize tracking for Tx timestamps
850 * @pf: Board private structure
851 * @tx: the Tx tracking structure to initialize
852 * @port: the port this structure tracks
853 *
854 * Initialize the Tx timestamp tracker for this port. For generic MAC devices,
855 * the timestamp block is shared for all ports in the same quad. To avoid
856 * ports using the same timestamp index, logically break the block of
857 * registers into chunks based on the port number.
858 *
859 * Return: 0 on success, -ENOMEM when out of memory
860 */
ice_ptp_init_tx_e82x(struct ice_pf * pf,struct ice_ptp_tx * tx,u8 port)861 static int ice_ptp_init_tx_e82x(struct ice_pf *pf, struct ice_ptp_tx *tx,
862 u8 port)
863 {
864 tx->block = ICE_GET_QUAD_NUM(port);
865 tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E82X;
866 tx->len = INDEX_PER_PORT_E82X;
867 tx->has_ready_bitmap = 1;
868
869 return ice_ptp_alloc_tx_tracker(tx);
870 }
871
872 /**
873 * ice_ptp_init_tx - Initialize tracking for Tx timestamps
874 * @pf: Board private structure
875 * @tx: the Tx tracking structure to initialize
876 * @port: the port this structure tracks
877 *
878 * Initialize the Tx timestamp tracker for this PF. For all PHYs except E82X,
879 * each port has its own block of timestamps, independent of the other ports.
880 *
881 * Return: 0 on success, -ENOMEM when out of memory
882 */
ice_ptp_init_tx(struct ice_pf * pf,struct ice_ptp_tx * tx,u8 port)883 static int ice_ptp_init_tx(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port)
884 {
885 tx->block = port;
886 tx->offset = 0;
887 tx->len = INDEX_PER_PORT;
888
889 /* The E810 PHY does not provide a timestamp ready bitmap. Instead,
890 * verify new timestamps against cached copy of the last read
891 * timestamp.
892 */
893 tx->has_ready_bitmap = pf->hw.mac_type != ICE_MAC_E810;
894
895 return ice_ptp_alloc_tx_tracker(tx);
896 }
897
898 /**
899 * ice_ptp_update_cached_phctime - Update the cached PHC time values
900 * @pf: Board specific private structure
901 *
902 * This function updates the system time values which are cached in the PF
903 * structure and the Rx rings.
904 *
905 * This function must be called periodically to ensure that the cached value
906 * is never more than 2 seconds old.
907 *
908 * Note that the cached copy in the PF PTP structure is always updated, even
909 * if we can't update the copy in the Rx rings.
910 *
911 * Return:
912 * * 0 - OK, successfully updated
913 * * -EAGAIN - PF was busy, need to reschedule the update
914 */
ice_ptp_update_cached_phctime(struct ice_pf * pf)915 static int ice_ptp_update_cached_phctime(struct ice_pf *pf)
916 {
917 struct device *dev = ice_pf_to_dev(pf);
918 unsigned long update_before;
919 u64 systime;
920 int i;
921
922 update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
923 if (pf->ptp.cached_phc_time &&
924 time_is_before_jiffies(update_before)) {
925 unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies;
926
927 dev_warn(dev, "%u msecs passed between update to cached PHC time\n",
928 jiffies_to_msecs(time_taken));
929 pf->ptp.late_cached_phc_updates++;
930 }
931
932 /* Read the current PHC time */
933 systime = ice_ptp_read_src_clk_reg(pf, NULL);
934
935 /* Update the cached PHC time stored in the PF structure */
936 WRITE_ONCE(pf->ptp.cached_phc_time, systime);
937 WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies);
938
939 if (test_and_set_bit(ICE_CFG_BUSY, pf->state))
940 return -EAGAIN;
941
942 ice_for_each_vsi(pf, i) {
943 struct ice_vsi *vsi = pf->vsi[i];
944 int j;
945
946 if (!vsi)
947 continue;
948
949 if (vsi->type != ICE_VSI_PF)
950 continue;
951
952 ice_for_each_rxq(vsi, j) {
953 if (!vsi->rx_rings[j])
954 continue;
955 WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
956 }
957 }
958 clear_bit(ICE_CFG_BUSY, pf->state);
959
960 return 0;
961 }
962
963 /**
964 * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update
965 * @pf: Board specific private structure
966 *
967 * This function must be called when the cached PHC time is no longer valid,
968 * such as after a time adjustment. It marks any currently outstanding Tx
969 * timestamps as stale and updates the cached PHC time for both the PF and Rx
970 * rings.
971 *
972 * If updating the PHC time cannot be done immediately, a warning message is
973 * logged and the work item is scheduled immediately to minimize the window
974 * with a wrong cached timestamp.
975 */
ice_ptp_reset_cached_phctime(struct ice_pf * pf)976 static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)
977 {
978 struct device *dev = ice_pf_to_dev(pf);
979 int err;
980
981 /* Update the cached PHC time immediately if possible, otherwise
982 * schedule the work item to execute soon.
983 */
984 err = ice_ptp_update_cached_phctime(pf);
985 if (err) {
986 /* If another thread is updating the Rx rings, we won't
987 * properly reset them here. This could lead to reporting of
988 * invalid timestamps, but there isn't much we can do.
989 */
990 dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n",
991 __func__);
992
993 /* Queue the work item to update the Rx rings when possible */
994 kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work,
995 msecs_to_jiffies(10));
996 }
997
998 /* Mark any outstanding timestamps as stale, since they might have
999 * been captured in hardware before the time update. This could lead
1000 * to us extending them with the wrong cached value resulting in
1001 * incorrect timestamp values.
1002 */
1003 ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx);
1004 }
1005
1006 /**
1007 * ice_ptp_write_init - Set PHC time to provided value
1008 * @pf: Board private structure
1009 * @ts: timespec structure that holds the new time value
1010 *
1011 * Set the PHC time to the specified time provided in the timespec.
1012 */
ice_ptp_write_init(struct ice_pf * pf,struct timespec64 * ts)1013 static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
1014 {
1015 u64 ns = timespec64_to_ns(ts);
1016 struct ice_hw *hw = &pf->hw;
1017
1018 return ice_ptp_init_time(hw, ns);
1019 }
1020
1021 /**
1022 * ice_ptp_write_adj - Adjust PHC clock time atomically
1023 * @pf: Board private structure
1024 * @adj: Adjustment in nanoseconds
1025 *
1026 * Perform an atomic adjustment of the PHC time by the specified number of
1027 * nanoseconds.
1028 */
ice_ptp_write_adj(struct ice_pf * pf,s32 adj)1029 static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
1030 {
1031 struct ice_hw *hw = &pf->hw;
1032
1033 return ice_ptp_adj_clock(hw, adj);
1034 }
1035
1036 /**
1037 * ice_base_incval - Get base timer increment value
1038 * @pf: Board private structure
1039 *
1040 * Look up the base timer increment value for this device. The base increment
1041 * value is used to define the nominal clock tick rate. This increment value
1042 * is programmed during device initialization. It is also used as the basis
1043 * for calculating adjustments using scaled_ppm.
1044 */
ice_base_incval(struct ice_pf * pf)1045 static u64 ice_base_incval(struct ice_pf *pf)
1046 {
1047 struct ice_hw *hw = &pf->hw;
1048 u64 incval;
1049
1050 incval = ice_get_base_incval(hw);
1051
1052 dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n",
1053 incval);
1054
1055 return incval;
1056 }
1057
1058 /**
1059 * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state
1060 * @port: PTP port for which Tx FIFO is checked
1061 */
ice_ptp_check_tx_fifo(struct ice_ptp_port * port)1062 static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port)
1063 {
1064 int offs = port->port_num % ICE_PORTS_PER_QUAD;
1065 int quad = ICE_GET_QUAD_NUM(port->port_num);
1066 struct ice_pf *pf;
1067 struct ice_hw *hw;
1068 u32 val, phy_sts;
1069 int err;
1070
1071 pf = ptp_port_to_pf(port);
1072 hw = &pf->hw;
1073
1074 if (port->tx_fifo_busy_cnt == FIFO_OK)
1075 return 0;
1076
1077 /* need to read FIFO state */
1078 if (offs == 0 || offs == 1)
1079 err = ice_read_quad_reg_e82x(hw, quad, Q_REG_FIFO01_STATUS,
1080 &val);
1081 else
1082 err = ice_read_quad_reg_e82x(hw, quad, Q_REG_FIFO23_STATUS,
1083 &val);
1084
1085 if (err) {
1086 dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n",
1087 port->port_num, err);
1088 return err;
1089 }
1090
1091 if (offs & 0x1)
1092 phy_sts = FIELD_GET(Q_REG_FIFO13_M, val);
1093 else
1094 phy_sts = FIELD_GET(Q_REG_FIFO02_M, val);
1095
1096 if (phy_sts & FIFO_EMPTY) {
1097 port->tx_fifo_busy_cnt = FIFO_OK;
1098 return 0;
1099 }
1100
1101 port->tx_fifo_busy_cnt++;
1102
1103 dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n",
1104 port->tx_fifo_busy_cnt, port->port_num);
1105
1106 if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) {
1107 dev_dbg(ice_pf_to_dev(pf),
1108 "Port %d Tx FIFO still not empty; resetting quad %d\n",
1109 port->port_num, quad);
1110 ice_ptp_reset_ts_memory_quad_e82x(hw, quad);
1111 port->tx_fifo_busy_cnt = FIFO_OK;
1112 return 0;
1113 }
1114
1115 return -EAGAIN;
1116 }
1117
1118 /**
1119 * ice_ptp_wait_for_offsets - Check for valid Tx and Rx offsets
1120 * @work: Pointer to the kthread_work structure for this task
1121 *
1122 * Check whether hardware has completed measuring the Tx and Rx offset values
1123 * used to configure and enable vernier timestamp calibration.
1124 *
1125 * Once the offset in either direction is measured, configure the associated
1126 * registers with the calibrated offset values and enable timestamping. The Tx
1127 * and Rx directions are configured independently as soon as their associated
1128 * offsets are known.
1129 *
1130 * This function reschedules itself until both Tx and Rx calibration have
1131 * completed.
1132 */
ice_ptp_wait_for_offsets(struct kthread_work * work)1133 static void ice_ptp_wait_for_offsets(struct kthread_work *work)
1134 {
1135 struct ice_ptp_port *port;
1136 struct ice_pf *pf;
1137 struct ice_hw *hw;
1138 int tx_err;
1139 int rx_err;
1140
1141 port = container_of(work, struct ice_ptp_port, ov_work.work);
1142 pf = ptp_port_to_pf(port);
1143 hw = &pf->hw;
1144
1145 if (ice_is_reset_in_progress(pf->state)) {
1146 /* wait for device driver to complete reset */
1147 kthread_queue_delayed_work(pf->ptp.kworker,
1148 &port->ov_work,
1149 msecs_to_jiffies(100));
1150 return;
1151 }
1152
1153 tx_err = ice_ptp_check_tx_fifo(port);
1154 if (!tx_err)
1155 tx_err = ice_phy_cfg_tx_offset_e82x(hw, port->port_num);
1156 rx_err = ice_phy_cfg_rx_offset_e82x(hw, port->port_num);
1157 if (tx_err || rx_err) {
1158 /* Tx and/or Rx offset not yet configured, try again later */
1159 kthread_queue_delayed_work(pf->ptp.kworker,
1160 &port->ov_work,
1161 msecs_to_jiffies(100));
1162 return;
1163 }
1164 }
1165
1166 /**
1167 * ice_ptp_port_phy_stop - Stop timestamping for a PHY port
1168 * @ptp_port: PTP port to stop
1169 */
1170 static int
ice_ptp_port_phy_stop(struct ice_ptp_port * ptp_port)1171 ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port)
1172 {
1173 struct ice_pf *pf = ptp_port_to_pf(ptp_port);
1174 u8 port = ptp_port->port_num;
1175 struct ice_hw *hw = &pf->hw;
1176 int err;
1177
1178 mutex_lock(&ptp_port->ps_lock);
1179
1180 switch (hw->mac_type) {
1181 case ICE_MAC_E810:
1182 case ICE_MAC_E830:
1183 err = 0;
1184 break;
1185 case ICE_MAC_GENERIC:
1186 kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1187
1188 err = ice_stop_phy_timer_e82x(hw, port, true);
1189 break;
1190 case ICE_MAC_GENERIC_3K_E825:
1191 err = ice_stop_phy_timer_eth56g(hw, port, true);
1192 break;
1193 default:
1194 err = -ENODEV;
1195 }
1196 if (err && err != -EBUSY)
1197 dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n",
1198 port, err);
1199
1200 mutex_unlock(&ptp_port->ps_lock);
1201
1202 return err;
1203 }
1204
1205 /**
1206 * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping
1207 * @ptp_port: PTP port for which the PHY start is set
1208 *
1209 * Start the PHY timestamping block, and initiate Vernier timestamping
1210 * calibration. If timestamping cannot be calibrated (such as if link is down)
1211 * then disable the timestamping block instead.
1212 */
1213 static int
ice_ptp_port_phy_restart(struct ice_ptp_port * ptp_port)1214 ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port)
1215 {
1216 struct ice_pf *pf = ptp_port_to_pf(ptp_port);
1217 u8 port = ptp_port->port_num;
1218 struct ice_hw *hw = &pf->hw;
1219 unsigned long flags;
1220 int err;
1221
1222 if (!ptp_port->link_up)
1223 return ice_ptp_port_phy_stop(ptp_port);
1224
1225 mutex_lock(&ptp_port->ps_lock);
1226
1227 switch (hw->mac_type) {
1228 case ICE_MAC_E810:
1229 case ICE_MAC_E830:
1230 err = 0;
1231 break;
1232 case ICE_MAC_GENERIC:
1233 /* Start the PHY timer in Vernier mode */
1234 kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1235
1236 /* temporarily disable Tx timestamps while calibrating
1237 * PHY offset
1238 */
1239 spin_lock_irqsave(&ptp_port->tx.lock, flags);
1240 ptp_port->tx.calibrating = true;
1241 spin_unlock_irqrestore(&ptp_port->tx.lock, flags);
1242 ptp_port->tx_fifo_busy_cnt = 0;
1243
1244 /* Start the PHY timer in Vernier mode */
1245 err = ice_start_phy_timer_e82x(hw, port);
1246 if (err)
1247 break;
1248
1249 /* Enable Tx timestamps right away */
1250 spin_lock_irqsave(&ptp_port->tx.lock, flags);
1251 ptp_port->tx.calibrating = false;
1252 spin_unlock_irqrestore(&ptp_port->tx.lock, flags);
1253
1254 kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work,
1255 0);
1256 break;
1257 case ICE_MAC_GENERIC_3K_E825:
1258 err = ice_start_phy_timer_eth56g(hw, port);
1259 break;
1260 default:
1261 err = -ENODEV;
1262 }
1263
1264 if (err)
1265 dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n",
1266 port, err);
1267
1268 mutex_unlock(&ptp_port->ps_lock);
1269
1270 return err;
1271 }
1272
1273 /**
1274 * ice_ptp_link_change - Reconfigure PTP after link status change
1275 * @pf: Board private structure
1276 * @linkup: Link is up or down
1277 */
ice_ptp_link_change(struct ice_pf * pf,bool linkup)1278 void ice_ptp_link_change(struct ice_pf *pf, bool linkup)
1279 {
1280 struct ice_ptp_port *ptp_port;
1281 struct ice_hw *hw = &pf->hw;
1282
1283 if (pf->ptp.state != ICE_PTP_READY)
1284 return;
1285
1286 ptp_port = &pf->ptp.port;
1287
1288 /* Update cached link status for this port immediately */
1289 ptp_port->link_up = linkup;
1290
1291 /* Skip HW writes if reset is in progress */
1292 if (pf->hw.reset_ongoing)
1293 return;
1294
1295 if (hw->mac_type == ICE_MAC_GENERIC_3K_E825 &&
1296 test_bit(ICE_FLAG_DPLL, pf->flags)) {
1297 int pin, err;
1298
1299 mutex_lock(&pf->dplls.lock);
1300 for (pin = 0; pin < ICE_SYNCE_CLK_NUM; pin++) {
1301 enum ice_synce_clk clk_pin;
1302 bool active;
1303 u8 port_num;
1304
1305 port_num = ptp_port->port_num;
1306 clk_pin = (enum ice_synce_clk)pin;
1307 err = ice_tspll_bypass_mux_active_e825c(hw,
1308 port_num,
1309 &active,
1310 clk_pin);
1311 if (err) {
1312 dev_err_once(ice_pf_to_dev(pf),
1313 "Failed to read SyncE bypass mux for pin %d, err %d\n",
1314 pin, err);
1315 break;
1316 }
1317
1318 err = ice_tspll_cfg_synce_ethdiv_e825c(hw, clk_pin);
1319 if (active && err) {
1320 dev_err_once(ice_pf_to_dev(pf),
1321 "Failed to configure SyncE ETH divider for pin %d, err %d\n",
1322 pin, err);
1323 break;
1324 }
1325 }
1326 mutex_unlock(&pf->dplls.lock);
1327
1328 if (linkup)
1329 ice_txclk_update_and_notify(pf);
1330 }
1331
1332 switch (hw->mac_type) {
1333 case ICE_MAC_E810:
1334 case ICE_MAC_E830:
1335 /* Do not reconfigure E810 or E830 PHY */
1336 return;
1337 case ICE_MAC_GENERIC:
1338 ice_ptp_port_phy_restart(ptp_port);
1339 return;
1340 case ICE_MAC_GENERIC_3K_E825:
1341 if (linkup)
1342 ice_ptp_port_phy_restart(ptp_port);
1343 return;
1344 default:
1345 dev_warn(ice_pf_to_dev(pf), "%s: Unknown PHY type\n", __func__);
1346 }
1347 }
1348
1349 /**
1350 * ice_ptp_cfg_phy_interrupt - Configure PHY interrupt settings
1351 * @pf: PF private structure
1352 * @ena: bool value to enable or disable interrupt
1353 * @threshold: Minimum number of packets at which intr is triggered
1354 *
1355 * Utility function to configure all the PHY interrupt settings, including
1356 * whether the PHY interrupt is enabled, and what threshold to use. Also
1357 * configures The E82X timestamp owner to react to interrupts from all PHYs.
1358 *
1359 * Return: 0 on success, -EOPNOTSUPP when PHY model incorrect, other error codes
1360 * when failed to configure PHY interrupt for E82X
1361 */
ice_ptp_cfg_phy_interrupt(struct ice_pf * pf,bool ena,u32 threshold)1362 static int ice_ptp_cfg_phy_interrupt(struct ice_pf *pf, bool ena, u32 threshold)
1363 {
1364 struct device *dev = ice_pf_to_dev(pf);
1365 struct ice_hw *hw = &pf->hw;
1366
1367 ice_ptp_reset_ts_memory(hw);
1368
1369 switch (hw->mac_type) {
1370 case ICE_MAC_E810:
1371 case ICE_MAC_E830:
1372 return 0;
1373 case ICE_MAC_GENERIC: {
1374 int quad;
1375
1376 for (quad = 0; quad < ICE_GET_QUAD_NUM(hw->ptp.num_lports);
1377 quad++) {
1378 int err;
1379
1380 err = ice_phy_cfg_intr_e82x(hw, quad, ena, threshold);
1381 if (err) {
1382 dev_err(dev, "Failed to configure PHY interrupt for quad %d, err %d\n",
1383 quad, err);
1384 return err;
1385 }
1386 }
1387
1388 return 0;
1389 }
1390 case ICE_MAC_GENERIC_3K_E825: {
1391 int port;
1392
1393 for (port = 0; port < hw->ptp.num_lports; port++) {
1394 int err;
1395
1396 err = ice_phy_cfg_intr_eth56g(hw, port, ena, threshold);
1397 if (err) {
1398 dev_err(dev, "Failed to configure PHY interrupt for port %d, err %d\n",
1399 port, err);
1400 return err;
1401 }
1402 }
1403
1404 return 0;
1405 }
1406 case ICE_MAC_UNKNOWN:
1407 default:
1408 return -EOPNOTSUPP;
1409 }
1410 }
1411
1412 /**
1413 * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block
1414 * @pf: Board private structure
1415 */
ice_ptp_reset_phy_timestamping(struct ice_pf * pf)1416 static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf)
1417 {
1418 ice_ptp_port_phy_restart(&pf->ptp.port);
1419 }
1420
1421 /**
1422 * ice_ptp_restart_all_phy - Restart all PHYs to recalibrate timestamping
1423 * @pf: Board private structure
1424 */
ice_ptp_restart_all_phy(struct ice_pf * pf)1425 static void ice_ptp_restart_all_phy(struct ice_pf *pf)
1426 {
1427 struct list_head *entry;
1428
1429 list_for_each(entry, &pf->adapter->ports.ports) {
1430 struct ice_ptp_port *port = list_entry(entry,
1431 struct ice_ptp_port,
1432 list_node);
1433
1434 if (port->link_up)
1435 ice_ptp_port_phy_restart(port);
1436 }
1437 }
1438
1439 /**
1440 * ice_ptp_adjfine - Adjust clock increment rate
1441 * @info: the driver's PTP info structure
1442 * @scaled_ppm: Parts per million with 16-bit fractional field
1443 *
1444 * Adjust the frequency of the clock by the indicated scaled ppm from the
1445 * base frequency.
1446 */
ice_ptp_adjfine(struct ptp_clock_info * info,long scaled_ppm)1447 static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
1448 {
1449 struct ice_pf *pf = ptp_info_to_pf(info);
1450 struct ice_hw *hw = &pf->hw;
1451 u64 incval;
1452 int err;
1453
1454 incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
1455 err = ice_ptp_write_incval_locked(hw, incval);
1456 if (err) {
1457 dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
1458 err);
1459 return -EIO;
1460 }
1461
1462 return 0;
1463 }
1464
1465 /**
1466 * ice_ptp_extts_event - Process PTP external clock event
1467 * @pf: Board private structure
1468 */
ice_ptp_extts_event(struct ice_pf * pf)1469 void ice_ptp_extts_event(struct ice_pf *pf)
1470 {
1471 struct ptp_clock_event event;
1472 struct ice_hw *hw = &pf->hw;
1473 u8 chan, tmr_idx;
1474 u32 hi, lo;
1475
1476 /* Don't process timestamp events if PTP is not ready */
1477 if (pf->ptp.state != ICE_PTP_READY)
1478 return;
1479
1480 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1481 /* Event time is captured by one of the two matched registers
1482 * GLTSYN_EVNT_L: 32 LSB of sampled time event
1483 * GLTSYN_EVNT_H: 32 MSB of sampled time event
1484 * Event is defined in GLTSYN_EVNT_0 register
1485 */
1486 for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
1487 int pin_desc_idx;
1488
1489 /* Check if channel is enabled */
1490 if (!(pf->ptp.ext_ts_irq & (1 << chan)))
1491 continue;
1492
1493 lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
1494 hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
1495 event.timestamp = (u64)hi << 32 | lo;
1496
1497 /* Add delay compensation */
1498 pin_desc_idx = ice_ptp_find_pin_idx(pf, PTP_PF_EXTTS, chan);
1499 if (pin_desc_idx >= 0) {
1500 const struct ice_ptp_pin_desc *desc;
1501
1502 desc = &pf->ptp.ice_pin_desc[pin_desc_idx];
1503 event.timestamp -= desc->delay[0];
1504 }
1505
1506 event.type = PTP_CLOCK_EXTTS;
1507 event.index = chan;
1508 pf->ptp.ext_ts_irq &= ~(1 << chan);
1509 ptp_clock_event(pf->ptp.clock, &event);
1510 }
1511 }
1512
1513 /**
1514 * ice_ptp_cfg_extts - Configure EXTTS pin and channel
1515 * @pf: Board private structure
1516 * @rq: External timestamp request
1517 * @on: Enable/disable flag
1518 *
1519 * Configure an external timestamp event on the requested channel.
1520 *
1521 * Return: 0 on success, negative error code otherwise
1522 */
ice_ptp_cfg_extts(struct ice_pf * pf,struct ptp_extts_request * rq,int on)1523 static int ice_ptp_cfg_extts(struct ice_pf *pf, struct ptp_extts_request *rq,
1524 int on)
1525 {
1526 u32 aux_reg, gpio_reg, irq_reg;
1527 struct ice_hw *hw = &pf->hw;
1528 unsigned int chan, gpio_pin;
1529 int pin_desc_idx;
1530 u8 tmr_idx;
1531
1532 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1533 chan = rq->index;
1534
1535 pin_desc_idx = ice_ptp_find_pin_idx(pf, PTP_PF_EXTTS, chan);
1536 if (pin_desc_idx < 0)
1537 return -EIO;
1538
1539 gpio_pin = pf->ptp.ice_pin_desc[pin_desc_idx].gpio[0];
1540 irq_reg = rd32(hw, PFINT_OICR_ENA);
1541
1542 if (on) {
1543 /* Enable the interrupt */
1544 irq_reg |= PFINT_OICR_TSYN_EVNT_M;
1545 aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
1546
1547 #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0)
1548 #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1)
1549
1550 /* set event level to requested edge */
1551 if (rq->flags & PTP_FALLING_EDGE)
1552 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
1553 if (rq->flags & PTP_RISING_EDGE)
1554 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
1555
1556 /* Write GPIO CTL reg.
1557 * 0x1 is input sampled by EVENT register(channel)
1558 * + num_in_channels * tmr_idx
1559 */
1560 gpio_reg = FIELD_PREP(GLGEN_GPIO_CTL_PIN_FUNC_M,
1561 1 + chan + (tmr_idx * 3));
1562 } else {
1563 bool last_enabled = true;
1564
1565 /* clear the values we set to reset defaults */
1566 aux_reg = 0;
1567 gpio_reg = 0;
1568
1569 for (unsigned int i = 0; i < pf->ptp.info.n_ext_ts; i++)
1570 if ((pf->ptp.extts_rqs[i].flags &
1571 PTP_ENABLE_FEATURE) &&
1572 i != chan) {
1573 last_enabled = false;
1574 }
1575
1576 if (last_enabled)
1577 irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
1578 }
1579
1580 wr32(hw, PFINT_OICR_ENA, irq_reg);
1581 wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
1582 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg);
1583
1584 return 0;
1585 }
1586
1587 /**
1588 * ice_ptp_disable_all_extts - Disable all EXTTS channels
1589 * @pf: Board private structure
1590 */
ice_ptp_disable_all_extts(struct ice_pf * pf)1591 static void ice_ptp_disable_all_extts(struct ice_pf *pf)
1592 {
1593 for (unsigned int i = 0; i < pf->ptp.info.n_ext_ts ; i++)
1594 if (pf->ptp.extts_rqs[i].flags & PTP_ENABLE_FEATURE)
1595 ice_ptp_cfg_extts(pf, &pf->ptp.extts_rqs[i],
1596 false);
1597
1598 synchronize_irq(pf->oicr_irq.virq);
1599 }
1600
1601 /**
1602 * ice_ptp_enable_all_extts - Enable all EXTTS channels
1603 * @pf: Board private structure
1604 *
1605 * Called during reset to restore user configuration.
1606 */
ice_ptp_enable_all_extts(struct ice_pf * pf)1607 static void ice_ptp_enable_all_extts(struct ice_pf *pf)
1608 {
1609 for (unsigned int i = 0; i < pf->ptp.info.n_ext_ts ; i++)
1610 if (pf->ptp.extts_rqs[i].flags & PTP_ENABLE_FEATURE)
1611 ice_ptp_cfg_extts(pf, &pf->ptp.extts_rqs[i],
1612 true);
1613 }
1614
1615 /**
1616 * ice_ptp_write_perout - Write periodic wave parameters to HW
1617 * @hw: pointer to the HW struct
1618 * @chan: target channel
1619 * @gpio_pin: target GPIO pin
1620 * @start: target time to start periodic output
1621 * @period: target period
1622 *
1623 * Return: 0 on success, negative error code otherwise
1624 */
ice_ptp_write_perout(struct ice_hw * hw,unsigned int chan,unsigned int gpio_pin,u64 start,u64 period)1625 static int ice_ptp_write_perout(struct ice_hw *hw, unsigned int chan,
1626 unsigned int gpio_pin, u64 start, u64 period)
1627 {
1628
1629 u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1630 u32 val = 0;
1631
1632 /* 0. Reset mode & out_en in AUX_OUT */
1633 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
1634
1635 if (hw->mac_type == ICE_MAC_GENERIC_3K_E825) {
1636 int err;
1637
1638 /* Enable/disable CGU 1PPS output for E825C */
1639 err = ice_tspll_cfg_pps_out_e825c(hw, !!period);
1640 if (err)
1641 return err;
1642 }
1643
1644 /* 1. Write perout with half of required period value.
1645 * HW toggles output when source clock hits the TGT and then adds
1646 * GLTSYN_CLKO value to the target, so it ends up with 50% duty cycle.
1647 */
1648 period >>= 1;
1649
1650 /* For proper operation, GLTSYN_CLKO must be larger than clock tick and
1651 * period has to fit in 32 bit register.
1652 */
1653 #define MIN_PULSE 3
1654 if (!!period && (period <= MIN_PULSE || period > U32_MAX)) {
1655 dev_err(ice_hw_to_dev(hw), "CLK period ticks must be >= %d && <= 2^32",
1656 MIN_PULSE);
1657 return -EIO;
1658 }
1659
1660 wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
1661
1662 /* 2. Write TARGET time */
1663 wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start));
1664 wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start));
1665
1666 /* 3. Write AUX_OUT register */
1667 if (!!period)
1668 val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
1669 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
1670
1671 /* 4. write GPIO CTL reg */
1672 val = GLGEN_GPIO_CTL_PIN_DIR_M;
1673 if (!!period)
1674 val |= FIELD_PREP(GLGEN_GPIO_CTL_PIN_FUNC_M,
1675 8 + chan + (tmr_idx * 4));
1676
1677 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1678 ice_flush(hw);
1679
1680 return 0;
1681 }
1682
1683 /**
1684 * ice_ptp_cfg_perout - Configure clock to generate periodic wave
1685 * @pf: Board private structure
1686 * @rq: Periodic output request
1687 * @on: Enable/disable flag
1688 *
1689 * Configure the internal clock generator modules to generate the clock wave of
1690 * specified period.
1691 *
1692 * Return: 0 on success, negative error code otherwise
1693 */
ice_ptp_cfg_perout(struct ice_pf * pf,struct ptp_perout_request * rq,int on)1694 static int ice_ptp_cfg_perout(struct ice_pf *pf, struct ptp_perout_request *rq,
1695 int on)
1696 {
1697 unsigned int gpio_pin, prop_delay_ns;
1698 u64 clk, period, start, phase;
1699 struct ice_hw *hw = &pf->hw;
1700 int pin_desc_idx;
1701
1702 pin_desc_idx = ice_ptp_find_pin_idx(pf, PTP_PF_PEROUT, rq->index);
1703 if (pin_desc_idx < 0)
1704 return -EIO;
1705
1706 gpio_pin = pf->ptp.ice_pin_desc[pin_desc_idx].gpio[1];
1707 prop_delay_ns = pf->ptp.ice_pin_desc[pin_desc_idx].delay[1];
1708 period = rq->period.sec * NSEC_PER_SEC + rq->period.nsec;
1709
1710 /* If we're disabling the output or period is 0, clear out CLKO and TGT
1711 * and keep output level low.
1712 */
1713 if (!on || !period)
1714 return ice_ptp_write_perout(hw, rq->index, gpio_pin, 0, 0);
1715
1716 if (strncmp(pf->ptp.pin_desc[pin_desc_idx].name, "1PPS", 64) == 0 &&
1717 period != NSEC_PER_SEC && hw->mac_type == ICE_MAC_GENERIC) {
1718 dev_err(ice_pf_to_dev(pf), "1PPS pin supports only 1 s period\n");
1719 return -EOPNOTSUPP;
1720 }
1721
1722 if (period & 0x1) {
1723 dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
1724 return -EIO;
1725 }
1726
1727 start = rq->start.sec * NSEC_PER_SEC + rq->start.nsec;
1728
1729 /* If PTP_PEROUT_PHASE is set, rq has phase instead of start time */
1730 if (rq->flags & PTP_PEROUT_PHASE)
1731 phase = start;
1732 else
1733 div64_u64_rem(start, period, &phase);
1734
1735 /* If we have only phase or start time is in the past, start the timer
1736 * at the next multiple of period, maintaining phase at least 0.5 second
1737 * from now, so we have time to write it to HW.
1738 */
1739 clk = ice_ptp_read_src_clk_reg(pf, NULL) + NSEC_PER_MSEC * 500;
1740 if (rq->flags & PTP_PEROUT_PHASE || start <= clk - prop_delay_ns)
1741 start = div64_u64(clk + period - 1, period) * period + phase;
1742
1743 /* Compensate for propagation delay from the generator to the pin. */
1744 start -= prop_delay_ns;
1745
1746 return ice_ptp_write_perout(hw, rq->index, gpio_pin, start, period);
1747 }
1748
1749 /**
1750 * ice_ptp_disable_all_perout - Disable all currently configured outputs
1751 * @pf: Board private structure
1752 *
1753 * Disable all currently configured clock outputs. This is necessary before
1754 * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_perout to
1755 * re-enable the clocks again.
1756 */
ice_ptp_disable_all_perout(struct ice_pf * pf)1757 static void ice_ptp_disable_all_perout(struct ice_pf *pf)
1758 {
1759 for (unsigned int i = 0; i < pf->ptp.info.n_per_out; i++)
1760 if (pf->ptp.perout_rqs[i].period.sec ||
1761 pf->ptp.perout_rqs[i].period.nsec)
1762 ice_ptp_cfg_perout(pf, &pf->ptp.perout_rqs[i],
1763 false);
1764 }
1765
1766 /**
1767 * ice_ptp_enable_all_perout - Enable all configured periodic clock outputs
1768 * @pf: Board private structure
1769 *
1770 * Enable all currently configured clock outputs. Use this after
1771 * ice_ptp_disable_all_perout to reconfigure the output signals according to
1772 * their configuration.
1773 */
ice_ptp_enable_all_perout(struct ice_pf * pf)1774 static void ice_ptp_enable_all_perout(struct ice_pf *pf)
1775 {
1776 for (unsigned int i = 0; i < pf->ptp.info.n_per_out; i++)
1777 if (pf->ptp.perout_rqs[i].period.sec ||
1778 pf->ptp.perout_rqs[i].period.nsec)
1779 ice_ptp_cfg_perout(pf, &pf->ptp.perout_rqs[i],
1780 true);
1781 }
1782
1783 /**
1784 * ice_verify_pin - verify if pin supports requested pin function
1785 * @info: the driver's PTP info structure
1786 * @pin: Pin index
1787 * @func: Assigned function
1788 * @chan: Assigned channel
1789 *
1790 * Return: 0 on success, -EOPNOTSUPP when function is not supported.
1791 */
ice_verify_pin(struct ptp_clock_info * info,unsigned int pin,enum ptp_pin_function func,unsigned int chan)1792 static int ice_verify_pin(struct ptp_clock_info *info, unsigned int pin,
1793 enum ptp_pin_function func, unsigned int chan)
1794 {
1795 struct ice_pf *pf = ptp_info_to_pf(info);
1796 const struct ice_ptp_pin_desc *pin_desc;
1797
1798 pin_desc = &pf->ptp.ice_pin_desc[pin];
1799
1800 /* Is assigned function allowed? */
1801 switch (func) {
1802 case PTP_PF_EXTTS:
1803 if (pin_desc->gpio[0] < 0)
1804 return -EOPNOTSUPP;
1805 break;
1806 case PTP_PF_PEROUT:
1807 if (pin_desc->gpio[1] < 0)
1808 return -EOPNOTSUPP;
1809 break;
1810 case PTP_PF_NONE:
1811 break;
1812 case PTP_PF_PHYSYNC:
1813 default:
1814 return -EOPNOTSUPP;
1815 }
1816
1817 return 0;
1818 }
1819
1820 /**
1821 * ice_ptp_gpio_enable - Enable/disable ancillary features of PHC
1822 * @info: The driver's PTP info structure
1823 * @rq: The requested feature to change
1824 * @on: Enable/disable flag
1825 *
1826 * Return: 0 on success, negative error code otherwise
1827 */
ice_ptp_gpio_enable(struct ptp_clock_info * info,struct ptp_clock_request * rq,int on)1828 static int ice_ptp_gpio_enable(struct ptp_clock_info *info,
1829 struct ptp_clock_request *rq, int on)
1830 {
1831 struct ice_pf *pf = ptp_info_to_pf(info);
1832 int err;
1833
1834 switch (rq->type) {
1835 case PTP_CLK_REQ_PEROUT:
1836 {
1837 struct ptp_perout_request *cached =
1838 &pf->ptp.perout_rqs[rq->perout.index];
1839
1840 err = ice_ptp_cfg_perout(pf, &rq->perout, on);
1841 if (!err) {
1842 *cached = rq->perout;
1843 } else {
1844 cached->period.sec = 0;
1845 cached->period.nsec = 0;
1846 }
1847 return err;
1848 }
1849 case PTP_CLK_REQ_EXTTS:
1850 {
1851 struct ptp_extts_request *cached =
1852 &pf->ptp.extts_rqs[rq->extts.index];
1853
1854 err = ice_ptp_cfg_extts(pf, &rq->extts, on);
1855 if (!err)
1856 *cached = rq->extts;
1857 else
1858 cached->flags &= ~PTP_ENABLE_FEATURE;
1859 return err;
1860 }
1861 default:
1862 return -EOPNOTSUPP;
1863 }
1864 }
1865
1866 /**
1867 * ice_ptp_gettimex64 - Get the time of the clock
1868 * @info: the driver's PTP info structure
1869 * @ts: timespec64 structure to hold the current time value
1870 * @sts: Optional parameter for holding a pair of system timestamps from
1871 * the system clock. Will be ignored if NULL is given.
1872 *
1873 * Read the device clock and return the correct value on ns, after converting it
1874 * into a timespec struct.
1875 */
1876 static int
ice_ptp_gettimex64(struct ptp_clock_info * info,struct timespec64 * ts,struct ptp_system_timestamp * sts)1877 ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
1878 struct ptp_system_timestamp *sts)
1879 {
1880 struct ice_pf *pf = ptp_info_to_pf(info);
1881 u64 time_ns;
1882
1883 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
1884 *ts = ns_to_timespec64(time_ns);
1885 return 0;
1886 }
1887
1888 /**
1889 * ice_ptp_settime64 - Set the time of the clock
1890 * @info: the driver's PTP info structure
1891 * @ts: timespec64 structure that holds the new time value
1892 *
1893 * Set the device clock to the user input value. The conversion from timespec
1894 * to ns happens in the write function.
1895 */
1896 static int
ice_ptp_settime64(struct ptp_clock_info * info,const struct timespec64 * ts)1897 ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
1898 {
1899 struct ice_pf *pf = ptp_info_to_pf(info);
1900 struct timespec64 ts64 = *ts;
1901 struct ice_hw *hw = &pf->hw;
1902 int err;
1903
1904 /* For Vernier mode on E82X, we need to recalibrate after new settime.
1905 * Start with marking timestamps as invalid.
1906 */
1907 if (hw->mac_type == ICE_MAC_GENERIC) {
1908 err = ice_ptp_clear_phy_offset_ready_e82x(hw);
1909 if (err)
1910 dev_warn(ice_pf_to_dev(pf), "Failed to mark timestamps as invalid before settime\n");
1911 }
1912
1913 if (!ice_ptp_lock(hw)) {
1914 err = -EBUSY;
1915 goto exit;
1916 }
1917
1918 /* Disable periodic outputs */
1919 ice_ptp_disable_all_perout(pf);
1920
1921 err = ice_ptp_write_init(pf, &ts64);
1922 ice_ptp_unlock(hw);
1923
1924 if (!err)
1925 ice_ptp_reset_cached_phctime(pf);
1926
1927 /* Reenable periodic outputs */
1928 ice_ptp_enable_all_perout(pf);
1929
1930 /* Recalibrate and re-enable timestamp blocks for E822/E823 */
1931 if (hw->mac_type == ICE_MAC_GENERIC)
1932 ice_ptp_restart_all_phy(pf);
1933 exit:
1934 if (err) {
1935 dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
1936 return err;
1937 }
1938
1939 return 0;
1940 }
1941
1942 /**
1943 * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
1944 * @info: the driver's PTP info structure
1945 * @delta: Offset in nanoseconds to adjust the time by
1946 */
ice_ptp_adjtime_nonatomic(struct ptp_clock_info * info,s64 delta)1947 static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
1948 {
1949 struct timespec64 now, then;
1950 int ret;
1951
1952 then = ns_to_timespec64(delta);
1953 ret = ice_ptp_gettimex64(info, &now, NULL);
1954 if (ret)
1955 return ret;
1956 now = timespec64_add(now, then);
1957
1958 return ice_ptp_settime64(info, (const struct timespec64 *)&now);
1959 }
1960
1961 /**
1962 * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
1963 * @info: the driver's PTP info structure
1964 * @delta: Offset in nanoseconds to adjust the time by
1965 */
ice_ptp_adjtime(struct ptp_clock_info * info,s64 delta)1966 static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
1967 {
1968 struct ice_pf *pf = ptp_info_to_pf(info);
1969 struct ice_hw *hw = &pf->hw;
1970 struct device *dev;
1971 int err;
1972
1973 dev = ice_pf_to_dev(pf);
1974
1975 /* Hardware only supports atomic adjustments using signed 32-bit
1976 * integers. For any adjustment outside this range, perform
1977 * a non-atomic get->adjust->set flow.
1978 */
1979 if (delta > S32_MAX || delta < S32_MIN) {
1980 dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
1981 return ice_ptp_adjtime_nonatomic(info, delta);
1982 }
1983
1984 if (!ice_ptp_lock(hw)) {
1985 dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
1986 return -EBUSY;
1987 }
1988
1989 /* Disable periodic outputs */
1990 ice_ptp_disable_all_perout(pf);
1991
1992 err = ice_ptp_write_adj(pf, delta);
1993
1994 /* Reenable periodic outputs */
1995 ice_ptp_enable_all_perout(pf);
1996
1997 ice_ptp_unlock(hw);
1998
1999 if (err) {
2000 dev_err(dev, "PTP failed to adjust time, err %d\n", err);
2001 return err;
2002 }
2003
2004 ice_ptp_reset_cached_phctime(pf);
2005
2006 return 0;
2007 }
2008
2009 /**
2010 * struct ice_crosststamp_cfg - Device cross timestamp configuration
2011 * @lock_reg: The hardware semaphore lock to use
2012 * @lock_busy: Bit in the semaphore lock indicating the lock is busy
2013 * @ctl_reg: The hardware register to request cross timestamp
2014 * @ctl_active: Bit in the control register to request cross timestamp
2015 * @art_time_l: Lower 32-bits of ART system time
2016 * @art_time_h: Upper 32-bits of ART system time
2017 * @dev_time_l: Lower 32-bits of device time (per timer index)
2018 * @dev_time_h: Upper 32-bits of device time (per timer index)
2019 */
2020 struct ice_crosststamp_cfg {
2021 /* HW semaphore lock register */
2022 u32 lock_reg;
2023 u32 lock_busy;
2024
2025 /* Capture control register */
2026 u32 ctl_reg;
2027 u32 ctl_active;
2028
2029 /* Time storage */
2030 u32 art_time_l;
2031 u32 art_time_h;
2032 u32 dev_time_l[2];
2033 u32 dev_time_h[2];
2034 };
2035
2036 static const struct ice_crosststamp_cfg ice_crosststamp_cfg_e82x = {
2037 .lock_reg = PFHH_SEM,
2038 .lock_busy = PFHH_SEM_BUSY_M,
2039 .ctl_reg = GLHH_ART_CTL,
2040 .ctl_active = GLHH_ART_CTL_ACTIVE_M,
2041 .art_time_l = GLHH_ART_TIME_L,
2042 .art_time_h = GLHH_ART_TIME_H,
2043 .dev_time_l[0] = GLTSYN_HHTIME_L(0),
2044 .dev_time_h[0] = GLTSYN_HHTIME_H(0),
2045 .dev_time_l[1] = GLTSYN_HHTIME_L(1),
2046 .dev_time_h[1] = GLTSYN_HHTIME_H(1),
2047 };
2048
2049 #ifdef CONFIG_ICE_HWTS
2050 static const struct ice_crosststamp_cfg ice_crosststamp_cfg_e830 = {
2051 .lock_reg = E830_PFPTM_SEM,
2052 .lock_busy = E830_PFPTM_SEM_BUSY_M,
2053 .ctl_reg = E830_GLPTM_ART_CTL,
2054 .ctl_active = E830_GLPTM_ART_CTL_ACTIVE_M,
2055 .art_time_l = E830_GLPTM_ART_TIME_L,
2056 .art_time_h = E830_GLPTM_ART_TIME_H,
2057 .dev_time_l[0] = E830_GLTSYN_PTMTIME_L(0),
2058 .dev_time_h[0] = E830_GLTSYN_PTMTIME_H(0),
2059 .dev_time_l[1] = E830_GLTSYN_PTMTIME_L(1),
2060 .dev_time_h[1] = E830_GLTSYN_PTMTIME_H(1),
2061 };
2062
2063 #endif /* CONFIG_ICE_HWTS */
2064 /**
2065 * struct ice_crosststamp_ctx - Device cross timestamp context
2066 * @snapshot: snapshot of system clocks for historic interpolation
2067 * @snapshot_clock_id: System clock ID for @snapshot
2068 * @pf: pointer to the PF private structure
2069 * @cfg: pointer to hardware configuration for cross timestamp
2070 */
2071 struct ice_crosststamp_ctx {
2072 struct system_time_snapshot snapshot;
2073 clockid_t snapshot_clock_id;
2074 struct ice_pf *pf;
2075 const struct ice_crosststamp_cfg *cfg;
2076 };
2077
2078 /**
2079 * ice_capture_crosststamp - Capture a device/system cross timestamp
2080 * @device: Current device time
2081 * @system: System counter value read synchronously with device time
2082 * @__ctx: Context passed from ice_ptp_getcrosststamp
2083 *
2084 * Read device and system (ART) clock simultaneously and return the corrected
2085 * clock values in ns.
2086 *
2087 * Return: zero on success, or a negative error code on failure.
2088 */
ice_capture_crosststamp(ktime_t * device,struct system_counterval_t * system,void * __ctx)2089 static int ice_capture_crosststamp(ktime_t *device,
2090 struct system_counterval_t *system,
2091 void *__ctx)
2092 {
2093 struct ice_crosststamp_ctx *ctx = __ctx;
2094 const struct ice_crosststamp_cfg *cfg;
2095 u32 lock, ctl, ts_lo, ts_hi, tmr_idx;
2096 struct ice_pf *pf;
2097 struct ice_hw *hw;
2098 int err;
2099 u64 ts;
2100
2101 cfg = ctx->cfg;
2102 pf = ctx->pf;
2103 hw = &pf->hw;
2104
2105 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
2106 if (tmr_idx > 1)
2107 return -EINVAL;
2108
2109 /* Poll until we obtain the cross-timestamp hardware semaphore */
2110 err = rd32_poll_timeout(hw, cfg->lock_reg, lock,
2111 !(lock & cfg->lock_busy),
2112 10 * USEC_PER_MSEC, 50 * USEC_PER_MSEC);
2113 if (err) {
2114 dev_err(ice_pf_to_dev(pf), "PTP failed to get cross timestamp lock\n");
2115 return -EBUSY;
2116 }
2117
2118 /* Snapshot system time for historic interpolation */
2119 ktime_get_snapshot_id(ctx->snapshot_clock_id, &ctx->snapshot);
2120
2121 /* Program cmd to master timer */
2122 ice_ptp_src_cmd(hw, ICE_PTP_READ_TIME);
2123
2124 /* Start the ART and device clock sync sequence */
2125 ctl = rd32(hw, cfg->ctl_reg);
2126 ctl |= cfg->ctl_active;
2127 wr32(hw, cfg->ctl_reg, ctl);
2128
2129 /* Poll until hardware completes the capture */
2130 err = rd32_poll_timeout(hw, cfg->ctl_reg, ctl, !(ctl & cfg->ctl_active),
2131 5, 20 * USEC_PER_MSEC);
2132 if (err)
2133 goto err_timeout;
2134
2135 /* Read ART system time */
2136 ts_lo = rd32(hw, cfg->art_time_l);
2137 ts_hi = rd32(hw, cfg->art_time_h);
2138 ts = ((u64)ts_hi << 32) | ts_lo;
2139 system->cycles = ts;
2140 system->cs_id = CSID_X86_ART;
2141 system->use_nsecs = true;
2142
2143 /* Read Device source clock time */
2144 ts_lo = rd32(hw, cfg->dev_time_l[tmr_idx]);
2145 ts_hi = rd32(hw, cfg->dev_time_h[tmr_idx]);
2146 ts = ((u64)ts_hi << 32) | ts_lo;
2147 *device = ns_to_ktime(ts);
2148
2149 err_timeout:
2150 /* Clear the master timer */
2151 ice_ptp_src_cmd(hw, ICE_PTP_NOP);
2152
2153 /* Release HW lock */
2154 lock = rd32(hw, cfg->lock_reg);
2155 lock &= ~cfg->lock_busy;
2156 wr32(hw, cfg->lock_reg, lock);
2157
2158 return err;
2159 }
2160
2161 /**
2162 * ice_ptp_getcrosststamp - Capture a device cross timestamp
2163 * @info: the driver's PTP info structure
2164 * @cts: The memory to fill the cross timestamp info
2165 *
2166 * Capture a cross timestamp between the ART and the device PTP hardware
2167 * clock. Fill the cross timestamp information and report it back to the
2168 * caller.
2169 *
2170 * In order to correctly correlate the ART timestamp back to the TSC time, the
2171 * CPU must have X86_FEATURE_TSC_KNOWN_FREQ.
2172 *
2173 * Return: zero on success, or a negative error code on failure.
2174 */
ice_ptp_getcrosststamp(struct ptp_clock_info * info,struct system_device_crosststamp * cts)2175 static int ice_ptp_getcrosststamp(struct ptp_clock_info *info,
2176 struct system_device_crosststamp *cts)
2177 {
2178 struct ice_pf *pf = ptp_info_to_pf(info);
2179 struct ice_crosststamp_ctx ctx = {
2180 .snapshot_clock_id = cts->clock_id,
2181 .pf = pf,
2182 };
2183
2184 switch (pf->hw.mac_type) {
2185 case ICE_MAC_GENERIC:
2186 case ICE_MAC_GENERIC_3K_E825:
2187 ctx.cfg = &ice_crosststamp_cfg_e82x;
2188 break;
2189 #ifdef CONFIG_ICE_HWTS
2190 case ICE_MAC_E830:
2191 ctx.cfg = &ice_crosststamp_cfg_e830;
2192 break;
2193 #endif /* CONFIG_ICE_HWTS */
2194 default:
2195 return -EOPNOTSUPP;
2196 }
2197
2198 return get_device_system_crosststamp(ice_capture_crosststamp, &ctx,
2199 &ctx.snapshot, cts);
2200 }
2201
2202 /**
2203 * ice_ptp_hwtstamp_get - interface to read the timestamping config
2204 * @netdev: Pointer to network interface device structure
2205 * @config: Timestamping configuration structure
2206 *
2207 * Copy the timestamping config to user buffer
2208 */
ice_ptp_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)2209 int ice_ptp_hwtstamp_get(struct net_device *netdev,
2210 struct kernel_hwtstamp_config *config)
2211 {
2212 struct ice_pf *pf = ice_netdev_to_pf(netdev);
2213
2214 if (pf->ptp.state != ICE_PTP_READY)
2215 return -EIO;
2216
2217 *config = pf->ptp.tstamp_config;
2218
2219 return 0;
2220 }
2221
2222 /**
2223 * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
2224 * @pf: Board private structure
2225 * @config: hwtstamp settings requested or saved
2226 */
ice_ptp_set_timestamp_mode(struct ice_pf * pf,struct kernel_hwtstamp_config * config)2227 static int ice_ptp_set_timestamp_mode(struct ice_pf *pf,
2228 struct kernel_hwtstamp_config *config)
2229 {
2230 switch (config->tx_type) {
2231 case HWTSTAMP_TX_OFF:
2232 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF;
2233 break;
2234 case HWTSTAMP_TX_ON:
2235 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON;
2236 break;
2237 default:
2238 return -ERANGE;
2239 }
2240
2241 switch (config->rx_filter) {
2242 case HWTSTAMP_FILTER_NONE:
2243 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2244 break;
2245 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2246 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2247 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2248 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2249 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2250 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2251 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2252 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2253 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2254 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2255 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2256 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2257 case HWTSTAMP_FILTER_NTP_ALL:
2258 case HWTSTAMP_FILTER_ALL:
2259 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL;
2260 break;
2261 default:
2262 return -ERANGE;
2263 }
2264
2265 /* Immediately update the device timestamping mode */
2266 ice_ptp_restore_timestamp_mode(pf);
2267
2268 return 0;
2269 }
2270
2271 /**
2272 * ice_ptp_hwtstamp_set - interface to control the timestamping
2273 * @netdev: Pointer to network interface device structure
2274 * @config: Timestamping configuration structure
2275 * @extack: Netlink extended ack structure for error reporting
2276 *
2277 * Get the user config and store it
2278 */
ice_ptp_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)2279 int ice_ptp_hwtstamp_set(struct net_device *netdev,
2280 struct kernel_hwtstamp_config *config,
2281 struct netlink_ext_ack *extack)
2282 {
2283 struct ice_pf *pf = ice_netdev_to_pf(netdev);
2284 int err;
2285
2286 if (pf->ptp.state != ICE_PTP_READY)
2287 return -EAGAIN;
2288
2289 err = ice_ptp_set_timestamp_mode(pf, config);
2290 if (err)
2291 return err;
2292
2293 /* Return the actual configuration set */
2294 *config = pf->ptp.tstamp_config;
2295
2296 return 0;
2297 }
2298
2299 /**
2300 * ice_ptp_get_rx_hwts - Get packet Rx timestamp in ns
2301 * @rx_desc: Receive descriptor
2302 * @pkt_ctx: Packet context to get the cached time
2303 *
2304 * The driver receives a notification in the receive descriptor with timestamp.
2305 */
ice_ptp_get_rx_hwts(const union ice_32b_rx_flex_desc * rx_desc,const struct ice_pkt_ctx * pkt_ctx)2306 u64 ice_ptp_get_rx_hwts(const union ice_32b_rx_flex_desc *rx_desc,
2307 const struct ice_pkt_ctx *pkt_ctx)
2308 {
2309 u64 ts_ns, cached_time;
2310 u32 ts_high;
2311
2312 if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID))
2313 return 0;
2314
2315 cached_time = READ_ONCE(pkt_ctx->cached_phctime);
2316
2317 /* Do not report a timestamp if we don't have a cached PHC time */
2318 if (!cached_time)
2319 return 0;
2320
2321 /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached
2322 * PHC value, rather than accessing the PF. This also allows us to
2323 * simply pass the upper 32bits of nanoseconds directly. Calling
2324 * ice_ptp_extend_40b_ts is unnecessary as it would just discard these
2325 * bits itself.
2326 */
2327 ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
2328 ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high);
2329
2330 return ts_ns;
2331 }
2332
2333 /**
2334 * ice_ptp_setup_pin_cfg - setup PTP pin_config structure
2335 * @pf: Board private structure
2336 */
ice_ptp_setup_pin_cfg(struct ice_pf * pf)2337 static void ice_ptp_setup_pin_cfg(struct ice_pf *pf)
2338 {
2339 for (unsigned int i = 0; i < pf->ptp.info.n_pins; i++) {
2340 const struct ice_ptp_pin_desc *desc = &pf->ptp.ice_pin_desc[i];
2341 struct ptp_pin_desc *pin = &pf->ptp.pin_desc[i];
2342 const char *name;
2343
2344 if (!ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
2345 name = ice_pin_names[desc->name_idx];
2346 else
2347 name = ice_pin_names_dpll[desc->name_idx];
2348
2349 strscpy(pin->name, name, sizeof(pin->name));
2350
2351 pin->index = i;
2352 }
2353
2354 pf->ptp.info.pin_config = pf->ptp.pin_desc;
2355 }
2356
2357 /**
2358 * ice_ptp_disable_pins - Disable PTP pins
2359 * @pf: pointer to the PF structure
2360 *
2361 * Disable the OS access to the pins. Called to clear out the OS
2362 * indications of pin support when we fail to setup pin array.
2363 */
ice_ptp_disable_pins(struct ice_pf * pf)2364 static void ice_ptp_disable_pins(struct ice_pf *pf)
2365 {
2366 struct ptp_clock_info *info = &pf->ptp.info;
2367
2368 dev_warn(ice_pf_to_dev(pf), "Failed to configure PTP pin control\n");
2369
2370 info->enable = NULL;
2371 info->verify = NULL;
2372 info->n_pins = 0;
2373 info->n_ext_ts = 0;
2374 info->n_per_out = 0;
2375 }
2376
2377 /**
2378 * ice_ptp_parse_sdp_entries - update ice_ptp_pin_desc structure from NVM
2379 * @pf: pointer to the PF structure
2380 * @entries: SDP connection section from NVM
2381 * @num_entries: number of valid entries in sdp_entries
2382 * @pins: PTP pins array to update
2383 *
2384 * Return: 0 on success, negative error code otherwise.
2385 */
ice_ptp_parse_sdp_entries(struct ice_pf * pf,__le16 * entries,unsigned int num_entries,struct ice_ptp_pin_desc * pins)2386 static int ice_ptp_parse_sdp_entries(struct ice_pf *pf, __le16 *entries,
2387 unsigned int num_entries,
2388 struct ice_ptp_pin_desc *pins)
2389 {
2390 unsigned int n_pins = 0;
2391 unsigned int i;
2392
2393 /* Setup ice_pin_desc array */
2394 for (i = 0; i < ICE_N_PINS_MAX; i++) {
2395 pins[i].name_idx = -1;
2396 pins[i].gpio[0] = -1;
2397 pins[i].gpio[1] = -1;
2398 }
2399
2400 for (i = 0; i < num_entries; i++) {
2401 u16 entry = le16_to_cpu(entries[i]);
2402 DECLARE_BITMAP(bitmap, GPIO_NA);
2403 unsigned int idx;
2404 bool dir;
2405 u16 gpio;
2406
2407 *bitmap = FIELD_GET(ICE_AQC_NVM_SDP_AC_PIN_M, entry);
2408
2409 /* Check if entry's pin bitmap is valid. */
2410 if (bitmap_empty(bitmap, GPIO_NA))
2411 continue;
2412
2413 dir = !!FIELD_GET(ICE_AQC_NVM_SDP_AC_DIR_M, entry);
2414 gpio = FIELD_GET(ICE_AQC_NVM_SDP_AC_SDP_NUM_M, entry);
2415
2416 for (idx = 0; idx < ICE_N_PINS_MAX; idx++) {
2417 if (pins[idx].name_idx == gpio)
2418 break;
2419 }
2420
2421 if (idx == ICE_N_PINS_MAX) {
2422 /* Pin not found, setup its entry and name */
2423 idx = n_pins++;
2424 pins[idx].name_idx = gpio;
2425 }
2426 pins[idx].gpio[dir] = gpio;
2427 }
2428
2429 for (i = 0; i < n_pins; i++) {
2430 dev_dbg(ice_pf_to_dev(pf),
2431 "NVM pin entry[%d] : name_idx %d gpio_out %d gpio_in %d\n",
2432 i, pins[i].name_idx, pins[i].gpio[1], pins[i].gpio[0]);
2433 }
2434
2435 pf->ptp.info.n_pins = n_pins;
2436 return 0;
2437 }
2438
2439 /**
2440 * ice_ptp_set_funcs_e82x - Set specialized functions for E82X support
2441 * @pf: Board private structure
2442 *
2443 * Assign functions to the PTP capabilities structure for E82X devices.
2444 * Functions which operate across all device families should be set directly
2445 * in ice_ptp_set_caps. Only add functions here which are distinct for E82X
2446 * devices.
2447 */
ice_ptp_set_funcs_e82x(struct ice_pf * pf)2448 static void ice_ptp_set_funcs_e82x(struct ice_pf *pf)
2449 {
2450 pf->ptp.info.getcrosststamp = ice_ptp_getcrosststamp;
2451
2452 if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
2453 pf->ptp.ice_pin_desc = ice_pin_desc_e825c;
2454 pf->ptp.info.n_pins = ARRAY_SIZE(ice_pin_desc_e825c);
2455 } else {
2456 pf->ptp.ice_pin_desc = ice_pin_desc_e82x;
2457 pf->ptp.info.n_pins = ARRAY_SIZE(ice_pin_desc_e82x);
2458 }
2459 ice_ptp_setup_pin_cfg(pf);
2460 }
2461
2462 /**
2463 * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
2464 * @pf: Board private structure
2465 *
2466 * Assign functions to the PTP capabiltiies structure for E810 devices.
2467 * Functions which operate across all device families should be set directly
2468 * in ice_ptp_set_caps. Only add functions here which are distinct for E810
2469 * devices.
2470 */
ice_ptp_set_funcs_e810(struct ice_pf * pf)2471 static void ice_ptp_set_funcs_e810(struct ice_pf *pf)
2472 {
2473 __le16 entries[ICE_AQC_NVM_SDP_AC_MAX_SIZE];
2474 struct ice_ptp_pin_desc *desc = NULL;
2475 struct ice_ptp *ptp = &pf->ptp;
2476 unsigned int num_entries;
2477 int err;
2478
2479 err = ice_ptp_read_sdp_ac(&pf->hw, entries, &num_entries);
2480 if (err) {
2481 /* SDP section does not exist in NVM or is corrupted */
2482 if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
2483 ptp->ice_pin_desc = ice_pin_desc_dpll;
2484 ptp->info.n_pins = ARRAY_SIZE(ice_pin_desc_dpll);
2485 } else {
2486 pf->ptp.ice_pin_desc = ice_pin_desc_e810;
2487 pf->ptp.info.n_pins = ARRAY_SIZE(ice_pin_desc_e810);
2488 }
2489 err = 0;
2490 } else {
2491 desc = devm_kcalloc(ice_pf_to_dev(pf), ICE_N_PINS_MAX,
2492 sizeof(struct ice_ptp_pin_desc),
2493 GFP_KERNEL);
2494 if (!desc)
2495 goto err;
2496
2497 err = ice_ptp_parse_sdp_entries(pf, entries, num_entries, desc);
2498 if (err)
2499 goto err;
2500
2501 ptp->ice_pin_desc = (const struct ice_ptp_pin_desc *)desc;
2502 }
2503
2504 ptp->info.pin_config = ptp->pin_desc;
2505 ice_ptp_setup_pin_cfg(pf);
2506
2507 err:
2508 if (err) {
2509 devm_kfree(ice_pf_to_dev(pf), desc);
2510 ice_ptp_disable_pins(pf);
2511 }
2512 }
2513
2514 /**
2515 * ice_ptp_set_funcs_e830 - Set specialized functions for E830 support
2516 * @pf: Board private structure
2517 *
2518 * Assign functions to the PTP capabiltiies structure for E830 devices.
2519 * Functions which operate across all device families should be set directly
2520 * in ice_ptp_set_caps. Only add functions here which are distinct for E830
2521 * devices.
2522 */
ice_ptp_set_funcs_e830(struct ice_pf * pf)2523 static void ice_ptp_set_funcs_e830(struct ice_pf *pf)
2524 {
2525 #ifdef CONFIG_ICE_HWTS
2526 if (pcie_ptm_enabled(pf->pdev) && boot_cpu_has(X86_FEATURE_ART))
2527 pf->ptp.info.getcrosststamp = ice_ptp_getcrosststamp;
2528
2529 #endif /* CONFIG_ICE_HWTS */
2530 /* Rest of the config is the same as base E810 */
2531 pf->ptp.ice_pin_desc = ice_pin_desc_e810;
2532 pf->ptp.info.n_pins = ARRAY_SIZE(ice_pin_desc_e810);
2533 ice_ptp_setup_pin_cfg(pf);
2534 }
2535
2536 /**
2537 * ice_ptp_set_caps - Set PTP capabilities
2538 * @pf: Board private structure
2539 */
ice_ptp_set_caps(struct ice_pf * pf)2540 static void ice_ptp_set_caps(struct ice_pf *pf)
2541 {
2542 struct ptp_clock_info *info = &pf->ptp.info;
2543 struct device *dev = ice_pf_to_dev(pf);
2544
2545 snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
2546 dev_driver_string(dev), dev_name(dev));
2547 info->owner = THIS_MODULE;
2548 info->max_adj = 100000000;
2549 info->adjtime = ice_ptp_adjtime;
2550 info->adjfine = ice_ptp_adjfine;
2551 info->gettimex64 = ice_ptp_gettimex64;
2552 info->settime64 = ice_ptp_settime64;
2553 info->n_per_out = GLTSYN_TGT_H_IDX_MAX;
2554 info->n_ext_ts = GLTSYN_EVNT_H_IDX_MAX;
2555 info->enable = ice_ptp_gpio_enable;
2556 info->verify = ice_verify_pin;
2557
2558 info->supported_extts_flags = PTP_RISING_EDGE |
2559 PTP_FALLING_EDGE |
2560 PTP_STRICT_FLAGS;
2561 info->supported_perout_flags = PTP_PEROUT_PHASE;
2562
2563 switch (pf->hw.mac_type) {
2564 case ICE_MAC_E810:
2565 ice_ptp_set_funcs_e810(pf);
2566 return;
2567 case ICE_MAC_E830:
2568 ice_ptp_set_funcs_e830(pf);
2569 return;
2570 case ICE_MAC_GENERIC:
2571 case ICE_MAC_GENERIC_3K_E825:
2572 ice_ptp_set_funcs_e82x(pf);
2573 return;
2574 default:
2575 return;
2576 }
2577 }
2578
2579 /**
2580 * ice_ptp_create_clock - Create PTP clock device for userspace
2581 * @pf: Board private structure
2582 *
2583 * This function creates a new PTP clock device. It only creates one if we
2584 * don't already have one. Will return error if it can't create one, but success
2585 * if we already have a device. Should be used by ice_ptp_init to create clock
2586 * initially, and prevent global resets from creating new clock devices.
2587 */
ice_ptp_create_clock(struct ice_pf * pf)2588 static long ice_ptp_create_clock(struct ice_pf *pf)
2589 {
2590 struct ptp_clock_info *info;
2591 struct device *dev;
2592
2593 /* No need to create a clock device if we already have one */
2594 if (pf->ptp.clock)
2595 return 0;
2596
2597 ice_ptp_set_caps(pf);
2598
2599 info = &pf->ptp.info;
2600 dev = ice_pf_to_dev(pf);
2601
2602 /* Attempt to register the clock before enabling the hardware. */
2603 pf->ptp.clock = ptp_clock_register(info, dev);
2604 if (IS_ERR(pf->ptp.clock)) {
2605 dev_err(ice_pf_to_dev(pf), "Failed to register PTP clock device");
2606 return PTR_ERR(pf->ptp.clock);
2607 }
2608
2609 return 0;
2610 }
2611
2612 /**
2613 * ice_ptp_request_ts - Request an available Tx timestamp index
2614 * @tx: the PTP Tx timestamp tracker to request from
2615 * @skb: the SKB to associate with this timestamp request
2616 */
ice_ptp_request_ts(struct ice_ptp_tx * tx,struct sk_buff * skb)2617 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
2618 {
2619 unsigned long flags;
2620 u8 idx;
2621
2622 spin_lock_irqsave(&tx->lock, flags);
2623
2624 /* Check that this tracker is accepting new timestamp requests */
2625 if (!ice_ptp_is_tx_tracker_up(tx)) {
2626 spin_unlock_irqrestore(&tx->lock, flags);
2627 return -1;
2628 }
2629
2630 /* Find and set the first available index */
2631 idx = find_next_zero_bit(tx->in_use, tx->len,
2632 tx->last_ll_ts_idx_read + 1);
2633 if (idx == tx->len)
2634 idx = find_first_zero_bit(tx->in_use, tx->len);
2635
2636 if (idx < tx->len) {
2637 /* We got a valid index that no other thread could have set. Store
2638 * a reference to the skb and the start time to allow discarding old
2639 * requests.
2640 */
2641 set_bit(idx, tx->in_use);
2642 clear_bit(idx, tx->stale);
2643 tx->tstamps[idx].start = jiffies;
2644 tx->tstamps[idx].skb = skb_get(skb);
2645 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2646 ice_trace(tx_tstamp_request, skb, idx);
2647 }
2648
2649 spin_unlock_irqrestore(&tx->lock, flags);
2650
2651 /* return the appropriate PHY timestamp register index, -1 if no
2652 * indexes were available.
2653 */
2654 if (idx >= tx->len)
2655 return -1;
2656 else
2657 return idx + tx->offset;
2658 }
2659
ice_ptp_process_ts(struct ice_pf * pf)2660 void ice_ptp_process_ts(struct ice_pf *pf)
2661 {
2662 switch (pf->ptp.tx_interrupt_mode) {
2663 case ICE_PTP_TX_INTERRUPT_NONE:
2664 /* This device has the clock owner handle timestamps for it */
2665 return;
2666 case ICE_PTP_TX_INTERRUPT_SELF:
2667 /* This device handles its own timestamps */
2668 ice_ptp_process_tx_tstamp(&pf->ptp.port.tx);
2669 return;
2670 case ICE_PTP_TX_INTERRUPT_ALL:
2671 /* This device handles timestamps for all ports */
2672 ice_ptp_tx_tstamp_owner(pf);
2673 return;
2674 default:
2675 WARN_ONCE(1, "Unexpected Tx timestamp interrupt mode %u\n",
2676 pf->ptp.tx_interrupt_mode);
2677 return;
2678 }
2679 }
2680
ice_port_has_timestamps(struct ice_ptp_tx * tx)2681 static bool ice_port_has_timestamps(struct ice_ptp_tx *tx)
2682 {
2683 bool more_timestamps;
2684
2685 scoped_guard(spinlock_irqsave, &tx->lock) {
2686 if (!tx->init)
2687 return false;
2688
2689 more_timestamps = !bitmap_empty(tx->in_use, tx->len);
2690 }
2691
2692 return more_timestamps;
2693 }
2694
ice_any_port_has_timestamps(struct ice_pf * pf)2695 static bool ice_any_port_has_timestamps(struct ice_pf *pf)
2696 {
2697 struct ice_ptp_port *port;
2698
2699 scoped_guard(mutex, &pf->adapter->ports.lock) {
2700 list_for_each_entry(port, &pf->adapter->ports.ports,
2701 list_node) {
2702 struct ice_ptp_tx *tx = &port->tx;
2703
2704 if (ice_port_has_timestamps(tx))
2705 return true;
2706 }
2707 }
2708
2709 return false;
2710 }
2711
ice_ptp_tx_tstamps_pending(struct ice_pf * pf)2712 bool ice_ptp_tx_tstamps_pending(struct ice_pf *pf)
2713 {
2714 struct ice_hw *hw = &pf->hw;
2715 int ret;
2716
2717 /* Check software indicator */
2718 switch (pf->ptp.tx_interrupt_mode) {
2719 case ICE_PTP_TX_INTERRUPT_NONE:
2720 return false;
2721 case ICE_PTP_TX_INTERRUPT_SELF:
2722 if (ice_port_has_timestamps(&pf->ptp.port.tx))
2723 return true;
2724 break;
2725 case ICE_PTP_TX_INTERRUPT_ALL:
2726 if (ice_any_port_has_timestamps(pf))
2727 return true;
2728 break;
2729 default:
2730 WARN_ONCE(1, "Unexpected Tx timestamp interrupt mode %u\n",
2731 pf->ptp.tx_interrupt_mode);
2732 break;
2733 }
2734
2735 /* Check hardware indicator */
2736 ret = ice_check_phy_tx_tstamp_ready(hw);
2737 if (ret < 0) {
2738 dev_dbg(ice_pf_to_dev(pf), "Unable to read PHY Tx timestamp ready bitmap, err %d\n",
2739 ret);
2740 /* Stop triggering IRQs if we're unable to read PHY */
2741 return false;
2742 }
2743
2744 /* ice_check_phy_tx_tstamp_ready() returns 1 if there are timestamps
2745 * available, 0 if there are no waiting timestamps, and a negative
2746 * value if there was an error (which we checked for above).
2747 */
2748 return ret > 0;
2749 }
2750
2751 /**
2752 * ice_ptp_ts_irq - Process the PTP Tx timestamps in IRQ context
2753 * @pf: Board private structure
2754 *
2755 * Return: IRQ_WAKE_THREAD if Tx timestamp read has to be handled in the bottom
2756 * half of the interrupt and IRQ_HANDLED otherwise.
2757 */
ice_ptp_ts_irq(struct ice_pf * pf)2758 irqreturn_t ice_ptp_ts_irq(struct ice_pf *pf)
2759 {
2760 struct ice_hw *hw = &pf->hw;
2761
2762 switch (hw->mac_type) {
2763 case ICE_MAC_E810:
2764 /* E810 capable of low latency timestamping with interrupt can
2765 * request a single timestamp in the top half and wait for
2766 * a second LL TS interrupt from the FW when it's ready.
2767 */
2768 if (hw->dev_caps.ts_dev_info.ts_ll_int_read) {
2769 struct ice_ptp_tx *tx = &pf->ptp.port.tx;
2770 u8 idx, last;
2771
2772 if (!ice_pf_state_is_nominal(pf))
2773 return IRQ_HANDLED;
2774
2775 spin_lock(&tx->lock);
2776 if (tx->init) {
2777 last = tx->last_ll_ts_idx_read + 1;
2778 idx = find_next_bit_wrap(tx->in_use, tx->len,
2779 last);
2780 if (idx != tx->len)
2781 ice_ptp_req_tx_single_tstamp(tx, idx);
2782 }
2783 spin_unlock(&tx->lock);
2784
2785 return IRQ_HANDLED;
2786 }
2787 fallthrough; /* non-LL_TS E810 */
2788 case ICE_MAC_GENERIC:
2789 case ICE_MAC_GENERIC_3K_E825:
2790 /* All other devices process timestamps in the bottom half due
2791 * to sleeping or polling.
2792 */
2793 if (!ice_ptp_pf_handles_tx_interrupt(pf))
2794 return IRQ_HANDLED;
2795
2796 set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
2797 return IRQ_WAKE_THREAD;
2798 case ICE_MAC_E830:
2799 /* E830 can read timestamps in the top half using rd32() */
2800 ice_ptp_process_ts(pf);
2801
2802 if (ice_ptp_tx_tstamps_pending(pf)) {
2803 /* Process outstanding Tx timestamps. If there
2804 * is more work, re-arm the interrupt to trigger again.
2805 */
2806 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
2807 ice_flush(hw);
2808 }
2809 return IRQ_HANDLED;
2810 default:
2811 return IRQ_HANDLED;
2812 }
2813 }
2814
2815 /**
2816 * ice_ptp_maybe_trigger_tx_interrupt - Trigger Tx timstamp interrupt
2817 * @pf: Board private structure
2818 *
2819 * The device PHY issues Tx timestamp interrupts to the driver for processing
2820 * timestamp data from the PHY. It will not interrupt again until all
2821 * current timestamp data is read. In rare circumstances, it is possible that
2822 * the driver fails to read all outstanding data.
2823 *
2824 * To avoid getting permanently stuck, periodically check if the PHY has
2825 * outstanding timestamp data. If so, trigger an interrupt from software to
2826 * process this data.
2827 */
ice_ptp_maybe_trigger_tx_interrupt(struct ice_pf * pf)2828 static void ice_ptp_maybe_trigger_tx_interrupt(struct ice_pf *pf)
2829 {
2830 struct device *dev = ice_pf_to_dev(pf);
2831 struct ice_hw *hw = &pf->hw;
2832 int ret;
2833
2834 if (!pf->ptp.port.tx.has_ready_bitmap)
2835 return;
2836
2837 if (!ice_pf_src_tmr_owned(pf))
2838 return;
2839
2840 ret = ice_check_phy_tx_tstamp_ready(hw);
2841 if (ret < 0) {
2842 dev_dbg(dev, "PTP periodic task unable to read PHY timestamp ready bitmap, err %d\n",
2843 ret);
2844 } else if (ret) {
2845 dev_dbg(dev, "PTP periodic task detected waiting timestamps. Triggering Tx timestamp interrupt now.\n");
2846
2847 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
2848 ice_flush(hw);
2849 }
2850 }
2851
ice_ptp_periodic_work(struct kthread_work * work)2852 static void ice_ptp_periodic_work(struct kthread_work *work)
2853 {
2854 struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
2855 struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
2856 int err;
2857
2858 if (pf->ptp.state != ICE_PTP_READY)
2859 return;
2860
2861 err = ice_ptp_update_cached_phctime(pf);
2862
2863 ice_ptp_maybe_trigger_tx_interrupt(pf);
2864
2865 /* Run twice a second or reschedule if phc update failed */
2866 kthread_queue_delayed_work(ptp->kworker, &ptp->work,
2867 msecs_to_jiffies(err ? 10 : 500));
2868 }
2869
2870 /**
2871 * ice_ptp_queue_work - Queue PTP periodic work for a PF
2872 * @pf: Board private structure
2873 *
2874 * Helper function to queue PTP periodic work after VSI rebuild completes.
2875 * This ensures that PTP work only runs when VSI structures are ready.
2876 */
ice_ptp_queue_work(struct ice_pf * pf)2877 void ice_ptp_queue_work(struct ice_pf *pf)
2878 {
2879 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags) &&
2880 pf->ptp.state == ICE_PTP_READY)
2881 kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 0);
2882 }
2883
2884 /**
2885 * ice_ptp_prepare_rebuild_sec - Prepare second NAC for PTP reset or rebuild
2886 * @pf: Board private structure
2887 * @rebuild: rebuild if true, prepare if false
2888 * @reset_type: the reset type being performed
2889 */
ice_ptp_prepare_rebuild_sec(struct ice_pf * pf,bool rebuild,enum ice_reset_req reset_type)2890 static void ice_ptp_prepare_rebuild_sec(struct ice_pf *pf, bool rebuild,
2891 enum ice_reset_req reset_type)
2892 {
2893 struct list_head *entry;
2894
2895 list_for_each(entry, &pf->adapter->ports.ports) {
2896 struct ice_ptp_port *port = list_entry(entry,
2897 struct ice_ptp_port,
2898 list_node);
2899 struct ice_pf *peer_pf = ptp_port_to_pf(port);
2900
2901 if (!ice_is_primary(&peer_pf->hw)) {
2902 if (rebuild) {
2903 /* TODO: When implementing rebuild=true:
2904 * 1. Ensure secondary PFs' VSIs are rebuilt
2905 * 2. Call ice_ptp_queue_work(peer_pf) after VSI rebuild
2906 */
2907 ice_ptp_rebuild(peer_pf, reset_type);
2908 } else {
2909 ice_ptp_prepare_for_reset(peer_pf, reset_type);
2910 }
2911 }
2912 }
2913 }
2914
2915 /**
2916 * ice_ptp_prepare_for_reset - Prepare PTP for reset
2917 * @pf: Board private structure
2918 * @reset_type: the reset type being performed
2919 */
ice_ptp_prepare_for_reset(struct ice_pf * pf,enum ice_reset_req reset_type)2920 void ice_ptp_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
2921 {
2922 struct ice_ptp *ptp = &pf->ptp;
2923 struct ice_hw *hw = &pf->hw;
2924 u8 src_tmr;
2925
2926 if (ptp->state != ICE_PTP_READY)
2927 return;
2928
2929 ptp->state = ICE_PTP_RESETTING;
2930
2931 /* Disable timestamping for both Tx and Rx */
2932 ice_ptp_disable_timestamp_mode(pf);
2933
2934 kthread_cancel_delayed_work_sync(&ptp->work);
2935
2936 if (reset_type == ICE_RESET_PFR)
2937 return;
2938
2939 if (ice_pf_src_tmr_owned(pf) && hw->mac_type == ICE_MAC_GENERIC_3K_E825)
2940 ice_ptp_prepare_rebuild_sec(pf, false, reset_type);
2941
2942 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
2943
2944 /* Disable periodic outputs */
2945 ice_ptp_disable_all_perout(pf);
2946
2947 src_tmr = ice_get_ptp_src_clock_index(&pf->hw);
2948
2949 /* Disable source clock */
2950 wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M);
2951
2952 /* Acquire PHC and system timer to restore after reset */
2953 ptp->reset_time = ktime_get_real_ns();
2954 }
2955
2956 /**
2957 * ice_ptp_rebuild_owner - Initialize PTP clock owner after reset
2958 * @pf: Board private structure
2959 *
2960 * Companion function for ice_ptp_rebuild() which handles tasks that only the
2961 * PTP clock owner instance should perform.
2962 */
ice_ptp_rebuild_owner(struct ice_pf * pf)2963 static int ice_ptp_rebuild_owner(struct ice_pf *pf)
2964 {
2965 struct ice_ptp *ptp = &pf->ptp;
2966 struct ice_hw *hw = &pf->hw;
2967 struct timespec64 ts;
2968 u64 time_diff;
2969 int err;
2970
2971 err = ice_ptp_init_phc(hw);
2972 if (err)
2973 return err;
2974
2975 err = ice_tspll_init(hw);
2976 if (err)
2977 return err;
2978
2979 /* Acquire the global hardware lock */
2980 if (!ice_ptp_lock(hw)) {
2981 err = -EBUSY;
2982 return err;
2983 }
2984
2985 /* Write the increment time value to PHY and LAN */
2986 err = ice_ptp_write_incval(hw, ice_base_incval(pf));
2987 if (err)
2988 goto err_unlock;
2989
2990 /* Write the initial Time value to PHY and LAN using the cached PHC
2991 * time before the reset and time difference between stopping and
2992 * starting the clock.
2993 */
2994 if (ptp->cached_phc_time) {
2995 time_diff = ktime_get_real_ns() - ptp->reset_time;
2996 ts = ns_to_timespec64(ptp->cached_phc_time + time_diff);
2997 } else {
2998 ts = ktime_to_timespec64(ktime_get_real());
2999 }
3000 err = ice_ptp_write_init(pf, &ts);
3001 if (err)
3002 goto err_unlock;
3003
3004 /* Release the global hardware lock */
3005 ice_ptp_unlock(hw);
3006
3007 /* Flush software tracking of any outstanding timestamps since we're
3008 * about to flush the PHY timestamp block.
3009 */
3010 ice_ptp_flush_all_tx_tracker(pf);
3011
3012 /* Enable quad interrupts */
3013 err = ice_ptp_cfg_phy_interrupt(pf, true, 1);
3014 if (err)
3015 return err;
3016
3017 ice_ptp_restart_all_phy(pf);
3018
3019 /* Re-enable all periodic outputs and external timestamp events */
3020 ice_ptp_enable_all_perout(pf);
3021 ice_ptp_enable_all_extts(pf);
3022
3023 return 0;
3024
3025 err_unlock:
3026 ice_ptp_unlock(hw);
3027 return err;
3028 }
3029
3030 /**
3031 * ice_ptp_rebuild - Initialize PTP hardware clock support after reset
3032 * @pf: Board private structure
3033 * @reset_type: the reset type being performed
3034 */
ice_ptp_rebuild(struct ice_pf * pf,enum ice_reset_req reset_type)3035 void ice_ptp_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
3036 {
3037 struct ice_ptp *ptp = &pf->ptp;
3038 int err;
3039
3040 if (ptp->state == ICE_PTP_UNINIT) {
3041 dev_dbg(ice_pf_to_dev(pf), "PTP was not initialized, skipping rebuild\n");
3042 return;
3043 }
3044
3045 if (ptp->state == ICE_PTP_READY) {
3046 ice_ptp_prepare_for_reset(pf, reset_type);
3047 } else if (ptp->state != ICE_PTP_RESETTING) {
3048 err = -EINVAL;
3049 dev_err(ice_pf_to_dev(pf), "PTP was not initialized\n");
3050 goto err;
3051 }
3052
3053 if (ice_pf_src_tmr_owned(pf) && reset_type != ICE_RESET_PFR) {
3054 err = ice_ptp_rebuild_owner(pf);
3055 if (err)
3056 goto err;
3057 }
3058
3059 ptp->state = ICE_PTP_READY;
3060
3061 dev_info(ice_pf_to_dev(pf), "PTP reset successful\n");
3062 return;
3063
3064 err:
3065 ptp->state = ICE_PTP_ERROR;
3066 dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err);
3067 }
3068
ice_ptp_setup_adapter(struct ice_pf * pf)3069 static void ice_ptp_setup_adapter(struct ice_pf *pf)
3070 {
3071 pf->adapter->ctrl_pf = pf;
3072 }
3073
ice_ptp_setup_pf(struct ice_pf * pf)3074 static int ice_ptp_setup_pf(struct ice_pf *pf)
3075 {
3076 struct ice_ptp *ctrl_ptp = ice_get_ctrl_ptp(pf);
3077 struct ice_ptp *ptp = &pf->ptp;
3078
3079 if (!ctrl_ptp) {
3080 dev_info(ice_pf_to_dev(pf),
3081 "PTP unavailable: no controlling PF\n");
3082 return -EOPNOTSUPP;
3083 }
3084
3085 if (pf->hw.mac_type == ICE_MAC_UNKNOWN)
3086 return -ENODEV;
3087
3088 INIT_LIST_HEAD(&ptp->port.list_node);
3089 mutex_lock(&pf->adapter->ports.lock);
3090
3091 list_add(&ptp->port.list_node,
3092 &pf->adapter->ports.ports);
3093 mutex_unlock(&pf->adapter->ports.lock);
3094
3095 /* Seed the per-PHY Tx reference clock usage map for this port.
3096 * Only meaningful on E825 (other MAC types don't expose tx-clk
3097 * selection). No locking is needed because this runs during
3098 * ice_ptp_init() before pf->dplls.lock exists and before any
3099 * link event or DPLL callback can observe the map.
3100 */
3101 if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
3102 u8 port_num, phy;
3103
3104 port_num = ptp->port.port_num;
3105 phy = port_num / pf->hw.ptp.ports_per_phy;
3106 set_bit(port_num,
3107 &ctrl_ptp->tx_refclks[phy][pf->ptp.port.tx_clk]);
3108 }
3109
3110 return 0;
3111 }
3112
ice_ptp_cleanup_pf(struct ice_pf * pf)3113 static void ice_ptp_cleanup_pf(struct ice_pf *pf)
3114 {
3115 struct ice_ptp *ptp = &pf->ptp;
3116
3117 if (pf->hw.mac_type != ICE_MAC_UNKNOWN) {
3118 mutex_lock(&pf->adapter->ports.lock);
3119 list_del(&ptp->port.list_node);
3120 mutex_unlock(&pf->adapter->ports.lock);
3121 }
3122 }
3123
3124 /**
3125 * ice_ptp_clock_index - Get the PTP clock index for this device
3126 * @pf: Board private structure
3127 *
3128 * Returns: the PTP clock index associated with this PF, or -1 if no PTP clock
3129 * is associated.
3130 */
ice_ptp_clock_index(struct ice_pf * pf)3131 int ice_ptp_clock_index(struct ice_pf *pf)
3132 {
3133 struct ice_ptp *ctrl_ptp = ice_get_ctrl_ptp(pf);
3134 struct ptp_clock *clock;
3135
3136 if (!ctrl_ptp)
3137 return -1;
3138 clock = ctrl_ptp->clock;
3139
3140 return clock ? ptp_clock_index(clock) : -1;
3141 }
3142
3143 /**
3144 * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
3145 * @pf: Board private structure
3146 *
3147 * Setup and initialize a PTP clock device that represents the device hardware
3148 * clock. Save the clock index for other functions connected to the same
3149 * hardware resource.
3150 */
ice_ptp_init_owner(struct ice_pf * pf)3151 static int ice_ptp_init_owner(struct ice_pf *pf)
3152 {
3153 struct ice_hw *hw = &pf->hw;
3154 struct timespec64 ts;
3155 int err;
3156
3157 err = ice_ptp_init_phc(hw);
3158 if (err) {
3159 dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n",
3160 err);
3161 return err;
3162 }
3163
3164 err = ice_tspll_init(hw);
3165 if (err) {
3166 dev_err(ice_pf_to_dev(pf), "Failed to initialize CGU, status %d\n",
3167 err);
3168 return err;
3169 }
3170
3171 /* Acquire the global hardware lock */
3172 if (!ice_ptp_lock(hw)) {
3173 err = -EBUSY;
3174 goto err_exit;
3175 }
3176
3177 /* Write the increment time value to PHY and LAN */
3178 err = ice_ptp_write_incval(hw, ice_base_incval(pf));
3179 if (err)
3180 goto err_unlock;
3181
3182 ts = ktime_to_timespec64(ktime_get_real());
3183 /* Write the initial Time value to PHY and LAN */
3184 err = ice_ptp_write_init(pf, &ts);
3185 if (err)
3186 goto err_unlock;
3187
3188 /* Release the global hardware lock */
3189 ice_ptp_unlock(hw);
3190
3191 /* Configure PHY interrupt settings */
3192 err = ice_ptp_cfg_phy_interrupt(pf, true, 1);
3193 if (err)
3194 goto err_exit;
3195
3196 /* Ensure we have a clock device */
3197 err = ice_ptp_create_clock(pf);
3198 if (err)
3199 goto err_clk;
3200
3201 return 0;
3202 err_clk:
3203 pf->ptp.clock = NULL;
3204 err_exit:
3205 return err;
3206
3207 err_unlock:
3208 ice_ptp_unlock(hw);
3209 return err;
3210 }
3211
3212 /**
3213 * ice_ptp_init_work - Initialize PTP work threads
3214 * @pf: Board private structure
3215 * @ptp: PF PTP structure
3216 */
ice_ptp_init_work(struct ice_pf * pf,struct ice_ptp * ptp)3217 static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp)
3218 {
3219 struct kthread_worker *kworker;
3220
3221 /* Initialize work functions */
3222 kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work);
3223
3224 /* Allocate a kworker for handling work required for the ports
3225 * connected to the PTP hardware clock.
3226 */
3227 kworker = kthread_run_worker(0, "ice-ptp-%s",
3228 dev_name(ice_pf_to_dev(pf)));
3229 if (IS_ERR(kworker))
3230 return PTR_ERR(kworker);
3231
3232 ptp->kworker = kworker;
3233
3234 /* Start periodic work going */
3235 kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
3236
3237 return 0;
3238 }
3239
3240 /**
3241 * ice_ptp_init_port - Initialize PTP port structure
3242 * @pf: Board private structure
3243 * @ptp_port: PTP port structure
3244 *
3245 * Return: 0 on success, -ENODEV on invalid MAC type, -ENOMEM on failed alloc.
3246 */
ice_ptp_init_port(struct ice_pf * pf,struct ice_ptp_port * ptp_port)3247 static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port)
3248 {
3249 struct ice_hw *hw = &pf->hw;
3250
3251 mutex_init(&ptp_port->ps_lock);
3252
3253 switch (hw->mac_type) {
3254 case ICE_MAC_E810:
3255 case ICE_MAC_E830:
3256 case ICE_MAC_GENERIC_3K_E825:
3257 return ice_ptp_init_tx(pf, &ptp_port->tx, ptp_port->port_num);
3258 case ICE_MAC_GENERIC:
3259 kthread_init_delayed_work(&ptp_port->ov_work,
3260 ice_ptp_wait_for_offsets);
3261 return ice_ptp_init_tx_e82x(pf, &ptp_port->tx,
3262 ptp_port->port_num);
3263 default:
3264 return -ENODEV;
3265 }
3266 }
3267
3268 /**
3269 * ice_ptp_init_tx_interrupt_mode - Initialize device Tx interrupt mode
3270 * @pf: Board private structure
3271 *
3272 * Initialize the Tx timestamp interrupt mode for this device. For most device
3273 * types, each PF processes the interrupt and manages its own timestamps. For
3274 * E822-based devices, only the clock owner processes the timestamps. Other
3275 * PFs disable the interrupt and do not process their own timestamps.
3276 */
ice_ptp_init_tx_interrupt_mode(struct ice_pf * pf)3277 static void ice_ptp_init_tx_interrupt_mode(struct ice_pf *pf)
3278 {
3279 switch (pf->hw.mac_type) {
3280 case ICE_MAC_GENERIC:
3281 case ICE_MAC_GENERIC_3K_E825:
3282 /* E82x hardware has the clock owner process timestamps for
3283 * all ports.
3284 */
3285 if (ice_pf_src_tmr_owned(pf))
3286 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_ALL;
3287 else
3288 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_NONE;
3289 break;
3290 default:
3291 /* other PHY types handle their own Tx interrupt */
3292 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_SELF;
3293 }
3294 }
3295
3296 /**
3297 * ice_ptp_init - Initialize PTP hardware clock support
3298 * @pf: Board private structure
3299 *
3300 * Set up the device for interacting with the PTP hardware clock for all
3301 * functions, both the function that owns the clock hardware, and the
3302 * functions connected to the clock hardware.
3303 *
3304 * The clock owner will allocate and register a ptp_clock with the
3305 * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work
3306 * items used for asynchronous work such as Tx timestamps and periodic work.
3307 */
ice_ptp_init(struct ice_pf * pf)3308 void ice_ptp_init(struct ice_pf *pf)
3309 {
3310 struct ice_ptp *ptp = &pf->ptp;
3311 struct ice_hw *hw = &pf->hw;
3312 int err;
3313
3314 ptp->state = ICE_PTP_INITIALIZING;
3315
3316 if (hw->lane_num < 0) {
3317 err = hw->lane_num;
3318 goto err_exit;
3319 }
3320 ptp->port.port_num = hw->lane_num;
3321
3322 ice_ptp_init_hw(hw);
3323
3324 ice_ptp_init_tx_interrupt_mode(pf);
3325
3326 /* If this function owns the clock hardware, it must allocate and
3327 * configure the PTP clock device to represent it.
3328 */
3329 if (ice_pf_src_tmr_owned(pf)) {
3330 ice_ptp_setup_adapter(pf);
3331
3332 err = ice_ptp_init_owner(pf);
3333 if (err)
3334 goto err_exit;
3335 }
3336
3337 ptp->port.tx_clk = ICE_REF_CLK_ENET;
3338 ptp->port.tx_clk_req = ICE_REF_CLK_ENET;
3339 if (hw->mac_type == ICE_MAC_GENERIC_3K_E825) {
3340 enum ice_e825c_ref_clk tx_ref_clk;
3341
3342 err = ice_get_serdes_ref_sel_e825c(hw, ptp->port.port_num,
3343 &tx_ref_clk);
3344 if (!err) {
3345 ptp->port.tx_clk = tx_ref_clk;
3346 ptp->port.tx_clk_req = tx_ref_clk;
3347 }
3348 }
3349
3350 err = ice_ptp_setup_pf(pf);
3351 if (err)
3352 goto err_exit;
3353
3354 err = ice_ptp_init_port(pf, &ptp->port);
3355 if (err)
3356 goto err_clean_pf;
3357
3358 /* Start the PHY timestamping block */
3359 ice_ptp_reset_phy_timestamping(pf);
3360
3361 /* Configure initial Tx interrupt settings */
3362 ice_ptp_cfg_tx_interrupt(pf);
3363
3364 ptp->state = ICE_PTP_READY;
3365
3366 err = ice_ptp_init_work(pf, ptp);
3367 if (err)
3368 goto err_exit;
3369
3370 dev_info(ice_pf_to_dev(pf), "PTP init successful\n");
3371 return;
3372
3373 err_clean_pf:
3374 mutex_destroy(&ptp->port.ps_lock);
3375 ice_ptp_cleanup_pf(pf);
3376 err_exit:
3377 /* If we registered a PTP clock, release it */
3378 if (pf->ptp.clock) {
3379 ptp_clock_unregister(ptp->clock);
3380 pf->ptp.clock = NULL;
3381 }
3382 /* Keep ICE_PTP_UNINIT state to avoid ambiguity at driver unload
3383 * and to avoid duplicated resources release.
3384 */
3385 ptp->state = ICE_PTP_UNINIT;
3386 dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err);
3387 }
3388
3389 /**
3390 * ice_ptp_release - Disable the driver/HW support and unregister the clock
3391 * @pf: Board private structure
3392 *
3393 * This function handles the cleanup work required from the initialization by
3394 * clearing out the important information and unregistering the clock
3395 */
ice_ptp_release(struct ice_pf * pf)3396 void ice_ptp_release(struct ice_pf *pf)
3397 {
3398 if (pf->ptp.state == ICE_PTP_UNINIT)
3399 return;
3400
3401 if (pf->ptp.state != ICE_PTP_READY) {
3402 mutex_destroy(&pf->ptp.port.ps_lock);
3403 ice_ptp_cleanup_pf(pf);
3404 if (pf->ptp.clock) {
3405 ptp_clock_unregister(pf->ptp.clock);
3406 pf->ptp.clock = NULL;
3407 }
3408 return;
3409 }
3410
3411 pf->ptp.state = ICE_PTP_UNINIT;
3412
3413 /* Disable timestamping for both Tx and Rx */
3414 ice_ptp_disable_timestamp_mode(pf);
3415
3416 ice_ptp_cleanup_pf(pf);
3417
3418 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
3419
3420 ice_ptp_disable_all_extts(pf);
3421
3422 kthread_cancel_delayed_work_sync(&pf->ptp.work);
3423
3424 ice_ptp_port_phy_stop(&pf->ptp.port);
3425 mutex_destroy(&pf->ptp.port.ps_lock);
3426 if (pf->ptp.kworker) {
3427 kthread_destroy_worker(pf->ptp.kworker);
3428 pf->ptp.kworker = NULL;
3429 }
3430
3431 if (!pf->ptp.clock)
3432 return;
3433
3434 /* Disable periodic outputs */
3435 ice_ptp_disable_all_perout(pf);
3436
3437 ptp_clock_unregister(pf->ptp.clock);
3438 pf->ptp.clock = NULL;
3439
3440 dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");
3441 }
3442