1/* $FreeBSD$ */
2/*
3 * Copyright (c) 2000 by Matthew Jacob
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer,
11 *    without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * Alternatively, this software may be distributed under the terms of the
16 * the GNU Public License ("GPL").
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * Matthew Jacob
31 * Feral Software
32 * mjacob@feral.com
33 */
34
35#include <unistd.h>
36#include <stddef.h>
37#include <stdint.h>
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
41#include <sys/ioctl.h>
42#include <fcntl.h>
43#include <cam/scsi/scsi_all.h>
44#include <cam/scsi/scsi_enc.h>
45
46#include "eltsub.h"
47
48int
49main(int a, char **v)
50{
51	encioc_element_t *objp;
52	encioc_elm_status_t ob;
53	encioc_elm_desc_t objd;
54	encioc_elm_devnames_t objdn;
55	int fd, nobj, f, i, verbose, quiet, errors;
56	u_char estat;
57
58	if (a < 2) {
59		fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v);
60		return (1);
61	}
62	errors = quiet = verbose = 0;
63	if (strcmp(v[1], "-V") == 0) {
64		verbose = 2;
65		v++;
66	} else if (strcmp(v[1], "-v") == 0) {
67		verbose = 1;
68		v++;
69	} else if (strcmp(v[1], "-q") == 0) {
70		quiet = 1;
71		verbose = 0;
72		v++;
73	}
74	while (*++v) {
75
76		fd = open(*v, O_RDONLY);
77		if (fd < 0) {
78			perror(*v);
79			continue;
80		}
81		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
82			perror("ENCIOC_GETNELM");
83			(void) close(fd);
84			continue;
85		}
86		if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
87			perror("ENCIOC_GETENCSTAT");
88			(void) close(fd);
89			continue;
90		}
91		if ((verbose == 0 || quiet == 1) && estat == 0) {
92			if (quiet == 0)
93				fprintf(stdout, "%s: Enclosure OK\n", *v);
94			(void) close(fd);
95			continue;
96		}
97		fprintf(stdout, "%s: Enclosure Status ", *v);
98		if (estat == 0) {
99			fprintf(stdout, "<OK");
100		} else {
101			errors++;
102			f = '<';
103			if (estat & SES_ENCSTAT_INFO) {
104				fprintf(stdout, "%cINFO", f);
105				f = ',';
106			}
107			if (estat & SES_ENCSTAT_NONCRITICAL) {
108				fprintf(stdout, "%cNONCRITICAL", f);
109				f = ',';
110			}
111			if (estat & SES_ENCSTAT_CRITICAL) {
112				fprintf(stdout, "%cCRITICAL", f);
113				f = ',';
114			}
115			if (estat & SES_ENCSTAT_UNRECOV) {
116				fprintf(stdout, "%cUNRECOV", f);
117				f = ',';
118			}
119		}
120		fprintf(stdout, ">\n");
121		objp = calloc(nobj, sizeof (encioc_element_t));
122		if (objp == NULL) {
123			perror("calloc");
124			(void) close(fd);
125			continue;
126		}
127                if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
128                        perror("ENCIOC_GETELMMAP");
129                        (void) close(fd);
130                        continue;
131                }
132		for (i = 0; i < nobj; i++) {
133			ob.elm_idx = objp[i].elm_idx;
134			if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &ob) < 0) {
135				perror("ENCIOC_GETELMSTAT");
136				(void) close(fd);
137				break;
138			}
139			bzero(&objd, sizeof(objd));
140			objd.elm_idx = objp[i].elm_idx;
141			objd.elm_desc_len = UINT16_MAX;
142			objd.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
143			if (objd.elm_desc_str == NULL) {
144				perror("calloc");
145				(void) close(fd);
146				continue;
147			}
148			if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t)&objd) < 0) {
149				perror("ENCIOC_GETELMDESC");
150				(void) close(fd);
151				break;
152			}
153			bzero(&objdn, sizeof(objdn));
154			objdn.elm_idx = objp[i].elm_idx;
155			objdn.elm_names_size = 128;
156			objdn.elm_devnames = calloc(128, sizeof(char));
157			if (objdn.elm_devnames == NULL) {
158				perror("calloc");
159				(void) close(fd);
160				break;
161			}
162			/*
163			 * This ioctl isn't critical and has a good chance
164			 * of returning -1.
165			 */
166			(void)ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t)&objdn);
167			fprintf(stdout, "Element 0x%x: %s", ob.elm_idx,
168			    geteltnm(objp[i].elm_type));
169			fprintf(stdout, ", %s",
170			    stat2ascii(objp[i].elm_type, ob.cstat));
171			if (objd.elm_desc_len > 0)
172				fprintf(stdout, ", descriptor: '%s'",
173				    objd.elm_desc_str);
174			if (objdn.elm_names_len > 0)
175				fprintf(stdout, ", dev: '%s'",
176				    objdn.elm_devnames);
177			fprintf(stdout, "\n");
178			free(objdn.elm_devnames);
179		}
180		free(objp);
181		(void) close(fd);
182	}
183	return (errors);
184}
185