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