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/endian.h>
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
51 #include <cam/scsi/scsi_enc.h>
55 #define SESUTIL_XO_VERSION "1"
57 static int encstatus(int argc, char **argv);
58 static int fault(int argc, char **argv);
59 static int locate(int argc, char **argv);
60 static int objmap(int argc, char **argv);
61 static int sesled(int argc, char **argv, bool fault);
62 static void sesutil_print(bool *title, const char *fmt, ...) __printflike(2,3);
64 static struct command {
68 int (*exec)(int argc, char **argv);
71 "(<disk>|<sesid>|all) (on|off)",
72 "Change the state of the fault LED associated with a disk",
75 "(<disk>|<sesid>|all) (on|off)",
76 "Change the state of the locate LED associated with a disk",
79 "Print a map of the devices managed by the enclosure", objmap } ,
80 { "status", "", "Print the status of the enclosure",
84 static const int nbcmds = nitems(cmds);
85 static const char *uflag;
88 usage(FILE *out, const char *subcmd)
93 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
95 fprintf(out, "Commands supported:\n");
97 for (i = 0; i < nbcmds; i++) {
99 if (strcmp(subcmd, cmds[i].name) == 0) {
100 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
101 "%s\n\t%s\n", getprogname(), subcmd,
102 cmds[i].param, cmds[i].desc);
107 fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name,
108 cmds[i].param, cmds[i].desc);
115 do_led(int fd, unsigned int idx, elm_type_t type, bool onoff, bool setfault)
117 int state = onoff ? 1 : 0;
118 encioc_elm_status_t o;
119 struct ses_ctrl_dev_slot *slot;
122 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
124 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
126 slot = (struct ses_ctrl_dev_slot *) &o.cstat[0];
129 case ELMTYP_ARRAY_DEV:
130 ses_ctrl_common_set_select(&slot->common, 1);
132 ses_ctrl_dev_slot_set_rqst_fault(slot, state);
134 ses_ctrl_dev_slot_set_rqst_ident(slot, state);
139 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
141 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
146 disk_match(const char *devnames, const char *disk, size_t len)
151 while ((dname = strstr(dname, disk)) != NULL) {
152 if (dname[len] == '\0' || dname[len] == ',') {
162 sesled(int argc, char **argv, bool setfault)
164 encioc_elm_devnames_t objdn;
165 encioc_element_t *objp;
168 size_t len, i, ndisks;
170 unsigned int nobj, j, sesid;
171 bool all, isses, onoff;
178 usage(stderr, (setfault ? "fault" : "locate"));
183 sesid = strtoul(disk, &endptr, 10);
184 if (*endptr == '\0') {
185 endptr = strrchr(uflag, '*');
186 if (endptr != NULL && *endptr == '*') {
187 xo_warnx("Must specifying a SES device (-u) to use a SES "
188 "id# to identify a disk");
189 usage(stderr, (setfault ? "fault" : "locate"));
194 if (strcmp(argv[2], "on") == 0) {
196 } else if (strcmp(argv[2], "off") == 0) {
199 usage(stderr, (setfault ? "fault" : "locate"));
202 if (strcmp(disk, "all") == 0) {
207 /* Get the list of ses devices */
208 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
211 xo_errx(EXIT_FAILURE, "No SES devices found");
215 for (i = 0; i < g.gl_pathc; i++) {
216 /* ensure we only got numbers after ses */
217 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
218 strlen(g.gl_pathv[i] + 8)) {
221 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
223 * Don't treat non-access errors as critical if we are
224 * accessing all devices
226 if (errno == EACCES && g.gl_pathc > 1) {
227 xo_err(EXIT_FAILURE, "unable to access SES device");
229 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
233 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
235 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
238 objp = calloc(nobj, sizeof(encioc_element_t));
241 xo_err(EXIT_FAILURE, "calloc()");
244 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
246 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
252 xo_errx(EXIT_FAILURE,
253 "Requested SES ID does not exist");
255 do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
260 for (j = 0; j < nobj; j++) {
262 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
266 memset(&objdn, 0, sizeof(objdn));
267 objdn.elm_idx = objp[j].elm_idx;
268 objdn.elm_names_size = 128;
269 objdn.elm_devnames = calloc(128, sizeof(char));
270 if (objdn.elm_devnames == NULL) {
272 xo_err(EXIT_FAILURE, "calloc()");
274 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
275 (caddr_t) &objdn) <0) {
278 if (objdn.elm_names_len > 0) {
279 if (disk_match(objdn.elm_devnames, disk, len)) {
280 do_led(fd, objdn.elm_idx, objp[j].elm_type,
291 if (ndisks == 0 && all == false) {
292 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
296 return (EXIT_SUCCESS);
300 locate(int argc, char **argv)
303 return (sesled(argc, argv, false));
307 fault(int argc, char **argv)
310 return (sesled(argc, argv, true));
313 #define TEMPERATURE_OFFSET 20
315 sesutil_print(bool *title, const char *fmt, ...)
320 xo_open_container("extra_status");
321 xo_emit("\t\tExtra status:\n");
325 xo_emit_hv(NULL, fmt, args);
330 print_extra_status(int eletype, u_char *cstat)
334 if (cstat[0] & 0x40) {
335 sesutil_print(&title, "\t\t-{e:predicted_failure/true} Predicted Failure\n");
337 if (cstat[0] & 0x20) {
338 sesutil_print(&title, "\t\t-{e:disabled/true} Disabled\n");
340 if (cstat[0] & 0x10) {
341 sesutil_print(&title, "\t\t-{e:swapped/true} Swapped\n");
345 case ELMTYP_ARRAY_DEV:
346 if (cstat[2] & 0x02) {
347 sesutil_print(&title, "\t\t- LED={q:led/locate}\n");
349 if (cstat[2] & 0x20) {
350 sesutil_print(&title, "\t\t- LED={q:led/fault}\n");
354 sesutil_print(&title, "\t\t- Speed: {:speed/%d}{Uw:rpm}\n",
355 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
359 sesutil_print(&title, "\t\t- Temperature: {:temperature/%d}{Uw:C}\n",
360 cstat[2] - TEMPERATURE_OFFSET);
362 sesutil_print(&title, "\t\t- Temperature: -{q:temperature/reserved}-\n");
366 sesutil_print(&title, "\t\t- Voltage: {:voltage/%.2f}{Uw:V}\n",
367 be16dec(cstat + 2) / 100.0);
371 xo_close_container("extra_status");
376 objmap(int argc, char **argv __unused)
378 encioc_string_t stri;
379 encioc_elm_devnames_t e_devname;
380 encioc_elm_status_t e_status;
381 encioc_elm_desc_t e_desc;
382 encioc_element_t *e_ptr;
385 unsigned int j, nobj;
390 usage(stderr, "map");
393 /* Get the list of ses devices */
394 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
396 xo_errx(EXIT_FAILURE, "No SES devices found");
398 xo_set_version(SESUTIL_XO_VERSION);
399 xo_open_container("sesutil");
400 xo_open_list("enclosures");
401 for (i = 0; i < g.gl_pathc; i++) {
402 /* ensure we only got numbers after ses */
403 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
404 strlen(g.gl_pathv[i] + 8)) {
407 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
409 * Don't treat non-access errors as critical if we are
410 * accessing all devices
412 if (errno == EACCES && g.gl_pathc > 1) {
413 xo_err(EXIT_FAILURE, "unable to access SES device");
415 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
419 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
421 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
424 e_ptr = calloc(nobj, sizeof(encioc_element_t));
427 xo_err(EXIT_FAILURE, "calloc()");
430 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
432 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
435 xo_open_instance("enclosures");
436 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
437 stri.bufsiz = sizeof(str);
439 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
440 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
441 stri.bufsiz = sizeof(str);
443 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
444 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
446 xo_open_list("elements");
447 for (j = 0; j < nobj; j++) {
448 /* Get the status of the element */
449 memset(&e_status, 0, sizeof(e_status));
450 e_status.elm_idx = e_ptr[j].elm_idx;
451 if (ioctl(fd, ENCIOC_GETELMSTAT,
452 (caddr_t) &e_status) < 0) {
454 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
456 /* Get the description of the element */
457 memset(&e_desc, 0, sizeof(e_desc));
458 e_desc.elm_idx = e_ptr[j].elm_idx;
459 e_desc.elm_desc_len = UINT16_MAX;
460 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
461 if (e_desc.elm_desc_str == NULL) {
463 xo_err(EXIT_FAILURE, "calloc()");
465 if (ioctl(fd, ENCIOC_GETELMDESC,
466 (caddr_t) &e_desc) < 0) {
468 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
470 /* Get the device name(s) of the element */
471 memset(&e_devname, 0, sizeof(e_devname));
472 e_devname.elm_idx = e_ptr[j].elm_idx;
473 e_devname.elm_names_size = 128;
474 e_devname.elm_devnames = calloc(128, sizeof(char));
475 if (e_devname.elm_devnames == NULL) {
477 xo_err(EXIT_FAILURE, "calloc()");
479 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
480 (caddr_t) &e_devname) <0) {
481 /* We don't care if this fails */
482 e_devname.elm_devnames[0] = '\0';
484 xo_open_instance("elements");
485 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
486 geteltnm(e_ptr[j].elm_type));
487 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
488 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
489 e_status.cstat[1], e_status.cstat[2],
491 if (e_desc.elm_desc_len > 0) {
492 xo_emit("\t\tDescription: {:description/%s}\n",
493 e_desc.elm_desc_str);
495 if (e_devname.elm_names_len > 0) {
496 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
497 e_devname.elm_devnames);
499 print_extra_status(e_ptr[j].elm_type, e_status.cstat);
500 xo_close_instance("elements");
501 free(e_devname.elm_devnames);
503 xo_close_list("elements");
508 xo_close_list("enclosures");
509 xo_close_container("sesutil");
512 return (EXIT_SUCCESS);
516 encstatus(int argc, char **argv __unused)
525 usage(stderr, "status");
528 /* Get the list of ses devices */
529 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
531 xo_errx(EXIT_FAILURE, "No SES devices found");
534 xo_set_version(SESUTIL_XO_VERSION);
535 xo_open_container("sesutil");
536 xo_open_list("enclosures");
537 for (i = 0; i < g.gl_pathc; i++) {
538 /* ensure we only got numbers after ses */
539 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
540 strlen(g.gl_pathv[i] + 8)) {
543 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
545 * Don't treat non-access errors as critical if we are
546 * accessing all devices
548 if (errno == EACCES && g.gl_pathc > 1) {
549 xo_err(EXIT_FAILURE, "unable to access SES device");
551 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
555 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
556 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
560 xo_open_instance("enclosures");
561 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
567 xo_emit("{q:status/OK}");
569 if (estat & SES_ENCSTAT_INFO) {
570 xo_emit("{lq:status/INFO}");
573 if (estat & SES_ENCSTAT_NONCRITICAL) {
576 xo_emit("{lq:status/NONCRITICAL}");
579 if (estat & SES_ENCSTAT_CRITICAL) {
582 xo_emit("{lq:status/CRITICAL}");
586 if (estat & SES_ENCSTAT_UNRECOV) {
589 xo_emit("{lq:status/UNRECOV}");
594 xo_close_instance("enclosures");
600 xo_close_list("enclosures");
601 xo_close_container("sesutil");
605 return (EXIT_SUCCESS);
607 return (EXIT_FAILURE);
612 main(int argc, char **argv)
615 struct command *cmd = NULL;
617 argc = xo_parse_args(argc, argv);
621 uflag = "/dev/ses[0-9]*";
622 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
636 warnx("Missing command");
640 for (i = 0; i < nbcmds; i++) {
641 if (strcmp(argv[0], cmds[i].name) == 0) {
648 warnx("unknown command %s", argv[0]);
652 return (cmd->exec(argc, argv));