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