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_SYNC_RATE
1038 | CTS_SPI_VALID_BUS_WIDTH
1039 | CTS_SPI_VALID_DISC;
1040 scsi->valid = CTS_SCSI_VALID_TQ;
1041 ccb->ccb_h.status = CAM_REQ_CMP;
1043 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1048 case XPT_CALC_GEOMETRY:
1051 * XXX Use Adaptec translation until I find out how to
1052 * get this information from the card.
1054 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1058 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1061 ccb->ccb_h.status = CAM_REQ_CMP;
1065 case XPT_TERM_IO: /* Terminate the I/O process */
1067 ccb->ccb_h.status = CAM_REQ_INVALID;
1070 case XPT_PATH_INQ: /* Path routing inquiry */
1072 struct ccb_pathinq *cpi = &ccb->cpi;
1074 cpi->version_num = 1;
1075 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1076 if (dpt->max_id > 7)
1077 cpi->hba_inquiry |= PI_WIDE_16;
1078 cpi->target_sprt = 0;
1080 cpi->hba_eng_cnt = 0;
1081 cpi->max_target = dpt->max_id;
1082 cpi->max_lun = dpt->max_lun;
1083 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1084 cpi->bus_id = cam_sim_bus(sim);
1085 cpi->base_transfer_speed = 3300;
1086 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1087 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1088 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1089 cpi->unit_number = cam_sim_unit(sim);
1090 cpi->transport = XPORT_SPI;
1091 cpi->transport_version = 2;
1092 cpi->protocol = PROTO_SCSI;
1093 cpi->protocol_version = SCSI_REV_2;
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)
1184 dpt_softc_t *dpt = device_get_softc(dev);
1187 dpt->tag = rman_get_bustag(dpt->io_res);
1188 dpt->bsh = rman_get_bushandle(dpt->io_res) + dpt->io_offset;
1189 dpt->unit = device_get_unit(dev);
1190 SLIST_INIT(&dpt->free_dccb_list);
1191 LIST_INIT(&dpt->pending_ccb_list);
1192 for (i = 0; i < MAX_CHANNELS; i++)
1193 dpt->resetlevel[i] = DPT_HA_OK;
1195 #ifdef DPT_MEASURE_PERFORMANCE
1196 dpt_reset_performance(dpt);
1197 #endif /* DPT_MEASURE_PERFORMANCE */
1202 dpt_free(struct dpt_softc *dpt)
1204 switch (dpt->init_level) {
1207 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1209 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1211 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1213 bus_dma_tag_destroy(dpt->dccb_dmat);
1215 bus_dma_tag_destroy(dpt->buffer_dmat);
1218 struct sg_map_node *sg_map;
1220 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1221 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1222 bus_dmamap_unload(dpt->sg_dmat,
1224 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1226 free(sg_map, M_DEVBUF);
1228 bus_dma_tag_destroy(dpt->sg_dmat);
1236 dpt_alloc_resources (device_t dev)
1241 dpt = device_get_softc(dev);
1243 dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type, &dpt->io_rid,
1245 if (dpt->io_res == NULL) {
1246 device_printf(dev, "No I/O space?!\n");
1251 dpt->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &dpt->irq_rid,
1253 if (dpt->irq_res == NULL) {
1254 device_printf(dev, "No IRQ!\n");
1266 dpt_release_resources (device_t dev)
1268 struct dpt_softc * dpt;
1270 dpt = device_get_softc(dev);
1273 bus_teardown_intr(dev, dpt->irq_res, dpt->ih);
1275 bus_release_resource(dev, dpt->io_type, dpt->io_rid, dpt->io_res);
1277 bus_release_resource(dev, SYS_RES_IRQ, dpt->irq_rid, dpt->irq_res);
1279 bus_release_resource(dev, SYS_RES_DRQ, dpt->drq_rid, dpt->drq_res);
1284 static u_int8_t string_sizes[] =
1286 sizeof(((dpt_inq_t*)NULL)->vendor),
1287 sizeof(((dpt_inq_t*)NULL)->modelNum),
1288 sizeof(((dpt_inq_t*)NULL)->firmware),
1289 sizeof(((dpt_inq_t*)NULL)->protocol),
1293 dpt_init(struct dpt_softc *dpt)
1296 struct sg_map_node *sg_map;
1303 dpt->init_level = 0;
1304 SLIST_INIT(&dpt->sg_maps);
1306 #ifdef DPT_RESET_BOARD
1307 printf("dpt%d: resetting HBA\n", dpt->unit);
1308 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1310 /* XXX Shouldn't we poll a status register or something??? */
1312 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1313 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1316 /* lowaddr */ BUS_SPACE_MAXADDR,
1317 /* highaddr */ BUS_SPACE_MAXADDR,
1319 /* filterarg */ NULL,
1320 /* maxsize */ PAGE_SIZE,
1322 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1324 /* lockfunc */ busdma_lock_mutex,
1325 /* lockarg */ &Giant,
1326 &dpt->sg_dmat) != 0) {
1333 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1334 * memory. To get the allocation size, we need to know how many
1335 * ccbs the card supports. This requires a ccb. We solve this
1336 * chicken and egg problem by allocating some re-usable S/G space
1337 * up front, and treating it as our status packet, CCB, and target
1338 * memory space for these commands.
1340 sg_map = dptallocsgmap(dpt);
1344 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1345 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1346 bzero(dccb, sizeof(*dccb));
1347 dpt->sp_physaddr = sg_map->sg_physaddr;
1348 dccb->eata_ccb.cp_dataDMA =
1349 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1350 dccb->eata_ccb.cp_busaddr = ~0;
1351 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1352 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1353 + offsetof(struct dpt_ccb, sense_data));
1355 /* Okay. Fetch our config */
1356 bzero(&dccb[1], sizeof(conf)); /* data area */
1357 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1358 sizeof(conf), 0xc1, 7, 1);
1361 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1364 bcopy(&dccb[1], &conf, sizeof(conf));
1366 bzero(&dccb[1], sizeof(dpt->board_data));
1367 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1368 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1370 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1373 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1375 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1376 (u_int8_t *)&dccb[1]);
1378 switch (ntohl(conf.splen)) {
1380 dpt->EATA_revision = 'a';
1383 dpt->EATA_revision = 'b';
1386 dpt->EATA_revision = 'c';
1389 dpt->EATA_revision = 'z';
1392 dpt->EATA_revision = '?';
1395 dpt->max_id = conf.MAX_ID;
1396 dpt->max_lun = conf.MAX_LUN;
1397 dpt->irq = conf.IRQ;
1398 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1399 dpt->channels = conf.MAX_CHAN + 1;
1400 dpt->state |= DPT_HA_OK;
1402 dpt->primary = FALSE;
1404 dpt->primary = TRUE;
1406 dpt->more_support = conf.MORE_support;
1408 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1409 dpt->immediate_support = 1;
1411 dpt->immediate_support = 0;
1413 dpt->broken_INQUIRY = FALSE;
1415 dpt->cplen = ntohl(conf.cplen);
1416 dpt->cppadlen = ntohs(conf.cppadlen);
1417 dpt->max_dccbs = ntohs(conf.queuesiz);
1419 if (dpt->max_dccbs > 256) {
1420 printf("dpt%d: Max CCBs reduced from %d to "
1421 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1422 dpt->max_dccbs = 256;
1425 dpt->hostid[0] = conf.scsi_id0;
1426 dpt->hostid[1] = conf.scsi_id1;
1427 dpt->hostid[2] = conf.scsi_id2;
1432 dpt->sgsize = ntohs(conf.SGsiz);
1434 /* We can only get 64k buffers, so don't bother to waste space. */
1435 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1438 if (dpt->sgsize > dpt_max_segs)
1439 dpt->sgsize = dpt_max_segs;
1441 /* DMA tag for mapping buffers into device visible space. */
1442 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1445 /* lowaddr */ BUS_SPACE_MAXADDR,
1446 /* highaddr */ BUS_SPACE_MAXADDR,
1448 /* filterarg */ NULL,
1449 /* maxsize */ MAXBSIZE,
1450 /* nsegments */ dpt->sgsize,
1451 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1452 /* flags */ BUS_DMA_ALLOCNOW,
1453 /* lockfunc */ busdma_lock_mutex,
1454 /* lockarg */ &Giant,
1455 &dpt->buffer_dmat) != 0) {
1456 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1462 /* DMA tag for our ccb structures and interrupt status packet */
1463 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1466 /* lowaddr */ BUS_SPACE_MAXADDR,
1467 /* highaddr */ BUS_SPACE_MAXADDR,
1469 /* filterarg */ NULL,
1470 /* maxsize */ (dpt->max_dccbs *
1471 sizeof(struct dpt_ccb)) +
1474 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1476 /* lockfunc */ busdma_lock_mutex,
1477 /* lockarg */ &Giant,
1478 &dpt->dccb_dmat) != 0) {
1479 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1485 /* Allocation for our ccbs and interrupt status packet */
1486 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1487 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1488 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1494 /* And permanently map them */
1495 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1497 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1499 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1501 /* Clear them out. */
1502 bzero(dpt->dpt_dccbs,
1503 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1505 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1507 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1508 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1509 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1512 /* Allocate our first batch of ccbs */
1513 if (dptallocccbs(dpt) == 0) {
1514 printf("dpt: dptallocccbs(dpt) == 0\n");
1518 /* Prepare for Target Mode */
1519 dpt->target_mode_enabled = 1;
1521 /* Nuke excess spaces from inquiry information */
1522 strp = dpt->board_data.vendor;
1523 for (i = 0; i < sizeof(string_sizes); i++) {
1524 index = string_sizes[i] - 1;
1525 while (index && (strp[index] == ' '))
1526 strp[index--] = '\0';
1527 strp += string_sizes[i];
1530 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1531 dpt->unit, dpt->board_data.vendor,
1532 dpt->board_data.modelNum, dpt->board_data.firmware);
1534 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1536 if (dpt->cache_type != DPT_NO_CACHE
1537 && dpt->cache_size != 0) {
1538 printf("%s Cache, ",
1539 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1540 ? "Write-Through" : "Write-Back");
1543 printf("%d CCBs\n", dpt->max_dccbs);
1551 dpt_attach(dpt_softc_t *dpt)
1553 struct cam_devq *devq;
1557 * Create the device queue for our SIM.
1559 devq = cam_simq_alloc(dpt->max_dccbs);
1563 for (i = 0; i < dpt->channels; i++) {
1565 * Construct our SIM entry
1567 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1568 dpt, dpt->unit, &Giant,
1570 /*tagged*/dpt->max_dccbs, devq);
1571 if (dpt->sims[i] == NULL) {
1573 cam_simq_free(devq);
1575 printf( "%s(): Unable to attach bus %d "
1576 "due to resource shortage\n",
1581 if (xpt_bus_register(dpt->sims[i], dpt->dev, i) != CAM_SUCCESS){
1582 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1583 dpt->sims[i] = NULL;
1587 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1588 cam_sim_path(dpt->sims[i]),
1589 CAM_TARGET_WILDCARD,
1590 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1591 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1592 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1593 dpt->sims[i] = NULL;
1599 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1600 dpt, SHUTDOWN_PRI_DEFAULT);
1605 dpt_detach (device_t dev)
1607 struct dpt_softc * dpt;
1610 dpt = device_get_softc(dev);
1612 for (i = 0; i < dpt->channels; i++) {
1614 xpt_async(AC_LOST_DEVICE, dpt->paths[i], NULL);
1616 xpt_free_path(dpt->paths[i]);
1617 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1618 cam_sim_free(dpt->sims[i], /*free_devq*/TRUE);
1621 dptshutdown((void *)dpt, SHUTDOWN_PRI_DEFAULT);
1623 dpt_release_resources(dev);
1631 * This is the interrupt handler for the DPT driver.
1643 u_int32_t residue_len; /* Number of bytes not transferred */
1645 dpt = (dpt_softc_t *)arg;
1647 /* First order of business is to check if this interrupt is for us */
1648 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1651 * What we want to do now, is to capture the status, all of it,
1652 * move it where it belongs, wake up whoever sleeps waiting to
1653 * process this result, and get out of here.
1655 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1656 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1657 printf("Encountered bogus status packet\n");
1658 status = dpt_inb(dpt, HA_RSTATUS);
1662 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1664 dpt->sp->ccb_busaddr = ~0;
1666 /* Ignore status packets with EOC not set */
1667 if (dpt->sp->EOC == 0) {
1668 printf("dpt%d ERROR: Request %d received with "
1669 "clear EOC.\n Marking as LOST.\n",
1670 dpt->unit, dccb->transaction_id);
1672 #ifdef DPT_HANDLE_TIMEOUTS
1673 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1675 /* This CLEARS the interrupt! */
1676 status = dpt_inb(dpt, HA_RSTATUS);
1682 * Double buffer the status information so the hardware can
1683 * work on updating the status packet while we decifer the
1684 * one we were just interrupted for.
1685 * According to Mark Salyzyn, we only need few pieces of it.
1687 hba_stat = dpt->sp->hba_stat;
1688 scsi_stat = dpt->sp->scsi_stat;
1689 residue_len = dpt->sp->residue_len;
1691 /* Clear interrupts, check for error */
1692 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1694 * Error Condition. Check for magic cookie. Exit
1695 * this test on earliest sign of non-reset condition
1698 /* Check that this is not a board reset interrupt */
1699 if (dpt_just_reset(dpt)) {
1700 printf("dpt%d: HBA rebooted.\n"
1701 " All transactions should be "
1705 printf("dpt%d: >>---->> This is incomplete, "
1706 "fix me.... <<----<<", dpt->unit);
1707 panic("DPT Rebooted");
1713 untimeout(dpttimeout, dccb, ccb->ccb_h.timeout_ch);
1714 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1715 bus_dmasync_op_t op;
1717 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1718 op = BUS_DMASYNC_POSTREAD;
1720 op = BUS_DMASYNC_POSTWRITE;
1721 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1722 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1725 /* Common Case inline... */
1726 if (hba_stat == HA_NO_ERROR) {
1727 ccb->csio.scsi_status = scsi_stat;
1728 ccb->ccb_h.status = 0;
1729 switch (scsi_stat) {
1730 case SCSI_STATUS_OK:
1731 ccb->ccb_h.status |= CAM_REQ_CMP;
1733 case SCSI_STATUS_CHECK_COND:
1734 case SCSI_STATUS_CMD_TERMINATED:
1735 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1736 ccb->csio.sense_len);
1737 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1740 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1741 /* XXX Freeze DevQ */
1744 ccb->csio.resid = residue_len;
1745 dptfreeccb(dpt, dccb);
1748 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1755 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1756 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1758 ccb->csio.resid = resid;
1761 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1764 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1766 case HA_SCSIBUS_RESET:
1767 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1768 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1771 case HA_CP_RESET: /* XXX ??? */
1772 case HA_CP_ABORT_NA: /* XXX ??? */
1773 case HA_CP_RESET_NA: /* XXX ??? */
1774 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1775 ccb->ccb_h.status = CAM_REQ_ABORTED;
1780 case HA_PCI_STABORT:
1784 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1786 case HA_UNX_MSGRJCT:
1787 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1789 case HA_UNX_BUSPHASE:
1790 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1792 case HA_UNX_BUS_FREE:
1793 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1796 case HA_RESET_STUCK:
1798 * Dead??? Can the controller get unstuck
1799 * from these conditions
1801 ccb->ccb_h.status = CAM_NO_HBA;
1803 case HA_RSENSE_FAIL:
1804 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1807 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1808 printf("Please mail this message to shimon@simon-shapiro.org\n");
1809 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1812 dptfreeccb(dpt, dccb);
1817 dpttimeout(void *arg)
1819 struct dpt_ccb *dccb;
1821 struct dpt_softc *dpt;
1824 dccb = (struct dpt_ccb *)arg;
1826 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1827 xpt_print_path(ccb->ccb_h.path);
1828 printf("CCB %p - timed out\n", (void *)dccb);
1833 * Try to clear any pending jobs. FreeBSD will lose interrupts,
1834 * leaving the controller suspended, and commands timed-out.
1835 * By calling the interrupt handler, any command thus stuck will be
1840 if ((dccb->state & DCCB_ACTIVE) == 0) {
1841 xpt_print_path(ccb->ccb_h.path);
1842 printf("CCB %p - timed out CCB already completed\n",
1848 /* Abort this particular command. Leave all others running */
1849 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1850 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1851 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1856 * Shutdown the controller and ensure that the cache is completely flushed.
1857 * Called from the shutdown_final event after all disk access has completed.
1860 dptshutdown(void *arg, int howto)
1864 dpt = (dpt_softc_t *)arg;
1866 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1870 * What we do for a shutdown, is give the DPT early power loss warning
1872 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1873 DELAY(1000 * 1000 * 5);
1874 printf("dpt%d: Controller was warned of shutdown and is now "
1875 "disabled\n", dpt->unit);
1878 /*============================================================================*/
1881 #ifdef DPT_RESET_HBA
1884 ** Function name : dpt_reset_hba
1886 ** Description : Reset the HBA and properly discard all pending work
1891 dpt_reset_hba(dpt_softc_t *dpt)
1895 dpt_ccb_t dccb, *dccbp;
1897 struct scsi_xfer *xs;
1899 /* Prepare a control block. The SCSI command part is immaterial */
1902 dccb.state = DPT_CCB_STATE_NEW;
1903 dccb.std_callback = NULL;
1904 dccb.wrbuff_callback = NULL;
1906 ccb = &dccb.eata_ccb;
1907 ccb->CP_OpCode = EATA_CMD_RESET;
1908 ccb->SCSI_Reset = 0;
1910 ccb->Auto_Req_Sen = 1;
1911 ccb->cp_id = 0; /* Should be ignored */
1915 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1916 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1917 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1918 ccb->cp_viraddr = (u_int32_t) & ccb;
1920 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1921 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1923 /* Lock up the submitted queue. We are very persistant here */
1925 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1929 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1932 /* Send the RESET message */
1933 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1934 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1935 printf("dpt%d: Failed to send the RESET message.\n"
1936 " Trying cold boot (ouch!)\n", dpt->unit);
1939 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1940 EATA_COLD_BOOT, 0, 0,
1942 panic("dpt%d: Faild to cold boot the HBA\n",
1945 #ifdef DPT_MEASURE_PERFORMANCE
1946 dpt->performance.cold_boots++;
1947 #endif /* DPT_MEASURE_PERFORMANCE */
1950 #ifdef DPT_MEASURE_PERFORMANCE
1951 dpt->performance.warm_starts++;
1952 #endif /* DPT_MEASURE_PERFORMANCE */
1954 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1957 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1958 struct scsi_xfer *xs = dccbp->xs;
1960 /* Not all transactions have xs structs */
1962 /* Tell the kernel proper this did not complete well */
1963 xs->error |= XS_SELTIMEOUT;
1964 xs->flags |= SCSI_ITSDONE;
1968 dpt_Qremove_submitted(dpt, dccbp);
1970 /* Remember, Callbacks are NOT in the standard queue */
1971 if (dccbp->std_callback != NULL) {
1972 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1976 dpt_Qpush_free(dpt, dccbp);
1981 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1982 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1985 #endif /* DPT_RESET_HBA */
1988 * Build a Command Block for target mode READ/WRITE BUFFER,
1989 * with the ``sync'' bit ON.
1991 * Although the length and offset are 24 bit fields in the command, they cannot
1992 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1993 * If they are sensless, we round them to zero offset, maximum length and
1998 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1999 dpt_ccb_t * ccb, int mode, u_int8_t command,
2000 u_int16_t length, u_int16_t offset)
2005 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
2006 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
2007 dpt->unit, length, offset);
2008 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
2013 ccb->state = DPT_CCB_STATE_NEW;
2014 ccb->std_callback = (ccb_callback) dpt_target_done;
2015 ccb->wrbuff_callback = NULL;
2017 cp = &ccb->eata_ccb;
2018 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
2021 cp->Auto_Req_Sen = 1;
2026 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
2027 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
2028 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
2029 cp->cp_viraddr = (u_int32_t) & ccb;
2031 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
2033 cp->cp_scsi_cmd = command;
2034 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
2035 cp->cp_lun = lun; /* Order is important here! */
2036 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
2037 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
2038 cp->cp_cdb[4] = (length >> 8) & 0xFF;
2039 cp->cp_cdb[5] = length & 0xFF;
2040 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
2041 cp->cp_cdb[7] = (length >> 8) & 0xFF;
2042 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
2043 cp->cp_cdb[9] = 0; /* No sync, no match bits */
2046 * This could be optimized to live in dpt_register_buffer.
2047 * We keep it here, just in case the kernel decides to reallocate pages
2049 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
2050 dpt->rw_buffer[bus][target][lun])) {
2051 printf("dpt%d: Failed to setup Scatter/Gather for "
2052 "Target-Mode buffer\n", dpt->unit);
2056 /* Setup a target mode READ command */
2059 dpt_set_target(int redo, dpt_softc_t * dpt,
2060 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
2061 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
2065 if (dpt->target_mode_enabled) {
2069 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
2070 SCSI_TM_READ_BUFFER, length, offset);
2072 ccb->transaction_id = ++dpt->commands_processed;
2074 #ifdef DPT_MEASURE_PERFORMANCE
2075 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2076 ccb->command_started = microtime_now;
2078 dpt_Qadd_waiting(dpt, ccb);
2079 dpt_sched_queue(dpt);
2083 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
2089 * Schedule a buffer to be sent to another target.
2090 * The work will be scheduled and the callback provided will be called when
2091 * the work is actually done.
2093 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
2094 * get notified of receipt of buffers.
2098 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2099 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
2100 buff_wr_done callback)
2103 dpt_ccb_t *ccb = NULL;
2106 /* This is an external call. Be a bit paranoid */
2107 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2109 dpt = TAILQ_NEXT(dpt, links)) {
2110 if (dpt->unit == unit)
2114 return (INVALID_UNIT);
2118 if (dpt->target_mode_enabled) {
2119 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2120 (lun > dpt->max_lun)) {
2121 return (INVALID_SENDER);
2123 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2124 (dpt->buffer_receiver[channel][target][lun] == NULL))
2125 return (NOT_REGISTERED);
2127 ospl = splsoftcam();
2128 /* Process the free list */
2129 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2130 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2131 " Please try later\n",
2134 return (NO_RESOURCES);
2136 /* Now grab the newest CCB */
2137 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2139 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2143 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2144 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2145 SCSI_TM_WRITE_BUFFER,
2147 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2150 ccb->transaction_id = ++dpt->commands_processed;
2152 #ifdef DPT_MEASURE_PERFORMANCE
2153 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2154 ccb->command_started = microtime_now;
2156 dpt_Qadd_waiting(dpt, ccb);
2157 dpt_sched_queue(dpt);
2162 return (DRIVER_DOWN);
2166 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2171 cp = &ccb->eata_ccb;
2174 * Remove the CCB from the waiting queue.
2175 * We do NOT put it back on the free, etc., queues as it is a special
2176 * ccb, owned by the dpt_softc of this unit.
2178 ospl = splsoftcam();
2179 dpt_Qremove_completed(dpt, ccb);
2182 #define br_channel (ccb->eata_ccb.cp_channel)
2183 #define br_target (ccb->eata_ccb.cp_id)
2184 #define br_lun (ccb->eata_ccb.cp_LUN)
2185 #define br_index [br_channel][br_target][br_lun]
2186 #define read_buffer_callback (dpt->buffer_receiver br_index )
2187 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2188 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2189 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2190 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2192 /* Different reasons for being here, you know... */
2193 switch (ccb->eata_ccb.cp_scsi_cmd) {
2194 case SCSI_TM_READ_BUFFER:
2195 if (read_buffer_callback != NULL) {
2196 /* This is a buffer generated by a kernel process */
2197 read_buffer_callback(dpt->unit, br_channel,
2200 br_offset, br_length);
2203 * This is a buffer waited for by a user (sleeping)
2209 /* We ALWAYS re-issue the same command; args are don't-care */
2210 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2213 case SCSI_TM_WRITE_BUFFER:
2214 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2215 br_offset, br_length,
2216 br_lun, ccb->status_packet.hba_stat);
2219 printf("dpt%d: %s is an unsupported command for target mode\n",
2220 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2222 ospl = splsoftcam();
2223 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2224 dpt_Qpush_free(dpt, ccb);
2230 * Use this function to register a client for a buffer read target operation.
2231 * The function you register will be called every time a buffer is received
2232 * by the target mode code.
2235 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2236 u_int8_t mode, u_int16_t length, u_int16_t offset,
2237 dpt_rec_buff callback, dpt_rb_op_t op)
2240 dpt_ccb_t *ccb = NULL;
2243 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2245 dpt = TAILQ_NEXT(dpt, links)) {
2246 if (dpt->unit == unit)
2250 return (INVALID_UNIT);
2254 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2255 return (DRIVER_DOWN);
2257 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2258 (lun > (dpt->max_lun - 1)))
2259 return (INVALID_SENDER);
2261 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2262 if (op == REGISTER_BUFFER) {
2263 /* Assign the requested callback */
2264 dpt->buffer_receiver[channel][target][lun] = callback;
2266 ospl = splsoftcam();
2268 /* Process the free list */
2269 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2270 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2271 " Please try later\n",
2274 return (NO_RESOURCES);
2276 /* Now grab the newest CCB */
2277 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2279 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2284 /* Clean up the leftover of the previous tenant */
2285 ccb->status = DPT_CCB_STATE_NEW;
2286 dpt->target_ccb[channel][target][lun] = ccb;
2288 dpt->rw_buffer[channel][target][lun] =
2289 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
2290 if (dpt->rw_buffer[channel][target][lun] == NULL) {
2291 printf("dpt%d: Failed to allocate "
2292 "Target-Mode buffer\n", dpt->unit);
2293 ospl = splsoftcam();
2294 dpt_Qpush_free(dpt, ccb);
2296 return (NO_RESOURCES);
2298 dpt_set_target(0, dpt, channel, target, lun, mode,
2299 length, offset, ccb);
2300 return (SUCCESSFULLY_REGISTERED);
2302 return (NOT_REGISTERED);
2304 if (op == REGISTER_BUFFER) {
2305 if (dpt->buffer_receiver[channel][target][lun] == callback)
2306 return (ALREADY_REGISTERED);
2308 return (REGISTERED_TO_ANOTHER);
2310 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2311 dpt->buffer_receiver[channel][target][lun] = NULL;
2312 ospl = splsoftcam();
2313 dpt_Qpush_free(dpt, ccb);
2315 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2316 return (SUCCESSFULLY_REGISTERED);
2318 return (INVALID_CALLBACK);
2324 /* Return the state of the blinking DPT LED's */
2326 dpt_blinking_led(dpt_softc_t * dpt)
2338 for (ndx = 0, state = 0, previous = 0;
2339 (ndx < 10) && (state != previous);
2342 state = dpt_inl(dpt, 1);
2345 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2346 result = dpt_inb(dpt, 5);
2353 * Execute a command which did not come from the kernel's SCSI layer.
2354 * The only way to map user commands to bus and target is to comply with the
2355 * standard DPT wire-down scheme:
2358 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2359 caddr_t cmdarg, int minor_no)
2363 int channel, target, lun;
2370 channel = minor2hba(minor_no);
2371 target = minor2target(minor_no);
2372 lun = minor2lun(minor_no);
2374 if ((channel > (dpt->channels - 1))
2375 || (target > dpt->max_id)
2376 || (lun > dpt->max_lun))
2379 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2380 /* This one is for the controller itself */
2381 if ((user_cmd->eataID[0] != 'E')
2382 || (user_cmd->eataID[1] != 'A')
2383 || (user_cmd->eataID[2] != 'T')
2384 || (user_cmd->eataID[3] != 'A')) {
2388 /* Get a DPT CCB, so we can prepare a command */
2389 ospl = splsoftcam();
2391 /* Process the free list */
2392 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2393 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2394 " Please try later\n",
2399 /* Now grab the newest CCB */
2400 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2402 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2405 /* Clean up the leftover of the previous tenant */
2406 ccb->status = DPT_CCB_STATE_NEW;
2409 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2410 sizeof(eata_ccb_t));
2412 /* We do not want to do user specified scatter/gather. Why?? */
2413 if (ccb->eata_ccb.scatter == 1)
2416 ccb->eata_ccb.Auto_Req_Sen = 1;
2417 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2418 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2419 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2420 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2421 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2422 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2424 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2425 /* Data I/O is involved in this command. Alocate buffer */
2426 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2427 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2428 M_TEMP, M_WAITOK, 0, ~0,
2429 ccb->eata_ccb.cp_datalen,
2432 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2437 printf("dpt%d: Cannot allocate %d bytes "
2438 "for EATA command\n", dpt->unit,
2439 ccb->eata_ccb.cp_datalen);
2442 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2443 if (ccb->eata_ccb.DataIn == 1) {
2444 if (copyin(usr_cmd_DMA,
2445 data, ccb->eata_ccb.cp_datalen) == -1)
2449 /* No data I/O involved here. Make sure the DPT knows that */
2450 ccb->eata_ccb.cp_datalen = 0;
2454 if (ccb->eata_ccb.FWNEST == 1)
2455 ccb->eata_ccb.FWNEST = 0;
2457 if (ccb->eata_ccb.cp_datalen != 0) {
2458 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2466 * We are required to quiet a SCSI bus.
2467 * since we do not queue comands on a bus basis,
2468 * we wait for ALL commands on a controller to complete.
2469 * In the mean time, sched_queue() will not schedule new commands.
2471 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2472 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2473 /* We wait for ALL traffic for this HBa to subside */
2474 ospl = splsoftcam();
2475 dpt->state |= DPT_HA_QUIET;
2478 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2479 huh = tsleep((void *) dpt, PCATCH | PRIBIO, "dptqt",
2483 /* Wakeup call received */
2494 /* Resume normal operation */
2495 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2496 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2497 ospl = splsoftcam();
2498 dpt->state &= ~DPT_HA_QUIET;
2502 * Schedule the command and submit it.
2503 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2507 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2509 ccb->transaction_id = ++dpt->commands_processed;
2510 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2511 ccb->result = (u_int32_t) & cmdarg;
2514 #ifdef DPT_MEASURE_PERFORMANCE
2515 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2516 ccb->command_started = microtime_now;
2519 dpt_Qadd_waiting(dpt, ccb);
2522 dpt_sched_queue(dpt);
2524 /* Wait for the command to complete */
2525 (void) tsleep((void *) ccb, PCATCH | PRIBIO, "dptucw", 100 * hz);
2527 /* Free allocated memory */
2535 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2537 int ospl = splsoftcam();
2542 * If Auto Request Sense is on, copyout the sense struct
2544 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2545 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2546 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2547 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2548 sizeof(struct scsi_sense_data))) {
2549 ccb->result = EFAULT;
2550 dpt_Qpush_free(dpt, ccb);
2556 /* If DataIn is on, copyout the data */
2557 if ((ccb->eata_ccb.DataIn == 1)
2558 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2559 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2560 dpt_Qpush_free(dpt, ccb);
2561 ccb->result = EFAULT;
2568 /* Copyout the status */
2569 result = ccb->status_packet.hba_stat;
2570 cmd_arg = (caddr_t) ccb->result;
2572 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2573 dpt_Qpush_free(dpt, ccb);
2574 ccb->result = EFAULT;
2579 /* Put the CCB back in the freelist */
2580 ccb->state |= DPT_CCB_STATE_COMPLETED;
2581 dpt_Qpush_free(dpt, ccb);
2583 /* Free allocated memory */
2588 #ifdef DPT_HANDLE_TIMEOUTS
2590 * This function walks down the SUBMITTED queue.
2591 * Every request that is too old gets aborted and marked.
2592 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2593 * We just walk the list, aborting old commands and marking them as such.
2594 * The dpt_complete function will get rid of the that were interrupted in the
2597 * This function needs to run at splcam(), as it interacts with the submitted
2598 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2599 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2600 * willbe able to pre-empt it, grab a transaction in progress (towards
2601 * destruction) and operate on it. The state of this transaction will be not
2603 * The only other option, is to lock it only as long as necessary but have
2604 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2605 * a SMP environment, the advantage is dubvious for a function that runs once
2606 * every ten seconds for few microseconds and, on systems with healthy
2607 * hardware, does not do anything anyway.
2611 dpt_handle_timeouts(dpt_softc_t * dpt)
2618 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2619 printf("dpt%d WARNING: Timeout Handling Collision\n",
2624 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2626 /* Loop through the entire submitted queue, looking for lost souls */
2627 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2629 ccb = TAILQ_NEXT(ccb, links)) {
2630 struct scsi_xfer *xs;
2631 u_int32_t age, max_age;
2634 age = dpt_time_delta(ccb->command_started, microtime_now);
2636 #define TenSec 10000000
2638 if (xs == NULL) { /* Local, non-kernel call */
2641 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2642 + DPT_TIMEOUT_FACTOR))
2644 ? (xs->timeout * (dpt->submitted_ccbs_count
2645 + DPT_TIMEOUT_FACTOR))
2650 * If a transaction is marked lost and is TWICE as old as we
2651 * care, then, and only then do we destroy it!
2653 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2654 /* Remember who is next */
2655 if (age > (max_age * 2)) {
2656 dpt_Qremove_submitted(dpt, ccb);
2657 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2658 ccb->state |= DPT_CCB_STATE_ABORTED;
2659 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2660 if (ccb->retries++ > DPT_RETRIES) {
2661 printf("dpt%d ERROR: Destroying stale "
2664 "c%db%dt%du%d (%d/%d)\n",
2665 dpt->unit, ccb->transaction_id,
2668 ccb->eata_ccb.cp_channel,
2669 ccb->eata_ccb.cp_id,
2670 ccb->eata_ccb.cp_LUN, age,
2672 #define send_ccb &ccb->eata_ccb
2673 #define ESA EATA_SPECIFIC_ABORT
2674 (void) dpt_send_immediate(dpt,
2678 dpt_Qpush_free(dpt, ccb);
2680 /* The SCSI layer should re-try */
2681 xs->error |= XS_TIMEOUT;
2682 xs->flags |= SCSI_ITSDONE;
2685 printf("dpt%d ERROR: Stale %d (%s) on "
2686 "c%db%dt%du%d (%d)\n"
2689 dpt->unit, ccb->transaction_id,
2692 ccb->eata_ccb.cp_channel,
2693 ccb->eata_ccb.cp_id,
2694 ccb->eata_ccb.cp_LUN,
2695 age, ccb->retries, DPT_RETRIES);
2697 dpt_Qpush_waiting(dpt, ccb);
2698 dpt_sched_queue(dpt);
2703 * This is a transaction that is not to be destroyed
2704 * (yet) But it is too old for our liking. We wait as
2705 * long as the upper layer thinks. Not really, we
2706 * multiply that by the number of commands in the
2707 * submitted queue + 1.
2709 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2710 (age != ~0) && (age > max_age)) {
2711 printf("dpt%d ERROR: Marking %d (%s) on "
2713 " as late after %dusec\n",
2714 dpt->unit, ccb->transaction_id,
2716 dpt->unit, ccb->eata_ccb.cp_channel,
2717 ccb->eata_ccb.cp_id,
2718 ccb->eata_ccb.cp_LUN, age);
2719 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2724 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2729 dpt_timeout(void *arg)
2731 dpt_softc_t *dpt = (dpt_softc_t *) arg;
2733 if (!(dpt->state & DPT_HA_TIMEOUTS_ACTIVE))
2734 dpt_handle_timeouts(dpt);
2736 timeout(dpt_timeout, (caddr_t) dpt, hz * 10);
2739 #endif /* DPT_HANDLE_TIMEOUTS */