xref: /netbsd-src/usr.sbin/iopctl/iopctl.c (revision bcc8ec9959e7b01e313d813067bfb43a3ad70551)
1 /*	$NetBSD: iopctl.c,v 1.6 2001/01/04 06:16:02 itojun Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: iopctl.c,v 1.6 2001/01/04 06:16:02 itojun Exp $");
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/uio.h>
47 #include <sys/device.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <stdarg.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <util.h>
58 #include <getopt.h>
59 
60 #include <dev/i2o/i2o.h>
61 #include <dev/i2o/iopvar.h>
62 
63 const char	*class2str(int);
64 void	getparam(int, int, void *, int);
65 int	main(int, char *[]);
66 int	show(const char *, const char *, ...);
67 void	i2ostrvis(const char *, int, char *, int);
68 void	usage(void);
69 
70 void	reconfig(char **);
71 void	showdevid(char **);
72 void	showddmid(char **);
73 void	showlct(char **);
74 void	showstatus(char **);
75 
76 struct {
77 	int	class;
78 	const char	*caption;
79 } const i2oclass[] = {
80 	{ I2O_CLASS_EXECUTIVE, "executive" },
81 	{ I2O_CLASS_DDM, "device driver module" },
82 	{ I2O_CLASS_RANDOM_BLOCK_STORAGE, "random block storage" },
83 	{ I2O_CLASS_SEQUENTIAL_STORAGE,	"sequential storage" },
84 	{ I2O_CLASS_LAN, "LAN port" },
85 	{ I2O_CLASS_WAN, "WAN port" },
86 	{ I2O_CLASS_FIBRE_CHANNEL_PORT,	"fibrechannel port" },
87 	{ I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL, "fibrechannel peripheral" },
88  	{ I2O_CLASS_SCSI_PERIPHERAL, "SCSI peripheral" },
89 	{ I2O_CLASS_ATE_PORT, "ATE port" },
90 	{ I2O_CLASS_ATE_PERIPHERAL, "ATE peripheral" },
91 	{ I2O_CLASS_FLOPPY_CONTROLLER, "floppy controller" },
92 	{ I2O_CLASS_FLOPPY_DEVICE, "floppy device" },
93 	{ I2O_CLASS_BUS_ADAPTER_PORT, "bus adapter port" },
94 };
95 
96 struct {
97 	const char	*label;
98 	int	takesargs;
99 	void	(*func)(char **);
100 } const cmdtab[] = {
101 	{ "reconfig", 0, reconfig },
102 	{ "showddmid", 1, showddmid },
103 	{ "showdevid", 1, showdevid },
104 	{ "showlct", 0, showlct },
105 	{ "showstatus",	0, showstatus },
106 };
107 
108 int	fd;
109 char	buf[32768];
110 struct	i2o_status status;
111 
112 int
113 main(int argc, char **argv)
114 {
115 	int ch, i;
116 	const char *dv;
117 	struct iovec iov;
118 
119 	dv = "/dev/iop0";
120 
121 	while ((ch = getopt(argc, argv, "f:")) != -1) {
122 		switch (ch) {
123 		case 'f':
124 			dv = optarg;
125 			break;
126 		default:
127 			usage();
128 			/* NOTREACHED */
129 		}
130 	}
131 
132 	if (argv[optind] == NULL)
133 		usage();
134 
135 	if ((fd = open(dv, O_RDWR)) < 0)
136 		err(EXIT_FAILURE, "%s", dv);
137 
138 	iov.iov_base = &status;
139 	iov.iov_len = sizeof(status);
140 	if (ioctl(fd, IOPIOCGSTATUS, &iov) < 0)
141 		err(EXIT_FAILURE, "IOPIOCGSTATUS");
142 
143 	for (i = 0; i < sizeof(cmdtab) / sizeof(cmdtab[0]); i++)
144 		if (strcmp(argv[optind], cmdtab[i].label) == 0) {
145 			if (cmdtab[i].takesargs == 0 &&
146 			    argv[optind + 1] != NULL)
147 			    	usage();
148 			(*cmdtab[i].func)(argv + optind + 1);
149 			break;
150 		}
151 
152 	if (i == sizeof(cmdtab) / sizeof(cmdtab[0]))
153 		usage();
154 
155 	close(fd);
156 	exit(EXIT_SUCCESS);
157 	/* NOTREACHED */
158 }
159 
160 void
161 usage(void)
162 {
163 	extern char *__progname;
164 
165 	(void)fprintf(stderr, "usage: %s [-f dev] <command> [target]\n",
166 	    __progname);
167 	exit(EXIT_FAILURE);
168 	/* NOTREACHED */
169 }
170 
171 int
172 show(const char *hdr, const char *fmt, ...)
173 {
174 	int i;
175 	va_list va;
176 
177 	for (i = printf("%s", hdr); i < 25; i++)
178 		putchar(' ');
179 	va_start(va, fmt);
180 	i += vprintf(fmt, va);
181 	va_end(va);
182 	putchar('\n');
183 	return (i);
184 }
185 
186 const char *
187 class2str(int class)
188 {
189 	int i;
190 
191 	for (i = 0; i < sizeof(i2oclass) / sizeof(i2oclass[0]); i++)
192 		if (class == i2oclass[i].class)
193 			return (i2oclass[i].caption);
194 
195 	return ("unknown");
196 }
197 
198 void
199 getparam(int tid, int group, void *pbuf, int pbufsize)
200 {
201 	struct ioppt pt;
202 	struct i2o_util_params_op mb;
203 	struct {
204 		struct	i2o_param_op_list_header olh;
205 		struct	i2o_param_op_all_template oat;
206 	} req;
207 
208 	mb.msgflags = I2O_MSGFLAGS(i2o_util_params_op);
209 	mb.msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
210 	mb.flags = 0;
211 
212 	req.olh.count = htole16(1);
213 	req.olh.reserved = htole16(0);
214 	req.oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
215 	req.oat.fieldcount = htole16(0xffff);
216 	req.oat.group = htole16(group);
217 
218 	pt.pt_msg = &mb;
219 	pt.pt_msglen = sizeof(mb);
220 	pt.pt_reply = buf;
221 	pt.pt_replylen = sizeof(buf);
222 	pt.pt_timo = 10000;
223 	pt.pt_nbufs = 2;
224 
225 	pt.pt_bufs[0].ptb_data = &req;
226 	pt.pt_bufs[0].ptb_datalen = sizeof(req);
227 	pt.pt_bufs[0].ptb_out = 1;
228 
229 	pt.pt_bufs[1].ptb_data = pbuf;
230 	pt.pt_bufs[1].ptb_datalen = pbufsize;
231 	pt.pt_bufs[1].ptb_out = 0;
232 
233 	if (ioctl(fd, IOPIOCPT, &pt) < 0)
234 		err(EXIT_FAILURE, "IOPIOCPT");
235 
236 	if (((struct i2o_reply *)buf)->reqstatus != 0)
237 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (%d)",
238 		    ((struct i2o_reply *)buf)->reqstatus);
239 }
240 
241 void
242 showlct(char **argv)
243 {
244 	struct iovec iov;
245 	struct i2o_lct *lct;
246 	struct i2o_lct_entry *ent;
247 	u_int32_t classid, usertid;
248 	int i, nent;
249 	char ident[sizeof(ent->identitytag) * 4 + 1];
250 
251 	iov.iov_base = buf;
252 	iov.iov_len = sizeof(buf);
253 
254 	if (ioctl(fd, IOPIOCGLCT, &iov) < 0)
255 		err(EXIT_FAILURE, "IOPIOCGLCT");
256 
257 	lct = (struct i2o_lct *)buf;
258 	ent = lct->entry;
259 	nent = ((le16toh(lct->tablesize) << 2) -
260 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
261 	    sizeof(struct i2o_lct_entry);
262 
263 	for (i = 0; i < nent; i++, ent++) {
264 		classid = le32toh(ent->classid);
265 		usertid = le32toh(ent->usertid);
266 
267 		show("lct entry", "%d", i);
268 		show("entry size", "%d bytes", le16toh(ent->entrysize) << 2);
269 		show("local tid", "%d", le16toh(ent->localtid) & 4095);
270 		show("change indicator", "%d", le32toh(ent->changeindicator));
271 		show("flags", "%x", le32toh(ent->deviceflags));
272 		show("class id", "%x (%s)", classid & 4095,
273 		    class2str(classid & 4095));
274 		show("version", "%x", (classid >> 12) & 15);
275 		show("organisation id", "%x", classid >> 16);
276 		show("subclass info", "%x", le32toh(ent->subclassinfo));
277 		show("user tid", "%d", usertid & 4095);
278 		show("parent tid", "%d", (usertid >> 12) & 4095);
279 		show("bios info", "%d", (usertid >> 24) & 255);
280 		i2ostrvis(ent->identitytag, sizeof(ent->identitytag), ident,
281 		    sizeof(ident));
282 		show("identity tag", "<%s>", ident);
283 		show("event caps", "%x", le32toh(ent->eventcaps));
284 
285 		if (i != nent - 1)
286 			printf("\n");
287 	}
288 }
289 
290 void
291 showstatus(char **argv)
292 {
293 	char ident[sizeof(status.productid) + 1];
294 	u_int32_t segnumber;
295 
296 	i2ostrvis(status.productid, sizeof(status.productid),
297 	    ident, sizeof(ident));
298 
299 	segnumber = le32toh(status.segnumber);
300 	show("organization id", "%d", le16toh(status.orgid));
301 	show("iop id", "%d", le32toh(status.iopid) & 4095);
302 	show("host unit id", "%d", (le32toh(status.iopid) >> 16));
303 	show("segment number", "%d", segnumber & 4095);
304 	show("i2o version", "%d", (segnumber >> 12) & 15);
305 	show("iop state", "%d", (segnumber >> 16) & 255);
306 	show("messenger type", "%d", segnumber >> 24);
307 	show("inbound frame sz", "%d", le32toh(status.inboundmframesize));
308 	show("init code", "%d", status.initcode);
309 	show("max inbound queue depth", "%d",
310 	    le32toh(status.maxinboundmframes));
311 	show("inbound queue depth", "%d",
312 	    le32toh(status.currentinboundmframes));
313 	show("max outbound queue depth", "%d",
314 	    le32toh(status.maxoutboundmframes));
315 	show("product id string", "<%s>", ident);
316 	show("expected lct size", "%d", le32toh(status.expectedlctsize));
317 	show("iop capabilities", "0x%08x", le32toh(status.iopcaps));
318 	show("desired priv mem sz", "0x%08x",
319 	    le32toh(status.desiredprivmemsize));
320 	show("current priv mem sz", "0x%08x",
321 	    le32toh(status.currentprivmemsize));
322 	show("current priv mem base", "0x%08x",
323 	    le32toh(status.currentprivmembase));
324 	show("desired priv io sz", "0x%08x",
325 	    le32toh(status.desiredpriviosize));
326 	show("current priv io sz", "0x%08x",
327 	    le32toh(status.currentpriviosize));
328 	show("current priv io base", "0x%08x",
329 	    le32toh(status.currentpriviobase));
330 }
331 
332 void
333 showddmid(char **argv)
334 {
335 	struct {
336 		struct	i2o_param_op_results pr;
337 		struct	i2o_param_read_results prr;
338 		struct	i2o_param_ddm_identity di;
339 	} __attribute__ ((__packed__)) p;
340 	char ident[128];
341 
342 	getparam(atoi(argv[0]), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p));
343 
344 	show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095);
345 	i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident));
346 	show("module name", "%s", ident);
347 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
348 	show("module revision", "%s", ident);
349 	show("serial # format", "%d", p.di.snformat);
350 	show("serial #", "%08x%08x%08x", *(u_int32_t *)&p.di.serialnumber[0],
351 	    *(u_int32_t *)&p.di.serialnumber[4],
352 	    *(u_int32_t *)&p.di.serialnumber[8]);
353 }
354 
355 void
356 showdevid(char **argv)
357 {
358 	struct {
359 		struct	i2o_param_op_results pr;
360 		struct	i2o_param_read_results prr;
361 		struct	i2o_param_device_identity di;
362 	} __attribute__ ((__packed__)) p;
363 	char ident[128];
364 
365 	getparam(atoi(argv[0]), I2O_PARAM_DEVICE_IDENTITY, &p, sizeof(p));
366 
367 	show("class id", "%d (%s)", le32toh(p.di.classid) & 4095,
368 	    class2str(le32toh(p.di.classid) & 4095));
369 	show("owner tid", "%d", le32toh(p.di.ownertid) & 4095);
370 	show("parent tid", "%d", le32toh(p.di.parenttid) & 4095);
371 
372 	i2ostrvis(p.di.vendorinfo, sizeof(p.di.vendorinfo), ident,
373 	    sizeof(ident));
374 	show("vendor", "<%s>", ident);
375 
376 	i2ostrvis(p.di.productinfo, sizeof(p.di.productinfo), ident,
377 	    sizeof(ident));
378 	show("product", "<%s>", ident);
379 
380 	i2ostrvis(p.di.description, sizeof(p.di.description), ident,
381 	    sizeof(ident));
382 	show("description", "<%s>", ident);
383 
384 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
385 	show("revision level", "<%s>", ident);
386 }
387 
388 void
389 reconfig(char **argv)
390 {
391 
392 	if (ioctl(fd, IOPIOCRECONFIG))
393 		err(EXIT_FAILURE, "IOPIOCRECONFIG");
394 }
395 
396 void
397 i2ostrvis(const char *src, int slen, char *dst, int dlen)
398 {
399 	int hc, lc, i, nit;
400 
401 	dlen--;
402 	lc = 0;
403 	hc = 0;
404 	i = 0;
405 
406 	/*
407 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
408 	 * spec has nothing to say about it.  Since AMI fields are usually
409 	 * filled with junk after the terminator, ...
410 	 */
411 	nit = (le16toh(status.orgid) != I2O_ORG_DPT);
412 
413 	while (slen-- != 0 && dlen-- != 0) {
414 		if (nit && *src == '\0')
415 			break;
416 		else if (*src <= 0x20 || *src >= 0x7f) {
417 			if (hc)
418 				dst[i++] = ' ';
419 		} else {
420 			hc = 1;
421 			dst[i++] = *src;
422 			lc = i;
423 		}
424 		src++;
425 	}
426 
427 	dst[lc] = '\0';
428 }
429