2 * CAM SCSI device driver for the Adaptec 174X SCSI Host adapter
4 * Copyright (c) 1998 Justin T. Gibbs
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/dev/ahb/ahb.c,v 1.18.2.3 2001/03/05 13:08:55 obrien Exp $
29 * $DragonFly: src/sys/dev/disk/ahb/ahb.c,v 1.8 2004/09/17 03:39:38 joerg Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
39 #include <machine/bus_pio.h>
40 #include <machine/bus.h>
41 #include <machine/clock.h>
42 #include <machine/resource.h>
45 #include <bus/cam/cam.h>
46 #include <bus/cam/cam_ccb.h>
47 #include <bus/cam/cam_sim.h>
48 #include <bus/cam/cam_xpt_sim.h>
49 #include <bus/cam/cam_debug.h>
51 #include <bus/cam/scsi/scsi_message.h>
53 #include <bus/eisa/eisaconf.h>
57 #define ccb_ecb_ptr spriv_ptr0
58 #define ccb_ahb_ptr spriv_ptr1
60 #define ahb_inb(ahb, port) \
61 bus_space_read_1((ahb)->tag, (ahb)->bsh, port)
63 #define ahb_inl(ahb, port) \
64 bus_space_read_4((ahb)->tag, (ahb)->bsh, port)
66 #define ahb_outb(ahb, port, value) \
67 bus_space_write_1((ahb)->tag, (ahb)->bsh, port, value)
69 #define ahb_outl(ahb, port, value) \
70 bus_space_write_4((ahb)->tag, (ahb)->bsh, port, value)
72 static const char *ahbmatch(eisa_id_t type);
73 static struct ahb_softc *ahballoc(u_long unit, struct resource *res);
74 static void ahbfree(struct ahb_softc *ahb);
75 static int ahbreset(struct ahb_softc *ahb);
76 static void ahbmapecbs(void *arg, bus_dma_segment_t *segs,
78 static int ahbxptattach(struct ahb_softc *ahb);
79 static void ahbhandleimmed(struct ahb_softc *ahb,
80 u_int32_t mbox, u_int intstat);
81 static void ahbcalcresid(struct ahb_softc *ahb,
82 struct ecb *ecb, union ccb *ccb);
83 static __inline void ahbdone(struct ahb_softc *ahb, u_int32_t mbox,
85 static void ahbintr(void *arg);
86 static bus_dmamap_callback_t ahbexecuteecb;
87 static void ahbaction(struct cam_sim *sim, union ccb *ccb);
88 static void ahbpoll(struct cam_sim *sim);
90 /* Our timeout handler */
91 static timeout_t ahbtimeout;
93 static __inline struct ecb* ahbecbget(struct ahb_softc *ahb);
94 static __inline void ahbecbfree(struct ahb_softc* ahb,
96 static __inline u_int32_t ahbecbvtop(struct ahb_softc *ahb,
98 static __inline struct ecb* ahbecbptov(struct ahb_softc *ahb,
100 static __inline u_int32_t ahbstatuspaddr(u_int32_t ecb_paddr);
101 static __inline u_int32_t ahbsensepaddr(u_int32_t ecb_paddr);
102 static __inline u_int32_t ahbsgpaddr(u_int32_t ecb_paddr);
103 static __inline void ahbqueuembox(struct ahb_softc *ahb,
107 static __inline struct ecb*
108 ahbecbget(struct ahb_softc *ahb)
114 if ((ecb = SLIST_FIRST(&ahb->free_ecbs)) != NULL)
115 SLIST_REMOVE_HEAD(&ahb->free_ecbs, links);
122 ahbecbfree(struct ahb_softc* ahb, struct ecb* ecb)
127 ecb->state = ECB_FREE;
128 SLIST_INSERT_HEAD(&ahb->free_ecbs, ecb, links);
132 static __inline u_int32_t
133 ahbecbvtop(struct ahb_softc *ahb, struct ecb *ecb)
135 return (ahb->ecb_physbase
136 + (u_int32_t)((caddr_t)ecb - (caddr_t)ahb->ecb_array));
139 static __inline struct ecb*
140 ahbecbptov(struct ahb_softc *ahb, u_int32_t ecb_addr)
142 return (ahb->ecb_array
143 + ((struct ecb*)ecb_addr - (struct ecb*)ahb->ecb_physbase));
146 static __inline u_int32_t
147 ahbstatuspaddr(u_int32_t ecb_paddr)
149 return (ecb_paddr + offsetof(struct ecb, status));
152 static __inline u_int32_t
153 ahbsensepaddr(u_int32_t ecb_paddr)
155 return (ecb_paddr + offsetof(struct ecb, sense));
158 static __inline u_int32_t
159 ahbsgpaddr(u_int32_t ecb_paddr)
161 return (ecb_paddr + offsetof(struct ecb, sg_list));
165 ahbqueuembox(struct ahb_softc *ahb, u_int32_t mboxval, u_int attn_code)
171 status = ahb_inb(ahb, HOSTSTAT);
172 if ((status & (HOSTSTAT_MBOX_EMPTY|HOSTSTAT_BUSY))
173 == HOSTSTAT_MBOX_EMPTY)
178 panic("ahb%ld: adapter not taking commands\n", ahb->unit);
180 ahb_outl(ahb, MBOXOUT0, mboxval);
181 ahb_outb(ahb, ATTN, attn_code);
185 ahbmatch(eisa_id_t type)
187 switch(type & 0xfffffe00) {
188 case EISA_DEVICE_ID_ADAPTEC_1740:
189 return ("Adaptec 174x SCSI host adapter");
198 ahbprobe(device_t dev)
206 desc = ahbmatch(eisa_get_id(dev));
209 device_set_desc(dev, desc);
211 iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE) +
212 AHB_EISA_SLOT_OFFSET;
214 eisa_add_iospace(dev, iobase, AHB_EISA_IOSIZE, RESVADDR_NONE);
216 intdef = inb(INTDEF + iobase);
217 switch (intdef & 0x7) {
237 printf("Adaptec 174X at slot %d: illegal "
238 "irq setting %d\n", eisa_get_slot(dev),
246 shared = (inb(INTDEF + iobase) & INTLEVEL) ?
247 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
249 eisa_add_intr(dev, irq, shared);
255 ahbattach(device_t dev)
258 * find unit and check we have that many defined
260 struct ahb_softc *ahb;
261 struct ecb* next_ecb;
262 struct resource *io = 0;
263 struct resource *irq = 0;
268 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
269 0, ~0, 1, RF_ACTIVE);
271 device_printf(dev, "No I/O space?!\n");
275 if ((ahb = ahballoc(device_get_unit(dev), io)) == NULL) {
279 if (ahbreset(ahb) != 0)
283 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
284 0, ~0, 1, RF_ACTIVE);
286 device_printf(dev, "Can't allocate interrupt\n");
291 * Create our DMA tags. These tags define the kinds of device
292 * accessible memory allocations and memory mappings we will
293 * need to perform during normal operation.
295 /* DMA tag for mapping buffers into device visible space. */
296 /* XXX Should be a child of the EISA bus dma tag */
297 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
298 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
299 /*highaddr*/BUS_SPACE_MAXADDR,
300 /*filter*/NULL, /*filterarg*/NULL,
301 /*maxsize*/MAXBSIZE, /*nsegments*/AHB_NSEG,
302 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
303 /*flags*/BUS_DMA_ALLOCNOW,
304 &ahb->buffer_dmat) != 0)
309 /* DMA tag for our ccb structures and ha inquiry data */
310 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, /*boundary*/0,
311 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
312 /*highaddr*/BUS_SPACE_MAXADDR,
313 /*filter*/NULL, /*filterarg*/NULL,
314 (AHB_NECB * sizeof(struct ecb))
315 + sizeof(*ahb->ha_inq_data),
317 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
318 /*flags*/0, &ahb->ecb_dmat) != 0)
323 /* Allocation for our ccbs */
324 if (bus_dmamem_alloc(ahb->ecb_dmat, (void **)&ahb->ecb_array,
325 BUS_DMA_NOWAIT, &ahb->ecb_dmamap) != 0)
328 ahb->ha_inq_data = (struct ha_inquiry_data *)&ahb->ecb_array[AHB_NECB];
332 /* And permanently map them */
333 bus_dmamap_load(ahb->ecb_dmat, ahb->ecb_dmamap,
334 ahb->ecb_array, AHB_NSEG * sizeof(struct ecb),
335 ahbmapecbs, ahb, /*flags*/0);
339 /* Allocate the buffer dmamaps for each of our ECBs */
340 bzero(ahb->ecb_array, (AHB_NECB * sizeof(struct ecb))
341 + sizeof(*ahb->ha_inq_data));
342 next_ecb = ahb->ecb_array;
343 while (ahb->num_ecbs < AHB_NECB) {
346 if (bus_dmamap_create(ahb->buffer_dmat, /*flags*/0,
349 ecb_paddr = ahbecbvtop(ahb, next_ecb);
350 next_ecb->hecb.status_ptr = ahbstatuspaddr(ecb_paddr);
351 next_ecb->hecb.sense_ptr = ahbsensepaddr(ecb_paddr);
353 ahbecbfree(ahb, next_ecb);
357 if (ahb->num_ecbs == 0)
363 * Now that we know we own the resources we need, register
364 * our bus with the XPT.
366 if (ahbxptattach(ahb))
369 /* Enable our interrupt */
370 bus_setup_intr(dev, irq, INTR_TYPE_CAM, ahbintr, ahb, &ih);
375 * The board's IRQ line will not be left enabled
376 * if we can't intialize correctly, so its safe
377 * to release the irq.
382 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
384 bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
388 static struct ahb_softc *
389 ahballoc(u_long unit, struct resource *res)
391 struct ahb_softc *ahb;
394 * Allocate a storage area for us
396 ahb = malloc(sizeof(struct ahb_softc), M_DEVBUF, M_INTWAIT | M_ZERO);
397 SLIST_INIT(&ahb->free_ecbs);
398 LIST_INIT(&ahb->pending_ccbs);
400 ahb->tag = rman_get_bustag(res);
401 ahb->bsh = rman_get_bushandle(res);
402 ahb->disc_permitted = ~0;
403 ahb->tags_permitted = ~0;
409 ahbfree(struct ahb_softc *ahb)
411 switch (ahb->init_level) {
414 bus_dmamap_unload(ahb->ecb_dmat, ahb->ecb_dmamap);
417 bus_dmamem_free(ahb->ecb_dmat, ahb->ecb_array,
419 bus_dmamap_destroy(ahb->ecb_dmat, ahb->ecb_dmamap);
422 bus_dma_tag_destroy(ahb->ecb_dmat);
425 bus_dma_tag_destroy(ahb->buffer_dmat);
434 * reset board, If it doesn't respond, return failure
437 ahbreset(struct ahb_softc *ahb)
439 int wait = 1000; /* 1 sec enough? */
442 if ((ahb_inb(ahb, PORTADDR) & PORTADDR_ENHANCED) == 0) {
443 printf("ahb_reset: Controller not in enhanced mode\n");
447 ahb_outb(ahb, CONTROL, CNTRL_HARD_RST);
449 ahb_outb(ahb, CONTROL, 0);
452 if ((ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_BUSY) == 0)
457 printf("ahbreset: No answer from aha1742 board\n");
460 if ((test = ahb_inb(ahb, MBOXIN0)) != 0) {
461 printf("ahb_reset: self test failed, val = 0x%x\n", test);
464 while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
465 ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
472 ahbmapecbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
474 struct ahb_softc* ahb;
476 ahb = (struct ahb_softc*)arg;
477 ahb->ecb_physbase = segs->ds_addr;
479 * Space for adapter inquiry information is on the
480 * tail of the ecb array.
482 ahb->ha_inq_physbase = ahbecbvtop(ahb, &ahb->ecb_array[AHB_NECB]);
486 ahbxptattach(struct ahb_softc *ahb)
491 /* Remeber who are we on the scsi bus */
492 ahb->scsi_id = ahb_inb(ahb, SCSIDEF) & HSCSIID;
494 /* Use extended translation?? */
495 ahb->extended_trans = ahb_inb(ahb, RESV1) & EXTENDED_TRANS;
497 /* Fetch adapter inquiry data */
498 ecb = ahbecbget(ahb); /* Always succeeds - no outstanding commands */
499 ecb->hecb.opcode = ECBOP_READ_HA_INQDATA;
500 ecb->hecb.flag_word1 = FW1_SUPPRESS_URUN_ERR|FW1_ERR_STATUS_BLK_ONLY;
501 ecb->hecb.data_ptr = ahb->ha_inq_physbase;
502 ecb->hecb.data_len = sizeof(struct ha_inquiry_data);
503 ecb->hecb.sense_ptr = 0;
504 ecb->state = ECB_ACTIVE;
506 /* Tell the adapter about this command */
507 ahbqueuembox(ahb, ahbecbvtop(ahb, ecb),
508 ATTN_STARTECB|ahb->scsi_id);
510 /* Poll for interrupt completion */
511 for (i = 1000; ecb->state != ECB_FREE && i != 0; i--) {
516 ahb->num_ecbs = MIN(ahb->num_ecbs,
517 ahb->ha_inq_data->scsi_data.reserved[1]);
518 printf("ahb%ld: %.8s %s SCSI Adapter, FW Rev. %.4s, ID=%d, %d ECBs\n",
519 ahb->unit, ahb->ha_inq_data->scsi_data.product,
520 (ahb->ha_inq_data->scsi_data.flags & 0x4) ? "Differential"
522 ahb->ha_inq_data->scsi_data.revision,
523 ahb->scsi_id, ahb->num_ecbs);
525 /* Restore sense paddr for future CCB clients */
526 ecb->hecb.sense_ptr = ahbsensepaddr(ahbecbvtop(ahb, ecb));
528 ahbecbfree(ahb, ecb);
531 * Construct our SIM entry
533 ahb->sim = cam_sim_alloc(ahbaction, ahbpoll, "ahb", ahb, ahb->unit,
534 2, ahb->num_ecbs, NULL);
535 if (ahb->sim == NULL)
538 if (xpt_bus_register(ahb->sim, 0) != CAM_SUCCESS) {
539 cam_sim_free(ahb->sim);
543 if (xpt_create_path(&ahb->path, /*periph*/NULL,
544 cam_sim_path(ahb->sim), CAM_TARGET_WILDCARD,
545 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
546 xpt_bus_deregister(cam_sim_path(ahb->sim));
547 cam_sim_free(ahb->sim);
552 * Allow the board to generate interrupts.
554 ahb_outb(ahb, INTDEF, ahb_inb(ahb, INTDEF) | INTEN);
560 ahbhandleimmed(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
562 struct ccb_hdr *ccb_h;
565 if (ahb->immed_cmd == 0) {
566 printf("ahb%ld: Immediate Command complete with no "
567 " pending command\n", ahb->unit);
571 target_id = intstat & INTSTAT_TARGET_MASK;
573 ccb_h = LIST_FIRST(&ahb->pending_ccbs);
574 while (ccb_h != NULL) {
575 struct ecb *pending_ecb;
578 pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
579 ccb = pending_ecb->ccb;
580 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
581 if (ccb->ccb_h.target_id == target_id
582 || target_id == ahb->scsi_id) {
583 callout_stop(&ccb->ccb_h.timeout_ch);
584 LIST_REMOVE(&ccb->ccb_h, sim_links.le);
585 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
586 bus_dmamap_unload(ahb->buffer_dmat,
587 pending_ecb->dmamap);
588 if (pending_ecb == ahb->immed_ecb)
590 CAM_CMD_TIMEOUT|CAM_RELEASE_SIMQ;
591 else if (target_id == ahb->scsi_id)
592 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
594 ccb->ccb_h.status = CAM_BDR_SENT;
595 ahbecbfree(ahb, pending_ecb);
597 } else if (ahb->immed_ecb != NULL) {
598 /* Re-instate timeout */
599 callout_reset(&ccb->ccb_h.timeout_ch,
600 (ccb->ccb_h.timeout * hz) / 1000,
601 ahbtimeout, pending_ecb);
605 if (ahb->immed_ecb != NULL) {
606 ahb->immed_ecb = NULL;
607 printf("ahb%ld: No longer in timeout\n", ahb->unit);
608 } else if (target_id == ahb->scsi_id)
609 printf("ahb%ld: SCSI Bus Reset Delivered\n", ahb->unit);
611 printf("ahb%ld: Bus Device Reset Delibered to target %d\n",
612 ahb->unit, target_id);
618 ahbcalcresid(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
620 if (ecb->status.data_overrun != 0) {
622 * Overrun Condition. The hardware doesn't
623 * provide a meaningful byte count in this case
624 * (the residual is always 0). Tell the XPT
625 * layer about the error.
627 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
629 ccb->csio.resid = ecb->status.resid_count;
631 if ((ecb->hecb.flag_word1 & FW1_SG_ECB) != 0) {
633 * For S/G transfers, the adapter provides a pointer
634 * to the address in the last S/G element used and a
635 * residual for that element. So, we need to sum up
636 * the elements that follow it in order to get a real
637 * residual number. If we have an overrun, the residual
638 * reported will be 0 and we already know that all S/G
639 * segments have been exhausted, so we can skip this
645 num_sg = ecb->hecb.data_len / sizeof(ahb_sg_t);
647 /* Find the S/G the adapter was working on */
648 for (sg = ecb->sg_list;
649 num_sg != 0 && sg->addr != ecb->status.resid_addr;
658 for (; num_sg != 0; num_sg--, sg++)
659 ccb->csio.resid += sg->len;
661 /* Underruns are not errors */
662 ccb->ccb_h.status = CAM_REQ_CMP;
667 ahbprocesserror(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
669 struct hardware_ecb *hecb;
670 struct ecb_status *status;
673 status = &ecb->status;
674 switch (status->ha_status) {
676 ccb->csio.scsi_status = status->scsi_status;
677 if (status->scsi_status != 0) {
678 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
679 if (status->sense_stored) {
680 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
681 ccb->csio.sense_resid =
682 ccb->csio.sense_len - status->sense_len;
683 bcopy(&ecb->sense, &ccb->csio.sense_data,
688 case HS_TARGET_NOT_ASSIGNED:
689 ccb->ccb_h.status = CAM_PATH_INVALID;
692 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
694 case HS_DATA_RUN_ERR:
695 ahbcalcresid(ahb, ecb, ccb);
697 case HS_UNEXPECTED_BUSFREE:
698 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
700 case HS_INVALID_PHASE:
701 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
703 case HS_REQUEST_SENSE_FAILED:
704 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
706 case HS_TAG_MSG_REJECTED:
708 struct ccb_trans_settings neg;
710 xpt_print_path(ccb->ccb_h.path);
711 printf("refuses tagged commands. Performing "
714 neg.valid = CCB_TRANS_TQ_VALID;
715 xpt_setup_ccb(&neg.ccb_h, ccb->ccb_h.path, /*priority*/1);
716 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, &neg);
717 ahb->tags_permitted &= ~(0x01 << ccb->ccb_h.target_id);
718 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
721 case HS_FIRMWARE_LOAD_REQ:
722 case HS_HARDWARE_ERR:
724 * Tell the system that the Adapter
725 * is no longer functional.
727 ccb->ccb_h.status = CAM_NO_HBA;
729 case HS_CMD_ABORTED_HOST:
730 case HS_CMD_ABORTED_ADAPTER:
731 case HS_ATN_TARGET_FAILED:
732 case HS_SCSI_RESET_ADAPTER:
733 case HS_SCSI_RESET_INCOMING:
734 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
736 case HS_INVALID_ECB_PARAM:
737 printf("ahb%ld: opcode 0x%02x, flag_word1 0x%02x, flag_word2 0x%02x\n",
738 ahb->unit, hecb->opcode, hecb->flag_word1, hecb->flag_word2);
739 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
741 case HS_DUP_TCB_RECEIVED:
742 case HS_INVALID_OPCODE:
743 case HS_INVALID_CMD_LINK:
744 case HS_PROGRAM_CKSUM_ERROR:
745 panic("ahb%ld: Can't happen host status %x occurred",
746 ahb->unit, status->ha_status);
749 if (ccb->ccb_h.status != CAM_REQ_CMP) {
750 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
751 ccb->ccb_h.status |= CAM_DEV_QFRZN;
756 ahbdone(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
761 ecb = ahbecbptov(ahb, mbox);
763 if ((ecb->state & ECB_ACTIVE) == 0)
764 panic("ecb not active");
769 callout_stop(&ccb->ccb_h.timeout_ch);
770 LIST_REMOVE(&ccb->ccb_h, sim_links.le);
772 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
775 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
776 op = BUS_DMASYNC_POSTREAD;
778 op = BUS_DMASYNC_POSTWRITE;
779 bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
780 bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
783 if ((intstat & INTSTAT_MASK) == INTSTAT_ECB_OK) {
784 ccb->ccb_h.status = CAM_REQ_CMP;
787 ahbprocesserror(ahb, ecb, ccb);
789 ahbecbfree(ahb, ecb);
792 /* Non CCB Command */
793 if ((intstat & INTSTAT_MASK) != INTSTAT_ECB_OK) {
794 printf("ahb%ld: Command 0%x Failed %x:%x:%x\n",
795 ahb->unit, ecb->hecb.opcode,
796 *((u_int16_t*)&ecb->status),
797 ecb->status.ha_status, ecb->status.resid_count);
799 /* Client owns this ECB and will release it. */
804 * Catch an interrupt from the adaptor
809 struct ahb_softc *ahb;
813 ahb = (struct ahb_softc *)arg;
815 while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
817 * Fetch information about this interrupt.
819 intstat = ahb_inb(ahb, INTSTAT);
820 mbox = ahb_inl(ahb, MBOXIN0);
823 * Reset interrupt latch.
825 ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
828 * Process the completed operation
830 switch (intstat & INTSTAT_MASK) {
832 case INTSTAT_ECB_CMPWRETRY:
833 case INTSTAT_ECB_CMPWERR:
834 ahbdone(ahb, mbox, intstat);
836 case INTSTAT_AEN_OCCURED:
837 if ((intstat & INTSTAT_TARGET_MASK) == ahb->scsi_id) {
839 xpt_print_path(ahb->path);
841 case HS_SCSI_RESET_ADAPTER:
842 printf("Host Adapter Initiated "
843 "Bus Reset occurred\n");
845 case HS_SCSI_RESET_INCOMING:
846 printf("Bus Reset Initiated "
847 "by another device occurred\n");
851 xpt_async(AC_BUS_RESET, ahb->path, NULL);
854 printf("Unsupported initiator selection AEN occured\n");
856 case INTSTAT_IMMED_OK:
857 case INTSTAT_IMMED_ERR:
858 ahbhandleimmed(ahb, mbox, intstat);
861 panic("Unrecoverable hardware Error Occurred\n");
867 ahbexecuteecb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
871 struct ahb_softc *ahb;
875 ecb = (struct ecb *)arg;
877 ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
881 printf("ahb%ld: Unexepected error 0x%x returned from "
882 "bus_dmamap_load\n", ahb->unit, error);
883 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
884 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
885 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
887 ahbecbfree(ahb, ecb);
892 ecb_paddr = ahbecbvtop(ahb, ecb);
896 bus_dma_segment_t *end_seg;
899 end_seg = dm_segs + nseg;
901 /* Copy the segments into our SG list */
903 while (dm_segs < end_seg) {
904 sg->addr = dm_segs->ds_addr;
905 sg->len = dm_segs->ds_len;
911 ecb->hecb.flag_word1 |= FW1_SG_ECB;
912 ecb->hecb.data_ptr = ahbsgpaddr(ecb_paddr);
913 ecb->hecb.data_len = sizeof(ahb_sg_t) * nseg;
915 ecb->hecb.data_ptr = ecb->sg_list->addr;
916 ecb->hecb.data_len = ecb->sg_list->len;
919 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
920 /* ecb->hecb.flag_word2 |= FW2_DATA_DIR_IN; */
921 op = BUS_DMASYNC_PREREAD;
923 op = BUS_DMASYNC_PREWRITE;
925 /* ecb->hecb.flag_word2 |= FW2_CHECK_DATA_DIR; */
927 bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
930 ecb->hecb.data_ptr = 0;
931 ecb->hecb.data_len = 0;
937 * Last time we need to check if this CCB needs to
940 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
942 bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
943 ahbecbfree(ahb, ecb);
949 ecb->state = ECB_ACTIVE;
950 ccb->ccb_h.status |= CAM_SIM_QUEUED;
951 LIST_INSERT_HEAD(&ahb->pending_ccbs, &ccb->ccb_h, sim_links.le);
953 /* Tell the adapter about this command */
954 ahbqueuembox(ahb, ecb_paddr, ATTN_STARTECB|ccb->ccb_h.target_id);
956 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
962 ahbaction(struct cam_sim *sim, union ccb *ccb)
964 struct ahb_softc *ahb;
966 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahbaction\n"));
968 ahb = (struct ahb_softc *)cam_sim_softc(sim);
970 switch (ccb->ccb_h.func_code) {
971 /* Common cases first */
972 case XPT_SCSI_IO: /* Execute the requested I/O operation */
975 struct hardware_ecb *hecb;
980 if ((ecb = ahbecbget(ahb)) == NULL) {
981 /* Should never occur */
982 panic("Failed to get an ecb");
986 * So we can find the ECB when an abort is requested
989 ccb->ccb_h.ccb_ecb_ptr = ecb;
990 ccb->ccb_h.ccb_ahb_ptr = ahb;
993 * Put all the arguments for the xfer in the ecb
996 hecb->opcode = ECBOP_INITIATOR_SCSI_CMD;
997 hecb->flag_word1 = FW1_AUTO_REQUEST_SENSE
998 | FW1_ERR_STATUS_BLK_ONLY;
999 hecb->flag_word2 = ccb->ccb_h.target_lun
1000 | FW2_NO_RETRY_ON_BUSY;
1001 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
1002 hecb->flag_word2 |= FW2_TAG_ENB
1003 | ((ccb->csio.tag_action & 0x3)
1004 << FW2_TAG_TYPE_SHIFT);
1006 if ((ccb->ccb_h.flags & CAM_DIS_DISCONNECT) != 0)
1007 hecb->flag_word2 |= FW2_DISABLE_DISC;
1008 hecb->sense_len = ccb->csio.sense_len;
1009 hecb->cdb_len = ccb->csio.cdb_len;
1010 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1011 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
1012 bcopy(ccb->csio.cdb_io.cdb_ptr,
1013 hecb->cdb, hecb->cdb_len);
1015 /* I guess I could map it in... */
1016 ccb->ccb_h.status = CAM_REQ_INVALID;
1017 ahbecbfree(ahb, ecb);
1022 bcopy(ccb->csio.cdb_io.cdb_bytes,
1023 hecb->cdb, hecb->cdb_len);
1027 * If we have any data to send with this command,
1028 * map it into bus space.
1030 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1031 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1033 * We've been given a pointer
1034 * to a single buffer.
1036 if ((ccb->ccb_h.flags & CAM_DATA_PHYS)==0) {
1041 error = bus_dmamap_load(
1045 ccb->csio.dxfer_len,
1048 if (error == EINPROGRESS) {
1050 * So as to maintain ordering,
1051 * freeze the controller queue
1052 * until our mapping is
1055 xpt_freeze_simq(ahb->sim, 1);
1056 ccb->ccb_h.status |=
1061 struct bus_dma_segment seg;
1063 /* Pointer to physical buffer */
1065 (bus_addr_t)ccb->csio.data_ptr;
1066 seg.ds_len = ccb->csio.dxfer_len;
1067 ahbexecuteecb(ecb, &seg, 1, 0);
1070 struct bus_dma_segment *segs;
1072 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
1073 panic("ahbaction - Physical segment "
1074 "pointers unsupported");
1076 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0)
1077 panic("btaction - Virtual segment "
1078 "addresses unsupported");
1080 /* Just use the segments provided */
1081 segs = (struct bus_dma_segment *)
1083 ahbexecuteecb(ecb, segs, ccb->csio.sglist_cnt,
1087 ahbexecuteecb(ecb, NULL, 0, 0);
1091 case XPT_EN_LUN: /* Enable LUN as a target */
1092 case XPT_TARGET_IO: /* Execute target I/O request */
1093 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1094 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1095 case XPT_ABORT: /* Abort the specified CCB */
1097 ccb->ccb_h.status = CAM_REQ_INVALID;
1100 case XPT_SET_TRAN_SETTINGS:
1102 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1106 case XPT_GET_TRAN_SETTINGS:
1107 /* Get default/user set transfer settings for the target */
1109 struct ccb_trans_settings *cts;
1113 target_mask = 0x01 << ccb->ccb_h.target_id;
1114 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1116 if ((ahb->disc_permitted & target_mask) != 0)
1117 cts->flags |= CCB_TRANS_DISC_ENB;
1118 if ((ahb->tags_permitted & target_mask) != 0)
1119 cts->flags |= CCB_TRANS_TAG_ENB;
1120 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1121 cts->sync_period = 25; /* 10MHz */
1123 if (cts->sync_period != 0)
1124 cts->sync_offset = 15;
1126 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1127 | CCB_TRANS_SYNC_OFFSET_VALID
1128 | CCB_TRANS_BUS_WIDTH_VALID
1129 | CCB_TRANS_DISC_VALID
1130 | CCB_TRANS_TQ_VALID;
1131 ccb->ccb_h.status = CAM_REQ_CMP;
1133 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1138 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1144 ahb->immed_cmd = IMMED_RESET;
1145 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1146 /* Poll for interrupt completion */
1147 for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--) {
1149 ahbintr(cam_sim_softc(sim));
1154 case XPT_CALC_GEOMETRY:
1156 struct ccb_calc_geometry *ccg;
1158 u_int32_t secs_per_cylinder;
1161 size_mb = ccg->volume_size
1162 / ((1024L * 1024L) / ccg->block_size);
1164 if (size_mb > 1024 && (ahb->extended_trans != 0)) {
1166 ccg->secs_per_track = 63;
1169 ccg->secs_per_track = 32;
1171 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1172 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1173 ccb->ccb_h.status = CAM_REQ_CMP;
1177 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1181 ahb->immed_cmd = IMMED_RESET;
1182 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1183 /* Poll for interrupt completion */
1184 for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--)
1186 ccb->ccb_h.status = CAM_REQ_CMP;
1190 case XPT_TERM_IO: /* Terminate the I/O process */
1192 ccb->ccb_h.status = CAM_REQ_INVALID;
1195 case XPT_PATH_INQ: /* Path routing inquiry */
1197 struct ccb_pathinq *cpi = &ccb->cpi;
1199 cpi->version_num = 1; /* XXX??? */
1200 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1201 cpi->target_sprt = 0;
1203 cpi->hba_eng_cnt = 0;
1204 cpi->max_target = 7;
1206 cpi->initiator_id = ahb->scsi_id;
1207 cpi->bus_id = cam_sim_bus(sim);
1208 cpi->base_transfer_speed = 3300;
1209 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1210 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
1211 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1212 cpi->unit_number = cam_sim_unit(sim);
1213 cpi->ccb_h.status = CAM_REQ_CMP;
1219 case XPT_IMMED_NOTIFY: /* Notify Host Target driver of event */
1220 case XPT_NOTIFY_ACK: /* Acknowledgement of event */
1223 ccb->ccb_h.status = CAM_REQ_INVALID;
1230 ahbpoll(struct cam_sim *sim)
1232 ahbintr(cam_sim_softc(sim));
1236 ahbtimeout(void *arg)
1240 struct ahb_softc *ahb;
1243 ecb = (struct ecb *)arg;
1245 ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
1246 xpt_print_path(ccb->ccb_h.path);
1247 printf("ECB %p - timed out\n", (void *)ecb);
1251 if ((ecb->state & ECB_ACTIVE) == 0) {
1252 xpt_print_path(ccb->ccb_h.path);
1253 printf("ECB %p - timed out ECB already completed\n",
1259 * In order to simplify the recovery process, we ask the XPT
1260 * layer to halt the queue of new transactions and we traverse
1261 * the list of pending CCBs and remove their timeouts. This
1262 * means that the driver attempts to clear only one error
1263 * condition at a time. In general, timeouts that occur
1264 * close together are related anyway, so there is no benefit
1265 * in attempting to handle errors in parrallel. Timeouts will
1266 * be reinstated when the recovery process ends.
1268 if ((ecb->state & ECB_DEVICE_RESET) == 0) {
1269 struct ccb_hdr *ccb_h;
1271 if ((ecb->state & ECB_RELEASE_SIMQ) == 0) {
1272 xpt_freeze_simq(ahb->sim, /*count*/1);
1273 ecb->state |= ECB_RELEASE_SIMQ;
1276 ccb_h = LIST_FIRST(&ahb->pending_ccbs);
1277 while (ccb_h != NULL) {
1278 struct ecb *pending_ecb;
1280 pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
1281 callout_stop(&ccb_h->timeout_ch);
1282 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1285 /* Store for our interrupt handler */
1286 ahb->immed_ecb = ecb;
1289 * Send a Bus Device Reset message:
1290 * The target that is holding up the bus may not
1291 * be the same as the one that triggered this timeout
1292 * (different commands have different timeout lengths),
1293 * but we have no way of determining this from our
1294 * timeout handler. Our strategy here is to queue a
1295 * BDR message to the target of the timed out command.
1296 * If this fails, we'll get another timeout 2 seconds
1297 * later which will attempt a bus reset.
1299 xpt_print_path(ccb->ccb_h.path);
1300 printf("Queuing BDR\n");
1301 ecb->state |= ECB_DEVICE_RESET;
1302 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, ahbtimeout, ecb);
1304 ahb->immed_cmd = IMMED_RESET;
1305 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1306 } else if ((ecb->state & ECB_SCSIBUS_RESET) != 0) {
1308 * Try a SCSI bus reset. We do this only if we
1309 * have already attempted to clear the condition with a BDR.
1311 xpt_print_path(ccb->ccb_h.path);
1312 printf("Attempting SCSI Bus reset\n");
1313 ecb->state |= ECB_SCSIBUS_RESET;
1314 callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, ahbtimeout, ecb);
1315 ahb->immed_cmd = IMMED_RESET;
1316 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1318 /* Bring out the hammer... */
1321 /* Simulate the reset complete interrupt */
1322 ahbhandleimmed(ahb, 0, ahb->scsi_id|INTSTAT_IMMED_OK);
1328 static device_method_t ahb_eisa_methods[] = {
1329 /* Device interface */
1330 DEVMETHOD(device_probe, ahbprobe),
1331 DEVMETHOD(device_attach, ahbattach),
1336 static driver_t ahb_eisa_driver = {
1342 static devclass_t ahb_devclass;
1344 DRIVER_MODULE(ahb, eisa, ahb_eisa_driver, ahb_devclass, 0, 0);