2 * Generic register and struct definitions for the Adaptech 154x/164x
3 * SCSI host adapters. Product specific probe and attach routines can
5 * aha 1540/1542B/1542C/1542CF/1542CP aha_isa.c
7 * 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 * without modification, immediately at the beginning of the file.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
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 FOR
24 * 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
58 * $FreeBSD: src/sys/dev/aha/aha.c,v 1.34.2.1 2000/08/02 22:24:39 peter Exp $
59 * $DragonFly: src/sys/dev/disk/aha/aha.c,v 1.10 2005/06/20 02:56:09 dillon Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67 #include <sys/thread2.h>
69 #include <machine/bus_pio.h>
70 #include <machine/bus.h>
71 #include <machine/clock.h>
73 #include <bus/cam/cam.h>
74 #include <bus/cam/cam_ccb.h>
75 #include <bus/cam/cam_sim.h>
76 #include <bus/cam/cam_xpt_sim.h>
77 #include <bus/cam/cam_debug.h>
79 #include <bus/cam/scsi/scsi_message.h>
86 struct aha_softc *aha_softcs[NAHATOT];
88 #define PRVERB(x) if (bootverbose) printf x
90 /* Macro to determine that a rev is potentially a new valid one
91 * so that the driver doesn't keep breaking on new revs as it
92 * did for the CF and CP.
94 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
96 /* MailBox Management functions */
97 static __inline void ahanextinbox(struct aha_softc *aha);
98 static __inline void ahanextoutbox(struct aha_softc *aha);
101 ahanextinbox(struct aha_softc *aha)
103 if (aha->cur_inbox == aha->last_inbox)
104 aha->cur_inbox = aha->in_boxes;
110 ahanextoutbox(struct aha_softc *aha)
112 if (aha->cur_outbox == aha->last_outbox)
113 aha->cur_outbox = aha->out_boxes;
118 #define ahautoa24(u,s3) \
119 (s3)[0] = ((u) >> 16) & 0xff; \
120 (s3)[1] = ((u) >> 8) & 0xff; \
121 (s3)[2] = (u) & 0xff;
123 #define aha_a24tou(s3) \
124 (((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
126 /* CCB Mangement functions */
127 static __inline u_int32_t ahaccbvtop(struct aha_softc *aha,
128 struct aha_ccb *accb);
129 static __inline struct aha_ccb* ahaccbptov(struct aha_softc *aha,
132 static __inline u_int32_t
133 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
135 return (aha->aha_ccb_physbase
136 + (u_int32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
138 static __inline struct aha_ccb *
139 ahaccbptov(struct aha_softc *aha, u_int32_t ccb_addr)
141 return (aha->aha_ccb_array +
142 + ((struct aha_ccb*)ccb_addr-(struct aha_ccb*)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);
152 /* Host adapter command functions */
153 static int ahareset(struct aha_softc* aha, int hard_reset);
155 /* Initialization functions */
156 static int ahainitmboxes(struct aha_softc *aha);
157 static bus_dmamap_callback_t ahamapmboxes;
158 static bus_dmamap_callback_t ahamapccbs;
159 static bus_dmamap_callback_t ahamapsgs;
161 /* Transfer Negotiation Functions */
162 static void ahafetchtransinfo(struct aha_softc *aha,
163 struct ccb_trans_settings *cts);
165 /* CAM SIM entry points */
166 #define ccb_accb_ptr spriv_ptr0
167 #define ccb_aha_ptr spriv_ptr1
168 static void ahaaction(struct cam_sim *sim, union ccb *ccb);
169 static void ahapoll(struct cam_sim *sim);
171 /* Our timeout handler */
172 static timeout_t ahatimeout;
177 * Do our own re-probe protection until a configuration
178 * manager can do it for us. This ensures that we don't
179 * reprobe a card already found by the EISA or PCI probes.
181 static struct aha_isa_port aha_isa_ports[] =
192 * I/O ports listed in the order enumerated by the
193 * card for certain op codes.
195 static u_int16_t aha_board_ports[] =
205 /* Exported functions */
207 aha_alloc(int unit, bus_space_tag_t tag, bus_space_handle_t bsh)
209 struct aha_softc *aha;
211 if (unit != AHA_TEMP_UNIT) {
212 if (unit >= NAHATOT) {
213 printf("aha: unit number (%d) too high\n", unit);
218 * Allocate a storage area for us
220 if (aha_softcs[unit]) {
221 printf("aha%d: memory already allocated\n", unit);
226 aha = malloc(sizeof(struct aha_softc), M_DEVBUF, M_INTWAIT | M_ZERO);
227 SLIST_INIT(&aha->free_aha_ccbs);
228 LIST_INIT(&aha->pending_ccbs);
229 SLIST_INIT(&aha->sg_maps);
233 aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
234 aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
236 if (aha->unit != AHA_TEMP_UNIT) {
237 aha_softcs[unit] = aha;
243 aha_free(struct aha_softc *aha)
245 switch (aha->init_level) {
249 struct sg_map_node *sg_map;
251 while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
252 SLIST_REMOVE_HEAD(&aha->sg_maps, links);
253 bus_dmamap_unload(aha->sg_dmat,
255 bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
257 free(sg_map, M_DEVBUF);
259 bus_dma_tag_destroy(aha->sg_dmat);
263 bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
266 bus_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);
267 bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
271 bus_dma_tag_destroy(aha->ccb_dmat);
274 bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
277 bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
278 aha->mailbox_dmamap);
279 bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);
282 bus_dma_tag_destroy(aha->buffer_dmat);
285 bus_dma_tag_destroy(aha->mailbox_dmat);
290 if (aha->unit != AHA_TEMP_UNIT) {
291 aha_softcs[aha->unit] = NULL;
297 * Probe the adapter and verify that the card is an Adaptec.
300 aha_probe(struct aha_softc* aha)
305 board_id_data_t board_id;
308 * See if the three I/O ports look reasonable.
309 * Touch the minimal number of registers in the
312 status = aha_inb(aha, STATUS_REG);
314 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
315 STATUS_REG_RSVD)) != 0) {
316 PRVERB(("%s: status reg test failed %x\n", aha_name(aha),
321 intstat = aha_inb(aha, INTSTAT_REG);
322 if ((intstat & INTSTAT_REG_RSVD) != 0) {
323 PRVERB(("%s: Failed Intstat Reg Test\n", aha_name(aha)));
328 * Looking good so far. Final test is to reset the
329 * adapter and fetch the board ID and ensure we aren't
330 * looking at a BusLogic.
332 if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
333 PRVERB(("%s: Failed Reset\n", aha_name(aha)));
338 * Get the board ID. We use this to see if we're dealing with
339 * a buslogic card or a aha card (or clone).
341 error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
342 (u_int8_t*)&board_id, sizeof(board_id),
343 DEFAULT_CMD_TIMEOUT);
345 PRVERB(("%s: INQUIRE failed %x\n", aha_name(aha), error));
348 aha->fw_major = board_id.firmware_rev_major;
349 aha->fw_minor = board_id.firmware_rev_minor;
350 aha->boardid = board_id.board_type;
353 * The Buslogic cards have an id of either 0x41 or 0x42. So
354 * if those come up in the probe, we test the geometry register
355 * of the board. Adaptec boards that are this old will not have
356 * this register, and return 0xff, while buslogic cards will return
357 * something different.
359 * It appears that for reasons unknow, for the for the
360 * aha-1542B cards, we need to wait a little bit before trying
361 * to read the geometry register. I picked 10ms since we have
362 * reports that a for loop to 1000 did the trick, and this
363 * errs on the side of conservatism. Besides, no one will
364 * notice a 10mS delay here, even the 1542B card users :-)
366 * Some compatible cards return 0 here. Some cards also
367 * seem to return 0x7f.
369 * XXX I'm not sure how this will impact other cloned cards
371 * This really should be replaced with the esetup command, since
372 * that appears to be more reliable. This becomes more and more
373 * true over time as we discover more cards that don't read the
374 * geometry register consistantly.
376 if (aha->boardid <= 0x42) {
377 /* Wait 10ms before reading */
379 status = aha_inb(aha, GEOMETRY_REG);
380 if (status != 0xff && status != 0x00 && status != 0x7f) {
381 PRVERB(("%s: Geometry Register test failed 0x%x\n",
382 aha_name(aha), status));
391 * Pull the boards setup information and record it in our softc.
394 aha_fetch_adapter_info(struct aha_softc *aha)
396 setup_data_t setup_info;
397 config_data_t config_data;
398 u_int8_t length_param;
400 struct aha_extbios extbios;
402 switch (aha->boardid) {
403 case BOARD_1540_16HEAD_BIOS:
404 snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
406 case BOARD_1540_64HEAD_BIOS:
407 snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
410 snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
413 snprintf(aha->model, sizeof(aha->model), "1640");
416 snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");
419 snprintf(aha->model, sizeof(aha->model), "1542C");
422 snprintf(aha->model, sizeof(aha->model), "1542CF");
425 snprintf(aha->model, sizeof(aha->model), "1542CP");
428 snprintf(aha->model, sizeof(aha->model), "Unknown");
432 * If we are a new type of 1542 board (anything newer than a 1542C)
433 * then disable the extended bios so that the
434 * mailbox interface is unlocked.
435 * This is also true for the 1542B Version 3.20. First Adaptec
436 * board that supports >1Gb drives.
437 * No need to check the extended bios flags as some of the
438 * extensions that cause us problems are not flagged in that byte.
440 if (PROBABLY_NEW_BOARD(aha->boardid) ||
441 (aha->boardid == 0x41
442 && aha->fw_major == 0x31 &&
443 aha->fw_minor >= 0x34)) {
444 error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
445 /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
446 DEFAULT_CMD_TIMEOUT);
447 error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (u_int8_t *)&extbios,
448 /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
450 if (aha->boardid < 0x41)
451 printf("%s: Warning: aha-1542A won't likely work.\n",
454 aha->max_sg = 17; /* Need >= 17 to do 64k I/O */
456 aha->extended_lun = 0;
457 aha->extended_trans = 0;
459 /* Determine Sync/Wide/Disc settings */
460 length_param = sizeof(setup_info);
461 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
462 /*paramlen*/1, (u_int8_t*)&setup_info,
463 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
465 printf("%s: aha_fetch_adapter_info - Failed "
466 "Get Setup Info\n", aha_name(aha));
469 if (setup_info.initiate_sync != 0) {
470 aha->sync_permitted = ALL_TARGETS;
472 aha->disc_permitted = ALL_TARGETS;
474 /* We need as many mailboxes as we can have ccbs */
475 aha->num_boxes = aha->max_ccbs;
477 /* Determine our SCSI ID */
479 error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
480 (u_int8_t*)&config_data, sizeof(config_data),
481 DEFAULT_CMD_TIMEOUT);
483 printf("%s: aha_fetch_adapter_info - Failed Get Config\n",
487 aha->scsi_id = config_data.scsi_id;
492 * Start the board, ready for normal operation
495 aha_init(struct aha_softc* aha)
497 /* Announce the Adapter */
498 printf("%s: AHA-%s FW Rev. %c.%c (ID=%x) ", aha_name(aha),
499 aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
501 if (aha->diff_bus != 0)
504 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
508 * Create our DMA tags. These tags define the kinds of device
509 * accessible memory allocations and memory mappings we will
510 * need to perform during normal operation.
512 * Unless we need to further restrict the allocation, we rely
513 * on the restrictions of the parent dmat, hence the common
514 * use of MAXADDR and MAXSIZE.
517 /* DMA tag for mapping buffers into device visible space. */
518 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
519 /*lowaddr*/BUS_SPACE_MAXADDR,
520 /*highaddr*/BUS_SPACE_MAXADDR,
521 /*filter*/NULL, /*filterarg*/NULL,
522 /*maxsize*/MAXBSIZE, /*nsegments*/AHA_NSEG,
523 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
524 /*flags*/BUS_DMA_ALLOCNOW,
525 &aha->buffer_dmat) != 0) {
530 /* DMA tag for our mailboxes */
531 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
532 /*lowaddr*/BUS_SPACE_MAXADDR,
533 /*highaddr*/BUS_SPACE_MAXADDR,
534 /*filter*/NULL, /*filterarg*/NULL,
535 aha->num_boxes * (sizeof(aha_mbox_in_t)
536 + sizeof(aha_mbox_out_t)),
538 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
539 /*flags*/0, &aha->mailbox_dmat) != 0) {
545 /* Allocation for our mailboxes */
546 if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
547 BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0) {
553 /* And permanently map them */
554 bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
556 aha->num_boxes * (sizeof(aha_mbox_in_t)
557 + sizeof(aha_mbox_out_t)),
558 ahamapmboxes, aha, /*flags*/0);
562 aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
566 /* DMA tag for our ccb structures */
567 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
568 /*lowaddr*/BUS_SPACE_MAXADDR,
569 /*highaddr*/BUS_SPACE_MAXADDR,
570 /*filter*/NULL, /*filterarg*/NULL,
571 aha->max_ccbs * sizeof(struct aha_ccb),
573 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
574 /*flags*/0, &aha->ccb_dmat) != 0) {
580 /* Allocation for our ccbs */
581 if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
582 BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0) {
588 /* And permanently map them */
589 bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap,
591 aha->max_ccbs * sizeof(struct aha_ccb),
592 ahamapccbs, aha, /*flags*/0);
596 /* DMA tag for our S/G structures. We allocate in page sized chunks */
597 if (bus_dma_tag_create(aha->parent_dmat, /*alignment*/1, /*boundary*/0,
598 /*lowaddr*/BUS_SPACE_MAXADDR,
599 /*highaddr*/BUS_SPACE_MAXADDR,
600 /*filter*/NULL, /*filterarg*/NULL,
601 PAGE_SIZE, /*nsegments*/1,
602 /*maxsegsz*/BUS_SPACE_MAXSIZE_24BIT,
603 /*flags*/0, &aha->sg_dmat) != 0) {
609 /* Perform initial CCB allocation */
610 bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
613 if (aha->num_ccbs == 0) {
614 printf("%s: aha_init - Unable to allocate initial ccbs\n",
620 * Note that we are going and return (to probe)
630 aha_attach(struct aha_softc *aha)
632 int tagged_dev_openings;
633 struct cam_devq *devq;
636 * We don't do tagged queueing, since the aha cards don't
639 tagged_dev_openings = 0;
642 * Create the device queue for our SIM.
644 devq = cam_simq_alloc(aha->max_ccbs - 1);
649 * Construct our SIM entry
651 aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha, aha->unit,
652 2, tagged_dev_openings, devq);
653 cam_simq_release(devq);
654 if (aha->sim == NULL) {
658 if (xpt_bus_register(aha->sim, 0) != CAM_SUCCESS) {
659 cam_sim_free(aha->sim);
663 if (xpt_create_path(&aha->path, /*periph*/NULL,
664 cam_sim_path(aha->sim), CAM_TARGET_WILDCARD,
665 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
666 xpt_bus_deregister(cam_sim_path(aha->sim));
667 cam_sim_free(aha->sim);
675 aha_name(struct aha_softc *aha)
677 static char name[10];
679 snprintf(name, sizeof(name), "aha%d", aha->unit);
684 aha_find_probe_range(int ioport, int *port_index, int *max_port_index)
689 for (i = 0;i < AHA_NUM_ISAPORTS; i++)
690 if (ioport <= aha_isa_ports[i].addr)
692 if ((i >= AHA_NUM_ISAPORTS)
693 || (ioport != aha_isa_ports[i].addr)) {
695 "aha_isa_probe: Invalid baseport of 0x%x specified.\n"
696 "aha_isa_probe: Nearest valid baseport is 0x%x.\n"
697 "aha_isa_probe: Failing probe.\n",
699 (i < AHA_NUM_ISAPORTS)
700 ? aha_isa_ports[i].addr
701 : aha_isa_ports[AHA_NUM_ISAPORTS - 1].addr);
702 *port_index = *max_port_index = -1;
705 *port_index = *max_port_index = aha_isa_ports[i].bio;
708 *max_port_index = AHA_NUM_ISAPORTS - 1;
713 aha_iop_from_bio(isa_compat_io_t bio_index)
715 if (bio_index >= 0 && bio_index < AHA_NUM_ISAPORTS)
716 return (aha_board_ports[bio_index]);
721 ahaallocccbs(struct aha_softc *aha)
723 struct aha_ccb *next_ccb;
724 struct sg_map_node *sg_map;
730 next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
732 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_INTWAIT);
734 /* Allocate S/G space for the next batch of CCBS */
735 if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
736 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
737 free(sg_map, M_DEVBUF);
741 SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
743 bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
744 PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
746 segs = sg_map->sg_vaddr;
747 physaddr = sg_map->sg_physaddr;
749 newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
750 for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
753 next_ccb->sg_list = segs;
754 next_ccb->sg_list_phys = physaddr;
755 next_ccb->flags = ACCB_FREE;
756 error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
760 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
762 physaddr += (AHA_NSEG * sizeof(aha_sg_t));
767 /* Reserve a CCB for error recovery */
768 if (aha->recovery_accb == NULL) {
769 aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
770 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
775 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
778 if ((accb->flags & ACCB_ACTIVE) != 0)
779 LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
780 if (aha->resource_shortage != 0
781 && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
782 accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
783 aha->resource_shortage = FALSE;
785 accb->flags = ACCB_FREE;
786 SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
791 static struct aha_ccb*
792 ahagetccb(struct aha_softc *aha)
794 struct aha_ccb* accb;
797 if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
798 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
800 } else if (aha->num_ccbs < aha->max_ccbs) {
802 accb = SLIST_FIRST(&aha->free_aha_ccbs);
804 printf("%s: Can't malloc ACCB\n", aha_name(aha));
806 SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
816 ahaaction(struct cam_sim *sim, union ccb *ccb)
818 struct aha_softc *aha;
820 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
822 aha = (struct aha_softc *)cam_sim_softc(sim);
824 switch (ccb->ccb_h.func_code) {
825 /* Common cases first */
826 case XPT_SCSI_IO: /* Execute the requested I/O operation */
827 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
829 struct aha_ccb *accb;
830 struct aha_hccb *hccb;
835 if ((accb = ahagetccb(aha)) == NULL) {
837 aha->resource_shortage = TRUE;
839 xpt_freeze_simq(aha->sim, /*count*/1);
840 ccb->ccb_h.status = CAM_REQUEUE_REQ;
848 * So we can find the ACCB when an abort is requested
851 ccb->ccb_h.ccb_accb_ptr = accb;
852 ccb->ccb_h.ccb_aha_ptr = aha;
855 * Put all the arguments for the xfer in the accb
857 hccb->target = ccb->ccb_h.target_id;
858 hccb->lun = ccb->ccb_h.target_lun;
862 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
863 struct ccb_scsiio *csio;
864 struct ccb_hdr *ccbh;
868 hccb->opcode = aha->ccb_ccb_opcode;
869 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
870 hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
871 hccb->cmd_len = csio->cdb_len;
872 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
873 ccb->ccb_h.status = CAM_REQ_INVALID;
874 ahafreeccb(aha, accb);
878 hccb->sense_len = csio->sense_len;
879 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
880 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
881 bcopy(csio->cdb_io.cdb_ptr,
882 hccb->scsi_cdb, hccb->cmd_len);
884 /* I guess I could map it in... */
885 ccbh->status = CAM_REQ_INVALID;
886 ahafreeccb(aha, accb);
891 bcopy(csio->cdb_io.cdb_bytes,
892 hccb->scsi_cdb, hccb->cmd_len);
895 * If we have any data to send with this command,
896 * map it into bus space.
898 /* Only use S/G if there is a transfer */
899 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
900 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
902 * We've been given a pointer
903 * to a single buffer.
905 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
909 error = bus_dmamap_load(
917 if (error == EINPROGRESS) {
920 * ordering, freeze the
922 * until our mapping is
925 xpt_freeze_simq(aha->sim,
927 csio->ccb_h.status |=
932 struct bus_dma_segment seg;
934 /* Pointer to physical buffer */
936 (bus_addr_t)csio->data_ptr;
937 seg.ds_len = csio->dxfer_len;
938 ahaexecuteccb(accb, &seg, 1, 0);
941 struct bus_dma_segment *segs;
943 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
944 panic("ahaaction - Physical "
948 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
949 panic("ahaaction - Virtual "
953 /* Just use the segments provided */
954 segs = (struct bus_dma_segment *)
956 ahaexecuteccb(accb, segs,
957 csio->sglist_cnt, 0);
960 ahaexecuteccb(accb, NULL, 0, 0);
963 hccb->opcode = INITIATOR_BUS_DEV_RESET;
964 /* No data transfer */
966 hccb->dataout = TRUE;
969 ahaexecuteccb(accb, NULL, 0, 0);
973 case XPT_EN_LUN: /* Enable LUN as a target */
974 case XPT_TARGET_IO: /* Execute target I/O request */
975 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
976 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
977 case XPT_ABORT: /* Abort the specified CCB */
979 ccb->ccb_h.status = CAM_REQ_INVALID;
982 case XPT_SET_TRAN_SETTINGS:
985 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
989 case XPT_GET_TRAN_SETTINGS:
990 /* Get default/user set transfer settings for the target */
992 struct ccb_trans_settings *cts;
996 target_mask = 0x01 << ccb->ccb_h.target_id;
997 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
999 if ((aha->disc_permitted & target_mask) != 0)
1000 cts->flags |= CCB_TRANS_DISC_ENB;
1001 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1002 if ((aha->sync_permitted & target_mask) != 0) {
1003 if (aha->boardid >= BOARD_1542CF)
1004 cts->sync_period = 25;
1006 cts->sync_period = 50;
1008 cts->sync_period = 0;
1010 if (cts->sync_period != 0)
1011 cts->sync_offset = 15;
1013 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1014 | CCB_TRANS_SYNC_OFFSET_VALID
1015 | CCB_TRANS_BUS_WIDTH_VALID
1016 | CCB_TRANS_DISC_VALID
1017 | CCB_TRANS_TQ_VALID;
1019 ahafetchtransinfo(aha, cts);
1022 ccb->ccb_h.status = CAM_REQ_CMP;
1026 case XPT_CALC_GEOMETRY:
1028 struct ccb_calc_geometry *ccg;
1030 u_int32_t secs_per_cylinder;
1033 size_mb = ccg->volume_size
1034 / ((1024L * 1024L) / ccg->block_size);
1036 if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1037 if (size_mb >= 2048) {
1039 ccg->secs_per_track = 63;
1042 ccg->secs_per_track = 32;
1046 ccg->secs_per_track = 32;
1048 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1049 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1050 ccb->ccb_h.status = CAM_REQ_CMP;
1054 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1056 ahareset(aha, /*hardreset*/TRUE);
1057 ccb->ccb_h.status = CAM_REQ_CMP;
1061 case XPT_TERM_IO: /* Terminate the I/O process */
1063 ccb->ccb_h.status = CAM_REQ_INVALID;
1066 case XPT_PATH_INQ: /* Path routing inquiry */
1068 struct ccb_pathinq *cpi = &ccb->cpi;
1070 cpi->version_num = 1; /* XXX??? */
1071 cpi->hba_inquiry = PI_SDTR_ABLE;
1072 cpi->target_sprt = 0;
1074 cpi->hba_eng_cnt = 0;
1075 cpi->max_target = 7;
1077 cpi->initiator_id = aha->scsi_id;
1078 cpi->bus_id = cam_sim_bus(sim);
1079 cpi->base_transfer_speed = 3300;
1080 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1081 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
1082 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1083 cpi->unit_number = cam_sim_unit(sim);
1084 cpi->ccb_h.status = CAM_REQ_CMP;
1089 ccb->ccb_h.status = CAM_REQ_INVALID;
1096 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1098 struct aha_ccb *accb;
1100 struct aha_softc *aha;
1103 accb = (struct aha_ccb *)arg;
1105 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1109 printf("%s: Unexepected error 0x%x returned from "
1110 "bus_dmamap_load\n", aha_name(aha), error);
1111 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1112 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1113 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1115 ahafreeccb(aha, accb);
1122 bus_dma_segment_t *end_seg;
1123 bus_dmasync_op_t op;
1125 end_seg = dm_segs + nseg;
1127 /* Copy the segments into our SG list */
1129 while (dm_segs < end_seg) {
1130 ahautoa24(dm_segs->ds_len, sg->len);
1131 ahautoa24(dm_segs->ds_addr, sg->addr);
1137 accb->hccb.opcode = aha->ccb_sg_opcode;
1138 ahautoa24((sizeof(aha_sg_t) * nseg),
1139 accb->hccb.data_len);
1140 ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1142 bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1143 bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1146 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1147 op = BUS_DMASYNC_PREREAD;
1149 op = BUS_DMASYNC_PREWRITE;
1151 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1154 accb->hccb.opcode = INITIATOR_CCB;
1155 ahautoa24(0, accb->hccb.data_len);
1156 ahautoa24(0, accb->hccb.data_addr);
1162 * Last time we need to check if this CCB needs to
1165 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1167 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1168 ahafreeccb(aha, accb);
1174 accb->flags = ACCB_ACTIVE;
1175 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1176 LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1178 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
1181 /* Tell the adapter about this command */
1182 if (aha->cur_outbox->action_code != AMBO_FREE) {
1184 * We should never encounter a busy mailbox.
1185 * If we do, warn the user, and treat it as
1186 * a resource shortage. If the controller is
1187 * hung, one of the pending transactions will
1188 * timeout causing us to start recovery operations.
1190 printf("%s: Encountered busy mailbox with %d out of %d "
1191 "commands active!!!", aha_name(aha), aha->active_ccbs,
1193 callout_stop(&ccb->ccb_h.timeout_ch);
1195 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1196 ahafreeccb(aha, accb);
1197 aha->resource_shortage = TRUE;
1198 xpt_freeze_simq(aha->sim, /*count*/1);
1199 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1204 paddr = ahaccbvtop(aha, accb);
1205 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1206 aha->cur_outbox->action_code = AMBO_START;
1207 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1216 struct aha_softc *aha;
1219 aha = (struct aha_softc *)arg;
1220 while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1221 if ((intstat & CMD_COMPLETE) != 0) {
1222 aha->latched_status = aha_inb(aha, STATUS_REG);
1223 aha->command_cmp = TRUE;
1226 aha_outb(aha, CONTROL_REG, RESET_INTR);
1228 if ((intstat & IMB_LOADED) != 0) {
1229 while (aha->cur_inbox->comp_code != AMBI_FREE) {
1231 paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1233 ahaccbptov(aha, paddr),
1234 aha->cur_inbox->comp_code);
1235 aha->cur_inbox->comp_code = AMBI_FREE;
1240 if ((intstat & SCSI_BUS_RESET) != 0) {
1241 ahareset(aha, /*hardreset*/FALSE);
1247 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1250 struct ccb_scsiio *csio;
1253 csio = &accb->ccb->csio;
1255 if ((accb->flags & ACCB_ACTIVE) == 0) {
1256 printf("%s: ahadone - Attempt to free non-active ACCB %p\n",
1257 aha_name(aha), (void *)accb);
1261 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1262 bus_dmasync_op_t op;
1264 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1265 op = BUS_DMASYNC_POSTREAD;
1267 op = BUS_DMASYNC_POSTWRITE;
1268 bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1269 bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1272 if (accb == aha->recovery_accb) {
1274 * The recovery ACCB does not have a CCB associated
1275 * with it, so short circuit the normal error handling.
1276 * We now traverse our list of pending CCBs and process
1277 * any that were terminated by the recovery CCBs action.
1278 * We also reinstate timeouts for all remaining, pending,
1281 struct cam_path *path;
1282 struct ccb_hdr *ccb_h;
1285 /* Notify all clients that a BDR occured */
1286 error = xpt_create_path(&path, /*periph*/NULL,
1287 cam_sim_path(aha->sim),
1291 if (error == CAM_REQ_CMP)
1292 xpt_async(AC_SENT_BDR, path, NULL);
1294 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1295 while (ccb_h != NULL) {
1296 struct aha_ccb *pending_accb;
1298 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1299 if (pending_accb->hccb.target == accb->hccb.target) {
1300 pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1301 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1302 ahadone(aha, pending_accb, AMBI_ERROR);
1304 callout_reset(&ccb_h->timeout_ch,
1305 (ccb_h->timeout * hz) / 1000,
1306 ahatimeout, pending_accb);
1307 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1310 printf("%s: No longer in timeout\n", aha_name(aha));
1314 callout_stop(&ccb->ccb_h.timeout_ch);
1316 switch (comp_code) {
1318 printf("%s: ahadone - CCB completed with free status!\n",
1321 case AMBI_NOT_FOUND:
1322 printf("%s: ahadone - CCB Abort failed to find CCB\n",
1327 /* An error occured */
1328 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1331 csio->resid = aha_a24tou(accb->hccb.data_len);
1332 switch(accb->hccb.ahastat) {
1333 case AHASTAT_DATARUN_ERROR:
1335 if (csio->resid <= 0) {
1336 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1341 case AHASTAT_NOERROR:
1342 csio->scsi_status = accb->hccb.sdstat;
1343 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1344 switch(csio->scsi_status) {
1345 case SCSI_STATUS_CHECK_COND:
1346 case SCSI_STATUS_CMD_TERMINATED:
1347 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1349 * The aha writes the sense data at different
1350 * offsets based on the scsi cmd len
1352 bcopy((caddr_t) &accb->hccb.scsi_cdb +
1354 (caddr_t) &csio->sense_data,
1355 accb->hccb.sense_len);
1359 case SCSI_STATUS_OK:
1360 csio->ccb_h.status = CAM_REQ_CMP;
1364 case AHASTAT_SELTIMEOUT:
1365 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1367 case AHASTAT_UNEXPECTED_BUSFREE:
1368 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1370 case AHASTAT_INVALID_PHASE:
1371 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1373 case AHASTAT_INVALID_ACTION_CODE:
1374 panic("%s: Inavlid Action code", aha_name(aha));
1376 case AHASTAT_INVALID_OPCODE:
1377 if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1378 panic("%s: Invalid CCB Opcode %x hccb = %p",
1379 aha_name(aha), accb->hccb.opcode,
1381 printf("%s: AHA-1540A detected, compensating\n",
1383 aha->ccb_sg_opcode = INITIATOR_SG_CCB;
1384 aha->ccb_ccb_opcode = INITIATOR_CCB;
1385 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1386 csio->ccb_h.status = CAM_REQUEUE_REQ;
1388 case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1389 /* We don't even support linked commands... */
1390 panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1392 case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1393 panic("%s: Invalid CCB or SG list", aha_name(aha));
1395 case AHASTAT_HA_SCSI_BUS_RESET:
1396 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1398 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1400 case AHASTAT_HA_BDR:
1401 if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1402 csio->ccb_h.status = CAM_BDR_SENT;
1404 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1407 if (csio->ccb_h.status != CAM_REQ_CMP) {
1408 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1409 csio->ccb_h.status |= CAM_DEV_QFRZN;
1411 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1412 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1413 ahafreeccb(aha, accb);
1417 /* All completed without incident */
1418 /* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1419 /* I don't think so since it works???? */
1420 ccb->ccb_h.status |= CAM_REQ_CMP;
1421 if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1422 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1423 ahafreeccb(aha, accb);
1430 ahareset(struct aha_softc* aha, int hard_reset)
1432 struct ccb_hdr *ccb_h;
1435 u_int8_t reset_type;
1437 if (hard_reset != 0)
1438 reset_type = HARD_RESET;
1440 reset_type = SOFT_RESET;
1441 aha_outb(aha, CONTROL_REG, reset_type);
1443 /* Wait 5sec. for Diagnostic start */
1444 timeout = 5 * 10000;
1446 status = aha_inb(aha, STATUS_REG);
1447 if ((status & DIAG_ACTIVE) != 0)
1452 PRVERB(("%s: ahareset - Diagnostic Active failed to "
1453 "assert. status = 0x%x\n", aha_name(aha),
1458 /* Wait 10sec. for Diagnostic end */
1459 timeout = 10 * 10000;
1461 status = aha_inb(aha, STATUS_REG);
1462 if ((status & DIAG_ACTIVE) == 0)
1467 panic("%s: ahareset - Diagnostic Active failed to drop. "
1468 "status = 0x%x\n", aha_name(aha), status);
1472 /* Wait for the host adapter to become ready or report a failure */
1475 status = aha_inb(aha, STATUS_REG);
1476 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1481 printf("%s: ahareset - Host adapter failed to come ready. "
1482 "status = 0x%x\n", aha_name(aha), status);
1486 /* If the diagnostics failed, tell the user */
1487 if ((status & DIAG_FAIL) != 0
1488 || (status & HA_READY) == 0) {
1489 printf("%s: ahareset - Adapter failed diagnostics\n",
1492 if ((status & DATAIN_REG_READY) != 0)
1493 printf("%s: ahareset - Host Adapter Error "
1494 "code = 0x%x\n", aha_name(aha),
1495 aha_inb(aha, DATAIN_REG));
1499 /* If we've allocated mailboxes, initialize them */
1500 if (aha->init_level > 4)
1503 /* If we've attached to the XPT, tell it about the event */
1504 if (aha->path != NULL)
1505 xpt_async(AC_BUS_RESET, aha->path, NULL);
1508 * Perform completion processing for all outstanding CCBs.
1510 while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1511 struct aha_ccb *pending_accb;
1513 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1514 pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1515 ahadone(aha, pending_accb, AMBI_ERROR);
1522 * Send a command to the adapter.
1525 aha_cmd(struct aha_softc *aha, aha_op_t opcode, u_int8_t *params,
1526 u_int param_len, u_int8_t *reply_data, u_int reply_len,
1533 u_int reply_buf_size;
1537 /* No data returned to start */
1538 reply_buf_size = reply_len;
1546 * All commands except for the "start mailbox" and the "enable
1547 * outgoing mailbox read interrupt" commands cannot be issued
1548 * while there are pending transactions. Freeze our SIMQ
1549 * and wait for all completions to occur if necessary.
1553 while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1554 /* Fire the interrupt handler in case interrupts are blocked */
1563 printf("%s: aha_cmd: Timeout waiting for adapter idle\n",
1567 aha->command_cmp = 0;
1569 * Wait up to 10 sec. for the adapter to become
1570 * ready to accept commands.
1575 status = aha_inb(aha, STATUS_REG);
1576 if ((status & HA_READY) != 0
1577 && (status & CMD_REG_BUSY) == 0)
1580 * Throw away any pending data which may be
1581 * left over from earlier commands that we
1584 if ((status & DATAIN_REG_READY) != 0)
1585 (void)aha_inb(aha, DATAIN_REG);
1589 printf("%s: aha_cmd: Timeout waiting for adapter ready, "
1590 "status = 0x%x\n", aha_name(aha), status);
1595 * Send the opcode followed by any necessary parameter bytes.
1597 aha_outb(aha, COMMAND_REG, opcode);
1600 * Wait for up to 1sec to get the parameter list sent
1603 while (param_len && --timeout) {
1606 status = aha_inb(aha, STATUS_REG);
1607 intstat = aha_inb(aha, INTSTAT_REG);
1610 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1611 == (INTR_PENDING|CMD_COMPLETE)) {
1612 saved_status = status;
1617 if (aha->command_cmp != 0) {
1618 saved_status = aha->latched_status;
1622 if ((status & DATAIN_REG_READY) != 0)
1624 if ((status & CMD_REG_BUSY) == 0) {
1625 aha_outb(aha, COMMAND_REG, *params++);
1631 printf("%s: aha_cmd: Timeout sending parameters, "
1632 "status = 0x%x\n", aha_name(aha), status);
1637 * For all other commands, we wait for any output data
1638 * and the final comand completion interrupt.
1640 while (cmd_complete == 0 && --cmd_timeout) {
1643 status = aha_inb(aha, STATUS_REG);
1644 intstat = aha_inb(aha, INTSTAT_REG);
1647 if (aha->command_cmp != 0) {
1649 saved_status = aha->latched_status;
1650 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1651 == (INTR_PENDING|CMD_COMPLETE)) {
1653 * Our poll (in case interrupts are blocked)
1654 * saw the CMD_COMPLETE interrupt.
1657 saved_status = status;
1659 if ((status & DATAIN_REG_READY) != 0) {
1662 data = aha_inb(aha, DATAIN_REG);
1663 if (reply_len < reply_buf_size) {
1664 *reply_data++ = data;
1666 printf("%s: aha_cmd - Discarded reply data "
1667 "byte for opcode 0x%x\n", aha_name(aha),
1671 * Reset timeout to ensure at least a second
1672 * between response bytes.
1674 cmd_timeout = MAX(cmd_timeout, 10000);
1679 if (cmd_timeout == 0) {
1680 printf("%s: aha_cmd: Timeout waiting for reply data and "
1681 "command complete.\n%s: status = 0x%x, intstat = 0x%x, "
1682 "reply_len = %d\n", aha_name(aha), aha_name(aha), status,
1683 intstat, reply_len);
1688 * Clear any pending interrupts. Block interrupts so our
1689 * interrupt handler is not re-entered.
1699 * If the command was rejected by the controller, tell the caller.
1701 if ((saved_status & CMD_INVALID) != 0) {
1702 PRVERB(("%s: Invalid Command 0x%x\n", aha_name(aha), opcode));
1704 * Some early adapters may not recover properly from
1705 * an invalid command. If it appears that the controller
1706 * has wedged (i.e. status was not cleared by our interrupt
1707 * reset above), perform a soft reset.
1710 status = aha_inb(aha, STATUS_REG);
1711 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1712 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1713 || (status & (HA_READY|INIT_REQUIRED))
1714 != (HA_READY|INIT_REQUIRED)) {
1715 ahareset(aha, /*hard_reset*/FALSE);
1720 if (param_len > 0) {
1721 /* The controller did not accept the full argument list */
1722 PRVERB(("%s: Controller did not accept full argument list "
1724 aha_name(aha), param_len));
1728 if (reply_len != reply_buf_size) {
1729 /* Too much or too little data received */
1730 PRVERB(("%s: Too much or too little data received (%d != %d)\n",
1731 aha_name(aha), reply_len, reply_buf_size));
1735 /* We were successful */
1740 ahainitmboxes(struct aha_softc *aha)
1743 init_24b_mbox_params_t init_mbox;
1745 bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1746 bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1747 aha->cur_inbox = aha->in_boxes;
1748 aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1749 aha->cur_outbox = aha->out_boxes;
1750 aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1752 /* Tell the adapter about them */
1753 init_mbox.num_mboxes = aha->num_boxes;
1754 ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1755 error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (u_int8_t *)&init_mbox,
1756 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1757 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1760 printf("ahainitmboxes: Initialization command failed\n");
1765 * Update the XPT's idea of the negotiated transfer
1766 * parameters for a particular target.
1769 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1771 setup_data_t setup_info;
1777 targ_syncinfo_t sync_info;
1779 target = cts->ccb_h.target_id;
1780 targ_offset = (target & 0x7);
1783 * Inquire Setup Information. This command retreives
1784 * the sync info for older models.
1786 param = sizeof(setup_info);
1787 error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, ¶m, /*paramlen*/1,
1788 (u_int8_t*)&setup_info, sizeof(setup_info),
1789 DEFAULT_CMD_TIMEOUT);
1792 printf("%s: ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1793 aha_name(aha), error);
1797 sync_info = setup_info.syncinfo[targ_offset];
1799 if (sync_info.sync == 0)
1800 cts->sync_offset = 0;
1802 cts->sync_offset = sync_info.offset;
1804 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1806 if (aha->boardid >= BOARD_1542CF)
1810 sync_period += 500 * sync_info.period;
1812 /* Convert ns value to standard SCSI sync rate */
1813 if (cts->sync_offset != 0)
1814 cts->sync_period = scsi_calc_syncparam(sync_period);
1816 cts->sync_period = 0;
1818 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1819 | CCB_TRANS_SYNC_OFFSET_VALID
1820 | CCB_TRANS_BUS_WIDTH_VALID;
1821 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1825 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1827 struct aha_softc* aha;
1829 aha = (struct aha_softc*)arg;
1830 aha->mailbox_physbase = segs->ds_addr;
1834 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1836 struct aha_softc* aha;
1838 aha = (struct aha_softc*)arg;
1839 aha->aha_ccb_physbase = segs->ds_addr;
1843 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1846 struct aha_softc* aha;
1848 aha = (struct aha_softc*)arg;
1849 SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1853 ahapoll(struct cam_sim *sim)
1855 aha_intr(cam_sim_softc(sim));
1859 ahatimeout(void *arg)
1861 struct aha_ccb *accb;
1863 struct aha_softc *aha;
1866 accb = (struct aha_ccb *)arg;
1868 aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1869 xpt_print_path(ccb->ccb_h.path);
1870 printf("CCB %p - timed out\n", (void *)accb);
1874 if ((accb->flags & ACCB_ACTIVE) == 0) {
1875 xpt_print_path(ccb->ccb_h.path);
1876 printf("CCB %p - timed out CCB already completed\n",
1883 * In order to simplify the recovery process, we ask the XPT
1884 * layer to halt the queue of new transactions and we traverse
1885 * the list of pending CCBs and remove their timeouts. This
1886 * means that the driver attempts to clear only one error
1887 * condition at a time. In general, timeouts that occur
1888 * close together are related anyway, so there is no benefit
1889 * in attempting to handle errors in parrallel. Timeouts will
1890 * be reinstated when the recovery process ends.
1892 if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1893 struct ccb_hdr *ccb_h;
1895 if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1896 xpt_freeze_simq(aha->sim, /*count*/1);
1897 accb->flags |= ACCB_RELEASE_SIMQ;
1900 ccb_h = LIST_FIRST(&aha->pending_ccbs);
1901 while (ccb_h != NULL) {
1902 struct aha_ccb *pending_accb;
1904 pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1905 callout_stop(&ccb_h->timeout_ch);
1906 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1910 if ((accb->flags & ACCB_DEVICE_RESET) != 0
1911 || aha->cur_outbox->action_code != AMBO_FREE) {
1913 * Try a full host adapter/SCSI bus reset.
1914 * We do this only if we have already attempted
1915 * to clear the condition with a BDR, or we cannot
1916 * attempt a BDR for lack of mailbox resources.
1918 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1919 ahareset(aha, /*hardreset*/TRUE);
1920 printf("%s: No longer in timeout\n", aha_name(aha));
1923 * Send a Bus Device Reset message:
1924 * The target that is holding up the bus may not
1925 * be the same as the one that triggered this timeout
1926 * (different commands have different timeout lengths),
1927 * but we have no way of determining this from our
1928 * timeout handler. Our strategy here is to queue a
1929 * BDR message to the target of the timed out command.
1930 * If this fails, we'll get another timeout 2 seconds
1931 * later which will attempt a bus reset.
1933 accb->flags |= ACCB_DEVICE_RESET;
1934 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, ahatimeout, accb);
1935 aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1937 /* No Data Transfer */
1938 aha->recovery_accb->hccb.datain = TRUE;
1939 aha->recovery_accb->hccb.dataout = TRUE;
1940 aha->recovery_accb->hccb.ahastat = 0;
1941 aha->recovery_accb->hccb.sdstat = 0;
1942 aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1944 /* Tell the adapter about this command */
1945 paddr = ahaccbvtop(aha, aha->recovery_accb);
1946 ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1947 aha->cur_outbox->action_code = AMBO_START;
1948 aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1956 aha_detach(struct aha_softc *aha)
1958 xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1959 xpt_free_path(aha->path);
1960 xpt_bus_deregister(cam_sim_path(aha->sim));
1961 cam_sim_free(aha->sim);