2 * Generic register and struct definitions for the Adaptech 154x/164x
3 * SCSI host adapters. Product specific probe and attach routines can
5 * aha 1542A/1542B/1542C/1542CF/1542CP aha_isa.c
9 * Copyright (c) 1998 M. Warner Losh.
10 * All Rights Reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Derived from bt.c written by:
35 * Copyright (c) 1998 Justin T. Gibbs.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions, and the following disclaimer,
43 * without modification, immediately at the beginning of the file.
44 * 2. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
51 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
69 #include <sys/module.h>
70 #include <sys/mutex.h>
72 #include <machine/bus.h>
75 #include <cam/cam_ccb.h>
76 #include <cam/cam_sim.h>
77 #include <cam/cam_xpt_sim.h>
78 #include <cam/cam_debug.h>
80 #include <cam/scsi/scsi_message.h>
82 #include <dev/aha/ahareg.h>
84 #define PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
86 /* Macro to determine that a rev is potentially a new valid one
87 * so that the driver doesn't keep breaking on new revs as it
88 * did for the CF and CP.
90 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
92 /* MailBox Management functions */
93 static __inline void ahanextinbox(struct aha_softc *aha);
94 static __inline void ahanextoutbox(struct aha_softc *aha);
96 #define aha_name(aha) device_get_nameunit(aha->dev)
99 ahanextinbox(struct aha_softc *aha)
101 if (aha->cur_inbox == aha->last_inbox)
102 aha->cur_inbox = aha->in_boxes;
108 ahanextoutbox(struct aha_softc *aha)
110 if (aha->cur_outbox == aha->last_outbox)
111 aha->cur_outbox = aha->out_boxes;
116 #define ahautoa24(u,s3) \
117 (s3)[0] = ((u) >> 16) & 0xff; \
118 (s3)[1] = ((u) >> 8) & 0xff; \
119 (s3)[2] = (u) & 0xff;
121 #define aha_a24tou(s3) \
122 (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
124 /* CCB Management functions */
125 static __inline uint32_t ahaccbvtop(struct aha_softc *aha,
126 struct aha_ccb *accb);
127 static __inline struct aha_ccb* ahaccbptov(struct aha_softc *aha,
130 static __inline uint32_t
131 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
133 return (aha->aha_ccb_physbase
134 + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
136 static __inline struct aha_ccb *
137 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
139 return (aha->aha_ccb_array +
140 + ((struct aha_ccb*)(uintptr_t)ccb_addr -
141 (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
144 static struct aha_ccb* ahagetccb(struct aha_softc *aha);
145 static __inline void ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
146 static void ahaallocccbs(struct aha_softc *aha);
147 static bus_dmamap_callback_t ahaexecuteccb;
148 static void ahadone(struct aha_softc *aha, struct aha_ccb *accb,
149 aha_mbi_comp_code_t comp_code);
151 /* Host adapter command functions */
152 static int ahareset(struct aha_softc* aha, int hard_reset);
154 /* Initialization functions */
155 static int ahainitmboxes(struct aha_softc *aha);
156 static bus_dmamap_callback_t ahamapmboxes;
157 static bus_dmamap_callback_t ahamapccbs;
158 static bus_dmamap_callback_t ahamapsgs;
160 /* Transfer Negotiation Functions */
161 static void ahafetchtransinfo(struct aha_softc *aha,
162 struct ccb_trans_settings *cts);
164 /* CAM SIM entry points */
165 #define ccb_accb_ptr spriv_ptr0
166 #define ccb_aha_ptr spriv_ptr1
167 static void ahaaction(struct cam_sim *sim, union ccb *ccb);
168 static void ahapoll(struct cam_sim *sim);
170 /* Our timeout handler */
171 static timeout_t ahatimeout;
173 /* Exported functions */
175 aha_alloc(struct aha_softc *aha, int unit, bus_space_tag_t tag,
176 bus_space_handle_t bsh)
179 SLIST_INIT(&aha->free_aha_ccbs);
180 LIST_INIT(&aha->pending_ccbs);
181 SLIST_INIT(&aha->sg_maps);
185 aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
186 aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
190 aha_free(struct aha_softc *aha)
192 switch (aha->init_level) {
196 struct sg_map_node *sg_map;
198 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
199 SLIST_REMOVE_HEAD(&aha->sg_maps, links);
200 bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
201 bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
203 free(sg_map, M_DEVBUF);
205 bus_dma_tag_destroy(aha->sg_dmat);
208 bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
210 bus_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);
211 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
214 bus_dma_tag_destroy(aha->ccb_dmat);
216 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
218 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
219 aha->mailbox_dmamap);
220 bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);
222 bus_dma_tag_destroy(aha->buffer_dmat);
224 bus_dma_tag_destroy(aha->mailbox_dmat);
231 * Probe the adapter and verify that the card is an Adaptec.
234 aha_probe(struct aha_softc* aha)
239 board_id_data_t board_id;
242 * See if the three I/O ports look reasonable.
243 * Touch the minimal number of registers in the
246 status = aha_inb(aha, STATUS_REG);
248 (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
249 PRVERB((aha->dev, "status reg test failed %x\n", status));
253 intstat = aha_inb(aha, INTSTAT_REG);
254 if ((intstat & INTSTAT_REG_RSVD) != 0) {
255 PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
260 * Looking good so far. Final test is to reset the
261 * adapter and fetch the board ID and ensure we aren't
262 * looking at a BusLogic.
264 if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
265 PRVERB((aha->dev, "Failed Reset\n"));
270 * Get the board ID. We use this to see if we're dealing with
271 * a buslogic card or an aha card (or clone).
273 error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
274 (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
276 PRVERB((aha->dev, "INQUIRE failed %x\n", error));
279 aha->fw_major = board_id.firmware_rev_major;
280 aha->fw_minor = board_id.firmware_rev_minor;
281 aha->boardid = board_id.board_type;
284 * The Buslogic cards have an id of either 0x41 or 0x42. So
285 * if those come up in the probe, we test the geometry register
286 * of the board. Adaptec boards that are this old will not have
287 * this register, and return 0xff, while buslogic cards will return
288 * something different.
290 * It appears that for reasons unknow, for the for the
291 * aha-1542B cards, we need to wait a little bit before trying
292 * to read the geometry register. I picked 10ms since we have
293 * reports that a for loop to 1000 did the trick, and this
294 * errs on the side of conservatism. Besides, no one will
295 * notice a 10mS delay here, even the 1542B card users :-)
297 * Some compatible cards return 0 here. Some cards also
298 * seem to return 0x7f.
300 * XXX I'm not sure how this will impact other cloned cards
302 * This really should be replaced with the esetup command, since
303 * that appears to be more reliable. This becomes more and more
304 * true over time as we discover more cards that don't read the
305 * geometry register consistantly.
307 if (aha->boardid <= 0x42) {
308 /* Wait 10ms before reading */
310 status = aha_inb(aha, GEOMETRY_REG);
311 if (status != 0xff && status != 0x00 && status != 0x7f) {
312 PRVERB((aha->dev, "Geometry Register test failed %#x\n",
322 * Pull the boards setup information and record it in our softc.
325 aha_fetch_adapter_info(struct aha_softc *aha)
327 setup_data_t setup_info;
328 config_data_t config_data;
329 uint8_t length_param;
331 struct aha_extbios extbios;
333 switch (aha->boardid) {
334 case BOARD_1540_16HEAD_BIOS:
335 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
337 case BOARD_1540_64HEAD_BIOS:
338 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
341 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
344 snprintf(aha->model, sizeof(aha->model), "1640");
347 snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");
350 snprintf(aha->model, sizeof(aha->model), "1542C");
353 snprintf(aha->model, sizeof(aha->model), "1542CF");
356 snprintf(aha->model, sizeof(aha->model), "1542CP");
359 snprintf(aha->model, sizeof(aha->model), "Unknown");
363 * If we are a new type of 1542 board (anything newer than a 1542C)
364 * then disable the extended bios so that the
365 * mailbox interface is unlocked.
366 * This is also true for the 1542B Version 3.20. First Adaptec
367 * board that supports >1Gb drives.
368 * No need to check the extended bios flags as some of the
369 * extensions that cause us problems are not flagged in that byte.
371 if (PROBABLY_NEW_BOARD(aha->boardid) ||
372 (aha->boardid == 0x41
373 && aha->fw_major == 0x31 &&
374 aha->fw_minor >= 0x34)) {
375 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
376 /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
377 DEFAULT_CMD_TIMEOUT);
379 device_printf(aha->dev,
380 "AOP_RETURN_EXT_BIOS_INFO - Failed.");
383 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
384 /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
386 device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
390 if (aha->boardid < 0x41)
391 device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
393 aha->max_sg = 17; /* Need >= 17 to do 64k I/O */
395 aha->extended_lun = 0;
396 aha->extended_trans = 0;
398 /* Determine Sync/Wide/Disc settings */
399 length_param = sizeof(setup_info);
400 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
401 /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
402 DEFAULT_CMD_TIMEOUT);
404 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
408 if (setup_info.initiate_sync != 0) {
409 aha->sync_permitted = ALL_TARGETS;
411 aha->disc_permitted = ALL_TARGETS;
413 /* We need as many mailboxes as we can have ccbs */
414 aha->num_boxes = aha->max_ccbs;
416 /* Determine our SCSI ID */
417 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
418 (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
420 device_printf(aha->dev,
421 "aha_fetch_adapter_info - Failed Get Config\n");
424 aha->scsi_id = config_data.scsi_id;
429 * Start the board, ready for normal operation
432 aha_init(struct aha_softc* aha)
434 /* Announce the Adapter */
435 device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
436 aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
438 if (aha->diff_bus != 0)
441 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
445 * Create our DMA tags. These tags define the kinds of device
446 * accessible memory allocations and memory mappings we will
447 * need to perform during normal operation.
449 * Unless we need to further restrict the allocation, we rely
450 * on the restrictions of the parent dmat, hence the common
451 * use of MAXADDR and MAXSIZE.
454 /* DMA tag for mapping buffers into device visible space. */
455 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
458 /* lowaddr */ BUS_SPACE_MAXADDR,
459 /* highaddr */ BUS_SPACE_MAXADDR,
461 /* filterarg */ NULL,
462 /* maxsize */ MAXBSIZE,
463 /* nsegments */ AHA_NSEG,
464 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
465 /* flags */ BUS_DMA_ALLOCNOW,
466 /* lockfunc */ busdma_lock_mutex,
467 /* lockarg */ &Giant,
468 &aha->buffer_dmat) != 0) {
473 /* DMA tag for our mailboxes */
474 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
477 /* lowaddr */ BUS_SPACE_MAXADDR,
478 /* highaddr */ BUS_SPACE_MAXADDR,
480 /* filterarg */ NULL,
481 /* maxsize */ aha->num_boxes *
482 (sizeof(aha_mbox_in_t) +
483 sizeof(aha_mbox_out_t)),
485 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
487 /* lockfunc */ busdma_lock_mutex,
488 /* lockarg */ &Giant,
489 &aha->mailbox_dmat) != 0) {
495 /* Allocation for our mailboxes */
496 if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
497 BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
502 /* And permanently map them */
503 bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
504 aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
505 sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
509 aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
513 /* DMA tag for our ccb structures */
514 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
517 /* lowaddr */ BUS_SPACE_MAXADDR,
518 /* highaddr */ BUS_SPACE_MAXADDR,
520 /* filterarg */ NULL,
521 /* maxsize */ aha->max_ccbs *
522 sizeof(struct aha_ccb),
524 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
526 /* lockfunc */ busdma_lock_mutex,
527 /* lockarg */ &Giant,
528 &aha->ccb_dmat) != 0) {
534 /* Allocation for our ccbs */
535 if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
536 BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
541 /* And permanently map them */
542 bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
543 aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
547 /* DMA tag for our S/G structures. We allocate in page sized chunks */
548 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
551 /* lowaddr */ BUS_SPACE_MAXADDR,
552 /* highaddr */ BUS_SPACE_MAXADDR,
554 /* filterarg */ NULL,
555 /* maxsize */ PAGE_SIZE,
557 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
559 /* lockfunc */ busdma_lock_mutex,
560 /* lockarg */ &Giant,
566 /* Perform initial CCB allocation */
567 bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
570 if (aha->num_ccbs == 0) {
571 device_printf(aha->dev,
572 "aha_init - Unable to allocate initial ccbs\n");
577 * Note that we are going and return (to probe)
587 aha_attach(struct aha_softc *aha)
589 int tagged_dev_openings;
590 struct cam_devq *devq;
593 * We don't do tagged queueing, since the aha cards don't
596 tagged_dev_openings = 0;
599 * Create the device queue for our SIM.
601 devq = cam_simq_alloc(aha->max_ccbs - 1);
606 * Construct our SIM entry
608 aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha, aha->unit,
609 &Giant, 2, tagged_dev_openings, devq);
610 if (aha->sim == NULL) {
614 if (xpt_bus_register(aha->sim, aha->dev, 0) != CAM_SUCCESS) {
615 cam_sim_free(aha->sim, /*free_devq*/TRUE);
618 if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
619 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
620 xpt_bus_deregister(cam_sim_path(aha->sim));
621 cam_sim_free(aha->sim, /*free_devq*/TRUE);
629 ahaallocccbs(struct aha_softc *aha)
631 struct aha_ccb *next_ccb;
632 struct sg_map_node *sg_map;
638 next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
640 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
645 /* Allocate S/G space for the next batch of CCBS */
646 if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
647 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
648 free(sg_map, M_DEVBUF);
652 SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
654 bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
655 PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
657 segs = sg_map->sg_vaddr;
658 physaddr = sg_map->sg_physaddr;
660 newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
661 for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
664 next_ccb->sg_list = segs;
665 next_ccb->sg_list_phys = physaddr;
666 next_ccb->flags = ACCB_FREE;
667 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
671 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
673 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
678 /* Reserve a CCB for error recovery */
679 if (aha->recovery_accb == NULL) {
680 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
681 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
686 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
691 if ((accb->flags & ACCB_ACTIVE) != 0)
692 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
693 if (aha->resource_shortage != 0
694 && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
695 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
696 aha->resource_shortage = FALSE;
698 accb->flags = ACCB_FREE;
699 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
704 static struct aha_ccb*
705 ahagetccb(struct aha_softc *aha)
707 struct aha_ccb* accb;
711 if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
712 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
714 } else if (aha->num_ccbs < aha->max_ccbs) {
716 accb = SLIST_FIRST(&aha->free_aha_ccbs);
718 device_printf(aha->dev, "Can't malloc ACCB\n");
720 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
730 ahaaction(struct cam_sim *sim, union ccb *ccb)
732 struct aha_softc *aha;
735 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
737 aha = (struct aha_softc *)cam_sim_softc(sim);
739 switch (ccb->ccb_h.func_code) {
740 /* Common cases first */
741 case XPT_SCSI_IO: /* Execute the requested I/O operation */
742 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ {
743 struct aha_ccb *accb;
744 struct aha_hccb *hccb;
747 * Get an accb to use.
749 if ((accb = ahagetccb(aha)) == NULL) {
751 aha->resource_shortage = TRUE;
753 xpt_freeze_simq(aha->sim, /*count*/1);
754 ccb->ccb_h.status = CAM_REQUEUE_REQ;
761 * So we can find the ACCB when an abort is requested
764 ccb->ccb_h.ccb_accb_ptr = accb;
765 ccb->ccb_h.ccb_aha_ptr = aha;
768 * Put all the arguments for the xfer in the accb
770 hccb->target = ccb->ccb_h.target_id;
771 hccb->lun = ccb->ccb_h.target_lun;
775 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
776 struct ccb_scsiio *csio;
777 struct ccb_hdr *ccbh;
781 hccb->opcode = aha->ccb_ccb_opcode;
782 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
783 hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
784 hccb->cmd_len = csio->cdb_len;
785 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
786 ccb->ccb_h.status = CAM_REQ_INVALID;
787 ahafreeccb(aha, accb);
791 hccb->sense_len = csio->sense_len;
792 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
793 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
794 bcopy(csio->cdb_io.cdb_ptr,
795 hccb->scsi_cdb, hccb->cmd_len);
797 /* I guess I could map it in... */
798 ccbh->status = CAM_REQ_INVALID;
799 ahafreeccb(aha, accb);
804 bcopy(csio->cdb_io.cdb_bytes,
805 hccb->scsi_cdb, hccb->cmd_len);
808 * If we have any data to send with this command,
809 * map it into bus space.
811 /* Only use S/G if there is a transfer */
812 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
813 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
815 * We've been given a pointer
816 * to a single buffer.
818 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
822 error = bus_dmamap_load(
830 if (error == EINPROGRESS) {
833 * ordering, freeze the
835 * until our mapping is
838 xpt_freeze_simq(aha->sim,
840 csio->ccb_h.status |=
845 struct bus_dma_segment seg;
847 /* Pointer to physical buffer */
849 (bus_addr_t)csio->data_ptr;
850 seg.ds_len = csio->dxfer_len;
851 ahaexecuteccb(accb, &seg, 1, 0);
854 struct bus_dma_segment *segs;
856 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
857 panic("ahaaction - Physical "
861 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
862 panic("ahaaction - Virtual "
866 /* Just use the segments provided */
867 segs = (struct bus_dma_segment *)
869 ahaexecuteccb(accb, segs,
870 csio->sglist_cnt, 0);
873 ahaexecuteccb(accb, NULL, 0, 0);
876 hccb->opcode = INITIATOR_BUS_DEV_RESET;
877 /* No data transfer */
879 hccb->dataout = TRUE;
882 ahaexecuteccb(accb, NULL, 0, 0);
886 case XPT_EN_LUN: /* Enable LUN as a target */
887 case XPT_TARGET_IO: /* Execute target I/O request */
888 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
889 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
890 case XPT_ABORT: /* Abort the specified CCB */
892 ccb->ccb_h.status = CAM_REQ_INVALID;
895 case XPT_SET_TRAN_SETTINGS:
897 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
900 case XPT_GET_TRAN_SETTINGS:
901 /* Get default/user set transfer settings for the target */
903 struct ccb_trans_settings *cts = &ccb->cts;
904 u_int target_mask = 0x01 << ccb->ccb_h.target_id;
905 struct ccb_trans_settings_scsi *scsi =
906 &cts->proto_specific.scsi;
907 struct ccb_trans_settings_spi *spi =
908 &cts->xport_specific.spi;
910 cts->protocol = PROTO_SCSI;
911 cts->protocol_version = SCSI_REV_2;
912 cts->transport = XPORT_SPI;
913 cts->transport_version = 2;
914 if (cts->type == CTS_TYPE_USER_SETTINGS) {
916 if ((aha->disc_permitted & target_mask) != 0)
917 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
918 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
919 if ((aha->sync_permitted & target_mask) != 0) {
920 if (aha->boardid >= BOARD_1542CF)
921 spi->sync_period = 25;
923 spi->sync_period = 50;
925 spi->sync_period = 0;
928 if (spi->sync_period != 0)
929 spi->sync_offset = 15;
931 spi->valid = CTS_SPI_VALID_SYNC_RATE
932 | CTS_SPI_VALID_SYNC_OFFSET
933 | CTS_SPI_VALID_BUS_WIDTH
934 | CTS_SPI_VALID_DISC;
935 scsi->valid = CTS_SCSI_VALID_TQ;
937 ahafetchtransinfo(aha, cts);
940 ccb->ccb_h.status = CAM_REQ_CMP;
944 case XPT_CALC_GEOMETRY:
946 struct ccb_calc_geometry *ccg;
948 uint32_t secs_per_cylinder;
951 size_mb = ccg->volume_size
952 / ((1024L * 1024L) / ccg->block_size);
953 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
954 if (size_mb >= 2048) {
956 ccg->secs_per_track = 63;
959 ccg->secs_per_track = 32;
963 ccg->secs_per_track = 32;
965 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
966 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
967 ccb->ccb_h.status = CAM_REQ_CMP;
971 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
972 ahareset(aha, /*hardreset*/TRUE);
973 ccb->ccb_h.status = CAM_REQ_CMP;
976 case XPT_TERM_IO: /* Terminate the I/O process */
978 ccb->ccb_h.status = CAM_REQ_INVALID;
981 case XPT_PATH_INQ: /* Path routing inquiry */
983 struct ccb_pathinq *cpi = &ccb->cpi;
985 cpi->version_num = 1; /* XXX??? */
986 cpi->hba_inquiry = PI_SDTR_ABLE;
987 cpi->target_sprt = 0;
989 cpi->hba_eng_cnt = 0;
992 cpi->initiator_id = aha->scsi_id;
993 cpi->bus_id = cam_sim_bus(sim);
994 cpi->base_transfer_speed = 3300;
995 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
996 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
997 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
998 cpi->unit_number = cam_sim_unit(sim);
999 cpi->transport = XPORT_SPI;
1000 cpi->transport_version = 2;
1001 cpi->protocol = PROTO_SCSI;
1002 cpi->protocol_version = SCSI_REV_2;
1003 cpi->ccb_h.status = CAM_REQ_CMP;
1008 ccb->ccb_h.status = CAM_REQ_INVALID;
1015 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1017 struct aha_ccb *accb;
1019 struct aha_softc *aha;
1023 accb = (struct aha_ccb *)arg;
1025 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1029 device_printf(aha->dev,
1030 "Unexepected error 0x%x returned from "
1031 "bus_dmamap_load\n", error);
1032 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1033 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1034 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1036 ahafreeccb(aha, accb);
1043 bus_dma_segment_t *end_seg;
1044 bus_dmasync_op_t op;
1046 end_seg = dm_segs + nseg;
1048 /* Copy the segments into our SG list */
1050 while (dm_segs < end_seg) {
1051 ahautoa24(dm_segs->ds_len, sg->len);
1052 ahautoa24(dm_segs->ds_addr, sg->addr);
1058 accb->hccb.opcode = aha->ccb_sg_opcode;
1059 ahautoa24((sizeof(aha_sg_t) * nseg),
1060 accb->hccb.data_len);
1061 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1063 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1064 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1067 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1068 op = BUS_DMASYNC_PREREAD;
1070 op = BUS_DMASYNC_PREWRITE;
1072 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1075 accb->hccb.opcode = INITIATOR_CCB;
1076 ahautoa24(0, accb->hccb.data_len);
1077 ahautoa24(0, accb->hccb.data_addr);
1083 * Last time we need to check if this CCB needs to
1086 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1088 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1089 ahafreeccb(aha, accb);
1095 accb->flags = ACCB_ACTIVE;
1096 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1097 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1099 ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb,
1100 (ccb->ccb_h.timeout * hz) / 1000);
1102 /* Tell the adapter about this command */
1103 if (aha->cur_outbox->action_code != AMBO_FREE) {
1105 * We should never encounter a busy mailbox.
1106 * If we do, warn the user, and treat it as
1107 * a resource shortage. If the controller is
1108 * hung, one of the pending transactions will
1109 * timeout causing us to start recovery operations.
1111 device_printf(aha->dev,
1112 "Encountered busy mailbox with %d out of %d "
1113 "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1114 untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
1116 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1117 ahafreeccb(aha, accb);
1118 aha->resource_shortage = TRUE;
1119 xpt_freeze_simq(aha->sim, /*count*/1);
1120 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1124 paddr = ahaccbvtop(aha, accb);
1125 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1126 aha->cur_outbox->action_code = AMBO_START;
1127 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1136 struct aha_softc *aha;
1140 aha = (struct aha_softc *)arg;
1141 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1142 if ((intstat & CMD_COMPLETE) != 0) {
1143 aha->latched_status = aha_inb(aha, STATUS_REG);
1144 aha->command_cmp = TRUE;
1147 aha_outb(aha, CONTROL_REG, RESET_INTR);
1149 if ((intstat & IMB_LOADED) != 0) {
1150 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1151 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1152 ahadone(aha, ahaccbptov(aha, paddr),
1153 aha->cur_inbox->comp_code);
1154 aha->cur_inbox->comp_code = AMBI_FREE;
1159 if ((intstat & SCSI_BUS_RESET) != 0) {
1160 ahareset(aha, /*hardreset*/FALSE);
1166 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1169 struct ccb_scsiio *csio;
1172 csio = &accb->ccb->csio;
1174 if ((accb->flags & ACCB_ACTIVE) == 0) {
1175 device_printf(aha->dev,
1176 "ahadone - Attempt to free non-active ACCB %p\n",
1181 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1182 bus_dmasync_op_t op;
1184 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1185 op = BUS_DMASYNC_POSTREAD;
1187 op = BUS_DMASYNC_POSTWRITE;
1188 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1189 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1192 if (accb == aha->recovery_accb) {
1194 * The recovery ACCB does not have a CCB associated
1195 * with it, so short circuit the normal error handling.
1196 * We now traverse our list of pending CCBs and process
1197 * any that were terminated by the recovery CCBs action.
1198 * We also reinstate timeouts for all remaining, pending,
1201 struct cam_path *path;
1202 struct ccb_hdr *ccb_h;
1205 /* Notify all clients that a BDR occured */
1206 error = xpt_create_path(&path, /*periph*/NULL,
1207 cam_sim_path(aha->sim), accb->hccb.target,
1210 if (error == CAM_REQ_CMP)
1211 xpt_async(AC_SENT_BDR, path, NULL);
1213 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1214 while (ccb_h != NULL) {
1215 struct aha_ccb *pending_accb;
1217 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1218 if (pending_accb->hccb.target == accb->hccb.target) {
1219 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1220 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1221 ahadone(aha, pending_accb, AMBI_ERROR);
1223 ccb_h->timeout_ch = timeout(ahatimeout,
1224 (caddr_t)pending_accb,
1225 (ccb_h->timeout * hz) / 1000);
1226 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1229 device_printf(aha->dev, "No longer in timeout\n");
1233 untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
1235 switch (comp_code) {
1237 device_printf(aha->dev,
1238 "ahadone - CCB completed with free status!\n");
1240 case AMBI_NOT_FOUND:
1241 device_printf(aha->dev,
1242 "ahadone - CCB Abort failed to find CCB\n");
1246 /* An error occured */
1247 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1250 csio->resid = aha_a24tou(accb->hccb.data_len);
1251 switch(accb->hccb.ahastat) {
1252 case AHASTAT_DATARUN_ERROR:
1254 if (csio->resid <= 0) {
1255 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1260 case AHASTAT_NOERROR:
1261 csio->scsi_status = accb->hccb.sdstat;
1262 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1263 switch(csio->scsi_status) {
1264 case SCSI_STATUS_CHECK_COND:
1265 case SCSI_STATUS_CMD_TERMINATED:
1266 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1268 * The aha writes the sense data at different
1269 * offsets based on the scsi cmd len
1271 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1273 (caddr_t) &csio->sense_data,
1274 accb->hccb.sense_len);
1278 case SCSI_STATUS_OK:
1279 csio->ccb_h.status = CAM_REQ_CMP;
1283 case AHASTAT_SELTIMEOUT:
1284 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1286 case AHASTAT_UNEXPECTED_BUSFREE:
1287 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1289 case AHASTAT_INVALID_PHASE:
1290 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1292 case AHASTAT_INVALID_ACTION_CODE:
1293 panic("%s: Inavlid Action code", aha_name(aha));
1295 case AHASTAT_INVALID_OPCODE:
1296 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1297 panic("%s: Invalid CCB Opcode %x hccb = %p",
1298 aha_name(aha), accb->hccb.opcode,
1300 device_printf(aha->dev,
1301 "AHA-1540A compensation failed\n");
1302 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1303 csio->ccb_h.status = CAM_REQUEUE_REQ;
1305 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1306 /* We don't even support linked commands... */
1307 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1309 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1310 panic("%s: Invalid CCB or SG list", aha_name(aha));
1312 case AHASTAT_HA_SCSI_BUS_RESET:
1313 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1315 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1317 case AHASTAT_HA_BDR:
1318 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1319 csio->ccb_h.status = CAM_BDR_SENT;
1321 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1324 if (csio->ccb_h.status != CAM_REQ_CMP) {
1325 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1326 csio->ccb_h.status |= CAM_DEV_QFRZN;
1328 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1329 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1330 ahafreeccb(aha, accb);
1334 /* All completed without incident */
1335 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1336 /* I don't think so since it works???? */
1337 ccb->ccb_h.status |= CAM_REQ_CMP;
1338 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1339 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1340 ahafreeccb(aha, accb);
1347 ahareset(struct aha_softc* aha, int hard_reset)
1349 struct ccb_hdr *ccb_h;
1354 if (hard_reset != 0)
1355 reset_type = HARD_RESET;
1357 reset_type = SOFT_RESET;
1358 aha_outb(aha, CONTROL_REG, reset_type);
1360 /* Wait 5sec. for Diagnostic start */
1361 timeout = 5 * 10000;
1363 status = aha_inb(aha, STATUS_REG);
1364 if ((status & DIAG_ACTIVE) != 0)
1369 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1370 "assert. status = %#x\n", status));
1374 /* Wait 10sec. for Diagnostic end */
1375 timeout = 10 * 10000;
1377 status = aha_inb(aha, STATUS_REG);
1378 if ((status & DIAG_ACTIVE) == 0)
1383 panic("%s: ahareset - Diagnostic Active failed to drop. "
1384 "status = 0x%x\n", aha_name(aha), status);
1388 /* Wait for the host adapter to become ready or report a failure */
1391 status = aha_inb(aha, STATUS_REG);
1392 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1397 device_printf(aha->dev, "ahareset - Host adapter failed to "
1398 "come ready. status = 0x%x\n", status);
1402 /* If the diagnostics failed, tell the user */
1403 if ((status & DIAG_FAIL) != 0
1404 || (status & HA_READY) == 0) {
1405 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1407 if ((status & DATAIN_REG_READY) != 0)
1408 device_printf(aha->dev, "ahareset - Host Adapter "
1409 "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1413 /* If we've attached to the XPT, tell it about the event */
1414 if (aha->path != NULL)
1415 xpt_async(AC_BUS_RESET, aha->path, NULL);
1418 * Perform completion processing for all outstanding CCBs.
1420 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1421 struct aha_ccb *pending_accb;
1423 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1424 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1425 ahadone(aha, pending_accb, AMBI_ERROR);
1428 /* If we've allocated mailboxes, initialize them */
1429 /* Must be done after we've aborted our queue, or aha_cmd fails */
1430 if (aha->init_level > 4)
1437 * Send a command to the adapter.
1440 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1441 u_int param_len, uint8_t *reply_data, u_int reply_len,
1448 u_int reply_buf_size;
1453 /* No data returned to start */
1454 reply_buf_size = reply_len;
1462 * All commands except for the "start mailbox" and the "enable
1463 * outgoing mailbox read interrupt" commands cannot be issued
1464 * while there are pending transactions. Freeze our SIMQ
1465 * and wait for all completions to occur if necessary.
1469 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1470 /* Fire the interrupt handler in case interrupts are blocked */
1479 device_printf(aha->dev,
1480 "aha_cmd: Timeout waiting for adapter idle\n");
1483 aha->command_cmp = 0;
1485 * Wait up to 10 sec. for the adapter to become
1486 * ready to accept commands.
1490 status = aha_inb(aha, STATUS_REG);
1491 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1494 * Throw away any pending data which may be
1495 * left over from earlier commands that we
1498 if ((status & DATAIN_REG_READY) != 0)
1499 (void)aha_inb(aha, DATAIN_REG);
1503 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1504 " ready, status = 0x%x\n", status);
1509 * Send the opcode followed by any necessary parameter bytes.
1511 aha_outb(aha, COMMAND_REG, opcode);
1514 * Wait for up to 1sec to get the parameter list sent
1517 while (param_len && --timeout) {
1520 status = aha_inb(aha, STATUS_REG);
1521 intstat = aha_inb(aha, INTSTAT_REG);
1524 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1525 == (INTR_PENDING|CMD_COMPLETE)) {
1526 saved_status = status;
1531 if (aha->command_cmp != 0) {
1532 saved_status = aha->latched_status;
1536 if ((status & DATAIN_REG_READY) != 0)
1538 if ((status & CMD_REG_BUSY) == 0) {
1539 aha_outb(aha, COMMAND_REG, *params++);
1545 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1546 "status = 0x%x\n", status);
1551 * For all other commands, we wait for any output data
1552 * and the final comand completion interrupt.
1554 while (cmd_complete == 0 && --cmd_timeout) {
1557 status = aha_inb(aha, STATUS_REG);
1558 intstat = aha_inb(aha, INTSTAT_REG);
1561 if (aha->command_cmp != 0) {
1563 saved_status = aha->latched_status;
1564 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1565 == (INTR_PENDING|CMD_COMPLETE)) {
1567 * Our poll (in case interrupts are blocked)
1568 * saw the CMD_COMPLETE interrupt.
1571 saved_status = status;
1573 if ((status & DATAIN_REG_READY) != 0) {
1576 data = aha_inb(aha, DATAIN_REG);
1577 if (reply_len < reply_buf_size) {
1578 *reply_data++ = data;
1580 device_printf(aha->dev,
1581 "aha_cmd - Discarded reply data "
1582 "byte for opcode 0x%x\n", opcode);
1585 * Reset timeout to ensure at least a second
1586 * between response bytes.
1588 cmd_timeout = MAX(cmd_timeout, 10000);
1593 if (cmd_timeout == 0) {
1594 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1595 "intstat = 0x%x, reply_len = %d\n", status, intstat,
1601 * Clear any pending interrupts. Block interrupts so our
1602 * interrupt handler is not re-entered.
1612 * If the command was rejected by the controller, tell the caller.
1614 if ((saved_status & CMD_INVALID) != 0) {
1615 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1617 * Some early adapters may not recover properly from
1618 * an invalid command. If it appears that the controller
1619 * has wedged (i.e. status was not cleared by our interrupt
1620 * reset above), perform a soft reset.
1623 status = aha_inb(aha, STATUS_REG);
1624 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1625 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1626 || (status & (HA_READY|INIT_REQUIRED))
1627 != (HA_READY|INIT_REQUIRED))
1628 ahareset(aha, /*hard_reset*/FALSE);
1632 if (param_len > 0) {
1633 /* The controller did not accept the full argument list */
1634 PRVERB((aha->dev, "Controller did not accept full argument "
1635 "list (%d > 0)\n", param_len));
1639 if (reply_len != reply_buf_size) {
1640 /* Too much or too little data received */
1641 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1642 reply_len, reply_buf_size));
1646 /* We were successful */
1651 ahainitmboxes(struct aha_softc *aha)
1654 init_24b_mbox_params_t init_mbox;
1656 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1657 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1658 aha->cur_inbox = aha->in_boxes;
1659 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1660 aha->cur_outbox = aha->out_boxes;
1661 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1663 /* Tell the adapter about them */
1664 init_mbox.num_mboxes = aha->num_boxes;
1665 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1666 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1667 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1668 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1671 printf("ahainitmboxes: Initialization command failed\n");
1676 * Update the XPT's idea of the negotiated transfer
1677 * parameters for a particular target.
1680 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1682 setup_data_t setup_info;
1688 targ_syncinfo_t sync_info;
1689 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1691 target = cts->ccb_h.target_id;
1692 targ_offset = (target & 0x7);
1695 * Inquire Setup Information. This command retreives
1696 * the sync info for older models.
1698 param = sizeof(setup_info);
1699 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1700 (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1703 device_printf(aha->dev,
1704 "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1709 sync_info = setup_info.syncinfo[targ_offset];
1711 if (sync_info.sync == 0)
1712 spi->sync_offset = 0;
1714 spi->sync_offset = sync_info.offset;
1716 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1718 if (aha->boardid >= BOARD_1542CF)
1722 sync_period += 500 * sync_info.period;
1724 /* Convert ns value to standard SCSI sync rate */
1725 if (spi->sync_offset != 0)
1726 spi->sync_period = scsi_calc_syncparam(sync_period);
1728 spi->sync_period = 0;
1730 spi->valid = CTS_SPI_VALID_SYNC_RATE
1731 | CTS_SPI_VALID_SYNC_OFFSET
1732 | CTS_SPI_VALID_BUS_WIDTH;
1733 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1737 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1739 struct aha_softc* aha;
1741 aha = (struct aha_softc*)arg;
1742 aha->mailbox_physbase = segs->ds_addr;
1746 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1748 struct aha_softc* aha;
1750 aha = (struct aha_softc*)arg;
1751 aha->aha_ccb_physbase = segs->ds_addr;
1755 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1758 struct aha_softc* aha;
1760 aha = (struct aha_softc*)arg;
1761 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1765 ahapoll(struct cam_sim *sim)
1767 aha_intr(cam_sim_softc(sim));
1771 ahatimeout(void *arg)
1773 struct aha_ccb *accb;
1775 struct aha_softc *aha;
1778 struct ccb_hdr *ccb_h;
1780 accb = (struct aha_ccb *)arg;
1782 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1783 xpt_print_path(ccb->ccb_h.path);
1784 printf("CCB %p - timed out\n", (void *)accb);
1788 if ((accb->flags & ACCB_ACTIVE) == 0) {
1789 xpt_print_path(ccb->ccb_h.path);
1790 printf("CCB %p - timed out CCB already completed\n",
1797 * In order to simplify the recovery process, we ask the XPT
1798 * layer to halt the queue of new transactions and we traverse
1799 * the list of pending CCBs and remove their timeouts. This
1800 * means that the driver attempts to clear only one error
1801 * condition at a time. In general, timeouts that occur
1802 * close together are related anyway, so there is no benefit
1803 * in attempting to handle errors in parrallel. Timeouts will
1804 * be reinstated when the recovery process ends.
1806 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1807 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1808 xpt_freeze_simq(aha->sim, /*count*/1);
1809 accb->flags |= ACCB_RELEASE_SIMQ;
1812 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1813 while (ccb_h != NULL) {
1814 struct aha_ccb *pending_accb;
1816 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1817 untimeout(ahatimeout, pending_accb, ccb_h->timeout_ch);
1818 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1822 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1823 || aha->cur_outbox->action_code != AMBO_FREE) {
1825 * Try a full host adapter/SCSI bus reset.
1826 * We do this only if we have already attempted
1827 * to clear the condition with a BDR, or we cannot
1828 * attempt a BDR for lack of mailbox resources.
1830 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1831 ahareset(aha, /*hardreset*/TRUE);
1832 device_printf(aha->dev, "No longer in timeout\n");
1835 * Send a Bus Device Reset message:
1836 * The target that is holding up the bus may not
1837 * be the same as the one that triggered this timeout
1838 * (different commands have different timeout lengths),
1839 * but we have no way of determining this from our
1840 * timeout handler. Our strategy here is to queue a
1841 * BDR message to the target of the timed out command.
1842 * If this fails, we'll get another timeout 2 seconds
1843 * later which will attempt a bus reset.
1845 accb->flags |= ACCB_DEVICE_RESET;
1846 ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb, 2 * hz);
1847 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1849 /* No Data Transfer */
1850 aha->recovery_accb->hccb.datain = TRUE;
1851 aha->recovery_accb->hccb.dataout = TRUE;
1852 aha->recovery_accb->hccb.ahastat = 0;
1853 aha->recovery_accb->hccb.sdstat = 0;
1854 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1856 /* Tell the adapter about this command */
1857 paddr = ahaccbvtop(aha, aha->recovery_accb);
1858 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1859 aha->cur_outbox->action_code = AMBO_START;
1860 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1868 aha_detach(struct aha_softc *aha)
1870 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1871 xpt_free_path(aha->path);
1872 xpt_bus_deregister(cam_sim_path(aha->sim));
1873 cam_sim_free(aha->sim, /*free_devq*/TRUE);
1876 MODULE_DEPEND(aha, cam, 1, 1, 1);