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
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_sim.h>
48 #include <cam/cam_xpt_sim.h>
49 #include <cam/cam_debug.h>
51 #include <cam/scsi/scsi_message.h>
53 #include <dev/eisa/eisaconf.h>
55 #include <dev/ahb/ahbreg.h>
57 #define ccb_ecb_ptr spriv_ptr0
58 #define ccb_ahb_ptr spriv_ptr1
60 #define ahb_inb(ahb, port) \
61 bus_read_1((ahb)->res, port)
63 #define ahb_inl(ahb, port) \
64 bus_read_4((ahb)->res, port)
66 #define ahb_outb(ahb, port, value) \
67 bus_write_1((ahb)->res, port, value)
69 #define ahb_outl(ahb, port, value) \
70 bus_write_4((ahb)->res, port, value)
72 static const char *ahbmatch(eisa_id_t type);
73 static struct ahb_softc *ahballoc(device_t dev, 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 void ahbintr_locked(struct ahb_softc *ahb);
87 static bus_dmamap_callback_t ahbexecuteecb;
88 static void ahbaction(struct cam_sim *sim, union ccb *ccb);
89 static void ahbpoll(struct cam_sim *sim);
91 /* Our timeout handler */
92 static void ahbtimeout(void *arg);
94 static __inline struct ecb* ahbecbget(struct ahb_softc *ahb);
95 static __inline void ahbecbfree(struct ahb_softc* ahb,
97 static __inline u_int32_t ahbecbvtop(struct ahb_softc *ahb,
99 static __inline struct ecb* ahbecbptov(struct ahb_softc *ahb,
101 static __inline u_int32_t ahbstatuspaddr(u_int32_t ecb_paddr);
102 static __inline u_int32_t ahbsensepaddr(u_int32_t ecb_paddr);
103 static __inline u_int32_t ahbsgpaddr(u_int32_t ecb_paddr);
104 static __inline void ahbqueuembox(struct ahb_softc *ahb,
108 static __inline struct ecb*
109 ahbecbget(struct ahb_softc *ahb)
114 mtx_assert(&ahb->lock, MA_OWNED);
115 if ((ecb = SLIST_FIRST(&ahb->free_ecbs)) != NULL)
116 SLIST_REMOVE_HEAD(&ahb->free_ecbs, links);
122 ahbecbfree(struct ahb_softc* ahb, struct ecb* ecb)
126 mtx_assert(&ahb->lock, MA_OWNED);
127 ecb->state = ECB_FREE;
128 SLIST_INSERT_HEAD(&ahb->free_ecbs, ecb, links);
131 static __inline u_int32_t
132 ahbecbvtop(struct ahb_softc *ahb, struct ecb *ecb)
134 return (ahb->ecb_physbase
135 + (u_int32_t)((caddr_t)ecb - (caddr_t)ahb->ecb_array));
138 static __inline struct ecb*
139 ahbecbptov(struct ahb_softc *ahb, u_int32_t ecb_addr)
141 return (ahb->ecb_array
142 + ((struct ecb*)(uintptr_t)ecb_addr
143 - (struct ecb*)(uintptr_t)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("%s: adapter not taking commands\n",
179 device_get_nameunit(ahb->dev));
181 ahb_outl(ahb, MBOXOUT0, mboxval);
182 ahb_outb(ahb, ATTN, attn_code);
186 ahbmatch(eisa_id_t type)
188 switch(type & 0xfffffe00) {
189 case EISA_DEVICE_ID_ADAPTEC_1740:
190 return ("Adaptec 174x SCSI host adapter");
199 ahbprobe(device_t dev)
207 desc = ahbmatch(eisa_get_id(dev));
210 device_set_desc(dev, desc);
212 iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE) +
213 AHB_EISA_SLOT_OFFSET;
215 eisa_add_iospace(dev, iobase, AHB_EISA_IOSIZE, RESVADDR_NONE);
217 intdef = inb(INTDEF + iobase);
218 switch (intdef & 0x7) {
238 printf("Adaptec 174X at slot %d: illegal "
239 "irq setting %d\n", eisa_get_slot(dev),
247 shared = (inb(INTDEF + iobase) & INTLEVEL) ?
248 EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
250 eisa_add_intr(dev, irq, shared);
256 ahbattach(device_t dev)
259 * find unit and check we have that many defined
261 struct ahb_softc *ahb;
262 struct ecb* next_ecb;
264 struct resource *irq;
270 io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
272 device_printf(dev, "No I/O space?!\n");
276 ahb = ahballoc(dev, io);
278 if (ahbreset(ahb) != 0)
282 irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
284 device_printf(dev, "Can't allocate interrupt\n");
289 * Create our DMA tags. These tags define the kinds of device
290 * accessible memory allocations and memory mappings we will
291 * need to perform during normal operation.
293 /* DMA tag for mapping buffers into device visible space. */
294 if (bus_dma_tag_create( /* parent */ bus_get_dma_tag(dev),
297 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
298 /* highaddr */ BUS_SPACE_MAXADDR,
300 /* filterarg */ NULL,
301 /* maxsize */ MAXBSIZE,
302 /* nsegments */ AHB_NSEG,
303 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
304 /* flags */ BUS_DMA_ALLOCNOW,
305 /* lockfunc */ busdma_lock_mutex,
306 /* lockarg */ &ahb->lock,
307 &ahb->buffer_dmat) != 0)
312 /* DMA tag for our ccb structures and ha inquiry data */
313 if (bus_dma_tag_create( /* parent */ bus_get_dma_tag(dev),
316 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
317 /* highaddr */ BUS_SPACE_MAXADDR,
319 /* filterarg */ NULL,
320 /* maxsize */ (AHB_NECB *
322 + sizeof(*ahb->ha_inq_data),
324 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
328 &ahb->ecb_dmat) != 0)
333 /* Allocation for our ccbs */
334 if (bus_dmamem_alloc(ahb->ecb_dmat, (void **)&ahb->ecb_array,
335 BUS_DMA_NOWAIT, &ahb->ecb_dmamap) != 0)
338 ahb->ha_inq_data = (struct ha_inquiry_data *)&ahb->ecb_array[AHB_NECB];
342 /* And permanently map them */
343 bus_dmamap_load(ahb->ecb_dmat, ahb->ecb_dmamap,
344 ahb->ecb_array, AHB_NSEG * sizeof(struct ecb),
345 ahbmapecbs, ahb, /*flags*/0);
349 /* Allocate the buffer dmamaps for each of our ECBs */
350 bzero(ahb->ecb_array, (AHB_NECB * sizeof(struct ecb))
351 + sizeof(*ahb->ha_inq_data));
352 next_ecb = ahb->ecb_array;
353 while (ahb->num_ecbs < AHB_NECB) {
356 if (bus_dmamap_create(ahb->buffer_dmat, /*flags*/0,
359 callout_init_mtx(&next_ecb->timer, &ahb->lock, 0);
360 ecb_paddr = ahbecbvtop(ahb, next_ecb);
361 next_ecb->hecb.status_ptr = ahbstatuspaddr(ecb_paddr);
362 next_ecb->hecb.sense_ptr = ahbsensepaddr(ecb_paddr);
364 ahbecbfree(ahb, next_ecb);
371 * Now that we know we own the resources we need, register
372 * our bus with the XPT.
374 if (ahbxptattach(ahb))
377 /* Enable our interrupt */
378 if (bus_setup_intr(dev, irq, INTR_TYPE_CAM|INTR_ENTROPY|INTR_MPSAFE,
379 NULL, ahbintr, ahb, &ih) != 0)
386 * The board's IRQ line will not be left enabled
387 * if we can't initialize correctly, so its safe
388 * to release the irq.
392 bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
393 bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
397 static struct ahb_softc *
398 ahballoc(device_t dev, struct resource *res)
400 struct ahb_softc *ahb;
402 ahb = device_get_softc(dev);
403 SLIST_INIT(&ahb->free_ecbs);
404 LIST_INIT(&ahb->pending_ccbs);
406 ahb->disc_permitted = ~0;
407 ahb->tags_permitted = ~0;
409 mtx_init(&ahb->lock, "ahb", NULL, MTX_DEF);
415 ahbfree(struct ahb_softc *ahb)
417 switch (ahb->init_level) {
420 bus_dmamap_unload(ahb->ecb_dmat, ahb->ecb_dmamap);
422 bus_dmamem_free(ahb->ecb_dmat, ahb->ecb_array,
424 bus_dmamap_destroy(ahb->ecb_dmat, ahb->ecb_dmamap);
426 bus_dma_tag_destroy(ahb->ecb_dmat);
428 bus_dma_tag_destroy(ahb->buffer_dmat);
432 mtx_destroy(&ahb->lock);
436 * reset board, If it doesn't respond, return failure
439 ahbreset(struct ahb_softc *ahb)
441 int wait = 1000; /* 1 sec enough? */
444 if ((ahb_inb(ahb, PORTADDR) & PORTADDR_ENHANCED) == 0) {
445 printf("ahb_reset: Controller not in enhanced mode\n");
449 ahb_outb(ahb, CONTROL, CNTRL_HARD_RST);
451 ahb_outb(ahb, CONTROL, 0);
454 if ((ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_BUSY) == 0)
459 printf("ahbreset: No answer from aha1742 board\n");
462 if ((test = ahb_inb(ahb, MBOXIN0)) != 0) {
463 printf("ahb_reset: self test failed, val = 0x%x\n", test);
466 while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
467 ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
474 ahbmapecbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
476 struct ahb_softc* ahb;
478 ahb = (struct ahb_softc*)arg;
479 ahb->ecb_physbase = segs->ds_addr;
481 * Space for adapter inquiry information is on the
482 * tail of the ecb array.
484 ahb->ha_inq_physbase = ahbecbvtop(ahb, &ahb->ecb_array[AHB_NECB]);
488 ahbxptattach(struct ahb_softc *ahb)
490 struct cam_devq *devq;
494 mtx_lock(&ahb->lock);
496 /* Remember who are we on the scsi bus */
497 ahb->scsi_id = ahb_inb(ahb, SCSIDEF) & HSCSIID;
499 /* Use extended translation?? */
500 ahb->extended_trans = ahb_inb(ahb, RESV1) & EXTENDED_TRANS;
502 /* Fetch adapter inquiry data */
503 ecb = ahbecbget(ahb); /* Always succeeds - no outstanding commands */
504 ecb->hecb.opcode = ECBOP_READ_HA_INQDATA;
505 ecb->hecb.flag_word1 = FW1_SUPPRESS_URUN_ERR|FW1_ERR_STATUS_BLK_ONLY;
506 ecb->hecb.data_ptr = ahb->ha_inq_physbase;
507 ecb->hecb.data_len = sizeof(struct ha_inquiry_data);
508 ecb->hecb.sense_ptr = 0;
509 ecb->state = ECB_ACTIVE;
511 /* Tell the adapter about this command */
512 ahbqueuembox(ahb, ahbecbvtop(ahb, ecb),
513 ATTN_STARTECB|ahb->scsi_id);
515 /* Poll for interrupt completion */
516 for (i = 1000; ecb->state != ECB_FREE && i != 0; i--) {
521 ahb->num_ecbs = MIN(ahb->num_ecbs,
522 ahb->ha_inq_data->scsi_data.spc2_flags);
523 device_printf(ahb->dev,
524 "%.8s %s SCSI Adapter, FW Rev. %.4s, ID=%d, %d ECBs\n",
525 ahb->ha_inq_data->scsi_data.product,
526 (ahb->ha_inq_data->scsi_data.flags & 0x4) ? "Differential"
528 ahb->ha_inq_data->scsi_data.revision,
529 ahb->scsi_id, ahb->num_ecbs);
531 /* Restore sense paddr for future CCB clients */
532 ecb->hecb.sense_ptr = ahbsensepaddr(ahbecbvtop(ahb, ecb));
534 ahbecbfree(ahb, ecb);
537 * Create the device queue for our SIM.
539 devq = cam_simq_alloc(ahb->num_ecbs);
541 mtx_unlock(&ahb->lock);
546 * Construct our SIM entry
548 ahb->sim = cam_sim_alloc(ahbaction, ahbpoll, "ahb", ahb,
549 device_get_unit(ahb->dev), &ahb->lock, 2, ahb->num_ecbs, devq);
550 if (ahb->sim == NULL) {
552 mtx_unlock(&ahb->lock);
556 if (xpt_bus_register(ahb->sim, ahb->dev, 0) != CAM_SUCCESS) {
557 cam_sim_free(ahb->sim, /*free_devq*/TRUE);
558 mtx_unlock(&ahb->lock);
562 if (xpt_create_path(&ahb->path, /*periph*/NULL,
563 cam_sim_path(ahb->sim), CAM_TARGET_WILDCARD,
564 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
565 xpt_bus_deregister(cam_sim_path(ahb->sim));
566 cam_sim_free(ahb->sim, /*free_devq*/TRUE);
567 mtx_unlock(&ahb->lock);
572 * Allow the board to generate interrupts.
574 ahb_outb(ahb, INTDEF, ahb_inb(ahb, INTDEF) | INTEN);
575 mtx_unlock(&ahb->lock);
581 ahbhandleimmed(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
583 struct ccb_hdr *ccb_h;
586 if (ahb->immed_cmd == 0) {
587 device_printf(ahb->dev, "Immediate Command complete with no "
588 " pending command\n");
592 target_id = intstat & INTSTAT_TARGET_MASK;
594 ccb_h = LIST_FIRST(&ahb->pending_ccbs);
595 while (ccb_h != NULL) {
596 struct ecb *pending_ecb;
599 pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
600 ccb = pending_ecb->ccb;
601 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
602 if (ccb->ccb_h.target_id == target_id
603 || target_id == ahb->scsi_id) {
604 callout_stop(&pending_ecb->timer);
605 LIST_REMOVE(&ccb->ccb_h, sim_links.le);
606 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
607 bus_dmamap_unload(ahb->buffer_dmat,
608 pending_ecb->dmamap);
609 if (pending_ecb == ahb->immed_ecb)
611 CAM_CMD_TIMEOUT|CAM_RELEASE_SIMQ;
612 else if (target_id == ahb->scsi_id)
613 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
615 ccb->ccb_h.status = CAM_BDR_SENT;
616 ahbecbfree(ahb, pending_ecb);
618 } else if (ahb->immed_ecb != NULL) {
619 /* Re-instate timeout */
620 callout_reset(&pending_ecb->timer,
621 (ccb->ccb_h.timeout * hz) / 1000,
622 ahbtimeout, pending_ecb);
626 if (ahb->immed_ecb != NULL) {
627 ahb->immed_ecb = NULL;
628 device_printf(ahb->dev, "No longer in timeout\n");
629 } else if (target_id == ahb->scsi_id)
630 device_printf(ahb->dev, "SCSI Bus Reset Delivered\n");
632 device_printf(ahb->dev,
633 "Bus Device Reset Delivered to target %d\n", target_id);
639 ahbcalcresid(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
641 if (ecb->status.data_overrun != 0) {
643 * Overrun Condition. The hardware doesn't
644 * provide a meaningful byte count in this case
645 * (the residual is always 0). Tell the XPT
646 * layer about the error.
648 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
650 ccb->csio.resid = ecb->status.resid_count;
652 if ((ecb->hecb.flag_word1 & FW1_SG_ECB) != 0) {
654 * For S/G transfers, the adapter provides a pointer
655 * to the address in the last S/G element used and a
656 * residual for that element. So, we need to sum up
657 * the elements that follow it in order to get a real
658 * residual number. If we have an overrun, the residual
659 * reported will be 0 and we already know that all S/G
660 * segments have been exhausted, so we can skip this
666 num_sg = ecb->hecb.data_len / sizeof(ahb_sg_t);
668 /* Find the S/G the adapter was working on */
669 for (sg = ecb->sg_list;
670 num_sg != 0 && sg->addr != ecb->status.resid_addr;
679 for (; num_sg != 0; num_sg--, sg++)
680 ccb->csio.resid += sg->len;
682 /* Underruns are not errors */
683 ccb->ccb_h.status = CAM_REQ_CMP;
688 ahbprocesserror(struct ahb_softc *ahb, struct ecb *ecb, union ccb *ccb)
690 struct hardware_ecb *hecb;
691 struct ecb_status *status;
694 status = &ecb->status;
695 switch (status->ha_status) {
697 ccb->csio.scsi_status = status->scsi_status;
698 if (status->scsi_status != 0) {
699 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
700 if (status->sense_stored) {
701 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
702 ccb->csio.sense_resid =
703 ccb->csio.sense_len - status->sense_len;
704 bcopy(&ecb->sense, &ccb->csio.sense_data,
709 case HS_TARGET_NOT_ASSIGNED:
710 ccb->ccb_h.status = CAM_PATH_INVALID;
713 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
715 case HS_DATA_RUN_ERR:
716 ahbcalcresid(ahb, ecb, ccb);
718 case HS_UNEXPECTED_BUSFREE:
719 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
721 case HS_INVALID_PHASE:
722 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
724 case HS_REQUEST_SENSE_FAILED:
725 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
727 case HS_TAG_MSG_REJECTED:
729 struct ccb_trans_settings neg;
730 struct ccb_trans_settings_scsi *scsi = &neg.proto_specific.scsi;
732 xpt_print_path(ccb->ccb_h.path);
733 printf("refuses tagged commands. Performing "
735 memset(&neg, 0, sizeof (neg));
736 neg.protocol = PROTO_SCSI;
737 neg.protocol_version = SCSI_REV_2;
738 neg.transport = XPORT_SPI;
739 neg.transport_version = 2;
740 scsi->flags = CTS_SCSI_VALID_TQ;
741 xpt_setup_ccb(&neg.ccb_h, ccb->ccb_h.path, /*priority*/1);
742 xpt_async(AC_TRANSFER_NEG, ccb->ccb_h.path, &neg);
743 ahb->tags_permitted &= ~(0x01 << ccb->ccb_h.target_id);
744 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
747 case HS_FIRMWARE_LOAD_REQ:
748 case HS_HARDWARE_ERR:
750 * Tell the system that the Adapter
751 * is no longer functional.
753 ccb->ccb_h.status = CAM_NO_HBA;
755 case HS_CMD_ABORTED_HOST:
756 case HS_CMD_ABORTED_ADAPTER:
757 case HS_ATN_TARGET_FAILED:
758 case HS_SCSI_RESET_ADAPTER:
759 case HS_SCSI_RESET_INCOMING:
760 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
762 case HS_INVALID_ECB_PARAM:
763 device_printf(ahb->dev,
764 "opcode 0x%02x, flag_word1 0x%02x, flag_word2 0x%02x\n",
765 hecb->opcode, hecb->flag_word1, hecb->flag_word2);
766 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
768 case HS_DUP_TCB_RECEIVED:
769 case HS_INVALID_OPCODE:
770 case HS_INVALID_CMD_LINK:
771 case HS_PROGRAM_CKSUM_ERROR:
772 panic("%s: Can't happen host status %x occurred",
773 device_get_nameunit(ahb->dev), status->ha_status);
776 if (ccb->ccb_h.status != CAM_REQ_CMP) {
777 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
778 ccb->ccb_h.status |= CAM_DEV_QFRZN;
783 ahbdone(struct ahb_softc *ahb, u_int32_t mbox, u_int intstat)
788 ecb = ahbecbptov(ahb, mbox);
790 if ((ecb->state & ECB_ACTIVE) == 0)
791 panic("ecb not active");
796 callout_stop(&ecb->timer);
797 LIST_REMOVE(&ccb->ccb_h, sim_links.le);
799 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
802 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
803 op = BUS_DMASYNC_POSTREAD;
805 op = BUS_DMASYNC_POSTWRITE;
806 bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
807 bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
810 if ((intstat & INTSTAT_MASK) == INTSTAT_ECB_OK) {
811 ccb->ccb_h.status = CAM_REQ_CMP;
814 ahbprocesserror(ahb, ecb, ccb);
816 ahbecbfree(ahb, ecb);
819 /* Non CCB Command */
820 if ((intstat & INTSTAT_MASK) != INTSTAT_ECB_OK) {
821 device_printf(ahb->dev, "Command 0%x Failed %x:%x:%x\n",
823 *((u_int16_t*)&ecb->status),
824 ecb->status.ha_status, ecb->status.resid_count);
826 /* Client owns this ECB and will release it. */
831 * Catch an interrupt from the adaptor
836 struct ahb_softc *ahb;
839 mtx_lock(&ahb->lock);
841 mtx_unlock(&ahb->lock);
845 ahbintr_locked(struct ahb_softc *ahb)
850 while (ahb_inb(ahb, HOSTSTAT) & HOSTSTAT_INTPEND) {
852 * Fetch information about this interrupt.
854 intstat = ahb_inb(ahb, INTSTAT);
855 mbox = ahb_inl(ahb, MBOXIN0);
858 * Reset interrupt latch.
860 ahb_outb(ahb, CONTROL, CNTRL_CLRINT);
863 * Process the completed operation
865 switch (intstat & INTSTAT_MASK) {
867 case INTSTAT_ECB_CMPWRETRY:
868 case INTSTAT_ECB_CMPWERR:
869 ahbdone(ahb, mbox, intstat);
871 case INTSTAT_AEN_OCCURED:
872 if ((intstat & INTSTAT_TARGET_MASK) == ahb->scsi_id) {
874 xpt_print_path(ahb->path);
876 case HS_SCSI_RESET_ADAPTER:
877 printf("Host Adapter Initiated "
878 "Bus Reset occurred\n");
880 case HS_SCSI_RESET_INCOMING:
881 printf("Bus Reset Initiated "
882 "by another device occurred\n");
886 xpt_async(AC_BUS_RESET, ahb->path, NULL);
889 printf("Unsupported initiator selection AEN occured\n");
891 case INTSTAT_IMMED_OK:
892 case INTSTAT_IMMED_ERR:
893 ahbhandleimmed(ahb, mbox, intstat);
896 panic("Unrecoverable hardware Error Occurred\n");
902 ahbexecuteecb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
906 struct ahb_softc *ahb;
909 ecb = (struct ecb *)arg;
911 ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
912 mtx_assert(&ahb->lock, MA_OWNED);
916 device_printf(ahb->dev,
917 "Unexepected error 0x%x returned from "
918 "bus_dmamap_load\n", error);
919 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
920 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
921 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
923 ahbecbfree(ahb, ecb);
928 ecb_paddr = ahbecbvtop(ahb, ecb);
932 bus_dma_segment_t *end_seg;
935 end_seg = dm_segs + nseg;
937 /* Copy the segments into our SG list */
939 while (dm_segs < end_seg) {
940 sg->addr = dm_segs->ds_addr;
941 sg->len = dm_segs->ds_len;
947 ecb->hecb.flag_word1 |= FW1_SG_ECB;
948 ecb->hecb.data_ptr = ahbsgpaddr(ecb_paddr);
949 ecb->hecb.data_len = sizeof(ahb_sg_t) * nseg;
951 ecb->hecb.data_ptr = ecb->sg_list->addr;
952 ecb->hecb.data_len = ecb->sg_list->len;
955 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
956 /* ecb->hecb.flag_word2 |= FW2_DATA_DIR_IN; */
957 op = BUS_DMASYNC_PREREAD;
959 op = BUS_DMASYNC_PREWRITE;
961 /* ecb->hecb.flag_word2 |= FW2_CHECK_DATA_DIR; */
963 bus_dmamap_sync(ahb->buffer_dmat, ecb->dmamap, op);
966 ecb->hecb.data_ptr = 0;
967 ecb->hecb.data_len = 0;
971 * Last time we need to check if this CCB needs to
974 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
976 bus_dmamap_unload(ahb->buffer_dmat, ecb->dmamap);
977 ahbecbfree(ahb, ecb);
982 ecb->state = ECB_ACTIVE;
983 ccb->ccb_h.status |= CAM_SIM_QUEUED;
984 LIST_INSERT_HEAD(&ahb->pending_ccbs, &ccb->ccb_h, sim_links.le);
986 /* Tell the adapter about this command */
987 ahbqueuembox(ahb, ecb_paddr, ATTN_STARTECB|ccb->ccb_h.target_id);
989 callout_reset(&ecb->timer, (ccb->ccb_h.timeout * hz) / 1000, ahbtimeout,
994 ahbaction(struct cam_sim *sim, union ccb *ccb)
996 struct ahb_softc *ahb;
998 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahbaction\n"));
1000 ahb = (struct ahb_softc *)cam_sim_softc(sim);
1001 mtx_assert(&ahb->lock, MA_OWNED);
1003 switch (ccb->ccb_h.func_code) {
1004 /* Common cases first */
1005 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1008 struct hardware_ecb *hecb;
1012 * get an ecb to use.
1014 if ((ecb = ahbecbget(ahb)) == NULL) {
1015 /* Should never occur */
1016 panic("Failed to get an ecb");
1020 * So we can find the ECB when an abort is requested
1023 ccb->ccb_h.ccb_ecb_ptr = ecb;
1024 ccb->ccb_h.ccb_ahb_ptr = ahb;
1027 * Put all the arguments for the xfer in the ecb
1030 hecb->opcode = ECBOP_INITIATOR_SCSI_CMD;
1031 hecb->flag_word1 = FW1_AUTO_REQUEST_SENSE
1032 | FW1_ERR_STATUS_BLK_ONLY;
1033 hecb->flag_word2 = ccb->ccb_h.target_lun
1034 | FW2_NO_RETRY_ON_BUSY;
1035 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
1036 hecb->flag_word2 |= FW2_TAG_ENB
1037 | ((ccb->csio.tag_action & 0x3)
1038 << FW2_TAG_TYPE_SHIFT);
1040 if ((ccb->ccb_h.flags & CAM_DIS_DISCONNECT) != 0)
1041 hecb->flag_word2 |= FW2_DISABLE_DISC;
1042 hecb->sense_len = ccb->csio.sense_len;
1043 hecb->cdb_len = ccb->csio.cdb_len;
1044 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1045 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
1046 bcopy(ccb->csio.cdb_io.cdb_ptr,
1047 hecb->cdb, hecb->cdb_len);
1049 /* I guess I could map it in... */
1050 ccb->ccb_h.status = CAM_REQ_INVALID;
1051 ahbecbfree(ahb, ecb);
1056 bcopy(ccb->csio.cdb_io.cdb_bytes,
1057 hecb->cdb, hecb->cdb_len);
1060 error = bus_dmamap_load_ccb(
1066 if (error == EINPROGRESS) {
1068 * So as to maintain ordering, freeze the controller
1069 * queue until our mapping is returned.
1071 xpt_freeze_simq(ahb->sim, 1);
1072 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1076 case XPT_EN_LUN: /* Enable LUN as a target */
1077 case XPT_TARGET_IO: /* Execute target I/O request */
1078 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1079 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1080 case XPT_ABORT: /* Abort the specified CCB */
1082 ccb->ccb_h.status = CAM_REQ_INVALID;
1085 case XPT_SET_TRAN_SETTINGS:
1087 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1091 case XPT_GET_TRAN_SETTINGS:
1092 /* Get default/user set transfer settings for the target */
1094 struct ccb_trans_settings *cts = &ccb->cts;
1095 u_int target_mask = 0x01 << ccb->ccb_h.target_id;
1096 struct ccb_trans_settings_scsi *scsi =
1097 &cts->proto_specific.scsi;
1098 struct ccb_trans_settings_spi *spi =
1099 &cts->xport_specific.spi;
1101 if (cts->type == CTS_TYPE_USER_SETTINGS) {
1102 cts->protocol = PROTO_SCSI;
1103 cts->protocol_version = SCSI_REV_2;
1104 cts->transport = XPORT_SPI;
1105 cts->transport_version = 2;
1107 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1108 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1109 if ((ahb->disc_permitted & target_mask) != 0)
1110 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1111 if ((ahb->tags_permitted & target_mask) != 0)
1112 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1113 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1114 spi->sync_period = 25; /* 10MHz */
1116 if (spi->sync_period != 0)
1117 spi->sync_offset = 15;
1119 spi->valid = CTS_SPI_VALID_SYNC_RATE
1120 | CTS_SPI_VALID_SYNC_OFFSET
1121 | CTS_SPI_VALID_BUS_WIDTH
1122 | CTS_SPI_VALID_DISC;
1123 scsi->valid = CTS_SCSI_VALID_TQ;
1124 ccb->ccb_h.status = CAM_REQ_CMP;
1126 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1131 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1135 ahb->immed_cmd = IMMED_RESET;
1136 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1137 /* Poll for interrupt completion */
1138 for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--) {
1140 ahbintr_locked(cam_sim_softc(sim));
1144 case XPT_CALC_GEOMETRY:
1146 cam_calc_geometry(&ccb->ccg, ahb->extended_trans);
1150 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1154 ahb->immed_cmd = IMMED_RESET;
1155 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1156 /* Poll for interrupt completion */
1157 for (i = 1000; ahb->immed_cmd != 0 && i != 0; i--)
1159 ccb->ccb_h.status = CAM_REQ_CMP;
1163 case XPT_TERM_IO: /* Terminate the I/O process */
1165 ccb->ccb_h.status = CAM_REQ_INVALID;
1168 case XPT_PATH_INQ: /* Path routing inquiry */
1170 struct ccb_pathinq *cpi = &ccb->cpi;
1172 cpi->version_num = 1; /* XXX??? */
1173 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1174 cpi->target_sprt = 0;
1176 cpi->hba_eng_cnt = 0;
1177 cpi->max_target = 7;
1179 cpi->initiator_id = ahb->scsi_id;
1180 cpi->bus_id = cam_sim_bus(sim);
1181 cpi->base_transfer_speed = 3300;
1182 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1183 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
1184 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1185 cpi->unit_number = cam_sim_unit(sim);
1186 cpi->transport = XPORT_SPI;
1187 cpi->transport_version = 2;
1188 cpi->protocol = PROTO_SCSI;
1189 cpi->protocol_version = SCSI_REV_2;
1190 cpi->ccb_h.status = CAM_REQ_CMP;
1196 case XPT_IMMED_NOTIFY: /* Notify Host Target driver of event */
1197 case XPT_NOTIFY_ACK: /* Acknowledgement of event */
1200 ccb->ccb_h.status = CAM_REQ_INVALID;
1207 ahbpoll(struct cam_sim *sim)
1209 ahbintr(cam_sim_softc(sim));
1213 ahbtimeout(void *arg)
1217 struct ahb_softc *ahb;
1219 ecb = (struct ecb *)arg;
1221 ahb = (struct ahb_softc *)ccb->ccb_h.ccb_ahb_ptr;
1222 mtx_assert(&ahb->lock, MA_OWNED);
1223 xpt_print_path(ccb->ccb_h.path);
1224 printf("ECB %p - timed out\n", (void *)ecb);
1226 if ((ecb->state & ECB_ACTIVE) == 0) {
1227 xpt_print_path(ccb->ccb_h.path);
1228 printf("ECB %p - timed out ECB already completed\n",
1233 * In order to simplify the recovery process, we ask the XPT
1234 * layer to halt the queue of new transactions and we traverse
1235 * the list of pending CCBs and remove their timeouts. This
1236 * means that the driver attempts to clear only one error
1237 * condition at a time. In general, timeouts that occur
1238 * close together are related anyway, so there is no benefit
1239 * in attempting to handle errors in parrallel. Timeouts will
1240 * be reinstated when the recovery process ends.
1242 if ((ecb->state & ECB_DEVICE_RESET) == 0) {
1243 struct ccb_hdr *ccb_h;
1245 if ((ecb->state & ECB_RELEASE_SIMQ) == 0) {
1246 xpt_freeze_simq(ahb->sim, /*count*/1);
1247 ecb->state |= ECB_RELEASE_SIMQ;
1250 LIST_FOREACH(ccb_h, &ahb->pending_ccbs, sim_links.le) {
1251 struct ecb *pending_ecb;
1253 pending_ecb = (struct ecb *)ccb_h->ccb_ecb_ptr;
1254 callout_stop(&pending_ecb->timer);
1257 /* Store for our interrupt handler */
1258 ahb->immed_ecb = ecb;
1261 * Send a Bus Device Reset message:
1262 * The target that is holding up the bus may not
1263 * be the same as the one that triggered this timeout
1264 * (different commands have different timeout lengths),
1265 * but we have no way of determining this from our
1266 * timeout handler. Our strategy here is to queue a
1267 * BDR message to the target of the timed out command.
1268 * If this fails, we'll get another timeout 2 seconds
1269 * later which will attempt a bus reset.
1271 xpt_print_path(ccb->ccb_h.path);
1272 printf("Queuing BDR\n");
1273 ecb->state |= ECB_DEVICE_RESET;
1274 callout_reset(&ecb->timer, 2 * hz, ahbtimeout, ecb);
1276 ahb->immed_cmd = IMMED_RESET;
1277 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ccb->ccb_h.target_id);
1278 } else if ((ecb->state & ECB_SCSIBUS_RESET) != 0) {
1280 * Try a SCSI bus reset. We do this only if we
1281 * have already attempted to clear the condition with a BDR.
1283 xpt_print_path(ccb->ccb_h.path);
1284 printf("Attempting SCSI Bus reset\n");
1285 ecb->state |= ECB_SCSIBUS_RESET;
1286 callout_reset(&ecb->timer, 2 * hz, ahbtimeout, ecb);
1287 ahb->immed_cmd = IMMED_RESET;
1288 ahbqueuembox(ahb, IMMED_RESET, ATTN_IMMED|ahb->scsi_id);
1290 /* Bring out the hammer... */
1293 /* Simulate the reset complete interrupt */
1294 ahbhandleimmed(ahb, 0, ahb->scsi_id|INTSTAT_IMMED_OK);
1298 static device_method_t ahb_eisa_methods[] = {
1299 /* Device interface */
1300 DEVMETHOD(device_probe, ahbprobe),
1301 DEVMETHOD(device_attach, ahbattach),
1306 static driver_t ahb_eisa_driver = {
1309 sizeof(struct ahb_softc),
1312 static devclass_t ahb_devclass;
1314 DRIVER_MODULE(ahb, eisa, ahb_eisa_driver, ahb_devclass, 0, 0);
1315 MODULE_DEPEND(ahb, eisa, 1, 1, 1);
1316 MODULE_DEPEND(ahb, cam, 1, 1, 1);