1 /* $NetBSD: iopctl.c,v 1.15 2007/12/15 16:03:30 perry 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.15 2007/12/15 16:03:30 perry 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 59 #include <dev/i2o/i2o.h> 60 #include <dev/i2o/iopio.h> 61 62 const char *class2str(int); 63 void getparam(int, int, void *, int); 64 int gettid(char **); 65 int main(int, char **); 66 int show(const char *, const char *, ...); 67 void i2ostrvis(const u_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 } __packed 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->msgflags & I2O_MSGFLAGS_FAIL) != 0) 240 errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (FAIL)"); 241 if (rf->reqstatus != 0) 242 errx(EXIT_FAILURE, "I2O_UTIL_PARAMS_GET failed (%d)", 243 ((struct i2o_reply *)buf)->reqstatus); 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 show("flags", "0x%x", le16toh(lct->flags)); 269 show("iop flags", "0x%x", le32toh(lct->iopflags)); 270 show("lct change indicator", "%d", le32toh(lct->changeindicator)); 271 printf("\n"); 272 273 for (i = 0; i < nent; i++, ent++) { 274 classid = le32toh(ent->classid); 275 usertid = le32toh(ent->usertid); 276 277 show("lct entry", "%d", i); 278 show("entry size", "%d bytes", le16toh(ent->entrysize) << 2); 279 show("local tid", "%d", le16toh(ent->localtid) & 4095); 280 show("change indicator", "%d", le32toh(ent->changeindicator)); 281 show("flags", "%x", le32toh(ent->deviceflags)); 282 show("class id", "%x (%s)", classid & 4095, 283 class2str(classid & 4095)); 284 show("version", "%x", (classid >> 12) & 15); 285 show("organisation id", "%x", classid >> 16); 286 show("subclass info", "%x", le32toh(ent->subclassinfo)); 287 show("user tid", "%d", usertid & 4095); 288 show("parent tid", "%d", (usertid >> 12) & 4095); 289 show("bios info", "%d", (usertid >> 24) & 255); 290 i2ostrvis(ent->identitytag, sizeof(ent->identitytag), ident, 291 sizeof(ident)); 292 show("identity tag", "<%s>", ident); 293 show("event caps", "%x", le32toh(ent->eventcaps)); 294 295 if (i != nent - 1) 296 printf("\n"); 297 } 298 } 299 300 void 301 showstatus(char **argv) 302 { 303 char ident[sizeof(status.productid) + 1]; 304 u_int32_t segnumber; 305 306 i2ostrvis(status.productid, sizeof(status.productid), 307 ident, sizeof(ident)); 308 309 segnumber = le32toh(status.segnumber); 310 show("organization id", "%d", le16toh(status.orgid)); 311 show("iop id", "%d", le32toh(status.iopid) & 4095); 312 show("host unit id", "%d", (le32toh(status.iopid) >> 16)); 313 show("segment number", "%d", segnumber & 4095); 314 show("i2o version", "%d", (segnumber >> 12) & 15); 315 show("iop state", "%d", (segnumber >> 16) & 255); 316 show("messenger type", "%d", segnumber >> 24); 317 show("inbound frame sz", "%d", le32toh(status.inboundmframesize)); 318 show("init code", "%d", status.initcode); 319 show("max inbound queue depth", "%d", 320 le32toh(status.maxinboundmframes)); 321 show("inbound queue depth", "%d", 322 le32toh(status.currentinboundmframes)); 323 show("max outbound queue depth", "%d", 324 le32toh(status.maxoutboundmframes)); 325 show("product id string", "<%s>", ident); 326 show("expected lct size", "%d", le32toh(status.expectedlctsize)); 327 show("iop capabilities", "0x%08x", le32toh(status.iopcaps)); 328 show("desired priv mem sz", "0x%08x", 329 le32toh(status.desiredprivmemsize)); 330 show("current priv mem sz", "0x%08x", 331 le32toh(status.currentprivmemsize)); 332 show("current priv mem base", "0x%08x", 333 le32toh(status.currentprivmembase)); 334 show("desired priv io sz", "0x%08x", 335 le32toh(status.desiredpriviosize)); 336 show("current priv io sz", "0x%08x", 337 le32toh(status.currentpriviosize)); 338 show("current priv io base", "0x%08x", 339 le32toh(status.currentpriviobase)); 340 } 341 342 void 343 showddmid(char **argv) 344 { 345 struct { 346 struct i2o_param_op_results pr; 347 struct i2o_param_read_results prr; 348 struct i2o_param_ddm_identity di; 349 char padding[128]; 350 } __packed p; 351 char ident[128]; 352 353 getparam(gettid(argv), I2O_PARAM_DDM_IDENTITY, &p, sizeof(p)); 354 355 show("ddm tid", "%d", le16toh(p.di.ddmtid) & 4095); 356 i2ostrvis(p.di.name, sizeof(p.di.name), ident, sizeof(ident)); 357 show("module name", "%s", ident); 358 i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident)); 359 show("module revision", "%s", ident); 360 show("serial # format", "%d", p.di.snformat); 361 show("serial #", "%08x%08x%08x", *(u_int32_t *)&p.di.serialnumber[0], 362 *(u_int32_t *)&p.di.serialnumber[4], 363 *(u_int32_t *)&p.di.serialnumber[8]); 364 } 365 366 void 367 showdevid(char **argv) 368 { 369 struct { 370 struct i2o_param_op_results pr; 371 struct i2o_param_read_results prr; 372 struct i2o_param_device_identity di; 373 char padding[128]; 374 } __packed p; 375 char ident[128]; 376 377 getparam(gettid(argv), I2O_PARAM_DEVICE_IDENTITY, &p, sizeof(p)); 378 379 show("class id", "%d (%s)", le32toh(p.di.classid) & 4095, 380 class2str(le32toh(p.di.classid) & 4095)); 381 show("owner tid", "%d", le32toh(p.di.ownertid) & 4095); 382 show("parent tid", "%d", le32toh(p.di.parenttid) & 4095); 383 384 i2ostrvis(p.di.vendorinfo, sizeof(p.di.vendorinfo), ident, 385 sizeof(ident)); 386 show("vendor", "<%s>", ident); 387 388 i2ostrvis(p.di.productinfo, sizeof(p.di.productinfo), ident, 389 sizeof(ident)); 390 show("product", "<%s>", ident); 391 392 i2ostrvis(p.di.description, sizeof(p.di.description), ident, 393 sizeof(ident)); 394 show("description", "<%s>", ident); 395 396 i2ostrvis(p.di.revlevel, sizeof(p.di.revlevel), ident, sizeof(ident)); 397 show("revision level", "<%s>", ident); 398 } 399 400 void 401 reconfig(char **argv) 402 { 403 404 if (ioctl(fd, IOPIOCRECONFIG)) 405 err(EXIT_FAILURE, "IOPIOCRECONFIG"); 406 } 407 408 void 409 showtidmap(char **argv) 410 { 411 struct iovec iov; 412 struct iop_tidmap *it; 413 int nent; 414 415 iov.iov_base = buf; 416 iov.iov_len = sizeof(buf); 417 418 if (ioctl(fd, IOPIOCGTIDMAP, &iov) < 0) 419 err(EXIT_FAILURE, "IOPIOCGTIDMAP"); 420 421 nent = iov.iov_len / sizeof(*it); 422 it = (struct iop_tidmap *)buf; 423 424 for (; nent-- != 0; it++) 425 if ((it->it_flags & IT_CONFIGURED) != 0) 426 printf("%s\ttid %d\n", it->it_dvname, it->it_tid); 427 } 428 429 void 430 i2ostrvis(const u_char *src, int slen, char *dst, int dlen) 431 { 432 int hc, lc, i, nit; 433 434 dlen--; 435 lc = 0; 436 hc = 0; 437 i = 0; 438 439 /* 440 * DPT use NUL as a space, whereas AMI use it as a terminator. The 441 * spec has nothing to say about it. Since AMI fields are usually 442 * filled with junk after the terminator, ... 443 */ 444 nit = (le16toh(status.orgid) != I2O_ORG_DPT); 445 446 while (slen-- != 0 && dlen-- != 0) { 447 if (nit && *src == '\0') 448 break; 449 else if (*src <= 0x20 || *src >= 0x7f) { 450 if (hc) 451 dst[i++] = ' '; 452 } else { 453 hc = 1; 454 dst[i++] = *src; 455 lc = i; 456 } 457 src++; 458 } 459 460 dst[lc] = '\0'; 461 } 462 463 int 464 gettid(char **argv) 465 { 466 char *argp; 467 int tid; 468 469 if (argv[1] != NULL) 470 usage(); 471 472 tid = (int)strtol(argv[0], &argp, 0); 473 if (*argp != '\0') 474 usage(); 475 476 return (tid); 477 } 478