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++) {
256 do_led(fd, objp[j].elm_idx, onoff, setfault);
259 memset(&objdn, 0, sizeof(objdn));
260 objdn.elm_idx = objp[j].elm_idx;
261 objdn.elm_names_size = 128;
262 objdn.elm_devnames = calloc(128, sizeof(char));
263 if (objdn.elm_devnames == NULL) {
265 err(EXIT_FAILURE, "calloc()");
267 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
268 (caddr_t) &objdn) <0) {
271 if (objdn.elm_names_len > 0) {
272 if (disk_match(objdn.elm_devnames, disk, len)) {
273 do_led(fd, objdn.elm_idx,
284 if (ndisks == 0 && all == false) {
285 errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
289 return (EXIT_SUCCESS);
293 locate(int argc, char **argv)
296 return (sesled(argc, argv, false));
300 fault(int argc, char **argv)
303 return (sesled(argc, argv, true));
307 objmap(int argc, char **argv __unused)
310 encioc_string_t stri;
311 encioc_elm_devnames_t e_devname;
312 encioc_elm_status_t e_status;
313 encioc_elm_desc_t e_desc;
314 encioc_element_t *e_ptr;
317 unsigned int j, nobj;
322 usage(stderr, "map");
325 /* Get the list of ses devices */
326 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
328 errx(EXIT_FAILURE, "No SES devices found");
330 for (i = 0; i < g.gl_pathc; i++) {
331 /* ensure we only got numbers after ses */
332 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
333 strlen(g.gl_pathv[i] + 8)) {
336 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
338 * Don't treat non-access errors as critical if we are
339 * accessing all devices
341 if (errno == EACCES && g.gl_pathc > 1) {
342 err(EXIT_FAILURE, "unable to access SES device");
344 warn("unable to access SES device: %s", g.gl_pathv[i]);
348 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
350 err(EXIT_FAILURE, "ENCIOC_GETNELM");
353 e_ptr = calloc(nobj, sizeof(encioc_element_t));
356 err(EXIT_FAILURE, "calloc()");
359 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
361 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
364 printf("%s:\n", g.gl_pathv[i] + 5);
365 stri.bufsiz = sizeof(str);
367 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)
368 printf("\tEnclosure Name: %s\n", stri.buf);
369 stri.bufsiz = sizeof(str);
371 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)
372 printf("\tEnclosure ID: %s\n", stri.buf);
374 for (j = 0; j < nobj; j++) {
375 /* Get the status of the element */
376 memset(&e_status, 0, sizeof(e_status));
377 e_status.elm_idx = e_ptr[j].elm_idx;
378 if (ioctl(fd, ENCIOC_GETELMSTAT,
379 (caddr_t) &e_status) < 0) {
381 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
383 /* Get the description of the element */
384 memset(&e_desc, 0, sizeof(e_desc));
385 e_desc.elm_idx = e_ptr[j].elm_idx;
386 e_desc.elm_desc_len = UINT16_MAX;
387 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
388 if (e_desc.elm_desc_str == NULL) {
390 err(EXIT_FAILURE, "calloc()");
392 if (ioctl(fd, ENCIOC_GETELMDESC,
393 (caddr_t) &e_desc) < 0) {
395 err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
397 /* Get the device name(s) of the element */
398 memset(&e_devname, 0, sizeof(e_devname));
399 e_devname.elm_idx = e_ptr[j].elm_idx;
400 e_devname.elm_names_size = 128;
401 e_devname.elm_devnames = calloc(128, sizeof(char));
402 if (e_devname.elm_devnames == NULL) {
404 err(EXIT_FAILURE, "calloc()");
406 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
407 (caddr_t) &e_devname) <0) {
408 /* We don't care if this fails */
409 e_devname.elm_devnames[0] = '\0';
411 printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx,
412 geteltnm(e_ptr[j].elm_type));
413 printf("\t\tStatus: %s (0x%02x 0x%02x 0x%02x 0x%02x)\n",
414 scode2ascii(e_status.cstat[0]), e_status.cstat[0],
415 e_status.cstat[1], e_status.cstat[2],
417 if (e_desc.elm_desc_len > 0) {
418 printf("\t\tDescription: %s\n",
419 e_desc.elm_desc_str);
421 if (e_devname.elm_names_len > 0) {
422 printf("\t\tDevice Names: %s\n",
423 e_devname.elm_devnames);
425 extra = stat2sbuf(e_ptr[j].elm_type, e_status.cstat);
426 if (sbuf_len(extra) > 0) {
427 printf("\t\tExtra status:\n%s",
431 free(e_devname.elm_devnames);
438 return (EXIT_SUCCESS);
442 encstatus(int argc, char **argv __unused)
451 usage(stderr, "status");
454 /* Get the list of ses devices */
455 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
457 errx(EXIT_FAILURE, "No SES devices found");
459 for (i = 0; i < g.gl_pathc; i++) {
460 /* ensure we only got numbers after ses */
461 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
462 strlen(g.gl_pathv[i] + 8)) {
465 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
467 * Don't treat non-access errors as critical if we are
468 * accessing all devices
470 if (errno == EACCES && g.gl_pathc > 1) {
471 err(EXIT_FAILURE, "unable to access SES device");
473 warn("unable to access SES device: %s", g.gl_pathv[i]);
477 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
479 err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
482 printf("%s: ", g.gl_pathv[i] + 5);
490 if (estat & SES_ENCSTAT_INFO) {
494 if (estat & SES_ENCSTAT_NONCRITICAL) {
497 printf("NONCRITICAL");
500 if (estat & SES_ENCSTAT_CRITICAL) {
507 if (estat & SES_ENCSTAT_UNRECOV) {
522 return (EXIT_SUCCESS);
524 return (EXIT_FAILURE);
529 main(int argc, char **argv)
532 struct command *cmd = NULL;
534 uflag = "/dev/ses[0-9]*";
535 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
549 warnx("Missing command");
553 for (i = 0; i < nbcmds; i++) {
554 if (strcmp(argv[0], cmds[i].name) == 0) {
561 warnx("unknown command %s", argv[0]);
565 return (cmd->exec(argc, argv));