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/arcmsr/arcmsr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2 ******************************************************************************************
    3 **        O.S   : FreeBSD
    4 **   FILE NAME  : arcmsr.c
    5 **        BY    : Erich Chen   
    6 **   Description: SCSI RAID Device Driver for 
    7 **                ARECA (ARC11XX/ARC12XX) SATA RAID HOST Adapter
    8 **                ARCMSR RAID Host adapter[RAID controller:INTEL 331(PCI-X) 341(PCI-EXPRESS) chip set]
    9 ******************************************************************************************
   10 ************************************************************************
   11 **
   12 ** Copyright (c) 2004-2006 ARECA Co. Ltd.
   13 **        Erich Chen, Taipei Taiwan All rights reserved.
   14 **
   15 ** Redistribution and use in source and binary forms,with or without
   16 ** modification,are permitted provided that the following conditions
   17 ** are met:
   18 ** 1. Redistributions of source code must retain the above copyright
   19 **    notice,this list of conditions and the following disclaimer.
   20 ** 2. Redistributions in binary form must reproduce the above copyright
   21 **    notice,this list of conditions and the following disclaimer in the
   22 **    documentation and/or other materials provided with the distribution.
   23 ** 3. The name of the author may not be used to endorse or promote products
   24 **    derived from this software without specific prior written permission.
   25 **
   26 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   27 ** IMPLIED WARRANTIES,INCLUDING,BUT NOT LIMITED TO,THE IMPLIED WARRANTIES
   28 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   29 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,INDIRECT,
   30 ** INCIDENTAL,SPECIAL,EXEMPLARY,OR CONSEQUENTIAL DAMAGES(INCLUDING,BUT
   31 ** NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   32 ** DATA,OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY
   33 ** THEORY OF LIABILITY,WHETHER IN CONTRACT,STRICT LIABILITY,OR TORT
   34 **(INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF
   35 ** THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   36 **************************************************************************
   37 ** History
   38 **
   39 **        REV#         DATE                 NAME                 DESCRIPTION
   40 **     1.00.00.00    3/31/2004         Erich Chen            First release
   41 **     1.20.00.02   11/29/2004         Erich Chen        bug fix with arcmsr_bus_reset when PHY error
   42 **     1.20.00.03    4/19/2005         Erich Chen        add SATA 24 Ports adapter type support
   43 **                                                       clean unused function
   44 **     1.20.00.12    9/12/2005         Erich Chen        bug fix with abort command handling,firmware version check
   45 **                                                       and firmware update notify for hardware bug fix
   46 **                                                       handling if none zero high part physical address 
   47 **                                                       of srb resource 
   48 ******************************************************************************************
   49 ** $FreeBSD$
   50 */
   51 #define ARCMSR_DEBUG            0
   52 /*
   53 **********************************
   54 */
   55 #include <sys/param.h>
   56 #include <sys/systm.h>
   57 #include <sys/malloc.h>
   58 #include <sys/kernel.h>
   59 #include <sys/bus.h>
   60 #include <sys/queue.h>
   61 #include <sys/stat.h>
   62 #include <sys/devicestat.h>
   63 #include <sys/kthread.h>
   64 #include <sys/module.h>
   65 #include <sys/proc.h>
   66 #include <sys/lock.h>
   67 #include <sys/sysctl.h>
   68 #include <sys/poll.h>
   69 #include <sys/ioccom.h>
   70 #include <vm/vm.h>
   71 #include <vm/vm_param.h>
   72 #include <vm/pmap.h>
   73 
   74 #include <isa/rtc.h>
   75 
   76 #include <machine/bus.h>
   77 #include <machine/clock.h>
   78 #include <machine/resource.h>
   79 #include <machine/atomic.h>
   80 #include <sys/conf.h>
   81 #include <sys/rman.h>
   82 
   83 #include <cam/cam.h>
   84 #include <cam/cam_ccb.h>
   85 #include <cam/cam_sim.h>
   86 #include <cam/cam_xpt_sim.h>
   87 #include <cam/cam_debug.h>
   88 #include <cam/scsi/scsi_all.h>
   89 #include <cam/scsi/scsi_message.h>
   90 /* 
   91 **************************************************************************
   92 ** Define the OS version specific locks 
   93 **************************************************************************
   94 */
   95 #if __FreeBSD_version >= 500005
   96     #include <sys/selinfo.h>
   97         #include <sys/mutex.h>
   98     #include <dev/pci/pcivar.h>
   99     #include <dev/pci/pcireg.h>
  100         #define ARCMSR_LOCK_INIT(l, s)          mtx_init(l, s,NULL, MTX_DEF|MTX_RECURSE)
  101         #define ARCMSR_LOCK_ACQUIRE(l)          mtx_lock(l)
  102         #define ARCMSR_LOCK_RELEASE(l)          mtx_unlock(l)
  103         typedef struct mtx                      arcmsr_lock_t;
  104 #else
  105     #include <sys/select.h>
  106     #include <pci/pcivar.h>
  107     #include <pci/pcireg.h>
  108         #define ARCMSR_LOCK_INIT(l, s)          simple_lock_init(l)
  109         #define ARCMSR_LOCK_ACQUIRE(l)          simple_lock(l)
  110         #define ARCMSR_LOCK_RELEASE(l)          simple_unlock(l)
  111         typedef struct simplelock               arcmsr_lock_t;
  112 #endif
  113 #include <dev/arcmsr/arcmsr.h>
  114 /*
  115 **************************************************************************
  116 ** __FreeBSD_version 502010
  117 **************************************************************************
  118 */
  119 static struct _SRB * arcmsr_get_freesrb(struct _ACB * pACB);
  120 static u_int8_t arcmsr_seek_cmd2abort(union ccb * pabortccb);
  121 static u_int8_t arcmsr_wait_msgint_ready(struct _ACB * pACB);
  122 static u_int32_t arcmsr_probe(device_t dev);
  123 static u_int32_t arcmsr_attach(device_t dev);
  124 static u_int32_t arcmsr_detach(device_t dev);
  125 static u_int32_t arcmsr_iop_ioctlcmd(struct _ACB * pACB,u_int32_t ioctl_cmd,caddr_t arg);
  126 static void arcmsr_iop_parking(struct _ACB *pACB);
  127 static void arcmsr_shutdown(device_t dev);
  128 static void arcmsr_interrupt(void *arg);
  129 static void arcmsr_polling_srbdone(struct _ACB *pACB,struct _SRB *poll_srb);
  130 static void arcmsr_free_resource(struct _ACB * pACB);
  131 static void arcmsr_bus_reset(struct _ACB * pACB);
  132 static void arcmsr_stop_adapter_bgrb(struct _ACB * pACB);
  133 static void arcmsr_start_adapter_bgrb(struct _ACB * pACB);
  134 static void arcmsr_iop_init(struct _ACB * pACB);
  135 static void arcmsr_flush_adapter_cache(struct _ACB * pACB);
  136 static void arcmsr_queue_wait2go_srb(struct _ACB * pACB,struct _SRB * pSRB);
  137 static void arcmsr_post_wait2go_srb(struct _ACB * pACB);
  138 static void arcmsr_post_Qbuffer(struct _ACB * pACB);
  139 static void arcmsr_abort_allcmd(struct _ACB * pACB);
  140 static void arcmsr_srb_complete(struct _SRB * pSRB);
  141 static void arcmsr_iop_reset(struct _ACB * pACB);
  142 static void arcmsr_report_sense_info(struct _SRB * pSRB);
  143 static void arcmsr_build_srb(struct _SRB * pSRB, bus_dma_segment_t * dm_segs, u_int32_t nseg);
  144 static int arcmsr_resume(device_t dev);
  145 static int arcmsr_suspend(device_t dev);
  146 /*
  147 *****************************************************************************************
  148 ** Character device switch table
  149 **struct cdevsw {
  150 **      d_open_t                *d_open;
  151 **      d_close_t               *d_close;
  152 **      d_read_t                *d_read;
  153 **      d_write_t               *d_write;
  154 **      d_ioctl_t               *d_ioctl;
  155 **      d_poll_t                *d_poll;
  156 **      d_mmap_t                *d_mmap;
  157 **      d_strategy_t    *d_strategy;
  158 **      const char          *d_name;       "" base device name, e.g. 'vn' 
  159 **      int                      d_maj;
  160 **      d_dump_t            *d_dump;
  161 **      d_psize_t           *d_psize;
  162 **      u_int                d_flags;
  163 **      int                      d_bmaj;
  164 **      d_kqfilter_t    *d_kqfilter;   "" additions below are not binary compatible with 4.2 and below 
  165 **};
  166 ******************************************************************************************
  167 */
  168 /*
  169 **************************************************************************
  170 ** Insert a delay in micro-seconds and milli-seconds.
  171 ** static void MDELAY(u_int32_t ms) { while (ms--) UDELAY(1000); }
  172 **************************************************************************
  173 */
  174 static void UDELAY(u_int32_t us) { DELAY(us); }
  175 /*
  176 **************************************************************************
  177 ** 
  178 **************************************************************************
  179 */
  180 static bus_dmamap_callback_t arcmsr_map_freesrb;
  181 static bus_dmamap_callback_t arcmsr_executesrb;
  182 /*
  183 **************************************************************************
  184 ** 
  185 **************************************************************************
  186 */
  187 static d_open_t arcmsr_open;
  188 static d_close_t arcmsr_close;
  189 static d_ioctl_t arcmsr_ioctl;
  190 
  191 static device_method_t arcmsr_methods[]={
  192         DEVMETHOD(device_probe,         arcmsr_probe),
  193         DEVMETHOD(device_attach,        arcmsr_attach),
  194         DEVMETHOD(device_detach,        arcmsr_detach),
  195     DEVMETHOD(device_shutdown,  arcmsr_shutdown),
  196     DEVMETHOD(device_suspend,   arcmsr_suspend),
  197     DEVMETHOD(device_resume,    arcmsr_resume),
  198 
  199     DEVMETHOD(bus_print_child,  bus_generic_print_child),
  200     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
  201         { 0,0 }
  202 };
  203 
  204 static driver_t arcmsr_driver={
  205         "arcmsr",arcmsr_methods,sizeof(struct _ACB)
  206 };
  207 
  208 static devclass_t arcmsr_devclass;
  209 DRIVER_MODULE(arcmsr,pci,arcmsr_driver,arcmsr_devclass,0,0);
  210 #ifndef BUS_DMA_COHERENT                
  211     #define     BUS_DMA_COHERENT        0x04    /* hint: map memory in a coherent way */
  212 #endif
  213 #if __FreeBSD_version >= 501000
  214     #ifndef D_NEEDGIANT
  215         #define D_NEEDGIANT     0x00400000      /* driver want Giant */
  216     #endif
  217         #ifndef D_VERSION
  218         #define D_VERSION       0x20011966
  219     #endif
  220         static struct cdevsw arcmsr_cdevsw={
  221         #if __FreeBSD_version > 502010
  222             .d_version = D_VERSION,
  223     #endif
  224             .d_flags   = D_NEEDGIANT,
  225                 .d_open    = arcmsr_open,               /* open     */
  226                 .d_close   = arcmsr_close,              /* close    */
  227                 .d_ioctl   = arcmsr_ioctl,              /* ioctl    */
  228                 .d_name    = "arcmsr",                  /* name     */
  229         };
  230 #else
  231         #define ARCMSR_CDEV_MAJOR       180
  232 
  233         static struct cdevsw arcmsr_cdevsw = {
  234                 arcmsr_open,                    /* open     */
  235                 arcmsr_close,                   /* close    */
  236                 noread,                             /* read     */
  237                 nowrite,                            /* write    */
  238                 arcmsr_ioctl,                   /* ioctl    */
  239                 nopoll,                         /* poll     */
  240                 nommap,                             /* mmap     */
  241                 nostrategy,                         /* strategy */
  242                 "arcmsr",                               /* name     */
  243                 ARCMSR_CDEV_MAJOR,                  /* major    */
  244                 nodump,                             /* dump     */
  245                 nopsize,                            /* psize    */
  246                 0                                       /* flags    */
  247         };
  248 #endif
  249 
  250 #if __FreeBSD_version < 500005
  251     static int arcmsr_open(dev_t dev, int flags, int fmt, struct proc *proc)
  252 #else
  253     #if __FreeBSD_version < 503000
  254         static int arcmsr_open(dev_t dev, int flags, int fmt, struct thread *proc)
  255     #else
  256         static int arcmsr_open(struct cdev *dev, int flags, int fmt, d_thread_t *proc)
  257     #endif 
  258 #endif
  259 {
  260         #if __FreeBSD_version < 503000
  261             struct _ACB * pACB=dev->si_drv1;
  262     #else
  263                 int     unit = minor(dev);
  264                 struct _ACB * pACB = devclass_get_softc(arcmsr_devclass, unit);   
  265     #endif
  266 
  267         if(pACB==NULL)
  268         {
  269                 return ENXIO;
  270         }
  271         return 0;
  272 }
  273 /*
  274 **************************************************************************
  275 **************************************************************************
  276 */
  277 #if __FreeBSD_version < 500005
  278     static int arcmsr_close(dev_t dev, int flags, int fmt, struct proc *proc)
  279 #else
  280     #if __FreeBSD_version < 503000
  281         static int arcmsr_close(dev_t dev, int flags, int fmt, struct thread *proc)
  282     #else
  283         static int arcmsr_close(struct cdev *dev, int flags, int fmt, d_thread_t *proc)
  284     #endif 
  285 #endif
  286 {
  287         #if __FreeBSD_version < 503000
  288             struct _ACB * pACB=dev->si_drv1;
  289     #else
  290                 int     unit = minor(dev);
  291                 struct _ACB * pACB = devclass_get_softc(arcmsr_devclass, unit);   
  292     #endif
  293 
  294         if(pACB==NULL)
  295         {
  296                 return ENXIO;
  297         }
  298         return 0;
  299 }
  300 /*
  301 **************************************************************************
  302 **ENOENT
  303 **ENOIOCTL
  304 **ENOMEM
  305 **EINVAL
  306 **************************************************************************
  307 */
  308 #if __FreeBSD_version < 500005
  309     static int arcmsr_ioctl(dev_t dev, u_long ioctl_cmd, caddr_t arg, int flags, struct proc *proc)
  310 #else
  311     #if __FreeBSD_version < 503000
  312         static int arcmsr_ioctl(dev_t dev, u_long ioctl_cmd, caddr_t arg, int flags, struct thread *proc)
  313     #else
  314         static int arcmsr_ioctl(struct cdev *dev, u_long ioctl_cmd, caddr_t arg,int flags, d_thread_t *proc)
  315     #endif 
  316 #endif
  317 {
  318         #if __FreeBSD_version < 503000
  319             struct _ACB * pACB=dev->si_drv1;
  320     #else
  321                 int     unit = minor(dev);
  322                 struct _ACB * pACB = devclass_get_softc(arcmsr_devclass, unit);   
  323     #endif
  324 
  325         if(pACB==NULL)
  326         {
  327                 return ENXIO;
  328         }
  329     return(arcmsr_iop_ioctlcmd(pACB,ioctl_cmd,arg));
  330 }
  331 /*
  332 *******************************************************************************
  333 ** Bring the controller to a quiescent state, ready for system suspend.
  334 *******************************************************************************
  335 */
  336 static int arcmsr_suspend(device_t dev)
  337 {
  338     struct _ACB *pACB = device_get_softc(dev);
  339     u_int32_t intmask_org;
  340     int s;
  341 
  342     s = splbio();
  343     /* disable all outbound interrupt */
  344     intmask_org=readl(&pACB->pmu->outbound_intmask);
  345     writel(&pACB->pmu->outbound_intmask,(intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE));
  346     /* flush controller */
  347         printf("arcmsr%d: flushing cache...\n",pACB->pci_unit);
  348         arcmsr_iop_parking(pACB);
  349     splx(s);
  350     return(0);
  351 }
  352 /*
  353 *******************************************************************************
  354 ** Bring the controller back to a state ready for operation.
  355 *******************************************************************************
  356 */
  357 static int arcmsr_resume(device_t dev)
  358 {
  359     struct _ACB *pACB = device_get_softc(dev);
  360 
  361     arcmsr_iop_init(pACB);
  362     return(0);
  363 }
  364 /*
  365 *********************************************************************************
  366 **  Asynchronous notification handler.
  367 *********************************************************************************
  368 */
  369 static void arcmsr_async(void *cb_arg, u_int32_t code, struct cam_path *path, void *arg)
  370 {
  371         struct _ACB * pACB;
  372         u_int8_t target_id,target_lun;
  373         struct cam_sim * sim;
  374         u_int32_t s;
  375    
  376         s=splcam();
  377         sim=(struct cam_sim *) cb_arg;
  378         pACB =(struct _ACB *) cam_sim_softc(sim);
  379         switch (code)
  380         {
  381         case AC_LOST_DEVICE:
  382                 target_id=xpt_path_target_id(path);
  383         target_lun=xpt_path_lun_id(path);
  384                 if((target_id > ARCMSR_MAX_TARGETID) || (target_lun > ARCMSR_MAX_TARGETLUN))
  385                 {
  386                         break;
  387                 }
  388         printf("%s:scsi id%d lun%d device lost \n",device_get_name(pACB->pci_dev),target_id,target_lun);
  389                 break;
  390         default:
  391                 break;
  392         }
  393         splx(s);
  394 }
  395 /*
  396 ************************************************************************
  397 **
  398 **
  399 ************************************************************************
  400 */
  401 static void arcmsr_flush_adapter_cache(struct _ACB * pACB)
  402 {
  403         writel(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_FLUSH_CACHE);
  404         return;
  405 }
  406 /*
  407 **********************************************************************
  408 ** 
  409 **  
  410 **
  411 **********************************************************************
  412 */
  413 static u_int8_t arcmsr_wait_msgint_ready(struct _ACB * pACB)
  414 {
  415         u_int32_t Index;
  416         u_int8_t Retries=0x00;
  417         do
  418         {
  419                 for(Index=0; Index < 100; Index++)
  420                 {
  421                         if(readl(&pACB->pmu->outbound_intstatus) & ARCMSR_MU_OUTBOUND_MESSAGE0_INT)
  422                         {
  423                                 writel(&pACB->pmu->outbound_intstatus, ARCMSR_MU_OUTBOUND_MESSAGE0_INT);/*clear interrupt*/
  424                                 return 0x00;
  425                         }
  426                         /* one us delay */
  427                         UDELAY(10000);
  428                 }/*max 1 seconds*/
  429         }while(Retries++ < 20);/*max 20 sec*/
  430         return 0xff;
  431 }
  432 /*
  433 **********************************************************************
  434 **
  435 **  Q back this SRB into ACB ArraySRB
  436 **
  437 **********************************************************************
  438 */
  439 static void arcmsr_srb_complete(struct _SRB * pSRB)
  440 {
  441         u_int32_t s;
  442         struct _ACB * pACB=pSRB->pACB;
  443     union ccb * pccb=pSRB->pccb;
  444 
  445         if((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
  446         {
  447                 bus_dmasync_op_t op;
  448 
  449                 if((pccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
  450                 {
  451                         op = BUS_DMASYNC_POSTREAD;
  452                 }
  453                 else
  454                 {
  455                         op = BUS_DMASYNC_POSTWRITE;
  456                 }
  457                 bus_dmamap_sync(pACB->buffer_dmat, pSRB->dmamap, op);
  458                 bus_dmamap_unload(pACB->buffer_dmat, pSRB->dmamap);
  459         }
  460     s=splcam();
  461         atomic_subtract_int(&pACB->srboutstandingcount,1);
  462         pSRB->startdone=ARCMSR_SRB_DONE;
  463         pSRB->srb_flags=0;
  464         pACB->psrbringQ[pACB->srb_doneindex]=pSRB;
  465     pACB->srb_doneindex++;
  466     pACB->srb_doneindex %= ARCMSR_MAX_FREESRB_NUM;
  467     splx(s);
  468     xpt_done(pccb);
  469         return;
  470 }
  471 /*
  472 **********************************************************************
  473 **       if scsi error do auto request sense
  474 **********************************************************************
  475 */
  476 static void arcmsr_report_sense_info(struct _SRB * pSRB)
  477 {
  478         union ccb * pccb=pSRB->pccb;
  479         PSENSE_DATA  psenseBuffer=(PSENSE_DATA)&pccb->csio.sense_data;
  480 
  481     pccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
  482         pccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
  483     if(psenseBuffer) 
  484         {
  485                 memset(psenseBuffer, 0, sizeof(pccb->csio.sense_data));
  486                 memcpy(psenseBuffer,pSRB->arcmsr_cdb.SenseData,get_min(sizeof(struct _SENSE_DATA),sizeof(pccb->csio.sense_data)));
  487             psenseBuffer->ErrorCode=0x70;
  488         psenseBuffer->Valid=1;
  489                 pccb->ccb_h.status |= CAM_AUTOSNS_VALID;
  490     }
  491     return;
  492 }
  493 /*
  494 *********************************************************************
  495 ** to insert pSRB into tail of pACB wait exec srbQ 
  496 *********************************************************************
  497 */
  498 static void arcmsr_queue_wait2go_srb(struct _ACB * pACB,struct _SRB * pSRB)
  499 {
  500     u_int32_t s;
  501         u_int32_t i=0;
  502 
  503         s=splcam();
  504         while(1)
  505         {
  506                 if(pACB->psrbwait2go[i]==NULL)
  507                 {
  508                         pACB->psrbwait2go[i]=pSRB;
  509                 atomic_add_int(&pACB->srbwait2gocount,1);
  510             splx(s);
  511                         return;
  512                 }
  513                 i++;
  514                 i%=ARCMSR_MAX_OUTSTANDING_CMD;
  515         }
  516         return;
  517 }
  518 /*
  519 *********************************************************************
  520 ** 
  521 *********************************************************************
  522 */
  523 static void arcmsr_abort_allcmd(struct _ACB * pACB)
  524 {
  525         writel(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_ABORT_CMD);
  526         return;
  527 }
  528 
  529 /*
  530 ****************************************************************************
  531 ** Routine Description: Reset 80331 iop.
  532 **           Arguments: 
  533 **        Return Value: Nothing.
  534 ****************************************************************************
  535 */
  536 static void arcmsr_iop_reset(struct _ACB * pACB)
  537 {
  538         struct _SRB * pSRB;
  539         u_int32_t intmask_org,mask;
  540     u_int32_t i=0;
  541 
  542         if(pACB->srboutstandingcount!=0)
  543         {
  544                 printf("arcmsr%d: iop reset srboutstandingcount=%d \n",pACB->pci_unit,pACB->srboutstandingcount);
  545         /* disable all outbound interrupt */
  546                 intmask_org=readl(&pACB->pmu->outbound_intmask);
  547         writel(&pACB->pmu->outbound_intmask,intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE);
  548         /* talk to iop 331 outstanding command aborted*/
  549                 arcmsr_abort_allcmd(pACB);
  550                 if(arcmsr_wait_msgint_ready(pACB))
  551                 {
  552             printf("arcmsr%d: iop reset wait 'abort all outstanding command' timeout \n",pACB->pci_unit);
  553                 }
  554                 /*clear all outbound posted Q*/
  555                 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
  556                 {
  557                         readl(&pACB->pmu->outbound_queueport);
  558                 }
  559                 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
  560                 {
  561                         pSRB=pACB->psrb_pool[i];
  562                         if(pSRB->startdone==ARCMSR_SRB_START)
  563                         {
  564                                 pSRB->startdone=ARCMSR_SRB_ABORTED;
  565                 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
  566                 arcmsr_srb_complete(pSRB);
  567                         }
  568                 }
  569                 /* enable all outbound interrupt */
  570                 mask=~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE|ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
  571         writel(&pACB->pmu->outbound_intmask,intmask_org & mask);
  572                 /* post abort all outstanding command message to RAID controller */
  573         }
  574         i=0;
  575         while(pACB->srbwait2gocount > 0)
  576         {
  577                 pSRB=pACB->psrbwait2go[i];
  578                 if(pSRB!=NULL)
  579                 {
  580                         printf("arcmsr%d:iop reset abort command srbwait2gocount=%d \n",pACB->pci_unit,pACB->srbwait2gocount);
  581                     pACB->psrbwait2go[i]=NULL;
  582             pSRB->startdone=ARCMSR_SRB_ABORTED;
  583                         pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
  584             arcmsr_srb_complete(pSRB);
  585                         atomic_subtract_int(&pACB->srbwait2gocount,1);
  586                 }
  587                 i++;
  588                 i%=ARCMSR_MAX_OUTSTANDING_CMD;
  589         }
  590         atomic_set_int(&pACB->srboutstandingcount,0);
  591         return;
  592 }
  593 /*
  594 **********************************************************************
  595 ** 
  596 ** PAGE_SIZE=4096 or 8192,PAGE_SHIFT=12
  597 **********************************************************************
  598 */
  599 static void arcmsr_build_srb(struct _SRB * pSRB, bus_dma_segment_t *dm_segs, u_int32_t nseg)
  600 {
  601     struct _ARCMSR_CDB * pARCMSR_CDB=&pSRB->arcmsr_cdb;
  602         u_int8_t * psge=(u_int8_t *)&pARCMSR_CDB->u;
  603         u_int32_t address_lo,address_hi;
  604         union ccb * pccb=pSRB->pccb;
  605         struct ccb_scsiio * pcsio=&pccb->csio;
  606         u_int32_t arccdbsize=0x30;
  607 
  608         memset(pARCMSR_CDB,0,sizeof(struct _ARCMSR_CDB));
  609     pARCMSR_CDB->Bus=0;
  610     pARCMSR_CDB->TargetID=pccb->ccb_h.target_id;
  611     pARCMSR_CDB->LUN=pccb->ccb_h.target_lun;
  612     pARCMSR_CDB->Function=1;
  613         pARCMSR_CDB->CdbLength=(u_int8_t)pcsio->cdb_len;
  614     pARCMSR_CDB->Context=(unsigned long)pARCMSR_CDB;
  615         bcopy(pcsio->cdb_io.cdb_bytes, pARCMSR_CDB->Cdb, pcsio->cdb_len);
  616         if(nseg != 0) 
  617         {
  618                 struct _ACB * pACB=pSRB->pACB;
  619                 bus_dmasync_op_t op;    
  620                 u_int32_t length,i,cdb_sgcount=0;
  621 
  622                 /* map stor port SG list to our iop SG List.*/
  623                 for(i=0;i<nseg;i++) 
  624                 {
  625                         /* Get the physical address of the current data pointer */
  626                         length=(u_int32_t) dm_segs[i].ds_len;
  627             address_lo=dma_addr_lo32(dm_segs[i].ds_addr);
  628                         address_hi=dma_addr_hi32(dm_segs[i].ds_addr);
  629                         if(address_hi==0)
  630                         {
  631                                 struct _SG32ENTRY * pdma_sg=(struct _SG32ENTRY *)psge;
  632                                 pdma_sg->address=address_lo;
  633                                 pdma_sg->length=length;
  634                                 psge += sizeof(struct _SG32ENTRY);
  635                                 arccdbsize += sizeof(struct _SG32ENTRY);
  636                         }
  637                         else
  638                         {
  639                                 u_int32_t sg64s_size=0,tmplength=length;
  640 
  641                         #if ARCMSR_DEBUG
  642                                 printf("arcmsr%d: !!!!!!!!!!! address_hi=%x \n",pACB->pci_unit,address_hi);
  643                                 #endif
  644                                 while(1)
  645                                 {
  646                                         u_int64_t span4G,length0;
  647                                         struct _SG64ENTRY * pdma_sg=(struct _SG64ENTRY *)psge;
  648 
  649                                         span4G=(u_int64_t)address_lo + tmplength;
  650                                         pdma_sg->addresshigh=address_hi;
  651                                         pdma_sg->address=address_lo;
  652                                         if(span4G > 0x100000000)
  653                                         {   
  654                                                 /*see if cross 4G boundary*/
  655                                                 length0=0x100000000-address_lo;
  656                                                 pdma_sg->length=(u_int32_t)length0|IS_SG64_ADDR;
  657                                                 address_hi=address_hi+1;
  658                                                 address_lo=0;
  659                                                 tmplength=tmplength-(u_int32_t)length0;
  660                                                 sg64s_size += sizeof(struct _SG64ENTRY);
  661                                                 psge += sizeof(struct _SG64ENTRY);
  662                                                 cdb_sgcount++;
  663                                         }
  664                                         else
  665                                         {
  666                                         pdma_sg->length=tmplength|IS_SG64_ADDR;
  667                                                 sg64s_size += sizeof(struct _SG64ENTRY);
  668                                                 psge += sizeof(struct _SG64ENTRY);
  669                                                 break;
  670                                         }
  671                                 }
  672                                 arccdbsize += sg64s_size;
  673                         }
  674                         cdb_sgcount++;
  675                 }
  676                 pARCMSR_CDB->sgcount=(u_int8_t)cdb_sgcount;
  677                 pARCMSR_CDB->DataLength=pcsio->dxfer_len;
  678                 if( arccdbsize > 256)
  679                 {
  680                         pARCMSR_CDB->Flags|=ARCMSR_CDB_FLAG_SGL_BSIZE;
  681                 }
  682                 if((pccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
  683                 {
  684                         op=BUS_DMASYNC_PREREAD;
  685                 }
  686                 else
  687                 {
  688                         op=BUS_DMASYNC_PREWRITE;
  689                         pARCMSR_CDB->Flags|=ARCMSR_CDB_FLAG_WRITE;
  690                         pSRB->srb_flags|=SRB_FLAG_WRITE;
  691                 }
  692         bus_dmamap_sync(pACB->buffer_dmat, pSRB->dmamap, op);
  693         }
  694     return;
  695 }
  696 /*
  697 **************************************************************************
  698 **
  699 **      arcmsr_post_srb - Send a protocol specific ARC send postcard to a AIOC .
  700 **      handle: Handle of registered ARC protocol driver
  701 **      adapter_id: AIOC unique identifier(integer)
  702 **      pPOSTCARD_SEND: Pointer to ARC send postcard
  703 **
  704 **      This routine posts a ARC send postcard to the request post FIFO of a
  705 **      specific ARC adapter.
  706 **                             
  707 **************************************************************************
  708 */ 
  709 static void arcmsr_post_srb(struct _ACB * pACB,struct _SRB * pSRB)
  710 {
  711         u_int32_t cdb_shifted_phyaddr=(u_int32_t) pSRB->cdb_shifted_phyaddr;
  712         struct _ARCMSR_CDB * pARCMSR_CDB=(struct _ARCMSR_CDB *)&pSRB->arcmsr_cdb;
  713 
  714     atomic_add_int(&pACB->srboutstandingcount,1);
  715         pSRB->startdone=ARCMSR_SRB_START;
  716         if(pARCMSR_CDB->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
  717         {
  718             writel(&pACB->pmu->inbound_queueport,cdb_shifted_phyaddr|ARCMSR_SRBPOST_FLAG_SGL_BSIZE);
  719         }
  720         else
  721         {
  722             writel(&pACB->pmu->inbound_queueport,cdb_shifted_phyaddr);
  723         }
  724         return;
  725 }
  726 /*
  727 **************************************************************************
  728 **
  729 **
  730 **************************************************************************
  731 */
  732 static void arcmsr_post_wait2go_srb(struct _ACB * pACB)
  733 {
  734         u_int32_t s;
  735         struct _SRB * pSRB;
  736         u_int32_t i=0;
  737 
  738     s=splcam();
  739         while((pACB->srbwait2gocount > 0) && (pACB->srboutstandingcount < ARCMSR_MAX_OUTSTANDING_CMD))
  740         {
  741                 pSRB=pACB->psrbwait2go[i];
  742                 if(pSRB!=NULL)
  743                 {
  744                         pACB->psrbwait2go[i]=NULL;
  745                         arcmsr_post_srb(pACB,pSRB);
  746                         atomic_subtract_int(&pACB->srbwait2gocount,1);
  747                 }
  748                 i++;
  749                 i%=ARCMSR_MAX_OUTSTANDING_CMD;
  750         }
  751         splx(s);
  752         return;
  753 }
  754 /*
  755 **********************************************************************
  756 **   Function: arcmsr_post_Qbuffer
  757 **     Output: 
  758 **********************************************************************
  759 */
  760 static void arcmsr_post_Qbuffer(struct _ACB * pACB)
  761 {
  762     u_int32_t s;
  763         u_int8_t * pQbuffer;
  764         struct _QBUFFER * pwbuffer=(struct _QBUFFER *)&pACB->pmu->ioctl_wbuffer;
  765     u_int8_t * iop_data=(u_int8_t *)pwbuffer->data;
  766         u_int32_t allxfer_len=0;
  767 
  768     s=splcam();
  769         while((pACB->wqbuf_firstindex!=pACB->wqbuf_lastindex) && (allxfer_len<124))
  770         {
  771                 pQbuffer=&pACB->wqbuffer[pACB->wqbuf_firstindex];
  772                 memcpy(iop_data,pQbuffer,1);
  773                 pACB->wqbuf_firstindex++;
  774                 pACB->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER; /*if last index number set it to 0 */
  775                 iop_data++;
  776                 allxfer_len++;
  777         }
  778         pwbuffer->data_len=allxfer_len;
  779         /*
  780         ** push inbound doorbell and wait reply at hwinterrupt routine for next Qbuffer post
  781         */
  782         writel(&pACB->pmu->inbound_doorbell,ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK);
  783         splx(s);
  784         return;
  785 }
  786 /*
  787 ************************************************************************
  788 ************************************************************************
  789 */
  790 static void arcmsr_stop_adapter_bgrb(struct _ACB * pACB)
  791 {
  792         pACB->acb_flags |= ACB_F_MSG_STOP_BGRB;
  793         pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
  794         writel(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_STOP_BGRB);
  795         return;
  796 }
  797 /*
  798 ************************************************************************
  799 ************************************************************************
  800 */
  801 static void arcmsr_poll(struct cam_sim * psim)
  802 {
  803         arcmsr_interrupt(cam_sim_softc(psim));
  804         return;
  805 }
  806 /*
  807 **********************************************************************
  808 **   Function:  arcmsr_interrupt
  809 **     Output:  void
  810 **   CAM  Status field values   
  811 **typedef enum {
  812 **      CAM_REQ_INPROG,            CCB request is in progress   
  813 **      CAM_REQ_CMP,               CCB request completed without error   
  814 **      CAM_REQ_ABORTED,           CCB request aborted by the host   
  815 **      CAM_UA_ABORT,              Unable to abort CCB request   
  816 **      CAM_REQ_CMP_ERR,           CCB request completed with an error   
  817 **      CAM_BUSY,                      CAM subsytem is busy   
  818 **      CAM_REQ_INVALID,           CCB request was invalid   
  819 **      CAM_PATH_INVALID,          Supplied Path ID is invalid   
  820 **      CAM_DEV_NOT_THERE,         SCSI Device Not Installed/there   
  821 **      CAM_UA_TERMIO,             Unable to terminate I/O CCB request   
  822 **      CAM_SEL_TIMEOUT,           Target Selection Timeout   
  823 **      CAM_CMD_TIMEOUT,           Command timeout   
  824 **      CAM_SCSI_STATUS_ERROR,     SCSI error, look at error code in CCB   
  825 **      CAM_MSG_REJECT_REC,        Message Reject Received   
  826 **      CAM_SCSI_BUS_RESET,        SCSI Bus Reset Sent/Received   
  827 **      CAM_UNCOR_PARITY,          Uncorrectable parity error occurred   
  828 **      CAM_AUTOSENSE_FAIL=0x10,   Autosense: request sense cmd fail   
  829 **      CAM_NO_HBA,                No HBA Detected error   
  830 **      CAM_DATA_RUN_ERR,          Data Overrun error   
  831 **      CAM_UNEXP_BUSFREE,         Unexpected Bus Free   
  832 **      CAM_SEQUENCE_FAIL,         Target Bus Phase Sequence Failure   
  833 **      CAM_CCB_LEN_ERR,           CCB length supplied is inadequate   
  834 **      CAM_PROVIDE_FAIL,          Unable to provide requested capability   
  835 **      CAM_BDR_SENT,              A SCSI BDR msg was sent to target   
  836 **      CAM_REQ_TERMIO,            CCB request terminated by the host   
  837 **      CAM_UNREC_HBA_ERROR,       Unrecoverable Host Bus Adapter Error   
  838 **      CAM_REQ_TOO_BIG,           The request was too large for this host   
  839 **      CAM_REQUEUE_REQ,          
  840 **                               * This request should be requeued to preserve
  841 **                               * transaction ordering.  This typically occurs
  842 **                               * when the SIM recognizes an error that should
  843 **                               * freeze the queue and must place additional
  844 **                               * requests for the target at the sim level
  845 **                               * back into the XPT queue.
  846 **                                 
  847 **      CAM_IDE=0x33,              Initiator Detected Error   
  848 **      CAM_RESRC_UNAVAIL,         Resource Unavailable   
  849 **      CAM_UNACKED_EVENT,         Unacknowledged Event by Host   
  850 **      CAM_MESSAGE_RECV,          Message Received in Host Target Mode   
  851 **      CAM_INVALID_CDB,           Invalid CDB received in Host Target Mode   
  852 **      CAM_LUN_INVALID,           Lun supplied is invalid   
  853 **      CAM_TID_INVALID,           Target ID supplied is invalid   
  854 **      CAM_FUNC_NOTAVAIL,         The requested function is not available   
  855 **      CAM_NO_NEXUS,              Nexus is not established   
  856 **      CAM_IID_INVALID,           The initiator ID is invalid   
  857 **      CAM_CDB_RECVD,             The SCSI CDB has been received   
  858 **      CAM_LUN_ALRDY_ENA,         The LUN is already eanbeld for target mode   
  859 **      CAM_SCSI_BUSY,             SCSI Bus Busy   
  860 **
  861 **      CAM_DEV_QFRZN=0x40,        The DEV queue is frozen w/this err   
  862 **
  863 **                                 Autosense data valid for target   
  864 **      CAM_AUTOSNS_VALID=0x80,
  865 **      CAM_RELEASE_SIMQ=0x100,   SIM ready to take more commands   
  866 **      CAM_SIM_QUEUED  =0x200,   SIM has this command in it's queue   
  867 **
  868 **      CAM_STATUS_MASK=0x3F,      Mask bits for just the status #   
  869 **
  870 **                                 Target Specific Adjunct Status   
  871 **      CAM_SENT_SENSE=0x40000000          sent sense with status   
  872 **} cam_status;
  873 **********************************************************************
  874 */
  875 static void arcmsr_interrupt(void *arg)
  876 {
  877         struct _ACB * pACB=(struct _ACB *)arg;
  878         struct _SRB * pSRB;
  879         u_int32_t flag_srb,outbound_intstatus,outbound_doorbell;
  880 
  881         /*
  882         *********************************************
  883         **   check outbound intstatus À˹µL¶l®t«öªù¹a
  884         *********************************************
  885         */
  886         outbound_intstatus=readl(&pACB->pmu->outbound_intstatus) & pACB->outbound_int_enable;
  887     writel(&pACB->pmu->outbound_intstatus, outbound_intstatus);/*clear interrupt*/
  888         if(outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT)
  889         {
  890                 /*
  891                 *********************************************
  892                 **  DOORBELL ¥m¾´! ¬O§_¦³¶l¥ó­nñ¦¬
  893                 *********************************************
  894                 */
  895                 outbound_doorbell=readl(&pACB->pmu->outbound_doorbell);
  896                 writel(&pACB->pmu->outbound_doorbell,outbound_doorbell);/*clear interrupt */
  897                 if(outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK)
  898                 {
  899                         struct _QBUFFER * prbuffer=(struct _QBUFFER *)&pACB->pmu->ioctl_rbuffer;
  900                         u_int8_t * iop_data=(u_int8_t *)prbuffer->data;
  901                         u_int8_t * pQbuffer;
  902                         u_int32_t my_empty_len,iop_len,rqbuf_firstindex,rqbuf_lastindex;
  903 
  904             /*check this iop data if overflow my rqbuffer*/
  905                         rqbuf_lastindex=pACB->rqbuf_lastindex;
  906                         rqbuf_firstindex=pACB->rqbuf_firstindex;
  907                         iop_len=prbuffer->data_len;
  908             my_empty_len=(rqbuf_firstindex-rqbuf_lastindex-1)&(ARCMSR_MAX_QBUFFER-1);
  909                         if(my_empty_len>=iop_len)
  910                         {
  911                                 while(iop_len > 0)
  912                                 {
  913                                         pQbuffer=&pACB->rqbuffer[pACB->rqbuf_lastindex];
  914                                         memcpy(pQbuffer,iop_data,1);
  915                                         pACB->rqbuf_lastindex++;
  916                                         pACB->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;/*if last index number set it to 0 */
  917                                         iop_data++;
  918                                         iop_len--;
  919                                 }
  920                                 writel(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
  921                         }
  922                         else
  923                         {
  924                                 pACB->acb_flags|=ACB_F_IOPDATA_OVERFLOW;
  925                         }
  926                 }
  927                 if(outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK)
  928                 {
  929                         /*
  930                         *********************************************
  931                         **           ¬Ý¬Ý¬O§_ÁÙ¦³¶l¥ó­n¶¶¹D±H¥X
  932                         *********************************************
  933                         */
  934                         if(pACB->wqbuf_firstindex!=pACB->wqbuf_lastindex)
  935                         {
  936                                 u_int8_t * pQbuffer;
  937                                 struct _QBUFFER * pwbuffer=(struct _QBUFFER *)&pACB->pmu->ioctl_wbuffer;
  938                                 u_int8_t * iop_data=(u_int8_t *)pwbuffer->data;
  939                                 u_int32_t allxfer_len=0;
  940 
  941                                 while((pACB->wqbuf_firstindex!=pACB->wqbuf_lastindex) && (allxfer_len<124))
  942                                 {
  943                                         pQbuffer=&pACB->wqbuffer[pACB->wqbuf_firstindex];
  944                                         memcpy(iop_data,pQbuffer,1);
  945                                         pACB->wqbuf_firstindex++;
  946                                         pACB->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER; /*if last index number set it to 0 */
  947                                         iop_data++;
  948                                         allxfer_len++;
  949                                 }
  950                                 pwbuffer->data_len=allxfer_len;
  951                                 /*
  952                                 ** push inbound doorbell tell iop driver data write ok and wait reply on next hwinterrupt for next Qbuffer post
  953                                 */
  954                                 writel(&pACB->pmu->inbound_doorbell,ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK);
  955                         }
  956                         else
  957                         {
  958                                 pACB->acb_flags |= ACB_F_IOCTL_WQBUFFER_CLEARED;
  959                         }
  960                 }
  961         }
  962         if(outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT)
  963         {
  964                 int target,lun;
  965                 /*
  966                 *****************************************************************************
  967                 **               areca cdb command done
  968                 *****************************************************************************
  969                 */
  970                 while(1)
  971                 {
  972                         if((flag_srb=readl(&pACB->pmu->outbound_queueport)) == 0xFFFFFFFF)
  973                         {
  974                                 break;/*chip FIFO no srb for completion already*/
  975                         }
  976                         /* check if command done with no error*/
  977                         pSRB=(struct _SRB *)(pACB->vir2phy_offset+(flag_srb << 5));/*frame must be 32 bytes aligned*/
  978                         if((pSRB->pACB!=pACB) || (pSRB->startdone!=ARCMSR_SRB_START))
  979                         {
  980                                 if(pSRB->startdone==ARCMSR_SRB_ABORTED)
  981                                 {
  982                                         printf("arcmsr%d: scsi id=%d lun=%d srb='%p' isr command abort successfully \n",pACB->pci_unit,pSRB->pccb->ccb_h.target_id,pSRB->pccb->ccb_h.target_lun,pSRB);
  983                                         pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
  984                                         arcmsr_srb_complete(pSRB);
  985                                         continue;
  986                                 }
  987                                 printf("arcmsr%d: isr get an illegal srb command done acb='%p' srb='%p' srbacb='%p' startdone=0x%x srboutstandingcount=%d \n",pACB->pci_unit,pACB,pSRB,pSRB->pACB,pSRB->startdone,pACB->srboutstandingcount);
  988                                 continue;
  989                         }
  990                         target=pSRB->pccb->ccb_h.target_id;
  991                     lun=pSRB->pccb->ccb_h.target_lun;
  992                         if((flag_srb & ARCMSR_SRBREPLY_FLAG_ERROR)==0)
  993                         {
  994                                 if(pACB->devstate[target][lun]==ARECA_RAID_GONE)
  995                                 {
  996                                         pACB->devstate[target][lun]=ARECA_RAID_GOOD;
  997                                 }
  998                                 pSRB->pccb->ccb_h.status=CAM_REQ_CMP;
  999                                 arcmsr_srb_complete(pSRB);
 1000                         } 
 1001                         else 
 1002                         {   
 1003                                 switch(pSRB->arcmsr_cdb.DeviceStatus)
 1004                                 {
 1005                                 case ARCMSR_DEV_SELECT_TIMEOUT:
 1006                                         {
 1007                                                 pACB->devstate[target][lun]=ARECA_RAID_GONE;
 1008                                                 pSRB->pccb->ccb_h.status=CAM_SEL_TIMEOUT;
 1009                                                 arcmsr_srb_complete(pSRB);
 1010                                         }
 1011                                         break;
 1012                                 case ARCMSR_DEV_ABORTED:
 1013                                 case ARCMSR_DEV_INIT_FAIL:
 1014                                         {
 1015                                                 pACB->devstate[target][lun]=ARECA_RAID_GONE;
 1016                                                 pSRB->pccb->ccb_h.status=CAM_DEV_NOT_THERE;
 1017                                                 arcmsr_srb_complete(pSRB);
 1018                                         }
 1019                                         break;
 1020                                 case SCSISTAT_CHECK_CONDITION:
 1021                                         {
 1022                                                 pACB->devstate[target][lun]=ARECA_RAID_GOOD;
 1023                         arcmsr_report_sense_info(pSRB);
 1024                                                 arcmsr_srb_complete(pSRB);
 1025                                         }
 1026                                         break;
 1027                                 default:
 1028                                         /* error occur Q all error srb to errorsrbpending Q*/
 1029                                         printf("arcmsr%d: scsi id=%d lun=%d isr get command error done, but got unknow DeviceStatus=0x%x \n",pACB->pci_unit,target,lun,pSRB->arcmsr_cdb.DeviceStatus);
 1030                                         pACB->devstate[target][lun]=ARECA_RAID_GONE;
 1031                                         pSRB->pccb->ccb_h.status=CAM_UNCOR_PARITY;/*unknow error or crc error just for retry*/
 1032                                         arcmsr_srb_complete(pSRB);
 1033                                         break;
 1034                                 }
 1035                         }
 1036                 }       /*drain reply FIFO*/
 1037         }
 1038         if(pACB->srbwait2gocount != 0)
 1039         {
 1040         arcmsr_post_wait2go_srb(pACB);/*try to post all pending srb*/
 1041         }
 1042         return;
 1043 }
 1044 /*
 1045 *******************************************************************************
 1046 *******************************************************************************
 1047 */
 1048 static void arcmsr_iop_parking(struct _ACB *pACB)
 1049 {
 1050         if(pACB!=NULL)
 1051         {
 1052                 /* stop adapter background rebuild */
 1053                 if(pACB->acb_flags & ACB_F_MSG_START_BGRB)
 1054                 {
 1055                         pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
 1056                         arcmsr_stop_adapter_bgrb(pACB);
 1057                         if(arcmsr_wait_msgint_ready(pACB))
 1058                         {
 1059                                 printf("arcmsr%d:iop parking wait 'stop adapter rebulid' timeout \n",pACB->pci_unit);
 1060                         }
 1061                         arcmsr_flush_adapter_cache(pACB);
 1062                         if(arcmsr_wait_msgint_ready(pACB))
 1063                         {
 1064                                 printf("arcmsr%d:iop parking wait 'flush adapter cache' timeout \n",pACB->pci_unit);
 1065                         }
 1066                 }
 1067         }
 1068 }
 1069 /*
 1070 ***********************************************************************
 1071 **
 1072 **int   copyin __P((const void *udaddr, void *kaddr, size_t len));
 1073 **int   copyout __P((const void *kaddr, void *udaddr, size_t len));
 1074 **
 1075 **ENOENT     "" No such file or directory ""
 1076 **ENOIOCTL   "" ioctl not handled by this layer ""
 1077 **ENOMEM     "" Cannot allocate memory ""
 1078 **EINVAL     "" Invalid argument ""
 1079 ************************************************************************
 1080 */
 1081 u_int32_t arcmsr_iop_ioctlcmd(struct _ACB * pACB,u_int32_t ioctl_cmd,caddr_t arg)
 1082 {
 1083         struct _CMD_IO_CONTROL * pccbioctl=(struct _CMD_IO_CONTROL *) arg;
 1084 
 1085         if(memcmp(pccbioctl->Signature,"ARCMSR",6)!=0)
 1086     {
 1087         return EINVAL;
 1088         }
 1089         switch(ioctl_cmd)
 1090         {
 1091         case ARCMSR_IOCTL_READ_RQBUFFER:
 1092                 {
 1093                         u_int32_t s;                    
 1094                         struct _CMD_IOCTL_FIELD * pccbioctlfld=(struct _CMD_IOCTL_FIELD *)arg;
 1095                         u_int8_t * pQbuffer;
 1096                         u_int8_t * ptmpQbuffer=pccbioctlfld->ioctldatabuffer;                   
 1097                         u_int32_t allxfer_len=0;
 1098      
 1099             s=splcam();
 1100                         while((pACB->rqbuf_firstindex!=pACB->rqbuf_lastindex) && (allxfer_len<1031))
 1101                         {
 1102                                 /*copy READ QBUFFER to srb*/
 1103                 pQbuffer=&pACB->rqbuffer[pACB->rqbuf_firstindex];
 1104                                 memcpy(ptmpQbuffer,pQbuffer,1);
 1105                                 pACB->rqbuf_firstindex++;
 1106                                 pACB->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER; /*if last index number set it to 0 */
 1107                                 ptmpQbuffer++;
 1108                                 allxfer_len++;
 1109                         }
 1110                         if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
 1111                         {
 1112                 struct _QBUFFER * prbuffer=(struct _QBUFFER *)&pACB->pmu->ioctl_rbuffer;
 1113                 u_int8_t * pQbuffer;
 1114                                 u_int8_t * iop_data=(u_int8_t *)prbuffer->data;
 1115                 u_int32_t iop_len;
 1116 
 1117                 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
 1118                             iop_len=(u_int32_t)prbuffer->data_len;
 1119                                 /*this iop data does no chance to make me overflow again here, so just do it*/
 1120                                 while(iop_len>0)
 1121                                 {
 1122                     pQbuffer=&pACB->rqbuffer[pACB->rqbuf_lastindex];
 1123                                         memcpy(pQbuffer,iop_data,1);
 1124                                         pACB->rqbuf_lastindex++;
 1125                                         pACB->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;/*if last index number set it to 0 */
 1126                                         iop_data++;
 1127                                         iop_len--;
 1128                                 }
 1129                                 writel(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
 1130                         }
 1131                         pccbioctl->Length=allxfer_len;
 1132                         pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
 1133                         splx(s);
 1134                         return ARC_IOCTL_SUCCESS;
 1135                 }
 1136                 break;
 1137         case ARCMSR_IOCTL_WRITE_WQBUFFER:
 1138                 {
 1139                         u_int32_t s;
 1140             struct _CMD_IOCTL_FIELD * pccbioctlfld=(struct _CMD_IOCTL_FIELD *)arg;
 1141                         u_int32_t my_empty_len,user_len,wqbuf_firstindex,wqbuf_lastindex;
 1142                         u_int8_t * pQbuffer;
 1143                         u_int8_t * ptmpuserbuffer=pccbioctlfld->ioctldatabuffer;
 1144 
 1145             s=splcam();
 1146             user_len=pccbioctl->Length;
 1147               
 1148                         /*check if data xfer length of this request will overflow my array qbuffer */
 1149                         wqbuf_lastindex=pACB->wqbuf_lastindex;
 1150                         wqbuf_firstindex=pACB->wqbuf_firstindex;
 1151                         my_empty_len=(wqbuf_firstindex-wqbuf_lastindex-1)&(ARCMSR_MAX_QBUFFER-1);
 1152                         if(my_empty_len>=user_len)
 1153                         {
 1154                                 while(user_len>0)
 1155                                 {
 1156                                         /*copy srb data to wqbuffer*/
 1157                                         pQbuffer=&pACB->wqbuffer[pACB->wqbuf_lastindex];
 1158                                         memcpy(pQbuffer,ptmpuserbuffer,1);
 1159                                         pACB->wqbuf_lastindex++;
 1160                                         pACB->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;/*if last index number set it to 0 */
 1161                                         ptmpuserbuffer++;
 1162                                         user_len--;
 1163                                 }
 1164                                 /*post fist Qbuffer*/
 1165                                 if(pACB->acb_flags & ACB_F_IOCTL_WQBUFFER_CLEARED)
 1166                                 {
 1167                                         pACB->acb_flags &=~ACB_F_IOCTL_WQBUFFER_CLEARED;
 1168                                         arcmsr_post_Qbuffer(pACB);
 1169                                 }
 1170                                 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
 1171                         }
 1172                         else
 1173                         {
 1174                                 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_ERROR;
 1175                         }
 1176                         splx(s);
 1177                         return ARC_IOCTL_SUCCESS;
 1178                 }
 1179                 break;
 1180         case ARCMSR_IOCTL_CLEAR_RQBUFFER:
 1181                 {
 1182                         u_int32_t s;
 1183                         u_int8_t * pQbuffer=pACB->rqbuffer;
 1184             s=splcam();
 1185                         if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
 1186                         {
 1187                 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
 1188                 writel(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
 1189                         }
 1190             pACB->acb_flags |= ACB_F_IOCTL_RQBUFFER_CLEARED;
 1191                         pACB->rqbuf_firstindex=0;
 1192                         pACB->rqbuf_lastindex=0;
 1193             memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
 1194                         pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
 1195                         splx(s);
 1196                         return ARC_IOCTL_SUCCESS;
 1197                 }
 1198                 break;
 1199         case ARCMSR_IOCTL_CLEAR_WQBUFFER:
 1200                 {
 1201                         u_int32_t s;
 1202                         u_int8_t * pQbuffer=pACB->wqbuffer;
 1203  
 1204             s=splcam();
 1205                         if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
 1206                         {
 1207                 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
 1208                 writel(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
 1209                         }
 1210                         pACB->acb_flags |= ACB_F_IOCTL_WQBUFFER_CLEARED;
 1211                         pACB->wqbuf_firstindex=0;
 1212                         pACB->wqbuf_lastindex=0;
 1213             memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
 1214                         pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
 1215                         splx(s);
 1216                         return ARC_IOCTL_SUCCESS;
 1217                 }
 1218                 break;
 1219         case ARCMSR_IOCTL_CLEAR_ALLQBUFFER:
 1220                 {
 1221                         u_int32_t s;
 1222                         u_int8_t * pQbuffer;
 1223  
 1224             s=splcam();
 1225                         if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
 1226                         {
 1227                 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
 1228                 writel(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
 1229                         }
 1230                         pACB->acb_flags |= (ACB_F_IOCTL_WQBUFFER_CLEARED|ACB_F_IOCTL_RQBUFFER_CLEARED);
 1231                         pACB->rqbuf_firstindex=0;
 1232                         pACB->rqbuf_lastindex=0;
 1233                         pACB->wqbuf_firstindex=0;
 1234                         pACB->wqbuf_lastindex=0;
 1235                         pQbuffer=pACB->rqbuffer;
 1236             memset(pQbuffer, 0, sizeof(struct _QBUFFER));
 1237                         pQbuffer=pACB->wqbuffer;
 1238             memset(pQbuffer, 0, sizeof(struct _QBUFFER));
 1239                         pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
 1240                         splx(s);
 1241                         return ARC_IOCTL_SUCCESS;
 1242                 }
 1243                 break;
 1244         case ARCMSR_IOCTL_REQUEST_RETURNCODE_3F:
 1245                 {
 1246                         pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_3F;
 1247                         return ARC_IOCTL_SUCCESS;
 1248                 }
 1249                 break;
 1250         case ARCMSR_IOCTL_SAY_HELLO:
 1251                 {
 1252                         struct _CMD_IOCTL_FIELD * pccbioctlfld=(struct _CMD_IOCTL_FIELD *)arg;
 1253                         u_int8_t * hello_string="Hello! I am ARCMSR";
 1254                         u_int8_t * puserbuffer=(u_int8_t *)pccbioctlfld->ioctldatabuffer;
 1255   
 1256                         if(memcpy(puserbuffer,hello_string,(int16_t)strlen(hello_string)))
 1257                         {
 1258                                 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_ERROR;
 1259                 return ENOIOCTL;
 1260                         }
 1261             pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
 1262                     return ARC_IOCTL_SUCCESS;
 1263                 }
 1264                 break;
 1265         case ARCMSR_IOCTL_SAY_GOODBYE:
 1266                 {
 1267             arcmsr_iop_parking(pACB);
 1268                         return ARC_IOCTL_SUCCESS;
 1269                 }
 1270                 break;
 1271     case ARCMSR_IOCTL_FLUSH_ADAPTER_CACHE:
 1272                 {
 1273                         arcmsr_flush_adapter_cache(pACB);
 1274                         if(arcmsr_wait_msgint_ready(pACB))
 1275                         {
 1276                                 printf("arcmsr%d: ioctl flush cache wait 'flush adapter cache' timeout \n",pACB->pci_unit);
 1277                         }
 1278                         return ARC_IOCTL_SUCCESS;
 1279                 }
 1280                 break;
 1281         }
 1282     return EINVAL;
 1283 }
 1284 /*
 1285 **************************************************************************
 1286 **
 1287 **************************************************************************
 1288 */
 1289 struct _SRB * arcmsr_get_freesrb(struct _ACB * pACB)
 1290 {
 1291     struct _SRB * pSRB=NULL;
 1292         u_int32_t s;
 1293         u_int32_t srb_startindex,srb_doneindex;
 1294 
 1295         s=splcam();
 1296         srb_doneindex=pACB->srb_doneindex;
 1297         srb_startindex=pACB->srb_startindex;
 1298         pSRB=pACB->psrbringQ[srb_startindex];
 1299         srb_startindex++;
 1300         srb_startindex %= ARCMSR_MAX_FREESRB_NUM;
 1301         if(srb_doneindex!=srb_startindex)
 1302         {
 1303                 pACB->srb_startindex=srb_startindex;
 1304         }
 1305         else
 1306         {
 1307         pSRB=NULL;
 1308         }
 1309         splx(s);
 1310         return(pSRB);
 1311 }
 1312 /*
 1313 *********************************************************************
 1314 **
 1315 **
 1316 **
 1317 *********************************************************************
 1318 */
 1319 static void arcmsr_executesrb(void *arg,bus_dma_segment_t *dm_segs,int nseg,int error)
 1320 {
 1321         struct _SRB * pSRB=(struct _SRB *)arg;
 1322     struct _ACB * pACB=(struct _ACB *)pSRB->pACB;
 1323         union ccb * pccb;
 1324         int target,lun; 
 1325 
 1326         pccb=pSRB->pccb;
 1327         target=pccb->ccb_h.target_id;
 1328         lun=pccb->ccb_h.target_lun;
 1329         if(error != 0) 
 1330         {
 1331                 if(error != EFBIG)
 1332                 {
 1333                         printf("arcmsr%d: unexpected error %x returned from 'bus_dmamap_load' \n",pACB->pci_unit,error);
 1334                 }
 1335                 if(pccb->ccb_h.status == CAM_REQ_INPROG) 
 1336                 {
 1337                         xpt_freeze_devq(pccb->ccb_h.path,/*count*/1);
 1338                         pccb->ccb_h.status=CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
 1339                 }
 1340                 arcmsr_srb_complete(pSRB);
 1341                 return;
 1342         }
 1343         if(pACB->acb_flags & ACB_F_BUS_RESET)
 1344         {
 1345                 printf("arcmsr%d: bus reset and return busy \n",pACB->pci_unit);
 1346             pccb->ccb_h.status|=CAM_SCSI_BUS_RESET;
 1347                 arcmsr_srb_complete(pSRB);
 1348                 return;
 1349         }
 1350         if(pACB->devstate[target][lun]==ARECA_RAID_GONE)
 1351         {
 1352                 u_int8_t block_cmd;
 1353 
 1354         block_cmd=pccb->csio.cdb_io.cdb_bytes[0] & 0x0f;
 1355                 if(block_cmd==0x08 || block_cmd==0x0a)
 1356                 {
 1357                         printf("arcmsr%d:block 'read/write' command with gone raid volume Cmd=%2x,TargetId=%d,Lun=%d \n",pACB->pci_unit,block_cmd,target,lun);
 1358                         pccb->ccb_h.status=CAM_DEV_NOT_THERE;
 1359                         arcmsr_srb_complete(pSRB);
 1360                         return;
 1361                 }
 1362         }
 1363     arcmsr_build_srb(pSRB,dm_segs,nseg);
 1364         if(pccb->ccb_h.status != CAM_REQ_INPROG)
 1365         {
 1366                 if(nseg != 0)
 1367                 {
 1368                         bus_dmamap_unload(pACB->buffer_dmat,pSRB->dmamap);
 1369                 }
 1370                 arcmsr_srb_complete(pSRB);
 1371                 return;
 1372         }
 1373         pccb->ccb_h.status |= CAM_SIM_QUEUED;
 1374         if(pACB->srboutstandingcount < ARCMSR_MAX_OUTSTANDING_CMD)
 1375         {   
 1376                 /*
 1377                 ******************************************************************
 1378                 ** and we can make sure there were no pending srb in this duration
 1379                 ******************************************************************
 1380                 */
 1381         arcmsr_post_srb(pACB,pSRB);
 1382         }
 1383         else
 1384         {
 1385                 /*
 1386                 ******************************************************************
 1387                 ** Q of srbwaitexec will be post out when any outstanding command complete
 1388                 ******************************************************************
 1389                 */
 1390                 arcmsr_queue_wait2go_srb(pACB,pSRB);
 1391         }
 1392         return;
 1393 }
 1394 /*
 1395 *****************************************************************************************
 1396 **
 1397 *****************************************************************************************
 1398 */
 1399 static u_int8_t arcmsr_seek_cmd2abort(union ccb * pabortccb)
 1400 {
 1401         struct _SRB * pSRB;
 1402     struct _ACB * pACB=(struct _ACB *) pabortccb->ccb_h.arcmsr_ccbacb_ptr;
 1403         u_int32_t s,intmask_org,mask;
 1404     u_int32_t i=0;
 1405 
 1406         s=splcam();
 1407         pACB->num_aborts++;
 1408         /* 
 1409         ***************************************************************************
 1410         ** It is the upper layer do abort command this lock just prior to calling us.
 1411         ** First determine if we currently own this command.
 1412         ** Start by searching the device queue. If not found
 1413         ** at all,and the system wanted us to just abort the
 1414         ** command return success.
 1415         ***************************************************************************
 1416         */
 1417         if(pACB->srboutstandingcount!=0)
 1418         {
 1419                 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
 1420                 {
 1421                         pSRB=pACB->psrb_pool[i];
 1422                         if(pSRB->startdone==ARCMSR_SRB_START)
 1423                         {
 1424                                 if(pSRB->pccb==pabortccb)
 1425                                 {
 1426                     pSRB->startdone=ARCMSR_SRB_ABORTED;
 1427                                         printf("arcmsr%d:scsi id=%d lun=%d abort srb '%p' outstanding command \n",pACB->pci_unit,pabortccb->ccb_h.target_id,pabortccb->ccb_h.target_lun,pSRB);
 1428                     goto abort_outstanding_cmd;
 1429                                 }
 1430                         }
 1431                 }
 1432         }
 1433         /*
 1434         ********************************************************
 1435         ** seek this command at our command list 
 1436         ** if command found then remove,abort it and free this SRB
 1437         ********************************************************
 1438         */
 1439         if(pACB->srbwait2gocount!=0)
 1440         {
 1441                 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
 1442                 {
 1443                         pSRB=pACB->psrbwait2go[i];
 1444                         if(pSRB!=NULL)
 1445                         {
 1446                                 if(pSRB->pccb==pabortccb)
 1447                                 {
 1448                                         printf("arcmsr%d:scsi id=%d lun=%d abort ccb '%p' pending command \n",pACB->pci_unit,pabortccb->ccb_h.target_id,pabortccb->ccb_h.target_lun,pSRB);
 1449                                         pACB->psrbwait2go[i]=NULL;
 1450                                         pSRB->startdone=ARCMSR_SRB_ABORTED;
 1451                                         pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED; 
 1452                                         arcmsr_srb_complete(pSRB);
 1453                                 atomic_subtract_int(&pACB->srbwait2gocount,1);
 1454                     splx(s);
 1455                                         return(TRUE);
 1456                                 }
 1457                         }
 1458                 }
 1459         }
 1460     splx(s);
 1461         return(FALSE);
 1462 abort_outstanding_cmd:
 1463     /* disable all outbound interrupt */
 1464         intmask_org=readl(&pACB->pmu->outbound_intmask);
 1465     writel(&pACB->pmu->outbound_intmask,intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE);
 1466     /* do not talk to iop 331 abort command */
 1467         arcmsr_polling_srbdone(pACB,pSRB);
 1468         /* enable all outbound interrupt */
 1469         mask=~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE|ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
 1470     writel(&pACB->pmu->outbound_intmask,intmask_org & mask);
 1471         atomic_set_int(&pACB->srboutstandingcount,0);
 1472         splx(s);
 1473         return (TRUE);
 1474 }
 1475 /*
 1476 ****************************************************************************
 1477 ** 
 1478 ****************************************************************************
 1479 */
 1480 static void arcmsr_bus_reset(struct _ACB * pACB)
 1481 {
 1482         int retry=0;
 1483 
 1484         pACB->num_resets++;
 1485         pACB->acb_flags |=ACB_F_BUS_RESET;
 1486         while(pACB->srboutstandingcount!=0 && retry < 400)
 1487         {
 1488                 arcmsr_interrupt((void *)pACB);
 1489         UDELAY(25000);
 1490                 retry++;
 1491         }
 1492         arcmsr_iop_reset(pACB);
 1493     pACB->acb_flags &= ~ACB_F_BUS_RESET;
 1494         return;
 1495 } 
 1496 /*
 1497 *********************************************************************
 1498 **
 1499 **   CAM  Status field values   
 1500 **typedef enum {
 1501 **      CAM_REQ_INPROG,            CCB request is in progress   
 1502 **      CAM_REQ_CMP,               CCB request completed without error   
 1503 **      CAM_REQ_ABORTED,           CCB request aborted by the host   
 1504 **      CAM_UA_ABORT,              Unable to abort CCB request   
 1505 **      CAM_REQ_CMP_ERR,           CCB request completed with an error   
 1506 **      CAM_BUSY,                      CAM subsytem is busy   
 1507 **      CAM_REQ_INVALID,           CCB request was invalid   
 1508 **      CAM_PATH_INVALID,          Supplied Path ID is invalid   
 1509 **      CAM_DEV_NOT_THERE,         SCSI Device Not Installed/there   
 1510 **      CAM_UA_TERMIO,             Unable to terminate I/O CCB request   
 1511 **      CAM_SEL_TIMEOUT,           Target Selection Timeout   
 1512 **      CAM_CMD_TIMEOUT,           Command timeout   
 1513 **      CAM_SCSI_STATUS_ERROR,     SCSI error, look at error code in CCB   
 1514 **      CAM_MSG_REJECT_REC,        Message Reject Received   
 1515 **      CAM_SCSI_BUS_RESET,        SCSI Bus Reset Sent/Received   
 1516 **      CAM_UNCOR_PARITY,          Uncorrectable parity error occurred   
 1517 **      CAM_AUTOSENSE_FAIL=0x10,   Autosense: request sense cmd fail   
 1518 **      CAM_NO_HBA,                No HBA Detected error   
 1519 **      CAM_DATA_RUN_ERR,          Data Overrun error   
 1520 **      CAM_UNEXP_BUSFREE,         Unexpected Bus Free   
 1521 **      CAM_SEQUENCE_FAIL,         Target Bus Phase Sequence Failure   
 1522 **      CAM_CCB_LEN_ERR,           CCB length supplied is inadequate   
 1523 **      CAM_PROVIDE_FAIL,          Unable to provide requested capability   
 1524 **      CAM_BDR_SENT,              A SCSI BDR msg was sent to target   
 1525 **      CAM_REQ_TERMIO,            CCB request terminated by the host   
 1526 **      CAM_UNREC_HBA_ERROR,       Unrecoverable Host Bus Adapter Error   
 1527 **      CAM_REQ_TOO_BIG,           The request was too large for this host   
 1528 **      CAM_REQUEUE_REQ,          
 1529 **                               * This request should be requeued to preserve
 1530 **                               * transaction ordering.  This typically occurs
 1531 **                               * when the SIM recognizes an error that should
 1532 **                               * freeze the queue and must place additional
 1533 **                               * requests for the target at the sim level
 1534 **                               * back into the XPT queue.
 1535 **                                 
 1536 **      CAM_IDE=0x33,              Initiator Detected Error   
 1537 **      CAM_RESRC_UNAVAIL,         Resource Unavailable   
 1538 **      CAM_UNACKED_EVENT,         Unacknowledged Event by Host   
 1539 **      CAM_MESSAGE_RECV,          Message Received in Host Target Mode   
 1540 **      CAM_INVALID_CDB,           Invalid CDB received in Host Target Mode   
 1541 **      CAM_LUN_INVALID,           Lun supplied is invalid   
 1542 **      CAM_TID_INVALID,           Target ID supplied is invalid   
 1543 **      CAM_FUNC_NOTAVAIL,         The requested function is not available   
 1544 **      CAM_NO_NEXUS,              Nexus is not established   
 1545 **      CAM_IID_INVALID,           The initiator ID is invalid   
 1546 **      CAM_CDB_RECVD,             The SCSI CDB has been received   
 1547 **      CAM_LUN_ALRDY_ENA,         The LUN is already eanbeld for target mode   
 1548 **      CAM_SCSI_BUSY,             SCSI Bus Busy   
 1549 **
 1550 **      CAM_DEV_QFRZN=0x40,        The DEV queue is frozen w/this err   
 1551 **
 1552 **                                 Autosense data valid for target   
 1553 **      CAM_AUTOSNS_VALID=0x80,
 1554 **      CAM_RELEASE_SIMQ=0x100,   SIM ready to take more commands   
 1555 **      CAM_SIM_QUEUED  =0x200,   SIM has this command in it's queue   
 1556 **
 1557 **      CAM_STATUS_MASK=0x3F,      Mask bits for just the status #   
 1558 **
 1559 **                                 Target Specific Adjunct Status   
 1560 **      CAM_SENT_SENSE=0x40000000          sent sense with status   
 1561 **} cam_status;
 1562 **
 1563 **union ccb {
 1564 **                      struct  ccb_hdr                 ccb_h;   For convenience 
 1565 **                      struct  ccb_scsiio              csio;
 1566 **                      struct  ccb_getdev              cgd;
 1567 **                      struct  ccb_getdevlist          cgdl;
 1568 **                      struct  ccb_pathinq             cpi;
 1569 **                      struct  ccb_relsim              crs;
 1570 **                      struct  ccb_setasync            csa;
 1571 **                      struct  ccb_setdev              csd;
 1572 **                      struct  ccb_pathstats           cpis;
 1573 **                      struct  ccb_getdevstats         cgds;
 1574 **                      struct  ccb_dev_match           cdm;
 1575 **                      struct  ccb_trans_settings      cts;
 1576 **                      struct  ccb_calc_geometry       ccg;    
 1577 **                      struct  ccb_abort               cab;
 1578 **                      struct  ccb_resetbus            crb;
 1579 **                      struct  ccb_resetdev            crd;
 1580 **                      struct  ccb_termio              tio;
 1581 **                      struct  ccb_accept_tio          atio;
 1582 **                      struct  ccb_scsiio              ctio;
 1583 **                      struct  ccb_en_lun              cel;
 1584 **                      struct  ccb_immed_notify        cin;
 1585 **                      struct  ccb_notify_ack          cna;
 1586 **                      struct  ccb_eng_inq             cei;
 1587 **                      struct  ccb_eng_exec            cee;
 1588 **                      struct  ccb_rescan              crcn;
 1589 **                      struct  ccb_debug               cdbg;
 1590 **          }
 1591 **
 1592 **struct ccb_hdr {
 1593 **      cam_pinfo           pinfo;                                          "" Info for priority scheduling 
 1594 **      camq_entry          xpt_links;                                  "" For chaining in the XPT layer        
 1595 **      camq_entry          sim_links;                                  "" For chaining in the SIM layer        
 1596 **      camq_entry          periph_links;                               "" For chaining in the type driver 
 1597 **      u_int32_t           retry_count;
 1598 **      void                (*cbfcnp)(struct cam_periph *, union ccb *);"" Callback on completion function 
 1599 **      xpt_opcode          func_code;                                  "" XPT function code 
 1600 **      u_int32_t           status;                                         "" Status returned by CAM subsystem 
 1601 **      struct              cam_path *path;                             "" Compiled path for this ccb 
 1602 **      path_id_t           path_id;                                    "" Path ID for the request 
 1603 **      target_id_t         target_id;                                  "" Target device ID 
 1604 **      lun_id_t            target_lun;                                 "" Target LUN number 
 1605 **      u_int32_t           flags;
 1606 **      ccb_ppriv_area  periph_priv;
 1607 **      ccb_spriv_area  sim_priv;
 1608 **      u_int32_t           timeout;                                    "" Timeout value 
 1609 **      struct              callout_handle timeout_ch;                  "" Callout handle used for timeouts 
 1610 **};
 1611 **
 1612 **typedef union {
 1613 **      u_int8_t  *cdb_ptr;                            "" Pointer to the CDB bytes to send 
 1614 **      u_int8_t  cdb_bytes[IOCDBLEN];         "" Area for the CDB send 
 1615 **} cdb_t;
 1616 **
 1617 ** SCSI I/O Request CCB used for the XPT_SCSI_IO and XPT_CONT_TARGET_IO
 1618 ** function codes.
 1619 **
 1620 **struct ccb_scsiio {
 1621 **      struct     ccb_hdr ccb_h;
 1622 **      union      ccb *next_ccb;                  "" Ptr for next CCB for action 
 1623 **      u_int8_t   *req_map;                       "" Ptr to mapping info 
 1624 **      u_int8_t   *data_ptr;                      "" Ptr to the data buf/SG list 
 1625 **      u_int32_t  dxfer_len;                      "" Data transfer length 
 1626 **      struct     scsi_sense_data sense_data; "" Autosense storage
 1627 **      u_int8_t   sense_len;                      "" Number of bytes to autosense
 1628 **      u_int8_t   cdb_len;                            "" Number of bytes for the CDB 
 1629 **      u_int16_t  sglist_cnt;                     "" Number of SG list entries
 1630 **      u_int8_t   scsi_status;                    "" Returned SCSI status 
 1631 **      u_int8_t   sense_resid;                    "" Autosense resid length: 2's comp 
 1632 **      u_int32_t  resid;                              "" Transfer residual length: 2's comp
 1633 **      cdb_t      cdb_io;                             "" Union for CDB bytes/pointer 
 1634 **      u_int8_t   *msg_ptr;                       "" Pointer to the message buffer
 1635 **      u_int16_t  msg_len;                            "" Number of bytes for the Message 
 1636 **      u_int8_t   tag_action;                     "" What to do for tag queueing 
 1637 **#define       CAM_TAG_ACTION_NONE     0x00       "" The tag action should be either the define below (to send a non-tagged transaction) or one of the defined scsi tag messages from scsi_message.h.
 1638 **      u_int      tag_id;                             "" tag id from initator (target mode) 
 1639 **      u_int      init_id;                            "" initiator id of who selected
 1640 **}
 1641 *********************************************************************
 1642 */
 1643 static void arcmsr_action(struct cam_sim * psim,union ccb * pccb)
 1644 {
 1645         struct _ACB *  pACB;
 1646 
 1647         pACB=(struct _ACB *) cam_sim_softc(psim);
 1648         if(pACB==NULL)
 1649         {
 1650         pccb->ccb_h.status=CAM_REQ_INVALID;
 1651                 xpt_done(pccb);
 1652                 return;
 1653         }
 1654         switch (pccb->ccb_h.func_code) 
 1655         {
 1656         case XPT_SCSI_IO:
 1657                 {
 1658                 struct _SRB * pSRB;
 1659 
 1660                         if((pSRB=arcmsr_get_freesrb(pACB)) == NULL) 
 1661                         {
 1662                                 pccb->ccb_h.status=CAM_RESRC_UNAVAIL;
 1663                                 xpt_done(pccb);
 1664                                 return;
 1665                         }
 1666                         pccb->ccb_h.arcmsr_ccbsrb_ptr=pSRB;
 1667                         pccb->ccb_h.arcmsr_ccbacb_ptr=pACB;
 1668                         pSRB->pccb=pccb;
 1669                         if((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) 
 1670                         {
 1671                                 if((pccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) 
 1672                                 {
 1673                                         if((pccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 
 1674                                         {
 1675                                                 u_int32_t error,s;
 1676 
 1677                                                 s=splsoftvm();
 1678                                                 error = bus_dmamap_load(pACB->buffer_dmat,pSRB->dmamap,pccb->csio.data_ptr,pccb->csio.dxfer_len,arcmsr_executesrb,pSRB,/*flags*/0);
 1679                                         if(error == EINPROGRESS)
 1680                                                 {
 1681                                                         xpt_freeze_simq(pACB->psim,1);
 1682                                                         pccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 1683                                                 }
 1684                                                 splx(s);
 1685                                         } 
 1686                                         else 
 1687                                         {
 1688                                                 struct bus_dma_segment seg;
 1689 
 1690                                                 seg.ds_addr=(bus_addr_t)pccb->csio.data_ptr;
 1691                                                 seg.ds_len=pccb->csio.dxfer_len;
 1692                                                 arcmsr_executesrb(pSRB,&seg,1,0);
 1693                                         }
 1694                                 } 
 1695                                 else 
 1696                                 {
 1697                                         struct bus_dma_segment *segs;
 1698 
 1699                                         if((pccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 || (pccb->ccb_h.flags & CAM_DATA_PHYS) != 0) 
 1700                                         {
 1701                                                 pccb->ccb_h.status=CAM_PROVIDE_FAIL;
 1702                                                 xpt_done(pccb);
 1703                                                 free(pSRB,M_DEVBUF);
 1704                                                 return;
 1705                                         }
 1706                                         segs=(struct bus_dma_segment *)pccb->csio.data_ptr;
 1707                                         arcmsr_executesrb(pSRB,segs,pccb->csio.sglist_cnt,0);
 1708                                 }
 1709                         } 
 1710                         else
 1711                         {
 1712                                 arcmsr_executesrb(pSRB,NULL,0,0);
 1713                         }
 1714                         break;
 1715                 }
 1716         case XPT_TARGET_IO:     
 1717                 {
 1718                         /*
 1719                         ** target mode not yet support vendor specific commands.
 1720                         */
 1721                         pccb->ccb_h.status=CAM_REQ_CMP;
 1722                         xpt_done(pccb);
 1723                         break;
 1724                 }
 1725         case XPT_PATH_INQ:
 1726                 {
 1727                         struct ccb_pathinq *cpi=&pccb->cpi;
 1728 
 1729                         cpi->version_num=1;
 1730                         cpi->hba_inquiry=PI_SDTR_ABLE | PI_TAG_ABLE;
 1731                         cpi->target_sprt=0;
 1732                         cpi->hba_misc=0;
 1733                         cpi->hba_eng_cnt=0;
 1734                         cpi->max_target=ARCMSR_MAX_TARGETID;
 1735                         cpi->max_lun=ARCMSR_MAX_TARGETLUN;      /* 7 or 0 */
 1736                         cpi->initiator_id=ARCMSR_SCSI_INITIATOR_ID;
 1737                         cpi->bus_id=cam_sim_bus(psim);
 1738                         strncpy(cpi->sim_vid,"FreeBSD",SIM_IDLEN);
 1739                         strncpy(cpi->hba_vid,"ARCMSR",HBA_IDLEN);
 1740                         strncpy(cpi->dev_name,cam_sim_name(psim),DEV_IDLEN);
 1741                         cpi->unit_number=cam_sim_unit(psim);
 1742                         cpi->ccb_h.status=CAM_REQ_CMP;
 1743                         xpt_done(pccb);
 1744                         break;
 1745                 }
 1746         case XPT_ABORT: 
 1747                 {
 1748                         union ccb *pabort_ccb;
 1749 
 1750                         pabort_ccb=pccb->cab.abort_ccb;
 1751                         switch (pabort_ccb->ccb_h.func_code) 
 1752                         {
 1753                         case XPT_ACCEPT_TARGET_IO:
 1754                         case XPT_IMMED_NOTIFY:
 1755                         case XPT_CONT_TARGET_IO:
 1756                                 if(arcmsr_seek_cmd2abort(pabort_ccb)==TRUE) 
 1757                                 {
 1758                                         pabort_ccb->ccb_h.status=CAM_REQ_ABORTED;
 1759                                         xpt_done(pabort_ccb);
 1760                                         pccb->ccb_h.status=CAM_REQ_CMP;
 1761                                 } 
 1762                                 else 
 1763                                 {
 1764                                         xpt_print_path(pabort_ccb->ccb_h.path);
 1765                                         printf("Not found\n");
 1766                                         pccb->ccb_h.status=CAM_PATH_INVALID;
 1767                                 }
 1768                                 break;
 1769                         case XPT_SCSI_IO:
 1770                                 pccb->ccb_h.status=CAM_UA_ABORT;
 1771                                 break;
 1772                         default:
 1773                                 pccb->ccb_h.status=CAM_REQ_INVALID;
 1774                                 break;
 1775                         }
 1776                         xpt_done(pccb);
 1777                         break;
 1778                 }
 1779         case XPT_RESET_BUS:
 1780         case XPT_RESET_DEV:
 1781                 {
 1782                         u_int32_t     i;
 1783 
 1784             arcmsr_bus_reset(pACB);
 1785                         for (i=0; i < 500; i++)
 1786                         {
 1787                                 DELAY(1000);    
 1788                         }
 1789                         pccb->ccb_h.status=CAM_REQ_CMP;
 1790                         xpt_done(pccb);
 1791                         break;
 1792                 }
 1793         case XPT_TERM_IO:
 1794                 {
 1795                         pccb->ccb_h.status=CAM_REQ_INVALID;
 1796                         xpt_done(pccb);
 1797                         break;
 1798                 }
 1799         case XPT_GET_TRAN_SETTINGS:
 1800                 {
 1801                         struct ccb_trans_settings *cts;
 1802                         u_int32_t s;
 1803 
 1804                         cts=&pccb->cts;
 1805                         s=splcam();
 1806                         cts->flags=(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
 1807                         cts->sync_period=3;
 1808                         cts->sync_offset=32;
 1809                         cts->bus_width=MSG_EXT_WDTR_BUS_16_BIT;
 1810             cts->valid=CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_SYNC_OFFSET_VALID | CCB_TRANS_BUS_WIDTH_VALID | CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
 1811                         splx(s);
 1812                         pccb->ccb_h.status=CAM_REQ_CMP;
 1813                         xpt_done(pccb);
 1814                         break;
 1815                 }
 1816         case XPT_SET_TRAN_SETTINGS:
 1817                 {
 1818                     pccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
 1819                     xpt_done(pccb);
 1820                         break;
 1821                 }
 1822         case XPT_CALC_GEOMETRY:
 1823                 {
 1824                         struct ccb_calc_geometry *ccg;
 1825                         u_int32_t size_mb;
 1826                         u_int32_t secs_per_cylinder;
 1827 
 1828                         ccg=&pccb->ccg;
 1829                         size_mb=ccg->volume_size/((1024L * 1024L)/ccg->block_size);
 1830                         if(size_mb > 1024 ) 
 1831                         {
 1832                                 ccg->heads=255;
 1833                                 ccg->secs_per_track=63;
 1834                         } 
 1835                         else 
 1836                         {
 1837                                 ccg->heads=64;
 1838                                 ccg->secs_per_track=32;
 1839                         }
 1840                         secs_per_cylinder=ccg->heads * ccg->secs_per_track;
 1841                         ccg->cylinders=ccg->volume_size / secs_per_cylinder;
 1842                         pccb->ccb_h.status=CAM_REQ_CMP;
 1843                         xpt_done(pccb);
 1844                         break;
 1845                 }
 1846         default:
 1847                 printf("arcmsr%d: invalid XPT function CAM_REQ_INVALID\n",pACB->pci_unit);
 1848         pccb->ccb_h.status=CAM_REQ_INVALID;
 1849                 xpt_done(pccb);
 1850                 break;
 1851         }
 1852         return;
 1853 }
 1854 /*
 1855 **********************************************************************
 1856 **  start background rebulid
 1857 **********************************************************************
 1858 */
 1859 static void arcmsr_start_adapter_bgrb(struct _ACB * pACB)
 1860 {
 1861         pACB->acb_flags |= ACB_F_MSG_START_BGRB;
 1862         pACB->acb_flags &= ~ACB_F_MSG_STOP_BGRB;
 1863     writel(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_START_BGRB);
 1864         return;
 1865 }
 1866 /*
 1867 **********************************************************************
 1868 **********************************************************************
 1869 */
 1870 static void arcmsr_polling_srbdone(struct _ACB *pACB,struct _SRB *poll_srb)
 1871 {
 1872         struct _SRB *pSRB;
 1873         uint32_t flag_srb,outbound_intstatus,poll_srb_done=0,poll_count=0;
 1874     int id,lun;
 1875 
 1876 polling_srb_retry:
 1877         poll_count++;
 1878         outbound_intstatus=readl(&pACB->pmu->outbound_intstatus) & pACB->outbound_int_enable;
 1879         writel(&pACB->pmu->outbound_intstatus,outbound_intstatus);/*clear interrupt*/
 1880         while(1)
 1881         {
 1882                 if((flag_srb=readl(&pACB->pmu->outbound_queueport))==0xFFFFFFFF)
 1883                 {
 1884                         if(poll_srb_done)
 1885                         {
 1886                                 break;/*chip FIFO no ccb for completion already*/
 1887                         }
 1888                         else
 1889                         {
 1890                 UDELAY(25000);
 1891                 if(poll_count > 100)
 1892                                 {
 1893                     break;
 1894                                 }
 1895                                 goto polling_srb_retry;
 1896                         }
 1897                 }
 1898                 /* check ifcommand done with no error*/
 1899                 pSRB=(struct _SRB *)(pACB->vir2phy_offset+(flag_srb << 5));/*frame must be 32 bytes aligned*/
 1900                 if((pSRB->pACB!=pACB) || (pSRB->startdone!=ARCMSR_SRB_START))
 1901                 {
 1902                         if((pSRB->startdone==ARCMSR_SRB_ABORTED) && (pSRB==poll_srb))
 1903                         {
 1904                                 printf("arcmsr%d: scsi id=%d lun=%d srb='%p' poll command abort successfully \n",pACB->pci_unit,pSRB->pccb->ccb_h.target_id,pSRB->pccb->ccb_h.target_lun,pSRB);
 1905                                 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
 1906                                 arcmsr_srb_complete(pSRB);
 1907                                 poll_srb_done=1;
 1908                                 continue;
 1909                         }
 1910                         printf("arcmsr%d: polling get an illegal srb command done srb='%p' srboutstandingcount=%d \n",pACB->pci_unit,pSRB,pACB->srboutstandingcount);
 1911                         continue;
 1912                 }
 1913                 id=pSRB->pccb->ccb_h.target_id;
 1914                 lun=pSRB->pccb->ccb_h.target_lun;
 1915                 if((flag_srb & ARCMSR_SRBREPLY_FLAG_ERROR)==0)
 1916                 {
 1917                         if(pACB->devstate[id][lun]==ARECA_RAID_GONE)
 1918                         {
 1919                 pACB->devstate[id][lun]=ARECA_RAID_GOOD;
 1920                         }
 1921                         pSRB->pccb->ccb_h.status=CAM_REQ_CMP;
 1922                         arcmsr_srb_complete(pSRB);
 1923                 } 
 1924                 else 
 1925                 {   
 1926                         switch(pSRB->arcmsr_cdb.DeviceStatus)
 1927                         {
 1928                         case ARCMSR_DEV_SELECT_TIMEOUT:
 1929                                 {
 1930                                         pACB->devstate[id][lun]=ARECA_RAID_GONE;
 1931                                         pSRB->pccb->ccb_h.status=CAM_SEL_TIMEOUT;
 1932                                         arcmsr_srb_complete(pSRB);
 1933                                 }
 1934                                 break;
 1935                         case ARCMSR_DEV_ABORTED:
 1936                         case ARCMSR_DEV_INIT_FAIL:
 1937                                 {
 1938                                     pACB->devstate[id][lun]=ARECA_RAID_GONE;
 1939                                         pSRB->pccb->ccb_h.status=CAM_DEV_NOT_THERE;
 1940                                         arcmsr_srb_complete(pSRB);
 1941                                 }
 1942                                 break;
 1943                         case SCSISTAT_CHECK_CONDITION:
 1944                                 {
 1945                                     pACB->devstate[id][lun]=ARECA_RAID_GOOD;
 1946                                 arcmsr_report_sense_info(pSRB);
 1947                                         arcmsr_srb_complete(pSRB);
 1948                                 }
 1949                                 break;
 1950                         default:
 1951                                 /* error occur Q all error ccb to errorccbpending Q*/
 1952                             printf("arcmsr%d: scsi id=%d lun=%d polling and getting command error done, but got unknow DeviceStatus=0x%x \n",pACB->pci_unit,id,lun,pSRB->arcmsr_cdb.DeviceStatus);
 1953                                 pACB->devstate[id][lun]=ARECA_RAID_GONE;
 1954                                 pSRB->pccb->ccb_h.status=CAM_UNCOR_PARITY;/*unknow error or crc error just for retry*/
 1955                                 arcmsr_srb_complete(pSRB);
 1956                                 break;
 1957                         }
 1958                 }
 1959         }       /*drain reply FIFO*/
 1960         return;
 1961 }
 1962 /*
 1963 **********************************************************************
 1964 **  get firmware miscellaneous data
 1965 **********************************************************************
 1966 */
 1967 static void arcmsr_get_firmware_spec(struct _ACB *pACB)
 1968 {
 1969     char *acb_firm_model=pACB->firm_model;
 1970     char *acb_firm_version=pACB->firm_version;
 1971     char *iop_firm_model=(char *) (&pACB->pmu->message_rwbuffer[15]);    /*firm_model,15,60-67*/
 1972     char *iop_firm_version=(char *) (&pACB->pmu->message_rwbuffer[17]);  /*firm_version,17,68-83*/
 1973         int count;
 1974 
 1975     writel(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_GET_CONFIG);
 1976         if(arcmsr_wait_msgint_ready(pACB))
 1977         {
 1978                 printf("arcmsr%d: wait 'get adapter firmware miscellaneous data' timeout \n",pACB->pci_unit);
 1979         }
 1980         count=8;
 1981         while(count)
 1982         {
 1983         *acb_firm_model=readb(iop_firm_model);
 1984         acb_firm_model++;
 1985                 iop_firm_model++;
 1986                 count--;
 1987         }
 1988         count=16;
 1989         while(count)
 1990         {
 1991         *acb_firm_version=readb(iop_firm_version);
 1992         acb_firm_version++;
 1993                 iop_firm_version++;
 1994                 count--;
 1995         }
 1996         printf("ARECA RAID ADAPTER%d: FIRMWARE VERSION %s \n",pACB->pci_unit,pACB->firm_version);
 1997         if(strncmp(pACB->firm_version,"V1.37",5) < 0)
 1998         {
 1999         printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
 2000         printf("!!!!!!   PLEASE UPDATE RAID FIRMWARE VERSION EQUAL OR MORE THAN 'V1.37'   !!!!!!\n");
 2001                 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
 2002         }
 2003     pACB->firm_request_len=readl(&pACB->pmu->message_rwbuffer[1]);   /*firm_request_len,1,04-07*/
 2004         pACB->firm_numbers_queue=readl(&pACB->pmu->message_rwbuffer[2]); /*firm_numbers_queue,2,08-11*/
 2005         pACB->firm_sdram_size=readl(&pACB->pmu->message_rwbuffer[3]);    /*firm_sdram_size,3,12-15*/
 2006     pACB->firm_ide_channels=readl(&pACB->pmu->message_rwbuffer[4]);  /*firm_ide_channels,4,16-19*/
 2007         return;
 2008 }
 2009 /*
 2010 **********************************************************************
 2011 **  start background rebulid
 2012 **********************************************************************
 2013 */
 2014 static void arcmsr_iop_init(struct _ACB * pACB)
 2015 {
 2016     u_int32_t intmask_org,mask,outbound_doorbell,firmware_state=0;
 2017 
 2018         do
 2019         {
 2020         firmware_state=readl(&pACB->pmu->outbound_msgaddr1);
 2021         }while((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK)==0);
 2022     intmask_org=readl(&pACB->pmu->outbound_intmask);
 2023         arcmsr_get_firmware_spec(pACB);
 2024         /*start background rebuild*/
 2025         arcmsr_start_adapter_bgrb(pACB);
 2026         if(arcmsr_wait_msgint_ready(pACB))
 2027         {
 2028                 printf("arcmsr%d: iop init wait 'start adapter background rebulid' timeout \n",pACB->pci_unit);
 2029         }
 2030         /* clear Qbuffer if door bell ringed */
 2031         outbound_doorbell=readl(&pACB->pmu->outbound_doorbell);
 2032         if(outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK)
 2033         {
 2034                 writel(&pACB->pmu->outbound_doorbell,outbound_doorbell);/*clear interrupt */
 2035         writel(&pACB->pmu->inbound_doorbell,ARCMSR_INBOUND_DRIVER_DATA_READ_OK);
 2036         }
 2037         /* enable outbound Post Queue,outbound message0,outbell doorbell Interrupt */
 2038         mask=~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE|ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
 2039     writel(&pACB->pmu->outbound_intmask,intmask_org & mask);
 2040         pACB->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
 2041         pACB->acb_flags |=ACB_F_IOP_INITED;
 2042         return;
 2043 }
 2044 /*
 2045 **********************************************************************
 2046 ** 
 2047 **  map freesrb
 2048 **
 2049 **********************************************************************
 2050 */
 2051 static void arcmsr_map_freesrb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 2052 {
 2053         struct _ACB * pACB=arg;
 2054         struct _SRB * psrb_tmp;
 2055     u_int8_t * dma_memptr;
 2056         u_int32_t i,cdb_phyaddr,srb_phyaddr_hi32;
 2057         unsigned long srb_phyaddr=(unsigned long)segs->ds_addr;
 2058 
 2059     dma_memptr=pACB->uncacheptr;
 2060         cdb_phyaddr=(u_int32_t)segs->ds_addr; /* We suppose bus_addr_t high part always 0 here*/
 2061         if(((unsigned long)dma_memptr & 0x1F)!=0)
 2062         {
 2063                 dma_memptr=dma_memptr+(0x20-((unsigned long)dma_memptr & 0x1F));
 2064                 cdb_phyaddr=cdb_phyaddr+(0x20-((unsigned long)cdb_phyaddr & 0x1F));
 2065         }
 2066     psrb_tmp=(struct _SRB *)dma_memptr;
 2067         for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
 2068         {
 2069                 if(((unsigned long)psrb_tmp & 0x1F)==0) /*srb address must 32 (0x20) boundary*/
 2070                 {
 2071             if(bus_dmamap_create(pACB->buffer_dmat, /*flags*/0, &psrb_tmp->dmamap)!=0)
 2072                         {
 2073                                 pACB->acb_flags |= ACB_F_MAPFREESRB_FAILD;
 2074                             printf("arcmsr%d: srb dmamap bus_dmamap_create error\n",pACB->pci_unit);
 2075                             return;
 2076                         }
 2077                         psrb_tmp->cdb_shifted_phyaddr=cdb_phyaddr >> 5;
 2078             psrb_tmp->pACB=pACB;
 2079                         pACB->psrbringQ[i]=pACB->psrb_pool[i]=psrb_tmp;
 2080                         cdb_phyaddr=cdb_phyaddr+sizeof(struct _SRB);
 2081                 }
 2082                 else
 2083                 {
 2084                         pACB->acb_flags |= ACB_F_MAPFREESRB_FAILD;
 2085                         printf("arcmsr%d: dma_memptr=%p i=%d this srb cross 32 bytes boundary ignored psrb_tmp=%p \n",pACB->pci_unit,dma_memptr,i,psrb_tmp);
 2086                         return;
 2087                 }
 2088                 psrb_tmp++;
 2089         }
 2090         /*
 2091         ********************************************************************
 2092         ** here we need to tell iop 331 our freesrb.HighPart 
 2093         ** if freesrb.HighPart is not zero
 2094         ********************************************************************
 2095         */
 2096         srb_phyaddr_hi32=(uint32_t) ((srb_phyaddr>>16)>>16);
 2097         if(srb_phyaddr_hi32!=0)
 2098         {
 2099         writel(&pACB->pmu->message_rwbuffer[0],ARCMSR_SIGNATURE_SET_CONFIG);
 2100         writel(&pACB->pmu->message_rwbuffer[1],srb_phyaddr_hi32);
 2101                 writel(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_SET_CONFIG);
 2102                 if(arcmsr_wait_msgint_ready(pACB))
 2103                 {
 2104                         printf("arcmsr%d: 'set srb high part physical address' timeout \n",pACB->pci_unit);
 2105                 }
 2106         }
 2107         pACB->vir2phy_offset=(unsigned long)psrb_tmp-(unsigned long)cdb_phyaddr;
 2108     return;
 2109 }
 2110 /*
 2111 ************************************************************************
 2112 **
 2113 **
 2114 ************************************************************************
 2115 */
 2116 static void arcmsr_free_resource(struct _ACB * pACB)
 2117 {
 2118         /* remove the control device */
 2119         if(pACB->ioctl_dev != NULL)
 2120         {
 2121                 destroy_dev(pACB->ioctl_dev);
 2122         }
 2123     bus_dmamap_unload(pACB->srb_dmat, pACB->srb_dmamap);
 2124     bus_dmamap_destroy(pACB->srb_dmat, pACB->srb_dmamap);
 2125     bus_dma_tag_destroy(pACB->srb_dmat);
 2126         bus_dma_tag_destroy(pACB->buffer_dmat);
 2127         bus_dma_tag_destroy(pACB->parent_dmat);
 2128         return;
 2129 }
 2130 /*
 2131 ************************************************************************
 2132 ** PCI config header registers for all devices 
 2133 **
 2134 ** #define PCIR_COMMAND         0x04
 2135 ** #define PCIM_CMD_PORTEN              0x0001
 2136 ** #define PCIM_CMD_MEMEN               0x0002
 2137 ** #define PCIM_CMD_BUSMASTEREN 0x0004
 2138 ** #define PCIM_CMD_MWRICEN         0x0010
 2139 ** #define PCIM_CMD_PERRESPEN   0x0040    
 2140 **        
 2141 ** Function      : arcmsr_initialize 
 2142 ** Purpose       : initialize the internal structures for a given SCSI host
 2143 ** Inputs        : host - pointer to this host adapter's structure
 2144 ** Preconditions : when this function is called,the chip_type
 2145 **                     field of the pACB structure MUST have been set.
 2146 **
 2147 ** 10h Base Address register #0
 2148 ** 14h Base Address register #1
 2149 ** 18h Base Address register #2
 2150 ** 1Ch Base Address register #3
 2151 ** 20h Base Address register #4
 2152 ** 24h Base Address register #5
 2153 ************************************************************************
 2154 */
 2155 static u_int32_t arcmsr_initialize(device_t dev)
 2156 {
 2157         struct _ACB * pACB=device_get_softc(dev);
 2158         u_int32_t intmask_org,rid=PCIR_BAR(0);
 2159         vm_offset_t     mem_base;
 2160         u_int16_t pci_command;
 2161         int i,j;
 2162 
 2163 #if __FreeBSD_version >= 502010
 2164         if(bus_dma_tag_create( /*parent*/NULL, 
 2165                                     /*alignemnt*/1, 
 2166                                                         /*boundary*/0,
 2167                                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
 2168                                                 /*highaddr*/BUS_SPACE_MAXADDR,
 2169                                                 /*filter*/NULL, 
 2170                                                         /*filterarg*/NULL,
 2171                                                 /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
 2172                                                 /*nsegments*/BUS_SPACE_UNRESTRICTED,
 2173                                                 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
 2174                                                 /*flags*/0, 
 2175                                                         /*lockfunc*/NULL,
 2176                                                         /*lockarg*/NULL,
 2177                                                         &pACB->parent_dmat) != 0) 
 2178 #else
 2179         if(bus_dma_tag_create( /*parent*/NULL, 
 2180                                     /*alignemnt*/1, 
 2181                                                         /*boundary*/0,
 2182                                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
 2183                                                 /*highaddr*/BUS_SPACE_MAXADDR,
 2184                                                 /*filter*/NULL, 
 2185                                                         /*filterarg*/NULL,
 2186                                                 /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
 2187                                                 /*nsegments*/BUS_SPACE_UNRESTRICTED,
 2188                                                 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
 2189                                                 /*flags*/0, 
 2190                                                         &pACB->parent_dmat) != 0) 
 2191 #endif
 2192         {
 2193                 printf("arcmsr%d: parent_dmat bus_dma_tag_create failure!\n",pACB->pci_unit);
 2194                 return ENOMEM;
 2195         }
 2196     /* Create a single tag describing a region large enough to hold all of the s/g lists we will need. */
 2197 #if __FreeBSD_version >= 502010
 2198         if(bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat,
 2199                                    /*alignment*/1,
 2200                                        /*boundary*/0,
 2201                                        /*lowaddr*/BUS_SPACE_MAXADDR,
 2202                                        /*highaddr*/BUS_SPACE_MAXADDR,
 2203                                        /*filter*/NULL,
 2204                                                    /*filterarg*/NULL,
 2205                                        /*maxsize*/MAXBSIZE,
 2206                                                    /*nsegments*/ARCMSR_MAX_SG_ENTRIES,
 2207                                        /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
 2208                                        /*flags*/BUS_DMA_ALLOCNOW,
 2209                                                    /*lockfunc*/busdma_lock_mutex,
 2210                                                    /*lockarg*/&Giant,
 2211                                        &pACB->buffer_dmat) != 0) 
 2212 #else
 2213         if(bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat,
 2214                                    /*alignment*/1,
 2215                                        /*boundary*/0,
 2216                                        /*lowaddr*/BUS_SPACE_MAXADDR,
 2217                                        /*highaddr*/BUS_SPACE_MAXADDR,
 2218                                        /*filter*/NULL,
 2219                                                    /*filterarg*/NULL,
 2220                                        /*maxsize*/MAXBSIZE,
 2221                                                    /*nsegments*/ARCMSR_MAX_SG_ENTRIES,
 2222                                        /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
 2223                                        /*flags*/BUS_DMA_ALLOCNOW,
 2224                                        &pACB->buffer_dmat) != 0) 
 2225 #endif
 2226         {
 2227                 bus_dma_tag_destroy(pACB->parent_dmat);
 2228                 printf("arcmsr%d: buffer_dmat bus_dma_tag_create failure!\n",pACB->pci_unit);
 2229                 return ENOMEM;
 2230     }
 2231         /* DMA tag for our srb structures.... Allocate the freesrb memory */
 2232 #if __FreeBSD_version >= 502010
 2233         if(bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat, 
 2234                                     /*alignment*/1, 
 2235                                     /*boundary*/0,
 2236                                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
 2237                                                 /*highaddr*/BUS_SPACE_MAXADDR,
 2238                                                 /*filter*/NULL, 
 2239                                                         /*filterarg*/NULL,
 2240                                                 /*maxsize*/((sizeof(struct _SRB) * ARCMSR_MAX_FREESRB_NUM)+0x20),
 2241                                                 /*nsegments*/1,
 2242                                                 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
 2243                                                 /*flags*/BUS_DMA_ALLOCNOW,
 2244                                                         /*lockfunc*/NULL,
 2245                                                         /*lockarg*/NULL,
 2246                                                         &pACB->srb_dmat) != 0) 
 2247 #else
 2248         if(bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat, 
 2249                                     /*alignment*/1, 
 2250                                     /*boundary*/0,
 2251                                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
 2252                                                 /*highaddr*/BUS_SPACE_MAXADDR,
 2253                                                 /*filter*/NULL, 
 2254                                                         /*filterarg*/NULL,
 2255                                                 /*maxsize*/((sizeof(struct _SRB) * ARCMSR_MAX_FREESRB_NUM)+0x20),
 2256                                                 /*nsegments*/1,
 2257                                                 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
 2258                                                 /*flags*/BUS_DMA_ALLOCNOW,
 2259                                                         &pACB->srb_dmat) != 0) 
 2260 #endif
 2261         {
 2262                 bus_dma_tag_destroy(pACB->buffer_dmat);
 2263                 bus_dma_tag_destroy(pACB->parent_dmat);
 2264                 printf("arcmsr%d: srb_dmat bus_dma_tag_create failure!\n",pACB->pci_unit);
 2265                 return ENXIO;
 2266     }
 2267         /* Allocation for our srbs */
 2268         if(bus_dmamem_alloc(pACB->srb_dmat, (void **)&pACB->uncacheptr, BUS_DMA_WAITOK | BUS_DMA_COHERENT, &pACB->srb_dmamap) != 0) 
 2269         {
 2270         bus_dma_tag_destroy(pACB->srb_dmat);
 2271                 bus_dma_tag_destroy(pACB->buffer_dmat);
 2272                 bus_dma_tag_destroy(pACB->parent_dmat);
 2273                 printf("arcmsr%d: srb_dmat bus_dmamem_alloc failure!\n",pACB->pci_unit);
 2274                 return ENXIO;
 2275         }
 2276         /* And permanently map them */
 2277         if(bus_dmamap_load(pACB->srb_dmat, pACB->srb_dmamap,pACB->uncacheptr,(sizeof(struct _SRB) * ARCMSR_MAX_FREESRB_NUM)+0x20,arcmsr_map_freesrb, pACB, /*flags*/0))
 2278         {
 2279         bus_dma_tag_destroy(pACB->srb_dmat);
 2280                 bus_dma_tag_destroy(pACB->buffer_dmat);
 2281                 bus_dma_tag_destroy(pACB->parent_dmat);
 2282                 printf("arcmsr%d: srb_dmat bus_dmamap_load failure!\n",pACB->pci_unit);
 2283                 return ENXIO;
 2284         }
 2285         pci_command=pci_read_config(dev,PCIR_COMMAND,2);
 2286         pci_command |= PCIM_CMD_BUSMASTEREN;
 2287         pci_command |= PCIM_CMD_PERRESPEN;
 2288         pci_command |= PCIM_CMD_MWRICEN;
 2289         /* Enable Busmaster/Mem */
 2290         pci_command |= PCIM_CMD_MEMEN;
 2291         pci_write_config(dev,PCIR_COMMAND,pci_command,2);
 2292         pACB->sys_res_arcmsr=bus_alloc_resource(dev,SYS_RES_MEMORY,&rid,0,~0,0x1000,RF_ACTIVE);
 2293         if(pACB->sys_res_arcmsr == NULL)
 2294         {
 2295                 arcmsr_free_resource(pACB);
 2296                 printf("arcmsr%d: bus_alloc_resource failure!\n",pACB->pci_unit);
 2297                 return ENOMEM;
 2298         }
 2299         if(rman_get_start(pACB->sys_res_arcmsr) <= 0)
 2300         {
 2301                 arcmsr_free_resource(pACB);
 2302                 printf("arcmsr%d: rman_get_start failure!\n",pACB->pci_unit);
 2303         return ENXIO;
 2304         }
 2305         mem_base=(vm_offset_t) rman_get_virtual(pACB->sys_res_arcmsr);
 2306         if(mem_base==0)
 2307         {
 2308                 arcmsr_free_resource(pACB);
 2309                 printf("arcmsr%d: rman_get_virtual failure!\n",pACB->pci_unit);
 2310                 return ENXIO;
 2311         }
 2312         if(pACB->acb_flags & ACB_F_MAPFREESRB_FAILD)
 2313         {
 2314                 arcmsr_free_resource(pACB);
 2315                 printf("arcmsr%d: map free srb failure!\n",pACB->pci_unit);
 2316                 return ENXIO;
 2317         }
 2318         pACB->btag=rman_get_bustag(pACB->sys_res_arcmsr);
 2319         pACB->bhandle=rman_get_bushandle(pACB->sys_res_arcmsr);
 2320     pACB->pmu=(struct _MU *)mem_base;
 2321     pACB->acb_flags |= (ACB_F_IOCTL_WQBUFFER_CLEARED|ACB_F_IOCTL_RQBUFFER_CLEARED);
 2322         pACB->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
 2323         /*
 2324         ********************************************************************
 2325         ** init raid volume state
 2326         ********************************************************************
 2327         */
 2328         for(i=0;i<ARCMSR_MAX_TARGETID;i++)
 2329         {
 2330                 for(j=0;j<ARCMSR_MAX_TARGETLUN;j++)
 2331                 {
 2332                         pACB->devstate[i][j]=ARECA_RAID_GOOD;
 2333                 }
 2334         }
 2335     /* disable iop all outbound interrupt */
 2336     intmask_org=readl(&pACB->pmu->outbound_intmask);
 2337     writel(&pACB->pmu->outbound_intmask,intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE);
 2338         arcmsr_iop_init(pACB);
 2339     return(0);
 2340 }
 2341 /*
 2342 ************************************************************************
 2343 **
 2344 **        attach and init a host adapter               
 2345 **
 2346 ************************************************************************
 2347 */
 2348 static u_int32_t arcmsr_attach(device_t dev)
 2349 {
 2350         struct _ACB * pACB=(struct _ACB *)device_get_softc(dev);
 2351         u_int32_t unit=device_get_unit(dev);
 2352         struct ccb_setasync csa;
 2353         struct cam_devq *devq;  /* Device Queue to use for this SIM */
 2354         struct resource *irqres;
 2355         int     rid;
 2356 
 2357         if(pACB == NULL) 
 2358         {
 2359                 printf("arcmsr%d: cannot allocate softc\n",unit);
 2360                 return (ENOMEM);
 2361         }
 2362         bzero(pACB, sizeof(struct _ACB));
 2363         if(arcmsr_initialize(dev)) 
 2364         {
 2365                 printf("arcmsr%d: initialize failure!\n",unit);
 2366                 return ENXIO;
 2367         }
 2368         /* After setting up the adapter,map our interrupt */
 2369         rid=0;
 2370         irqres=bus_alloc_resource(dev,SYS_RES_IRQ,&rid,0,~0,1,RF_SHAREABLE | RF_ACTIVE);
 2371         if(irqres == NULL || bus_setup_intr(dev,irqres,INTR_TYPE_CAM,arcmsr_interrupt,pACB,&pACB->ih)) 
 2372         {
 2373                 arcmsr_free_resource(pACB);
 2374                 printf("arcmsr%d: unable to register interrupt handler!\n",unit);
 2375                 return ENXIO;
 2376         }
 2377         pACB->irqres=irqres;
 2378         pACB->pci_dev=dev;
 2379         pACB->pci_unit=unit;
 2380         /*
 2381          * Now let the CAM generic SCSI layer find the SCSI devices on
 2382          * the bus *  start queue to reset to the idle loop. *
 2383          * Create device queue of SIM(s) *  (MAX_START_JOB - 1) :
 2384          * max_sim_transactions
 2385         */
 2386         devq=cam_simq_alloc(ARCMSR_MAX_START_JOB);
 2387         if(devq == NULL) 
 2388         {
 2389             arcmsr_free_resource(pACB);
 2390                 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
 2391                 printf("arcmsr%d: cam_simq_alloc failure!\n",unit);
 2392                 return ENXIO;
 2393         }
 2394         pACB->psim=cam_sim_alloc(arcmsr_action,arcmsr_poll,"arcmsr",pACB,unit,1,ARCMSR_MAX_OUTSTANDING_CMD,devq);
 2395         if(pACB->psim == NULL) 
 2396         {
 2397                 arcmsr_free_resource(pACB);
 2398                 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
 2399                 cam_simq_free(devq);
 2400                 printf("arcmsr%d: cam_sim_alloc failure!\n",unit);
 2401                 return ENXIO;
 2402         }
 2403         if(xpt_bus_register(pACB->psim,0) != CAM_SUCCESS) 
 2404         {
 2405                 arcmsr_free_resource(pACB);
 2406                 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
 2407                 cam_sim_free(pACB->psim,/*free_devq*/TRUE);
 2408                 printf("arcmsr%d: xpt_bus_register failure!\n",unit);
 2409                 return ENXIO;
 2410         }
 2411         if(xpt_create_path(&pACB->ppath,/* periph */ NULL,cam_sim_path(pACB->psim),CAM_TARGET_WILDCARD,CAM_LUN_WILDCARD) != CAM_REQ_CMP) 
 2412         {
 2413                 arcmsr_free_resource(pACB);
 2414                 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
 2415                 xpt_bus_deregister(cam_sim_path(pACB->psim));
 2416                 cam_sim_free(pACB->psim,/* free_simq */ TRUE);
 2417                 printf("arcmsr%d: xpt_create_path failure!\n",unit);
 2418                 return ENXIO;
 2419         }
 2420     /*
 2421         ****************************************************
 2422         */
 2423         xpt_setup_ccb(&csa.ccb_h,pACB->ppath,/*priority*/5);
 2424         csa.ccb_h.func_code=XPT_SASYNC_CB;
 2425         csa.event_enable=AC_FOUND_DEVICE|AC_LOST_DEVICE;
 2426         csa.callback=arcmsr_async;
 2427         csa.callback_arg=pACB->psim;
 2428         xpt_action((union ccb *)&csa);
 2429     /* Create the control device.  */
 2430     pACB->ioctl_dev=make_dev(&arcmsr_cdevsw, unit, UID_ROOT, GID_WHEEL /* GID_OPERATOR */, S_IRUSR | S_IWUSR, "arcmsr%d", unit);
 2431 #if __FreeBSD_version < 503000
 2432         pACB->ioctl_dev->si_drv1=pACB;
 2433 #endif
 2434 #if __FreeBSD_version > 500005
 2435         (void)make_dev_alias(pACB->ioctl_dev, "arc%d", unit);
 2436 #endif
 2437         return 0;
 2438 }
 2439 /*
 2440 ************************************************************************
 2441 **
 2442 **                     
 2443 **
 2444 ************************************************************************
 2445 */
 2446 static u_int32_t arcmsr_probe(device_t dev)
 2447 {
 2448         u_int32_t id;
 2449 
 2450     switch(id=pci_get_devid(dev))
 2451         {
 2452         case PCIDevVenIDARC1110:
 2453                 device_set_desc(dev,"ARECA ARC1110 PCI-X 4 PORTS SATA RAID CONTROLLER \n" ARCMSR_DRIVER_VERSION );
 2454             return 0;
 2455     case PCIDevVenIDARC1120:
 2456                 device_set_desc(dev,"ARECA ARC1120 PCI-X 8 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2457                 return 0;
 2458     case PCIDevVenIDARC1130:
 2459                 device_set_desc(dev,"ARECA ARC1130 PCI-X 12 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2460                 return 0;
 2461     case PCIDevVenIDARC1160:
 2462                 device_set_desc(dev,"ARECA ARC1160 PCI-X 16 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2463                 return 0;
 2464     case PCIDevVenIDARC1170:
 2465                 device_set_desc(dev,"ARECA ARC1170 PCI-X 24 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2466                 return 0;
 2467     case PCIDevVenIDARC1210:
 2468                 device_set_desc(dev,"ARECA ARC1210 PCI-EXPRESS 4 PORTS SATA RAID CONTROLLER \n" ARCMSR_DRIVER_VERSION);
 2469                 return 0;
 2470     case PCIDevVenIDARC1220:
 2471                 device_set_desc(dev,"ARECA ARC1220 PCI-EXPRESS 8 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2472                 return 0;
 2473    case PCIDevVenIDARC1230:
 2474                 device_set_desc(dev,"ARECA ARC1230 PCI-EXPRESS 12 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2475                 return 0;
 2476     case PCIDevVenIDARC1260:
 2477                 device_set_desc(dev,"ARECA ARC1260 PCI-EXPRESS 16 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2478     case PCIDevVenIDARC1270:
 2479                 device_set_desc(dev,"ARECA ARC1270 PCI-EXPRESS 24 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
 2480                 return 0;
 2481         }
 2482         return ENXIO;
 2483 }
 2484 /*
 2485 ************************************************************************
 2486 **
 2487 **                     
 2488 **
 2489 ************************************************************************
 2490 */
 2491 static void arcmsr_shutdown(device_t dev)
 2492 {
 2493         u_int32_t  i,poll_count=0;
 2494         u_int32_t s,intmask_org;
 2495         struct _SRB * pSRB;
 2496     struct _ACB * pACB=(struct _ACB *)device_get_softc(dev);
 2497 
 2498         s=splcam();
 2499     /* disable all outbound interrupt */
 2500     intmask_org=readl(&pACB->pmu->outbound_intmask);
 2501     writel(&pACB->pmu->outbound_intmask,(intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE));
 2502         /* stop adapter background rebuild */
 2503         arcmsr_stop_adapter_bgrb(pACB);
 2504         if(arcmsr_wait_msgint_ready(pACB))
 2505         {
 2506                 printf("arcmsr%d: shutdown wait 'stop adapter rebulid' timeout \n",pACB->pci_unit);
 2507         }
 2508         arcmsr_flush_adapter_cache(pACB);
 2509         if(arcmsr_wait_msgint_ready(pACB))
 2510         {
 2511                 printf("arcmsr%d: shutdown wait 'flush adapter cache' timeout \n",pACB->pci_unit);
 2512         }
 2513         /* abort all outstanding command */
 2514         pACB->acb_flags |= ACB_F_SCSISTOPADAPTER;
 2515         pACB->acb_flags &= ~ACB_F_IOP_INITED;
 2516         if(pACB->srboutstandingcount!=0)
 2517         {  
 2518                 while((pACB->srboutstandingcount!=0) && (poll_count < 256))
 2519                 {
 2520                         arcmsr_interrupt((void *)pACB);
 2521             UDELAY(25000);
 2522                         poll_count++;
 2523                 }
 2524                 if(pACB->srboutstandingcount!=0)
 2525                 {
 2526                         printf("arcmsr%d: shutdown srboutstandingcount!=0 \n",pACB->pci_unit);
 2527                         arcmsr_abort_allcmd(pACB);
 2528                         if(arcmsr_wait_msgint_ready(pACB))
 2529                         {
 2530                                 printf("arcmsr%d: shutdown wait 'abort all outstanding command' timeout \n",pACB->pci_unit);
 2531                         }
 2532                         for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
 2533                         {
 2534                         pSRB=pACB->psrb_pool[i];
 2535                                 if(pSRB->startdone==ARCMSR_SRB_START)
 2536                                 {
 2537                                         pSRB->startdone=ARCMSR_SRB_ABORTED;
 2538                                         pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
 2539                                         arcmsr_srb_complete(pSRB);
 2540                                 }
 2541                         }
 2542                 }
 2543         }
 2544         if(pACB->srbwait2gocount!=0)
 2545         {       /*remove first wait2go srb and abort it*/
 2546                 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
 2547                 {
 2548                         pSRB=pACB->psrbwait2go[i];
 2549                         if(pSRB!=NULL)
 2550                         {
 2551                                 pACB->psrbwait2go[i]=NULL;
 2552                 pSRB->startdone=ARCMSR_SRB_ABORTED;
 2553                                 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED; 
 2554                                 arcmsr_srb_complete(pSRB);
 2555                                 atomic_subtract_int(&pACB->srbwait2gocount,1);
 2556                         }
 2557                 }
 2558         }
 2559         atomic_set_int(&pACB->srboutstandingcount,0);
 2560         splx(s);
 2561     return;
 2562 }
 2563 /*
 2564 ************************************************************************
 2565 **
 2566 **                     
 2567 **
 2568 ************************************************************************
 2569 */
 2570 static u_int32_t arcmsr_detach(device_t dev)
 2571 {
 2572         struct _ACB * pACB=(struct _ACB *)device_get_softc(dev);
 2573 
 2574         arcmsr_shutdown(dev);
 2575         arcmsr_free_resource(pACB);
 2576         bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), pACB->sys_res_arcmsr);
 2577         bus_teardown_intr(dev, pACB->irqres, pACB->ih);
 2578         bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
 2579         xpt_async(AC_LOST_DEVICE, pACB->ppath, NULL);
 2580         xpt_free_path(pACB->ppath);
 2581         xpt_bus_deregister(cam_sim_path(pACB->psim));
 2582         cam_sim_free(pACB->psim, TRUE);
 2583         return (0);
 2584 }
 2585 
 2586 
 2587 

Cache object: 8778936d2bb6b62b304f812af58a5feb


[ 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.