4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #include "spdk/likely.h"
48 struct spdk_pci_addr pci_addr
;
50 enum app_action action
;
51 enum spdk_vmd_led_state led_state
;
54 struct app_opts g_opts
= {
56 .action
= APP_ACTION_GET
,
57 .led_state
= SPDK_VMD_LED_STATE_UNKNOWN
,
60 static const char *g_led_states
[] = {
61 [SPDK_VMD_LED_STATE_OFF
] = "off",
62 [SPDK_VMD_LED_STATE_IDENTIFY
] = "identify",
63 [SPDK_VMD_LED_STATE_FAULT
] = "fault",
64 [SPDK_VMD_LED_STATE_REBUILD
] = "rebuild",
65 [SPDK_VMD_LED_STATE_UNKNOWN
] = "unknown",
71 printf("Usage: %s [-d] [-s STATE] [-r TRADDR]\n", g_opts
.app_name
);
74 printf(" -d enables debug logs from the VMD module\n");
75 printf(" -s STATE sets the state of the LEDs. Available states are:\n");
76 printf(" off, identify, fault, rebuild\n");
77 printf(" -r TRADDR uses device identified by TRADDR\n");
78 printf(" -h shows this help\n");
82 parse_args(int argc
, char **argv
)
87 g_opts
.app_name
= argv
[0];
89 while ((op
= getopt(argc
, argv
, "dhr:s:")) != -1) {
92 if (spdk_pci_addr_parse(&g_opts
.pci_addr
, optarg
)) {
93 fprintf(stderr
, "Unable to parse PCI address: %s\n", optarg
);
97 g_opts
.all_devices
= false;
102 spdk_log_set_print_level(SPDK_LOG_DEBUG
);
103 spdk_log_set_flag("vmd");
106 fprintf(stderr
, "%s must be rebuilt with --enable-debug for the -d flag\n",
111 for (led_state
= SPDK_VMD_LED_STATE_OFF
;
112 led_state
<= SPDK_VMD_LED_STATE_REBUILD
;
114 if (strcmp(optarg
, g_led_states
[led_state
]) == 0) {
115 g_opts
.led_state
= (enum spdk_vmd_led_state
)led_state
;
120 if (g_opts
.led_state
== SPDK_VMD_LED_STATE_UNKNOWN
) {
121 fprintf(stderr
, "Invalid LED state\n");
125 g_opts
.action
= APP_ACTION_SET
;
129 g_opts
.action
= APP_ACTION_NOP
;
142 main(int argc
, char **argv
)
144 struct spdk_env_opts opts
;
145 struct spdk_pci_device
*pci_device
;
146 enum spdk_vmd_led_state led_state
;
150 if (parse_args(argc
, argv
) != 0) {
155 if (g_opts
.action
== APP_ACTION_NOP
) {
159 spdk_env_opts_init(&opts
);
162 if (spdk_env_init(&opts
) < 0) {
163 fprintf(stderr
, "Unable to initialize SPDK environment\n");
167 rc
= spdk_vmd_init();
169 fprintf(stderr
, "Unable to initialize VMD subsystem\n");
173 for (pci_device
= spdk_pci_get_first_device(); pci_device
!= NULL
;
174 pci_device
= spdk_pci_get_next_device(pci_device
)) {
175 if (strcmp(spdk_pci_device_get_type(pci_device
), "vmd") != 0) {
179 if (!g_opts
.all_devices
&&
180 spdk_pci_addr_compare(&g_opts
.pci_addr
, &pci_device
->addr
) != 0) {
184 rc
= spdk_pci_addr_fmt(addr_buf
, sizeof(addr_buf
), &pci_device
->addr
);
186 fprintf(stderr
, "Failed to format VMD's PCI address\n");
191 if (g_opts
.action
== APP_ACTION_GET
) {
192 rc
= spdk_vmd_get_led_state(pci_device
, &led_state
);
193 if (spdk_unlikely(rc
!= 0)) {
194 fprintf(stderr
, "Failed to retrieve the state of the LED on %s\n",
200 printf("%s: %s\n", addr_buf
, g_led_states
[led_state
]);
202 rc
= spdk_vmd_set_led_state(pci_device
, g_opts
.led_state
);
203 if (spdk_unlikely(rc
!= 0)) {
204 fprintf(stderr
, "Failed to set LED state on %s\n", addr_buf
);