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