2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999,2000 Jonathan Lemon
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Generic driver for Compaq SMART RAID adapters.
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
51 #include <sys/endian.h>
53 #include <machine/bus.h>
56 #include <geom/geom_disk.h>
58 #include <dev/ida/idareg.h>
59 #include <dev/ida/idavar.h>
60 #include <dev/ida/idaio.h>
63 static int ida_alloc_qcbs(struct ida_softc *ida);
64 static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb);
65 static void ida_start(struct ida_softc *ida);
66 static void ida_startio(struct ida_softc *ida);
67 static void ida_startup(void *arg);
68 static void ida_timeout(void *arg);
69 static int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb);
71 static d_ioctl_t ida_ioctl;
72 static struct cdevsw ida_cdevsw = {
73 .d_version = D_VERSION,
79 ida_free(struct ida_softc *ida)
84 bus_teardown_intr(ida->dev, ida->irq, ida->ih);
87 callout_stop(&ida->ch);
88 mtx_unlock(&ida->lock);
89 callout_drain(&ida->ch);
91 if (ida->buffer_dmat) {
92 for (i = 0; i < IDA_QCB_MAX; i++)
93 bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap);
94 bus_dma_tag_destroy(ida->buffer_dmat);
97 if (ida->hwqcb_dmat) {
98 if (ida->hwqcb_busaddr)
99 bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap);
101 bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs,
103 bus_dma_tag_destroy(ida->hwqcb_dmat);
106 if (ida->qcbs != NULL)
107 free(ida->qcbs, M_DEVBUF);
109 if (ida->irq != NULL)
110 bus_release_resource(ida->dev, ida->irq_res_type,
113 if (ida->parent_dmat != NULL)
114 bus_dma_tag_destroy(ida->parent_dmat);
116 if (ida->regs != NULL)
117 bus_release_resource(ida->dev, ida->regs_res_type,
118 ida->regs_res_id, ida->regs);
120 mtx_destroy(&ida->lock);
124 * record bus address from bus_dmamap_load
127 ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
131 baddr = (bus_addr_t *)arg;
132 *baddr = segs->ds_addr;
135 static __inline struct ida_qcb *
136 ida_get_qcb(struct ida_softc *ida)
140 if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) {
141 SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
142 bzero(qcb->hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
148 ida_free_qcb(struct ida_softc *ida, struct ida_qcb *qcb)
151 qcb->state = QCB_FREE;
154 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
157 static __inline bus_addr_t
158 idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb)
160 return (ida->hwqcb_busaddr +
161 ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs));
164 static __inline struct ida_qcb *
165 idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr)
167 struct ida_hardware_qcb *hwqcb;
169 hwqcb = (struct ida_hardware_qcb *)
170 ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr));
175 ida_alloc_qcbs(struct ida_softc *ida)
180 for (i = 0; i < IDA_QCB_MAX; i++) {
183 error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap);
188 qcb->flags = QCB_FREE;
189 qcb->hwqcb = &ida->hwqcbs[i];
190 qcb->hwqcb->qcb = qcb;
191 qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb);
192 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
198 ida_setup(struct ida_softc *ida)
200 struct ida_controller_info cinfo;
204 SLIST_INIT(&ida->free_qcbs);
205 STAILQ_INIT(&ida->qcb_queue);
206 bioq_init(&ida->bio_queue);
208 ida->qcbs = (struct ida_qcb *)
209 malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF,
211 if (ida->qcbs == NULL)
215 * Create our DMA tags
218 /* DMA tag for our hardware QCB structures */
219 error = bus_dma_tag_create(
220 /* parent */ ida->parent_dmat,
223 /* lowaddr */ BUS_SPACE_MAXADDR,
224 /* highaddr */ BUS_SPACE_MAXADDR,
226 /* filterarg */ NULL,
227 /* maxsize */ IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
229 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
237 /* DMA tag for mapping buffers into device space */
238 error = bus_dma_tag_create(
239 /* parent */ ida->parent_dmat,
242 /* lowaddr */ BUS_SPACE_MAXADDR,
243 /* highaddr */ BUS_SPACE_MAXADDR,
245 /* filterarg */ NULL,
246 /* maxsize */ DFLTPHYS,
247 /* nsegments */ IDA_NSEG,
248 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
250 /* lockfunc */ busdma_lock_mutex,
251 /* lockarg */ &Giant,
256 /* Allocation of hardware QCBs */
257 /* XXX allocation is rounded to hardware page size */
258 error = bus_dmamem_alloc(ida->hwqcb_dmat,
259 (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap);
263 /* And permanently map them in */
264 bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap,
265 ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
266 ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0);
268 bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb));
270 error = ida_alloc_qcbs(ida);
274 mtx_lock(&ida->lock);
275 ida->cmd.int_enable(ida, 0);
277 error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo),
278 IDA_CONTROLLER, 0, DMA_DATA_IN);
280 mtx_unlock(&ida->lock);
281 device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n");
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]);
289 if (ida->flags & IDA_FIRMWARE) {
292 error = ida_command(ida, CMD_START_FIRMWARE,
293 &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN);
295 mtx_unlock(&ida->lock);
296 device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n");
301 ida->cmd.int_enable(ida, 1);
302 ida->flags |= IDA_ATTACHED;
303 mtx_unlock(&ida->lock);
305 for (i = 0; i < cinfo.num_drvs; i++) {
306 child = device_add_child(ida->dev, /*"idad"*/NULL, -1);
308 device_set_ivars(child, (void *)(intptr_t)i);
311 ida->ich.ich_func = ida_startup;
312 ida->ich.ich_arg = ida;
313 if (config_intrhook_establish(&ida->ich) != 0) {
314 device_delete_children(ida->dev);
315 device_printf(ida->dev, "Cannot establish configuration hook\n");
319 unit = device_get_unit(ida->dev);
320 ida->ida_dev_t = make_dev(&ida_cdevsw, unit,
321 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
323 ida->ida_dev_t->si_drv1 = ida;
329 ida_startup(void *arg)
331 struct ida_softc *ida;
335 config_intrhook_disestablish(&ida->ich);
338 bus_generic_attach(ida->dev);
343 ida_detach(device_t dev)
345 struct ida_softc *ida;
348 ida = (struct ida_softc *)device_get_softc(dev);
350 error = bus_generic_detach(dev);
353 error = device_delete_children(dev);
359 * before detaching, we must make sure that the system is
360 * quiescent; nothing mounted, no pending activity.
365 * now, how are we supposed to maintain a list of our drives?
366 * iterate over our "child devices"?
369 destroy_dev(ida->ida_dev_t);
375 ida_data_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
377 struct ida_hardware_qcb *hwqcb;
378 struct ida_softc *ida;
386 mtx_assert(&ida->lock, MA_OWNED);
394 hwqcb->hdr.size = htole16((sizeof(struct ida_req) +
395 sizeof(struct ida_sgb) * IDA_NSEG) >> 2);
397 for (i = 0; i < nsegments; i++) {
398 hwqcb->seg[i].addr = htole32(segs[i].ds_addr);
399 hwqcb->seg[i].length = htole32(segs[i].ds_len);
401 hwqcb->req.sgcount = nsegments;
402 if (qcb->flags & DMA_DATA_TRANSFER) {
403 switch (qcb->flags & DMA_DATA_TRANSFER) {
404 case DMA_DATA_TRANSFER:
405 op = BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE;
408 op = BUS_DMASYNC_PREREAD;
411 KASSERT((qcb->flags & DMA_DATA_TRANSFER) ==
412 DMA_DATA_OUT, ("bad DMA data flags"));
413 op = BUS_DMASYNC_PREWRITE;
416 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
418 bus_dmamap_sync(ida->hwqcb_dmat, ida->hwqcb_dmamap,
419 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
421 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
423 ida->flags &= ~IDA_QFROZEN;
427 ida_map_qcb(struct ida_softc *ida, struct ida_qcb *qcb, void *data,
432 if (ida->flags & IDA_INTERRUPTS)
433 flags = BUS_DMA_WAITOK;
435 flags = BUS_DMA_NOWAIT;
436 error = bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, data, datasize,
437 ida_data_cb, qcb, flags);
438 if (error == EINPROGRESS) {
439 ida->flags |= IDA_QFROZEN;
446 ida_command(struct ida_softc *ida, int command, void *data, int datasize,
447 int drive, u_int32_t pblkno, int flags)
449 struct ida_hardware_qcb *hwqcb;
454 mtx_assert(&ida->lock, MA_OWNED);
455 qcb = ida_get_qcb(ida);
458 device_printf(ida->dev, "out of QCBs\n");
462 qcb->flags = flags | IDA_COMMAND;
464 hwqcb->hdr.drive = drive;
465 hwqcb->req.blkno = htole32(pblkno);
466 hwqcb->req.bcount = htole16(howmany(datasize, DEV_BSIZE));
467 hwqcb->req.command = command;
469 error = ida_map_qcb(ida, qcb, data, datasize);
471 error = ida_wait(ida, qcb);
472 /* Don't free QCB on a timeout in case it later completes. */
478 /* XXX should have status returned here? */
479 /* XXX have "status pointer" area in QCB? */
481 ida_free_qcb(ida, qcb);
486 ida_submit_buf(struct ida_softc *ida, struct bio *bp)
488 mtx_lock(&ida->lock);
489 bioq_insert_tail(&ida->bio_queue, bp);
491 mtx_unlock(&ida->lock);
495 ida_startio(struct ida_softc *ida)
497 struct ida_hardware_qcb *hwqcb;
499 struct idad_softc *drv;
503 mtx_assert(&ida->lock, MA_OWNED);
505 if (ida->flags & IDA_QFROZEN)
507 bp = bioq_first(&ida->bio_queue);
509 return; /* no more buffers */
511 qcb = ida_get_qcb(ida);
513 return; /* out of resources */
515 bioq_remove(&ida->bio_queue, bp);
517 qcb->flags = bp->bio_cmd == BIO_READ ? DMA_DATA_IN : DMA_DATA_OUT;
520 drv = bp->bio_driver1;
521 hwqcb->hdr.drive = drv->drive;
522 hwqcb->req.blkno = bp->bio_pblkno;
523 hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE);
524 hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE;
526 error = ida_map_qcb(ida, qcb, bp->bio_data, bp->bio_bcount);
535 ida_start(struct ida_softc *ida)
540 mtx_assert(&ida->lock, MA_OWNED);
541 while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) {
542 if (ida->cmd.fifo_full(ida))
544 STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe);
547 * place the qcb on an active list?
551 if (!ida->qactive && !dumping)
552 callout_reset(&ida->ch, hz * 5, ida_timeout, ida);
555 qcb->state = QCB_ACTIVE;
556 ida->cmd.submit(ida, qcb);
561 ida_wait(struct ida_softc *ida, struct ida_qcb *qcb)
563 struct ida_qcb *qcb_done = NULL;
564 bus_addr_t completed;
568 mtx_assert(&ida->lock, MA_OWNED);
569 if (ida->flags & IDA_INTERRUPTS) {
570 if (mtx_sleep(qcb, &ida->lock, PRIBIO, "idacmd", 5 * hz)) {
571 qcb->state = QCB_TIMEDOUT;
578 delay = 5 * 1000 * 100; /* 5 sec delay */
579 while ((completed = ida->cmd.done(ida)) == 0) {
581 qcb->state = QCB_TIMEDOUT;
587 qcb_done = idahwqcbptov(ida, completed & ~3);
597 struct ida_softc *ida;
599 bus_addr_t completed;
601 ida = (struct ida_softc *)data;
603 mtx_lock(&ida->lock);
604 if (ida->cmd.int_pending(ida) == 0) {
605 mtx_unlock(&ida->lock);
606 return; /* not our interrupt */
609 while ((completed = ida->cmd.done(ida)) != 0) {
610 qcb = idahwqcbptov(ida, completed & ~3);
612 if (qcb == NULL || qcb->state != QCB_ACTIVE) {
613 device_printf(ida->dev,
614 "ignoring completion %jx\n", (intmax_t)completed);
617 /* Handle "Bad Command List" errors. */
618 if ((completed & 3) && (qcb->hwqcb->req.error == 0))
619 qcb->hwqcb->req.error = CMD_REJECTED;
623 mtx_unlock(&ida->lock);
627 * should switch out command type; may be status, not just I/O.
630 ida_done(struct ida_softc *ida, struct ida_qcb *qcb)
633 int active, error = 0;
639 mtx_assert(&ida->lock, MA_OWNED);
640 active = (qcb->state != QCB_FREE);
641 if (qcb->flags & DMA_DATA_TRANSFER && active) {
642 switch (qcb->flags & DMA_DATA_TRANSFER) {
643 case DMA_DATA_TRANSFER:
644 op = BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE;
647 op = BUS_DMASYNC_POSTREAD;
650 KASSERT((qcb->flags & DMA_DATA_TRANSFER) ==
651 DMA_DATA_OUT, ("bad DMA data flags"));
652 op = BUS_DMASYNC_POSTWRITE;
655 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
656 bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap);
659 bus_dmamap_sync(ida->hwqcb_dmat, ida->hwqcb_dmamap,
660 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
662 if (qcb->hwqcb->req.error & SOFT_ERROR) {
664 device_printf(ida->dev, "soft %s error\n",
665 qcb->buf->bio_cmd == BIO_READ ?
668 device_printf(ida->dev, "soft error\n");
670 if (qcb->hwqcb->req.error & HARD_ERROR) {
673 device_printf(ida->dev, "hard %s error\n",
674 qcb->buf->bio_cmd == BIO_READ ?
677 device_printf(ida->dev, "hard error\n");
679 if (qcb->hwqcb->req.error & CMD_REJECTED) {
681 device_printf(ida->dev, "invalid request\n");
685 device_printf(ida->dev, "request failed to map: %d\n", qcb->error);
688 if (qcb->flags & IDA_COMMAND) {
689 if (ida->flags & IDA_INTERRUPTS)
691 if (qcb->state == QCB_TIMEDOUT)
692 ida_free_qcb(ida, qcb);
694 KASSERT(qcb->buf != NULL, ("ida_done(): qcb->buf is NULL!"));
696 qcb->buf->bio_flags |= BIO_ERROR;
698 ida_free_qcb(ida, qcb);
705 /* Reschedule or cancel timeout */
707 callout_reset(&ida->ch, hz * 5, ida_timeout, ida);
709 callout_stop(&ida->ch);
713 ida_timeout(void *arg)
715 struct ida_softc *ida;
717 ida = (struct ida_softc *)arg;
718 device_printf(ida->dev, "%s() qactive %d\n", __func__, ida->qactive);
720 if (ida->flags & IDA_INTERRUPTS)
721 device_printf(ida->dev, "IDA_INTERRUPTS\n");
723 device_printf(ida->dev, "\t R_CMD_FIFO: %08x\n"
724 "\t R_DONE_FIFO: %08x\n"
725 "\t R_INT_MASK: %08x\n"
726 "\t R_STATUS: %08x\n"
727 "\tR_INT_PENDING: %08x\n",
728 ida_inl(ida, R_CMD_FIFO),
729 ida_inl(ida, R_DONE_FIFO),
730 ida_inl(ida, R_INT_MASK),
731 ida_inl(ida, R_STATUS),
732 ida_inl(ida, R_INT_PENDING));
738 * IOCTL stuff follows.
745 static struct cmd_info *ida_cmd_lookup(int);
748 ida_ioctl (struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
750 struct ida_softc *sc;
751 struct ida_user_command *uc;
759 sc = (struct ida_softc *)dev->si_drv1;
760 uc = (struct ida_user_command *)addr;
765 ci = ida_cmd_lookup(uc->command);
779 error = ida_command(sc, uc->command, daddr, len,
780 uc->drive, uc->blkno, flags);
781 mtx_unlock(&sc->lock);
790 static struct cmd_info ci_list[] = {
791 { CMD_GET_LOG_DRV_INFO,
792 sizeof(struct ida_drive_info), DMA_DATA_IN },
794 sizeof(struct ida_controller_info), DMA_DATA_IN },
795 { CMD_SENSE_DRV_STATUS,
796 sizeof(struct ida_drive_status), DMA_DATA_IN },
797 { CMD_START_RECOVERY, 0, 0 },
798 { CMD_GET_PHYS_DRV_INFO,
799 sizeof(struct ida_phys_drv_info), DMA_DATA_TRANSFER },
800 { CMD_BLINK_DRV_LEDS,
801 sizeof(struct ida_blink_drv_leds), DMA_DATA_OUT },
802 { CMD_SENSE_DRV_LEDS,
803 sizeof(struct ida_blink_drv_leds), DMA_DATA_IN },
804 { CMD_GET_LOG_DRV_EXT,
805 sizeof(struct ida_drive_info_ext), DMA_DATA_IN },
806 { CMD_RESET_CTRL, 0, 0 },
807 { CMD_GET_CONFIG, 0, 0 },
808 { CMD_SET_CONFIG, 0, 0 },
810 sizeof(struct ida_label_logical), DMA_DATA_OUT },
811 { CMD_SET_SURFACE_DELAY, 0, 0 },
812 { CMD_SENSE_BUS_PARAMS, 0, 0 },
813 { CMD_SENSE_SUBSYS_INFO, 0, 0 },
814 { CMD_SENSE_SURFACE_ATS, 0, 0 },
815 { CMD_PASSTHROUGH, 0, 0 },
816 { CMD_RESET_SCSI_DEV, 0, 0 },
817 { CMD_PAUSE_BG_ACT, 0, 0 },
818 { CMD_RESUME_BG_ACT, 0, 0 },
819 { CMD_START_FIRMWARE, 0, 0 },
820 { CMD_SENSE_DRV_ERR_LOG, 0, 0 },
821 { CMD_START_CPM, 0, 0 },
822 { CMD_SENSE_CP, 0, 0 },
823 { CMD_STOP_CPM, 0, 0 },
824 { CMD_FLUSH_CACHE, 0, 0 },
825 { CMD_ACCEPT_MEDIA_EXCH, 0, 0 },
829 static struct cmd_info *
830 ida_cmd_lookup (int command)
836 if (ci->cmd == command)