| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) |
|---|
| 1 | 2 | /* QLogic qede NIC Driver |
|---|
| 2 | 3 | * Copyright (c) 2015-2017 QLogic Corporation |
|---|
| 3 | | - * |
|---|
| 4 | | - * This software is available to you under a choice of one of two |
|---|
| 5 | | - * licenses. You may choose to be licensed under the terms of the GNU |
|---|
| 6 | | - * General Public License (GPL) Version 2, available from the file |
|---|
| 7 | | - * COPYING in the main directory of this source tree, or the |
|---|
| 8 | | - * OpenIB.org BSD license below: |
|---|
| 9 | | - * |
|---|
| 10 | | - * Redistribution and use in source and binary forms, with or |
|---|
| 11 | | - * without modification, are permitted provided that the following |
|---|
| 12 | | - * conditions are met: |
|---|
| 13 | | - * |
|---|
| 14 | | - * - Redistributions of source code must retain the above |
|---|
| 15 | | - * copyright notice, this list of conditions and the following |
|---|
| 16 | | - * disclaimer. |
|---|
| 17 | | - * |
|---|
| 18 | | - * - Redistributions in binary form must reproduce the above |
|---|
| 19 | | - * copyright notice, this list of conditions and the following |
|---|
| 20 | | - * disclaimer in the documentation and /or other materials |
|---|
| 21 | | - * provided with the distribution. |
|---|
| 22 | | - * |
|---|
| 23 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 24 | | - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 25 | | - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 26 | | - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
|---|
| 27 | | - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
|---|
| 28 | | - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|---|
| 29 | | - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|---|
| 30 | | - * SOFTWARE. |
|---|
| 4 | + * Copyright (c) 2019-2020 Marvell International Ltd. |
|---|
| 31 | 5 | */ |
|---|
| 6 | + |
|---|
| 32 | 7 | #include "qede_ptp.h" |
|---|
| 8 | +#define QEDE_PTP_TX_TIMEOUT (2 * HZ) |
|---|
| 33 | 9 | |
|---|
| 34 | 10 | struct qede_ptp { |
|---|
| 35 | 11 | const struct qed_eth_ptp_ops *ops; |
|---|
| .. | .. |
|---|
| 38 | 14 | struct timecounter tc; |
|---|
| 39 | 15 | struct ptp_clock *clock; |
|---|
| 40 | 16 | struct work_struct work; |
|---|
| 17 | + unsigned long ptp_tx_start; |
|---|
| 41 | 18 | struct qede_dev *edev; |
|---|
| 42 | 19 | struct sk_buff *tx_skb; |
|---|
| 43 | 20 | |
|---|
| .. | .. |
|---|
| 51 | 28 | }; |
|---|
| 52 | 29 | |
|---|
| 53 | 30 | /** |
|---|
| 54 | | - * qede_ptp_adjfreq |
|---|
| 55 | | - * @ptp: the ptp clock structure |
|---|
| 56 | | - * @ppb: parts per billion adjustment from base |
|---|
| 31 | + * qede_ptp_adjfreq() - Adjust the frequency of the PTP cycle counter. |
|---|
| 57 | 32 | * |
|---|
| 58 | | - * Adjust the frequency of the ptp cycle counter by the |
|---|
| 59 | | - * indicated ppb from the base frequency. |
|---|
| 33 | + * @info: The PTP clock info structure. |
|---|
| 34 | + * @ppb: Parts per billion adjustment from base. |
|---|
| 35 | + * |
|---|
| 36 | + * Return: Zero on success, negative errno otherwise. |
|---|
| 60 | 37 | */ |
|---|
| 61 | 38 | static int qede_ptp_adjfreq(struct ptp_clock_info *info, s32 ppb) |
|---|
| 62 | 39 | { |
|---|
| .. | .. |
|---|
| 160 | 137 | struct qede_dev *edev; |
|---|
| 161 | 138 | struct qede_ptp *ptp; |
|---|
| 162 | 139 | u64 timestamp, ns; |
|---|
| 140 | + bool timedout; |
|---|
| 163 | 141 | int rc; |
|---|
| 164 | 142 | |
|---|
| 165 | 143 | ptp = container_of(work, struct qede_ptp, work); |
|---|
| 166 | 144 | edev = ptp->edev; |
|---|
| 145 | + timedout = time_is_before_jiffies(ptp->ptp_tx_start + |
|---|
| 146 | + QEDE_PTP_TX_TIMEOUT); |
|---|
| 167 | 147 | |
|---|
| 168 | 148 | /* Read Tx timestamp registers */ |
|---|
| 169 | 149 | spin_lock_bh(&ptp->lock); |
|---|
| 170 | 150 | rc = ptp->ops->read_tx_ts(edev->cdev, ×tamp); |
|---|
| 171 | 151 | spin_unlock_bh(&ptp->lock); |
|---|
| 172 | 152 | if (rc) { |
|---|
| 173 | | - /* Reschedule to keep checking for a valid timestamp value */ |
|---|
| 174 | | - schedule_work(&ptp->work); |
|---|
| 153 | + if (unlikely(timedout)) { |
|---|
| 154 | + DP_INFO(edev, "Tx timestamp is not recorded\n"); |
|---|
| 155 | + dev_kfree_skb_any(ptp->tx_skb); |
|---|
| 156 | + ptp->tx_skb = NULL; |
|---|
| 157 | + clear_bit_unlock(QEDE_FLAGS_PTP_TX_IN_PRORGESS, |
|---|
| 158 | + &edev->flags); |
|---|
| 159 | + edev->ptp_skip_txts++; |
|---|
| 160 | + } else { |
|---|
| 161 | + /* Reschedule to keep checking for a valid TS value */ |
|---|
| 162 | + schedule_work(&ptp->work); |
|---|
| 163 | + } |
|---|
| 175 | 164 | return; |
|---|
| 176 | 165 | } |
|---|
| 177 | 166 | |
|---|
| .. | .. |
|---|
| 223 | 212 | |
|---|
| 224 | 213 | switch (ptp->tx_type) { |
|---|
| 225 | 214 | case HWTSTAMP_TX_ON: |
|---|
| 226 | | - edev->flags |= QEDE_TX_TIMESTAMPING_EN; |
|---|
| 215 | + set_bit(QEDE_FLAGS_TX_TIMESTAMPING_EN, &edev->flags); |
|---|
| 227 | 216 | tx_type = QED_PTP_HWTSTAMP_TX_ON; |
|---|
| 228 | 217 | break; |
|---|
| 229 | 218 | |
|---|
| 230 | 219 | case HWTSTAMP_TX_OFF: |
|---|
| 231 | | - edev->flags &= ~QEDE_TX_TIMESTAMPING_EN; |
|---|
| 220 | + clear_bit(QEDE_FLAGS_TX_TIMESTAMPING_EN, &edev->flags); |
|---|
| 232 | 221 | tx_type = QED_PTP_HWTSTAMP_TX_OFF; |
|---|
| 233 | 222 | break; |
|---|
| 234 | 223 | |
|---|
| 235 | 224 | case HWTSTAMP_TX_ONESTEP_SYNC: |
|---|
| 225 | + case HWTSTAMP_TX_ONESTEP_P2P: |
|---|
| 236 | 226 | DP_ERR(edev, "One-step timestamping is not supported\n"); |
|---|
| 237 | 227 | return -ERANGE; |
|---|
| 238 | 228 | } |
|---|
| .. | .. |
|---|
| 397 | 387 | if (ptp->tx_skb) { |
|---|
| 398 | 388 | dev_kfree_skb_any(ptp->tx_skb); |
|---|
| 399 | 389 | ptp->tx_skb = NULL; |
|---|
| 390 | + clear_bit_unlock(QEDE_FLAGS_PTP_TX_IN_PRORGESS, &edev->flags); |
|---|
| 400 | 391 | } |
|---|
| 401 | 392 | |
|---|
| 402 | 393 | /* Disable PTP in HW */ |
|---|
| .. | .. |
|---|
| 408 | 399 | edev->ptp = NULL; |
|---|
| 409 | 400 | } |
|---|
| 410 | 401 | |
|---|
| 411 | | -static int qede_ptp_init(struct qede_dev *edev, bool init_tc) |
|---|
| 402 | +static int qede_ptp_init(struct qede_dev *edev) |
|---|
| 412 | 403 | { |
|---|
| 413 | 404 | struct qede_ptp *ptp; |
|---|
| 414 | 405 | int rc; |
|---|
| .. | .. |
|---|
| 429 | 420 | /* Init work queue for Tx timestamping */ |
|---|
| 430 | 421 | INIT_WORK(&ptp->work, qede_ptp_task); |
|---|
| 431 | 422 | |
|---|
| 432 | | - /* Init cyclecounter and timecounter. This is done only in the first |
|---|
| 433 | | - * load. If done in every load, PTP application will fail when doing |
|---|
| 434 | | - * unload / load (e.g. MTU change) while it is running. |
|---|
| 435 | | - */ |
|---|
| 436 | | - if (init_tc) { |
|---|
| 437 | | - memset(&ptp->cc, 0, sizeof(ptp->cc)); |
|---|
| 438 | | - ptp->cc.read = qede_ptp_read_cc; |
|---|
| 439 | | - ptp->cc.mask = CYCLECOUNTER_MASK(64); |
|---|
| 440 | | - ptp->cc.shift = 0; |
|---|
| 441 | | - ptp->cc.mult = 1; |
|---|
| 423 | + /* Init cyclecounter and timecounter */ |
|---|
| 424 | + memset(&ptp->cc, 0, sizeof(ptp->cc)); |
|---|
| 425 | + ptp->cc.read = qede_ptp_read_cc; |
|---|
| 426 | + ptp->cc.mask = CYCLECOUNTER_MASK(64); |
|---|
| 427 | + ptp->cc.shift = 0; |
|---|
| 428 | + ptp->cc.mult = 1; |
|---|
| 442 | 429 | |
|---|
| 443 | | - timecounter_init(&ptp->tc, &ptp->cc, |
|---|
| 444 | | - ktime_to_ns(ktime_get_real())); |
|---|
| 445 | | - } |
|---|
| 430 | + timecounter_init(&ptp->tc, &ptp->cc, ktime_to_ns(ktime_get_real())); |
|---|
| 446 | 431 | |
|---|
| 447 | | - return rc; |
|---|
| 432 | + return 0; |
|---|
| 448 | 433 | } |
|---|
| 449 | 434 | |
|---|
| 450 | | -int qede_ptp_enable(struct qede_dev *edev, bool init_tc) |
|---|
| 435 | +int qede_ptp_enable(struct qede_dev *edev) |
|---|
| 451 | 436 | { |
|---|
| 452 | 437 | struct qede_ptp *ptp; |
|---|
| 453 | 438 | int rc; |
|---|
| .. | .. |
|---|
| 468 | 453 | |
|---|
| 469 | 454 | edev->ptp = ptp; |
|---|
| 470 | 455 | |
|---|
| 471 | | - rc = qede_ptp_init(edev, init_tc); |
|---|
| 456 | + rc = qede_ptp_init(edev); |
|---|
| 472 | 457 | if (rc) |
|---|
| 473 | 458 | goto err1; |
|---|
| 474 | 459 | |
|---|
| .. | .. |
|---|
| 514 | 499 | if (!ptp) |
|---|
| 515 | 500 | return; |
|---|
| 516 | 501 | |
|---|
| 517 | | - if (test_and_set_bit_lock(QEDE_FLAGS_PTP_TX_IN_PRORGESS, &edev->flags)) |
|---|
| 502 | + if (test_and_set_bit_lock(QEDE_FLAGS_PTP_TX_IN_PRORGESS, |
|---|
| 503 | + &edev->flags)) { |
|---|
| 504 | + DP_ERR(edev, "Timestamping in progress\n"); |
|---|
| 505 | + edev->ptp_skip_txts++; |
|---|
| 518 | 506 | return; |
|---|
| 507 | + } |
|---|
| 519 | 508 | |
|---|
| 520 | | - if (unlikely(!(edev->flags & QEDE_TX_TIMESTAMPING_EN))) { |
|---|
| 521 | | - DP_NOTICE(edev, |
|---|
| 522 | | - "Tx timestamping was not enabled, this packet will not be timestamped\n"); |
|---|
| 509 | + if (unlikely(!test_bit(QEDE_FLAGS_TX_TIMESTAMPING_EN, &edev->flags))) { |
|---|
| 510 | + DP_ERR(edev, |
|---|
| 511 | + "Tx timestamping was not enabled, this packet will not be timestamped\n"); |
|---|
| 512 | + clear_bit_unlock(QEDE_FLAGS_PTP_TX_IN_PRORGESS, &edev->flags); |
|---|
| 513 | + edev->ptp_skip_txts++; |
|---|
| 523 | 514 | } else if (unlikely(ptp->tx_skb)) { |
|---|
| 524 | | - DP_NOTICE(edev, |
|---|
| 525 | | - "The device supports only a single outstanding packet to timestamp, this packet will not be timestamped\n"); |
|---|
| 515 | + DP_ERR(edev, |
|---|
| 516 | + "The device supports only a single outstanding packet to timestamp, this packet will not be timestamped\n"); |
|---|
| 517 | + clear_bit_unlock(QEDE_FLAGS_PTP_TX_IN_PRORGESS, &edev->flags); |
|---|
| 518 | + edev->ptp_skip_txts++; |
|---|
| 526 | 519 | } else { |
|---|
| 527 | 520 | skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; |
|---|
| 528 | 521 | /* schedule check for Tx timestamp */ |
|---|
| 529 | 522 | ptp->tx_skb = skb_get(skb); |
|---|
| 523 | + ptp->ptp_tx_start = jiffies; |
|---|
| 530 | 524 | schedule_work(&ptp->work); |
|---|
| 531 | 525 | } |
|---|
| 532 | 526 | } |
|---|