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/spinlock.h>
10 #include <linux/xarray.h>
11 #include "ice_adapter.h"
12
13 static DEFINE_XARRAY(ice_adapters);
14 static DEFINE_MUTEX(ice_adapters_mutex);
15
16 /* PCI bus number is 8 bits. Slot is 5 bits. Domain can have the rest. */
17 #define INDEX_FIELD_DOMAIN GENMASK(BITS_PER_LONG - 1, 13)
18 #define INDEX_FIELD_BUS GENMASK(12, 5)
19 #define INDEX_FIELD_SLOT GENMASK(4, 0)
20
ice_adapter_index(const struct pci_dev * pdev)21 static unsigned long ice_adapter_index(const struct pci_dev *pdev)
22 {
23 unsigned int domain = pci_domain_nr(pdev->bus);
24
25 WARN_ON(domain > FIELD_MAX(INDEX_FIELD_DOMAIN));
26
27 return FIELD_PREP(INDEX_FIELD_DOMAIN, domain) |
28 FIELD_PREP(INDEX_FIELD_BUS, pdev->bus->number) |
29 FIELD_PREP(INDEX_FIELD_SLOT, PCI_SLOT(pdev->devfn));
30 }
31
ice_adapter_new(void)32 static struct ice_adapter *ice_adapter_new(void)
33 {
34 struct ice_adapter *adapter;
35
36 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
37 if (!adapter)
38 return NULL;
39
40 spin_lock_init(&adapter->ptp_gltsyn_time_lock);
41 refcount_set(&adapter->refcount, 1);
42
43 return adapter;
44 }
45
ice_adapter_free(struct ice_adapter * adapter)46 static void ice_adapter_free(struct ice_adapter *adapter)
47 {
48 kfree(adapter);
49 }
50
51 /**
52 * ice_adapter_get - Get a shared ice_adapter structure.
53 * @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter.
54 *
55 * Gets a pointer to a shared ice_adapter structure. Physical functions (PFs)
56 * of the same multi-function PCI device share one ice_adapter structure.
57 * The ice_adapter is reference-counted. The PF driver must use ice_adapter_put
58 * to release its reference.
59 *
60 * Context: Process, may sleep.
61 * Return: Pointer to ice_adapter on success.
62 * ERR_PTR() on error. -ENOMEM is the only possible error.
63 */
ice_adapter_get(const struct pci_dev * pdev)64 struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev)
65 {
66 unsigned long index = ice_adapter_index(pdev);
67 struct ice_adapter *adapter;
68 int err;
69
70 scoped_guard(mutex, &ice_adapters_mutex) {
71 err = xa_insert(&ice_adapters, index, NULL, GFP_KERNEL);
72 if (err == -EBUSY) {
73 adapter = xa_load(&ice_adapters, index);
74 refcount_inc(&adapter->refcount);
75 return adapter;
76 }
77 if (err)
78 return ERR_PTR(err);
79
80 adapter = ice_adapter_new();
81 if (!adapter)
82 return ERR_PTR(-ENOMEM);
83 xa_store(&ice_adapters, index, adapter, GFP_KERNEL);
84 }
85 return adapter;
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: Process, may sleep.
96 */
ice_adapter_put(const struct pci_dev * pdev)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 scoped_guard(mutex, &ice_adapters_mutex) {
103 adapter = xa_load(&ice_adapters, index);
104 if (WARN_ON(!adapter))
105 return;
106 if (!refcount_dec_and_test(&adapter->refcount))
107 return;
108
109 WARN_ON(xa_erase(&ice_adapters, index) != adapter);
110 }
111 ice_adapter_free(adapter);
112 }
113