Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright 2020 Mellanox Technologies Ltd 3 : : */ 4 : : 5 : : #include <stdlib.h> 6 : : 7 : : #include <eal_export.h> 8 : : #include <rte_malloc.h> 9 : : #include <rte_devargs.h> 10 : : #include <rte_errno.h> 11 : : #include <rte_class.h> 12 : : #include <rte_pci.h> 13 : : #include <bus_driver.h> 14 : : #include <bus_pci_driver.h> 15 : : 16 : : #include "mlx5_common_log.h" 17 : : #include "mlx5_common_private.h" 18 : : 19 : : static struct rte_pci_driver mlx5_common_pci_driver; 20 : : 21 : : /* PCI ID table is build dynamically based on registered mlx5 drivers. */ 22 : : static struct rte_pci_id *mlx5_pci_id_table; 23 : : 24 : : static int 25 : : pci_id_table_size_get(const struct rte_pci_id *id_table) 26 : : { 27 : : int table_size = 0; 28 : : 29 [ + + + + ]: 31248 : for (; id_table->vendor_id != 0; id_table++) 30 : 29736 : table_size++; 31 : : return table_size; 32 : : } 33 : : 34 : : static bool 35 : : pci_id_exists(const struct rte_pci_id *id, const struct rte_pci_id *table, 36 : : int next_idx) 37 : : { 38 : 9828 : int current_size = next_idx - 1; 39 : : int i; 40 : : 41 [ + + ]: 119700 : for (i = 0; i < current_size; i++) { 42 [ + + ]: 114660 : if (id->device_id == table[i].device_id && 43 : : id->vendor_id == table[i].vendor_id && 44 [ - + ]: 4788 : id->subsystem_vendor_id == table[i].subsystem_vendor_id && 45 : : id->subsystem_device_id == table[i].subsystem_device_id) 46 : : return true; 47 : : } 48 : : return false; 49 : : } 50 : : 51 : : static void 52 : 1260 : pci_id_insert(struct rte_pci_id *new_table, int *next_idx, 53 : : const struct rte_pci_id *id_table) 54 : : { 55 : : /* Traverse the id_table, check if entry exists in new_table; 56 : : * Add non duplicate entries to new table. 57 : : */ 58 [ + + ]: 11088 : for (; id_table->vendor_id != 0; id_table++) { 59 [ + + ]: 19656 : if (!pci_id_exists(id_table, new_table, *next_idx)) { 60 : : /* New entry; add to the table. */ 61 : 5040 : new_table[*next_idx] = *id_table; 62 : 5040 : (*next_idx)++; 63 : : } 64 : : } 65 : 1260 : } 66 : : 67 : : static int 68 : 1512 : pci_ids_table_update(const struct rte_pci_id *driver_id_table) 69 : : { 70 : : const struct rte_pci_id *id_iter; 71 : : struct rte_pci_id *updated_table; 72 : : struct rte_pci_id *old_table; 73 : : int num_ids = 0; 74 : 1512 : int i = 0; 75 : : 76 : 1512 : old_table = mlx5_pci_id_table; 77 [ + + ]: 1512 : if (old_table) 78 : : num_ids = pci_id_table_size_get(old_table); 79 : 1512 : num_ids += pci_id_table_size_get(driver_id_table); 80 : : /* Increase size by one for the termination entry of vendor_id = 0. */ 81 : 1512 : num_ids += 1; 82 : 1512 : updated_table = calloc(num_ids, sizeof(*updated_table)); 83 [ + - ]: 1512 : if (!updated_table) 84 : : return -ENOMEM; 85 [ + + ]: 1512 : if (old_table == NULL) { 86 : : /* Copy the first driver's ID table. */ 87 [ - + ]: 252 : for (id_iter = driver_id_table; id_iter->vendor_id != 0; 88 : 0 : id_iter++, i++) 89 : 0 : updated_table[i] = *id_iter; 90 : : } else { 91 : : /* First copy existing table entries. */ 92 [ + + ]: 21168 : for (id_iter = old_table; id_iter->vendor_id != 0; 93 : 19908 : id_iter++, i++) 94 : 19908 : updated_table[i] = *id_iter; 95 : : /* New id to be added at the end of current ID table. */ 96 : 1260 : pci_id_insert(updated_table, &i, driver_id_table); 97 : : } 98 : : /* Terminate table with empty entry. */ 99 : 1512 : updated_table[i].vendor_id = 0; 100 : 1512 : mlx5_common_pci_driver.id_table = updated_table; 101 : 1512 : mlx5_pci_id_table = updated_table; 102 : 1512 : free(old_table); 103 : 1512 : return 0; 104 : : } 105 : : 106 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_dev_is_pci) 107 : : bool 108 : 0 : mlx5_dev_is_pci(const struct rte_device *dev) 109 : : { 110 : 0 : return strcmp(dev->bus->name, "pci") == 0; 111 : : } 112 : : 113 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_dev_is_vf_pci) 114 : : bool 115 : 0 : mlx5_dev_is_vf_pci(const struct rte_pci_device *pci_dev) 116 : : { 117 [ # # ]: 0 : switch (pci_dev->id.device_id) { 118 : : case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF: 119 : : case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF: 120 : : case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF: 121 : : case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF: 122 : : case PCI_DEVICE_ID_MELLANOX_BLUEFIELDVF: 123 : : case PCI_DEVICE_ID_MELLANOX_CONNECTX6VF: 124 : : case PCI_DEVICE_ID_MELLANOX_CONNECTXVF: 125 : : return true; 126 : : default: 127 : : break; 128 : : } 129 : 0 : return false; 130 : : } 131 : : 132 : : bool 133 : 0 : mlx5_dev_pci_match(const struct mlx5_class_driver *drv, 134 : : const struct rte_device *dev) 135 : : { 136 : : const struct rte_pci_device *pci_dev; 137 : : const struct rte_pci_id *id_table; 138 : : 139 [ # # ]: 0 : if (!mlx5_dev_is_pci(dev)) 140 : : return false; 141 : 0 : pci_dev = RTE_DEV_TO_PCI_CONST(dev); 142 [ # # ]: 0 : for (id_table = drv->id_table; id_table->vendor_id != 0; 143 : 0 : id_table++) { 144 : : /* Check if device's ids match the class driver's ids. */ 145 [ # # # # ]: 0 : if (id_table->vendor_id != pci_dev->id.vendor_id && 146 : : id_table->vendor_id != RTE_PCI_ANY_ID) 147 : 0 : continue; 148 [ # # # # ]: 0 : if (id_table->device_id != pci_dev->id.device_id && 149 : : id_table->device_id != RTE_PCI_ANY_ID) 150 : 0 : continue; 151 : 0 : if (id_table->subsystem_vendor_id != 152 [ # # # # ]: 0 : pci_dev->id.subsystem_vendor_id && 153 : : id_table->subsystem_vendor_id != RTE_PCI_ANY_ID) 154 : 0 : continue; 155 : 0 : if (id_table->subsystem_device_id != 156 [ # # # # ]: 0 : pci_dev->id.subsystem_device_id && 157 : : id_table->subsystem_device_id != RTE_PCI_ANY_ID) 158 : 0 : continue; 159 [ # # # # ]: 0 : if (id_table->class_id != pci_dev->id.class_id && 160 : : id_table->class_id != RTE_CLASS_ANY_ID) 161 : 0 : continue; 162 : : return true; 163 : : } 164 : : return false; 165 : : } 166 : : 167 : : static int 168 : 0 : mlx5_common_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 169 : : struct rte_pci_device *pci_dev) 170 : : { 171 : 0 : return mlx5_common_dev_probe(&pci_dev->device); 172 : : } 173 : : 174 : : static int 175 : 0 : mlx5_common_pci_remove(struct rte_pci_device *pci_dev) 176 : : { 177 : 0 : return mlx5_common_dev_remove(&pci_dev->device); 178 : : } 179 : : 180 : : static int 181 : 0 : mlx5_common_pci_dma_map(struct rte_pci_device *pci_dev, void *addr, 182 : : uint64_t iova, size_t len) 183 : : { 184 : 0 : return mlx5_common_dev_dma_map(&pci_dev->device, addr, iova, len); 185 : : } 186 : : 187 : : static int 188 : 0 : mlx5_common_pci_dma_unmap(struct rte_pci_device *pci_dev, void *addr, 189 : : uint64_t iova, size_t len) 190 : : { 191 : 0 : return mlx5_common_dev_dma_unmap(&pci_dev->device, addr, iova, len); 192 : : } 193 : : 194 : : void 195 : 1260 : mlx5_common_driver_on_register_pci(struct mlx5_class_driver *driver) 196 : : { 197 [ + - ]: 1260 : if (driver->id_table != NULL) { 198 [ + - ]: 1260 : if (pci_ids_table_update(driver->id_table) != 0) 199 : : return; 200 : : } 201 [ + + ]: 1260 : if (driver->probe_again) 202 : 252 : mlx5_common_pci_driver.drv_flags |= RTE_PCI_DRV_PROBE_AGAIN; 203 [ + + ]: 1260 : if (driver->intr_lsc) 204 : 252 : mlx5_common_pci_driver.drv_flags |= RTE_PCI_DRV_INTR_LSC; 205 [ + + ]: 1260 : if (driver->intr_rmv) 206 : 252 : mlx5_common_pci_driver.drv_flags |= RTE_PCI_DRV_INTR_RMV; 207 : : } 208 : : 209 : : static struct rte_pci_driver mlx5_common_pci_driver = { 210 : : .driver = { 211 : : .name = MLX5_PCI_DRIVER_NAME, 212 : : }, 213 : : .probe = mlx5_common_pci_probe, 214 : : .remove = mlx5_common_pci_remove, 215 : : .dma_map = mlx5_common_pci_dma_map, 216 : : .dma_unmap = mlx5_common_pci_dma_unmap, 217 : : }; 218 : : 219 : 252 : void mlx5_common_pci_init(void) 220 : : { 221 : 252 : const struct rte_pci_id empty_table[] = { 222 : : { 223 : : .vendor_id = 0 224 : : }, 225 : : }; 226 : : 227 : : /* All mlx5 PMDs constructor runs at same priority. So any of the PMD 228 : : * including this one can register the PCI table first. If any other 229 : : * PMD(s) have registered the PCI ID table, No need to register an empty 230 : : * default one. 231 : : */ 232 [ + - - + ]: 252 : if (mlx5_pci_id_table == NULL && pci_ids_table_update(empty_table)) 233 : 0 : return; 234 : 252 : rte_pci_register(&mlx5_common_pci_driver); 235 : : } 236 : : 237 : 252 : RTE_FINI(mlx5_common_pci_finish) 238 : : { 239 [ + - ]: 252 : if (mlx5_pci_id_table != NULL) { 240 : : /* Constructor doesn't register with PCI bus if it failed 241 : : * to build the table. 242 : : */ 243 : 252 : rte_pci_unregister(&mlx5_common_pci_driver); 244 : 252 : free(mlx5_pci_id_table); 245 : : } 246 : 252 : } 247 : : 248 : : RTE_PMD_EXPORT_NAME(mlx5_common_pci, __COUNTER__);