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) {
247 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
254 xo_errx(EXIT_FAILURE,
255 "Requested SES ID does not exist");
257 do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault);
263 for (j = 0; j < nobj; j++) {
265 do_led(fd, objp[j].elm_idx, objp[j].elm_type,
269 memset(&objdn, 0, sizeof(objdn));
270 objdn.elm_idx = objp[j].elm_idx;
271 objdn.elm_names_size = 128;
272 objdn.elm_devnames = calloc(128, sizeof(char));
273 if (objdn.elm_devnames == NULL) {
275 xo_err(EXIT_FAILURE, "calloc()");
277 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
278 (caddr_t) &objdn) <0) {
281 if (objdn.elm_names_len > 0) {
282 if (disk_match(objdn.elm_devnames, disk, len)) {
283 do_led(fd, objdn.elm_idx, objp[j].elm_type,
294 if (ndisks == 0 && all == false) {
295 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
299 return (EXIT_SUCCESS);
303 locate(int argc, char **argv)
306 return (sesled(argc, argv, false));
310 fault(int argc, char **argv)
313 return (sesled(argc, argv, true));
316 #define TEMPERATURE_OFFSET 20
318 sesutil_print(bool *title, const char *fmt, ...)
323 xo_open_container("extra_status");
324 xo_emit("\t\tExtra status:\n");
328 xo_emit_hv(NULL, fmt, args);
333 print_extra_status(int eletype, u_char *cstat)
337 if (cstat[0] & 0x40) {
338 sesutil_print(&title, "\t\t-{e:predicted_failure/true} Predicted Failure\n");
340 if (cstat[0] & 0x20) {
341 sesutil_print(&title, "\t\t-{e:disabled/true} Disabled\n");
343 if (cstat[0] & 0x10) {
344 sesutil_print(&title, "\t\t-{e:swapped/true} Swapped\n");
348 case ELMTYP_ARRAY_DEV:
349 if (cstat[2] & 0x02) {
350 sesutil_print(&title, "\t\t- LED={q:led/locate}\n");
352 if (cstat[2] & 0x20) {
353 sesutil_print(&title, "\t\t- LED={q:led/fault}\n");
357 sesutil_print(&title, "\t\t- Speed: {:speed/%d}{Uw:rpm}\n",
358 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
362 sesutil_print(&title, "\t\t- Temperature: {:temperature/%d}{Uw:C}\n",
363 cstat[2] - TEMPERATURE_OFFSET);
365 sesutil_print(&title, "\t\t- Temperature: -{q:temperature/reserved}-\n");
369 sesutil_print(&title, "\t\t- Voltage: {:voltage/%.2f}{Uw:V}\n",
370 be16dec(cstat + 2) / 100.0);
374 xo_close_container("extra_status");
379 objmap(int argc, char **argv __unused)
381 encioc_string_t stri;
382 encioc_elm_devnames_t e_devname;
383 encioc_elm_status_t e_status;
384 encioc_elm_desc_t e_desc;
385 encioc_element_t *e_ptr;
388 unsigned int j, nobj;
393 usage(stderr, "map");
396 /* Get the list of ses devices */
397 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
399 xo_errx(EXIT_FAILURE, "No SES devices found");
401 xo_set_version(SESUTIL_XO_VERSION);
402 xo_open_container("sesutil");
403 xo_open_list("enclosures");
404 for (i = 0; i < g.gl_pathc; i++) {
405 /* ensure we only got numbers after ses */
406 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
407 strlen(g.gl_pathv[i] + 8)) {
410 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
412 * Don't treat non-access errors as critical if we are
413 * accessing all devices
415 if (errno == EACCES && g.gl_pathc > 1) {
416 xo_err(EXIT_FAILURE, "unable to access SES device");
418 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
422 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
424 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
427 e_ptr = calloc(nobj, sizeof(encioc_element_t));
430 xo_err(EXIT_FAILURE, "calloc()");
433 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
435 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
438 xo_open_instance("enclosures");
439 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
440 stri.bufsiz = sizeof(str);
442 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
443 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
444 stri.bufsiz = sizeof(str);
446 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
447 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
449 xo_open_list("elements");
450 for (j = 0; j < nobj; j++) {
451 /* Get the status of the element */
452 memset(&e_status, 0, sizeof(e_status));
453 e_status.elm_idx = e_ptr[j].elm_idx;
454 if (ioctl(fd, ENCIOC_GETELMSTAT,
455 (caddr_t) &e_status) < 0) {
457 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
459 /* Get the description of the element */
460 memset(&e_desc, 0, sizeof(e_desc));
461 e_desc.elm_idx = e_ptr[j].elm_idx;
462 e_desc.elm_desc_len = UINT16_MAX;
463 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
464 if (e_desc.elm_desc_str == NULL) {
466 xo_err(EXIT_FAILURE, "calloc()");
468 if (ioctl(fd, ENCIOC_GETELMDESC,
469 (caddr_t) &e_desc) < 0) {
471 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
473 /* Get the device name(s) of the element */
474 memset(&e_devname, 0, sizeof(e_devname));
475 e_devname.elm_idx = e_ptr[j].elm_idx;
476 e_devname.elm_names_size = 128;
477 e_devname.elm_devnames = calloc(128, sizeof(char));
478 if (e_devname.elm_devnames == NULL) {
480 xo_err(EXIT_FAILURE, "calloc()");
482 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
483 (caddr_t) &e_devname) <0) {
484 /* We don't care if this fails */
485 e_devname.elm_devnames[0] = '\0';
487 xo_open_instance("elements");
488 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
489 geteltnm(e_ptr[j].elm_type));
490 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
491 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
492 e_status.cstat[1], e_status.cstat[2],
494 if (e_desc.elm_desc_len > 0) {
495 xo_emit("\t\tDescription: {:description/%s}\n",
496 e_desc.elm_desc_str);
498 if (e_devname.elm_names_len > 0) {
499 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
500 e_devname.elm_devnames);
502 print_extra_status(e_ptr[j].elm_type, e_status.cstat);
503 xo_close_instance("elements");
504 free(e_devname.elm_devnames);
506 xo_close_list("elements");
511 xo_close_list("enclosures");
512 xo_close_container("sesutil");
515 return (EXIT_SUCCESS);
519 encstatus(int argc, char **argv __unused)
528 usage(stderr, "status");
531 /* Get the list of ses devices */
532 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
534 xo_errx(EXIT_FAILURE, "No SES devices found");
537 xo_set_version(SESUTIL_XO_VERSION);
538 xo_open_container("sesutil");
539 xo_open_list("enclosures");
540 for (i = 0; i < g.gl_pathc; i++) {
541 /* ensure we only got numbers after ses */
542 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
543 strlen(g.gl_pathv[i] + 8)) {
546 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
548 * Don't treat non-access errors as critical if we are
549 * accessing all devices
551 if (errno == EACCES && g.gl_pathc > 1) {
552 xo_err(EXIT_FAILURE, "unable to access SES device");
554 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
558 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
559 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
563 xo_open_instance("enclosures");
564 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
570 xo_emit("{q:status/OK}");
572 if (estat & SES_ENCSTAT_INFO) {
573 xo_emit("{lq:status/INFO}");
576 if (estat & SES_ENCSTAT_NONCRITICAL) {
579 xo_emit("{lq:status/NONCRITICAL}");
582 if (estat & SES_ENCSTAT_CRITICAL) {
585 xo_emit("{lq:status/CRITICAL}");
589 if (estat & SES_ENCSTAT_UNRECOV) {
592 xo_emit("{lq:status/UNRECOV}");
597 xo_close_instance("enclosures");
603 xo_close_list("enclosures");
604 xo_close_container("sesutil");
608 return (EXIT_SUCCESS);
610 return (EXIT_FAILURE);
615 main(int argc, char **argv)
618 struct command *cmd = NULL;
620 argc = xo_parse_args(argc, argv);
624 uflag = "/dev/ses[0-9]*";
625 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
639 warnx("Missing command");
643 for (i = 0; i < nbcmds; i++) {
644 if (strcmp(argv[0], cmds[i].name) == 0) {
651 warnx("unknown command %s", argv[0]);
655 return (cmd->exec(argc, argv));