|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright(c) 2024 Intel Corporation. */ |
| 3 | + |
| 4 | +#include "iavf.h" |
| 5 | +#include "iavf_ptp.h" |
| 6 | + |
| 7 | +/** |
| 8 | + * iavf_ptp_cap_supported - Check if a PTP capability is supported |
| 9 | + * @adapter: private adapter structure |
| 10 | + * @cap: the capability bitmask to check |
| 11 | + * |
| 12 | + * Return: true if every capability set in cap is also set in the enabled |
| 13 | + * capabilities reported by the PF, false otherwise. |
| 14 | + */ |
| 15 | +bool iavf_ptp_cap_supported(const struct iavf_adapter *adapter, u32 cap) |
| 16 | +{ |
| 17 | + if (!IAVF_PTP_ALLOWED(adapter)) |
| 18 | + return false; |
| 19 | + |
| 20 | + /* Only return true if every bit in cap is set in hw_caps.caps */ |
| 21 | + return (adapter->ptp.hw_caps.caps & cap) == cap; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * iavf_ptp_register_clock - Register a new PTP for userspace |
| 26 | + * @adapter: private adapter structure |
| 27 | + * |
| 28 | + * Allocate and register a new PTP clock device if necessary. |
| 29 | + * |
| 30 | + * Return: 0 if success, error otherwise. |
| 31 | + */ |
| 32 | +static int iavf_ptp_register_clock(struct iavf_adapter *adapter) |
| 33 | +{ |
| 34 | + struct ptp_clock_info *ptp_info = &adapter->ptp.info; |
| 35 | + struct device *dev = &adapter->pdev->dev; |
| 36 | + struct ptp_clock *clock; |
| 37 | + |
| 38 | + snprintf(ptp_info->name, sizeof(ptp_info->name), "%s-%s-clk", |
| 39 | + KBUILD_MODNAME, dev_name(dev)); |
| 40 | + ptp_info->owner = THIS_MODULE; |
| 41 | + |
| 42 | + clock = ptp_clock_register(ptp_info, dev); |
| 43 | + if (IS_ERR(clock)) |
| 44 | + return PTR_ERR(clock); |
| 45 | + |
| 46 | + adapter->ptp.clock = clock; |
| 47 | + |
| 48 | + dev_dbg(&adapter->pdev->dev, "PTP clock %s registered\n", |
| 49 | + adapter->ptp.info.name); |
| 50 | + |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * iavf_ptp_init - Initialize PTP support if capability was negotiated |
| 56 | + * @adapter: private adapter structure |
| 57 | + * |
| 58 | + * Initialize PTP functionality, based on the capabilities that the PF has |
| 59 | + * enabled for this VF. |
| 60 | + */ |
| 61 | +void iavf_ptp_init(struct iavf_adapter *adapter) |
| 62 | +{ |
| 63 | + int err; |
| 64 | + |
| 65 | + if (!iavf_ptp_cap_supported(adapter, VIRTCHNL_1588_PTP_CAP_READ_PHC)) { |
| 66 | + pci_notice(adapter->pdev, |
| 67 | + "Device does not have PTP clock support\n"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + err = iavf_ptp_register_clock(adapter); |
| 72 | + if (err) { |
| 73 | + pci_err(adapter->pdev, |
| 74 | + "Failed to register PTP clock device (%p)\n", |
| 75 | + ERR_PTR(err)); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + for (int i = 0; i < adapter->num_active_queues; i++) { |
| 80 | + struct iavf_ring *rx_ring = &adapter->rx_rings[i]; |
| 81 | + |
| 82 | + rx_ring->ptp = &adapter->ptp; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * iavf_ptp_release - Disable PTP support |
| 88 | + * @adapter: private adapter structure |
| 89 | + * |
| 90 | + * Release all PTP resources that were previously initialized. |
| 91 | + */ |
| 92 | +void iavf_ptp_release(struct iavf_adapter *adapter) |
| 93 | +{ |
| 94 | + if (!adapter->ptp.clock) |
| 95 | + return; |
| 96 | + |
| 97 | + pci_dbg(adapter->pdev, "removing PTP clock %s\n", |
| 98 | + adapter->ptp.info.name); |
| 99 | + ptp_clock_unregister(adapter->ptp.clock); |
| 100 | + adapter->ptp.clock = NULL; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * iavf_ptp_process_caps - Handle change in PTP capabilities |
| 105 | + * @adapter: private adapter structure |
| 106 | + * |
| 107 | + * Handle any state changes necessary due to change in PTP capabilities, such |
| 108 | + * as after a device reset or change in configuration from the PF. |
| 109 | + */ |
| 110 | +void iavf_ptp_process_caps(struct iavf_adapter *adapter) |
| 111 | +{ |
| 112 | + bool phc = iavf_ptp_cap_supported(adapter, VIRTCHNL_1588_PTP_CAP_READ_PHC); |
| 113 | + |
| 114 | + /* Check if the device gained or lost necessary access to support the |
| 115 | + * PTP hardware clock. If so, driver must respond appropriately by |
| 116 | + * creating or destroying the PTP clock device. |
| 117 | + */ |
| 118 | + if (adapter->ptp.clock && !phc) |
| 119 | + iavf_ptp_release(adapter); |
| 120 | + else if (!adapter->ptp.clock && phc) |
| 121 | + iavf_ptp_init(adapter); |
| 122 | +} |
0 commit comments