|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// SPDX-FileCopyrightText: Copyright Red Hat |
| 3 | + |
| 4 | +#include <linux/bitfield.h> |
| 5 | +#include <linux/cleanup.h> |
| 6 | +#include <linux/mutex.h> |
| 7 | +#include <linux/pci.h> |
| 8 | +#include <linux/slab.h> |
| 9 | +#include <linux/xarray.h> |
| 10 | +#include "ice_adapter.h" |
| 11 | + |
| 12 | +static DEFINE_XARRAY(ice_adapters); |
| 13 | + |
| 14 | +/* PCI bus number is 8 bits. Slot is 5 bits. Domain can have the rest. */ |
| 15 | +#define INDEX_FIELD_DOMAIN GENMASK(BITS_PER_LONG - 1, 13) |
| 16 | +#define INDEX_FIELD_BUS GENMASK(12, 5) |
| 17 | +#define INDEX_FIELD_SLOT GENMASK(4, 0) |
| 18 | + |
| 19 | +static unsigned long ice_adapter_index(const struct pci_dev *pdev) |
| 20 | +{ |
| 21 | + unsigned int domain = pci_domain_nr(pdev->bus); |
| 22 | + |
| 23 | + WARN_ON(domain > FIELD_MAX(INDEX_FIELD_DOMAIN)); |
| 24 | + |
| 25 | + return FIELD_PREP(INDEX_FIELD_DOMAIN, domain) | |
| 26 | + FIELD_PREP(INDEX_FIELD_BUS, pdev->bus->number) | |
| 27 | + FIELD_PREP(INDEX_FIELD_SLOT, PCI_SLOT(pdev->devfn)); |
| 28 | +} |
| 29 | + |
| 30 | +static struct ice_adapter *ice_adapter_new(void) |
| 31 | +{ |
| 32 | + struct ice_adapter *adapter; |
| 33 | + |
| 34 | + adapter = kzalloc(sizeof(*adapter), GFP_KERNEL); |
| 35 | + if (!adapter) |
| 36 | + return NULL; |
| 37 | + |
| 38 | + refcount_set(&adapter->refcount, 1); |
| 39 | + |
| 40 | + return adapter; |
| 41 | +} |
| 42 | + |
| 43 | +static void ice_adapter_free(struct ice_adapter *adapter) |
| 44 | +{ |
| 45 | + kfree(adapter); |
| 46 | +} |
| 47 | + |
| 48 | +DEFINE_FREE(ice_adapter_free, struct ice_adapter*, if (_T) ice_adapter_free(_T)) |
| 49 | + |
| 50 | +/** |
| 51 | + * ice_adapter_get - Get a shared ice_adapter structure. |
| 52 | + * @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. |
| 53 | + * |
| 54 | + * Gets a pointer to a shared ice_adapter structure. Physical functions (PFs) |
| 55 | + * of the same multi-function PCI device share one ice_adapter structure. |
| 56 | + * The ice_adapter is reference-counted. The PF driver must use ice_adapter_put |
| 57 | + * to release its reference. |
| 58 | + * |
| 59 | + * Context: Process, may sleep. |
| 60 | + * Return: Pointer to ice_adapter on success. |
| 61 | + * ERR_PTR() on error. -ENOMEM is the only possible error. |
| 62 | + */ |
| 63 | +struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev) |
| 64 | +{ |
| 65 | + struct ice_adapter *ret, __free(ice_adapter_free) *adapter = NULL; |
| 66 | + unsigned long index = ice_adapter_index(pdev); |
| 67 | + |
| 68 | + adapter = ice_adapter_new(); |
| 69 | + if (!adapter) |
| 70 | + return ERR_PTR(-ENOMEM); |
| 71 | + |
| 72 | + xa_lock(&ice_adapters); |
| 73 | + ret = __xa_cmpxchg(&ice_adapters, index, NULL, adapter, GFP_KERNEL); |
| 74 | + if (xa_is_err(ret)) { |
| 75 | + ret = ERR_PTR(xa_err(ret)); |
| 76 | + goto unlock; |
| 77 | + } |
| 78 | + if (ret) { |
| 79 | + refcount_inc(&ret->refcount); |
| 80 | + goto unlock; |
| 81 | + } |
| 82 | + ret = no_free_ptr(adapter); |
| 83 | +unlock: |
| 84 | + xa_unlock(&ice_adapters); |
| 85 | + return ret; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * ice_adapter_put - Release a reference to the shared ice_adapter structure. |
| 90 | + * @pdev: Pointer to the pci_dev whose driver is releasing the ice_adapter. |
| 91 | + * |
| 92 | + * Releases the reference to ice_adapter previously obtained with |
| 93 | + * ice_adapter_get. |
| 94 | + * |
| 95 | + * Context: Any. |
| 96 | + */ |
| 97 | +void ice_adapter_put(const struct pci_dev *pdev) |
| 98 | +{ |
| 99 | + unsigned long index = ice_adapter_index(pdev); |
| 100 | + struct ice_adapter *adapter; |
| 101 | + |
| 102 | + xa_lock(&ice_adapters); |
| 103 | + adapter = xa_load(&ice_adapters, index); |
| 104 | + if (WARN_ON(!adapter)) |
| 105 | + goto unlock; |
| 106 | + |
| 107 | + if (!refcount_dec_and_test(&adapter->refcount)) |
| 108 | + goto unlock; |
| 109 | + |
| 110 | + WARN_ON(__xa_erase(&ice_adapters, index) != adapter); |
| 111 | + ice_adapter_free(adapter); |
| 112 | +unlock: |
| 113 | + xa_unlock(&ice_adapters); |
| 114 | +} |
0 commit comments