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