1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2024 Intel Corporation */ 3 4 #include "idpf.h" 5 #include "idpf_ptp.h" 6 7 /** 8 * idpf_ptp_get_access - Determine the access type of the PTP features 9 * @adapter: Driver specific private structure 10 * @direct: Capability that indicates the direct access 11 * @mailbox: Capability that indicates the mailbox access 12 * 13 * Return: the type of supported access for the PTP feature. 14 */ 15 static enum idpf_ptp_access 16 idpf_ptp_get_access(const struct idpf_adapter *adapter, u32 direct, u32 mailbox) 17 { 18 if (adapter->ptp->caps & direct) 19 return IDPF_PTP_DIRECT; 20 else if (adapter->ptp->caps & mailbox) 21 return IDPF_PTP_MAILBOX; 22 else 23 return IDPF_PTP_NONE; 24 } 25 26 /** 27 * idpf_ptp_get_features_access - Determine the access type of PTP features 28 * @adapter: Driver specific private structure 29 * 30 * Fulfill the adapter structure with type of the supported PTP features 31 * access. 32 */ 33 void idpf_ptp_get_features_access(const struct idpf_adapter *adapter) 34 { 35 struct idpf_ptp *ptp = adapter->ptp; 36 u32 direct, mailbox; 37 38 /* Get the device clock time */ 39 direct = VIRTCHNL2_CAP_PTP_GET_DEVICE_CLK_TIME; 40 mailbox = VIRTCHNL2_CAP_PTP_GET_DEVICE_CLK_TIME_MB; 41 ptp->get_dev_clk_time_access = idpf_ptp_get_access(adapter, 42 direct, 43 mailbox); 44 45 /* Get the cross timestamp */ 46 direct = VIRTCHNL2_CAP_PTP_GET_CROSS_TIME; 47 mailbox = VIRTCHNL2_CAP_PTP_GET_CROSS_TIME_MB; 48 ptp->get_cross_tstamp_access = idpf_ptp_get_access(adapter, 49 direct, 50 mailbox); 51 52 /* Set the device clock time */ 53 direct = VIRTCHNL2_CAP_PTP_SET_DEVICE_CLK_TIME; 54 mailbox = VIRTCHNL2_CAP_PTP_SET_DEVICE_CLK_TIME; 55 ptp->set_dev_clk_time_access = idpf_ptp_get_access(adapter, 56 direct, 57 mailbox); 58 59 /* Adjust the device clock time */ 60 direct = VIRTCHNL2_CAP_PTP_ADJ_DEVICE_CLK; 61 mailbox = VIRTCHNL2_CAP_PTP_ADJ_DEVICE_CLK_MB; 62 ptp->adj_dev_clk_time_access = idpf_ptp_get_access(adapter, 63 direct, 64 mailbox); 65 66 /* Tx timestamping */ 67 direct = VIRTCHNL2_CAP_PTP_TX_TSTAMPS; 68 mailbox = VIRTCHNL2_CAP_PTP_TX_TSTAMPS_MB; 69 ptp->tx_tstamp_access = idpf_ptp_get_access(adapter, 70 direct, 71 mailbox); 72 } 73 74 /** 75 * idpf_ptp_enable_shtime - Enable shadow time and execute a command 76 * @adapter: Driver specific private structure 77 */ 78 static void idpf_ptp_enable_shtime(struct idpf_adapter *adapter) 79 { 80 u32 shtime_enable, exec_cmd; 81 82 /* Get offsets */ 83 shtime_enable = adapter->ptp->cmd.shtime_enable_mask; 84 exec_cmd = adapter->ptp->cmd.exec_cmd_mask; 85 86 /* Set the shtime en and the sync field */ 87 writel(shtime_enable, adapter->ptp->dev_clk_regs.cmd_sync); 88 writel(exec_cmd | shtime_enable, adapter->ptp->dev_clk_regs.cmd_sync); 89 } 90 91 /** 92 * idpf_ptp_read_src_clk_reg_direct - Read directly the main timer value 93 * @adapter: Driver specific private structure 94 * @sts: Optional parameter for holding a pair of system timestamps from 95 * the system clock. Will be ignored when NULL is given. 96 * 97 * Return: the device clock time. 98 */ 99 static u64 idpf_ptp_read_src_clk_reg_direct(struct idpf_adapter *adapter, 100 struct ptp_system_timestamp *sts) 101 { 102 struct idpf_ptp *ptp = adapter->ptp; 103 u32 hi, lo; 104 105 spin_lock(&ptp->read_dev_clk_lock); 106 107 /* Read the system timestamp pre PHC read */ 108 ptp_read_system_prets(sts); 109 110 idpf_ptp_enable_shtime(adapter); 111 lo = readl(ptp->dev_clk_regs.dev_clk_ns_l); 112 113 /* Read the system timestamp post PHC read */ 114 ptp_read_system_postts(sts); 115 116 hi = readl(ptp->dev_clk_regs.dev_clk_ns_h); 117 118 spin_unlock(&ptp->read_dev_clk_lock); 119 120 return ((u64)hi << 32) | lo; 121 } 122 123 /** 124 * idpf_ptp_read_src_clk_reg_mailbox - Read the main timer value through mailbox 125 * @adapter: Driver specific private structure 126 * @sts: Optional parameter for holding a pair of system timestamps from 127 * the system clock. Will be ignored when NULL is given. 128 * @src_clk: Returned main timer value in nanoseconds unit 129 * 130 * Return: 0 on success, -errno otherwise. 131 */ 132 static int idpf_ptp_read_src_clk_reg_mailbox(struct idpf_adapter *adapter, 133 struct ptp_system_timestamp *sts, 134 u64 *src_clk) 135 { 136 struct idpf_ptp_dev_timers clk_time; 137 int err; 138 139 /* Read the system timestamp pre PHC read */ 140 ptp_read_system_prets(sts); 141 142 err = idpf_ptp_get_dev_clk_time(adapter, &clk_time); 143 if (err) 144 return err; 145 146 /* Read the system timestamp post PHC read */ 147 ptp_read_system_postts(sts); 148 149 *src_clk = clk_time.dev_clk_time_ns; 150 151 return 0; 152 } 153 154 /** 155 * idpf_ptp_read_src_clk_reg - Read the main timer value 156 * @adapter: Driver specific private structure 157 * @src_clk: Returned main timer value in nanoseconds unit 158 * @sts: Optional parameter for holding a pair of system timestamps from 159 * the system clock. Will be ignored if NULL is given. 160 * 161 * Return: the device clock time on success, -errno otherwise. 162 */ 163 static int idpf_ptp_read_src_clk_reg(struct idpf_adapter *adapter, u64 *src_clk, 164 struct ptp_system_timestamp *sts) 165 { 166 switch (adapter->ptp->get_dev_clk_time_access) { 167 case IDPF_PTP_NONE: 168 return -EOPNOTSUPP; 169 case IDPF_PTP_MAILBOX: 170 return idpf_ptp_read_src_clk_reg_mailbox(adapter, sts, src_clk); 171 case IDPF_PTP_DIRECT: 172 *src_clk = idpf_ptp_read_src_clk_reg_direct(adapter, sts); 173 break; 174 default: 175 return -EOPNOTSUPP; 176 } 177 178 return 0; 179 } 180 181 #if IS_ENABLED(CONFIG_ARM_ARCH_TIMER) || IS_ENABLED(CONFIG_X86) 182 /** 183 * idpf_ptp_get_sync_device_time_direct - Get the cross time stamp values 184 * directly 185 * @adapter: Driver specific private structure 186 * @dev_time: 64bit main timer value 187 * @sys_time: 64bit system time value 188 */ 189 static void idpf_ptp_get_sync_device_time_direct(struct idpf_adapter *adapter, 190 u64 *dev_time, u64 *sys_time) 191 { 192 u32 dev_time_lo, dev_time_hi, sys_time_lo, sys_time_hi; 193 struct idpf_ptp *ptp = adapter->ptp; 194 195 spin_lock(&ptp->read_dev_clk_lock); 196 197 idpf_ptp_enable_shtime(adapter); 198 199 dev_time_lo = readl(ptp->dev_clk_regs.dev_clk_ns_l); 200 dev_time_hi = readl(ptp->dev_clk_regs.dev_clk_ns_h); 201 202 sys_time_lo = readl(ptp->dev_clk_regs.sys_time_ns_l); 203 sys_time_hi = readl(ptp->dev_clk_regs.sys_time_ns_h); 204 205 spin_unlock(&ptp->read_dev_clk_lock); 206 207 *dev_time = (u64)dev_time_hi << 32 | dev_time_lo; 208 *sys_time = (u64)sys_time_hi << 32 | sys_time_lo; 209 } 210 211 /** 212 * idpf_ptp_get_sync_device_time_mailbox - Get the cross time stamp values 213 * through mailbox 214 * @adapter: Driver specific private structure 215 * @dev_time: 64bit main timer value expressed in nanoseconds 216 * @sys_time: 64bit system time value expressed in nanoseconds 217 * 218 * Return: 0 on success, -errno otherwise. 219 */ 220 static int idpf_ptp_get_sync_device_time_mailbox(struct idpf_adapter *adapter, 221 u64 *dev_time, u64 *sys_time) 222 { 223 struct idpf_ptp_dev_timers cross_time; 224 int err; 225 226 err = idpf_ptp_get_cross_time(adapter, &cross_time); 227 if (err) 228 return err; 229 230 *dev_time = cross_time.dev_clk_time_ns; 231 *sys_time = cross_time.sys_time_ns; 232 233 return err; 234 } 235 236 /** 237 * idpf_ptp_get_sync_device_time - Get the cross time stamp info 238 * @device: Current device time 239 * @system: System counter value read synchronously with device time 240 * @ctx: Context provided by timekeeping code 241 * 242 * The device and the system clocks time read simultaneously. 243 * 244 * Return: 0 on success, -errno otherwise. 245 */ 246 static int idpf_ptp_get_sync_device_time(ktime_t *device, 247 struct system_counterval_t *system, 248 void *ctx) 249 { 250 struct idpf_adapter *adapter = ctx; 251 u64 ns_time_dev, ns_time_sys; 252 int err; 253 254 switch (adapter->ptp->get_cross_tstamp_access) { 255 case IDPF_PTP_NONE: 256 return -EOPNOTSUPP; 257 case IDPF_PTP_DIRECT: 258 idpf_ptp_get_sync_device_time_direct(adapter, &ns_time_dev, 259 &ns_time_sys); 260 break; 261 case IDPF_PTP_MAILBOX: 262 err = idpf_ptp_get_sync_device_time_mailbox(adapter, 263 &ns_time_dev, 264 &ns_time_sys); 265 if (err) 266 return err; 267 break; 268 default: 269 return -EOPNOTSUPP; 270 } 271 272 *device = ns_to_ktime(ns_time_dev); 273 274 system->cs_id = IS_ENABLED(CONFIG_X86) ? CSID_X86_ART 275 : CSID_ARM_ARCH_COUNTER; 276 system->cycles = ns_time_sys; 277 system->use_nsecs = true; 278 279 return 0; 280 } 281 282 /** 283 * idpf_ptp_get_crosststamp - Capture a device cross timestamp 284 * @info: the driver's PTP info structure 285 * @cts: The memory to fill the cross timestamp info 286 * 287 * Capture a cross timestamp between the system time and the device PTP hardware 288 * clock. 289 * 290 * Return: cross timestamp value on success, -errno on failure. 291 */ 292 static int idpf_ptp_get_crosststamp(struct ptp_clock_info *info, 293 struct system_device_crosststamp *cts) 294 { 295 struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info); 296 297 return get_device_system_crosststamp(idpf_ptp_get_sync_device_time, 298 adapter, NULL, cts); 299 } 300 #endif /* CONFIG_ARM_ARCH_TIMER || CONFIG_X86 */ 301 302 /** 303 * idpf_ptp_gettimex64 - Get the time of the clock 304 * @info: the driver's PTP info structure 305 * @ts: timespec64 structure to hold the current time value 306 * @sts: Optional parameter for holding a pair of system timestamps from 307 * the system clock. Will be ignored if NULL is given. 308 * 309 * Return: the device clock value in ns, after converting it into a timespec 310 * struct on success, -errno otherwise. 311 */ 312 static int idpf_ptp_gettimex64(struct ptp_clock_info *info, 313 struct timespec64 *ts, 314 struct ptp_system_timestamp *sts) 315 { 316 struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info); 317 u64 time_ns; 318 int err; 319 320 err = idpf_ptp_read_src_clk_reg(adapter, &time_ns, sts); 321 if (err) 322 return -EACCES; 323 324 *ts = ns_to_timespec64(time_ns); 325 326 return 0; 327 } 328 329 /** 330 * idpf_ptp_update_phctime_rxq_grp - Update the cached PHC time for a given Rx 331 * queue group. 332 * @grp: receive queue group in which Rx timestamp is enabled 333 * @split: Indicates whether the queue model is split or single queue 334 * @systime: Cached system time 335 */ 336 static void 337 idpf_ptp_update_phctime_rxq_grp(const struct idpf_rxq_group *grp, bool split, 338 u64 systime) 339 { 340 struct idpf_rx_queue *rxq; 341 u16 i; 342 343 if (!split) { 344 for (i = 0; i < grp->singleq.num_rxq; i++) { 345 rxq = grp->singleq.rxqs[i]; 346 if (rxq) 347 WRITE_ONCE(rxq->cached_phc_time, systime); 348 } 349 } else { 350 for (i = 0; i < grp->splitq.num_rxq_sets; i++) { 351 rxq = &grp->splitq.rxq_sets[i]->rxq; 352 if (rxq) 353 WRITE_ONCE(rxq->cached_phc_time, systime); 354 } 355 } 356 } 357 358 /** 359 * idpf_ptp_update_cached_phctime - Update the cached PHC time values 360 * @adapter: Driver specific private structure 361 * 362 * This function updates the system time values which are cached in the adapter 363 * structure and the Rx queues. 364 * 365 * This function must be called periodically to ensure that the cached value 366 * is never more than 2 seconds old. 367 * 368 * Return: 0 on success, -errno otherwise. 369 */ 370 static int idpf_ptp_update_cached_phctime(struct idpf_adapter *adapter) 371 { 372 u64 systime; 373 int err; 374 375 err = idpf_ptp_read_src_clk_reg(adapter, &systime, NULL); 376 if (err) 377 return -EACCES; 378 379 /* Update the cached PHC time stored in the adapter structure. 380 * These values are used to extend Tx timestamp values to 64 bit 381 * expected by the stack. 382 */ 383 WRITE_ONCE(adapter->ptp->cached_phc_time, systime); 384 WRITE_ONCE(adapter->ptp->cached_phc_jiffies, jiffies); 385 386 idpf_for_each_vport(adapter, vport) { 387 struct idpf_q_vec_rsrc *rsrc; 388 bool split; 389 390 if (!vport || !vport->dflt_qv_rsrc.rxq_grps) 391 continue; 392 393 rsrc = &vport->dflt_qv_rsrc; 394 split = idpf_is_queue_model_split(rsrc->rxq_model); 395 396 for (u16 i = 0; i < rsrc->num_rxq_grp; i++) { 397 struct idpf_rxq_group *grp = &rsrc->rxq_grps[i]; 398 399 idpf_ptp_update_phctime_rxq_grp(grp, split, systime); 400 } 401 } 402 403 return 0; 404 } 405 406 /** 407 * idpf_ptp_settime64 - Set the time of the clock 408 * @info: the driver's PTP info structure 409 * @ts: timespec64 structure that holds the new time value 410 * 411 * Set the device clock to the user input value. The conversion from timespec 412 * to ns happens in the write function. 413 * 414 * Return: 0 on success, -errno otherwise. 415 */ 416 static int idpf_ptp_settime64(struct ptp_clock_info *info, 417 const struct timespec64 *ts) 418 { 419 struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info); 420 enum idpf_ptp_access access; 421 int err; 422 u64 ns; 423 424 access = adapter->ptp->set_dev_clk_time_access; 425 if (access != IDPF_PTP_MAILBOX) 426 return -EOPNOTSUPP; 427 428 ns = timespec64_to_ns(ts); 429 430 err = idpf_ptp_set_dev_clk_time(adapter, ns); 431 if (err) { 432 pci_err(adapter->pdev, "Failed to set the time, err: %pe\n", 433 ERR_PTR(err)); 434 return err; 435 } 436 437 err = idpf_ptp_update_cached_phctime(adapter); 438 if (err) 439 pci_warn(adapter->pdev, 440 "Unable to immediately update cached PHC time\n"); 441 442 return 0; 443 } 444 445 /** 446 * idpf_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 447 * @info: the driver's PTP info structure 448 * @delta: Offset in nanoseconds to adjust the time by 449 * 450 * Return: 0 on success, -errno otherwise. 451 */ 452 static int idpf_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 453 { 454 struct timespec64 now, then; 455 int err; 456 457 err = idpf_ptp_gettimex64(info, &now, NULL); 458 if (err) 459 return err; 460 461 then = ns_to_timespec64(delta); 462 now = timespec64_add(now, then); 463 464 return idpf_ptp_settime64(info, &now); 465 } 466 467 /** 468 * idpf_ptp_adjtime - Adjust the time of the clock by the indicated delta 469 * @info: the driver's PTP info structure 470 * @delta: Offset in nanoseconds to adjust the time by 471 * 472 * Return: 0 on success, -errno otherwise. 473 */ 474 static int idpf_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 475 { 476 struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info); 477 enum idpf_ptp_access access; 478 int err; 479 480 access = adapter->ptp->adj_dev_clk_time_access; 481 if (access != IDPF_PTP_MAILBOX) 482 return -EOPNOTSUPP; 483 484 /* Hardware only supports atomic adjustments using signed 32-bit 485 * integers. For any adjustment outside this range, perform 486 * a non-atomic get->adjust->set flow. 487 */ 488 if (delta > S32_MAX || delta < S32_MIN) 489 return idpf_ptp_adjtime_nonatomic(info, delta); 490 491 err = idpf_ptp_adj_dev_clk_time(adapter, delta); 492 if (err) { 493 pci_err(adapter->pdev, "Failed to adjust the clock with delta %lld err: %pe\n", 494 delta, ERR_PTR(err)); 495 return err; 496 } 497 498 err = idpf_ptp_update_cached_phctime(adapter); 499 if (err) 500 pci_warn(adapter->pdev, 501 "Unable to immediately update cached PHC time\n"); 502 503 return 0; 504 } 505 506 /** 507 * idpf_ptp_adjfine - Adjust clock increment rate 508 * @info: the driver's PTP info structure 509 * @scaled_ppm: Parts per million with 16-bit fractional field 510 * 511 * Adjust the frequency of the clock by the indicated scaled ppm from the 512 * base frequency. 513 * 514 * Return: 0 on success, -errno otherwise. 515 */ 516 static int idpf_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 517 { 518 struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info); 519 enum idpf_ptp_access access; 520 u64 incval, diff; 521 int err; 522 523 access = adapter->ptp->adj_dev_clk_time_access; 524 if (access != IDPF_PTP_MAILBOX) 525 return -EOPNOTSUPP; 526 527 incval = adapter->ptp->base_incval; 528 529 diff = adjust_by_scaled_ppm(incval, scaled_ppm); 530 err = idpf_ptp_adj_dev_clk_fine(adapter, diff); 531 if (err) 532 pci_err(adapter->pdev, "Failed to adjust clock increment rate for scaled ppm %ld %pe\n", 533 scaled_ppm, ERR_PTR(err)); 534 535 return 0; 536 } 537 538 /** 539 * idpf_ptp_verify_pin - Verify if pin supports requested pin function 540 * @info: the driver's PTP info structure 541 * @pin: Pin index 542 * @func: Assigned function 543 * @chan: Assigned channel 544 * 545 * Return: EOPNOTSUPP as not supported yet. 546 */ 547 static int idpf_ptp_verify_pin(struct ptp_clock_info *info, unsigned int pin, 548 enum ptp_pin_function func, unsigned int chan) 549 { 550 return -EOPNOTSUPP; 551 } 552 553 /** 554 * idpf_ptp_gpio_enable - Enable/disable ancillary features of PHC 555 * @info: the driver's PTP info structure 556 * @rq: The requested feature to change 557 * @on: Enable/disable flag 558 * 559 * Return: EOPNOTSUPP as not supported yet. 560 */ 561 static int idpf_ptp_gpio_enable(struct ptp_clock_info *info, 562 struct ptp_clock_request *rq, int on) 563 { 564 return -EOPNOTSUPP; 565 } 566 567 /** 568 * idpf_ptp_tstamp_extend_32b_to_64b - Convert a 32b nanoseconds Tx or Rx 569 * timestamp value to 64b. 570 * @cached_phc_time: recently cached copy of PHC time 571 * @in_timestamp: Ingress/egress 32b nanoseconds timestamp value 572 * 573 * Hardware captures timestamps which contain only 32 bits of nominal 574 * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 575 * 576 * Return: Tx timestamp value extended to 64 bits based on cached PHC time. 577 */ 578 u64 idpf_ptp_tstamp_extend_32b_to_64b(u64 cached_phc_time, u32 in_timestamp) 579 { 580 u32 delta, phc_time_lo; 581 u64 ns; 582 583 /* Extract the lower 32 bits of the PHC time */ 584 phc_time_lo = (u32)cached_phc_time; 585 586 /* Calculate the delta between the lower 32bits of the cached PHC 587 * time and the in_timestamp value. 588 */ 589 delta = in_timestamp - phc_time_lo; 590 591 if (delta > U32_MAX / 2) { 592 /* Reverse the delta calculation here */ 593 delta = phc_time_lo - in_timestamp; 594 ns = cached_phc_time - delta; 595 } else { 596 ns = cached_phc_time + delta; 597 } 598 599 return ns; 600 } 601 602 /** 603 * idpf_ptp_extend_ts - Convert a 40b timestamp to 64b nanoseconds 604 * @vport: Virtual port structure 605 * @in_tstamp: Ingress/egress timestamp value 606 * 607 * It is assumed that the caller verifies the timestamp is valid prior to 608 * calling this function. 609 * 610 * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 611 * time stored in the device private PTP structure as the basis for timestamp 612 * extension. 613 * 614 * Return: Tx timestamp value extended to 64 bits. 615 */ 616 u64 idpf_ptp_extend_ts(struct idpf_vport *vport, u64 in_tstamp) 617 { 618 struct idpf_ptp *ptp = vport->adapter->ptp; 619 unsigned long discard_time; 620 621 discard_time = ptp->cached_phc_jiffies + 2 * HZ; 622 623 if (time_is_before_jiffies(discard_time)) { 624 u64_stats_update_begin(&vport->tstamp_stats.stats_sync); 625 u64_stats_inc(&vport->tstamp_stats.discarded); 626 u64_stats_update_end(&vport->tstamp_stats.stats_sync); 627 628 return 0; 629 } 630 631 return idpf_ptp_tstamp_extend_32b_to_64b(ptp->cached_phc_time, 632 lower_32_bits(in_tstamp)); 633 } 634 635 /** 636 * idpf_ptp_request_ts - Request an available Tx timestamp index 637 * @tx_q: Transmit queue on which the Tx timestamp is requested 638 * @skb: The SKB to associate with this timestamp request 639 * @idx: Index of the Tx timestamp latch 640 * 641 * Request tx timestamp index negotiated during PTP init that will be set into 642 * Tx descriptor. 643 * 644 * Return: 0 and the index that can be provided to Tx descriptor on success, 645 * -errno otherwise. 646 */ 647 int idpf_ptp_request_ts(struct idpf_tx_queue *tx_q, struct sk_buff *skb, 648 u32 *idx) 649 { 650 struct idpf_ptp_tx_tstamp *ptp_tx_tstamp; 651 struct list_head *head; 652 653 /* Get the index from the free latches list */ 654 spin_lock(&tx_q->cached_tstamp_caps->latches_lock); 655 656 head = &tx_q->cached_tstamp_caps->latches_free; 657 if (list_empty(head)) { 658 spin_unlock(&tx_q->cached_tstamp_caps->latches_lock); 659 return -ENOBUFS; 660 } 661 662 ptp_tx_tstamp = list_first_entry(head, struct idpf_ptp_tx_tstamp, 663 list_member); 664 list_del(&ptp_tx_tstamp->list_member); 665 666 ptp_tx_tstamp->skb = skb_get(skb); 667 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 668 669 /* Move the element to the used latches list */ 670 list_add(&ptp_tx_tstamp->list_member, 671 &tx_q->cached_tstamp_caps->latches_in_use); 672 spin_unlock(&tx_q->cached_tstamp_caps->latches_lock); 673 674 *idx = ptp_tx_tstamp->idx; 675 676 return 0; 677 } 678 679 /** 680 * idpf_ptp_set_rx_tstamp - Enable or disable Rx timestamping 681 * @vport: Virtual port structure 682 * @rx_filter: Receive timestamp filter 683 */ 684 static void idpf_ptp_set_rx_tstamp(struct idpf_vport *vport, int rx_filter) 685 { 686 struct idpf_q_vec_rsrc *rsrc = &vport->dflt_qv_rsrc; 687 bool enable = true, splitq; 688 689 splitq = idpf_is_queue_model_split(rsrc->rxq_model); 690 691 if (rx_filter == HWTSTAMP_FILTER_NONE) { 692 enable = false; 693 vport->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 694 } else { 695 vport->tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL; 696 } 697 698 for (u16 i = 0; i < rsrc->num_rxq_grp; i++) { 699 struct idpf_rxq_group *grp = &rsrc->rxq_grps[i]; 700 struct idpf_rx_queue *rx_queue; 701 u16 j, num_rxq; 702 703 if (splitq) 704 num_rxq = grp->splitq.num_rxq_sets; 705 else 706 num_rxq = grp->singleq.num_rxq; 707 708 for (j = 0; j < num_rxq; j++) { 709 if (splitq) 710 rx_queue = &grp->splitq.rxq_sets[j]->rxq; 711 else 712 rx_queue = grp->singleq.rxqs[j]; 713 714 if (enable) 715 idpf_queue_set(PTP, rx_queue); 716 else 717 idpf_queue_clear(PTP, rx_queue); 718 } 719 } 720 } 721 722 /** 723 * idpf_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 724 * @vport: Virtual port structure 725 * @config: Hwtstamp settings requested or saved 726 * 727 * Return: 0 on success, -errno otherwise. 728 */ 729 int idpf_ptp_set_timestamp_mode(struct idpf_vport *vport, 730 struct kernel_hwtstamp_config *config) 731 { 732 switch (config->tx_type) { 733 case HWTSTAMP_TX_OFF: 734 break; 735 case HWTSTAMP_TX_ON: 736 if (!idpf_ptp_is_vport_tx_tstamp_ena(vport)) 737 return -EINVAL; 738 break; 739 default: 740 return -EINVAL; 741 } 742 743 vport->tstamp_config.tx_type = config->tx_type; 744 idpf_ptp_set_rx_tstamp(vport, config->rx_filter); 745 *config = vport->tstamp_config; 746 747 return 0; 748 } 749 750 /** 751 * idpf_tstamp_task - Delayed task to handle Tx tstamps 752 * @work: work_struct handle 753 */ 754 void idpf_tstamp_task(struct work_struct *work) 755 { 756 struct idpf_vport *vport; 757 758 vport = container_of(work, struct idpf_vport, tstamp_task); 759 760 idpf_ptp_get_tx_tstamp(vport); 761 } 762 763 /** 764 * idpf_ptp_do_aux_work - Do PTP periodic work 765 * @info: Driver's PTP info structure 766 * 767 * Return: Number of jiffies to periodic work. 768 */ 769 static long idpf_ptp_do_aux_work(struct ptp_clock_info *info) 770 { 771 struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info); 772 773 idpf_ptp_update_cached_phctime(adapter); 774 775 return msecs_to_jiffies(500); 776 } 777 778 /** 779 * idpf_ptp_set_caps - Set PTP capabilities 780 * @adapter: Driver specific private structure 781 * 782 * This function sets the PTP functions. 783 */ 784 static void idpf_ptp_set_caps(const struct idpf_adapter *adapter) 785 { 786 struct ptp_clock_info *info = &adapter->ptp->info; 787 788 snprintf(info->name, sizeof(info->name), "%s-%s-clk", 789 KBUILD_MODNAME, pci_name(adapter->pdev)); 790 791 info->owner = THIS_MODULE; 792 info->max_adj = adapter->ptp->max_adj; 793 info->gettimex64 = idpf_ptp_gettimex64; 794 info->settime64 = idpf_ptp_settime64; 795 info->adjfine = idpf_ptp_adjfine; 796 info->adjtime = idpf_ptp_adjtime; 797 info->verify = idpf_ptp_verify_pin; 798 info->enable = idpf_ptp_gpio_enable; 799 info->do_aux_work = idpf_ptp_do_aux_work; 800 #if IS_ENABLED(CONFIG_ARM_ARCH_TIMER) 801 info->getcrosststamp = idpf_ptp_get_crosststamp; 802 #elif IS_ENABLED(CONFIG_X86) 803 if (pcie_ptm_enabled(adapter->pdev) && 804 boot_cpu_has(X86_FEATURE_ART) && 805 boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) 806 info->getcrosststamp = idpf_ptp_get_crosststamp; 807 #endif /* CONFIG_ARM_ARCH_TIMER */ 808 } 809 810 /** 811 * idpf_ptp_create_clock - Create PTP clock device for userspace 812 * @adapter: Driver specific private structure 813 * 814 * This function creates a new PTP clock device. 815 * 816 * Return: 0 on success, -errno otherwise. 817 */ 818 static int idpf_ptp_create_clock(const struct idpf_adapter *adapter) 819 { 820 struct ptp_clock *clock; 821 822 idpf_ptp_set_caps(adapter); 823 824 /* Attempt to register the clock before enabling the hardware. */ 825 clock = ptp_clock_register(&adapter->ptp->info, 826 &adapter->pdev->dev); 827 if (IS_ERR(clock)) { 828 pci_err(adapter->pdev, "PTP clock creation failed: %pe\n", 829 clock); 830 return PTR_ERR(clock); 831 } 832 833 adapter->ptp->clock = clock; 834 835 return 0; 836 } 837 838 /** 839 * idpf_ptp_release_vport_tstamp - Release the Tx timestamps trakcers for a 840 * given vport. 841 * @vport: Virtual port structure 842 * 843 * Remove the queues and delete lists that tracks Tx timestamp entries for a 844 * given vport. 845 */ 846 static void idpf_ptp_release_vport_tstamp(struct idpf_vport *vport) 847 { 848 struct idpf_ptp_tx_tstamp *ptp_tx_tstamp, *tmp; 849 struct list_head *head; 850 851 cancel_work_sync(&vport->tstamp_task); 852 853 /* Remove list with free latches */ 854 spin_lock_bh(&vport->tx_tstamp_caps->latches_lock); 855 856 head = &vport->tx_tstamp_caps->latches_free; 857 list_for_each_entry_safe(ptp_tx_tstamp, tmp, head, list_member) { 858 list_del(&ptp_tx_tstamp->list_member); 859 kfree(ptp_tx_tstamp); 860 } 861 862 /* Remove list with latches in use */ 863 head = &vport->tx_tstamp_caps->latches_in_use; 864 u64_stats_update_begin(&vport->tstamp_stats.stats_sync); 865 list_for_each_entry_safe(ptp_tx_tstamp, tmp, head, list_member) { 866 u64_stats_inc(&vport->tstamp_stats.flushed); 867 868 list_del(&ptp_tx_tstamp->list_member); 869 if (ptp_tx_tstamp->skb) 870 consume_skb(ptp_tx_tstamp->skb); 871 872 kfree(ptp_tx_tstamp); 873 } 874 u64_stats_update_end(&vport->tstamp_stats.stats_sync); 875 876 spin_unlock_bh(&vport->tx_tstamp_caps->latches_lock); 877 878 kfree(vport->tx_tstamp_caps); 879 vport->tx_tstamp_caps = NULL; 880 } 881 882 /** 883 * idpf_ptp_release_tstamp - Release the Tx timestamps trackers 884 * @adapter: Driver specific private structure 885 * 886 * Remove the queues and delete lists that tracks Tx timestamp entries. 887 */ 888 static void idpf_ptp_release_tstamp(struct idpf_adapter *adapter) 889 { 890 idpf_for_each_vport(adapter, vport) { 891 if (!idpf_ptp_is_vport_tx_tstamp_ena(vport)) 892 continue; 893 894 idpf_ptp_release_vport_tstamp(vport); 895 } 896 } 897 898 /** 899 * idpf_ptp_get_txq_tstamp_capability - Verify the timestamping capability 900 * for a given tx queue. 901 * @txq: Transmit queue 902 * 903 * Since performing timestamp flows requires reading the device clock value and 904 * the support in the Control Plane, the function checks both factors and 905 * summarizes the support for the timestamping. 906 * 907 * Return: true if the timestamping is supported, false otherwise. 908 */ 909 bool idpf_ptp_get_txq_tstamp_capability(struct idpf_tx_queue *txq) 910 { 911 if (!txq || !txq->cached_tstamp_caps) 912 return false; 913 else if (txq->cached_tstamp_caps->access) 914 return true; 915 else 916 return false; 917 } 918 919 /** 920 * idpf_ptp_init - Initialize PTP hardware clock support 921 * @adapter: Driver specific private structure 922 * 923 * Set up the device for interacting with the PTP hardware clock for all 924 * functions. Function will allocate and register a ptp_clock with the 925 * PTP_1588_CLOCK infrastructure. 926 * 927 * Return: 0 on success, -errno otherwise. 928 */ 929 int idpf_ptp_init(struct idpf_adapter *adapter) 930 { 931 struct timespec64 ts; 932 int err; 933 934 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PTP)) { 935 pci_dbg(adapter->pdev, "PTP capability is not detected\n"); 936 return -EOPNOTSUPP; 937 } 938 939 adapter->ptp = kzalloc(sizeof(*adapter->ptp), GFP_KERNEL); 940 if (!adapter->ptp) 941 return -ENOMEM; 942 943 /* add a back pointer to adapter */ 944 adapter->ptp->adapter = adapter; 945 946 if (adapter->dev_ops.reg_ops.ptp_reg_init) 947 adapter->dev_ops.reg_ops.ptp_reg_init(adapter); 948 949 err = idpf_ptp_get_caps(adapter); 950 if (err) { 951 pci_err(adapter->pdev, "Failed to get PTP caps err %d\n", err); 952 goto free_ptp; 953 } 954 955 err = idpf_ptp_create_clock(adapter); 956 if (err) 957 goto free_ptp; 958 959 if (adapter->ptp->get_dev_clk_time_access != IDPF_PTP_NONE) 960 ptp_schedule_worker(adapter->ptp->clock, 0); 961 962 /* Write the default increment time value if the clock adjustments 963 * are enabled. 964 */ 965 if (adapter->ptp->adj_dev_clk_time_access != IDPF_PTP_NONE) { 966 err = idpf_ptp_adj_dev_clk_fine(adapter, 967 adapter->ptp->base_incval); 968 if (err) 969 goto remove_clock; 970 } 971 972 /* Write the initial time value if the set time operation is enabled */ 973 if (adapter->ptp->set_dev_clk_time_access != IDPF_PTP_NONE) { 974 ts = ktime_to_timespec64(ktime_get_real()); 975 err = idpf_ptp_settime64(&adapter->ptp->info, &ts); 976 if (err) 977 goto remove_clock; 978 } 979 980 spin_lock_init(&adapter->ptp->read_dev_clk_lock); 981 982 pci_dbg(adapter->pdev, "PTP init successful\n"); 983 984 return 0; 985 986 remove_clock: 987 if (adapter->ptp->get_dev_clk_time_access != IDPF_PTP_NONE) 988 ptp_cancel_worker_sync(adapter->ptp->clock); 989 990 ptp_clock_unregister(adapter->ptp->clock); 991 adapter->ptp->clock = NULL; 992 993 free_ptp: 994 kfree(adapter->ptp); 995 adapter->ptp = NULL; 996 997 return err; 998 } 999 1000 /** 1001 * idpf_ptp_release - Clear PTP hardware clock support 1002 * @adapter: Driver specific private structure 1003 */ 1004 void idpf_ptp_release(struct idpf_adapter *adapter) 1005 { 1006 struct idpf_ptp *ptp = adapter->ptp; 1007 1008 if (!ptp) 1009 return; 1010 1011 if (ptp->tx_tstamp_access != IDPF_PTP_NONE && 1012 ptp->get_dev_clk_time_access != IDPF_PTP_NONE) 1013 idpf_ptp_release_tstamp(adapter); 1014 1015 if (ptp->clock) { 1016 if (adapter->ptp->get_dev_clk_time_access != IDPF_PTP_NONE) 1017 ptp_cancel_worker_sync(adapter->ptp->clock); 1018 1019 ptp_clock_unregister(ptp->clock); 1020 } 1021 1022 kfree(ptp); 1023 adapter->ptp = NULL; 1024 } 1025