xref: /netbsd/sys/dev/ieee1394/firewire.c (revision 6550d01e)
1 /*	$NetBSD: firewire.c,v 1.38 2010/09/07 07:26:54 cegger Exp $	*/
2 /*-
3  * Copyright (c) 2003 Hidetoshi Shimokawa
4  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the acknowledgement as bellow:
17  *
18  *    This product includes software developed by K. Kobayashi and H. Shimokawa
19  *
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/dev/firewire/firewire.c,v 1.110 2009/04/07 02:33:46 sbruno Exp $
36  *
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: firewire.c,v 1.38 2010/09/07 07:26:54 cegger Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/callout.h>
45 #include <sys/condvar.h>
46 #include <sys/conf.h>
47 #include <sys/device.h>
48 #include <sys/errno.h>
49 #include <sys/kernel.h>
50 #include <sys/kthread.h>
51 #include <sys/malloc.h>
52 #include <sys/queue.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 
56 #include <dev/ieee1394/firewire.h>
57 #include <dev/ieee1394/firewirereg.h>
58 #include <dev/ieee1394/fwmem.h>
59 #include <dev/ieee1394/iec13213.h>
60 #include <dev/ieee1394/iec68113.h>
61 
62 #include "locators.h"
63 
64 struct crom_src_buf {
65 	struct crom_src	src;
66 	struct crom_chunk root;
67 	struct crom_chunk vendor;
68 	struct crom_chunk hw;
69 };
70 
71 int firewire_debug = 0, try_bmr = 1, hold_count = 0;
72 /*
73  * Setup sysctl(3) MIB, hw.ieee1394if.*
74  *
75  * TBD condition CTLFLAG_PERMANENT on being a module or not
76  */
77 SYSCTL_SETUP(sysctl_ieee1394if, "sysctl ieee1394if(4) subtree setup")
78 {
79 	int rc, ieee1394if_node_num;
80 	const struct sysctlnode *node;
81 
82 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
83 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
84 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
85 		goto err;
86 	}
87 
88 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
89 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee1394if",
90 	    SYSCTL_DESCR("ieee1394if controls"),
91 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
92 		goto err;
93 	}
94 	ieee1394if_node_num = node->sysctl_num;
95 
96 	/* ieee1394if try bus manager flag */
97 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
98 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
99 	    "try_bmr", SYSCTL_DESCR("Try to be a bus manager"),
100 	    NULL, 0, &try_bmr,
101 	    0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
102 		goto err;
103 	}
104 
105 	/* ieee1394if hold count */
106 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
107 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
108 	    "hold_count", SYSCTL_DESCR("Number of count of "
109 	    "bus resets for removing lost device information"),
110 	    NULL, 0, &hold_count,
111 	    0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
112 		goto err;
113 	}
114 
115 	/* ieee1394if driver debug flag */
116 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
117 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
118 	    "ieee1394_debug", SYSCTL_DESCR("ieee1394if driver debug flag"),
119 	    NULL, 0, &firewire_debug,
120 	    0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
121 		goto err;
122 	}
123 
124 	return;
125 
126 err:
127 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
128 }
129 
130 MALLOC_DEFINE(M_FW, "ieee1394", "IEEE1394");
131 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/IEEE1394");
132 
133 #define FW_MAXASYRTY 4
134 
135 #define FW_GENERATION_CHANGEABLE	2
136 
137 static int firewirematch (device_t, cfdata_t, void *);
138 static void firewireattach (device_t, device_t, void *);
139 static int firewiredetach (device_t, int);
140 static int firewire_print (void *, const char *);
141 
142 int firewire_resume (struct firewire_comm *);
143 
144 static void fw_asystart(struct fw_xfer *);
145 static void firewire_xfer_timeout(struct firewire_comm *);
146 static void firewire_watchdog(void *);
147 static void fw_xferq_drain(struct fw_xferq *);
148 static void fw_reset_csr(struct firewire_comm *);
149 static void fw_init_crom(struct firewire_comm *);
150 static void fw_reset_crom(struct firewire_comm *);
151 static void fw_dump_hdr(struct fw_pkt *, const char *);
152 static void fw_tl_free(struct firewire_comm *, struct fw_xfer *);
153 static struct fw_xfer *fw_tl2xfer(struct firewire_comm *, int, int, int);
154 static void fw_phy_config(struct firewire_comm *, int, int);
155 static void fw_print_sid(uint32_t);
156 static void fw_bus_probe(struct firewire_comm *);
157 static int fw_explore_read_quads(struct fw_device *, int, uint32_t *, int);
158 static int fw_explore_csrblock(struct fw_device *, int, int);
159 static int fw_explore_node(struct fw_device *);
160 static union fw_self_id *fw_find_self_id(struct firewire_comm *, int);
161 static void fw_explore(struct firewire_comm *);
162 static void fw_bus_probe_thread(void *);
163 static void fw_attach_dev(struct firewire_comm *);
164 static int fw_get_tlabel(struct firewire_comm *, struct fw_xfer *);
165 static void fw_rcv_copy(struct fw_rcv_buf *);
166 static void fw_try_bmr_callback(struct fw_xfer *);
167 static void fw_try_bmr(void *);
168 static int fw_bmr(struct firewire_comm *);
169 
170 
171 CFATTACH_DECL_NEW(ieee1394if, sizeof(struct firewire_softc),
172     firewirematch, firewireattach, firewiredetach, NULL);
173 
174 
175 const char *fw_linkspeed[] = {
176 	"S100", "S200", "S400", "S800",
177 	"S1600", "S3200", "undef", "undef"
178 };
179 
180 static const char *tcode_str[] = {
181 	"WREQQ", "WREQB", "WRES",   "undef",
182 	"RREQQ", "RREQB", "RRESQ",  "RRESB",
183 	"CYCS",  "LREQ",  "STREAM", "LRES",
184 	"undef", "undef", "PHY",    "undef"
185 };
186 
187 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
188 #define MAX_GAPHOP 15
189 u_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
190 		   21, 24, 26, 29, 32, 35, 37, 40};
191 
192 
193 static int
194 firewirematch(device_t parent, cfdata_t cf, void *aux)
195 {
196 
197 	return 1;	/* always match */
198 }
199 
200 static void
201 firewireattach(device_t parent, device_t self, void *aux)
202 {
203 	struct firewire_softc *sc = device_private(self);
204 	struct firewire_comm *fc = device_private(parent);
205 	struct fw_attach_args faa;
206 	struct firewire_dev_list *devlist;
207 
208 	aprint_naive("\n");
209 	aprint_normal(": IEEE1394 bus\n");
210 
211 	fc->bdev = sc->dev = self;
212 	sc->fc = fc;
213 	SLIST_INIT(&sc->devlist);
214 
215 	fc->status = FWBUSNOTREADY;
216 
217 	if (fc->nisodma > FWMAXNDMA)
218 	    fc->nisodma = FWMAXNDMA;
219 
220 	fc->crom_src_buf =
221 	    (struct crom_src_buf *)malloc(sizeof(struct crom_src_buf),
222 	    M_FW, M_NOWAIT | M_ZERO);
223 	if (fc->crom_src_buf == NULL) {
224 		aprint_error_dev(fc->bdev, "Malloc Failure crom src buff\n");
225 		return;
226 	}
227 	fc->topology_map =
228 	    (struct fw_topology_map *)malloc(sizeof(struct fw_topology_map),
229 	    M_FW, M_NOWAIT | M_ZERO);
230 	if (fc->topology_map == NULL) {
231 		aprint_error_dev(fc->dev, "Malloc Failure topology map\n");
232 		free(fc->crom_src_buf, M_FW);
233 		return;
234 	}
235 	fc->speed_map =
236 	    (struct fw_speed_map *)malloc(sizeof(struct fw_speed_map),
237 	    M_FW, M_NOWAIT | M_ZERO);
238 	if (fc->speed_map == NULL) {
239 		aprint_error_dev(fc->dev, "Malloc Failure speed map\n");
240 		free(fc->crom_src_buf, M_FW);
241 		free(fc->topology_map, M_FW);
242 		return;
243 	}
244 
245 	mutex_init(&fc->tlabel_lock, MUTEX_DEFAULT, IPL_VM);
246 	mutex_init(&fc->fc_mtx, MUTEX_DEFAULT, IPL_VM);
247 	mutex_init(&fc->wait_lock, MUTEX_DEFAULT, IPL_VM);
248 	cv_init(&fc->fc_cv, "ieee1394");
249 
250 	callout_init(&fc->timeout_callout, CALLOUT_MPSAFE);
251 	callout_setfunc(&fc->timeout_callout, firewire_watchdog, fc);
252 	callout_init(&fc->bmr_callout, CALLOUT_MPSAFE);
253 	callout_setfunc(&fc->bmr_callout, fw_try_bmr, fc);
254 	callout_init(&fc->busprobe_callout, CALLOUT_MPSAFE);
255 	callout_setfunc(&fc->busprobe_callout, (void *)fw_bus_probe, fc);
256 
257 	callout_schedule(&fc->timeout_callout, hz);
258 
259 	/* create thread */
260 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, fw_bus_probe_thread,
261 	    fc, &fc->probe_thread, "fw%dprobe", device_unit(fc->bdev)))
262 		aprint_error_dev(self, "kthread_create failed\n");
263 	config_pending_incr();
264 
265 	devlist = malloc(sizeof(struct firewire_dev_list), M_DEVBUF, M_NOWAIT);
266 	if (devlist == NULL) {
267 		aprint_error_dev(self, "device list allocation failed\n");
268 		return;
269 	}
270 
271 	faa.name = "fwip";
272 	faa.fc = fc;
273 	faa.fwdev = NULL;
274 	devlist->dev = config_found(sc->dev, &faa, firewire_print);
275 	if (devlist->dev == NULL)
276 		free(devlist, M_DEVBUF);
277 	else
278 		SLIST_INSERT_HEAD(&sc->devlist, devlist, link);
279 
280 	/* bus_reset */
281 	fw_busreset(fc, FWBUSNOTREADY);
282 	fc->ibr(fc);
283 
284 	if (!pmf_device_register(self, NULL, NULL))
285 		aprint_error_dev(self, "couldn't establish power handler\n");
286 
287 	return;
288 }
289 
290 static int
291 firewiredetach(device_t self, int flags)
292 {
293 	struct firewire_softc *sc = device_private(self);
294 	struct firewire_comm *fc;
295 	struct fw_device *fwdev, *fwdev_next;
296 	struct firewire_dev_list *devlist;
297 	int err;
298 
299 	fc = sc->fc;
300 	mutex_enter(&fc->wait_lock);
301 	fc->status = FWBUSDETACH;
302 	cv_signal(&fc->fc_cv);
303 	while (fc->status != FWBUSDETACHOK) {
304 		err = cv_timedwait_sig(&fc->fc_cv, &fc->wait_lock, hz * 60);
305 		if (err == EWOULDBLOCK) {
306 			aprint_error_dev(self,
307 			    "firewire probe thread didn't die\n");
308 			break;
309 		}
310 	}
311 	mutex_exit(&fc->wait_lock);
312 
313 
314 	while ((devlist = SLIST_FIRST(&sc->devlist)) != NULL) {
315 		if ((err = config_detach(devlist->dev, flags)) != 0)
316 			return err;
317 		SLIST_REMOVE(&sc->devlist, devlist, firewire_dev_list, link);
318 		free(devlist, M_DEVBUF);
319 	}
320 
321 	callout_stop(&fc->timeout_callout);
322 	callout_stop(&fc->bmr_callout);
323 	callout_stop(&fc->busprobe_callout);
324 
325 	/* XXX xfer_free and untimeout on all xfers */
326 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
327 	    fwdev = fwdev_next) {
328 		fwdev_next = STAILQ_NEXT(fwdev, link);
329 		free(fwdev, M_FW);
330 	}
331 	free(fc->topology_map, M_FW);
332 	free(fc->speed_map, M_FW);
333 	free(fc->crom_src_buf, M_FW);
334 
335 	cv_destroy(&fc->fc_cv);
336 	mutex_destroy(&fc->wait_lock);
337 	mutex_destroy(&fc->fc_mtx);
338 	mutex_destroy(&fc->tlabel_lock);
339 	return 0;
340 }
341 
342 static int
343 firewire_print(void *aux, const char *pnp)
344 {
345 	struct fw_attach_args *fwa = (struct fw_attach_args *)aux;
346 
347 	if (pnp)
348 		aprint_normal("%s at %s", fwa->name, pnp);
349 
350 	return UNCONF;
351 }
352 
353 int
354 firewire_resume(struct firewire_comm *fc)
355 {
356 
357 	fc->status = FWBUSNOTREADY;
358 	return 0;
359 }
360 
361 
362 /*
363  * Lookup fwdev by node id.
364  */
365 struct fw_device *
366 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
367 {
368 	struct fw_device *fwdev;
369 
370 	mutex_enter(&fc->fc_mtx);
371 	STAILQ_FOREACH(fwdev, &fc->devices, link)
372 		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
373 			break;
374 	mutex_exit(&fc->fc_mtx);
375 
376 	return fwdev;
377 }
378 
379 /*
380  * Lookup fwdev by EUI64.
381  */
382 struct fw_device *
383 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
384 {
385 	struct fw_device *fwdev;
386 
387 	mutex_enter(&fc->fc_mtx);
388 	STAILQ_FOREACH(fwdev, &fc->devices, link)
389 		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
390 			break;
391 	mutex_exit(&fc->fc_mtx);
392 
393 	if (fwdev == NULL)
394 		return NULL;
395 	if (fwdev->status == FWDEVINVAL)
396 		return NULL;
397 	return fwdev;
398 }
399 
400 /*
401  * Async. request procedure for userland application.
402  */
403 int
404 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
405 {
406 	struct fw_xferq *xferq;
407 	int len;
408 	struct fw_pkt *fp;
409 	int tcode;
410 	const struct tcode_info *info;
411 
412 	if (xfer == NULL)
413 		return EINVAL;
414 	if (xfer->hand == NULL) {
415 		aprint_error_dev(fc->bdev, "hand == NULL\n");
416 		return EINVAL;
417 	}
418 	fp = &xfer->send.hdr;
419 
420 	tcode = fp->mode.common.tcode & 0xf;
421 	info = &fc->tcode[tcode];
422 	if (info->flag == 0) {
423 		aprint_error_dev(fc->bdev, "invalid tcode=%x\n", tcode);
424 		return EINVAL;
425 	}
426 
427 	/* XXX allow bus explore packets only after bus rest */
428 	if ((fc->status < FWBUSEXPLORE) &&
429 	    ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) ||
430 	    (fp->mode.rreqq.dest_lo < 0xf0000000) ||
431 	    (fp->mode.rreqq.dest_lo >= 0xf0001000))) {
432 		xfer->resp = EAGAIN;
433 		xfer->flag = FWXF_BUSY;
434 		return EAGAIN;
435 	}
436 
437 	if (info->flag & FWTI_REQ)
438 		xferq = fc->atq;
439 	else
440 		xferq = fc->ats;
441 	len = info->hdr_len;
442 	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
443 		aprint_error_dev(fc->bdev, "send.pay_len > maxrec\n");
444 		return EINVAL;
445 	}
446 	if (info->flag & FWTI_BLOCK_STR)
447 		len = fp->mode.stream.len;
448 	else if (info->flag & FWTI_BLOCK_ASY)
449 		len = fp->mode.rresb.len;
450 	else
451 		len = 0;
452 	if (len != xfer->send.pay_len) {
453 		aprint_error_dev(fc->bdev,
454 		    "len(%d) != send.pay_len(%d) %s(%x)\n",
455 		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
456 		return EINVAL;
457 	}
458 
459 	if (xferq->start == NULL) {
460 		aprint_error_dev(fc->bdev, "xferq->start == NULL\n");
461 		return EINVAL;
462 	}
463 	if (!(xferq->queued < xferq->maxq)) {
464 		aprint_error_dev(fc->bdev, "Discard a packet (queued=%d)\n",
465 			xferq->queued);
466 		return EAGAIN;
467 	}
468 
469 	xfer->tl = -1;
470 	if (info->flag & FWTI_TLABEL)
471 		if (fw_get_tlabel(fc, xfer) < 0)
472 			return EAGAIN;
473 
474 	xfer->resp = 0;
475 	xfer->fc = fc;
476 	xfer->q = xferq;
477 
478 	fw_asystart(xfer);
479 	return 0;
480 }
481 
482 /*
483  * Wakeup blocked process.
484  */
485 void
486 fw_xferwake(struct fw_xfer *xfer)
487 {
488 
489 	mutex_enter(&xfer->fc->wait_lock);
490 	xfer->flag |= FWXF_WAKE;
491 	cv_signal(&xfer->cv);
492 	mutex_exit(&xfer->fc->wait_lock);
493 
494 	return;
495 }
496 
497 int
498 fw_xferwait(struct fw_xfer *xfer)
499 {
500 	struct firewire_comm *fc = xfer->fc;
501 	int err = 0;
502 
503 	mutex_enter(&fc->wait_lock);
504 	while (!(xfer->flag & FWXF_WAKE))
505 		err = cv_wait_sig(&xfer->cv, &fc->wait_lock);
506 	mutex_exit(&fc->wait_lock);
507 
508 	return err;
509 }
510 
511 void
512 fw_drain_txq(struct firewire_comm *fc)
513 {
514 	struct fw_xfer *xfer;
515 	STAILQ_HEAD(, fw_xfer) xfer_drain;
516 	int i;
517 
518 	STAILQ_INIT(&xfer_drain);
519 
520 	mutex_enter(&fc->atq->q_mtx);
521 	fw_xferq_drain(fc->atq);
522 	mutex_exit(&fc->atq->q_mtx);
523 	mutex_enter(&fc->ats->q_mtx);
524 	fw_xferq_drain(fc->ats);
525 	mutex_exit(&fc->ats->q_mtx);
526 	for (i = 0; i < fc->nisodma; i++)
527 		fw_xferq_drain(fc->it[i]);
528 
529 	mutex_enter(&fc->tlabel_lock);
530 	for (i = 0; i < 0x40; i++)
531 		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
532 			if (firewire_debug)
533 				printf("tl=%d flag=%d\n", i, xfer->flag);
534 			xfer->resp = EAGAIN;
535 			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
536 			STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel);
537 		}
538 	mutex_exit(&fc->tlabel_lock);
539 
540 	STAILQ_FOREACH(xfer, &xfer_drain, tlabel)
541 		xfer->hand(xfer);
542 }
543 
544 /*
545  * Called after bus reset.
546  */
547 void
548 fw_busreset(struct firewire_comm *fc, uint32_t new_status)
549 {
550 	struct firewire_softc *sc = device_private(fc->bdev);
551 	struct firewire_dev_list *devlist;
552 	struct firewire_dev_comm *fdc;
553 	struct crom_src *src;
554 	uint32_t *newrom;
555 
556 	if (fc->status == FWBUSMGRELECT)
557 		callout_stop(&fc->bmr_callout);
558 
559 	fc->status = new_status;
560 	fw_reset_csr(fc);
561 
562 	if (fc->status == FWBUSNOTREADY)
563 		fw_init_crom(fc);
564 
565 	fw_reset_crom(fc);
566 
567 	/* How many safe this access? */
568 	SLIST_FOREACH(devlist, &sc->devlist, link) {
569 		fdc = device_private(devlist->dev);
570 		if (fdc->post_busreset != NULL)
571 			fdc->post_busreset(fdc);
572 	}
573 
574 	/*
575 	 * If the old config rom needs to be overwritten,
576 	 * bump the businfo.generation indicator to
577 	 * indicate that we need to be reprobed
578 	 * See 1394a-2000 8.3.2.5.4 for more details.
579 	 * generation starts at 2 and rolls over at 0xF
580 	 * back to 2.
581 	 *
582 	 * A generation of 0 indicates a device
583 	 * that is not 1394a-2000 compliant.
584 	 * A generation of 1 indicates a device that
585 	 * does not change it's Bus Info Block or
586 	 * Configuration ROM.
587 	 */
588 #define FW_MAX_GENERATION	0xF
589 	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
590 	src = &fc->crom_src_buf->src;
591 	crom_load(src, newrom, CROMSIZE);
592 	if (memcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
593 		if (src->businfo.generation++ > FW_MAX_GENERATION)
594 			src->businfo.generation = FW_GENERATION_CHANGEABLE;
595 		memcpy((void *)fc->config_rom, newrom, CROMSIZE);
596 	}
597 	free(newrom, M_FW);
598 }
599 
600 /* Call once after reboot */
601 void
602 fw_init(struct firewire_comm *fc)
603 {
604 	int i;
605 
606 	fc->arq->queued = 0;
607 	fc->ars->queued = 0;
608 	fc->atq->queued = 0;
609 	fc->ats->queued = 0;
610 
611 	fc->arq->buf = NULL;
612 	fc->ars->buf = NULL;
613 	fc->atq->buf = NULL;
614 	fc->ats->buf = NULL;
615 
616 	fc->arq->flag = 0;
617 	fc->ars->flag = 0;
618 	fc->atq->flag = 0;
619 	fc->ats->flag = 0;
620 
621 	STAILQ_INIT(&fc->atq->q);
622 	STAILQ_INIT(&fc->ats->q);
623 	mutex_init(&fc->arq->q_mtx, MUTEX_DEFAULT, IPL_VM);
624 	mutex_init(&fc->ars->q_mtx, MUTEX_DEFAULT, IPL_VM);
625 	mutex_init(&fc->atq->q_mtx, MUTEX_DEFAULT, IPL_VM);
626 	mutex_init(&fc->ats->q_mtx, MUTEX_DEFAULT, IPL_VM);
627 
628 	for (i = 0; i < fc->nisodma; i++) {
629 		fc->it[i]->queued = 0;
630 		fc->ir[i]->queued = 0;
631 
632 		fc->it[i]->start = NULL;
633 		fc->ir[i]->start = NULL;
634 
635 		fc->it[i]->buf = NULL;
636 		fc->ir[i]->buf = NULL;
637 
638 		fc->it[i]->flag = FWXFERQ_STREAM;
639 		fc->ir[i]->flag = FWXFERQ_STREAM;
640 
641 		STAILQ_INIT(&fc->it[i]->q);
642 		STAILQ_INIT(&fc->ir[i]->q);
643 	}
644 
645 	fc->arq->maxq = FWMAXQUEUE;
646 	fc->ars->maxq = FWMAXQUEUE;
647 	fc->atq->maxq = FWMAXQUEUE;
648 	fc->ats->maxq = FWMAXQUEUE;
649 
650 	for (i = 0; i < fc->nisodma; i++) {
651 		fc->ir[i]->maxq = FWMAXQUEUE;
652 		fc->it[i]->maxq = FWMAXQUEUE;
653 	}
654 
655 	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
656 	CSRARC(fc, TOPO_MAP + 4) = 1;
657 	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
658 	CSRARC(fc, SPED_MAP + 4) = 1;
659 
660 	STAILQ_INIT(&fc->devices);
661 
662 /* Initialize Async handlers */
663 	STAILQ_INIT(&fc->binds);
664 	for (i = 0; i < 0x40; i++)
665 		STAILQ_INIT(&fc->tlabels[i]);
666 
667 /* DV depend CSRs see blue book */
668 #if 0
669 	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
670 	CSRARC(fc, oPCR) = 0x8000007a;
671 	for (i = 4; i < 0x7c/4; i+=4)
672 		CSRARC(fc, i + oPCR) = 0x8000007a;
673 
674 	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
675 	CSRARC(fc, iPCR) = 0x803f0000;
676 	for (i = 4; i < 0x7c/4; i+=4)
677 		CSRARC(fc, i + iPCR) = 0x0;
678 #endif
679 
680 	fc->crom_src_buf = NULL;
681 }
682 
683 #define BIND_CMP(addr, fwb) \
684 	(((addr) < (fwb)->start) ? -1 : ((fwb)->end < (addr)) ? 1 : 0)
685 
686 /*
687  * To lookup bound process from IEEE1394 address.
688  */
689 struct fw_bind *
690 fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
691 {
692 	u_int64_t addr;
693 	struct fw_bind *tfw, *r = NULL;
694 
695 	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
696 	mutex_enter(&fc->fc_mtx);
697 	STAILQ_FOREACH(tfw, &fc->binds, fclist)
698 		if (BIND_CMP(addr, tfw) == 0) {
699 			r = tfw;
700 			break;
701 		}
702 	mutex_exit(&fc->fc_mtx);
703 	return r;
704 }
705 
706 /*
707  * To bind IEEE1394 address block to process.
708  */
709 int
710 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
711 {
712 	struct fw_bind *tfw, *prev = NULL;
713 	int r = 0;
714 
715 	if (fwb->start > fwb->end) {
716 		aprint_error_dev(fc->bdev, "invalid range\n");
717 		return EINVAL;
718 	}
719 
720 	mutex_enter(&fc->fc_mtx);
721 	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
722 		if (fwb->end < tfw->start)
723 			break;
724 		prev = tfw;
725 	}
726 	if (prev == NULL)
727 		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
728 	else if (prev->end < fwb->start)
729 		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
730 	else {
731 		aprint_error_dev(fc->bdev, "bind failed\n");
732 		r = EBUSY;
733 	}
734 	mutex_exit(&fc->fc_mtx);
735 	return r;
736 }
737 
738 /*
739  * To free IEEE1394 address block.
740  */
741 int
742 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
743 {
744 #if 0
745 	struct fw_xfer *xfer, *next;
746 #endif
747 	struct fw_bind *tfw;
748 
749 	mutex_enter(&fc->fc_mtx);
750 	STAILQ_FOREACH(tfw, &fc->binds, fclist)
751 		if (tfw == fwb) {
752 			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
753 			mutex_exit(&fc->fc_mtx);
754 			goto found;
755 		}
756 
757 	mutex_exit(&fc->fc_mtx);
758 	aprint_error_dev(fc->bdev, "no such binding\n");
759 	return 1;
760 found:
761 #if 0
762 	/* shall we do this? */
763 	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
764 		next = STAILQ_NEXT(xfer, link);
765 		fw_xfer_free(xfer);
766 	}
767 	STAILQ_INIT(&fwb->xferlist);
768 #endif
769 
770 	return 0;
771 }
772 
773 int
774 fw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type, int slen,
775 		int rlen, int n, struct firewire_comm *fc, void *sc,
776 		void (*hand)(struct fw_xfer *))
777 {
778 	struct fw_xfer *xfer;
779 	int i;
780 
781 	for (i = 0; i < n; i++) {
782 		xfer = fw_xfer_alloc_buf(type, slen, rlen);
783 		if (xfer == NULL)
784 			return n;
785 		xfer->fc = fc;
786 		xfer->sc = sc;
787 		xfer->hand = hand;
788 		STAILQ_INSERT_TAIL(q, xfer, link);
789 	}
790 	return n;
791 }
792 
793 void
794 fw_xferlist_remove(struct fw_xferlist *q)
795 {
796 	struct fw_xfer *xfer, *next;
797 
798 	for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
799 		next = STAILQ_NEXT(xfer, link);
800 		fw_xfer_free_buf(xfer);
801 	}
802 	STAILQ_INIT(q);
803 }
804 
805 /*
806  * To allocate IEEE1394 XFER structure.
807  */
808 struct fw_xfer *
809 fw_xfer_alloc(struct malloc_type *type)
810 {
811 	struct fw_xfer *xfer;
812 
813 	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
814 	if (xfer == NULL)
815 		return xfer;
816 
817 	xfer->malloc = type;
818 	cv_init(&xfer->cv, "fwxfer");
819 
820 	return xfer;
821 }
822 
823 struct fw_xfer *
824 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
825 {
826 	struct fw_xfer *xfer;
827 
828 	xfer = fw_xfer_alloc(type);
829 	if (xfer == NULL)
830 		return NULL;
831 	xfer->send.pay_len = send_len;
832 	xfer->recv.pay_len = recv_len;
833 	if (send_len > 0) {
834 		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
835 		if (xfer->send.payload == NULL) {
836 			fw_xfer_free(xfer);
837 			return NULL;
838 		}
839 	}
840 	if (recv_len > 0) {
841 		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
842 		if (xfer->recv.payload == NULL) {
843 			if (xfer->send.payload != NULL)
844 				free(xfer->send.payload, type);
845 			fw_xfer_free(xfer);
846 			return NULL;
847 		}
848 	}
849 	return xfer;
850 }
851 
852 /*
853  * IEEE1394 XFER post process.
854  */
855 void
856 fw_xfer_done(struct fw_xfer *xfer)
857 {
858 
859 	if (xfer->hand == NULL) {
860 		aprint_error_dev(xfer->fc->bdev, "hand == NULL\n");
861 		return;
862 	}
863 
864 	if (xfer->fc == NULL)
865 		panic("fw_xfer_done: why xfer->fc is NULL?");
866 
867 	fw_tl_free(xfer->fc, xfer);
868 	xfer->hand(xfer);
869 }
870 
871 void
872 fw_xfer_unload(struct fw_xfer* xfer)
873 {
874 
875 	if (xfer == NULL)
876 		return;
877 	if (xfer->flag & FWXF_INQ) {
878 		aprint_error_dev(xfer->fc->bdev, "fw_xfer_free FWXF_INQ\n");
879 		mutex_enter(&xfer->q->q_mtx);
880 		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
881 #if 0
882 		xfer->q->queued--;
883 #endif
884 		mutex_exit(&xfer->q->q_mtx);
885 	}
886 	if (xfer->fc != NULL) {
887 #if 1
888 		if (xfer->flag == FWXF_START)
889 			/*
890 			 * This could happen if:
891 			 *  1. We call fwohci_arcv() before fwohci_txd().
892 			 *  2. firewire_watch() is called.
893 			 */
894 			aprint_error_dev(xfer->fc->bdev,
895 			    "fw_xfer_free FWXF_START\n");
896 #endif
897 	}
898 	xfer->flag = FWXF_INIT;
899 	xfer->resp = 0;
900 }
901 
902 /*
903  * To free IEEE1394 XFER structure.
904  */
905 void
906 fw_xfer_free(struct fw_xfer* xfer)
907 {
908 
909 	if (xfer == NULL) {
910 		aprint_error("fw_xfer_free: xfer == NULL\n");
911 		return;
912 	}
913 	fw_xfer_unload(xfer);
914 	cv_destroy(&xfer->cv);
915 	free(xfer, xfer->malloc);
916 }
917 
918 void
919 fw_xfer_free_buf(struct fw_xfer* xfer)
920 {
921 
922 	if (xfer == NULL) {
923 		aprint_error("fw_xfer_free_buf: xfer == NULL\n");
924 		return;
925 	}
926 	fw_xfer_unload(xfer);
927 	if (xfer->send.payload != NULL) {
928 		free(xfer->send.payload, xfer->malloc);
929 	}
930 	if (xfer->recv.payload != NULL) {
931 		free(xfer->recv.payload, xfer->malloc);
932 	}
933 	cv_destroy(&xfer->cv);
934 	free(xfer, xfer->malloc);
935 }
936 
937 void
938 fw_asy_callback_free(struct fw_xfer *xfer)
939 {
940 
941 #if 0
942 	printf("asyreq done flag=%d resp=%d\n", xfer->flag, xfer->resp);
943 #endif
944 	fw_xfer_free(xfer);
945 }
946 
947 /*
948  * To receive self ID.
949  */
950 void
951 fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
952 {
953 	uint32_t *p;
954 	union fw_self_id *self_id;
955 	u_int i, j, node, c_port = 0, i_branch = 0;
956 
957 	fc->sid_cnt = len / (sizeof(uint32_t) * 2);
958 	fc->max_node = fc->nodeid & 0x3f;
959 	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
960 	fc->status = FWBUSCYMELECT;
961 	fc->topology_map->crc_len = 2;
962 	fc->topology_map->generation++;
963 	fc->topology_map->self_id_count = 0;
964 	fc->topology_map->node_count = 0;
965 	fc->speed_map->generation++;
966 	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
967 	self_id = fc->topology_map->self_id;
968 	for (i = 0; i < fc->sid_cnt; i++) {
969 		if (sid[1] != ~sid[0]) {
970 			aprint_error_dev(fc->bdev,
971 			    "ERROR invalid self-id packet\n");
972 			sid += 2;
973 			continue;
974 		}
975 		*self_id = *((union fw_self_id *)sid);
976 		fc->topology_map->crc_len++;
977 		if (self_id->p0.sequel == 0) {
978 			fc->topology_map->node_count++;
979 			c_port = 0;
980 			if (firewire_debug)
981 				fw_print_sid(sid[0]);
982 			node = self_id->p0.phy_id;
983 			if (fc->max_node < node)
984 				fc->max_node = self_id->p0.phy_id;
985 			/* XXX I'm not sure this is the right speed_map */
986 			fc->speed_map->speed[node][node] =
987 			    self_id->p0.phy_speed;
988 			for (j = 0; j < node; j++)
989 				fc->speed_map->speed[j][node] =
990 				    fc->speed_map->speed[node][j] =
991 				    min(fc->speed_map->speed[j][j],
992 							self_id->p0.phy_speed);
993 			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
994 			    (self_id->p0.link_active && self_id->p0.contender))
995 				fc->irm = self_id->p0.phy_id;
996 			if (self_id->p0.port0 >= 0x2)
997 				c_port++;
998 			if (self_id->p0.port1 >= 0x2)
999 				c_port++;
1000 			if (self_id->p0.port2 >= 0x2)
1001 				c_port++;
1002 		}
1003 		if (c_port > 2)
1004 			i_branch += (c_port - 2);
1005 		sid += 2;
1006 		self_id++;
1007 		fc->topology_map->self_id_count++;
1008 	}
1009 	/* CRC */
1010 	fc->topology_map->crc =
1011 	    fw_crc16((uint32_t *)&fc->topology_map->generation,
1012 						fc->topology_map->crc_len * 4);
1013 	fc->speed_map->crc = fw_crc16((uint32_t *)&fc->speed_map->generation,
1014 	    fc->speed_map->crc_len * 4);
1015 	/* byteswap and copy to CSR */
1016 	p = (uint32_t *)fc->topology_map;
1017 	for (i = 0; i <= fc->topology_map->crc_len; i++)
1018 		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1019 	p = (uint32_t *)fc->speed_map;
1020 	CSRARC(fc, SPED_MAP) = htonl(*p++);
1021 	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1022 	/* don't byte-swap uint8_t array */
1023 	memcpy(&CSRARC(fc, SPED_MAP + 8), p, (fc->speed_map->crc_len - 1) * 4);
1024 
1025 	fc->max_hop = fc->max_node - i_branch;
1026 	aprint_normal_dev(fc->bdev, "%d nodes, maxhop <= %d %s irm(%d)%s\n",
1027 	    fc->max_node + 1, fc->max_hop,
1028 	    (fc->irm == -1) ? "Not IRM capable" : "cable IRM",
1029 	    fc->irm,
1030 	    (fc->irm == fc->nodeid) ? " (me)" : "");
1031 
1032 	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1033 		if (fc->irm == fc->nodeid) {
1034 			fc->status = FWBUSMGRDONE;
1035 			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1036 			fw_bmr(fc);
1037 		} else {
1038 			fc->status = FWBUSMGRELECT;
1039 			callout_schedule(&fc->bmr_callout, hz/8);
1040 		}
1041 	} else
1042 		fc->status = FWBUSMGRDONE;
1043 
1044 	callout_schedule(&fc->busprobe_callout, hz/4);
1045 }
1046 
1047 /*
1048  * Generic packet receiving process.
1049  */
1050 void
1051 fw_rcv(struct fw_rcv_buf *rb)
1052 {
1053 	struct fw_pkt *fp, *resfp;
1054 	struct fw_bind *bind;
1055 	int tcode;
1056 	int i, len, oldstate;
1057 #if 0
1058 	{
1059 		uint32_t *qld;
1060 		int i;
1061 		qld = (uint32_t *)buf;
1062 		printf("spd %d len:%d\n", spd, len);
1063 		for (i = 0; i <= len && i < 32; i+= 4) {
1064 			printf("0x%08x ", ntohl(qld[i/4]));
1065 			if ((i % 16) == 15) printf("\n");
1066 		}
1067 		if ((i % 16) != 15) printf("\n");
1068 	}
1069 #endif
1070 	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1071 	tcode = fp->mode.common.tcode;
1072 	switch (tcode) {
1073 	case FWTCODE_WRES:
1074 	case FWTCODE_RRESQ:
1075 	case FWTCODE_RRESB:
1076 	case FWTCODE_LRES:
1077 		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1078 		    fp->mode.hdr.tlrt >> 2, tcode);
1079 		if (rb->xfer == NULL) {
1080 			aprint_error_dev(rb->fc->bdev, "unknown response"
1081 			    " %s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1082 			    tcode_str[tcode], tcode,
1083 			    fp->mode.hdr.src,
1084 			    fp->mode.hdr.tlrt >> 2,
1085 			    fp->mode.hdr.tlrt & 3,
1086 			    fp->mode.rresq.data);
1087 #if 0
1088 			printf("try ad-hoc work around!!\n");
1089 			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1090 			    (fp->mode.hdr.tlrt >> 2) ^ 3);
1091 			if (rb->xfer == NULL) {
1092 				printf("no use...\n");
1093 				return;
1094 			}
1095 #else
1096 			return;
1097 #endif
1098 		}
1099 		fw_rcv_copy(rb);
1100 		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1101 			rb->xfer->resp = EIO;
1102 		else
1103 			rb->xfer->resp = 0;
1104 		/* make sure the packet is drained in AT queue */
1105 		oldstate = rb->xfer->flag;
1106 		rb->xfer->flag = FWXF_RCVD;
1107 		switch (oldstate) {
1108 		case FWXF_SENT:
1109 			fw_xfer_done(rb->xfer);
1110 			break;
1111 		case FWXF_START:
1112 #if 0
1113 			if (firewire_debug)
1114 				printf("not sent yet tl=%x\n", rb->xfer->tl);
1115 #endif
1116 			break;
1117 		default:
1118 			aprint_error_dev(rb->fc->bdev,
1119 			    "unexpected flag 0x%02x\n", rb->xfer->flag);
1120 		}
1121 		return;
1122 	case FWTCODE_WREQQ:
1123 	case FWTCODE_WREQB:
1124 	case FWTCODE_RREQQ:
1125 	case FWTCODE_RREQB:
1126 	case FWTCODE_LREQ:
1127 		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1128 		    fp->mode.rreqq.dest_lo);
1129 		if (bind == NULL) {
1130 #if 1
1131 			aprint_error_dev(rb->fc->bdev, "Unknown service addr"
1132 			    " 0x%04x:0x%08x %s(%x) src=0x%x data=%x\n",
1133 			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1134 			    tcode_str[tcode], tcode,
1135 			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1136 #endif
1137 			if (rb->fc->status == FWBUSINIT) {
1138 				aprint_error_dev(rb->fc->bdev,
1139 				    "cannot respond(bus reset)!\n");
1140 				return;
1141 			}
1142 			rb->xfer = fw_xfer_alloc(M_FWXFER);
1143 			if (rb->xfer == NULL)
1144 				return;
1145 			rb->xfer->send.spd = rb->spd;
1146 			rb->xfer->send.pay_len = 0;
1147 			resfp = &rb->xfer->send.hdr;
1148 			switch (tcode) {
1149 			case FWTCODE_WREQQ:
1150 			case FWTCODE_WREQB:
1151 				resfp->mode.hdr.tcode = FWTCODE_WRES;
1152 				break;
1153 			case FWTCODE_RREQQ:
1154 				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1155 				break;
1156 			case FWTCODE_RREQB:
1157 				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1158 				break;
1159 			case FWTCODE_LREQ:
1160 				resfp->mode.hdr.tcode = FWTCODE_LRES;
1161 				break;
1162 			}
1163 			resfp->mode.hdr.dst = fp->mode.hdr.src;
1164 			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1165 			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1166 			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1167 			resfp->mode.rresb.extcode = 0;
1168 			resfp->mode.rresb.len = 0;
1169 /*
1170 			rb->xfer->hand = fw_xferwake;
1171 */
1172 			rb->xfer->hand = fw_xfer_free;
1173 			if (fw_asyreq(rb->fc, -1, rb->xfer)) {
1174 				fw_xfer_free(rb->xfer);
1175 				return;
1176 			}
1177 			return;
1178 		}
1179 		len = 0;
1180 		for (i = 0; i < rb->nvec; i++)
1181 			len += rb->vec[i].iov_len;
1182 		mutex_enter(&bind->fwb_mtx);
1183 		rb->xfer = STAILQ_FIRST(&bind->xferlist);
1184 		if (rb->xfer == NULL) {
1185 			mutex_exit(&bind->fwb_mtx);
1186 #if 1
1187 			aprint_error_dev(rb->fc->bdev,
1188 			    "Discard a packet for this bind.\n");
1189 #endif
1190 			return;
1191 		}
1192 		STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1193 		mutex_exit(&bind->fwb_mtx);
1194 		fw_rcv_copy(rb);
1195 		rb->xfer->hand(rb->xfer);
1196 		return;
1197 
1198 	default:
1199 		aprint_error_dev(rb->fc->bdev, "unknow tcode %d\n", tcode);
1200 		break;
1201 	}
1202 }
1203 
1204 /*
1205  * CRC16 check-sum for IEEE1394 register blocks.
1206  */
1207 uint16_t
1208 fw_crc16(uint32_t *ptr, uint32_t len)
1209 {
1210 	uint32_t i, sum, crc = 0;
1211 	int shift;
1212 
1213 	len = (len + 3) & ~3;
1214 	for (i = 0; i < len; i+= 4) {
1215 		for (shift = 28; shift >= 0; shift -= 4) {
1216 			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
1217 			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
1218 		}
1219 		crc &= 0xffff;
1220 	}
1221 	return (uint16_t)crc;
1222 }
1223 
1224 int
1225 fw_open_isodma(struct firewire_comm *fc, int tx)
1226 {
1227 	struct fw_xferq **xferqa;
1228 	struct fw_xferq *xferq;
1229 	int i;
1230 
1231 	if (tx)
1232 		xferqa = fc->it;
1233 	else
1234 		xferqa = fc->ir;
1235 
1236 	mutex_enter(&fc->fc_mtx);
1237 	for (i = 0; i < fc->nisodma; i++) {
1238 		xferq = xferqa[i];
1239 		if (!(xferq->flag & FWXFERQ_OPEN)) {
1240 			xferq->flag |= FWXFERQ_OPEN;
1241 			break;
1242 		}
1243 	}
1244 	if (i == fc->nisodma) {
1245 		aprint_error_dev(fc->bdev, "no free dma channel (tx=%d)\n", tx);
1246 		i = -1;
1247 	}
1248 	mutex_exit(&fc->fc_mtx);
1249 	return i;
1250 }
1251 
1252 /*
1253  * Async. request with given xfer structure.
1254  */
1255 static void
1256 fw_asystart(struct fw_xfer *xfer)
1257 {
1258 	struct firewire_comm *fc = xfer->fc;
1259 
1260 	/* Protect from interrupt/timeout */
1261 	mutex_enter(&xfer->q->q_mtx);
1262 	xfer->flag = FWXF_INQ;
1263 	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
1264 #if 0
1265 	xfer->q->queued++;
1266 #endif
1267 	mutex_exit(&xfer->q->q_mtx);
1268 	/* XXX just queue for mbuf */
1269 	if (xfer->mbuf == NULL)
1270 		xfer->q->start(fc);
1271 	return;
1272 }
1273 
1274 static void
1275 firewire_xfer_timeout(struct firewire_comm *fc)
1276 {
1277 	struct fw_xfer *xfer;
1278 	struct timeval tv;
1279 	struct timeval split_timeout;
1280 	STAILQ_HEAD(, fw_xfer) xfer_timeout;
1281 	int i;
1282 
1283 	split_timeout.tv_sec = 0;
1284 	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
1285 
1286 	microtime(&tv);
1287 	timersub(&tv, &split_timeout, &tv);
1288 	STAILQ_INIT(&xfer_timeout);
1289 
1290 	mutex_enter(&fc->tlabel_lock);
1291 	for (i = 0; i < 0x40; i++) {
1292 		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
1293 			if ((xfer->flag & FWXF_SENT) == 0)
1294 				/* not sent yet */
1295 				break;
1296 			if (timercmp(&xfer->tv, &tv, >))
1297 				/* the rests are newer than this */
1298 				break;
1299 			aprint_error_dev(fc->bdev,
1300 			    "split transaction timeout: tl=0x%x flag=0x%02x\n",
1301 			    i, xfer->flag);
1302 			fw_dump_hdr(&xfer->send.hdr, "send");
1303 			xfer->resp = ETIMEDOUT;
1304 			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
1305 			STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel);
1306 		}
1307 	}
1308 	mutex_exit(&fc->tlabel_lock);
1309 	fc->timeout(fc);
1310 
1311 	STAILQ_FOREACH(xfer, &xfer_timeout, tlabel)
1312 	    xfer->hand(xfer);
1313 }
1314 
1315 #define WATCHDOG_HZ 10
1316 static void
1317 firewire_watchdog(void *arg)
1318 {
1319 	struct firewire_comm *fc;
1320 	static int watchdog_clock = 0;
1321 
1322 	fc = (struct firewire_comm *)arg;
1323 
1324 	/*
1325 	 * At boot stage, the device interrupt is disabled and
1326 	 * We encounter a timeout easily. To avoid this,
1327 	 * ignore clock interrupt for a while.
1328 	 */
1329 	if (watchdog_clock > WATCHDOG_HZ * 15)
1330 		firewire_xfer_timeout(fc);
1331 	else
1332 		watchdog_clock++;
1333 
1334 	callout_schedule(&fc->timeout_callout, hz / WATCHDOG_HZ);
1335 }
1336 
1337 static void
1338 fw_xferq_drain(struct fw_xferq *xferq)
1339 {
1340 	struct fw_xfer *xfer;
1341 
1342 	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
1343 		STAILQ_REMOVE_HEAD(&xferq->q, link);
1344 #if 0
1345 		xferq->queued--;
1346 #endif
1347 		xfer->resp = EAGAIN;
1348 		xfer->flag = FWXF_SENTERR;
1349 		fw_xfer_done(xfer);
1350 	}
1351 }
1352 
1353 static void
1354 fw_reset_csr(struct firewire_comm *fc)
1355 {
1356 	int i;
1357 
1358 	CSRARC(fc, STATE_CLEAR) =
1359 	    1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14;
1360 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1361 	CSRARC(fc, NODE_IDS) = 0x3f;
1362 
1363 	CSRARC(fc, TOPO_MAP + 8) = 0;
1364 	fc->irm = -1;
1365 
1366 	fc->max_node = -1;
1367 
1368 	for (i = 2; i < 0x100/4 - 2; i++)
1369 		CSRARC(fc, SPED_MAP + i * 4) = 0;
1370 	CSRARC(fc, STATE_CLEAR) =
1371 	    1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14;
1372 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1373 	CSRARC(fc, RESET_START) = 0;
1374 	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
1375 	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
1376 	CSRARC(fc, CYCLE_TIME) = 0x0;
1377 	CSRARC(fc, BUS_TIME) = 0x0;
1378 	CSRARC(fc, BUS_MGR_ID) = 0x3f;
1379 	CSRARC(fc, BANDWIDTH_AV) = 4915;
1380 	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
1381 	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
1382 	CSRARC(fc, IP_CHANNELS) = (1 << 31);
1383 
1384 	CSRARC(fc, CONF_ROM) = 0x04 << 24;
1385 	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
1386 	CSRARC(fc, CONF_ROM + 8) =
1387 	    1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 | 0xff << 16 | 0x09 << 8;
1388 	CSRARC(fc, CONF_ROM + 0xc) = 0;
1389 
1390 /* DV depend CSRs see blue book */
1391 	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
1392 	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
1393 
1394 	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14);
1395 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1396 }
1397 
1398 static void
1399 fw_init_crom(struct firewire_comm *fc)
1400 {
1401 	struct crom_src *src;
1402 
1403 	src = &fc->crom_src_buf->src;
1404 	memset(src, 0, sizeof(struct crom_src));
1405 
1406 	/* BUS info sample */
1407 	src->hdr.info_len = 4;
1408 
1409 	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
1410 
1411 	src->businfo.irmc = 1;
1412 	src->businfo.cmc = 1;
1413 	src->businfo.isc = 1;
1414 	src->businfo.bmc = 1;
1415 	src->businfo.pmc = 0;
1416 	src->businfo.cyc_clk_acc = 100;
1417 	src->businfo.max_rec = fc->maxrec;
1418 	src->businfo.max_rom = MAXROM_4;
1419 	src->businfo.generation = FW_GENERATION_CHANGEABLE;
1420 	src->businfo.link_spd = fc->speed;
1421 
1422 	src->businfo.eui64.hi = fc->eui.hi;
1423 	src->businfo.eui64.lo = fc->eui.lo;
1424 
1425 	STAILQ_INIT(&src->chunk_list);
1426 
1427 	fc->crom_src = src;
1428 	fc->crom_root = &fc->crom_src_buf->root;
1429 }
1430 
1431 static void
1432 fw_reset_crom(struct firewire_comm *fc)
1433 {
1434 	struct crom_src_buf *buf;
1435 	struct crom_src *src;
1436 	struct crom_chunk *root;
1437 
1438 	buf = fc->crom_src_buf;
1439 	src = fc->crom_src;
1440 	root = fc->crom_root;
1441 
1442 	STAILQ_INIT(&src->chunk_list);
1443 
1444 	memset(root, 0, sizeof(struct crom_chunk));
1445 	crom_add_chunk(src, NULL, root, 0);
1446 	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
1447 	/* private company_id */
1448 	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
1449 	crom_add_simple_text(src, root, &buf->vendor, PROJECT_STR);
1450 	crom_add_entry(root, CSRKEY_HW, __NetBSD_Version__);
1451 	crom_add_simple_text(src, root, &buf->hw, hostname);
1452 }
1453 
1454 /*
1455  * dump packet header
1456  */
1457 static void
1458 fw_dump_hdr(struct fw_pkt *fp, const char *prefix)
1459 {
1460 
1461 	printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
1462 	    "src=0x%03x\n", prefix,
1463 	     fp->mode.hdr.dst & 0x3f,
1464 	     fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3,
1465 	     fp->mode.hdr.tcode, fp->mode.hdr.pri,
1466 	     fp->mode.hdr.src);
1467 }
1468 
1469 /*
1470  * To free transaction label.
1471  */
1472 static void
1473 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
1474 {
1475 	struct fw_xfer *txfer;
1476 
1477 	if (xfer->tl < 0)
1478 		return;
1479 
1480 	mutex_enter(&fc->tlabel_lock);
1481 #if 1 /* make sure the label is allocated */
1482 	STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
1483 		if (txfer == xfer)
1484 			break;
1485 	if (txfer == NULL) {
1486 		mutex_exit(&fc->tlabel_lock);
1487 		aprint_error_dev(fc->bdev,
1488 		    "the xfer is not in the queue (tlabel=%d, flag=0x%x)\n",
1489 		    xfer->tl, xfer->flag);
1490 		fw_dump_hdr(&xfer->send.hdr, "send");
1491 		fw_dump_hdr(&xfer->recv.hdr, "recv");
1492 		KASSERT(FALSE);
1493 		return;
1494 	}
1495 #endif
1496 
1497 	STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
1498 	mutex_exit(&fc->tlabel_lock);
1499 	return;
1500 }
1501 
1502 /*
1503  * To obtain XFER structure by transaction label.
1504  */
1505 static struct fw_xfer *
1506 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode)
1507 {
1508 	struct fw_xfer *xfer;
1509 	int req;
1510 
1511 	mutex_enter(&fc->tlabel_lock);
1512 	STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
1513 		if (xfer->send.hdr.mode.hdr.dst == node) {
1514 			mutex_exit(&fc->tlabel_lock);
1515 			KASSERT(xfer->tl == tlabel);
1516 			/* extra sanity check */
1517 			req = xfer->send.hdr.mode.hdr.tcode;
1518 			if (xfer->fc->tcode[req].valid_res != tcode) {
1519 				aprint_error_dev(fc->bdev,
1520 				    "invalid response tcode (0x%x for 0x%x)\n",
1521 				    tcode, req);
1522 				return NULL;
1523 			}
1524 
1525 			if (firewire_debug > 2)
1526 				printf("fw_tl2xfer: found tl=%d\n", tlabel);
1527 			return xfer;
1528 		}
1529 	mutex_exit(&fc->tlabel_lock);
1530 	if (firewire_debug > 1)
1531 		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
1532 	return NULL;
1533 }
1534 
1535 /*
1536  * To configure PHY.
1537  */
1538 static void
1539 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1540 {
1541 	struct fw_xfer *xfer;
1542 	struct fw_pkt *fp;
1543 
1544 	fc->status = FWBUSPHYCONF;
1545 
1546 	xfer = fw_xfer_alloc(M_FWXFER);
1547 	if (xfer == NULL)
1548 		return;
1549 	xfer->fc = fc;
1550 	xfer->hand = fw_asy_callback_free;
1551 
1552 	fp = &xfer->send.hdr;
1553 	fp->mode.ld[1] = 0;
1554 	if (root_node >= 0)
1555 		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1556 	if (gap_count >= 0)
1557 		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1558 	fp->mode.ld[2] = ~fp->mode.ld[1];
1559 /* XXX Dangerous, how to pass PHY packet to device driver */
1560 	fp->mode.common.tcode |= FWTCODE_PHY;
1561 
1562 	if (firewire_debug)
1563 		printf("root_node=%d gap_count=%d\n", root_node, gap_count);
1564 	fw_asyreq(fc, -1, xfer);
1565 }
1566 
1567 /*
1568  * Dump self ID.
1569  */
1570 static void
1571 fw_print_sid(uint32_t sid)
1572 {
1573 	union fw_self_id *s;
1574 
1575 	s = (union fw_self_id *) &sid;
1576 	if (s->p0.sequel) {
1577 		if (s->p1.sequence_num == FW_SELF_ID_PAGE0)
1578 			printf("node:%d p3:%d p4:%d p5:%d p6:%d p7:%d"
1579 			    "p8:%d p9:%d p10:%d\n",
1580 			    s->p1.phy_id, s->p1.port3, s->p1.port4,
1581 			    s->p1.port5, s->p1.port6, s->p1.port7,
1582 			    s->p1.port8, s->p1.port9, s->p1.port10);
1583 		else if (s->p2.sequence_num == FW_SELF_ID_PAGE1)
1584 			printf("node:%d p11:%d p12:%d p13:%d p14:%d p15:%d\n",
1585 			    s->p2.phy_id, s->p2.port11, s->p2.port12,
1586 			    s->p2.port13, s->p2.port14, s->p2.port15);
1587 		else
1588 			printf("node:%d Unknown Self ID Page number %d\n",
1589 			    s->p1.phy_id, s->p1.sequence_num);
1590 	} else
1591 		printf("node:%d link:%d gap:%d spd:%d con:%d pwr:%d"
1592 		    " p0:%d p1:%d p2:%d i:%d m:%d\n",
1593 		    s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1594 		    s->p0.phy_speed, s->p0.contender,
1595 		    s->p0.power_class, s->p0.port0, s->p0.port1,
1596 		    s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1597 }
1598 
1599 /*
1600  * To probe devices on the IEEE1394 bus.
1601  */
1602 static void
1603 fw_bus_probe(struct firewire_comm *fc)
1604 {
1605 	struct fw_device *fwdev;
1606 
1607 	mutex_enter(&fc->wait_lock);
1608 	fc->status = FWBUSEXPLORE;
1609 
1610 	/* Invalidate all devices, just after bus reset. */
1611 	if (firewire_debug)
1612 		printf("iterate and invalidate all nodes\n");
1613 	mutex_enter(&fc->fc_mtx);
1614 	STAILQ_FOREACH(fwdev, &fc->devices, link)
1615 		if (fwdev->status != FWDEVINVAL) {
1616 			fwdev->status = FWDEVINVAL;
1617 			fwdev->rcnt = 0;
1618 			if (firewire_debug)
1619 				printf("Invalidate Dev ID: %08x%08x\n",
1620 				    fwdev->eui.hi, fwdev->eui.lo);
1621 		} else
1622 			if (firewire_debug)
1623 				printf("Dev ID: %08x%08x already invalid\n",
1624 				    fwdev->eui.hi, fwdev->eui.lo);
1625 	mutex_exit(&fc->fc_mtx);
1626 
1627 	cv_signal(&fc->fc_cv);
1628 	mutex_exit(&fc->wait_lock);
1629 }
1630 
1631 static int
1632 fw_explore_read_quads(struct fw_device *fwdev, int offset, uint32_t *quad,
1633 		      int length)
1634 {
1635 	struct fw_xfer *xfer;
1636 	uint32_t tmp;
1637 	int i, error;
1638 
1639 	for (i = 0; i < length; i++, offset += sizeof(uint32_t)) {
1640 		xfer = fwmem_read_quad(fwdev, NULL, -1, 0xffff,
1641 		    0xf0000000 | offset, (void *)&tmp, fw_xferwake);
1642 		if (xfer == NULL)
1643 			return -1;
1644 		fw_xferwait(xfer);
1645 
1646 		if (xfer->resp == 0)
1647 			quad[i] = ntohl(tmp);
1648 
1649 		error = xfer->resp;
1650 		fw_xfer_free(xfer);
1651 		if (error)
1652 			return error;
1653 	}
1654 	return 0;
1655 }
1656 
1657 
1658 static int
1659 fw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
1660 {
1661 	int err, i, off;
1662 	struct csrdirectory *dir;
1663 	struct csrreg *reg;
1664 
1665 
1666 	dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1667 	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, (uint32_t *)dir,
1668 	    1);
1669 	if (err)
1670 		return -1;
1671 
1672 	offset += sizeof(uint32_t);
1673 	reg = (struct csrreg *)&fwdev->csrrom[offset / sizeof(uint32_t)];
1674 	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, (uint32_t *)reg,
1675 	    dir->crc_len);
1676 	if (err)
1677 		return -1;
1678 
1679 	/* XXX check CRC */
1680 
1681 	off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
1682 	if (fwdev->rommax < off)
1683 		fwdev->rommax = off;
1684 
1685 	if (recur == 0)
1686 		return 0;
1687 
1688 	for (i = 0; i < dir->crc_len; i++, offset += sizeof(uint32_t)) {
1689 		if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D)
1690 			recur = 1;
1691 		else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L)
1692 			recur = 0;
1693 		else
1694 			continue;
1695 
1696 		off = offset + reg[i].val * sizeof(uint32_t);
1697 		if (off > CROMSIZE) {
1698 			aprint_error_dev(fwdev->fc->bdev, "invalid offset %d\n",
1699 			    off);
1700 			return -1;
1701 		}
1702 		err = fw_explore_csrblock(fwdev, off, recur);
1703 		if (err)
1704 			return -1;
1705 	}
1706 	return 0;
1707 }
1708 
1709 static int
1710 fw_explore_node(struct fw_device *dfwdev)
1711 {
1712 	struct firewire_comm *fc;
1713 	struct fw_device *fwdev, *pfwdev, *tfwdev;
1714 	struct csrhdr *hdr;
1715 	struct bus_info *binfo;
1716 	uint32_t *csr, speed_test = 0;
1717 	int err, node;
1718 
1719 	fc = dfwdev->fc;
1720 	csr = dfwdev->csrrom;
1721 	node = dfwdev->dst;
1722 
1723 	/* First quad */
1724 	err = fw_explore_read_quads(dfwdev, CSRROMOFF, csr, 1);
1725 	if (err) {
1726 		aprint_error_dev(fc->bdev,
1727 		    "node%d: explore_read_quads failure\n", node);
1728 		dfwdev->status = FWDEVINVAL;
1729 		return -1;
1730 	}
1731 	hdr = (struct csrhdr *)csr;
1732 	if (hdr->info_len != 4) {
1733 		if (firewire_debug)
1734 			printf("node%d: wrong bus info len(%d)\n",
1735 			    node, hdr->info_len);
1736 		dfwdev->status = FWDEVINVAL;
1737 		return -1;
1738 	}
1739 
1740 	/* bus info */
1741 	err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
1742 	if (err) {
1743 		aprint_error_dev(fc->bdev, "node%d: error reading 0x04\n",
1744 		    node);
1745 		dfwdev->status = FWDEVINVAL;
1746 		return -1;
1747 	}
1748 	binfo = (struct bus_info *)&csr[1];
1749 	if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
1750 		aprint_error_dev(fc->bdev, "node%d: invalid bus name 0x%08x\n",
1751 		    node, binfo->bus_name);
1752 		dfwdev->status = FWDEVINVAL;
1753 		return -1;
1754 	}
1755 	if (firewire_debug)
1756 		printf("node(%d) BUS INFO BLOCK:\n"
1757 		    "irmc(%d) cmc(%d) isc(%d) bmc(%d) pmc(%d) "
1758 		    "cyc_clk_acc(%d) max_rec(%d) max_rom(%d) "
1759 		    "generation(%d) link_spd(%d)\n",
1760 		    node, binfo->irmc, binfo->cmc, binfo->isc,
1761 		    binfo->bmc, binfo->pmc, binfo->cyc_clk_acc,
1762 		    binfo->max_rec, binfo->max_rom,
1763 		    binfo->generation, binfo->link_spd);
1764 
1765 	mutex_enter(&fc->fc_mtx);
1766 	STAILQ_FOREACH(fwdev, &fc->devices, link)
1767 		if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
1768 			break;
1769 	mutex_exit(&fc->fc_mtx);
1770 	if (fwdev == NULL) {
1771 		/* new device */
1772 		fwdev =
1773 		    malloc(sizeof(struct fw_device), M_FW, M_NOWAIT | M_ZERO);
1774 		if (fwdev == NULL) {
1775 			if (firewire_debug)
1776 				printf("node%d: no memory\n", node);
1777 			return -1;
1778 		}
1779 		fwdev->fc = fc;
1780 		fwdev->eui = binfo->eui64;
1781 		fwdev->dst = dfwdev->dst;
1782 		fwdev->maxrec = dfwdev->maxrec;
1783 		fwdev->status = FWDEVNEW;
1784 		/*
1785 		 * Pre-1394a-2000 didn't have link_spd in
1786 		 * the Bus Info block, so try and use the
1787 		 * speed map value.
1788 		 * 1394a-2000 compliant devices only use
1789 		 * the Bus Info Block link spd value, so
1790 		 * ignore the speed map alltogether. SWB
1791 		 */
1792 		if (binfo->link_spd == FWSPD_S100 /* 0 */) {
1793 			aprint_normal_dev(fc->bdev,
1794 			    "Pre 1394a-2000 detected\n");
1795 			fwdev->speed = fc->speed_map->speed[fc->nodeid][node];
1796 		} else
1797 			fwdev->speed = binfo->link_spd;
1798 		/*
1799 		 * Test this speed with a read to the CSRROM.
1800 		 * If it fails, slow down the speed and retry.
1801 		 */
1802 		while (fwdev->speed > FWSPD_S100 /* 0 */) {
1803 			err = fw_explore_read_quads(fwdev, CSRROMOFF,
1804 			    &speed_test, 1);
1805 			if (err) {
1806 				aprint_error_dev(fc->bdev, "fwdev->speed(%s)"
1807 				    " decremented due to negotiation\n",
1808 				    fw_linkspeed[fwdev->speed]);
1809 				fwdev->speed--;
1810 			} else
1811 				break;
1812 		}
1813 		/*
1814 		 * If the fwdev is not found in the
1815 		 * fc->devices TAILQ, then we will add it.
1816 		 */
1817 		pfwdev = NULL;
1818 		mutex_enter(&fc->fc_mtx);
1819 		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1820 			if (tfwdev->eui.hi > fwdev->eui.hi ||
1821 			    (tfwdev->eui.hi == fwdev->eui.hi &&
1822 						tfwdev->eui.lo > fwdev->eui.lo))
1823 				break;
1824 			pfwdev = tfwdev;
1825 		}
1826 		if (pfwdev == NULL)
1827 			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1828 		else
1829 			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1830 		mutex_exit(&fc->fc_mtx);
1831 
1832 		aprint_normal_dev(fc->bdev, "New %s device ID:%08x%08x\n",
1833 		    fw_linkspeed[fwdev->speed], fwdev->eui.hi, fwdev->eui.lo);
1834 	} else {
1835 		fwdev->dst = node;
1836 		fwdev->status = FWDEVINIT;
1837 		/* unchanged ? */
1838 		if (memcmp(csr, fwdev->csrrom, sizeof(uint32_t) * 5) == 0) {
1839 			if (firewire_debug)
1840 				printf("node%d: crom unchanged\n", node);
1841 			return 0;
1842 		}
1843 	}
1844 
1845 	memset(fwdev->csrrom, 0, CROMSIZE);
1846 
1847 	/* copy first quad and bus info block */
1848 	memcpy(fwdev->csrrom, csr, sizeof(uint32_t) * 5);
1849 	fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
1850 
1851 	err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
1852 
1853 	if (err) {
1854 		if (firewire_debug)
1855 			printf("explore csrblock failed err(%d)\n", err);
1856 		fwdev->status = FWDEVINVAL;
1857 		fwdev->csrrom[0] = 0;
1858 	}
1859 	return err;
1860 }
1861 
1862 /*
1863  * Find the self_id packet for a node, ignoring sequels.
1864  */
1865 static union fw_self_id *
1866 fw_find_self_id(struct firewire_comm *fc, int node)
1867 {
1868 	uint32_t i;
1869 	union fw_self_id *s;
1870 
1871 	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1872 		s = &fc->topology_map->self_id[i];
1873 		if (s->p0.sequel)
1874 			continue;
1875 		if (s->p0.phy_id == node)
1876 			return s;
1877 	}
1878 	return 0;
1879 }
1880 
1881 static void
1882 fw_explore(struct firewire_comm *fc)
1883 {
1884 	struct fw_device *dfwdev;
1885 	union fw_self_id *fwsid;
1886 	int node, err, i, todo, todo2, trys;
1887 	char nodes[63];
1888 
1889 	todo = 0;
1890 	dfwdev = malloc(sizeof(*dfwdev), M_TEMP, M_NOWAIT);
1891 	if (dfwdev == NULL)
1892 		return;
1893 	/* setup dummy fwdev */
1894 	dfwdev->fc = fc;
1895 	dfwdev->speed = 0;
1896 	dfwdev->maxrec = 8; /* 512 */
1897 	dfwdev->status = FWDEVINIT;
1898 
1899 	for (node = 0; node <= fc->max_node; node++) {
1900 		/* We don't probe myself and linkdown nodes */
1901 		if (node == fc->nodeid) {
1902 			if (firewire_debug)
1903 				printf("found myself node(%d) fc->nodeid(%d)"
1904 				    " fc->max_node(%d)\n",
1905 				    node, fc->nodeid, fc->max_node);
1906 			continue;
1907 		} else if (firewire_debug)
1908 			printf("node(%d) fc->max_node(%d) found\n",
1909 			    node, fc->max_node);
1910 		fwsid = fw_find_self_id(fc, node);
1911 		if (!fwsid || !fwsid->p0.link_active) {
1912 			if (firewire_debug)
1913 				printf("node%d: link down\n", node);
1914 			continue;
1915 		}
1916 		nodes[todo++] = node;
1917 	}
1918 
1919 	for (trys = 0; todo > 0 && trys < 3; trys++) {
1920 		todo2 = 0;
1921 		for (i = 0; i < todo; i++) {
1922 			dfwdev->dst = nodes[i];
1923 			err = fw_explore_node(dfwdev);
1924 			if (err)
1925 				nodes[todo2++] = nodes[i];
1926 			if (firewire_debug)
1927 				printf("node %d, err = %d\n", nodes[i], err);
1928 		}
1929 		todo = todo2;
1930 	}
1931 	free(dfwdev, M_TEMP);
1932 }
1933 
1934 static void
1935 fw_bus_probe_thread(void *arg)
1936 {
1937 	struct firewire_comm *fc = (struct firewire_comm *)arg;
1938 
1939 	config_pending_decr();
1940 
1941 	mutex_enter(&fc->wait_lock);
1942 	while (fc->status != FWBUSDETACH) {
1943 		if (fc->status == FWBUSEXPLORE) {
1944 			mutex_exit(&fc->wait_lock);
1945 			fw_explore(fc);
1946 			fc->status = FWBUSEXPDONE;
1947 			if (firewire_debug)
1948 				printf("bus_explore done\n");
1949 			fw_attach_dev(fc);
1950 			mutex_enter(&fc->wait_lock);
1951 		}
1952 		cv_wait_sig(&fc->fc_cv, &fc->wait_lock);
1953 	}
1954 	fc->status = FWBUSDETACHOK;
1955 	cv_signal(&fc->fc_cv);
1956 	mutex_exit(&fc->wait_lock);
1957 	kthread_exit(0);
1958 
1959 	/* NOTREACHED */
1960 }
1961 
1962 static const char *
1963 fw_get_devclass(struct fw_device *fwdev)
1964 {
1965 	struct crom_context cc;
1966 	struct csrreg *reg;
1967 
1968 	crom_init_context(&cc, fwdev->csrrom);
1969 	reg = crom_search_key(&cc, CSRKEY_VER);
1970 	if (reg == NULL)
1971 		return "null";
1972 
1973 	switch (reg->val) {
1974 	case CSR_PROTAVC:
1975 		return "av/c";
1976 	case CSR_PROTCAL:
1977 		return "cal";
1978 	case CSR_PROTEHS:
1979 		return "ehs";
1980 	case CSR_PROTHAVI:
1981 		return "havi";
1982 	case CSR_PROTCAM104:
1983 		return "cam104";
1984 	case CSR_PROTCAM120:
1985 		return "cam120";
1986 	case CSR_PROTCAM130:
1987 		return "cam130";
1988 	case CSR_PROTDPP:
1989 		return "printer";
1990 	case CSR_PROTIICP:
1991 		return "iicp";
1992 	case CSRVAL_T10SBP2:
1993 		return "sbp";
1994 	default:
1995 		if (firewire_debug)
1996 			printf("%s: reg->val 0x%x\n",
1997 				__func__, reg->val);
1998 		return "sbp";
1999 	}
2000 }
2001 
2002 /*
2003  * To attach sub-devices layer onto IEEE1394 bus.
2004  */
2005 static void
2006 fw_attach_dev(struct firewire_comm *fc)
2007 {
2008 	struct firewire_softc *sc = device_private(fc->bdev);
2009 	struct firewire_dev_list *devlist, *elm;
2010 	struct fw_device *fwdev, *next;
2011 	struct firewire_dev_comm *fdc;
2012 	struct fw_attach_args fwa;
2013 	int locs[IEEE1394IFCF_NLOCS];
2014 
2015 	fwa.name = "null";
2016 	fwa.fc = fc;
2017 
2018 	mutex_enter(&fc->fc_mtx);
2019 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
2020 		next = STAILQ_NEXT(fwdev, link);
2021 		mutex_exit(&fc->fc_mtx);
2022 		switch (fwdev->status) {
2023 		case FWDEVNEW:
2024 			devlist = malloc(sizeof(struct firewire_dev_list),
2025 			    M_DEVBUF, M_NOWAIT);
2026 			if (devlist == NULL) {
2027 				aprint_error_dev(fc->bdev,
2028 				    "memory allocation failed\n");
2029 				break;
2030 			}
2031 
2032 			locs[IEEE1394IFCF_EUIHI] = fwdev->eui.hi;
2033 			locs[IEEE1394IFCF_EUILO] = fwdev->eui.lo;
2034 
2035 			fwa.name = fw_get_devclass(fwdev);
2036 			fwa.fwdev = fwdev;
2037 			fwdev->dev = config_found_sm_loc(sc->dev, "ieee1394if",
2038 			    locs, &fwa, firewire_print, config_stdsubmatch);
2039 			if (fwdev->dev == NULL) {
2040 				free(devlist, M_DEVBUF);
2041 				break;
2042 			}
2043 
2044 			devlist->fwdev = fwdev;
2045 			devlist->dev = fwdev->dev;
2046 
2047 			mutex_enter(&fc->fc_mtx);
2048 			if (SLIST_EMPTY(&sc->devlist))
2049 				SLIST_INSERT_HEAD(&sc->devlist, devlist, link);
2050 			else {
2051 				for (elm = SLIST_FIRST(&sc->devlist);
2052 				    SLIST_NEXT(elm, link) != NULL;
2053 				    elm = SLIST_NEXT(elm, link));
2054 				SLIST_INSERT_AFTER(elm, devlist, link);
2055 			}
2056 			mutex_exit(&fc->fc_mtx);
2057 
2058 			/* FALLTHROUGH */
2059 
2060 		case FWDEVINIT:
2061 		case FWDEVATTACHED:
2062 			fwdev->status = FWDEVATTACHED;
2063 			break;
2064 
2065 		case FWDEVINVAL:
2066 			fwdev->rcnt++;
2067 			if (firewire_debug)
2068 				printf("fwdev->rcnt(%d), hold_count(%d)\n",
2069 				    fwdev->rcnt, hold_count);
2070 			break;
2071 
2072 		default:
2073 			/* XXX */
2074 			break;
2075 		}
2076 		mutex_enter(&fc->fc_mtx);
2077 	}
2078 	mutex_exit(&fc->fc_mtx);
2079 
2080 	SLIST_FOREACH(devlist, &sc->devlist, link) {
2081 		fdc = device_private(devlist->dev);
2082 		if (fdc->post_explore != NULL)
2083 			fdc->post_explore(fdc);
2084 	}
2085 
2086 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
2087 		next = STAILQ_NEXT(fwdev, link);
2088 		if (fwdev->rcnt > 0 && fwdev->rcnt > hold_count) {
2089 			/*
2090 			 * Remove devices which have not been seen
2091 			 * for a while.
2092 			 */
2093 			SLIST_FOREACH(devlist, &sc->devlist, link)
2094 				if (devlist->fwdev == fwdev)
2095 					break;
2096 
2097 			if (devlist == NULL)
2098 				continue;
2099 
2100 			if (devlist->fwdev != fwdev)
2101 				panic("already detached");
2102 
2103 			SLIST_REMOVE(&sc->devlist, devlist, firewire_dev_list,
2104 			    link);
2105 			free(devlist, M_DEVBUF);
2106 
2107 			if (config_detach(fwdev->dev, DETACH_FORCE) != 0)
2108 				return;
2109 
2110 			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
2111 			free(fwdev, M_FW);
2112 		}
2113 	}
2114 
2115 	return;
2116 }
2117 
2118 /*
2119  * To allocate unique transaction label.
2120  */
2121 static int
2122 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
2123 {
2124 	u_int dst, new_tlabel;
2125 	struct fw_xfer *txfer;
2126 
2127 	dst = xfer->send.hdr.mode.hdr.dst & 0x3f;
2128 	mutex_enter(&fc->tlabel_lock);
2129 	new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f;
2130 	STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel)
2131 		if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst)
2132 			break;
2133 	if (txfer == NULL) {
2134 		fc->last_tlabel[dst] = new_tlabel;
2135 		STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel);
2136 		mutex_exit(&fc->tlabel_lock);
2137 		xfer->tl = new_tlabel;
2138 		xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2;
2139 		if (firewire_debug > 1)
2140 			printf("fw_get_tlabel: dst=%d tl=%d\n",
2141 			    dst, new_tlabel);
2142 		return new_tlabel;
2143 	}
2144 	mutex_exit(&fc->tlabel_lock);
2145 
2146 	if (firewire_debug > 1)
2147 		printf("fw_get_tlabel: no free tlabel\n");
2148 	return -1;
2149 }
2150 
2151 static void
2152 fw_rcv_copy(struct fw_rcv_buf *rb)
2153 {
2154 	struct fw_pkt *pkt;
2155 	u_char *p;
2156 	const struct tcode_info *tinfo;
2157 	u_int res, i, len, plen;
2158 
2159 	rb->xfer->recv.spd = rb->spd;
2160 
2161 	pkt = (struct fw_pkt *)rb->vec->iov_base;
2162 	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
2163 
2164 	/* Copy header */
2165 	p = (u_char *)&rb->xfer->recv.hdr;
2166 	memcpy(p, rb->vec->iov_base, tinfo->hdr_len);
2167 	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
2168 	rb->vec->iov_len -= tinfo->hdr_len;
2169 
2170 	/* Copy payload */
2171 	p = (u_char *)rb->xfer->recv.payload;
2172 	res = rb->xfer->recv.pay_len;
2173 
2174 	/* special handling for RRESQ */
2175 	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
2176 	    p != NULL && res >= sizeof(uint32_t)) {
2177 		*(uint32_t *)p = pkt->mode.rresq.data;
2178 		rb->xfer->recv.pay_len = sizeof(uint32_t);
2179 		return;
2180 	}
2181 
2182 	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
2183 		return;
2184 
2185 	plen = pkt->mode.rresb.len;
2186 
2187 	for (i = 0; i < rb->nvec; i++, rb->vec++) {
2188 		len = MIN(rb->vec->iov_len, plen);
2189 		if (res < len) {
2190 			aprint_error_dev(rb->fc->bdev,
2191 			    "rcv buffer(%d) is %d bytes short.\n",
2192 			    rb->xfer->recv.pay_len, len - res);
2193 			len = res;
2194 		}
2195 		if (p) {
2196 			memcpy(p, rb->vec->iov_base, len);
2197 			p += len;
2198 		}
2199 		res -= len;
2200 		plen -= len;
2201 		if (res == 0 || plen == 0)
2202 			break;
2203 	}
2204 	rb->xfer->recv.pay_len -= res;
2205 
2206 }
2207 
2208 /*
2209  * Post process for Bus Manager election process.
2210  */
2211 static void
2212 fw_try_bmr_callback(struct fw_xfer *xfer)
2213 {
2214 	struct firewire_comm *fc;
2215 	int bmr;
2216 
2217 	if (xfer == NULL)
2218 		return;
2219 	fc = xfer->fc;
2220 	if (xfer->resp != 0)
2221 		goto error;
2222 	if (xfer->recv.payload == NULL)
2223 		goto error;
2224 	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2225 		goto error;
2226 
2227 	bmr = ntohl(xfer->recv.payload[0]);
2228 	if (bmr == 0x3f)
2229 		bmr = fc->nodeid;
2230 
2231 	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2232 	fw_xfer_free_buf(xfer);
2233 	fw_bmr(fc);
2234 	return;
2235 
2236 error:
2237 	aprint_error_dev(fc->bdev, "bus manager election failed\n");
2238 	fw_xfer_free_buf(xfer);
2239 }
2240 
2241 
2242 /*
2243  * To candidate Bus Manager election process.
2244  */
2245 static void
2246 fw_try_bmr(void *arg)
2247 {
2248 	struct fw_xfer *xfer;
2249 	struct firewire_comm *fc = (struct firewire_comm *)arg;
2250 	struct fw_pkt *fp;
2251 	int err = 0;
2252 
2253 	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2254 	if (xfer == NULL)
2255 		return;
2256 	xfer->send.spd = 0;
2257 	fc->status = FWBUSMGRELECT;
2258 
2259 	fp = &xfer->send.hdr;
2260 	fp->mode.lreq.dest_hi = 0xffff;
2261 	fp->mode.lreq.tlrt = 0;
2262 	fp->mode.lreq.tcode = FWTCODE_LREQ;
2263 	fp->mode.lreq.pri = 0;
2264 	fp->mode.lreq.src = 0;
2265 	fp->mode.lreq.len = 8;
2266 	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2267 	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2268 	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2269 	xfer->send.payload[0] = htonl(0x3f);
2270 	xfer->send.payload[1] = htonl(fc->nodeid);
2271 	xfer->hand = fw_try_bmr_callback;
2272 
2273 	err = fw_asyreq(fc, -1, xfer);
2274 	if (err) {
2275 		fw_xfer_free_buf(xfer);
2276 		return;
2277 	}
2278 	return;
2279 }
2280 
2281 /*
2282  * Find the root node, if it is not
2283  * Cycle Master Capable, then we should
2284  * override this and become the Cycle
2285  * Master
2286  */
2287 static int
2288 fw_bmr(struct firewire_comm *fc)
2289 {
2290 	struct fw_device fwdev;
2291 	union fw_self_id *self_id;
2292 	int cmstr;
2293 	uint32_t quad;
2294 
2295 	/* Check to see if the current root node is cycle master capable */
2296 	self_id = fw_find_self_id(fc, fc->max_node);
2297 	if (fc->max_node > 0) {
2298 		/* XXX check cmc bit of businfo block rather than contender */
2299 		if (self_id->p0.link_active && self_id->p0.contender)
2300 			cmstr = fc->max_node;
2301 		else {
2302 			aprint_normal_dev(fc->bdev,
2303 				"root node is not cycle master capable\n");
2304 			/* XXX shall we be the cycle master? */
2305 			cmstr = fc->nodeid;
2306 			/* XXX need bus reset */
2307 		}
2308 	} else
2309 		cmstr = -1;
2310 
2311 	aprint_normal_dev(fc->bdev, "bus manager %d%s\n",
2312 	    CSRARC(fc, BUS_MGR_ID),
2313 	    (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) ? " (me)" : "");
2314 	if (CSRARC(fc, BUS_MGR_ID) != fc->nodeid)
2315 		/* We are not the bus manager */
2316 		return 0;
2317 
2318 	/* Optimize gapcount */
2319 	if (fc->max_hop <= MAX_GAPHOP)
2320 		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2321 	/* If we are the cycle master, nothing to do */
2322 	if (cmstr == fc->nodeid || cmstr == -1)
2323 		return 0;
2324 	/* Bus probe has not finished, make dummy fwdev for cmstr */
2325 	memset(&fwdev, 0, sizeof(fwdev));
2326 	fwdev.fc = fc;
2327 	fwdev.dst = cmstr;
2328 	fwdev.speed = 0;
2329 	fwdev.maxrec = 8; /* 512 */
2330 	fwdev.status = FWDEVINIT;
2331 	/* Set cmstr bit on the cycle master */
2332 	quad = htonl(1 << 8);
2333 	fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 0xffff, 0xf0000000 | STATE_SET,
2334 	    &quad, fw_asy_callback_free);
2335 
2336 	return 0;
2337 }
2338