2 * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
4 * Copyright (c) 2000 by Matthew Jacob
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
50 #include <cam/scsi/scsi_enc.h>
54 static int encstatus(int argc, char **argv);
55 static int fault(int argc, char **argv);
56 static int locate(int argc, char **argv);
57 static int objmap(int argc, char **argv);
58 static int sesled(int argc, char **argv, bool fault);
60 static struct command {
64 int (*exec)(int argc, char **argv);
67 "(<disk>|<sesid>|all) (on|off)",
68 "Change the state of the fault LED associated with a disk",
71 "(<disk>|<sesid>|all) (on|off)",
72 "Change the state of the locate LED associated with a disk",
75 "Print a map of the devices managed by the enclosure", objmap } ,
76 { "status", "", "Print the status of the enclosure",
80 static const int nbcmds = nitems(cmds);
81 static const char *uflag;
84 usage(FILE *out, const char *subcmd)
89 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
91 fprintf(out, "Commands supported:\n");
93 for (i = 0; i < nbcmds; i++) {
95 if (strcmp(subcmd, cmds[i].name) == 0) {
96 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
97 "%s\n\t%s\n", getprogname(), subcmd,
98 cmds[i].param, cmds[i].desc);
103 fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name,
104 cmds[i].param, cmds[i].desc);
111 do_led(int fd, unsigned int idx, bool onoff, bool setfault)
113 encioc_elm_status_t o;
116 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
118 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
133 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
135 err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
140 disk_match(const char *devnames, const char *disk, size_t len)
145 while ((dname = strstr(dname, disk)) != NULL) {
146 if (dname[len] == '\0' || dname[len] == ',') {
156 sesled(int argc, char **argv, bool setfault)
158 encioc_elm_devnames_t objdn;
159 encioc_element_t *objp;
162 size_t len, i, ndisks;
164 unsigned int nobj, j, sesid;
165 bool all, isses, onoff;
172 usage(stderr, (setfault ? "fault" : "locate"));
177 sesid = strtoul(disk, &endptr, 10);
178 if (*endptr == '\0') {
179 endptr = strrchr(uflag, '*');
180 if (endptr != NULL && *endptr == '*') {
181 warnx("Must specifying a SES device (-u) to use a SES "
182 "id# to identify a disk");
183 usage(stderr, (setfault ? "fault" : "locate"));
188 if (strcmp(argv[2], "on") == 0) {
190 } else if (strcmp(argv[2], "off") == 0) {
193 usage(stderr, (setfault ? "fault" : "locate"));
196 if (strcmp(disk, "all") == 0) {
201 /* Get the list of ses devices */
202 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
205 errx(EXIT_FAILURE, "No SES devices found");
209 for (i = 0; i < g.gl_pathc; i++) {
210 /* ensure we only got numbers after ses */
211 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
212 strlen(g.gl_pathv[i] + 8)) {
215 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
217 * Don't treat non-access errors as critical if we are
218 * accessing all devices
220 if (errno == EACCES && g.gl_pathc > 1) {
221 err(EXIT_FAILURE, "unable to access SES device");
223 warn("unable to access SES device: %s", g.gl_pathv[i]);
227 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
229 err(EXIT_FAILURE, "ENCIOC_GETNELM");
232 objp = calloc(nobj, sizeof(encioc_element_t));
235 err(EXIT_FAILURE, "calloc()");
238 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
240 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
247 "Requested SES ID does not exist");
249 do_led(fd, sesid, onoff, setfault);
254 for (j = 0; j < nobj; j++) {
255 memset(&objdn, 0, sizeof(objdn));
256 objdn.elm_idx = objp[j].elm_idx;
257 objdn.elm_names_size = 128;
258 objdn.elm_devnames = calloc(128, sizeof(char));
259 if (objdn.elm_devnames == NULL) {
261 err(EXIT_FAILURE, "calloc()");
263 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
264 (caddr_t) &objdn) <0) {
267 if (objdn.elm_names_len > 0) {
269 do_led(fd, objdn.elm_idx,
273 if (disk_match(objdn.elm_devnames, disk, len)) {
274 do_led(fd, objdn.elm_idx,
285 if (ndisks == 0 && all == false) {
286 errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
290 return (EXIT_SUCCESS);
294 locate(int argc, char **argv)
297 return (sesled(argc, argv, false));
301 fault(int argc, char **argv)
304 return (sesled(argc, argv, true));
308 objmap(int argc, char **argv __unused)
311 encioc_string_t stri;
312 encioc_elm_devnames_t e_devname;
313 encioc_elm_status_t e_status;
314 encioc_elm_desc_t e_desc;
315 encioc_element_t *e_ptr;
318 unsigned int j, nobj;
323 usage(stderr, "map");
326 /* Get the list of ses devices */
327 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
329 errx(EXIT_FAILURE, "No SES devices found");
331 for (i = 0; i < g.gl_pathc; i++) {
332 /* ensure we only got numbers after ses */
333 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
334 strlen(g.gl_pathv[i] + 8)) {
337 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
339 * Don't treat non-access errors as critical if we are
340 * accessing all devices
342 if (errno == EACCES && g.gl_pathc > 1) {
343 err(EXIT_FAILURE, "unable to access SES device");
345 warn("unable to access SES device: %s", g.gl_pathv[i]);
349 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
351 err(EXIT_FAILURE, "ENCIOC_GETNELM");
354 e_ptr = calloc(nobj, sizeof(encioc_element_t));
357 err(EXIT_FAILURE, "calloc()");
360 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
362 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
365 printf("%s:\n", g.gl_pathv[i] + 5);
366 stri.bufsiz = sizeof(str);
368 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
369 printf("\tEnclosure Name: %s\n", stri.buf);
370 stri.bufsiz = sizeof(str);
372 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
373 printf("\tEnclosure ID: %s\n", stri.buf);
375 for (j = 0; j < nobj; j++) {
376 /* Get the status of the element */
377 memset(&e_status, 0, sizeof(e_status));
378 e_status.elm_idx = e_ptr[j].elm_idx;
379 if (ioctl(fd, ENCIOC_GETELMSTAT,
380 (caddr_t) &e_status) < 0) {
382 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
384 /* Get the description of the element */
385 memset(&e_desc, 0, sizeof(e_desc));
386 e_desc.elm_idx = e_ptr[j].elm_idx;
387 e_desc.elm_desc_len = UINT16_MAX;
388 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
389 if (e_desc.elm_desc_str == NULL) {
391 err(EXIT_FAILURE, "calloc()");
393 if (ioctl(fd, ENCIOC_GETELMDESC,
394 (caddr_t) &e_desc) < 0) {
396 err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
398 /* Get the device name(s) of the element */
399 memset(&e_devname, 0, sizeof(e_devname));
400 e_devname.elm_idx = e_ptr[j].elm_idx;
401 e_devname.elm_names_size = 128;
402 e_devname.elm_devnames = calloc(128, sizeof(char));
403 if (e_devname.elm_devnames == NULL) {
405 err(EXIT_FAILURE, "calloc()");
407 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
408 (caddr_t) &e_devname) <0) {
409 /* We don't care if this fails */
410 e_devname.elm_devnames[0] = '\0';
412 printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx,
413 geteltnm(e_ptr[j].elm_type));
414 printf("\t\tStatus: %s (0x%02x 0x%02x 0x%02x 0x%02x)\n",
415 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
416 e_status.cstat[1], e_status.cstat[2],
418 if (e_desc.elm_desc_len > 0) {
419 printf("\t\tDescription: %s\n",
420 e_desc.elm_desc_str);
422 if (e_devname.elm_names_len > 0) {
423 printf("\t\tDevice Names: %s\n",
424 e_devname.elm_devnames);
426 extra = stat2sbuf(e_ptr[j].elm_type, e_status.cstat);
427 if (sbuf_len(extra) > 0) {
428 printf("\t\tExtra status:\n%s",
432 free(e_devname.elm_devnames);
439 return (EXIT_SUCCESS);
443 encstatus(int argc, char **argv __unused)
452 usage(stderr, "status");
455 /* Get the list of ses devices */
456 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
458 errx(EXIT_FAILURE, "No SES devices found");
460 for (i = 0; i < g.gl_pathc; i++) {
461 /* ensure we only got numbers after ses */
462 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
463 strlen(g.gl_pathv[i] + 8)) {
466 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
468 * Don't treat non-access errors as critical if we are
469 * accessing all devices
471 if (errno == EACCES && g.gl_pathc > 1) {
472 err(EXIT_FAILURE, "unable to access SES device");
474 warn("unable to access SES device: %s", g.gl_pathv[i]);
478 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
480 err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
483 printf("%s: ", g.gl_pathv[i] + 5);
491 if (estat & SES_ENCSTAT_INFO) {
495 if (estat & SES_ENCSTAT_NONCRITICAL) {
498 printf("NONCRITICAL");
501 if (estat & SES_ENCSTAT_CRITICAL) {
508 if (estat & SES_ENCSTAT_UNRECOV) {
523 return (EXIT_SUCCESS);
525 return (EXIT_FAILURE);
530 main(int argc, char **argv)
533 struct command *cmd = NULL;
535 uflag = "/dev/ses[0-9]*";
536 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
550 warnx("Missing command");
554 for (i = 0; i < nbcmds; i++) {
555 if (strcmp(argv[0], cmds[i].name) == 0) {
562 warnx("unknown command %s", argv[0]);
566 return (cmd->exec(argc, argv));