2 * Copyright (c) 1999,2000 Jonathan Lemon
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $FreeBSD: src/sys/dev/ida/ida.c,v 1.7.2.3 2001/03/01 01:57:32 ps Exp $
34 * Generic driver for Compaq SMART RAID adapters.
36 * Specific probe routines are in:
37 * dev/raid/ida/ida_pci.c
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
48 #include <sys/devicestat.h>
52 #include <sys/thread2.h>
54 #include <machine/clock.h>
60 static void ida_alloc_qcb(struct ida_softc *ida);
61 static void ida_construct_qcb(struct ida_softc *ida);
62 static void ida_start(struct ida_softc *ida);
63 static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb);
64 static int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb);
66 DECLARE_DUMMY_MODULE(ida);
69 ida_free(struct ida_softc *ida)
73 for (i = 0; i < ida->num_qcbs; i++)
74 bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap);
76 if (ida->hwqcb_busaddr)
77 bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap);
80 bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs,
84 bus_dma_tag_destroy(ida->buffer_dmat);
87 bus_dma_tag_destroy(ida->hwqcb_dmat);
89 if (ida->qcbs != NULL)
90 kfree(ida->qcbs, M_DEVBUF);
93 bus_teardown_intr(ida->dev, ida->irq, ida->ih);
96 bus_release_resource(ida->dev, ida->irq_res_type,
99 if (ida->parent_dmat != NULL)
100 bus_dma_tag_destroy(ida->parent_dmat);
102 if (ida->regs != NULL)
103 bus_release_resource(ida->dev, ida->regs_res_type,
104 ida->regs_res_id, ida->regs);
108 * record bus address from bus_dmamap_load
111 ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
115 baddr = (bus_addr_t *)arg;
116 *baddr = segs->ds_addr;
119 static __inline struct ida_qcb *
120 ida_get_qcb(struct ida_softc *ida)
124 if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) {
125 SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
128 if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL)
129 SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle);
134 static __inline bus_addr_t
135 idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb)
137 return (ida->hwqcb_busaddr +
138 ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs));
141 static __inline struct ida_qcb *
142 idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr)
144 struct ida_hardware_qcb *hwqcb;
146 hwqcb = (struct ida_hardware_qcb *)
147 ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr));
153 * since we allocate all QCB space up front during initialization, then
154 * why bother with this routine?
157 ida_alloc_qcb(struct ida_softc *ida)
162 if (ida->num_qcbs >= IDA_QCB_MAX)
165 qcb = &ida->qcbs[ida->num_qcbs];
167 error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap);
171 qcb->flags = QCB_FREE;
172 qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs];
173 qcb->hwqcb->qcb = qcb;
174 qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb);
175 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
180 ida_init(struct ida_softc *ida)
184 ida->unit = device_get_unit(ida->dev);
185 ida->tag = rman_get_bustag(ida->regs);
186 ida->bsh = rman_get_bushandle(ida->regs);
188 SLIST_INIT(&ida->free_qcbs);
189 STAILQ_INIT(&ida->qcb_queue);
190 bioq_init(&ida->bio_queue);
192 ida->qcbs = kmalloc(IDA_QCB_MAX * sizeof(struct ida_qcb),
193 M_DEVBUF, M_INTWAIT|M_ZERO);
196 * Create our DMA tags
199 /* DMA tag for our hardware QCB structures */
200 error = bus_dma_tag_create(ida->parent_dmat,
201 /*alignment*/1, /*boundary*/0,
202 /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR,
203 /*filter*/NULL, /*filterarg*/NULL,
204 IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
205 /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
206 /*flags*/0, &ida->hwqcb_dmat);
210 /* DMA tag for mapping buffers into device space */
211 error = bus_dma_tag_create(ida->parent_dmat,
212 /*alignment*/1, /*boundary*/0,
213 /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR,
214 /*filter*/NULL, /*filterarg*/NULL,
215 /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG,
216 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, &ida->buffer_dmat);
220 /* Allocation of hardware QCBs */
221 /* XXX allocation is rounded to hardware page size */
222 error = bus_dmamem_alloc(ida->hwqcb_dmat,
223 (void *)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap);
227 /* And permanently map them in */
228 bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap,
229 ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb),
230 ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0);
232 bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb));
234 ida_alloc_qcb(ida); /* allocate an initial qcb */
240 ida_attach(struct ida_softc *ida)
242 struct ida_controller_info cinfo;
245 ida->cmd.int_enable(ida, 0);
247 error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo),
248 IDA_CONTROLLER, 0, DMA_DATA_IN);
250 device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n");
254 device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n",
255 cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1],
256 cinfo.firm_rev[2], cinfo.firm_rev[3]);
258 if (ida->flags & IDA_FIRMWARE) {
261 error = ida_command(ida, CMD_START_FIRMWARE,
262 &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN);
264 device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n");
270 for (i = 0; i < cinfo.num_drvs; i++)
271 device_add_child(ida->dev, /*"idad"*/NULL, -1);
273 bus_generic_attach(ida->dev);
275 ida->cmd.int_enable(ida, 1);
279 ida_detach(device_t dev)
281 struct ida_softc *ida;
284 ida = (struct ida_softc *)device_get_softc(dev);
288 * before detaching, we must make sure that the system is
289 * quiescent; nothing mounted, no pending activity.
294 * now, how are we supposed to maintain a list of our drives?
295 * iterate over our "child devices"?
304 ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
306 struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg;
309 hwqcb->hdr.size = (sizeof(struct ida_req) +
310 sizeof(struct ida_sgb) * IDA_NSEG) >> 2;
312 for (i = 0; i < nsegments; i++) {
313 hwqcb->seg[i].addr = segs[i].ds_addr;
314 hwqcb->seg[i].length = segs[i].ds_len;
316 hwqcb->req.sgcount = nsegments;
320 ida_command(struct ida_softc *ida, int command, void *data, int datasize,
321 int drive, u_int64_t pblkno, int flags)
323 struct ida_hardware_qcb *hwqcb;
329 qcb = ida_get_qcb(ida);
333 kprintf("ida_command: out of QCBs");
338 bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
340 bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
341 (void *)data, datasize, ida_setup_dmamap, hwqcb, 0);
342 op = qcb->flags & DMA_DATA_IN ?
343 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
344 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
346 hwqcb->hdr.drive = drive;
347 hwqcb->req.blkno = pblkno;
348 hwqcb->req.bcount = howmany(datasize, DEV_BSIZE);
349 hwqcb->req.command = command;
351 KKASSERT(pblkno < 0x100000000ULL);
353 qcb->flags = flags | IDA_COMMAND;
356 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
358 error = ida_wait(ida, qcb);
361 /* XXX should have status returned here? */
362 /* XXX have "status pointer" area in QCB? */
368 ida_submit_buf(struct ida_softc *ida, struct bio *bio)
370 bioqdisksort(&ida->bio_queue, bio);
371 ida_construct_qcb(ida);
376 ida_construct_qcb(struct ida_softc *ida)
378 struct ida_hardware_qcb *hwqcb;
384 bio = bioq_first(&ida->bio_queue);
386 return; /* no more buffers */
388 qcb = ida_get_qcb(ida);
390 return; /* out of resources */
392 bioq_remove(&ida->bio_queue, bio);
397 bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req));
400 bus_dmamap_load(ida->buffer_dmat, qcb->dmamap,
401 (void *)bp->b_data, bp->b_bcount, ida_setup_dmamap, hwqcb, 0);
402 op = qcb->flags & DMA_DATA_IN ?
403 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
404 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
407 struct idad_softc *drv;
409 drv = (struct idad_softc *)bio->bio_driver_info;
410 hwqcb->hdr.drive = drv->drive;
413 hwqcb->req.blkno = bio->bio_offset >> DEV_BSHIFT;
414 hwqcb->req.bcount = howmany(bp->b_bcount, DEV_BSIZE);
415 hwqcb->req.command = (bp->b_cmd == BUF_CMD_READ) ? CMD_READ : CMD_WRITE;
417 KKASSERT(bio->bio_offset < 0x100000000ULL * DEV_BSIZE);
419 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe);
423 * This routine will be called from ida_intr in order to queue up more
424 * I/O, meaning that we may be in an interrupt context. Hence, we should
425 * not muck around with spl() in this routine.
428 ida_start(struct ida_softc *ida)
432 while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) {
433 if (ida->cmd.fifo_full(ida))
435 STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe);
438 * place the qcb on an active list and set a timeout?
440 qcb->state = QCB_ACTIVE;
441 ida->cmd.submit(ida, qcb);
446 ida_wait(struct ida_softc *ida, struct ida_qcb *qcb)
448 struct ida_qcb *qcb_done = NULL;
449 bus_addr_t completed;
452 if (ida->flags & IDA_INTERRUPTS) {
453 if (tsleep((caddr_t)qcb, 0, "idacmd", 5 * hz))
459 delay = 5 * 1000 * 100; /* 5 sec delay */
460 while ((completed = ida->cmd.done(ida)) == 0) {
466 qcb_done = idahwqcbptov(ida, completed & ~3);
476 struct ida_softc *ida;
478 bus_addr_t completed;
480 ida = (struct ida_softc *)data;
482 if (ida->cmd.int_pending(ida) == 0)
483 return; /* not our interrupt */
485 while ((completed = ida->cmd.done(ida)) != 0) {
486 qcb = idahwqcbptov(ida, completed & ~3);
488 if (qcb == NULL || qcb->state != QCB_ACTIVE) {
489 device_printf(ida->dev,
490 "ignoring completion %jx\n", (uintmax_t)completed);
499 * should switch out command type; may be status, not just I/O.
502 ida_done(struct ida_softc *ida, struct ida_qcb *qcb)
509 if (qcb->flags & DMA_DATA_TRANSFER) {
512 op = qcb->flags & DMA_DATA_IN ?
513 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE;
514 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op);
515 bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap);
518 if (qcb->hwqcb->req.error & SOFT_ERROR)
519 device_printf(ida->dev, "soft error\n");
520 if (qcb->hwqcb->req.error & HARD_ERROR) {
522 device_printf(ida->dev, "hard error\n");
524 if (qcb->hwqcb->req.error & CMD_REJECTED) {
526 device_printf(ida->dev, "invalid request\n");
529 if (qcb->flags & IDA_COMMAND) {
530 if (ida->flags & IDA_INTERRUPTS)
534 qcb->bio->bio_buf->b_flags |= B_ERROR;
538 qcb->state = QCB_FREE;
539 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle);
540 ida_construct_qcb(ida);