2 * Generic driver for the BusLogic MultiMaster SCSI host adapters
3 * Product specific probe and attach routines can be found in:
4 * sys/dev/buslogic/bt_isa.c BT-54X, BT-445 cards
5 * sys/dev/buslogic/bt_eisa.c BT-74X, BT-75x cards, SDC3222F
6 * sys/dev/buslogic/bt_pci.c BT-946, BT-948, BT-956, BT-958 cards
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
32 * $FreeBSD: src/sys/dev/buslogic/bt.c,v 1.25.2.1 2000/08/02 22:32:26 peter Exp $
33 * $DragonFly: src/sys/dev/disk/buslogic/bt.c,v 1.15 2006/12/22 23:26:16 swildner Exp $
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>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
52 #include <sys/thread2.h>
54 #include <machine/clock.h>
56 #include <bus/cam/cam.h>
57 #include <bus/cam/cam_ccb.h>
58 #include <bus/cam/cam_sim.h>
59 #include <bus/cam/cam_xpt_sim.h>
60 #include <bus/cam/cam_debug.h>
61 #include <bus/cam/scsi/scsi_message.h>
68 /* MailBox Management functions */
69 static __inline void btnextinbox(struct bt_softc *bt);
70 static __inline void btnextoutbox(struct bt_softc *bt);
73 btnextinbox(struct bt_softc *bt)
75 if (bt->cur_inbox == bt->last_inbox)
76 bt->cur_inbox = bt->in_boxes;
82 btnextoutbox(struct bt_softc *bt)
84 if (bt->cur_outbox == bt->last_outbox)
85 bt->cur_outbox = bt->out_boxes;
90 /* CCB Mangement functions */
91 static __inline u_int32_t btccbvtop(struct bt_softc *bt,
93 static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
95 static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
97 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
100 static __inline u_int32_t
101 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
103 return (bt->bt_ccb_physbase
104 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
107 static __inline struct bt_ccb *
108 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
110 return (bt->bt_ccb_array +
111 ((struct bt_ccb*)ccb_addr-(struct bt_ccb*)bt->bt_ccb_physbase));
114 static __inline u_int32_t
115 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
119 index = (u_int)(bccb - bt->bt_ccb_array);
120 return (bt->sense_buffers_physbase
121 + (index * sizeof(struct scsi_sense_data)));
124 static __inline struct scsi_sense_data *
125 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
129 index = (u_int)(bccb - bt->bt_ccb_array);
130 return (bt->sense_buffers + index);
133 static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
134 static __inline void btfreeccb(struct bt_softc *bt,
135 struct bt_ccb *bccb);
136 static void btallocccbs(struct bt_softc *bt);
137 static bus_dmamap_callback_t btexecuteccb;
138 static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
139 bt_mbi_comp_code_t comp_code);
141 /* Host adapter command functions */
142 static int btreset(struct bt_softc* bt, int hard_reset);
144 /* Initialization functions */
145 static int btinitmboxes(struct bt_softc *bt);
146 static bus_dmamap_callback_t btmapmboxes;
147 static bus_dmamap_callback_t btmapccbs;
148 static bus_dmamap_callback_t btmapsgs;
150 /* Transfer Negotiation Functions */
151 static void btfetchtransinfo(struct bt_softc *bt,
152 struct ccb_trans_settings *cts);
154 /* CAM SIM entry points */
155 #define ccb_bccb_ptr spriv_ptr0
156 #define ccb_bt_ptr spriv_ptr1
157 static void btaction(struct cam_sim *sim, union ccb *ccb);
158 static void btpoll(struct cam_sim *sim);
160 /* Our timeout handler */
167 * Do our own re-probe protection until a configuration
168 * manager can do it for us. This ensures that we don't
169 * reprobe a card already found by the EISA or PCI probes.
171 struct bt_isa_port bt_isa_ports[] =
182 * I/O ports listed in the order enumerated by the
183 * card for certain op codes.
185 u_int16_t bt_board_ports[] =
195 /* Exported functions */
197 bt_init_softc(device_t dev, struct resource *port,
198 struct resource *irq, struct resource *drq)
200 struct bt_softc *bt = device_get_softc(dev);
202 SLIST_INIT(&bt->free_bt_ccbs);
203 LIST_INIT(&bt->pending_ccbs);
204 SLIST_INIT(&bt->sg_maps);
206 bt->unit = device_get_unit(dev);
210 bt->tag = rman_get_bustag(port);
211 bt->bsh = rman_get_bushandle(port);
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 kfree(sg_map, M_DEVBUF);
240 bus_dma_tag_destroy(bt->sg_dmat);
243 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
245 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
247 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
249 bus_dma_tag_destroy(bt->ccb_dmat);
251 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
253 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
255 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
257 bus_dma_tag_destroy(bt->buffer_dmat);
259 bus_dma_tag_destroy(bt->mailbox_dmat);
266 bt_port_probe(device_t dev, struct bt_probe_info *info)
268 struct bt_softc *bt = device_get_softc(dev);
269 config_data_t config_data;
272 /* See if there is really a card present */
273 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
277 * Determine our IRQ, and DMA settings and
278 * export them to the configuration system.
280 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
281 (u_int8_t*)&config_data, sizeof(config_data),
282 DEFAULT_CMD_TIMEOUT);
284 kprintf("bt_port_probe: Could not determine IRQ or DMA "
285 "settings for adapter.\n");
289 if (bt->model[0] == '5') {
290 /* DMA settings only make sense for ISA cards */
291 switch (config_data.dma_chan) {
302 kprintf("bt_port_probe: Invalid DMA setting "
303 "detected for adapter.\n");
307 /* VL/EISA/PCI DMA */
310 switch (config_data.irq) {
317 info->irq = ffs(config_data.irq) + 8;
320 kprintf("bt_port_probe: Invalid IRQ setting %x"
321 "detected for adapter.\n", config_data.irq);
328 * Probe the adapter and verify that the card is a BusLogic.
331 bt_probe(device_t dev)
333 struct bt_softc *bt = device_get_softc(dev);
334 esetup_info_data_t esetup_info;
342 * See if the three I/O ports look reasonable.
343 * Touch the minimal number of registers in the
346 status = bt_inb(bt, STATUS_REG);
348 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
349 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
351 device_printf(dev, "Failed Status Reg Test - %x\n",
356 intstat = bt_inb(bt, INTSTAT_REG);
357 if ((intstat & INTSTAT_REG_RSVD) != 0) {
358 device_printf(dev, "Failed Intstat Reg Test\n");
362 geometry = bt_inb(bt, GEOMETRY_REG);
363 if (geometry == 0xFF) {
365 device_printf(dev, "Failed Geometry Reg Test\n");
370 * Looking good so far. Final test is to reset the
371 * adapter and attempt to fetch the extended setup
372 * information. This should filter out all 1542 cards.
374 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
376 device_printf(dev, "Failed Reset\n");
380 param = sizeof(esetup_info);
381 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1,
382 (u_int8_t*)&esetup_info, sizeof(esetup_info),
383 DEFAULT_CMD_TIMEOUT);
392 * Pull the boards setup information and record it in our softc.
395 bt_fetch_adapter_info(device_t dev)
397 struct bt_softc *bt = device_get_softc(dev);
398 board_id_data_t board_id;
399 esetup_info_data_t esetup_info;
400 config_data_t config_data;
402 u_int8_t length_param;
404 /* First record the firmware version */
405 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
406 (u_int8_t*)&board_id, sizeof(board_id),
407 DEFAULT_CMD_TIMEOUT);
409 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
412 bt->firmware_ver[0] = board_id.firmware_rev_major;
413 bt->firmware_ver[1] = '.';
414 bt->firmware_ver[2] = board_id.firmware_rev_minor;
415 bt->firmware_ver[3] = '\0';
418 * Depending on the firmware major and minor version,
419 * we may be able to fetch additional minor version info.
421 if (bt->firmware_ver[0] > '0') {
423 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
424 (u_int8_t*)&bt->firmware_ver[3], 1,
425 DEFAULT_CMD_TIMEOUT);
428 "bt_fetch_adapter_info - Failed Get "
429 "Firmware 3rd Digit\n");
432 if (bt->firmware_ver[3] == ' ')
433 bt->firmware_ver[3] = '\0';
434 bt->firmware_ver[4] = '\0';
437 if (strcmp(bt->firmware_ver, "3.3") >= 0) {
439 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
440 (u_int8_t*)&bt->firmware_ver[4], 1,
441 DEFAULT_CMD_TIMEOUT);
444 "bt_fetch_adapter_info - Failed Get "
445 "Firmware 4th Digit\n");
448 if (bt->firmware_ver[4] == ' ')
449 bt->firmware_ver[4] = '\0';
450 bt->firmware_ver[5] = '\0';
454 * Some boards do not handle the "recently documented"
455 * Inquire Board Model Number command correctly or do not give
456 * exact information. Use the Firmware and Extended Setup
457 * information in these cases to come up with the right answer.
458 * The major firmware revision number indicates:
460 * 5.xx BusLogic "W" Series Host Adapters:
462 * 4.xx BusLogic "C" Series Host Adapters:
463 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
464 * 3.xx BusLogic "S" Series Host Adapters:
465 * BT-747S/747D/757S/757D/445S/545S/542D
466 * BT-542B/742A (revision H)
467 * 2.xx BusLogic "A" Series Host Adapters:
468 * BT-542B/742A (revision G and below)
469 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
471 length_param = sizeof(esetup_info);
472 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
473 (u_int8_t*)&esetup_info, sizeof(esetup_info),
474 DEFAULT_CMD_TIMEOUT);
479 bt->bios_addr = esetup_info.bios_addr << 12;
481 if (esetup_info.bus_type == 'A'
482 && bt->firmware_ver[0] == '2') {
483 ksnprintf(bt->model, sizeof(bt->model), "542B");
484 } else if (esetup_info.bus_type == 'E'
485 && (strncmp(bt->firmware_ver, "2.1", 3) == 0
486 || strncmp(bt->firmware_ver, "2.20", 4) == 0)) {
487 ksnprintf(bt->model, sizeof(bt->model), "742A");
488 } else if (esetup_info.bus_type == 'E'
489 && bt->firmware_ver[0] == '0') {
490 /* AMI FastDisk EISA Series 441 0.x */
491 ksnprintf(bt->model, sizeof(bt->model), "747A");
493 ha_model_data_t model_data;
496 length_param = sizeof(model_data);
497 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
498 (u_int8_t*)&model_data, sizeof(model_data),
499 DEFAULT_CMD_TIMEOUT);
502 "bt_fetch_adapter_info - Failed Inquire "
506 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
507 bt->model[i] = model_data.ascii_model[i];
508 if (bt->model[i] == ' ')
514 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
516 /* SG element limits */
517 bt->max_sg = esetup_info.max_sg;
519 /* Set feature flags */
520 bt->wide_bus = esetup_info.wide_bus;
521 bt->diff_bus = esetup_info.diff_bus;
522 bt->ultra_scsi = esetup_info.ultra_scsi;
524 if ((bt->firmware_ver[0] == '5')
525 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
526 bt->extended_lun = TRUE;
528 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
531 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
534 * Determine max CCB count and whether tagged queuing is
535 * available based on controller type. Tagged queuing
536 * only works on 'W' series adapters, 'C' series adapters
537 * with firmware of rev 4.42 and higher, and 'S' series
538 * adapters with firmware of rev 3.35 and higher. The
539 * maximum CCB counts are as follows:
541 * 192 BT-948/958/958D
542 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
544 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
546 if (bt->firmware_ver[0] == '5') {
548 bt->tag_capable = TRUE;
549 } else if (bt->firmware_ver[0] == '4') {
550 if (bt->model[0] == '5')
554 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
557 if (bt->firmware_ver[0] == '3'
558 && (strcmp(bt->firmware_ver, "3.35") >= 0))
559 bt->tag_capable = TRUE;
561 bt->tag_capable = FALSE;
564 if (bt->tag_capable != FALSE)
565 bt->tags_permitted = ALL_TARGETS;
567 /* Determine Sync/Wide/Disc settings */
568 if (bt->firmware_ver[0] >= '4') {
569 auto_scsi_data_t auto_scsi_data;
570 fetch_lram_params_t fetch_lram_params;
574 * These settings are stored in the
575 * AutoSCSI data in LRAM of 'W' and 'C'
578 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
579 fetch_lram_params.response_len = sizeof(auto_scsi_data);
580 error = bt_cmd(bt, BOP_FETCH_LRAM,
581 (u_int8_t*)&fetch_lram_params,
582 sizeof(fetch_lram_params),
583 (u_int8_t*)&auto_scsi_data,
584 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
588 "bt_fetch_adapter_info - Failed "
589 "Get Auto SCSI Info\n");
593 bt->disc_permitted = auto_scsi_data.low_disc_permitted
594 | (auto_scsi_data.high_disc_permitted << 8);
595 bt->sync_permitted = auto_scsi_data.low_sync_permitted
596 | (auto_scsi_data.high_sync_permitted << 8);
597 bt->fast_permitted = auto_scsi_data.low_fast_permitted
598 | (auto_scsi_data.high_fast_permitted << 8);
599 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
600 | (auto_scsi_data.high_ultra_permitted << 8);
601 bt->wide_permitted = auto_scsi_data.low_wide_permitted
602 | (auto_scsi_data.high_wide_permitted << 8);
604 if (bt->ultra_scsi == FALSE)
605 bt->ultra_permitted = 0;
607 if (bt->wide_bus == FALSE)
608 bt->wide_permitted = 0;
611 * 'S' and 'A' series have this information in the setup
612 * information structure.
614 setup_data_t setup_info;
616 length_param = sizeof(setup_info);
617 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
618 /*paramlen*/1, (u_int8_t*)&setup_info,
619 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
623 "bt_fetch_adapter_info - Failed "
628 if (setup_info.initiate_sync != 0) {
629 bt->sync_permitted = ALL_TARGETS;
631 if (bt->model[0] == '7') {
632 if (esetup_info.sync_neg10MB != 0)
633 bt->fast_permitted = ALL_TARGETS;
634 if (strcmp(bt->model, "757") == 0)
635 bt->wide_permitted = ALL_TARGETS;
638 bt->disc_permitted = ALL_TARGETS;
641 /* We need as many mailboxes as we can have ccbs */
642 bt->num_boxes = bt->max_ccbs;
644 /* Determine our SCSI ID */
646 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
647 (u_int8_t*)&config_data, sizeof(config_data),
648 DEFAULT_CMD_TIMEOUT);
651 "bt_fetch_adapter_info - Failed Get Config\n");
654 bt->scsi_id = config_data.scsi_id;
660 * Start the board, ready for normal operation
663 bt_init(device_t dev)
665 struct bt_softc *bt = device_get_softc(dev);
667 /* Announce the Adapter */
668 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
670 if (bt->ultra_scsi != 0)
673 if (bt->wide_bus != 0)
678 if (bt->diff_bus != 0)
681 kprintf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
685 * Create our DMA tags. These tags define the kinds of device
686 * accessible memory allocations and memory mappings we will
687 * need to perform during normal operation.
689 * Unless we need to further restrict the allocation, we rely
690 * on the restrictions of the parent dmat, hence the common
691 * use of MAXADDR and MAXSIZE.
694 /* DMA tag for mapping buffers into device visible space. */
695 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
696 /*lowaddr*/BUS_SPACE_MAXADDR,
697 /*highaddr*/BUS_SPACE_MAXADDR,
698 /*filter*/NULL, /*filterarg*/NULL,
699 /*maxsize*/MAXBSIZE, /*nsegments*/BT_NSEG,
700 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
701 /*flags*/BUS_DMA_ALLOCNOW,
702 &bt->buffer_dmat) != 0) {
707 /* DMA tag for our mailboxes */
708 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
709 /*lowaddr*/BUS_SPACE_MAXADDR,
710 /*highaddr*/BUS_SPACE_MAXADDR,
711 /*filter*/NULL, /*filterarg*/NULL,
712 bt->num_boxes * (sizeof(bt_mbox_in_t)
713 + sizeof(bt_mbox_out_t)),
715 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
716 /*flags*/0, &bt->mailbox_dmat) != 0) {
722 /* Allocation for our mailboxes */
723 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
724 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
730 /* And permanently map them */
731 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
733 bt->num_boxes * (sizeof(bt_mbox_in_t)
734 + sizeof(bt_mbox_out_t)),
735 btmapmboxes, bt, /*flags*/0);
739 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
743 /* DMA tag for our ccb structures */
744 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
745 /*lowaddr*/BUS_SPACE_MAXADDR,
746 /*highaddr*/BUS_SPACE_MAXADDR,
747 /*filter*/NULL, /*filterarg*/NULL,
748 bt->max_ccbs * sizeof(struct bt_ccb),
750 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
751 /*flags*/0, &bt->ccb_dmat) != 0) {
757 /* Allocation for our ccbs */
758 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
759 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
765 /* And permanently map them */
766 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
768 bt->max_ccbs * sizeof(struct bt_ccb),
769 btmapccbs, bt, /*flags*/0);
773 /* DMA tag for our S/G structures. We allocate in page sized chunks */
774 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
775 /*lowaddr*/BUS_SPACE_MAXADDR,
776 /*highaddr*/BUS_SPACE_MAXADDR,
777 /*filter*/NULL, /*filterarg*/NULL,
778 PAGE_SIZE, /*nsegments*/1,
779 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
780 /*flags*/0, &bt->sg_dmat) != 0) {
786 /* Perform initial CCB allocation */
787 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
790 if (bt->num_ccbs == 0) {
792 "bt_init - Unable to allocate initial ccbs\n");
797 * Note that we are going and return (to probe)
807 bt_attach(device_t dev)
809 struct bt_softc *bt = device_get_softc(dev);
810 int tagged_dev_openings;
811 struct cam_devq *devq;
815 * We reserve 1 ccb for error recovery, so don't
816 * tell the XPT about it.
818 if (bt->tag_capable != 0)
819 tagged_dev_openings = bt->max_ccbs - 1;
821 tagged_dev_openings = 0;
824 * Create the device queue for our SIM.
826 devq = cam_simq_alloc(bt->max_ccbs - 1);
831 * Construct our SIM entry
833 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
834 2, tagged_dev_openings, devq);
835 cam_simq_release(devq);
839 if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
840 cam_sim_free(bt->sim);
844 if (xpt_create_path(&bt->path, /*periph*/NULL,
845 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
846 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
847 xpt_bus_deregister(cam_sim_path(bt->sim));
848 cam_sim_free(bt->sim);
855 error = bus_setup_intr(dev, bt->irq, 0,
856 bt_intr, bt, &bt->ih, NULL);
858 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
866 bt_check_probed_iop(u_int ioport)
870 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
871 if (bt_isa_ports[i].addr == ioport) {
872 if (bt_isa_ports[i].probed != 0)
883 bt_mark_probed_bio(isa_compat_io_t port)
885 if (port < BIO_DISABLED)
886 bt_mark_probed_iop(bt_board_ports[port]);
890 bt_mark_probed_iop(u_int ioport)
894 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
895 if (ioport == bt_isa_ports[i].addr) {
896 bt_isa_ports[i].probed = 1;
903 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
908 for (i = 0;i < BT_NUM_ISAPORTS; i++)
909 if (ioport <= bt_isa_ports[i].addr)
911 if ((i >= BT_NUM_ISAPORTS)
912 || (ioport != bt_isa_ports[i].addr)) {
913 kprintf("\nbt_isa_probe: Invalid baseport of 0x%x specified.\n"
914 "bt_isa_probe: Nearest valid baseport is 0x%x.\n"
915 "bt_isa_probe: Failing probe.\n",
917 (i < BT_NUM_ISAPORTS)
918 ? bt_isa_ports[i].addr
919 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
920 *port_index = *max_port_index = -1;
923 *port_index = *max_port_index = bt_isa_ports[i].bio;
926 *max_port_index = BT_NUM_ISAPORTS - 1;
931 bt_iop_from_bio(isa_compat_io_t bio_index)
933 if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
934 return (bt_board_ports[bio_index]);
940 btallocccbs(struct bt_softc *bt)
942 struct bt_ccb *next_ccb;
943 struct sg_map_node *sg_map;
949 if (bt->num_ccbs >= bt->max_ccbs)
950 /* Can't allocate any more */
953 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
955 sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_WAITOK);
957 /* Allocate S/G space for the next batch of CCBS */
958 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
959 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
960 kfree(sg_map, M_DEVBUF);
964 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
966 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
967 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
969 segs = sg_map->sg_vaddr;
970 physaddr = sg_map->sg_physaddr;
972 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
973 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
976 next_ccb->sg_list = segs;
977 next_ccb->sg_list_phys = physaddr;
978 next_ccb->flags = BCCB_FREE;
979 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
983 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
985 physaddr += (BT_NSEG * sizeof(bt_sg_t));
990 /* Reserve a CCB for error recovery */
991 if (bt->recovery_bccb == NULL) {
992 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
993 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
996 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
1000 device_printf(bt->dev, "Can't malloc BCCBs\n");
1003 static __inline void
1004 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1007 if ((bccb->flags & BCCB_ACTIVE) != 0)
1008 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1009 if (bt->resource_shortage != 0
1010 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1011 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1012 bt->resource_shortage = FALSE;
1014 bccb->flags = BCCB_FREE;
1015 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1020 static __inline struct bt_ccb*
1021 btgetccb(struct bt_softc *bt)
1023 struct bt_ccb* bccb;
1026 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1027 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1031 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1033 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1043 btaction(struct cam_sim *sim, union ccb *ccb)
1045 struct bt_softc *bt;
1047 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1049 bt = (struct bt_softc *)cam_sim_softc(sim);
1051 switch (ccb->ccb_h.func_code) {
1052 /* Common cases first */
1053 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1054 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1056 struct bt_ccb *bccb;
1057 struct bt_hccb *hccb;
1060 * get a bccb to use.
1062 if ((bccb = btgetccb(bt)) == NULL) {
1064 bt->resource_shortage = TRUE;
1066 xpt_freeze_simq(bt->sim, /*count*/1);
1067 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1075 * So we can find the BCCB when an abort is requested
1078 ccb->ccb_h.ccb_bccb_ptr = bccb;
1079 ccb->ccb_h.ccb_bt_ptr = bt;
1082 * Put all the arguments for the xfer in the bccb
1084 hccb->target_id = ccb->ccb_h.target_id;
1085 hccb->target_lun = ccb->ccb_h.target_lun;
1089 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1090 struct ccb_scsiio *csio;
1091 struct ccb_hdr *ccbh;
1094 ccbh = &csio->ccb_h;
1095 hccb->opcode = INITIATOR_CCB_WRESID;
1096 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1097 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1098 hccb->cmd_len = csio->cdb_len;
1099 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1100 ccb->ccb_h.status = CAM_REQ_INVALID;
1101 btfreeccb(bt, bccb);
1105 hccb->sense_len = csio->sense_len;
1106 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1107 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1108 hccb->tag_enable = TRUE;
1109 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1111 hccb->tag_enable = FALSE;
1114 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1115 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1116 bcopy(csio->cdb_io.cdb_ptr,
1117 hccb->scsi_cdb, hccb->cmd_len);
1119 /* I guess I could map it in... */
1120 ccbh->status = CAM_REQ_INVALID;
1121 btfreeccb(bt, bccb);
1126 bcopy(csio->cdb_io.cdb_bytes,
1127 hccb->scsi_cdb, hccb->cmd_len);
1129 /* If need be, bounce our sense buffer */
1130 if (bt->sense_buffers != NULL) {
1131 hccb->sense_addr = btsensepaddr(bt, bccb);
1133 hccb->sense_addr = vtophys(&csio->sense_data);
1136 * If we have any data to send with this command,
1137 * map it into bus space.
1139 /* Only use S/G if there is a transfer */
1140 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1141 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
1143 * We've been given a pointer
1144 * to a single buffer.
1146 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
1150 error = bus_dmamap_load(
1158 if (error == EINPROGRESS) {
1161 * ordering, freeze the
1163 * until our mapping is
1166 xpt_freeze_simq(bt->sim,
1168 csio->ccb_h.status |=
1173 struct bus_dma_segment seg;
1175 /* Pointer to physical buffer */
1177 (bus_addr_t)csio->data_ptr;
1178 seg.ds_len = csio->dxfer_len;
1179 btexecuteccb(bccb, &seg, 1, 0);
1182 struct bus_dma_segment *segs;
1184 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1185 panic("btaction - Physical "
1189 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1190 panic("btaction - Virtual "
1191 "segment addresses "
1194 /* Just use the segments provided */
1195 segs = (struct bus_dma_segment *)
1197 btexecuteccb(bccb, segs,
1198 csio->sglist_cnt, 0);
1201 btexecuteccb(bccb, NULL, 0, 0);
1204 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1205 /* No data transfer */
1206 hccb->datain = TRUE;
1207 hccb->dataout = TRUE;
1209 hccb->sense_len = 0;
1210 hccb->tag_enable = FALSE;
1212 btexecuteccb(bccb, NULL, 0, 0);
1216 case XPT_EN_LUN: /* Enable LUN as a target */
1217 case XPT_TARGET_IO: /* Execute target I/O request */
1218 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1219 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1220 case XPT_ABORT: /* Abort the specified CCB */
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->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1242 if ((bt->disc_permitted & target_mask) != 0)
1243 cts->flags |= CCB_TRANS_DISC_ENB;
1244 if ((bt->tags_permitted & target_mask) != 0)
1245 cts->flags |= CCB_TRANS_TAG_ENB;
1246 if ((bt->wide_permitted & target_mask) != 0)
1247 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1249 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1250 if ((bt->ultra_permitted & target_mask) != 0)
1251 cts->sync_period = 12;
1252 else if ((bt->fast_permitted & target_mask) != 0)
1253 cts->sync_period = 25;
1254 else if ((bt->sync_permitted & target_mask) != 0)
1255 cts->sync_period = 50;
1257 cts->sync_period = 0;
1259 if (cts->sync_period != 0)
1260 cts->sync_offset = 15;
1262 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1263 | CCB_TRANS_SYNC_OFFSET_VALID
1264 | CCB_TRANS_BUS_WIDTH_VALID
1265 | CCB_TRANS_DISC_VALID
1266 | CCB_TRANS_TQ_VALID;
1268 btfetchtransinfo(bt, cts);
1271 ccb->ccb_h.status = CAM_REQ_CMP;
1275 case XPT_CALC_GEOMETRY:
1277 struct ccb_calc_geometry *ccg;
1279 u_int32_t secs_per_cylinder;
1282 size_mb = ccg->volume_size
1283 / ((1024L * 1024L) / ccg->block_size);
1285 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1286 if (size_mb >= 2048) {
1288 ccg->secs_per_track = 63;
1291 ccg->secs_per_track = 32;
1295 ccg->secs_per_track = 32;
1297 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1298 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1299 ccb->ccb_h.status = CAM_REQ_CMP;
1303 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1305 btreset(bt, /*hardreset*/TRUE);
1306 ccb->ccb_h.status = CAM_REQ_CMP;
1310 case XPT_TERM_IO: /* Terminate the I/O process */
1312 ccb->ccb_h.status = CAM_REQ_INVALID;
1315 case XPT_PATH_INQ: /* Path routing inquiry */
1317 struct ccb_pathinq *cpi = &ccb->cpi;
1319 cpi->version_num = 1; /* XXX??? */
1320 cpi->hba_inquiry = PI_SDTR_ABLE;
1321 if (bt->tag_capable != 0)
1322 cpi->hba_inquiry |= PI_TAG_ABLE;
1323 if (bt->wide_bus != 0)
1324 cpi->hba_inquiry |= PI_WIDE_16;
1325 cpi->target_sprt = 0;
1327 cpi->hba_eng_cnt = 0;
1328 cpi->max_target = bt->wide_bus ? 15 : 7;
1330 cpi->initiator_id = bt->scsi_id;
1331 cpi->bus_id = cam_sim_bus(sim);
1332 cpi->base_transfer_speed = 3300;
1333 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1334 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1335 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1336 cpi->unit_number = cam_sim_unit(sim);
1337 cpi->ccb_h.status = CAM_REQ_CMP;
1342 ccb->ccb_h.status = CAM_REQ_INVALID;
1349 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1351 struct bt_ccb *bccb;
1353 struct bt_softc *bt;
1355 bccb = (struct bt_ccb *)arg;
1357 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1361 device_printf(bt->dev,
1362 "Unexepected error 0x%x returned from "
1363 "bus_dmamap_load\n", error);
1364 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1365 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1366 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1368 btfreeccb(bt, bccb);
1375 bus_dma_segment_t *end_seg;
1376 bus_dmasync_op_t op;
1378 end_seg = dm_segs + nseg;
1380 /* Copy the segments into our SG list */
1382 while (dm_segs < end_seg) {
1383 sg->len = dm_segs->ds_len;
1384 sg->addr = dm_segs->ds_addr;
1390 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1391 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1392 bccb->hccb.data_addr = bccb->sg_list_phys;
1394 bccb->hccb.data_len = bccb->sg_list->len;
1395 bccb->hccb.data_addr = bccb->sg_list->addr;
1398 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1399 op = BUS_DMASYNC_PREREAD;
1401 op = BUS_DMASYNC_PREWRITE;
1403 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1406 bccb->hccb.opcode = INITIATOR_CCB;
1407 bccb->hccb.data_len = 0;
1408 bccb->hccb.data_addr = 0;
1414 * Last time we need to check if this CCB needs to
1417 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1419 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1420 btfreeccb(bt, bccb);
1426 bccb->flags = BCCB_ACTIVE;
1427 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1428 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1430 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
1433 /* Tell the adapter about this command */
1434 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1435 if (bt->cur_outbox->action_code != BMBO_FREE) {
1437 * We should never encounter a busy mailbox.
1438 * If we do, warn the user, and treat it as
1439 * a resource shortage. If the controller is
1440 * hung, one of the pending transactions will
1441 * timeout causing us to start recovery operations.
1443 device_printf(bt->dev,
1444 "Encountered busy mailbox with %d out of %d "
1445 "commands active!!!\n", bt->active_ccbs,
1447 callout_stop(&ccb->ccb_h.timeout_ch);
1449 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1450 btfreeccb(bt, bccb);
1451 bt->resource_shortage = TRUE;
1452 xpt_freeze_simq(bt->sim, /*count*/1);
1453 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1457 bt->cur_outbox->action_code = BMBO_START;
1458 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1466 struct bt_softc *bt;
1469 bt = (struct bt_softc *)arg;
1470 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1472 if ((intstat & CMD_COMPLETE) != 0) {
1473 bt->latched_status = bt_inb(bt, STATUS_REG);
1474 bt->command_cmp = TRUE;
1477 bt_outb(bt, CONTROL_REG, RESET_INTR);
1479 if ((intstat & IMB_LOADED) != 0) {
1480 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1482 btccbptov(bt, bt->cur_inbox->ccb_addr),
1483 bt->cur_inbox->comp_code);
1484 bt->cur_inbox->comp_code = BMBI_FREE;
1489 if ((intstat & SCSI_BUS_RESET) != 0) {
1490 btreset(bt, /*hardreset*/FALSE);
1496 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1499 struct ccb_scsiio *csio;
1502 csio = &bccb->ccb->csio;
1504 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1505 device_printf(bt->dev,
1506 "btdone - Attempt to free non-active BCCB %p\n",
1511 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1512 bus_dmasync_op_t op;
1514 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1515 op = BUS_DMASYNC_POSTREAD;
1517 op = BUS_DMASYNC_POSTWRITE;
1518 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1519 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1522 if (bccb == bt->recovery_bccb) {
1524 * The recovery BCCB does not have a CCB associated
1525 * with it, so short circuit the normal error handling.
1526 * We now traverse our list of pending CCBs and process
1527 * any that were terminated by the recovery CCBs action.
1528 * We also reinstate timeouts for all remaining, pending,
1531 struct cam_path *path;
1532 struct ccb_hdr *ccb_h;
1535 /* Notify all clients that a BDR occured */
1536 error = xpt_create_path(&path, /*periph*/NULL,
1537 cam_sim_path(bt->sim),
1538 bccb->hccb.target_id,
1541 if (error == CAM_REQ_CMP)
1542 xpt_async(AC_SENT_BDR, path, NULL);
1544 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1545 while (ccb_h != NULL) {
1546 struct bt_ccb *pending_bccb;
1548 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1549 if (pending_bccb->hccb.target_id
1550 == bccb->hccb.target_id) {
1551 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1552 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1553 btdone(bt, pending_bccb, BMBI_ERROR);
1555 callout_reset(&ccb_h->timeout_ch,
1556 (ccb_h->timeout * hz) / 1000,
1557 bttimeout, pending_bccb);
1558 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1561 device_printf(bt->dev, "No longer in timeout\n");
1565 callout_stop(&ccb->ccb_h.timeout_ch);
1567 switch (comp_code) {
1569 device_printf(bt->dev,
1570 "btdone - CCB completed with free status!\n");
1572 case BMBI_NOT_FOUND:
1573 device_printf(bt->dev,
1574 "btdone - CCB Abort failed to find CCB\n");
1579 kprintf("bt: ccb %p - error %x occured. "
1580 "btstat = %x, sdstat = %x\n",
1581 (void *)bccb, comp_code, bccb->hccb.btstat,
1584 /* An error occured */
1585 switch(bccb->hccb.btstat) {
1586 case BTSTAT_DATARUN_ERROR:
1587 if (bccb->hccb.data_len == 0) {
1589 * At least firmware 4.22, does this
1590 * for a QUEUE FULL condition.
1592 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1593 } else if (bccb->hccb.data_len < 0) {
1594 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1598 case BTSTAT_NOERROR:
1599 case BTSTAT_LINKED_CMD_COMPLETE:
1600 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1601 case BTSTAT_DATAUNDERUN_ERROR:
1603 csio->scsi_status = bccb->hccb.sdstat;
1604 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1605 switch(csio->scsi_status) {
1606 case SCSI_STATUS_CHECK_COND:
1607 case SCSI_STATUS_CMD_TERMINATED:
1608 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1609 /* Bounce sense back if necessary */
1610 if (bt->sense_buffers != NULL) {
1612 *btsensevaddr(bt, bccb);
1617 case SCSI_STATUS_OK:
1618 csio->ccb_h.status = CAM_REQ_CMP;
1621 csio->resid = bccb->hccb.data_len;
1623 case BTSTAT_SELTIMEOUT:
1624 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1626 case BTSTAT_UNEXPECTED_BUSFREE:
1627 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1629 case BTSTAT_INVALID_PHASE:
1630 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1632 case BTSTAT_INVALID_ACTION_CODE:
1633 panic("%s: Inavlid Action code", bt_name(bt));
1635 case BTSTAT_INVALID_OPCODE:
1636 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1638 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1639 /* We don't even support linked commands... */
1640 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1642 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1643 panic("%s: Invalid CCB or SG list", bt_name(bt));
1645 case BTSTAT_AUTOSENSE_FAILED:
1646 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1648 case BTSTAT_TAGGED_MSG_REJECTED:
1650 struct ccb_trans_settings neg;
1652 xpt_print_path(csio->ccb_h.path);
1653 kprintf("refuses tagged commands. Performing "
1654 "non-tagged I/O\n");
1656 neg.valid = CCB_TRANS_TQ_VALID;
1657 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1659 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1660 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1661 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1664 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1666 * XXX You would think that this is
1667 * a recoverable error... Hmmm.
1669 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1671 case BTSTAT_HA_SOFTWARE_ERROR:
1672 case BTSTAT_HA_WATCHDOG_ERROR:
1673 case BTSTAT_HARDWARE_FAILURE:
1674 /* Hardware reset ??? Can we recover ??? */
1675 csio->ccb_h.status = CAM_NO_HBA;
1677 case BTSTAT_TARGET_IGNORED_ATN:
1678 case BTSTAT_OTHER_SCSI_BUS_RESET:
1679 case BTSTAT_HA_SCSI_BUS_RESET:
1680 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1682 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1685 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1686 csio->ccb_h.status = CAM_BDR_SENT;
1688 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1690 case BTSTAT_INVALID_RECONNECT:
1691 case BTSTAT_ABORT_QUEUE_GENERATED:
1692 csio->ccb_h.status = CAM_REQ_TERMIO;
1694 case BTSTAT_SCSI_PERROR_DETECTED:
1695 csio->ccb_h.status = CAM_UNCOR_PARITY;
1698 if (csio->ccb_h.status != CAM_REQ_CMP) {
1699 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1700 csio->ccb_h.status |= CAM_DEV_QFRZN;
1702 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1703 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1704 btfreeccb(bt, bccb);
1708 /* All completed without incident */
1709 ccb->ccb_h.status |= CAM_REQ_CMP;
1710 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1711 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1712 btfreeccb(bt, bccb);
1719 btreset(struct bt_softc* bt, int hard_reset)
1721 struct ccb_hdr *ccb_h;
1724 u_int8_t reset_type;
1726 if (hard_reset != 0)
1727 reset_type = HARD_RESET;
1729 reset_type = SOFT_RESET;
1730 bt_outb(bt, CONTROL_REG, reset_type);
1732 /* Wait 5sec. for Diagnostic start */
1733 timeout = 5 * 10000;
1735 status = bt_inb(bt, STATUS_REG);
1736 if ((status & DIAG_ACTIVE) != 0)
1742 kprintf("%s: btreset - Diagnostic Active failed to "
1743 "assert. status = 0x%x\n", bt_name(bt), status);
1747 /* Wait 10sec. for Diagnostic end */
1748 timeout = 10 * 10000;
1750 status = bt_inb(bt, STATUS_REG);
1751 if ((status & DIAG_ACTIVE) == 0)
1756 panic("%s: btreset - Diagnostic Active failed to drop. "
1757 "status = 0x%x\n", bt_name(bt), status);
1761 /* Wait for the host adapter to become ready or report a failure */
1764 status = bt_inb(bt, STATUS_REG);
1765 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1770 kprintf("%s: btreset - Host adapter failed to come ready. "
1771 "status = 0x%x\n", bt_name(bt), status);
1775 /* If the diagnostics failed, tell the user */
1776 if ((status & DIAG_FAIL) != 0
1777 || (status & HA_READY) == 0) {
1778 kprintf("%s: btreset - Adapter failed diagnostics\n",
1781 if ((status & DATAIN_REG_READY) != 0)
1782 kprintf("%s: btreset - Host Adapter Error code = 0x%x\n",
1783 bt_name(bt), bt_inb(bt, DATAIN_REG));
1787 /* If we've allocated mailboxes, initialize them */
1788 if (bt->init_level > 4)
1791 /* If we've attached to the XPT, tell it about the event */
1792 if (bt->path != NULL)
1793 xpt_async(AC_BUS_RESET, bt->path, NULL);
1796 * Perform completion processing for all outstanding CCBs.
1798 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1799 struct bt_ccb *pending_bccb;
1801 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1802 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1803 btdone(bt, pending_bccb, BMBI_ERROR);
1810 * Send a command to the adapter.
1813 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1814 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1820 u_int reply_buf_size;
1824 /* No data returned to start */
1825 reply_buf_size = reply_len;
1832 bt->command_cmp = 0;
1834 * Wait up to 10 sec. for the adapter to become
1835 * ready to accept commands.
1839 status = bt_inb(bt, STATUS_REG);
1840 if ((status & HA_READY) != 0
1841 && (status & CMD_REG_BUSY) == 0)
1844 * Throw away any pending data which may be
1845 * left over from earlier commands that we
1848 if ((status & DATAIN_REG_READY) != 0)
1849 (void)bt_inb(bt, DATAIN_REG);
1853 kprintf("%s: bt_cmd: Timeout waiting for adapter ready, "
1854 "status = 0x%x\n", bt_name(bt), status);
1859 * Send the opcode followed by any necessary parameter bytes.
1861 bt_outb(bt, COMMAND_REG, opcode);
1864 * Wait for up to 1sec for each byte of the the
1865 * parameter list sent to be sent.
1868 while (param_len && --timeout) {
1871 status = bt_inb(bt, STATUS_REG);
1872 intstat = bt_inb(bt, INTSTAT_REG);
1875 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1876 == (INTR_PENDING|CMD_COMPLETE)) {
1877 saved_status = status;
1881 if (bt->command_cmp != 0) {
1882 saved_status = bt->latched_status;
1886 if ((status & DATAIN_REG_READY) != 0)
1888 if ((status & CMD_REG_BUSY) == 0) {
1889 bt_outb(bt, COMMAND_REG, *params++);
1895 kprintf("%s: bt_cmd: Timeout sending parameters, "
1896 "status = 0x%x\n", bt_name(bt), status);
1898 saved_status = status;
1903 * Wait for the command to complete.
1905 while (cmd_complete == 0 && --cmd_timeout) {
1908 status = bt_inb(bt, STATUS_REG);
1909 intstat = bt_inb(bt, INTSTAT_REG);
1911 * It may be that this command was issued with
1912 * controller interrupts disabled. We'll never
1913 * get to our command if an incoming mailbox
1914 * interrupt is pending, so take care of completed
1915 * mailbox commands by calling our interrupt handler.
1917 if ((intstat & (INTR_PENDING|IMB_LOADED))
1918 == (INTR_PENDING|IMB_LOADED))
1922 if (bt->command_cmp != 0) {
1924 * Our interrupt handler saw CMD_COMPLETE
1925 * status before we did.
1928 saved_status = bt->latched_status;
1929 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1930 == (INTR_PENDING|CMD_COMPLETE)) {
1932 * Our poll (in case interrupts are blocked)
1933 * saw the CMD_COMPLETE interrupt.
1936 saved_status = status;
1937 } else if (opcode == BOP_MODIFY_IO_ADDR
1938 && (status & CMD_REG_BUSY) == 0) {
1940 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1941 * but it should update the status register. So, we
1942 * consider this command complete when the CMD_REG_BUSY
1945 saved_status = status;
1947 } else if ((status & DATAIN_REG_READY) != 0) {
1950 data = bt_inb(bt, DATAIN_REG);
1951 if (reply_len < reply_buf_size) {
1952 *reply_data++ = data;
1954 kprintf("%s: bt_cmd - Discarded reply data byte "
1955 "for opcode 0x%x\n", bt_name(bt),
1959 * Reset timeout to ensure at least a second
1960 * between response bytes.
1962 cmd_timeout = MAX(cmd_timeout, 10000);
1965 } else if ((opcode == BOP_FETCH_LRAM)
1966 && (status & HA_READY) != 0) {
1967 saved_status = status;
1972 if (cmd_timeout == 0) {
1973 kprintf("%s: bt_cmd: Timeout waiting for command (%x) "
1974 "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
1975 "rlen %d\n", bt_name(bt), opcode,
1976 bt_name(bt), status, intstat, reply_len);
1977 error = (ETIMEDOUT);
1981 * Clear any pending interrupts. Block interrupts so our
1982 * interrupt handler is not re-entered.
1992 * If the command was rejected by the controller, tell the caller.
1994 if ((saved_status & CMD_INVALID) != 0) {
1996 * Some early adapters may not recover properly from
1997 * an invalid command. If it appears that the controller
1998 * has wedged (i.e. status was not cleared by our interrupt
1999 * reset above), perform a soft reset.
2002 kprintf("%s: Invalid Command 0x%x\n", bt_name(bt),
2005 status = bt_inb(bt, STATUS_REG);
2006 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2007 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2008 || (status & (HA_READY|INIT_REQUIRED))
2009 != (HA_READY|INIT_REQUIRED)) {
2010 btreset(bt, /*hard_reset*/FALSE);
2015 if (param_len > 0) {
2016 /* The controller did not accept the full argument list */
2020 if (reply_len != reply_buf_size) {
2021 /* Too much or too little data received */
2025 /* We were successful */
2030 btinitmboxes(struct bt_softc *bt) {
2031 init_32b_mbox_params_t init_mbox;
2034 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2035 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2036 bt->cur_inbox = bt->in_boxes;
2037 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2038 bt->cur_outbox = bt->out_boxes;
2039 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2041 /* Tell the adapter about them */
2042 init_mbox.num_boxes = bt->num_boxes;
2043 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2044 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2045 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2046 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2047 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2048 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2049 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2052 kprintf("btinitmboxes: Initialization command failed\n");
2053 else if (bt->strict_rr != 0) {
2055 * If the controller supports
2056 * strict round robin mode,
2062 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1,
2063 /*reply_buf*/NULL, /*reply_len*/0,
2064 DEFAULT_CMD_TIMEOUT);
2067 kprintf("btinitmboxes: Unable to enable strict RR\n");
2069 } else if (bootverbose) {
2070 kprintf("%s: Using Strict Round Robin Mailbox Mode\n",
2079 * Update the XPT's idea of the negotiated transfer
2080 * parameters for a particular target.
2083 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings* cts)
2085 setup_data_t setup_info;
2092 targ_syncinfo_t sync_info;
2094 target = cts->ccb_h.target_id;
2095 targ_offset = (target & 0x7);
2096 targ_mask = (0x01 << targ_offset);
2099 * Inquire Setup Information. This command retreives the
2100 * Wide negotiation status for recent adapters as well as
2101 * the sync info for older models.
2103 param = sizeof(setup_info);
2104 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
2105 (u_int8_t*)&setup_info, sizeof(setup_info),
2106 DEFAULT_CMD_TIMEOUT);
2109 kprintf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
2110 bt_name(bt), error);
2115 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2116 : setup_info.high_syncinfo[targ_offset];
2118 if (sync_info.sync == 0)
2119 cts->sync_offset = 0;
2121 cts->sync_offset = sync_info.offset;
2123 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2124 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2128 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2129 : (setup_info.high_wide_active & targ_mask);
2132 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2133 } else if ((bt->wide_permitted & targ_mask) != 0) {
2134 struct ccb_getdev cgd;
2137 * Prior to rev 5.06L, wide status isn't provided,
2138 * so we "guess" that wide transfers are in effect
2139 * if the user settings allow for wide and the inquiry
2140 * data for the device indicates that it can handle
2143 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2144 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2145 xpt_action((union ccb *)&cgd);
2146 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2147 && (cgd.inq_data.flags & SID_WBus16) != 0)
2148 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2151 if (bt->firmware_ver[0] >= '3') {
2153 * For adapters that can do fast or ultra speeds,
2154 * use the more exact Target Sync Information command.
2156 target_sync_info_data_t sync_info;
2158 param = sizeof(sync_info);
2159 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1,
2160 (u_int8_t*)&sync_info, sizeof(sync_info),
2161 DEFAULT_CMD_TIMEOUT);
2164 kprintf("%s: btfetchtransinfo - Inquire Sync "
2165 "Info Failed 0x%x\n", bt_name(bt), error);
2169 sync_period = sync_info.sync_rate[target] * 100;
2171 sync_period = 2000 + (500 * sync_info.period);
2174 /* Convert ns value to standard SCSI sync rate */
2175 if (cts->sync_offset != 0)
2176 cts->sync_period = scsi_calc_syncparam(sync_period);
2178 cts->sync_period = 0;
2180 cts->valid = CCB_TRANS_SYNC_RATE_VALID
2181 | CCB_TRANS_SYNC_OFFSET_VALID
2182 | CCB_TRANS_BUS_WIDTH_VALID;
2183 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2187 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2189 struct bt_softc* bt;
2191 bt = (struct bt_softc*)arg;
2192 bt->mailbox_physbase = segs->ds_addr;
2196 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2198 struct bt_softc* bt;
2200 bt = (struct bt_softc*)arg;
2201 bt->bt_ccb_physbase = segs->ds_addr;
2205 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2208 struct bt_softc* bt;
2210 bt = (struct bt_softc*)arg;
2211 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2215 btpoll(struct cam_sim *sim)
2217 bt_intr(cam_sim_softc(sim));
2221 bttimeout(void *arg)
2223 struct bt_ccb *bccb;
2225 struct bt_softc *bt;
2227 bccb = (struct bt_ccb *)arg;
2229 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2230 xpt_print_path(ccb->ccb_h.path);
2231 kprintf("CCB %p - timed out\n", (void *)bccb);
2235 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2236 xpt_print_path(ccb->ccb_h.path);
2237 kprintf("CCB %p - timed out CCB already completed\n",
2244 * In order to simplify the recovery process, we ask the XPT
2245 * layer to halt the queue of new transactions and we traverse
2246 * the list of pending CCBs and remove their timeouts. This
2247 * means that the driver attempts to clear only one error
2248 * condition at a time. In general, timeouts that occur
2249 * close together are related anyway, so there is no benefit
2250 * in attempting to handle errors in parrallel. Timeouts will
2251 * be reinstated when the recovery process ends.
2253 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2254 struct ccb_hdr *ccb_h;
2256 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2257 xpt_freeze_simq(bt->sim, /*count*/1);
2258 bccb->flags |= BCCB_RELEASE_SIMQ;
2261 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2262 while (ccb_h != NULL) {
2263 struct bt_ccb *pending_bccb;
2265 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2266 callout_stop(&ccb_h->timeout_ch);
2267 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2271 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2272 || bt->cur_outbox->action_code != BMBO_FREE
2273 || ((bccb->hccb.tag_enable == TRUE)
2274 && (bt->firmware_ver[0] < '5'))) {
2276 * Try a full host adapter/SCSI bus reset.
2277 * We do this only if we have already attempted
2278 * to clear the condition with a BDR, or we cannot
2279 * attempt a BDR for lack of mailbox resources
2280 * or because of faulty firmware. It turns out
2281 * that firmware versions prior to 5.xx treat BDRs
2282 * as untagged commands that cannot be sent until
2283 * all outstanding tagged commands have been processed.
2284 * This makes it somewhat difficult to use a BDR to
2285 * clear up a problem with an uncompleted tagged command.
2287 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2288 btreset(bt, /*hardreset*/TRUE);
2289 kprintf("%s: No longer in timeout\n", bt_name(bt));
2292 * Send a Bus Device Reset message:
2293 * The target that is holding up the bus may not
2294 * be the same as the one that triggered this timeout
2295 * (different commands have different timeout lengths),
2296 * but we have no way of determining this from our
2297 * timeout handler. Our strategy here is to queue a
2298 * BDR message to the target of the timed out command.
2299 * If this fails, we'll get another timeout 2 seconds
2300 * later which will attempt a bus reset.
2302 bccb->flags |= BCCB_DEVICE_RESET;
2303 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, bttimeout, bccb);
2305 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2307 /* No Data Transfer */
2308 bt->recovery_bccb->hccb.datain = TRUE;
2309 bt->recovery_bccb->hccb.dataout = TRUE;
2310 bt->recovery_bccb->hccb.btstat = 0;
2311 bt->recovery_bccb->hccb.sdstat = 0;
2312 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2314 /* Tell the adapter about this command */
2315 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2316 bt->cur_outbox->action_code = BMBO_START;
2317 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);