xref: /netbsd/usr.sbin/iopctl/iopctl.c (revision bf9ec67e)
1 /*	$NetBSD: iopctl.c,v 1.12 2002/01/04 10:17:20 ad 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.12 2002/01/04 10:17:20 ad 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/iopio.h>
62 
63 const char	*class2str(int);
64 void	getparam(int, int, void *, int);
65 int	gettid(char **);
66 int	main(int, char **);
67 int	show(const char *, const char *, ...);
68 void	i2ostrvis(const char *, int, char *, int);
69 void	usage(void);
70 
71 void	reconfig(char **);
72 void	showdevid(char **);
73 void	showddmid(char **);
74 void	showlct(char **);
75 void	showstatus(char **);
76 void	showtidmap(char **);
77 
78 struct {
79 	int	class;
80 	const char	*caption;
81 } const i2oclass[] = {
82 	{ I2O_CLASS_EXECUTIVE, "executive" },
83 	{ I2O_CLASS_DDM, "device driver module" },
84 	{ I2O_CLASS_RANDOM_BLOCK_STORAGE, "random block storage" },
85 	{ I2O_CLASS_SEQUENTIAL_STORAGE,	"sequential storage" },
86 	{ I2O_CLASS_LAN, "LAN port" },
87 	{ I2O_CLASS_WAN, "WAN port" },
88 	{ I2O_CLASS_FIBRE_CHANNEL_PORT,	"fibrechannel port" },
89 	{ I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL, "fibrechannel peripheral" },
90  	{ I2O_CLASS_SCSI_PERIPHERAL, "SCSI peripheral" },
91 	{ I2O_CLASS_ATE_PORT, "ATE port" },
92 	{ I2O_CLASS_ATE_PERIPHERAL, "ATE peripheral" },
93 	{ I2O_CLASS_FLOPPY_CONTROLLER, "floppy controller" },
94 	{ I2O_CLASS_FLOPPY_DEVICE, "floppy device" },
95 	{ I2O_CLASS_BUS_ADAPTER_PORT, "bus adapter port" },
96 };
97 
98 struct {
99 	const char	*label;
100 	int	takesargs;
101 	void	(*func)(char **);
102 } const cmdtab[] = {
103 	{ "reconfig", 0, reconfig },
104 	{ "showddmid", 1, showddmid },
105 	{ "showdevid", 1, showdevid },
106 	{ "showlct", 0, showlct },
107 	{ "showstatus",	0, showstatus },
108 	{ "showtidmap",	0, showtidmap },
109 };
110 
111 int	fd;
112 char	buf[32768];
113 struct	i2o_status status;
114 
115 int
116 main(int argc, char **argv)
117 {
118 	int ch, i;
119 	const char *dv;
120 	struct iovec iov;
121 
122 	dv = "/dev/iop0";
123 
124 	while ((ch = getopt(argc, argv, "f:")) != -1) {
125 		switch (ch) {
126 		case 'f':
127 			dv = optarg;
128 			break;
129 		default:
130 			usage();
131 			/* NOTREACHED */
132 		}
133 	}
134 
135 	if (argv[optind] == NULL)
136 		usage();
137 
138 	if ((fd = open(dv, O_RDWR)) < 0)
139 		err(EXIT_FAILURE, "%s", dv);
140 
141 	iov.iov_base = &status;
142 	iov.iov_len = sizeof(status);
143 	if (ioctl(fd, IOPIOCGSTATUS, &iov) < 0)
144 		err(EXIT_FAILURE, "IOPIOCGSTATUS");
145 
146 	for (i = 0; i < sizeof(cmdtab) / sizeof(cmdtab[0]); i++)
147 		if (strcmp(argv[optind], cmdtab[i].label) == 0) {
148 			if (cmdtab[i].takesargs == 0 &&
149 			    argv[optind + 1] != NULL)
150 			    	usage();
151 			(*cmdtab[i].func)(argv + optind + 1);
152 			break;
153 		}
154 
155 	if (i == sizeof(cmdtab) / sizeof(cmdtab[0]))
156 		errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]);
157 
158 	close(fd);
159 	exit(EXIT_SUCCESS);
160 	/* NOTREACHED */
161 }
162 
163 void
164 usage(void)
165 {
166 
167 	(void)fprintf(stderr, "usage: %s [-f dev] <command> [target]\n",
168 	    getprogname());
169 	exit(EXIT_FAILURE);
170 	/* NOTREACHED */
171 }
172 
173 int
174 show(const char *hdr, const char *fmt, ...)
175 {
176 	int i;
177 	va_list va;
178 
179 	for (i = printf("%s", hdr); i < 25; i++)
180 		putchar(' ');
181 	va_start(va, fmt);
182 	i += vprintf(fmt, va);
183 	va_end(va);
184 	putchar('\n');
185 	return (i);
186 }
187 
188 const char *
189 class2str(int class)
190 {
191 	int i;
192 
193 	for (i = 0; i < sizeof(i2oclass) / sizeof(i2oclass[0]); i++)
194 		if (class == i2oclass[i].class)
195 			return (i2oclass[i].caption);
196 
197 	return ("unknown");
198 }
199 
200 void
201 getparam(int tid, int group, void *pbuf, int pbufsize)
202 {
203 	struct ioppt pt;
204 	struct i2o_util_params_op mb;
205 	struct i2o_reply *rf;
206 	struct {
207 		struct	i2o_param_op_list_header olh;
208 		struct	i2o_param_op_all_template oat;
209 	} __attribute__ ((__packed__)) req;
210 
211 	mb.msgflags = I2O_MSGFLAGS(i2o_util_params_op);
212 	mb.msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
213 	mb.flags = 0;
214 
215 	req.olh.count = htole16(1);
216 	req.olh.reserved = htole16(0);
217 	req.oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
218 	req.oat.fieldcount = htole16(0xffff);
219 	req.oat.group = htole16(group);
220 
221 	pt.pt_msg = &mb;
222 	pt.pt_msglen = sizeof(mb);
223 	pt.pt_reply = buf;
224 	pt.pt_replylen = sizeof(buf);
225 	pt.pt_timo = 10000;
226 	pt.pt_nbufs = 2;
227 
228 	pt.pt_bufs[0].ptb_data = &req;
229 	pt.pt_bufs[0].ptb_datalen = sizeof(req);
230 	pt.pt_bufs[0].ptb_out = 1;
231 
232 	pt.pt_bufs[1].ptb_data = pbuf;
233 	pt.pt_bufs[1].ptb_datalen = pbufsize;
234 	pt.pt_bufs[1].ptb_out = 0;
235 
236 	if (ioctl(fd, IOPIOCPT, &pt) < 0)
237 		err(EXIT_FAILURE, "IOPIOCPT");
238 
239 	rf = (struct i2o_reply *)buf;
240 	if ((rf->msgflags & I2O_MSGFLAGS_FAIL) != 0)
241 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (FAIL)");
242 	if (rf->reqstatus != 0)
243 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (%d)",
244 		    ((struct i2o_reply *)buf)->reqstatus);
245 }
246 
247 void
248 showlct(char **argv)
249 {
250 	struct iovec iov;
251 	struct i2o_lct *lct;
252 	struct i2o_lct_entry *ent;
253 	u_int32_t classid, usertid;
254 	int i, nent;
255 	char ident[sizeof(ent->identitytag) * 4 + 1];
256 
257 	iov.iov_base = buf;
258 	iov.iov_len = sizeof(buf);
259 
260 	if (ioctl(fd, IOPIOCGLCT, &iov) < 0)
261 		err(EXIT_FAILURE, "IOPIOCGLCT");
262 
263 	lct = (struct i2o_lct *)buf;
264 	ent = lct->entry;
265 	nent = ((le16toh(lct->tablesize) << 2) -
266 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
267 	    sizeof(struct i2o_lct_entry);
268 
269 	show("flags", "0x%x", le16toh(lct->flags));
270 	show("iop flags", "0x%x", le32toh(lct->iopflags));
271 	show("lct change indicator", "%d", le32toh(lct->changeindicator));
272 	printf("\n");
273 
274 	for (i = 0; i < nent; i++, ent++) {
275 		classid = le32toh(ent->classid);
276 		usertid = le32toh(ent->usertid);
277 
278 		show("lct entry", "%d", i);
279 		show("entry size", "%d bytes", le16toh(ent->entrysize) << 2);
280 		show("local tid", "%d", le16toh(ent->localtid) & 4095);
281 		show("change indicator", "%d", le32toh(ent->changeindicator));
282 		show("flags", "%x", le32toh(ent->deviceflags));
283 		show("class id", "%x (%s)", classid & 4095,
284 		    class2str(classid & 4095));
285 		show("version", "%x", (classid >> 12) & 15);
286 		show("organisation id", "%x", classid >> 16);
287 		show("subclass info", "%x", le32toh(ent->subclassinfo));
288 		show("user tid", "%d", usertid & 4095);
289 		show("parent tid", "%d", (usertid >> 12) & 4095);
290 		show("bios info", "%d", (usertid >> 24) & 255);
291 		i2ostrvis(ent->identitytag, sizeof(ent->identitytag), ident,
292 		    sizeof(ident));
293 		show("identity tag", "<%s>", ident);
294 		show("event caps", "%x", le32toh(ent->eventcaps));
295 
296 		if (i != nent - 1)
297 			printf("\n");
298 	}
299 }
300 
301 void
302 showstatus(char **argv)
303 {
304 	char ident[sizeof(status.productid) + 1];
305 	u_int32_t segnumber;
306 
307 	i2ostrvis(status.productid, sizeof(status.productid),
308 	    ident, sizeof(ident));
309 
310 	segnumber = le32toh(status.segnumber);
311 	show("organization id", "%d", le16toh(status.orgid));
312 	show("iop id", "%d", le32toh(status.iopid) & 4095);
313 	show("host unit id", "%d", (le32toh(status.iopid) >> 16));
314 	show("segment number", "%d", segnumber & 4095);
315 	show("i2o version", "%d", (segnumber >> 12) & 15);
316 	show("iop state", "%d", (segnumber >> 16) & 255);
317 	show("messenger type", "%d", segnumber >> 24);
318 	show("inbound frame sz", "%d", le32toh(status.inboundmframesize));
319 	show("init code", "%d", status.initcode);
320 	show("max inbound queue depth", "%d",
321 	    le32toh(status.maxinboundmframes));
322 	show("inbound queue depth", "%d",
323 	    le32toh(status.currentinboundmframes));
324 	show("max outbound queue depth", "%d",
325 	    le32toh(status.maxoutboundmframes));
326 	show("product id string", "<%s>", ident);
327 	show("expected lct size", "%d", le32toh(status.expectedlctsize));
328 	show("iop capabilities", "0x%08x", le32toh(status.iopcaps));
329 	show("desired priv mem sz", "0x%08x",
330 	    le32toh(status.desiredprivmemsize));
331 	show("current priv mem sz", "0x%08x",
332 	    le32toh(status.currentprivmemsize));
333 	show("current priv mem base", "0x%08x",
334 	    le32toh(status.currentprivmembase));
335 	show("desired priv io sz", "0x%08x",
336 	    le32toh(status.desiredpriviosize));
337 	show("current priv io sz", "0x%08x",
338 	    le32toh(status.currentpriviosize));
339 	show("current priv io base", "0x%08x",
340 	    le32toh(status.currentpriviobase));
341 }
342 
343 void
344 showddmid(char **argv)
345 {
346 	struct {
347 		struct	i2o_param_op_results pr;
348 		struct	i2o_param_read_results prr;
349 		struct	i2o_param_ddm_identity di;
350 		char padding[128];
351 	} __attribute__ ((__packed__)) p;
352 	char ident[128];
353 
354 	getparam(gettid(argv), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p));
355 
356 	show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095);
357 	i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident));
358 	show("module name", "%s", ident);
359 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
360 	show("module revision", "%s", ident);
361 	show("serial # format", "%d", p.di.snformat);
362 	show("serial #", "%08x%08x%08x", *(u_int32_t *)&p.di.serialnumber[0],
363 	    *(u_int32_t *)&p.di.serialnumber[4],
364 	    *(u_int32_t *)&p.di.serialnumber[8]);
365 }
366 
367 void
368 showdevid(char **argv)
369 {
370 	struct {
371 		struct	i2o_param_op_results pr;
372 		struct	i2o_param_read_results prr;
373 		struct	i2o_param_device_identity di;
374 		char padding[128];
375 	} __attribute__ ((__packed__)) p;
376 	char ident[128];
377 
378 	getparam(gettid(argv), I2O_PARAM_DEVICE_IDENTITY, &p, sizeof(p));
379 
380 	show("class id", "%d (%s)", le32toh(p.di.classid) & 4095,
381 	    class2str(le32toh(p.di.classid) & 4095));
382 	show("owner tid", "%d", le32toh(p.di.ownertid) & 4095);
383 	show("parent tid", "%d", le32toh(p.di.parenttid) & 4095);
384 
385 	i2ostrvis(p.di.vendorinfo, sizeof(p.di.vendorinfo), ident,
386 	    sizeof(ident));
387 	show("vendor", "<%s>", ident);
388 
389 	i2ostrvis(p.di.productinfo, sizeof(p.di.productinfo), ident,
390 	    sizeof(ident));
391 	show("product", "<%s>", ident);
392 
393 	i2ostrvis(p.di.description, sizeof(p.di.description), ident,
394 	    sizeof(ident));
395 	show("description", "<%s>", ident);
396 
397 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
398 	show("revision level", "<%s>", ident);
399 }
400 
401 void
402 reconfig(char **argv)
403 {
404 
405 	if (ioctl(fd, IOPIOCRECONFIG))
406 		err(EXIT_FAILURE, "IOPIOCRECONFIG");
407 }
408 
409 void
410 showtidmap(char **argv)
411 {
412 	struct iovec iov;
413 	struct iop_tidmap *it;
414 	int nent;
415 
416 	iov.iov_base = buf;
417 	iov.iov_len = sizeof(buf);
418 
419 	if (ioctl(fd, IOPIOCGTIDMAP, &iov) < 0)
420 		err(EXIT_FAILURE, "IOPIOCGTIDMAP");
421 
422 	nent = iov.iov_len / sizeof(*it);
423 	it = (struct iop_tidmap *)buf;
424 
425 	for (; nent-- != 0; it++)
426 		if ((it->it_flags & IT_CONFIGURED) != 0)
427 			printf("%s\ttid %d\n", it->it_dvname, it->it_tid);
428 }
429 
430 void
431 i2ostrvis(const char *src, int slen, char *dst, int dlen)
432 {
433 	int hc, lc, i, nit;
434 
435 	dlen--;
436 	lc = 0;
437 	hc = 0;
438 	i = 0;
439 
440 	/*
441 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
442 	 * spec has nothing to say about it.  Since AMI fields are usually
443 	 * filled with junk after the terminator, ...
444 	 */
445 	nit = (le16toh(status.orgid) != I2O_ORG_DPT);
446 
447 	while (slen-- != 0 && dlen-- != 0) {
448 		if (nit && *src == '\0')
449 			break;
450 		else if (*src <= 0x20 || *src >= 0x7f) {
451 			if (hc)
452 				dst[i++] = ' ';
453 		} else {
454 			hc = 1;
455 			dst[i++] = *src;
456 			lc = i;
457 		}
458 		src++;
459 	}
460 
461 	dst[lc] = '\0';
462 }
463 
464 int
465 gettid(char **argv)
466 {
467 	char *argp;
468 	int tid;
469 
470 	if (argv[1] != NULL)
471 		usage();
472 
473 	tid = (int)strtol(argv[0], &argp, 0);
474 	if (*argp != '\0')
475 		usage();
476 
477 	return (tid);
478 }
479