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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * dpt_scsi.c: SCSI dependant code for the DPT driver
37 * credits: Assisted by Mike Neuffer in the early low level DPT code
38 * Thanx to Mark Salyzyn of DPT for his assistance.
39 * Special thanx to Justin Gibbs for invaluable help in
40 * making this driver look and work like a FreeBSD component.
41 * Last but not least, many thanx to UCB and the FreeBSD
42 * team for creating and maintaining such a wonderful O/S.
44 * TODO: * Add ISA probe code.
45 * * Add driver-level RAID-0. This will allow interoperability with
46 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
47 * arrays that span controllers (Wow!).
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/eventhandler.h>
58 #include <sys/malloc.h>
59 #include <sys/kernel.h>
63 #include <machine/bus.h>
65 #include <machine/resource.h>
70 #include <cam/cam_ccb.h>
71 #include <cam/cam_sim.h>
72 #include <cam/cam_xpt_sim.h>
73 #include <cam/cam_debug.h>
74 #include <cam/scsi/scsi_all.h>
75 #include <cam/scsi/scsi_message.h>
80 #include <dev/dpt/dpt.h>
82 /* dpt_isa.c, dpt_eisa.c, and dpt_pci.c need this in a central place */
83 int dpt_controllers_present;
84 devclass_t dpt_devclass;
86 #define microtime_now dpt_time_now()
88 #define dpt_inl(dpt, port) \
89 bus_space_read_4((dpt)->tag, (dpt)->bsh, port)
90 #define dpt_inb(dpt, port) \
91 bus_space_read_1((dpt)->tag, (dpt)->bsh, port)
92 #define dpt_outl(dpt, port, value) \
93 bus_space_write_4((dpt)->tag, (dpt)->bsh, port, value)
94 #define dpt_outb(dpt, port, value) \
95 bus_space_write_1((dpt)->tag, (dpt)->bsh, port, value)
98 * These will have to be setup by parameters passed at boot/load time. For
99 * perfromance reasons, we make them constants for the time being.
101 #define dpt_min_segs DPT_MAX_SEGS
102 #define dpt_max_segs DPT_MAX_SEGS
104 /* Definitions for our use of the SIM private CCB area */
105 #define ccb_dccb_ptr spriv_ptr0
106 #define ccb_dpt_ptr spriv_ptr1
108 /* ================= Private Inline Function declarations ===================*/
109 static __inline int dpt_just_reset(dpt_softc_t * dpt);
110 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);
114 static __inline int dpt_wait(dpt_softc_t *dpt, u_int bits,
116 static __inline struct dpt_ccb* dptgetccb(struct dpt_softc *dpt);
117 static __inline void dptfreeccb(struct dpt_softc *dpt,
118 struct dpt_ccb *dccb);
119 static __inline bus_addr_t dptccbvtop(struct dpt_softc *dpt,
120 struct dpt_ccb *dccb);
122 static __inline int dpt_send_immediate(dpt_softc_t *dpt,
123 eata_ccb_t *cmd_block,
124 u_int32_t cmd_busaddr,
126 u_int ifc, u_int code,
129 /* ==================== Private Function declarations =======================*/
130 static void dptmapmem(void *arg, bus_dma_segment_t *segs,
131 int nseg, int error);
133 static struct sg_map_node*
134 dptallocsgmap(struct dpt_softc *dpt);
136 static int dptallocccbs(dpt_softc_t *dpt);
138 static int dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb,
139 u_int32_t dccb_busaddr, u_int size,
140 u_int page, u_int target, int extent);
141 static void dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb,
142 u_int32_t dccb_busaddr,
145 static void dpt_poll(struct cam_sim *sim);
147 static void dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
148 int nseg, int error);
150 static void dpt_action(struct cam_sim *sim, union ccb *ccb);
152 static int dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd,
153 u_int32_t cmd_busaddr,
154 u_int command, u_int retries,
155 u_int ifc, u_int code,
157 static void dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb,
158 union ccb *ccb, u_int hba_stat,
159 u_int scsi_stat, u_int32_t resid);
161 static void dpttimeout(void *arg);
162 static void dptshutdown(void *arg, int howto);
164 /* ================= Private Inline Function definitions ====================*/
166 dpt_just_reset(dpt_softc_t * dpt)
168 if ((dpt_inb(dpt, 2) == 'D')
169 && (dpt_inb(dpt, 3) == 'P')
170 && (dpt_inb(dpt, 4) == 'T')
171 && (dpt_inb(dpt, 5) == 'H'))
178 dpt_raid_busy(dpt_softc_t * dpt)
180 if ((dpt_inb(dpt, 0) == 'D')
181 && (dpt_inb(dpt, 1) == 'P')
182 && (dpt_inb(dpt, 2) == 'T'))
190 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
195 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
196 c = inb(base + reg) & bits;
207 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
212 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
213 c = dpt_inb(dpt, HA_RSTATUS) & bits;
222 static __inline struct dpt_ccb*
223 dptgetccb(struct dpt_softc *dpt)
225 struct dpt_ccb* dccb;
229 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
230 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
232 } else if (dpt->total_dccbs < dpt->max_dccbs) {
234 dccb = SLIST_FIRST(&dpt->free_dccb_list);
236 printf("dpt%d: Can't malloc DCCB\n", dpt->unit);
238 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
248 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
253 if ((dccb->state & DCCB_ACTIVE) != 0)
254 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
255 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
256 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
257 else if (dpt->resource_shortage != 0
258 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
259 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
260 dpt->resource_shortage = FALSE;
262 dccb->state = DCCB_FREE;
263 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
268 static __inline bus_addr_t
269 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
271 return (dpt->dpt_ccb_busbase
272 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
275 static __inline struct dpt_ccb *
276 dptccbptov(struct dpt_softc *dpt, bus_addr_t busaddr)
278 return (dpt->dpt_dccbs
279 + ((struct dpt_ccb *)busaddr
280 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
284 * Send a command for immediate execution by the DPT
285 * See above function for IMPORTANT notes.
288 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
289 u_int32_t cmd_busaddr, u_int retries,
290 u_int ifc, u_int code, u_int code2)
292 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
293 EATA_CMD_IMMEDIATE, retries, ifc,
298 /* ===================== Private Function definitions =======================*/
300 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
302 bus_addr_t *busaddrp;
304 busaddrp = (bus_addr_t *)arg;
305 *busaddrp = segs->ds_addr;
308 static struct sg_map_node *
309 dptallocsgmap(struct dpt_softc *dpt)
311 struct sg_map_node *sg_map;
313 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
318 /* Allocate S/G space for the next batch of CCBS */
319 if (bus_dmamem_alloc(dpt->sg_dmat, (void **)&sg_map->sg_vaddr,
320 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
321 free(sg_map, M_DEVBUF);
325 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
326 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
329 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
335 * Allocate another chunk of CCB's. Return count of entries added.
336 * Assumed to be called at splcam().
339 dptallocccbs(dpt_softc_t *dpt)
341 struct dpt_ccb *next_ccb;
342 struct sg_map_node *sg_map;
348 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
350 if (next_ccb == dpt->dpt_dccbs) {
352 * First time through. Re-use the S/G
353 * space we allocated for initialization
356 sg_map = SLIST_FIRST(&dpt->sg_maps);
358 sg_map = dptallocsgmap(dpt);
364 segs = sg_map->sg_vaddr;
365 physaddr = sg_map->sg_physaddr;
367 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
368 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
371 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
375 next_ccb->sg_list = segs;
376 next_ccb->sg_busaddr = htonl(physaddr);
377 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
378 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
379 next_ccb->eata_ccb.cp_reqDMA =
380 htonl(dptccbvtop(dpt, next_ccb)
381 + offsetof(struct dpt_ccb, sense_data));
382 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
383 next_ccb->state = DCCB_FREE;
384 next_ccb->tag = dpt->total_dccbs;
385 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
387 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
388 dpt->dpt_ccb_busend += sizeof(*next_ccb);
397 dpt_pio_get_conf (u_int32_t base)
399 static dpt_conf_t * conf;
404 * Allocate a dpt_conf_t
407 conf = (dpt_conf_t *)malloc(sizeof(dpt_conf_t),
412 * If we didn't get one then we probably won't ever get one.
415 printf("dpt: unable to allocate dpt_conf_t\n");
420 * If we have one, clean it up.
422 bzero(conf, sizeof(dpt_conf_t));
425 * Reset the controller.
427 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
430 * Wait for the controller to become ready.
431 * For some reason there can be -no- delays after calling reset
432 * before we wait on ready status.
434 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
435 printf("dpt: timeout waiting for controller to become ready\n");
439 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
440 printf("dpt: timetout waiting for adapter ready.\n");
445 * Send the PIO_READ_CONFIG command.
447 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
450 * Read the data into the struct.
452 p = (u_int16_t *)conf;
453 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
455 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
457 printf("dpt: timeout in data read.\n");
461 (*p) = inw(base + HA_RDATA);
465 if (inb(base + HA_RSTATUS) & HA_SERROR) {
467 printf("dpt: error reading configuration data.\n");
471 #define BE_EATA_SIGNATURE 0x45415441
472 #define LE_EATA_SIGNATURE 0x41544145
475 * Test to see if we have a valid card.
477 if ((conf->signature == BE_EATA_SIGNATURE) ||
478 (conf->signature == LE_EATA_SIGNATURE)) {
480 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
481 inw(base + HA_RDATA);
491 * Read a configuration page into the supplied dpt_cont_t buffer.
494 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
495 u_int size, u_int page, u_int target, int extent)
505 cp = &dccb->eata_ccb;
506 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
510 cp->Auto_Req_Sen = 1;
511 cp->reqlen = sizeof(struct scsi_sense_data);
514 cp->cp_LUN = 0; /* In the EATA packet */
515 cp->cp_lun = 0; /* In the SCSI command */
517 cp->cp_scsi_cmd = INQUIRY;
520 cp->cp_extent = extent;
523 cp->cp_channel = 0; /* DNC, Interpret mode is set */
525 cp->cp_datalen = htonl(size);
530 * This could be a simple for loop, but we suspected the compiler To
531 * have optimized it a bit too much. Wait for the controller to
534 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
535 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
536 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
537 || (dpt_wait(dpt, HA_SBUSY, 0))) {
540 * RAID Drives still Spinning up? (This should only occur if
541 * the DPT controller is in a NON PC (PCI?) platform).
543 if (dpt_raid_busy(dpt)) {
544 printf("dpt%d WARNING: Get_conf() RSUS failed.\n",
551 DptStat_Reset_BUSY(dpt->sp);
554 * XXXX We might want to do something more clever than aborting at
555 * this point, like resetting (rebooting) the controller and trying
558 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
559 EATA_CMD_DMA_SEND_CP,
560 10000, 0, 0, 0)) != 0) {
561 printf("dpt%d WARNING: Get_conf() failed (%d) to send "
562 "EATA_CMD_DMA_READ_CONFIG\n",
567 /* Wait for two seconds for a response. This can be slow */
570 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
575 /* Grab the status and clear interrupts */
576 status = dpt_inb(dpt, HA_RSTATUS);
581 * Check the status carefully. Return only if the
582 * command was successful.
584 if (((status & HA_SERROR) == 0)
585 && (dpt->sp->hba_stat == 0)
586 && (dpt->sp->scsi_stat == 0)
587 && (dpt->sp->residue_len == 0))
590 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
596 /* Detect Cache parameters and size */
598 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
610 * Default setting, for best perfromance..
611 * This is what virtually all cards default to..
613 dpt->cache_type = DPT_CACHE_WRITEBACK;
616 cp = &dccb->eata_ccb;
617 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
620 /* Setup the command structure */
623 cp->Auto_Req_Sen = 1;
624 cp->reqlen = sizeof(struct scsi_sense_data);
626 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
627 cp->cp_LUN = 0; /* In the EATA packet */
628 cp->cp_lun = 0; /* In the SCSI command */
631 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
640 * Build the EATA Command Packet structure
641 * for a Log Sense Command.
643 cp->cp_cdb[0] = 0x4d;
645 cp->cp_cdb[2] = 0x40 | 0x33;
648 cp->cp_datalen = htonl(512);
651 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
652 EATA_CMD_DMA_SEND_CP,
655 printf("dpt%d WARNING: detect_cache() failed (%d) to send "
656 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
660 /* Wait for two seconds for a response. This can be slow... */
663 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
668 /* Grab the status and clear interrupts */
669 status = dpt_inb(dpt, HA_RSTATUS);
675 if (buff[0] != 0x33) {
678 bytes = DPT_HCP_LENGTH(buff);
679 param = DPT_HCP_FIRST(buff);
681 if (DPT_HCP_CODE(param) != 1) {
683 * DPT Log Page layout error
685 printf("dpt%d: NOTICE: Log Page (1) layout error\n",
689 if (!(param[4] & 0x4)) {
690 dpt->cache_type = DPT_NO_CACHE;
693 while (DPT_HCP_CODE(param) != 6) {
694 param = DPT_HCP_NEXT(param);
696 || (param >= &buff[bytes])) {
701 if (param[4] & 0x2) {
705 dpt->cache_type = DPT_NO_CACHE;
709 if (param[4] & 0x4) {
710 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
713 /* XXX This isn't correct. This log parameter only has two bytes.... */
715 dpt->cache_size = param[5]
723 dpt_poll(struct cam_sim *sim)
725 dpt_intr(cam_sim_softc(sim));
729 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
731 struct dpt_ccb *dccb;
733 struct dpt_softc *dpt;
736 dccb = (struct dpt_ccb *)arg;
738 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
742 printf("dpt%d: Unexepected error 0x%x returned from "
743 "bus_dmamap_load\n", dpt->unit, error);
744 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
745 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
746 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
748 dptfreeccb(dpt, dccb);
755 bus_dma_segment_t *end_seg;
758 end_seg = dm_segs + nseg;
760 /* Copy the segments into our SG list */
762 while (dm_segs < end_seg) {
763 sg->seg_len = htonl(dm_segs->ds_len);
764 sg->seg_addr = htonl(dm_segs->ds_addr);
770 dccb->eata_ccb.scatter = 1;
771 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
772 dccb->eata_ccb.cp_datalen =
773 htonl(nseg * sizeof(dpt_sg_t));
775 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
776 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
779 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
780 op = BUS_DMASYNC_PREREAD;
782 op = BUS_DMASYNC_PREWRITE;
784 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
787 dccb->eata_ccb.cp_dataDMA = 0;
788 dccb->eata_ccb.cp_datalen = 0;
794 * Last time we need to check if this CCB needs to
797 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
799 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
800 dptfreeccb(dpt, dccb);
806 dccb->state |= DCCB_ACTIVE;
807 ccb->ccb_h.status |= CAM_SIM_QUEUED;
808 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
809 ccb->ccb_h.timeout_ch =
810 timeout(dpttimeout, (caddr_t)dccb,
811 (ccb->ccb_h.timeout * hz) / 1000);
812 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
813 dccb->eata_ccb.cp_busaddr,
814 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
815 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
817 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
818 dptfreeccb(dpt, dccb);
826 dpt_action(struct cam_sim *sim, union ccb *ccb)
828 struct dpt_softc *dpt;
830 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
832 dpt = (struct dpt_softc *)cam_sim_softc(sim);
834 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
835 xpt_print_path(ccb->ccb_h.path);
836 printf("controller is shutdown. Aborting CCB.\n");
837 ccb->ccb_h.status = CAM_NO_HBA;
842 switch (ccb->ccb_h.func_code) {
843 /* Common cases first */
844 case XPT_SCSI_IO: /* Execute the requested I/O operation */
846 struct ccb_scsiio *csio;
847 struct ccb_hdr *ccbh;
848 struct dpt_ccb *dccb;
849 struct eata_ccb *eccb;
853 /* Max CDB length is 12 bytes */
854 if (csio->cdb_len > 12) {
855 ccb->ccb_h.status = CAM_REQ_INVALID;
859 if ((dccb = dptgetccb(dpt)) == NULL) {
863 dpt->resource_shortage = 1;
865 xpt_freeze_simq(sim, /*count*/1);
866 ccb->ccb_h.status = CAM_REQUEUE_REQ;
870 eccb = &dccb->eata_ccb;
872 /* Link dccb and ccb so we can find one from the other */
874 ccb->ccb_h.ccb_dccb_ptr = dccb;
875 ccb->ccb_h.ccb_dpt_ptr = dpt;
878 * Explicitly set all flags so that the compiler can
879 * be smart about setting them.
881 eccb->SCSI_Reset = 0;
883 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
888 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
890 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
891 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
892 eccb->reqlen = csio->sense_len;
893 eccb->cp_id = ccb->ccb_h.target_id;
894 eccb->cp_channel = cam_sim_bus(sim);
895 eccb->cp_LUN = ccb->ccb_h.target_lun;
897 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
899 eccb->cp_identify = 1;
901 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
902 && csio->tag_action != CAM_TAG_ACTION_NONE) {
903 eccb->cp_msg[0] = csio->tag_action;
904 eccb->cp_msg[1] = dccb->tag;
911 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
912 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
913 bcopy(csio->cdb_io.cdb_ptr,
914 eccb->cp_cdb, csio->cdb_len);
916 /* I guess I could map it in... */
917 ccb->ccb_h.status = CAM_REQ_INVALID;
918 dptfreeccb(dpt, dccb);
923 bcopy(csio->cdb_io.cdb_bytes,
924 eccb->cp_cdb, csio->cdb_len);
927 * If we have any data to send with this command,
928 * map it into bus space.
930 /* Only use S/G if there is a transfer */
931 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
932 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
934 * We've been given a pointer
935 * to a single buffer.
937 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
943 bus_dmamap_load(dpt->buffer_dmat,
949 if (error == EINPROGRESS) {
951 * So as to maintain ordering,
952 * freeze the controller queue
953 * until our mapping is
956 xpt_freeze_simq(sim, 1);
957 dccb->state |= CAM_RELEASE_SIMQ;
961 struct bus_dma_segment seg;
963 /* Pointer to physical buffer */
965 (bus_addr_t)csio->data_ptr;
966 seg.ds_len = csio->dxfer_len;
967 dptexecuteccb(dccb, &seg, 1, 0);
970 struct bus_dma_segment *segs;
972 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
973 panic("dpt_action - Physical "
977 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
978 panic("dpt_action - Virtual "
982 /* Just use the segments provided */
983 segs = (struct bus_dma_segment *)csio->data_ptr;
984 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
989 * Does it want them both on or both off?
990 * CAM_DIR_NONE is both on, so this code can
991 * be removed if this is also what the DPT
996 dptexecuteccb(dccb, NULL, 0, 0);
1000 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1001 case XPT_ABORT: /* Abort the specified CCB */
1003 ccb->ccb_h.status = CAM_REQ_INVALID;
1006 case XPT_SET_TRAN_SETTINGS:
1008 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1012 case XPT_GET_TRAN_SETTINGS:
1013 /* Get default/user set transfer settings for the target */
1015 struct ccb_trans_settings *cts = &ccb->cts;
1016 struct ccb_trans_settings_scsi *scsi =
1017 &cts->proto_specific.scsi;
1018 struct ccb_trans_settings_spi *spi =
1019 &cts->xport_specific.spi;
1021 cts->protocol = PROTO_SCSI;
1022 cts->protocol_version = SCSI_REV_2;
1023 cts->transport = XPORT_SPI;
1024 cts->transport_version = 2;
1026 if (cts->type == CTS_TYPE_USER_SETTINGS) {
1027 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
1028 spi->bus_width = (dpt->max_id > 7)
1029 ? MSG_EXT_WDTR_BUS_8_BIT
1030 : MSG_EXT_WDTR_BUS_16_BIT;
1031 spi->sync_period = 25; /* 10MHz */
1032 if (spi->sync_period != 0)
1033 spi->sync_offset = 15;
1034 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1036 spi->valid = CTS_SPI_VALID_SYNC_RATE
1037 | CTS_SPI_VALID_SYNC_OFFSET
1038 | CTS_SPI_VALID_SYNC_RATE
1039 | CTS_SPI_VALID_BUS_WIDTH
1040 | CTS_SPI_VALID_DISC;
1041 scsi->valid = CTS_SCSI_VALID_TQ;
1042 ccb->ccb_h.status = CAM_REQ_CMP;
1044 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1049 case XPT_CALC_GEOMETRY:
1052 * XXX Use Adaptec translation until I find out how to
1053 * get this information from the card.
1055 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1059 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1062 ccb->ccb_h.status = CAM_REQ_CMP;
1066 case XPT_TERM_IO: /* Terminate the I/O process */
1068 ccb->ccb_h.status = CAM_REQ_INVALID;
1071 case XPT_PATH_INQ: /* Path routing inquiry */
1073 struct ccb_pathinq *cpi = &ccb->cpi;
1075 cpi->version_num = 1;
1076 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
1077 if (dpt->max_id > 7)
1078 cpi->hba_inquiry |= PI_WIDE_16;
1079 cpi->target_sprt = 0;
1081 cpi->hba_eng_cnt = 0;
1082 cpi->max_target = dpt->max_id;
1083 cpi->max_lun = dpt->max_lun;
1084 cpi->initiator_id = dpt->hostid[cam_sim_bus(sim)];
1085 cpi->bus_id = cam_sim_bus(sim);
1086 cpi->base_transfer_speed = 3300;
1087 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1088 strncpy(cpi->hba_vid, "DPT", HBA_IDLEN);
1089 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1090 cpi->unit_number = cam_sim_unit(sim);
1091 cpi->transport = XPORT_SPI;
1092 cpi->transport_version = 2;
1093 cpi->protocol = PROTO_SCSI;
1094 cpi->protocol_version = SCSI_REV_2;
1095 cpi->ccb_h.status = CAM_REQ_CMP;
1100 ccb->ccb_h.status = CAM_REQ_INVALID;
1107 * This routine will try to send an EATA command to the DPT HBA.
1108 * It will, by default, try 20,000 times, waiting 50us between tries.
1109 * It returns 0 on success and 1 on failure.
1110 * It is assumed to be called at splcam().
1113 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1114 u_int32_t cmd_busaddr, u_int command, u_int retries,
1115 u_int ifc, u_int code, u_int code2)
1123 * I hate this polling nonsense. Wish there was a way to tell the DPT
1124 * to go get commands at its own pace, or to interrupt when ready.
1125 * In the mean time we will measure how many itterations it really
1128 for (loop = 0; loop < retries; loop++) {
1129 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1135 if (loop < retries) {
1136 #ifdef DPT_MEASURE_PERFORMANCE
1137 if (loop > dpt->performance.max_eata_tries)
1138 dpt->performance.max_eata_tries = loop;
1140 if (loop < dpt->performance.min_eata_tries)
1141 dpt->performance.min_eata_tries = loop;
1144 #ifdef DPT_MEASURE_PERFORMANCE
1145 ++dpt->performance.command_too_busy;
1150 /* The controller is alive, advance the wedge timer */
1151 #ifdef DPT_RESET_HBA
1152 dpt->last_contact = microtime_now;
1155 if (cmd_block == NULL)
1157 #if (BYTE_ORDER == BIG_ENDIAN)
1159 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1160 | ((cmd_busaddr >> 16) & 0xFF)
1161 | ((cmd_busaddr >> 8) & 0xFF)
1162 | (cmd_busaddr & 0xFF);
1165 /* And now the address */
1166 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1168 if (command == EATA_CMD_IMMEDIATE) {
1169 if (cmd_block == NULL) {
1170 dpt_outb(dpt, HA_WCODE2, code2);
1171 dpt_outb(dpt, HA_WCODE, code);
1173 dpt_outb(dpt, HA_WIFC, ifc);
1175 dpt_outb(dpt, HA_WCOMMAND, command);
1181 /* ==================== Exported Function definitions =======================*/
1183 dpt_alloc(device_t dev)
1185 dpt_softc_t *dpt = device_get_softc(dev);
1188 dpt->tag = rman_get_bustag(dpt->io_res);
1189 dpt->bsh = rman_get_bushandle(dpt->io_res) + dpt->io_offset;
1190 dpt->unit = device_get_unit(dev);
1191 SLIST_INIT(&dpt->free_dccb_list);
1192 LIST_INIT(&dpt->pending_ccb_list);
1193 for (i = 0; i < MAX_CHANNELS; i++)
1194 dpt->resetlevel[i] = DPT_HA_OK;
1196 #ifdef DPT_MEASURE_PERFORMANCE
1197 dpt_reset_performance(dpt);
1198 #endif /* DPT_MEASURE_PERFORMANCE */
1203 dpt_free(struct dpt_softc *dpt)
1205 switch (dpt->init_level) {
1208 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1210 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1212 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1214 bus_dma_tag_destroy(dpt->dccb_dmat);
1216 bus_dma_tag_destroy(dpt->buffer_dmat);
1219 struct sg_map_node *sg_map;
1221 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1222 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1223 bus_dmamap_unload(dpt->sg_dmat,
1225 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1227 free(sg_map, M_DEVBUF);
1229 bus_dma_tag_destroy(dpt->sg_dmat);
1237 dpt_alloc_resources (device_t dev)
1242 dpt = device_get_softc(dev);
1244 dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type, &dpt->io_rid,
1246 if (dpt->io_res == NULL) {
1247 device_printf(dev, "No I/O space?!\n");
1252 dpt->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &dpt->irq_rid,
1254 if (dpt->irq_res == NULL) {
1255 device_printf(dev, "No IRQ!\n");
1267 dpt_release_resources (device_t dev)
1269 struct dpt_softc * dpt;
1271 dpt = device_get_softc(dev);
1274 bus_teardown_intr(dev, dpt->irq_res, dpt->ih);
1276 bus_release_resource(dev, dpt->io_type, dpt->io_rid, dpt->io_res);
1278 bus_release_resource(dev, SYS_RES_IRQ, dpt->irq_rid, dpt->irq_res);
1280 bus_release_resource(dev, SYS_RES_DRQ, dpt->drq_rid, dpt->drq_res);
1285 static u_int8_t string_sizes[] =
1287 sizeof(((dpt_inq_t*)NULL)->vendor),
1288 sizeof(((dpt_inq_t*)NULL)->modelNum),
1289 sizeof(((dpt_inq_t*)NULL)->firmware),
1290 sizeof(((dpt_inq_t*)NULL)->protocol),
1294 dpt_init(struct dpt_softc *dpt)
1297 struct sg_map_node *sg_map;
1304 dpt->init_level = 0;
1305 SLIST_INIT(&dpt->sg_maps);
1307 #ifdef DPT_RESET_BOARD
1308 printf("dpt%d: resetting HBA\n", dpt->unit);
1309 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1311 /* XXX Shouldn't we poll a status register or something??? */
1313 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1314 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1317 /* lowaddr */ BUS_SPACE_MAXADDR,
1318 /* highaddr */ BUS_SPACE_MAXADDR,
1320 /* filterarg */ NULL,
1321 /* maxsize */ PAGE_SIZE,
1323 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1325 /* lockfunc */ busdma_lock_mutex,
1326 /* lockarg */ &Giant,
1327 &dpt->sg_dmat) != 0) {
1334 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1335 * memory. To get the allocation size, we need to know how many
1336 * ccbs the card supports. This requires a ccb. We solve this
1337 * chicken and egg problem by allocating some re-usable S/G space
1338 * up front, and treating it as our status packet, CCB, and target
1339 * memory space for these commands.
1341 sg_map = dptallocsgmap(dpt);
1345 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1346 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1347 bzero(dccb, sizeof(*dccb));
1348 dpt->sp_physaddr = sg_map->sg_physaddr;
1349 dccb->eata_ccb.cp_dataDMA =
1350 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1351 dccb->eata_ccb.cp_busaddr = ~0;
1352 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1353 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1354 + offsetof(struct dpt_ccb, sense_data));
1356 /* Okay. Fetch our config */
1357 bzero(&dccb[1], sizeof(conf)); /* data area */
1358 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1359 sizeof(conf), 0xc1, 7, 1);
1362 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1365 bcopy(&dccb[1], &conf, sizeof(conf));
1367 bzero(&dccb[1], sizeof(dpt->board_data));
1368 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1369 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1371 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1374 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1376 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1377 (u_int8_t *)&dccb[1]);
1379 switch (ntohl(conf.splen)) {
1381 dpt->EATA_revision = 'a';
1384 dpt->EATA_revision = 'b';
1387 dpt->EATA_revision = 'c';
1390 dpt->EATA_revision = 'z';
1393 dpt->EATA_revision = '?';
1396 dpt->max_id = conf.MAX_ID;
1397 dpt->max_lun = conf.MAX_LUN;
1398 dpt->irq = conf.IRQ;
1399 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1400 dpt->channels = conf.MAX_CHAN + 1;
1401 dpt->state |= DPT_HA_OK;
1403 dpt->primary = FALSE;
1405 dpt->primary = TRUE;
1407 dpt->more_support = conf.MORE_support;
1409 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1410 dpt->immediate_support = 1;
1412 dpt->immediate_support = 0;
1414 dpt->broken_INQUIRY = FALSE;
1416 dpt->cplen = ntohl(conf.cplen);
1417 dpt->cppadlen = ntohs(conf.cppadlen);
1418 dpt->max_dccbs = ntohs(conf.queuesiz);
1420 if (dpt->max_dccbs > 256) {
1421 printf("dpt%d: Max CCBs reduced from %d to "
1422 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1423 dpt->max_dccbs = 256;
1426 dpt->hostid[0] = conf.scsi_id0;
1427 dpt->hostid[1] = conf.scsi_id1;
1428 dpt->hostid[2] = conf.scsi_id2;
1433 dpt->sgsize = ntohs(conf.SGsiz);
1435 /* We can only get 64k buffers, so don't bother to waste space. */
1436 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1439 if (dpt->sgsize > dpt_max_segs)
1440 dpt->sgsize = dpt_max_segs;
1442 /* DMA tag for mapping buffers into device visible space. */
1443 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1446 /* lowaddr */ BUS_SPACE_MAXADDR,
1447 /* highaddr */ BUS_SPACE_MAXADDR,
1449 /* filterarg */ NULL,
1450 /* maxsize */ MAXBSIZE,
1451 /* nsegments */ dpt->sgsize,
1452 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1453 /* flags */ BUS_DMA_ALLOCNOW,
1454 /* lockfunc */ busdma_lock_mutex,
1455 /* lockarg */ &Giant,
1456 &dpt->buffer_dmat) != 0) {
1457 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1463 /* DMA tag for our ccb structures and interrupt status packet */
1464 if (bus_dma_tag_create( /* parent */ dpt->parent_dmat,
1467 /* lowaddr */ BUS_SPACE_MAXADDR,
1468 /* highaddr */ BUS_SPACE_MAXADDR,
1470 /* filterarg */ NULL,
1471 /* maxsize */ (dpt->max_dccbs *
1472 sizeof(struct dpt_ccb)) +
1475 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1477 /* lockfunc */ busdma_lock_mutex,
1478 /* lockarg */ &Giant,
1479 &dpt->dccb_dmat) != 0) {
1480 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1486 /* Allocation for our ccbs and interrupt status packet */
1487 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1488 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1489 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1495 /* And permanently map them */
1496 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1498 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1500 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1502 /* Clear them out. */
1503 bzero(dpt->dpt_dccbs,
1504 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1506 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1508 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1509 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1510 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1513 /* Allocate our first batch of ccbs */
1514 if (dptallocccbs(dpt) == 0) {
1515 printf("dpt: dptallocccbs(dpt) == 0\n");
1519 /* Prepare for Target Mode */
1520 dpt->target_mode_enabled = 1;
1522 /* Nuke excess spaces from inquiry information */
1523 strp = dpt->board_data.vendor;
1524 for (i = 0; i < sizeof(string_sizes); i++) {
1525 index = string_sizes[i] - 1;
1526 while (index && (strp[index] == ' '))
1527 strp[index--] = '\0';
1528 strp += string_sizes[i];
1531 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1532 dpt->unit, dpt->board_data.vendor,
1533 dpt->board_data.modelNum, dpt->board_data.firmware);
1535 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1537 if (dpt->cache_type != DPT_NO_CACHE
1538 && dpt->cache_size != 0) {
1539 printf("%s Cache, ",
1540 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1541 ? "Write-Through" : "Write-Back");
1544 printf("%d CCBs\n", dpt->max_dccbs);
1552 dpt_attach(dpt_softc_t *dpt)
1554 struct cam_devq *devq;
1558 * Create the device queue for our SIM.
1560 devq = cam_simq_alloc(dpt->max_dccbs);
1564 for (i = 0; i < dpt->channels; i++) {
1566 * Construct our SIM entry
1568 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1569 dpt, dpt->unit, &Giant,
1571 /*tagged*/dpt->max_dccbs, devq);
1572 if (dpt->sims[i] == NULL) {
1574 cam_simq_free(devq);
1576 printf( "%s(): Unable to attach bus %d "
1577 "due to resource shortage\n",
1582 if (xpt_bus_register(dpt->sims[i], dpt->dev, i) != CAM_SUCCESS){
1583 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1584 dpt->sims[i] = NULL;
1588 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1589 cam_sim_path(dpt->sims[i]),
1590 CAM_TARGET_WILDCARD,
1591 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1592 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1593 cam_sim_free(dpt->sims[i], /*free_devq*/i == 0);
1594 dpt->sims[i] = NULL;
1600 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1601 dpt, SHUTDOWN_PRI_DEFAULT);
1606 dpt_detach (device_t dev)
1608 struct dpt_softc * dpt;
1611 dpt = device_get_softc(dev);
1613 for (i = 0; i < dpt->channels; i++) {
1615 xpt_async(AC_LOST_DEVICE, dpt->paths[i], NULL);
1617 xpt_free_path(dpt->paths[i]);
1618 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1619 cam_sim_free(dpt->sims[i], /*free_devq*/TRUE);
1622 dptshutdown((void *)dpt, SHUTDOWN_PRI_DEFAULT);
1624 dpt_release_resources(dev);
1632 * This is the interrupt handler for the DPT driver.
1644 u_int32_t residue_len; /* Number of bytes not transferred */
1646 dpt = (dpt_softc_t *)arg;
1648 /* First order of business is to check if this interrupt is for us */
1649 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1652 * What we want to do now, is to capture the status, all of it,
1653 * move it where it belongs, wake up whoever sleeps waiting to
1654 * process this result, and get out of here.
1656 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1657 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1658 printf("Encountered bogus status packet\n");
1659 status = dpt_inb(dpt, HA_RSTATUS);
1663 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1665 dpt->sp->ccb_busaddr = ~0;
1667 /* Ignore status packets with EOC not set */
1668 if (dpt->sp->EOC == 0) {
1669 printf("dpt%d ERROR: Request %d received with "
1670 "clear EOC.\n Marking as LOST.\n",
1671 dpt->unit, dccb->transaction_id);
1673 #ifdef DPT_HANDLE_TIMEOUTS
1674 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1676 /* This CLEARS the interrupt! */
1677 status = dpt_inb(dpt, HA_RSTATUS);
1683 * Double buffer the status information so the hardware can
1684 * work on updating the status packet while we decifer the
1685 * one we were just interrupted for.
1686 * According to Mark Salyzyn, we only need few pieces of it.
1688 hba_stat = dpt->sp->hba_stat;
1689 scsi_stat = dpt->sp->scsi_stat;
1690 residue_len = dpt->sp->residue_len;
1692 /* Clear interrupts, check for error */
1693 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1695 * Error Condition. Check for magic cookie. Exit
1696 * this test on earliest sign of non-reset condition
1699 /* Check that this is not a board reset interrupt */
1700 if (dpt_just_reset(dpt)) {
1701 printf("dpt%d: HBA rebooted.\n"
1702 " All transactions should be "
1706 printf("dpt%d: >>---->> This is incomplete, "
1707 "fix me.... <<----<<", dpt->unit);
1708 panic("DPT Rebooted");
1714 untimeout(dpttimeout, dccb, ccb->ccb_h.timeout_ch);
1715 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1716 bus_dmasync_op_t op;
1718 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1719 op = BUS_DMASYNC_POSTREAD;
1721 op = BUS_DMASYNC_POSTWRITE;
1722 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1723 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1726 /* Common Case inline... */
1727 if (hba_stat == HA_NO_ERROR) {
1728 ccb->csio.scsi_status = scsi_stat;
1729 ccb->ccb_h.status = 0;
1730 switch (scsi_stat) {
1731 case SCSI_STATUS_OK:
1732 ccb->ccb_h.status |= CAM_REQ_CMP;
1734 case SCSI_STATUS_CHECK_COND:
1735 case SCSI_STATUS_CMD_TERMINATED:
1736 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1737 ccb->csio.sense_len);
1738 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1741 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1742 /* XXX Freeze DevQ */
1745 ccb->csio.resid = residue_len;
1746 dptfreeccb(dpt, dccb);
1749 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1756 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1757 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1759 ccb->csio.resid = resid;
1762 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1765 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1767 case HA_SCSIBUS_RESET:
1768 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1769 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1772 case HA_CP_RESET: /* XXX ??? */
1773 case HA_CP_ABORT_NA: /* XXX ??? */
1774 case HA_CP_RESET_NA: /* XXX ??? */
1775 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1776 ccb->ccb_h.status = CAM_REQ_ABORTED;
1781 case HA_PCI_STABORT:
1785 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1787 case HA_UNX_MSGRJCT:
1788 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1790 case HA_UNX_BUSPHASE:
1791 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1793 case HA_UNX_BUS_FREE:
1794 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1797 case HA_RESET_STUCK:
1799 * Dead??? Can the controller get unstuck
1800 * from these conditions
1802 ccb->ccb_h.status = CAM_NO_HBA;
1804 case HA_RSENSE_FAIL:
1805 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1808 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1809 printf("Please mail this message to shimon@simon-shapiro.org\n");
1810 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1813 dptfreeccb(dpt, dccb);
1818 dpttimeout(void *arg)
1820 struct dpt_ccb *dccb;
1822 struct dpt_softc *dpt;
1825 dccb = (struct dpt_ccb *)arg;
1827 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1828 xpt_print_path(ccb->ccb_h.path);
1829 printf("CCB %p - timed out\n", (void *)dccb);
1834 * Try to clear any pending jobs. FreeBSD will lose interrupts,
1835 * leaving the controller suspended, and commands timed-out.
1836 * By calling the interrupt handler, any command thus stuck will be
1841 if ((dccb->state & DCCB_ACTIVE) == 0) {
1842 xpt_print_path(ccb->ccb_h.path);
1843 printf("CCB %p - timed out CCB already completed\n",
1849 /* Abort this particular command. Leave all others running */
1850 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1851 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1852 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1857 * Shutdown the controller and ensure that the cache is completely flushed.
1858 * Called from the shutdown_final event after all disk access has completed.
1861 dptshutdown(void *arg, int howto)
1865 dpt = (dpt_softc_t *)arg;
1867 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1871 * What we do for a shutdown, is give the DPT early power loss warning
1873 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1874 DELAY(1000 * 1000 * 5);
1875 printf("dpt%d: Controller was warned of shutdown and is now "
1876 "disabled\n", dpt->unit);
1879 /*============================================================================*/
1882 #ifdef DPT_RESET_HBA
1885 ** Function name : dpt_reset_hba
1887 ** Description : Reset the HBA and properly discard all pending work
1892 dpt_reset_hba(dpt_softc_t *dpt)
1896 dpt_ccb_t dccb, *dccbp;
1898 struct scsi_xfer *xs;
1900 /* Prepare a control block. The SCSI command part is immaterial */
1903 dccb.state = DPT_CCB_STATE_NEW;
1904 dccb.std_callback = NULL;
1905 dccb.wrbuff_callback = NULL;
1907 ccb = &dccb.eata_ccb;
1908 ccb->CP_OpCode = EATA_CMD_RESET;
1909 ccb->SCSI_Reset = 0;
1911 ccb->Auto_Req_Sen = 1;
1912 ccb->cp_id = 0; /* Should be ignored */
1916 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1917 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1918 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1919 ccb->cp_viraddr = (u_int32_t) & ccb;
1921 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1922 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1924 /* Lock up the submitted queue. We are very persistant here */
1926 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1930 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1933 /* Send the RESET message */
1934 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1935 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1936 printf("dpt%d: Failed to send the RESET message.\n"
1937 " Trying cold boot (ouch!)\n", dpt->unit);
1940 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1941 EATA_COLD_BOOT, 0, 0,
1943 panic("dpt%d: Faild to cold boot the HBA\n",
1946 #ifdef DPT_MEASURE_PERFORMANCE
1947 dpt->performance.cold_boots++;
1948 #endif /* DPT_MEASURE_PERFORMANCE */
1951 #ifdef DPT_MEASURE_PERFORMANCE
1952 dpt->performance.warm_starts++;
1953 #endif /* DPT_MEASURE_PERFORMANCE */
1955 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1958 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1959 struct scsi_xfer *xs = dccbp->xs;
1961 /* Not all transactions have xs structs */
1963 /* Tell the kernel proper this did not complete well */
1964 xs->error |= XS_SELTIMEOUT;
1965 xs->flags |= SCSI_ITSDONE;
1969 dpt_Qremove_submitted(dpt, dccbp);
1971 /* Remember, Callbacks are NOT in the standard queue */
1972 if (dccbp->std_callback != NULL) {
1973 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1977 dpt_Qpush_free(dpt, dccbp);
1982 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1983 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1986 #endif /* DPT_RESET_HBA */
1989 * Build a Command Block for target mode READ/WRITE BUFFER,
1990 * with the ``sync'' bit ON.
1992 * Although the length and offset are 24 bit fields in the command, they cannot
1993 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1994 * If they are sensless, we round them to zero offset, maximum length and
1999 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
2000 dpt_ccb_t * ccb, int mode, u_int8_t command,
2001 u_int16_t length, u_int16_t offset)
2006 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
2007 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
2008 dpt->unit, length, offset);
2009 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
2014 ccb->state = DPT_CCB_STATE_NEW;
2015 ccb->std_callback = (ccb_callback) dpt_target_done;
2016 ccb->wrbuff_callback = NULL;
2018 cp = &ccb->eata_ccb;
2019 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
2022 cp->Auto_Req_Sen = 1;
2027 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
2028 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
2029 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
2030 cp->cp_viraddr = (u_int32_t) & ccb;
2032 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
2034 cp->cp_scsi_cmd = command;
2035 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
2036 cp->cp_lun = lun; /* Order is important here! */
2037 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
2038 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
2039 cp->cp_cdb[4] = (length >> 8) & 0xFF;
2040 cp->cp_cdb[5] = length & 0xFF;
2041 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
2042 cp->cp_cdb[7] = (length >> 8) & 0xFF;
2043 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
2044 cp->cp_cdb[9] = 0; /* No sync, no match bits */
2047 * This could be optimized to live in dpt_register_buffer.
2048 * We keep it here, just in case the kernel decides to reallocate pages
2050 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
2051 dpt->rw_buffer[bus][target][lun])) {
2052 printf("dpt%d: Failed to setup Scatter/Gather for "
2053 "Target-Mode buffer\n", dpt->unit);
2057 /* Setup a target mode READ command */
2060 dpt_set_target(int redo, dpt_softc_t * dpt,
2061 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
2062 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
2066 if (dpt->target_mode_enabled) {
2070 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
2071 SCSI_TM_READ_BUFFER, length, offset);
2073 ccb->transaction_id = ++dpt->commands_processed;
2075 #ifdef DPT_MEASURE_PERFORMANCE
2076 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2077 ccb->command_started = microtime_now;
2079 dpt_Qadd_waiting(dpt, ccb);
2080 dpt_sched_queue(dpt);
2084 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
2090 * Schedule a buffer to be sent to another target.
2091 * The work will be scheduled and the callback provided will be called when
2092 * the work is actually done.
2094 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
2095 * get notified of receipt of buffers.
2099 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2100 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
2101 buff_wr_done callback)
2104 dpt_ccb_t *ccb = NULL;
2107 /* This is an external call. Be a bit paranoid */
2108 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2110 dpt = TAILQ_NEXT(dpt, links)) {
2111 if (dpt->unit == unit)
2115 return (INVALID_UNIT);
2119 if (dpt->target_mode_enabled) {
2120 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
2121 (lun > dpt->max_lun)) {
2122 return (INVALID_SENDER);
2124 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
2125 (dpt->buffer_receiver[channel][target][lun] == NULL))
2126 return (NOT_REGISTERED);
2128 ospl = splsoftcam();
2129 /* Process the free list */
2130 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2131 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2132 " Please try later\n",
2135 return (NO_RESOURCES);
2137 /* Now grab the newest CCB */
2138 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2140 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2144 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2145 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2146 SCSI_TM_WRITE_BUFFER,
2148 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2151 ccb->transaction_id = ++dpt->commands_processed;
2153 #ifdef DPT_MEASURE_PERFORMANCE
2154 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2155 ccb->command_started = microtime_now;
2157 dpt_Qadd_waiting(dpt, ccb);
2158 dpt_sched_queue(dpt);
2163 return (DRIVER_DOWN);
2167 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2172 cp = &ccb->eata_ccb;
2175 * Remove the CCB from the waiting queue.
2176 * We do NOT put it back on the free, etc., queues as it is a special
2177 * ccb, owned by the dpt_softc of this unit.
2179 ospl = splsoftcam();
2180 dpt_Qremove_completed(dpt, ccb);
2183 #define br_channel (ccb->eata_ccb.cp_channel)
2184 #define br_target (ccb->eata_ccb.cp_id)
2185 #define br_lun (ccb->eata_ccb.cp_LUN)
2186 #define br_index [br_channel][br_target][br_lun]
2187 #define read_buffer_callback (dpt->buffer_receiver br_index )
2188 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2189 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2190 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2191 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2193 /* Different reasons for being here, you know... */
2194 switch (ccb->eata_ccb.cp_scsi_cmd) {
2195 case SCSI_TM_READ_BUFFER:
2196 if (read_buffer_callback != NULL) {
2197 /* This is a buffer generated by a kernel process */
2198 read_buffer_callback(dpt->unit, br_channel,
2201 br_offset, br_length);
2204 * This is a buffer waited for by a user (sleeping)
2210 /* We ALWAYS re-issue the same command; args are don't-care */
2211 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2214 case SCSI_TM_WRITE_BUFFER:
2215 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2216 br_offset, br_length,
2217 br_lun, ccb->status_packet.hba_stat);
2220 printf("dpt%d: %s is an unsupported command for target mode\n",
2221 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2223 ospl = splsoftcam();
2224 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2225 dpt_Qpush_free(dpt, ccb);
2231 * Use this function to register a client for a buffer read target operation.
2232 * The function you register will be called every time a buffer is received
2233 * by the target mode code.
2236 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2237 u_int8_t mode, u_int16_t length, u_int16_t offset,
2238 dpt_rec_buff callback, dpt_rb_op_t op)
2241 dpt_ccb_t *ccb = NULL;
2244 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2246 dpt = TAILQ_NEXT(dpt, links)) {
2247 if (dpt->unit == unit)
2251 return (INVALID_UNIT);
2255 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2256 return (DRIVER_DOWN);
2258 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2259 (lun > (dpt->max_lun - 1)))
2260 return (INVALID_SENDER);
2262 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2263 if (op == REGISTER_BUFFER) {
2264 /* Assign the requested callback */
2265 dpt->buffer_receiver[channel][target][lun] = callback;
2267 ospl = splsoftcam();
2269 /* Process the free list */
2270 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2271 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2272 " Please try later\n",
2275 return (NO_RESOURCES);
2277 /* Now grab the newest CCB */
2278 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2280 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2285 /* Clean up the leftover of the previous tenant */
2286 ccb->status = DPT_CCB_STATE_NEW;
2287 dpt->target_ccb[channel][target][lun] = ccb;
2289 dpt->rw_buffer[channel][target][lun] =
2290 malloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
2291 if (dpt->rw_buffer[channel][target][lun] == NULL) {
2292 printf("dpt%d: Failed to allocate "
2293 "Target-Mode buffer\n", dpt->unit);
2294 ospl = splsoftcam();
2295 dpt_Qpush_free(dpt, ccb);
2297 return (NO_RESOURCES);
2299 dpt_set_target(0, dpt, channel, target, lun, mode,
2300 length, offset, ccb);
2301 return (SUCCESSFULLY_REGISTERED);
2303 return (NOT_REGISTERED);
2305 if (op == REGISTER_BUFFER) {
2306 if (dpt->buffer_receiver[channel][target][lun] == callback)
2307 return (ALREADY_REGISTERED);
2309 return (REGISTERED_TO_ANOTHER);
2311 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2312 dpt->buffer_receiver[channel][target][lun] = NULL;
2313 ospl = splsoftcam();
2314 dpt_Qpush_free(dpt, ccb);
2316 free(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2317 return (SUCCESSFULLY_REGISTERED);
2319 return (INVALID_CALLBACK);
2325 /* Return the state of the blinking DPT LED's */
2327 dpt_blinking_led(dpt_softc_t * dpt)
2339 for (ndx = 0, state = 0, previous = 0;
2340 (ndx < 10) && (state != previous);
2343 state = dpt_inl(dpt, 1);
2346 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2347 result = dpt_inb(dpt, 5);
2354 * Execute a command which did not come from the kernel's SCSI layer.
2355 * The only way to map user commands to bus and target is to comply with the
2356 * standard DPT wire-down scheme:
2359 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2360 caddr_t cmdarg, int minor_no)
2364 int channel, target, lun;
2371 channel = minor2hba(minor_no);
2372 target = minor2target(minor_no);
2373 lun = minor2lun(minor_no);
2375 if ((channel > (dpt->channels - 1))
2376 || (target > dpt->max_id)
2377 || (lun > dpt->max_lun))
2380 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2381 /* This one is for the controller itself */
2382 if ((user_cmd->eataID[0] != 'E')
2383 || (user_cmd->eataID[1] != 'A')
2384 || (user_cmd->eataID[2] != 'T')
2385 || (user_cmd->eataID[3] != 'A')) {
2389 /* Get a DPT CCB, so we can prepare a command */
2390 ospl = splsoftcam();
2392 /* Process the free list */
2393 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2394 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2395 " Please try later\n",
2400 /* Now grab the newest CCB */
2401 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2403 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2406 /* Clean up the leftover of the previous tenant */
2407 ccb->status = DPT_CCB_STATE_NEW;
2410 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2411 sizeof(eata_ccb_t));
2413 /* We do not want to do user specified scatter/gather. Why?? */
2414 if (ccb->eata_ccb.scatter == 1)
2417 ccb->eata_ccb.Auto_Req_Sen = 1;
2418 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2419 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2420 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2421 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2422 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2423 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2425 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2426 /* Data I/O is involved in this command. Alocate buffer */
2427 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2428 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2429 M_TEMP, M_WAITOK, 0, ~0,
2430 ccb->eata_ccb.cp_datalen,
2433 data = malloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2438 printf("dpt%d: Cannot allocate %d bytes "
2439 "for EATA command\n", dpt->unit,
2440 ccb->eata_ccb.cp_datalen);
2443 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2444 if (ccb->eata_ccb.DataIn == 1) {
2445 if (copyin(usr_cmd_DMA,
2446 data, ccb->eata_ccb.cp_datalen) == -1)
2450 /* No data I/O involved here. Make sure the DPT knows that */
2451 ccb->eata_ccb.cp_datalen = 0;
2455 if (ccb->eata_ccb.FWNEST == 1)
2456 ccb->eata_ccb.FWNEST = 0;
2458 if (ccb->eata_ccb.cp_datalen != 0) {
2459 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2467 * We are required to quiet a SCSI bus.
2468 * since we do not queue comands on a bus basis,
2469 * we wait for ALL commands on a controller to complete.
2470 * In the mean time, sched_queue() will not schedule new commands.
2472 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2473 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2474 /* We wait for ALL traffic for this HBa to subside */
2475 ospl = splsoftcam();
2476 dpt->state |= DPT_HA_QUIET;
2479 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2480 huh = tsleep((void *) dpt, PCATCH | PRIBIO, "dptqt",
2484 /* Wakeup call received */
2495 /* Resume normal operation */
2496 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2497 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2498 ospl = splsoftcam();
2499 dpt->state &= ~DPT_HA_QUIET;
2503 * Schedule the command and submit it.
2504 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2508 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2510 ccb->transaction_id = ++dpt->commands_processed;
2511 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2512 ccb->result = (u_int32_t) & cmdarg;
2515 #ifdef DPT_MEASURE_PERFORMANCE
2516 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2517 ccb->command_started = microtime_now;
2520 dpt_Qadd_waiting(dpt, ccb);
2523 dpt_sched_queue(dpt);
2525 /* Wait for the command to complete */
2526 (void) tsleep((void *) ccb, PCATCH | PRIBIO, "dptucw", 100 * hz);
2528 /* Free allocated memory */
2536 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2538 int ospl = splsoftcam();
2543 * If Auto Request Sense is on, copyout the sense struct
2545 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2546 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2547 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2548 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2549 sizeof(struct scsi_sense_data))) {
2550 ccb->result = EFAULT;
2551 dpt_Qpush_free(dpt, ccb);
2557 /* If DataIn is on, copyout the data */
2558 if ((ccb->eata_ccb.DataIn == 1)
2559 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2560 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2561 dpt_Qpush_free(dpt, ccb);
2562 ccb->result = EFAULT;
2569 /* Copyout the status */
2570 result = ccb->status_packet.hba_stat;
2571 cmd_arg = (caddr_t) ccb->result;
2573 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2574 dpt_Qpush_free(dpt, ccb);
2575 ccb->result = EFAULT;
2580 /* Put the CCB back in the freelist */
2581 ccb->state |= DPT_CCB_STATE_COMPLETED;
2582 dpt_Qpush_free(dpt, ccb);
2584 /* Free allocated memory */
2589 #ifdef DPT_HANDLE_TIMEOUTS
2591 * This function walks down the SUBMITTED queue.
2592 * Every request that is too old gets aborted and marked.
2593 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2594 * We just walk the list, aborting old commands and marking them as such.
2595 * The dpt_complete function will get rid of the that were interrupted in the
2598 * This function needs to run at splcam(), as it interacts with the submitted
2599 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2600 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2601 * willbe able to pre-empt it, grab a transaction in progress (towards
2602 * destruction) and operate on it. The state of this transaction will be not
2604 * The only other option, is to lock it only as long as necessary but have
2605 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2606 * a SMP environment, the advantage is dubvious for a function that runs once
2607 * every ten seconds for few microseconds and, on systems with healthy
2608 * hardware, does not do anything anyway.
2612 dpt_handle_timeouts(dpt_softc_t * dpt)
2619 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2620 printf("dpt%d WARNING: Timeout Handling Collision\n",
2625 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2627 /* Loop through the entire submitted queue, looking for lost souls */
2628 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2630 ccb = TAILQ_NEXT(ccb, links)) {
2631 struct scsi_xfer *xs;
2632 u_int32_t age, max_age;
2635 age = dpt_time_delta(ccb->command_started, microtime_now);
2637 #define TenSec 10000000
2639 if (xs == NULL) { /* Local, non-kernel call */
2642 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2643 + DPT_TIMEOUT_FACTOR))
2645 ? (xs->timeout * (dpt->submitted_ccbs_count
2646 + DPT_TIMEOUT_FACTOR))
2651 * If a transaction is marked lost and is TWICE as old as we
2652 * care, then, and only then do we destroy it!
2654 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2655 /* Remember who is next */
2656 if (age > (max_age * 2)) {
2657 dpt_Qremove_submitted(dpt, ccb);
2658 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2659 ccb->state |= DPT_CCB_STATE_ABORTED;
2660 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2661 if (ccb->retries++ > DPT_RETRIES) {
2662 printf("dpt%d ERROR: Destroying stale "
2665 "c%db%dt%du%d (%d/%d)\n",
2666 dpt->unit, ccb->transaction_id,
2669 ccb->eata_ccb.cp_channel,
2670 ccb->eata_ccb.cp_id,
2671 ccb->eata_ccb.cp_LUN, age,
2673 #define send_ccb &ccb->eata_ccb
2674 #define ESA EATA_SPECIFIC_ABORT
2675 (void) dpt_send_immediate(dpt,
2679 dpt_Qpush_free(dpt, ccb);
2681 /* The SCSI layer should re-try */
2682 xs->error |= XS_TIMEOUT;
2683 xs->flags |= SCSI_ITSDONE;
2686 printf("dpt%d ERROR: Stale %d (%s) on "
2687 "c%db%dt%du%d (%d)\n"
2690 dpt->unit, ccb->transaction_id,
2693 ccb->eata_ccb.cp_channel,
2694 ccb->eata_ccb.cp_id,
2695 ccb->eata_ccb.cp_LUN,
2696 age, ccb->retries, DPT_RETRIES);
2698 dpt_Qpush_waiting(dpt, ccb);
2699 dpt_sched_queue(dpt);
2704 * This is a transaction that is not to be destroyed
2705 * (yet) But it is too old for our liking. We wait as
2706 * long as the upper layer thinks. Not really, we
2707 * multiply that by the number of commands in the
2708 * submitted queue + 1.
2710 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2711 (age != ~0) && (age > max_age)) {
2712 printf("dpt%d ERROR: Marking %d (%s) on "
2714 " as late after %dusec\n",
2715 dpt->unit, ccb->transaction_id,
2717 dpt->unit, ccb->eata_ccb.cp_channel,
2718 ccb->eata_ccb.cp_id,
2719 ccb->eata_ccb.cp_LUN, age);
2720 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2725 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2730 dpt_timeout(void *arg)
2732 dpt_softc_t *dpt = (dpt_softc_t *) arg;
2734 if (!(dpt->state & DPT_HA_TIMEOUTS_ACTIVE))
2735 dpt_handle_timeouts(dpt);
2737 timeout(dpt_timeout, (caddr_t) dpt, hz * 10);
2740 #endif /* DPT_HANDLE_TIMEOUTS */