1 /* $NetBSD: iopctl.c,v 1.22 2011/08/30 19:03:25 joerg 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 #include <sys/cdefs.h> 34 __RCSID("$NetBSD: iopctl.c,v 1.22 2011/08/30 19:03:25 joerg Exp $"); 35 #endif /* not lint */ 36 37 #include <sys/param.h> 38 #include <sys/ioctl.h> 39 #include <sys/uio.h> 40 41 #include <err.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <stdarg.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <util.h> 50 51 #include <dev/i2o/i2o.h> 52 #include <dev/i2o/iopio.h> 53 54 static const char *class2str(int); 55 static void getparam(int, int, void *, int); 56 static int gettid(char **); 57 static int show(const char *, const char *, ...) __printflike(2, 3); 58 static void i2ostrvis(const u_char *, int, char *, int); 59 static void usage(void) __dead; 60 61 static void reconfig(char **); 62 static void showdevid(char **); 63 static void showddmid(char **); 64 static void showlct(char **); 65 static void showstatus(char **); 66 static void showtidmap(char **); 67 68 static struct { 69 int class; 70 const char *caption; 71 } const i2oclass[] = { 72 { I2O_CLASS_EXECUTIVE, "executive" }, 73 { I2O_CLASS_DDM, "device driver module" }, 74 { I2O_CLASS_RANDOM_BLOCK_STORAGE, "random block storage" }, 75 { I2O_CLASS_SEQUENTIAL_STORAGE, "sequential storage" }, 76 { I2O_CLASS_LAN, "LAN port" }, 77 { I2O_CLASS_WAN, "WAN port" }, 78 { I2O_CLASS_FIBRE_CHANNEL_PORT, "fibrechannel port" }, 79 { I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL, "fibrechannel peripheral" }, 80 { I2O_CLASS_SCSI_PERIPHERAL, "SCSI peripheral" }, 81 { I2O_CLASS_ATE_PORT, "ATE port" }, 82 { I2O_CLASS_ATE_PERIPHERAL, "ATE peripheral" }, 83 { I2O_CLASS_FLOPPY_CONTROLLER, "floppy controller" }, 84 { I2O_CLASS_FLOPPY_DEVICE, "floppy device" }, 85 { I2O_CLASS_BUS_ADAPTER_PORT, "bus adapter port" }, 86 }; 87 88 static struct { 89 const char *label; 90 int takesargs; 91 void (*func)(char **); 92 } const cmdtab[] = { 93 { "reconfig", 0, reconfig }, 94 { "showddmid", 1, showddmid }, 95 { "showdevid", 1, showdevid }, 96 { "showlct", 0, showlct }, 97 { "showstatus", 0, showstatus }, 98 { "showtidmap", 0, showtidmap }, 99 }; 100 101 static int fd; 102 static char buf[32768]; 103 static struct i2o_status status; 104 105 int 106 main(int argc, char **argv) 107 { 108 int ch; 109 size_t i; 110 const char *dv; 111 struct iovec iov; 112 113 dv = "/dev/iop0"; 114 115 while ((ch = getopt(argc, argv, "f:")) != -1) { 116 switch (ch) { 117 case 'f': 118 dv = optarg; 119 break; 120 default: 121 usage(); 122 /* NOTREACHED */ 123 } 124 } 125 126 if (argv[optind] == NULL) 127 usage(); 128 129 if ((fd = open(dv, O_RDWR)) < 0) 130 err(EXIT_FAILURE, "%s", dv); 131 132 iov.iov_base = &status; 133 iov.iov_len = sizeof(status); 134 if (ioctl(fd, IOPIOCGSTATUS, &iov) < 0) 135 err(EXIT_FAILURE, "IOPIOCGSTATUS"); 136 137 for (i = 0; i < sizeof(cmdtab) / sizeof(cmdtab[0]); i++) 138 if (strcmp(argv[optind], cmdtab[i].label) == 0) { 139 if (cmdtab[i].takesargs == 0 && 140 argv[optind + 1] != NULL) 141 usage(); 142 (*cmdtab[i].func)(argv + optind + 1); 143 break; 144 } 145 146 if (i == sizeof(cmdtab) / sizeof(cmdtab[0])) 147 errx(EXIT_FAILURE, "unknown command ``%s''", argv[optind]); 148 149 close(fd); 150 exit(EXIT_SUCCESS); 151 /* NOTREACHED */ 152 } 153 154 static void 155 usage(void) 156 { 157 158 (void)fprintf(stderr, "usage: %s [-f dev] <command> [target]\n", 159 getprogname()); 160 exit(EXIT_FAILURE); 161 /* NOTREACHED */ 162 } 163 164 static int 165 show(const char *hdr, const char *fmt, ...) 166 { 167 int i; 168 va_list va; 169 170 for (i = printf("%s", hdr); i < 25; i++) 171 putchar(' '); 172 va_start(va, fmt); 173 i += vprintf(fmt, va); 174 va_end(va); 175 putchar('\n'); 176 return (i); 177 } 178 179 static const char * 180 class2str(int class) 181 { 182 size_t i; 183 184 for (i = 0; i < sizeof(i2oclass) / sizeof(i2oclass[0]); i++) 185 if (class == i2oclass[i].class) 186 return (i2oclass[i].caption); 187 188 return ("unknown"); 189 } 190 191 static void 192 getparam(int tid, int group, void *pbuf, int pbufsize) 193 { 194 struct ioppt pt; 195 struct i2o_util_params_op mb; 196 struct i2o_reply *rf; 197 struct { 198 struct i2o_param_op_list_header olh; 199 struct i2o_param_op_all_template oat; 200 } __packed req; 201 202 mb.msgflags = I2O_MSGFLAGS(i2o_util_params_op); 203 mb.msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET); 204 mb.flags = 0; 205 206 req.olh.count = htole16(1); 207 req.olh.reserved = htole16(0); 208 req.oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET); 209 req.oat.fieldcount = htole16(0xffff); 210 req.oat.group = htole16(group); 211 212 pt.pt_msg = &mb; 213 pt.pt_msglen = sizeof(mb); 214 pt.pt_reply = buf; 215 pt.pt_replylen = sizeof(buf); 216 pt.pt_timo = 10000; 217 pt.pt_nbufs = 2; 218 219 pt.pt_bufs[0].ptb_data = &req; 220 pt.pt_bufs[0].ptb_datalen = sizeof(req); 221 pt.pt_bufs[0].ptb_out = 1; 222 223 pt.pt_bufs[1].ptb_data = pbuf; 224 pt.pt_bufs[1].ptb_datalen = pbufsize; 225 pt.pt_bufs[1].ptb_out = 0; 226 227 if (ioctl(fd, IOPIOCPT, &pt) < 0) 228 err(EXIT_FAILURE, "IOPIOCPT"); 229 230 rf = (struct i2o_reply *)buf; 231 if ((rf->msgflags & I2O_MSGFLAGS_FAIL) != 0) 232 errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (FAIL)"); 233 if (rf->reqstatus != 0) 234 errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (%d)", 235 rf->reqstatus); 236 } 237 238 static void 239 showlct(char **argv) 240 { 241 struct iovec iov; 242 struct i2o_lct *lct; 243 struct i2o_lct_entry *ent; 244 u_int32_t classid, usertid; 245 int i, nent; 246 char ident[sizeof(ent->identitytag) * 4 + 1]; 247 248 iov.iov_base = buf; 249 iov.iov_len = sizeof(buf); 250 251 if (ioctl(fd, IOPIOCGLCT, &iov) < 0) 252 err(EXIT_FAILURE, "IOPIOCGLCT"); 253 254 lct = (struct i2o_lct *)buf; 255 ent = lct->entry; 256 nent = ((le16toh(lct->tablesize) << 2) - 257 sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) / 258 sizeof(struct i2o_lct_entry); 259 260 show("flags", "0x%x", le16toh(lct->flags)); 261 show("iop flags", "0x%x", le32toh(lct->iopflags)); 262 show("lct change indicator", "%d", le32toh(lct->changeindicator)); 263 printf("\n"); 264 265 for (i = 0; i < nent; i++, ent++) { 266 classid = le32toh(ent->classid); 267 usertid = le32toh(ent->usertid); 268 269 show("lct entry", "%d", i); 270 show("entry size", "%d bytes", le16toh(ent->entrysize) << 2); 271 show("local tid", "%d", le16toh(ent->localtid) & 4095); 272 show("change indicator", "%d", le32toh(ent->changeindicator)); 273 show("flags", "%x", le32toh(ent->deviceflags)); 274 show("class id", "%x (%s)", classid & 4095, 275 class2str(classid & 4095)); 276 show("version", "%x", (classid >> 12) & 15); 277 show("organisation id", "%x", classid >> 16); 278 show("subclass info", "%x", le32toh(ent->subclassinfo)); 279 show("user tid", "%d", usertid & 4095); 280 show("parent tid", "%d", (usertid >> 12) & 4095); 281 show("bios info", "%d", (usertid >> 24) & 255); 282 i2ostrvis(ent->identitytag, sizeof(ent->identitytag), ident, 283 sizeof(ident)); 284 show("identity tag", "<%s>", ident); 285 show("event caps", "%x", le32toh(ent->eventcaps)); 286 287 if (i != nent - 1) 288 printf("\n"); 289 } 290 } 291 292 static void 293 showstatus(char **argv) 294 { 295 char ident[sizeof(status.productid) + 1]; 296 u_int32_t segnumber; 297 298 i2ostrvis(status.productid, sizeof(status.productid), 299 ident, sizeof(ident)); 300 301 segnumber = le32toh(status.segnumber); 302 show("organization id", "%d", le16toh(status.orgid)); 303 show("iop id", "%d", le32toh(status.iopid) & 4095); 304 show("host unit id", "%d", (le32toh(status.iopid) >> 16)); 305 show("segment number", "%d", segnumber & 4095); 306 show("i2o version", "%d", (segnumber >> 12) & 15); 307 show("iop state", "%d", (segnumber >> 16) & 255); 308 show("messenger type", "%d", segnumber >> 24); 309 show("inbound frame sz", "%d", le16toh(status.inboundmframesize)); 310 show("init code", "%d", status.initcode); 311 show("max inbound queue depth", "%d", 312 le32toh(status.maxinboundmframes)); 313 show("inbound queue depth", "%d", 314 le32toh(status.currentinboundmframes)); 315 show("max outbound queue depth", "%d", 316 le32toh(status.maxoutboundmframes)); 317 show("product id string", "<%s>", ident); 318 show("expected lct size", "%d", le32toh(status.expectedlctsize)); 319 show("iop capabilities", "0x%08x", le32toh(status.iopcaps)); 320 show("desired priv mem sz", "0x%08x", 321 le32toh(status.desiredprivmemsize)); 322 show("current priv mem sz", "0x%08x", 323 le32toh(status.currentprivmemsize)); 324 show("current priv mem base", "0x%08x", 325 le32toh(status.currentprivmembase)); 326 show("desired priv io sz", "0x%08x", 327 le32toh(status.desiredpriviosize)); 328 show("current priv io sz", "0x%08x", 329 le32toh(status.currentpriviosize)); 330 show("current priv io base", "0x%08x", 331 le32toh(status.currentpriviobase)); 332 } 333 334 static void 335 showddmid(char **argv) 336 { 337 struct { 338 struct i2o_param_op_results pr; 339 struct i2o_param_read_results prr; 340 struct i2o_param_ddm_identity di; 341 char padding[128]; 342 } __packed p; 343 char ident[128]; 344 uint32_t serial[3]; 345 346 getparam(gettid(argv), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p)); 347 348 show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095); 349 i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident)); 350 show("module name", "%s", ident); 351 i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident)); 352 show("module revision", "%s", ident); 353 show("serial # format", "%d", p.di.snformat); 354 __CTASSERT(sizeof(serial) == sizeof(p.di.serialnumber)); 355 memcpy(serial, &p.di.serialnumber, sizeof(serial)); 356 show("serial #", "%08x%08x%08x", serial[0], serial[1], serial[2]); 357 } 358 359 static void 360 showdevid(char **argv) 361 { 362 struct { 363 struct i2o_param_op_results pr; 364 struct i2o_param_read_results prr; 365 struct i2o_param_device_identity di; 366 char padding[128]; 367 } __packed p; 368 char ident[128]; 369 370 getparam(gettid(argv), 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 static void 394 reconfig(char **argv) 395 { 396 397 if (ioctl(fd, IOPIOCRECONFIG)) 398 err(EXIT_FAILURE, "IOPIOCRECONFIG"); 399 } 400 401 static 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-- != 0; it++) 418 if ((it->it_flags & IT_CONFIGURED) != 0) 419 printf("%s\ttid %d\n", it->it_dvname, it->it_tid); 420 } 421 422 static void 423 i2ostrvis(const u_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 456 static int 457 gettid(char **argv) 458 { 459 char *argp; 460 int tid; 461 462 if (argv[1] != NULL) 463 usage(); 464 465 tid = (int)strtol(argv[0], &argp, 0); 466 if (*argp != '\0') 467 usage(); 468 469 return (tid); 470 } 471