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