Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | // SPDX-License-Identifier: GPL-2.0 /* Copyright(c) 2017 - 2021 Pensando Systems, Inc */ #include <linux/netdevice.h> #include <linux/etherdevice.h> #include "ionic.h" #include "ionic_bus.h" #include "ionic_lif.h" #include "ionic_ethtool.h" static int ionic_hwstamp_tx_mode(int config_tx_type) { switch (config_tx_type) { case HWTSTAMP_TX_OFF: return IONIC_TXSTAMP_OFF; case HWTSTAMP_TX_ON: return IONIC_TXSTAMP_ON; case HWTSTAMP_TX_ONESTEP_SYNC: return IONIC_TXSTAMP_ONESTEP_SYNC; case HWTSTAMP_TX_ONESTEP_P2P: return IONIC_TXSTAMP_ONESTEP_P2P; default: return -ERANGE; } } static u64 ionic_hwstamp_rx_filt(int config_rx_filter) { switch (config_rx_filter) { case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: return IONIC_PKT_CLS_PTP1_ALL; case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: return IONIC_PKT_CLS_PTP1_SYNC; case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: return IONIC_PKT_CLS_PTP1_SYNC | IONIC_PKT_CLS_PTP1_DREQ; case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: return IONIC_PKT_CLS_PTP2_L4_ALL; case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: return IONIC_PKT_CLS_PTP2_L4_SYNC; case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: return IONIC_PKT_CLS_PTP2_L4_SYNC | IONIC_PKT_CLS_PTP2_L4_DREQ; case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: return IONIC_PKT_CLS_PTP2_L2_ALL; case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: return IONIC_PKT_CLS_PTP2_L2_SYNC; case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: return IONIC_PKT_CLS_PTP2_L2_SYNC | IONIC_PKT_CLS_PTP2_L2_DREQ; case HWTSTAMP_FILTER_PTP_V2_EVENT: return IONIC_PKT_CLS_PTP2_ALL; case HWTSTAMP_FILTER_PTP_V2_SYNC: return IONIC_PKT_CLS_PTP2_SYNC; case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: return IONIC_PKT_CLS_PTP2_SYNC | IONIC_PKT_CLS_PTP2_DREQ; case HWTSTAMP_FILTER_NTP_ALL: return IONIC_PKT_CLS_NTP_ALL; default: return 0; } } static int ionic_lif_hwstamp_set_ts_config(struct ionic_lif *lif, struct hwtstamp_config *new_ts) { struct ionic *ionic = lif->ionic; struct hwtstamp_config *config; struct hwtstamp_config ts; int tx_mode = 0; u64 rx_filt = 0; int err, err2; bool rx_all; __le64 mask; if (!lif->phc || !lif->phc->ptp) return -EOPNOTSUPP; mutex_lock(&lif->phc->config_lock); if (new_ts) { config = new_ts; } else { /* If called with new_ts == NULL, replay the previous request * primarily for recovery after a FW_RESET. * We saved the previous configuration request info, so copy * the previous request for reference, clear the current state * to match the device's reset state, and run with it. */ config = &ts; memcpy(config, &lif->phc->ts_config, sizeof(*config)); memset(&lif->phc->ts_config, 0, sizeof(lif->phc->ts_config)); lif->phc->ts_config_tx_mode = 0; lif->phc->ts_config_rx_filt = 0; } tx_mode = ionic_hwstamp_tx_mode(config->tx_type); if (tx_mode < 0) { err = tx_mode; goto err_queues; } mask = cpu_to_le64(BIT_ULL(tx_mode)); if ((ionic->ident.lif.eth.hwstamp_tx_modes & mask) != mask) { err = -ERANGE; goto err_queues; } rx_filt = ionic_hwstamp_rx_filt(config->rx_filter); rx_all = config->rx_filter != HWTSTAMP_FILTER_NONE && !rx_filt; mask = cpu_to_le64(rx_filt); if ((ionic->ident.lif.eth.hwstamp_rx_filters & mask) != mask) { rx_filt = 0; rx_all = true; config->rx_filter = HWTSTAMP_FILTER_ALL; } dev_dbg(ionic->dev, "%s: config_rx_filter %d rx_filt %#llx rx_all %d\n", __func__, config->rx_filter, rx_filt, rx_all); if (tx_mode) { err = ionic_lif_create_hwstamp_txq(lif); if (err) goto err_queues; } if (rx_filt) { err = ionic_lif_create_hwstamp_rxq(lif); if (err) goto err_queues; } if (tx_mode != lif->phc->ts_config_tx_mode) { err = ionic_lif_set_hwstamp_txmode(lif, tx_mode); if (err) goto err_txmode; } if (rx_filt != lif->phc->ts_config_rx_filt) { err = ionic_lif_set_hwstamp_rxfilt(lif, rx_filt); if (err) goto err_rxfilt; } if (rx_all != (lif->phc->ts_config.rx_filter == HWTSTAMP_FILTER_ALL)) { err = ionic_lif_config_hwstamp_rxq_all(lif, rx_all); if (err) goto err_rxall; } memcpy(&lif->phc->ts_config, config, sizeof(*config)); lif->phc->ts_config_rx_filt = rx_filt; lif->phc->ts_config_tx_mode = tx_mode; mutex_unlock(&lif->phc->config_lock); return 0; err_rxall: if (rx_filt != lif->phc->ts_config_rx_filt) { rx_filt = lif->phc->ts_config_rx_filt; err2 = ionic_lif_set_hwstamp_rxfilt(lif, rx_filt); if (err2) dev_err(ionic->dev, "Failed to revert rx timestamp filter: %d\n", err2); } err_rxfilt: if (tx_mode != lif->phc->ts_config_tx_mode) { tx_mode = lif->phc->ts_config_tx_mode; err2 = ionic_lif_set_hwstamp_txmode(lif, tx_mode); if (err2) dev_err(ionic->dev, "Failed to revert tx timestamp mode: %d\n", err2); } err_txmode: /* special queues remain allocated, just unused */ err_queues: mutex_unlock(&lif->phc->config_lock); return err; } int ionic_lif_hwstamp_set(struct ionic_lif *lif, struct ifreq *ifr) { struct hwtstamp_config config; int err; if (!lif->phc || !lif->phc->ptp) return -EOPNOTSUPP; if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) return -EFAULT; mutex_lock(&lif->queue_lock); err = ionic_lif_hwstamp_set_ts_config(lif, &config); mutex_unlock(&lif->queue_lock); if (err) { netdev_info(lif->netdev, "hwstamp set failed: %d\n", err); return err; } if (copy_to_user(ifr->ifr_data, &config, sizeof(config))) return -EFAULT; return 0; } void ionic_lif_hwstamp_replay(struct ionic_lif *lif) { int err; if (!lif->phc || !lif->phc->ptp) return; mutex_lock(&lif->queue_lock); err = ionic_lif_hwstamp_set_ts_config(lif, NULL); mutex_unlock(&lif->queue_lock); if (err) netdev_info(lif->netdev, "hwstamp replay failed: %d\n", err); } void ionic_lif_hwstamp_recreate_queues(struct ionic_lif *lif) { int err; if (!lif->phc || !lif->phc->ptp) return; mutex_lock(&lif->phc->config_lock); if (lif->phc->ts_config_tx_mode) { err = ionic_lif_create_hwstamp_txq(lif); if (err) netdev_info(lif->netdev, "hwstamp recreate txq failed: %d\n", err); } if (lif->phc->ts_config_rx_filt) { err = ionic_lif_create_hwstamp_rxq(lif); if (err) netdev_info(lif->netdev, "hwstamp recreate rxq failed: %d\n", err); } mutex_unlock(&lif->phc->config_lock); } int ionic_lif_hwstamp_get(struct ionic_lif *lif, struct ifreq *ifr) { struct hwtstamp_config config; if (!lif->phc || !lif->phc->ptp) return -EOPNOTSUPP; mutex_lock(&lif->phc->config_lock); memcpy(&config, &lif->phc->ts_config, sizeof(config)); mutex_unlock(&lif->phc->config_lock); if (copy_to_user(ifr->ifr_data, &config, sizeof(config))) return -EFAULT; return 0; } static u64 ionic_hwstamp_read(struct ionic *ionic, struct ptp_system_timestamp *sts) { u32 tick_high_before, tick_high, tick_low; /* read and discard low part to defeat hw staging of high part */ ioread32(&ionic->idev.hwstamp_regs->tick_low); tick_high_before = ioread32(&ionic->idev.hwstamp_regs->tick_high); ptp_read_system_prets(sts); tick_low = ioread32(&ionic->idev.hwstamp_regs->tick_low); ptp_read_system_postts(sts); tick_high = ioread32(&ionic->idev.hwstamp_regs->tick_high); /* If tick_high changed, re-read tick_low once more. Assume tick_high * cannot change again so soon as in the span of re-reading tick_low. */ if (tick_high != tick_high_before) { ptp_read_system_prets(sts); tick_low = ioread32(&ionic->idev.hwstamp_regs->tick_low); ptp_read_system_postts(sts); } return (u64)tick_low | ((u64)tick_high << 32); } static u64 ionic_cc_read(const struct cyclecounter *cc) { struct ionic_phc *phc = container_of(cc, struct ionic_phc, cc); struct ionic *ionic = phc->lif->ionic; return ionic_hwstamp_read(ionic, NULL); } static int ionic_setphc_cmd(struct ionic_phc *phc, struct ionic_admin_ctx *ctx) { ctx->work = COMPLETION_INITIALIZER_ONSTACK(ctx->work); ctx->cmd.lif_setphc.opcode = IONIC_CMD_LIF_SETPHC; ctx->cmd.lif_setphc.lif_index = cpu_to_le16(phc->lif->index); ctx->cmd.lif_setphc.tick = cpu_to_le64(phc->tc.cycle_last); ctx->cmd.lif_setphc.nsec = cpu_to_le64(phc->tc.nsec); ctx->cmd.lif_setphc.frac = cpu_to_le64(phc->tc.frac); ctx->cmd.lif_setphc.mult = cpu_to_le32(phc->cc.mult); ctx->cmd.lif_setphc.shift = cpu_to_le32(phc->cc.shift); return ionic_adminq_post(phc->lif, ctx); } static int ionic_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm) { struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info); struct ionic_admin_ctx ctx = {}; unsigned long irqflags; s64 adj; int err; /* Reject phc adjustments during device upgrade */ if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state)) return -EBUSY; /* Adjustment value scaled by 2^16 million */ adj = (s64)scaled_ppm * phc->init_cc_mult; /* Adjustment value to scale */ adj /= (s64)SCALED_PPM; /* Final adjusted multiplier */ adj += phc->init_cc_mult; spin_lock_irqsave(&phc->lock, irqflags); /* update the point-in-time basis to now, before adjusting the rate */ timecounter_read(&phc->tc); phc->cc.mult = adj; /* Setphc commands are posted in-order, sequenced by phc->lock. We * need to drop the lock before waiting for the command to complete. */ err = ionic_setphc_cmd(phc, &ctx); spin_unlock_irqrestore(&phc->lock, irqflags); return ionic_adminq_wait(phc->lif, &ctx, err, true); } static int ionic_phc_adjtime(struct ptp_clock_info *info, s64 delta) { struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info); struct ionic_admin_ctx ctx = {}; unsigned long irqflags; int err; /* Reject phc adjustments during device upgrade */ if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state)) return -EBUSY; spin_lock_irqsave(&phc->lock, irqflags); timecounter_adjtime(&phc->tc, delta); /* Setphc commands are posted in-order, sequenced by phc->lock. We * need to drop the lock before waiting for the command to complete. */ err = ionic_setphc_cmd(phc, &ctx); spin_unlock_irqrestore(&phc->lock, irqflags); return ionic_adminq_wait(phc->lif, &ctx, err, true); } static int ionic_phc_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) { struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info); struct ionic_admin_ctx ctx = {}; unsigned long irqflags; int err; u64 ns; /* Reject phc adjustments during device upgrade */ if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state)) return -EBUSY; ns = timespec64_to_ns(ts); spin_lock_irqsave(&phc->lock, irqflags); timecounter_init(&phc->tc, &phc->cc, ns); /* Setphc commands are posted in-order, sequenced by phc->lock. We * need to drop the lock before waiting for the command to complete. */ err = ionic_setphc_cmd(phc, &ctx); spin_unlock_irqrestore(&phc->lock, irqflags); return ionic_adminq_wait(phc->lif, &ctx, err, true); } static int ionic_phc_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, struct ptp_system_timestamp *sts) { struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info); struct ionic *ionic = phc->lif->ionic; unsigned long irqflags; u64 tick, ns; /* Do not attempt to read device time during upgrade */ if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state)) return -EBUSY; spin_lock_irqsave(&phc->lock, irqflags); tick = ionic_hwstamp_read(ionic, sts); ns = timecounter_cyc2time(&phc->tc, tick); spin_unlock_irqrestore(&phc->lock, irqflags); *ts = ns_to_timespec64(ns); return 0; } static long ionic_phc_aux_work(struct ptp_clock_info *info) { struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info); struct ionic_admin_ctx ctx = {}; unsigned long irqflags; int err; /* Do not update phc during device upgrade, but keep polling to resume * after upgrade. Since we don't update the point in time basis, there * is no expectation that we are maintaining the phc time during the * upgrade. After upgrade, it will need to be readjusted back to the * correct time by the ptp daemon. */ if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state)) return phc->aux_work_delay; spin_lock_irqsave(&phc->lock, irqflags); /* update point-in-time basis to now */ timecounter_read(&phc->tc); /* Setphc commands are posted in-order, sequenced by phc->lock. We * need to drop the lock before waiting for the command to complete. */ err = ionic_setphc_cmd(phc, &ctx); spin_unlock_irqrestore(&phc->lock, irqflags); ionic_adminq_wait(phc->lif, &ctx, err, true); return phc->aux_work_delay; } ktime_t ionic_lif_phc_ktime(struct ionic_lif *lif, u64 tick) { unsigned long irqflags; u64 ns; if (!lif->phc) return 0; spin_lock_irqsave(&lif->phc->lock, irqflags); ns = timecounter_cyc2time(&lif->phc->tc, tick); spin_unlock_irqrestore(&lif->phc->lock, irqflags); return ns_to_ktime(ns); } static const struct ptp_clock_info ionic_ptp_info = { .owner = THIS_MODULE, .name = "ionic_ptp", .adjfine = ionic_phc_adjfine, .adjtime = ionic_phc_adjtime, .gettimex64 = ionic_phc_gettimex64, .settime64 = ionic_phc_settime64, .do_aux_work = ionic_phc_aux_work, }; void ionic_lif_register_phc(struct ionic_lif *lif) { if (!lif->phc || !(lif->hw_features & IONIC_ETH_HW_TIMESTAMP)) return; lif->phc->ptp = ptp_clock_register(&lif->phc->ptp_info, lif->ionic->dev); if (IS_ERR(lif->phc->ptp)) { dev_warn(lif->ionic->dev, "Cannot register phc device: %ld\n", PTR_ERR(lif->phc->ptp)); lif->phc->ptp = NULL; } if (lif->phc->ptp) ptp_schedule_worker(lif->phc->ptp, lif->phc->aux_work_delay); } void ionic_lif_unregister_phc(struct ionic_lif *lif) { if (!lif->phc || !lif->phc->ptp) return; ptp_clock_unregister(lif->phc->ptp); lif->phc->ptp = NULL; } void ionic_lif_alloc_phc(struct ionic_lif *lif) { struct ionic *ionic = lif->ionic; struct ionic_phc *phc; u64 delay, diff, mult; u64 frac = 0; u64 features; u32 shift; if (!ionic->idev.hwstamp_regs) return; features = le64_to_cpu(ionic->ident.lif.eth.config.features); if (!(features & IONIC_ETH_HW_TIMESTAMP)) return; phc = devm_kzalloc(ionic->dev, sizeof(*phc), GFP_KERNEL); if (!phc) return; phc->lif = lif; phc->cc.read = ionic_cc_read; phc->cc.mask = le64_to_cpu(ionic->ident.dev.hwstamp_mask); phc->cc.mult = le32_to_cpu(ionic->ident.dev.hwstamp_mult); phc->cc.shift = le32_to_cpu(ionic->ident.dev.hwstamp_shift); if (!phc->cc.mult) { dev_err(lif->ionic->dev, "Invalid device PHC mask multiplier %u, disabling HW timestamp support\n", phc->cc.mult); devm_kfree(lif->ionic->dev, phc); lif->phc = NULL; return; } dev_dbg(lif->ionic->dev, "Device PHC mask %#llx mult %u shift %u\n", phc->cc.mask, phc->cc.mult, phc->cc.shift); spin_lock_init(&phc->lock); mutex_init(&phc->config_lock); /* max ticks is limited by the multiplier, or by the update period. */ if (phc->cc.shift + 2 + ilog2(IONIC_PHC_UPDATE_NS) >= 64) { /* max ticks that do not overflow when multiplied by max * adjusted multiplier (twice the initial multiplier) */ diff = U64_MAX / phc->cc.mult / 2; } else { /* approx ticks at four times the update period */ diff = (u64)IONIC_PHC_UPDATE_NS << (phc->cc.shift + 2); diff = DIV_ROUND_UP(diff, phc->cc.mult); } /* transform to bitmask */ diff |= diff >> 1; diff |= diff >> 2; diff |= diff >> 4; diff |= diff >> 8; diff |= diff >> 16; diff |= diff >> 32; /* constrain to the hardware bitmask */ diff &= phc->cc.mask; /* the wrap period is now defined by diff * * we will update the time basis at about 1/4 the wrap period, so * should not see a difference of more than +/- diff/4. * * this is sufficient not see a difference of more than +/- diff/2, as * required by timecounter_cyc2time, to detect an old time stamp. * * adjust the initial multiplier, being careful to avoid overflow: * - do not overflow 63 bits: init_cc_mult * SCALED_PPM * - do not overflow 64 bits: max_mult * (diff / 2) * * we want to increase the initial multiplier as much as possible, to * allow for more precise adjustment in ionic_phc_adjfine. * * only adjust the multiplier if we can double it or more. */ mult = U64_MAX / 2 / max(diff / 2, SCALED_PPM); shift = mult / phc->cc.mult; if (shift >= 2) { /* initial multiplier will be 2^n of hardware cc.mult */ shift = fls(shift); /* increase cc.mult and cc.shift by the same 2^n and n. */ phc->cc.mult <<= shift; phc->cc.shift += shift; } dev_dbg(lif->ionic->dev, "Initial PHC mask %#llx mult %u shift %u\n", phc->cc.mask, phc->cc.mult, phc->cc.shift); /* frequency adjustments are relative to the initial multiplier */ phc->init_cc_mult = phc->cc.mult; timecounter_init(&phc->tc, &phc->cc, ktime_get_real_ns()); /* Update cycle_last at 1/4 the wrap period, or IONIC_PHC_UPDATE_NS */ delay = min_t(u64, IONIC_PHC_UPDATE_NS, cyclecounter_cyc2ns(&phc->cc, diff / 4, 0, &frac)); dev_dbg(lif->ionic->dev, "Work delay %llu ms\n", delay / NSEC_PER_MSEC); phc->aux_work_delay = nsecs_to_jiffies(delay); phc->ptp_info = ionic_ptp_info; /* We have allowed to adjust the multiplier up to +/- 1 part per 1. * Here expressed as NORMAL_PPB (1 billion parts per billion). */ phc->ptp_info.max_adj = NORMAL_PPB; lif->phc = phc; } void ionic_lif_free_phc(struct ionic_lif *lif) { if (!lif->phc) return; mutex_destroy(&lif->phc->config_lock); devm_kfree(lif->ionic->dev, lif->phc); lif->phc = NULL; } |