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