cac.c revision 1.6.2.3 1 /* $NetBSD: cac.c,v 1.6.2.3 2001/10/25 18:03:20 he 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/vmparam.h>
55 #include <machine/bswap.h>
56 #include <machine/bus.h>
57
58 #include <dev/ic/cacreg.h>
59 #include <dev/ic/cacvar.h>
60
61 static struct cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
62 static void cac_ccb_done(struct cac_softc *, struct cac_ccb *);
63 static void cac_ccb_free(struct cac_softc *, struct cac_ccb *);
64 static int cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
65 static int cac_ccb_start(struct cac_softc *, struct cac_ccb *);
66 static int cac_print(void *, const char *);
67 static void cac_shutdown(void *);
68 static int cac_submatch(struct device *, struct cfdata *, void *);
69
70 static struct cac_ccb *cac_l0_completed(struct cac_softc *);
71 static int cac_l0_fifo_full(struct cac_softc *);
72 static void cac_l0_intr_enable(struct cac_softc *, int);
73 static int cac_l0_intr_pending(struct cac_softc *);
74 static void cac_l0_submit(struct cac_softc *, struct cac_ccb *);
75
76 static void *cac_sdh; /* shutdown hook */
77
78 struct cac_linkage cac_l0 = {
79 cac_l0_completed,
80 cac_l0_fifo_full,
81 cac_l0_intr_enable,
82 cac_l0_intr_pending,
83 cac_l0_submit
84 };
85
86 /*
87 * Initialise our interface to the controller.
88 */
89 int
90 cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
91 {
92 struct cac_controller_info cinfo;
93 struct cac_attach_args caca;
94 int error, rseg, size, i;
95 bus_dma_segment_t seg;
96 struct cac_ccb *ccb;
97
98 if (intrstr != NULL)
99 printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
100 intrstr);
101
102 SIMPLEQ_INIT(&sc->sc_ccb_free);
103 SIMPLEQ_INIT(&sc->sc_ccb_queue);
104
105 size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
106
107 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1,
108 &rseg, BUS_DMA_NOWAIT)) != 0) {
109 printf("%s: unable to allocate CCBs, error = %d\n",
110 sc->sc_dv.dv_xname, error);
111 return (-1);
112 }
113
114 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
115 (caddr_t *)&sc->sc_ccbs,
116 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
117 printf("%s: unable to map CCBs, error = %d\n",
118 sc->sc_dv.dv_xname, error);
119 return (-1);
120 }
121
122 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
123 BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
124 printf("%s: unable to create CCB DMA map, error = %d\n",
125 sc->sc_dv.dv_xname, error);
126 return (-1);
127 }
128
129 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
130 size, NULL, BUS_DMA_NOWAIT)) != 0) {
131 printf("%s: unable to load CCB DMA map, error = %d\n",
132 sc->sc_dv.dv_xname, error);
133 return (-1);
134 }
135
136 sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
137 memset(sc->sc_ccbs, 0, size);
138 ccb = (struct cac_ccb *)sc->sc_ccbs;
139
140 for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
141 /* Create the DMA map for this CCB's data */
142 error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
143 CAC_SG_SIZE, CAC_MAX_XFER, 0,
144 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
145 &ccb->ccb_dmamap_xfer);
146
147 if (error) {
148 printf("%s: can't create ccb dmamap (%d)\n",
149 sc->sc_dv.dv_xname, error);
150 break;
151 }
152
153 ccb->ccb_flags = 0;
154 ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
155 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
156 }
157
158 /* Start firmware background tasks, if needed. */
159 if (startfw) {
160 if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
161 0, 0, CAC_CCB_DATA_IN, NULL)) {
162 printf("%s: CAC_CMD_START_FIRMWARE failed\n",
163 sc->sc_dv.dv_xname);
164 return (-1);
165 }
166 }
167
168 if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
169 CAC_CCB_DATA_IN, NULL)) {
170 printf("%s: CAC_CMD_GET_CTRL_INFO failed\n",
171 sc->sc_dv.dv_xname);
172 return (-1);
173 }
174
175 sc->sc_nunits = cinfo.num_drvs;
176 for (i = 0; i < cinfo.num_drvs; i++) {
177 caca.caca_unit = i;
178 config_found_sm(&sc->sc_dv, &caca, cac_print, cac_submatch);
179 }
180
181 /* Set our `shutdownhook' before we start any device activity. */
182 if (cac_sdh == NULL)
183 cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
184
185 (*sc->sc_cl->cl_intr_enable)(sc, CAC_INTR_ENABLE);
186 return (0);
187 }
188
189 /*
190 * Shut down all `cac' controllers.
191 */
192 static void
193 cac_shutdown(void *cookie)
194 {
195 extern struct cfdriver cac_cd;
196 struct cac_softc *sc;
197 u_int8_t buf[512];
198 int i;
199
200 for (i = 0; i < cac_cd.cd_ndevs; i++) {
201 if ((sc = cac_cd.cd_devs[i]) == NULL)
202 continue;
203 memset(buf, 0, sizeof(buf));
204 buf[0] = 1;
205 cac_cmd(sc, CAC_CMD_FLUSH_CACHE, buf, sizeof(buf), 0, 0,
206 CAC_CCB_DATA_OUT, NULL);
207 }
208 }
209
210 /*
211 * Print autoconfiguration message for a sub-device.
212 */
213 static int
214 cac_print(void *aux, const char *pnp)
215 {
216 struct cac_attach_args *caca;
217
218 caca = (struct cac_attach_args *)aux;
219
220 if (pnp != NULL)
221 printf("block device at %s", pnp);
222 printf(" unit %d", caca->caca_unit);
223 return (UNCONF);
224 }
225
226 /*
227 * Match a sub-device.
228 */
229 static int
230 cac_submatch(struct device *parent, struct cfdata *cf, void *aux)
231 {
232 struct cac_attach_args *caca;
233
234 caca = (struct cac_attach_args *)aux;
235
236 if (cf->cacacf_unit != CACCF_UNIT_DEFAULT &&
237 cf->cacacf_unit != caca->caca_unit)
238 return (0);
239
240 return (cf->cf_attach->ca_match(parent, cf, aux));
241 }
242
243 /*
244 * Handle an interrupt from the controller: process finished CCBs and
245 * dequeue any waiting CCBs.
246 */
247 int
248 cac_intr(void *cookie)
249 {
250 struct cac_softc *sc;
251 struct cac_ccb *ccb;
252
253 sc = (struct cac_softc *)cookie;
254
255 if (!(*sc->sc_cl->cl_intr_pending)(sc)) {
256 #ifdef DEBUG
257 printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
258 #endif
259 return (0);
260 }
261
262 while ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL) {
263 cac_ccb_done(sc, ccb);
264 cac_ccb_start(sc, NULL);
265 }
266
267 return (1);
268 }
269
270 /*
271 * Execute a [polled] command.
272 */
273 int
274 cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
275 int drive, int blkno, int flags, struct cac_context *context)
276 {
277 struct cac_ccb *ccb;
278 struct cac_sgb *sgb;
279 int s, i, rv, size, nsegs;
280
281 size = 0;
282
283 if ((ccb = cac_ccb_alloc(sc, 0)) == NULL) {
284 printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
285 return (ENOMEM);
286 }
287
288 if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
289 bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
290 (void *)data, datasize, NULL, BUS_DMA_NOWAIT);
291
292 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
293 (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
294 BUS_DMASYNC_PREWRITE);
295
296 sgb = ccb->ccb_seg;
297 nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
298
299 for (i = 0; i < nsegs; i++, sgb++) {
300 size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
301 sgb->length =
302 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
303 sgb->addr =
304 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
305 }
306 } else {
307 size = datasize;
308 nsegs = 0;
309 }
310
311 ccb->ccb_hdr.drive = drive;
312 ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
313 sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
314
315 ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
316 ccb->ccb_req.command = command;
317 ccb->ccb_req.sgcount = nsegs;
318 ccb->ccb_req.blkno = htole32(blkno);
319
320 ccb->ccb_flags = flags;
321 ccb->ccb_datasize = size;
322
323 if (context == NULL) {
324 memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
325 s = splbio();
326
327 /* Synchronous commands musn't wait. */
328 if ((*sc->sc_cl->cl_fifo_full)(sc)) {
329 cac_ccb_free(sc, ccb);
330 rv = -1;
331 } else {
332 #ifdef DIAGNOSTIC
333 ccb->ccb_flags |= CAC_CCB_ACTIVE;
334 #endif
335 (*sc->sc_cl->cl_submit)(sc, ccb);
336 rv = cac_ccb_poll(sc, ccb, 2000);
337 cac_ccb_free(sc, ccb);
338 }
339 } else {
340 memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
341 s = splbio();
342 rv = cac_ccb_start(sc, ccb);
343 }
344
345 splx(s);
346 return (rv);
347 }
348
349 /*
350 * Wait for the specified CCB to complete. Must be called at splbio.
351 */
352 static int
353 cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
354 {
355 struct cac_ccb *ccb;
356
357 timo *= 10;
358
359 do {
360 for (; timo != 0; timo--) {
361 if ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL)
362 break;
363 DELAY(100);
364 }
365
366 if (timo == 0) {
367 printf("%s: timeout", sc->sc_dv.dv_xname);
368 return (EBUSY);
369 }
370 cac_ccb_done(sc, ccb);
371 } while (ccb != wantccb);
372
373 return (0);
374 }
375
376 /*
377 * Enqueue the specifed command (if any) and attempt to start all enqueued
378 * commands. Must be called at splbio.
379 */
380 static int
381 cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
382 {
383
384 if (ccb != NULL)
385 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
386
387 while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
388 if ((*sc->sc_cl->cl_fifo_full)(sc))
389 return (EBUSY);
390 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain);
391 #ifdef DIAGNOSTIC
392 ccb->ccb_flags |= CAC_CCB_ACTIVE;
393 #endif
394 (*sc->sc_cl->cl_submit)(sc, ccb);
395 }
396
397 return (0);
398 }
399
400 /*
401 * Process a finished CCB.
402 */
403 static void
404 cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
405 {
406 struct device *dv;
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 error = ccb->ccb_req.error;
426 if (ccb->ccb_context.cc_handler != NULL) {
427 dv = ccb->ccb_context.cc_dv;
428 context = ccb->ccb_context.cc_context;
429 cac_ccb_free(sc, ccb);
430 (*ccb->ccb_context.cc_handler)(dv, context, error);
431 } else {
432 if ((error & CAC_RET_SOFT_ERROR) != 0)
433 printf("%s: soft error; array may be degraded\n",
434 sc->sc_dv.dv_xname);
435 if ((error & CAC_RET_HARD_ERROR) != 0)
436 printf("%s: hard error\n", sc->sc_dv.dv_xname);
437 if ((error & CAC_RET_CMD_REJECTED) != 0) {
438 error = 1;
439 printf("%s: invalid request\n", sc->sc_dv.dv_xname);
440 }
441 }
442 }
443
444 /*
445 * Allocate a CCB.
446 */
447 struct cac_ccb *
448 cac_ccb_alloc(struct cac_softc *sc, int nosleep)
449 {
450 struct cac_ccb *ccb;
451 int s;
452
453 s = splbio();
454
455 for (;;) {
456 if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
457 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
458 break;
459 }
460 if (nosleep) {
461 ccb = NULL;
462 break;
463 }
464 tsleep(&sc->sc_ccb_free, PRIBIO, "cacccb", 0);
465 }
466
467 splx(s);
468 return (ccb);
469 }
470
471 /*
472 * Put a CCB onto the freelist.
473 */
474 void
475 cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
476 {
477 int s;
478
479 ccb->ccb_flags = 0;
480 s = splbio();
481 SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
482 if (SIMPLEQ_NEXT(ccb, ccb_chain) == NULL)
483 wakeup_one(&sc->sc_ccb_free);
484 splx(s);
485 }
486
487 /*
488 * Board specific linkage shared between multiple bus types.
489 */
490
491 static int
492 cac_l0_fifo_full(struct cac_softc *sc)
493 {
494
495 return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
496 }
497
498 static void
499 cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
500 {
501
502 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
503 sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
504 cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
505 }
506
507 static struct cac_ccb *
508 cac_l0_completed(struct cac_softc *sc)
509 {
510 struct cac_ccb *ccb;
511 paddr_t off;
512
513 off = (cac_inl(sc, CAC_REG_DONE_FIFO) & ~3) - sc->sc_ccbs_paddr;
514 ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
515
516 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
517 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
518
519 return (ccb);
520 }
521
522 static int
523 cac_l0_intr_pending(struct cac_softc *sc)
524 {
525
526 return (cac_inl(sc, CAC_REG_INTR_PENDING));
527 }
528
529 static void
530 cac_l0_intr_enable(struct cac_softc *sc, int state)
531 {
532
533 cac_outl(sc, CAC_REG_INTR_MASK,
534 state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
535 }
536