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
33 * $FreeBSD: src/sys/dev/buslogic/bt.c,v 1.25.2.1 2000/08/02 22:32:26 peter Exp $
34 * $DragonFly: src/sys/dev/disk/buslogic/bt.c,v 1.2 2003/06/17 04:28:23 dillon Exp $
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>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
54 * XXX It appears that BusLogic PCI adapters go out to lunch if you
55 * attempt to perform memory mapped I/O.
60 #include <machine/bus_memio.h>
63 #include <machine/bus_pio.h>
64 #include <machine/bus.h>
65 #include <machine/clock.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_sim.h>
71 #include <cam/cam_xpt_sim.h>
72 #include <cam/cam_debug.h>
74 #include <cam/scsi/scsi_message.h>
79 #include <dev/buslogic/btreg.h>
82 #define MAX(a, b) ((a) > (b) ? (a) : (b))
85 /* MailBox Management functions */
86 static __inline void btnextinbox(struct bt_softc *bt);
87 static __inline void btnextoutbox(struct bt_softc *bt);
90 btnextinbox(struct bt_softc *bt)
92 if (bt->cur_inbox == bt->last_inbox)
93 bt->cur_inbox = bt->in_boxes;
99 btnextoutbox(struct bt_softc *bt)
101 if (bt->cur_outbox == bt->last_outbox)
102 bt->cur_outbox = bt->out_boxes;
107 /* CCB Mangement functions */
108 static __inline u_int32_t btccbvtop(struct bt_softc *bt,
109 struct bt_ccb *bccb);
110 static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
112 static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
113 struct bt_ccb *bccb);
114 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
115 struct bt_ccb *bccb);
117 static __inline u_int32_t
118 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
120 return (bt->bt_ccb_physbase
121 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
124 static __inline struct bt_ccb *
125 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
127 return (bt->bt_ccb_array +
128 ((struct bt_ccb*)ccb_addr-(struct bt_ccb*)bt->bt_ccb_physbase));
131 static __inline u_int32_t
132 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
136 index = (u_int)(bccb - bt->bt_ccb_array);
137 return (bt->sense_buffers_physbase
138 + (index * sizeof(struct scsi_sense_data)));
141 static __inline struct scsi_sense_data *
142 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
146 index = (u_int)(bccb - bt->bt_ccb_array);
147 return (bt->sense_buffers + index);
150 static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
151 static __inline void btfreeccb(struct bt_softc *bt,
152 struct bt_ccb *bccb);
153 static void btallocccbs(struct bt_softc *bt);
154 static bus_dmamap_callback_t btexecuteccb;
155 static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
156 bt_mbi_comp_code_t comp_code);
158 /* Host adapter command functions */
159 static int btreset(struct bt_softc* bt, int hard_reset);
161 /* Initialization functions */
162 static int btinitmboxes(struct bt_softc *bt);
163 static bus_dmamap_callback_t btmapmboxes;
164 static bus_dmamap_callback_t btmapccbs;
165 static bus_dmamap_callback_t btmapsgs;
167 /* Transfer Negotiation Functions */
168 static void btfetchtransinfo(struct bt_softc *bt,
169 struct ccb_trans_settings *cts);
171 /* CAM SIM entry points */
172 #define ccb_bccb_ptr spriv_ptr0
173 #define ccb_bt_ptr spriv_ptr1
174 static void btaction(struct cam_sim *sim, union ccb *ccb);
175 static void btpoll(struct cam_sim *sim);
177 /* Our timeout handler */
184 * Do our own re-probe protection until a configuration
185 * manager can do it for us. This ensures that we don't
186 * reprobe a card already found by the EISA or PCI probes.
188 struct bt_isa_port bt_isa_ports[] =
199 * I/O ports listed in the order enumerated by the
200 * card for certain op codes.
202 u_int16_t bt_board_ports[] =
212 /* Exported functions */
214 bt_init_softc(device_t dev, struct resource *port,
215 struct resource *irq, struct resource *drq)
217 struct bt_softc *bt = device_get_softc(dev);
219 SLIST_INIT(&bt->free_bt_ccbs);
220 LIST_INIT(&bt->pending_ccbs);
221 SLIST_INIT(&bt->sg_maps);
223 bt->unit = device_get_unit(dev);
227 bt->tag = rman_get_bustag(port);
228 bt->bsh = rman_get_bushandle(port);
232 bt_free_softc(device_t dev)
234 struct bt_softc *bt = device_get_softc(dev);
236 switch (bt->init_level) {
239 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
241 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
244 bus_dma_tag_destroy(bt->sense_dmat);
247 struct sg_map_node *sg_map;
249 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
250 SLIST_REMOVE_HEAD(&bt->sg_maps, links);
251 bus_dmamap_unload(bt->sg_dmat,
253 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
255 free(sg_map, M_DEVBUF);
257 bus_dma_tag_destroy(bt->sg_dmat);
260 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
262 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
264 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
266 bus_dma_tag_destroy(bt->ccb_dmat);
268 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
270 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
272 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
274 bus_dma_tag_destroy(bt->buffer_dmat);
276 bus_dma_tag_destroy(bt->mailbox_dmat);
283 bt_port_probe(device_t dev, struct bt_probe_info *info)
285 struct bt_softc *bt = device_get_softc(dev);
286 config_data_t config_data;
289 /* See if there is really a card present */
290 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
294 * Determine our IRQ, and DMA settings and
295 * export them to the configuration system.
297 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
298 (u_int8_t*)&config_data, sizeof(config_data),
299 DEFAULT_CMD_TIMEOUT);
301 printf("bt_port_probe: Could not determine IRQ or DMA "
302 "settings for adapter.\n");
306 if (bt->model[0] == '5') {
307 /* DMA settings only make sense for ISA cards */
308 switch (config_data.dma_chan) {
319 printf("bt_port_probe: Invalid DMA setting "
320 "detected for adapter.\n");
324 /* VL/EISA/PCI DMA */
327 switch (config_data.irq) {
334 info->irq = ffs(config_data.irq) + 8;
337 printf("bt_port_probe: Invalid IRQ setting %x"
338 "detected for adapter.\n", config_data.irq);
345 * Probe the adapter and verify that the card is a BusLogic.
348 bt_probe(device_t dev)
350 struct bt_softc *bt = device_get_softc(dev);
351 esetup_info_data_t esetup_info;
359 * See if the three I/O ports look reasonable.
360 * Touch the minimal number of registers in the
363 status = bt_inb(bt, STATUS_REG);
365 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
366 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
368 device_printf(dev, "Failed Status Reg Test - %x\n",
373 intstat = bt_inb(bt, INTSTAT_REG);
374 if ((intstat & INTSTAT_REG_RSVD) != 0) {
375 device_printf(dev, "Failed Intstat Reg Test\n");
379 geometry = bt_inb(bt, GEOMETRY_REG);
380 if (geometry == 0xFF) {
382 device_printf(dev, "Failed Geometry Reg Test\n");
387 * Looking good so far. Final test is to reset the
388 * adapter and attempt to fetch the extended setup
389 * information. This should filter out all 1542 cards.
391 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
393 device_printf(dev, "Failed Reset\n");
397 param = sizeof(esetup_info);
398 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1,
399 (u_int8_t*)&esetup_info, sizeof(esetup_info),
400 DEFAULT_CMD_TIMEOUT);
409 * Pull the boards setup information and record it in our softc.
412 bt_fetch_adapter_info(device_t dev)
414 struct bt_softc *bt = device_get_softc(dev);
415 board_id_data_t board_id;
416 esetup_info_data_t esetup_info;
417 config_data_t config_data;
419 u_int8_t length_param;
421 /* First record the firmware version */
422 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
423 (u_int8_t*)&board_id, sizeof(board_id),
424 DEFAULT_CMD_TIMEOUT);
426 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
429 bt->firmware_ver[0] = board_id.firmware_rev_major;
430 bt->firmware_ver[1] = '.';
431 bt->firmware_ver[2] = board_id.firmware_rev_minor;
432 bt->firmware_ver[3] = '\0';
435 * Depending on the firmware major and minor version,
436 * we may be able to fetch additional minor version info.
438 if (bt->firmware_ver[0] > '0') {
440 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
441 (u_int8_t*)&bt->firmware_ver[3], 1,
442 DEFAULT_CMD_TIMEOUT);
445 "bt_fetch_adapter_info - Failed Get "
446 "Firmware 3rd Digit\n");
449 if (bt->firmware_ver[3] == ' ')
450 bt->firmware_ver[3] = '\0';
451 bt->firmware_ver[4] = '\0';
454 if (strcmp(bt->firmware_ver, "3.3") >= 0) {
456 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
457 (u_int8_t*)&bt->firmware_ver[4], 1,
458 DEFAULT_CMD_TIMEOUT);
461 "bt_fetch_adapter_info - Failed Get "
462 "Firmware 4th Digit\n");
465 if (bt->firmware_ver[4] == ' ')
466 bt->firmware_ver[4] = '\0';
467 bt->firmware_ver[5] = '\0';
471 * Some boards do not handle the "recently documented"
472 * Inquire Board Model Number command correctly or do not give
473 * exact information. Use the Firmware and Extended Setup
474 * information in these cases to come up with the right answer.
475 * The major firmware revision number indicates:
477 * 5.xx BusLogic "W" Series Host Adapters:
479 * 4.xx BusLogic "C" Series Host Adapters:
480 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
481 * 3.xx BusLogic "S" Series Host Adapters:
482 * BT-747S/747D/757S/757D/445S/545S/542D
483 * BT-542B/742A (revision H)
484 * 2.xx BusLogic "A" Series Host Adapters:
485 * BT-542B/742A (revision G and below)
486 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
488 length_param = sizeof(esetup_info);
489 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
490 (u_int8_t*)&esetup_info, sizeof(esetup_info),
491 DEFAULT_CMD_TIMEOUT);
496 bt->bios_addr = esetup_info.bios_addr << 12;
498 if (esetup_info.bus_type == 'A'
499 && bt->firmware_ver[0] == '2') {
500 snprintf(bt->model, sizeof(bt->model), "542B");
501 } else if (esetup_info.bus_type == 'E'
502 && (strncmp(bt->firmware_ver, "2.1", 3) == 0
503 || strncmp(bt->firmware_ver, "2.20", 4) == 0)) {
504 snprintf(bt->model, sizeof(bt->model), "742A");
505 } else if (esetup_info.bus_type == 'E'
506 && bt->firmware_ver[0] == '0') {
507 /* AMI FastDisk EISA Series 441 0.x */
508 snprintf(bt->model, sizeof(bt->model), "747A");
510 ha_model_data_t model_data;
513 length_param = sizeof(model_data);
514 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
515 (u_int8_t*)&model_data, sizeof(model_data),
516 DEFAULT_CMD_TIMEOUT);
519 "bt_fetch_adapter_info - Failed Inquire "
523 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
524 bt->model[i] = model_data.ascii_model[i];
525 if (bt->model[i] == ' ')
531 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
533 /* SG element limits */
534 bt->max_sg = esetup_info.max_sg;
536 /* Set feature flags */
537 bt->wide_bus = esetup_info.wide_bus;
538 bt->diff_bus = esetup_info.diff_bus;
539 bt->ultra_scsi = esetup_info.ultra_scsi;
541 if ((bt->firmware_ver[0] == '5')
542 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
543 bt->extended_lun = TRUE;
545 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
548 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
551 * Determine max CCB count and whether tagged queuing is
552 * available based on controller type. Tagged queuing
553 * only works on 'W' series adapters, 'C' series adapters
554 * with firmware of rev 4.42 and higher, and 'S' series
555 * adapters with firmware of rev 3.35 and higher. The
556 * maximum CCB counts are as follows:
558 * 192 BT-948/958/958D
559 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
561 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
563 if (bt->firmware_ver[0] == '5') {
565 bt->tag_capable = TRUE;
566 } else if (bt->firmware_ver[0] == '4') {
567 if (bt->model[0] == '5')
571 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
574 if (bt->firmware_ver[0] == '3'
575 && (strcmp(bt->firmware_ver, "3.35") >= 0))
576 bt->tag_capable = TRUE;
578 bt->tag_capable = FALSE;
581 if (bt->tag_capable != FALSE)
582 bt->tags_permitted = ALL_TARGETS;
584 /* Determine Sync/Wide/Disc settings */
585 if (bt->firmware_ver[0] >= '4') {
586 auto_scsi_data_t auto_scsi_data;
587 fetch_lram_params_t fetch_lram_params;
591 * These settings are stored in the
592 * AutoSCSI data in LRAM of 'W' and 'C'
595 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
596 fetch_lram_params.response_len = sizeof(auto_scsi_data);
597 error = bt_cmd(bt, BOP_FETCH_LRAM,
598 (u_int8_t*)&fetch_lram_params,
599 sizeof(fetch_lram_params),
600 (u_int8_t*)&auto_scsi_data,
601 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
605 "bt_fetch_adapter_info - Failed "
606 "Get Auto SCSI Info\n");
610 bt->disc_permitted = auto_scsi_data.low_disc_permitted
611 | (auto_scsi_data.high_disc_permitted << 8);
612 bt->sync_permitted = auto_scsi_data.low_sync_permitted
613 | (auto_scsi_data.high_sync_permitted << 8);
614 bt->fast_permitted = auto_scsi_data.low_fast_permitted
615 | (auto_scsi_data.high_fast_permitted << 8);
616 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
617 | (auto_scsi_data.high_ultra_permitted << 8);
618 bt->wide_permitted = auto_scsi_data.low_wide_permitted
619 | (auto_scsi_data.high_wide_permitted << 8);
621 if (bt->ultra_scsi == FALSE)
622 bt->ultra_permitted = 0;
624 if (bt->wide_bus == FALSE)
625 bt->wide_permitted = 0;
628 * 'S' and 'A' series have this information in the setup
629 * information structure.
631 setup_data_t setup_info;
633 length_param = sizeof(setup_info);
634 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
635 /*paramlen*/1, (u_int8_t*)&setup_info,
636 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
640 "bt_fetch_adapter_info - Failed "
645 if (setup_info.initiate_sync != 0) {
646 bt->sync_permitted = ALL_TARGETS;
648 if (bt->model[0] == '7') {
649 if (esetup_info.sync_neg10MB != 0)
650 bt->fast_permitted = ALL_TARGETS;
651 if (strcmp(bt->model, "757") == 0)
652 bt->wide_permitted = ALL_TARGETS;
655 bt->disc_permitted = ALL_TARGETS;
658 /* We need as many mailboxes as we can have ccbs */
659 bt->num_boxes = bt->max_ccbs;
661 /* Determine our SCSI ID */
663 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
664 (u_int8_t*)&config_data, sizeof(config_data),
665 DEFAULT_CMD_TIMEOUT);
668 "bt_fetch_adapter_info - Failed Get Config\n");
671 bt->scsi_id = config_data.scsi_id;
677 * Start the board, ready for normal operation
680 bt_init(device_t dev)
682 struct bt_softc *bt = device_get_softc(dev);
684 /* Announce the Adapter */
685 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
687 if (bt->ultra_scsi != 0)
690 if (bt->wide_bus != 0)
695 if (bt->diff_bus != 0)
698 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
702 * Create our DMA tags. These tags define the kinds of device
703 * accessible memory allocations and memory mappings we will
704 * need to perform during normal operation.
706 * Unless we need to further restrict the allocation, we rely
707 * on the restrictions of the parent dmat, hence the common
708 * use of MAXADDR and MAXSIZE.
711 /* DMA tag for mapping buffers into device visible space. */
712 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
713 /*lowaddr*/BUS_SPACE_MAXADDR,
714 /*highaddr*/BUS_SPACE_MAXADDR,
715 /*filter*/NULL, /*filterarg*/NULL,
716 /*maxsize*/MAXBSIZE, /*nsegments*/BT_NSEG,
717 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
718 /*flags*/BUS_DMA_ALLOCNOW,
719 &bt->buffer_dmat) != 0) {
724 /* DMA tag for our mailboxes */
725 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
726 /*lowaddr*/BUS_SPACE_MAXADDR,
727 /*highaddr*/BUS_SPACE_MAXADDR,
728 /*filter*/NULL, /*filterarg*/NULL,
729 bt->num_boxes * (sizeof(bt_mbox_in_t)
730 + sizeof(bt_mbox_out_t)),
732 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
733 /*flags*/0, &bt->mailbox_dmat) != 0) {
739 /* Allocation for our mailboxes */
740 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
741 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
747 /* And permanently map them */
748 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
750 bt->num_boxes * (sizeof(bt_mbox_in_t)
751 + sizeof(bt_mbox_out_t)),
752 btmapmboxes, bt, /*flags*/0);
756 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
760 /* DMA tag for our ccb structures */
761 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
762 /*lowaddr*/BUS_SPACE_MAXADDR,
763 /*highaddr*/BUS_SPACE_MAXADDR,
764 /*filter*/NULL, /*filterarg*/NULL,
765 bt->max_ccbs * sizeof(struct bt_ccb),
767 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
768 /*flags*/0, &bt->ccb_dmat) != 0) {
774 /* Allocation for our ccbs */
775 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
776 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
782 /* And permanently map them */
783 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
785 bt->max_ccbs * sizeof(struct bt_ccb),
786 btmapccbs, bt, /*flags*/0);
790 /* DMA tag for our S/G structures. We allocate in page sized chunks */
791 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
792 /*lowaddr*/BUS_SPACE_MAXADDR,
793 /*highaddr*/BUS_SPACE_MAXADDR,
794 /*filter*/NULL, /*filterarg*/NULL,
795 PAGE_SIZE, /*nsegments*/1,
796 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
797 /*flags*/0, &bt->sg_dmat) != 0) {
803 /* Perform initial CCB allocation */
804 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
807 if (bt->num_ccbs == 0) {
809 "bt_init - Unable to allocate initial ccbs\n");
814 * Note that we are going and return (to probe)
824 bt_attach(device_t dev)
826 struct bt_softc *bt = device_get_softc(dev);
827 int tagged_dev_openings;
828 struct cam_devq *devq;
832 * We reserve 1 ccb for error recovery, so don't
833 * tell the XPT about it.
835 if (bt->tag_capable != 0)
836 tagged_dev_openings = bt->max_ccbs - 1;
838 tagged_dev_openings = 0;
841 * Create the device queue for our SIM.
843 devq = cam_simq_alloc(bt->max_ccbs - 1);
848 * Construct our SIM entry
850 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
851 2, tagged_dev_openings, devq);
852 if (bt->sim == NULL) {
857 if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
858 cam_sim_free(bt->sim, /*free_devq*/TRUE);
862 if (xpt_create_path(&bt->path, /*periph*/NULL,
863 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
864 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
865 xpt_bus_deregister(cam_sim_path(bt->sim));
866 cam_sim_free(bt->sim, /*free_devq*/TRUE);
873 error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM,
874 bt_intr, bt, &bt->ih);
876 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
884 bt_check_probed_iop(u_int ioport)
888 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
889 if (bt_isa_ports[i].addr == ioport) {
890 if (bt_isa_ports[i].probed != 0)
901 bt_mark_probed_bio(isa_compat_io_t port)
903 if (port < BIO_DISABLED)
904 bt_mark_probed_iop(bt_board_ports[port]);
908 bt_mark_probed_iop(u_int ioport)
912 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
913 if (ioport == bt_isa_ports[i].addr) {
914 bt_isa_ports[i].probed = 1;
921 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
926 for (i = 0;i < BT_NUM_ISAPORTS; i++)
927 if (ioport <= bt_isa_ports[i].addr)
929 if ((i >= BT_NUM_ISAPORTS)
930 || (ioport != bt_isa_ports[i].addr)) {
932 bt_isa_probe: Invalid baseport of 0x%x specified.
933 bt_isa_probe: Nearest valid baseport is 0x%x.
934 bt_isa_probe: Failing probe.\n",
936 (i < BT_NUM_ISAPORTS)
937 ? bt_isa_ports[i].addr
938 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
939 *port_index = *max_port_index = -1;
942 *port_index = *max_port_index = bt_isa_ports[i].bio;
945 *max_port_index = BT_NUM_ISAPORTS - 1;
950 bt_iop_from_bio(isa_compat_io_t bio_index)
952 if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
953 return (bt_board_ports[bio_index]);
959 btallocccbs(struct bt_softc *bt)
961 struct bt_ccb *next_ccb;
962 struct sg_map_node *sg_map;
968 if (bt->num_ccbs >= bt->max_ccbs)
969 /* Can't allocate any more */
972 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
974 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
979 /* Allocate S/G space for the next batch of CCBS */
980 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
981 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
982 free(sg_map, M_DEVBUF);
986 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
988 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
989 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
991 segs = sg_map->sg_vaddr;
992 physaddr = sg_map->sg_physaddr;
994 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
995 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
998 next_ccb->sg_list = segs;
999 next_ccb->sg_list_phys = physaddr;
1000 next_ccb->flags = BCCB_FREE;
1001 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
1005 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
1007 physaddr += (BT_NSEG * sizeof(bt_sg_t));
1012 /* Reserve a CCB for error recovery */
1013 if (bt->recovery_bccb == NULL) {
1014 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1015 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1018 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
1022 device_printf(bt->dev, "Can't malloc BCCBs\n");
1025 static __inline void
1026 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1031 if ((bccb->flags & BCCB_ACTIVE) != 0)
1032 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1033 if (bt->resource_shortage != 0
1034 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1035 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1036 bt->resource_shortage = FALSE;
1038 bccb->flags = BCCB_FREE;
1039 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1044 static __inline struct bt_ccb*
1045 btgetccb(struct bt_softc *bt)
1047 struct bt_ccb* bccb;
1051 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1052 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1056 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1058 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1068 btaction(struct cam_sim *sim, union ccb *ccb)
1070 struct bt_softc *bt;
1072 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1074 bt = (struct bt_softc *)cam_sim_softc(sim);
1076 switch (ccb->ccb_h.func_code) {
1077 /* Common cases first */
1078 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1079 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1081 struct bt_ccb *bccb;
1082 struct bt_hccb *hccb;
1085 * get a bccb to use.
1087 if ((bccb = btgetccb(bt)) == NULL) {
1091 bt->resource_shortage = TRUE;
1093 xpt_freeze_simq(bt->sim, /*count*/1);
1094 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1102 * So we can find the BCCB when an abort is requested
1105 ccb->ccb_h.ccb_bccb_ptr = bccb;
1106 ccb->ccb_h.ccb_bt_ptr = bt;
1109 * Put all the arguments for the xfer in the bccb
1111 hccb->target_id = ccb->ccb_h.target_id;
1112 hccb->target_lun = ccb->ccb_h.target_lun;
1116 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1117 struct ccb_scsiio *csio;
1118 struct ccb_hdr *ccbh;
1121 ccbh = &csio->ccb_h;
1122 hccb->opcode = INITIATOR_CCB_WRESID;
1123 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1124 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1125 hccb->cmd_len = csio->cdb_len;
1126 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1127 ccb->ccb_h.status = CAM_REQ_INVALID;
1128 btfreeccb(bt, bccb);
1132 hccb->sense_len = csio->sense_len;
1133 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1134 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1135 hccb->tag_enable = TRUE;
1136 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1138 hccb->tag_enable = FALSE;
1141 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1142 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1143 bcopy(csio->cdb_io.cdb_ptr,
1144 hccb->scsi_cdb, hccb->cmd_len);
1146 /* I guess I could map it in... */
1147 ccbh->status = CAM_REQ_INVALID;
1148 btfreeccb(bt, bccb);
1153 bcopy(csio->cdb_io.cdb_bytes,
1154 hccb->scsi_cdb, hccb->cmd_len);
1156 /* If need be, bounce our sense buffer */
1157 if (bt->sense_buffers != NULL) {
1158 hccb->sense_addr = btsensepaddr(bt, bccb);
1160 hccb->sense_addr = vtophys(&csio->sense_data);
1163 * If we have any data to send with this command,
1164 * map it into bus space.
1166 /* Only use S/G if there is a transfer */
1167 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1168 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
1170 * We've been given a pointer
1171 * to a single buffer.
1173 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
1178 error = bus_dmamap_load(
1186 if (error == EINPROGRESS) {
1189 * ordering, freeze the
1191 * until our mapping is
1194 xpt_freeze_simq(bt->sim,
1196 csio->ccb_h.status |=
1201 struct bus_dma_segment seg;
1203 /* Pointer to physical buffer */
1205 (bus_addr_t)csio->data_ptr;
1206 seg.ds_len = csio->dxfer_len;
1207 btexecuteccb(bccb, &seg, 1, 0);
1210 struct bus_dma_segment *segs;
1212 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1213 panic("btaction - Physical "
1217 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1218 panic("btaction - Virtual "
1219 "segment addresses "
1222 /* Just use the segments provided */
1223 segs = (struct bus_dma_segment *)
1225 btexecuteccb(bccb, segs,
1226 csio->sglist_cnt, 0);
1229 btexecuteccb(bccb, NULL, 0, 0);
1232 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1233 /* No data transfer */
1234 hccb->datain = TRUE;
1235 hccb->dataout = TRUE;
1237 hccb->sense_len = 0;
1238 hccb->tag_enable = FALSE;
1240 btexecuteccb(bccb, NULL, 0, 0);
1244 case XPT_EN_LUN: /* Enable LUN as a target */
1245 case XPT_TARGET_IO: /* Execute target I/O request */
1246 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1247 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1248 case XPT_ABORT: /* Abort the specified CCB */
1250 ccb->ccb_h.status = CAM_REQ_INVALID;
1253 case XPT_SET_TRAN_SETTINGS:
1256 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1260 case XPT_GET_TRAN_SETTINGS:
1261 /* Get default/user set transfer settings for the target */
1263 struct ccb_trans_settings *cts;
1267 target_mask = 0x01 << ccb->ccb_h.target_id;
1268 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1270 if ((bt->disc_permitted & target_mask) != 0)
1271 cts->flags |= CCB_TRANS_DISC_ENB;
1272 if ((bt->tags_permitted & target_mask) != 0)
1273 cts->flags |= CCB_TRANS_TAG_ENB;
1274 if ((bt->wide_permitted & target_mask) != 0)
1275 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1277 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1278 if ((bt->ultra_permitted & target_mask) != 0)
1279 cts->sync_period = 12;
1280 else if ((bt->fast_permitted & target_mask) != 0)
1281 cts->sync_period = 25;
1282 else if ((bt->sync_permitted & target_mask) != 0)
1283 cts->sync_period = 50;
1285 cts->sync_period = 0;
1287 if (cts->sync_period != 0)
1288 cts->sync_offset = 15;
1290 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1291 | CCB_TRANS_SYNC_OFFSET_VALID
1292 | CCB_TRANS_BUS_WIDTH_VALID
1293 | CCB_TRANS_DISC_VALID
1294 | CCB_TRANS_TQ_VALID;
1296 btfetchtransinfo(bt, cts);
1299 ccb->ccb_h.status = CAM_REQ_CMP;
1303 case XPT_CALC_GEOMETRY:
1305 struct ccb_calc_geometry *ccg;
1307 u_int32_t secs_per_cylinder;
1310 size_mb = ccg->volume_size
1311 / ((1024L * 1024L) / ccg->block_size);
1313 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1314 if (size_mb >= 2048) {
1316 ccg->secs_per_track = 63;
1319 ccg->secs_per_track = 32;
1323 ccg->secs_per_track = 32;
1325 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1326 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1327 ccb->ccb_h.status = CAM_REQ_CMP;
1331 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1333 btreset(bt, /*hardreset*/TRUE);
1334 ccb->ccb_h.status = CAM_REQ_CMP;
1338 case XPT_TERM_IO: /* Terminate the I/O process */
1340 ccb->ccb_h.status = CAM_REQ_INVALID;
1343 case XPT_PATH_INQ: /* Path routing inquiry */
1345 struct ccb_pathinq *cpi = &ccb->cpi;
1347 cpi->version_num = 1; /* XXX??? */
1348 cpi->hba_inquiry = PI_SDTR_ABLE;
1349 if (bt->tag_capable != 0)
1350 cpi->hba_inquiry |= PI_TAG_ABLE;
1351 if (bt->wide_bus != 0)
1352 cpi->hba_inquiry |= PI_WIDE_16;
1353 cpi->target_sprt = 0;
1355 cpi->hba_eng_cnt = 0;
1356 cpi->max_target = bt->wide_bus ? 15 : 7;
1358 cpi->initiator_id = bt->scsi_id;
1359 cpi->bus_id = cam_sim_bus(sim);
1360 cpi->base_transfer_speed = 3300;
1361 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1362 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1363 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1364 cpi->unit_number = cam_sim_unit(sim);
1365 cpi->ccb_h.status = CAM_REQ_CMP;
1370 ccb->ccb_h.status = CAM_REQ_INVALID;
1377 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1379 struct bt_ccb *bccb;
1381 struct bt_softc *bt;
1384 bccb = (struct bt_ccb *)arg;
1386 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1390 device_printf(bt->dev,
1391 "Unexepected error 0x%x returned from "
1392 "bus_dmamap_load\n", error);
1393 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1394 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1395 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1397 btfreeccb(bt, bccb);
1404 bus_dma_segment_t *end_seg;
1405 bus_dmasync_op_t op;
1407 end_seg = dm_segs + nseg;
1409 /* Copy the segments into our SG list */
1411 while (dm_segs < end_seg) {
1412 sg->len = dm_segs->ds_len;
1413 sg->addr = dm_segs->ds_addr;
1419 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1420 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1421 bccb->hccb.data_addr = bccb->sg_list_phys;
1423 bccb->hccb.data_len = bccb->sg_list->len;
1424 bccb->hccb.data_addr = bccb->sg_list->addr;
1427 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1428 op = BUS_DMASYNC_PREREAD;
1430 op = BUS_DMASYNC_PREWRITE;
1432 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1435 bccb->hccb.opcode = INITIATOR_CCB;
1436 bccb->hccb.data_len = 0;
1437 bccb->hccb.data_addr = 0;
1443 * Last time we need to check if this CCB needs to
1446 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1448 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1449 btfreeccb(bt, bccb);
1455 bccb->flags = BCCB_ACTIVE;
1456 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1457 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1459 ccb->ccb_h.timeout_ch =
1460 timeout(bttimeout, (caddr_t)bccb,
1461 (ccb->ccb_h.timeout * hz) / 1000);
1463 /* Tell the adapter about this command */
1464 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1465 if (bt->cur_outbox->action_code != BMBO_FREE) {
1467 * We should never encounter a busy mailbox.
1468 * If we do, warn the user, and treat it as
1469 * a resource shortage. If the controller is
1470 * hung, one of the pending transactions will
1471 * timeout causing us to start recovery operations.
1473 device_printf(bt->dev,
1474 "Encountered busy mailbox with %d out of %d "
1475 "commands active!!!\n", bt->active_ccbs,
1477 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1479 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1480 btfreeccb(bt, bccb);
1481 bt->resource_shortage = TRUE;
1482 xpt_freeze_simq(bt->sim, /*count*/1);
1483 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1487 bt->cur_outbox->action_code = BMBO_START;
1488 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1496 struct bt_softc *bt;
1499 bt = (struct bt_softc *)arg;
1500 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1502 if ((intstat & CMD_COMPLETE) != 0) {
1503 bt->latched_status = bt_inb(bt, STATUS_REG);
1504 bt->command_cmp = TRUE;
1507 bt_outb(bt, CONTROL_REG, RESET_INTR);
1509 if ((intstat & IMB_LOADED) != 0) {
1510 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1512 btccbptov(bt, bt->cur_inbox->ccb_addr),
1513 bt->cur_inbox->comp_code);
1514 bt->cur_inbox->comp_code = BMBI_FREE;
1519 if ((intstat & SCSI_BUS_RESET) != 0) {
1520 btreset(bt, /*hardreset*/FALSE);
1526 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1529 struct ccb_scsiio *csio;
1532 csio = &bccb->ccb->csio;
1534 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1535 device_printf(bt->dev,
1536 "btdone - Attempt to free non-active BCCB %p\n",
1541 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1542 bus_dmasync_op_t op;
1544 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1545 op = BUS_DMASYNC_POSTREAD;
1547 op = BUS_DMASYNC_POSTWRITE;
1548 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1549 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1552 if (bccb == bt->recovery_bccb) {
1554 * The recovery BCCB does not have a CCB associated
1555 * with it, so short circuit the normal error handling.
1556 * We now traverse our list of pending CCBs and process
1557 * any that were terminated by the recovery CCBs action.
1558 * We also reinstate timeouts for all remaining, pending,
1561 struct cam_path *path;
1562 struct ccb_hdr *ccb_h;
1565 /* Notify all clients that a BDR occured */
1566 error = xpt_create_path(&path, /*periph*/NULL,
1567 cam_sim_path(bt->sim),
1568 bccb->hccb.target_id,
1571 if (error == CAM_REQ_CMP)
1572 xpt_async(AC_SENT_BDR, path, NULL);
1574 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1575 while (ccb_h != NULL) {
1576 struct bt_ccb *pending_bccb;
1578 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1579 if (pending_bccb->hccb.target_id
1580 == bccb->hccb.target_id) {
1581 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1582 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1583 btdone(bt, pending_bccb, BMBI_ERROR);
1586 timeout(bttimeout, (caddr_t)pending_bccb,
1587 (ccb_h->timeout * hz) / 1000);
1588 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1591 device_printf(bt->dev, "No longer in timeout\n");
1595 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1597 switch (comp_code) {
1599 device_printf(bt->dev,
1600 "btdone - CCB completed with free status!\n");
1602 case BMBI_NOT_FOUND:
1603 device_printf(bt->dev,
1604 "btdone - CCB Abort failed to find CCB\n");
1609 printf("bt: ccb %p - error %x occured. "
1610 "btstat = %x, sdstat = %x\n",
1611 (void *)bccb, comp_code, bccb->hccb.btstat,
1614 /* An error occured */
1615 switch(bccb->hccb.btstat) {
1616 case BTSTAT_DATARUN_ERROR:
1617 if (bccb->hccb.data_len == 0) {
1619 * At least firmware 4.22, does this
1620 * for a QUEUE FULL condition.
1622 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1623 } else if (bccb->hccb.data_len < 0) {
1624 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1628 case BTSTAT_NOERROR:
1629 case BTSTAT_LINKED_CMD_COMPLETE:
1630 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1631 case BTSTAT_DATAUNDERUN_ERROR:
1633 csio->scsi_status = bccb->hccb.sdstat;
1634 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1635 switch(csio->scsi_status) {
1636 case SCSI_STATUS_CHECK_COND:
1637 case SCSI_STATUS_CMD_TERMINATED:
1638 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1639 /* Bounce sense back if necessary */
1640 if (bt->sense_buffers != NULL) {
1642 *btsensevaddr(bt, bccb);
1647 case SCSI_STATUS_OK:
1648 csio->ccb_h.status = CAM_REQ_CMP;
1651 csio->resid = bccb->hccb.data_len;
1653 case BTSTAT_SELTIMEOUT:
1654 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1656 case BTSTAT_UNEXPECTED_BUSFREE:
1657 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1659 case BTSTAT_INVALID_PHASE:
1660 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1662 case BTSTAT_INVALID_ACTION_CODE:
1663 panic("%s: Inavlid Action code", bt_name(bt));
1665 case BTSTAT_INVALID_OPCODE:
1666 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1668 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1669 /* We don't even support linked commands... */
1670 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1672 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1673 panic("%s: Invalid CCB or SG list", bt_name(bt));
1675 case BTSTAT_AUTOSENSE_FAILED:
1676 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1678 case BTSTAT_TAGGED_MSG_REJECTED:
1680 struct ccb_trans_settings neg;
1682 xpt_print_path(csio->ccb_h.path);
1683 printf("refuses tagged commands. Performing "
1684 "non-tagged I/O\n");
1686 neg.valid = CCB_TRANS_TQ_VALID;
1687 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1689 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1690 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1691 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1694 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1696 * XXX You would think that this is
1697 * a recoverable error... Hmmm.
1699 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1701 case BTSTAT_HA_SOFTWARE_ERROR:
1702 case BTSTAT_HA_WATCHDOG_ERROR:
1703 case BTSTAT_HARDWARE_FAILURE:
1704 /* Hardware reset ??? Can we recover ??? */
1705 csio->ccb_h.status = CAM_NO_HBA;
1707 case BTSTAT_TARGET_IGNORED_ATN:
1708 case BTSTAT_OTHER_SCSI_BUS_RESET:
1709 case BTSTAT_HA_SCSI_BUS_RESET:
1710 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1712 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1715 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1716 csio->ccb_h.status = CAM_BDR_SENT;
1718 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1720 case BTSTAT_INVALID_RECONNECT:
1721 case BTSTAT_ABORT_QUEUE_GENERATED:
1722 csio->ccb_h.status = CAM_REQ_TERMIO;
1724 case BTSTAT_SCSI_PERROR_DETECTED:
1725 csio->ccb_h.status = CAM_UNCOR_PARITY;
1728 if (csio->ccb_h.status != CAM_REQ_CMP) {
1729 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1730 csio->ccb_h.status |= CAM_DEV_QFRZN;
1732 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1733 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1734 btfreeccb(bt, bccb);
1738 /* All completed without incident */
1739 ccb->ccb_h.status |= CAM_REQ_CMP;
1740 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1741 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1742 btfreeccb(bt, bccb);
1749 btreset(struct bt_softc* bt, int hard_reset)
1751 struct ccb_hdr *ccb_h;
1754 u_int8_t reset_type;
1756 if (hard_reset != 0)
1757 reset_type = HARD_RESET;
1759 reset_type = SOFT_RESET;
1760 bt_outb(bt, CONTROL_REG, reset_type);
1762 /* Wait 5sec. for Diagnostic start */
1763 timeout = 5 * 10000;
1765 status = bt_inb(bt, STATUS_REG);
1766 if ((status & DIAG_ACTIVE) != 0)
1772 printf("%s: btreset - Diagnostic Active failed to "
1773 "assert. status = 0x%x\n", bt_name(bt), status);
1777 /* Wait 10sec. for Diagnostic end */
1778 timeout = 10 * 10000;
1780 status = bt_inb(bt, STATUS_REG);
1781 if ((status & DIAG_ACTIVE) == 0)
1786 panic("%s: btreset - Diagnostic Active failed to drop. "
1787 "status = 0x%x\n", bt_name(bt), status);
1791 /* Wait for the host adapter to become ready or report a failure */
1794 status = bt_inb(bt, STATUS_REG);
1795 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1800 printf("%s: btreset - Host adapter failed to come ready. "
1801 "status = 0x%x\n", bt_name(bt), status);
1805 /* If the diagnostics failed, tell the user */
1806 if ((status & DIAG_FAIL) != 0
1807 || (status & HA_READY) == 0) {
1808 printf("%s: btreset - Adapter failed diagnostics\n",
1811 if ((status & DATAIN_REG_READY) != 0)
1812 printf("%s: btreset - Host Adapter Error code = 0x%x\n",
1813 bt_name(bt), bt_inb(bt, DATAIN_REG));
1817 /* If we've allocated mailboxes, initialize them */
1818 if (bt->init_level > 4)
1821 /* If we've attached to the XPT, tell it about the event */
1822 if (bt->path != NULL)
1823 xpt_async(AC_BUS_RESET, bt->path, NULL);
1826 * Perform completion processing for all outstanding CCBs.
1828 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1829 struct bt_ccb *pending_bccb;
1831 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1832 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1833 btdone(bt, pending_bccb, BMBI_ERROR);
1840 * Send a command to the adapter.
1843 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1844 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1850 u_int reply_buf_size;
1855 /* No data returned to start */
1856 reply_buf_size = reply_len;
1863 bt->command_cmp = 0;
1865 * Wait up to 10 sec. for the adapter to become
1866 * ready to accept commands.
1870 status = bt_inb(bt, STATUS_REG);
1871 if ((status & HA_READY) != 0
1872 && (status & CMD_REG_BUSY) == 0)
1875 * Throw away any pending data which may be
1876 * left over from earlier commands that we
1879 if ((status & DATAIN_REG_READY) != 0)
1880 (void)bt_inb(bt, DATAIN_REG);
1884 printf("%s: bt_cmd: Timeout waiting for adapter ready, "
1885 "status = 0x%x\n", bt_name(bt), status);
1890 * Send the opcode followed by any necessary parameter bytes.
1892 bt_outb(bt, COMMAND_REG, opcode);
1895 * Wait for up to 1sec for each byte of the the
1896 * parameter list sent to be sent.
1899 while (param_len && --timeout) {
1902 status = bt_inb(bt, STATUS_REG);
1903 intstat = bt_inb(bt, INTSTAT_REG);
1906 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1907 == (INTR_PENDING|CMD_COMPLETE)) {
1908 saved_status = status;
1912 if (bt->command_cmp != 0) {
1913 saved_status = bt->latched_status;
1917 if ((status & DATAIN_REG_READY) != 0)
1919 if ((status & CMD_REG_BUSY) == 0) {
1920 bt_outb(bt, COMMAND_REG, *params++);
1926 printf("%s: bt_cmd: Timeout sending parameters, "
1927 "status = 0x%x\n", bt_name(bt), status);
1929 saved_status = status;
1934 * Wait for the command to complete.
1936 while (cmd_complete == 0 && --cmd_timeout) {
1939 status = bt_inb(bt, STATUS_REG);
1940 intstat = bt_inb(bt, INTSTAT_REG);
1942 * It may be that this command was issued with
1943 * controller interrupts disabled. We'll never
1944 * get to our command if an incoming mailbox
1945 * interrupt is pending, so take care of completed
1946 * mailbox commands by calling our interrupt handler.
1948 if ((intstat & (INTR_PENDING|IMB_LOADED))
1949 == (INTR_PENDING|IMB_LOADED))
1953 if (bt->command_cmp != 0) {
1955 * Our interrupt handler saw CMD_COMPLETE
1956 * status before we did.
1959 saved_status = bt->latched_status;
1960 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1961 == (INTR_PENDING|CMD_COMPLETE)) {
1963 * Our poll (in case interrupts are blocked)
1964 * saw the CMD_COMPLETE interrupt.
1967 saved_status = status;
1968 } else if (opcode == BOP_MODIFY_IO_ADDR
1969 && (status & CMD_REG_BUSY) == 0) {
1971 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1972 * but it should update the status register. So, we
1973 * consider this command complete when the CMD_REG_BUSY
1976 saved_status = status;
1978 } else if ((status & DATAIN_REG_READY) != 0) {
1981 data = bt_inb(bt, DATAIN_REG);
1982 if (reply_len < reply_buf_size) {
1983 *reply_data++ = data;
1985 printf("%s: bt_cmd - Discarded reply data byte "
1986 "for opcode 0x%x\n", bt_name(bt),
1990 * Reset timeout to ensure at least a second
1991 * between response bytes.
1993 cmd_timeout = MAX(cmd_timeout, 10000);
1996 } else if ((opcode == BOP_FETCH_LRAM)
1997 && (status & HA_READY) != 0) {
1998 saved_status = status;
2003 if (cmd_timeout == 0) {
2004 printf("%s: bt_cmd: Timeout waiting for command (%x) "
2005 "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
2006 "rlen %d\n", bt_name(bt), opcode,
2007 bt_name(bt), status, intstat, reply_len);
2008 error = (ETIMEDOUT);
2012 * Clear any pending interrupts. Block interrupts so our
2013 * interrupt handler is not re-entered.
2023 * If the command was rejected by the controller, tell the caller.
2025 if ((saved_status & CMD_INVALID) != 0) {
2027 * Some early adapters may not recover properly from
2028 * an invalid command. If it appears that the controller
2029 * has wedged (i.e. status was not cleared by our interrupt
2030 * reset above), perform a soft reset.
2033 printf("%s: Invalid Command 0x%x\n", bt_name(bt),
2036 status = bt_inb(bt, STATUS_REG);
2037 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2038 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2039 || (status & (HA_READY|INIT_REQUIRED))
2040 != (HA_READY|INIT_REQUIRED)) {
2041 btreset(bt, /*hard_reset*/FALSE);
2046 if (param_len > 0) {
2047 /* The controller did not accept the full argument list */
2051 if (reply_len != reply_buf_size) {
2052 /* Too much or too little data received */
2056 /* We were successful */
2061 btinitmboxes(struct bt_softc *bt) {
2062 init_32b_mbox_params_t init_mbox;
2065 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2066 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2067 bt->cur_inbox = bt->in_boxes;
2068 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2069 bt->cur_outbox = bt->out_boxes;
2070 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2072 /* Tell the adapter about them */
2073 init_mbox.num_boxes = bt->num_boxes;
2074 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2075 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2076 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2077 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2078 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2079 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2080 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2083 printf("btinitmboxes: Initialization command failed\n");
2084 else if (bt->strict_rr != 0) {
2086 * If the controller supports
2087 * strict round robin mode,
2093 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1,
2094 /*reply_buf*/NULL, /*reply_len*/0,
2095 DEFAULT_CMD_TIMEOUT);
2098 printf("btinitmboxes: Unable to enable strict RR\n");
2100 } else if (bootverbose) {
2101 printf("%s: Using Strict Round Robin Mailbox Mode\n",
2110 * Update the XPT's idea of the negotiated transfer
2111 * parameters for a particular target.
2114 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings* cts)
2116 setup_data_t setup_info;
2123 targ_syncinfo_t sync_info;
2125 target = cts->ccb_h.target_id;
2126 targ_offset = (target & 0x7);
2127 targ_mask = (0x01 << targ_offset);
2130 * Inquire Setup Information. This command retreives the
2131 * Wide negotiation status for recent adapters as well as
2132 * the sync info for older models.
2134 param = sizeof(setup_info);
2135 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
2136 (u_int8_t*)&setup_info, sizeof(setup_info),
2137 DEFAULT_CMD_TIMEOUT);
2140 printf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
2141 bt_name(bt), error);
2146 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2147 : setup_info.high_syncinfo[targ_offset];
2149 if (sync_info.sync == 0)
2150 cts->sync_offset = 0;
2152 cts->sync_offset = sync_info.offset;
2154 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2155 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2159 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2160 : (setup_info.high_wide_active & targ_mask);
2163 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2164 } else if ((bt->wide_permitted & targ_mask) != 0) {
2165 struct ccb_getdev cgd;
2168 * Prior to rev 5.06L, wide status isn't provided,
2169 * so we "guess" that wide transfers are in effect
2170 * if the user settings allow for wide and the inquiry
2171 * data for the device indicates that it can handle
2174 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2175 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2176 xpt_action((union ccb *)&cgd);
2177 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2178 && (cgd.inq_data.flags & SID_WBus16) != 0)
2179 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2182 if (bt->firmware_ver[0] >= '3') {
2184 * For adapters that can do fast or ultra speeds,
2185 * use the more exact Target Sync Information command.
2187 target_sync_info_data_t sync_info;
2189 param = sizeof(sync_info);
2190 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1,
2191 (u_int8_t*)&sync_info, sizeof(sync_info),
2192 DEFAULT_CMD_TIMEOUT);
2195 printf("%s: btfetchtransinfo - Inquire Sync "
2196 "Info Failed 0x%x\n", bt_name(bt), error);
2200 sync_period = sync_info.sync_rate[target] * 100;
2202 sync_period = 2000 + (500 * sync_info.period);
2205 /* Convert ns value to standard SCSI sync rate */
2206 if (cts->sync_offset != 0)
2207 cts->sync_period = scsi_calc_syncparam(sync_period);
2209 cts->sync_period = 0;
2211 cts->valid = CCB_TRANS_SYNC_RATE_VALID
2212 | CCB_TRANS_SYNC_OFFSET_VALID
2213 | CCB_TRANS_BUS_WIDTH_VALID;
2214 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2218 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2220 struct bt_softc* bt;
2222 bt = (struct bt_softc*)arg;
2223 bt->mailbox_physbase = segs->ds_addr;
2227 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2229 struct bt_softc* bt;
2231 bt = (struct bt_softc*)arg;
2232 bt->bt_ccb_physbase = segs->ds_addr;
2236 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2239 struct bt_softc* bt;
2241 bt = (struct bt_softc*)arg;
2242 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2246 btpoll(struct cam_sim *sim)
2248 bt_intr(cam_sim_softc(sim));
2252 bttimeout(void *arg)
2254 struct bt_ccb *bccb;
2256 struct bt_softc *bt;
2259 bccb = (struct bt_ccb *)arg;
2261 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2262 xpt_print_path(ccb->ccb_h.path);
2263 printf("CCB %p - timed out\n", (void *)bccb);
2267 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2268 xpt_print_path(ccb->ccb_h.path);
2269 printf("CCB %p - timed out CCB already completed\n",
2276 * In order to simplify the recovery process, we ask the XPT
2277 * layer to halt the queue of new transactions and we traverse
2278 * the list of pending CCBs and remove their timeouts. This
2279 * means that the driver attempts to clear only one error
2280 * condition at a time. In general, timeouts that occur
2281 * close together are related anyway, so there is no benefit
2282 * in attempting to handle errors in parrallel. Timeouts will
2283 * be reinstated when the recovery process ends.
2285 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2286 struct ccb_hdr *ccb_h;
2288 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2289 xpt_freeze_simq(bt->sim, /*count*/1);
2290 bccb->flags |= BCCB_RELEASE_SIMQ;
2293 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2294 while (ccb_h != NULL) {
2295 struct bt_ccb *pending_bccb;
2297 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2298 untimeout(bttimeout, pending_bccb, ccb_h->timeout_ch);
2299 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2303 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2304 || bt->cur_outbox->action_code != BMBO_FREE
2305 || ((bccb->hccb.tag_enable == TRUE)
2306 && (bt->firmware_ver[0] < '5'))) {
2308 * Try a full host adapter/SCSI bus reset.
2309 * We do this only if we have already attempted
2310 * to clear the condition with a BDR, or we cannot
2311 * attempt a BDR for lack of mailbox resources
2312 * or because of faulty firmware. It turns out
2313 * that firmware versions prior to 5.xx treat BDRs
2314 * as untagged commands that cannot be sent until
2315 * all outstanding tagged commands have been processed.
2316 * This makes it somewhat difficult to use a BDR to
2317 * clear up a problem with an uncompleted tagged command.
2319 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2320 btreset(bt, /*hardreset*/TRUE);
2321 printf("%s: No longer in timeout\n", bt_name(bt));
2324 * Send a Bus Device Reset message:
2325 * The target that is holding up the bus may not
2326 * be the same as the one that triggered this timeout
2327 * (different commands have different timeout lengths),
2328 * but we have no way of determining this from our
2329 * timeout handler. Our strategy here is to queue a
2330 * BDR message to the target of the timed out command.
2331 * If this fails, we'll get another timeout 2 seconds
2332 * later which will attempt a bus reset.
2334 bccb->flags |= BCCB_DEVICE_RESET;
2335 ccb->ccb_h.timeout_ch =
2336 timeout(bttimeout, (caddr_t)bccb, 2 * hz);
2338 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2340 /* No Data Transfer */
2341 bt->recovery_bccb->hccb.datain = TRUE;
2342 bt->recovery_bccb->hccb.dataout = TRUE;
2343 bt->recovery_bccb->hccb.btstat = 0;
2344 bt->recovery_bccb->hccb.sdstat = 0;
2345 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2347 /* Tell the adapter about this command */
2348 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2349 bt->cur_outbox->action_code = BMBO_START;
2350 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);