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> 42a85ce5b6SMatthew N. Dodd #include <sys/stat.h> 43db57feb7SJonathan Lemon 449626b608SPoul-Henning Kamp #include <sys/bio.h> 45db57feb7SJonathan Lemon #include <sys/bus.h> 46e5dc8339SPoul-Henning Kamp #include <sys/conf.h> 47cacc81a6SMatthew N. Dodd #include <sys/endian.h> 48db57feb7SJonathan Lemon 49db57feb7SJonathan Lemon #include <machine/bus_memio.h> 50db57feb7SJonathan Lemon #include <machine/bus_pio.h> 51db57feb7SJonathan Lemon #include <machine/bus.h> 52db57feb7SJonathan Lemon #include <sys/rman.h> 53db57feb7SJonathan Lemon 54891619a6SPoul-Henning Kamp #include <geom/geom_disk.h> 55891619a6SPoul-Henning Kamp 56db57feb7SJonathan Lemon #include <dev/ida/idareg.h> 57db57feb7SJonathan Lemon #include <dev/ida/idavar.h> 58a85ce5b6SMatthew N. Dodd #include <dev/ida/idaio.h> 59db57feb7SJonathan Lemon 60db57feb7SJonathan Lemon /* prototypes */ 61db57feb7SJonathan Lemon static void ida_alloc_qcb(struct ida_softc *ida); 62db57feb7SJonathan Lemon static void ida_construct_qcb(struct ida_softc *ida); 63db57feb7SJonathan Lemon static void ida_start(struct ida_softc *ida); 64db57feb7SJonathan Lemon static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb); 6555d782fcSJonathan Lemon static int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb); 66db57feb7SJonathan Lemon 67a85ce5b6SMatthew N. Dodd static d_ioctl_t ida_ioctl; 68a85ce5b6SMatthew N. Dodd static struct cdevsw ida_cdevsw = { 69dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 70dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 71a85ce5b6SMatthew N. Dodd .d_ioctl = ida_ioctl, 72a85ce5b6SMatthew N. Dodd .d_name = "ida", 73a85ce5b6SMatthew N. Dodd }; 74a85ce5b6SMatthew N. Dodd 75db57feb7SJonathan Lemon void 76db57feb7SJonathan Lemon ida_free(struct ida_softc *ida) 77db57feb7SJonathan Lemon { 78ee7eb00eSJonathan Lemon int i; 79db57feb7SJonathan Lemon 80ee7eb00eSJonathan Lemon for (i = 0; i < ida->num_qcbs; i++) 81ee7eb00eSJonathan Lemon bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap); 82db57feb7SJonathan Lemon 83db57feb7SJonathan Lemon if (ida->hwqcb_busaddr) 84db57feb7SJonathan Lemon bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap); 85db57feb7SJonathan Lemon 86db57feb7SJonathan Lemon if (ida->hwqcbs) 87db57feb7SJonathan Lemon bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs, 88db57feb7SJonathan Lemon ida->hwqcb_dmamap); 89db57feb7SJonathan Lemon 90db57feb7SJonathan Lemon if (ida->buffer_dmat) 91db57feb7SJonathan Lemon bus_dma_tag_destroy(ida->buffer_dmat); 92db57feb7SJonathan Lemon 93db57feb7SJonathan Lemon if (ida->hwqcb_dmat) 94db57feb7SJonathan Lemon bus_dma_tag_destroy(ida->hwqcb_dmat); 95db57feb7SJonathan Lemon 96db57feb7SJonathan Lemon if (ida->qcbs != NULL) 97db57feb7SJonathan Lemon free(ida->qcbs, M_DEVBUF); 98db57feb7SJonathan Lemon 99db57feb7SJonathan Lemon if (ida->ih != NULL) 100db57feb7SJonathan Lemon bus_teardown_intr(ida->dev, ida->irq, ida->ih); 101db57feb7SJonathan Lemon 102db57feb7SJonathan Lemon if (ida->irq != NULL) 103db57feb7SJonathan Lemon bus_release_resource(ida->dev, ida->irq_res_type, 104db57feb7SJonathan Lemon 0, ida->irq); 105db57feb7SJonathan Lemon 106db57feb7SJonathan Lemon if (ida->parent_dmat != NULL) 107db57feb7SJonathan Lemon bus_dma_tag_destroy(ida->parent_dmat); 108db57feb7SJonathan Lemon 109db57feb7SJonathan Lemon if (ida->regs != NULL) 110db57feb7SJonathan Lemon bus_release_resource(ida->dev, ida->regs_res_type, 111db57feb7SJonathan Lemon ida->regs_res_id, ida->regs); 112db57feb7SJonathan Lemon } 113db57feb7SJonathan Lemon 114db57feb7SJonathan Lemon /* 115db57feb7SJonathan Lemon * record bus address from bus_dmamap_load 116db57feb7SJonathan Lemon */ 117db57feb7SJonathan Lemon static void 118db57feb7SJonathan Lemon ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 119db57feb7SJonathan Lemon { 120db57feb7SJonathan Lemon bus_addr_t *baddr; 121db57feb7SJonathan Lemon 122db57feb7SJonathan Lemon baddr = (bus_addr_t *)arg; 123db57feb7SJonathan Lemon *baddr = segs->ds_addr; 124db57feb7SJonathan Lemon } 125db57feb7SJonathan Lemon 126db57feb7SJonathan Lemon static __inline struct ida_qcb * 127db57feb7SJonathan Lemon ida_get_qcb(struct ida_softc *ida) 128db57feb7SJonathan Lemon { 129db57feb7SJonathan Lemon struct ida_qcb *qcb; 130db57feb7SJonathan Lemon 131db57feb7SJonathan Lemon if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) { 132db57feb7SJonathan Lemon SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 133db57feb7SJonathan Lemon } else { 134db57feb7SJonathan Lemon ida_alloc_qcb(ida); 135db57feb7SJonathan Lemon if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) 136db57feb7SJonathan Lemon SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 137db57feb7SJonathan Lemon } 138db57feb7SJonathan Lemon return (qcb); 139db57feb7SJonathan Lemon } 140db57feb7SJonathan Lemon 141ee7eb00eSJonathan Lemon static __inline bus_addr_t 142ee7eb00eSJonathan Lemon idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb) 143ee7eb00eSJonathan Lemon { 144ee7eb00eSJonathan Lemon return (ida->hwqcb_busaddr + 145ee7eb00eSJonathan Lemon ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs)); 146ee7eb00eSJonathan Lemon } 147ee7eb00eSJonathan Lemon 148ee7eb00eSJonathan Lemon static __inline struct ida_qcb * 149ee7eb00eSJonathan Lemon idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr) 150ee7eb00eSJonathan Lemon { 151ee7eb00eSJonathan Lemon struct ida_hardware_qcb *hwqcb; 152ee7eb00eSJonathan Lemon 153ee7eb00eSJonathan Lemon hwqcb = (struct ida_hardware_qcb *) 154ee7eb00eSJonathan Lemon ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr)); 155ee7eb00eSJonathan Lemon return (hwqcb->qcb); 156ee7eb00eSJonathan Lemon } 157ee7eb00eSJonathan Lemon 158db57feb7SJonathan Lemon /* 159db57feb7SJonathan Lemon * XXX 160db57feb7SJonathan Lemon * since we allocate all QCB space up front during initialization, then 161db57feb7SJonathan Lemon * why bother with this routine? 162db57feb7SJonathan Lemon */ 163db57feb7SJonathan Lemon static void 164db57feb7SJonathan Lemon ida_alloc_qcb(struct ida_softc *ida) 165db57feb7SJonathan Lemon { 166db57feb7SJonathan Lemon struct ida_qcb *qcb; 167db57feb7SJonathan Lemon int error; 168db57feb7SJonathan Lemon 169db57feb7SJonathan Lemon if (ida->num_qcbs >= IDA_QCB_MAX) 170db57feb7SJonathan Lemon return; 171db57feb7SJonathan Lemon 172db57feb7SJonathan Lemon qcb = &ida->qcbs[ida->num_qcbs]; 173db57feb7SJonathan Lemon 174db57feb7SJonathan Lemon error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap); 175db57feb7SJonathan Lemon if (error != 0) 176db57feb7SJonathan Lemon return; 177db57feb7SJonathan Lemon 178db57feb7SJonathan Lemon qcb->flags = QCB_FREE; 179db57feb7SJonathan Lemon qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs]; 180db57feb7SJonathan Lemon qcb->hwqcb->qcb = qcb; 181ee7eb00eSJonathan Lemon qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb); 182db57feb7SJonathan Lemon SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 183db57feb7SJonathan Lemon ida->num_qcbs++; 184db57feb7SJonathan Lemon } 185db57feb7SJonathan Lemon 186db57feb7SJonathan Lemon int 187db57feb7SJonathan Lemon ida_init(struct ida_softc *ida) 188db57feb7SJonathan Lemon { 189db57feb7SJonathan Lemon int error; 190db57feb7SJonathan Lemon 191db57feb7SJonathan Lemon ida->unit = device_get_unit(ida->dev); 192db57feb7SJonathan Lemon ida->tag = rman_get_bustag(ida->regs); 193db57feb7SJonathan Lemon ida->bsh = rman_get_bushandle(ida->regs); 194db57feb7SJonathan Lemon 195db57feb7SJonathan Lemon SLIST_INIT(&ida->free_qcbs); 196db57feb7SJonathan Lemon STAILQ_INIT(&ida->qcb_queue); 1978177437dSPoul-Henning Kamp bioq_init(&ida->bio_queue); 198db57feb7SJonathan Lemon 199db57feb7SJonathan Lemon ida->qcbs = (struct ida_qcb *) 2007cc0979fSDavid Malone malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF, 2017cc0979fSDavid Malone M_NOWAIT | M_ZERO); 202db57feb7SJonathan Lemon if (ida->qcbs == NULL) 203db57feb7SJonathan Lemon return (ENOMEM); 204db57feb7SJonathan Lemon 205db57feb7SJonathan Lemon /* 206db57feb7SJonathan Lemon * Create our DMA tags 207db57feb7SJonathan Lemon */ 208db57feb7SJonathan Lemon 209db57feb7SJonathan Lemon /* DMA tag for our hardware QCB structures */ 210db57feb7SJonathan Lemon error = bus_dma_tag_create(ida->parent_dmat, 211086646f7SJustin T. Gibbs /*alignment*/1, /*boundary*/0, 212db57feb7SJonathan Lemon /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 213db57feb7SJonathan Lemon /*filter*/NULL, /*filterarg*/NULL, 214db57feb7SJonathan Lemon IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 215db57feb7SJonathan Lemon /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 216f6b1c44dSScott Long /*flags*/0, /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant, 217f6b1c44dSScott Long &ida->hwqcb_dmat); 218db57feb7SJonathan Lemon if (error) 219db57feb7SJonathan Lemon return (ENOMEM); 220db57feb7SJonathan Lemon 221db57feb7SJonathan Lemon /* DMA tag for mapping buffers into device space */ 222db57feb7SJonathan Lemon error = bus_dma_tag_create(ida->parent_dmat, 223086646f7SJustin T. Gibbs /*alignment*/1, /*boundary*/0, 224db57feb7SJonathan Lemon /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 225db57feb7SJonathan Lemon /*filter*/NULL, /*filterarg*/NULL, 226db57feb7SJonathan Lemon /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG, 227f6b1c44dSScott Long /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, 228f6b1c44dSScott Long /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant, &ida->buffer_dmat); 229db57feb7SJonathan Lemon if (error) 230db57feb7SJonathan Lemon return (ENOMEM); 231db57feb7SJonathan Lemon 232db57feb7SJonathan Lemon /* Allocation of hardware QCBs */ 233db57feb7SJonathan Lemon /* XXX allocation is rounded to hardware page size */ 234db57feb7SJonathan Lemon error = bus_dmamem_alloc(ida->hwqcb_dmat, 235db57feb7SJonathan Lemon (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap); 236db57feb7SJonathan Lemon if (error) 237db57feb7SJonathan Lemon return (ENOMEM); 238db57feb7SJonathan Lemon 239db57feb7SJonathan Lemon /* And permanently map them in */ 240db57feb7SJonathan Lemon bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap, 241db57feb7SJonathan Lemon ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 242db57feb7SJonathan Lemon ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0); 243db57feb7SJonathan Lemon 244db57feb7SJonathan Lemon bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb)); 245db57feb7SJonathan Lemon 246db57feb7SJonathan Lemon ida_alloc_qcb(ida); /* allocate an initial qcb */ 247db57feb7SJonathan Lemon 248db57feb7SJonathan Lemon return (0); 249db57feb7SJonathan Lemon } 250db57feb7SJonathan Lemon 251db57feb7SJonathan Lemon void 252db57feb7SJonathan Lemon ida_attach(struct ida_softc *ida) 253db57feb7SJonathan Lemon { 254db57feb7SJonathan Lemon struct ida_controller_info cinfo; 255db57feb7SJonathan Lemon int error, i; 256db57feb7SJonathan Lemon 257ee7eb00eSJonathan Lemon ida->cmd.int_enable(ida, 0); 258db57feb7SJonathan Lemon 259db57feb7SJonathan Lemon error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 26055d782fcSJonathan Lemon IDA_CONTROLLER, 0, DMA_DATA_IN); 261db57feb7SJonathan Lemon if (error) { 262db57feb7SJonathan Lemon device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n"); 263db57feb7SJonathan Lemon return; 264db57feb7SJonathan Lemon } 265db57feb7SJonathan Lemon 266db57feb7SJonathan Lemon device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n", 267db57feb7SJonathan Lemon cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1], 268db57feb7SJonathan Lemon cinfo.firm_rev[2], cinfo.firm_rev[3]); 269db57feb7SJonathan Lemon 2701277c3baSJonathan Lemon if (ida->flags & IDA_FIRMWARE) { 2711277c3baSJonathan Lemon int data; 272db57feb7SJonathan Lemon 2731277c3baSJonathan Lemon error = ida_command(ida, CMD_START_FIRMWARE, 27455d782fcSJonathan Lemon &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN); 2751277c3baSJonathan Lemon if (error) { 2761277c3baSJonathan Lemon device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n"); 2771277c3baSJonathan Lemon return; 2781277c3baSJonathan Lemon } 2791277c3baSJonathan Lemon } 2801277c3baSJonathan Lemon 281a85ce5b6SMatthew N. Dodd ida->ida_dev_t = make_dev(&ida_cdevsw, ida->unit, 282a85ce5b6SMatthew N. Dodd UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR, 283a85ce5b6SMatthew N. Dodd "ida%d", ida->unit); 284a85ce5b6SMatthew N. Dodd ida->ida_dev_t->si_drv1 = ida; 285a85ce5b6SMatthew N. Dodd 2861277c3baSJonathan Lemon ida->num_drives = 0; 2871277c3baSJonathan Lemon for (i = 0; i < cinfo.num_drvs; i++) 288ee7eb00eSJonathan Lemon device_add_child(ida->dev, /*"idad"*/NULL, -1); 289db57feb7SJonathan Lemon 290db57feb7SJonathan Lemon bus_generic_attach(ida->dev); 291db57feb7SJonathan Lemon 292ee7eb00eSJonathan Lemon ida->cmd.int_enable(ida, 1); 293ee7eb00eSJonathan Lemon } 294ee7eb00eSJonathan Lemon 295ee7eb00eSJonathan Lemon int 296ee7eb00eSJonathan Lemon ida_detach(device_t dev) 297ee7eb00eSJonathan Lemon { 298ee7eb00eSJonathan Lemon struct ida_softc *ida; 299ee7eb00eSJonathan Lemon int error = 0; 300ee7eb00eSJonathan Lemon 301ee7eb00eSJonathan Lemon ida = (struct ida_softc *)device_get_softc(dev); 302ee7eb00eSJonathan Lemon 303ee7eb00eSJonathan Lemon /* 304ee7eb00eSJonathan Lemon * XXX 305ee7eb00eSJonathan Lemon * before detaching, we must make sure that the system is 306ee7eb00eSJonathan Lemon * quiescent; nothing mounted, no pending activity. 307ee7eb00eSJonathan Lemon */ 308ee7eb00eSJonathan Lemon 309ee7eb00eSJonathan Lemon /* 310ee7eb00eSJonathan Lemon * XXX 311ee7eb00eSJonathan Lemon * now, how are we supposed to maintain a list of our drives? 312ee7eb00eSJonathan Lemon * iterate over our "child devices"? 313ee7eb00eSJonathan Lemon */ 314ee7eb00eSJonathan Lemon 315a85ce5b6SMatthew N. Dodd destroy_dev(ida->ida_dev_t); 316ee7eb00eSJonathan Lemon ida_free(ida); 317ee7eb00eSJonathan Lemon return (error); 318db57feb7SJonathan Lemon } 319db57feb7SJonathan Lemon 320db57feb7SJonathan Lemon static void 321db57feb7SJonathan Lemon ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 322db57feb7SJonathan Lemon { 323db57feb7SJonathan Lemon struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg; 324db57feb7SJonathan Lemon int i; 325db57feb7SJonathan Lemon 326cacc81a6SMatthew N. Dodd hwqcb->hdr.size = htole16((sizeof(struct ida_req) + 327cacc81a6SMatthew N. Dodd sizeof(struct ida_sgb) * IDA_NSEG) >> 2); 328db57feb7SJonathan Lemon 329db57feb7SJonathan Lemon for (i = 0; i < nsegments; i++) { 330cacc81a6SMatthew N. Dodd hwqcb->seg[i].addr = htole32(segs[i].ds_addr); 331cacc81a6SMatthew N. Dodd hwqcb->seg[i].length = htole32(segs[i].ds_len); 332db57feb7SJonathan Lemon } 333db57feb7SJonathan Lemon hwqcb->req.sgcount = nsegments; 334db57feb7SJonathan Lemon } 335db57feb7SJonathan Lemon 336db57feb7SJonathan Lemon int 337db57feb7SJonathan Lemon ida_command(struct ida_softc *ida, int command, void *data, int datasize, 33855d782fcSJonathan Lemon int drive, u_int32_t pblkno, int flags) 339db57feb7SJonathan Lemon { 340db57feb7SJonathan Lemon struct ida_hardware_qcb *hwqcb; 341db57feb7SJonathan Lemon struct ida_qcb *qcb; 3427e71df93SScott Long bus_dmasync_op_t op; 34355d782fcSJonathan Lemon int s, error; 344db57feb7SJonathan Lemon 345db57feb7SJonathan Lemon s = splbio(); 346db57feb7SJonathan Lemon qcb = ida_get_qcb(ida); 347db57feb7SJonathan Lemon splx(s); 348db57feb7SJonathan Lemon 349db57feb7SJonathan Lemon if (qcb == NULL) { 350db57feb7SJonathan Lemon printf("ida_command: out of QCBs"); 35155d782fcSJonathan Lemon return (EAGAIN); 352db57feb7SJonathan Lemon } 353db57feb7SJonathan Lemon 354db57feb7SJonathan Lemon hwqcb = qcb->hwqcb; 355db57feb7SJonathan Lemon bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 356db57feb7SJonathan Lemon 357db57feb7SJonathan Lemon bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 358db57feb7SJonathan Lemon (void *)data, datasize, ida_setup_dmamap, hwqcb, 0); 359db57feb7SJonathan Lemon op = qcb->flags & DMA_DATA_IN ? 360db57feb7SJonathan Lemon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 361db57feb7SJonathan Lemon bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 362db57feb7SJonathan Lemon 3631277c3baSJonathan Lemon hwqcb->hdr.drive = drive; 364cacc81a6SMatthew N. Dodd hwqcb->req.blkno = htole32(pblkno); 365cacc81a6SMatthew N. Dodd hwqcb->req.bcount = htole16(howmany(datasize, DEV_BSIZE)); 366db57feb7SJonathan Lemon hwqcb->req.command = command; 367db57feb7SJonathan Lemon 368db57feb7SJonathan Lemon qcb->flags = flags | IDA_COMMAND; 369db57feb7SJonathan Lemon 370db57feb7SJonathan Lemon s = splbio(); 371db57feb7SJonathan Lemon STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 372db57feb7SJonathan Lemon ida_start(ida); 37355d782fcSJonathan Lemon error = ida_wait(ida, qcb); 374db57feb7SJonathan Lemon splx(s); 375db57feb7SJonathan Lemon 376db57feb7SJonathan Lemon /* XXX should have status returned here? */ 377db57feb7SJonathan Lemon /* XXX have "status pointer" area in QCB? */ 378db57feb7SJonathan Lemon 37955d782fcSJonathan Lemon return (error); 380db57feb7SJonathan Lemon } 381db57feb7SJonathan Lemon 382db57feb7SJonathan Lemon void 3838177437dSPoul-Henning Kamp ida_submit_buf(struct ida_softc *ida, struct bio *bp) 384db57feb7SJonathan Lemon { 3858177437dSPoul-Henning Kamp bioq_insert_tail(&ida->bio_queue, bp); 386db57feb7SJonathan Lemon ida_construct_qcb(ida); 387db57feb7SJonathan Lemon ida_start(ida); 388db57feb7SJonathan Lemon } 389db57feb7SJonathan Lemon 390db57feb7SJonathan Lemon static void 391db57feb7SJonathan Lemon ida_construct_qcb(struct ida_softc *ida) 392db57feb7SJonathan Lemon { 393db57feb7SJonathan Lemon struct ida_hardware_qcb *hwqcb; 394db57feb7SJonathan Lemon struct ida_qcb *qcb; 3957e71df93SScott Long bus_dmasync_op_t op; 3968177437dSPoul-Henning Kamp struct bio *bp; 397db57feb7SJonathan Lemon 3988177437dSPoul-Henning Kamp bp = bioq_first(&ida->bio_queue); 399db57feb7SJonathan Lemon if (bp == NULL) 400db57feb7SJonathan Lemon return; /* no more buffers */ 401db57feb7SJonathan Lemon 402db57feb7SJonathan Lemon qcb = ida_get_qcb(ida); 403db57feb7SJonathan Lemon if (qcb == NULL) 404db57feb7SJonathan Lemon return; /* out of resources */ 405db57feb7SJonathan Lemon 4068177437dSPoul-Henning Kamp bioq_remove(&ida->bio_queue, bp); 407db57feb7SJonathan Lemon qcb->buf = bp; 40868d2672dSMatthew N. Dodd qcb->flags = bp->bio_cmd == BIO_READ ? DMA_DATA_IN : DMA_DATA_OUT; 409db57feb7SJonathan Lemon 410db57feb7SJonathan Lemon hwqcb = qcb->hwqcb; 411db57feb7SJonathan Lemon bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 412db57feb7SJonathan Lemon 413db57feb7SJonathan Lemon bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 4148177437dSPoul-Henning Kamp (void *)bp->bio_data, bp->bio_bcount, ida_setup_dmamap, hwqcb, 0); 415db57feb7SJonathan Lemon op = qcb->flags & DMA_DATA_IN ? 416db57feb7SJonathan Lemon BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 417db57feb7SJonathan Lemon bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 418db57feb7SJonathan Lemon 419db57feb7SJonathan Lemon { 42081530866SMatthew N. Dodd struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1; 4211277c3baSJonathan Lemon hwqcb->hdr.drive = drv->drive; 422db57feb7SJonathan Lemon } 423db57feb7SJonathan Lemon 4248177437dSPoul-Henning Kamp hwqcb->req.blkno = bp->bio_pblkno; 4258177437dSPoul-Henning Kamp hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE); 4268177437dSPoul-Henning Kamp hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE; 427db57feb7SJonathan Lemon 428db57feb7SJonathan Lemon STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 429db57feb7SJonathan Lemon } 430db57feb7SJonathan Lemon 431db57feb7SJonathan Lemon /* 432db57feb7SJonathan Lemon * This routine will be called from ida_intr in order to queue up more 433db57feb7SJonathan Lemon * I/O, meaning that we may be in an interrupt context. Hence, we should 434db57feb7SJonathan Lemon * not muck around with spl() in this routine. 435db57feb7SJonathan Lemon */ 436db57feb7SJonathan Lemon static void 437db57feb7SJonathan Lemon ida_start(struct ida_softc *ida) 438db57feb7SJonathan Lemon { 439db57feb7SJonathan Lemon struct ida_qcb *qcb; 440db57feb7SJonathan Lemon 441db57feb7SJonathan Lemon while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) { 442ee7eb00eSJonathan Lemon if (ida->cmd.fifo_full(ida)) 443ee7eb00eSJonathan Lemon break; 444db57feb7SJonathan Lemon STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe); 445db57feb7SJonathan Lemon /* 446db57feb7SJonathan Lemon * XXX 447db57feb7SJonathan Lemon * place the qcb on an active list and set a timeout? 448db57feb7SJonathan Lemon */ 449db57feb7SJonathan Lemon qcb->state = QCB_ACTIVE; 450ee7eb00eSJonathan Lemon ida->cmd.submit(ida, qcb); 451db57feb7SJonathan Lemon } 452db57feb7SJonathan Lemon } 453db57feb7SJonathan Lemon 45455d782fcSJonathan Lemon static int 45555d782fcSJonathan Lemon ida_wait(struct ida_softc *ida, struct ida_qcb *qcb) 456db57feb7SJonathan Lemon { 457db57feb7SJonathan Lemon struct ida_qcb *qcb_done = NULL; 458db57feb7SJonathan Lemon bus_addr_t completed; 45955d782fcSJonathan Lemon int delay; 460db57feb7SJonathan Lemon 46155d782fcSJonathan Lemon if (ida->flags & IDA_INTERRUPTS) { 462521f364bSDag-Erling Smørgrav if (tsleep(qcb, PRIBIO, "idacmd", 5 * hz)) 46355d782fcSJonathan Lemon return (ETIMEDOUT); 46455d782fcSJonathan Lemon return (0); 465db57feb7SJonathan Lemon } 466db57feb7SJonathan Lemon 46755d782fcSJonathan Lemon again: 46855d782fcSJonathan Lemon delay = 5 * 1000 * 100; /* 5 sec delay */ 469ee7eb00eSJonathan Lemon while ((completed = ida->cmd.done(ida)) == 0) { 470db57feb7SJonathan Lemon if (delay-- == 0) 47155d782fcSJonathan Lemon return (ETIMEDOUT); 472db57feb7SJonathan Lemon DELAY(10); 473db57feb7SJonathan Lemon } 474db57feb7SJonathan Lemon 475db57feb7SJonathan Lemon qcb_done = idahwqcbptov(ida, completed & ~3); 476db57feb7SJonathan Lemon if (qcb_done != qcb) 47755d782fcSJonathan Lemon goto again; 478db57feb7SJonathan Lemon ida_done(ida, qcb); 47955d782fcSJonathan Lemon return (0); 480db57feb7SJonathan Lemon } 481db57feb7SJonathan Lemon 482db57feb7SJonathan Lemon void 483db57feb7SJonathan Lemon ida_intr(void *data) 484db57feb7SJonathan Lemon { 485db57feb7SJonathan Lemon struct ida_softc *ida; 486db57feb7SJonathan Lemon struct ida_qcb *qcb; 487db57feb7SJonathan Lemon bus_addr_t completed; 488db57feb7SJonathan Lemon 489db57feb7SJonathan Lemon ida = (struct ida_softc *)data; 490db57feb7SJonathan Lemon 491ee7eb00eSJonathan Lemon if (ida->cmd.int_pending(ida) == 0) 492db57feb7SJonathan Lemon return; /* not our interrupt */ 493db57feb7SJonathan Lemon 494ee7eb00eSJonathan Lemon while ((completed = ida->cmd.done(ida)) != 0) { 495db57feb7SJonathan Lemon qcb = idahwqcbptov(ida, completed & ~3); 496db57feb7SJonathan Lemon 497db57feb7SJonathan Lemon if (qcb == NULL || qcb->state != QCB_ACTIVE) { 498db57feb7SJonathan Lemon device_printf(ida->dev, 499482a5259SJohn Baldwin "ignoring completion %jx\n", (intmax_t)completed); 500db57feb7SJonathan Lemon continue; 501db57feb7SJonathan Lemon } 502a14b52feSMatthew N. Dodd /* Handle "Bad Command List" errors. */ 503a14b52feSMatthew N. Dodd if ((completed & 3) && (qcb->hwqcb->req.error == 0)) 504a14b52feSMatthew N. Dodd qcb->hwqcb->req.error = CMD_REJECTED; 505db57feb7SJonathan Lemon ida_done(ida, qcb); 506db57feb7SJonathan Lemon } 507db57feb7SJonathan Lemon ida_start(ida); 508db57feb7SJonathan Lemon } 509db57feb7SJonathan Lemon 510db57feb7SJonathan Lemon /* 511db57feb7SJonathan Lemon * should switch out command type; may be status, not just I/O. 512db57feb7SJonathan Lemon */ 513db57feb7SJonathan Lemon static void 514db57feb7SJonathan Lemon ida_done(struct ida_softc *ida, struct ida_qcb *qcb) 515db57feb7SJonathan Lemon { 516db57feb7SJonathan Lemon int error = 0; 517db57feb7SJonathan Lemon 518db57feb7SJonathan Lemon /* 519db57feb7SJonathan Lemon * finish up command 520db57feb7SJonathan Lemon */ 521db57feb7SJonathan Lemon if (qcb->flags & DMA_DATA_TRANSFER) { 5227e71df93SScott Long bus_dmasync_op_t op; 523db57feb7SJonathan Lemon 524db57feb7SJonathan Lemon op = qcb->flags & DMA_DATA_IN ? 525db57feb7SJonathan Lemon BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE; 526db57feb7SJonathan Lemon bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 527db57feb7SJonathan Lemon bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap); 528db57feb7SJonathan Lemon } 529db57feb7SJonathan Lemon 53068d2672dSMatthew N. Dodd if (qcb->hwqcb->req.error & SOFT_ERROR) { 53168d2672dSMatthew N. Dodd if (qcb->buf) 53268d2672dSMatthew N. Dodd device_printf(ida->dev, "soft %s error\n", 53368d2672dSMatthew N. Dodd qcb->buf->bio_cmd == BIO_READ ? 53468d2672dSMatthew N. Dodd "read" : "write"); 53568d2672dSMatthew N. Dodd else 536db57feb7SJonathan Lemon device_printf(ida->dev, "soft error\n"); 53768d2672dSMatthew N. Dodd } 538db57feb7SJonathan Lemon if (qcb->hwqcb->req.error & HARD_ERROR) { 539db57feb7SJonathan Lemon error = 1; 54068d2672dSMatthew N. Dodd if (qcb->buf) 54168d2672dSMatthew N. Dodd device_printf(ida->dev, "hard %s error\n", 54268d2672dSMatthew N. Dodd qcb->buf->bio_cmd == BIO_READ ? 54368d2672dSMatthew N. Dodd "read" : "write"); 54468d2672dSMatthew N. Dodd else 545db57feb7SJonathan Lemon device_printf(ida->dev, "hard error\n"); 546db57feb7SJonathan Lemon } 547db57feb7SJonathan Lemon if (qcb->hwqcb->req.error & CMD_REJECTED) { 548db57feb7SJonathan Lemon error = 1; 549db57feb7SJonathan Lemon device_printf(ida->dev, "invalid request\n"); 550db57feb7SJonathan Lemon } 551db57feb7SJonathan Lemon 552db57feb7SJonathan Lemon if (qcb->flags & IDA_COMMAND) { 55355d782fcSJonathan Lemon if (ida->flags & IDA_INTERRUPTS) 554db57feb7SJonathan Lemon wakeup(qcb); 555db57feb7SJonathan Lemon } else { 556db57feb7SJonathan Lemon if (error) 5578177437dSPoul-Henning Kamp qcb->buf->bio_flags |= BIO_ERROR; 55881530866SMatthew N. Dodd idad_intr(qcb->buf); 559db57feb7SJonathan Lemon } 560db57feb7SJonathan Lemon 561db57feb7SJonathan Lemon qcb->state = QCB_FREE; 56268d2672dSMatthew N. Dodd qcb->buf = NULL; 563db57feb7SJonathan Lemon SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 564db57feb7SJonathan Lemon ida_construct_qcb(ida); 565db57feb7SJonathan Lemon } 566a85ce5b6SMatthew N. Dodd 567a85ce5b6SMatthew N. Dodd /* 568a85ce5b6SMatthew N. Dodd * IOCTL stuff follows. 569a85ce5b6SMatthew N. Dodd */ 570a85ce5b6SMatthew N. Dodd struct cmd_info { 571a85ce5b6SMatthew N. Dodd int cmd; 572a85ce5b6SMatthew N. Dodd int len; 573a85ce5b6SMatthew N. Dodd int flags; 574a85ce5b6SMatthew N. Dodd }; 575a85ce5b6SMatthew N. Dodd static struct cmd_info *ida_cmd_lookup(int); 576a85ce5b6SMatthew N. Dodd 577a85ce5b6SMatthew N. Dodd static int 57889c9c53dSPoul-Henning Kamp ida_ioctl (struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td) 579a85ce5b6SMatthew N. Dodd { 580a85ce5b6SMatthew N. Dodd struct ida_softc *sc; 581a85ce5b6SMatthew N. Dodd struct ida_user_command *uc; 582a85ce5b6SMatthew N. Dodd struct cmd_info *ci; 583a85ce5b6SMatthew N. Dodd int len; 584a85ce5b6SMatthew N. Dodd int flags; 585a85ce5b6SMatthew N. Dodd int error; 586a85ce5b6SMatthew N. Dodd int data; 587a85ce5b6SMatthew N. Dodd void *daddr; 588a85ce5b6SMatthew N. Dodd 589a85ce5b6SMatthew N. Dodd sc = (struct ida_softc *)dev->si_drv1; 590a85ce5b6SMatthew N. Dodd uc = (struct ida_user_command *)addr; 591a85ce5b6SMatthew N. Dodd error = 0; 592a85ce5b6SMatthew N. Dodd 593a85ce5b6SMatthew N. Dodd switch (cmd) { 594a85ce5b6SMatthew N. Dodd case IDAIO_COMMAND: 595a85ce5b6SMatthew N. Dodd ci = ida_cmd_lookup(uc->command); 596a85ce5b6SMatthew N. Dodd if (ci == NULL) { 597a85ce5b6SMatthew N. Dodd error = EINVAL; 598a85ce5b6SMatthew N. Dodd break; 599a85ce5b6SMatthew N. Dodd } 600a85ce5b6SMatthew N. Dodd len = ci->len; 601a85ce5b6SMatthew N. Dodd flags = ci->flags; 602a85ce5b6SMatthew N. Dodd if (len) 603a85ce5b6SMatthew N. Dodd daddr = &uc->d.buf; 604a85ce5b6SMatthew N. Dodd else { 605a85ce5b6SMatthew N. Dodd daddr = &data; 606a85ce5b6SMatthew N. Dodd len = sizeof(data); 607a85ce5b6SMatthew N. Dodd } 608a85ce5b6SMatthew N. Dodd error = ida_command(sc, uc->command, daddr, len, 609a85ce5b6SMatthew N. Dodd uc->drive, uc->blkno, flags); 610a85ce5b6SMatthew N. Dodd break; 611a85ce5b6SMatthew N. Dodd default: 612a85ce5b6SMatthew N. Dodd error = ENOIOCTL; 613a85ce5b6SMatthew N. Dodd break; 614a85ce5b6SMatthew N. Dodd } 615a85ce5b6SMatthew N. Dodd return (error); 616a85ce5b6SMatthew N. Dodd } 617a85ce5b6SMatthew N. Dodd 618a85ce5b6SMatthew N. Dodd static struct cmd_info ci_list[] = { 619a85ce5b6SMatthew N. Dodd { CMD_GET_LOG_DRV_INFO, 620a85ce5b6SMatthew N. Dodd sizeof(struct ida_drive_info), DMA_DATA_IN }, 621a85ce5b6SMatthew N. Dodd { CMD_GET_CTRL_INFO, 622a85ce5b6SMatthew N. Dodd sizeof(struct ida_controller_info), DMA_DATA_IN }, 623a85ce5b6SMatthew N. Dodd { CMD_SENSE_DRV_STATUS, 624a85ce5b6SMatthew N. Dodd sizeof(struct ida_drive_status), DMA_DATA_IN }, 625a85ce5b6SMatthew N. Dodd { CMD_START_RECOVERY, 0, 0 }, 626a85ce5b6SMatthew N. Dodd { CMD_GET_PHYS_DRV_INFO, 627a85ce5b6SMatthew N. Dodd sizeof(struct ida_phys_drv_info), DMA_DATA_TRANSFER }, 628a85ce5b6SMatthew N. Dodd { CMD_BLINK_DRV_LEDS, 629a85ce5b6SMatthew N. Dodd sizeof(struct ida_blink_drv_leds), DMA_DATA_OUT }, 630a85ce5b6SMatthew N. Dodd { CMD_SENSE_DRV_LEDS, 631a85ce5b6SMatthew N. Dodd sizeof(struct ida_blink_drv_leds), DMA_DATA_IN }, 632a85ce5b6SMatthew N. Dodd { CMD_GET_LOG_DRV_EXT, 633a85ce5b6SMatthew N. Dodd sizeof(struct ida_drive_info_ext), DMA_DATA_IN }, 634a85ce5b6SMatthew N. Dodd { CMD_RESET_CTRL, 0, 0 }, 635a85ce5b6SMatthew N. Dodd { CMD_GET_CONFIG, 0, 0 }, 636a85ce5b6SMatthew N. Dodd { CMD_SET_CONFIG, 0, 0 }, 637a85ce5b6SMatthew N. Dodd { CMD_LABEL_LOG_DRV, 638a85ce5b6SMatthew N. Dodd sizeof(struct ida_label_logical), DMA_DATA_OUT }, 639a85ce5b6SMatthew N. Dodd { CMD_SET_SURFACE_DELAY, 0, 0 }, 640a85ce5b6SMatthew N. Dodd { CMD_SENSE_BUS_PARAMS, 0, 0 }, 641a85ce5b6SMatthew N. Dodd { CMD_SENSE_SUBSYS_INFO, 0, 0 }, 642a85ce5b6SMatthew N. Dodd { CMD_SENSE_SURFACE_ATS, 0, 0 }, 643a85ce5b6SMatthew N. Dodd { CMD_PASSTHROUGH, 0, 0 }, 644a85ce5b6SMatthew N. Dodd { CMD_RESET_SCSI_DEV, 0, 0 }, 645a85ce5b6SMatthew N. Dodd { CMD_PAUSE_BG_ACT, 0, 0 }, 646a85ce5b6SMatthew N. Dodd { CMD_RESUME_BG_ACT, 0, 0 }, 647a85ce5b6SMatthew N. Dodd { CMD_START_FIRMWARE, 0, 0 }, 648a85ce5b6SMatthew N. Dodd { CMD_SENSE_DRV_ERR_LOG, 0, 0 }, 649a85ce5b6SMatthew N. Dodd { CMD_START_CPM, 0, 0 }, 650a85ce5b6SMatthew N. Dodd { CMD_SENSE_CP, 0, 0 }, 651a85ce5b6SMatthew N. Dodd { CMD_STOP_CPM, 0, 0 }, 652a85ce5b6SMatthew N. Dodd { CMD_FLUSH_CACHE, 0, 0 }, 653a85ce5b6SMatthew N. Dodd { CMD_ACCEPT_MEDIA_EXCH, 0, 0 }, 654a85ce5b6SMatthew N. Dodd { 0, 0, 0 } 655a85ce5b6SMatthew N. Dodd }; 656a85ce5b6SMatthew N. Dodd 657a85ce5b6SMatthew N. Dodd static struct cmd_info * 658a85ce5b6SMatthew N. Dodd ida_cmd_lookup (int command) 659a85ce5b6SMatthew N. Dodd { 660a85ce5b6SMatthew N. Dodd struct cmd_info *ci; 661a85ce5b6SMatthew N. Dodd 662a85ce5b6SMatthew N. Dodd ci = ci_list; 663a85ce5b6SMatthew N. Dodd while (ci->cmd) { 664a85ce5b6SMatthew N. Dodd if (ci->cmd == command) 665a85ce5b6SMatthew N. Dodd return (ci); 666a85ce5b6SMatthew N. Dodd ci++; 667a85ce5b6SMatthew N. Dodd } 668a85ce5b6SMatthew N. Dodd return (NULL); 669a85ce5b6SMatthew N. Dodd } 670