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