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