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 45 #include <netinet/in.h> 46 #include <fcntl.h> 47 #include <stdio.h> 48 #include <err.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 extern int dvrecv(int, char *, char, int); 54 extern int dvsend(int, char *, char, int); 55 56 static void 57 usage(void) 58 { 59 fprintf(stderr, 60 "fwcontrol [-u bus_num] [-rt] [-g gap_count] [-o node] " 61 "[-b pri_req] [-c node] [-d node] [-l file] " 62 "[-R file] [-S file]\n" 63 "\t-u: specify bus number\n" 64 "\t-g: broadcast gap_count by phy_config packet\n" 65 "\t-o: send link-on packet to the node\n" 66 "\t-s: write RESET_START register on the node\n" 67 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n" 68 "\t-c: read configuration ROM\n" 69 "\t-r: bus reset\n" 70 "\t-t: read topology map\n" 71 "\t-d: hex dump of configuration ROM\n" 72 "\t-l: load and parse hex dump file of configuration ROM\n" 73 "\t-R: Receive DV stream\n" 74 "\t-S: Send DV stream\n"); 75 exit(0); 76 } 77 78 static struct fw_devlstreq * 79 get_dev(int fd) 80 { 81 struct fw_devlstreq *data; 82 83 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq)); 84 if (data == NULL) 85 err(1, "malloc"); 86 if( ioctl(fd, FW_GDEVLST, data) < 0) { 87 err(1, "ioctl"); 88 } 89 return data; 90 } 91 92 static void 93 list_dev(int fd) 94 { 95 struct fw_devlstreq *data; 96 struct fw_devinfo *devinfo; 97 int i; 98 99 data = get_dev(fd); 100 printf("%d devices (info_len=%d)\n", data->n, data->info_len); 101 printf("node EUI64 status\n"); 102 for (i = 0; i < data->info_len; i++) { 103 devinfo = &data->dev[i]; 104 printf("%4d %08x%08x %6d\n", 105 (devinfo->status || i == 0) ? devinfo->dst : -1, 106 devinfo->eui.hi, 107 devinfo->eui.lo, 108 devinfo->status 109 ); 110 } 111 free((void *)data); 112 } 113 114 static u_int32_t 115 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data) 116 { 117 struct fw_asyreq *asyreq; 118 u_int32_t *qld, res; 119 120 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); 121 asyreq->req.len = 16; 122 #if 0 123 asyreq->req.type = FWASREQNODE; 124 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node; 125 #else 126 asyreq->req.type = FWASREQEUI; 127 asyreq->req.dst.eui = eui; 128 #endif 129 asyreq->pkt.mode.rreqq.tlrt = 0; 130 if (read) 131 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ; 132 else 133 asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ; 134 135 asyreq->pkt.mode.rreqq.dest_hi = 0xffff; 136 asyreq->pkt.mode.rreqq.dest_lo = addr_lo; 137 138 qld = (u_int32_t *)&asyreq->pkt; 139 if (!read) 140 asyreq->pkt.mode.wreqq.data = data; 141 142 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) { 143 err(1, "ioctl"); 144 } 145 res = qld[3]; 146 free(asyreq); 147 if (read) 148 return ntohl(res); 149 else 150 return 0; 151 } 152 153 static void 154 send_phy_config(int fd, int root_node, int gap_count) 155 { 156 struct fw_asyreq *asyreq; 157 158 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); 159 asyreq->req.len = 12; 160 asyreq->req.type = FWASREQNODE; 161 asyreq->pkt.mode.ld[0] = 0; 162 asyreq->pkt.mode.ld[1] = 0; 163 asyreq->pkt.mode.common.tcode = FWTCODE_PHY; 164 if (root_node >= 0) 165 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 166 if (gap_count >= 0) 167 asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 168 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1]; 169 170 printf("send phy_config root_node=%d gap_count=%d\n", 171 root_node, gap_count); 172 173 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 174 err(1, "ioctl"); 175 free(asyreq); 176 } 177 178 static void 179 send_link_on(int fd, int node) 180 { 181 struct fw_asyreq *asyreq; 182 183 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12); 184 asyreq->req.len = 12; 185 asyreq->req.type = FWASREQNODE; 186 asyreq->pkt.mode.common.tcode = FWTCODE_PHY; 187 asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24); 188 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1]; 189 190 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 191 err(1, "ioctl"); 192 free(asyreq); 193 } 194 195 static void 196 reset_start(int fd, int node) 197 { 198 struct fw_asyreq *asyreq; 199 200 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16); 201 asyreq->req.len = 16; 202 asyreq->req.type = FWASREQNODE; 203 asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f); 204 asyreq->pkt.mode.wreqq.tlrt = 0; 205 asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ; 206 207 asyreq->pkt.mode.wreqq.dest_hi = 0xffff; 208 asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 209 210 asyreq->pkt.mode.wreqq.data = htonl(0x1); 211 212 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) 213 err(1, "ioctl"); 214 free(asyreq); 215 } 216 217 static void 218 set_pri_req(int fd, int pri_req) 219 { 220 struct fw_devlstreq *data; 221 struct fw_devinfo *devinfo; 222 u_int32_t max, reg, old; 223 int i; 224 225 data = get_dev(fd); 226 #define BUGET_REG 0xf0000218 227 for (i = 0; i < data->info_len; i++) { 228 devinfo = &data->dev[i]; 229 if (!devinfo->status) 230 continue; 231 reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0); 232 printf("%d %08x:%08x, %08x", 233 devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg); 234 if (reg > 0 && pri_req >= 0) { 235 old = (reg & 0x3f); 236 max = (reg & 0x3f00) >> 8; 237 if (pri_req > max) 238 pri_req = max; 239 printf(" 0x%x -> 0x%x\n", old, pri_req); 240 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req); 241 } else { 242 printf("\n"); 243 } 244 } 245 free((void *)data); 246 } 247 248 static void 249 parse_bus_info_block(u_int32_t *p, int info_len) 250 { 251 int i; 252 struct bus_info *bi; 253 254 bi = (struct bus_info *)p; 255 printf("bus_name: 0x%04x\n" 256 "irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n" 257 "cyc_clk_acc:%d max_rec:%d max_rom:%d\n" 258 "generation:%d link_spd:%d\n" 259 "EUI64: 0x%08x 0x%08x\n", 260 bi->bus_name, 261 bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc, 262 bi->cyc_clk_acc, bi->max_rec, bi->max_rom, 263 bi->generation, bi->link_spd, 264 bi->eui64.hi, bi->eui64.lo); 265 } 266 267 static int 268 get_crom(int fd, int node, void *crom_buf, int len) 269 { 270 struct fw_crom_buf buf; 271 int i, error; 272 struct fw_devlstreq *data; 273 274 data = get_dev(fd); 275 276 for (i = 0; i < data->info_len; i++) { 277 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0) 278 break; 279 } 280 if (i == data->info_len) 281 errx(1, "no such node %d.", node); 282 else 283 buf.eui = data->dev[i].eui; 284 free((void *)data); 285 286 buf.len = len; 287 buf.ptr = crom_buf; 288 bzero(crom_buf, len); 289 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) { 290 err(1, "ioctl"); 291 } 292 293 return error; 294 } 295 296 static void 297 show_crom(u_int32_t *crom_buf) 298 { 299 int i; 300 struct crom_context cc; 301 char *desc, info[256]; 302 static char *key_types = "ICLD"; 303 struct csrreg *reg; 304 struct csrdirectory *dir; 305 struct csrhdr *hdr; 306 u_int16_t crc; 307 308 printf("first quad: 0x%08x ", *crom_buf); 309 hdr = (struct csrhdr *)crom_buf; 310 if (hdr->info_len == 1) { 311 /* minimum ROM */ 312 struct csrreg *reg; 313 reg = (struct csrreg *)hdr; 314 printf("verndor ID: 0x%06x\n", reg->val); 315 return; 316 } 317 printf("info_len=%d crc_len=%d crc=0x%04x", 318 hdr->info_len, hdr->crc_len, hdr->crc); 319 crc = crom_crc(crom_buf+1, hdr->crc_len); 320 if (crc == hdr->crc) 321 printf("(OK)\n"); 322 else 323 printf("(NG)\n"); 324 parse_bus_info_block(crom_buf+1, hdr->info_len); 325 326 crom_init_context(&cc, crom_buf); 327 dir = cc.stack[0].dir; 328 printf("root_directory: len=0x%04x(%d) crc=0x%04x", 329 dir->crc_len, dir->crc_len, dir->crc); 330 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len); 331 if (crc == dir->crc) 332 printf("(OK)\n"); 333 else 334 printf("(NG)\n"); 335 if (dir->crc_len < 1) 336 return; 337 while (cc.depth >= 0) { 338 desc = crom_desc(&cc, info, sizeof(info)); 339 reg = crom_get(&cc); 340 for (i = 0; i < cc.depth; i++) 341 printf("\t"); 342 printf("%02x(%c:%02x) %06x %s: %s\n", 343 reg->key, 344 key_types[(reg->key & CSRTYPE_MASK)>>6], 345 reg->key & CSRKEY_MASK, reg->val, 346 desc, info); 347 crom_next(&cc); 348 } 349 } 350 351 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n" 352 353 static void 354 dump_crom(u_int32_t *p) 355 { 356 int len=1024, i; 357 358 for (i = 0; i < len/(4*8); i ++) { 359 printf(DUMP_FORMAT, 360 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 361 p += 8; 362 } 363 } 364 365 static void 366 load_crom(char *filename, u_int32_t *p) 367 { 368 FILE *file; 369 int len=1024, i; 370 371 if ((file = fopen(filename, "r")) == NULL) 372 err(1, "load_crom"); 373 for (i = 0; i < len/(4*8); i ++) { 374 fscanf(file, DUMP_FORMAT, 375 p, p+1, p+2, p+3, p+4, p+5, p+6, p+7); 376 p += 8; 377 } 378 } 379 380 static void 381 show_topology_map(int fd) 382 { 383 struct fw_topology_map *tmap; 384 union fw_self_id sid; 385 int i; 386 static char *port_status[] = {" ", "-", "P", "C"}; 387 static char *pwr_class[] = {" 0W", "15W", "30W", "45W", 388 "-1W", "-2W", "-5W", "-9W"}; 389 static char *speed[] = {"S100", "S200", "S400", "S800"}; 390 tmap = malloc(sizeof(struct fw_topology_map)); 391 if (tmap == NULL) 392 return; 393 if (ioctl(fd, FW_GTPMAP, tmap) < 0) { 394 err(1, "ioctl"); 395 } 396 printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n", 397 tmap->crc_len, tmap->generation, 398 tmap->node_count, tmap->self_id_count); 399 printf("id link gap_cnt speed delay cIRM power port0 port1 port2" 400 " ini more\n"); 401 for (i = 0; i < tmap->crc_len - 2; i++) { 402 sid = tmap->self_id[i]; 403 if (sid.p0.sequel) { 404 printf("%02d sequel packet\n", sid.p0.phy_id); 405 continue; 406 } 407 printf("%02d %2d %2d %4s %d %d %3s" 408 " %s %s %s %d %d\n", 409 sid.p0.phy_id, 410 sid.p0.link_active, 411 sid.p0.gap_count, 412 speed[sid.p0.phy_speed], 413 sid.p0.phy_delay, 414 sid.p0.contender, 415 pwr_class[sid.p0.power_class], 416 port_status[sid.p0.port0], 417 port_status[sid.p0.port1], 418 port_status[sid.p0.port2], 419 sid.p0.initiated_reset, 420 sid.p0.more_packets 421 ); 422 } 423 free(tmap); 424 } 425 426 static void 427 open_dev(int *fd, char *devbase) 428 { 429 char devname[256]; 430 int i; 431 432 if (*fd < 0) { 433 for (i = 0; i < 4; i++) { 434 snprintf(devname, sizeof(devname), "%s.%d", devbase, i); 435 if ((*fd = open(devname, O_RDWR)) >= 0) 436 break; 437 } 438 if (*fd < 0) 439 err(1, "open"); 440 441 } 442 } 443 444 int 445 main(int argc, char **argv) 446 { 447 u_int32_t crom_buf[1024/4]; 448 char devbase[1024] = "/dev/fw0"; 449 int fd, i, tmp, ch, len=1024; 450 451 fd = -1; 452 453 if (argc < 2) { 454 open_dev(&fd, devbase); 455 list_dev(fd); 456 } 457 458 while ((ch = getopt(argc, argv, "g:o:s:b:rtc:d:l:u:R:S:")) != -1) 459 switch(ch) { 460 case 'b': 461 tmp = strtol(optarg, NULL, 0); 462 open_dev(&fd, devbase); 463 set_pri_req(fd, tmp); 464 break; 465 case 'c': 466 tmp = strtol(optarg, NULL, 0); 467 open_dev(&fd, devbase); 468 get_crom(fd, tmp, crom_buf, len); 469 show_crom(crom_buf); 470 break; 471 case 'd': 472 tmp = strtol(optarg, NULL, 0); 473 open_dev(&fd, devbase); 474 get_crom(fd, tmp, crom_buf, len); 475 dump_crom(crom_buf); 476 break; 477 case 'g': 478 tmp = strtol(optarg, NULL, 0); 479 open_dev(&fd, devbase); 480 send_phy_config(fd, -1, tmp); 481 break; 482 case 'l': 483 load_crom(optarg, crom_buf); 484 show_crom(crom_buf); 485 break; 486 case 'o': 487 tmp = strtol(optarg, NULL, 0); 488 open_dev(&fd, devbase); 489 send_link_on(fd, tmp); 490 break; 491 case 'r': 492 open_dev(&fd, devbase); 493 if(ioctl(fd, FW_IBUSRST, &tmp) < 0) 494 err(1, "ioctl"); 495 break; 496 case 's': 497 tmp = strtol(optarg, NULL, 0); 498 open_dev(&fd, devbase); 499 reset_start(fd, tmp); 500 break; 501 case 't': 502 open_dev(&fd, devbase); 503 show_topology_map(fd); 504 break; 505 case 'u': 506 tmp = strtol(optarg, NULL, 0); 507 snprintf(devbase, sizeof(devbase), "/dev/fw%d", tmp); 508 if (fd > 0) { 509 close(fd); 510 fd = -1; 511 } 512 if (argc == optind) { 513 open_dev(&fd, devbase); 514 list_dev(fd); 515 } 516 break; 517 #define TAG (1<<6) 518 #define CHANNEL 63 519 case 'R': 520 open_dev(&fd, devbase); 521 dvrecv(fd, optarg, TAG | CHANNEL, -1); 522 break; 523 case 'S': 524 open_dev(&fd, devbase); 525 dvsend(fd, optarg, TAG | CHANNEL, -1); 526 break; 527 default: 528 usage(); 529 } 530 return 0; 531 } 532