1/*- 2 * Generic driver for the BusLogic MultiMaster SCSI host adapters 3 * Product specific probe and attach routines can be found in: 4 * sys/dev/buslogic/bt_isa.c BT-54X, BT-445 cards 5 * sys/dev/buslogic/bt_mca.c BT-64X, SDC3211B, SDC3211F 6 * sys/dev/buslogic/bt_eisa.c BT-74X, BT-75x cards, SDC3222F 7 * sys/dev/buslogic/bt_pci.c BT-946, BT-948, BT-956, BT-958 cards 8 * 9 * Copyright (c) 1998, 1999 Justin T. Gibbs. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions, and the following disclaimer, 17 * without modification, immediately at the beginning of the file. 18 * 2. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD$"); 36 37 /* 38 * Special thanks to Leonard N. Zubkoff for writing such a complete and 39 * well documented Mylex/BusLogic MultiMaster driver for Linux. Support 40 * in this driver for the wide range of MultiMaster controllers and 41 * firmware revisions, with their otherwise undocumented quirks, would not 42 * have been possible without his efforts. 43 */ 44 45#include <sys/param.h> 46#include <sys/conf.h> 47#include <sys/systm.h> 48#include <sys/malloc.h> 49#include <sys/kernel.h> 50#include <sys/lock.h> 51#include <sys/module.h> 52#include <sys/mutex.h> 53#include <sys/sysctl.h> 54#include <sys/bus.h> 55 56#include <machine/bus.h> 57#include <sys/rman.h> 58 59#include <cam/cam.h> 60#include <cam/cam_ccb.h> 61#include <cam/cam_sim.h> 62#include <cam/cam_xpt_sim.h> 63#include <cam/cam_debug.h> 64 65#include <cam/scsi/scsi_message.h> 66 67#include <vm/vm.h> 68#include <vm/pmap.h> 69 70#include <dev/buslogic/btreg.h> 71 72/* MailBox Management functions */ 73static __inline void btnextinbox(struct bt_softc *bt); 74static __inline void btnextoutbox(struct bt_softc *bt); 75 76static __inline void 77btnextinbox(struct bt_softc *bt) 78{ 79 if (bt->cur_inbox == bt->last_inbox) 80 bt->cur_inbox = bt->in_boxes; 81 else 82 bt->cur_inbox++; 83} 84 85static __inline void 86btnextoutbox(struct bt_softc *bt) 87{ 88 if (bt->cur_outbox == bt->last_outbox) 89 bt->cur_outbox = bt->out_boxes; 90 else 91 bt->cur_outbox++; 92} 93 94/* CCB Mangement functions */ 95static __inline u_int32_t btccbvtop(struct bt_softc *bt, 96 struct bt_ccb *bccb); 97static __inline struct bt_ccb* btccbptov(struct bt_softc *bt, 98 u_int32_t ccb_addr); 99static __inline u_int32_t btsensepaddr(struct bt_softc *bt, 100 struct bt_ccb *bccb); 101static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt, 102 struct bt_ccb *bccb); 103 104static __inline u_int32_t 105btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb) 106{ 107 return (bt->bt_ccb_physbase 108 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array)); 109} 110 111static __inline struct bt_ccb * 112btccbptov(struct bt_softc *bt, u_int32_t ccb_addr) 113{ 114 return (bt->bt_ccb_array + 115 ((struct bt_ccb*)(uintptr_t)ccb_addr - (struct bt_ccb*)(uintptr_t)bt->bt_ccb_physbase)); 116} 117 118static __inline u_int32_t 119btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb) 120{ 121 u_int index; 122 123 index = (u_int)(bccb - bt->bt_ccb_array); 124 return (bt->sense_buffers_physbase 125 + (index * sizeof(struct scsi_sense_data))); 126} 127 128static __inline struct scsi_sense_data * 129btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb) 130{ 131 u_int index; 132 133 index = (u_int)(bccb - bt->bt_ccb_array); 134 return (bt->sense_buffers + index); 135} 136 137static __inline struct bt_ccb* btgetccb(struct bt_softc *bt); 138static __inline void btfreeccb(struct bt_softc *bt, 139 struct bt_ccb *bccb); 140static void btallocccbs(struct bt_softc *bt); 141static bus_dmamap_callback_t btexecuteccb; 142static void btdone(struct bt_softc *bt, struct bt_ccb *bccb, 143 bt_mbi_comp_code_t comp_code); 144static void bt_intr_locked(struct bt_softc *bt); 145 146/* Host adapter command functions */ 147static int btreset(struct bt_softc* bt, int hard_reset); 148 149/* Initialization functions */ 150static int btinitmboxes(struct bt_softc *bt); 151static bus_dmamap_callback_t btmapmboxes; 152static bus_dmamap_callback_t btmapccbs; 153static bus_dmamap_callback_t btmapsgs; 154 155/* Transfer Negotiation Functions */ 156static void btfetchtransinfo(struct bt_softc *bt, 157 struct ccb_trans_settings *cts); 158 159/* CAM SIM entry points */ 160#define ccb_bccb_ptr spriv_ptr0 161#define ccb_bt_ptr spriv_ptr1 162static void btaction(struct cam_sim *sim, union ccb *ccb); 163static void btpoll(struct cam_sim *sim); 164 165/* Our timeout handler */ 166static void bttimeout(void *arg); 167 168/* 169 * XXX 170 * Do our own re-probe protection until a configuration 171 * manager can do it for us. This ensures that we don't 172 * reprobe a card already found by the EISA or PCI probes. 173 */ 174struct bt_isa_port bt_isa_ports[] = 175{ 176 { 0x130, 0, 4 }, 177 { 0x134, 0, 5 }, 178 { 0x230, 0, 2 }, 179 { 0x234, 0, 3 }, 180 { 0x330, 0, 0 }, 181 { 0x334, 0, 1 } 182}; 183 184/* 185 * I/O ports listed in the order enumerated by the 186 * card for certain op codes. 187 */ 188u_int16_t bt_board_ports[] = 189{ 190 0x330, 191 0x334, 192 0x230, 193 0x234, 194 0x130, 195 0x134 196}; 197 198/* Exported functions */ 199void 200bt_init_softc(device_t dev, struct resource *port, 201 struct resource *irq, struct resource *drq) 202{ 203 struct bt_softc *bt = device_get_softc(dev); 204 205 SLIST_INIT(&bt->free_bt_ccbs); 206 LIST_INIT(&bt->pending_ccbs); 207 SLIST_INIT(&bt->sg_maps); 208 bt->dev = dev; 209 bt->port = port; 210 bt->irq = irq; 211 bt->drq = drq; 212 mtx_init(&bt->lock, "bt", NULL, MTX_DEF); 213} 214 215void 216bt_free_softc(device_t dev) 217{ 218 struct bt_softc *bt = device_get_softc(dev); 219 220 switch (bt->init_level) { 221 default: 222 case 11: 223 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap); 224 case 10: 225 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers, 226 bt->sense_dmamap); 227 case 9: 228 bus_dma_tag_destroy(bt->sense_dmat); 229 case 8: 230 { 231 struct sg_map_node *sg_map; 232 233 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) { 234 SLIST_REMOVE_HEAD(&bt->sg_maps, links); 235 bus_dmamap_unload(bt->sg_dmat, 236 sg_map->sg_dmamap); 237 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr, 238 sg_map->sg_dmamap); 239 free(sg_map, M_DEVBUF); 240 } 241 bus_dma_tag_destroy(bt->sg_dmat); 242 } 243 case 7: 244 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap); 245 /* FALLTHROUGH */ 246 case 6: 247 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array, 248 bt->ccb_dmamap); 249 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap); 250 /* FALLTHROUGH */ 251 case 5: 252 bus_dma_tag_destroy(bt->ccb_dmat); 253 /* FALLTHROUGH */ 254 case 4: 255 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap); 256 /* FALLTHROUGH */ 257 case 3: 258 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes, 259 bt->mailbox_dmamap); 260 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap); 261 /* FALLTHROUGH */ 262 case 2: 263 bus_dma_tag_destroy(bt->buffer_dmat); 264 /* FALLTHROUGH */ 265 case 1: 266 bus_dma_tag_destroy(bt->mailbox_dmat); 267 /* FALLTHROUGH */ 268 case 0: 269 break; 270 } 271 mtx_destroy(&bt->lock); 272} 273 274int 275bt_port_probe(device_t dev, struct bt_probe_info *info) 276{ 277 struct bt_softc *bt = device_get_softc(dev); 278 config_data_t config_data; 279 int error; 280 281 /* See if there is really a card present */ 282 if (bt_probe(dev) || bt_fetch_adapter_info(dev)) 283 return(1); 284 285 /* 286 * Determine our IRQ, and DMA settings and 287 * export them to the configuration system. 288 */ 289 mtx_lock(&bt->lock); 290 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0, 291 (u_int8_t*)&config_data, sizeof(config_data), 292 DEFAULT_CMD_TIMEOUT); 293 mtx_unlock(&bt->lock); 294 if (error != 0) { 295 printf("bt_port_probe: Could not determine IRQ or DMA " 296 "settings for adapter.\n"); 297 return (1); 298 } 299 300 if (bt->model[0] == '5') { 301 /* DMA settings only make sense for ISA cards */ 302 switch (config_data.dma_chan) { 303 case DMA_CHAN_5: 304 info->drq = 5; 305 break; 306 case DMA_CHAN_6: 307 info->drq = 6; 308 break; 309 case DMA_CHAN_7: 310 info->drq = 7; 311 break; 312 default: 313 printf("bt_port_probe: Invalid DMA setting " 314 "detected for adapter.\n"); 315 return (1); 316 } 317 } else { 318 /* VL/EISA/PCI DMA */ 319 info->drq = -1; 320 } 321 switch (config_data.irq) { 322 case IRQ_9: 323 case IRQ_10: 324 case IRQ_11: 325 case IRQ_12: 326 case IRQ_14: 327 case IRQ_15: 328 info->irq = ffs(config_data.irq) + 8; 329 break; 330 default: 331 printf("bt_port_probe: Invalid IRQ setting %x" 332 "detected for adapter.\n", config_data.irq); 333 return (1); 334 } 335 return (0); 336} 337 338/* 339 * Probe the adapter and verify that the card is a BusLogic. 340 */ 341int 342bt_probe(device_t dev) 343{ 344 struct bt_softc *bt = device_get_softc(dev); 345 esetup_info_data_t esetup_info; 346 u_int status; 347 u_int intstat; 348 u_int geometry; 349 int error; 350 u_int8_t param; 351 352 /* 353 * See if the three I/O ports look reasonable. 354 * Touch the minimal number of registers in the 355 * failure case. 356 */ 357 status = bt_inb(bt, STATUS_REG); 358 if ((status == 0) 359 || (status & (DIAG_ACTIVE|CMD_REG_BUSY| 360 STATUS_REG_RSVD|CMD_INVALID)) != 0) { 361 if (bootverbose) 362 device_printf(dev, "Failed Status Reg Test - %x\n", 363 status); 364 return (ENXIO); 365 } 366 367 intstat = bt_inb(bt, INTSTAT_REG); 368 if ((intstat & INTSTAT_REG_RSVD) != 0) { 369 device_printf(dev, "Failed Intstat Reg Test\n"); 370 return (ENXIO); 371 } 372 373 geometry = bt_inb(bt, GEOMETRY_REG); 374 if (geometry == 0xFF) { 375 if (bootverbose) 376 device_printf(dev, "Failed Geometry Reg Test\n"); 377 return (ENXIO); 378 } 379 380 /* 381 * Looking good so far. Final test is to reset the 382 * adapter and attempt to fetch the extended setup 383 * information. This should filter out all 1542 cards. 384 */ 385 mtx_lock(&bt->lock); 386 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) { 387 mtx_unlock(&bt->lock); 388 if (bootverbose) 389 device_printf(dev, "Failed Reset\n"); 390 return (ENXIO); 391 } 392 393 param = sizeof(esetup_info); 394 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1, 395 (u_int8_t*)&esetup_info, sizeof(esetup_info), 396 DEFAULT_CMD_TIMEOUT); 397 mtx_unlock(&bt->lock); 398 if (error != 0) { 399 return (ENXIO); 400 } 401 402 return (0); 403} 404 405/* 406 * Pull the boards setup information and record it in our softc. 407 */ 408int 409bt_fetch_adapter_info(device_t dev) 410{ 411 struct bt_softc *bt = device_get_softc(dev); 412 board_id_data_t board_id; 413 esetup_info_data_t esetup_info; 414 config_data_t config_data; 415 int error; 416 u_int8_t length_param; 417 418 /* First record the firmware version */ 419 mtx_lock(&bt->lock); 420 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0, 421 (u_int8_t*)&board_id, sizeof(board_id), 422 DEFAULT_CMD_TIMEOUT); 423 if (error != 0) { 424 mtx_unlock(&bt->lock); 425 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n"); 426 return (error); 427 } 428 bt->firmware_ver[0] = board_id.firmware_rev_major; 429 bt->firmware_ver[1] = '.'; 430 bt->firmware_ver[2] = board_id.firmware_rev_minor; 431 bt->firmware_ver[3] = '\0'; 432 433 /* 434 * Depending on the firmware major and minor version, 435 * we may be able to fetch additional minor version info. 436 */ 437 if (bt->firmware_ver[0] > '0') { 438 439 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0, 440 (u_int8_t*)&bt->firmware_ver[3], 1, 441 DEFAULT_CMD_TIMEOUT); 442 if (error != 0) { 443 mtx_unlock(&bt->lock); 444 device_printf(dev, 445 "bt_fetch_adapter_info - Failed Get " 446 "Firmware 3rd Digit\n"); 447 return (error); 448 } 449 if (bt->firmware_ver[3] == ' ') 450 bt->firmware_ver[3] = '\0'; 451 bt->firmware_ver[4] = '\0'; 452 } 453 454 if (strcmp(bt->firmware_ver, "3.3") >= 0) { 455 456 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0, 457 (u_int8_t*)&bt->firmware_ver[4], 1, 458 DEFAULT_CMD_TIMEOUT); 459 if (error != 0) { 460 mtx_unlock(&bt->lock); 461 device_printf(dev, 462 "bt_fetch_adapter_info - Failed Get " 463 "Firmware 4th Digit\n"); 464 return (error); 465 } 466 if (bt->firmware_ver[4] == ' ') 467 bt->firmware_ver[4] = '\0'; 468 bt->firmware_ver[5] = '\0'; 469 } 470 471 /* 472 * Some boards do not handle the "recently documented" 473 * Inquire Board Model Number command correctly or do not give 474 * exact information. Use the Firmware and Extended Setup 475 * information in these cases to come up with the right answer. 476 * The major firmware revision number indicates: 477 * 478 * 5.xx BusLogic "W" Series Host Adapters: 479 * BT-948/958/958D 480 * 4.xx BusLogic "C" Series Host Adapters: 481 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF 482 * 3.xx BusLogic "S" Series Host Adapters: 483 * BT-747S/747D/757S/757D/445S/545S/542D 484 * BT-542B/742A (revision H) 485 * 2.xx BusLogic "A" Series Host Adapters: 486 * BT-542B/742A (revision G and below) 487 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter 488 */ 489 length_param = sizeof(esetup_info); 490 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1, 491 (u_int8_t*)&esetup_info, sizeof(esetup_info), 492 DEFAULT_CMD_TIMEOUT); 493 if (error != 0) { 494 mtx_unlock(&bt->lock); 495 return (error); 496 } 497 498 bt->bios_addr = esetup_info.bios_addr << 12; 499 500 bt->mailbox_addrlimit = BUS_SPACE_MAXADDR; 501 if (esetup_info.bus_type == 'A' 502 && bt->firmware_ver[0] == '2') { 503 snprintf(bt->model, sizeof(bt->model), "542B"); 504 } else if (esetup_info.bus_type == 'E' 505 && bt->firmware_ver[0] == '2') { 506 507 /* 508 * The 742A seems to object if its mailboxes are 509 * allocated above the 16MB mark. 510 */ 511 bt->mailbox_addrlimit = BUS_SPACE_MAXADDR_24BIT; 512 snprintf(bt->model, sizeof(bt->model), "742A"); 513 } else if (esetup_info.bus_type == 'E' 514 && bt->firmware_ver[0] == '0') { 515 /* AMI FastDisk EISA Series 441 0.x */ 516 snprintf(bt->model, sizeof(bt->model), "747A"); 517 } else { 518 ha_model_data_t model_data; 519 int i; 520 521 length_param = sizeof(model_data); 522 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1, 523 (u_int8_t*)&model_data, sizeof(model_data), 524 DEFAULT_CMD_TIMEOUT); 525 if (error != 0) { 526 mtx_unlock(&bt->lock); 527 device_printf(dev, 528 "bt_fetch_adapter_info - Failed Inquire " 529 "Model Number\n"); 530 return (error); 531 } 532 for (i = 0; i < sizeof(model_data.ascii_model); i++) { 533 bt->model[i] = model_data.ascii_model[i]; 534 if (bt->model[i] == ' ') 535 break; 536 } 537 bt->model[i] = '\0'; 538 } 539 540 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0; 541 542 /* SG element limits */ 543 bt->max_sg = esetup_info.max_sg; 544 545 /* Set feature flags */ 546 bt->wide_bus = esetup_info.wide_bus; 547 bt->diff_bus = esetup_info.diff_bus; 548 bt->ultra_scsi = esetup_info.ultra_scsi; 549 550 if ((bt->firmware_ver[0] == '5') 551 || (bt->firmware_ver[0] == '4' && bt->wide_bus)) 552 bt->extended_lun = TRUE; 553 554 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0); 555 556 bt->extended_trans = 557 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0); 558 559 /* 560 * Determine max CCB count and whether tagged queuing is 561 * available based on controller type. Tagged queuing 562 * only works on 'W' series adapters, 'C' series adapters 563 * with firmware of rev 4.42 and higher, and 'S' series 564 * adapters with firmware of rev 3.35 and higher. The 565 * maximum CCB counts are as follows: 566 * 567 * 192 BT-948/958/958D 568 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C 569 * 50 BT-545C/540CF 570 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A 571 */ 572 if (bt->firmware_ver[0] == '5') { 573 bt->max_ccbs = 192; 574 bt->tag_capable = TRUE; 575 } else if (bt->firmware_ver[0] == '4') { 576 if (bt->model[0] == '5') 577 bt->max_ccbs = 50; 578 else 579 bt->max_ccbs = 100; 580 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0); 581 } else { 582 bt->max_ccbs = 30; 583 if (bt->firmware_ver[0] == '3' 584 && (strcmp(bt->firmware_ver, "3.35") >= 0)) 585 bt->tag_capable = TRUE; 586 else 587 bt->tag_capable = FALSE; 588 } 589 590 if (bt->tag_capable != FALSE) 591 bt->tags_permitted = ALL_TARGETS; 592 593 /* Determine Sync/Wide/Disc settings */ 594 if (bt->firmware_ver[0] >= '4') { 595 auto_scsi_data_t auto_scsi_data; 596 fetch_lram_params_t fetch_lram_params; 597 int error; 598 599 /* 600 * These settings are stored in the 601 * AutoSCSI data in LRAM of 'W' and 'C' 602 * adapters. 603 */ 604 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET; 605 fetch_lram_params.response_len = sizeof(auto_scsi_data); 606 error = bt_cmd(bt, BOP_FETCH_LRAM, 607 (u_int8_t*)&fetch_lram_params, 608 sizeof(fetch_lram_params), 609 (u_int8_t*)&auto_scsi_data, 610 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT); 611 612 if (error != 0) { 613 mtx_unlock(&bt->lock); 614 device_printf(dev, 615 "bt_fetch_adapter_info - Failed " 616 "Get Auto SCSI Info\n"); 617 return (error); 618 } 619 620 bt->disc_permitted = auto_scsi_data.low_disc_permitted 621 | (auto_scsi_data.high_disc_permitted << 8); 622 bt->sync_permitted = auto_scsi_data.low_sync_permitted 623 | (auto_scsi_data.high_sync_permitted << 8); 624 bt->fast_permitted = auto_scsi_data.low_fast_permitted 625 | (auto_scsi_data.high_fast_permitted << 8); 626 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted 627 | (auto_scsi_data.high_ultra_permitted << 8); 628 bt->wide_permitted = auto_scsi_data.low_wide_permitted 629 | (auto_scsi_data.high_wide_permitted << 8); 630 631 if (bt->ultra_scsi == FALSE) 632 bt->ultra_permitted = 0; 633 634 if (bt->wide_bus == FALSE) 635 bt->wide_permitted = 0; 636 } else { 637 /* 638 * 'S' and 'A' series have this information in the setup 639 * information structure. 640 */ 641 setup_data_t setup_info; 642 643 length_param = sizeof(setup_info); 644 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param, 645 /*paramlen*/1, (u_int8_t*)&setup_info, 646 sizeof(setup_info), DEFAULT_CMD_TIMEOUT); 647 648 if (error != 0) { 649 mtx_unlock(&bt->lock); 650 device_printf(dev, 651 "bt_fetch_adapter_info - Failed " 652 "Get Setup Info\n"); 653 return (error); 654 } 655 656 if (setup_info.initiate_sync != 0) { 657 bt->sync_permitted = ALL_TARGETS; 658 659 if (bt->model[0] == '7') { 660 if (esetup_info.sync_neg10MB != 0) 661 bt->fast_permitted = ALL_TARGETS; 662 if (strcmp(bt->model, "757") == 0) 663 bt->wide_permitted = ALL_TARGETS; 664 } 665 } 666 bt->disc_permitted = ALL_TARGETS; 667 } 668 669 /* We need as many mailboxes as we can have ccbs */ 670 bt->num_boxes = bt->max_ccbs; 671 672 /* Determine our SCSI ID */ 673 674 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0, 675 (u_int8_t*)&config_data, sizeof(config_data), 676 DEFAULT_CMD_TIMEOUT); 677 mtx_unlock(&bt->lock); 678 if (error != 0) { 679 device_printf(dev, 680 "bt_fetch_adapter_info - Failed Get Config\n"); 681 return (error); 682 } 683 bt->scsi_id = config_data.scsi_id; 684 685 return (0); 686} 687 688/* 689 * Start the board, ready for normal operation 690 */ 691int 692bt_init(device_t dev) 693{ 694 struct bt_softc *bt = device_get_softc(dev); 695 696 /* Announce the Adapter */ 697 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver); 698 699 if (bt->ultra_scsi != 0) 700 printf("Ultra "); 701 702 if (bt->wide_bus != 0) 703 printf("Wide "); 704 else 705 printf("Narrow "); 706 707 if (bt->diff_bus != 0) 708 printf("Diff "); 709 710 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id, 711 bt->max_ccbs); 712 713 /* 714 * Create our DMA tags. These tags define the kinds of device 715 * accessible memory allocations and memory mappings we will 716 * need to perform during normal operation. 717 * 718 * Unless we need to further restrict the allocation, we rely 719 * on the restrictions of the parent dmat, hence the common 720 * use of MAXADDR and MAXSIZE. 721 */ 722 723 /* DMA tag for mapping buffers into device visible space. */ 724 if (bus_dma_tag_create( /* parent */ bt->parent_dmat, 725 /* alignment */ 1, 726 /* boundary */ 0, 727 /* lowaddr */ BUS_SPACE_MAXADDR, 728 /* highaddr */ BUS_SPACE_MAXADDR, 729 /* filter */ NULL, 730 /* filterarg */ NULL, 731 /* maxsize */ MAXBSIZE, 732 /* nsegments */ BT_NSEG, 733 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT, 734 /* flags */ BUS_DMA_ALLOCNOW, 735 /* lockfunc */ busdma_lock_mutex, 736 /* lockarg */ &bt->lock, 737 &bt->buffer_dmat) != 0) { 738 goto error_exit; 739 } 740 741 bt->init_level++; 742 /* DMA tag for our mailboxes */ 743 if (bus_dma_tag_create( /* parent */ bt->parent_dmat, 744 /* alignment */ 1, 745 /* boundary */ 0, 746 /* lowaddr */ bt->mailbox_addrlimit, 747 /* highaddr */ BUS_SPACE_MAXADDR, 748 /* filter */ NULL, 749 /* filterarg */ NULL, 750 /* maxsize */ bt->num_boxes * 751 (sizeof(bt_mbox_in_t) + 752 sizeof(bt_mbox_out_t)), 753 /* nsegments */ 1, 754 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT, 755 /* flags */ 0, 756 /* lockfunc */ NULL, 757 /* lockarg */ NULL, 758 &bt->mailbox_dmat) != 0) { 759 goto error_exit; 760 } 761 762 bt->init_level++; 763 764 /* Allocation for our mailboxes */ 765 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes, 766 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) { 767 goto error_exit; 768 } 769 770 bt->init_level++; 771 772 /* And permanently map them */ 773 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap, 774 bt->out_boxes, 775 bt->num_boxes * (sizeof(bt_mbox_in_t) 776 + sizeof(bt_mbox_out_t)), 777 btmapmboxes, bt, /*flags*/0); 778 779 bt->init_level++; 780 781 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes]; 782 783 mtx_lock(&bt->lock); 784 btinitmboxes(bt); 785 mtx_unlock(&bt->lock); 786 787 /* DMA tag for our ccb structures */ 788 if (bus_dma_tag_create( /* parent */ bt->parent_dmat, 789 /* alignment */ 1, 790 /* boundary */ 0, 791 /* lowaddr */ BUS_SPACE_MAXADDR, 792 /* highaddr */ BUS_SPACE_MAXADDR, 793 /* filter */ NULL, 794 /* filterarg */ NULL, 795 /* maxsize */ bt->max_ccbs * 796 sizeof(struct bt_ccb), 797 /* nsegments */ 1, 798 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT, 799 /* flags */ 0, 800 /* lockfunc */ NULL, 801 /* lockarg */ NULL, 802 &bt->ccb_dmat) != 0) { 803 goto error_exit; 804 } 805 806 bt->init_level++; 807 808 /* Allocation for our ccbs */ 809 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array, 810 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) { 811 goto error_exit; 812 } 813 814 bt->init_level++; 815 816 /* And permanently map them */ 817 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap, 818 bt->bt_ccb_array, 819 bt->max_ccbs * sizeof(struct bt_ccb), 820 btmapccbs, bt, /*flags*/0); 821 822 bt->init_level++; 823 824 /* DMA tag for our S/G structures. We allocate in page sized chunks */ 825 if (bus_dma_tag_create( /* parent */ bt->parent_dmat, 826 /* alignment */ 1, 827 /* boundary */ 0, 828 /* lowaddr */ BUS_SPACE_MAXADDR, 829 /* highaddr */ BUS_SPACE_MAXADDR, 830 /* filter */ NULL, 831 /* filterarg */ NULL, 832 /* maxsize */ PAGE_SIZE, 833 /* nsegments */ 1, 834 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT, 835 /* flags */ 0, 836 /* lockfunc */ NULL, 837 /* lockarg */ NULL, 838 &bt->sg_dmat) != 0) { 839 goto error_exit; 840 } 841 842 bt->init_level++; 843 844 /* Perform initial CCB allocation */ 845 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb)); 846 btallocccbs(bt); 847 848 if (bt->num_ccbs == 0) { 849 device_printf(dev, 850 "bt_init - Unable to allocate initial ccbs\n"); 851 goto error_exit; 852 } 853 854 /* 855 * Note that we are going and return (to attach) 856 */ 857 return 0; 858 859error_exit: 860 861 return (ENXIO); 862} 863 864int 865bt_attach(device_t dev) 866{ 867 struct bt_softc *bt = device_get_softc(dev); 868 int tagged_dev_openings; 869 struct cam_devq *devq; 870 int error; 871 872 /* 873 * We reserve 1 ccb for error recovery, so don't 874 * tell the XPT about it. 875 */ 876 if (bt->tag_capable != 0) 877 tagged_dev_openings = bt->max_ccbs - 1; 878 else 879 tagged_dev_openings = 0; 880 881 /* 882 * Create the device queue for our SIM. 883 */ 884 devq = cam_simq_alloc(bt->max_ccbs - 1); 885 if (devq == NULL) 886 return (ENOMEM); 887 888 /* 889 * Construct our SIM entry 890 */ 891 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, 892 device_get_unit(bt->dev), &bt->lock, 2, tagged_dev_openings, devq); 893 if (bt->sim == NULL) { 894 cam_simq_free(devq); 895 return (ENOMEM); 896 } 897 898 mtx_lock(&bt->lock); 899 if (xpt_bus_register(bt->sim, dev, 0) != CAM_SUCCESS) { 900 cam_sim_free(bt->sim, /*free_devq*/TRUE); 901 mtx_unlock(&bt->lock); 902 return (ENXIO); 903 } 904 905 if (xpt_create_path(&bt->path, /*periph*/NULL, 906 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD, 907 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 908 xpt_bus_deregister(cam_sim_path(bt->sim)); 909 cam_sim_free(bt->sim, /*free_devq*/TRUE); 910 mtx_unlock(&bt->lock); 911 return (ENXIO); 912 } 913 mtx_unlock(&bt->lock); 914 915 /* 916 * Setup interrupt. 917 */ 918 error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM | INTR_ENTROPY | 919 INTR_MPSAFE, NULL, bt_intr, bt, &bt->ih); 920 if (error) { 921 device_printf(dev, "bus_setup_intr() failed: %d\n", error); 922 return (error); 923 } 924 925 return (0); 926} 927 928int 929bt_check_probed_iop(u_int ioport) 930{ 931 u_int i; 932 933 for (i = 0; i < BT_NUM_ISAPORTS; i++) { 934 if (bt_isa_ports[i].addr == ioport) { 935 if (bt_isa_ports[i].probed != 0) 936 return (1); 937 else { 938 return (0); 939 } 940 } 941 } 942 return (1); 943} 944 945void 946bt_mark_probed_bio(isa_compat_io_t port) 947{ 948 if (port < BIO_DISABLED) 949 bt_mark_probed_iop(bt_board_ports[port]); 950} 951 952void 953bt_mark_probed_iop(u_int ioport) 954{ 955 u_int i; 956 957 for (i = 0; i < BT_NUM_ISAPORTS; i++) { 958 if (ioport == bt_isa_ports[i].addr) { 959 bt_isa_ports[i].probed = 1; 960 break; 961 } 962 } 963} 964 965void 966bt_find_probe_range(int ioport, int *port_index, int *max_port_index) 967{ 968 if (ioport > 0) { 969 int i; 970 971 for (i = 0;i < BT_NUM_ISAPORTS; i++) 972 if (ioport <= bt_isa_ports[i].addr) 973 break; 974 if ((i >= BT_NUM_ISAPORTS) 975 || (ioport != bt_isa_ports[i].addr)) { 976 printf( 977"bt_find_probe_range: Invalid baseport of 0x%x specified.\n" 978"bt_find_probe_range: Nearest valid baseport is 0x%x.\n" 979"bt_find_probe_range: Failing probe.\n", 980 ioport, 981 (i < BT_NUM_ISAPORTS) 982 ? bt_isa_ports[i].addr 983 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr); 984 *port_index = *max_port_index = -1; 985 return; 986 } 987 *port_index = *max_port_index = bt_isa_ports[i].bio; 988 } else { 989 *port_index = 0; 990 *max_port_index = BT_NUM_ISAPORTS - 1; 991 } 992} 993 994int 995bt_iop_from_bio(isa_compat_io_t bio_index) 996{ 997 if (bio_index < BT_NUM_ISAPORTS) 998 return (bt_board_ports[bio_index]); 999 return (-1); 1000} 1001 1002 1003static void 1004btallocccbs(struct bt_softc *bt) 1005{ 1006 struct bt_ccb *next_ccb; 1007 struct sg_map_node *sg_map; 1008 bus_addr_t physaddr; 1009 bt_sg_t *segs; 1010 int newcount; 1011 int i; 1012 1013 if (bt->num_ccbs >= bt->max_ccbs) 1014 /* Can't allocate any more */ 1015 return; 1016 1017 next_ccb = &bt->bt_ccb_array[bt->num_ccbs]; 1018 1019 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT); 1020 1021 if (sg_map == NULL) 1022 goto error_exit; 1023 1024 /* Allocate S/G space for the next batch of CCBS */ 1025 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr, 1026 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) { 1027 free(sg_map, M_DEVBUF); 1028 goto error_exit; 1029 } 1030 1031 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links); 1032 1033 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr, 1034 PAGE_SIZE, btmapsgs, bt, /*flags*/0); 1035 1036 segs = sg_map->sg_vaddr; 1037 physaddr = sg_map->sg_physaddr; 1038 1039 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t))); 1040 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) { 1041 int error; 1042 1043 next_ccb->sg_list = segs; 1044 next_ccb->sg_list_phys = physaddr; 1045 next_ccb->flags = BCCB_FREE; 1046 callout_init_mtx(&next_ccb->timer, &bt->lock, 0); 1047 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0, 1048 &next_ccb->dmamap); 1049 if (error != 0) 1050 break; 1051 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links); 1052 segs += BT_NSEG; 1053 physaddr += (BT_NSEG * sizeof(bt_sg_t)); 1054 next_ccb++; 1055 bt->num_ccbs++; 1056 } 1057 1058 /* Reserve a CCB for error recovery */ 1059 if (bt->recovery_bccb == NULL) { 1060 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs); 1061 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links); 1062 } 1063 1064 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL) 1065 return; 1066 1067error_exit: 1068 device_printf(bt->dev, "Can't malloc BCCBs\n"); 1069} 1070 1071static __inline void 1072btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb) 1073{ 1074 1075 if (!dumping) 1076 mtx_assert(&bt->lock, MA_OWNED); 1077 if ((bccb->flags & BCCB_ACTIVE) != 0) 1078 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le); 1079 if (bt->resource_shortage != 0 1080 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 1081 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1082 bt->resource_shortage = FALSE; 1083 } 1084 bccb->flags = BCCB_FREE; 1085 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links); 1086 bt->active_ccbs--; 1087} 1088 1089static __inline struct bt_ccb* 1090btgetccb(struct bt_softc *bt) 1091{ 1092 struct bt_ccb* bccb; 1093 1094 if (!dumping) 1095 mtx_assert(&bt->lock, MA_OWNED); 1096 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) { 1097 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links); 1098 bt->active_ccbs++; 1099 } else { 1100 btallocccbs(bt); 1101 bccb = SLIST_FIRST(&bt->free_bt_ccbs); 1102 if (bccb != NULL) { 1103 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links); 1104 bt->active_ccbs++; 1105 } 1106 } 1107 1108 return (bccb); 1109} 1110 1111static void 1112btaction(struct cam_sim *sim, union ccb *ccb) 1113{ 1114 struct bt_softc *bt; 1115 1116 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n")); 1117 1118 bt = (struct bt_softc *)cam_sim_softc(sim); 1119 mtx_assert(&bt->lock, MA_OWNED); 1120 1121 switch (ccb->ccb_h.func_code) { 1122 /* Common cases first */ 1123 case XPT_SCSI_IO: /* Execute the requested I/O operation */ 1124 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 1125 { 1126 struct bt_ccb *bccb; 1127 struct bt_hccb *hccb; 1128 1129 /* 1130 * get a bccb to use. 1131 */ 1132 if ((bccb = btgetccb(bt)) == NULL) { 1133 1134 bt->resource_shortage = TRUE; 1135 xpt_freeze_simq(bt->sim, /*count*/1); 1136 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1137 xpt_done(ccb); 1138 return; 1139 } 1140 1141 hccb = &bccb->hccb; 1142 1143 /* 1144 * So we can find the BCCB when an abort is requested 1145 */ 1146 bccb->ccb = ccb; 1147 ccb->ccb_h.ccb_bccb_ptr = bccb; 1148 ccb->ccb_h.ccb_bt_ptr = bt; 1149 1150 /* 1151 * Put all the arguments for the xfer in the bccb 1152 */ 1153 hccb->target_id = ccb->ccb_h.target_id; 1154 hccb->target_lun = ccb->ccb_h.target_lun; 1155 hccb->btstat = 0; 1156 hccb->sdstat = 0; 1157 1158 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1159 struct ccb_scsiio *csio; 1160 struct ccb_hdr *ccbh; 1161 int error; 1162 1163 csio = &ccb->csio; 1164 ccbh = &csio->ccb_h; 1165 hccb->opcode = INITIATOR_CCB_WRESID; 1166 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0; 1167 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0; 1168 hccb->cmd_len = csio->cdb_len; 1169 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) { 1170 ccb->ccb_h.status = CAM_REQ_INVALID; 1171 btfreeccb(bt, bccb); 1172 xpt_done(ccb); 1173 return; 1174 } 1175 hccb->sense_len = csio->sense_len; 1176 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0 1177 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) { 1178 hccb->tag_enable = TRUE; 1179 hccb->tag_type = (ccb->csio.tag_action & 0x3); 1180 } else { 1181 hccb->tag_enable = FALSE; 1182 hccb->tag_type = 0; 1183 } 1184 if ((ccbh->flags & CAM_CDB_POINTER) != 0) { 1185 if ((ccbh->flags & CAM_CDB_PHYS) == 0) { 1186 bcopy(csio->cdb_io.cdb_ptr, 1187 hccb->scsi_cdb, hccb->cmd_len); 1188 } else { 1189 /* I guess I could map it in... */ 1190 ccbh->status = CAM_REQ_INVALID; 1191 btfreeccb(bt, bccb); 1192 xpt_done(ccb); 1193 return; 1194 } 1195 } else { 1196 bcopy(csio->cdb_io.cdb_bytes, 1197 hccb->scsi_cdb, hccb->cmd_len); 1198 } 1199 /* If need be, bounce our sense buffer */ 1200 if (bt->sense_buffers != NULL) { 1201 hccb->sense_addr = btsensepaddr(bt, bccb); 1202 } else { 1203 hccb->sense_addr = vtophys(&csio->sense_data); 1204 } 1205 /* 1206 * If we have any data to send with this command, 1207 * map it into bus space. 1208 */ 1209 error = bus_dmamap_load_ccb( 1210 bt->buffer_dmat, 1211 bccb->dmamap, 1212 ccb, 1213 btexecuteccb, 1214 bccb, 1215 /*flags*/0); 1216 if (error == EINPROGRESS) { 1217 /* 1218 * So as to maintain ordering, freeze the 1219 * controller queue until our mapping is 1220 * returned. 1221 */ 1222 xpt_freeze_simq(bt->sim, 1); 1223 csio->ccb_h.status |= CAM_RELEASE_SIMQ; 1224 } 1225 } else { 1226 hccb->opcode = INITIATOR_BUS_DEV_RESET; 1227 /* No data transfer */ 1228 hccb->datain = TRUE; 1229 hccb->dataout = TRUE; 1230 hccb->cmd_len = 0; 1231 hccb->sense_len = 0; 1232 hccb->tag_enable = FALSE; 1233 hccb->tag_type = 0; 1234 btexecuteccb(bccb, NULL, 0, 0); 1235 } 1236 break; 1237 } 1238 case XPT_EN_LUN: /* Enable LUN as a target */ 1239 case XPT_TARGET_IO: /* Execute target I/O request */ 1240 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 1241 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/ 1242 case XPT_ABORT: /* Abort the specified CCB */ 1243 /* XXX Implement */ 1244 ccb->ccb_h.status = CAM_REQ_INVALID; 1245 xpt_done(ccb); 1246 break; 1247 case XPT_SET_TRAN_SETTINGS: 1248 { 1249 /* XXX Implement */ 1250 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 1251 xpt_done(ccb); 1252 break; 1253 } 1254 case XPT_GET_TRAN_SETTINGS: 1255 /* Get default/user set transfer settings for the target */ 1256 { 1257 struct ccb_trans_settings *cts; 1258 u_int target_mask; 1259 1260 cts = &ccb->cts; 1261 target_mask = 0x01 << ccb->ccb_h.target_id; 1262 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) { 1263 struct ccb_trans_settings_scsi *scsi = 1264 &cts->proto_specific.scsi; 1265 struct ccb_trans_settings_spi *spi = 1266 &cts->xport_specific.spi; 1267 cts->protocol = PROTO_SCSI; 1268 cts->protocol_version = SCSI_REV_2; 1269 cts->transport = XPORT_SPI; 1270 cts->transport_version = 2; 1271 1272 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 1273 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB; 1274 1275 if ((bt->disc_permitted & target_mask) != 0) 1276 spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 1277 if ((bt->tags_permitted & target_mask) != 0) 1278 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 1279 1280 if ((bt->ultra_permitted & target_mask) != 0) 1281 spi->sync_period = 12; 1282 else if ((bt->fast_permitted & target_mask) != 0) 1283 spi->sync_period = 25; 1284 else if ((bt->sync_permitted & target_mask) != 0) 1285 spi->sync_period = 50; 1286 else 1287 spi->sync_period = 0; 1288 1289 if (spi->sync_period != 0) 1290 spi->sync_offset = 15; 1291 1292 spi->valid |= CTS_SPI_VALID_SYNC_RATE; 1293 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET; 1294 1295 spi->valid |= CTS_SPI_VALID_BUS_WIDTH; 1296 if ((bt->wide_permitted & target_mask) != 0) 1297 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT; 1298 else 1299 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT; 1300 1301 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) { 1302 scsi->valid = CTS_SCSI_VALID_TQ; 1303 spi->valid |= CTS_SPI_VALID_DISC; 1304 } else 1305 scsi->valid = 0; 1306 } else { 1307 btfetchtransinfo(bt, cts); 1308 } 1309 1310 ccb->ccb_h.status = CAM_REQ_CMP; 1311 xpt_done(ccb); 1312 break; 1313 } 1314 case XPT_CALC_GEOMETRY: 1315 { 1316 struct ccb_calc_geometry *ccg; 1317 u_int32_t size_mb; 1318 u_int32_t secs_per_cylinder; 1319 1320 ccg = &ccb->ccg; 1321 size_mb = ccg->volume_size 1322 / ((1024L * 1024L) / ccg->block_size); 1323 1324 if (size_mb >= 1024 && (bt->extended_trans != 0)) { 1325 if (size_mb >= 2048) { 1326 ccg->heads = 255; 1327 ccg->secs_per_track = 63; 1328 } else { 1329 ccg->heads = 128; 1330 ccg->secs_per_track = 32; 1331 } 1332 } else { 1333 ccg->heads = 64; 1334 ccg->secs_per_track = 32; 1335 } 1336 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 1337 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 1338 ccb->ccb_h.status = CAM_REQ_CMP; 1339 xpt_done(ccb); 1340 break; 1341 } 1342 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1343 { 1344 btreset(bt, /*hardreset*/TRUE); 1345 ccb->ccb_h.status = CAM_REQ_CMP; 1346 xpt_done(ccb); 1347 break; 1348 } 1349 case XPT_TERM_IO: /* Terminate the I/O process */ 1350 /* XXX Implement */ 1351 ccb->ccb_h.status = CAM_REQ_INVALID; 1352 xpt_done(ccb); 1353 break; 1354 case XPT_PATH_INQ: /* Path routing inquiry */ 1355 { 1356 struct ccb_pathinq *cpi = &ccb->cpi; 1357 1358 cpi->version_num = 1; /* XXX??? */ 1359 cpi->hba_inquiry = PI_SDTR_ABLE; 1360 if (bt->tag_capable != 0) 1361 cpi->hba_inquiry |= PI_TAG_ABLE; 1362 if (bt->wide_bus != 0) 1363 cpi->hba_inquiry |= PI_WIDE_16; 1364 cpi->target_sprt = 0; 1365 cpi->hba_misc = 0; 1366 cpi->hba_eng_cnt = 0; 1367 cpi->max_target = bt->wide_bus ? 15 : 7; 1368 cpi->max_lun = 7; 1369 cpi->initiator_id = bt->scsi_id; 1370 cpi->bus_id = cam_sim_bus(sim); 1371 cpi->base_transfer_speed = 3300; 1372 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1373 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN); 1374 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 1375 cpi->unit_number = cam_sim_unit(sim); 1376 cpi->ccb_h.status = CAM_REQ_CMP; 1377 cpi->transport = XPORT_SPI; 1378 cpi->transport_version = 2; 1379 cpi->protocol = PROTO_SCSI; 1380 cpi->protocol_version = SCSI_REV_2; 1381 xpt_done(ccb); 1382 break; 1383 } 1384 default: 1385 ccb->ccb_h.status = CAM_REQ_INVALID; 1386 xpt_done(ccb); 1387 break; 1388 } 1389} 1390 1391static void 1392btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error) 1393{ 1394 struct bt_ccb *bccb; 1395 union ccb *ccb; 1396 struct bt_softc *bt; 1397 1398 bccb = (struct bt_ccb *)arg; 1399 ccb = bccb->ccb; 1400 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr; 1401 1402 if (error != 0) { 1403 if (error != EFBIG) 1404 device_printf(bt->dev, 1405 "Unexepected error 0x%x returned from " 1406 "bus_dmamap_load\n", error); 1407 if (ccb->ccb_h.status == CAM_REQ_INPROG) { 1408 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1); 1409 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN; 1410 } 1411 btfreeccb(bt, bccb); 1412 xpt_done(ccb); 1413 return; 1414 } 1415 1416 if (nseg != 0) { 1417 bt_sg_t *sg; 1418 bus_dma_segment_t *end_seg; 1419 bus_dmasync_op_t op; 1420 1421 end_seg = dm_segs + nseg; 1422 1423 /* Copy the segments into our SG list */ 1424 sg = bccb->sg_list; 1425 while (dm_segs < end_seg) { 1426 sg->len = dm_segs->ds_len; 1427 sg->addr = dm_segs->ds_addr; 1428 sg++; 1429 dm_segs++; 1430 } 1431 1432 if (nseg > 1) { 1433 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID; 1434 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg; 1435 bccb->hccb.data_addr = bccb->sg_list_phys; 1436 } else { 1437 bccb->hccb.data_len = bccb->sg_list->len; 1438 bccb->hccb.data_addr = bccb->sg_list->addr; 1439 } 1440 1441 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1442 op = BUS_DMASYNC_PREREAD; 1443 else 1444 op = BUS_DMASYNC_PREWRITE; 1445 1446 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op); 1447 1448 } else { 1449 bccb->hccb.opcode = INITIATOR_CCB; 1450 bccb->hccb.data_len = 0; 1451 bccb->hccb.data_addr = 0; 1452 } 1453 1454 /* 1455 * Last time we need to check if this CCB needs to 1456 * be aborted. 1457 */ 1458 if (ccb->ccb_h.status != CAM_REQ_INPROG) { 1459 if (nseg != 0) 1460 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap); 1461 btfreeccb(bt, bccb); 1462 xpt_done(ccb); 1463 return; 1464 } 1465 1466 bccb->flags = BCCB_ACTIVE; 1467 ccb->ccb_h.status |= CAM_SIM_QUEUED; 1468 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le); 1469 1470 callout_reset(&bccb->timer, (ccb->ccb_h.timeout * hz) / 1000, 1471 bttimeout, bccb); 1472 1473 /* Tell the adapter about this command */ 1474 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb); 1475 if (bt->cur_outbox->action_code != BMBO_FREE) { 1476 /* 1477 * We should never encounter a busy mailbox. 1478 * If we do, warn the user, and treat it as 1479 * a resource shortage. If the controller is 1480 * hung, one of the pending transactions will 1481 * timeout causing us to start recovery operations. 1482 */ 1483 device_printf(bt->dev, 1484 "Encountered busy mailbox with %d out of %d " 1485 "commands active!!!\n", bt->active_ccbs, 1486 bt->max_ccbs); 1487 callout_stop(&bccb->timer); 1488 if (nseg != 0) 1489 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap); 1490 btfreeccb(bt, bccb); 1491 bt->resource_shortage = TRUE; 1492 xpt_freeze_simq(bt->sim, /*count*/1); 1493 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1494 xpt_done(ccb); 1495 return; 1496 } 1497 bt->cur_outbox->action_code = BMBO_START; 1498 bt_outb(bt, COMMAND_REG, BOP_START_MBOX); 1499 btnextoutbox(bt); 1500} 1501 1502void 1503bt_intr(void *arg) 1504{ 1505 struct bt_softc *bt; 1506 1507 bt = arg; 1508 mtx_lock(&bt->lock); 1509 bt_intr_locked(bt); 1510 mtx_unlock(&bt->lock); 1511} 1512 1513void 1514bt_intr_locked(struct bt_softc *bt) 1515{ 1516 u_int intstat; 1517 1518 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) { 1519 1520 if ((intstat & CMD_COMPLETE) != 0) { 1521 bt->latched_status = bt_inb(bt, STATUS_REG); 1522 bt->command_cmp = TRUE; 1523 } 1524 1525 bt_outb(bt, CONTROL_REG, RESET_INTR); 1526 1527 if ((intstat & IMB_LOADED) != 0) { 1528 while (bt->cur_inbox->comp_code != BMBI_FREE) { 1529 btdone(bt, 1530 btccbptov(bt, bt->cur_inbox->ccb_addr), 1531 bt->cur_inbox->comp_code); 1532 bt->cur_inbox->comp_code = BMBI_FREE; 1533 btnextinbox(bt); 1534 } 1535 } 1536 1537 if ((intstat & SCSI_BUS_RESET) != 0) { 1538 btreset(bt, /*hardreset*/FALSE); 1539 } 1540 } 1541} 1542 1543static void 1544btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code) 1545{ 1546 union ccb *ccb; 1547 struct ccb_scsiio *csio; 1548 1549 ccb = bccb->ccb; 1550 csio = &bccb->ccb->csio; 1551 1552 if ((bccb->flags & BCCB_ACTIVE) == 0) { 1553 device_printf(bt->dev, 1554 "btdone - Attempt to free non-active BCCB %p\n", 1555 (void *)bccb); 1556 return; 1557 } 1558 1559 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 1560 bus_dmasync_op_t op; 1561 1562 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 1563 op = BUS_DMASYNC_POSTREAD; 1564 else 1565 op = BUS_DMASYNC_POSTWRITE; 1566 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op); 1567 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap); 1568 } 1569 1570 if (bccb == bt->recovery_bccb) { 1571 /* 1572 * The recovery BCCB does not have a CCB associated 1573 * with it, so short circuit the normal error handling. 1574 * We now traverse our list of pending CCBs and process 1575 * any that were terminated by the recovery CCBs action. 1576 * We also reinstate timeouts for all remaining, pending, 1577 * CCBs. 1578 */ 1579 struct cam_path *path; 1580 struct ccb_hdr *ccb_h; 1581 cam_status error; 1582 1583 /* Notify all clients that a BDR occured */ 1584 error = xpt_create_path(&path, /*periph*/NULL, 1585 cam_sim_path(bt->sim), 1586 bccb->hccb.target_id, 1587 CAM_LUN_WILDCARD); 1588 1589 if (error == CAM_REQ_CMP) 1590 xpt_async(AC_SENT_BDR, path, NULL); 1591 1592 ccb_h = LIST_FIRST(&bt->pending_ccbs); 1593 while (ccb_h != NULL) { 1594 struct bt_ccb *pending_bccb; 1595 1596 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr; 1597 if (pending_bccb->hccb.target_id 1598 == bccb->hccb.target_id) { 1599 pending_bccb->hccb.btstat = BTSTAT_HA_BDR; 1600 ccb_h = LIST_NEXT(ccb_h, sim_links.le); 1601 btdone(bt, pending_bccb, BMBI_ERROR); 1602 } else { 1603 callout_reset(&pending_bccb->timer, 1604 (ccb_h->timeout * hz) / 1000, 1605 bttimeout, pending_bccb); 1606 ccb_h = LIST_NEXT(ccb_h, sim_links.le); 1607 } 1608 } 1609 device_printf(bt->dev, "No longer in timeout\n"); 1610 return; 1611 } 1612 1613 callout_stop(&bccb->timer); 1614 1615 switch (comp_code) { 1616 case BMBI_FREE: 1617 device_printf(bt->dev, 1618 "btdone - CCB completed with free status!\n"); 1619 break; 1620 case BMBI_NOT_FOUND: 1621 device_printf(bt->dev, 1622 "btdone - CCB Abort failed to find CCB\n"); 1623 break; 1624 case BMBI_ABORT: 1625 case BMBI_ERROR: 1626 if (bootverbose) { 1627 printf("bt: ccb %p - error %x occured. " 1628 "btstat = %x, sdstat = %x\n", 1629 (void *)bccb, comp_code, bccb->hccb.btstat, 1630 bccb->hccb.sdstat); 1631 } 1632 /* An error occured */ 1633 switch(bccb->hccb.btstat) { 1634 case BTSTAT_DATARUN_ERROR: 1635 if (bccb->hccb.data_len == 0) { 1636 /* 1637 * At least firmware 4.22, does this 1638 * for a QUEUE FULL condition. 1639 */ 1640 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL; 1641 } else if (bccb->hccb.data_len < 0) { 1642 csio->ccb_h.status = CAM_DATA_RUN_ERR; 1643 break; 1644 } 1645 /* FALLTHROUGH */ 1646 case BTSTAT_NOERROR: 1647 case BTSTAT_LINKED_CMD_COMPLETE: 1648 case BTSTAT_LINKED_CMD_FLAG_COMPLETE: 1649 case BTSTAT_DATAUNDERUN_ERROR: 1650 1651 csio->scsi_status = bccb->hccb.sdstat; 1652 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 1653 switch(csio->scsi_status) { 1654 case SCSI_STATUS_CHECK_COND: 1655 case SCSI_STATUS_CMD_TERMINATED: 1656 csio->ccb_h.status |= CAM_AUTOSNS_VALID; 1657 /* Bounce sense back if necessary */ 1658 if (bt->sense_buffers != NULL) { 1659 csio->sense_data = 1660 *btsensevaddr(bt, bccb); 1661 } 1662 break; 1663 default: 1664 break; 1665 case SCSI_STATUS_OK: 1666 csio->ccb_h.status = CAM_REQ_CMP; 1667 break; 1668 } 1669 csio->resid = bccb->hccb.data_len; 1670 break; 1671 case BTSTAT_SELTIMEOUT: 1672 csio->ccb_h.status = CAM_SEL_TIMEOUT; 1673 break; 1674 case BTSTAT_UNEXPECTED_BUSFREE: 1675 csio->ccb_h.status = CAM_UNEXP_BUSFREE; 1676 break; 1677 case BTSTAT_INVALID_PHASE: 1678 csio->ccb_h.status = CAM_SEQUENCE_FAIL; 1679 break; 1680 case BTSTAT_INVALID_ACTION_CODE: 1681 panic("%s: Inavlid Action code", bt_name(bt)); 1682 break; 1683 case BTSTAT_INVALID_OPCODE: 1684 panic("%s: Inavlid CCB Opcode code", bt_name(bt)); 1685 break; 1686 case BTSTAT_LINKED_CCB_LUN_MISMATCH: 1687 /* We don't even support linked commands... */ 1688 panic("%s: Linked CCB Lun Mismatch", bt_name(bt)); 1689 break; 1690 case BTSTAT_INVALID_CCB_OR_SG_PARAM: 1691 panic("%s: Invalid CCB or SG list", bt_name(bt)); 1692 break; 1693 case BTSTAT_AUTOSENSE_FAILED: 1694 csio->ccb_h.status = CAM_AUTOSENSE_FAIL; 1695 break; 1696 case BTSTAT_TAGGED_MSG_REJECTED: 1697 { 1698 struct ccb_trans_settings neg; 1699 struct ccb_trans_settings_scsi *scsi = 1700 &neg.proto_specific.scsi; 1701 1702 neg.protocol = PROTO_SCSI; 1703 neg.protocol_version = SCSI_REV_2; 1704 neg.transport = XPORT_SPI; 1705 neg.transport_version = 2; 1706 scsi->valid = CTS_SCSI_VALID_TQ; 1707 scsi->flags = 0; 1708 xpt_print_path(csio->ccb_h.path); 1709 printf("refuses tagged commands. Performing " 1710 "non-tagged I/O\n"); 1711 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path, 1712 /*priority*/1); 1713 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg); 1714 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id); 1715 csio->ccb_h.status = CAM_MSG_REJECT_REC; 1716 break; 1717 } 1718 case BTSTAT_UNSUPPORTED_MSG_RECEIVED: 1719 /* 1720 * XXX You would think that this is 1721 * a recoverable error... Hmmm. 1722 */ 1723 csio->ccb_h.status = CAM_REQ_CMP_ERR; 1724 break; 1725 case BTSTAT_HA_SOFTWARE_ERROR: 1726 case BTSTAT_HA_WATCHDOG_ERROR: 1727 case BTSTAT_HARDWARE_FAILURE: 1728 /* Hardware reset ??? Can we recover ??? */ 1729 csio->ccb_h.status = CAM_NO_HBA; 1730 break; 1731 case BTSTAT_TARGET_IGNORED_ATN: 1732 case BTSTAT_OTHER_SCSI_BUS_RESET: 1733 case BTSTAT_HA_SCSI_BUS_RESET: 1734 if ((csio->ccb_h.status & CAM_STATUS_MASK) 1735 != CAM_CMD_TIMEOUT) 1736 csio->ccb_h.status = CAM_SCSI_BUS_RESET; 1737 break; 1738 case BTSTAT_HA_BDR: 1739 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) 1740 csio->ccb_h.status = CAM_BDR_SENT; 1741 else 1742 csio->ccb_h.status = CAM_CMD_TIMEOUT; 1743 break; 1744 case BTSTAT_INVALID_RECONNECT: 1745 case BTSTAT_ABORT_QUEUE_GENERATED: 1746 csio->ccb_h.status = CAM_REQ_TERMIO; 1747 break; 1748 case BTSTAT_SCSI_PERROR_DETECTED: 1749 csio->ccb_h.status = CAM_UNCOR_PARITY; 1750 break; 1751 } 1752 if (csio->ccb_h.status != CAM_REQ_CMP) { 1753 xpt_freeze_devq(csio->ccb_h.path, /*count*/1); 1754 csio->ccb_h.status |= CAM_DEV_QFRZN; 1755 } 1756 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0) 1757 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1758 btfreeccb(bt, bccb); 1759 xpt_done(ccb); 1760 break; 1761 case BMBI_OK: 1762 /* All completed without incident */ 1763 ccb->ccb_h.status |= CAM_REQ_CMP; 1764 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0) 1765 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1766 btfreeccb(bt, bccb); 1767 xpt_done(ccb); 1768 break; 1769 } 1770} 1771 1772static int 1773btreset(struct bt_softc* bt, int hard_reset) 1774{ 1775 struct ccb_hdr *ccb_h; 1776 u_int status; 1777 u_int timeout; 1778 u_int8_t reset_type; 1779 1780 if (hard_reset != 0) 1781 reset_type = HARD_RESET; 1782 else 1783 reset_type = SOFT_RESET; 1784 bt_outb(bt, CONTROL_REG, reset_type); 1785 1786 /* Wait 5sec. for Diagnostic start */ 1787 timeout = 5 * 10000; 1788 while (--timeout) { 1789 status = bt_inb(bt, STATUS_REG); 1790 if ((status & DIAG_ACTIVE) != 0) 1791 break; 1792 DELAY(100); 1793 } 1794 if (timeout == 0) { 1795 if (bootverbose) 1796 device_printf(bt->dev, 1797 "btreset - Diagnostic Active failed to " 1798 "assert. status = 0x%x\n", status); 1799 return (ETIMEDOUT); 1800 } 1801 1802 /* Wait 10sec. for Diagnostic end */ 1803 timeout = 10 * 10000; 1804 while (--timeout) { 1805 status = bt_inb(bt, STATUS_REG); 1806 if ((status & DIAG_ACTIVE) == 0) 1807 break; 1808 DELAY(100); 1809 } 1810 if (timeout == 0) { 1811 panic("%s: btreset - Diagnostic Active failed to drop. " 1812 "status = 0x%x\n", bt_name(bt), status); 1813 return (ETIMEDOUT); 1814 } 1815 1816 /* Wait for the host adapter to become ready or report a failure */ 1817 timeout = 10000; 1818 while (--timeout) { 1819 status = bt_inb(bt, STATUS_REG); 1820 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0) 1821 break; 1822 DELAY(100); 1823 } 1824 if (timeout == 0) { 1825 device_printf(bt->dev, 1826 "btreset - Host adapter failed to come ready. " 1827 "status = 0x%x\n", status); 1828 return (ETIMEDOUT); 1829 } 1830 1831 /* If the diagnostics failed, tell the user */ 1832 if ((status & DIAG_FAIL) != 0 1833 || (status & HA_READY) == 0) { 1834 device_printf(bt->dev, 1835 "btreset - Adapter failed diagnostics\n"); 1836 1837 if ((status & DATAIN_REG_READY) != 0) 1838 device_printf(bt->dev, 1839 "btreset - Host Adapter Error code = 0x%x\n", 1840 bt_inb(bt, DATAIN_REG)); 1841 return (ENXIO); 1842 } 1843 1844 /* If we've allocated mailboxes, initialize them */ 1845 if (bt->init_level > 4) 1846 btinitmboxes(bt); 1847 1848 /* If we've attached to the XPT, tell it about the event */ 1849 if (bt->path != NULL) 1850 xpt_async(AC_BUS_RESET, bt->path, NULL); 1851 1852 /* 1853 * Perform completion processing for all outstanding CCBs. 1854 */ 1855 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) { 1856 struct bt_ccb *pending_bccb; 1857 1858 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr; 1859 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET; 1860 btdone(bt, pending_bccb, BMBI_ERROR); 1861 } 1862 1863 return (0); 1864} 1865 1866/* 1867 * Send a command to the adapter. 1868 */ 1869int 1870bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len, 1871 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout) 1872{ 1873 u_int timeout; 1874 u_int status; 1875 u_int saved_status; 1876 u_int intstat; 1877 u_int reply_buf_size; 1878 int cmd_complete; 1879 int error; 1880 1881 /* No data returned to start */ 1882 reply_buf_size = reply_len; 1883 reply_len = 0; 1884 intstat = 0; 1885 cmd_complete = 0; 1886 saved_status = 0; 1887 error = 0; 1888 1889 bt->command_cmp = 0; 1890 /* 1891 * Wait up to 10 sec. for the adapter to become 1892 * ready to accept commands. 1893 */ 1894 timeout = 100000; 1895 while (--timeout) { 1896 status = bt_inb(bt, STATUS_REG); 1897 if ((status & HA_READY) != 0 1898 && (status & CMD_REG_BUSY) == 0) 1899 break; 1900 /* 1901 * Throw away any pending data which may be 1902 * left over from earlier commands that we 1903 * timedout on. 1904 */ 1905 if ((status & DATAIN_REG_READY) != 0) 1906 (void)bt_inb(bt, DATAIN_REG); 1907 DELAY(100); 1908 } 1909 if (timeout == 0) { 1910 device_printf(bt->dev, 1911 "bt_cmd: Timeout waiting for adapter ready, " 1912 "status = 0x%x\n", status); 1913 return (ETIMEDOUT); 1914 } 1915 1916 /* 1917 * Send the opcode followed by any necessary parameter bytes. 1918 */ 1919 bt_outb(bt, COMMAND_REG, opcode); 1920 1921 /* 1922 * Wait for up to 1sec for each byte of the 1923 * parameter list sent to be sent. 1924 */ 1925 timeout = 10000; 1926 while (param_len && --timeout) { 1927 DELAY(100); 1928 status = bt_inb(bt, STATUS_REG); 1929 intstat = bt_inb(bt, INTSTAT_REG); 1930 1931 if ((intstat & (INTR_PENDING|CMD_COMPLETE)) 1932 == (INTR_PENDING|CMD_COMPLETE)) { 1933 saved_status = status; 1934 cmd_complete = 1; 1935 break; 1936 } 1937 if (bt->command_cmp != 0) { 1938 saved_status = bt->latched_status; 1939 cmd_complete = 1; 1940 break; 1941 } 1942 if ((status & DATAIN_REG_READY) != 0) 1943 break; 1944 if ((status & CMD_REG_BUSY) == 0) { 1945 bt_outb(bt, COMMAND_REG, *params++); 1946 param_len--; 1947 timeout = 10000; 1948 } 1949 } 1950 if (timeout == 0) { 1951 device_printf(bt->dev, "bt_cmd: Timeout sending parameters, " 1952 "status = 0x%x\n", status); 1953 cmd_complete = 1; 1954 saved_status = status; 1955 error = ETIMEDOUT; 1956 } 1957 1958 /* 1959 * Wait for the command to complete. 1960 */ 1961 while (cmd_complete == 0 && --cmd_timeout) { 1962 1963 status = bt_inb(bt, STATUS_REG); 1964 intstat = bt_inb(bt, INTSTAT_REG); 1965 /* 1966 * It may be that this command was issued with 1967 * controller interrupts disabled. We'll never 1968 * get to our command if an incoming mailbox 1969 * interrupt is pending, so take care of completed 1970 * mailbox commands by calling our interrupt handler. 1971 */ 1972 if ((intstat & (INTR_PENDING|IMB_LOADED)) 1973 == (INTR_PENDING|IMB_LOADED)) 1974 bt_intr_locked(bt); 1975 1976 if (bt->command_cmp != 0) { 1977 /* 1978 * Our interrupt handler saw CMD_COMPLETE 1979 * status before we did. 1980 */ 1981 cmd_complete = 1; 1982 saved_status = bt->latched_status; 1983 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE)) 1984 == (INTR_PENDING|CMD_COMPLETE)) { 1985 /* 1986 * Our poll (in case interrupts are blocked) 1987 * saw the CMD_COMPLETE interrupt. 1988 */ 1989 cmd_complete = 1; 1990 saved_status = status; 1991 } else if (opcode == BOP_MODIFY_IO_ADDR 1992 && (status & CMD_REG_BUSY) == 0) { 1993 /* 1994 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE, 1995 * but it should update the status register. So, we 1996 * consider this command complete when the CMD_REG_BUSY 1997 * status clears. 1998 */ 1999 saved_status = status; 2000 cmd_complete = 1; 2001 } else if ((status & DATAIN_REG_READY) != 0) { 2002 u_int8_t data; 2003 2004 data = bt_inb(bt, DATAIN_REG); 2005 if (reply_len < reply_buf_size) { 2006 *reply_data++ = data; 2007 } else { 2008 device_printf(bt->dev, 2009 "bt_cmd - Discarded reply data byte " 2010 "for opcode 0x%x\n", opcode); 2011 } 2012 /* 2013 * Reset timeout to ensure at least a second 2014 * between response bytes. 2015 */ 2016 cmd_timeout = MAX(cmd_timeout, 10000); 2017 reply_len++; 2018 2019 } else if ((opcode == BOP_FETCH_LRAM) 2020 && (status & HA_READY) != 0) { 2021 saved_status = status; 2022 cmd_complete = 1; 2023 } 2024 DELAY(100); 2025 } 2026 if (cmd_timeout == 0) { 2027 device_printf(bt->dev, 2028 "bt_cmd: Timeout waiting for command (%x) " 2029 "to complete.\n", opcode); 2030 device_printf(bt->dev, "status = 0x%x, intstat = 0x%x, " 2031 "rlen %d\n", status, intstat, reply_len); 2032 error = (ETIMEDOUT); 2033 } 2034 2035 /* 2036 * Clear any pending interrupts. 2037 */ 2038 bt_intr_locked(bt); 2039 2040 if (error != 0) 2041 return (error); 2042 2043 /* 2044 * If the command was rejected by the controller, tell the caller. 2045 */ 2046 if ((saved_status & CMD_INVALID) != 0) { 2047 /* 2048 * Some early adapters may not recover properly from 2049 * an invalid command. If it appears that the controller 2050 * has wedged (i.e. status was not cleared by our interrupt 2051 * reset above), perform a soft reset. 2052 */ 2053 if (bootverbose) 2054 device_printf(bt->dev, "Invalid Command 0x%x\n", 2055 opcode); 2056 DELAY(1000); 2057 status = bt_inb(bt, STATUS_REG); 2058 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY| 2059 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0 2060 || (status & (HA_READY|INIT_REQUIRED)) 2061 != (HA_READY|INIT_REQUIRED)) { 2062 btreset(bt, /*hard_reset*/FALSE); 2063 } 2064 return (EINVAL); 2065 } 2066 2067 if (param_len > 0) { 2068 /* The controller did not accept the full argument list */ 2069 return (E2BIG); 2070 } 2071 2072 if (reply_len != reply_buf_size) { 2073 /* Too much or too little data received */ 2074 return (EMSGSIZE); 2075 } 2076 2077 /* We were successful */ 2078 return (0); 2079} 2080 2081static int 2082btinitmboxes(struct bt_softc *bt) { 2083 init_32b_mbox_params_t init_mbox; 2084 int error; 2085 2086 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes); 2087 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes); 2088 bt->cur_inbox = bt->in_boxes; 2089 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1; 2090 bt->cur_outbox = bt->out_boxes; 2091 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1; 2092 2093 /* Tell the adapter about them */ 2094 init_mbox.num_boxes = bt->num_boxes; 2095 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF; 2096 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF; 2097 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF; 2098 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF; 2099 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox, 2100 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL, 2101 /*reply_len*/0, DEFAULT_CMD_TIMEOUT); 2102 2103 if (error != 0) 2104 printf("btinitmboxes: Initialization command failed\n"); 2105 else if (bt->strict_rr != 0) { 2106 /* 2107 * If the controller supports 2108 * strict round robin mode, 2109 * enable it 2110 */ 2111 u_int8_t param; 2112 2113 param = 0; 2114 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1, 2115 /*reply_buf*/NULL, /*reply_len*/0, 2116 DEFAULT_CMD_TIMEOUT); 2117 2118 if (error != 0) { 2119 printf("btinitmboxes: Unable to enable strict RR\n"); 2120 error = 0; 2121 } else if (bootverbose) { 2122 device_printf(bt->dev, 2123 "Using Strict Round Robin Mailbox Mode\n"); 2124 } 2125 } 2126 2127 return (error); 2128} 2129 2130/* 2131 * Update the XPT's idea of the negotiated transfer 2132 * parameters for a particular target. 2133 */ 2134static void 2135btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings *cts) 2136{ 2137 setup_data_t setup_info; 2138 u_int target; 2139 u_int targ_offset; 2140 u_int targ_mask; 2141 u_int sync_period; 2142 u_int sync_offset; 2143 u_int bus_width; 2144 int error; 2145 u_int8_t param; 2146 targ_syncinfo_t sync_info; 2147 struct ccb_trans_settings_scsi *scsi = 2148 &cts->proto_specific.scsi; 2149 struct ccb_trans_settings_spi *spi = 2150 &cts->xport_specific.spi; 2151 2152 spi->valid = 0; 2153 scsi->valid = 0; 2154 2155 target = cts->ccb_h.target_id; 2156 targ_offset = (target & 0x7); 2157 targ_mask = (0x01 << targ_offset); 2158 2159 /* 2160 * Inquire Setup Information. This command retreives the 2161 * Wide negotiation status for recent adapters as well as 2162 * the sync info for older models. 2163 */ 2164 param = sizeof(setup_info); 2165 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1, 2166 (u_int8_t*)&setup_info, sizeof(setup_info), 2167 DEFAULT_CMD_TIMEOUT); 2168 2169 if (error != 0) { 2170 device_printf(bt->dev, 2171 "btfetchtransinfo - Inquire Setup Info Failed %x\n", 2172 error); 2173 return; 2174 } 2175 2176 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset] 2177 : setup_info.high_syncinfo[targ_offset]; 2178 2179 if (sync_info.sync == 0) 2180 sync_offset = 0; 2181 else 2182 sync_offset = sync_info.offset; 2183 2184 2185 bus_width = MSG_EXT_WDTR_BUS_8_BIT; 2186 if (strcmp(bt->firmware_ver, "5.06L") >= 0) { 2187 u_int wide_active; 2188 2189 wide_active = 2190 (target < 8) ? (setup_info.low_wide_active & targ_mask) 2191 : (setup_info.high_wide_active & targ_mask); 2192 2193 if (wide_active) 2194 bus_width = MSG_EXT_WDTR_BUS_16_BIT; 2195 } else if ((bt->wide_permitted & targ_mask) != 0) { 2196 struct ccb_getdev cgd; 2197 2198 /* 2199 * Prior to rev 5.06L, wide status isn't provided, 2200 * so we "guess" that wide transfers are in effect 2201 * if the user settings allow for wide and the inquiry 2202 * data for the device indicates that it can handle 2203 * wide transfers. 2204 */ 2205 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1); 2206 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2207 xpt_action((union ccb *)&cgd); 2208 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP 2209 && (cgd.inq_data.flags & SID_WBus16) != 0) 2210 bus_width = MSG_EXT_WDTR_BUS_16_BIT; 2211 } 2212 2213 if (bt->firmware_ver[0] >= '3') { 2214 /* 2215 * For adapters that can do fast or ultra speeds, 2216 * use the more exact Target Sync Information command. 2217 */ 2218 target_sync_info_data_t sync_info; 2219 2220 param = sizeof(sync_info); 2221 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1, 2222 (u_int8_t*)&sync_info, sizeof(sync_info), 2223 DEFAULT_CMD_TIMEOUT); 2224 2225 if (error != 0) { 2226 device_printf(bt->dev, 2227 "btfetchtransinfo - Inquire Sync " 2228 "Info Failed 0x%x\n", error); 2229 return; 2230 } 2231 sync_period = sync_info.sync_rate[target] * 100; 2232 } else { 2233 sync_period = 2000 + (500 * sync_info.period); 2234 } 2235 2236 cts->protocol = PROTO_SCSI; 2237 cts->protocol_version = SCSI_REV_2; 2238 cts->transport = XPORT_SPI; 2239 cts->transport_version = 2; 2240 2241 spi->sync_period = sync_period; 2242 spi->valid |= CTS_SPI_VALID_SYNC_RATE; 2243 spi->sync_offset = sync_offset; 2244 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET; 2245 2246 spi->valid |= CTS_SPI_VALID_BUS_WIDTH; 2247 spi->bus_width = bus_width; 2248 2249 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) { 2250 scsi->valid = CTS_SCSI_VALID_TQ; 2251 spi->valid |= CTS_SPI_VALID_DISC; 2252 } else 2253 scsi->valid = 0; 2254 2255 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts); 2256} 2257 2258static void 2259btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error) 2260{ 2261 struct bt_softc* bt; 2262 2263 bt = (struct bt_softc*)arg; 2264 bt->mailbox_physbase = segs->ds_addr; 2265} 2266 2267static void 2268btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error) 2269{ 2270 struct bt_softc* bt; 2271 2272 bt = (struct bt_softc*)arg; 2273 bt->bt_ccb_physbase = segs->ds_addr; 2274} 2275 2276static void 2277btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error) 2278{ 2279 2280 struct bt_softc* bt; 2281 2282 bt = (struct bt_softc*)arg; 2283 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr; 2284} 2285 2286static void 2287btpoll(struct cam_sim *sim) 2288{ 2289 bt_intr_locked(cam_sim_softc(sim)); 2290} 2291 2292void 2293bttimeout(void *arg) 2294{ 2295 struct bt_ccb *bccb; 2296 union ccb *ccb; 2297 struct bt_softc *bt; 2298 2299 bccb = (struct bt_ccb *)arg; 2300 ccb = bccb->ccb; 2301 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr; 2302 mtx_assert(&bt->lock, MA_OWNED); 2303 xpt_print_path(ccb->ccb_h.path); 2304 printf("CCB %p - timed out\n", (void *)bccb); 2305 2306 if ((bccb->flags & BCCB_ACTIVE) == 0) { 2307 xpt_print_path(ccb->ccb_h.path); 2308 printf("CCB %p - timed out CCB already completed\n", 2309 (void *)bccb); 2310 return; 2311 } 2312 2313 /* 2314 * In order to simplify the recovery process, we ask the XPT 2315 * layer to halt the queue of new transactions and we traverse 2316 * the list of pending CCBs and remove their timeouts. This 2317 * means that the driver attempts to clear only one error 2318 * condition at a time. In general, timeouts that occur 2319 * close together are related anyway, so there is no benefit 2320 * in attempting to handle errors in parrallel. Timeouts will 2321 * be reinstated when the recovery process ends. 2322 */ 2323 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) { 2324 struct ccb_hdr *ccb_h; 2325 2326 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) { 2327 xpt_freeze_simq(bt->sim, /*count*/1); 2328 bccb->flags |= BCCB_RELEASE_SIMQ; 2329 } 2330 2331 ccb_h = LIST_FIRST(&bt->pending_ccbs); 2332 while (ccb_h != NULL) { 2333 struct bt_ccb *pending_bccb; 2334 2335 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr; 2336 callout_stop(&pending_bccb->timer); 2337 ccb_h = LIST_NEXT(ccb_h, sim_links.le); 2338 } 2339 } 2340 2341 if ((bccb->flags & BCCB_DEVICE_RESET) != 0 2342 || bt->cur_outbox->action_code != BMBO_FREE 2343 || ((bccb->hccb.tag_enable == TRUE) 2344 && (bt->firmware_ver[0] < '5'))) { 2345 /* 2346 * Try a full host adapter/SCSI bus reset. 2347 * We do this only if we have already attempted 2348 * to clear the condition with a BDR, or we cannot 2349 * attempt a BDR for lack of mailbox resources 2350 * or because of faulty firmware. It turns out 2351 * that firmware versions prior to 5.xx treat BDRs 2352 * as untagged commands that cannot be sent until 2353 * all outstanding tagged commands have been processed. 2354 * This makes it somewhat difficult to use a BDR to 2355 * clear up a problem with an uncompleted tagged command. 2356 */ 2357 ccb->ccb_h.status = CAM_CMD_TIMEOUT; 2358 btreset(bt, /*hardreset*/TRUE); 2359 device_printf(bt->dev, "No longer in timeout\n"); 2360 } else { 2361 /* 2362 * Send a Bus Device Reset message: 2363 * The target that is holding up the bus may not 2364 * be the same as the one that triggered this timeout 2365 * (different commands have different timeout lengths), 2366 * but we have no way of determining this from our 2367 * timeout handler. Our strategy here is to queue a 2368 * BDR message to the target of the timed out command. 2369 * If this fails, we'll get another timeout 2 seconds 2370 * later which will attempt a bus reset. 2371 */ 2372 bccb->flags |= BCCB_DEVICE_RESET; 2373 callout_reset(&bccb->timer, 2 * hz, bttimeout, bccb); 2374 2375 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET; 2376 2377 /* No Data Transfer */ 2378 bt->recovery_bccb->hccb.datain = TRUE; 2379 bt->recovery_bccb->hccb.dataout = TRUE; 2380 bt->recovery_bccb->hccb.btstat = 0; 2381 bt->recovery_bccb->hccb.sdstat = 0; 2382 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id; 2383 2384 /* Tell the adapter about this command */ 2385 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb); 2386 bt->cur_outbox->action_code = BMBO_START; 2387 bt_outb(bt, COMMAND_REG, BOP_START_MBOX); 2388 btnextoutbox(bt); 2389 } 2390} 2391 2392MODULE_VERSION(bt, 1); 2393MODULE_DEPEND(bt, cam, 1, 1, 1); 2394