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