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.6 2004/03/15 01:10:43 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>
68 #include <bus/cam/cam.h>
69 #include <bus/cam/cam_ccb.h>
70 #include <bus/cam/cam_sim.h>
71 #include <bus/cam/cam_xpt_sim.h>
72 #include <bus/cam/cam_debug.h>
74 #include <bus/cam/scsi/scsi_message.h>
81 /* MailBox Management functions */
82 static __inline void btnextinbox(struct bt_softc *bt);
83 static __inline void btnextoutbox(struct bt_softc *bt);
86 btnextinbox(struct bt_softc *bt)
88 if (bt->cur_inbox == bt->last_inbox)
89 bt->cur_inbox = bt->in_boxes;
95 btnextoutbox(struct bt_softc *bt)
97 if (bt->cur_outbox == bt->last_outbox)
98 bt->cur_outbox = bt->out_boxes;
103 /* CCB Mangement functions */
104 static __inline u_int32_t btccbvtop(struct bt_softc *bt,
105 struct bt_ccb *bccb);
106 static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
108 static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
109 struct bt_ccb *bccb);
110 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
111 struct bt_ccb *bccb);
113 static __inline u_int32_t
114 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
116 return (bt->bt_ccb_physbase
117 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
120 static __inline struct bt_ccb *
121 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
123 return (bt->bt_ccb_array +
124 ((struct bt_ccb*)ccb_addr-(struct bt_ccb*)bt->bt_ccb_physbase));
127 static __inline u_int32_t
128 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
132 index = (u_int)(bccb - bt->bt_ccb_array);
133 return (bt->sense_buffers_physbase
134 + (index * sizeof(struct scsi_sense_data)));
137 static __inline struct scsi_sense_data *
138 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
142 index = (u_int)(bccb - bt->bt_ccb_array);
143 return (bt->sense_buffers + index);
146 static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
147 static __inline void btfreeccb(struct bt_softc *bt,
148 struct bt_ccb *bccb);
149 static void btallocccbs(struct bt_softc *bt);
150 static bus_dmamap_callback_t btexecuteccb;
151 static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
152 bt_mbi_comp_code_t comp_code);
154 /* Host adapter command functions */
155 static int btreset(struct bt_softc* bt, int hard_reset);
157 /* Initialization functions */
158 static int btinitmboxes(struct bt_softc *bt);
159 static bus_dmamap_callback_t btmapmboxes;
160 static bus_dmamap_callback_t btmapccbs;
161 static bus_dmamap_callback_t btmapsgs;
163 /* Transfer Negotiation Functions */
164 static void btfetchtransinfo(struct bt_softc *bt,
165 struct ccb_trans_settings *cts);
167 /* CAM SIM entry points */
168 #define ccb_bccb_ptr spriv_ptr0
169 #define ccb_bt_ptr spriv_ptr1
170 static void btaction(struct cam_sim *sim, union ccb *ccb);
171 static void btpoll(struct cam_sim *sim);
173 /* Our timeout handler */
180 * Do our own re-probe protection until a configuration
181 * manager can do it for us. This ensures that we don't
182 * reprobe a card already found by the EISA or PCI probes.
184 struct bt_isa_port bt_isa_ports[] =
195 * I/O ports listed in the order enumerated by the
196 * card for certain op codes.
198 u_int16_t bt_board_ports[] =
208 /* Exported functions */
210 bt_init_softc(device_t dev, struct resource *port,
211 struct resource *irq, struct resource *drq)
213 struct bt_softc *bt = device_get_softc(dev);
215 SLIST_INIT(&bt->free_bt_ccbs);
216 LIST_INIT(&bt->pending_ccbs);
217 SLIST_INIT(&bt->sg_maps);
219 bt->unit = device_get_unit(dev);
223 bt->tag = rman_get_bustag(port);
224 bt->bsh = rman_get_bushandle(port);
228 bt_free_softc(device_t dev)
230 struct bt_softc *bt = device_get_softc(dev);
232 switch (bt->init_level) {
235 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
237 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
240 bus_dma_tag_destroy(bt->sense_dmat);
243 struct sg_map_node *sg_map;
245 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
246 SLIST_REMOVE_HEAD(&bt->sg_maps, links);
247 bus_dmamap_unload(bt->sg_dmat,
249 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
251 free(sg_map, M_DEVBUF);
253 bus_dma_tag_destroy(bt->sg_dmat);
256 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
258 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
260 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
262 bus_dma_tag_destroy(bt->ccb_dmat);
264 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
266 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
268 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
270 bus_dma_tag_destroy(bt->buffer_dmat);
272 bus_dma_tag_destroy(bt->mailbox_dmat);
279 bt_port_probe(device_t dev, struct bt_probe_info *info)
281 struct bt_softc *bt = device_get_softc(dev);
282 config_data_t config_data;
285 /* See if there is really a card present */
286 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
290 * Determine our IRQ, and DMA settings and
291 * export them to the configuration system.
293 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
294 (u_int8_t*)&config_data, sizeof(config_data),
295 DEFAULT_CMD_TIMEOUT);
297 printf("bt_port_probe: Could not determine IRQ or DMA "
298 "settings for adapter.\n");
302 if (bt->model[0] == '5') {
303 /* DMA settings only make sense for ISA cards */
304 switch (config_data.dma_chan) {
315 printf("bt_port_probe: Invalid DMA setting "
316 "detected for adapter.\n");
320 /* VL/EISA/PCI DMA */
323 switch (config_data.irq) {
330 info->irq = ffs(config_data.irq) + 8;
333 printf("bt_port_probe: Invalid IRQ setting %x"
334 "detected for adapter.\n", config_data.irq);
341 * Probe the adapter and verify that the card is a BusLogic.
344 bt_probe(device_t dev)
346 struct bt_softc *bt = device_get_softc(dev);
347 esetup_info_data_t esetup_info;
355 * See if the three I/O ports look reasonable.
356 * Touch the minimal number of registers in the
359 status = bt_inb(bt, STATUS_REG);
361 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
362 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
364 device_printf(dev, "Failed Status Reg Test - %x\n",
369 intstat = bt_inb(bt, INTSTAT_REG);
370 if ((intstat & INTSTAT_REG_RSVD) != 0) {
371 device_printf(dev, "Failed Intstat Reg Test\n");
375 geometry = bt_inb(bt, GEOMETRY_REG);
376 if (geometry == 0xFF) {
378 device_printf(dev, "Failed Geometry Reg Test\n");
383 * Looking good so far. Final test is to reset the
384 * adapter and attempt to fetch the extended setup
385 * information. This should filter out all 1542 cards.
387 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
389 device_printf(dev, "Failed Reset\n");
393 param = sizeof(esetup_info);
394 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1,
395 (u_int8_t*)&esetup_info, sizeof(esetup_info),
396 DEFAULT_CMD_TIMEOUT);
405 * Pull the boards setup information and record it in our softc.
408 bt_fetch_adapter_info(device_t dev)
410 struct bt_softc *bt = device_get_softc(dev);
411 board_id_data_t board_id;
412 esetup_info_data_t esetup_info;
413 config_data_t config_data;
415 u_int8_t length_param;
417 /* First record the firmware version */
418 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
419 (u_int8_t*)&board_id, sizeof(board_id),
420 DEFAULT_CMD_TIMEOUT);
422 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
425 bt->firmware_ver[0] = board_id.firmware_rev_major;
426 bt->firmware_ver[1] = '.';
427 bt->firmware_ver[2] = board_id.firmware_rev_minor;
428 bt->firmware_ver[3] = '\0';
431 * Depending on the firmware major and minor version,
432 * we may be able to fetch additional minor version info.
434 if (bt->firmware_ver[0] > '0') {
436 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
437 (u_int8_t*)&bt->firmware_ver[3], 1,
438 DEFAULT_CMD_TIMEOUT);
441 "bt_fetch_adapter_info - Failed Get "
442 "Firmware 3rd Digit\n");
445 if (bt->firmware_ver[3] == ' ')
446 bt->firmware_ver[3] = '\0';
447 bt->firmware_ver[4] = '\0';
450 if (strcmp(bt->firmware_ver, "3.3") >= 0) {
452 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
453 (u_int8_t*)&bt->firmware_ver[4], 1,
454 DEFAULT_CMD_TIMEOUT);
457 "bt_fetch_adapter_info - Failed Get "
458 "Firmware 4th Digit\n");
461 if (bt->firmware_ver[4] == ' ')
462 bt->firmware_ver[4] = '\0';
463 bt->firmware_ver[5] = '\0';
467 * Some boards do not handle the "recently documented"
468 * Inquire Board Model Number command correctly or do not give
469 * exact information. Use the Firmware and Extended Setup
470 * information in these cases to come up with the right answer.
471 * The major firmware revision number indicates:
473 * 5.xx BusLogic "W" Series Host Adapters:
475 * 4.xx BusLogic "C" Series Host Adapters:
476 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
477 * 3.xx BusLogic "S" Series Host Adapters:
478 * BT-747S/747D/757S/757D/445S/545S/542D
479 * BT-542B/742A (revision H)
480 * 2.xx BusLogic "A" Series Host Adapters:
481 * BT-542B/742A (revision G and below)
482 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
484 length_param = sizeof(esetup_info);
485 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
486 (u_int8_t*)&esetup_info, sizeof(esetup_info),
487 DEFAULT_CMD_TIMEOUT);
492 bt->bios_addr = esetup_info.bios_addr << 12;
494 if (esetup_info.bus_type == 'A'
495 && bt->firmware_ver[0] == '2') {
496 snprintf(bt->model, sizeof(bt->model), "542B");
497 } else if (esetup_info.bus_type == 'E'
498 && (strncmp(bt->firmware_ver, "2.1", 3) == 0
499 || strncmp(bt->firmware_ver, "2.20", 4) == 0)) {
500 snprintf(bt->model, sizeof(bt->model), "742A");
501 } else if (esetup_info.bus_type == 'E'
502 && bt->firmware_ver[0] == '0') {
503 /* AMI FastDisk EISA Series 441 0.x */
504 snprintf(bt->model, sizeof(bt->model), "747A");
506 ha_model_data_t model_data;
509 length_param = sizeof(model_data);
510 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
511 (u_int8_t*)&model_data, sizeof(model_data),
512 DEFAULT_CMD_TIMEOUT);
515 "bt_fetch_adapter_info - Failed Inquire "
519 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
520 bt->model[i] = model_data.ascii_model[i];
521 if (bt->model[i] == ' ')
527 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
529 /* SG element limits */
530 bt->max_sg = esetup_info.max_sg;
532 /* Set feature flags */
533 bt->wide_bus = esetup_info.wide_bus;
534 bt->diff_bus = esetup_info.diff_bus;
535 bt->ultra_scsi = esetup_info.ultra_scsi;
537 if ((bt->firmware_ver[0] == '5')
538 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
539 bt->extended_lun = TRUE;
541 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
544 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
547 * Determine max CCB count and whether tagged queuing is
548 * available based on controller type. Tagged queuing
549 * only works on 'W' series adapters, 'C' series adapters
550 * with firmware of rev 4.42 and higher, and 'S' series
551 * adapters with firmware of rev 3.35 and higher. The
552 * maximum CCB counts are as follows:
554 * 192 BT-948/958/958D
555 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
557 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
559 if (bt->firmware_ver[0] == '5') {
561 bt->tag_capable = TRUE;
562 } else if (bt->firmware_ver[0] == '4') {
563 if (bt->model[0] == '5')
567 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
570 if (bt->firmware_ver[0] == '3'
571 && (strcmp(bt->firmware_ver, "3.35") >= 0))
572 bt->tag_capable = TRUE;
574 bt->tag_capable = FALSE;
577 if (bt->tag_capable != FALSE)
578 bt->tags_permitted = ALL_TARGETS;
580 /* Determine Sync/Wide/Disc settings */
581 if (bt->firmware_ver[0] >= '4') {
582 auto_scsi_data_t auto_scsi_data;
583 fetch_lram_params_t fetch_lram_params;
587 * These settings are stored in the
588 * AutoSCSI data in LRAM of 'W' and 'C'
591 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
592 fetch_lram_params.response_len = sizeof(auto_scsi_data);
593 error = bt_cmd(bt, BOP_FETCH_LRAM,
594 (u_int8_t*)&fetch_lram_params,
595 sizeof(fetch_lram_params),
596 (u_int8_t*)&auto_scsi_data,
597 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
601 "bt_fetch_adapter_info - Failed "
602 "Get Auto SCSI Info\n");
606 bt->disc_permitted = auto_scsi_data.low_disc_permitted
607 | (auto_scsi_data.high_disc_permitted << 8);
608 bt->sync_permitted = auto_scsi_data.low_sync_permitted
609 | (auto_scsi_data.high_sync_permitted << 8);
610 bt->fast_permitted = auto_scsi_data.low_fast_permitted
611 | (auto_scsi_data.high_fast_permitted << 8);
612 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
613 | (auto_scsi_data.high_ultra_permitted << 8);
614 bt->wide_permitted = auto_scsi_data.low_wide_permitted
615 | (auto_scsi_data.high_wide_permitted << 8);
617 if (bt->ultra_scsi == FALSE)
618 bt->ultra_permitted = 0;
620 if (bt->wide_bus == FALSE)
621 bt->wide_permitted = 0;
624 * 'S' and 'A' series have this information in the setup
625 * information structure.
627 setup_data_t setup_info;
629 length_param = sizeof(setup_info);
630 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
631 /*paramlen*/1, (u_int8_t*)&setup_info,
632 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
636 "bt_fetch_adapter_info - Failed "
641 if (setup_info.initiate_sync != 0) {
642 bt->sync_permitted = ALL_TARGETS;
644 if (bt->model[0] == '7') {
645 if (esetup_info.sync_neg10MB != 0)
646 bt->fast_permitted = ALL_TARGETS;
647 if (strcmp(bt->model, "757") == 0)
648 bt->wide_permitted = ALL_TARGETS;
651 bt->disc_permitted = ALL_TARGETS;
654 /* We need as many mailboxes as we can have ccbs */
655 bt->num_boxes = bt->max_ccbs;
657 /* Determine our SCSI ID */
659 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
660 (u_int8_t*)&config_data, sizeof(config_data),
661 DEFAULT_CMD_TIMEOUT);
664 "bt_fetch_adapter_info - Failed Get Config\n");
667 bt->scsi_id = config_data.scsi_id;
673 * Start the board, ready for normal operation
676 bt_init(device_t dev)
678 struct bt_softc *bt = device_get_softc(dev);
680 /* Announce the Adapter */
681 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
683 if (bt->ultra_scsi != 0)
686 if (bt->wide_bus != 0)
691 if (bt->diff_bus != 0)
694 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
698 * Create our DMA tags. These tags define the kinds of device
699 * accessible memory allocations and memory mappings we will
700 * need to perform during normal operation.
702 * Unless we need to further restrict the allocation, we rely
703 * on the restrictions of the parent dmat, hence the common
704 * use of MAXADDR and MAXSIZE.
707 /* DMA tag for mapping buffers into device visible space. */
708 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
709 /*lowaddr*/BUS_SPACE_MAXADDR,
710 /*highaddr*/BUS_SPACE_MAXADDR,
711 /*filter*/NULL, /*filterarg*/NULL,
712 /*maxsize*/MAXBSIZE, /*nsegments*/BT_NSEG,
713 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
714 /*flags*/BUS_DMA_ALLOCNOW,
715 &bt->buffer_dmat) != 0) {
720 /* DMA tag for our mailboxes */
721 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
722 /*lowaddr*/BUS_SPACE_MAXADDR,
723 /*highaddr*/BUS_SPACE_MAXADDR,
724 /*filter*/NULL, /*filterarg*/NULL,
725 bt->num_boxes * (sizeof(bt_mbox_in_t)
726 + sizeof(bt_mbox_out_t)),
728 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
729 /*flags*/0, &bt->mailbox_dmat) != 0) {
735 /* Allocation for our mailboxes */
736 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
737 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
743 /* And permanently map them */
744 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
746 bt->num_boxes * (sizeof(bt_mbox_in_t)
747 + sizeof(bt_mbox_out_t)),
748 btmapmboxes, bt, /*flags*/0);
752 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
756 /* DMA tag for our ccb structures */
757 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
758 /*lowaddr*/BUS_SPACE_MAXADDR,
759 /*highaddr*/BUS_SPACE_MAXADDR,
760 /*filter*/NULL, /*filterarg*/NULL,
761 bt->max_ccbs * sizeof(struct bt_ccb),
763 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
764 /*flags*/0, &bt->ccb_dmat) != 0) {
770 /* Allocation for our ccbs */
771 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
772 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
778 /* And permanently map them */
779 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
781 bt->max_ccbs * sizeof(struct bt_ccb),
782 btmapccbs, bt, /*flags*/0);
786 /* DMA tag for our S/G structures. We allocate in page sized chunks */
787 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
788 /*lowaddr*/BUS_SPACE_MAXADDR,
789 /*highaddr*/BUS_SPACE_MAXADDR,
790 /*filter*/NULL, /*filterarg*/NULL,
791 PAGE_SIZE, /*nsegments*/1,
792 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
793 /*flags*/0, &bt->sg_dmat) != 0) {
799 /* Perform initial CCB allocation */
800 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
803 if (bt->num_ccbs == 0) {
805 "bt_init - Unable to allocate initial ccbs\n");
810 * Note that we are going and return (to probe)
820 bt_attach(device_t dev)
822 struct bt_softc *bt = device_get_softc(dev);
823 int tagged_dev_openings;
824 struct cam_devq *devq;
828 * We reserve 1 ccb for error recovery, so don't
829 * tell the XPT about it.
831 if (bt->tag_capable != 0)
832 tagged_dev_openings = bt->max_ccbs - 1;
834 tagged_dev_openings = 0;
837 * Create the device queue for our SIM.
839 devq = cam_simq_alloc(bt->max_ccbs - 1);
844 * Construct our SIM entry
846 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
847 2, tagged_dev_openings, devq);
848 cam_simq_release(devq);
852 if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
853 cam_sim_free(bt->sim);
857 if (xpt_create_path(&bt->path, /*periph*/NULL,
858 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
859 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
860 xpt_bus_deregister(cam_sim_path(bt->sim));
861 cam_sim_free(bt->sim);
868 error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM,
869 bt_intr, bt, &bt->ih);
871 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
879 bt_check_probed_iop(u_int ioport)
883 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
884 if (bt_isa_ports[i].addr == ioport) {
885 if (bt_isa_ports[i].probed != 0)
896 bt_mark_probed_bio(isa_compat_io_t port)
898 if (port < BIO_DISABLED)
899 bt_mark_probed_iop(bt_board_ports[port]);
903 bt_mark_probed_iop(u_int ioport)
907 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
908 if (ioport == bt_isa_ports[i].addr) {
909 bt_isa_ports[i].probed = 1;
916 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
921 for (i = 0;i < BT_NUM_ISAPORTS; i++)
922 if (ioport <= bt_isa_ports[i].addr)
924 if ((i >= BT_NUM_ISAPORTS)
925 || (ioport != bt_isa_ports[i].addr)) {
926 printf("\nbt_isa_probe: Invalid baseport of 0x%x specified.\n"
927 "bt_isa_probe: Nearest valid baseport is 0x%x.\n"
928 "bt_isa_probe: Failing probe.\n",
930 (i < BT_NUM_ISAPORTS)
931 ? bt_isa_ports[i].addr
932 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
933 *port_index = *max_port_index = -1;
936 *port_index = *max_port_index = bt_isa_ports[i].bio;
939 *max_port_index = BT_NUM_ISAPORTS - 1;
944 bt_iop_from_bio(isa_compat_io_t bio_index)
946 if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
947 return (bt_board_ports[bio_index]);
953 btallocccbs(struct bt_softc *bt)
955 struct bt_ccb *next_ccb;
956 struct sg_map_node *sg_map;
962 if (bt->num_ccbs >= bt->max_ccbs)
963 /* Can't allocate any more */
966 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
968 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_WAITOK);
970 /* Allocate S/G space for the next batch of CCBS */
971 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
972 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
973 free(sg_map, M_DEVBUF);
977 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
979 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
980 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
982 segs = sg_map->sg_vaddr;
983 physaddr = sg_map->sg_physaddr;
985 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
986 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
989 next_ccb->sg_list = segs;
990 next_ccb->sg_list_phys = physaddr;
991 next_ccb->flags = BCCB_FREE;
992 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
996 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
998 physaddr += (BT_NSEG * sizeof(bt_sg_t));
1003 /* Reserve a CCB for error recovery */
1004 if (bt->recovery_bccb == NULL) {
1005 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1006 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1009 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
1013 device_printf(bt->dev, "Can't malloc BCCBs\n");
1016 static __inline void
1017 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1022 if ((bccb->flags & BCCB_ACTIVE) != 0)
1023 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1024 if (bt->resource_shortage != 0
1025 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1026 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1027 bt->resource_shortage = FALSE;
1029 bccb->flags = BCCB_FREE;
1030 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1035 static __inline struct bt_ccb*
1036 btgetccb(struct bt_softc *bt)
1038 struct bt_ccb* bccb;
1042 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1043 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1047 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1049 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1059 btaction(struct cam_sim *sim, union ccb *ccb)
1061 struct bt_softc *bt;
1063 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1065 bt = (struct bt_softc *)cam_sim_softc(sim);
1067 switch (ccb->ccb_h.func_code) {
1068 /* Common cases first */
1069 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1070 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1072 struct bt_ccb *bccb;
1073 struct bt_hccb *hccb;
1076 * get a bccb to use.
1078 if ((bccb = btgetccb(bt)) == NULL) {
1082 bt->resource_shortage = TRUE;
1084 xpt_freeze_simq(bt->sim, /*count*/1);
1085 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1093 * So we can find the BCCB when an abort is requested
1096 ccb->ccb_h.ccb_bccb_ptr = bccb;
1097 ccb->ccb_h.ccb_bt_ptr = bt;
1100 * Put all the arguments for the xfer in the bccb
1102 hccb->target_id = ccb->ccb_h.target_id;
1103 hccb->target_lun = ccb->ccb_h.target_lun;
1107 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1108 struct ccb_scsiio *csio;
1109 struct ccb_hdr *ccbh;
1112 ccbh = &csio->ccb_h;
1113 hccb->opcode = INITIATOR_CCB_WRESID;
1114 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1115 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1116 hccb->cmd_len = csio->cdb_len;
1117 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1118 ccb->ccb_h.status = CAM_REQ_INVALID;
1119 btfreeccb(bt, bccb);
1123 hccb->sense_len = csio->sense_len;
1124 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1125 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1126 hccb->tag_enable = TRUE;
1127 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1129 hccb->tag_enable = FALSE;
1132 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1133 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1134 bcopy(csio->cdb_io.cdb_ptr,
1135 hccb->scsi_cdb, hccb->cmd_len);
1137 /* I guess I could map it in... */
1138 ccbh->status = CAM_REQ_INVALID;
1139 btfreeccb(bt, bccb);
1144 bcopy(csio->cdb_io.cdb_bytes,
1145 hccb->scsi_cdb, hccb->cmd_len);
1147 /* If need be, bounce our sense buffer */
1148 if (bt->sense_buffers != NULL) {
1149 hccb->sense_addr = btsensepaddr(bt, bccb);
1151 hccb->sense_addr = vtophys(&csio->sense_data);
1154 * If we have any data to send with this command,
1155 * map it into bus space.
1157 /* Only use S/G if there is a transfer */
1158 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1159 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
1161 * We've been given a pointer
1162 * to a single buffer.
1164 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
1169 error = bus_dmamap_load(
1177 if (error == EINPROGRESS) {
1180 * ordering, freeze the
1182 * until our mapping is
1185 xpt_freeze_simq(bt->sim,
1187 csio->ccb_h.status |=
1192 struct bus_dma_segment seg;
1194 /* Pointer to physical buffer */
1196 (bus_addr_t)csio->data_ptr;
1197 seg.ds_len = csio->dxfer_len;
1198 btexecuteccb(bccb, &seg, 1, 0);
1201 struct bus_dma_segment *segs;
1203 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1204 panic("btaction - Physical "
1208 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1209 panic("btaction - Virtual "
1210 "segment addresses "
1213 /* Just use the segments provided */
1214 segs = (struct bus_dma_segment *)
1216 btexecuteccb(bccb, segs,
1217 csio->sglist_cnt, 0);
1220 btexecuteccb(bccb, NULL, 0, 0);
1223 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1224 /* No data transfer */
1225 hccb->datain = TRUE;
1226 hccb->dataout = TRUE;
1228 hccb->sense_len = 0;
1229 hccb->tag_enable = FALSE;
1231 btexecuteccb(bccb, NULL, 0, 0);
1235 case XPT_EN_LUN: /* Enable LUN as a target */
1236 case XPT_TARGET_IO: /* Execute target I/O request */
1237 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1238 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1239 case XPT_ABORT: /* Abort the specified CCB */
1241 ccb->ccb_h.status = CAM_REQ_INVALID;
1244 case XPT_SET_TRAN_SETTINGS:
1247 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1251 case XPT_GET_TRAN_SETTINGS:
1252 /* Get default/user set transfer settings for the target */
1254 struct ccb_trans_settings *cts;
1258 target_mask = 0x01 << ccb->ccb_h.target_id;
1259 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1261 if ((bt->disc_permitted & target_mask) != 0)
1262 cts->flags |= CCB_TRANS_DISC_ENB;
1263 if ((bt->tags_permitted & target_mask) != 0)
1264 cts->flags |= CCB_TRANS_TAG_ENB;
1265 if ((bt->wide_permitted & target_mask) != 0)
1266 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1268 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1269 if ((bt->ultra_permitted & target_mask) != 0)
1270 cts->sync_period = 12;
1271 else if ((bt->fast_permitted & target_mask) != 0)
1272 cts->sync_period = 25;
1273 else if ((bt->sync_permitted & target_mask) != 0)
1274 cts->sync_period = 50;
1276 cts->sync_period = 0;
1278 if (cts->sync_period != 0)
1279 cts->sync_offset = 15;
1281 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1282 | CCB_TRANS_SYNC_OFFSET_VALID
1283 | CCB_TRANS_BUS_WIDTH_VALID
1284 | CCB_TRANS_DISC_VALID
1285 | CCB_TRANS_TQ_VALID;
1287 btfetchtransinfo(bt, cts);
1290 ccb->ccb_h.status = CAM_REQ_CMP;
1294 case XPT_CALC_GEOMETRY:
1296 struct ccb_calc_geometry *ccg;
1298 u_int32_t secs_per_cylinder;
1301 size_mb = ccg->volume_size
1302 / ((1024L * 1024L) / ccg->block_size);
1304 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1305 if (size_mb >= 2048) {
1307 ccg->secs_per_track = 63;
1310 ccg->secs_per_track = 32;
1314 ccg->secs_per_track = 32;
1316 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1317 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1318 ccb->ccb_h.status = CAM_REQ_CMP;
1322 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1324 btreset(bt, /*hardreset*/TRUE);
1325 ccb->ccb_h.status = CAM_REQ_CMP;
1329 case XPT_TERM_IO: /* Terminate the I/O process */
1331 ccb->ccb_h.status = CAM_REQ_INVALID;
1334 case XPT_PATH_INQ: /* Path routing inquiry */
1336 struct ccb_pathinq *cpi = &ccb->cpi;
1338 cpi->version_num = 1; /* XXX??? */
1339 cpi->hba_inquiry = PI_SDTR_ABLE;
1340 if (bt->tag_capable != 0)
1341 cpi->hba_inquiry |= PI_TAG_ABLE;
1342 if (bt->wide_bus != 0)
1343 cpi->hba_inquiry |= PI_WIDE_16;
1344 cpi->target_sprt = 0;
1346 cpi->hba_eng_cnt = 0;
1347 cpi->max_target = bt->wide_bus ? 15 : 7;
1349 cpi->initiator_id = bt->scsi_id;
1350 cpi->bus_id = cam_sim_bus(sim);
1351 cpi->base_transfer_speed = 3300;
1352 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1353 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1354 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1355 cpi->unit_number = cam_sim_unit(sim);
1356 cpi->ccb_h.status = CAM_REQ_CMP;
1361 ccb->ccb_h.status = CAM_REQ_INVALID;
1368 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1370 struct bt_ccb *bccb;
1372 struct bt_softc *bt;
1375 bccb = (struct bt_ccb *)arg;
1377 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1381 device_printf(bt->dev,
1382 "Unexepected error 0x%x returned from "
1383 "bus_dmamap_load\n", error);
1384 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1385 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1386 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1388 btfreeccb(bt, bccb);
1395 bus_dma_segment_t *end_seg;
1396 bus_dmasync_op_t op;
1398 end_seg = dm_segs + nseg;
1400 /* Copy the segments into our SG list */
1402 while (dm_segs < end_seg) {
1403 sg->len = dm_segs->ds_len;
1404 sg->addr = dm_segs->ds_addr;
1410 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1411 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1412 bccb->hccb.data_addr = bccb->sg_list_phys;
1414 bccb->hccb.data_len = bccb->sg_list->len;
1415 bccb->hccb.data_addr = bccb->sg_list->addr;
1418 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1419 op = BUS_DMASYNC_PREREAD;
1421 op = BUS_DMASYNC_PREWRITE;
1423 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1426 bccb->hccb.opcode = INITIATOR_CCB;
1427 bccb->hccb.data_len = 0;
1428 bccb->hccb.data_addr = 0;
1434 * Last time we need to check if this CCB needs to
1437 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1439 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1440 btfreeccb(bt, bccb);
1446 bccb->flags = BCCB_ACTIVE;
1447 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1448 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1450 ccb->ccb_h.timeout_ch =
1451 timeout(bttimeout, (caddr_t)bccb,
1452 (ccb->ccb_h.timeout * hz) / 1000);
1454 /* Tell the adapter about this command */
1455 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1456 if (bt->cur_outbox->action_code != BMBO_FREE) {
1458 * We should never encounter a busy mailbox.
1459 * If we do, warn the user, and treat it as
1460 * a resource shortage. If the controller is
1461 * hung, one of the pending transactions will
1462 * timeout causing us to start recovery operations.
1464 device_printf(bt->dev,
1465 "Encountered busy mailbox with %d out of %d "
1466 "commands active!!!\n", bt->active_ccbs,
1468 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1470 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1471 btfreeccb(bt, bccb);
1472 bt->resource_shortage = TRUE;
1473 xpt_freeze_simq(bt->sim, /*count*/1);
1474 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1478 bt->cur_outbox->action_code = BMBO_START;
1479 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1487 struct bt_softc *bt;
1490 bt = (struct bt_softc *)arg;
1491 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1493 if ((intstat & CMD_COMPLETE) != 0) {
1494 bt->latched_status = bt_inb(bt, STATUS_REG);
1495 bt->command_cmp = TRUE;
1498 bt_outb(bt, CONTROL_REG, RESET_INTR);
1500 if ((intstat & IMB_LOADED) != 0) {
1501 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1503 btccbptov(bt, bt->cur_inbox->ccb_addr),
1504 bt->cur_inbox->comp_code);
1505 bt->cur_inbox->comp_code = BMBI_FREE;
1510 if ((intstat & SCSI_BUS_RESET) != 0) {
1511 btreset(bt, /*hardreset*/FALSE);
1517 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1520 struct ccb_scsiio *csio;
1523 csio = &bccb->ccb->csio;
1525 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1526 device_printf(bt->dev,
1527 "btdone - Attempt to free non-active BCCB %p\n",
1532 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1533 bus_dmasync_op_t op;
1535 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1536 op = BUS_DMASYNC_POSTREAD;
1538 op = BUS_DMASYNC_POSTWRITE;
1539 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1540 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1543 if (bccb == bt->recovery_bccb) {
1545 * The recovery BCCB does not have a CCB associated
1546 * with it, so short circuit the normal error handling.
1547 * We now traverse our list of pending CCBs and process
1548 * any that were terminated by the recovery CCBs action.
1549 * We also reinstate timeouts for all remaining, pending,
1552 struct cam_path *path;
1553 struct ccb_hdr *ccb_h;
1556 /* Notify all clients that a BDR occured */
1557 error = xpt_create_path(&path, /*periph*/NULL,
1558 cam_sim_path(bt->sim),
1559 bccb->hccb.target_id,
1562 if (error == CAM_REQ_CMP)
1563 xpt_async(AC_SENT_BDR, path, NULL);
1565 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1566 while (ccb_h != NULL) {
1567 struct bt_ccb *pending_bccb;
1569 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1570 if (pending_bccb->hccb.target_id
1571 == bccb->hccb.target_id) {
1572 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1573 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1574 btdone(bt, pending_bccb, BMBI_ERROR);
1577 timeout(bttimeout, (caddr_t)pending_bccb,
1578 (ccb_h->timeout * hz) / 1000);
1579 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1582 device_printf(bt->dev, "No longer in timeout\n");
1586 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1588 switch (comp_code) {
1590 device_printf(bt->dev,
1591 "btdone - CCB completed with free status!\n");
1593 case BMBI_NOT_FOUND:
1594 device_printf(bt->dev,
1595 "btdone - CCB Abort failed to find CCB\n");
1600 printf("bt: ccb %p - error %x occured. "
1601 "btstat = %x, sdstat = %x\n",
1602 (void *)bccb, comp_code, bccb->hccb.btstat,
1605 /* An error occured */
1606 switch(bccb->hccb.btstat) {
1607 case BTSTAT_DATARUN_ERROR:
1608 if (bccb->hccb.data_len == 0) {
1610 * At least firmware 4.22, does this
1611 * for a QUEUE FULL condition.
1613 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1614 } else if (bccb->hccb.data_len < 0) {
1615 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1619 case BTSTAT_NOERROR:
1620 case BTSTAT_LINKED_CMD_COMPLETE:
1621 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1622 case BTSTAT_DATAUNDERUN_ERROR:
1624 csio->scsi_status = bccb->hccb.sdstat;
1625 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1626 switch(csio->scsi_status) {
1627 case SCSI_STATUS_CHECK_COND:
1628 case SCSI_STATUS_CMD_TERMINATED:
1629 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1630 /* Bounce sense back if necessary */
1631 if (bt->sense_buffers != NULL) {
1633 *btsensevaddr(bt, bccb);
1638 case SCSI_STATUS_OK:
1639 csio->ccb_h.status = CAM_REQ_CMP;
1642 csio->resid = bccb->hccb.data_len;
1644 case BTSTAT_SELTIMEOUT:
1645 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1647 case BTSTAT_UNEXPECTED_BUSFREE:
1648 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1650 case BTSTAT_INVALID_PHASE:
1651 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1653 case BTSTAT_INVALID_ACTION_CODE:
1654 panic("%s: Inavlid Action code", bt_name(bt));
1656 case BTSTAT_INVALID_OPCODE:
1657 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1659 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1660 /* We don't even support linked commands... */
1661 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1663 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1664 panic("%s: Invalid CCB or SG list", bt_name(bt));
1666 case BTSTAT_AUTOSENSE_FAILED:
1667 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1669 case BTSTAT_TAGGED_MSG_REJECTED:
1671 struct ccb_trans_settings neg;
1673 xpt_print_path(csio->ccb_h.path);
1674 printf("refuses tagged commands. Performing "
1675 "non-tagged I/O\n");
1677 neg.valid = CCB_TRANS_TQ_VALID;
1678 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1680 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1681 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1682 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1685 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1687 * XXX You would think that this is
1688 * a recoverable error... Hmmm.
1690 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1692 case BTSTAT_HA_SOFTWARE_ERROR:
1693 case BTSTAT_HA_WATCHDOG_ERROR:
1694 case BTSTAT_HARDWARE_FAILURE:
1695 /* Hardware reset ??? Can we recover ??? */
1696 csio->ccb_h.status = CAM_NO_HBA;
1698 case BTSTAT_TARGET_IGNORED_ATN:
1699 case BTSTAT_OTHER_SCSI_BUS_RESET:
1700 case BTSTAT_HA_SCSI_BUS_RESET:
1701 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1703 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1706 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1707 csio->ccb_h.status = CAM_BDR_SENT;
1709 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1711 case BTSTAT_INVALID_RECONNECT:
1712 case BTSTAT_ABORT_QUEUE_GENERATED:
1713 csio->ccb_h.status = CAM_REQ_TERMIO;
1715 case BTSTAT_SCSI_PERROR_DETECTED:
1716 csio->ccb_h.status = CAM_UNCOR_PARITY;
1719 if (csio->ccb_h.status != CAM_REQ_CMP) {
1720 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1721 csio->ccb_h.status |= CAM_DEV_QFRZN;
1723 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1724 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1725 btfreeccb(bt, bccb);
1729 /* All completed without incident */
1730 ccb->ccb_h.status |= CAM_REQ_CMP;
1731 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1732 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1733 btfreeccb(bt, bccb);
1740 btreset(struct bt_softc* bt, int hard_reset)
1742 struct ccb_hdr *ccb_h;
1745 u_int8_t reset_type;
1747 if (hard_reset != 0)
1748 reset_type = HARD_RESET;
1750 reset_type = SOFT_RESET;
1751 bt_outb(bt, CONTROL_REG, reset_type);
1753 /* Wait 5sec. for Diagnostic start */
1754 timeout = 5 * 10000;
1756 status = bt_inb(bt, STATUS_REG);
1757 if ((status & DIAG_ACTIVE) != 0)
1763 printf("%s: btreset - Diagnostic Active failed to "
1764 "assert. status = 0x%x\n", bt_name(bt), status);
1768 /* Wait 10sec. for Diagnostic end */
1769 timeout = 10 * 10000;
1771 status = bt_inb(bt, STATUS_REG);
1772 if ((status & DIAG_ACTIVE) == 0)
1777 panic("%s: btreset - Diagnostic Active failed to drop. "
1778 "status = 0x%x\n", bt_name(bt), status);
1782 /* Wait for the host adapter to become ready or report a failure */
1785 status = bt_inb(bt, STATUS_REG);
1786 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1791 printf("%s: btreset - Host adapter failed to come ready. "
1792 "status = 0x%x\n", bt_name(bt), status);
1796 /* If the diagnostics failed, tell the user */
1797 if ((status & DIAG_FAIL) != 0
1798 || (status & HA_READY) == 0) {
1799 printf("%s: btreset - Adapter failed diagnostics\n",
1802 if ((status & DATAIN_REG_READY) != 0)
1803 printf("%s: btreset - Host Adapter Error code = 0x%x\n",
1804 bt_name(bt), bt_inb(bt, DATAIN_REG));
1808 /* If we've allocated mailboxes, initialize them */
1809 if (bt->init_level > 4)
1812 /* If we've attached to the XPT, tell it about the event */
1813 if (bt->path != NULL)
1814 xpt_async(AC_BUS_RESET, bt->path, NULL);
1817 * Perform completion processing for all outstanding CCBs.
1819 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1820 struct bt_ccb *pending_bccb;
1822 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1823 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1824 btdone(bt, pending_bccb, BMBI_ERROR);
1831 * Send a command to the adapter.
1834 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1835 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1841 u_int reply_buf_size;
1846 /* No data returned to start */
1847 reply_buf_size = reply_len;
1854 bt->command_cmp = 0;
1856 * Wait up to 10 sec. for the adapter to become
1857 * ready to accept commands.
1861 status = bt_inb(bt, STATUS_REG);
1862 if ((status & HA_READY) != 0
1863 && (status & CMD_REG_BUSY) == 0)
1866 * Throw away any pending data which may be
1867 * left over from earlier commands that we
1870 if ((status & DATAIN_REG_READY) != 0)
1871 (void)bt_inb(bt, DATAIN_REG);
1875 printf("%s: bt_cmd: Timeout waiting for adapter ready, "
1876 "status = 0x%x\n", bt_name(bt), status);
1881 * Send the opcode followed by any necessary parameter bytes.
1883 bt_outb(bt, COMMAND_REG, opcode);
1886 * Wait for up to 1sec for each byte of the the
1887 * parameter list sent to be sent.
1890 while (param_len && --timeout) {
1893 status = bt_inb(bt, STATUS_REG);
1894 intstat = bt_inb(bt, INTSTAT_REG);
1897 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1898 == (INTR_PENDING|CMD_COMPLETE)) {
1899 saved_status = status;
1903 if (bt->command_cmp != 0) {
1904 saved_status = bt->latched_status;
1908 if ((status & DATAIN_REG_READY) != 0)
1910 if ((status & CMD_REG_BUSY) == 0) {
1911 bt_outb(bt, COMMAND_REG, *params++);
1917 printf("%s: bt_cmd: Timeout sending parameters, "
1918 "status = 0x%x\n", bt_name(bt), status);
1920 saved_status = status;
1925 * Wait for the command to complete.
1927 while (cmd_complete == 0 && --cmd_timeout) {
1930 status = bt_inb(bt, STATUS_REG);
1931 intstat = bt_inb(bt, INTSTAT_REG);
1933 * It may be that this command was issued with
1934 * controller interrupts disabled. We'll never
1935 * get to our command if an incoming mailbox
1936 * interrupt is pending, so take care of completed
1937 * mailbox commands by calling our interrupt handler.
1939 if ((intstat & (INTR_PENDING|IMB_LOADED))
1940 == (INTR_PENDING|IMB_LOADED))
1944 if (bt->command_cmp != 0) {
1946 * Our interrupt handler saw CMD_COMPLETE
1947 * status before we did.
1950 saved_status = bt->latched_status;
1951 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1952 == (INTR_PENDING|CMD_COMPLETE)) {
1954 * Our poll (in case interrupts are blocked)
1955 * saw the CMD_COMPLETE interrupt.
1958 saved_status = status;
1959 } else if (opcode == BOP_MODIFY_IO_ADDR
1960 && (status & CMD_REG_BUSY) == 0) {
1962 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1963 * but it should update the status register. So, we
1964 * consider this command complete when the CMD_REG_BUSY
1967 saved_status = status;
1969 } else if ((status & DATAIN_REG_READY) != 0) {
1972 data = bt_inb(bt, DATAIN_REG);
1973 if (reply_len < reply_buf_size) {
1974 *reply_data++ = data;
1976 printf("%s: bt_cmd - Discarded reply data byte "
1977 "for opcode 0x%x\n", bt_name(bt),
1981 * Reset timeout to ensure at least a second
1982 * between response bytes.
1984 cmd_timeout = MAX(cmd_timeout, 10000);
1987 } else if ((opcode == BOP_FETCH_LRAM)
1988 && (status & HA_READY) != 0) {
1989 saved_status = status;
1994 if (cmd_timeout == 0) {
1995 printf("%s: bt_cmd: Timeout waiting for command (%x) "
1996 "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
1997 "rlen %d\n", bt_name(bt), opcode,
1998 bt_name(bt), status, intstat, reply_len);
1999 error = (ETIMEDOUT);
2003 * Clear any pending interrupts. Block interrupts so our
2004 * interrupt handler is not re-entered.
2014 * If the command was rejected by the controller, tell the caller.
2016 if ((saved_status & CMD_INVALID) != 0) {
2018 * Some early adapters may not recover properly from
2019 * an invalid command. If it appears that the controller
2020 * has wedged (i.e. status was not cleared by our interrupt
2021 * reset above), perform a soft reset.
2024 printf("%s: Invalid Command 0x%x\n", bt_name(bt),
2027 status = bt_inb(bt, STATUS_REG);
2028 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2029 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2030 || (status & (HA_READY|INIT_REQUIRED))
2031 != (HA_READY|INIT_REQUIRED)) {
2032 btreset(bt, /*hard_reset*/FALSE);
2037 if (param_len > 0) {
2038 /* The controller did not accept the full argument list */
2042 if (reply_len != reply_buf_size) {
2043 /* Too much or too little data received */
2047 /* We were successful */
2052 btinitmboxes(struct bt_softc *bt) {
2053 init_32b_mbox_params_t init_mbox;
2056 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2057 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2058 bt->cur_inbox = bt->in_boxes;
2059 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2060 bt->cur_outbox = bt->out_boxes;
2061 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2063 /* Tell the adapter about them */
2064 init_mbox.num_boxes = bt->num_boxes;
2065 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2066 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2067 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2068 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2069 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2070 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2071 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2074 printf("btinitmboxes: Initialization command failed\n");
2075 else if (bt->strict_rr != 0) {
2077 * If the controller supports
2078 * strict round robin mode,
2084 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1,
2085 /*reply_buf*/NULL, /*reply_len*/0,
2086 DEFAULT_CMD_TIMEOUT);
2089 printf("btinitmboxes: Unable to enable strict RR\n");
2091 } else if (bootverbose) {
2092 printf("%s: Using Strict Round Robin Mailbox Mode\n",
2101 * Update the XPT's idea of the negotiated transfer
2102 * parameters for a particular target.
2105 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings* cts)
2107 setup_data_t setup_info;
2114 targ_syncinfo_t sync_info;
2116 target = cts->ccb_h.target_id;
2117 targ_offset = (target & 0x7);
2118 targ_mask = (0x01 << targ_offset);
2121 * Inquire Setup Information. This command retreives the
2122 * Wide negotiation status for recent adapters as well as
2123 * the sync info for older models.
2125 param = sizeof(setup_info);
2126 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
2127 (u_int8_t*)&setup_info, sizeof(setup_info),
2128 DEFAULT_CMD_TIMEOUT);
2131 printf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
2132 bt_name(bt), error);
2137 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2138 : setup_info.high_syncinfo[targ_offset];
2140 if (sync_info.sync == 0)
2141 cts->sync_offset = 0;
2143 cts->sync_offset = sync_info.offset;
2145 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2146 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2150 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2151 : (setup_info.high_wide_active & targ_mask);
2154 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2155 } else if ((bt->wide_permitted & targ_mask) != 0) {
2156 struct ccb_getdev cgd;
2159 * Prior to rev 5.06L, wide status isn't provided,
2160 * so we "guess" that wide transfers are in effect
2161 * if the user settings allow for wide and the inquiry
2162 * data for the device indicates that it can handle
2165 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2166 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2167 xpt_action((union ccb *)&cgd);
2168 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2169 && (cgd.inq_data.flags & SID_WBus16) != 0)
2170 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2173 if (bt->firmware_ver[0] >= '3') {
2175 * For adapters that can do fast or ultra speeds,
2176 * use the more exact Target Sync Information command.
2178 target_sync_info_data_t sync_info;
2180 param = sizeof(sync_info);
2181 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1,
2182 (u_int8_t*)&sync_info, sizeof(sync_info),
2183 DEFAULT_CMD_TIMEOUT);
2186 printf("%s: btfetchtransinfo - Inquire Sync "
2187 "Info Failed 0x%x\n", bt_name(bt), error);
2191 sync_period = sync_info.sync_rate[target] * 100;
2193 sync_period = 2000 + (500 * sync_info.period);
2196 /* Convert ns value to standard SCSI sync rate */
2197 if (cts->sync_offset != 0)
2198 cts->sync_period = scsi_calc_syncparam(sync_period);
2200 cts->sync_period = 0;
2202 cts->valid = CCB_TRANS_SYNC_RATE_VALID
2203 | CCB_TRANS_SYNC_OFFSET_VALID
2204 | CCB_TRANS_BUS_WIDTH_VALID;
2205 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2209 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2211 struct bt_softc* bt;
2213 bt = (struct bt_softc*)arg;
2214 bt->mailbox_physbase = segs->ds_addr;
2218 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2220 struct bt_softc* bt;
2222 bt = (struct bt_softc*)arg;
2223 bt->bt_ccb_physbase = segs->ds_addr;
2227 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2230 struct bt_softc* bt;
2232 bt = (struct bt_softc*)arg;
2233 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2237 btpoll(struct cam_sim *sim)
2239 bt_intr(cam_sim_softc(sim));
2243 bttimeout(void *arg)
2245 struct bt_ccb *bccb;
2247 struct bt_softc *bt;
2250 bccb = (struct bt_ccb *)arg;
2252 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2253 xpt_print_path(ccb->ccb_h.path);
2254 printf("CCB %p - timed out\n", (void *)bccb);
2258 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2259 xpt_print_path(ccb->ccb_h.path);
2260 printf("CCB %p - timed out CCB already completed\n",
2267 * In order to simplify the recovery process, we ask the XPT
2268 * layer to halt the queue of new transactions and we traverse
2269 * the list of pending CCBs and remove their timeouts. This
2270 * means that the driver attempts to clear only one error
2271 * condition at a time. In general, timeouts that occur
2272 * close together are related anyway, so there is no benefit
2273 * in attempting to handle errors in parrallel. Timeouts will
2274 * be reinstated when the recovery process ends.
2276 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2277 struct ccb_hdr *ccb_h;
2279 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2280 xpt_freeze_simq(bt->sim, /*count*/1);
2281 bccb->flags |= BCCB_RELEASE_SIMQ;
2284 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2285 while (ccb_h != NULL) {
2286 struct bt_ccb *pending_bccb;
2288 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2289 untimeout(bttimeout, pending_bccb, ccb_h->timeout_ch);
2290 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2294 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2295 || bt->cur_outbox->action_code != BMBO_FREE
2296 || ((bccb->hccb.tag_enable == TRUE)
2297 && (bt->firmware_ver[0] < '5'))) {
2299 * Try a full host adapter/SCSI bus reset.
2300 * We do this only if we have already attempted
2301 * to clear the condition with a BDR, or we cannot
2302 * attempt a BDR for lack of mailbox resources
2303 * or because of faulty firmware. It turns out
2304 * that firmware versions prior to 5.xx treat BDRs
2305 * as untagged commands that cannot be sent until
2306 * all outstanding tagged commands have been processed.
2307 * This makes it somewhat difficult to use a BDR to
2308 * clear up a problem with an uncompleted tagged command.
2310 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2311 btreset(bt, /*hardreset*/TRUE);
2312 printf("%s: No longer in timeout\n", bt_name(bt));
2315 * Send a Bus Device Reset message:
2316 * The target that is holding up the bus may not
2317 * be the same as the one that triggered this timeout
2318 * (different commands have different timeout lengths),
2319 * but we have no way of determining this from our
2320 * timeout handler. Our strategy here is to queue a
2321 * BDR message to the target of the timed out command.
2322 * If this fails, we'll get another timeout 2 seconds
2323 * later which will attempt a bus reset.
2325 bccb->flags |= BCCB_DEVICE_RESET;
2326 ccb->ccb_h.timeout_ch =
2327 timeout(bttimeout, (caddr_t)bccb, 2 * hz);
2329 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2331 /* No Data Transfer */
2332 bt->recovery_bccb->hccb.datain = TRUE;
2333 bt->recovery_bccb->hccb.dataout = TRUE;
2334 bt->recovery_bccb->hccb.btstat = 0;
2335 bt->recovery_bccb->hccb.sdstat = 0;
2336 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2338 /* Tell the adapter about this command */
2339 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2340 bt->cur_outbox->action_code = BMBO_START;
2341 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);