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