xref: /freebsd/usr.sbin/fwcontrol/fwcontrol.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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, "fwcontrol [-g gap_count] [-b pri_req] [-c node]"
60 		" [-r] [-t] [-d node] [-l file] [-R file] [-S file]\n");
61 	fprintf(stderr, "\t-g: broadcast gap_count by phy_config packet\n");
62 	fprintf(stderr,
63 		"\t-b: set PRIORITY_BUDGET register on all supported nodes\n");
64 	fprintf(stderr, "\t-c: read configuration ROM\n");
65 	fprintf(stderr, "\t-r: bus reset\n");
66 	fprintf(stderr, "\t-t: read topology map\n");
67 	fprintf(stderr, "\t-d: hex dump of configuration ROM\n");
68 	fprintf(stderr,
69 		"\t-l: load and parse hex dump file of configuration ROM\n");
70 	fprintf(stderr, "\t-R: Receive DV stream\n");
71 	fprintf(stderr, "\t-S: Send DV stream\n");
72 	exit(0);
73 }
74 
75 static struct fw_devlstreq *
76 get_dev(int fd)
77 {
78 	struct fw_devlstreq *data;
79 
80 	data = (struct fw_devlstreq *)malloc(sizeof(struct fw_devlstreq));
81 	if (data == NULL)
82 		err(1, "malloc");
83 	if( ioctl(fd, FW_GDEVLST, data) < 0) {
84        			err(1, "ioctl");
85 	}
86 	return data;
87 }
88 
89 static void
90 list_dev(int fd)
91 {
92 	struct fw_devlstreq *data;
93 	struct fw_devinfo *devinfo;
94 	int i;
95 
96 	data = get_dev(fd);
97 	printf("%d devices (info_len=%d)\n", data->n, data->info_len);
98 	printf("node       EUI64       status\n");
99 	for (i = 0; i < data->info_len; i++) {
100 		devinfo = &data->dev[i];
101 		printf("%4d  %08x%08x %6d\n",
102 			(devinfo->status || i == 0) ? devinfo->dst : -1,
103 			devinfo->eui.hi,
104 			devinfo->eui.lo,
105 			devinfo->status
106 		);
107 	}
108 	free((void *)data);
109 }
110 
111 static u_int32_t
112 read_write_quad(int fd, struct fw_eui64 eui, u_int32_t addr_lo, int read, u_int32_t data)
113 {
114         struct fw_asyreq *asyreq;
115 	u_int32_t *qld, res;
116 
117         asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 16);
118 	asyreq->req.len = 16;
119 	asyreq->req.type = FWASREQEUI;
120 	asyreq->req.dst.eui = eui;
121 #if 0
122 	asyreq->pkt.mode.rreqq.dst = htons(FWLOCALBUS | node);
123 #endif
124 	asyreq->pkt.mode.rreqq.tlrt = 0;
125 	if (read)
126 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_RREQQ;
127 	else
128 		asyreq->pkt.mode.rreqq.tcode = FWTCODE_WREQQ;
129 
130 	asyreq->pkt.mode.rreqq.dest_hi = htons(0xffff);
131 	asyreq->pkt.mode.rreqq.dest_lo = htonl(addr_lo);
132 
133 	qld = (u_int32_t *)&asyreq->pkt;
134 	if (!read)
135 		asyreq->pkt.mode.wreqq.data = data;
136 
137 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
138        		err(1, "ioctl");
139 	}
140 	res = qld[3];
141 	free(asyreq);
142 	if (read)
143 		return ntohl(res);
144 	else
145 		return 0;
146 }
147 
148 static void
149 send_phy_config(int fd, int root_node, int gap_count)
150 {
151         struct fw_asyreq *asyreq;
152 
153 	asyreq = (struct fw_asyreq *)malloc(sizeof(struct fw_asyreq_t) + 12);
154 	asyreq->req.len = 12;
155 	asyreq->req.type = FWASREQNODE;
156 	asyreq->pkt.mode.ld[0] = 0;
157 	asyreq->pkt.mode.ld[1] = 0;
158 	asyreq->pkt.mode.common.tcode = FWTCODE_PHY;
159 	if (root_node >= 0)
160 		asyreq->pkt.mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
161 	if (gap_count >= 0)
162 		asyreq->pkt.mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
163 	asyreq->pkt.mode.ld[2] = ~asyreq->pkt.mode.ld[1];
164 
165 	printf("send phy_config root_node=%d gap_count=%d\n",
166 						root_node, gap_count);
167 
168 	if (ioctl(fd, FW_ASYREQ, asyreq) < 0) {
169        		err(1, "ioctl");
170 	}
171 }
172 
173 static void
174 set_pri_req(int fd, int pri_req)
175 {
176 	struct fw_devlstreq *data;
177 	struct fw_devinfo *devinfo;
178 	u_int32_t max, reg, old;
179 	int i;
180 
181 	data = get_dev(fd);
182 #define BUGET_REG 0xf0000218
183 	for (i = 0; i < data->info_len; i++) {
184 		devinfo = &data->dev[i];
185 		if (!devinfo->status)
186 			continue;
187 		reg = read_write_quad(fd, devinfo->eui, BUGET_REG, 1, 0);
188 		printf("%d %08x:%08x, %08x",
189 			devinfo->dst, devinfo->eui.hi, devinfo->eui.lo, reg);
190 		if (reg > 0 && pri_req >= 0) {
191 			old = (reg & 0x3f);
192 			max = (reg & 0x3f00) >> 8;
193 			if (pri_req > max)
194 				pri_req =  max;
195 			printf(" 0x%x -> 0x%x\n", old, pri_req);
196 			read_write_quad(fd, devinfo->eui, BUGET_REG, 0, pri_req);
197 		} else {
198 			printf("\n");
199 		}
200 	}
201 	free((void *)data);
202 }
203 
204 static void
205 parse_bus_info_block(u_int32_t *p, int info_len)
206 {
207 	int i;
208 
209 	for (i = 0; i < info_len; i++) {
210 		printf("bus_info%d: 0x%08x\n", i, *p++);
211 	}
212 }
213 
214 static int
215 get_crom(int fd, int node, void *crom_buf, int len)
216 {
217 	struct fw_crom_buf buf;
218 	int i, error;
219 	struct fw_devlstreq *data;
220 
221 	data = get_dev(fd);
222 
223 	for (i = 0; i < data->info_len; i++) {
224 		if (data->dev[i].dst == node && data->dev[i].eui.lo != 0)
225 			break;
226 	}
227 	if (i == data->info_len)
228 		errx(1, "no such node %d.", node);
229 	else if (i == 0)
230 		errx(1, "node %d is myself.", node);
231 	else
232 		buf.eui = data->dev[i].eui;
233 	free((void *)data);
234 
235 	buf.len = len;
236 	buf.ptr = crom_buf;
237 	if ((error = ioctl(fd, FW_GCROM, &buf)) < 0) {
238        		err(1, "ioctl");
239 	}
240 
241 	return error;
242 }
243 
244 static void
245 show_crom(u_int32_t *crom_buf)
246 {
247 	int i;
248 	struct crom_context cc;
249 	char *desc, info[256];
250 	static char *key_types = "ICLD";
251 	struct csrreg *reg;
252 	struct csrdirectory *dir;
253 	struct csrhdr *hdr;
254 
255 	printf("first quad: 0x%08x\n", *crom_buf);
256 	hdr = (struct csrhdr *)crom_buf;
257 	if (hdr->info_len == 1) {
258 		/* minimum ROM */
259 		struct csrreg *reg;
260 		reg = (struct csrreg *)hdr;
261 		printf("verndor ID: 0x%06x\n",  reg->val);
262 		return;
263 	}
264 	printf("len: %d\n", hdr->crc_len);
265 	parse_bus_info_block(crom_buf+1, hdr->info_len);
266 
267 	crom_init_context(&cc, crom_buf);
268 	dir = cc.stack[0].dir;
269 	printf("root_directory: len=0x%04x(%d) crc=0x%04x\n",
270 			dir->crc_len, dir->crc_len, dir->crc);
271 	if (dir->crc_len < 1)
272 		return;
273 	while (cc.depth >= 0) {
274 		desc = crom_desc(&cc, info, sizeof(info));
275 		reg = crom_get(&cc);
276 		for (i = 0; i < cc.depth; i++)
277 			printf("\t");
278 		printf("%02x(%c:%02x) %06x %s: %s\n",
279 			reg->key,
280 			key_types[(reg->key & CSRTYPE_MASK)>>6],
281 			reg->key & CSRKEY_MASK, reg->val,
282 			desc, info);
283 		crom_next(&cc);
284 	}
285 }
286 
287 #define DUMP_FORMAT	"%08x %08x %08x %08x %08x %08x %08x %08x\n"
288 
289 static void
290 dump_crom(u_int32_t *p)
291 {
292 	int len=1024, i;
293 
294 	for (i = 0; i < len/(4*8); i ++) {
295 		printf(DUMP_FORMAT,
296 			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
297 		p += 8;
298 	}
299 }
300 
301 static void
302 load_crom(char *filename, u_int32_t *p)
303 {
304 	FILE *file;
305 	int len=1024, i;
306 
307 	if ((file = fopen(filename, "r")) == NULL)
308 		err(1, "load_crom");
309 	for (i = 0; i < len/(4*8); i ++) {
310 		fscanf(file, DUMP_FORMAT,
311 			p, p+1, p+2, p+3, p+4, p+5, p+6, p+7);
312 		p += 8;
313 	}
314 }
315 
316 static void
317 show_topology_map(int fd)
318 {
319 	struct fw_topology_map *tmap;
320 	union fw_self_id sid;
321 	int i;
322 	static char *port_status[] = {" ", "-", "P", "C"};
323 	static char *pwr_class[] = {" 0W", "15W", "30W", "45W",
324 					"-1W", "-2W", "-5W", "-9W"};
325 	static char *speed[] = {"S100", "S200", "S400", "S800"};
326 	tmap = malloc(sizeof(struct fw_topology_map));
327 	if (tmap == NULL)
328 		return;
329 	if (ioctl(fd, FW_GTPMAP, tmap) < 0) {
330        		err(1, "ioctl");
331 	}
332 	printf("crc_len: %d generation:%d node_count:%d sid_count:%d\n",
333 		tmap->crc_len, tmap->generation,
334 		tmap->node_count, tmap->self_id_count);
335 	printf("id link gap_cnt speed delay cIRM power port0 port1 port2"
336 		" ini more\n");
337 	for (i = 0; i < tmap->crc_len - 2; i++) {
338 		sid = tmap->self_id[i];
339 		if (sid.p0.sequel) {
340 			printf("%02d sequel packet\n", sid.p0.phy_id);
341 			continue;
342 		}
343 		printf("%02d   %2d      %2d  %4s     %d    %d   %3s"
344 				"     %s     %s     %s   %d    %d\n",
345 			sid.p0.phy_id,
346 			sid.p0.link_active,
347 			sid.p0.gap_count,
348 			speed[sid.p0.phy_speed],
349 			sid.p0.phy_delay,
350 			sid.p0.contender,
351 			pwr_class[sid.p0.power_class],
352 			port_status[sid.p0.port0],
353 			port_status[sid.p0.port1],
354 			port_status[sid.p0.port2],
355 			sid.p0.initiated_reset,
356 			sid.p0.more_packets
357 		);
358 	}
359 	free(tmap);
360 }
361 
362 int
363 main(int argc, char **argv)
364 {
365 	char devname[256];
366 	u_int32_t crom_buf[1024/4];
367 	int fd, i, tmp, ch, len=1024;
368 
369 	for (i = 0; i < 4; i++) {
370 		snprintf(devname, sizeof(devname), "/dev/fw%d", i);
371 		if ((fd = open(devname, O_RDWR)) >= 0)
372 			break;
373 	}
374 	if (fd < 0)
375 		err(1, "open");
376 
377 	if (argc < 2) {
378 		list_dev(fd);
379 	}
380 
381 	while ((ch = getopt(argc, argv, "g:b:rtc:d:l:R:S:")) != -1)
382 		switch(ch) {
383 		case 'g':
384 			/* gap count */
385 			tmp = strtol(optarg, NULL, 0);
386 			send_phy_config(fd, -1, tmp);
387 			break;
388 		case 'b':
389 			tmp = strtol(optarg, NULL, 0);
390 			set_pri_req(fd, tmp);
391 			break;
392 		case 'r':
393 			if(ioctl(fd, FW_IBUSRST, &tmp) < 0)
394                        		err(1, "ioctl");
395 			break;
396 		case 't':
397 			show_topology_map(fd);
398 			break;
399 		case 'c':
400 			tmp = strtol(optarg, NULL, 0);
401 			get_crom(fd, tmp, crom_buf, len);
402 			show_crom(crom_buf);
403 			break;
404 		case 'd':
405 			tmp = strtol(optarg, NULL, 0);
406 			get_crom(fd, tmp, crom_buf, len);
407 			dump_crom(crom_buf);
408 			break;
409 		case 'l':
410 			load_crom(optarg, crom_buf);
411 			show_crom(crom_buf);
412 			break;
413 #define TAG	(1<<6)
414 #define CHANNEL	63
415 		case 'R':
416 			dvrecv(fd, optarg, TAG | CHANNEL, -1);
417 			break;
418 		case 'S':
419 			dvsend(fd, optarg, TAG | CHANNEL, -1);
420 			break;
421 		default:
422 			usage();
423 		}
424 	return 0;
425 }
426