2 * Copyright (c) 1997 by Simon Shapiro
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * dpt_scsi.c: SCSI dependant code for the DPT driver
33 * credits: Assisted by Mike Neuffer in the early low level DPT code
34 * Thanx to Mark Salyzyn of DPT for his assistance.
35 * Special thanx to Justin Gibbs for invaluable help in
36 * making this driver look and work like a FreeBSD component.
37 * Last but not least, many thanx to UCB and the FreeBSD
38 * team for creating and maintaining such a wonderful O/S.
40 * TODO: * Add ISA probe code.
41 * * Add driver-level RAID-0. This will allow interoperability with
42 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
43 * arrays that span controllers (Wow!).
46 #ident "$FreeBSD: src/sys/dev/dpt/dpt_scsi.c,v 1.28.2.3 2003/01/31 02:47:10 grog Exp $"
47 #ident "$DragonFly: src/sys/dev/raid/dpt/dpt_scsi.c,v 1.8 2004/09/17 03:39:39 joerg Exp $"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/eventhandler.h>
55 #include <sys/malloc.h>
56 #include <sys/kernel.h>
60 #include <machine/bus_memio.h>
61 #include <machine/bus_pio.h>
62 #include <machine/bus.h>
64 #include <machine/clock.h>
66 #include <bus/cam/cam.h>
67 #include <bus/cam/cam_ccb.h>
68 #include <bus/cam/cam_sim.h>
69 #include <bus/cam/cam_xpt_sim.h>
70 #include <bus/cam/cam_debug.h>
71 #include <bus/cam/scsi/scsi_all.h>
72 #include <bus/cam/scsi/scsi_message.h>
79 /* dpt_isa.c, dpt_eisa.c, and dpt_pci.c need this in a central place */
80 int dpt_controllers_present;
82 u_long dpt_unit; /* Next unit number to use */
84 /* The linked list of softc structures */
85 struct dpt_softc_list dpt_softcs = TAILQ_HEAD_INITIALIZER(dpt_softcs);
87 #define microtime_now dpt_time_now()
89 #define dpt_inl(dpt, port) \
90 bus_space_read_4((dpt)->tag, (dpt)->bsh, port)
91 #define dpt_inb(dpt, port) \
92 bus_space_read_1((dpt)->tag, (dpt)->bsh, port)
93 #define dpt_outl(dpt, port, value) \
94 bus_space_write_4((dpt)->tag, (dpt)->bsh, port, value)
95 #define dpt_outb(dpt, port, value) \
96 bus_space_write_1((dpt)->tag, (dpt)->bsh, port, value)
99 * These will have to be setup by parameters passed at boot/load time. For
100 * perfromance reasons, we make them constants for the time being.
102 #define dpt_min_segs DPT_MAX_SEGS
103 #define dpt_max_segs DPT_MAX_SEGS
105 /* Definitions for our use of the SIM private CCB area */
106 #define ccb_dccb_ptr spriv_ptr0
107 #define ccb_dpt_ptr spriv_ptr1
109 /* ================= Private Inline Function declarations ===================*/
110 static __inline int dpt_just_reset(dpt_softc_t * dpt);
111 static __inline int dpt_raid_busy(dpt_softc_t * dpt);
112 static __inline int dpt_pio_wait (u_int32_t, u_int, u_int, u_int);
113 static __inline int dpt_wait(dpt_softc_t *dpt, u_int bits,
115 static __inline struct dpt_ccb* dptgetccb(struct dpt_softc *dpt);
116 static __inline void dptfreeccb(struct dpt_softc *dpt,
117 struct dpt_ccb *dccb);
118 static __inline u_int32_t dptccbvtop(struct dpt_softc *dpt,
119 struct dpt_ccb *dccb);
121 static __inline int dpt_send_immediate(dpt_softc_t *dpt,
122 eata_ccb_t *cmd_block,
123 u_int32_t cmd_busaddr,
125 u_int ifc, u_int code,
128 /* ==================== Private Function declarations =======================*/
129 static void dptmapmem(void *arg, bus_dma_segment_t *segs,
130 int nseg, int error);
132 static struct sg_map_node*
133 dptallocsgmap(struct dpt_softc *dpt);
135 static int dptallocccbs(dpt_softc_t *dpt);
137 static int dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb,
138 u_int32_t dccb_busaddr, u_int size,
139 u_int page, u_int target, int extent);
140 static void dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb,
141 u_int32_t dccb_busaddr,
144 static void dpt_poll(struct cam_sim *sim);
146 static void dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
147 int nseg, int error);
149 static void dpt_action(struct cam_sim *sim, union ccb *ccb);
151 static int dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd,
152 u_int32_t cmd_busaddr,
153 u_int command, u_int retries,
154 u_int ifc, u_int code,
156 static void dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb,
157 union ccb *ccb, u_int hba_stat,
158 u_int scsi_stat, u_int32_t resid);
160 static void dpttimeout(void *arg);
161 static void dptshutdown(void *arg, int howto);
163 /* ================= Private Inline Function definitions ====================*/
165 dpt_just_reset(dpt_softc_t * dpt)
167 if ((dpt_inb(dpt, 2) == 'D')
168 && (dpt_inb(dpt, 3) == 'P')
169 && (dpt_inb(dpt, 4) == 'T')
170 && (dpt_inb(dpt, 5) == 'H'))
177 dpt_raid_busy(dpt_softc_t * dpt)
179 if ((dpt_inb(dpt, 0) == 'D')
180 && (dpt_inb(dpt, 1) == 'P')
181 && (dpt_inb(dpt, 2) == 'T'))
188 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
193 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
194 c = inb(base + reg) & bits;
204 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
209 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
210 c = dpt_inb(dpt, HA_RSTATUS) & bits;
219 static __inline struct dpt_ccb*
220 dptgetccb(struct dpt_softc *dpt)
222 struct dpt_ccb* dccb;
226 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
227 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
229 } else if (dpt->total_dccbs < dpt->max_dccbs) {
231 dccb = SLIST_FIRST(&dpt->free_dccb_list);
233 printf("dpt%d: Can't malloc DCCB\n", dpt->unit);
235 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
245 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
250 if ((dccb->state & DCCB_ACTIVE) != 0)
251 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
252 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
253 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
254 else if (dpt->resource_shortage != 0
255 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
256 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
257 dpt->resource_shortage = FALSE;
259 dccb->state = DCCB_FREE;
260 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
265 static __inline u_int32_t
266 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
268 return (dpt->dpt_ccb_busbase
269 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
272 static __inline struct dpt_ccb *
273 dptccbptov(struct dpt_softc *dpt, u_int32_t busaddr)
275 return (dpt->dpt_dccbs
276 + ((struct dpt_ccb *)busaddr
277 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
281 * Send a command for immediate execution by the DPT
282 * See above function for IMPORTANT notes.
285 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
286 u_int32_t cmd_busaddr, u_int retries,
287 u_int ifc, u_int code, u_int code2)
289 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
290 EATA_CMD_IMMEDIATE, retries, ifc,
295 /* ===================== Private Function definitions =======================*/
297 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
299 bus_addr_t *busaddrp;
301 busaddrp = (bus_addr_t *)arg;
302 *busaddrp = segs->ds_addr;
305 static struct sg_map_node *
306 dptallocsgmap(struct dpt_softc *dpt)
308 struct sg_map_node *sg_map;
310 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_INTWAIT);
312 /* Allocate S/G space for the next batch of CCBS */
313 if (bus_dmamem_alloc(dpt->sg_dmat, (void **)&sg_map->sg_vaddr,
314 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
315 free(sg_map, M_DEVBUF);
319 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
320 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
323 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
329 * Allocate another chunk of CCB's. Return count of entries added.
330 * Assumed to be called at splcam().
333 dptallocccbs(dpt_softc_t *dpt)
335 struct dpt_ccb *next_ccb;
336 struct sg_map_node *sg_map;
342 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
344 if (next_ccb == dpt->dpt_dccbs) {
346 * First time through. Re-use the S/G
347 * space we allocated for initialization
350 sg_map = SLIST_FIRST(&dpt->sg_maps);
352 sg_map = dptallocsgmap(dpt);
358 segs = sg_map->sg_vaddr;
359 physaddr = sg_map->sg_physaddr;
361 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
362 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
365 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
369 next_ccb->sg_list = segs;
370 next_ccb->sg_busaddr = htonl(physaddr);
371 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
372 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
373 next_ccb->eata_ccb.cp_reqDMA =
374 htonl(dptccbvtop(dpt, next_ccb)
375 + offsetof(struct dpt_ccb, sense_data));
376 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
377 next_ccb->state = DCCB_FREE;
378 next_ccb->tag = dpt->total_dccbs;
379 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
381 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
382 dpt->dpt_ccb_busend += sizeof(*next_ccb);
390 dpt_pio_get_conf (u_int32_t base)
392 static dpt_conf_t * conf;
397 * Allocate a dpt_conf_t
400 conf = malloc(sizeof(dpt_conf_t), M_DEVBUF, M_INTWAIT);
403 * If we have one, clean it up.
405 bzero(conf, sizeof(dpt_conf_t));
408 * Reset the controller.
410 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
413 * Wait for the controller to become ready.
414 * For some reason there can be -no- delays after calling reset
415 * before we wait on ready status.
417 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
418 printf("dpt: timeout waiting for controller to become ready\n");
422 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
423 printf("dpt: timetout waiting for adapter ready.\n");
428 * Send the PIO_READ_CONFIG command.
430 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
433 * Read the data into the struct.
435 p = (u_int16_t *)conf;
436 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
438 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
439 printf("dpt: timeout in data read.\n");
443 (*p) = inw(base + HA_RDATA);
447 if (inb(base + HA_RSTATUS) & HA_SERROR) {
448 printf("dpt: error reading configuration data.\n");
452 #define BE_EATA_SIGNATURE 0x45415441
453 #define LE_EATA_SIGNATURE 0x41544145
456 * Test to see if we have a valid card.
458 if ((conf->signature == BE_EATA_SIGNATURE) ||
459 (conf->signature == LE_EATA_SIGNATURE)) {
461 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
462 inw(base + HA_RDATA);
471 * Read a configuration page into the supplied dpt_cont_t buffer.
474 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
475 u_int size, u_int page, u_int target, int extent)
485 cp = &dccb->eata_ccb;
486 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
490 cp->Auto_Req_Sen = 1;
491 cp->reqlen = sizeof(struct scsi_sense_data);
494 cp->cp_LUN = 0; /* In the EATA packet */
495 cp->cp_lun = 0; /* In the SCSI command */
497 cp->cp_scsi_cmd = INQUIRY;
500 cp->cp_extent = extent;
503 cp->cp_channel = 0; /* DNC, Interpret mode is set */
505 cp->cp_datalen = htonl(size);
510 * This could be a simple for loop, but we suspected the compiler To
511 * have optimized it a bit too much. Wait for the controller to
514 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
515 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
516 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
517 || (dpt_wait(dpt, HA_SBUSY, 0))) {
520 * RAID Drives still Spinning up? (This should only occur if
521 * the DPT controller is in a NON PC (PCI?) platform).
523 if (dpt_raid_busy(dpt)) {
524 printf("dpt%d WARNING: Get_conf() RSUS failed.\n",
531 DptStat_Reset_BUSY(dpt->sp);
534 * XXXX We might want to do something more clever than aborting at
535 * this point, like resetting (rebooting) the controller and trying
538 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
539 EATA_CMD_DMA_SEND_CP,
540 10000, 0, 0, 0)) != 0) {
541 printf("dpt%d WARNING: Get_conf() failed (%d) to send "
542 "EATA_CMD_DMA_READ_CONFIG\n",
547 /* Wait for two seconds for a response. This can be slow */
550 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
555 /* Grab the status and clear interrupts */
556 status = dpt_inb(dpt, HA_RSTATUS);
561 * Check the status carefully. Return only if the
562 * command was successful.
564 if (((status & HA_SERROR) == 0)
565 && (dpt->sp->hba_stat == 0)
566 && (dpt->sp->scsi_stat == 0)
567 && (dpt->sp->residue_len == 0))
570 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
576 /* Detect Cache parameters and size */
578 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
590 * Default setting, for best perfromance..
591 * This is what virtually all cards default to..
593 dpt->cache_type = DPT_CACHE_WRITEBACK;
596 cp = &dccb->eata_ccb;
597 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
600 /* Setup the command structure */
603 cp->Auto_Req_Sen = 1;
604 cp->reqlen = sizeof(struct scsi_sense_data);
606 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
607 cp->cp_LUN = 0; /* In the EATA packet */
608 cp->cp_lun = 0; /* In the SCSI command */
611 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
620 * Build the EATA Command Packet structure
621 * for a Log Sense Command.
623 cp->cp_cdb[0] = 0x4d;
625 cp->cp_cdb[2] = 0x40 | 0x33;
628 cp->cp_datalen = htonl(512);
631 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
632 EATA_CMD_DMA_SEND_CP,
635 printf("dpt%d WARNING: detect_cache() failed (%d) to send "
636 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
640 /* Wait for two seconds for a response. This can be slow... */
643 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
648 /* Grab the status and clear interrupts */
649 status = dpt_inb(dpt, HA_RSTATUS);
655 if (buff[0] != 0x33) {
658 bytes = DPT_HCP_LENGTH(buff);
659 param = DPT_HCP_FIRST(buff);
661 if (DPT_HCP_CODE(param) != 1) {
663 * DPT Log Page layout error
665 printf("dpt%d: NOTICE: Log Page (1) layout error\n",
669 if (!(param[4] & 0x4)) {
670 dpt->cache_type = DPT_NO_CACHE;
673 while (DPT_HCP_CODE(param) != 6) {
674 param = DPT_HCP_NEXT(param);
676 || (param >= &buff[bytes])) {
681 if (param[4] & 0x2) {
685 dpt->cache_type = DPT_NO_CACHE;
689 if (param[4] & 0x4) {
690 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
693 /* XXX This isn't correct. This log parameter only has two bytes.... */
695 dpt->cache_size = param[5]
703 dpt_poll(struct cam_sim *sim)
705 dpt_intr(cam_sim_softc(sim));
709 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
711 struct dpt_ccb *dccb;
713 struct dpt_softc *dpt;
716 dccb = (struct dpt_ccb *)arg;
718 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
722 printf("dpt%d: Unexepected error 0x%x returned from "
723 "bus_dmamap_load\n", dpt->unit, error);
724 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
725 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
726 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
728 dptfreeccb(dpt, dccb);
735 bus_dma_segment_t *end_seg;
738 end_seg = dm_segs + nseg;
740 /* Copy the segments into our SG list */
742 while (dm_segs < end_seg) {
743 sg->seg_len = htonl(dm_segs->ds_len);
744 sg->seg_addr = htonl(dm_segs->ds_addr);
750 dccb->eata_ccb.scatter = 1;
751 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
752 dccb->eata_ccb.cp_datalen =
753 htonl(nseg * sizeof(dpt_sg_t));
755 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
756 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
759 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
760 op = BUS_DMASYNC_PREREAD;
762 op = BUS_DMASYNC_PREWRITE;
764 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
767 dccb->eata_ccb.cp_dataDMA = 0;
768 dccb->eata_ccb.cp_datalen = 0;
774 * Last time we need to check if this CCB needs to
777 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
779 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
780 dptfreeccb(dpt, dccb);
786 dccb->state |= DCCB_ACTIVE;
787 ccb->ccb_h.status |= CAM_SIM_QUEUED;
788 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
789 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
791 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
792 dccb->eata_ccb.cp_busaddr,
793 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
794 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
796 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
797 dptfreeccb(dpt, dccb);
805 dpt_action(struct cam_sim *sim, union ccb *ccb)
807 struct dpt_softc *dpt;
809 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
811 dpt = (struct dpt_softc *)cam_sim_softc(sim);
813 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
814 xpt_print_path(ccb->ccb_h.path);
815 printf("controller is shutdown. Aborting CCB.\n");
816 ccb->ccb_h.status = CAM_NO_HBA;
821 switch (ccb->ccb_h.func_code) {
822 /* Common cases first */
823 case XPT_SCSI_IO: /* Execute the requested I/O operation */
825 struct ccb_scsiio *csio;
826 struct ccb_hdr *ccbh;
827 struct dpt_ccb *dccb;
828 struct eata_ccb *eccb;
832 /* Max CDB length is 12 bytes */
833 if (csio->cdb_len > 12) {
834 ccb->ccb_h.status = CAM_REQ_INVALID;
838 if ((dccb = dptgetccb(dpt)) == NULL) {
842 dpt->resource_shortage = 1;
844 xpt_freeze_simq(sim, /*count*/1);
845 ccb->ccb_h.status = CAM_REQUEUE_REQ;
849 eccb = &dccb->eata_ccb;
851 /* Link dccb and ccb so we can find one from the other */
853 ccb->ccb_h.ccb_dccb_ptr = dccb;
854 ccb->ccb_h.ccb_dpt_ptr = dpt;
857 * Explicitly set all flags so that the compiler can
858 * be smart about setting them.
860 eccb->SCSI_Reset = 0;
862 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
867 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
869 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
870 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
871 eccb->reqlen = csio->sense_len;
872 eccb->cp_id = ccb->ccb_h.target_id;
873 eccb->cp_channel = cam_sim_bus(sim);
874 eccb->cp_LUN = ccb->ccb_h.target_lun;
876 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
878 eccb->cp_identify = 1;
880 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
881 && csio->tag_action != CAM_TAG_ACTION_NONE) {
882 eccb->cp_msg[0] = csio->tag_action;
883 eccb->cp_msg[1] = dccb->tag;
890 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
891 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
892 bcopy(csio->cdb_io.cdb_ptr,
893 eccb->cp_cdb, csio->cdb_len);
895 /* I guess I could map it in... */
896 ccb->ccb_h.status = CAM_REQ_INVALID;
897 dptfreeccb(dpt, dccb);
902 bcopy(csio->cdb_io.cdb_bytes,
903 eccb->cp_cdb, csio->cdb_len);
906 * If we have any data to send with this command,
907 * map it into bus space.
909 /* Only use S/G if there is a transfer */
910 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
911 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
913 * We've been given a pointer
914 * to a single buffer.
916 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
922 bus_dmamap_load(dpt->buffer_dmat,
928 if (error == EINPROGRESS) {
930 * So as to maintain ordering,
931 * freeze the controller queue
932 * until our mapping is
935 xpt_freeze_simq(sim, 1);
936 dccb->state |= CAM_RELEASE_SIMQ;
940 struct bus_dma_segment seg;
942 /* Pointer to physical buffer */
944 (bus_addr_t)csio->data_ptr;
945 seg.ds_len = csio->dxfer_len;
946 dptexecuteccb(dccb, &seg, 1, 0);
949 struct bus_dma_segment *segs;
951 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
952 panic("dpt_action - Physical "
956 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
957 panic("dpt_action - Virtual "
961 /* Just use the segments provided */
962 segs = (struct bus_dma_segment *)csio->data_ptr;
963 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
968 * Does it want them both on or both off?
969 * CAM_DIR_NONE is both on, so this code can
970 * be removed if this is also what the DPT
975 dptexecuteccb(dccb, NULL, 0, 0);
979 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
980 case XPT_ABORT: /* Abort the specified CCB */
982 ccb->ccb_h.status = CAM_REQ_INVALID;
985 case XPT_SET_TRAN_SETTINGS:
987 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
991 case XPT_GET_TRAN_SETTINGS:
992 /* Get default/user set transfer settings for the target */
994 struct ccb_trans_settings *cts;
998 target_mask = 0x01 << ccb->ccb_h.target_id;
999 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1000 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
1001 cts->bus_width = (dpt->max_id > 7)
1002 ? MSG_EXT_WDTR_BUS_8_BIT
1003 : MSG_EXT_WDTR_BUS_16_BIT;
1004 cts->sync_period = 25; /* 10MHz */
1006 if (cts->sync_period != 0)
1007 cts->sync_offset = 15;
1009 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1010 | CCB_TRANS_SYNC_OFFSET_VALID
1011 | CCB_TRANS_BUS_WIDTH_VALID
1012 | CCB_TRANS_DISC_VALID
1013 | CCB_TRANS_TQ_VALID;
1014 ccb->ccb_h.status = CAM_REQ_CMP;
1016 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1021 case XPT_CALC_GEOMETRY:
1023 struct ccb_calc_geometry *ccg;
1025 u_int32_t secs_per_cylinder;
1029 * XXX Use Adaptec translation until I find out how to
1030 * get this information from the card.
1033 size_mb = ccg->volume_size
1034 / ((1024L * 1024L) / ccg->block_size);
1037 if (size_mb > 1024 && extended) {
1039 ccg->secs_per_track = 63;
1042 ccg->secs_per_track = 32;
1044 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1045 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1046 ccb->ccb_h.status = CAM_REQ_CMP;
1050 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1053 ccb->ccb_h.status = CAM_REQ_CMP;
1057 case XPT_TERM_IO: /* Terminate the I/O process */
1059 ccb->ccb_h.status = CAM_REQ_INVALID;
1062 case XPT_PATH_INQ: /* Path routing inquiry */
1064 struct ccb_pathinq *cpi = &ccb->cpi;
1066 cpi->version_num = 1;
1067 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1068 if (dpt->max_id > 7)
1069 cpi->hba_inquiry |= PI_WIDE_16;
1070 cpi->target_sprt = 0;
1072 cpi->hba_eng_cnt = 0;
1073 cpi->max_target = dpt->max_id;
1074 cpi->max_lun = dpt->max_lun;
1075 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1076 cpi->bus_id = cam_sim_bus(sim);
1077 cpi->base_transfer_speed = 3300;
1078 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1079 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1080 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1081 cpi->unit_number = cam_sim_unit(sim);
1082 cpi->ccb_h.status = CAM_REQ_CMP;
1087 ccb->ccb_h.status = CAM_REQ_INVALID;
1094 * This routine will try to send an EATA command to the DPT HBA.
1095 * It will, by default, try 20,000 times, waiting 50us between tries.
1096 * It returns 0 on success and 1 on failure.
1097 * It is assumed to be called at splcam().
1100 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1101 u_int32_t cmd_busaddr, u_int command, u_int retries,
1102 u_int ifc, u_int code, u_int code2)
1110 * I hate this polling nonsense. Wish there was a way to tell the DPT
1111 * to go get commands at its own pace, or to interrupt when ready.
1112 * In the mean time we will measure how many itterations it really
1115 for (loop = 0; loop < retries; loop++) {
1116 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1122 if (loop < retries) {
1123 #ifdef DPT_MEASURE_PERFORMANCE
1124 if (loop > dpt->performance.max_eata_tries)
1125 dpt->performance.max_eata_tries = loop;
1127 if (loop < dpt->performance.min_eata_tries)
1128 dpt->performance.min_eata_tries = loop;
1131 #ifdef DPT_MEASURE_PERFORMANCE
1132 ++dpt->performance.command_too_busy;
1137 /* The controller is alive, advance the wedge timer */
1138 #ifdef DPT_RESET_HBA
1139 dpt->last_contact = microtime_now;
1142 if (cmd_block == NULL)
1144 #if (BYTE_ORDER == BIG_ENDIAN)
1146 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1147 | ((cmd_busaddr >> 16) & 0xFF)
1148 | ((cmd_busaddr >> 8) & 0xFF)
1149 | (cmd_busaddr & 0xFF);
1152 /* And now the address */
1153 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1155 if (command == EATA_CMD_IMMEDIATE) {
1156 if (cmd_block == NULL) {
1157 dpt_outb(dpt, HA_WCODE2, code2);
1158 dpt_outb(dpt, HA_WCODE, code);
1160 dpt_outb(dpt, HA_WIFC, ifc);
1162 dpt_outb(dpt, HA_WCOMMAND, command);
1168 /* ==================== Exported Function definitions =======================*/
1170 dpt_alloc(device_t dev, bus_space_tag_t tag, bus_space_handle_t bsh)
1172 dpt_softc_t *dpt = device_get_softc(dev);
1175 bzero(dpt, sizeof(dpt_softc_t));
1178 dpt->unit = device_get_unit(dev);
1179 SLIST_INIT(&dpt->free_dccb_list);
1180 LIST_INIT(&dpt->pending_ccb_list);
1181 TAILQ_INSERT_TAIL(&dpt_softcs, dpt, links);
1182 for (i = 0; i < MAX_CHANNELS; i++)
1183 dpt->resetlevel[i] = DPT_HA_OK;
1185 #ifdef DPT_MEASURE_PERFORMANCE
1186 dpt_reset_performance(dpt);
1187 #endif /* DPT_MEASURE_PERFORMANCE */
1192 dpt_free(struct dpt_softc *dpt)
1194 switch (dpt->init_level) {
1197 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1199 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1201 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1203 bus_dma_tag_destroy(dpt->dccb_dmat);
1205 bus_dma_tag_destroy(dpt->buffer_dmat);
1208 struct sg_map_node *sg_map;
1210 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1211 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1212 bus_dmamap_unload(dpt->sg_dmat,
1214 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1216 free(sg_map, M_DEVBUF);
1218 bus_dma_tag_destroy(dpt->sg_dmat);
1223 TAILQ_REMOVE(&dpt_softcs, dpt, links);
1226 static u_int8_t string_sizes[] =
1228 sizeof(((dpt_inq_t*)NULL)->vendor),
1229 sizeof(((dpt_inq_t*)NULL)->modelNum),
1230 sizeof(((dpt_inq_t*)NULL)->firmware),
1231 sizeof(((dpt_inq_t*)NULL)->protocol),
1235 dpt_init(struct dpt_softc *dpt)
1238 struct sg_map_node *sg_map;
1245 dpt->init_level = 0;
1246 SLIST_INIT(&dpt->sg_maps);
1248 #ifdef DPT_RESET_BOARD
1249 printf("dpt%d: resetting HBA\n", dpt->unit);
1250 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1252 /* XXX Shouldn't we poll a status register or something??? */
1254 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1255 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1256 /*lowaddr*/BUS_SPACE_MAXADDR,
1257 /*highaddr*/BUS_SPACE_MAXADDR,
1258 /*filter*/NULL, /*filterarg*/NULL,
1259 PAGE_SIZE, /*nsegments*/1,
1260 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1261 /*flags*/0, &dpt->sg_dmat) != 0) {
1268 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1269 * memory. To get the allocation size, we need to know how many
1270 * ccbs the card supports. This requires a ccb. We solve this
1271 * chicken and egg problem by allocating some re-usable S/G space
1272 * up front, and treating it as our status packet, CCB, and target
1273 * memory space for these commands.
1275 sg_map = dptallocsgmap(dpt);
1279 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1280 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1281 bzero(dccb, sizeof(*dccb));
1282 dpt->sp_physaddr = sg_map->sg_physaddr;
1283 dccb->eata_ccb.cp_dataDMA =
1284 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1285 dccb->eata_ccb.cp_busaddr = ~0;
1286 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1287 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1288 + offsetof(struct dpt_ccb, sense_data));
1290 /* Okay. Fetch our config */
1291 bzero(&dccb[1], sizeof(conf)); /* data area */
1292 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1293 sizeof(conf), 0xc1, 7, 1);
1296 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1299 bcopy(&dccb[1], &conf, sizeof(conf));
1301 bzero(&dccb[1], sizeof(dpt->board_data));
1302 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1303 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1305 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1308 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1310 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1311 (u_int8_t *)&dccb[1]);
1313 switch (ntohl(conf.splen)) {
1315 dpt->EATA_revision = 'a';
1318 dpt->EATA_revision = 'b';
1321 dpt->EATA_revision = 'c';
1324 dpt->EATA_revision = 'z';
1327 dpt->EATA_revision = '?';
1330 dpt->max_id = conf.MAX_ID;
1331 dpt->max_lun = conf.MAX_LUN;
1332 dpt->irq = conf.IRQ;
1333 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1334 dpt->channels = conf.MAX_CHAN + 1;
1335 dpt->state |= DPT_HA_OK;
1337 dpt->primary = FALSE;
1339 dpt->primary = TRUE;
1341 dpt->more_support = conf.MORE_support;
1343 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1344 dpt->immediate_support = 1;
1346 dpt->immediate_support = 0;
1348 dpt->broken_INQUIRY = FALSE;
1350 dpt->cplen = ntohl(conf.cplen);
1351 dpt->cppadlen = ntohs(conf.cppadlen);
1352 dpt->max_dccbs = ntohs(conf.queuesiz);
1354 if (dpt->max_dccbs > 256) {
1355 printf("dpt%d: Max CCBs reduced from %d to "
1356 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1357 dpt->max_dccbs = 256;
1360 dpt->hostid[0] = conf.scsi_id0;
1361 dpt->hostid[1] = conf.scsi_id1;
1362 dpt->hostid[2] = conf.scsi_id2;
1367 dpt->sgsize = ntohs(conf.SGsiz);
1369 /* We can only get 64k buffers, so don't bother to waste space. */
1370 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1373 if (dpt->sgsize > dpt_max_segs)
1374 dpt->sgsize = dpt_max_segs;
1376 /* DMA tag for mapping buffers into device visible space. */
1377 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1378 /*lowaddr*/BUS_SPACE_MAXADDR,
1379 /*highaddr*/BUS_SPACE_MAXADDR,
1380 /*filter*/NULL, /*filterarg*/NULL,
1381 /*maxsize*/MAXBSIZE, /*nsegments*/dpt->sgsize,
1382 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1383 /*flags*/BUS_DMA_ALLOCNOW,
1384 &dpt->buffer_dmat) != 0) {
1385 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1391 /* DMA tag for our ccb structures and interrupt status packet */
1392 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1393 /*lowaddr*/BUS_SPACE_MAXADDR,
1394 /*highaddr*/BUS_SPACE_MAXADDR,
1395 /*filter*/NULL, /*filterarg*/NULL,
1396 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1399 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1400 /*flags*/0, &dpt->dccb_dmat) != 0) {
1401 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1407 /* Allocation for our ccbs and interrupt status packet */
1408 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1409 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1410 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1416 /* And permanently map them */
1417 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1419 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1421 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1423 /* Clear them out. */
1424 bzero(dpt->dpt_dccbs,
1425 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1427 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1429 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1430 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1431 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1434 /* Allocate our first batch of ccbs */
1435 if (dptallocccbs(dpt) == 0) {
1436 printf("dpt: dptallocccbs(dpt) == 0\n");
1440 /* Prepare for Target Mode */
1441 dpt->target_mode_enabled = 1;
1443 /* Nuke excess spaces from inquiry information */
1444 strp = dpt->board_data.vendor;
1445 for (i = 0; i < sizeof(string_sizes); i++) {
1446 index = string_sizes[i] - 1;
1447 while (index && (strp[index] == ' '))
1448 strp[index--] = '\0';
1449 strp += string_sizes[i];
1452 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1453 dpt->unit, dpt->board_data.vendor,
1454 dpt->board_data.modelNum, dpt->board_data.firmware);
1456 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1458 if (dpt->cache_type != DPT_NO_CACHE
1459 && dpt->cache_size != 0) {
1460 printf("%s Cache, ",
1461 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1462 ? "Write-Through" : "Write-Back");
1465 printf("%d CCBs\n", dpt->max_dccbs);
1473 dpt_attach(dpt_softc_t *dpt)
1475 struct cam_devq *devq;
1479 * Create the device queue for our SIM.
1481 devq = cam_simq_alloc(dpt->max_dccbs);
1485 for (i = 0; i < dpt->channels; i++) {
1487 * Construct our SIM entry
1489 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1490 dpt, dpt->unit, /*untagged*/2,
1491 /*tagged*/dpt->max_dccbs, devq);
1492 if (xpt_bus_register(dpt->sims[i], i) != CAM_SUCCESS) {
1493 cam_sim_free(dpt->sims[i]);
1497 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1498 cam_sim_path(dpt->sims[i]),
1499 CAM_TARGET_WILDCARD,
1500 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1501 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1502 cam_sim_free(dpt->sims[i]);
1507 cam_simq_release(devq);
1509 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1510 dpt, SHUTDOWN_PRI_DEFAULT);
1516 * This is the interrupt handler for the DPT driver.
1528 u_int32_t residue_len; /* Number of bytes not transferred */
1530 dpt = (dpt_softc_t *)arg;
1532 /* First order of business is to check if this interrupt is for us */
1533 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1536 * What we want to do now, is to capture the status, all of it,
1537 * move it where it belongs, wake up whoever sleeps waiting to
1538 * process this result, and get out of here.
1540 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1541 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1542 printf("Encountered bogus status packet\n");
1543 status = dpt_inb(dpt, HA_RSTATUS);
1547 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1549 dpt->sp->ccb_busaddr = ~0;
1551 /* Ignore status packets with EOC not set */
1552 if (dpt->sp->EOC == 0) {
1553 printf("dpt%d ERROR: Request %d received with "
1554 "clear EOC.\n Marking as LOST.\n",
1555 dpt->unit, dccb->transaction_id);
1557 #ifdef DPT_HANDLE_TIMEOUTS
1558 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1560 /* This CLEARS the interrupt! */
1561 status = dpt_inb(dpt, HA_RSTATUS);
1567 * Double buffer the status information so the hardware can
1568 * work on updating the status packet while we decifer the
1569 * one we were just interrupted for.
1570 * According to Mark Salyzyn, we only need few pieces of it.
1572 hba_stat = dpt->sp->hba_stat;
1573 scsi_stat = dpt->sp->scsi_stat;
1574 residue_len = dpt->sp->residue_len;
1576 /* Clear interrupts, check for error */
1577 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1579 * Error Condition. Check for magic cookie. Exit
1580 * this test on earliest sign of non-reset condition
1583 /* Check that this is not a board reset interrupt */
1584 if (dpt_just_reset(dpt)) {
1585 printf("dpt%d: HBA rebooted.\n"
1586 " All transactions should be "
1590 printf("dpt%d: >>---->> This is incomplete, "
1591 "fix me.... <<----<<", dpt->unit);
1592 panic("DPT Rebooted");
1598 callout_stop(&ccb->ccb_h.timeout_ch);
1599 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1600 bus_dmasync_op_t op;
1602 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1603 op = BUS_DMASYNC_POSTREAD;
1605 op = BUS_DMASYNC_POSTWRITE;
1606 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1607 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1610 /* Common Case inline... */
1611 if (hba_stat == HA_NO_ERROR) {
1612 ccb->csio.scsi_status = scsi_stat;
1613 ccb->ccb_h.status = 0;
1614 switch (scsi_stat) {
1615 case SCSI_STATUS_OK:
1616 ccb->ccb_h.status |= CAM_REQ_CMP;
1618 case SCSI_STATUS_CHECK_COND:
1619 case SCSI_STATUS_CMD_TERMINATED:
1620 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1621 ccb->csio.sense_len);
1622 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1625 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1626 /* XXX Freeze DevQ */
1629 ccb->csio.resid = residue_len;
1630 dptfreeccb(dpt, dccb);
1633 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1640 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1641 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1643 ccb->csio.resid = resid;
1646 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1649 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1651 case HA_SCSIBUS_RESET:
1652 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1653 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1656 case HA_CP_RESET: /* XXX ??? */
1657 case HA_CP_ABORT_NA: /* XXX ??? */
1658 case HA_CP_RESET_NA: /* XXX ??? */
1659 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1660 ccb->ccb_h.status = CAM_REQ_ABORTED;
1665 case HA_PCI_STABORT:
1669 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1671 case HA_UNX_MSGRJCT:
1672 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1674 case HA_UNX_BUSPHASE:
1675 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1677 case HA_UNX_BUS_FREE:
1678 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1681 case HA_RESET_STUCK:
1683 * Dead??? Can the controller get unstuck
1684 * from these conditions
1686 ccb->ccb_h.status = CAM_NO_HBA;
1688 case HA_RSENSE_FAIL:
1689 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1692 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1693 printf("Please mail this message to shimon@simon-shapiro.org\n");
1694 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1697 dptfreeccb(dpt, dccb);
1702 dpttimeout(void *arg)
1704 struct dpt_ccb *dccb;
1706 struct dpt_softc *dpt;
1709 dccb = (struct dpt_ccb *)arg;
1711 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1712 xpt_print_path(ccb->ccb_h.path);
1713 printf("CCB %p - timed out\n", (void *)dccb);
1718 * Try to clear any pending jobs. FreeBSD will loose interrupts,
1719 * leaving the controller suspended, and commands timed-out.
1720 * By calling the interrupt handler, any command thus stuck will be
1725 if ((dccb->state & DCCB_ACTIVE) == 0) {
1726 xpt_print_path(ccb->ccb_h.path);
1727 printf("CCB %p - timed out CCB already completed\n",
1733 /* Abort this particular command. Leave all others running */
1734 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1735 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1736 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1741 * Shutdown the controller and ensure that the cache is completely flushed.
1742 * Called from the shutdown_final event after all disk access has completed.
1745 dptshutdown(void *arg, int howto)
1749 dpt = (dpt_softc_t *)arg;
1751 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1755 * What we do for a shutdown, is give the DPT early power loss warning
1757 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1758 DELAY(1000 * 1000 * 5);
1759 printf("dpt%d: Controller was warned of shutdown and is now "
1760 "disabled\n", dpt->unit);
1763 /*============================================================================*/
1766 #ifdef DPT_RESET_HBA
1769 ** Function name : dpt_reset_hba
1771 ** Description : Reset the HBA and properly discard all pending work
1776 dpt_reset_hba(dpt_softc_t *dpt)
1780 dpt_ccb_t dccb, *dccbp;
1782 struct scsi_xfer *xs;
1784 /* Prepare a control block. The SCSI command part is immaterial */
1787 dccb.state = DPT_CCB_STATE_NEW;
1788 dccb.std_callback = NULL;
1789 dccb.wrbuff_callback = NULL;
1791 ccb = &dccb.eata_ccb;
1792 ccb->CP_OpCode = EATA_CMD_RESET;
1793 ccb->SCSI_Reset = 0;
1795 ccb->Auto_Req_Sen = 1;
1796 ccb->cp_id = 0; /* Should be ignored */
1800 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1801 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1802 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1803 ccb->cp_viraddr = (u_int32_t) & ccb;
1805 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1806 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1808 /* Lock up the submitted queue. We are very persistant here */
1810 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1814 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1817 /* Send the RESET message */
1818 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1819 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1820 printf("dpt%d: Failed to send the RESET message.\n"
1821 " Trying cold boot (ouch!)\n", dpt->unit);
1824 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1825 EATA_COLD_BOOT, 0, 0,
1827 panic("dpt%d: Faild to cold boot the HBA\n",
1830 #ifdef DPT_MEASURE_PERFORMANCE
1831 dpt->performance.cold_boots++;
1832 #endif /* DPT_MEASURE_PERFORMANCE */
1835 #ifdef DPT_MEASURE_PERFORMANCE
1836 dpt->performance.warm_starts++;
1837 #endif /* DPT_MEASURE_PERFORMANCE */
1839 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1842 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1843 struct scsi_xfer *xs = dccbp->xs;
1845 /* Not all transactions have xs structs */
1847 /* Tell the kernel proper this did not complete well */
1848 xs->error |= XS_SELTIMEOUT;
1849 xs->flags |= SCSI_ITSDONE;
1853 dpt_Qremove_submitted(dpt, dccbp);
1855 /* Remember, Callbacks are NOT in the standard queue */
1856 if (dccbp->std_callback != NULL) {
1857 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1861 dpt_Qpush_free(dpt, dccbp);
1866 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1867 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1870 #endif /* DPT_RESET_HBA */
1873 * Build a Command Block for target mode READ/WRITE BUFFER,
1874 * with the ``sync'' bit ON.
1876 * Although the length and offset are 24 bit fields in the command, they cannot
1877 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1878 * If they are sensless, we round them to zero offset, maximum length and
1883 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1884 dpt_ccb_t * ccb, int mode, u_int8_t command,
1885 u_int16_t length, u_int16_t offset)
1890 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
1891 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
1892 dpt->unit, length, offset);
1893 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
1898 ccb->state = DPT_CCB_STATE_NEW;
1899 ccb->std_callback = (ccb_callback) dpt_target_done;
1900 ccb->wrbuff_callback = NULL;
1902 cp = &ccb->eata_ccb;
1903 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
1906 cp->Auto_Req_Sen = 1;
1911 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
1912 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
1913 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
1914 cp->cp_viraddr = (u_int32_t) & ccb;
1916 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1918 cp->cp_scsi_cmd = command;
1919 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
1920 cp->cp_lun = lun; /* Order is important here! */
1921 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
1922 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
1923 cp->cp_cdb[4] = (length >> 8) & 0xFF;
1924 cp->cp_cdb[5] = length & 0xFF;
1925 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
1926 cp->cp_cdb[7] = (length >> 8) & 0xFF;
1927 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
1928 cp->cp_cdb[9] = 0; /* No sync, no match bits */
1931 * This could be optimized to live in dpt_register_buffer.
1932 * We keep it here, just in case the kernel decides to reallocate pages
1934 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
1935 dpt->rw_buffer[bus][target][lun])) {
1936 printf("dpt%d: Failed to setup Scatter/Gather for "
1937 "Target-Mode buffer\n", dpt->unit);
1941 /* Setup a target mode READ command */
1944 dpt_set_target(int redo, dpt_softc_t * dpt,
1945 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
1946 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
1950 if (dpt->target_mode_enabled) {
1954 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
1955 SCSI_TM_READ_BUFFER, length, offset);
1957 ccb->transaction_id = ++dpt->commands_processed;
1959 #ifdef DPT_MEASURE_PERFORMANCE
1960 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
1961 ccb->command_started = microtime_now;
1963 dpt_Qadd_waiting(dpt, ccb);
1964 dpt_sched_queue(dpt);
1968 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
1974 * Schedule a buffer to be sent to another target.
1975 * The work will be scheduled and the callback provided will be called when
1976 * the work is actually done.
1978 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
1979 * get notified of receipt of buffers.
1983 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
1984 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
1985 buff_wr_done callback)
1988 dpt_ccb_t *ccb = NULL;
1991 /* This is an external call. Be a bit paranoid */
1992 for (dpt = TAILQ_FIRST(&dpt_softc_list);
1994 dpt = TAILQ_NEXT(dpt, links)) {
1995 if (dpt->unit == unit)
1999 return (INVALID_UNIT);
2003 if (dpt->target_mode_enabled) {
2004 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2005 (lun > dpt->max_lun)) {
2006 return (INVALID_SENDER);
2008 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2009 (dpt->buffer_receiver[channel][target][lun] == NULL))
2010 return (NOT_REGISTERED);
2012 ospl = splsoftcam();
2013 /* Process the free list */
2014 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2015 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2016 " Please try later\n",
2019 return (NO_RESOURCES);
2021 /* Now grab the newest CCB */
2022 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2024 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2028 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2029 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2030 SCSI_TM_WRITE_BUFFER,
2032 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2035 ccb->transaction_id = ++dpt->commands_processed;
2037 #ifdef DPT_MEASURE_PERFORMANCE
2038 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2039 ccb->command_started = microtime_now;
2041 dpt_Qadd_waiting(dpt, ccb);
2042 dpt_sched_queue(dpt);
2047 return (DRIVER_DOWN);
2051 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2056 cp = &ccb->eata_ccb;
2059 * Remove the CCB from the waiting queue.
2060 * We do NOT put it back on the free, etc., queues as it is a special
2061 * ccb, owned by the dpt_softc of this unit.
2063 ospl = splsoftcam();
2064 dpt_Qremove_completed(dpt, ccb);
2067 #define br_channel (ccb->eata_ccb.cp_channel)
2068 #define br_target (ccb->eata_ccb.cp_id)
2069 #define br_lun (ccb->eata_ccb.cp_LUN)
2070 #define br_index [br_channel][br_target][br_lun]
2071 #define read_buffer_callback (dpt->buffer_receiver br_index )
2072 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2073 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2074 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2075 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2077 /* Different reasons for being here, you know... */
2078 switch (ccb->eata_ccb.cp_scsi_cmd) {
2079 case SCSI_TM_READ_BUFFER:
2080 if (read_buffer_callback != NULL) {
2081 /* This is a buffer generated by a kernel process */
2082 read_buffer_callback(dpt->unit, br_channel,
2085 br_offset, br_length);
2088 * This is a buffer waited for by a user (sleeping)
2094 /* We ALWAYS re-issue the same command; args are don't-care */
2095 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2098 case SCSI_TM_WRITE_BUFFER:
2099 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2100 br_offset, br_length,
2101 br_lun, ccb->status_packet.hba_stat);
2104 printf("dpt%d: %s is an unsupported command for target mode\n",
2105 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2107 ospl = splsoftcam();
2108 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2109 dpt_Qpush_free(dpt, ccb);
2115 * Use this function to register a client for a buffer read target operation.
2116 * The function you register will be called every time a buffer is received
2117 * by the target mode code.
2120 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2121 u_int8_t mode, u_int16_t length, u_int16_t offset,
2122 dpt_rec_buff callback, dpt_rb_op_t op)
2125 dpt_ccb_t *ccb = NULL;
2128 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2130 dpt = TAILQ_NEXT(dpt, links)) {
2131 if (dpt->unit == unit)
2135 return (INVALID_UNIT);
2139 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2140 return (DRIVER_DOWN);
2142 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2143 (lun > (dpt->max_lun - 1)))
2144 return (INVALID_SENDER);
2146 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2147 if (op == REGISTER_BUFFER) {
2148 /* Assign the requested callback */
2149 dpt->buffer_receiver[channel][target][lun] = callback;
2151 ospl = splsoftcam();
2153 /* Process the free list */
2154 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2155 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2156 " Please try later\n",
2159 return (NO_RESOURCES);
2161 /* Now grab the newest CCB */
2162 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2164 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2169 /* Clean up the leftover of the previous tenant */
2170 ccb->status = DPT_CCB_STATE_NEW;
2171 dpt->target_ccb[channel][target][lun] = ccb;
2173 dpt->rw_buffer[channel][target][lun] =
2174 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_INTWAIT);
2175 dpt_set_target(0, dpt, channel, target, lun, mode,
2176 length, offset, ccb);
2177 return (SUCCESSFULLY_REGISTERED);
2179 return (NOT_REGISTERED);
2181 if (op == REGISTER_BUFFER) {
2182 if (dpt->buffer_receiver[channel][target][lun] == callback)
2183 return (ALREADY_REGISTERED);
2185 return (REGISTERED_TO_ANOTHER);
2187 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2188 dpt->buffer_receiver[channel][target][lun] = NULL;
2189 ospl = splsoftcam();
2190 dpt_Qpush_free(dpt, ccb);
2192 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2193 return (SUCCESSFULLY_REGISTERED);
2195 return (INVALID_CALLBACK);
2201 /* Return the state of the blinking DPT LED's */
2203 dpt_blinking_led(dpt_softc_t * dpt)
2215 for (ndx = 0, state = 0, previous = 0;
2216 (ndx < 10) && (state != previous);
2219 state = dpt_inl(dpt, 1);
2222 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2223 result = dpt_inb(dpt, 5);
2230 * Execute a command which did not come from the kernel's SCSI layer.
2231 * The only way to map user commands to bus and target is to comply with the
2232 * standard DPT wire-down scheme:
2235 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2236 caddr_t cmdarg, int minor_no)
2240 int channel, target, lun;
2247 channel = minor2hba(minor_no);
2248 target = minor2target(minor_no);
2249 lun = minor2lun(minor_no);
2251 if ((channel > (dpt->channels - 1))
2252 || (target > dpt->max_id)
2253 || (lun > dpt->max_lun))
2256 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2257 /* This one is for the controller itself */
2258 if ((user_cmd->eataID[0] != 'E')
2259 || (user_cmd->eataID[1] != 'A')
2260 || (user_cmd->eataID[2] != 'T')
2261 || (user_cmd->eataID[3] != 'A')) {
2265 /* Get a DPT CCB, so we can prepare a command */
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",
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", dpt->unit);
2282 /* Clean up the leftover of the previous tenant */
2283 ccb->status = DPT_CCB_STATE_NEW;
2286 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2287 sizeof(eata_ccb_t));
2289 /* We do not want to do user specified scatter/gather. Why?? */
2290 if (ccb->eata_ccb.scatter == 1)
2293 ccb->eata_ccb.Auto_Req_Sen = 1;
2294 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2295 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2296 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2297 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2298 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2299 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2301 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2302 /* Data I/O is involved in this command. Alocate buffer */
2303 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2304 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2305 M_TEMP, M_WAITOK, 0, ~0,
2306 ccb->eata_ccb.cp_datalen,
2309 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2314 printf("dpt%d: Cannot allocate %d bytes "
2315 "for EATA command\n", dpt->unit,
2316 ccb->eata_ccb.cp_datalen);
2319 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2320 if (ccb->eata_ccb.DataIn == 1) {
2321 if (copyin(usr_cmd_DMA,
2322 data, ccb->eata_ccb.cp_datalen) == -1)
2326 /* No data I/O involved here. Make sure the DPT knows that */
2327 ccb->eata_ccb.cp_datalen = 0;
2331 if (ccb->eata_ccb.FWNEST == 1)
2332 ccb->eata_ccb.FWNEST = 0;
2334 if (ccb->eata_ccb.cp_datalen != 0) {
2335 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2343 * We are required to quiet a SCSI bus.
2344 * since we do not queue comands on a bus basis,
2345 * we wait for ALL commands on a controller to complete.
2346 * In the mean time, sched_queue() will not schedule new commands.
2348 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2349 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2350 /* We wait for ALL traffic for this HBa to subside */
2351 ospl = splsoftcam();
2352 dpt->state |= DPT_HA_QUIET;
2355 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2356 huh = tsleep((void *) dpt, PCATCH, "dptqt", 100 * hz);
2359 /* Wakeup call received */
2370 /* Resume normal operation */
2371 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2372 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2373 ospl = splsoftcam();
2374 dpt->state &= ~DPT_HA_QUIET;
2378 * Schedule the command and submit it.
2379 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2383 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2385 ccb->transaction_id = ++dpt->commands_processed;
2386 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2387 ccb->result = (u_int32_t) & cmdarg;
2390 #ifdef DPT_MEASURE_PERFORMANCE
2391 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2392 ccb->command_started = microtime_now;
2395 dpt_Qadd_waiting(dpt, ccb);
2398 dpt_sched_queue(dpt);
2400 /* Wait for the command to complete */
2401 (void) tsleep((void *) ccb, PCATCH, "dptucw", 100 * hz);
2403 /* Free allocated memory */
2411 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2413 int ospl = splsoftcam();
2418 * If Auto Request Sense is on, copyout the sense struct
2420 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2421 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2422 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2423 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2424 sizeof(struct scsi_sense_data))) {
2425 ccb->result = EFAULT;
2426 dpt_Qpush_free(dpt, ccb);
2432 /* If DataIn is on, copyout the data */
2433 if ((ccb->eata_ccb.DataIn == 1)
2434 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2435 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2436 dpt_Qpush_free(dpt, ccb);
2437 ccb->result = EFAULT;
2444 /* Copyout the status */
2445 result = ccb->status_packet.hba_stat;
2446 cmd_arg = (caddr_t) ccb->result;
2448 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2449 dpt_Qpush_free(dpt, ccb);
2450 ccb->result = EFAULT;
2455 /* Put the CCB back in the freelist */
2456 ccb->state |= DPT_CCB_STATE_COMPLETED;
2457 dpt_Qpush_free(dpt, ccb);
2459 /* Free allocated memory */
2464 #ifdef DPT_HANDLE_TIMEOUTS
2466 * This function walks down the SUBMITTED queue.
2467 * Every request that is too old gets aborted and marked.
2468 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2469 * We just walk the list, aborting old commands and marking them as such.
2470 * The dpt_complete function will get rid of the that were interrupted in the
2473 * This function needs to run at splcam(), as it interacts with the submitted
2474 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2475 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2476 * willbe able to pre-empt it, grab a transaction in progress (towards
2477 * destruction) and operate on it. The state of this transaction will be not
2479 * The only other option, is to lock it only as long as necessary but have
2480 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2481 * a SMP environment, the advantage is dubvious for a function that runs once
2482 * every ten seconds for few microseconds and, on systems with healthy
2483 * hardware, does not do anything anyway.
2487 dpt_handle_timeouts(dpt_softc_t * dpt)
2494 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2495 printf("dpt%d WARNING: Timeout Handling Collision\n",
2500 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2502 /* Loop through the entire submitted queue, looking for lost souls */
2503 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2505 ccb = TAILQ_NEXT(ccb, links)) {
2506 struct scsi_xfer *xs;
2507 u_int32_t age, max_age;
2510 age = dpt_time_delta(ccb->command_started, microtime_now);
2512 #define TenSec 10000000
2514 if (xs == NULL) { /* Local, non-kernel call */
2517 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2518 + DPT_TIMEOUT_FACTOR))
2520 ? (xs->timeout * (dpt->submitted_ccbs_count
2521 + DPT_TIMEOUT_FACTOR))
2526 * If a transaction is marked lost and is TWICE as old as we
2527 * care, then, and only then do we destroy it!
2529 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2530 /* Remember who is next */
2531 if (age > (max_age * 2)) {
2532 dpt_Qremove_submitted(dpt, ccb);
2533 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2534 ccb->state |= DPT_CCB_STATE_ABORTED;
2535 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2536 if (ccb->retries++ > DPT_RETRIES) {
2537 printf("dpt%d ERROR: Destroying stale "
2540 "c%db%dt%du%d (%d/%d)\n",
2541 dpt->unit, ccb->transaction_id,
2544 ccb->eata_ccb.cp_channel,
2545 ccb->eata_ccb.cp_id,
2546 ccb->eata_ccb.cp_LUN, age,
2548 #define send_ccb &ccb->eata_ccb
2549 #define ESA EATA_SPECIFIC_ABORT
2550 (void) dpt_send_immediate(dpt,
2554 dpt_Qpush_free(dpt, ccb);
2556 /* The SCSI layer should re-try */
2557 xs->error |= XS_TIMEOUT;
2558 xs->flags |= SCSI_ITSDONE;
2561 printf("dpt%d ERROR: Stale %d (%s) on "
2562 "c%db%dt%du%d (%d)\n"
2565 dpt->unit, ccb->transaction_id,
2568 ccb->eata_ccb.cp_channel,
2569 ccb->eata_ccb.cp_id,
2570 ccb->eata_ccb.cp_LUN,
2571 age, ccb->retries, DPT_RETRIES);
2573 dpt_Qpush_waiting(dpt, ccb);
2574 dpt_sched_queue(dpt);
2579 * This is a transaction that is not to be destroyed
2580 * (yet) But it is too old for our liking. We wait as
2581 * long as the upper layer thinks. Not really, we
2582 * multiply that by the number of commands in the
2583 * submitted queue + 1.
2585 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2586 (age != ~0) && (age > max_age)) {
2587 printf("dpt%d ERROR: Marking %d (%s) on "
2589 " as late after %dusec\n",
2590 dpt->unit, ccb->transaction_id,
2592 dpt->unit, ccb->eata_ccb.cp_channel,
2593 ccb->eata_ccb.cp_id,
2594 ccb->eata_ccb.cp_LUN, age);
2595 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2600 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2604 #endif /* DPT_HANDLE_TIMEOUTS */