ida.c revision 106591
144165Sjulian/*- 244165Sjulian * Copyright (c) 1999,2000 Jonathan Lemon 344165Sjulian * All rights reserved. 444165Sjulian * 544165Sjulian # Derived from the original IDA Compaq RAID driver, which is 644165Sjulian * Copyright (c) 1996, 1997, 1998, 1999 744165Sjulian * Mark Dawson and David James. All rights reserved. 844165Sjulian * 944165Sjulian * Redistribution and use in source and binary forms, with or without 1044165Sjulian * modification, are permitted provided that the following conditions 1144165Sjulian * are met: 1244165Sjulian * 1. Redistributions of source code must retain the above copyright 1344165Sjulian * notice, this list of conditions and the following disclaimer. 1444165Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1544165Sjulian * notice, this list of conditions and the following disclaimer in the 1644165Sjulian * documentation and/or other materials provided with the distribution. 1744165Sjulian * 1844165Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1944165Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2044165Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2144165Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2256896Sfenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2398527Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2498527Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2598527Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2656896Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2744165Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2844165Sjulian * SUCH DAMAGE. 29127675Sbms * 30127675Sbms * $FreeBSD: head/sys/dev/ida/ida.c 106591 2002-11-07 22:23:46Z jhb $ 3144165Sjulian */ 3244165Sjulian 3398527Sfenner/* 3498527Sfenner * Generic driver for Compaq SMART RAID adapters. 3598527Sfenner * 3698527Sfenner * Specific probe routines are in: 37127675Sbms * pci/ida_pci.c 3844165Sjulian * i386/eisa/ida_eisa.c 3998527Sfenner */ 4044165Sjulian 4198527Sfenner#include <sys/param.h> 4244165Sjulian#include <sys/kernel.h> 4344165Sjulian#include <sys/stdint.h> 4444165Sjulian#include <sys/systm.h> 4544165Sjulian#include <sys/malloc.h> 4644165Sjulian 4798527Sfenner#include <sys/bio.h> 4898527Sfenner#include <sys/bus.h> 4944165Sjulian#include <sys/devicestat.h> 5098527Sfenner#include <sys/disk.h> 5144165Sjulian 5298527Sfenner#include <machine/bus_memio.h> 5344165Sjulian#include <machine/bus_pio.h> 5498527Sfenner#include <machine/bus.h> 5598527Sfenner#include <sys/rman.h> 5698527Sfenner 5744165Sjulian#include <dev/ida/idareg.h> 5898527Sfenner#include <dev/ida/idavar.h> 5998527Sfenner 6098527Sfenner/* prototypes */ 6198527Sfennerstatic void ida_alloc_qcb(struct ida_softc *ida); 62127675Sbmsstatic void ida_construct_qcb(struct ida_softc *ida); 6398527Sfennerstatic void ida_start(struct ida_softc *ida); 6498527Sfennerstatic void ida_done(struct ida_softc *ida, struct ida_qcb *qcb); 6598527Sfennerstatic int ida_wait(struct ida_softc *ida, struct ida_qcb *qcb); 6650514Slile 6798527Sfennervoid 6898527Sfennerida_free(struct ida_softc *ida) 6944165Sjulian{ 7098527Sfenner int i; 7198527Sfenner 7298527Sfenner for (i = 0; i < ida->num_qcbs; i++) 7398527Sfenner bus_dmamap_destroy(ida->buffer_dmat, ida->qcbs[i].dmamap); 7498527Sfenner 7598527Sfenner if (ida->hwqcb_busaddr) 7644165Sjulian bus_dmamap_unload(ida->hwqcb_dmat, ida->hwqcb_dmamap); 7798527Sfenner 7844165Sjulian if (ida->hwqcbs) 7944165Sjulian bus_dmamem_free(ida->hwqcb_dmat, ida->hwqcbs, 8098527Sfenner ida->hwqcb_dmamap); 8198527Sfenner 8298527Sfenner if (ida->buffer_dmat) 8398527Sfenner bus_dma_tag_destroy(ida->buffer_dmat); 8498527Sfenner 8598527Sfenner if (ida->hwqcb_dmat) 8698527Sfenner bus_dma_tag_destroy(ida->hwqcb_dmat); 8798527Sfenner 8898527Sfenner if (ida->qcbs != NULL) 8998527Sfenner free(ida->qcbs, M_DEVBUF); 9098527Sfenner 9198527Sfenner if (ida->ih != NULL) 9298527Sfenner bus_teardown_intr(ida->dev, ida->irq, ida->ih); 9398527Sfenner 9498527Sfenner if (ida->irq != NULL) 9598527Sfenner bus_release_resource(ida->dev, ida->irq_res_type, 9698527Sfenner 0, ida->irq); 9798527Sfenner 9898527Sfenner if (ida->parent_dmat != NULL) 9998527Sfenner bus_dma_tag_destroy(ida->parent_dmat); 10098527Sfenner 10198527Sfenner if (ida->regs != NULL) 102127675Sbms bus_release_resource(ida->dev, ida->regs_res_type, 103127675Sbms ida->regs_res_id, ida->regs); 10444165Sjulian} 10598527Sfenner 10656896Sfenner/* 10798527Sfenner * record bus address from bus_dmamap_load 108127675Sbms */ 109127675Sbmsstatic void 11044165Sjulianida_dma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 11198527Sfenner{ 11244165Sjulian bus_addr_t *baddr; 11398527Sfenner 11444165Sjulian baddr = (bus_addr_t *)arg; 115127675Sbms *baddr = segs->ds_addr; 11644165Sjulian} 117127675Sbms 11844165Sjulianstatic __inline struct ida_qcb * 11998527Sfennerida_get_qcb(struct ida_softc *ida) 12098527Sfenner{ 12198527Sfenner struct ida_qcb *qcb; 12244165Sjulian 12398527Sfenner if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) { 12498527Sfenner SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 12598527Sfenner } else { 12698527Sfenner ida_alloc_qcb(ida); 12750514Slile if ((qcb = SLIST_FIRST(&ida->free_qcbs)) != NULL) 12898527Sfenner SLIST_REMOVE_HEAD(&ida->free_qcbs, link.sle); 129127675Sbms } 13050514Slile return (qcb); 13198527Sfenner} 13298527Sfenner 13398527Sfennerstatic __inline bus_addr_t 13498527Sfenneridahwqcbvtop(struct ida_softc *ida, struct ida_hardware_qcb *hwqcb) 135127675Sbms{ 13698527Sfenner return (ida->hwqcb_busaddr + 13798527Sfenner ((bus_addr_t)hwqcb - (bus_addr_t)ida->hwqcbs)); 13898527Sfenner} 13998527Sfenner 14098527Sfennerstatic __inline struct ida_qcb * 141127675Sbmsidahwqcbptov(struct ida_softc *ida, bus_addr_t hwqcb_addr) 14298527Sfenner{ 14398527Sfenner struct ida_hardware_qcb *hwqcb; 14498527Sfenner 14598527Sfenner hwqcb = (struct ida_hardware_qcb *) 14698527Sfenner ((bus_addr_t)ida->hwqcbs + (hwqcb_addr - ida->hwqcb_busaddr)); 14798527Sfenner return (hwqcb->qcb); 148127675Sbms} 14998527Sfenner 15044165Sjulian/* 15198527Sfenner * XXX 152127675Sbms * since we allocate all QCB space up front during initialization, then 153127675Sbms * why bother with this routine? 154127675Sbms */ 155127675Sbmsstatic void 15644165Sjulianida_alloc_qcb(struct ida_softc *ida) 15798527Sfenner{ 15844165Sjulian struct ida_qcb *qcb; 15998527Sfenner int error; 16098527Sfenner 16198527Sfenner if (ida->num_qcbs >= IDA_QCB_MAX) 16298527Sfenner return; 16398527Sfenner 16498527Sfenner qcb = &ida->qcbs[ida->num_qcbs]; 165127675Sbms 16698527Sfenner error = bus_dmamap_create(ida->buffer_dmat, /*flags*/0, &qcb->dmamap); 16798527Sfenner if (error != 0) 16898527Sfenner return; 16998527Sfenner 17098527Sfenner qcb->flags = QCB_FREE; 17198527Sfenner qcb->hwqcb = &ida->hwqcbs[ida->num_qcbs]; 17298527Sfenner qcb->hwqcb->qcb = qcb; 17398527Sfenner qcb->hwqcb_busaddr = idahwqcbvtop(ida, qcb->hwqcb); 17498527Sfenner SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 17598527Sfenner ida->num_qcbs++; 17698527Sfenner} 17798527Sfenner 17844165Sjulianint 179127675Sbmsida_init(struct ida_softc *ida) 18098527Sfenner{ 18144165Sjulian int error; 18244165Sjulian 18344165Sjulian ida->unit = device_get_unit(ida->dev); 184127675Sbms ida->tag = rman_get_bustag(ida->regs); 18544165Sjulian ida->bsh = rman_get_bushandle(ida->regs); 186127675Sbms 187127675Sbms SLIST_INIT(&ida->free_qcbs); 188127675Sbms STAILQ_INIT(&ida->qcb_queue); 189127675Sbms bioq_init(&ida->bio_queue); 190127675Sbms 191127675Sbms ida->qcbs = (struct ida_qcb *) 192127675Sbms malloc(IDA_QCB_MAX * sizeof(struct ida_qcb), M_DEVBUF, 193127675Sbms M_NOWAIT | M_ZERO); 194127675Sbms if (ida->qcbs == NULL) 195127675Sbms return (ENOMEM); 196127675Sbms 197127675Sbms /* 198 * Create our DMA tags 199 */ 200 201 /* DMA tag for our hardware QCB structures */ 202 error = bus_dma_tag_create(ida->parent_dmat, 203 /*alignment*/1, /*boundary*/0, 204 /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 205 /*filter*/NULL, /*filterarg*/NULL, 206 IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 207 /*nsegments*/1, /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, 208 /*flags*/0, &ida->hwqcb_dmat); 209 if (error) 210 return (ENOMEM); 211 212 /* DMA tag for mapping buffers into device space */ 213 error = bus_dma_tag_create(ida->parent_dmat, 214 /*alignment*/1, /*boundary*/0, 215 /*lowaddr*/BUS_SPACE_MAXADDR, /*highaddr*/BUS_SPACE_MAXADDR, 216 /*filter*/NULL, /*filterarg*/NULL, 217 /*maxsize*/MAXBSIZE, /*nsegments*/IDA_NSEG, 218 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0, &ida->buffer_dmat); 219 if (error) 220 return (ENOMEM); 221 222 /* Allocation of hardware QCBs */ 223 /* XXX allocation is rounded to hardware page size */ 224 error = bus_dmamem_alloc(ida->hwqcb_dmat, 225 (void **)&ida->hwqcbs, BUS_DMA_NOWAIT, &ida->hwqcb_dmamap); 226 if (error) 227 return (ENOMEM); 228 229 /* And permanently map them in */ 230 bus_dmamap_load(ida->hwqcb_dmat, ida->hwqcb_dmamap, 231 ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb), 232 ida_dma_map_cb, &ida->hwqcb_busaddr, /*flags*/0); 233 234 bzero(ida->hwqcbs, IDA_QCB_MAX * sizeof(struct ida_hardware_qcb)); 235 236 ida_alloc_qcb(ida); /* allocate an initial qcb */ 237 238 return (0); 239} 240 241void 242ida_attach(struct ida_softc *ida) 243{ 244 struct ida_controller_info cinfo; 245 int error, i; 246 247 ida->cmd.int_enable(ida, 0); 248 249 error = ida_command(ida, CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 250 IDA_CONTROLLER, 0, DMA_DATA_IN); 251 if (error) { 252 device_printf(ida->dev, "CMD_GET_CTRL_INFO failed.\n"); 253 return; 254 } 255 256 device_printf(ida->dev, "drives=%d firm_rev=%c%c%c%c\n", 257 cinfo.num_drvs, cinfo.firm_rev[0], cinfo.firm_rev[1], 258 cinfo.firm_rev[2], cinfo.firm_rev[3]); 259 260 if (ida->flags & IDA_FIRMWARE) { 261 int data; 262 263 error = ida_command(ida, CMD_START_FIRMWARE, 264 &data, sizeof(data), IDA_CONTROLLER, 0, DMA_DATA_IN); 265 if (error) { 266 device_printf(ida->dev, "CMD_START_FIRMWARE failed.\n"); 267 return; 268 } 269 } 270 271 ida->num_drives = 0; 272 for (i = 0; i < cinfo.num_drvs; i++) 273 device_add_child(ida->dev, /*"idad"*/NULL, -1); 274 275 bus_generic_attach(ida->dev); 276 277 ida->cmd.int_enable(ida, 1); 278} 279 280int 281ida_detach(device_t dev) 282{ 283 struct ida_softc *ida; 284 int error = 0; 285 286 ida = (struct ida_softc *)device_get_softc(dev); 287 288 /* 289 * XXX 290 * before detaching, we must make sure that the system is 291 * quiescent; nothing mounted, no pending activity. 292 */ 293 294 /* 295 * XXX 296 * now, how are we supposed to maintain a list of our drives? 297 * iterate over our "child devices"? 298 */ 299 300 301 ida_free(ida); 302 return (error); 303} 304 305static void 306ida_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 307{ 308 struct ida_hardware_qcb *hwqcb = (struct ida_hardware_qcb *)arg; 309 int i; 310 311 hwqcb->hdr.size = (sizeof(struct ida_req) + 312 sizeof(struct ida_sgb) * IDA_NSEG) >> 2; 313 314 for (i = 0; i < nsegments; i++) { 315 hwqcb->seg[i].addr = segs[i].ds_addr; 316 hwqcb->seg[i].length = segs[i].ds_len; 317 } 318 hwqcb->req.sgcount = nsegments; 319} 320 321int 322ida_command(struct ida_softc *ida, int command, void *data, int datasize, 323 int drive, u_int32_t pblkno, int flags) 324{ 325 struct ida_hardware_qcb *hwqcb; 326 struct ida_qcb *qcb; 327 bus_dmasync_op_t op; 328 int s, error; 329 330 s = splbio(); 331 qcb = ida_get_qcb(ida); 332 splx(s); 333 334 if (qcb == NULL) { 335 printf("ida_command: out of QCBs"); 336 return (EAGAIN); 337 } 338 339 hwqcb = qcb->hwqcb; 340 bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 341 342 bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 343 (void *)data, datasize, ida_setup_dmamap, hwqcb, 0); 344 op = qcb->flags & DMA_DATA_IN ? 345 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 346 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 347 348 hwqcb->hdr.drive = drive; 349 hwqcb->req.blkno = pblkno; 350 hwqcb->req.bcount = howmany(datasize, DEV_BSIZE); 351 hwqcb->req.command = command; 352 353 qcb->flags = flags | IDA_COMMAND; 354 355 s = splbio(); 356 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 357 ida_start(ida); 358 error = ida_wait(ida, qcb); 359 splx(s); 360 361 /* XXX should have status returned here? */ 362 /* XXX have "status pointer" area in QCB? */ 363 364 return (error); 365} 366 367void 368ida_submit_buf(struct ida_softc *ida, struct bio *bp) 369{ 370 bioq_insert_tail(&ida->bio_queue, bp); 371 ida_construct_qcb(ida); 372 ida_start(ida); 373} 374 375static void 376ida_construct_qcb(struct ida_softc *ida) 377{ 378 struct ida_hardware_qcb *hwqcb; 379 struct ida_qcb *qcb; 380 bus_dmasync_op_t op; 381 struct bio *bp; 382 383 bp = bioq_first(&ida->bio_queue); 384 if (bp == NULL) 385 return; /* no more buffers */ 386 387 qcb = ida_get_qcb(ida); 388 if (qcb == NULL) 389 return; /* out of resources */ 390 391 bioq_remove(&ida->bio_queue, bp); 392 qcb->buf = bp; 393 qcb->flags = 0; 394 395 hwqcb = qcb->hwqcb; 396 bzero(hwqcb, sizeof(struct ida_hdr) + sizeof(struct ida_req)); 397 398 bus_dmamap_load(ida->buffer_dmat, qcb->dmamap, 399 (void *)bp->bio_data, bp->bio_bcount, ida_setup_dmamap, hwqcb, 0); 400 op = qcb->flags & DMA_DATA_IN ? 401 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 402 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 403 404 { 405 struct idad_softc *drv = (struct idad_softc *)bp->bio_driver1; 406 hwqcb->hdr.drive = drv->drive; 407 } 408 409 hwqcb->req.blkno = bp->bio_pblkno; 410 hwqcb->req.bcount = howmany(bp->bio_bcount, DEV_BSIZE); 411 hwqcb->req.command = bp->bio_cmd == BIO_READ ? CMD_READ : CMD_WRITE; 412 413 STAILQ_INSERT_TAIL(&ida->qcb_queue, qcb, link.stqe); 414} 415 416/* 417 * This routine will be called from ida_intr in order to queue up more 418 * I/O, meaning that we may be in an interrupt context. Hence, we should 419 * not muck around with spl() in this routine. 420 */ 421static void 422ida_start(struct ida_softc *ida) 423{ 424 struct ida_qcb *qcb; 425 426 while ((qcb = STAILQ_FIRST(&ida->qcb_queue)) != NULL) { 427 if (ida->cmd.fifo_full(ida)) 428 break; 429 STAILQ_REMOVE_HEAD(&ida->qcb_queue, link.stqe); 430 /* 431 * XXX 432 * place the qcb on an active list and set a timeout? 433 */ 434 qcb->state = QCB_ACTIVE; 435 ida->cmd.submit(ida, qcb); 436 } 437} 438 439static int 440ida_wait(struct ida_softc *ida, struct ida_qcb *qcb) 441{ 442 struct ida_qcb *qcb_done = NULL; 443 bus_addr_t completed; 444 int delay; 445 446 if (ida->flags & IDA_INTERRUPTS) { 447 if (tsleep((caddr_t)qcb, PRIBIO, "idacmd", 5 * hz)) 448 return (ETIMEDOUT); 449 return (0); 450 } 451 452again: 453 delay = 5 * 1000 * 100; /* 5 sec delay */ 454 while ((completed = ida->cmd.done(ida)) == 0) { 455 if (delay-- == 0) 456 return (ETIMEDOUT); 457 DELAY(10); 458 } 459 460 qcb_done = idahwqcbptov(ida, completed & ~3); 461 if (qcb_done != qcb) 462 goto again; 463 ida_done(ida, qcb); 464 return (0); 465} 466 467void 468ida_intr(void *data) 469{ 470 struct ida_softc *ida; 471 struct ida_qcb *qcb; 472 bus_addr_t completed; 473 474 ida = (struct ida_softc *)data; 475 476 if (ida->cmd.int_pending(ida) == 0) 477 return; /* not our interrupt */ 478 479 while ((completed = ida->cmd.done(ida)) != 0) { 480 qcb = idahwqcbptov(ida, completed & ~3); 481 482 if (qcb == NULL || qcb->state != QCB_ACTIVE) { 483 device_printf(ida->dev, 484 "ignoring completion %jx\n", (intmax_t)completed); 485 continue; 486 } 487 ida_done(ida, qcb); 488 } 489 ida_start(ida); 490} 491 492/* 493 * should switch out command type; may be status, not just I/O. 494 */ 495static void 496ida_done(struct ida_softc *ida, struct ida_qcb *qcb) 497{ 498 int error = 0; 499 500 /* 501 * finish up command 502 */ 503 if (qcb->flags & DMA_DATA_TRANSFER) { 504 bus_dmasync_op_t op; 505 506 op = qcb->flags & DMA_DATA_IN ? 507 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE; 508 bus_dmamap_sync(ida->buffer_dmat, qcb->dmamap, op); 509 bus_dmamap_unload(ida->buffer_dmat, qcb->dmamap); 510 } 511 512 if (qcb->hwqcb->req.error & SOFT_ERROR) 513 device_printf(ida->dev, "soft error\n"); 514 if (qcb->hwqcb->req.error & HARD_ERROR) { 515 error = 1; 516 device_printf(ida->dev, "hard error\n"); 517 } 518 if (qcb->hwqcb->req.error & CMD_REJECTED) { 519 error = 1; 520 device_printf(ida->dev, "invalid request\n"); 521 } 522 523 if (qcb->flags & IDA_COMMAND) { 524 if (ida->flags & IDA_INTERRUPTS) 525 wakeup(qcb); 526 } else { 527 if (error) 528 qcb->buf->bio_flags |= BIO_ERROR; 529 idad_intr(qcb->buf); 530 } 531 532 qcb->state = QCB_FREE; 533 SLIST_INSERT_HEAD(&ida->free_qcbs, qcb, link.sle); 534 ida_construct_qcb(ida); 535} 536