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
34 * $FreeBSD: src/usr.sbin/fwcontrol/fwcontrol.c,v 1.1.2.8 2003/05/01 06:26:35 simokawa Exp $
35 * $DragonFly: src/usr.sbin/fwcontrol/fwcontrol.c,v 1.3 2003/08/08 04:18:44 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <sys/errno.h>
43 #include <bus/firewire/firewire.h>
44 #include <bus/firewire/iec13213.h>
46 #include <netinet/in.h>
54 extern int dvrecv(int, char *, char, int);
55 extern int dvsend(int, char *, char, int);
61 "fwcontrol [-g gap_count] [-o node] [-b pri_req] [-c node]"
62 " [-r] [-t] [-d node] [-l file] [-R file] [-S file]\n"
63 "\t-g: broadcast gap_count by phy_config packet\n"
64 "\t-o: send link-on packet to the node\n"
65 "\t-s: write RESET_START register on the node\n"
66 "\t-b: set PRIORITY_BUDGET register on all supported nodes\n"
67 "\t-c: read configuration ROM\n"
69 "\t-t: read topology map\n"
70 "\t-d: hex dump of configuration ROM\n"
71 "\t-l: load and parse hex dump file of configuration ROM\n"
72 "\t-R: Receive DV stream\n"
73 "\t-S: Send DV stream\n");
77 static struct fw_devlstreq *
80 struct fw_devlstreq *data;
82 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
85 if( ioctl(fd, FW_GDEVLST, data) < 0) {
94 struct fw_devlstreq *data;
95 struct fw_devinfo *devinfo;
99 printf("%d devices (info_len=%d)\n", data->n, data->info_len);
100 printf("node EUI64 status\n");
101 for (i = 0; i < data->info_len; i++) {
102 devinfo = &data->dev[i];
103 printf("%4d %08x%08x %6d\n",
104 (devinfo->status || i == 0) ? devinfo->dst : -1,
114 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data)
116 struct fw_asyreq *asyreq;
119 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
120 asyreq->req.len = 16;
122 asyreq->req.type = FWASREQNODE;
123 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
125 asyreq->req.type = FWASREQEUI;
126 asyreq->req.dst.eui = eui;
128 asyreq->pkt.mode.rreqq.tlrt = 0;
130 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
132 asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
134 asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
135 asyreq->pkt.mode.rreqq.dest_lo = addr_lo;
137 qld = (u_int32_t *)&asyreq->pkt;
139 asyreq->pkt.mode.wreqq.data = data;
141 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
153 send_phy_config(int fd, int root_node, int gap_count)
155 struct fw_asyreq *asyreq;
157 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
158 asyreq->req.len = 12;
159 asyreq->req.type = FWASREQNODE;
160 asyreq->pkt.mode.ld[0] = 0;
161 asyreq->pkt.mode.ld[1] = 0;
162 asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
164 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
166 asyreq->pkt.mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
167 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
169 printf("send phy_config root_node=%d gap_count=%d\n",
170 root_node, gap_count);
172 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
178 send_link_on(int fd, int node)
180 struct fw_asyreq *asyreq;
182 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
183 asyreq->req.len = 12;
184 asyreq->req.type = FWASREQNODE;
185 asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
186 asyreq->pkt.mode.ld[1] |= (1 << 30) | ((node & 0x3f) << 24);
187 asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
189 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
195 reset_start(int fd, int node)
197 struct fw_asyreq *asyreq;
199 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
200 asyreq->req.len = 16;
201 asyreq->req.type = FWASREQNODE;
202 asyreq->pkt.mode.wreqq.dst = FWLOCALBUS | (node & 0x3f);
203 asyreq->pkt.mode.wreqq.tlrt = 0;
204 asyreq->pkt.mode.wreqq.tcode = FWTCODE_WREQQ;
206 asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
207 asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
209 asyreq->pkt.mode.wreqq.data = htonl(0x1);
211 if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
217 set_pri_req(int fd, int pri_req)
219 struct fw_devlstreq *data;
220 struct fw_devinfo *devinfo;
221 u_int32_t max, reg, old;
225 #define BUGET_REG 0xf0000218
226 for (i = 0; i < data->info_len; i++) {
227 devinfo = &data->dev[i];
228 if (!devinfo->status)
230 reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
231 printf("%d %08x:%08x, %08x",
232 devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg);
233 if (reg > 0 && pri_req >= 0) {
235 max = (reg & 0x3f00) >> 8;
238 printf(" 0x%x -> 0x%x\n", old, pri_req);
239 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
248 parse_bus_info_block(u_int32_t *p, int info_len)
252 for (i = 0; i < info_len; i++) {
253 printf("bus_info%d: 0x%08x\n", i, *p++);
258 get_crom(int fd, int node, void *crom_buf, int len)
260 struct fw_crom_buf buf;
262 struct fw_devlstreq *data;
266 for (i = 0; i < data->info_len; i++) {
267 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
270 if (i == data->info_len)
271 errx(1, "no such node %d.", node);
273 errx(1, "node %d is myself.", node);
275 buf.eui = data->dev[i].eui;
280 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
288 show_crom(u_int32_t *crom_buf)
291 struct crom_context cc;
292 char *desc, info[256];
293 static char *key_types = "ICLD";
295 struct csrdirectory *dir;
299 printf("first quad: 0x%08x ", *crom_buf);
300 hdr = (struct csrhdr *)crom_buf;
301 if (hdr->info_len == 1) {
304 reg = (struct csrreg *)hdr;
305 printf("verndor ID: 0x%06x\n", reg->val);
308 printf("info_len=%d crc_len=%d crc=0x%04x",
309 hdr->info_len, hdr->crc_len, hdr->crc);
310 crc = crom_crc(crom_buf+1, hdr->crc_len);
315 parse_bus_info_block(crom_buf+1, hdr->info_len);
317 crom_init_context(&cc, crom_buf);
318 dir = cc.stack[0].dir;
319 printf("root_directory: len=0x%04x(%d) crc=0x%04x",
320 dir->crc_len, dir->crc_len, dir->crc);
321 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
326 if (dir->crc_len < 1)
328 while (cc.depth >= 0) {
329 desc = crom_desc(&cc, info, sizeof(info));
331 for (i = 0; i < cc.depth; i++)
333 printf("%02x(%c:%02x) %06x %s: %s\n",
335 key_types[(reg->key & CSRTYPE_MASK)>>6],
336 reg->key & CSRKEY_MASK, reg->val,
342 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
345 dump_crom(u_int32_t *p)
349 for (i = 0; i < len/(4*8); i ++) {
351 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
357 load_crom(char *filename, u_int32_t *p)
362 if ((file = fopen(filename, "r")) == NULL)
364 for (i = 0; i < len/(4*8); i ++) {
365 fscanf(file, DUMP_FORMAT,
366 p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
372 show_topology_map(int fd)
374 struct fw_topology_map *tmap;
375 union fw_self_id sid;
377 static char *port_status[] = {" ", "-", "P", "C"};
378 static char *pwr_class[] = {" 0W", "15W", "30W", "45W",
379 "-1W", "-2W", "-5W", "-9W"};
380 static char *speed[] = {"S100", "S200", "S400", "S800"};
381 tmap = malloc(sizeof(struct fw_topology_map));
384 if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
387 printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
388 tmap->crc_len, tmap->generation,
389 tmap->node_count, tmap->self_id_count);
390 printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
392 for (i = 0; i < tmap->crc_len - 2; i++) {
393 sid = tmap->self_id[i];
395 printf("%02d sequel packet\n", sid.p0.phy_id);
398 printf("%02d %2d %2d %4s %d %d %3s"
403 speed[sid.p0.phy_speed],
406 pwr_class[sid.p0.power_class],
407 port_status[sid.p0.port0],
408 port_status[sid.p0.port1],
409 port_status[sid.p0.port2],
410 sid.p0.initiated_reset,
418 main(int argc, char **argv)
421 u_int32_t crom_buf[1024/4];
422 int fd, i, tmp, ch, len=1024;
424 for (i = 0; i < 4; i++) {
425 snprintf(devname, sizeof(devname), "/dev/fw%d", i);
426 if ((fd = open(devname, O_RDWR)) >= 0)
436 while ((ch = getopt(argc, argv, "g:o:s:b:rtc:d:l:R:S:")) != -1)
439 tmp = strtol(optarg, NULL, 0);
440 send_phy_config(fd, -1, tmp);
443 tmp = strtol(optarg, NULL, 0);
444 send_link_on(fd, tmp);
447 tmp = strtol(optarg, NULL, 0);
448 reset_start(fd, tmp);
451 tmp = strtol(optarg, NULL, 0);
452 set_pri_req(fd, tmp);
455 if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
459 show_topology_map(fd);
462 tmp = strtol(optarg, NULL, 0);
463 get_crom(fd, tmp, crom_buf, len);
467 tmp = strtol(optarg, NULL, 0);
468 get_crom(fd, tmp, crom_buf, len);
472 load_crom(optarg, crom_buf);
478 dvrecv(fd, optarg, TAG | CHANNEL, -1);
481 dvsend(fd, optarg, TAG | CHANNEL, -1);