xref: /netbsd-src/sys/dev/ic/cac.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: cac.c,v 1.19 2001/11/13 13:14:35 lukem 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.19 2001/11/13 13:14:35 lukem 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 static struct	cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
66 static void	cac_ccb_done(struct cac_softc *, struct cac_ccb *);
67 static void	cac_ccb_free(struct cac_softc *, struct cac_ccb *);
68 static int	cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
69 static int	cac_ccb_start(struct cac_softc *, struct cac_ccb *);
70 static int	cac_print(void *, const char *);
71 static void	cac_shutdown(void *);
72 static int	cac_submatch(struct device *, struct cfdata *, void *);
73 
74 static struct	cac_ccb *cac_l0_completed(struct cac_softc *);
75 static int	cac_l0_fifo_full(struct cac_softc *);
76 static void	cac_l0_intr_enable(struct cac_softc *, int);
77 static int	cac_l0_intr_pending(struct cac_softc *);
78 static void	cac_l0_submit(struct cac_softc *, struct cac_ccb *);
79 
80 static void	*cac_sdh;	/* shutdown hook */
81 
82 struct cac_linkage cac_l0 = {
83 	cac_l0_completed,
84 	cac_l0_fifo_full,
85 	cac_l0_intr_enable,
86 	cac_l0_intr_pending,
87 	cac_l0_submit
88 };
89 
90 /*
91  * Initialise our interface to the controller.
92  */
93 int
94 cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
95 {
96 	struct cac_controller_info cinfo;
97 	struct cac_attach_args caca;
98 	int error, rseg, size, i;
99 	bus_dma_segment_t seg;
100 	struct cac_ccb *ccb;
101 
102 	if (intrstr != NULL)
103 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
104 		    intrstr);
105 
106 	SIMPLEQ_INIT(&sc->sc_ccb_free);
107 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
108 
109         size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
110 
111 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
112 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
113 		printf("%s: unable to allocate CCBs, error = %d\n",
114 		    sc->sc_dv.dv_xname, error);
115 		return (-1);
116 	}
117 
118 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
119 	    (caddr_t *)&sc->sc_ccbs,
120 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
121 		printf("%s: unable to map CCBs, error = %d\n",
122 		    sc->sc_dv.dv_xname, error);
123 		return (-1);
124 	}
125 
126 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
127 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
128 		printf("%s: unable to create CCB DMA map, error = %d\n",
129 		    sc->sc_dv.dv_xname, error);
130 		return (-1);
131 	}
132 
133 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
134 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
135 		printf("%s: unable to load CCB DMA map, error = %d\n",
136 		    sc->sc_dv.dv_xname, error);
137 		return (-1);
138 	}
139 
140 	sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
141 	memset(sc->sc_ccbs, 0, size);
142 	ccb = (struct cac_ccb *)sc->sc_ccbs;
143 
144 	for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
145 		/* Create the DMA map for this CCB's data */
146 		error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
147 		    CAC_SG_SIZE, CAC_MAX_XFER, 0,
148 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
149 		    &ccb->ccb_dmamap_xfer);
150 
151 		if (error) {
152 			printf("%s: can't create ccb dmamap (%d)\n",
153 			    sc->sc_dv.dv_xname, error);
154 			break;
155 		}
156 
157 		ccb->ccb_flags = 0;
158 		ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
159 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
160 	}
161 
162 	/* Start firmware background tasks, if needed. */
163 	if (startfw) {
164 		if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
165 		    0, 0, CAC_CCB_DATA_IN, NULL)) {
166 			printf("%s: CAC_CMD_START_FIRMWARE failed\n",
167 			    sc->sc_dv.dv_xname);
168 			return (-1);
169 		}
170 	}
171 
172 	if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
173 	    CAC_CCB_DATA_IN, NULL)) {
174 		printf("%s: CAC_CMD_GET_CTRL_INFO failed\n",
175 		    sc->sc_dv.dv_xname);
176 		return (-1);
177 	}
178 
179 	sc->sc_nunits = cinfo.num_drvs;
180 	for (i = 0; i < cinfo.num_drvs; i++) {
181 		caca.caca_unit = i;
182 		config_found_sm(&sc->sc_dv, &caca, cac_print, cac_submatch);
183 	}
184 
185 	/* Set our `shutdownhook' before we start any device activity. */
186 	if (cac_sdh == NULL)
187 		cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
188 
189 	(*sc->sc_cl->cl_intr_enable)(sc, CAC_INTR_ENABLE);
190 	return (0);
191 }
192 
193 /*
194  * Shut down all `cac' controllers.
195  */
196 static void
197 cac_shutdown(void *cookie)
198 {
199 	extern struct cfdriver cac_cd;
200 	struct cac_softc *sc;
201 	u_int8_t buf[512];
202 	int i;
203 
204 	for (i = 0; i < cac_cd.cd_ndevs; i++) {
205 		if ((sc = device_lookup(&cac_cd, i)) == NULL)
206 			continue;
207 		memset(buf, 0, sizeof(buf));
208 		buf[0] = 1;
209 		cac_cmd(sc, CAC_CMD_FLUSH_CACHE, buf, sizeof(buf), 0, 0,
210 		    CAC_CCB_DATA_OUT, NULL);
211 	}
212 }
213 
214 /*
215  * Print autoconfiguration message for a sub-device.
216  */
217 static int
218 cac_print(void *aux, const char *pnp)
219 {
220 	struct cac_attach_args *caca;
221 
222 	caca = (struct cac_attach_args *)aux;
223 
224 	if (pnp != NULL)
225 		printf("block device at %s", pnp);
226 	printf(" unit %d", caca->caca_unit);
227 	return (UNCONF);
228 }
229 
230 /*
231  * Match a sub-device.
232  */
233 static int
234 cac_submatch(struct device *parent, struct cfdata *cf, void *aux)
235 {
236 	struct cac_attach_args *caca;
237 
238 	caca = (struct cac_attach_args *)aux;
239 
240 	if (cf->cacacf_unit != CACCF_UNIT_DEFAULT &&
241 	    cf->cacacf_unit != caca->caca_unit)
242 		return (0);
243 
244 	return (cf->cf_attach->ca_match(parent, cf, aux));
245 }
246 
247 /*
248  * Handle an interrupt from the controller: process finished CCBs and
249  * dequeue any waiting CCBs.
250  */
251 int
252 cac_intr(void *cookie)
253 {
254 	struct cac_softc *sc;
255 	struct cac_ccb *ccb;
256 
257 	sc = (struct cac_softc *)cookie;
258 
259 	if (!(*sc->sc_cl->cl_intr_pending)(sc)) {
260 #ifdef DEBUG
261 		printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
262 #endif
263 		return (0);
264 	}
265 
266 	while ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL) {
267 		cac_ccb_done(sc, ccb);
268 		cac_ccb_start(sc, NULL);
269 	}
270 
271 	return (1);
272 }
273 
274 /*
275  * Execute a [polled] command.
276  */
277 int
278 cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
279 	int drive, int blkno, int flags, struct cac_context *context)
280 {
281 	struct cac_ccb *ccb;
282 	struct cac_sgb *sgb;
283 	int s, i, rv, size, nsegs;
284 
285 	size = 0;
286 
287 	if ((ccb = cac_ccb_alloc(sc, 0)) == NULL) {
288 		printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
289 		return (ENOMEM);
290 	}
291 
292 	if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
293 		bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
294 		    (void *)data, datasize, NULL, BUS_DMA_NOWAIT |
295 		    BUS_DMA_STREAMING | ((flags & CAC_CCB_DATA_IN) ?
296 		    BUS_DMA_READ : BUS_DMA_WRITE));
297 
298 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
299 		    (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
300 		    BUS_DMASYNC_PREWRITE);
301 
302 		sgb = ccb->ccb_seg;
303 		nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
304 
305 		for (i = 0; i < nsegs; i++, sgb++) {
306 			size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
307 			sgb->length =
308 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
309 			sgb->addr =
310 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
311 		}
312 	} else {
313 		size = datasize;
314 		nsegs = 0;
315 	}
316 
317 	ccb->ccb_hdr.drive = drive;
318 	ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
319 	    sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
320 
321 	ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
322 	ccb->ccb_req.command = command;
323 	ccb->ccb_req.sgcount = nsegs;
324 	ccb->ccb_req.blkno = htole32(blkno);
325 
326 	ccb->ccb_flags = flags;
327 	ccb->ccb_datasize = size;
328 
329 	if (context == NULL) {
330 		memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
331 		s = splbio();
332 
333 		/* Synchronous commands musn't wait. */
334 		if ((*sc->sc_cl->cl_fifo_full)(sc)) {
335 			cac_ccb_free(sc, ccb);
336 			rv = -1;
337 		} else {
338 #ifdef DIAGNOSTIC
339 			ccb->ccb_flags |= CAC_CCB_ACTIVE;
340 #endif
341 			(*sc->sc_cl->cl_submit)(sc, ccb);
342 			rv = cac_ccb_poll(sc, ccb, 2000);
343 			cac_ccb_free(sc, ccb);
344 		}
345 	} else {
346 		memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
347 		s = splbio();
348 		rv = cac_ccb_start(sc, ccb);
349 	}
350 
351 	splx(s);
352 	return (rv);
353 }
354 
355 /*
356  * Wait for the specified CCB to complete.  Must be called at splbio.
357  */
358 static int
359 cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
360 {
361 	struct cac_ccb *ccb;
362 
363 	timo *= 10;
364 
365 	do {
366 		for (; timo != 0; timo--) {
367 			if ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL)
368 				break;
369 			DELAY(100);
370 		}
371 
372 		if (timo == 0) {
373 			printf("%s: timeout", 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 specifed 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 (EBUSY);
396 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, 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 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, 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 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 	off = (cac_inl(sc, CAC_REG_DONE_FIFO) & ~3) - sc->sc_ccbs_paddr;
520 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
521 
522 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
523 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
524 
525 	return (ccb);
526 }
527 
528 static int
529 cac_l0_intr_pending(struct cac_softc *sc)
530 {
531 
532 	return (cac_inl(sc, CAC_REG_INTR_PENDING));
533 }
534 
535 static void
536 cac_l0_intr_enable(struct cac_softc *sc, int state)
537 {
538 
539 	cac_outl(sc, CAC_REG_INTR_MASK,
540 	    state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
541 }
542