xref: /netbsd-src/usr.sbin/iopctl/iopctl.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: iopctl.c,v 1.16 2008/02/11 22:45:03 dyoung 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.16 2008/02/11 22:45:03 dyoung Exp $");
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/uio.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <stdarg.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <util.h>
57 
58 #include <dev/i2o/i2o.h>
59 #include <dev/i2o/iopio.h>
60 
61 const char	*class2str(int);
62 void	getparam(int, int, void *, int);
63 int	gettid(char **);
64 int	main(int, char **);
65 int	show(const char *, const char *, ...);
66 void	i2ostrvis(const u_char *, int, char *, int);
67 void	usage(void);
68 
69 void	reconfig(char **);
70 void	showdevid(char **);
71 void	showddmid(char **);
72 void	showlct(char **);
73 void	showstatus(char **);
74 void	showtidmap(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 	{ "showtidmap",	0, showtidmap },
107 };
108 
109 int	fd;
110 char	buf[32768];
111 struct	i2o_status status;
112 
113 int
114 main(int argc, char **argv)
115 {
116 	int ch, i;
117 	const char *dv;
118 	struct iovec iov;
119 
120 	dv = "/dev/iop0";
121 
122 	while ((ch = getopt(argc, argv, "f:")) != -1) {
123 		switch (ch) {
124 		case 'f':
125 			dv = optarg;
126 			break;
127 		default:
128 			usage();
129 			/* NOTREACHED */
130 		}
131 	}
132 
133 	if (argv[optind] == NULL)
134 		usage();
135 
136 	if ((fd = open(dv, O_RDWR)) < 0)
137 		err(EXIT_FAILURE, "%s", dv);
138 
139 	iov.iov_base = &status;
140 	iov.iov_len = sizeof(status);
141 	if (ioctl(fd, IOPIOCGSTATUS, &iov) < 0)
142 		err(EXIT_FAILURE, "IOPIOCGSTATUS");
143 
144 	for (i = 0; i < sizeof(cmdtab) / sizeof(cmdtab[0]); i++)
145 		if (strcmp(argv[optind], cmdtab[i].label) == 0) {
146 			if (cmdtab[i].takesargs == 0 &&
147 			    argv[optind + 1] != NULL)
148 			    	usage();
149 			(*cmdtab[i].func)(argv + optind + 1);
150 			break;
151 		}
152 
153 	if (i == sizeof(cmdtab) / sizeof(cmdtab[0]))
154 		errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]);
155 
156 	close(fd);
157 	exit(EXIT_SUCCESS);
158 	/* NOTREACHED */
159 }
160 
161 void
162 usage(void)
163 {
164 
165 	(void)fprintf(stderr, "usage: %s [-f dev] <command> [target]\n",
166 	    getprogname());
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 i2o_reply *rf;
204 	struct {
205 		struct	i2o_param_op_list_header olh;
206 		struct	i2o_param_op_all_template oat;
207 	} __packed req;
208 
209 	mb.msgflags = I2O_MSGFLAGS(i2o_util_params_op);
210 	mb.msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
211 	mb.flags = 0;
212 
213 	req.olh.count = htole16(1);
214 	req.olh.reserved = htole16(0);
215 	req.oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
216 	req.oat.fieldcount = htole16(0xffff);
217 	req.oat.group = htole16(group);
218 
219 	pt.pt_msg = &mb;
220 	pt.pt_msglen = sizeof(mb);
221 	pt.pt_reply = buf;
222 	pt.pt_replylen = sizeof(buf);
223 	pt.pt_timo = 10000;
224 	pt.pt_nbufs = 2;
225 
226 	pt.pt_bufs[0].ptb_data = &req;
227 	pt.pt_bufs[0].ptb_datalen = sizeof(req);
228 	pt.pt_bufs[0].ptb_out = 1;
229 
230 	pt.pt_bufs[1].ptb_data = pbuf;
231 	pt.pt_bufs[1].ptb_datalen = pbufsize;
232 	pt.pt_bufs[1].ptb_out = 0;
233 
234 	if (ioctl(fd, IOPIOCPT, &pt) < 0)
235 		err(EXIT_FAILURE, "IOPIOCPT");
236 
237 	rf = (struct i2o_reply *)buf;
238 	if ((rf->msgflags & I2O_MSGFLAGS_FAIL) != 0)
239 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (FAIL)");
240 	if (rf->reqstatus != 0)
241 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (%d)",
242 		    ((struct i2o_reply *)buf)->reqstatus);
243 }
244 
245 void
246 showlct(char **argv)
247 {
248 	struct iovec iov;
249 	struct i2o_lct *lct;
250 	struct i2o_lct_entry *ent;
251 	u_int32_t classid, usertid;
252 	int i, nent;
253 	char ident[sizeof(ent->identitytag) * 4 + 1];
254 
255 	iov.iov_base = buf;
256 	iov.iov_len = sizeof(buf);
257 
258 	if (ioctl(fd, IOPIOCGLCT, &iov) < 0)
259 		err(EXIT_FAILURE, "IOPIOCGLCT");
260 
261 	lct = (struct i2o_lct *)buf;
262 	ent = lct->entry;
263 	nent = ((le16toh(lct->tablesize) << 2) -
264 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
265 	    sizeof(struct i2o_lct_entry);
266 
267 	show("flags", "0x%x", le16toh(lct->flags));
268 	show("iop flags", "0x%x", le32toh(lct->iopflags));
269 	show("lct change indicator", "%d", le32toh(lct->changeindicator));
270 	printf("\n");
271 
272 	for (i = 0; i < nent; i++, ent++) {
273 		classid = le32toh(ent->classid);
274 		usertid = le32toh(ent->usertid);
275 
276 		show("lct entry", "%d", i);
277 		show("entry size", "%d bytes", le16toh(ent->entrysize) << 2);
278 		show("local tid", "%d", le16toh(ent->localtid) & 4095);
279 		show("change indicator", "%d", le32toh(ent->changeindicator));
280 		show("flags", "%x", le32toh(ent->deviceflags));
281 		show("class id", "%x (%s)", classid & 4095,
282 		    class2str(classid & 4095));
283 		show("version", "%x", (classid >> 12) & 15);
284 		show("organisation id", "%x", classid >> 16);
285 		show("subclass info", "%x", le32toh(ent->subclassinfo));
286 		show("user tid", "%d", usertid & 4095);
287 		show("parent tid", "%d", (usertid >> 12) & 4095);
288 		show("bios info", "%d", (usertid >> 24) & 255);
289 		i2ostrvis(ent->identitytag, sizeof(ent->identitytag), ident,
290 		    sizeof(ident));
291 		show("identity tag", "<%s>", ident);
292 		show("event caps", "%x", le32toh(ent->eventcaps));
293 
294 		if (i != nent - 1)
295 			printf("\n");
296 	}
297 }
298 
299 void
300 showstatus(char **argv)
301 {
302 	char ident[sizeof(status.productid) + 1];
303 	u_int32_t segnumber;
304 
305 	i2ostrvis(status.productid, sizeof(status.productid),
306 	    ident, sizeof(ident));
307 
308 	segnumber = le32toh(status.segnumber);
309 	show("organization id", "%d", le16toh(status.orgid));
310 	show("iop id", "%d", le32toh(status.iopid) & 4095);
311 	show("host unit id", "%d", (le32toh(status.iopid) >> 16));
312 	show("segment number", "%d", segnumber & 4095);
313 	show("i2o version", "%d", (segnumber >> 12) & 15);
314 	show("iop state", "%d", (segnumber >> 16) & 255);
315 	show("messenger type", "%d", segnumber >> 24);
316 	show("inbound frame sz", "%d", le32toh(status.inboundmframesize));
317 	show("init code", "%d", status.initcode);
318 	show("max inbound queue depth", "%d",
319 	    le32toh(status.maxinboundmframes));
320 	show("inbound queue depth", "%d",
321 	    le32toh(status.currentinboundmframes));
322 	show("max outbound queue depth", "%d",
323 	    le32toh(status.maxoutboundmframes));
324 	show("product id string", "<%s>", ident);
325 	show("expected lct size", "%d", le32toh(status.expectedlctsize));
326 	show("iop capabilities", "0x%08x", le32toh(status.iopcaps));
327 	show("desired priv mem sz", "0x%08x",
328 	    le32toh(status.desiredprivmemsize));
329 	show("current priv mem sz", "0x%08x",
330 	    le32toh(status.currentprivmemsize));
331 	show("current priv mem base", "0x%08x",
332 	    le32toh(status.currentprivmembase));
333 	show("desired priv io sz", "0x%08x",
334 	    le32toh(status.desiredpriviosize));
335 	show("current priv io sz", "0x%08x",
336 	    le32toh(status.currentpriviosize));
337 	show("current priv io base", "0x%08x",
338 	    le32toh(status.currentpriviobase));
339 }
340 
341 void
342 showddmid(char **argv)
343 {
344 	struct {
345 		struct	i2o_param_op_results pr;
346 		struct	i2o_param_read_results prr;
347 		struct	i2o_param_ddm_identity di;
348 		char padding[128];
349 	} __packed p;
350 	char ident[128];
351 
352 	getparam(gettid(argv), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p));
353 
354 	show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095);
355 	i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident));
356 	show("module name", "%s", ident);
357 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
358 	show("module revision", "%s", ident);
359 	show("serial # format", "%d", p.di.snformat);
360 	show("serial #", "%08x%08x%08x", *(u_int32_t *)&p.di.serialnumber[0],
361 	    *(u_int32_t *)&p.di.serialnumber[4],
362 	    *(u_int32_t *)&p.di.serialnumber[8]);
363 }
364 
365 void
366 showdevid(char **argv)
367 {
368 	struct {
369 		struct	i2o_param_op_results pr;
370 		struct	i2o_param_read_results prr;
371 		struct	i2o_param_device_identity di;
372 		char padding[128];
373 	} __packed p;
374 	char ident[128];
375 
376 	getparam(gettid(argv), I2O_PARAM_DEVICE_IDENTITY, &p, sizeof(p));
377 
378 	show("class id", "%d (%s)", le32toh(p.di.classid) & 4095,
379 	    class2str(le32toh(p.di.classid) & 4095));
380 	show("owner tid", "%d", le32toh(p.di.ownertid) & 4095);
381 	show("parent tid", "%d", le32toh(p.di.parenttid) & 4095);
382 
383 	i2ostrvis(p.di.vendorinfo, sizeof(p.di.vendorinfo), ident,
384 	    sizeof(ident));
385 	show("vendor", "<%s>", ident);
386 
387 	i2ostrvis(p.di.productinfo, sizeof(p.di.productinfo), ident,
388 	    sizeof(ident));
389 	show("product", "<%s>", ident);
390 
391 	i2ostrvis(p.di.description, sizeof(p.di.description), ident,
392 	    sizeof(ident));
393 	show("description", "<%s>", ident);
394 
395 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
396 	show("revision level", "<%s>", ident);
397 }
398 
399 void
400 reconfig(char **argv)
401 {
402 
403 	if (ioctl(fd, IOPIOCRECONFIG))
404 		err(EXIT_FAILURE, "IOPIOCRECONFIG");
405 }
406 
407 void
408 showtidmap(char **argv)
409 {
410 	struct iovec iov;
411 	struct iop_tidmap *it;
412 	int nent;
413 
414 	iov.iov_base = buf;
415 	iov.iov_len = sizeof(buf);
416 
417 	if (ioctl(fd, IOPIOCGTIDMAP, &iov) < 0)
418 		err(EXIT_FAILURE, "IOPIOCGTIDMAP");
419 
420 	nent = iov.iov_len / sizeof(*it);
421 	it = (struct iop_tidmap *)buf;
422 
423 	for (; nent-- != 0; it++)
424 		if ((it->it_flags & IT_CONFIGURED) != 0)
425 			printf("%s\ttid %d\n", it->it_dvname, it->it_tid);
426 }
427 
428 void
429 i2ostrvis(const u_char *src, int slen, char *dst, int dlen)
430 {
431 	int hc, lc, i, nit;
432 
433 	dlen--;
434 	lc = 0;
435 	hc = 0;
436 	i = 0;
437 
438 	/*
439 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
440 	 * spec has nothing to say about it.  Since AMI fields are usually
441 	 * filled with junk after the terminator, ...
442 	 */
443 	nit = (le16toh(status.orgid) != I2O_ORG_DPT);
444 
445 	while (slen-- != 0 && dlen-- != 0) {
446 		if (nit && *src == '\0')
447 			break;
448 		else if (*src <= 0x20 || *src >= 0x7f) {
449 			if (hc)
450 				dst[i++] = ' ';
451 		} else {
452 			hc = 1;
453 			dst[i++] = *src;
454 			lc = i;
455 		}
456 		src++;
457 	}
458 
459 	dst[lc] = '\0';
460 }
461 
462 int
463 gettid(char **argv)
464 {
465 	char *argp;
466 	int tid;
467 
468 	if (argv[1] != NULL)
469 		usage();
470 
471 	tid = (int)strtol(argv[0], &argp, 0);
472 	if (*argp != '\0')
473 		usage();
474 
475 	return (tid);
476 }
477