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