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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * dpt_scsi.c: SCSI dependant code for the DPT driver
37 * credits: Assisted by Mike Neuffer in the early low level DPT code
38 * Thanx to Mark Salyzyn of DPT for his assistance.
39 * Special thanx to Justin Gibbs for invaluable help in
40 * making this driver look and work like a FreeBSD component.
41 * Last but not least, many thanx to UCB and the FreeBSD
42 * team for creating and maintaining such a wonderful O/S.
44 * TODO: * Add ISA probe code.
45 * * Add driver-level RAID-0. This will allow interoperability with
46 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
47 * arrays that span controllers (Wow!).
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/eventhandler.h>
56 #include <sys/malloc.h>
57 #include <sys/kernel.h>
61 #include <machine/bus.h>
63 #include <machine/resource.h>
66 #include <machine/clock.h>
69 #include <cam/cam_ccb.h>
70 #include <cam/cam_sim.h>
71 #include <cam/cam_xpt_sim.h>
72 #include <cam/cam_debug.h>
73 #include <cam/scsi/scsi_all.h>
74 #include <cam/scsi/scsi_message.h>
79 #include <dev/dpt/dpt.h>
81 /* dpt_isa.c, dpt_eisa.c, and dpt_pci.c need this in a central place */
82 int dpt_controllers_present;
83 devclass_t dpt_devclass;
85 #define microtime_now dpt_time_now()
87 #define dpt_inl(dpt, port) \
88 bus_space_read_4((dpt)->tag, (dpt)->bsh, port)
89 #define dpt_inb(dpt, port) \
90 bus_space_read_1((dpt)->tag, (dpt)->bsh, port)
91 #define dpt_outl(dpt, port, value) \
92 bus_space_write_4((dpt)->tag, (dpt)->bsh, port, value)
93 #define dpt_outb(dpt, port, value) \
94 bus_space_write_1((dpt)->tag, (dpt)->bsh, port, value)
97 * These will have to be setup by parameters passed at boot/load time. For
98 * perfromance reasons, we make them constants for the time being.
100 #define dpt_min_segs DPT_MAX_SEGS
101 #define dpt_max_segs DPT_MAX_SEGS
103 /* Definitions for our use of the SIM private CCB area */
104 #define ccb_dccb_ptr spriv_ptr0
105 #define ccb_dpt_ptr spriv_ptr1
107 /* ================= Private Inline Function declarations ===================*/
108 static __inline int dpt_just_reset(dpt_softc_t * dpt);
109 static __inline int dpt_raid_busy(dpt_softc_t * dpt);
110 static __inline int dpt_pio_wait (u_int32_t, u_int, u_int, u_int);
111 static __inline int dpt_wait(dpt_softc_t *dpt, u_int bits,
113 static __inline struct dpt_ccb* dptgetccb(struct dpt_softc *dpt);
114 static __inline void dptfreeccb(struct dpt_softc *dpt,
115 struct dpt_ccb *dccb);
116 static __inline bus_addr_t dptccbvtop(struct dpt_softc *dpt,
117 struct dpt_ccb *dccb);
119 static __inline int dpt_send_immediate(dpt_softc_t *dpt,
120 eata_ccb_t *cmd_block,
121 u_int32_t cmd_busaddr,
123 u_int ifc, u_int code,
126 /* ==================== Private Function declarations =======================*/
127 static void dptmapmem(void *arg, bus_dma_segment_t *segs,
128 int nseg, int error);
130 static struct sg_map_node*
131 dptallocsgmap(struct dpt_softc *dpt);
133 static int dptallocccbs(dpt_softc_t *dpt);
135 static int dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb,
136 u_int32_t dccb_busaddr, u_int size,
137 u_int page, u_int target, int extent);
138 static void dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb,
139 u_int32_t dccb_busaddr,
142 static void dpt_poll(struct cam_sim *sim);
144 static void dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
145 int nseg, int error);
147 static void dpt_action(struct cam_sim *sim, union ccb *ccb);
149 static int dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd,
150 u_int32_t cmd_busaddr,
151 u_int command, u_int retries,
152 u_int ifc, u_int code,
154 static void dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb,
155 union ccb *ccb, u_int hba_stat,
156 u_int scsi_stat, u_int32_t resid);
158 static void dpttimeout(void *arg);
159 static void dptshutdown(void *arg, int howto);
161 /* ================= Private Inline Function definitions ====================*/
163 dpt_just_reset(dpt_softc_t * dpt)
165 if ((dpt_inb(dpt, 2) == 'D')
166 && (dpt_inb(dpt, 3) == 'P')
167 && (dpt_inb(dpt, 4) == 'T')
168 && (dpt_inb(dpt, 5) == 'H'))
175 dpt_raid_busy(dpt_softc_t * dpt)
177 if ((dpt_inb(dpt, 0) == 'D')
178 && (dpt_inb(dpt, 1) == 'P')
179 && (dpt_inb(dpt, 2) == 'T'))
186 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
191 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
192 c = inb(base + reg) & bits;
202 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
207 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
208 c = dpt_inb(dpt, HA_RSTATUS) & bits;
217 static __inline struct dpt_ccb*
218 dptgetccb(struct dpt_softc *dpt)
220 struct dpt_ccb* dccb;
224 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
225 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
227 } else if (dpt->total_dccbs < dpt->max_dccbs) {
229 dccb = SLIST_FIRST(&dpt->free_dccb_list);
231 printf("dpt%d: Can't malloc DCCB\n", dpt->unit);
233 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
243 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
248 if ((dccb->state & DCCB_ACTIVE) != 0)
249 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
250 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
251 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
252 else if (dpt->resource_shortage != 0
253 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
254 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
255 dpt->resource_shortage = FALSE;
257 dccb->state = DCCB_FREE;
258 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
263 static __inline bus_addr_t
264 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
266 return (dpt->dpt_ccb_busbase
267 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
270 static __inline struct dpt_ccb *
271 dptccbptov(struct dpt_softc *dpt, bus_addr_t busaddr)
273 return (dpt->dpt_dccbs
274 + ((struct dpt_ccb *)busaddr
275 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
279 * Send a command for immediate execution by the DPT
280 * See above function for IMPORTANT notes.
283 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
284 u_int32_t cmd_busaddr, u_int retries,
285 u_int ifc, u_int code, u_int code2)
287 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
288 EATA_CMD_IMMEDIATE, retries, ifc,
293 /* ===================== Private Function definitions =======================*/
295 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
297 bus_addr_t *busaddrp;
299 busaddrp = (bus_addr_t *)arg;
300 *busaddrp = segs->ds_addr;
303 static struct sg_map_node *
304 dptallocsgmap(struct dpt_softc *dpt)
306 struct sg_map_node *sg_map;
308 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
313 /* Allocate S/G space for the next batch of CCBS */
314 if (bus_dmamem_alloc(dpt->sg_dmat, (void **)&sg_map->sg_vaddr,
315 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
316 free(sg_map, M_DEVBUF);
320 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
321 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
324 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
330 * Allocate another chunk of CCB's. Return count of entries added.
331 * Assumed to be called at splcam().
334 dptallocccbs(dpt_softc_t *dpt)
336 struct dpt_ccb *next_ccb;
337 struct sg_map_node *sg_map;
343 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
345 if (next_ccb == dpt->dpt_dccbs) {
347 * First time through. Re-use the S/G
348 * space we allocated for initialization
351 sg_map = SLIST_FIRST(&dpt->sg_maps);
353 sg_map = dptallocsgmap(dpt);
359 segs = sg_map->sg_vaddr;
360 physaddr = sg_map->sg_physaddr;
362 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
363 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
366 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
370 next_ccb->sg_list = segs;
371 next_ccb->sg_busaddr = htonl(physaddr);
372 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
373 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
374 next_ccb->eata_ccb.cp_reqDMA =
375 htonl(dptccbvtop(dpt, next_ccb)
376 + offsetof(struct dpt_ccb, sense_data));
377 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
378 next_ccb->state = DCCB_FREE;
379 next_ccb->tag = dpt->total_dccbs;
380 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
382 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
383 dpt->dpt_ccb_busend += sizeof(*next_ccb);
391 dpt_pio_get_conf (u_int32_t base)
393 static dpt_conf_t * conf;
398 * Allocate a dpt_conf_t
401 conf = (dpt_conf_t *)malloc(sizeof(dpt_conf_t),
406 * If we didn't get one then we probably won't ever get one.
409 printf("dpt: unable to allocate dpt_conf_t\n");
414 * If we have one, clean it up.
416 bzero(conf, sizeof(dpt_conf_t));
419 * Reset the controller.
421 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
424 * Wait for the controller to become ready.
425 * For some reason there can be -no- delays after calling reset
426 * before we wait on ready status.
428 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
429 printf("dpt: timeout waiting for controller to become ready\n");
433 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
434 printf("dpt: timetout waiting for adapter ready.\n");
439 * Send the PIO_READ_CONFIG command.
441 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
444 * Read the data into the struct.
446 p = (u_int16_t *)conf;
447 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
449 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
451 printf("dpt: timeout in data read.\n");
455 (*p) = inw(base + HA_RDATA);
459 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:
1038 * XXX Use Adaptec translation until I find out how to
1039 * get this information from the card.
1041 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1045 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1048 ccb->ccb_h.status = CAM_REQ_CMP;
1052 case XPT_TERM_IO: /* Terminate the I/O process */
1054 ccb->ccb_h.status = CAM_REQ_INVALID;
1057 case XPT_PATH_INQ: /* Path routing inquiry */
1059 struct ccb_pathinq *cpi = &ccb->cpi;
1061 cpi->version_num = 1;
1062 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1063 if (dpt->max_id > 7)
1064 cpi->hba_inquiry |= PI_WIDE_16;
1065 cpi->target_sprt = 0;
1067 cpi->hba_eng_cnt = 0;
1068 cpi->max_target = dpt->max_id;
1069 cpi->max_lun = dpt->max_lun;
1070 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1071 cpi->bus_id = cam_sim_bus(sim);
1072 cpi->base_transfer_speed = 3300;
1073 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1074 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1075 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1076 cpi->unit_number = cam_sim_unit(sim);
1077 cpi->ccb_h.status = CAM_REQ_CMP;
1082 ccb->ccb_h.status = CAM_REQ_INVALID;
1089 * This routine will try to send an EATA command to the DPT HBA.
1090 * It will, by default, try 20,000 times, waiting 50us between tries.
1091 * It returns 0 on success and 1 on failure.
1092 * It is assumed to be called at splcam().
1095 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1096 u_int32_t cmd_busaddr, u_int command, u_int retries,
1097 u_int ifc, u_int code, u_int code2)
1105 * I hate this polling nonsense. Wish there was a way to tell the DPT
1106 * to go get commands at its own pace, or to interrupt when ready.
1107 * In the mean time we will measure how many itterations it really
1110 for (loop = 0; loop < retries; loop++) {
1111 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1117 if (loop < retries) {
1118 #ifdef DPT_MEASURE_PERFORMANCE
1119 if (loop > dpt->performance.max_eata_tries)
1120 dpt->performance.max_eata_tries = loop;
1122 if (loop < dpt->performance.min_eata_tries)
1123 dpt->performance.min_eata_tries = loop;
1126 #ifdef DPT_MEASURE_PERFORMANCE
1127 ++dpt->performance.command_too_busy;
1132 /* The controller is alive, advance the wedge timer */
1133 #ifdef DPT_RESET_HBA
1134 dpt->last_contact = microtime_now;
1137 if (cmd_block == NULL)
1139 #if (BYTE_ORDER == BIG_ENDIAN)
1141 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1142 | ((cmd_busaddr >> 16) & 0xFF)
1143 | ((cmd_busaddr >> 8) & 0xFF)
1144 | (cmd_busaddr & 0xFF);
1147 /* And now the address */
1148 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1150 if (command == EATA_CMD_IMMEDIATE) {
1151 if (cmd_block == NULL) {
1152 dpt_outb(dpt, HA_WCODE2, code2);
1153 dpt_outb(dpt, HA_WCODE, code);
1155 dpt_outb(dpt, HA_WIFC, ifc);
1157 dpt_outb(dpt, HA_WCOMMAND, command);
1163 /* ==================== Exported Function definitions =======================*/
1165 dpt_alloc(device_t dev)
1167 dpt_softc_t *dpt = device_get_softc(dev);
1170 dpt->tag = rman_get_bustag(dpt->io_res);
1171 dpt->bsh = rman_get_bushandle(dpt->io_res) + dpt->io_offset;
1172 dpt->unit = device_get_unit(dev);
1173 SLIST_INIT(&dpt->free_dccb_list);
1174 LIST_INIT(&dpt->pending_ccb_list);
1175 for (i = 0; i < MAX_CHANNELS; i++)
1176 dpt->resetlevel[i] = DPT_HA_OK;
1178 #ifdef DPT_MEASURE_PERFORMANCE
1179 dpt_reset_performance(dpt);
1180 #endif /* DPT_MEASURE_PERFORMANCE */
1185 dpt_free(struct dpt_softc *dpt)
1187 switch (dpt->init_level) {
1190 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1192 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1194 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1196 bus_dma_tag_destroy(dpt->dccb_dmat);
1198 bus_dma_tag_destroy(dpt->buffer_dmat);
1201 struct sg_map_node *sg_map;
1203 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1204 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1205 bus_dmamap_unload(dpt->sg_dmat,
1207 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1209 free(sg_map, M_DEVBUF);
1211 bus_dma_tag_destroy(dpt->sg_dmat);
1219 dpt_alloc_resources (device_t dev)
1224 dpt = device_get_softc(dev);
1226 dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type, &dpt->io_rid,
1228 if (dpt->io_res == NULL) {
1229 device_printf(dev, "No I/O space?!\n");
1234 dpt->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &dpt->irq_rid,
1236 if (dpt->irq_res == NULL) {
1237 device_printf(dev, "No IRQ!\n");
1249 dpt_release_resources (device_t dev)
1251 struct dpt_softc * dpt;
1253 dpt = device_get_softc(dev);
1256 bus_teardown_intr(dev, dpt->irq_res, dpt->ih);
1258 bus_release_resource(dev, dpt->io_type, dpt->io_rid, dpt->io_res);
1260 bus_release_resource(dev, SYS_RES_IRQ, dpt->irq_rid, dpt->irq_res);
1262 bus_release_resource(dev, SYS_RES_DRQ, dpt->drq_rid, dpt->drq_res);
1267 static u_int8_t string_sizes[] =
1269 sizeof(((dpt_inq_t*)NULL)->vendor),
1270 sizeof(((dpt_inq_t*)NULL)->modelNum),
1271 sizeof(((dpt_inq_t*)NULL)->firmware),
1272 sizeof(((dpt_inq_t*)NULL)->protocol),
1276 dpt_init(struct dpt_softc *dpt)
1279 struct sg_map_node *sg_map;
1286 dpt->init_level = 0;
1287 SLIST_INIT(&dpt->sg_maps);
1289 #ifdef DPT_RESET_BOARD
1290 printf("dpt%d: resetting HBA\n", dpt->unit);
1291 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1293 /* XXX Shouldn't we poll a status register or something??? */
1295 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1296 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1299 /* lowaddr */ BUS_SPACE_MAXADDR,
1300 /* highaddr */ BUS_SPACE_MAXADDR,
1302 /* filterarg */ NULL,
1303 /* maxsize */ PAGE_SIZE,
1305 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1307 /* lockfunc */ busdma_lock_mutex,
1308 /* lockarg */ &Giant,
1309 &dpt->sg_dmat) != 0) {
1316 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1317 * memory. To get the allocation size, we need to know how many
1318 * ccbs the card supports. This requires a ccb. We solve this
1319 * chicken and egg problem by allocating some re-usable S/G space
1320 * up front, and treating it as our status packet, CCB, and target
1321 * memory space for these commands.
1323 sg_map = dptallocsgmap(dpt);
1327 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1328 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1329 bzero(dccb, sizeof(*dccb));
1330 dpt->sp_physaddr = sg_map->sg_physaddr;
1331 dccb->eata_ccb.cp_dataDMA =
1332 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1333 dccb->eata_ccb.cp_busaddr = ~0;
1334 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1335 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1336 + offsetof(struct dpt_ccb, sense_data));
1338 /* Okay. Fetch our config */
1339 bzero(&dccb[1], sizeof(conf)); /* data area */
1340 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1341 sizeof(conf), 0xc1, 7, 1);
1344 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1347 bcopy(&dccb[1], &conf, sizeof(conf));
1349 bzero(&dccb[1], sizeof(dpt->board_data));
1350 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1351 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1353 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1356 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1358 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1359 (u_int8_t *)&dccb[1]);
1361 switch (ntohl(conf.splen)) {
1363 dpt->EATA_revision = 'a';
1366 dpt->EATA_revision = 'b';
1369 dpt->EATA_revision = 'c';
1372 dpt->EATA_revision = 'z';
1375 dpt->EATA_revision = '?';
1378 dpt->max_id = conf.MAX_ID;
1379 dpt->max_lun = conf.MAX_LUN;
1380 dpt->irq = conf.IRQ;
1381 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1382 dpt->channels = conf.MAX_CHAN + 1;
1383 dpt->state |= DPT_HA_OK;
1385 dpt->primary = FALSE;
1387 dpt->primary = TRUE;
1389 dpt->more_support = conf.MORE_support;
1391 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1392 dpt->immediate_support = 1;
1394 dpt->immediate_support = 0;
1396 dpt->broken_INQUIRY = FALSE;
1398 dpt->cplen = ntohl(conf.cplen);
1399 dpt->cppadlen = ntohs(conf.cppadlen);
1400 dpt->max_dccbs = ntohs(conf.queuesiz);
1402 if (dpt->max_dccbs > 256) {
1403 printf("dpt%d: Max CCBs reduced from %d to "
1404 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1405 dpt->max_dccbs = 256;
1408 dpt->hostid[0] = conf.scsi_id0;
1409 dpt->hostid[1] = conf.scsi_id1;
1410 dpt->hostid[2] = conf.scsi_id2;
1415 dpt->sgsize = ntohs(conf.SGsiz);
1417 /* We can only get 64k buffers, so don't bother to waste space. */
1418 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1421 if (dpt->sgsize > dpt_max_segs)
1422 dpt->sgsize = dpt_max_segs;
1424 /* DMA tag for mapping buffers into device visible space. */
1425 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1428 /* lowaddr */ BUS_SPACE_MAXADDR,
1429 /* highaddr */ BUS_SPACE_MAXADDR,
1431 /* filterarg */ NULL,
1432 /* maxsize */ MAXBSIZE,
1433 /* nsegments */ dpt->sgsize,
1434 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1435 /* flags */ BUS_DMA_ALLOCNOW,
1436 /* lockfunc */ busdma_lock_mutex,
1437 /* lockarg */ &Giant,
1438 &dpt->buffer_dmat) != 0) {
1439 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1445 /* DMA tag for our ccb structures and interrupt status packet */
1446 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1449 /* lowaddr */ BUS_SPACE_MAXADDR,
1450 /* highaddr */ BUS_SPACE_MAXADDR,
1452 /* filterarg */ NULL,
1453 /* maxsize */ (dpt->max_dccbs *
1454 sizeof(struct dpt_ccb)) +
1457 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1459 /* lockfunc */ busdma_lock_mutex,
1460 /* lockarg */ &Giant,
1461 &dpt->dccb_dmat) != 0) {
1462 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1468 /* Allocation for our ccbs and interrupt status packet */
1469 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1470 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1471 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1477 /* And permanently map them */
1478 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1480 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1482 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1484 /* Clear them out. */
1485 bzero(dpt->dpt_dccbs,
1486 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1488 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1490 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1491 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1492 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1495 /* Allocate our first batch of ccbs */
1496 if (dptallocccbs(dpt) == 0) {
1497 printf("dpt: dptallocccbs(dpt) == 0\n");
1501 /* Prepare for Target Mode */
1502 dpt->target_mode_enabled = 1;
1504 /* Nuke excess spaces from inquiry information */
1505 strp = dpt->board_data.vendor;
1506 for (i = 0; i < sizeof(string_sizes); i++) {
1507 index = string_sizes[i] - 1;
1508 while (index && (strp[index] == ' '))
1509 strp[index--] = '\0';
1510 strp += string_sizes[i];
1513 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1514 dpt->unit, dpt->board_data.vendor,
1515 dpt->board_data.modelNum, dpt->board_data.firmware);
1517 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1519 if (dpt->cache_type != DPT_NO_CACHE
1520 && dpt->cache_size != 0) {
1521 printf("%s Cache, ",
1522 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1523 ? "Write-Through" : "Write-Back");
1526 printf("%d CCBs\n", dpt->max_dccbs);
1534 dpt_attach(dpt_softc_t *dpt)
1536 struct cam_devq *devq;
1540 * Create the device queue for our SIM.
1542 devq = cam_simq_alloc(dpt->max_dccbs);
1546 for (i = 0; i < dpt->channels; i++) {
1548 * Construct our SIM entry
1550 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1551 dpt, dpt->unit, /*untagged*/2,
1552 /*tagged*/dpt->max_dccbs, devq);
1553 if (dpt->sims[i] == NULL) {
1555 cam_simq_free(devq);
1557 printf( "%s(): Unable to attach bus %d "
1558 "due to resource shortage\n",
1563 if (xpt_bus_register(dpt->sims[i], i) != CAM_SUCCESS) {
1564 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1565 dpt->sims[i] = NULL;
1569 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1570 cam_sim_path(dpt->sims[i]),
1571 CAM_TARGET_WILDCARD,
1572 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1573 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1574 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1575 dpt->sims[i] = NULL;
1581 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1582 dpt, SHUTDOWN_PRI_DEFAULT);
1587 dpt_detach (device_t dev)
1589 struct dpt_softc * dpt;
1592 dpt = device_get_softc(dev);
1594 for (i = 0; i < dpt->channels; i++) {
1596 xpt_async(AC_LOST_DEVICE, dpt->paths[i], NULL);
1598 xpt_free_path(dpt->paths[i]);
1599 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1600 cam_sim_free(dpt->sims[i], /*free_devq*/TRUE);
1603 dptshutdown((void *)dpt, SHUTDOWN_PRI_DEFAULT);
1605 dpt_release_resources(dev);
1613 * This is the interrupt handler for the DPT driver.
1625 u_int32_t residue_len; /* Number of bytes not transferred */
1627 dpt = (dpt_softc_t *)arg;
1629 /* First order of business is to check if this interrupt is for us */
1630 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1633 * What we want to do now, is to capture the status, all of it,
1634 * move it where it belongs, wake up whoever sleeps waiting to
1635 * process this result, and get out of here.
1637 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1638 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1639 printf("Encountered bogus status packet\n");
1640 status = dpt_inb(dpt, HA_RSTATUS);
1644 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1646 dpt->sp->ccb_busaddr = ~0;
1648 /* Ignore status packets with EOC not set */
1649 if (dpt->sp->EOC == 0) {
1650 printf("dpt%d ERROR: Request %d received with "
1651 "clear EOC.\n Marking as LOST.\n",
1652 dpt->unit, dccb->transaction_id);
1654 #ifdef DPT_HANDLE_TIMEOUTS
1655 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1657 /* This CLEARS the interrupt! */
1658 status = dpt_inb(dpt, HA_RSTATUS);
1664 * Double buffer the status information so the hardware can
1665 * work on updating the status packet while we decifer the
1666 * one we were just interrupted for.
1667 * According to Mark Salyzyn, we only need few pieces of it.
1669 hba_stat = dpt->sp->hba_stat;
1670 scsi_stat = dpt->sp->scsi_stat;
1671 residue_len = dpt->sp->residue_len;
1673 /* Clear interrupts, check for error */
1674 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1676 * Error Condition. Check for magic cookie. Exit
1677 * this test on earliest sign of non-reset condition
1680 /* Check that this is not a board reset interrupt */
1681 if (dpt_just_reset(dpt)) {
1682 printf("dpt%d: HBA rebooted.\n"
1683 " All transactions should be "
1687 printf("dpt%d: >>---->> This is incomplete, "
1688 "fix me.... <<----<<", dpt->unit);
1689 panic("DPT Rebooted");
1695 untimeout(dpttimeout, dccb, ccb->ccb_h.timeout_ch);
1696 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1697 bus_dmasync_op_t op;
1699 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1700 op = BUS_DMASYNC_POSTREAD;
1702 op = BUS_DMASYNC_POSTWRITE;
1703 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1704 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1707 /* Common Case inline... */
1708 if (hba_stat == HA_NO_ERROR) {
1709 ccb->csio.scsi_status = scsi_stat;
1710 ccb->ccb_h.status = 0;
1711 switch (scsi_stat) {
1712 case SCSI_STATUS_OK:
1713 ccb->ccb_h.status |= CAM_REQ_CMP;
1715 case SCSI_STATUS_CHECK_COND:
1716 case SCSI_STATUS_CMD_TERMINATED:
1717 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1718 ccb->csio.sense_len);
1719 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1722 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1723 /* XXX Freeze DevQ */
1726 ccb->csio.resid = residue_len;
1727 dptfreeccb(dpt, dccb);
1730 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1737 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1738 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1740 ccb->csio.resid = resid;
1743 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1746 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1748 case HA_SCSIBUS_RESET:
1749 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1750 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1753 case HA_CP_RESET: /* XXX ??? */
1754 case HA_CP_ABORT_NA: /* XXX ??? */
1755 case HA_CP_RESET_NA: /* XXX ??? */
1756 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1757 ccb->ccb_h.status = CAM_REQ_ABORTED;
1762 case HA_PCI_STABORT:
1766 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1768 case HA_UNX_MSGRJCT:
1769 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1771 case HA_UNX_BUSPHASE:
1772 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1774 case HA_UNX_BUS_FREE:
1775 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1778 case HA_RESET_STUCK:
1780 * Dead??? Can the controller get unstuck
1781 * from these conditions
1783 ccb->ccb_h.status = CAM_NO_HBA;
1785 case HA_RSENSE_FAIL:
1786 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1789 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1790 printf("Please mail this message to shimon@simon-shapiro.org\n");
1791 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1794 dptfreeccb(dpt, dccb);
1799 dpttimeout(void *arg)
1801 struct dpt_ccb *dccb;
1803 struct dpt_softc *dpt;
1806 dccb = (struct dpt_ccb *)arg;
1808 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1809 xpt_print_path(ccb->ccb_h.path);
1810 printf("CCB %p - timed out\n", (void *)dccb);
1815 * Try to clear any pending jobs. FreeBSD will loose interrupts,
1816 * leaving the controller suspended, and commands timed-out.
1817 * By calling the interrupt handler, any command thus stuck will be
1822 if ((dccb->state & DCCB_ACTIVE) == 0) {
1823 xpt_print_path(ccb->ccb_h.path);
1824 printf("CCB %p - timed out CCB already completed\n",
1830 /* Abort this particular command. Leave all others running */
1831 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1832 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1833 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1838 * Shutdown the controller and ensure that the cache is completely flushed.
1839 * Called from the shutdown_final event after all disk access has completed.
1842 dptshutdown(void *arg, int howto)
1846 dpt = (dpt_softc_t *)arg;
1848 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1852 * What we do for a shutdown, is give the DPT early power loss warning
1854 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1855 DELAY(1000 * 1000 * 5);
1856 printf("dpt%d: Controller was warned of shutdown and is now "
1857 "disabled\n", dpt->unit);
1860 /*============================================================================*/
1863 #ifdef DPT_RESET_HBA
1866 ** Function name : dpt_reset_hba
1868 ** Description : Reset the HBA and properly discard all pending work
1873 dpt_reset_hba(dpt_softc_t *dpt)
1877 dpt_ccb_t dccb, *dccbp;
1879 struct scsi_xfer *xs;
1881 /* Prepare a control block. The SCSI command part is immaterial */
1884 dccb.state = DPT_CCB_STATE_NEW;
1885 dccb.std_callback = NULL;
1886 dccb.wrbuff_callback = NULL;
1888 ccb = &dccb.eata_ccb;
1889 ccb->CP_OpCode = EATA_CMD_RESET;
1890 ccb->SCSI_Reset = 0;
1892 ccb->Auto_Req_Sen = 1;
1893 ccb->cp_id = 0; /* Should be ignored */
1897 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1898 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1899 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1900 ccb->cp_viraddr = (u_int32_t) & ccb;
1902 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1903 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1905 /* Lock up the submitted queue. We are very persistant here */
1907 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1911 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1914 /* Send the RESET message */
1915 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1916 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1917 printf("dpt%d: Failed to send the RESET message.\n"
1918 " Trying cold boot (ouch!)\n", dpt->unit);
1921 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1922 EATA_COLD_BOOT, 0, 0,
1924 panic("dpt%d: Faild to cold boot the HBA\n",
1927 #ifdef DPT_MEASURE_PERFORMANCE
1928 dpt->performance.cold_boots++;
1929 #endif /* DPT_MEASURE_PERFORMANCE */
1932 #ifdef DPT_MEASURE_PERFORMANCE
1933 dpt->performance.warm_starts++;
1934 #endif /* DPT_MEASURE_PERFORMANCE */
1936 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1939 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1940 struct scsi_xfer *xs = dccbp->xs;
1942 /* Not all transactions have xs structs */
1944 /* Tell the kernel proper this did not complete well */
1945 xs->error |= XS_SELTIMEOUT;
1946 xs->flags |= SCSI_ITSDONE;
1950 dpt_Qremove_submitted(dpt, dccbp);
1952 /* Remember, Callbacks are NOT in the standard queue */
1953 if (dccbp->std_callback != NULL) {
1954 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1958 dpt_Qpush_free(dpt, dccbp);
1963 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1964 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1967 #endif /* DPT_RESET_HBA */
1970 * Build a Command Block for target mode READ/WRITE BUFFER,
1971 * with the ``sync'' bit ON.
1973 * Although the length and offset are 24 bit fields in the command, they cannot
1974 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1975 * If they are sensless, we round them to zero offset, maximum length and
1980 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1981 dpt_ccb_t * ccb, int mode, u_int8_t command,
1982 u_int16_t length, u_int16_t offset)
1987 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
1988 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
1989 dpt->unit, length, offset);
1990 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
1995 ccb->state = DPT_CCB_STATE_NEW;
1996 ccb->std_callback = (ccb_callback) dpt_target_done;
1997 ccb->wrbuff_callback = NULL;
1999 cp = &ccb->eata_ccb;
2000 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
2003 cp->Auto_Req_Sen = 1;
2008 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
2009 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
2010 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
2011 cp->cp_viraddr = (u_int32_t) & ccb;
2013 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
2015 cp->cp_scsi_cmd = command;
2016 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
2017 cp->cp_lun = lun; /* Order is important here! */
2018 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
2019 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
2020 cp->cp_cdb[4] = (length >> 8) & 0xFF;
2021 cp->cp_cdb[5] = length & 0xFF;
2022 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
2023 cp->cp_cdb[7] = (length >> 8) & 0xFF;
2024 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
2025 cp->cp_cdb[9] = 0; /* No sync, no match bits */
2028 * This could be optimized to live in dpt_register_buffer.
2029 * We keep it here, just in case the kernel decides to reallocate pages
2031 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
2032 dpt->rw_buffer[bus][target][lun])) {
2033 printf("dpt%d: Failed to setup Scatter/Gather for "
2034 "Target-Mode buffer\n", dpt->unit);
2038 /* Setup a target mode READ command */
2041 dpt_set_target(int redo, dpt_softc_t * dpt,
2042 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
2043 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
2047 if (dpt->target_mode_enabled) {
2051 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
2052 SCSI_TM_READ_BUFFER, length, offset);
2054 ccb->transaction_id = ++dpt->commands_processed;
2056 #ifdef DPT_MEASURE_PERFORMANCE
2057 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2058 ccb->command_started = microtime_now;
2060 dpt_Qadd_waiting(dpt, ccb);
2061 dpt_sched_queue(dpt);
2065 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
2071 * Schedule a buffer to be sent to another target.
2072 * The work will be scheduled and the callback provided will be called when
2073 * the work is actually done.
2075 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
2076 * get notified of receipt of buffers.
2080 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2081 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
2082 buff_wr_done callback)
2085 dpt_ccb_t *ccb = NULL;
2088 /* This is an external call. Be a bit paranoid */
2089 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2091 dpt = TAILQ_NEXT(dpt, links)) {
2092 if (dpt->unit == unit)
2096 return (INVALID_UNIT);
2100 if (dpt->target_mode_enabled) {
2101 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2102 (lun > dpt->max_lun)) {
2103 return (INVALID_SENDER);
2105 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2106 (dpt->buffer_receiver[channel][target][lun] == NULL))
2107 return (NOT_REGISTERED);
2109 ospl = splsoftcam();
2110 /* Process the free list */
2111 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2112 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2113 " Please try later\n",
2116 return (NO_RESOURCES);
2118 /* Now grab the newest CCB */
2119 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2121 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2125 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2126 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2127 SCSI_TM_WRITE_BUFFER,
2129 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2132 ccb->transaction_id = ++dpt->commands_processed;
2134 #ifdef DPT_MEASURE_PERFORMANCE
2135 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2136 ccb->command_started = microtime_now;
2138 dpt_Qadd_waiting(dpt, ccb);
2139 dpt_sched_queue(dpt);
2144 return (DRIVER_DOWN);
2148 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2153 cp = &ccb->eata_ccb;
2156 * Remove the CCB from the waiting queue.
2157 * We do NOT put it back on the free, etc., queues as it is a special
2158 * ccb, owned by the dpt_softc of this unit.
2160 ospl = splsoftcam();
2161 dpt_Qremove_completed(dpt, ccb);
2164 #define br_channel (ccb->eata_ccb.cp_channel)
2165 #define br_target (ccb->eata_ccb.cp_id)
2166 #define br_lun (ccb->eata_ccb.cp_LUN)
2167 #define br_index [br_channel][br_target][br_lun]
2168 #define read_buffer_callback (dpt->buffer_receiver br_index )
2169 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2170 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2171 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2172 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2174 /* Different reasons for being here, you know... */
2175 switch (ccb->eata_ccb.cp_scsi_cmd) {
2176 case SCSI_TM_READ_BUFFER:
2177 if (read_buffer_callback != NULL) {
2178 /* This is a buffer generated by a kernel process */
2179 read_buffer_callback(dpt->unit, br_channel,
2182 br_offset, br_length);
2185 * This is a buffer waited for by a user (sleeping)
2191 /* We ALWAYS re-issue the same command; args are don't-care */
2192 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2195 case SCSI_TM_WRITE_BUFFER:
2196 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2197 br_offset, br_length,
2198 br_lun, ccb->status_packet.hba_stat);
2201 printf("dpt%d: %s is an unsupported command for target mode\n",
2202 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2204 ospl = splsoftcam();
2205 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2206 dpt_Qpush_free(dpt, ccb);
2212 * Use this function to register a client for a buffer read target operation.
2213 * The function you register will be called every time a buffer is received
2214 * by the target mode code.
2217 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2218 u_int8_t mode, u_int16_t length, u_int16_t offset,
2219 dpt_rec_buff callback, dpt_rb_op_t op)
2222 dpt_ccb_t *ccb = NULL;
2225 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2227 dpt = TAILQ_NEXT(dpt, links)) {
2228 if (dpt->unit == unit)
2232 return (INVALID_UNIT);
2236 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2237 return (DRIVER_DOWN);
2239 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2240 (lun > (dpt->max_lun - 1)))
2241 return (INVALID_SENDER);
2243 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2244 if (op == REGISTER_BUFFER) {
2245 /* Assign the requested callback */
2246 dpt->buffer_receiver[channel][target][lun] = callback;
2248 ospl = splsoftcam();
2250 /* Process the free list */
2251 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2252 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2253 " Please try later\n",
2256 return (NO_RESOURCES);
2258 /* Now grab the newest CCB */
2259 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2261 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2266 /* Clean up the leftover of the previous tenant */
2267 ccb->status = DPT_CCB_STATE_NEW;
2268 dpt->target_ccb[channel][target][lun] = ccb;
2270 dpt->rw_buffer[channel][target][lun] =
2271 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
2272 if (dpt->rw_buffer[channel][target][lun] == NULL) {
2273 printf("dpt%d: Failed to allocate "
2274 "Target-Mode buffer\n", dpt->unit);
2275 ospl = splsoftcam();
2276 dpt_Qpush_free(dpt, ccb);
2278 return (NO_RESOURCES);
2280 dpt_set_target(0, dpt, channel, target, lun, mode,
2281 length, offset, ccb);
2282 return (SUCCESSFULLY_REGISTERED);
2284 return (NOT_REGISTERED);
2286 if (op == REGISTER_BUFFER) {
2287 if (dpt->buffer_receiver[channel][target][lun] == callback)
2288 return (ALREADY_REGISTERED);
2290 return (REGISTERED_TO_ANOTHER);
2292 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2293 dpt->buffer_receiver[channel][target][lun] = NULL;
2294 ospl = splsoftcam();
2295 dpt_Qpush_free(dpt, ccb);
2297 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2298 return (SUCCESSFULLY_REGISTERED);
2300 return (INVALID_CALLBACK);
2306 /* Return the state of the blinking DPT LED's */
2308 dpt_blinking_led(dpt_softc_t * dpt)
2320 for (ndx = 0, state = 0, previous = 0;
2321 (ndx < 10) && (state != previous);
2324 state = dpt_inl(dpt, 1);
2327 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2328 result = dpt_inb(dpt, 5);
2335 * Execute a command which did not come from the kernel's SCSI layer.
2336 * The only way to map user commands to bus and target is to comply with the
2337 * standard DPT wire-down scheme:
2340 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2341 caddr_t cmdarg, int minor_no)
2345 int channel, target, lun;
2352 channel = minor2hba(minor_no);
2353 target = minor2target(minor_no);
2354 lun = minor2lun(minor_no);
2356 if ((channel > (dpt->channels - 1))
2357 || (target > dpt->max_id)
2358 || (lun > dpt->max_lun))
2361 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2362 /* This one is for the controller itself */
2363 if ((user_cmd->eataID[0] != 'E')
2364 || (user_cmd->eataID[1] != 'A')
2365 || (user_cmd->eataID[2] != 'T')
2366 || (user_cmd->eataID[3] != 'A')) {
2370 /* Get a DPT CCB, so we can prepare a command */
2371 ospl = splsoftcam();
2373 /* Process the free list */
2374 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2375 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2376 " Please try later\n",
2381 /* Now grab the newest CCB */
2382 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2384 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2387 /* Clean up the leftover of the previous tenant */
2388 ccb->status = DPT_CCB_STATE_NEW;
2391 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2392 sizeof(eata_ccb_t));
2394 /* We do not want to do user specified scatter/gather. Why?? */
2395 if (ccb->eata_ccb.scatter == 1)
2398 ccb->eata_ccb.Auto_Req_Sen = 1;
2399 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2400 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2401 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2402 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2403 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2404 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2406 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2407 /* Data I/O is involved in this command. Alocate buffer */
2408 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2409 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2410 M_TEMP, M_WAITOK, 0, ~0,
2411 ccb->eata_ccb.cp_datalen,
2414 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2419 printf("dpt%d: Cannot allocate %d bytes "
2420 "for EATA command\n", dpt->unit,
2421 ccb->eata_ccb.cp_datalen);
2424 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2425 if (ccb->eata_ccb.DataIn == 1) {
2426 if (copyin(usr_cmd_DMA,
2427 data, ccb->eata_ccb.cp_datalen) == -1)
2431 /* No data I/O involved here. Make sure the DPT knows that */
2432 ccb->eata_ccb.cp_datalen = 0;
2436 if (ccb->eata_ccb.FWNEST == 1)
2437 ccb->eata_ccb.FWNEST = 0;
2439 if (ccb->eata_ccb.cp_datalen != 0) {
2440 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2448 * We are required to quiet a SCSI bus.
2449 * since we do not queue comands on a bus basis,
2450 * we wait for ALL commands on a controller to complete.
2451 * In the mean time, sched_queue() will not schedule new commands.
2453 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2454 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2455 /* We wait for ALL traffic for this HBa to subside */
2456 ospl = splsoftcam();
2457 dpt->state |= DPT_HA_QUIET;
2460 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2461 huh = tsleep((void *) dpt, PCATCH | PRIBIO, "dptqt",
2465 /* Wakeup call received */
2476 /* Resume normal operation */
2477 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2478 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2479 ospl = splsoftcam();
2480 dpt->state &= ~DPT_HA_QUIET;
2484 * Schedule the command and submit it.
2485 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2489 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2491 ccb->transaction_id = ++dpt->commands_processed;
2492 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2493 ccb->result = (u_int32_t) & cmdarg;
2496 #ifdef DPT_MEASURE_PERFORMANCE
2497 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2498 ccb->command_started = microtime_now;
2501 dpt_Qadd_waiting(dpt, ccb);
2504 dpt_sched_queue(dpt);
2506 /* Wait for the command to complete */
2507 (void) tsleep((void *) ccb, PCATCH | PRIBIO, "dptucw", 100 * hz);
2509 /* Free allocated memory */
2517 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2519 int ospl = splsoftcam();
2524 * If Auto Request Sense is on, copyout the sense struct
2526 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2527 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2528 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2529 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2530 sizeof(struct scsi_sense_data))) {
2531 ccb->result = EFAULT;
2532 dpt_Qpush_free(dpt, ccb);
2538 /* If DataIn is on, copyout the data */
2539 if ((ccb->eata_ccb.DataIn == 1)
2540 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2541 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2542 dpt_Qpush_free(dpt, ccb);
2543 ccb->result = EFAULT;
2550 /* Copyout the status */
2551 result = ccb->status_packet.hba_stat;
2552 cmd_arg = (caddr_t) ccb->result;
2554 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2555 dpt_Qpush_free(dpt, ccb);
2556 ccb->result = EFAULT;
2561 /* Put the CCB back in the freelist */
2562 ccb->state |= DPT_CCB_STATE_COMPLETED;
2563 dpt_Qpush_free(dpt, ccb);
2565 /* Free allocated memory */
2570 #ifdef DPT_HANDLE_TIMEOUTS
2572 * This function walks down the SUBMITTED queue.
2573 * Every request that is too old gets aborted and marked.
2574 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2575 * We just walk the list, aborting old commands and marking them as such.
2576 * The dpt_complete function will get rid of the that were interrupted in the
2579 * This function needs to run at splcam(), as it interacts with the submitted
2580 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2581 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2582 * willbe able to pre-empt it, grab a transaction in progress (towards
2583 * destruction) and operate on it. The state of this transaction will be not
2585 * The only other option, is to lock it only as long as necessary but have
2586 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2587 * a SMP environment, the advantage is dubvious for a function that runs once
2588 * every ten seconds for few microseconds and, on systems with healthy
2589 * hardware, does not do anything anyway.
2593 dpt_handle_timeouts(dpt_softc_t * dpt)
2600 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2601 printf("dpt%d WARNING: Timeout Handling Collision\n",
2606 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2608 /* Loop through the entire submitted queue, looking for lost souls */
2609 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2611 ccb = TAILQ_NEXT(ccb, links)) {
2612 struct scsi_xfer *xs;
2613 u_int32_t age, max_age;
2616 age = dpt_time_delta(ccb->command_started, microtime_now);
2618 #define TenSec 10000000
2620 if (xs == NULL) { /* Local, non-kernel call */
2623 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2624 + DPT_TIMEOUT_FACTOR))
2626 ? (xs->timeout * (dpt->submitted_ccbs_count
2627 + DPT_TIMEOUT_FACTOR))
2632 * If a transaction is marked lost and is TWICE as old as we
2633 * care, then, and only then do we destroy it!
2635 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2636 /* Remember who is next */
2637 if (age > (max_age * 2)) {
2638 dpt_Qremove_submitted(dpt, ccb);
2639 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2640 ccb->state |= DPT_CCB_STATE_ABORTED;
2641 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2642 if (ccb->retries++ > DPT_RETRIES) {
2643 printf("dpt%d ERROR: Destroying stale "
2646 "c%db%dt%du%d (%d/%d)\n",
2647 dpt->unit, ccb->transaction_id,
2650 ccb->eata_ccb.cp_channel,
2651 ccb->eata_ccb.cp_id,
2652 ccb->eata_ccb.cp_LUN, age,
2654 #define send_ccb &ccb->eata_ccb
2655 #define ESA EATA_SPECIFIC_ABORT
2656 (void) dpt_send_immediate(dpt,
2660 dpt_Qpush_free(dpt, ccb);
2662 /* The SCSI layer should re-try */
2663 xs->error |= XS_TIMEOUT;
2664 xs->flags |= SCSI_ITSDONE;
2667 printf("dpt%d ERROR: Stale %d (%s) on "
2668 "c%db%dt%du%d (%d)\n"
2671 dpt->unit, ccb->transaction_id,
2674 ccb->eata_ccb.cp_channel,
2675 ccb->eata_ccb.cp_id,
2676 ccb->eata_ccb.cp_LUN,
2677 age, ccb->retries, DPT_RETRIES);
2679 dpt_Qpush_waiting(dpt, ccb);
2680 dpt_sched_queue(dpt);
2685 * This is a transaction that is not to be destroyed
2686 * (yet) But it is too old for our liking. We wait as
2687 * long as the upper layer thinks. Not really, we
2688 * multiply that by the number of commands in the
2689 * submitted queue + 1.
2691 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2692 (age != ~0) && (age > max_age)) {
2693 printf("dpt%d ERROR: Marking %d (%s) on "
2695 " as late after %dusec\n",
2696 dpt->unit, ccb->transaction_id,
2698 dpt->unit, ccb->eata_ccb.cp_channel,
2699 ccb->eata_ccb.cp_id,
2700 ccb->eata_ccb.cp_LUN, age);
2701 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2706 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2711 dpt_timeout(void *arg)
2713 dpt_softc_t *dpt = (dpt_softc_t *) arg;
2715 if (!(dpt->state & DPT_HA_TIMEOUTS_ACTIVE))
2716 dpt_handle_timeouts(dpt);
2718 timeout(dpt_timeout, (caddr_t) dpt, hz * 10);
2721 #endif /* DPT_HANDLE_TIMEOUTS */