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