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/cam/scsi/scsi_da.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  * 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 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <sys/param.h>
   33 
   34 #ifdef _KERNEL
   35 #include <sys/systm.h>
   36 #include <sys/kernel.h>
   37 #include <sys/bio.h>
   38 #include <sys/sysctl.h>
   39 #include <sys/taskqueue.h>
   40 #include <sys/lock.h>
   41 #include <sys/mutex.h>
   42 #include <sys/conf.h>
   43 #include <sys/devicestat.h>
   44 #include <sys/eventhandler.h>
   45 #include <sys/malloc.h>
   46 #include <sys/cons.h>
   47 #include <sys/endian.h>
   48 #include <sys/proc.h>
   49 #include <geom/geom.h>
   50 #include <geom/geom_disk.h>
   51 #endif /* _KERNEL */
   52 
   53 #ifndef _KERNEL
   54 #include <stdio.h>
   55 #include <string.h>
   56 #endif /* _KERNEL */
   57 
   58 #include <cam/cam.h>
   59 #include <cam/cam_ccb.h>
   60 #include <cam/cam_periph.h>
   61 #include <cam/cam_xpt_periph.h>
   62 #include <cam/cam_sim.h>
   63 
   64 #include <cam/scsi/scsi_message.h>
   65 
   66 #ifndef _KERNEL 
   67 #include <cam/scsi/scsi_da.h>
   68 #endif /* !_KERNEL */
   69 
   70 #ifdef _KERNEL
   71 typedef enum {
   72         DA_STATE_PROBE_WP,
   73         DA_STATE_PROBE_RC,
   74         DA_STATE_PROBE_RC16,
   75         DA_STATE_PROBE_LBP,
   76         DA_STATE_PROBE_BLK_LIMITS,
   77         DA_STATE_PROBE_BDC,
   78         DA_STATE_PROBE_ATA,
   79         DA_STATE_NORMAL
   80 } da_state;
   81 
   82 typedef enum {
   83         DA_FLAG_PACK_INVALID    = 0x001,
   84         DA_FLAG_NEW_PACK        = 0x002,
   85         DA_FLAG_PACK_LOCKED     = 0x004,
   86         DA_FLAG_PACK_REMOVABLE  = 0x008,
   87         DA_FLAG_NEED_OTAG       = 0x020,
   88         DA_FLAG_WAS_OTAG        = 0x040,
   89         DA_FLAG_RETRY_UA        = 0x080,
   90         DA_FLAG_OPEN            = 0x100,
   91         DA_FLAG_SCTX_INIT       = 0x200,
   92         DA_FLAG_CAN_RC16        = 0x400,
   93         DA_FLAG_PROBED          = 0x800,
   94         DA_FLAG_DIRTY           = 0x1000,
   95         DA_FLAG_ANNOUNCED       = 0x2000
   96 } da_flags;
   97 
   98 typedef enum {
   99         DA_Q_NONE               = 0x00,
  100         DA_Q_NO_SYNC_CACHE      = 0x01,
  101         DA_Q_NO_6_BYTE          = 0x02,
  102         DA_Q_NO_PREVENT         = 0x04,
  103         DA_Q_4K                 = 0x08,
  104         DA_Q_NO_RC16            = 0x10,
  105         DA_Q_NO_UNMAP           = 0x20,
  106         DA_Q_RETRY_BUSY         = 0x40
  107 } da_quirks;
  108 
  109 #define DA_Q_BIT_STRING         \
  110         "\020"                  \
  111         "\001NO_SYNC_CACHE"     \
  112         "\002NO_6_BYTE"         \
  113         "\003NO_PREVENT"        \
  114         "\0044K"                \
  115         "\005NO_RC16"           \
  116         "\006NO_UNMAP"          \
  117         "\007RETRY_BUSY"
  118 
  119 typedef enum {
  120         DA_CCB_PROBE_RC         = 0x01,
  121         DA_CCB_PROBE_RC16       = 0x02,
  122         DA_CCB_PROBE_LBP        = 0x03,
  123         DA_CCB_PROBE_BLK_LIMITS = 0x04,
  124         DA_CCB_PROBE_BDC        = 0x05,
  125         DA_CCB_PROBE_ATA        = 0x06,
  126         DA_CCB_BUFFER_IO        = 0x07,
  127         DA_CCB_DUMP             = 0x0A,
  128         DA_CCB_DELETE           = 0x0B,
  129         DA_CCB_TUR              = 0x0C,
  130         DA_CCB_PROBE_WP         = 0x12,
  131         DA_CCB_TYPE_MASK        = 0x1F,
  132         DA_CCB_RETRY_UA         = 0x20
  133 } da_ccb_state;
  134 
  135 /*
  136  * Order here is important for method choice
  137  *
  138  * We prefer ATA_TRIM as tests run against a Sandforce 2281 SSD attached to
  139  * LSI 2008 (mps) controller (FW: v12, Drv: v14) resulted 20% quicker deletes
  140  * using ATA_TRIM than the corresponding UNMAP results for a real world mysql
  141  * import taking 5mins.
  142  *
  143  */
  144 typedef enum {
  145         DA_DELETE_NONE,
  146         DA_DELETE_DISABLE,
  147         DA_DELETE_ATA_TRIM,
  148         DA_DELETE_UNMAP,
  149         DA_DELETE_WS16,
  150         DA_DELETE_WS10,
  151         DA_DELETE_ZERO,
  152         DA_DELETE_MIN = DA_DELETE_ATA_TRIM,
  153         DA_DELETE_MAX = DA_DELETE_ZERO
  154 } da_delete_methods;
  155 
  156 typedef void da_delete_func_t (struct cam_periph *periph, union ccb *ccb,
  157                               struct bio *bp);
  158 static da_delete_func_t da_delete_trim;
  159 static da_delete_func_t da_delete_unmap;
  160 static da_delete_func_t da_delete_ws;
  161 
  162 static const void * da_delete_functions[] = {
  163         NULL,
  164         NULL,
  165         da_delete_trim,
  166         da_delete_unmap,
  167         da_delete_ws,
  168         da_delete_ws,
  169         da_delete_ws
  170 };
  171 
  172 static const char *da_delete_method_names[] =
  173     { "NONE", "DISABLE", "ATA_TRIM", "UNMAP", "WS16", "WS10", "ZERO" };
  174 static const char *da_delete_method_desc[] =
  175     { "NONE", "DISABLED", "ATA TRIM", "UNMAP", "WRITE SAME(16) with UNMAP",
  176       "WRITE SAME(10) with UNMAP", "ZERO" };
  177 
  178 /* Offsets into our private area for storing information */
  179 #define ccb_state       ppriv_field0
  180 #define ccb_bp          ppriv_ptr1
  181 
  182 struct disk_params {
  183         u_int8_t  heads;
  184         u_int32_t cylinders;
  185         u_int8_t  secs_per_track;
  186         u_int32_t secsize;      /* Number of bytes/sector */
  187         u_int64_t sectors;      /* total number sectors */
  188         u_int     stripesize;
  189         u_int     stripeoffset;
  190 };
  191 
  192 #define UNMAP_RANGE_MAX         0xffffffff
  193 #define UNMAP_HEAD_SIZE         8
  194 #define UNMAP_RANGE_SIZE        16
  195 #define UNMAP_MAX_RANGES        2048 /* Protocol Max is 4095 */
  196 #define UNMAP_BUF_SIZE          ((UNMAP_MAX_RANGES * UNMAP_RANGE_SIZE) + \
  197                                 UNMAP_HEAD_SIZE)
  198 
  199 #define WS10_MAX_BLKS           0xffff
  200 #define WS16_MAX_BLKS           0xffffffff
  201 #define ATA_TRIM_MAX_RANGES     ((UNMAP_BUF_SIZE / \
  202         (ATA_DSM_RANGE_SIZE * ATA_DSM_BLK_SIZE)) * ATA_DSM_BLK_SIZE)
  203 
  204 struct da_softc {
  205         struct   bio_queue_head bio_queue;
  206         struct   bio_queue_head delete_queue;
  207         struct   bio_queue_head delete_run_queue;
  208         LIST_HEAD(, ccb_hdr) pending_ccbs;
  209         int      tur;                   /* TEST UNIT READY should be sent */
  210         int      refcount;              /* Active xpt_action() calls */
  211         da_state state;
  212         da_flags flags; 
  213         da_quirks quirks;
  214         int      sort_io_queue;
  215         int      minimum_cmd_size;
  216         int      error_inject;
  217         int      trim_max_ranges;
  218         int      delete_running;
  219         int      delete_available;      /* Delete methods possibly available */
  220         u_int    maxio;
  221         uint32_t                unmap_max_ranges;
  222         uint32_t                unmap_max_lba; /* Max LBAs in UNMAP req */
  223         uint64_t                ws_max_blks;
  224         da_delete_methods       delete_method_pref;
  225         da_delete_methods       delete_method;
  226         da_delete_func_t        *delete_func;
  227         struct   disk_params params;
  228         struct   disk *disk;
  229         union    ccb saved_ccb;
  230         struct task             sysctl_task;
  231         struct sysctl_ctx_list  sysctl_ctx;
  232         struct sysctl_oid       *sysctl_tree;
  233         struct callout          sendordered_c;
  234         uint64_t wwpn;
  235         uint8_t  unmap_buf[UNMAP_BUF_SIZE];
  236         struct scsi_read_capacity_data_long rcaplong;
  237         struct callout          mediapoll_c;
  238 };
  239 
  240 #define dadeleteflag(softc, delete_method, enable)                      \
  241         if (enable) {                                                   \
  242                 softc->delete_available |= (1 << delete_method);        \
  243         } else {                                                        \
  244                 softc->delete_available &= ~(1 << delete_method);       \
  245         }
  246 
  247 struct da_quirk_entry {
  248         struct scsi_inquiry_pattern inq_pat;
  249         da_quirks quirks;
  250 };
  251 
  252 static const char quantum[] = "QUANTUM";
  253 static const char microp[] = "MICROP";
  254 
  255 static struct da_quirk_entry da_quirk_table[] =
  256 {
  257         /* SPI, FC devices */
  258         {
  259                 /*
  260                  * Fujitsu M2513A MO drives.
  261                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
  262                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
  263                  * Reported by: W.Scholten <whs@xs4all.nl>
  264                  */
  265                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
  266                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  267         },
  268         {
  269                 /* See above. */
  270                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
  271                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  272         },
  273         {
  274                 /*
  275                  * This particular Fujitsu drive doesn't like the
  276                  * synchronize cache command.
  277                  * Reported by: Tom Jackson <toj@gorilla.net>
  278                  */
  279                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
  280                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  281         },
  282         {
  283                 /*
  284                  * This drive doesn't like the synchronize cache command
  285                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
  286                  * in NetBSD PR kern/6027, August 24, 1998.
  287                  */
  288                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
  289                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  290         },
  291         {
  292                 /*
  293                  * This drive doesn't like the synchronize cache command
  294                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
  295                  * (PR 8882).
  296                  */
  297                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
  298                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  299         },
  300         {
  301                 /*
  302                  * Doesn't like the synchronize cache command.
  303                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
  304                  */
  305                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
  306                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  307         },
  308         {
  309                 /*
  310                  * Doesn't like the synchronize cache command.
  311                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
  312                  */
  313                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
  314                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  315         },
  316         {
  317                 /*
  318                  * Doesn't like the synchronize cache command.
  319                  */
  320                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
  321                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  322         },
  323         {
  324                 /*
  325                  * Doesn't like the synchronize cache command.
  326                  * Reported by: walter@pelissero.de
  327                  */
  328                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
  329                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  330         },
  331         {
  332                 /*
  333                  * Doesn't work correctly with 6 byte reads/writes.
  334                  * Returns illegal request, and points to byte 9 of the
  335                  * 6-byte CDB.
  336                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
  337                  */
  338                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
  339                 /*quirks*/ DA_Q_NO_6_BYTE
  340         },
  341         {
  342                 /* See above. */
  343                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
  344                 /*quirks*/ DA_Q_NO_6_BYTE
  345         },
  346         {
  347                 /*
  348                  * Doesn't like the synchronize cache command.
  349                  * Reported by: walter@pelissero.de
  350                  */
  351                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
  352                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  353         },
  354         {
  355                 /*
  356                  * The CISS RAID controllers do not support SYNC_CACHE
  357                  */
  358                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
  359                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  360         },
  361         {
  362                 /*
  363                  * The STEC SSDs sometimes hang on UNMAP.
  364                  */
  365                 {T_DIRECT, SIP_MEDIA_FIXED, "STEC", "*", "*"},
  366                 /*quirks*/ DA_Q_NO_UNMAP
  367         },
  368         {
  369                 /*
  370                  * VMware returns BUSY status when storage has transient
  371                  * connectivity problems, so better wait.
  372                  */
  373                 {T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*"},
  374                 /*quirks*/ DA_Q_RETRY_BUSY
  375         },
  376         /* USB mass storage devices supported by umass(4) */
  377         {
  378                 /*
  379                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
  380                  * PR: kern/51675
  381                  */
  382                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
  383                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  384         },
  385         {
  386                 /*
  387                  * Power Quotient Int. (PQI) USB flash key
  388                  * PR: kern/53067
  389                  */
  390                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
  391                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  392         },
  393         {
  394                 /*
  395                  * Creative Nomad MUVO mp3 player (USB)
  396                  * PR: kern/53094
  397                  */
  398                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
  399                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  400         },
  401         {
  402                 /*
  403                  * Jungsoft NEXDISK USB flash key
  404                  * PR: kern/54737
  405                  */
  406                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
  407                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  408         },
  409         {
  410                 /*
  411                  * FreeDik USB Mini Data Drive
  412                  * PR: kern/54786
  413                  */
  414                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
  415                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  416         },
  417         {
  418                 /*
  419                  * Sigmatel USB Flash MP3 Player
  420                  * PR: kern/57046
  421                  */
  422                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
  423                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  424         },
  425         {
  426                 /*
  427                  * Neuros USB Digital Audio Computer
  428                  * PR: kern/63645
  429                  */
  430                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
  431                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  432         },
  433         {
  434                 /*
  435                  * SEAGRAND NP-900 MP3 Player
  436                  * PR: kern/64563
  437                  */
  438                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
  439                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  440         },
  441         {
  442                 /*
  443                  * iRiver iFP MP3 player (with UMS Firmware)
  444                  * PR: kern/54881, i386/63941, kern/66124
  445                  */
  446                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
  447                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  448         },
  449         {
  450                 /*
  451                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
  452                  * PR: kern/70158
  453                  */
  454                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
  455                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  456         },
  457         {
  458                 /*
  459                  * ZICPlay USB MP3 Player with FM
  460                  * PR: kern/75057
  461                  */
  462                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
  463                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  464         },
  465         {
  466                 /*
  467                  * TEAC USB floppy mechanisms
  468                  */
  469                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
  470                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  471         },
  472         {
  473                 /*
  474                  * Kingston DataTraveler II+ USB Pen-Drive.
  475                  * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
  476                  */
  477                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
  478                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  479         },
  480         {
  481                 /*
  482                  * USB DISK Pro PMAP
  483                  * Reported by: jhs
  484                  * PR: usb/96381
  485                  */
  486                 {T_DIRECT, SIP_MEDIA_REMOVABLE, " ", "USB DISK Pro", "PMAP"},
  487                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  488         },
  489         {
  490                 /*
  491                  * Motorola E398 Mobile Phone (TransFlash memory card).
  492                  * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
  493                  * PR: usb/89889
  494                  */
  495                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
  496                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  497         },
  498         {
  499                 /*
  500                  * Qware BeatZkey! Pro
  501                  * PR: usb/79164
  502                  */
  503                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
  504                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  505         },
  506         {
  507                 /*
  508                  * Time DPA20B 1GB MP3 Player
  509                  * PR: usb/81846
  510                  */
  511                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
  512                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  513         },
  514         {
  515                 /*
  516                  * Samsung USB key 128Mb
  517                  * PR: usb/90081
  518                  */
  519                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
  520                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  521         },
  522         {
  523                 /*
  524                  * Kingston DataTraveler 2.0 USB Flash memory.
  525                  * PR: usb/89196
  526                  */
  527                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
  528                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  529         },
  530         {
  531                 /*
  532                  * Creative MUVO Slim mp3 player (USB)
  533                  * PR: usb/86131
  534                  */
  535                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
  536                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  537                 },
  538         {
  539                 /*
  540                  * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
  541                  * PR: usb/80487
  542                  */
  543                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
  544                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  545         },
  546         {
  547                 /*
  548                  * SanDisk Micro Cruzer 128MB
  549                  * PR: usb/75970
  550                  */
  551                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
  552                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  553         },
  554         {
  555                 /*
  556                  * TOSHIBA TransMemory USB sticks
  557                  * PR: kern/94660
  558                  */
  559                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
  560                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  561         },
  562         {
  563                 /*
  564                  * PNY USB 3.0 Flash Drives
  565                 */
  566                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PNY", "USB 3.0 FD*",
  567                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_RC16
  568         },
  569         {
  570                 /*
  571                  * PNY USB Flash keys
  572                  * PR: usb/75578, usb/72344, usb/65436 
  573                  */
  574                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
  575                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  576         },
  577         {
  578                 /*
  579                  * Genesys 6-in-1 Card Reader
  580                  * PR: usb/94647
  581                  */
  582                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
  583                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  584         },
  585         {
  586                 /*
  587                  * Rekam Digital CAMERA
  588                  * PR: usb/98713
  589                  */
  590                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
  591                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  592         },
  593         {
  594                 /*
  595                  * iRiver H10 MP3 player
  596                  * PR: usb/102547
  597                  */
  598                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
  599                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  600         },
  601         {
  602                 /*
  603                  * iRiver U10 MP3 player
  604                  * PR: usb/92306
  605                  */
  606                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
  607                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  608         },
  609         {
  610                 /*
  611                  * X-Micro Flash Disk
  612                  * PR: usb/96901
  613                  */
  614                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
  615                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  616         },
  617         {
  618                 /*
  619                  * EasyMP3 EM732X USB 2.0 Flash MP3 Player
  620                  * PR: usb/96546
  621                  */
  622                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
  623                 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  624         },
  625         {
  626                 /*
  627                  * Denver MP3 player
  628                  * PR: usb/107101
  629                  */
  630                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
  631                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  632         },
  633         {
  634                 /*
  635                  * Philips USB Key Audio KEY013
  636                  * PR: usb/68412
  637                  */
  638                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
  639                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
  640         },
  641         {
  642                 /*
  643                  * JNC MP3 Player
  644                  * PR: usb/94439
  645                  */
  646                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
  647                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  648         },
  649         {
  650                 /*
  651                  * SAMSUNG MP0402H
  652                  * PR: usb/108427
  653                  */
  654                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
  655                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  656         },
  657         {
  658                 /*
  659                  * I/O Magic USB flash - Giga Bank
  660                  * PR: usb/108810
  661                  */
  662                 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
  663                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  664         },
  665         {
  666                 /*
  667                  * JoyFly 128mb USB Flash Drive
  668                  * PR: 96133
  669                  */
  670                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
  671                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  672         },
  673         {
  674                 /*
  675                  * ChipsBnk usb stick
  676                  * PR: 103702
  677                  */
  678                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
  679                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  680         },
  681         {
  682                 /*
  683                  * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
  684                  * PR: 129858
  685                  */
  686                 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
  687                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  688         },
  689         {
  690                 /*
  691                  * Samsung YP-U3 mp3-player
  692                  * PR: 125398
  693                  */
  694                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
  695                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  696         },
  697         {
  698                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
  699                  "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  700         },
  701         {
  702                 /*
  703                  * Sony Cyber-Shot DSC cameras
  704                  * PR: usb/137035
  705                  */
  706                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
  707                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
  708         },
  709         {
  710                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler G3",
  711                  "1.00"}, /*quirks*/ DA_Q_NO_PREVENT
  712         },
  713         {
  714                 /* At least several Transcent USB sticks lie on RC16. */
  715                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JetFlash", "Transcend*",
  716                  "*"}, /*quirks*/ DA_Q_NO_RC16
  717         },
  718         {
  719                 /*
  720                  * I-O Data USB Flash Disk
  721                  * PR: usb/211716
  722                  */
  723                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "I-O DATA", "USB Flash Disk*",
  724                  "*"}, /*quirks*/ DA_Q_NO_RC16
  725         },
  726         /* ATA/SATA devices over SAS/USB/... */
  727         {
  728                 /* Hitachi Advanced Format (4k) drives */
  729                 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
  730                 /*quirks*/DA_Q_4K
  731         },
  732         {
  733                 /* Samsung Advanced Format (4k) drives */
  734                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
  735                 /*quirks*/DA_Q_4K
  736         },
  737         {
  738                 /* Samsung Advanced Format (4k) drives */
  739                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
  740                 /*quirks*/DA_Q_4K
  741         },
  742         {
  743                 /* Samsung Advanced Format (4k) drives */
  744                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
  745                 /*quirks*/DA_Q_4K
  746         },
  747         {
  748                 /* Samsung Advanced Format (4k) drives */
  749                 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
  750                 /*quirks*/DA_Q_4K
  751         },
  752         {
  753                 /* Seagate Barracuda Green Advanced Format (4k) drives */
  754                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
  755                 /*quirks*/DA_Q_4K
  756         },
  757         {
  758                 /* Seagate Barracuda Green Advanced Format (4k) drives */
  759                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
  760                 /*quirks*/DA_Q_4K
  761         },
  762         {
  763                 /* Seagate Barracuda Green Advanced Format (4k) drives */
  764                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
  765                 /*quirks*/DA_Q_4K
  766         },
  767         {
  768                 /* Seagate Barracuda Green Advanced Format (4k) drives */
  769                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
  770                 /*quirks*/DA_Q_4K
  771         },
  772         {
  773                 /* Seagate Barracuda Green Advanced Format (4k) drives */
  774                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
  775                 /*quirks*/DA_Q_4K
  776         },
  777         {
  778                 /* Seagate Barracuda Green Advanced Format (4k) drives */
  779                 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
  780                 /*quirks*/DA_Q_4K
  781         },
  782         {
  783                 /* Seagate Momentus Advanced Format (4k) drives */
  784                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
  785                 /*quirks*/DA_Q_4K
  786         },
  787         {
  788                 /* Seagate Momentus Advanced Format (4k) drives */
  789                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
  790                 /*quirks*/DA_Q_4K
  791         },
  792         {
  793                 /* Seagate Momentus Advanced Format (4k) drives */
  794                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
  795                 /*quirks*/DA_Q_4K
  796         },
  797         {
  798                 /* Seagate Momentus Advanced Format (4k) drives */
  799                 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
  800                 /*quirks*/DA_Q_4K
  801         },
  802         {
  803                 /* Seagate Momentus Advanced Format (4k) drives */
  804                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
  805                 /*quirks*/DA_Q_4K
  806         },
  807         {
  808                 /* Seagate Momentus Advanced Format (4k) drives */
  809                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
  810                 /*quirks*/DA_Q_4K
  811         },
  812         {
  813                 /* Seagate Momentus Advanced Format (4k) drives */
  814                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
  815                 /*quirks*/DA_Q_4K
  816         },
  817         {
  818                 /* Seagate Momentus Advanced Format (4k) drives */
  819                 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
  820                 /*quirks*/DA_Q_4K
  821         },
  822         {
  823                 /* Seagate Momentus Advanced Format (4k) drives */
  824                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
  825                 /*quirks*/DA_Q_4K
  826         },
  827         {
  828                 /* Seagate Momentus Advanced Format (4k) drives */
  829                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
  830                 /*quirks*/DA_Q_4K
  831         },
  832         {
  833                 /* Seagate Momentus Advanced Format (4k) drives */
  834                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
  835                 /*quirks*/DA_Q_4K
  836         },
  837         {
  838                 /* Seagate Momentus Advanced Format (4k) drives */
  839                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
  840                 /*quirks*/DA_Q_4K
  841         },
  842         {
  843                 /* Seagate Momentus Advanced Format (4k) drives */
  844                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
  845                 /*quirks*/DA_Q_4K
  846         },
  847         {
  848                 /* Seagate Momentus Advanced Format (4k) drives */
  849                 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
  850                 /*quirks*/DA_Q_4K
  851         },
  852         {
  853                 /* Seagate Momentus Thin Advanced Format (4k) drives */
  854                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
  855                 /*quirks*/DA_Q_4K
  856         },
  857         {
  858                 /* Seagate Momentus Thin Advanced Format (4k) drives */
  859                 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
  860                 /*quirks*/DA_Q_4K
  861         },
  862         {
  863                 /* WDC Caviar Green Advanced Format (4k) drives */
  864                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
  865                 /*quirks*/DA_Q_4K
  866         },
  867         {
  868                 /* WDC Caviar Green Advanced Format (4k) drives */
  869                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
  870                 /*quirks*/DA_Q_4K
  871         },
  872         {
  873                 /* WDC Caviar Green Advanced Format (4k) drives */
  874                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
  875                 /*quirks*/DA_Q_4K
  876         },
  877         {
  878                 /* WDC Caviar Green Advanced Format (4k) drives */
  879                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
  880                 /*quirks*/DA_Q_4K
  881         },
  882         {
  883                 /* WDC Caviar Green Advanced Format (4k) drives */
  884                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
  885                 /*quirks*/DA_Q_4K
  886         },
  887         {
  888                 /* WDC Caviar Green Advanced Format (4k) drives */
  889                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
  890                 /*quirks*/DA_Q_4K
  891         },
  892         {
  893                 /* WDC Caviar Green Advanced Format (4k) drives */
  894                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
  895                 /*quirks*/DA_Q_4K
  896         },
  897         {
  898                 /* WDC Caviar Green Advanced Format (4k) drives */
  899                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
  900                 /*quirks*/DA_Q_4K
  901         },
  902         {
  903                 /* WDC Scorpio Black Advanced Format (4k) drives */
  904                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
  905                 /*quirks*/DA_Q_4K
  906         },
  907         {
  908                 /* WDC Scorpio Black Advanced Format (4k) drives */
  909                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
  910                 /*quirks*/DA_Q_4K
  911         },
  912         {
  913                 /* WDC Scorpio Black Advanced Format (4k) drives */
  914                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
  915                 /*quirks*/DA_Q_4K
  916         },
  917         {
  918                 /* WDC Scorpio Black Advanced Format (4k) drives */
  919                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
  920                 /*quirks*/DA_Q_4K
  921         },
  922         {
  923                 /* WDC Scorpio Blue Advanced Format (4k) drives */
  924                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
  925                 /*quirks*/DA_Q_4K
  926         },
  927         {
  928                 /* WDC Scorpio Blue Advanced Format (4k) drives */
  929                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
  930                 /*quirks*/DA_Q_4K
  931         },
  932         {
  933                 /* WDC Scorpio Blue Advanced Format (4k) drives */
  934                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
  935                 /*quirks*/DA_Q_4K
  936         },
  937         {
  938                 /* WDC Scorpio Blue Advanced Format (4k) drives */
  939                 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
  940                 /*quirks*/DA_Q_4K
  941         },
  942         {
  943                 /*
  944                  * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1)
  945                  * PR: usb/97472
  946                  */
  947                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C*", "*"},
  948                 /*quirks*/ DA_Q_NO_6_BYTE | DA_Q_NO_SYNC_CACHE
  949         },
  950         {
  951                 /*
  952                  * Olympus digital cameras (D-370)
  953                  * PR: usb/97472
  954                  */
  955                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "D*", "*"},
  956                 /*quirks*/ DA_Q_NO_6_BYTE
  957         },
  958         {
  959                 /*
  960                  * Olympus digital cameras (E-100RS, E-10).
  961                  * PR: usb/97472
  962                  */
  963                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E*", "*"},
  964                 /*quirks*/ DA_Q_NO_6_BYTE | DA_Q_NO_SYNC_CACHE
  965         },
  966         {
  967                 /*
  968                  * Olympus FE-210 camera
  969                  */
  970                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "FE210*",
  971                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  972         },
  973         {
  974                 /*
  975                  * LG UP3S MP3 player
  976                  */
  977                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "LG", "UP3S",
  978                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  979         },
  980         {
  981                 /*
  982                  * Laser MP3-2GA13 MP3 player
  983                  */
  984                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "(HS) Flash Disk",
  985                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  986         },
  987         {
  988                 /*
  989                  * LaCie external 250GB Hard drive des by Porsche
  990                  * Submitted by: Ben Stuyts <ben@altesco.nl>
  991                  * PR: 121474
  992                  */
  993                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HM250JI", "*"},
  994                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  995         },
  996         /* SATA SSDs */
  997         {
  998                 /*
  999                  * Corsair Force 2 SSDs
 1000                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1001                  */
 1002                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair CSSD-F*", "*" },
 1003                 /*quirks*/DA_Q_4K
 1004         },
 1005         {
 1006                 /*
 1007                  * Corsair Force 3 SSDs
 1008                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1009                  */
 1010                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force 3*", "*" },
 1011                 /*quirks*/DA_Q_4K
 1012         },
 1013         {
 1014                 /*
 1015                  * Corsair Neutron GTX SSDs
 1016                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1017                  */
 1018                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "Corsair Neutron GTX*", "*" },
 1019                 /*quirks*/DA_Q_4K
 1020         },
 1021         {
 1022                 /*
 1023                  * Corsair Force GT & GS SSDs
 1024                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1025                  */
 1026                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Corsair Force G*", "*" },
 1027                 /*quirks*/DA_Q_4K
 1028         },
 1029         {
 1030                 /*
 1031                  * Crucial M4 SSDs
 1032                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1033                  */
 1034                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "M4-CT???M4SSD2*", "*" },
 1035                 /*quirks*/DA_Q_4K
 1036         },
 1037         {
 1038                 /*
 1039                  * Crucial RealSSD C300 SSDs
 1040                  * 4k optimised
 1041                  */
 1042                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "C300-CTFDDAC???MAG*",
 1043                 "*" }, /*quirks*/DA_Q_4K
 1044         },
 1045         {
 1046                 /*
 1047                  * Intel 320 Series SSDs
 1048                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1049                  */
 1050                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2CW*", "*" },
 1051                 /*quirks*/DA_Q_4K
 1052         },
 1053         {
 1054                 /*
 1055                  * Intel 330 Series SSDs
 1056                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1057                  */
 1058                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2CT*", "*" },
 1059                 /*quirks*/DA_Q_4K
 1060         },
 1061         {
 1062                 /*
 1063                  * Intel 510 Series SSDs
 1064                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1065                  */
 1066                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2MH*", "*" },
 1067                 /*quirks*/DA_Q_4K
 1068         },
 1069         {
 1070                 /*
 1071                  * Intel 520 Series SSDs
 1072                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1073                  */
 1074                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSC2BW*", "*" },
 1075                 /*quirks*/DA_Q_4K
 1076         },
 1077         {
 1078                 /*
 1079                  * Intel X25-M Series SSDs
 1080                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1081                  */
 1082                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "INTEL SSDSA2M*", "*" },
 1083                 /*quirks*/DA_Q_4K
 1084         },
 1085         {
 1086                 /*
 1087                  * Kingston E100 Series SSDs
 1088                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1089                  */
 1090                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SE100S3*", "*" },
 1091                 /*quirks*/DA_Q_4K
 1092         },
 1093         {
 1094                 /*
 1095                  * Kingston HyperX 3k SSDs
 1096                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1097                  */
 1098                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "KINGSTON SH103S3*", "*" },
 1099                 /*quirks*/DA_Q_4K
 1100         },
 1101         {
 1102                 /*
 1103                  * Marvell SSDs (entry taken from OpenSolaris)
 1104                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1105                  */
 1106                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "MARVELL SD88SA02*", "*" },
 1107                 /*quirks*/DA_Q_4K
 1108         },
 1109         {
 1110                 /*
 1111                  * OCZ Agility 2 SSDs
 1112                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1113                  */
 1114                 { T_DIRECT, SIP_MEDIA_FIXED, "*", "OCZ-AGILITY2*", "*" },
 1115                 /*quirks*/DA_Q_4K
 1116         },
 1117         {
 1118                 /*
 1119                  * OCZ Agility 3 SSDs
 1120                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1121                  */
 1122                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-AGILITY3*", "*" },
 1123                 /*quirks*/DA_Q_4K
 1124         },
 1125         {
 1126                 /*
 1127                  * OCZ Deneva R Series SSDs
 1128                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1129                  */
 1130                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "DENRSTE251M45*", "*" },
 1131                 /*quirks*/DA_Q_4K
 1132         },
 1133         {
 1134                 /*
 1135                  * OCZ Vertex 2 SSDs (inc pro series)
 1136                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1137                  */
 1138                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ?VERTEX2*", "*" },
 1139                 /*quirks*/DA_Q_4K
 1140         },
 1141         {
 1142                 /*
 1143                  * OCZ Vertex 3 SSDs
 1144                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1145                  */
 1146                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX3*", "*" },
 1147                 /*quirks*/DA_Q_4K
 1148         },
 1149         {
 1150                 /*
 1151                  * OCZ Vertex 4 SSDs
 1152                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1153                  */
 1154                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "OCZ-VERTEX4*", "*" },
 1155                 /*quirks*/DA_Q_4K
 1156         },
 1157         {
 1158                 /*
 1159                  * Samsung 830 Series SSDs
 1160                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1161                  */
 1162                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG SSD 830 Series*", "*" },
 1163                 /*quirks*/DA_Q_4K
 1164         },
 1165         {
 1166                 /*
 1167                  * Samsung 840 SSDs
 1168                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1169                  */
 1170                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 840*", "*" },
 1171                 /*quirks*/DA_Q_4K
 1172         },
 1173         {
 1174                 /*
 1175                  * Samsung 850 SSDs
 1176                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1177                  */
 1178                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "Samsung SSD 850*", "*" },
 1179                 /*quirks*/DA_Q_4K
 1180         },
 1181         {
 1182                 /*
 1183                  * Samsung 843T Series SSDs (MZ7WD*)
 1184                  * Samsung PM851 Series SSDs (MZ7TE*)
 1185                  * Samsung PM853T Series SSDs (MZ7GE*)
 1186                  * Samsung SM863 Series SSDs (MZ7KM*)
 1187                  * 4k optimised
 1188                  */
 1189                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG MZ7*", "*" },
 1190                 /*quirks*/DA_Q_4K
 1191         },
 1192         {
 1193                 /*
 1194                  * SuperTalent TeraDrive CT SSDs
 1195                  * 4k optimised & trim only works in 4k requests + 4k aligned
 1196                  */
 1197                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "FTM??CT25H*", "*" },
 1198                 /*quirks*/DA_Q_4K
 1199         },
 1200         {
 1201                 /*
 1202                  * XceedIOPS SATA SSDs
 1203                  * 4k optimised
 1204                  */
 1205                 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SG9XCS2D*", "*" },
 1206                 /*quirks*/DA_Q_4K
 1207         },
 1208         {
 1209                 /*
 1210                  * Hama Innostor USB-Stick 
 1211                  */
 1212                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Innostor", "Innostor*", "*" }, 
 1213                 /*quirks*/DA_Q_NO_RC16
 1214         },
 1215         {
 1216                 /*
 1217                  * MX-ES USB Drive by Mach Xtreme
 1218                  */
 1219                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "MX", "MXUB3*", "*"},
 1220                 /*quirks*/DA_Q_NO_RC16
 1221         },
 1222 };
 1223 
 1224 static  disk_strategy_t dastrategy;
 1225 static  dumper_t        dadump;
 1226 static  periph_init_t   dainit;
 1227 static  void            daasync(void *callback_arg, u_int32_t code,
 1228                                 struct cam_path *path, void *arg);
 1229 static  void            dasysctlinit(void *context, int pending);
 1230 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
 1231 static  int             dadeletemethodsysctl(SYSCTL_HANDLER_ARGS);
 1232 static  int             dadeletemaxsysctl(SYSCTL_HANDLER_ARGS);
 1233 static  void            dadeletemethodset(struct da_softc *softc,
 1234                                           da_delete_methods delete_method);
 1235 static  off_t           dadeletemaxsize(struct da_softc *softc,
 1236                                         da_delete_methods delete_method);
 1237 static  void            dadeletemethodchoose(struct da_softc *softc,
 1238                                              da_delete_methods default_method);
 1239 static  void            daprobedone(struct cam_periph *periph, union ccb *ccb);
 1240 
 1241 static  periph_ctor_t   daregister;
 1242 static  periph_dtor_t   dacleanup;
 1243 static  periph_start_t  dastart;
 1244 static  periph_oninv_t  daoninvalidate;
 1245 static  void            dadone(struct cam_periph *periph,
 1246                                union ccb *done_ccb);
 1247 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
 1248                                 u_int32_t sense_flags);
 1249 static void             daprevent(struct cam_periph *periph, int action);
 1250 static void             dareprobe(struct cam_periph *periph);
 1251 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
 1252                                   uint64_t maxsector,
 1253                                   struct scsi_read_capacity_data_long *rcaplong,
 1254                                   size_t rcap_size);
 1255 static timeout_t        dasendorderedtag;
 1256 static void             dashutdown(void *arg, int howto);
 1257 static timeout_t        damediapoll;
 1258 
 1259 #ifndef DA_DEFAULT_POLL_PERIOD
 1260 #define DA_DEFAULT_POLL_PERIOD  3
 1261 #endif
 1262 
 1263 #ifndef DA_DEFAULT_TIMEOUT
 1264 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
 1265 #endif
 1266 
 1267 #ifndef DA_DEFAULT_RETRY
 1268 #define DA_DEFAULT_RETRY        4
 1269 #endif
 1270 
 1271 #ifndef DA_DEFAULT_SEND_ORDERED
 1272 #define DA_DEFAULT_SEND_ORDERED 1
 1273 #endif
 1274 
 1275 #define DA_SIO (softc->sort_io_queue >= 0 ? \
 1276     softc->sort_io_queue : cam_sort_io_queues)
 1277 
 1278 static int da_poll_period = DA_DEFAULT_POLL_PERIOD;
 1279 static int da_retry_count = DA_DEFAULT_RETRY;
 1280 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
 1281 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
 1282 
 1283 static SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
 1284             "CAM Direct Access Disk driver");
 1285 SYSCTL_INT(_kern_cam_da, OID_AUTO, poll_period, CTLFLAG_RW,
 1286            &da_poll_period, 0, "Media polling period in seconds");
 1287 TUNABLE_INT("kern.cam.da.poll_period", &da_poll_period);
 1288 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
 1289            &da_retry_count, 0, "Normal I/O retry count");
 1290 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
 1291 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
 1292            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
 1293 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
 1294 SYSCTL_INT(_kern_cam_da, OID_AUTO, send_ordered, CTLFLAG_RW,
 1295            &da_send_ordered, 0, "Send Ordered Tags");
 1296 TUNABLE_INT("kern.cam.da.send_ordered", &da_send_ordered);
 1297 
 1298 /*
 1299  * DA_ORDEREDTAG_INTERVAL determines how often, relative
 1300  * to the default timeout, we check to see whether an ordered
 1301  * tagged transaction is appropriate to prevent simple tag
 1302  * starvation.  Since we'd like to ensure that there is at least
 1303  * 1/2 of the timeout length left for a starved transaction to
 1304  * complete after we've sent an ordered tag, we must poll at least
 1305  * four times in every timeout period.  This takes care of the worst
 1306  * case where a starved transaction starts during an interval that
 1307  * meets the requirement "don't send an ordered tag" test so it takes
 1308  * us two intervals to determine that a tag must be sent.
 1309  */
 1310 #ifndef DA_ORDEREDTAG_INTERVAL
 1311 #define DA_ORDEREDTAG_INTERVAL 4
 1312 #endif
 1313 
 1314 static struct periph_driver dadriver =
 1315 {
 1316         dainit, "da",
 1317         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
 1318 };
 1319 
 1320 PERIPHDRIVER_DECLARE(da, dadriver);
 1321 
 1322 static MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
 1323 
 1324 static int
 1325 daopen(struct disk *dp)
 1326 {
 1327         struct cam_periph *periph;
 1328         struct da_softc *softc;
 1329         int error;
 1330 
 1331         periph = (struct cam_periph *)dp->d_drv1;
 1332         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
 1333                 return (ENXIO);
 1334         }
 1335 
 1336         cam_periph_lock(periph);
 1337         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
 1338                 cam_periph_unlock(periph);
 1339                 cam_periph_release(periph);
 1340                 return (error);
 1341         }
 1342 
 1343         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
 1344             ("daopen\n"));
 1345 
 1346         softc = (struct da_softc *)periph->softc;
 1347         dareprobe(periph);
 1348 
 1349         /* Wait for the disk size update.  */
 1350         error = cam_periph_sleep(periph, &softc->disk->d_mediasize, PRIBIO,
 1351             "dareprobe", 0);
 1352         if (error != 0)
 1353                 xpt_print(periph->path, "unable to retrieve capacity data\n");
 1354 
 1355         if (periph->flags & CAM_PERIPH_INVALID)
 1356                 error = ENXIO;
 1357 
 1358         if (error == 0 && (softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
 1359             (softc->quirks & DA_Q_NO_PREVENT) == 0)
 1360                 daprevent(periph, PR_PREVENT);
 1361 
 1362         if (error == 0) {
 1363                 softc->flags &= ~DA_FLAG_PACK_INVALID;
 1364                 softc->flags |= DA_FLAG_OPEN;
 1365         }
 1366 
 1367         cam_periph_unhold(periph);
 1368         cam_periph_unlock(periph);
 1369 
 1370         if (error != 0)
 1371                 cam_periph_release(periph);
 1372 
 1373         return (error);
 1374 }
 1375 
 1376 static int
 1377 daclose(struct disk *dp)
 1378 {
 1379         struct  cam_periph *periph;
 1380         struct  da_softc *softc;
 1381         union   ccb *ccb;
 1382         int error;
 1383 
 1384         periph = (struct cam_periph *)dp->d_drv1;
 1385         softc = (struct da_softc *)periph->softc;
 1386         cam_periph_lock(periph);
 1387         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE | CAM_DEBUG_PERIPH,
 1388             ("daclose\n"));
 1389 
 1390         if (cam_periph_hold(periph, PRIBIO) == 0) {
 1391 
 1392                 /* Flush disk cache. */
 1393                 if ((softc->flags & DA_FLAG_DIRTY) != 0 &&
 1394                     (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0 &&
 1395                     (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
 1396                         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
 1397                         scsi_synchronize_cache(&ccb->csio, /*retries*/1,
 1398                             /*cbfcnp*/dadone, MSG_SIMPLE_Q_TAG,
 1399                             /*begin_lba*/0, /*lb_count*/0, SSD_FULL_SIZE,
 1400                             5 * 60 * 1000);
 1401                         error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
 1402                             /*sense_flags*/SF_RETRY_UA | SF_QUIET_IR,
 1403                             softc->disk->d_devstat);
 1404                         softc->flags &= ~DA_FLAG_DIRTY;
 1405                         xpt_release_ccb(ccb);
 1406                 }
 1407 
 1408                 /* Allow medium removal. */
 1409                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
 1410                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
 1411                         daprevent(periph, PR_ALLOW);
 1412 
 1413                 cam_periph_unhold(periph);
 1414         }
 1415 
 1416         /*
 1417          * If we've got removeable media, mark the blocksize as
 1418          * unavailable, since it could change when new media is
 1419          * inserted.
 1420          */
 1421         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0)
 1422                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
 1423 
 1424         softc->flags &= ~DA_FLAG_OPEN;
 1425         while (softc->refcount != 0)
 1426                 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "daclose", 1);
 1427         cam_periph_unlock(periph);
 1428         cam_periph_release(periph);
 1429         return (0);
 1430 }
 1431 
 1432 static void
 1433 daschedule(struct cam_periph *periph)
 1434 {
 1435         struct da_softc *softc = (struct da_softc *)periph->softc;
 1436 
 1437         if (softc->state != DA_STATE_NORMAL)
 1438                 return;
 1439 
 1440         /* Check if we have more work to do. */
 1441         if (bioq_first(&softc->bio_queue) ||
 1442             (!softc->delete_running && bioq_first(&softc->delete_queue)) ||
 1443             softc->tur) {
 1444                 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
 1445         }
 1446 }
 1447 
 1448 /*
 1449  * Actually translate the requested transfer into one the physical driver
 1450  * can understand.  The transfer is described by a buf and will include
 1451  * only one physical transfer.
 1452  */
 1453 static void
 1454 dastrategy(struct bio *bp)
 1455 {
 1456         struct cam_periph *periph;
 1457         struct da_softc *softc;
 1458         
 1459         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
 1460         softc = (struct da_softc *)periph->softc;
 1461 
 1462         cam_periph_lock(periph);
 1463 
 1464         /*
 1465          * If the device has been made invalid, error out
 1466          */
 1467         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
 1468                 cam_periph_unlock(periph);
 1469                 biofinish(bp, NULL, ENXIO);
 1470                 return;
 1471         }
 1472 
 1473         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastrategy(%p)\n", bp));
 1474 
 1475         /*
 1476          * Place it in the queue of disk activities for this disk
 1477          */
 1478         if (bp->bio_cmd == BIO_DELETE) {
 1479                 bioq_disksort(&softc->delete_queue, bp);
 1480         } else if (DA_SIO) {
 1481                 bioq_disksort(&softc->bio_queue, bp);
 1482         } else {
 1483                 bioq_insert_tail(&softc->bio_queue, bp);
 1484         }
 1485 
 1486         /*
 1487          * Schedule ourselves for performing the work.
 1488          */
 1489         daschedule(periph);
 1490         cam_periph_unlock(periph);
 1491 
 1492         return;
 1493 }
 1494 
 1495 static int
 1496 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
 1497 {
 1498         struct      cam_periph *periph;
 1499         struct      da_softc *softc;
 1500         u_int       secsize;
 1501         struct      ccb_scsiio csio;
 1502         struct      disk *dp;
 1503         int         error = 0;
 1504 
 1505         dp = arg;
 1506         periph = dp->d_drv1;
 1507         softc = (struct da_softc *)periph->softc;
 1508         cam_periph_lock(periph);
 1509         secsize = softc->params.secsize;
 1510         
 1511         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
 1512                 cam_periph_unlock(periph);
 1513                 return (ENXIO);
 1514         }
 1515 
 1516         if (length > 0) {
 1517                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 1518                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
 1519                 scsi_read_write(&csio,
 1520                                 /*retries*/0,
 1521                                 dadone,
 1522                                 MSG_ORDERED_Q_TAG,
 1523                                 /*read*/SCSI_RW_WRITE,
 1524                                 /*byte2*/0,
 1525                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
 1526                                 offset / secsize,
 1527                                 length / secsize,
 1528                                 /*data_ptr*/(u_int8_t *) virtual,
 1529                                 /*dxfer_len*/length,
 1530                                 /*sense_len*/SSD_FULL_SIZE,
 1531                                 da_default_timeout * 1000);
 1532                 xpt_polled_action((union ccb *)&csio);
 1533 
 1534                 error = cam_periph_error((union ccb *)&csio,
 1535                     0, SF_NO_RECOVERY | SF_NO_RETRY, NULL);
 1536                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
 1537                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
 1538                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
 1539                 if (error != 0)
 1540                         printf("Aborting dump due to I/O error.\n");
 1541                 cam_periph_unlock(periph);
 1542                 return (error);
 1543         }
 1544                 
 1545         /*
 1546          * Sync the disk cache contents to the physical media.
 1547          */
 1548         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
 1549 
 1550                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 1551                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
 1552                 scsi_synchronize_cache(&csio,
 1553                                        /*retries*/0,
 1554                                        /*cbfcnp*/dadone,
 1555                                        MSG_SIMPLE_Q_TAG,
 1556                                        /*begin_lba*/0,/* Cover the whole disk */
 1557                                        /*lb_count*/0,
 1558                                        SSD_FULL_SIZE,
 1559                                        5 * 60 * 1000);
 1560                 xpt_polled_action((union ccb *)&csio);
 1561 
 1562                 error = cam_periph_error((union ccb *)&csio,
 1563                     0, SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR, NULL);
 1564                 if ((csio.ccb_h.status & CAM_DEV_QFRZN) != 0)
 1565                         cam_release_devq(csio.ccb_h.path, /*relsim_flags*/0,
 1566                             /*reduction*/0, /*timeout*/0, /*getcount_only*/0);
 1567                 if (error != 0)
 1568                         xpt_print(periph->path, "Synchronize cache failed\n");
 1569         }
 1570         cam_periph_unlock(periph);
 1571         return (error);
 1572 }
 1573 
 1574 static int
 1575 dagetattr(struct bio *bp)
 1576 {
 1577         int ret;
 1578         struct cam_periph *periph;
 1579 
 1580         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
 1581         cam_periph_lock(periph);
 1582         ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
 1583             periph->path);
 1584         cam_periph_unlock(periph);
 1585         if (ret == 0)
 1586                 bp->bio_completed = bp->bio_length;
 1587         return ret;
 1588 }
 1589 
 1590 static void
 1591 dainit(void)
 1592 {
 1593         cam_status status;
 1594 
 1595         /*
 1596          * Install a global async callback.  This callback will
 1597          * receive async callbacks like "new device found".
 1598          */
 1599         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
 1600 
 1601         if (status != CAM_REQ_CMP) {
 1602                 printf("da: Failed to attach master async callback "
 1603                        "due to status 0x%x!\n", status);
 1604         } else if (da_send_ordered) {
 1605 
 1606                 /* Register our shutdown event handler */
 1607                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
 1608                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
 1609                     printf("dainit: shutdown event registration failed!\n");
 1610         }
 1611 }
 1612 
 1613 /*
 1614  * Callback from GEOM, called when it has finished cleaning up its
 1615  * resources.
 1616  */
 1617 static void
 1618 dadiskgonecb(struct disk *dp)
 1619 {
 1620         struct cam_periph *periph;
 1621 
 1622         periph = (struct cam_periph *)dp->d_drv1;
 1623         cam_periph_release(periph);
 1624 }
 1625 
 1626 static void
 1627 daoninvalidate(struct cam_periph *periph)
 1628 {
 1629         struct da_softc *softc;
 1630 
 1631         softc = (struct da_softc *)periph->softc;
 1632 
 1633         /*
 1634          * De-register any async callbacks.
 1635          */
 1636         xpt_register_async(0, daasync, periph, periph->path);
 1637 
 1638         softc->flags |= DA_FLAG_PACK_INVALID;
 1639 
 1640         /*
 1641          * Return all queued I/O with ENXIO.
 1642          * XXX Handle any transactions queued to the card
 1643          *     with XPT_ABORT_CCB.
 1644          */
 1645         bioq_flush(&softc->bio_queue, NULL, ENXIO);
 1646         bioq_flush(&softc->delete_queue, NULL, ENXIO);
 1647 
 1648         /*
 1649          * Tell GEOM that we've gone away, we'll get a callback when it is
 1650          * done cleaning up its resources.
 1651          */
 1652         disk_gone(softc->disk);
 1653 }
 1654 
 1655 static void
 1656 dacleanup(struct cam_periph *periph)
 1657 {
 1658         struct da_softc *softc;
 1659 
 1660         softc = (struct da_softc *)periph->softc;
 1661 
 1662         cam_periph_unlock(periph);
 1663 
 1664         /*
 1665          * If we can't free the sysctl tree, oh well...
 1666          */
 1667         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
 1668             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
 1669                 xpt_print(periph->path, "can't remove sysctl context\n");
 1670         }
 1671 
 1672         callout_drain(&softc->mediapoll_c);
 1673         disk_destroy(softc->disk);
 1674         callout_drain(&softc->sendordered_c);
 1675         free(softc, M_DEVBUF);
 1676         cam_periph_lock(periph);
 1677 }
 1678 
 1679 static void
 1680 daasync(void *callback_arg, u_int32_t code,
 1681         struct cam_path *path, void *arg)
 1682 {
 1683         struct cam_periph *periph;
 1684         struct da_softc *softc;
 1685 
 1686         periph = (struct cam_periph *)callback_arg;
 1687         switch (code) {
 1688         case AC_FOUND_DEVICE:
 1689         {
 1690                 struct ccb_getdev *cgd;
 1691                 cam_status status;
 1692  
 1693                 cgd = (struct ccb_getdev *)arg;
 1694                 if (cgd == NULL)
 1695                         break;
 1696 
 1697                 if (cgd->protocol != PROTO_SCSI)
 1698                         break;
 1699                 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
 1700                         break;
 1701                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
 1702                     && SID_TYPE(&cgd->inq_data) != T_RBC
 1703                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
 1704                         break;
 1705 
 1706                 /*
 1707                  * Allocate a peripheral instance for
 1708                  * this device and start the probe
 1709                  * process.
 1710                  */
 1711                 status = cam_periph_alloc(daregister, daoninvalidate,
 1712                                           dacleanup, dastart,
 1713                                           "da", CAM_PERIPH_BIO,
 1714                                           path, daasync,
 1715                                           AC_FOUND_DEVICE, cgd);
 1716 
 1717                 if (status != CAM_REQ_CMP
 1718                  && status != CAM_REQ_INPROG)
 1719                         printf("daasync: Unable to attach to new device "
 1720                                 "due to status 0x%x\n", status);
 1721                 return;
 1722         }
 1723         case AC_ADVINFO_CHANGED:
 1724         {
 1725                 uintptr_t buftype;
 1726 
 1727                 buftype = (uintptr_t)arg;
 1728                 if (buftype == CDAI_TYPE_PHYS_PATH) {
 1729                         struct da_softc *softc;
 1730 
 1731                         softc = periph->softc;
 1732                         disk_attr_changed(softc->disk, "GEOM::physpath",
 1733                                           M_NOWAIT);
 1734                 }
 1735                 break;
 1736         }
 1737         case AC_UNIT_ATTENTION:
 1738         {
 1739                 union ccb *ccb;
 1740                 int error_code, sense_key, asc, ascq;
 1741 
 1742                 softc = (struct da_softc *)periph->softc;
 1743                 ccb = (union ccb *)arg;
 1744 
 1745                 /*
 1746                  * Handle all UNIT ATTENTIONs except our own,
 1747                  * as they will be handled by daerror().
 1748                  */
 1749                 if (xpt_path_periph(ccb->ccb_h.path) != periph &&
 1750                     scsi_extract_sense_ccb(ccb,
 1751                      &error_code, &sense_key, &asc, &ascq)) {
 1752                         if (asc == 0x2A && ascq == 0x09) {
 1753                                 xpt_print(ccb->ccb_h.path,
 1754                                     "Capacity data has changed\n");
 1755                                 softc->flags &= ~DA_FLAG_PROBED;
 1756                                 dareprobe(periph);
 1757                         } else if (asc == 0x28 && ascq == 0x00) {
 1758                                 softc->flags &= ~DA_FLAG_PROBED;
 1759                                 disk_media_changed(softc->disk, M_NOWAIT);
 1760                         } else if (asc == 0x3F && ascq == 0x03) {
 1761                                 xpt_print(ccb->ccb_h.path,
 1762                                     "INQUIRY data has changed\n");
 1763                                 softc->flags &= ~DA_FLAG_PROBED;
 1764                                 dareprobe(periph);
 1765                         }
 1766                 }
 1767                 cam_periph_async(periph, code, path, arg);
 1768                 break;
 1769         }
 1770         case AC_SCSI_AEN:
 1771                 softc = (struct da_softc *)periph->softc;
 1772                 if (!softc->tur) {
 1773                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
 1774                                 softc->tur = 1;
 1775                                 daschedule(periph);
 1776                         }
 1777                 }
 1778                 /* FALLTHROUGH */
 1779         case AC_SENT_BDR:
 1780         case AC_BUS_RESET:
 1781         {
 1782                 struct ccb_hdr *ccbh;
 1783 
 1784                 softc = (struct da_softc *)periph->softc;
 1785                 /*
 1786                  * Don't fail on the expected unit attention
 1787                  * that will occur.
 1788                  */
 1789                 softc->flags |= DA_FLAG_RETRY_UA;
 1790                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
 1791                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
 1792                 break;
 1793         }
 1794         case AC_INQ_CHANGED:
 1795                 softc = (struct da_softc *)periph->softc;
 1796                 softc->flags &= ~DA_FLAG_PROBED;
 1797                 dareprobe(periph);
 1798                 break;
 1799         default:
 1800                 break;
 1801         }
 1802         cam_periph_async(periph, code, path, arg);
 1803 }
 1804 
 1805 static void
 1806 dasysctlinit(void *context, int pending)
 1807 {
 1808         struct cam_periph *periph;
 1809         struct da_softc *softc;
 1810         char tmpstr[80], tmpstr2[80];
 1811         struct ccb_trans_settings cts;
 1812 
 1813         periph = (struct cam_periph *)context;
 1814         /*
 1815          * periph was held for us when this task was enqueued
 1816          */
 1817         if (periph->flags & CAM_PERIPH_INVALID) {
 1818                 cam_periph_release(periph);
 1819                 return;
 1820         }
 1821 
 1822         softc = (struct da_softc *)periph->softc;
 1823         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
 1824         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
 1825 
 1826         sysctl_ctx_init(&softc->sysctl_ctx);
 1827         softc->flags |= DA_FLAG_SCTX_INIT;
 1828         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
 1829                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
 1830                 CTLFLAG_RD, 0, tmpstr);
 1831         if (softc->sysctl_tree == NULL) {
 1832                 printf("dasysctlinit: unable to allocate sysctl tree\n");
 1833                 cam_periph_release(periph);
 1834                 return;
 1835         }
 1836 
 1837         /*
 1838          * Now register the sysctl handler, so the user can change the value on
 1839          * the fly.
 1840          */
 1841         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
 1842                 OID_AUTO, "delete_method", CTLTYPE_STRING | CTLFLAG_RWTUN,
 1843                 softc, 0, dadeletemethodsysctl, "A",
 1844                 "BIO_DELETE execution method");
 1845         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
 1846                 OID_AUTO, "delete_max", CTLTYPE_U64 | CTLFLAG_RW,
 1847                 softc, 0, dadeletemaxsysctl, "Q",
 1848                 "Maximum BIO_DELETE size");
 1849         SYSCTL_ADD_PROC(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
 1850                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
 1851                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
 1852                 "Minimum CDB size");
 1853         SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
 1854                 OID_AUTO, "sort_io_queue", CTLFLAG_RW, &softc->sort_io_queue, 0,
 1855                 "Sort IO queue to try and optimise disk access patterns");
 1856 
 1857         SYSCTL_ADD_INT(&softc->sysctl_ctx,
 1858                        SYSCTL_CHILDREN(softc->sysctl_tree),
 1859                        OID_AUTO,
 1860                        "error_inject",
 1861                        CTLFLAG_RW,
 1862                        &softc->error_inject,
 1863                        0,
 1864                        "error_inject leaf");
 1865 
 1866 
 1867         /*
 1868          * Add some addressing info.
 1869          */
 1870         memset(&cts, 0, sizeof (cts));
 1871         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
 1872         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
 1873         cts.type = CTS_TYPE_CURRENT_SETTINGS;
 1874         cam_periph_lock(periph);
 1875         xpt_action((union ccb *)&cts);
 1876         cam_periph_unlock(periph);
 1877         if (cts.ccb_h.status != CAM_REQ_CMP) {
 1878                 cam_periph_release(periph);
 1879                 return;
 1880         }
 1881         if (cts.protocol == PROTO_SCSI && cts.transport == XPORT_FC) {
 1882                 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
 1883                 if (fc->valid & CTS_FC_VALID_WWPN) {
 1884                         softc->wwpn = fc->wwpn;
 1885                         SYSCTL_ADD_UQUAD(&softc->sysctl_ctx,
 1886                             SYSCTL_CHILDREN(softc->sysctl_tree),
 1887                             OID_AUTO, "wwpn", CTLFLAG_RD,
 1888                             &softc->wwpn, "World Wide Port Name");
 1889                 }
 1890         }
 1891         cam_periph_release(periph);
 1892 }
 1893 
 1894 static int
 1895 dadeletemaxsysctl(SYSCTL_HANDLER_ARGS)
 1896 {
 1897         int error;
 1898         uint64_t value;
 1899         struct da_softc *softc;
 1900 
 1901         softc = (struct da_softc *)arg1;
 1902 
 1903         value = softc->disk->d_delmaxsize;
 1904         error = sysctl_handle_64(oidp, &value, 0, req);
 1905         if ((error != 0) || (req->newptr == NULL))
 1906                 return (error);
 1907 
 1908         /* only accept values smaller than the calculated value */
 1909         if (value > dadeletemaxsize(softc, softc->delete_method)) {
 1910                 return (EINVAL);
 1911         }
 1912         softc->disk->d_delmaxsize = value;
 1913 
 1914         return (0);
 1915 }
 1916 
 1917 static int
 1918 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
 1919 {
 1920         int error, value;
 1921 
 1922         value = *(int *)arg1;
 1923 
 1924         error = sysctl_handle_int(oidp, &value, 0, req);
 1925 
 1926         if ((error != 0)
 1927          || (req->newptr == NULL))
 1928                 return (error);
 1929 
 1930         /*
 1931          * Acceptable values here are 6, 10, 12 or 16.
 1932          */
 1933         if (value < 6)
 1934                 value = 6;
 1935         else if ((value > 6)
 1936               && (value <= 10))
 1937                 value = 10;
 1938         else if ((value > 10)
 1939               && (value <= 12))
 1940                 value = 12;
 1941         else if (value > 12)
 1942                 value = 16;
 1943 
 1944         *(int *)arg1 = value;
 1945 
 1946         return (0);
 1947 }
 1948 
 1949 static void
 1950 dadeletemethodset(struct da_softc *softc, da_delete_methods delete_method)
 1951 {
 1952 
 1953         softc->delete_method = delete_method;
 1954         softc->disk->d_delmaxsize = dadeletemaxsize(softc, delete_method);
 1955         softc->delete_func = da_delete_functions[delete_method];
 1956 
 1957         if (softc->delete_method > DA_DELETE_DISABLE)
 1958                 softc->disk->d_flags |= DISKFLAG_CANDELETE;
 1959         else
 1960                 softc->disk->d_flags &= ~DISKFLAG_CANDELETE;
 1961 }
 1962 
 1963 static off_t
 1964 dadeletemaxsize(struct da_softc *softc, da_delete_methods delete_method)
 1965 {
 1966         off_t sectors;
 1967 
 1968         switch(delete_method) {
 1969         case DA_DELETE_UNMAP:
 1970                 sectors = (off_t)softc->unmap_max_lba;
 1971                 break;
 1972         case DA_DELETE_ATA_TRIM:
 1973                 sectors = (off_t)ATA_DSM_RANGE_MAX * softc->trim_max_ranges;
 1974                 break;
 1975         case DA_DELETE_WS16:
 1976                 sectors = omin(softc->ws_max_blks, WS16_MAX_BLKS);
 1977                 break;
 1978         case DA_DELETE_ZERO:
 1979         case DA_DELETE_WS10:
 1980                 sectors = omin(softc->ws_max_blks, WS10_MAX_BLKS);
 1981                 break;
 1982         default:
 1983                 return 0;
 1984         }
 1985 
 1986         return (off_t)softc->params.secsize *
 1987             omin(sectors, softc->params.sectors);
 1988 }
 1989 
 1990 static void
 1991 daprobedone(struct cam_periph *periph, union ccb *ccb)
 1992 {
 1993         struct da_softc *softc;
 1994 
 1995         softc = (struct da_softc *)periph->softc;
 1996 
 1997         dadeletemethodchoose(softc, DA_DELETE_NONE);
 1998 
 1999         if (bootverbose && (softc->flags & DA_FLAG_ANNOUNCED) == 0) {
 2000                 char buf[80];
 2001                 int i, sep;
 2002 
 2003                 snprintf(buf, sizeof(buf), "Delete methods: <");
 2004                 sep = 0;
 2005                 for (i = 0; i <= DA_DELETE_MAX; i++) {
 2006                         if ((softc->delete_available & (1 << i)) == 0 &&
 2007                             i != softc->delete_method)
 2008                                 continue;
 2009                         if (sep)
 2010                                 strlcat(buf, ",", sizeof(buf));
 2011                         strlcat(buf, da_delete_method_names[i],
 2012                             sizeof(buf));
 2013                         if (i == softc->delete_method)
 2014                                 strlcat(buf, "(*)", sizeof(buf));
 2015                         sep = 1;
 2016                 }
 2017                 strlcat(buf, ">", sizeof(buf));
 2018                 printf("%s%d: %s\n", periph->periph_name,
 2019                     periph->unit_number, buf);
 2020         }
 2021 
 2022         /*
 2023          * Since our peripheral may be invalidated by an error
 2024          * above or an external event, we must release our CCB
 2025          * before releasing the probe lock on the peripheral.
 2026          * The peripheral will only go away once the last lock
 2027          * is removed, and we need it around for the CCB release
 2028          * operation.
 2029          */
 2030         xpt_release_ccb(ccb);
 2031         softc->state = DA_STATE_NORMAL;
 2032         softc->flags |= DA_FLAG_PROBED;
 2033         daschedule(periph);
 2034         wakeup(&softc->disk->d_mediasize);
 2035         if ((softc->flags & DA_FLAG_ANNOUNCED) == 0) {
 2036                 softc->flags |= DA_FLAG_ANNOUNCED;
 2037                 cam_periph_unhold(periph);
 2038         } else
 2039                 cam_periph_release_locked(periph);
 2040 }
 2041 
 2042 static void
 2043 dadeletemethodchoose(struct da_softc *softc, da_delete_methods default_method)
 2044 {
 2045         int i, methods;
 2046 
 2047         /* If available, prefer the method requested by user. */
 2048         i = softc->delete_method_pref;
 2049         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
 2050         if (methods & (1 << i)) {
 2051                 dadeletemethodset(softc, i);
 2052                 return;
 2053         }
 2054 
 2055         /* Use the pre-defined order to choose the best performing delete. */
 2056         for (i = DA_DELETE_MIN; i <= DA_DELETE_MAX; i++) {
 2057                 if (i == DA_DELETE_ZERO)
 2058                         continue;
 2059                 if (softc->delete_available & (1 << i)) {
 2060                         dadeletemethodset(softc, i);
 2061                         return;
 2062                 }
 2063         }
 2064 
 2065         /* Fallback to default. */
 2066         dadeletemethodset(softc, default_method);
 2067 }
 2068 
 2069 static int
 2070 dadeletemethodsysctl(SYSCTL_HANDLER_ARGS)
 2071 {
 2072         char buf[16];
 2073         const char *p;
 2074         struct da_softc *softc;
 2075         int i, error, methods, value;
 2076 
 2077         softc = (struct da_softc *)arg1;
 2078 
 2079         value = softc->delete_method;
 2080         if (value < 0 || value > DA_DELETE_MAX)
 2081                 p = "UNKNOWN";
 2082         else
 2083                 p = da_delete_method_names[value];
 2084         strncpy(buf, p, sizeof(buf));
 2085         error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
 2086         if (error != 0 || req->newptr == NULL)
 2087                 return (error);
 2088         methods = softc->delete_available | (1 << DA_DELETE_DISABLE);
 2089         for (i = 0; i <= DA_DELETE_MAX; i++) {
 2090                 if (strcmp(buf, da_delete_method_names[i]) == 0)
 2091                         break;
 2092         }
 2093         if (i > DA_DELETE_MAX)
 2094                 return (EINVAL);
 2095         softc->delete_method_pref = i;
 2096         dadeletemethodchoose(softc, DA_DELETE_NONE);
 2097         return (0);
 2098 }
 2099 
 2100 static cam_status
 2101 daregister(struct cam_periph *periph, void *arg)
 2102 {
 2103         struct da_softc *softc;
 2104         struct ccb_pathinq cpi;
 2105         struct ccb_getdev *cgd;
 2106         char tmpstr[80];
 2107         caddr_t match;
 2108 
 2109         cgd = (struct ccb_getdev *)arg;
 2110         if (cgd == NULL) {
 2111                 printf("daregister: no getdev CCB, can't register device\n");
 2112                 return(CAM_REQ_CMP_ERR);
 2113         }
 2114 
 2115         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
 2116             M_NOWAIT|M_ZERO);
 2117 
 2118         if (softc == NULL) {
 2119                 printf("daregister: Unable to probe new device. "
 2120                        "Unable to allocate softc\n");                           
 2121                 return(CAM_REQ_CMP_ERR);
 2122         }
 2123 
 2124         LIST_INIT(&softc->pending_ccbs);
 2125         softc->state = DA_STATE_PROBE_WP;
 2126         bioq_init(&softc->bio_queue);
 2127         bioq_init(&softc->delete_queue);
 2128         bioq_init(&softc->delete_run_queue);
 2129         if (SID_IS_REMOVABLE(&cgd->inq_data))
 2130                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
 2131         softc->unmap_max_ranges = UNMAP_MAX_RANGES;
 2132         softc->unmap_max_lba = UNMAP_RANGE_MAX;
 2133         softc->ws_max_blks = WS16_MAX_BLKS;
 2134         softc->trim_max_ranges = ATA_TRIM_MAX_RANGES;
 2135         softc->sort_io_queue = -1;
 2136 
 2137         periph->softc = softc;
 2138 
 2139         /*
 2140          * See if this device has any quirks.
 2141          */
 2142         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
 2143                                (caddr_t)da_quirk_table,
 2144                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
 2145                                sizeof(*da_quirk_table), scsi_inquiry_match);
 2146 
 2147         if (match != NULL)
 2148                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
 2149         else
 2150                 softc->quirks = DA_Q_NONE;
 2151 
 2152         /* Check if the SIM does not want 6 byte commands */
 2153         bzero(&cpi, sizeof(cpi));
 2154         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 2155         cpi.ccb_h.func_code = XPT_PATH_INQ;
 2156         xpt_action((union ccb *)&cpi);
 2157         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
 2158                 softc->quirks |= DA_Q_NO_6_BYTE;
 2159 
 2160         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
 2161 
 2162         /*
 2163          * Take an exclusive refcount on the periph while dastart is called
 2164          * to finish the probe.  The reference will be dropped in dadone at
 2165          * the end of probe.
 2166          */
 2167         (void)cam_periph_hold(periph, PRIBIO);
 2168 
 2169         /*
 2170          * Schedule a periodic event to occasionally send an
 2171          * ordered tag to a device.
 2172          */
 2173         callout_init_mtx(&softc->sendordered_c, cam_periph_mtx(periph), 0);
 2174         callout_reset(&softc->sendordered_c,
 2175             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
 2176             dasendorderedtag, softc);
 2177 
 2178         cam_periph_unlock(periph);
 2179         /*
 2180          * RBC devices don't have to support READ(6), only READ(10).
 2181          */
 2182         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
 2183                 softc->minimum_cmd_size = 10;
 2184         else
 2185                 softc->minimum_cmd_size = 6;
 2186 
 2187         /*
 2188          * Load the user's default, if any.
 2189          */
 2190         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
 2191                  periph->unit_number);
 2192         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
 2193 
 2194         /*
 2195          * 6, 10, 12 and 16 are the currently permissible values.
 2196          */
 2197         if (softc->minimum_cmd_size < 6)
 2198                 softc->minimum_cmd_size = 6;
 2199         else if ((softc->minimum_cmd_size > 6)
 2200               && (softc->minimum_cmd_size <= 10))
 2201                 softc->minimum_cmd_size = 10;
 2202         else if ((softc->minimum_cmd_size > 10)
 2203               && (softc->minimum_cmd_size <= 12))
 2204                 softc->minimum_cmd_size = 12;
 2205         else if (softc->minimum_cmd_size > 12)
 2206                 softc->minimum_cmd_size = 16;
 2207 
 2208         /* Predict whether device may support READ CAPACITY(16). */
 2209         if (SID_ANSI_REV(&cgd->inq_data) >= SCSI_REV_SPC3 &&
 2210             (softc->quirks & DA_Q_NO_RC16) == 0) {
 2211                 softc->flags |= DA_FLAG_CAN_RC16;
 2212         }
 2213 
 2214         /*
 2215          * Register this media as a disk.
 2216          */
 2217         softc->disk = disk_alloc();
 2218         softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
 2219                           periph->unit_number, 0,
 2220                           DEVSTAT_BS_UNAVAILABLE,
 2221                           SID_TYPE(&cgd->inq_data) |
 2222                           XPORT_DEVSTAT_TYPE(cpi.transport),
 2223                           DEVSTAT_PRIORITY_DISK);
 2224         softc->disk->d_open = daopen;
 2225         softc->disk->d_close = daclose;
 2226         softc->disk->d_strategy = dastrategy;
 2227         softc->disk->d_dump = dadump;
 2228         softc->disk->d_getattr = dagetattr;
 2229         softc->disk->d_gone = dadiskgonecb;
 2230         softc->disk->d_name = "da";
 2231         softc->disk->d_drv1 = periph;
 2232         if (cpi.maxio == 0)
 2233                 softc->maxio = DFLTPHYS;        /* traditional default */
 2234         else if (cpi.maxio > MAXPHYS)
 2235                 softc->maxio = MAXPHYS;         /* for safety */
 2236         else
 2237                 softc->maxio = cpi.maxio;
 2238         softc->disk->d_maxsize = softc->maxio;
 2239         softc->disk->d_unit = periph->unit_number;
 2240         softc->disk->d_flags = DISKFLAG_DIRECT_COMPLETION;
 2241         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
 2242                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
 2243         if ((cpi.hba_misc & PIM_UNMAPPED) != 0)
 2244                 softc->disk->d_flags |= DISKFLAG_UNMAPPED_BIO;
 2245         cam_strvis(softc->disk->d_descr, cgd->inq_data.vendor,
 2246             sizeof(cgd->inq_data.vendor), sizeof(softc->disk->d_descr));
 2247         strlcat(softc->disk->d_descr, " ", sizeof(softc->disk->d_descr));
 2248         cam_strvis(&softc->disk->d_descr[strlen(softc->disk->d_descr)],
 2249             cgd->inq_data.product, sizeof(cgd->inq_data.product),
 2250             sizeof(softc->disk->d_descr) - strlen(softc->disk->d_descr));
 2251         softc->disk->d_hba_vendor = cpi.hba_vendor;
 2252         softc->disk->d_hba_device = cpi.hba_device;
 2253         softc->disk->d_hba_subvendor = cpi.hba_subvendor;
 2254         softc->disk->d_hba_subdevice = cpi.hba_subdevice;
 2255 
 2256         /*
 2257          * Acquire a reference to the periph before we register with GEOM.
 2258          * We'll release this reference once GEOM calls us back (via
 2259          * dadiskgonecb()) telling us that our provider has been freed.
 2260          */
 2261         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
 2262                 xpt_print(periph->path, "%s: lost periph during "
 2263                           "registration!\n", __func__);
 2264                 cam_periph_lock(periph);
 2265                 return (CAM_REQ_CMP_ERR);
 2266         }
 2267 
 2268         disk_create(softc->disk, DISK_VERSION);
 2269         cam_periph_lock(periph);
 2270 
 2271         /*
 2272          * Add async callbacks for events of interest.
 2273          * I don't bother checking if this fails as,
 2274          * in most cases, the system will function just
 2275          * fine without them and the only alternative
 2276          * would be to not attach the device on failure.
 2277          */
 2278         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
 2279             AC_ADVINFO_CHANGED | AC_SCSI_AEN | AC_UNIT_ATTENTION |
 2280             AC_INQ_CHANGED, daasync, periph, periph->path);
 2281 
 2282         /*
 2283          * Emit an attribute changed notification just in case 
 2284          * physical path information arrived before our async
 2285          * event handler was registered, but after anyone attaching
 2286          * to our disk device polled it.
 2287          */
 2288         disk_attr_changed(softc->disk, "GEOM::physpath", M_NOWAIT);
 2289 
 2290         /*
 2291          * Schedule a periodic media polling events.
 2292          */
 2293         callout_init_mtx(&softc->mediapoll_c, cam_periph_mtx(periph), 0);
 2294         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) &&
 2295             (cgd->inq_flags & SID_AEN) == 0 &&
 2296             da_poll_period != 0)
 2297                 callout_reset(&softc->mediapoll_c, da_poll_period * hz,
 2298                     damediapoll, periph);
 2299 
 2300         xpt_schedule(periph, CAM_PRIORITY_DEV);
 2301 
 2302         return(CAM_REQ_CMP);
 2303 }
 2304 
 2305 static void
 2306 dastart(struct cam_periph *periph, union ccb *start_ccb)
 2307 {
 2308         struct da_softc *softc;
 2309 
 2310         softc = (struct da_softc *)periph->softc;
 2311 
 2312         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dastart\n"));
 2313 
 2314 skipstate:
 2315         switch (softc->state) {
 2316         case DA_STATE_NORMAL:
 2317         {
 2318                 struct bio *bp;
 2319                 uint8_t tag_code;
 2320 
 2321                 /* Run BIO_DELETE if not running yet. */
 2322                 if (!softc->delete_running &&
 2323                     (bp = bioq_first(&softc->delete_queue)) != NULL) {
 2324                         if (softc->delete_func != NULL) {
 2325                                 softc->delete_func(periph, start_ccb, bp);
 2326                                 goto out;
 2327                         } else {
 2328                                 bioq_flush(&softc->delete_queue, NULL, 0);
 2329                                 /* FALLTHROUGH */
 2330                         }
 2331                 }
 2332 
 2333                 /* Run regular command. */
 2334                 bp = bioq_takefirst(&softc->bio_queue);
 2335                 if (bp == NULL) {
 2336                         if (softc->tur) {
 2337                                 softc->tur = 0;
 2338                                 scsi_test_unit_ready(&start_ccb->csio,
 2339                                      /*retries*/ da_retry_count,
 2340                                      dadone,
 2341                                      MSG_SIMPLE_Q_TAG,
 2342                                      SSD_FULL_SIZE,
 2343                                      da_default_timeout * 1000);
 2344                                 start_ccb->ccb_h.ccb_bp = NULL;
 2345                                 start_ccb->ccb_h.ccb_state = DA_CCB_TUR;
 2346                                 xpt_action(start_ccb);
 2347                         } else
 2348                                 xpt_release_ccb(start_ccb);
 2349                         break;
 2350                 }
 2351                 if (softc->tur) {
 2352                         softc->tur = 0;
 2353                         cam_periph_release_locked(periph);
 2354                 }
 2355 
 2356                 if ((bp->bio_flags & BIO_ORDERED) != 0 ||
 2357                     (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
 2358                         softc->flags &= ~DA_FLAG_NEED_OTAG;
 2359                         softc->flags |= DA_FLAG_WAS_OTAG;
 2360                         tag_code = MSG_ORDERED_Q_TAG;
 2361                 } else {
 2362                         tag_code = MSG_SIMPLE_Q_TAG;
 2363                 }
 2364 
 2365                 switch (bp->bio_cmd) {
 2366                 case BIO_WRITE:
 2367                 case BIO_READ:
 2368                 {
 2369                         void *data_ptr;
 2370                         int rw_op;
 2371 
 2372                         if (bp->bio_cmd == BIO_WRITE) {
 2373                                 softc->flags |= DA_FLAG_DIRTY;
 2374                                 rw_op = SCSI_RW_WRITE;
 2375                         } else {
 2376                                 rw_op = SCSI_RW_READ;
 2377                         }
 2378 
 2379                         data_ptr = bp->bio_data;
 2380                         if ((bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0) {
 2381                                 rw_op |= SCSI_RW_BIO;
 2382                                 data_ptr = bp;
 2383                         }
 2384 
 2385                         scsi_read_write(&start_ccb->csio,
 2386                                         /*retries*/da_retry_count,
 2387                                         /*cbfcnp*/dadone,
 2388                                         /*tag_action*/tag_code,
 2389                                         rw_op,
 2390                                         /*byte2*/0,
 2391                                         softc->minimum_cmd_size,
 2392                                         /*lba*/bp->bio_pblkno,
 2393                                         /*block_count*/bp->bio_bcount /
 2394                                         softc->params.secsize,
 2395                                         data_ptr,
 2396                                         /*dxfer_len*/ bp->bio_bcount,
 2397                                         /*sense_len*/SSD_FULL_SIZE,
 2398                                         da_default_timeout * 1000);
 2399                         break;
 2400                 }
 2401                 case BIO_FLUSH:
 2402                         /*
 2403                          * BIO_FLUSH doesn't currently communicate
 2404                          * range data, so we synchronize the cache
 2405                          * over the whole disk.  We also force
 2406                          * ordered tag semantics the flush applies
 2407                          * to all previously queued I/O.
 2408                          */
 2409                         scsi_synchronize_cache(&start_ccb->csio,
 2410                                                /*retries*/1,
 2411                                                /*cbfcnp*/dadone,
 2412                                                MSG_ORDERED_Q_TAG,
 2413                                                /*begin_lba*/0,
 2414                                                /*lb_count*/0,
 2415                                                SSD_FULL_SIZE,
 2416                                                da_default_timeout*1000);
 2417                         break;
 2418                 }
 2419                 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
 2420                 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
 2421 
 2422 out:
 2423                 LIST_INSERT_HEAD(&softc->pending_ccbs,
 2424                                  &start_ccb->ccb_h, periph_links.le);
 2425 
 2426                 /* We expect a unit attention from this device */
 2427                 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
 2428                         start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
 2429                         softc->flags &= ~DA_FLAG_RETRY_UA;
 2430                 }
 2431 
 2432                 start_ccb->ccb_h.ccb_bp = bp;
 2433                 softc->refcount++;
 2434                 cam_periph_unlock(periph);
 2435                 xpt_action(start_ccb);
 2436                 cam_periph_lock(periph);
 2437                 softc->refcount--;
 2438 
 2439                 /* May have more work to do, so ensure we stay scheduled */
 2440                 daschedule(periph);
 2441                 break;
 2442         }
 2443         case DA_STATE_PROBE_WP:
 2444         {
 2445                 void  *mode_buf;
 2446                 int    mode_buf_len;
 2447 
 2448                 mode_buf_len = 192;
 2449                 mode_buf = malloc(mode_buf_len, M_SCSIDA, M_NOWAIT);
 2450                 if (mode_buf == NULL) {
 2451                         xpt_print(periph->path, "Unable to send mode sense - "
 2452                             "malloc failure\n");
 2453                         softc->state = DA_STATE_PROBE_RC;
 2454                         goto skipstate;
 2455                 }
 2456                 scsi_mode_sense_len(&start_ccb->csio,
 2457                                     /*retries*/ da_retry_count,
 2458                                     /*cbfcnp*/ dadone,
 2459                                     /*tag_action*/ MSG_SIMPLE_Q_TAG,
 2460                                     /*dbd*/ FALSE,
 2461                                     /*pc*/ SMS_PAGE_CTRL_CURRENT,
 2462                                     /*page*/ SMS_ALL_PAGES_PAGE,
 2463                                     /*param_buf*/ mode_buf,
 2464                                     /*param_len*/ mode_buf_len,
 2465                                     /*minimum_cmd_size*/ softc->minimum_cmd_size,
 2466                                     /*sense_len*/ SSD_FULL_SIZE,
 2467                                     /*timeout*/ da_default_timeout * 1000);
 2468                 start_ccb->ccb_h.ccb_bp = NULL;
 2469                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_WP;
 2470                 xpt_action(start_ccb);
 2471                 break;
 2472         }
 2473         case DA_STATE_PROBE_RC:
 2474         {
 2475                 struct scsi_read_capacity_data *rcap;
 2476 
 2477                 rcap = (struct scsi_read_capacity_data *)
 2478                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
 2479                 if (rcap == NULL) {
 2480                         printf("dastart: Couldn't malloc read_capacity data\n");
 2481                         /* da_free_periph??? */
 2482                         break;
 2483                 }
 2484                 scsi_read_capacity(&start_ccb->csio,
 2485                                    /*retries*/da_retry_count,
 2486                                    dadone,
 2487                                    MSG_SIMPLE_Q_TAG,
 2488                                    rcap,
 2489                                    SSD_FULL_SIZE,
 2490                                    /*timeout*/5000);
 2491                 start_ccb->ccb_h.ccb_bp = NULL;
 2492                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC;
 2493                 xpt_action(start_ccb);
 2494                 break;
 2495         }
 2496         case DA_STATE_PROBE_RC16:
 2497         {
 2498                 struct scsi_read_capacity_data_long *rcaplong;
 2499 
 2500                 rcaplong = (struct scsi_read_capacity_data_long *)
 2501                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
 2502                 if (rcaplong == NULL) {
 2503                         printf("dastart: Couldn't malloc read_capacity data\n");
 2504                         /* da_free_periph??? */
 2505                         break;
 2506                 }
 2507                 scsi_read_capacity_16(&start_ccb->csio,
 2508                                       /*retries*/ da_retry_count,
 2509                                       /*cbfcnp*/ dadone,
 2510                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
 2511                                       /*lba*/ 0,
 2512                                       /*reladr*/ 0,
 2513                                       /*pmi*/ 0,
 2514                                       /*rcap_buf*/ (uint8_t *)rcaplong,
 2515                                       /*rcap_buf_len*/ sizeof(*rcaplong),
 2516                                       /*sense_len*/ SSD_FULL_SIZE,
 2517                                       /*timeout*/ da_default_timeout * 1000);
 2518                 start_ccb->ccb_h.ccb_bp = NULL;
 2519                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_RC16;
 2520                 xpt_action(start_ccb);
 2521                 break;
 2522         }
 2523         case DA_STATE_PROBE_LBP:
 2524         {
 2525                 struct scsi_vpd_logical_block_prov *lbp;
 2526 
 2527                 if (!scsi_vpd_supported_page(periph, SVPD_LBP)) {
 2528                         /*
 2529                          * If we get here we don't support any SBC-3 delete
 2530                          * methods with UNMAP as the Logical Block Provisioning
 2531                          * VPD page support is required for devices which
 2532                          * support it according to T10/1799-D Revision 31
 2533                          * however older revisions of the spec don't mandate
 2534                          * this so we currently don't remove these methods
 2535                          * from the available set.
 2536                          */
 2537                         softc->state = DA_STATE_PROBE_BLK_LIMITS;
 2538                         goto skipstate;
 2539                 }
 2540 
 2541                 lbp = (struct scsi_vpd_logical_block_prov *)
 2542                         malloc(sizeof(*lbp), M_SCSIDA, M_NOWAIT|M_ZERO);
 2543 
 2544                 if (lbp == NULL) {
 2545                         printf("dastart: Couldn't malloc lbp data\n");
 2546                         /* da_free_periph??? */
 2547                         break;
 2548                 }
 2549 
 2550                 scsi_inquiry(&start_ccb->csio,
 2551                              /*retries*/da_retry_count,
 2552                              /*cbfcnp*/dadone,
 2553                              /*tag_action*/MSG_SIMPLE_Q_TAG,
 2554                              /*inq_buf*/(u_int8_t *)lbp,
 2555                              /*inq_len*/sizeof(*lbp),
 2556                              /*evpd*/TRUE,
 2557                              /*page_code*/SVPD_LBP,
 2558                              /*sense_len*/SSD_MIN_SIZE,
 2559                              /*timeout*/da_default_timeout * 1000);
 2560                 start_ccb->ccb_h.ccb_bp = NULL;
 2561                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_LBP;
 2562                 xpt_action(start_ccb);
 2563                 break;
 2564         }
 2565         case DA_STATE_PROBE_BLK_LIMITS:
 2566         {
 2567                 struct scsi_vpd_block_limits *block_limits;
 2568 
 2569                 if (!scsi_vpd_supported_page(periph, SVPD_BLOCK_LIMITS)) {
 2570                         /* Not supported skip to next probe */
 2571                         softc->state = DA_STATE_PROBE_BDC;
 2572                         goto skipstate;
 2573                 }
 2574 
 2575                 block_limits = (struct scsi_vpd_block_limits *)
 2576                         malloc(sizeof(*block_limits), M_SCSIDA, M_NOWAIT|M_ZERO);
 2577 
 2578                 if (block_limits == NULL) {
 2579                         printf("dastart: Couldn't malloc block_limits data\n");
 2580                         /* da_free_periph??? */
 2581                         break;
 2582                 }
 2583 
 2584                 scsi_inquiry(&start_ccb->csio,
 2585                              /*retries*/da_retry_count,
 2586                              /*cbfcnp*/dadone,
 2587                              /*tag_action*/MSG_SIMPLE_Q_TAG,
 2588                              /*inq_buf*/(u_int8_t *)block_limits,
 2589                              /*inq_len*/sizeof(*block_limits),
 2590                              /*evpd*/TRUE,
 2591                              /*page_code*/SVPD_BLOCK_LIMITS,
 2592                              /*sense_len*/SSD_MIN_SIZE,
 2593                              /*timeout*/da_default_timeout * 1000);
 2594                 start_ccb->ccb_h.ccb_bp = NULL;
 2595                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BLK_LIMITS;
 2596                 xpt_action(start_ccb);
 2597                 break;
 2598         }
 2599         case DA_STATE_PROBE_BDC:
 2600         {
 2601                 struct scsi_vpd_block_characteristics *bdc;
 2602 
 2603                 if (!scsi_vpd_supported_page(periph, SVPD_BDC)) {
 2604                         softc->state = DA_STATE_PROBE_ATA;
 2605                         goto skipstate;
 2606                 }
 2607 
 2608                 bdc = (struct scsi_vpd_block_characteristics *)
 2609                         malloc(sizeof(*bdc), M_SCSIDA, M_NOWAIT|M_ZERO);
 2610 
 2611                 if (bdc == NULL) {
 2612                         printf("dastart: Couldn't malloc bdc data\n");
 2613                         /* da_free_periph??? */
 2614                         break;
 2615                 }
 2616 
 2617                 scsi_inquiry(&start_ccb->csio,
 2618                              /*retries*/da_retry_count,
 2619                              /*cbfcnp*/dadone,
 2620                              /*tag_action*/MSG_SIMPLE_Q_TAG,
 2621                              /*inq_buf*/(u_int8_t *)bdc,
 2622                              /*inq_len*/sizeof(*bdc),
 2623                              /*evpd*/TRUE,
 2624                              /*page_code*/SVPD_BDC,
 2625                              /*sense_len*/SSD_MIN_SIZE,
 2626                              /*timeout*/da_default_timeout * 1000);
 2627                 start_ccb->ccb_h.ccb_bp = NULL;
 2628                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_BDC;
 2629                 xpt_action(start_ccb);
 2630                 break;
 2631         }
 2632         case DA_STATE_PROBE_ATA:
 2633         {
 2634                 struct ata_params *ata_params;
 2635 
 2636                 if (!scsi_vpd_supported_page(periph, SVPD_ATA_INFORMATION)) {
 2637                         daprobedone(periph, start_ccb);
 2638                         break;
 2639                 }
 2640 
 2641                 ata_params = (struct ata_params*)
 2642                         malloc(sizeof(*ata_params), M_SCSIDA, M_NOWAIT|M_ZERO);
 2643 
 2644                 if (ata_params == NULL) {
 2645                         printf("dastart: Couldn't malloc ata_params data\n");
 2646                         /* da_free_periph??? */
 2647                         break;
 2648                 }
 2649 
 2650                 scsi_ata_identify(&start_ccb->csio,
 2651                                   /*retries*/da_retry_count,
 2652                                   /*cbfcnp*/dadone,
 2653                                   /*tag_action*/MSG_SIMPLE_Q_TAG,
 2654                                   /*data_ptr*/(u_int8_t *)ata_params,
 2655                                   /*dxfer_len*/sizeof(*ata_params),
 2656                                   /*sense_len*/SSD_FULL_SIZE,
 2657                                   /*timeout*/da_default_timeout * 1000);
 2658                 start_ccb->ccb_h.ccb_bp = NULL;
 2659                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE_ATA;
 2660                 xpt_action(start_ccb);
 2661                 break;
 2662         }
 2663         }
 2664 }
 2665 
 2666 /*
 2667  * In each of the methods below, while its the caller's
 2668  * responsibility to ensure the request will fit into a
 2669  * single device request, we might have changed the delete
 2670  * method due to the device incorrectly advertising either
 2671  * its supported methods or limits.
 2672  * 
 2673  * To prevent this causing further issues we validate the
 2674  * against the methods limits, and warn which would
 2675  * otherwise be unnecessary.
 2676  */
 2677 static void
 2678 da_delete_unmap(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
 2679 {
 2680         struct da_softc *softc = (struct da_softc *)periph->softc;;
 2681         struct bio *bp1;
 2682         uint8_t *buf = softc->unmap_buf;
 2683         uint64_t lba, lastlba = (uint64_t)-1;
 2684         uint64_t totalcount = 0;
 2685         uint64_t count;
 2686         uint32_t lastcount = 0, c;
 2687         uint32_t off, ranges = 0;
 2688 
 2689         /*
 2690          * Currently this doesn't take the UNMAP
 2691          * Granularity and Granularity Alignment
 2692          * fields into account.
 2693          *
 2694          * This could result in both unoptimal unmap
 2695          * requests as as well as UNMAP calls unmapping
 2696          * fewer LBA's than requested.
 2697          */
 2698 
 2699         softc->delete_running = 1;
 2700         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
 2701         bp1 = bp;
 2702         do {
 2703                 bioq_remove(&softc->delete_queue, bp1);
 2704                 if (bp1 != bp)
 2705                         bioq_insert_tail(&softc->delete_run_queue, bp1);
 2706                 lba = bp1->bio_pblkno;
 2707                 count = bp1->bio_bcount / softc->params.secsize;
 2708 
 2709                 /* Try to extend the previous range. */
 2710                 if (lba == lastlba) {
 2711                         c = omin(count, UNMAP_RANGE_MAX - lastcount);
 2712                         lastcount += c;
 2713                         off = ((ranges - 1) * UNMAP_RANGE_SIZE) +
 2714                               UNMAP_HEAD_SIZE;
 2715                         scsi_ulto4b(lastcount, &buf[off + 8]);
 2716                         count -= c;
 2717                         lba +=c;
 2718                         totalcount += c;
 2719                 }
 2720 
 2721                 while (count > 0) {
 2722                         c = omin(count, UNMAP_RANGE_MAX);
 2723                         if (totalcount + c > softc->unmap_max_lba ||
 2724                             ranges >= softc->unmap_max_ranges) {
 2725                                 xpt_print(periph->path,
 2726                                     "%s issuing short delete %ld > %ld"
 2727                                     "|| %d >= %d",
 2728                                     da_delete_method_desc[softc->delete_method],
 2729                                     totalcount + c, softc->unmap_max_lba,
 2730                                     ranges, softc->unmap_max_ranges);
 2731                                 break;
 2732                         }
 2733                         off = (ranges * UNMAP_RANGE_SIZE) + UNMAP_HEAD_SIZE;
 2734                         scsi_u64to8b(lba, &buf[off + 0]);
 2735                         scsi_ulto4b(c, &buf[off + 8]);
 2736                         lba += c;
 2737                         totalcount += c;
 2738                         ranges++;
 2739                         count -= c;
 2740                         lastcount = c;
 2741                 }
 2742                 lastlba = lba;
 2743                 bp1 = bioq_first(&softc->delete_queue);
 2744                 if (bp1 == NULL || ranges >= softc->unmap_max_ranges ||
 2745                     totalcount + bp1->bio_bcount /
 2746                     softc->params.secsize > softc->unmap_max_lba)
 2747                         break;
 2748         } while (1);
 2749         scsi_ulto2b(ranges * 16 + 6, &buf[0]);
 2750         scsi_ulto2b(ranges * 16, &buf[2]);
 2751 
 2752         scsi_unmap(&ccb->csio,
 2753                    /*retries*/da_retry_count,
 2754                    /*cbfcnp*/dadone,
 2755                    /*tag_action*/MSG_SIMPLE_Q_TAG,
 2756                    /*byte2*/0,
 2757                    /*data_ptr*/ buf,
 2758                    /*dxfer_len*/ ranges * 16 + 8,
 2759                    /*sense_len*/SSD_FULL_SIZE,
 2760                    da_default_timeout * 1000);
 2761         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
 2762         ccb->ccb_h.flags |= CAM_UNLOCKED;
 2763 }
 2764 
 2765 static void
 2766 da_delete_trim(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
 2767 {
 2768         struct da_softc *softc = (struct da_softc *)periph->softc;
 2769         struct bio *bp1;
 2770         uint8_t *buf = softc->unmap_buf;
 2771         uint64_t lastlba = (uint64_t)-1;
 2772         uint64_t count;
 2773         uint64_t lba;
 2774         uint32_t lastcount = 0, c, requestcount;
 2775         int ranges = 0, off, block_count;
 2776 
 2777         softc->delete_running = 1;
 2778         bzero(softc->unmap_buf, sizeof(softc->unmap_buf));
 2779         bp1 = bp;
 2780         do {
 2781                 bioq_remove(&softc->delete_queue, bp1);
 2782                 if (bp1 != bp)
 2783                         bioq_insert_tail(&softc->delete_run_queue, bp1);
 2784                 lba = bp1->bio_pblkno;
 2785                 count = bp1->bio_bcount / softc->params.secsize;
 2786                 requestcount = count;
 2787 
 2788                 /* Try to extend the previous range. */
 2789                 if (lba == lastlba) {
 2790                         c = omin(count, ATA_DSM_RANGE_MAX - lastcount);
 2791                         lastcount += c;
 2792                         off = (ranges - 1) * 8;
 2793                         buf[off + 6] = lastcount & 0xff;
 2794                         buf[off + 7] = (lastcount >> 8) & 0xff;
 2795                         count -= c;
 2796                         lba += c;
 2797                 }
 2798 
 2799                 while (count > 0) {
 2800                         c = omin(count, ATA_DSM_RANGE_MAX);
 2801                         off = ranges * 8;
 2802 
 2803                         buf[off + 0] = lba & 0xff;
 2804                         buf[off + 1] = (lba >> 8) & 0xff;
 2805                         buf[off + 2] = (lba >> 16) & 0xff;
 2806                         buf[off + 3] = (lba >> 24) & 0xff;
 2807                         buf[off + 4] = (lba >> 32) & 0xff;
 2808                         buf[off + 5] = (lba >> 40) & 0xff;
 2809                         buf[off + 6] = c & 0xff;
 2810                         buf[off + 7] = (c >> 8) & 0xff;
 2811                         lba += c;
 2812                         ranges++;
 2813                         count -= c;
 2814                         lastcount = c;
 2815                         if (count != 0 && ranges == softc->trim_max_ranges) {
 2816                                 xpt_print(periph->path,
 2817                                     "%s issuing short delete %ld > %ld\n",
 2818                                     da_delete_method_desc[softc->delete_method],
 2819                                     requestcount,
 2820                                     (softc->trim_max_ranges - ranges) *
 2821                                     ATA_DSM_RANGE_MAX);
 2822                                 break;
 2823                         }
 2824                 }
 2825                 lastlba = lba;
 2826                 bp1 = bioq_first(&softc->delete_queue);
 2827                 if (bp1 == NULL || bp1->bio_bcount / softc->params.secsize >
 2828                     (softc->trim_max_ranges - ranges) * ATA_DSM_RANGE_MAX)
 2829                         break;
 2830         } while (1);
 2831 
 2832         block_count = (ranges + ATA_DSM_BLK_RANGES - 1) / ATA_DSM_BLK_RANGES;
 2833         scsi_ata_trim(&ccb->csio,
 2834                       /*retries*/da_retry_count,
 2835                       /*cbfcnp*/dadone,
 2836                       /*tag_action*/MSG_SIMPLE_Q_TAG,
 2837                       block_count,
 2838                       /*data_ptr*/buf,
 2839                       /*dxfer_len*/block_count * ATA_DSM_BLK_SIZE,
 2840                       /*sense_len*/SSD_FULL_SIZE,
 2841                       da_default_timeout * 1000);
 2842         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
 2843         ccb->ccb_h.flags |= CAM_UNLOCKED;
 2844 }
 2845 
 2846 /*
 2847  * We calculate ws_max_blks here based off d_delmaxsize instead
 2848  * of using softc->ws_max_blks as it is absolute max for the
 2849  * device not the protocol max which may well be lower.
 2850  */
 2851 static void
 2852 da_delete_ws(struct cam_periph *periph, union ccb *ccb, struct bio *bp)
 2853 {
 2854         struct da_softc *softc;
 2855         struct bio *bp1;
 2856         uint64_t ws_max_blks;
 2857         uint64_t lba;
 2858         uint64_t count; /* forward compat with WS32 */
 2859 
 2860         softc = (struct da_softc *)periph->softc;
 2861         ws_max_blks = softc->disk->d_delmaxsize / softc->params.secsize;
 2862         softc->delete_running = 1;
 2863         lba = bp->bio_pblkno;
 2864         count = 0;
 2865         bp1 = bp;
 2866         do {
 2867                 bioq_remove(&softc->delete_queue, bp1);
 2868                 if (bp1 != bp)
 2869                         bioq_insert_tail(&softc->delete_run_queue, bp1);
 2870                 count += bp1->bio_bcount / softc->params.secsize;
 2871                 if (count > ws_max_blks) {
 2872                         xpt_print(periph->path,
 2873                             "%s issuing short delete %ld > %ld\n",
 2874                             da_delete_method_desc[softc->delete_method],
 2875                             count, ws_max_blks);
 2876                         count = omin(count, ws_max_blks);
 2877                         break;
 2878                 }
 2879                 bp1 = bioq_first(&softc->delete_queue);
 2880                 if (bp1 == NULL || lba + count != bp1->bio_pblkno ||
 2881                     count + bp1->bio_bcount /
 2882                     softc->params.secsize > ws_max_blks)
 2883                         break;
 2884         } while (1);
 2885 
 2886         scsi_write_same(&ccb->csio,
 2887                         /*retries*/da_retry_count,
 2888                         /*cbfcnp*/dadone,
 2889                         /*tag_action*/MSG_SIMPLE_Q_TAG,
 2890                         /*byte2*/softc->delete_method ==
 2891                             DA_DELETE_ZERO ? 0 : SWS_UNMAP,
 2892                         softc->delete_method == DA_DELETE_WS16 ? 16 : 10,
 2893                         /*lba*/lba,
 2894                         /*block_count*/count,
 2895                         /*data_ptr*/ __DECONST(void *, zero_region),
 2896                         /*dxfer_len*/ softc->params.secsize,
 2897                         /*sense_len*/SSD_FULL_SIZE,
 2898                         da_default_timeout * 1000);
 2899         ccb->ccb_h.ccb_state = DA_CCB_DELETE;
 2900         ccb->ccb_h.flags |= CAM_UNLOCKED;
 2901 }
 2902 
 2903 static int
 2904 cmd6workaround(union ccb *ccb)
 2905 {
 2906         struct scsi_rw_6 cmd6;
 2907         struct scsi_rw_10 *cmd10;
 2908         struct da_softc *softc;
 2909         u_int8_t *cdb;
 2910         struct bio *bp;
 2911         int frozen;
 2912 
 2913         cdb = ccb->csio.cdb_io.cdb_bytes;
 2914         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
 2915 
 2916         if (ccb->ccb_h.ccb_state == DA_CCB_DELETE) {
 2917                 da_delete_methods old_method = softc->delete_method;
 2918 
 2919                 /*
 2920                  * Typically there are two reasons for failure here
 2921                  * 1. Delete method was detected as supported but isn't
 2922                  * 2. Delete failed due to invalid params e.g. too big
 2923                  *
 2924                  * While we will attempt to choose an alternative delete method
 2925                  * this may result in short deletes if the existing delete
 2926                  * requests from geom are big for the new method chosen.
 2927                  *
 2928                  * This method assumes that the error which triggered this
 2929                  * will not retry the io otherwise a panic will occur
 2930                  */
 2931                 dadeleteflag(softc, old_method, 0);
 2932                 dadeletemethodchoose(softc, DA_DELETE_DISABLE);
 2933                 if (softc->delete_method == DA_DELETE_DISABLE)
 2934                         xpt_print(ccb->ccb_h.path,
 2935                                   "%s failed, disabling BIO_DELETE\n",
 2936                                   da_delete_method_desc[old_method]);
 2937                 else
 2938                         xpt_print(ccb->ccb_h.path,
 2939                                   "%s failed, switching to %s BIO_DELETE\n",
 2940                                   da_delete_method_desc[old_method],
 2941                                   da_delete_method_desc[softc->delete_method]);
 2942 
 2943                 while ((bp = bioq_takefirst(&softc->delete_run_queue)) != NULL)
 2944                         bioq_disksort(&softc->delete_queue, bp);
 2945                 bioq_disksort(&softc->delete_queue,
 2946                     (struct bio *)ccb->ccb_h.ccb_bp);
 2947                 ccb->ccb_h.ccb_bp = NULL;
 2948                 return (0);
 2949         }
 2950 
 2951         /* Detect unsupported PREVENT ALLOW MEDIUM REMOVAL. */
 2952         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
 2953             (*cdb == PREVENT_ALLOW) &&
 2954             (softc->quirks & DA_Q_NO_PREVENT) == 0) {
 2955                 if (bootverbose)
 2956                         xpt_print(ccb->ccb_h.path,
 2957                             "PREVENT ALLOW MEDIUM REMOVAL not supported.\n");
 2958                 softc->quirks |= DA_Q_NO_PREVENT;
 2959                 return (0);
 2960         }
 2961 
 2962         /* Detect unsupported SYNCHRONIZE CACHE(10). */
 2963         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) == 0 &&
 2964             (*cdb == SYNCHRONIZE_CACHE) &&
 2965             (softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
 2966                 if (bootverbose)
 2967                         xpt_print(ccb->ccb_h.path,
 2968                             "SYNCHRONIZE CACHE(10) not supported.\n");
 2969                 softc->quirks |= DA_Q_NO_SYNC_CACHE;
 2970                 softc->disk->d_flags &= ~DISKFLAG_CANFLUSHCACHE;
 2971                 return (0);
 2972         }
 2973 
 2974         /* Translation only possible if CDB is an array and cmd is R/W6 */
 2975         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
 2976             (*cdb != READ_6 && *cdb != WRITE_6))
 2977                 return 0;
 2978 
 2979         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
 2980             "increasing minimum_cmd_size to 10.\n");
 2981         softc->minimum_cmd_size = 10;
 2982 
 2983         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
 2984         cmd10 = (struct scsi_rw_10 *)cdb;
 2985         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
 2986         cmd10->byte2 = 0;
 2987         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
 2988         cmd10->reserved = 0;
 2989         scsi_ulto2b(cmd6.length, cmd10->length);
 2990         cmd10->control = cmd6.control;
 2991         ccb->csio.cdb_len = sizeof(*cmd10);
 2992 
 2993         /* Requeue request, unfreezing queue if necessary */
 2994         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
 2995         ccb->ccb_h.status = CAM_REQUEUE_REQ;
 2996         xpt_action(ccb);
 2997         if (frozen) {
 2998                 cam_release_devq(ccb->ccb_h.path,
 2999                                  /*relsim_flags*/0,
 3000                                  /*reduction*/0,
 3001                                  /*timeout*/0,
 3002                                  /*getcount_only*/0);
 3003         }
 3004         return (ERESTART);
 3005 }
 3006 
 3007 static void
 3008 dadone(struct cam_periph *periph, union ccb *done_ccb)
 3009 {
 3010         struct da_softc *softc;
 3011         struct ccb_scsiio *csio;
 3012         u_int32_t  priority;
 3013         da_ccb_state state;
 3014 
 3015         softc = (struct da_softc *)periph->softc;
 3016         priority = done_ccb->ccb_h.pinfo.priority;
 3017 
 3018         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("dadone\n"));
 3019 
 3020         csio = &done_ccb->csio;
 3021         state = csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK;
 3022         switch (state) {
 3023         case DA_CCB_BUFFER_IO:
 3024         case DA_CCB_DELETE:
 3025         {
 3026                 struct bio *bp, *bp1;
 3027 
 3028                 cam_periph_lock(periph);
 3029                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
 3030                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 3031                         int error;
 3032                         int sf;
 3033 
 3034                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
 3035                                 sf = SF_RETRY_UA;
 3036                         else
 3037                                 sf = 0;
 3038 
 3039                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
 3040                         if (error == ERESTART) {
 3041                                 /*
 3042                                  * A retry was scheduled, so
 3043                                  * just return.
 3044                                  */
 3045                                 cam_periph_unlock(periph);
 3046                                 return;
 3047                         }
 3048                         bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
 3049                         if (error != 0) {
 3050                                 int queued_error;
 3051 
 3052                                 /*
 3053                                  * return all queued I/O with EIO, so that
 3054                                  * the client can retry these I/Os in the
 3055                                  * proper order should it attempt to recover.
 3056                                  */
 3057                                 queued_error = EIO;
 3058 
 3059                                 if (error == ENXIO
 3060                                  && (softc->flags & DA_FLAG_PACK_INVALID)== 0) {
 3061                                         /*
 3062                                          * Catastrophic error.  Mark our pack as
 3063                                          * invalid.
 3064                                          */
 3065                                         /*
 3066                                          * XXX See if this is really a media
 3067                                          * XXX change first?
 3068                                          */
 3069                                         xpt_print(periph->path,
 3070                                             "Invalidating pack\n");
 3071                                         softc->flags |= DA_FLAG_PACK_INVALID;
 3072                                         queued_error = ENXIO;
 3073                                 }
 3074                                 bioq_flush(&softc->bio_queue, NULL,
 3075                                            queued_error);
 3076                                 if (bp != NULL) {
 3077                                         bp->bio_error = error;
 3078                                         bp->bio_resid = bp->bio_bcount;
 3079                                         bp->bio_flags |= BIO_ERROR;
 3080                                 }
 3081                         } else if (bp != NULL) {
 3082                                 if (state == DA_CCB_DELETE)
 3083                                         bp->bio_resid = 0;
 3084                                 else
 3085                                         bp->bio_resid = csio->resid;
 3086                                 bp->bio_error = 0;
 3087                                 if (bp->bio_resid != 0)
 3088                                         bp->bio_flags |= BIO_ERROR;
 3089                         }
 3090                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 3091                                 cam_release_devq(done_ccb->ccb_h.path,
 3092                                                  /*relsim_flags*/0,
 3093                                                  /*reduction*/0,
 3094                                                  /*timeout*/0,
 3095                                                  /*getcount_only*/0);
 3096                 } else if (bp != NULL) {
 3097                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 3098                                 panic("REQ_CMP with QFRZN");
 3099                         if (state == DA_CCB_DELETE)
 3100                                 bp->bio_resid = 0;
 3101                         else
 3102                                 bp->bio_resid = csio->resid;
 3103                         if (csio->resid > 0)
 3104                                 bp->bio_flags |= BIO_ERROR;
 3105                         if (softc->error_inject != 0) {
 3106                                 bp->bio_error = softc->error_inject;
 3107                                 bp->bio_resid = bp->bio_bcount;
 3108                                 bp->bio_flags |= BIO_ERROR;
 3109                                 softc->error_inject = 0;
 3110                         }
 3111                 }
 3112 
 3113                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
 3114                 if (LIST_EMPTY(&softc->pending_ccbs))
 3115                         softc->flags |= DA_FLAG_WAS_OTAG;
 3116 
 3117                 xpt_release_ccb(done_ccb);
 3118                 if (state == DA_CCB_DELETE) {
 3119                         TAILQ_HEAD(, bio) queue;
 3120 
 3121                         TAILQ_INIT(&queue);
 3122                         TAILQ_CONCAT(&queue, &softc->delete_run_queue.queue, bio_queue);
 3123                         softc->delete_run_queue.insert_point = NULL;
 3124                         /*
 3125                          * Normally, the xpt_release_ccb() above would make sure
 3126                          * that when we have more work to do, that work would
 3127                          * get kicked off. However, we specifically keep
 3128                          * delete_running set to 0 before the call above to
 3129                          * allow other I/O to progress when many BIO_DELETE
 3130                          * requests are pushed down. We set delete_running to 0
 3131                          * and call daschedule again so that we don't stall if
 3132                          * there are no other I/Os pending apart from BIO_DELETEs.
 3133                          */
 3134                         softc->delete_running = 0;
 3135                         daschedule(periph);
 3136                         cam_periph_unlock(periph);
 3137                         while ((bp1 = TAILQ_FIRST(&queue)) != NULL) {
 3138                                 TAILQ_REMOVE(&queue, bp1, bio_queue);
 3139                                 bp1->bio_error = bp->bio_error;
 3140                                 if (bp->bio_flags & BIO_ERROR) {
 3141                                         bp1->bio_flags |= BIO_ERROR;
 3142                                         bp1->bio_resid = bp1->bio_bcount;
 3143                                 } else
 3144                                         bp1->bio_resid = 0;
 3145                                 biodone(bp1);
 3146                         }
 3147                 } else
 3148                         cam_periph_unlock(periph);
 3149                 if (bp != NULL)
 3150                         biodone(bp);
 3151                 return;
 3152         }
 3153         case DA_CCB_PROBE_WP:
 3154         {
 3155                 struct scsi_mode_header_6 *mode_hdr6;
 3156                 struct scsi_mode_header_10 *mode_hdr10;
 3157                 uint8_t dev_spec;
 3158 
 3159                 if (softc->minimum_cmd_size > 6) {
 3160                         mode_hdr10 = (struct scsi_mode_header_10 *)csio->data_ptr;
 3161                         dev_spec = mode_hdr10->dev_spec;
 3162                 } else {
 3163                         mode_hdr6 = (struct scsi_mode_header_6 *)csio->data_ptr;
 3164                         dev_spec = mode_hdr6->dev_spec;
 3165                 }
 3166                 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
 3167                         if ((dev_spec & 0x80) != 0)
 3168                                 softc->disk->d_flags |= DISKFLAG_WRITE_PROTECT;
 3169                         else
 3170                                 softc->disk->d_flags &= ~DISKFLAG_WRITE_PROTECT;
 3171                 } else {
 3172                         int error;
 3173 
 3174                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 3175                                         SF_RETRY_UA|SF_NO_PRINT);
 3176                         if (error == ERESTART)
 3177                                 return;
 3178                         else if (error != 0) {
 3179                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
 3180                                         /* Don't wedge this device's queue */
 3181                                         cam_release_devq(done_ccb->ccb_h.path,
 3182                                                          /*relsim_flags*/0,
 3183                                                          /*reduction*/0,
 3184                                                          /*timeout*/0,
 3185                                                          /*getcount_only*/0);
 3186                                 }
 3187                         }
 3188                 }
 3189 
 3190                 free(csio->data_ptr, M_SCSIDA);
 3191                 xpt_release_ccb(done_ccb);
 3192                 if ((softc->flags & DA_FLAG_CAN_RC16) != 0)
 3193                         softc->state = DA_STATE_PROBE_RC16;
 3194                 else
 3195                         softc->state = DA_STATE_PROBE_RC;
 3196                 xpt_schedule(periph, priority);
 3197                 return;
 3198         }
 3199         case DA_CCB_PROBE_RC:
 3200         case DA_CCB_PROBE_RC16:
 3201         {
 3202                 struct     scsi_read_capacity_data *rdcap;
 3203                 struct     scsi_read_capacity_data_long *rcaplong;
 3204                 char       announce_buf[80];
 3205                 int        lbp;
 3206 
 3207                 lbp = 0;
 3208                 rdcap = NULL;
 3209                 rcaplong = NULL;
 3210                 if (state == DA_CCB_PROBE_RC)
 3211                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
 3212                 else
 3213                         rcaplong = (struct scsi_read_capacity_data_long *)
 3214                                 csio->data_ptr;
 3215 
 3216                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 3217                         struct disk_params *dp;
 3218                         uint32_t block_size;
 3219                         uint64_t maxsector;
 3220                         u_int lbppbe;   /* LB per physical block exponent. */
 3221                         u_int lalba;    /* Lowest aligned LBA. */
 3222 
 3223                         if (state == DA_CCB_PROBE_RC) {
 3224                                 block_size = scsi_4btoul(rdcap->length);
 3225                                 maxsector = scsi_4btoul(rdcap->addr);
 3226                                 lbppbe = 0;
 3227                                 lalba = 0;
 3228 
 3229                                 /*
 3230                                  * According to SBC-2, if the standard 10
 3231                                  * byte READ CAPACITY command returns 2^32,
 3232                                  * we should issue the 16 byte version of
 3233                                  * the command, since the device in question
 3234                                  * has more sectors than can be represented
 3235                                  * with the short version of the command.
 3236                                  */
 3237                                 if (maxsector == 0xffffffff) {
 3238                                         free(rdcap, M_SCSIDA);
 3239                                         xpt_release_ccb(done_ccb);
 3240                                         softc->state = DA_STATE_PROBE_RC16;
 3241                                         xpt_schedule(periph, priority);
 3242                                         return;
 3243                                 }
 3244                         } else {
 3245                                 block_size = scsi_4btoul(rcaplong->length);
 3246                                 maxsector = scsi_8btou64(rcaplong->addr);
 3247                                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
 3248                                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
 3249                         }
 3250 
 3251                         /*
 3252                          * Because GEOM code just will panic us if we
 3253                          * give them an 'illegal' value we'll avoid that
 3254                          * here.
 3255                          */
 3256                         if (block_size == 0) {
 3257                                 block_size = 512;
 3258                                 if (maxsector == 0)
 3259                                         maxsector = -1;
 3260                         }
 3261                         if (block_size >= MAXPHYS) {
 3262                                 xpt_print(periph->path,
 3263                                     "unsupportable block size %ju\n",
 3264                                     (uintmax_t) block_size);
 3265                                 announce_buf[0] = '\0';
 3266                                 cam_periph_invalidate(periph);
 3267                         } else {
 3268                                 /*
 3269                                  * We pass rcaplong into dasetgeom(),
 3270                                  * because it will only use it if it is
 3271                                  * non-NULL.
 3272                                  */
 3273                                 dasetgeom(periph, block_size, maxsector,
 3274                                           rcaplong, sizeof(*rcaplong));
 3275                                 lbp = (lalba & SRC16_LBPME_A);
 3276                                 dp = &softc->params;
 3277                                 snprintf(announce_buf, sizeof(announce_buf),
 3278                                     "%juMB (%ju %u byte sectors)",
 3279                                     ((uintmax_t)dp->secsize * dp->sectors) /
 3280                                      (1024 * 1024),
 3281                                     (uintmax_t)dp->sectors, dp->secsize);
 3282                         }
 3283                 } else {
 3284                         int     error;
 3285 
 3286                         announce_buf[0] = '\0';
 3287 
 3288                         /*
 3289                          * Retry any UNIT ATTENTION type errors.  They
 3290                          * are expected at boot.
 3291                          */
 3292                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 3293                                         SF_RETRY_UA|SF_NO_PRINT);
 3294                         if (error == ERESTART) {
 3295                                 /*
 3296                                  * A retry was scheuled, so
 3297                                  * just return.
 3298                                  */
 3299                                 return;
 3300                         } else if (error != 0) {
 3301                                 int asc, ascq;
 3302                                 int sense_key, error_code;
 3303                                 int have_sense;
 3304                                 cam_status status;
 3305                                 struct ccb_getdev cgd;
 3306 
 3307                                 /* Don't wedge this device's queue */
 3308                                 status = done_ccb->ccb_h.status;
 3309                                 if ((status & CAM_DEV_QFRZN) != 0)
 3310                                         cam_release_devq(done_ccb->ccb_h.path,
 3311                                                          /*relsim_flags*/0,
 3312                                                          /*reduction*/0,
 3313                                                          /*timeout*/0,
 3314                                                          /*getcount_only*/0);
 3315 
 3316 
 3317                                 xpt_setup_ccb(&cgd.ccb_h, 
 3318                                               done_ccb->ccb_h.path,
 3319                                               CAM_PRIORITY_NORMAL);
 3320                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
 3321                                 xpt_action((union ccb *)&cgd);
 3322 
 3323                                 if (scsi_extract_sense_ccb(done_ccb,
 3324                                     &error_code, &sense_key, &asc, &ascq))
 3325                                         have_sense = TRUE;
 3326                                 else
 3327                                         have_sense = FALSE;
 3328 
 3329                                 /*
 3330                                  * If we tried READ CAPACITY(16) and failed,
 3331                                  * fallback to READ CAPACITY(10).
 3332                                  */
 3333                                 if ((state == DA_CCB_PROBE_RC16) &&
 3334                                     (softc->flags & DA_FLAG_CAN_RC16) &&
 3335                                     (((csio->ccb_h.status & CAM_STATUS_MASK) ==
 3336                                         CAM_REQ_INVALID) ||
 3337                                      ((have_sense) &&
 3338                                       (error_code == SSD_CURRENT_ERROR) &&
 3339                                       (sense_key == SSD_KEY_ILLEGAL_REQUEST)))) {
 3340                                         softc->flags &= ~DA_FLAG_CAN_RC16;
 3341                                         free(rdcap, M_SCSIDA);
 3342                                         xpt_release_ccb(done_ccb);
 3343                                         softc->state = DA_STATE_PROBE_RC;
 3344                                         xpt_schedule(periph, priority);
 3345                                         return;
 3346                                 } else
 3347                                 /*
 3348                                  * Attach to anything that claims to be a
 3349                                  * direct access or optical disk device,
 3350                                  * as long as it doesn't return a "Logical
 3351                                  * unit not supported" (0x25) error.
 3352                                  */
 3353                                 if ((have_sense) && (asc != 0x25)
 3354                                  && (error_code == SSD_CURRENT_ERROR)) {
 3355                                         const char *sense_key_desc;
 3356                                         const char *asc_desc;
 3357 
 3358                                         dasetgeom(periph, 512, -1, NULL, 0);
 3359                                         scsi_sense_desc(sense_key, asc, ascq,
 3360                                                         &cgd.inq_data,
 3361                                                         &sense_key_desc,
 3362                                                         &asc_desc);
 3363                                         snprintf(announce_buf,
 3364                                             sizeof(announce_buf),
 3365                                                 "Attempt to query device "
 3366                                                 "size failed: %s, %s",
 3367                                                 sense_key_desc,
 3368                                                 asc_desc);
 3369                                 } else { 
 3370                                         if (have_sense)
 3371                                                 scsi_sense_print(
 3372                                                         &done_ccb->csio);
 3373                                         else {
 3374                                                 xpt_print(periph->path,
 3375                                                     "got CAM status %#x\n",
 3376                                                     done_ccb->ccb_h.status);
 3377                                         }
 3378 
 3379                                         xpt_print(periph->path, "fatal error, "
 3380                                             "failed to attach to device\n");
 3381 
 3382                                         /*
 3383                                          * Free up resources.
 3384                                          */
 3385                                         cam_periph_invalidate(periph);
 3386                                 } 
 3387                         }
 3388                 }
 3389                 free(csio->data_ptr, M_SCSIDA);
 3390                 if (announce_buf[0] != '\0' &&
 3391                     ((softc->flags & DA_FLAG_ANNOUNCED) == 0)) {
 3392                         /*
 3393                          * Create our sysctl variables, now that we know
 3394                          * we have successfully attached.
 3395                          */
 3396                         /* increase the refcount */
 3397                         if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
 3398                                 taskqueue_enqueue(taskqueue_thread,
 3399                                                   &softc->sysctl_task);
 3400                                 xpt_announce_periph(periph, announce_buf);
 3401                                 xpt_announce_quirks(periph, softc->quirks,
 3402                                     DA_Q_BIT_STRING);
 3403                         } else {
 3404                                 xpt_print(periph->path, "fatal error, "
 3405                                     "could not acquire reference count\n");
 3406                         }
 3407                 }
 3408 
 3409                 /* We already probed the device. */
 3410                 if (softc->flags & DA_FLAG_PROBED) {
 3411                         daprobedone(periph, done_ccb);
 3412                         return;
 3413                 }
 3414 
 3415                 /* Ensure re-probe doesn't see old delete. */
 3416                 softc->delete_available = 0;
 3417                 dadeleteflag(softc, DA_DELETE_ZERO, 1);
 3418                 if (lbp && (softc->quirks & DA_Q_NO_UNMAP) == 0) {
 3419                         /*
 3420                          * Based on older SBC-3 spec revisions
 3421                          * any of the UNMAP methods "may" be
 3422                          * available via LBP given this flag so
 3423                          * we flag all of them as available and
 3424                          * then remove those which further
 3425                          * probes confirm aren't available
 3426                          * later.
 3427                          *
 3428                          * We could also check readcap(16) p_type
 3429                          * flag to exclude one or more invalid
 3430                          * write same (X) types here
 3431                          */
 3432                         dadeleteflag(softc, DA_DELETE_WS16, 1);
 3433                         dadeleteflag(softc, DA_DELETE_WS10, 1);
 3434                         dadeleteflag(softc, DA_DELETE_UNMAP, 1);
 3435 
 3436                         xpt_release_ccb(done_ccb);
 3437                         softc->state = DA_STATE_PROBE_LBP;
 3438                         xpt_schedule(periph, priority);
 3439                         return;
 3440                 }
 3441 
 3442                 xpt_release_ccb(done_ccb);
 3443                 softc->state = DA_STATE_PROBE_BDC;
 3444                 xpt_schedule(periph, priority);
 3445                 return;
 3446         }
 3447         case DA_CCB_PROBE_LBP:
 3448         {
 3449                 struct scsi_vpd_logical_block_prov *lbp;
 3450 
 3451                 lbp = (struct scsi_vpd_logical_block_prov *)csio->data_ptr;
 3452 
 3453                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 3454                         /*
 3455                          * T10/1799-D Revision 31 states at least one of these
 3456                          * must be supported but we don't currently enforce this.
 3457                          */
 3458                         dadeleteflag(softc, DA_DELETE_WS16,
 3459                                      (lbp->flags & SVPD_LBP_WS16));
 3460                         dadeleteflag(softc, DA_DELETE_WS10,
 3461                                      (lbp->flags & SVPD_LBP_WS10));
 3462                         dadeleteflag(softc, DA_DELETE_UNMAP,
 3463                                      (lbp->flags & SVPD_LBP_UNMAP));
 3464                 } else {
 3465                         int error;
 3466                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 3467                                         SF_RETRY_UA|SF_NO_PRINT);
 3468                         if (error == ERESTART)
 3469                                 return;
 3470                         else if (error != 0) {
 3471                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
 3472                                         /* Don't wedge this device's queue */
 3473                                         cam_release_devq(done_ccb->ccb_h.path,
 3474                                                          /*relsim_flags*/0,
 3475                                                          /*reduction*/0,
 3476                                                          /*timeout*/0,
 3477                                                          /*getcount_only*/0);
 3478                                 }
 3479 
 3480                                 /*
 3481                                  * Failure indicates we don't support any SBC-3
 3482                                  * delete methods with UNMAP
 3483                                  */
 3484                         }
 3485                 }
 3486 
 3487                 free(lbp, M_SCSIDA);
 3488                 xpt_release_ccb(done_ccb);
 3489                 softc->state = DA_STATE_PROBE_BLK_LIMITS;
 3490                 xpt_schedule(periph, priority);
 3491                 return;
 3492         }
 3493         case DA_CCB_PROBE_BLK_LIMITS:
 3494         {
 3495                 struct scsi_vpd_block_limits *block_limits;
 3496 
 3497                 block_limits = (struct scsi_vpd_block_limits *)csio->data_ptr;
 3498 
 3499                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 3500                         uint32_t max_txfer_len = scsi_4btoul(
 3501                                 block_limits->max_txfer_len);
 3502                         uint32_t max_unmap_lba_cnt = scsi_4btoul(
 3503                                 block_limits->max_unmap_lba_cnt);
 3504                         uint32_t max_unmap_blk_cnt = scsi_4btoul(
 3505                                 block_limits->max_unmap_blk_cnt);
 3506                         uint64_t ws_max_blks = scsi_8btou64(
 3507                                 block_limits->max_write_same_length);
 3508 
 3509                         if (max_txfer_len != 0) {
 3510                                 softc->disk->d_maxsize = MIN(softc->maxio,
 3511                                     (off_t)max_txfer_len * softc->params.secsize);
 3512                         }
 3513 
 3514                         /*
 3515                          * We should already support UNMAP but we check lba
 3516                          * and block count to be sure
 3517                          */
 3518                         if (max_unmap_lba_cnt != 0x00L &&
 3519                             max_unmap_blk_cnt != 0x00L) {
 3520                                 softc->unmap_max_lba = max_unmap_lba_cnt;
 3521                                 softc->unmap_max_ranges = min(max_unmap_blk_cnt,
 3522                                         UNMAP_MAX_RANGES);
 3523                         } else {
 3524                                 /*
 3525                                  * Unexpected UNMAP limits which means the
 3526                                  * device doesn't actually support UNMAP
 3527                                  */
 3528                                 dadeleteflag(softc, DA_DELETE_UNMAP, 0);
 3529                         }
 3530 
 3531                         if (ws_max_blks != 0x00L)
 3532                                 softc->ws_max_blks = ws_max_blks;
 3533                 } else {
 3534                         int error;
 3535                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 3536                                         SF_RETRY_UA|SF_NO_PRINT);
 3537                         if (error == ERESTART)
 3538                                 return;
 3539                         else if (error != 0) {
 3540                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
 3541                                         /* Don't wedge this device's queue */
 3542                                         cam_release_devq(done_ccb->ccb_h.path,
 3543                                                          /*relsim_flags*/0,
 3544                                                          /*reduction*/0,
 3545                                                          /*timeout*/0,
 3546                                                          /*getcount_only*/0);
 3547                                 }
 3548 
 3549                                 /*
 3550                                  * Failure here doesn't mean UNMAP is not
 3551                                  * supported as this is an optional page.
 3552                                  */
 3553                                 softc->unmap_max_lba = 1;
 3554                                 softc->unmap_max_ranges = 1;
 3555                         }
 3556                 }
 3557 
 3558                 free(block_limits, M_SCSIDA);
 3559                 xpt_release_ccb(done_ccb);
 3560                 softc->state = DA_STATE_PROBE_BDC;
 3561                 xpt_schedule(periph, priority);
 3562                 return;
 3563         }
 3564         case DA_CCB_PROBE_BDC:
 3565         {
 3566                 struct scsi_vpd_block_characteristics *bdc;
 3567 
 3568                 bdc = (struct scsi_vpd_block_characteristics *)csio->data_ptr;
 3569 
 3570                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 3571                         /*
 3572                          * Disable queue sorting for non-rotational media
 3573                          * by default.
 3574                          */
 3575                         u_int16_t old_rate = softc->disk->d_rotation_rate;
 3576 
 3577                         softc->disk->d_rotation_rate =
 3578                                 scsi_2btoul(bdc->medium_rotation_rate);
 3579                         if (softc->disk->d_rotation_rate ==
 3580                             SVPD_BDC_RATE_NON_ROTATING) {
 3581                                 softc->sort_io_queue = 0;
 3582                         }
 3583                         if (softc->disk->d_rotation_rate != old_rate) {
 3584                                 disk_attr_changed(softc->disk,
 3585                                     "GEOM::rotation_rate", M_NOWAIT);
 3586                         }
 3587                 } else {
 3588                         int error;
 3589                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 3590                                         SF_RETRY_UA|SF_NO_PRINT);
 3591                         if (error == ERESTART)
 3592                                 return;
 3593                         else if (error != 0) {
 3594                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
 3595                                         /* Don't wedge this device's queue */
 3596                                         cam_release_devq(done_ccb->ccb_h.path,
 3597                                                          /*relsim_flags*/0,
 3598                                                          /*reduction*/0,
 3599                                                          /*timeout*/0,
 3600                                                          /*getcount_only*/0);
 3601                                 }
 3602                         }
 3603                 }
 3604 
 3605                 free(bdc, M_SCSIDA);
 3606                 xpt_release_ccb(done_ccb);
 3607                 softc->state = DA_STATE_PROBE_ATA;
 3608                 xpt_schedule(periph, priority);
 3609                 return;
 3610         }
 3611         case DA_CCB_PROBE_ATA:
 3612         {
 3613                 int i;
 3614                 struct ata_params *ata_params;
 3615                 int16_t *ptr;
 3616 
 3617                 ata_params = (struct ata_params *)csio->data_ptr;
 3618                 ptr = (uint16_t *)ata_params;
 3619 
 3620                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 3621                         uint16_t old_rate;
 3622 
 3623                         for (i = 0; i < sizeof(*ata_params) / 2; i++)
 3624                                 ptr[i] = le16toh(ptr[i]);
 3625                         if (ata_params->support_dsm & ATA_SUPPORT_DSM_TRIM &&
 3626                             (softc->quirks & DA_Q_NO_UNMAP) == 0) {
 3627                                 dadeleteflag(softc, DA_DELETE_ATA_TRIM, 1);
 3628                                 if (ata_params->max_dsm_blocks != 0)
 3629                                         softc->trim_max_ranges = min(
 3630                                           softc->trim_max_ranges,
 3631                                           ata_params->max_dsm_blocks *
 3632                                           ATA_DSM_BLK_RANGES);
 3633                         }
 3634                         /*
 3635                          * Disable queue sorting for non-rotational media
 3636                          * by default.
 3637                          */
 3638                         old_rate = softc->disk->d_rotation_rate;
 3639                         softc->disk->d_rotation_rate =
 3640                             ata_params->media_rotation_rate;
 3641                         if (softc->disk->d_rotation_rate ==
 3642                             ATA_RATE_NON_ROTATING) {
 3643                                 softc->sort_io_queue = 0;
 3644                         }
 3645 
 3646                         if (softc->disk->d_rotation_rate != old_rate) {
 3647                                 disk_attr_changed(softc->disk,
 3648                                     "GEOM::rotation_rate", M_NOWAIT);
 3649                         }
 3650                 } else {
 3651                         int error;
 3652                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 3653                                         SF_RETRY_UA|SF_NO_PRINT);
 3654                         if (error == ERESTART)
 3655                                 return;
 3656                         else if (error != 0) {
 3657                                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
 3658                                         /* Don't wedge this device's queue */
 3659                                         cam_release_devq(done_ccb->ccb_h.path,
 3660                                                          /*relsim_flags*/0,
 3661                                                          /*reduction*/0,
 3662                                                          /*timeout*/0,
 3663                                                          /*getcount_only*/0);
 3664                                 }
 3665                         }
 3666                 }
 3667 
 3668                 free(ata_params, M_SCSIDA);
 3669                 daprobedone(periph, done_ccb);
 3670                 return;
 3671         }
 3672         case DA_CCB_DUMP:
 3673                 /* No-op.  We're polling */
 3674                 return;
 3675         case DA_CCB_TUR:
 3676         {
 3677                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 3678 
 3679                         if (daerror(done_ccb, CAM_RETRY_SELTO,
 3680                             SF_RETRY_UA | SF_NO_RECOVERY | SF_NO_PRINT) ==
 3681                             ERESTART)
 3682                                 return;
 3683                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 3684                                 cam_release_devq(done_ccb->ccb_h.path,
 3685                                                  /*relsim_flags*/0,
 3686                                                  /*reduction*/0,
 3687                                                  /*timeout*/0,
 3688                                                  /*getcount_only*/0);
 3689                 }
 3690                 xpt_release_ccb(done_ccb);
 3691                 cam_periph_release_locked(periph);
 3692                 return;
 3693         }
 3694         default:
 3695                 break;
 3696         }
 3697         xpt_release_ccb(done_ccb);
 3698 }
 3699 
 3700 static void
 3701 dareprobe(struct cam_periph *periph)
 3702 {
 3703         struct da_softc   *softc;
 3704         cam_status status;
 3705 
 3706         softc = (struct da_softc *)periph->softc;
 3707 
 3708         /* Probe in progress; don't interfere. */
 3709         if (softc->state != DA_STATE_NORMAL)
 3710                 return;
 3711 
 3712         status = cam_periph_acquire(periph);
 3713         KASSERT(status == CAM_REQ_CMP,
 3714             ("dareprobe: cam_periph_acquire failed"));
 3715 
 3716         softc->state = DA_STATE_PROBE_WP;
 3717         xpt_schedule(periph, CAM_PRIORITY_DEV);
 3718 }
 3719 
 3720 static int
 3721 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
 3722 {
 3723         struct da_softc   *softc;
 3724         struct cam_periph *periph;
 3725         int error, error_code, sense_key, asc, ascq;
 3726 
 3727         periph = xpt_path_periph(ccb->ccb_h.path);
 3728         softc = (struct da_softc *)periph->softc;
 3729 
 3730         /*
 3731          * Automatically detect devices that do not support
 3732          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
 3733          */
 3734         error = 0;
 3735         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
 3736                 error = cmd6workaround(ccb);
 3737         } else if (scsi_extract_sense_ccb(ccb,
 3738             &error_code, &sense_key, &asc, &ascq)) {
 3739                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
 3740                         error = cmd6workaround(ccb);
 3741                 /*
 3742                  * If the target replied with CAPACITY DATA HAS CHANGED UA,
 3743                  * query the capacity and notify upper layers.
 3744                  */
 3745                 else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
 3746                     asc == 0x2A && ascq == 0x09) {
 3747                         xpt_print(periph->path, "Capacity data has changed\n");
 3748                         softc->flags &= ~DA_FLAG_PROBED;
 3749                         dareprobe(periph);
 3750                         sense_flags |= SF_NO_PRINT;
 3751                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
 3752                     asc == 0x28 && ascq == 0x00) {
 3753                         softc->flags &= ~DA_FLAG_PROBED;
 3754                         disk_media_changed(softc->disk, M_NOWAIT);
 3755                 } else if (sense_key == SSD_KEY_UNIT_ATTENTION &&
 3756                     asc == 0x3F && ascq == 0x03) {
 3757                         xpt_print(periph->path, "INQUIRY data has changed\n");
 3758                         softc->flags &= ~DA_FLAG_PROBED;
 3759                         dareprobe(periph);
 3760                         sense_flags |= SF_NO_PRINT;
 3761                 } else if (sense_key == SSD_KEY_NOT_READY &&
 3762                     asc == 0x3a && (softc->flags & DA_FLAG_PACK_INVALID) == 0) {
 3763                         softc->flags |= DA_FLAG_PACK_INVALID;
 3764                         disk_media_gone(softc->disk, M_NOWAIT);
 3765                 }
 3766         }
 3767         if (error == ERESTART)
 3768                 return (ERESTART);
 3769 
 3770         /*
 3771          * XXX
 3772          * Until we have a better way of doing pack validation,
 3773          * don't treat UAs as errors.
 3774          */
 3775         sense_flags |= SF_RETRY_UA;
 3776 
 3777         if (softc->quirks & DA_Q_RETRY_BUSY)
 3778                 sense_flags |= SF_RETRY_BUSY;
 3779         return(cam_periph_error(ccb, cam_flags, sense_flags,
 3780                                 &softc->saved_ccb));
 3781 }
 3782 
 3783 static void
 3784 damediapoll(void *arg)
 3785 {
 3786         struct cam_periph *periph = arg;
 3787         struct da_softc *softc = periph->softc;
 3788 
 3789         if (!softc->tur && LIST_EMPTY(&softc->pending_ccbs)) {
 3790                 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
 3791                         softc->tur = 1;
 3792                         daschedule(periph);
 3793                 }
 3794         }
 3795         /* Queue us up again */
 3796         if (da_poll_period != 0)
 3797                 callout_schedule(&softc->mediapoll_c, da_poll_period * hz);
 3798 }
 3799 
 3800 static void
 3801 daprevent(struct cam_periph *periph, int action)
 3802 {
 3803         struct  da_softc *softc;
 3804         union   ccb *ccb;               
 3805         int     error;
 3806                 
 3807         softc = (struct da_softc *)periph->softc;
 3808 
 3809         if (((action == PR_ALLOW)
 3810           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
 3811          || ((action == PR_PREVENT)
 3812           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
 3813                 return;
 3814         }
 3815 
 3816         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
 3817 
 3818         scsi_prevent(&ccb->csio,
 3819                      /*retries*/1,
 3820                      /*cbcfp*/dadone,
 3821                      MSG_SIMPLE_Q_TAG,
 3822                      action,
 3823                      SSD_FULL_SIZE,
 3824                      5000);
 3825 
 3826         error = cam_periph_runccb(ccb, daerror, CAM_RETRY_SELTO,
 3827             SF_RETRY_UA | SF_NO_PRINT, softc->disk->d_devstat);
 3828 
 3829         if (error == 0) {
 3830                 if (action == PR_ALLOW)
 3831                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
 3832                 else
 3833                         softc->flags |= DA_FLAG_PACK_LOCKED;
 3834         }
 3835 
 3836         xpt_release_ccb(ccb);
 3837 }
 3838 
 3839 static void
 3840 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
 3841           struct scsi_read_capacity_data_long *rcaplong, size_t rcap_len)
 3842 {
 3843         struct ccb_calc_geometry ccg;
 3844         struct da_softc *softc;
 3845         struct disk_params *dp;
 3846         u_int lbppbe, lalba;
 3847         int error;
 3848 
 3849         softc = (struct da_softc *)periph->softc;
 3850 
 3851         dp = &softc->params;
 3852         dp->secsize = block_len;
 3853         dp->sectors = maxsector + 1;
 3854         if (rcaplong != NULL) {
 3855                 lbppbe = rcaplong->prot_lbppbe & SRC16_LBPPBE;
 3856                 lalba = scsi_2btoul(rcaplong->lalba_lbp);
 3857                 lalba &= SRC16_LALBA_A;
 3858         } else {
 3859                 lbppbe = 0;
 3860                 lalba = 0;
 3861         }
 3862 
 3863         if (lbppbe > 0) {
 3864                 dp->stripesize = block_len << lbppbe;
 3865                 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
 3866                     dp->stripesize;
 3867         } else if (softc->quirks & DA_Q_4K) {
 3868                 dp->stripesize = 4096;
 3869                 dp->stripeoffset = 0;
 3870         } else {
 3871                 dp->stripesize = 0;
 3872                 dp->stripeoffset = 0;
 3873         }
 3874         /*
 3875          * Have the controller provide us with a geometry
 3876          * for this disk.  The only time the geometry
 3877          * matters is when we boot and the controller
 3878          * is the only one knowledgeable enough to come
 3879          * up with something that will make this a bootable
 3880          * device.
 3881          */
 3882         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 3883         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
 3884         ccg.block_size = dp->secsize;
 3885         ccg.volume_size = dp->sectors;
 3886         ccg.heads = 0;
 3887         ccg.secs_per_track = 0;
 3888         ccg.cylinders = 0;
 3889         xpt_action((union ccb*)&ccg);
 3890         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 3891                 /*
 3892                  * We don't know what went wrong here- but just pick
 3893                  * a geometry so we don't have nasty things like divide
 3894                  * by zero.
 3895                  */
 3896                 dp->heads = 255;
 3897                 dp->secs_per_track = 255;
 3898                 dp->cylinders = dp->sectors / (255 * 255);
 3899                 if (dp->cylinders == 0) {
 3900                         dp->cylinders = 1;
 3901                 }
 3902         } else {
 3903                 dp->heads = ccg.heads;
 3904                 dp->secs_per_track = ccg.secs_per_track;
 3905                 dp->cylinders = ccg.cylinders;
 3906         }
 3907 
 3908         /*
 3909          * If the user supplied a read capacity buffer, and if it is
 3910          * different than the previous buffer, update the data in the EDT.
 3911          * If it's the same, we don't bother.  This avoids sending an
 3912          * update every time someone opens this device.
 3913          */
 3914         if ((rcaplong != NULL)
 3915          && (bcmp(rcaplong, &softc->rcaplong,
 3916                   min(sizeof(softc->rcaplong), rcap_len)) != 0)) {
 3917                 struct ccb_dev_advinfo cdai;
 3918 
 3919                 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 3920                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
 3921                 cdai.buftype = CDAI_TYPE_RCAPLONG;
 3922                 cdai.flags = CDAI_FLAG_STORE;
 3923                 cdai.bufsiz = rcap_len;
 3924                 cdai.buf = (uint8_t *)rcaplong;
 3925                 xpt_action((union ccb *)&cdai);
 3926                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
 3927                         cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
 3928                 if (cdai.ccb_h.status != CAM_REQ_CMP) {
 3929                         xpt_print(periph->path, "%s: failed to set read "
 3930                                   "capacity advinfo\n", __func__);
 3931                         /* Use cam_error_print() to decode the status */
 3932                         cam_error_print((union ccb *)&cdai, CAM_ESF_CAM_STATUS,
 3933                                         CAM_EPF_ALL);
 3934                 } else {
 3935                         bcopy(rcaplong, &softc->rcaplong,
 3936                               min(sizeof(softc->rcaplong), rcap_len));
 3937                 }
 3938         }
 3939 
 3940         softc->disk->d_sectorsize = softc->params.secsize;
 3941         softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
 3942         softc->disk->d_stripesize = softc->params.stripesize;
 3943         softc->disk->d_stripeoffset = softc->params.stripeoffset;
 3944         /* XXX: these are not actually "firmware" values, so they may be wrong */
 3945         softc->disk->d_fwsectors = softc->params.secs_per_track;
 3946         softc->disk->d_fwheads = softc->params.heads;
 3947         softc->disk->d_devstat->block_size = softc->params.secsize;
 3948         softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
 3949 
 3950         error = disk_resize(softc->disk, M_NOWAIT);
 3951         if (error != 0)
 3952                 xpt_print(periph->path, "disk_resize(9) failed, error = %d\n", error);
 3953 }
 3954 
 3955 static void
 3956 dasendorderedtag(void *arg)
 3957 {
 3958         struct da_softc *softc = arg;
 3959 
 3960         if (da_send_ordered) {
 3961                 if (!LIST_EMPTY(&softc->pending_ccbs)) {
 3962                         if ((softc->flags & DA_FLAG_WAS_OTAG) == 0)
 3963                                 softc->flags |= DA_FLAG_NEED_OTAG;
 3964                         softc->flags &= ~DA_FLAG_WAS_OTAG;
 3965                 }
 3966         }
 3967         /* Queue us up again */
 3968         callout_reset(&softc->sendordered_c,
 3969             (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
 3970             dasendorderedtag, softc);
 3971 }
 3972 
 3973 /*
 3974  * Step through all DA peripheral drivers, and if the device is still open,
 3975  * sync the disk cache to physical media.
 3976  */
 3977 static void
 3978 dashutdown(void * arg, int howto)
 3979 {
 3980         struct cam_periph *periph;
 3981         struct da_softc *softc;
 3982         union ccb *ccb;
 3983         int error;
 3984 
 3985         CAM_PERIPH_FOREACH(periph, &dadriver) {
 3986                 softc = (struct da_softc *)periph->softc;
 3987                 if (SCHEDULER_STOPPED()) {
 3988                         /* If we paniced with the lock held, do not recurse. */
 3989                         if (!cam_periph_owned(periph) &&
 3990                             (softc->flags & DA_FLAG_OPEN)) {
 3991                                 dadump(softc->disk, NULL, 0, 0, 0);
 3992                         }
 3993                         continue;
 3994                 }
 3995                 cam_periph_lock(periph);
 3996 
 3997                 /*
 3998                  * We only sync the cache if the drive is still open, and
 3999                  * if the drive is capable of it..
 4000                  */
 4001                 if (((softc->flags & DA_FLAG_OPEN) == 0)
 4002                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
 4003                         cam_periph_unlock(periph);
 4004                         continue;
 4005                 }
 4006 
 4007                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
 4008                 scsi_synchronize_cache(&ccb->csio,
 4009                                        /*retries*/0,
 4010                                        /*cbfcnp*/dadone,
 4011                                        MSG_SIMPLE_Q_TAG,
 4012                                        /*begin_lba*/0, /* whole disk */
 4013                                        /*lb_count*/0,
 4014                                        SSD_FULL_SIZE,
 4015                                        60 * 60 * 1000);
 4016 
 4017                 error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0,
 4018                     /*sense_flags*/ SF_NO_RECOVERY | SF_NO_RETRY | SF_QUIET_IR,
 4019                     softc->disk->d_devstat);
 4020                 if (error != 0)
 4021                         xpt_print(periph->path, "Synchronize cache failed\n");
 4022                 xpt_release_ccb(ccb);
 4023                 cam_periph_unlock(periph);
 4024         }
 4025 }
 4026 
 4027 #else /* !_KERNEL */
 4028 
 4029 /*
 4030  * XXX These are only left out of the kernel build to silence warnings.  If,
 4031  * for some reason these functions are used in the kernel, the ifdefs should
 4032  * be moved so they are included both in the kernel and userland.
 4033  */
 4034 void
 4035 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
 4036                  void (*cbfcnp)(struct cam_periph *, union ccb *),
 4037                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
 4038                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
 4039                  u_int32_t timeout)
 4040 {
 4041         struct scsi_format_unit *scsi_cmd;
 4042 
 4043         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
 4044         scsi_cmd->opcode = FORMAT_UNIT;
 4045         scsi_cmd->byte2 = byte2;
 4046         scsi_ulto2b(ileave, scsi_cmd->interleave);
 4047 
 4048         cam_fill_csio(csio,
 4049                       retries,
 4050                       cbfcnp,
 4051                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
 4052                       tag_action,
 4053                       data_ptr,
 4054                       dxfer_len,
 4055                       sense_len,
 4056                       sizeof(*scsi_cmd),
 4057                       timeout);
 4058 }
 4059 
 4060 void
 4061 scsi_read_defects(struct ccb_scsiio *csio, uint32_t retries,
 4062                   void (*cbfcnp)(struct cam_periph *, union ccb *),
 4063                   uint8_t tag_action, uint8_t list_format,
 4064                   uint32_t addr_desc_index, uint8_t *data_ptr,
 4065                   uint32_t dxfer_len, int minimum_cmd_size, 
 4066                   uint8_t sense_len, uint32_t timeout)
 4067 {
 4068         uint8_t cdb_len;
 4069 
 4070         /*
 4071          * These conditions allow using the 10 byte command.  Otherwise we
 4072          * need to use the 12 byte command.
 4073          */
 4074         if ((minimum_cmd_size <= 10)
 4075          && (addr_desc_index == 0) 
 4076          && (dxfer_len <= SRDD10_MAX_LENGTH)) {
 4077                 struct scsi_read_defect_data_10 *cdb10;
 4078 
 4079                 cdb10 = (struct scsi_read_defect_data_10 *)
 4080                         &csio->cdb_io.cdb_bytes;
 4081 
 4082                 cdb_len = sizeof(*cdb10);
 4083                 bzero(cdb10, cdb_len);
 4084                 cdb10->opcode = READ_DEFECT_DATA_10;
 4085                 cdb10->format = list_format;
 4086                 scsi_ulto2b(dxfer_len, cdb10->alloc_length);
 4087         } else {
 4088                 struct scsi_read_defect_data_12 *cdb12;
 4089 
 4090                 cdb12 = (struct scsi_read_defect_data_12 *)
 4091                         &csio->cdb_io.cdb_bytes;
 4092 
 4093                 cdb_len = sizeof(*cdb12);
 4094                 bzero(cdb12, cdb_len);
 4095                 cdb12->opcode = READ_DEFECT_DATA_12;
 4096                 cdb12->format = list_format;
 4097                 scsi_ulto4b(dxfer_len, cdb12->alloc_length);
 4098                 scsi_ulto4b(addr_desc_index, cdb12->address_descriptor_index);
 4099         }
 4100 
 4101         cam_fill_csio(csio,
 4102                       retries,
 4103                       cbfcnp,
 4104                       /*flags*/ CAM_DIR_IN,
 4105                       tag_action,
 4106                       data_ptr,
 4107                       dxfer_len,
 4108                       sense_len,
 4109                       cdb_len,
 4110                       timeout);
 4111 }
 4112 
 4113 void
 4114 scsi_sanitize(struct ccb_scsiio *csio, u_int32_t retries,
 4115               void (*cbfcnp)(struct cam_periph *, union ccb *),
 4116               u_int8_t tag_action, u_int8_t byte2, u_int16_t control,
 4117               u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
 4118               u_int32_t timeout)
 4119 {
 4120         struct scsi_sanitize *scsi_cmd;
 4121 
 4122         scsi_cmd = (struct scsi_sanitize *)&csio->cdb_io.cdb_bytes;
 4123         scsi_cmd->opcode = SANITIZE;
 4124         scsi_cmd->byte2 = byte2;
 4125         scsi_cmd->control = control;
 4126         scsi_ulto2b(dxfer_len, scsi_cmd->length);
 4127 
 4128         cam_fill_csio(csio,
 4129                       retries,
 4130                       cbfcnp,
 4131                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
 4132                       tag_action,
 4133                       data_ptr,
 4134                       dxfer_len,
 4135                       sense_len,
 4136                       sizeof(*scsi_cmd),
 4137                       timeout);
 4138 }
 4139 
 4140 #endif /* _KERNEL */

Cache object: 53871b95208abf1b506b10b3ee5b1f15


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