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_all.h>
51 #include <cam/scsi/scsi_enc.h>
55 static int encstatus(int argc, char **argv);
56 static int fault(int argc, char **argv);
57 static int locate(int argc, char **argv);
58 static int objmap(int argc, char **argv);
59 static int sesled(int argc, char **argv, bool fault);
61 static struct command {
65 int (*exec)(int argc, char **argv);
68 "(<disk>|<sesid>|all) (on|off)",
69 "Change the state of the fault LED associated with a disk",
72 "(<disk>|<sesid>|all) (on|off)",
73 "Change the state of the locate LED associated with a disk",
76 "Print a map of the devices managed by the enclosure", objmap } ,
77 { "status", "", "Print the status of the enclosure",
81 static const int nbcmds = nitems(cmds);
82 static const char *uflag;
85 usage(FILE *out, const char *subcmd)
90 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
92 fprintf(out, "Commands supported:\n");
94 for (i = 0; i < nbcmds; i++) {
96 if (strcmp(subcmd, cmds[i].name) == 0) {
97 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
98 "%s\n\t%s\n", getprogname(), subcmd,
99 cmds[i].param, cmds[i].desc);
104 fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name,
105 cmds[i].param, cmds[i].desc);
112 do_led(int fd, unsigned int idx, bool onoff, bool setfault)
114 encioc_elm_status_t o;
117 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
119 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
123 o.cstat[2] |= (setfault ? 0x20 : 0x02);
125 o.cstat[2] &= (setfault ? 0xdf : 0xfd);
128 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
130 err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
135 disk_match(const char *devnames, const char *disk, size_t len)
140 while ((dname = strstr(dname, disk)) != NULL) {
141 if (dname[len] == '\0' || dname[len] == ',') {
151 sesled(int argc, char **argv, bool setfault)
153 encioc_elm_devnames_t objdn;
154 encioc_element_t *objp;
157 size_t len, i, ndisks;
159 unsigned int nobj, j, sesid;
160 bool all, isses, onoff;
167 usage(stderr, (setfault ? "fault" : "locate"));
172 sesid = strtoul(disk, &endptr, 10);
173 if (*endptr == '\0') {
174 endptr = strrchr(uflag, '*');
175 if (endptr != NULL && *endptr == '*') {
176 warnx("Must specifying a SES device (-u) to use a SES "
177 "id# to identify a disk");
178 usage(stderr, (setfault ? "fault" : "locate"));
183 if (strcmp(argv[2], "on") == 0) {
185 } else if (strcmp(argv[2], "off") == 0) {
188 usage(stderr, (setfault ? "fault" : "locate"));
191 if (strcmp(disk, "all") == 0) {
196 /* Get the list of ses devices */
197 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
200 errx(EXIT_FAILURE, "No SES devices found");
204 for (i = 0; i < g.gl_pathc; i++) {
205 /* ensure we only got numbers after ses */
206 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
207 strlen(g.gl_pathv[i] + 8)) {
210 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
212 * Don't treat non-access errors as critical if we are
213 * accessing all devices
215 if (errno == EACCES && g.gl_pathc > 1) {
216 err(EXIT_FAILURE, "unable to access SES device");
218 warn("unable to access SES device: %s", g.gl_pathv[i]);
222 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
224 err(EXIT_FAILURE, "ENCIOC_GETNELM");
227 objp = calloc(nobj, sizeof(encioc_element_t));
230 err(EXIT_FAILURE, "calloc()");
233 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
235 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
242 "Requested SES ID does not exist");
244 do_led(fd, sesid, onoff, setfault);
249 for (j = 0; j < nobj; j++) {
250 memset(&objdn, 0, sizeof(objdn));
251 objdn.elm_idx = objp[j].elm_idx;
252 objdn.elm_names_size = 128;
253 objdn.elm_devnames = calloc(128, sizeof(char));
254 if (objdn.elm_devnames == NULL) {
256 err(EXIT_FAILURE, "calloc()");
258 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
259 (caddr_t) &objdn) <0) {
262 if (objdn.elm_names_len > 0) {
264 do_led(fd, objdn.elm_idx,
268 if (disk_match(objdn.elm_devnames, disk, len)) {
269 do_led(fd, objdn.elm_idx,
279 if (ndisks == 0 && all == false) {
280 errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
284 return (EXIT_SUCCESS);
288 locate(int argc, char **argv)
291 return (sesled(argc, argv, false));
295 fault(int argc, char **argv)
298 return (sesled(argc, argv, true));
302 objmap(int argc, char **argv __unused)
305 encioc_string_t stri;
306 encioc_elm_devnames_t e_devname;
307 encioc_elm_status_t e_status;
308 encioc_elm_desc_t e_desc;
309 encioc_element_t *e_ptr;
312 unsigned int j, nobj;
317 usage(stderr, "map");
320 /* Get the list of ses devices */
321 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
323 errx(EXIT_FAILURE, "No SES devices found");
325 for (i = 0; i < g.gl_pathc; i++) {
326 /* ensure we only got numbers after ses */
327 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
328 strlen(g.gl_pathv[i] + 8)) {
331 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
333 * Don't treat non-access errors as critical if we are
334 * accessing all devices
336 if (errno == EACCES && g.gl_pathc > 1) {
337 err(EXIT_FAILURE, "unable to access SES device");
339 warn("unable to access SES device: %s", g.gl_pathv[i]);
343 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
345 err(EXIT_FAILURE, "ENCIOC_GETNELM");
348 e_ptr = calloc(nobj, sizeof(encioc_element_t));
351 err(EXIT_FAILURE, "calloc()");
354 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
356 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
359 printf("%s:\n", g.gl_pathv[i] + 5);
360 stri.bufsiz = sizeof(str);
362 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
363 printf("\tEnclosure Name: %s\n", stri.buf);
364 stri.bufsiz = sizeof(str);
366 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
367 printf("\tEnclosure ID: %s\n", stri.buf);
369 for (j = 0; j < nobj; j++) {
370 /* Get the status of the element */
371 memset(&e_status, 0, sizeof(e_status));
372 e_status.elm_idx = e_ptr[j].elm_idx;
373 if (ioctl(fd, ENCIOC_GETELMSTAT,
374 (caddr_t) &e_status) < 0) {
376 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
378 /* Get the description of the element */
379 memset(&e_desc, 0, sizeof(e_desc));
380 e_desc.elm_idx = e_ptr[j].elm_idx;
381 e_desc.elm_desc_len = UINT16_MAX;
382 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
383 if (e_desc.elm_desc_str == NULL) {
385 err(EXIT_FAILURE, "calloc()");
387 if (ioctl(fd, ENCIOC_GETELMDESC,
388 (caddr_t) &e_desc) < 0) {
390 err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
392 /* Get the device name(s) of the element */
393 memset(&e_devname, 0, sizeof(e_devname));
394 e_devname.elm_idx = e_ptr[j].elm_idx;
395 e_devname.elm_names_size = 128;
396 e_devname.elm_devnames = calloc(128, sizeof(char));
397 if (e_devname.elm_devnames == NULL) {
399 err(EXIT_FAILURE, "calloc()");
401 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
402 (caddr_t) &e_devname) <0) {
403 /* We don't care if this fails */
404 e_devname.elm_devnames[0] = '\0';
406 printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx,
407 geteltnm(e_ptr[j].elm_type));
408 printf("\t\tStatus: %s (0x%02x 0x%02x 0x%02x 0x%02x)\n",
409 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
410 e_status.cstat[1], e_status.cstat[2],
412 if (e_desc.elm_desc_len > 0) {
413 printf("\t\tDescription: %s\n",
414 e_desc.elm_desc_str);
416 if (e_devname.elm_names_len > 0) {
417 printf("\t\tDevice Names: %s\n",
418 e_devname.elm_devnames);
420 extra = stat2sbuf(e_ptr[j].elm_type, e_status.cstat);
421 if (sbuf_len(extra) > 0) {
422 printf("\t\tExtra status:\n%s",
426 free(e_devname.elm_devnames);
432 return (EXIT_SUCCESS);
436 encstatus(int argc, char **argv __unused)
445 usage(stderr, "status");
448 /* Get the list of ses devices */
449 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
451 errx(EXIT_FAILURE, "No SES devices found");
453 for (i = 0; i < g.gl_pathc; i++) {
454 /* ensure we only got numbers after ses */
455 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
456 strlen(g.gl_pathv[i] + 8)) {
459 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
461 * Don't treat non-access errors as critical if we are
462 * accessing all devices
464 if (errno == EACCES && g.gl_pathc > 1) {
465 err(EXIT_FAILURE, "unable to access SES device");
467 warn("unable to access SES device: %s", g.gl_pathv[i]);
471 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
473 err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
476 printf("%s: ", g.gl_pathv[i] + 5);
484 if (estat & SES_ENCSTAT_INFO) {
488 if (estat & SES_ENCSTAT_NONCRITICAL) {
491 printf("NONCRITICAL");
494 if (estat & SES_ENCSTAT_CRITICAL) {
501 if (estat & SES_ENCSTAT_UNRECOV) {
516 return (EXIT_SUCCESS);
518 return (EXIT_FAILURE);
523 main(int argc, char **argv)
526 struct command *cmd = NULL;
528 uflag = "/dev/ses[0-9]*";
529 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
543 warnx("Missing command");
547 for (i = 0; i < nbcmds; i++) {
548 if (strcmp(argv[0], cmds[i].name) == 0) {
555 warnx("unknown command %s", argv[0]);
559 return (cmd->exec(argc, argv));