cac.c revision 1.38 1 /* $NetBSD: cac.c,v 1.38 2006/12/29 14:07:08 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2006 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/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: cac.c,v 1.38 2006/12/29 14:07:08 ad Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/queue.h>
51 #include <sys/proc.h>
52 #include <sys/buf.h>
53 #include <sys/endian.h>
54 #include <sys/malloc.h>
55 #include <sys/pool.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <sys/bswap.h>
60 #include <machine/bus.h>
61
62 #include <dev/ic/cacreg.h>
63 #include <dev/ic/cacvar.h>
64
65 #include "locators.h"
66
67 static struct cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
68 static void cac_ccb_done(struct cac_softc *, struct cac_ccb *);
69 static void cac_ccb_free(struct cac_softc *, struct cac_ccb *);
70 static int cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
71 static int cac_ccb_start(struct cac_softc *, struct cac_ccb *);
72 static int cac_print(void *, const char *);
73 static void cac_shutdown(void *);
74
75 static struct cac_ccb *cac_l0_completed(struct cac_softc *);
76 static int cac_l0_fifo_full(struct cac_softc *);
77 static void cac_l0_intr_enable(struct cac_softc *, int);
78 static int cac_l0_intr_pending(struct cac_softc *);
79 static void cac_l0_submit(struct cac_softc *, struct cac_ccb *);
80
81 static void *cac_sdh; /* shutdown hook */
82
83 const struct cac_linkage cac_l0 = {
84 cac_l0_completed,
85 cac_l0_fifo_full,
86 cac_l0_intr_enable,
87 cac_l0_intr_pending,
88 cac_l0_submit
89 };
90
91 /*
92 * Initialise our interface to the controller.
93 */
94 int
95 cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
96 {
97 struct cac_controller_info cinfo;
98 struct cac_attach_args caca;
99 int error, rseg, size, i;
100 bus_dma_segment_t seg;
101 struct cac_ccb *ccb;
102 int locs[CACCF_NLOCS];
103 char firm[8];
104
105 if (intrstr != NULL)
106 aprint_normal("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
107 intrstr);
108
109 SIMPLEQ_INIT(&sc->sc_ccb_free);
110 SIMPLEQ_INIT(&sc->sc_ccb_queue);
111
112 size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
113
114 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
115 &rseg, BUS_DMA_NOWAIT)) != 0) {
116 aprint_error("%s: unable to allocate CCBs, error = %d\n",
117 sc->sc_dv.dv_xname, error);
118 return (-1);
119 }
120
121 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
122 (caddr_t *)&sc->sc_ccbs,
123 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
124 aprint_error("%s: unable to map CCBs, error = %d\n",
125 sc->sc_dv.dv_xname, error);
126 return (-1);
127 }
128
129 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
130 BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
131 aprint_error("%s: unable to create CCB DMA map, error = %d\n",
132 sc->sc_dv.dv_xname, error);
133 return (-1);
134 }
135
136 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
137 size, NULL, BUS_DMA_NOWAIT)) != 0) {
138 aprint_error("%s: unable to load CCB DMA map, error = %d\n",
139 sc->sc_dv.dv_xname, error);
140 return (-1);
141 }
142
143 sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
144 memset(sc->sc_ccbs, 0, size);
145 ccb = (struct cac_ccb *)sc->sc_ccbs;
146
147 for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
148 /* Create the DMA map for this CCB's data */
149 error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
150 CAC_SG_SIZE, CAC_MAX_XFER, 0,
151 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
152 &ccb->ccb_dmamap_xfer);
153
154 if (error) {
155 aprint_error("%s: can't create ccb dmamap (%d)\n",
156 sc->sc_dv.dv_xname, error);
157 break;
158 }
159
160 ccb->ccb_flags = 0;
161 ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
162 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
163 }
164
165 /* Start firmware background tasks, if needed. */
166 if (startfw) {
167 if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
168 0, 0, CAC_CCB_DATA_IN, NULL)) {
169 aprint_error("%s: CAC_CMD_START_FIRMWARE failed\n",
170 sc->sc_dv.dv_xname);
171 return (-1);
172 }
173 }
174
175 if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
176 CAC_CCB_DATA_IN, NULL)) {
177 aprint_error("%s: CAC_CMD_GET_CTRL_INFO failed\n",
178 sc->sc_dv.dv_xname);
179 return (-1);
180 }
181
182 strlcpy(firm, cinfo.firm_rev, 4+1);
183 printf("%s: %d channels, firmware <%s>\n", sc->sc_dv.dv_xname,
184 cinfo.scsi_chips, firm);
185
186 sc->sc_nunits = cinfo.num_drvs;
187 for (i = 0; i < cinfo.num_drvs; i++) {
188 caca.caca_unit = i;
189
190 locs[CACCF_UNIT] = i;
191
192 config_found_sm_loc(&sc->sc_dv, "cac", locs, &caca,
193 cac_print, config_stdsubmatch);
194 }
195
196 /* Set our `shutdownhook' before we start any device activity. */
197 if (cac_sdh == NULL)
198 cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
199
200 (*sc->sc_cl.cl_intr_enable)(sc, CAC_INTR_ENABLE);
201 return (0);
202 }
203
204 /*
205 * Shut down all `cac' controllers.
206 */
207 static void
208 cac_shutdown(void *cookie)
209 {
210 extern struct cfdriver cac_cd;
211 struct cac_softc *sc;
212 u_int8_t tbuf[512];
213 int i;
214
215 for (i = 0; i < cac_cd.cd_ndevs; i++) {
216 if ((sc = device_lookup(&cac_cd, i)) == NULL)
217 continue;
218 memset(tbuf, 0, sizeof(tbuf));
219 tbuf[0] = 1;
220 cac_cmd(sc, CAC_CMD_FLUSH_CACHE, tbuf, sizeof(tbuf), 0, 0,
221 CAC_CCB_DATA_OUT, NULL);
222 }
223 }
224
225 /*
226 * Print autoconfiguration message for a sub-device.
227 */
228 static int
229 cac_print(void *aux, const char *pnp)
230 {
231 struct cac_attach_args *caca;
232
233 caca = (struct cac_attach_args *)aux;
234
235 if (pnp != NULL)
236 aprint_normal("block device at %s", pnp);
237 aprint_normal(" unit %d", caca->caca_unit);
238 return (UNCONF);
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, 1)) == NULL) {
282 printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
283 return (EAGAIN);
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 BUS_DMA_STREAMING | ((flags & CAC_CCB_DATA_IN) ?
290 BUS_DMA_READ : BUS_DMA_WRITE));
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.priority = 0;
313 ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
314 sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
315
316 ccb->ccb_req.next = 0;
317 ccb->ccb_req.error = 0;
318 ccb->ccb_req.reserved = 0;
319 ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
320 ccb->ccb_req.command = command;
321 ccb->ccb_req.sgcount = nsegs;
322 ccb->ccb_req.blkno = htole32(blkno);
323
324 ccb->ccb_flags = flags;
325 ccb->ccb_datasize = size;
326
327 if (context == NULL) {
328 memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
329 s = splbio();
330
331 /* Synchronous commands musn't wait. */
332 if ((*sc->sc_cl.cl_fifo_full)(sc)) {
333 cac_ccb_free(sc, ccb);
334 rv = EAGAIN;
335 } else {
336 #ifdef DIAGNOSTIC
337 ccb->ccb_flags |= CAC_CCB_ACTIVE;
338 #endif
339 (*sc->sc_cl.cl_submit)(sc, ccb);
340 rv = cac_ccb_poll(sc, ccb, 2000);
341 cac_ccb_free(sc, ccb);
342 }
343 } else {
344 memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
345 s = splbio();
346 (void)cac_ccb_start(sc, ccb);
347 rv = 0;
348 }
349
350 splx(s);
351 return (rv);
352 }
353
354 /*
355 * Wait for the specified CCB to complete. Must be called at splbio.
356 */
357 static int
358 cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
359 {
360 struct cac_ccb *ccb;
361
362 timo *= 1000;
363
364 do {
365 for (; timo != 0; timo--) {
366 ccb = (*sc->sc_cl.cl_completed)(sc);
367 if (ccb != NULL)
368 break;
369 DELAY(1);
370 }
371
372 if (timo == 0) {
373 printf("%s: timeout\n", sc->sc_dv.dv_xname);
374 return (EBUSY);
375 }
376 cac_ccb_done(sc, ccb);
377 } while (ccb != wantccb);
378
379 return (0);
380 }
381
382 /*
383 * Enqueue the specified command (if any) and attempt to start all enqueued
384 * commands. Must be called at splbio.
385 */
386 static int
387 cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
388 {
389
390 if (ccb != NULL)
391 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
392
393 while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
394 if ((*sc->sc_cl.cl_fifo_full)(sc))
395 return (EAGAIN);
396 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb_chain);
397 #ifdef DIAGNOSTIC
398 ccb->ccb_flags |= CAC_CCB_ACTIVE;
399 #endif
400 (*sc->sc_cl.cl_submit)(sc, ccb);
401 }
402
403 return (0);
404 }
405
406 /*
407 * Process a finished CCB.
408 */
409 static void
410 cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
411 {
412 struct device *dv;
413 void *context;
414 int error;
415
416 error = 0;
417
418 #ifdef DIAGNOSTIC
419 if ((ccb->ccb_flags & CAC_CCB_ACTIVE) == 0)
420 panic("cac_ccb_done: CCB not active");
421 ccb->ccb_flags &= ~CAC_CCB_ACTIVE;
422 #endif
423
424 if ((ccb->ccb_flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
425 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
426 ccb->ccb_datasize, ccb->ccb_flags & CAC_CCB_DATA_IN ?
427 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
428 bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
429 }
430
431 error = ccb->ccb_req.error;
432 if (ccb->ccb_context.cc_handler != NULL) {
433 dv = ccb->ccb_context.cc_dv;
434 context = ccb->ccb_context.cc_context;
435 cac_ccb_free(sc, ccb);
436 (*ccb->ccb_context.cc_handler)(dv, context, error);
437 } else {
438 if ((error & CAC_RET_SOFT_ERROR) != 0)
439 printf("%s: soft error; array may be degraded\n",
440 sc->sc_dv.dv_xname);
441 if ((error & CAC_RET_HARD_ERROR) != 0)
442 printf("%s: hard error\n", sc->sc_dv.dv_xname);
443 if ((error & CAC_RET_CMD_REJECTED) != 0) {
444 error = 1;
445 printf("%s: invalid request\n", sc->sc_dv.dv_xname);
446 }
447 }
448 }
449
450 /*
451 * Allocate a CCB.
452 */
453 static struct cac_ccb *
454 cac_ccb_alloc(struct cac_softc *sc, int nosleep)
455 {
456 struct cac_ccb *ccb;
457 int s;
458
459 s = splbio();
460
461 for (;;) {
462 if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
463 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb_chain);
464 break;
465 }
466 if (nosleep) {
467 ccb = NULL;
468 break;
469 }
470 tsleep(&sc->sc_ccb_free, PRIBIO, "cacccb", 0);
471 }
472
473 splx(s);
474 return (ccb);
475 }
476
477 /*
478 * Put a CCB onto the freelist.
479 */
480 static void
481 cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
482 {
483 int s;
484
485 ccb->ccb_flags = 0;
486 s = splbio();
487 SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
488 if (SIMPLEQ_NEXT(ccb, ccb_chain) == NULL)
489 wakeup_one(&sc->sc_ccb_free);
490 splx(s);
491 }
492
493 /*
494 * Board specific linkage shared between multiple bus types.
495 */
496
497 static int
498 cac_l0_fifo_full(struct cac_softc *sc)
499 {
500
501 return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
502 }
503
504 static void
505 cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
506 {
507
508 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
509 sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
510 cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
511 }
512
513 static struct cac_ccb *
514 cac_l0_completed(struct cac_softc *sc)
515 {
516 struct cac_ccb *ccb;
517 paddr_t off;
518
519 if ((off = cac_inl(sc, CAC_REG_DONE_FIFO)) == 0)
520 return (NULL);
521
522 if ((off & 3) != 0)
523 printf("%s: failed command list returned: %lx\n",
524 sc->sc_dv.dv_xname, (long)off);
525
526 off = (off & ~3) - sc->sc_ccbs_paddr;
527 ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
528
529 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
530 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
531
532 if ((off & 3) != 0 && ccb->ccb_req.error == 0)
533 ccb->ccb_req.error = CAC_RET_CMD_REJECTED;
534
535 return (ccb);
536 }
537
538 static int
539 cac_l0_intr_pending(struct cac_softc *sc)
540 {
541
542 return (cac_inl(sc, CAC_REG_INTR_PENDING) & CAC_INTR_ENABLE);
543 }
544
545 static void
546 cac_l0_intr_enable(struct cac_softc *sc, int state)
547 {
548
549 cac_outl(sc, CAC_REG_INTR_MASK,
550 state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
551 }
552