xref: /freebsd/usr.sbin/fwcontrol/fwcontrol.c (revision e0c27215058b5786c78fcfb3963eebe61a989511)
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$
35  */
36 
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 <dev/firewire/firewire.h>
43 #include <dev/firewire/iec13213.h>
44 
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>
52 
53 extern int dvrecv(int, char *, char, int);
54 extern int dvsend(int, char *, char, int);
55 
56 static void
57 usage(void)
58 {
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);
74 }
75 
76 static struct fw_devlstreq *
77 get_dev(int fd)
78 {
79 	struct fw_devlstreq *data;
80 
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");
86 	}
87 	return data;
88 }
89 
90 static void
91 list_dev(int fd)
92 {
93 	struct fw_devlstreq *data;
94 	struct fw_devinfo *devinfo;
95 	int i;
96 
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
107 		);
108 	}
109 	free((void *)data);
110 }
111 
112 static u_int32_t
113 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data)
114 {
115         struct fw_asyreq *asyreq;
116 	u_int32_t *qld, res;
117 
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 (read)
129 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
130 	else
131 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
132 
133 	asyreq->pkt.mode.rreqq.dest_hi = 0xffff;
134 	asyreq->pkt.mode.rreqq.dest_lo = addr_lo;
135 
136 	qld = (u_int32_t *)&asyreq->pkt;
137 	if (!read)
138 		asyreq->pkt.mode.wreqq.data = data;
139 
140 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
141        		err(1, "ioctl");
142 	}
143 	res = qld[3];
144 	free(asyreq);
145 	if (read)
146 		return ntohl(res);
147 	else
148 		return 0;
149 }
150 
151 static void
152 send_phy_config(int fd, int root_node, int gap_count)
153 {
154         struct fw_asyreq *asyreq;
155 
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];
167 
168 	printf("send phy_config root_node=%d gap_count=%d\n",
169 						root_node, gap_count);
170 
171 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
172        		err(1, "ioctl");
173 	free(asyreq);
174 }
175 
176 static void
177 send_link_on(int fd, int node)
178 {
179         struct fw_asyreq *asyreq;
180 
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];
187 
188 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
189        		err(1, "ioctl");
190 	free(asyreq);
191 }
192 
193 static void
194 reset_start(int fd, int node)
195 {
196         struct fw_asyreq *asyreq;
197 
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;
204 
205 	asyreq->pkt.mode.wreqq.dest_hi = 0xffff;
206 	asyreq->pkt.mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
207 
208 	asyreq->pkt.mode.wreqq.data = htonl(0x1);
209 
210 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0)
211        		err(1, "ioctl");
212 	free(asyreq);
213 }
214 
215 static void
216 set_pri_req(int fd, int pri_req)
217 {
218 	struct fw_devlstreq *data;
219 	struct fw_devinfo *devinfo;
220 	u_int32_t max, reg, old;
221 	int i;
222 
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 && pri_req >= 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");
241 		}
242 	}
243 	free((void *)data);
244 }
245 
246 static void
247 parse_bus_info_block(u_int32_t *p, int info_len)
248 {
249 	int i;
250 	struct bus_info *bi;
251 
252 	bi = (struct bus_info *)p;
253 	printf("bus_name: 0x%04x\n"
254 		"irmc:%d cmc:%d isc:%d bmc:%d pmc:%d\n"
255 		"cyc_clk_acc:%d max_rec:%d max_rom:%d\n"
256 		"generation:%d link_spd:%d\n"
257 		"EUI64: 0x%08x 0x%08x\n",
258 		bi->bus_name,
259 		bi->irmc, bi->cmc, bi->isc, bi->bmc, bi->pmc,
260 		bi->cyc_clk_acc, bi->max_rec, bi->max_rom,
261 		bi->generation, bi->link_spd,
262 		bi->eui64.hi, bi->eui64.lo);
263 }
264 
265 static int
266 get_crom(int fd, int node, void *crom_buf, int len)
267 {
268 	struct fw_crom_buf buf;
269 	int i, error;
270 	struct fw_devlstreq *data;
271 
272 	data = get_dev(fd);
273 
274 	for (i = 0; i < data->info_len; i++) {
275 		if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
276 			break;
277 	}
278 	if (i == data->info_len)
279 		errx(1, "no such node %d.", node);
280 	else
281 		buf.eui = data->dev[i].eui;
282 	free((void *)data);
283 
284 	buf.len = len;
285 	buf.ptr = crom_buf;
286 	bzero(crom_buf, len);
287 	if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
288        		err(1, "ioctl");
289 	}
290 
291 	return error;
292 }
293 
294 static void
295 show_crom(u_int32_t *crom_buf)
296 {
297 	int i;
298 	struct crom_context cc;
299 	char *desc, info[256];
300 	static char *key_types = "ICLD";
301 	struct csrreg *reg;
302 	struct csrdirectory *dir;
303 	struct csrhdr *hdr;
304 	u_int16_t crc;
305 
306 	printf("first quad: 0x%08x ", *crom_buf);
307 	hdr = (struct csrhdr *)crom_buf;
308 	if (hdr->info_len == 1) {
309 		/* minimum ROM */
310 		struct csrreg *reg;
311 		reg = (struct csrreg *)hdr;
312 		printf("verndor ID: 0x%06x\n",  reg->val);
313 		return;
314 	}
315 	printf("info_len=%d crc_len=%d crc=0x%04x",
316 		hdr->info_len, hdr->crc_len, hdr->crc);
317 	crc = crom_crc(crom_buf+1, hdr->crc_len);
318 	if (crc == hdr->crc)
319 		printf("(OK)\n");
320 	else
321 		printf("(NG)\n");
322 	parse_bus_info_block(crom_buf+1, hdr->info_len);
323 
324 	crom_init_context(&cc, crom_buf);
325 	dir = cc.stack[0].dir;
326 	printf("root_directory: len=0x%04x(%d) crc=0x%04x",
327 			dir->crc_len, dir->crc_len, dir->crc);
328 	crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
329 	if (crc == dir->crc)
330 		printf("(OK)\n");
331 	else
332 		printf("(NG)\n");
333 	if (dir->crc_len < 1)
334 		return;
335 	while (cc.depth >= 0) {
336 		desc = crom_desc(&cc, info, sizeof(info));
337 		reg = crom_get(&cc);
338 		for (i = 0; i < cc.depth; i++)
339 			printf("\t");
340 		printf("%02x(%c:%02x) %06x %s: %s\n",
341 			reg->key,
342 			key_types[(reg->key & CSRTYPE_MASK)>>6],
343 			reg->key & CSRKEY_MASK, reg->val,
344 			desc, info);
345 		crom_next(&cc);
346 	}
347 }
348 
349 #define DUMP_FORMAT	"%08x %08x %08x %08x %08x %08x %08x %08x\n"
350 
351 static void
352 dump_crom(u_int32_t *p)
353 {
354 	int len=1024, i;
355 
356 	for (i = 0; i < len/(4*8); i ++) {
357 		printf(DUMP_FORMAT,
358 			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
359 		p += 8;
360 	}
361 }
362 
363 static void
364 load_crom(char *filename, u_int32_t *p)
365 {
366 	FILE *file;
367 	int len=1024, i;
368 
369 	if ((file = fopen(filename, "r")) == NULL)
370 		err(1, "load_crom");
371 	for (i = 0; i < len/(4*8); i ++) {
372 		fscanf(file, DUMP_FORMAT,
373 			p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
374 		p += 8;
375 	}
376 }
377 
378 static void
379 show_topology_map(int fd)
380 {
381 	struct fw_topology_map *tmap;
382 	union fw_self_id sid;
383 	int i;
384 	static char *port_status[] = {" ", "-", "P", "C"};
385 	static char *pwr_class[] = {" 0W", "15W", "30W", "45W",
386 					"-1W", "-2W", "-5W", "-9W"};
387 	static char *speed[] = {"S100", "S200", "S400", "S800"};
388 	tmap = malloc(sizeof(struct fw_topology_map));
389 	if (tmap == NULL)
390 		return;
391 	if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
392        		err(1, "ioctl");
393 	}
394 	printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
395 		tmap->crc_len, tmap->generation,
396 		tmap->node_count, tmap->self_id_count);
397 	printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
398 		" ini more\n");
399 	for (i = 0; i < tmap->crc_len - 2; i++) {
400 		sid = tmap->self_id[i];
401 		if (sid.p0.sequel) {
402 			printf("%02d sequel packet\n", sid.p0.phy_id);
403 			continue;
404 		}
405 		printf("%02d   %2d      %2d  %4s     %d    %d   %3s"
406 				"     %s     %s     %s   %d    %d\n",
407 			sid.p0.phy_id,
408 			sid.p0.link_active,
409 			sid.p0.gap_count,
410 			speed[sid.p0.phy_speed],
411 			sid.p0.phy_delay,
412 			sid.p0.contender,
413 			pwr_class[sid.p0.power_class],
414 			port_status[sid.p0.port0],
415 			port_status[sid.p0.port1],
416 			port_status[sid.p0.port2],
417 			sid.p0.initiated_reset,
418 			sid.p0.more_packets
419 		);
420 	}
421 	free(tmap);
422 }
423 
424 int
425 main(int argc, char **argv)
426 {
427 	char devname[256];
428 	u_int32_t crom_buf[1024/4];
429 	int fd, i, tmp, ch, len=1024;
430 
431 	for (i = 0; i < 4; i++) {
432 		snprintf(devname, sizeof(devname), "/dev/fw%d", i);
433 		if ((fd = open(devname, O_RDWR)) >= 0)
434 			break;
435 	}
436 	if (fd < 0)
437 		err(1, "open");
438 
439 	if (argc < 2) {
440 		list_dev(fd);
441 	}
442 
443 	while ((ch = getopt(argc, argv, "g:o:s:b:rtc:d:l:R:S:")) != -1)
444 		switch(ch) {
445 		case 'g':
446 			tmp = strtol(optarg, NULL, 0);
447 			send_phy_config(fd, -1, tmp);
448 			break;
449 		case 'o':
450 			tmp = strtol(optarg, NULL, 0);
451 			send_link_on(fd, tmp);
452 			break;
453 		case 's':
454 			tmp = strtol(optarg, NULL, 0);
455 			reset_start(fd, tmp);
456 			break;
457 		case 'b':
458 			tmp = strtol(optarg, NULL, 0);
459 			set_pri_req(fd, tmp);
460 			break;
461 		case 'r':
462 			if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
463                        		err(1, "ioctl");
464 			break;
465 		case 't':
466 			show_topology_map(fd);
467 			break;
468 		case 'c':
469 			tmp = strtol(optarg, NULL, 0);
470 			get_crom(fd, tmp, crom_buf, len);
471 			show_crom(crom_buf);
472 			break;
473 		case 'd':
474 			tmp = strtol(optarg, NULL, 0);
475 			get_crom(fd, tmp, crom_buf, len);
476 			dump_crom(crom_buf);
477 			break;
478 		case 'l':
479 			load_crom(optarg, crom_buf);
480 			show_crom(crom_buf);
481 			break;
482 #define TAG	(1<<6)
483 #define CHANNEL	63
484 		case 'R':
485 			dvrecv(fd, optarg, TAG | CHANNEL, -1);
486 			break;
487 		case 'S':
488 			dvsend(fd, optarg, TAG | CHANNEL, -1);
489 			break;
490 		default:
491 			usage();
492 		}
493 	return 0;
494 }
495