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