NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
nvme_pci_common.h
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 #ifndef NVME_PCI_COMMON_H_
27 #define NVME_PCI_COMMON_H_
28 
29 #include <stdbool.h>
30 
31 #include <doca_error.h>
32 #include <doca_argp.h>
33 #include <doca_log.h>
34 #include <doca_devemu_pci_type.h>
35 #include <doca_devemu_pci.h>
36 #include <doca_dpa.h>
37 
38 #include "nvme_pci_type_config.h"
39 
40 #define SPDK_APP_DEBUG
41 
42 /* Function to check if a given device supports PCI emulation */
43 typedef doca_error_t (*emulation_supported_cb_t)(const struct doca_devinfo *,
44  const struct doca_devemu_pci_type *pci_type,
45  uint8_t *is_supported);
46 
47 /*
48  * Open a DOCA device according to a given device name
49  * Picks a device that has given name and supports hotplug of the PCI type
50  *
51  * @dev_name [in]: device name
52  * @pci_type [in]: The emulated PCI type
53  * @has_support [in]: Method to check if device hsupports emulation
54  * @dev [out]: pointer to doca_dev struct, NULL if not found
55  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
56  */
57 doca_error_t find_supported_device(const char *dev_name,
58  const struct doca_devemu_pci_type *pci_type,
59  emulation_supported_cb_t has_support,
60  struct doca_dev **dev);
61 /*
62  * Open an emulated PCI device representor according to type and given VUID
63  *
64  * @pci_type [in]: The emulated PCI type
65  * @vuid [in]: The VUID of the emulated device
66  * @rep [out]: pointer to doca_dev_rep struct, NULL if not found
67  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
68  */
69 doca_error_t find_emulated_device(struct doca_devemu_pci_type *pci_type, const char *vuid, struct doca_dev_rep **rep);
70 
71 /*
72  * Sets the PCI configurations of the type and then starts it
73  * Once device is hotplugged the configurations will be visible to the Host as part of the
74  * PCI configuration space of that device
75  *
76  * @pci_type [in]: The emulated PCI type
77  * @dev [in]: The device that manages the PCI type
78  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
79  */
80 doca_error_t configure_and_start_pci_type(struct doca_devemu_pci_type *pci_type, struct doca_dev *dev);
81 
82 /*
83  * Creates a pci type, selects a device with the specified PCI device name and supports hotplug for the PCI type,
84  * configures the PCI settings for the type, and then starts it.
85  *
86  * @dev_name [in]: The emulated device name
87  * @pci_type [out]: The PCI type*
88  * @dev [out]: The device that manages the PCI type
89  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
90  */
91 doca_error_t create_find_start_pci_type(char *dev_name, struct doca_devemu_pci_type **pci_type, struct doca_dev **dev);
92 
93 /*
94  * Frees the given resources.
95  *
96  * @pci_type [in]: The PCI type*
97  * @dev [in]: The device that manages the PCI type
98  */
99 void cleanup_pci_resources(struct doca_devemu_pci_type *pci_type, struct doca_dev *dev);
100 
101 /*
102  * Convert enum doca_devemu_pci_hotplug_state to string
103  *
104  * @hotplug_state [in]: The hotplug state to convert
105  * @return: String representation of the hotplug state
106  */
107 const char *hotplug_state_to_string(enum doca_devemu_pci_hotplug_state hotplug_state);
108 
109 #ifdef SPDK_APP_DEBUG
110 /*
111  * Create a string Hex dump representation of the given input buffer
112  *
113  * @data [in]: Pointer to the input buffer
114  * @size [in]: Number of bytes to be analyzed
115  * @return: pointer to the string representation, or NULL if an error was encountered
116  */
117 char *hex_dump(const void *data, size_t size);
118 #endif // SPDK_APP_DEBUG
119 
120 #endif // DEVEMU_PCI_COMMON_H_
doca_devemu_pci_hotplug_state
DOCA devemu pci hotplug state.
enum doca_error doca_error_t
DOCA API return codes.
char * hex_dump(const void *data, size_t size)
doca_error_t configure_and_start_pci_type(struct doca_devemu_pci_type *pci_type, struct doca_dev *dev)
void cleanup_pci_resources(struct doca_devemu_pci_type *pci_type, struct doca_dev *dev)
doca_error_t create_find_start_pci_type(char *dev_name, struct doca_devemu_pci_type **pci_type, struct doca_dev **dev)
const char * hotplug_state_to_string(enum doca_devemu_pci_hotplug_state hotplug_state)
doca_error_t find_emulated_device(struct doca_devemu_pci_type *pci_type, const char *vuid, struct doca_dev_rep **rep)
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)
doca_error_t(* emulation_supported_cb_t)(const struct doca_devinfo *, const struct doca_devemu_pci_type *pci_type, uint8_t *is_supported)