amr.c revision 74936
1/*- 2 * Copyright (c) 1999,2000 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/dev/amr/amr.c 74936 2001-03-28 14:11:15Z hm $ 28 */ 29 30/* 31 * Driver for the AMI MegaRaid family of controllers. 32 */ 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/malloc.h> 37#include <sys/kernel.h> 38 39#include <dev/amr/amr_compat.h> 40#include <sys/bus.h> 41#include <sys/conf.h> 42#include <sys/devicestat.h> 43#include <sys/disk.h> 44#include <sys/stat.h> 45 46#include <machine/bus_memio.h> 47#include <machine/bus_pio.h> 48#include <machine/bus.h> 49#include <machine/resource.h> 50#include <sys/rman.h> 51 52#include <pci/pcireg.h> 53#include <pci/pcivar.h> 54 55#include <dev/amr/amrio.h> 56#include <dev/amr/amrreg.h> 57#include <dev/amr/amrvar.h> 58#define AMR_DEFINE_TABLES 59#include <dev/amr/amr_tables.h> 60 61#define AMR_CDEV_MAJOR 132 62 63static d_open_t amr_open; 64static d_close_t amr_close; 65static d_ioctl_t amr_ioctl; 66 67static struct cdevsw amr_cdevsw = { 68 /* open */ amr_open, 69 /* close */ amr_close, 70 /* read */ noread, 71 /* write */ nowrite, 72 /* ioctl */ amr_ioctl, 73 /* poll */ nopoll, 74 /* mmap */ nommap, 75 /* strategy */ nostrategy, 76 /* name */ "amr", 77 /* maj */ AMR_CDEV_MAJOR, 78 /* dump */ nodump, 79 /* psize */ nopsize, 80 /* flags */ 0, 81}; 82 83/* 84 * Initialisation, bus interface. 85 */ 86static void amr_startup(void *arg); 87 88/* 89 * Command wrappers 90 */ 91static int amr_query_controller(struct amr_softc *sc); 92static void *amr_enquiry(struct amr_softc *sc, size_t bufsize, 93 u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual); 94static void amr_completeio(struct amr_command *ac); 95 96/* 97 * Command buffer allocation. 98 */ 99static void amr_alloccmd_cluster(struct amr_softc *sc); 100static void amr_freecmd_cluster(struct amr_command_cluster *acc); 101 102/* 103 * Command processing. 104 */ 105static int amr_bio_command(struct amr_softc *sc, struct amr_command **acp); 106static int amr_wait_command(struct amr_command *ac); 107static int amr_poll_command(struct amr_command *ac); 108static int amr_getslot(struct amr_command *ac); 109static void amr_mapcmd(struct amr_command *ac); 110static void amr_unmapcmd(struct amr_command *ac); 111static int amr_start(struct amr_command *ac); 112static void amr_complete(void *context, int pending); 113 114/* 115 * Status monitoring 116 */ 117static void amr_periodic(void *data); 118 119/* 120 * Interface-specific shims 121 */ 122static int amr_quartz_submit_command(struct amr_softc *sc); 123static int amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave); 124 125static int amr_std_submit_command(struct amr_softc *sc); 126static int amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave); 127static void amr_std_attach_mailbox(struct amr_softc *sc); 128 129#ifdef AMR_BOARD_INIT 130static int amr_quartz_init(struct amr_softc *sc); 131static int amr_std_init(struct amr_softc *sc); 132#endif 133 134/* 135 * Debugging 136 */ 137static void amr_describe_controller(struct amr_softc *sc); 138#ifdef AMR_DEBUG 139static void amr_printcommand(struct amr_command *ac); 140#endif 141 142/******************************************************************************** 143 ******************************************************************************** 144 Inline Glue 145 ******************************************************************************** 146 ********************************************************************************/ 147 148/******************************************************************************** 149 ******************************************************************************** 150 Public Interfaces 151 ******************************************************************************** 152 ********************************************************************************/ 153 154/******************************************************************************** 155 * Initialise the controller and softc. 156 */ 157int 158amr_attach(struct amr_softc *sc) 159{ 160 161 debug_called(1); 162 163 /* 164 * Initialise per-controller queues. 165 */ 166 TAILQ_INIT(&sc->amr_completed); 167 TAILQ_INIT(&sc->amr_freecmds); 168 TAILQ_INIT(&sc->amr_cmd_clusters); 169 TAILQ_INIT(&sc->amr_ready); 170 bioq_init(&sc->amr_bioq); 171 172#if __FreeBSD_version >= 500005 173 /* 174 * Initialise command-completion task. 175 */ 176 TASK_INIT(&sc->amr_task_complete, 0, amr_complete, sc); 177#endif 178 179 debug(2, "queue init done"); 180 181 /* 182 * Configure for this controller type. 183 */ 184 if (AMR_IS_QUARTZ(sc)) { 185 sc->amr_submit_command = amr_quartz_submit_command; 186 sc->amr_get_work = amr_quartz_get_work; 187 } else { 188 sc->amr_submit_command = amr_std_submit_command; 189 sc->amr_get_work = amr_std_get_work; 190 amr_std_attach_mailbox(sc);; 191 } 192 193#ifdef AMR_BOARD_INIT 194 if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc)))) 195 return(ENXIO); 196#endif 197 198 /* 199 * Quiz controller for features and limits. 200 */ 201 if (amr_query_controller(sc)) 202 return(ENXIO); 203 204 debug(2, "controller query complete"); 205 206#ifdef AMR_SCSI_PASSTHROUGH 207 /* 208 * Attach our 'real' SCSI channels to CAM. 209 */ 210 if (amr_cam_attach(sc)) 211 return(ENXIO); 212 debug(2, "CAM attach done"); 213#endif 214 215 /* 216 * Create the control device. 217 */ 218 sc->amr_dev_t = make_dev(&amr_cdevsw, device_get_unit(sc->amr_dev), UID_ROOT, GID_OPERATOR, 219 S_IRUSR | S_IWUSR, "amr%d", device_get_unit(sc->amr_dev)); 220 sc->amr_dev_t->si_drv1 = sc; 221 222 /* 223 * Schedule ourselves to bring the controller up once interrupts are 224 * available. 225 */ 226 bzero(&sc->amr_ich, sizeof(struct intr_config_hook)); 227 sc->amr_ich.ich_func = amr_startup; 228 sc->amr_ich.ich_arg = sc; 229 if (config_intrhook_establish(&sc->amr_ich) != 0) { 230 device_printf(sc->amr_dev, "can't establish configuration hook\n"); 231 return(ENOMEM); 232 } 233 234 /* 235 * Print a little information about the controller. 236 */ 237 amr_describe_controller(sc); 238 239 debug(2, "attach complete"); 240 return(0); 241} 242 243/******************************************************************************** 244 * Locate disk resources and attach children to them. 245 */ 246static void 247amr_startup(void *arg) 248{ 249 struct amr_softc *sc = (struct amr_softc *)arg; 250 struct amr_logdrive *dr; 251 int i, error; 252 253 debug_called(1); 254 255 /* pull ourselves off the intrhook chain */ 256 config_intrhook_disestablish(&sc->amr_ich); 257 258 /* get up-to-date drive information */ 259 if (amr_query_controller(sc)) { 260 device_printf(sc->amr_dev, "can't scan controller for drives\n"); 261 return; 262 } 263 264 /* iterate over available drives */ 265 for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) { 266 /* are we already attached to this drive? */ 267 if (dr->al_disk == 0) { 268 /* generate geometry information */ 269 if (dr->al_size > 0x200000) { /* extended translation? */ 270 dr->al_heads = 255; 271 dr->al_sectors = 63; 272 } else { 273 dr->al_heads = 64; 274 dr->al_sectors = 32; 275 } 276 dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors); 277 278 dr->al_disk = device_add_child(sc->amr_dev, NULL, -1); 279 if (dr->al_disk == 0) 280 device_printf(sc->amr_dev, "device_add_child failed\n"); 281 device_set_ivars(dr->al_disk, dr); 282 } 283 } 284 285 if ((error = bus_generic_attach(sc->amr_dev)) != 0) 286 device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error); 287 288 /* mark controller back up */ 289 sc->amr_state &= ~AMR_STATE_SHUTDOWN; 290 291 /* interrupts will be enabled before we do anything more */ 292 sc->amr_state |= AMR_STATE_INTEN; 293 294 /* 295 * Start the timeout routine. 296 */ 297/* sc->amr_timeout = timeout(amr_periodic, sc, hz);*/ 298 299 return; 300} 301 302/******************************************************************************* 303 * Free resources associated with a controller instance 304 */ 305void 306amr_free(struct amr_softc *sc) 307{ 308 struct amr_command_cluster *acc; 309 310#ifdef AMR_SCSI_PASSTHROUGH 311 /* detach from CAM */ 312 amr_cam_detach(sc); 313#endif 314 315 /* cancel status timeout */ 316 untimeout(amr_periodic, sc, sc->amr_timeout); 317 318 /* throw away any command buffers */ 319 while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) { 320 TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link); 321 amr_freecmd_cluster(acc); 322 } 323} 324 325/******************************************************************************* 326 * Receive a bio structure from a child device and queue it on a particular 327 * disk resource, then poke the disk resource to start as much work as it can. 328 */ 329int 330amr_submit_bio(struct amr_softc *sc, struct bio *bio) 331{ 332 debug_called(2); 333 334 amr_enqueue_bio(sc, bio); 335 amr_startio(sc); 336 return(0); 337} 338 339/******************************************************************************** 340 * Accept an open operation on the control device. 341 */ 342int 343amr_open(dev_t dev, int flags, int fmt, struct proc *p) 344{ 345 int unit = minor(dev); 346 struct amr_softc *sc = devclass_get_softc(amr_devclass, unit); 347 348 debug_called(1); 349 350 sc->amr_state |= AMR_STATE_OPEN; 351 return(0); 352} 353 354/******************************************************************************** 355 * Accept the last close on the control device. 356 */ 357int 358amr_close(dev_t dev, int flags, int fmt, struct proc *p) 359{ 360 int unit = minor(dev); 361 struct amr_softc *sc = devclass_get_softc(amr_devclass, unit); 362 363 debug_called(1); 364 365 sc->amr_state &= ~AMR_STATE_OPEN; 366 return (0); 367} 368 369/******************************************************************************** 370 * Handle controller-specific control operations. 371 */ 372int 373amr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p) 374{ 375 struct amr_softc *sc = (struct amr_softc *)dev->si_drv1; 376 int *arg = (int *)addr; 377 struct amr_user_ioctl *au = (struct amr_user_ioctl *)addr; 378 struct amr_command *ac; 379 struct amr_mailbox_ioctl *mbi; 380 struct amr_passthrough *ap; 381 void *dp; 382 int error; 383 384 debug_called(1); 385 386 error = 0; 387 dp = NULL; 388 ap = NULL; 389 ac = NULL; 390 switch(cmd) { 391 392 case AMR_IO_VERSION: 393 debug(1, "AMR_IO_VERSION"); 394 *arg = AMR_IO_VERSION_NUMBER; 395 break; 396 397 case AMR_IO_COMMAND: 398 debug(1, "AMR_IO_COMMAND 0x%x", au->au_cmd[0]); 399 /* handle inbound data buffer */ 400 if (au->au_length != 0) { 401 if ((dp = malloc(au->au_length, M_DEVBUF, M_WAITOK)) == NULL) { 402 error = ENOMEM; 403 break; 404 } 405 if ((error = copyin(au->au_buffer, dp, au->au_length)) != 0) 406 break; 407 debug(2, "copyin %ld bytes from %p -> %p", au->au_length, au->au_buffer, dp); 408 } 409 410 if ((ac = amr_alloccmd(sc)) == NULL) { 411 error = ENOMEM; 412 break; 413 } 414 415 /* handle SCSI passthrough command */ 416 if (au->au_cmd[0] == AMR_CMD_PASS) { 417 if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) { 418 error = ENOMEM; 419 break; 420 } 421 422 /* copy cdb */ 423 ap->ap_cdb_length = au->au_cmd[2]; 424 bcopy(&au->au_cmd[3], &ap->ap_cdb[0], ap->ap_cdb_length); 425 426 /* build passthrough */ 427 ap->ap_timeout = au->au_cmd[ap->ap_cdb_length + 3] & 0x07; 428 ap->ap_ars = (au->au_cmd[ap->ap_cdb_length + 3] & 0x08) ? 1 : 0; 429 ap->ap_islogical = (au->au_cmd[ap->ap_cdb_length + 3] & 0x80) ? 1 : 0; 430 ap->ap_logical_drive_no = au->au_cmd[ap->ap_cdb_length + 4]; 431 ap->ap_channel = au->au_cmd[ap->ap_cdb_length + 5]; 432 ap->ap_scsi_id = au->au_cmd[ap->ap_cdb_length + 6]; 433 ap->ap_request_sense_length = 14; 434 /* XXX what about the request-sense area? does the caller want it? */ 435 436 /* build command */ 437 ac->ac_data = ap; 438 ac->ac_length = sizeof(*ap); 439 ac->ac_flags |= AMR_CMD_DATAOUT; 440 ac->ac_ccb_data = dp; 441 ac->ac_ccb_length = au->au_length; 442 if (au->au_direction & AMR_IO_READ) 443 ac->ac_flags |= AMR_CMD_CCB_DATAIN; 444 if (au->au_direction & AMR_IO_WRITE) 445 ac->ac_flags |= AMR_CMD_CCB_DATAOUT; 446 447 ac->ac_mailbox.mb_command = AMR_CMD_PASS; 448 449 } else { 450 /* direct command to controller */ 451 mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox; 452 453 /* copy pertinent mailbox items */ 454 mbi->mb_command = au->au_cmd[0]; 455 mbi->mb_channel = au->au_cmd[1]; 456 mbi->mb_param = au->au_cmd[2]; 457 mbi->mb_pad[0] = au->au_cmd[3]; 458 mbi->mb_drive = au->au_cmd[4]; 459 460 /* build the command */ 461 ac->ac_data = dp; 462 ac->ac_length = au->au_length; 463 if (au->au_direction & AMR_IO_READ) 464 ac->ac_flags |= AMR_CMD_DATAIN; 465 if (au->au_direction & AMR_IO_WRITE) 466 ac->ac_flags |= AMR_CMD_DATAOUT; 467 } 468 469 /* run the command */ 470 if ((error = amr_wait_command(ac)) != 0) 471 break; 472 473 /* copy out data and set status */ 474 if (au->au_length != 0) 475 error = copyout(dp, au->au_buffer, au->au_length); 476 debug(2, "copyout %ld bytes from %p -> %p", au->au_length, dp, au->au_buffer); 477 if (dp != NULL) 478 debug(2, "%16D", dp, " "); 479 au->au_status = ac->ac_status; 480 break; 481 482 default: 483 debug(1, "unknown ioctl 0x%lx", cmd); 484 error = ENOIOCTL; 485 break; 486 } 487 488 if (dp != NULL) 489 free(dp, M_DEVBUF); 490 if (ap != NULL) 491 free(ap, M_DEVBUF); 492 if (ac != NULL) 493 amr_releasecmd(ac); 494 return(error); 495} 496 497/******************************************************************************** 498 ******************************************************************************** 499 Status Monitoring 500 ******************************************************************************** 501 ********************************************************************************/ 502 503/******************************************************************************** 504 * Perform a periodic check of the controller status 505 */ 506static void 507amr_periodic(void *data) 508{ 509 struct amr_softc *sc = (struct amr_softc *)data; 510 511 debug_called(2); 512 513 /* XXX perform periodic status checks here */ 514 515 /* compensate for missed interrupts */ 516 amr_done(sc); 517 518 /* reschedule */ 519 sc->amr_timeout = timeout(amr_periodic, sc, hz); 520} 521 522/******************************************************************************** 523 ******************************************************************************** 524 Command Wrappers 525 ******************************************************************************** 526 ********************************************************************************/ 527 528/******************************************************************************** 529 * Interrogate the controller for the operational parameters we require. 530 */ 531static int 532amr_query_controller(struct amr_softc *sc) 533{ 534 struct amr_enquiry3 *aex; 535 struct amr_prodinfo *ap; 536 struct amr_enquiry *ae; 537 int ldrv; 538 539 /* 540 * If we haven't found the real limit yet, let us have a couple of commands in 541 * order to be able to probe. 542 */ 543 if (sc->amr_maxio == 0) 544 sc->amr_maxio = 2; 545 546 /* 547 * Try to issue an ENQUIRY3 command 548 */ 549 if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3, 550 AMR_CONFIG_ENQ3_SOLICITED_FULL)) != NULL) { 551 552 /* 553 * Fetch current state of logical drives. 554 */ 555 for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) { 556 sc->amr_drive[ldrv].al_size = aex->ae_drivesize[ldrv]; 557 sc->amr_drive[ldrv].al_state = aex->ae_drivestate[ldrv]; 558 sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv]; 559 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size, 560 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties); 561 } 562 free(aex, M_DEVBUF); 563 564 /* 565 * Get product info for channel count. 566 */ 567 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) == NULL) { 568 device_printf(sc->amr_dev, "can't obtain product data from controller\n"); 569 return(1); 570 } 571 sc->amr_maxdrives = 40; 572 sc->amr_maxchan = ap->ap_nschan; 573 sc->amr_maxio = ap->ap_maxio; 574 sc->amr_type |= AMR_TYPE_40LD; 575 free(ap, M_DEVBUF); 576 577 } else { 578 579 /* failed, try the 8LD ENQUIRY commands */ 580 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) == NULL) { 581 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) { 582 device_printf(sc->amr_dev, "can't obtain configuration data from controller\n"); 583 return(1); 584 } 585 ae->ae_signature = 0; 586 } 587 588 /* 589 * Fetch current state of logical drives. 590 */ 591 for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) { 592 sc->amr_drive[ldrv].al_size = ae->ae_ldrv.al_size[ldrv]; 593 sc->amr_drive[ldrv].al_state = ae->ae_ldrv.al_state[ldrv]; 594 sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv]; 595 debug(2, " drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size, 596 sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties); 597 } 598 599 sc->amr_maxdrives = 8; 600 sc->amr_maxchan = ae->ae_adapter.aa_channels; 601 sc->amr_maxio = ae->ae_adapter.aa_maxio; 602 free(ae, M_DEVBUF); 603 } 604 605 /* 606 * Mark remaining drives as unused. 607 */ 608 for (; ldrv < AMR_MAXLD; ldrv++) 609 sc->amr_drive[ldrv].al_size = 0xffffffff; 610 611 /* 612 * Cap the maximum number of outstanding I/Os. AMI's Linux driver doesn't trust 613 * the controller's reported value, and lockups have been seen when we do. 614 */ 615 sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD); 616 617 return(0); 618} 619 620/******************************************************************************** 621 * Run a generic enquiry-style command. 622 */ 623static void * 624amr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual) 625{ 626 struct amr_command *ac; 627 void *result; 628 u_int8_t *mbox; 629 int error; 630 631 debug_called(1); 632 633 error = 1; 634 result = NULL; 635 636 /* get ourselves a command buffer */ 637 if ((ac = amr_alloccmd(sc)) == NULL) 638 goto out; 639 /* allocate the response structure */ 640 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL) 641 goto out; 642 /* set command flags */ 643 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT; 644 645 /* point the command at our data */ 646 ac->ac_data = result; 647 ac->ac_length = bufsize; 648 649 /* build the command proper */ 650 mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */ 651 mbox[0] = cmd; 652 mbox[2] = cmdsub; 653 mbox[3] = cmdqual; 654 655 /* can't assume that interrupts are going to work here, so play it safe */ 656 if (amr_poll_command(ac)) 657 goto out; 658 error = ac->ac_status; 659 660 out: 661 if (ac != NULL) 662 amr_releasecmd(ac); 663 if ((error != 0) && (result != NULL)) { 664 free(result, M_DEVBUF); 665 result = NULL; 666 } 667 return(result); 668} 669 670/******************************************************************************** 671 * Flush the controller's internal cache, return status. 672 */ 673int 674amr_flush(struct amr_softc *sc) 675{ 676 struct amr_command *ac; 677 int error; 678 679 /* get ourselves a command buffer */ 680 error = 1; 681 if ((ac = amr_alloccmd(sc)) == NULL) 682 goto out; 683 /* set command flags */ 684 ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT; 685 686 /* build the command proper */ 687 ac->ac_mailbox.mb_command = AMR_CMD_FLUSH; 688 689 /* we have to poll, as the system may be going down or otherwise damaged */ 690 if (amr_poll_command(ac)) 691 goto out; 692 error = ac->ac_status; 693 694 out: 695 if (ac != NULL) 696 amr_releasecmd(ac); 697 return(error); 698} 699 700/******************************************************************************** 701 * Try to find I/O work for the controller from one or more of the work queues. 702 * 703 * We make the assumption that if the controller is not ready to take a command 704 * at some given time, it will generate an interrupt at some later time when 705 * it is. 706 */ 707void 708amr_startio(struct amr_softc *sc) 709{ 710 struct amr_command *ac; 711 712 /* spin until something prevents us from doing any work */ 713 for (;;) { 714 715 /* try to get a ready command */ 716 ac = amr_dequeue_ready(sc); 717 718 /* if that failed, build a command from a bio */ 719 if (ac == NULL) 720 (void)amr_bio_command(sc, &ac); 721 722#ifdef AMR_SCSI_PASSTHROUGH 723 /* if that failed, build a command from a ccb */ 724 if (ac == NULL) 725 (void)amr_cam_command(sc, &ac); 726#endif 727 728 /* if we don't have anything to do, give up */ 729 if (ac == NULL) 730 break; 731 732 /* try to give the command to the controller; if this fails save it for later and give up */ 733 if (amr_start(ac)) { 734 debug(2, "controller busy, command deferred"); 735 amr_requeue_ready(ac); /* XXX schedule retry very soon? */ 736 break; 737 } 738 } 739} 740 741/******************************************************************************** 742 * Handle completion of an I/O command. 743 */ 744static void 745amr_completeio(struct amr_command *ac) 746{ 747 struct amr_softc *sc = ac->ac_sc; 748 749 if (ac->ac_status != AMR_STATUS_SUCCESS) { /* could be more verbose here? */ 750 ac->ac_bio->bio_error = EIO; 751 ac->ac_bio->bio_flags |= BIO_ERROR; 752 753 device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status); 754/* amr_printcommand(ac);*/ 755 } 756 amrd_intr(ac->ac_bio); 757 amr_releasecmd(ac); 758} 759 760/******************************************************************************** 761 ******************************************************************************** 762 Command Processing 763 ******************************************************************************** 764 ********************************************************************************/ 765 766/******************************************************************************** 767 * Convert a bio off the top of the bio queue into a command. 768 */ 769static int 770amr_bio_command(struct amr_softc *sc, struct amr_command **acp) 771{ 772 struct amr_command *ac; 773 struct amrd_softc *amrd; 774 struct bio *bio; 775 int error; 776 int blkcount; 777 int driveno; 778 int cmd; 779 780 ac = NULL; 781 error = 0; 782 783 /* get a bio to work on */ 784 if ((bio = amr_dequeue_bio(sc)) == NULL) 785 goto out; 786 787 /* get a command */ 788 if ((ac = amr_alloccmd(sc)) == NULL) { 789 error = ENOMEM; 790 goto out; 791 } 792 793 /* connect the bio to the command */ 794 ac->ac_complete = amr_completeio; 795 ac->ac_bio = bio; 796 ac->ac_data = bio->bio_data; 797 ac->ac_length = bio->bio_bcount; 798 if (BIO_IS_READ(bio)) { 799 ac->ac_flags |= AMR_CMD_DATAIN; 800 cmd = AMR_CMD_LREAD; 801 } else { 802 ac->ac_flags |= AMR_CMD_DATAOUT; 803 cmd = AMR_CMD_LWRITE; 804 } 805 amrd = (struct amrd_softc *)bio->bio_dev->si_drv1; 806 driveno = amrd->amrd_drive - sc->amr_drive; 807 blkcount = (bio->bio_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE; 808 809 ac->ac_mailbox.mb_command = cmd; 810 ac->ac_mailbox.mb_blkcount = blkcount; 811 ac->ac_mailbox.mb_lba = bio->bio_pblkno; 812 ac->ac_mailbox.mb_drive = driveno; 813 /* we fill in the s/g related data when the command is mapped */ 814 815 if ((bio->bio_pblkno + blkcount) > sc->amr_drive[driveno].al_size) 816 device_printf(sc->amr_dev, "I/O beyond end of unit (%u,%d > %u)\n", 817 bio->bio_pblkno, blkcount, sc->amr_drive[driveno].al_size); 818 819out: 820 if (error != 0) { 821 if (ac != NULL) 822 amr_releasecmd(ac); 823 if (bio != NULL) /* this breaks ordering... */ 824 amr_enqueue_bio(sc, bio); 825 } 826 *acp = ac; 827 return(error); 828} 829 830/******************************************************************************** 831 * Take a command, submit it to the controller and sleep until it completes 832 * or fails. Interrupts must be enabled, returns nonzero on error. 833 */ 834static int 835amr_wait_command(struct amr_command *ac) 836{ 837 int error, count; 838 839 debug_called(1); 840 841 ac->ac_complete = NULL; 842 ac->ac_flags |= AMR_CMD_SLEEP; 843 if ((error = amr_start(ac)) != 0) 844 return(error); 845 846 count = 0; 847 /* XXX better timeout? */ 848 while ((ac->ac_flags & AMR_CMD_BUSY) && (count < 30)) { 849 tsleep(ac, PRIBIO | PCATCH, "amrwcmd", hz); 850 } 851 return(0); 852} 853 854/******************************************************************************** 855 * Take a command, submit it to the controller and busy-wait for it to return. 856 * Returns nonzero on error. Can be safely called with interrupts enabled. 857 */ 858static int 859amr_poll_command(struct amr_command *ac) 860{ 861 struct amr_softc *sc = ac->ac_sc; 862 int error, count; 863 864 debug_called(2); 865 866 ac->ac_complete = NULL; 867 if ((error = amr_start(ac)) != 0) 868 return(error); 869 870 count = 0; 871 do { 872 /* 873 * Poll for completion, although the interrupt handler may beat us to it. 874 * Note that the timeout here is somewhat arbitrary. 875 */ 876 amr_done(sc); 877 DELAY(1000); 878 } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000)); 879 if (!(ac->ac_flags & AMR_CMD_BUSY)) { 880 error = 0; 881 } else { 882 /* XXX the slot is now marked permanently busy */ 883 error = EIO; 884 device_printf(sc->amr_dev, "polled command timeout\n"); 885 } 886 return(error); 887} 888 889/******************************************************************************** 890 * Get a free command slot for a command if it doesn't already have one. 891 * 892 * May be safely called multiple times for a given command. 893 */ 894static int 895amr_getslot(struct amr_command *ac) 896{ 897 struct amr_softc *sc = ac->ac_sc; 898 int s, slot, limit, error; 899 900 debug_called(3); 901 902 /* if the command already has a slot, don't try to give it another one */ 903 if (ac->ac_slot != 0) 904 return(0); 905 906 /* enforce slot usage limit */ 907 limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4; 908 if (sc->amr_busyslots > limit) 909 return(EBUSY); 910 911 /* 912 * Allocate a slot. XXX linear scan is slow 913 */ 914 error = EBUSY; 915 s = splbio(); 916 for (slot = 0; slot < sc->amr_maxio; slot++) { 917 if (sc->amr_busycmd[slot] == NULL) { 918 sc->amr_busycmd[slot] = ac; 919 sc->amr_busyslots++; 920 ac->ac_slot = slot; 921 error = 0; 922 break; 923 } 924 } 925 splx(s); 926 927 return(error); 928} 929 930/******************************************************************************** 931 * Map/unmap (ac)'s data in the controller's addressable space as required. 932 * 933 * These functions may be safely called multiple times on a given command. 934 */ 935static void 936amr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 937{ 938 struct amr_command *ac = (struct amr_command *)arg; 939 struct amr_softc *sc = ac->ac_sc; 940 struct amr_sgentry *sg; 941 int i; 942 u_int8_t *sgc; 943 944 debug_called(3); 945 946 /* get base address of s/g table */ 947 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG); 948 949 /* save data physical address */ 950 ac->ac_dataphys = segs[0].ds_addr; 951 952 /* for AMR_CMD_CONFIG the s/g count goes elsewhere */ 953 if (ac->ac_mailbox.mb_command == AMR_CMD_CONFIG) { 954 sgc = &(((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_param); 955 } else { 956 sgc = &ac->ac_mailbox.mb_nsgelem; 957 } 958 959 /* decide whether we need to populate the s/g table */ 960 if (nsegments < 2) { 961 *sgc = 0; 962 ac->ac_mailbox.mb_physaddr = ac->ac_dataphys; 963 } else { 964 *sgc = nsegments; 965 ac->ac_mailbox.mb_physaddr = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry)); 966 for (i = 0; i < nsegments; i++, sg++) { 967 sg->sg_addr = segs[i].ds_addr; 968 sg->sg_count = segs[i].ds_len; 969 } 970 } 971} 972 973static void 974amr_setup_ccbmap(void *arg, bus_dma_segment_t *segs, int nsegments, int error) 975{ 976 struct amr_command *ac = (struct amr_command *)arg; 977 struct amr_softc *sc = ac->ac_sc; 978 struct amr_sgentry *sg; 979 struct amr_passthrough *ap = (struct amr_passthrough *)ac->ac_data; 980 int i; 981 982 /* get base address of s/g table */ 983 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG); 984 985 /* save s/g table information in passthrough */ 986 ap->ap_no_sg_elements = nsegments; 987 ap->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry)); 988 989 /* save pointer to passthrough in command XXX is this already done above? */ 990 ac->ac_mailbox.mb_physaddr = ac->ac_dataphys; 991 992 debug(3, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot, 993 ap->ap_no_sg_elements, ap->ap_data_transfer_address, ac->ac_dataphys); 994 995 /* populate s/g table (overwrites previous call which mapped the passthrough) */ 996 for (i = 0; i < nsegments; i++, sg++) { 997 sg->sg_addr = segs[i].ds_addr; 998 sg->sg_count = segs[i].ds_len; 999 debug(3, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count); 1000 } 1001} 1002 1003static void 1004amr_mapcmd(struct amr_command *ac) 1005{ 1006 struct amr_softc *sc = ac->ac_sc; 1007 1008 debug_called(3); 1009 1010 /* if the command involves data at all, and hasn't been mapped */ 1011 if (!(ac->ac_flags & AMR_CMD_MAPPED)) { 1012 1013 if (ac->ac_data != NULL) { 1014 /* map the data buffers into bus space and build the s/g list */ 1015 bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length, 1016 amr_setup_dmamap, ac, 0); 1017 if (ac->ac_flags & AMR_CMD_DATAIN) 1018 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD); 1019 if (ac->ac_flags & AMR_CMD_DATAOUT) 1020 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE); 1021 } 1022 1023 if (ac->ac_ccb_data != NULL) { 1024 bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, ac->ac_ccb_data, ac->ac_ccb_length, 1025 amr_setup_ccbmap, ac, 0); 1026 if (ac->ac_flags & AMR_CMD_CCB_DATAIN) 1027 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREREAD); 1028 if (ac->ac_flags & AMR_CMD_CCB_DATAOUT) 1029 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREWRITE); 1030 } 1031 ac->ac_flags |= AMR_CMD_MAPPED; 1032 } 1033} 1034 1035static void 1036amr_unmapcmd(struct amr_command *ac) 1037{ 1038 struct amr_softc *sc = ac->ac_sc; 1039 1040 debug_called(3); 1041 1042 /* if the command involved data at all and was mapped */ 1043 if (ac->ac_flags & AMR_CMD_MAPPED) { 1044 1045 if (ac->ac_data != NULL) { 1046 if (ac->ac_flags & AMR_CMD_DATAIN) 1047 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD); 1048 if (ac->ac_flags & AMR_CMD_DATAOUT) 1049 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE); 1050 bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap); 1051 } 1052 1053 if (ac->ac_ccb_data != NULL) { 1054 if (ac->ac_flags & AMR_CMD_CCB_DATAIN) 1055 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTREAD); 1056 if (ac->ac_flags & AMR_CMD_CCB_DATAOUT) 1057 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTWRITE); 1058 bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_ccb_dmamap); 1059 } 1060 ac->ac_flags &= ~AMR_CMD_MAPPED; 1061 } 1062} 1063 1064/******************************************************************************** 1065 * Take a command and give it to the controller, returns 0 if successful, or 1066 * EBUSY if the command should be retried later. 1067 */ 1068static int 1069amr_start(struct amr_command *ac) 1070{ 1071 struct amr_softc *sc = ac->ac_sc; 1072 int done, s, i; 1073 1074 debug_called(3); 1075 1076 /* mark command as busy so that polling consumer can tell */ 1077 ac->ac_flags |= AMR_CMD_BUSY; 1078 1079 /* get a command slot (freed in amr_done) */ 1080 if (amr_getslot(ac)) 1081 return(EBUSY); 1082 1083 /* now we have a slot, we can map the command (unmapped in amr_complete) */ 1084 amr_mapcmd(ac); 1085 1086 /* mark the new mailbox we are going to copy in as busy */ 1087 ac->ac_mailbox.mb_busy = 1; 1088 1089 /* clear the poll/ack fields in the mailbox */ 1090 sc->amr_mailbox->mb_poll = 0; 1091 sc->amr_mailbox->mb_ack = 0; 1092 1093 /* 1094 * Save the slot number so that we can locate this command when complete. 1095 * Note that ident = 0 seems to be special, so we don't use it. 1096 */ 1097 ac->ac_mailbox.mb_ident = ac->ac_slot + 1; 1098 1099 /* 1100 * Spin waiting for the mailbox, give up after ~1 second. We expect the 1101 * controller to be able to handle our I/O. 1102 * 1103 * XXX perhaps we should wait for less time, and count on the deferred command 1104 * handling to deal with retries? 1105 */ 1106 debug(4, "wait for mailbox"); 1107 for (i = 10000, done = 0; (i > 0) && !done; i--) { 1108 s = splbio(); 1109 1110 /* is the mailbox free? */ 1111 if (sc->amr_mailbox->mb_busy == 0) { 1112 debug(4, "got mailbox"); 1113 sc->amr_mailbox64->mb64_segment = 0; 1114 bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE); 1115 done = 1; 1116 1117 /* not free, spin waiting */ 1118 } else { 1119 debug(4, "busy flag %x\n", sc->amr_mailbox->mb_busy); 1120 /* this is somewhat ugly */ 1121 DELAY(100); 1122 } 1123 splx(s); /* drop spl to allow completion interrupts */ 1124 } 1125 1126 /* 1127 * Now give the command to the controller 1128 */ 1129 if (done) { 1130 if (sc->amr_submit_command(sc)) { 1131 /* the controller wasn't ready to take the command, forget that we tried to post it */ 1132 sc->amr_mailbox->mb_busy = 0; 1133 return(EBUSY); 1134 } 1135 debug(3, "posted command"); 1136 return(0); 1137 } 1138 1139 /* 1140 * The controller wouldn't take the command. Return the command as busy 1141 * so that it is retried later. 1142 */ 1143 return(EBUSY); 1144} 1145 1146/******************************************************************************** 1147 * Extract one or more completed commands from the controller (sc) 1148 * 1149 * Returns nonzero if any commands on the work queue were marked as completed. 1150 */ 1151int 1152amr_done(struct amr_softc *sc) 1153{ 1154 struct amr_command *ac; 1155 struct amr_mailbox mbox; 1156 int i, idx, result; 1157 1158 debug_called(3); 1159 1160 /* See if there's anything for us to do */ 1161 result = 0; 1162 1163 /* loop collecting completed commands */ 1164 for (;;) { 1165 /* poll for a completed command's identifier and status */ 1166 if (sc->amr_get_work(sc, &mbox)) { 1167 result = 1; 1168 1169 /* iterate over completed commands in this result */ 1170 for (i = 0; i < mbox.mb_nstatus; i++) { 1171 /* get pointer to busy command */ 1172 idx = mbox.mb_completed[i] - 1; 1173 ac = sc->amr_busycmd[idx]; 1174 1175 /* really a busy command? */ 1176 if (ac != NULL) { 1177 1178 /* pull the command from the busy index */ 1179 sc->amr_busycmd[idx] = NULL; 1180 sc->amr_busyslots--; 1181 1182 /* save status for later use */ 1183 ac->ac_status = mbox.mb_status; 1184 amr_enqueue_completed(ac); 1185 debug(3, "completed command with status %x", mbox.mb_status); 1186 } else { 1187 device_printf(sc->amr_dev, "bad slot %d completed\n", idx); 1188 } 1189 } 1190 } else { 1191 break; /* no work */ 1192 } 1193 } 1194 1195 /* if we've completed any commands, try posting some more */ 1196 if (result) 1197 amr_startio(sc); 1198 1199 /* handle completion and timeouts */ 1200#if __FreeBSD_version >= 500005 1201 if (sc->amr_state & AMR_STATE_INTEN) 1202 taskqueue_enqueue(taskqueue_swi, &sc->amr_task_complete); 1203 else 1204#endif 1205 amr_complete(sc, 0); 1206 1207 return(result); 1208} 1209 1210/******************************************************************************** 1211 * Do completion processing on done commands on (sc) 1212 */ 1213static void 1214amr_complete(void *context, int pending) 1215{ 1216 struct amr_softc *sc = (struct amr_softc *)context; 1217 struct amr_command *ac; 1218 1219 debug_called(3); 1220 1221 /* pull completed commands off the queue */ 1222 for (;;) { 1223 ac = amr_dequeue_completed(sc); 1224 if (ac == NULL) 1225 break; 1226 1227 /* unmap the command's data buffer */ 1228 amr_unmapcmd(ac); 1229 1230 /* unbusy the command */ 1231 ac->ac_flags &= ~AMR_CMD_BUSY; 1232 1233 /* 1234 * Is there a completion handler? 1235 */ 1236 if (ac->ac_complete != NULL) { 1237 ac->ac_complete(ac); 1238 1239 /* 1240 * Is someone sleeping on this one? 1241 */ 1242 } else if (ac->ac_flags & AMR_CMD_SLEEP) { 1243 wakeup(ac); 1244 } 1245 } 1246} 1247 1248/******************************************************************************** 1249 ******************************************************************************** 1250 Command Buffer Management 1251 ******************************************************************************** 1252 ********************************************************************************/ 1253 1254/******************************************************************************** 1255 * Get a new command buffer. 1256 * 1257 * This may return NULL in low-memory cases. 1258 * 1259 * If possible, we recycle a command buffer that's been used before. 1260 */ 1261struct amr_command * 1262amr_alloccmd(struct amr_softc *sc) 1263{ 1264 struct amr_command *ac; 1265 1266 debug_called(3); 1267 1268 ac = amr_dequeue_free(sc); 1269 if (ac == NULL) { 1270 amr_alloccmd_cluster(sc); 1271 ac = amr_dequeue_free(sc); 1272 } 1273 if (ac == NULL) 1274 return(NULL); 1275 1276 /* clear out significant fields */ 1277 ac->ac_slot = 0; 1278 ac->ac_status = 0; 1279 bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox)); 1280 ac->ac_flags = 0; 1281 ac->ac_bio = NULL; 1282 ac->ac_data = NULL; 1283 ac->ac_ccb_data = NULL; 1284 ac->ac_complete = NULL; 1285 return(ac); 1286} 1287 1288/******************************************************************************** 1289 * Release a command buffer for recycling. 1290 */ 1291void 1292amr_releasecmd(struct amr_command *ac) 1293{ 1294 debug_called(3); 1295 1296 amr_enqueue_free(ac); 1297} 1298 1299/******************************************************************************** 1300 * Allocate a new command cluster and initialise it. 1301 */ 1302void 1303amr_alloccmd_cluster(struct amr_softc *sc) 1304{ 1305 struct amr_command_cluster *acc; 1306 struct amr_command *ac; 1307 int s, i; 1308 1309 acc = malloc(AMR_CMD_CLUSTERSIZE, M_DEVBUF, M_NOWAIT); 1310 if (acc != NULL) { 1311 s = splbio(); 1312 TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link); 1313 splx(s); 1314 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) { 1315 ac = &acc->acc_command[i]; 1316 bzero(ac, sizeof(*ac)); 1317 ac->ac_sc = sc; 1318 if (!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap) && 1319 !bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_ccb_dmamap)) 1320 amr_releasecmd(ac); 1321 } 1322 } 1323} 1324 1325/******************************************************************************** 1326 * Free a command cluster 1327 */ 1328void 1329amr_freecmd_cluster(struct amr_command_cluster *acc) 1330{ 1331 struct amr_softc *sc = acc->acc_command[0].ac_sc; 1332 int i; 1333 1334 for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) 1335 bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap); 1336 free(acc, M_DEVBUF); 1337} 1338 1339/******************************************************************************** 1340 ******************************************************************************** 1341 Interface-specific Shims 1342 ******************************************************************************** 1343 ********************************************************************************/ 1344 1345/******************************************************************************** 1346 * Tell the controller that the mailbox contains a valid command 1347 */ 1348static int 1349amr_quartz_submit_command(struct amr_softc *sc) 1350{ 1351 debug_called(3); 1352 1353 if (AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT) 1354 return(EBUSY); 1355 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT); 1356 return(0); 1357} 1358 1359static int 1360amr_std_submit_command(struct amr_softc *sc) 1361{ 1362 debug_called(3); 1363 1364 if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG) 1365 return(EBUSY); 1366 AMR_SPOST_COMMAND(sc); 1367 return(0); 1368} 1369 1370/******************************************************************************** 1371 * Claim any work that the controller has completed; acknowledge completion, 1372 * save details of the completion in (mbsave) 1373 */ 1374static int 1375amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave) 1376{ 1377 int s, worked; 1378 u_int32_t outd; 1379 1380 debug_called(3); 1381 1382 worked = 0; 1383 s = splbio(); 1384 1385 /* work waiting for us? */ 1386 if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) { 1387 1388 /* save mailbox, which contains a list of completed commands */ 1389 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave)); 1390 1391 /* acknowledge interrupt */ 1392 AMR_QPUT_ODB(sc, AMR_QODB_READY); 1393 1394 /* acknowledge that we have the commands */ 1395 AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK); 1396 1397#ifndef AMR_QUARTZ_GOFASTER 1398 /* 1399 * This waits for the controller to notice that we've taken the 1400 * command from it. It's very inefficient, and we shouldn't do it, 1401 * but if we remove this code, we stop completing commands under 1402 * load. 1403 * 1404 * Peter J says we shouldn't do this. The documentation says we 1405 * should. Who is right? 1406 */ 1407 while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK) 1408 ; /* XXX aiee! what if it dies? */ 1409#endif 1410 1411 worked = 1; /* got some work */ 1412 } 1413 1414 splx(s); 1415 return(worked); 1416} 1417 1418static int 1419amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave) 1420{ 1421 int s, worked; 1422 u_int8_t istat; 1423 1424 debug_called(3); 1425 1426 worked = 0; 1427 s = splbio(); 1428 1429 /* check for valid interrupt status */ 1430 istat = AMR_SGET_ISTAT(sc); 1431 if ((istat & AMR_SINTR_VALID) != 0) { 1432 AMR_SPUT_ISTAT(sc, istat); /* ack interrupt status */ 1433 1434 /* save mailbox, which contains a list of completed commands */ 1435 bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave)); 1436 1437 AMR_SACK_INTERRUPT(sc); /* acknowledge we have the mailbox */ 1438 worked = 1; 1439 } 1440 1441 splx(s); 1442 return(worked); 1443} 1444 1445/******************************************************************************** 1446 * Notify the controller of the mailbox location. 1447 */ 1448static void 1449amr_std_attach_mailbox(struct amr_softc *sc) 1450{ 1451 1452 /* program the mailbox physical address */ 1453 AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys & 0xff); 1454 AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >> 8) & 0xff); 1455 AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff); 1456 AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff); 1457 AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR); 1458 1459 /* clear any outstanding interrupt and enable interrupts proper */ 1460 AMR_SACK_INTERRUPT(sc); 1461 AMR_SENABLE_INTR(sc); 1462} 1463 1464#ifdef AMR_BOARD_INIT 1465/******************************************************************************** 1466 * Initialise the controller 1467 */ 1468static int 1469amr_quartz_init(struct amr_softc *sc) 1470{ 1471 int status, ostatus; 1472 1473 device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc)); 1474 1475 AMR_QRESET(sc); 1476 1477 ostatus = 0xff; 1478 while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) { 1479 if (status != ostatus) { 1480 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status)); 1481 ostatus = status; 1482 } 1483 switch (status) { 1484 case AMR_QINIT_NOMEM: 1485 return(ENOMEM); 1486 1487 case AMR_QINIT_SCAN: 1488 /* XXX we could print channel/target here */ 1489 break; 1490 } 1491 } 1492 return(0); 1493} 1494 1495static int 1496amr_std_init(struct amr_softc *sc) 1497{ 1498 int status, ostatus; 1499 1500 device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc)); 1501 1502 AMR_SRESET(sc); 1503 1504 ostatus = 0xff; 1505 while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) { 1506 if (status != ostatus) { 1507 device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status)); 1508 ostatus = status; 1509 } 1510 switch (status) { 1511 case AMR_SINIT_NOMEM: 1512 return(ENOMEM); 1513 1514 case AMR_SINIT_INPROG: 1515 /* XXX we could print channel/target here? */ 1516 break; 1517 } 1518 } 1519 return(0); 1520} 1521#endif 1522 1523/******************************************************************************** 1524 ******************************************************************************** 1525 Debugging 1526 ******************************************************************************** 1527 ********************************************************************************/ 1528 1529/******************************************************************************** 1530 * Identify the controller and print some information about it. 1531 */ 1532static void 1533amr_describe_controller(struct amr_softc *sc) 1534{ 1535 struct amr_prodinfo *ap; 1536 struct amr_enquiry *ae; 1537 char *prod; 1538 1539 /* 1540 * Try to get 40LD product info, which tells us what the card is labelled as. 1541 */ 1542 if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) != NULL) { 1543 device_printf(sc->amr_dev, "<%.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n", 1544 ap->ap_product, ap->ap_firmware, ap->ap_bios, 1545 ap->ap_memsize); 1546 1547 free(ap, M_DEVBUF); 1548 return; 1549 } 1550 1551 /* 1552 * Try 8LD extended ENQUIRY to get controller signature, and use lookup table. 1553 */ 1554 if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) { 1555 prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature); 1556 1557 } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) != NULL) { 1558 1559 /* 1560 * Try to work it out based on the PCI signatures. 1561 */ 1562 switch (pci_get_device(sc->amr_dev)) { 1563 case 0x9010: 1564 prod = "Series 428"; 1565 break; 1566 case 0x9060: 1567 prod = "Series 434"; 1568 break; 1569 default: 1570 prod = "unknown controller"; 1571 break; 1572 } 1573 } else { 1574 prod = "unsupported controller"; 1575 } 1576 1577 /* 1578 * HP NetRaid controllers have a special encoding of the firmware and 1579 * BIOS versions. The AMI version seems to have it as strings whereas 1580 * the HP version does it with a leading uppercase character and two 1581 * binary numbers. 1582 */ 1583 1584 if(ae->ae_adapter.aa_firmware[2] >= 'A' && 1585 ae->ae_adapter.aa_firmware[2] <= 'Z' && 1586 ae->ae_adapter.aa_firmware[1] < ' ' && 1587 ae->ae_adapter.aa_firmware[0] < ' ' && 1588 ae->ae_adapter.aa_bios[2] >= 'A' && 1589 ae->ae_adapter.aa_bios[2] <= 'Z' && 1590 ae->ae_adapter.aa_bios[1] < ' ' && 1591 ae->ae_adapter.aa_bios[0] < ' ') { 1592 1593 /* this looks like we have an HP NetRaid version of the MegaRaid */ 1594 1595 if(ae->ae_signature == AMR_SIG_438) { 1596 /* the AMI 438 is an NetRaid 3si in HP-land */ 1597 prod = "HP NetRaid 3si"; 1598 } 1599 1600 device_printf(sc->amr_dev, "<%s> Firmware %c.%02d.%02d, BIOS %c.%02d.%02d, %dMB RAM\n", 1601 prod, ae->ae_adapter.aa_firmware[2], 1602 ae->ae_adapter.aa_firmware[1], 1603 ae->ae_adapter.aa_firmware[0], 1604 ae->ae_adapter.aa_bios[2], 1605 ae->ae_adapter.aa_bios[1], 1606 ae->ae_adapter.aa_bios[0], 1607 ae->ae_adapter.aa_memorysize); 1608 } else { 1609 device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n", 1610 prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios, 1611 ae->ae_adapter.aa_memorysize); 1612 } 1613 free(ae, M_DEVBUF); 1614} 1615 1616#ifdef AMR_DEBUG 1617/******************************************************************************** 1618 * Print the command (ac) in human-readable format 1619 */ 1620static void 1621amr_printcommand(struct amr_command *ac) 1622{ 1623 struct amr_softc *sc = ac->ac_sc; 1624 struct amr_sgentry *sg; 1625 int i; 1626 1627 device_printf(sc->amr_dev, "cmd %x ident %d drive %d\n", 1628 ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive); 1629 device_printf(sc->amr_dev, "blkcount %d lba %d\n", 1630 ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba); 1631 device_printf(sc->amr_dev, "virtaddr %p length %lu\n", ac->ac_data, (unsigned long)ac->ac_length); 1632 device_printf(sc->amr_dev, "sg physaddr %08x nsg %d\n", 1633 ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem); 1634 device_printf(sc->amr_dev, "ccb %p bio %p\n", ac->ac_ccb_data, ac->ac_bio); 1635 1636 /* get base address of s/g table */ 1637 sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG); 1638 for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++) 1639 device_printf(sc->amr_dev, " %x/%d\n", sg->sg_addr, sg->sg_count); 1640} 1641#endif 1642