3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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:
16 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
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 <sys/eui64.h>
43 #include <dev/firewire/firewire.h>
44 #include <dev/firewire/iec13213.h>
45 #include <dev/firewire/fwphyreg.h>
47 #include <netinet/in.h>
55 extern int dvrecv(int, char *, char, int);
56 extern int dvsend(int, char *, char, int);
62 "fwcontrol [-u bus_num] [-rt] [-g gap_count] [-o node] "
63 "[-b pri_req] [-c node] [-d node] [-l file] "
64 "[-R file] [-S file] [-m target]\n"
65 "\t-u: specify bus number\n"
66 "\t-g: broadcast gap_count by phy_config packet\n"
67 "\t-o: send link-on packet to the node\n"
68 "\t-s: write RESET_START register on the node\n"
69 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
70 "\t-c: read configuration ROM\n"
72 "\t-t: read topology map\n"
73 "\t-d: hex dump of configuration ROM\n"
74 "\t-l: load and parse hex dump file of configuration ROM\n"
75 "\t-R: Receive DV stream\n"
76 "\t-S: Send DV stream\n"
77 "\t-m: set fwmem target\n");
82 fweui2eui64(const struct fw_eui64 *fweui, struct eui64 *eui)
84 *(u_int32_t*)&(eui->octet[0]) = htonl(fweui->hi);
85 *(u_int32_t*)&(eui->octet[4]) = htonl(fweui->lo);
88 static struct fw_devlstreq *
91 struct fw_devlstreq *data;
93 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
96 if( ioctl(fd, FW_GDEVLST, data) < 0) {
103 str2node(int fd, const char *nodestr)
105 struct eui64 eui, tmpeui;
106 struct fw_devlstreq *data;
114 * Deal with classic node specifications.
116 node = strtol(nodestr, &endptr, 0);
121 * Try to get an eui and match it against available nodes.
123 if (eui64_hostton(nodestr, &eui) != 0 && eui64_aton(nodestr, &eui) != 0)
128 for (i = 0; i < data->info_len; i++) {
129 fweui2eui64(&data->dev[i].eui, &tmpeui);
130 if (memcmp(&eui, &tmpeui, sizeof(struct eui64)) == 0) {
131 node = data->dev[i].dst;
135 if (i >= data->info_len)
139 if (node < 0 || node > 63)
148 struct fw_devlstreq *data;
149 struct fw_devinfo *devinfo;
151 char addr[EUI64_SIZ];
155 printf("%d devices (info_len=%d)\n", data->n, data->info_len);
156 printf("node EUI64 status\n");
157 for (i = 0; i < data->info_len; i++) {
158 devinfo = &data->dev[i];
159 fweui2eui64(&devinfo->eui, &eui);
160 eui64_ntoa(&eui, addr, sizeof(addr));
161 printf("%4d %s %6d\n",
162 (devinfo->status || i == 0) ? devinfo->dst : -1,
171 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data)
173 struct fw_asyreq *asyreq;
176 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
177 asyreq->req.len = 16;
179 asyreq->req.type = FWASREQNODE;
180 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
182 asyreq->req.type = FWASREQEUI;
183 asyreq->req.dst.eui = eui;
185 asyreq->pkt.mode.rreqq.tlrt = 0;
187 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
189 asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
191 asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
192 asyreq->pkt.mode.rreqq.dest_lo = addr_lo;
194 qld = (u_int32_t *)&asyreq->pkt;
196 asyreq->pkt.mode.wreqq.data = data;
198 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
210 send_phy_config(int fd, int root_node, int gap_count)
212 struct fw_asyreq *asyreq;
214 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
215 asyreq->req.len = 12;
216 asyreq->req.type = FWASREQNODE;
217 asyreq->pkt.mode.ld[0] = 0;
218 asyreq->pkt.mode.ld[1] = 0;
219 asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
221 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
223 asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
224 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
226 printf("send phy_config root_node=%d gap_count=%d\n",
227 root_node, gap_count);
229 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
235 send_link_on(int fd, int node)
237 struct fw_asyreq *asyreq;
239 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
240 asyreq->req.len = 12;
241 asyreq->req.type = FWASREQNODE;
242 asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
243 asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
244 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
246 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
252 reset_start(int fd, int node)
254 struct fw_asyreq *asyreq;
256 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
257 asyreq->req.len = 16;
258 asyreq->req.type = FWASREQNODE;
259 asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
260 asyreq->pkt.mode.wreqq.tlrt = 0;
261 asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;
263 asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
264 asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
266 asyreq->pkt.mode.wreqq.data = htonl(0x1);
268 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
274 set_pri_req(int fd, int pri_req)
276 struct fw_devlstreq *data;
277 struct fw_devinfo *devinfo;
279 char addr[EUI64_SIZ];
280 u_int32_t max, reg, old;
284 #define BUGET_REG 0xf0000218
285 for (i = 0; i < data->info_len; i++) {
286 devinfo = &data->dev[i];
287 if (!devinfo->status)
289 reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
290 fweui2eui64(&devinfo->eui, &eui);
291 eui64_ntoa(&eui, addr, sizeof(addr));
292 printf("%d %s, %08x",
293 devinfo->dst, addr, reg);
294 if (reg > 0 && pri_req >= 0) {
296 max = (reg & 0x3f00) >> 8;
299 printf(" 0x%x -> 0x%x\n", old, pri_req);
300 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
309 parse_bus_info_block(u_int32_t *p, int info_len)
312 char addr[EUI64_SIZ];
316 bi = (struct bus_info *)p;
317 fweui2eui64(&bi->eui64, &eui);
318 eui64_ntoa(&eui, addr, sizeof(addr));
319 printf("bus_name: 0x%04x\n"
320 "irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
321 "cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
322 "generation:%d link_spd:%d\n"
325 bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc,
326 bi->cyc_clk_acc, bi->max_rec, bi->max_rom,
327 bi->generation, bi->link_spd,
332 get_crom(int fd, int node, void *crom_buf, int len)
334 struct fw_crom_buf buf;
336 struct fw_devlstreq *data;
340 for (i = 0; i < data->info_len; i++) {
341 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
344 if (i == data->info_len)
345 errx(1, "no such node %d.", node);
347 buf.eui = data->dev[i].eui;
352 bzero(crom_buf, len);
353 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
361 show_crom(u_int32_t *crom_buf)
364 struct crom_context cc;
365 char *desc, info[256];
366 static char *key_types = "ICLD";
368 struct csrdirectory *dir;
372 printf("first quad: 0x%08x ", *crom_buf);
373 if (crom_buf[0] == 0) {
374 printf("(Invalid Configuration ROM)\n");
377 hdr = (struct csrhdr *)crom_buf;
378 if (hdr->info_len == 1) {
381 reg = (struct csrreg *)hdr;
382 printf("verndor ID: 0x%06x\n", reg->val);
385 printf("info_len=%d crc_len=%d crc=0x%04x",
386 hdr->info_len, hdr->crc_len, hdr->crc);
387 crc = crom_crc(crom_buf+1, hdr->crc_len);
392 parse_bus_info_block(crom_buf+1, hdr->info_len);
394 crom_init_context(&cc, crom_buf);
395 dir = cc.stack[0].dir;
397 printf("no root directory - giving up\n");
400 printf("root_directory: len=0x%04x(%d) crc=0x%04x",
401 dir->crc_len, dir->crc_len, dir->crc);
402 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
407 if (dir->crc_len < 1)
409 while (cc.depth >= 0) {
410 desc = crom_desc(&cc, info, sizeof(info));
412 for (i = 0; i < cc.depth; i++)
414 printf("%02x(%c:%02x) %06x %s: %s\n",
416 key_types[(reg->key & CSRTYPE_MASK)>>6],
417 reg->key & CSRKEY_MASK, reg->val,
423 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
426 dump_crom(u_int32_t *p)
430 for (i = 0; i < len/(4*8); i ++) {
432 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
438 load_crom(char *filename, u_int32_t *p)
443 if ((file = fopen(filename, "r")) == NULL)
445 for (i = 0; i < len/(4*8); i ++) {
446 fscanf(file, DUMP_FORMAT,
447 p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
453 show_topology_map(int fd)
455 struct fw_topology_map *tmap;
456 union fw_self_id sid;
458 static char *port_status[] = {" ", "-", "P", "C"};
459 static char *pwr_class[] = {" 0W", "15W", "30W", "45W",
460 "-1W", "-2W", "-5W", "-9W"};
461 static char *speed[] = {"S100", "S200", "S400", "S800"};
462 tmap = malloc(sizeof(struct fw_topology_map));
465 if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
468 printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
469 tmap->crc_len, tmap->generation,
470 tmap->node_count, tmap->self_id_count);
471 printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
473 for (i = 0; i < tmap->crc_len - 2; i++) {
474 sid = tmap->self_id[i];
476 printf("%02d sequel packet\n", sid.p0.phy_id);
479 printf("%02d %2d %2d %4s %d %d %3s"
484 speed[sid.p0.phy_speed],
487 pwr_class[sid.p0.power_class],
488 port_status[sid.p0.port0],
489 port_status[sid.p0.port1],
490 port_status[sid.p0.port2],
491 sid.p0.initiated_reset,
499 read_phy_registers(int fd, u_int8_t *buf, int offset, int len)
501 struct fw_reg_req_t reg;
504 for (i = 0; i < len; i++) {
505 reg.addr = offset + i;
506 if (ioctl(fd, FWOHCI_RDPHYREG, ®) < 0)
508 buf[i] = (u_int8_t) reg.data;
509 printf("0x%02x ", reg.data);
515 read_phy_page(int fd, u_int8_t *buf, int page, int port)
517 struct fw_reg_req_t reg;
520 reg.data = ((page & 7) << 5) | (port & 0xf);
521 if (ioctl(fd, FWOHCI_WRPHYREG, ®) < 0)
523 read_phy_registers(fd, buf, 8, 8);
527 dump_phy_registers(int fd)
529 struct phyreg_base b;
530 struct phyreg_page0 p;
531 struct phyreg_page1 v;
534 printf("=== base register ===\n");
535 read_phy_registers(fd, (u_int8_t *)&b, 0, 8);
537 "Physical_ID:%d R:%d CPS:%d\n"
538 "RHB:%d IBR:%d Gap_Count:%d\n"
539 "Extended:%d Num_Ports:%d\n"
540 "PHY_Speed:%d Delay:%d\n"
541 "LCtrl:%d C:%d Jitter:%d Pwr_Class:%d\n"
542 "WDIE:%d ISBR:%d CTOI:%d CPSI:%d STOI:%d PEI:%d EAA:%d EMC:%d\n"
543 "Max_Legacy_SPD:%d BLINK:%d Bridge:%d\n"
544 "Page_Select:%d Port_Select%d\n",
545 b.phy_id, b.r, b.cps,
546 b.rhb, b.ibr, b.gap_count,
547 b.extended, b.num_ports,
548 b.phy_speed, b.delay,
549 b.lctrl, b.c, b.jitter, b.pwr_class,
550 b.wdie, b.isbr, b.ctoi, b.cpsi, b.stoi, b.pei, b.eaa, b.emc,
551 b.legacy_spd, b.blink, b.bridge,
552 b.page_select, b.port_select
555 for (i = 0; i < b.num_ports; i ++) {
556 printf("\n=== page 0 port %d ===\n", i);
557 read_phy_page(fd, (u_int8_t *)&p, 0, i);
559 "Astat:%d BStat:%d Ch:%d Con:%d RXOK:%d Dis:%d\n"
560 "Negotiated_speed:%d PIE:%d Fault:%d Stanby_fault:%d Disscrm:%d B_Only:%d\n"
561 "DC_connected:%d Max_port_speed:%d LPP:%d Cable_speed:%d\n"
562 "Connection_unreliable:%d Beta_mode:%d\n"
564 "Loop_disable:%d In_standby:%d Hard_disable:%d\n",
565 p.astat, p.bstat, p.ch, p.con, p.rxok, p.dis,
566 p.negotiated_speed, p.pie, p.fault, p.stanby_fault, p.disscrm, p.b_only,
567 p.dc_connected, p.max_port_speed, p.lpp, p.cable_speed,
568 p.connection_unreliable, p.beta_mode,
570 p.loop_disable, p.in_standby, p.hard_disable
573 printf("\n=== page 1 ===\n");
574 read_phy_page(fd, (u_int8_t *)&v, 1, 0);
578 "Product_ID:0x%06x\n",
580 (v.vendor_id[0] << 16) | (v.vendor_id[1] << 8) | v.vendor_id[2],
581 (v.product_id[0] << 16) | (v.product_id[1] << 8) | v.product_id[2]
586 open_dev(int *fd, char *devbase)
592 for (i = 0; i < 4; i++) {
593 snprintf(devname, sizeof(devname), "%s.%d", devbase, i);
594 if ((*fd = open(devname, O_RDWR)) >= 0)
604 sysctl_set_int(char *name, int val)
606 if (sysctlbyname(name, NULL, NULL, &val, sizeof(int)) < 0)
607 err(1, "sysctl %s failed.", name);
611 main(int argc, char **argv)
613 u_int32_t crom_buf[1024/4];
614 char devbase[1024] = "/dev/fw0";
615 int fd, i, tmp, ch, len=1024;
622 open_dev(&fd, devbase);
626 while ((ch = getopt(argc, argv, "g:m:o:s:b:prtc:d:l:u:R:S:")) != -1)
629 tmp = strtol(optarg, NULL, 0);
630 open_dev(&fd, devbase);
631 set_pri_req(fd, tmp);
634 open_dev(&fd, devbase);
635 tmp = str2node(fd, optarg);
636 get_crom(fd, tmp, crom_buf, len);
640 open_dev(&fd, devbase);
641 tmp = str2node(fd, optarg);
642 get_crom(fd, tmp, crom_buf, len);
646 tmp = strtol(optarg, NULL, 0);
647 open_dev(&fd, devbase);
648 send_phy_config(fd, -1, tmp);
651 load_crom(optarg, crom_buf);
655 if (eui64_hostton(optarg, &target) != 0 &&
656 eui64_aton(optarg, &target) != 0)
657 errx(1, "invalid target: %s", optarg);
658 eui.hi = ntohl(*(u_int32_t*)&(target.octet[0]));
659 eui.lo = ntohl(*(u_int32_t*)&(target.octet[4]));
660 sysctl_set_int("hw.firewire.fwmem.eui64_hi", eui.hi);
661 sysctl_set_int("hw.firewire.fwmem.eui64_lo", eui.lo);
664 open_dev(&fd, devbase);
665 tmp = str2node(fd, optarg);
666 send_link_on(fd, tmp);
669 open_dev(&fd, devbase);
670 dump_phy_registers(fd);
673 open_dev(&fd, devbase);
674 if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
678 open_dev(&fd, devbase);
679 tmp = str2node(fd, optarg);
680 reset_start(fd, tmp);
683 open_dev(&fd, devbase);
684 show_topology_map(fd);
687 tmp = strtol(optarg, NULL, 0);
688 snprintf(devbase, sizeof(devbase), "/dev/fw%d", tmp);
693 if (argc == optind) {
694 open_dev(&fd, devbase);
701 open_dev(&fd, devbase);
702 dvrecv(fd, optarg, TAG | CHANNEL, -1);
705 open_dev(&fd, devbase);
706 dvsend(fd, optarg, TAG | CHANNEL, -1);