xref: /linux/drivers/net/ethernet/intel/ice/ice_adapter.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // SPDX-FileCopyrightText: Copyright Red Hat
3 
4 #include <linux/cleanup.h>
5 #include <linux/mutex.h>
6 #include <linux/pci.h>
7 #include <linux/slab.h>
8 #include <linux/spinlock.h>
9 #include <linux/srcu.h>
10 #include <linux/xarray.h>
11 #include "ice_adapter.h"
12 #include "ice.h"
13 
14 static DEFINE_XARRAY(ice_adapters);
15 static DEFINE_MUTEX(ice_adapters_mutex);
16 
17 #define ICE_ADAPTER_FIXED_INDEX	BIT_ULL(63)
18 
19 #define ICE_ADAPTER_INDEX_E825C	\
20 	(ICE_DEV_ID_E825C_BACKPLANE | ICE_ADAPTER_FIXED_INDEX)
21 
22 static u64 ice_adapter_index(struct pci_dev *pdev)
23 {
24 	switch (pdev->device) {
25 	case ICE_DEV_ID_E825C_BACKPLANE:
26 	case ICE_DEV_ID_E825C_QSFP:
27 	case ICE_DEV_ID_E825C_SFP:
28 	case ICE_DEV_ID_E825C_SGMII:
29 		/* E825C devices have multiple NACs which are connected to the
30 		 * same clock source, and which must share the same
31 		 * ice_adapter structure. We can't use the serial number since
32 		 * each NAC has its own NVM generated with its own unique
33 		 * Device Serial Number. Instead, rely on the embedded nature
34 		 * of the E825C devices, and use a fixed index. This relies on
35 		 * the fact that all E825C physical functions in a given
36 		 * system are part of the same overall device.
37 		 */
38 		return ICE_ADAPTER_INDEX_E825C;
39 	default:
40 		return pci_get_dsn(pdev) & ~ICE_ADAPTER_FIXED_INDEX;
41 	}
42 }
43 
44 static unsigned long ice_adapter_xa_index(struct pci_dev *pdev)
45 {
46 	u64 index = ice_adapter_index(pdev);
47 
48 #if BITS_PER_LONG == 64
49 	return index;
50 #else
51 	return (u32)index ^ (u32)(index >> 32);
52 #endif
53 }
54 
55 static struct ice_adapter *ice_adapter_new(struct pci_dev *pdev)
56 {
57 	struct ice_adapter *adapter;
58 	int err;
59 
60 	adapter = kzalloc_obj(*adapter);
61 	if (!adapter)
62 		return NULL;
63 
64 	err = init_srcu_struct(&adapter->ports.srcu);
65 	if (err) {
66 		kfree(adapter);
67 		return NULL;
68 	}
69 
70 	adapter->index = ice_adapter_index(pdev);
71 	spin_lock_init(&adapter->ptp_gltsyn_time_lock);
72 	spin_lock_init(&adapter->txq_ctx_lock);
73 	for (int i = 0; i < ARRAY_SIZE(adapter->cpi_phy_lock); i++)
74 		mutex_init(&adapter->cpi_phy_lock[i]);
75 	mutex_init(&adapter->cgu_synce_lock);
76 	refcount_set(&adapter->refcount, 1);
77 
78 	spin_lock_init(&adapter->ports.lock);
79 	INIT_LIST_HEAD(&adapter->ports.list);
80 
81 	return adapter;
82 }
83 
84 static void ice_adapter_free(struct ice_adapter *adapter)
85 {
86 	WARN_ON(!list_empty(&adapter->ports.list));
87 	for (int i = 0; i < ARRAY_SIZE(adapter->cpi_phy_lock); i++)
88 		mutex_destroy(&adapter->cpi_phy_lock[i]);
89 	mutex_destroy(&adapter->cgu_synce_lock);
90 
91 	cleanup_srcu_struct(&adapter->ports.srcu);
92 
93 	kfree(adapter);
94 }
95 
96 /**
97  * ice_adapter_get - Get a shared ice_adapter structure.
98  * @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter.
99  *
100  * Gets a pointer to a shared ice_adapter structure. Physical functions (PFs)
101  * of the same multi-function PCI device share one ice_adapter structure.
102  * The ice_adapter is reference-counted. The PF driver must use ice_adapter_put
103  * to release its reference.
104  *
105  * Context: Process, may sleep.
106  * Return:  Pointer to ice_adapter on success.
107  *          ERR_PTR() on error. -ENOMEM is the only possible error.
108  */
109 struct ice_adapter *ice_adapter_get(struct pci_dev *pdev)
110 {
111 	struct ice_adapter *adapter;
112 	unsigned long index;
113 	int err;
114 
115 	index = ice_adapter_xa_index(pdev);
116 	scoped_guard(mutex, &ice_adapters_mutex) {
117 		adapter = xa_load(&ice_adapters, index);
118 		if (adapter) {
119 			refcount_inc(&adapter->refcount);
120 			WARN_ON_ONCE(adapter->index != ice_adapter_index(pdev));
121 			return adapter;
122 		}
123 		err = xa_reserve(&ice_adapters, index, GFP_KERNEL);
124 		if (err)
125 			return ERR_PTR(err);
126 
127 		adapter = ice_adapter_new(pdev);
128 		if (!adapter) {
129 			xa_release(&ice_adapters, index);
130 			return ERR_PTR(-ENOMEM);
131 		}
132 		xa_store(&ice_adapters, index, adapter, GFP_KERNEL);
133 	}
134 	return adapter;
135 }
136 
137 /**
138  * ice_adapter_put - Release a reference to the shared ice_adapter structure.
139  * @pdev: Pointer to the pci_dev whose driver is releasing the ice_adapter.
140  *
141  * Releases the reference to ice_adapter previously obtained with
142  * ice_adapter_get.
143  *
144  * Context: Process, may sleep.
145  */
146 void ice_adapter_put(struct pci_dev *pdev)
147 {
148 	struct ice_adapter *adapter;
149 	unsigned long index;
150 
151 	index = ice_adapter_xa_index(pdev);
152 	scoped_guard(mutex, &ice_adapters_mutex) {
153 		adapter = xa_load(&ice_adapters, index);
154 		if (WARN_ON(!adapter))
155 			return;
156 		if (!refcount_dec_and_test(&adapter->refcount))
157 			return;
158 
159 		WARN_ON(xa_erase(&ice_adapters, index) != adapter);
160 	}
161 	ice_adapter_free(adapter);
162 }
163