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