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