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