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