iopctl.c revision 1.11.2.2       1 /*	$NetBSD: iopctl.c,v 1.11.2.2 2001/12/09 18:38:09 he 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.11.2.2 2001/12/09 18:38:09 he 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 	extern char *__progname;
    167 
    168 	(void)fprintf(stderr, "usage: %s [-f dev] <command> [target]\n",
    169 	    __progname);
    170 	exit(EXIT_FAILURE);
    171 	/* NOTREACHED */
    172 }
    173 
    174 int
    175 show(const char *hdr, const char *fmt, ...)
    176 {
    177 	int i;
    178 	va_list va;
    179 
    180 	for (i = printf("%s", hdr); i < 25; i++)
    181 		putchar(' ');
    182 	va_start(va, fmt);
    183 	i += vprintf(fmt, va);
    184 	va_end(va);
    185 	putchar('\n');
    186 	return (i);
    187 }
    188 
    189 const char *
    190 class2str(int class)
    191 {
    192 	int i;
    193 
    194 	for (i = 0; i < sizeof(i2oclass) / sizeof(i2oclass[0]); i++)
    195 		if (class == i2oclass[i].class)
    196 			return (i2oclass[i].caption);
    197 
    198 	return ("unknown");
    199 }
    200 
    201 void
    202 getparam(int tid, int group, void *pbuf, int pbufsize)
    203 {
    204 	struct ioppt pt;
    205 	struct i2o_util_params_op mb;
    206 	struct i2o_reply *rf;
    207 	struct {
    208 		struct	i2o_param_op_list_header olh;
    209 		struct	i2o_param_op_all_template oat;
    210 	} __attribute__ ((__packed__)) req;
    211 
    212 	mb.msgflags = I2O_MSGFLAGS(i2o_util_params_op);
    213 	mb.msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
    214 	mb.flags = 0;
    215 
    216 	req.olh.count = htole16(1);
    217 	req.olh.reserved = htole16(0);
    218 	req.oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
    219 	req.oat.fieldcount = htole16(0xffff);
    220 	req.oat.group = htole16(group);
    221 
    222 	pt.pt_msg = &mb;
    223 	pt.pt_msglen = sizeof(mb);
    224 	pt.pt_reply = buf;
    225 	pt.pt_replylen = sizeof(buf);
    226 	pt.pt_timo = 10000;
    227 	pt.pt_nbufs = 2;
    228 
    229 	pt.pt_bufs[0].ptb_data = &req;
    230 	pt.pt_bufs[0].ptb_datalen = sizeof(req);
    231 	pt.pt_bufs[0].ptb_out = 1;
    232 
    233 	pt.pt_bufs[1].ptb_data = pbuf;
    234 	pt.pt_bufs[1].ptb_datalen = pbufsize;
    235 	pt.pt_bufs[1].ptb_out = 0;
    236 
    237 	if (ioctl(fd, IOPIOCPT, &pt) < 0)
    238 		err(EXIT_FAILURE, "IOPIOCPT");
    239 
    240 	rf = (struct i2o_reply *)buf;
    241 	if ((rf->msgflags & I2O_MSGFLAGS_FAIL) != 0)
    242 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (FAIL)");
    243 	if (rf->reqstatus != 0)
    244 		errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (%d)",
    245 		    ((struct i2o_reply *)buf)->reqstatus);
    246 }
    247 
    248 void
    249 showlct(char **argv)
    250 {
    251 	struct iovec iov;
    252 	struct i2o_lct *lct;
    253 	struct i2o_lct_entry *ent;
    254 	u_int32_t classid, usertid;
    255 	int i, nent;
    256 	char ident[sizeof(ent->identitytag) * 4 + 1];
    257 
    258 	iov.iov_base = buf;
    259 	iov.iov_len = sizeof(buf);
    260 
    261 	if (ioctl(fd, IOPIOCGLCT, &iov) < 0)
    262 		err(EXIT_FAILURE, "IOPIOCGLCT");
    263 
    264 	lct = (struct i2o_lct *)buf;
    265 	ent = lct->entry;
    266 	nent = ((le16toh(lct->tablesize) << 2) -
    267 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
    268 	    sizeof(struct i2o_lct_entry);
    269 
    270 	for (i = 0; i < nent; i++, ent++) {
    271 		classid = le32toh(ent->classid);
    272 		usertid = le32toh(ent->usertid);
    273 
    274 		show("lct entry", "%d", i);
    275 		show("entry size", "%d bytes", le16toh(ent->entrysize) << 2);
    276 		show("local tid", "%d", le16toh(ent->localtid) & 4095);
    277 		show("change indicator", "%d", le32toh(ent->changeindicator));
    278 		show("flags", "%x", le32toh(ent->deviceflags));
    279 		show("class id", "%x (%s)", classid & 4095,
    280 		    class2str(classid & 4095));
    281 		show("version", "%x", (classid >> 12) & 15);
    282 		show("organisation id", "%x", classid >> 16);
    283 		show("subclass info", "%x", le32toh(ent->subclassinfo));
    284 		show("user tid", "%d", usertid & 4095);
    285 		show("parent tid", "%d", (usertid >> 12) & 4095);
    286 		show("bios info", "%d", (usertid >> 24) & 255);
    287 		i2ostrvis(ent->identitytag, sizeof(ent->identitytag), ident,
    288 		    sizeof(ident));
    289 		show("identity tag", "<%s>", ident);
    290 		show("event caps", "%x", le32toh(ent->eventcaps));
    291 
    292 		if (i != nent - 1)
    293 			printf("\n");
    294 	}
    295 }
    296 
    297 void
    298 showstatus(char **argv)
    299 {
    300 	char ident[sizeof(status.productid) + 1];
    301 	u_int32_t segnumber;
    302 
    303 	i2ostrvis(status.productid, sizeof(status.productid),
    304 	    ident, sizeof(ident));
    305 
    306 	segnumber = le32toh(status.segnumber);
    307 	show("organization id", "%d", le16toh(status.orgid));
    308 	show("iop id", "%d", le32toh(status.iopid) & 4095);
    309 	show("host unit id", "%d", (le32toh(status.iopid) >> 16));
    310 	show("segment number", "%d", segnumber & 4095);
    311 	show("i2o version", "%d", (segnumber >> 12) & 15);
    312 	show("iop state", "%d", (segnumber >> 16) & 255);
    313 	show("messenger type", "%d", segnumber >> 24);
    314 	show("inbound frame sz", "%d", le32toh(status.inboundmframesize));
    315 	show("init code", "%d", status.initcode);
    316 	show("max inbound queue depth", "%d",
    317 	    le32toh(status.maxinboundmframes));
    318 	show("inbound queue depth", "%d",
    319 	    le32toh(status.currentinboundmframes));
    320 	show("max outbound queue depth", "%d",
    321 	    le32toh(status.maxoutboundmframes));
    322 	show("product id string", "<%s>", ident);
    323 	show("expected lct size", "%d", le32toh(status.expectedlctsize));
    324 	show("iop capabilities", "0x%08x", le32toh(status.iopcaps));
    325 	show("desired priv mem sz", "0x%08x",
    326 	    le32toh(status.desiredprivmemsize));
    327 	show("current priv mem sz", "0x%08x",
    328 	    le32toh(status.currentprivmemsize));
    329 	show("current priv mem base", "0x%08x",
    330 	    le32toh(status.currentprivmembase));
    331 	show("desired priv io sz", "0x%08x",
    332 	    le32toh(status.desiredpriviosize));
    333 	show("current priv io sz", "0x%08x",
    334 	    le32toh(status.currentpriviosize));
    335 	show("current priv io base", "0x%08x",
    336 	    le32toh(status.currentpriviobase));
    337 }
    338 
    339 void
    340 showddmid(char **argv)
    341 {
    342 	struct {
    343 		struct	i2o_param_op_results pr;
    344 		struct	i2o_param_read_results prr;
    345 		struct	i2o_param_ddm_identity di;
    346 		char padding[128];
    347 	} __attribute__ ((__packed__)) p;
    348 	char ident[128];
    349 
    350 	getparam(gettid(argv), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p));
    351 
    352 	show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095);
    353 	i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident));
    354 	show("module name", "%s", ident);
    355 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
    356 	show("module revision", "%s", ident);
    357 	show("serial # format", "%d", p.di.snformat);
    358 	show("serial #", "%08x%08x%08x", *(u_int32_t *)&p.di.serialnumber[0],
    359 	    *(u_int32_t *)&p.di.serialnumber[4],
    360 	    *(u_int32_t *)&p.di.serialnumber[8]);
    361 }
    362 
    363 void
    364 showdevid(char **argv)
    365 {
    366 	struct {
    367 		struct	i2o_param_op_results pr;
    368 		struct	i2o_param_read_results prr;
    369 		struct	i2o_param_device_identity di;
    370 		char padding[128];
    371 	} __attribute__ ((__packed__)) p;
    372 	char ident[128];
    373 
    374 	getparam(gettid(argv), I2O_PARAM_DEVICE_IDENTITY, &p, sizeof(p));
    375 
    376 	show("class id", "%d (%s)", le32toh(p.di.classid) & 4095,
    377 	    class2str(le32toh(p.di.classid) & 4095));
    378 	show("owner tid", "%d", le32toh(p.di.ownertid) & 4095);
    379 	show("parent tid", "%d", le32toh(p.di.parenttid) & 4095);
    380 
    381 	i2ostrvis(p.di.vendorinfo, sizeof(p.di.vendorinfo), ident,
    382 	    sizeof(ident));
    383 	show("vendor", "<%s>", ident);
    384 
    385 	i2ostrvis(p.di.productinfo, sizeof(p.di.productinfo), ident,
    386 	    sizeof(ident));
    387 	show("product", "<%s>", ident);
    388 
    389 	i2ostrvis(p.di.description, sizeof(p.di.description), ident,
    390 	    sizeof(ident));
    391 	show("description", "<%s>", ident);
    392 
    393 	i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident));
    394 	show("revision level", "<%s>", ident);
    395 }
    396 
    397 void
    398 reconfig(char **argv)
    399 {
    400 
    401 	if (ioctl(fd, IOPIOCRECONFIG))
    402 		err(EXIT_FAILURE, "IOPIOCRECONFIG");
    403 }
    404 
    405 void
    406 showtidmap(char **argv)
    407 {
    408 	struct iovec iov;
    409 	struct iop_tidmap *it;
    410 	int nent;
    411 
    412 	iov.iov_base = buf;
    413 	iov.iov_len = sizeof(buf);
    414 
    415 	if (ioctl(fd, IOPIOCGTIDMAP, &iov) < 0)
    416 		err(EXIT_FAILURE, "IOPIOCGTIDMAP");
    417 
    418 	nent = iov.iov_len / sizeof(*it);
    419 	it = (struct iop_tidmap *)buf;
    420 
    421 	for (; nent-- != 0; it++)
    422 		if ((it->it_flags & IT_CONFIGURED) != 0)
    423 			printf("%s\ttid %d\n", it->it_dvname, it->it_tid);
    424 }
    425 
    426 void
    427 i2ostrvis(const char *src, int slen, char *dst, int dlen)
    428 {
    429 	int hc, lc, i, nit;
    430 
    431 	dlen--;
    432 	lc = 0;
    433 	hc = 0;
    434 	i = 0;
    435 
    436 	/*
    437 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
    438 	 * spec has nothing to say about it.  Since AMI fields are usually
    439 	 * filled with junk after the terminator, ...
    440 	 */
    441 	nit = (le16toh(status.orgid) != I2O_ORG_DPT);
    442 
    443 	while (slen-- != 0 && dlen-- != 0) {
    444 		if (nit && *src == '\0')
    445 			break;
    446 		else if (*src <= 0x20 || *src >= 0x7f) {
    447 			if (hc)
    448 				dst[i++] = ' ';
    449 		} else {
    450 			hc = 1;
    451 			dst[i++] = *src;
    452 			lc = i;
    453 		}
    454 		src++;
    455 	}
    456 
    457 	dst[lc] = '\0';
    458 }
    459 
    460 int
    461 gettid(char **argv)
    462 {
    463 	char *argp;
    464 	int tid;
    465 
    466 	if (argv[1] != NULL)
    467 		usage();
    468 
    469 	tid = (int)strtol(argv[0], &argp, 0);
    470 	if (*argp != '\0')
    471 		usage();
    472 
    473 	return (tid);
    474 }
    475