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