1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Intel Corporation */ 3 4 #include "igc.h" 5 6 #include <linux/module.h> 7 #include <linux/device.h> 8 #include <linux/pci.h> 9 #include <linux/ptp_classify.h> 10 #include <linux/clocksource.h> 11 #include <linux/ktime.h> 12 13 #define INCVALUE_MASK 0x7fffffff 14 #define ISGN 0x80000000 15 16 #define IGC_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9) 17 #define IGC_PTP_TX_TIMEOUT (HZ * 15) 18 19 /* SYSTIM read access for I225 */ 20 void igc_ptp_read(struct igc_adapter *adapter, struct timespec64 *ts) 21 { 22 struct igc_hw *hw = &adapter->hw; 23 u32 sec, nsec; 24 25 /* The timestamp is latched when SYSTIML is read. */ 26 nsec = rd32(IGC_SYSTIML); 27 sec = rd32(IGC_SYSTIMH); 28 29 ts->tv_sec = sec; 30 ts->tv_nsec = nsec; 31 } 32 33 static void igc_ptp_write_i225(struct igc_adapter *adapter, 34 const struct timespec64 *ts) 35 { 36 struct igc_hw *hw = &adapter->hw; 37 38 wr32(IGC_SYSTIML, ts->tv_nsec); 39 wr32(IGC_SYSTIMH, ts->tv_sec); 40 } 41 42 static int igc_ptp_adjfine_i225(struct ptp_clock_info *ptp, long scaled_ppm) 43 { 44 struct igc_adapter *igc = container_of(ptp, struct igc_adapter, 45 ptp_caps); 46 struct igc_hw *hw = &igc->hw; 47 int neg_adj = 0; 48 u64 rate; 49 u32 inca; 50 51 if (scaled_ppm < 0) { 52 neg_adj = 1; 53 scaled_ppm = -scaled_ppm; 54 } 55 rate = scaled_ppm; 56 rate <<= 14; 57 rate = div_u64(rate, 78125); 58 59 inca = rate & INCVALUE_MASK; 60 if (neg_adj) 61 inca |= ISGN; 62 63 wr32(IGC_TIMINCA, inca); 64 65 return 0; 66 } 67 68 static int igc_ptp_adjtime_i225(struct ptp_clock_info *ptp, s64 delta) 69 { 70 struct igc_adapter *igc = container_of(ptp, struct igc_adapter, 71 ptp_caps); 72 struct timespec64 now, then = ns_to_timespec64(delta); 73 unsigned long flags; 74 75 spin_lock_irqsave(&igc->tmreg_lock, flags); 76 77 igc_ptp_read(igc, &now); 78 now = timespec64_add(now, then); 79 igc_ptp_write_i225(igc, (const struct timespec64 *)&now); 80 81 spin_unlock_irqrestore(&igc->tmreg_lock, flags); 82 83 return 0; 84 } 85 86 static int igc_ptp_gettimex64_i225(struct ptp_clock_info *ptp, 87 struct timespec64 *ts, 88 struct ptp_system_timestamp *sts) 89 { 90 struct igc_adapter *igc = container_of(ptp, struct igc_adapter, 91 ptp_caps); 92 struct igc_hw *hw = &igc->hw; 93 unsigned long flags; 94 95 spin_lock_irqsave(&igc->tmreg_lock, flags); 96 97 ptp_read_system_prets(sts); 98 ts->tv_nsec = rd32(IGC_SYSTIML); 99 ts->tv_sec = rd32(IGC_SYSTIMH); 100 ptp_read_system_postts(sts); 101 102 spin_unlock_irqrestore(&igc->tmreg_lock, flags); 103 104 return 0; 105 } 106 107 static int igc_ptp_settime_i225(struct ptp_clock_info *ptp, 108 const struct timespec64 *ts) 109 { 110 struct igc_adapter *igc = container_of(ptp, struct igc_adapter, 111 ptp_caps); 112 unsigned long flags; 113 114 spin_lock_irqsave(&igc->tmreg_lock, flags); 115 116 igc_ptp_write_i225(igc, ts); 117 118 spin_unlock_irqrestore(&igc->tmreg_lock, flags); 119 120 return 0; 121 } 122 123 static int igc_ptp_feature_enable_i225(struct ptp_clock_info *ptp, 124 struct ptp_clock_request *rq, int on) 125 { 126 return -EOPNOTSUPP; 127 } 128 129 /** 130 * igc_ptp_systim_to_hwtstamp - convert system time value to HW timestamp 131 * @adapter: board private structure 132 * @hwtstamps: timestamp structure to update 133 * @systim: unsigned 64bit system time value 134 * 135 * We need to convert the system time value stored in the RX/TXSTMP registers 136 * into a hwtstamp which can be used by the upper level timestamping functions. 137 **/ 138 static void igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter, 139 struct skb_shared_hwtstamps *hwtstamps, 140 u64 systim) 141 { 142 switch (adapter->hw.mac.type) { 143 case igc_i225: 144 memset(hwtstamps, 0, sizeof(*hwtstamps)); 145 /* Upper 32 bits contain s, lower 32 bits contain ns. */ 146 hwtstamps->hwtstamp = ktime_set(systim >> 32, 147 systim & 0xFFFFFFFF); 148 break; 149 default: 150 break; 151 } 152 } 153 154 /** 155 * igc_ptp_rx_pktstamp - Retrieve timestamp from Rx packet buffer 156 * @adapter: Pointer to adapter the packet buffer belongs to 157 * @buf: Pointer to packet buffer 158 * 159 * This function retrieves the timestamp saved in the beginning of packet 160 * buffer. While two timestamps are available, one in timer0 reference and the 161 * other in timer1 reference, this function considers only the timestamp in 162 * timer0 reference. 163 * 164 * Returns timestamp value. 165 */ 166 ktime_t igc_ptp_rx_pktstamp(struct igc_adapter *adapter, __le32 *buf) 167 { 168 ktime_t timestamp; 169 u32 secs, nsecs; 170 int adjust; 171 172 /* Timestamps are saved in little endian at the beginning of the packet 173 * buffer following the layout: 174 * 175 * DWORD: | 0 | 1 | 2 | 3 | 176 * Field: | Timer1 SYSTIML | Timer1 SYSTIMH | Timer0 SYSTIML | Timer0 SYSTIMH | 177 * 178 * SYSTIML holds the nanoseconds part while SYSTIMH holds the seconds 179 * part of the timestamp. 180 */ 181 nsecs = le32_to_cpu(buf[2]); 182 secs = le32_to_cpu(buf[3]); 183 184 timestamp = ktime_set(secs, nsecs); 185 186 /* Adjust timestamp for the RX latency based on link speed */ 187 switch (adapter->link_speed) { 188 case SPEED_10: 189 adjust = IGC_I225_RX_LATENCY_10; 190 break; 191 case SPEED_100: 192 adjust = IGC_I225_RX_LATENCY_100; 193 break; 194 case SPEED_1000: 195 adjust = IGC_I225_RX_LATENCY_1000; 196 break; 197 case SPEED_2500: 198 adjust = IGC_I225_RX_LATENCY_2500; 199 break; 200 default: 201 adjust = 0; 202 netdev_warn_once(adapter->netdev, "Imprecise timestamp\n"); 203 break; 204 } 205 206 return ktime_sub_ns(timestamp, adjust); 207 } 208 209 static void igc_ptp_disable_rx_timestamp(struct igc_adapter *adapter) 210 { 211 struct igc_hw *hw = &adapter->hw; 212 u32 val; 213 int i; 214 215 wr32(IGC_TSYNCRXCTL, 0); 216 217 for (i = 0; i < adapter->num_rx_queues; i++) { 218 val = rd32(IGC_SRRCTL(i)); 219 val &= ~IGC_SRRCTL_TIMESTAMP; 220 wr32(IGC_SRRCTL(i), val); 221 } 222 223 val = rd32(IGC_RXPBS); 224 val &= ~IGC_RXPBS_CFG_TS_EN; 225 wr32(IGC_RXPBS, val); 226 } 227 228 static void igc_ptp_enable_rx_timestamp(struct igc_adapter *adapter) 229 { 230 struct igc_hw *hw = &adapter->hw; 231 u32 val; 232 int i; 233 234 val = rd32(IGC_RXPBS); 235 val |= IGC_RXPBS_CFG_TS_EN; 236 wr32(IGC_RXPBS, val); 237 238 for (i = 0; i < adapter->num_rx_queues; i++) { 239 val = rd32(IGC_SRRCTL(i)); 240 /* FIXME: For now, only support retrieving RX timestamps from 241 * timer 0. 242 */ 243 val |= IGC_SRRCTL_TIMER1SEL(0) | IGC_SRRCTL_TIMER0SEL(0) | 244 IGC_SRRCTL_TIMESTAMP; 245 wr32(IGC_SRRCTL(i), val); 246 } 247 248 val = IGC_TSYNCRXCTL_ENABLED | IGC_TSYNCRXCTL_TYPE_ALL | 249 IGC_TSYNCRXCTL_RXSYNSIG; 250 wr32(IGC_TSYNCRXCTL, val); 251 } 252 253 static void igc_ptp_disable_tx_timestamp(struct igc_adapter *adapter) 254 { 255 struct igc_hw *hw = &adapter->hw; 256 257 wr32(IGC_TSYNCTXCTL, 0); 258 } 259 260 static void igc_ptp_enable_tx_timestamp(struct igc_adapter *adapter) 261 { 262 struct igc_hw *hw = &adapter->hw; 263 264 wr32(IGC_TSYNCTXCTL, IGC_TSYNCTXCTL_ENABLED | IGC_TSYNCTXCTL_TXSYNSIG); 265 266 /* Read TXSTMP registers to discard any timestamp previously stored. */ 267 rd32(IGC_TXSTMPL); 268 rd32(IGC_TXSTMPH); 269 } 270 271 /** 272 * igc_ptp_set_timestamp_mode - setup hardware for timestamping 273 * @adapter: networking device structure 274 * @config: hwtstamp configuration 275 * 276 * Return: 0 in case of success, negative errno code otherwise. 277 */ 278 static int igc_ptp_set_timestamp_mode(struct igc_adapter *adapter, 279 struct hwtstamp_config *config) 280 { 281 /* reserved for future extensions */ 282 if (config->flags) 283 return -EINVAL; 284 285 switch (config->tx_type) { 286 case HWTSTAMP_TX_OFF: 287 igc_ptp_disable_tx_timestamp(adapter); 288 break; 289 case HWTSTAMP_TX_ON: 290 igc_ptp_enable_tx_timestamp(adapter); 291 break; 292 default: 293 return -ERANGE; 294 } 295 296 switch (config->rx_filter) { 297 case HWTSTAMP_FILTER_NONE: 298 igc_ptp_disable_rx_timestamp(adapter); 299 break; 300 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 301 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 302 case HWTSTAMP_FILTER_PTP_V2_EVENT: 303 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 304 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 305 case HWTSTAMP_FILTER_PTP_V2_SYNC: 306 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 307 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 308 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 309 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 310 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 311 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 312 case HWTSTAMP_FILTER_NTP_ALL: 313 case HWTSTAMP_FILTER_ALL: 314 igc_ptp_enable_rx_timestamp(adapter); 315 config->rx_filter = HWTSTAMP_FILTER_ALL; 316 break; 317 default: 318 return -ERANGE; 319 } 320 321 return 0; 322 } 323 324 static void igc_ptp_tx_timeout(struct igc_adapter *adapter) 325 { 326 struct igc_hw *hw = &adapter->hw; 327 328 dev_kfree_skb_any(adapter->ptp_tx_skb); 329 adapter->ptp_tx_skb = NULL; 330 adapter->tx_hwtstamp_timeouts++; 331 clear_bit_unlock(__IGC_PTP_TX_IN_PROGRESS, &adapter->state); 332 /* Clear the tx valid bit in TSYNCTXCTL register to enable interrupt. */ 333 rd32(IGC_TXSTMPH); 334 netdev_warn(adapter->netdev, "Tx timestamp timeout\n"); 335 } 336 337 void igc_ptp_tx_hang(struct igc_adapter *adapter) 338 { 339 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start + 340 IGC_PTP_TX_TIMEOUT); 341 342 if (!test_bit(__IGC_PTP_TX_IN_PROGRESS, &adapter->state)) 343 return; 344 345 /* If we haven't received a timestamp within the timeout, it is 346 * reasonable to assume that it will never occur, so we can unlock the 347 * timestamp bit when this occurs. 348 */ 349 if (timeout) { 350 cancel_work_sync(&adapter->ptp_tx_work); 351 igc_ptp_tx_timeout(adapter); 352 } 353 } 354 355 /** 356 * igc_ptp_tx_hwtstamp - utility function which checks for TX time stamp 357 * @adapter: Board private structure 358 * 359 * If we were asked to do hardware stamping and such a time stamp is 360 * available, then it must have been for this skb here because we only 361 * allow only one such packet into the queue. 362 */ 363 static void igc_ptp_tx_hwtstamp(struct igc_adapter *adapter) 364 { 365 struct sk_buff *skb = adapter->ptp_tx_skb; 366 struct skb_shared_hwtstamps shhwtstamps; 367 struct igc_hw *hw = &adapter->hw; 368 int adjust = 0; 369 u64 regval; 370 371 if (WARN_ON_ONCE(!skb)) 372 return; 373 374 regval = rd32(IGC_TXSTMPL); 375 regval |= (u64)rd32(IGC_TXSTMPH) << 32; 376 igc_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval); 377 378 switch (adapter->link_speed) { 379 case SPEED_10: 380 adjust = IGC_I225_TX_LATENCY_10; 381 break; 382 case SPEED_100: 383 adjust = IGC_I225_TX_LATENCY_100; 384 break; 385 case SPEED_1000: 386 adjust = IGC_I225_TX_LATENCY_1000; 387 break; 388 case SPEED_2500: 389 adjust = IGC_I225_TX_LATENCY_2500; 390 break; 391 } 392 393 shhwtstamps.hwtstamp = 394 ktime_add_ns(shhwtstamps.hwtstamp, adjust); 395 396 /* Clear the lock early before calling skb_tstamp_tx so that 397 * applications are not woken up before the lock bit is clear. We use 398 * a copy of the skb pointer to ensure other threads can't change it 399 * while we're notifying the stack. 400 */ 401 adapter->ptp_tx_skb = NULL; 402 clear_bit_unlock(__IGC_PTP_TX_IN_PROGRESS, &adapter->state); 403 404 /* Notify the stack and free the skb after we've unlocked */ 405 skb_tstamp_tx(skb, &shhwtstamps); 406 dev_kfree_skb_any(skb); 407 } 408 409 /** 410 * igc_ptp_tx_work 411 * @work: pointer to work struct 412 * 413 * This work function polls the TSYNCTXCTL valid bit to determine when a 414 * timestamp has been taken for the current stored skb. 415 */ 416 static void igc_ptp_tx_work(struct work_struct *work) 417 { 418 struct igc_adapter *adapter = container_of(work, struct igc_adapter, 419 ptp_tx_work); 420 struct igc_hw *hw = &adapter->hw; 421 u32 tsynctxctl; 422 423 if (!test_bit(__IGC_PTP_TX_IN_PROGRESS, &adapter->state)) 424 return; 425 426 tsynctxctl = rd32(IGC_TSYNCTXCTL); 427 if (WARN_ON_ONCE(!(tsynctxctl & IGC_TSYNCTXCTL_TXTT_0))) 428 return; 429 430 igc_ptp_tx_hwtstamp(adapter); 431 } 432 433 /** 434 * igc_ptp_set_ts_config - set hardware time stamping config 435 * @netdev: network interface device structure 436 * @ifr: interface request data 437 * 438 **/ 439 int igc_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr) 440 { 441 struct igc_adapter *adapter = netdev_priv(netdev); 442 struct hwtstamp_config config; 443 int err; 444 445 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 446 return -EFAULT; 447 448 err = igc_ptp_set_timestamp_mode(adapter, &config); 449 if (err) 450 return err; 451 452 /* save these settings for future reference */ 453 memcpy(&adapter->tstamp_config, &config, 454 sizeof(adapter->tstamp_config)); 455 456 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 457 -EFAULT : 0; 458 } 459 460 /** 461 * igc_ptp_get_ts_config - get hardware time stamping config 462 * @netdev: network interface device structure 463 * @ifr: interface request data 464 * 465 * Get the hwtstamp_config settings to return to the user. Rather than attempt 466 * to deconstruct the settings from the registers, just return a shadow copy 467 * of the last known settings. 468 **/ 469 int igc_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr) 470 { 471 struct igc_adapter *adapter = netdev_priv(netdev); 472 struct hwtstamp_config *config = &adapter->tstamp_config; 473 474 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 475 -EFAULT : 0; 476 } 477 478 /** 479 * igc_ptp_init - Initialize PTP functionality 480 * @adapter: Board private structure 481 * 482 * This function is called at device probe to initialize the PTP 483 * functionality. 484 */ 485 void igc_ptp_init(struct igc_adapter *adapter) 486 { 487 struct net_device *netdev = adapter->netdev; 488 struct igc_hw *hw = &adapter->hw; 489 490 switch (hw->mac.type) { 491 case igc_i225: 492 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 493 adapter->ptp_caps.owner = THIS_MODULE; 494 adapter->ptp_caps.max_adj = 62499999; 495 adapter->ptp_caps.adjfine = igc_ptp_adjfine_i225; 496 adapter->ptp_caps.adjtime = igc_ptp_adjtime_i225; 497 adapter->ptp_caps.gettimex64 = igc_ptp_gettimex64_i225; 498 adapter->ptp_caps.settime64 = igc_ptp_settime_i225; 499 adapter->ptp_caps.enable = igc_ptp_feature_enable_i225; 500 break; 501 default: 502 adapter->ptp_clock = NULL; 503 return; 504 } 505 506 spin_lock_init(&adapter->tmreg_lock); 507 INIT_WORK(&adapter->ptp_tx_work, igc_ptp_tx_work); 508 509 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 510 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF; 511 512 adapter->prev_ptp_time = ktime_to_timespec64(ktime_get_real()); 513 adapter->ptp_reset_start = ktime_get(); 514 515 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps, 516 &adapter->pdev->dev); 517 if (IS_ERR(adapter->ptp_clock)) { 518 adapter->ptp_clock = NULL; 519 netdev_err(netdev, "ptp_clock_register failed\n"); 520 } else if (adapter->ptp_clock) { 521 netdev_info(netdev, "PHC added\n"); 522 adapter->ptp_flags |= IGC_PTP_ENABLED; 523 } 524 } 525 526 static void igc_ptp_time_save(struct igc_adapter *adapter) 527 { 528 igc_ptp_read(adapter, &adapter->prev_ptp_time); 529 adapter->ptp_reset_start = ktime_get(); 530 } 531 532 static void igc_ptp_time_restore(struct igc_adapter *adapter) 533 { 534 struct timespec64 ts = adapter->prev_ptp_time; 535 ktime_t delta; 536 537 delta = ktime_sub(ktime_get(), adapter->ptp_reset_start); 538 539 timespec64_add_ns(&ts, ktime_to_ns(delta)); 540 541 igc_ptp_write_i225(adapter, &ts); 542 } 543 544 /** 545 * igc_ptp_suspend - Disable PTP work items and prepare for suspend 546 * @adapter: Board private structure 547 * 548 * This function stops the overflow check work and PTP Tx timestamp work, and 549 * will prepare the device for OS suspend. 550 */ 551 void igc_ptp_suspend(struct igc_adapter *adapter) 552 { 553 if (!(adapter->ptp_flags & IGC_PTP_ENABLED)) 554 return; 555 556 cancel_work_sync(&adapter->ptp_tx_work); 557 dev_kfree_skb_any(adapter->ptp_tx_skb); 558 adapter->ptp_tx_skb = NULL; 559 clear_bit_unlock(__IGC_PTP_TX_IN_PROGRESS, &adapter->state); 560 561 igc_ptp_time_save(adapter); 562 } 563 564 /** 565 * igc_ptp_stop - Disable PTP device and stop the overflow check. 566 * @adapter: Board private structure. 567 * 568 * This function stops the PTP support and cancels the delayed work. 569 **/ 570 void igc_ptp_stop(struct igc_adapter *adapter) 571 { 572 igc_ptp_suspend(adapter); 573 574 if (adapter->ptp_clock) { 575 ptp_clock_unregister(adapter->ptp_clock); 576 netdev_info(adapter->netdev, "PHC removed\n"); 577 adapter->ptp_flags &= ~IGC_PTP_ENABLED; 578 } 579 } 580 581 /** 582 * igc_ptp_reset - Re-enable the adapter for PTP following a reset. 583 * @adapter: Board private structure. 584 * 585 * This function handles the reset work required to re-enable the PTP device. 586 **/ 587 void igc_ptp_reset(struct igc_adapter *adapter) 588 { 589 struct igc_hw *hw = &adapter->hw; 590 unsigned long flags; 591 592 /* reset the tstamp_config */ 593 igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config); 594 595 spin_lock_irqsave(&adapter->tmreg_lock, flags); 596 597 switch (adapter->hw.mac.type) { 598 case igc_i225: 599 wr32(IGC_TSAUXC, 0x0); 600 wr32(IGC_TSSDP, 0x0); 601 wr32(IGC_TSIM, IGC_TSICR_INTERRUPTS); 602 wr32(IGC_IMS, IGC_IMS_TS); 603 break; 604 default: 605 /* No work to do. */ 606 goto out; 607 } 608 609 /* Re-initialize the timer. */ 610 if (hw->mac.type == igc_i225) { 611 igc_ptp_time_restore(adapter); 612 } else { 613 timecounter_init(&adapter->tc, &adapter->cc, 614 ktime_to_ns(ktime_get_real())); 615 } 616 out: 617 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 618 619 wrfl(); 620 } 621