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