blob: 3310841c6c7e48f24d5a8ce478527de1127ae551 [file] [log] [blame]
Damjan Marion38c61912023-10-17 16:06:26 +00001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2023 Cisco Systems, Inc.
3 */
4
5#include "vlib/pci/pci.h"
6#include <vnet/vnet.h>
7#include <vnet/dev/dev.h>
8#include <vnet/dev/pci.h>
9#include <vnet/dev/log.h>
10
11VLIB_REGISTER_LOG_CLASS (dev_log, static) = {
12 .class_name = "dev",
13 .subclass_name = "pci",
14 .default_syslog_level = VLIB_LOG_LEVEL_DEBUG,
15};
16
17static int
18vnet_dev_bus_pci_device_id_to_pci_addr (vlib_pci_addr_t *addr, char *str)
19{
20 unformat_input_t input;
21 uword rv;
22 unformat_init_string (&input, str, strlen (str));
23 rv = unformat (&input, "pci" VNET_DEV_DEVICE_ID_PREFIX_DELIMITER "%U",
24 unformat_vlib_pci_addr, addr);
25 unformat_free (&input);
26 return rv;
27}
28
29static void *
30vnet_dev_bus_pci_get_device_info (vlib_main_t *vm, char *device_id)
31{
32 vnet_dev_bus_pci_device_info_t *info;
33 vlib_pci_addr_t addr = {};
34 clib_error_t *err = 0;
35 vlib_pci_device_info_t *di = 0;
36
37 vlib_log_debug (dev_log.class, "device %s", device_id);
38
39 if (vnet_dev_bus_pci_device_id_to_pci_addr (&addr, device_id) == 0)
40 return 0;
41
42 di = vlib_pci_get_device_info (vm, &addr, &err);
43 if (err)
44 {
45 vlib_log_err (dev_log.class, "get_device_info: %U", format_clib_error,
46 err);
47 clib_error_free (err);
48 return 0;
49 }
50
51 info = clib_mem_alloc (sizeof (vnet_dev_bus_pci_device_info_t));
52 info->addr = addr;
53 info->vendor_id = di->vendor_id;
54 info->device_id = di->device_id;
55 info->revision = di->revision;
56
57 vlib_pci_free_device_info (di);
58 return info;
59}
60
61static void
62vnet_dev_bus_pci_free_device_info (vlib_main_t *vm, void *dev_info)
63{
64 clib_mem_free (dev_info);
65}
66
67static vnet_dev_rv_t
68vnet_dev_bus_pci_open (vlib_main_t *vm, vnet_dev_t *dev)
69{
70 clib_error_t *err = 0;
71 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
72
73 if (vnet_dev_bus_pci_device_id_to_pci_addr (&pdd->addr, dev->device_id) == 0)
74 return VNET_DEV_ERR_INVALID_DEVICE_ID;
75
76 if ((err = vlib_pci_device_open (vm, &pdd->addr, 0, &pdd->handle)))
77 {
78 log_err (dev, "device_open: %U", format_clib_error, err);
79 clib_error_free (err);
80 return VNET_DEV_ERR_BUS;
81 }
82
83 dev->numa_node = vlib_pci_get_numa_node (vm, pdd->handle);
84
85 if (vlib_pci_supports_virtual_addr_dma (vm, pdd->handle))
86 {
87 dev->va_dma = 1;
88 log_debug (dev, "device supports VA DMA");
89 }
90
91 vlib_pci_set_private_data (vm, pdd->handle, (uword) dev);
92
93 pdd->n_msix_int = vlib_pci_get_num_msix_interrupts (vm, pdd->handle);
94 if (pdd->n_msix_int)
95 {
96 u32 sz = sizeof (pdd->msix_handlers[0]) * pdd->n_msix_int;
97 sz = round_pow2 (sz, CLIB_CACHE_LINE_BYTES);
98 pdd->msix_handlers = clib_mem_alloc_aligned (sz, CLIB_CACHE_LINE_BYTES);
99 clib_memset (pdd->msix_handlers, 0, sz);
100 }
101
102 return VNET_DEV_OK;
103}
104
105static void
106vnet_dev_bus_pci_close (vlib_main_t *vm, vnet_dev_t *dev)
107{
108 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
109
110 if (pdd->intx_handler)
111 vnet_dev_pci_intx_remove_handler (vm, dev);
112
113 if (pdd->msix_handlers)
114 {
115 for (u16 i = 0; i < pdd->n_msix_int; i++)
116 if (pdd->msix_handlers[i])
117 vnet_dev_pci_msix_remove_handler (vm, dev, i, 1);
118 clib_mem_free (pdd->msix_handlers);
119 pdd->msix_handlers = 0;
120 }
121
122 if (pdd->pci_handle_valid)
123 vlib_pci_device_close (vm, pdd->handle);
124}
125
126static vnet_dev_rv_t
127vnet_dev_bus_pci_dma_mem_alloc (vlib_main_t *vm, vnet_dev_t *dev, u32 size,
128 u32 align, void **pp)
129{
130 clib_error_t *err;
131 void *p;
132
133 align = align ? align : CLIB_CACHE_LINE_BYTES;
134 size = round_pow2 (size, align);
135
136 p = vlib_physmem_alloc_aligned_on_numa (vm, size, align, dev->numa_node);
137
138 if (p == 0)
139 {
140 err = vlib_physmem_last_error (vm);
141 log_err (dev, "dev_dma_mem_alloc: physmem_alloc_aligned error %U",
142 format_clib_error, err);
143 clib_error_free (err);
144 return VNET_DEV_ERR_DMA_MEM_ALLOC_FAIL;
145 }
146
147 if ((err = vlib_pci_map_dma (vm, vnet_dev_get_pci_handle (dev), p)))
148 {
149 log_err (dev, "dev_dma_mem_alloc: pci_map_dma: %U", format_clib_error,
150 err);
151 clib_error_free (err);
152 return VNET_DEV_ERR_DMA_MEM_ALLOC_FAIL;
153 }
154
155 clib_memset (p, 0, size);
156 pp[0] = p;
157 return VNET_DEV_OK;
158}
159
160static void
161vnet_dev_bus_pci_dma_mem_free (vlib_main_t *vm, vnet_dev_t *dev, void *p)
162{
163 if (p)
164 vlib_physmem_free (vm, p);
165}
166
167vnet_dev_rv_t
168vnet_dev_pci_read_config_header (vlib_main_t *vm, vnet_dev_t *dev,
169 vlib_pci_config_hdr_t *hdr)
170{
171 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
172 clib_error_t *err;
173
174 err = vlib_pci_read_write_config (vm, h, VLIB_READ, 0, hdr, sizeof (*hdr));
175 if (err)
176 {
177 log_err (dev, "pci_read_config_header: %U", format_clib_error, err);
178 clib_error_free (err);
179 return VNET_DEV_ERR_BUS;
180 }
181 return VNET_DEV_OK;
182}
183
184vnet_dev_rv_t
185vnet_dev_pci_map_region (vlib_main_t *vm, vnet_dev_t *dev, u8 region,
186 void **pp)
187{
188 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
189 clib_error_t *err;
190
191 if ((err = vlib_pci_map_region (vm, h, region, pp)))
192 {
193 log_err (dev, "pci_map_region: %U", format_clib_error, err);
194 clib_error_free (err);
195 return VNET_DEV_ERR_BUS;
196 }
197
198 return VNET_DEV_OK;
199}
200
201vnet_dev_rv_t
202vnet_dev_pci_function_level_reset (vlib_main_t *vm, vnet_dev_t *dev)
203{
204 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
205 clib_error_t *err;
206
207 if ((err = vlib_pci_function_level_reset (vm, h)))
208 {
209 log_err (dev, "pci_function_level_reset: %U", format_clib_error, err);
210 clib_error_free (err);
211 return VNET_DEV_ERR_BUS;
212 }
213
214 return VNET_DEV_OK;
215}
216
217vnet_dev_rv_t
218vnet_dev_pci_bus_master_enable (vlib_main_t *vm, vnet_dev_t *dev)
219{
220 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
221 clib_error_t *err;
222
223 if ((err = vlib_pci_bus_master_enable (vm, h)))
224 {
225 log_err (dev, "pci_bus_master_enable: %U", format_clib_error, err);
226 clib_error_free (err);
227 return VNET_DEV_ERR_BUS;
228 }
229 return VNET_DEV_OK;
230}
231
232static void
233vnet_dev_pci_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h)
234{
235 vnet_dev_t *dev = (vnet_dev_t *) vlib_pci_get_private_data (vm, h);
236 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
237
238 if (pdd->intx_handler)
239 pdd->intx_handler (vm, dev);
240}
241
242vnet_dev_rv_t
243vnet_dev_pci_intx_add_handler (vlib_main_t *vm, vnet_dev_t *dev,
244 vnet_dev_pci_intx_handler_fn_t *fn)
245{
246 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
247 clib_error_t *err;
248
249 err = vlib_pci_register_intx_handler (vm, h, vnet_dev_pci_intx_handler);
250
251 if (err)
252 {
253 log_err (dev, "pci_register_intx_handler: %U", format_clib_error, err);
254 clib_error_free (err);
255 return VNET_DEV_ERR_BUS;
256 }
257
258 return VNET_DEV_OK;
259}
260
261vnet_dev_rv_t
262vnet_dev_pci_intx_remove_handler (vlib_main_t *vm, vnet_dev_t *dev)
263{
264 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
265 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
266 clib_error_t *err;
267
268 err = vlib_pci_unregister_intx_handler (vm, h);
269
270 if (err)
271 {
272 log_err (dev, "pci_unregister_intx_handler: %U", format_clib_error, err);
273 clib_error_free (err);
274 return VNET_DEV_ERR_BUS;
275 }
276
277 pdd->intx_handler = 0;
278
279 return VNET_DEV_OK;
280}
281
282static void
283vnet_dev_pci_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 line)
284{
285 vnet_dev_t *dev = (vnet_dev_t *) vlib_pci_get_private_data (vm, h);
286 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
287
288 if (line < vec_len (pdd->msix_handlers) && pdd->msix_handlers[line])
289 pdd->msix_handlers[line](vm, dev, line);
290}
291
292vnet_dev_rv_t
293vnet_dev_pci_msix_add_handler (vlib_main_t *vm, vnet_dev_t *dev,
294 vnet_dev_pci_msix_handler_fn_t *fn, u16 first,
295 u16 count)
296{
297 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
298 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
299 clib_error_t *err;
300
301 err = vlib_pci_register_msix_handler (vm, h, first, count,
302 vnet_dev_pci_msix_handler);
303
304 if (err)
305 {
306 log_err (dev, "pci_register_msix_handler: %U", format_clib_error, err);
307 clib_error_free (err);
308 return VNET_DEV_ERR_BUS;
309 }
310
311 for (u16 i = first; i < first + count; i++)
312 {
313 ASSERT (pdd->msix_handlers[i] == 0);
314 pdd->msix_handlers[i] = fn;
315 }
316
317 return VNET_DEV_OK;
318}
319
320vnet_dev_rv_t
321vnet_dev_pci_msix_remove_handler (vlib_main_t *vm, vnet_dev_t *dev, u16 first,
322 u16 count)
323{
324 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
325 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
326 clib_error_t *err;
327
328 err = vlib_pci_unregister_msix_handler (vm, h, first, count);
329
330 if (err)
331 {
332 log_err (dev, "pci_unregister_msix_handler: %U", format_clib_error, err);
333 clib_error_free (err);
334 return VNET_DEV_ERR_BUS;
335 }
336
337 for (u16 i = first; i < first + count; i++)
338 {
339 ASSERT (pdd->msix_handlers[i] != 0);
340 pdd->msix_handlers[i] = 0;
341 }
342
343 return VNET_DEV_OK;
344}
345
346vnet_dev_rv_t
347vnet_dev_pci_msix_enable (vlib_main_t *vm, vnet_dev_t *dev, u16 first,
348 u16 count)
349{
350 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
351 clib_error_t *err;
352
353 err = vlib_pci_enable_msix_irq (vm, h, first, count);
354
355 if (err)
356 {
357 log_err (dev, "pci_enable_msix_irq: %U", format_clib_error, err);
358 clib_error_free (err);
359 return VNET_DEV_ERR_BUS;
360 }
361
362 return VNET_DEV_OK;
363}
364
365vnet_dev_rv_t
366vnet_dev_pci_msix_disable (vlib_main_t *vm, vnet_dev_t *dev, u16 first,
367 u16 count)
368{
369 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
370 clib_error_t *err;
371
372 err = vlib_pci_disable_msix_irq (vm, h, first, count);
373
374 if (err)
375 {
376 log_err (dev, "pci_disble_msix_irq: %U", format_clib_error, err);
377 clib_error_free (err);
378 return VNET_DEV_ERR_BUS;
379 }
380
381 return VNET_DEV_OK;
382}
383
384vnet_dev_rv_t
385vnet_dev_pci_bus_master_disable (vlib_main_t *vm, vnet_dev_t *dev)
386{
387 vlib_pci_dev_handle_t h = vnet_dev_get_pci_handle (dev);
388 clib_error_t *err;
389
390 if ((err = vlib_pci_bus_master_disable (vm, h)))
391 {
392 log_err (dev, "pci_bus_master_disable: %U", format_clib_error, err);
393 clib_error_free (err);
394 return VNET_DEV_ERR_BUS;
395 }
396 return VNET_DEV_OK;
397}
398
399static u8 *
400format_dev_pci_device_info (u8 *s, va_list *args)
401{
402 vnet_dev_format_args_t __clib_unused *a =
403 va_arg (*args, vnet_dev_format_args_t *);
404 vnet_dev_t *dev = va_arg (*args, vnet_dev_t *);
405 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
406 vlib_main_t *vm = vlib_get_main ();
407 vlib_pci_config_t cfg = {};
408 clib_error_t *err;
409
410 s = format (s, "PCIe address is %U", format_vlib_pci_addr, &pdd->addr);
411
412 err = vlib_pci_read_write_config (vm, pdd->handle, VLIB_READ, 0, &cfg,
413 sizeof (cfg));
414 if (!err)
415 {
416 s = format (s, ", port is %U, speed is %U (max %U)",
417 format_vlib_pci_link_port, &cfg, format_vlib_pci_link_speed,
418 &cfg, format_vlib_pci_link_speed_cap, &cfg);
419 }
420 else
421 clib_error_free (err);
422
423 return s;
424}
425
426static u8 *
427format_dev_pci_device_addr (u8 *s, va_list *args)
428{
429 vnet_dev_t *dev = va_arg (*args, vnet_dev_t *);
430 vnet_dev_bus_pci_device_data_t *pdd = vnet_dev_get_bus_pci_device_data (dev);
431 return format (s, "%U", format_vlib_pci_addr, &pdd->addr);
432}
433
434VNET_DEV_REGISTER_BUS (pci) = {
435 .name = "pci",
436 .device_data_size = sizeof (vnet_dev_bus_pci_device_info_t),
437 .ops = {
438 .device_open = vnet_dev_bus_pci_open,
439 .device_close = vnet_dev_bus_pci_close,
440 .get_device_info = vnet_dev_bus_pci_get_device_info,
441 .free_device_info = vnet_dev_bus_pci_free_device_info,
442 .dma_mem_alloc_fn = vnet_dev_bus_pci_dma_mem_alloc,
443 .dma_mem_free_fn = vnet_dev_bus_pci_dma_mem_free,
444 .format_device_info = format_dev_pci_device_info,
445 .format_device_addr = format_dev_pci_device_addr,
446 },
447};