2 * Copyright (c) 1997 by Simon Shapiro
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * dpt_scsi.c: SCSI dependant code for the DPT driver
33 * credits: Assisted by Mike Neuffer in the early low level DPT code
34 * Thanx to Mark Salyzyn of DPT for his assistance.
35 * Special thanx to Justin Gibbs for invaluable help in
36 * making this driver look and work like a FreeBSD component.
37 * Last but not least, many thanx to UCB and the FreeBSD
38 * team for creating and maintaining such a wonderful O/S.
40 * TODO: * Add ISA probe code.
41 * * Add driver-level RAID-0. This will allow interoperability with
42 * NiceTry, M$-Doze, Win-Dog, Slowlaris, etc., in recognizing RAID
43 * arrays that span controllers (Wow!).
46 #ident "$FreeBSD: src/sys/dev/dpt/dpt_scsi.c,v 1.28.2.3 2003/01/31 02:47:10 grog Exp $"
47 #ident "$DragonFly: src/sys/dev/raid/dpt/dpt_scsi.c,v 1.10 2006/09/05 00:55:41 dillon Exp $"
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/eventhandler.h>
55 #include <sys/malloc.h>
56 #include <sys/kernel.h>
59 #include <sys/thread2.h>
61 #include <machine/bus_memio.h>
62 #include <machine/bus_pio.h>
63 #include <machine/bus.h>
65 #include <machine/clock.h>
67 #include <bus/cam/cam.h>
68 #include <bus/cam/cam_ccb.h>
69 #include <bus/cam/cam_sim.h>
70 #include <bus/cam/cam_xpt_sim.h>
71 #include <bus/cam/cam_debug.h>
72 #include <bus/cam/scsi/scsi_all.h>
73 #include <bus/cam/scsi/scsi_message.h>
80 /* dpt_isa.c, dpt_eisa.c, and dpt_pci.c need this in a central place */
81 int dpt_controllers_present;
83 u_long dpt_unit; /* Next unit number to use */
85 /* The linked list of softc structures */
86 struct dpt_softc_list dpt_softcs = TAILQ_HEAD_INITIALIZER(dpt_softcs);
88 #define microtime_now dpt_time_now()
90 #define dpt_inl(dpt, port) \
91 bus_space_read_4((dpt)->tag, (dpt)->bsh, port)
92 #define dpt_inb(dpt, port) \
93 bus_space_read_1((dpt)->tag, (dpt)->bsh, port)
94 #define dpt_outl(dpt, port, value) \
95 bus_space_write_4((dpt)->tag, (dpt)->bsh, port, value)
96 #define dpt_outb(dpt, port, value) \
97 bus_space_write_1((dpt)->tag, (dpt)->bsh, port, value)
100 * These will have to be setup by parameters passed at boot/load time. For
101 * perfromance reasons, we make them constants for the time being.
103 #define dpt_min_segs DPT_MAX_SEGS
104 #define dpt_max_segs DPT_MAX_SEGS
106 /* Definitions for our use of the SIM private CCB area */
107 #define ccb_dccb_ptr spriv_ptr0
108 #define ccb_dpt_ptr spriv_ptr1
110 /* ================= Private Inline Function declarations ===================*/
111 static __inline int dpt_just_reset(dpt_softc_t * dpt);
112 static __inline int dpt_raid_busy(dpt_softc_t * dpt);
113 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 u_int32_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'))
189 dpt_pio_wait (u_int32_t base, u_int reg, u_int bits, u_int state)
194 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
195 c = inb(base + reg) & bits;
205 dpt_wait(dpt_softc_t *dpt, u_int bits, u_int state)
210 for (i = 0; i < 20000; i++) { /* wait 20ms for not busy */
211 c = dpt_inb(dpt, HA_RSTATUS) & bits;
220 static __inline struct dpt_ccb*
221 dptgetccb(struct dpt_softc *dpt)
223 struct dpt_ccb* dccb;
226 if ((dccb = SLIST_FIRST(&dpt->free_dccb_list)) != NULL) {
227 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
229 } else if (dpt->total_dccbs < dpt->max_dccbs) {
231 dccb = SLIST_FIRST(&dpt->free_dccb_list);
233 printf("dpt%d: Can't malloc DCCB\n", dpt->unit);
235 SLIST_REMOVE_HEAD(&dpt->free_dccb_list, links);
245 dptfreeccb(struct dpt_softc *dpt, struct dpt_ccb *dccb)
248 if ((dccb->state & DCCB_ACTIVE) != 0)
249 LIST_REMOVE(&dccb->ccb->ccb_h, sim_links.le);
250 if ((dccb->state & DCCB_RELEASE_SIMQ) != 0)
251 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
252 else if (dpt->resource_shortage != 0
253 && (dccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
254 dccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
255 dpt->resource_shortage = FALSE;
257 dccb->state = DCCB_FREE;
258 SLIST_INSERT_HEAD(&dpt->free_dccb_list, dccb, links);
263 static __inline u_int32_t
264 dptccbvtop(struct dpt_softc *dpt, struct dpt_ccb *dccb)
266 return (dpt->dpt_ccb_busbase
267 + (u_int32_t)((caddr_t)dccb - (caddr_t)dpt->dpt_dccbs));
270 static __inline struct dpt_ccb *
271 dptccbptov(struct dpt_softc *dpt, u_int32_t busaddr)
273 return (dpt->dpt_dccbs
274 + ((struct dpt_ccb *)busaddr
275 - (struct dpt_ccb *)dpt->dpt_ccb_busbase));
279 * Send a command for immediate execution by the DPT
280 * See above function for IMPORTANT notes.
283 dpt_send_immediate(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
284 u_int32_t cmd_busaddr, u_int retries,
285 u_int ifc, u_int code, u_int code2)
287 return (dpt_send_eata_command(dpt, cmd_block, cmd_busaddr,
288 EATA_CMD_IMMEDIATE, retries, ifc,
293 /* ===================== Private Function definitions =======================*/
295 dptmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
297 bus_addr_t *busaddrp;
299 busaddrp = (bus_addr_t *)arg;
300 *busaddrp = segs->ds_addr;
303 static struct sg_map_node *
304 dptallocsgmap(struct dpt_softc *dpt)
306 struct sg_map_node *sg_map;
308 sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_INTWAIT);
310 /* Allocate S/G space for the next batch of CCBS */
311 if (bus_dmamem_alloc(dpt->sg_dmat, (void **)&sg_map->sg_vaddr,
312 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
313 kfree(sg_map, M_DEVBUF);
317 (void)bus_dmamap_load(dpt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
318 PAGE_SIZE, dptmapmem, &sg_map->sg_physaddr,
321 SLIST_INSERT_HEAD(&dpt->sg_maps, sg_map, links);
327 * Allocate another chunk of CCB's. Return count of entries added.
328 * Assumed to be called at splcam().
331 dptallocccbs(dpt_softc_t *dpt)
333 struct dpt_ccb *next_ccb;
334 struct sg_map_node *sg_map;
340 next_ccb = &dpt->dpt_dccbs[dpt->total_dccbs];
342 if (next_ccb == dpt->dpt_dccbs) {
344 * First time through. Re-use the S/G
345 * space we allocated for initialization
348 sg_map = SLIST_FIRST(&dpt->sg_maps);
350 sg_map = dptallocsgmap(dpt);
356 segs = sg_map->sg_vaddr;
357 physaddr = sg_map->sg_physaddr;
359 newcount = (PAGE_SIZE / (dpt->sgsize * sizeof(dpt_sg_t)));
360 for (i = 0; dpt->total_dccbs < dpt->max_dccbs && i < newcount; i++) {
363 error = bus_dmamap_create(dpt->buffer_dmat, /*flags*/0,
367 next_ccb->sg_list = segs;
368 next_ccb->sg_busaddr = htonl(physaddr);
369 next_ccb->eata_ccb.cp_dataDMA = htonl(physaddr);
370 next_ccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
371 next_ccb->eata_ccb.cp_reqDMA =
372 htonl(dptccbvtop(dpt, next_ccb)
373 + offsetof(struct dpt_ccb, sense_data));
374 next_ccb->eata_ccb.cp_busaddr = dpt->dpt_ccb_busend;
375 next_ccb->state = DCCB_FREE;
376 next_ccb->tag = dpt->total_dccbs;
377 SLIST_INSERT_HEAD(&dpt->free_dccb_list, next_ccb, links);
379 physaddr += (dpt->sgsize * sizeof(dpt_sg_t));
380 dpt->dpt_ccb_busend += sizeof(*next_ccb);
388 dpt_pio_get_conf (u_int32_t base)
390 static dpt_conf_t * conf;
395 * Allocate a dpt_conf_t
398 conf = kmalloc(sizeof(dpt_conf_t), M_DEVBUF, M_INTWAIT);
401 * If we have one, clean it up.
403 bzero(conf, sizeof(dpt_conf_t));
406 * Reset the controller.
408 outb((base + HA_WCOMMAND), EATA_CMD_RESET);
411 * Wait for the controller to become ready.
412 * For some reason there can be -no- delays after calling reset
413 * before we wait on ready status.
415 if (dpt_pio_wait(base, HA_RSTATUS, HA_SBUSY, 0)) {
416 printf("dpt: timeout waiting for controller to become ready\n");
420 if (dpt_pio_wait(base, HA_RAUXSTAT, HA_ABUSY, 0)) {
421 printf("dpt: timetout waiting for adapter ready.\n");
426 * Send the PIO_READ_CONFIG command.
428 outb((base + HA_WCOMMAND), EATA_CMD_PIO_READ_CONFIG);
431 * Read the data into the struct.
433 p = (u_int16_t *)conf;
434 for (i = 0; i < (sizeof(dpt_conf_t) / 2); i++) {
436 if (dpt_pio_wait(base, HA_RSTATUS, HA_SDRQ, 0)) {
437 printf("dpt: timeout in data read.\n");
441 (*p) = inw(base + HA_RDATA);
445 if (inb(base + HA_RSTATUS) & HA_SERROR) {
446 printf("dpt: error reading configuration data.\n");
450 #define BE_EATA_SIGNATURE 0x45415441
451 #define LE_EATA_SIGNATURE 0x41544145
454 * Test to see if we have a valid card.
456 if ((conf->signature == BE_EATA_SIGNATURE) ||
457 (conf->signature == LE_EATA_SIGNATURE)) {
459 while (inb(base + HA_RSTATUS) & HA_SDRQ) {
460 inw(base + HA_RDATA);
469 * Read a configuration page into the supplied dpt_cont_t buffer.
472 dpt_get_conf(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
473 u_int size, u_int page, u_int target, int extent)
482 cp = &dccb->eata_ccb;
483 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(*dpt->sp));
487 cp->Auto_Req_Sen = 1;
488 cp->reqlen = sizeof(struct scsi_sense_data);
491 cp->cp_LUN = 0; /* In the EATA packet */
492 cp->cp_lun = 0; /* In the SCSI command */
494 cp->cp_scsi_cmd = INQUIRY;
497 cp->cp_extent = extent;
500 cp->cp_channel = 0; /* DNC, Interpret mode is set */
502 cp->cp_datalen = htonl(size);
507 * This could be a simple for loop, but we suspected the compiler To
508 * have optimized it a bit too much. Wait for the controller to
511 while (((status = dpt_inb(dpt, HA_RSTATUS)) != (HA_SREADY | HA_SSC)
512 && (status != (HA_SREADY | HA_SSC | HA_SERROR))
513 && (status != (HA_SDRDY | HA_SERROR | HA_SDRQ)))
514 || (dpt_wait(dpt, HA_SBUSY, 0))) {
517 * RAID Drives still Spinning up? (This should only occur if
518 * the DPT controller is in a NON PC (PCI?) platform).
520 if (dpt_raid_busy(dpt)) {
521 printf("dpt%d WARNING: Get_conf() RSUS failed.\n",
528 DptStat_Reset_BUSY(dpt->sp);
531 * XXXX We might want to do something more clever than aborting at
532 * this point, like resetting (rebooting) the controller and trying
535 if ((result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
536 EATA_CMD_DMA_SEND_CP,
537 10000, 0, 0, 0)) != 0) {
538 printf("dpt%d WARNING: Get_conf() failed (%d) to send "
539 "EATA_CMD_DMA_READ_CONFIG\n",
544 /* Wait for two seconds for a response. This can be slow */
547 && !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
552 /* Grab the status and clear interrupts */
553 status = dpt_inb(dpt, HA_RSTATUS);
558 * Check the status carefully. Return only if the
559 * command was successful.
561 if (((status & HA_SERROR) == 0)
562 && (dpt->sp->hba_stat == 0)
563 && (dpt->sp->scsi_stat == 0)
564 && (dpt->sp->residue_len == 0))
567 if (dpt->sp->scsi_stat == SCSI_STATUS_CHECK_COND)
573 /* Detect Cache parameters and size */
575 dpt_detect_cache(dpt_softc_t *dpt, dpt_ccb_t *dccb, u_int32_t dccb_busaddr,
586 * Default setting, for best perfromance..
587 * This is what virtually all cards default to..
589 dpt->cache_type = DPT_CACHE_WRITEBACK;
592 cp = &dccb->eata_ccb;
593 bzero((void *)(uintptr_t)(volatile void *)dpt->sp, sizeof(dpt->sp));
596 /* Setup the command structure */
599 cp->Auto_Req_Sen = 1;
600 cp->reqlen = sizeof(struct scsi_sense_data);
602 cp->cp_id = 0; /* who cares? The HBA will interpret.. */
603 cp->cp_LUN = 0; /* In the EATA packet */
604 cp->cp_lun = 0; /* In the SCSI command */
607 cp->cp_scsi_cmd = EATA_CMD_DMA_SEND_CP;
616 * Build the EATA Command Packet structure
617 * for a Log Sense Command.
619 cp->cp_cdb[0] = 0x4d;
621 cp->cp_cdb[2] = 0x40 | 0x33;
624 cp->cp_datalen = htonl(512);
627 result = dpt_send_eata_command(dpt, cp, dccb_busaddr,
628 EATA_CMD_DMA_SEND_CP,
631 printf("dpt%d WARNING: detect_cache() failed (%d) to send "
632 "EATA_CMD_DMA_SEND_CP\n", dpt->unit, result);
636 /* Wait for two seconds for a response. This can be slow... */
639 !((status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ);
644 /* Grab the status and clear interrupts */
645 status = dpt_inb(dpt, HA_RSTATUS);
651 if (buff[0] != 0x33) {
654 bytes = DPT_HCP_LENGTH(buff);
655 param = DPT_HCP_FIRST(buff);
657 if (DPT_HCP_CODE(param) != 1) {
659 * DPT Log Page layout error
661 printf("dpt%d: NOTICE: Log Page (1) layout error\n",
665 if (!(param[4] & 0x4)) {
666 dpt->cache_type = DPT_NO_CACHE;
669 while (DPT_HCP_CODE(param) != 6) {
670 param = DPT_HCP_NEXT(param);
672 || (param >= &buff[bytes])) {
677 if (param[4] & 0x2) {
681 dpt->cache_type = DPT_NO_CACHE;
685 if (param[4] & 0x4) {
686 dpt->cache_type = DPT_CACHE_WRITETHROUGH;
689 /* XXX This isn't correct. This log parameter only has two bytes.... */
691 dpt->cache_size = param[5]
699 dpt_poll(struct cam_sim *sim)
701 dpt_intr(cam_sim_softc(sim));
705 dptexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
707 struct dpt_ccb *dccb;
709 struct dpt_softc *dpt;
711 dccb = (struct dpt_ccb *)arg;
713 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
717 printf("dpt%d: Unexepected error 0x%x returned from "
718 "bus_dmamap_load\n", dpt->unit, error);
719 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
720 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
721 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
723 dptfreeccb(dpt, dccb);
730 bus_dma_segment_t *end_seg;
733 end_seg = dm_segs + nseg;
735 /* Copy the segments into our SG list */
737 while (dm_segs < end_seg) {
738 sg->seg_len = htonl(dm_segs->ds_len);
739 sg->seg_addr = htonl(dm_segs->ds_addr);
745 dccb->eata_ccb.scatter = 1;
746 dccb->eata_ccb.cp_dataDMA = dccb->sg_busaddr;
747 dccb->eata_ccb.cp_datalen =
748 htonl(nseg * sizeof(dpt_sg_t));
750 dccb->eata_ccb.cp_dataDMA = dccb->sg_list[0].seg_addr;
751 dccb->eata_ccb.cp_datalen = dccb->sg_list[0].seg_len;
754 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
755 op = BUS_DMASYNC_PREREAD;
757 op = BUS_DMASYNC_PREWRITE;
759 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
762 dccb->eata_ccb.cp_dataDMA = 0;
763 dccb->eata_ccb.cp_datalen = 0;
769 * Last time we need to check if this CCB needs to
772 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
774 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
775 dptfreeccb(dpt, dccb);
781 dccb->state |= DCCB_ACTIVE;
782 ccb->ccb_h.status |= CAM_SIM_QUEUED;
783 LIST_INSERT_HEAD(&dpt->pending_ccb_list, &ccb->ccb_h, sim_links.le);
784 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
786 if (dpt_send_eata_command(dpt, &dccb->eata_ccb,
787 dccb->eata_ccb.cp_busaddr,
788 EATA_CMD_DMA_SEND_CP, 0, 0, 0, 0) != 0) {
789 ccb->ccb_h.status = CAM_NO_HBA; /* HBA dead or just busy?? */
791 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
792 dptfreeccb(dpt, dccb);
800 dpt_action(struct cam_sim *sim, union ccb *ccb)
802 struct dpt_softc *dpt;
804 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("dpt_action\n"));
806 dpt = (struct dpt_softc *)cam_sim_softc(sim);
808 if ((dpt->state & DPT_HA_SHUTDOWN_ACTIVE) != 0) {
809 xpt_print_path(ccb->ccb_h.path);
810 printf("controller is shutdown. Aborting CCB.\n");
811 ccb->ccb_h.status = CAM_NO_HBA;
816 switch (ccb->ccb_h.func_code) {
817 /* Common cases first */
818 case XPT_SCSI_IO: /* Execute the requested I/O operation */
820 struct ccb_scsiio *csio;
821 struct ccb_hdr *ccbh;
822 struct dpt_ccb *dccb;
823 struct eata_ccb *eccb;
827 /* Max CDB length is 12 bytes */
828 if (csio->cdb_len > 12) {
829 ccb->ccb_h.status = CAM_REQ_INVALID;
833 if ((dccb = dptgetccb(dpt)) == NULL) {
835 dpt->resource_shortage = 1;
837 xpt_freeze_simq(sim, /*count*/1);
838 ccb->ccb_h.status = CAM_REQUEUE_REQ;
842 eccb = &dccb->eata_ccb;
844 /* Link dccb and ccb so we can find one from the other */
846 ccb->ccb_h.ccb_dccb_ptr = dccb;
847 ccb->ccb_h.ccb_dpt_ptr = dpt;
850 * Explicitly set all flags so that the compiler can
851 * be smart about setting them.
853 eccb->SCSI_Reset = 0;
855 eccb->Auto_Req_Sen = (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE)
860 ccb->ccb_h.target_id == dpt->hostid[cam_sim_bus(sim)]
862 eccb->DataOut = (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
863 eccb->DataIn = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
864 eccb->reqlen = csio->sense_len;
865 eccb->cp_id = ccb->ccb_h.target_id;
866 eccb->cp_channel = cam_sim_bus(sim);
867 eccb->cp_LUN = ccb->ccb_h.target_lun;
869 eccb->cp_dispri = (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
871 eccb->cp_identify = 1;
873 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0
874 && csio->tag_action != CAM_TAG_ACTION_NONE) {
875 eccb->cp_msg[0] = csio->tag_action;
876 eccb->cp_msg[1] = dccb->tag;
883 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
884 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
885 bcopy(csio->cdb_io.cdb_ptr,
886 eccb->cp_cdb, csio->cdb_len);
888 /* I guess I could map it in... */
889 ccb->ccb_h.status = CAM_REQ_INVALID;
890 dptfreeccb(dpt, dccb);
895 bcopy(csio->cdb_io.cdb_bytes,
896 eccb->cp_cdb, csio->cdb_len);
899 * If we have any data to send with this command,
900 * map it into bus space.
902 /* Only use S/G if there is a transfer */
903 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
904 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
906 * We've been given a pointer
907 * to a single buffer.
909 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
914 bus_dmamap_load(dpt->buffer_dmat,
920 if (error == EINPROGRESS) {
922 * So as to maintain ordering,
923 * freeze the controller queue
924 * until our mapping is
927 xpt_freeze_simq(sim, 1);
928 dccb->state |= CAM_RELEASE_SIMQ;
932 struct bus_dma_segment seg;
934 /* Pointer to physical buffer */
936 (bus_addr_t)csio->data_ptr;
937 seg.ds_len = csio->dxfer_len;
938 dptexecuteccb(dccb, &seg, 1, 0);
941 struct bus_dma_segment *segs;
943 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
944 panic("dpt_action - Physical "
948 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
949 panic("dpt_action - Virtual "
953 /* Just use the segments provided */
954 segs = (struct bus_dma_segment *)csio->data_ptr;
955 dptexecuteccb(dccb, segs, csio->sglist_cnt, 0);
960 * Does it want them both on or both off?
961 * CAM_DIR_NONE is both on, so this code can
962 * be removed if this is also what the DPT
967 dptexecuteccb(dccb, NULL, 0, 0);
971 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
972 case XPT_ABORT: /* Abort the specified CCB */
974 ccb->ccb_h.status = CAM_REQ_INVALID;
977 case XPT_SET_TRAN_SETTINGS:
979 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
983 case XPT_GET_TRAN_SETTINGS:
984 /* Get default/user set transfer settings for the target */
986 struct ccb_trans_settings *cts;
990 target_mask = 0x01 << ccb->ccb_h.target_id;
991 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
992 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
993 cts->bus_width = (dpt->max_id > 7)
994 ? MSG_EXT_WDTR_BUS_8_BIT
995 : MSG_EXT_WDTR_BUS_16_BIT;
996 cts->sync_period = 25; /* 10MHz */
998 if (cts->sync_period != 0)
999 cts->sync_offset = 15;
1001 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1002 | CCB_TRANS_SYNC_OFFSET_VALID
1003 | CCB_TRANS_BUS_WIDTH_VALID
1004 | CCB_TRANS_DISC_VALID
1005 | CCB_TRANS_TQ_VALID;
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->ccb_h.status = CAM_REQ_CMP;
1079 ccb->ccb_h.status = CAM_REQ_INVALID;
1086 * This routine will try to send an EATA command to the DPT HBA.
1087 * It will, by default, try 20,000 times, waiting 50us between tries.
1088 * It returns 0 on success and 1 on failure.
1089 * It is assumed to be called at splcam().
1092 dpt_send_eata_command(dpt_softc_t *dpt, eata_ccb_t *cmd_block,
1093 u_int32_t cmd_busaddr, u_int command, u_int retries,
1094 u_int ifc, u_int code, u_int code2)
1102 * I hate this polling nonsense. Wish there was a way to tell the DPT
1103 * to go get commands at its own pace, or to interrupt when ready.
1104 * In the mean time we will measure how many itterations it really
1107 for (loop = 0; loop < retries; loop++) {
1108 if ((dpt_inb(dpt, HA_RAUXSTAT) & HA_ABUSY) == 0)
1114 if (loop < retries) {
1115 #ifdef DPT_MEASURE_PERFORMANCE
1116 if (loop > dpt->performance.max_eata_tries)
1117 dpt->performance.max_eata_tries = loop;
1119 if (loop < dpt->performance.min_eata_tries)
1120 dpt->performance.min_eata_tries = loop;
1123 #ifdef DPT_MEASURE_PERFORMANCE
1124 ++dpt->performance.command_too_busy;
1129 /* The controller is alive, advance the wedge timer */
1130 #ifdef DPT_RESET_HBA
1131 dpt->last_contact = microtime_now;
1134 if (cmd_block == NULL)
1136 #if (BYTE_ORDER == BIG_ENDIAN)
1138 cmd_busaddr = ((cmd_busaddr >> 24) & 0xFF)
1139 | ((cmd_busaddr >> 16) & 0xFF)
1140 | ((cmd_busaddr >> 8) & 0xFF)
1141 | (cmd_busaddr & 0xFF);
1144 /* And now the address */
1145 dpt_outl(dpt, HA_WDMAADDR, cmd_busaddr);
1147 if (command == EATA_CMD_IMMEDIATE) {
1148 if (cmd_block == NULL) {
1149 dpt_outb(dpt, HA_WCODE2, code2);
1150 dpt_outb(dpt, HA_WCODE, code);
1152 dpt_outb(dpt, HA_WIFC, ifc);
1154 dpt_outb(dpt, HA_WCOMMAND, command);
1160 /* ==================== Exported Function definitions =======================*/
1162 dpt_alloc(device_t dev, bus_space_tag_t tag, bus_space_handle_t bsh)
1164 dpt_softc_t *dpt = device_get_softc(dev);
1167 bzero(dpt, sizeof(dpt_softc_t));
1170 dpt->unit = device_get_unit(dev);
1171 SLIST_INIT(&dpt->free_dccb_list);
1172 LIST_INIT(&dpt->pending_ccb_list);
1173 TAILQ_INSERT_TAIL(&dpt_softcs, dpt, links);
1174 for (i = 0; i < MAX_CHANNELS; i++)
1175 dpt->resetlevel[i] = DPT_HA_OK;
1177 #ifdef DPT_MEASURE_PERFORMANCE
1178 dpt_reset_performance(dpt);
1179 #endif /* DPT_MEASURE_PERFORMANCE */
1184 dpt_free(struct dpt_softc *dpt)
1186 switch (dpt->init_level) {
1189 bus_dmamap_unload(dpt->dccb_dmat, dpt->dccb_dmamap);
1191 bus_dmamem_free(dpt->dccb_dmat, dpt->dpt_dccbs,
1193 bus_dmamap_destroy(dpt->dccb_dmat, dpt->dccb_dmamap);
1195 bus_dma_tag_destroy(dpt->dccb_dmat);
1197 bus_dma_tag_destroy(dpt->buffer_dmat);
1200 struct sg_map_node *sg_map;
1202 while ((sg_map = SLIST_FIRST(&dpt->sg_maps)) != NULL) {
1203 SLIST_REMOVE_HEAD(&dpt->sg_maps, links);
1204 bus_dmamap_unload(dpt->sg_dmat,
1206 bus_dmamem_free(dpt->sg_dmat, sg_map->sg_vaddr,
1208 kfree(sg_map, M_DEVBUF);
1210 bus_dma_tag_destroy(dpt->sg_dmat);
1215 TAILQ_REMOVE(&dpt_softcs, dpt, links);
1218 static u_int8_t string_sizes[] =
1220 sizeof(((dpt_inq_t*)NULL)->vendor),
1221 sizeof(((dpt_inq_t*)NULL)->modelNum),
1222 sizeof(((dpt_inq_t*)NULL)->firmware),
1223 sizeof(((dpt_inq_t*)NULL)->protocol),
1227 dpt_init(struct dpt_softc *dpt)
1230 struct sg_map_node *sg_map;
1237 dpt->init_level = 0;
1238 SLIST_INIT(&dpt->sg_maps);
1240 #ifdef DPT_RESET_BOARD
1241 printf("dpt%d: resetting HBA\n", dpt->unit);
1242 dpt_outb(dpt, HA_WCOMMAND, EATA_CMD_RESET);
1244 /* XXX Shouldn't we poll a status register or something??? */
1246 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1247 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1248 /*lowaddr*/BUS_SPACE_MAXADDR,
1249 /*highaddr*/BUS_SPACE_MAXADDR,
1250 /*filter*/NULL, /*filterarg*/NULL,
1251 PAGE_SIZE, /*nsegments*/1,
1252 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1253 /*flags*/0, &dpt->sg_dmat) != 0) {
1260 * We allocate our DPT ccbs as a contiguous array of bus dma'able
1261 * memory. To get the allocation size, we need to know how many
1262 * ccbs the card supports. This requires a ccb. We solve this
1263 * chicken and egg problem by allocating some re-usable S/G space
1264 * up front, and treating it as our status packet, CCB, and target
1265 * memory space for these commands.
1267 sg_map = dptallocsgmap(dpt);
1271 dpt->sp = (volatile dpt_sp_t *)sg_map->sg_vaddr;
1272 dccb = (struct dpt_ccb *)(uintptr_t)(volatile void *)&dpt->sp[1];
1273 bzero(dccb, sizeof(*dccb));
1274 dpt->sp_physaddr = sg_map->sg_physaddr;
1275 dccb->eata_ccb.cp_dataDMA =
1276 htonl(sg_map->sg_physaddr + sizeof(dpt_sp_t) + sizeof(*dccb));
1277 dccb->eata_ccb.cp_busaddr = ~0;
1278 dccb->eata_ccb.cp_statDMA = htonl(dpt->sp_physaddr);
1279 dccb->eata_ccb.cp_reqDMA = htonl(dpt->sp_physaddr + sizeof(*dccb)
1280 + offsetof(struct dpt_ccb, sense_data));
1282 /* Okay. Fetch our config */
1283 bzero(&dccb[1], sizeof(conf)); /* data area */
1284 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1285 sizeof(conf), 0xc1, 7, 1);
1288 printf("dpt%d: Failed to get board configuration\n", dpt->unit);
1291 bcopy(&dccb[1], &conf, sizeof(conf));
1293 bzero(&dccb[1], sizeof(dpt->board_data));
1294 retval = dpt_get_conf(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1295 sizeof(dpt->board_data), 0, conf.scsi_id0, 0);
1297 printf("dpt%d: Failed to get inquiry information\n", dpt->unit);
1300 bcopy(&dccb[1], &dpt->board_data, sizeof(dpt->board_data));
1302 dpt_detect_cache(dpt, dccb, sg_map->sg_physaddr + sizeof(dpt_sp_t),
1303 (u_int8_t *)&dccb[1]);
1305 switch (ntohl(conf.splen)) {
1307 dpt->EATA_revision = 'a';
1310 dpt->EATA_revision = 'b';
1313 dpt->EATA_revision = 'c';
1316 dpt->EATA_revision = 'z';
1319 dpt->EATA_revision = '?';
1322 dpt->max_id = conf.MAX_ID;
1323 dpt->max_lun = conf.MAX_LUN;
1324 dpt->irq = conf.IRQ;
1325 dpt->dma_channel = (8 - conf.DMA_channel) & 7;
1326 dpt->channels = conf.MAX_CHAN + 1;
1327 dpt->state |= DPT_HA_OK;
1329 dpt->primary = FALSE;
1331 dpt->primary = TRUE;
1333 dpt->more_support = conf.MORE_support;
1335 if (strncmp(dpt->board_data.firmware, "07G0", 4) >= 0)
1336 dpt->immediate_support = 1;
1338 dpt->immediate_support = 0;
1340 dpt->broken_INQUIRY = FALSE;
1342 dpt->cplen = ntohl(conf.cplen);
1343 dpt->cppadlen = ntohs(conf.cppadlen);
1344 dpt->max_dccbs = ntohs(conf.queuesiz);
1346 if (dpt->max_dccbs > 256) {
1347 printf("dpt%d: Max CCBs reduced from %d to "
1348 "256 due to tag algorithm\n", dpt->unit, dpt->max_dccbs);
1349 dpt->max_dccbs = 256;
1352 dpt->hostid[0] = conf.scsi_id0;
1353 dpt->hostid[1] = conf.scsi_id1;
1354 dpt->hostid[2] = conf.scsi_id2;
1359 dpt->sgsize = ntohs(conf.SGsiz);
1361 /* We can only get 64k buffers, so don't bother to waste space. */
1362 if (dpt->sgsize < 17 || dpt->sgsize > 32)
1365 if (dpt->sgsize > dpt_max_segs)
1366 dpt->sgsize = dpt_max_segs;
1368 /* DMA tag for mapping buffers into device visible space. */
1369 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1370 /*lowaddr*/BUS_SPACE_MAXADDR,
1371 /*highaddr*/BUS_SPACE_MAXADDR,
1372 /*filter*/NULL, /*filterarg*/NULL,
1373 /*maxsize*/MAXBSIZE, /*nsegments*/dpt->sgsize,
1374 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1375 /*flags*/BUS_DMA_ALLOCNOW,
1376 &dpt->buffer_dmat) != 0) {
1377 printf("dpt: bus_dma_tag_create(...,dpt->buffer_dmat) failed\n");
1383 /* DMA tag for our ccb structures and interrupt status packet */
1384 if (bus_dma_tag_create(dpt->parent_dmat, /*alignment*/1, /*boundary*/0,
1385 /*lowaddr*/BUS_SPACE_MAXADDR,
1386 /*highaddr*/BUS_SPACE_MAXADDR,
1387 /*filter*/NULL, /*filterarg*/NULL,
1388 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1391 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
1392 /*flags*/0, &dpt->dccb_dmat) != 0) {
1393 printf("dpt: bus_dma_tag_create(...,dpt->dccb_dmat) failed\n");
1399 /* Allocation for our ccbs and interrupt status packet */
1400 if (bus_dmamem_alloc(dpt->dccb_dmat, (void **)&dpt->dpt_dccbs,
1401 BUS_DMA_NOWAIT, &dpt->dccb_dmamap) != 0) {
1402 printf("dpt: bus_dmamem_alloc(dpt->dccb_dmat,...) failed\n");
1408 /* And permanently map them */
1409 bus_dmamap_load(dpt->dccb_dmat, dpt->dccb_dmamap,
1411 (dpt->max_dccbs * sizeof(struct dpt_ccb))
1413 dptmapmem, &dpt->dpt_ccb_busbase, /*flags*/0);
1415 /* Clear them out. */
1416 bzero(dpt->dpt_dccbs,
1417 (dpt->max_dccbs * sizeof(struct dpt_ccb)) + sizeof(dpt_sp_t));
1419 dpt->dpt_ccb_busend = dpt->dpt_ccb_busbase;
1421 dpt->sp = (dpt_sp_t*)&dpt->dpt_dccbs[dpt->max_dccbs];
1422 dpt->sp_physaddr = dpt->dpt_ccb_busbase
1423 + (dpt->max_dccbs * sizeof(dpt_ccb_t));
1426 /* Allocate our first batch of ccbs */
1427 if (dptallocccbs(dpt) == 0) {
1428 printf("dpt: dptallocccbs(dpt) == 0\n");
1432 /* Prepare for Target Mode */
1433 dpt->target_mode_enabled = 1;
1435 /* Nuke excess spaces from inquiry information */
1436 strp = dpt->board_data.vendor;
1437 for (i = 0; i < sizeof(string_sizes); i++) {
1438 index = string_sizes[i] - 1;
1439 while (index && (strp[index] == ' '))
1440 strp[index--] = '\0';
1441 strp += string_sizes[i];
1444 printf("dpt%d: %.8s %.16s FW Rev. %.4s, ",
1445 dpt->unit, dpt->board_data.vendor,
1446 dpt->board_data.modelNum, dpt->board_data.firmware);
1448 printf("%d channel%s, ", dpt->channels, dpt->channels > 1 ? "s" : "");
1450 if (dpt->cache_type != DPT_NO_CACHE
1451 && dpt->cache_size != 0) {
1452 printf("%s Cache, ",
1453 dpt->cache_type == DPT_CACHE_WRITETHROUGH
1454 ? "Write-Through" : "Write-Back");
1457 printf("%d CCBs\n", dpt->max_dccbs);
1465 dpt_attach(dpt_softc_t *dpt)
1467 struct cam_devq *devq;
1471 * Create the device queue for our SIM.
1473 devq = cam_simq_alloc(dpt->max_dccbs);
1477 for (i = 0; i < dpt->channels; i++) {
1479 * Construct our SIM entry
1481 dpt->sims[i] = cam_sim_alloc(dpt_action, dpt_poll, "dpt",
1482 dpt, dpt->unit, /*untagged*/2,
1483 /*tagged*/dpt->max_dccbs, devq);
1484 if (xpt_bus_register(dpt->sims[i], i) != CAM_SUCCESS) {
1485 cam_sim_free(dpt->sims[i]);
1489 if (xpt_create_path(&dpt->paths[i], /*periph*/NULL,
1490 cam_sim_path(dpt->sims[i]),
1491 CAM_TARGET_WILDCARD,
1492 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1493 xpt_bus_deregister(cam_sim_path(dpt->sims[i]));
1494 cam_sim_free(dpt->sims[i]);
1499 cam_simq_release(devq);
1501 EVENTHANDLER_REGISTER(shutdown_final, dptshutdown,
1502 dpt, SHUTDOWN_PRI_DEFAULT);
1508 * This is the interrupt handler for the DPT driver.
1520 u_int32_t residue_len; /* Number of bytes not transferred */
1522 dpt = (dpt_softc_t *)arg;
1524 /* First order of business is to check if this interrupt is for us */
1525 while (((aux_status = dpt_inb(dpt, HA_RAUXSTAT)) & HA_AIRQ) != 0) {
1528 * What we want to do now, is to capture the status, all of it,
1529 * move it where it belongs, wake up whoever sleeps waiting to
1530 * process this result, and get out of here.
1532 if (dpt->sp->ccb_busaddr < dpt->dpt_ccb_busbase
1533 || dpt->sp->ccb_busaddr >= dpt->dpt_ccb_busend) {
1534 printf("Encountered bogus status packet\n");
1535 status = dpt_inb(dpt, HA_RSTATUS);
1539 dccb = dptccbptov(dpt, dpt->sp->ccb_busaddr);
1541 dpt->sp->ccb_busaddr = ~0;
1543 /* Ignore status packets with EOC not set */
1544 if (dpt->sp->EOC == 0) {
1545 printf("dpt%d ERROR: Request %d received with "
1546 "clear EOC.\n Marking as LOST.\n",
1547 dpt->unit, dccb->transaction_id);
1549 #ifdef DPT_HANDLE_TIMEOUTS
1550 dccb->state |= DPT_CCB_STATE_MARKED_LOST;
1552 /* This CLEARS the interrupt! */
1553 status = dpt_inb(dpt, HA_RSTATUS);
1559 * Double buffer the status information so the hardware can
1560 * work on updating the status packet while we decifer the
1561 * one we were just interrupted for.
1562 * According to Mark Salyzyn, we only need few pieces of it.
1564 hba_stat = dpt->sp->hba_stat;
1565 scsi_stat = dpt->sp->scsi_stat;
1566 residue_len = dpt->sp->residue_len;
1568 /* Clear interrupts, check for error */
1569 if ((status = dpt_inb(dpt, HA_RSTATUS)) & HA_SERROR) {
1571 * Error Condition. Check for magic cookie. Exit
1572 * this test on earliest sign of non-reset condition
1575 /* Check that this is not a board reset interrupt */
1576 if (dpt_just_reset(dpt)) {
1577 printf("dpt%d: HBA rebooted.\n"
1578 " All transactions should be "
1582 printf("dpt%d: >>---->> This is incomplete, "
1583 "fix me.... <<----<<", dpt->unit);
1584 panic("DPT Rebooted");
1590 callout_stop(&ccb->ccb_h.timeout_ch);
1591 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1592 bus_dmasync_op_t op;
1594 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1595 op = BUS_DMASYNC_POSTREAD;
1597 op = BUS_DMASYNC_POSTWRITE;
1598 bus_dmamap_sync(dpt->buffer_dmat, dccb->dmamap, op);
1599 bus_dmamap_unload(dpt->buffer_dmat, dccb->dmamap);
1602 /* Common Case inline... */
1603 if (hba_stat == HA_NO_ERROR) {
1604 ccb->csio.scsi_status = scsi_stat;
1605 ccb->ccb_h.status = 0;
1606 switch (scsi_stat) {
1607 case SCSI_STATUS_OK:
1608 ccb->ccb_h.status |= CAM_REQ_CMP;
1610 case SCSI_STATUS_CHECK_COND:
1611 case SCSI_STATUS_CMD_TERMINATED:
1612 bcopy(&dccb->sense_data, &ccb->csio.sense_data,
1613 ccb->csio.sense_len);
1614 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1617 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1618 /* XXX Freeze DevQ */
1621 ccb->csio.resid = residue_len;
1622 dptfreeccb(dpt, dccb);
1625 dptprocesserror(dpt, dccb, ccb, hba_stat, scsi_stat,
1632 dptprocesserror(dpt_softc_t *dpt, dpt_ccb_t *dccb, union ccb *ccb,
1633 u_int hba_stat, u_int scsi_stat, u_int32_t resid)
1635 ccb->csio.resid = resid;
1638 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1641 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1643 case HA_SCSIBUS_RESET:
1644 case HA_HBA_POWER_UP: /* Similar effect to a bus reset??? */
1645 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1648 case HA_CP_RESET: /* XXX ??? */
1649 case HA_CP_ABORT_NA: /* XXX ??? */
1650 case HA_CP_RESET_NA: /* XXX ??? */
1651 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
1652 ccb->ccb_h.status = CAM_REQ_ABORTED;
1657 case HA_PCI_STABORT:
1661 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1663 case HA_UNX_MSGRJCT:
1664 ccb->ccb_h.status = CAM_MSG_REJECT_REC;
1666 case HA_UNX_BUSPHASE:
1667 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1669 case HA_UNX_BUS_FREE:
1670 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1673 case HA_RESET_STUCK:
1675 * Dead??? Can the controller get unstuck
1676 * from these conditions
1678 ccb->ccb_h.status = CAM_NO_HBA;
1680 case HA_RSENSE_FAIL:
1681 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1684 printf("dpt%d: Undocumented Error %x\n", dpt->unit, hba_stat);
1685 printf("Please mail this message to shimon@simon-shapiro.org\n");
1686 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1689 dptfreeccb(dpt, dccb);
1694 dpttimeout(void *arg)
1696 struct dpt_ccb *dccb;
1698 struct dpt_softc *dpt;
1700 dccb = (struct dpt_ccb *)arg;
1702 dpt = (struct dpt_softc *)ccb->ccb_h.ccb_dpt_ptr;
1703 xpt_print_path(ccb->ccb_h.path);
1704 printf("CCB %p - timed out\n", (void *)dccb);
1709 * Try to clear any pending jobs. FreeBSD will loose interrupts,
1710 * leaving the controller suspended, and commands timed-out.
1711 * By calling the interrupt handler, any command thus stuck will be
1716 if ((dccb->state & DCCB_ACTIVE) == 0) {
1717 xpt_print_path(ccb->ccb_h.path);
1718 printf("CCB %p - timed out CCB already completed\n",
1724 /* Abort this particular command. Leave all others running */
1725 dpt_send_immediate(dpt, &dccb->eata_ccb, dccb->eata_ccb.cp_busaddr,
1726 /*retries*/20000, EATA_SPECIFIC_ABORT, 0, 0);
1727 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1732 * Shutdown the controller and ensure that the cache is completely flushed.
1733 * Called from the shutdown_final event after all disk access has completed.
1736 dptshutdown(void *arg, int howto)
1740 dpt = (dpt_softc_t *)arg;
1742 printf("dpt%d: Shutting down (mode %x) HBA. Please wait...\n",
1746 * What we do for a shutdown, is give the DPT early power loss warning
1748 dpt_send_immediate(dpt, NULL, 0, EATA_POWER_OFF_WARN, 0, 0, 0);
1749 DELAY(1000 * 1000 * 5);
1750 printf("dpt%d: Controller was warned of shutdown and is now "
1751 "disabled\n", dpt->unit);
1754 /*============================================================================*/
1757 #ifdef DPT_RESET_HBA
1760 ** Function name : dpt_reset_hba
1762 ** Description : Reset the HBA and properly discard all pending work
1767 dpt_reset_hba(dpt_softc_t *dpt)
1770 dpt_ccb_t dccb, *dccbp;
1772 struct scsi_xfer *xs;
1774 /* Prepare a control block. The SCSI command part is immaterial */
1777 dccb.state = DPT_CCB_STATE_NEW;
1778 dccb.std_callback = NULL;
1779 dccb.wrbuff_callback = NULL;
1781 ccb = &dccb.eata_ccb;
1782 ccb->CP_OpCode = EATA_CMD_RESET;
1783 ccb->SCSI_Reset = 0;
1785 ccb->Auto_Req_Sen = 1;
1786 ccb->cp_id = 0; /* Should be ignored */
1790 ccb->reqlen = htonl(sizeof(struct scsi_sense_data));
1791 ccb->cp_statDMA = htonl(vtophys(&ccb->cp_statDMA));
1792 ccb->cp_reqDMA = htonl(vtophys(&ccb->cp_reqDMA));
1793 ccb->cp_viraddr = (u_int32_t) & ccb;
1795 ccb->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1796 ccb->cp_scsi_cmd = 0; /* Should be ignored */
1798 /* Lock up the submitted queue. We are very persistant here */
1800 while (dpt->queue_status & DPT_SUBMITTED_QUEUE_ACTIVE) {
1804 dpt->queue_status |= DPT_SUBMITTED_QUEUE_ACTIVE;
1807 /* Send the RESET message */
1808 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1809 EATA_CMD_RESET, 0, 0, 0, 0)) != 0) {
1810 printf("dpt%d: Failed to send the RESET message.\n"
1811 " Trying cold boot (ouch!)\n", dpt->unit);
1814 if ((result = dpt_send_eata_command(dpt, &dccb.eata_ccb,
1815 EATA_COLD_BOOT, 0, 0,
1817 panic("dpt%d: Faild to cold boot the HBA\n",
1820 #ifdef DPT_MEASURE_PERFORMANCE
1821 dpt->performance.cold_boots++;
1822 #endif /* DPT_MEASURE_PERFORMANCE */
1825 #ifdef DPT_MEASURE_PERFORMANCE
1826 dpt->performance.warm_starts++;
1827 #endif /* DPT_MEASURE_PERFORMANCE */
1829 printf("dpt%d: Aborting pending requests. O/S should re-submit\n",
1832 while ((dccbp = TAILQ_FIRST(&dpt->completed_ccbs)) != NULL) {
1833 struct scsi_xfer *xs = dccbp->xs;
1835 /* Not all transactions have xs structs */
1837 /* Tell the kernel proper this did not complete well */
1838 xs->error |= XS_SELTIMEOUT;
1839 xs->flags |= SCSI_ITSDONE;
1843 dpt_Qremove_submitted(dpt, dccbp);
1845 /* Remember, Callbacks are NOT in the standard queue */
1846 if (dccbp->std_callback != NULL) {
1847 (dccbp->std_callback)(dpt, dccbp->eata_ccb.cp_channel,
1851 dpt_Qpush_free(dpt, dccbp);
1856 printf("dpt%d: reset done aborting all pending commands\n", dpt->unit);
1857 dpt->queue_status &= ~DPT_SUBMITTED_QUEUE_ACTIVE;
1860 #endif /* DPT_RESET_HBA */
1863 * Build a Command Block for target mode READ/WRITE BUFFER,
1864 * with the ``sync'' bit ON.
1866 * Although the length and offset are 24 bit fields in the command, they cannot
1867 * exceed 8192 bytes, so we take them as short integers andcheck their range.
1868 * If they are sensless, we round them to zero offset, maximum length and
1873 dpt_target_ccb(dpt_softc_t * dpt, int bus, u_int8_t target, u_int8_t lun,
1874 dpt_ccb_t * ccb, int mode, u_int8_t command,
1875 u_int16_t length, u_int16_t offset)
1879 if ((length + offset) > DPT_MAX_TARGET_MODE_BUFFER_SIZE) {
1880 printf("dpt%d: Length of %d, and offset of %d are wrong\n",
1881 dpt->unit, length, offset);
1882 length = DPT_MAX_TARGET_MODE_BUFFER_SIZE;
1887 ccb->state = DPT_CCB_STATE_NEW;
1888 ccb->std_callback = (ccb_callback) dpt_target_done;
1889 ccb->wrbuff_callback = NULL;
1891 cp = &ccb->eata_ccb;
1892 cp->CP_OpCode = EATA_CMD_DMA_SEND_CP;
1895 cp->Auto_Req_Sen = 1;
1900 cp->reqlen = htonl(sizeof(struct scsi_sense_data));
1901 cp->cp_statDMA = htonl(vtophys(&cp->cp_statDMA));
1902 cp->cp_reqDMA = htonl(vtophys(&cp->cp_reqDMA));
1903 cp->cp_viraddr = (u_int32_t) & ccb;
1905 cp->cp_msg[0] = HA_IDENTIFY_MSG | HA_DISCO_RECO;
1907 cp->cp_scsi_cmd = command;
1908 cp->cp_cdb[1] = (u_int8_t) (mode & SCSI_TM_MODE_MASK);
1909 cp->cp_lun = lun; /* Order is important here! */
1910 cp->cp_cdb[2] = 0x00; /* Buffer Id, only 1 :-( */
1911 cp->cp_cdb[3] = (length >> 16) & 0xFF; /* Buffer offset MSB */
1912 cp->cp_cdb[4] = (length >> 8) & 0xFF;
1913 cp->cp_cdb[5] = length & 0xFF;
1914 cp->cp_cdb[6] = (length >> 16) & 0xFF; /* Length MSB */
1915 cp->cp_cdb[7] = (length >> 8) & 0xFF;
1916 cp->cp_cdb[8] = length & 0xFF; /* Length LSB */
1917 cp->cp_cdb[9] = 0; /* No sync, no match bits */
1920 * This could be optimized to live in dpt_register_buffer.
1921 * We keep it here, just in case the kernel decides to reallocate pages
1923 if (dpt_scatter_gather(dpt, ccb, DPT_RW_BUFFER_SIZE,
1924 dpt->rw_buffer[bus][target][lun])) {
1925 printf("dpt%d: Failed to setup Scatter/Gather for "
1926 "Target-Mode buffer\n", dpt->unit);
1930 /* Setup a target mode READ command */
1933 dpt_set_target(int redo, dpt_softc_t * dpt,
1934 u_int8_t bus, u_int8_t target, u_int8_t lun, int mode,
1935 u_int16_t length, u_int16_t offset, dpt_ccb_t * ccb)
1937 if (dpt->target_mode_enabled) {
1941 dpt_target_ccb(dpt, bus, target, lun, ccb, mode,
1942 SCSI_TM_READ_BUFFER, length, offset);
1944 ccb->transaction_id = ++dpt->commands_processed;
1946 #ifdef DPT_MEASURE_PERFORMANCE
1947 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
1948 ccb->command_started = microtime_now;
1950 dpt_Qadd_waiting(dpt, ccb);
1951 dpt_sched_queue(dpt);
1955 printf("dpt%d: Target Mode Request, but Target Mode is OFF\n",
1961 * Schedule a buffer to be sent to another target.
1962 * The work will be scheduled and the callback provided will be called when
1963 * the work is actually done.
1965 * Please NOTE: ``Anyone'' can send a buffer, but only registered clients
1966 * get notified of receipt of buffers.
1970 dpt_send_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
1971 u_int8_t mode, u_int16_t length, u_int16_t offset, void *data,
1972 buff_wr_done callback)
1975 dpt_ccb_t *ccb = NULL;
1977 /* This is an external call. Be a bit paranoid */
1978 for (dpt = TAILQ_FIRST(&dpt_softc_list);
1980 dpt = TAILQ_NEXT(dpt, links)) {
1981 if (dpt->unit == unit)
1985 return (INVALID_UNIT);
1989 if (dpt->target_mode_enabled) {
1990 if ((channel >= dpt->channels) || (target > dpt->max_id) ||
1991 (lun > dpt->max_lun)) {
1992 return (INVALID_SENDER);
1994 if ((dpt->rw_buffer[channel][target][lun] == NULL) ||
1995 (dpt->buffer_receiver[channel][target][lun] == NULL))
1996 return (NOT_REGISTERED);
1999 /* Process the free list */
2000 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2001 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2002 " Please try later\n",
2005 return (NO_RESOURCES);
2007 /* Now grab the newest CCB */
2008 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2010 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2014 bcopy(dpt->rw_buffer[channel][target][lun] + offset, data, length);
2015 dpt_target_ccb(dpt, channel, target, lun, ccb, mode,
2016 SCSI_TM_WRITE_BUFFER,
2018 ccb->std_callback = (ccb_callback) callback; /* Potential trouble */
2021 ccb->transaction_id = ++dpt->commands_processed;
2023 #ifdef DPT_MEASURE_PERFORMANCE
2024 dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd]++;
2025 ccb->command_started = microtime_now;
2027 dpt_Qadd_waiting(dpt, ccb);
2028 dpt_sched_queue(dpt);
2033 return (DRIVER_DOWN);
2037 dpt_target_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2041 cp = &ccb->eata_ccb;
2044 * Remove the CCB from the waiting queue.
2045 * We do NOT put it back on the free, etc., queues as it is a special
2046 * ccb, owned by the dpt_softc of this unit.
2049 dpt_Qremove_completed(dpt, ccb);
2052 #define br_channel (ccb->eata_ccb.cp_channel)
2053 #define br_target (ccb->eata_ccb.cp_id)
2054 #define br_lun (ccb->eata_ccb.cp_LUN)
2055 #define br_index [br_channel][br_target][br_lun]
2056 #define read_buffer_callback (dpt->buffer_receiver br_index )
2057 #define read_buffer (dpt->rw_buffer[br_channel][br_target][br_lun])
2058 #define cb(offset) (ccb->eata_ccb.cp_cdb[offset])
2059 #define br_offset ((cb(3) << 16) | (cb(4) << 8) | cb(5))
2060 #define br_length ((cb(6) << 16) | (cb(7) << 8) | cb(8))
2062 /* Different reasons for being here, you know... */
2063 switch (ccb->eata_ccb.cp_scsi_cmd) {
2064 case SCSI_TM_READ_BUFFER:
2065 if (read_buffer_callback != NULL) {
2066 /* This is a buffer generated by a kernel process */
2067 read_buffer_callback(dpt->unit, br_channel,
2070 br_offset, br_length);
2073 * This is a buffer waited for by a user (sleeping)
2079 /* We ALWAYS re-issue the same command; args are don't-care */
2080 dpt_set_target(1, 0, 0, 0, 0, 0, 0, 0, 0);
2083 case SCSI_TM_WRITE_BUFFER:
2084 (ccb->wrbuff_callback) (dpt->unit, br_channel, br_target,
2085 br_offset, br_length,
2086 br_lun, ccb->status_packet.hba_stat);
2089 printf("dpt%d: %s is an unsupported command for target mode\n",
2090 dpt->unit, scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd));
2093 dpt->target_ccb[br_channel][br_target][br_lun] = NULL;
2094 dpt_Qpush_free(dpt, ccb);
2100 * Use this function to register a client for a buffer read target operation.
2101 * The function you register will be called every time a buffer is received
2102 * by the target mode code.
2105 dpt_register_buffer(int unit, u_int8_t channel, u_int8_t target, u_int8_t lun,
2106 u_int8_t mode, u_int16_t length, u_int16_t offset,
2107 dpt_rec_buff callback, dpt_rb_op_t op)
2110 dpt_ccb_t *ccb = NULL;
2112 for (dpt = TAILQ_FIRST(&dpt_softc_list);
2114 dpt = TAILQ_NEXT(dpt, links)) {
2115 if (dpt->unit == unit)
2119 return (INVALID_UNIT);
2123 if (dpt->state & DPT_HA_SHUTDOWN_ACTIVE)
2124 return (DRIVER_DOWN);
2126 if ((channel > (dpt->channels - 1)) || (target > (dpt->max_id - 1)) ||
2127 (lun > (dpt->max_lun - 1)))
2128 return (INVALID_SENDER);
2130 if (dpt->buffer_receiver[channel][target][lun] == NULL) {
2131 if (op == REGISTER_BUFFER) {
2132 /* Assign the requested callback */
2133 dpt->buffer_receiver[channel][target][lun] = callback;
2137 /* Process the free list */
2138 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2139 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2140 " Please try later\n",
2143 return (NO_RESOURCES);
2145 /* Now grab the newest CCB */
2146 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2148 panic("dpt%d: Got a NULL CCB from pop_free()\n",
2153 /* Clean up the leftover of the previous tenant */
2154 ccb->status = DPT_CCB_STATE_NEW;
2155 dpt->target_ccb[channel][target][lun] = ccb;
2157 dpt->rw_buffer[channel][target][lun] =
2158 kmalloc(DPT_RW_BUFFER_SIZE, M_DEVBUF, M_INTWAIT);
2159 dpt_set_target(0, dpt, channel, target, lun, mode,
2160 length, offset, ccb);
2161 return (SUCCESSFULLY_REGISTERED);
2163 return (NOT_REGISTERED);
2165 if (op == REGISTER_BUFFER) {
2166 if (dpt->buffer_receiver[channel][target][lun] == callback)
2167 return (ALREADY_REGISTERED);
2169 return (REGISTERED_TO_ANOTHER);
2171 if (dpt->buffer_receiver[channel][target][lun] == callback) {
2172 dpt->buffer_receiver[channel][target][lun] = NULL;
2174 dpt_Qpush_free(dpt, ccb);
2176 kfree(dpt->rw_buffer[channel][target][lun], M_DEVBUF);
2177 return (SUCCESSFULLY_REGISTERED);
2179 return (INVALID_CALLBACK);
2185 /* Return the state of the blinking DPT LED's */
2187 dpt_blinking_led(dpt_softc_t * dpt)
2198 for (ndx = 0, state = 0, previous = 0;
2199 (ndx < 10) && (state != previous);
2202 state = dpt_inl(dpt, 1);
2205 if ((state == previous) && (state == DPT_BLINK_INDICATOR))
2206 result = dpt_inb(dpt, 5);
2213 * Execute a command which did not come from the kernel's SCSI layer.
2214 * The only way to map user commands to bus and target is to comply with the
2215 * standard DPT wire-down scheme:
2218 dpt_user_cmd(dpt_softc_t * dpt, eata_pt_t * user_cmd,
2219 caddr_t cmdarg, int minor_no)
2223 int channel, target, lun;
2229 channel = minor2hba(minor_no);
2230 target = minor2target(minor_no);
2231 lun = minor2lun(minor_no);
2233 if ((channel > (dpt->channels - 1))
2234 || (target > dpt->max_id)
2235 || (lun > dpt->max_lun))
2238 if (target == dpt->sc_scsi_link[channel].adapter_targ) {
2239 /* This one is for the controller itself */
2240 if ((user_cmd->eataID[0] != 'E')
2241 || (user_cmd->eataID[1] != 'A')
2242 || (user_cmd->eataID[2] != 'T')
2243 || (user_cmd->eataID[3] != 'A')) {
2247 /* Get a DPT CCB, so we can prepare a command */
2250 /* Process the free list */
2251 if ((TAILQ_EMPTY(&dpt->free_ccbs)) && dpt_alloc_freelist(dpt)) {
2252 printf("dpt%d ERROR: Cannot allocate any more free CCB's.\n"
2253 " Please try later\n",
2258 /* Now grab the newest CCB */
2259 if ((ccb = dpt_Qpop_free(dpt)) == NULL) {
2261 panic("dpt%d: Got a NULL CCB from pop_free()\n", dpt->unit);
2264 /* Clean up the leftover of the previous tenant */
2265 ccb->status = DPT_CCB_STATE_NEW;
2268 bcopy((caddr_t) & user_cmd->command_packet, (caddr_t) & ccb->eata_ccb,
2269 sizeof(eata_ccb_t));
2271 /* We do not want to do user specified scatter/gather. Why?? */
2272 if (ccb->eata_ccb.scatter == 1)
2275 ccb->eata_ccb.Auto_Req_Sen = 1;
2276 ccb->eata_ccb.reqlen = htonl(sizeof(struct scsi_sense_data));
2277 ccb->eata_ccb.cp_datalen = htonl(sizeof(ccb->eata_ccb.cp_datalen));
2278 ccb->eata_ccb.cp_dataDMA = htonl(vtophys(ccb->eata_ccb.cp_dataDMA));
2279 ccb->eata_ccb.cp_statDMA = htonl(vtophys(&ccb->eata_ccb.cp_statDMA));
2280 ccb->eata_ccb.cp_reqDMA = htonl(vtophys(&ccb->eata_ccb.cp_reqDMA));
2281 ccb->eata_ccb.cp_viraddr = (u_int32_t) & ccb;
2283 if (ccb->eata_ccb.DataIn || ccb->eata_ccb.DataOut) {
2284 /* Data I/O is involved in this command. Alocate buffer */
2285 if (ccb->eata_ccb.cp_datalen > PAGE_SIZE) {
2286 data = contigmalloc(ccb->eata_ccb.cp_datalen,
2287 M_TEMP, M_WAITOK, 0, ~0,
2288 ccb->eata_ccb.cp_datalen,
2291 data = kmalloc(ccb->eata_ccb.cp_datalen, M_TEMP,
2296 printf("dpt%d: Cannot allocate %d bytes "
2297 "for EATA command\n", dpt->unit,
2298 ccb->eata_ccb.cp_datalen);
2301 #define usr_cmd_DMA (caddr_t)user_cmd->command_packet.cp_dataDMA
2302 if (ccb->eata_ccb.DataIn == 1) {
2303 if (copyin(usr_cmd_DMA,
2304 data, ccb->eata_ccb.cp_datalen) == -1)
2308 /* No data I/O involved here. Make sure the DPT knows that */
2309 ccb->eata_ccb.cp_datalen = 0;
2313 if (ccb->eata_ccb.FWNEST == 1)
2314 ccb->eata_ccb.FWNEST = 0;
2316 if (ccb->eata_ccb.cp_datalen != 0) {
2317 if (dpt_scatter_gather(dpt, ccb, ccb->eata_ccb.cp_datalen,
2320 kfree(data, M_TEMP);
2325 * We are required to quiet a SCSI bus.
2326 * since we do not queue comands on a bus basis,
2327 * we wait for ALL commands on a controller to complete.
2328 * In the mean time, sched_queue() will not schedule new commands.
2330 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2331 && (ccb->eata_ccb.cp_cdb[2] == BUS_QUIET)) {
2332 /* We wait for ALL traffic for this HBa to subside */
2334 dpt->state |= DPT_HA_QUIET;
2337 while ((submitted = dpt->submitted_ccbs_count) != 0) {
2338 huh = tsleep((void *) dpt, PCATCH, "dptqt", 100 * hz);
2341 /* Wakeup call received */
2352 /* Resume normal operation */
2353 if ((ccb->eata_ccb.cp_cdb[0] == MULTIFUNCTION_CMD)
2354 && (ccb->eata_ccb.cp_cdb[2] == BUS_UNQUIET)) {
2356 dpt->state &= ~DPT_HA_QUIET;
2360 * Schedule the command and submit it.
2361 * We bypass dpt_sched_queue, as it will block on DPT_HA_QUIET
2365 ccb->eata_ccb.Auto_Req_Sen = 1; /* We always want this feature */
2367 ccb->transaction_id = ++dpt->commands_processed;
2368 ccb->std_callback = (ccb_callback) dpt_user_cmd_done;
2369 ccb->result = (u_int32_t) & cmdarg;
2372 #ifdef DPT_MEASURE_PERFORMANCE
2373 ++dpt->performance.command_count[ccb->eata_ccb.cp_scsi_cmd];
2374 ccb->command_started = microtime_now;
2377 dpt_Qadd_waiting(dpt, ccb);
2380 dpt_sched_queue(dpt);
2382 /* Wait for the command to complete */
2383 (void) tsleep((void *) ccb, PCATCH, "dptucw", 100 * hz);
2385 /* Free allocated memory */
2387 kfree(data, M_TEMP);
2393 dpt_user_cmd_done(dpt_softc_t * dpt, int bus, dpt_ccb_t * ccb)
2401 * If Auto Request Sense is on, copyout the sense struct
2403 #define usr_pckt_DMA (caddr_t)(intptr_t)ntohl(ccb->eata_ccb.cp_reqDMA)
2404 #define usr_pckt_len ntohl(ccb->eata_ccb.cp_datalen)
2405 if (ccb->eata_ccb.Auto_Req_Sen == 1) {
2406 if (copyout((caddr_t) & ccb->sense_data, usr_pckt_DMA,
2407 sizeof(struct scsi_sense_data))) {
2408 ccb->result = EFAULT;
2409 dpt_Qpush_free(dpt, ccb);
2415 /* If DataIn is on, copyout the data */
2416 if ((ccb->eata_ccb.DataIn == 1)
2417 && (ccb->status_packet.hba_stat == HA_NO_ERROR)) {
2418 if (copyout(ccb->data, usr_pckt_DMA, usr_pckt_len)) {
2419 dpt_Qpush_free(dpt, ccb);
2420 ccb->result = EFAULT;
2427 /* Copyout the status */
2428 result = ccb->status_packet.hba_stat;
2429 cmd_arg = (caddr_t) ccb->result;
2431 if (copyout((caddr_t) & result, cmd_arg, sizeof(result))) {
2432 dpt_Qpush_free(dpt, ccb);
2433 ccb->result = EFAULT;
2438 /* Put the CCB back in the freelist */
2439 ccb->state |= DPT_CCB_STATE_COMPLETED;
2440 dpt_Qpush_free(dpt, ccb);
2442 /* Free allocated memory */
2447 #ifdef DPT_HANDLE_TIMEOUTS
2449 * This function walks down the SUBMITTED queue.
2450 * Every request that is too old gets aborted and marked.
2451 * Since the DPT will complete (interrupt) immediately (what does that mean?),
2452 * We just walk the list, aborting old commands and marking them as such.
2453 * The dpt_complete function will get rid of the that were interrupted in the
2456 * This function needs to run at splcam(), as it interacts with the submitted
2457 * queue, as well as the completed and free queues. Just like dpt_intr() does.
2458 * To run it at any ISPL other than that of dpt_intr(), will mean that dpt_intr
2459 * willbe able to pre-empt it, grab a transaction in progress (towards
2460 * destruction) and operate on it. The state of this transaction will be not
2462 * The only other option, is to lock it only as long as necessary but have
2463 * dpt_intr() spin-wait on it. In a UP environment this makes no sense and in
2464 * a SMP environment, the advantage is dubvious for a function that runs once
2465 * every ten seconds for few microseconds and, on systems with healthy
2466 * hardware, does not do anything anyway.
2470 dpt_handle_timeouts(dpt_softc_t * dpt)
2476 if (dpt->state & DPT_HA_TIMEOUTS_ACTIVE) {
2477 printf("dpt%d WARNING: Timeout Handling Collision\n",
2482 dpt->state |= DPT_HA_TIMEOUTS_ACTIVE;
2484 /* Loop through the entire submitted queue, looking for lost souls */
2485 for (ccb = TAILQ_FIRST(&dpt->submitted_ccbs);
2487 ccb = TAILQ_NEXT(ccb, links)) {
2488 struct scsi_xfer *xs;
2489 u_int32_t age, max_age;
2492 age = dpt_time_delta(ccb->command_started, microtime_now);
2494 #define TenSec 10000000
2496 if (xs == NULL) { /* Local, non-kernel call */
2499 max_age = (((xs->timeout * (dpt->submitted_ccbs_count
2500 + DPT_TIMEOUT_FACTOR))
2502 ? (xs->timeout * (dpt->submitted_ccbs_count
2503 + DPT_TIMEOUT_FACTOR))
2508 * If a transaction is marked lost and is TWICE as old as we
2509 * care, then, and only then do we destroy it!
2511 if (ccb->state & DPT_CCB_STATE_MARKED_LOST) {
2512 /* Remember who is next */
2513 if (age > (max_age * 2)) {
2514 dpt_Qremove_submitted(dpt, ccb);
2515 ccb->state &= ~DPT_CCB_STATE_MARKED_LOST;
2516 ccb->state |= DPT_CCB_STATE_ABORTED;
2517 #define cmd_name scsi_cmd_name(ccb->eata_ccb.cp_scsi_cmd)
2518 if (ccb->retries++ > DPT_RETRIES) {
2519 printf("dpt%d ERROR: Destroying stale "
2522 "c%db%dt%du%d (%d/%d)\n",
2523 dpt->unit, ccb->transaction_id,
2526 ccb->eata_ccb.cp_channel,
2527 ccb->eata_ccb.cp_id,
2528 ccb->eata_ccb.cp_LUN, age,
2530 #define send_ccb &ccb->eata_ccb
2531 #define ESA EATA_SPECIFIC_ABORT
2532 (void) dpt_send_immediate(dpt,
2536 dpt_Qpush_free(dpt, ccb);
2538 /* The SCSI layer should re-try */
2539 xs->error |= XS_TIMEOUT;
2540 xs->flags |= SCSI_ITSDONE;
2543 printf("dpt%d ERROR: Stale %d (%s) on "
2544 "c%db%dt%du%d (%d)\n"
2547 dpt->unit, ccb->transaction_id,
2550 ccb->eata_ccb.cp_channel,
2551 ccb->eata_ccb.cp_id,
2552 ccb->eata_ccb.cp_LUN,
2553 age, ccb->retries, DPT_RETRIES);
2555 dpt_Qpush_waiting(dpt, ccb);
2556 dpt_sched_queue(dpt);
2561 * This is a transaction that is not to be destroyed
2562 * (yet) But it is too old for our liking. We wait as
2563 * long as the upper layer thinks. Not really, we
2564 * multiply that by the number of commands in the
2565 * submitted queue + 1.
2567 if (!(ccb->state & DPT_CCB_STATE_MARKED_LOST) &&
2568 (age != ~0) && (age > max_age)) {
2569 printf("dpt%d ERROR: Marking %d (%s) on "
2571 " as late after %dusec\n",
2572 dpt->unit, ccb->transaction_id,
2574 dpt->unit, ccb->eata_ccb.cp_channel,
2575 ccb->eata_ccb.cp_id,
2576 ccb->eata_ccb.cp_LUN, age);
2577 ccb->state |= DPT_CCB_STATE_MARKED_LOST;
2582 dpt->state &= ~DPT_HA_TIMEOUTS_ACTIVE;
2586 #endif /* DPT_HANDLE_TIMEOUTS */