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_EN_LUN: /* Enable LUN as a target */
835 case XPT_TARGET_IO: /* Execute target I/O request */
836 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
837 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
838 case XPT_ABORT: /* Abort the specified CCB */
840 ccb->ccb_h.status = CAM_REQ_INVALID;
843 case XPT_SET_TRAN_SETTINGS:
845 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
848 case XPT_GET_TRAN_SETTINGS:
849 /* Get default/user set transfer settings for the target */
851 struct ccb_trans_settings *cts = &ccb->cts;
852 u_int target_mask = 0x01 << ccb->ccb_h.target_id;
853 struct ccb_trans_settings_scsi *scsi =
854 &cts->proto_specific.scsi;
855 struct ccb_trans_settings_spi *spi =
856 &cts->xport_specific.spi;
858 cts->protocol = PROTO_SCSI;
859 cts->protocol_version = SCSI_REV_2;
860 cts->transport = XPORT_SPI;
861 cts->transport_version = 2;
862 if (cts->type == CTS_TYPE_USER_SETTINGS) {
864 if ((aha->disc_permitted & target_mask) != 0)
865 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
866 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
867 if ((aha->sync_permitted & target_mask) != 0) {
868 if (aha->boardid >= BOARD_1542CF)
869 spi->sync_period = 25;
871 spi->sync_period = 50;
873 spi->sync_period = 0;
876 if (spi->sync_period != 0)
877 spi->sync_offset = 15;
879 spi->valid = CTS_SPI_VALID_SYNC_RATE
880 | CTS_SPI_VALID_SYNC_OFFSET
881 | CTS_SPI_VALID_BUS_WIDTH
882 | CTS_SPI_VALID_DISC;
883 scsi->valid = CTS_SCSI_VALID_TQ;
885 ahafetchtransinfo(aha, cts);
888 ccb->ccb_h.status = CAM_REQ_CMP;
892 case XPT_CALC_GEOMETRY:
894 struct ccb_calc_geometry *ccg;
896 uint32_t secs_per_cylinder;
899 size_mb = ccg->volume_size
900 / ((1024L * 1024L) / ccg->block_size);
901 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
902 if (size_mb >= 2048) {
904 ccg->secs_per_track = 63;
907 ccg->secs_per_track = 32;
911 ccg->secs_per_track = 32;
913 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
914 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
915 ccb->ccb_h.status = CAM_REQ_CMP;
919 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
920 ahareset(aha, /*hardreset*/TRUE);
921 ccb->ccb_h.status = CAM_REQ_CMP;
924 case XPT_TERM_IO: /* Terminate the I/O process */
926 ccb->ccb_h.status = CAM_REQ_INVALID;
929 case XPT_PATH_INQ: /* Path routing inquiry */
931 struct ccb_pathinq *cpi = &ccb->cpi;
933 cpi->version_num = 1; /* XXX??? */
934 cpi->hba_inquiry = PI_SDTR_ABLE;
935 cpi->target_sprt = 0;
937 cpi->hba_eng_cnt = 0;
940 cpi->initiator_id = aha->scsi_id;
941 cpi->bus_id = cam_sim_bus(sim);
942 cpi->base_transfer_speed = 3300;
943 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
944 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
945 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
946 cpi->unit_number = cam_sim_unit(sim);
947 cpi->transport = XPORT_SPI;
948 cpi->transport_version = 2;
949 cpi->protocol = PROTO_SCSI;
950 cpi->protocol_version = SCSI_REV_2;
951 cpi->ccb_h.status = CAM_REQ_CMP;
956 ccb->ccb_h.status = CAM_REQ_INVALID;
963 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
965 struct aha_ccb *accb;
967 struct aha_softc *aha;
970 accb = (struct aha_ccb *)arg;
972 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
976 device_printf(aha->dev,
977 "Unexepected error 0x%x returned from "
978 "bus_dmamap_load\n", error);
979 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
980 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
981 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
983 ahafreeccb(aha, accb);
990 bus_dma_segment_t *end_seg;
993 end_seg = dm_segs + nseg;
995 /* Copy the segments into our SG list */
997 while (dm_segs < end_seg) {
998 ahautoa24(dm_segs->ds_len, sg->len);
999 ahautoa24(dm_segs->ds_addr, sg->addr);
1005 accb->hccb.opcode = aha->ccb_sg_opcode;
1006 ahautoa24((sizeof(aha_sg_t) * nseg),
1007 accb->hccb.data_len);
1008 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1010 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1011 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1014 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1015 op = BUS_DMASYNC_PREREAD;
1017 op = BUS_DMASYNC_PREWRITE;
1019 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1022 accb->hccb.opcode = INITIATOR_CCB;
1023 ahautoa24(0, accb->hccb.data_len);
1024 ahautoa24(0, accb->hccb.data_addr);
1028 * Last time we need to check if this CCB needs to
1031 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1033 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1034 ahafreeccb(aha, accb);
1039 accb->flags = ACCB_ACTIVE;
1040 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1041 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1043 callout_reset_sbt(&accb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1044 ahatimeout, accb, 0);
1046 /* Tell the adapter about this command */
1047 if (aha->cur_outbox->action_code != AMBO_FREE) {
1049 * We should never encounter a busy mailbox.
1050 * If we do, warn the user, and treat it as
1051 * a resource shortage. If the controller is
1052 * hung, one of the pending transactions will
1053 * timeout causing us to start recovery operations.
1055 device_printf(aha->dev,
1056 "Encountered busy mailbox with %d out of %d "
1057 "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1058 callout_stop(&accb->timer);
1060 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1061 ahafreeccb(aha, accb);
1062 aha->resource_shortage = TRUE;
1063 xpt_freeze_simq(aha->sim, /*count*/1);
1064 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1068 paddr = ahaccbvtop(aha, accb);
1069 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1070 aha->cur_outbox->action_code = AMBO_START;
1071 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1079 struct aha_softc *aha;
1082 mtx_lock(&aha->lock);
1083 aha_intr_locked(aha);
1084 mtx_unlock(&aha->lock);
1088 aha_intr_locked(struct aha_softc *aha)
1093 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1094 if ((intstat & CMD_COMPLETE) != 0) {
1095 aha->latched_status = aha_inb(aha, STATUS_REG);
1096 aha->command_cmp = TRUE;
1099 aha_outb(aha, CONTROL_REG, RESET_INTR);
1101 if ((intstat & IMB_LOADED) != 0) {
1102 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1103 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1104 ahadone(aha, ahaccbptov(aha, paddr),
1105 aha->cur_inbox->comp_code);
1106 aha->cur_inbox->comp_code = AMBI_FREE;
1111 if ((intstat & SCSI_BUS_RESET) != 0) {
1112 ahareset(aha, /*hardreset*/FALSE);
1118 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1121 struct ccb_scsiio *csio;
1124 csio = &accb->ccb->csio;
1126 if ((accb->flags & ACCB_ACTIVE) == 0) {
1127 device_printf(aha->dev,
1128 "ahadone - Attempt to free non-active ACCB %p\n",
1133 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1134 bus_dmasync_op_t op;
1136 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1137 op = BUS_DMASYNC_POSTREAD;
1139 op = BUS_DMASYNC_POSTWRITE;
1140 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1141 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1144 if (accb == aha->recovery_accb) {
1146 * The recovery ACCB does not have a CCB associated
1147 * with it, so short circuit the normal error handling.
1148 * We now traverse our list of pending CCBs and process
1149 * any that were terminated by the recovery CCBs action.
1150 * We also reinstate timeouts for all remaining, pending,
1153 struct cam_path *path;
1154 struct ccb_hdr *ccb_h;
1157 /* Notify all clients that a BDR occurred */
1158 error = xpt_create_path(&path, /*periph*/NULL,
1159 cam_sim_path(aha->sim), accb->hccb.target,
1162 if (error == CAM_REQ_CMP) {
1163 xpt_async(AC_SENT_BDR, path, NULL);
1164 xpt_free_path(path);
1167 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1168 while (ccb_h != NULL) {
1169 struct aha_ccb *pending_accb;
1171 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1172 if (pending_accb->hccb.target == accb->hccb.target) {
1173 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1174 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1175 ahadone(aha, pending_accb, AMBI_ERROR);
1177 callout_reset_sbt(&pending_accb->timer,
1178 SBT_1MS * ccb_h->timeout, 0, ahatimeout,
1180 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1183 device_printf(aha->dev, "No longer in timeout\n");
1187 callout_stop(&accb->timer);
1189 switch (comp_code) {
1191 device_printf(aha->dev,
1192 "ahadone - CCB completed with free status!\n");
1194 case AMBI_NOT_FOUND:
1195 device_printf(aha->dev,
1196 "ahadone - CCB Abort failed to find CCB\n");
1200 /* An error occurred */
1201 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1204 csio->resid = aha_a24tou(accb->hccb.data_len);
1205 switch(accb->hccb.ahastat) {
1206 case AHASTAT_DATARUN_ERROR:
1208 if (csio->resid <= 0) {
1209 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1214 case AHASTAT_NOERROR:
1215 csio->scsi_status = accb->hccb.sdstat;
1216 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1217 switch(csio->scsi_status) {
1218 case SCSI_STATUS_CHECK_COND:
1219 case SCSI_STATUS_CMD_TERMINATED:
1220 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1222 * The aha writes the sense data at different
1223 * offsets based on the scsi cmd len
1225 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1227 (caddr_t) &csio->sense_data,
1228 accb->hccb.sense_len);
1232 case SCSI_STATUS_OK:
1233 csio->ccb_h.status = CAM_REQ_CMP;
1237 case AHASTAT_SELTIMEOUT:
1238 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1240 case AHASTAT_UNEXPECTED_BUSFREE:
1241 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1243 case AHASTAT_INVALID_PHASE:
1244 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1246 case AHASTAT_INVALID_ACTION_CODE:
1247 panic("%s: Inavlid Action code", aha_name(aha));
1249 case AHASTAT_INVALID_OPCODE:
1250 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1251 panic("%s: Invalid CCB Opcode %x hccb = %p",
1252 aha_name(aha), accb->hccb.opcode,
1254 device_printf(aha->dev,
1255 "AHA-1540A compensation failed\n");
1256 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1257 csio->ccb_h.status = CAM_REQUEUE_REQ;
1259 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1260 /* We don't even support linked commands... */
1261 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1263 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1264 panic("%s: Invalid CCB or SG list", aha_name(aha));
1266 case AHASTAT_HA_SCSI_BUS_RESET:
1267 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1269 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1271 case AHASTAT_HA_BDR:
1272 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1273 csio->ccb_h.status = CAM_BDR_SENT;
1275 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1278 if (csio->ccb_h.status != CAM_REQ_CMP) {
1279 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1280 csio->ccb_h.status |= CAM_DEV_QFRZN;
1282 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1283 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1284 ahafreeccb(aha, accb);
1288 /* All completed without incident */
1289 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1290 /* I don't think so since it works???? */
1291 ccb->ccb_h.status |= CAM_REQ_CMP;
1292 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1293 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1294 ahafreeccb(aha, accb);
1301 ahareset(struct aha_softc* aha, int hard_reset)
1303 struct ccb_hdr *ccb_h;
1308 if (hard_reset != 0)
1309 reset_type = HARD_RESET;
1311 reset_type = SOFT_RESET;
1312 aha_outb(aha, CONTROL_REG, reset_type);
1314 /* Wait 5sec. for Diagnostic start */
1315 timeout = 5 * 10000;
1317 status = aha_inb(aha, STATUS_REG);
1318 if ((status & DIAG_ACTIVE) != 0)
1323 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1324 "assert. status = %#x\n", status));
1328 /* Wait 10sec. for Diagnostic end */
1329 timeout = 10 * 10000;
1331 status = aha_inb(aha, STATUS_REG);
1332 if ((status & DIAG_ACTIVE) == 0)
1337 panic("%s: ahareset - Diagnostic Active failed to drop. "
1338 "status = 0x%x\n", aha_name(aha), status);
1342 /* Wait for the host adapter to become ready or report a failure */
1345 status = aha_inb(aha, STATUS_REG);
1346 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1351 device_printf(aha->dev, "ahareset - Host adapter failed to "
1352 "come ready. status = 0x%x\n", status);
1356 /* If the diagnostics failed, tell the user */
1357 if ((status & DIAG_FAIL) != 0
1358 || (status & HA_READY) == 0) {
1359 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1361 if ((status & DATAIN_REG_READY) != 0)
1362 device_printf(aha->dev, "ahareset - Host Adapter "
1363 "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1367 /* If we've attached to the XPT, tell it about the event */
1368 if (aha->path != NULL)
1369 xpt_async(AC_BUS_RESET, aha->path, NULL);
1372 * Perform completion processing for all outstanding CCBs.
1374 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1375 struct aha_ccb *pending_accb;
1377 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1378 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1379 ahadone(aha, pending_accb, AMBI_ERROR);
1382 /* If we've allocated mailboxes, initialize them */
1383 /* Must be done after we've aborted our queue, or aha_cmd fails */
1384 if (aha->init_level > 4)
1391 * Send a command to the adapter.
1394 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1395 u_int param_len, uint8_t *reply_data, u_int reply_len,
1402 u_int reply_buf_size;
1406 /* No data returned to start */
1407 reply_buf_size = reply_len;
1415 * All commands except for the "start mailbox" and the "enable
1416 * outgoing mailbox read interrupt" commands cannot be issued
1417 * while there are pending transactions. Freeze our SIMQ
1418 * and wait for all completions to occur if necessary.
1421 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1422 /* Fire the interrupt handler in case interrupts are blocked */
1428 device_printf(aha->dev,
1429 "aha_cmd: Timeout waiting for adapter idle\n");
1432 aha->command_cmp = 0;
1434 * Wait up to 10 sec. for the adapter to become
1435 * ready to accept commands.
1439 status = aha_inb(aha, STATUS_REG);
1440 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1443 * Throw away any pending data which may be
1444 * left over from earlier commands that we
1447 if ((status & DATAIN_REG_READY) != 0)
1448 (void)aha_inb(aha, DATAIN_REG);
1452 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1453 " ready, status = 0x%x\n", status);
1458 * Send the opcode followed by any necessary parameter bytes.
1460 aha_outb(aha, COMMAND_REG, opcode);
1463 * Wait for up to 1sec to get the parameter list sent
1466 while (param_len && --timeout) {
1468 status = aha_inb(aha, STATUS_REG);
1469 intstat = aha_inb(aha, INTSTAT_REG);
1471 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1472 == (INTR_PENDING|CMD_COMPLETE)) {
1473 saved_status = status;
1478 if (aha->command_cmp != 0) {
1479 saved_status = aha->latched_status;
1483 if ((status & DATAIN_REG_READY) != 0)
1485 if ((status & CMD_REG_BUSY) == 0) {
1486 aha_outb(aha, COMMAND_REG, *params++);
1492 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1493 "status = 0x%x\n", status);
1498 * For all other commands, we wait for any output data
1499 * and the final comand completion interrupt.
1501 while (cmd_complete == 0 && --cmd_timeout) {
1503 status = aha_inb(aha, STATUS_REG);
1504 intstat = aha_inb(aha, INTSTAT_REG);
1506 if (aha->command_cmp != 0) {
1508 saved_status = aha->latched_status;
1509 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1510 == (INTR_PENDING|CMD_COMPLETE)) {
1512 * Our poll (in case interrupts are blocked)
1513 * saw the CMD_COMPLETE interrupt.
1516 saved_status = status;
1518 if ((status & DATAIN_REG_READY) != 0) {
1521 data = aha_inb(aha, DATAIN_REG);
1522 if (reply_len < reply_buf_size) {
1523 *reply_data++ = data;
1525 device_printf(aha->dev,
1526 "aha_cmd - Discarded reply data "
1527 "byte for opcode 0x%x\n", opcode);
1530 * Reset timeout to ensure at least a second
1531 * between response bytes.
1533 cmd_timeout = MAX(cmd_timeout, 10000);
1538 if (cmd_timeout == 0) {
1539 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1540 "intstat = 0x%x, reply_len = %d\n", status, intstat,
1546 * Clear any pending interrupts. Block interrupts so our
1547 * interrupt handler is not re-entered.
1555 * If the command was rejected by the controller, tell the caller.
1557 if ((saved_status & CMD_INVALID) != 0) {
1558 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1560 * Some early adapters may not recover properly from
1561 * an invalid command. If it appears that the controller
1562 * has wedged (i.e. status was not cleared by our interrupt
1563 * reset above), perform a soft reset.
1566 status = aha_inb(aha, STATUS_REG);
1567 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1568 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1569 || (status & (HA_READY|INIT_REQUIRED))
1570 != (HA_READY|INIT_REQUIRED))
1571 ahareset(aha, /*hard_reset*/FALSE);
1575 if (param_len > 0) {
1576 /* The controller did not accept the full argument list */
1577 PRVERB((aha->dev, "Controller did not accept full argument "
1578 "list (%d > 0)\n", param_len));
1582 if (reply_len != reply_buf_size) {
1583 /* Too much or too little data received */
1584 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1585 reply_len, reply_buf_size));
1589 /* We were successful */
1594 ahainitmboxes(struct aha_softc *aha)
1597 init_24b_mbox_params_t init_mbox;
1599 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1600 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1601 aha->cur_inbox = aha->in_boxes;
1602 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1603 aha->cur_outbox = aha->out_boxes;
1604 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1606 /* Tell the adapter about them */
1607 init_mbox.num_mboxes = aha->num_boxes;
1608 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1609 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1610 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1611 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1614 printf("ahainitmboxes: Initialization command failed\n");
1619 * Update the XPT's idea of the negotiated transfer
1620 * parameters for a particular target.
1623 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1625 setup_data_t setup_info;
1631 targ_syncinfo_t sync_info;
1632 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1634 target = cts->ccb_h.target_id;
1635 targ_offset = (target & 0x7);
1638 * Inquire Setup Information. This command retreives
1639 * the sync info for older models.
1641 param = sizeof(setup_info);
1642 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1643 (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1646 device_printf(aha->dev,
1647 "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1652 sync_info = setup_info.syncinfo[targ_offset];
1654 if (sync_info.sync == 0)
1655 spi->sync_offset = 0;
1657 spi->sync_offset = sync_info.offset;
1659 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1661 if (aha->boardid >= BOARD_1542CF)
1665 sync_period += 500 * sync_info.period;
1667 /* Convert ns value to standard SCSI sync rate */
1668 if (spi->sync_offset != 0)
1669 spi->sync_period = scsi_calc_syncparam(sync_period);
1671 spi->sync_period = 0;
1673 spi->valid = CTS_SPI_VALID_SYNC_RATE
1674 | CTS_SPI_VALID_SYNC_OFFSET
1675 | CTS_SPI_VALID_BUS_WIDTH;
1676 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1680 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1682 struct aha_softc* aha;
1684 aha = (struct aha_softc*)arg;
1685 aha->mailbox_physbase = segs->ds_addr;
1689 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1691 struct aha_softc* aha;
1693 aha = (struct aha_softc*)arg;
1694 aha->aha_ccb_physbase = segs->ds_addr;
1698 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1701 struct aha_softc* aha;
1703 aha = (struct aha_softc*)arg;
1704 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1708 ahapoll(struct cam_sim *sim)
1710 aha_intr_locked(cam_sim_softc(sim));
1714 ahatimeout(void *arg)
1716 struct aha_ccb *accb;
1718 struct aha_softc *aha;
1720 struct ccb_hdr *ccb_h;
1722 accb = (struct aha_ccb *)arg;
1724 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1725 mtx_assert(&aha->lock, MA_OWNED);
1726 xpt_print_path(ccb->ccb_h.path);
1727 printf("CCB %p - timed out\n", (void *)accb);
1729 if ((accb->flags & ACCB_ACTIVE) == 0) {
1730 xpt_print_path(ccb->ccb_h.path);
1731 printf("CCB %p - timed out CCB already completed\n",
1737 * In order to simplify the recovery process, we ask the XPT
1738 * layer to halt the queue of new transactions and we traverse
1739 * the list of pending CCBs and remove their timeouts. This
1740 * means that the driver attempts to clear only one error
1741 * condition at a time. In general, timeouts that occur
1742 * close together are related anyway, so there is no benefit
1743 * in attempting to handle errors in parallel. Timeouts will
1744 * be reinstated when the recovery process ends.
1746 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1747 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1748 xpt_freeze_simq(aha->sim, /*count*/1);
1749 accb->flags |= ACCB_RELEASE_SIMQ;
1752 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1753 while (ccb_h != NULL) {
1754 struct aha_ccb *pending_accb;
1756 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1757 callout_stop(&pending_accb->timer);
1758 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1762 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1763 || aha->cur_outbox->action_code != AMBO_FREE) {
1765 * Try a full host adapter/SCSI bus reset.
1766 * We do this only if we have already attempted
1767 * to clear the condition with a BDR, or we cannot
1768 * attempt a BDR for lack of mailbox resources.
1770 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1771 ahareset(aha, /*hardreset*/TRUE);
1772 device_printf(aha->dev, "No longer in timeout\n");
1775 * Send a Bus Device Reset message:
1776 * The target that is holding up the bus may not
1777 * be the same as the one that triggered this timeout
1778 * (different commands have different timeout lengths),
1779 * but we have no way of determining this from our
1780 * timeout handler. Our strategy here is to queue a
1781 * BDR message to the target of the timed out command.
1782 * If this fails, we'll get another timeout 2 seconds
1783 * later which will attempt a bus reset.
1785 accb->flags |= ACCB_DEVICE_RESET;
1786 callout_reset(&accb->timer, 2 * hz, ahatimeout, accb);
1787 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1789 /* No Data Transfer */
1790 aha->recovery_accb->hccb.datain = TRUE;
1791 aha->recovery_accb->hccb.dataout = TRUE;
1792 aha->recovery_accb->hccb.ahastat = 0;
1793 aha->recovery_accb->hccb.sdstat = 0;
1794 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1796 /* Tell the adapter about this command */
1797 paddr = ahaccbvtop(aha, aha->recovery_accb);
1798 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1799 aha->cur_outbox->action_code = AMBO_START;
1800 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1806 aha_detach(struct aha_softc *aha)
1808 mtx_lock(&aha->lock);
1809 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1810 xpt_free_path(aha->path);
1811 xpt_bus_deregister(cam_sim_path(aha->sim));
1812 cam_sim_free(aha->sim, /*free_devq*/TRUE);
1813 mtx_unlock(&aha->lock);
1814 /* XXX: Drain all timers? */
1817 MODULE_DEPEND(aha, cam, 1, 1, 1);