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, bool onoff, bool setfault)
117 encioc_elm_status_t o;
120 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
122 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
137 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
139 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
144 disk_match(const char *devnames, const char *disk, size_t len)
149 while ((dname = strstr(dname, disk)) != NULL) {
150 if (dname[len] == '\0' || dname[len] == ',') {
160 sesled(int argc, char **argv, bool setfault)
162 encioc_elm_devnames_t objdn;
163 encioc_element_t *objp;
166 size_t len, i, ndisks;
168 unsigned int nobj, j, sesid;
169 bool all, isses, onoff;
176 usage(stderr, (setfault ? "fault" : "locate"));
181 sesid = strtoul(disk, &endptr, 10);
182 if (*endptr == '\0') {
183 endptr = strrchr(uflag, '*');
184 if (endptr != NULL && *endptr == '*') {
185 xo_warnx("Must specifying a SES device (-u) to use a SES "
186 "id# to identify a disk");
187 usage(stderr, (setfault ? "fault" : "locate"));
192 if (strcmp(argv[2], "on") == 0) {
194 } else if (strcmp(argv[2], "off") == 0) {
197 usage(stderr, (setfault ? "fault" : "locate"));
200 if (strcmp(disk, "all") == 0) {
205 /* Get the list of ses devices */
206 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
209 xo_errx(EXIT_FAILURE, "No SES devices found");
213 for (i = 0; i < g.gl_pathc; i++) {
214 /* ensure we only got numbers after ses */
215 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
216 strlen(g.gl_pathv[i] + 8)) {
219 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
221 * Don't treat non-access errors as critical if we are
222 * accessing all devices
224 if (errno == EACCES && g.gl_pathc > 1) {
225 xo_err(EXIT_FAILURE, "unable to access SES device");
227 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
231 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
233 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
236 objp = calloc(nobj, sizeof(encioc_element_t));
239 xo_err(EXIT_FAILURE, "calloc()");
242 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
244 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
250 xo_errx(EXIT_FAILURE,
251 "Requested SES ID does not exist");
253 do_led(fd, sesid, onoff, setfault);
258 for (j = 0; j < nobj; j++) {
260 do_led(fd, objp[j].elm_idx, onoff, setfault);
263 memset(&objdn, 0, sizeof(objdn));
264 objdn.elm_idx = objp[j].elm_idx;
265 objdn.elm_names_size = 128;
266 objdn.elm_devnames = calloc(128, sizeof(char));
267 if (objdn.elm_devnames == NULL) {
269 xo_err(EXIT_FAILURE, "calloc()");
271 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
272 (caddr_t) &objdn) <0) {
275 if (objdn.elm_names_len > 0) {
276 if (disk_match(objdn.elm_devnames, disk, len)) {
277 do_led(fd, objdn.elm_idx,
288 if (ndisks == 0 && all == false) {
289 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
293 return (EXIT_SUCCESS);
297 locate(int argc, char **argv)
300 return (sesled(argc, argv, false));
304 fault(int argc, char **argv)
307 return (sesled(argc, argv, true));
310 #define TEMPERATURE_OFFSET 20
312 sesutil_print(bool *title, const char *fmt, ...)
317 xo_open_container("extra_status");
318 xo_emit("\t\tExtra status:\n");
322 xo_emit_hv(NULL, fmt, args);
327 print_extra_status(int eletype, u_char *cstat)
331 if (cstat[0] & 0x40) {
332 sesutil_print(&title, "\t\t-{e:predicted_failure/true} Predicted Failure\n");
334 if (cstat[0] & 0x20) {
335 sesutil_print(&title, "\t\t-{e:disabled/true} Disabled\n");
337 if (cstat[0] & 0x10) {
338 sesutil_print(&title, "\t\t-{e:swapped/true} Swapped\n");
342 case ELMTYP_ARRAY_DEV:
343 if (cstat[2] & 0x02) {
344 sesutil_print(&title, "\t\t- LED={q:led/locate}\n");
346 if (cstat[2] & 0x20) {
347 sesutil_print(&title, "\t\t- LED={q:led/fault}\n");
351 sesutil_print(&title, "\t\t- Speed: {:speed/%d}{Uw:rpm}\n",
352 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
356 sesutil_print(&title, "\t\t- Temperature: {:temperature/%d}{Uw:C}\n",
357 cstat[2] - TEMPERATURE_OFFSET);
359 sesutil_print(&title, "\t\t- Temperature: -{q:temperature/reserved}-\n");
363 sesutil_print(&title, "\t\t- Voltage: {:voltage/%.2f}{Uw:V}\n",
364 be16dec(cstat + 2) / 100.0);
368 xo_close_container("extra_status");
373 objmap(int argc, char **argv __unused)
375 encioc_string_t stri;
376 encioc_elm_devnames_t e_devname;
377 encioc_elm_status_t e_status;
378 encioc_elm_desc_t e_desc;
379 encioc_element_t *e_ptr;
382 unsigned int j, nobj;
387 usage(stderr, "map");
390 /* Get the list of ses devices */
391 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
393 xo_errx(EXIT_FAILURE, "No SES devices found");
395 xo_set_version(SESUTIL_XO_VERSION);
396 xo_open_container("sesutil");
397 xo_open_list("enclosures");
398 for (i = 0; i < g.gl_pathc; i++) {
399 /* ensure we only got numbers after ses */
400 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
401 strlen(g.gl_pathv[i] + 8)) {
404 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
406 * Don't treat non-access errors as critical if we are
407 * accessing all devices
409 if (errno == EACCES && g.gl_pathc > 1) {
410 xo_err(EXIT_FAILURE, "unable to access SES device");
412 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
416 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
418 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM");
421 e_ptr = calloc(nobj, sizeof(encioc_element_t));
424 xo_err(EXIT_FAILURE, "calloc()");
427 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
429 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
432 xo_open_instance("enclosures");
433 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5);
434 stri.bufsiz = sizeof(str);
436 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
437 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf);
438 stri.bufsiz = sizeof(str);
440 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
441 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf);
443 xo_open_list("elements");
444 for (j = 0; j < nobj; j++) {
445 /* Get the status of the element */
446 memset(&e_status, 0, sizeof(e_status));
447 e_status.elm_idx = e_ptr[j].elm_idx;
448 if (ioctl(fd, ENCIOC_GETELMSTAT,
449 (caddr_t) &e_status) < 0) {
451 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
453 /* Get the description of the element */
454 memset(&e_desc, 0, sizeof(e_desc));
455 e_desc.elm_idx = e_ptr[j].elm_idx;
456 e_desc.elm_desc_len = UINT16_MAX;
457 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
458 if (e_desc.elm_desc_str == NULL) {
460 xo_err(EXIT_FAILURE, "calloc()");
462 if (ioctl(fd, ENCIOC_GETELMDESC,
463 (caddr_t) &e_desc) < 0) {
465 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
467 /* Get the device name(s) of the element */
468 memset(&e_devname, 0, sizeof(e_devname));
469 e_devname.elm_idx = e_ptr[j].elm_idx;
470 e_devname.elm_names_size = 128;
471 e_devname.elm_devnames = calloc(128, sizeof(char));
472 if (e_devname.elm_devnames == NULL) {
474 xo_err(EXIT_FAILURE, "calloc()");
476 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
477 (caddr_t) &e_devname) <0) {
478 /* We don't care if this fails */
479 e_devname.elm_devnames[0] = '\0';
481 xo_open_instance("elements");
482 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx,
483 geteltnm(e_ptr[j].elm_type));
484 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n",
485 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
486 e_status.cstat[1], e_status.cstat[2],
488 if (e_desc.elm_desc_len > 0) {
489 xo_emit("\t\tDescription: {:description/%s}\n",
490 e_desc.elm_desc_str);
492 if (e_devname.elm_names_len > 0) {
493 xo_emit("\t\tDevice Names: {:device_names/%s}\n",
494 e_devname.elm_devnames);
496 print_extra_status(e_ptr[j].elm_type, e_status.cstat);
497 xo_close_instance("elements");
498 free(e_devname.elm_devnames);
500 xo_close_list("elements");
505 xo_close_list("enclosures");
506 xo_close_container("sesutil");
509 return (EXIT_SUCCESS);
513 encstatus(int argc, char **argv __unused)
522 usage(stderr, "status");
525 /* Get the list of ses devices */
526 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
528 xo_errx(EXIT_FAILURE, "No SES devices found");
531 xo_set_version(SESUTIL_XO_VERSION);
532 xo_open_container("sesutil");
533 xo_open_list("enclosures");
534 for (i = 0; i < g.gl_pathc; i++) {
535 /* ensure we only got numbers after ses */
536 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
537 strlen(g.gl_pathv[i] + 8)) {
540 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
542 * Don't treat non-access errors as critical if we are
543 * accessing all devices
545 if (errno == EACCES && g.gl_pathc > 1) {
546 xo_err(EXIT_FAILURE, "unable to access SES device");
548 xo_warn("unable to access SES device: %s", g.gl_pathv[i]);
552 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
553 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
557 xo_open_instance("enclosures");
558 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5);
564 xo_emit("{q:status/OK}");
566 if (estat & SES_ENCSTAT_INFO) {
567 xo_emit("{lq:status/INFO}");
570 if (estat & SES_ENCSTAT_NONCRITICAL) {
573 xo_emit("{lq:status/NONCRITICAL}");
576 if (estat & SES_ENCSTAT_CRITICAL) {
579 xo_emit("{lq:status/CRITICAL}");
583 if (estat & SES_ENCSTAT_UNRECOV) {
586 xo_emit("{lq:status/UNRECOV}");
591 xo_close_instance("enclosures");
597 xo_close_list("enclosures");
598 xo_close_container("sesutil");
602 return (EXIT_SUCCESS);
604 return (EXIT_FAILURE);
609 main(int argc, char **argv)
612 struct command *cmd = NULL;
614 argc = xo_parse_args(argc, argv);
618 uflag = "/dev/ses[0-9]*";
619 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
633 warnx("Missing command");
637 for (i = 0; i < nbcmds; i++) {
638 if (strcmp(argv[0], cmds[i].name) == 0) {
645 warnx("unknown command %s", argv[0]);
649 return (cmd->exec(argc, argv));