1 /*- 2 * Copyright (c) 1999,2000 Jonathan Lemon 3 * All rights reserved. 4 * 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. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 17 * 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 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33 /* 34 * Generic driver for Compaq SMART RAID adapters. 35 * 36 * Specific probe routines are in: 37 * pci/ida_pci.c 38 * i386/eisa/ida_eisa.c 39 */ 40 41 #include <pci.h> 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/malloc.h> 46 47 #include <sys/buf.h> 48 #include <sys/bus.h> 49 #include <sys/devicestat.h> 50 #include <sys/disk.h> 51 52 #if NPCI > 0 53 #include <machine/bus_memio.h> 54 #endif 55 #include <machine/bus_pio.h> 56 #include <machine/bus.h> 57 #include <machine/clock.h> 58 #include <sys/rman.h> 59 60 #include <dev/ida/idareg.h> 61 #include <dev/ida/idavar.h> 62 63 /* prototypes */ 64 static void ida_alloc_qcb(struct ida_softc *ida); 65 static void ida_construct_qcb(struct ida_softc *ida); 66 static void ida_start(struct ida_softc *ida); 67 static void ida_done(struct ida_softc *ida, struct ida_qcb *qcb); 68 static void ida_wait(struct ida_softc *ida, struct ida_qcb *qcb, int delay); 69 70 void 71 ida_free(struct ida_softc *ida) 72 { 73 int i; 74 75 for (i = 0; i < ida->num_qcbs; i++) 76 bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap); 77 78 if (ida->hwqcb_busaddr) 79 bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap); 80 81 if (ida->hwqcbs) 82 bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs, 83 ida->hwqcb_dmamap); 84 85 if (ida->buffer_dmat) 86 bus_dma_tag_destroy(ida->buffer_dmat); 87 88 if (ida->hwqcb_dmat) 89 bus_dma_tag_destroy(ida->hwqcb_dmat); 90 91 if (ida->qcbs != NULL) 92 free(ida->qcbs, M_DEVBUF); 93 94 if (ida->ih != NULL) 95 bus_teardown_intr(ida->dev, ida->irq, ida->ih); 96 97 if (ida->irq != NULL) 98 bus_release_resource(ida->dev, ida->irq_res_type, 99 0, ida->irq); 100 101 if (ida->parent_dmat != NULL) 102 bus_dma_tag_destroy(ida->parent_dmat); 103 104 if (ida->regs != NULL) 105 bus_release_resource(ida->dev, ida->regs_res_type, 106 ida->regs_res_id, ida->regs); 107 } 108 109 /* 110 * record bus address from bus_dmamap_load 111 */ 112 static void 113 ida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 114 { 115 bus_addr_t *baddr; 116 117 baddr = (bus_addr_t *)arg; 118 *baddr = segs->ds_addr; 119 } 120 121 static __inline struct ida_qcb * 122 ida_get_qcb(struct ida_softc *ida) 123 { 124 struct ida_qcb *qcb; 125 126 if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) { 127 SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 128 } else { 129 ida_alloc_qcb(ida); 130 if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) 131 SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 132 } 133 return (qcb); 134 } 135 136 static __inline bus_addr_t 137 idahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb) 138 { 139 return (ida->hwqcb_busaddr + 140 ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs)); 141 } 142 143 static __inline struct ida_qcb * 144 idahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr) 145 { 146 struct ida_hardware_qcb *hwqcb; 147 148 hwqcb = (struct ida_hardware_qcb *) 149 ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr)); 150 return (hwqcb->qcb); 151 } 152 153 /* 154 * XXX 155 * since we allocate all QCB space up front during initialization, then 156 * why bother with this routine? 157 */ 158 static void 159 ida_alloc_qcb(struct ida_softc *ida) 160 { 161 struct ida_qcb *qcb; 162 int error; 163 164 if (ida->num_qcbs >= IDA_QCB_MAX) 165 return; 166 167 qcb = &ida->qcbs[ida->num_qcbs]; 168 169 error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap); 170 if (error != 0) 171 return; 172 173 qcb->flags = QCB_FREE; 174 qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs]; 175 qcb->hwqcb->qcb = qcb; 176 qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb); 177 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 178 ida->num_qcbs++; 179 } 180 181 int 182 ida_init(struct ida_softc *ida) 183 { 184 int error; 185 186 ida->unit = device_get_unit(ida->dev); 187 ida->tag = rman_get_bustag(ida->regs); 188 ida->bsh = rman_get_bushandle(ida->regs); 189 190 SLIST_INIT(&ida->free_qcbs); 191 STAILQ_INIT(&ida->qcb_queue); 192 bioq_init(&ida->bio_queue); 193 194 ida->qcbs = (struct ida_qcb *) 195 malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF, M_NOWAIT); 196 if (ida->qcbs == NULL) 197 return (ENOMEM); 198 bzero(ida->qcbs, IDA_QCB_MAX * sizeof(struct ida_qcb)); 199 200 /* 201 * Create our DMA tags 202 */ 203 204 /* DMA tag for our hardware QCB structures */ 205 error = bus_dma_tag_create(ida->parent_dmat, 206 /*alignment*/1, /*boundary*/0, 207 /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 208 /*filter*/NULL, /*filterarg*/NULL, 209 IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 210 /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 211 /*flags*/0, &ida->hwqcb_dmat); 212 if (error) 213 return (ENOMEM); 214 215 /* DMA tag for mapping buffers into device space */ 216 error = bus_dma_tag_create(ida->parent_dmat, 217 /*alignment*/1, /*boundary*/0, 218 /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 219 /*filter*/NULL, /*filterarg*/NULL, 220 /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG, 221 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, &ida->buffer_dmat); 222 if (error) 223 return (ENOMEM); 224 225 /* Allocation of hardware QCBs */ 226 /* XXX allocation is rounded to hardware page size */ 227 error = bus_dmamem_alloc(ida->hwqcb_dmat, 228 (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap); 229 if (error) 230 return (ENOMEM); 231 232 /* And permanently map them in */ 233 bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap, 234 ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 235 ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0); 236 237 bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb)); 238 239 ida_alloc_qcb(ida); /* allocate an initial qcb */ 240 241 return (0); 242 } 243 244 void 245 ida_attach(struct ida_softc *ida) 246 { 247 struct ida_controller_info cinfo; 248 int error, i; 249 250 ida->cmd.int_enable(ida, 0); 251 252 error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 253 IDA_CONTROLLER, DMA_DATA_IN); 254 if (error) { 255 device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n"); 256 return; 257 } 258 259 device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n", 260 cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1], 261 cinfo.firm_rev[2], cinfo.firm_rev[3]); 262 263 ida->num_drives = cinfo.num_drvs; 264 265 for (i = 0; i < ida->num_drives; i++) 266 device_add_child(ida->dev, /*"idad"*/NULL, -1); 267 268 bus_generic_attach(ida->dev); 269 270 ida->cmd.int_enable(ida, 1); 271 } 272 273 int 274 ida_detach(device_t dev) 275 { 276 struct ida_softc *ida; 277 int error = 0; 278 279 ida = (struct ida_softc *)device_get_softc(dev); 280 281 /* 282 * XXX 283 * before detaching, we must make sure that the system is 284 * quiescent; nothing mounted, no pending activity. 285 */ 286 287 /* 288 * XXX 289 * now, how are we supposed to maintain a list of our drives? 290 * iterate over our "child devices"? 291 */ 292 293 294 ida_free(ida); 295 return (error); 296 } 297 298 static void 299 ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 300 { 301 struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg; 302 int i; 303 304 hwqcb->hdr.size = (sizeof(struct ida_req) + 305 sizeof(struct ida_sgb) * IDA_NSEG) >> 2; 306 307 for (i = 0; i < nsegments; i++) { 308 hwqcb->seg[i].addr = segs[i].ds_addr; 309 hwqcb->seg[i].length = segs[i].ds_len; 310 } 311 hwqcb->req.sgcount = nsegments; 312 } 313 314 int 315 ida_command(struct ida_softc *ida, int command, void *data, int datasize, 316 int drive, int flags) 317 { 318 struct ida_hardware_qcb *hwqcb; 319 struct ida_qcb *qcb; 320 bus_dmasync_op_t op; 321 int s; 322 323 s = splbio(); 324 qcb = ida_get_qcb(ida); 325 splx(s); 326 327 if (qcb == NULL) { 328 printf("ida_command: out of QCBs"); 329 return (1); 330 } 331 332 hwqcb = qcb->hwqcb; 333 bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 334 335 bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 336 (void *)data, datasize, ida_setup_dmamap, hwqcb, 0); 337 op = qcb->flags & DMA_DATA_IN ? 338 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 339 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 340 341 hwqcb->hdr.drive = drive; /* XXX */ 342 hwqcb->req.bcount = howmany(datasize, DEV_BSIZE); 343 hwqcb->req.command = command; 344 345 qcb->flags = flags | IDA_COMMAND; 346 347 s = splbio(); 348 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 349 ida_start(ida); 350 ida_wait(ida, qcb, 500); 351 splx(s); 352 353 /* XXX should have status returned here? */ 354 /* XXX have "status pointer" area in QCB? */ 355 356 return (0); 357 } 358 359 void 360 ida_submit_buf(struct ida_softc *ida, struct bio *bp) 361 { 362 bioq_insert_tail(&ida->bio_queue, bp); 363 ida_construct_qcb(ida); 364 ida_start(ida); 365 } 366 367 static void 368 ida_construct_qcb(struct ida_softc *ida) 369 { 370 struct ida_hardware_qcb *hwqcb; 371 struct ida_qcb *qcb; 372 bus_dmasync_op_t op; 373 struct bio *bp; 374 375 bp = bioq_first(&ida->bio_queue); 376 if (bp == NULL) 377 return; /* no more buffers */ 378 379 qcb = ida_get_qcb(ida); 380 if (qcb == NULL) 381 return; /* out of resources */ 382 383 bioq_remove(&ida->bio_queue, bp); 384 qcb->buf = bp; 385 qcb->flags = 0; 386 387 hwqcb = qcb->hwqcb; 388 bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 389 390 bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 391 (void *)bp->bio_data, bp->bio_bcount, ida_setup_dmamap, hwqcb, 0); 392 op = qcb->flags & DMA_DATA_IN ? 393 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 394 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 395 396 /* 397 * XXX 398 */ 399 { 400 struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1; 401 hwqcb->hdr.drive = drv->unit; 402 } 403 404 hwqcb->req.blkno = bp->bio_pblkno; 405 hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE); 406 hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE; 407 408 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 409 } 410 411 /* 412 * This routine will be called from ida_intr in order to queue up more 413 * I/O, meaning that we may be in an interrupt context. Hence, we should 414 * not muck around with spl() in this routine. 415 */ 416 static void 417 ida_start(struct ida_softc *ida) 418 { 419 struct ida_qcb *qcb; 420 421 while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) { 422 if (ida->cmd.fifo_full(ida)) 423 break; 424 STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe); 425 /* 426 * XXX 427 * place the qcb on an active list and set a timeout? 428 */ 429 qcb->state = QCB_ACTIVE; 430 ida->cmd.submit(ida, qcb); 431 } 432 } 433 434 static 435 void 436 ida_wait(struct ida_softc *ida, struct ida_qcb *qcb, int delay) 437 { 438 struct ida_qcb *qcb_done = NULL; 439 bus_addr_t completed; 440 441 if (ida->flags & IDA_ATTACHED) { 442 if (tsleep((caddr_t)qcb, PRIBIO, "idacmd", delay)) 443 panic("ida_command: timeout waiting for interrupt"); 444 return; 445 } 446 447 while ((completed = ida->cmd.done(ida)) == 0) { 448 if (delay-- == 0) 449 panic("ida_wait: timeout waiting for completion"); 450 DELAY(10); 451 } 452 453 qcb_done = idahwqcbptov(ida, completed & ~3); 454 if (qcb_done != qcb) 455 panic("ida_wait: incorrect qcb returned"); 456 ida_done(ida, qcb); 457 return; 458 } 459 460 void 461 ida_intr(void *data) 462 { 463 struct ida_softc *ida; 464 struct ida_qcb *qcb; 465 bus_addr_t completed; 466 467 ida = (struct ida_softc *)data; 468 469 if (ida->cmd.int_pending(ida) == 0) 470 return; /* not our interrupt */ 471 472 while ((completed = ida->cmd.done(ida)) != 0) { 473 qcb = idahwqcbptov(ida, completed & ~3); 474 475 if (qcb == NULL || qcb->state != QCB_ACTIVE) { 476 device_printf(ida->dev, 477 "ignoring completion %x\n", completed); 478 continue; 479 } 480 ida_done(ida, qcb); 481 } 482 ida_start(ida); 483 } 484 485 /* 486 * should switch out command type; may be status, not just I/O. 487 */ 488 static void 489 ida_done(struct ida_softc *ida, struct ida_qcb *qcb) 490 { 491 int error = 0; 492 493 /* 494 * finish up command 495 */ 496 if (qcb->flags & DMA_DATA_TRANSFER) { 497 bus_dmasync_op_t op; 498 499 op = qcb->flags & DMA_DATA_IN ? 500 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE; 501 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 502 bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap); 503 } 504 505 if (qcb->hwqcb->req.error & SOFT_ERROR) 506 device_printf(ida->dev, "soft error\n"); 507 if (qcb->hwqcb->req.error & HARD_ERROR) { 508 error = 1; 509 device_printf(ida->dev, "hard error\n"); 510 } 511 if (qcb->hwqcb->req.error & CMD_REJECTED) { 512 error = 1; 513 device_printf(ida->dev, "invalid request\n"); 514 } 515 516 if (qcb->flags & IDA_COMMAND) { 517 if (ida->flags & IDA_ATTACHED) 518 wakeup(qcb); 519 } else { 520 if (error) 521 qcb->buf->bio_flags |= BIO_ERROR; 522 idad_intr(qcb->buf); 523 } 524 525 qcb->state = QCB_FREE; 526 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 527 ida_construct_qcb(ida); 528 } 529