xref: /freebsd/sys/dev/ida/ida.c (revision ceaec73d406831b1251babb61675df0a1aa54a31)
1 /*-
2  * Copyright (c) 1999,2000 Jonathan Lemon
3  * All rights reserved.
4  *
5  # Derived from the original IDA Compaq RAID driver, which is
6  * Copyright (c) 1996, 1997, 1998, 1999
7  *    Mark Dawson and David James. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * Generic driver for Compaq SMART RAID adapters.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/stat.h>
43 
44 #include <sys/bio.h>
45 #include <sys/bus.h>
46 #include <sys/conf.h>
47 #include <sys/endian.h>
48 
49 #include <machine/bus_memio.h>
50 #include <machine/bus_pio.h>
51 #include <machine/bus.h>
52 #include <sys/rman.h>
53 
54 #include <geom/geom_disk.h>
55 
56 #include <dev/ida/idareg.h>
57 #include <dev/ida/idavar.h>
58 #include <dev/ida/idaio.h>
59 
60 /* prototypes */
61 static void ida_alloc_qcb(struct ida_softc *ida);
62 static void ida_construct_qcb(struct ida_softc *ida);
63 static void ida_start(struct ida_softc *ida);
64 static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb);
65 static int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb);
66 static void ida_timeout (void *arg);
67 
68 static d_ioctl_t ida_ioctl;
69 static struct cdevsw ida_cdevsw = {
70 	.d_version =	D_VERSION,
71 	.d_flags =	D_NEEDGIANT,
72 	.d_ioctl =	ida_ioctl,
73 	.d_name =	"ida",
74 };
75 
76 void
77 ida_free(struct ida_softc *ida)
78 {
79 	int i;
80 
81 	callout_stop(&ida->ch);
82 
83 	if (ida->buffer_dmat) {
84 		for (i = 0; i < ida->num_qcbs; i++)
85 			bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap);
86 		bus_dma_tag_destroy(ida->buffer_dmat);
87 	}
88 
89 	if (ida->hwqcb_dmat) {
90 		if (ida->hwqcb_busaddr)
91 			bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap);
92 		if (ida->hwqcbs)
93 			bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs,
94 			    ida->hwqcb_dmamap);
95 		bus_dma_tag_destroy(ida->hwqcb_dmat);
96 	}
97 
98 	if (ida->qcbs != NULL)
99 		free(ida->qcbs, M_DEVBUF);
100 
101 	if (ida->ih != NULL)
102 		bus_teardown_intr(ida->dev, ida->irq, ida->ih);
103 
104 	if (ida->irq != NULL)
105 		bus_release_resource(ida->dev, ida->irq_res_type,
106 		    0, ida->irq);
107 
108 	if (ida->parent_dmat != NULL)
109 		bus_dma_tag_destroy(ida->parent_dmat);
110 
111 	if (ida->regs != NULL)
112 		bus_release_resource(ida->dev, ida->regs_res_type,
113 		    ida->regs_res_id, ida->regs);
114 }
115 
116 /*
117  * record bus address from bus_dmamap_load
118  */
119 static void
120 ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
121 {
122 	bus_addr_t *baddr;
123 
124 	baddr = (bus_addr_t *)arg;
125 	*baddr = segs->ds_addr;
126 }
127 
128 static __inline struct ida_qcb *
129 ida_get_qcb(struct ida_softc *ida)
130 {
131 	struct ida_qcb *qcb;
132 
133 	if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) {
134 		SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
135 	} else {
136 		ida_alloc_qcb(ida);
137 		if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL)
138 			SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
139 	}
140 	return (qcb);
141 }
142 
143 static __inline bus_addr_t
144 idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb)
145 {
146 	return (ida->hwqcb_busaddr +
147 	    ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs));
148 }
149 
150 static __inline struct ida_qcb *
151 idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr)
152 {
153 	struct ida_hardware_qcb *hwqcb;
154 
155 	hwqcb = (struct ida_hardware_qcb *)
156 	    ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr));
157 	return (hwqcb->qcb);
158 }
159 
160 /*
161  * XXX
162  * since we allocate all QCB space up front during initialization, then
163  * why bother with this routine?
164  */
165 static void
166 ida_alloc_qcb(struct ida_softc *ida)
167 {
168 	struct ida_qcb *qcb;
169 	int error;
170 
171 	if (ida->num_qcbs >= IDA_QCB_MAX)
172 		return;
173 
174 	qcb = &ida->qcbs[ida->num_qcbs];
175 
176 	error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap);
177 	if (error != 0)
178 		return;
179 
180 	qcb->flags = QCB_FREE;
181 	qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs];
182 	qcb->hwqcb->qcb = qcb;
183 	qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb);
184 	SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
185 	ida->num_qcbs++;
186 }
187 
188 int
189 ida_init(struct ida_softc *ida)
190 {
191 	int error;
192 
193 	ida->unit = device_get_unit(ida->dev);
194 	ida->tag = rman_get_bustag(ida->regs);
195 	ida->bsh = rman_get_bushandle(ida->regs);
196 
197 	SLIST_INIT(&ida->free_qcbs);
198 	STAILQ_INIT(&ida->qcb_queue);
199 	bioq_init(&ida->bio_queue);
200 
201 	ida->qcbs = (struct ida_qcb *)
202 	    malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF,
203 		M_NOWAIT | M_ZERO);
204 	if (ida->qcbs == NULL)
205 		return (ENOMEM);
206 
207 	/*
208 	 * Create our DMA tags
209 	 */
210 
211 	/* DMA tag for our hardware QCB structures */
212 	error = bus_dma_tag_create(
213 		/* parent	*/ ida->parent_dmat,
214 		/* alignment	*/ 1,
215 		/* boundary	*/ 0,
216 		/* lowaddr	*/ BUS_SPACE_MAXADDR,
217 		/* highaddr	*/ BUS_SPACE_MAXADDR,
218 		/* filter	*/ NULL,
219 		/* filterarg	*/ NULL,
220 		/* maxsize	*/ IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
221 		/* nsegments	*/ 1,
222 		/* maxsegsz	*/ BUS_SPACE_MAXSIZE_32BIT,
223 		/* flags	*/ 0,
224 		/* lockfunc	*/ busdma_lock_mutex,
225 		/* lockarg	*/ &Giant,
226 		&ida->hwqcb_dmat);
227 	if (error)
228 		return (ENOMEM);
229 
230 	/* DMA tag for mapping buffers into device space */
231 	error = bus_dma_tag_create(
232 		/* parent 	*/ ida->parent_dmat,
233 		/* alignment	*/ 1,
234 		/* boundary	*/ 0,
235 		/* lowaddr	*/ BUS_SPACE_MAXADDR,
236 		/* highaddr	*/ BUS_SPACE_MAXADDR,
237 		/* filter	*/ NULL,
238 		/* filterarg	*/ NULL,
239 		/* maxsize	*/ MAXBSIZE,
240 		/* nsegments	*/ IDA_NSEG,
241 		/* maxsegsz	*/ BUS_SPACE_MAXSIZE_32BIT,
242 		/* flags	*/ 0,
243 		/* lockfunc	*/ busdma_lock_mutex,
244 		/* lockarg	*/ &Giant,
245 		&ida->buffer_dmat);
246 	if (error)
247 		return (ENOMEM);
248 
249 	/* Allocation of hardware QCBs */
250 	/* XXX allocation is rounded to hardware page size */
251 	error = bus_dmamem_alloc(ida->hwqcb_dmat,
252 	    (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap);
253 	if (error)
254 		return (ENOMEM);
255 
256 	/* And permanently map them in */
257 	bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap,
258 	    ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
259 	    ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0);
260 
261 	bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb));
262 
263 	ida_alloc_qcb(ida);		/* allocate an initial qcb */
264 
265 	callout_init(&ida->ch, CALLOUT_MPSAFE);
266 
267 	return (0);
268 }
269 
270 void
271 ida_attach(struct ida_softc *ida)
272 {
273 	struct ida_controller_info cinfo;
274 	int error, i;
275 
276 	ida->cmd.int_enable(ida, 0);
277 
278 	error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo),
279 	    IDA_CONTROLLER, 0, DMA_DATA_IN);
280 	if (error) {
281 		device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n");
282 		return;
283 	}
284 
285 	device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n",
286 	    cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1],
287 	    cinfo.firm_rev[2], cinfo.firm_rev[3]);
288 
289 	if (ida->flags & IDA_FIRMWARE) {
290 		int data;
291 
292 		error = ida_command(ida, CMD_START_FIRMWARE,
293 		    &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN);
294 		if (error) {
295 			device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n");
296 			return;
297 		}
298 	}
299 
300 	ida->ida_dev_t = make_dev(&ida_cdevsw, ida->unit,
301 				 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
302 				 "ida%d", ida->unit);
303 	ida->ida_dev_t->si_drv1 = ida;
304 
305 	ida->num_drives = 0;
306 	for (i = 0; i < cinfo.num_drvs; i++)
307 		device_add_child(ida->dev, /*"idad"*/NULL, -1);
308 
309 	bus_generic_attach(ida->dev);
310 
311 	ida->cmd.int_enable(ida, 1);
312 }
313 
314 int
315 ida_detach(device_t dev)
316 {
317 	struct ida_softc *ida;
318 	int error = 0;
319 
320 	ida = (struct ida_softc *)device_get_softc(dev);
321 
322 	/*
323 	 * XXX
324 	 * before detaching, we must make sure that the system is
325 	 * quiescent; nothing mounted, no pending activity.
326 	 */
327 
328 	/*
329 	 * XXX
330 	 * now, how are we supposed to maintain a list of our drives?
331 	 * iterate over our "child devices"?
332 	 */
333 
334 	destroy_dev(ida->ida_dev_t);
335 	ida_free(ida);
336 	return (error);
337 }
338 
339 static void
340 ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
341 {
342 	struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg;
343 	int i;
344 
345 	hwqcb->hdr.size = htole16((sizeof(struct ida_req) +
346 	    sizeof(struct ida_sgb) * IDA_NSEG) >> 2);
347 
348 	for (i = 0; i < nsegments; i++) {
349 		hwqcb->seg[i].addr = htole32(segs[i].ds_addr);
350 		hwqcb->seg[i].length = htole32(segs[i].ds_len);
351 	}
352 	hwqcb->req.sgcount = nsegments;
353 }
354 
355 int
356 ida_command(struct ida_softc *ida, int command, void *data, int datasize,
357 	int drive, u_int32_t pblkno, int flags)
358 {
359 	struct ida_hardware_qcb *hwqcb;
360 	struct ida_qcb *qcb;
361 	bus_dmasync_op_t op;
362 	int s, error;
363 
364 	s = splbio();
365 	qcb = ida_get_qcb(ida);
366 	splx(s);
367 
368 	if (qcb == NULL) {
369 		printf("ida_command: out of QCBs");
370 		return (EAGAIN);
371 	}
372 
373 	hwqcb = qcb->hwqcb;
374 	bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
375 
376 	bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
377 	    (void *)data, datasize, ida_setup_dmamap, hwqcb, 0);
378 	op = qcb->flags & DMA_DATA_IN ?
379 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
380 	bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
381 
382 	hwqcb->hdr.drive = drive;
383 	hwqcb->req.blkno = htole32(pblkno);
384 	hwqcb->req.bcount = htole16(howmany(datasize, DEV_BSIZE));
385 	hwqcb->req.command = command;
386 
387 	qcb->flags = flags | IDA_COMMAND;
388 
389 	s = splbio();
390 	STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
391 	ida_start(ida);
392 	error = ida_wait(ida, qcb);
393 	splx(s);
394 
395 	/* XXX should have status returned here? */
396 	/* XXX have "status pointer" area in QCB? */
397 
398 	return (error);
399 }
400 
401 void
402 ida_submit_buf(struct ida_softc *ida, struct bio *bp)
403 {
404 	bioq_insert_tail(&ida->bio_queue, bp);
405 	ida_construct_qcb(ida);
406 	ida_start(ida);
407 }
408 
409 static void
410 ida_construct_qcb(struct ida_softc *ida)
411 {
412 	struct ida_hardware_qcb *hwqcb;
413 	struct ida_qcb *qcb;
414 	bus_dmasync_op_t op;
415 	struct bio *bp;
416 
417 	bp = bioq_first(&ida->bio_queue);
418 	if (bp == NULL)
419 		return;				/* no more buffers */
420 
421 	qcb = ida_get_qcb(ida);
422 	if (qcb == NULL)
423 		return;				/* out of resources */
424 
425 	bioq_remove(&ida->bio_queue, bp);
426 	qcb->buf = bp;
427 	qcb->flags = bp->bio_cmd == BIO_READ ? DMA_DATA_IN : DMA_DATA_OUT;
428 
429 	hwqcb = qcb->hwqcb;
430 	bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
431 
432 	bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
433 	    (void *)bp->bio_data, bp->bio_bcount, ida_setup_dmamap, hwqcb, 0);
434 	op = qcb->flags & DMA_DATA_IN ?
435 	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
436 	bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
437 
438 	{
439 		struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1;
440 		hwqcb->hdr.drive = drv->drive;
441 	}
442 
443 	hwqcb->req.blkno = bp->bio_pblkno;
444 	hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE);
445 	hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE;
446 
447 	STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
448 }
449 
450 /*
451  * This routine will be called from ida_intr in order to queue up more
452  * I/O, meaning that we may be in an interrupt context.  Hence, we should
453  * not muck around with spl() in this routine.
454  */
455 static void
456 ida_start(struct ida_softc *ida)
457 {
458 	struct ida_qcb *qcb;
459 
460 	while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) {
461 		if (ida->cmd.fifo_full(ida))
462 			break;
463 		STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe);
464 		/*
465 		 * XXX
466 		 * place the qcb on an active list?
467 		 */
468 
469 		/* Set a timeout. */
470 		if (!ida->qactive)
471 			callout_reset(&ida->ch, hz * 5, ida_timeout, ida);
472 		ida->qactive++;
473 
474 		qcb->state = QCB_ACTIVE;
475 		ida->cmd.submit(ida, qcb);
476 	}
477 }
478 
479 static int
480 ida_wait(struct ida_softc *ida, struct ida_qcb *qcb)
481 {
482 	struct ida_qcb *qcb_done = NULL;
483 	bus_addr_t completed;
484 	int delay;
485 
486 	if (ida->flags & IDA_INTERRUPTS) {
487 		if (tsleep(qcb, PRIBIO, "idacmd", 5 * hz))
488 			return (ETIMEDOUT);
489 		return (0);
490 	}
491 
492 again:
493 	delay = 5 * 1000 * 100;			/* 5 sec delay */
494 	while ((completed = ida->cmd.done(ida)) == 0) {
495 		if (delay-- == 0)
496 			return (ETIMEDOUT);
497 		DELAY(10);
498 	}
499 
500 	qcb_done = idahwqcbptov(ida, completed & ~3);
501 	if (qcb_done != qcb)
502 		goto again;
503 	ida_done(ida, qcb);
504 	return (0);
505 }
506 
507 void
508 ida_intr(void *data)
509 {
510 	struct ida_softc *ida;
511 	struct ida_qcb *qcb;
512 	bus_addr_t completed;
513 
514 	ida = (struct ida_softc *)data;
515 
516 	if (ida->cmd.int_pending(ida) == 0)
517 		return;				/* not our interrupt */
518 
519 	while ((completed = ida->cmd.done(ida)) != 0) {
520 		qcb = idahwqcbptov(ida, completed & ~3);
521 
522 		if (qcb == NULL || qcb->state != QCB_ACTIVE) {
523 			device_printf(ida->dev,
524 			    "ignoring completion %jx\n", (intmax_t)completed);
525 			continue;
526 		}
527 		/* Handle "Bad Command List" errors. */
528 		if ((completed & 3) && (qcb->hwqcb->req.error == 0))
529 			qcb->hwqcb->req.error = CMD_REJECTED;
530 		ida_done(ida, qcb);
531 	}
532 	ida_start(ida);
533 }
534 
535 /*
536  * should switch out command type; may be status, not just I/O.
537  */
538 static void
539 ida_done(struct ida_softc *ida, struct ida_qcb *qcb)
540 {
541 	int error = 0;
542 
543 	/*
544 	 * finish up command
545 	 */
546 	if (qcb->flags & DMA_DATA_TRANSFER) {
547 		bus_dmasync_op_t op;
548 
549 		op = qcb->flags & DMA_DATA_IN ?
550 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE;
551 		bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
552 		bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap);
553 	}
554 
555 	if (qcb->hwqcb->req.error & SOFT_ERROR) {
556 		if (qcb->buf)
557 			device_printf(ida->dev, "soft %s error\n",
558 				qcb->buf->bio_cmd == BIO_READ ?
559 					"read" : "write");
560 		else
561 			device_printf(ida->dev, "soft error\n");
562 	}
563 	if (qcb->hwqcb->req.error & HARD_ERROR) {
564 		error = 1;
565 		if (qcb->buf)
566 			device_printf(ida->dev, "hard %s error\n",
567 				qcb->buf->bio_cmd == BIO_READ ?
568 					"read" : "write");
569 		else
570 			device_printf(ida->dev, "hard error\n");
571 	}
572 	if (qcb->hwqcb->req.error & CMD_REJECTED) {
573 		error = 1;
574 		device_printf(ida->dev, "invalid request\n");
575 	}
576 
577 	if (qcb->flags & IDA_COMMAND) {
578 		if (ida->flags & IDA_INTERRUPTS)
579 			wakeup(qcb);
580 	} else {
581 		KASSERT(qcb->buf != NULL, ("ida_done(): qcb->buf is NULL!"));
582 		if (error)
583 			qcb->buf->bio_flags |= BIO_ERROR;
584 		idad_intr(qcb->buf);
585 	}
586 
587 	ida->qactive--;
588 	/* Reschedule or cancel timeout */
589 	if (ida->qactive)
590 		callout_reset(&ida->ch, hz * 5, ida_timeout, ida);
591 	else
592 		callout_stop(&ida->ch);
593 
594 	qcb->state = QCB_FREE;
595 	qcb->buf = NULL;
596 	SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
597 	ida_construct_qcb(ida);
598 }
599 
600 static void
601 ida_timeout (void *arg)
602 {
603 	struct ida_softc *ida;
604 
605 	ida = (struct ida_softc *)arg;
606 	device_printf(ida->dev, "%s() qactive %d\n", __func__, ida->qactive);
607 
608 	if (ida->flags & IDA_INTERRUPTS)
609 		device_printf(ida->dev, "IDA_INTERRUPTS\n");
610 
611 	device_printf(ida->dev,	"\t   R_CMD_FIFO: %08x\n"
612 				"\t  R_DONE_FIFO: %08x\n"
613 				"\t   R_INT_MASK: %08x\n"
614 				"\t     R_STATUS: %08x\n"
615 				"\tR_INT_PENDING: %08x\n",
616 					ida_inl(ida, R_CMD_FIFO),
617 					ida_inl(ida, R_DONE_FIFO),
618 					ida_inl(ida, R_INT_MASK),
619 					ida_inl(ida, R_STATUS),
620 					ida_inl(ida, R_INT_PENDING));
621 
622 	return;
623 }
624 
625 /*
626  * IOCTL stuff follows.
627  */
628 struct cmd_info {
629 	int	cmd;
630 	int	len;
631 	int	flags;
632 };
633 static struct cmd_info *ida_cmd_lookup(int);
634 
635 static int
636 ida_ioctl (struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
637 {
638 	struct ida_softc *sc;
639 	struct ida_user_command *uc;
640 	struct cmd_info *ci;
641 	int len;
642 	int flags;
643 	int error;
644 	int data;
645 	void *daddr;
646 
647 	sc = (struct ida_softc *)dev->si_drv1;
648 	uc = (struct ida_user_command *)addr;
649 	error = 0;
650 
651 	switch (cmd) {
652 	case IDAIO_COMMAND:
653 		ci = ida_cmd_lookup(uc->command);
654 		if (ci == NULL) {
655 			error = EINVAL;
656 			break;
657 		}
658 		len = ci->len;
659 		flags = ci->flags;
660 		if (len)
661 			daddr = &uc->d.buf;
662 		else {
663 			daddr = &data;
664 			len = sizeof(data);
665 		}
666 		error = ida_command(sc, uc->command, daddr, len,
667 				    uc->drive, uc->blkno, flags);
668 		break;
669 	default:
670 		error = ENOIOCTL;
671 		break;
672 	}
673 	return (error);
674 }
675 
676 static struct cmd_info ci_list[] = {
677 	{ CMD_GET_LOG_DRV_INFO,
678 			sizeof(struct ida_drive_info), DMA_DATA_IN },
679 	{ CMD_GET_CTRL_INFO,
680 			sizeof(struct ida_controller_info), DMA_DATA_IN },
681 	{ CMD_SENSE_DRV_STATUS,
682 			sizeof(struct ida_drive_status), DMA_DATA_IN },
683 	{ CMD_START_RECOVERY,		0, 0 },
684 	{ CMD_GET_PHYS_DRV_INFO,
685 			sizeof(struct ida_phys_drv_info), DMA_DATA_TRANSFER },
686 	{ CMD_BLINK_DRV_LEDS,
687 			sizeof(struct ida_blink_drv_leds), DMA_DATA_OUT },
688 	{ CMD_SENSE_DRV_LEDS,
689 			sizeof(struct ida_blink_drv_leds), DMA_DATA_IN },
690 	{ CMD_GET_LOG_DRV_EXT,
691 			sizeof(struct ida_drive_info_ext), DMA_DATA_IN },
692 	{ CMD_RESET_CTRL,		0, 0 },
693 	{ CMD_GET_CONFIG,		0, 0 },
694 	{ CMD_SET_CONFIG,		0, 0 },
695 	{ CMD_LABEL_LOG_DRV,
696 			sizeof(struct ida_label_logical), DMA_DATA_OUT },
697 	{ CMD_SET_SURFACE_DELAY,	0, 0 },
698 	{ CMD_SENSE_BUS_PARAMS,		0, 0 },
699 	{ CMD_SENSE_SUBSYS_INFO,	0, 0 },
700 	{ CMD_SENSE_SURFACE_ATS,	0, 0 },
701 	{ CMD_PASSTHROUGH,		0, 0 },
702 	{ CMD_RESET_SCSI_DEV,		0, 0 },
703 	{ CMD_PAUSE_BG_ACT,		0, 0 },
704 	{ CMD_RESUME_BG_ACT,		0, 0 },
705 	{ CMD_START_FIRMWARE,		0, 0 },
706 	{ CMD_SENSE_DRV_ERR_LOG,	0, 0 },
707 	{ CMD_START_CPM,		0, 0 },
708 	{ CMD_SENSE_CP,			0, 0 },
709 	{ CMD_STOP_CPM,			0, 0 },
710 	{ CMD_FLUSH_CACHE,		0, 0 },
711 	{ CMD_ACCEPT_MEDIA_EXCH,	0, 0 },
712 	{ 0, 0, 0 }
713 };
714 
715 static struct cmd_info *
716 ida_cmd_lookup (int command)
717 {
718 	struct cmd_info *ci;
719 
720 	ci = ci_list;
721 	while (ci->cmd) {
722 		if (ci->cmd == command)
723 			return (ci);
724 		ci++;
725 	}
726 	return (NULL);
727 }
728