2 * Generic register and struct definitions for the Adaptech 154x/164x
3 * SCSI host adapters. Product specific probe and attach routines can
5 * aha 1542A/1542B/1542C/1542CF/1542CP aha_isa.c
9 * Copyright (c) 1998 M. Warner Losh.
10 * All Rights Reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Derived from bt.c written by:
35 * Copyright (c) 1998 Justin T. Gibbs.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions, and the following disclaimer,
43 * without modification, immediately at the beginning of the file.
44 * 2. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
51 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
69 #include <sys/mutex.h>
71 #include <machine/bus.h>
74 #include <cam/cam_ccb.h>
75 #include <cam/cam_sim.h>
76 #include <cam/cam_xpt_sim.h>
77 #include <cam/cam_debug.h>
79 #include <cam/scsi/scsi_message.h>
81 #include <dev/aha/ahareg.h>
83 #define PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
85 /* Macro to determine that a rev is potentially a new valid one
86 * so that the driver doesn't keep breaking on new revs as it
87 * did for the CF and CP.
89 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
91 /* MailBox Management functions */
92 static __inline void ahanextinbox(struct aha_softc *aha);
93 static __inline void ahanextoutbox(struct aha_softc *aha);
95 #define aha_name(aha) device_get_nameunit(aha->dev)
98 ahanextinbox(struct aha_softc *aha)
100 if (aha->cur_inbox == aha->last_inbox)
101 aha->cur_inbox = aha->in_boxes;
107 ahanextoutbox(struct aha_softc *aha)
109 if (aha->cur_outbox == aha->last_outbox)
110 aha->cur_outbox = aha->out_boxes;
115 #define ahautoa24(u,s3) \
116 (s3)[0] = ((u) >> 16) & 0xff; \
117 (s3)[1] = ((u) >> 8) & 0xff; \
118 (s3)[2] = (u) & 0xff;
120 #define aha_a24tou(s3) \
121 (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
123 /* CCB Mangement functions */
124 static __inline uint32_t ahaccbvtop(struct aha_softc *aha,
125 struct aha_ccb *accb);
126 static __inline struct aha_ccb* ahaccbptov(struct aha_softc *aha,
129 static __inline uint32_t
130 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
132 return (aha->aha_ccb_physbase
133 + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
135 static __inline struct aha_ccb *
136 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
138 return (aha->aha_ccb_array +
139 + ((struct aha_ccb*)(uintptr_t)ccb_addr -
140 (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
143 static struct aha_ccb* ahagetccb(struct aha_softc *aha);
144 static __inline void ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
145 static void ahaallocccbs(struct aha_softc *aha);
146 static bus_dmamap_callback_t ahaexecuteccb;
147 static void ahadone(struct aha_softc *aha, struct aha_ccb *accb,
148 aha_mbi_comp_code_t comp_code);
150 /* Host adapter command functions */
151 static int ahareset(struct aha_softc* aha, int hard_reset);
153 /* Initialization functions */
154 static int ahainitmboxes(struct aha_softc *aha);
155 static bus_dmamap_callback_t ahamapmboxes;
156 static bus_dmamap_callback_t ahamapccbs;
157 static bus_dmamap_callback_t ahamapsgs;
159 /* Transfer Negotiation Functions */
160 static void ahafetchtransinfo(struct aha_softc *aha,
161 struct ccb_trans_settings *cts);
163 /* CAM SIM entry points */
164 #define ccb_accb_ptr spriv_ptr0
165 #define ccb_aha_ptr spriv_ptr1
166 static void ahaaction(struct cam_sim *sim, union ccb *ccb);
167 static void ahapoll(struct cam_sim *sim);
169 /* Our timeout handler */
170 static timeout_t ahatimeout;
172 /* Exported functions */
174 aha_alloc(struct aha_softc *aha, int unit, bus_space_tag_t tag,
175 bus_space_handle_t bsh)
178 SLIST_INIT(&aha->free_aha_ccbs);
179 LIST_INIT(&aha->pending_ccbs);
180 SLIST_INIT(&aha->sg_maps);
184 aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
185 aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
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_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);
210 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
213 bus_dma_tag_destroy(aha->ccb_dmat);
215 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
217 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
218 aha->mailbox_dmamap);
219 bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);
221 bus_dma_tag_destroy(aha->buffer_dmat);
223 bus_dma_tag_destroy(aha->mailbox_dmat);
230 * Probe the adapter and verify that the card is an Adaptec.
233 aha_probe(struct aha_softc* aha)
238 board_id_data_t board_id;
241 * See if the three I/O ports look reasonable.
242 * Touch the minimal number of registers in the
245 status = aha_inb(aha, STATUS_REG);
247 (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
248 PRVERB((aha->dev, "status reg test failed %x\n", status));
252 intstat = aha_inb(aha, INTSTAT_REG);
253 if ((intstat & INTSTAT_REG_RSVD) != 0) {
254 PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
259 * Looking good so far. Final test is to reset the
260 * adapter and fetch the board ID and ensure we aren't
261 * looking at a BusLogic.
263 if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
264 PRVERB((aha->dev, "Failed Reset\n"));
269 * Get the board ID. We use this to see if we're dealing with
270 * a buslogic card or an aha card (or clone).
272 error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
273 (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
275 PRVERB((aha->dev, "INQUIRE failed %x\n", error));
278 aha->fw_major = board_id.firmware_rev_major;
279 aha->fw_minor = board_id.firmware_rev_minor;
280 aha->boardid = board_id.board_type;
283 * The Buslogic cards have an id of either 0x41 or 0x42. So
284 * if those come up in the probe, we test the geometry register
285 * of the board. Adaptec boards that are this old will not have
286 * this register, and return 0xff, while buslogic cards will return
287 * something different.
289 * It appears that for reasons unknow, for the for the
290 * aha-1542B cards, we need to wait a little bit before trying
291 * to read the geometry register. I picked 10ms since we have
292 * reports that a for loop to 1000 did the trick, and this
293 * errs on the side of conservatism. Besides, no one will
294 * notice a 10mS delay here, even the 1542B card users :-)
296 * Some compatible cards return 0 here. Some cards also
297 * seem to return 0x7f.
299 * XXX I'm not sure how this will impact other cloned cards
301 * This really should be replaced with the esetup command, since
302 * that appears to be more reliable. This becomes more and more
303 * true over time as we discover more cards that don't read the
304 * geometry register consistantly.
306 if (aha->boardid <= 0x42) {
307 /* Wait 10ms before reading */
309 status = aha_inb(aha, GEOMETRY_REG);
310 if (status != 0xff && status != 0x00 && status != 0x7f) {
311 PRVERB((aha->dev, "Geometry Register test failed %#x\n",
321 * Pull the boards setup information and record it in our softc.
324 aha_fetch_adapter_info(struct aha_softc *aha)
326 setup_data_t setup_info;
327 config_data_t config_data;
328 uint8_t length_param;
330 struct aha_extbios extbios;
332 switch (aha->boardid) {
333 case BOARD_1540_16HEAD_BIOS:
334 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
336 case BOARD_1540_64HEAD_BIOS:
337 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
340 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
343 snprintf(aha->model, sizeof(aha->model), "1640");
346 snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");
349 snprintf(aha->model, sizeof(aha->model), "1542C");
352 snprintf(aha->model, sizeof(aha->model), "1542CF");
355 snprintf(aha->model, sizeof(aha->model), "1542CP");
358 snprintf(aha->model, sizeof(aha->model), "Unknown");
362 * If we are a new type of 1542 board (anything newer than a 1542C)
363 * then disable the extended bios so that the
364 * mailbox interface is unlocked.
365 * This is also true for the 1542B Version 3.20. First Adaptec
366 * board that supports >1Gb drives.
367 * No need to check the extended bios flags as some of the
368 * extensions that cause us problems are not flagged in that byte.
370 if (PROBABLY_NEW_BOARD(aha->boardid) ||
371 (aha->boardid == 0x41
372 && aha->fw_major == 0x31 &&
373 aha->fw_minor >= 0x34)) {
374 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
375 /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
376 DEFAULT_CMD_TIMEOUT);
378 device_printf(aha->dev,
379 "AOP_RETURN_EXT_BIOS_INFO - Failed.");
382 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
383 /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
385 device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
389 if (aha->boardid < 0x41)
390 device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
392 aha->max_sg = 17; /* Need >= 17 to do 64k I/O */
394 aha->extended_lun = 0;
395 aha->extended_trans = 0;
397 /* Determine Sync/Wide/Disc settings */
398 length_param = sizeof(setup_info);
399 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
400 /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
401 DEFAULT_CMD_TIMEOUT);
403 device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
407 if (setup_info.initiate_sync != 0) {
408 aha->sync_permitted = ALL_TARGETS;
410 aha->disc_permitted = ALL_TARGETS;
412 /* We need as many mailboxes as we can have ccbs */
413 aha->num_boxes = aha->max_ccbs;
415 /* Determine our SCSI ID */
416 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
417 (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
419 device_printf(aha->dev,
420 "aha_fetch_adapter_info - Failed Get Config\n");
423 aha->scsi_id = config_data.scsi_id;
428 * Start the board, ready for normal operation
431 aha_init(struct aha_softc* aha)
433 /* Announce the Adapter */
434 device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
435 aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
437 if (aha->diff_bus != 0)
440 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
444 * Create our DMA tags. These tags define the kinds of device
445 * accessible memory allocations and memory mappings we will
446 * need to perform during normal operation.
448 * Unless we need to further restrict the allocation, we rely
449 * on the restrictions of the parent dmat, hence the common
450 * use of MAXADDR and MAXSIZE.
453 /* DMA tag for mapping buffers into device visible space. */
454 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
457 /* lowaddr */ BUS_SPACE_MAXADDR,
458 /* highaddr */ BUS_SPACE_MAXADDR,
460 /* filterarg */ NULL,
461 /* maxsize */ MAXBSIZE,
462 /* nsegments */ AHA_NSEG,
463 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
464 /* flags */ BUS_DMA_ALLOCNOW,
465 /* lockfunc */ busdma_lock_mutex,
466 /* lockarg */ &Giant,
467 &aha->buffer_dmat) != 0) {
472 /* DMA tag for our mailboxes */
473 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
476 /* lowaddr */ BUS_SPACE_MAXADDR,
477 /* highaddr */ BUS_SPACE_MAXADDR,
479 /* filterarg */ NULL,
480 /* maxsize */ aha->num_boxes *
481 (sizeof(aha_mbox_in_t) +
482 sizeof(aha_mbox_out_t)),
484 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
486 /* lockfunc */ busdma_lock_mutex,
487 /* lockarg */ &Giant,
488 &aha->mailbox_dmat) != 0) {
494 /* Allocation for our mailboxes */
495 if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
496 BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
501 /* And permanently map them */
502 bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
503 aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
504 sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
508 aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
512 /* DMA tag for our ccb structures */
513 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
516 /* lowaddr */ BUS_SPACE_MAXADDR,
517 /* highaddr */ BUS_SPACE_MAXADDR,
519 /* filterarg */ NULL,
520 /* maxsize */ aha->max_ccbs *
521 sizeof(struct aha_ccb),
523 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
525 /* lockfunc */ busdma_lock_mutex,
526 /* lockarg */ &Giant,
527 &aha->ccb_dmat) != 0) {
533 /* Allocation for our ccbs */
534 if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
535 BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
540 /* And permanently map them */
541 bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
542 aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
546 /* DMA tag for our S/G structures. We allocate in page sized chunks */
547 if (bus_dma_tag_create( /* parent */ aha->parent_dmat,
550 /* lowaddr */ BUS_SPACE_MAXADDR,
551 /* highaddr */ BUS_SPACE_MAXADDR,
553 /* filterarg */ NULL,
554 /* maxsize */ PAGE_SIZE,
556 /* maxsegsz */ BUS_SPACE_MAXSIZE_24BIT,
558 /* lockfunc */ busdma_lock_mutex,
559 /* lockarg */ &Giant,
565 /* Perform initial CCB allocation */
566 bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
569 if (aha->num_ccbs == 0) {
570 device_printf(aha->dev,
571 "aha_init - Unable to allocate initial ccbs\n");
576 * Note that we are going and return (to probe)
586 aha_attach(struct aha_softc *aha)
588 int tagged_dev_openings;
589 struct cam_devq *devq;
592 * We don't do tagged queueing, since the aha cards don't
595 tagged_dev_openings = 0;
598 * Create the device queue for our SIM.
600 devq = cam_simq_alloc(aha->max_ccbs - 1);
605 * Construct our SIM entry
607 aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha, aha->unit, 2,
608 tagged_dev_openings, devq);
609 if (aha->sim == NULL) {
613 if (xpt_bus_register(aha->sim, 0) != CAM_SUCCESS) {
614 cam_sim_free(aha->sim, /*free_devq*/TRUE);
617 if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
618 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
619 xpt_bus_deregister(cam_sim_path(aha->sim));
620 cam_sim_free(aha->sim, /*free_devq*/TRUE);
628 ahaallocccbs(struct aha_softc *aha)
630 struct aha_ccb *next_ccb;
631 struct sg_map_node *sg_map;
637 next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
639 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
644 /* Allocate S/G space for the next batch of CCBS */
645 if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
646 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
647 free(sg_map, M_DEVBUF);
651 SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
653 bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
654 PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
656 segs = sg_map->sg_vaddr;
657 physaddr = sg_map->sg_physaddr;
659 newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
660 for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
663 next_ccb->sg_list = segs;
664 next_ccb->sg_list_phys = physaddr;
665 next_ccb->flags = ACCB_FREE;
666 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
670 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
672 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
677 /* Reserve a CCB for error recovery */
678 if (aha->recovery_accb == NULL) {
679 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
680 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
685 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
690 if ((accb->flags & ACCB_ACTIVE) != 0)
691 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
692 if (aha->resource_shortage != 0
693 && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
694 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
695 aha->resource_shortage = FALSE;
697 accb->flags = ACCB_FREE;
698 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
703 static struct aha_ccb*
704 ahagetccb(struct aha_softc *aha)
706 struct aha_ccb* accb;
710 if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
711 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
713 } else if (aha->num_ccbs < aha->max_ccbs) {
715 accb = SLIST_FIRST(&aha->free_aha_ccbs);
717 device_printf(aha->dev, "Can't malloc ACCB\n");
719 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
729 ahaaction(struct cam_sim *sim, union ccb *ccb)
731 struct aha_softc *aha;
734 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
736 aha = (struct aha_softc *)cam_sim_softc(sim);
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) {
750 aha->resource_shortage = TRUE;
752 xpt_freeze_simq(aha->sim, /*count*/1);
753 ccb->ccb_h.status = CAM_REQUEUE_REQ;
760 * So we can find the ACCB when an abort is requested
763 ccb->ccb_h.ccb_accb_ptr = accb;
764 ccb->ccb_h.ccb_aha_ptr = aha;
767 * Put all the arguments for the xfer in the accb
769 hccb->target = ccb->ccb_h.target_id;
770 hccb->lun = ccb->ccb_h.target_lun;
774 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
775 struct ccb_scsiio *csio;
776 struct ccb_hdr *ccbh;
780 hccb->opcode = aha->ccb_ccb_opcode;
781 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
782 hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
783 hccb->cmd_len = csio->cdb_len;
784 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
785 ccb->ccb_h.status = CAM_REQ_INVALID;
786 ahafreeccb(aha, accb);
790 hccb->sense_len = csio->sense_len;
791 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
792 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
793 bcopy(csio->cdb_io.cdb_ptr,
794 hccb->scsi_cdb, hccb->cmd_len);
796 /* I guess I could map it in... */
797 ccbh->status = CAM_REQ_INVALID;
798 ahafreeccb(aha, accb);
803 bcopy(csio->cdb_io.cdb_bytes,
804 hccb->scsi_cdb, hccb->cmd_len);
807 * If we have any data to send with this command,
808 * map it into bus space.
810 /* Only use S/G if there is a transfer */
811 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
812 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
814 * We've been given a pointer
815 * to a single buffer.
817 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
821 error = bus_dmamap_load(
829 if (error == EINPROGRESS) {
832 * ordering, freeze the
834 * until our mapping is
837 xpt_freeze_simq(aha->sim,
839 csio->ccb_h.status |=
844 struct bus_dma_segment seg;
846 /* Pointer to physical buffer */
848 (bus_addr_t)csio->data_ptr;
849 seg.ds_len = csio->dxfer_len;
850 ahaexecuteccb(accb, &seg, 1, 0);
853 struct bus_dma_segment *segs;
855 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
856 panic("ahaaction - Physical "
860 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
861 panic("ahaaction - Virtual "
865 /* Just use the segments provided */
866 segs = (struct bus_dma_segment *)
868 ahaexecuteccb(accb, segs,
869 csio->sglist_cnt, 0);
872 ahaexecuteccb(accb, NULL, 0, 0);
875 hccb->opcode = INITIATOR_BUS_DEV_RESET;
876 /* No data transfer */
878 hccb->dataout = TRUE;
881 ahaexecuteccb(accb, NULL, 0, 0);
885 case XPT_EN_LUN: /* Enable LUN as a target */
886 case XPT_TARGET_IO: /* Execute target I/O request */
887 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
888 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
889 case XPT_ABORT: /* Abort the specified CCB */
891 ccb->ccb_h.status = CAM_REQ_INVALID;
894 case XPT_SET_TRAN_SETTINGS:
896 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
899 case XPT_GET_TRAN_SETTINGS:
900 /* Get default/user set transfer settings for the target */
902 struct ccb_trans_settings *cts;
906 target_mask = 0x01 << ccb->ccb_h.target_id;
907 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
909 if ((aha->disc_permitted & target_mask) != 0)
910 cts->flags |= CCB_TRANS_DISC_ENB;
911 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
912 if ((aha->sync_permitted & target_mask) != 0) {
913 if (aha->boardid >= BOARD_1542CF)
914 cts->sync_period = 25;
916 cts->sync_period = 50;
918 cts->sync_period = 0;
920 if (cts->sync_period != 0)
921 cts->sync_offset = 15;
923 cts->valid = CCB_TRANS_SYNC_RATE_VALID
924 | CCB_TRANS_SYNC_OFFSET_VALID
925 | CCB_TRANS_BUS_WIDTH_VALID
926 | CCB_TRANS_DISC_VALID
927 | CCB_TRANS_TQ_VALID;
929 ahafetchtransinfo(aha, cts);
932 ccb->ccb_h.status = CAM_REQ_CMP;
936 case XPT_CALC_GEOMETRY:
938 struct ccb_calc_geometry *ccg;
940 uint32_t secs_per_cylinder;
943 size_mb = ccg->volume_size
944 / ((1024L * 1024L) / ccg->block_size);
945 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
946 if (size_mb >= 2048) {
948 ccg->secs_per_track = 63;
951 ccg->secs_per_track = 32;
955 ccg->secs_per_track = 32;
957 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
958 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
959 ccb->ccb_h.status = CAM_REQ_CMP;
963 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
964 ahareset(aha, /*hardreset*/TRUE);
965 ccb->ccb_h.status = CAM_REQ_CMP;
968 case XPT_TERM_IO: /* Terminate the I/O process */
970 ccb->ccb_h.status = CAM_REQ_INVALID;
973 case XPT_PATH_INQ: /* Path routing inquiry */
975 struct ccb_pathinq *cpi = &ccb->cpi;
977 cpi->version_num = 1; /* XXX??? */
978 cpi->hba_inquiry = PI_SDTR_ABLE;
979 cpi->target_sprt = 0;
981 cpi->hba_eng_cnt = 0;
984 cpi->initiator_id = aha->scsi_id;
985 cpi->bus_id = cam_sim_bus(sim);
986 cpi->base_transfer_speed = 3300;
987 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
988 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
989 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
990 cpi->unit_number = cam_sim_unit(sim);
991 cpi->ccb_h.status = CAM_REQ_CMP;
996 ccb->ccb_h.status = CAM_REQ_INVALID;
1003 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1005 struct aha_ccb *accb;
1007 struct aha_softc *aha;
1011 accb = (struct aha_ccb *)arg;
1013 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1017 device_printf(aha->dev,
1018 "Unexepected error 0x%x returned from "
1019 "bus_dmamap_load\n", error);
1020 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1021 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1022 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1024 ahafreeccb(aha, accb);
1031 bus_dma_segment_t *end_seg;
1032 bus_dmasync_op_t op;
1034 end_seg = dm_segs + nseg;
1036 /* Copy the segments into our SG list */
1038 while (dm_segs < end_seg) {
1039 ahautoa24(dm_segs->ds_len, sg->len);
1040 ahautoa24(dm_segs->ds_addr, sg->addr);
1046 accb->hccb.opcode = aha->ccb_sg_opcode;
1047 ahautoa24((sizeof(aha_sg_t) * nseg),
1048 accb->hccb.data_len);
1049 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1051 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1052 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1055 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1056 op = BUS_DMASYNC_PREREAD;
1058 op = BUS_DMASYNC_PREWRITE;
1060 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1063 accb->hccb.opcode = INITIATOR_CCB;
1064 ahautoa24(0, accb->hccb.data_len);
1065 ahautoa24(0, accb->hccb.data_addr);
1071 * Last time we need to check if this CCB needs to
1074 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1076 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1077 ahafreeccb(aha, accb);
1083 accb->flags = ACCB_ACTIVE;
1084 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1085 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1087 ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb,
1088 (ccb->ccb_h.timeout * hz) / 1000);
1090 /* Tell the adapter about this command */
1091 if (aha->cur_outbox->action_code != AMBO_FREE) {
1093 * We should never encounter a busy mailbox.
1094 * If we do, warn the user, and treat it as
1095 * a resource shortage. If the controller is
1096 * hung, one of the pending transactions will
1097 * timeout causing us to start recovery operations.
1099 device_printf(aha->dev,
1100 "Encountered busy mailbox with %d out of %d "
1101 "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1102 untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
1104 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1105 ahafreeccb(aha, accb);
1106 aha->resource_shortage = TRUE;
1107 xpt_freeze_simq(aha->sim, /*count*/1);
1108 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1112 paddr = ahaccbvtop(aha, accb);
1113 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1114 aha->cur_outbox->action_code = AMBO_START;
1115 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1124 struct aha_softc *aha;
1128 aha = (struct aha_softc *)arg;
1129 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1130 if ((intstat & CMD_COMPLETE) != 0) {
1131 aha->latched_status = aha_inb(aha, STATUS_REG);
1132 aha->command_cmp = TRUE;
1135 aha_outb(aha, CONTROL_REG, RESET_INTR);
1137 if ((intstat & IMB_LOADED) != 0) {
1138 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1139 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1140 ahadone(aha, ahaccbptov(aha, paddr),
1141 aha->cur_inbox->comp_code);
1142 aha->cur_inbox->comp_code = AMBI_FREE;
1147 if ((intstat & SCSI_BUS_RESET) != 0) {
1148 ahareset(aha, /*hardreset*/FALSE);
1154 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1157 struct ccb_scsiio *csio;
1160 csio = &accb->ccb->csio;
1162 if ((accb->flags & ACCB_ACTIVE) == 0) {
1163 device_printf(aha->dev,
1164 "ahadone - Attempt to free non-active ACCB %p\n",
1169 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1170 bus_dmasync_op_t op;
1172 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1173 op = BUS_DMASYNC_POSTREAD;
1175 op = BUS_DMASYNC_POSTWRITE;
1176 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1177 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1180 if (accb == aha->recovery_accb) {
1182 * The recovery ACCB does not have a CCB associated
1183 * with it, so short circuit the normal error handling.
1184 * We now traverse our list of pending CCBs and process
1185 * any that were terminated by the recovery CCBs action.
1186 * We also reinstate timeouts for all remaining, pending,
1189 struct cam_path *path;
1190 struct ccb_hdr *ccb_h;
1193 /* Notify all clients that a BDR occured */
1194 error = xpt_create_path(&path, /*periph*/NULL,
1195 cam_sim_path(aha->sim), accb->hccb.target,
1198 if (error == CAM_REQ_CMP)
1199 xpt_async(AC_SENT_BDR, path, NULL);
1201 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1202 while (ccb_h != NULL) {
1203 struct aha_ccb *pending_accb;
1205 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1206 if (pending_accb->hccb.target == accb->hccb.target) {
1207 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1208 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1209 ahadone(aha, pending_accb, AMBI_ERROR);
1211 ccb_h->timeout_ch = timeout(ahatimeout,
1212 (caddr_t)pending_accb,
1213 (ccb_h->timeout * hz) / 1000);
1214 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1217 device_printf(aha->dev, "No longer in timeout\n");
1221 untimeout(ahatimeout, accb, ccb->ccb_h.timeout_ch);
1223 switch (comp_code) {
1225 device_printf(aha->dev,
1226 "ahadone - CCB completed with free status!\n");
1228 case AMBI_NOT_FOUND:
1229 device_printf(aha->dev,
1230 "ahadone - CCB Abort failed to find CCB\n");
1234 /* An error occured */
1235 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1238 csio->resid = aha_a24tou(accb->hccb.data_len);
1239 switch(accb->hccb.ahastat) {
1240 case AHASTAT_DATARUN_ERROR:
1242 if (csio->resid <= 0) {
1243 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1248 case AHASTAT_NOERROR:
1249 csio->scsi_status = accb->hccb.sdstat;
1250 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1251 switch(csio->scsi_status) {
1252 case SCSI_STATUS_CHECK_COND:
1253 case SCSI_STATUS_CMD_TERMINATED:
1254 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1256 * The aha writes the sense data at different
1257 * offsets based on the scsi cmd len
1259 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1261 (caddr_t) &csio->sense_data,
1262 accb->hccb.sense_len);
1266 case SCSI_STATUS_OK:
1267 csio->ccb_h.status = CAM_REQ_CMP;
1271 case AHASTAT_SELTIMEOUT:
1272 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1274 case AHASTAT_UNEXPECTED_BUSFREE:
1275 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1277 case AHASTAT_INVALID_PHASE:
1278 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1280 case AHASTAT_INVALID_ACTION_CODE:
1281 panic("%s: Inavlid Action code", aha_name(aha));
1283 case AHASTAT_INVALID_OPCODE:
1284 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1285 panic("%s: Invalid CCB Opcode %x hccb = %p",
1286 aha_name(aha), accb->hccb.opcode,
1288 device_printf(aha->dev,
1289 "AHA-1540A compensation failed\n");
1290 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1291 csio->ccb_h.status = CAM_REQUEUE_REQ;
1293 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1294 /* We don't even support linked commands... */
1295 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1297 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1298 panic("%s: Invalid CCB or SG list", aha_name(aha));
1300 case AHASTAT_HA_SCSI_BUS_RESET:
1301 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1303 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1305 case AHASTAT_HA_BDR:
1306 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1307 csio->ccb_h.status = CAM_BDR_SENT;
1309 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1312 if (csio->ccb_h.status != CAM_REQ_CMP) {
1313 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1314 csio->ccb_h.status |= CAM_DEV_QFRZN;
1316 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1317 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1318 ahafreeccb(aha, accb);
1322 /* All completed without incident */
1323 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1324 /* I don't think so since it works???? */
1325 ccb->ccb_h.status |= CAM_REQ_CMP;
1326 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1327 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1328 ahafreeccb(aha, accb);
1335 ahareset(struct aha_softc* aha, int hard_reset)
1337 struct ccb_hdr *ccb_h;
1342 if (hard_reset != 0)
1343 reset_type = HARD_RESET;
1345 reset_type = SOFT_RESET;
1346 aha_outb(aha, CONTROL_REG, reset_type);
1348 /* Wait 5sec. for Diagnostic start */
1349 timeout = 5 * 10000;
1351 status = aha_inb(aha, STATUS_REG);
1352 if ((status & DIAG_ACTIVE) != 0)
1357 PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1358 "assert. status = %#x\n", status));
1362 /* Wait 10sec. for Diagnostic end */
1363 timeout = 10 * 10000;
1365 status = aha_inb(aha, STATUS_REG);
1366 if ((status & DIAG_ACTIVE) == 0)
1371 panic("%s: ahareset - Diagnostic Active failed to drop. "
1372 "status = 0x%x\n", aha_name(aha), status);
1376 /* Wait for the host adapter to become ready or report a failure */
1379 status = aha_inb(aha, STATUS_REG);
1380 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1385 device_printf(aha->dev, "ahareset - Host adapter failed to "
1386 "come ready. status = 0x%x\n", status);
1390 /* If the diagnostics failed, tell the user */
1391 if ((status & DIAG_FAIL) != 0
1392 || (status & HA_READY) == 0) {
1393 device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1395 if ((status & DATAIN_REG_READY) != 0)
1396 device_printf(aha->dev, "ahareset - Host Adapter "
1397 "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1401 /* If we've attached to the XPT, tell it about the event */
1402 if (aha->path != NULL)
1403 xpt_async(AC_BUS_RESET, aha->path, NULL);
1406 * Perform completion processing for all outstanding CCBs.
1408 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1409 struct aha_ccb *pending_accb;
1411 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1412 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1413 ahadone(aha, pending_accb, AMBI_ERROR);
1416 /* If we've allocated mailboxes, initialize them */
1417 /* Must be done after we've aborted our queue, or aha_cmd fails */
1418 if (aha->init_level > 4)
1425 * Send a command to the adapter.
1428 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1429 u_int param_len, uint8_t *reply_data, u_int reply_len,
1436 u_int reply_buf_size;
1441 /* No data returned to start */
1442 reply_buf_size = reply_len;
1450 * All commands except for the "start mailbox" and the "enable
1451 * outgoing mailbox read interrupt" commands cannot be issued
1452 * while there are pending transactions. Freeze our SIMQ
1453 * and wait for all completions to occur if necessary.
1457 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1458 /* Fire the interrupt handler in case interrupts are blocked */
1467 device_printf(aha->dev,
1468 "aha_cmd: Timeout waiting for adapter idle\n");
1471 aha->command_cmp = 0;
1473 * Wait up to 10 sec. for the adapter to become
1474 * ready to accept commands.
1478 status = aha_inb(aha, STATUS_REG);
1479 if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1482 * Throw away any pending data which may be
1483 * left over from earlier commands that we
1486 if ((status & DATAIN_REG_READY) != 0)
1487 (void)aha_inb(aha, DATAIN_REG);
1491 device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1492 " ready, status = 0x%x\n", status);
1497 * Send the opcode followed by any necessary parameter bytes.
1499 aha_outb(aha, COMMAND_REG, opcode);
1502 * Wait for up to 1sec to get the parameter list sent
1505 while (param_len && --timeout) {
1508 status = aha_inb(aha, STATUS_REG);
1509 intstat = aha_inb(aha, INTSTAT_REG);
1512 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1513 == (INTR_PENDING|CMD_COMPLETE)) {
1514 saved_status = status;
1519 if (aha->command_cmp != 0) {
1520 saved_status = aha->latched_status;
1524 if ((status & DATAIN_REG_READY) != 0)
1526 if ((status & CMD_REG_BUSY) == 0) {
1527 aha_outb(aha, COMMAND_REG, *params++);
1533 device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1534 "status = 0x%x\n", status);
1539 * For all other commands, we wait for any output data
1540 * and the final comand completion interrupt.
1542 while (cmd_complete == 0 && --cmd_timeout) {
1545 status = aha_inb(aha, STATUS_REG);
1546 intstat = aha_inb(aha, INTSTAT_REG);
1549 if (aha->command_cmp != 0) {
1551 saved_status = aha->latched_status;
1552 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1553 == (INTR_PENDING|CMD_COMPLETE)) {
1555 * Our poll (in case interrupts are blocked)
1556 * saw the CMD_COMPLETE interrupt.
1559 saved_status = status;
1561 if ((status & DATAIN_REG_READY) != 0) {
1564 data = aha_inb(aha, DATAIN_REG);
1565 if (reply_len < reply_buf_size) {
1566 *reply_data++ = data;
1568 device_printf(aha->dev,
1569 "aha_cmd - Discarded reply data "
1570 "byte for opcode 0x%x\n", opcode);
1573 * Reset timeout to ensure at least a second
1574 * between response bytes.
1576 cmd_timeout = MAX(cmd_timeout, 10000);
1581 if (cmd_timeout == 0) {
1582 device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1583 "intstat = 0x%x, reply_len = %d\n", status, intstat,
1589 * Clear any pending interrupts. Block interrupts so our
1590 * interrupt handler is not re-entered.
1600 * If the command was rejected by the controller, tell the caller.
1602 if ((saved_status & CMD_INVALID) != 0) {
1603 PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1605 * Some early adapters may not recover properly from
1606 * an invalid command. If it appears that the controller
1607 * has wedged (i.e. status was not cleared by our interrupt
1608 * reset above), perform a soft reset.
1611 status = aha_inb(aha, STATUS_REG);
1612 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1613 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1614 || (status & (HA_READY|INIT_REQUIRED))
1615 != (HA_READY|INIT_REQUIRED))
1616 ahareset(aha, /*hard_reset*/FALSE);
1620 if (param_len > 0) {
1621 /* The controller did not accept the full argument list */
1622 PRVERB((aha->dev, "Controller did not accept full argument "
1623 "list (%d > 0)\n", param_len));
1627 if (reply_len != reply_buf_size) {
1628 /* Too much or too little data received */
1629 PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1630 reply_len, reply_buf_size));
1634 /* We were successful */
1639 ahainitmboxes(struct aha_softc *aha)
1642 init_24b_mbox_params_t init_mbox;
1644 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1645 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1646 aha->cur_inbox = aha->in_boxes;
1647 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1648 aha->cur_outbox = aha->out_boxes;
1649 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1651 /* Tell the adapter about them */
1652 init_mbox.num_mboxes = aha->num_boxes;
1653 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1654 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1655 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1656 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1659 printf("ahainitmboxes: Initialization command failed\n");
1664 * Update the XPT's idea of the negotiated transfer
1665 * parameters for a particular target.
1668 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1670 setup_data_t setup_info;
1676 targ_syncinfo_t sync_info;
1678 target = cts->ccb_h.target_id;
1679 targ_offset = (target & 0x7);
1682 * Inquire Setup Information. This command retreives
1683 * the sync info for older models.
1685 param = sizeof(setup_info);
1686 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1687 (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1690 device_printf(aha->dev,
1691 "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1696 sync_info = setup_info.syncinfo[targ_offset];
1698 if (sync_info.sync == 0)
1699 cts->sync_offset = 0;
1701 cts->sync_offset = sync_info.offset;
1703 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1705 if (aha->boardid >= BOARD_1542CF)
1709 sync_period += 500 * sync_info.period;
1711 /* Convert ns value to standard SCSI sync rate */
1712 if (cts->sync_offset != 0)
1713 cts->sync_period = scsi_calc_syncparam(sync_period);
1715 cts->sync_period = 0;
1717 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1718 | CCB_TRANS_SYNC_OFFSET_VALID
1719 | CCB_TRANS_BUS_WIDTH_VALID;
1720 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1724 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1726 struct aha_softc* aha;
1728 aha = (struct aha_softc*)arg;
1729 aha->mailbox_physbase = segs->ds_addr;
1733 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1735 struct aha_softc* aha;
1737 aha = (struct aha_softc*)arg;
1738 aha->aha_ccb_physbase = segs->ds_addr;
1742 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1745 struct aha_softc* aha;
1747 aha = (struct aha_softc*)arg;
1748 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1752 ahapoll(struct cam_sim *sim)
1754 aha_intr(cam_sim_softc(sim));
1758 ahatimeout(void *arg)
1760 struct aha_ccb *accb;
1762 struct aha_softc *aha;
1765 struct ccb_hdr *ccb_h;
1767 accb = (struct aha_ccb *)arg;
1769 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1770 xpt_print_path(ccb->ccb_h.path);
1771 printf("CCB %p - timed out\n", (void *)accb);
1775 if ((accb->flags & ACCB_ACTIVE) == 0) {
1776 xpt_print_path(ccb->ccb_h.path);
1777 printf("CCB %p - timed out CCB already completed\n",
1784 * In order to simplify the recovery process, we ask the XPT
1785 * layer to halt the queue of new transactions and we traverse
1786 * the list of pending CCBs and remove their timeouts. This
1787 * means that the driver attempts to clear only one error
1788 * condition at a time. In general, timeouts that occur
1789 * close together are related anyway, so there is no benefit
1790 * in attempting to handle errors in parrallel. Timeouts will
1791 * be reinstated when the recovery process ends.
1793 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1794 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1795 xpt_freeze_simq(aha->sim, /*count*/1);
1796 accb->flags |= ACCB_RELEASE_SIMQ;
1799 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1800 while (ccb_h != NULL) {
1801 struct aha_ccb *pending_accb;
1803 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1804 untimeout(ahatimeout, pending_accb, ccb_h->timeout_ch);
1805 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1809 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1810 || aha->cur_outbox->action_code != AMBO_FREE) {
1812 * Try a full host adapter/SCSI bus reset.
1813 * We do this only if we have already attempted
1814 * to clear the condition with a BDR, or we cannot
1815 * attempt a BDR for lack of mailbox resources.
1817 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1818 ahareset(aha, /*hardreset*/TRUE);
1819 device_printf(aha->dev, "No longer in timeout\n");
1822 * Send a Bus Device Reset message:
1823 * The target that is holding up the bus may not
1824 * be the same as the one that triggered this timeout
1825 * (different commands have different timeout lengths),
1826 * but we have no way of determining this from our
1827 * timeout handler. Our strategy here is to queue a
1828 * BDR message to the target of the timed out command.
1829 * If this fails, we'll get another timeout 2 seconds
1830 * later which will attempt a bus reset.
1832 accb->flags |= ACCB_DEVICE_RESET;
1833 ccb->ccb_h.timeout_ch = timeout(ahatimeout, (caddr_t)accb, 2 * hz);
1834 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1836 /* No Data Transfer */
1837 aha->recovery_accb->hccb.datain = TRUE;
1838 aha->recovery_accb->hccb.dataout = TRUE;
1839 aha->recovery_accb->hccb.ahastat = 0;
1840 aha->recovery_accb->hccb.sdstat = 0;
1841 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1843 /* Tell the adapter about this command */
1844 paddr = ahaccbvtop(aha, aha->recovery_accb);
1845 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1846 aha->cur_outbox->action_code = AMBO_START;
1847 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1855 aha_detach(struct aha_softc *aha)
1857 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1858 xpt_free_path(aha->path);
1859 xpt_bus_deregister(cam_sim_path(aha->sim));
1860 cam_sim_free(aha->sim, /*free_devq*/TRUE);