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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * dpt_scsi.c: SCSI dependant code for the DPT driver
36 * credits: Assisted by Mike Neuffer in the early low level DPT code
37 * Thanx to Mark Salyzyn of DPT for his assistance.
38 * Special thanx to Justin Gibbs for invaluable help in
39 * making this driver look and work like a FreeBSD component.
40 * Last but not least, many thanx to UCB and the FreeBSD
41 * team for creating and maintaining such a wonderful O/S.
43 * TODO: * Add ISA probe code.
44 * * Add driver-level RAID-0. This will allow interoperability with
45 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
46 * arrays that span controllers (Wow!).
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/eventhandler.h>
57 #include <sys/malloc.h>
58 #include <sys/kernel.h>
62 #include <machine/bus.h>
64 #include <machine/resource.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);
111 static __inline int dpt_pio_wait (u_int32_t, u_int, u_int, u_int);
113 static __inline int dpt_wait(dpt_softc_t *dpt, u_int bits,
115 static __inline struct dpt_ccb* dptgetccb(struct dpt_softc *dpt);
116 static __inline void dptfreeccb(struct dpt_softc *dpt,
117 struct dpt_ccb *dccb);
118 static __inline bus_addr_t dptccbvtop(struct dpt_softc *dpt,
119 struct dpt_ccb *dccb);
121 static __inline int dpt_send_immediate(dpt_softc_t *dpt,
122 eata_ccb_t *cmd_block,
123 u_int32_t cmd_busaddr,
125 u_int ifc, u_int code,
128 /* ==================== Private Function declarations =======================*/
129 static void dptmapmem(void *arg, bus_dma_segment_t *segs,
130 int nseg, int error);
132 static struct sg_map_node*
133 dptallocsgmap(struct dpt_softc *dpt);
135 static int dptallocccbs(dpt_softc_t *dpt);
137 static int dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb,
138 u_int32_t dccb_busaddr, u_int size,
139 u_int page, u_int target, int extent);
140 static void dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb,
141 u_int32_t dccb_busaddr,
144 static void dpt_poll(struct cam_sim *sim);
146 static void dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
147 int nseg, int error);
149 static void dpt_action(struct cam_sim *sim, union ccb *ccb);
151 static int dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd,
152 u_int32_t cmd_busaddr,
153 u_int command, u_int retries,
154 u_int ifc, u_int code,
156 static void dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb,
157 union ccb *ccb, u_int hba_stat,
158 u_int scsi_stat, u_int32_t resid);
160 static void dpttimeout(void *arg);
161 static void dptshutdown(void *arg, int howto);
163 /* ================= Private Inline Function definitions ====================*/
165 dpt_just_reset(dpt_softc_t * dpt)
167 if ((dpt_inb(dpt, 2) == 'D')
168 && (dpt_inb(dpt, 3) == 'P')
169 && (dpt_inb(dpt, 4) == 'T')
170 && (dpt_inb(dpt, 5) == 'H'))
177 dpt_raid_busy(dpt_softc_t * dpt)
179 if ((dpt_inb(dpt, 0) == 'D')
180 && (dpt_inb(dpt, 1) == 'P')
181 && (dpt_inb(dpt, 2) == 'T'))
189 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
194 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
195 c = inb(base + reg) & bits;
206 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
211 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
212 c = dpt_inb(dpt, HA_RSTATUS) & bits;
221 static __inline struct dpt_ccb*
222 dptgetccb(struct dpt_softc *dpt)
224 struct dpt_ccb* dccb;
228 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
229 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
231 } else if (dpt->total_dccbs < dpt->max_dccbs) {
233 dccb = SLIST_FIRST(&dpt->free_dccb_list);
235 printf("dpt%d: Can't malloc DCCB\n", dpt->unit);
237 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
247 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
252 if ((dccb->state & DCCB_ACTIVE) != 0)
253 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
254 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
255 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
256 else if (dpt->resource_shortage != 0
257 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
258 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
259 dpt->resource_shortage = FALSE;
261 dccb->state = DCCB_FREE;
262 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
267 static __inline bus_addr_t
268 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
270 return (dpt->dpt_ccb_busbase
271 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
274 static __inline struct dpt_ccb *
275 dptccbptov(struct dpt_softc *dpt, bus_addr_t busaddr)
277 return (dpt->dpt_dccbs
278 + ((struct dpt_ccb *)busaddr
279 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
283 * Send a command for immediate execution by the DPT
284 * See above function for IMPORTANT notes.
287 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
288 u_int32_t cmd_busaddr, u_int retries,
289 u_int ifc, u_int code, u_int code2)
291 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
292 EATA_CMD_IMMEDIATE, retries, ifc,
297 /* ===================== Private Function definitions =======================*/
299 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
301 bus_addr_t *busaddrp;
303 busaddrp = (bus_addr_t *)arg;
304 *busaddrp = segs->ds_addr;
307 static struct sg_map_node *
308 dptallocsgmap(struct dpt_softc *dpt)
310 struct sg_map_node *sg_map;
312 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
317 /* Allocate S/G space for the next batch of CCBS */
318 if (bus_dmamem_alloc(dpt->sg_dmat, (void **)&sg_map->sg_vaddr,
319 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
320 free(sg_map, M_DEVBUF);
324 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
325 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
328 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
334 * Allocate another chunk of CCB's. Return count of entries added.
335 * Assumed to be called at splcam().
338 dptallocccbs(dpt_softc_t *dpt)
340 struct dpt_ccb *next_ccb;
341 struct sg_map_node *sg_map;
347 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
349 if (next_ccb == dpt->dpt_dccbs) {
351 * First time through. Re-use the S/G
352 * space we allocated for initialization
355 sg_map = SLIST_FIRST(&dpt->sg_maps);
357 sg_map = dptallocsgmap(dpt);
363 segs = sg_map->sg_vaddr;
364 physaddr = sg_map->sg_physaddr;
366 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
367 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
370 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
374 next_ccb->sg_list = segs;
375 next_ccb->sg_busaddr = htonl(physaddr);
376 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
377 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
378 next_ccb->eata_ccb.cp_reqDMA =
379 htonl(dptccbvtop(dpt, next_ccb)
380 + offsetof(struct dpt_ccb, sense_data));
381 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
382 next_ccb->state = DCCB_FREE;
383 next_ccb->tag = dpt->total_dccbs;
384 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
386 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
387 dpt->dpt_ccb_busend += sizeof(*next_ccb);
396 dpt_pio_get_conf (u_int32_t base)
398 static dpt_conf_t * conf;
403 * Allocate a dpt_conf_t
406 conf = (dpt_conf_t *)malloc(sizeof(dpt_conf_t),
411 * If we didn't get one then we probably won't ever get one.
414 printf("dpt: unable to allocate dpt_conf_t\n");
419 * If we have one, clean it up.
421 bzero(conf, sizeof(dpt_conf_t));
424 * Reset the controller.
426 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
429 * Wait for the controller to become ready.
430 * For some reason there can be -no- delays after calling reset
431 * before we wait on ready status.
433 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
434 printf("dpt: timeout waiting for controller to become ready\n");
438 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
439 printf("dpt: timetout waiting for adapter ready.\n");
444 * Send the PIO_READ_CONFIG command.
446 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
449 * Read the data into the struct.
451 p = (u_int16_t *)conf;
452 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
454 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
456 printf("dpt: timeout in data read.\n");
460 (*p) = inw(base + HA_RDATA);
464 if (inb(base + HA_RSTATUS) & HA_SERROR) {
466 printf("dpt: error reading configuration data.\n");
470 #define BE_EATA_SIGNATURE 0x45415441
471 #define LE_EATA_SIGNATURE 0x41544145
474 * Test to see if we have a valid card.
476 if ((conf->signature == BE_EATA_SIGNATURE) ||
477 (conf->signature == LE_EATA_SIGNATURE)) {
479 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
480 inw(base + HA_RDATA);
490 * Read a configuration page into the supplied dpt_cont_t buffer.
493 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
494 u_int size, u_int page, u_int target, int extent)
504 cp = &dccb->eata_ccb;
505 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
509 cp->Auto_Req_Sen = 1;
510 cp->reqlen = sizeof(struct scsi_sense_data);
513 cp->cp_LUN = 0; /* In the EATA packet */
514 cp->cp_lun = 0; /* In the SCSI command */
516 cp->cp_scsi_cmd = INQUIRY;
519 cp->cp_extent = extent;
522 cp->cp_channel = 0; /* DNC, Interpret mode is set */
524 cp->cp_datalen = htonl(size);
529 * This could be a simple for loop, but we suspected the compiler To
530 * have optimized it a bit too much. Wait for the controller to
533 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
534 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
535 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
536 || (dpt_wait(dpt, HA_SBUSY, 0))) {
539 * RAID Drives still Spinning up? (This should only occur if
540 * the DPT controller is in a NON PC (PCI?) platform).
542 if (dpt_raid_busy(dpt)) {
543 printf("dpt%d WARNING: Get_conf() RSUS failed.\n",
550 DptStat_Reset_BUSY(dpt->sp);
553 * XXXX We might want to do something more clever than aborting at
554 * this point, like resetting (rebooting) the controller and trying
557 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
558 EATA_CMD_DMA_SEND_CP,
559 10000, 0, 0, 0)) != 0) {
560 printf("dpt%d WARNING: Get_conf() failed (%d) to send "
561 "EATA_CMD_DMA_READ_CONFIG\n",
566 /* Wait for two seconds for a response. This can be slow */
569 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
574 /* Grab the status and clear interrupts */
575 status = dpt_inb(dpt, HA_RSTATUS);
580 * Check the status carefully. Return only if the
581 * command was successful.
583 if (((status & HA_SERROR) == 0)
584 && (dpt->sp->hba_stat == 0)
585 && (dpt->sp->scsi_stat == 0)
586 && (dpt->sp->residue_len == 0))
589 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
595 /* Detect Cache parameters and size */
597 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
609 * Default setting, for best perfromance..
610 * This is what virtually all cards default to..
612 dpt->cache_type = DPT_CACHE_WRITEBACK;
615 cp = &dccb->eata_ccb;
616 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
619 /* Setup the command structure */
622 cp->Auto_Req_Sen = 1;
623 cp->reqlen = sizeof(struct scsi_sense_data);
625 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
626 cp->cp_LUN = 0; /* In the EATA packet */
627 cp->cp_lun = 0; /* In the SCSI command */
630 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
639 * Build the EATA Command Packet structure
640 * for a Log Sense Command.
642 cp->cp_cdb[0] = 0x4d;
644 cp->cp_cdb[2] = 0x40 | 0x33;
647 cp->cp_datalen = htonl(512);
650 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
651 EATA_CMD_DMA_SEND_CP,
654 printf("dpt%d WARNING: detect_cache() failed (%d) to send "
655 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
659 /* Wait for two seconds for a response. This can be slow... */
662 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
667 /* Grab the status and clear interrupts */
668 status = dpt_inb(dpt, HA_RSTATUS);
674 if (buff[0] != 0x33) {
677 bytes = DPT_HCP_LENGTH(buff);
678 param = DPT_HCP_FIRST(buff);
680 if (DPT_HCP_CODE(param) != 1) {
682 * DPT Log Page layout error
684 printf("dpt%d: NOTICE: Log Page (1) layout error\n",
688 if (!(param[4] & 0x4)) {
689 dpt->cache_type = DPT_NO_CACHE;
692 while (DPT_HCP_CODE(param) != 6) {
693 param = DPT_HCP_NEXT(param);
695 || (param >= &buff[bytes])) {
700 if (param[4] & 0x2) {
704 dpt->cache_type = DPT_NO_CACHE;
708 if (param[4] & 0x4) {
709 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
712 /* XXX This isn't correct. This log parameter only has two bytes.... */
714 dpt->cache_size = param[5]
722 dpt_poll(struct cam_sim *sim)
724 dpt_intr(cam_sim_softc(sim));
728 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
730 struct dpt_ccb *dccb;
732 struct dpt_softc *dpt;
735 dccb = (struct dpt_ccb *)arg;
737 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
741 printf("dpt%d: Unexepected error 0x%x returned from "
742 "bus_dmamap_load\n", dpt->unit, error);
743 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
744 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
745 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
747 dptfreeccb(dpt, dccb);
754 bus_dma_segment_t *end_seg;
757 end_seg = dm_segs + nseg;
759 /* Copy the segments into our SG list */
761 while (dm_segs < end_seg) {
762 sg->seg_len = htonl(dm_segs->ds_len);
763 sg->seg_addr = htonl(dm_segs->ds_addr);
769 dccb->eata_ccb.scatter = 1;
770 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
771 dccb->eata_ccb.cp_datalen =
772 htonl(nseg * sizeof(dpt_sg_t));
774 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
775 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
778 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
779 op = BUS_DMASYNC_PREREAD;
781 op = BUS_DMASYNC_PREWRITE;
783 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
786 dccb->eata_ccb.cp_dataDMA = 0;
787 dccb->eata_ccb.cp_datalen = 0;
793 * Last time we need to check if this CCB needs to
796 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
798 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
799 dptfreeccb(dpt, dccb);
805 dccb->state |= DCCB_ACTIVE;
806 ccb->ccb_h.status |= CAM_SIM_QUEUED;
807 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
808 ccb->ccb_h.timeout_ch =
809 timeout(dpttimeout, (caddr_t)dccb,
810 (ccb->ccb_h.timeout * hz) / 1000);
811 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
812 dccb->eata_ccb.cp_busaddr,
813 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
814 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
816 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
817 dptfreeccb(dpt, dccb);
825 dpt_action(struct cam_sim *sim, union ccb *ccb)
827 struct dpt_softc *dpt;
829 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
831 dpt = (struct dpt_softc *)cam_sim_softc(sim);
833 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
834 xpt_print_path(ccb->ccb_h.path);
835 printf("controller is shutdown. Aborting CCB.\n");
836 ccb->ccb_h.status = CAM_NO_HBA;
841 switch (ccb->ccb_h.func_code) {
842 /* Common cases first */
843 case XPT_SCSI_IO: /* Execute the requested I/O operation */
845 struct ccb_scsiio *csio;
846 struct ccb_hdr *ccbh;
847 struct dpt_ccb *dccb;
848 struct eata_ccb *eccb;
852 /* Max CDB length is 12 bytes */
853 if (csio->cdb_len > 12) {
854 ccb->ccb_h.status = CAM_REQ_INVALID;
858 if ((dccb = dptgetccb(dpt)) == NULL) {
862 dpt->resource_shortage = 1;
864 xpt_freeze_simq(sim, /*count*/1);
865 ccb->ccb_h.status = CAM_REQUEUE_REQ;
869 eccb = &dccb->eata_ccb;
871 /* Link dccb and ccb so we can find one from the other */
873 ccb->ccb_h.ccb_dccb_ptr = dccb;
874 ccb->ccb_h.ccb_dpt_ptr = dpt;
877 * Explicitly set all flags so that the compiler can
878 * be smart about setting them.
880 eccb->SCSI_Reset = 0;
882 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
887 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
889 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
890 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
891 eccb->reqlen = csio->sense_len;
892 eccb->cp_id = ccb->ccb_h.target_id;
893 eccb->cp_channel = cam_sim_bus(sim);
894 eccb->cp_LUN = ccb->ccb_h.target_lun;
896 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
898 eccb->cp_identify = 1;
900 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
901 && csio->tag_action != CAM_TAG_ACTION_NONE) {
902 eccb->cp_msg[0] = csio->tag_action;
903 eccb->cp_msg[1] = dccb->tag;
910 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
911 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
912 bcopy(csio->cdb_io.cdb_ptr,
913 eccb->cp_cdb, csio->cdb_len);
915 /* I guess I could map it in... */
916 ccb->ccb_h.status = CAM_REQ_INVALID;
917 dptfreeccb(dpt, dccb);
922 bcopy(csio->cdb_io.cdb_bytes,
923 eccb->cp_cdb, csio->cdb_len);
926 * If we have any data to send with this command,
927 * map it into bus space.
929 /* Only use S/G if there is a transfer */
930 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
931 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
933 * We've been given a pointer
934 * to a single buffer.
936 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
942 bus_dmamap_load(dpt->buffer_dmat,
948 if (error == EINPROGRESS) {
950 * So as to maintain ordering,
951 * freeze the controller queue
952 * until our mapping is
955 xpt_freeze_simq(sim, 1);
956 dccb->state |= CAM_RELEASE_SIMQ;
960 struct bus_dma_segment seg;
962 /* Pointer to physical buffer */
964 (bus_addr_t)csio->data_ptr;
965 seg.ds_len = csio->dxfer_len;
966 dptexecuteccb(dccb, &seg, 1, 0);
969 struct bus_dma_segment *segs;
971 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
972 panic("dpt_action - Physical "
976 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
977 panic("dpt_action - Virtual "
981 /* Just use the segments provided */
982 segs = (struct bus_dma_segment *)csio->data_ptr;
983 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
988 * Does it want them both on or both off?
989 * CAM_DIR_NONE is both on, so this code can
990 * be removed if this is also what the DPT
995 dptexecuteccb(dccb, NULL, 0, 0);
999 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1000 case XPT_ABORT: /* Abort the specified CCB */
1002 ccb->ccb_h.status = CAM_REQ_INVALID;
1005 case XPT_SET_TRAN_SETTINGS:
1007 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1011 case XPT_GET_TRAN_SETTINGS:
1012 /* Get default/user set transfer settings for the target */
1014 struct ccb_trans_settings *cts = &ccb->cts;
1015 struct ccb_trans_settings_scsi *scsi =
1016 &cts->proto_specific.scsi;
1017 struct ccb_trans_settings_spi *spi =
1018 &cts->xport_specific.spi;
1020 cts->protocol = PROTO_SCSI;
1021 cts->protocol_version = SCSI_REV_2;
1022 cts->transport = XPORT_SPI;
1023 cts->transport_version = 2;
1025 if (cts->type == CTS_TYPE_USER_SETTINGS) {
1026 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
1027 spi->bus_width = (dpt->max_id > 7)
1028 ? MSG_EXT_WDTR_BUS_8_BIT
1029 : MSG_EXT_WDTR_BUS_16_BIT;
1030 spi->sync_period = 25; /* 10MHz */
1031 if (spi->sync_period != 0)
1032 spi->sync_offset = 15;
1033 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1035 spi->valid = CTS_SPI_VALID_SYNC_RATE
1036 | CTS_SPI_VALID_SYNC_OFFSET
1037 | CTS_SPI_VALID_BUS_WIDTH
1038 | CTS_SPI_VALID_DISC;
1039 scsi->valid = CTS_SCSI_VALID_TQ;
1040 ccb->ccb_h.status = CAM_REQ_CMP;
1042 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1047 case XPT_CALC_GEOMETRY:
1050 * XXX Use Adaptec translation until I find out how to
1051 * get this information from the card.
1053 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1057 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1060 ccb->ccb_h.status = CAM_REQ_CMP;
1064 case XPT_TERM_IO: /* Terminate the I/O process */
1066 ccb->ccb_h.status = CAM_REQ_INVALID;
1069 case XPT_PATH_INQ: /* Path routing inquiry */
1071 struct ccb_pathinq *cpi = &ccb->cpi;
1073 cpi->version_num = 1;
1074 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1075 if (dpt->max_id > 7)
1076 cpi->hba_inquiry |= PI_WIDE_16;
1077 cpi->target_sprt = 0;
1079 cpi->hba_eng_cnt = 0;
1080 cpi->max_target = dpt->max_id;
1081 cpi->max_lun = dpt->max_lun;
1082 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1083 cpi->bus_id = cam_sim_bus(sim);
1084 cpi->base_transfer_speed = 3300;
1085 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1086 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1087 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1088 cpi->unit_number = cam_sim_unit(sim);
1089 cpi->transport = XPORT_SPI;
1090 cpi->transport_version = 2;
1091 cpi->protocol = PROTO_SCSI;
1092 cpi->protocol_version = SCSI_REV_2;
1093 cpi->ccb_h.status = CAM_REQ_CMP;
1098 ccb->ccb_h.status = CAM_REQ_INVALID;
1105 * This routine will try to send an EATA command to the DPT HBA.
1106 * It will, by default, try 20,000 times, waiting 50us between tries.
1107 * It returns 0 on success and 1 on failure.
1108 * It is assumed to be called at splcam().
1111 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1112 u_int32_t cmd_busaddr, u_int command, u_int retries,
1113 u_int ifc, u_int code, u_int code2)
1121 * I hate this polling nonsense. Wish there was a way to tell the DPT
1122 * to go get commands at its own pace, or to interrupt when ready.
1123 * In the mean time we will measure how many itterations it really
1126 for (loop = 0; loop < retries; loop++) {
1127 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1133 if (loop < retries) {
1134 #ifdef DPT_MEASURE_PERFORMANCE
1135 if (loop > dpt->performance.max_eata_tries)
1136 dpt->performance.max_eata_tries = loop;
1138 if (loop < dpt->performance.min_eata_tries)
1139 dpt->performance.min_eata_tries = loop;
1142 #ifdef DPT_MEASURE_PERFORMANCE
1143 ++dpt->performance.command_too_busy;
1148 /* The controller is alive, advance the wedge timer */
1149 #ifdef DPT_RESET_HBA
1150 dpt->last_contact = microtime_now;
1153 if (cmd_block == NULL)
1155 #if (BYTE_ORDER == BIG_ENDIAN)
1157 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1158 | ((cmd_busaddr >> 16) & 0xFF)
1159 | ((cmd_busaddr >> 8) & 0xFF)
1160 | (cmd_busaddr & 0xFF);
1163 /* And now the address */
1164 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1166 if (command == EATA_CMD_IMMEDIATE) {
1167 if (cmd_block == NULL) {
1168 dpt_outb(dpt, HA_WCODE2, code2);
1169 dpt_outb(dpt, HA_WCODE, code);
1171 dpt_outb(dpt, HA_WIFC, ifc);
1173 dpt_outb(dpt, HA_WCOMMAND, command);
1179 /* ==================== Exported Function definitions =======================*/
1181 dpt_alloc(device_t dev)
1183 dpt_softc_t *dpt = device_get_softc(dev);
1186 dpt->tag = rman_get_bustag(dpt->io_res);
1187 dpt->bsh = rman_get_bushandle(dpt->io_res) + dpt->io_offset;
1188 dpt->unit = device_get_unit(dev);
1189 SLIST_INIT(&dpt->free_dccb_list);
1190 LIST_INIT(&dpt->pending_ccb_list);
1191 for (i = 0; i < MAX_CHANNELS; i++)
1192 dpt->resetlevel[i] = DPT_HA_OK;
1194 #ifdef DPT_MEASURE_PERFORMANCE
1195 dpt_reset_performance(dpt);
1196 #endif /* DPT_MEASURE_PERFORMANCE */
1201 dpt_free(struct dpt_softc *dpt)
1203 switch (dpt->init_level) {
1206 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1208 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1210 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1212 bus_dma_tag_destroy(dpt->dccb_dmat);
1214 bus_dma_tag_destroy(dpt->buffer_dmat);
1217 struct sg_map_node *sg_map;
1219 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1220 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1221 bus_dmamap_unload(dpt->sg_dmat,
1223 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1225 free(sg_map, M_DEVBUF);
1227 bus_dma_tag_destroy(dpt->sg_dmat);
1235 dpt_alloc_resources (device_t dev)
1240 dpt = device_get_softc(dev);
1242 dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type, &dpt->io_rid,
1244 if (dpt->io_res == NULL) {
1245 device_printf(dev, "No I/O space?!\n");
1250 dpt->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &dpt->irq_rid,
1252 if (dpt->irq_res == NULL) {
1253 device_printf(dev, "No IRQ!\n");
1265 dpt_release_resources (device_t dev)
1267 struct dpt_softc * dpt;
1269 dpt = device_get_softc(dev);
1272 bus_teardown_intr(dev, dpt->irq_res, dpt->ih);
1274 bus_release_resource(dev, dpt->io_type, dpt->io_rid, dpt->io_res);
1276 bus_release_resource(dev, SYS_RES_IRQ, dpt->irq_rid, dpt->irq_res);
1278 bus_release_resource(dev, SYS_RES_DRQ, dpt->drq_rid, dpt->drq_res);
1283 static u_int8_t string_sizes[] =
1285 sizeof(((dpt_inq_t*)NULL)->vendor),
1286 sizeof(((dpt_inq_t*)NULL)->modelNum),
1287 sizeof(((dpt_inq_t*)NULL)->firmware),
1288 sizeof(((dpt_inq_t*)NULL)->protocol),
1292 dpt_init(struct dpt_softc *dpt)
1295 struct sg_map_node *sg_map;
1302 dpt->init_level = 0;
1303 SLIST_INIT(&dpt->sg_maps);
1305 #ifdef DPT_RESET_BOARD
1306 printf("dpt%d: resetting HBA\n", dpt->unit);
1307 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1309 /* XXX Shouldn't we poll a status register or something??? */
1311 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1312 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1315 /* lowaddr */ BUS_SPACE_MAXADDR,
1316 /* highaddr */ BUS_SPACE_MAXADDR,
1318 /* filterarg */ NULL,
1319 /* maxsize */ PAGE_SIZE,
1321 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1323 /* lockfunc */ busdma_lock_mutex,
1324 /* lockarg */ &Giant,
1325 &dpt->sg_dmat) != 0) {
1332 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1333 * memory. To get the allocation size, we need to know how many
1334 * ccbs the card supports. This requires a ccb. We solve this
1335 * chicken and egg problem by allocating some re-usable S/G space
1336 * up front, and treating it as our status packet, CCB, and target
1337 * memory space for these commands.
1339 sg_map = dptallocsgmap(dpt);
1343 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1344 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1345 bzero(dccb, sizeof(*dccb));
1346 dpt->sp_physaddr = sg_map->sg_physaddr;
1347 dccb->eata_ccb.cp_dataDMA =
1348 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1349 dccb->eata_ccb.cp_busaddr = ~0;
1350 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1351 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1352 + offsetof(struct dpt_ccb, sense_data));
1354 /* Okay. Fetch our config */
1355 bzero(&dccb[1], sizeof(conf)); /* data area */
1356 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1357 sizeof(conf), 0xc1, 7, 1);
1360 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1363 bcopy(&dccb[1], &conf, sizeof(conf));
1365 bzero(&dccb[1], sizeof(dpt->board_data));
1366 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1367 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1369 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1372 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1374 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1375 (u_int8_t *)&dccb[1]);
1377 switch (ntohl(conf.splen)) {
1379 dpt->EATA_revision = 'a';
1382 dpt->EATA_revision = 'b';
1385 dpt->EATA_revision = 'c';
1388 dpt->EATA_revision = 'z';
1391 dpt->EATA_revision = '?';
1394 dpt->max_id = conf.MAX_ID;
1395 dpt->max_lun = conf.MAX_LUN;
1396 dpt->irq = conf.IRQ;
1397 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1398 dpt->channels = conf.MAX_CHAN + 1;
1399 dpt->state |= DPT_HA_OK;
1401 dpt->primary = FALSE;
1403 dpt->primary = TRUE;
1405 dpt->more_support = conf.MORE_support;
1407 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1408 dpt->immediate_support = 1;
1410 dpt->immediate_support = 0;
1412 dpt->broken_INQUIRY = FALSE;
1414 dpt->cplen = ntohl(conf.cplen);
1415 dpt->cppadlen = ntohs(conf.cppadlen);
1416 dpt->max_dccbs = ntohs(conf.queuesiz);
1418 if (dpt->max_dccbs > 256) {
1419 printf("dpt%d: Max CCBs reduced from %d to "
1420 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1421 dpt->max_dccbs = 256;
1424 dpt->hostid[0] = conf.scsi_id0;
1425 dpt->hostid[1] = conf.scsi_id1;
1426 dpt->hostid[2] = conf.scsi_id2;
1431 dpt->sgsize = ntohs(conf.SGsiz);
1433 /* We can only get 64k buffers, so don't bother to waste space. */
1434 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1437 if (dpt->sgsize > dpt_max_segs)
1438 dpt->sgsize = dpt_max_segs;
1440 /* DMA tag for mapping buffers into device visible space. */
1441 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1444 /* lowaddr */ BUS_SPACE_MAXADDR,
1445 /* highaddr */ BUS_SPACE_MAXADDR,
1447 /* filterarg */ NULL,
1448 /* maxsize */ MAXBSIZE,
1449 /* nsegments */ dpt->sgsize,
1450 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1451 /* flags */ BUS_DMA_ALLOCNOW,
1452 /* lockfunc */ busdma_lock_mutex,
1453 /* lockarg */ &Giant,
1454 &dpt->buffer_dmat) != 0) {
1455 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1461 /* DMA tag for our ccb structures and interrupt status packet */
1462 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1465 /* lowaddr */ BUS_SPACE_MAXADDR,
1466 /* highaddr */ BUS_SPACE_MAXADDR,
1468 /* filterarg */ NULL,
1469 /* maxsize */ (dpt->max_dccbs *
1470 sizeof(struct dpt_ccb)) +
1473 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1475 /* lockfunc */ busdma_lock_mutex,
1476 /* lockarg */ &Giant,
1477 &dpt->dccb_dmat) != 0) {
1478 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1484 /* Allocation for our ccbs and interrupt status packet */
1485 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1486 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1487 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1493 /* And permanently map them */
1494 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1496 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1498 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1500 /* Clear them out. */
1501 bzero(dpt->dpt_dccbs,
1502 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1504 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1506 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1507 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1508 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1511 /* Allocate our first batch of ccbs */
1512 if (dptallocccbs(dpt) == 0) {
1513 printf("dpt: dptallocccbs(dpt) == 0\n");
1517 /* Prepare for Target Mode */
1518 dpt->target_mode_enabled = 1;
1520 /* Nuke excess spaces from inquiry information */
1521 strp = dpt->board_data.vendor;
1522 for (i = 0; i < sizeof(string_sizes); i++) {
1523 index = string_sizes[i] - 1;
1524 while (index && (strp[index] == ' '))
1525 strp[index--] = '\0';
1526 strp += string_sizes[i];
1529 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1530 dpt->unit, dpt->board_data.vendor,
1531 dpt->board_data.modelNum, dpt->board_data.firmware);
1533 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1535 if (dpt->cache_type != DPT_NO_CACHE
1536 && dpt->cache_size != 0) {
1537 printf("%s Cache, ",
1538 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1539 ? "Write-Through" : "Write-Back");
1542 printf("%d CCBs\n", dpt->max_dccbs);
1550 dpt_attach(dpt_softc_t *dpt)
1552 struct cam_devq *devq;
1556 * Create the device queue for our SIM.
1558 devq = cam_simq_alloc(dpt->max_dccbs);
1562 for (i = 0; i < dpt->channels; i++) {
1564 * Construct our SIM entry
1566 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1567 dpt, dpt->unit, &Giant,
1569 /*tagged*/dpt->max_dccbs, devq);
1570 if (dpt->sims[i] == NULL) {
1572 cam_simq_free(devq);
1574 printf( "%s(): Unable to attach bus %d "
1575 "due to resource shortage\n",
1580 if (xpt_bus_register(dpt->sims[i], dpt->dev, i) != CAM_SUCCESS){
1581 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1582 dpt->sims[i] = NULL;
1586 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1587 cam_sim_path(dpt->sims[i]),
1588 CAM_TARGET_WILDCARD,
1589 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1590 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1591 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1592 dpt->sims[i] = NULL;
1598 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1599 dpt, SHUTDOWN_PRI_DEFAULT);
1604 dpt_detach (device_t dev)
1606 struct dpt_softc * dpt;
1609 dpt = device_get_softc(dev);
1611 for (i = 0; i < dpt->channels; i++) {
1613 xpt_async(AC_LOST_DEVICE, dpt->paths[i], NULL);
1615 xpt_free_path(dpt->paths[i]);
1616 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1617 cam_sim_free(dpt->sims[i], /*free_devq*/TRUE);
1620 dptshutdown((void *)dpt, SHUTDOWN_PRI_DEFAULT);
1622 dpt_release_resources(dev);
1630 * This is the interrupt handler for the DPT driver.
1642 u_int32_t residue_len; /* Number of bytes not transferred */
1644 dpt = (dpt_softc_t *)arg;
1646 /* First order of business is to check if this interrupt is for us */
1647 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1650 * What we want to do now, is to capture the status, all of it,
1651 * move it where it belongs, wake up whoever sleeps waiting to
1652 * process this result, and get out of here.
1654 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1655 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1656 printf("Encountered bogus status packet\n");
1657 status = dpt_inb(dpt, HA_RSTATUS);
1661 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1663 dpt->sp->ccb_busaddr = ~0;
1665 /* Ignore status packets with EOC not set */
1666 if (dpt->sp->EOC == 0) {
1667 printf("dpt%d ERROR: Request %d received with "
1668 "clear EOC.\n Marking as LOST.\n",
1669 dpt->unit, dccb->transaction_id);
1671 #ifdef DPT_HANDLE_TIMEOUTS
1672 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1674 /* This CLEARS the interrupt! */
1675 status = dpt_inb(dpt, HA_RSTATUS);
1681 * Double buffer the status information so the hardware can
1682 * work on updating the status packet while we decifer the
1683 * one we were just interrupted for.
1684 * According to Mark Salyzyn, we only need few pieces of it.
1686 hba_stat = dpt->sp->hba_stat;
1687 scsi_stat = dpt->sp->scsi_stat;
1688 residue_len = dpt->sp->residue_len;
1690 /* Clear interrupts, check for error */
1691 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1693 * Error Condition. Check for magic cookie. Exit
1694 * this test on earliest sign of non-reset condition
1697 /* Check that this is not a board reset interrupt */
1698 if (dpt_just_reset(dpt)) {
1699 printf("dpt%d: HBA rebooted.\n"
1700 " All transactions should be "
1704 printf("dpt%d: >>---->> This is incomplete, "
1705 "fix me.... <<----<<", dpt->unit);
1706 panic("DPT Rebooted");
1712 untimeout(dpttimeout, dccb, ccb->ccb_h.timeout_ch);
1713 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1714 bus_dmasync_op_t op;
1716 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1717 op = BUS_DMASYNC_POSTREAD;
1719 op = BUS_DMASYNC_POSTWRITE;
1720 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1721 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1724 /* Common Case inline... */
1725 if (hba_stat == HA_NO_ERROR) {
1726 ccb->csio.scsi_status = scsi_stat;
1727 ccb->ccb_h.status = 0;
1728 switch (scsi_stat) {
1729 case SCSI_STATUS_OK:
1730 ccb->ccb_h.status |= CAM_REQ_CMP;
1732 case SCSI_STATUS_CHECK_COND:
1733 case SCSI_STATUS_CMD_TERMINATED:
1734 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1735 ccb->csio.sense_len);
1736 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1739 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1740 /* XXX Freeze DevQ */
1743 ccb->csio.resid = residue_len;
1744 dptfreeccb(dpt, dccb);
1747 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1754 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1755 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1757 ccb->csio.resid = resid;
1760 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1763 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1765 case HA_SCSIBUS_RESET:
1766 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1767 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1770 case HA_CP_RESET: /* XXX ??? */
1771 case HA_CP_ABORT_NA: /* XXX ??? */
1772 case HA_CP_RESET_NA: /* XXX ??? */
1773 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1774 ccb->ccb_h.status = CAM_REQ_ABORTED;
1779 case HA_PCI_STABORT:
1783 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1785 case HA_UNX_MSGRJCT:
1786 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1788 case HA_UNX_BUSPHASE:
1789 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1791 case HA_UNX_BUS_FREE:
1792 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1795 case HA_RESET_STUCK:
1797 * Dead??? Can the controller get unstuck
1798 * from these conditions
1800 ccb->ccb_h.status = CAM_NO_HBA;
1802 case HA_RSENSE_FAIL:
1803 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1806 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1807 printf("Please mail this message to shimon@simon-shapiro.org\n");
1808 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1811 dptfreeccb(dpt, dccb);
1816 dpttimeout(void *arg)
1818 struct dpt_ccb *dccb;
1820 struct dpt_softc *dpt;
1823 dccb = (struct dpt_ccb *)arg;
1825 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1826 xpt_print_path(ccb->ccb_h.path);
1827 printf("CCB %p - timed out\n", (void *)dccb);
1832 * Try to clear any pending jobs. FreeBSD will lose interrupts,
1833 * leaving the controller suspended, and commands timed-out.
1834 * By calling the interrupt handler, any command thus stuck will be
1839 if ((dccb->state & DCCB_ACTIVE) == 0) {
1840 xpt_print_path(ccb->ccb_h.path);
1841 printf("CCB %p - timed out CCB already completed\n",
1847 /* Abort this particular command. Leave all others running */
1848 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1849 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1850 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1855 * Shutdown the controller and ensure that the cache is completely flushed.
1856 * Called from the shutdown_final event after all disk access has completed.
1859 dptshutdown(void *arg, int howto)
1863 dpt = (dpt_softc_t *)arg;
1865 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1869 * What we do for a shutdown, is give the DPT early power loss warning
1871 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1872 DELAY(1000 * 1000 * 5);
1873 printf("dpt%d: Controller was warned of shutdown and is now "
1874 "disabled\n", dpt->unit);
1877 /*============================================================================*/
1880 #ifdef DPT_RESET_HBA
1883 ** Function name : dpt_reset_hba
1885 ** Description : Reset the HBA and properly discard all pending work
1890 dpt_reset_hba(dpt_softc_t *dpt)
1894 dpt_ccb_t dccb, *dccbp;
1896 struct scsi_xfer *xs;
1898 /* Prepare a control block. The SCSI command part is immaterial */
1901 dccb.state = DPT_CCB_STATE_NEW;
1902 dccb.std_callback = NULL;
1903 dccb.wrbuff_callback = NULL;
1905 ccb = &dccb.eata_ccb;
1906 ccb->CP_OpCode = EATA_CMD_RESET;
1907 ccb->SCSI_Reset = 0;
1909 ccb->Auto_Req_Sen = 1;
1910 ccb->cp_id = 0; /* Should be ignored */
1914 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1915 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1916 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1917 ccb->cp_viraddr = (u_int32_t) & ccb;
1919 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1920 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1922 /* Lock up the submitted queue. We are very persistant here */
1924 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1928 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1931 /* Send the RESET message */
1932 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1933 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1934 printf("dpt%d: Failed to send the RESET message.\n"
1935 " Trying cold boot (ouch!)\n", dpt->unit);
1938 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1939 EATA_COLD_BOOT, 0, 0,
1941 panic("dpt%d: Faild to cold boot the HBA\n",
1944 #ifdef DPT_MEASURE_PERFORMANCE
1945 dpt->performance.cold_boots++;
1946 #endif /* DPT_MEASURE_PERFORMANCE */
1949 #ifdef DPT_MEASURE_PERFORMANCE
1950 dpt->performance.warm_starts++;
1951 #endif /* DPT_MEASURE_PERFORMANCE */
1953 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1956 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1957 struct scsi_xfer *xs = dccbp->xs;
1959 /* Not all transactions have xs structs */
1961 /* Tell the kernel proper this did not complete well */
1962 xs->error |= XS_SELTIMEOUT;
1963 xs->flags |= SCSI_ITSDONE;
1967 dpt_Qremove_submitted(dpt, dccbp);
1969 /* Remember, Callbacks are NOT in the standard queue */
1970 if (dccbp->std_callback != NULL) {
1971 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1975 dpt_Qpush_free(dpt, dccbp);
1980 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1981 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1984 #endif /* DPT_RESET_HBA */
1987 * Build a Command Block for target mode READ/WRITE BUFFER,
1988 * with the ``sync'' bit ON.
1990 * Although the length and offset are 24 bit fields in the command, they cannot
1991 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1992 * If they are sensless, we round them to zero offset, maximum length and
1997 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1998 dpt_ccb_t * ccb, int mode, u_int8_t command,
1999 u_int16_t length, u_int16_t offset)
2004 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
2005 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
2006 dpt->unit, length, offset);
2007 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
2012 ccb->state = DPT_CCB_STATE_NEW;
2013 ccb->std_callback = (ccb_callback) dpt_target_done;
2014 ccb->wrbuff_callback = NULL;
2016 cp = &ccb->eata_ccb;
2017 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
2020 cp->Auto_Req_Sen = 1;
2025 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
2026 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
2027 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
2028 cp->cp_viraddr = (u_int32_t) & ccb;
2030 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
2032 cp->cp_scsi_cmd = command;
2033 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
2034 cp->cp_lun = lun; /* Order is important here! */
2035 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
2036 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
2037 cp->cp_cdb[4] = (length >> 8) & 0xFF;
2038 cp->cp_cdb[5] = length & 0xFF;
2039 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
2040 cp->cp_cdb[7] = (length >> 8) & 0xFF;
2041 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
2042 cp->cp_cdb[9] = 0; /* No sync, no match bits */
2045 * This could be optimized to live in dpt_register_buffer.
2046 * We keep it here, just in case the kernel decides to reallocate pages
2048 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
2049 dpt->rw_buffer[bus][target][lun])) {
2050 printf("dpt%d: Failed to setup Scatter/Gather for "
2051 "Target-Mode buffer\n", dpt->unit);
2055 /* Setup a target mode READ command */
2058 dpt_set_target(int redo, dpt_softc_t * dpt,
2059 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
2060 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
2064 if (dpt->target_mode_enabled) {
2068 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
2069 SCSI_TM_READ_BUFFER, length, offset);
2071 ccb->transaction_id = ++dpt->commands_processed;
2073 #ifdef DPT_MEASURE_PERFORMANCE
2074 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2075 ccb->command_started = microtime_now;
2077 dpt_Qadd_waiting(dpt, ccb);
2078 dpt_sched_queue(dpt);
2082 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
2088 * Schedule a buffer to be sent to another target.
2089 * The work will be scheduled and the callback provided will be called when
2090 * the work is actually done.
2092 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
2093 * get notified of receipt of buffers.
2097 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2098 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
2099 buff_wr_done callback)
2102 dpt_ccb_t *ccb = NULL;
2105 /* This is an external call. Be a bit paranoid */
2106 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2108 dpt = TAILQ_NEXT(dpt, links)) {
2109 if (dpt->unit == unit)
2113 return (INVALID_UNIT);
2117 if (dpt->target_mode_enabled) {
2118 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2119 (lun > dpt->max_lun)) {
2120 return (INVALID_SENDER);
2122 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2123 (dpt->buffer_receiver[channel][target][lun] == NULL))
2124 return (NOT_REGISTERED);
2126 ospl = splsoftcam();
2127 /* Process the free list */
2128 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2129 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2130 " Please try later\n",
2133 return (NO_RESOURCES);
2135 /* Now grab the newest CCB */
2136 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2138 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2142 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2143 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2144 SCSI_TM_WRITE_BUFFER,
2146 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2149 ccb->transaction_id = ++dpt->commands_processed;
2151 #ifdef DPT_MEASURE_PERFORMANCE
2152 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2153 ccb->command_started = microtime_now;
2155 dpt_Qadd_waiting(dpt, ccb);
2156 dpt_sched_queue(dpt);
2161 return (DRIVER_DOWN);
2165 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2170 cp = &ccb->eata_ccb;
2173 * Remove the CCB from the waiting queue.
2174 * We do NOT put it back on the free, etc., queues as it is a special
2175 * ccb, owned by the dpt_softc of this unit.
2177 ospl = splsoftcam();
2178 dpt_Qremove_completed(dpt, ccb);
2181 #define br_channel (ccb->eata_ccb.cp_channel)
2182 #define br_target (ccb->eata_ccb.cp_id)
2183 #define br_lun (ccb->eata_ccb.cp_LUN)
2184 #define br_index [br_channel][br_target][br_lun]
2185 #define read_buffer_callback (dpt->buffer_receiver br_index )
2186 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2187 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2188 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2189 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2191 /* Different reasons for being here, you know... */
2192 switch (ccb->eata_ccb.cp_scsi_cmd) {
2193 case SCSI_TM_READ_BUFFER:
2194 if (read_buffer_callback != NULL) {
2195 /* This is a buffer generated by a kernel process */
2196 read_buffer_callback(dpt->unit, br_channel,
2199 br_offset, br_length);
2202 * This is a buffer waited for by a user (sleeping)
2208 /* We ALWAYS re-issue the same command; args are don't-care */
2209 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2212 case SCSI_TM_WRITE_BUFFER:
2213 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2214 br_offset, br_length,
2215 br_lun, ccb->status_packet.hba_stat);
2218 printf("dpt%d: %s is an unsupported command for target mode\n",
2219 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2221 ospl = splsoftcam();
2222 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2223 dpt_Qpush_free(dpt, ccb);
2229 * Use this function to register a client for a buffer read target operation.
2230 * The function you register will be called every time a buffer is received
2231 * by the target mode code.
2234 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2235 u_int8_t mode, u_int16_t length, u_int16_t offset,
2236 dpt_rec_buff callback, dpt_rb_op_t op)
2239 dpt_ccb_t *ccb = NULL;
2242 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2244 dpt = TAILQ_NEXT(dpt, links)) {
2245 if (dpt->unit == unit)
2249 return (INVALID_UNIT);
2253 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2254 return (DRIVER_DOWN);
2256 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2257 (lun > (dpt->max_lun - 1)))
2258 return (INVALID_SENDER);
2260 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2261 if (op == REGISTER_BUFFER) {
2262 /* Assign the requested callback */
2263 dpt->buffer_receiver[channel][target][lun] = callback;
2265 ospl = splsoftcam();
2267 /* Process the free list */
2268 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2269 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2270 " Please try later\n",
2273 return (NO_RESOURCES);
2275 /* Now grab the newest CCB */
2276 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2278 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2283 /* Clean up the leftover of the previous tenant */
2284 ccb->status = DPT_CCB_STATE_NEW;
2285 dpt->target_ccb[channel][target][lun] = ccb;
2287 dpt->rw_buffer[channel][target][lun] =
2288 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
2289 if (dpt->rw_buffer[channel][target][lun] == NULL) {
2290 printf("dpt%d: Failed to allocate "
2291 "Target-Mode buffer\n", dpt->unit);
2292 ospl = splsoftcam();
2293 dpt_Qpush_free(dpt, ccb);
2295 return (NO_RESOURCES);
2297 dpt_set_target(0, dpt, channel, target, lun, mode,
2298 length, offset, ccb);
2299 return (SUCCESSFULLY_REGISTERED);
2301 return (NOT_REGISTERED);
2303 if (op == REGISTER_BUFFER) {
2304 if (dpt->buffer_receiver[channel][target][lun] == callback)
2305 return (ALREADY_REGISTERED);
2307 return (REGISTERED_TO_ANOTHER);
2309 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2310 dpt->buffer_receiver[channel][target][lun] = NULL;
2311 ospl = splsoftcam();
2312 dpt_Qpush_free(dpt, ccb);
2314 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2315 return (SUCCESSFULLY_REGISTERED);
2317 return (INVALID_CALLBACK);
2323 /* Return the state of the blinking DPT LED's */
2325 dpt_blinking_led(dpt_softc_t * dpt)
2337 for (ndx = 0, state = 0, previous = 0;
2338 (ndx < 10) && (state != previous);
2341 state = dpt_inl(dpt, 1);
2344 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2345 result = dpt_inb(dpt, 5);
2352 * Execute a command which did not come from the kernel's SCSI layer.
2353 * The only way to map user commands to bus and target is to comply with the
2354 * standard DPT wire-down scheme:
2357 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2358 caddr_t cmdarg, int minor_no)
2362 int channel, target, lun;
2369 channel = minor2hba(minor_no);
2370 target = minor2target(minor_no);
2371 lun = minor2lun(minor_no);
2373 if ((channel > (dpt->channels - 1))
2374 || (target > dpt->max_id)
2375 || (lun > dpt->max_lun))
2378 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2379 /* This one is for the controller itself */
2380 if ((user_cmd->eataID[0] != 'E')
2381 || (user_cmd->eataID[1] != 'A')
2382 || (user_cmd->eataID[2] != 'T')
2383 || (user_cmd->eataID[3] != 'A')) {
2387 /* Get a DPT CCB, so we can prepare a command */
2388 ospl = splsoftcam();
2390 /* Process the free list */
2391 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2392 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2393 " Please try later\n",
2398 /* Now grab the newest CCB */
2399 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2401 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2404 /* Clean up the leftover of the previous tenant */
2405 ccb->status = DPT_CCB_STATE_NEW;
2408 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2409 sizeof(eata_ccb_t));
2411 /* We do not want to do user specified scatter/gather. Why?? */
2412 if (ccb->eata_ccb.scatter == 1)
2415 ccb->eata_ccb.Auto_Req_Sen = 1;
2416 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2417 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2418 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2419 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2420 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2421 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2423 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2424 /* Data I/O is involved in this command. Alocate buffer */
2425 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2426 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2427 M_TEMP, M_WAITOK, 0, ~0,
2428 ccb->eata_ccb.cp_datalen,
2431 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2436 printf("dpt%d: Cannot allocate %d bytes "
2437 "for EATA command\n", dpt->unit,
2438 ccb->eata_ccb.cp_datalen);
2441 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2442 if (ccb->eata_ccb.DataIn == 1) {
2443 if (copyin(usr_cmd_DMA,
2444 data, ccb->eata_ccb.cp_datalen) == -1)
2448 /* No data I/O involved here. Make sure the DPT knows that */
2449 ccb->eata_ccb.cp_datalen = 0;
2453 if (ccb->eata_ccb.FWNEST == 1)
2454 ccb->eata_ccb.FWNEST = 0;
2456 if (ccb->eata_ccb.cp_datalen != 0) {
2457 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2465 * We are required to quiet a SCSI bus.
2466 * since we do not queue comands on a bus basis,
2467 * we wait for ALL commands on a controller to complete.
2468 * In the mean time, sched_queue() will not schedule new commands.
2470 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2471 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2472 /* We wait for ALL traffic for this HBa to subside */
2473 ospl = splsoftcam();
2474 dpt->state |= DPT_HA_QUIET;
2477 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2478 huh = tsleep((void *) dpt, PCATCH | PRIBIO, "dptqt",
2482 /* Wakeup call received */
2493 /* Resume normal operation */
2494 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2495 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2496 ospl = splsoftcam();
2497 dpt->state &= ~DPT_HA_QUIET;
2501 * Schedule the command and submit it.
2502 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2506 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2508 ccb->transaction_id = ++dpt->commands_processed;
2509 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2510 ccb->result = (u_int32_t) & cmdarg;
2513 #ifdef DPT_MEASURE_PERFORMANCE
2514 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2515 ccb->command_started = microtime_now;
2518 dpt_Qadd_waiting(dpt, ccb);
2521 dpt_sched_queue(dpt);
2523 /* Wait for the command to complete */
2524 (void) tsleep((void *) ccb, PCATCH | PRIBIO, "dptucw", 100 * hz);
2526 /* Free allocated memory */
2534 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2536 int ospl = splsoftcam();
2541 * If Auto Request Sense is on, copyout the sense struct
2543 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2544 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2545 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2546 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2547 sizeof(struct scsi_sense_data))) {
2548 ccb->result = EFAULT;
2549 dpt_Qpush_free(dpt, ccb);
2555 /* If DataIn is on, copyout the data */
2556 if ((ccb->eata_ccb.DataIn == 1)
2557 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2558 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2559 dpt_Qpush_free(dpt, ccb);
2560 ccb->result = EFAULT;
2567 /* Copyout the status */
2568 result = ccb->status_packet.hba_stat;
2569 cmd_arg = (caddr_t) ccb->result;
2571 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2572 dpt_Qpush_free(dpt, ccb);
2573 ccb->result = EFAULT;
2578 /* Put the CCB back in the freelist */
2579 ccb->state |= DPT_CCB_STATE_COMPLETED;
2580 dpt_Qpush_free(dpt, ccb);
2582 /* Free allocated memory */
2587 #ifdef DPT_HANDLE_TIMEOUTS
2589 * This function walks down the SUBMITTED queue.
2590 * Every request that is too old gets aborted and marked.
2591 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2592 * We just walk the list, aborting old commands and marking them as such.
2593 * The dpt_complete function will get rid of the that were interrupted in the
2596 * This function needs to run at splcam(), as it interacts with the submitted
2597 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2598 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2599 * willbe able to pre-empt it, grab a transaction in progress (towards
2600 * destruction) and operate on it. The state of this transaction will be not
2602 * The only other option, is to lock it only as long as necessary but have
2603 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2604 * a SMP environment, the advantage is dubvious for a function that runs once
2605 * every ten seconds for few microseconds and, on systems with healthy
2606 * hardware, does not do anything anyway.
2610 dpt_handle_timeouts(dpt_softc_t * dpt)
2617 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2618 printf("dpt%d WARNING: Timeout Handling Collision\n",
2623 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2625 /* Loop through the entire submitted queue, looking for lost souls */
2626 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2628 ccb = TAILQ_NEXT(ccb, links)) {
2629 struct scsi_xfer *xs;
2630 u_int32_t age, max_age;
2633 age = dpt_time_delta(ccb->command_started, microtime_now);
2635 #define TenSec 10000000
2637 if (xs == NULL) { /* Local, non-kernel call */
2640 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2641 + DPT_TIMEOUT_FACTOR))
2643 ? (xs->timeout * (dpt->submitted_ccbs_count
2644 + DPT_TIMEOUT_FACTOR))
2649 * If a transaction is marked lost and is TWICE as old as we
2650 * care, then, and only then do we destroy it!
2652 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2653 /* Remember who is next */
2654 if (age > (max_age * 2)) {
2655 dpt_Qremove_submitted(dpt, ccb);
2656 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2657 ccb->state |= DPT_CCB_STATE_ABORTED;
2658 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2659 if (ccb->retries++ > DPT_RETRIES) {
2660 printf("dpt%d ERROR: Destroying stale "
2663 "c%db%dt%du%d (%d/%d)\n",
2664 dpt->unit, ccb->transaction_id,
2667 ccb->eata_ccb.cp_channel,
2668 ccb->eata_ccb.cp_id,
2669 ccb->eata_ccb.cp_LUN, age,
2671 #define send_ccb &ccb->eata_ccb
2672 #define ESA EATA_SPECIFIC_ABORT
2673 (void) dpt_send_immediate(dpt,
2677 dpt_Qpush_free(dpt, ccb);
2679 /* The SCSI layer should re-try */
2680 xs->error |= XS_TIMEOUT;
2681 xs->flags |= SCSI_ITSDONE;
2684 printf("dpt%d ERROR: Stale %d (%s) on "
2685 "c%db%dt%du%d (%d)\n"
2688 dpt->unit, ccb->transaction_id,
2691 ccb->eata_ccb.cp_channel,
2692 ccb->eata_ccb.cp_id,
2693 ccb->eata_ccb.cp_LUN,
2694 age, ccb->retries, DPT_RETRIES);
2696 dpt_Qpush_waiting(dpt, ccb);
2697 dpt_sched_queue(dpt);
2702 * This is a transaction that is not to be destroyed
2703 * (yet) But it is too old for our liking. We wait as
2704 * long as the upper layer thinks. Not really, we
2705 * multiply that by the number of commands in the
2706 * submitted queue + 1.
2708 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2709 (age != ~0) && (age > max_age)) {
2710 printf("dpt%d ERROR: Marking %d (%s) on "
2712 " as late after %dusec\n",
2713 dpt->unit, ccb->transaction_id,
2715 dpt->unit, ccb->eata_ccb.cp_channel,
2716 ccb->eata_ccb.cp_id,
2717 ccb->eata_ccb.cp_LUN, age);
2718 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2723 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2728 dpt_timeout(void *arg)
2730 dpt_softc_t *dpt = (dpt_softc_t *) arg;
2732 if (!(dpt->state & DPT_HA_TIMEOUTS_ACTIVE))
2733 dpt_handle_timeouts(dpt);
2735 timeout(dpt_timeout, (caddr_t) dpt, hz * 10);
2738 #endif /* DPT_HANDLE_TIMEOUTS */