cac.c revision 1.15.2.2 1 1.15.2.2 bouyer /* $NetBSD: cac.c,v 1.15.2.2 2000/11/20 11:40:25 bouyer Exp $ */
2 1.15.2.2 bouyer
3 1.15.2.2 bouyer /*-
4 1.15.2.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.15.2.2 bouyer * All rights reserved.
6 1.15.2.2 bouyer *
7 1.15.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.15.2.2 bouyer * by Andrew Doran.
9 1.15.2.2 bouyer *
10 1.15.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.15.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.15.2.2 bouyer * are met:
13 1.15.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.15.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.15.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.15.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.15.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.15.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.15.2.2 bouyer * must display the following acknowledgement:
20 1.15.2.2 bouyer * This product includes software developed by the NetBSD
21 1.15.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.15.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.15.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.15.2.2 bouyer * from this software without specific prior written permission.
25 1.15.2.2 bouyer *
26 1.15.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.15.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.15.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.15.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.15.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.15.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.15.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.15.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.15.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.15.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.15.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.15.2.2 bouyer */
38 1.15.2.2 bouyer
39 1.15.2.2 bouyer /*
40 1.15.2.2 bouyer * Driver for Compaq array controllers.
41 1.15.2.2 bouyer */
42 1.15.2.2 bouyer
43 1.15.2.2 bouyer #include <sys/param.h>
44 1.15.2.2 bouyer #include <sys/systm.h>
45 1.15.2.2 bouyer #include <sys/kernel.h>
46 1.15.2.2 bouyer #include <sys/device.h>
47 1.15.2.2 bouyer #include <sys/queue.h>
48 1.15.2.2 bouyer #include <sys/proc.h>
49 1.15.2.2 bouyer #include <sys/buf.h>
50 1.15.2.2 bouyer #include <sys/endian.h>
51 1.15.2.2 bouyer #include <sys/malloc.h>
52 1.15.2.2 bouyer #include <sys/pool.h>
53 1.15.2.2 bouyer
54 1.15.2.2 bouyer #include <machine/bswap.h>
55 1.15.2.2 bouyer #include <machine/bus.h>
56 1.15.2.2 bouyer
57 1.15.2.2 bouyer #include <dev/ic/cacreg.h>
58 1.15.2.2 bouyer #include <dev/ic/cacvar.h>
59 1.15.2.2 bouyer
60 1.15.2.2 bouyer static struct cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
61 1.15.2.2 bouyer static void cac_ccb_done(struct cac_softc *, struct cac_ccb *);
62 1.15.2.2 bouyer static void cac_ccb_free(struct cac_softc *, struct cac_ccb *);
63 1.15.2.2 bouyer static int cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
64 1.15.2.2 bouyer static int cac_ccb_start(struct cac_softc *, struct cac_ccb *);
65 1.15.2.2 bouyer static int cac_print(void *, const char *);
66 1.15.2.2 bouyer static void cac_shutdown(void *);
67 1.15.2.2 bouyer static int cac_submatch(struct device *, struct cfdata *, void *);
68 1.15.2.2 bouyer
69 1.15.2.2 bouyer static struct cac_ccb *cac_l0_completed(struct cac_softc *);
70 1.15.2.2 bouyer static int cac_l0_fifo_full(struct cac_softc *);
71 1.15.2.2 bouyer static void cac_l0_intr_enable(struct cac_softc *, int);
72 1.15.2.2 bouyer static int cac_l0_intr_pending(struct cac_softc *);
73 1.15.2.2 bouyer static void cac_l0_submit(struct cac_softc *, struct cac_ccb *);
74 1.15.2.2 bouyer
75 1.15.2.2 bouyer static void *cac_sdh; /* shutdown hook */
76 1.15.2.2 bouyer
77 1.15.2.2 bouyer struct cac_linkage cac_l0 = {
78 1.15.2.2 bouyer cac_l0_completed,
79 1.15.2.2 bouyer cac_l0_fifo_full,
80 1.15.2.2 bouyer cac_l0_intr_enable,
81 1.15.2.2 bouyer cac_l0_intr_pending,
82 1.15.2.2 bouyer cac_l0_submit
83 1.15.2.2 bouyer };
84 1.15.2.2 bouyer
85 1.15.2.2 bouyer /*
86 1.15.2.2 bouyer * Initialise our interface to the controller.
87 1.15.2.2 bouyer */
88 1.15.2.2 bouyer int
89 1.15.2.2 bouyer cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
90 1.15.2.2 bouyer {
91 1.15.2.2 bouyer struct cac_controller_info cinfo;
92 1.15.2.2 bouyer struct cac_attach_args caca;
93 1.15.2.2 bouyer int error, rseg, size, i;
94 1.15.2.2 bouyer bus_dma_segment_t seg;
95 1.15.2.2 bouyer struct cac_ccb *ccb;
96 1.15.2.2 bouyer
97 1.15.2.2 bouyer if (intrstr != NULL)
98 1.15.2.2 bouyer printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
99 1.15.2.2 bouyer intrstr);
100 1.15.2.2 bouyer
101 1.15.2.2 bouyer SIMPLEQ_INIT(&sc->sc_ccb_free);
102 1.15.2.2 bouyer SIMPLEQ_INIT(&sc->sc_ccb_queue);
103 1.15.2.2 bouyer
104 1.15.2.2 bouyer size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
105 1.15.2.2 bouyer
106 1.15.2.2 bouyer if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1,
107 1.15.2.2 bouyer &rseg, BUS_DMA_NOWAIT)) != 0) {
108 1.15.2.2 bouyer printf("%s: unable to allocate CCBs, error = %d\n",
109 1.15.2.2 bouyer sc->sc_dv.dv_xname, error);
110 1.15.2.2 bouyer return (-1);
111 1.15.2.2 bouyer }
112 1.15.2.2 bouyer
113 1.15.2.2 bouyer if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
114 1.15.2.2 bouyer (caddr_t *)&sc->sc_ccbs,
115 1.15.2.2 bouyer BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
116 1.15.2.2 bouyer printf("%s: unable to map CCBs, error = %d\n",
117 1.15.2.2 bouyer sc->sc_dv.dv_xname, error);
118 1.15.2.2 bouyer return (-1);
119 1.15.2.2 bouyer }
120 1.15.2.2 bouyer
121 1.15.2.2 bouyer if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
122 1.15.2.2 bouyer BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
123 1.15.2.2 bouyer printf("%s: unable to create CCB DMA map, error = %d\n",
124 1.15.2.2 bouyer sc->sc_dv.dv_xname, error);
125 1.15.2.2 bouyer return (-1);
126 1.15.2.2 bouyer }
127 1.15.2.2 bouyer
128 1.15.2.2 bouyer if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
129 1.15.2.2 bouyer size, NULL, BUS_DMA_NOWAIT)) != 0) {
130 1.15.2.2 bouyer printf("%s: unable to load CCB DMA map, error = %d\n",
131 1.15.2.2 bouyer sc->sc_dv.dv_xname, error);
132 1.15.2.2 bouyer return (-1);
133 1.15.2.2 bouyer }
134 1.15.2.2 bouyer
135 1.15.2.2 bouyer sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
136 1.15.2.2 bouyer memset(sc->sc_ccbs, 0, size);
137 1.15.2.2 bouyer ccb = (struct cac_ccb *)sc->sc_ccbs;
138 1.15.2.2 bouyer
139 1.15.2.2 bouyer for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
140 1.15.2.2 bouyer /* Create the DMA map for this CCB's data */
141 1.15.2.2 bouyer error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
142 1.15.2.2 bouyer CAC_SG_SIZE, CAC_MAX_XFER, 0,
143 1.15.2.2 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
144 1.15.2.2 bouyer &ccb->ccb_dmamap_xfer);
145 1.15.2.2 bouyer
146 1.15.2.2 bouyer if (error) {
147 1.15.2.2 bouyer printf("%s: can't create ccb dmamap (%d)\n",
148 1.15.2.2 bouyer sc->sc_dv.dv_xname, error);
149 1.15.2.2 bouyer break;
150 1.15.2.2 bouyer }
151 1.15.2.2 bouyer
152 1.15.2.2 bouyer ccb->ccb_flags = 0;
153 1.15.2.2 bouyer ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
154 1.15.2.2 bouyer SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
155 1.15.2.2 bouyer }
156 1.15.2.2 bouyer
157 1.15.2.2 bouyer /* Start firmware background tasks, if needed. */
158 1.15.2.2 bouyer if (startfw) {
159 1.15.2.2 bouyer if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
160 1.15.2.2 bouyer 0, 0, CAC_CCB_DATA_IN, NULL)) {
161 1.15.2.2 bouyer printf("%s: CAC_CMD_START_FIRMWARE failed\n",
162 1.15.2.2 bouyer sc->sc_dv.dv_xname);
163 1.15.2.2 bouyer return (-1);
164 1.15.2.2 bouyer }
165 1.15.2.2 bouyer }
166 1.15.2.2 bouyer
167 1.15.2.2 bouyer if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
168 1.15.2.2 bouyer CAC_CCB_DATA_IN, NULL)) {
169 1.15.2.2 bouyer printf("%s: CAC_CMD_GET_CTRL_INFO failed\n",
170 1.15.2.2 bouyer sc->sc_dv.dv_xname);
171 1.15.2.2 bouyer return (-1);
172 1.15.2.2 bouyer }
173 1.15.2.2 bouyer
174 1.15.2.2 bouyer for (i = 0; i < cinfo.num_drvs; i++) {
175 1.15.2.2 bouyer caca.caca_unit = i;
176 1.15.2.2 bouyer config_found_sm(&sc->sc_dv, &caca, cac_print, cac_submatch);
177 1.15.2.2 bouyer }
178 1.15.2.2 bouyer
179 1.15.2.2 bouyer /* Set our `shutdownhook' before we start any device activity. */
180 1.15.2.2 bouyer if (cac_sdh == NULL)
181 1.15.2.2 bouyer cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
182 1.15.2.2 bouyer
183 1.15.2.2 bouyer (*sc->sc_cl->cl_intr_enable)(sc, CAC_INTR_ENABLE);
184 1.15.2.2 bouyer return (0);
185 1.15.2.2 bouyer }
186 1.15.2.2 bouyer
187 1.15.2.2 bouyer /*
188 1.15.2.2 bouyer * Shut down all `cac' controllers.
189 1.15.2.2 bouyer */
190 1.15.2.2 bouyer static void
191 1.15.2.2 bouyer cac_shutdown(void *cookie)
192 1.15.2.2 bouyer {
193 1.15.2.2 bouyer extern struct cfdriver cac_cd;
194 1.15.2.2 bouyer struct cac_softc *sc;
195 1.15.2.2 bouyer u_int8_t buf[512];
196 1.15.2.2 bouyer int i;
197 1.15.2.2 bouyer
198 1.15.2.2 bouyer for (i = 0; i < cac_cd.cd_ndevs; i++) {
199 1.15.2.2 bouyer if ((sc = device_lookup(&cac_cd, i)) == NULL)
200 1.15.2.2 bouyer continue;
201 1.15.2.2 bouyer memset(buf, 0, sizeof(buf));
202 1.15.2.2 bouyer buf[0] = 1;
203 1.15.2.2 bouyer cac_cmd(sc, CAC_CMD_FLUSH_CACHE, buf, sizeof(buf), 0, 0,
204 1.15.2.2 bouyer CAC_CCB_DATA_OUT, NULL);
205 1.15.2.2 bouyer }
206 1.15.2.2 bouyer }
207 1.15.2.2 bouyer
208 1.15.2.2 bouyer /*
209 1.15.2.2 bouyer * Print autoconfiguration message for a sub-device.
210 1.15.2.2 bouyer */
211 1.15.2.2 bouyer static int
212 1.15.2.2 bouyer cac_print(void *aux, const char *pnp)
213 1.15.2.2 bouyer {
214 1.15.2.2 bouyer struct cac_attach_args *caca;
215 1.15.2.2 bouyer
216 1.15.2.2 bouyer caca = (struct cac_attach_args *)aux;
217 1.15.2.2 bouyer
218 1.15.2.2 bouyer if (pnp != NULL)
219 1.15.2.2 bouyer printf("block device at %s", pnp);
220 1.15.2.2 bouyer printf(" unit %d", caca->caca_unit);
221 1.15.2.2 bouyer return (UNCONF);
222 1.15.2.2 bouyer }
223 1.15.2.2 bouyer
224 1.15.2.2 bouyer /*
225 1.15.2.2 bouyer * Match a sub-device.
226 1.15.2.2 bouyer */
227 1.15.2.2 bouyer static int
228 1.15.2.2 bouyer cac_submatch(struct device *parent, struct cfdata *cf, void *aux)
229 1.15.2.2 bouyer {
230 1.15.2.2 bouyer struct cac_attach_args *caca;
231 1.15.2.2 bouyer
232 1.15.2.2 bouyer caca = (struct cac_attach_args *)aux;
233 1.15.2.2 bouyer
234 1.15.2.2 bouyer if (cf->cacacf_unit != CACCF_UNIT_DEFAULT &&
235 1.15.2.2 bouyer cf->cacacf_unit != caca->caca_unit)
236 1.15.2.2 bouyer return (0);
237 1.15.2.2 bouyer
238 1.15.2.2 bouyer return (cf->cf_attach->ca_match(parent, cf, aux));
239 1.15.2.2 bouyer }
240 1.15.2.2 bouyer
241 1.15.2.2 bouyer /*
242 1.15.2.2 bouyer * Handle an interrupt from the controller: process finished CCBs and
243 1.15.2.2 bouyer * dequeue any waiting CCBs.
244 1.15.2.2 bouyer */
245 1.15.2.2 bouyer int
246 1.15.2.2 bouyer cac_intr(void *cookie)
247 1.15.2.2 bouyer {
248 1.15.2.2 bouyer struct cac_softc *sc;
249 1.15.2.2 bouyer struct cac_ccb *ccb;
250 1.15.2.2 bouyer
251 1.15.2.2 bouyer sc = (struct cac_softc *)cookie;
252 1.15.2.2 bouyer
253 1.15.2.2 bouyer if (!(*sc->sc_cl->cl_intr_pending)(sc)) {
254 1.15.2.2 bouyer #ifdef DEBUG
255 1.15.2.2 bouyer printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
256 1.15.2.2 bouyer #endif
257 1.15.2.2 bouyer return (0);
258 1.15.2.2 bouyer }
259 1.15.2.2 bouyer
260 1.15.2.2 bouyer while ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL) {
261 1.15.2.2 bouyer cac_ccb_done(sc, ccb);
262 1.15.2.2 bouyer cac_ccb_start(sc, NULL);
263 1.15.2.2 bouyer }
264 1.15.2.2 bouyer
265 1.15.2.2 bouyer return (1);
266 1.15.2.2 bouyer }
267 1.15.2.2 bouyer
268 1.15.2.2 bouyer /*
269 1.15.2.2 bouyer * Execute a [polled] command.
270 1.15.2.2 bouyer */
271 1.15.2.2 bouyer int
272 1.15.2.2 bouyer cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
273 1.15.2.2 bouyer int drive, int blkno, int flags, struct cac_context *context)
274 1.15.2.2 bouyer {
275 1.15.2.2 bouyer struct cac_ccb *ccb;
276 1.15.2.2 bouyer struct cac_sgb *sgb;
277 1.15.2.2 bouyer int s, i, rv, size, nsegs;
278 1.15.2.2 bouyer
279 1.15.2.2 bouyer size = 0;
280 1.15.2.2 bouyer
281 1.15.2.2 bouyer if ((ccb = cac_ccb_alloc(sc, 0)) == NULL) {
282 1.15.2.2 bouyer printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
283 1.15.2.2 bouyer return (ENOMEM);
284 1.15.2.2 bouyer }
285 1.15.2.2 bouyer
286 1.15.2.2 bouyer if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
287 1.15.2.2 bouyer bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
288 1.15.2.2 bouyer (void *)data, datasize, NULL, BUS_DMA_NOWAIT);
289 1.15.2.2 bouyer
290 1.15.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
291 1.15.2.2 bouyer (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
292 1.15.2.2 bouyer BUS_DMASYNC_PREWRITE);
293 1.15.2.2 bouyer
294 1.15.2.2 bouyer sgb = ccb->ccb_seg;
295 1.15.2.2 bouyer nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
296 1.15.2.2 bouyer
297 1.15.2.2 bouyer for (i = 0; i < nsegs; i++, sgb++) {
298 1.15.2.2 bouyer size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
299 1.15.2.2 bouyer sgb->length =
300 1.15.2.2 bouyer htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
301 1.15.2.2 bouyer sgb->addr =
302 1.15.2.2 bouyer htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
303 1.15.2.2 bouyer }
304 1.15.2.2 bouyer } else {
305 1.15.2.2 bouyer size = datasize;
306 1.15.2.2 bouyer nsegs = 0;
307 1.15.2.2 bouyer }
308 1.15.2.2 bouyer
309 1.15.2.2 bouyer ccb->ccb_hdr.drive = drive;
310 1.15.2.2 bouyer ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
311 1.15.2.2 bouyer sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
312 1.15.2.2 bouyer
313 1.15.2.2 bouyer ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
314 1.15.2.2 bouyer ccb->ccb_req.command = command;
315 1.15.2.2 bouyer ccb->ccb_req.sgcount = nsegs;
316 1.15.2.2 bouyer ccb->ccb_req.blkno = htole32(blkno);
317 1.15.2.2 bouyer
318 1.15.2.2 bouyer ccb->ccb_flags = flags;
319 1.15.2.2 bouyer ccb->ccb_datasize = size;
320 1.15.2.2 bouyer
321 1.15.2.2 bouyer if (context == NULL) {
322 1.15.2.2 bouyer memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
323 1.15.2.2 bouyer s = splbio();
324 1.15.2.2 bouyer
325 1.15.2.2 bouyer /* Synchronous commands musn't wait. */
326 1.15.2.2 bouyer if ((*sc->sc_cl->cl_fifo_full)(sc)) {
327 1.15.2.2 bouyer cac_ccb_free(sc, ccb);
328 1.15.2.2 bouyer rv = -1;
329 1.15.2.2 bouyer } else {
330 1.15.2.2 bouyer #ifdef DIAGNOSTIC
331 1.15.2.2 bouyer ccb->ccb_flags |= CAC_CCB_ACTIVE;
332 1.15.2.2 bouyer #endif
333 1.15.2.2 bouyer (*sc->sc_cl->cl_submit)(sc, ccb);
334 1.15.2.2 bouyer rv = cac_ccb_poll(sc, ccb, 2000);
335 1.15.2.2 bouyer cac_ccb_free(sc, ccb);
336 1.15.2.2 bouyer }
337 1.15.2.2 bouyer } else {
338 1.15.2.2 bouyer memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
339 1.15.2.2 bouyer s = splbio();
340 1.15.2.2 bouyer rv = cac_ccb_start(sc, ccb);
341 1.15.2.2 bouyer }
342 1.15.2.2 bouyer
343 1.15.2.2 bouyer splx(s);
344 1.15.2.2 bouyer return (rv);
345 1.15.2.2 bouyer }
346 1.15.2.2 bouyer
347 1.15.2.2 bouyer /*
348 1.15.2.2 bouyer * Wait for the specified CCB to complete. Must be called at splbio.
349 1.15.2.2 bouyer */
350 1.15.2.2 bouyer static int
351 1.15.2.2 bouyer cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
352 1.15.2.2 bouyer {
353 1.15.2.2 bouyer struct cac_ccb *ccb;
354 1.15.2.2 bouyer
355 1.15.2.2 bouyer timo *= 10;
356 1.15.2.2 bouyer
357 1.15.2.2 bouyer do {
358 1.15.2.2 bouyer for (; timo != 0; timo--) {
359 1.15.2.2 bouyer if ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL)
360 1.15.2.2 bouyer break;
361 1.15.2.2 bouyer DELAY(100);
362 1.15.2.2 bouyer }
363 1.15.2.2 bouyer
364 1.15.2.2 bouyer if (timo == 0) {
365 1.15.2.2 bouyer printf("%s: timeout", sc->sc_dv.dv_xname);
366 1.15.2.2 bouyer return (EBUSY);
367 1.15.2.2 bouyer }
368 1.15.2.2 bouyer cac_ccb_done(sc, ccb);
369 1.15.2.2 bouyer } while (ccb != wantccb);
370 1.15.2.2 bouyer
371 1.15.2.2 bouyer return (0);
372 1.15.2.2 bouyer }
373 1.15.2.2 bouyer
374 1.15.2.2 bouyer /*
375 1.15.2.2 bouyer * Enqueue the specifed command (if any) and attempt to start all enqueued
376 1.15.2.2 bouyer * commands. Must be called at splbio.
377 1.15.2.2 bouyer */
378 1.15.2.2 bouyer static int
379 1.15.2.2 bouyer cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
380 1.15.2.2 bouyer {
381 1.15.2.2 bouyer
382 1.15.2.2 bouyer if (ccb != NULL)
383 1.15.2.2 bouyer SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
384 1.15.2.2 bouyer
385 1.15.2.2 bouyer while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
386 1.15.2.2 bouyer if ((*sc->sc_cl->cl_fifo_full)(sc))
387 1.15.2.2 bouyer return (EBUSY);
388 1.15.2.2 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain);
389 1.15.2.2 bouyer #ifdef DIAGNOSTIC
390 1.15.2.2 bouyer ccb->ccb_flags |= CAC_CCB_ACTIVE;
391 1.15.2.2 bouyer #endif
392 1.15.2.2 bouyer (*sc->sc_cl->cl_submit)(sc, ccb);
393 1.15.2.2 bouyer }
394 1.15.2.2 bouyer
395 1.15.2.2 bouyer return (0);
396 1.15.2.2 bouyer }
397 1.15.2.2 bouyer
398 1.15.2.2 bouyer /*
399 1.15.2.2 bouyer * Process a finished CCB.
400 1.15.2.2 bouyer */
401 1.15.2.2 bouyer static void
402 1.15.2.2 bouyer cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
403 1.15.2.2 bouyer {
404 1.15.2.2 bouyer const char *errdvn;
405 1.15.2.2 bouyer int error;
406 1.15.2.2 bouyer
407 1.15.2.2 bouyer error = 0;
408 1.15.2.2 bouyer
409 1.15.2.2 bouyer #ifdef DIAGNOSTIC
410 1.15.2.2 bouyer if ((ccb->ccb_flags & CAC_CCB_ACTIVE) == 0)
411 1.15.2.2 bouyer panic("cac_ccb_done: CCB not active");
412 1.15.2.2 bouyer ccb->ccb_flags &= ~CAC_CCB_ACTIVE;
413 1.15.2.2 bouyer #endif
414 1.15.2.2 bouyer
415 1.15.2.2 bouyer if ((ccb->ccb_flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
416 1.15.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
417 1.15.2.2 bouyer ccb->ccb_datasize, ccb->ccb_flags & CAC_CCB_DATA_IN ?
418 1.15.2.2 bouyer BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
419 1.15.2.2 bouyer bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
420 1.15.2.2 bouyer }
421 1.15.2.2 bouyer
422 1.15.2.2 bouyer if (ccb->ccb_context.cc_dv != NULL)
423 1.15.2.2 bouyer errdvn = ccb->ccb_context.cc_dv->dv_xname;
424 1.15.2.2 bouyer else
425 1.15.2.2 bouyer errdvn = sc->sc_dv.dv_xname;
426 1.15.2.2 bouyer
427 1.15.2.2 bouyer if ((ccb->ccb_req.error & CAC_RET_SOFT_ERROR) != 0)
428 1.15.2.2 bouyer printf("%s: soft error; corrected\n", errdvn);
429 1.15.2.2 bouyer if ((ccb->ccb_req.error & CAC_RET_HARD_ERROR) != 0) {
430 1.15.2.2 bouyer error = 1;
431 1.15.2.2 bouyer printf("%s: hard error\n", errdvn);
432 1.15.2.2 bouyer }
433 1.15.2.2 bouyer if ((ccb->ccb_req.error & CAC_RET_CMD_REJECTED) != 0) {
434 1.15.2.2 bouyer error = 1;
435 1.15.2.2 bouyer printf("%s: invalid request\n", errdvn);
436 1.15.2.2 bouyer }
437 1.15.2.2 bouyer
438 1.15.2.2 bouyer if (ccb->ccb_context.cc_handler != NULL) {
439 1.15.2.2 bouyer (*ccb->ccb_context.cc_handler)(ccb, error);
440 1.15.2.2 bouyer cac_ccb_free(sc, ccb);
441 1.15.2.2 bouyer }
442 1.15.2.2 bouyer }
443 1.15.2.2 bouyer
444 1.15.2.2 bouyer /*
445 1.15.2.2 bouyer * Allocate a CCB.
446 1.15.2.2 bouyer */
447 1.15.2.2 bouyer struct cac_ccb *
448 1.15.2.2 bouyer cac_ccb_alloc(struct cac_softc *sc, int nosleep)
449 1.15.2.2 bouyer {
450 1.15.2.2 bouyer struct cac_ccb *ccb;
451 1.15.2.2 bouyer int s;
452 1.15.2.2 bouyer
453 1.15.2.2 bouyer s = splbio();
454 1.15.2.2 bouyer
455 1.15.2.2 bouyer for (;;) {
456 1.15.2.2 bouyer if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
457 1.15.2.2 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
458 1.15.2.2 bouyer break;
459 1.15.2.2 bouyer }
460 1.15.2.2 bouyer if (nosleep) {
461 1.15.2.2 bouyer ccb = NULL;
462 1.15.2.2 bouyer break;
463 1.15.2.2 bouyer }
464 1.15.2.2 bouyer tsleep(&sc->sc_ccb_free, PRIBIO, "cacccb", 0);
465 1.15.2.2 bouyer }
466 1.15.2.2 bouyer
467 1.15.2.2 bouyer splx(s);
468 1.15.2.2 bouyer return (ccb);
469 1.15.2.2 bouyer }
470 1.15.2.2 bouyer
471 1.15.2.2 bouyer /*
472 1.15.2.2 bouyer * Put a CCB onto the freelist.
473 1.15.2.2 bouyer */
474 1.15.2.2 bouyer void
475 1.15.2.2 bouyer cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
476 1.15.2.2 bouyer {
477 1.15.2.2 bouyer int s;
478 1.15.2.2 bouyer
479 1.15.2.2 bouyer ccb->ccb_flags = 0;
480 1.15.2.2 bouyer s = splbio();
481 1.15.2.2 bouyer SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
482 1.15.2.2 bouyer if (SIMPLEQ_NEXT(ccb, ccb_chain) == NULL)
483 1.15.2.2 bouyer wakeup_one(&sc->sc_ccb_free);
484 1.15.2.2 bouyer splx(s);
485 1.15.2.2 bouyer }
486 1.15.2.2 bouyer
487 1.15.2.2 bouyer /*
488 1.15.2.2 bouyer * Board specific linkage shared between multiple bus types.
489 1.15.2.2 bouyer */
490 1.15.2.2 bouyer
491 1.15.2.2 bouyer static int
492 1.15.2.2 bouyer cac_l0_fifo_full(struct cac_softc *sc)
493 1.15.2.2 bouyer {
494 1.15.2.2 bouyer
495 1.15.2.2 bouyer return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
496 1.15.2.2 bouyer }
497 1.15.2.2 bouyer
498 1.15.2.2 bouyer static void
499 1.15.2.2 bouyer cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
500 1.15.2.2 bouyer {
501 1.15.2.2 bouyer
502 1.15.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
503 1.15.2.2 bouyer sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
504 1.15.2.2 bouyer cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
505 1.15.2.2 bouyer }
506 1.15.2.2 bouyer
507 1.15.2.2 bouyer static struct cac_ccb *
508 1.15.2.2 bouyer cac_l0_completed(struct cac_softc *sc)
509 1.15.2.2 bouyer {
510 1.15.2.2 bouyer struct cac_ccb *ccb;
511 1.15.2.2 bouyer paddr_t off;
512 1.15.2.2 bouyer
513 1.15.2.2 bouyer off = (cac_inl(sc, CAC_REG_DONE_FIFO) & ~3) - sc->sc_ccbs_paddr;
514 1.15.2.2 bouyer ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
515 1.15.2.2 bouyer
516 1.15.2.2 bouyer bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
517 1.15.2.2 bouyer BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
518 1.15.2.2 bouyer
519 1.15.2.2 bouyer return (ccb);
520 1.15.2.2 bouyer }
521 1.15.2.2 bouyer
522 1.15.2.2 bouyer static int
523 1.15.2.2 bouyer cac_l0_intr_pending(struct cac_softc *sc)
524 1.15.2.2 bouyer {
525 1.15.2.2 bouyer
526 1.15.2.2 bouyer return (cac_inl(sc, CAC_REG_INTR_PENDING));
527 1.15.2.2 bouyer }
528 1.15.2.2 bouyer
529 1.15.2.2 bouyer static void
530 1.15.2.2 bouyer cac_l0_intr_enable(struct cac_softc *sc, int state)
531 1.15.2.2 bouyer {
532 1.15.2.2 bouyer
533 1.15.2.2 bouyer cac_outl(sc, CAC_REG_INTR_MASK,
534 1.15.2.2 bouyer state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
535 1.15.2.2 bouyer }
536