1 /* $NetBSD: iopctl.c,v 1.18 2008/09/17 16:03:28 mhitch 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.18 2008/09/17 16:03:28 mhitch 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 const char *class2str(int); 55 void getparam(int, int, void *, int); 56 int gettid(char **); 57 int main(int, char **); 58 int show(const char *, const char *, ...); 59 void i2ostrvis(const u_char *, int, char *, int); 60 void usage(void); 61 62 void reconfig(char **); 63 void showdevid(char **); 64 void showddmid(char **); 65 void showlct(char **); 66 void showstatus(char **); 67 void showtidmap(char **); 68 69 struct { 70 int class; 71 const char *caption; 72 } const i2oclass[] = { 73 { I2O_CLASS_EXECUTIVE, "executive" }, 74 { I2O_CLASS_DDM, "device driver module" }, 75 { I2O_CLASS_RANDOM_BLOCK_STORAGE, "random block storage" }, 76 { I2O_CLASS_SEQUENTIAL_STORAGE, "sequential storage" }, 77 { I2O_CLASS_LAN, "LAN port" }, 78 { I2O_CLASS_WAN, "WAN port" }, 79 { I2O_CLASS_FIBRE_CHANNEL_PORT, "fibrechannel port" }, 80 { I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL, "fibrechannel peripheral" }, 81 { I2O_CLASS_SCSI_PERIPHERAL, "SCSI peripheral" }, 82 { I2O_CLASS_ATE_PORT, "ATE port" }, 83 { I2O_CLASS_ATE_PERIPHERAL, "ATE peripheral" }, 84 { I2O_CLASS_FLOPPY_CONTROLLER, "floppy controller" }, 85 { I2O_CLASS_FLOPPY_DEVICE, "floppy device" }, 86 { I2O_CLASS_BUS_ADAPTER_PORT, "bus adapter port" }, 87 }; 88 89 struct { 90 const char *label; 91 int takesargs; 92 void (*func)(char **); 93 } const cmdtab[] = { 94 { "reconfig", 0, reconfig }, 95 { "showddmid", 1, showddmid }, 96 { "showdevid", 1, showdevid }, 97 { "showlct", 0, showlct }, 98 { "showstatus", 0, showstatus }, 99 { "showtidmap", 0, showtidmap }, 100 }; 101 102 int fd; 103 char buf[32768]; 104 struct i2o_status status; 105 106 int 107 main(int argc, char **argv) 108 { 109 int ch, 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 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 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 const char * 180 class2str(int class) 181 { 182 int 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 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 ((struct i2o_reply *)buf)->reqstatus); 236 } 237 238 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 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 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 345 getparam(gettid(argv), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p)); 346 347 show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095); 348 i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident)); 349 show("module name", "%s", ident); 350 i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident)); 351 show("module revision", "%s", ident); 352 show("serial # format", "%d", p.di.snformat); 353 show("serial #", "%08x%08x%08x", *(u_int32_t *)&p.di.serialnumber[0], 354 *(u_int32_t *)&p.di.serialnumber[4], 355 *(u_int32_t *)&p.di.serialnumber[8]); 356 } 357 358 void 359 showdevid(char **argv) 360 { 361 struct { 362 struct i2o_param_op_results pr; 363 struct i2o_param_read_results prr; 364 struct i2o_param_device_identity di; 365 char padding[128]; 366 } __packed p; 367 char ident[128]; 368 369 getparam(gettid(argv), I2O_PARAM_DEVICE_IDENTITY, &p, sizeof(p)); 370 371 show("class id", "%d (%s)", le32toh(p.di.classid) & 4095, 372 class2str(le32toh(p.di.classid) & 4095)); 373 show("owner tid", "%d", le32toh(p.di.ownertid) & 4095); 374 show("parent tid", "%d", le32toh(p.di.parenttid) & 4095); 375 376 i2ostrvis(p.di.vendorinfo, sizeof(p.di.vendorinfo), ident, 377 sizeof(ident)); 378 show("vendor", "<%s>", ident); 379 380 i2ostrvis(p.di.productinfo, sizeof(p.di.productinfo), ident, 381 sizeof(ident)); 382 show("product", "<%s>", ident); 383 384 i2ostrvis(p.di.description, sizeof(p.di.description), ident, 385 sizeof(ident)); 386 show("description", "<%s>", ident); 387 388 i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident)); 389 show("revision level", "<%s>", ident); 390 } 391 392 void 393 reconfig(char **argv) 394 { 395 396 if (ioctl(fd, IOPIOCRECONFIG)) 397 err(EXIT_FAILURE, "IOPIOCRECONFIG"); 398 } 399 400 void 401 showtidmap(char **argv) 402 { 403 struct iovec iov; 404 struct iop_tidmap *it; 405 int nent; 406 407 iov.iov_base = buf; 408 iov.iov_len = sizeof(buf); 409 410 if (ioctl(fd, IOPIOCGTIDMAP, &iov) < 0) 411 err(EXIT_FAILURE, "IOPIOCGTIDMAP"); 412 413 nent = iov.iov_len / sizeof(*it); 414 it = (struct iop_tidmap *)buf; 415 416 for (; nent-- != 0; it++) 417 if ((it->it_flags & IT_CONFIGURED) != 0) 418 printf("%s\ttid %d\n", it->it_dvname, it->it_tid); 419 } 420 421 void 422 i2ostrvis(const u_char *src, int slen, char *dst, int dlen) 423 { 424 int hc, lc, i, nit; 425 426 dlen--; 427 lc = 0; 428 hc = 0; 429 i = 0; 430 431 /* 432 * DPT use NUL as a space, whereas AMI use it as a terminator. The 433 * spec has nothing to say about it. Since AMI fields are usually 434 * filled with junk after the terminator, ... 435 */ 436 nit = (le16toh(status.orgid) != I2O_ORG_DPT); 437 438 while (slen-- != 0 && dlen-- != 0) { 439 if (nit && *src == '\0') 440 break; 441 else if (*src <= 0x20 || *src >= 0x7f) { 442 if (hc) 443 dst[i++] = ' '; 444 } else { 445 hc = 1; 446 dst[i++] = *src; 447 lc = i; 448 } 449 src++; 450 } 451 452 dst[lc] = '\0'; 453 } 454 455 int 456 gettid(char **argv) 457 { 458 char *argp; 459 int tid; 460 461 if (argv[1] != NULL) 462 usage(); 463 464 tid = (int)strtol(argv[0], &argp, 0); 465 if (*argp != '\0') 466 usage(); 467 468 return (tid); 469 } 470