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 #include "ice.h" 13 14 static DEFINE_XARRAY(ice_adapters); 15 static DEFINE_MUTEX(ice_adapters_mutex); 16 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) 22 23 static unsigned long ice_adapter_index(const struct pci_dev *pdev) 24 { 25 unsigned int domain = pci_domain_nr(pdev->bus); 26 27 WARN_ON(domain > FIELD_MAX(INDEX_FIELD_DOMAIN)); 28 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); 35 default: 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)); 39 } 40 } 41 42 static struct ice_adapter *ice_adapter_new(void) 43 { 44 struct ice_adapter *adapter; 45 46 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL); 47 if (!adapter) 48 return NULL; 49 50 spin_lock_init(&adapter->ptp_gltsyn_time_lock); 51 refcount_set(&adapter->refcount, 1); 52 53 mutex_init(&adapter->ports.lock); 54 INIT_LIST_HEAD(&adapter->ports.ports); 55 56 return adapter; 57 } 58 59 static void ice_adapter_free(struct ice_adapter *adapter) 60 { 61 WARN_ON(!list_empty(&adapter->ports.ports)); 62 mutex_destroy(&adapter->ports.lock); 63 64 kfree(adapter); 65 } 66 67 /** 68 * ice_adapter_get - Get a shared ice_adapter structure. 69 * @pdev: Pointer to the pci_dev whose driver is getting the ice_adapter. 70 * 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. 75 * 76 * Context: Process, may sleep. 77 * Return: Pointer to ice_adapter on success. 78 * ERR_PTR() on error. -ENOMEM is the only possible error. 79 */ 80 struct ice_adapter *ice_adapter_get(const struct pci_dev *pdev) 81 { 82 unsigned long index = ice_adapter_index(pdev); 83 struct ice_adapter *adapter; 84 int err; 85 86 scoped_guard(mutex, &ice_adapters_mutex) { 87 err = xa_insert(&ice_adapters, index, NULL, GFP_KERNEL); 88 if (err == -EBUSY) { 89 adapter = xa_load(&ice_adapters, index); 90 refcount_inc(&adapter->refcount); 91 return adapter; 92 } 93 if (err) 94 return ERR_PTR(err); 95 96 adapter = ice_adapter_new(); 97 if (!adapter) 98 return ERR_PTR(-ENOMEM); 99 xa_store(&ice_adapters, index, adapter, GFP_KERNEL); 100 } 101 return adapter; 102 } 103 104 /** 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. 107 * 108 * Releases the reference to ice_adapter previously obtained with 109 * ice_adapter_get. 110 * 111 * Context: Process, may sleep. 112 */ 113 void ice_adapter_put(const struct pci_dev *pdev) 114 { 115 unsigned long index = ice_adapter_index(pdev); 116 struct ice_adapter *adapter; 117 118 scoped_guard(mutex, &ice_adapters_mutex) { 119 adapter = xa_load(&ice_adapters, index); 120 if (WARN_ON(!adapter)) 121 return; 122 if (!refcount_dec_and_test(&adapter->refcount)) 123 return; 124 125 WARN_ON(xa_erase(&ice_adapters, index) != adapter); 126 } 127 ice_adapter_free(adapter); 128 } 129