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