NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_list_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <devemu_pci_common.h>
27 
28 #include <signal.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <time.h>
33 
34 #include <doca_ctx.h>
35 #include <doca_devemu_pci.h>
36 #include <doca_dev.h>
37 #include <doca_error.h>
38 #include <doca_log.h>
39 
40 DOCA_LOG_REGISTER(DPU_DEVEMU_PCI_DEVICE_LIST);
41 
42 /*
43  * Run DOCA Device Emulation List sample
44  *
45  * @pci_address [in]: Device PCI address
46  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
47  */
48 doca_error_t devemu_pci_device_list(const char *pci_address)
49 {
51  struct doca_devinfo_rep **rep_list;
52  uint32_t nb_reps;
53  uint32_t rep_idx;
54  char rep_vuid[DOCA_DEVINFO_REP_VUID_SIZE];
55  char rep_pci[DOCA_DEVINFO_REP_PCI_ADDR_SIZE];
56  struct devemu_resources resources = {0};
57  const char pci_type_name[DOCA_DEVEMU_PCI_TYPE_NAME_LEN] = PCI_TYPE_NAME;
58  bool destroy_rep = false;
59 
60  result = doca_devemu_pci_type_create(pci_type_name, &resources.pci_type);
61  if (result != DOCA_SUCCESS) {
62  DOCA_LOG_ERR("Unable to create PCI type: %s", doca_error_get_descr(result));
63  devemu_resources_cleanup(&resources, destroy_rep);
64  return result;
65  }
66 
67  result = find_supported_device(pci_address,
68  resources.pci_type,
70  &resources.dev);
71  if (result != DOCA_SUCCESS) {
72  devemu_resources_cleanup(&resources, destroy_rep);
73  return result;
74  }
75 
76  /* Set PCIe configuration space values */
78  if (result != DOCA_SUCCESS) {
79  devemu_resources_cleanup(&resources, destroy_rep);
80  return result;
81  }
82 
83  /* Get the list of emulated device representors that match this type */
84  result = doca_devemu_pci_type_create_rep_list(resources.pci_type, &rep_list, &nb_reps);
85  if (result != DOCA_SUCCESS) {
86  DOCA_LOG_ERR("Unable to create list of emulated device representors: %s", doca_error_get_descr(result));
87  devemu_resources_cleanup(&resources, destroy_rep);
88  return result;
89  }
90 
91  DOCA_LOG_INFO("Found a total of %u emulated devices", nb_reps);
92 
93  for (rep_idx = 0; rep_idx < nb_reps; ++rep_idx) {
94  DOCA_LOG_INFO("Emulated PCI device representor:");
95  result = doca_devinfo_rep_get_vuid(rep_list[rep_idx], rep_vuid, DOCA_DEVINFO_REP_VUID_SIZE);
96  DOCA_LOG_INFO("\tVUID:\t%s", result == DOCA_SUCCESS ? rep_vuid : doca_error_get_name(result));
97  result = doca_devinfo_rep_get_pci_addr_str(rep_list[rep_idx], rep_pci);
98  DOCA_LOG_INFO("\tPCI:\t%s", result == DOCA_SUCCESS ? rep_pci : doca_error_get_name(result));
99  }
100 
102 
103  /* Clean and destroy all relevant objects */
104  devemu_resources_cleanup(&resources, destroy_rep);
105 
106  return result;
107 }
int32_t result
void devemu_resources_cleanup(struct devemu_resources *resources, bool destroy_rep)
DOCA_LOG_REGISTER(DPU_DEVEMU_PCI_DEVICE_LIST)
doca_error_t devemu_pci_device_list(const char *pci_address)
#define PCI_TYPE_NAME
struct rdma_resources resources
DOCA_EXPERIMENTAL doca_error_t doca_devemu_pci_type_create_rep_list(struct doca_devemu_pci_type *pci_type, struct doca_devinfo_rep ***dev_list_rep, uint32_t *nb_devs_rep)
Create list of available representor devices for a given DOCA devemu PCI type.
DOCA_EXPERIMENTAL doca_error_t doca_devemu_pci_type_create(const char *name, struct doca_devemu_pci_type **pci_type)
Create a stopped DOCA devemu PCI type.
#define DOCA_DEVEMU_PCI_TYPE_NAME_LEN
Maximal length for the NULL terminated string that describe the name of the emulated PCI device type.
DOCA_EXPERIMENTAL doca_error_t doca_devemu_pci_cap_type_is_mgmt_supported(const struct doca_devinfo *devinfo, const struct doca_devemu_pci_type *pci_type, uint8_t *supported)
Get the management capability of the device for a given DOCA devemu PCI type.
DOCA_STABLE doca_error_t doca_devinfo_rep_destroy_list(struct doca_devinfo_rep **dev_list_rep)
Destroy list of representor device info structures.
#define DOCA_DEVINFO_REP_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:665
DOCA_STABLE doca_error_t doca_devinfo_rep_get_vuid(const struct doca_devinfo_rep *devinfo_rep, char *rep_vuid, uint32_t size)
Get the Vendor Unique ID of a representor DOCA devinfo.
DOCA_STABLE doca_error_t doca_devinfo_rep_get_pci_addr_str(const struct doca_devinfo_rep *devinfo_rep, char *pci_addr_str)
Get the PCI address of a DOCA devinfo_rep.
#define DOCA_DEVINFO_REP_VUID_SIZE
Buffer size to hold VUID. Including a null terminator.
Definition: doca_dev.h:661
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_name(doca_error_t error)
Returns the string representation of an error code name.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_SUCCESS
Definition: doca_error.h:38
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
doca_error_t configure_and_start_pci_type(struct doca_devemu_pci_type *pci_type, struct doca_dev *dev)
doca_error_t find_supported_device(const char *dev_name, const struct doca_devemu_pci_type *pci_type, emulation_supported_cb_t has_support, struct doca_dev **dev)