NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
rmax_set_clock_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2023 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 <doca_log.h>
27 
28 #include "rmax_common.h"
29 
30 DOCA_LOG_REGISTER(RMAX_SET_CLOCK::MAIN);
31 
32 /* Sample's logic */
33 doca_error_t set_clock(const char *pcie_addr, struct program_core_objects *state);
34 
35 /*
36  * ARGP Callback - Handle PCI device address parameter
37  *
38  * @param [in]: Input parameter
39  * @opaque [in/out]: PCIe address
40  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
41  */
42 static doca_error_t pci_address_callback(void *param, void *opaque)
43 {
44  char *pcie_addr = (char *)opaque;
45  char *pci_address = (char *)param;
46  int len;
47 
48  len = strnlen(pci_address, DOCA_DEVINFO_PCI_ADDR_SIZE);
50  DOCA_LOG_ERR("PCI address too long, max %d", DOCA_DEVINFO_PCI_ADDR_SIZE - 1);
52  }
53  strncpy(pcie_addr, pci_address, len + 1);
54  return DOCA_SUCCESS;
55 }
56 
57 /*
58  * Register the command line parameter for the sample.
59  *
60  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
61  */
63 {
65  struct doca_argp_param *pci_param;
66 
67  result = doca_argp_param_create(&pci_param);
68  if (result != DOCA_SUCCESS) {
69  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
70  return result;
71  }
72  doca_argp_param_set_short_name(pci_param, "p");
73  doca_argp_param_set_long_name(pci_param, "pci-addr");
74  doca_argp_param_set_description(pci_param, "PCI device address");
77  result = doca_argp_register_param(pci_param);
78  if (result != DOCA_SUCCESS) {
79  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
80  return result;
81  }
82 
83  return DOCA_SUCCESS;
84 }
85 
86 /*
87  * Sample main function
88  *
89  * @argc [in]: command line arguments size
90  * @argv [in]: array of command line arguments
91  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
92  */
93 int main(int argc, char **argv)
94 {
96  struct doca_log_backend *sdk_log;
97  int exit_status = EXIT_FAILURE;
98  struct program_core_objects state;
99  char pcie_address[DOCA_DEVINFO_PCI_ADDR_SIZE];
100 
101  strcpy(pcie_address, "03:00.0");
102 
103  /* Register a logger backend */
105  if (result != DOCA_SUCCESS)
106  goto sample_exit;
107 
108  /* Register a logger backend for internal SDK errors and warnings */
109  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
110  if (result != DOCA_SUCCESS)
111  goto sample_exit;
113  if (result != DOCA_SUCCESS)
114  goto sample_exit;
115 
116  DOCA_LOG_INFO("Starting the sample");
117 
118  /* ARGP initialization */
119  result = doca_argp_init(NULL, &pcie_address);
120  if (result != DOCA_SUCCESS) {
121  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
122  goto sample_exit;
123  }
124 
125  /* register ARGP parameters of the sample */
127  if (result != DOCA_SUCCESS) {
128  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
130  goto argp_cleanup;
131  }
132 
133  /* start parsing received arguments */
134  result = doca_argp_start(argc, argv);
135  if (result != DOCA_SUCCESS) {
136  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
138  goto argp_cleanup;
139  }
140 
141  /* Sample's main logic */
142  result = set_clock(pcie_address, &state);
143  if (result != DOCA_SUCCESS) {
144  DOCA_LOG_ERR("set_clock() encountered an error: %s", doca_error_get_descr(result));
145  goto argp_cleanup;
146  }
147 
148  exit_status = EXIT_SUCCESS;
149 
150 argp_cleanup:
152 sample_exit:
153  if (exit_status == EXIT_SUCCESS)
154  DOCA_LOG_INFO("Sample finished successfully");
155  else
156  DOCA_LOG_INFO("Sample finished with errors");
157  return exit_status;
158 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
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_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#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_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
static doca_error_t pci_address_callback(void *param, void *opaque)
doca_error_t register_set_clock_params(void)
int main(int argc, char **argv)
DOCA_LOG_REGISTER(RMAX_SET_CLOCK::MAIN)
doca_error_t set_clock(const char *pcie_addr, struct program_core_objects *state)