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