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