firewire.c revision 1.8 1 /* $NetBSD: firewire.c,v 1.8 2006/06/13 02:39:11 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 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((caddr_t)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 (caddr_t)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((caddr_t)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