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 * 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 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
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
24 * FOR 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
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #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>
73 #include <machine/bus.h>
76 #include <cam/cam_ccb.h>
77 #include <cam/cam_sim.h>
78 #include <cam/cam_xpt_sim.h>
79 #include <cam/cam_debug.h>
81 #include <cam/scsi/scsi_message.h>
83 #include <dev/aha/ahareg.h>
85 #define PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
87 /* Macro to determine that a rev is potentially a new valid one
88 * so that the driver doesn't keep breaking on new revs as it
89 * did for the CF and CP.
91 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
93 /* MailBox Management functions */
94 static __inline void ahanextinbox(struct aha_softc *aha);
95 static __inline void ahanextoutbox(struct aha_softc *aha);
97 #define aha_name(aha) device_get_nameunit(aha->dev)
100 ahanextinbox(struct aha_softc *aha)
102 if (aha->cur_inbox == aha->last_inbox)
103 aha->cur_inbox = aha->in_boxes;
109 ahanextoutbox(struct aha_softc *aha)
111 if (aha->cur_outbox == aha->last_outbox)
112 aha->cur_outbox = aha->out_boxes;
117 #define ahautoa24(u,s3) \
118 (s3)[0] = ((u) >> 16) & 0xff; \
119 (s3)[1] = ((u) >> 8) & 0xff; \
120 (s3)[2] = (u) & 0xff;
122 #define aha_a24tou(s3) \
123 (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
125 /* CCB Management functions */
126 static __inline uint32_t ahaccbvtop(struct aha_softc *aha,
127 struct aha_ccb *accb);
128 static __inline struct aha_ccb* ahaccbptov(struct aha_softc *aha,
131 static __inline uint32_t
132 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
134 return (aha->aha_ccb_physbase
135 + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
137 static __inline struct aha_ccb *
138 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
140 return (aha->aha_ccb_array +
141 + ((struct aha_ccb*)(uintptr_t)ccb_addr -
142 (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
145 static struct aha_ccb* ahagetccb(struct aha_softc *aha);
146 static __inline void ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
147 static void ahaallocccbs(struct aha_softc *aha);
148 static bus_dmamap_callback_t ahaexecuteccb;
149 static void ahadone(struct aha_softc *aha, struct aha_ccb *accb,
150 aha_mbi_comp_code_t comp_code);
151 static void aha_intr_locked(struct aha_softc *aha);
153 /* Host adapter command functions */
154 static int ahareset(struct aha_softc* aha, int hard_reset);
156 /* Initialization functions */
157 static int ahainitmboxes(struct aha_softc *aha);
158 static bus_dmamap_callback_t ahamapmboxes;
159 static bus_dmamap_callback_t ahamapccbs;
160 static bus_dmamap_callback_t ahamapsgs;
162 /* Transfer Negotiation Functions */
163 static void ahafetchtransinfo(struct aha_softc *aha,
164 struct ccb_trans_settings *cts);
166 /* CAM SIM entry points */
167 #define ccb_accb_ptr spriv_ptr0
168 #define ccb_aha_ptr spriv_ptr1
169 static void ahaaction(struct cam_sim *sim, union ccb *ccb);
170 static void ahapoll(struct cam_sim *sim);
172 /* Our timeout handler */
173 static void ahatimeout(void *arg);
175 /* Exported functions */
177 aha_alloc(struct aha_softc *aha)
180 SLIST_INIT(&aha->free_aha_ccbs);
181 LIST_INIT(&aha->pending_ccbs);
182 SLIST_INIT(&aha->sg_maps);
183 aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
184 aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
185 mtx_init(&aha->lock, "aha", NULL, MTX_DEF);
189 aha_free(struct aha_softc *aha)
191 switch (aha->init_level) {
195 struct sg_map_node *sg_map;
197 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
198 SLIST_REMOVE_HEAD(&aha->sg_maps, links);
199 bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
200 bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
202 free(sg_map, M_DEVBUF);
204 bus_dma_tag_destroy(aha->sg_dmat);
207 bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
209 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
212 bus_dma_tag_destroy(aha->ccb_dmat);
214 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
216 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
217 aha->mailbox_dmamap);
219 bus_dma_tag_destroy(aha->buffer_dmat);
221 bus_dma_tag_destroy(aha->mailbox_dmat);
225 mtx_destroy(&aha->lock);
229 * Probe the adapter and verify that the card is an Adaptec.
232 aha_probe(struct aha_softc* aha)
237 board_id_data_t board_id;
240 * See if the three I/O ports look reasonable.
241 * Touch the minimal number of registers in the
244 status = aha_inb(aha, STATUS_REG);
246 (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
247 PRVERB((aha->dev, "status reg test failed %x\n", status));
251 intstat = aha_inb(aha, INTSTAT_REG);
252 if ((intstat & INTSTAT_REG_RSVD) != 0) {
253 PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
258 * Looking good so far. Final test is to reset the
259 * adapter and fetch the board ID and ensure we aren't
260 * looking at a BusLogic.
262 if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
263 PRVERB((aha->dev, "Failed Reset\n"));
268 * Get the board ID. We use this to see if we're dealing with
269 * a buslogic card or an aha card (or clone).
271 error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
272 (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
274 PRVERB((aha->dev, "INQUIRE failed %x\n", error));
277 aha->fw_major = board_id.firmware_rev_major;
278 aha->fw_minor = board_id.firmware_rev_minor;
279 aha->boardid = board_id.board_type;
282 * The Buslogic cards have an id of either 0x41 or 0x42. So
283 * if those come up in the probe, we test the geometry register
284 * of the board. Adaptec boards that are this old will not have
285 * this register, and return 0xff, while buslogic cards will return
286 * something different.
288 * It appears that for reasons unknow, for the for the
289 * aha-1542B cards, we need to wait a little bit before trying
290 * to read the geometry register. I picked 10ms since we have
291 * reports that a for loop to 1000 did the trick, and this
292 * errs on the side of conservatism. Besides, no one will
293 * notice a 10mS delay here, even the 1542B card users :-)
295 * Some compatible cards return 0 here. Some cards also
296 * seem to return 0x7f.
298 * XXX I'm not sure how this will impact other cloned cards
300 * This really should be replaced with the esetup command, since
301 * that appears to be more reliable. This becomes more and more
302 * true over time as we discover more cards that don't read the
303 * geometry register consistently.
305 if (aha->boardid <= 0x42) {
306 /* Wait 10ms before reading */
308 status = aha_inb(aha, GEOMETRY_REG);
309 if (status != 0xff && status != 0x00 && status != 0x7f) {
310 PRVERB((aha->dev, "Geometry Register test failed %#x\n",
320 * Pull the boards setup information and record it in our softc.
323 aha_fetch_adapter_info(struct aha_softc *aha)
325 setup_data_t setup_info;
326 config_data_t config_data;
327 uint8_t length_param;
329 struct aha_extbios extbios;
331 switch (aha->boardid) {
332 case BOARD_1540_16HEAD_BIOS:
333 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
335 case BOARD_1540_64HEAD_BIOS:
336 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
339 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
342 snprintf(aha->model, sizeof(aha->model), "1542C");
345 snprintf(aha->model, sizeof(aha->model), "1542CF");
348 snprintf(aha->model, sizeof(aha->model), "1542CP");
351 snprintf(aha->model, sizeof(aha->model), "Unknown");
355 * If we are a new type of 1542 board (anything newer than a 1542C)
356 * then disable the extended bios so that the
357 * mailbox interface is unlocked.
358 * This is also true for the 1542B Version 3.20. First Adaptec
359 * board that supports >1Gb drives.
360 * No need to check the extended bios flags as some of the
361 * extensions that cause us problems are not flagged in that byte.
363 if (PROBABLY_NEW_BOARD(aha->boardid) ||
364 (aha->boardid == 0x41
365 && aha->fw_major == 0x31 &&
366 aha->fw_minor >= 0x34)) {
367 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
368 /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
369 DEFAULT_CMD_TIMEOUT);
371 device_printf(aha->dev,
372 "AOP_RETURN_EXT_BIOS_INFO - Failed.");
375 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
376 /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
378 device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
382 if (aha->boardid < 0x41)
383 device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
385 aha->max_sg = 17; /* Need >= 17 to do 64k I/O */
387 aha->extended_lun = 0;
388 aha->extended_trans = 0;
390 /* Determine Sync/Wide/Disc settings */
391 length_param = sizeof(setup_info);
392 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
393 /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
394 DEFAULT_CMD_TIMEOUT);
396 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
400 if (setup_info.initiate_sync != 0) {
401 aha->sync_permitted = ALL_TARGETS;
403 aha->disc_permitted = ALL_TARGETS;
405 /* We need as many mailboxes as we can have ccbs */
406 aha->num_boxes = aha->max_ccbs;
408 /* Determine our SCSI ID */
409 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
410 (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
412 device_printf(aha->dev,
413 "aha_fetch_adapter_info - Failed Get Config\n");
416 aha->scsi_id = config_data.scsi_id;
421 * Start the board, ready for normal operation
424 aha_init(struct aha_softc* aha)
426 /* Announce the Adapter */
427 device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
428 aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
430 if (aha->diff_bus != 0)
433 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
437 * Create our DMA tags. These tags define the kinds of device
438 * accessible memory allocations and memory mappings we will
439 * need to perform during normal operation.
441 * Unless we need to further restrict the allocation, we rely
442 * on the restrictions of the parent dmat, hence the common
443 * use of MAXADDR and MAXSIZE.
446 /* DMA tag for mapping buffers into device visible space. */
447 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
450 /* lowaddr */ BUS_SPACE_MAXADDR,
451 /* highaddr */ BUS_SPACE_MAXADDR,
453 /* filterarg */ NULL,
454 /* maxsize */ DFLTPHYS,
455 /* nsegments */ AHA_NSEG,
456 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
457 /* flags */ BUS_DMA_ALLOCNOW,
458 /* lockfunc */ busdma_lock_mutex,
459 /* lockarg */ &aha->lock,
460 &aha->buffer_dmat) != 0) {
465 /* DMA tag for our mailboxes */
466 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
469 /* lowaddr */ BUS_SPACE_MAXADDR,
470 /* highaddr */ BUS_SPACE_MAXADDR,
472 /* filterarg */ NULL,
473 /* maxsize */ aha->num_boxes *
474 (sizeof(aha_mbox_in_t) +
475 sizeof(aha_mbox_out_t)),
477 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
481 &aha->mailbox_dmat) != 0) {
487 /* Allocation for our mailboxes */
488 if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
489 BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
494 /* And permanently map them */
495 bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
496 aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
497 sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
501 aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
505 /* DMA tag for our ccb structures */
506 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
509 /* lowaddr */ BUS_SPACE_MAXADDR,
510 /* highaddr */ BUS_SPACE_MAXADDR,
512 /* filterarg */ NULL,
513 /* maxsize */ aha->max_ccbs *
514 sizeof(struct aha_ccb),
516 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
520 &aha->ccb_dmat) != 0) {
526 /* Allocation for our ccbs */
527 if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
528 BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
533 /* And permanently map them */
534 bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
535 aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
539 /* DMA tag for our S/G structures. We allocate in page sized chunks */
540 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
543 /* lowaddr */ BUS_SPACE_MAXADDR,
544 /* highaddr */ BUS_SPACE_MAXADDR,
546 /* filterarg */ NULL,
547 /* maxsize */ PAGE_SIZE,
549 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
558 /* Perform initial CCB allocation */
559 bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
562 if (aha->num_ccbs == 0) {
563 device_printf(aha->dev,
564 "aha_init - Unable to allocate initial ccbs\n");
569 * Note that we are going and return (to probe)
579 aha_attach(struct aha_softc *aha)
581 int tagged_dev_openings;
582 struct cam_devq *devq;
585 * We don't do tagged queueing, since the aha cards don't
588 tagged_dev_openings = 0;
591 * Create the device queue for our SIM.
593 devq = cam_simq_alloc(aha->max_ccbs - 1);
598 * Construct our SIM entry
600 aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha,
601 device_get_unit(aha->dev), &aha->lock, 2, tagged_dev_openings,
603 if (aha->sim == NULL) {
607 mtx_lock(&aha->lock);
608 if (xpt_bus_register(aha->sim, aha->dev, 0) != CAM_SUCCESS) {
609 cam_sim_free(aha->sim, /*free_devq*/TRUE);
610 mtx_unlock(&aha->lock);
613 if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
614 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
615 xpt_bus_deregister(cam_sim_path(aha->sim));
616 cam_sim_free(aha->sim, /*free_devq*/TRUE);
617 mtx_unlock(&aha->lock);
620 mtx_unlock(&aha->lock);
626 ahaallocccbs(struct aha_softc *aha)
628 struct aha_ccb *next_ccb;
629 struct sg_map_node *sg_map;
635 next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
637 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
642 /* Allocate S/G space for the next batch of CCBS */
643 if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
644 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
645 free(sg_map, M_DEVBUF);
649 SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
651 bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
652 PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
654 segs = sg_map->sg_vaddr;
655 physaddr = sg_map->sg_physaddr;
657 newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
658 for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
661 next_ccb->sg_list = segs;
662 next_ccb->sg_list_phys = physaddr;
663 next_ccb->flags = ACCB_FREE;
664 callout_init_mtx(&next_ccb->timer, &aha->lock, 0);
665 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
669 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
671 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
676 /* Reserve a CCB for error recovery */
677 if (aha->recovery_accb == NULL) {
678 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
679 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
684 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
688 mtx_assert(&aha->lock, MA_OWNED);
689 if ((accb->flags & ACCB_ACTIVE) != 0)
690 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
691 if (aha->resource_shortage != 0
692 && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
693 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
694 aha->resource_shortage = FALSE;
696 accb->flags = ACCB_FREE;
697 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
701 static struct aha_ccb*
702 ahagetccb(struct aha_softc *aha)
704 struct aha_ccb* accb;
707 mtx_assert(&aha->lock, MA_OWNED);
708 if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
709 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
711 } else if (aha->num_ccbs < aha->max_ccbs) {
713 accb = SLIST_FIRST(&aha->free_aha_ccbs);
715 device_printf(aha->dev, "Can't malloc ACCB\n");
717 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
726 ahaaction(struct cam_sim *sim, union ccb *ccb)
728 struct aha_softc *aha;
730 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
732 aha = (struct aha_softc *)cam_sim_softc(sim);
733 mtx_assert(&aha->lock, MA_OWNED);
735 switch (ccb->ccb_h.func_code) {
736 /* Common cases first */
737 case XPT_SCSI_IO: /* Execute the requested I/O operation */
738 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ {
739 struct aha_ccb *accb;
740 struct aha_hccb *hccb;
743 * Get an accb to use.
745 if ((accb = ahagetccb(aha)) == NULL) {
746 aha->resource_shortage = TRUE;
747 xpt_freeze_simq(aha->sim, /*count*/1);
748 ccb->ccb_h.status = CAM_REQUEUE_REQ;
755 * So we can find the ACCB when an abort is requested
758 ccb->ccb_h.ccb_accb_ptr = accb;
759 ccb->ccb_h.ccb_aha_ptr = aha;
762 * Put all the arguments for the xfer in the accb
764 hccb->target = ccb->ccb_h.target_id;
765 hccb->lun = ccb->ccb_h.target_lun;
769 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
770 struct ccb_scsiio *csio;
771 struct ccb_hdr *ccbh;
776 hccb->opcode = aha->ccb_ccb_opcode;
777 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
778 hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
779 hccb->cmd_len = csio->cdb_len;
780 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
781 ccb->ccb_h.status = CAM_REQ_INVALID;
782 ahafreeccb(aha, accb);
786 hccb->sense_len = csio->sense_len;
787 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
788 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
789 bcopy(csio->cdb_io.cdb_ptr,
790 hccb->scsi_cdb, hccb->cmd_len);
792 /* I guess I could map it in... */
793 ccbh->status = CAM_REQ_INVALID;
794 ahafreeccb(aha, accb);
799 bcopy(csio->cdb_io.cdb_bytes,
800 hccb->scsi_cdb, hccb->cmd_len);
803 * If we have any data to send with this command,
804 * map it into bus space.
807 error = bus_dmamap_load_ccb(
814 if (error == EINPROGRESS) {
816 * So as to maintain ordering, freeze the
817 * controller queue until our mapping is
820 xpt_freeze_simq(aha->sim, 1);
821 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
824 hccb->opcode = INITIATOR_BUS_DEV_RESET;
825 /* No data transfer */
827 hccb->dataout = TRUE;
830 ahaexecuteccb(accb, NULL, 0, 0);
834 case XPT_ABORT: /* Abort the specified CCB */
836 ccb->ccb_h.status = CAM_REQ_INVALID;
839 case XPT_SET_TRAN_SETTINGS:
841 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
844 case XPT_GET_TRAN_SETTINGS:
845 /* Get default/user set transfer settings for the target */
847 struct ccb_trans_settings *cts = &ccb->cts;
848 u_int target_mask = 0x01 << ccb->ccb_h.target_id;
849 struct ccb_trans_settings_scsi *scsi =
850 &cts->proto_specific.scsi;
851 struct ccb_trans_settings_spi *spi =
852 &cts->xport_specific.spi;
854 cts->protocol = PROTO_SCSI;
855 cts->protocol_version = SCSI_REV_2;
856 cts->transport = XPORT_SPI;
857 cts->transport_version = 2;
858 if (cts->type == CTS_TYPE_USER_SETTINGS) {
860 if ((aha->disc_permitted & target_mask) != 0)
861 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
862 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
863 if ((aha->sync_permitted & target_mask) != 0) {
864 if (aha->boardid >= BOARD_1542CF)
865 spi->sync_period = 25;
867 spi->sync_period = 50;
869 spi->sync_period = 0;
872 if (spi->sync_period != 0)
873 spi->sync_offset = 15;
875 spi->valid = CTS_SPI_VALID_SYNC_RATE
876 | CTS_SPI_VALID_SYNC_OFFSET
877 | CTS_SPI_VALID_BUS_WIDTH
878 | CTS_SPI_VALID_DISC;
879 scsi->valid = CTS_SCSI_VALID_TQ;
881 ahafetchtransinfo(aha, cts);
884 ccb->ccb_h.status = CAM_REQ_CMP;
888 case XPT_CALC_GEOMETRY:
890 struct ccb_calc_geometry *ccg;
892 uint32_t secs_per_cylinder;
895 size_mb = ccg->volume_size
896 / ((1024L * 1024L) / ccg->block_size);
897 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
898 if (size_mb >= 2048) {
900 ccg->secs_per_track = 63;
903 ccg->secs_per_track = 32;
907 ccg->secs_per_track = 32;
909 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
910 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
911 ccb->ccb_h.status = CAM_REQ_CMP;
915 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
916 ahareset(aha, /*hardreset*/TRUE);
917 ccb->ccb_h.status = CAM_REQ_CMP;
920 case XPT_TERM_IO: /* Terminate the I/O process */
922 ccb->ccb_h.status = CAM_REQ_INVALID;
925 case XPT_PATH_INQ: /* Path routing inquiry */
927 struct ccb_pathinq *cpi = &ccb->cpi;
929 cpi->version_num = 1; /* XXX??? */
930 cpi->hba_inquiry = PI_SDTR_ABLE;
931 cpi->target_sprt = 0;
933 cpi->hba_eng_cnt = 0;
936 cpi->initiator_id = aha->scsi_id;
937 cpi->bus_id = cam_sim_bus(sim);
938 cpi->base_transfer_speed = 3300;
939 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
940 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
941 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
942 cpi->unit_number = cam_sim_unit(sim);
943 cpi->transport = XPORT_SPI;
944 cpi->transport_version = 2;
945 cpi->protocol = PROTO_SCSI;
946 cpi->protocol_version = SCSI_REV_2;
947 cpi->ccb_h.status = CAM_REQ_CMP;
952 ccb->ccb_h.status = CAM_REQ_INVALID;
959 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
961 struct aha_ccb *accb;
963 struct aha_softc *aha;
966 accb = (struct aha_ccb *)arg;
968 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
972 device_printf(aha->dev,
973 "Unexepected error 0x%x returned from "
974 "bus_dmamap_load\n", error);
975 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
976 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
977 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
979 ahafreeccb(aha, accb);
986 bus_dma_segment_t *end_seg;
989 end_seg = dm_segs + nseg;
991 /* Copy the segments into our SG list */
993 while (dm_segs < end_seg) {
994 ahautoa24(dm_segs->ds_len, sg->len);
995 ahautoa24(dm_segs->ds_addr, sg->addr);
1001 accb->hccb.opcode = aha->ccb_sg_opcode;
1002 ahautoa24((sizeof(aha_sg_t) * nseg),
1003 accb->hccb.data_len);
1004 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1006 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1007 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1010 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1011 op = BUS_DMASYNC_PREREAD;
1013 op = BUS_DMASYNC_PREWRITE;
1015 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1018 accb->hccb.opcode = INITIATOR_CCB;
1019 ahautoa24(0, accb->hccb.data_len);
1020 ahautoa24(0, accb->hccb.data_addr);
1024 * Last time we need to check if this CCB needs to
1027 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1029 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1030 ahafreeccb(aha, accb);
1035 accb->flags = ACCB_ACTIVE;
1036 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1037 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1039 callout_reset_sbt(&accb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1040 ahatimeout, accb, 0);
1042 /* Tell the adapter about this command */
1043 if (aha->cur_outbox->action_code != AMBO_FREE) {
1045 * We should never encounter a busy mailbox.
1046 * If we do, warn the user, and treat it as
1047 * a resource shortage. If the controller is
1048 * hung, one of the pending transactions will
1049 * timeout causing us to start recovery operations.
1051 device_printf(aha->dev,
1052 "Encountered busy mailbox with %d out of %d "
1053 "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1054 callout_stop(&accb->timer);
1056 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1057 ahafreeccb(aha, accb);
1058 aha->resource_shortage = TRUE;
1059 xpt_freeze_simq(aha->sim, /*count*/1);
1060 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1064 paddr = ahaccbvtop(aha, accb);
1065 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1066 aha->cur_outbox->action_code = AMBO_START;
1067 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1075 struct aha_softc *aha;
1078 mtx_lock(&aha->lock);
1079 aha_intr_locked(aha);
1080 mtx_unlock(&aha->lock);
1084 aha_intr_locked(struct aha_softc *aha)
1089 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1090 if ((intstat & CMD_COMPLETE) != 0) {
1091 aha->latched_status = aha_inb(aha, STATUS_REG);
1092 aha->command_cmp = TRUE;
1095 aha_outb(aha, CONTROL_REG, RESET_INTR);
1097 if ((intstat & IMB_LOADED) != 0) {
1098 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1099 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1100 ahadone(aha, ahaccbptov(aha, paddr),
1101 aha->cur_inbox->comp_code);
1102 aha->cur_inbox->comp_code = AMBI_FREE;
1107 if ((intstat & SCSI_BUS_RESET) != 0) {
1108 ahareset(aha, /*hardreset*/FALSE);
1114 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1117 struct ccb_scsiio *csio;
1120 csio = &accb->ccb->csio;
1122 if ((accb->flags & ACCB_ACTIVE) == 0) {
1123 device_printf(aha->dev,
1124 "ahadone - Attempt to free non-active ACCB %p\n",
1129 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1130 bus_dmasync_op_t op;
1132 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1133 op = BUS_DMASYNC_POSTREAD;
1135 op = BUS_DMASYNC_POSTWRITE;
1136 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1137 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1140 if (accb == aha->recovery_accb) {
1142 * The recovery ACCB does not have a CCB associated
1143 * with it, so short circuit the normal error handling.
1144 * We now traverse our list of pending CCBs and process
1145 * any that were terminated by the recovery CCBs action.
1146 * We also reinstate timeouts for all remaining, pending,
1149 struct cam_path *path;
1150 struct ccb_hdr *ccb_h;
1153 /* Notify all clients that a BDR occurred */
1154 error = xpt_create_path(&path, /*periph*/NULL,
1155 cam_sim_path(aha->sim), accb->hccb.target,
1158 if (error == CAM_REQ_CMP) {
1159 xpt_async(AC_SENT_BDR, path, NULL);
1160 xpt_free_path(path);
1163 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1164 while (ccb_h != NULL) {
1165 struct aha_ccb *pending_accb;
1167 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1168 if (pending_accb->hccb.target == accb->hccb.target) {
1169 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1170 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1171 ahadone(aha, pending_accb, AMBI_ERROR);
1173 callout_reset_sbt(&pending_accb->timer,
1174 SBT_1MS * ccb_h->timeout, 0, ahatimeout,
1176 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1179 device_printf(aha->dev, "No longer in timeout\n");
1183 callout_stop(&accb->timer);
1185 switch (comp_code) {
1187 device_printf(aha->dev,
1188 "ahadone - CCB completed with free status!\n");
1190 case AMBI_NOT_FOUND:
1191 device_printf(aha->dev,
1192 "ahadone - CCB Abort failed to find CCB\n");
1196 /* An error occurred */
1197 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1200 csio->resid = aha_a24tou(accb->hccb.data_len);
1201 switch(accb->hccb.ahastat) {
1202 case AHASTAT_DATARUN_ERROR:
1204 if (csio->resid <= 0) {
1205 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1210 case AHASTAT_NOERROR:
1211 csio->scsi_status = accb->hccb.sdstat;
1212 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1213 switch(csio->scsi_status) {
1214 case SCSI_STATUS_CHECK_COND:
1215 case SCSI_STATUS_CMD_TERMINATED:
1216 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1218 * The aha writes the sense data at different
1219 * offsets based on the scsi cmd len
1221 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1223 (caddr_t) &csio->sense_data,
1224 accb->hccb.sense_len);
1228 case SCSI_STATUS_OK:
1229 csio->ccb_h.status = CAM_REQ_CMP;
1233 case AHASTAT_SELTIMEOUT:
1234 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1236 case AHASTAT_UNEXPECTED_BUSFREE:
1237 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1239 case AHASTAT_INVALID_PHASE:
1240 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1242 case AHASTAT_INVALID_ACTION_CODE:
1243 panic("%s: Inavlid Action code", aha_name(aha));
1245 case AHASTAT_INVALID_OPCODE:
1246 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1247 panic("%s: Invalid CCB Opcode %x hccb = %p",
1248 aha_name(aha), accb->hccb.opcode,
1250 device_printf(aha->dev,
1251 "AHA-1540A compensation failed\n");
1252 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1253 csio->ccb_h.status = CAM_REQUEUE_REQ;
1255 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1256 /* We don't even support linked commands... */
1257 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1259 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1260 panic("%s: Invalid CCB or SG list", aha_name(aha));
1262 case AHASTAT_HA_SCSI_BUS_RESET:
1263 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1265 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1267 case AHASTAT_HA_BDR:
1268 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1269 csio->ccb_h.status = CAM_BDR_SENT;
1271 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1274 if (csio->ccb_h.status != CAM_REQ_CMP) {
1275 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1276 csio->ccb_h.status |= CAM_DEV_QFRZN;
1278 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1279 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1280 ahafreeccb(aha, accb);
1284 /* All completed without incident */
1285 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1286 /* I don't think so since it works???? */
1287 ccb->ccb_h.status |= CAM_REQ_CMP;
1288 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1289 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1290 ahafreeccb(aha, accb);
1297 ahareset(struct aha_softc* aha, int hard_reset)
1299 struct ccb_hdr *ccb_h;
1304 if (hard_reset != 0)
1305 reset_type = HARD_RESET;
1307 reset_type = SOFT_RESET;
1308 aha_outb(aha, CONTROL_REG, reset_type);
1310 /* Wait 5sec. for Diagnostic start */
1311 timeout = 5 * 10000;
1313 status = aha_inb(aha, STATUS_REG);
1314 if ((status & DIAG_ACTIVE) != 0)
1319 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1320 "assert. status = %#x\n", status));
1324 /* Wait 10sec. for Diagnostic end */
1325 timeout = 10 * 10000;
1327 status = aha_inb(aha, STATUS_REG);
1328 if ((status & DIAG_ACTIVE) == 0)
1333 panic("%s: ahareset - Diagnostic Active failed to drop. "
1334 "status = 0x%x\n", aha_name(aha), status);
1338 /* Wait for the host adapter to become ready or report a failure */
1341 status = aha_inb(aha, STATUS_REG);
1342 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1347 device_printf(aha->dev, "ahareset - Host adapter failed to "
1348 "come ready. status = 0x%x\n", status);
1352 /* If the diagnostics failed, tell the user */
1353 if ((status & DIAG_FAIL) != 0
1354 || (status & HA_READY) == 0) {
1355 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1357 if ((status & DATAIN_REG_READY) != 0)
1358 device_printf(aha->dev, "ahareset - Host Adapter "
1359 "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1363 /* If we've attached to the XPT, tell it about the event */
1364 if (aha->path != NULL)
1365 xpt_async(AC_BUS_RESET, aha->path, NULL);
1368 * Perform completion processing for all outstanding CCBs.
1370 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1371 struct aha_ccb *pending_accb;
1373 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1374 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1375 ahadone(aha, pending_accb, AMBI_ERROR);
1378 /* If we've allocated mailboxes, initialize them */
1379 /* Must be done after we've aborted our queue, or aha_cmd fails */
1380 if (aha->init_level > 4)
1387 * Send a command to the adapter.
1390 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1391 u_int param_len, uint8_t *reply_data, u_int reply_len,
1398 u_int reply_buf_size;
1402 /* No data returned to start */
1403 reply_buf_size = reply_len;
1411 * All commands except for the "start mailbox" and the "enable
1412 * outgoing mailbox read interrupt" commands cannot be issued
1413 * while there are pending transactions. Freeze our SIMQ
1414 * and wait for all completions to occur if necessary.
1417 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1418 /* Fire the interrupt handler in case interrupts are blocked */
1424 device_printf(aha->dev,
1425 "aha_cmd: Timeout waiting for adapter idle\n");
1428 aha->command_cmp = 0;
1430 * Wait up to 10 sec. for the adapter to become
1431 * ready to accept commands.
1435 status = aha_inb(aha, STATUS_REG);
1436 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1439 * Throw away any pending data which may be
1440 * left over from earlier commands that we
1443 if ((status & DATAIN_REG_READY) != 0)
1444 (void)aha_inb(aha, DATAIN_REG);
1448 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1449 " ready, status = 0x%x\n", status);
1454 * Send the opcode followed by any necessary parameter bytes.
1456 aha_outb(aha, COMMAND_REG, opcode);
1459 * Wait for up to 1sec to get the parameter list sent
1462 while (param_len && --timeout) {
1464 status = aha_inb(aha, STATUS_REG);
1465 intstat = aha_inb(aha, INTSTAT_REG);
1467 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1468 == (INTR_PENDING|CMD_COMPLETE)) {
1469 saved_status = status;
1474 if (aha->command_cmp != 0) {
1475 saved_status = aha->latched_status;
1479 if ((status & DATAIN_REG_READY) != 0)
1481 if ((status & CMD_REG_BUSY) == 0) {
1482 aha_outb(aha, COMMAND_REG, *params++);
1488 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1489 "status = 0x%x\n", status);
1494 * For all other commands, we wait for any output data
1495 * and the final comand completion interrupt.
1497 while (cmd_complete == 0 && --cmd_timeout) {
1499 status = aha_inb(aha, STATUS_REG);
1500 intstat = aha_inb(aha, INTSTAT_REG);
1502 if (aha->command_cmp != 0) {
1504 saved_status = aha->latched_status;
1505 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1506 == (INTR_PENDING|CMD_COMPLETE)) {
1508 * Our poll (in case interrupts are blocked)
1509 * saw the CMD_COMPLETE interrupt.
1512 saved_status = status;
1514 if ((status & DATAIN_REG_READY) != 0) {
1517 data = aha_inb(aha, DATAIN_REG);
1518 if (reply_len < reply_buf_size) {
1519 *reply_data++ = data;
1521 device_printf(aha->dev,
1522 "aha_cmd - Discarded reply data "
1523 "byte for opcode 0x%x\n", opcode);
1526 * Reset timeout to ensure at least a second
1527 * between response bytes.
1529 cmd_timeout = MAX(cmd_timeout, 10000);
1534 if (cmd_timeout == 0) {
1535 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1536 "intstat = 0x%x, reply_len = %d\n", status, intstat,
1542 * Clear any pending interrupts. Block interrupts so our
1543 * interrupt handler is not re-entered.
1551 * If the command was rejected by the controller, tell the caller.
1553 if ((saved_status & CMD_INVALID) != 0) {
1554 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1556 * Some early adapters may not recover properly from
1557 * an invalid command. If it appears that the controller
1558 * has wedged (i.e. status was not cleared by our interrupt
1559 * reset above), perform a soft reset.
1562 status = aha_inb(aha, STATUS_REG);
1563 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1564 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1565 || (status & (HA_READY|INIT_REQUIRED))
1566 != (HA_READY|INIT_REQUIRED))
1567 ahareset(aha, /*hard_reset*/FALSE);
1571 if (param_len > 0) {
1572 /* The controller did not accept the full argument list */
1573 PRVERB((aha->dev, "Controller did not accept full argument "
1574 "list (%d > 0)\n", param_len));
1578 if (reply_len != reply_buf_size) {
1579 /* Too much or too little data received */
1580 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1581 reply_len, reply_buf_size));
1585 /* We were successful */
1590 ahainitmboxes(struct aha_softc *aha)
1593 init_24b_mbox_params_t init_mbox;
1595 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1596 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1597 aha->cur_inbox = aha->in_boxes;
1598 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1599 aha->cur_outbox = aha->out_boxes;
1600 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1602 /* Tell the adapter about them */
1603 init_mbox.num_mboxes = aha->num_boxes;
1604 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1605 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1606 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1607 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1610 printf("ahainitmboxes: Initialization command failed\n");
1615 * Update the XPT's idea of the negotiated transfer
1616 * parameters for a particular target.
1619 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1621 setup_data_t setup_info;
1627 targ_syncinfo_t sync_info;
1628 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1630 target = cts->ccb_h.target_id;
1631 targ_offset = (target & 0x7);
1634 * Inquire Setup Information. This command retreives
1635 * the sync info for older models.
1637 param = sizeof(setup_info);
1638 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1639 (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1642 device_printf(aha->dev,
1643 "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1648 sync_info = setup_info.syncinfo[targ_offset];
1650 if (sync_info.sync == 0)
1651 spi->sync_offset = 0;
1653 spi->sync_offset = sync_info.offset;
1655 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1657 if (aha->boardid >= BOARD_1542CF)
1661 sync_period += 500 * sync_info.period;
1663 /* Convert ns value to standard SCSI sync rate */
1664 if (spi->sync_offset != 0)
1665 spi->sync_period = scsi_calc_syncparam(sync_period);
1667 spi->sync_period = 0;
1669 spi->valid = CTS_SPI_VALID_SYNC_RATE
1670 | CTS_SPI_VALID_SYNC_OFFSET
1671 | CTS_SPI_VALID_BUS_WIDTH;
1672 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1676 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1678 struct aha_softc* aha;
1680 aha = (struct aha_softc*)arg;
1681 aha->mailbox_physbase = segs->ds_addr;
1685 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1687 struct aha_softc* aha;
1689 aha = (struct aha_softc*)arg;
1690 aha->aha_ccb_physbase = segs->ds_addr;
1694 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1697 struct aha_softc* aha;
1699 aha = (struct aha_softc*)arg;
1700 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1704 ahapoll(struct cam_sim *sim)
1706 aha_intr_locked(cam_sim_softc(sim));
1710 ahatimeout(void *arg)
1712 struct aha_ccb *accb;
1714 struct aha_softc *aha;
1716 struct ccb_hdr *ccb_h;
1718 accb = (struct aha_ccb *)arg;
1720 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1721 mtx_assert(&aha->lock, MA_OWNED);
1722 xpt_print_path(ccb->ccb_h.path);
1723 printf("CCB %p - timed out\n", (void *)accb);
1725 if ((accb->flags & ACCB_ACTIVE) == 0) {
1726 xpt_print_path(ccb->ccb_h.path);
1727 printf("CCB %p - timed out CCB already completed\n",
1733 * In order to simplify the recovery process, we ask the XPT
1734 * layer to halt the queue of new transactions and we traverse
1735 * the list of pending CCBs and remove their timeouts. This
1736 * means that the driver attempts to clear only one error
1737 * condition at a time. In general, timeouts that occur
1738 * close together are related anyway, so there is no benefit
1739 * in attempting to handle errors in parallel. Timeouts will
1740 * be reinstated when the recovery process ends.
1742 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1743 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1744 xpt_freeze_simq(aha->sim, /*count*/1);
1745 accb->flags |= ACCB_RELEASE_SIMQ;
1748 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1749 while (ccb_h != NULL) {
1750 struct aha_ccb *pending_accb;
1752 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1753 callout_stop(&pending_accb->timer);
1754 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1758 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1759 || aha->cur_outbox->action_code != AMBO_FREE) {
1761 * Try a full host adapter/SCSI bus reset.
1762 * We do this only if we have already attempted
1763 * to clear the condition with a BDR, or we cannot
1764 * attempt a BDR for lack of mailbox resources.
1766 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1767 ahareset(aha, /*hardreset*/TRUE);
1768 device_printf(aha->dev, "No longer in timeout\n");
1771 * Send a Bus Device Reset message:
1772 * The target that is holding up the bus may not
1773 * be the same as the one that triggered this timeout
1774 * (different commands have different timeout lengths),
1775 * but we have no way of determining this from our
1776 * timeout handler. Our strategy here is to queue a
1777 * BDR message to the target of the timed out command.
1778 * If this fails, we'll get another timeout 2 seconds
1779 * later which will attempt a bus reset.
1781 accb->flags |= ACCB_DEVICE_RESET;
1782 callout_reset(&accb->timer, 2 * hz, ahatimeout, accb);
1783 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1785 /* No Data Transfer */
1786 aha->recovery_accb->hccb.datain = TRUE;
1787 aha->recovery_accb->hccb.dataout = TRUE;
1788 aha->recovery_accb->hccb.ahastat = 0;
1789 aha->recovery_accb->hccb.sdstat = 0;
1790 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1792 /* Tell the adapter about this command */
1793 paddr = ahaccbvtop(aha, aha->recovery_accb);
1794 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1795 aha->cur_outbox->action_code = AMBO_START;
1796 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1802 aha_detach(struct aha_softc *aha)
1804 mtx_lock(&aha->lock);
1805 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1806 xpt_free_path(aha->path);
1807 xpt_bus_deregister(cam_sim_path(aha->sim));
1808 cam_sim_free(aha->sim, /*free_devq*/TRUE);
1809 mtx_unlock(&aha->lock);
1810 /* XXX: Drain all timers? */
1813 MODULE_DEPEND(aha, cam, 1, 1, 1);