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!).
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/eventhandler.h>
54 #include <sys/malloc.h>
55 #include <sys/kernel.h>
59 #include <machine/bus_memio.h>
60 #include <machine/bus_pio.h>
61 #include <machine/bus.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/cam_sim.h>
67 #include <cam/cam_xpt_sim.h>
68 #include <cam/cam_debug.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_message.h>
75 #include <dev/dpt/dpt.h>
77 /* dpt_isa.c, dpt_eisa.c, and dpt_pci.c need this in a central place */
78 int dpt_controllers_present;
80 u_long dpt_unit; /* Next unit number to use */
82 /* The linked list of softc structures */
83 struct dpt_softc_list dpt_softcs = TAILQ_HEAD_INITIALIZER(dpt_softcs);
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 u_int32_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 u_int32_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, u_int32_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)) {
450 printf("dpt: timeout in data read.\n");
454 (*p) = inw(base + HA_RDATA);
458 if (inb(base + HA_RSTATUS) & HA_SERROR) {
459 printf("dpt: error reading configuration data.\n");
463 #define BE_EATA_SIGNATURE 0x45415441
464 #define LE_EATA_SIGNATURE 0x41544145
467 * Test to see if we have a valid card.
469 if ((conf->signature == BE_EATA_SIGNATURE) ||
470 (conf->signature == LE_EATA_SIGNATURE)) {
472 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
473 inw(base + HA_RDATA);
482 * Read a configuration page into the supplied dpt_cont_t buffer.
485 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
486 u_int size, u_int page, u_int target, int extent)
496 cp = &dccb->eata_ccb;
497 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
501 cp->Auto_Req_Sen = 1;
502 cp->reqlen = sizeof(struct scsi_sense_data);
505 cp->cp_LUN = 0; /* In the EATA packet */
506 cp->cp_lun = 0; /* In the SCSI command */
508 cp->cp_scsi_cmd = INQUIRY;
511 cp->cp_extent = extent;
514 cp->cp_channel = 0; /* DNC, Interpret mode is set */
516 cp->cp_datalen = htonl(size);
521 * This could be a simple for loop, but we suspected the compiler To
522 * have optimized it a bit too much. Wait for the controller to
525 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
526 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
527 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
528 || (dpt_wait(dpt, HA_SBUSY, 0))) {
531 * RAID Drives still Spinning up? (This should only occur if
532 * the DPT controller is in a NON PC (PCI?) platform).
534 if (dpt_raid_busy(dpt)) {
535 printf("dpt%d WARNING: Get_conf() RSUS failed.\n",
542 DptStat_Reset_BUSY(dpt->sp);
545 * XXXX We might want to do something more clever than aborting at
546 * this point, like resetting (rebooting) the controller and trying
549 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
550 EATA_CMD_DMA_SEND_CP,
551 10000, 0, 0, 0)) != 0) {
552 printf("dpt%d WARNING: Get_conf() failed (%d) to send "
553 "EATA_CMD_DMA_READ_CONFIG\n",
558 /* Wait for two seconds for a response. This can be slow */
561 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
566 /* Grab the status and clear interrupts */
567 status = dpt_inb(dpt, HA_RSTATUS);
572 * Check the status carefully. Return only if the
573 * command was successful.
575 if (((status & HA_SERROR) == 0)
576 && (dpt->sp->hba_stat == 0)
577 && (dpt->sp->scsi_stat == 0)
578 && (dpt->sp->residue_len == 0))
581 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
587 /* Detect Cache parameters and size */
589 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
601 * Default setting, for best perfromance..
602 * This is what virtually all cards default to..
604 dpt->cache_type = DPT_CACHE_WRITEBACK;
607 cp = &dccb->eata_ccb;
608 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
611 /* Setup the command structure */
614 cp->Auto_Req_Sen = 1;
615 cp->reqlen = sizeof(struct scsi_sense_data);
617 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
618 cp->cp_LUN = 0; /* In the EATA packet */
619 cp->cp_lun = 0; /* In the SCSI command */
622 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
631 * Build the EATA Command Packet structure
632 * for a Log Sense Command.
634 cp->cp_cdb[0] = 0x4d;
636 cp->cp_cdb[2] = 0x40 | 0x33;
639 cp->cp_datalen = htonl(512);
642 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
643 EATA_CMD_DMA_SEND_CP,
646 printf("dpt%d WARNING: detect_cache() failed (%d) to send "
647 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
651 /* Wait for two seconds for a response. This can be slow... */
654 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
659 /* Grab the status and clear interrupts */
660 status = dpt_inb(dpt, HA_RSTATUS);
666 if (buff[0] != 0x33) {
669 bytes = DPT_HCP_LENGTH(buff);
670 param = DPT_HCP_FIRST(buff);
672 if (DPT_HCP_CODE(param) != 1) {
674 * DPT Log Page layout error
676 printf("dpt%d: NOTICE: Log Page (1) layout error\n",
680 if (!(param[4] & 0x4)) {
681 dpt->cache_type = DPT_NO_CACHE;
684 while (DPT_HCP_CODE(param) != 6) {
685 param = DPT_HCP_NEXT(param);
687 || (param >= &buff[bytes])) {
692 if (param[4] & 0x2) {
696 dpt->cache_type = DPT_NO_CACHE;
700 if (param[4] & 0x4) {
701 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
704 /* XXX This isn't correct. This log parameter only has two bytes.... */
706 dpt->cache_size = param[5]
714 dpt_poll(struct cam_sim *sim)
716 dpt_intr(cam_sim_softc(sim));
720 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
722 struct dpt_ccb *dccb;
724 struct dpt_softc *dpt;
727 dccb = (struct dpt_ccb *)arg;
729 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
733 printf("dpt%d: Unexepected error 0x%x returned from "
734 "bus_dmamap_load\n", dpt->unit, error);
735 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
736 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
737 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
739 dptfreeccb(dpt, dccb);
746 bus_dma_segment_t *end_seg;
749 end_seg = dm_segs + nseg;
751 /* Copy the segments into our SG list */
753 while (dm_segs < end_seg) {
754 sg->seg_len = htonl(dm_segs->ds_len);
755 sg->seg_addr = htonl(dm_segs->ds_addr);
761 dccb->eata_ccb.scatter = 1;
762 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
763 dccb->eata_ccb.cp_datalen =
764 htonl(nseg * sizeof(dpt_sg_t));
766 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
767 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
770 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
771 op = BUS_DMASYNC_PREREAD;
773 op = BUS_DMASYNC_PREWRITE;
775 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
778 dccb->eata_ccb.cp_dataDMA = 0;
779 dccb->eata_ccb.cp_datalen = 0;
785 * Last time we need to check if this CCB needs to
788 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
790 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
791 dptfreeccb(dpt, dccb);
797 dccb->state |= DCCB_ACTIVE;
798 ccb->ccb_h.status |= CAM_SIM_QUEUED;
799 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
800 ccb->ccb_h.timeout_ch =
801 timeout(dpttimeout, (caddr_t)dccb,
802 (ccb->ccb_h.timeout * hz) / 1000);
803 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
804 dccb->eata_ccb.cp_busaddr,
805 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
806 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
808 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
809 dptfreeccb(dpt, dccb);
817 dpt_action(struct cam_sim *sim, union ccb *ccb)
819 struct dpt_softc *dpt;
821 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
823 dpt = (struct dpt_softc *)cam_sim_softc(sim);
825 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
826 xpt_print_path(ccb->ccb_h.path);
827 printf("controller is shutdown. Aborting CCB.\n");
828 ccb->ccb_h.status = CAM_NO_HBA;
833 switch (ccb->ccb_h.func_code) {
834 /* Common cases first */
835 case XPT_SCSI_IO: /* Execute the requested I/O operation */
837 struct ccb_scsiio *csio;
838 struct ccb_hdr *ccbh;
839 struct dpt_ccb *dccb;
840 struct eata_ccb *eccb;
844 /* Max CDB length is 12 bytes */
845 if (csio->cdb_len > 12) {
846 ccb->ccb_h.status = CAM_REQ_INVALID;
850 if ((dccb = dptgetccb(dpt)) == NULL) {
854 dpt->resource_shortage = 1;
856 xpt_freeze_simq(sim, /*count*/1);
857 ccb->ccb_h.status = CAM_REQUEUE_REQ;
861 eccb = &dccb->eata_ccb;
863 /* Link dccb and ccb so we can find one from the other */
865 ccb->ccb_h.ccb_dccb_ptr = dccb;
866 ccb->ccb_h.ccb_dpt_ptr = dpt;
869 * Explicitly set all flags so that the compiler can
870 * be smart about setting them.
872 eccb->SCSI_Reset = 0;
874 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
879 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
881 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
882 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
883 eccb->reqlen = csio->sense_len;
884 eccb->cp_id = ccb->ccb_h.target_id;
885 eccb->cp_channel = cam_sim_bus(sim);
886 eccb->cp_LUN = ccb->ccb_h.target_lun;
888 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
890 eccb->cp_identify = 1;
892 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
893 && csio->tag_action != CAM_TAG_ACTION_NONE) {
894 eccb->cp_msg[0] = csio->tag_action;
895 eccb->cp_msg[1] = dccb->tag;
902 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
903 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
904 bcopy(csio->cdb_io.cdb_ptr,
905 eccb->cp_cdb, csio->cdb_len);
907 /* I guess I could map it in... */
908 ccb->ccb_h.status = CAM_REQ_INVALID;
909 dptfreeccb(dpt, dccb);
914 bcopy(csio->cdb_io.cdb_bytes,
915 eccb->cp_cdb, csio->cdb_len);
918 * If we have any data to send with this command,
919 * map it into bus space.
921 /* Only use S/G if there is a transfer */
922 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
923 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
925 * We've been given a pointer
926 * to a single buffer.
928 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
934 bus_dmamap_load(dpt->buffer_dmat,
940 if (error == EINPROGRESS) {
942 * So as to maintain ordering,
943 * freeze the controller queue
944 * until our mapping is
947 xpt_freeze_simq(sim, 1);
948 dccb->state |= CAM_RELEASE_SIMQ;
952 struct bus_dma_segment seg;
954 /* Pointer to physical buffer */
956 (bus_addr_t)csio->data_ptr;
957 seg.ds_len = csio->dxfer_len;
958 dptexecuteccb(dccb, &seg, 1, 0);
961 struct bus_dma_segment *segs;
963 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
964 panic("dpt_action - Physical "
968 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
969 panic("dpt_action - Virtual "
973 /* Just use the segments provided */
974 segs = (struct bus_dma_segment *)csio->data_ptr;
975 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
980 * Does it want them both on or both off?
981 * CAM_DIR_NONE is both on, so this code can
982 * be removed if this is also what the DPT
987 dptexecuteccb(dccb, NULL, 0, 0);
991 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
992 case XPT_ABORT: /* Abort the specified CCB */
994 ccb->ccb_h.status = CAM_REQ_INVALID;
997 case XPT_SET_TRAN_SETTINGS:
999 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1003 case XPT_GET_TRAN_SETTINGS:
1004 /* Get default/user set transfer settings for the target */
1006 struct ccb_trans_settings *cts;
1010 target_mask = 0x01 << ccb->ccb_h.target_id;
1011 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1012 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
1013 cts->bus_width = (dpt->max_id > 7)
1014 ? MSG_EXT_WDTR_BUS_8_BIT
1015 : MSG_EXT_WDTR_BUS_16_BIT;
1016 cts->sync_period = 25; /* 10MHz */
1018 if (cts->sync_period != 0)
1019 cts->sync_offset = 15;
1021 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1022 | CCB_TRANS_SYNC_OFFSET_VALID
1023 | CCB_TRANS_BUS_WIDTH_VALID
1024 | CCB_TRANS_DISC_VALID
1025 | CCB_TRANS_TQ_VALID;
1026 ccb->ccb_h.status = CAM_REQ_CMP;
1028 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1033 case XPT_CALC_GEOMETRY:
1035 struct ccb_calc_geometry *ccg;
1037 u_int32_t secs_per_cylinder;
1041 * XXX Use Adaptec translation until I find out how to
1042 * get this information from the card.
1045 size_mb = ccg->volume_size
1046 / ((1024L * 1024L) / ccg->block_size);
1049 if (size_mb > 1024 && extended) {
1051 ccg->secs_per_track = 63;
1054 ccg->secs_per_track = 32;
1056 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1057 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1058 ccb->ccb_h.status = CAM_REQ_CMP;
1062 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1065 ccb->ccb_h.status = CAM_REQ_CMP;
1069 case XPT_TERM_IO: /* Terminate the I/O process */
1071 ccb->ccb_h.status = CAM_REQ_INVALID;
1074 case XPT_PATH_INQ: /* Path routing inquiry */
1076 struct ccb_pathinq *cpi = &ccb->cpi;
1078 cpi->version_num = 1;
1079 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1080 if (dpt->max_id > 7)
1081 cpi->hba_inquiry |= PI_WIDE_16;
1082 cpi->target_sprt = 0;
1084 cpi->hba_eng_cnt = 0;
1085 cpi->max_target = dpt->max_id;
1086 cpi->max_lun = dpt->max_lun;
1087 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1088 cpi->bus_id = cam_sim_bus(sim);
1089 cpi->base_transfer_speed = 3300;
1090 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1091 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1092 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1093 cpi->unit_number = cam_sim_unit(sim);
1094 cpi->ccb_h.status = CAM_REQ_CMP;
1099 ccb->ccb_h.status = CAM_REQ_INVALID;
1106 * This routine will try to send an EATA command to the DPT HBA.
1107 * It will, by default, try 20,000 times, waiting 50us between tries.
1108 * It returns 0 on success and 1 on failure.
1109 * It is assumed to be called at splcam().
1112 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1113 u_int32_t cmd_busaddr, u_int command, u_int retries,
1114 u_int ifc, u_int code, u_int code2)
1122 * I hate this polling nonsense. Wish there was a way to tell the DPT
1123 * to go get commands at its own pace, or to interrupt when ready.
1124 * In the mean time we will measure how many itterations it really
1127 for (loop = 0; loop < retries; loop++) {
1128 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1134 if (loop < retries) {
1135 #ifdef DPT_MEASURE_PERFORMANCE
1136 if (loop > dpt->performance.max_eata_tries)
1137 dpt->performance.max_eata_tries = loop;
1139 if (loop < dpt->performance.min_eata_tries)
1140 dpt->performance.min_eata_tries = loop;
1143 #ifdef DPT_MEASURE_PERFORMANCE
1144 ++dpt->performance.command_too_busy;
1149 /* The controller is alive, advance the wedge timer */
1150 #ifdef DPT_RESET_HBA
1151 dpt->last_contact = microtime_now;
1154 if (cmd_block == NULL)
1156 #if (BYTE_ORDER == BIG_ENDIAN)
1158 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1159 | ((cmd_busaddr >> 16) & 0xFF)
1160 | ((cmd_busaddr >> 8) & 0xFF)
1161 | (cmd_busaddr & 0xFF);
1164 /* And now the address */
1165 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1167 if (command == EATA_CMD_IMMEDIATE) {
1168 if (cmd_block == NULL) {
1169 dpt_outb(dpt, HA_WCODE2, code2);
1170 dpt_outb(dpt, HA_WCODE, code);
1172 dpt_outb(dpt, HA_WIFC, ifc);
1174 dpt_outb(dpt, HA_WCOMMAND, command);
1180 /* ==================== Exported Function definitions =======================*/
1182 dpt_alloc(device_t dev, bus_space_tag_t tag, bus_space_handle_t bsh)
1184 dpt_softc_t *dpt = device_get_softc(dev);
1187 bzero(dpt, sizeof(dpt_softc_t));
1190 dpt->unit = device_get_unit(dev);
1191 SLIST_INIT(&dpt->free_dccb_list);
1192 LIST_INIT(&dpt->pending_ccb_list);
1193 TAILQ_INSERT_TAIL(&dpt_softcs, dpt, links);
1194 for (i = 0; i < MAX_CHANNELS; i++)
1195 dpt->resetlevel[i] = DPT_HA_OK;
1197 #ifdef DPT_MEASURE_PERFORMANCE
1198 dpt_reset_performance(dpt);
1199 #endif /* DPT_MEASURE_PERFORMANCE */
1204 dpt_free(struct dpt_softc *dpt)
1206 switch (dpt->init_level) {
1209 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1211 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1213 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1215 bus_dma_tag_destroy(dpt->dccb_dmat);
1217 bus_dma_tag_destroy(dpt->buffer_dmat);
1220 struct sg_map_node *sg_map;
1222 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1223 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1224 bus_dmamap_unload(dpt->sg_dmat,
1226 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1228 free(sg_map, M_DEVBUF);
1230 bus_dma_tag_destroy(dpt->sg_dmat);
1235 TAILQ_REMOVE(&dpt_softcs, dpt, links);
1238 static u_int8_t string_sizes[] =
1240 sizeof(((dpt_inq_t*)NULL)->vendor),
1241 sizeof(((dpt_inq_t*)NULL)->modelNum),
1242 sizeof(((dpt_inq_t*)NULL)->firmware),
1243 sizeof(((dpt_inq_t*)NULL)->protocol),
1247 dpt_init(struct dpt_softc *dpt)
1250 struct sg_map_node *sg_map;
1257 dpt->init_level = 0;
1258 SLIST_INIT(&dpt->sg_maps);
1260 #ifdef DPT_RESET_BOARD
1261 printf("dpt%d: resetting HBA\n", dpt->unit);
1262 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1264 /* XXX Shouldn't we poll a status register or something??? */
1266 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1267 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1268 /*lowaddr*/BUS_SPACE_MAXADDR,
1269 /*highaddr*/BUS_SPACE_MAXADDR,
1270 /*filter*/NULL, /*filterarg*/NULL,
1271 PAGE_SIZE, /*nsegments*/1,
1272 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1273 /*flags*/0, &dpt->sg_dmat) != 0) {
1280 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1281 * memory. To get the allocation size, we need to know how many
1282 * ccbs the card supports. This requires a ccb. We solve this
1283 * chicken and egg problem by allocating some re-usable S/G space
1284 * up front, and treating it as our status packet, CCB, and target
1285 * memory space for these commands.
1287 sg_map = dptallocsgmap(dpt);
1291 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1292 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1293 bzero(dccb, sizeof(*dccb));
1294 dpt->sp_physaddr = sg_map->sg_physaddr;
1295 dccb->eata_ccb.cp_dataDMA =
1296 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1297 dccb->eata_ccb.cp_busaddr = ~0;
1298 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1299 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1300 + offsetof(struct dpt_ccb, sense_data));
1302 /* Okay. Fetch our config */
1303 bzero(&dccb[1], sizeof(conf)); /* data area */
1304 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1305 sizeof(conf), 0xc1, 7, 1);
1308 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1311 bcopy(&dccb[1], &conf, sizeof(conf));
1313 bzero(&dccb[1], sizeof(dpt->board_data));
1314 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1315 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1317 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1320 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1322 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1323 (u_int8_t *)&dccb[1]);
1325 switch (ntohl(conf.splen)) {
1327 dpt->EATA_revision = 'a';
1330 dpt->EATA_revision = 'b';
1333 dpt->EATA_revision = 'c';
1336 dpt->EATA_revision = 'z';
1339 dpt->EATA_revision = '?';
1342 dpt->max_id = conf.MAX_ID;
1343 dpt->max_lun = conf.MAX_LUN;
1344 dpt->irq = conf.IRQ;
1345 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1346 dpt->channels = conf.MAX_CHAN + 1;
1347 dpt->state |= DPT_HA_OK;
1349 dpt->primary = FALSE;
1351 dpt->primary = TRUE;
1353 dpt->more_support = conf.MORE_support;
1355 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1356 dpt->immediate_support = 1;
1358 dpt->immediate_support = 0;
1360 dpt->broken_INQUIRY = FALSE;
1362 dpt->cplen = ntohl(conf.cplen);
1363 dpt->cppadlen = ntohs(conf.cppadlen);
1364 dpt->max_dccbs = ntohs(conf.queuesiz);
1366 if (dpt->max_dccbs > 256) {
1367 printf("dpt%d: Max CCBs reduced from %d to "
1368 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1369 dpt->max_dccbs = 256;
1372 dpt->hostid[0] = conf.scsi_id0;
1373 dpt->hostid[1] = conf.scsi_id1;
1374 dpt->hostid[2] = conf.scsi_id2;
1379 dpt->sgsize = ntohs(conf.SGsiz);
1381 /* We can only get 64k buffers, so don't bother to waste space. */
1382 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1385 if (dpt->sgsize > dpt_max_segs)
1386 dpt->sgsize = dpt_max_segs;
1388 /* DMA tag for mapping buffers into device visible space. */
1389 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1390 /*lowaddr*/BUS_SPACE_MAXADDR,
1391 /*highaddr*/BUS_SPACE_MAXADDR,
1392 /*filter*/NULL, /*filterarg*/NULL,
1393 /*maxsize*/MAXBSIZE, /*nsegments*/dpt->sgsize,
1394 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1395 /*flags*/BUS_DMA_ALLOCNOW,
1396 &dpt->buffer_dmat) != 0) {
1397 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1403 /* DMA tag for our ccb structures and interrupt status packet */
1404 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1405 /*lowaddr*/BUS_SPACE_MAXADDR,
1406 /*highaddr*/BUS_SPACE_MAXADDR,
1407 /*filter*/NULL, /*filterarg*/NULL,
1408 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1411 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1412 /*flags*/0, &dpt->dccb_dmat) != 0) {
1413 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1419 /* Allocation for our ccbs and interrupt status packet */
1420 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1421 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1422 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1428 /* And permanently map them */
1429 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1431 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1433 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1435 /* Clear them out. */
1436 bzero(dpt->dpt_dccbs,
1437 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1439 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1441 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1442 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1443 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1446 /* Allocate our first batch of ccbs */
1447 if (dptallocccbs(dpt) == 0) {
1448 printf("dpt: dptallocccbs(dpt) == 0\n");
1452 /* Prepare for Target Mode */
1453 dpt->target_mode_enabled = 1;
1455 /* Nuke excess spaces from inquiry information */
1456 strp = dpt->board_data.vendor;
1457 for (i = 0; i < sizeof(string_sizes); i++) {
1458 index = string_sizes[i] - 1;
1459 while (index && (strp[index] == ' '))
1460 strp[index--] = '\0';
1461 strp += string_sizes[i];
1464 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1465 dpt->unit, dpt->board_data.vendor,
1466 dpt->board_data.modelNum, dpt->board_data.firmware);
1468 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1470 if (dpt->cache_type != DPT_NO_CACHE
1471 && dpt->cache_size != 0) {
1472 printf("%s Cache, ",
1473 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1474 ? "Write-Through" : "Write-Back");
1477 printf("%d CCBs\n", dpt->max_dccbs);
1485 dpt_attach(dpt_softc_t *dpt)
1487 struct cam_devq *devq;
1491 * Create the device queue for our SIM.
1493 devq = cam_simq_alloc(dpt->max_dccbs);
1497 for (i = 0; i < dpt->channels; i++) {
1499 * Construct our SIM entry
1501 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1502 dpt, dpt->unit, /*untagged*/2,
1503 /*tagged*/dpt->max_dccbs, devq);
1504 if (xpt_bus_register(dpt->sims[i], i) != CAM_SUCCESS) {
1505 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1509 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1510 cam_sim_path(dpt->sims[i]),
1511 CAM_TARGET_WILDCARD,
1512 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1513 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1514 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1520 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1521 dpt, SHUTDOWN_PRI_DEFAULT);
1527 * This is the interrupt handler for the DPT driver.
1539 u_int32_t residue_len; /* Number of bytes not transferred */
1541 dpt = (dpt_softc_t *)arg;
1543 /* First order of business is to check if this interrupt is for us */
1544 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1547 * What we want to do now, is to capture the status, all of it,
1548 * move it where it belongs, wake up whoever sleeps waiting to
1549 * process this result, and get out of here.
1551 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1552 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1553 printf("Encountered bogus status packet\n");
1554 status = dpt_inb(dpt, HA_RSTATUS);
1558 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1560 dpt->sp->ccb_busaddr = ~0;
1562 /* Ignore status packets with EOC not set */
1563 if (dpt->sp->EOC == 0) {
1564 printf("dpt%d ERROR: Request %d recieved with "
1565 "clear EOC.\n Marking as LOST.\n",
1566 dpt->unit, dccb->transaction_id);
1568 #ifdef DPT_HANDLE_TIMEOUTS
1569 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1571 /* This CLEARS the interrupt! */
1572 status = dpt_inb(dpt, HA_RSTATUS);
1578 * Double buffer the status information so the hardware can
1579 * work on updating the status packet while we decifer the
1580 * one we were just interrupted for.
1581 * According to Mark Salyzyn, we only need few pieces of it.
1583 hba_stat = dpt->sp->hba_stat;
1584 scsi_stat = dpt->sp->scsi_stat;
1585 residue_len = dpt->sp->residue_len;
1587 /* Clear interrupts, check for error */
1588 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1590 * Error Condition. Check for magic cookie. Exit
1591 * this test on earliest sign of non-reset condition
1594 /* Check that this is not a board reset interrupt */
1595 if (dpt_just_reset(dpt)) {
1596 printf("dpt%d: HBA rebooted.\n"
1597 " All transactions should be "
1601 printf("dpt%d: >>---->> This is incomplete, "
1602 "fix me.... <<----<<", dpt->unit);
1603 panic("DPT Rebooted");
1609 untimeout(dpttimeout, dccb, ccb->ccb_h.timeout_ch);
1610 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1611 bus_dmasync_op_t op;
1613 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1614 op = BUS_DMASYNC_POSTREAD;
1616 op = BUS_DMASYNC_POSTWRITE;
1617 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1618 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1621 /* Common Case inline... */
1622 if (hba_stat == HA_NO_ERROR) {
1623 ccb->csio.scsi_status = scsi_stat;
1624 ccb->ccb_h.status = 0;
1625 switch (scsi_stat) {
1626 case SCSI_STATUS_OK:
1627 ccb->ccb_h.status |= CAM_REQ_CMP;
1629 case SCSI_STATUS_CHECK_COND:
1630 case SCSI_STATUS_CMD_TERMINATED:
1631 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1632 ccb->csio.sense_len);
1633 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1636 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1637 /* XXX Freeze DevQ */
1640 ccb->csio.resid = residue_len;
1641 dptfreeccb(dpt, dccb);
1644 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1651 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1652 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1654 ccb->csio.resid = resid;
1657 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1660 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1662 case HA_SCSIBUS_RESET:
1663 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1664 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1667 case HA_CP_RESET: /* XXX ??? */
1668 case HA_CP_ABORT_NA: /* XXX ??? */
1669 case HA_CP_RESET_NA: /* XXX ??? */
1670 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1671 ccb->ccb_h.status = CAM_REQ_ABORTED;
1676 case HA_PCI_STABORT:
1680 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1682 case HA_UNX_MSGRJCT:
1683 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1685 case HA_UNX_BUSPHASE:
1686 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1688 case HA_UNX_BUS_FREE:
1689 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1692 case HA_RESET_STUCK:
1694 * Dead??? Can the controller get unstuck
1695 * from these conditions
1697 ccb->ccb_h.status = CAM_NO_HBA;
1699 case HA_RSENSE_FAIL:
1700 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1703 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1704 printf("Please mail this message to shimon@simon-shapiro.org\n");
1705 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1708 dptfreeccb(dpt, dccb);
1713 dpttimeout(void *arg)
1715 struct dpt_ccb *dccb;
1717 struct dpt_softc *dpt;
1720 dccb = (struct dpt_ccb *)arg;
1722 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1723 xpt_print_path(ccb->ccb_h.path);
1724 printf("CCB %p - timed out\n", (void *)dccb);
1729 * Try to clear any pending jobs. FreeBSD will loose interrupts,
1730 * leaving the controller suspended, and commands timed-out.
1731 * By calling the interrupt handler, any command thus stuck will be
1736 if ((dccb->state & DCCB_ACTIVE) == 0) {
1737 xpt_print_path(ccb->ccb_h.path);
1738 printf("CCB %p - timed out CCB already completed\n",
1744 /* Abort this particular command. Leave all others running */
1745 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1746 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1747 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1752 * Shutdown the controller and ensure that the cache is completely flushed.
1753 * Called from the shutdown_final event after all disk access has completed.
1756 dptshutdown(void *arg, int howto)
1760 dpt = (dpt_softc_t *)arg;
1762 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1766 * What we do for a shutdown, is give the DPT early power loss warning
1768 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1769 DELAY(1000 * 1000 * 5);
1770 printf("dpt%d: Controller was warned of shutdown and is now "
1771 "disabled\n", dpt->unit);
1774 /*============================================================================*/
1777 #ifdef DPT_RESET_HBA
1780 ** Function name : dpt_reset_hba
1782 ** Description : Reset the HBA and properly discard all pending work
1787 dpt_reset_hba(dpt_softc_t *dpt)
1791 dpt_ccb_t dccb, *dccbp;
1793 struct scsi_xfer *xs;
1795 /* Prepare a control block. The SCSI command part is immaterial */
1798 dccb.state = DPT_CCB_STATE_NEW;
1799 dccb.std_callback = NULL;
1800 dccb.wrbuff_callback = NULL;
1802 ccb = &dccb.eata_ccb;
1803 ccb->CP_OpCode = EATA_CMD_RESET;
1804 ccb->SCSI_Reset = 0;
1806 ccb->Auto_Req_Sen = 1;
1807 ccb->cp_id = 0; /* Should be ignored */
1811 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1812 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1813 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1814 ccb->cp_viraddr = (u_int32_t) & ccb;
1816 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1817 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1819 /* Lock up the submitted queue. We are very persistant here */
1821 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1825 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1828 /* Send the RESET message */
1829 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1830 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1831 printf("dpt%d: Failed to send the RESET message.\n"
1832 " Trying cold boot (ouch!)\n", dpt->unit);
1835 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1836 EATA_COLD_BOOT, 0, 0,
1838 panic("dpt%d: Faild to cold boot the HBA\n",
1841 #ifdef DPT_MEASURE_PERFORMANCE
1842 dpt->performance.cold_boots++;
1843 #endif /* DPT_MEASURE_PERFORMANCE */
1846 #ifdef DPT_MEASURE_PERFORMANCE
1847 dpt->performance.warm_starts++;
1848 #endif /* DPT_MEASURE_PERFORMANCE */
1850 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1853 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1854 struct scsi_xfer *xs = dccbp->xs;
1856 /* Not all transactions have xs structs */
1858 /* Tell the kernel proper this did not complete well */
1859 xs->error |= XS_SELTIMEOUT;
1860 xs->flags |= SCSI_ITSDONE;
1864 dpt_Qremove_submitted(dpt, dccbp);
1866 /* Remember, Callbacks are NOT in the standard queue */
1867 if (dccbp->std_callback != NULL) {
1868 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1872 dpt_Qpush_free(dpt, dccbp);
1877 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1878 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1881 #endif /* DPT_RESET_HBA */
1884 * Build a Command Block for target mode READ/WRITE BUFFER,
1885 * with the ``sync'' bit ON.
1887 * Although the length and offset are 24 bit fields in the command, they cannot
1888 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1889 * If they are sensless, we round them to zero offset, maximum length and
1894 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1895 dpt_ccb_t * ccb, int mode, u_int8_t command,
1896 u_int16_t length, u_int16_t offset)
1901 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
1902 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
1903 dpt->unit, length, offset);
1904 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
1909 ccb->state = DPT_CCB_STATE_NEW;
1910 ccb->std_callback = (ccb_callback) dpt_target_done;
1911 ccb->wrbuff_callback = NULL;
1913 cp = &ccb->eata_ccb;
1914 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
1917 cp->Auto_Req_Sen = 1;
1922 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
1923 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
1924 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
1925 cp->cp_viraddr = (u_int32_t) & ccb;
1927 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1929 cp->cp_scsi_cmd = command;
1930 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
1931 cp->cp_lun = lun; /* Order is important here! */
1932 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
1933 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
1934 cp->cp_cdb[4] = (length >> 8) & 0xFF;
1935 cp->cp_cdb[5] = length & 0xFF;
1936 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
1937 cp->cp_cdb[7] = (length >> 8) & 0xFF;
1938 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
1939 cp->cp_cdb[9] = 0; /* No sync, no match bits */
1942 * This could be optimized to live in dpt_register_buffer.
1943 * We keep it here, just in case the kernel decides to reallocate pages
1945 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
1946 dpt->rw_buffer[bus][target][lun])) {
1947 printf("dpt%d: Failed to setup Scatter/Gather for "
1948 "Target-Mode buffer\n", dpt->unit);
1952 /* Setup a target mode READ command */
1955 dpt_set_target(int redo, dpt_softc_t * dpt,
1956 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
1957 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
1961 if (dpt->target_mode_enabled) {
1965 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
1966 SCSI_TM_READ_BUFFER, length, offset);
1968 ccb->transaction_id = ++dpt->commands_processed;
1970 #ifdef DPT_MEASURE_PERFORMANCE
1971 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
1972 ccb->command_started = microtime_now;
1974 dpt_Qadd_waiting(dpt, ccb);
1975 dpt_sched_queue(dpt);
1979 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
1985 * Schedule a buffer to be sent to another target.
1986 * The work will be scheduled and the callback provided will be called when
1987 * the work is actually done.
1989 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
1990 * get notified of receipt of buffers.
1994 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
1995 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
1996 buff_wr_done callback)
1999 dpt_ccb_t *ccb = NULL;
2002 /* This is an external call. Be a bit paranoid */
2003 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2005 dpt = TAILQ_NEXT(dpt, links)) {
2006 if (dpt->unit == unit)
2010 return (INVALID_UNIT);
2014 if (dpt->target_mode_enabled) {
2015 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2016 (lun > dpt->max_lun)) {
2017 return (INVALID_SENDER);
2019 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2020 (dpt->buffer_receiver[channel][target][lun] == NULL))
2021 return (NOT_REGISTERED);
2023 ospl = splsoftcam();
2024 /* Process the free list */
2025 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2026 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2027 " Please try later\n",
2030 return (NO_RESOURCES);
2032 /* Now grab the newest CCB */
2033 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2035 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2039 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2040 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2041 SCSI_TM_WRITE_BUFFER,
2043 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2046 ccb->transaction_id = ++dpt->commands_processed;
2048 #ifdef DPT_MEASURE_PERFORMANCE
2049 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2050 ccb->command_started = microtime_now;
2052 dpt_Qadd_waiting(dpt, ccb);
2053 dpt_sched_queue(dpt);
2058 return (DRIVER_DOWN);
2062 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2067 cp = &ccb->eata_ccb;
2070 * Remove the CCB from the waiting queue.
2071 * We do NOT put it back on the free, etc., queues as it is a special
2072 * ccb, owned by the dpt_softc of this unit.
2074 ospl = splsoftcam();
2075 dpt_Qremove_completed(dpt, ccb);
2078 #define br_channel (ccb->eata_ccb.cp_channel)
2079 #define br_target (ccb->eata_ccb.cp_id)
2080 #define br_lun (ccb->eata_ccb.cp_LUN)
2081 #define br_index [br_channel][br_target][br_lun]
2082 #define read_buffer_callback (dpt->buffer_receiver br_index )
2083 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2084 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2085 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2086 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2088 /* Different reasons for being here, you know... */
2089 switch (ccb->eata_ccb.cp_scsi_cmd) {
2090 case SCSI_TM_READ_BUFFER:
2091 if (read_buffer_callback != NULL) {
2092 /* This is a buffer generated by a kernel process */
2093 read_buffer_callback(dpt->unit, br_channel,
2096 br_offset, br_length);
2099 * This is a buffer waited for by a user (sleeping)
2105 /* We ALWAYS re-issue the same command; args are don't-care */
2106 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2109 case SCSI_TM_WRITE_BUFFER:
2110 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2111 br_offset, br_length,
2112 br_lun, ccb->status_packet.hba_stat);
2115 printf("dpt%d: %s is an unsupported command for target mode\n",
2116 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2118 ospl = splsoftcam();
2119 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2120 dpt_Qpush_free(dpt, ccb);
2126 * Use this function to register a client for a buffer read target operation.
2127 * The function you register will be called every time a buffer is received
2128 * by the target mode code.
2131 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2132 u_int8_t mode, u_int16_t length, u_int16_t offset,
2133 dpt_rec_buff callback, dpt_rb_op_t op)
2136 dpt_ccb_t *ccb = NULL;
2139 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2141 dpt = TAILQ_NEXT(dpt, links)) {
2142 if (dpt->unit == unit)
2146 return (INVALID_UNIT);
2150 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2151 return (DRIVER_DOWN);
2153 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2154 (lun > (dpt->max_lun - 1)))
2155 return (INVALID_SENDER);
2157 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2158 if (op == REGISTER_BUFFER) {
2159 /* Assign the requested callback */
2160 dpt->buffer_receiver[channel][target][lun] = callback;
2162 ospl = splsoftcam();
2164 /* Process the free list */
2165 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2166 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2167 " Please try later\n",
2170 return (NO_RESOURCES);
2172 /* Now grab the newest CCB */
2173 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2175 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2180 /* Clean up the leftover of the previous tenant */
2181 ccb->status = DPT_CCB_STATE_NEW;
2182 dpt->target_ccb[channel][target][lun] = ccb;
2184 dpt->rw_buffer[channel][target][lun] =
2185 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
2186 if (dpt->rw_buffer[channel][target][lun] == NULL) {
2187 printf("dpt%d: Failed to allocate "
2188 "Target-Mode buffer\n", dpt->unit);
2189 ospl = splsoftcam();
2190 dpt_Qpush_free(dpt, ccb);
2192 return (NO_RESOURCES);
2194 dpt_set_target(0, dpt, channel, target, lun, mode,
2195 length, offset, ccb);
2196 return (SUCCESSFULLY_REGISTERED);
2198 return (NOT_REGISTERED);
2200 if (op == REGISTER_BUFFER) {
2201 if (dpt->buffer_receiver[channel][target][lun] == callback)
2202 return (ALREADY_REGISTERED);
2204 return (REGISTERED_TO_ANOTHER);
2206 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2207 dpt->buffer_receiver[channel][target][lun] = NULL;
2208 ospl = splsoftcam();
2209 dpt_Qpush_free(dpt, ccb);
2211 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2212 return (SUCCESSFULLY_REGISTERED);
2214 return (INVALID_CALLBACK);
2220 /* Return the state of the blinking DPT LED's */
2222 dpt_blinking_led(dpt_softc_t * dpt)
2234 for (ndx = 0, state = 0, previous = 0;
2235 (ndx < 10) && (state != previous);
2238 state = dpt_inl(dpt, 1);
2241 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2242 result = dpt_inb(dpt, 5);
2249 * Execute a command which did not come from the kernel's SCSI layer.
2250 * The only way to map user commands to bus and target is to comply with the
2251 * standard DPT wire-down scheme:
2254 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2255 caddr_t cmdarg, int minor_no)
2259 int channel, target, lun;
2266 channel = minor2hba(minor_no);
2267 target = minor2target(minor_no);
2268 lun = minor2lun(minor_no);
2270 if ((channel > (dpt->channels - 1))
2271 || (target > dpt->max_id)
2272 || (lun > dpt->max_lun))
2275 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2276 /* This one is for the controller itself */
2277 if ((user_cmd->eataID[0] != 'E')
2278 || (user_cmd->eataID[1] != 'A')
2279 || (user_cmd->eataID[2] != 'T')
2280 || (user_cmd->eataID[3] != 'A')) {
2284 /* Get a DPT CCB, so we can prepare a command */
2285 ospl = splsoftcam();
2287 /* Process the free list */
2288 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2289 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2290 " Please try later\n",
2295 /* Now grab the newest CCB */
2296 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2298 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2301 /* Clean up the leftover of the previous tenant */
2302 ccb->status = DPT_CCB_STATE_NEW;
2305 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2306 sizeof(eata_ccb_t));
2308 /* We do not want to do user specified scatter/gather. Why?? */
2309 if (ccb->eata_ccb.scatter == 1)
2312 ccb->eata_ccb.Auto_Req_Sen = 1;
2313 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2314 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2315 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2316 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2317 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2318 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2320 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2321 /* Data I/O is involved in this command. Alocate buffer */
2322 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2323 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2324 M_TEMP, M_WAITOK, 0, ~0,
2325 ccb->eata_ccb.cp_datalen,
2328 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2333 printf("dpt%d: Cannot allocate %d bytes "
2334 "for EATA command\n", dpt->unit,
2335 ccb->eata_ccb.cp_datalen);
2338 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2339 if (ccb->eata_ccb.DataIn == 1) {
2340 if (copyin(usr_cmd_DMA,
2341 data, ccb->eata_ccb.cp_datalen) == -1)
2345 /* No data I/O involved here. Make sure the DPT knows that */
2346 ccb->eata_ccb.cp_datalen = 0;
2350 if (ccb->eata_ccb.FWNEST == 1)
2351 ccb->eata_ccb.FWNEST = 0;
2353 if (ccb->eata_ccb.cp_datalen != 0) {
2354 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2362 * We are required to quiet a SCSI bus.
2363 * since we do not queue comands on a bus basis,
2364 * we wait for ALL commands on a controller to complete.
2365 * In the mean time, sched_queue() will not schedule new commands.
2367 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2368 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2369 /* We wait for ALL traffic for this HBa to subside */
2370 ospl = splsoftcam();
2371 dpt->state |= DPT_HA_QUIET;
2374 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2375 huh = tsleep((void *) dpt, PCATCH | PRIBIO, "dptqt",
2379 /* Wakeup call received */
2390 /* Resume normal operation */
2391 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2392 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2393 ospl = splsoftcam();
2394 dpt->state &= ~DPT_HA_QUIET;
2398 * Schedule the command and submit it.
2399 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2403 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2405 ccb->transaction_id = ++dpt->commands_processed;
2406 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2407 ccb->result = (u_int32_t) & cmdarg;
2410 #ifdef DPT_MEASURE_PERFORMANCE
2411 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2412 ccb->command_started = microtime_now;
2415 dpt_Qadd_waiting(dpt, ccb);
2418 dpt_sched_queue(dpt);
2420 /* Wait for the command to complete */
2421 (void) tsleep((void *) ccb, PCATCH | PRIBIO, "dptucw", 100 * hz);
2423 /* Free allocated memory */
2431 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2433 int ospl = splsoftcam();
2438 * If Auto Request Sense is on, copyout the sense struct
2440 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2441 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2442 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2443 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2444 sizeof(struct scsi_sense_data))) {
2445 ccb->result = EFAULT;
2446 dpt_Qpush_free(dpt, ccb);
2452 /* If DataIn is on, copyout the data */
2453 if ((ccb->eata_ccb.DataIn == 1)
2454 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2455 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2456 dpt_Qpush_free(dpt, ccb);
2457 ccb->result = EFAULT;
2464 /* Copyout the status */
2465 result = ccb->status_packet.hba_stat;
2466 cmd_arg = (caddr_t) ccb->result;
2468 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2469 dpt_Qpush_free(dpt, ccb);
2470 ccb->result = EFAULT;
2475 /* Put the CCB back in the freelist */
2476 ccb->state |= DPT_CCB_STATE_COMPLETED;
2477 dpt_Qpush_free(dpt, ccb);
2479 /* Free allocated memory */
2484 #ifdef DPT_HANDLE_TIMEOUTS
2486 * This function walks down the SUBMITTED queue.
2487 * Every request that is too old gets aborted and marked.
2488 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2489 * We just walk the list, aborting old commands and marking them as such.
2490 * The dpt_complete function will get rid of the that were interrupted in the
2493 * This function needs to run at splcam(), as it interacts with the submitted
2494 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2495 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2496 * willbe able to pre-empt it, grab a transaction in progress (towards
2497 * destruction) and operate on it. The state of this transaction will be not
2499 * The only other option, is to lock it only as long as necessary but have
2500 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2501 * a SMP environment, the advantage is dubvious for a function that runs once
2502 * every ten seconds for few microseconds and, on systems with healthy
2503 * hardware, does not do anything anyway.
2507 dpt_handle_timeouts(dpt_softc_t * dpt)
2514 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2515 printf("dpt%d WARNING: Timeout Handling Collision\n",
2520 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2522 /* Loop through the entire submitted queue, looking for lost souls */
2523 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2525 ccb = TAILQ_NEXT(ccb, links)) {
2526 struct scsi_xfer *xs;
2527 u_int32_t age, max_age;
2530 age = dpt_time_delta(ccb->command_started, microtime_now);
2532 #define TenSec 10000000
2534 if (xs == NULL) { /* Local, non-kernel call */
2537 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2538 + DPT_TIMEOUT_FACTOR))
2540 ? (xs->timeout * (dpt->submitted_ccbs_count
2541 + DPT_TIMEOUT_FACTOR))
2546 * If a transaction is marked lost and is TWICE as old as we
2547 * care, then, and only then do we destroy it!
2549 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2550 /* Remember who is next */
2551 if (age > (max_age * 2)) {
2552 dpt_Qremove_submitted(dpt, ccb);
2553 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2554 ccb->state |= DPT_CCB_STATE_ABORTED;
2555 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2556 if (ccb->retries++ > DPT_RETRIES) {
2557 printf("dpt%d ERROR: Destroying stale "
2560 "c%db%dt%du%d (%d/%d)\n",
2561 dpt->unit, ccb->transaction_id,
2564 ccb->eata_ccb.cp_channel,
2565 ccb->eata_ccb.cp_id,
2566 ccb->eata_ccb.cp_LUN, age,
2568 #define send_ccb &ccb->eata_ccb
2569 #define ESA EATA_SPECIFIC_ABORT
2570 (void) dpt_send_immediate(dpt,
2574 dpt_Qpush_free(dpt, ccb);
2576 /* The SCSI layer should re-try */
2577 xs->error |= XS_TIMEOUT;
2578 xs->flags |= SCSI_ITSDONE;
2581 printf("dpt%d ERROR: Stale %d (%s) on "
2582 "c%db%dt%du%d (%d)\n"
2585 dpt->unit, ccb->transaction_id,
2588 ccb->eata_ccb.cp_channel,
2589 ccb->eata_ccb.cp_id,
2590 ccb->eata_ccb.cp_LUN,
2591 age, ccb->retries, DPT_RETRIES);
2593 dpt_Qpush_waiting(dpt, ccb);
2594 dpt_sched_queue(dpt);
2599 * This is a transaction that is not to be destroyed
2600 * (yet) But it is too old for our liking. We wait as
2601 * long as the upper layer thinks. Not really, we
2602 * multiply that by the number of commands in the
2603 * submitted queue + 1.
2605 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2606 (age != ~0) && (age > max_age)) {
2607 printf("dpt%d ERROR: Marking %d (%s) on "
2609 " as late after %dusec\n",
2610 dpt->unit, ccb->transaction_id,
2612 dpt->unit, ccb->eata_ccb.cp_channel,
2613 ccb->eata_ccb.cp_id,
2614 ccb->eata_ccb.cp_LUN, age);
2615 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2620 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2625 dpt_timeout(void *arg)
2627 dpt_softc_t *dpt = (dpt_softc_t *) arg;
2629 if (!(dpt->state & DPT_HA_TIMEOUTS_ACTIVE))
2630 dpt_handle_timeouts(dpt);
2632 timeout(dpt_timeout, (caddr_t) dpt, hz * 10);
2635 #endif /* DPT_HANDLE_TIMEOUTS */