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