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_pci.c BT-946, BT-948, BT-956, BT-958 cards
8 * Copyright (c) 1998, 1999 Justin T. Gibbs.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification, immediately at the beginning of the file.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Special thanks to Leonard N. Zubkoff for writing such a complete and
38 * well documented Mylex/BusLogic MultiMaster driver for Linux. Support
39 * in this driver for the wide range of MultiMaster controllers and
40 * firmware revisions, with their otherwise undocumented quirks, would not
41 * have been possible without his efforts.
44 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
55 #include <machine/bus.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_sim.h>
61 #include <cam/cam_xpt_sim.h>
62 #include <cam/cam_debug.h>
64 #include <cam/scsi/scsi_message.h>
69 #include <dev/buslogic/btreg.h>
71 /* MailBox Management functions */
72 static __inline void btnextinbox(struct bt_softc *bt);
73 static __inline void btnextoutbox(struct bt_softc *bt);
76 btnextinbox(struct bt_softc *bt)
78 if (bt->cur_inbox == bt->last_inbox)
79 bt->cur_inbox = bt->in_boxes;
85 btnextoutbox(struct bt_softc *bt)
87 if (bt->cur_outbox == bt->last_outbox)
88 bt->cur_outbox = bt->out_boxes;
93 /* CCB Mangement functions */
94 static __inline u_int32_t btccbvtop(struct bt_softc *bt,
96 static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
98 static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
100 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
101 struct bt_ccb *bccb);
103 static __inline u_int32_t
104 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
106 return (bt->bt_ccb_physbase
107 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
110 static __inline struct bt_ccb *
111 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
113 return (bt->bt_ccb_array +
114 ((struct bt_ccb*)(uintptr_t)ccb_addr - (struct bt_ccb*)(uintptr_t)bt->bt_ccb_physbase));
117 static __inline u_int32_t
118 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
122 index = (u_int)(bccb - bt->bt_ccb_array);
123 return (bt->sense_buffers_physbase
124 + (index * sizeof(struct scsi_sense_data)));
127 static __inline struct scsi_sense_data *
128 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
132 index = (u_int)(bccb - bt->bt_ccb_array);
133 return (bt->sense_buffers + index);
136 static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
137 static __inline void btfreeccb(struct bt_softc *bt,
138 struct bt_ccb *bccb);
139 static void btallocccbs(struct bt_softc *bt);
140 static bus_dmamap_callback_t btexecuteccb;
141 static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
142 bt_mbi_comp_code_t comp_code);
143 static void bt_intr_locked(struct bt_softc *bt);
145 /* Host adapter command functions */
146 static int btreset(struct bt_softc* bt, int hard_reset);
148 /* Initialization functions */
149 static int btinitmboxes(struct bt_softc *bt);
150 static bus_dmamap_callback_t btmapmboxes;
151 static bus_dmamap_callback_t btmapccbs;
152 static bus_dmamap_callback_t btmapsgs;
154 /* Transfer Negotiation Functions */
155 static void btfetchtransinfo(struct bt_softc *bt,
156 struct ccb_trans_settings *cts);
158 /* CAM SIM entry points */
159 #define ccb_bccb_ptr spriv_ptr0
160 #define ccb_bt_ptr spriv_ptr1
161 static void btaction(struct cam_sim *sim, union ccb *ccb);
162 static void btpoll(struct cam_sim *sim);
164 /* Our timeout handler */
165 static void bttimeout(void *arg);
169 * Do our own re-probe protection until a configuration
170 * manager can do it for us. This ensures that we don't
171 * reprobe a card already found by the PCI probes.
173 struct bt_isa_port bt_isa_ports[] =
184 * I/O ports listed in the order enumerated by the
185 * card for certain op codes.
187 u_int16_t bt_board_ports[] =
197 /* Exported functions */
199 bt_init_softc(device_t dev, struct resource *port,
200 struct resource *irq, struct resource *drq)
202 struct bt_softc *bt = device_get_softc(dev);
204 SLIST_INIT(&bt->free_bt_ccbs);
205 LIST_INIT(&bt->pending_ccbs);
206 SLIST_INIT(&bt->sg_maps);
211 mtx_init(&bt->lock, "bt", NULL, MTX_DEF);
215 bt_free_softc(device_t dev)
217 struct bt_softc *bt = device_get_softc(dev);
219 switch (bt->init_level) {
222 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
224 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
227 bus_dma_tag_destroy(bt->sense_dmat);
230 struct sg_map_node *sg_map;
232 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
233 SLIST_REMOVE_HEAD(&bt->sg_maps, links);
234 bus_dmamap_unload(bt->sg_dmat,
236 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
238 free(sg_map, M_DEVBUF);
240 bus_dma_tag_destroy(bt->sg_dmat);
243 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
246 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
250 bus_dma_tag_destroy(bt->ccb_dmat);
253 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
256 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
260 bus_dma_tag_destroy(bt->buffer_dmat);
263 bus_dma_tag_destroy(bt->mailbox_dmat);
268 mtx_destroy(&bt->lock);
272 bt_port_probe(device_t dev, struct bt_probe_info *info)
274 struct bt_softc *bt = device_get_softc(dev);
275 config_data_t config_data;
278 /* See if there is really a card present */
279 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
283 * Determine our IRQ, and DMA settings and
284 * export them to the configuration system.
287 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
288 (u_int8_t*)&config_data, sizeof(config_data),
289 DEFAULT_CMD_TIMEOUT);
290 mtx_unlock(&bt->lock);
292 printf("bt_port_probe: Could not determine IRQ or DMA "
293 "settings for adapter.\n");
297 if (bt->model[0] == '5') {
298 /* DMA settings only make sense for ISA cards */
299 switch (config_data.dma_chan) {
310 printf("bt_port_probe: Invalid DMA setting "
311 "detected for adapter.\n");
317 switch (config_data.irq) {
324 info->irq = ffs(config_data.irq) + 8;
327 printf("bt_port_probe: Invalid IRQ setting %x"
328 "detected for adapter.\n", config_data.irq);
335 * Probe the adapter and verify that the card is a BusLogic.
338 bt_probe(device_t dev)
340 struct bt_softc *bt = device_get_softc(dev);
341 esetup_info_data_t esetup_info;
349 * See if the three I/O ports look reasonable.
350 * Touch the minimal number of registers in the
353 status = bt_inb(bt, STATUS_REG);
355 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
356 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
358 device_printf(dev, "Failed Status Reg Test - %x\n",
363 intstat = bt_inb(bt, INTSTAT_REG);
364 if ((intstat & INTSTAT_REG_RSVD) != 0) {
365 device_printf(dev, "Failed Intstat Reg Test\n");
369 geometry = bt_inb(bt, GEOMETRY_REG);
370 if (geometry == 0xFF) {
372 device_printf(dev, "Failed Geometry Reg Test\n");
377 * Looking good so far. Final test is to reset the
378 * adapter and attempt to fetch the extended setup
379 * information. This should filter out all 1542 cards.
382 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
383 mtx_unlock(&bt->lock);
385 device_printf(dev, "Failed Reset\n");
389 param = sizeof(esetup_info);
390 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1,
391 (u_int8_t*)&esetup_info, sizeof(esetup_info),
392 DEFAULT_CMD_TIMEOUT);
393 mtx_unlock(&bt->lock);
402 * Pull the boards setup information and record it in our softc.
405 bt_fetch_adapter_info(device_t dev)
407 struct bt_softc *bt = device_get_softc(dev);
408 board_id_data_t board_id;
409 esetup_info_data_t esetup_info;
410 config_data_t config_data;
412 u_int8_t length_param;
414 /* First record the firmware version */
416 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
417 (u_int8_t*)&board_id, sizeof(board_id),
418 DEFAULT_CMD_TIMEOUT);
420 mtx_unlock(&bt->lock);
421 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
424 bt->firmware_ver[0] = board_id.firmware_rev_major;
425 bt->firmware_ver[1] = '.';
426 bt->firmware_ver[2] = board_id.firmware_rev_minor;
427 bt->firmware_ver[3] = '\0';
430 * Depending on the firmware major and minor version,
431 * we may be able to fetch additional minor version info.
433 if (bt->firmware_ver[0] > '0') {
435 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
436 (u_int8_t*)&bt->firmware_ver[3], 1,
437 DEFAULT_CMD_TIMEOUT);
439 mtx_unlock(&bt->lock);
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);
456 mtx_unlock(&bt->lock);
458 "bt_fetch_adapter_info - Failed Get "
459 "Firmware 4th Digit\n");
462 if (bt->firmware_ver[4] == ' ')
463 bt->firmware_ver[4] = '\0';
464 bt->firmware_ver[5] = '\0';
468 * Some boards do not handle the "recently documented"
469 * Inquire Board Model Number command correctly or do not give
470 * exact information. Use the Firmware and Extended Setup
471 * information in these cases to come up with the right answer.
472 * The major firmware revision number indicates:
474 * 5.xx BusLogic "W" Series Host Adapters:
476 * 4.xx BusLogic "C" Series Host Adapters:
477 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
478 * 3.xx BusLogic "S" Series Host Adapters:
479 * BT-747S/747D/757S/757D/445S/545S/542D
480 * BT-542B/742A (revision H)
481 * 2.xx BusLogic "A" Series Host Adapters:
482 * BT-542B/742A (revision G and below)
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);
489 mtx_unlock(&bt->lock);
493 bt->bios_addr = esetup_info.bios_addr << 12;
495 bt->mailbox_addrlimit = BUS_SPACE_MAXADDR;
496 if (esetup_info.bus_type == 'A'
497 && bt->firmware_ver[0] == '2') {
498 snprintf(bt->model, sizeof(bt->model), "542B");
500 ha_model_data_t model_data;
503 length_param = sizeof(model_data);
504 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
505 (u_int8_t*)&model_data, sizeof(model_data),
506 DEFAULT_CMD_TIMEOUT);
508 mtx_unlock(&bt->lock);
510 "bt_fetch_adapter_info - Failed Inquire "
514 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
515 bt->model[i] = model_data.ascii_model[i];
516 if (bt->model[i] == ' ')
522 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
524 /* SG element limits */
525 bt->max_sg = esetup_info.max_sg;
527 /* Set feature flags */
528 bt->wide_bus = esetup_info.wide_bus;
529 bt->diff_bus = esetup_info.diff_bus;
530 bt->ultra_scsi = esetup_info.ultra_scsi;
532 if ((bt->firmware_ver[0] == '5')
533 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
534 bt->extended_lun = TRUE;
536 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
539 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
542 * Determine max CCB count and whether tagged queuing is
543 * available based on controller type. Tagged queuing
544 * only works on 'W' series adapters, 'C' series adapters
545 * with firmware of rev 4.42 and higher, and 'S' series
546 * adapters with firmware of rev 3.35 and higher. The
547 * maximum CCB counts are as follows:
549 * 192 BT-948/958/958D
550 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
552 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
554 if (bt->firmware_ver[0] == '5') {
556 bt->tag_capable = TRUE;
557 } else if (bt->firmware_ver[0] == '4') {
558 if (bt->model[0] == '5')
562 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
565 if (bt->firmware_ver[0] == '3'
566 && (strcmp(bt->firmware_ver, "3.35") >= 0))
567 bt->tag_capable = TRUE;
569 bt->tag_capable = FALSE;
572 if (bt->tag_capable != FALSE)
573 bt->tags_permitted = ALL_TARGETS;
575 /* Determine Sync/Wide/Disc settings */
576 if (bt->firmware_ver[0] >= '4') {
577 auto_scsi_data_t auto_scsi_data;
578 fetch_lram_params_t fetch_lram_params;
582 * These settings are stored in the
583 * AutoSCSI data in LRAM of 'W' and 'C'
586 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
587 fetch_lram_params.response_len = sizeof(auto_scsi_data);
588 error = bt_cmd(bt, BOP_FETCH_LRAM,
589 (u_int8_t*)&fetch_lram_params,
590 sizeof(fetch_lram_params),
591 (u_int8_t*)&auto_scsi_data,
592 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
595 mtx_unlock(&bt->lock);
597 "bt_fetch_adapter_info - Failed "
598 "Get Auto SCSI Info\n");
602 bt->disc_permitted = auto_scsi_data.low_disc_permitted
603 | (auto_scsi_data.high_disc_permitted << 8);
604 bt->sync_permitted = auto_scsi_data.low_sync_permitted
605 | (auto_scsi_data.high_sync_permitted << 8);
606 bt->fast_permitted = auto_scsi_data.low_fast_permitted
607 | (auto_scsi_data.high_fast_permitted << 8);
608 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
609 | (auto_scsi_data.high_ultra_permitted << 8);
610 bt->wide_permitted = auto_scsi_data.low_wide_permitted
611 | (auto_scsi_data.high_wide_permitted << 8);
613 if (bt->ultra_scsi == FALSE)
614 bt->ultra_permitted = 0;
616 if (bt->wide_bus == FALSE)
617 bt->wide_permitted = 0;
620 * 'S' and 'A' series have this information in the setup
621 * information structure.
623 setup_data_t setup_info;
625 length_param = sizeof(setup_info);
626 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
627 /*paramlen*/1, (u_int8_t*)&setup_info,
628 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
631 mtx_unlock(&bt->lock);
633 "bt_fetch_adapter_info - Failed "
638 if (setup_info.initiate_sync != 0) {
639 bt->sync_permitted = ALL_TARGETS;
641 if (bt->model[0] == '7') {
642 if (esetup_info.sync_neg10MB != 0)
643 bt->fast_permitted = ALL_TARGETS;
644 if (strcmp(bt->model, "757") == 0)
645 bt->wide_permitted = ALL_TARGETS;
648 bt->disc_permitted = ALL_TARGETS;
651 /* We need as many mailboxes as we can have ccbs */
652 bt->num_boxes = bt->max_ccbs;
654 /* Determine our SCSI ID */
656 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
657 (u_int8_t*)&config_data, sizeof(config_data),
658 DEFAULT_CMD_TIMEOUT);
659 mtx_unlock(&bt->lock);
662 "bt_fetch_adapter_info - Failed Get Config\n");
665 bt->scsi_id = config_data.scsi_id;
671 * Start the board, ready for normal operation
674 bt_init(device_t dev)
676 struct bt_softc *bt = device_get_softc(dev);
678 /* Announce the Adapter */
679 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
681 if (bt->ultra_scsi != 0)
684 if (bt->wide_bus != 0)
689 if (bt->diff_bus != 0)
692 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
696 * Create our DMA tags. These tags define the kinds of device
697 * accessible memory allocations and memory mappings we will
698 * need to perform during normal operation.
700 * Unless we need to further restrict the allocation, we rely
701 * on the restrictions of the parent dmat, hence the common
702 * use of MAXADDR and MAXSIZE.
705 /* DMA tag for mapping buffers into device visible space. */
706 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
709 /* lowaddr */ BUS_SPACE_MAXADDR,
710 /* highaddr */ BUS_SPACE_MAXADDR,
712 /* filterarg */ NULL,
713 /* maxsize */ DFLTPHYS,
714 /* nsegments */ BT_NSEG,
715 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
716 /* flags */ BUS_DMA_ALLOCNOW,
717 /* lockfunc */ busdma_lock_mutex,
718 /* lockarg */ &bt->lock,
719 &bt->buffer_dmat) != 0) {
724 /* DMA tag for our mailboxes */
725 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
728 /* lowaddr */ bt->mailbox_addrlimit,
729 /* highaddr */ BUS_SPACE_MAXADDR,
731 /* filterarg */ NULL,
732 /* maxsize */ bt->num_boxes *
733 (sizeof(bt_mbox_in_t) +
734 sizeof(bt_mbox_out_t)),
736 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
740 &bt->mailbox_dmat) != 0) {
746 /* Allocation for our mailboxes */
747 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
748 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
754 /* And permanently map them */
755 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
757 bt->num_boxes * (sizeof(bt_mbox_in_t)
758 + sizeof(bt_mbox_out_t)),
759 btmapmboxes, bt, /*flags*/0);
763 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
767 mtx_unlock(&bt->lock);
769 /* DMA tag for our ccb structures */
770 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
773 /* lowaddr */ BUS_SPACE_MAXADDR,
774 /* highaddr */ BUS_SPACE_MAXADDR,
776 /* filterarg */ NULL,
777 /* maxsize */ bt->max_ccbs *
778 sizeof(struct bt_ccb),
780 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
784 &bt->ccb_dmat) != 0) {
790 /* Allocation for our ccbs */
791 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
792 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
798 /* And permanently map them */
799 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
801 bt->max_ccbs * sizeof(struct bt_ccb),
802 btmapccbs, bt, /*flags*/0);
806 /* DMA tag for our S/G structures. We allocate in page sized chunks */
807 if (bus_dma_tag_create( /* parent */ bt->parent_dmat,
810 /* lowaddr */ BUS_SPACE_MAXADDR,
811 /* highaddr */ BUS_SPACE_MAXADDR,
813 /* filterarg */ NULL,
814 /* maxsize */ PAGE_SIZE,
816 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
820 &bt->sg_dmat) != 0) {
826 /* Perform initial CCB allocation */
827 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
830 if (bt->num_ccbs == 0) {
832 "bt_init - Unable to allocate initial ccbs\n");
837 * Note that we are going and return (to attach)
847 bt_attach(device_t dev)
849 struct bt_softc *bt = device_get_softc(dev);
850 int tagged_dev_openings;
851 struct cam_devq *devq;
855 * We reserve 1 ccb for error recovery, so don't
856 * tell the XPT about it.
858 if (bt->tag_capable != 0)
859 tagged_dev_openings = bt->max_ccbs - 1;
861 tagged_dev_openings = 0;
864 * Create the device queue for our SIM.
866 devq = cam_simq_alloc(bt->max_ccbs - 1);
871 * Construct our SIM entry
873 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt,
874 device_get_unit(bt->dev), &bt->lock, 2, tagged_dev_openings, devq);
875 if (bt->sim == NULL) {
881 if (xpt_bus_register(bt->sim, dev, 0) != CAM_SUCCESS) {
882 cam_sim_free(bt->sim, /*free_devq*/TRUE);
883 mtx_unlock(&bt->lock);
887 if (xpt_create_path(&bt->path, /*periph*/NULL,
888 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
889 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
890 xpt_bus_deregister(cam_sim_path(bt->sim));
891 cam_sim_free(bt->sim, /*free_devq*/TRUE);
892 mtx_unlock(&bt->lock);
895 mtx_unlock(&bt->lock);
900 error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM | INTR_ENTROPY |
901 INTR_MPSAFE, NULL, bt_intr, bt, &bt->ih);
903 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
911 bt_check_probed_iop(u_int ioport)
915 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
916 if (bt_isa_ports[i].addr == ioport) {
917 if (bt_isa_ports[i].probed != 0)
928 bt_mark_probed_bio(isa_compat_io_t port)
930 if (port < BIO_DISABLED)
931 bt_mark_probed_iop(bt_board_ports[port]);
935 bt_mark_probed_iop(u_int ioport)
939 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
940 if (ioport == bt_isa_ports[i].addr) {
941 bt_isa_ports[i].probed = 1;
948 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
953 for (i = 0;i < BT_NUM_ISAPORTS; i++)
954 if (ioport <= bt_isa_ports[i].addr)
956 if ((i >= BT_NUM_ISAPORTS)
957 || (ioport != bt_isa_ports[i].addr)) {
959 "bt_find_probe_range: Invalid baseport of 0x%x specified.\n"
960 "bt_find_probe_range: Nearest valid baseport is 0x%x.\n"
961 "bt_find_probe_range: Failing probe.\n",
963 (i < BT_NUM_ISAPORTS)
964 ? bt_isa_ports[i].addr
965 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
966 *port_index = *max_port_index = -1;
969 *port_index = *max_port_index = bt_isa_ports[i].bio;
972 *max_port_index = BT_NUM_ISAPORTS - 1;
977 bt_iop_from_bio(isa_compat_io_t bio_index)
979 if (bio_index < BT_NUM_ISAPORTS)
980 return (bt_board_ports[bio_index]);
986 btallocccbs(struct bt_softc *bt)
988 struct bt_ccb *next_ccb;
989 struct sg_map_node *sg_map;
995 if (bt->num_ccbs >= bt->max_ccbs)
996 /* Can't allocate any more */
999 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
1001 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
1006 /* Allocate S/G space for the next batch of CCBS */
1007 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
1008 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
1009 free(sg_map, M_DEVBUF);
1013 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
1015 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
1016 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
1018 segs = sg_map->sg_vaddr;
1019 physaddr = sg_map->sg_physaddr;
1021 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
1022 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
1025 next_ccb->sg_list = segs;
1026 next_ccb->sg_list_phys = physaddr;
1027 next_ccb->flags = BCCB_FREE;
1028 callout_init_mtx(&next_ccb->timer, &bt->lock, 0);
1029 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
1033 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
1035 physaddr += (BT_NSEG * sizeof(bt_sg_t));
1040 /* Reserve a CCB for error recovery */
1041 if (bt->recovery_bccb == NULL) {
1042 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1043 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1046 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
1050 device_printf(bt->dev, "Can't malloc BCCBs\n");
1053 static __inline void
1054 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1058 mtx_assert(&bt->lock, MA_OWNED);
1059 if ((bccb->flags & BCCB_ACTIVE) != 0)
1060 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1061 if (bt->resource_shortage != 0
1062 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1063 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1064 bt->resource_shortage = FALSE;
1066 bccb->flags = BCCB_FREE;
1067 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1071 static __inline struct bt_ccb*
1072 btgetccb(struct bt_softc *bt)
1074 struct bt_ccb* bccb;
1077 mtx_assert(&bt->lock, MA_OWNED);
1078 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1079 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1083 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1085 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1094 btaction(struct cam_sim *sim, union ccb *ccb)
1096 struct bt_softc *bt;
1098 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1100 bt = (struct bt_softc *)cam_sim_softc(sim);
1101 mtx_assert(&bt->lock, MA_OWNED);
1103 switch (ccb->ccb_h.func_code) {
1104 /* Common cases first */
1105 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1106 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1108 struct bt_ccb *bccb;
1109 struct bt_hccb *hccb;
1112 * get a bccb to use.
1114 if ((bccb = btgetccb(bt)) == NULL) {
1116 bt->resource_shortage = TRUE;
1117 xpt_freeze_simq(bt->sim, /*count*/1);
1118 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1126 * So we can find the BCCB when an abort is requested
1129 ccb->ccb_h.ccb_bccb_ptr = bccb;
1130 ccb->ccb_h.ccb_bt_ptr = bt;
1133 * Put all the arguments for the xfer in the bccb
1135 hccb->target_id = ccb->ccb_h.target_id;
1136 hccb->target_lun = ccb->ccb_h.target_lun;
1140 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1141 struct ccb_scsiio *csio;
1142 struct ccb_hdr *ccbh;
1146 ccbh = &csio->ccb_h;
1147 hccb->opcode = INITIATOR_CCB_WRESID;
1148 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1149 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1150 hccb->cmd_len = csio->cdb_len;
1151 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1152 ccb->ccb_h.status = CAM_REQ_INVALID;
1153 btfreeccb(bt, bccb);
1157 hccb->sense_len = csio->sense_len;
1158 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1159 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1160 hccb->tag_enable = TRUE;
1161 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1163 hccb->tag_enable = FALSE;
1166 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1167 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1168 bcopy(csio->cdb_io.cdb_ptr,
1169 hccb->scsi_cdb, hccb->cmd_len);
1171 /* I guess I could map it in... */
1172 ccbh->status = CAM_REQ_INVALID;
1173 btfreeccb(bt, bccb);
1178 bcopy(csio->cdb_io.cdb_bytes,
1179 hccb->scsi_cdb, hccb->cmd_len);
1181 /* If need be, bounce our sense buffer */
1182 if (bt->sense_buffers != NULL) {
1183 hccb->sense_addr = btsensepaddr(bt, bccb);
1185 hccb->sense_addr = vtophys(&csio->sense_data);
1188 * If we have any data to send with this command,
1189 * map it into bus space.
1191 error = bus_dmamap_load_ccb(
1198 if (error == EINPROGRESS) {
1200 * So as to maintain ordering, freeze the
1201 * controller queue until our mapping is
1204 xpt_freeze_simq(bt->sim, 1);
1205 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
1208 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1209 /* No data transfer */
1210 hccb->datain = TRUE;
1211 hccb->dataout = TRUE;
1213 hccb->sense_len = 0;
1214 hccb->tag_enable = FALSE;
1216 btexecuteccb(bccb, NULL, 0, 0);
1220 case XPT_ABORT: /* Abort the specified CCB */
1222 ccb->ccb_h.status = CAM_REQ_INVALID;
1225 case XPT_SET_TRAN_SETTINGS:
1228 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1232 case XPT_GET_TRAN_SETTINGS:
1233 /* Get default/user set transfer settings for the target */
1235 struct ccb_trans_settings *cts;
1239 target_mask = 0x01 << ccb->ccb_h.target_id;
1240 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1241 struct ccb_trans_settings_scsi *scsi =
1242 &cts->proto_specific.scsi;
1243 struct ccb_trans_settings_spi *spi =
1244 &cts->xport_specific.spi;
1245 cts->protocol = PROTO_SCSI;
1246 cts->protocol_version = SCSI_REV_2;
1247 cts->transport = XPORT_SPI;
1248 cts->transport_version = 2;
1250 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1251 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1253 if ((bt->disc_permitted & target_mask) != 0)
1254 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1255 if ((bt->tags_permitted & target_mask) != 0)
1256 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1258 if ((bt->ultra_permitted & target_mask) != 0)
1259 spi->sync_period = 12;
1260 else if ((bt->fast_permitted & target_mask) != 0)
1261 spi->sync_period = 25;
1262 else if ((bt->sync_permitted & target_mask) != 0)
1263 spi->sync_period = 50;
1265 spi->sync_period = 0;
1267 if (spi->sync_period != 0)
1268 spi->sync_offset = 15;
1270 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1271 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1273 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1274 if ((bt->wide_permitted & target_mask) != 0)
1275 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1277 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1279 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1280 scsi->valid = CTS_SCSI_VALID_TQ;
1281 spi->valid |= CTS_SPI_VALID_DISC;
1285 btfetchtransinfo(bt, cts);
1288 ccb->ccb_h.status = CAM_REQ_CMP;
1292 case XPT_CALC_GEOMETRY:
1294 struct ccb_calc_geometry *ccg;
1296 u_int32_t secs_per_cylinder;
1299 size_mb = ccg->volume_size
1300 / ((1024L * 1024L) / ccg->block_size);
1302 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1303 if (size_mb >= 2048) {
1305 ccg->secs_per_track = 63;
1308 ccg->secs_per_track = 32;
1312 ccg->secs_per_track = 32;
1314 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1315 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1316 ccb->ccb_h.status = CAM_REQ_CMP;
1320 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1322 btreset(bt, /*hardreset*/TRUE);
1323 ccb->ccb_h.status = CAM_REQ_CMP;
1327 case XPT_TERM_IO: /* Terminate the I/O process */
1329 ccb->ccb_h.status = CAM_REQ_INVALID;
1332 case XPT_PATH_INQ: /* Path routing inquiry */
1334 struct ccb_pathinq *cpi = &ccb->cpi;
1336 cpi->version_num = 1; /* XXX??? */
1337 cpi->hba_inquiry = PI_SDTR_ABLE;
1338 if (bt->tag_capable != 0)
1339 cpi->hba_inquiry |= PI_TAG_ABLE;
1340 if (bt->wide_bus != 0)
1341 cpi->hba_inquiry |= PI_WIDE_16;
1342 cpi->target_sprt = 0;
1344 cpi->hba_eng_cnt = 0;
1345 cpi->max_target = bt->wide_bus ? 15 : 7;
1347 cpi->initiator_id = bt->scsi_id;
1348 cpi->bus_id = cam_sim_bus(sim);
1349 cpi->base_transfer_speed = 3300;
1350 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1351 strlcpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1352 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1353 cpi->unit_number = cam_sim_unit(sim);
1354 cpi->ccb_h.status = CAM_REQ_CMP;
1355 cpi->transport = XPORT_SPI;
1356 cpi->transport_version = 2;
1357 cpi->protocol = PROTO_SCSI;
1358 cpi->protocol_version = SCSI_REV_2;
1363 ccb->ccb_h.status = CAM_REQ_INVALID;
1370 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1372 struct bt_ccb *bccb;
1374 struct bt_softc *bt;
1376 bccb = (struct bt_ccb *)arg;
1378 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1382 device_printf(bt->dev,
1383 "Unexepected error 0x%x returned from "
1384 "bus_dmamap_load\n", error);
1385 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1386 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1387 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1389 btfreeccb(bt, bccb);
1396 bus_dma_segment_t *end_seg;
1397 bus_dmasync_op_t op;
1399 end_seg = dm_segs + nseg;
1401 /* Copy the segments into our SG list */
1403 while (dm_segs < end_seg) {
1404 sg->len = dm_segs->ds_len;
1405 sg->addr = dm_segs->ds_addr;
1411 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1412 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1413 bccb->hccb.data_addr = bccb->sg_list_phys;
1415 bccb->hccb.data_len = bccb->sg_list->len;
1416 bccb->hccb.data_addr = bccb->sg_list->addr;
1419 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1420 op = BUS_DMASYNC_PREREAD;
1422 op = BUS_DMASYNC_PREWRITE;
1424 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1427 bccb->hccb.opcode = INITIATOR_CCB;
1428 bccb->hccb.data_len = 0;
1429 bccb->hccb.data_addr = 0;
1433 * Last time we need to check if this CCB needs to
1436 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1438 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1439 btfreeccb(bt, bccb);
1444 bccb->flags = BCCB_ACTIVE;
1445 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1446 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1448 callout_reset_sbt(&bccb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1449 bttimeout, bccb, 0);
1451 /* Tell the adapter about this command */
1452 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1453 if (bt->cur_outbox->action_code != BMBO_FREE) {
1455 * We should never encounter a busy mailbox.
1456 * If we do, warn the user, and treat it as
1457 * a resource shortage. If the controller is
1458 * hung, one of the pending transactions will
1459 * timeout causing us to start recovery operations.
1461 device_printf(bt->dev,
1462 "Encountered busy mailbox with %d out of %d "
1463 "commands active!!!\n", bt->active_ccbs,
1465 callout_stop(&bccb->timer);
1467 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1468 btfreeccb(bt, bccb);
1469 bt->resource_shortage = TRUE;
1470 xpt_freeze_simq(bt->sim, /*count*/1);
1471 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1475 bt->cur_outbox->action_code = BMBO_START;
1476 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1483 struct bt_softc *bt;
1486 mtx_lock(&bt->lock);
1488 mtx_unlock(&bt->lock);
1492 bt_intr_locked(struct bt_softc *bt)
1496 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1498 if ((intstat & CMD_COMPLETE) != 0) {
1499 bt->latched_status = bt_inb(bt, STATUS_REG);
1500 bt->command_cmp = TRUE;
1503 bt_outb(bt, CONTROL_REG, RESET_INTR);
1505 if ((intstat & IMB_LOADED) != 0) {
1506 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1508 btccbptov(bt, bt->cur_inbox->ccb_addr),
1509 bt->cur_inbox->comp_code);
1510 bt->cur_inbox->comp_code = BMBI_FREE;
1515 if ((intstat & SCSI_BUS_RESET) != 0) {
1516 btreset(bt, /*hardreset*/FALSE);
1522 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1525 struct ccb_scsiio *csio;
1528 csio = &bccb->ccb->csio;
1530 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1531 device_printf(bt->dev,
1532 "btdone - Attempt to free non-active BCCB %p\n",
1537 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1538 bus_dmasync_op_t op;
1540 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1541 op = BUS_DMASYNC_POSTREAD;
1543 op = BUS_DMASYNC_POSTWRITE;
1544 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1545 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1548 if (bccb == bt->recovery_bccb) {
1550 * The recovery BCCB does not have a CCB associated
1551 * with it, so short circuit the normal error handling.
1552 * We now traverse our list of pending CCBs and process
1553 * any that were terminated by the recovery CCBs action.
1554 * We also reinstate timeouts for all remaining, pending,
1557 struct cam_path *path;
1558 struct ccb_hdr *ccb_h;
1561 /* Notify all clients that a BDR occurred */
1562 error = xpt_create_path(&path, /*periph*/NULL,
1563 cam_sim_path(bt->sim),
1564 bccb->hccb.target_id,
1567 if (error == CAM_REQ_CMP) {
1568 xpt_async(AC_SENT_BDR, path, NULL);
1569 xpt_free_path(path);
1572 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1573 while (ccb_h != NULL) {
1574 struct bt_ccb *pending_bccb;
1576 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1577 if (pending_bccb->hccb.target_id
1578 == bccb->hccb.target_id) {
1579 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1580 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1581 btdone(bt, pending_bccb, BMBI_ERROR);
1583 callout_reset_sbt(&pending_bccb->timer,
1584 SBT_1MS * ccb_h->timeout, 0, bttimeout,
1586 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1589 device_printf(bt->dev, "No longer in timeout\n");
1593 callout_stop(&bccb->timer);
1595 switch (comp_code) {
1597 device_printf(bt->dev,
1598 "btdone - CCB completed with free status!\n");
1600 case BMBI_NOT_FOUND:
1601 device_printf(bt->dev,
1602 "btdone - CCB Abort failed to find CCB\n");
1607 printf("bt: ccb %p - error %x occurred. "
1608 "btstat = %x, sdstat = %x\n",
1609 (void *)bccb, comp_code, bccb->hccb.btstat,
1612 /* An error occurred */
1613 switch(bccb->hccb.btstat) {
1614 case BTSTAT_DATARUN_ERROR:
1615 if (bccb->hccb.data_len == 0) {
1617 * At least firmware 4.22, does this
1618 * for a QUEUE FULL condition.
1620 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1621 } else if (bccb->hccb.data_len < 0) {
1622 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1626 case BTSTAT_NOERROR:
1627 case BTSTAT_LINKED_CMD_COMPLETE:
1628 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1629 case BTSTAT_DATAUNDERUN_ERROR:
1631 csio->scsi_status = bccb->hccb.sdstat;
1632 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1633 switch(csio->scsi_status) {
1634 case SCSI_STATUS_CHECK_COND:
1635 case SCSI_STATUS_CMD_TERMINATED:
1636 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1637 /* Bounce sense back if necessary */
1638 if (bt->sense_buffers != NULL) {
1640 *btsensevaddr(bt, bccb);
1645 case SCSI_STATUS_OK:
1646 csio->ccb_h.status = CAM_REQ_CMP;
1649 csio->resid = bccb->hccb.data_len;
1651 case BTSTAT_SELTIMEOUT:
1652 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1654 case BTSTAT_UNEXPECTED_BUSFREE:
1655 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1657 case BTSTAT_INVALID_PHASE:
1658 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1660 case BTSTAT_INVALID_ACTION_CODE:
1661 panic("%s: Inavlid Action code", bt_name(bt));
1663 case BTSTAT_INVALID_OPCODE:
1664 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1666 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1667 /* We don't even support linked commands... */
1668 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1670 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1671 panic("%s: Invalid CCB or SG list", bt_name(bt));
1673 case BTSTAT_AUTOSENSE_FAILED:
1674 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1676 case BTSTAT_TAGGED_MSG_REJECTED:
1678 struct ccb_trans_settings neg;
1679 struct ccb_trans_settings_scsi *scsi =
1680 &neg.proto_specific.scsi;
1682 neg.protocol = PROTO_SCSI;
1683 neg.protocol_version = SCSI_REV_2;
1684 neg.transport = XPORT_SPI;
1685 neg.transport_version = 2;
1686 scsi->valid = CTS_SCSI_VALID_TQ;
1688 xpt_print_path(csio->ccb_h.path);
1689 printf("refuses tagged commands. Performing "
1690 "non-tagged I/O\n");
1691 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1693 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1694 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1695 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1698 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1700 * XXX You would think that this is
1701 * a recoverable error... Hmmm.
1703 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1705 case BTSTAT_HA_SOFTWARE_ERROR:
1706 case BTSTAT_HA_WATCHDOG_ERROR:
1707 case BTSTAT_HARDWARE_FAILURE:
1708 /* Hardware reset ??? Can we recover ??? */
1709 csio->ccb_h.status = CAM_NO_HBA;
1711 case BTSTAT_TARGET_IGNORED_ATN:
1712 case BTSTAT_OTHER_SCSI_BUS_RESET:
1713 case BTSTAT_HA_SCSI_BUS_RESET:
1714 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1716 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1719 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1720 csio->ccb_h.status = CAM_BDR_SENT;
1722 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1724 case BTSTAT_INVALID_RECONNECT:
1725 case BTSTAT_ABORT_QUEUE_GENERATED:
1726 csio->ccb_h.status = CAM_REQ_TERMIO;
1728 case BTSTAT_SCSI_PERROR_DETECTED:
1729 csio->ccb_h.status = CAM_UNCOR_PARITY;
1732 if (csio->ccb_h.status != CAM_REQ_CMP) {
1733 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1734 csio->ccb_h.status |= CAM_DEV_QFRZN;
1736 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1737 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1738 btfreeccb(bt, bccb);
1742 /* All completed without incident */
1743 ccb->ccb_h.status |= CAM_REQ_CMP;
1744 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1745 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1746 btfreeccb(bt, bccb);
1753 btreset(struct bt_softc* bt, int hard_reset)
1755 struct ccb_hdr *ccb_h;
1758 u_int8_t reset_type;
1760 if (hard_reset != 0)
1761 reset_type = HARD_RESET;
1763 reset_type = SOFT_RESET;
1764 bt_outb(bt, CONTROL_REG, reset_type);
1766 /* Wait 5sec. for Diagnostic start */
1767 timeout = 5 * 10000;
1769 status = bt_inb(bt, STATUS_REG);
1770 if ((status & DIAG_ACTIVE) != 0)
1776 device_printf(bt->dev,
1777 "btreset - Diagnostic Active failed to "
1778 "assert. status = 0x%x\n", status);
1782 /* Wait 10sec. for Diagnostic end */
1783 timeout = 10 * 10000;
1785 status = bt_inb(bt, STATUS_REG);
1786 if ((status & DIAG_ACTIVE) == 0)
1791 panic("%s: btreset - Diagnostic Active failed to drop. "
1792 "status = 0x%x\n", bt_name(bt), status);
1796 /* Wait for the host adapter to become ready or report a failure */
1799 status = bt_inb(bt, STATUS_REG);
1800 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1805 device_printf(bt->dev,
1806 "btreset - Host adapter failed to come ready. "
1807 "status = 0x%x\n", status);
1811 /* If the diagnostics failed, tell the user */
1812 if ((status & DIAG_FAIL) != 0
1813 || (status & HA_READY) == 0) {
1814 device_printf(bt->dev,
1815 "btreset - Adapter failed diagnostics\n");
1817 if ((status & DATAIN_REG_READY) != 0)
1818 device_printf(bt->dev,
1819 "btreset - Host Adapter Error code = 0x%x\n",
1820 bt_inb(bt, DATAIN_REG));
1824 /* If we've allocated mailboxes, initialize them */
1825 if (bt->init_level > 4)
1828 /* If we've attached to the XPT, tell it about the event */
1829 if (bt->path != NULL)
1830 xpt_async(AC_BUS_RESET, bt->path, NULL);
1833 * Perform completion processing for all outstanding CCBs.
1835 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1836 struct bt_ccb *pending_bccb;
1838 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1839 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1840 btdone(bt, pending_bccb, BMBI_ERROR);
1847 * Send a command to the adapter.
1850 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1851 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1857 u_int reply_buf_size;
1861 /* No data returned to start */
1862 reply_buf_size = reply_len;
1869 bt->command_cmp = 0;
1871 * Wait up to 10 sec. for the adapter to become
1872 * ready to accept commands.
1876 status = bt_inb(bt, STATUS_REG);
1877 if ((status & HA_READY) != 0
1878 && (status & CMD_REG_BUSY) == 0)
1881 * Throw away any pending data which may be
1882 * left over from earlier commands that we
1885 if ((status & DATAIN_REG_READY) != 0)
1886 (void)bt_inb(bt, DATAIN_REG);
1890 device_printf(bt->dev,
1891 "bt_cmd: Timeout waiting for adapter ready, "
1892 "status = 0x%x\n", status);
1897 * Send the opcode followed by any necessary parameter bytes.
1899 bt_outb(bt, COMMAND_REG, opcode);
1902 * Wait for up to 1sec for each byte of the
1903 * parameter list sent to be sent.
1906 while (param_len && --timeout) {
1908 status = bt_inb(bt, STATUS_REG);
1909 intstat = bt_inb(bt, INTSTAT_REG);
1911 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1912 == (INTR_PENDING|CMD_COMPLETE)) {
1913 saved_status = status;
1917 if (bt->command_cmp != 0) {
1918 saved_status = bt->latched_status;
1922 if ((status & DATAIN_REG_READY) != 0)
1924 if ((status & CMD_REG_BUSY) == 0) {
1925 bt_outb(bt, COMMAND_REG, *params++);
1931 device_printf(bt->dev, "bt_cmd: Timeout sending parameters, "
1932 "status = 0x%x\n", status);
1934 saved_status = status;
1939 * Wait for the command to complete.
1941 while (cmd_complete == 0 && --cmd_timeout) {
1943 status = bt_inb(bt, STATUS_REG);
1944 intstat = bt_inb(bt, INTSTAT_REG);
1946 * It may be that this command was issued with
1947 * controller interrupts disabled. We'll never
1948 * get to our command if an incoming mailbox
1949 * interrupt is pending, so take care of completed
1950 * mailbox commands by calling our interrupt handler.
1952 if ((intstat & (INTR_PENDING|IMB_LOADED))
1953 == (INTR_PENDING|IMB_LOADED))
1956 if (bt->command_cmp != 0) {
1958 * Our interrupt handler saw CMD_COMPLETE
1959 * status before we did.
1962 saved_status = bt->latched_status;
1963 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1964 == (INTR_PENDING|CMD_COMPLETE)) {
1966 * Our poll (in case interrupts are blocked)
1967 * saw the CMD_COMPLETE interrupt.
1970 saved_status = status;
1971 } else if (opcode == BOP_MODIFY_IO_ADDR
1972 && (status & CMD_REG_BUSY) == 0) {
1974 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1975 * but it should update the status register. So, we
1976 * consider this command complete when the CMD_REG_BUSY
1979 saved_status = status;
1981 } else if ((status & DATAIN_REG_READY) != 0) {
1984 data = bt_inb(bt, DATAIN_REG);
1985 if (reply_len < reply_buf_size) {
1986 *reply_data++ = data;
1988 device_printf(bt->dev,
1989 "bt_cmd - Discarded reply data byte "
1990 "for opcode 0x%x\n", opcode);
1993 * Reset timeout to ensure at least a second
1994 * between response bytes.
1996 cmd_timeout = MAX(cmd_timeout, 10000);
1999 } else if ((opcode == BOP_FETCH_LRAM)
2000 && (status & HA_READY) != 0) {
2001 saved_status = status;
2006 if (cmd_timeout == 0) {
2007 device_printf(bt->dev,
2008 "bt_cmd: Timeout waiting for command (%x) "
2009 "to complete.\n", opcode);
2010 device_printf(bt->dev, "status = 0x%x, intstat = 0x%x, "
2011 "rlen %d\n", status, intstat, reply_len);
2012 error = (ETIMEDOUT);
2016 * Clear any pending interrupts.
2024 * If the command was rejected by the controller, tell the caller.
2026 if ((saved_status & CMD_INVALID) != 0) {
2028 * Some early adapters may not recover properly from
2029 * an invalid command. If it appears that the controller
2030 * has wedged (i.e. status was not cleared by our interrupt
2031 * reset above), perform a soft reset.
2034 device_printf(bt->dev, "Invalid Command 0x%x\n",
2037 status = bt_inb(bt, STATUS_REG);
2038 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2039 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2040 || (status & (HA_READY|INIT_REQUIRED))
2041 != (HA_READY|INIT_REQUIRED)) {
2042 btreset(bt, /*hard_reset*/FALSE);
2047 if (param_len > 0) {
2048 /* The controller did not accept the full argument list */
2052 if (reply_len != reply_buf_size) {
2053 /* Too much or too little data received */
2057 /* We were successful */
2062 btinitmboxes(struct bt_softc *bt) {
2063 init_32b_mbox_params_t init_mbox;
2066 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2067 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2068 bt->cur_inbox = bt->in_boxes;
2069 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2070 bt->cur_outbox = bt->out_boxes;
2071 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2073 /* Tell the adapter about them */
2074 init_mbox.num_boxes = bt->num_boxes;
2075 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2076 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2077 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2078 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2079 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2080 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2081 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2084 printf("btinitmboxes: Initialization command failed\n");
2085 else if (bt->strict_rr != 0) {
2087 * If the controller supports
2088 * strict round robin mode,
2094 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1,
2095 /*reply_buf*/NULL, /*reply_len*/0,
2096 DEFAULT_CMD_TIMEOUT);
2099 printf("btinitmboxes: Unable to enable strict RR\n");
2101 } else if (bootverbose) {
2102 device_printf(bt->dev,
2103 "Using Strict Round Robin Mailbox Mode\n");
2111 * Update the XPT's idea of the negotiated transfer
2112 * parameters for a particular target.
2115 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings *cts)
2117 setup_data_t setup_info;
2126 targ_syncinfo_t sync_info;
2127 struct ccb_trans_settings_scsi *scsi =
2128 &cts->proto_specific.scsi;
2129 struct ccb_trans_settings_spi *spi =
2130 &cts->xport_specific.spi;
2135 target = cts->ccb_h.target_id;
2136 targ_offset = (target & 0x7);
2137 targ_mask = (0x01 << targ_offset);
2140 * Inquire Setup Information. This command retreives the
2141 * Wide negotiation status for recent adapters as well as
2142 * the sync info for older models.
2144 param = sizeof(setup_info);
2145 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
2146 (u_int8_t*)&setup_info, sizeof(setup_info),
2147 DEFAULT_CMD_TIMEOUT);
2150 device_printf(bt->dev,
2151 "btfetchtransinfo - Inquire Setup Info Failed %x\n",
2156 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2157 : setup_info.high_syncinfo[targ_offset];
2159 if (sync_info.sync == 0)
2162 sync_offset = sync_info.offset;
2165 bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2166 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2170 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2171 : (setup_info.high_wide_active & targ_mask);
2174 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2175 } else if ((bt->wide_permitted & targ_mask) != 0) {
2176 struct ccb_getdev cgd;
2179 * Prior to rev 5.06L, wide status isn't provided,
2180 * so we "guess" that wide transfers are in effect
2181 * if the user settings allow for wide and the inquiry
2182 * data for the device indicates that it can handle
2185 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2186 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2187 xpt_action((union ccb *)&cgd);
2188 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2189 && (cgd.inq_data.flags & SID_WBus16) != 0)
2190 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2193 if (bt->firmware_ver[0] >= '3') {
2195 * For adapters that can do fast or ultra speeds,
2196 * use the more exact Target Sync Information command.
2198 target_sync_info_data_t sync_info;
2200 param = sizeof(sync_info);
2201 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1,
2202 (u_int8_t*)&sync_info, sizeof(sync_info),
2203 DEFAULT_CMD_TIMEOUT);
2206 device_printf(bt->dev,
2207 "btfetchtransinfo - Inquire Sync "
2208 "Info Failed 0x%x\n", error);
2211 sync_period = sync_info.sync_rate[target] * 100;
2213 sync_period = 2000 + (500 * sync_info.period);
2216 cts->protocol = PROTO_SCSI;
2217 cts->protocol_version = SCSI_REV_2;
2218 cts->transport = XPORT_SPI;
2219 cts->transport_version = 2;
2221 spi->sync_period = sync_period;
2222 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2223 spi->sync_offset = sync_offset;
2224 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2226 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2227 spi->bus_width = bus_width;
2229 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2230 scsi->valid = CTS_SCSI_VALID_TQ;
2231 spi->valid |= CTS_SPI_VALID_DISC;
2235 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2239 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2241 struct bt_softc* bt;
2243 bt = (struct bt_softc*)arg;
2244 bt->mailbox_physbase = segs->ds_addr;
2248 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2250 struct bt_softc* bt;
2252 bt = (struct bt_softc*)arg;
2253 bt->bt_ccb_physbase = segs->ds_addr;
2257 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2260 struct bt_softc* bt;
2262 bt = (struct bt_softc*)arg;
2263 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2267 btpoll(struct cam_sim *sim)
2269 bt_intr_locked(cam_sim_softc(sim));
2273 bttimeout(void *arg)
2275 struct bt_ccb *bccb;
2277 struct bt_softc *bt;
2279 bccb = (struct bt_ccb *)arg;
2281 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2282 mtx_assert(&bt->lock, MA_OWNED);
2283 xpt_print_path(ccb->ccb_h.path);
2284 printf("CCB %p - timed out\n", (void *)bccb);
2286 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2287 xpt_print_path(ccb->ccb_h.path);
2288 printf("CCB %p - timed out CCB already completed\n",
2294 * In order to simplify the recovery process, we ask the XPT
2295 * layer to halt the queue of new transactions and we traverse
2296 * the list of pending CCBs and remove their timeouts. This
2297 * means that the driver attempts to clear only one error
2298 * condition at a time. In general, timeouts that occur
2299 * close together are related anyway, so there is no benefit
2300 * in attempting to handle errors in parallel. Timeouts will
2301 * be reinstated when the recovery process ends.
2303 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2304 struct ccb_hdr *ccb_h;
2306 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2307 xpt_freeze_simq(bt->sim, /*count*/1);
2308 bccb->flags |= BCCB_RELEASE_SIMQ;
2311 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2312 while (ccb_h != NULL) {
2313 struct bt_ccb *pending_bccb;
2315 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2316 callout_stop(&pending_bccb->timer);
2317 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2321 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2322 || bt->cur_outbox->action_code != BMBO_FREE
2323 || ((bccb->hccb.tag_enable == TRUE)
2324 && (bt->firmware_ver[0] < '5'))) {
2326 * Try a full host adapter/SCSI bus reset.
2327 * We do this only if we have already attempted
2328 * to clear the condition with a BDR, or we cannot
2329 * attempt a BDR for lack of mailbox resources
2330 * or because of faulty firmware. It turns out
2331 * that firmware versions prior to 5.xx treat BDRs
2332 * as untagged commands that cannot be sent until
2333 * all outstanding tagged commands have been processed.
2334 * This makes it somewhat difficult to use a BDR to
2335 * clear up a problem with an uncompleted tagged command.
2337 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2338 btreset(bt, /*hardreset*/TRUE);
2339 device_printf(bt->dev, "No longer in timeout\n");
2342 * Send a Bus Device Reset message:
2343 * The target that is holding up the bus may not
2344 * be the same as the one that triggered this timeout
2345 * (different commands have different timeout lengths),
2346 * but we have no way of determining this from our
2347 * timeout handler. Our strategy here is to queue a
2348 * BDR message to the target of the timed out command.
2349 * If this fails, we'll get another timeout 2 seconds
2350 * later which will attempt a bus reset.
2352 bccb->flags |= BCCB_DEVICE_RESET;
2353 callout_reset(&bccb->timer, 2 * hz, bttimeout, bccb);
2355 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2357 /* No Data Transfer */
2358 bt->recovery_bccb->hccb.datain = TRUE;
2359 bt->recovery_bccb->hccb.dataout = TRUE;
2360 bt->recovery_bccb->hccb.btstat = 0;
2361 bt->recovery_bccb->hccb.sdstat = 0;
2362 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2364 /* Tell the adapter about this command */
2365 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2366 bt->cur_outbox->action_code = BMBO_START;
2367 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
2372 MODULE_VERSION(bt, 1);
2373 MODULE_DEPEND(bt, cam, 1, 1, 1);