The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/iir/iir.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  *       Copyright (c) 2000-04 ICP vortex GmbH
    3  *       Copyright (c) 2002-04 Intel Corporation
    4  *       Copyright (c) 2003-04 Adaptec Inc.
    5  *       All Rights Reserved
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions, and the following disclaimer,
   12  *    without modification, immediately at the beginning of the file.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. The name of the author may not be used to endorse or promote products
   17  *    derived from this software without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * iir.c: SCSI dependant code for the Intel Integrated RAID Controller driver
   34  *
   35  * Written by: Achim Leubner <achim_leubner@adaptec.com>
   36  * Fixes/Additions: Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com>
   37  *
   38  * credits:     Niklas Hallqvist;       OpenBSD driver for the ICP Controllers.
   39  *              Mike Smith;             Some driver source code.
   40  *              FreeBSD.ORG;            Great O/S to work on and for.
   41  *
   42  * $Id: iir.c 1.5 2004/03/30 10:17:53 achim Exp $"
   43  */
   44 
   45 #include <sys/cdefs.h>
   46 __FBSDID("$FreeBSD: releng/8.0/sys/dev/iir/iir.c 193396 2009-06-03 20:25:13Z jhb $");
   47 
   48 #define _IIR_C_
   49 
   50 /* #include "opt_iir.h" */
   51 #include <sys/param.h>
   52 #include <sys/systm.h>
   53 #include <sys/endian.h>
   54 #include <sys/eventhandler.h>
   55 #include <sys/malloc.h>
   56 #include <sys/kernel.h>
   57 #include <sys/bus.h>
   58 
   59 #include <machine/bus.h>
   60 #include <machine/stdarg.h>
   61 
   62 #include <cam/cam.h>
   63 #include <cam/cam_ccb.h>
   64 #include <cam/cam_sim.h>
   65 #include <cam/cam_xpt_sim.h>
   66 #include <cam/cam_debug.h>
   67 #include <cam/scsi/scsi_all.h>
   68 #include <cam/scsi/scsi_message.h>
   69 
   70 #include <dev/iir/iir.h>
   71 
   72 MALLOC_DEFINE(M_GDTBUF, "iirbuf", "iir driver buffer");
   73 
   74 struct gdt_softc *gdt_wait_gdt;
   75 int     gdt_wait_index;
   76 
   77 #ifdef GDT_DEBUG
   78 int     gdt_debug = GDT_DEBUG;
   79 #ifdef __SERIAL__
   80 #define MAX_SERBUF 160
   81 static void ser_init(void);
   82 static void ser_puts(char *str);
   83 static void ser_putc(int c);
   84 static char strbuf[MAX_SERBUF+1];
   85 #ifdef __COM2__
   86 #define COM_BASE 0x2f8
   87 #else
   88 #define COM_BASE 0x3f8
   89 #endif
   90 static void ser_init()
   91 {
   92     unsigned port=COM_BASE;
   93 
   94     outb(port+3, 0x80);
   95     outb(port+1, 0);
   96     /* 19200 Baud, if 9600: outb(12,port) */
   97     outb(port, 6);
   98     outb(port+3, 3);
   99     outb(port+1, 0);
  100 }
  101 
  102 static void ser_puts(char *str)
  103 {
  104     char *ptr;
  105 
  106     ser_init();
  107     for (ptr=str;*ptr;++ptr)
  108         ser_putc((int)(*ptr));
  109 }
  110 
  111 static void ser_putc(int c)
  112 {
  113     unsigned port=COM_BASE;
  114 
  115     while ((inb(port+5) & 0x20)==0);
  116     outb(port, c);
  117     if (c==0x0a)
  118     {
  119         while ((inb(port+5) & 0x20)==0);
  120         outb(port, 0x0d);
  121     }
  122 }
  123 
  124 int ser_printf(const char *fmt, ...)
  125 {
  126     va_list args;
  127     int i;
  128 
  129     va_start(args,fmt);
  130     i = vsprintf(strbuf,fmt,args);
  131     ser_puts(strbuf);
  132     va_end(args);
  133     return i;
  134 }
  135 #endif
  136 #endif
  137 
  138 /* The linked list of softc structures */
  139 struct gdt_softc_list gdt_softcs = TAILQ_HEAD_INITIALIZER(gdt_softcs);
  140 /* controller cnt. */
  141 int gdt_cnt = 0;
  142 /* event buffer */
  143 static gdt_evt_str ebuffer[GDT_MAX_EVENTS];
  144 static int elastidx, eoldidx;
  145 /* statistics */
  146 gdt_statist_t gdt_stat;
  147 
  148 /* Definitions for our use of the SIM private CCB area */
  149 #define ccb_sim_ptr     spriv_ptr0
  150 #define ccb_priority    spriv_field1
  151 
  152 static void     iir_action(struct cam_sim *sim, union ccb *ccb);
  153 static void     iir_poll(struct cam_sim *sim);
  154 static void     iir_shutdown(void *arg, int howto);
  155 static void     iir_timeout(void *arg);
  156 static void     iir_watchdog(void *arg);
  157 
  158 static void     gdt_eval_mapping(u_int32_t size, int *cyls, int *heads, 
  159                                  int *secs);
  160 static int      gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb, 
  161                                  u_int8_t service, u_int16_t opcode, 
  162                                  u_int32_t arg1, u_int32_t arg2, u_int32_t arg3);
  163 static int      gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *ccb, 
  164                          int timeout);
  165 
  166 static struct gdt_ccb *gdt_get_ccb(struct gdt_softc *gdt);
  167 
  168 static int      gdt_sync_event(struct gdt_softc *gdt, int service, 
  169                                u_int8_t index, struct gdt_ccb *gccb);
  170 static int      gdt_async_event(struct gdt_softc *gdt, int service);
  171 static struct gdt_ccb *gdt_raw_cmd(struct gdt_softc *gdt, 
  172                                    union ccb *ccb, int *lock);
  173 static struct gdt_ccb *gdt_cache_cmd(struct gdt_softc *gdt, 
  174                                      union ccb *ccb, int *lock);
  175 static struct gdt_ccb *gdt_ioctl_cmd(struct gdt_softc *gdt, 
  176                                      gdt_ucmd_t *ucmd, int *lock);
  177 static void     gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb);
  178 
  179 static void     gdtmapmem(void *arg, bus_dma_segment_t *dm_segs,
  180                           int nseg, int error);
  181 static void     gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs,
  182                               int nseg, int error);
  183 
  184 int
  185 iir_init(struct gdt_softc *gdt)
  186 {
  187     u_int16_t cdev_cnt;
  188     int i, id, drv_cyls, drv_hds, drv_secs;
  189     struct gdt_ccb *gccb;
  190 
  191     GDT_DPRINTF(GDT_D_DEBUG, ("iir_init()\n"));
  192 
  193     gdt->sc_state = GDT_POLLING;
  194     gdt_clear_events(); 
  195     bzero(&gdt_stat, sizeof(gdt_statist_t));
  196 
  197     SLIST_INIT(&gdt->sc_free_gccb);
  198     SLIST_INIT(&gdt->sc_pending_gccb);
  199     TAILQ_INIT(&gdt->sc_ccb_queue);
  200     TAILQ_INIT(&gdt->sc_ucmd_queue);
  201     TAILQ_INSERT_TAIL(&gdt_softcs, gdt, links);
  202 
  203     /* DMA tag for mapping buffers into device visible space. */
  204     if (bus_dma_tag_create(gdt->sc_parent_dmat, /*alignment*/1, /*boundary*/0,
  205                            /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  206                            /*highaddr*/BUS_SPACE_MAXADDR,
  207                            /*filter*/NULL, /*filterarg*/NULL,
  208                            /*maxsize*/MAXBSIZE, /*nsegments*/GDT_MAXSG,
  209                            /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  210                            /*flags*/BUS_DMA_ALLOCNOW,
  211                            /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant,
  212                            &gdt->sc_buffer_dmat) != 0) {
  213         printf("iir%d: bus_dma_tag_create(...,gdt->sc_buffer_dmat) failed\n",
  214                gdt->sc_hanum);
  215         return (1);
  216     }
  217     gdt->sc_init_level++;
  218 
  219     /* DMA tag for our ccb structures */
  220     if (bus_dma_tag_create(gdt->sc_parent_dmat,
  221                            /*alignment*/1,
  222                            /*boundary*/0,
  223                            /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  224                            /*highaddr*/BUS_SPACE_MAXADDR,
  225                            /*filter*/NULL,
  226                            /*filterarg*/NULL,
  227                            GDT_MAXCMDS * GDT_SCRATCH_SZ, /* maxsize */
  228                            /*nsegments*/1,
  229                            /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  230                            /*flags*/0, /*lockfunc*/busdma_lock_mutex,
  231                            /*lockarg*/&Giant, &gdt->sc_gcscratch_dmat) != 0) {
  232         printf("iir%d: bus_dma_tag_create(...,gdt->sc_gcscratch_dmat) failed\n",
  233                gdt->sc_hanum);
  234         return (1);
  235     }
  236     gdt->sc_init_level++;
  237 
  238     /* Allocation for our ccb scratch area */
  239     if (bus_dmamem_alloc(gdt->sc_gcscratch_dmat, (void **)&gdt->sc_gcscratch,
  240                          BUS_DMA_NOWAIT, &gdt->sc_gcscratch_dmamap) != 0) {
  241         printf("iir%d: bus_dmamem_alloc(...,&gdt->sc_gccbs,...) failed\n",
  242                gdt->sc_hanum);
  243         return (1);
  244     }
  245     gdt->sc_init_level++;
  246 
  247     /* And permanently map them */
  248     bus_dmamap_load(gdt->sc_gcscratch_dmat, gdt->sc_gcscratch_dmamap,
  249                     gdt->sc_gcscratch, GDT_MAXCMDS * GDT_SCRATCH_SZ,
  250                     gdtmapmem, &gdt->sc_gcscratch_busbase, /*flags*/0);
  251     gdt->sc_init_level++;
  252 
  253     /* Clear them out. */
  254     bzero(gdt->sc_gcscratch, GDT_MAXCMDS * GDT_SCRATCH_SZ);
  255 
  256     /* Initialize the ccbs */
  257     gdt->sc_gccbs = malloc(sizeof(struct gdt_ccb) * GDT_MAXCMDS, M_GDTBUF,
  258         M_NOWAIT | M_ZERO);
  259     if (gdt->sc_gccbs == NULL) {
  260         printf("iir%d: no memory for gccbs.\n", gdt->sc_hanum);
  261         return (1);
  262     }
  263     for (i = GDT_MAXCMDS-1; i >= 0; i--) {
  264         gccb = &gdt->sc_gccbs[i];
  265         gccb->gc_cmd_index = i + 2;
  266         gccb->gc_flags = GDT_GCF_UNUSED;
  267         gccb->gc_map_flag = FALSE;
  268         if (bus_dmamap_create(gdt->sc_buffer_dmat, /*flags*/0,
  269                               &gccb->gc_dmamap) != 0)
  270             return(1);
  271         gccb->gc_map_flag = TRUE;
  272         gccb->gc_scratch = &gdt->sc_gcscratch[GDT_SCRATCH_SZ * i];
  273         gccb->gc_scratch_busbase = gdt->sc_gcscratch_busbase + GDT_SCRATCH_SZ * i;
  274         SLIST_INSERT_HEAD(&gdt->sc_free_gccb, gccb, sle);
  275     }
  276     gdt->sc_init_level++;
  277 
  278     /* create the control device */
  279     gdt->sc_dev = gdt_make_dev(gdt->sc_hanum);
  280 
  281     /* allocate ccb for gdt_internal_cmd() */
  282     gccb = gdt_get_ccb(gdt);
  283     if (gccb == NULL) {
  284         printf("iir%d: No free command index found\n",
  285                gdt->sc_hanum);
  286         return (1);
  287     }
  288     bzero(gccb->gc_cmd, GDT_CMD_SZ);
  289 
  290     if (!gdt_internal_cmd(gdt, gccb, GDT_SCREENSERVICE, GDT_INIT, 
  291                           0, 0, 0)) {
  292         printf("iir%d: Screen service initialization error %d\n",
  293                gdt->sc_hanum, gdt->sc_status);
  294         gdt_free_ccb(gdt, gccb);
  295         return (1);
  296     }
  297 
  298     gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_UNFREEZE_IO,
  299                      0, 0, 0);
  300 
  301     if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INIT, 
  302                           GDT_LINUX_OS, 0, 0)) {
  303         printf("iir%d: Cache service initialization error %d\n",
  304                gdt->sc_hanum, gdt->sc_status);
  305         gdt_free_ccb(gdt, gccb);
  306         return (1);
  307     }
  308     cdev_cnt = (u_int16_t)gdt->sc_info;
  309     gdt->sc_fw_vers = gdt->sc_service;
  310 
  311     /* Detect number of buses */
  312     gdt_enc32(gccb->gc_scratch + GDT_IOC_VERSION, GDT_IOC_NEWEST);
  313     gccb->gc_scratch[GDT_IOC_LIST_ENTRIES] = GDT_MAXBUS;
  314     gccb->gc_scratch[GDT_IOC_FIRST_CHAN] = 0;
  315     gccb->gc_scratch[GDT_IOC_LAST_CHAN] = GDT_MAXBUS - 1;
  316     gdt_enc32(gccb->gc_scratch + GDT_IOC_LIST_OFFSET, GDT_IOC_HDR_SZ);
  317     if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
  318                          GDT_IOCHAN_RAW_DESC, GDT_INVALID_CHANNEL,
  319                          GDT_IOC_HDR_SZ + GDT_MAXBUS * GDT_RAWIOC_SZ)) {
  320         gdt->sc_bus_cnt = gccb->gc_scratch[GDT_IOC_CHAN_COUNT];
  321         for (i = 0; i < gdt->sc_bus_cnt; i++) {
  322             id = gccb->gc_scratch[GDT_IOC_HDR_SZ +
  323                                  i * GDT_RAWIOC_SZ + GDT_RAWIOC_PROC_ID];
  324             gdt->sc_bus_id[i] = id < GDT_MAXID_FC ? id : 0xff;
  325         }
  326     } else {
  327         /* New method failed, use fallback. */
  328         for (i = 0; i < GDT_MAXBUS; i++) {
  329             gdt_enc32(gccb->gc_scratch + GDT_GETCH_CHANNEL_NO, i);
  330             if (!gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
  331                                   GDT_SCSI_CHAN_CNT | GDT_L_CTRL_PATTERN,
  332                                   GDT_IO_CHANNEL | GDT_INVALID_CHANNEL,
  333                                   GDT_GETCH_SZ)) {
  334                 if (i == 0) {
  335                     printf("iir%d: Cannot get channel count, "
  336                            "error %d\n", gdt->sc_hanum, gdt->sc_status);
  337                     gdt_free_ccb(gdt, gccb);
  338                     return (1);
  339                 }
  340                 break;
  341             }
  342             gdt->sc_bus_id[i] =
  343                 (gccb->gc_scratch[GDT_GETCH_SIOP_ID] < GDT_MAXID_FC) ?
  344                 gccb->gc_scratch[GDT_GETCH_SIOP_ID] : 0xff;
  345         }
  346         gdt->sc_bus_cnt = i;
  347     }
  348     /* add one "virtual" channel for the host drives */
  349     gdt->sc_virt_bus = gdt->sc_bus_cnt;
  350     gdt->sc_bus_cnt++;
  351 
  352     if (!gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_INIT, 
  353                           0, 0, 0)) {
  354             printf("iir%d: Raw service initialization error %d\n",
  355                    gdt->sc_hanum, gdt->sc_status);
  356             gdt_free_ccb(gdt, gccb);
  357             return (1);
  358     }
  359 
  360     /* Set/get features raw service (scatter/gather) */
  361     gdt->sc_raw_feat = 0;
  362     if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_SET_FEAT,
  363                          GDT_SCATTER_GATHER, 0, 0)) {
  364         if (gdt_internal_cmd(gdt, gccb, GDT_SCSIRAWSERVICE, GDT_GET_FEAT, 
  365                              0, 0, 0)) {
  366             gdt->sc_raw_feat = gdt->sc_info;
  367             if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
  368                 panic("iir%d: Scatter/Gather Raw Service "
  369                       "required but not supported!\n", gdt->sc_hanum);
  370                 gdt_free_ccb(gdt, gccb);
  371                 return (1);
  372             }
  373         }
  374     }
  375 
  376     /* Set/get features cache service (scatter/gather) */
  377     gdt->sc_cache_feat = 0;
  378     if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_SET_FEAT, 
  379                          0, GDT_SCATTER_GATHER, 0)) {
  380         if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_GET_FEAT, 
  381                              0, 0, 0)) {
  382             gdt->sc_cache_feat = gdt->sc_info;
  383             if (!(gdt->sc_info & GDT_SCATTER_GATHER)) {
  384                 panic("iir%d: Scatter/Gather Cache Service "
  385                   "required but not supported!\n", gdt->sc_hanum);
  386                 gdt_free_ccb(gdt, gccb);
  387                 return (1);
  388             }
  389         }
  390     }
  391 
  392     /* OEM */
  393     gdt_enc32(gccb->gc_scratch + GDT_OEM_VERSION, 0x01);
  394     gdt_enc32(gccb->gc_scratch + GDT_OEM_BUFSIZE, sizeof(gdt_oem_record_t));
  395     if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_IOCTL,
  396                          GDT_OEM_STR_RECORD, GDT_INVALID_CHANNEL,
  397                          sizeof(gdt_oem_str_record_t))) {
  398             strncpy(gdt->oem_name, ((gdt_oem_str_record_t *)
  399             gccb->gc_scratch)->text.scsi_host_drive_inquiry_vendor_id, 7);
  400                 gdt->oem_name[7]='\0';
  401         } else {
  402                 /* Old method, based on PCI ID */
  403                 if (gdt->sc_vendor == INTEL_VENDOR_ID)
  404             strcpy(gdt->oem_name,"Intel  ");
  405         else 
  406             strcpy(gdt->oem_name,"ICP    ");
  407     }
  408 
  409     /* Scan for cache devices */
  410     for (i = 0; i < cdev_cnt && i < GDT_MAX_HDRIVES; i++) {
  411         if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE, GDT_INFO, 
  412                              i, 0, 0)) {
  413             gdt->sc_hdr[i].hd_present = 1;
  414             gdt->sc_hdr[i].hd_size = gdt->sc_info;
  415             
  416             /*
  417              * Evaluate mapping (sectors per head, heads per cyl)
  418              */
  419             gdt->sc_hdr[i].hd_size &= ~GDT_SECS32;
  420             if (gdt->sc_info2 == 0)
  421                 gdt_eval_mapping(gdt->sc_hdr[i].hd_size,
  422                                  &drv_cyls, &drv_hds, &drv_secs);
  423             else {
  424                 drv_hds = gdt->sc_info2 & 0xff;
  425                 drv_secs = (gdt->sc_info2 >> 8) & 0xff;
  426                 drv_cyls = gdt->sc_hdr[i].hd_size / drv_hds /
  427                     drv_secs;
  428             }
  429             gdt->sc_hdr[i].hd_heads = drv_hds;
  430             gdt->sc_hdr[i].hd_secs = drv_secs;
  431             /* Round the size */
  432             gdt->sc_hdr[i].hd_size = drv_cyls * drv_hds * drv_secs;
  433             
  434             if (gdt_internal_cmd(gdt, gccb, GDT_CACHESERVICE,
  435                                  GDT_DEVTYPE, i, 0, 0))
  436                 gdt->sc_hdr[i].hd_devtype = gdt->sc_info;
  437         }
  438     }
  439     
  440     GDT_DPRINTF(GDT_D_INIT, ("dpmem %x %d-bus %d cache device%s\n", 
  441                              gdt->sc_dpmembase,
  442                              gdt->sc_bus_cnt, cdev_cnt, 
  443                              cdev_cnt == 1 ? "" : "s"));
  444     gdt_free_ccb(gdt, gccb);
  445 
  446     gdt_cnt++;
  447     return (0);
  448 }
  449 
  450 void
  451 iir_free(struct gdt_softc *gdt)
  452 {
  453     int i;
  454 
  455     GDT_DPRINTF(GDT_D_INIT, ("iir_free()\n"));
  456 
  457     switch (gdt->sc_init_level) {
  458       default:
  459         gdt_destroy_dev(gdt->sc_dev);
  460       case 5:
  461         for (i = GDT_MAXCMDS-1; i >= 0; i--) 
  462             if (gdt->sc_gccbs[i].gc_map_flag)
  463                 bus_dmamap_destroy(gdt->sc_buffer_dmat,
  464                                    gdt->sc_gccbs[i].gc_dmamap);
  465         bus_dmamap_unload(gdt->sc_gcscratch_dmat, gdt->sc_gcscratch_dmamap);
  466         free(gdt->sc_gccbs, M_GDTBUF);
  467       case 4:
  468         bus_dmamem_free(gdt->sc_gcscratch_dmat, gdt->sc_gcscratch, gdt->sc_gcscratch_dmamap);
  469       case 3:
  470         bus_dma_tag_destroy(gdt->sc_gcscratch_dmat);
  471       case 2:
  472         bus_dma_tag_destroy(gdt->sc_buffer_dmat);
  473       case 1:
  474         bus_dma_tag_destroy(gdt->sc_parent_dmat);
  475       case 0:
  476         break;
  477     }
  478     TAILQ_REMOVE(&gdt_softcs, gdt, links);
  479 }
  480 
  481 void
  482 iir_attach(struct gdt_softc *gdt)
  483 {
  484     struct cam_devq *devq;
  485     int i;
  486 
  487     GDT_DPRINTF(GDT_D_INIT, ("iir_attach()\n"));
  488 
  489     /*
  490      * Create the device queue for our SIM.
  491      * XXX Throttle this down since the card has problems under load.
  492      */
  493     devq = cam_simq_alloc(32);
  494     if (devq == NULL)
  495         return;
  496 
  497     for (i = 0; i < gdt->sc_bus_cnt; i++) {
  498         /*
  499          * Construct our SIM entry
  500          */
  501         gdt->sims[i] = cam_sim_alloc(iir_action, iir_poll, "iir",
  502                                      gdt, gdt->sc_hanum, &Giant,
  503                                      /*untagged*/1,
  504                                      /*tagged*/GDT_MAXCMDS, devq);
  505         if (xpt_bus_register(gdt->sims[i], gdt->sc_devnode, i) != CAM_SUCCESS) {
  506             cam_sim_free(gdt->sims[i], /*free_devq*/i == 0);
  507             break;
  508         }
  509 
  510         if (xpt_create_path(&gdt->paths[i], /*periph*/NULL,
  511                             cam_sim_path(gdt->sims[i]),
  512                             CAM_TARGET_WILDCARD,
  513                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  514             xpt_bus_deregister(cam_sim_path(gdt->sims[i]));
  515             cam_sim_free(gdt->sims[i], /*free_devq*/i == 0);
  516             break;
  517         }
  518     }
  519     if (i > 0)
  520         EVENTHANDLER_REGISTER(shutdown_final, iir_shutdown,
  521                               gdt, SHUTDOWN_PRI_DEFAULT);
  522     /* iir_watchdog(gdt); */
  523     gdt->sc_state = GDT_NORMAL;
  524 }
  525 
  526 static void
  527 gdt_eval_mapping(u_int32_t size, int *cyls, int *heads, int *secs)
  528 {
  529     *cyls = size / GDT_HEADS / GDT_SECS;
  530     if (*cyls < GDT_MAXCYLS) {
  531         *heads = GDT_HEADS;
  532         *secs = GDT_SECS;
  533     } else {
  534         /* Too high for 64 * 32 */
  535         *cyls = size / GDT_MEDHEADS / GDT_MEDSECS;
  536         if (*cyls < GDT_MAXCYLS) {
  537             *heads = GDT_MEDHEADS;
  538             *secs = GDT_MEDSECS;
  539         } else {
  540             /* Too high for 127 * 63 */
  541             *cyls = size / GDT_BIGHEADS / GDT_BIGSECS;
  542             *heads = GDT_BIGHEADS;
  543             *secs = GDT_BIGSECS;
  544         }
  545     }
  546 }
  547 
  548 static int
  549 gdt_wait(struct gdt_softc *gdt, struct gdt_ccb *gccb, 
  550          int timeout)
  551 {
  552     int rv = 0;
  553 
  554     GDT_DPRINTF(GDT_D_INIT,
  555                 ("gdt_wait(%p, %p, %d)\n", gdt, gccb, timeout));
  556 
  557     gdt->sc_state |= GDT_POLL_WAIT;
  558     do {
  559         iir_intr(gdt);
  560         if (gdt == gdt_wait_gdt &&
  561             gccb->gc_cmd_index == gdt_wait_index) {
  562             rv = 1;
  563             break;
  564         }
  565         DELAY(1);
  566     } while (--timeout);
  567     gdt->sc_state &= ~GDT_POLL_WAIT;
  568     
  569     while (gdt->sc_test_busy(gdt))
  570         DELAY(1);               /* XXX correct? */
  571 
  572     return (rv);
  573 }
  574 
  575 static int
  576 gdt_internal_cmd(struct gdt_softc *gdt, struct gdt_ccb *gccb,
  577                  u_int8_t service, u_int16_t opcode, 
  578                  u_int32_t arg1, u_int32_t arg2, u_int32_t arg3)
  579 {
  580     int retries;
  581     
  582     GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cmd(%p, %d, %d, %d, %d, %d)\n",
  583                             gdt, service, opcode, arg1, arg2, arg3));
  584 
  585     bzero(gccb->gc_cmd, GDT_CMD_SZ);
  586 
  587     for (retries = GDT_RETRIES; ; ) {
  588         gccb->gc_service = service;
  589         gccb->gc_flags = GDT_GCF_INTERNAL;
  590         
  591         gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
  592                   gccb->gc_cmd_index);
  593         gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, opcode);
  594 
  595         switch (service) {
  596           case GDT_CACHESERVICE:
  597             if (opcode == GDT_IOCTL) {
  598                 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
  599                           GDT_IOCTL_SUBFUNC, arg1);
  600                 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
  601                           GDT_IOCTL_CHANNEL, arg2);
  602                 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION +
  603                           GDT_IOCTL_PARAM_SIZE, (u_int16_t)arg3);
  604                 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
  605                           gccb->gc_scratch_busbase);
  606             } else {
  607                 gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION +
  608                           GDT_CACHE_DEVICENO, (u_int16_t)arg1);
  609                 gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
  610                           GDT_CACHE_BLOCKNO, arg2);
  611             }
  612             break;
  613 
  614           case GDT_SCSIRAWSERVICE:
  615             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION +
  616                       GDT_RAW_DIRECTION, arg1);
  617             gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] =
  618                 (u_int8_t)arg2;
  619             gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] =
  620                 (u_int8_t)arg3;
  621             gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] =
  622                 (u_int8_t)(arg3 >> 8);
  623         }
  624 
  625         gdt->sc_set_sema0(gdt);
  626         gccb->gc_cmd_len = GDT_CMD_SZ;
  627         gdt->sc_cmd_off = 0;
  628         gdt->sc_cmd_cnt = 0;
  629         gdt->sc_copy_cmd(gdt, gccb);
  630         gdt->sc_release_event(gdt);
  631         DELAY(20);
  632         if (!gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT))
  633             return (0);
  634         if (gdt->sc_status != GDT_S_BSY || --retries == 0)
  635             break;
  636         DELAY(1);
  637     }
  638     return (gdt->sc_status == GDT_S_OK);
  639 }
  640 
  641 static struct gdt_ccb *
  642 gdt_get_ccb(struct gdt_softc *gdt)
  643 {
  644     struct gdt_ccb *gccb;
  645     int lock;
  646     
  647     GDT_DPRINTF(GDT_D_QUEUE, ("gdt_get_ccb(%p)\n", gdt));
  648 
  649     lock = splcam();
  650     gccb = SLIST_FIRST(&gdt->sc_free_gccb);
  651     if (gccb != NULL) {
  652         SLIST_REMOVE_HEAD(&gdt->sc_free_gccb, sle);
  653         SLIST_INSERT_HEAD(&gdt->sc_pending_gccb, gccb, sle);
  654         ++gdt_stat.cmd_index_act;
  655         if (gdt_stat.cmd_index_act > gdt_stat.cmd_index_max)
  656             gdt_stat.cmd_index_max = gdt_stat.cmd_index_act;
  657     }
  658     splx(lock);
  659     return (gccb);
  660 }
  661 
  662 void
  663 gdt_free_ccb(struct gdt_softc *gdt, struct gdt_ccb *gccb)
  664 {
  665     int lock;
  666 
  667     GDT_DPRINTF(GDT_D_QUEUE, ("gdt_free_ccb(%p, %p)\n", gdt, gccb));
  668     
  669     lock = splcam();
  670     gccb->gc_flags = GDT_GCF_UNUSED;
  671     SLIST_REMOVE(&gdt->sc_pending_gccb, gccb, gdt_ccb, sle);
  672     SLIST_INSERT_HEAD(&gdt->sc_free_gccb, gccb, sle);
  673     --gdt_stat.cmd_index_act;
  674     splx(lock);
  675     if (gdt->sc_state & GDT_SHUTDOWN)
  676         wakeup(gccb);
  677 }
  678 
  679 void    
  680 gdt_next(struct gdt_softc *gdt)
  681 {
  682     int lock;
  683     union ccb *ccb;
  684     gdt_ucmd_t *ucmd;
  685     struct cam_sim *sim;
  686     int bus, target, lun;
  687     int next_cmd;
  688 
  689     struct ccb_scsiio *csio;
  690     struct ccb_hdr *ccbh;
  691     struct gdt_ccb *gccb = NULL;
  692     u_int8_t cmd;
  693 
  694     GDT_DPRINTF(GDT_D_QUEUE, ("gdt_next(%p)\n", gdt));
  695 
  696     lock = splcam();
  697     if (gdt->sc_test_busy(gdt)) {
  698         if (!(gdt->sc_state & GDT_POLLING)) {
  699             splx(lock);
  700             return;
  701         }
  702         while (gdt->sc_test_busy(gdt))
  703             DELAY(1);
  704     }
  705 
  706     gdt->sc_cmd_cnt = gdt->sc_cmd_off = 0;
  707     next_cmd = TRUE;
  708     for (;;) {
  709         /* I/Os in queue? controller ready? */
  710         if (!TAILQ_FIRST(&gdt->sc_ucmd_queue) &&
  711             !TAILQ_FIRST(&gdt->sc_ccb_queue))
  712             break;
  713 
  714         /* 1.: I/Os without ccb (IOCTLs) */
  715         ucmd = TAILQ_FIRST(&gdt->sc_ucmd_queue);
  716         if (ucmd != NULL) {
  717             TAILQ_REMOVE(&gdt->sc_ucmd_queue, ucmd, links);
  718             if ((gccb = gdt_ioctl_cmd(gdt, ucmd, &lock)) == NULL) {
  719                 TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
  720                 break;
  721             }
  722             break;      
  723             /* wenn mehrere Kdos. zulassen: if (!gdt_polling) continue; */
  724         }
  725 
  726         /* 2.: I/Os with ccb */
  727         ccb = (union ccb *)TAILQ_FIRST(&gdt->sc_ccb_queue); 
  728         /* ist dann immer != NULL, da oben getestet */
  729         sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
  730         bus = cam_sim_bus(sim);
  731         target = ccb->ccb_h.target_id;
  732         lun = ccb->ccb_h.target_lun;
  733     
  734         TAILQ_REMOVE(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
  735         --gdt_stat.req_queue_act;
  736         /* ccb->ccb_h.func_code is XPT_SCSI_IO */
  737         GDT_DPRINTF(GDT_D_QUEUE, ("XPT_SCSI_IO flags 0x%x)\n", 
  738                                   ccb->ccb_h.flags));
  739         csio = &ccb->csio;
  740         ccbh = &ccb->ccb_h;
  741         cmd  = csio->cdb_io.cdb_bytes[0];
  742         /* Max CDB length is 12 bytes */
  743         if (csio->cdb_len > 12) { 
  744             ccbh->status = CAM_REQ_INVALID;
  745             --gdt_stat.io_count_act;
  746             xpt_done(ccb);
  747         } else if (bus != gdt->sc_virt_bus) {
  748             /* raw service command */
  749             if ((gccb = gdt_raw_cmd(gdt, ccb, &lock)) == NULL) {
  750                 TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h, 
  751                                   sim_links.tqe);
  752                 ++gdt_stat.req_queue_act;
  753                 if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
  754                     gdt_stat.req_queue_max = gdt_stat.req_queue_act;
  755                 next_cmd = FALSE;
  756             }
  757         } else if (target >= GDT_MAX_HDRIVES || 
  758                    !gdt->sc_hdr[target].hd_present || lun != 0) {
  759             ccbh->status = CAM_DEV_NOT_THERE;
  760             --gdt_stat.io_count_act;
  761             xpt_done(ccb);
  762         } else {
  763             /* cache service command */
  764             if (cmd == READ_6  || cmd == WRITE_6 ||
  765                 cmd == READ_10 || cmd == WRITE_10) {
  766                 if ((gccb = gdt_cache_cmd(gdt, ccb, &lock)) == NULL) {
  767                     TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h, 
  768                                       sim_links.tqe);
  769                     ++gdt_stat.req_queue_act;
  770                     if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
  771                         gdt_stat.req_queue_max = gdt_stat.req_queue_act;
  772                     next_cmd = FALSE;
  773                 }
  774             } else {
  775                 splx(lock);
  776                 gdt_internal_cache_cmd(gdt, ccb);
  777                 lock = splcam();
  778             }
  779         }           
  780         if ((gdt->sc_state & GDT_POLLING) || !next_cmd)
  781             break;
  782     }
  783     if (gdt->sc_cmd_cnt > 0)
  784         gdt->sc_release_event(gdt);
  785 
  786     splx(lock);
  787 
  788     if ((gdt->sc_state & GDT_POLLING) && gdt->sc_cmd_cnt > 0) {
  789         gdt_wait(gdt, gccb, GDT_POLL_TIMEOUT);
  790     }
  791 }
  792 
  793 static struct gdt_ccb *
  794 gdt_raw_cmd(struct gdt_softc *gdt, union ccb *ccb, int *lock)
  795 {
  796     struct gdt_ccb *gccb;
  797     struct cam_sim *sim;
  798 
  799     GDT_DPRINTF(GDT_D_CMD, ("gdt_raw_cmd(%p, %p)\n", gdt, ccb));
  800 
  801     if (roundup(GDT_CMD_UNION + GDT_RAW_SZ, sizeof(u_int32_t)) +
  802         gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
  803         gdt->sc_ic_all_size) {
  804         GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_raw_cmd(): DPMEM overflow\n", 
  805                                     gdt->sc_hanum));
  806         return (NULL);
  807     }
  808 
  809     gccb = gdt_get_ccb(gdt);
  810     if (gccb == NULL) {
  811         GDT_DPRINTF(GDT_D_INVALID, ("iir%d: No free command index found\n",
  812                                     gdt->sc_hanum));
  813         return (gccb);
  814     }
  815     bzero(gccb->gc_cmd, GDT_CMD_SZ);
  816     sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
  817     gccb->gc_ccb = ccb;
  818     gccb->gc_service = GDT_SCSIRAWSERVICE;
  819     gccb->gc_flags = GDT_GCF_SCSI;
  820         
  821     if (gdt->sc_cmd_cnt == 0)
  822         gdt->sc_set_sema0(gdt);
  823     splx(*lock);
  824     gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
  825               gccb->gc_cmd_index);
  826     gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
  827 
  828     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
  829               (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
  830               GDT_DATA_IN : GDT_DATA_OUT);
  831     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
  832               ccb->csio.dxfer_len);
  833     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
  834               ccb->csio.cdb_len);
  835     bcopy(ccb->csio.cdb_io.cdb_bytes, gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
  836           ccb->csio.cdb_len);
  837     gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] = 
  838         ccb->ccb_h.target_id;
  839     gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] = 
  840         ccb->ccb_h.target_lun;
  841     gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] = 
  842         cam_sim_bus(sim);
  843     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
  844               sizeof(struct scsi_sense_data));
  845     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
  846               gccb->gc_scratch_busbase);
  847  
  848     /*
  849      * If we have any data to send with this command,
  850      * map it into bus space.
  851      */
  852     /* Only use S/G if there is a transfer */
  853     if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  854         if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { 
  855             if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
  856                 int s;
  857                 int error;
  858             
  859                 /* vorher unlock von splcam() ??? */
  860                 s = splsoftvm();
  861                 error =
  862                     bus_dmamap_load(gdt->sc_buffer_dmat,
  863                                     gccb->gc_dmamap,
  864                                     ccb->csio.data_ptr,
  865                                     ccb->csio.dxfer_len,
  866                                     gdtexecuteccb,
  867                                     gccb, /*flags*/0);
  868                 if (error == EINPROGRESS) {
  869                     xpt_freeze_simq(sim, 1);
  870                     gccb->gc_ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
  871                 }
  872                 splx(s);
  873             } else {
  874                 panic("iir: CAM_DATA_PHYS not supported");
  875             }
  876         } else {
  877             struct bus_dma_segment *segs;
  878 
  879             if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
  880                 panic("iir%d: iir_action - Physical "
  881                       "segment pointers unsupported", gdt->sc_hanum);
  882 
  883             if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
  884                 panic("iir%d: iir_action - Virtual "
  885                       "segment addresses unsupported", gdt->sc_hanum);
  886 
  887             /* Just use the segments provided */
  888             segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
  889             gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
  890         }
  891     } else {
  892         gdtexecuteccb(gccb, NULL, 0, 0);
  893     }
  894 
  895     *lock = splcam();
  896     return (gccb);
  897 }
  898 
  899 static struct gdt_ccb *
  900 gdt_cache_cmd(struct gdt_softc *gdt, union ccb *ccb, int *lock)
  901 {
  902     struct gdt_ccb *gccb;
  903     struct cam_sim *sim;
  904     u_int8_t *cmdp;
  905     u_int16_t opcode;
  906     u_int32_t blockno, blockcnt;
  907 
  908     GDT_DPRINTF(GDT_D_CMD, ("gdt_cache_cmd(%p, %p)\n", gdt, ccb));
  909 
  910     if (roundup(GDT_CMD_UNION + GDT_CACHE_SZ, sizeof(u_int32_t)) +
  911         gdt->sc_cmd_off + GDT_DPMEM_COMMAND_OFFSET >
  912         gdt->sc_ic_all_size) {
  913         GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_cache_cmd(): DPMEM overflow\n", 
  914                                     gdt->sc_hanum));
  915         return (NULL);
  916     }
  917 
  918     gccb = gdt_get_ccb(gdt);
  919     if (gccb == NULL) {
  920         GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
  921                                   gdt->sc_hanum));
  922         return (gccb);
  923     }
  924     bzero(gccb->gc_cmd, GDT_CMD_SZ);
  925     sim = (struct cam_sim *)ccb->ccb_h.ccb_sim_ptr;
  926     gccb->gc_ccb = ccb;
  927     gccb->gc_service = GDT_CACHESERVICE;
  928     gccb->gc_flags = GDT_GCF_SCSI;
  929         
  930     if (gdt->sc_cmd_cnt == 0)
  931         gdt->sc_set_sema0(gdt);
  932     splx(*lock);
  933     gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
  934               gccb->gc_cmd_index);
  935     cmdp = ccb->csio.cdb_io.cdb_bytes;
  936     opcode = (*cmdp == WRITE_6 || *cmdp == WRITE_10) ? GDT_WRITE : GDT_READ;
  937     if ((gdt->sc_state & GDT_SHUTDOWN) && opcode == GDT_WRITE)
  938         opcode = GDT_WRITE_THR;
  939     gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, opcode);
  940  
  941     gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
  942               ccb->ccb_h.target_id);
  943     if (ccb->csio.cdb_len == 6) {
  944         struct scsi_rw_6 *rw = (struct scsi_rw_6 *)cmdp;
  945         blockno = scsi_3btoul(rw->addr) & ((SRW_TOPADDR<<16) | 0xffff);
  946         blockcnt = rw->length ? rw->length : 0x100;
  947     } else {
  948         struct scsi_rw_10 *rw = (struct scsi_rw_10 *)cmdp;
  949         blockno = scsi_4btoul(rw->addr);
  950         blockcnt = scsi_2btoul(rw->length);
  951     }
  952     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
  953               blockno);
  954     gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
  955               blockcnt);
  956 
  957     /*
  958      * If we have any data to send with this command,
  959      * map it into bus space.
  960      */
  961     /* Only use S/G if there is a transfer */
  962     if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) { 
  963         if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
  964             int s;
  965             int error;
  966             
  967             /* vorher unlock von splcam() ??? */
  968             s = splsoftvm();
  969             error =
  970                 bus_dmamap_load(gdt->sc_buffer_dmat,
  971                                 gccb->gc_dmamap,
  972                                 ccb->csio.data_ptr,
  973                                 ccb->csio.dxfer_len,
  974                                 gdtexecuteccb,
  975                                 gccb, /*flags*/0);
  976             if (error == EINPROGRESS) {
  977                 xpt_freeze_simq(sim, 1);
  978                 gccb->gc_ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
  979             }
  980             splx(s);
  981         } else {
  982             panic("iir: CAM_DATA_PHYS not supported");
  983         }
  984     } else {
  985         struct bus_dma_segment *segs;
  986 
  987         if ((ccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
  988             panic("iir%d: iir_action - Physical "
  989                   "segment pointers unsupported", gdt->sc_hanum);
  990 
  991         if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS)==0)
  992             panic("iir%d: iir_action - Virtual "
  993                   "segment addresses unsupported", gdt->sc_hanum);
  994 
  995         /* Just use the segments provided */
  996         segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
  997         gdtexecuteccb(gccb, segs, ccb->csio.sglist_cnt, 0);
  998     }
  999 
 1000     *lock = splcam();
 1001     return (gccb);
 1002 }
 1003 
 1004 static struct gdt_ccb *
 1005 gdt_ioctl_cmd(struct gdt_softc *gdt, gdt_ucmd_t *ucmd, int *lock)
 1006 {
 1007     struct gdt_ccb *gccb;
 1008     u_int32_t cnt;
 1009 
 1010     GDT_DPRINTF(GDT_D_DEBUG, ("gdt_ioctl_cmd(%p, %p)\n", gdt, ucmd));
 1011 
 1012     gccb = gdt_get_ccb(gdt);
 1013     if (gccb == NULL) {
 1014         GDT_DPRINTF(GDT_D_DEBUG, ("iir%d: No free command index found\n",
 1015                                   gdt->sc_hanum));
 1016         return (gccb);
 1017     }
 1018     bzero(gccb->gc_cmd, GDT_CMD_SZ);
 1019     gccb->gc_ucmd = ucmd;
 1020     gccb->gc_service = ucmd->service;
 1021     gccb->gc_flags = GDT_GCF_IOCTL;
 1022         
 1023     /* check DPMEM space, copy data buffer from user space */
 1024     if (ucmd->service == GDT_CACHESERVICE) {
 1025         if (ucmd->OpCode == GDT_IOCTL) {
 1026             gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_IOCTL_SZ,
 1027                                       sizeof(u_int32_t));
 1028             cnt = ucmd->u.ioctl.param_size;
 1029             if (cnt > GDT_SCRATCH_SZ) {
 1030                 printf("iir%d: Scratch buffer too small (%d/%d)\n", 
 1031                        gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
 1032                 gdt_free_ccb(gdt, gccb);
 1033                 return (NULL);
 1034             }
 1035         } else {
 1036             gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
 1037                                       GDT_SG_SZ, sizeof(u_int32_t));
 1038             cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
 1039             if (cnt > GDT_SCRATCH_SZ) {
 1040                 printf("iir%d: Scratch buffer too small (%d/%d)\n", 
 1041                        gdt->sc_hanum, GDT_SCRATCH_SZ, cnt);
 1042                 gdt_free_ccb(gdt, gccb);
 1043                 return (NULL);
 1044             }
 1045         }
 1046     } else {
 1047         gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
 1048                                   GDT_SG_SZ, sizeof(u_int32_t));
 1049         cnt = ucmd->u.raw.sdlen;
 1050         if (cnt + ucmd->u.raw.sense_len > GDT_SCRATCH_SZ) {
 1051             printf("iir%d: Scratch buffer too small (%d/%d)\n", 
 1052                    gdt->sc_hanum, GDT_SCRATCH_SZ, cnt + ucmd->u.raw.sense_len);
 1053             gdt_free_ccb(gdt, gccb);
 1054             return (NULL);
 1055         }
 1056     }
 1057     if (cnt != 0) 
 1058         bcopy(ucmd->data, gccb->gc_scratch, cnt);
 1059 
 1060     if (gdt->sc_cmd_off + gccb->gc_cmd_len + GDT_DPMEM_COMMAND_OFFSET >
 1061         gdt->sc_ic_all_size) {
 1062         GDT_DPRINTF(GDT_D_INVALID, ("iir%d: gdt_ioctl_cmd(): DPMEM overflow\n", 
 1063                                     gdt->sc_hanum));
 1064         gdt_free_ccb(gdt, gccb);
 1065         return (NULL);
 1066     }
 1067 
 1068     if (gdt->sc_cmd_cnt == 0)
 1069         gdt->sc_set_sema0(gdt);
 1070     splx(*lock);
 1071 
 1072     /* fill cmd structure */
 1073     gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
 1074               gccb->gc_cmd_index);
 1075     gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, 
 1076               ucmd->OpCode);
 1077 
 1078     if (ucmd->service == GDT_CACHESERVICE) {
 1079         if (ucmd->OpCode == GDT_IOCTL) {
 1080             /* IOCTL */
 1081             gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_PARAM_SIZE,
 1082                       ucmd->u.ioctl.param_size);
 1083             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_SUBFUNC,
 1084                       ucmd->u.ioctl.subfunc);
 1085             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_CHANNEL,
 1086                       ucmd->u.ioctl.channel);
 1087             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_IOCTL_P_PARAM,
 1088                       gccb->gc_scratch_busbase);
 1089         } else {
 1090             /* cache service command */
 1091             gdt_enc16(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DEVICENO,
 1092                       ucmd->u.cache.DeviceNo);
 1093             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKNO,
 1094                       ucmd->u.cache.BlockNo);
 1095             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_BLOCKCNT,
 1096                       ucmd->u.cache.BlockCnt);
 1097             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR,
 1098                       0xffffffffUL);
 1099             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,
 1100                       1);
 1101             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST + 
 1102                       GDT_SG_PTR, gccb->gc_scratch_busbase);
 1103             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
 1104                       GDT_SG_LEN, ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE);
 1105         }
 1106     } else {
 1107         /* raw service command */
 1108         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_DIRECTION,
 1109                   ucmd->u.raw.direction);
 1110         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA,
 1111                   0xffffffffUL);
 1112         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDLEN,
 1113                   ucmd->u.raw.sdlen);
 1114         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CLEN,
 1115                   ucmd->u.raw.clen);
 1116         bcopy(ucmd->u.raw.cmd, gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_CMD,
 1117               12);
 1118         gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_TARGET] = 
 1119             ucmd->u.raw.target;
 1120         gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_LUN] = 
 1121             ucmd->u.raw.lun;
 1122         gccb->gc_cmd[GDT_CMD_UNION + GDT_RAW_BUS] = 
 1123             ucmd->u.raw.bus;
 1124         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_LEN,
 1125                   ucmd->u.raw.sense_len);
 1126         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SENSE_DATA,
 1127                   gccb->gc_scratch_busbase + ucmd->u.raw.sdlen);
 1128         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,
 1129                   1);
 1130         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST + 
 1131                   GDT_SG_PTR, gccb->gc_scratch_busbase);
 1132         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
 1133                   GDT_SG_LEN, ucmd->u.raw.sdlen);
 1134     }
 1135 
 1136     *lock = splcam();
 1137     gdt_stat.sg_count_act = 1;
 1138     gdt->sc_copy_cmd(gdt, gccb);
 1139     return (gccb);
 1140 }
 1141 
 1142 static void 
 1143 gdt_internal_cache_cmd(struct gdt_softc *gdt,union ccb *ccb)
 1144 {
 1145     int t;
 1146 
 1147     t = ccb->ccb_h.target_id;
 1148     GDT_DPRINTF(GDT_D_CMD, ("gdt_internal_cache_cmd(%p, %p, 0x%x, %d)\n", 
 1149         gdt, ccb, ccb->csio.cdb_io.cdb_bytes[0], t));
 1150 
 1151     switch (ccb->csio.cdb_io.cdb_bytes[0]) {
 1152       case TEST_UNIT_READY:
 1153       case START_STOP:
 1154         break;
 1155       case REQUEST_SENSE:
 1156         GDT_DPRINTF(GDT_D_MISC, ("REQUEST_SENSE\n"));
 1157         break;
 1158       case INQUIRY:
 1159         {
 1160             struct scsi_inquiry_data inq;
 1161             size_t copylen = MIN(sizeof(inq), ccb->csio.dxfer_len);
 1162 
 1163             bzero(&inq, sizeof(inq));
 1164             inq.device = (gdt->sc_hdr[t].hd_devtype & 4) ?
 1165                 T_CDROM : T_DIRECT;
 1166             inq.dev_qual2 = (gdt->sc_hdr[t].hd_devtype & 1) ? 0x80 : 0;
 1167             inq.version = SCSI_REV_2;
 1168             inq.response_format = 2; 
 1169             inq.additional_length = 32; 
 1170             inq.flags = SID_CmdQue | SID_Sync; 
 1171             strncpy(inq.vendor, gdt->oem_name, sizeof(inq.vendor));
 1172             snprintf(inq.product, sizeof(inq.product),
 1173                      "Host Drive   #%02d", t);
 1174             strncpy(inq.revision, "   ", sizeof(inq.revision));
 1175             bcopy(&inq, ccb->csio.data_ptr, copylen );
 1176             if( ccb->csio.dxfer_len > copylen )
 1177                 bzero( ccb->csio.data_ptr+copylen,
 1178                        ccb->csio.dxfer_len - copylen );
 1179             break;
 1180         }
 1181       case MODE_SENSE_6:
 1182         {
 1183             struct mpd_data {
 1184                 struct scsi_mode_hdr_6 hd;
 1185                 struct scsi_mode_block_descr bd;
 1186                 struct scsi_control_page cp;
 1187             } mpd;
 1188             size_t copylen = MIN(sizeof(mpd), ccb->csio.dxfer_len);
 1189             u_int8_t page;
 1190 
 1191             /*mpd = (struct mpd_data *)ccb->csio.data_ptr;*/
 1192             bzero(&mpd, sizeof(mpd));
 1193             mpd.hd.datalen = sizeof(struct scsi_mode_hdr_6) +
 1194                 sizeof(struct scsi_mode_block_descr);
 1195             mpd.hd.dev_specific = (gdt->sc_hdr[t].hd_devtype & 2) ? 0x80 : 0;
 1196             mpd.hd.block_descr_len = sizeof(struct scsi_mode_block_descr);
 1197             mpd.bd.block_len[0] = (GDT_SECTOR_SIZE & 0x00ff0000) >> 16;
 1198             mpd.bd.block_len[1] = (GDT_SECTOR_SIZE & 0x0000ff00) >> 8;
 1199             mpd.bd.block_len[2] = (GDT_SECTOR_SIZE & 0x000000ff);
 1200 
 1201             bcopy(&mpd, ccb->csio.data_ptr, copylen );
 1202             if( ccb->csio.dxfer_len > copylen )
 1203                 bzero( ccb->csio.data_ptr+copylen,
 1204                        ccb->csio.dxfer_len - copylen );
 1205             page=((struct scsi_mode_sense_6 *)ccb->csio.cdb_io.cdb_bytes)->page;
 1206             switch (page) {
 1207               default:
 1208                 GDT_DPRINTF(GDT_D_MISC, ("MODE_SENSE_6: page 0x%x\n", page));
 1209                 break;
 1210             }
 1211             break;
 1212         }
 1213       case READ_CAPACITY:
 1214         {
 1215             struct scsi_read_capacity_data rcd;
 1216             size_t copylen = MIN(sizeof(rcd), ccb->csio.dxfer_len);
 1217               
 1218             /*rcd = (struct scsi_read_capacity_data *)ccb->csio.data_ptr;*/
 1219             bzero(&rcd, sizeof(rcd));
 1220             scsi_ulto4b(gdt->sc_hdr[t].hd_size - 1, rcd.addr);
 1221             scsi_ulto4b(GDT_SECTOR_SIZE, rcd.length);
 1222             bcopy(&rcd, ccb->csio.data_ptr, copylen );
 1223             if( ccb->csio.dxfer_len > copylen )
 1224                 bzero( ccb->csio.data_ptr+copylen,
 1225                        ccb->csio.dxfer_len - copylen );
 1226             break;
 1227         }
 1228       default:
 1229         GDT_DPRINTF(GDT_D_MISC, ("gdt_internal_cache_cmd(%d) unknown\n", 
 1230                                     ccb->csio.cdb_io.cdb_bytes[0]));
 1231         break;
 1232     }
 1233     ccb->ccb_h.status |= CAM_REQ_CMP;
 1234     --gdt_stat.io_count_act;
 1235     xpt_done(ccb);
 1236 }
 1237 
 1238 static void     
 1239 gdtmapmem(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
 1240 {
 1241     bus_addr_t *busaddrp;
 1242     
 1243     busaddrp = (bus_addr_t *)arg;
 1244     *busaddrp = dm_segs->ds_addr;
 1245 }
 1246 
 1247 static void     
 1248 gdtexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
 1249 {
 1250     struct gdt_ccb *gccb;
 1251     union ccb *ccb;
 1252     struct gdt_softc *gdt;
 1253     int i, lock;
 1254 
 1255     lock = splcam();
 1256 
 1257     gccb = (struct gdt_ccb *)arg;
 1258     ccb = gccb->gc_ccb;
 1259     gdt = cam_sim_softc((struct cam_sim *)ccb->ccb_h.ccb_sim_ptr);
 1260 
 1261     GDT_DPRINTF(GDT_D_CMD, ("gdtexecuteccb(%p, %p, %p, %d, %d)\n", 
 1262                             gdt, gccb, dm_segs, nseg, error));
 1263     gdt_stat.sg_count_act = nseg;
 1264     if (nseg > gdt_stat.sg_count_max)
 1265         gdt_stat.sg_count_max = nseg;
 1266 
 1267     /* Copy the segments into our SG list */
 1268     if (gccb->gc_service == GDT_CACHESERVICE) {
 1269         for (i = 0; i < nseg; ++i) {
 1270             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
 1271                       i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
 1272             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_LST +
 1273                       i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
 1274             dm_segs++;
 1275         }
 1276         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_SG_CANZ,      
 1277                   nseg);
 1278         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_CACHE_DESTADDR, 
 1279                   0xffffffffUL);
 1280 
 1281         gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_CACHE_SG_LST +
 1282                                   nseg * GDT_SG_SZ, sizeof(u_int32_t));
 1283     } else {
 1284         for (i = 0; i < nseg; ++i) {
 1285             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
 1286                       i * GDT_SG_SZ + GDT_SG_PTR, dm_segs->ds_addr);
 1287             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_LST +
 1288                       i * GDT_SG_SZ + GDT_SG_LEN, dm_segs->ds_len);
 1289             dm_segs++;
 1290         }
 1291         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SG_RANZ,        
 1292                   nseg);
 1293         gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_RAW_SDATA, 
 1294                   0xffffffffUL);
 1295 
 1296         gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_RAW_SG_LST +
 1297                                   nseg * GDT_SG_SZ, sizeof(u_int32_t));
 1298     }
 1299 
 1300     if (nseg != 0) {
 1301         bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap, 
 1302             (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
 1303             BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
 1304     }
 1305     
 1306     /* We must NOT abort the command here if CAM_REQ_INPROG is not set,
 1307      * because command semaphore is already set!
 1308      */
 1309     
 1310     ccb->ccb_h.status |= CAM_SIM_QUEUED;
 1311     /* timeout handling */
 1312     ccb->ccb_h.timeout_ch =
 1313         timeout(iir_timeout, (caddr_t)gccb,
 1314                 (ccb->ccb_h.timeout * hz) / 1000);
 1315 
 1316     gdt->sc_copy_cmd(gdt, gccb);
 1317     splx(lock);
 1318 }
 1319 
 1320 
 1321 static void     
 1322 iir_action( struct cam_sim *sim, union ccb *ccb )
 1323 {
 1324     struct gdt_softc *gdt;
 1325     int lock, bus, target, lun;
 1326 
 1327     gdt = (struct gdt_softc *)cam_sim_softc( sim );
 1328     ccb->ccb_h.ccb_sim_ptr = sim;
 1329     bus = cam_sim_bus(sim);
 1330     target = ccb->ccb_h.target_id;
 1331     lun = ccb->ccb_h.target_lun;
 1332     GDT_DPRINTF(GDT_D_CMD, 
 1333                 ("iir_action(%p) func 0x%x cmd 0x%x bus %d target %d lun %d\n", 
 1334                  gdt, ccb->ccb_h.func_code, ccb->csio.cdb_io.cdb_bytes[0], 
 1335                  bus, target, lun)); 
 1336     ++gdt_stat.io_count_act;
 1337     if (gdt_stat.io_count_act > gdt_stat.io_count_max)
 1338         gdt_stat.io_count_max = gdt_stat.io_count_act;
 1339 
 1340     switch (ccb->ccb_h.func_code) {
 1341       case XPT_SCSI_IO:
 1342         lock = splcam();
 1343         TAILQ_INSERT_TAIL(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
 1344         ++gdt_stat.req_queue_act;
 1345         if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
 1346             gdt_stat.req_queue_max = gdt_stat.req_queue_act;
 1347         splx(lock);
 1348         gdt_next(gdt);
 1349         break;
 1350       case XPT_RESET_DEV:   /* Bus Device Reset the specified SCSI device */
 1351       case XPT_ABORT:                       /* Abort the specified CCB */
 1352         /* XXX Implement */
 1353         ccb->ccb_h.status = CAM_REQ_INVALID;
 1354         --gdt_stat.io_count_act;
 1355         xpt_done(ccb);
 1356         break;
 1357       case XPT_SET_TRAN_SETTINGS:
 1358         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
 1359         --gdt_stat.io_count_act;
 1360         xpt_done(ccb);  
 1361         break;
 1362       case XPT_GET_TRAN_SETTINGS:
 1363         /* Get default/user set transfer settings for the target */
 1364           {
 1365               struct        ccb_trans_settings *cts = &ccb->cts;
 1366               struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
 1367               struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
 1368 
 1369               cts->protocol = PROTO_SCSI;
 1370               cts->protocol_version = SCSI_REV_2;
 1371               cts->transport = XPORT_SPI;
 1372               cts->transport_version = 2;
 1373 
 1374               if (cts->type == CTS_TYPE_USER_SETTINGS) {
 1375                   spi->flags = CTS_SPI_FLAGS_DISC_ENB;
 1376                   scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
 1377                   spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
 1378                   spi->sync_period = 25; /* 10MHz */
 1379                   if (spi->sync_period != 0)
 1380                       spi->sync_offset = 15;
 1381                   
 1382                   spi->valid = CTS_SPI_VALID_SYNC_RATE
 1383                       | CTS_SPI_VALID_SYNC_OFFSET
 1384                       | CTS_SPI_VALID_BUS_WIDTH
 1385                       | CTS_SPI_VALID_DISC;
 1386                   scsi->valid = CTS_SCSI_VALID_TQ;
 1387                   ccb->ccb_h.status = CAM_REQ_CMP;
 1388               } else {
 1389                   ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
 1390               }
 1391               --gdt_stat.io_count_act;
 1392               xpt_done(ccb);
 1393               break;
 1394           }
 1395       case XPT_CALC_GEOMETRY:
 1396           {
 1397               struct ccb_calc_geometry *ccg;
 1398               u_int32_t secs_per_cylinder;
 1399 
 1400               ccg = &ccb->ccg;
 1401               ccg->heads = gdt->sc_hdr[target].hd_heads;
 1402               ccg->secs_per_track = gdt->sc_hdr[target].hd_secs;
 1403               secs_per_cylinder = ccg->heads * ccg->secs_per_track;
 1404               ccg->cylinders = ccg->volume_size / secs_per_cylinder;
 1405               ccb->ccb_h.status = CAM_REQ_CMP;
 1406               --gdt_stat.io_count_act;
 1407               xpt_done(ccb);
 1408               break;
 1409           }
 1410       case XPT_RESET_BUS:           /* Reset the specified SCSI bus */
 1411           {
 1412               /* XXX Implement */
 1413               ccb->ccb_h.status = CAM_REQ_CMP;
 1414               --gdt_stat.io_count_act;
 1415               xpt_done(ccb);
 1416               break;
 1417           }
 1418       case XPT_TERM_IO:             /* Terminate the I/O process */
 1419         /* XXX Implement */
 1420         ccb->ccb_h.status = CAM_REQ_INVALID;
 1421         --gdt_stat.io_count_act;
 1422         xpt_done(ccb);
 1423         break;
 1424       case XPT_PATH_INQ:            /* Path routing inquiry */
 1425           {
 1426               struct ccb_pathinq *cpi = &ccb->cpi;
 1427               
 1428               cpi->version_num = 1;
 1429               cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
 1430               cpi->hba_inquiry |= PI_WIDE_16;
 1431               cpi->target_sprt = 1;
 1432               cpi->hba_misc = 0;
 1433               cpi->hba_eng_cnt = 0;
 1434               if (bus == gdt->sc_virt_bus)
 1435                   cpi->max_target = GDT_MAX_HDRIVES - 1;
 1436               else if (gdt->sc_class & GDT_FC)
 1437                   cpi->max_target = GDT_MAXID_FC - 1;
 1438               else
 1439                   cpi->max_target = GDT_MAXID - 1;
 1440               cpi->max_lun = 7;
 1441               cpi->unit_number = cam_sim_unit(sim);
 1442               cpi->bus_id = bus;
 1443               cpi->initiator_id = 
 1444                   (bus == gdt->sc_virt_bus ? 127 : gdt->sc_bus_id[bus]);
 1445               cpi->base_transfer_speed = 3300;
 1446               strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
 1447               if (gdt->sc_vendor == INTEL_VENDOR_ID)
 1448                   strncpy(cpi->hba_vid, "Intel Corp.", HBA_IDLEN);
 1449               else
 1450                   strncpy(cpi->hba_vid, "ICP vortex ", HBA_IDLEN);
 1451               strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
 1452               cpi->transport = XPORT_SPI;
 1453               cpi->transport_version = 2;
 1454               cpi->protocol = PROTO_SCSI;
 1455               cpi->protocol_version = SCSI_REV_2;
 1456               cpi->ccb_h.status = CAM_REQ_CMP;
 1457               --gdt_stat.io_count_act;
 1458               xpt_done(ccb);
 1459               break;
 1460           }
 1461       default:
 1462         GDT_DPRINTF(GDT_D_INVALID, ("gdt_next(%p) cmd 0x%x invalid\n", 
 1463                                     gdt, ccb->ccb_h.func_code));
 1464         ccb->ccb_h.status = CAM_REQ_INVALID;
 1465         --gdt_stat.io_count_act;
 1466         xpt_done(ccb);
 1467         break;
 1468     }
 1469 }
 1470 
 1471 static void     
 1472 iir_poll( struct cam_sim *sim )
 1473 {
 1474     struct gdt_softc *gdt;
 1475 
 1476     gdt = (struct gdt_softc *)cam_sim_softc( sim );
 1477     GDT_DPRINTF(GDT_D_CMD, ("iir_poll sim %p gdt %p\n", sim, gdt));
 1478     iir_intr(gdt);
 1479 }
 1480 
 1481 static void     
 1482 iir_timeout(void *arg)
 1483 {
 1484     GDT_DPRINTF(GDT_D_TIMEOUT, ("iir_timeout(%p)\n", gccb));
 1485 }
 1486 
 1487 static void
 1488 iir_watchdog(void *arg)
 1489 {
 1490     struct gdt_softc *gdt;
 1491 
 1492     gdt = (struct gdt_softc *)arg;
 1493     GDT_DPRINTF(GDT_D_DEBUG, ("iir_watchdog(%p)\n", gdt));
 1494 
 1495     {
 1496         int ccbs = 0, ucmds = 0, frees = 0, pends = 0;
 1497         struct gdt_ccb *p;
 1498         struct ccb_hdr *h;
 1499         struct gdt_ucmd *u;
 1500 
 1501         for (h = TAILQ_FIRST(&gdt->sc_ccb_queue); h != NULL; 
 1502              h = TAILQ_NEXT(h, sim_links.tqe))
 1503             ccbs++;
 1504         for (u = TAILQ_FIRST(&gdt->sc_ucmd_queue); u != NULL; 
 1505              u = TAILQ_NEXT(u, links))
 1506             ucmds++;
 1507         for (p = SLIST_FIRST(&gdt->sc_free_gccb); p != NULL; 
 1508              p = SLIST_NEXT(p, sle))
 1509             frees++;
 1510         for (p = SLIST_FIRST(&gdt->sc_pending_gccb); p != NULL; 
 1511              p = SLIST_NEXT(p, sle))
 1512             pends++;
 1513 
 1514         GDT_DPRINTF(GDT_D_TIMEOUT, ("ccbs %d ucmds %d frees %d pends %d\n",
 1515                ccbs, ucmds, frees, pends));
 1516     }
 1517 
 1518     timeout(iir_watchdog, (caddr_t)gdt, hz * 15);
 1519 }
 1520 
 1521 static void     
 1522 iir_shutdown( void *arg, int howto )
 1523 {
 1524     struct gdt_softc *gdt;
 1525     struct gdt_ccb *gccb;
 1526     gdt_ucmd_t *ucmd;
 1527     int lock, i;
 1528 
 1529     gdt = (struct gdt_softc *)arg;
 1530     GDT_DPRINTF(GDT_D_CMD, ("iir_shutdown(%p, %d)\n", gdt, howto));
 1531 
 1532     printf("iir%d: Flushing all Host Drives. Please wait ...  ",
 1533            gdt->sc_hanum);
 1534 
 1535     /* allocate ucmd buffer */
 1536     ucmd = malloc(sizeof(gdt_ucmd_t), M_GDTBUF, M_NOWAIT);
 1537     if (ucmd == NULL) {
 1538         printf("iir%d: iir_shutdown(): Cannot allocate resource\n",
 1539                gdt->sc_hanum);
 1540         return;
 1541     }
 1542     bzero(ucmd, sizeof(gdt_ucmd_t));
 1543 
 1544     /* wait for pending IOs */
 1545     lock = splcam();
 1546     gdt->sc_state = GDT_SHUTDOWN;
 1547     splx(lock);
 1548     if ((gccb = SLIST_FIRST(&gdt->sc_pending_gccb)) != NULL)
 1549         (void) tsleep((void *)gccb, PCATCH | PRIBIO, "iirshw", 100 * hz);
 1550 
 1551     /* flush */
 1552     for (i = 0; i < GDT_MAX_HDRIVES; ++i) {
 1553         if (gdt->sc_hdr[i].hd_present) {
 1554             ucmd->service = GDT_CACHESERVICE;
 1555             ucmd->OpCode = GDT_FLUSH;
 1556             ucmd->u.cache.DeviceNo = i;
 1557             lock = splcam();
 1558             TAILQ_INSERT_TAIL(&gdt->sc_ucmd_queue, ucmd, links);
 1559             ucmd->complete_flag = FALSE;
 1560             splx(lock);
 1561             gdt_next(gdt);
 1562             if (!ucmd->complete_flag)
 1563                 (void) tsleep((void *)ucmd, PCATCH|PRIBIO, "iirshw", 10*hz);
 1564         }
 1565     }
 1566 
 1567     free(ucmd, M_DEVBUF);
 1568     printf("Done.\n");
 1569 }
 1570 
 1571 void
 1572 iir_intr(void *arg)
 1573 {
 1574     struct gdt_softc *gdt = arg;
 1575     struct gdt_intr_ctx ctx;
 1576     int lock = 0;
 1577     struct gdt_ccb *gccb;
 1578     gdt_ucmd_t *ucmd;
 1579     u_int32_t cnt;
 1580 
 1581     GDT_DPRINTF(GDT_D_INTR, ("gdt_intr(%p)\n", gdt));
 1582 
 1583     /* If polling and we were not called from gdt_wait, just return */
 1584     if ((gdt->sc_state & GDT_POLLING) &&
 1585         !(gdt->sc_state & GDT_POLL_WAIT))
 1586         return;
 1587 
 1588     if (!(gdt->sc_state & GDT_POLLING)) 
 1589         lock = splcam();
 1590     gdt_wait_index = 0;
 1591 
 1592     ctx.istatus = gdt->sc_get_status(gdt);
 1593     if (ctx.istatus == 0x00) {
 1594         if (!(gdt->sc_state & GDT_POLLING)) 
 1595             splx(lock);
 1596         gdt->sc_status = GDT_S_NO_STATUS;
 1597         return;
 1598     }
 1599 
 1600     gdt->sc_intr(gdt, &ctx);
 1601 
 1602     gdt->sc_status = ctx.cmd_status;
 1603     gdt->sc_service = ctx.service;
 1604     gdt->sc_info = ctx.info;
 1605     gdt->sc_info2 = ctx.info2;
 1606 
 1607     if (gdt->sc_state & GDT_POLL_WAIT) { 
 1608         gdt_wait_gdt = gdt;
 1609         gdt_wait_index = ctx.istatus;
 1610     }
 1611 
 1612     if (ctx.istatus == GDT_ASYNCINDEX) {
 1613         gdt_async_event(gdt, ctx.service);
 1614         if (!(gdt->sc_state & GDT_POLLING)) 
 1615             splx(lock);
 1616         return;
 1617     }
 1618     if (ctx.istatus == GDT_SPEZINDEX) {
 1619         GDT_DPRINTF(GDT_D_INVALID, 
 1620                     ("iir%d: Service unknown or not initialized!\n", 
 1621                      gdt->sc_hanum));   
 1622         gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
 1623         gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
 1624         gdt_store_event(GDT_ES_DRIVER, 4, &gdt->sc_dvr);
 1625         if (!(gdt->sc_state & GDT_POLLING)) 
 1626             splx(lock);
 1627         return;
 1628     }
 1629 
 1630     gccb = &gdt->sc_gccbs[ctx.istatus - 2];
 1631     ctx.service = gccb->gc_service;
 1632 
 1633     switch (gccb->gc_flags) {
 1634       case GDT_GCF_UNUSED:
 1635         GDT_DPRINTF(GDT_D_INVALID, ("iir%d: Index (%d) to unused command!\n",
 1636                     gdt->sc_hanum, ctx.istatus));
 1637         gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.driver);
 1638         gdt->sc_dvr.eu.driver.ionode = gdt->sc_hanum;
 1639         gdt->sc_dvr.eu.driver.index = ctx.istatus;
 1640         gdt_store_event(GDT_ES_DRIVER, 1, &gdt->sc_dvr);
 1641         gdt_free_ccb(gdt, gccb);
 1642         /* fallthrough */
 1643 
 1644       case GDT_GCF_INTERNAL:
 1645         if (!(gdt->sc_state & GDT_POLLING)) 
 1646             splx(lock);
 1647         break;
 1648 
 1649       case GDT_GCF_IOCTL:
 1650         ucmd = gccb->gc_ucmd; 
 1651         if (gdt->sc_status == GDT_S_BSY) {
 1652             GDT_DPRINTF(GDT_D_DEBUG, ("iir_intr(%p) ioctl: gccb %p busy\n", 
 1653                                       gdt, gccb));
 1654             TAILQ_INSERT_HEAD(&gdt->sc_ucmd_queue, ucmd, links);
 1655             if (!(gdt->sc_state & GDT_POLLING)) 
 1656                 splx(lock);
 1657         } else {
 1658             ucmd->status = gdt->sc_status;
 1659             ucmd->info = gdt->sc_info;
 1660             ucmd->complete_flag = TRUE;
 1661             if (ucmd->service == GDT_CACHESERVICE) {
 1662                 if (ucmd->OpCode == GDT_IOCTL) {
 1663                     cnt = ucmd->u.ioctl.param_size;
 1664                     if (cnt != 0)
 1665                         bcopy(gccb->gc_scratch, ucmd->data, cnt);
 1666                 } else {
 1667                     cnt = ucmd->u.cache.BlockCnt * GDT_SECTOR_SIZE;
 1668                     if (cnt != 0)
 1669                         bcopy(gccb->gc_scratch, ucmd->data, cnt);
 1670                 }
 1671             } else {
 1672                 cnt = ucmd->u.raw.sdlen;
 1673                 if (cnt != 0)
 1674                     bcopy(gccb->gc_scratch, ucmd->data, cnt);
 1675                 if (ucmd->u.raw.sense_len != 0) 
 1676                     bcopy(gccb->gc_scratch, ucmd->data, cnt);
 1677             }
 1678             gdt_free_ccb(gdt, gccb);
 1679             if (!(gdt->sc_state & GDT_POLLING)) 
 1680                 splx(lock);
 1681             /* wakeup */
 1682             wakeup(ucmd);
 1683         }
 1684         gdt_next(gdt); 
 1685         break;
 1686 
 1687       default:
 1688         gdt_free_ccb(gdt, gccb);
 1689         gdt_sync_event(gdt, ctx.service, ctx.istatus, gccb);
 1690         if (!(gdt->sc_state & GDT_POLLING)) 
 1691             splx(lock);
 1692         gdt_next(gdt); 
 1693         break;
 1694     }
 1695 }
 1696 
 1697 int
 1698 gdt_async_event(struct gdt_softc *gdt, int service)
 1699 {
 1700     struct gdt_ccb *gccb;
 1701 
 1702     GDT_DPRINTF(GDT_D_INTR, ("gdt_async_event(%p, %d)\n", gdt, service));
 1703 
 1704     if (service == GDT_SCREENSERVICE) {
 1705         if (gdt->sc_status == GDT_MSG_REQUEST) {
 1706             while (gdt->sc_test_busy(gdt))
 1707                 DELAY(1);
 1708             gccb = gdt_get_ccb(gdt);
 1709             if (gccb == NULL) {
 1710                 printf("iir%d: No free command index found\n",
 1711                        gdt->sc_hanum);
 1712                 return (1);
 1713             }
 1714             bzero(gccb->gc_cmd, GDT_CMD_SZ);
 1715             gccb->gc_service = service;
 1716             gccb->gc_flags = GDT_GCF_SCREEN;
 1717             gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
 1718                       gccb->gc_cmd_index);
 1719             gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_READ);
 1720             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
 1721                       GDT_MSG_INV_HANDLE);
 1722             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
 1723                       gccb->gc_scratch_busbase);
 1724             gdt->sc_set_sema0(gdt);
 1725             gdt->sc_cmd_off = 0;
 1726             gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ, 
 1727                                       sizeof(u_int32_t));
 1728             gdt->sc_cmd_cnt = 0;
 1729             gdt->sc_copy_cmd(gdt, gccb);
 1730             printf("iir%d: [PCI %d/%d] ",
 1731                 gdt->sc_hanum,gdt->sc_bus,gdt->sc_slot);
 1732             gdt->sc_release_event(gdt);
 1733         }
 1734 
 1735     } else {
 1736         if ((gdt->sc_fw_vers & 0xff) >= 0x1a) {
 1737             gdt->sc_dvr.size = 0;
 1738             gdt->sc_dvr.eu.async.ionode = gdt->sc_hanum;
 1739             gdt->sc_dvr.eu.async.status  = gdt->sc_status;
 1740             /* severity and event_string already set! */
 1741         } else {        
 1742             gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.async);
 1743             gdt->sc_dvr.eu.async.ionode   = gdt->sc_hanum;
 1744             gdt->sc_dvr.eu.async.service = service;
 1745             gdt->sc_dvr.eu.async.status  = gdt->sc_status;
 1746             gdt->sc_dvr.eu.async.info    = gdt->sc_info;
 1747             *(u_int32_t *)gdt->sc_dvr.eu.async.scsi_coord  = gdt->sc_info2;
 1748         }
 1749         gdt_store_event(GDT_ES_ASYNC, service, &gdt->sc_dvr);
 1750         printf("iir%d: %s\n", gdt->sc_hanum, gdt->sc_dvr.event_string);
 1751     }
 1752     
 1753     return (0);
 1754 }
 1755 
 1756 int
 1757 gdt_sync_event(struct gdt_softc *gdt, int service, 
 1758                u_int8_t index, struct gdt_ccb *gccb)
 1759 {
 1760     union ccb *ccb;
 1761 
 1762     GDT_DPRINTF(GDT_D_INTR,
 1763                 ("gdt_sync_event(%p, %d, %d, %p)\n", gdt,service,index,gccb));
 1764 
 1765     ccb = gccb->gc_ccb;
 1766 
 1767     if (service == GDT_SCREENSERVICE) {
 1768         u_int32_t msg_len;
 1769 
 1770         msg_len = gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_LEN);
 1771         if (msg_len)
 1772             if (!(gccb->gc_scratch[GDT_SCR_MSG_ANSWER] && 
 1773                   gccb->gc_scratch[GDT_SCR_MSG_EXT])) {
 1774                 gccb->gc_scratch[GDT_SCR_MSG_TEXT + msg_len] = '\0';
 1775                 printf("%s",&gccb->gc_scratch[GDT_SCR_MSG_TEXT]);
 1776             }
 1777 
 1778         if (gccb->gc_scratch[GDT_SCR_MSG_EXT] && 
 1779             !gccb->gc_scratch[GDT_SCR_MSG_ANSWER]) {
 1780             while (gdt->sc_test_busy(gdt))
 1781                 DELAY(1);
 1782             bzero(gccb->gc_cmd, GDT_CMD_SZ);
 1783             gccb = gdt_get_ccb(gdt);
 1784             if (gccb == NULL) {
 1785                 printf("iir%d: No free command index found\n",
 1786                        gdt->sc_hanum);
 1787                 return (1);
 1788             }
 1789             gccb->gc_service = service;
 1790             gccb->gc_flags = GDT_GCF_SCREEN;
 1791             gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
 1792                       gccb->gc_cmd_index);
 1793             gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_READ);
 1794             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
 1795                       gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
 1796             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
 1797                       gccb->gc_scratch_busbase);
 1798             gdt->sc_set_sema0(gdt);
 1799             gdt->sc_cmd_off = 0;
 1800             gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ, 
 1801                                       sizeof(u_int32_t));
 1802             gdt->sc_cmd_cnt = 0;
 1803             gdt->sc_copy_cmd(gdt, gccb);
 1804             gdt->sc_release_event(gdt);
 1805             return (0);
 1806         }
 1807 
 1808         if (gccb->gc_scratch[GDT_SCR_MSG_ANSWER] && 
 1809             gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN)) {
 1810             /* default answers (getchar() not possible) */
 1811             if (gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) == 1) {
 1812                 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN, 0);
 1813                 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 1);
 1814                 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 0;
 1815             } else {
 1816                 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_ALEN, 
 1817                           gdt_dec32(gccb->gc_scratch + GDT_SCR_MSG_ALEN) - 2);
 1818                 gdt_enc32(gccb->gc_scratch + GDT_SCR_MSG_LEN, 2);
 1819                 gccb->gc_scratch[GDT_SCR_MSG_TEXT] = 1;
 1820                 gccb->gc_scratch[GDT_SCR_MSG_TEXT + 1] = 0;
 1821             }
 1822             gccb->gc_scratch[GDT_SCR_MSG_EXT] = 0;
 1823             gccb->gc_scratch[GDT_SCR_MSG_ANSWER] = 0;
 1824             while (gdt->sc_test_busy(gdt))
 1825                 DELAY(1);
 1826             bzero(gccb->gc_cmd, GDT_CMD_SZ);
 1827             gccb = gdt_get_ccb(gdt);
 1828             if (gccb == NULL) {
 1829                 printf("iir%d: No free command index found\n",
 1830                        gdt->sc_hanum);
 1831                 return (1);
 1832             }
 1833             gccb->gc_service = service;
 1834             gccb->gc_flags = GDT_GCF_SCREEN;
 1835             gdt_enc32(gccb->gc_cmd + GDT_CMD_COMMANDINDEX,
 1836                       gccb->gc_cmd_index);
 1837             gdt_enc16(gccb->gc_cmd + GDT_CMD_OPCODE, GDT_WRITE);
 1838             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_HANDLE,
 1839                       gccb->gc_scratch[GDT_SCR_MSG_HANDLE]);
 1840             gdt_enc32(gccb->gc_cmd + GDT_CMD_UNION + GDT_SCREEN_MSG_ADDR,
 1841                       gccb->gc_scratch_busbase);
 1842             gdt->sc_set_sema0(gdt);
 1843             gdt->sc_cmd_off = 0;
 1844             gccb->gc_cmd_len = roundup(GDT_CMD_UNION + GDT_SCREEN_SZ, 
 1845                                       sizeof(u_int32_t));
 1846             gdt->sc_cmd_cnt = 0;
 1847             gdt->sc_copy_cmd(gdt, gccb);
 1848             gdt->sc_release_event(gdt);
 1849             return (0);
 1850         }
 1851         printf("\n");
 1852         return (0);
 1853     } else {
 1854         untimeout(iir_timeout, gccb, ccb->ccb_h.timeout_ch);
 1855         if (gdt->sc_status == GDT_S_BSY) {
 1856             GDT_DPRINTF(GDT_D_DEBUG, ("gdt_sync_event(%p) gccb %p busy\n", 
 1857                                       gdt, gccb));
 1858             TAILQ_INSERT_HEAD(&gdt->sc_ccb_queue, &ccb->ccb_h, sim_links.tqe);
 1859             ++gdt_stat.req_queue_act;
 1860             if (gdt_stat.req_queue_act > gdt_stat.req_queue_max)
 1861                 gdt_stat.req_queue_max = gdt_stat.req_queue_act;
 1862             return (2);
 1863         }
 1864 
 1865         bus_dmamap_sync(gdt->sc_buffer_dmat, gccb->gc_dmamap, 
 1866             (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN ?
 1867             BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
 1868         bus_dmamap_unload(gdt->sc_buffer_dmat, gccb->gc_dmamap);
 1869 
 1870         ccb->csio.resid = 0;
 1871         if (gdt->sc_status == GDT_S_OK) {
 1872             ccb->ccb_h.status |= CAM_REQ_CMP;
 1873             ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
 1874         } else {
 1875             /* error */
 1876             if (gccb->gc_service == GDT_CACHESERVICE) {
 1877                 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
 1878                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
 1879                 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
 1880                 bzero(&ccb->csio.sense_data, ccb->csio.sense_len);
 1881                 ccb->csio.sense_data.error_code =
 1882                     SSD_CURRENT_ERROR | SSD_ERRCODE_VALID;
 1883                 ccb->csio.sense_data.flags = SSD_KEY_NOT_READY;
 1884 
 1885                 gdt->sc_dvr.size = sizeof(gdt->sc_dvr.eu.sync);
 1886                 gdt->sc_dvr.eu.sync.ionode  = gdt->sc_hanum;
 1887                 gdt->sc_dvr.eu.sync.service = service;
 1888                 gdt->sc_dvr.eu.sync.status  = gdt->sc_status;
 1889                 gdt->sc_dvr.eu.sync.info    = gdt->sc_info;
 1890                 gdt->sc_dvr.eu.sync.hostdrive = ccb->ccb_h.target_id;
 1891                 if (gdt->sc_status >= 0x8000)
 1892                     gdt_store_event(GDT_ES_SYNC, 0, &gdt->sc_dvr);
 1893                 else
 1894                     gdt_store_event(GDT_ES_SYNC, service, &gdt->sc_dvr);
 1895             } else {
 1896                 /* raw service */
 1897                 if (gdt->sc_status != GDT_S_RAW_SCSI || gdt->sc_info >= 0x100) {
 1898                     ccb->ccb_h.status = CAM_DEV_NOT_THERE;
 1899                 } else {
 1900                     ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR|CAM_AUTOSNS_VALID;
 1901                     ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
 1902                     ccb->csio.scsi_status = gdt->sc_info;
 1903                     bcopy(gccb->gc_scratch, &ccb->csio.sense_data,
 1904                           ccb->csio.sense_len);
 1905                 }
 1906             }
 1907         }
 1908         --gdt_stat.io_count_act;
 1909         xpt_done(ccb);
 1910     }
 1911     return (0);
 1912 }
 1913 
 1914 /* Controller event handling functions */
 1915 gdt_evt_str *gdt_store_event(u_int16_t source, u_int16_t idx,
 1916                              gdt_evt_data *evt)
 1917 {
 1918     gdt_evt_str *e;
 1919     struct timeval tv;
 1920 
 1921     GDT_DPRINTF(GDT_D_MISC, ("gdt_store_event(%d, %d)\n", source, idx));
 1922     if (source == 0)                        /* no source -> no event */
 1923         return 0;
 1924 
 1925     if (ebuffer[elastidx].event_source == source &&
 1926         ebuffer[elastidx].event_idx == idx &&
 1927         ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
 1928           !memcmp((char *)&ebuffer[elastidx].event_data.eu,
 1929                   (char *)&evt->eu, evt->size)) ||
 1930          (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
 1931           !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
 1932                   (char *)&evt->event_string)))) { 
 1933         e = &ebuffer[elastidx];
 1934         getmicrotime(&tv);
 1935         e->last_stamp = tv.tv_sec;
 1936         ++e->same_count;
 1937     } else {
 1938         if (ebuffer[elastidx].event_source != 0) {  /* entry not free ? */
 1939             ++elastidx;
 1940             if (elastidx == GDT_MAX_EVENTS)
 1941                 elastidx = 0;
 1942             if (elastidx == eoldidx) {              /* reached mark ? */
 1943                 ++eoldidx;
 1944                 if (eoldidx == GDT_MAX_EVENTS)
 1945                     eoldidx = 0;
 1946             }
 1947         }
 1948         e = &ebuffer[elastidx];
 1949         e->event_source = source;
 1950         e->event_idx = idx;
 1951         getmicrotime(&tv);
 1952         e->first_stamp = e->last_stamp = tv.tv_sec;
 1953         e->same_count = 1;
 1954         e->event_data = *evt;
 1955         e->application = 0;
 1956     }
 1957     return e;
 1958 }
 1959 
 1960 int gdt_read_event(int handle, gdt_evt_str *estr)
 1961 {
 1962     gdt_evt_str *e;
 1963     int eindex, lock;
 1964     
 1965     GDT_DPRINTF(GDT_D_MISC, ("gdt_read_event(%d)\n", handle));
 1966     lock = splcam();
 1967     if (handle == -1)
 1968         eindex = eoldidx;
 1969     else
 1970         eindex = handle;
 1971     estr->event_source = 0;
 1972 
 1973     if (eindex >= GDT_MAX_EVENTS) {
 1974         splx(lock);
 1975         return eindex;
 1976     }
 1977     e = &ebuffer[eindex];
 1978     if (e->event_source != 0) {
 1979         if (eindex != elastidx) {
 1980             if (++eindex == GDT_MAX_EVENTS)
 1981                 eindex = 0;
 1982         } else {
 1983             eindex = -1;
 1984         }
 1985         memcpy(estr, e, sizeof(gdt_evt_str));
 1986     }
 1987     splx(lock);
 1988     return eindex;
 1989 }
 1990 
 1991 void gdt_readapp_event(u_int8_t application, gdt_evt_str *estr)
 1992 {
 1993     gdt_evt_str *e;
 1994     int found = FALSE;
 1995     int eindex, lock;
 1996     
 1997     GDT_DPRINTF(GDT_D_MISC, ("gdt_readapp_event(%d)\n", application));
 1998     lock = splcam();
 1999     eindex = eoldidx;
 2000     for (;;) {
 2001         e = &ebuffer[eindex];
 2002         if (e->event_source == 0)
 2003             break;
 2004         if ((e->application & application) == 0) {
 2005             e->application |= application;
 2006             found = TRUE;
 2007             break;
 2008         }
 2009         if (eindex == elastidx)
 2010             break;
 2011         if (++eindex == GDT_MAX_EVENTS)
 2012             eindex = 0;
 2013     }
 2014     if (found)
 2015         memcpy(estr, e, sizeof(gdt_evt_str));
 2016     else
 2017         estr->event_source = 0;
 2018     splx(lock);
 2019 }
 2020 
 2021 void gdt_clear_events()
 2022 {
 2023     GDT_DPRINTF(GDT_D_MISC, ("gdt_clear_events\n"));
 2024 
 2025     eoldidx = elastidx = 0;
 2026     ebuffer[0].event_source = 0;
 2027 }

Cache object: 75d8d58e21f110450f7cad7ea5753f46


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.