2 * Copyright (c) 1997 by Simon Shapiro
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * dpt_scsi.c: SCSI dependant code for the DPT driver
33 * credits: Assisted by Mike Neuffer in the early low level DPT code
34 * Thanx to Mark Salyzyn of DPT for his assistance.
35 * Special thanx to Justin Gibbs for invaluable help in
36 * making this driver look and work like a FreeBSD component.
37 * Last but not least, many thanx to UCB and the FreeBSD
38 * team for creating and maintaining such a wonderful O/S.
40 * TODO: * Add ISA probe code.
41 * * Add driver-level RAID-0. This will allow interoperability with
42 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
43 * arrays that span controllers (Wow!).
46 #ident "$FreeBSD: src/sys/dev/dpt/dpt_scsi.c,v 1.28.2.3 2003/01/31 02:47:10 grog Exp $"
47 #ident "$DragonFly: src/sys/dev/raid/dpt/dpt_scsi.c,v 1.5 2004/03/15 03:05:10 dillon Exp $"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/eventhandler.h>
55 #include <sys/malloc.h>
56 #include <sys/kernel.h>
60 #include <machine/bus_memio.h>
61 #include <machine/bus_pio.h>
62 #include <machine/bus.h>
64 #include <machine/clock.h>
66 #include <bus/cam/cam.h>
67 #include <bus/cam/cam_ccb.h>
68 #include <bus/cam/cam_sim.h>
69 #include <bus/cam/cam_xpt_sim.h>
70 #include <bus/cam/cam_debug.h>
71 #include <bus/cam/scsi/scsi_all.h>
72 #include <bus/cam/scsi/scsi_message.h>
79 /* dpt_isa.c, dpt_eisa.c, and dpt_pci.c need this in a central place */
80 int dpt_controllers_present;
82 u_long dpt_unit; /* Next unit number to use */
84 /* The linked list of softc structures */
85 struct dpt_softc_list dpt_softcs = TAILQ_HEAD_INITIALIZER(dpt_softcs);
87 #define microtime_now dpt_time_now()
89 #define dpt_inl(dpt, port) \
90 bus_space_read_4((dpt)->tag, (dpt)->bsh, port)
91 #define dpt_inb(dpt, port) \
92 bus_space_read_1((dpt)->tag, (dpt)->bsh, port)
93 #define dpt_outl(dpt, port, value) \
94 bus_space_write_4((dpt)->tag, (dpt)->bsh, port, value)
95 #define dpt_outb(dpt, port, value) \
96 bus_space_write_1((dpt)->tag, (dpt)->bsh, port, value)
99 * These will have to be setup by parameters passed at boot/load time. For
100 * perfromance reasons, we make them constants for the time being.
102 #define dpt_min_segs DPT_MAX_SEGS
103 #define dpt_max_segs DPT_MAX_SEGS
105 /* Definitions for our use of the SIM private CCB area */
106 #define ccb_dccb_ptr spriv_ptr0
107 #define ccb_dpt_ptr spriv_ptr1
109 /* ================= Private Inline Function declarations ===================*/
110 static __inline int dpt_just_reset(dpt_softc_t * dpt);
111 static __inline int dpt_raid_busy(dpt_softc_t * dpt);
112 static __inline int dpt_pio_wait (u_int32_t, u_int, u_int, u_int);
113 static __inline int dpt_wait(dpt_softc_t *dpt, u_int bits,
115 static __inline struct dpt_ccb* dptgetccb(struct dpt_softc *dpt);
116 static __inline void dptfreeccb(struct dpt_softc *dpt,
117 struct dpt_ccb *dccb);
118 static __inline u_int32_t dptccbvtop(struct dpt_softc *dpt,
119 struct dpt_ccb *dccb);
121 static __inline int dpt_send_immediate(dpt_softc_t *dpt,
122 eata_ccb_t *cmd_block,
123 u_int32_t cmd_busaddr,
125 u_int ifc, u_int code,
128 /* ==================== Private Function declarations =======================*/
129 static void dptmapmem(void *arg, bus_dma_segment_t *segs,
130 int nseg, int error);
132 static struct sg_map_node*
133 dptallocsgmap(struct dpt_softc *dpt);
135 static int dptallocccbs(dpt_softc_t *dpt);
137 static int dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb,
138 u_int32_t dccb_busaddr, u_int size,
139 u_int page, u_int target, int extent);
140 static void dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb,
141 u_int32_t dccb_busaddr,
144 static void dpt_poll(struct cam_sim *sim);
146 static void dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
147 int nseg, int error);
149 static void dpt_action(struct cam_sim *sim, union ccb *ccb);
151 static int dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd,
152 u_int32_t cmd_busaddr,
153 u_int command, u_int retries,
154 u_int ifc, u_int code,
156 static void dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb,
157 union ccb *ccb, u_int hba_stat,
158 u_int scsi_stat, u_int32_t resid);
160 static void dpttimeout(void *arg);
161 static void dptshutdown(void *arg, int howto);
163 /* ================= Private Inline Function definitions ====================*/
165 dpt_just_reset(dpt_softc_t * dpt)
167 if ((dpt_inb(dpt, 2) == 'D')
168 && (dpt_inb(dpt, 3) == 'P')
169 && (dpt_inb(dpt, 4) == 'T')
170 && (dpt_inb(dpt, 5) == 'H'))
177 dpt_raid_busy(dpt_softc_t * dpt)
179 if ((dpt_inb(dpt, 0) == 'D')
180 && (dpt_inb(dpt, 1) == 'P')
181 && (dpt_inb(dpt, 2) == 'T'))
188 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
193 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
194 c = inb(base + reg) & bits;
204 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
209 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
210 c = dpt_inb(dpt, HA_RSTATUS) & bits;
219 static __inline struct dpt_ccb*
220 dptgetccb(struct dpt_softc *dpt)
222 struct dpt_ccb* dccb;
226 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
227 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
229 } else if (dpt->total_dccbs < dpt->max_dccbs) {
231 dccb = SLIST_FIRST(&dpt->free_dccb_list);
233 printf("dpt%d: Can't malloc DCCB\n", dpt->unit);
235 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
245 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
250 if ((dccb->state & DCCB_ACTIVE) != 0)
251 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
252 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
253 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
254 else if (dpt->resource_shortage != 0
255 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
256 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
257 dpt->resource_shortage = FALSE;
259 dccb->state = DCCB_FREE;
260 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
265 static __inline u_int32_t
266 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
268 return (dpt->dpt_ccb_busbase
269 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
272 static __inline struct dpt_ccb *
273 dptccbptov(struct dpt_softc *dpt, u_int32_t busaddr)
275 return (dpt->dpt_dccbs
276 + ((struct dpt_ccb *)busaddr
277 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
281 * Send a command for immediate execution by the DPT
282 * See above function for IMPORTANT notes.
285 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
286 u_int32_t cmd_busaddr, u_int retries,
287 u_int ifc, u_int code, u_int code2)
289 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
290 EATA_CMD_IMMEDIATE, retries, ifc,
295 /* ===================== Private Function definitions =======================*/
297 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
299 bus_addr_t *busaddrp;
301 busaddrp = (bus_addr_t *)arg;
302 *busaddrp = segs->ds_addr;
305 static struct sg_map_node *
306 dptallocsgmap(struct dpt_softc *dpt)
308 struct sg_map_node *sg_map;
310 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
315 /* Allocate S/G space for the next batch of CCBS */
316 if (bus_dmamem_alloc(dpt->sg_dmat, (void **)&sg_map->sg_vaddr,
317 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
318 free(sg_map, M_DEVBUF);
322 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
323 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
326 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
332 * Allocate another chunk of CCB's. Return count of entries added.
333 * Assumed to be called at splcam().
336 dptallocccbs(dpt_softc_t *dpt)
338 struct dpt_ccb *next_ccb;
339 struct sg_map_node *sg_map;
345 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
347 if (next_ccb == dpt->dpt_dccbs) {
349 * First time through. Re-use the S/G
350 * space we allocated for initialization
353 sg_map = SLIST_FIRST(&dpt->sg_maps);
355 sg_map = dptallocsgmap(dpt);
361 segs = sg_map->sg_vaddr;
362 physaddr = sg_map->sg_physaddr;
364 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
365 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
368 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
372 next_ccb->sg_list = segs;
373 next_ccb->sg_busaddr = htonl(physaddr);
374 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
375 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
376 next_ccb->eata_ccb.cp_reqDMA =
377 htonl(dptccbvtop(dpt, next_ccb)
378 + offsetof(struct dpt_ccb, sense_data));
379 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
380 next_ccb->state = DCCB_FREE;
381 next_ccb->tag = dpt->total_dccbs;
382 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
384 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
385 dpt->dpt_ccb_busend += sizeof(*next_ccb);
393 dpt_pio_get_conf (u_int32_t base)
395 static dpt_conf_t * conf;
400 * Allocate a dpt_conf_t
403 conf = (dpt_conf_t *)malloc(sizeof(dpt_conf_t),
408 * If we didn't get one then we probably won't ever get one.
411 printf("dpt: unable to allocate dpt_conf_t\n");
416 * If we have one, clean it up.
418 bzero(conf, sizeof(dpt_conf_t));
421 * Reset the controller.
423 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
426 * Wait for the controller to become ready.
427 * For some reason there can be -no- delays after calling reset
428 * before we wait on ready status.
430 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
431 printf("dpt: timeout waiting for controller to become ready\n");
435 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
436 printf("dpt: timetout waiting for adapter ready.\n");
441 * Send the PIO_READ_CONFIG command.
443 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
446 * Read the data into the struct.
448 p = (u_int16_t *)conf;
449 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
451 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
452 printf("dpt: timeout in data read.\n");
456 (*p) = inw(base + HA_RDATA);
460 if (inb(base + HA_RSTATUS) & HA_SERROR) {
461 printf("dpt: error reading configuration data.\n");
465 #define BE_EATA_SIGNATURE 0x45415441
466 #define LE_EATA_SIGNATURE 0x41544145
469 * Test to see if we have a valid card.
471 if ((conf->signature == BE_EATA_SIGNATURE) ||
472 (conf->signature == LE_EATA_SIGNATURE)) {
474 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
475 inw(base + HA_RDATA);
484 * Read a configuration page into the supplied dpt_cont_t buffer.
487 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
488 u_int size, u_int page, u_int target, int extent)
498 cp = &dccb->eata_ccb;
499 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
503 cp->Auto_Req_Sen = 1;
504 cp->reqlen = sizeof(struct scsi_sense_data);
507 cp->cp_LUN = 0; /* In the EATA packet */
508 cp->cp_lun = 0; /* In the SCSI command */
510 cp->cp_scsi_cmd = INQUIRY;
513 cp->cp_extent = extent;
516 cp->cp_channel = 0; /* DNC, Interpret mode is set */
518 cp->cp_datalen = htonl(size);
523 * This could be a simple for loop, but we suspected the compiler To
524 * have optimized it a bit too much. Wait for the controller to
527 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
528 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
529 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
530 || (dpt_wait(dpt, HA_SBUSY, 0))) {
533 * RAID Drives still Spinning up? (This should only occur if
534 * the DPT controller is in a NON PC (PCI?) platform).
536 if (dpt_raid_busy(dpt)) {
537 printf("dpt%d WARNING: Get_conf() RSUS failed.\n",
544 DptStat_Reset_BUSY(dpt->sp);
547 * XXXX We might want to do something more clever than aborting at
548 * this point, like resetting (rebooting) the controller and trying
551 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
552 EATA_CMD_DMA_SEND_CP,
553 10000, 0, 0, 0)) != 0) {
554 printf("dpt%d WARNING: Get_conf() failed (%d) to send "
555 "EATA_CMD_DMA_READ_CONFIG\n",
560 /* Wait for two seconds for a response. This can be slow */
563 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
568 /* Grab the status and clear interrupts */
569 status = dpt_inb(dpt, HA_RSTATUS);
574 * Check the status carefully. Return only if the
575 * command was successful.
577 if (((status & HA_SERROR) == 0)
578 && (dpt->sp->hba_stat == 0)
579 && (dpt->sp->scsi_stat == 0)
580 && (dpt->sp->residue_len == 0))
583 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
589 /* Detect Cache parameters and size */
591 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
603 * Default setting, for best perfromance..
604 * This is what virtually all cards default to..
606 dpt->cache_type = DPT_CACHE_WRITEBACK;
609 cp = &dccb->eata_ccb;
610 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
613 /* Setup the command structure */
616 cp->Auto_Req_Sen = 1;
617 cp->reqlen = sizeof(struct scsi_sense_data);
619 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
620 cp->cp_LUN = 0; /* In the EATA packet */
621 cp->cp_lun = 0; /* In the SCSI command */
624 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
633 * Build the EATA Command Packet structure
634 * for a Log Sense Command.
636 cp->cp_cdb[0] = 0x4d;
638 cp->cp_cdb[2] = 0x40 | 0x33;
641 cp->cp_datalen = htonl(512);
644 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
645 EATA_CMD_DMA_SEND_CP,
648 printf("dpt%d WARNING: detect_cache() failed (%d) to send "
649 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
653 /* Wait for two seconds for a response. This can be slow... */
656 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
661 /* Grab the status and clear interrupts */
662 status = dpt_inb(dpt, HA_RSTATUS);
668 if (buff[0] != 0x33) {
671 bytes = DPT_HCP_LENGTH(buff);
672 param = DPT_HCP_FIRST(buff);
674 if (DPT_HCP_CODE(param) != 1) {
676 * DPT Log Page layout error
678 printf("dpt%d: NOTICE: Log Page (1) layout error\n",
682 if (!(param[4] & 0x4)) {
683 dpt->cache_type = DPT_NO_CACHE;
686 while (DPT_HCP_CODE(param) != 6) {
687 param = DPT_HCP_NEXT(param);
689 || (param >= &buff[bytes])) {
694 if (param[4] & 0x2) {
698 dpt->cache_type = DPT_NO_CACHE;
702 if (param[4] & 0x4) {
703 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
706 /* XXX This isn't correct. This log parameter only has two bytes.... */
708 dpt->cache_size = param[5]
716 dpt_poll(struct cam_sim *sim)
718 dpt_intr(cam_sim_softc(sim));
722 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
724 struct dpt_ccb *dccb;
726 struct dpt_softc *dpt;
729 dccb = (struct dpt_ccb *)arg;
731 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
735 printf("dpt%d: Unexepected error 0x%x returned from "
736 "bus_dmamap_load\n", dpt->unit, error);
737 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
738 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
739 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
741 dptfreeccb(dpt, dccb);
748 bus_dma_segment_t *end_seg;
751 end_seg = dm_segs + nseg;
753 /* Copy the segments into our SG list */
755 while (dm_segs < end_seg) {
756 sg->seg_len = htonl(dm_segs->ds_len);
757 sg->seg_addr = htonl(dm_segs->ds_addr);
763 dccb->eata_ccb.scatter = 1;
764 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
765 dccb->eata_ccb.cp_datalen =
766 htonl(nseg * sizeof(dpt_sg_t));
768 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
769 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
772 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
773 op = BUS_DMASYNC_PREREAD;
775 op = BUS_DMASYNC_PREWRITE;
777 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
780 dccb->eata_ccb.cp_dataDMA = 0;
781 dccb->eata_ccb.cp_datalen = 0;
787 * Last time we need to check if this CCB needs to
790 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
792 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
793 dptfreeccb(dpt, dccb);
799 dccb->state |= DCCB_ACTIVE;
800 ccb->ccb_h.status |= CAM_SIM_QUEUED;
801 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
802 ccb->ccb_h.timeout_ch =
803 timeout(dpttimeout, (caddr_t)dccb,
804 (ccb->ccb_h.timeout * hz) / 1000);
805 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
806 dccb->eata_ccb.cp_busaddr,
807 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
808 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
810 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
811 dptfreeccb(dpt, dccb);
819 dpt_action(struct cam_sim *sim, union ccb *ccb)
821 struct dpt_softc *dpt;
823 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
825 dpt = (struct dpt_softc *)cam_sim_softc(sim);
827 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
828 xpt_print_path(ccb->ccb_h.path);
829 printf("controller is shutdown. Aborting CCB.\n");
830 ccb->ccb_h.status = CAM_NO_HBA;
835 switch (ccb->ccb_h.func_code) {
836 /* Common cases first */
837 case XPT_SCSI_IO: /* Execute the requested I/O operation */
839 struct ccb_scsiio *csio;
840 struct ccb_hdr *ccbh;
841 struct dpt_ccb *dccb;
842 struct eata_ccb *eccb;
846 /* Max CDB length is 12 bytes */
847 if (csio->cdb_len > 12) {
848 ccb->ccb_h.status = CAM_REQ_INVALID;
852 if ((dccb = dptgetccb(dpt)) == NULL) {
856 dpt->resource_shortage = 1;
858 xpt_freeze_simq(sim, /*count*/1);
859 ccb->ccb_h.status = CAM_REQUEUE_REQ;
863 eccb = &dccb->eata_ccb;
865 /* Link dccb and ccb so we can find one from the other */
867 ccb->ccb_h.ccb_dccb_ptr = dccb;
868 ccb->ccb_h.ccb_dpt_ptr = dpt;
871 * Explicitly set all flags so that the compiler can
872 * be smart about setting them.
874 eccb->SCSI_Reset = 0;
876 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
881 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
883 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
884 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
885 eccb->reqlen = csio->sense_len;
886 eccb->cp_id = ccb->ccb_h.target_id;
887 eccb->cp_channel = cam_sim_bus(sim);
888 eccb->cp_LUN = ccb->ccb_h.target_lun;
890 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
892 eccb->cp_identify = 1;
894 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
895 && csio->tag_action != CAM_TAG_ACTION_NONE) {
896 eccb->cp_msg[0] = csio->tag_action;
897 eccb->cp_msg[1] = dccb->tag;
904 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
905 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
906 bcopy(csio->cdb_io.cdb_ptr,
907 eccb->cp_cdb, csio->cdb_len);
909 /* I guess I could map it in... */
910 ccb->ccb_h.status = CAM_REQ_INVALID;
911 dptfreeccb(dpt, dccb);
916 bcopy(csio->cdb_io.cdb_bytes,
917 eccb->cp_cdb, csio->cdb_len);
920 * If we have any data to send with this command,
921 * map it into bus space.
923 /* Only use S/G if there is a transfer */
924 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
925 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
927 * We've been given a pointer
928 * to a single buffer.
930 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
936 bus_dmamap_load(dpt->buffer_dmat,
942 if (error == EINPROGRESS) {
944 * So as to maintain ordering,
945 * freeze the controller queue
946 * until our mapping is
949 xpt_freeze_simq(sim, 1);
950 dccb->state |= CAM_RELEASE_SIMQ;
954 struct bus_dma_segment seg;
956 /* Pointer to physical buffer */
958 (bus_addr_t)csio->data_ptr;
959 seg.ds_len = csio->dxfer_len;
960 dptexecuteccb(dccb, &seg, 1, 0);
963 struct bus_dma_segment *segs;
965 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
966 panic("dpt_action - Physical "
970 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
971 panic("dpt_action - Virtual "
975 /* Just use the segments provided */
976 segs = (struct bus_dma_segment *)csio->data_ptr;
977 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
982 * Does it want them both on or both off?
983 * CAM_DIR_NONE is both on, so this code can
984 * be removed if this is also what the DPT
989 dptexecuteccb(dccb, NULL, 0, 0);
993 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
994 case XPT_ABORT: /* Abort the specified CCB */
996 ccb->ccb_h.status = CAM_REQ_INVALID;
999 case XPT_SET_TRAN_SETTINGS:
1001 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1005 case XPT_GET_TRAN_SETTINGS:
1006 /* Get default/user set transfer settings for the target */
1008 struct ccb_trans_settings *cts;
1012 target_mask = 0x01 << ccb->ccb_h.target_id;
1013 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1014 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
1015 cts->bus_width = (dpt->max_id > 7)
1016 ? MSG_EXT_WDTR_BUS_8_BIT
1017 : MSG_EXT_WDTR_BUS_16_BIT;
1018 cts->sync_period = 25; /* 10MHz */
1020 if (cts->sync_period != 0)
1021 cts->sync_offset = 15;
1023 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1024 | CCB_TRANS_SYNC_OFFSET_VALID
1025 | CCB_TRANS_BUS_WIDTH_VALID
1026 | CCB_TRANS_DISC_VALID
1027 | CCB_TRANS_TQ_VALID;
1028 ccb->ccb_h.status = CAM_REQ_CMP;
1030 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1035 case XPT_CALC_GEOMETRY:
1037 struct ccb_calc_geometry *ccg;
1039 u_int32_t secs_per_cylinder;
1043 * XXX Use Adaptec translation until I find out how to
1044 * get this information from the card.
1047 size_mb = ccg->volume_size
1048 / ((1024L * 1024L) / ccg->block_size);
1051 if (size_mb > 1024 && extended) {
1053 ccg->secs_per_track = 63;
1056 ccg->secs_per_track = 32;
1058 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1059 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1060 ccb->ccb_h.status = CAM_REQ_CMP;
1064 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1067 ccb->ccb_h.status = CAM_REQ_CMP;
1071 case XPT_TERM_IO: /* Terminate the I/O process */
1073 ccb->ccb_h.status = CAM_REQ_INVALID;
1076 case XPT_PATH_INQ: /* Path routing inquiry */
1078 struct ccb_pathinq *cpi = &ccb->cpi;
1080 cpi->version_num = 1;
1081 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1082 if (dpt->max_id > 7)
1083 cpi->hba_inquiry |= PI_WIDE_16;
1084 cpi->target_sprt = 0;
1086 cpi->hba_eng_cnt = 0;
1087 cpi->max_target = dpt->max_id;
1088 cpi->max_lun = dpt->max_lun;
1089 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1090 cpi->bus_id = cam_sim_bus(sim);
1091 cpi->base_transfer_speed = 3300;
1092 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1093 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1094 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1095 cpi->unit_number = cam_sim_unit(sim);
1096 cpi->ccb_h.status = CAM_REQ_CMP;
1101 ccb->ccb_h.status = CAM_REQ_INVALID;
1108 * This routine will try to send an EATA command to the DPT HBA.
1109 * It will, by default, try 20,000 times, waiting 50us between tries.
1110 * It returns 0 on success and 1 on failure.
1111 * It is assumed to be called at splcam().
1114 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1115 u_int32_t cmd_busaddr, u_int command, u_int retries,
1116 u_int ifc, u_int code, u_int code2)
1124 * I hate this polling nonsense. Wish there was a way to tell the DPT
1125 * to go get commands at its own pace, or to interrupt when ready.
1126 * In the mean time we will measure how many itterations it really
1129 for (loop = 0; loop < retries; loop++) {
1130 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1136 if (loop < retries) {
1137 #ifdef DPT_MEASURE_PERFORMANCE
1138 if (loop > dpt->performance.max_eata_tries)
1139 dpt->performance.max_eata_tries = loop;
1141 if (loop < dpt->performance.min_eata_tries)
1142 dpt->performance.min_eata_tries = loop;
1145 #ifdef DPT_MEASURE_PERFORMANCE
1146 ++dpt->performance.command_too_busy;
1151 /* The controller is alive, advance the wedge timer */
1152 #ifdef DPT_RESET_HBA
1153 dpt->last_contact = microtime_now;
1156 if (cmd_block == NULL)
1158 #if (BYTE_ORDER == BIG_ENDIAN)
1160 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1161 | ((cmd_busaddr >> 16) & 0xFF)
1162 | ((cmd_busaddr >> 8) & 0xFF)
1163 | (cmd_busaddr & 0xFF);
1166 /* And now the address */
1167 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1169 if (command == EATA_CMD_IMMEDIATE) {
1170 if (cmd_block == NULL) {
1171 dpt_outb(dpt, HA_WCODE2, code2);
1172 dpt_outb(dpt, HA_WCODE, code);
1174 dpt_outb(dpt, HA_WIFC, ifc);
1176 dpt_outb(dpt, HA_WCOMMAND, command);
1182 /* ==================== Exported Function definitions =======================*/
1184 dpt_alloc(device_t dev, bus_space_tag_t tag, bus_space_handle_t bsh)
1186 dpt_softc_t *dpt = device_get_softc(dev);
1189 bzero(dpt, sizeof(dpt_softc_t));
1192 dpt->unit = device_get_unit(dev);
1193 SLIST_INIT(&dpt->free_dccb_list);
1194 LIST_INIT(&dpt->pending_ccb_list);
1195 TAILQ_INSERT_TAIL(&dpt_softcs, dpt, links);
1196 for (i = 0; i < MAX_CHANNELS; i++)
1197 dpt->resetlevel[i] = DPT_HA_OK;
1199 #ifdef DPT_MEASURE_PERFORMANCE
1200 dpt_reset_performance(dpt);
1201 #endif /* DPT_MEASURE_PERFORMANCE */
1206 dpt_free(struct dpt_softc *dpt)
1208 switch (dpt->init_level) {
1211 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1213 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1215 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1217 bus_dma_tag_destroy(dpt->dccb_dmat);
1219 bus_dma_tag_destroy(dpt->buffer_dmat);
1222 struct sg_map_node *sg_map;
1224 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1225 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1226 bus_dmamap_unload(dpt->sg_dmat,
1228 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1230 free(sg_map, M_DEVBUF);
1232 bus_dma_tag_destroy(dpt->sg_dmat);
1237 TAILQ_REMOVE(&dpt_softcs, dpt, links);
1240 static u_int8_t string_sizes[] =
1242 sizeof(((dpt_inq_t*)NULL)->vendor),
1243 sizeof(((dpt_inq_t*)NULL)->modelNum),
1244 sizeof(((dpt_inq_t*)NULL)->firmware),
1245 sizeof(((dpt_inq_t*)NULL)->protocol),
1249 dpt_init(struct dpt_softc *dpt)
1252 struct sg_map_node *sg_map;
1259 dpt->init_level = 0;
1260 SLIST_INIT(&dpt->sg_maps);
1262 #ifdef DPT_RESET_BOARD
1263 printf("dpt%d: resetting HBA\n", dpt->unit);
1264 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1266 /* XXX Shouldn't we poll a status register or something??? */
1268 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1269 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1270 /*lowaddr*/BUS_SPACE_MAXADDR,
1271 /*highaddr*/BUS_SPACE_MAXADDR,
1272 /*filter*/NULL, /*filterarg*/NULL,
1273 PAGE_SIZE, /*nsegments*/1,
1274 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1275 /*flags*/0, &dpt->sg_dmat) != 0) {
1282 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1283 * memory. To get the allocation size, we need to know how many
1284 * ccbs the card supports. This requires a ccb. We solve this
1285 * chicken and egg problem by allocating some re-usable S/G space
1286 * up front, and treating it as our status packet, CCB, and target
1287 * memory space for these commands.
1289 sg_map = dptallocsgmap(dpt);
1293 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1294 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1295 bzero(dccb, sizeof(*dccb));
1296 dpt->sp_physaddr = sg_map->sg_physaddr;
1297 dccb->eata_ccb.cp_dataDMA =
1298 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1299 dccb->eata_ccb.cp_busaddr = ~0;
1300 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1301 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1302 + offsetof(struct dpt_ccb, sense_data));
1304 /* Okay. Fetch our config */
1305 bzero(&dccb[1], sizeof(conf)); /* data area */
1306 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1307 sizeof(conf), 0xc1, 7, 1);
1310 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1313 bcopy(&dccb[1], &conf, sizeof(conf));
1315 bzero(&dccb[1], sizeof(dpt->board_data));
1316 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1317 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1319 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1322 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1324 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1325 (u_int8_t *)&dccb[1]);
1327 switch (ntohl(conf.splen)) {
1329 dpt->EATA_revision = 'a';
1332 dpt->EATA_revision = 'b';
1335 dpt->EATA_revision = 'c';
1338 dpt->EATA_revision = 'z';
1341 dpt->EATA_revision = '?';
1344 dpt->max_id = conf.MAX_ID;
1345 dpt->max_lun = conf.MAX_LUN;
1346 dpt->irq = conf.IRQ;
1347 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1348 dpt->channels = conf.MAX_CHAN + 1;
1349 dpt->state |= DPT_HA_OK;
1351 dpt->primary = FALSE;
1353 dpt->primary = TRUE;
1355 dpt->more_support = conf.MORE_support;
1357 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1358 dpt->immediate_support = 1;
1360 dpt->immediate_support = 0;
1362 dpt->broken_INQUIRY = FALSE;
1364 dpt->cplen = ntohl(conf.cplen);
1365 dpt->cppadlen = ntohs(conf.cppadlen);
1366 dpt->max_dccbs = ntohs(conf.queuesiz);
1368 if (dpt->max_dccbs > 256) {
1369 printf("dpt%d: Max CCBs reduced from %d to "
1370 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1371 dpt->max_dccbs = 256;
1374 dpt->hostid[0] = conf.scsi_id0;
1375 dpt->hostid[1] = conf.scsi_id1;
1376 dpt->hostid[2] = conf.scsi_id2;
1381 dpt->sgsize = ntohs(conf.SGsiz);
1383 /* We can only get 64k buffers, so don't bother to waste space. */
1384 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1387 if (dpt->sgsize > dpt_max_segs)
1388 dpt->sgsize = dpt_max_segs;
1390 /* DMA tag for mapping buffers into device visible space. */
1391 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1392 /*lowaddr*/BUS_SPACE_MAXADDR,
1393 /*highaddr*/BUS_SPACE_MAXADDR,
1394 /*filter*/NULL, /*filterarg*/NULL,
1395 /*maxsize*/MAXBSIZE, /*nsegments*/dpt->sgsize,
1396 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1397 /*flags*/BUS_DMA_ALLOCNOW,
1398 &dpt->buffer_dmat) != 0) {
1399 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1405 /* DMA tag for our ccb structures and interrupt status packet */
1406 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1407 /*lowaddr*/BUS_SPACE_MAXADDR,
1408 /*highaddr*/BUS_SPACE_MAXADDR,
1409 /*filter*/NULL, /*filterarg*/NULL,
1410 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1413 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1414 /*flags*/0, &dpt->dccb_dmat) != 0) {
1415 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1421 /* Allocation for our ccbs and interrupt status packet */
1422 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1423 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1424 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1430 /* And permanently map them */
1431 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1433 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1435 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1437 /* Clear them out. */
1438 bzero(dpt->dpt_dccbs,
1439 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1441 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1443 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1444 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1445 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1448 /* Allocate our first batch of ccbs */
1449 if (dptallocccbs(dpt) == 0) {
1450 printf("dpt: dptallocccbs(dpt) == 0\n");
1454 /* Prepare for Target Mode */
1455 dpt->target_mode_enabled = 1;
1457 /* Nuke excess spaces from inquiry information */
1458 strp = dpt->board_data.vendor;
1459 for (i = 0; i < sizeof(string_sizes); i++) {
1460 index = string_sizes[i] - 1;
1461 while (index && (strp[index] == ' '))
1462 strp[index--] = '\0';
1463 strp += string_sizes[i];
1466 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1467 dpt->unit, dpt->board_data.vendor,
1468 dpt->board_data.modelNum, dpt->board_data.firmware);
1470 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1472 if (dpt->cache_type != DPT_NO_CACHE
1473 && dpt->cache_size != 0) {
1474 printf("%s Cache, ",
1475 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1476 ? "Write-Through" : "Write-Back");
1479 printf("%d CCBs\n", dpt->max_dccbs);
1487 dpt_attach(dpt_softc_t *dpt)
1489 struct cam_devq *devq;
1493 * Create the device queue for our SIM.
1495 devq = cam_simq_alloc(dpt->max_dccbs);
1499 for (i = 0; i < dpt->channels; i++) {
1501 * Construct our SIM entry
1503 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1504 dpt, dpt->unit, /*untagged*/2,
1505 /*tagged*/dpt->max_dccbs, devq);
1506 if (xpt_bus_register(dpt->sims[i], i) != CAM_SUCCESS) {
1507 cam_sim_free(dpt->sims[i]);
1511 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1512 cam_sim_path(dpt->sims[i]),
1513 CAM_TARGET_WILDCARD,
1514 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1515 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1516 cam_sim_free(dpt->sims[i]);
1521 cam_simq_release(devq);
1523 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1524 dpt, SHUTDOWN_PRI_DEFAULT);
1530 * This is the interrupt handler for the DPT driver.
1542 u_int32_t residue_len; /* Number of bytes not transferred */
1544 dpt = (dpt_softc_t *)arg;
1546 /* First order of business is to check if this interrupt is for us */
1547 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1550 * What we want to do now, is to capture the status, all of it,
1551 * move it where it belongs, wake up whoever sleeps waiting to
1552 * process this result, and get out of here.
1554 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1555 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1556 printf("Encountered bogus status packet\n");
1557 status = dpt_inb(dpt, HA_RSTATUS);
1561 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1563 dpt->sp->ccb_busaddr = ~0;
1565 /* Ignore status packets with EOC not set */
1566 if (dpt->sp->EOC == 0) {
1567 printf("dpt%d ERROR: Request %d received with "
1568 "clear EOC.\n Marking as LOST.\n",
1569 dpt->unit, dccb->transaction_id);
1571 #ifdef DPT_HANDLE_TIMEOUTS
1572 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1574 /* This CLEARS the interrupt! */
1575 status = dpt_inb(dpt, HA_RSTATUS);
1581 * Double buffer the status information so the hardware can
1582 * work on updating the status packet while we decifer the
1583 * one we were just interrupted for.
1584 * According to Mark Salyzyn, we only need few pieces of it.
1586 hba_stat = dpt->sp->hba_stat;
1587 scsi_stat = dpt->sp->scsi_stat;
1588 residue_len = dpt->sp->residue_len;
1590 /* Clear interrupts, check for error */
1591 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1593 * Error Condition. Check for magic cookie. Exit
1594 * this test on earliest sign of non-reset condition
1597 /* Check that this is not a board reset interrupt */
1598 if (dpt_just_reset(dpt)) {
1599 printf("dpt%d: HBA rebooted.\n"
1600 " All transactions should be "
1604 printf("dpt%d: >>---->> This is incomplete, "
1605 "fix me.... <<----<<", dpt->unit);
1606 panic("DPT Rebooted");
1612 untimeout(dpttimeout, dccb, ccb->ccb_h.timeout_ch);
1613 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1614 bus_dmasync_op_t op;
1616 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1617 op = BUS_DMASYNC_POSTREAD;
1619 op = BUS_DMASYNC_POSTWRITE;
1620 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1621 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1624 /* Common Case inline... */
1625 if (hba_stat == HA_NO_ERROR) {
1626 ccb->csio.scsi_status = scsi_stat;
1627 ccb->ccb_h.status = 0;
1628 switch (scsi_stat) {
1629 case SCSI_STATUS_OK:
1630 ccb->ccb_h.status |= CAM_REQ_CMP;
1632 case SCSI_STATUS_CHECK_COND:
1633 case SCSI_STATUS_CMD_TERMINATED:
1634 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1635 ccb->csio.sense_len);
1636 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1639 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1640 /* XXX Freeze DevQ */
1643 ccb->csio.resid = residue_len;
1644 dptfreeccb(dpt, dccb);
1647 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1654 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1655 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1657 ccb->csio.resid = resid;
1660 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1663 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1665 case HA_SCSIBUS_RESET:
1666 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1667 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1670 case HA_CP_RESET: /* XXX ??? */
1671 case HA_CP_ABORT_NA: /* XXX ??? */
1672 case HA_CP_RESET_NA: /* XXX ??? */
1673 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1674 ccb->ccb_h.status = CAM_REQ_ABORTED;
1679 case HA_PCI_STABORT:
1683 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1685 case HA_UNX_MSGRJCT:
1686 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1688 case HA_UNX_BUSPHASE:
1689 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1691 case HA_UNX_BUS_FREE:
1692 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1695 case HA_RESET_STUCK:
1697 * Dead??? Can the controller get unstuck
1698 * from these conditions
1700 ccb->ccb_h.status = CAM_NO_HBA;
1702 case HA_RSENSE_FAIL:
1703 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1706 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1707 printf("Please mail this message to shimon@simon-shapiro.org\n");
1708 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1711 dptfreeccb(dpt, dccb);
1716 dpttimeout(void *arg)
1718 struct dpt_ccb *dccb;
1720 struct dpt_softc *dpt;
1723 dccb = (struct dpt_ccb *)arg;
1725 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1726 xpt_print_path(ccb->ccb_h.path);
1727 printf("CCB %p - timed out\n", (void *)dccb);
1732 * Try to clear any pending jobs. FreeBSD will loose interrupts,
1733 * leaving the controller suspended, and commands timed-out.
1734 * By calling the interrupt handler, any command thus stuck will be
1739 if ((dccb->state & DCCB_ACTIVE) == 0) {
1740 xpt_print_path(ccb->ccb_h.path);
1741 printf("CCB %p - timed out CCB already completed\n",
1747 /* Abort this particular command. Leave all others running */
1748 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1749 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1750 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1755 * Shutdown the controller and ensure that the cache is completely flushed.
1756 * Called from the shutdown_final event after all disk access has completed.
1759 dptshutdown(void *arg, int howto)
1763 dpt = (dpt_softc_t *)arg;
1765 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1769 * What we do for a shutdown, is give the DPT early power loss warning
1771 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1772 DELAY(1000 * 1000 * 5);
1773 printf("dpt%d: Controller was warned of shutdown and is now "
1774 "disabled\n", dpt->unit);
1777 /*============================================================================*/
1780 #ifdef DPT_RESET_HBA
1783 ** Function name : dpt_reset_hba
1785 ** Description : Reset the HBA and properly discard all pending work
1790 dpt_reset_hba(dpt_softc_t *dpt)
1794 dpt_ccb_t dccb, *dccbp;
1796 struct scsi_xfer *xs;
1798 /* Prepare a control block. The SCSI command part is immaterial */
1801 dccb.state = DPT_CCB_STATE_NEW;
1802 dccb.std_callback = NULL;
1803 dccb.wrbuff_callback = NULL;
1805 ccb = &dccb.eata_ccb;
1806 ccb->CP_OpCode = EATA_CMD_RESET;
1807 ccb->SCSI_Reset = 0;
1809 ccb->Auto_Req_Sen = 1;
1810 ccb->cp_id = 0; /* Should be ignored */
1814 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1815 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1816 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1817 ccb->cp_viraddr = (u_int32_t) & ccb;
1819 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1820 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1822 /* Lock up the submitted queue. We are very persistant here */
1824 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1828 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1831 /* Send the RESET message */
1832 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1833 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1834 printf("dpt%d: Failed to send the RESET message.\n"
1835 " Trying cold boot (ouch!)\n", dpt->unit);
1838 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1839 EATA_COLD_BOOT, 0, 0,
1841 panic("dpt%d: Faild to cold boot the HBA\n",
1844 #ifdef DPT_MEASURE_PERFORMANCE
1845 dpt->performance.cold_boots++;
1846 #endif /* DPT_MEASURE_PERFORMANCE */
1849 #ifdef DPT_MEASURE_PERFORMANCE
1850 dpt->performance.warm_starts++;
1851 #endif /* DPT_MEASURE_PERFORMANCE */
1853 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1856 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1857 struct scsi_xfer *xs = dccbp->xs;
1859 /* Not all transactions have xs structs */
1861 /* Tell the kernel proper this did not complete well */
1862 xs->error |= XS_SELTIMEOUT;
1863 xs->flags |= SCSI_ITSDONE;
1867 dpt_Qremove_submitted(dpt, dccbp);
1869 /* Remember, Callbacks are NOT in the standard queue */
1870 if (dccbp->std_callback != NULL) {
1871 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1875 dpt_Qpush_free(dpt, dccbp);
1880 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1881 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1884 #endif /* DPT_RESET_HBA */
1887 * Build a Command Block for target mode READ/WRITE BUFFER,
1888 * with the ``sync'' bit ON.
1890 * Although the length and offset are 24 bit fields in the command, they cannot
1891 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1892 * If they are sensless, we round them to zero offset, maximum length and
1897 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1898 dpt_ccb_t * ccb, int mode, u_int8_t command,
1899 u_int16_t length, u_int16_t offset)
1904 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
1905 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
1906 dpt->unit, length, offset);
1907 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
1912 ccb->state = DPT_CCB_STATE_NEW;
1913 ccb->std_callback = (ccb_callback) dpt_target_done;
1914 ccb->wrbuff_callback = NULL;
1916 cp = &ccb->eata_ccb;
1917 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
1920 cp->Auto_Req_Sen = 1;
1925 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
1926 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
1927 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
1928 cp->cp_viraddr = (u_int32_t) & ccb;
1930 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1932 cp->cp_scsi_cmd = command;
1933 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
1934 cp->cp_lun = lun; /* Order is important here! */
1935 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
1936 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
1937 cp->cp_cdb[4] = (length >> 8) & 0xFF;
1938 cp->cp_cdb[5] = length & 0xFF;
1939 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
1940 cp->cp_cdb[7] = (length >> 8) & 0xFF;
1941 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
1942 cp->cp_cdb[9] = 0; /* No sync, no match bits */
1945 * This could be optimized to live in dpt_register_buffer.
1946 * We keep it here, just in case the kernel decides to reallocate pages
1948 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
1949 dpt->rw_buffer[bus][target][lun])) {
1950 printf("dpt%d: Failed to setup Scatter/Gather for "
1951 "Target-Mode buffer\n", dpt->unit);
1955 /* Setup a target mode READ command */
1958 dpt_set_target(int redo, dpt_softc_t * dpt,
1959 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
1960 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
1964 if (dpt->target_mode_enabled) {
1968 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
1969 SCSI_TM_READ_BUFFER, length, offset);
1971 ccb->transaction_id = ++dpt->commands_processed;
1973 #ifdef DPT_MEASURE_PERFORMANCE
1974 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
1975 ccb->command_started = microtime_now;
1977 dpt_Qadd_waiting(dpt, ccb);
1978 dpt_sched_queue(dpt);
1982 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
1988 * Schedule a buffer to be sent to another target.
1989 * The work will be scheduled and the callback provided will be called when
1990 * the work is actually done.
1992 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
1993 * get notified of receipt of buffers.
1997 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
1998 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
1999 buff_wr_done callback)
2002 dpt_ccb_t *ccb = NULL;
2005 /* This is an external call. Be a bit paranoid */
2006 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2008 dpt = TAILQ_NEXT(dpt, links)) {
2009 if (dpt->unit == unit)
2013 return (INVALID_UNIT);
2017 if (dpt->target_mode_enabled) {
2018 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2019 (lun > dpt->max_lun)) {
2020 return (INVALID_SENDER);
2022 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2023 (dpt->buffer_receiver[channel][target][lun] == NULL))
2024 return (NOT_REGISTERED);
2026 ospl = splsoftcam();
2027 /* Process the free list */
2028 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2029 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2030 " Please try later\n",
2033 return (NO_RESOURCES);
2035 /* Now grab the newest CCB */
2036 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2038 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2042 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2043 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2044 SCSI_TM_WRITE_BUFFER,
2046 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2049 ccb->transaction_id = ++dpt->commands_processed;
2051 #ifdef DPT_MEASURE_PERFORMANCE
2052 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2053 ccb->command_started = microtime_now;
2055 dpt_Qadd_waiting(dpt, ccb);
2056 dpt_sched_queue(dpt);
2061 return (DRIVER_DOWN);
2065 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2070 cp = &ccb->eata_ccb;
2073 * Remove the CCB from the waiting queue.
2074 * We do NOT put it back on the free, etc., queues as it is a special
2075 * ccb, owned by the dpt_softc of this unit.
2077 ospl = splsoftcam();
2078 dpt_Qremove_completed(dpt, ccb);
2081 #define br_channel (ccb->eata_ccb.cp_channel)
2082 #define br_target (ccb->eata_ccb.cp_id)
2083 #define br_lun (ccb->eata_ccb.cp_LUN)
2084 #define br_index [br_channel][br_target][br_lun]
2085 #define read_buffer_callback (dpt->buffer_receiver br_index )
2086 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2087 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2088 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2089 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2091 /* Different reasons for being here, you know... */
2092 switch (ccb->eata_ccb.cp_scsi_cmd) {
2093 case SCSI_TM_READ_BUFFER:
2094 if (read_buffer_callback != NULL) {
2095 /* This is a buffer generated by a kernel process */
2096 read_buffer_callback(dpt->unit, br_channel,
2099 br_offset, br_length);
2102 * This is a buffer waited for by a user (sleeping)
2108 /* We ALWAYS re-issue the same command; args are don't-care */
2109 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2112 case SCSI_TM_WRITE_BUFFER:
2113 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2114 br_offset, br_length,
2115 br_lun, ccb->status_packet.hba_stat);
2118 printf("dpt%d: %s is an unsupported command for target mode\n",
2119 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2121 ospl = splsoftcam();
2122 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2123 dpt_Qpush_free(dpt, ccb);
2129 * Use this function to register a client for a buffer read target operation.
2130 * The function you register will be called every time a buffer is received
2131 * by the target mode code.
2134 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2135 u_int8_t mode, u_int16_t length, u_int16_t offset,
2136 dpt_rec_buff callback, dpt_rb_op_t op)
2139 dpt_ccb_t *ccb = NULL;
2142 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2144 dpt = TAILQ_NEXT(dpt, links)) {
2145 if (dpt->unit == unit)
2149 return (INVALID_UNIT);
2153 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2154 return (DRIVER_DOWN);
2156 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2157 (lun > (dpt->max_lun - 1)))
2158 return (INVALID_SENDER);
2160 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2161 if (op == REGISTER_BUFFER) {
2162 /* Assign the requested callback */
2163 dpt->buffer_receiver[channel][target][lun] = callback;
2165 ospl = splsoftcam();
2167 /* Process the free list */
2168 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2169 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2170 " Please try later\n",
2173 return (NO_RESOURCES);
2175 /* Now grab the newest CCB */
2176 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2178 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2183 /* Clean up the leftover of the previous tenant */
2184 ccb->status = DPT_CCB_STATE_NEW;
2185 dpt->target_ccb[channel][target][lun] = ccb;
2187 dpt->rw_buffer[channel][target][lun] =
2188 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
2189 if (dpt->rw_buffer[channel][target][lun] == NULL) {
2190 printf("dpt%d: Failed to allocate "
2191 "Target-Mode buffer\n", dpt->unit);
2192 ospl = splsoftcam();
2193 dpt_Qpush_free(dpt, ccb);
2195 return (NO_RESOURCES);
2197 dpt_set_target(0, dpt, channel, target, lun, mode,
2198 length, offset, ccb);
2199 return (SUCCESSFULLY_REGISTERED);
2201 return (NOT_REGISTERED);
2203 if (op == REGISTER_BUFFER) {
2204 if (dpt->buffer_receiver[channel][target][lun] == callback)
2205 return (ALREADY_REGISTERED);
2207 return (REGISTERED_TO_ANOTHER);
2209 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2210 dpt->buffer_receiver[channel][target][lun] = NULL;
2211 ospl = splsoftcam();
2212 dpt_Qpush_free(dpt, ccb);
2214 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2215 return (SUCCESSFULLY_REGISTERED);
2217 return (INVALID_CALLBACK);
2223 /* Return the state of the blinking DPT LED's */
2225 dpt_blinking_led(dpt_softc_t * dpt)
2237 for (ndx = 0, state = 0, previous = 0;
2238 (ndx < 10) && (state != previous);
2241 state = dpt_inl(dpt, 1);
2244 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2245 result = dpt_inb(dpt, 5);
2252 * Execute a command which did not come from the kernel's SCSI layer.
2253 * The only way to map user commands to bus and target is to comply with the
2254 * standard DPT wire-down scheme:
2257 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2258 caddr_t cmdarg, int minor_no)
2262 int channel, target, lun;
2269 channel = minor2hba(minor_no);
2270 target = minor2target(minor_no);
2271 lun = minor2lun(minor_no);
2273 if ((channel > (dpt->channels - 1))
2274 || (target > dpt->max_id)
2275 || (lun > dpt->max_lun))
2278 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2279 /* This one is for the controller itself */
2280 if ((user_cmd->eataID[0] != 'E')
2281 || (user_cmd->eataID[1] != 'A')
2282 || (user_cmd->eataID[2] != 'T')
2283 || (user_cmd->eataID[3] != 'A')) {
2287 /* Get a DPT CCB, so we can prepare a command */
2288 ospl = splsoftcam();
2290 /* Process the free list */
2291 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2292 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2293 " Please try later\n",
2298 /* Now grab the newest CCB */
2299 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2301 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2304 /* Clean up the leftover of the previous tenant */
2305 ccb->status = DPT_CCB_STATE_NEW;
2308 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2309 sizeof(eata_ccb_t));
2311 /* We do not want to do user specified scatter/gather. Why?? */
2312 if (ccb->eata_ccb.scatter == 1)
2315 ccb->eata_ccb.Auto_Req_Sen = 1;
2316 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2317 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2318 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2319 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2320 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2321 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2323 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2324 /* Data I/O is involved in this command. Alocate buffer */
2325 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2326 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2327 M_TEMP, M_WAITOK, 0, ~0,
2328 ccb->eata_ccb.cp_datalen,
2331 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2336 printf("dpt%d: Cannot allocate %d bytes "
2337 "for EATA command\n", dpt->unit,
2338 ccb->eata_ccb.cp_datalen);
2341 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2342 if (ccb->eata_ccb.DataIn == 1) {
2343 if (copyin(usr_cmd_DMA,
2344 data, ccb->eata_ccb.cp_datalen) == -1)
2348 /* No data I/O involved here. Make sure the DPT knows that */
2349 ccb->eata_ccb.cp_datalen = 0;
2353 if (ccb->eata_ccb.FWNEST == 1)
2354 ccb->eata_ccb.FWNEST = 0;
2356 if (ccb->eata_ccb.cp_datalen != 0) {
2357 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2365 * We are required to quiet a SCSI bus.
2366 * since we do not queue comands on a bus basis,
2367 * we wait for ALL commands on a controller to complete.
2368 * In the mean time, sched_queue() will not schedule new commands.
2370 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2371 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2372 /* We wait for ALL traffic for this HBa to subside */
2373 ospl = splsoftcam();
2374 dpt->state |= DPT_HA_QUIET;
2377 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2378 huh = tsleep((void *) dpt, PCATCH, "dptqt", 100 * hz);
2381 /* Wakeup call received */
2392 /* Resume normal operation */
2393 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2394 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2395 ospl = splsoftcam();
2396 dpt->state &= ~DPT_HA_QUIET;
2400 * Schedule the command and submit it.
2401 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2405 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2407 ccb->transaction_id = ++dpt->commands_processed;
2408 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2409 ccb->result = (u_int32_t) & cmdarg;
2412 #ifdef DPT_MEASURE_PERFORMANCE
2413 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2414 ccb->command_started = microtime_now;
2417 dpt_Qadd_waiting(dpt, ccb);
2420 dpt_sched_queue(dpt);
2422 /* Wait for the command to complete */
2423 (void) tsleep((void *) ccb, PCATCH, "dptucw", 100 * hz);
2425 /* Free allocated memory */
2433 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2435 int ospl = splsoftcam();
2440 * If Auto Request Sense is on, copyout the sense struct
2442 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2443 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2444 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2445 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2446 sizeof(struct scsi_sense_data))) {
2447 ccb->result = EFAULT;
2448 dpt_Qpush_free(dpt, ccb);
2454 /* If DataIn is on, copyout the data */
2455 if ((ccb->eata_ccb.DataIn == 1)
2456 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2457 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2458 dpt_Qpush_free(dpt, ccb);
2459 ccb->result = EFAULT;
2466 /* Copyout the status */
2467 result = ccb->status_packet.hba_stat;
2468 cmd_arg = (caddr_t) ccb->result;
2470 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2471 dpt_Qpush_free(dpt, ccb);
2472 ccb->result = EFAULT;
2477 /* Put the CCB back in the freelist */
2478 ccb->state |= DPT_CCB_STATE_COMPLETED;
2479 dpt_Qpush_free(dpt, ccb);
2481 /* Free allocated memory */
2486 #ifdef DPT_HANDLE_TIMEOUTS
2488 * This function walks down the SUBMITTED queue.
2489 * Every request that is too old gets aborted and marked.
2490 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2491 * We just walk the list, aborting old commands and marking them as such.
2492 * The dpt_complete function will get rid of the that were interrupted in the
2495 * This function needs to run at splcam(), as it interacts with the submitted
2496 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2497 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2498 * willbe able to pre-empt it, grab a transaction in progress (towards
2499 * destruction) and operate on it. The state of this transaction will be not
2501 * The only other option, is to lock it only as long as necessary but have
2502 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2503 * a SMP environment, the advantage is dubvious for a function that runs once
2504 * every ten seconds for few microseconds and, on systems with healthy
2505 * hardware, does not do anything anyway.
2509 dpt_handle_timeouts(dpt_softc_t * dpt)
2516 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2517 printf("dpt%d WARNING: Timeout Handling Collision\n",
2522 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2524 /* Loop through the entire submitted queue, looking for lost souls */
2525 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2527 ccb = TAILQ_NEXT(ccb, links)) {
2528 struct scsi_xfer *xs;
2529 u_int32_t age, max_age;
2532 age = dpt_time_delta(ccb->command_started, microtime_now);
2534 #define TenSec 10000000
2536 if (xs == NULL) { /* Local, non-kernel call */
2539 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2540 + DPT_TIMEOUT_FACTOR))
2542 ? (xs->timeout * (dpt->submitted_ccbs_count
2543 + DPT_TIMEOUT_FACTOR))
2548 * If a transaction is marked lost and is TWICE as old as we
2549 * care, then, and only then do we destroy it!
2551 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2552 /* Remember who is next */
2553 if (age > (max_age * 2)) {
2554 dpt_Qremove_submitted(dpt, ccb);
2555 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2556 ccb->state |= DPT_CCB_STATE_ABORTED;
2557 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2558 if (ccb->retries++ > DPT_RETRIES) {
2559 printf("dpt%d ERROR: Destroying stale "
2562 "c%db%dt%du%d (%d/%d)\n",
2563 dpt->unit, ccb->transaction_id,
2566 ccb->eata_ccb.cp_channel,
2567 ccb->eata_ccb.cp_id,
2568 ccb->eata_ccb.cp_LUN, age,
2570 #define send_ccb &ccb->eata_ccb
2571 #define ESA EATA_SPECIFIC_ABORT
2572 (void) dpt_send_immediate(dpt,
2576 dpt_Qpush_free(dpt, ccb);
2578 /* The SCSI layer should re-try */
2579 xs->error |= XS_TIMEOUT;
2580 xs->flags |= SCSI_ITSDONE;
2583 printf("dpt%d ERROR: Stale %d (%s) on "
2584 "c%db%dt%du%d (%d)\n"
2587 dpt->unit, ccb->transaction_id,
2590 ccb->eata_ccb.cp_channel,
2591 ccb->eata_ccb.cp_id,
2592 ccb->eata_ccb.cp_LUN,
2593 age, ccb->retries, DPT_RETRIES);
2595 dpt_Qpush_waiting(dpt, ccb);
2596 dpt_sched_queue(dpt);
2601 * This is a transaction that is not to be destroyed
2602 * (yet) But it is too old for our liking. We wait as
2603 * long as the upper layer thinks. Not really, we
2604 * multiply that by the number of commands in the
2605 * submitted queue + 1.
2607 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2608 (age != ~0) && (age > max_age)) {
2609 printf("dpt%d ERROR: Marking %d (%s) on "
2611 " as late after %dusec\n",
2612 dpt->unit, ccb->transaction_id,
2614 dpt->unit, ccb->eata_ccb.cp_channel,
2615 ccb->eata_ccb.cp_id,
2616 ccb->eata_ccb.cp_LUN, age);
2617 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2622 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2627 dpt_timeout(void *arg)
2629 dpt_softc_t *dpt = (dpt_softc_t *) arg;
2631 if (!(dpt->state & DPT_HA_TIMEOUTS_ACTIVE))
2632 dpt_handle_timeouts(dpt);
2634 timeout(dpt_timeout, (caddr_t) dpt, hz * 10);
2637 #endif /* DPT_HANDLE_TIMEOUTS */