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