2 * Generic register and struct definitions for the Adaptech 154x/164x
3 * SCSI host adapters. Product specific probe and attach routines can
5 * aha 1540/1542B/1542C/1542CF/1542CP aha_isa.c
7 * Copyright (c) 1998 M. Warner Losh.
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 * Derived from bt.c written by:
34 * Copyright (c) 1998 Justin T. Gibbs.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions, and the following disclaimer,
42 * without modification, immediately at the beginning of the file.
43 * 2. The name of the author may not be used to endorse or promote products
44 * derived from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
50 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * $FreeBSD: src/sys/dev/aha/aha.c,v 1.34.2.1 2000/08/02 22:24:39 peter Exp $
59 * $DragonFly: src/sys/dev/disk/aha/aha.c,v 1.13 2006/09/05 00:55:37 dillon Exp $
62 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
67 #include <sys/kernel.h>
68 #include <sys/thread2.h>
70 #include <machine/bus_pio.h>
71 #include <machine/bus.h>
72 #include <machine/clock.h>
74 #include <bus/cam/cam.h>
75 #include <bus/cam/cam_ccb.h>
76 #include <bus/cam/cam_sim.h>
77 #include <bus/cam/cam_xpt_sim.h>
78 #include <bus/cam/cam_debug.h>
80 #include <bus/cam/scsi/scsi_message.h>
87 struct aha_softc *aha_softcs[NAHATOT];
89 #define PRVERB(x) do { if (bootverbose) device_printf x; } while(0)
91 /* Macro to determine that a rev is potentially a new valid one
92 * so that the driver doesn't keep breaking on new revs as it
93 * did for the CF and CP.
95 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
97 /* MailBox Management functions */
98 static __inline void ahanextinbox(struct aha_softc *aha);
99 static __inline void ahanextoutbox(struct aha_softc *aha);
101 #define aha_name(aha) device_get_nameunit(aha->dev)
104 ahanextinbox(struct aha_softc *aha)
106 if (aha->cur_inbox == aha->last_inbox)
107 aha->cur_inbox = aha->in_boxes;
113 ahanextoutbox(struct aha_softc *aha)
115 if (aha->cur_outbox == aha->last_outbox)
116 aha->cur_outbox = aha->out_boxes;
121 #define ahautoa24(u,s3) \
122 (s3)[0] = ((u) >> 16) & 0xff; \
123 (s3)[1] = ((u) >> 8) & 0xff; \
124 (s3)[2] = (u) & 0xff;
126 #define aha_a24tou(s3) \
127 (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
129 /* CCB Mangement functions */
130 static __inline u_int32_t ahaccbvtop(struct aha_softc *aha,
131 struct aha_ccb *accb);
132 static __inline struct aha_ccb* ahaccbptov(struct aha_softc *aha,
135 static __inline u_int32_t
136 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
138 return (aha->aha_ccb_physbase
139 + (u_int32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
141 static __inline struct aha_ccb *
142 ahaccbptov(struct aha_softc *aha, u_int32_t ccb_addr)
144 return (aha->aha_ccb_array +
145 + ((struct aha_ccb*)ccb_addr-(struct aha_ccb*)aha->aha_ccb_physbase));
148 static struct aha_ccb* ahagetccb(struct aha_softc *aha);
149 static __inline void ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
150 static void ahaallocccbs(struct aha_softc *aha);
151 static bus_dmamap_callback_t ahaexecuteccb;
152 static void ahadone(struct aha_softc *aha, struct aha_ccb *accb,
153 aha_mbi_comp_code_t comp_code);
155 /* Host adapter command functions */
156 static int ahareset(struct aha_softc* aha, int hard_reset);
158 /* Initialization functions */
159 static int ahainitmboxes(struct aha_softc *aha);
160 static bus_dmamap_callback_t ahamapmboxes;
161 static bus_dmamap_callback_t ahamapccbs;
162 static bus_dmamap_callback_t ahamapsgs;
164 /* Transfer Negotiation Functions */
165 static void ahafetchtransinfo(struct aha_softc *aha,
166 struct ccb_trans_settings *cts);
168 /* CAM SIM entry points */
169 #define ccb_accb_ptr spriv_ptr0
170 #define ccb_aha_ptr spriv_ptr1
171 static void ahaaction(struct cam_sim *sim, union ccb *ccb);
172 static void ahapoll(struct cam_sim *sim);
174 /* Our timeout handler */
175 static timeout_t ahatimeout;
180 * Do our own re-probe protection until a configuration
181 * manager can do it for us. This ensures that we don't
182 * reprobe a card already found by the EISA or PCI probes.
184 static struct aha_isa_port aha_isa_ports[] =
195 * I/O ports listed in the order enumerated by the
196 * card for certain op codes.
198 static u_int16_t aha_board_ports[] =
208 /* Exported functions */
210 aha_alloc(device_t dev, bus_space_tag_t tag, bus_space_handle_t bsh)
212 struct aha_softc *aha;
214 int unit = device_get_unit(dev);
215 if (unit != AHA_TEMP_UNIT) {
216 if (unit >= NAHATOT) {
217 device_printf(dev, "unit number (%d) too high\n", unit);
222 * Allocate a storage area for us
224 if (aha_softcs[unit]) {
225 device_printf(dev, "memory already allocated\n");
230 aha = kmalloc(sizeof(struct aha_softc), M_DEVBUF, M_INTWAIT | M_ZERO);
231 SLIST_INIT(&aha->free_aha_ccbs);
232 LIST_INIT(&aha->pending_ccbs);
233 SLIST_INIT(&aha->sg_maps);
237 aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
238 aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
240 if (unit != AHA_TEMP_UNIT) {
241 aha_softcs[unit] = aha;
247 aha_free(struct aha_softc *aha)
249 int unit = device_get_unit(aha->dev);
251 switch (aha->init_level) {
255 struct sg_map_node *sg_map;
257 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
258 SLIST_REMOVE_HEAD(&aha->sg_maps, links);
259 bus_dmamap_unload(aha->sg_dmat,
261 bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
263 kfree(sg_map, M_DEVBUF);
265 bus_dma_tag_destroy(aha->sg_dmat);
269 bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
272 bus_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);
273 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
277 bus_dma_tag_destroy(aha->ccb_dmat);
280 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
283 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
284 aha->mailbox_dmamap);
285 bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);
288 bus_dma_tag_destroy(aha->buffer_dmat);
291 bus_dma_tag_destroy(aha->mailbox_dmat);
296 if (unit != AHA_TEMP_UNIT) {
297 aha_softcs[unit] = NULL;
299 kfree(aha, M_DEVBUF);
303 * Probe the adapter and verify that the card is an Adaptec.
306 aha_probe(struct aha_softc* aha)
311 board_id_data_t board_id;
314 * See if the three I/O ports look reasonable.
315 * Touch the minimal number of registers in the
318 status = aha_inb(aha, STATUS_REG);
320 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
321 STATUS_REG_RSVD)) != 0) {
322 PRVERB((aha->dev, "status reg test failed %x\n", status));
326 intstat = aha_inb(aha, INTSTAT_REG);
327 if ((intstat & INTSTAT_REG_RSVD) != 0) {
328 PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
333 * Looking good so far. Final test is to reset the
334 * adapter and fetch the board ID and ensure we aren't
335 * looking at a BusLogic.
337 if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
338 PRVERB((aha->dev, "Failed Reset\n"));
343 * Get the board ID. We use this to see if we're dealing with
344 * a buslogic card or a aha card (or clone).
346 error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
347 (u_int8_t*)&board_id, sizeof(board_id),
348 DEFAULT_CMD_TIMEOUT);
350 PRVERB((aha->dev, "INQUIRE failed %x\n", error));
353 aha->fw_major = board_id.firmware_rev_major;
354 aha->fw_minor = board_id.firmware_rev_minor;
355 aha->boardid = board_id.board_type;
358 * The Buslogic cards have an id of either 0x41 or 0x42. So
359 * if those come up in the probe, we test the geometry register
360 * of the board. Adaptec boards that are this old will not have
361 * this register, and return 0xff, while buslogic cards will return
362 * something different.
364 * It appears that for reasons unknow, for the for the
365 * aha-1542B cards, we need to wait a little bit before trying
366 * to read the geometry register. I picked 10ms since we have
367 * reports that a for loop to 1000 did the trick, and this
368 * errs on the side of conservatism. Besides, no one will
369 * notice a 10mS delay here, even the 1542B card users :-)
371 * Some compatible cards return 0 here. Some cards also
372 * seem to return 0x7f.
374 * XXX I'm not sure how this will impact other cloned cards
376 * This really should be replaced with the esetup command, since
377 * that appears to be more reliable. This becomes more and more
378 * true over time as we discover more cards that don't read the
379 * geometry register consistantly.
381 if (aha->boardid <= 0x42) {
382 /* Wait 10ms before reading */
384 status = aha_inb(aha, GEOMETRY_REG);
385 if (status != 0xff && status != 0x00 && status != 0x7f) {
386 PRVERB((aha->dev, "Geometry Register test failed 0x%x\n", status));
395 * Pull the boards setup information and record it in our softc.
398 aha_fetch_adapter_info(struct aha_softc *aha)
400 setup_data_t setup_info;
401 config_data_t config_data;
402 u_int8_t length_param;
404 struct aha_extbios extbios;
406 switch (aha->boardid) {
407 case BOARD_1540_16HEAD_BIOS:
408 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
410 case BOARD_1540_64HEAD_BIOS:
411 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
414 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
417 snprintf(aha->model, sizeof(aha->model), "1640");
420 snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");
423 snprintf(aha->model, sizeof(aha->model), "1542C");
426 snprintf(aha->model, sizeof(aha->model), "1542CF");
429 snprintf(aha->model, sizeof(aha->model), "1542CP");
432 snprintf(aha->model, sizeof(aha->model), "Unknown");
436 * If we are a new type of 1542 board (anything newer than a 1542C)
437 * then disable the extended bios so that the
438 * mailbox interface is unlocked.
439 * This is also true for the 1542B Version 3.20. First Adaptec
440 * board that supports >1Gb drives.
441 * No need to check the extended bios flags as some of the
442 * extensions that cause us problems are not flagged in that byte.
444 if (PROBABLY_NEW_BOARD(aha->boardid) ||
445 (aha->boardid == 0x41
446 && aha->fw_major == 0x31 &&
447 aha->fw_minor >= 0x34)) {
448 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
449 /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
450 DEFAULT_CMD_TIMEOUT);
451 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (u_int8_t *)&extbios,
452 /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
454 if (aha->boardid < 0x41)
455 device_printf(aha->dev, "Warning: aha-1542A won't likely work.\n");
457 aha->max_sg = 17; /* Need >= 17 to do 64k I/O */
459 aha->extended_lun = 0;
460 aha->extended_trans = 0;
462 /* Determine Sync/Wide/Disc settings */
463 length_param = sizeof(setup_info);
464 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
465 /*paramlen*/1, (u_int8_t*)&setup_info,
466 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
468 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
472 if (setup_info.initiate_sync != 0) {
473 aha->sync_permitted = ALL_TARGETS;
475 aha->disc_permitted = ALL_TARGETS;
477 /* We need as many mailboxes as we can have ccbs */
478 aha->num_boxes = aha->max_ccbs;
480 /* Determine our SCSI ID */
482 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
483 (u_int8_t*)&config_data, sizeof(config_data),
484 DEFAULT_CMD_TIMEOUT);
486 device_printf(aha->dev, "aha_fetch_adapter_info - Failed Get Config\n");
489 aha->scsi_id = config_data.scsi_id;
494 * Start the board, ready for normal operation
497 aha_init(struct aha_softc* aha)
499 /* Announce the Adapter */
500 device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
501 aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
503 if (aha->diff_bus != 0)
505 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
509 * Create our DMA tags. These tags define the kinds of device
510 * accessible memory allocations and memory mappings we will
511 * need to perform during normal operation.
513 * Unless we need to further restrict the allocation, we rely
514 * on the restrictions of the parent dmat, hence the common
515 * use of MAXADDR and MAXSIZE.
518 /* DMA tag for mapping buffers into device visible space. */
519 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
520 /*lowaddr*/BUS_SPACE_MAXADDR,
521 /*highaddr*/BUS_SPACE_MAXADDR,
522 /*filter*/NULL, /*filterarg*/NULL,
523 /*maxsize*/MAXBSIZE, /*nsegments*/AHA_NSEG,
524 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
525 /*flags*/BUS_DMA_ALLOCNOW,
526 &aha->buffer_dmat) != 0) {
531 /* DMA tag for our mailboxes */
532 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
533 /*lowaddr*/BUS_SPACE_MAXADDR,
534 /*highaddr*/BUS_SPACE_MAXADDR,
535 /*filter*/NULL, /*filterarg*/NULL,
536 aha->num_boxes * (sizeof(aha_mbox_in_t)
537 + sizeof(aha_mbox_out_t)),
539 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
540 /*flags*/0, &aha->mailbox_dmat) != 0) {
546 /* Allocation for our mailboxes */
547 if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
548 BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0) {
554 /* And permanently map them */
555 bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
557 aha->num_boxes * (sizeof(aha_mbox_in_t)
558 + sizeof(aha_mbox_out_t)),
559 ahamapmboxes, aha, /*flags*/0);
563 aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
567 /* DMA tag for our ccb structures */
568 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
569 /*lowaddr*/BUS_SPACE_MAXADDR,
570 /*highaddr*/BUS_SPACE_MAXADDR,
571 /*filter*/NULL, /*filterarg*/NULL,
572 aha->max_ccbs * sizeof(struct aha_ccb),
574 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
575 /*flags*/0, &aha->ccb_dmat) != 0) {
581 /* Allocation for our ccbs */
582 if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
583 BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0) {
589 /* And permanently map them */
590 bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap,
592 aha->max_ccbs * sizeof(struct aha_ccb),
593 ahamapccbs, aha, /*flags*/0);
597 /* DMA tag for our S/G structures. We allocate in page sized chunks */
598 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
599 /*lowaddr*/BUS_SPACE_MAXADDR,
600 /*highaddr*/BUS_SPACE_MAXADDR,
601 /*filter*/NULL, /*filterarg*/NULL,
602 PAGE_SIZE, /*nsegments*/1,
603 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
604 /*flags*/0, &aha->sg_dmat) != 0) {
610 /* Perform initial CCB allocation */
611 bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
614 if (aha->num_ccbs == 0) {
615 device_printf(aha->dev,
616 "aha_init - Unable to allocate initial ccbs\n");
621 * Note that we are going and return (to probe)
631 aha_attach(struct aha_softc *aha)
633 int tagged_dev_openings;
634 struct cam_devq *devq;
637 * We don't do tagged queueing, since the aha cards don't
640 tagged_dev_openings = 0;
643 * Create the device queue for our SIM.
645 devq = cam_simq_alloc(aha->max_ccbs - 1);
650 * Construct our SIM entry
652 aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha, device_get_unit(aha->dev),
653 2, tagged_dev_openings, devq);
654 cam_simq_release(devq);
655 if (aha->sim == NULL) {
659 if (xpt_bus_register(aha->sim, 0) != CAM_SUCCESS) {
660 cam_sim_free(aha->sim);
664 if (xpt_create_path(&aha->path, /*periph*/NULL,
665 cam_sim_path(aha->sim), CAM_TARGET_WILDCARD,
666 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
667 xpt_bus_deregister(cam_sim_path(aha->sim));
668 cam_sim_free(aha->sim);
676 aha_find_probe_range(int ioport, int *port_index, int *max_port_index)
681 for (i = 0;i < AHA_NUM_ISAPORTS; i++)
682 if (ioport <= aha_isa_ports[i].addr)
684 if ((i >= AHA_NUM_ISAPORTS)
685 || (ioport != aha_isa_ports[i].addr)) {
687 "aha_isa_probe: Invalid baseport of 0x%x specified.\n"
688 "aha_isa_probe: Nearest valid baseport is 0x%x.\n"
689 "aha_isa_probe: Failing probe.\n",
691 (i < AHA_NUM_ISAPORTS)
692 ? aha_isa_ports[i].addr
693 : aha_isa_ports[AHA_NUM_ISAPORTS - 1].addr);
694 *port_index = *max_port_index = -1;
697 *port_index = *max_port_index = aha_isa_ports[i].bio;
700 *max_port_index = AHA_NUM_ISAPORTS - 1;
705 aha_iop_from_bio(isa_compat_io_t bio_index)
707 if (bio_index >= 0 && bio_index < AHA_NUM_ISAPORTS)
708 return (aha_board_ports[bio_index]);
713 ahaallocccbs(struct aha_softc *aha)
715 struct aha_ccb *next_ccb;
716 struct sg_map_node *sg_map;
722 next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
724 sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_INTWAIT);
726 /* Allocate S/G space for the next batch of CCBS */
727 if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
728 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
729 kfree(sg_map, M_DEVBUF);
733 SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
735 bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
736 PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
738 segs = sg_map->sg_vaddr;
739 physaddr = sg_map->sg_physaddr;
741 newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
742 for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
745 next_ccb->sg_list = segs;
746 next_ccb->sg_list_phys = physaddr;
747 next_ccb->flags = ACCB_FREE;
748 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
752 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
754 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
759 /* Reserve a CCB for error recovery */
760 if (aha->recovery_accb == NULL) {
761 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
762 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
767 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
770 if ((accb->flags & ACCB_ACTIVE) != 0)
771 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
772 if (aha->resource_shortage != 0
773 && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
774 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
775 aha->resource_shortage = FALSE;
777 accb->flags = ACCB_FREE;
778 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
783 static struct aha_ccb*
784 ahagetccb(struct aha_softc *aha)
786 struct aha_ccb* accb;
789 if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
790 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
792 } else if (aha->num_ccbs < aha->max_ccbs) {
794 accb = SLIST_FIRST(&aha->free_aha_ccbs);
796 device_printf(aha->dev, "Can't malloc ACCB\n");
798 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
808 ahaaction(struct cam_sim *sim, union ccb *ccb)
810 struct aha_softc *aha;
812 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
814 aha = (struct aha_softc *)cam_sim_softc(sim);
816 switch (ccb->ccb_h.func_code) {
817 /* Common cases first */
818 case XPT_SCSI_IO: /* Execute the requested I/O operation */
819 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
821 struct aha_ccb *accb;
822 struct aha_hccb *hccb;
827 if ((accb = ahagetccb(aha)) == NULL) {
829 aha->resource_shortage = TRUE;
831 xpt_freeze_simq(aha->sim, /*count*/1);
832 ccb->ccb_h.status = CAM_REQUEUE_REQ;
840 * So we can find the ACCB when an abort is requested
843 ccb->ccb_h.ccb_accb_ptr = accb;
844 ccb->ccb_h.ccb_aha_ptr = aha;
847 * Put all the arguments for the xfer in the accb
849 hccb->target = ccb->ccb_h.target_id;
850 hccb->lun = ccb->ccb_h.target_lun;
854 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
855 struct ccb_scsiio *csio;
856 struct ccb_hdr *ccbh;
860 hccb->opcode = aha->ccb_ccb_opcode;
861 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
862 hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
863 hccb->cmd_len = csio->cdb_len;
864 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
865 ccb->ccb_h.status = CAM_REQ_INVALID;
866 ahafreeccb(aha, accb);
870 hccb->sense_len = csio->sense_len;
871 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
872 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
873 bcopy(csio->cdb_io.cdb_ptr,
874 hccb->scsi_cdb, hccb->cmd_len);
876 /* I guess I could map it in... */
877 ccbh->status = CAM_REQ_INVALID;
878 ahafreeccb(aha, accb);
883 bcopy(csio->cdb_io.cdb_bytes,
884 hccb->scsi_cdb, hccb->cmd_len);
887 * If we have any data to send with this command,
888 * map it into bus space.
890 /* Only use S/G if there is a transfer */
891 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
892 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
894 * We've been given a pointer
895 * to a single buffer.
897 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
901 error = bus_dmamap_load(
909 if (error == EINPROGRESS) {
912 * ordering, freeze the
914 * until our mapping is
917 xpt_freeze_simq(aha->sim,
919 csio->ccb_h.status |=
924 struct bus_dma_segment seg;
926 /* Pointer to physical buffer */
928 (bus_addr_t)csio->data_ptr;
929 seg.ds_len = csio->dxfer_len;
930 ahaexecuteccb(accb, &seg, 1, 0);
933 struct bus_dma_segment *segs;
935 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
936 panic("ahaaction - Physical "
940 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
941 panic("ahaaction - Virtual "
945 /* Just use the segments provided */
946 segs = (struct bus_dma_segment *)
948 ahaexecuteccb(accb, segs,
949 csio->sglist_cnt, 0);
952 ahaexecuteccb(accb, NULL, 0, 0);
955 hccb->opcode = INITIATOR_BUS_DEV_RESET;
956 /* No data transfer */
958 hccb->dataout = TRUE;
961 ahaexecuteccb(accb, NULL, 0, 0);
965 case XPT_EN_LUN: /* Enable LUN as a target */
966 case XPT_TARGET_IO: /* Execute target I/O request */
967 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
968 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
969 case XPT_ABORT: /* Abort the specified CCB */
971 ccb->ccb_h.status = CAM_REQ_INVALID;
974 case XPT_SET_TRAN_SETTINGS:
977 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
981 case XPT_GET_TRAN_SETTINGS:
982 /* Get default/user set transfer settings for the target */
984 struct ccb_trans_settings *cts;
988 target_mask = 0x01 << ccb->ccb_h.target_id;
989 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
991 if ((aha->disc_permitted & target_mask) != 0)
992 cts->flags |= CCB_TRANS_DISC_ENB;
993 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
994 if ((aha->sync_permitted & target_mask) != 0) {
995 if (aha->boardid >= BOARD_1542CF)
996 cts->sync_period = 25;
998 cts->sync_period = 50;
1000 cts->sync_period = 0;
1002 if (cts->sync_period != 0)
1003 cts->sync_offset = 15;
1005 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1006 | CCB_TRANS_SYNC_OFFSET_VALID
1007 | CCB_TRANS_BUS_WIDTH_VALID
1008 | CCB_TRANS_DISC_VALID
1009 | CCB_TRANS_TQ_VALID;
1011 ahafetchtransinfo(aha, cts);
1014 ccb->ccb_h.status = CAM_REQ_CMP;
1018 case XPT_CALC_GEOMETRY:
1020 struct ccb_calc_geometry *ccg;
1022 u_int32_t secs_per_cylinder;
1025 size_mb = ccg->volume_size
1026 / ((1024L * 1024L) / ccg->block_size);
1028 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1029 if (size_mb >= 2048) {
1031 ccg->secs_per_track = 63;
1034 ccg->secs_per_track = 32;
1038 ccg->secs_per_track = 32;
1040 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1041 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1042 ccb->ccb_h.status = CAM_REQ_CMP;
1046 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1048 ahareset(aha, /*hardreset*/TRUE);
1049 ccb->ccb_h.status = CAM_REQ_CMP;
1053 case XPT_TERM_IO: /* Terminate the I/O process */
1055 ccb->ccb_h.status = CAM_REQ_INVALID;
1058 case XPT_PATH_INQ: /* Path routing inquiry */
1060 struct ccb_pathinq *cpi = &ccb->cpi;
1062 cpi->version_num = 1; /* XXX??? */
1063 cpi->hba_inquiry = PI_SDTR_ABLE;
1064 cpi->target_sprt = 0;
1066 cpi->hba_eng_cnt = 0;
1067 cpi->max_target = 7;
1069 cpi->initiator_id = aha->scsi_id;
1070 cpi->bus_id = cam_sim_bus(sim);
1071 cpi->base_transfer_speed = 3300;
1072 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1073 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
1074 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1075 cpi->unit_number = cam_sim_unit(sim);
1076 cpi->ccb_h.status = CAM_REQ_CMP;
1081 ccb->ccb_h.status = CAM_REQ_INVALID;
1088 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1090 struct aha_ccb *accb;
1092 struct aha_softc *aha;
1095 accb = (struct aha_ccb *)arg;
1097 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1101 device_printf(aha->dev,
1102 "Unexepected error 0x%x returned from "
1103 "bus_dmamap_load\n", error);
1104 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1105 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1106 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1108 ahafreeccb(aha, accb);
1115 bus_dma_segment_t *end_seg;
1116 bus_dmasync_op_t op;
1118 end_seg = dm_segs + nseg;
1120 /* Copy the segments into our SG list */
1122 while (dm_segs < end_seg) {
1123 ahautoa24(dm_segs->ds_len, sg->len);
1124 ahautoa24(dm_segs->ds_addr, sg->addr);
1130 accb->hccb.opcode = aha->ccb_sg_opcode;
1131 ahautoa24((sizeof(aha_sg_t) * nseg),
1132 accb->hccb.data_len);
1133 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1135 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1136 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1139 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1140 op = BUS_DMASYNC_PREREAD;
1142 op = BUS_DMASYNC_PREWRITE;
1144 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1147 accb->hccb.opcode = INITIATOR_CCB;
1148 ahautoa24(0, accb->hccb.data_len);
1149 ahautoa24(0, accb->hccb.data_addr);
1155 * Last time we need to check if this CCB needs to
1158 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1160 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1161 ahafreeccb(aha, accb);
1167 accb->flags = ACCB_ACTIVE;
1168 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1169 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1171 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
1174 /* Tell the adapter about this command */
1175 if (aha->cur_outbox->action_code != AMBO_FREE) {
1177 * We should never encounter a busy mailbox.
1178 * If we do, warn the user, and treat it as
1179 * a resource shortage. If the controller is
1180 * hung, one of the pending transactions will
1181 * timeout causing us to start recovery operations.
1183 device_printf(aha->dev,
1184 "Encountered busy mailbox with %d out of %d "
1185 "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1186 callout_stop(&ccb->ccb_h.timeout_ch);
1188 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1189 ahafreeccb(aha, accb);
1190 aha->resource_shortage = TRUE;
1191 xpt_freeze_simq(aha->sim, /*count*/1);
1192 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1197 paddr = ahaccbvtop(aha, accb);
1198 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1199 aha->cur_outbox->action_code = AMBO_START;
1200 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1209 struct aha_softc *aha;
1212 aha = (struct aha_softc *)arg;
1213 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1214 if ((intstat & CMD_COMPLETE) != 0) {
1215 aha->latched_status = aha_inb(aha, STATUS_REG);
1216 aha->command_cmp = TRUE;
1219 aha_outb(aha, CONTROL_REG, RESET_INTR);
1221 if ((intstat & IMB_LOADED) != 0) {
1222 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1224 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1226 ahaccbptov(aha, paddr),
1227 aha->cur_inbox->comp_code);
1228 aha->cur_inbox->comp_code = AMBI_FREE;
1233 if ((intstat & SCSI_BUS_RESET) != 0) {
1234 ahareset(aha, /*hardreset*/FALSE);
1240 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1243 struct ccb_scsiio *csio;
1246 csio = &accb->ccb->csio;
1248 if ((accb->flags & ACCB_ACTIVE) == 0) {
1249 device_printf(aha->dev,
1250 "ahadone - Attempt to free non-active ACCB %p\n",
1255 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1256 bus_dmasync_op_t op;
1258 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1259 op = BUS_DMASYNC_POSTREAD;
1261 op = BUS_DMASYNC_POSTWRITE;
1262 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1263 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1266 if (accb == aha->recovery_accb) {
1268 * The recovery ACCB does not have a CCB associated
1269 * with it, so short circuit the normal error handling.
1270 * We now traverse our list of pending CCBs and process
1271 * any that were terminated by the recovery CCBs action.
1272 * We also reinstate timeouts for all remaining, pending,
1275 struct cam_path *path;
1276 struct ccb_hdr *ccb_h;
1279 /* Notify all clients that a BDR occured */
1280 error = xpt_create_path(&path, /*periph*/NULL,
1281 cam_sim_path(aha->sim),
1285 if (error == CAM_REQ_CMP)
1286 xpt_async(AC_SENT_BDR, path, NULL);
1288 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1289 while (ccb_h != NULL) {
1290 struct aha_ccb *pending_accb;
1292 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1293 if (pending_accb->hccb.target == accb->hccb.target) {
1294 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1295 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1296 ahadone(aha, pending_accb, AMBI_ERROR);
1298 callout_reset(&ccb_h->timeout_ch,
1299 (ccb_h->timeout * hz) / 1000,
1300 ahatimeout, pending_accb);
1301 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1304 device_printf(aha->dev, "No longer in timeout\n");
1308 callout_stop(&ccb->ccb_h.timeout_ch);
1310 switch (comp_code) {
1312 device_printf(aha->dev,
1313 "ahadone - CCB completed with free status!\n");
1315 case AMBI_NOT_FOUND:
1316 device_printf(aha->dev,
1317 "ahadone - CCB Abort failed to find CCB\n");
1321 /* An error occured */
1322 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1325 csio->resid = aha_a24tou(accb->hccb.data_len);
1326 switch(accb->hccb.ahastat) {
1327 case AHASTAT_DATARUN_ERROR:
1329 if (csio->resid <= 0) {
1330 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1335 case AHASTAT_NOERROR:
1336 csio->scsi_status = accb->hccb.sdstat;
1337 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1338 switch(csio->scsi_status) {
1339 case SCSI_STATUS_CHECK_COND:
1340 case SCSI_STATUS_CMD_TERMINATED:
1341 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1343 * The aha writes the sense data at different
1344 * offsets based on the scsi cmd len
1346 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1348 (caddr_t) &csio->sense_data,
1349 accb->hccb.sense_len);
1353 case SCSI_STATUS_OK:
1354 csio->ccb_h.status = CAM_REQ_CMP;
1358 case AHASTAT_SELTIMEOUT:
1359 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1361 case AHASTAT_UNEXPECTED_BUSFREE:
1362 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1364 case AHASTAT_INVALID_PHASE:
1365 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1367 case AHASTAT_INVALID_ACTION_CODE:
1368 panic("%s: Inavlid Action code", aha_name(aha));
1370 case AHASTAT_INVALID_OPCODE:
1371 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1372 panic("%s: Invalid CCB Opcode %x hccb = %p",
1373 aha_name(aha), accb->hccb.opcode,
1375 device_printf(aha->dev,
1376 "AHA-1540A detected, compensating\n");
1377 aha->ccb_sg_opcode = INITIATOR_SG_CCB;
1378 aha->ccb_ccb_opcode = INITIATOR_CCB;
1379 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1380 csio->ccb_h.status = CAM_REQUEUE_REQ;
1382 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1383 /* We don't even support linked commands... */
1384 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1386 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1387 panic("%s: Invalid CCB or SG list", aha_name(aha));
1389 case AHASTAT_HA_SCSI_BUS_RESET:
1390 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1392 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1394 case AHASTAT_HA_BDR:
1395 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1396 csio->ccb_h.status = CAM_BDR_SENT;
1398 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1401 if (csio->ccb_h.status != CAM_REQ_CMP) {
1402 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1403 csio->ccb_h.status |= CAM_DEV_QFRZN;
1405 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1406 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1407 ahafreeccb(aha, accb);
1411 /* All completed without incident */
1412 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1413 /* I don't think so since it works???? */
1414 ccb->ccb_h.status |= CAM_REQ_CMP;
1415 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1416 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1417 ahafreeccb(aha, accb);
1424 ahareset(struct aha_softc* aha, int hard_reset)
1426 struct ccb_hdr *ccb_h;
1429 u_int8_t reset_type;
1431 if (hard_reset != 0)
1432 reset_type = HARD_RESET;
1434 reset_type = SOFT_RESET;
1435 aha_outb(aha, CONTROL_REG, reset_type);
1437 /* Wait 5sec. for Diagnostic start */
1438 timeout = 5 * 10000;
1440 status = aha_inb(aha, STATUS_REG);
1441 if ((status & DIAG_ACTIVE) != 0)
1446 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1447 "assert. status = 0x%x\n", status));
1451 /* Wait 10sec. for Diagnostic end */
1452 timeout = 10 * 10000;
1454 status = aha_inb(aha, STATUS_REG);
1455 if ((status & DIAG_ACTIVE) == 0)
1460 panic("%s: ahareset - Diagnostic Active failed to drop. "
1461 "status = 0x%x\n", aha_name(aha), status);
1465 /* Wait for the host adapter to become ready or report a failure */
1468 status = aha_inb(aha, STATUS_REG);
1469 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1474 device_printf(aha->dev, "ahareset - Host adapter failed to "
1475 "come ready. status = 0x%x\n", status);
1479 /* If the diagnostics failed, tell the user */
1480 if ((status & DIAG_FAIL) != 0
1481 || (status & HA_READY) == 0) {
1482 device_printf(aha->dev, "ahareset - Adapter failed diagnostics\n");
1483 if ((status & DATAIN_REG_READY) != 0)
1484 device_printf(aha->dev, "ahareset - Host Adapter "
1485 "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1489 /* If we've allocated mailboxes, initialize them */
1490 if (aha->init_level > 4)
1493 /* If we've attached to the XPT, tell it about the event */
1494 if (aha->path != NULL)
1495 xpt_async(AC_BUS_RESET, aha->path, NULL);
1498 * Perform completion processing for all outstanding CCBs.
1500 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1501 struct aha_ccb *pending_accb;
1503 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1504 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1505 ahadone(aha, pending_accb, AMBI_ERROR);
1512 * Send a command to the adapter.
1515 aha_cmd(struct aha_softc *aha, aha_op_t opcode, u_int8_t *params,
1516 u_int param_len, u_int8_t *reply_data, u_int reply_len,
1523 u_int reply_buf_size;
1527 /* No data returned to start */
1528 reply_buf_size = reply_len;
1536 * All commands except for the "start mailbox" and the "enable
1537 * outgoing mailbox read interrupt" commands cannot be issued
1538 * while there are pending transactions. Freeze our SIMQ
1539 * and wait for all completions to occur if necessary.
1543 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1544 /* Fire the interrupt handler in case interrupts are blocked */
1553 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter idle\n");
1556 aha->command_cmp = 0;
1558 * Wait up to 10 sec. for the adapter to become
1559 * ready to accept commands.
1564 status = aha_inb(aha, STATUS_REG);
1565 if ((status & HA_READY) != 0
1566 && (status & CMD_REG_BUSY) == 0)
1569 * Throw away any pending data which may be
1570 * left over from earlier commands that we
1573 if ((status & DATAIN_REG_READY) != 0)
1574 (void)aha_inb(aha, DATAIN_REG);
1578 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter "
1579 "ready, status = 0x%x\n", status);
1584 * Send the opcode followed by any necessary parameter bytes.
1586 aha_outb(aha, COMMAND_REG, opcode);
1589 * Wait for up to 1sec to get the parameter list sent
1592 while (param_len && --timeout) {
1595 status = aha_inb(aha, STATUS_REG);
1596 intstat = aha_inb(aha, INTSTAT_REG);
1599 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1600 == (INTR_PENDING|CMD_COMPLETE)) {
1601 saved_status = status;
1606 if (aha->command_cmp != 0) {
1607 saved_status = aha->latched_status;
1611 if ((status & DATAIN_REG_READY) != 0)
1613 if ((status & CMD_REG_BUSY) == 0) {
1614 aha_outb(aha, COMMAND_REG, *params++);
1620 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1621 "status = 0x%x\n", status);
1626 * For all other commands, we wait for any output data
1627 * and the final comand completion interrupt.
1629 while (cmd_complete == 0 && --cmd_timeout) {
1632 status = aha_inb(aha, STATUS_REG);
1633 intstat = aha_inb(aha, INTSTAT_REG);
1636 if (aha->command_cmp != 0) {
1638 saved_status = aha->latched_status;
1639 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1640 == (INTR_PENDING|CMD_COMPLETE)) {
1642 * Our poll (in case interrupts are blocked)
1643 * saw the CMD_COMPLETE interrupt.
1646 saved_status = status;
1648 if ((status & DATAIN_REG_READY) != 0) {
1651 data = aha_inb(aha, DATAIN_REG);
1652 if (reply_len < reply_buf_size) {
1653 *reply_data++ = data;
1655 device_printf(aha->dev, "aha_cmd - Discarded reply data "
1656 "byte for opcode 0x%x\n", opcode);
1659 * Reset timeout to ensure at least a second
1660 * between response bytes.
1662 cmd_timeout = MAX(cmd_timeout, 10000);
1667 if (cmd_timeout == 0) {
1668 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1669 "intstat = 0x%x, reply_len = %d\n", status, intstat,
1675 * Clear any pending interrupts. Block interrupts so our
1676 * interrupt handler is not re-entered.
1686 * If the command was rejected by the controller, tell the caller.
1688 if ((saved_status & CMD_INVALID) != 0) {
1689 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1691 * Some early adapters may not recover properly from
1692 * an invalid command. If it appears that the controller
1693 * has wedged (i.e. status was not cleared by our interrupt
1694 * reset above), perform a soft reset.
1697 status = aha_inb(aha, STATUS_REG);
1698 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1699 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1700 || (status & (HA_READY|INIT_REQUIRED))
1701 != (HA_READY|INIT_REQUIRED)) {
1702 ahareset(aha, /*hard_reset*/FALSE);
1707 if (param_len > 0) {
1708 /* The controller did not accept the full argument list */
1709 PRVERB((aha->dev, "Controller did not accept full argument "
1710 "list (%d > 0)\n", param_len));
1714 if (reply_len != reply_buf_size) {
1715 /* Too much or too little data received */
1716 PRVERB((aha->dev,"data received mistmatch (%d != %d)\n",
1717 reply_len, reply_buf_size));
1721 /* We were successful */
1726 ahainitmboxes(struct aha_softc *aha)
1729 init_24b_mbox_params_t init_mbox;
1731 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1732 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1733 aha->cur_inbox = aha->in_boxes;
1734 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1735 aha->cur_outbox = aha->out_boxes;
1736 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1738 /* Tell the adapter about them */
1739 init_mbox.num_mboxes = aha->num_boxes;
1740 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1741 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (u_int8_t *)&init_mbox,
1742 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1743 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1746 device_printf(aha->dev,
1747 "ahainitmboxes: Initialization command failed\n");
1752 * Update the XPT's idea of the negotiated transfer
1753 * parameters for a particular target.
1756 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1758 setup_data_t setup_info;
1764 targ_syncinfo_t sync_info;
1766 target = cts->ccb_h.target_id;
1767 targ_offset = (target & 0x7);
1770 * Inquire Setup Information. This command retreives
1771 * the sync info for older models.
1773 param = sizeof(setup_info);
1774 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1775 (u_int8_t*)&setup_info, sizeof(setup_info),
1776 DEFAULT_CMD_TIMEOUT);
1779 device_printf(aha->dev,
1780 "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1785 sync_info = setup_info.syncinfo[targ_offset];
1787 if (sync_info.sync == 0)
1788 cts->sync_offset = 0;
1790 cts->sync_offset = sync_info.offset;
1792 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1794 if (aha->boardid >= BOARD_1542CF)
1798 sync_period += 500 * sync_info.period;
1800 /* Convert ns value to standard SCSI sync rate */
1801 if (cts->sync_offset != 0)
1802 cts->sync_period = scsi_calc_syncparam(sync_period);
1804 cts->sync_period = 0;
1806 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1807 | CCB_TRANS_SYNC_OFFSET_VALID
1808 | CCB_TRANS_BUS_WIDTH_VALID;
1809 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1813 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1815 struct aha_softc* aha;
1817 aha = (struct aha_softc*)arg;
1818 aha->mailbox_physbase = segs->ds_addr;
1822 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1824 struct aha_softc* aha;
1826 aha = (struct aha_softc*)arg;
1827 aha->aha_ccb_physbase = segs->ds_addr;
1831 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1834 struct aha_softc* aha;
1836 aha = (struct aha_softc*)arg;
1837 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1841 ahapoll(struct cam_sim *sim)
1843 aha_intr(cam_sim_softc(sim));
1847 ahatimeout(void *arg)
1849 struct aha_ccb *accb;
1851 struct aha_softc *aha;
1854 accb = (struct aha_ccb *)arg;
1856 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1857 xpt_print_path(ccb->ccb_h.path);
1858 printf("CCB %p - timed out\n", (void *)accb);
1862 if ((accb->flags & ACCB_ACTIVE) == 0) {
1863 xpt_print_path(ccb->ccb_h.path);
1864 printf("CCB %p - timed out CCB already completed\n",
1871 * In order to simplify the recovery process, we ask the XPT
1872 * layer to halt the queue of new transactions and we traverse
1873 * the list of pending CCBs and remove their timeouts. This
1874 * means that the driver attempts to clear only one error
1875 * condition at a time. In general, timeouts that occur
1876 * close together are related anyway, so there is no benefit
1877 * in attempting to handle errors in parrallel. Timeouts will
1878 * be reinstated when the recovery process ends.
1880 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1881 struct ccb_hdr *ccb_h;
1883 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1884 xpt_freeze_simq(aha->sim, /*count*/1);
1885 accb->flags |= ACCB_RELEASE_SIMQ;
1888 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1889 while (ccb_h != NULL) {
1890 struct aha_ccb *pending_accb;
1892 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1893 callout_stop(&ccb_h->timeout_ch);
1894 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1898 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1899 || aha->cur_outbox->action_code != AMBO_FREE) {
1901 * Try a full host adapter/SCSI bus reset.
1902 * We do this only if we have already attempted
1903 * to clear the condition with a BDR, or we cannot
1904 * attempt a BDR for lack of mailbox resources.
1906 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1907 ahareset(aha, /*hardreset*/TRUE);
1908 device_printf(aha->dev, "No longer in timeout\n");
1911 * Send a Bus Device Reset message:
1912 * The target that is holding up the bus may not
1913 * be the same as the one that triggered this timeout
1914 * (different commands have different timeout lengths),
1915 * but we have no way of determining this from our
1916 * timeout handler. Our strategy here is to queue a
1917 * BDR message to the target of the timed out command.
1918 * If this fails, we'll get another timeout 2 seconds
1919 * later which will attempt a bus reset.
1921 accb->flags |= ACCB_DEVICE_RESET;
1922 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, ahatimeout, accb);
1923 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1925 /* No Data Transfer */
1926 aha->recovery_accb->hccb.datain = TRUE;
1927 aha->recovery_accb->hccb.dataout = TRUE;
1928 aha->recovery_accb->hccb.ahastat = 0;
1929 aha->recovery_accb->hccb.sdstat = 0;
1930 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1932 /* Tell the adapter about this command */
1933 paddr = ahaccbvtop(aha, aha->recovery_accb);
1934 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1935 aha->cur_outbox->action_code = AMBO_START;
1936 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1944 aha_detach(struct aha_softc *aha)
1946 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1947 xpt_free_path(aha->path);
1948 xpt_bus_deregister(cam_sim_path(aha->sim));
1949 cam_sim_free(aha->sim);