twe.c revision 1.4.2.7 1 1.4.2.7 bouyer /* $NetBSD: twe.c,v 1.4.2.7 2001/03/12 13:31:16 bouyer Exp $ */
2 1.4.2.2 bouyer
3 1.4.2.2 bouyer /*-
4 1.4.2.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.4.2.2 bouyer * All rights reserved.
6 1.4.2.2 bouyer *
7 1.4.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.4.2.2 bouyer * by Andrew Doran.
9 1.4.2.2 bouyer *
10 1.4.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.4.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.4.2.2 bouyer * are met:
13 1.4.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.4.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.4.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.4.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.4.2.2 bouyer * must display the following acknowledgement:
20 1.4.2.2 bouyer * This product includes software developed by the NetBSD
21 1.4.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.4.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.4.2.2 bouyer * from this software without specific prior written permission.
25 1.4.2.2 bouyer *
26 1.4.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.4.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.4.2.2 bouyer */
38 1.4.2.2 bouyer
39 1.4.2.2 bouyer /*-
40 1.4.2.2 bouyer * Copyright (c) 2000 Michael Smith
41 1.4.2.2 bouyer * Copyright (c) 2000 BSDi
42 1.4.2.2 bouyer * All rights reserved.
43 1.4.2.2 bouyer *
44 1.4.2.2 bouyer * Redistribution and use in source and binary forms, with or without
45 1.4.2.2 bouyer * modification, are permitted provided that the following conditions
46 1.4.2.2 bouyer * are met:
47 1.4.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
48 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer.
49 1.4.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
50 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
51 1.4.2.2 bouyer * documentation and/or other materials provided with the distribution.
52 1.4.2.2 bouyer *
53 1.4.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
54 1.4.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.4.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.4.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 1.4.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.4.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.4.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.4.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.4.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.4.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.4.2.2 bouyer * SUCH DAMAGE.
64 1.4.2.2 bouyer *
65 1.4.2.2 bouyer * from FreeBSD: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
66 1.4.2.2 bouyer */
67 1.4.2.2 bouyer
68 1.4.2.2 bouyer /*
69 1.4.2.2 bouyer * Driver for the 3ware Escalade family of RAID controllers.
70 1.4.2.2 bouyer */
71 1.4.2.2 bouyer
72 1.4.2.2 bouyer #include <sys/param.h>
73 1.4.2.2 bouyer #include <sys/systm.h>
74 1.4.2.2 bouyer #include <sys/kernel.h>
75 1.4.2.2 bouyer #include <sys/device.h>
76 1.4.2.2 bouyer #include <sys/queue.h>
77 1.4.2.2 bouyer #include <sys/proc.h>
78 1.4.2.2 bouyer #include <sys/buf.h>
79 1.4.2.2 bouyer #include <sys/endian.h>
80 1.4.2.2 bouyer #include <sys/malloc.h>
81 1.4.2.2 bouyer #include <sys/disk.h>
82 1.4.2.2 bouyer
83 1.4.2.2 bouyer #include <uvm/uvm_extern.h>
84 1.4.2.2 bouyer
85 1.4.2.2 bouyer #include <machine/bswap.h>
86 1.4.2.2 bouyer #include <machine/bus.h>
87 1.4.2.2 bouyer
88 1.4.2.2 bouyer #include <dev/pci/pcireg.h>
89 1.4.2.2 bouyer #include <dev/pci/pcivar.h>
90 1.4.2.2 bouyer #include <dev/pci/pcidevs.h>
91 1.4.2.2 bouyer #include <dev/pci/twereg.h>
92 1.4.2.2 bouyer #include <dev/pci/twevar.h>
93 1.4.2.2 bouyer
94 1.4.2.2 bouyer #define TWE_INL(sc, port) \
95 1.4.2.2 bouyer bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, port)
96 1.4.2.2 bouyer #define TWE_OUTL(sc, port, val) \
97 1.4.2.2 bouyer bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, port, val)
98 1.4.2.2 bouyer
99 1.4.2.3 bouyer #define PCI_CBIO 0x10
100 1.4.2.3 bouyer
101 1.4.2.2 bouyer static void twe_aen_handler(struct twe_ccb *, int);
102 1.4.2.2 bouyer static void twe_attach(struct device *, struct device *, void *);
103 1.4.2.2 bouyer static int twe_init_connection(struct twe_softc *);
104 1.4.2.2 bouyer static int twe_intr(void *);
105 1.4.2.2 bouyer static int twe_match(struct device *, struct cfdata *, void *);
106 1.4.2.6 bouyer static int twe_param_get(struct twe_softc *, int, int, size_t,
107 1.4.2.6 bouyer void (*)(struct twe_ccb *, int), void **);
108 1.4.2.2 bouyer static void twe_poll(struct twe_softc *);
109 1.4.2.2 bouyer static int twe_print(void *, const char *);
110 1.4.2.2 bouyer static int twe_reset(struct twe_softc *);
111 1.4.2.2 bouyer static int twe_submatch(struct device *, struct cfdata *, void *);
112 1.4.2.2 bouyer static int twe_status_check(struct twe_softc *, u_int);
113 1.4.2.2 bouyer static int twe_status_wait(struct twe_softc *, u_int, int);
114 1.4.2.2 bouyer
115 1.4.2.2 bouyer struct cfattach twe_ca = {
116 1.4.2.2 bouyer sizeof(struct twe_softc), twe_match, twe_attach
117 1.4.2.2 bouyer };
118 1.4.2.2 bouyer
119 1.4.2.2 bouyer struct {
120 1.4.2.3 bouyer const u_int aen; /* High byte non-zero if w/unit */
121 1.4.2.2 bouyer const char *desc;
122 1.4.2.2 bouyer } static const twe_aen_names[] = {
123 1.4.2.2 bouyer { 0x0000, "queue empty" },
124 1.4.2.2 bouyer { 0x0001, "soft reset" },
125 1.4.2.3 bouyer { 0x0102, "degraded mirror" },
126 1.4.2.2 bouyer { 0x0003, "controller error" },
127 1.4.2.3 bouyer { 0x0104, "rebuild fail" },
128 1.4.2.3 bouyer { 0x0105, "rebuild done" },
129 1.4.2.3 bouyer { 0x0106, "incompatible unit" },
130 1.4.2.3 bouyer { 0x0107, "init done" },
131 1.4.2.3 bouyer { 0x0108, "unclean shutdown" },
132 1.4.2.3 bouyer { 0x0109, "aport timeout" },
133 1.4.2.3 bouyer { 0x010a, "drive error" },
134 1.4.2.3 bouyer { 0x010b, "rebuild started" },
135 1.4.2.3 bouyer { 0x0015, "table undefined" },
136 1.4.2.2 bouyer { 0x00ff, "aen queue full" },
137 1.4.2.2 bouyer };
138 1.4.2.2 bouyer
139 1.4.2.2 bouyer /*
140 1.4.2.2 bouyer * Match a supported board.
141 1.4.2.2 bouyer */
142 1.4.2.2 bouyer static int
143 1.4.2.2 bouyer twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
144 1.4.2.2 bouyer {
145 1.4.2.2 bouyer struct pci_attach_args *pa;
146 1.4.2.2 bouyer
147 1.4.2.2 bouyer pa = aux;
148 1.4.2.2 bouyer
149 1.4.2.2 bouyer return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
150 1.4.2.7 bouyer (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE ||
151 1.4.2.7 bouyer PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE_ASIC));
152 1.4.2.2 bouyer }
153 1.4.2.2 bouyer
154 1.4.2.2 bouyer /*
155 1.4.2.2 bouyer * Attach a supported board.
156 1.4.2.2 bouyer *
157 1.4.2.2 bouyer * XXX This doesn't fail gracefully.
158 1.4.2.2 bouyer */
159 1.4.2.2 bouyer static void
160 1.4.2.2 bouyer twe_attach(struct device *parent, struct device *self, void *aux)
161 1.4.2.2 bouyer {
162 1.4.2.2 bouyer struct pci_attach_args *pa;
163 1.4.2.2 bouyer struct twe_softc *sc;
164 1.4.2.2 bouyer pci_chipset_tag_t pc;
165 1.4.2.2 bouyer pci_intr_handle_t ih;
166 1.4.2.2 bouyer pcireg_t csr;
167 1.4.2.2 bouyer const char *intrstr;
168 1.4.2.2 bouyer int size, i, rv, rseg;
169 1.4.2.2 bouyer struct twe_param *dtp, *ctp;
170 1.4.2.2 bouyer bus_dma_segment_t seg;
171 1.4.2.2 bouyer struct twe_cmd *tc;
172 1.4.2.2 bouyer struct twe_attach_args twea;
173 1.4.2.2 bouyer struct twe_ccb *ccb;
174 1.4.2.2 bouyer
175 1.4.2.2 bouyer sc = (struct twe_softc *)self;
176 1.4.2.2 bouyer pa = aux;
177 1.4.2.2 bouyer pc = pa->pa_pc;
178 1.4.2.2 bouyer sc->sc_dmat = pa->pa_dmat;
179 1.4.2.2 bouyer SIMPLEQ_INIT(&sc->sc_ccb_queue);
180 1.4.2.2 bouyer SLIST_INIT(&sc->sc_ccb_freelist);
181 1.4.2.2 bouyer
182 1.4.2.3 bouyer printf(": 3ware Escalade\n");
183 1.4.2.2 bouyer
184 1.4.2.2 bouyer if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
185 1.4.2.2 bouyer &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
186 1.4.2.2 bouyer printf("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
187 1.4.2.2 bouyer return;
188 1.4.2.2 bouyer }
189 1.4.2.2 bouyer
190 1.4.2.2 bouyer /* Enable the device. */
191 1.4.2.2 bouyer csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
192 1.4.2.2 bouyer pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
193 1.4.2.2 bouyer csr | PCI_COMMAND_MASTER_ENABLE);
194 1.4.2.2 bouyer
195 1.4.2.2 bouyer /* Map and establish the interrupt. */
196 1.4.2.4 bouyer if (pci_intr_map(pa, &ih)) {
197 1.4.2.2 bouyer printf("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
198 1.4.2.2 bouyer return;
199 1.4.2.2 bouyer }
200 1.4.2.2 bouyer intrstr = pci_intr_string(pc, ih);
201 1.4.2.2 bouyer sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
202 1.4.2.2 bouyer if (sc->sc_ih == NULL) {
203 1.4.2.2 bouyer printf("%s: can't establish interrupt", sc->sc_dv.dv_xname);
204 1.4.2.2 bouyer if (intrstr != NULL)
205 1.4.2.2 bouyer printf(" at %s", intrstr);
206 1.4.2.2 bouyer printf("\n");
207 1.4.2.2 bouyer return;
208 1.4.2.2 bouyer }
209 1.4.2.2 bouyer if (intrstr != NULL)
210 1.4.2.2 bouyer printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
211 1.4.2.2 bouyer
212 1.4.2.2 bouyer /*
213 1.4.2.2 bouyer * Allocate and initialise the command blocks and CCBs.
214 1.4.2.2 bouyer */
215 1.4.2.6 bouyer size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
216 1.4.2.2 bouyer
217 1.4.2.3 bouyer if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
218 1.4.2.2 bouyer &rseg, BUS_DMA_NOWAIT)) != 0) {
219 1.4.2.2 bouyer printf("%s: unable to allocate commands, rv = %d\n",
220 1.4.2.2 bouyer sc->sc_dv.dv_xname, rv);
221 1.4.2.2 bouyer return;
222 1.4.2.2 bouyer }
223 1.4.2.2 bouyer
224 1.4.2.2 bouyer if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
225 1.4.2.2 bouyer (caddr_t *)&sc->sc_cmds,
226 1.4.2.2 bouyer BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
227 1.4.2.2 bouyer printf("%s: unable to map commands, rv = %d\n",
228 1.4.2.2 bouyer sc->sc_dv.dv_xname, rv);
229 1.4.2.2 bouyer return;
230 1.4.2.2 bouyer }
231 1.4.2.2 bouyer
232 1.4.2.2 bouyer if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
233 1.4.2.2 bouyer BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
234 1.4.2.2 bouyer printf("%s: unable to create command DMA map, rv = %d\n",
235 1.4.2.2 bouyer sc->sc_dv.dv_xname, rv);
236 1.4.2.2 bouyer return;
237 1.4.2.2 bouyer }
238 1.4.2.2 bouyer
239 1.4.2.2 bouyer if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
240 1.4.2.2 bouyer size, NULL, BUS_DMA_NOWAIT)) != 0) {
241 1.4.2.2 bouyer printf("%s: unable to load command DMA map, rv = %d\n",
242 1.4.2.2 bouyer sc->sc_dv.dv_xname, rv);
243 1.4.2.2 bouyer return;
244 1.4.2.2 bouyer }
245 1.4.2.2 bouyer
246 1.4.2.2 bouyer sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
247 1.4.2.2 bouyer memset(sc->sc_cmds, 0, size);
248 1.4.2.2 bouyer
249 1.4.2.6 bouyer ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
250 1.4.2.2 bouyer sc->sc_ccbs = ccb;
251 1.4.2.2 bouyer tc = (struct twe_cmd *)sc->sc_cmds;
252 1.4.2.2 bouyer
253 1.4.2.6 bouyer for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
254 1.4.2.2 bouyer ccb->ccb_cmd = tc;
255 1.4.2.2 bouyer ccb->ccb_cmdid = i;
256 1.4.2.2 bouyer ccb->ccb_flags = 0;
257 1.4.2.2 bouyer rv = bus_dmamap_create(sc->sc_dmat, TWE_MAX_XFER,
258 1.4.2.3 bouyer TWE_MAX_SEGS, PAGE_SIZE, 0,
259 1.4.2.3 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
260 1.4.2.2 bouyer &ccb->ccb_dmamap_xfer);
261 1.4.2.6 bouyer if (rv != 0) {
262 1.4.2.6 bouyer printf("%s: can't create dmamap, rv = %d\n",
263 1.4.2.6 bouyer sc->sc_dv.dv_xname, rv);
264 1.4.2.6 bouyer return;
265 1.4.2.6 bouyer }
266 1.4.2.3 bouyer /* Save one CCB for parameter retrieval. */
267 1.4.2.3 bouyer if (i != 0)
268 1.4.2.3 bouyer SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
269 1.4.2.3 bouyer ccb_chain.slist);
270 1.4.2.3 bouyer }
271 1.4.2.2 bouyer
272 1.4.2.2 bouyer /* Wait for the controller to become ready. */
273 1.4.2.2 bouyer if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
274 1.4.2.2 bouyer printf("%s: microcontroller not ready\n", sc->sc_dv.dv_xname);
275 1.4.2.2 bouyer return;
276 1.4.2.2 bouyer }
277 1.4.2.2 bouyer
278 1.4.2.2 bouyer TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
279 1.4.2.2 bouyer
280 1.4.2.2 bouyer /* Reset the controller. */
281 1.4.2.2 bouyer if (twe_reset(sc)) {
282 1.4.2.2 bouyer printf("%s: reset failed\n", sc->sc_dv.dv_xname);
283 1.4.2.2 bouyer return;
284 1.4.2.2 bouyer }
285 1.4.2.2 bouyer
286 1.4.2.3 bouyer /* Find attached units. */
287 1.4.2.6 bouyer rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
288 1.4.2.6 bouyer TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, (void **)&dtp);
289 1.4.2.6 bouyer if (rv != 0) {
290 1.4.2.6 bouyer printf("%s: can't detect attached units (%d)\n",
291 1.4.2.6 bouyer sc->sc_dv.dv_xname, rv);
292 1.4.2.2 bouyer return;
293 1.4.2.2 bouyer }
294 1.4.2.2 bouyer
295 1.4.2.2 bouyer /* For each detected unit, collect size and store in an array. */
296 1.4.2.3 bouyer for (i = 0, sc->sc_nunits = 0; i < TWE_MAX_UNITS; i++) {
297 1.4.2.2 bouyer /* Unit present? */
298 1.4.2.3 bouyer if ((dtp->tp_data[i] & TWE_PARAM_UNITSTATUS_Online) == 0) {
299 1.4.2.2 bouyer sc->sc_dsize[i] = 0;
300 1.4.2.2 bouyer continue;
301 1.4.2.2 bouyer }
302 1.4.2.2 bouyer
303 1.4.2.6 bouyer rv = twe_param_get(sc, TWE_PARAM_UNITINFO + i,
304 1.4.2.6 bouyer TWE_PARAM_UNITINFO_Capacity, 4, NULL, (void **)&ctp);
305 1.4.2.6 bouyer if (rv != 0) {
306 1.4.2.6 bouyer printf("%s: error %d fetching capacity for unit %d\n",
307 1.4.2.6 bouyer sc->sc_dv.dv_xname, rv, i);
308 1.4.2.2 bouyer continue;
309 1.4.2.2 bouyer }
310 1.4.2.2 bouyer
311 1.4.2.2 bouyer sc->sc_dsize[i] = le32toh(*(u_int32_t *)ctp->tp_data);
312 1.4.2.2 bouyer free(ctp, M_DEVBUF);
313 1.4.2.3 bouyer sc->sc_nunits++;
314 1.4.2.2 bouyer }
315 1.4.2.2 bouyer free(dtp, M_DEVBUF);
316 1.4.2.2 bouyer
317 1.4.2.2 bouyer /* Initialise connection with controller and enable interrupts. */
318 1.4.2.2 bouyer twe_init_connection(sc);
319 1.4.2.2 bouyer TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
320 1.4.2.2 bouyer TWE_CTL_UNMASK_RESP_INTR |
321 1.4.2.2 bouyer TWE_CTL_ENABLE_INTRS);
322 1.4.2.2 bouyer
323 1.4.2.2 bouyer /* Attach sub-devices. */
324 1.4.2.2 bouyer for (i = 0; i < TWE_MAX_UNITS; i++) {
325 1.4.2.2 bouyer if (sc->sc_dsize[i] == 0)
326 1.4.2.2 bouyer continue;
327 1.4.2.2 bouyer twea.twea_unit = i;
328 1.4.2.2 bouyer config_found_sm(&sc->sc_dv, &twea, twe_print, twe_submatch);
329 1.4.2.2 bouyer }
330 1.4.2.2 bouyer }
331 1.4.2.2 bouyer
332 1.4.2.2 bouyer /*
333 1.4.2.2 bouyer * Reset the controller. Currently only useful at attach time; must be
334 1.4.2.2 bouyer * called with interrupts blocked.
335 1.4.2.2 bouyer */
336 1.4.2.2 bouyer static int
337 1.4.2.2 bouyer twe_reset(struct twe_softc *sc)
338 1.4.2.2 bouyer {
339 1.4.2.2 bouyer struct twe_param *tp;
340 1.4.2.2 bouyer u_int aen, status;
341 1.4.2.2 bouyer volatile u_int32_t junk;
342 1.4.2.6 bouyer int got, rv;
343 1.4.2.2 bouyer
344 1.4.2.2 bouyer /* Issue a soft reset. */
345 1.4.2.2 bouyer TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
346 1.4.2.2 bouyer TWE_CTL_CLEAR_HOST_INTR |
347 1.4.2.2 bouyer TWE_CTL_CLEAR_ATTN_INTR |
348 1.4.2.2 bouyer TWE_CTL_MASK_CMD_INTR |
349 1.4.2.2 bouyer TWE_CTL_MASK_RESP_INTR |
350 1.4.2.2 bouyer TWE_CTL_CLEAR_ERROR_STS |
351 1.4.2.2 bouyer TWE_CTL_DISABLE_INTRS);
352 1.4.2.2 bouyer
353 1.4.2.2 bouyer if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 15)) {
354 1.4.2.2 bouyer printf("%s: no attention interrupt\n",
355 1.4.2.2 bouyer sc->sc_dv.dv_xname);
356 1.4.2.2 bouyer return (-1);
357 1.4.2.2 bouyer }
358 1.4.2.2 bouyer
359 1.4.2.2 bouyer /* Pull AENs out of the controller; look for a soft reset AEN. */
360 1.4.2.2 bouyer for (got = 0;;) {
361 1.4.2.6 bouyer rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
362 1.4.2.6 bouyer 2, NULL, (void **)&tp);
363 1.4.2.6 bouyer if (rv != 0)
364 1.4.2.6 bouyer printf("%s: error %d while draining response queue\n",
365 1.4.2.6 bouyer sc->sc_dv.dv_xname, rv);
366 1.4.2.3 bouyer aen = TWE_AEN_CODE(le16toh(*(u_int16_t *)tp->tp_data));
367 1.4.2.2 bouyer free(tp, M_DEVBUF);
368 1.4.2.2 bouyer if (aen == TWE_AEN_QUEUE_EMPTY)
369 1.4.2.2 bouyer break;
370 1.4.2.2 bouyer if (aen == TWE_AEN_SOFT_RESET)
371 1.4.2.2 bouyer got = 1;
372 1.4.2.2 bouyer }
373 1.4.2.2 bouyer if (!got) {
374 1.4.2.2 bouyer printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
375 1.4.2.2 bouyer return (-1);
376 1.4.2.2 bouyer }
377 1.4.2.2 bouyer
378 1.4.2.2 bouyer /* Check controller status. */
379 1.4.2.2 bouyer status = TWE_INL(sc, TWE_REG_STS);
380 1.4.2.2 bouyer if (twe_status_check(sc, status)) {
381 1.4.2.2 bouyer printf("%s: controller errors detected\n",
382 1.4.2.2 bouyer sc->sc_dv.dv_xname);
383 1.4.2.2 bouyer return (-1);
384 1.4.2.2 bouyer }
385 1.4.2.2 bouyer
386 1.4.2.2 bouyer /* Drain the response queue. */
387 1.4.2.2 bouyer for (;;) {
388 1.4.2.2 bouyer status = TWE_INL(sc, TWE_REG_STS);
389 1.4.2.2 bouyer if (twe_status_check(sc, status) != 0) {
390 1.4.2.2 bouyer printf("%s: can't drain response queue\n",
391 1.4.2.2 bouyer sc->sc_dv.dv_xname);
392 1.4.2.2 bouyer return (-1);
393 1.4.2.2 bouyer }
394 1.4.2.2 bouyer if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
395 1.4.2.2 bouyer break;
396 1.4.2.2 bouyer junk = TWE_INL(sc, TWE_REG_RESP_QUEUE);
397 1.4.2.2 bouyer }
398 1.4.2.2 bouyer
399 1.4.2.2 bouyer return (0);
400 1.4.2.2 bouyer }
401 1.4.2.2 bouyer
402 1.4.2.2 bouyer /*
403 1.4.2.2 bouyer * Print autoconfiguration message for a sub-device.
404 1.4.2.2 bouyer */
405 1.4.2.2 bouyer static int
406 1.4.2.2 bouyer twe_print(void *aux, const char *pnp)
407 1.4.2.2 bouyer {
408 1.4.2.2 bouyer struct twe_attach_args *twea;
409 1.4.2.2 bouyer
410 1.4.2.2 bouyer twea = aux;
411 1.4.2.2 bouyer
412 1.4.2.2 bouyer if (pnp != NULL)
413 1.4.2.2 bouyer printf("block device at %s", pnp);
414 1.4.2.2 bouyer printf(" unit %d", twea->twea_unit);
415 1.4.2.2 bouyer return (UNCONF);
416 1.4.2.2 bouyer }
417 1.4.2.2 bouyer
418 1.4.2.2 bouyer /*
419 1.4.2.2 bouyer * Match a sub-device.
420 1.4.2.2 bouyer */
421 1.4.2.2 bouyer static int
422 1.4.2.2 bouyer twe_submatch(struct device *parent, struct cfdata *cf, void *aux)
423 1.4.2.2 bouyer {
424 1.4.2.2 bouyer struct twe_attach_args *twea;
425 1.4.2.2 bouyer
426 1.4.2.2 bouyer twea = aux;
427 1.4.2.2 bouyer
428 1.4.2.2 bouyer if (cf->tweacf_unit != TWECF_UNIT_DEFAULT &&
429 1.4.2.2 bouyer cf->tweacf_unit != twea->twea_unit)
430 1.4.2.2 bouyer return (0);
431 1.4.2.2 bouyer
432 1.4.2.2 bouyer return ((*cf->cf_attach->ca_match)(parent, cf, aux));
433 1.4.2.2 bouyer }
434 1.4.2.2 bouyer
435 1.4.2.2 bouyer /*
436 1.4.2.2 bouyer * Interrupt service routine.
437 1.4.2.2 bouyer */
438 1.4.2.2 bouyer static int
439 1.4.2.2 bouyer twe_intr(void *arg)
440 1.4.2.2 bouyer {
441 1.4.2.2 bouyer struct twe_softc *sc;
442 1.4.2.2 bouyer u_int status;
443 1.4.2.6 bouyer int caught, rv;
444 1.4.2.2 bouyer
445 1.4.2.2 bouyer sc = arg;
446 1.4.2.2 bouyer caught = 0;
447 1.4.2.2 bouyer status = TWE_INL(sc, TWE_REG_STS);
448 1.4.2.2 bouyer twe_status_check(sc, status);
449 1.4.2.2 bouyer
450 1.4.2.2 bouyer /* Host interrupts - purpose unknown. */
451 1.4.2.2 bouyer if ((status & TWE_STS_HOST_INTR) != 0) {
452 1.4.2.2 bouyer #ifdef DIAGNOSTIC
453 1.4.2.2 bouyer printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
454 1.4.2.2 bouyer #endif
455 1.4.2.2 bouyer TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
456 1.4.2.2 bouyer caught = 1;
457 1.4.2.2 bouyer }
458 1.4.2.2 bouyer
459 1.4.2.2 bouyer /*
460 1.4.2.2 bouyer * Attention interrupts, signalled when a controller or child device
461 1.4.2.2 bouyer * state change has occured.
462 1.4.2.2 bouyer */
463 1.4.2.2 bouyer if ((status & TWE_STS_ATTN_INTR) != 0) {
464 1.4.2.7 bouyer if ((sc->sc_flags & TWEF_AEN) == 0) {
465 1.4.2.7 bouyer rv = twe_param_get(sc, TWE_PARAM_AEN,
466 1.4.2.7 bouyer TWE_PARAM_AEN_UnitCode, 2, twe_aen_handler,
467 1.4.2.7 bouyer NULL);
468 1.4.2.7 bouyer if (rv != 0) {
469 1.4.2.7 bouyer printf("%s: unable to retrieve AEN (%d)\n",
470 1.4.2.7 bouyer sc->sc_dv.dv_xname, rv);
471 1.4.2.7 bouyer TWE_OUTL(sc, TWE_REG_CTL,
472 1.4.2.7 bouyer TWE_CTL_CLEAR_ATTN_INTR);
473 1.4.2.7 bouyer } else
474 1.4.2.7 bouyer sc->sc_flags |= TWEF_AEN;
475 1.4.2.6 bouyer }
476 1.4.2.2 bouyer caught = 1;
477 1.4.2.2 bouyer }
478 1.4.2.2 bouyer
479 1.4.2.2 bouyer /*
480 1.4.2.2 bouyer * Command interrupts, signalled when the controller can accept more
481 1.4.2.2 bouyer * commands. We don't use this; instead, we try to submit commands
482 1.4.2.2 bouyer * when we receive them, and when other commands have completed.
483 1.4.2.2 bouyer * Mask it so we don't get another one.
484 1.4.2.2 bouyer */
485 1.4.2.2 bouyer if ((status & TWE_STS_CMD_INTR) != 0) {
486 1.4.2.2 bouyer #ifdef DIAGNOSTIC
487 1.4.2.2 bouyer printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
488 1.4.2.2 bouyer #endif
489 1.4.2.2 bouyer TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
490 1.4.2.2 bouyer caught = 1;
491 1.4.2.2 bouyer }
492 1.4.2.2 bouyer
493 1.4.2.2 bouyer if ((status & TWE_STS_RESP_INTR) != 0) {
494 1.4.2.2 bouyer twe_poll(sc);
495 1.4.2.2 bouyer caught = 1;
496 1.4.2.2 bouyer }
497 1.4.2.2 bouyer
498 1.4.2.2 bouyer return (caught);
499 1.4.2.2 bouyer }
500 1.4.2.2 bouyer
501 1.4.2.2 bouyer /*
502 1.4.2.2 bouyer * Handle an AEN returned by the controller.
503 1.4.2.2 bouyer */
504 1.4.2.2 bouyer static void
505 1.4.2.2 bouyer twe_aen_handler(struct twe_ccb *ccb, int error)
506 1.4.2.2 bouyer {
507 1.4.2.2 bouyer struct twe_softc *sc;
508 1.4.2.2 bouyer struct twe_param *tp;
509 1.4.2.2 bouyer const char *str;
510 1.4.2.2 bouyer u_int aen;
511 1.4.2.6 bouyer int i, hu, rv;
512 1.4.2.2 bouyer
513 1.4.2.2 bouyer sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
514 1.4.2.2 bouyer tp = ccb->ccb_tx.tx_context;
515 1.4.2.2 bouyer twe_ccb_unmap(sc, ccb);
516 1.4.2.2 bouyer
517 1.4.2.3 bouyer if (error) {
518 1.4.2.2 bouyer printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
519 1.4.2.3 bouyer aen = TWE_AEN_QUEUE_EMPTY;
520 1.4.2.3 bouyer } else
521 1.4.2.2 bouyer aen = le16toh(*(u_int16_t *)tp->tp_data);
522 1.4.2.3 bouyer free(tp, M_DEVBUF);
523 1.4.2.3 bouyer twe_ccb_free(sc, ccb);
524 1.4.2.3 bouyer
525 1.4.2.6 bouyer if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
526 1.4.2.6 bouyer TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
527 1.4.2.7 bouyer sc->sc_flags &= ~TWEF_AEN;
528 1.4.2.6 bouyer return;
529 1.4.2.6 bouyer }
530 1.4.2.2 bouyer
531 1.4.2.6 bouyer str = "<unknown>";
532 1.4.2.6 bouyer i = 0;
533 1.4.2.6 bouyer hu = 0;
534 1.4.2.6 bouyer
535 1.4.2.6 bouyer while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
536 1.4.2.6 bouyer if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
537 1.4.2.6 bouyer str = twe_aen_names[i].desc;
538 1.4.2.6 bouyer hu = (TWE_AEN_UNIT(twe_aen_names[i].aen) != 0);
539 1.4.2.6 bouyer break;
540 1.4.2.6 bouyer }
541 1.4.2.6 bouyer i++;
542 1.4.2.3 bouyer }
543 1.4.2.6 bouyer printf("%s: AEN 0x%04x (%s) received", sc->sc_dv.dv_xname,
544 1.4.2.6 bouyer TWE_AEN_CODE(aen), str);
545 1.4.2.6 bouyer if (hu != 0)
546 1.4.2.6 bouyer printf(" for unit %d", TWE_AEN_UNIT(aen));
547 1.4.2.6 bouyer printf("\n");
548 1.4.2.6 bouyer
549 1.4.2.6 bouyer /*
550 1.4.2.6 bouyer * Chain another retrieval in case interrupts have been
551 1.4.2.6 bouyer * coalesced.
552 1.4.2.6 bouyer */
553 1.4.2.6 bouyer rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
554 1.4.2.6 bouyer twe_aen_handler, NULL);
555 1.4.2.6 bouyer if (rv != 0)
556 1.4.2.6 bouyer printf("%s: unable to retrieve AEN (%d)\n",
557 1.4.2.6 bouyer sc->sc_dv.dv_xname, rv);
558 1.4.2.2 bouyer }
559 1.4.2.2 bouyer
560 1.4.2.2 bouyer /*
561 1.4.2.2 bouyer * Execute a TWE_OP_GET_PARAM command. If a callback function is provided,
562 1.4.2.2 bouyer * it will be called with generated context when the command has completed.
563 1.4.2.2 bouyer * If no callback is provided, the command will be executed synchronously
564 1.4.2.3 bouyer * and a pointer to a buffer containing the data returned.
565 1.4.2.2 bouyer *
566 1.4.2.3 bouyer * The caller or callback is responsible for freeing the buffer.
567 1.4.2.2 bouyer */
568 1.4.2.6 bouyer static int
569 1.4.2.2 bouyer twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
570 1.4.2.6 bouyer void (*func)(struct twe_ccb *, int), void **pbuf)
571 1.4.2.2 bouyer {
572 1.4.2.2 bouyer struct twe_ccb *ccb;
573 1.4.2.2 bouyer struct twe_cmd *tc;
574 1.4.2.2 bouyer struct twe_param *tp;
575 1.4.2.2 bouyer int rv, s;
576 1.4.2.2 bouyer
577 1.4.2.6 bouyer rv = twe_ccb_alloc(sc, &ccb,
578 1.4.2.6 bouyer TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
579 1.4.2.6 bouyer if (rv != 0)
580 1.4.2.6 bouyer return (rv);
581 1.4.2.6 bouyer
582 1.4.2.2 bouyer tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
583 1.4.2.6 bouyer if (pbuf != NULL)
584 1.4.2.6 bouyer *pbuf = tp;
585 1.4.2.2 bouyer
586 1.4.2.2 bouyer ccb->ccb_data = tp;
587 1.4.2.2 bouyer ccb->ccb_datasize = TWE_SECTOR_SIZE;
588 1.4.2.2 bouyer ccb->ccb_tx.tx_handler = func;
589 1.4.2.2 bouyer ccb->ccb_tx.tx_context = tp;
590 1.4.2.2 bouyer ccb->ccb_tx.tx_dv = &sc->sc_dv;
591 1.4.2.2 bouyer
592 1.4.2.2 bouyer tc = ccb->ccb_cmd;
593 1.4.2.2 bouyer tc->tc_size = 2;
594 1.4.2.2 bouyer tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
595 1.4.2.2 bouyer tc->tc_unit = 0;
596 1.4.2.2 bouyer tc->tc_count = htole16(1);
597 1.4.2.2 bouyer
598 1.4.2.2 bouyer /* Fill in the outbound parameter data. */
599 1.4.2.2 bouyer tp->tp_table_id = htole16(table_id);
600 1.4.2.2 bouyer tp->tp_param_id = param_id;
601 1.4.2.2 bouyer tp->tp_param_size = size;
602 1.4.2.2 bouyer
603 1.4.2.2 bouyer /* Map the transfer. */
604 1.4.2.6 bouyer if ((rv = twe_ccb_map(sc, ccb)) != 0) {
605 1.4.2.2 bouyer twe_ccb_free(sc, ccb);
606 1.4.2.2 bouyer free(tp, M_DEVBUF);
607 1.4.2.6 bouyer return (rv);
608 1.4.2.2 bouyer }
609 1.4.2.2 bouyer
610 1.4.2.2 bouyer /* Submit the command and either wait or let the callback handle it. */
611 1.4.2.2 bouyer if (func == NULL) {
612 1.4.2.2 bouyer s = splbio();
613 1.4.2.6 bouyer rv = twe_ccb_poll(sc, ccb, 5);
614 1.4.2.2 bouyer twe_ccb_unmap(sc, ccb);
615 1.4.2.2 bouyer twe_ccb_free(sc, ccb);
616 1.4.2.2 bouyer splx(s);
617 1.4.2.6 bouyer if (rv != 0)
618 1.4.2.2 bouyer free(tp, M_DEVBUF);
619 1.4.2.2 bouyer } else {
620 1.4.2.2 bouyer twe_ccb_enqueue(sc, ccb);
621 1.4.2.6 bouyer rv = 0;
622 1.4.2.2 bouyer }
623 1.4.2.2 bouyer
624 1.4.2.6 bouyer return (rv);
625 1.4.2.2 bouyer }
626 1.4.2.2 bouyer
627 1.4.2.2 bouyer /*
628 1.4.2.2 bouyer * Execute a TWE_OP_INIT_CONNECTION command. Return non-zero on error.
629 1.4.2.2 bouyer * Must be called with interrupts blocked.
630 1.4.2.2 bouyer */
631 1.4.2.2 bouyer static int
632 1.4.2.2 bouyer twe_init_connection(struct twe_softc *sc)
633 1.4.2.2 bouyer {
634 1.4.2.2 bouyer struct twe_ccb *ccb;
635 1.4.2.2 bouyer struct twe_cmd *tc;
636 1.4.2.2 bouyer int rv;
637 1.4.2.2 bouyer
638 1.4.2.3 bouyer if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
639 1.4.2.2 bouyer return (rv);
640 1.4.2.2 bouyer
641 1.4.2.2 bouyer /* Build the command. */
642 1.4.2.2 bouyer tc = ccb->ccb_cmd;
643 1.4.2.2 bouyer tc->tc_size = 3;
644 1.4.2.2 bouyer tc->tc_opcode = TWE_OP_INIT_CONNECTION;
645 1.4.2.2 bouyer tc->tc_unit = 0;
646 1.4.2.3 bouyer tc->tc_count = htole16(TWE_MAX_CMDS);
647 1.4.2.2 bouyer tc->tc_args.init_connection.response_queue_pointer = 0;
648 1.4.2.2 bouyer
649 1.4.2.2 bouyer /* Submit the command for immediate execution. */
650 1.4.2.6 bouyer rv = twe_ccb_poll(sc, ccb, 5);
651 1.4.2.2 bouyer twe_ccb_free(sc, ccb);
652 1.4.2.2 bouyer return (rv);
653 1.4.2.2 bouyer }
654 1.4.2.2 bouyer
655 1.4.2.2 bouyer /*
656 1.4.2.2 bouyer * Poll the controller for completed commands. Must be called with
657 1.4.2.2 bouyer * interrupts blocked.
658 1.4.2.2 bouyer */
659 1.4.2.2 bouyer static void
660 1.4.2.2 bouyer twe_poll(struct twe_softc *sc)
661 1.4.2.2 bouyer {
662 1.4.2.2 bouyer struct twe_ccb *ccb;
663 1.4.2.2 bouyer int found;
664 1.4.2.2 bouyer u_int status, cmdid;
665 1.4.2.2 bouyer
666 1.4.2.2 bouyer found = 0;
667 1.4.2.2 bouyer
668 1.4.2.2 bouyer for (;;) {
669 1.4.2.2 bouyer status = TWE_INL(sc, TWE_REG_STS);
670 1.4.2.2 bouyer twe_status_check(sc, status);
671 1.4.2.2 bouyer
672 1.4.2.2 bouyer if ((status & TWE_STS_RESP_QUEUE_EMPTY))
673 1.4.2.2 bouyer break;
674 1.4.2.2 bouyer
675 1.4.2.2 bouyer found = 1;
676 1.4.2.2 bouyer cmdid = TWE_INL(sc, TWE_REG_RESP_QUEUE);
677 1.4.2.2 bouyer cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
678 1.4.2.6 bouyer if (cmdid >= TWE_MAX_QUEUECNT) {
679 1.4.2.2 bouyer printf("%s: bad completion\n", sc->sc_dv.dv_xname);
680 1.4.2.2 bouyer continue;
681 1.4.2.2 bouyer }
682 1.4.2.2 bouyer
683 1.4.2.2 bouyer ccb = sc->sc_ccbs + cmdid;
684 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
685 1.4.2.2 bouyer printf("%s: bad completion (not active)\n",
686 1.4.2.2 bouyer sc->sc_dv.dv_xname);
687 1.4.2.2 bouyer continue;
688 1.4.2.2 bouyer }
689 1.4.2.2 bouyer ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
690 1.4.2.2 bouyer
691 1.4.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
692 1.4.2.2 bouyer (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
693 1.4.2.2 bouyer sizeof(struct twe_cmd),
694 1.4.2.2 bouyer BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
695 1.4.2.2 bouyer
696 1.4.2.2 bouyer /* Pass notification to upper layers. */
697 1.4.2.2 bouyer if (ccb->ccb_tx.tx_handler != NULL)
698 1.4.2.2 bouyer (*ccb->ccb_tx.tx_handler)(ccb,
699 1.4.2.2 bouyer ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
700 1.4.2.2 bouyer }
701 1.4.2.2 bouyer
702 1.4.2.2 bouyer /* If any commands have completed, run the software queue. */
703 1.4.2.2 bouyer if (found)
704 1.4.2.2 bouyer twe_ccb_enqueue(sc, NULL);
705 1.4.2.2 bouyer }
706 1.4.2.2 bouyer
707 1.4.2.2 bouyer /*
708 1.4.2.2 bouyer * Wait for `status' to be set in the controller status register. Return
709 1.4.2.2 bouyer * zero if found, non-zero if the operation timed out.
710 1.4.2.2 bouyer */
711 1.4.2.2 bouyer static int
712 1.4.2.2 bouyer twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
713 1.4.2.2 bouyer {
714 1.4.2.2 bouyer
715 1.4.2.7 bouyer for (timo *= 10; timo != 0; timo--) {
716 1.4.2.2 bouyer if ((TWE_INL(sc, TWE_REG_STS) & status) == status)
717 1.4.2.2 bouyer break;
718 1.4.2.2 bouyer delay(100000);
719 1.4.2.2 bouyer }
720 1.4.2.2 bouyer
721 1.4.2.2 bouyer return (timo == 0);
722 1.4.2.2 bouyer }
723 1.4.2.2 bouyer
724 1.4.2.2 bouyer /*
725 1.4.2.2 bouyer * Complain if the status bits aren't what we expect.
726 1.4.2.2 bouyer */
727 1.4.2.2 bouyer static int
728 1.4.2.2 bouyer twe_status_check(struct twe_softc *sc, u_int status)
729 1.4.2.2 bouyer {
730 1.4.2.2 bouyer int rv;
731 1.4.2.2 bouyer
732 1.4.2.2 bouyer rv = 0;
733 1.4.2.2 bouyer
734 1.4.2.2 bouyer if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
735 1.4.2.2 bouyer printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
736 1.4.2.2 bouyer status & ~TWE_STS_EXPECTED_BITS);
737 1.4.2.2 bouyer rv = -1;
738 1.4.2.2 bouyer }
739 1.4.2.2 bouyer
740 1.4.2.2 bouyer if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
741 1.4.2.2 bouyer printf("%s: unexpected status bits: 0x%08x\n",
742 1.4.2.2 bouyer sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
743 1.4.2.2 bouyer rv = -1;
744 1.4.2.2 bouyer }
745 1.4.2.2 bouyer
746 1.4.2.2 bouyer return (rv);
747 1.4.2.2 bouyer }
748 1.4.2.2 bouyer
749 1.4.2.2 bouyer /*
750 1.4.2.2 bouyer * Allocate and initialise a CCB.
751 1.4.2.2 bouyer */
752 1.4.2.2 bouyer int
753 1.4.2.3 bouyer twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
754 1.4.2.2 bouyer {
755 1.4.2.2 bouyer struct twe_cmd *tc;
756 1.4.2.2 bouyer struct twe_ccb *ccb;
757 1.4.2.2 bouyer int s;
758 1.4.2.2 bouyer
759 1.4.2.6 bouyer s = splbio();
760 1.4.2.3 bouyer if ((flags & TWE_CCB_PARAM) != 0)
761 1.4.2.3 bouyer ccb = sc->sc_ccbs;
762 1.4.2.3 bouyer else {
763 1.4.2.3 bouyer /* Allocate a CCB and command block. */
764 1.4.2.3 bouyer if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
765 1.4.2.2 bouyer splx(s);
766 1.4.2.2 bouyer return (EAGAIN);
767 1.4.2.2 bouyer }
768 1.4.2.3 bouyer ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
769 1.4.2.3 bouyer SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
770 1.4.2.2 bouyer }
771 1.4.2.3 bouyer #ifdef DIAGNOSTIC
772 1.4.2.3 bouyer if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
773 1.4.2.3 bouyer panic("twe_ccb_alloc: CCB already allocated");
774 1.4.2.3 bouyer flags |= TWE_CCB_ALLOCED;
775 1.4.2.3 bouyer #endif
776 1.4.2.6 bouyer splx(s);
777 1.4.2.2 bouyer
778 1.4.2.2 bouyer /* Initialise some fields and return. */
779 1.4.2.2 bouyer ccb->ccb_tx.tx_handler = NULL;
780 1.4.2.3 bouyer ccb->ccb_flags = flags;
781 1.4.2.2 bouyer tc = ccb->ccb_cmd;
782 1.4.2.2 bouyer tc->tc_status = 0;
783 1.4.2.2 bouyer tc->tc_flags = 0;
784 1.4.2.2 bouyer tc->tc_cmdid = ccb->ccb_cmdid;
785 1.4.2.2 bouyer *ccbp = ccb;
786 1.4.2.3 bouyer
787 1.4.2.2 bouyer return (0);
788 1.4.2.2 bouyer }
789 1.4.2.2 bouyer
790 1.4.2.2 bouyer /*
791 1.4.2.3 bouyer * Free a CCB.
792 1.4.2.2 bouyer */
793 1.4.2.2 bouyer void
794 1.4.2.2 bouyer twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
795 1.4.2.2 bouyer {
796 1.4.2.2 bouyer int s;
797 1.4.2.2 bouyer
798 1.4.2.2 bouyer s = splbio();
799 1.4.2.3 bouyer if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
800 1.4.2.3 bouyer SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
801 1.4.2.3 bouyer ccb->ccb_flags = 0;
802 1.4.2.2 bouyer splx(s);
803 1.4.2.2 bouyer }
804 1.4.2.2 bouyer
805 1.4.2.2 bouyer /*
806 1.4.2.2 bouyer * Map the specified CCB's command block and data buffer (if any) into
807 1.4.2.2 bouyer * controller visible space. Perform DMA synchronisation.
808 1.4.2.2 bouyer */
809 1.4.2.2 bouyer int
810 1.4.2.2 bouyer twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
811 1.4.2.2 bouyer {
812 1.4.2.2 bouyer struct twe_cmd *tc;
813 1.4.2.6 bouyer int flags, nsegs, i, s, rv;
814 1.4.2.2 bouyer void *data;
815 1.4.2.2 bouyer
816 1.4.2.6 bouyer /*
817 1.4.2.6 bouyer * The data as a whole must be 512-byte aligned.
818 1.4.2.6 bouyer */
819 1.4.2.2 bouyer if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
820 1.4.2.5 bouyer s = splvm();
821 1.4.2.2 bouyer /* XXX */
822 1.4.2.2 bouyer ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
823 1.4.2.2 bouyer ccb->ccb_datasize, UVM_KMF_NOWAIT);
824 1.4.2.2 bouyer splx(s);
825 1.4.2.2 bouyer data = (void *)ccb->ccb_abuf;
826 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
827 1.4.2.2 bouyer memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
828 1.4.2.2 bouyer } else {
829 1.4.2.2 bouyer ccb->ccb_abuf = (vaddr_t)0;
830 1.4.2.2 bouyer data = ccb->ccb_data;
831 1.4.2.2 bouyer }
832 1.4.2.2 bouyer
833 1.4.2.6 bouyer /*
834 1.4.2.6 bouyer * Map the data buffer into bus space and build the S/G list.
835 1.4.2.6 bouyer */
836 1.4.2.6 bouyer rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
837 1.4.2.7 bouyer ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING);
838 1.4.2.6 bouyer if (rv != 0) {
839 1.4.2.6 bouyer if (ccb->ccb_abuf != (vaddr_t)0) {
840 1.4.2.6 bouyer s = splvm();
841 1.4.2.6 bouyer /* XXX */
842 1.4.2.6 bouyer uvm_km_free(kmem_map, ccb->ccb_abuf,
843 1.4.2.6 bouyer ccb->ccb_datasize);
844 1.4.2.6 bouyer splx(s);
845 1.4.2.6 bouyer }
846 1.4.2.6 bouyer return (rv);
847 1.4.2.6 bouyer }
848 1.4.2.2 bouyer
849 1.4.2.2 bouyer nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
850 1.4.2.2 bouyer tc = ccb->ccb_cmd;
851 1.4.2.2 bouyer tc->tc_size += 2 * nsegs;
852 1.4.2.2 bouyer
853 1.4.2.2 bouyer /* The location of the S/G list is dependant upon command type. */
854 1.4.2.2 bouyer switch (tc->tc_opcode >> 5) {
855 1.4.2.2 bouyer case 2:
856 1.4.2.2 bouyer for (i = 0; i < nsegs; i++) {
857 1.4.2.2 bouyer tc->tc_args.param.sgl[i].tsg_address =
858 1.4.2.2 bouyer htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
859 1.4.2.2 bouyer tc->tc_args.param.sgl[i].tsg_length =
860 1.4.2.2 bouyer htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
861 1.4.2.2 bouyer }
862 1.4.2.2 bouyer /* XXX Needed? */
863 1.4.2.2 bouyer for (; i < TWE_SG_SIZE; i++) {
864 1.4.2.2 bouyer tc->tc_args.param.sgl[i].tsg_address = 0;
865 1.4.2.2 bouyer tc->tc_args.param.sgl[i].tsg_length = 0;
866 1.4.2.2 bouyer }
867 1.4.2.2 bouyer break;
868 1.4.2.2 bouyer case 3:
869 1.4.2.2 bouyer for (i = 0; i < nsegs; i++) {
870 1.4.2.2 bouyer tc->tc_args.io.sgl[i].tsg_address =
871 1.4.2.2 bouyer htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
872 1.4.2.2 bouyer tc->tc_args.io.sgl[i].tsg_length =
873 1.4.2.2 bouyer htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
874 1.4.2.2 bouyer }
875 1.4.2.2 bouyer /* XXX Needed? */
876 1.4.2.2 bouyer for (; i < TWE_SG_SIZE; i++) {
877 1.4.2.2 bouyer tc->tc_args.io.sgl[i].tsg_address = 0;
878 1.4.2.2 bouyer tc->tc_args.io.sgl[i].tsg_length = 0;
879 1.4.2.2 bouyer }
880 1.4.2.2 bouyer break;
881 1.4.2.2 bouyer #ifdef DEBUG
882 1.4.2.2 bouyer default:
883 1.4.2.2 bouyer panic("twe_ccb_map: oops");
884 1.4.2.2 bouyer #endif
885 1.4.2.2 bouyer }
886 1.4.2.2 bouyer
887 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
888 1.4.2.2 bouyer flags = BUS_DMASYNC_PREREAD;
889 1.4.2.2 bouyer else
890 1.4.2.2 bouyer flags = 0;
891 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
892 1.4.2.2 bouyer flags |= BUS_DMASYNC_PREWRITE;
893 1.4.2.2 bouyer
894 1.4.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
895 1.4.2.2 bouyer ccb->ccb_datasize, flags);
896 1.4.2.2 bouyer return (0);
897 1.4.2.2 bouyer }
898 1.4.2.2 bouyer
899 1.4.2.2 bouyer /*
900 1.4.2.2 bouyer * Unmap the specified CCB's command block and data buffer (if any) and
901 1.4.2.2 bouyer * perform DMA synchronisation.
902 1.4.2.2 bouyer */
903 1.4.2.2 bouyer void
904 1.4.2.2 bouyer twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
905 1.4.2.2 bouyer {
906 1.4.2.2 bouyer int flags, s;
907 1.4.2.2 bouyer
908 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
909 1.4.2.2 bouyer flags = BUS_DMASYNC_POSTREAD;
910 1.4.2.2 bouyer else
911 1.4.2.2 bouyer flags = 0;
912 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
913 1.4.2.2 bouyer flags |= BUS_DMASYNC_POSTWRITE;
914 1.4.2.2 bouyer
915 1.4.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
916 1.4.2.2 bouyer ccb->ccb_datasize, flags);
917 1.4.2.2 bouyer bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
918 1.4.2.2 bouyer
919 1.4.2.2 bouyer if (ccb->ccb_abuf != (vaddr_t)0) {
920 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
921 1.4.2.2 bouyer memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
922 1.4.2.2 bouyer ccb->ccb_datasize);
923 1.4.2.5 bouyer s = splvm();
924 1.4.2.2 bouyer /* XXX */
925 1.4.2.2 bouyer uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
926 1.4.2.2 bouyer splx(s);
927 1.4.2.2 bouyer }
928 1.4.2.2 bouyer }
929 1.4.2.2 bouyer
930 1.4.2.2 bouyer /*
931 1.4.2.6 bouyer * Submit a command to the controller and poll on completion. Return
932 1.4.2.6 bouyer * non-zero on timeout (but don't check status, as some command types don't
933 1.4.2.6 bouyer * return status). Must be called with interrupts blocked.
934 1.4.2.2 bouyer */
935 1.4.2.2 bouyer int
936 1.4.2.2 bouyer twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
937 1.4.2.2 bouyer {
938 1.4.2.6 bouyer int rv;
939 1.4.2.6 bouyer
940 1.4.2.6 bouyer if ((rv = twe_ccb_submit(sc, ccb)) != 0)
941 1.4.2.6 bouyer return (rv);
942 1.4.2.2 bouyer
943 1.4.2.2 bouyer for (; timo != 0; timo--) {
944 1.4.2.2 bouyer twe_poll(sc);
945 1.4.2.2 bouyer if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
946 1.4.2.2 bouyer break;
947 1.4.2.2 bouyer DELAY(100000);
948 1.4.2.2 bouyer }
949 1.4.2.2 bouyer
950 1.4.2.2 bouyer return (timo == 0);
951 1.4.2.2 bouyer }
952 1.4.2.2 bouyer
953 1.4.2.2 bouyer /*
954 1.4.2.2 bouyer * If a CCB is specified, enqueue it. Pull CCBs off the software queue in
955 1.4.2.2 bouyer * the order that they were enqueued and try to submit their command blocks
956 1.4.2.2 bouyer * to the controller for execution.
957 1.4.2.2 bouyer */
958 1.4.2.2 bouyer void
959 1.4.2.2 bouyer twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
960 1.4.2.2 bouyer {
961 1.4.2.2 bouyer int s;
962 1.4.2.2 bouyer
963 1.4.2.2 bouyer s = splbio();
964 1.4.2.2 bouyer
965 1.4.2.2 bouyer if (ccb != NULL)
966 1.4.2.2 bouyer SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
967 1.4.2.2 bouyer
968 1.4.2.2 bouyer while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
969 1.4.2.2 bouyer if (twe_ccb_submit(sc, ccb))
970 1.4.2.2 bouyer break;
971 1.4.2.2 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
972 1.4.2.2 bouyer }
973 1.4.2.2 bouyer
974 1.4.2.2 bouyer splx(s);
975 1.4.2.2 bouyer }
976 1.4.2.2 bouyer
977 1.4.2.2 bouyer /*
978 1.4.2.2 bouyer * Submit the command block associated with the specified CCB to the
979 1.4.2.2 bouyer * controller for execution. Must be called with interrupts blocked.
980 1.4.2.2 bouyer */
981 1.4.2.2 bouyer int
982 1.4.2.2 bouyer twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
983 1.4.2.2 bouyer {
984 1.4.2.2 bouyer bus_addr_t pa;
985 1.4.2.2 bouyer int rv;
986 1.4.2.2 bouyer u_int status;
987 1.4.2.2 bouyer
988 1.4.2.2 bouyer /* Check to see if we can post a command. */
989 1.4.2.2 bouyer status = TWE_INL(sc, TWE_REG_STS);
990 1.4.2.2 bouyer twe_status_check(sc, status);
991 1.4.2.2 bouyer
992 1.4.2.2 bouyer if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
993 1.4.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
994 1.4.2.2 bouyer (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
995 1.4.2.2 bouyer BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
996 1.4.2.2 bouyer ccb->ccb_flags |= TWE_CCB_ACTIVE;
997 1.4.2.2 bouyer pa = sc->sc_cmds_paddr +
998 1.4.2.2 bouyer ccb->ccb_cmdid * sizeof(struct twe_cmd);
999 1.4.2.2 bouyer TWE_OUTL(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
1000 1.4.2.2 bouyer rv = 0;
1001 1.4.2.2 bouyer } else
1002 1.4.2.2 bouyer rv = EBUSY;
1003 1.4.2.2 bouyer
1004 1.4.2.2 bouyer return (rv);
1005 1.4.2.2 bouyer }
1006