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