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