NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
dma_copy_host_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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 <signal.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include <doca_dma.h>
34 #include <doca_error.h>
35 #include <doca_log.h>
36 #include <doca_mmap.h>
37 
38 #include "dma_common.h"
39 
40 DOCA_LOG_REGISTER(DMA_COPY_HOST);
41 
42 /*
43  * Saves export descriptor and buffer information into two separate files
44  *
45  * @export_desc [in]: Export descriptor to write into a file
46  * @export_desc_len [in]: Export descriptor length
47  * @src_buffer [in]: Source buffer
48  * @src_buffer_len [in]: Source buffer length
49  * @export_desc_file_path [in]: Export descriptor file path
50  * @buffer_info_file_path [in]: Buffer information file path
51  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
52  */
53 static doca_error_t save_config_info_to_files(const void *export_desc,
54  size_t export_desc_len,
55  const char *src_buffer,
56  size_t src_buffer_len,
57  char *export_desc_file_path,
58  char *buffer_info_file_path)
59 {
60  FILE *fp;
61  uint64_t buffer_addr = (uintptr_t)src_buffer;
62  uint64_t buffer_len = (uint64_t)src_buffer_len;
63 
64  fp = fopen(export_desc_file_path, "wb");
65  if (fp == NULL) {
66  DOCA_LOG_ERR("Failed to create the DMA copy file");
67  return DOCA_ERROR_IO_FAILED;
68  }
69 
70  if (fwrite(export_desc, 1, export_desc_len, fp) != export_desc_len) {
71  DOCA_LOG_ERR("Failed to write all data into the file");
72  fclose(fp);
73  return DOCA_ERROR_IO_FAILED;
74  }
75 
76  fclose(fp);
77 
78  fp = fopen(buffer_info_file_path, "w");
79  if (fp == NULL) {
80  DOCA_LOG_ERR("Failed to create the DMA copy file");
81  return DOCA_ERROR_IO_FAILED;
82  }
83 
84  fprintf(fp, "%" PRIu64 "\n", buffer_addr);
85  fprintf(fp, "%" PRIu64 "", buffer_len);
86 
87  fclose(fp);
88 
89  return DOCA_SUCCESS;
90 }
91 
92 /*
93  * Run DOCA DMA Host copy sample
94  *
95  * @pcie_addr [in]: Device PCI address
96  * @src_buffer [in]: Source buffer to copy
97  * @src_buffer_size [in]: Buffer size
98  * @export_desc_file_path [in]: Export descriptor file path
99  * @buffer_info_file_name [in]: Buffer info file path
100  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
101  */
102 doca_error_t dma_copy_host(const char *pcie_addr,
103  char *src_buffer,
104  size_t src_buffer_size,
105  char *export_desc_file_path,
106  char *buffer_info_file_name)
107 {
108  struct program_core_objects state = {0};
109  const void *export_desc;
110  size_t export_desc_len;
111  int enter = 0;
112  doca_error_t result, tmp_result;
113 
114  /* Allocate resources */
115  result = allocate_dma_host_resources(pcie_addr, &state);
116  if (result != DOCA_SUCCESS) {
117  DOCA_LOG_ERR("Failed to allocate DMA host resources: %s", doca_error_get_descr(result));
118  return result;
119  }
120 
121  /* Allow exporting the mmap to DPU for read only operations */
123  if (result != DOCA_SUCCESS) {
124  DOCA_LOG_ERR("Failed to set mmap permissions: %s", doca_error_get_descr(result));
125  goto destroy_resources;
126  }
127 
128  /* Populate the memory map with the allocated memory */
129  result = doca_mmap_set_memrange(state.src_mmap, src_buffer, src_buffer_size);
130  if (result != DOCA_SUCCESS) {
131  DOCA_LOG_ERR("Failed to set memory range for source mmap: %s", doca_error_get_descr(result));
132  goto destroy_resources;
133  }
134 
136  if (result != DOCA_SUCCESS) {
137  DOCA_LOG_ERR("Failed to start source mmap: %s", doca_error_get_descr(result));
138  goto destroy_resources;
139  }
140 
141  /* Export DOCA mmap to enable DMA on Host*/
142  result = doca_mmap_export_pci(state.src_mmap, state.dev, &export_desc, &export_desc_len);
143  if (result != DOCA_SUCCESS) {
144  DOCA_LOG_ERR("Failed to start export source mmap: %s", doca_error_get_descr(result));
145  goto destroy_resources;
146  }
147 
148  DOCA_LOG_INFO("Please copy %s and %s to the DPU and run DMA Copy DPU sample",
149  export_desc_file_path,
150  buffer_info_file_name);
151 
152  /* Saves the export desc and buffer info to files, it is the user responsibility to transfer them to the dpu */
153  result = save_config_info_to_files(export_desc,
154  export_desc_len,
155  src_buffer,
156  src_buffer_size,
157  export_desc_file_path,
158  buffer_info_file_name);
159  if (result != DOCA_SUCCESS) {
160  DOCA_LOG_ERR("Failed to save configurations information: %s", doca_error_get_descr(result));
161  goto destroy_resources;
162  }
163 
164  /* Wait for enter which means that the requester has finished reading */
165  DOCA_LOG_INFO("Wait till the DPU has finished and press enter");
166  while (enter != '\r' && enter != '\n')
167  enter = getchar();
168 
169 destroy_resources:
170  tmp_result = destroy_dma_host_resources(&state);
171  if (tmp_result != DOCA_SUCCESS) {
172  DOCA_ERROR_PROPAGATE(result, tmp_result);
173  DOCA_LOG_ERR("Failed to destroy DMA host resources: %s", doca_error_get_descr(tmp_result));
174  }
175 
176  return result;
177 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t destroy_dma_host_resources(struct program_core_objects *state)
Definition: dma_common.c:576
doca_error_t allocate_dma_host_resources(const char *pcie_addr, struct program_core_objects *state)
Definition: dma_common.c:536
doca_error_t dma_copy_host(const char *pcie_addr, char *src_buffer, size_t src_buffer_size, char *export_desc_file_path, char *buffer_info_file_name)
DOCA_LOG_REGISTER(DMA_COPY_HOST)
static doca_error_t save_config_info_to_files(const void *export_desc, size_t export_desc_len, const char *src_buffer, size_t src_buffer_len, char *export_desc_file_path, char *buffer_info_file_path)
#define DOCA_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_IO_FAILED
Definition: doca_error.h:55
@ 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_STABLE doca_error_t doca_mmap_set_memrange(struct doca_mmap *mmap, void *addr, size_t len)
Set the memory range of DOCA memory map.
DOCA_STABLE doca_error_t doca_mmap_set_permissions(struct doca_mmap *mmap, uint32_t access_mask)
Set access flags of the registered memory.
DOCA_STABLE doca_error_t doca_mmap_export_pci(struct doca_mmap *mmap, const struct doca_dev *dev, const void **export_desc, size_t *export_desc_len)
Compose memory map representation for later import with doca_mmap_create_from_export() for one of the...
DOCA_STABLE doca_error_t doca_mmap_start(struct doca_mmap *mmap)
Start DOCA Memory Map.
@ DOCA_ACCESS_FLAG_PCI_READ_ONLY
Definition: doca_types.h:87
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:298
struct doca_mmap * src_mmap
Definition: common.h:47
struct doca_dev * dev
Definition: common.h:46