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