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