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
29 * $FreeBSD: src/sys/dev/dpt/dpt_scsi.c,v 1.28.2.3 2003/01/31 02:47:10 grog Exp $
33 * dpt_scsi.c: SCSI dependant code for the DPT driver
35 * credits: Assisted by Mike Neuffer in the early low level DPT code
36 * Thanx to Mark Salyzyn of DPT for his assistance.
37 * Special thanx to Justin Gibbs for invaluable help in
38 * making this driver look and work like a FreeBSD component.
39 * Last but not least, many thanx to UCB and the FreeBSD
40 * team for creating and maintaining such a wonderful O/S.
42 * TODO: * Add ISA probe code.
43 * * Add driver-level RAID-0. This will allow interoperability with
44 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
45 * arrays that span controllers (Wow!).
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/eventhandler.h>
54 #include <sys/malloc.h>
55 #include <sys/kernel.h>
57 #include <sys/thread2.h>
59 #include <machine/clock.h>
61 #include <bus/cam/cam.h>
62 #include <bus/cam/cam_ccb.h>
63 #include <bus/cam/cam_sim.h>
64 #include <bus/cam/cam_xpt_sim.h>
65 #include <bus/cam/cam_debug.h>
66 #include <bus/cam/scsi/scsi_all.h>
67 #include <bus/cam/scsi/scsi_message.h>
74 /* dpt_isa.c and dpt_pci.c need this in a central place */
75 int dpt_controllers_present;
77 u_long dpt_unit; /* Next unit number to use */
79 /* The linked list of softc structures */
80 struct dpt_softc_list dpt_softcs = TAILQ_HEAD_INITIALIZER(dpt_softcs);
82 #define microtime_now dpt_time_now()
84 #define dpt_inl(dpt, port) \
85 bus_space_read_4((dpt)->tag, (dpt)->bsh, port)
86 #define dpt_inb(dpt, port) \
87 bus_space_read_1((dpt)->tag, (dpt)->bsh, port)
88 #define dpt_outl(dpt, port, value) \
89 bus_space_write_4((dpt)->tag, (dpt)->bsh, port, value)
90 #define dpt_outb(dpt, port, value) \
91 bus_space_write_1((dpt)->tag, (dpt)->bsh, port, value)
94 * These will have to be setup by parameters passed at boot/load time. For
95 * perfromance reasons, we make them constants for the time being.
97 #define dpt_min_segs DPT_MAX_SEGS
98 #define dpt_max_segs DPT_MAX_SEGS
100 /* Definitions for our use of the SIM private CCB area */
101 #define ccb_dccb_ptr spriv_ptr0
102 #define ccb_dpt_ptr spriv_ptr1
104 /* ================= Private Inline Function declarations ===================*/
105 static __inline int dpt_just_reset(dpt_softc_t * dpt);
106 static __inline int dpt_raid_busy(dpt_softc_t * dpt);
107 static __inline int dpt_pio_wait (u_int32_t, u_int, u_int, u_int);
108 static __inline int dpt_wait(dpt_softc_t *dpt, u_int bits,
110 static __inline struct dpt_ccb* dptgetccb(struct dpt_softc *dpt);
111 static __inline void dptfreeccb(struct dpt_softc *dpt,
112 struct dpt_ccb *dccb);
113 static __inline bus_addr_t dptccbvtop(struct dpt_softc *dpt,
114 struct dpt_ccb *dccb);
116 static __inline int dpt_send_immediate(dpt_softc_t *dpt,
117 eata_ccb_t *cmd_block,
118 u_int32_t cmd_busaddr,
120 u_int ifc, u_int code,
123 /* ==================== Private Function declarations =======================*/
124 static void dptmapmem(void *arg, bus_dma_segment_t *segs,
125 int nseg, int error);
127 static struct sg_map_node*
128 dptallocsgmap(struct dpt_softc *dpt);
130 static int dptallocccbs(dpt_softc_t *dpt);
132 static int dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb,
133 u_int32_t dccb_busaddr, u_int size,
134 u_int page, u_int target, int extent);
135 static void dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb,
136 u_int32_t dccb_busaddr,
139 static void dpt_poll(struct cam_sim *sim);
141 static void dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
142 int nseg, int error);
144 static void dpt_action(struct cam_sim *sim, union ccb *ccb);
146 static int dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd,
147 u_int32_t cmd_busaddr,
148 u_int command, u_int retries,
149 u_int ifc, u_int code,
151 static void dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb,
152 union ccb *ccb, u_int hba_stat,
153 u_int scsi_stat, u_int32_t resid);
155 static void dpttimeout(void *arg);
156 static void dptshutdown(void *arg, int howto);
158 /* ================= Private Inline Function definitions ====================*/
160 dpt_just_reset(dpt_softc_t * dpt)
162 if ((dpt_inb(dpt, 2) == 'D')
163 && (dpt_inb(dpt, 3) == 'P')
164 && (dpt_inb(dpt, 4) == 'T')
165 && (dpt_inb(dpt, 5) == 'H'))
172 dpt_raid_busy(dpt_softc_t * dpt)
174 if ((dpt_inb(dpt, 0) == 'D')
175 && (dpt_inb(dpt, 1) == 'P')
176 && (dpt_inb(dpt, 2) == 'T'))
183 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
188 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
189 c = inb(base + reg) & bits;
199 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
204 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
205 c = dpt_inb(dpt, HA_RSTATUS) & bits;
214 static __inline struct dpt_ccb*
215 dptgetccb(struct dpt_softc *dpt)
217 struct dpt_ccb* dccb;
220 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
221 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
223 } else if (dpt->total_dccbs < dpt->max_dccbs) {
225 dccb = SLIST_FIRST(&dpt->free_dccb_list);
227 kprintf("dpt%d: Can't malloc DCCB\n", dpt->unit);
229 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
239 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
242 if ((dccb->state & DCCB_ACTIVE) != 0)
243 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
244 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
245 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
246 else if (dpt->resource_shortage != 0
247 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
248 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
249 dpt->resource_shortage = FALSE;
251 dccb->state = DCCB_FREE;
252 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
257 static __inline bus_addr_t
258 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
260 return (dpt->dpt_ccb_busbase
261 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
264 static __inline struct dpt_ccb *
265 dptccbptov(struct dpt_softc *dpt, bus_addr_t busaddr)
267 return (dpt->dpt_dccbs
268 + ((struct dpt_ccb *)busaddr
269 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
273 * Send a command for immediate execution by the DPT
274 * See above function for IMPORTANT notes.
277 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
278 u_int32_t cmd_busaddr, u_int retries,
279 u_int ifc, u_int code, u_int code2)
281 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
282 EATA_CMD_IMMEDIATE, retries, ifc,
287 /* ===================== Private Function definitions =======================*/
289 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
291 bus_addr_t *busaddrp;
293 busaddrp = (bus_addr_t *)arg;
294 *busaddrp = segs->ds_addr;
297 static struct sg_map_node *
298 dptallocsgmap(struct dpt_softc *dpt)
300 struct sg_map_node *sg_map;
302 sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_INTWAIT);
304 /* Allocate S/G space for the next batch of CCBS */
305 if (bus_dmamem_alloc(dpt->sg_dmat, (void *)&sg_map->sg_vaddr,
306 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
307 kfree(sg_map, M_DEVBUF);
311 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
312 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
315 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
321 * Allocate another chunk of CCB's. Return count of entries added.
322 * Assumed to be called at splcam().
325 dptallocccbs(dpt_softc_t *dpt)
327 struct dpt_ccb *next_ccb;
328 struct sg_map_node *sg_map;
334 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
336 if (next_ccb == dpt->dpt_dccbs) {
338 * First time through. Re-use the S/G
339 * space we allocated for initialization
342 sg_map = SLIST_FIRST(&dpt->sg_maps);
344 sg_map = dptallocsgmap(dpt);
350 segs = sg_map->sg_vaddr;
351 physaddr = sg_map->sg_physaddr;
353 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
354 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
357 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
361 next_ccb->sg_list = segs;
362 next_ccb->sg_busaddr = htonl(physaddr);
363 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
364 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
365 next_ccb->eata_ccb.cp_reqDMA =
366 htonl(dptccbvtop(dpt, next_ccb)
367 + offsetof(struct dpt_ccb, sense_data));
368 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
369 next_ccb->state = DCCB_FREE;
370 next_ccb->tag = dpt->total_dccbs;
371 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
373 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
374 dpt->dpt_ccb_busend += sizeof(*next_ccb);
382 dpt_pio_get_conf (u_int32_t base)
384 static dpt_conf_t * conf;
389 * Allocate a dpt_conf_t
392 conf = kmalloc(sizeof(dpt_conf_t), M_DEVBUF, M_INTWAIT);
395 * If we have one, clean it up.
397 bzero(conf, sizeof(dpt_conf_t));
400 * Reset the controller.
402 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
405 * Wait for the controller to become ready.
406 * For some reason there can be -no- delays after calling reset
407 * before we wait on ready status.
409 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
410 kprintf("dpt: timeout waiting for controller to become ready\n");
414 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
415 kprintf("dpt: timetout waiting for adapter ready.\n");
420 * Send the PIO_READ_CONFIG command.
422 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
425 * Read the data into the struct.
427 p = (u_int16_t *)conf;
428 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
430 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
431 kprintf("dpt: timeout in data read.\n");
435 (*p) = inw(base + HA_RDATA);
439 if (inb(base + HA_RSTATUS) & HA_SERROR) {
440 kprintf("dpt: error reading configuration data.\n");
444 #define BE_EATA_SIGNATURE 0x45415441
445 #define LE_EATA_SIGNATURE 0x41544145
448 * Test to see if we have a valid card.
450 if ((conf->signature == BE_EATA_SIGNATURE) ||
451 (conf->signature == LE_EATA_SIGNATURE)) {
453 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
454 inw(base + HA_RDATA);
463 * Read a configuration page into the supplied dpt_cont_t buffer.
466 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
467 u_int size, u_int page, u_int target, int extent)
476 cp = &dccb->eata_ccb;
477 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
481 cp->Auto_Req_Sen = 1;
482 cp->reqlen = sizeof(struct scsi_sense_data);
485 cp->cp_LUN = 0; /* In the EATA packet */
486 cp->cp_lun = 0; /* In the SCSI command */
488 cp->cp_scsi_cmd = INQUIRY;
491 cp->cp_extent = extent;
494 cp->cp_channel = 0; /* DNC, Interpret mode is set */
496 cp->cp_datalen = htonl(size);
501 * This could be a simple for loop, but we suspected the compiler To
502 * have optimized it a bit too much. Wait for the controller to
505 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
506 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
507 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
508 || (dpt_wait(dpt, HA_SBUSY, 0))) {
511 * RAID Drives still Spinning up? (This should only occur if
512 * the DPT controller is in a NON PC (PCI?) platform).
514 if (dpt_raid_busy(dpt)) {
515 kprintf("dpt%d WARNING: Get_conf() RSUS failed.\n",
522 DptStat_Reset_BUSY(dpt->sp);
525 * XXXX We might want to do something more clever than aborting at
526 * this point, like resetting (rebooting) the controller and trying
529 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
530 EATA_CMD_DMA_SEND_CP,
531 10000, 0, 0, 0)) != 0) {
532 kprintf("dpt%d WARNING: Get_conf() failed (%d) to send "
533 "EATA_CMD_DMA_READ_CONFIG\n",
538 /* Wait for two seconds for a response. This can be slow */
541 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
546 /* Grab the status and clear interrupts */
547 status = dpt_inb(dpt, HA_RSTATUS);
552 * Check the status carefully. Return only if the
553 * command was successful.
555 if (((status & HA_SERROR) == 0)
556 && (dpt->sp->hba_stat == 0)
557 && (dpt->sp->scsi_stat == 0)
558 && (dpt->sp->residue_len == 0))
561 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
567 /* Detect Cache parameters and size */
569 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
580 * Default setting, for best perfromance..
581 * This is what virtually all cards default to..
583 dpt->cache_type = DPT_CACHE_WRITEBACK;
586 cp = &dccb->eata_ccb;
587 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
590 /* Setup the command structure */
593 cp->Auto_Req_Sen = 1;
594 cp->reqlen = sizeof(struct scsi_sense_data);
596 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
597 cp->cp_LUN = 0; /* In the EATA packet */
598 cp->cp_lun = 0; /* In the SCSI command */
601 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
610 * Build the EATA Command Packet structure
611 * for a Log Sense Command.
613 cp->cp_cdb[0] = 0x4d;
615 cp->cp_cdb[2] = 0x40 | 0x33;
618 cp->cp_datalen = htonl(512);
621 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
622 EATA_CMD_DMA_SEND_CP,
625 kprintf("dpt%d WARNING: detect_cache() failed (%d) to send "
626 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
630 /* Wait for two seconds for a response. This can be slow... */
633 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
638 /* Grab the status and clear interrupts */
639 status = dpt_inb(dpt, HA_RSTATUS);
645 if (buff[0] != 0x33) {
648 bytes = DPT_HCP_LENGTH(buff);
649 param = DPT_HCP_FIRST(buff);
651 if (DPT_HCP_CODE(param) != 1) {
653 * DPT Log Page layout error
655 kprintf("dpt%d: NOTICE: Log Page (1) layout error\n",
659 if (!(param[4] & 0x4)) {
660 dpt->cache_type = DPT_NO_CACHE;
663 while (DPT_HCP_CODE(param) != 6) {
664 param = DPT_HCP_NEXT(param);
666 || (param >= &buff[bytes])) {
671 if (param[4] & 0x2) {
675 dpt->cache_type = DPT_NO_CACHE;
679 if (param[4] & 0x4) {
680 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
683 /* XXX This isn't correct. This log parameter only has two bytes.... */
685 dpt->cache_size = param[5]
693 dpt_poll(struct cam_sim *sim)
695 dpt_intr(cam_sim_softc(sim));
699 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
701 struct dpt_ccb *dccb;
703 struct dpt_softc *dpt;
705 dccb = (struct dpt_ccb *)arg;
707 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
711 kprintf("dpt%d: Unexpected error 0x%x returned from "
712 "bus_dmamap_load\n", dpt->unit, error);
713 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
714 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
715 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
717 dptfreeccb(dpt, dccb);
724 bus_dma_segment_t *end_seg;
727 end_seg = dm_segs + nseg;
729 /* Copy the segments into our SG list */
731 while (dm_segs < end_seg) {
732 sg->seg_len = htonl(dm_segs->ds_len);
733 sg->seg_addr = htonl(dm_segs->ds_addr);
739 dccb->eata_ccb.scatter = 1;
740 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
741 dccb->eata_ccb.cp_datalen =
742 htonl(nseg * sizeof(dpt_sg_t));
744 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
745 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
748 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
749 op = BUS_DMASYNC_PREREAD;
751 op = BUS_DMASYNC_PREWRITE;
753 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
756 dccb->eata_ccb.cp_dataDMA = 0;
757 dccb->eata_ccb.cp_datalen = 0;
763 * Last time we need to check if this CCB needs to
766 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
768 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
769 dptfreeccb(dpt, dccb);
775 dccb->state |= DCCB_ACTIVE;
776 ccb->ccb_h.status |= CAM_SIM_QUEUED;
777 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
778 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
780 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
781 dccb->eata_ccb.cp_busaddr,
782 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
783 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
785 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
786 dptfreeccb(dpt, dccb);
794 dpt_action(struct cam_sim *sim, union ccb *ccb)
796 struct dpt_softc *dpt;
798 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
800 dpt = (struct dpt_softc *)cam_sim_softc(sim);
802 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
803 xpt_print_path(ccb->ccb_h.path);
804 kprintf("controller is shutdown. Aborting CCB.\n");
805 ccb->ccb_h.status = CAM_NO_HBA;
810 switch (ccb->ccb_h.func_code) {
811 /* Common cases first */
812 case XPT_SCSI_IO: /* Execute the requested I/O operation */
814 struct ccb_scsiio *csio;
815 struct ccb_hdr *ccbh;
816 struct dpt_ccb *dccb;
817 struct eata_ccb *eccb;
821 /* Max CDB length is 12 bytes */
822 if (csio->cdb_len > 12) {
823 ccb->ccb_h.status = CAM_REQ_INVALID;
827 if ((dccb = dptgetccb(dpt)) == NULL) {
829 dpt->resource_shortage = 1;
831 xpt_freeze_simq(sim, /*count*/1);
832 ccb->ccb_h.status = CAM_REQUEUE_REQ;
836 eccb = &dccb->eata_ccb;
838 /* Link dccb and ccb so we can find one from the other */
840 ccb->ccb_h.ccb_dccb_ptr = dccb;
841 ccb->ccb_h.ccb_dpt_ptr = dpt;
844 * Explicitly set all flags so that the compiler can
845 * be smart about setting them.
847 eccb->SCSI_Reset = 0;
849 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
854 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
856 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
857 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
858 eccb->reqlen = csio->sense_len;
859 eccb->cp_id = ccb->ccb_h.target_id;
860 eccb->cp_channel = cam_sim_bus(sim);
861 eccb->cp_LUN = ccb->ccb_h.target_lun;
863 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
865 eccb->cp_identify = 1;
867 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
868 && csio->tag_action != CAM_TAG_ACTION_NONE) {
869 eccb->cp_msg[0] = csio->tag_action;
870 eccb->cp_msg[1] = dccb->tag;
877 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
878 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
879 bcopy(csio->cdb_io.cdb_ptr,
880 eccb->cp_cdb, csio->cdb_len);
882 /* I guess I could map it in... */
883 ccb->ccb_h.status = CAM_REQ_INVALID;
884 dptfreeccb(dpt, dccb);
889 bcopy(csio->cdb_io.cdb_bytes,
890 eccb->cp_cdb, csio->cdb_len);
893 * If we have any data to send with this command,
894 * map it into bus space.
896 /* Only use S/G if there is a transfer */
897 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
898 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
900 * We've been given a pointer
901 * to a single buffer.
903 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
908 bus_dmamap_load(dpt->buffer_dmat,
914 if (error == EINPROGRESS) {
916 * So as to maintain ordering,
917 * freeze the controller queue
918 * until our mapping is
921 xpt_freeze_simq(sim, 1);
922 dccb->state |= CAM_RELEASE_SIMQ;
926 struct bus_dma_segment seg;
928 /* Pointer to physical buffer */
930 (bus_addr_t)csio->data_ptr;
931 seg.ds_len = csio->dxfer_len;
932 dptexecuteccb(dccb, &seg, 1, 0);
935 struct bus_dma_segment *segs;
937 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
938 panic("dpt_action - Physical "
942 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
943 panic("dpt_action - Virtual "
947 /* Just use the segments provided */
948 segs = (struct bus_dma_segment *)csio->data_ptr;
949 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
954 * Does it want them both on or both off?
955 * CAM_DIR_NONE is both on, so this code can
956 * be removed if this is also what the DPT
961 dptexecuteccb(dccb, NULL, 0, 0);
965 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
966 case XPT_ABORT: /* Abort the specified CCB */
968 ccb->ccb_h.status = CAM_REQ_INVALID;
971 case XPT_SET_TRAN_SETTINGS:
973 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
977 case XPT_GET_TRAN_SETTINGS:
978 /* Get default/user set transfer settings for the target */
980 struct ccb_trans_settings *cts = &ccb->cts;
981 struct ccb_trans_settings_scsi *scsi =
982 &cts->proto_specific.scsi;
983 struct ccb_trans_settings_spi *spi =
984 &cts->xport_specific.spi;
986 cts->protocol = PROTO_SCSI;
987 cts->protocol_version = SCSI_REV_2;
988 cts->transport = XPORT_SPI;
989 cts->transport_version = 2;
991 if (cts->type == CTS_TYPE_USER_SETTINGS) {
992 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
993 spi->bus_width = (dpt->max_id > 7)
994 ? MSG_EXT_WDTR_BUS_8_BIT
995 : MSG_EXT_WDTR_BUS_16_BIT;
996 spi->sync_period = 25; /* 10MHz */
997 if (spi->sync_period != 0)
998 spi->sync_offset = 15;
999 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1001 spi->valid = CTS_SPI_VALID_SYNC_RATE
1002 | CTS_SPI_VALID_SYNC_OFFSET
1003 | CTS_SPI_VALID_BUS_WIDTH
1004 | CTS_SPI_VALID_DISC;
1005 scsi->valid = CTS_SCSI_VALID_TQ;
1006 ccb->ccb_h.status = CAM_REQ_CMP;
1008 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1013 case XPT_CALC_GEOMETRY:
1015 struct ccb_calc_geometry *ccg;
1017 u_int32_t secs_per_cylinder;
1021 * XXX Use Adaptec translation until I find out how to
1022 * get this information from the card.
1025 size_mb = ccg->volume_size
1026 / ((1024L * 1024L) / ccg->block_size);
1029 if (size_mb > 1024 && extended) {
1031 ccg->secs_per_track = 63;
1034 ccg->secs_per_track = 32;
1036 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1037 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1038 ccb->ccb_h.status = CAM_REQ_CMP;
1042 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1045 ccb->ccb_h.status = CAM_REQ_CMP;
1049 case XPT_TERM_IO: /* Terminate the I/O process */
1051 ccb->ccb_h.status = CAM_REQ_INVALID;
1054 case XPT_PATH_INQ: /* Path routing inquiry */
1056 struct ccb_pathinq *cpi = &ccb->cpi;
1058 cpi->version_num = 1;
1059 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1060 if (dpt->max_id > 7)
1061 cpi->hba_inquiry |= PI_WIDE_16;
1062 cpi->target_sprt = 0;
1064 cpi->hba_eng_cnt = 0;
1065 cpi->max_target = dpt->max_id;
1066 cpi->max_lun = dpt->max_lun;
1067 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1068 cpi->bus_id = cam_sim_bus(sim);
1069 cpi->base_transfer_speed = 3300;
1070 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1071 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1072 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1073 cpi->unit_number = cam_sim_unit(sim);
1074 cpi->transport = XPORT_SPI;
1075 cpi->transport_version = 2;
1076 cpi->protocol = PROTO_SCSI;
1077 cpi->protocol_version = SCSI_REV_2;
1078 cpi->ccb_h.status = CAM_REQ_CMP;
1083 ccb->ccb_h.status = CAM_REQ_INVALID;
1090 * This routine will try to send an EATA command to the DPT HBA.
1091 * It will, by default, try 20,000 times, waiting 50us between tries.
1092 * It returns 0 on success and 1 on failure.
1093 * It is assumed to be called at splcam().
1096 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1097 u_int32_t cmd_busaddr, u_int command, u_int retries,
1098 u_int ifc, u_int code, u_int code2)
1106 * I hate this polling nonsense. Wish there was a way to tell the DPT
1107 * to go get commands at its own pace, or to interrupt when ready.
1108 * In the mean time we will measure how many itterations it really
1111 for (loop = 0; loop < retries; loop++) {
1112 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1118 if (loop < retries) {
1119 #ifdef DPT_MEASURE_PERFORMANCE
1120 if (loop > dpt->performance.max_eata_tries)
1121 dpt->performance.max_eata_tries = loop;
1123 if (loop < dpt->performance.min_eata_tries)
1124 dpt->performance.min_eata_tries = loop;
1127 #ifdef DPT_MEASURE_PERFORMANCE
1128 ++dpt->performance.command_too_busy;
1133 /* The controller is alive, advance the wedge timer */
1134 #ifdef DPT_RESET_HBA
1135 dpt->last_contact = microtime_now;
1138 if (cmd_block == NULL)
1140 #if (BYTE_ORDER == BIG_ENDIAN)
1142 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1143 | ((cmd_busaddr >> 16) & 0xFF)
1144 | ((cmd_busaddr >> 8) & 0xFF)
1145 | (cmd_busaddr & 0xFF);
1148 /* And now the address */
1149 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1151 if (command == EATA_CMD_IMMEDIATE) {
1152 if (cmd_block == NULL) {
1153 dpt_outb(dpt, HA_WCODE2, code2);
1154 dpt_outb(dpt, HA_WCODE, code);
1156 dpt_outb(dpt, HA_WIFC, ifc);
1158 dpt_outb(dpt, HA_WCOMMAND, command);
1164 /* ==================== Exported Function definitions =======================*/
1166 dpt_alloc(device_t dev, bus_space_tag_t tag, bus_space_handle_t bsh)
1168 dpt_softc_t *dpt = device_get_softc(dev);
1171 bzero(dpt, sizeof(dpt_softc_t));
1174 dpt->unit = device_get_unit(dev);
1175 SLIST_INIT(&dpt->free_dccb_list);
1176 LIST_INIT(&dpt->pending_ccb_list);
1177 TAILQ_INSERT_TAIL(&dpt_softcs, dpt, links);
1178 for (i = 0; i < MAX_CHANNELS; i++)
1179 dpt->resetlevel[i] = DPT_HA_OK;
1181 #ifdef DPT_MEASURE_PERFORMANCE
1182 dpt_reset_performance(dpt);
1183 #endif /* DPT_MEASURE_PERFORMANCE */
1188 dpt_free(struct dpt_softc *dpt)
1190 switch (dpt->init_level) {
1193 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1195 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1197 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1199 bus_dma_tag_destroy(dpt->dccb_dmat);
1201 bus_dma_tag_destroy(dpt->buffer_dmat);
1204 struct sg_map_node *sg_map;
1206 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1207 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1208 bus_dmamap_unload(dpt->sg_dmat,
1210 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1212 kfree(sg_map, M_DEVBUF);
1214 bus_dma_tag_destroy(dpt->sg_dmat);
1219 TAILQ_REMOVE(&dpt_softcs, dpt, links);
1222 static u_int8_t string_sizes[] =
1224 sizeof(((dpt_inq_t*)NULL)->vendor),
1225 sizeof(((dpt_inq_t*)NULL)->modelNum),
1226 sizeof(((dpt_inq_t*)NULL)->firmware),
1227 sizeof(((dpt_inq_t*)NULL)->protocol),
1231 dpt_init(struct dpt_softc *dpt)
1234 struct sg_map_node *sg_map;
1241 dpt->init_level = 0;
1242 SLIST_INIT(&dpt->sg_maps);
1244 #ifdef DPT_RESET_BOARD
1245 kprintf("dpt%d: resetting HBA\n", dpt->unit);
1246 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1248 /* XXX Shouldn't we poll a status register or something??? */
1250 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1251 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1252 /*lowaddr*/BUS_SPACE_MAXADDR,
1253 /*highaddr*/BUS_SPACE_MAXADDR,
1254 /*filter*/NULL, /*filterarg*/NULL,
1255 PAGE_SIZE, /*nsegments*/1,
1256 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1257 /*flags*/0, &dpt->sg_dmat) != 0) {
1264 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1265 * memory. To get the allocation size, we need to know how many
1266 * ccbs the card supports. This requires a ccb. We solve this
1267 * chicken and egg problem by allocating some re-usable S/G space
1268 * up front, and treating it as our status packet, CCB, and target
1269 * memory space for these commands.
1271 sg_map = dptallocsgmap(dpt);
1275 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1276 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1277 bzero(dccb, sizeof(*dccb));
1278 dpt->sp_physaddr = sg_map->sg_physaddr;
1279 dccb->eata_ccb.cp_dataDMA =
1280 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1281 dccb->eata_ccb.cp_busaddr = ~0;
1282 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1283 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1284 + offsetof(struct dpt_ccb, sense_data));
1286 /* Okay. Fetch our config */
1287 bzero(&dccb[1], sizeof(conf)); /* data area */
1288 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1289 sizeof(conf), 0xc1, 7, 1);
1292 kprintf("dpt%d: Failed to get board configuration\n", dpt->unit);
1295 bcopy(&dccb[1], &conf, sizeof(conf));
1297 bzero(&dccb[1], sizeof(dpt->board_data));
1298 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1299 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1301 kprintf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1304 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1306 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1307 (u_int8_t *)&dccb[1]);
1309 switch (ntohl(conf.splen)) {
1311 dpt->EATA_revision = 'a';
1314 dpt->EATA_revision = 'b';
1317 dpt->EATA_revision = 'c';
1320 dpt->EATA_revision = 'z';
1323 dpt->EATA_revision = '?';
1326 dpt->max_id = conf.MAX_ID;
1327 dpt->max_lun = conf.MAX_LUN;
1328 dpt->irq = conf.IRQ;
1329 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1330 dpt->channels = conf.MAX_CHAN + 1;
1331 dpt->state |= DPT_HA_OK;
1333 dpt->primary = FALSE;
1335 dpt->primary = TRUE;
1337 dpt->more_support = conf.MORE_support;
1339 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1340 dpt->immediate_support = 1;
1342 dpt->immediate_support = 0;
1344 dpt->broken_INQUIRY = FALSE;
1346 dpt->cplen = ntohl(conf.cplen);
1347 dpt->cppadlen = ntohs(conf.cppadlen);
1348 dpt->max_dccbs = ntohs(conf.queuesiz);
1350 if (dpt->max_dccbs > 256) {
1351 kprintf("dpt%d: Max CCBs reduced from %d to "
1352 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1353 dpt->max_dccbs = 256;
1356 dpt->hostid[0] = conf.scsi_id0;
1357 dpt->hostid[1] = conf.scsi_id1;
1358 dpt->hostid[2] = conf.scsi_id2;
1363 dpt->sgsize = ntohs(conf.SGsiz);
1365 /* We can only get 64k buffers, so don't bother to waste space. */
1366 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1369 if (dpt->sgsize > dpt_max_segs)
1370 dpt->sgsize = dpt_max_segs;
1372 /* DMA tag for mapping buffers into device visible space. */
1373 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1374 /*lowaddr*/BUS_SPACE_MAXADDR,
1375 /*highaddr*/BUS_SPACE_MAXADDR,
1376 /*filter*/NULL, /*filterarg*/NULL,
1377 /*maxsize*/MAXBSIZE, /*nsegments*/dpt->sgsize,
1378 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1379 /*flags*/BUS_DMA_ALLOCNOW,
1380 &dpt->buffer_dmat) != 0) {
1381 kprintf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1387 /* DMA tag for our ccb structures and interrupt status packet */
1388 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1389 /*lowaddr*/BUS_SPACE_MAXADDR,
1390 /*highaddr*/BUS_SPACE_MAXADDR,
1391 /*filter*/NULL, /*filterarg*/NULL,
1392 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1395 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1396 /*flags*/0, &dpt->dccb_dmat) != 0) {
1397 kprintf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1403 /* Allocation for our ccbs and interrupt status packet */
1404 if (bus_dmamem_alloc(dpt->dccb_dmat, (void *)&dpt->dpt_dccbs,
1405 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1406 kprintf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1412 /* And permanently map them */
1413 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1415 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1417 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1419 /* Clear them out. */
1420 bzero(dpt->dpt_dccbs,
1421 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1423 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1425 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1426 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1427 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1430 /* Allocate our first batch of ccbs */
1431 if (dptallocccbs(dpt) == 0) {
1432 kprintf("dpt: dptallocccbs(dpt) == 0\n");
1436 /* Prepare for Target Mode */
1437 dpt->target_mode_enabled = 1;
1439 /* Nuke excess spaces from inquiry information */
1440 strp = dpt->board_data.vendor;
1441 for (i = 0; i < sizeof(string_sizes); i++) {
1442 index = string_sizes[i] - 1;
1443 while (index && (strp[index] == ' '))
1444 strp[index--] = '\0';
1445 strp += string_sizes[i];
1448 kprintf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1449 dpt->unit, dpt->board_data.vendor,
1450 dpt->board_data.modelNum, dpt->board_data.firmware);
1452 kprintf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1454 if (dpt->cache_type != DPT_NO_CACHE
1455 && dpt->cache_size != 0) {
1456 kprintf("%s Cache, ",
1457 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1458 ? "Write-Through" : "Write-Back");
1461 kprintf("%d CCBs\n", dpt->max_dccbs);
1469 dpt_attach(dpt_softc_t *dpt)
1471 struct cam_devq *devq;
1475 * Create the device queue for our SIM.
1477 devq = cam_simq_alloc(dpt->max_dccbs);
1481 for (i = 0; i < dpt->channels; i++) {
1483 * Construct our SIM entry
1485 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1486 dpt, dpt->unit, &sim_mplock,
1488 /*tagged*/dpt->max_dccbs, devq);
1489 if (xpt_bus_register(dpt->sims[i], i) != CAM_SUCCESS) {
1490 cam_sim_free(dpt->sims[i]);
1494 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1495 cam_sim_path(dpt->sims[i]),
1496 CAM_TARGET_WILDCARD,
1497 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1498 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1499 cam_sim_free(dpt->sims[i]);
1504 cam_simq_release(devq);
1506 EVENTHANDLER_REGISTER(shutdown_post_sync, dptshutdown,
1507 dpt, SHUTDOWN_PRI_DRIVER);
1513 * This is the interrupt handler for the DPT driver.
1525 u_int32_t residue_len; /* Number of bytes not transferred */
1527 dpt = (dpt_softc_t *)arg;
1529 /* First order of business is to check if this interrupt is for us */
1530 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1533 * What we want to do now, is to capture the status, all of it,
1534 * move it where it belongs, wake up whoever sleeps waiting to
1535 * process this result, and get out of here.
1537 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1538 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1539 kprintf("Encountered bogus status packet\n");
1540 status = dpt_inb(dpt, HA_RSTATUS);
1544 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1546 dpt->sp->ccb_busaddr = ~0;
1548 /* Ignore status packets with EOC not set */
1549 if (dpt->sp->EOC == 0) {
1550 kprintf("dpt%d ERROR: Request %d received with "
1551 "clear EOC.\n Marking as LOST.\n",
1552 dpt->unit, dccb->transaction_id);
1554 #ifdef DPT_HANDLE_TIMEOUTS
1555 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1557 /* This CLEARS the interrupt! */
1558 status = dpt_inb(dpt, HA_RSTATUS);
1564 * Double buffer the status information so the hardware can
1565 * work on updating the status packet while we decifer the
1566 * one we were just interrupted for.
1567 * According to Mark Salyzyn, we only need few pieces of it.
1569 hba_stat = dpt->sp->hba_stat;
1570 scsi_stat = dpt->sp->scsi_stat;
1571 residue_len = dpt->sp->residue_len;
1573 /* Clear interrupts, check for error */
1574 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1576 * Error Condition. Check for magic cookie. Exit
1577 * this test on earliest sign of non-reset condition
1580 /* Check that this is not a board reset interrupt */
1581 if (dpt_just_reset(dpt)) {
1582 kprintf("dpt%d: HBA rebooted.\n"
1583 " All transactions should be "
1587 kprintf("dpt%d: >>---->> This is incomplete, "
1588 "fix me.... <<----<<", dpt->unit);
1589 panic("DPT Rebooted");
1595 callout_stop(&ccb->ccb_h.timeout_ch);
1596 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1597 bus_dmasync_op_t op;
1599 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1600 op = BUS_DMASYNC_POSTREAD;
1602 op = BUS_DMASYNC_POSTWRITE;
1603 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1604 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1607 /* Common Case inline... */
1608 if (hba_stat == HA_NO_ERROR) {
1609 ccb->csio.scsi_status = scsi_stat;
1610 ccb->ccb_h.status = 0;
1611 switch (scsi_stat) {
1612 case SCSI_STATUS_OK:
1613 ccb->ccb_h.status |= CAM_REQ_CMP;
1615 case SCSI_STATUS_CHECK_COND:
1616 case SCSI_STATUS_CMD_TERMINATED:
1617 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1618 ccb->csio.sense_len);
1619 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1622 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1623 /* XXX Freeze DevQ */
1626 ccb->csio.resid = residue_len;
1627 dptfreeccb(dpt, dccb);
1630 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1637 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1638 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1640 ccb->csio.resid = resid;
1643 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1646 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1648 case HA_SCSIBUS_RESET:
1649 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1650 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1653 case HA_CP_RESET: /* XXX ??? */
1654 case HA_CP_ABORT_NA: /* XXX ??? */
1655 case HA_CP_RESET_NA: /* XXX ??? */
1656 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1657 ccb->ccb_h.status = CAM_REQ_ABORTED;
1662 case HA_PCI_STABORT:
1666 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1668 case HA_UNX_MSGRJCT:
1669 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1671 case HA_UNX_BUSPHASE:
1672 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1674 case HA_UNX_BUS_FREE:
1675 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1678 case HA_RESET_STUCK:
1680 * Dead??? Can the controller get unstuck
1681 * from these conditions
1683 ccb->ccb_h.status = CAM_NO_HBA;
1685 case HA_RSENSE_FAIL:
1686 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1689 kprintf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1690 kprintf("Please mail this message to shimon@simon-shapiro.org\n");
1691 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1694 dptfreeccb(dpt, dccb);
1699 dpttimeout(void *arg)
1701 struct dpt_ccb *dccb;
1703 struct dpt_softc *dpt;
1705 dccb = (struct dpt_ccb *)arg;
1707 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1708 xpt_print_path(ccb->ccb_h.path);
1709 kprintf("CCB %p - timed out\n", (void *)dccb);
1714 * Try to clear any pending jobs. FreeBSD will loose interrupts,
1715 * leaving the controller suspended, and commands timed-out.
1716 * By calling the interrupt handler, any command thus stuck will be
1721 if ((dccb->state & DCCB_ACTIVE) == 0) {
1722 xpt_print_path(ccb->ccb_h.path);
1723 kprintf("CCB %p - timed out CCB already completed\n",
1729 /* Abort this particular command. Leave all others running */
1730 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1731 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1732 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1737 * Shutdown the controller and ensure that the cache is completely flushed.
1738 * Called from the shutdown_final event after all disk access has completed.
1741 dptshutdown(void *arg, int howto)
1745 dpt = (dpt_softc_t *)arg;
1747 kprintf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1751 * What we do for a shutdown, is give the DPT early power loss warning
1753 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1754 DELAY(1000 * 1000 * 5);
1755 kprintf("dpt%d: Controller was warned of shutdown and is now "
1756 "disabled\n", dpt->unit);
1759 /*============================================================================*/
1762 #ifdef DPT_RESET_HBA
1765 ** Function name : dpt_reset_hba
1767 ** Description : Reset the HBA and properly discard all pending work
1772 dpt_reset_hba(dpt_softc_t *dpt)
1775 dpt_ccb_t dccb, *dccbp;
1777 struct scsi_xfer *xs;
1779 /* Prepare a control block. The SCSI command part is immaterial */
1782 dccb.state = DPT_CCB_STATE_NEW;
1783 dccb.std_callback = NULL;
1784 dccb.wrbuff_callback = NULL;
1786 ccb = &dccb.eata_ccb;
1787 ccb->CP_OpCode = EATA_CMD_RESET;
1788 ccb->SCSI_Reset = 0;
1790 ccb->Auto_Req_Sen = 1;
1791 ccb->cp_id = 0; /* Should be ignored */
1795 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1796 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1797 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1798 ccb->cp_viraddr = (u_int32_t) & ccb;
1800 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1801 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1803 /* Lock up the submitted queue. We are very persistant here */
1805 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1809 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1812 /* Send the RESET message */
1813 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1814 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1815 kprintf("dpt%d: Failed to send the RESET message.\n"
1816 " Trying cold boot (ouch!)\n", dpt->unit);
1819 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1820 EATA_COLD_BOOT, 0, 0,
1822 panic("dpt%d: Faild to cold boot the HBA",
1825 #ifdef DPT_MEASURE_PERFORMANCE
1826 dpt->performance.cold_boots++;
1827 #endif /* DPT_MEASURE_PERFORMANCE */
1830 #ifdef DPT_MEASURE_PERFORMANCE
1831 dpt->performance.warm_starts++;
1832 #endif /* DPT_MEASURE_PERFORMANCE */
1834 kprintf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1837 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1838 struct scsi_xfer *xs = dccbp->xs;
1840 /* Not all transactions have xs structs */
1842 /* Tell the kernel proper this did not complete well */
1843 xs->error |= XS_SELTIMEOUT;
1844 xs->flags |= SCSI_ITSDONE;
1848 dpt_Qremove_submitted(dpt, dccbp);
1850 /* Remember, Callbacks are NOT in the standard queue */
1851 if (dccbp->std_callback != NULL) {
1852 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1856 dpt_Qpush_free(dpt, dccbp);
1861 kprintf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1862 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1865 #endif /* DPT_RESET_HBA */
1868 * Build a Command Block for target mode READ/WRITE BUFFER,
1869 * with the ``sync'' bit ON.
1871 * Although the length and offset are 24 bit fields in the command, they cannot
1872 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1873 * If they are sensless, we round them to zero offset, maximum length and
1878 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1879 dpt_ccb_t * ccb, int mode, u_int8_t command,
1880 u_int16_t length, u_int16_t offset)
1884 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
1885 kprintf("dpt%d: Length of %d, and offset of %d are wrong\n",
1886 dpt->unit, length, offset);
1887 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
1892 ccb->state = DPT_CCB_STATE_NEW;
1893 ccb->std_callback = (ccb_callback) dpt_target_done;
1894 ccb->wrbuff_callback = NULL;
1896 cp = &ccb->eata_ccb;
1897 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
1900 cp->Auto_Req_Sen = 1;
1905 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
1906 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
1907 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
1908 cp->cp_viraddr = (u_int32_t) & ccb;
1910 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1912 cp->cp_scsi_cmd = command;
1913 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
1914 cp->cp_lun = lun; /* Order is important here! */
1915 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
1916 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
1917 cp->cp_cdb[4] = (length >> 8) & 0xFF;
1918 cp->cp_cdb[5] = length & 0xFF;
1919 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
1920 cp->cp_cdb[7] = (length >> 8) & 0xFF;
1921 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
1922 cp->cp_cdb[9] = 0; /* No sync, no match bits */
1925 * This could be optimized to live in dpt_register_buffer.
1926 * We keep it here, just in case the kernel decides to reallocate pages
1928 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
1929 dpt->rw_buffer[bus][target][lun])) {
1930 kprintf("dpt%d: Failed to setup Scatter/Gather for "
1931 "Target-Mode buffer\n", dpt->unit);
1935 /* Setup a target mode READ command */
1938 dpt_set_target(int redo, dpt_softc_t * dpt,
1939 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
1940 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
1942 if (dpt->target_mode_enabled) {
1946 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
1947 SCSI_TM_READ_BUFFER, length, offset);
1949 ccb->transaction_id = ++dpt->commands_processed;
1951 #ifdef DPT_MEASURE_PERFORMANCE
1952 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
1953 ccb->command_started = microtime_now;
1955 dpt_Qadd_waiting(dpt, ccb);
1956 dpt_sched_queue(dpt);
1960 kprintf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
1966 * Schedule a buffer to be sent to another target.
1967 * The work will be scheduled and the callback provided will be called when
1968 * the work is actually done.
1970 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
1971 * get notified of receipt of buffers.
1975 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
1976 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
1977 buff_wr_done callback)
1980 dpt_ccb_t *ccb = NULL;
1982 /* This is an external call. Be a bit paranoid */
1983 for (dpt = TAILQ_FIRST(&dpt_softc_list);
1985 dpt = TAILQ_NEXT(dpt, links)) {
1986 if (dpt->unit == unit)
1990 return (INVALID_UNIT);
1994 if (dpt->target_mode_enabled) {
1995 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
1996 (lun > dpt->max_lun)) {
1997 return (INVALID_SENDER);
1999 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2000 (dpt->buffer_receiver[channel][target][lun] == NULL))
2001 return (NOT_REGISTERED);
2004 /* Process the free list */
2005 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2006 kprintf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2007 " Please try later\n",
2010 return (NO_RESOURCES);
2012 /* Now grab the newest CCB */
2013 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2015 panic("dpt%d: Got a NULL CCB from pop_free()", dpt->unit);
2019 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2020 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2021 SCSI_TM_WRITE_BUFFER,
2023 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2026 ccb->transaction_id = ++dpt->commands_processed;
2028 #ifdef DPT_MEASURE_PERFORMANCE
2029 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2030 ccb->command_started = microtime_now;
2032 dpt_Qadd_waiting(dpt, ccb);
2033 dpt_sched_queue(dpt);
2038 return (DRIVER_DOWN);
2042 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2046 cp = &ccb->eata_ccb;
2049 * Remove the CCB from the waiting queue.
2050 * We do NOT put it back on the free, etc., queues as it is a special
2051 * ccb, owned by the dpt_softc of this unit.
2054 dpt_Qremove_completed(dpt, ccb);
2057 #define br_channel (ccb->eata_ccb.cp_channel)
2058 #define br_target (ccb->eata_ccb.cp_id)
2059 #define br_lun (ccb->eata_ccb.cp_LUN)
2060 #define br_index [br_channel][br_target][br_lun]
2061 #define read_buffer_callback (dpt->buffer_receiver br_index )
2062 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2063 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2064 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2065 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2067 /* Different reasons for being here, you know... */
2068 switch (ccb->eata_ccb.cp_scsi_cmd) {
2069 case SCSI_TM_READ_BUFFER:
2070 if (read_buffer_callback != NULL) {
2071 /* This is a buffer generated by a kernel process */
2072 read_buffer_callback(dpt->unit, br_channel,
2075 br_offset, br_length);
2078 * This is a buffer waited for by a user (sleeping)
2084 /* We ALWAYS re-issue the same command; args are don't-care */
2085 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2088 case SCSI_TM_WRITE_BUFFER:
2089 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2090 br_offset, br_length,
2091 br_lun, ccb->status_packet.hba_stat);
2094 kprintf("dpt%d: %s is an unsupported command for target mode\n",
2095 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2098 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2099 dpt_Qpush_free(dpt, ccb);
2105 * Use this function to register a client for a buffer read target operation.
2106 * The function you register will be called every time a buffer is received
2107 * by the target mode code.
2110 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2111 u_int8_t mode, u_int16_t length, u_int16_t offset,
2112 dpt_rec_buff callback, dpt_rb_op_t op)
2115 dpt_ccb_t *ccb = NULL;
2117 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2119 dpt = TAILQ_NEXT(dpt, links)) {
2120 if (dpt->unit == unit)
2124 return (INVALID_UNIT);
2128 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2129 return (DRIVER_DOWN);
2131 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2132 (lun > (dpt->max_lun - 1)))
2133 return (INVALID_SENDER);
2135 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2136 if (op == REGISTER_BUFFER) {
2137 /* Assign the requested callback */
2138 dpt->buffer_receiver[channel][target][lun] = callback;
2142 /* Process the free list */
2143 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2144 kprintf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2145 " Please try later\n",
2148 return (NO_RESOURCES);
2150 /* Now grab the newest CCB */
2151 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2153 panic("dpt%d: Got a NULL CCB from pop_free()",
2158 /* Clean up the leftover of the previous tenant */
2159 ccb->status = DPT_CCB_STATE_NEW;
2160 dpt->target_ccb[channel][target][lun] = ccb;
2162 dpt->rw_buffer[channel][target][lun] =
2163 kmalloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_INTWAIT);
2164 dpt_set_target(0, dpt, channel, target, lun, mode,
2165 length, offset, ccb);
2166 return (SUCCESSFULLY_REGISTERED);
2168 return (NOT_REGISTERED);
2170 if (op == REGISTER_BUFFER) {
2171 if (dpt->buffer_receiver[channel][target][lun] == callback)
2172 return (ALREADY_REGISTERED);
2174 return (REGISTERED_TO_ANOTHER);
2176 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2177 dpt->buffer_receiver[channel][target][lun] = NULL;
2179 dpt_Qpush_free(dpt, ccb);
2181 kfree(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2182 return (SUCCESSFULLY_REGISTERED);
2184 return (INVALID_CALLBACK);
2190 /* Return the state of the blinking DPT LED's */
2192 dpt_blinking_led(dpt_softc_t * dpt)
2203 for (ndx = 0, state = 0, previous = 0;
2204 (ndx < 10) && (state != previous);
2207 state = dpt_inl(dpt, 1);
2210 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2211 result = dpt_inb(dpt, 5);
2218 * Execute a command which did not come from the kernel's SCSI layer.
2219 * The only way to map user commands to bus and target is to comply with the
2220 * standard DPT wire-down scheme:
2223 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2224 caddr_t cmdarg, int minor_no)
2228 int channel, target, lun;
2232 size_t contigsize = 0;
2235 channel = minor2hba(minor_no);
2236 target = minor2target(minor_no);
2237 lun = minor2lun(minor_no);
2239 if ((channel > (dpt->channels - 1))
2240 || (target > dpt->max_id)
2241 || (lun > dpt->max_lun))
2244 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2245 /* This one is for the controller itself */
2246 if ((user_cmd->eataID[0] != 'E')
2247 || (user_cmd->eataID[1] != 'A')
2248 || (user_cmd->eataID[2] != 'T')
2249 || (user_cmd->eataID[3] != 'A')) {
2253 /* Get a DPT CCB, so we can prepare a command */
2256 /* Process the free list */
2257 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2258 kprintf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2259 " Please try later\n",
2264 /* Now grab the newest CCB */
2265 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2267 panic("dpt%d: Got a NULL CCB from pop_free()", dpt->unit);
2270 /* Clean up the leftover of the previous tenant */
2271 ccb->status = DPT_CCB_STATE_NEW;
2274 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2275 sizeof(eata_ccb_t));
2277 /* We do not want to do user specified scatter/gather. Why?? */
2278 if (ccb->eata_ccb.scatter == 1)
2281 ccb->eata_ccb.Auto_Req_Sen = 1;
2282 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2283 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2284 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2285 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2286 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2287 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2289 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2290 /* Data I/O is involved in this command. Alocate buffer */
2291 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2292 contigsize = ccb->eata_ccb.cp_datalen;
2293 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2294 M_TEMP, M_WAITOK, 0, ~0,
2295 ccb->eata_ccb.cp_datalen,
2298 data = kmalloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2303 kprintf("dpt%d: Cannot allocate %d bytes "
2304 "for EATA command\n", dpt->unit,
2305 ccb->eata_ccb.cp_datalen);
2308 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2309 if (ccb->eata_ccb.DataIn == 1) {
2310 if (copyin(usr_cmd_DMA,
2311 data, ccb->eata_ccb.cp_datalen) == -1)
2315 /* No data I/O involved here. Make sure the DPT knows that */
2316 ccb->eata_ccb.cp_datalen = 0;
2320 if (ccb->eata_ccb.FWNEST == 1)
2321 ccb->eata_ccb.FWNEST = 0;
2323 if (ccb->eata_ccb.cp_datalen != 0) {
2324 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2328 contigfree(data, contigsize, M_TEMP);
2330 kfree(data, M_TEMP);
2336 * We are required to quiet a SCSI bus.
2337 * since we do not queue comands on a bus basis,
2338 * we wait for ALL commands on a controller to complete.
2339 * In the mean time, sched_queue() will not schedule new commands.
2341 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2342 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2343 /* We wait for ALL traffic for this HBa to subside */
2345 dpt->state |= DPT_HA_QUIET;
2348 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2349 huh = tsleep((void *) dpt, PCATCH, "dptqt", 100 * hz);
2352 /* Wakeup call received */
2363 /* Resume normal operation */
2364 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2365 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2367 dpt->state &= ~DPT_HA_QUIET;
2371 * Schedule the command and submit it.
2372 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2376 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2378 ccb->transaction_id = ++dpt->commands_processed;
2379 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2380 ccb->result = (u_int32_t) & cmdarg;
2383 #ifdef DPT_MEASURE_PERFORMANCE
2384 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2385 ccb->command_started = microtime_now;
2388 dpt_Qadd_waiting(dpt, ccb);
2391 dpt_sched_queue(dpt);
2393 /* Wait for the command to complete */
2394 (void) tsleep((void *) ccb, PCATCH, "dptucw", 100 * hz);
2396 /* Free allocated memory */
2399 contigfree(data, contigsize, M_TEMP);
2401 kfree(data, M_TEMP);
2408 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2416 * If Auto Request Sense is on, copyout the sense struct
2418 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2419 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2420 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2421 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2422 sizeof(struct scsi_sense_data))) {
2423 ccb->result = EFAULT;
2424 dpt_Qpush_free(dpt, ccb);
2430 /* If DataIn is on, copyout the data */
2431 if ((ccb->eata_ccb.DataIn == 1)
2432 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2433 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2434 dpt_Qpush_free(dpt, ccb);
2435 ccb->result = EFAULT;
2442 /* Copyout the status */
2443 result = ccb->status_packet.hba_stat;
2444 cmd_arg = (caddr_t) ccb->result;
2446 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2447 dpt_Qpush_free(dpt, ccb);
2448 ccb->result = EFAULT;
2453 /* Put the CCB back in the freelist */
2454 ccb->state |= DPT_CCB_STATE_COMPLETED;
2455 dpt_Qpush_free(dpt, ccb);
2457 /* Free allocated memory */
2462 #ifdef DPT_HANDLE_TIMEOUTS
2464 * This function walks down the SUBMITTED queue.
2465 * Every request that is too old gets aborted and marked.
2466 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2467 * We just walk the list, aborting old commands and marking them as such.
2468 * The dpt_complete function will get rid of the that were interrupted in the
2471 * This function needs to run at splcam(), as it interacts with the submitted
2472 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2473 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2474 * willbe able to pre-empt it, grab a transaction in progress (towards
2475 * destruction) and operate on it. The state of this transaction will be not
2477 * The only other option, is to lock it only as long as necessary but have
2478 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2479 * a SMP environment, the advantage is dubvious for a function that runs once
2480 * every ten seconds for few microseconds and, on systems with healthy
2481 * hardware, does not do anything anyway.
2485 dpt_handle_timeouts(dpt_softc_t * dpt)
2491 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2492 kprintf("dpt%d WARNING: Timeout Handling Collision\n",
2497 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2499 /* Loop through the entire submitted queue, looking for lost souls */
2500 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2502 ccb = TAILQ_NEXT(ccb, links)) {
2503 struct scsi_xfer *xs;
2504 u_int32_t age, max_age;
2507 age = dpt_time_delta(ccb->command_started, microtime_now);
2509 #define TenSec 10000000
2511 if (xs == NULL) { /* Local, non-kernel call */
2514 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2515 + DPT_TIMEOUT_FACTOR))
2517 ? (xs->timeout * (dpt->submitted_ccbs_count
2518 + DPT_TIMEOUT_FACTOR))
2523 * If a transaction is marked lost and is TWICE as old as we
2524 * care, then, and only then do we destroy it!
2526 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2527 /* Remember who is next */
2528 if (age > (max_age * 2)) {
2529 dpt_Qremove_submitted(dpt, ccb);
2530 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2531 ccb->state |= DPT_CCB_STATE_ABORTED;
2532 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2533 if (ccb->retries++ > DPT_RETRIES) {
2534 kprintf("dpt%d ERROR: Destroying stale "
2537 "c%db%dt%du%d (%d/%d)\n",
2538 dpt->unit, ccb->transaction_id,
2541 ccb->eata_ccb.cp_channel,
2542 ccb->eata_ccb.cp_id,
2543 ccb->eata_ccb.cp_LUN, age,
2545 #define send_ccb &ccb->eata_ccb
2546 #define ESA EATA_SPECIFIC_ABORT
2547 (void) dpt_send_immediate(dpt,
2551 dpt_Qpush_free(dpt, ccb);
2553 /* The SCSI layer should re-try */
2554 xs->error |= XS_TIMEOUT;
2555 xs->flags |= SCSI_ITSDONE;
2558 kprintf("dpt%d ERROR: Stale %d (%s) on "
2559 "c%db%dt%du%d (%d)\n"
2562 dpt->unit, ccb->transaction_id,
2565 ccb->eata_ccb.cp_channel,
2566 ccb->eata_ccb.cp_id,
2567 ccb->eata_ccb.cp_LUN,
2568 age, ccb->retries, DPT_RETRIES);
2570 dpt_Qpush_waiting(dpt, ccb);
2571 dpt_sched_queue(dpt);
2576 * This is a transaction that is not to be destroyed
2577 * (yet) But it is too old for our liking. We wait as
2578 * long as the upper layer thinks. Not really, we
2579 * multiply that by the number of commands in the
2580 * submitted queue + 1.
2582 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2583 (age != ~0) && (age > max_age)) {
2584 kprintf("dpt%d ERROR: Marking %d (%s) on "
2586 " as late after %dusec\n",
2587 dpt->unit, ccb->transaction_id,
2589 dpt->unit, ccb->eata_ccb.cp_channel,
2590 ccb->eata_ccb.cp_id,
2591 ccb->eata_ccb.cp_LUN, age);
2592 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2597 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2601 #endif /* DPT_HANDLE_TIMEOUTS */