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/cam/scsi/scsi_da.c

Version: -  FREEBSD  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Implementation of SCSI Direct Access Peripheral driver for CAM.
    3  *
    4  * Copyright (c) 1997 Justin T. Gibbs.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions, and the following disclaimer,
   12  *    without modification, immediately at the beginning of the file.
   13  * 2. The name of the author may not be used to endorse or promote products
   14  *    derived from this software without specific prior written permission.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.55 2004/08/21 06:40:07 sanpei Exp $
   29  */
   30 
   31 #ifdef _KERNEL
   32 #include "opt_hw_wdog.h"
   33 #endif /* _KERNEL */
   34 
   35 #include <sys/param.h>
   36 
   37 #ifdef _KERNEL
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/buf.h>
   41 #include <sys/sysctl.h>
   42 #include <sys/taskqueue.h>
   43 #endif /* _KERNEL */
   44 
   45 #include <sys/devicestat.h>
   46 #include <sys/conf.h>
   47 #include <sys/disk.h>
   48 #include <sys/eventhandler.h>
   49 #include <sys/malloc.h>
   50 #include <sys/cons.h>
   51 
   52 #include <machine/md_var.h>
   53 
   54 #include <vm/vm.h>
   55 #include <vm/pmap.h>
   56 
   57 #ifndef _KERNEL
   58 #include <stdio.h>
   59 #include <string.h>
   60 #endif /* _KERNEL */
   61 
   62 #include <cam/cam.h>
   63 #include <cam/cam_ccb.h>
   64 #include <cam/cam_extend.h>
   65 #include <cam/cam_periph.h>
   66 #include <cam/cam_xpt_periph.h>
   67 
   68 #include <cam/scsi/scsi_message.h>
   69 
   70 #ifndef _KERNEL 
   71 #include <cam/scsi/scsi_da.h>
   72 #endif /* !_KERNEL */
   73 
   74 #ifdef _KERNEL
   75 typedef enum {
   76         DA_STATE_PROBE,
   77         DA_STATE_NORMAL
   78 } da_state;
   79 
   80 typedef enum {
   81         DA_FLAG_PACK_INVALID    = 0x001,
   82         DA_FLAG_NEW_PACK        = 0x002,
   83         DA_FLAG_PACK_LOCKED     = 0x004,
   84         DA_FLAG_PACK_REMOVABLE  = 0x008,
   85         DA_FLAG_TAGGED_QUEUING  = 0x010,
   86         DA_FLAG_NEED_OTAG       = 0x020,
   87         DA_FLAG_WENT_IDLE       = 0x040,
   88         DA_FLAG_RETRY_UA        = 0x080,
   89         DA_FLAG_OPEN            = 0x100,
   90         DA_FLAG_SCTX_INIT       = 0x200
   91 } da_flags;
   92 
   93 typedef enum {
   94         DA_Q_NONE               = 0x00,
   95         DA_Q_NO_SYNC_CACHE      = 0x01,
   96         DA_Q_NO_6_BYTE          = 0x02,
   97         DA_Q_NO_PREVENT         = 0x04
   98 } da_quirks;
   99 
  100 typedef enum {
  101         DA_CCB_PROBE            = 0x01,
  102         DA_CCB_BUFFER_IO        = 0x02,
  103         DA_CCB_WAITING          = 0x03,
  104         DA_CCB_DUMP             = 0x04,
  105         DA_CCB_TYPE_MASK        = 0x0F,
  106         DA_CCB_RETRY_UA         = 0x10
  107 } da_ccb_state;
  108 
  109 /* Offsets into our private area for storing information */
  110 #define ccb_state       ppriv_field0
  111 #define ccb_bp          ppriv_ptr1
  112 
  113 struct disk_params {
  114         u_int8_t  heads;
  115         u_int16_t cylinders;
  116         u_int8_t  secs_per_track;
  117         u_int32_t secsize;      /* Number of bytes/sector */
  118         u_int32_t sectors;      /* total number sectors */
  119 };
  120 
  121 struct da_softc {
  122         struct   buf_queue_head buf_queue;
  123         struct   devstat device_stats;
  124         SLIST_ENTRY(da_softc) links;
  125         LIST_HEAD(, ccb_hdr) pending_ccbs;
  126         da_state state;
  127         da_flags flags; 
  128         da_quirks quirks;
  129         int      minimum_cmd_size;
  130         int      ordered_tag_count;
  131         struct   disk_params params;
  132         struct   disk disk;
  133         union    ccb saved_ccb;
  134         struct task             sysctl_task;
  135         struct sysctl_ctx_list  sysctl_ctx;
  136         struct sysctl_oid       *sysctl_tree;
  137 };
  138 
  139 struct da_quirk_entry {
  140         struct scsi_inquiry_pattern inq_pat;
  141         da_quirks quirks;
  142 };
  143 
  144 static const char quantum[] = "QUANTUM";
  145 static const char microp[] = "MICROP";
  146 
  147 static struct da_quirk_entry da_quirk_table[] =
  148 {
  149         /* SPI, FC devices */
  150         {
  151                 /*
  152                  * Fujitsu M2513A MO drives.
  153                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
  154                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
  155                  * Reported by: W.Scholten <whs@xs4all.nl>
  156                  */
  157                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
  158                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  159         },
  160         {
  161                 /* See above. */
  162                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
  163                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  164         },
  165         {
  166                 /*
  167                  * This particular Fujitsu drive doesn't like the
  168                  * synchronize cache command.
  169                  * Reported by: Tom Jackson <toj@gorilla.net>
  170                  */
  171                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
  172                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  173         
  174         },
  175         {
  176                 /*
  177                  * This drive doesn't like the synchronize cache command
  178                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
  179                  * in NetBSD PR kern/6027, August 24, 1998.
  180                  */
  181                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
  182                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  183         },
  184         {
  185                 /*
  186                  * This drive doesn't like the synchronize cache command
  187                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
  188                  * (PR 8882).
  189                  */
  190                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
  191                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  192         },
  193         {
  194                 /*
  195                  * Doesn't like the synchronize cache command.
  196                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
  197                  */
  198                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
  199                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  200         },
  201         {
  202                 /*
  203                  * Doesn't like the synchronize cache command.
  204                  */
  205                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
  206                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  207         },
  208         {
  209                 /*
  210                  * Doesn't like the synchronize cache command.
  211                  */
  212                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
  213                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  214         },
  215         {
  216                 /*
  217                  * Doesn't work correctly with 6 byte reads/writes.
  218                  * Returns illegal request, and points to byte 9 of the
  219                  * 6-byte CDB.
  220                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
  221                  */
  222                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
  223                 /*quirks*/ DA_Q_NO_6_BYTE
  224         },
  225         {
  226                 /* See above. */
  227                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
  228                 /*quirks*/ DA_Q_NO_6_BYTE
  229         },
  230         {
  231                 /*
  232                  * The CISS RAID controllers do not support SYNC_CACHE
  233                  */
  234                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
  235                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  236         },
  237         /* XXX USB floppy quirks temporarily enabled for 4.9R */
  238         /* USB floppy devices supported by umass(4) */
  239         {
  240                 /*
  241                  * This USB floppy drive uses the UFI command set. This
  242                  * command set is a derivative of the ATAPI command set and
  243                  * does not support READ_6 commands only READ_10. It also does
  244                  * not support sync cache (0x35).
  245                  */
  246                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"},
  247                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  248         },
  249         {
  250                 /* Another USB floppy */
  251                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"},
  252                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  253         },
  254         {
  255                 /*
  256                  * The vendor, product and version strings coming from the
  257                  * controller are null terminated instead of being padded with
  258                  * spaces. The trailing wildcard character '*' is required.
  259                  */
  260                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"},
  261                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  262         },
  263         {
  264                 /*
  265                  * SmartDisk (Mitsumi) USB floppy drive
  266                  * PR: kern/50226
  267                  */
  268                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"},
  269                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  270         },
  271         /* USB mass storage devices supported by umass(4) */
  272         {
  273                 /*
  274                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
  275                  * PR: kern/51675
  276                  */
  277                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
  278                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  279         },
  280         {
  281                 /*
  282                  * Power Quotient Int. (PQI) USB flash key
  283                  * PR: kern/53067
  284                  */
  285                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
  286                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  287         }, 
  288         {
  289                 /*
  290                  * Creative Nomad MUVO mp3 player (USB)
  291                  * PR: kern/53094
  292                  */
  293                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
  294                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  295         },
  296         {
  297                 /*
  298                  * Jungsoft NEXDISK USB flash key
  299                  * PR: kern/54737
  300                  */
  301                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
  302                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  303         },
  304         {
  305                 /*
  306                  * FreeDik USB Mini Data Drive
  307                  * PR: kern/54786
  308                  */
  309                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
  310                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  311         },
  312         {
  313                 /*
  314                  * Sigmatel USB Flash MP3 Player
  315                  * PR: kern/57046
  316                  */
  317                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
  318                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  319         },
  320         {
  321                 /*
  322                  * Neuros USB Digital Audio Computer
  323                  * PR: kern/63645
  324                  */
  325                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
  326                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  327         },
  328         {
  329                 /*
  330                  * SEAGRAND NP-900 MP3 Player
  331                  * PR: kern/64563
  332                  */
  333                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
  334                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  335         },
  336         {
  337                 /*
  338                  * iRiver iFP MP3 player (with UMS Firmware)
  339                  * PR: kern/54881, i386/63941, kern/66124
  340                  */
  341                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
  342                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  343         },
  344         {
  345                 /*
  346                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
  347                  * PR: kern/70158
  348                  */
  349                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
  350                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  351         },
  352 };
  353 
  354 static  d_open_t        daopen;
  355 static  d_close_t       daclose;
  356 static  d_strategy_t    dastrategy;
  357 static  d_ioctl_t       daioctl;
  358 static  d_dump_t        dadump;
  359 static  periph_init_t   dainit;
  360 static  void            daasync(void *callback_arg, u_int32_t code,
  361                                 struct cam_path *path, void *arg);
  362 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
  363 static  periph_ctor_t   daregister;
  364 static  periph_dtor_t   dacleanup;
  365 static  periph_start_t  dastart;
  366 static  periph_oninv_t  daoninvalidate;
  367 static  void            dadone(struct cam_periph *periph,
  368                                union ccb *done_ccb);
  369 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
  370                                 u_int32_t sense_flags);
  371 static void             daprevent(struct cam_periph *periph, int action);
  372 static void             dasetgeom(struct cam_periph *periph,
  373                                   struct scsi_read_capacity_data * rdcap);
  374 static timeout_t        dasendorderedtag;
  375 static void             dashutdown(void *arg, int howto);
  376 
  377 #ifndef DA_DEFAULT_TIMEOUT
  378 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
  379 #endif
  380 
  381 #ifndef DA_DEFAULT_RETRY
  382 #define DA_DEFAULT_RETRY        4
  383 #endif
  384 
  385 static int da_retry_count = DA_DEFAULT_RETRY;
  386 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
  387 
  388 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
  389 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
  390             "CAM Direct Access Disk driver");
  391 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
  392            &da_retry_count, 0, "Normal I/O retry count");
  393 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
  394 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
  395            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
  396 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
  397 
  398 /*
  399  * DA_ORDEREDTAG_INTERVAL determines how often, relative
  400  * to the default timeout, we check to see whether an ordered
  401  * tagged transaction is appropriate to prevent simple tag
  402  * starvation.  Since we'd like to ensure that there is at least
  403  * 1/2 of the timeout length left for a starved transaction to
  404  * complete after we've sent an ordered tag, we must poll at least
  405  * four times in every timeout period.  This takes care of the worst
  406  * case where a starved transaction starts during an interval that
  407  * meets the requirement "don't send an ordered tag" test so it takes
  408  * us two intervals to determine that a tag must be sent.
  409  */
  410 #ifndef DA_ORDEREDTAG_INTERVAL
  411 #define DA_ORDEREDTAG_INTERVAL 4
  412 #endif
  413 
  414 static struct periph_driver dadriver =
  415 {
  416         dainit, "da",
  417         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
  418 };
  419 
  420 DATA_SET(periphdriver_set, dadriver);
  421 
  422 #define DA_CDEV_MAJOR 13
  423 #define DA_BDEV_MAJOR 4
  424 
  425 /* For 2.2-stable support */
  426 #ifndef D_DISK
  427 #define D_DISK 0
  428 #endif
  429 
  430 static struct cdevsw da_cdevsw = {
  431         /* open */      daopen,
  432         /* close */     daclose,
  433         /* read */      physread,
  434         /* write */     physwrite,
  435         /* ioctl */     daioctl,
  436         /* poll */      nopoll,
  437         /* mmap */      nommap,
  438         /* strategy */  dastrategy,
  439         /* name */      "da",
  440         /* maj */       DA_CDEV_MAJOR,
  441         /* dump */      dadump,
  442         /* psize */     nopsize,
  443         /* flags */     D_DISK,
  444         /* bmaj */      DA_BDEV_MAJOR
  445 };
  446 
  447 static struct cdevsw dadisk_cdevsw;
  448 
  449 static SLIST_HEAD(,da_softc) softc_list;
  450 static struct extend_array *daperiphs;
  451 
  452 static int
  453 daopen(dev_t dev, int flags, int fmt, struct proc *p)
  454 {
  455         struct cam_periph *periph;
  456         struct da_softc *softc;
  457         struct disklabel *label;        
  458         int unit;
  459         int part;
  460         int error;
  461         int s;
  462 
  463         unit = dkunit(dev);
  464         part = dkpart(dev);
  465         periph = cam_extend_get(daperiphs, unit);
  466         if (periph == NULL)
  467                 return (ENXIO); 
  468 
  469         softc = (struct da_softc *)periph->softc;
  470 
  471         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
  472             ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev),
  473              unit, part));
  474 
  475         if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
  476                 return (error); /* error code from tsleep */
  477         }
  478 
  479         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
  480                 return(ENXIO);
  481         softc->flags |= DA_FLAG_OPEN;
  482 
  483         s = splsoftcam();
  484         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
  485                 /* Invalidate our pack information. */
  486                 disk_invalidate(&softc->disk);
  487                 softc->flags &= ~DA_FLAG_PACK_INVALID;
  488         }
  489         splx(s);
  490 
  491         /* Do a read capacity */
  492         {
  493                 struct scsi_read_capacity_data *rcap;
  494                 union  ccb *ccb;
  495 
  496                 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
  497                                                                 M_TEMP,
  498                                                                 M_WAITOK);
  499                 
  500                 ccb = cam_periph_getccb(periph, /*priority*/1);
  501                 scsi_read_capacity(&ccb->csio,
  502                                    /*retries*/1,
  503                                    /*cbfncp*/dadone,
  504                                    MSG_SIMPLE_Q_TAG,
  505                                    rcap,
  506                                    SSD_FULL_SIZE,
  507                                    /*timeout*/60000);
  508                 ccb->ccb_h.ccb_bp = NULL;
  509 
  510                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
  511                                           /*sense_flags*/SF_RETRY_UA |
  512                                                          SF_RETRY_SELTO,
  513                                           &softc->device_stats);
  514 
  515                 xpt_release_ccb(ccb);
  516 
  517                 if (error == 0) {
  518                         dasetgeom(periph, rcap);
  519                 }
  520 
  521                 free(rcap, M_TEMP);
  522         }
  523 
  524         if (error == 0) {
  525                 struct ccb_getdev cgd;
  526 
  527                 /* Build label for whole disk. */
  528                 label = &softc->disk.d_label;
  529                 bzero(label, sizeof(*label));
  530                 label->d_type = DTYPE_SCSI;
  531 
  532                 /*
  533                  * Grab the inquiry data to get the vendor and product names.
  534                  * Put them in the typename and packname for the label.
  535                  */
  536                 xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1);
  537                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
  538                 xpt_action((union ccb *)&cgd);
  539 
  540                 strncpy(label->d_typename, cgd.inq_data.vendor,
  541                         min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
  542                 strncpy(label->d_packname, cgd.inq_data.product,
  543                         min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
  544                 
  545                 label->d_secsize = softc->params.secsize;
  546                 label->d_nsectors = softc->params.secs_per_track;
  547                 label->d_ntracks = softc->params.heads;
  548                 label->d_ncylinders = softc->params.cylinders;
  549                 label->d_secpercyl = softc->params.heads
  550                                   * softc->params.secs_per_track;
  551                 label->d_secperunit = softc->params.sectors;
  552 
  553                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
  554                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
  555                         daprevent(periph, PR_PREVENT);
  556         
  557                 /*
  558                  * Check to see whether or not the blocksize is set yet.
  559                  * If it isn't, set it and then clear the blocksize
  560                  * unavailable flag for the device statistics.
  561                  */
  562                 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
  563                         softc->device_stats.block_size = softc->params.secsize;
  564                         softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
  565                 }
  566         }
  567         
  568         if (error != 0) {
  569                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
  570                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
  571                         daprevent(periph, PR_ALLOW);
  572                 softc->flags &= ~DA_FLAG_OPEN;
  573                 cam_periph_release(periph);
  574         }
  575         cam_periph_unlock(periph);
  576         return (error);
  577 }
  578 
  579 static int
  580 daclose(dev_t dev, int flag, int fmt, struct proc *p)
  581 {
  582         struct  cam_periph *periph;
  583         struct  da_softc *softc;
  584         int     unit;
  585         int     error;
  586 
  587         unit = dkunit(dev);
  588         periph = cam_extend_get(daperiphs, unit);
  589         if (periph == NULL)
  590                 return (ENXIO); 
  591 
  592         softc = (struct da_softc *)periph->softc;
  593 
  594         if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
  595                 return (error); /* error code from tsleep */
  596         }
  597 
  598         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
  599                 union   ccb *ccb;
  600 
  601                 ccb = cam_periph_getccb(periph, /*priority*/1);
  602 
  603                 scsi_synchronize_cache(&ccb->csio,
  604                                        /*retries*/1,
  605                                        /*cbfcnp*/dadone,
  606                                        MSG_SIMPLE_Q_TAG,
  607                                        /*begin_lba*/0,/* Cover the whole disk */
  608                                        /*lb_count*/0,
  609                                        SSD_FULL_SIZE,
  610                                        5 * 60 * 1000);
  611 
  612                 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
  613                                   /*sense_flags*/SF_RETRY_UA,
  614                                   &softc->device_stats);
  615 
  616                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
  617                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
  618                              CAM_SCSI_STATUS_ERROR) {
  619                                 int asc, ascq;
  620                                 int sense_key, error_code;
  621 
  622                                 scsi_extract_sense(&ccb->csio.sense_data,
  623                                                    &error_code,
  624                                                    &sense_key, 
  625                                                    &asc, &ascq);
  626                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
  627                                         scsi_sense_print(&ccb->csio);
  628                         } else {
  629                                 xpt_print_path(periph->path);
  630                                 printf("Synchronize cache failed, status "
  631                                        "== 0x%x, scsi status == 0x%x\n",
  632                                        ccb->csio.ccb_h.status,
  633                                        ccb->csio.scsi_status);
  634                         }
  635                 }
  636 
  637                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
  638                         cam_release_devq(ccb->ccb_h.path,
  639                                          /*relsim_flags*/0,
  640                                          /*reduction*/0,
  641                                          /*timeout*/0,
  642                                          /*getcount_only*/0);
  643 
  644                 xpt_release_ccb(ccb);
  645 
  646         }
  647 
  648         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
  649                 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
  650                         daprevent(periph, PR_ALLOW);
  651                 /*
  652                  * If we've got removeable media, mark the blocksize as
  653                  * unavailable, since it could change when new media is
  654                  * inserted.
  655                  */
  656                 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
  657         }
  658 
  659         softc->flags &= ~DA_FLAG_OPEN;
  660         cam_periph_unlock(periph);
  661         cam_periph_release(periph);
  662         return (0);     
  663 }
  664 
  665 /*
  666  * Actually translate the requested transfer into one the physical driver
  667  * can understand.  The transfer is described by a buf and will include
  668  * only one physical transfer.
  669  */
  670 static void
  671 dastrategy(struct buf *bp)
  672 {
  673         struct cam_periph *periph;
  674         struct da_softc *softc;
  675         u_int  unit;
  676         u_int  part;
  677         int    s;
  678         
  679         unit = dkunit(bp->b_dev);
  680         part = dkpart(bp->b_dev);
  681         periph = cam_extend_get(daperiphs, unit);
  682         if (periph == NULL) {
  683                 bp->b_error = ENXIO;
  684                 goto bad;               
  685         }
  686         softc = (struct da_softc *)periph->softc;
  687 #if 0
  688         /*
  689          * check it's not too big a transfer for our adapter
  690          */
  691         scsi_minphys(bp,&sd_switch);
  692 #endif
  693 
  694         /*
  695          * Mask interrupts so that the pack cannot be invalidated until
  696          * after we are in the queue.  Otherwise, we might not properly
  697          * clean up one of the buffers.
  698          */
  699         s = splbio();
  700         
  701         /*
  702          * If the device has been made invalid, error out
  703          */
  704         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
  705                 splx(s);
  706                 bp->b_error = ENXIO;
  707                 goto bad;
  708         }
  709         
  710         /*
  711          * Place it in the queue of disk activities for this disk
  712          */
  713         bufqdisksort(&softc->buf_queue, bp);
  714 
  715         splx(s);
  716         
  717         /*
  718          * Schedule ourselves for performing the work.
  719          */
  720         xpt_schedule(periph, /* XXX priority */1);
  721 
  722         return;
  723 bad:
  724         bp->b_flags |= B_ERROR;
  725 
  726         /*
  727          * Correctly set the buf to indicate a completed xfer
  728          */
  729         bp->b_resid = bp->b_bcount;
  730         biodone(bp);
  731         return;
  732 }
  733 
  734 /* For 2.2-stable support */
  735 #ifndef ENOIOCTL
  736 #define ENOIOCTL -1
  737 #endif
  738 
  739 static int
  740 daioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  741 {
  742         struct cam_periph *periph;
  743         struct da_softc *softc;
  744         int unit;
  745         int error;
  746 
  747         unit = dkunit(dev);
  748         periph = cam_extend_get(daperiphs, unit);
  749         if (periph == NULL)
  750                 return (ENXIO); 
  751 
  752         softc = (struct da_softc *)periph->softc;
  753 
  754         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n"));
  755 
  756         if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
  757                 return (error); /* error code from tsleep */
  758         }       
  759 
  760         error = cam_periph_ioctl(periph, cmd, addr, daerror);
  761 
  762         cam_periph_unlock(periph);
  763         
  764         return (error);
  765 }
  766 
  767 static int
  768 dadump(dev_t dev)
  769 {
  770         struct      cam_periph *periph;
  771         struct      da_softc *softc;
  772         u_int       unit;
  773         u_int       part;
  774         u_int       secsize;
  775         u_int       num;        /* number of sectors to write */
  776         u_int       blknum;
  777         long        blkcnt;
  778         vm_paddr_t  addr;       
  779         struct      ccb_scsiio csio;
  780         int         dumppages = MAXDUMPPGS;
  781         int         error;
  782         int         i;
  783 
  784         /* toss any characters present prior to dump */
  785         while (cncheckc() != -1)
  786                 ;
  787 
  788         unit = dkunit(dev);
  789         part = dkpart(dev);
  790         periph = cam_extend_get(daperiphs, unit);
  791         if (periph == NULL) {
  792                 return (ENXIO);
  793         }
  794         softc = (struct da_softc *)periph->softc;
  795         
  796         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
  797                 return (ENXIO);
  798 
  799         error = disk_dumpcheck(dev, &num, &blknum, &secsize);
  800         if (error)
  801                 return (error);
  802 
  803         addr = 0;       /* starting address */
  804         blkcnt = howmany(PAGE_SIZE, secsize);
  805 
  806         while (num > 0) {
  807                 caddr_t va = NULL;
  808 
  809                 if ((num / blkcnt) < dumppages)
  810                         dumppages = num / blkcnt;
  811 
  812                 for (i = 0; i < dumppages; ++i) {
  813                         vm_paddr_t a = addr + (i * PAGE_SIZE);
  814                         if (is_physical_memory(a))
  815                                 va = pmap_kenter_temporary(trunc_page(a), i);
  816                         else
  817                                 va = pmap_kenter_temporary(trunc_page(0), i);
  818                 }
  819 
  820                 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
  821                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
  822                 scsi_read_write(&csio,
  823                                 /*retries*/1,
  824                                 dadone,
  825                                 MSG_ORDERED_Q_TAG,
  826                                 /*read*/FALSE,
  827                                 /*byte2*/0,
  828                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
  829                                 blknum,
  830                                 blkcnt * dumppages,
  831                                 /*data_ptr*/(u_int8_t *) va,
  832                                 /*dxfer_len*/blkcnt * secsize * dumppages,
  833                                 /*sense_len*/SSD_FULL_SIZE,
  834                                 DA_DEFAULT_TIMEOUT * 1000);             
  835                 xpt_polled_action((union ccb *)&csio);
  836 
  837                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
  838                         printf("Aborting dump due to I/O error.\n");
  839                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
  840                              CAM_SCSI_STATUS_ERROR)
  841                                 scsi_sense_print(&csio);
  842                         else
  843                                 printf("status == 0x%x, scsi status == 0x%x\n",
  844                                        csio.ccb_h.status, csio.scsi_status);
  845                         return(EIO);
  846                 }
  847                 
  848                 if (dumpstatus(addr, (off_t)num * softc->params.secsize) < 0)
  849                         return (EINTR);
  850 
  851                 /* update block count */
  852                 num -= blkcnt * dumppages;
  853                 blknum += blkcnt * dumppages;
  854                 addr += PAGE_SIZE * dumppages;
  855         }
  856 
  857         /*
  858          * Sync the disk cache contents to the physical media.
  859          */
  860         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
  861 
  862                 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
  863                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
  864                 scsi_synchronize_cache(&csio,
  865                                        /*retries*/1,
  866                                        /*cbfcnp*/dadone,
  867                                        MSG_SIMPLE_Q_TAG,
  868                                        /*begin_lba*/0,/* Cover the whole disk */
  869                                        /*lb_count*/0,
  870                                        SSD_FULL_SIZE,
  871                                        5 * 60 * 1000);
  872                 xpt_polled_action((union ccb *)&csio);
  873 
  874                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
  875                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
  876                              CAM_SCSI_STATUS_ERROR) {
  877                                 int asc, ascq;
  878                                 int sense_key, error_code;
  879 
  880                                 scsi_extract_sense(&csio.sense_data,
  881                                                    &error_code,
  882                                                    &sense_key, 
  883                                                    &asc, &ascq);
  884                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
  885                                         scsi_sense_print(&csio);
  886                         } else {
  887                                 xpt_print_path(periph->path);
  888                                 printf("Synchronize cache failed, status "
  889                                        "== 0x%x, scsi status == 0x%x\n",
  890                                        csio.ccb_h.status, csio.scsi_status);
  891                         }
  892                 }
  893         }
  894         return (0);
  895 }
  896 
  897 static void
  898 dainit(void)
  899 {
  900         cam_status status;
  901         struct cam_path *path;
  902 
  903         /*
  904          * Create our extend array for storing the devices we attach to.
  905          */
  906         daperiphs = cam_extend_new();
  907         SLIST_INIT(&softc_list);
  908         if (daperiphs == NULL) {
  909                 printf("da: Failed to alloc extend array!\n");
  910                 return;
  911         }
  912         
  913         /*
  914          * Install a global async callback.  This callback will
  915          * receive async callbacks like "new device found".
  916          */
  917         status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
  918                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
  919 
  920         if (status == CAM_REQ_CMP) {
  921                 struct ccb_setasync csa;
  922 
  923                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
  924                 csa.ccb_h.func_code = XPT_SASYNC_CB;
  925                 csa.event_enable = AC_FOUND_DEVICE;
  926                 csa.callback = daasync;
  927                 csa.callback_arg = NULL;
  928                 xpt_action((union ccb *)&csa);
  929                 status = csa.ccb_h.status;
  930                 xpt_free_path(path);
  931         }
  932 
  933         if (status != CAM_REQ_CMP) {
  934                 printf("da: Failed to attach master async callback "
  935                        "due to status 0x%x!\n", status);
  936         } else {
  937 
  938                 /*
  939                  * Schedule a periodic event to occasionally send an
  940                  * ordered tag to a device.
  941                  */
  942                 timeout(dasendorderedtag, NULL,
  943                         (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
  944 
  945                 /* Register our shutdown event handler */
  946                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
  947                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
  948                     printf("dainit: shutdown event registration failed!\n");
  949         }
  950 }
  951 
  952 static void
  953 daoninvalidate(struct cam_periph *periph)
  954 {
  955         int s;
  956         struct da_softc *softc;
  957         struct buf *q_bp;
  958         struct ccb_setasync csa;
  959 
  960         softc = (struct da_softc *)periph->softc;
  961 
  962         /*
  963          * De-register any async callbacks.
  964          */
  965         xpt_setup_ccb(&csa.ccb_h, periph->path,
  966                       /* priority */ 5);
  967         csa.ccb_h.func_code = XPT_SASYNC_CB;
  968         csa.event_enable = 0;
  969         csa.callback = daasync;
  970         csa.callback_arg = periph;
  971         xpt_action((union ccb *)&csa);
  972 
  973         softc->flags |= DA_FLAG_PACK_INVALID;
  974 
  975         /*
  976          * Although the oninvalidate() routines are always called at
  977          * splsoftcam, we need to be at splbio() here to keep the buffer
  978          * queue from being modified while we traverse it.
  979          */
  980         s = splbio();
  981 
  982         /*
  983          * Return all queued I/O with ENXIO.
  984          * XXX Handle any transactions queued to the card
  985          *     with XPT_ABORT_CCB.
  986          */
  987         while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
  988                 bufq_remove(&softc->buf_queue, q_bp);
  989                 q_bp->b_resid = q_bp->b_bcount;
  990                 q_bp->b_error = ENXIO;
  991                 q_bp->b_flags |= B_ERROR;
  992                 biodone(q_bp);
  993         }
  994         splx(s);
  995 
  996         SLIST_REMOVE(&softc_list, softc, da_softc, links);
  997 
  998         xpt_print_path(periph->path);
  999         printf("lost device\n");
 1000 }
 1001 
 1002 static void
 1003 dacleanup(struct cam_periph *periph)
 1004 {
 1005         struct da_softc *softc;
 1006 
 1007         softc = (struct da_softc *)periph->softc;
 1008 
 1009         devstat_remove_entry(&softc->device_stats);
 1010         cam_extend_release(daperiphs, periph->unit_number);
 1011         xpt_print_path(periph->path);
 1012         printf("removing device entry\n");
 1013         /*
 1014          * If we can't free the sysctl tree, oh well...
 1015          */
 1016         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
 1017             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
 1018                 xpt_print_path(periph->path);
 1019                 printf("can't remove sysctl context\n");
 1020         }
 1021         if (softc->disk.d_dev) {
 1022                 disk_destroy(softc->disk.d_dev);
 1023         }
 1024         free(softc, M_DEVBUF);
 1025 }
 1026 
 1027 static void
 1028 daasync(void *callback_arg, u_int32_t code,
 1029         struct cam_path *path, void *arg)
 1030 {
 1031         struct cam_periph *periph;
 1032 
 1033         periph = (struct cam_periph *)callback_arg;
 1034         switch (code) {
 1035         case AC_FOUND_DEVICE:
 1036         {
 1037                 struct ccb_getdev *cgd;
 1038                 cam_status status;
 1039  
 1040                 cgd = (struct ccb_getdev *)arg;
 1041 
 1042                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
 1043                     && SID_TYPE(&cgd->inq_data) != T_RBC
 1044                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
 1045                         break;
 1046 
 1047                 /*
 1048                  * Allocate a peripheral instance for
 1049                  * this device and start the probe
 1050                  * process.
 1051                  */
 1052                 status = cam_periph_alloc(daregister, daoninvalidate,
 1053                                           dacleanup, dastart,
 1054                                           "da", CAM_PERIPH_BIO,
 1055                                           cgd->ccb_h.path, daasync,
 1056                                           AC_FOUND_DEVICE, cgd);
 1057 
 1058                 if (status != CAM_REQ_CMP
 1059                  && status != CAM_REQ_INPROG)
 1060                         printf("daasync: Unable to attach to new device "
 1061                                 "due to status 0x%x\n", status);
 1062                 break;
 1063         }
 1064         case AC_SENT_BDR:
 1065         case AC_BUS_RESET:
 1066         {
 1067                 struct da_softc *softc;
 1068                 struct ccb_hdr *ccbh;
 1069                 int s;
 1070 
 1071                 softc = (struct da_softc *)periph->softc;
 1072                 s = splsoftcam();
 1073                 /*
 1074                  * Don't fail on the expected unit attention
 1075                  * that will occur.
 1076                  */
 1077                 softc->flags |= DA_FLAG_RETRY_UA;
 1078                 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
 1079                      ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
 1080                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
 1081                 splx(s);
 1082                 /* FALLTHROUGH*/
 1083         }
 1084         default:
 1085                 cam_periph_async(periph, code, path, arg);
 1086                 break;
 1087         }
 1088 }
 1089 
 1090 static void
 1091 dasysctlinit(void *context, int pending)
 1092 {
 1093         struct cam_periph *periph;
 1094         struct da_softc *softc;
 1095         char tmpstr[80], tmpstr2[80];
 1096 
 1097         periph = (struct cam_periph *)context;
 1098         softc = (struct da_softc *)periph->softc;
 1099 
 1100         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
 1101         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
 1102 
 1103         sysctl_ctx_init(&softc->sysctl_ctx);
 1104         softc->flags |= DA_FLAG_SCTX_INIT;
 1105         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
 1106                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
 1107                 CTLFLAG_RD, 0, tmpstr);
 1108         if (softc->sysctl_tree == NULL) {
 1109                 printf("dasysctlinit: unable to allocate sysctl tree\n");
 1110                 return;
 1111         }
 1112 
 1113         /*
 1114          * Now register the sysctl handler, so the user can the value on
 1115          * the fly.
 1116          */
 1117         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
 1118                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
 1119                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
 1120                 "Minimum CDB size");
 1121 }
 1122 
 1123 static int
 1124 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
 1125 {
 1126         int error, value;
 1127 
 1128         value = *(int *)arg1;
 1129 
 1130         error = sysctl_handle_int(oidp, &value, 0, req);
 1131 
 1132         if ((error != 0)
 1133          || (req->newptr == NULL))
 1134                 return (error);
 1135 
 1136         /*
 1137          * Acceptable values here are 6, 10 or 12.  It's possible we may
 1138          * support a 16 byte minimum command size in the future, since
 1139          * there are now READ(16) and WRITE(16) commands defined in the
 1140          * SBC-2 spec.
 1141          */
 1142         if (value < 6)
 1143                 value = 6;
 1144         else if ((value > 6)
 1145               && (value <= 10))
 1146                 value = 10;
 1147         else if (value > 10)
 1148                 value = 12;
 1149 
 1150         *(int *)arg1 = value;
 1151 
 1152         return (0);
 1153 }
 1154 
 1155 static cam_status
 1156 daregister(struct cam_periph *periph, void *arg)
 1157 {
 1158         int s;
 1159         struct da_softc *softc;
 1160         struct ccb_setasync csa;
 1161         struct ccb_pathinq cpi;
 1162         struct ccb_getdev *cgd;
 1163         char tmpstr[80];
 1164         caddr_t match;
 1165 
 1166         cgd = (struct ccb_getdev *)arg;
 1167         if (periph == NULL) {
 1168                 printf("daregister: periph was NULL!!\n");
 1169                 return(CAM_REQ_CMP_ERR);
 1170         }
 1171 
 1172         if (cgd == NULL) {
 1173                 printf("daregister: no getdev CCB, can't register device\n");
 1174                 return(CAM_REQ_CMP_ERR);
 1175         }
 1176 
 1177         softc = (struct da_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
 1178 
 1179         if (softc == NULL) {
 1180                 printf("daregister: Unable to probe new device. "
 1181                        "Unable to allocate softc\n");                           
 1182                 return(CAM_REQ_CMP_ERR);
 1183         }
 1184 
 1185         bzero(softc, sizeof(*softc));
 1186         LIST_INIT(&softc->pending_ccbs);
 1187         softc->state = DA_STATE_PROBE;
 1188         bufq_init(&softc->buf_queue);
 1189         if (SID_IS_REMOVABLE(&cgd->inq_data))
 1190                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
 1191         if ((cgd->inq_data.flags & SID_CmdQue) != 0)
 1192                 softc->flags |= DA_FLAG_TAGGED_QUEUING;
 1193 
 1194         periph->softc = softc;
 1195         
 1196         cam_extend_set(daperiphs, periph->unit_number, periph);
 1197 
 1198         /*
 1199          * See if this device has any quirks.
 1200          */
 1201         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
 1202                                (caddr_t)da_quirk_table,
 1203                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
 1204                                sizeof(*da_quirk_table), scsi_inquiry_match);
 1205 
 1206         if (match != NULL)
 1207                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
 1208         else
 1209                 softc->quirks = DA_Q_NONE;
 1210 
 1211         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
 1212 
 1213         /* Check if the SIM does not want 6 byte commands */
 1214         xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
 1215         cpi.ccb_h.func_code = XPT_PATH_INQ;
 1216         xpt_action((union ccb *)&cpi);
 1217         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
 1218                 softc->quirks |= DA_Q_NO_6_BYTE;
 1219 
 1220         /*
 1221          * RBC devices don't have to support READ(6), only READ(10).
 1222          */
 1223         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
 1224                 softc->minimum_cmd_size = 10;
 1225         else
 1226                 softc->minimum_cmd_size = 6;
 1227 
 1228         /*
 1229          * Load the user's default, if any.
 1230          */
 1231         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
 1232                  periph->unit_number);
 1233         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
 1234 
 1235         /*
 1236          * 6, 10 and 12 are the currently permissible values.
 1237          */
 1238         if (softc->minimum_cmd_size < 6)
 1239                 softc->minimum_cmd_size = 6;
 1240         else if ((softc->minimum_cmd_size > 6)
 1241               && (softc->minimum_cmd_size <= 10))
 1242                 softc->minimum_cmd_size = 10;
 1243         else if (softc->minimum_cmd_size > 12)
 1244                 softc->minimum_cmd_size = 12;
 1245 
 1246         /*
 1247          * Block our timeout handler while we
 1248          * add this softc to the dev list.
 1249          */
 1250         s = splsoftclock();
 1251         SLIST_INSERT_HEAD(&softc_list, softc, links);
 1252         splx(s);
 1253 
 1254         /*
 1255          * The DA driver supports a blocksize, but
 1256          * we don't know the blocksize until we do 
 1257          * a read capacity.  So, set a flag to
 1258          * indicate that the blocksize is 
 1259          * unavailable right now.  We'll clear the
 1260          * flag as soon as we've done a read capacity.
 1261          */
 1262         devstat_add_entry(&softc->device_stats, "da", 
 1263                           periph->unit_number, 0,
 1264                           DEVSTAT_BS_UNAVAILABLE,
 1265                           SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
 1266                           DEVSTAT_PRIORITY_DISK);
 1267 
 1268         /*
 1269          * Register this media as a disk
 1270          */
 1271         disk_create(periph->unit_number, &softc->disk, 0, 
 1272             &da_cdevsw, &dadisk_cdevsw);
 1273 
 1274         /*
 1275          * Add async callbacks for bus reset and
 1276          * bus device reset calls.  I don't bother
 1277          * checking if this fails as, in most cases,
 1278          * the system will function just fine without
 1279          * them and the only alternative would be to
 1280          * not attach the device on failure.
 1281          */
 1282         xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
 1283         csa.ccb_h.func_code = XPT_SASYNC_CB;
 1284         csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
 1285         csa.callback = daasync;
 1286         csa.callback_arg = periph;
 1287         xpt_action((union ccb *)&csa);
 1288         /*
 1289          * Lock this peripheral until we are setup.
 1290          * This first call can't block
 1291          */
 1292         (void)cam_periph_lock(periph, PRIBIO);
 1293         xpt_schedule(periph, /*priority*/5);
 1294 
 1295         return(CAM_REQ_CMP);
 1296 }
 1297 
 1298 static void
 1299 dastart(struct cam_periph *periph, union ccb *start_ccb)
 1300 {
 1301         struct da_softc *softc;
 1302 
 1303         softc = (struct da_softc *)periph->softc;
 1304 
 1305         
 1306         switch (softc->state) {
 1307         case DA_STATE_NORMAL:
 1308         {
 1309                 /* Pull a buffer from the queue and get going on it */          
 1310                 struct buf *bp;
 1311                 int s;
 1312 
 1313                 /*
 1314                  * See if there is a buf with work for us to do..
 1315                  */
 1316                 s = splbio();
 1317                 bp = bufq_first(&softc->buf_queue);
 1318                 if (periph->immediate_priority <= periph->pinfo.priority) {
 1319                         CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
 1320                                         ("queuing for immediate ccb\n"));
 1321                         start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
 1322                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
 1323                                           periph_links.sle);
 1324                         periph->immediate_priority = CAM_PRIORITY_NONE;
 1325                         splx(s);
 1326                         wakeup(&periph->ccb_list);
 1327                 } else if (bp == NULL) {
 1328                         splx(s);
 1329                         xpt_release_ccb(start_ccb);
 1330                 } else {
 1331                         int oldspl;
 1332                         u_int8_t tag_code;
 1333 
 1334                         bufq_remove(&softc->buf_queue, bp);
 1335 
 1336                         devstat_start_transaction(&softc->device_stats);
 1337 
 1338                         if ((bp->b_flags & B_ORDERED) != 0
 1339                          || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
 1340                                 softc->flags &= ~DA_FLAG_NEED_OTAG;
 1341                                 softc->ordered_tag_count++;
 1342                                 tag_code = MSG_ORDERED_Q_TAG;
 1343                         } else {
 1344                                 tag_code = MSG_SIMPLE_Q_TAG;
 1345                         }
 1346                         scsi_read_write(&start_ccb->csio,
 1347                                         /*retries*/da_retry_count,
 1348                                         dadone,
 1349                                         tag_code,
 1350                                         bp->b_flags & B_READ,
 1351                                         /*byte2*/0,
 1352                                         softc->minimum_cmd_size,
 1353                                         bp->b_pblkno,
 1354                                         bp->b_bcount / softc->params.secsize,
 1355                                         bp->b_data,
 1356                                         bp->b_bcount,
 1357                                         /*sense_len*/SSD_FULL_SIZE,
 1358                                         da_default_timeout * 1000);
 1359                         start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
 1360 
 1361                         /*
 1362                          * Block out any asyncronous callbacks
 1363                          * while we touch the pending ccb list.
 1364                          */
 1365                         oldspl = splcam();
 1366                         LIST_INSERT_HEAD(&softc->pending_ccbs,
 1367                                          &start_ccb->ccb_h, periph_links.le);
 1368                         splx(oldspl);
 1369 
 1370                         /* We expect a unit attention from this device */
 1371                         if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
 1372                                 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
 1373                                 softc->flags &= ~DA_FLAG_RETRY_UA;
 1374                         }
 1375 
 1376                         start_ccb->ccb_h.ccb_bp = bp;
 1377                         bp = bufq_first(&softc->buf_queue);
 1378                         splx(s);
 1379 
 1380                         xpt_action(start_ccb);
 1381                 }
 1382                 
 1383                 if (bp != NULL) {
 1384                         /* Have more work to do, so ensure we stay scheduled */
 1385                         xpt_schedule(periph, /* XXX priority */1);
 1386                 }
 1387                 break;
 1388         }
 1389         case DA_STATE_PROBE:
 1390         {
 1391                 struct ccb_scsiio *csio;
 1392                 struct scsi_read_capacity_data *rcap;
 1393 
 1394                 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
 1395                                                                 M_TEMP,
 1396                                                                 M_NOWAIT);
 1397                 if (rcap == NULL) {
 1398                         printf("dastart: Couldn't malloc read_capacity data\n");
 1399                         /* da_free_periph??? */
 1400                         break;
 1401                 }
 1402                 csio = &start_ccb->csio;
 1403                 scsi_read_capacity(csio,
 1404                                    /*retries*/4,
 1405                                    dadone,
 1406                                    MSG_SIMPLE_Q_TAG,
 1407                                    rcap,
 1408                                    SSD_FULL_SIZE,
 1409                                    /*timeout*/5000);
 1410                 start_ccb->ccb_h.ccb_bp = NULL;
 1411                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
 1412                 xpt_action(start_ccb);
 1413                 break;
 1414         }
 1415         }
 1416 }
 1417 
 1418 static int
 1419 cmd6workaround(union ccb *ccb)
 1420 {
 1421         struct scsi_rw_6 cmd6;
 1422         struct scsi_rw_10 *cmd10;
 1423         struct da_softc *softc;
 1424         u_int8_t *cdb;
 1425         int frozen;
 1426 
 1427         cdb = ccb->csio.cdb_io.cdb_bytes;
 1428 
 1429         /* Translation only possible if CDB is an array and cmd is R/W6 */
 1430         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
 1431             (*cdb != READ_6 && *cdb != WRITE_6))
 1432                 return 0;
 1433 
 1434         xpt_print_path(ccb->ccb_h.path);
 1435         printf("READ(6)/WRITE(6) not supported, "
 1436                "increasing minimum_cmd_size to 10.\n");
 1437         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
 1438         softc->minimum_cmd_size = 10;
 1439 
 1440         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
 1441         cmd10 = (struct scsi_rw_10 *)cdb;
 1442         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
 1443         cmd10->byte2 = 0;
 1444         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
 1445         cmd10->reserved = 0;
 1446         scsi_ulto2b(cmd6.length, cmd10->length);
 1447         cmd10->control = cmd6.control;
 1448         ccb->csio.cdb_len = sizeof(*cmd10);
 1449 
 1450         /* Requeue request, unfreezing queue if necessary */
 1451         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
 1452         ccb->ccb_h.status = CAM_REQUEUE_REQ;
 1453         xpt_action(ccb);
 1454         if (frozen) {
 1455                 cam_release_devq(ccb->ccb_h.path,
 1456                                  /*relsim_flags*/0,
 1457                                  /*reduction*/0,
 1458                                  /*timeout*/0,
 1459                                  /*getcount_only*/0);
 1460         }
 1461         return (ERESTART);
 1462 }
 1463 
 1464 static void
 1465 dadone(struct cam_periph *periph, union ccb *done_ccb)
 1466 {
 1467         struct da_softc *softc;
 1468         struct ccb_scsiio *csio;
 1469 
 1470         softc = (struct da_softc *)periph->softc;
 1471         csio = &done_ccb->csio;
 1472         switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
 1473         case DA_CCB_BUFFER_IO:
 1474         {
 1475                 struct buf *bp;
 1476                 int    oldspl;
 1477 
 1478                 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
 1479                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 1480                         int error;
 1481                         int s;
 1482                         int sf;
 1483                         
 1484                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
 1485                                 sf = SF_RETRY_UA;
 1486                         else
 1487                                 sf = 0;
 1488 
 1489                         /* Retry selection timeouts */
 1490                         sf |= SF_RETRY_SELTO;
 1491 
 1492                         if ((error = daerror(done_ccb, 0, sf)) == ERESTART) {
 1493                                 /*
 1494                                  * A retry was scheuled, so
 1495                                  * just return.
 1496                                  */
 1497                                 return;
 1498                         }
 1499                         if (error != 0) {
 1500                                 struct buf *q_bp;
 1501 
 1502                                 s = splbio();
 1503 
 1504                                 if (error == ENXIO) {
 1505                                         /*
 1506                                          * Catastrophic error.  Mark our pack as
 1507                                          * invalid.
 1508                                          */
 1509                                         /* XXX See if this is really a media
 1510                                          *     change first.
 1511                                          */
 1512                                         xpt_print_path(periph->path);
 1513                                         printf("Invalidating pack\n");
 1514                                         softc->flags |= DA_FLAG_PACK_INVALID;
 1515                                 }
 1516 
 1517                                 /*
 1518                                  * return all queued I/O with EIO, so that
 1519                                  * the client can retry these I/Os in the
 1520                                  * proper order should it attempt to recover.
 1521                                  */
 1522                                 while ((q_bp = bufq_first(&softc->buf_queue))
 1523                                         != NULL) {
 1524                                         bufq_remove(&softc->buf_queue, q_bp);
 1525                                         q_bp->b_resid = q_bp->b_bcount;
 1526                                         q_bp->b_error = EIO;
 1527                                         q_bp->b_flags |= B_ERROR;
 1528                                         biodone(q_bp);
 1529                                 }
 1530                                 splx(s);
 1531                                 bp->b_error = error;
 1532                                 bp->b_resid = bp->b_bcount;
 1533                                 bp->b_flags |= B_ERROR;
 1534                         } else {
 1535                                 bp->b_resid = csio->resid;
 1536                                 bp->b_error = 0;
 1537                                 if (bp->b_resid != 0)
 1538                                         bp->b_flags |= B_ERROR;
 1539                         }
 1540                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1541                                 cam_release_devq(done_ccb->ccb_h.path,
 1542                                                  /*relsim_flags*/0,
 1543                                                  /*reduction*/0,
 1544                                                  /*timeout*/0,
 1545                                                  /*getcount_only*/0);
 1546                 } else {
 1547                         bp->b_resid = csio->resid;
 1548                         if (csio->resid > 0)
 1549                                 bp->b_flags |= B_ERROR;
 1550                 }
 1551 
 1552                 /*
 1553                  * Block out any asyncronous callbacks
 1554                  * while we touch the pending ccb list.
 1555                  */
 1556                 oldspl = splcam();
 1557                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
 1558                 splx(oldspl);
 1559 
 1560                 if (softc->device_stats.busy_count == 0)
 1561                         softc->flags |= DA_FLAG_WENT_IDLE;
 1562 
 1563                 devstat_end_transaction_buf(&softc->device_stats, bp);
 1564                 biodone(bp);
 1565                 break;
 1566         }
 1567         case DA_CCB_PROBE:
 1568         {
 1569                 struct     scsi_read_capacity_data *rdcap;
 1570                 char       announce_buf[80];
 1571 
 1572                 rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
 1573                 
 1574                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 1575                         struct disk_params *dp;
 1576 
 1577                         dasetgeom(periph, rdcap);
 1578                         dp = &softc->params;
 1579                         snprintf(announce_buf, sizeof(announce_buf),
 1580                                 "%luMB (%u %u byte sectors: %dH %dS/T %dC)",
 1581                                 (unsigned long) (((u_int64_t)dp->secsize *
 1582                                 dp->sectors) / (1024*1024)), dp->sectors,
 1583                                 dp->secsize, dp->heads, dp->secs_per_track,
 1584                                 dp->cylinders);
 1585                 } else {
 1586                         int     error;
 1587 
 1588                         announce_buf[0] = '\0';
 1589 
 1590                         /*
 1591                          * Retry any UNIT ATTENTION type errors.  They
 1592                          * are expected at boot.
 1593                          */
 1594                         error = daerror(done_ccb, 0, SF_RETRY_UA |
 1595                                         SF_RETRY_SELTO | SF_NO_PRINT);
 1596                         if (error == ERESTART) {
 1597                                 /*
 1598                                  * A retry was scheuled, so
 1599                                  * just return.
 1600                                  */
 1601                                 return;
 1602                         } else if (error != 0) {
 1603                                 struct scsi_sense_data *sense;
 1604                                 int asc, ascq;
 1605                                 int sense_key, error_code;
 1606                                 int have_sense;
 1607                                 cam_status status;
 1608                                 struct ccb_getdev cgd;
 1609 
 1610                                 /* Don't wedge this device's queue */
 1611                                 cam_release_devq(done_ccb->ccb_h.path,
 1612                                                  /*relsim_flags*/0,
 1613                                                  /*reduction*/0,
 1614                                                  /*timeout*/0,
 1615                                                  /*getcount_only*/0);
 1616 
 1617                                 status = done_ccb->ccb_h.status;
 1618 
 1619                                 xpt_setup_ccb(&cgd.ccb_h, 
 1620                                               done_ccb->ccb_h.path,
 1621                                               /* priority */ 1);
 1622                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
 1623                                 xpt_action((union ccb *)&cgd);
 1624 
 1625                                 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
 1626                                  || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
 1627                                  || ((status & CAM_AUTOSNS_VALID) == 0))
 1628                                         have_sense = FALSE;
 1629                                 else
 1630                                         have_sense = TRUE;
 1631 
 1632                                 if (have_sense) {
 1633                                         sense = &csio->sense_data;
 1634                                         scsi_extract_sense(sense, &error_code,
 1635                                                            &sense_key, 
 1636                                                            &asc, &ascq);
 1637                                 }
 1638                                 /*
 1639                                  * Attach to anything that claims to be a
 1640                                  * direct access or optical disk device,
 1641                                  * as long as it doesn't return a "Logical
 1642                                  * unit not supported" (0x25) error.
 1643                                  */
 1644                                 if ((have_sense) && (asc != 0x25)
 1645                                  && (error_code == SSD_CURRENT_ERROR))
 1646                                         snprintf(announce_buf,
 1647                                             sizeof(announce_buf),
 1648                                                 "Attempt to query device "
 1649                                                 "size failed: %s, %s",
 1650                                                 scsi_sense_key_text[sense_key],
 1651                                                 scsi_sense_desc(asc,ascq,
 1652                                                                 &cgd.inq_data));
 1653                                 else { 
 1654                                         if (have_sense)
 1655                                                 scsi_sense_print(
 1656                                                         &done_ccb->csio);
 1657                                         else {
 1658                                                 xpt_print_path(periph->path);
 1659                                                 printf("got CAM status %#x\n",
 1660                                                        done_ccb->ccb_h.status);
 1661                                         }
 1662 
 1663                                         xpt_print_path(periph->path);
 1664                                         printf("fatal error, failed" 
 1665                                                " to attach to device\n");
 1666 
 1667                                         /*
 1668                                          * Free up resources.
 1669                                          */
 1670                                         cam_periph_invalidate(periph);
 1671                                 } 
 1672                         }
 1673                 }
 1674                 free(rdcap, M_TEMP);
 1675                 if (announce_buf[0] != '\0') {
 1676                         xpt_announce_periph(periph, announce_buf);
 1677                         /*
 1678                          * Create our sysctl variables, now that we know
 1679                          * we have successfully attached.
 1680                          */
 1681                         taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
 1682                 }
 1683                 softc->state = DA_STATE_NORMAL;         
 1684                 /*
 1685                  * Since our peripheral may be invalidated by an error
 1686                  * above or an external event, we must release our CCB
 1687                  * before releasing the probe lock on the peripheral.
 1688                  * The peripheral will only go away once the last lock
 1689                  * is removed, and we need it around for the CCB release
 1690                  * operation.
 1691                  */
 1692                 xpt_release_ccb(done_ccb);
 1693                 cam_periph_unlock(periph);
 1694                 return;
 1695         }
 1696         case DA_CCB_WAITING:
 1697         {
 1698                 /* Caller will release the CCB */
 1699                 wakeup(&done_ccb->ccb_h.cbfcnp);
 1700                 return;
 1701         }
 1702         case DA_CCB_DUMP:
 1703                 /* No-op.  We're polling */
 1704                 return;
 1705         default:
 1706                 break;
 1707         }
 1708         xpt_release_ccb(done_ccb);
 1709 }
 1710 
 1711 static int
 1712 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
 1713 {
 1714         struct da_softc   *softc;
 1715         struct cam_periph *periph;
 1716         int error;
 1717 
 1718         periph = xpt_path_periph(ccb->ccb_h.path);
 1719         softc = (struct da_softc *)periph->softc;
 1720 
 1721         /*
 1722          * Automatically detect devices that do not support
 1723          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
 1724          */
 1725         error = 0;
 1726         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
 1727                 error = cmd6workaround(ccb);
 1728         } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
 1729                    CAM_SCSI_STATUS_ERROR)
 1730          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
 1731          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
 1732          && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
 1733          && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
 1734                 int sense_key, error_code, asc, ascq;
 1735 
 1736                 scsi_extract_sense(&ccb->csio.sense_data,
 1737                                    &error_code, &sense_key, &asc, &ascq);
 1738                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
 1739                         error = cmd6workaround(ccb);
 1740         }
 1741         if (error == ERESTART)
 1742                 return (ERESTART);
 1743 
 1744         /*
 1745          * XXX
 1746          * Until we have a better way of doing pack validation,
 1747          * don't treat UAs as errors.
 1748          */
 1749         sense_flags |= SF_RETRY_UA;
 1750         return(cam_periph_error(ccb, cam_flags, sense_flags,
 1751                                 &softc->saved_ccb));
 1752 }
 1753 
 1754 static void
 1755 daprevent(struct cam_periph *periph, int action)
 1756 {
 1757         struct  da_softc *softc;
 1758         union   ccb *ccb;               
 1759         int     error;
 1760                 
 1761         softc = (struct da_softc *)periph->softc;
 1762 
 1763         if (((action == PR_ALLOW)
 1764           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
 1765          || ((action == PR_PREVENT)
 1766           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
 1767                 return;
 1768         }
 1769 
 1770         ccb = cam_periph_getccb(periph, /*priority*/1);
 1771 
 1772         scsi_prevent(&ccb->csio,
 1773                      /*retries*/1,
 1774                      /*cbcfp*/dadone,
 1775                      MSG_SIMPLE_Q_TAG,
 1776                      action,
 1777                      SSD_FULL_SIZE,
 1778                      5000);
 1779 
 1780         error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
 1781                                   /*sense_flags*/0, &softc->device_stats);
 1782 
 1783         if (error == 0) {
 1784                 if (action == PR_ALLOW)
 1785                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
 1786                 else
 1787                         softc->flags |= DA_FLAG_PACK_LOCKED;
 1788         }
 1789 
 1790         xpt_release_ccb(ccb);
 1791 }
 1792 
 1793 static void
 1794 dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap)
 1795 {
 1796         struct ccb_calc_geometry ccg;
 1797         struct da_softc *softc;
 1798         struct disk_params *dp;
 1799 
 1800         softc = (struct da_softc *)periph->softc;
 1801 
 1802         dp = &softc->params;
 1803         dp->secsize = scsi_4btoul(rdcap->length);
 1804         dp->sectors = scsi_4btoul(rdcap->addr) + 1;
 1805         /*
 1806          * Have the controller provide us with a geometry
 1807          * for this disk.  The only time the geometry
 1808          * matters is when we boot and the controller
 1809          * is the only one knowledgeable enough to come
 1810          * up with something that will make this a bootable
 1811          * device.
 1812          */
 1813         xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
 1814         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
 1815         ccg.block_size = dp->secsize;
 1816         ccg.volume_size = dp->sectors;
 1817         ccg.heads = 0;
 1818         ccg.secs_per_track = 0;
 1819         ccg.cylinders = 0;
 1820         xpt_action((union ccb*)&ccg);
 1821         dp->heads = ccg.heads;
 1822         dp->secs_per_track = ccg.secs_per_track;
 1823         dp->cylinders = ccg.cylinders;
 1824 }
 1825 
 1826 static void
 1827 dasendorderedtag(void *arg)
 1828 {
 1829         struct da_softc *softc;
 1830         int s;
 1831 
 1832         for (softc = SLIST_FIRST(&softc_list);
 1833              softc != NULL;
 1834              softc = SLIST_NEXT(softc, links)) {
 1835                 s = splsoftcam();
 1836                 if ((softc->ordered_tag_count == 0) 
 1837                  && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
 1838                         softc->flags |= DA_FLAG_NEED_OTAG;
 1839                 }
 1840                 if (softc->device_stats.busy_count > 0)
 1841                         softc->flags &= ~DA_FLAG_WENT_IDLE;
 1842 
 1843                 softc->ordered_tag_count = 0;
 1844                 splx(s);
 1845         }
 1846         /* Queue us up again */
 1847         timeout(dasendorderedtag, NULL,
 1848                 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
 1849 }
 1850 
 1851 /*
 1852  * Step through all DA peripheral drivers, and if the device is still open,
 1853  * sync the disk cache to physical media.
 1854  */
 1855 static void
 1856 dashutdown(void * arg, int howto)
 1857 {
 1858         struct cam_periph *periph;
 1859         struct da_softc *softc;
 1860 
 1861         for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL;
 1862              periph = TAILQ_NEXT(periph, unit_links)) {
 1863                 union ccb ccb;
 1864                 softc = (struct da_softc *)periph->softc;
 1865 
 1866                 /*
 1867                  * We only sync the cache if the drive is still open, and
 1868                  * if the drive is capable of it..
 1869                  */
 1870                 if (((softc->flags & DA_FLAG_OPEN) == 0)
 1871                  || (softc->quirks & DA_Q_NO_SYNC_CACHE))
 1872                         continue;
 1873 
 1874                 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
 1875 
 1876                 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
 1877                 scsi_synchronize_cache(&ccb.csio,
 1878                                        /*retries*/1,
 1879                                        /*cbfcnp*/dadone,
 1880                                        MSG_SIMPLE_Q_TAG,
 1881                                        /*begin_lba*/0, /* whole disk */
 1882                                        /*lb_count*/0,
 1883                                        SSD_FULL_SIZE,
 1884                                        5 * 60 * 1000);
 1885 
 1886                 xpt_polled_action(&ccb);
 1887 
 1888                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 1889                         if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
 1890                              CAM_SCSI_STATUS_ERROR)
 1891                          && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
 1892                                 int error_code, sense_key, asc, ascq;
 1893 
 1894                                 scsi_extract_sense(&ccb.csio.sense_data,
 1895                                                    &error_code, &sense_key,
 1896                                                    &asc, &ascq);
 1897 
 1898                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
 1899                                         scsi_sense_print(&ccb.csio);
 1900                         } else {
 1901                                 xpt_print_path(periph->path);
 1902                                 printf("Synchronize cache failed, status "
 1903                                        "== 0x%x, scsi status == 0x%x\n",
 1904                                        ccb.ccb_h.status, ccb.csio.scsi_status);
 1905                         }
 1906                 }
 1907 
 1908                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
 1909                         cam_release_devq(ccb.ccb_h.path,
 1910                                          /*relsim_flags*/0,
 1911                                          /*reduction*/0,
 1912                                          /*timeout*/0,
 1913                                          /*getcount_only*/0);
 1914 
 1915         }
 1916 }
 1917 
 1918 #else /* !_KERNEL */
 1919 
 1920 /*
 1921  * XXX This is only left out of the kernel build to silence warnings.  If,
 1922  * for some reason this function is used in the kernel, the ifdefs should
 1923  * be moved so it is included both in the kernel and userland.
 1924  */
 1925 void
 1926 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
 1927                  void (*cbfcnp)(struct cam_periph *, union ccb *),
 1928                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
 1929                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
 1930                  u_int32_t timeout)
 1931 {
 1932         struct scsi_format_unit *scsi_cmd;
 1933 
 1934         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
 1935         scsi_cmd->opcode = FORMAT_UNIT;
 1936         scsi_cmd->byte2 = byte2;
 1937         scsi_ulto2b(ileave, scsi_cmd->interleave);
 1938 
 1939         cam_fill_csio(csio,
 1940                       retries,
 1941                       cbfcnp,
 1942                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
 1943                       tag_action,
 1944                       data_ptr,
 1945                       dxfer_len,
 1946                       sense_len,
 1947                       sizeof(*scsi_cmd),
 1948                       timeout);
 1949 }
 1950 
 1951 #endif /* _KERNEL */

Cache object: 5f3a7971a11bbf0de3609452ffb669f0


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