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