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_pci.c BT-946, BT-948, BT-956, BT-958 cards
7 * Copyright (c) 1998, 1999 Justin T. Gibbs.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/sys/dev/buslogic/bt.c,v 1.25.2.1 2000/08/02 22:32:26 peter Exp $
35 * Special thanks to Leonard N. Zubkoff for writing such a complete and
36 * well documented Mylex/BusLogic MultiMaster driver for Linux. Support
37 * in this driver for the wide range of MultiMaster controllers and
38 * firmware revisions, with their otherwise undocumented quirks, would not
39 * have been possible without his efforts.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
52 #include <machine/clock.h>
54 #include <bus/cam/cam.h>
55 #include <bus/cam/cam_ccb.h>
56 #include <bus/cam/cam_sim.h>
57 #include <bus/cam/cam_xpt_sim.h>
58 #include <bus/cam/cam_debug.h>
59 #include <bus/cam/scsi/scsi_message.h>
66 /* MailBox Management functions */
67 static __inline void btnextinbox(struct bt_softc *bt);
68 static __inline void btnextoutbox(struct bt_softc *bt);
71 btnextinbox(struct bt_softc *bt)
73 if (bt->cur_inbox == bt->last_inbox)
74 bt->cur_inbox = bt->in_boxes;
80 btnextoutbox(struct bt_softc *bt)
82 if (bt->cur_outbox == bt->last_outbox)
83 bt->cur_outbox = bt->out_boxes;
88 /* CCB Mangement functions */
89 static __inline u_int32_t btccbvtop(struct bt_softc *bt,
91 static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
93 static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
95 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
98 static __inline u_int32_t
99 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
101 return (bt->bt_ccb_physbase
102 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
105 static __inline struct bt_ccb *
106 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
108 return (bt->bt_ccb_array +
109 ((struct bt_ccb*)(uintptr_t)ccb_addr-(struct bt_ccb*)(uintptr_t)bt->bt_ccb_physbase));
112 static __inline u_int32_t
113 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
117 index = (u_int)(bccb - bt->bt_ccb_array);
118 return (bt->sense_buffers_physbase
119 + (index * sizeof(struct scsi_sense_data)));
122 static __inline struct scsi_sense_data *
123 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
127 index = (u_int)(bccb - bt->bt_ccb_array);
128 return (bt->sense_buffers + index);
131 static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
132 static __inline void btfreeccb(struct bt_softc *bt,
133 struct bt_ccb *bccb);
134 static void btallocccbs(struct bt_softc *bt);
135 static bus_dmamap_callback_t btexecuteccb;
136 static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
137 bt_mbi_comp_code_t comp_code);
139 /* Host adapter command functions */
140 static int btreset(struct bt_softc* bt, int hard_reset);
142 /* Initialization functions */
143 static int btinitmboxes(struct bt_softc *bt);
144 static bus_dmamap_callback_t btmapmboxes;
145 static bus_dmamap_callback_t btmapccbs;
146 static bus_dmamap_callback_t btmapsgs;
148 /* Transfer Negotiation Functions */
149 static void btfetchtransinfo(struct bt_softc *bt,
150 struct ccb_trans_settings *cts);
152 /* CAM SIM entry points */
153 #define ccb_bccb_ptr spriv_ptr0
154 #define ccb_bt_ptr spriv_ptr1
155 static void btaction(struct cam_sim *sim, union ccb *ccb);
156 static void btpoll(struct cam_sim *sim);
158 /* Our timeout handler */
165 * Do our own re-probe protection until a configuration
166 * manager can do it for us. This ensures that we don't
167 * reprobe a card already found by the PCI probes.
169 struct bt_isa_port bt_isa_ports[] =
180 * I/O ports listed in the order enumerated by the
181 * card for certain op codes.
183 u_int16_t bt_board_ports[] =
193 /* Exported functions */
195 bt_init_softc(device_t dev, struct resource *port,
196 struct resource *irq, struct resource *drq)
198 struct bt_softc *bt = device_get_softc(dev);
200 SLIST_INIT(&bt->free_bt_ccbs);
201 LIST_INIT(&bt->pending_ccbs);
202 SLIST_INIT(&bt->sg_maps);
204 bt->unit = device_get_unit(dev);
208 bt->tag = rman_get_bustag(port);
209 bt->bsh = rman_get_bushandle(port);
213 bt_free_softc(device_t dev)
215 struct bt_softc *bt = device_get_softc(dev);
217 switch (bt->init_level) {
220 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
222 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
225 bus_dma_tag_destroy(bt->sense_dmat);
228 struct sg_map_node *sg_map;
230 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
231 SLIST_REMOVE_HEAD(&bt->sg_maps, links);
232 bus_dmamap_unload(bt->sg_dmat,
234 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
236 kfree(sg_map, M_DEVBUF);
238 bus_dma_tag_destroy(bt->sg_dmat);
241 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
243 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
245 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
247 bus_dma_tag_destroy(bt->ccb_dmat);
249 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
251 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
253 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
255 bus_dma_tag_destroy(bt->buffer_dmat);
257 bus_dma_tag_destroy(bt->mailbox_dmat);
264 bt_port_probe(device_t dev, struct bt_probe_info *info)
266 struct bt_softc *bt = device_get_softc(dev);
267 config_data_t config_data;
270 /* See if there is really a card present */
271 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
275 * Determine our IRQ, and DMA settings and
276 * export them to the configuration system.
278 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
279 (u_int8_t*)&config_data, sizeof(config_data),
280 DEFAULT_CMD_TIMEOUT);
282 kprintf("bt_port_probe: Could not determine IRQ or DMA "
283 "settings for adapter.\n");
287 if (bt->model[0] == '5') {
288 /* DMA settings only make sense for ISA cards */
289 switch (config_data.dma_chan) {
300 kprintf("bt_port_probe: Invalid DMA setting "
301 "detected for adapter.\n");
305 /* VL/EISA/PCI DMA */
308 switch (config_data.irq) {
315 info->irq = ffs(config_data.irq) + 8;
318 kprintf("bt_port_probe: Invalid IRQ setting %x"
319 "detected for adapter.\n", config_data.irq);
326 * Probe the adapter and verify that the card is a BusLogic.
329 bt_probe(device_t dev)
331 struct bt_softc *bt = device_get_softc(dev);
332 esetup_info_data_t esetup_info;
340 * See if the three I/O ports look reasonable.
341 * Touch the minimal number of registers in the
344 status = bt_inb(bt, STATUS_REG);
346 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
347 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
349 device_printf(dev, "Failed Status Reg Test - %x\n",
354 intstat = bt_inb(bt, INTSTAT_REG);
355 if ((intstat & INTSTAT_REG_RSVD) != 0) {
356 device_printf(dev, "Failed Intstat Reg Test\n");
360 geometry = bt_inb(bt, GEOMETRY_REG);
361 if (geometry == 0xFF) {
363 device_printf(dev, "Failed Geometry Reg Test\n");
368 * Looking good so far. Final test is to reset the
369 * adapter and attempt to fetch the extended setup
370 * information. This should filter out all 1542 cards.
372 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
374 device_printf(dev, "Failed Reset\n");
378 param = sizeof(esetup_info);
379 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, ¶m, /*parmlen*/1,
380 (u_int8_t*)&esetup_info, sizeof(esetup_info),
381 DEFAULT_CMD_TIMEOUT);
390 * Pull the boards setup information and record it in our softc.
393 bt_fetch_adapter_info(device_t dev)
395 struct bt_softc *bt = device_get_softc(dev);
396 board_id_data_t board_id;
397 esetup_info_data_t esetup_info;
398 config_data_t config_data;
400 u_int8_t length_param;
402 /* First record the firmware version */
403 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
404 (u_int8_t*)&board_id, sizeof(board_id),
405 DEFAULT_CMD_TIMEOUT);
407 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
410 bt->firmware_ver[0] = board_id.firmware_rev_major;
411 bt->firmware_ver[1] = '.';
412 bt->firmware_ver[2] = board_id.firmware_rev_minor;
413 bt->firmware_ver[3] = '\0';
416 * Depending on the firmware major and minor version,
417 * we may be able to fetch additional minor version info.
419 if (bt->firmware_ver[0] > '0') {
421 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
422 (u_int8_t*)&bt->firmware_ver[3], 1,
423 DEFAULT_CMD_TIMEOUT);
426 "bt_fetch_adapter_info - Failed Get "
427 "Firmware 3rd Digit\n");
430 if (bt->firmware_ver[3] == ' ')
431 bt->firmware_ver[3] = '\0';
432 bt->firmware_ver[4] = '\0';
435 if (strcmp(bt->firmware_ver, "3.3") >= 0) {
437 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
438 (u_int8_t*)&bt->firmware_ver[4], 1,
439 DEFAULT_CMD_TIMEOUT);
442 "bt_fetch_adapter_info - Failed Get "
443 "Firmware 4th Digit\n");
446 if (bt->firmware_ver[4] == ' ')
447 bt->firmware_ver[4] = '\0';
448 bt->firmware_ver[5] = '\0';
452 * Some boards do not handle the "recently documented"
453 * Inquire Board Model Number command correctly or do not give
454 * exact information. Use the Firmware and Extended Setup
455 * information in these cases to come up with the right answer.
456 * The major firmware revision number indicates:
458 * 5.xx BusLogic "W" Series Host Adapters:
460 * 4.xx BusLogic "C" Series Host Adapters:
461 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
462 * 3.xx BusLogic "S" Series Host Adapters:
463 * BT-747S/747D/757S/757D/445S/545S/542D
464 * BT-542B/742A (revision H)
465 * 2.xx BusLogic "A" Series Host Adapters:
466 * BT-542B/742A (revision G and below)
468 length_param = sizeof(esetup_info);
469 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
470 (u_int8_t*)&esetup_info, sizeof(esetup_info),
471 DEFAULT_CMD_TIMEOUT);
476 bt->bios_addr = esetup_info.bios_addr << 12;
478 if (esetup_info.bus_type == 'A'
479 && bt->firmware_ver[0] == '2') {
480 ksnprintf(bt->model, sizeof(bt->model), "542B");
481 } else if (esetup_info.bus_type == 'E'
482 && (strncmp(bt->firmware_ver, "2.1", 3) == 0
483 || strncmp(bt->firmware_ver, "2.20", 4) == 0)) {
484 ksnprintf(bt->model, sizeof(bt->model), "742A");
486 ha_model_data_t model_data;
489 length_param = sizeof(model_data);
490 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
491 (u_int8_t*)&model_data, sizeof(model_data),
492 DEFAULT_CMD_TIMEOUT);
495 "bt_fetch_adapter_info - Failed Inquire "
499 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
500 bt->model[i] = model_data.ascii_model[i];
501 if (bt->model[i] == ' ')
507 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
509 /* SG element limits */
510 bt->max_sg = esetup_info.max_sg;
512 /* Set feature flags */
513 bt->wide_bus = esetup_info.wide_bus;
514 bt->diff_bus = esetup_info.diff_bus;
515 bt->ultra_scsi = esetup_info.ultra_scsi;
517 if ((bt->firmware_ver[0] == '5')
518 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
519 bt->extended_lun = TRUE;
521 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
524 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
527 * Determine max CCB count and whether tagged queuing is
528 * available based on controller type. Tagged queuing
529 * only works on 'W' series adapters, 'C' series adapters
530 * with firmware of rev 4.42 and higher, and 'S' series
531 * adapters with firmware of rev 3.35 and higher. The
532 * maximum CCB counts are as follows:
534 * 192 BT-948/958/958D
535 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
537 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
539 if (bt->firmware_ver[0] == '5') {
541 bt->tag_capable = TRUE;
542 } else if (bt->firmware_ver[0] == '4') {
543 if (bt->model[0] == '5')
547 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
550 if (bt->firmware_ver[0] == '3'
551 && (strcmp(bt->firmware_ver, "3.35") >= 0))
552 bt->tag_capable = TRUE;
554 bt->tag_capable = FALSE;
557 if (bt->tag_capable != FALSE)
558 bt->tags_permitted = ALL_TARGETS;
560 /* Determine Sync/Wide/Disc settings */
561 if (bt->firmware_ver[0] >= '4') {
562 auto_scsi_data_t auto_scsi_data;
563 fetch_lram_params_t fetch_lram_params;
567 * These settings are stored in the
568 * AutoSCSI data in LRAM of 'W' and 'C'
571 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
572 fetch_lram_params.response_len = sizeof(auto_scsi_data);
573 error = bt_cmd(bt, BOP_FETCH_LRAM,
574 (u_int8_t*)&fetch_lram_params,
575 sizeof(fetch_lram_params),
576 (u_int8_t*)&auto_scsi_data,
577 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
581 "bt_fetch_adapter_info - Failed "
582 "Get Auto SCSI Info\n");
586 bt->disc_permitted = auto_scsi_data.low_disc_permitted
587 | (auto_scsi_data.high_disc_permitted << 8);
588 bt->sync_permitted = auto_scsi_data.low_sync_permitted
589 | (auto_scsi_data.high_sync_permitted << 8);
590 bt->fast_permitted = auto_scsi_data.low_fast_permitted
591 | (auto_scsi_data.high_fast_permitted << 8);
592 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
593 | (auto_scsi_data.high_ultra_permitted << 8);
594 bt->wide_permitted = auto_scsi_data.low_wide_permitted
595 | (auto_scsi_data.high_wide_permitted << 8);
597 if (bt->ultra_scsi == FALSE)
598 bt->ultra_permitted = 0;
600 if (bt->wide_bus == FALSE)
601 bt->wide_permitted = 0;
604 * 'S' and 'A' series have this information in the setup
605 * information structure.
607 setup_data_t setup_info;
609 length_param = sizeof(setup_info);
610 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
611 /*paramlen*/1, (u_int8_t*)&setup_info,
612 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
616 "bt_fetch_adapter_info - Failed "
621 if (setup_info.initiate_sync != 0) {
622 bt->sync_permitted = ALL_TARGETS;
624 if (bt->model[0] == '7') {
625 if (esetup_info.sync_neg10MB != 0)
626 bt->fast_permitted = ALL_TARGETS;
627 if (strcmp(bt->model, "757") == 0)
628 bt->wide_permitted = ALL_TARGETS;
631 bt->disc_permitted = ALL_TARGETS;
634 /* We need as many mailboxes as we can have ccbs */
635 bt->num_boxes = bt->max_ccbs;
637 /* Determine our SCSI ID */
639 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
640 (u_int8_t*)&config_data, sizeof(config_data),
641 DEFAULT_CMD_TIMEOUT);
644 "bt_fetch_adapter_info - Failed Get Config\n");
647 bt->scsi_id = config_data.scsi_id;
653 * Start the board, ready for normal operation
656 bt_init(device_t dev)
658 struct bt_softc *bt = device_get_softc(dev);
660 /* Announce the Adapter */
661 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
663 if (bt->ultra_scsi != 0)
666 if (bt->wide_bus != 0)
671 if (bt->diff_bus != 0)
674 kprintf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
678 * Create our DMA tags. These tags define the kinds of device
679 * accessible memory allocations and memory mappings we will
680 * need to perform during normal operation.
682 * Unless we need to further restrict the allocation, we rely
683 * on the restrictions of the parent dmat, hence the common
684 * use of MAXADDR and MAXSIZE.
687 /* DMA tag for mapping buffers into device visible space. */
688 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
689 /*lowaddr*/BUS_SPACE_MAXADDR,
690 /*highaddr*/BUS_SPACE_MAXADDR,
691 /*filter*/NULL, /*filterarg*/NULL,
692 /*maxsize*/MAXBSIZE, /*nsegments*/BT_NSEG,
693 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
694 /*flags*/BUS_DMA_ALLOCNOW,
695 &bt->buffer_dmat) != 0) {
700 /* DMA tag for our mailboxes */
701 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
702 /*lowaddr*/BUS_SPACE_MAXADDR,
703 /*highaddr*/BUS_SPACE_MAXADDR,
704 /*filter*/NULL, /*filterarg*/NULL,
705 bt->num_boxes * (sizeof(bt_mbox_in_t)
706 + sizeof(bt_mbox_out_t)),
708 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
709 /*flags*/0, &bt->mailbox_dmat) != 0) {
715 /* Allocation for our mailboxes */
716 if (bus_dmamem_alloc(bt->mailbox_dmat, (void *)&bt->out_boxes,
717 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
723 /* And permanently map them */
724 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
726 bt->num_boxes * (sizeof(bt_mbox_in_t)
727 + sizeof(bt_mbox_out_t)),
728 btmapmboxes, bt, /*flags*/0);
732 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
736 /* DMA tag for our ccb structures */
737 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
738 /*lowaddr*/BUS_SPACE_MAXADDR,
739 /*highaddr*/BUS_SPACE_MAXADDR,
740 /*filter*/NULL, /*filterarg*/NULL,
741 bt->max_ccbs * sizeof(struct bt_ccb),
743 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
744 /*flags*/0, &bt->ccb_dmat) != 0) {
750 /* Allocation for our ccbs */
751 if (bus_dmamem_alloc(bt->ccb_dmat, (void *)&bt->bt_ccb_array,
752 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
758 /* And permanently map them */
759 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
761 bt->max_ccbs * sizeof(struct bt_ccb),
762 btmapccbs, bt, /*flags*/0);
766 /* DMA tag for our S/G structures. We allocate in page sized chunks */
767 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
768 /*lowaddr*/BUS_SPACE_MAXADDR,
769 /*highaddr*/BUS_SPACE_MAXADDR,
770 /*filter*/NULL, /*filterarg*/NULL,
771 PAGE_SIZE, /*nsegments*/1,
772 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
773 /*flags*/0, &bt->sg_dmat) != 0) {
779 /* Perform initial CCB allocation */
780 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
783 if (bt->num_ccbs == 0) {
785 "bt_init - Unable to allocate initial ccbs\n");
790 * Note that we are going and return (to probe)
800 bt_attach(device_t dev)
802 struct bt_softc *bt = device_get_softc(dev);
803 int tagged_dev_openings;
804 struct cam_devq *devq;
808 * We reserve 1 ccb for error recovery, so don't
809 * tell the XPT about it.
811 if (bt->tag_capable != 0)
812 tagged_dev_openings = bt->max_ccbs - 1;
814 tagged_dev_openings = 0;
817 * Create the device queue for our SIM.
819 devq = cam_simq_alloc(bt->max_ccbs - 1);
824 * Construct our SIM entry
826 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
827 &sim_mplock, 2, tagged_dev_openings, devq);
828 cam_simq_release(devq);
832 if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
833 cam_sim_free(bt->sim);
837 if (xpt_create_path(&bt->path, /*periph*/NULL,
838 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
839 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
840 xpt_bus_deregister(cam_sim_path(bt->sim));
841 cam_sim_free(bt->sim);
848 error = bus_setup_intr(dev, bt->irq, 0,
849 bt_intr, bt, &bt->ih, NULL);
851 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
859 bt_check_probed_iop(u_int ioport)
863 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
864 if (bt_isa_ports[i].addr == ioport) {
865 if (bt_isa_ports[i].probed != 0)
876 bt_mark_probed_bio(isa_compat_io_t port)
878 if (port < BIO_DISABLED)
879 bt_mark_probed_iop(bt_board_ports[port]);
883 bt_mark_probed_iop(u_int ioport)
887 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
888 if (ioport == bt_isa_ports[i].addr) {
889 bt_isa_ports[i].probed = 1;
896 bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
901 for (i = 0;i < BT_NUM_ISAPORTS; i++)
902 if (ioport <= bt_isa_ports[i].addr)
904 if ((i >= BT_NUM_ISAPORTS)
905 || (ioport != bt_isa_ports[i].addr)) {
906 kprintf("\nbt_isa_probe: Invalid baseport of 0x%x specified.\n"
907 "bt_isa_probe: Nearest valid baseport is 0x%x.\n"
908 "bt_isa_probe: Failing probe.\n",
910 (i < BT_NUM_ISAPORTS)
911 ? bt_isa_ports[i].addr
912 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
913 *port_index = *max_port_index = -1;
916 *port_index = *max_port_index = bt_isa_ports[i].bio;
919 *max_port_index = BT_NUM_ISAPORTS - 1;
924 bt_iop_from_bio(isa_compat_io_t bio_index)
926 if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
927 return (bt_board_ports[bio_index]);
933 btallocccbs(struct bt_softc *bt)
935 struct bt_ccb *next_ccb;
936 struct sg_map_node *sg_map;
942 if (bt->num_ccbs >= bt->max_ccbs)
943 /* Can't allocate any more */
946 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
948 sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_WAITOK);
950 /* Allocate S/G space for the next batch of CCBS */
951 if (bus_dmamem_alloc(bt->sg_dmat, (void *)&sg_map->sg_vaddr,
952 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
953 kfree(sg_map, M_DEVBUF);
957 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
959 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
960 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
962 segs = sg_map->sg_vaddr;
963 physaddr = sg_map->sg_physaddr;
965 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
966 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
969 next_ccb->sg_list = segs;
970 next_ccb->sg_list_phys = physaddr;
971 next_ccb->flags = BCCB_FREE;
972 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
976 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
978 physaddr += (BT_NSEG * sizeof(bt_sg_t));
983 /* Reserve a CCB for error recovery */
984 if (bt->recovery_bccb == NULL) {
985 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
986 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
989 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
993 device_printf(bt->dev, "Can't malloc BCCBs\n");
997 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1000 if ((bccb->flags & BCCB_ACTIVE) != 0)
1001 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1002 if (bt->resource_shortage != 0
1003 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1004 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1005 bt->resource_shortage = FALSE;
1007 bccb->flags = BCCB_FREE;
1008 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1013 static __inline struct bt_ccb*
1014 btgetccb(struct bt_softc *bt)
1016 struct bt_ccb* bccb;
1019 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1020 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1024 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1026 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1036 btaction(struct cam_sim *sim, union ccb *ccb)
1038 struct bt_softc *bt;
1040 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1042 bt = (struct bt_softc *)cam_sim_softc(sim);
1044 switch (ccb->ccb_h.func_code) {
1045 /* Common cases first */
1046 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1047 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1049 struct bt_ccb *bccb;
1050 struct bt_hccb *hccb;
1053 * get a bccb to use.
1055 if ((bccb = btgetccb(bt)) == NULL) {
1057 bt->resource_shortage = TRUE;
1059 xpt_freeze_simq(bt->sim, /*count*/1);
1060 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1068 * So we can find the BCCB when an abort is requested
1071 ccb->ccb_h.ccb_bccb_ptr = bccb;
1072 ccb->ccb_h.ccb_bt_ptr = bt;
1075 * Put all the arguments for the xfer in the bccb
1077 hccb->target_id = ccb->ccb_h.target_id;
1078 hccb->target_lun = ccb->ccb_h.target_lun;
1082 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1083 struct ccb_scsiio *csio;
1084 struct ccb_hdr *ccbh;
1087 ccbh = &csio->ccb_h;
1088 hccb->opcode = INITIATOR_CCB_WRESID;
1089 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1090 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1091 hccb->cmd_len = csio->cdb_len;
1092 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1093 ccb->ccb_h.status = CAM_REQ_INVALID;
1094 btfreeccb(bt, bccb);
1098 hccb->sense_len = csio->sense_len;
1099 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1100 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1101 hccb->tag_enable = TRUE;
1102 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1104 hccb->tag_enable = FALSE;
1107 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1108 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1109 bcopy(csio->cdb_io.cdb_ptr,
1110 hccb->scsi_cdb, hccb->cmd_len);
1112 /* I guess I could map it in... */
1113 ccbh->status = CAM_REQ_INVALID;
1114 btfreeccb(bt, bccb);
1119 bcopy(csio->cdb_io.cdb_bytes,
1120 hccb->scsi_cdb, hccb->cmd_len);
1122 /* If need be, bounce our sense buffer */
1123 if (bt->sense_buffers != NULL) {
1124 hccb->sense_addr = btsensepaddr(bt, bccb);
1126 hccb->sense_addr = vtophys(&csio->sense_data);
1129 * If we have any data to send with this command,
1130 * map it into bus space.
1132 /* Only use S/G if there is a transfer */
1133 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1134 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
1136 * We've been given a pointer
1137 * to a single buffer.
1139 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
1143 error = bus_dmamap_load(
1151 if (error == EINPROGRESS) {
1154 * ordering, freeze the
1156 * until our mapping is
1159 xpt_freeze_simq(bt->sim,
1161 csio->ccb_h.status |=
1166 struct bus_dma_segment seg;
1168 /* Pointer to physical buffer */
1170 (bus_addr_t)csio->data_ptr;
1171 seg.ds_len = csio->dxfer_len;
1172 btexecuteccb(bccb, &seg, 1, 0);
1175 struct bus_dma_segment *segs;
1177 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1178 panic("btaction - Physical "
1182 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1183 panic("btaction - Virtual "
1184 "segment addresses "
1187 /* Just use the segments provided */
1188 segs = (struct bus_dma_segment *)
1190 btexecuteccb(bccb, segs,
1191 csio->sglist_cnt, 0);
1194 btexecuteccb(bccb, NULL, 0, 0);
1197 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1198 /* No data transfer */
1199 hccb->datain = TRUE;
1200 hccb->dataout = TRUE;
1202 hccb->sense_len = 0;
1203 hccb->tag_enable = FALSE;
1205 btexecuteccb(bccb, NULL, 0, 0);
1209 case XPT_EN_LUN: /* Enable LUN as a target */
1210 case XPT_TARGET_IO: /* Execute target I/O request */
1211 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1212 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1213 case XPT_ABORT: /* Abort the specified CCB */
1215 ccb->ccb_h.status = CAM_REQ_INVALID;
1218 case XPT_SET_TRAN_SETTINGS:
1221 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1225 case XPT_GET_TRAN_SETTINGS:
1226 /* Get default/user set transfer settings for the target */
1228 struct ccb_trans_settings *cts;
1232 target_mask = 0x01 << ccb->ccb_h.target_id;
1233 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1234 struct ccb_trans_settings_scsi *scsi =
1235 &cts->proto_specific.scsi;
1236 struct ccb_trans_settings_spi *spi =
1237 &cts->xport_specific.spi;
1238 cts->protocol = PROTO_SCSI;
1239 cts->protocol_version = SCSI_REV_2;
1240 cts->transport = XPORT_SPI;
1241 cts->transport_version = 2;
1243 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1244 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1246 if ((bt->disc_permitted & target_mask) != 0)
1247 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1248 if ((bt->tags_permitted & target_mask) != 0)
1249 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1251 if ((bt->ultra_permitted & target_mask) != 0)
1252 spi->sync_period = 12;
1253 else if ((bt->fast_permitted & target_mask) != 0)
1254 spi->sync_period = 25;
1255 else if ((bt->sync_permitted & target_mask) != 0)
1256 spi->sync_period = 50;
1258 spi->sync_period = 0;
1260 if (spi->sync_period != 0)
1261 spi->sync_offset = 15;
1263 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1264 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1266 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1267 if ((bt->wide_permitted & target_mask) != 0)
1268 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1270 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1272 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1273 scsi->valid = CTS_SCSI_VALID_TQ;
1274 spi->valid |= CTS_SPI_VALID_DISC;
1278 btfetchtransinfo(bt, cts);
1281 ccb->ccb_h.status = CAM_REQ_CMP;
1285 case XPT_CALC_GEOMETRY:
1287 struct ccb_calc_geometry *ccg;
1289 u_int32_t secs_per_cylinder;
1292 size_mb = ccg->volume_size
1293 / ((1024L * 1024L) / ccg->block_size);
1295 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1296 if (size_mb >= 2048) {
1298 ccg->secs_per_track = 63;
1301 ccg->secs_per_track = 32;
1305 ccg->secs_per_track = 32;
1307 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1308 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1309 ccb->ccb_h.status = CAM_REQ_CMP;
1313 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1315 btreset(bt, /*hardreset*/TRUE);
1316 ccb->ccb_h.status = CAM_REQ_CMP;
1320 case XPT_TERM_IO: /* Terminate the I/O process */
1322 ccb->ccb_h.status = CAM_REQ_INVALID;
1325 case XPT_PATH_INQ: /* Path routing inquiry */
1327 struct ccb_pathinq *cpi = &ccb->cpi;
1329 cpi->version_num = 1; /* XXX??? */
1330 cpi->hba_inquiry = PI_SDTR_ABLE;
1331 if (bt->tag_capable != 0)
1332 cpi->hba_inquiry |= PI_TAG_ABLE;
1333 if (bt->wide_bus != 0)
1334 cpi->hba_inquiry |= PI_WIDE_16;
1335 cpi->target_sprt = 0;
1337 cpi->hba_eng_cnt = 0;
1338 cpi->max_target = bt->wide_bus ? 15 : 7;
1340 cpi->initiator_id = bt->scsi_id;
1341 cpi->bus_id = cam_sim_bus(sim);
1342 cpi->base_transfer_speed = 3300;
1343 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1344 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1345 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1346 cpi->unit_number = cam_sim_unit(sim);
1347 cpi->ccb_h.status = CAM_REQ_CMP;
1348 cpi->transport = XPORT_SPI;
1349 cpi->transport_version = 2;
1350 cpi->protocol = PROTO_SCSI;
1351 cpi->protocol_version = SCSI_REV_2;
1356 ccb->ccb_h.status = CAM_REQ_INVALID;
1363 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1365 struct bt_ccb *bccb;
1367 struct bt_softc *bt;
1369 bccb = (struct bt_ccb *)arg;
1371 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1375 device_printf(bt->dev,
1376 "Unexpected error 0x%x returned from "
1377 "bus_dmamap_load\n", error);
1378 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1379 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1380 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1382 btfreeccb(bt, bccb);
1389 bus_dma_segment_t *end_seg;
1390 bus_dmasync_op_t op;
1392 end_seg = dm_segs + nseg;
1394 /* Copy the segments into our SG list */
1396 while (dm_segs < end_seg) {
1397 sg->len = dm_segs->ds_len;
1398 sg->addr = dm_segs->ds_addr;
1404 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1405 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1406 bccb->hccb.data_addr = bccb->sg_list_phys;
1408 bccb->hccb.data_len = bccb->sg_list->len;
1409 bccb->hccb.data_addr = bccb->sg_list->addr;
1412 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1413 op = BUS_DMASYNC_PREREAD;
1415 op = BUS_DMASYNC_PREWRITE;
1417 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1420 bccb->hccb.opcode = INITIATOR_CCB;
1421 bccb->hccb.data_len = 0;
1422 bccb->hccb.data_addr = 0;
1428 * Last time we need to check if this CCB needs to
1431 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1433 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1434 btfreeccb(bt, bccb);
1440 bccb->flags = BCCB_ACTIVE;
1441 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1442 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1444 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
1447 /* Tell the adapter about this command */
1448 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1449 if (bt->cur_outbox->action_code != BMBO_FREE) {
1451 * We should never encounter a busy mailbox.
1452 * If we do, warn the user, and treat it as
1453 * a resource shortage. If the controller is
1454 * hung, one of the pending transactions will
1455 * timeout causing us to start recovery operations.
1457 device_printf(bt->dev,
1458 "Encountered busy mailbox with %d out of %d "
1459 "commands active!!!\n", bt->active_ccbs,
1461 callout_stop(&ccb->ccb_h.timeout_ch);
1463 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1464 btfreeccb(bt, bccb);
1465 bt->resource_shortage = TRUE;
1466 xpt_freeze_simq(bt->sim, /*count*/1);
1467 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1471 bt->cur_outbox->action_code = BMBO_START;
1472 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1480 struct bt_softc *bt;
1483 bt = (struct bt_softc *)arg;
1484 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1486 if ((intstat & CMD_COMPLETE) != 0) {
1487 bt->latched_status = bt_inb(bt, STATUS_REG);
1488 bt->command_cmp = TRUE;
1491 bt_outb(bt, CONTROL_REG, RESET_INTR);
1493 if ((intstat & IMB_LOADED) != 0) {
1494 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1496 btccbptov(bt, bt->cur_inbox->ccb_addr),
1497 bt->cur_inbox->comp_code);
1498 bt->cur_inbox->comp_code = BMBI_FREE;
1503 if ((intstat & SCSI_BUS_RESET) != 0) {
1504 btreset(bt, /*hardreset*/FALSE);
1510 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1513 struct ccb_scsiio *csio;
1516 csio = &bccb->ccb->csio;
1518 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1519 device_printf(bt->dev,
1520 "btdone - Attempt to free non-active BCCB %p\n",
1525 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1526 bus_dmasync_op_t op;
1528 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1529 op = BUS_DMASYNC_POSTREAD;
1531 op = BUS_DMASYNC_POSTWRITE;
1532 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1533 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1536 if (bccb == bt->recovery_bccb) {
1538 * The recovery BCCB does not have a CCB associated
1539 * with it, so short circuit the normal error handling.
1540 * We now traverse our list of pending CCBs and process
1541 * any that were terminated by the recovery CCBs action.
1542 * We also reinstate timeouts for all remaining, pending,
1545 struct cam_path *path;
1546 struct ccb_hdr *ccb_h;
1549 /* Notify all clients that a BDR occured */
1550 error = xpt_create_path(&path, /*periph*/NULL,
1551 cam_sim_path(bt->sim),
1552 bccb->hccb.target_id,
1555 if (error == CAM_REQ_CMP)
1556 xpt_async(AC_SENT_BDR, path, NULL);
1558 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1559 while (ccb_h != NULL) {
1560 struct bt_ccb *pending_bccb;
1562 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1563 if (pending_bccb->hccb.target_id
1564 == bccb->hccb.target_id) {
1565 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1566 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1567 btdone(bt, pending_bccb, BMBI_ERROR);
1569 callout_reset(&ccb_h->timeout_ch,
1570 (ccb_h->timeout * hz) / 1000,
1571 bttimeout, pending_bccb);
1572 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1575 device_printf(bt->dev, "No longer in timeout\n");
1579 callout_stop(&ccb->ccb_h.timeout_ch);
1581 switch (comp_code) {
1583 device_printf(bt->dev,
1584 "btdone - CCB completed with free status!\n");
1586 case BMBI_NOT_FOUND:
1587 device_printf(bt->dev,
1588 "btdone - CCB Abort failed to find CCB\n");
1593 kprintf("bt: ccb %p - error %x occurred. "
1594 "btstat = %x, sdstat = %x\n",
1595 (void *)bccb, comp_code, bccb->hccb.btstat,
1598 /* An error occured */
1599 switch(bccb->hccb.btstat) {
1600 case BTSTAT_DATARUN_ERROR:
1601 if (bccb->hccb.data_len == 0) {
1603 * At least firmware 4.22, does this
1604 * for a QUEUE FULL condition.
1606 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1607 } else if (bccb->hccb.data_len < 0) {
1608 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1612 case BTSTAT_NOERROR:
1613 case BTSTAT_LINKED_CMD_COMPLETE:
1614 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1615 case BTSTAT_DATAUNDERUN_ERROR:
1617 csio->scsi_status = bccb->hccb.sdstat;
1618 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1619 switch(csio->scsi_status) {
1620 case SCSI_STATUS_CHECK_COND:
1621 case SCSI_STATUS_CMD_TERMINATED:
1622 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1623 /* Bounce sense back if necessary */
1624 if (bt->sense_buffers != NULL) {
1626 *btsensevaddr(bt, bccb);
1631 case SCSI_STATUS_OK:
1632 csio->ccb_h.status = CAM_REQ_CMP;
1635 csio->resid = bccb->hccb.data_len;
1637 case BTSTAT_SELTIMEOUT:
1638 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1640 case BTSTAT_UNEXPECTED_BUSFREE:
1641 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1643 case BTSTAT_INVALID_PHASE:
1644 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1646 case BTSTAT_INVALID_ACTION_CODE:
1647 panic("%s: Inavlid Action code", bt_name(bt));
1649 case BTSTAT_INVALID_OPCODE:
1650 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1652 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1653 /* We don't even support linked commands... */
1654 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1656 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1657 panic("%s: Invalid CCB or SG list", bt_name(bt));
1659 case BTSTAT_AUTOSENSE_FAILED:
1660 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1662 case BTSTAT_TAGGED_MSG_REJECTED:
1664 struct ccb_trans_settings neg;
1665 struct ccb_trans_settings_scsi *scsi =
1666 &neg.proto_specific.scsi;
1668 neg.protocol = PROTO_SCSI;
1669 neg.protocol_version = SCSI_REV_2;
1670 neg.transport = XPORT_SPI;
1671 neg.transport_version = 2;
1672 scsi->valid = CTS_SCSI_VALID_TQ;
1674 xpt_print_path(csio->ccb_h.path);
1675 kprintf("refuses tagged commands. Performing "
1676 "non-tagged I/O\n");
1677 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1679 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1680 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1681 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1684 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1686 * XXX You would think that this is
1687 * a recoverable error... Hmmm.
1689 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1691 case BTSTAT_HA_SOFTWARE_ERROR:
1692 case BTSTAT_HA_WATCHDOG_ERROR:
1693 case BTSTAT_HARDWARE_FAILURE:
1694 /* Hardware reset ??? Can we recover ??? */
1695 csio->ccb_h.status = CAM_NO_HBA;
1697 case BTSTAT_TARGET_IGNORED_ATN:
1698 case BTSTAT_OTHER_SCSI_BUS_RESET:
1699 case BTSTAT_HA_SCSI_BUS_RESET:
1700 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1702 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1705 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1706 csio->ccb_h.status = CAM_BDR_SENT;
1708 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1710 case BTSTAT_INVALID_RECONNECT:
1711 case BTSTAT_ABORT_QUEUE_GENERATED:
1712 csio->ccb_h.status = CAM_REQ_TERMIO;
1714 case BTSTAT_SCSI_PERROR_DETECTED:
1715 csio->ccb_h.status = CAM_UNCOR_PARITY;
1718 if (csio->ccb_h.status != CAM_REQ_CMP) {
1719 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1720 csio->ccb_h.status |= CAM_DEV_QFRZN;
1722 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1723 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1724 btfreeccb(bt, bccb);
1728 /* All completed without incident */
1729 ccb->ccb_h.status |= CAM_REQ_CMP;
1730 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1731 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1732 btfreeccb(bt, bccb);
1739 btreset(struct bt_softc* bt, int hard_reset)
1741 struct ccb_hdr *ccb_h;
1744 u_int8_t reset_type;
1746 if (hard_reset != 0)
1747 reset_type = HARD_RESET;
1749 reset_type = SOFT_RESET;
1750 bt_outb(bt, CONTROL_REG, reset_type);
1752 /* Wait 5sec. for Diagnostic start */
1753 timeout = 5 * 10000;
1755 status = bt_inb(bt, STATUS_REG);
1756 if ((status & DIAG_ACTIVE) != 0)
1762 kprintf("%s: btreset - Diagnostic Active failed to "
1763 "assert. status = 0x%x\n", bt_name(bt), status);
1767 /* Wait 10sec. for Diagnostic end */
1768 timeout = 10 * 10000;
1770 status = bt_inb(bt, STATUS_REG);
1771 if ((status & DIAG_ACTIVE) == 0)
1776 panic("%s: btreset - Diagnostic Active failed to drop. "
1777 "status = 0x%x\n", bt_name(bt), status);
1781 /* Wait for the host adapter to become ready or report a failure */
1784 status = bt_inb(bt, STATUS_REG);
1785 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1790 kprintf("%s: btreset - Host adapter failed to come ready. "
1791 "status = 0x%x\n", bt_name(bt), status);
1795 /* If the diagnostics failed, tell the user */
1796 if ((status & DIAG_FAIL) != 0
1797 || (status & HA_READY) == 0) {
1798 kprintf("%s: btreset - Adapter failed diagnostics\n",
1801 if ((status & DATAIN_REG_READY) != 0)
1802 kprintf("%s: btreset - Host Adapter Error code = 0x%x\n",
1803 bt_name(bt), bt_inb(bt, DATAIN_REG));
1807 /* If we've allocated mailboxes, initialize them */
1808 if (bt->init_level > 4)
1811 /* If we've attached to the XPT, tell it about the event */
1812 if (bt->path != NULL)
1813 xpt_async(AC_BUS_RESET, bt->path, NULL);
1816 * Perform completion processing for all outstanding CCBs.
1818 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1819 struct bt_ccb *pending_bccb;
1821 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1822 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1823 btdone(bt, pending_bccb, BMBI_ERROR);
1830 * Send a command to the adapter.
1833 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1834 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1840 u_int reply_buf_size;
1844 /* No data returned to start */
1845 reply_buf_size = reply_len;
1852 bt->command_cmp = 0;
1854 * Wait up to 10 sec. for the adapter to become
1855 * ready to accept commands.
1859 status = bt_inb(bt, STATUS_REG);
1860 if ((status & HA_READY) != 0
1861 && (status & CMD_REG_BUSY) == 0)
1864 * Throw away any pending data which may be
1865 * left over from earlier commands that we
1868 if ((status & DATAIN_REG_READY) != 0)
1869 (void)bt_inb(bt, DATAIN_REG);
1873 kprintf("%s: bt_cmd: Timeout waiting for adapter ready, "
1874 "status = 0x%x\n", bt_name(bt), status);
1879 * Send the opcode followed by any necessary parameter bytes.
1881 bt_outb(bt, COMMAND_REG, opcode);
1884 * Wait for up to 1sec for each byte of the the
1885 * parameter list sent to be sent.
1888 while (param_len && --timeout) {
1891 status = bt_inb(bt, STATUS_REG);
1892 intstat = bt_inb(bt, INTSTAT_REG);
1895 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1896 == (INTR_PENDING|CMD_COMPLETE)) {
1897 saved_status = status;
1901 if (bt->command_cmp != 0) {
1902 saved_status = bt->latched_status;
1906 if ((status & DATAIN_REG_READY) != 0)
1908 if ((status & CMD_REG_BUSY) == 0) {
1909 bt_outb(bt, COMMAND_REG, *params++);
1915 kprintf("%s: bt_cmd: Timeout sending parameters, "
1916 "status = 0x%x\n", bt_name(bt), status);
1918 saved_status = status;
1923 * Wait for the command to complete.
1925 while (cmd_complete == 0 && --cmd_timeout) {
1928 status = bt_inb(bt, STATUS_REG);
1929 intstat = bt_inb(bt, INTSTAT_REG);
1931 * It may be that this command was issued with
1932 * controller interrupts disabled. We'll never
1933 * get to our command if an incoming mailbox
1934 * interrupt is pending, so take care of completed
1935 * mailbox commands by calling our interrupt handler.
1937 if ((intstat & (INTR_PENDING|IMB_LOADED))
1938 == (INTR_PENDING|IMB_LOADED))
1942 if (bt->command_cmp != 0) {
1944 * Our interrupt handler saw CMD_COMPLETE
1945 * status before we did.
1948 saved_status = bt->latched_status;
1949 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1950 == (INTR_PENDING|CMD_COMPLETE)) {
1952 * Our poll (in case interrupts are blocked)
1953 * saw the CMD_COMPLETE interrupt.
1956 saved_status = status;
1957 } else if (opcode == BOP_MODIFY_IO_ADDR
1958 && (status & CMD_REG_BUSY) == 0) {
1960 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1961 * but it should update the status register. So, we
1962 * consider this command complete when the CMD_REG_BUSY
1965 saved_status = status;
1967 } else if ((status & DATAIN_REG_READY) != 0) {
1970 data = bt_inb(bt, DATAIN_REG);
1971 if (reply_len < reply_buf_size) {
1972 *reply_data++ = data;
1974 kprintf("%s: bt_cmd - Discarded reply data byte "
1975 "for opcode 0x%x\n", bt_name(bt),
1979 * Reset timeout to ensure at least a second
1980 * between response bytes.
1982 cmd_timeout = MAX(cmd_timeout, 10000);
1985 } else if ((opcode == BOP_FETCH_LRAM)
1986 && (status & HA_READY) != 0) {
1987 saved_status = status;
1992 if (cmd_timeout == 0) {
1993 kprintf("%s: bt_cmd: Timeout waiting for command (%x) "
1994 "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
1995 "rlen %d\n", bt_name(bt), opcode,
1996 bt_name(bt), status, intstat, reply_len);
1997 error = (ETIMEDOUT);
2001 * Clear any pending interrupts. Block interrupts so our
2002 * interrupt handler is not re-entered.
2012 * If the command was rejected by the controller, tell the caller.
2014 if ((saved_status & CMD_INVALID) != 0) {
2016 * Some early adapters may not recover properly from
2017 * an invalid command. If it appears that the controller
2018 * has wedged (i.e. status was not cleared by our interrupt
2019 * reset above), perform a soft reset.
2022 kprintf("%s: Invalid Command 0x%x\n", bt_name(bt),
2025 status = bt_inb(bt, STATUS_REG);
2026 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2027 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2028 || (status & (HA_READY|INIT_REQUIRED))
2029 != (HA_READY|INIT_REQUIRED)) {
2030 btreset(bt, /*hard_reset*/FALSE);
2035 if (param_len > 0) {
2036 /* The controller did not accept the full argument list */
2040 if (reply_len != reply_buf_size) {
2041 /* Too much or too little data received */
2045 /* We were successful */
2050 btinitmboxes(struct bt_softc *bt) {
2051 init_32b_mbox_params_t init_mbox;
2054 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2055 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2056 bt->cur_inbox = bt->in_boxes;
2057 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2058 bt->cur_outbox = bt->out_boxes;
2059 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2061 /* Tell the adapter about them */
2062 init_mbox.num_boxes = bt->num_boxes;
2063 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2064 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2065 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2066 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2067 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2068 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2069 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2072 kprintf("btinitmboxes: Initialization command failed\n");
2073 else if (bt->strict_rr != 0) {
2075 * If the controller supports
2076 * strict round robin mode,
2082 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, ¶m, 1,
2083 /*reply_buf*/NULL, /*reply_len*/0,
2084 DEFAULT_CMD_TIMEOUT);
2087 kprintf("btinitmboxes: Unable to enable strict RR\n");
2089 } else if (bootverbose) {
2090 kprintf("%s: Using Strict Round Robin Mailbox Mode\n",
2099 * Update the XPT's idea of the negotiated transfer
2100 * parameters for a particular target.
2103 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings *cts)
2105 setup_data_t setup_info;
2114 targ_syncinfo_t sync_info;
2115 struct ccb_trans_settings_scsi *scsi =
2116 &cts->proto_specific.scsi;
2117 struct ccb_trans_settings_spi *spi =
2118 &cts->xport_specific.spi;
2123 target = cts->ccb_h.target_id;
2124 targ_offset = (target & 0x7);
2125 targ_mask = (0x01 << targ_offset);
2128 * Inquire Setup Information. This command retreives the
2129 * Wide negotiation status for recent adapters as well as
2130 * the sync info for older models.
2132 param = sizeof(setup_info);
2133 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
2134 (u_int8_t*)&setup_info, sizeof(setup_info),
2135 DEFAULT_CMD_TIMEOUT);
2138 kprintf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
2139 bt_name(bt), error);
2143 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2144 : setup_info.high_syncinfo[targ_offset];
2146 if (sync_info.sync == 0)
2149 sync_offset = sync_info.offset;
2152 bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2153 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2157 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2158 : (setup_info.high_wide_active & targ_mask);
2161 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2162 } else if ((bt->wide_permitted & targ_mask) != 0) {
2163 struct ccb_getdev cgd;
2166 * Prior to rev 5.06L, wide status isn't provided,
2167 * so we "guess" that wide transfers are in effect
2168 * if the user settings allow for wide and the inquiry
2169 * data for the device indicates that it can handle
2172 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2173 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2174 xpt_action((union ccb *)&cgd);
2175 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2176 && (cgd.inq_data.flags & SID_WBus16) != 0)
2177 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2180 if (bt->firmware_ver[0] >= '3') {
2182 * For adapters that can do fast or ultra speeds,
2183 * use the more exact Target Sync Information command.
2185 target_sync_info_data_t sync_info;
2187 param = sizeof(sync_info);
2188 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, ¶m, /*paramlen*/1,
2189 (u_int8_t*)&sync_info, sizeof(sync_info),
2190 DEFAULT_CMD_TIMEOUT);
2193 kprintf("%s: btfetchtransinfo - Inquire Sync "
2194 "Info Failed 0x%x\n", bt_name(bt), error);
2197 sync_period = sync_info.sync_rate[target] * 100;
2199 sync_period = 2000 + (500 * sync_info.period);
2202 cts->protocol = PROTO_SCSI;
2203 cts->protocol_version = SCSI_REV_2;
2204 cts->transport = XPORT_SPI;
2205 cts->transport_version = 2;
2207 spi->sync_period = sync_period;
2208 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2209 spi->sync_offset = sync_offset;
2210 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2212 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2213 spi->bus_width = bus_width;
2215 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2216 scsi->valid = CTS_SCSI_VALID_TQ;
2217 spi->valid |= CTS_SPI_VALID_DISC;
2221 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2225 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2227 struct bt_softc* bt;
2229 bt = (struct bt_softc*)arg;
2230 bt->mailbox_physbase = segs->ds_addr;
2234 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2236 struct bt_softc* bt;
2238 bt = (struct bt_softc*)arg;
2239 bt->bt_ccb_physbase = segs->ds_addr;
2243 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2246 struct bt_softc* bt;
2248 bt = (struct bt_softc*)arg;
2249 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2253 btpoll(struct cam_sim *sim)
2255 bt_intr(cam_sim_softc(sim));
2259 bttimeout(void *arg)
2261 struct bt_ccb *bccb;
2263 struct bt_softc *bt;
2265 bccb = (struct bt_ccb *)arg;
2267 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2268 xpt_print_path(ccb->ccb_h.path);
2269 kprintf("CCB %p - timed out\n", (void *)bccb);
2273 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2274 xpt_print_path(ccb->ccb_h.path);
2275 kprintf("CCB %p - timed out CCB already completed\n",
2282 * In order to simplify the recovery process, we ask the XPT
2283 * layer to halt the queue of new transactions and we traverse
2284 * the list of pending CCBs and remove their timeouts. This
2285 * means that the driver attempts to clear only one error
2286 * condition at a time. In general, timeouts that occur
2287 * close together are related anyway, so there is no benefit
2288 * in attempting to handle errors in parrallel. Timeouts will
2289 * be reinstated when the recovery process ends.
2291 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2292 struct ccb_hdr *ccb_h;
2294 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2295 xpt_freeze_simq(bt->sim, /*count*/1);
2296 bccb->flags |= BCCB_RELEASE_SIMQ;
2299 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2300 while (ccb_h != NULL) {
2301 struct bt_ccb *pending_bccb;
2303 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2304 callout_stop(&ccb_h->timeout_ch);
2305 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2309 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2310 || bt->cur_outbox->action_code != BMBO_FREE
2311 || ((bccb->hccb.tag_enable == TRUE)
2312 && (bt->firmware_ver[0] < '5'))) {
2314 * Try a full host adapter/SCSI bus reset.
2315 * We do this only if we have already attempted
2316 * to clear the condition with a BDR, or we cannot
2317 * attempt a BDR for lack of mailbox resources
2318 * or because of faulty firmware. It turns out
2319 * that firmware versions prior to 5.xx treat BDRs
2320 * as untagged commands that cannot be sent until
2321 * all outstanding tagged commands have been processed.
2322 * This makes it somewhat difficult to use a BDR to
2323 * clear up a problem with an uncompleted tagged command.
2325 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2326 btreset(bt, /*hardreset*/TRUE);
2327 kprintf("%s: No longer in timeout\n", bt_name(bt));
2330 * Send a Bus Device Reset message:
2331 * The target that is holding up the bus may not
2332 * be the same as the one that triggered this timeout
2333 * (different commands have different timeout lengths),
2334 * but we have no way of determining this from our
2335 * timeout handler. Our strategy here is to queue a
2336 * BDR message to the target of the timed out command.
2337 * If this fails, we'll get another timeout 2 seconds
2338 * later which will attempt a bus reset.
2340 bccb->flags |= BCCB_DEVICE_RESET;
2341 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, bttimeout, bccb);
2343 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2345 /* No Data Transfer */
2346 bt->recovery_bccb->hccb.datain = TRUE;
2347 bt->recovery_bccb->hccb.dataout = TRUE;
2348 bt->recovery_bccb->hccb.btstat = 0;
2349 bt->recovery_bccb->hccb.sdstat = 0;
2350 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2352 /* Tell the adapter about this command */
2353 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2354 bt->cur_outbox->action_code = BMBO_START;
2355 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);