1db57feb7SJonathan Lemon /*- 2ee7eb00eSJonathan Lemon * Copyright (c) 1999,2000 Jonathan Lemon 3db57feb7SJonathan Lemon * All rights reserved. 4db57feb7SJonathan Lemon * 5db57feb7SJonathan Lemon # Derived from the original IDA Compaq RAID driver, which is 6db57feb7SJonathan Lemon * Copyright (c) 1996, 1997, 1998, 1999 7db57feb7SJonathan Lemon * Mark Dawson and David James. All rights reserved. 8db57feb7SJonathan Lemon * 9db57feb7SJonathan Lemon * Redistribution and use in source and binary forms, with or without 10db57feb7SJonathan Lemon * modification, are permitted provided that the following conditions 11db57feb7SJonathan Lemon * are met: 12db57feb7SJonathan Lemon * 1. Redistributions of source code must retain the above copyright 13db57feb7SJonathan Lemon * notice, this list of conditions and the following disclaimer. 14db57feb7SJonathan Lemon * 2. Redistributions in binary form must reproduce the above copyright 15db57feb7SJonathan Lemon * notice, this list of conditions and the following disclaimer in the 16db57feb7SJonathan Lemon * documentation and/or other materials provided with the distribution. 17db57feb7SJonathan Lemon * 18db57feb7SJonathan Lemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19db57feb7SJonathan Lemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20db57feb7SJonathan Lemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21db57feb7SJonathan Lemon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22db57feb7SJonathan Lemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23db57feb7SJonathan Lemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24db57feb7SJonathan Lemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25db57feb7SJonathan Lemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26db57feb7SJonathan Lemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27db57feb7SJonathan Lemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28db57feb7SJonathan Lemon * SUCH DAMAGE. 29db57feb7SJonathan Lemon */ 30db57feb7SJonathan Lemon 31aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 33aad970f1SDavid E. O'Brien 34db57feb7SJonathan Lemon /* 35db57feb7SJonathan Lemon * Generic driver for Compaq SMART RAID adapters. 36db57feb7SJonathan Lemon */ 37db57feb7SJonathan Lemon 38db57feb7SJonathan Lemon #include <sys/param.h> 3955d782fcSJonathan Lemon #include <sys/kernel.h> 40db57feb7SJonathan Lemon #include <sys/systm.h> 41db57feb7SJonathan Lemon #include <sys/malloc.h> 42db57feb7SJonathan Lemon 439626b608SPoul-Henning Kamp #include <sys/bio.h> 44db57feb7SJonathan Lemon #include <sys/bus.h> 45e5dc8339SPoul-Henning Kamp #include <sys/conf.h> 46db57feb7SJonathan Lemon 47db57feb7SJonathan Lemon #include <machine/bus_memio.h> 48db57feb7SJonathan Lemon #include <machine/bus_pio.h> 49db57feb7SJonathan Lemon #include <machine/bus.h> 50db57feb7SJonathan Lemon #include <sys/rman.h> 51db57feb7SJonathan Lemon 52891619a6SPoul-Henning Kamp #include <geom/geom_disk.h> 53891619a6SPoul-Henning Kamp 54db57feb7SJonathan Lemon #include <dev/ida/idareg.h> 55db57feb7SJonathan Lemon #include <dev/ida/idavar.h> 56db57feb7SJonathan Lemon 57db57feb7SJonathan Lemon /* prototypes */ 58db57feb7SJonathan Lemon static void ida_alloc_qcb(struct ida_softc *ida); 59db57feb7SJonathan Lemon static void ida_construct_qcb(struct ida_softc *ida); 60db57feb7SJonathan Lemon static void ida_start(struct ida_softc *ida); 61db57feb7SJonathan Lemon static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb); 6255d782fcSJonathan Lemon static int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb); 63db57feb7SJonathan Lemon 64db57feb7SJonathan Lemon void 65db57feb7SJonathan Lemon ida_free(struct ida_softc *ida) 66db57feb7SJonathan Lemon { 67ee7eb00eSJonathan Lemon int i; 68db57feb7SJonathan Lemon 69ee7eb00eSJonathan Lemon for (i = 0; i < ida->num_qcbs; i++) 70ee7eb00eSJonathan Lemon bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap); 71db57feb7SJonathan Lemon 72db57feb7SJonathan Lemon if (ida->hwqcb_busaddr) 73db57feb7SJonathan Lemon bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap); 74db57feb7SJonathan Lemon 75db57feb7SJonathan Lemon if (ida->hwqcbs) 76db57feb7SJonathan Lemon bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs, 77db57feb7SJonathan Lemon ida->hwqcb_dmamap); 78db57feb7SJonathan Lemon 79db57feb7SJonathan Lemon if (ida->buffer_dmat) 80db57feb7SJonathan Lemon bus_dma_tag_destroy(ida->buffer_dmat); 81db57feb7SJonathan Lemon 82db57feb7SJonathan Lemon if (ida->hwqcb_dmat) 83db57feb7SJonathan Lemon bus_dma_tag_destroy(ida->hwqcb_dmat); 84db57feb7SJonathan Lemon 85db57feb7SJonathan Lemon if (ida->qcbs != NULL) 86db57feb7SJonathan Lemon free(ida->qcbs, M_DEVBUF); 87db57feb7SJonathan Lemon 88db57feb7SJonathan Lemon if (ida->ih != NULL) 89db57feb7SJonathan Lemon bus_teardown_intr(ida->dev, ida->irq, ida->ih); 90db57feb7SJonathan Lemon 91db57feb7SJonathan Lemon if (ida->irq != NULL) 92db57feb7SJonathan Lemon bus_release_resource(ida->dev, ida->irq_res_type, 93db57feb7SJonathan Lemon 0, ida->irq); 94db57feb7SJonathan Lemon 95db57feb7SJonathan Lemon if (ida->parent_dmat != NULL) 96db57feb7SJonathan Lemon bus_dma_tag_destroy(ida->parent_dmat); 97db57feb7SJonathan Lemon 98db57feb7SJonathan Lemon if (ida->regs != NULL) 99db57feb7SJonathan Lemon bus_release_resource(ida->dev, ida->regs_res_type, 100db57feb7SJonathan Lemon ida->regs_res_id, ida->regs); 101db57feb7SJonathan Lemon } 102db57feb7SJonathan Lemon 103db57feb7SJonathan Lemon /* 104db57feb7SJonathan Lemon * record bus address from bus_dmamap_load 105db57feb7SJonathan Lemon */ 106db57feb7SJonathan Lemon static void 107db57feb7SJonathan Lemon ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 108db57feb7SJonathan Lemon { 109db57feb7SJonathan Lemon bus_addr_t *baddr; 110db57feb7SJonathan Lemon 111db57feb7SJonathan Lemon baddr = (bus_addr_t *)arg; 112db57feb7SJonathan Lemon *baddr = segs->ds_addr; 113db57feb7SJonathan Lemon } 114db57feb7SJonathan Lemon 115db57feb7SJonathan Lemon static __inline struct ida_qcb * 116db57feb7SJonathan Lemon ida_get_qcb(struct ida_softc *ida) 117db57feb7SJonathan Lemon { 118db57feb7SJonathan Lemon struct ida_qcb *qcb; 119db57feb7SJonathan Lemon 120db57feb7SJonathan Lemon if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) { 121db57feb7SJonathan Lemon SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 122db57feb7SJonathan Lemon } else { 123db57feb7SJonathan Lemon ida_alloc_qcb(ida); 124db57feb7SJonathan Lemon if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) 125db57feb7SJonathan Lemon SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 126db57feb7SJonathan Lemon } 127db57feb7SJonathan Lemon return (qcb); 128db57feb7SJonathan Lemon } 129db57feb7SJonathan Lemon 130ee7eb00eSJonathan Lemon static __inline bus_addr_t 131ee7eb00eSJonathan Lemon idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb) 132ee7eb00eSJonathan Lemon { 133ee7eb00eSJonathan Lemon return (ida->hwqcb_busaddr + 134ee7eb00eSJonathan Lemon ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs)); 135ee7eb00eSJonathan Lemon } 136ee7eb00eSJonathan Lemon 137ee7eb00eSJonathan Lemon static __inline struct ida_qcb * 138ee7eb00eSJonathan Lemon idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr) 139ee7eb00eSJonathan Lemon { 140ee7eb00eSJonathan Lemon struct ida_hardware_qcb *hwqcb; 141ee7eb00eSJonathan Lemon 142ee7eb00eSJonathan Lemon hwqcb = (struct ida_hardware_qcb *) 143ee7eb00eSJonathan Lemon ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr)); 144ee7eb00eSJonathan Lemon return (hwqcb->qcb); 145ee7eb00eSJonathan Lemon } 146ee7eb00eSJonathan Lemon 147db57feb7SJonathan Lemon /* 148db57feb7SJonathan Lemon * XXX 149db57feb7SJonathan Lemon * since we allocate all QCB space up front during initialization, then 150db57feb7SJonathan Lemon * why bother with this routine? 151db57feb7SJonathan Lemon */ 152db57feb7SJonathan Lemon static void 153db57feb7SJonathan Lemon ida_alloc_qcb(struct ida_softc *ida) 154db57feb7SJonathan Lemon { 155db57feb7SJonathan Lemon struct ida_qcb *qcb; 156db57feb7SJonathan Lemon int error; 157db57feb7SJonathan Lemon 158db57feb7SJonathan Lemon if (ida->num_qcbs >= IDA_QCB_MAX) 159db57feb7SJonathan Lemon return; 160db57feb7SJonathan Lemon 161db57feb7SJonathan Lemon qcb = &ida->qcbs[ida->num_qcbs]; 162db57feb7SJonathan Lemon 163db57feb7SJonathan Lemon error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap); 164db57feb7SJonathan Lemon if (error != 0) 165db57feb7SJonathan Lemon return; 166db57feb7SJonathan Lemon 167db57feb7SJonathan Lemon qcb->flags = QCB_FREE; 168db57feb7SJonathan Lemon qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs]; 169db57feb7SJonathan Lemon qcb->hwqcb->qcb = qcb; 170ee7eb00eSJonathan Lemon qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb); 171db57feb7SJonathan Lemon SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 172db57feb7SJonathan Lemon ida->num_qcbs++; 173db57feb7SJonathan Lemon } 174db57feb7SJonathan Lemon 175db57feb7SJonathan Lemon int 176db57feb7SJonathan Lemon ida_init(struct ida_softc *ida) 177db57feb7SJonathan Lemon { 178db57feb7SJonathan Lemon int error; 179db57feb7SJonathan Lemon 180db57feb7SJonathan Lemon ida->unit = device_get_unit(ida->dev); 181db57feb7SJonathan Lemon ida->tag = rman_get_bustag(ida->regs); 182db57feb7SJonathan Lemon ida->bsh = rman_get_bushandle(ida->regs); 183db57feb7SJonathan Lemon 184db57feb7SJonathan Lemon SLIST_INIT(&ida->free_qcbs); 185db57feb7SJonathan Lemon STAILQ_INIT(&ida->qcb_queue); 1868177437dSPoul-Henning Kamp bioq_init(&ida->bio_queue); 187db57feb7SJonathan Lemon 188db57feb7SJonathan Lemon ida->qcbs = (struct ida_qcb *) 1897cc0979fSDavid Malone malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF, 1907cc0979fSDavid Malone M_NOWAIT | M_ZERO); 191db57feb7SJonathan Lemon if (ida->qcbs == NULL) 192db57feb7SJonathan Lemon return (ENOMEM); 193db57feb7SJonathan Lemon 194db57feb7SJonathan Lemon /* 195db57feb7SJonathan Lemon * Create our DMA tags 196db57feb7SJonathan Lemon */ 197db57feb7SJonathan Lemon 198db57feb7SJonathan Lemon /* DMA tag for our hardware QCB structures */ 199db57feb7SJonathan Lemon error = bus_dma_tag_create(ida->parent_dmat, 200086646f7SJustin T. Gibbs /*alignment*/1, /*boundary*/0, 201db57feb7SJonathan Lemon /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 202db57feb7SJonathan Lemon /*filter*/NULL, /*filterarg*/NULL, 203db57feb7SJonathan Lemon IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 204db57feb7SJonathan Lemon /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 205f6b1c44dSScott Long /*flags*/0, /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant, 206f6b1c44dSScott Long &ida->hwqcb_dmat); 207db57feb7SJonathan Lemon if (error) 208db57feb7SJonathan Lemon return (ENOMEM); 209db57feb7SJonathan Lemon 210db57feb7SJonathan Lemon /* DMA tag for mapping buffers into device space */ 211db57feb7SJonathan Lemon error = bus_dma_tag_create(ida->parent_dmat, 212086646f7SJustin T. Gibbs /*alignment*/1, /*boundary*/0, 213db57feb7SJonathan Lemon /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 214db57feb7SJonathan Lemon /*filter*/NULL, /*filterarg*/NULL, 215db57feb7SJonathan Lemon /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG, 216f6b1c44dSScott Long /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, 217f6b1c44dSScott Long /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant, &ida->buffer_dmat); 218db57feb7SJonathan Lemon if (error) 219db57feb7SJonathan Lemon return (ENOMEM); 220db57feb7SJonathan Lemon 221db57feb7SJonathan Lemon /* Allocation of hardware QCBs */ 222db57feb7SJonathan Lemon /* XXX allocation is rounded to hardware page size */ 223db57feb7SJonathan Lemon error = bus_dmamem_alloc(ida->hwqcb_dmat, 224db57feb7SJonathan Lemon (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap); 225db57feb7SJonathan Lemon if (error) 226db57feb7SJonathan Lemon return (ENOMEM); 227db57feb7SJonathan Lemon 228db57feb7SJonathan Lemon /* And permanently map them in */ 229db57feb7SJonathan Lemon bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap, 230db57feb7SJonathan Lemon ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 231db57feb7SJonathan Lemon ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0); 232db57feb7SJonathan Lemon 233db57feb7SJonathan Lemon bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb)); 234db57feb7SJonathan Lemon 235db57feb7SJonathan Lemon ida_alloc_qcb(ida); /* allocate an initial qcb */ 236db57feb7SJonathan Lemon 237db57feb7SJonathan Lemon return (0); 238db57feb7SJonathan Lemon } 239db57feb7SJonathan Lemon 240db57feb7SJonathan Lemon void 241db57feb7SJonathan Lemon ida_attach(struct ida_softc *ida) 242db57feb7SJonathan Lemon { 243db57feb7SJonathan Lemon struct ida_controller_info cinfo; 244db57feb7SJonathan Lemon int error, i; 245db57feb7SJonathan Lemon 246ee7eb00eSJonathan Lemon ida->cmd.int_enable(ida, 0); 247db57feb7SJonathan Lemon 248db57feb7SJonathan Lemon error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 24955d782fcSJonathan Lemon IDA_CONTROLLER, 0, DMA_DATA_IN); 250db57feb7SJonathan Lemon if (error) { 251db57feb7SJonathan Lemon device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n"); 252db57feb7SJonathan Lemon return; 253db57feb7SJonathan Lemon } 254db57feb7SJonathan Lemon 255db57feb7SJonathan Lemon device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n", 256db57feb7SJonathan Lemon cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1], 257db57feb7SJonathan Lemon cinfo.firm_rev[2], cinfo.firm_rev[3]); 258db57feb7SJonathan Lemon 2591277c3baSJonathan Lemon if (ida->flags & IDA_FIRMWARE) { 2601277c3baSJonathan Lemon int data; 261db57feb7SJonathan Lemon 2621277c3baSJonathan Lemon error = ida_command(ida, CMD_START_FIRMWARE, 26355d782fcSJonathan Lemon &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN); 2641277c3baSJonathan Lemon if (error) { 2651277c3baSJonathan Lemon device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n"); 2661277c3baSJonathan Lemon return; 2671277c3baSJonathan Lemon } 2681277c3baSJonathan Lemon } 2691277c3baSJonathan Lemon 2701277c3baSJonathan Lemon ida->num_drives = 0; 2711277c3baSJonathan Lemon for (i = 0; i < cinfo.num_drvs; i++) 272ee7eb00eSJonathan Lemon device_add_child(ida->dev, /*"idad"*/NULL, -1); 273db57feb7SJonathan Lemon 274db57feb7SJonathan Lemon bus_generic_attach(ida->dev); 275db57feb7SJonathan Lemon 276ee7eb00eSJonathan Lemon ida->cmd.int_enable(ida, 1); 277ee7eb00eSJonathan Lemon } 278ee7eb00eSJonathan Lemon 279ee7eb00eSJonathan Lemon int 280ee7eb00eSJonathan Lemon ida_detach(device_t dev) 281ee7eb00eSJonathan Lemon { 282ee7eb00eSJonathan Lemon struct ida_softc *ida; 283ee7eb00eSJonathan Lemon int error = 0; 284ee7eb00eSJonathan Lemon 285ee7eb00eSJonathan Lemon ida = (struct ida_softc *)device_get_softc(dev); 286ee7eb00eSJonathan Lemon 287ee7eb00eSJonathan Lemon /* 288ee7eb00eSJonathan Lemon * XXX 289ee7eb00eSJonathan Lemon * before detaching, we must make sure that the system is 290ee7eb00eSJonathan Lemon * quiescent; nothing mounted, no pending activity. 291ee7eb00eSJonathan Lemon */ 292ee7eb00eSJonathan Lemon 293ee7eb00eSJonathan Lemon /* 294ee7eb00eSJonathan Lemon * XXX 295ee7eb00eSJonathan Lemon * now, how are we supposed to maintain a list of our drives? 296ee7eb00eSJonathan Lemon * iterate over our "child devices"? 297ee7eb00eSJonathan Lemon */ 298ee7eb00eSJonathan Lemon 299ee7eb00eSJonathan Lemon 300ee7eb00eSJonathan Lemon ida_free(ida); 301ee7eb00eSJonathan Lemon return (error); 302db57feb7SJonathan Lemon } 303db57feb7SJonathan Lemon 304db57feb7SJonathan Lemon static void 305db57feb7SJonathan Lemon ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 306db57feb7SJonathan Lemon { 307db57feb7SJonathan Lemon struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg; 308db57feb7SJonathan Lemon int i; 309db57feb7SJonathan Lemon 310db57feb7SJonathan Lemon hwqcb->hdr.size = (sizeof(struct ida_req) + 311db57feb7SJonathan Lemon sizeof(struct ida_sgb) * IDA_NSEG) >> 2; 312db57feb7SJonathan Lemon 313db57feb7SJonathan Lemon for (i = 0; i < nsegments; i++) { 314db57feb7SJonathan Lemon hwqcb->seg[i].addr = segs[i].ds_addr; 315db57feb7SJonathan Lemon hwqcb->seg[i].length = segs[i].ds_len; 316db57feb7SJonathan Lemon } 317db57feb7SJonathan Lemon hwqcb->req.sgcount = nsegments; 318db57feb7SJonathan Lemon } 319db57feb7SJonathan Lemon 320db57feb7SJonathan Lemon int 321db57feb7SJonathan Lemon ida_command(struct ida_softc *ida, int command, void *data, int datasize, 32255d782fcSJonathan Lemon int drive, u_int32_t pblkno, int flags) 323db57feb7SJonathan Lemon { 324db57feb7SJonathan Lemon struct ida_hardware_qcb *hwqcb; 325db57feb7SJonathan Lemon struct ida_qcb *qcb; 3267e71df93SScott Long bus_dmasync_op_t op; 32755d782fcSJonathan Lemon int s, error; 328db57feb7SJonathan Lemon 329db57feb7SJonathan Lemon s = splbio(); 330db57feb7SJonathan Lemon qcb = ida_get_qcb(ida); 331db57feb7SJonathan Lemon splx(s); 332db57feb7SJonathan Lemon 333db57feb7SJonathan Lemon if (qcb == NULL) { 334db57feb7SJonathan Lemon printf("ida_command: out of QCBs"); 33555d782fcSJonathan Lemon return (EAGAIN); 336db57feb7SJonathan Lemon } 337db57feb7SJonathan Lemon 338db57feb7SJonathan Lemon hwqcb = qcb->hwqcb; 339db57feb7SJonathan Lemon bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 340db57feb7SJonathan Lemon 341db57feb7SJonathan Lemon bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 342db57feb7SJonathan Lemon (void *)data, datasize, ida_setup_dmamap, hwqcb, 0); 343db57feb7SJonathan Lemon op = qcb->flags & DMA_DATA_IN ? 344db57feb7SJonathan Lemon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 345db57feb7SJonathan Lemon bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 346db57feb7SJonathan Lemon 3471277c3baSJonathan Lemon hwqcb->hdr.drive = drive; 34855d782fcSJonathan Lemon hwqcb->req.blkno = pblkno; 349db57feb7SJonathan Lemon hwqcb->req.bcount = howmany(datasize, DEV_BSIZE); 350db57feb7SJonathan Lemon hwqcb->req.command = command; 351db57feb7SJonathan Lemon 352db57feb7SJonathan Lemon qcb->flags = flags | IDA_COMMAND; 353db57feb7SJonathan Lemon 354db57feb7SJonathan Lemon s = splbio(); 355db57feb7SJonathan Lemon STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 356db57feb7SJonathan Lemon ida_start(ida); 35755d782fcSJonathan Lemon error = ida_wait(ida, qcb); 358db57feb7SJonathan Lemon splx(s); 359db57feb7SJonathan Lemon 360db57feb7SJonathan Lemon /* XXX should have status returned here? */ 361db57feb7SJonathan Lemon /* XXX have "status pointer" area in QCB? */ 362db57feb7SJonathan Lemon 36355d782fcSJonathan Lemon return (error); 364db57feb7SJonathan Lemon } 365db57feb7SJonathan Lemon 366db57feb7SJonathan Lemon void 3678177437dSPoul-Henning Kamp ida_submit_buf(struct ida_softc *ida, struct bio *bp) 368db57feb7SJonathan Lemon { 3698177437dSPoul-Henning Kamp bioq_insert_tail(&ida->bio_queue, bp); 370db57feb7SJonathan Lemon ida_construct_qcb(ida); 371db57feb7SJonathan Lemon ida_start(ida); 372db57feb7SJonathan Lemon } 373db57feb7SJonathan Lemon 374db57feb7SJonathan Lemon static void 375db57feb7SJonathan Lemon ida_construct_qcb(struct ida_softc *ida) 376db57feb7SJonathan Lemon { 377db57feb7SJonathan Lemon struct ida_hardware_qcb *hwqcb; 378db57feb7SJonathan Lemon struct ida_qcb *qcb; 3797e71df93SScott Long bus_dmasync_op_t op; 3808177437dSPoul-Henning Kamp struct bio *bp; 381db57feb7SJonathan Lemon 3828177437dSPoul-Henning Kamp bp = bioq_first(&ida->bio_queue); 383db57feb7SJonathan Lemon if (bp == NULL) 384db57feb7SJonathan Lemon return; /* no more buffers */ 385db57feb7SJonathan Lemon 386db57feb7SJonathan Lemon qcb = ida_get_qcb(ida); 387db57feb7SJonathan Lemon if (qcb == NULL) 388db57feb7SJonathan Lemon return; /* out of resources */ 389db57feb7SJonathan Lemon 3908177437dSPoul-Henning Kamp bioq_remove(&ida->bio_queue, bp); 391db57feb7SJonathan Lemon qcb->buf = bp; 39268d2672dSMatthew N. Dodd qcb->flags = bp->bio_cmd == BIO_READ ? DMA_DATA_IN : DMA_DATA_OUT; 393db57feb7SJonathan Lemon 394db57feb7SJonathan Lemon hwqcb = qcb->hwqcb; 395db57feb7SJonathan Lemon bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 396db57feb7SJonathan Lemon 397db57feb7SJonathan Lemon bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 3988177437dSPoul-Henning Kamp (void *)bp->bio_data, bp->bio_bcount, ida_setup_dmamap, hwqcb, 0); 399db57feb7SJonathan Lemon op = qcb->flags & DMA_DATA_IN ? 400db57feb7SJonathan Lemon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 401db57feb7SJonathan Lemon bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 402db57feb7SJonathan Lemon 403db57feb7SJonathan Lemon { 40481530866SMatthew N. Dodd struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1; 4051277c3baSJonathan Lemon hwqcb->hdr.drive = drv->drive; 406db57feb7SJonathan Lemon } 407db57feb7SJonathan Lemon 4088177437dSPoul-Henning Kamp hwqcb->req.blkno = bp->bio_pblkno; 4098177437dSPoul-Henning Kamp hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE); 4108177437dSPoul-Henning Kamp hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE; 411db57feb7SJonathan Lemon 412db57feb7SJonathan Lemon STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 413db57feb7SJonathan Lemon } 414db57feb7SJonathan Lemon 415db57feb7SJonathan Lemon /* 416db57feb7SJonathan Lemon * This routine will be called from ida_intr in order to queue up more 417db57feb7SJonathan Lemon * I/O, meaning that we may be in an interrupt context. Hence, we should 418db57feb7SJonathan Lemon * not muck around with spl() in this routine. 419db57feb7SJonathan Lemon */ 420db57feb7SJonathan Lemon static void 421db57feb7SJonathan Lemon ida_start(struct ida_softc *ida) 422db57feb7SJonathan Lemon { 423db57feb7SJonathan Lemon struct ida_qcb *qcb; 424db57feb7SJonathan Lemon 425db57feb7SJonathan Lemon while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) { 426ee7eb00eSJonathan Lemon if (ida->cmd.fifo_full(ida)) 427ee7eb00eSJonathan Lemon break; 428db57feb7SJonathan Lemon STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe); 429db57feb7SJonathan Lemon /* 430db57feb7SJonathan Lemon * XXX 431db57feb7SJonathan Lemon * place the qcb on an active list and set a timeout? 432db57feb7SJonathan Lemon */ 433db57feb7SJonathan Lemon qcb->state = QCB_ACTIVE; 434ee7eb00eSJonathan Lemon ida->cmd.submit(ida, qcb); 435db57feb7SJonathan Lemon } 436db57feb7SJonathan Lemon } 437db57feb7SJonathan Lemon 43855d782fcSJonathan Lemon static int 43955d782fcSJonathan Lemon ida_wait(struct ida_softc *ida, struct ida_qcb *qcb) 440db57feb7SJonathan Lemon { 441db57feb7SJonathan Lemon struct ida_qcb *qcb_done = NULL; 442db57feb7SJonathan Lemon bus_addr_t completed; 44355d782fcSJonathan Lemon int delay; 444db57feb7SJonathan Lemon 44555d782fcSJonathan Lemon if (ida->flags & IDA_INTERRUPTS) { 446521f364bSDag-Erling Smørgrav if (tsleep(qcb, PRIBIO, "idacmd", 5 * hz)) 44755d782fcSJonathan Lemon return (ETIMEDOUT); 44855d782fcSJonathan Lemon return (0); 449db57feb7SJonathan Lemon } 450db57feb7SJonathan Lemon 45155d782fcSJonathan Lemon again: 45255d782fcSJonathan Lemon delay = 5 * 1000 * 100; /* 5 sec delay */ 453ee7eb00eSJonathan Lemon while ((completed = ida->cmd.done(ida)) == 0) { 454db57feb7SJonathan Lemon if (delay-- == 0) 45555d782fcSJonathan Lemon return (ETIMEDOUT); 456db57feb7SJonathan Lemon DELAY(10); 457db57feb7SJonathan Lemon } 458db57feb7SJonathan Lemon 459db57feb7SJonathan Lemon qcb_done = idahwqcbptov(ida, completed & ~3); 460db57feb7SJonathan Lemon if (qcb_done != qcb) 46155d782fcSJonathan Lemon goto again; 462db57feb7SJonathan Lemon ida_done(ida, qcb); 46355d782fcSJonathan Lemon return (0); 464db57feb7SJonathan Lemon } 465db57feb7SJonathan Lemon 466db57feb7SJonathan Lemon void 467db57feb7SJonathan Lemon ida_intr(void *data) 468db57feb7SJonathan Lemon { 469db57feb7SJonathan Lemon struct ida_softc *ida; 470db57feb7SJonathan Lemon struct ida_qcb *qcb; 471db57feb7SJonathan Lemon bus_addr_t completed; 472db57feb7SJonathan Lemon 473db57feb7SJonathan Lemon ida = (struct ida_softc *)data; 474db57feb7SJonathan Lemon 475ee7eb00eSJonathan Lemon if (ida->cmd.int_pending(ida) == 0) 476db57feb7SJonathan Lemon return; /* not our interrupt */ 477db57feb7SJonathan Lemon 478ee7eb00eSJonathan Lemon while ((completed = ida->cmd.done(ida)) != 0) { 479db57feb7SJonathan Lemon qcb = idahwqcbptov(ida, completed & ~3); 480db57feb7SJonathan Lemon 481db57feb7SJonathan Lemon if (qcb == NULL || qcb->state != QCB_ACTIVE) { 482db57feb7SJonathan Lemon device_printf(ida->dev, 483482a5259SJohn Baldwin "ignoring completion %jx\n", (intmax_t)completed); 484db57feb7SJonathan Lemon continue; 485db57feb7SJonathan Lemon } 486a14b52feSMatthew N. Dodd /* Handle "Bad Command List" errors. */ 487a14b52feSMatthew N. Dodd if ((completed & 3) && (qcb->hwqcb->req.error == 0)) 488a14b52feSMatthew N. Dodd qcb->hwqcb->req.error = CMD_REJECTED; 489db57feb7SJonathan Lemon ida_done(ida, qcb); 490db57feb7SJonathan Lemon } 491db57feb7SJonathan Lemon ida_start(ida); 492db57feb7SJonathan Lemon } 493db57feb7SJonathan Lemon 494db57feb7SJonathan Lemon /* 495db57feb7SJonathan Lemon * should switch out command type; may be status, not just I/O. 496db57feb7SJonathan Lemon */ 497db57feb7SJonathan Lemon static void 498db57feb7SJonathan Lemon ida_done(struct ida_softc *ida, struct ida_qcb *qcb) 499db57feb7SJonathan Lemon { 500db57feb7SJonathan Lemon int error = 0; 501db57feb7SJonathan Lemon 502db57feb7SJonathan Lemon /* 503db57feb7SJonathan Lemon * finish up command 504db57feb7SJonathan Lemon */ 505db57feb7SJonathan Lemon if (qcb->flags & DMA_DATA_TRANSFER) { 5067e71df93SScott Long bus_dmasync_op_t op; 507db57feb7SJonathan Lemon 508db57feb7SJonathan Lemon op = qcb->flags & DMA_DATA_IN ? 509db57feb7SJonathan Lemon BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE; 510db57feb7SJonathan Lemon bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 511db57feb7SJonathan Lemon bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap); 512db57feb7SJonathan Lemon } 513db57feb7SJonathan Lemon 51468d2672dSMatthew N. Dodd if (qcb->hwqcb->req.error & SOFT_ERROR) { 51568d2672dSMatthew N. Dodd if (qcb->buf) 51668d2672dSMatthew N. Dodd device_printf(ida->dev, "soft %s error\n", 51768d2672dSMatthew N. Dodd qcb->buf->bio_cmd == BIO_READ ? 51868d2672dSMatthew N. Dodd "read" : "write"); 51968d2672dSMatthew N. Dodd else 520db57feb7SJonathan Lemon device_printf(ida->dev, "soft error\n"); 52168d2672dSMatthew N. Dodd } 522db57feb7SJonathan Lemon if (qcb->hwqcb->req.error & HARD_ERROR) { 523db57feb7SJonathan Lemon error = 1; 52468d2672dSMatthew N. Dodd if (qcb->buf) 52568d2672dSMatthew N. Dodd device_printf(ida->dev, "hard %s error\n", 52668d2672dSMatthew N. Dodd qcb->buf->bio_cmd == BIO_READ ? 52768d2672dSMatthew N. Dodd "read" : "write"); 52868d2672dSMatthew N. Dodd else 529db57feb7SJonathan Lemon device_printf(ida->dev, "hard error\n"); 530db57feb7SJonathan Lemon } 531db57feb7SJonathan Lemon if (qcb->hwqcb->req.error & CMD_REJECTED) { 532db57feb7SJonathan Lemon error = 1; 533db57feb7SJonathan Lemon device_printf(ida->dev, "invalid request\n"); 534db57feb7SJonathan Lemon } 535db57feb7SJonathan Lemon 536db57feb7SJonathan Lemon if (qcb->flags & IDA_COMMAND) { 53755d782fcSJonathan Lemon if (ida->flags & IDA_INTERRUPTS) 538db57feb7SJonathan Lemon wakeup(qcb); 539db57feb7SJonathan Lemon } else { 540db57feb7SJonathan Lemon if (error) 5418177437dSPoul-Henning Kamp qcb->buf->bio_flags |= BIO_ERROR; 54281530866SMatthew N. Dodd idad_intr(qcb->buf); 543db57feb7SJonathan Lemon } 544db57feb7SJonathan Lemon 545db57feb7SJonathan Lemon qcb->state = QCB_FREE; 54668d2672dSMatthew N. Dodd qcb->buf = NULL; 547db57feb7SJonathan Lemon SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 548db57feb7SJonathan Lemon ida_construct_qcb(ida); 549db57feb7SJonathan Lemon } 550