1 // SPDX-License-Identifier: GPL-2.0-only
2 // SPDX-FileCopyrightText: Copyright Red Hat
4 #include <linux/bitfield.h>
5 #include <linux/cleanup.h>
6 #include <linux/mutex.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
10 #include <linux/xarray.h>
11 #include "ice_adapter.h"
14 static DEFINE_XARRAY(ice_adapters);
15 static DEFINE_MUTEX(ice_adapters_mutex);
17 /* PCI bus number is 8 bits. Slot is 5 bits. Domain can have the rest. */
18 #define INDEX_FIELD_DOMAIN GENMASK(BITS_PER_LONG - 1, 13)
19 #define INDEX_FIELD_DEV GENMASK(31, 16)
20 #define INDEX_FIELD_BUS GENMASK(12, 5)
21 #define INDEX_FIELD_SLOT GENMASK(4, 0)
23 static unsigned long ice_adapter_index(const struct pci_dev *pdev)
25 unsigned int domain = pci_domain_nr(pdev->bus);
27 WARN_ON(domain > FIELD_MAX(INDEX_FIELD_DOMAIN));
29 switch (pdev->device) {
30 case ICE_DEV_ID_E825C_BACKPLANE:
31 case ICE_DEV_ID_E825C_QSFP:
32 case ICE_DEV_ID_E825C_SFP:
33 case ICE_DEV_ID_E825C_SGMII:
34 return FIELD_PREP(INDEX_FIELD_DEV, pdev->device);
36 return FIELD_PREP(INDEX_FIELD_DOMAIN, domain) |
37 FIELD_PREP(INDEX_FIELD_BUS, pdev->bus->number) |
38 FIELD_PREP(INDEX_FIELD_SLOT, PCI_SLOT(pdev->devfn));
42 static struct ice_adapter *ice_adapter_new(void)
44 struct ice_adapter *adapter;
46 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
50 spin_lock_init(&adapter->ptp_gltsyn_time_lock);
51 refcount_set(&adapter->refcount, 1);
53 mutex_init(&adapter->ports.lock);
54 INIT_LIST_HEAD(&adapter->ports.ports);
59 static void ice_adapter_free(struct ice_adapter *adapter)
61 WARN_ON(!list_empty(&adapter->ports.ports));
62 mutex_destroy(&adapter->ports.lock);
68 * ice_adapter_get - Get a shared ice_adapter structure.
69 * @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter.
71 * Gets a pointer to a shared ice_adapter structure. Physical functions (PFs)
72 * of the same multi-function PCI device share one ice_adapter structure.
73 * The ice_adapter is reference-counted. The PF driver must use ice_adapter_put
74 * to release its reference.
76 * Context: Process, may sleep.
77 * Return: Pointer to ice_adapter on success.
78 * ERR_PTR() on error. -ENOMEM is the only possible error.
80 struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev)
82 unsigned long index = ice_adapter_index(pdev);
83 struct ice_adapter *adapter;
86 scoped_guard(mutex, &ice_adapters_mutex) {
87 err = xa_insert(&ice_adapters, index, NULL, GFP_KERNEL);
89 adapter = xa_load(&ice_adapters, index);
90 refcount_inc(&adapter->refcount);
96 adapter = ice_adapter_new();
98 return ERR_PTR(-ENOMEM);
99 xa_store(&ice_adapters, index, adapter, GFP_KERNEL);
105 * ice_adapter_put - Release a reference to the shared ice_adapter structure.
106 * @pdev: Pointer to the pci_dev whose driver is releasing the ice_adapter.
108 * Releases the reference to ice_adapter previously obtained with
111 * Context: Process, may sleep.
113 void ice_adapter_put(const struct pci_dev *pdev)
115 unsigned long index = ice_adapter_index(pdev);
116 struct ice_adapter *adapter;
118 scoped_guard(mutex, &ice_adapters_mutex) {
119 adapter = xa_load(&ice_adapters, index);
120 if (WARN_ON(!adapter))
122 if (!refcount_dec_and_test(&adapter->refcount))
125 WARN_ON(xa_erase(&ice_adapters, index) != adapter);
127 ice_adapter_free(adapter);