fwohci.c revision 1.8.2.4 1 1.8.2.2 bouyer /*-
2 1.8.2.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
3 1.8.2.2 bouyer * All rights reserved.
4 1.8.2.2 bouyer *
5 1.8.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
6 1.8.2.2 bouyer * by Matt Thomas of 3am Software Foundry.
7 1.8.2.2 bouyer *
8 1.8.2.2 bouyer * Redistribution and use in source and binary forms, with or without
9 1.8.2.2 bouyer * modification, are permitted provided that the following conditions
10 1.8.2.2 bouyer * are met:
11 1.8.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
12 1.8.2.2 bouyer * notice, this list of conditions and the following disclaimer.
13 1.8.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.8.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.8.2.2 bouyer * documentation and/or other materials provided with the distribution.
16 1.8.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
17 1.8.2.2 bouyer * must display the following acknowledgement:
18 1.8.2.2 bouyer * This product includes software developed by the NetBSD
19 1.8.2.2 bouyer * Foundation, Inc. and its contributors.
20 1.8.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
21 1.8.2.2 bouyer * contributors may be used to endorse or promote products derived
22 1.8.2.2 bouyer * from this software without specific prior written permission.
23 1.8.2.2 bouyer *
24 1.8.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.8.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.8.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.8.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.8.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.8.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.8.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.8.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.8.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.8.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.8.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
35 1.8.2.2 bouyer */
36 1.8.2.2 bouyer
37 1.8.2.3 bouyer /*
38 1.8.2.3 bouyer * IEEE1394 Open Host Controller Interface
39 1.8.2.3 bouyer * based on OHCI Specification 1.1 (January 6, 2000)
40 1.8.2.3 bouyer * The first version to support network interface part is wrtten by
41 1.8.2.3 bouyer * Atsushi Onoe <onoe (at) netbsd.org>.
42 1.8.2.3 bouyer */
43 1.8.2.3 bouyer
44 1.8.2.3 bouyer #include "opt_inet.h"
45 1.8.2.3 bouyer
46 1.8.2.2 bouyer #include <sys/param.h>
47 1.8.2.2 bouyer #include <sys/systm.h>
48 1.8.2.2 bouyer #include <sys/types.h>
49 1.8.2.2 bouyer #include <sys/socket.h>
50 1.8.2.3 bouyer #include <sys/callout.h>
51 1.8.2.2 bouyer #include <sys/device.h>
52 1.8.2.3 bouyer #include <sys/kernel.h>
53 1.8.2.3 bouyer #include <sys/malloc.h>
54 1.8.2.3 bouyer #include <sys/mbuf.h>
55 1.8.2.3 bouyer
56 1.8.2.3 bouyer #if __NetBSD_Version__ >= 105010000
57 1.8.2.3 bouyer #include <uvm/uvm_extern.h>
58 1.8.2.3 bouyer #else
59 1.8.2.3 bouyer #include <vm/vm.h>
60 1.8.2.3 bouyer #endif
61 1.8.2.2 bouyer
62 1.8.2.2 bouyer #include <machine/bus.h>
63 1.8.2.2 bouyer
64 1.8.2.2 bouyer #include <dev/ieee1394/ieee1394reg.h>
65 1.8.2.2 bouyer #include <dev/ieee1394/fwohcireg.h>
66 1.8.2.2 bouyer
67 1.8.2.2 bouyer #include <dev/ieee1394/ieee1394var.h>
68 1.8.2.2 bouyer #include <dev/ieee1394/fwohcivar.h>
69 1.8.2.2 bouyer
70 1.8.2.2 bouyer static const char * const ieee1394_speeds[] = { IEEE1394_SPD_STRINGS };
71 1.8.2.2 bouyer
72 1.8.2.3 bouyer #if 0
73 1.8.2.3 bouyer static int fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
74 1.8.2.3 bouyer bus_dmamap_t *mapp, caddr_t *kvap, int flags);
75 1.8.2.3 bouyer #endif
76 1.8.2.3 bouyer static void fwohci_hw_init(struct fwohci_softc *);
77 1.8.2.3 bouyer static void fwohci_power(int, void *);
78 1.8.2.3 bouyer static void fwohci_shutdown(void *);
79 1.8.2.3 bouyer
80 1.8.2.3 bouyer static int fwohci_desc_alloc(struct fwohci_softc *);
81 1.8.2.3 bouyer static struct fwohci_desc *fwohci_desc_get(struct fwohci_softc *, int);
82 1.8.2.3 bouyer static void fwohci_desc_put(struct fwohci_softc *, struct fwohci_desc *, int);
83 1.8.2.3 bouyer
84 1.8.2.3 bouyer static int fwohci_ctx_alloc(struct fwohci_softc *, struct fwohci_ctx **,
85 1.8.2.3 bouyer int, int);
86 1.8.2.3 bouyer static void fwohci_ctx_free(struct fwohci_softc *, struct fwohci_ctx *);
87 1.8.2.3 bouyer static void fwohci_ctx_init(struct fwohci_softc *, struct fwohci_ctx *);
88 1.8.2.3 bouyer
89 1.8.2.3 bouyer static int fwohci_buf_alloc(struct fwohci_softc *, struct fwohci_buf *);
90 1.8.2.3 bouyer static void fwohci_buf_free(struct fwohci_softc *, struct fwohci_buf *);
91 1.8.2.3 bouyer static void fwohci_buf_init(struct fwohci_softc *);
92 1.8.2.3 bouyer static void fwohci_buf_start(struct fwohci_softc *);
93 1.8.2.3 bouyer static void fwohci_buf_stop(struct fwohci_softc *);
94 1.8.2.3 bouyer static void fwohci_buf_next(struct fwohci_softc *, struct fwohci_ctx *);
95 1.8.2.3 bouyer static int fwohci_buf_pktget(struct fwohci_softc *, struct fwohci_ctx *,
96 1.8.2.3 bouyer caddr_t *, int);
97 1.8.2.3 bouyer static int fwohci_buf_input(struct fwohci_softc *, struct fwohci_ctx *,
98 1.8.2.3 bouyer struct fwohci_pkt *);
99 1.8.2.3 bouyer
100 1.8.2.3 bouyer static u_int8_t fwohci_phy_read(struct fwohci_softc *, u_int8_t);
101 1.8.2.3 bouyer static void fwohci_phy_write(struct fwohci_softc *, u_int8_t, u_int8_t);
102 1.8.2.3 bouyer static void fwohci_phy_busreset(struct fwohci_softc *);
103 1.8.2.3 bouyer static void fwohci_phy_input(struct fwohci_softc *, struct fwohci_pkt *);
104 1.8.2.3 bouyer
105 1.8.2.3 bouyer static int fwohci_handler_set(struct fwohci_softc *, int, u_int32_t, u_int32_t,
106 1.8.2.3 bouyer int (*)(struct fwohci_softc *, void *, struct fwohci_pkt *),
107 1.8.2.3 bouyer void *);
108 1.8.2.3 bouyer
109 1.8.2.3 bouyer static void fwohci_arrq_input(struct fwohci_softc *, struct fwohci_ctx *);
110 1.8.2.3 bouyer static void fwohci_arrs_input(struct fwohci_softc *, struct fwohci_ctx *);
111 1.8.2.3 bouyer static void fwohci_ir_input(struct fwohci_softc *, struct fwohci_ctx *);
112 1.8.2.3 bouyer
113 1.8.2.3 bouyer static int fwohci_at_output(struct fwohci_softc *, struct fwohci_ctx *,
114 1.8.2.3 bouyer struct fwohci_pkt *);
115 1.8.2.3 bouyer static void fwohci_at_done(struct fwohci_softc *, struct fwohci_ctx *, int);
116 1.8.2.3 bouyer static void fwohci_atrs_output(struct fwohci_softc *, int, struct fwohci_pkt *,
117 1.8.2.3 bouyer struct fwohci_pkt *);
118 1.8.2.3 bouyer
119 1.8.2.3 bouyer static void fwohci_configrom_init(struct fwohci_softc *);
120 1.8.2.3 bouyer
121 1.8.2.3 bouyer static void fwohci_selfid_init(struct fwohci_softc *);
122 1.8.2.3 bouyer static int fwohci_selfid_input(struct fwohci_softc *);
123 1.8.2.3 bouyer
124 1.8.2.3 bouyer static void fwohci_csr_init(struct fwohci_softc *);
125 1.8.2.3 bouyer static int fwohci_csr_input(struct fwohci_softc *, void *,
126 1.8.2.3 bouyer struct fwohci_pkt *);
127 1.8.2.3 bouyer
128 1.8.2.3 bouyer static void fwohci_uid_collect(struct fwohci_softc *);
129 1.8.2.3 bouyer static int fwohci_uid_input(struct fwohci_softc *, void *,
130 1.8.2.3 bouyer struct fwohci_pkt *);
131 1.8.2.3 bouyer static int fwohci_uid_lookup(struct fwohci_softc *, const u_int8_t *);
132 1.8.2.3 bouyer
133 1.8.2.3 bouyer static int fwohci_if_inreg(struct device *, u_int32_t, u_int32_t,
134 1.8.2.3 bouyer void (*)(struct device *, struct mbuf *));
135 1.8.2.3 bouyer static int fwohci_if_input(struct fwohci_softc *, void *, struct fwohci_pkt *);
136 1.8.2.3 bouyer static int fwohci_if_output(struct device *, struct mbuf *,
137 1.8.2.3 bouyer void (*)(struct device *, struct mbuf *));
138 1.8.2.3 bouyer
139 1.8.2.3 bouyer #ifdef FW_DEBUG
140 1.8.2.3 bouyer int fw_verbose = 0;
141 1.8.2.3 bouyer int fw_dump = 0;
142 1.8.2.3 bouyer #endif
143 1.8.2.3 bouyer
144 1.8.2.2 bouyer int
145 1.8.2.3 bouyer fwohci_init(struct fwohci_softc *sc, const struct evcnt *ev)
146 1.8.2.2 bouyer {
147 1.8.2.3 bouyer int i;
148 1.8.2.2 bouyer u_int32_t val;
149 1.8.2.3 bouyer #if 0
150 1.8.2.3 bouyer int error;
151 1.8.2.3 bouyer #endif
152 1.8.2.3 bouyer
153 1.8.2.3 bouyer evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ev,
154 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, "intr");
155 1.8.2.3 bouyer
156 1.8.2.3 bouyer /*
157 1.8.2.3 bouyer * Wait for reset completion
158 1.8.2.3 bouyer */
159 1.8.2.3 bouyer for (i = 0; i < OHCI_LOOP; i++) {
160 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
161 1.8.2.3 bouyer if ((val & OHCI_HCControl_SoftReset) == 0)
162 1.8.2.3 bouyer break;
163 1.8.2.3 bouyer }
164 1.8.2.2 bouyer
165 1.8.2.2 bouyer /* What dialect of OHCI is this device?
166 1.8.2.2 bouyer */
167 1.8.2.2 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_Version);
168 1.8.2.2 bouyer printf("%s: OHCI %u.%u", sc->sc_sc1394.sc1394_dev.dv_xname,
169 1.8.2.2 bouyer OHCI_Version_GET_Version(val), OHCI_Version_GET_Revision(val));
170 1.8.2.2 bouyer
171 1.8.2.2 bouyer /* Is the Global UID ROM present?
172 1.8.2.2 bouyer */
173 1.8.2.2 bouyer if ((val & OHCI_Version_GUID_ROM) == 0) {
174 1.8.2.2 bouyer printf("\n%s: fatal: no global UID ROM\n", sc->sc_sc1394.sc1394_dev.dv_xname);
175 1.8.2.2 bouyer return -1;
176 1.8.2.3 bouyer } else {
177 1.8.2.2 bouyer
178 1.8.2.3 bouyer /* Extract the Global UID
179 1.8.2.3 bouyer */
180 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_GUIDHi);
181 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[0] = (val >> 24) & 0xff;
182 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[1] = (val >> 16) & 0xff;
183 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[2] = (val >> 8) & 0xff;
184 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[3] = (val >> 0) & 0xff;
185 1.8.2.3 bouyer
186 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_GUIDLo);
187 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[4] = (val >> 24) & 0xff;
188 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[5] = (val >> 16) & 0xff;
189 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[6] = (val >> 8) & 0xff;
190 1.8.2.3 bouyer sc->sc_sc1394.sc1394_guid[7] = (val >> 0) & 0xff;
191 1.8.2.3 bouyer }
192 1.8.2.2 bouyer
193 1.8.2.2 bouyer printf(", %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
194 1.8.2.2 bouyer sc->sc_sc1394.sc1394_guid[0], sc->sc_sc1394.sc1394_guid[1],
195 1.8.2.2 bouyer sc->sc_sc1394.sc1394_guid[2], sc->sc_sc1394.sc1394_guid[3],
196 1.8.2.2 bouyer sc->sc_sc1394.sc1394_guid[4], sc->sc_sc1394.sc1394_guid[5],
197 1.8.2.2 bouyer sc->sc_sc1394.sc1394_guid[6], sc->sc_sc1394.sc1394_guid[7]);
198 1.8.2.2 bouyer
199 1.8.2.2 bouyer /* Get the maximum link speed and receive size
200 1.8.2.2 bouyer */
201 1.8.2.2 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
202 1.8.2.2 bouyer sc->sc_sc1394.sc1394_link_speed =
203 1.8.2.2 bouyer (val & OHCI_BusOptions_LinkSpd_MASK)
204 1.8.2.2 bouyer >> OHCI_BusOptions_LinkSpd_BITPOS;
205 1.8.2.2 bouyer if (sc->sc_sc1394.sc1394_link_speed < IEEE1394_SPD_MAX) {
206 1.8.2.2 bouyer printf(", %s", ieee1394_speeds[sc->sc_sc1394.sc1394_link_speed]);
207 1.8.2.2 bouyer } else {
208 1.8.2.2 bouyer printf(", unknown speed %u", sc->sc_sc1394.sc1394_link_speed);
209 1.8.2.2 bouyer }
210 1.8.2.2 bouyer
211 1.8.2.2 bouyer /* MaxRec is encoded as log2(max_rec_octets)-1
212 1.8.2.2 bouyer */
213 1.8.2.2 bouyer sc->sc_sc1394.sc1394_max_receive =
214 1.8.2.2 bouyer 1 << (((val & OHCI_BusOptions_MaxRec_MASK)
215 1.8.2.2 bouyer >> OHCI_BusOptions_MaxRec_BITPOS) + 1);
216 1.8.2.3 bouyer printf(", %u max_rec", sc->sc_sc1394.sc1394_max_receive);
217 1.8.2.3 bouyer
218 1.8.2.3 bouyer /*
219 1.8.2.3 bouyer * Count how many isochronous ctx we have.
220 1.8.2.3 bouyer */
221 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
222 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntMaskClear);
223 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskClear, ~0);
224 1.8.2.3 bouyer for (i = 0; val != 0; val >>= 1) {
225 1.8.2.3 bouyer if (val & 0x1)
226 1.8.2.3 bouyer i++;
227 1.8.2.3 bouyer }
228 1.8.2.3 bouyer sc->sc_isoctx = i;
229 1.8.2.3 bouyer printf(", %d iso_ctx", sc->sc_isoctx);
230 1.8.2.2 bouyer
231 1.8.2.2 bouyer printf("\n");
232 1.8.2.3 bouyer
233 1.8.2.3 bouyer #if 0
234 1.8.2.3 bouyer error = fwohci_dnamem_alloc(sc, OHCI_CONFIG_SIZE, OHCI_CONFIG_ALIGNMENT,
235 1.8.2.3 bouyer &sc->sc_configrom_map,
236 1.8.2.3 bouyer (caddr_t *) &sc->sc_configrom,
237 1.8.2.3 bouyer BUS_DMA_WAITOK|BUS_DMA_COHERENT);
238 1.8.2.3 bouyer return error;
239 1.8.2.3 bouyer #endif
240 1.8.2.3 bouyer
241 1.8.2.3 bouyer /*
242 1.8.2.3 bouyer * Enable Link Power
243 1.8.2.3 bouyer */
244 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
245 1.8.2.3 bouyer
246 1.8.2.3 bouyer /*
247 1.8.2.3 bouyer * Allocate descriptors
248 1.8.2.3 bouyer */
249 1.8.2.3 bouyer if (fwohci_desc_alloc(sc))
250 1.8.2.3 bouyer return -1;
251 1.8.2.3 bouyer
252 1.8.2.3 bouyer /*
253 1.8.2.3 bouyer * Allocate DMA Context
254 1.8.2.3 bouyer */
255 1.8.2.3 bouyer fwohci_ctx_alloc(sc, &sc->sc_ctx_arrq, OHCI_BUF_ARRQ_CNT,
256 1.8.2.3 bouyer OHCI_CTX_ASYNC_RX_REQUEST);
257 1.8.2.3 bouyer fwohci_ctx_alloc(sc, &sc->sc_ctx_arrs, OHCI_BUF_ARRS_CNT,
258 1.8.2.3 bouyer OHCI_CTX_ASYNC_RX_RESPONSE);
259 1.8.2.3 bouyer fwohci_ctx_alloc(sc, &sc->sc_ctx_atrq, 0, OHCI_CTX_ASYNC_TX_REQUEST);
260 1.8.2.3 bouyer fwohci_ctx_alloc(sc, &sc->sc_ctx_atrs, 0, OHCI_CTX_ASYNC_TX_RESPONSE);
261 1.8.2.3 bouyer sc->sc_ctx_ir = malloc(sizeof(sc->sc_ctx_ir[0]) * sc->sc_isoctx,
262 1.8.2.3 bouyer M_DEVBUF, M_WAITOK);
263 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
264 1.8.2.3 bouyer sc->sc_ctx_ir[i] = NULL;
265 1.8.2.3 bouyer #if 0
266 1.8.2.3 bouyer fwohci_ctx_alloc(sc, &sc->sc_ctx_ir[i], OHCI_BUF_IR_CNT, i);
267 1.8.2.3 bouyer sc->sc_ctx_ir[i]->fc_isoch = 1;
268 1.8.2.3 bouyer #endif
269 1.8.2.3 bouyer }
270 1.8.2.3 bouyer
271 1.8.2.3 bouyer /*
272 1.8.2.3 bouyer * Allocate buffer for configuration ROM and SelfID buffer
273 1.8.2.3 bouyer */
274 1.8.2.3 bouyer fwohci_buf_alloc(sc, &sc->sc_buf_cnfrom);
275 1.8.2.3 bouyer fwohci_buf_alloc(sc, &sc->sc_buf_selfid);
276 1.8.2.3 bouyer
277 1.8.2.3 bouyer /*
278 1.8.2.3 bouyer * establish hooks for shutdown and suspend/resume
279 1.8.2.3 bouyer */
280 1.8.2.3 bouyer sc->sc_shutdownhook = shutdownhook_establish(fwohci_shutdown, sc);
281 1.8.2.3 bouyer sc->sc_powerhook = powerhook_establish(fwohci_power, sc);
282 1.8.2.3 bouyer callout_init(&sc->sc_selfid_callout);
283 1.8.2.3 bouyer
284 1.8.2.3 bouyer /*
285 1.8.2.3 bouyer * Initialize hardware registers.
286 1.8.2.3 bouyer */
287 1.8.2.3 bouyer fwohci_hw_init(sc);
288 1.8.2.3 bouyer
289 1.8.2.3 bouyer /*
290 1.8.2.3 bouyer * Initiate Bus Reset
291 1.8.2.3 bouyer */
292 1.8.2.3 bouyer config_defer(&sc->sc_sc1394.sc1394_dev,
293 1.8.2.3 bouyer (void (*)(struct device *))fwohci_phy_busreset);
294 1.8.2.3 bouyer
295 1.8.2.3 bouyer sc->sc_sc1394.sc1394_ifinreg = fwohci_if_inreg;
296 1.8.2.3 bouyer sc->sc_sc1394.sc1394_ifoutput = fwohci_if_output;
297 1.8.2.3 bouyer sc->sc_sc1394.sc1394_if = config_found(&sc->sc_sc1394.sc1394_dev,
298 1.8.2.3 bouyer "fw", fwohci_print);
299 1.8.2.3 bouyer
300 1.8.2.2 bouyer return 0;
301 1.8.2.2 bouyer }
302 1.8.2.2 bouyer
303 1.8.2.2 bouyer int
304 1.8.2.2 bouyer fwohci_intr(void *arg)
305 1.8.2.2 bouyer {
306 1.8.2.2 bouyer struct fwohci_softc * const sc = arg;
307 1.8.2.3 bouyer int i;
308 1.8.2.2 bouyer int progress = 0;
309 1.8.2.3 bouyer u_int32_t intmask, iso;
310 1.8.2.2 bouyer
311 1.8.2.2 bouyer for (;;) {
312 1.8.2.3 bouyer intmask = OHCI_CSR_READ(sc, OHCI_REG_IntEventClear);
313 1.8.2.2 bouyer if (intmask == 0)
314 1.8.2.2 bouyer return progress;
315 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
316 1.8.2.3 bouyer intmask & ~OHCI_Int_BusReset);
317 1.8.2.3 bouyer #ifdef FW_DEBUG
318 1.8.2.3 bouyer if (fw_verbose) {
319 1.8.2.3 bouyer printf("%s: intmask=0x%08x:",
320 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, intmask);
321 1.8.2.3 bouyer if (intmask & OHCI_Int_CycleTooLong)
322 1.8.2.3 bouyer printf(" CycleTooLong");
323 1.8.2.3 bouyer if (intmask & OHCI_Int_UnrecoverableError)
324 1.8.2.3 bouyer printf(" UnrecoverableError");
325 1.8.2.3 bouyer if (intmask & OHCI_Int_CycleInconsistent)
326 1.8.2.3 bouyer printf(" CycleInconsistent");
327 1.8.2.3 bouyer if (intmask & OHCI_Int_BusReset)
328 1.8.2.3 bouyer printf(" BusReset");
329 1.8.2.3 bouyer if (intmask & OHCI_Int_SelfIDComplete)
330 1.8.2.3 bouyer printf(" SelfIDComplete");
331 1.8.2.3 bouyer if (intmask & OHCI_Int_LockRespErr)
332 1.8.2.3 bouyer printf(" LockRespErr");
333 1.8.2.3 bouyer if (intmask & OHCI_Int_PostedWriteErr)
334 1.8.2.3 bouyer printf(" PostedWriteErr");
335 1.8.2.3 bouyer if (intmask & OHCI_Int_ReqTxComplete)
336 1.8.2.3 bouyer printf(" ReqTxComplete(0x%04x)",
337 1.8.2.3 bouyer OHCI_ASYNC_DMA_READ(sc,
338 1.8.2.3 bouyer OHCI_CTX_ASYNC_TX_REQUEST,
339 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear));
340 1.8.2.3 bouyer if (intmask & OHCI_Int_RespTxComplete)
341 1.8.2.3 bouyer printf(" RespTxComplete(0x%04x)",
342 1.8.2.3 bouyer OHCI_ASYNC_DMA_READ(sc,
343 1.8.2.3 bouyer OHCI_CTX_ASYNC_TX_RESPONSE,
344 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear));
345 1.8.2.3 bouyer if (intmask & OHCI_Int_ARRS)
346 1.8.2.3 bouyer printf(" ARRS(0x%04x)",
347 1.8.2.3 bouyer OHCI_ASYNC_DMA_READ(sc,
348 1.8.2.3 bouyer OHCI_CTX_ASYNC_RX_RESPONSE,
349 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear));
350 1.8.2.3 bouyer if (intmask & OHCI_Int_ARRQ)
351 1.8.2.3 bouyer printf(" ARRQ(0x%04x)",
352 1.8.2.3 bouyer OHCI_ASYNC_DMA_READ(sc,
353 1.8.2.3 bouyer OHCI_CTX_ASYNC_RX_REQUEST,
354 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear));
355 1.8.2.3 bouyer if (intmask & OHCI_Int_IsochRx)
356 1.8.2.3 bouyer printf(" IsochRx(0x%08x)",
357 1.8.2.3 bouyer OHCI_CSR_READ(sc,
358 1.8.2.3 bouyer OHCI_REG_IsoRecvIntEventClear));
359 1.8.2.3 bouyer if (intmask & OHCI_Int_IsochTx)
360 1.8.2.3 bouyer printf(" IsochTx(0x%08x)",
361 1.8.2.3 bouyer OHCI_CSR_READ(sc,
362 1.8.2.3 bouyer OHCI_REG_IsoXmitIntEventClear));
363 1.8.2.3 bouyer if (intmask & OHCI_Int_RQPkt)
364 1.8.2.3 bouyer printf(" RQPkt(0x%04x)",
365 1.8.2.3 bouyer OHCI_ASYNC_DMA_READ(sc,
366 1.8.2.3 bouyer OHCI_CTX_ASYNC_RX_REQUEST,
367 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear));
368 1.8.2.3 bouyer if (intmask & OHCI_Int_RSPkt)
369 1.8.2.3 bouyer printf(" RSPkt(0x%04x)",
370 1.8.2.3 bouyer OHCI_ASYNC_DMA_READ(sc,
371 1.8.2.3 bouyer OHCI_CTX_ASYNC_RX_RESPONSE,
372 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear));
373 1.8.2.3 bouyer printf("\n");
374 1.8.2.3 bouyer }
375 1.8.2.3 bouyer #endif /* FW_DEBUG */
376 1.8.2.3 bouyer if (intmask & OHCI_Int_BusReset) {
377 1.8.2.3 bouyer /*
378 1.8.2.3 bouyer * According to OHCI spec 6.1.1 "busReset",
379 1.8.2.3 bouyer * All asynchronous transmit must be stopped before
380 1.8.2.3 bouyer * clearing BusReset. Moreover, the BusReset
381 1.8.2.3 bouyer * interrupt bit should not be cleared during the
382 1.8.2.3 bouyer * SelfID phase. Thus we turned off interrupt mask
383 1.8.2.3 bouyer * bit of BusReset instead until SelfID completion
384 1.8.2.3 bouyer * or SelfID timeout.
385 1.8.2.3 bouyer */
386 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear,
387 1.8.2.3 bouyer OHCI_Int_BusReset);
388 1.8.2.3 bouyer intmask &= OHCI_Int_SelfIDComplete;
389 1.8.2.3 bouyer fwohci_buf_stop(sc);
390 1.8.2.3 bouyer fwohci_buf_init(sc);
391 1.8.2.3 bouyer if (sc->sc_uidtbl != NULL) {
392 1.8.2.3 bouyer free(sc->sc_uidtbl, M_DEVBUF);
393 1.8.2.3 bouyer sc->sc_uidtbl = NULL;
394 1.8.2.3 bouyer }
395 1.8.2.3 bouyer callout_reset(&sc->sc_selfid_callout,
396 1.8.2.3 bouyer OHCI_SELFID_TIMEOUT,
397 1.8.2.3 bouyer (void (*)(void *))fwohci_phy_busreset, sc);
398 1.8.2.3 bouyer sc->sc_nodeid = 0xffff; /* indicate invalid */
399 1.8.2.3 bouyer sc->sc_rootid = 0;
400 1.8.2.3 bouyer sc->sc_irmid = IEEE1394_BCAST_PHY_ID;
401 1.8.2.3 bouyer }
402 1.8.2.3 bouyer
403 1.8.2.3 bouyer if (intmask & OHCI_Int_SelfIDComplete) {
404 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
405 1.8.2.3 bouyer OHCI_Int_BusReset);
406 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet,
407 1.8.2.3 bouyer OHCI_Int_BusReset);
408 1.8.2.3 bouyer callout_stop(&sc->sc_selfid_callout);
409 1.8.2.3 bouyer if (fwohci_selfid_input(sc) == 0) {
410 1.8.2.3 bouyer fwohci_buf_start(sc);
411 1.8.2.3 bouyer fwohci_uid_collect(sc);
412 1.8.2.3 bouyer }
413 1.8.2.3 bouyer }
414 1.8.2.3 bouyer
415 1.8.2.3 bouyer if (intmask & OHCI_Int_ReqTxComplete)
416 1.8.2.3 bouyer fwohci_at_done(sc, sc->sc_ctx_atrq, 0);
417 1.8.2.3 bouyer if (intmask & OHCI_Int_RespTxComplete)
418 1.8.2.3 bouyer fwohci_at_done(sc, sc->sc_ctx_atrs, 0);
419 1.8.2.3 bouyer if (intmask & OHCI_Int_RQPkt)
420 1.8.2.3 bouyer fwohci_arrq_input(sc, sc->sc_ctx_arrq);
421 1.8.2.3 bouyer if (intmask & OHCI_Int_RSPkt)
422 1.8.2.3 bouyer fwohci_arrs_input(sc, sc->sc_ctx_arrs);
423 1.8.2.3 bouyer
424 1.8.2.3 bouyer if (intmask & OHCI_Int_IsochTx) {
425 1.8.2.3 bouyer iso = OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntEventClear);
426 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntEventClear, iso);
427 1.8.2.3 bouyer }
428 1.8.2.3 bouyer if (intmask & OHCI_Int_IsochRx) {
429 1.8.2.3 bouyer iso = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntEventClear);
430 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntEventClear, iso);
431 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
432 1.8.2.3 bouyer if ((iso & (1<<i)) && sc->sc_ctx_ir[i] != NULL)
433 1.8.2.3 bouyer fwohci_ir_input(sc, sc->sc_ctx_ir[i]);
434 1.8.2.3 bouyer }
435 1.8.2.3 bouyer }
436 1.8.2.3 bouyer
437 1.8.2.3 bouyer if (!progress) {
438 1.8.2.3 bouyer sc->sc_intrcnt.ev_count++;
439 1.8.2.3 bouyer progress = 1;
440 1.8.2.3 bouyer }
441 1.8.2.3 bouyer }
442 1.8.2.3 bouyer }
443 1.8.2.3 bouyer
444 1.8.2.3 bouyer #if 0
445 1.8.2.3 bouyer static int
446 1.8.2.3 bouyer fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
447 1.8.2.3 bouyer bus_dmamap_t *mapp, caddr_t *kvap, int flags)
448 1.8.2.3 bouyer {
449 1.8.2.3 bouyer bus_dma_segment_t segs[1];
450 1.8.2.3 bouyer int error, nsegs, steps;
451 1.8.2.3 bouyer
452 1.8.2.3 bouyer steps = 0;
453 1.8.2.3 bouyer error = bus_dmamem_alloc(sc->sc_dmat, size, alignment, alignment,
454 1.8.2.3 bouyer segs, 1, &nsegs, flags);
455 1.8.2.3 bouyer if (error)
456 1.8.2.3 bouyer goto cleanup;
457 1.8.2.3 bouyer
458 1.8.2.3 bouyer steps = 1;
459 1.8.2.3 bouyer error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, segs[0].ds_len,
460 1.8.2.3 bouyer kvap, flags);
461 1.8.2.3 bouyer if (error)
462 1.8.2.3 bouyer goto cleanup;
463 1.8.2.3 bouyer
464 1.8.2.3 bouyer if (error == 0)
465 1.8.2.3 bouyer error = bus_dmamap_create(sc->sc_dmat, size, 1, alignment,
466 1.8.2.3 bouyer size, flags, mapp);
467 1.8.2.3 bouyer if (error)
468 1.8.2.3 bouyer goto cleanup;
469 1.8.2.3 bouyer if (error == 0)
470 1.8.2.3 bouyer error = bus_dmamap_load(sc->sc_dmat, *mapp, *kvap, size, NULL, flags);
471 1.8.2.3 bouyer if (error)
472 1.8.2.3 bouyer goto cleanup;
473 1.8.2.3 bouyer
474 1.8.2.3 bouyer cleanup:
475 1.8.2.3 bouyer switch (steps) {
476 1.8.2.3 bouyer case 1:
477 1.8.2.3 bouyer bus_dmamem_free(sc->sc_dmat, segs, nsegs);
478 1.8.2.3 bouyer }
479 1.8.2.3 bouyer
480 1.8.2.3 bouyer return error;
481 1.8.2.3 bouyer }
482 1.8.2.3 bouyer #endif
483 1.8.2.3 bouyer
484 1.8.2.3 bouyer int
485 1.8.2.3 bouyer fwohci_print(void *aux, const char *pnp)
486 1.8.2.3 bouyer {
487 1.8.2.3 bouyer char *name = aux;
488 1.8.2.3 bouyer
489 1.8.2.3 bouyer if (pnp)
490 1.8.2.3 bouyer printf("%s at %s", name, pnp);
491 1.8.2.3 bouyer
492 1.8.2.3 bouyer return UNCONF;
493 1.8.2.3 bouyer }
494 1.8.2.3 bouyer
495 1.8.2.3 bouyer static void
496 1.8.2.3 bouyer fwohci_hw_init(struct fwohci_softc *sc)
497 1.8.2.3 bouyer {
498 1.8.2.3 bouyer int i;
499 1.8.2.3 bouyer u_int32_t val;
500 1.8.2.3 bouyer
501 1.8.2.3 bouyer /*
502 1.8.2.3 bouyer * Software Reset.
503 1.8.2.3 bouyer */
504 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
505 1.8.2.3 bouyer for (i = 0; i < OHCI_LOOP; i++) {
506 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
507 1.8.2.3 bouyer if ((val & OHCI_HCControl_SoftReset) == 0)
508 1.8.2.3 bouyer break;
509 1.8.2.3 bouyer }
510 1.8.2.3 bouyer
511 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
512 1.8.2.3 bouyer
513 1.8.2.3 bouyer /*
514 1.8.2.3 bouyer * First, initilize CSRs with undefined value to default settings.
515 1.8.2.3 bouyer */
516 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
517 1.8.2.3 bouyer val |= OHCI_BusOptions_ISC | OHCI_BusOptions_CMC;
518 1.8.2.3 bouyer #if 0
519 1.8.2.3 bouyer val |= OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC;
520 1.8.2.3 bouyer #else
521 1.8.2.3 bouyer val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC);
522 1.8.2.3 bouyer #endif
523 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
524 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
525 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlClear,
526 1.8.2.3 bouyer ~0);
527 1.8.2.3 bouyer }
528 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear, ~0);
529 1.8.2.3 bouyer
530 1.8.2.3 bouyer fwohci_configrom_init(sc);
531 1.8.2.3 bouyer fwohci_selfid_init(sc);
532 1.8.2.3 bouyer fwohci_buf_init(sc);
533 1.8.2.3 bouyer fwohci_csr_init(sc);
534 1.8.2.3 bouyer
535 1.8.2.3 bouyer /*
536 1.8.2.3 bouyer * Final CSR settings.
537 1.8.2.3 bouyer */
538 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
539 1.8.2.3 bouyer OHCI_LinkControl_CycleTimerEnable |
540 1.8.2.3 bouyer OHCI_LinkControl_RcvSelfID | OHCI_LinkControl_RcvPhyPkt);
541 1.8.2.3 bouyer
542 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_ATRetries, 0x00000888); /*XXX*/
543 1.8.2.3 bouyer
544 1.8.2.3 bouyer /* clear receive filter */
545 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskHiClear, ~0);
546 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskLoClear, ~0);
547 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_AsynchronousRequestFilterHiSet, 0x80000000);
548 1.8.2.3 bouyer
549 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear,
550 1.8.2.3 bouyer OHCI_HCControl_NoByteSwapData | OHCI_HCControl_APhyEnhanceEnable);
551 1.8.2.3 bouyer
552 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, ~0);
553 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset |
554 1.8.2.3 bouyer OHCI_Int_SelfIDComplete | OHCI_Int_IsochRx | OHCI_Int_IsochTx |
555 1.8.2.3 bouyer OHCI_Int_RSPkt | OHCI_Int_RQPkt | OHCI_Int_ARRS | OHCI_Int_ARRQ |
556 1.8.2.3 bouyer OHCI_Int_RespTxComplete | OHCI_Int_ReqTxComplete);
557 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_CycleTooLong |
558 1.8.2.3 bouyer OHCI_Int_UnrecoverableError | OHCI_Int_CycleInconsistent |
559 1.8.2.3 bouyer OHCI_Int_LockRespErr | OHCI_Int_PostedWriteErr);
560 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntMaskSet, ~0);
561 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
562 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_MasterEnable);
563 1.8.2.3 bouyer
564 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LinkEnable);
565 1.8.2.3 bouyer
566 1.8.2.3 bouyer /*
567 1.8.2.3 bouyer * Start the receivers
568 1.8.2.3 bouyer */
569 1.8.2.3 bouyer fwohci_buf_start(sc);
570 1.8.2.3 bouyer }
571 1.8.2.3 bouyer
572 1.8.2.3 bouyer static void
573 1.8.2.3 bouyer fwohci_power(int why, void *arg)
574 1.8.2.3 bouyer {
575 1.8.2.3 bouyer struct fwohci_softc *sc = arg;
576 1.8.2.3 bouyer int s;
577 1.8.2.3 bouyer
578 1.8.2.3 bouyer s = splimp();
579 1.8.2.4 bouyer switch (why) {
580 1.8.2.4 bouyer case PWR_SUSPEND:
581 1.8.2.4 bouyer case PWR_STANDBY:
582 1.8.2.4 bouyer fwohci_shutdown(sc);
583 1.8.2.4 bouyer break;
584 1.8.2.4 bouyer case PWR_RESUME:
585 1.8.2.3 bouyer fwohci_hw_init(sc);
586 1.8.2.3 bouyer fwohci_phy_busreset(sc);
587 1.8.2.4 bouyer break;
588 1.8.2.4 bouyer case PWR_SOFTSUSPEND:
589 1.8.2.4 bouyer case PWR_SOFTSTANDBY:
590 1.8.2.4 bouyer case PWR_SOFTRESUME:
591 1.8.2.4 bouyer break;
592 1.8.2.3 bouyer }
593 1.8.2.3 bouyer splx(s);
594 1.8.2.3 bouyer }
595 1.8.2.3 bouyer
596 1.8.2.3 bouyer static void
597 1.8.2.3 bouyer fwohci_shutdown(void *arg)
598 1.8.2.3 bouyer {
599 1.8.2.3 bouyer struct fwohci_softc *sc = arg;
600 1.8.2.3 bouyer u_int32_t val;
601 1.8.2.3 bouyer
602 1.8.2.3 bouyer callout_stop(&sc->sc_selfid_callout);
603 1.8.2.3 bouyer /* disable all interrupt */
604 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, OHCI_Int_MasterEnable);
605 1.8.2.3 bouyer fwohci_buf_stop(sc);
606 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
607 1.8.2.3 bouyer val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_ISC |
608 1.8.2.3 bouyer OHCI_BusOptions_CMC | OHCI_BusOptions_IRMC);
609 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
610 1.8.2.3 bouyer fwohci_phy_busreset(sc);
611 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear, OHCI_HCControl_LPS);
612 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
613 1.8.2.3 bouyer }
614 1.8.2.3 bouyer
615 1.8.2.3 bouyer /*
616 1.8.2.3 bouyer * COMMON FUNCTIONS
617 1.8.2.3 bouyer */
618 1.8.2.3 bouyer
619 1.8.2.3 bouyer /*
620 1.8.2.3 bouyer * read the PHY Register.
621 1.8.2.3 bouyer */
622 1.8.2.3 bouyer static u_int8_t
623 1.8.2.3 bouyer fwohci_phy_read(struct fwohci_softc *sc, u_int8_t reg)
624 1.8.2.3 bouyer {
625 1.8.2.3 bouyer int i;
626 1.8.2.3 bouyer u_int32_t val;
627 1.8.2.3 bouyer
628 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl,
629 1.8.2.3 bouyer OHCI_PhyControl_RdReg | (reg << OHCI_PhyControl_RegAddr_BITPOS));
630 1.8.2.3 bouyer for (i = 0; i < OHCI_LOOP; i++) {
631 1.8.2.3 bouyer if (OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
632 1.8.2.3 bouyer OHCI_PhyControl_RdDone)
633 1.8.2.3 bouyer break;
634 1.8.2.3 bouyer }
635 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_PhyControl);
636 1.8.2.3 bouyer return (val & OHCI_PhyControl_RdData) >> OHCI_PhyControl_RdData_BITPOS;
637 1.8.2.3 bouyer }
638 1.8.2.3 bouyer
639 1.8.2.3 bouyer /*
640 1.8.2.3 bouyer * write the PHY Register.
641 1.8.2.3 bouyer */
642 1.8.2.3 bouyer static void
643 1.8.2.3 bouyer fwohci_phy_write(struct fwohci_softc *sc, u_int8_t reg, u_int8_t val)
644 1.8.2.3 bouyer {
645 1.8.2.3 bouyer int i;
646 1.8.2.3 bouyer
647 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl, OHCI_PhyControl_WrReg |
648 1.8.2.3 bouyer (reg << OHCI_PhyControl_RegAddr_BITPOS) |
649 1.8.2.3 bouyer (val << OHCI_PhyControl_WrData_BITPOS));
650 1.8.2.3 bouyer for (i = 0; i < OHCI_LOOP; i++) {
651 1.8.2.3 bouyer if (!(OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
652 1.8.2.3 bouyer OHCI_PhyControl_WrReg))
653 1.8.2.3 bouyer break;
654 1.8.2.3 bouyer }
655 1.8.2.3 bouyer }
656 1.8.2.3 bouyer
657 1.8.2.3 bouyer /*
658 1.8.2.3 bouyer * Initiate Bus Reset
659 1.8.2.3 bouyer */
660 1.8.2.3 bouyer static void
661 1.8.2.3 bouyer fwohci_phy_busreset(struct fwohci_softc *sc)
662 1.8.2.3 bouyer {
663 1.8.2.3 bouyer int s;
664 1.8.2.3 bouyer u_int8_t val;
665 1.8.2.3 bouyer
666 1.8.2.3 bouyer s = splimp();
667 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
668 1.8.2.3 bouyer OHCI_Int_BusReset | OHCI_Int_SelfIDComplete);
669 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset);
670 1.8.2.3 bouyer callout_stop(&sc->sc_selfid_callout);
671 1.8.2.3 bouyer val = fwohci_phy_read(sc, 1);
672 1.8.2.3 bouyer val = (val & 0x80) | /* preserve RHB (force root) */
673 1.8.2.3 bouyer 0x40 | /* Initiate Bus Reset */
674 1.8.2.3 bouyer 0x3f; /* default GAP count */
675 1.8.2.3 bouyer fwohci_phy_write(sc, 1, val);
676 1.8.2.3 bouyer splx(s);
677 1.8.2.3 bouyer }
678 1.8.2.3 bouyer
679 1.8.2.3 bouyer /*
680 1.8.2.3 bouyer * PHY Packet
681 1.8.2.3 bouyer */
682 1.8.2.3 bouyer static void
683 1.8.2.3 bouyer fwohci_phy_input(struct fwohci_softc *sc, struct fwohci_pkt *pkt)
684 1.8.2.3 bouyer {
685 1.8.2.3 bouyer u_int32_t val;
686 1.8.2.3 bouyer u_int8_t key, phyid;
687 1.8.2.3 bouyer
688 1.8.2.3 bouyer val = pkt->fp_hdr[1];
689 1.8.2.3 bouyer if (val != ~pkt->fp_hdr[2]) {
690 1.8.2.3 bouyer if (val == 0 && ((*pkt->fp_trail & 0x001f0000) >> 16) ==
691 1.8.2.3 bouyer OHCI_CTXCTL_EVENT_BUS_RESET) {
692 1.8.2.3 bouyer #ifdef FW_DEBUG
693 1.8.2.3 bouyer if (fw_verbose)
694 1.8.2.3 bouyer printf("fwohci_phy_input: BusReset: 0x%08x\n",
695 1.8.2.3 bouyer pkt->fp_hdr[2]);
696 1.8.2.3 bouyer #endif
697 1.8.2.3 bouyer } else {
698 1.8.2.3 bouyer printf("%s: phy packet corrupted (0x%08x, 0x%08x)\n",
699 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, val,
700 1.8.2.3 bouyer pkt->fp_hdr[2]);
701 1.8.2.3 bouyer }
702 1.8.2.3 bouyer return;
703 1.8.2.3 bouyer }
704 1.8.2.3 bouyer key = (val & 0xc0000000) >> 30;
705 1.8.2.3 bouyer phyid = (val & 0x3f000000) >> 24;
706 1.8.2.3 bouyer switch (key) {
707 1.8.2.3 bouyer case 0:
708 1.8.2.3 bouyer #ifdef FW_DEBUG
709 1.8.2.3 bouyer if (fw_verbose) {
710 1.8.2.3 bouyer printf("fwohci_phy_input: PHY Config from %d:", phyid);
711 1.8.2.3 bouyer if (val & 0x00800000)
712 1.8.2.3 bouyer printf(" ForceRoot");
713 1.8.2.3 bouyer if (val & 0x00400000)
714 1.8.2.3 bouyer printf(" Gap=%x", (val & 0x003f0000) >> 16);
715 1.8.2.3 bouyer printf("\n");
716 1.8.2.3 bouyer }
717 1.8.2.3 bouyer #endif
718 1.8.2.3 bouyer break;
719 1.8.2.3 bouyer case 1:
720 1.8.2.3 bouyer #ifdef FW_DEBUG
721 1.8.2.3 bouyer if (fw_verbose)
722 1.8.2.3 bouyer printf("fwohci_phy_input: Link-on from %d\n", phyid);
723 1.8.2.3 bouyer #endif
724 1.8.2.3 bouyer break;
725 1.8.2.3 bouyer case 2:
726 1.8.2.3 bouyer #ifdef FW_DEBUG
727 1.8.2.3 bouyer if (fw_verbose) {
728 1.8.2.3 bouyer printf("fwohci_phy_input: SelfID from %d:", phyid);
729 1.8.2.3 bouyer if (val & 0x00800000) {
730 1.8.2.3 bouyer printf(" #%d", (val & 0x00700000) >> 20);
731 1.8.2.3 bouyer } else {
732 1.8.2.3 bouyer if (val & 0x00400000)
733 1.8.2.3 bouyer printf(" LinkActive");
734 1.8.2.3 bouyer printf(" Gap=%x", (val & 0x003f0000) >> 16);
735 1.8.2.3 bouyer printf(" Spd=S%d",
736 1.8.2.3 bouyer 100 << ((val & 0x0000c000) >> 14));
737 1.8.2.3 bouyer if (val & 0x00000800)
738 1.8.2.3 bouyer printf(" Cont");
739 1.8.2.3 bouyer if (val & 0x00000002)
740 1.8.2.3 bouyer printf(" InitiateBusReset");
741 1.8.2.3 bouyer }
742 1.8.2.3 bouyer if (val & 0x00000001)
743 1.8.2.3 bouyer printf(" +");
744 1.8.2.3 bouyer printf("\n");
745 1.8.2.3 bouyer }
746 1.8.2.3 bouyer #endif
747 1.8.2.3 bouyer break;
748 1.8.2.3 bouyer default:
749 1.8.2.3 bouyer printf("%s: unknown PHY packet: 0x%08x\n",
750 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, val);
751 1.8.2.3 bouyer break;
752 1.8.2.3 bouyer }
753 1.8.2.3 bouyer }
754 1.8.2.3 bouyer
755 1.8.2.3 bouyer /*
756 1.8.2.3 bouyer * Descriptor for context DMA.
757 1.8.2.3 bouyer */
758 1.8.2.3 bouyer static int
759 1.8.2.3 bouyer fwohci_desc_alloc(struct fwohci_softc *sc)
760 1.8.2.3 bouyer {
761 1.8.2.3 bouyer int error, mapsize, dsize;
762 1.8.2.3 bouyer
763 1.8.2.3 bouyer /*
764 1.8.2.3 bouyer * allocate descriptor buffer
765 1.8.2.3 bouyer */
766 1.8.2.3 bouyer
767 1.8.2.3 bouyer sc->sc_descsize = OHCI_BUF_ARRQ_CNT + OHCI_BUF_ARRS_CNT +
768 1.8.2.3 bouyer OHCI_BUF_ATRQ_CNT + OHCI_BUF_ATRS_CNT +
769 1.8.2.3 bouyer OHCI_BUF_IR_CNT * sc->sc_isoctx + 2;
770 1.8.2.3 bouyer dsize = sizeof(struct fwohci_desc) * sc->sc_descsize;
771 1.8.2.3 bouyer mapsize = howmany(sc->sc_descsize, NBBY);
772 1.8.2.3 bouyer sc->sc_descmap = malloc(mapsize, M_DEVBUF, M_WAITOK);
773 1.8.2.3 bouyer memset(sc->sc_descmap, 0, mapsize);
774 1.8.2.3 bouyer
775 1.8.2.3 bouyer if ((error = bus_dmamem_alloc(sc->sc_dmat, dsize, PAGE_SIZE, 0,
776 1.8.2.3 bouyer &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
777 1.8.2.3 bouyer printf("%s: unable to allocate descriptor buffer, error = %d\n",
778 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, error);
779 1.8.2.3 bouyer goto fail_0;
780 1.8.2.3 bouyer }
781 1.8.2.3 bouyer
782 1.8.2.3 bouyer if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
783 1.8.2.3 bouyer dsize, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT | BUS_DMA_WAITOK))
784 1.8.2.3 bouyer != 0) {
785 1.8.2.3 bouyer printf("%s: unable to map descriptor buffer, error = %d\n",
786 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, error);
787 1.8.2.3 bouyer goto fail_1;
788 1.8.2.3 bouyer }
789 1.8.2.3 bouyer
790 1.8.2.3 bouyer if ((error = bus_dmamap_create(sc->sc_dmat, dsize, sc->sc_dnseg,
791 1.8.2.4 bouyer dsize, 0, BUS_DMA_WAITOK, &sc->sc_ddmamap)) != 0) {
792 1.8.2.3 bouyer printf("%s: unable to create descriptor buffer DMA map, "
793 1.8.2.3 bouyer "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
794 1.8.2.3 bouyer goto fail_2;
795 1.8.2.3 bouyer }
796 1.8.2.3 bouyer
797 1.8.2.3 bouyer if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
798 1.8.2.3 bouyer dsize, NULL, BUS_DMA_WAITOK)) != 0) {
799 1.8.2.3 bouyer printf("%s: unable to load descriptor buffer DMA map, "
800 1.8.2.3 bouyer "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
801 1.8.2.3 bouyer goto fail_3;
802 1.8.2.3 bouyer }
803 1.8.2.3 bouyer
804 1.8.2.3 bouyer return 0;
805 1.8.2.3 bouyer
806 1.8.2.3 bouyer fail_3:
807 1.8.2.3 bouyer bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
808 1.8.2.3 bouyer fail_2:
809 1.8.2.3 bouyer bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, dsize);
810 1.8.2.3 bouyer fail_1:
811 1.8.2.3 bouyer bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
812 1.8.2.3 bouyer fail_0:
813 1.8.2.3 bouyer return error;
814 1.8.2.3 bouyer }
815 1.8.2.3 bouyer
816 1.8.2.3 bouyer static struct fwohci_desc *
817 1.8.2.3 bouyer fwohci_desc_get(struct fwohci_softc *sc, int ndesc)
818 1.8.2.3 bouyer {
819 1.8.2.3 bouyer int i, n;
820 1.8.2.3 bouyer
821 1.8.2.3 bouyer for (n = 0; n <= sc->sc_descsize - ndesc; n++) {
822 1.8.2.3 bouyer for (i = 0; ; i++) {
823 1.8.2.3 bouyer if (i == ndesc) {
824 1.8.2.3 bouyer for (i = 0; i < ndesc; i++)
825 1.8.2.3 bouyer setbit(sc->sc_descmap, n + i);
826 1.8.2.3 bouyer return sc->sc_desc + n;
827 1.8.2.3 bouyer }
828 1.8.2.3 bouyer if (isset(sc->sc_descmap, n + i))
829 1.8.2.3 bouyer break;
830 1.8.2.3 bouyer }
831 1.8.2.3 bouyer }
832 1.8.2.3 bouyer return NULL;
833 1.8.2.3 bouyer }
834 1.8.2.3 bouyer
835 1.8.2.3 bouyer static void
836 1.8.2.3 bouyer fwohci_desc_put(struct fwohci_softc *sc, struct fwohci_desc *fd, int ndesc)
837 1.8.2.3 bouyer {
838 1.8.2.3 bouyer int i, n;
839 1.8.2.3 bouyer
840 1.8.2.3 bouyer n = fd - sc->sc_desc;
841 1.8.2.3 bouyer for (i = 0; i < ndesc; i++, n++) {
842 1.8.2.3 bouyer #ifdef DIAGNOSTICS
843 1.8.2.3 bouyer if (isclr(sc->sc_descmap, n))
844 1.8.2.3 bouyer panic("fwohci_desc_put: duplicated free");
845 1.8.2.3 bouyer #endif
846 1.8.2.3 bouyer clrbit(sc->sc_descmap, n);
847 1.8.2.3 bouyer }
848 1.8.2.3 bouyer }
849 1.8.2.3 bouyer
850 1.8.2.3 bouyer /*
851 1.8.2.3 bouyer * Asyncronous/Isochronous Transmit/Receive Context
852 1.8.2.3 bouyer */
853 1.8.2.3 bouyer static int
854 1.8.2.3 bouyer fwohci_ctx_alloc(struct fwohci_softc *sc, struct fwohci_ctx **fcp,
855 1.8.2.3 bouyer int bufcnt, int ctx)
856 1.8.2.3 bouyer {
857 1.8.2.3 bouyer int i, error;
858 1.8.2.3 bouyer struct fwohci_ctx *fc;
859 1.8.2.3 bouyer struct fwohci_buf *fb;
860 1.8.2.3 bouyer struct fwohci_desc *fd;
861 1.8.2.3 bouyer
862 1.8.2.3 bouyer fc = malloc(sizeof(*fc) + sizeof(*fb) * bufcnt, M_DEVBUF, M_WAITOK);
863 1.8.2.3 bouyer memset(fc, 0, sizeof(*fc) + sizeof(*fb) * bufcnt);
864 1.8.2.3 bouyer LIST_INIT(&fc->fc_handler);
865 1.8.2.3 bouyer TAILQ_INIT(&fc->fc_buf);
866 1.8.2.3 bouyer fc->fc_ctx = ctx;
867 1.8.2.3 bouyer fc->fc_bufcnt = bufcnt;
868 1.8.2.3 bouyer fb = (struct fwohci_buf *)&fc[1];
869 1.8.2.3 bouyer for (i = 0; i < bufcnt; i++, fb++) {
870 1.8.2.3 bouyer if ((error = fwohci_buf_alloc(sc, fb)) != 0)
871 1.8.2.3 bouyer goto fail;
872 1.8.2.3 bouyer if ((fd = fwohci_desc_get(sc, 1)) == NULL) {
873 1.8.2.3 bouyer error = ENOBUFS;
874 1.8.2.3 bouyer goto fail;
875 1.8.2.3 bouyer }
876 1.8.2.3 bouyer fb->fb_desc = fd;
877 1.8.2.3 bouyer fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
878 1.8.2.3 bouyer ((caddr_t)fd - (caddr_t)sc->sc_desc);
879 1.8.2.3 bouyer fd->fd_flags = OHCI_DESC_INPUT | OHCI_DESC_STATUS |
880 1.8.2.3 bouyer OHCI_DESC_INTR_ALWAYS | OHCI_DESC_BRANCH;
881 1.8.2.3 bouyer fd->fd_reqcount = fb->fb_dmamap->dm_segs[0].ds_len;
882 1.8.2.3 bouyer fd->fd_data = fb->fb_dmamap->dm_segs[0].ds_addr;
883 1.8.2.3 bouyer TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
884 1.8.2.3 bouyer }
885 1.8.2.3 bouyer *fcp = fc;
886 1.8.2.3 bouyer return 0;
887 1.8.2.3 bouyer
888 1.8.2.3 bouyer fail:
889 1.8.2.3 bouyer while (i-- > 0)
890 1.8.2.3 bouyer fwohci_buf_free(sc, --fb);
891 1.8.2.3 bouyer free(fc, M_DEVBUF);
892 1.8.2.3 bouyer return error;
893 1.8.2.3 bouyer }
894 1.8.2.3 bouyer
895 1.8.2.3 bouyer static void
896 1.8.2.3 bouyer fwohci_ctx_free(struct fwohci_softc *sc, struct fwohci_ctx *fc)
897 1.8.2.3 bouyer {
898 1.8.2.3 bouyer struct fwohci_buf *fb;
899 1.8.2.3 bouyer struct fwohci_handler *fh;
900 1.8.2.3 bouyer
901 1.8.2.3 bouyer while ((fh = LIST_FIRST(&fc->fc_handler)) != NULL)
902 1.8.2.3 bouyer fwohci_handler_set(sc, fh->fh_tcode, fh->fh_key1, fh->fh_key2,
903 1.8.2.3 bouyer NULL, NULL);
904 1.8.2.3 bouyer while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
905 1.8.2.3 bouyer TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
906 1.8.2.3 bouyer fwohci_buf_free(sc, fb);
907 1.8.2.3 bouyer }
908 1.8.2.3 bouyer free(fc, M_DEVBUF);
909 1.8.2.3 bouyer }
910 1.8.2.3 bouyer
911 1.8.2.3 bouyer static void
912 1.8.2.3 bouyer fwohci_ctx_init(struct fwohci_softc *sc, struct fwohci_ctx *fc)
913 1.8.2.3 bouyer {
914 1.8.2.3 bouyer struct fwohci_buf *fb, *nfb;
915 1.8.2.3 bouyer struct fwohci_desc *fd;
916 1.8.2.3 bouyer int n;
917 1.8.2.3 bouyer
918 1.8.2.3 bouyer for (fb = TAILQ_FIRST(&fc->fc_buf); fb != NULL; fb = nfb) {
919 1.8.2.3 bouyer nfb = TAILQ_NEXT(fb, fb_list);
920 1.8.2.3 bouyer fb->fb_off = 0;
921 1.8.2.3 bouyer fd = fb->fb_desc;
922 1.8.2.3 bouyer fd->fd_branch = (nfb != NULL) ? (nfb->fb_daddr | 1) : 0;
923 1.8.2.3 bouyer fd->fd_rescount = fd->fd_reqcount;
924 1.8.2.3 bouyer }
925 1.8.2.3 bouyer
926 1.8.2.3 bouyer n = fc->fc_ctx;
927 1.8.2.3 bouyer fb = TAILQ_FIRST(&fc->fc_buf);
928 1.8.2.3 bouyer if (fc->fc_isoch) {
929 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_CommandPtr,
930 1.8.2.3 bouyer fb->fb_daddr | 1);
931 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlClear,
932 1.8.2.3 bouyer OHCI_CTXCTL_RX_BUFFER_FILL |
933 1.8.2.3 bouyer OHCI_CTXCTL_RX_CYCLE_MATCH_ENABLE |
934 1.8.2.3 bouyer OHCI_CTXCTL_RX_MULTI_CHAN_MODE |
935 1.8.2.3 bouyer OHCI_CTXCTL_RX_DUAL_BUFFER_MODE);
936 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlSet,
937 1.8.2.3 bouyer OHCI_CTXCTL_RX_ISOCH_HEADER);
938 1.8.2.3 bouyer } else {
939 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, n, OHCI_SUBREG_CommandPtr,
940 1.8.2.3 bouyer fb->fb_daddr | 1);
941 1.8.2.3 bouyer }
942 1.8.2.3 bouyer }
943 1.8.2.3 bouyer
944 1.8.2.3 bouyer /*
945 1.8.2.3 bouyer * DMA data buffer
946 1.8.2.3 bouyer */
947 1.8.2.3 bouyer static int
948 1.8.2.3 bouyer fwohci_buf_alloc(struct fwohci_softc *sc, struct fwohci_buf *fb)
949 1.8.2.3 bouyer {
950 1.8.2.3 bouyer int error;
951 1.8.2.3 bouyer
952 1.8.2.3 bouyer if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
953 1.8.2.3 bouyer PAGE_SIZE, &fb->fb_seg, 1, &fb->fb_nseg, BUS_DMA_WAITOK)) != 0) {
954 1.8.2.3 bouyer printf("%s: unable to allocate buffer, error = %d\n",
955 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, error);
956 1.8.2.3 bouyer goto fail_0;
957 1.8.2.3 bouyer }
958 1.8.2.3 bouyer
959 1.8.2.3 bouyer if ((error = bus_dmamem_map(sc->sc_dmat, &fb->fb_seg,
960 1.8.2.3 bouyer fb->fb_nseg, PAGE_SIZE, &fb->fb_buf, BUS_DMA_WAITOK)) != 0) {
961 1.8.2.3 bouyer printf("%s: unable to map buffer, error = %d\n",
962 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, error);
963 1.8.2.3 bouyer goto fail_1;
964 1.8.2.3 bouyer }
965 1.8.2.3 bouyer
966 1.8.2.3 bouyer if ((error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, fb->fb_nseg,
967 1.8.2.3 bouyer PAGE_SIZE, 0, BUS_DMA_WAITOK, &fb->fb_dmamap)) != 0) {
968 1.8.2.3 bouyer printf("%s: unable to create buffer DMA map, "
969 1.8.2.3 bouyer "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
970 1.8.2.3 bouyer error);
971 1.8.2.3 bouyer goto fail_2;
972 1.8.2.3 bouyer }
973 1.8.2.3 bouyer
974 1.8.2.3 bouyer if ((error = bus_dmamap_load(sc->sc_dmat, fb->fb_dmamap,
975 1.8.2.3 bouyer fb->fb_buf, PAGE_SIZE, NULL, BUS_DMA_WAITOK)) != 0) {
976 1.8.2.3 bouyer printf("%s: unable to load buffer DMA map, "
977 1.8.2.3 bouyer "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
978 1.8.2.3 bouyer error);
979 1.8.2.3 bouyer goto fail_3;
980 1.8.2.3 bouyer }
981 1.8.2.3 bouyer
982 1.8.2.3 bouyer return 0;
983 1.8.2.3 bouyer
984 1.8.2.3 bouyer bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
985 1.8.2.3 bouyer fail_3:
986 1.8.2.3 bouyer bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
987 1.8.2.3 bouyer fail_2:
988 1.8.2.3 bouyer bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
989 1.8.2.3 bouyer fail_1:
990 1.8.2.3 bouyer bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
991 1.8.2.3 bouyer fail_0:
992 1.8.2.3 bouyer return error;
993 1.8.2.3 bouyer }
994 1.8.2.3 bouyer
995 1.8.2.3 bouyer static void
996 1.8.2.3 bouyer fwohci_buf_free(struct fwohci_softc *sc, struct fwohci_buf *fb)
997 1.8.2.3 bouyer {
998 1.8.2.3 bouyer
999 1.8.2.3 bouyer bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
1000 1.8.2.3 bouyer bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
1001 1.8.2.3 bouyer bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
1002 1.8.2.3 bouyer bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
1003 1.8.2.3 bouyer }
1004 1.8.2.3 bouyer
1005 1.8.2.3 bouyer static void
1006 1.8.2.3 bouyer fwohci_buf_init(struct fwohci_softc *sc)
1007 1.8.2.3 bouyer {
1008 1.8.2.3 bouyer int i;
1009 1.8.2.3 bouyer
1010 1.8.2.3 bouyer /*
1011 1.8.2.3 bouyer * Initialize for Asynchronous Transmit Queue.
1012 1.8.2.3 bouyer */
1013 1.8.2.3 bouyer fwohci_at_done(sc, sc->sc_ctx_atrq, 1);
1014 1.8.2.3 bouyer fwohci_at_done(sc, sc->sc_ctx_atrs, 1);
1015 1.8.2.3 bouyer
1016 1.8.2.3 bouyer /*
1017 1.8.2.3 bouyer * Initialize for Asynchronous Receive Queue.
1018 1.8.2.3 bouyer */
1019 1.8.2.3 bouyer fwohci_ctx_init(sc, sc->sc_ctx_arrq);
1020 1.8.2.3 bouyer fwohci_ctx_init(sc, sc->sc_ctx_arrs);
1021 1.8.2.3 bouyer
1022 1.8.2.3 bouyer /*
1023 1.8.2.3 bouyer * Initialize for Isochronous Receive Queue.
1024 1.8.2.3 bouyer */
1025 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
1026 1.8.2.3 bouyer if (sc->sc_ctx_ir[i] != NULL)
1027 1.8.2.3 bouyer fwohci_ctx_init(sc, sc->sc_ctx_ir[i]);
1028 1.8.2.3 bouyer }
1029 1.8.2.3 bouyer }
1030 1.8.2.3 bouyer
1031 1.8.2.3 bouyer static void
1032 1.8.2.3 bouyer fwohci_buf_start(struct fwohci_softc *sc)
1033 1.8.2.3 bouyer {
1034 1.8.2.3 bouyer int i;
1035 1.8.2.3 bouyer
1036 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
1037 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
1038 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
1039 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
1040 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
1041 1.8.2.3 bouyer if (sc->sc_ctx_ir[i] != NULL &&
1042 1.8.2.3 bouyer LIST_FIRST(&sc->sc_ctx_ir[i]->fc_handler) != NULL) {
1043 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, i,
1044 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
1045 1.8.2.3 bouyer }
1046 1.8.2.3 bouyer }
1047 1.8.2.3 bouyer }
1048 1.8.2.3 bouyer
1049 1.8.2.3 bouyer static void
1050 1.8.2.3 bouyer fwohci_buf_stop(struct fwohci_softc *sc)
1051 1.8.2.3 bouyer {
1052 1.8.2.3 bouyer int i, j;
1053 1.8.2.3 bouyer
1054 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_REQUEST,
1055 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1056 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
1057 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1058 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
1059 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1060 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
1061 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1062 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
1063 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, i,
1064 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1065 1.8.2.3 bouyer }
1066 1.8.2.3 bouyer
1067 1.8.2.3 bouyer /*
1068 1.8.2.3 bouyer * Make sure the transmitter is stopped.
1069 1.8.2.3 bouyer */
1070 1.8.2.3 bouyer for (j = 0; j < OHCI_LOOP; j++) {
1071 1.8.2.3 bouyer if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_REQUEST,
1072 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
1073 1.8.2.3 bouyer continue;
1074 1.8.2.3 bouyer if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
1075 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
1076 1.8.2.3 bouyer continue;
1077 1.8.2.3 bouyer break;
1078 1.8.2.3 bouyer }
1079 1.8.2.3 bouyer }
1080 1.8.2.3 bouyer
1081 1.8.2.3 bouyer static void
1082 1.8.2.3 bouyer fwohci_buf_next(struct fwohci_softc *sc, struct fwohci_ctx *fc)
1083 1.8.2.3 bouyer {
1084 1.8.2.3 bouyer struct fwohci_buf *fb, *tfb;
1085 1.8.2.3 bouyer
1086 1.8.2.3 bouyer while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
1087 1.8.2.3 bouyer if (fb->fb_off != fb->fb_desc->fd_reqcount ||
1088 1.8.2.3 bouyer fb->fb_desc->fd_rescount != 0)
1089 1.8.2.3 bouyer break;
1090 1.8.2.3 bouyer TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
1091 1.8.2.3 bouyer fb->fb_desc->fd_rescount = fb->fb_desc->fd_reqcount;
1092 1.8.2.3 bouyer fb->fb_off = 0;
1093 1.8.2.3 bouyer fb->fb_desc->fd_branch = 0;
1094 1.8.2.3 bouyer tfb = TAILQ_LAST(&fc->fc_buf, fwohci_buf_s);
1095 1.8.2.3 bouyer tfb->fb_desc->fd_branch = fb->fb_daddr | 1;
1096 1.8.2.3 bouyer TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
1097 1.8.2.3 bouyer }
1098 1.8.2.3 bouyer }
1099 1.8.2.3 bouyer
1100 1.8.2.3 bouyer static int
1101 1.8.2.3 bouyer fwohci_buf_pktget(struct fwohci_softc *sc, struct fwohci_ctx *fc, caddr_t *pp,
1102 1.8.2.3 bouyer int len)
1103 1.8.2.3 bouyer {
1104 1.8.2.3 bouyer struct fwohci_buf *fb;
1105 1.8.2.3 bouyer struct fwohci_desc *fd;
1106 1.8.2.3 bouyer int bufend;
1107 1.8.2.3 bouyer
1108 1.8.2.3 bouyer fb = TAILQ_FIRST(&fc->fc_buf);
1109 1.8.2.3 bouyer again:
1110 1.8.2.3 bouyer fd = fb->fb_desc;
1111 1.8.2.3 bouyer #ifdef FW_DEBUG
1112 1.8.2.3 bouyer if (fw_verbose)
1113 1.8.2.4 bouyer printf("fwohci_buf_pktget: desc %ld, off %d, req %d, res %d,"
1114 1.8.2.3 bouyer " len %d, avail %d\n",
1115 1.8.2.4 bouyer (long)(fd - sc->sc_desc), fb->fb_off, fd->fd_reqcount,
1116 1.8.2.3 bouyer fd->fd_rescount, len,
1117 1.8.2.3 bouyer fd->fd_reqcount - fd->fd_rescount - fb->fb_off);
1118 1.8.2.3 bouyer #endif
1119 1.8.2.3 bouyer bufend = fd->fd_reqcount - fd->fd_rescount;
1120 1.8.2.3 bouyer if (fb->fb_off >= bufend) {
1121 1.8.2.3 bouyer if (fc->fc_isoch && fb->fb_off > 0) {
1122 1.8.2.3 bouyer fb->fb_off = fd->fd_reqcount;
1123 1.8.2.3 bouyer fd->fd_rescount = 0;
1124 1.8.2.3 bouyer }
1125 1.8.2.3 bouyer if (fd->fd_rescount == 0) {
1126 1.8.2.3 bouyer if ((fb = TAILQ_NEXT(fb, fb_list)) != NULL)
1127 1.8.2.3 bouyer goto again;
1128 1.8.2.3 bouyer }
1129 1.8.2.3 bouyer return 0;
1130 1.8.2.3 bouyer }
1131 1.8.2.3 bouyer if (fb->fb_off + len > bufend)
1132 1.8.2.3 bouyer len = bufend - fb->fb_off;
1133 1.8.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, fb->fb_off, len,
1134 1.8.2.3 bouyer BUS_DMASYNC_POSTREAD);
1135 1.8.2.3 bouyer *pp = fb->fb_buf + fb->fb_off;
1136 1.8.2.3 bouyer fb->fb_off += roundup(len, 4);
1137 1.8.2.3 bouyer return len;
1138 1.8.2.3 bouyer }
1139 1.8.2.3 bouyer
1140 1.8.2.3 bouyer static int
1141 1.8.2.3 bouyer fwohci_buf_input(struct fwohci_softc *sc, struct fwohci_ctx *fc,
1142 1.8.2.3 bouyer struct fwohci_pkt *pkt)
1143 1.8.2.3 bouyer {
1144 1.8.2.3 bouyer caddr_t p;
1145 1.8.2.3 bouyer int len, count, i;
1146 1.8.2.3 bouyer
1147 1.8.2.3 bouyer memset(pkt, 0, sizeof(*pkt));
1148 1.8.2.3 bouyer pkt->fp_uio.uio_iov = pkt->fp_iov;
1149 1.8.2.3 bouyer pkt->fp_uio.uio_rw = UIO_WRITE;
1150 1.8.2.3 bouyer pkt->fp_uio.uio_segflg = UIO_SYSSPACE;
1151 1.8.2.3 bouyer
1152 1.8.2.3 bouyer /* get first quadlet */
1153 1.8.2.3 bouyer count = 4;
1154 1.8.2.3 bouyer if (fc->fc_isoch) {
1155 1.8.2.3 bouyer /*
1156 1.8.2.3 bouyer * get trailer first, may be bogus data unless status update
1157 1.8.2.3 bouyer * in descriptor is set.
1158 1.8.2.3 bouyer */
1159 1.8.2.3 bouyer len = fwohci_buf_pktget(sc, fc, (caddr_t *)&pkt->fp_trail,
1160 1.8.2.4 bouyer sizeof(*pkt->fp_trail));
1161 1.8.2.3 bouyer if (len <= 0) {
1162 1.8.2.3 bouyer #ifdef FW_DEBUG
1163 1.8.2.3 bouyer if (fw_verbose)
1164 1.8.2.3 bouyer printf("fwohci_buf_input: no input for is#%d\n",
1165 1.8.2.3 bouyer fc->fc_ctx);
1166 1.8.2.3 bouyer #endif
1167 1.8.2.3 bouyer return 0;
1168 1.8.2.3 bouyer }
1169 1.8.2.3 bouyer *pkt->fp_trail = (*pkt->fp_trail & 0xffff) |
1170 1.8.2.3 bouyer (TAILQ_FIRST(&fc->fc_buf)->fb_desc->fd_status << 16);
1171 1.8.2.3 bouyer }
1172 1.8.2.3 bouyer len = fwohci_buf_pktget(sc, fc, &p, count);
1173 1.8.2.3 bouyer if (len <= 0) {
1174 1.8.2.3 bouyer #ifdef FW_DEBUG
1175 1.8.2.3 bouyer if (fw_verbose)
1176 1.8.2.3 bouyer printf("fwohci_buf_input: no input for %d\n",
1177 1.8.2.3 bouyer fc->fc_ctx);
1178 1.8.2.3 bouyer #endif
1179 1.8.2.3 bouyer return 0;
1180 1.8.2.3 bouyer }
1181 1.8.2.3 bouyer pkt->fp_hdr[0] = *(u_int32_t *)p;
1182 1.8.2.3 bouyer pkt->fp_tcode = (pkt->fp_hdr[0] & 0x000000f0) >> 4;
1183 1.8.2.3 bouyer switch (pkt->fp_tcode) {
1184 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_REQ_QUAD:
1185 1.8.2.3 bouyer case IEEE1394_TCODE_READ_RESP_QUAD:
1186 1.8.2.3 bouyer pkt->fp_hlen = 12;
1187 1.8.2.3 bouyer pkt->fp_dlen = 4;
1188 1.8.2.3 bouyer break;
1189 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_REQ_BLOCK:
1190 1.8.2.3 bouyer case IEEE1394_TCODE_READ_RESP_BLOCK:
1191 1.8.2.3 bouyer case IEEE1394_TCODE_LOCK_REQ:
1192 1.8.2.3 bouyer case IEEE1394_TCODE_LOCK_RESP:
1193 1.8.2.3 bouyer pkt->fp_hlen = 16;
1194 1.8.2.3 bouyer break;
1195 1.8.2.3 bouyer case IEEE1394_TCODE_STREAM_DATA:
1196 1.8.2.3 bouyer pkt->fp_hlen = 4;
1197 1.8.2.3 bouyer pkt->fp_dlen = pkt->fp_hdr[0] >> 16;
1198 1.8.2.3 bouyer break;
1199 1.8.2.3 bouyer default:
1200 1.8.2.3 bouyer pkt->fp_hlen = 12;
1201 1.8.2.3 bouyer pkt->fp_dlen = 0;
1202 1.8.2.3 bouyer break;
1203 1.8.2.3 bouyer }
1204 1.8.2.3 bouyer
1205 1.8.2.3 bouyer /* get header */
1206 1.8.2.3 bouyer while (count < pkt->fp_hlen) {
1207 1.8.2.3 bouyer len = fwohci_buf_pktget(sc, fc, &p, pkt->fp_hlen - count);
1208 1.8.2.3 bouyer if (len == 0) {
1209 1.8.2.3 bouyer printf("fwohci_buf_input: malformed input 1: %d\n",
1210 1.8.2.3 bouyer pkt->fp_hlen - count);
1211 1.8.2.3 bouyer return 0;
1212 1.8.2.3 bouyer }
1213 1.8.2.3 bouyer memcpy((caddr_t)pkt->fp_hdr + count, p, len);
1214 1.8.2.3 bouyer count += len;
1215 1.8.2.3 bouyer }
1216 1.8.2.3 bouyer if (pkt->fp_hlen == 16)
1217 1.8.2.3 bouyer pkt->fp_dlen = pkt->fp_hdr[3] >> 16;
1218 1.8.2.3 bouyer #ifdef FW_DEBUG
1219 1.8.2.3 bouyer if (fw_verbose)
1220 1.8.2.3 bouyer printf("fwohci_buf_input: tcode=0x%x, hlen=%d, dlen=%d\n",
1221 1.8.2.3 bouyer pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen);
1222 1.8.2.3 bouyer #endif
1223 1.8.2.3 bouyer
1224 1.8.2.3 bouyer /* get data */
1225 1.8.2.3 bouyer count = 0;
1226 1.8.2.3 bouyer i = 0;
1227 1.8.2.3 bouyer while (count < pkt->fp_dlen) {
1228 1.8.2.3 bouyer len = fwohci_buf_pktget(sc, fc,
1229 1.8.2.3 bouyer (caddr_t *)&pkt->fp_iov[i].iov_base,
1230 1.8.2.3 bouyer pkt->fp_dlen - count);
1231 1.8.2.3 bouyer if (len == 0) {
1232 1.8.2.3 bouyer printf("fwohci_buf_input: malformed input 2: %d\n",
1233 1.8.2.3 bouyer pkt->fp_hlen - count);
1234 1.8.2.3 bouyer return 0;
1235 1.8.2.3 bouyer }
1236 1.8.2.3 bouyer pkt->fp_iov[i++].iov_len = len;
1237 1.8.2.3 bouyer count += len;
1238 1.8.2.3 bouyer }
1239 1.8.2.3 bouyer pkt->fp_uio.uio_iovcnt = i;
1240 1.8.2.3 bouyer pkt->fp_uio.uio_resid = count;
1241 1.8.2.3 bouyer
1242 1.8.2.3 bouyer if (!fc->fc_isoch) {
1243 1.8.2.3 bouyer /* get trailer */
1244 1.8.2.3 bouyer len = fwohci_buf_pktget(sc, fc, (caddr_t *)&pkt->fp_trail,
1245 1.8.2.4 bouyer sizeof(*pkt->fp_trail));
1246 1.8.2.3 bouyer if (len <= 0) {
1247 1.8.2.3 bouyer printf("fwohci_buf_input: malformed input 3: %d\n",
1248 1.8.2.3 bouyer pkt->fp_hlen - count);
1249 1.8.2.3 bouyer return 0;
1250 1.8.2.3 bouyer }
1251 1.8.2.3 bouyer }
1252 1.8.2.3 bouyer return 1;
1253 1.8.2.3 bouyer }
1254 1.8.2.3 bouyer
1255 1.8.2.3 bouyer static int
1256 1.8.2.3 bouyer fwohci_handler_set(struct fwohci_softc *sc,
1257 1.8.2.3 bouyer int tcode, u_int32_t key1, u_int32_t key2,
1258 1.8.2.3 bouyer int (*handler)(struct fwohci_softc *, void *, struct fwohci_pkt *),
1259 1.8.2.3 bouyer void *arg)
1260 1.8.2.3 bouyer {
1261 1.8.2.3 bouyer struct fwohci_ctx *fc;
1262 1.8.2.3 bouyer struct fwohci_handler *fh;
1263 1.8.2.3 bouyer int i, j;
1264 1.8.2.3 bouyer
1265 1.8.2.3 bouyer if (tcode == IEEE1394_TCODE_STREAM_DATA) {
1266 1.8.2.3 bouyer j = sc->sc_isoctx;
1267 1.8.2.3 bouyer fh = NULL;
1268 1.8.2.3 bouyer for (i = 0; i < sc->sc_isoctx; i++) {
1269 1.8.2.3 bouyer if ((fc = sc->sc_ctx_ir[i]) == NULL) {
1270 1.8.2.3 bouyer if (j == sc->sc_isoctx)
1271 1.8.2.3 bouyer j = i;
1272 1.8.2.3 bouyer continue;
1273 1.8.2.3 bouyer }
1274 1.8.2.3 bouyer fh = LIST_FIRST(&fc->fc_handler);
1275 1.8.2.3 bouyer if (fh == NULL) {
1276 1.8.2.3 bouyer j = i;
1277 1.8.2.3 bouyer break;
1278 1.8.2.3 bouyer }
1279 1.8.2.3 bouyer if (fh->fh_tcode == tcode &&
1280 1.8.2.3 bouyer fh->fh_key1 == key1 && fh->fh_key2 == key2)
1281 1.8.2.3 bouyer break;
1282 1.8.2.3 bouyer fh = NULL;
1283 1.8.2.3 bouyer }
1284 1.8.2.3 bouyer if (fh == NULL) {
1285 1.8.2.3 bouyer if (handler == NULL)
1286 1.8.2.3 bouyer return 0;
1287 1.8.2.3 bouyer if (j == sc->sc_isoctx) {
1288 1.8.2.3 bouyer #ifdef FW_DEBUG
1289 1.8.2.3 bouyer if (fw_verbose)
1290 1.8.2.3 bouyer printf("fwohci_handler_set: "
1291 1.8.2.3 bouyer "no more free context\n");
1292 1.8.2.3 bouyer #endif
1293 1.8.2.3 bouyer return ENOMEM;
1294 1.8.2.3 bouyer }
1295 1.8.2.3 bouyer if ((fc = sc->sc_ctx_ir[j]) == NULL) {
1296 1.8.2.3 bouyer fwohci_ctx_alloc(sc, &fc, OHCI_BUF_IR_CNT, j);
1297 1.8.2.3 bouyer fc->fc_isoch = 1;
1298 1.8.2.3 bouyer sc->sc_ctx_ir[j] = fc;
1299 1.8.2.3 bouyer }
1300 1.8.2.3 bouyer }
1301 1.8.2.3 bouyer } else {
1302 1.8.2.3 bouyer switch (tcode) {
1303 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_REQ_QUAD:
1304 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_REQ_BLOCK:
1305 1.8.2.3 bouyer case IEEE1394_TCODE_READ_REQ_QUAD:
1306 1.8.2.3 bouyer case IEEE1394_TCODE_READ_REQ_BLOCK:
1307 1.8.2.3 bouyer case IEEE1394_TCODE_LOCK_REQ:
1308 1.8.2.3 bouyer fc = sc->sc_ctx_arrq;
1309 1.8.2.3 bouyer break;
1310 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_RESP:
1311 1.8.2.3 bouyer case IEEE1394_TCODE_READ_RESP_QUAD:
1312 1.8.2.3 bouyer case IEEE1394_TCODE_READ_RESP_BLOCK:
1313 1.8.2.3 bouyer case IEEE1394_TCODE_LOCK_RESP:
1314 1.8.2.3 bouyer fc = sc->sc_ctx_arrs;
1315 1.8.2.3 bouyer break;
1316 1.8.2.3 bouyer default:
1317 1.8.2.3 bouyer return EIO;
1318 1.8.2.3 bouyer }
1319 1.8.2.3 bouyer for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
1320 1.8.2.3 bouyer fh = LIST_NEXT(fh, fh_list)) {
1321 1.8.2.3 bouyer if (fh->fh_tcode == tcode &&
1322 1.8.2.3 bouyer fh->fh_key1 == key1 && fh->fh_key2 == key2)
1323 1.8.2.3 bouyer break;
1324 1.8.2.3 bouyer }
1325 1.8.2.3 bouyer }
1326 1.8.2.3 bouyer if (handler == NULL) {
1327 1.8.2.3 bouyer if (fh != NULL) {
1328 1.8.2.3 bouyer LIST_REMOVE(fh, fh_list);
1329 1.8.2.3 bouyer free(fh, M_DEVBUF);
1330 1.8.2.3 bouyer }
1331 1.8.2.3 bouyer if (tcode == IEEE1394_TCODE_STREAM_DATA) {
1332 1.8.2.3 bouyer sc->sc_ctx_ir[fc->fc_ctx] = NULL;
1333 1.8.2.3 bouyer fwohci_ctx_free(sc, fc);
1334 1.8.2.3 bouyer }
1335 1.8.2.3 bouyer return 0;
1336 1.8.2.3 bouyer }
1337 1.8.2.3 bouyer if (fh == NULL) {
1338 1.8.2.3 bouyer fh = malloc(sizeof(*fh), M_DEVBUF, M_NOWAIT);
1339 1.8.2.3 bouyer if (fh == NULL)
1340 1.8.2.3 bouyer return ENOMEM;
1341 1.8.2.3 bouyer LIST_INSERT_HEAD(&fc->fc_handler, fh, fh_list);
1342 1.8.2.3 bouyer }
1343 1.8.2.3 bouyer fh->fh_tcode = tcode;
1344 1.8.2.3 bouyer fh->fh_key1 = key1;
1345 1.8.2.3 bouyer fh->fh_key2 = key2;
1346 1.8.2.3 bouyer fh->fh_handler = handler;
1347 1.8.2.3 bouyer fh->fh_handarg = arg;
1348 1.8.2.3 bouyer #ifdef FW_DEBUG
1349 1.8.2.3 bouyer if (fw_verbose)
1350 1.8.2.3 bouyer printf("fwohci_handler_set: ctx %d, tcode %x, key 0x%x, 0x%x\n",
1351 1.8.2.3 bouyer fc->fc_ctx, tcode, key1, key2);
1352 1.8.2.3 bouyer #endif
1353 1.8.2.3 bouyer
1354 1.8.2.3 bouyer if (tcode == IEEE1394_TCODE_STREAM_DATA) {
1355 1.8.2.3 bouyer fwohci_ctx_init(sc, fc);
1356 1.8.2.3 bouyer #ifdef FW_DEBUG
1357 1.8.2.3 bouyer if (fw_verbose)
1358 1.8.2.4 bouyer printf("fwohci_handler_set: SYNC desc %ld\n",
1359 1.8.2.4 bouyer (long)(TAILQ_FIRST(&fc->fc_buf)->fb_desc -
1360 1.8.2.4 bouyer sc->sc_desc));
1361 1.8.2.3 bouyer #endif
1362 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_ContextMatch,
1363 1.8.2.3 bouyer (OHCI_CTXMATCH_TAG0 << key2) | key1);
1364 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
1365 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
1366 1.8.2.3 bouyer }
1367 1.8.2.3 bouyer return 0;
1368 1.8.2.3 bouyer }
1369 1.8.2.3 bouyer
1370 1.8.2.3 bouyer /*
1371 1.8.2.3 bouyer * Asyncronous Receive Requests input frontend.
1372 1.8.2.3 bouyer */
1373 1.8.2.3 bouyer static void
1374 1.8.2.3 bouyer fwohci_arrq_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
1375 1.8.2.3 bouyer {
1376 1.8.2.3 bouyer int rcode;
1377 1.8.2.3 bouyer u_int32_t key1, key2;
1378 1.8.2.3 bouyer struct fwohci_handler *fh;
1379 1.8.2.3 bouyer struct fwohci_pkt pkt, res;
1380 1.8.2.3 bouyer
1381 1.8.2.3 bouyer while (fwohci_buf_input(sc, fc, &pkt)) {
1382 1.8.2.3 bouyer if (pkt.fp_tcode == OHCI_TCODE_PHY) {
1383 1.8.2.3 bouyer fwohci_phy_input(sc, &pkt);
1384 1.8.2.3 bouyer continue;
1385 1.8.2.3 bouyer }
1386 1.8.2.3 bouyer key1 = pkt.fp_hdr[1] & 0xffff;
1387 1.8.2.3 bouyer key2 = pkt.fp_hdr[2];
1388 1.8.2.3 bouyer memset(&res, 0, sizeof(res));
1389 1.8.2.3 bouyer res.fp_uio.uio_rw = UIO_WRITE;
1390 1.8.2.3 bouyer res.fp_uio.uio_segflg = UIO_SYSSPACE;
1391 1.8.2.3 bouyer for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
1392 1.8.2.3 bouyer fh = LIST_NEXT(fh, fh_list)) {
1393 1.8.2.3 bouyer if (pkt.fp_tcode == fh->fh_tcode &&
1394 1.8.2.3 bouyer key1 == fh->fh_key1 &&
1395 1.8.2.3 bouyer key2 == fh->fh_key2) {
1396 1.8.2.3 bouyer rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
1397 1.8.2.3 bouyer &pkt);
1398 1.8.2.3 bouyer break;
1399 1.8.2.3 bouyer }
1400 1.8.2.3 bouyer }
1401 1.8.2.3 bouyer if (fh == NULL) {
1402 1.8.2.3 bouyer rcode = IEEE1394_RCODE_ADDRESS_ERROR;
1403 1.8.2.3 bouyer #ifdef FW_DEBUG
1404 1.8.2.3 bouyer if (fw_verbose)
1405 1.8.2.3 bouyer printf("fwohci_arrq_input: no listener:"
1406 1.8.2.3 bouyer " tcode 0x%x, addr=0x%04x %08x\n",
1407 1.8.2.3 bouyer pkt.fp_tcode, key1, key2);
1408 1.8.2.3 bouyer #endif
1409 1.8.2.3 bouyer }
1410 1.8.2.3 bouyer if (((*pkt.fp_trail & 0x001f0000) >> 16) !=
1411 1.8.2.3 bouyer OHCI_CTXCTL_EVENT_ACK_PENDING)
1412 1.8.2.3 bouyer continue;
1413 1.8.2.3 bouyer if (rcode != -1)
1414 1.8.2.3 bouyer fwohci_atrs_output(sc, rcode, &pkt, &res);
1415 1.8.2.3 bouyer }
1416 1.8.2.3 bouyer fwohci_buf_next(sc, fc);
1417 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1418 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
1419 1.8.2.3 bouyer }
1420 1.8.2.3 bouyer
1421 1.8.2.3 bouyer /*
1422 1.8.2.3 bouyer * Asynchronous Receive Response input frontend.
1423 1.8.2.3 bouyer */
1424 1.8.2.3 bouyer static void
1425 1.8.2.3 bouyer fwohci_arrs_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
1426 1.8.2.3 bouyer {
1427 1.8.2.3 bouyer struct fwohci_pkt pkt;
1428 1.8.2.3 bouyer struct fwohci_handler *fh;
1429 1.8.2.3 bouyer u_int16_t srcid;
1430 1.8.2.3 bouyer int rcode, tlabel;
1431 1.8.2.3 bouyer
1432 1.8.2.3 bouyer while (fwohci_buf_input(sc, fc, &pkt)) {
1433 1.8.2.3 bouyer srcid = pkt.fp_hdr[1] >> 16;
1434 1.8.2.3 bouyer rcode = (pkt.fp_hdr[1] & 0x0000f000) >> 12;
1435 1.8.2.3 bouyer tlabel = (pkt.fp_hdr[0] & 0x0000fc00) >> 10;
1436 1.8.2.3 bouyer #ifdef FW_DEBUG
1437 1.8.2.3 bouyer if (fw_verbose)
1438 1.8.2.3 bouyer printf("fwohci_arrs_input: tcode 0x%x, from 0x%04x,"
1439 1.8.2.3 bouyer " tlabel 0x%x, rcode 0x%x, hlen %d, dlen %d\n",
1440 1.8.2.3 bouyer pkt.fp_tcode, srcid, tlabel, rcode, pkt.fp_hlen,
1441 1.8.2.3 bouyer pkt.fp_dlen);
1442 1.8.2.3 bouyer #endif
1443 1.8.2.3 bouyer for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
1444 1.8.2.3 bouyer fh = LIST_NEXT(fh, fh_list)) {
1445 1.8.2.3 bouyer if (pkt.fp_tcode == fh->fh_tcode &&
1446 1.8.2.3 bouyer (srcid & OHCI_NodeId_NodeNumber) == fh->fh_key1 &&
1447 1.8.2.3 bouyer tlabel == fh->fh_key2) {
1448 1.8.2.3 bouyer (*fh->fh_handler)(sc, fh->fh_handarg, &pkt);
1449 1.8.2.3 bouyer LIST_REMOVE(fh, fh_list);
1450 1.8.2.3 bouyer free(fh, M_DEVBUF);
1451 1.8.2.3 bouyer break;
1452 1.8.2.3 bouyer }
1453 1.8.2.3 bouyer }
1454 1.8.2.3 bouyer #ifdef FW_DEBUG
1455 1.8.2.3 bouyer if (fw_verbose)
1456 1.8.2.3 bouyer if (fh == NULL)
1457 1.8.2.3 bouyer printf("fwohci_arrs_input: no lister\n");
1458 1.8.2.3 bouyer #endif
1459 1.8.2.3 bouyer }
1460 1.8.2.3 bouyer fwohci_buf_next(sc, fc);
1461 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1462 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
1463 1.8.2.3 bouyer }
1464 1.8.2.3 bouyer
1465 1.8.2.3 bouyer /*
1466 1.8.2.3 bouyer * Isochronous Receive input frontend.
1467 1.8.2.3 bouyer */
1468 1.8.2.3 bouyer static void
1469 1.8.2.3 bouyer fwohci_ir_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
1470 1.8.2.3 bouyer {
1471 1.8.2.3 bouyer int rcode, chan, tag;
1472 1.8.2.3 bouyer struct iovec *iov;
1473 1.8.2.3 bouyer struct fwohci_handler *fh;
1474 1.8.2.3 bouyer struct fwohci_pkt pkt;
1475 1.8.2.3 bouyer
1476 1.8.2.3 bouyer while (fwohci_buf_input(sc, fc, &pkt)) {
1477 1.8.2.3 bouyer chan = (pkt.fp_hdr[0] & 0x00003f00) >> 8;
1478 1.8.2.3 bouyer tag = (pkt.fp_hdr[0] & 0x0000c000) >> 14;
1479 1.8.2.3 bouyer #ifdef FW_DEBUG
1480 1.8.2.3 bouyer if (fw_verbose)
1481 1.8.2.3 bouyer printf("fwohci_ir_input: hdr 0x%08x, tcode %d,"
1482 1.8.2.3 bouyer " hlen %d, dlen %d\n", pkt.fp_hdr[0],
1483 1.8.2.3 bouyer pkt.fp_tcode, pkt.fp_hlen, pkt.fp_dlen);
1484 1.8.2.3 bouyer #endif
1485 1.8.2.3 bouyer if (tag == IEEE1394_TAG_GASP) {
1486 1.8.2.3 bouyer /*
1487 1.8.2.3 bouyer * The pkt with tag=3 is GASP format.
1488 1.8.2.3 bouyer * Move GASP header to header part.
1489 1.8.2.3 bouyer */
1490 1.8.2.3 bouyer if (pkt.fp_dlen < 8)
1491 1.8.2.3 bouyer continue;
1492 1.8.2.3 bouyer iov = pkt.fp_iov;
1493 1.8.2.3 bouyer /* assuming pkt per buffer mode */
1494 1.8.2.3 bouyer pkt.fp_hdr[1] = ntohl(((u_int32_t *)iov->iov_base)[0]);
1495 1.8.2.3 bouyer pkt.fp_hdr[2] = ntohl(((u_int32_t *)iov->iov_base)[1]);
1496 1.8.2.3 bouyer iov->iov_base = (caddr_t)iov->iov_base + 8;
1497 1.8.2.3 bouyer iov->iov_len -= 8;
1498 1.8.2.3 bouyer pkt.fp_hlen += 8;
1499 1.8.2.3 bouyer pkt.fp_dlen -= 8;
1500 1.8.2.3 bouyer }
1501 1.8.2.3 bouyer for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
1502 1.8.2.3 bouyer fh = LIST_NEXT(fh, fh_list)) {
1503 1.8.2.3 bouyer if (pkt.fp_tcode == fh->fh_tcode &&
1504 1.8.2.3 bouyer chan == fh->fh_key1 && tag == fh->fh_key2) {
1505 1.8.2.3 bouyer rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
1506 1.8.2.3 bouyer &pkt);
1507 1.8.2.3 bouyer break;
1508 1.8.2.3 bouyer }
1509 1.8.2.3 bouyer }
1510 1.8.2.3 bouyer #ifdef FW_DEBUG
1511 1.8.2.3 bouyer if (fw_verbose) {
1512 1.8.2.3 bouyer if (fh == NULL)
1513 1.8.2.3 bouyer printf("fwohci_ir_input: no handler\n");
1514 1.8.2.3 bouyer else
1515 1.8.2.3 bouyer printf("fwohci_ir_input: rcode %d\n", rcode);
1516 1.8.2.3 bouyer }
1517 1.8.2.3 bouyer #endif
1518 1.8.2.3 bouyer }
1519 1.8.2.3 bouyer fwohci_buf_next(sc, fc);
1520 1.8.2.3 bouyer OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_ContextControlSet,
1521 1.8.2.3 bouyer OHCI_CTXCTL_WAKE);
1522 1.8.2.3 bouyer }
1523 1.8.2.3 bouyer
1524 1.8.2.3 bouyer /*
1525 1.8.2.3 bouyer * Asynchronous Transmit common routine.
1526 1.8.2.3 bouyer */
1527 1.8.2.3 bouyer static int
1528 1.8.2.3 bouyer fwohci_at_output(struct fwohci_softc *sc, struct fwohci_ctx *fc,
1529 1.8.2.3 bouyer struct fwohci_pkt *pkt)
1530 1.8.2.3 bouyer {
1531 1.8.2.3 bouyer struct fwohci_buf *fb;
1532 1.8.2.3 bouyer struct fwohci_desc *fd;
1533 1.8.2.3 bouyer struct mbuf *m, *m0;
1534 1.8.2.3 bouyer int i, ndesc, error, off, len;
1535 1.8.2.3 bouyer u_int32_t val;
1536 1.8.2.3 bouyer
1537 1.8.2.3 bouyer if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) > sc->sc_rootid) {
1538 1.8.2.3 bouyer /* We can't send anything during selfid duration */
1539 1.8.2.3 bouyer return EAGAIN;
1540 1.8.2.3 bouyer }
1541 1.8.2.3 bouyer #ifdef FW_DEBUG
1542 1.8.2.3 bouyer if (fw_verbose) {
1543 1.8.2.3 bouyer struct iovec *iov;
1544 1.8.2.3 bouyer printf("fwohci_at_output: tcode 0x%x, hlen %d, dlen %d",
1545 1.8.2.3 bouyer pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen);
1546 1.8.2.3 bouyer if (fw_dump) {
1547 1.8.2.3 bouyer for (i = 0; i < pkt->fp_hlen/4; i++)
1548 1.8.2.3 bouyer printf("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]);
1549 1.8.2.3 bouyer printf("$");
1550 1.8.2.3 bouyer for (ndesc = 0, iov = pkt->fp_iov;
1551 1.8.2.3 bouyer ndesc < pkt->fp_uio.uio_iovcnt; ndesc++, iov++) {
1552 1.8.2.3 bouyer for (i = 0; i < iov->iov_len; i++)
1553 1.8.2.3 bouyer printf("%s%02x",
1554 1.8.2.3 bouyer (i%32)?((i%4)?"":" "):"\n\t",
1555 1.8.2.3 bouyer ((u_int8_t *)iov->iov_base)[i]);
1556 1.8.2.3 bouyer printf("$");
1557 1.8.2.3 bouyer }
1558 1.8.2.3 bouyer }
1559 1.8.2.3 bouyer printf("\n");
1560 1.8.2.3 bouyer }
1561 1.8.2.3 bouyer #endif
1562 1.8.2.3 bouyer
1563 1.8.2.3 bouyer if ((m = pkt->fp_m) != NULL) {
1564 1.8.2.3 bouyer for (ndesc = 2; m != NULL; m = m->m_next)
1565 1.8.2.3 bouyer ndesc++;
1566 1.8.2.3 bouyer if (ndesc > OHCI_DESC_MAX) {
1567 1.8.2.3 bouyer m0 = NULL;
1568 1.8.2.3 bouyer ndesc = 2;
1569 1.8.2.3 bouyer for (off = 0; off < pkt->fp_dlen; off += len) {
1570 1.8.2.3 bouyer if (m0 == NULL) {
1571 1.8.2.3 bouyer MGETHDR(m0, M_DONTWAIT, MT_DATA);
1572 1.8.2.3 bouyer if (m0 != NULL)
1573 1.8.2.3 bouyer M_COPY_PKTHDR(m0, pkt->fp_m);
1574 1.8.2.3 bouyer m = m0;
1575 1.8.2.3 bouyer } else {
1576 1.8.2.3 bouyer MGET(m->m_next, M_DONTWAIT, MT_DATA);
1577 1.8.2.3 bouyer m = m->m_next;
1578 1.8.2.3 bouyer }
1579 1.8.2.3 bouyer if (m != NULL)
1580 1.8.2.3 bouyer MCLGET(m, M_DONTWAIT);
1581 1.8.2.3 bouyer if (m == NULL || (m->m_flags & M_EXT) == 0) {
1582 1.8.2.3 bouyer m_freem(m0);
1583 1.8.2.3 bouyer return ENOMEM;
1584 1.8.2.3 bouyer }
1585 1.8.2.3 bouyer len = pkt->fp_dlen - off;
1586 1.8.2.3 bouyer if (len > m->m_ext.ext_size)
1587 1.8.2.3 bouyer len = m->m_ext.ext_size;
1588 1.8.2.3 bouyer m_copydata(pkt->fp_m, off, len,
1589 1.8.2.3 bouyer mtod(m, caddr_t));
1590 1.8.2.3 bouyer ndesc++;
1591 1.8.2.3 bouyer }
1592 1.8.2.3 bouyer m_freem(pkt->fp_m);
1593 1.8.2.3 bouyer pkt->fp_m = m0;
1594 1.8.2.3 bouyer }
1595 1.8.2.3 bouyer } else
1596 1.8.2.3 bouyer ndesc = 2 + pkt->fp_uio.uio_iovcnt;
1597 1.8.2.3 bouyer
1598 1.8.2.3 bouyer if (ndesc > OHCI_DESC_MAX)
1599 1.8.2.3 bouyer return ENOBUFS;
1600 1.8.2.3 bouyer
1601 1.8.2.3 bouyer if (fc->fc_bufcnt > 50) /*XXX*/
1602 1.8.2.3 bouyer return ENOBUFS;
1603 1.8.2.3 bouyer if ((fb = malloc(sizeof(*fb), M_DEVBUF, M_NOWAIT)) == NULL)
1604 1.8.2.3 bouyer return ENOBUFS;
1605 1.8.2.3 bouyer fb->fb_nseg = ndesc;
1606 1.8.2.3 bouyer fb->fb_desc = fwohci_desc_get(sc, ndesc);
1607 1.8.2.3 bouyer if (fb->fb_desc == NULL) {
1608 1.8.2.3 bouyer free(fb, M_DEVBUF);
1609 1.8.2.3 bouyer return ENOBUFS;
1610 1.8.2.3 bouyer }
1611 1.8.2.3 bouyer fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
1612 1.8.2.3 bouyer ((caddr_t)fb->fb_desc - (caddr_t)sc->sc_desc);
1613 1.8.2.3 bouyer fb->fb_m = pkt->fp_m;
1614 1.8.2.3 bouyer fb->fb_callback = pkt->fp_callback;
1615 1.8.2.3 bouyer
1616 1.8.2.3 bouyer if (ndesc > 2) {
1617 1.8.2.3 bouyer if ((error = bus_dmamap_create(sc->sc_dmat, pkt->fp_dlen, ndesc,
1618 1.8.2.3 bouyer PAGE_SIZE, 0, BUS_DMA_NOWAIT, &fb->fb_dmamap)) != 0) {
1619 1.8.2.3 bouyer fwohci_desc_put(sc, fb->fb_desc, ndesc);
1620 1.8.2.3 bouyer free(fb, M_DEVBUF);
1621 1.8.2.3 bouyer return error;
1622 1.8.2.3 bouyer }
1623 1.8.2.3 bouyer
1624 1.8.2.3 bouyer if (pkt->fp_m != NULL)
1625 1.8.2.3 bouyer error = bus_dmamap_load_mbuf(sc->sc_dmat, fb->fb_dmamap,
1626 1.8.2.3 bouyer pkt->fp_m, BUS_DMA_NOWAIT);
1627 1.8.2.3 bouyer else
1628 1.8.2.3 bouyer error = bus_dmamap_load_uio(sc->sc_dmat, fb->fb_dmamap,
1629 1.8.2.3 bouyer &pkt->fp_uio, BUS_DMA_NOWAIT);
1630 1.8.2.3 bouyer if (error != 0) {
1631 1.8.2.3 bouyer bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
1632 1.8.2.3 bouyer fwohci_desc_put(sc, fb->fb_desc, ndesc);
1633 1.8.2.3 bouyer free(fb, M_DEVBUF);
1634 1.8.2.3 bouyer return error;
1635 1.8.2.3 bouyer }
1636 1.8.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0, pkt->fp_dlen,
1637 1.8.2.3 bouyer BUS_DMASYNC_PREWRITE);
1638 1.8.2.3 bouyer }
1639 1.8.2.3 bouyer
1640 1.8.2.3 bouyer fd = fb->fb_desc;
1641 1.8.2.3 bouyer fd->fd_flags = OHCI_DESC_IMMED;
1642 1.8.2.3 bouyer fd->fd_reqcount = pkt->fp_hlen;
1643 1.8.2.3 bouyer fd->fd_data = 0;
1644 1.8.2.3 bouyer fd->fd_branch = 0;
1645 1.8.2.3 bouyer fd->fd_status = 0;
1646 1.8.2.3 bouyer if (fc->fc_ctx == OHCI_CTX_ASYNC_TX_RESPONSE) {
1647 1.8.2.3 bouyer i = 3; /* XXX: 3 sec */
1648 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
1649 1.8.2.3 bouyer fd->fd_timestamp = ((val >> 12) & 0x1fff) |
1650 1.8.2.3 bouyer ((((val >> 25) + i) & 0x7) << 13);
1651 1.8.2.3 bouyer } else
1652 1.8.2.3 bouyer fd->fd_timestamp = 0;
1653 1.8.2.3 bouyer memcpy(fd + 1, pkt->fp_hdr, pkt->fp_hlen);
1654 1.8.2.3 bouyer for (i = 0; i < ndesc - 2; i++) {
1655 1.8.2.3 bouyer fd = fb->fb_desc + 2 + i;
1656 1.8.2.3 bouyer fd->fd_flags = 0;
1657 1.8.2.3 bouyer fd->fd_reqcount = fb->fb_dmamap->dm_segs[i].ds_len;
1658 1.8.2.3 bouyer fd->fd_data = fb->fb_dmamap->dm_segs[i].ds_addr;
1659 1.8.2.3 bouyer fd->fd_branch = 0;
1660 1.8.2.3 bouyer fd->fd_status = 0;
1661 1.8.2.3 bouyer fd->fd_timestamp = 0;
1662 1.8.2.3 bouyer }
1663 1.8.2.3 bouyer fd->fd_flags |= OHCI_DESC_LAST | OHCI_DESC_BRANCH;
1664 1.8.2.3 bouyer fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
1665 1.8.2.3 bouyer
1666 1.8.2.3 bouyer #ifdef FW_DEBUG
1667 1.8.2.3 bouyer if (fw_verbose) {
1668 1.8.2.4 bouyer printf("fwohci_at_output: desc %ld",
1669 1.8.2.4 bouyer (long)(fb->fb_desc - sc->sc_desc));
1670 1.8.2.3 bouyer for (i = 0; i < ndesc * 4; i++)
1671 1.8.2.3 bouyer printf("%s%08x", i&7?" ":"\n\t",
1672 1.8.2.3 bouyer ((u_int32_t *)fb->fb_desc)[i]);
1673 1.8.2.3 bouyer printf("\n");
1674 1.8.2.3 bouyer }
1675 1.8.2.3 bouyer #endif
1676 1.8.2.3 bouyer
1677 1.8.2.3 bouyer val = OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
1678 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear);
1679 1.8.2.3 bouyer
1680 1.8.2.3 bouyer if (val & OHCI_CTXCTL_RUN) {
1681 1.8.2.3 bouyer if (fc->fc_branch == NULL) {
1682 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1683 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1684 1.8.2.3 bouyer goto run;
1685 1.8.2.3 bouyer }
1686 1.8.2.3 bouyer *fc->fc_branch = fb->fb_daddr | ndesc;
1687 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1688 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
1689 1.8.2.3 bouyer } else {
1690 1.8.2.3 bouyer run:
1691 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1692 1.8.2.3 bouyer OHCI_SUBREG_CommandPtr, fb->fb_daddr | ndesc);
1693 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1694 1.8.2.3 bouyer OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
1695 1.8.2.3 bouyer }
1696 1.8.2.3 bouyer fc->fc_branch = &fd->fd_branch;
1697 1.8.2.3 bouyer
1698 1.8.2.3 bouyer fc->fc_bufcnt++;
1699 1.8.2.3 bouyer TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
1700 1.8.2.3 bouyer return 0;
1701 1.8.2.3 bouyer }
1702 1.8.2.3 bouyer
1703 1.8.2.3 bouyer static void
1704 1.8.2.3 bouyer fwohci_at_done(struct fwohci_softc *sc, struct fwohci_ctx *fc, int force)
1705 1.8.2.3 bouyer {
1706 1.8.2.3 bouyer struct fwohci_buf *fb;
1707 1.8.2.3 bouyer struct fwohci_desc *fd;
1708 1.8.2.3 bouyer int i;
1709 1.8.2.3 bouyer
1710 1.8.2.3 bouyer while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
1711 1.8.2.3 bouyer fd = fb->fb_desc;
1712 1.8.2.3 bouyer #ifdef FW_DEBUG
1713 1.8.2.3 bouyer if (fw_verbose) {
1714 1.8.2.4 bouyer printf("fwohci_at_done: %sdesc %ld (%d)",
1715 1.8.2.3 bouyer force ? "force " : "",
1716 1.8.2.4 bouyer (long)(fd - sc->sc_desc), fb->fb_nseg);
1717 1.8.2.3 bouyer for (i = 0; i < fb->fb_nseg * 4; i++)
1718 1.8.2.3 bouyer printf("%s%08x", i&7?" ":"\n ",
1719 1.8.2.3 bouyer ((u_int32_t *)fd)[i]);
1720 1.8.2.3 bouyer printf("\n");
1721 1.8.2.3 bouyer }
1722 1.8.2.3 bouyer #endif
1723 1.8.2.3 bouyer if (fb->fb_nseg > 2)
1724 1.8.2.3 bouyer fd += fb->fb_nseg - 1;
1725 1.8.2.3 bouyer if (!force && !(fd->fd_status & OHCI_CTXCTL_ACTIVE))
1726 1.8.2.3 bouyer break;
1727 1.8.2.3 bouyer TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
1728 1.8.2.3 bouyer if (fc->fc_branch == &fd->fd_branch) {
1729 1.8.2.3 bouyer OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
1730 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
1731 1.8.2.3 bouyer fc->fc_branch = NULL;
1732 1.8.2.3 bouyer for (i = 0; i < OHCI_LOOP; i++) {
1733 1.8.2.3 bouyer if (!(OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
1734 1.8.2.3 bouyer OHCI_SUBREG_ContextControlClear) &
1735 1.8.2.3 bouyer OHCI_CTXCTL_ACTIVE))
1736 1.8.2.3 bouyer break;
1737 1.8.2.3 bouyer }
1738 1.8.2.3 bouyer }
1739 1.8.2.3 bouyer fwohci_desc_put(sc, fb->fb_desc, fb->fb_nseg);
1740 1.8.2.3 bouyer if (fb->fb_nseg > 2)
1741 1.8.2.3 bouyer bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
1742 1.8.2.3 bouyer fc->fc_bufcnt--;
1743 1.8.2.3 bouyer if (fb->fb_callback != NULL) {
1744 1.8.2.3 bouyer (*fb->fb_callback)(sc->sc_sc1394.sc1394_if, fb->fb_m);
1745 1.8.2.3 bouyer fb->fb_callback = NULL;
1746 1.8.2.3 bouyer } else if (fb->fb_m != NULL)
1747 1.8.2.3 bouyer m_freem(fb->fb_m);
1748 1.8.2.3 bouyer free(fb, M_DEVBUF);
1749 1.8.2.3 bouyer }
1750 1.8.2.3 bouyer }
1751 1.8.2.3 bouyer
1752 1.8.2.3 bouyer /*
1753 1.8.2.3 bouyer * Asynchronous Transmit Reponse -- in response of request packet.
1754 1.8.2.3 bouyer */
1755 1.8.2.3 bouyer static void
1756 1.8.2.3 bouyer fwohci_atrs_output(struct fwohci_softc *sc, int rcode, struct fwohci_pkt *req,
1757 1.8.2.3 bouyer struct fwohci_pkt *res)
1758 1.8.2.3 bouyer {
1759 1.8.2.3 bouyer
1760 1.8.2.3 bouyer if (((*req->fp_trail & 0x001f0000) >> 16) !=
1761 1.8.2.3 bouyer OHCI_CTXCTL_EVENT_ACK_PENDING)
1762 1.8.2.3 bouyer return;
1763 1.8.2.3 bouyer
1764 1.8.2.3 bouyer res->fp_hdr[0] = (req->fp_hdr[0] & 0x0000fc00) | 0x00000100;
1765 1.8.2.3 bouyer res->fp_hdr[1] = (req->fp_hdr[1] & 0xffff0000) | (rcode << 12);
1766 1.8.2.3 bouyer switch (req->fp_tcode) {
1767 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_REQ_QUAD:
1768 1.8.2.3 bouyer case IEEE1394_TCODE_WRITE_REQ_BLOCK:
1769 1.8.2.3 bouyer res->fp_tcode = IEEE1394_TCODE_WRITE_RESP;
1770 1.8.2.3 bouyer res->fp_hlen = 12;
1771 1.8.2.3 bouyer break;
1772 1.8.2.3 bouyer case IEEE1394_TCODE_READ_REQ_QUAD:
1773 1.8.2.3 bouyer res->fp_tcode = IEEE1394_TCODE_READ_RESP_QUAD;
1774 1.8.2.3 bouyer res->fp_hlen = 16;
1775 1.8.2.3 bouyer res->fp_dlen = 0;
1776 1.8.2.3 bouyer if (res->fp_uio.uio_iovcnt == 1 && res->fp_iov[0].iov_len == 4)
1777 1.8.2.3 bouyer res->fp_hdr[3] =
1778 1.8.2.3 bouyer *(u_int32_t *)res->fp_iov[0].iov_base;
1779 1.8.2.3 bouyer res->fp_uio.uio_iovcnt = 0;
1780 1.8.2.3 bouyer break;
1781 1.8.2.3 bouyer case IEEE1394_TCODE_READ_REQ_BLOCK:
1782 1.8.2.3 bouyer case IEEE1394_TCODE_LOCK_REQ:
1783 1.8.2.3 bouyer if (req->fp_tcode == IEEE1394_TCODE_LOCK_REQ)
1784 1.8.2.3 bouyer res->fp_tcode = IEEE1394_TCODE_LOCK_RESP;
1785 1.8.2.3 bouyer else
1786 1.8.2.3 bouyer res->fp_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
1787 1.8.2.3 bouyer res->fp_hlen = 16;
1788 1.8.2.3 bouyer res->fp_dlen = res->fp_uio.uio_resid;
1789 1.8.2.3 bouyer res->fp_hdr[3] = res->fp_dlen << 16;
1790 1.8.2.3 bouyer break;
1791 1.8.2.3 bouyer }
1792 1.8.2.3 bouyer res->fp_hdr[0] |= (res->fp_tcode << 4);
1793 1.8.2.3 bouyer fwohci_at_output(sc, sc->sc_ctx_atrs, res);
1794 1.8.2.3 bouyer }
1795 1.8.2.3 bouyer
1796 1.8.2.3 bouyer /*
1797 1.8.2.3 bouyer * APPLICATION LAYER SERVICES
1798 1.8.2.3 bouyer */
1799 1.8.2.3 bouyer
1800 1.8.2.3 bouyer /*
1801 1.8.2.3 bouyer * Initialization for Configuration ROM (no DMA context)
1802 1.8.2.3 bouyer */
1803 1.8.2.3 bouyer
1804 1.8.2.3 bouyer #define CFR_MAXUNIT 20
1805 1.8.2.3 bouyer
1806 1.8.2.3 bouyer struct configromctx {
1807 1.8.2.3 bouyer u_int32_t *ptr;
1808 1.8.2.3 bouyer int curunit;
1809 1.8.2.3 bouyer struct {
1810 1.8.2.3 bouyer u_int32_t *start;
1811 1.8.2.3 bouyer int length;
1812 1.8.2.3 bouyer u_int32_t *refer;
1813 1.8.2.3 bouyer int refunit;
1814 1.8.2.3 bouyer } unit[CFR_MAXUNIT];
1815 1.8.2.3 bouyer };
1816 1.8.2.3 bouyer
1817 1.8.2.3 bouyer #define CFR_PUT_DATA4(cfr, d1, d2, d3, d4) \
1818 1.8.2.3 bouyer (*(cfr)->ptr++ = (((d1)<<24) | ((d2)<<16) | ((d3)<<8) | (d4)))
1819 1.8.2.3 bouyer
1820 1.8.2.3 bouyer #define CFR_PUT_DATA1(cfr, d) (*(cfr)->ptr++ = (d))
1821 1.8.2.3 bouyer
1822 1.8.2.3 bouyer #define CFR_PUT_VALUE(cfr, key, d) (*(cfr)->ptr++ = ((key)<<24) | (d))
1823 1.8.2.3 bouyer
1824 1.8.2.3 bouyer #define CFR_PUT_CRC(cfr, n) \
1825 1.8.2.3 bouyer (*(cfr)->unit[n].start = ((cfr)->unit[n].length << 16) | \
1826 1.8.2.3 bouyer fwohci_crc16((cfr)->unit[n].start + 1, (cfr)->unit[n].length))
1827 1.8.2.3 bouyer
1828 1.8.2.3 bouyer #define CFR_START_UNIT(cfr, n) \
1829 1.8.2.3 bouyer do { \
1830 1.8.2.3 bouyer if ((cfr)->unit[n].refer != NULL) { \
1831 1.8.2.3 bouyer *(cfr)->unit[n].refer |= \
1832 1.8.2.3 bouyer (cfr)->ptr - (cfr)->unit[n].refer; \
1833 1.8.2.3 bouyer CFR_PUT_CRC(cfr, (cfr)->unit[n].refunit); \
1834 1.8.2.3 bouyer } \
1835 1.8.2.3 bouyer (cfr)->curunit = (n); \
1836 1.8.2.3 bouyer (cfr)->unit[n].start = (cfr)->ptr++; \
1837 1.8.2.3 bouyer } while (0 /* CONSTCOND */)
1838 1.8.2.3 bouyer
1839 1.8.2.3 bouyer #define CFR_PUT_REFER(cfr, key, n) \
1840 1.8.2.3 bouyer do { \
1841 1.8.2.3 bouyer (cfr)->unit[n].refer = (cfr)->ptr; \
1842 1.8.2.3 bouyer (cfr)->unit[n].refunit = (cfr)->curunit; \
1843 1.8.2.3 bouyer *(cfr)->ptr++ = (key) << 24; \
1844 1.8.2.3 bouyer } while (0 /* CONSTCOND */)
1845 1.8.2.3 bouyer
1846 1.8.2.3 bouyer #define CFR_END_UNIT(cfr) \
1847 1.8.2.3 bouyer do { \
1848 1.8.2.3 bouyer (cfr)->unit[(cfr)->curunit].length = (cfr)->ptr - \
1849 1.8.2.3 bouyer ((cfr)->unit[(cfr)->curunit].start + 1); \
1850 1.8.2.3 bouyer CFR_PUT_CRC(cfr, (cfr)->curunit); \
1851 1.8.2.3 bouyer } while (0 /* CONSTCOND */)
1852 1.8.2.3 bouyer
1853 1.8.2.3 bouyer static u_int16_t
1854 1.8.2.3 bouyer fwohci_crc16(u_int32_t *ptr, int len)
1855 1.8.2.3 bouyer {
1856 1.8.2.3 bouyer int shift;
1857 1.8.2.3 bouyer u_int32_t crc, sum, data;
1858 1.8.2.3 bouyer
1859 1.8.2.3 bouyer crc = 0;
1860 1.8.2.3 bouyer while (len-- > 0) {
1861 1.8.2.3 bouyer data = *ptr++;
1862 1.8.2.3 bouyer for (shift = 28; shift >= 0; shift -= 4) {
1863 1.8.2.3 bouyer sum = ((crc >> 12) ^ (data >> shift)) & 0x000f;
1864 1.8.2.3 bouyer crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
1865 1.8.2.3 bouyer }
1866 1.8.2.3 bouyer crc &= 0xffff;
1867 1.8.2.3 bouyer }
1868 1.8.2.3 bouyer return crc;
1869 1.8.2.3 bouyer }
1870 1.8.2.3 bouyer
1871 1.8.2.3 bouyer static void
1872 1.8.2.3 bouyer fwohci_configrom_init(struct fwohci_softc *sc)
1873 1.8.2.3 bouyer {
1874 1.8.2.3 bouyer int i;
1875 1.8.2.3 bouyer struct fwohci_buf *fb;
1876 1.8.2.3 bouyer u_int32_t *hdr;
1877 1.8.2.3 bouyer struct configromctx cfr;
1878 1.8.2.3 bouyer
1879 1.8.2.3 bouyer fb = &sc->sc_buf_cnfrom;
1880 1.8.2.3 bouyer memset(&cfr, 0, sizeof(cfr));
1881 1.8.2.3 bouyer cfr.ptr = hdr = (u_int32_t *)fb->fb_buf;
1882 1.8.2.3 bouyer
1883 1.8.2.3 bouyer /* headers */
1884 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 0);
1885 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusId));
1886 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusOptions));
1887 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDHi));
1888 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDLo));
1889 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1890 1.8.2.3 bouyer /* copy info_length from crc_length */
1891 1.8.2.3 bouyer *hdr |= (*hdr & 0x00ff0000) << 8;
1892 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMhdr, *hdr);
1893 1.8.2.3 bouyer
1894 1.8.2.3 bouyer /* root directory */
1895 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 1);
1896 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0x03, 0x00005e); /* vendor id */
1897 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0x81, 2); /* textual descriptor offset */
1898 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0x0c, 0x0083c0); /* node capability */
1899 1.8.2.3 bouyer /* spt,64,fix,lst,drq */
1900 1.8.2.3 bouyer #ifdef INET
1901 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0xd1, 3); /* IPv4 unit directory */
1902 1.8.2.3 bouyer #endif /* INET */
1903 1.8.2.3 bouyer #ifdef INET6
1904 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0xd1, 4); /* IPv6 unit directory */
1905 1.8.2.3 bouyer #endif /* INET6 */
1906 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1907 1.8.2.3 bouyer
1908 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 2);
1909 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0, 0); /* textual descriptor */
1910 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, 0); /* minimal ASCII */
1911 1.8.2.3 bouyer CFR_PUT_DATA4(&cfr, 'N', 'e', 't', 'B');
1912 1.8.2.3 bouyer CFR_PUT_DATA4(&cfr, 'S', 'D', 0x00, 0x00);
1913 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1914 1.8.2.3 bouyer
1915 1.8.2.3 bouyer #ifdef INET
1916 1.8.2.3 bouyer /* IPv4 unit directory */
1917 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 3);
1918 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0x12, 0x00005e); /* unit spec id */
1919 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0x81, 6); /* textual descriptor offset */
1920 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0x13, 0x000001); /* unit sw version */
1921 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0x81, 7); /* textual descriptor offset */
1922 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1923 1.8.2.3 bouyer
1924 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 6);
1925 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0, 0); /* textual descriptor */
1926 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, 0); /* minimal ASCII */
1927 1.8.2.3 bouyer CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
1928 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1929 1.8.2.3 bouyer
1930 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 7);
1931 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0, 0); /* textual descriptor */
1932 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, 0); /* minimal ASCII */
1933 1.8.2.3 bouyer CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '4');
1934 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1935 1.8.2.3 bouyer #endif /* INET */
1936 1.8.2.3 bouyer
1937 1.8.2.3 bouyer #ifdef INET6
1938 1.8.2.3 bouyer /* IPv6 unit directory */
1939 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 4);
1940 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0x12, 0x00005e); /* unit spec id */
1941 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0x81, 8); /* textual descriptor offset */
1942 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0x13, 0x000002); /* unit sw version */
1943 1.8.2.3 bouyer /* XXX: TBA by IANA */
1944 1.8.2.3 bouyer CFR_PUT_REFER(&cfr, 0x81, 9); /* textual descriptor offset */
1945 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1946 1.8.2.3 bouyer
1947 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 8);
1948 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0, 0); /* textual descriptor */
1949 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, 0); /* minimal ASCII */
1950 1.8.2.3 bouyer CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
1951 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1952 1.8.2.3 bouyer
1953 1.8.2.3 bouyer CFR_START_UNIT(&cfr, 9);
1954 1.8.2.3 bouyer CFR_PUT_VALUE(&cfr, 0, 0); /* textual descriptor */
1955 1.8.2.3 bouyer CFR_PUT_DATA1(&cfr, 0);
1956 1.8.2.3 bouyer CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '6');
1957 1.8.2.3 bouyer CFR_END_UNIT(&cfr);
1958 1.8.2.3 bouyer #endif /* INET6 */
1959 1.8.2.3 bouyer
1960 1.8.2.3 bouyer #ifdef FW_DEBUG
1961 1.8.2.3 bouyer if (fw_dump) {
1962 1.8.2.3 bouyer printf("%s: Config ROM:", sc->sc_sc1394.sc1394_dev.dv_xname);
1963 1.8.2.3 bouyer for (i = 0; i < cfr.ptr - hdr; i++)
1964 1.8.2.3 bouyer printf("%s%08x", i&7?" ":"\n ", hdr[i]);
1965 1.8.2.3 bouyer printf("\n");
1966 1.8.2.3 bouyer }
1967 1.8.2.3 bouyer #endif /* FW_DEBUG */
1968 1.8.2.3 bouyer
1969 1.8.2.3 bouyer /*
1970 1.8.2.3 bouyer * Make network byte order for DMA
1971 1.8.2.3 bouyer */
1972 1.8.2.3 bouyer for (i = 0; i < cfr.ptr - hdr; i++)
1973 1.8.2.3 bouyer HTONL(hdr[i]);
1974 1.8.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
1975 1.8.2.3 bouyer (caddr_t)cfr.ptr - fb->fb_buf, BUS_DMASYNC_PREWRITE);
1976 1.8.2.3 bouyer
1977 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMmap,
1978 1.8.2.3 bouyer fb->fb_dmamap->dm_segs[0].ds_addr);
1979 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_BIBImageValid);
1980 1.8.2.3 bouyer }
1981 1.8.2.3 bouyer
1982 1.8.2.3 bouyer /*
1983 1.8.2.3 bouyer * SelfID buffer (no DMA context)
1984 1.8.2.3 bouyer */
1985 1.8.2.3 bouyer static void
1986 1.8.2.3 bouyer fwohci_selfid_init(struct fwohci_softc *sc)
1987 1.8.2.3 bouyer {
1988 1.8.2.3 bouyer struct fwohci_buf *fb;
1989 1.8.2.3 bouyer u_int32_t val;
1990 1.8.2.3 bouyer
1991 1.8.2.3 bouyer fb = &sc->sc_buf_selfid;
1992 1.8.2.3 bouyer #ifdef DIAGNOSTICS
1993 1.8.2.3 bouyer if ((fb->fb_dmamap->dm_segs[0].ds_addr & 0x7ff) != 0)
1994 1.8.2.3 bouyer panic("fwohci_selfid_init: not aligned: %p (%ld) %p",
1995 1.8.2.3 bouyer (caddr_t)fb->fb_dmamap->dm_segs[0].ds_addr,
1996 1.8.2.3 bouyer fb->fb_dmamap->dm_segs[0].ds_len, fb->fb_buf);
1997 1.8.2.3 bouyer #endif
1998 1.8.2.3 bouyer memset(fb->fb_buf, 0, fb->fb_dmamap->dm_segs[0].ds_len);
1999 1.8.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
2000 1.8.2.3 bouyer fb->fb_dmamap->dm_segs[0].ds_len, BUS_DMASYNC_PREREAD);
2001 1.8.2.3 bouyer
2002 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_SelfIDBuffer,
2003 1.8.2.3 bouyer fb->fb_dmamap->dm_segs[0].ds_addr);
2004 1.8.2.3 bouyer
2005 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
2006 1.8.2.3 bouyer }
2007 1.8.2.3 bouyer
2008 1.8.2.3 bouyer static int
2009 1.8.2.3 bouyer fwohci_selfid_input(struct fwohci_softc *sc)
2010 1.8.2.3 bouyer {
2011 1.8.2.3 bouyer int i;
2012 1.8.2.3 bouyer u_int32_t count, val, gen;
2013 1.8.2.3 bouyer u_int32_t *buf;
2014 1.8.2.3 bouyer
2015 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
2016 1.8.2.3 bouyer if (val & OHCI_SelfID_Error) {
2017 1.8.2.3 bouyer printf("%s: SelfID Error\n", sc->sc_sc1394.sc1394_dev.dv_xname);
2018 1.8.2.3 bouyer return -1;
2019 1.8.2.3 bouyer }
2020 1.8.2.3 bouyer count = (val & OHCI_SelfID_Size_MASK) >> OHCI_SelfID_Size_BITPOS;
2021 1.8.2.3 bouyer gen = (val & OHCI_SelfID_Gen_MASK) >> OHCI_SelfID_Gen_BITPOS;
2022 1.8.2.3 bouyer
2023 1.8.2.3 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_buf_selfid.fb_dmamap,
2024 1.8.2.3 bouyer 0, count << 2, BUS_DMASYNC_POSTREAD);
2025 1.8.2.3 bouyer
2026 1.8.2.3 bouyer buf = (u_int32_t *)sc->sc_buf_selfid.fb_buf;
2027 1.8.2.3 bouyer if ((val & OHCI_SelfID_Gen_MASK) != (buf[0] & OHCI_SelfID_Gen_MASK)) {
2028 1.8.2.3 bouyer printf("%s: SelfID Gen mismatch (%d, %d)\n",
2029 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, gen,
2030 1.8.2.3 bouyer (buf[0] & OHCI_SelfID_Gen_MASK) >> OHCI_SelfID_Gen_BITPOS);
2031 1.8.2.3 bouyer return -1;
2032 1.8.2.3 bouyer }
2033 1.8.2.3 bouyer
2034 1.8.2.3 bouyer #ifdef FW_DEBUG
2035 1.8.2.3 bouyer if (fw_verbose) {
2036 1.8.2.3 bouyer printf("%s: SelfID: 0x%08x", sc->sc_sc1394.sc1394_dev.dv_xname,
2037 1.8.2.3 bouyer val);
2038 1.8.2.3 bouyer for (i = 0; i < count; i++)
2039 1.8.2.3 bouyer printf("%s%08x", i&7?" ":"\n ", buf[i]);
2040 1.8.2.3 bouyer printf("\n");
2041 1.8.2.3 bouyer }
2042 1.8.2.3 bouyer #endif /* FW_DEBUG */
2043 1.8.2.3 bouyer
2044 1.8.2.3 bouyer val = OHCI_CSR_READ(sc, OHCI_REG_NodeId);
2045 1.8.2.3 bouyer if ((val & OHCI_NodeId_IDValid) == 0) {
2046 1.8.2.3 bouyer sc->sc_nodeid = 0xffff; /* invalid */
2047 1.8.2.3 bouyer printf("%s: nodeid is invalid\n",
2048 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname);
2049 1.8.2.3 bouyer return -1;
2050 1.8.2.3 bouyer }
2051 1.8.2.3 bouyer sc->sc_nodeid = val & 0xffff;
2052 1.8.2.3 bouyer
2053 1.8.2.3 bouyer for (i = 1; i < count; i += 2) {
2054 1.8.2.3 bouyer if (buf[i] != ~buf[i + 1]) {
2055 1.8.2.3 bouyer printf("%s: SelfID corrupted (%d, 0x%08x, 0x%08x)\n",
2056 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, i,
2057 1.8.2.3 bouyer buf[i], buf[i + 1]);
2058 1.8.2.3 bouyer if (i == 1 && buf[i] == 0 && buf[i + 1] == 0) {
2059 1.8.2.3 bouyer /*
2060 1.8.2.3 bouyer * XXX: CXD3222 sometimes fails to DMA
2061 1.8.2.3 bouyer * selfid packet??
2062 1.8.2.3 bouyer */
2063 1.8.2.3 bouyer sc->sc_rootid = (count - 1) / 2 - 1;
2064 1.8.2.3 bouyer sc->sc_irmid = sc->sc_rootid;
2065 1.8.2.3 bouyer break;
2066 1.8.2.3 bouyer }
2067 1.8.2.3 bouyer return -1;
2068 1.8.2.3 bouyer }
2069 1.8.2.3 bouyer if (buf[i] & 0x00000001)
2070 1.8.2.3 bouyer continue; /* more pkt */
2071 1.8.2.3 bouyer if (buf[i] & 0x00800000)
2072 1.8.2.3 bouyer continue; /* external id */
2073 1.8.2.3 bouyer sc->sc_rootid = (buf[i] & 0x3f000000) >> 24;
2074 1.8.2.3 bouyer if ((buf[i] & 0x00400800) == 0x00400800)
2075 1.8.2.3 bouyer sc->sc_irmid = sc->sc_rootid;
2076 1.8.2.3 bouyer }
2077 1.8.2.3 bouyer #ifdef FW_DEBUG
2078 1.8.2.3 bouyer if (fw_verbose)
2079 1.8.2.3 bouyer printf("%s: nodeid=0x%04x(%d), rootid=%d, irmid=%d\n",
2080 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname,
2081 1.8.2.3 bouyer sc->sc_nodeid, sc->sc_nodeid & OHCI_NodeId_NodeNumber,
2082 1.8.2.3 bouyer sc->sc_rootid, sc->sc_irmid);
2083 1.8.2.3 bouyer #endif
2084 1.8.2.3 bouyer
2085 1.8.2.3 bouyer if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) > sc->sc_rootid)
2086 1.8.2.3 bouyer return -1;
2087 1.8.2.3 bouyer
2088 1.8.2.3 bouyer if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) == sc->sc_rootid)
2089 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
2090 1.8.2.3 bouyer OHCI_LinkControl_CycleMaster);
2091 1.8.2.3 bouyer else
2092 1.8.2.3 bouyer OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear,
2093 1.8.2.3 bouyer OHCI_LinkControl_CycleMaster);
2094 1.8.2.3 bouyer return 0;
2095 1.8.2.3 bouyer }
2096 1.8.2.3 bouyer
2097 1.8.2.3 bouyer /*
2098 1.8.2.3 bouyer * some CSRs are handled by driver.
2099 1.8.2.3 bouyer */
2100 1.8.2.3 bouyer static void
2101 1.8.2.3 bouyer fwohci_csr_init(struct fwohci_softc *sc)
2102 1.8.2.3 bouyer {
2103 1.8.2.3 bouyer int i;
2104 1.8.2.3 bouyer static u_int32_t csr[] = {
2105 1.8.2.3 bouyer CSR_STATE_CLEAR, CSR_STATE_SET, CSR_SB_CYCLE_TIME,
2106 1.8.2.3 bouyer CSR_SB_BUS_TIME, CSR_SB_BUSY_TIMEOUT, CSR_SB_BUS_MANAGER_ID,
2107 1.8.2.3 bouyer CSR_SB_CHANNEL_AVAILABLE_HI, CSR_SB_CHANNEL_AVAILABLE_LO,
2108 1.8.2.3 bouyer CSR_SB_BROADCAST_CHANNEL
2109 1.8.2.3 bouyer };
2110 1.8.2.3 bouyer
2111 1.8.2.3 bouyer for (i = 0; i < sizeof(csr) / sizeof(csr[0]); i++) {
2112 1.8.2.3 bouyer fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_QUAD,
2113 1.8.2.3 bouyer CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
2114 1.8.2.3 bouyer fwohci_handler_set(sc, IEEE1394_TCODE_READ_REQ_QUAD,
2115 1.8.2.3 bouyer CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
2116 1.8.2.3 bouyer }
2117 1.8.2.3 bouyer sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] = 31; /*XXX*/
2118 1.8.2.3 bouyer }
2119 1.8.2.3 bouyer
2120 1.8.2.3 bouyer static int
2121 1.8.2.3 bouyer fwohci_csr_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
2122 1.8.2.3 bouyer {
2123 1.8.2.3 bouyer struct fwohci_pkt res;
2124 1.8.2.3 bouyer u_int32_t reg;
2125 1.8.2.3 bouyer
2126 1.8.2.3 bouyer /*
2127 1.8.2.3 bouyer * XXX need to do special functionality other than just r/w...
2128 1.8.2.3 bouyer */
2129 1.8.2.3 bouyer reg = pkt->fp_hdr[2] - CSR_BASE_LO;
2130 1.8.2.3 bouyer
2131 1.8.2.3 bouyer if ((reg & 0x03) != 0) {
2132 1.8.2.3 bouyer /* alignment error */
2133 1.8.2.3 bouyer return IEEE1394_RCODE_ADDRESS_ERROR;
2134 1.8.2.3 bouyer }
2135 1.8.2.3 bouyer #ifdef FW_DEBUG
2136 1.8.2.3 bouyer if (fw_verbose)
2137 1.8.2.3 bouyer printf("fwohci_csr_input: CSR[0x%04x]: 0x%08x",
2138 1.8.2.3 bouyer reg, *(u_int32_t *)(&sc->sc_csr[reg]));
2139 1.8.2.3 bouyer #endif
2140 1.8.2.3 bouyer if (pkt->fp_tcode == IEEE1394_TCODE_WRITE_REQ_QUAD) {
2141 1.8.2.3 bouyer #ifdef FW_DEBUG
2142 1.8.2.3 bouyer if (fw_verbose)
2143 1.8.2.3 bouyer printf(" -> 0x%08x\n",
2144 1.8.2.3 bouyer ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base));
2145 1.8.2.3 bouyer #endif
2146 1.8.2.3 bouyer *(u_int32_t *)&sc->sc_csr[reg] =
2147 1.8.2.3 bouyer ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base);
2148 1.8.2.3 bouyer } else {
2149 1.8.2.3 bouyer #ifdef FW_DEBUG
2150 1.8.2.3 bouyer if (fw_verbose)
2151 1.8.2.3 bouyer printf("\n");
2152 1.8.2.3 bouyer #endif
2153 1.8.2.3 bouyer res.fp_hdr[3] = htonl(*(u_int32_t *)&sc->sc_csr[reg]);
2154 1.8.2.3 bouyer res.fp_iov[0].iov_base = &res.fp_hdr[3];
2155 1.8.2.3 bouyer res.fp_iov[0].iov_len = 4;
2156 1.8.2.3 bouyer res.fp_uio.uio_resid = 4;
2157 1.8.2.3 bouyer res.fp_uio.uio_iovcnt = 1;
2158 1.8.2.3 bouyer fwohci_atrs_output(sc, IEEE1394_RCODE_COMPLETE, pkt, &res);
2159 1.8.2.3 bouyer return -1;
2160 1.8.2.3 bouyer }
2161 1.8.2.3 bouyer return IEEE1394_RCODE_COMPLETE;
2162 1.8.2.3 bouyer }
2163 1.8.2.3 bouyer
2164 1.8.2.3 bouyer /*
2165 1.8.2.3 bouyer * Mapping between nodeid and unique ID (EUI-64).
2166 1.8.2.3 bouyer */
2167 1.8.2.3 bouyer static void
2168 1.8.2.3 bouyer fwohci_uid_collect(struct fwohci_softc *sc)
2169 1.8.2.3 bouyer {
2170 1.8.2.3 bouyer int i;
2171 1.8.2.3 bouyer struct fwohci_uidtbl *fu;
2172 1.8.2.3 bouyer struct fwohci_pkt pkt;
2173 1.8.2.3 bouyer
2174 1.8.2.3 bouyer if (sc->sc_uidtbl != NULL)
2175 1.8.2.3 bouyer free(sc->sc_uidtbl, M_DEVBUF);
2176 1.8.2.3 bouyer sc->sc_uidtbl = malloc(sizeof(*fu) * (sc->sc_rootid + 1),
2177 1.8.2.3 bouyer M_DEVBUF, M_NOWAIT);
2178 1.8.2.3 bouyer if (sc->sc_uidtbl == NULL)
2179 1.8.2.3 bouyer return;
2180 1.8.2.3 bouyer memset(sc->sc_uidtbl, 0, sizeof(*fu) * (sc->sc_rootid + 1));
2181 1.8.2.3 bouyer
2182 1.8.2.3 bouyer memset(&pkt, 0, sizeof(pkt));
2183 1.8.2.3 bouyer for (i = 0, fu = sc->sc_uidtbl; i <= sc->sc_rootid; i++, fu++) {
2184 1.8.2.3 bouyer if (i == (sc->sc_nodeid & OHCI_NodeId_NodeNumber)) {
2185 1.8.2.3 bouyer memcpy(fu->fu_uid, sc->sc_sc1394.sc1394_guid, 8);
2186 1.8.2.3 bouyer fu->fu_valid = 3;
2187 1.8.2.3 bouyer continue;
2188 1.8.2.3 bouyer }
2189 1.8.2.3 bouyer fu->fu_valid = 0;
2190 1.8.2.3 bouyer pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
2191 1.8.2.3 bouyer pkt.fp_hlen = 12;
2192 1.8.2.3 bouyer pkt.fp_dlen = 0;
2193 1.8.2.3 bouyer pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
2194 1.8.2.3 bouyer (pkt.fp_tcode << 4);
2195 1.8.2.3 bouyer pkt.fp_hdr[1] = ((0xffc0 | i) << 16) | CSR_BASE_HI;
2196 1.8.2.3 bouyer pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 12;
2197 1.8.2.3 bouyer fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, i,
2198 1.8.2.3 bouyer sc->sc_tlabel, fwohci_uid_input, (void *)0);
2199 1.8.2.3 bouyer sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
2200 1.8.2.3 bouyer fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
2201 1.8.2.3 bouyer
2202 1.8.2.3 bouyer pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
2203 1.8.2.3 bouyer (pkt.fp_tcode << 4);
2204 1.8.2.3 bouyer pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 16;
2205 1.8.2.3 bouyer fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, i,
2206 1.8.2.3 bouyer sc->sc_tlabel, fwohci_uid_input, (void *)1);
2207 1.8.2.3 bouyer sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
2208 1.8.2.3 bouyer fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
2209 1.8.2.3 bouyer }
2210 1.8.2.3 bouyer }
2211 1.8.2.3 bouyer
2212 1.8.2.3 bouyer static int
2213 1.8.2.3 bouyer fwohci_uid_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *res)
2214 1.8.2.3 bouyer {
2215 1.8.2.3 bouyer int n, rcode;
2216 1.8.2.3 bouyer struct fwohci_uidtbl *fu;
2217 1.8.2.3 bouyer
2218 1.8.2.3 bouyer n = (res->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
2219 1.8.2.3 bouyer rcode = (res->fp_hdr[1] & 0x0000f000) >> 12;
2220 1.8.2.3 bouyer if (rcode != IEEE1394_RCODE_COMPLETE ||
2221 1.8.2.3 bouyer sc->sc_uidtbl == NULL ||
2222 1.8.2.3 bouyer n > sc->sc_rootid)
2223 1.8.2.3 bouyer return 0;
2224 1.8.2.3 bouyer fu = &sc->sc_uidtbl[n];
2225 1.8.2.3 bouyer if (arg == 0) {
2226 1.8.2.3 bouyer memcpy(fu->fu_uid, res->fp_iov[0].iov_base, 4);
2227 1.8.2.3 bouyer fu->fu_valid |= 0x1;
2228 1.8.2.3 bouyer } else {
2229 1.8.2.3 bouyer memcpy(fu->fu_uid + 4, res->fp_iov[0].iov_base, 4);
2230 1.8.2.3 bouyer fu->fu_valid |= 0x2;
2231 1.8.2.3 bouyer }
2232 1.8.2.3 bouyer #ifdef FW_DEBUG
2233 1.8.2.3 bouyer if (fw_verbose && fu->fu_valid == 0x3)
2234 1.8.2.3 bouyer printf("fwohci_uid_input: "
2235 1.8.2.3 bouyer "Node %d, UID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", n,
2236 1.8.2.3 bouyer fu->fu_uid[0], fu->fu_uid[1], fu->fu_uid[2], fu->fu_uid[3],
2237 1.8.2.3 bouyer fu->fu_uid[4], fu->fu_uid[5], fu->fu_uid[6], fu->fu_uid[7]);
2238 1.8.2.3 bouyer #endif
2239 1.8.2.3 bouyer return 0;
2240 1.8.2.3 bouyer }
2241 1.8.2.3 bouyer
2242 1.8.2.3 bouyer static int
2243 1.8.2.3 bouyer fwohci_uid_lookup(struct fwohci_softc *sc, const u_int8_t *uid)
2244 1.8.2.3 bouyer {
2245 1.8.2.3 bouyer struct fwohci_uidtbl *fu;
2246 1.8.2.3 bouyer int n;
2247 1.8.2.3 bouyer static const u_int8_t bcast[] =
2248 1.8.2.3 bouyer { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2249 1.8.2.3 bouyer
2250 1.8.2.3 bouyer fu = sc->sc_uidtbl;
2251 1.8.2.3 bouyer if (fu == NULL) {
2252 1.8.2.3 bouyer notfound:
2253 1.8.2.3 bouyer if (memcmp(uid, bcast, sizeof(bcast)) == 0)
2254 1.8.2.3 bouyer return IEEE1394_BCAST_PHY_ID;
2255 1.8.2.3 bouyer fwohci_uid_collect(sc); /* try to get */
2256 1.8.2.3 bouyer return -1;
2257 1.8.2.3 bouyer }
2258 1.8.2.3 bouyer for (n = 0; ; n++, fu++) {
2259 1.8.2.3 bouyer if (n > sc->sc_rootid)
2260 1.8.2.3 bouyer goto notfound;
2261 1.8.2.3 bouyer if (fu->fu_valid == 0x3 && memcmp(fu->fu_uid, uid, 8) == 0)
2262 1.8.2.3 bouyer break;
2263 1.8.2.3 bouyer }
2264 1.8.2.3 bouyer return n;
2265 1.8.2.3 bouyer }
2266 1.8.2.3 bouyer
2267 1.8.2.3 bouyer /*
2268 1.8.2.3 bouyer * functions to support network interface
2269 1.8.2.3 bouyer */
2270 1.8.2.3 bouyer static int
2271 1.8.2.3 bouyer fwohci_if_inreg(struct device *self, u_int32_t offhi, u_int32_t offlo,
2272 1.8.2.3 bouyer void (*handler)(struct device *, struct mbuf *))
2273 1.8.2.3 bouyer {
2274 1.8.2.3 bouyer struct fwohci_softc *sc = (struct fwohci_softc *)self;
2275 1.8.2.3 bouyer int s;
2276 1.8.2.3 bouyer
2277 1.8.2.3 bouyer s = splimp();
2278 1.8.2.3 bouyer fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_BLOCK, offhi, offlo,
2279 1.8.2.3 bouyer fwohci_if_input, handler);
2280 1.8.2.3 bouyer fwohci_handler_set(sc, IEEE1394_TCODE_STREAM_DATA,
2281 1.8.2.3 bouyer sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] & OHCI_NodeId_NodeNumber,
2282 1.8.2.3 bouyer IEEE1394_TAG_GASP, fwohci_if_input, handler);
2283 1.8.2.3 bouyer splx(s);
2284 1.8.2.3 bouyer return 0;
2285 1.8.2.3 bouyer }
2286 1.8.2.3 bouyer
2287 1.8.2.3 bouyer static int
2288 1.8.2.3 bouyer fwohci_if_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
2289 1.8.2.3 bouyer {
2290 1.8.2.3 bouyer int n, len;
2291 1.8.2.3 bouyer struct mbuf *m;
2292 1.8.2.3 bouyer struct iovec *iov;
2293 1.8.2.3 bouyer void (*handler)(struct device *, struct mbuf *) = arg;
2294 1.8.2.3 bouyer
2295 1.8.2.3 bouyer #ifdef FW_DEBUG
2296 1.8.2.3 bouyer if (fw_verbose) {
2297 1.8.2.3 bouyer int i;
2298 1.8.2.3 bouyer printf("fwohci_if_input: tcode=0x%x, dlen=%d",
2299 1.8.2.3 bouyer pkt->fp_tcode, pkt->fp_dlen);
2300 1.8.2.3 bouyer if (fw_dump) {
2301 1.8.2.3 bouyer for (i = 0; i < pkt->fp_hlen/4; i++)
2302 1.8.2.3 bouyer printf("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]);
2303 1.8.2.3 bouyer printf("$");
2304 1.8.2.3 bouyer for (n = 0, len = pkt->fp_dlen; len > 0; len -= i, n++){
2305 1.8.2.3 bouyer iov = &pkt->fp_iov[n];
2306 1.8.2.3 bouyer for (i = 0; i < iov->iov_len; i++)
2307 1.8.2.3 bouyer printf("%s%02x",
2308 1.8.2.3 bouyer (i%32)?((i%4)?"":" "):"\n\t",
2309 1.8.2.3 bouyer ((u_int8_t *)iov->iov_base)[i]);
2310 1.8.2.3 bouyer printf("$");
2311 1.8.2.3 bouyer }
2312 1.8.2.3 bouyer }
2313 1.8.2.3 bouyer printf("\n");
2314 1.8.2.3 bouyer }
2315 1.8.2.3 bouyer #endif /* FW_DEBUG */
2316 1.8.2.3 bouyer len = pkt->fp_dlen;
2317 1.8.2.3 bouyer MGETHDR(m, M_DONTWAIT, MT_DATA);
2318 1.8.2.3 bouyer if (m == NULL)
2319 1.8.2.3 bouyer return IEEE1394_RCODE_COMPLETE;
2320 1.8.2.3 bouyer if (len + m->m_len > MHLEN) {
2321 1.8.2.3 bouyer MCLGET(m, M_DONTWAIT);
2322 1.8.2.3 bouyer if ((m->m_flags & M_EXT) == 0) {
2323 1.8.2.3 bouyer m_freem(m);
2324 1.8.2.3 bouyer return IEEE1394_RCODE_COMPLETE;
2325 1.8.2.3 bouyer }
2326 1.8.2.3 bouyer }
2327 1.8.2.3 bouyer m->m_len = 16;
2328 1.8.2.3 bouyer n = (pkt->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
2329 1.8.2.3 bouyer if (sc->sc_uidtbl == NULL || n > sc->sc_rootid ||
2330 1.8.2.3 bouyer sc->sc_uidtbl[n].fu_valid != 0x3) {
2331 1.8.2.3 bouyer printf("%s: packet from unknown node: phy id %d\n",
2332 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname, n);
2333 1.8.2.3 bouyer m_freem(m);
2334 1.8.2.3 bouyer return IEEE1394_RCODE_COMPLETE;
2335 1.8.2.3 bouyer }
2336 1.8.2.3 bouyer memcpy(mtod(m, caddr_t), sc->sc_uidtbl[n].fu_uid, 8);
2337 1.8.2.3 bouyer if (pkt->fp_tcode == IEEE1394_TCODE_STREAM_DATA) {
2338 1.8.2.3 bouyer m->m_flags |= M_BCAST;
2339 1.8.2.3 bouyer mtod(m, u_int32_t *)[2] = mtod(m, u_int32_t *)[3] = 0;
2340 1.8.2.3 bouyer } else {
2341 1.8.2.3 bouyer mtod(m, u_int32_t *)[2] = htonl(pkt->fp_hdr[1]);
2342 1.8.2.3 bouyer mtod(m, u_int32_t *)[3] = htonl(pkt->fp_hdr[2]);
2343 1.8.2.3 bouyer }
2344 1.8.2.3 bouyer mtod(m, u_int8_t *)[8] = n; /*XXX: node id for debug */
2345 1.8.2.3 bouyer mtod(m, u_int8_t *)[9] =
2346 1.8.2.3 bouyer (*pkt->fp_trail >> (16 + OHCI_CTXCTL_SPD_BITPOS)) &
2347 1.8.2.3 bouyer ((1 << OHCI_CTXCTL_SPD_BITLEN) - 1);
2348 1.8.2.3 bouyer
2349 1.8.2.3 bouyer m->m_pkthdr.rcvif = NULL; /* set in child */
2350 1.8.2.3 bouyer m->m_pkthdr.len = len + m->m_len;
2351 1.8.2.3 bouyer /*
2352 1.8.2.3 bouyer * We may use receive buffer by external mbuf instead of copy here.
2353 1.8.2.3 bouyer * But asynchronous receive buffer must be operate in buffer fill
2354 1.8.2.3 bouyer * mode, so that each receive buffer will shared by multiple mbufs.
2355 1.8.2.3 bouyer * If upper layer doesn't free mbuf soon, e.g. application program
2356 1.8.2.3 bouyer * is suspended, buffer must be reallocated.
2357 1.8.2.3 bouyer * Isochronous buffer must be operate in packet buffer mode, and
2358 1.8.2.3 bouyer * it is easy to map receive buffer to external mbuf. But it is
2359 1.8.2.3 bouyer * used for broadcast/multicast only, and is expected not so
2360 1.8.2.3 bouyer * performance sensitive for now.
2361 1.8.2.3 bouyer * XXX: The performance may be important for multicast case,
2362 1.8.2.3 bouyer * so we should revisit here later.
2363 1.8.2.3 bouyer * -- onoe
2364 1.8.2.3 bouyer */
2365 1.8.2.3 bouyer n = 0;
2366 1.8.2.3 bouyer iov = pkt->fp_uio.uio_iov;
2367 1.8.2.3 bouyer while (len > 0) {
2368 1.8.2.3 bouyer memcpy(mtod(m, caddr_t) + m->m_len, iov->iov_base,
2369 1.8.2.3 bouyer iov->iov_len);
2370 1.8.2.3 bouyer m->m_len += iov->iov_len;
2371 1.8.2.3 bouyer len -= iov->iov_len;
2372 1.8.2.3 bouyer iov++;
2373 1.8.2.3 bouyer }
2374 1.8.2.3 bouyer (*handler)(sc->sc_sc1394.sc1394_if, m);
2375 1.8.2.3 bouyer return IEEE1394_RCODE_COMPLETE;
2376 1.8.2.3 bouyer }
2377 1.8.2.3 bouyer
2378 1.8.2.3 bouyer static int
2379 1.8.2.3 bouyer fwohci_if_output(struct device *self, struct mbuf *m0,
2380 1.8.2.3 bouyer void (*callback)(struct device *, struct mbuf *))
2381 1.8.2.3 bouyer {
2382 1.8.2.3 bouyer struct fwohci_softc *sc = (struct fwohci_softc *)self;
2383 1.8.2.3 bouyer struct fwohci_pkt pkt;
2384 1.8.2.3 bouyer u_int8_t *p;
2385 1.8.2.3 bouyer int s, n, error, spd, hdrlen, maxrec;
2386 1.8.2.3 bouyer
2387 1.8.2.3 bouyer p = mtod(m0, u_int8_t *);
2388 1.8.2.3 bouyer if (m0->m_flags & (M_BCAST | M_MCAST)) {
2389 1.8.2.3 bouyer spd = IEEE1394_SPD_S100; /*XXX*/
2390 1.8.2.3 bouyer maxrec = 512; /*XXX*/
2391 1.8.2.3 bouyer hdrlen = 8;
2392 1.8.2.3 bouyer } else {
2393 1.8.2.3 bouyer n = fwohci_uid_lookup(sc, p);
2394 1.8.2.3 bouyer if (n < 0) {
2395 1.8.2.3 bouyer printf("%s: nodeid unknown:"
2396 1.8.2.3 bouyer " %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
2397 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname,
2398 1.8.2.3 bouyer p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
2399 1.8.2.3 bouyer error = EHOSTUNREACH;
2400 1.8.2.3 bouyer goto end;
2401 1.8.2.3 bouyer }
2402 1.8.2.3 bouyer if (n == IEEE1394_BCAST_PHY_ID) {
2403 1.8.2.3 bouyer printf("%s: broadcast with !M_MCAST\n",
2404 1.8.2.3 bouyer sc->sc_sc1394.sc1394_dev.dv_xname);
2405 1.8.2.3 bouyer #ifdef FW_DEBUG
2406 1.8.2.3 bouyer if (fw_dump) {
2407 1.8.2.3 bouyer struct mbuf *m;
2408 1.8.2.3 bouyer printf("packet:");
2409 1.8.2.3 bouyer for (m = m0; m != NULL; m = m->m_next) {
2410 1.8.2.3 bouyer for (n = 0; n < m->m_len; n++)
2411 1.8.2.3 bouyer printf("%s%02x", (n%32)?
2412 1.8.2.3 bouyer ((n%4)?"":" "):"\n\t",
2413 1.8.2.3 bouyer mtod(m, u_int8_t *)[n]);
2414 1.8.2.3 bouyer printf("$");
2415 1.8.2.3 bouyer }
2416 1.8.2.3 bouyer printf("\n");
2417 1.8.2.3 bouyer }
2418 1.8.2.3 bouyer #endif
2419 1.8.2.3 bouyer error = EHOSTUNREACH;
2420 1.8.2.3 bouyer goto end;
2421 1.8.2.3 bouyer }
2422 1.8.2.3 bouyer maxrec = 2 << p[8];
2423 1.8.2.3 bouyer spd = p[9];
2424 1.8.2.3 bouyer hdrlen = 0;
2425 1.8.2.3 bouyer }
2426 1.8.2.3 bouyer if (spd > sc->sc_sc1394.sc1394_link_speed) {
2427 1.8.2.3 bouyer #ifdef FW_DEBUG
2428 1.8.2.3 bouyer if (fw_verbose)
2429 1.8.2.3 bouyer printf("fwohci_if_output: spd (%d) is faster than %d\n",
2430 1.8.2.3 bouyer spd, sc->sc_sc1394.sc1394_link_speed);
2431 1.8.2.3 bouyer #endif
2432 1.8.2.3 bouyer spd = sc->sc_sc1394.sc1394_link_speed;
2433 1.8.2.3 bouyer }
2434 1.8.2.3 bouyer if (maxrec > (512 << spd)) {
2435 1.8.2.3 bouyer #ifdef FW_DEBUG
2436 1.8.2.3 bouyer if (fw_verbose)
2437 1.8.2.3 bouyer printf("fwohci_if_output: maxrec (%d) is larger for"
2438 1.8.2.3 bouyer " spd (%d)\n", maxrec, spd);
2439 1.8.2.3 bouyer #endif
2440 1.8.2.3 bouyer maxrec = 512 << spd;
2441 1.8.2.3 bouyer }
2442 1.8.2.3 bouyer while (maxrec > sc->sc_sc1394.sc1394_max_receive) {
2443 1.8.2.3 bouyer #ifdef FW_DEBUG
2444 1.8.2.3 bouyer if (fw_verbose)
2445 1.8.2.3 bouyer printf("fwohci_if_output: maxrec (%d) is larger than"
2446 1.8.2.3 bouyer " %d\n", maxrec, sc->sc_sc1394.sc1394_max_receive);
2447 1.8.2.3 bouyer #endif
2448 1.8.2.3 bouyer maxrec >>= 1;
2449 1.8.2.3 bouyer }
2450 1.8.2.3 bouyer if (maxrec < 512) {
2451 1.8.2.3 bouyer #ifdef FW_DEBUG
2452 1.8.2.3 bouyer if (fw_verbose)
2453 1.8.2.3 bouyer printf("fwohci_if_output: maxrec (%d) is smaller"
2454 1.8.2.3 bouyer " than minimum\n", maxrec);
2455 1.8.2.3 bouyer #endif
2456 1.8.2.3 bouyer maxrec = 512;
2457 1.8.2.3 bouyer }
2458 1.8.2.3 bouyer
2459 1.8.2.3 bouyer m_adj(m0, 16 - hdrlen);
2460 1.8.2.3 bouyer if (m0->m_pkthdr.len > maxrec) {
2461 1.8.2.3 bouyer #ifdef FW_DEBUG
2462 1.8.2.3 bouyer if (fw_verbose)
2463 1.8.2.3 bouyer printf("fwohci_if_output: packet too big:"
2464 1.8.2.3 bouyer " hdr %d, pktlen %d, maxrec %d\n",
2465 1.8.2.3 bouyer hdrlen, m0->m_pkthdr.len, maxrec);
2466 1.8.2.3 bouyer #endif
2467 1.8.2.3 bouyer error = E2BIG; /*XXX*/
2468 1.8.2.3 bouyer goto end;
2469 1.8.2.3 bouyer }
2470 1.8.2.3 bouyer
2471 1.8.2.3 bouyer memset(&pkt, 0, sizeof(pkt));
2472 1.8.2.3 bouyer pkt.fp_uio.uio_iov = pkt.fp_iov;
2473 1.8.2.3 bouyer pkt.fp_uio.uio_segflg = UIO_SYSSPACE;
2474 1.8.2.3 bouyer pkt.fp_uio.uio_rw = UIO_WRITE;
2475 1.8.2.3 bouyer s = splimp();
2476 1.8.2.3 bouyer if (m0->m_flags & (M_BCAST | M_MCAST)) {
2477 1.8.2.3 bouyer /* construct GASP header */
2478 1.8.2.3 bouyer p = mtod(m0, u_int8_t *);
2479 1.8.2.3 bouyer p[0] = sc->sc_nodeid >> 8;
2480 1.8.2.3 bouyer p[1] = sc->sc_nodeid & 0xff;
2481 1.8.2.3 bouyer p[2] = 0x00; p[3] = 0x00; p[4] = 0x5e;
2482 1.8.2.3 bouyer p[5] = 0x00; p[6] = 0x00; p[7] = 0x01;
2483 1.8.2.3 bouyer pkt.fp_tcode = IEEE1394_TCODE_STREAM_DATA;
2484 1.8.2.3 bouyer pkt.fp_hlen = 8;
2485 1.8.2.3 bouyer pkt.fp_hdr[0] = (spd << 16) | (IEEE1394_TAG_GASP << 14) |
2486 1.8.2.3 bouyer ((sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] &
2487 1.8.2.3 bouyer OHCI_NodeId_NodeNumber) << 8);
2488 1.8.2.3 bouyer pkt.fp_hdr[1] = m0->m_pkthdr.len << 16;
2489 1.8.2.3 bouyer } else {
2490 1.8.2.3 bouyer pkt.fp_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
2491 1.8.2.3 bouyer pkt.fp_hlen = 16;
2492 1.8.2.3 bouyer pkt.fp_hdr[0] = 0x00800100 | (sc->sc_tlabel << 10) |
2493 1.8.2.3 bouyer (spd << 16);
2494 1.8.2.3 bouyer pkt.fp_hdr[1] =
2495 1.8.2.3 bouyer (((sc->sc_nodeid & OHCI_NodeId_BusNumber) | n) << 16) |
2496 1.8.2.3 bouyer (p[10] << 8) | p[11];
2497 1.8.2.3 bouyer pkt.fp_hdr[2] = (p[12]<<24) | (p[13]<<16) | (p[14]<<8) | p[15];
2498 1.8.2.3 bouyer pkt.fp_hdr[3] = m0->m_pkthdr.len << 16;
2499 1.8.2.3 bouyer sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
2500 1.8.2.3 bouyer }
2501 1.8.2.3 bouyer pkt.fp_hdr[0] |= (pkt.fp_tcode << 4);
2502 1.8.2.3 bouyer pkt.fp_dlen = m0->m_pkthdr.len;
2503 1.8.2.3 bouyer pkt.fp_m = m0;
2504 1.8.2.3 bouyer pkt.fp_callback = callback;
2505 1.8.2.3 bouyer error = fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
2506 1.8.2.3 bouyer splx(s);
2507 1.8.2.3 bouyer m0 = pkt.fp_m;
2508 1.8.2.3 bouyer end:
2509 1.8.2.3 bouyer if (error) {
2510 1.8.2.3 bouyer if (callback)
2511 1.8.2.3 bouyer (*callback)(sc->sc_sc1394.sc1394_if, m0);
2512 1.8.2.3 bouyer else
2513 1.8.2.3 bouyer m_freem(m0);
2514 1.8.2.2 bouyer }
2515 1.8.2.3 bouyer return error;
2516 1.8.2.2 bouyer }
2517