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
9 * Copyright (c) 1998, 1999 Justin T. Gibbs.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
50 #include <sys/mutex.h>
51 #include <sys/sysctl.h>
54 #include <machine/bus.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_sim.h>
60 #include <cam/cam_xpt_sim.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_message.h>
68 #include <dev/buslogic/btreg.h>
70 /* MailBox Management functions */
71 static __inline void btnextinbox(struct bt_softc *bt);
72 static __inline void btnextoutbox(struct bt_softc *bt);
75 btnextinbox(struct bt_softc *bt)
77 if (bt->cur_inbox == bt->last_inbox)
78 bt->cur_inbox = bt->in_boxes;
84 btnextoutbox(struct bt_softc *bt)
86 if (bt->cur_outbox == bt->last_outbox)
87 bt->cur_outbox = bt->out_boxes;
92 /* CCB Mangement functions */
93 static __inline u_int32_t btccbvtop(struct bt_softc *bt,
95 static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
97 static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
99 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
100 struct bt_ccb *bccb);
102 static __inline u_int32_t
103 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
105 return (bt->bt_ccb_physbase
106 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
109 static __inline struct bt_ccb *
110 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
112 return (bt->bt_ccb_array +
113 ((struct bt_ccb*)(uintptr_t)ccb_addr - (struct bt_ccb*)(uintptr_t)bt->bt_ccb_physbase));
116 static __inline u_int32_t
117 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
121 index = (u_int)(bccb - bt->bt_ccb_array);
122 return (bt->sense_buffers_physbase
123 + (index * sizeof(struct scsi_sense_data)));
126 static __inline struct scsi_sense_data *
127 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
131 index = (u_int)(bccb - bt->bt_ccb_array);
132 return (bt->sense_buffers + index);
135 static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
136 static __inline void btfreeccb(struct bt_softc *bt,
137 struct bt_ccb *bccb);
138 static void btallocccbs(struct bt_softc *bt);
139 static bus_dmamap_callback_t btexecuteccb;
140 static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
141 bt_mbi_comp_code_t comp_code);
143 /* Host adapter command functions */
144 static int btreset(struct bt_softc* bt, int hard_reset);
146 /* Initialization functions */
147 static int btinitmboxes(struct bt_softc *bt);
148 static bus_dmamap_callback_t btmapmboxes;
149 static bus_dmamap_callback_t btmapccbs;
150 static bus_dmamap_callback_t btmapsgs;
152 /* Transfer Negotiation Functions */
153 static void btfetchtransinfo(struct bt_softc *bt,
154 struct ccb_trans_settings *cts);
156 /* CAM SIM entry points */
157 #define ccb_bccb_ptr spriv_ptr0
158 #define ccb_bt_ptr spriv_ptr1
159 static void btaction(struct cam_sim *sim, union ccb *ccb);
160 static void btpoll(struct cam_sim *sim);
162 /* Our timeout handler */
169 * Do our own re-probe protection until a configuration
170 * manager can do it for us. This ensures that we don't
171 * reprobe a card already found by the EISA or PCI probes.
173 struct bt_isa_port bt_isa_ports[] =
184 * I/O ports listed in the order enumerated by the
185 * card for certain op codes.
187 u_int16_t bt_board_ports[] =
197 /* Exported functions */
199 bt_init_softc(device_t dev, struct resource *port,
200 struct resource *irq, struct resource *drq)
202 struct bt_softc *bt = device_get_softc(dev);
204 SLIST_INIT(&bt->free_bt_ccbs);
205 LIST_INIT(&bt->pending_ccbs);
206 SLIST_INIT(&bt->sg_maps);
208 bt->unit = device_get_unit(dev);
212 bt->tag = rman_get_bustag(port);
213 bt->bsh = rman_get_bushandle(port);
217 bt_free_softc(device_t dev)
219 struct bt_softc *bt = device_get_softc(dev);
221 switch (bt->init_level) {
224 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
226 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
229 bus_dma_tag_destroy(bt->sense_dmat);
232 struct sg_map_node *sg_map;
234 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
235 SLIST_REMOVE_HEAD(&bt->sg_maps, links);
236 bus_dmamap_unload(bt->sg_dmat,
238 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
240 free(sg_map, M_DEVBUF);
242 bus_dma_tag_destroy(bt->sg_dmat);
245 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
248 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
250 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
253 bus_dma_tag_destroy(bt->ccb_dmat);
256 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
259 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
261 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
264 bus_dma_tag_destroy(bt->buffer_dmat);
267 bus_dma_tag_destroy(bt->mailbox_dmat);
275 bt_port_probe(device_t dev, struct bt_probe_info *info)
277 struct bt_softc *bt = device_get_softc(dev);
278 config_data_t config_data;
281 /* See if there is really a card present */
282 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
286 * Determine our IRQ, and DMA settings and
287 * export them to the configuration system.
289 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
290 (u_int8_t*)&config_data, sizeof(config_data),
291 DEFAULT_CMD_TIMEOUT);
293 printf("bt_port_probe: Could not determine IRQ or DMA "
294 "settings for adapter.\n");
298 if (bt->model[0] == '5') {
299 /* DMA settings only make sense for ISA cards */
300 switch (config_data.dma_chan) {
311 printf("bt_port_probe: Invalid DMA setting "
312 "detected for adapter.\n");
316 /* VL/EISA/PCI DMA */
319 switch (config_data.irq) {
326 info->irq = ffs(config_data.irq) + 8;
329 printf("bt_port_probe: Invalid IRQ setting %x"
330 "detected for adapter.\n", config_data.irq);
337 * Probe the adapter and verify that the card is a BusLogic.
340 bt_probe(device_t dev)
342 struct bt_softc *bt = device_get_softc(dev);
343 esetup_info_data_t esetup_info;
351 * See if the three I/O ports look reasonable.
352 * Touch the minimal number of registers in the
355 status = bt_inb(bt, STATUS_REG);
357 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
358 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
360 device_printf(dev, "Failed Status Reg Test - %x\n",
365 intstat = bt_inb(bt, INTSTAT_REG);
366 if ((intstat & INTSTAT_REG_RSVD) != 0) {
367 device_printf(dev, "Failed Intstat Reg Test\n");
371 geometry = bt_inb(bt, GEOMETRY_REG);
372 if (geometry == 0xFF) {
374 device_printf(dev, "Failed Geometry Reg Test\n");
379 * Looking good so far. Final test is to reset the
380 * adapter and attempt to fetch the extended setup
381 * information. This should filter out all 1542 cards.
383 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
385 device_printf(dev, "Failed Reset\n");
389 param = sizeof(esetup_info);
390 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1,
391 (u_int8_t*)&esetup_info, sizeof(esetup_info),
392 DEFAULT_CMD_TIMEOUT);
401 * Pull the boards setup information and record it in our softc.
404 bt_fetch_adapter_info(device_t dev)
406 struct bt_softc *bt = device_get_softc(dev);
407 board_id_data_t board_id;
408 esetup_info_data_t esetup_info;
409 config_data_t config_data;
411 u_int8_t length_param;
413 /* First record the firmware version */
414 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
415 (u_int8_t*)&board_id, sizeof(board_id),
416 DEFAULT_CMD_TIMEOUT);
418 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
421 bt->firmware_ver[0] = board_id.firmware_rev_major;
422 bt->firmware_ver[1] = '.';
423 bt->firmware_ver[2] = board_id.firmware_rev_minor;
424 bt->firmware_ver[3] = '\0';
427 * Depending on the firmware major and minor version,
428 * we may be able to fetch additional minor version info.
430 if (bt->firmware_ver[0] > '0') {
432 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
433 (u_int8_t*)&bt->firmware_ver[3], 1,
434 DEFAULT_CMD_TIMEOUT);
437 "bt_fetch_adapter_info - Failed Get "
438 "Firmware 3rd Digit\n");
441 if (bt->firmware_ver[3] == ' ')
442 bt->firmware_ver[3] = '\0';
443 bt->firmware_ver[4] = '\0';
446 if (strcmp(bt->firmware_ver, "3.3") >= 0) {
448 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
449 (u_int8_t*)&bt->firmware_ver[4], 1,
450 DEFAULT_CMD_TIMEOUT);
453 "bt_fetch_adapter_info - Failed Get "
454 "Firmware 4th Digit\n");
457 if (bt->firmware_ver[4] == ' ')
458 bt->firmware_ver[4] = '\0';
459 bt->firmware_ver[5] = '\0';
463 * Some boards do not handle the "recently documented"
464 * Inquire Board Model Number command correctly or do not give
465 * exact information. Use the Firmware and Extended Setup
466 * information in these cases to come up with the right answer.
467 * The major firmware revision number indicates:
469 * 5.xx BusLogic "W" Series Host Adapters:
471 * 4.xx BusLogic "C" Series Host Adapters:
472 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
473 * 3.xx BusLogic "S" Series Host Adapters:
474 * BT-747S/747D/757S/757D/445S/545S/542D
475 * BT-542B/742A (revision H)
476 * 2.xx BusLogic "A" Series Host Adapters:
477 * BT-542B/742A (revision G and below)
478 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
480 length_param = sizeof(esetup_info);
481 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
482 (u_int8_t*)&esetup_info, sizeof(esetup_info),
483 DEFAULT_CMD_TIMEOUT);
488 bt->bios_addr = esetup_info.bios_addr << 12;
490 bt->mailbox_addrlimit = BUS_SPACE_MAXADDR;
491 if (esetup_info.bus_type == 'A'
492 && bt->firmware_ver[0] == '2') {
493 snprintf(bt->model, sizeof(bt->model), "542B");
494 } else if (esetup_info.bus_type == 'E'
495 && bt->firmware_ver[0] == '2') {
498 * The 742A seems to object if its mailboxes are
499 * allocated above the 16MB mark.
501 bt->mailbox_addrlimit = BUS_SPACE_MAXADDR_24BIT;
502 snprintf(bt->model, sizeof(bt->model), "742A");
503 } else if (esetup_info.bus_type == 'E'
504 && bt->firmware_ver[0] == '0') {
505 /* AMI FastDisk EISA Series 441 0.x */
506 snprintf(bt->model, sizeof(bt->model), "747A");
508 ha_model_data_t model_data;
511 length_param = sizeof(model_data);
512 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
513 (u_int8_t*)&model_data, sizeof(model_data),
514 DEFAULT_CMD_TIMEOUT);
517 "bt_fetch_adapter_info - Failed Inquire "
521 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
522 bt->model[i] = model_data.ascii_model[i];
523 if (bt->model[i] == ' ')
529 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
531 /* SG element limits */
532 bt->max_sg = esetup_info.max_sg;
534 /* Set feature flags */
535 bt->wide_bus = esetup_info.wide_bus;
536 bt->diff_bus = esetup_info.diff_bus;
537 bt->ultra_scsi = esetup_info.ultra_scsi;
539 if ((bt->firmware_ver[0] == '5')
540 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
541 bt->extended_lun = TRUE;
543 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
546 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
549 * Determine max CCB count and whether tagged queuing is
550 * available based on controller type. Tagged queuing
551 * only works on 'W' series adapters, 'C' series adapters
552 * with firmware of rev 4.42 and higher, and 'S' series
553 * adapters with firmware of rev 3.35 and higher. The
554 * maximum CCB counts are as follows:
556 * 192 BT-948/958/958D
557 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
559 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
561 if (bt->firmware_ver[0] == '5') {
563 bt->tag_capable = TRUE;
564 } else if (bt->firmware_ver[0] == '4') {
565 if (bt->model[0] == '5')
569 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
572 if (bt->firmware_ver[0] == '3'
573 && (strcmp(bt->firmware_ver, "3.35") >= 0))
574 bt->tag_capable = TRUE;
576 bt->tag_capable = FALSE;
579 if (bt->tag_capable != FALSE)
580 bt->tags_permitted = ALL_TARGETS;
582 /* Determine Sync/Wide/Disc settings */
583 if (bt->firmware_ver[0] >= '4') {
584 auto_scsi_data_t auto_scsi_data;
585 fetch_lram_params_t fetch_lram_params;
589 * These settings are stored in the
590 * AutoSCSI data in LRAM of 'W' and 'C'
593 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
594 fetch_lram_params.response_len = sizeof(auto_scsi_data);
595 error = bt_cmd(bt, BOP_FETCH_LRAM,
596 (u_int8_t*)&fetch_lram_params,
597 sizeof(fetch_lram_params),
598 (u_int8_t*)&auto_scsi_data,
599 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
603 "bt_fetch_adapter_info - Failed "
604 "Get Auto SCSI Info\n");
608 bt->disc_permitted = auto_scsi_data.low_disc_permitted
609 | (auto_scsi_data.high_disc_permitted << 8);
610 bt->sync_permitted = auto_scsi_data.low_sync_permitted
611 | (auto_scsi_data.high_sync_permitted << 8);
612 bt->fast_permitted = auto_scsi_data.low_fast_permitted
613 | (auto_scsi_data.high_fast_permitted << 8);
614 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
615 | (auto_scsi_data.high_ultra_permitted << 8);
616 bt->wide_permitted = auto_scsi_data.low_wide_permitted
617 | (auto_scsi_data.high_wide_permitted << 8);
619 if (bt->ultra_scsi == FALSE)
620 bt->ultra_permitted = 0;
622 if (bt->wide_bus == FALSE)
623 bt->wide_permitted = 0;
626 * 'S' and 'A' series have this information in the setup
627 * information structure.
629 setup_data_t setup_info;
631 length_param = sizeof(setup_info);
632 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
633 /*paramlen*/1, (u_int8_t*)&setup_info,
634 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
638 "bt_fetch_adapter_info - Failed "
643 if (setup_info.initiate_sync != 0) {
644 bt->sync_permitted = ALL_TARGETS;
646 if (bt->model[0] == '7') {
647 if (esetup_info.sync_neg10MB != 0)
648 bt->fast_permitted = ALL_TARGETS;
649 if (strcmp(bt->model, "757") == 0)
650 bt->wide_permitted = ALL_TARGETS;
653 bt->disc_permitted = ALL_TARGETS;
656 /* We need as many mailboxes as we can have ccbs */
657 bt->num_boxes = bt->max_ccbs;
659 /* Determine our SCSI ID */
661 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
662 (u_int8_t*)&config_data, sizeof(config_data),
663 DEFAULT_CMD_TIMEOUT);
666 "bt_fetch_adapter_info - Failed Get Config\n");
669 bt->scsi_id = config_data.scsi_id;
675 * Start the board, ready for normal operation
678 bt_init(device_t dev)
680 struct bt_softc *bt = device_get_softc(dev);
682 /* Announce the Adapter */
683 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
685 if (bt->ultra_scsi != 0)
688 if (bt->wide_bus != 0)
693 if (bt->diff_bus != 0)
696 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
700 * Create our DMA tags. These tags define the kinds of device
701 * accessible memory allocations and memory mappings we will
702 * need to perform during normal operation.
704 * Unless we need to further restrict the allocation, we rely
705 * on the restrictions of the parent dmat, hence the common
706 * use of MAXADDR and MAXSIZE.
709 /* DMA tag for mapping buffers into device visible space. */
710 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
713 /* lowaddr */ BUS_SPACE_MAXADDR,
714 /* highaddr */ BUS_SPACE_MAXADDR,
716 /* filterarg */ NULL,
717 /* maxsize */ MAXBSIZE,
718 /* nsegments */ BT_NSEG,
719 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
720 /* flags */ BUS_DMA_ALLOCNOW,
721 /* lockfunc */ busdma_lock_mutex,
722 /* lockarg */ &Giant,
723 &bt->buffer_dmat) != 0) {
728 /* DMA tag for our mailboxes */
729 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
732 /* lowaddr */ bt->mailbox_addrlimit,
733 /* highaddr */ BUS_SPACE_MAXADDR,
735 /* filterarg */ NULL,
736 /* maxsize */ bt->num_boxes *
737 (sizeof(bt_mbox_in_t) +
738 sizeof(bt_mbox_out_t)),
740 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
742 /* lockfunc */ busdma_lock_mutex,
743 /* lockarg */ &Giant,
744 &bt->mailbox_dmat) != 0) {
750 /* Allocation for our mailboxes */
751 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
752 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
758 /* And permanently map them */
759 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
761 bt->num_boxes * (sizeof(bt_mbox_in_t)
762 + sizeof(bt_mbox_out_t)),
763 btmapmboxes, bt, /*flags*/0);
767 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
771 /* DMA tag for our ccb structures */
772 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
775 /* lowaddr */ BUS_SPACE_MAXADDR,
776 /* highaddr */ BUS_SPACE_MAXADDR,
778 /* filterarg */ NULL,
779 /* maxsize */ bt->max_ccbs *
780 sizeof(struct bt_ccb),
782 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
784 /* lockfunc */ busdma_lock_mutex,
785 /* lockarg */ &Giant,
786 &bt->ccb_dmat) != 0) {
792 /* Allocation for our ccbs */
793 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
794 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
800 /* And permanently map them */
801 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
803 bt->max_ccbs * sizeof(struct bt_ccb),
804 btmapccbs, bt, /*flags*/0);
808 /* DMA tag for our S/G structures. We allocate in page sized chunks */
809 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
812 /* lowaddr */ BUS_SPACE_MAXADDR,
813 /* highaddr */ BUS_SPACE_MAXADDR,
815 /* filterarg */ NULL,
816 /* maxsize */ PAGE_SIZE,
818 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
820 /* lockfunc */ busdma_lock_mutex,
821 /* lockarg */ &Giant,
822 &bt->sg_dmat) != 0) {
828 /* Perform initial CCB allocation */
829 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
832 if (bt->num_ccbs == 0) {
834 "bt_init - Unable to allocate initial ccbs\n");
839 * Note that we are going and return (to probe)
849 bt_attach(device_t dev)
851 struct bt_softc *bt = device_get_softc(dev);
852 int tagged_dev_openings;
853 struct cam_devq *devq;
857 * We reserve 1 ccb for error recovery, so don't
858 * tell the XPT about it.
860 if (bt->tag_capable != 0)
861 tagged_dev_openings = bt->max_ccbs - 1;
863 tagged_dev_openings = 0;
866 * Create the device queue for our SIM.
868 devq = cam_simq_alloc(bt->max_ccbs - 1);
873 * Construct our SIM entry
875 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
876 2, tagged_dev_openings, devq);
877 if (bt->sim == NULL) {
882 if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
883 cam_sim_free(bt->sim, /*free_devq*/TRUE);
887 if (xpt_create_path(&bt->path, /*periph*/NULL,
888 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
889 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
890 xpt_bus_deregister(cam_sim_path(bt->sim));
891 cam_sim_free(bt->sim, /*free_devq*/TRUE);
898 error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM|INTR_ENTROPY,
899 bt_intr, bt, &bt->ih);
901 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
909 bt_check_probed_iop(u_int ioport)
913 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
914 if (bt_isa_ports[i].addr == ioport) {
915 if (bt_isa_ports[i].probed != 0)
926 bt_mark_probed_bio(isa_compat_io_t port)
928 if (port < BIO_DISABLED)
929 bt_mark_probed_iop(bt_board_ports[port]);
933 bt_mark_probed_iop(u_int ioport)
937 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
938 if (ioport == bt_isa_ports[i].addr) {
939 bt_isa_ports[i].probed = 1;
946 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
951 for (i = 0;i < BT_NUM_ISAPORTS; i++)
952 if (ioport <= bt_isa_ports[i].addr)
954 if ((i >= BT_NUM_ISAPORTS)
955 || (ioport != bt_isa_ports[i].addr)) {
957 "bt_find_probe_range: Invalid baseport of 0x%x specified.\n"
958 "bt_find_probe_range: Nearest valid baseport is 0x%x.\n"
959 "bt_find_probe_range: Failing probe.\n",
961 (i < BT_NUM_ISAPORTS)
962 ? bt_isa_ports[i].addr
963 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
964 *port_index = *max_port_index = -1;
967 *port_index = *max_port_index = bt_isa_ports[i].bio;
970 *max_port_index = BT_NUM_ISAPORTS - 1;
975 bt_iop_from_bio(isa_compat_io_t bio_index)
977 if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
978 return (bt_board_ports[bio_index]);
984 btallocccbs(struct bt_softc *bt)
986 struct bt_ccb *next_ccb;
987 struct sg_map_node *sg_map;
993 if (bt->num_ccbs >= bt->max_ccbs)
994 /* Can't allocate any more */
997 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
999 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
1004 /* Allocate S/G space for the next batch of CCBS */
1005 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
1006 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
1007 free(sg_map, M_DEVBUF);
1011 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
1013 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
1014 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
1016 segs = sg_map->sg_vaddr;
1017 physaddr = sg_map->sg_physaddr;
1019 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
1020 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
1023 next_ccb->sg_list = segs;
1024 next_ccb->sg_list_phys = physaddr;
1025 next_ccb->flags = BCCB_FREE;
1026 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
1030 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
1032 physaddr += (BT_NSEG * sizeof(bt_sg_t));
1037 /* Reserve a CCB for error recovery */
1038 if (bt->recovery_bccb == NULL) {
1039 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1040 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1043 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
1047 device_printf(bt->dev, "Can't malloc BCCBs\n");
1050 static __inline void
1051 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1056 if ((bccb->flags & BCCB_ACTIVE) != 0)
1057 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1058 if (bt->resource_shortage != 0
1059 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1060 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1061 bt->resource_shortage = FALSE;
1063 bccb->flags = BCCB_FREE;
1064 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1069 static __inline struct bt_ccb*
1070 btgetccb(struct bt_softc *bt)
1072 struct bt_ccb* bccb;
1076 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1077 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1081 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1083 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1093 btaction(struct cam_sim *sim, union ccb *ccb)
1095 struct bt_softc *bt;
1097 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1099 bt = (struct bt_softc *)cam_sim_softc(sim);
1101 switch (ccb->ccb_h.func_code) {
1102 /* Common cases first */
1103 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1104 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1106 struct bt_ccb *bccb;
1107 struct bt_hccb *hccb;
1110 * get a bccb to use.
1112 if ((bccb = btgetccb(bt)) == NULL) {
1116 bt->resource_shortage = TRUE;
1118 xpt_freeze_simq(bt->sim, /*count*/1);
1119 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1127 * So we can find the BCCB when an abort is requested
1130 ccb->ccb_h.ccb_bccb_ptr = bccb;
1131 ccb->ccb_h.ccb_bt_ptr = bt;
1134 * Put all the arguments for the xfer in the bccb
1136 hccb->target_id = ccb->ccb_h.target_id;
1137 hccb->target_lun = ccb->ccb_h.target_lun;
1141 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1142 struct ccb_scsiio *csio;
1143 struct ccb_hdr *ccbh;
1146 ccbh = &csio->ccb_h;
1147 hccb->opcode = INITIATOR_CCB_WRESID;
1148 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1149 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1150 hccb->cmd_len = csio->cdb_len;
1151 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1152 ccb->ccb_h.status = CAM_REQ_INVALID;
1153 btfreeccb(bt, bccb);
1157 hccb->sense_len = csio->sense_len;
1158 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1159 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1160 hccb->tag_enable = TRUE;
1161 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1163 hccb->tag_enable = FALSE;
1166 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1167 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1168 bcopy(csio->cdb_io.cdb_ptr,
1169 hccb->scsi_cdb, hccb->cmd_len);
1171 /* I guess I could map it in... */
1172 ccbh->status = CAM_REQ_INVALID;
1173 btfreeccb(bt, bccb);
1178 bcopy(csio->cdb_io.cdb_bytes,
1179 hccb->scsi_cdb, hccb->cmd_len);
1181 /* If need be, bounce our sense buffer */
1182 if (bt->sense_buffers != NULL) {
1183 hccb->sense_addr = btsensepaddr(bt, bccb);
1185 hccb->sense_addr = vtophys(&csio->sense_data);
1188 * If we have any data to send with this command,
1189 * map it into bus space.
1191 /* Only use S/G if there is a transfer */
1192 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1193 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
1195 * We've been given a pointer
1196 * to a single buffer.
1198 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
1203 error = bus_dmamap_load(
1211 if (error == EINPROGRESS) {
1214 * ordering, freeze the
1216 * until our mapping is
1219 xpt_freeze_simq(bt->sim,
1221 csio->ccb_h.status |=
1226 struct bus_dma_segment seg;
1228 /* Pointer to physical buffer */
1230 (bus_addr_t)csio->data_ptr;
1231 seg.ds_len = csio->dxfer_len;
1232 btexecuteccb(bccb, &seg, 1, 0);
1235 struct bus_dma_segment *segs;
1237 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1238 panic("btaction - Physical "
1242 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1243 panic("btaction - Virtual "
1244 "segment addresses "
1247 /* Just use the segments provided */
1248 segs = (struct bus_dma_segment *)
1250 btexecuteccb(bccb, segs,
1251 csio->sglist_cnt, 0);
1254 btexecuteccb(bccb, NULL, 0, 0);
1257 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1258 /* No data transfer */
1259 hccb->datain = TRUE;
1260 hccb->dataout = TRUE;
1262 hccb->sense_len = 0;
1263 hccb->tag_enable = FALSE;
1265 btexecuteccb(bccb, NULL, 0, 0);
1269 case XPT_EN_LUN: /* Enable LUN as a target */
1270 case XPT_TARGET_IO: /* Execute target I/O request */
1271 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1272 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1273 case XPT_ABORT: /* Abort the specified CCB */
1275 ccb->ccb_h.status = CAM_REQ_INVALID;
1278 case XPT_SET_TRAN_SETTINGS:
1281 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1285 case XPT_GET_TRAN_SETTINGS:
1286 /* Get default/user set transfer settings for the target */
1288 struct ccb_trans_settings *cts;
1292 target_mask = 0x01 << ccb->ccb_h.target_id;
1293 #ifdef CAM_NEW_TRAN_CODE
1294 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1295 struct ccb_trans_settings_scsi *scsi =
1296 &cts->proto_specific.scsi;
1297 struct ccb_trans_settings_spi *spi =
1298 &cts->xport_specific.spi;
1299 cts->protocol = PROTO_SCSI;
1300 cts->protocol_version = SCSI_REV_2;
1301 cts->transport = XPORT_SPI;
1302 cts->transport_version = 2;
1304 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1305 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1307 if ((bt->disc_permitted & target_mask) != 0)
1308 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1309 if ((bt->tags_permitted & target_mask) != 0)
1310 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1312 if ((bt->ultra_permitted & target_mask) != 0)
1313 spi->sync_period = 12;
1314 else if ((bt->fast_permitted & target_mask) != 0)
1315 spi->sync_period = 25;
1316 else if ((bt->sync_permitted & target_mask) != 0)
1317 spi->sync_period = 50;
1319 spi->sync_period = 0;
1321 if (spi->sync_period != 0)
1322 spi->sync_offset = 15;
1324 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1325 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1327 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1328 if ((bt->wide_permitted & target_mask) != 0)
1329 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1331 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1333 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1334 scsi->valid = CTS_SCSI_VALID_TQ;
1335 spi->valid |= CTS_SPI_VALID_DISC;
1340 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1342 if ((bt->disc_permitted & target_mask) != 0)
1343 cts->flags |= CCB_TRANS_DISC_ENB;
1344 if ((bt->tags_permitted & target_mask) != 0)
1345 cts->flags |= CCB_TRANS_TAG_ENB;
1346 if ((bt->wide_permitted & target_mask) != 0)
1347 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1349 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1350 if ((bt->ultra_permitted & target_mask) != 0)
1351 cts->sync_period = 12;
1352 else if ((bt->fast_permitted & target_mask) != 0)
1353 cts->sync_period = 25;
1354 else if ((bt->sync_permitted & target_mask) != 0)
1355 cts->sync_period = 50;
1357 cts->sync_period = 0;
1359 if (cts->sync_period != 0)
1360 cts->sync_offset = 15;
1362 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1363 | CCB_TRANS_SYNC_OFFSET_VALID
1364 | CCB_TRANS_BUS_WIDTH_VALID
1365 | CCB_TRANS_DISC_VALID
1366 | CCB_TRANS_TQ_VALID;
1369 btfetchtransinfo(bt, cts);
1372 ccb->ccb_h.status = CAM_REQ_CMP;
1376 case XPT_CALC_GEOMETRY:
1378 struct ccb_calc_geometry *ccg;
1380 u_int32_t secs_per_cylinder;
1383 size_mb = ccg->volume_size
1384 / ((1024L * 1024L) / ccg->block_size);
1386 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1387 if (size_mb >= 2048) {
1389 ccg->secs_per_track = 63;
1392 ccg->secs_per_track = 32;
1396 ccg->secs_per_track = 32;
1398 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1399 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1400 ccb->ccb_h.status = CAM_REQ_CMP;
1404 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1406 btreset(bt, /*hardreset*/TRUE);
1407 ccb->ccb_h.status = CAM_REQ_CMP;
1411 case XPT_TERM_IO: /* Terminate the I/O process */
1413 ccb->ccb_h.status = CAM_REQ_INVALID;
1416 case XPT_PATH_INQ: /* Path routing inquiry */
1418 struct ccb_pathinq *cpi = &ccb->cpi;
1420 cpi->version_num = 1; /* XXX??? */
1421 cpi->hba_inquiry = PI_SDTR_ABLE;
1422 if (bt->tag_capable != 0)
1423 cpi->hba_inquiry |= PI_TAG_ABLE;
1424 if (bt->wide_bus != 0)
1425 cpi->hba_inquiry |= PI_WIDE_16;
1426 cpi->target_sprt = 0;
1428 cpi->hba_eng_cnt = 0;
1429 cpi->max_target = bt->wide_bus ? 15 : 7;
1431 cpi->initiator_id = bt->scsi_id;
1432 cpi->bus_id = cam_sim_bus(sim);
1433 cpi->base_transfer_speed = 3300;
1434 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1435 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1436 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1437 cpi->unit_number = cam_sim_unit(sim);
1438 cpi->ccb_h.status = CAM_REQ_CMP;
1439 #ifdef CAM_NEW_TRAN_CODE
1440 cpi->transport = XPORT_SPI;
1441 cpi->transport_version = 2;
1442 cpi->protocol = PROTO_SCSI;
1443 cpi->protocol_version = SCSI_REV_2;
1449 ccb->ccb_h.status = CAM_REQ_INVALID;
1456 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1458 struct bt_ccb *bccb;
1460 struct bt_softc *bt;
1463 bccb = (struct bt_ccb *)arg;
1465 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1469 device_printf(bt->dev,
1470 "Unexepected error 0x%x returned from "
1471 "bus_dmamap_load\n", error);
1472 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1473 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1474 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1476 btfreeccb(bt, bccb);
1483 bus_dma_segment_t *end_seg;
1484 bus_dmasync_op_t op;
1486 end_seg = dm_segs + nseg;
1488 /* Copy the segments into our SG list */
1490 while (dm_segs < end_seg) {
1491 sg->len = dm_segs->ds_len;
1492 sg->addr = dm_segs->ds_addr;
1498 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1499 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1500 bccb->hccb.data_addr = bccb->sg_list_phys;
1502 bccb->hccb.data_len = bccb->sg_list->len;
1503 bccb->hccb.data_addr = bccb->sg_list->addr;
1506 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1507 op = BUS_DMASYNC_PREREAD;
1509 op = BUS_DMASYNC_PREWRITE;
1511 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1514 bccb->hccb.opcode = INITIATOR_CCB;
1515 bccb->hccb.data_len = 0;
1516 bccb->hccb.data_addr = 0;
1522 * Last time we need to check if this CCB needs to
1525 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1527 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1528 btfreeccb(bt, bccb);
1534 bccb->flags = BCCB_ACTIVE;
1535 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1536 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1538 ccb->ccb_h.timeout_ch =
1539 timeout(bttimeout, (caddr_t)bccb,
1540 (ccb->ccb_h.timeout * hz) / 1000);
1542 /* Tell the adapter about this command */
1543 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1544 if (bt->cur_outbox->action_code != BMBO_FREE) {
1546 * We should never encounter a busy mailbox.
1547 * If we do, warn the user, and treat it as
1548 * a resource shortage. If the controller is
1549 * hung, one of the pending transactions will
1550 * timeout causing us to start recovery operations.
1552 device_printf(bt->dev,
1553 "Encountered busy mailbox with %d out of %d "
1554 "commands active!!!\n", bt->active_ccbs,
1556 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1558 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1559 btfreeccb(bt, bccb);
1560 bt->resource_shortage = TRUE;
1561 xpt_freeze_simq(bt->sim, /*count*/1);
1562 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1566 bt->cur_outbox->action_code = BMBO_START;
1567 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1575 struct bt_softc *bt;
1578 bt = (struct bt_softc *)arg;
1579 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1581 if ((intstat & CMD_COMPLETE) != 0) {
1582 bt->latched_status = bt_inb(bt, STATUS_REG);
1583 bt->command_cmp = TRUE;
1586 bt_outb(bt, CONTROL_REG, RESET_INTR);
1588 if ((intstat & IMB_LOADED) != 0) {
1589 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1591 btccbptov(bt, bt->cur_inbox->ccb_addr),
1592 bt->cur_inbox->comp_code);
1593 bt->cur_inbox->comp_code = BMBI_FREE;
1598 if ((intstat & SCSI_BUS_RESET) != 0) {
1599 btreset(bt, /*hardreset*/FALSE);
1605 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1608 struct ccb_scsiio *csio;
1611 csio = &bccb->ccb->csio;
1613 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1614 device_printf(bt->dev,
1615 "btdone - Attempt to free non-active BCCB %p\n",
1620 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1621 bus_dmasync_op_t op;
1623 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1624 op = BUS_DMASYNC_POSTREAD;
1626 op = BUS_DMASYNC_POSTWRITE;
1627 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1628 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1631 if (bccb == bt->recovery_bccb) {
1633 * The recovery BCCB does not have a CCB associated
1634 * with it, so short circuit the normal error handling.
1635 * We now traverse our list of pending CCBs and process
1636 * any that were terminated by the recovery CCBs action.
1637 * We also reinstate timeouts for all remaining, pending,
1640 struct cam_path *path;
1641 struct ccb_hdr *ccb_h;
1644 /* Notify all clients that a BDR occured */
1645 error = xpt_create_path(&path, /*periph*/NULL,
1646 cam_sim_path(bt->sim),
1647 bccb->hccb.target_id,
1650 if (error == CAM_REQ_CMP)
1651 xpt_async(AC_SENT_BDR, path, NULL);
1653 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1654 while (ccb_h != NULL) {
1655 struct bt_ccb *pending_bccb;
1657 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1658 if (pending_bccb->hccb.target_id
1659 == bccb->hccb.target_id) {
1660 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1661 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1662 btdone(bt, pending_bccb, BMBI_ERROR);
1665 timeout(bttimeout, (caddr_t)pending_bccb,
1666 (ccb_h->timeout * hz) / 1000);
1667 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1670 device_printf(bt->dev, "No longer in timeout\n");
1674 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1676 switch (comp_code) {
1678 device_printf(bt->dev,
1679 "btdone - CCB completed with free status!\n");
1681 case BMBI_NOT_FOUND:
1682 device_printf(bt->dev,
1683 "btdone - CCB Abort failed to find CCB\n");
1688 printf("bt: ccb %p - error %x occured. "
1689 "btstat = %x, sdstat = %x\n",
1690 (void *)bccb, comp_code, bccb->hccb.btstat,
1693 /* An error occured */
1694 switch(bccb->hccb.btstat) {
1695 case BTSTAT_DATARUN_ERROR:
1696 if (bccb->hccb.data_len == 0) {
1698 * At least firmware 4.22, does this
1699 * for a QUEUE FULL condition.
1701 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1702 } else if (bccb->hccb.data_len < 0) {
1703 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1707 case BTSTAT_NOERROR:
1708 case BTSTAT_LINKED_CMD_COMPLETE:
1709 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1710 case BTSTAT_DATAUNDERUN_ERROR:
1712 csio->scsi_status = bccb->hccb.sdstat;
1713 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1714 switch(csio->scsi_status) {
1715 case SCSI_STATUS_CHECK_COND:
1716 case SCSI_STATUS_CMD_TERMINATED:
1717 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1718 /* Bounce sense back if necessary */
1719 if (bt->sense_buffers != NULL) {
1721 *btsensevaddr(bt, bccb);
1726 case SCSI_STATUS_OK:
1727 csio->ccb_h.status = CAM_REQ_CMP;
1730 csio->resid = bccb->hccb.data_len;
1732 case BTSTAT_SELTIMEOUT:
1733 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1735 case BTSTAT_UNEXPECTED_BUSFREE:
1736 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1738 case BTSTAT_INVALID_PHASE:
1739 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1741 case BTSTAT_INVALID_ACTION_CODE:
1742 panic("%s: Inavlid Action code", bt_name(bt));
1744 case BTSTAT_INVALID_OPCODE:
1745 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1747 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1748 /* We don't even support linked commands... */
1749 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1751 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1752 panic("%s: Invalid CCB or SG list", bt_name(bt));
1754 case BTSTAT_AUTOSENSE_FAILED:
1755 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1757 case BTSTAT_TAGGED_MSG_REJECTED:
1759 struct ccb_trans_settings neg;
1760 #ifdef CAM_NEW_TRAN_CODE
1761 struct ccb_trans_settings_scsi *scsi =
1762 &neg.proto_specific.scsi;
1764 neg.protocol = PROTO_SCSI;
1765 neg.protocol_version = SCSI_REV_2;
1766 neg.transport = XPORT_SPI;
1767 neg.transport_version = 2;
1768 scsi->valid = CTS_SCSI_VALID_TQ;
1773 neg.valid = CCB_TRANS_TQ_VALID;
1775 xpt_print_path(csio->ccb_h.path);
1776 printf("refuses tagged commands. Performing "
1777 "non-tagged I/O\n");
1778 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1780 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1781 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1782 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1785 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1787 * XXX You would think that this is
1788 * a recoverable error... Hmmm.
1790 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1792 case BTSTAT_HA_SOFTWARE_ERROR:
1793 case BTSTAT_HA_WATCHDOG_ERROR:
1794 case BTSTAT_HARDWARE_FAILURE:
1795 /* Hardware reset ??? Can we recover ??? */
1796 csio->ccb_h.status = CAM_NO_HBA;
1798 case BTSTAT_TARGET_IGNORED_ATN:
1799 case BTSTAT_OTHER_SCSI_BUS_RESET:
1800 case BTSTAT_HA_SCSI_BUS_RESET:
1801 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1803 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1806 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1807 csio->ccb_h.status = CAM_BDR_SENT;
1809 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1811 case BTSTAT_INVALID_RECONNECT:
1812 case BTSTAT_ABORT_QUEUE_GENERATED:
1813 csio->ccb_h.status = CAM_REQ_TERMIO;
1815 case BTSTAT_SCSI_PERROR_DETECTED:
1816 csio->ccb_h.status = CAM_UNCOR_PARITY;
1819 if (csio->ccb_h.status != CAM_REQ_CMP) {
1820 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1821 csio->ccb_h.status |= CAM_DEV_QFRZN;
1823 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1824 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1825 btfreeccb(bt, bccb);
1829 /* All completed without incident */
1830 ccb->ccb_h.status |= CAM_REQ_CMP;
1831 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1832 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1833 btfreeccb(bt, bccb);
1840 btreset(struct bt_softc* bt, int hard_reset)
1842 struct ccb_hdr *ccb_h;
1845 u_int8_t reset_type;
1847 if (hard_reset != 0)
1848 reset_type = HARD_RESET;
1850 reset_type = SOFT_RESET;
1851 bt_outb(bt, CONTROL_REG, reset_type);
1853 /* Wait 5sec. for Diagnostic start */
1854 timeout = 5 * 10000;
1856 status = bt_inb(bt, STATUS_REG);
1857 if ((status & DIAG_ACTIVE) != 0)
1863 printf("%s: btreset - Diagnostic Active failed to "
1864 "assert. status = 0x%x\n", bt_name(bt), status);
1868 /* Wait 10sec. for Diagnostic end */
1869 timeout = 10 * 10000;
1871 status = bt_inb(bt, STATUS_REG);
1872 if ((status & DIAG_ACTIVE) == 0)
1877 panic("%s: btreset - Diagnostic Active failed to drop. "
1878 "status = 0x%x\n", bt_name(bt), status);
1882 /* Wait for the host adapter to become ready or report a failure */
1885 status = bt_inb(bt, STATUS_REG);
1886 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1891 printf("%s: btreset - Host adapter failed to come ready. "
1892 "status = 0x%x\n", bt_name(bt), status);
1896 /* If the diagnostics failed, tell the user */
1897 if ((status & DIAG_FAIL) != 0
1898 || (status & HA_READY) == 0) {
1899 printf("%s: btreset - Adapter failed diagnostics\n",
1902 if ((status & DATAIN_REG_READY) != 0)
1903 printf("%s: btreset - Host Adapter Error code = 0x%x\n",
1904 bt_name(bt), bt_inb(bt, DATAIN_REG));
1908 /* If we've allocated mailboxes, initialize them */
1909 if (bt->init_level > 4)
1912 /* If we've attached to the XPT, tell it about the event */
1913 if (bt->path != NULL)
1914 xpt_async(AC_BUS_RESET, bt->path, NULL);
1917 * Perform completion processing for all outstanding CCBs.
1919 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1920 struct bt_ccb *pending_bccb;
1922 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1923 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1924 btdone(bt, pending_bccb, BMBI_ERROR);
1931 * Send a command to the adapter.
1934 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1935 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1941 u_int reply_buf_size;
1946 /* No data returned to start */
1947 reply_buf_size = reply_len;
1954 bt->command_cmp = 0;
1956 * Wait up to 10 sec. for the adapter to become
1957 * ready to accept commands.
1961 status = bt_inb(bt, STATUS_REG);
1962 if ((status & HA_READY) != 0
1963 && (status & CMD_REG_BUSY) == 0)
1966 * Throw away any pending data which may be
1967 * left over from earlier commands that we
1970 if ((status & DATAIN_REG_READY) != 0)
1971 (void)bt_inb(bt, DATAIN_REG);
1975 printf("%s: bt_cmd: Timeout waiting for adapter ready, "
1976 "status = 0x%x\n", bt_name(bt), status);
1981 * Send the opcode followed by any necessary parameter bytes.
1983 bt_outb(bt, COMMAND_REG, opcode);
1986 * Wait for up to 1sec for each byte of the the
1987 * parameter list sent to be sent.
1990 while (param_len && --timeout) {
1993 status = bt_inb(bt, STATUS_REG);
1994 intstat = bt_inb(bt, INTSTAT_REG);
1997 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1998 == (INTR_PENDING|CMD_COMPLETE)) {
1999 saved_status = status;
2003 if (bt->command_cmp != 0) {
2004 saved_status = bt->latched_status;
2008 if ((status & DATAIN_REG_READY) != 0)
2010 if ((status & CMD_REG_BUSY) == 0) {
2011 bt_outb(bt, COMMAND_REG, *params++);
2017 printf("%s: bt_cmd: Timeout sending parameters, "
2018 "status = 0x%x\n", bt_name(bt), status);
2020 saved_status = status;
2025 * Wait for the command to complete.
2027 while (cmd_complete == 0 && --cmd_timeout) {
2030 status = bt_inb(bt, STATUS_REG);
2031 intstat = bt_inb(bt, INTSTAT_REG);
2033 * It may be that this command was issued with
2034 * controller interrupts disabled. We'll never
2035 * get to our command if an incoming mailbox
2036 * interrupt is pending, so take care of completed
2037 * mailbox commands by calling our interrupt handler.
2039 if ((intstat & (INTR_PENDING|IMB_LOADED))
2040 == (INTR_PENDING|IMB_LOADED))
2044 if (bt->command_cmp != 0) {
2046 * Our interrupt handler saw CMD_COMPLETE
2047 * status before we did.
2050 saved_status = bt->latched_status;
2051 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
2052 == (INTR_PENDING|CMD_COMPLETE)) {
2054 * Our poll (in case interrupts are blocked)
2055 * saw the CMD_COMPLETE interrupt.
2058 saved_status = status;
2059 } else if (opcode == BOP_MODIFY_IO_ADDR
2060 && (status & CMD_REG_BUSY) == 0) {
2062 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
2063 * but it should update the status register. So, we
2064 * consider this command complete when the CMD_REG_BUSY
2067 saved_status = status;
2069 } else if ((status & DATAIN_REG_READY) != 0) {
2072 data = bt_inb(bt, DATAIN_REG);
2073 if (reply_len < reply_buf_size) {
2074 *reply_data++ = data;
2076 printf("%s: bt_cmd - Discarded reply data byte "
2077 "for opcode 0x%x\n", bt_name(bt),
2081 * Reset timeout to ensure at least a second
2082 * between response bytes.
2084 cmd_timeout = MAX(cmd_timeout, 10000);
2087 } else if ((opcode == BOP_FETCH_LRAM)
2088 && (status & HA_READY) != 0) {
2089 saved_status = status;
2094 if (cmd_timeout == 0) {
2095 printf("%s: bt_cmd: Timeout waiting for command (%x) "
2096 "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
2097 "rlen %d\n", bt_name(bt), opcode,
2098 bt_name(bt), status, intstat, reply_len);
2099 error = (ETIMEDOUT);
2103 * Clear any pending interrupts. Block interrupts so our
2104 * interrupt handler is not re-entered.
2114 * If the command was rejected by the controller, tell the caller.
2116 if ((saved_status & CMD_INVALID) != 0) {
2118 * Some early adapters may not recover properly from
2119 * an invalid command. If it appears that the controller
2120 * has wedged (i.e. status was not cleared by our interrupt
2121 * reset above), perform a soft reset.
2124 printf("%s: Invalid Command 0x%x\n", bt_name(bt),
2127 status = bt_inb(bt, STATUS_REG);
2128 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2129 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2130 || (status & (HA_READY|INIT_REQUIRED))
2131 != (HA_READY|INIT_REQUIRED)) {
2132 btreset(bt, /*hard_reset*/FALSE);
2137 if (param_len > 0) {
2138 /* The controller did not accept the full argument list */
2142 if (reply_len != reply_buf_size) {
2143 /* Too much or too little data received */
2147 /* We were successful */
2152 btinitmboxes(struct bt_softc *bt) {
2153 init_32b_mbox_params_t init_mbox;
2156 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2157 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2158 bt->cur_inbox = bt->in_boxes;
2159 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2160 bt->cur_outbox = bt->out_boxes;
2161 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2163 /* Tell the adapter about them */
2164 init_mbox.num_boxes = bt->num_boxes;
2165 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2166 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2167 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2168 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2169 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2170 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2171 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2174 printf("btinitmboxes: Initialization command failed\n");
2175 else if (bt->strict_rr != 0) {
2177 * If the controller supports
2178 * strict round robin mode,
2184 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1,
2185 /*reply_buf*/NULL, /*reply_len*/0,
2186 DEFAULT_CMD_TIMEOUT);
2189 printf("btinitmboxes: Unable to enable strict RR\n");
2191 } else if (bootverbose) {
2192 printf("%s: Using Strict Round Robin Mailbox Mode\n",
2201 * Update the XPT's idea of the negotiated transfer
2202 * parameters for a particular target.
2205 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings *cts)
2207 setup_data_t setup_info;
2216 targ_syncinfo_t sync_info;
2217 #ifdef CAM_NEW_TRAN_CODE
2218 struct ccb_trans_settings_scsi *scsi =
2219 &cts->proto_specific.scsi;
2220 struct ccb_trans_settings_spi *spi =
2221 &cts->xport_specific.spi;
2230 target = cts->ccb_h.target_id;
2231 targ_offset = (target & 0x7);
2232 targ_mask = (0x01 << targ_offset);
2235 * Inquire Setup Information. This command retreives the
2236 * Wide negotiation status for recent adapters as well as
2237 * the sync info for older models.
2239 param = sizeof(setup_info);
2240 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
2241 (u_int8_t*)&setup_info, sizeof(setup_info),
2242 DEFAULT_CMD_TIMEOUT);
2245 printf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
2246 bt_name(bt), error);
2250 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2251 : setup_info.high_syncinfo[targ_offset];
2253 if (sync_info.sync == 0)
2256 sync_offset = sync_info.offset;
2259 bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2260 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2264 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2265 : (setup_info.high_wide_active & targ_mask);
2268 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2269 } else if ((bt->wide_permitted & targ_mask) != 0) {
2270 struct ccb_getdev cgd;
2273 * Prior to rev 5.06L, wide status isn't provided,
2274 * so we "guess" that wide transfers are in effect
2275 * if the user settings allow for wide and the inquiry
2276 * data for the device indicates that it can handle
2279 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2280 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2281 xpt_action((union ccb *)&cgd);
2282 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2283 && (cgd.inq_data.flags & SID_WBus16) != 0)
2284 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2287 if (bt->firmware_ver[0] >= '3') {
2289 * For adapters that can do fast or ultra speeds,
2290 * use the more exact Target Sync Information command.
2292 target_sync_info_data_t sync_info;
2294 param = sizeof(sync_info);
2295 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1,
2296 (u_int8_t*)&sync_info, sizeof(sync_info),
2297 DEFAULT_CMD_TIMEOUT);
2300 printf("%s: btfetchtransinfo - Inquire Sync "
2301 "Info Failed 0x%x\n", bt_name(bt), error);
2304 sync_period = sync_info.sync_rate[target] * 100;
2306 sync_period = 2000 + (500 * sync_info.period);
2309 #ifdef CAM_NEW_TRAN_CODE
2310 cts->protocol = PROTO_SCSI;
2311 cts->protocol_version = SCSI_REV_2;
2312 cts->transport = XPORT_SPI;
2313 cts->transport_version = 2;
2315 spi->sync_period = sync_period;
2316 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2317 spi->sync_offset = sync_offset;
2318 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2320 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2321 spi->bus_width = bus_width;
2323 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2324 scsi->valid = CTS_SCSI_VALID_TQ;
2325 spi->valid |= CTS_SPI_VALID_DISC;
2330 /* Convert ns value to standard SCSI sync rate */
2331 if (cts->sync_offset != 0)
2332 cts->sync_period = scsi_calc_syncparam(sync_period);
2334 cts->sync_period = 0;
2335 cts->sync_offset = sync_offset;
2336 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2338 cts->valid = CCB_TRANS_SYNC_RATE_VALID
2339 | CCB_TRANS_SYNC_OFFSET_VALID
2340 | CCB_TRANS_BUS_WIDTH_VALID;
2343 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2347 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2349 struct bt_softc* bt;
2351 bt = (struct bt_softc*)arg;
2352 bt->mailbox_physbase = segs->ds_addr;
2356 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2358 struct bt_softc* bt;
2360 bt = (struct bt_softc*)arg;
2361 bt->bt_ccb_physbase = segs->ds_addr;
2365 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2368 struct bt_softc* bt;
2370 bt = (struct bt_softc*)arg;
2371 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2375 btpoll(struct cam_sim *sim)
2377 bt_intr(cam_sim_softc(sim));
2381 bttimeout(void *arg)
2383 struct bt_ccb *bccb;
2385 struct bt_softc *bt;
2388 bccb = (struct bt_ccb *)arg;
2390 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2391 xpt_print_path(ccb->ccb_h.path);
2392 printf("CCB %p - timed out\n", (void *)bccb);
2396 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2397 xpt_print_path(ccb->ccb_h.path);
2398 printf("CCB %p - timed out CCB already completed\n",
2405 * In order to simplify the recovery process, we ask the XPT
2406 * layer to halt the queue of new transactions and we traverse
2407 * the list of pending CCBs and remove their timeouts. This
2408 * means that the driver attempts to clear only one error
2409 * condition at a time. In general, timeouts that occur
2410 * close together are related anyway, so there is no benefit
2411 * in attempting to handle errors in parrallel. Timeouts will
2412 * be reinstated when the recovery process ends.
2414 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2415 struct ccb_hdr *ccb_h;
2417 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2418 xpt_freeze_simq(bt->sim, /*count*/1);
2419 bccb->flags |= BCCB_RELEASE_SIMQ;
2422 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2423 while (ccb_h != NULL) {
2424 struct bt_ccb *pending_bccb;
2426 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2427 untimeout(bttimeout, pending_bccb, ccb_h->timeout_ch);
2428 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2432 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2433 || bt->cur_outbox->action_code != BMBO_FREE
2434 || ((bccb->hccb.tag_enable == TRUE)
2435 && (bt->firmware_ver[0] < '5'))) {
2437 * Try a full host adapter/SCSI bus reset.
2438 * We do this only if we have already attempted
2439 * to clear the condition with a BDR, or we cannot
2440 * attempt a BDR for lack of mailbox resources
2441 * or because of faulty firmware. It turns out
2442 * that firmware versions prior to 5.xx treat BDRs
2443 * as untagged commands that cannot be sent until
2444 * all outstanding tagged commands have been processed.
2445 * This makes it somewhat difficult to use a BDR to
2446 * clear up a problem with an uncompleted tagged command.
2448 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2449 btreset(bt, /*hardreset*/TRUE);
2450 printf("%s: No longer in timeout\n", bt_name(bt));
2453 * Send a Bus Device Reset message:
2454 * The target that is holding up the bus may not
2455 * be the same as the one that triggered this timeout
2456 * (different commands have different timeout lengths),
2457 * but we have no way of determining this from our
2458 * timeout handler. Our strategy here is to queue a
2459 * BDR message to the target of the timed out command.
2460 * If this fails, we'll get another timeout 2 seconds
2461 * later which will attempt a bus reset.
2463 bccb->flags |= BCCB_DEVICE_RESET;
2464 ccb->ccb_h.timeout_ch =
2465 timeout(bttimeout, (caddr_t)bccb, 2 * hz);
2467 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2469 /* No Data Transfer */
2470 bt->recovery_bccb->hccb.datain = TRUE;
2471 bt->recovery_bccb->hccb.dataout = TRUE;
2472 bt->recovery_bccb->hccb.btstat = 0;
2473 bt->recovery_bccb->hccb.sdstat = 0;
2474 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2476 /* Tell the adapter about this command */
2477 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2478 bt->cur_outbox->action_code = BMBO_START;
2479 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);