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>
48 #include <cam/scsi/scsi_all.h>
49 #include <cam/scsi/scsi_enc.h>
53 static int encstatus(int argc, char **argv);
54 static int fault(int argc, char **argv);
55 static int locate(int argc, char **argv);
56 static int objmap(int argc, char **argv);
57 static int sesled(int argc, char **argv, bool fault);
59 static struct command {
63 int (*exec)(int argc, char **argv);
66 "(<disk>|<sesid>|all) (on|off)",
67 "Change the state of the fault LED associated with a disk",
70 "(<disk>|<sesid>|all) (on|off)",
71 "Change the state of the locate LED associated with a disk",
74 "Print a map of the devices managed by the enclosure", objmap } ,
75 { "status", "", "Print the status of the enclosure",
79 static const int nbcmds = nitems(cmds);
80 static const char *uflag;
83 usage(FILE *out, const char *subcmd)
88 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n",
90 fprintf(out, "Commands supported:\n");
92 for (i = 0; i < nbcmds; i++) {
94 if (strcmp(subcmd, cmds[i].name) == 0) {
95 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] "
96 "%s\n\t%s\n", getprogname(), subcmd,
97 cmds[i].param, cmds[i].desc);
102 fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name,
103 cmds[i].param, cmds[i].desc);
110 do_led(int fd, unsigned int idx, bool onoff, bool setfault)
112 encioc_elm_status_t o;
115 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) {
117 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
121 o.cstat[2] |= (setfault ? 0x20 : 0x02);
123 o.cstat[2] &= (setfault ? 0xdf : 0xfd);
126 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) {
128 err(EXIT_FAILURE, "ENCIOC_SETELMSTAT");
133 disk_match(const char *devnames, const char *disk, size_t len)
138 while ((dname = strstr(dname, disk)) != NULL) {
139 if (dname[len] == '\0' || dname[len] == ',') {
149 sesled(int argc, char **argv, bool setfault)
151 encioc_elm_devnames_t objdn;
152 encioc_element_t *objp;
155 size_t len, i, ndisks;
157 unsigned int nobj, j, sesid;
158 bool all, isses, onoff;
165 usage(stderr, (setfault ? "fault" : "locate"));
170 sesid = strtoul(disk, &endptr, 10);
171 if (*endptr == '\0') {
172 endptr = strrchr(uflag, '*');
173 if (*endptr == '*') {
174 warnx("Must specifying a SES device (-u) to use a SES "
175 "id# to identify a disk");
176 usage(stderr, (setfault ? "fault" : "locate"));
181 if (strcmp(argv[2], "on") == 0) {
183 } else if (strcmp(argv[2], "off") == 0) {
186 usage(stderr, (setfault ? "fault" : "locate"));
189 if (strcmp(disk, "all") == 0) {
194 /* Get the list of ses devices */
195 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) ==
198 errx(EXIT_FAILURE, "No SES devices found");
202 for (i = 0; i < g.gl_pathc; i++) {
203 /* ensure we only got numbers after ses */
204 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
205 strlen(g.gl_pathv[i] + 8)) {
208 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
210 * Don't treat non-access errors as critical if we are
211 * accessing all devices
213 if (errno == EACCES && g.gl_pathc > 1) {
214 err(EXIT_FAILURE, "unable to access SES device");
216 warn("unable to access SES device: %s", g.gl_pathv[i]);
220 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
222 err(EXIT_FAILURE, "ENCIOC_GETNELM");
225 objp = calloc(nobj, sizeof(encioc_element_t));
228 err(EXIT_FAILURE, "calloc()");
231 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
233 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
240 "Requested SES ID does not exist");
242 do_led(fd, sesid, onoff, setfault);
247 for (j = 0; j < nobj; j++) {
248 memset(&objdn, 0, sizeof(objdn));
249 objdn.elm_idx = objp[j].elm_idx;
250 objdn.elm_names_size = 128;
251 objdn.elm_devnames = calloc(128, sizeof(char));
252 if (objdn.elm_devnames == NULL) {
254 err(EXIT_FAILURE, "calloc()");
256 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
257 (caddr_t) &objdn) <0) {
260 if (objdn.elm_names_len > 0) {
262 do_led(fd, objdn.elm_idx,
266 if (disk_match(objdn.elm_devnames, disk, len)) {
267 do_led(fd, objdn.elm_idx,
277 if (ndisks == 0 && all == false) {
278 errx(EXIT_FAILURE, "Count not find the SES id of device '%s'",
282 return (EXIT_SUCCESS);
286 locate(int argc, char **argv)
289 return (sesled(argc, argv, false));
293 fault(int argc, char **argv)
296 return (sesled(argc, argv, true));
300 objmap(int argc, char **argv __unused)
302 encioc_elm_devnames_t e_devname;
303 encioc_elm_status_t e_status;
304 encioc_elm_desc_t e_desc;
305 encioc_element_t *e_ptr;
308 unsigned int j, nobj;
312 usage(stderr, "map");
315 /* Get the list of ses devices */
316 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
318 errx(EXIT_FAILURE, "No SES devices found");
320 for (i = 0; i < g.gl_pathc; i++) {
321 /* ensure we only got numbers after ses */
322 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
323 strlen(g.gl_pathv[i] + 8)) {
326 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
328 * Don't treat non-access errors as critical if we are
329 * accessing all devices
331 if (errno == EACCES && g.gl_pathc > 1) {
332 err(EXIT_FAILURE, "unable to access SES device");
334 warn("unable to access SES device: %s", g.gl_pathv[i]);
338 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
340 err(EXIT_FAILURE, "ENCIOC_GETNELM");
343 e_ptr = calloc(nobj, sizeof(encioc_element_t));
346 err(EXIT_FAILURE, "calloc()");
349 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) {
351 err(EXIT_FAILURE, "ENCIOC_GETELMMAP");
354 printf("%s:\n", g.gl_pathv[i] + 5);
355 for (j = 0; j < nobj; j++) {
356 /* Get the status of the element */
357 memset(&e_status, 0, sizeof(e_status));
358 e_status.elm_idx = e_ptr[j].elm_idx;
359 if (ioctl(fd, ENCIOC_GETELMSTAT,
360 (caddr_t) &e_status) < 0) {
362 err(EXIT_FAILURE, "ENCIOC_GETELMSTAT");
364 /* Get the description of the element */
365 memset(&e_desc, 0, sizeof(e_desc));
366 e_desc.elm_idx = e_ptr[j].elm_idx;
367 e_desc.elm_desc_len = UINT16_MAX;
368 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
369 if (e_desc.elm_desc_str == NULL) {
371 err(EXIT_FAILURE, "calloc()");
373 if (ioctl(fd, ENCIOC_GETELMDESC,
374 (caddr_t) &e_desc) < 0) {
376 err(EXIT_FAILURE, "ENCIOC_GETELMDESC");
378 /* Get the device name(s) of the element */
379 memset(&e_devname, 0, sizeof(e_devname));
380 e_devname.elm_idx = e_ptr[j].elm_idx;
381 e_devname.elm_names_size = 128;
382 e_devname.elm_devnames = calloc(128, sizeof(char));
383 if (e_devname.elm_devnames == NULL) {
385 err(EXIT_FAILURE, "calloc()");
387 if (ioctl(fd, ENCIOC_GETELMDEVNAMES,
388 (caddr_t) &e_devname) <0) {
389 /* We don't care if this fails */
390 e_devname.elm_devnames[0] = '\0';
392 printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx,
393 geteltnm(e_ptr[j].elm_type));
394 printf("\t\tStatus: %s\n",
395 stat2ascii(e_ptr[i].elm_type, e_status.cstat));
396 if (e_desc.elm_desc_len > 0) {
397 printf("\t\tDescription: %s\n",
398 e_desc.elm_desc_str);
400 if (e_devname.elm_names_len > 0) {
401 printf("\t\tDevice Names: %s\n",
402 e_devname.elm_devnames);
404 free(e_devname.elm_devnames);
410 return (EXIT_SUCCESS);
414 encstatus(int argc, char **argv __unused)
423 usage(stderr, "status");
426 /* Get the list of ses devices */
427 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) {
429 errx(EXIT_FAILURE, "No SES devices found");
431 for (i = 0; i < g.gl_pathc; i++) {
432 /* ensure we only got numbers after ses */
433 if (strspn(g.gl_pathv[i] + 8, "0123456789") !=
434 strlen(g.gl_pathv[i] + 8)) {
437 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) {
439 * Don't treat non-access errors as critical if we are
440 * accessing all devices
442 if (errno == EACCES && g.gl_pathc > 1) {
443 err(EXIT_FAILURE, "unable to access SES device");
445 warn("unable to access SES device: %s", g.gl_pathv[i]);
449 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
451 err(EXIT_FAILURE, "ENCIOC_GETENCSTAT");
454 printf("%s: ", g.gl_pathv[i] + 5);
462 if (estat & SES_ENCSTAT_INFO) {
466 if (estat & SES_ENCSTAT_NONCRITICAL) {
469 printf("NONCRITICAL");
472 if (estat & SES_ENCSTAT_CRITICAL) {
479 if (estat & SES_ENCSTAT_UNRECOV) {
494 return (EXIT_SUCCESS);
496 return (EXIT_FAILURE);
501 main(int argc, char **argv)
504 struct command *cmd = NULL;
506 uflag = "/dev/ses[0-9]*";
507 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) {
521 warnx("Missing command");
525 for (i = 0; i < nbcmds; i++) {
526 if (strcmp(argv[0], cmds[i].name) == 0) {
533 warnx("unknown command %s", argv[0]);
537 return (cmd->exec(argc, argv));