2 * Generic register and struct definitions for the Adaptech 154x
3 * SCSI host adapters. Product specific probe and attach routines can
5 * aha 1542A/1542B/1542C/1542CF/1542CP aha_isa.c
8 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
10 * Copyright (c) 1998 M. Warner Losh.
11 * All Rights Reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Derived from bt.c written by:
36 * Copyright (c) 1998 Justin T. Gibbs.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions, and the following disclaimer,
44 * without modification, immediately at the beginning of the file.
45 * 2. The name of the author may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
52 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
69 #include <sys/kernel.h>
71 #include <sys/module.h>
72 #include <sys/mutex.h>
75 #include <machine/bus.h>
78 #include <cam/cam_ccb.h>
79 #include <cam/cam_sim.h>
80 #include <cam/cam_xpt_sim.h>
81 #include <cam/cam_debug.h>
83 #include <cam/scsi/scsi_message.h>
85 #include <dev/aha/ahareg.h>
87 #define PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
89 /* Macro to determine that a rev is potentially a new valid one
90 * so that the driver doesn't keep breaking on new revs as it
91 * did for the CF and CP.
93 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
95 /* MailBox Management functions */
96 static __inline void ahanextinbox(struct aha_softc *aha);
97 static __inline void ahanextoutbox(struct aha_softc *aha);
99 #define aha_name(aha) device_get_nameunit(aha->dev)
102 ahanextinbox(struct aha_softc *aha)
104 if (aha->cur_inbox == aha->last_inbox)
105 aha->cur_inbox = aha->in_boxes;
111 ahanextoutbox(struct aha_softc *aha)
113 if (aha->cur_outbox == aha->last_outbox)
114 aha->cur_outbox = aha->out_boxes;
119 #define ahautoa24(u,s3) \
120 (s3)[0] = ((u) >> 16) & 0xff; \
121 (s3)[1] = ((u) >> 8) & 0xff; \
122 (s3)[2] = (u) & 0xff;
124 #define aha_a24tou(s3) \
125 (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
127 /* CCB Management functions */
128 static __inline uint32_t ahaccbvtop(struct aha_softc *aha,
129 struct aha_ccb *accb);
130 static __inline struct aha_ccb* ahaccbptov(struct aha_softc *aha,
133 static __inline uint32_t
134 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
136 return (aha->aha_ccb_physbase
137 + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
139 static __inline struct aha_ccb *
140 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
142 return (aha->aha_ccb_array +
143 + ((struct aha_ccb*)(uintptr_t)ccb_addr -
144 (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
147 static struct aha_ccb* ahagetccb(struct aha_softc *aha);
148 static __inline void ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
149 static void ahaallocccbs(struct aha_softc *aha);
150 static bus_dmamap_callback_t ahaexecuteccb;
151 static void ahadone(struct aha_softc *aha, struct aha_ccb *accb,
152 aha_mbi_comp_code_t comp_code);
153 static void aha_intr_locked(struct aha_softc *aha);
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 void ahatimeout(void *arg);
177 /* Exported functions */
179 aha_alloc(struct aha_softc *aha)
182 SLIST_INIT(&aha->free_aha_ccbs);
183 LIST_INIT(&aha->pending_ccbs);
184 SLIST_INIT(&aha->sg_maps);
185 aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
186 aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
187 mtx_init(&aha->lock, "aha", NULL, MTX_DEF);
191 aha_free(struct aha_softc *aha)
193 switch (aha->init_level) {
197 struct sg_map_node *sg_map;
199 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
200 SLIST_REMOVE_HEAD(&aha->sg_maps, links);
201 bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
202 bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
204 free(sg_map, M_DEVBUF);
206 bus_dma_tag_destroy(aha->sg_dmat);
209 bus_dmamap_unload(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);
221 bus_dma_tag_destroy(aha->buffer_dmat);
223 bus_dma_tag_destroy(aha->mailbox_dmat);
227 mtx_destroy(&aha->lock);
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 consistently.
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), "1542C");
347 snprintf(aha->model, sizeof(aha->model), "1542CF");
350 snprintf(aha->model, sizeof(aha->model), "1542CP");
353 snprintf(aha->model, sizeof(aha->model), "Unknown");
357 * If we are a new type of 1542 board (anything newer than a 1542C)
358 * then disable the extended bios so that the
359 * mailbox interface is unlocked.
360 * This is also true for the 1542B Version 3.20. First Adaptec
361 * board that supports >1Gb drives.
362 * No need to check the extended bios flags as some of the
363 * extensions that cause us problems are not flagged in that byte.
365 if (PROBABLY_NEW_BOARD(aha->boardid) ||
366 (aha->boardid == 0x41
367 && aha->fw_major == 0x31 &&
368 aha->fw_minor >= 0x34)) {
369 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
370 /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
371 DEFAULT_CMD_TIMEOUT);
373 device_printf(aha->dev,
374 "AOP_RETURN_EXT_BIOS_INFO - Failed.");
377 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
378 /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
380 device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
384 if (aha->boardid < 0x41)
385 device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
387 aha->max_sg = 17; /* Need >= 17 to do 64k I/O */
389 aha->extended_lun = 0;
390 aha->extended_trans = 0;
392 /* Determine Sync/Wide/Disc settings */
393 length_param = sizeof(setup_info);
394 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
395 /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
396 DEFAULT_CMD_TIMEOUT);
398 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
402 if (setup_info.initiate_sync != 0) {
403 aha->sync_permitted = ALL_TARGETS;
405 aha->disc_permitted = ALL_TARGETS;
407 /* We need as many mailboxes as we can have ccbs */
408 aha->num_boxes = aha->max_ccbs;
410 /* Determine our SCSI ID */
411 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
412 (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
414 device_printf(aha->dev,
415 "aha_fetch_adapter_info - Failed Get Config\n");
418 aha->scsi_id = config_data.scsi_id;
423 * Start the board, ready for normal operation
426 aha_init(struct aha_softc* aha)
428 /* Announce the Adapter */
429 device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
430 aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
432 if (aha->diff_bus != 0)
435 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
439 * Create our DMA tags. These tags define the kinds of device
440 * accessible memory allocations and memory mappings we will
441 * need to perform during normal operation.
443 * Unless we need to further restrict the allocation, we rely
444 * on the restrictions of the parent dmat, hence the common
445 * use of MAXADDR and MAXSIZE.
448 /* DMA tag for mapping buffers into device visible space. */
449 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
452 /* lowaddr */ BUS_SPACE_MAXADDR,
453 /* highaddr */ BUS_SPACE_MAXADDR,
455 /* filterarg */ NULL,
456 /* maxsize */ DFLTPHYS,
457 /* nsegments */ AHA_NSEG,
458 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
459 /* flags */ BUS_DMA_ALLOCNOW,
460 /* lockfunc */ busdma_lock_mutex,
461 /* lockarg */ &aha->lock,
462 &aha->buffer_dmat) != 0) {
467 /* DMA tag for our mailboxes */
468 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
471 /* lowaddr */ BUS_SPACE_MAXADDR,
472 /* highaddr */ BUS_SPACE_MAXADDR,
474 /* filterarg */ NULL,
475 /* maxsize */ aha->num_boxes *
476 (sizeof(aha_mbox_in_t) +
477 sizeof(aha_mbox_out_t)),
479 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
483 &aha->mailbox_dmat) != 0) {
489 /* Allocation for our mailboxes */
490 if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
491 BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
496 /* And permanently map them */
497 bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
498 aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
499 sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
503 aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
507 /* DMA tag for our ccb structures */
508 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
511 /* lowaddr */ BUS_SPACE_MAXADDR,
512 /* highaddr */ BUS_SPACE_MAXADDR,
514 /* filterarg */ NULL,
515 /* maxsize */ aha->max_ccbs *
516 sizeof(struct aha_ccb),
518 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
522 &aha->ccb_dmat) != 0) {
528 /* Allocation for our ccbs */
529 if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
530 BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
535 /* And permanently map them */
536 bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
537 aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
541 /* DMA tag for our S/G structures. We allocate in page sized chunks */
542 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
545 /* lowaddr */ BUS_SPACE_MAXADDR,
546 /* highaddr */ BUS_SPACE_MAXADDR,
548 /* filterarg */ NULL,
549 /* maxsize */ PAGE_SIZE,
551 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
560 /* Perform initial CCB allocation */
561 bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
564 if (aha->num_ccbs == 0) {
565 device_printf(aha->dev,
566 "aha_init - Unable to allocate initial ccbs\n");
571 * Note that we are going and return (to probe)
581 aha_attach(struct aha_softc *aha)
583 int tagged_dev_openings;
584 struct cam_devq *devq;
587 * We don't do tagged queueing, since the aha cards don't
590 tagged_dev_openings = 0;
593 * Create the device queue for our SIM.
595 devq = cam_simq_alloc(aha->max_ccbs - 1);
600 * Construct our SIM entry
602 aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha,
603 device_get_unit(aha->dev), &aha->lock, 2, tagged_dev_openings,
605 if (aha->sim == NULL) {
609 mtx_lock(&aha->lock);
610 if (xpt_bus_register(aha->sim, aha->dev, 0) != CAM_SUCCESS) {
611 cam_sim_free(aha->sim, /*free_devq*/TRUE);
612 mtx_unlock(&aha->lock);
615 if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
616 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
617 xpt_bus_deregister(cam_sim_path(aha->sim));
618 cam_sim_free(aha->sim, /*free_devq*/TRUE);
619 mtx_unlock(&aha->lock);
622 mtx_unlock(&aha->lock);
623 gone_in_dev(aha->dev, 12, "aha(4) driver");
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 callout_init_mtx(&next_ccb->timer, &aha->lock, 0);
668 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
672 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
674 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
679 /* Reserve a CCB for error recovery */
680 if (aha->recovery_accb == NULL) {
681 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
682 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
687 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
691 mtx_assert(&aha->lock, MA_OWNED);
692 if ((accb->flags & ACCB_ACTIVE) != 0)
693 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
694 if (aha->resource_shortage != 0
695 && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
696 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
697 aha->resource_shortage = FALSE;
699 accb->flags = ACCB_FREE;
700 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;
710 mtx_assert(&aha->lock, MA_OWNED);
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);
729 ahaaction(struct cam_sim *sim, union ccb *ccb)
731 struct aha_softc *aha;
733 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
735 aha = (struct aha_softc *)cam_sim_softc(sim);
736 mtx_assert(&aha->lock, MA_OWNED);
738 switch (ccb->ccb_h.func_code) {
739 /* Common cases first */
740 case XPT_SCSI_IO: /* Execute the requested I/O operation */
741 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ {
742 struct aha_ccb *accb;
743 struct aha_hccb *hccb;
746 * Get an accb to use.
748 if ((accb = ahagetccb(aha)) == NULL) {
749 aha->resource_shortage = TRUE;
750 xpt_freeze_simq(aha->sim, /*count*/1);
751 ccb->ccb_h.status = CAM_REQUEUE_REQ;
758 * So we can find the ACCB when an abort is requested
761 ccb->ccb_h.ccb_accb_ptr = accb;
762 ccb->ccb_h.ccb_aha_ptr = aha;
765 * Put all the arguments for the xfer in the accb
767 hccb->target = ccb->ccb_h.target_id;
768 hccb->lun = ccb->ccb_h.target_lun;
772 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
773 struct ccb_scsiio *csio;
774 struct ccb_hdr *ccbh;
779 hccb->opcode = aha->ccb_ccb_opcode;
780 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
781 hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
782 hccb->cmd_len = csio->cdb_len;
783 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
784 ccb->ccb_h.status = CAM_REQ_INVALID;
785 ahafreeccb(aha, accb);
789 hccb->sense_len = csio->sense_len;
790 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
791 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
792 bcopy(csio->cdb_io.cdb_ptr,
793 hccb->scsi_cdb, hccb->cmd_len);
795 /* I guess I could map it in... */
796 ccbh->status = CAM_REQ_INVALID;
797 ahafreeccb(aha, accb);
802 bcopy(csio->cdb_io.cdb_bytes,
803 hccb->scsi_cdb, hccb->cmd_len);
806 * If we have any data to send with this command,
807 * map it into bus space.
810 error = bus_dmamap_load_ccb(
817 if (error == EINPROGRESS) {
819 * So as to maintain ordering, freeze the
820 * controller queue until our mapping is
823 xpt_freeze_simq(aha->sim, 1);
824 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
827 hccb->opcode = INITIATOR_BUS_DEV_RESET;
828 /* No data transfer */
830 hccb->dataout = TRUE;
833 ahaexecuteccb(accb, NULL, 0, 0);
837 case XPT_ABORT: /* Abort the specified CCB */
839 ccb->ccb_h.status = CAM_REQ_INVALID;
842 case XPT_SET_TRAN_SETTINGS:
844 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
847 case XPT_GET_TRAN_SETTINGS:
848 /* Get default/user set transfer settings for the target */
850 struct ccb_trans_settings *cts = &ccb->cts;
851 u_int target_mask = 0x01 << ccb->ccb_h.target_id;
852 struct ccb_trans_settings_scsi *scsi =
853 &cts->proto_specific.scsi;
854 struct ccb_trans_settings_spi *spi =
855 &cts->xport_specific.spi;
857 cts->protocol = PROTO_SCSI;
858 cts->protocol_version = SCSI_REV_2;
859 cts->transport = XPORT_SPI;
860 cts->transport_version = 2;
861 if (cts->type == CTS_TYPE_USER_SETTINGS) {
863 if ((aha->disc_permitted & target_mask) != 0)
864 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
865 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
866 if ((aha->sync_permitted & target_mask) != 0) {
867 if (aha->boardid >= BOARD_1542CF)
868 spi->sync_period = 25;
870 spi->sync_period = 50;
872 spi->sync_period = 0;
875 if (spi->sync_period != 0)
876 spi->sync_offset = 15;
878 spi->valid = CTS_SPI_VALID_SYNC_RATE
879 | CTS_SPI_VALID_SYNC_OFFSET
880 | CTS_SPI_VALID_BUS_WIDTH
881 | CTS_SPI_VALID_DISC;
882 scsi->valid = CTS_SCSI_VALID_TQ;
884 ahafetchtransinfo(aha, cts);
887 ccb->ccb_h.status = CAM_REQ_CMP;
891 case XPT_CALC_GEOMETRY:
893 struct ccb_calc_geometry *ccg;
895 uint32_t secs_per_cylinder;
898 size_mb = ccg->volume_size
899 / ((1024L * 1024L) / ccg->block_size);
900 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
901 if (size_mb >= 2048) {
903 ccg->secs_per_track = 63;
906 ccg->secs_per_track = 32;
910 ccg->secs_per_track = 32;
912 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
913 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
914 ccb->ccb_h.status = CAM_REQ_CMP;
918 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
919 ahareset(aha, /*hardreset*/TRUE);
920 ccb->ccb_h.status = CAM_REQ_CMP;
923 case XPT_TERM_IO: /* Terminate the I/O process */
925 ccb->ccb_h.status = CAM_REQ_INVALID;
928 case XPT_PATH_INQ: /* Path routing inquiry */
930 struct ccb_pathinq *cpi = &ccb->cpi;
932 cpi->version_num = 1; /* XXX??? */
933 cpi->hba_inquiry = PI_SDTR_ABLE;
934 cpi->target_sprt = 0;
936 cpi->hba_eng_cnt = 0;
939 cpi->initiator_id = aha->scsi_id;
940 cpi->bus_id = cam_sim_bus(sim);
941 cpi->base_transfer_speed = 3300;
942 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
943 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
944 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
945 cpi->unit_number = cam_sim_unit(sim);
946 cpi->transport = XPORT_SPI;
947 cpi->transport_version = 2;
948 cpi->protocol = PROTO_SCSI;
949 cpi->protocol_version = SCSI_REV_2;
950 cpi->ccb_h.status = CAM_REQ_CMP;
955 ccb->ccb_h.status = CAM_REQ_INVALID;
962 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
964 struct aha_ccb *accb;
966 struct aha_softc *aha;
969 accb = (struct aha_ccb *)arg;
971 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
975 device_printf(aha->dev,
976 "Unexepected error 0x%x returned from "
977 "bus_dmamap_load\n", error);
978 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
979 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
980 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
982 ahafreeccb(aha, accb);
989 bus_dma_segment_t *end_seg;
992 end_seg = dm_segs + nseg;
994 /* Copy the segments into our SG list */
996 while (dm_segs < end_seg) {
997 ahautoa24(dm_segs->ds_len, sg->len);
998 ahautoa24(dm_segs->ds_addr, sg->addr);
1004 accb->hccb.opcode = aha->ccb_sg_opcode;
1005 ahautoa24((sizeof(aha_sg_t) * nseg),
1006 accb->hccb.data_len);
1007 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1009 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1010 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1013 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1014 op = BUS_DMASYNC_PREREAD;
1016 op = BUS_DMASYNC_PREWRITE;
1018 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1021 accb->hccb.opcode = INITIATOR_CCB;
1022 ahautoa24(0, accb->hccb.data_len);
1023 ahautoa24(0, accb->hccb.data_addr);
1027 * Last time we need to check if this CCB needs to
1030 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1032 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1033 ahafreeccb(aha, accb);
1038 accb->flags = ACCB_ACTIVE;
1039 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1040 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1042 callout_reset_sbt(&accb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1043 ahatimeout, accb, 0);
1045 /* Tell the adapter about this command */
1046 if (aha->cur_outbox->action_code != AMBO_FREE) {
1048 * We should never encounter a busy mailbox.
1049 * If we do, warn the user, and treat it as
1050 * a resource shortage. If the controller is
1051 * hung, one of the pending transactions will
1052 * timeout causing us to start recovery operations.
1054 device_printf(aha->dev,
1055 "Encountered busy mailbox with %d out of %d "
1056 "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1057 callout_stop(&accb->timer);
1059 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1060 ahafreeccb(aha, accb);
1061 aha->resource_shortage = TRUE;
1062 xpt_freeze_simq(aha->sim, /*count*/1);
1063 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1067 paddr = ahaccbvtop(aha, accb);
1068 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1069 aha->cur_outbox->action_code = AMBO_START;
1070 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1078 struct aha_softc *aha;
1081 mtx_lock(&aha->lock);
1082 aha_intr_locked(aha);
1083 mtx_unlock(&aha->lock);
1087 aha_intr_locked(struct aha_softc *aha)
1092 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1093 if ((intstat & CMD_COMPLETE) != 0) {
1094 aha->latched_status = aha_inb(aha, STATUS_REG);
1095 aha->command_cmp = TRUE;
1098 aha_outb(aha, CONTROL_REG, RESET_INTR);
1100 if ((intstat & IMB_LOADED) != 0) {
1101 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1102 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1103 ahadone(aha, ahaccbptov(aha, paddr),
1104 aha->cur_inbox->comp_code);
1105 aha->cur_inbox->comp_code = AMBI_FREE;
1110 if ((intstat & SCSI_BUS_RESET) != 0) {
1111 ahareset(aha, /*hardreset*/FALSE);
1117 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1120 struct ccb_scsiio *csio;
1123 csio = &accb->ccb->csio;
1125 if ((accb->flags & ACCB_ACTIVE) == 0) {
1126 device_printf(aha->dev,
1127 "ahadone - Attempt to free non-active ACCB %p\n",
1132 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1133 bus_dmasync_op_t op;
1135 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1136 op = BUS_DMASYNC_POSTREAD;
1138 op = BUS_DMASYNC_POSTWRITE;
1139 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1140 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1143 if (accb == aha->recovery_accb) {
1145 * The recovery ACCB does not have a CCB associated
1146 * with it, so short circuit the normal error handling.
1147 * We now traverse our list of pending CCBs and process
1148 * any that were terminated by the recovery CCBs action.
1149 * We also reinstate timeouts for all remaining, pending,
1152 struct cam_path *path;
1153 struct ccb_hdr *ccb_h;
1156 /* Notify all clients that a BDR occurred */
1157 error = xpt_create_path(&path, /*periph*/NULL,
1158 cam_sim_path(aha->sim), accb->hccb.target,
1161 if (error == CAM_REQ_CMP) {
1162 xpt_async(AC_SENT_BDR, path, NULL);
1163 xpt_free_path(path);
1166 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1167 while (ccb_h != NULL) {
1168 struct aha_ccb *pending_accb;
1170 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1171 if (pending_accb->hccb.target == accb->hccb.target) {
1172 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1173 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1174 ahadone(aha, pending_accb, AMBI_ERROR);
1176 callout_reset_sbt(&pending_accb->timer,
1177 SBT_1MS * ccb_h->timeout, 0, ahatimeout,
1179 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1182 device_printf(aha->dev, "No longer in timeout\n");
1186 callout_stop(&accb->timer);
1188 switch (comp_code) {
1190 device_printf(aha->dev,
1191 "ahadone - CCB completed with free status!\n");
1193 case AMBI_NOT_FOUND:
1194 device_printf(aha->dev,
1195 "ahadone - CCB Abort failed to find CCB\n");
1199 /* An error occurred */
1200 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1203 csio->resid = aha_a24tou(accb->hccb.data_len);
1204 switch(accb->hccb.ahastat) {
1205 case AHASTAT_DATARUN_ERROR:
1207 if (csio->resid <= 0) {
1208 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1213 case AHASTAT_NOERROR:
1214 csio->scsi_status = accb->hccb.sdstat;
1215 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1216 switch(csio->scsi_status) {
1217 case SCSI_STATUS_CHECK_COND:
1218 case SCSI_STATUS_CMD_TERMINATED:
1219 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1221 * The aha writes the sense data at different
1222 * offsets based on the scsi cmd len
1224 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1226 (caddr_t) &csio->sense_data,
1227 accb->hccb.sense_len);
1231 case SCSI_STATUS_OK:
1232 csio->ccb_h.status = CAM_REQ_CMP;
1236 case AHASTAT_SELTIMEOUT:
1237 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1239 case AHASTAT_UNEXPECTED_BUSFREE:
1240 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1242 case AHASTAT_INVALID_PHASE:
1243 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1245 case AHASTAT_INVALID_ACTION_CODE:
1246 panic("%s: Inavlid Action code", aha_name(aha));
1248 case AHASTAT_INVALID_OPCODE:
1249 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1250 panic("%s: Invalid CCB Opcode %x hccb = %p",
1251 aha_name(aha), accb->hccb.opcode,
1253 device_printf(aha->dev,
1254 "AHA-1540A compensation failed\n");
1255 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1256 csio->ccb_h.status = CAM_REQUEUE_REQ;
1258 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1259 /* We don't even support linked commands... */
1260 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1262 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1263 panic("%s: Invalid CCB or SG list", aha_name(aha));
1265 case AHASTAT_HA_SCSI_BUS_RESET:
1266 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1268 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1270 case AHASTAT_HA_BDR:
1271 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1272 csio->ccb_h.status = CAM_BDR_SENT;
1274 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1277 if (csio->ccb_h.status != CAM_REQ_CMP) {
1278 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1279 csio->ccb_h.status |= CAM_DEV_QFRZN;
1281 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1282 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1283 ahafreeccb(aha, accb);
1287 /* All completed without incident */
1288 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1289 /* I don't think so since it works???? */
1290 ccb->ccb_h.status |= CAM_REQ_CMP;
1291 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1292 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1293 ahafreeccb(aha, accb);
1300 ahareset(struct aha_softc* aha, int hard_reset)
1302 struct ccb_hdr *ccb_h;
1307 if (hard_reset != 0)
1308 reset_type = HARD_RESET;
1310 reset_type = SOFT_RESET;
1311 aha_outb(aha, CONTROL_REG, reset_type);
1313 /* Wait 5sec. for Diagnostic start */
1314 timeout = 5 * 10000;
1316 status = aha_inb(aha, STATUS_REG);
1317 if ((status & DIAG_ACTIVE) != 0)
1322 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1323 "assert. status = %#x\n", status));
1327 /* Wait 10sec. for Diagnostic end */
1328 timeout = 10 * 10000;
1330 status = aha_inb(aha, STATUS_REG);
1331 if ((status & DIAG_ACTIVE) == 0)
1336 panic("%s: ahareset - Diagnostic Active failed to drop. "
1337 "status = 0x%x\n", aha_name(aha), status);
1341 /* Wait for the host adapter to become ready or report a failure */
1344 status = aha_inb(aha, STATUS_REG);
1345 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1350 device_printf(aha->dev, "ahareset - Host adapter failed to "
1351 "come ready. status = 0x%x\n", status);
1355 /* If the diagnostics failed, tell the user */
1356 if ((status & DIAG_FAIL) != 0
1357 || (status & HA_READY) == 0) {
1358 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1360 if ((status & DATAIN_REG_READY) != 0)
1361 device_printf(aha->dev, "ahareset - Host Adapter "
1362 "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1366 /* If we've attached to the XPT, tell it about the event */
1367 if (aha->path != NULL)
1368 xpt_async(AC_BUS_RESET, aha->path, NULL);
1371 * Perform completion processing for all outstanding CCBs.
1373 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1374 struct aha_ccb *pending_accb;
1376 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1377 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1378 ahadone(aha, pending_accb, AMBI_ERROR);
1381 /* If we've allocated mailboxes, initialize them */
1382 /* Must be done after we've aborted our queue, or aha_cmd fails */
1383 if (aha->init_level > 4)
1390 * Send a command to the adapter.
1393 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1394 u_int param_len, uint8_t *reply_data, u_int reply_len,
1401 u_int reply_buf_size;
1405 /* No data returned to start */
1406 reply_buf_size = reply_len;
1414 * All commands except for the "start mailbox" and the "enable
1415 * outgoing mailbox read interrupt" commands cannot be issued
1416 * while there are pending transactions. Freeze our SIMQ
1417 * and wait for all completions to occur if necessary.
1420 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1421 /* Fire the interrupt handler in case interrupts are blocked */
1427 device_printf(aha->dev,
1428 "aha_cmd: Timeout waiting for adapter idle\n");
1431 aha->command_cmp = 0;
1433 * Wait up to 10 sec. for the adapter to become
1434 * ready to accept commands.
1438 status = aha_inb(aha, STATUS_REG);
1439 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1442 * Throw away any pending data which may be
1443 * left over from earlier commands that we
1446 if ((status & DATAIN_REG_READY) != 0)
1447 (void)aha_inb(aha, DATAIN_REG);
1451 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1452 " ready, status = 0x%x\n", status);
1457 * Send the opcode followed by any necessary parameter bytes.
1459 aha_outb(aha, COMMAND_REG, opcode);
1462 * Wait for up to 1sec to get the parameter list sent
1465 while (param_len && --timeout) {
1467 status = aha_inb(aha, STATUS_REG);
1468 intstat = aha_inb(aha, INTSTAT_REG);
1470 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1471 == (INTR_PENDING|CMD_COMPLETE)) {
1472 saved_status = status;
1477 if (aha->command_cmp != 0) {
1478 saved_status = aha->latched_status;
1482 if ((status & DATAIN_REG_READY) != 0)
1484 if ((status & CMD_REG_BUSY) == 0) {
1485 aha_outb(aha, COMMAND_REG, *params++);
1491 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1492 "status = 0x%x\n", status);
1497 * For all other commands, we wait for any output data
1498 * and the final comand completion interrupt.
1500 while (cmd_complete == 0 && --cmd_timeout) {
1502 status = aha_inb(aha, STATUS_REG);
1503 intstat = aha_inb(aha, INTSTAT_REG);
1505 if (aha->command_cmp != 0) {
1507 saved_status = aha->latched_status;
1508 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1509 == (INTR_PENDING|CMD_COMPLETE)) {
1511 * Our poll (in case interrupts are blocked)
1512 * saw the CMD_COMPLETE interrupt.
1515 saved_status = status;
1517 if ((status & DATAIN_REG_READY) != 0) {
1520 data = aha_inb(aha, DATAIN_REG);
1521 if (reply_len < reply_buf_size) {
1522 *reply_data++ = data;
1524 device_printf(aha->dev,
1525 "aha_cmd - Discarded reply data "
1526 "byte for opcode 0x%x\n", opcode);
1529 * Reset timeout to ensure at least a second
1530 * between response bytes.
1532 cmd_timeout = MAX(cmd_timeout, 10000);
1537 if (cmd_timeout == 0) {
1538 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1539 "intstat = 0x%x, reply_len = %d\n", status, intstat,
1545 * Clear any pending interrupts. Block interrupts so our
1546 * interrupt handler is not re-entered.
1554 * If the command was rejected by the controller, tell the caller.
1556 if ((saved_status & CMD_INVALID) != 0) {
1557 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1559 * Some early adapters may not recover properly from
1560 * an invalid command. If it appears that the controller
1561 * has wedged (i.e. status was not cleared by our interrupt
1562 * reset above), perform a soft reset.
1565 status = aha_inb(aha, STATUS_REG);
1566 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1567 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1568 || (status & (HA_READY|INIT_REQUIRED))
1569 != (HA_READY|INIT_REQUIRED))
1570 ahareset(aha, /*hard_reset*/FALSE);
1574 if (param_len > 0) {
1575 /* The controller did not accept the full argument list */
1576 PRVERB((aha->dev, "Controller did not accept full argument "
1577 "list (%d > 0)\n", param_len));
1581 if (reply_len != reply_buf_size) {
1582 /* Too much or too little data received */
1583 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1584 reply_len, reply_buf_size));
1588 /* We were successful */
1593 ahainitmboxes(struct aha_softc *aha)
1596 init_24b_mbox_params_t init_mbox;
1598 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1599 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1600 aha->cur_inbox = aha->in_boxes;
1601 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1602 aha->cur_outbox = aha->out_boxes;
1603 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1605 /* Tell the adapter about them */
1606 init_mbox.num_mboxes = aha->num_boxes;
1607 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1608 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1609 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1610 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1613 printf("ahainitmboxes: Initialization command failed\n");
1618 * Update the XPT's idea of the negotiated transfer
1619 * parameters for a particular target.
1622 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1624 setup_data_t setup_info;
1630 targ_syncinfo_t sync_info;
1631 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1633 target = cts->ccb_h.target_id;
1634 targ_offset = (target & 0x7);
1637 * Inquire Setup Information. This command retreives
1638 * the sync info for older models.
1640 param = sizeof(setup_info);
1641 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1642 (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1645 device_printf(aha->dev,
1646 "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1651 sync_info = setup_info.syncinfo[targ_offset];
1653 if (sync_info.sync == 0)
1654 spi->sync_offset = 0;
1656 spi->sync_offset = sync_info.offset;
1658 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1660 if (aha->boardid >= BOARD_1542CF)
1664 sync_period += 500 * sync_info.period;
1666 /* Convert ns value to standard SCSI sync rate */
1667 if (spi->sync_offset != 0)
1668 spi->sync_period = scsi_calc_syncparam(sync_period);
1670 spi->sync_period = 0;
1672 spi->valid = CTS_SPI_VALID_SYNC_RATE
1673 | CTS_SPI_VALID_SYNC_OFFSET
1674 | CTS_SPI_VALID_BUS_WIDTH;
1675 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1679 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1681 struct aha_softc* aha;
1683 aha = (struct aha_softc*)arg;
1684 aha->mailbox_physbase = segs->ds_addr;
1688 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1690 struct aha_softc* aha;
1692 aha = (struct aha_softc*)arg;
1693 aha->aha_ccb_physbase = segs->ds_addr;
1697 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1700 struct aha_softc* aha;
1702 aha = (struct aha_softc*)arg;
1703 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1707 ahapoll(struct cam_sim *sim)
1709 aha_intr_locked(cam_sim_softc(sim));
1713 ahatimeout(void *arg)
1715 struct aha_ccb *accb;
1717 struct aha_softc *aha;
1719 struct ccb_hdr *ccb_h;
1721 accb = (struct aha_ccb *)arg;
1723 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1724 mtx_assert(&aha->lock, MA_OWNED);
1725 xpt_print_path(ccb->ccb_h.path);
1726 printf("CCB %p - timed out\n", (void *)accb);
1728 if ((accb->flags & ACCB_ACTIVE) == 0) {
1729 xpt_print_path(ccb->ccb_h.path);
1730 printf("CCB %p - timed out CCB already completed\n",
1736 * In order to simplify the recovery process, we ask the XPT
1737 * layer to halt the queue of new transactions and we traverse
1738 * the list of pending CCBs and remove their timeouts. This
1739 * means that the driver attempts to clear only one error
1740 * condition at a time. In general, timeouts that occur
1741 * close together are related anyway, so there is no benefit
1742 * in attempting to handle errors in parallel. Timeouts will
1743 * be reinstated when the recovery process ends.
1745 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1746 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1747 xpt_freeze_simq(aha->sim, /*count*/1);
1748 accb->flags |= ACCB_RELEASE_SIMQ;
1751 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1752 while (ccb_h != NULL) {
1753 struct aha_ccb *pending_accb;
1755 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1756 callout_stop(&pending_accb->timer);
1757 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1761 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1762 || aha->cur_outbox->action_code != AMBO_FREE) {
1764 * Try a full host adapter/SCSI bus reset.
1765 * We do this only if we have already attempted
1766 * to clear the condition with a BDR, or we cannot
1767 * attempt a BDR for lack of mailbox resources.
1769 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1770 ahareset(aha, /*hardreset*/TRUE);
1771 device_printf(aha->dev, "No longer in timeout\n");
1774 * Send a Bus Device Reset message:
1775 * The target that is holding up the bus may not
1776 * be the same as the one that triggered this timeout
1777 * (different commands have different timeout lengths),
1778 * but we have no way of determining this from our
1779 * timeout handler. Our strategy here is to queue a
1780 * BDR message to the target of the timed out command.
1781 * If this fails, we'll get another timeout 2 seconds
1782 * later which will attempt a bus reset.
1784 accb->flags |= ACCB_DEVICE_RESET;
1785 callout_reset(&accb->timer, 2 * hz, ahatimeout, accb);
1786 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1788 /* No Data Transfer */
1789 aha->recovery_accb->hccb.datain = TRUE;
1790 aha->recovery_accb->hccb.dataout = TRUE;
1791 aha->recovery_accb->hccb.ahastat = 0;
1792 aha->recovery_accb->hccb.sdstat = 0;
1793 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1795 /* Tell the adapter about this command */
1796 paddr = ahaccbvtop(aha, aha->recovery_accb);
1797 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1798 aha->cur_outbox->action_code = AMBO_START;
1799 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1805 aha_detach(struct aha_softc *aha)
1807 mtx_lock(&aha->lock);
1808 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1809 xpt_free_path(aha->path);
1810 xpt_bus_deregister(cam_sim_path(aha->sim));
1811 cam_sim_free(aha->sim, /*free_devq*/TRUE);
1812 mtx_unlock(&aha->lock);
1813 /* XXX: Drain all timers? */
1816 MODULE_DEPEND(aha, cam, 1, 1, 1);