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