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