NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_db_host_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 <errno.h>
27 #include <linux/vfio.h>
28 #include <sys/ioctl.h>
29 #include <sys/eventfd.h>
30 #include <unistd.h>
31 
32 #include <doca_error.h>
33 #include <doca_log.h>
34 
35 #include <common.h>
36 #include <devemu_pci_common.h>
37 #include <devemu_pci_host_common.h>
38 
39 DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_DB_HOST);
40 
41 /*
42  * Run DOCA Device Emulation DB Host sample
43  *
44  * @pci_address [in]: Emulated device PCI address
45  * @vfio_group [in]: VFIO group ID
46  * @region_idx [in]: The index of the DB region
47  * @db_idx [in]: The index of the DB in the DB region
48  * @db_value [in]: The value to write to the DB
49  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
50  */
51 doca_error_t devemu_pci_device_db_host(const char *pci_address,
52  int vfio_group,
53  int region_idx,
54  uint16_t db_idx,
55  uint32_t db_value)
56 {
58  struct devemu_host_resources resources = {0};
59 
60  resources.container_fd = -1;
61  resources.group_fd = -1;
62  resources.device_fd = -1;
63 
64  const struct bar_db_region_config *db_region_cfg = &db_configs[region_idx];
65  size_t db_stride_size = (1UL << db_region_cfg->log_db_stride_size);
66  size_t db_offset = db_idx * db_stride_size;
67  if (db_offset > (db_region_cfg->region.size - sizeof(uint32_t))) {
68  DOCA_LOG_ERR("The given DB index falls outside the DB region");
70  }
71 
72  result = init_vfio_device(&resources, vfio_group, pci_address);
73  if (result != DOCA_SUCCESS) {
75  return result;
76  }
77 
78  result = map_bar_region_memory(&resources, &db_region_cfg->region, &resources.db_region);
79  if (result != DOCA_SUCCESS) {
81  return result;
82  }
83 
84  /* Ring the DoorBell */
85  *((uint32_t *)&resources.db_region.mem[db_offset]) = db_value;
86 
87  DOCA_LOG_INFO("Wrote a DB value of %u (BigEndian) at offset %zu", db_value, db_offset);
88 
89  return DOCA_SUCCESS;
90 }
int32_t result
doca_error_t devemu_pci_device_db_host(const char *pci_address, int vfio_group, int region_idx, uint16_t db_idx, uint32_t db_value)
DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_DB_HOST)
void devemu_host_resources_cleanup(struct devemu_host_resources *resources)
doca_error_t map_bar_region_memory(struct devemu_host_resources *resources, const struct bar_region_config *bar_region_config, struct bar_mapped_region *mapped_mem)
doca_error_t init_vfio_device(struct devemu_host_resources *resources, int vfio_group, const char *pci_address)
uint32_t db_value
struct rdma_resources resources
enum doca_error doca_error_t
DOCA API return codes.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ 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
static const struct bar_db_region_config db_configs[PCI_TYPE_NUM_BAR_DB_REGIONS]
struct bar_region_config region