cac.c revision 1.12 1 /* $NetBSD: cac.c,v 1.12 2000/09/08 12:16:17 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 void 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 for (i = 0; i < cinfo.num_drvs; i++) {
175 caca.caca_unit = i;
176 config_found_sm(&sc->sc_dv, &caca, cac_print, cac_submatch);
177 }
178
179 /* Set our `shutdownhook' before we start any device activity. */
180 if (cac_sdh == NULL)
181 cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
182
183 (*sc->sc_cl->cl_intr_enable)(sc, CAC_INTR_ENABLE);
184 return (0);
185 }
186
187 /*
188 * Shut down all `cac' controllers.
189 */
190 static void
191 cac_shutdown(void *cookie)
192 {
193 extern struct cfdriver cac_cd;
194 struct cac_softc *sc;
195 u_int8_t buf[512];
196 int i;
197
198 for (i = 0; i < cac_cd.cd_ndevs; i++) {
199 if ((sc = device_lookup(&cac_cd, i)) == NULL)
200 continue;
201 memset(buf, 0, sizeof(buf));
202 buf[0] = 1;
203 cac_cmd(sc, CAC_CMD_FLUSH_CACHE, buf, sizeof(buf), 0, 0,
204 CAC_CCB_DATA_OUT, NULL);
205 }
206 }
207
208 /*
209 * Print autoconfiguration message for a sub-device.
210 */
211 static int
212 cac_print(void *aux, const char *pnp)
213 {
214 struct cac_attach_args *caca;
215
216 caca = (struct cac_attach_args *)aux;
217
218 if (pnp != NULL)
219 printf("block device at %s", pnp);
220 printf(" unit %d", caca->caca_unit);
221 return (UNCONF);
222 }
223
224 /*
225 * Match a sub-device.
226 */
227 static int
228 cac_submatch(struct device *parent, struct cfdata *cf, void *aux)
229 {
230 struct cac_attach_args *caca;
231
232 caca = (struct cac_attach_args *)aux;
233
234 if (cf->cacacf_unit != CACACF_UNIT_UNKNOWN &&
235 cf->cacacf_unit != caca->caca_unit)
236 return (0);
237
238 return (cf->cf_attach->ca_match(parent, cf, aux));
239 }
240
241 /*
242 * Handle an interrupt from the controller: process finished CCBs and
243 * dequeue any waiting CCBs.
244 */
245 int
246 cac_intr(void *cookie)
247 {
248 struct cac_softc *sc;
249 struct cac_ccb *ccb;
250
251 sc = (struct cac_softc *)cookie;
252
253 if (!(*sc->sc_cl->cl_intr_pending)(sc)) {
254 #ifdef DEBUG
255 printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
256 #endif
257 return (0);
258 }
259
260 while ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL) {
261 cac_ccb_done(sc, ccb);
262 cac_ccb_start(sc, NULL);
263 }
264
265 return (1);
266 }
267
268 /*
269 * Execute a [polled] command.
270 */
271 int
272 cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
273 int drive, int blkno, int flags, struct cac_context *context)
274 {
275 struct cac_ccb *ccb;
276 struct cac_sgb *sgb;
277 int s, i, rv, size, nsegs;
278
279 size = 0;
280
281 if ((ccb = cac_ccb_alloc(sc, 0)) == NULL) {
282 printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
283 return (1);
284 }
285
286 if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
287 bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
288 (void *)data, datasize, NULL, BUS_DMA_NOWAIT);
289
290 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
291 (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
292 BUS_DMASYNC_PREWRITE);
293
294 sgb = ccb->ccb_seg;
295 nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
296
297 for (i = 0; i < nsegs; i++, sgb++) {
298 size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
299 sgb->length =
300 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
301 sgb->addr =
302 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
303 }
304 } else {
305 size = datasize;
306 nsegs = 0;
307 }
308
309 ccb->ccb_hdr.drive = drive;
310 ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
311 sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
312
313 ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
314 ccb->ccb_req.command = command;
315 ccb->ccb_req.sgcount = nsegs;
316 ccb->ccb_req.blkno = htole32(blkno);
317
318 ccb->ccb_flags = flags;
319 ccb->ccb_datasize = size;
320
321 if (context == NULL) {
322 memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
323 s = splbio();
324
325 /* Synchronous commands musn't wait. */
326 if ((*sc->sc_cl->cl_fifo_full)(sc)) {
327 cac_ccb_free(sc, ccb);
328 rv = -1;
329 } else {
330 #ifdef DIAGNOSTIC
331 ccb->ccb_flags |= CAC_CCB_ACTIVE;
332 #endif
333 (*sc->sc_cl->cl_submit)(sc, ccb);
334 cac_ccb_poll(sc, ccb, 2000);
335 cac_ccb_free(sc, ccb);
336 rv = 0;
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 void
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 panic("%s: cac_ccb_poll: timeout", sc->sc_dv.dv_xname);
367
368 cac_ccb_done(sc, ccb);
369 } while (ccb != wantccb);
370 }
371
372 /*
373 * Enqueue the specifed command (if any) and attempt to start all enqueued
374 * commands. Must be called at splbio.
375 */
376 static int
377 cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
378 {
379
380 if (ccb != NULL)
381 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
382
383 while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
384 if ((*sc->sc_cl->cl_fifo_full)(sc))
385 return (-1);
386 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain);
387 #ifdef DIAGNOSTIC
388 ccb->ccb_flags |= CAC_CCB_ACTIVE;
389 #endif
390 (*sc->sc_cl->cl_submit)(sc, ccb);
391 }
392
393 return (0);
394 }
395
396 /*
397 * Process a finished CCB.
398 */
399 static void
400 cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
401 {
402 const char *errdvn;
403 int error;
404
405 error = 0;
406
407 #ifdef DIAGNOSTIC
408 if ((ccb->ccb_flags & CAC_CCB_ACTIVE) == 0)
409 panic("cac_ccb_done: CCB not active");
410 ccb->ccb_flags &= ~CAC_CCB_ACTIVE;
411 #endif
412
413 if ((ccb->ccb_flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
414 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
415 ccb->ccb_datasize, ccb->ccb_flags & CAC_CCB_DATA_IN ?
416 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
417 bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
418 }
419
420 if (ccb->ccb_context.cc_dv != NULL)
421 errdvn = ccb->ccb_context.cc_dv->dv_xname;
422 else
423 errdvn = sc->sc_dv.dv_xname;
424
425 if ((ccb->ccb_req.error & CAC_RET_SOFT_ERROR) != 0)
426 printf("%s: soft error; corrected\n", errdvn);
427 if ((ccb->ccb_req.error & CAC_RET_HARD_ERROR) != 0) {
428 error = 1;
429 printf("%s: hard error\n", errdvn);
430 }
431 if ((ccb->ccb_req.error & CAC_RET_CMD_REJECTED) != 0) {
432 error = 1;
433 printf("%s: invalid request\n", errdvn);
434 }
435
436 if (ccb->ccb_context.cc_handler != NULL) {
437 (*ccb->ccb_context.cc_handler)(ccb, error);
438 cac_ccb_free(sc, ccb);
439 }
440 }
441
442 /*
443 * Allocate a CCB.
444 */
445 struct cac_ccb *
446 cac_ccb_alloc(struct cac_softc *sc, int nosleep)
447 {
448 struct cac_ccb *ccb;
449 int s;
450
451 s = splbio();
452
453 for (;;) {
454 if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
455 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
456 break;
457 }
458 if (nosleep) {
459 ccb = NULL;
460 break;
461 }
462 tsleep(&sc->sc_ccb_free, PRIBIO, "cacccb", 0);
463 }
464
465 splx(s);
466 return (ccb);
467 }
468
469 /*
470 * Put a CCB onto the freelist.
471 */
472 void
473 cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
474 {
475 int s;
476
477 s = splbio();
478 ccb->ccb_flags = 0;
479 SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
480 if (SIMPLEQ_NEXT(ccb, ccb_chain) == NULL)
481 wakeup_one(&sc->sc_ccb_free);
482 splx(s);
483 }
484
485 /*
486 * Adjust the size of a transfer.
487 */
488 void
489 cac_minphys(struct buf *bp)
490 {
491
492 if (bp->b_bcount > CAC_MAX_XFER)
493 bp->b_bcount = CAC_MAX_XFER;
494 minphys(bp);
495 }
496
497 /*
498 * Board specific linkage shared between multiple bus types.
499 */
500
501 static int
502 cac_l0_fifo_full(struct cac_softc *sc)
503 {
504
505 return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
506 }
507
508 static void
509 cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
510 {
511
512 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
513 sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
514 cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
515 }
516
517 static struct cac_ccb *
518 cac_l0_completed(struct cac_softc *sc)
519 {
520 struct cac_ccb *ccb;
521 paddr_t off;
522
523 off = (cac_inl(sc, CAC_REG_DONE_FIFO) & ~3) - sc->sc_ccbs_paddr;
524 ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
525
526 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
527 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
528
529 return (ccb);
530 }
531
532 static int
533 cac_l0_intr_pending(struct cac_softc *sc)
534 {
535
536 return (cac_inl(sc, CAC_REG_INTR_PENDING));
537 }
538
539 static void
540 cac_l0_intr_enable(struct cac_softc *sc, int state)
541 {
542
543 cac_outl(sc, CAC_REG_INTR_MASK,
544 state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
545 }
546