1 /* 2 * Copyright (C) 2002 3 * Hidetoshi Shimokawa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * 16 * This product includes software developed by Hidetoshi Shimokawa. 17 * 18 * 4. Neither the name of the author nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/malloc.h> 39 #include <sys/socket.h> 40 #include <sys/ioctl.h> 41 #include <sys/errno.h> 42 #include <dev/firewire/firewire.h> 43 #include <dev/firewire/iec13213.h> 44 #include <dev/firewire/fwphyreg.h> 45 46 #include <netinet/in.h> 47 #include <fcntl.h> 48 #include <stdio.h> 49 #include <err.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 extern int dvrecv(int, char *, char, int); 55 extern int dvsend(int, char *, char, int); 56 57 static void 58 usage(void) 59 { 60 fprintf(stderr, 61 "fwcontrol [-u bus_num] [-rt] [-g gap_count] [-o node] " 62 "[-b pri_req] [-c node] [-d node] [-l file] " 63 "[-R file] [-S file]\n" 64 "\t-u: specify bus number\n" 65 "\t-g: broadcast gap_count by phy_config packet\n" 66 "\t-o: send link-on packet to the node\n" 67 "\t-s: write RESET_START register on the node\n" 68 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n" 69 "\t-c: read configuration ROM\n" 70 "\t-r: bus reset\n" 71 "\t-t: read topology map\n" 72 "\t-d: hex dump of configuration ROM\n" 73 "\t-l: load and parse hex dump file of configuration ROM\n" 74 "\t-R: Receive DV stream\n" 75 "\t-S: Send DV stream\n"); 76 exit(0); 77 } 78 79 static struct fw_devlstreq * 80 get_dev(int fd) 81 { 82 struct fw_devlstreq *data; 83 84 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq)); 85 if (data == NULL) 86 err(1, "malloc"); 87 if( ioctl(fd, FW_GDEVLST, data) < 0) { 88 err(1, "ioctl"); 89 } 90 return data; 91 } 92 93 static void 94 list_dev(int fd) 95 { 96 struct fw_devlstreq *data; 97 struct fw_devinfo *devinfo; 98 int i; 99 100 data = get_dev(fd); 101 printf("%d devices (info_len=%d)\n", data->n, data->info_len); 102 printf("node EUI64 status\n"); 103 for (i = 0; i < data->info_len; i++) { 104 devinfo = &data->dev[i]; 105 printf("%4d 0x%08x%08x %6d\n", 106 (devinfo->status || i == 0) ? devinfo->dst : -1, 107 devinfo->eui.hi, 108 devinfo->eui.lo, 109 devinfo->status 110 ); 111 } 112 free((void *)data); 113 } 114 115 static u_int32_t 116 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data) 117 { 118 struct fw_asyreq *asyreq; 119 u_int32_t *qld, res; 120 121 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); 122 asyreq->req.len = 16; 123 #if 0 124 asyreq->req.type = FWASREQNODE; 125 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node; 126 #else 127 asyreq->req.type = FWASREQEUI; 128 asyreq->req.dst.eui = eui; 129 #endif 130 asyreq->pkt.mode.rreqq.tlrt = 0; 131 if (read) 132 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ; 133 else 134 asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ; 135 136 asyreq->pkt.mode.rreqq.dest_hi = 0xffff; 137 asyreq->pkt.mode.rreqq.dest_lo = addr_lo; 138 139 qld = (u_int32_t *)&asyreq->pkt; 140 if (!read) 141 asyreq->pkt.mode.wreqq.data = data; 142 143 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) { 144 err(1, "ioctl"); 145 } 146 res = qld[3]; 147 free(asyreq); 148 if (read) 149 return ntohl(res); 150 else 151 return 0; 152 } 153 154 static void 155 send_phy_config(int fd, int root_node, int gap_count) 156 { 157 struct fw_asyreq *asyreq; 158 159 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); 160 asyreq->req.len = 12; 161 asyreq->req.type = FWASREQNODE; 162 asyreq->pkt.mode.ld[0] = 0; 163 asyreq->pkt.mode.ld[1] = 0; 164 asyreq->pkt.mode.common.tcode = FWTCODE_PHY; 165 if (root_node >= 0) 166 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 167 if (gap_count >= 0) 168 asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 169 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1]; 170 171 printf("send phy_config root_node=%d gap_count=%d\n", 172 root_node, gap_count); 173 174 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 175 err(1, "ioctl"); 176 free(asyreq); 177 } 178 179 static void 180 send_link_on(int fd, int node) 181 { 182 struct fw_asyreq *asyreq; 183 184 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); 185 asyreq->req.len = 12; 186 asyreq->req.type = FWASREQNODE; 187 asyreq->pkt.mode.common.tcode = FWTCODE_PHY; 188 asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24); 189 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1]; 190 191 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 192 err(1, "ioctl"); 193 free(asyreq); 194 } 195 196 static void 197 reset_start(int fd, int node) 198 { 199 struct fw_asyreq *asyreq; 200 201 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); 202 asyreq->req.len = 16; 203 asyreq->req.type = FWASREQNODE; 204 asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f); 205 asyreq->pkt.mode.wreqq.tlrt = 0; 206 asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ; 207 208 asyreq->pkt.mode.wreqq.dest_hi = 0xffff; 209 asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 210 211 asyreq->pkt.mode.wreqq.data = htonl(0x1); 212 213 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 214 err(1, "ioctl"); 215 free(asyreq); 216 } 217 218 static void 219 set_pri_req(int fd, int pri_req) 220 { 221 struct fw_devlstreq *data; 222 struct fw_devinfo *devinfo; 223 u_int32_t max, reg, old; 224 int i; 225 226 data = get_dev(fd); 227 #define BUGET_REG 0xf0000218 228 for (i = 0; i < data->info_len; i++) { 229 devinfo = &data->dev[i]; 230 if (!devinfo->status) 231 continue; 232 reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0); 233 printf("%d %08x:%08x, %08x", 234 devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg); 235 if (reg > 0 && pri_req >= 0) { 236 old = (reg & 0x3f); 237 max = (reg & 0x3f00) >> 8; 238 if (pri_req > max) 239 pri_req = max; 240 printf(" 0x%x -> 0x%x\n", old, pri_req); 241 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req); 242 } else { 243 printf("\n"); 244 } 245 } 246 free((void *)data); 247 } 248 249 static void 250 parse_bus_info_block(u_int32_t *p, int info_len) 251 { 252 int i; 253 struct bus_info *bi; 254 255 bi = (struct bus_info *)p; 256 printf("bus_name: 0x%04x\n" 257 "irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n" 258 "cyc_clk_acc:%d max_rec:%d max_rom:%d\n" 259 "generation:%d link_spd:%d\n" 260 "EUI64: 0x%08x 0x%08x\n", 261 bi->bus_name, 262 bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc, 263 bi->cyc_clk_acc, bi->max_rec, bi->max_rom, 264 bi->generation, bi->link_spd, 265 bi->eui64.hi, bi->eui64.lo); 266 } 267 268 static int 269 get_crom(int fd, int node, void *crom_buf, int len) 270 { 271 struct fw_crom_buf buf; 272 int i, error; 273 struct fw_devlstreq *data; 274 275 data = get_dev(fd); 276 277 for (i = 0; i < data->info_len; i++) { 278 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0) 279 break; 280 } 281 if (i == data->info_len) 282 errx(1, "no such node %d.", node); 283 else 284 buf.eui = data->dev[i].eui; 285 free((void *)data); 286 287 buf.len = len; 288 buf.ptr = crom_buf; 289 bzero(crom_buf, len); 290 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) { 291 err(1, "ioctl"); 292 } 293 294 return error; 295 } 296 297 static void 298 show_crom(u_int32_t *crom_buf) 299 { 300 int i; 301 struct crom_context cc; 302 char *desc, info[256]; 303 static char *key_types = "ICLD"; 304 struct csrreg *reg; 305 struct csrdirectory *dir; 306 struct csrhdr *hdr; 307 u_int16_t crc; 308 309 printf("first quad: 0x%08x ", *crom_buf); 310 hdr = (struct csrhdr *)crom_buf; 311 if (hdr->info_len == 1) { 312 /* minimum ROM */ 313 struct csrreg *reg; 314 reg = (struct csrreg *)hdr; 315 printf("verndor ID: 0x%06x\n", reg->val); 316 return; 317 } 318 printf("info_len=%d crc_len=%d crc=0x%04x", 319 hdr->info_len, hdr->crc_len, hdr->crc); 320 crc = crom_crc(crom_buf+1, hdr->crc_len); 321 if (crc == hdr->crc) 322 printf("(OK)\n"); 323 else 324 printf("(NG)\n"); 325 parse_bus_info_block(crom_buf+1, hdr->info_len); 326 327 crom_init_context(&cc, crom_buf); 328 dir = cc.stack[0].dir; 329 printf("root_directory: len=0x%04x(%d) crc=0x%04x", 330 dir->crc_len, dir->crc_len, dir->crc); 331 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len); 332 if (crc == dir->crc) 333 printf("(OK)\n"); 334 else 335 printf("(NG)\n"); 336 if (dir->crc_len < 1) 337 return; 338 while (cc.depth >= 0) { 339 desc = crom_desc(&cc, info, sizeof(info)); 340 reg = crom_get(&cc); 341 for (i = 0; i < cc.depth; i++) 342 printf("\t"); 343 printf("%02x(%c:%02x) %06x %s: %s\n", 344 reg->key, 345 key_types[(reg->key & CSRTYPE_MASK)>>6], 346 reg->key & CSRKEY_MASK, reg->val, 347 desc, info); 348 crom_next(&cc); 349 } 350 } 351 352 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n" 353 354 static void 355 dump_crom(u_int32_t *p) 356 { 357 int len=1024, i; 358 359 for (i = 0; i < len/(4*8); i ++) { 360 printf(DUMP_FORMAT, 361 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 362 p += 8; 363 } 364 } 365 366 static void 367 load_crom(char *filename, u_int32_t *p) 368 { 369 FILE *file; 370 int len=1024, i; 371 372 if ((file = fopen(filename, "r")) == NULL) 373 err(1, "load_crom"); 374 for (i = 0; i < len/(4*8); i ++) { 375 fscanf(file, DUMP_FORMAT, 376 p, p+1, p+2, p+3, p+4, p+5, p+6, p+7); 377 p += 8; 378 } 379 } 380 381 static void 382 show_topology_map(int fd) 383 { 384 struct fw_topology_map *tmap; 385 union fw_self_id sid; 386 int i; 387 static char *port_status[] = {" ", "-", "P", "C"}; 388 static char *pwr_class[] = {" 0W", "15W", "30W", "45W", 389 "-1W", "-2W", "-5W", "-9W"}; 390 static char *speed[] = {"S100", "S200", "S400", "S800"}; 391 tmap = malloc(sizeof(struct fw_topology_map)); 392 if (tmap == NULL) 393 return; 394 if (ioctl(fd, FW_GTPMAP, tmap) < 0) { 395 err(1, "ioctl"); 396 } 397 printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n", 398 tmap->crc_len, tmap->generation, 399 tmap->node_count, tmap->self_id_count); 400 printf("id link gap_cnt speed delay cIRM power port0 port1 port2" 401 " ini more\n"); 402 for (i = 0; i < tmap->crc_len - 2; i++) { 403 sid = tmap->self_id[i]; 404 if (sid.p0.sequel) { 405 printf("%02d sequel packet\n", sid.p0.phy_id); 406 continue; 407 } 408 printf("%02d %2d %2d %4s %d %d %3s" 409 " %s %s %s %d %d\n", 410 sid.p0.phy_id, 411 sid.p0.link_active, 412 sid.p0.gap_count, 413 speed[sid.p0.phy_speed], 414 sid.p0.phy_delay, 415 sid.p0.contender, 416 pwr_class[sid.p0.power_class], 417 port_status[sid.p0.port0], 418 port_status[sid.p0.port1], 419 port_status[sid.p0.port2], 420 sid.p0.initiated_reset, 421 sid.p0.more_packets 422 ); 423 } 424 free(tmap); 425 } 426 427 static void 428 read_phy_registers(int fd, u_int8_t *buf, int offset, int len) 429 { 430 struct fw_reg_req_t reg; 431 int i; 432 433 for (i = 0; i < len; i++) { 434 reg.addr = offset + i; 435 if (ioctl(fd, FWOHCI_RDPHYREG, ®) < 0) 436 err(1, "ioctl"); 437 buf[i] = (u_int8_t) reg.data; 438 printf("0x%02x ", reg.data); 439 } 440 printf("\n"); 441 } 442 443 static void 444 read_phy_page(int fd, u_int8_t *buf, int page, int port) 445 { 446 struct fw_reg_req_t reg; 447 448 reg.addr = 0x7; 449 reg.data = ((page & 7) << 5) | (port & 0xf); 450 if (ioctl(fd, FWOHCI_WRPHYREG, ®) < 0) 451 err(1, "ioctl"); 452 read_phy_registers(fd, buf, 8, 8); 453 } 454 455 static void 456 dump_phy_registers(int fd) 457 { 458 struct phyreg_base b; 459 struct phyreg_page0 p; 460 struct phyreg_page1 v; 461 int i; 462 463 printf("=== base register ===\n"); 464 read_phy_registers(fd, (u_int8_t *)&b, 0, 8); 465 printf( 466 "Physical_ID:%d R:%d CPS:%d\n" 467 "RHB:%d IBR:%d Gap_Count:%d\n" 468 "Extended:%d Num_Ports:%d\n" 469 "PHY_Speed:%d Delay:%d\n" 470 "LCtrl:%d C:%d Jitter:%d Pwr_Class:%d\n" 471 "WDIE:%d ISBR:%d CTOI:%d CPSI:%d STOI:%d PEI:%d EAA:%d EMC:%d\n" 472 "Max_Legacy_SPD:%d BLINK:%d Bridge:%d\n" 473 "Page_Select:%d Port_Select%d\n", 474 b.phy_id, b.r, b.cps, 475 b.rhb, b.ibr, b.gap_count, 476 b.extended, b.num_ports, 477 b.phy_speed, b.delay, 478 b.lctrl, b.c, b.jitter, b.pwr_class, 479 b.wdie, b.isbr, b.ctoi, b.cpsi, b.stoi, b.pei, b.eaa, b.emc, 480 b.legacy_spd, b.blink, b.bridge, 481 b.page_select, b.port_select 482 ); 483 484 for (i = 0; i < b.num_ports; i ++) { 485 printf("\n=== page 0 port %d ===\n", i); 486 read_phy_page(fd, (u_int8_t *)&p, 0, i); 487 printf( 488 "Astat:%d BStat:%d Ch:%d Con:%d RXOK:%d Dis:%d\n" 489 "Negotiated_speed:%d PIE:%d Fault:%d Stanby_fault:%d Disscrm:%d B_Only:%d\n" 490 "DC_connected:%d Max_port_speed:%d LPP:%d Cable_speed:%d\n" 491 "Connection_unreliable:%d Beta_mode:%d\n" 492 "Port_error:0x%x\n" 493 "Loop_disable:%d In_standby:%d Hard_disable:%d\n", 494 p.astat, p.bstat, p.ch, p.con, p.rxok, p.dis, 495 p.negotiated_speed, p.pie, p.fault, p.stanby_fault, p.disscrm, p.b_only, 496 p.dc_connected, p.max_port_speed, p.lpp, p.cable_speed, 497 p.connection_unreliable, p.beta_mode, 498 p.port_error, 499 p.loop_disable, p.in_standby, p.hard_disable 500 ); 501 } 502 printf("\n=== page 1 ===\n"); 503 read_phy_page(fd, (u_int8_t *)&v, 1, 0); 504 printf( 505 "Compliance:%d\n" 506 "Vendor_ID:0x%06x\n" 507 "Product_ID:0x%06x\n", 508 v.compliance, 509 (v.vendor_id[0] << 16) | (v.vendor_id[1] << 8) | v.vendor_id[2], 510 (v.product_id[0] << 16) | (v.product_id[1] << 8) | v.product_id[2] 511 ); 512 } 513 514 static void 515 open_dev(int *fd, char *devbase) 516 { 517 char devname[256]; 518 int i; 519 520 if (*fd < 0) { 521 for (i = 0; i < 4; i++) { 522 snprintf(devname, sizeof(devname), "%s.%d", devbase, i); 523 if ((*fd = open(devname, O_RDWR)) >= 0) 524 break; 525 } 526 if (*fd < 0) 527 err(1, "open"); 528 529 } 530 } 531 532 int 533 main(int argc, char **argv) 534 { 535 u_int32_t crom_buf[1024/4]; 536 char devbase[1024] = "/dev/fw0"; 537 int fd, i, tmp, ch, len=1024; 538 539 fd = -1; 540 541 if (argc < 2) { 542 open_dev(&fd, devbase); 543 list_dev(fd); 544 } 545 546 while ((ch = getopt(argc, argv, "g:o:s:b:prtc:d:l:u:R:S:")) != -1) 547 switch(ch) { 548 case 'b': 549 tmp = strtol(optarg, NULL, 0); 550 open_dev(&fd, devbase); 551 set_pri_req(fd, tmp); 552 break; 553 case 'c': 554 tmp = strtol(optarg, NULL, 0); 555 open_dev(&fd, devbase); 556 get_crom(fd, tmp, crom_buf, len); 557 show_crom(crom_buf); 558 break; 559 case 'd': 560 tmp = strtol(optarg, NULL, 0); 561 open_dev(&fd, devbase); 562 get_crom(fd, tmp, crom_buf, len); 563 dump_crom(crom_buf); 564 break; 565 case 'g': 566 tmp = strtol(optarg, NULL, 0); 567 open_dev(&fd, devbase); 568 send_phy_config(fd, -1, tmp); 569 break; 570 case 'l': 571 load_crom(optarg, crom_buf); 572 show_crom(crom_buf); 573 break; 574 case 'o': 575 tmp = strtol(optarg, NULL, 0); 576 open_dev(&fd, devbase); 577 send_link_on(fd, tmp); 578 break; 579 case 'p': 580 open_dev(&fd, devbase); 581 dump_phy_registers(fd); 582 break; 583 case 'r': 584 open_dev(&fd, devbase); 585 if(ioctl(fd, FW_IBUSRST, &tmp) < 0) 586 err(1, "ioctl"); 587 break; 588 case 's': 589 tmp = strtol(optarg, NULL, 0); 590 open_dev(&fd, devbase); 591 reset_start(fd, tmp); 592 break; 593 case 't': 594 open_dev(&fd, devbase); 595 show_topology_map(fd); 596 break; 597 case 'u': 598 tmp = strtol(optarg, NULL, 0); 599 snprintf(devbase, sizeof(devbase), "/dev/fw%d", tmp); 600 if (fd > 0) { 601 close(fd); 602 fd = -1; 603 } 604 if (argc == optind) { 605 open_dev(&fd, devbase); 606 list_dev(fd); 607 } 608 break; 609 #define TAG (1<<6) 610 #define CHANNEL 63 611 case 'R': 612 open_dev(&fd, devbase); 613 dvrecv(fd, optarg, TAG | CHANNEL, -1); 614 break; 615 case 'S': 616 open_dev(&fd, devbase); 617 dvsend(fd, optarg, TAG | CHANNEL, -1); 618 break; 619 default: 620 usage(); 621 } 622 return 0; 623 } 624