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