Clarify that the argument to -t is in seconds.
[dragonfly/netmp.git] / usr.sbin / fwcontrol / fwcontrol.c
blob16312e2ea9961a8d1da2c3119e63b6d56a4fb4d8
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:
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
32 * SUCH DAMAGE.
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>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <err.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 extern int dvrecv(int, char *, char, int);
55 extern int dvsend(int, char *, char, int);
57 static void
58 usage(void)
60 fprintf(stderr,
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"
68 "\t-r: bus reset\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");
74 exit(0);
77 static struct fw_devlstreq *
78 get_dev(int fd)
80 struct fw_devlstreq *data;
82 data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
83 if (data == NULL)
84 err(1, "malloc");
85 if( ioctl(fd, FW_GDEVLST, data) < 0) {
86 err(1, "ioctl");
88 return data;
91 static void
92 list_dev(int fd)
94 struct fw_devlstreq *data;
95 struct fw_devinfo *devinfo;
96 int i;
98 data = get_dev(fd);
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,
105 devinfo->eui.hi,
106 devinfo->eui.lo,
107 devinfo->status
110 free((void *)data);
113 static u_int32_t
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;
117 u_int32_t *qld, res;
119 asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
120 asyreq->req.len = 16;
121 #if 0
122 asyreq->req.type = FWASREQNODE;
123 asyreq->pkt.mode.rreqq.dst = FWLOCALBUS | node;
124 #else
125 asyreq->req.type = FWASREQEUI;
126 asyreq->req.dst.eui = eui;
127 #endif
128 asyreq->pkt.mode.rreqq.tlrt = 0;
129 if (read)
130 asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
131 else
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;
138 if (!read)
139 asyreq->pkt.mode.wreqq.data = data;
141 if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
142 err(1, "ioctl");
144 res = qld[3];
145 free(asyreq);
146 if (read)
147 return ntohl(res);
148 else
149 return 0;
152 static void
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;
163 if (root_node >= 0)
164 asyreq->pkt.mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
165 if (gap_count >= 0)
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)
173 err(1, "ioctl");
174 free(asyreq);
177 static void
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)
190 err(1, "ioctl");
191 free(asyreq);
194 static void
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)
212 err(1, "ioctl");
213 free(asyreq);
216 static void
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;
222 int i;
224 data = get_dev(fd);
225 #define BUGET_REG 0xf0000218
226 for (i = 0; i < data->info_len; i++) {
227 devinfo = &data->dev[i];
228 if (!devinfo->status)
229 continue;
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) {
234 old = (reg & 0x3f);
235 max = (reg & 0x3f00) >> 8;
236 if (pri_req > max)
237 pri_req = max;
238 printf(" 0x%x -> 0x%x\n", old, pri_req);
239 read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
240 } else {
241 printf("\n");
244 free((void *)data);
247 static void
248 parse_bus_info_block(u_int32_t *p, int info_len)
250 int i;
252 for (i = 0; i < info_len; i++) {
253 printf("bus_info%d: 0x%08x\n", i, *p++);
257 static int
258 get_crom(int fd, int node, void *crom_buf, int len)
260 struct fw_crom_buf buf;
261 int i, error;
262 struct fw_devlstreq *data;
264 data = get_dev(fd);
266 for (i = 0; i < data->info_len; i++) {
267 if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
268 break;
270 if (i == data->info_len)
271 errx(1, "no such node %d.", node);
272 else if (i == 0)
273 errx(1, "node %d is myself.", node);
274 else
275 buf.eui = data->dev[i].eui;
276 free((void *)data);
278 buf.len = len;
279 buf.ptr = crom_buf;
280 if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
281 err(1, "ioctl");
284 return error;
287 static void
288 show_crom(u_int32_t *crom_buf)
290 int i;
291 struct crom_context cc;
292 char *desc, info[256];
293 static char *key_types = "ICLD";
294 struct csrreg *reg;
295 struct csrdirectory *dir;
296 struct csrhdr *hdr;
297 u_int16_t crc;
299 printf("first quad: 0x%08x ", *crom_buf);
300 hdr = (struct csrhdr *)crom_buf;
301 if (hdr->info_len == 1) {
302 /* minimum ROM */
303 struct csrreg *reg;
304 reg = (struct csrreg *)hdr;
305 printf("verndor ID: 0x%06x\n", reg->val);
306 return;
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);
311 if (crc == hdr->crc)
312 printf("(OK)\n");
313 else
314 printf("(NG)\n");
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);
322 if (crc == dir->crc)
323 printf("(OK)\n");
324 else
325 printf("(NG)\n");
326 if (dir->crc_len < 1)
327 return;
328 while (cc.depth >= 0) {
329 desc = crom_desc(&cc, info, sizeof(info));
330 reg = crom_get(&cc);
331 for (i = 0; i < cc.depth; i++)
332 printf("\t");
333 printf("%02x(%c:%02x) %06x %s: %s\n",
334 reg->key,
335 key_types[(reg->key & CSRTYPE_MASK)>>6],
336 reg->key & CSRKEY_MASK, reg->val,
337 desc, info);
338 crom_next(&cc);
342 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
344 static void
345 dump_crom(u_int32_t *p)
347 int len=1024, i;
349 for (i = 0; i < len/(4*8); i ++) {
350 printf(DUMP_FORMAT,
351 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
352 p += 8;
356 static void
357 load_crom(char *filename, u_int32_t *p)
359 FILE *file;
360 int len=1024, i;
362 if ((file = fopen(filename, "r")) == NULL)
363 err(1, "load_crom");
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);
367 p += 8;
371 static void
372 show_topology_map(int fd)
374 struct fw_topology_map *tmap;
375 union fw_self_id sid;
376 int i;
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));
382 if (tmap == NULL)
383 return;
384 if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
385 err(1, "ioctl");
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"
391 " ini more\n");
392 for (i = 0; i < tmap->crc_len - 2; i++) {
393 sid = tmap->self_id[i];
394 if (sid.p0.sequel) {
395 printf("%02d sequel packet\n", sid.p0.phy_id);
396 continue;
398 printf("%02d %2d %2d %4s %d %d %3s"
399 " %s %s %s %d %d\n",
400 sid.p0.phy_id,
401 sid.p0.link_active,
402 sid.p0.gap_count,
403 speed[sid.p0.phy_speed],
404 sid.p0.phy_delay,
405 sid.p0.contender,
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,
411 sid.p0.more_packets
414 free(tmap);
418 main(int argc, char **argv)
420 char devname[256];
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)
427 break;
429 if (fd < 0)
430 err(1, "open");
432 if (argc < 2) {
433 list_dev(fd);
436 while ((ch = getopt(argc, argv, "g:o:s:b:rtc:d:l:R:S:")) != -1)
437 switch(ch) {
438 case 'g':
439 tmp = strtol(optarg, NULL, 0);
440 send_phy_config(fd, -1, tmp);
441 break;
442 case 'o':
443 tmp = strtol(optarg, NULL, 0);
444 send_link_on(fd, tmp);
445 break;
446 case 's':
447 tmp = strtol(optarg, NULL, 0);
448 reset_start(fd, tmp);
449 break;
450 case 'b':
451 tmp = strtol(optarg, NULL, 0);
452 set_pri_req(fd, tmp);
453 break;
454 case 'r':
455 if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
456 err(1, "ioctl");
457 break;
458 case 't':
459 show_topology_map(fd);
460 break;
461 case 'c':
462 tmp = strtol(optarg, NULL, 0);
463 get_crom(fd, tmp, crom_buf, len);
464 show_crom(crom_buf);
465 break;
466 case 'd':
467 tmp = strtol(optarg, NULL, 0);
468 get_crom(fd, tmp, crom_buf, len);
469 dump_crom(crom_buf);
470 break;
471 case 'l':
472 load_crom(optarg, crom_buf);
473 show_crom(crom_buf);
474 break;
475 #define TAG (1<<6)
476 #define CHANNEL 63
477 case 'R':
478 dvrecv(fd, optarg, TAG | CHANNEL, -1);
479 break;
480 case 'S':
481 dvsend(fd, optarg, TAG | CHANNEL, -1);
482 break;
483 default:
484 usage();
486 return 0;