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