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