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: releng/8.2/sys/cam/scsi/scsi_da.c 215773 2010-11-23 21:35:13Z mav $");
   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 <geom/geom_disk.h>
   48 #endif /* _KERNEL */
   49 
   50 #ifndef _KERNEL
   51 #include <stdio.h>
   52 #include <string.h>
   53 #endif /* _KERNEL */
   54 
   55 #include <cam/cam.h>
   56 #include <cam/cam_ccb.h>
   57 #include <cam/cam_periph.h>
   58 #include <cam/cam_xpt_periph.h>
   59 #include <cam/cam_sim.h>
   60 
   61 #include <cam/scsi/scsi_message.h>
   62 
   63 #ifndef _KERNEL 
   64 #include <cam/scsi/scsi_da.h>
   65 #endif /* !_KERNEL */
   66 
   67 #ifdef _KERNEL
   68 typedef enum {
   69         DA_STATE_PROBE,
   70         DA_STATE_PROBE2,
   71         DA_STATE_NORMAL
   72 } da_state;
   73 
   74 typedef enum {
   75         DA_FLAG_PACK_INVALID    = 0x001,
   76         DA_FLAG_NEW_PACK        = 0x002,
   77         DA_FLAG_PACK_LOCKED     = 0x004,
   78         DA_FLAG_PACK_REMOVABLE  = 0x008,
   79         DA_FLAG_TAGGED_QUEUING  = 0x010,
   80         DA_FLAG_NEED_OTAG       = 0x020,
   81         DA_FLAG_WENT_IDLE       = 0x040,
   82         DA_FLAG_RETRY_UA        = 0x080,
   83         DA_FLAG_OPEN            = 0x100,
   84         DA_FLAG_SCTX_INIT       = 0x200
   85 } da_flags;
   86 
   87 typedef enum {
   88         DA_Q_NONE               = 0x00,
   89         DA_Q_NO_SYNC_CACHE      = 0x01,
   90         DA_Q_NO_6_BYTE          = 0x02,
   91         DA_Q_NO_PREVENT         = 0x04
   92 } da_quirks;
   93 
   94 typedef enum {
   95         DA_CCB_PROBE            = 0x01,
   96         DA_CCB_PROBE2           = 0x02,
   97         DA_CCB_BUFFER_IO        = 0x03,
   98         DA_CCB_WAITING          = 0x04,
   99         DA_CCB_DUMP             = 0x05,
  100         DA_CCB_TYPE_MASK        = 0x0F,
  101         DA_CCB_RETRY_UA         = 0x10
  102 } da_ccb_state;
  103 
  104 /* Offsets into our private area for storing information */
  105 #define ccb_state       ppriv_field0
  106 #define ccb_bp          ppriv_ptr1
  107 
  108 struct disk_params {
  109         u_int8_t  heads;
  110         u_int32_t cylinders;
  111         u_int8_t  secs_per_track;
  112         u_int32_t secsize;      /* Number of bytes/sector */
  113         u_int64_t sectors;      /* total number sectors */
  114 };
  115 
  116 struct da_softc {
  117         struct   bio_queue_head bio_queue;
  118         SLIST_ENTRY(da_softc) links;
  119         LIST_HEAD(, ccb_hdr) pending_ccbs;
  120         da_state state;
  121         da_flags flags; 
  122         da_quirks quirks;
  123         int      minimum_cmd_size;
  124         int      ordered_tag_count;
  125         int      outstanding_cmds;
  126         struct   disk_params params;
  127         struct   disk *disk;
  128         union    ccb saved_ccb;
  129         struct task             sysctl_task;
  130         struct sysctl_ctx_list  sysctl_ctx;
  131         struct sysctl_oid       *sysctl_tree;
  132         struct callout          sendordered_c;
  133 };
  134 
  135 struct da_quirk_entry {
  136         struct scsi_inquiry_pattern inq_pat;
  137         da_quirks quirks;
  138 };
  139 
  140 static const char quantum[] = "QUANTUM";
  141 static const char microp[] = "MICROP";
  142 
  143 static struct da_quirk_entry da_quirk_table[] =
  144 {
  145         /* SPI, FC devices */
  146         {
  147                 /*
  148                  * Fujitsu M2513A MO drives.
  149                  * Tested devices: M2513A2 firmware versions 1200 & 1300.
  150                  * (dip switch selects whether T_DIRECT or T_OPTICAL device)
  151                  * Reported by: W.Scholten <whs@xs4all.nl>
  152                  */
  153                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
  154                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  155         },
  156         {
  157                 /* See above. */
  158                 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
  159                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  160         },
  161         {
  162                 /*
  163                  * This particular Fujitsu drive doesn't like the
  164                  * synchronize cache command.
  165                  * Reported by: Tom Jackson <toj@gorilla.net>
  166                  */
  167                 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
  168                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  169         },
  170         {
  171                 /*
  172                  * This drive doesn't like the synchronize cache command
  173                  * either.  Reported by: Matthew Jacob <mjacob@feral.com>
  174                  * in NetBSD PR kern/6027, August 24, 1998.
  175                  */
  176                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
  177                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  178         },
  179         {
  180                 /*
  181                  * This drive doesn't like the synchronize cache command
  182                  * either.  Reported by: Hellmuth Michaelis (hm@kts.org)
  183                  * (PR 8882).
  184                  */
  185                 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
  186                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  187         },
  188         {
  189                 /*
  190                  * Doesn't like the synchronize cache command.
  191                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
  192                  */
  193                 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
  194                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  195         },
  196         {
  197                 /*
  198                  * Doesn't like the synchronize cache command.
  199                  * Reported by: Blaz Zupan <blaz@gold.amis.net>
  200                  */
  201                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
  202                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  203         },
  204         {
  205                 /*
  206                  * Doesn't like the synchronize cache command.
  207                  */
  208                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
  209                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  210         },
  211         {
  212                 /*
  213                  * Doesn't like the synchronize cache command.
  214                  * Reported by: walter@pelissero.de
  215                  */
  216                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
  217                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  218         },
  219         {
  220                 /*
  221                  * Doesn't work correctly with 6 byte reads/writes.
  222                  * Returns illegal request, and points to byte 9 of the
  223                  * 6-byte CDB.
  224                  * Reported by:  Adam McDougall <bsdx@spawnet.com>
  225                  */
  226                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
  227                 /*quirks*/ DA_Q_NO_6_BYTE
  228         },
  229         {
  230                 /* See above. */
  231                 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
  232                 /*quirks*/ DA_Q_NO_6_BYTE
  233         },
  234         {
  235                 /*
  236                  * Doesn't like the synchronize cache command.
  237                  * Reported by: walter@pelissero.de
  238                  */
  239                 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
  240                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  241         },
  242         {
  243                 /*
  244                  * The CISS RAID controllers do not support SYNC_CACHE
  245                  */
  246                 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
  247                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  248         },
  249         /* USB mass storage devices supported by umass(4) */
  250         {
  251                 /*
  252                  * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
  253                  * PR: kern/51675
  254                  */
  255                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
  256                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  257         },
  258         {
  259                 /*
  260                  * Power Quotient Int. (PQI) USB flash key
  261                  * PR: kern/53067
  262                  */
  263                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
  264                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  265         },
  266         {
  267                 /*
  268                  * Creative Nomad MUVO mp3 player (USB)
  269                  * PR: kern/53094
  270                  */
  271                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
  272                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  273         },
  274         {
  275                 /*
  276                  * Jungsoft NEXDISK USB flash key
  277                  * PR: kern/54737
  278                  */
  279                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
  280                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  281         },
  282         {
  283                 /*
  284                  * FreeDik USB Mini Data Drive
  285                  * PR: kern/54786
  286                  */
  287                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
  288                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  289         },
  290         {
  291                 /*
  292                  * Sigmatel USB Flash MP3 Player
  293                  * PR: kern/57046
  294                  */
  295                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
  296                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  297         },
  298         {
  299                 /*
  300                  * Neuros USB Digital Audio Computer
  301                  * PR: kern/63645
  302                  */
  303                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
  304                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  305         },
  306         {
  307                 /*
  308                  * SEAGRAND NP-900 MP3 Player
  309                  * PR: kern/64563
  310                  */
  311                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
  312                 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  313         },
  314         {
  315                 /*
  316                  * iRiver iFP MP3 player (with UMS Firmware)
  317                  * PR: kern/54881, i386/63941, kern/66124
  318                  */
  319                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
  320                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  321         },
  322         {
  323                 /*
  324                  * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
  325                  * PR: kern/70158
  326                  */
  327                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
  328                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  329         },
  330         {
  331                 /*
  332                  * ZICPlay USB MP3 Player with FM
  333                  * PR: kern/75057
  334                  */
  335                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
  336                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  337         },
  338         {
  339                 /*
  340                  * TEAC USB floppy mechanisms
  341                  */
  342                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
  343                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  344         },
  345         {
  346                 /*
  347                  * Kingston DataTraveler II+ USB Pen-Drive.
  348                  * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
  349                  */
  350                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
  351                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  352         },
  353         {
  354                 /*
  355                  * Motorola E398 Mobile Phone (TransFlash memory card).
  356                  * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
  357                  * PR: usb/89889
  358                  */
  359                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
  360                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  361         },
  362         {
  363                 /*
  364                  * Qware BeatZkey! Pro
  365                  * PR: usb/79164
  366                  */
  367                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
  368                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  369         },
  370         {
  371                 /*
  372                  * Time DPA20B 1GB MP3 Player
  373                  * PR: usb/81846
  374                  */
  375                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
  376                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  377         },
  378         {
  379                 /*
  380                  * Samsung USB key 128Mb
  381                  * PR: usb/90081
  382                  */
  383                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
  384                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  385         },
  386         {
  387                 /*
  388                  * Kingston DataTraveler 2.0 USB Flash memory.
  389                  * PR: usb/89196
  390                  */
  391                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
  392                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  393         },
  394         {
  395                 /*
  396                  * Creative MUVO Slim mp3 player (USB)
  397                  * PR: usb/86131
  398                  */
  399                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
  400                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
  401                 },
  402         {
  403                 /*
  404                  * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
  405                  * PR: usb/80487
  406                  */
  407                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
  408                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  409         },
  410         {
  411                 /*
  412                  * SanDisk Micro Cruzer 128MB
  413                  * PR: usb/75970
  414                  */
  415                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
  416                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  417         },
  418         {
  419                 /*
  420                  * TOSHIBA TransMemory USB sticks
  421                  * PR: kern/94660
  422                  */
  423                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
  424                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  425         },
  426         {
  427                 /*
  428                  * PNY USB Flash keys
  429                  * PR: usb/75578, usb/72344, usb/65436 
  430                  */
  431                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
  432                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  433         },
  434         {
  435                 /*
  436                  * Genesys 6-in-1 Card Reader
  437                  * PR: usb/94647
  438                  */
  439                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
  440                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  441         },
  442         {
  443                 /*
  444                  * Rekam Digital CAMERA
  445                  * PR: usb/98713
  446                  */
  447                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
  448                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  449         },
  450         {
  451                 /*
  452                  * iRiver H10 MP3 player
  453                  * PR: usb/102547
  454                  */
  455                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
  456                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  457         },
  458         {
  459                 /*
  460                  * iRiver U10 MP3 player
  461                  * PR: usb/92306
  462                  */
  463                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
  464                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  465         },
  466         {
  467                 /*
  468                  * X-Micro Flash Disk
  469                  * PR: usb/96901
  470                  */
  471                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
  472                 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  473         },
  474         {
  475                 /*
  476                  * EasyMP3 EM732X USB 2.0 Flash MP3 Player
  477                  * PR: usb/96546
  478                  */
  479                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
  480                 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  481         },
  482         {
  483                 /*
  484                  * Denver MP3 player
  485                  * PR: usb/107101
  486                  */
  487                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
  488                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  489         },
  490         {
  491                 /*
  492                  * Philips USB Key Audio KEY013
  493                  * PR: usb/68412
  494                  */
  495                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
  496                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
  497         },
  498         {
  499                 /*
  500                  * JNC MP3 Player
  501                  * PR: usb/94439
  502                  */
  503                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
  504                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  505         },
  506         {
  507                 /*
  508                  * SAMSUNG MP0402H
  509                  * PR: usb/108427
  510                  */
  511                 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
  512                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  513         },
  514         {
  515                 /*
  516                  * I/O Magic USB flash - Giga Bank
  517                  * PR: usb/108810
  518                  */
  519                 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
  520                 /*quirks*/ DA_Q_NO_SYNC_CACHE
  521         },
  522         {
  523                 /*
  524                  * JoyFly 128mb USB Flash Drive
  525                  * PR: 96133
  526                  */
  527                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
  528                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  529         },
  530         {
  531                 /*
  532                  * ChipsBnk usb stick
  533                  * PR: 103702
  534                  */
  535                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
  536                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  537         },
  538         {
  539                 /*
  540                  * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
  541                  * PR: 129858
  542                  */
  543                 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
  544                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  545         },
  546         {
  547                 /*
  548                  * Samsung YP-U3 mp3-player
  549                  * PR: 125398
  550                  */
  551                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
  552                  "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  553         },
  554         {
  555                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
  556                  "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
  557         },
  558         {
  559                 /*
  560                  * Sony Cyber-Shot DSC cameras
  561                  * PR: usb/137035
  562                  */
  563                 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
  564                 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
  565         }
  566 };
  567 
  568 static  disk_strategy_t dastrategy;
  569 static  dumper_t        dadump;
  570 static  periph_init_t   dainit;
  571 static  void            daasync(void *callback_arg, u_int32_t code,
  572                                 struct cam_path *path, void *arg);
  573 static  void            dasysctlinit(void *context, int pending);
  574 static  int             dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
  575 static  periph_ctor_t   daregister;
  576 static  periph_dtor_t   dacleanup;
  577 static  periph_start_t  dastart;
  578 static  periph_oninv_t  daoninvalidate;
  579 static  void            dadone(struct cam_periph *periph,
  580                                union ccb *done_ccb);
  581 static  int             daerror(union ccb *ccb, u_int32_t cam_flags,
  582                                 u_int32_t sense_flags);
  583 static void             daprevent(struct cam_periph *periph, int action);
  584 static int              dagetcapacity(struct cam_periph *periph);
  585 static void             dasetgeom(struct cam_periph *periph, uint32_t block_len,
  586                                   uint64_t maxsector);
  587 static timeout_t        dasendorderedtag;
  588 static void             dashutdown(void *arg, int howto);
  589 
  590 #ifndef DA_DEFAULT_TIMEOUT
  591 #define DA_DEFAULT_TIMEOUT 60   /* Timeout in seconds */
  592 #endif
  593 
  594 #ifndef DA_DEFAULT_RETRY
  595 #define DA_DEFAULT_RETRY        4
  596 #endif
  597 
  598 #ifndef DA_DEFAULT_SEND_ORDERED
  599 #define DA_DEFAULT_SEND_ORDERED 1
  600 #endif
  601 
  602 
  603 static int da_retry_count = DA_DEFAULT_RETRY;
  604 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
  605 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
  606 
  607 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
  608             "CAM Direct Access Disk driver");
  609 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
  610            &da_retry_count, 0, "Normal I/O retry count");
  611 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
  612 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
  613            &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
  614 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
  615 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
  616            &da_send_ordered, 0, "Send Ordered Tags");
  617 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
  618 
  619 /*
  620  * DA_ORDEREDTAG_INTERVAL determines how often, relative
  621  * to the default timeout, we check to see whether an ordered
  622  * tagged transaction is appropriate to prevent simple tag
  623  * starvation.  Since we'd like to ensure that there is at least
  624  * 1/2 of the timeout length left for a starved transaction to
  625  * complete after we've sent an ordered tag, we must poll at least
  626  * four times in every timeout period.  This takes care of the worst
  627  * case where a starved transaction starts during an interval that
  628  * meets the requirement "don't send an ordered tag" test so it takes
  629  * us two intervals to determine that a tag must be sent.
  630  */
  631 #ifndef DA_ORDEREDTAG_INTERVAL
  632 #define DA_ORDEREDTAG_INTERVAL 4
  633 #endif
  634 
  635 static struct periph_driver dadriver =
  636 {
  637         dainit, "da",
  638         TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
  639 };
  640 
  641 PERIPHDRIVER_DECLARE(da, dadriver);
  642 
  643 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
  644 
  645 static int
  646 daopen(struct disk *dp)
  647 {
  648         struct cam_periph *periph;
  649         struct da_softc *softc;
  650         int unit;
  651         int error;
  652 
  653         periph = (struct cam_periph *)dp->d_drv1;
  654         if (periph == NULL) {
  655                 return (ENXIO); 
  656         }
  657 
  658         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
  659                 return(ENXIO);
  660         }
  661 
  662         cam_periph_lock(periph);
  663         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
  664                 cam_periph_unlock(periph);
  665                 cam_periph_release(periph);
  666                 return (error);
  667         }
  668 
  669         unit = periph->unit_number;
  670         softc = (struct da_softc *)periph->softc;
  671         softc->flags |= DA_FLAG_OPEN;
  672 
  673         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
  674             ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
  675              unit));
  676 
  677         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
  678                 /* Invalidate our pack information. */
  679                 softc->flags &= ~DA_FLAG_PACK_INVALID;
  680         }
  681 
  682         error = dagetcapacity(periph);
  683 
  684         if (error == 0) {
  685 
  686                 softc->disk->d_sectorsize = softc->params.secsize;
  687                 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
  688                 /* XXX: these are not actually "firmware" values, so they may be wrong */
  689                 softc->disk->d_fwsectors = softc->params.secs_per_track;
  690                 softc->disk->d_fwheads = softc->params.heads;
  691                 softc->disk->d_devstat->block_size = softc->params.secsize;
  692                 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
  693 
  694                 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
  695                     (softc->quirks & DA_Q_NO_PREVENT) == 0)
  696                         daprevent(periph, PR_PREVENT);
  697         } else
  698                 softc->flags &= ~DA_FLAG_OPEN;
  699 
  700         cam_periph_unhold(periph);
  701         cam_periph_unlock(periph);
  702 
  703         if (error != 0) {
  704                 cam_periph_release(periph);
  705         }
  706         return (error);
  707 }
  708 
  709 static int
  710 daclose(struct disk *dp)
  711 {
  712         struct  cam_periph *periph;
  713         struct  da_softc *softc;
  714         int error;
  715 
  716         periph = (struct cam_periph *)dp->d_drv1;
  717         if (periph == NULL)
  718                 return (ENXIO); 
  719 
  720         cam_periph_lock(periph);
  721         if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
  722                 cam_periph_unlock(periph);
  723                 cam_periph_release(periph);
  724                 return (error);
  725         }
  726 
  727         softc = (struct da_softc *)periph->softc;
  728 
  729         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
  730                 union   ccb *ccb;
  731 
  732                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
  733 
  734                 scsi_synchronize_cache(&ccb->csio,
  735                                        /*retries*/1,
  736                                        /*cbfcnp*/dadone,
  737                                        MSG_SIMPLE_Q_TAG,
  738                                        /*begin_lba*/0,/* Cover the whole disk */
  739                                        /*lb_count*/0,
  740                                        SSD_FULL_SIZE,
  741                                        5 * 60 * 1000);
  742 
  743                 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
  744                                   /*sense_flags*/SF_RETRY_UA,
  745                                   softc->disk->d_devstat);
  746 
  747                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
  748                         if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
  749                              CAM_SCSI_STATUS_ERROR) {
  750                                 int asc, ascq;
  751                                 int sense_key, error_code;
  752 
  753                                 scsi_extract_sense(&ccb->csio.sense_data,
  754                                                    &error_code,
  755                                                    &sense_key, 
  756                                                    &asc, &ascq);
  757                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
  758                                         scsi_sense_print(&ccb->csio);
  759                         } else {
  760                                 xpt_print(periph->path, "Synchronize cache "
  761                                     "failed, status == 0x%x, scsi status == "
  762                                     "0x%x\n", ccb->csio.ccb_h.status,
  763                                     ccb->csio.scsi_status);
  764                         }
  765                 }
  766 
  767                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
  768                         cam_release_devq(ccb->ccb_h.path,
  769                                          /*relsim_flags*/0,
  770                                          /*reduction*/0,
  771                                          /*timeout*/0,
  772                                          /*getcount_only*/0);
  773 
  774                 xpt_release_ccb(ccb);
  775 
  776         }
  777 
  778         if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
  779                 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
  780                         daprevent(periph, PR_ALLOW);
  781                 /*
  782                  * If we've got removeable media, mark the blocksize as
  783                  * unavailable, since it could change when new media is
  784                  * inserted.
  785                  */
  786                 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
  787         }
  788 
  789         softc->flags &= ~DA_FLAG_OPEN;
  790         cam_periph_unhold(periph);
  791         cam_periph_unlock(periph);
  792         cam_periph_release(periph);
  793         return (0);     
  794 }
  795 
  796 /*
  797  * Actually translate the requested transfer into one the physical driver
  798  * can understand.  The transfer is described by a buf and will include
  799  * only one physical transfer.
  800  */
  801 static void
  802 dastrategy(struct bio *bp)
  803 {
  804         struct cam_periph *periph;
  805         struct da_softc *softc;
  806         
  807         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
  808         if (periph == NULL) {
  809                 biofinish(bp, NULL, ENXIO);
  810                 return;
  811         }
  812         softc = (struct da_softc *)periph->softc;
  813 
  814         cam_periph_lock(periph);
  815 
  816         /*
  817          * If the device has been made invalid, error out
  818          */
  819         if ((softc->flags & DA_FLAG_PACK_INVALID)) {
  820                 cam_periph_unlock(periph);
  821                 biofinish(bp, NULL, ENXIO);
  822                 return;
  823         }
  824         
  825         /*
  826          * Place it in the queue of disk activities for this disk
  827          */
  828         bioq_disksort(&softc->bio_queue, bp);
  829 
  830         /*
  831          * Schedule ourselves for performing the work.
  832          */
  833         xpt_schedule(periph, CAM_PRIORITY_NORMAL);
  834         cam_periph_unlock(periph);
  835 
  836         return;
  837 }
  838 
  839 static int
  840 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
  841 {
  842         struct      cam_periph *periph;
  843         struct      da_softc *softc;
  844         u_int       secsize;
  845         struct      ccb_scsiio csio;
  846         struct      disk *dp;
  847 
  848         dp = arg;
  849         periph = dp->d_drv1;
  850         if (periph == NULL)
  851                 return (ENXIO);
  852         softc = (struct da_softc *)periph->softc;
  853         cam_periph_lock(periph);
  854         secsize = softc->params.secsize;
  855         
  856         if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
  857                 cam_periph_unlock(periph);
  858                 return (ENXIO);
  859         }
  860 
  861         if (length > 0) {
  862                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
  863                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
  864                 scsi_read_write(&csio,
  865                                 /*retries*/1,
  866                                 dadone,
  867                                 MSG_ORDERED_Q_TAG,
  868                                 /*read*/FALSE,
  869                                 /*byte2*/0,
  870                                 /*minimum_cmd_size*/ softc->minimum_cmd_size,
  871                                 offset / secsize,
  872                                 length / secsize,
  873                                 /*data_ptr*/(u_int8_t *) virtual,
  874                                 /*dxfer_len*/length,
  875                                 /*sense_len*/SSD_FULL_SIZE,
  876                                 DA_DEFAULT_TIMEOUT * 1000);             
  877                 xpt_polled_action((union ccb *)&csio);
  878 
  879                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
  880                         printf("Aborting dump due to I/O error.\n");
  881                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
  882                              CAM_SCSI_STATUS_ERROR)
  883                                 scsi_sense_print(&csio);
  884                         else
  885                                 printf("status == 0x%x, scsi status == 0x%x\n",
  886                                        csio.ccb_h.status, csio.scsi_status);
  887                         return(EIO);
  888                 }
  889                 cam_periph_unlock(periph);
  890                 return(0);
  891         }
  892                 
  893         /*
  894          * Sync the disk cache contents to the physical media.
  895          */
  896         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
  897 
  898                 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
  899                 csio.ccb_h.ccb_state = DA_CCB_DUMP;
  900                 scsi_synchronize_cache(&csio,
  901                                        /*retries*/1,
  902                                        /*cbfcnp*/dadone,
  903                                        MSG_SIMPLE_Q_TAG,
  904                                        /*begin_lba*/0,/* Cover the whole disk */
  905                                        /*lb_count*/0,
  906                                        SSD_FULL_SIZE,
  907                                        5 * 60 * 1000);
  908                 xpt_polled_action((union ccb *)&csio);
  909 
  910                 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
  911                         if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
  912                              CAM_SCSI_STATUS_ERROR) {
  913                                 int asc, ascq;
  914                                 int sense_key, error_code;
  915 
  916                                 scsi_extract_sense(&csio.sense_data,
  917                                                    &error_code,
  918                                                    &sense_key, 
  919                                                    &asc, &ascq);
  920                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
  921                                         scsi_sense_print(&csio);
  922                         } else {
  923                                 xpt_print(periph->path, "Synchronize cache "
  924                                     "failed, status == 0x%x, scsi status == "
  925                                     "0x%x\n", csio.ccb_h.status,
  926                                     csio.scsi_status);
  927                         }
  928                 }
  929         }
  930         cam_periph_unlock(periph);
  931         return (0);
  932 }
  933 
  934 static void
  935 dainit(void)
  936 {
  937         cam_status status;
  938 
  939         /*
  940          * Install a global async callback.  This callback will
  941          * receive async callbacks like "new device found".
  942          */
  943         status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
  944 
  945         if (status != CAM_REQ_CMP) {
  946                 printf("da: Failed to attach master async callback "
  947                        "due to status 0x%x!\n", status);
  948         } else if (da_send_ordered) {
  949 
  950                 /* Register our shutdown event handler */
  951                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, 
  952                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
  953                     printf("dainit: shutdown event registration failed!\n");
  954         }
  955 }
  956 
  957 static void
  958 daoninvalidate(struct cam_periph *periph)
  959 {
  960         struct da_softc *softc;
  961 
  962         softc = (struct da_softc *)periph->softc;
  963 
  964         /*
  965          * De-register any async callbacks.
  966          */
  967         xpt_register_async(0, daasync, periph, periph->path);
  968 
  969         softc->flags |= DA_FLAG_PACK_INVALID;
  970 
  971         /*
  972          * Return all queued I/O with ENXIO.
  973          * XXX Handle any transactions queued to the card
  974          *     with XPT_ABORT_CCB.
  975          */
  976         bioq_flush(&softc->bio_queue, NULL, ENXIO);
  977 
  978         disk_gone(softc->disk);
  979         xpt_print(periph->path, "lost device\n");
  980 }
  981 
  982 static void
  983 dacleanup(struct cam_periph *periph)
  984 {
  985         struct da_softc *softc;
  986 
  987         softc = (struct da_softc *)periph->softc;
  988 
  989         xpt_print(periph->path, "removing device entry\n");
  990         cam_periph_unlock(periph);
  991 
  992         /*
  993          * If we can't free the sysctl tree, oh well...
  994          */
  995         if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
  996             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
  997                 xpt_print(periph->path, "can't remove sysctl context\n");
  998         }
  999 
 1000         disk_destroy(softc->disk);
 1001         callout_drain(&softc->sendordered_c);
 1002         free(softc, M_DEVBUF);
 1003         cam_periph_lock(periph);
 1004 }
 1005 
 1006 static void
 1007 daasync(void *callback_arg, u_int32_t code,
 1008         struct cam_path *path, void *arg)
 1009 {
 1010         struct cam_periph *periph;
 1011 
 1012         periph = (struct cam_periph *)callback_arg;
 1013         switch (code) {
 1014         case AC_FOUND_DEVICE:
 1015         {
 1016                 struct ccb_getdev *cgd;
 1017                 cam_status status;
 1018  
 1019                 cgd = (struct ccb_getdev *)arg;
 1020                 if (cgd == NULL)
 1021                         break;
 1022 
 1023                 if (cgd->protocol != PROTO_SCSI)
 1024                         break;
 1025 
 1026                 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
 1027                     && SID_TYPE(&cgd->inq_data) != T_RBC
 1028                     && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
 1029                         break;
 1030 
 1031                 /*
 1032                  * Allocate a peripheral instance for
 1033                  * this device and start the probe
 1034                  * process.
 1035                  */
 1036                 status = cam_periph_alloc(daregister, daoninvalidate,
 1037                                           dacleanup, dastart,
 1038                                           "da", CAM_PERIPH_BIO,
 1039                                           cgd->ccb_h.path, daasync,
 1040                                           AC_FOUND_DEVICE, cgd);
 1041 
 1042                 if (status != CAM_REQ_CMP
 1043                  && status != CAM_REQ_INPROG)
 1044                         printf("daasync: Unable to attach to new device "
 1045                                 "due to status 0x%x\n", status);
 1046                 break;
 1047         }
 1048         case AC_SENT_BDR:
 1049         case AC_BUS_RESET:
 1050         {
 1051                 struct da_softc *softc;
 1052                 struct ccb_hdr *ccbh;
 1053 
 1054                 softc = (struct da_softc *)periph->softc;
 1055                 /*
 1056                  * Don't fail on the expected unit attention
 1057                  * that will occur.
 1058                  */
 1059                 softc->flags |= DA_FLAG_RETRY_UA;
 1060                 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
 1061                         ccbh->ccb_state |= DA_CCB_RETRY_UA;
 1062                 /* FALLTHROUGH*/
 1063         }
 1064         default:
 1065                 cam_periph_async(periph, code, path, arg);
 1066                 break;
 1067         }
 1068 }
 1069 
 1070 static void
 1071 dasysctlinit(void *context, int pending)
 1072 {
 1073         struct cam_periph *periph;
 1074         struct da_softc *softc;
 1075         char tmpstr[80], tmpstr2[80];
 1076 
 1077         periph = (struct cam_periph *)context;
 1078         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
 1079                 return;
 1080 
 1081         softc = (struct da_softc *)periph->softc;
 1082         snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
 1083         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
 1084 
 1085         sysctl_ctx_init(&softc->sysctl_ctx);
 1086         softc->flags |= DA_FLAG_SCTX_INIT;
 1087         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
 1088                 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
 1089                 CTLFLAG_RD, 0, tmpstr);
 1090         if (softc->sysctl_tree == NULL) {
 1091                 printf("dasysctlinit: unable to allocate sysctl tree\n");
 1092                 cam_periph_release(periph);
 1093                 return;
 1094         }
 1095 
 1096         /*
 1097          * Now register the sysctl handler, so the user can the value on
 1098          * the fly.
 1099          */
 1100         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
 1101                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
 1102                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
 1103                 "Minimum CDB size");
 1104 
 1105         cam_periph_release(periph);
 1106 }
 1107 
 1108 static int
 1109 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
 1110 {
 1111         int error, value;
 1112 
 1113         value = *(int *)arg1;
 1114 
 1115         error = sysctl_handle_int(oidp, &value, 0, req);
 1116 
 1117         if ((error != 0)
 1118          || (req->newptr == NULL))
 1119                 return (error);
 1120 
 1121         /*
 1122          * Acceptable values here are 6, 10, 12 or 16.
 1123          */
 1124         if (value < 6)
 1125                 value = 6;
 1126         else if ((value > 6)
 1127               && (value <= 10))
 1128                 value = 10;
 1129         else if ((value > 10)
 1130               && (value <= 12))
 1131                 value = 12;
 1132         else if (value > 12)
 1133                 value = 16;
 1134 
 1135         *(int *)arg1 = value;
 1136 
 1137         return (0);
 1138 }
 1139 
 1140 static cam_status
 1141 daregister(struct cam_periph *periph, void *arg)
 1142 {
 1143         struct da_softc *softc;
 1144         struct ccb_pathinq cpi;
 1145         struct ccb_getdev *cgd;
 1146         char tmpstr[80];
 1147         caddr_t match;
 1148 
 1149         cgd = (struct ccb_getdev *)arg;
 1150         if (periph == NULL) {
 1151                 printf("daregister: periph was NULL!!\n");
 1152                 return(CAM_REQ_CMP_ERR);
 1153         }
 1154 
 1155         if (cgd == NULL) {
 1156                 printf("daregister: no getdev CCB, can't register device\n");
 1157                 return(CAM_REQ_CMP_ERR);
 1158         }
 1159 
 1160         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
 1161             M_NOWAIT|M_ZERO);
 1162 
 1163         if (softc == NULL) {
 1164                 printf("daregister: Unable to probe new device. "
 1165                        "Unable to allocate softc\n");                           
 1166                 return(CAM_REQ_CMP_ERR);
 1167         }
 1168 
 1169         LIST_INIT(&softc->pending_ccbs);
 1170         softc->state = DA_STATE_PROBE;
 1171         bioq_init(&softc->bio_queue);
 1172         if (SID_IS_REMOVABLE(&cgd->inq_data))
 1173                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
 1174         if ((cgd->inq_data.flags & SID_CmdQue) != 0)
 1175                 softc->flags |= DA_FLAG_TAGGED_QUEUING;
 1176 
 1177         periph->softc = softc;
 1178 
 1179         /*
 1180          * See if this device has any quirks.
 1181          */
 1182         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
 1183                                (caddr_t)da_quirk_table,
 1184                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
 1185                                sizeof(*da_quirk_table), scsi_inquiry_match);
 1186 
 1187         if (match != NULL)
 1188                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
 1189         else
 1190                 softc->quirks = DA_Q_NONE;
 1191 
 1192         /* Check if the SIM does not want 6 byte commands */
 1193         bzero(&cpi, sizeof(cpi));
 1194         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 1195         cpi.ccb_h.func_code = XPT_PATH_INQ;
 1196         xpt_action((union ccb *)&cpi);
 1197         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
 1198                 softc->quirks |= DA_Q_NO_6_BYTE;
 1199 
 1200         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
 1201 
 1202         /*
 1203          * RBC devices don't have to support READ(6), only READ(10).
 1204          */
 1205         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
 1206                 softc->minimum_cmd_size = 10;
 1207         else
 1208                 softc->minimum_cmd_size = 6;
 1209 
 1210         /*
 1211          * Load the user's default, if any.
 1212          */
 1213         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
 1214                  periph->unit_number);
 1215         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
 1216 
 1217         /*
 1218          * 6, 10, 12 and 16 are the currently permissible values.
 1219          */
 1220         if (softc->minimum_cmd_size < 6)
 1221                 softc->minimum_cmd_size = 6;
 1222         else if ((softc->minimum_cmd_size > 6)
 1223               && (softc->minimum_cmd_size <= 10))
 1224                 softc->minimum_cmd_size = 10;
 1225         else if ((softc->minimum_cmd_size > 10)
 1226               && (softc->minimum_cmd_size <= 12))
 1227                 softc->minimum_cmd_size = 12;
 1228         else if (softc->minimum_cmd_size > 12)
 1229                 softc->minimum_cmd_size = 16;
 1230 
 1231         /*
 1232          * Register this media as a disk
 1233          */
 1234 
 1235         mtx_unlock(periph->sim->mtx);
 1236         softc->disk = disk_alloc();
 1237         softc->disk->d_open = daopen;
 1238         softc->disk->d_close = daclose;
 1239         softc->disk->d_strategy = dastrategy;
 1240         softc->disk->d_dump = dadump;
 1241         softc->disk->d_name = "da";
 1242         softc->disk->d_drv1 = periph;
 1243         if (cpi.maxio == 0)
 1244                 softc->disk->d_maxsize = DFLTPHYS;      /* traditional default */
 1245         else if (cpi.maxio > MAXPHYS)
 1246                 softc->disk->d_maxsize = MAXPHYS;       /* for safety */
 1247         else
 1248                 softc->disk->d_maxsize = cpi.maxio;
 1249         softc->disk->d_unit = periph->unit_number;
 1250         softc->disk->d_flags = 0;
 1251         if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
 1252                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
 1253         strlcpy(softc->disk->d_ident, cgd->serial_num,
 1254             MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
 1255         disk_create(softc->disk, DISK_VERSION);
 1256         mtx_lock(periph->sim->mtx);
 1257 
 1258         /*
 1259          * Add async callbacks for bus reset and
 1260          * bus device reset calls.  I don't bother
 1261          * checking if this fails as, in most cases,
 1262          * the system will function just fine without
 1263          * them and the only alternative would be to
 1264          * not attach the device on failure.
 1265          */
 1266         xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
 1267                            daasync, periph, periph->path);
 1268 
 1269         /*
 1270          * Take an exclusive refcount on the periph while dastart is called
 1271          * to finish the probe.  The reference will be dropped in dadone at
 1272          * the end of probe.
 1273          */
 1274         (void)cam_periph_hold(periph, PRIBIO);
 1275         xpt_schedule(periph, CAM_PRIORITY_DEV);
 1276 
 1277         /*
 1278          * Schedule a periodic event to occasionally send an
 1279          * ordered tag to a device.
 1280          */
 1281         callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
 1282         callout_reset(&softc->sendordered_c,
 1283             (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
 1284             dasendorderedtag, softc);
 1285 
 1286         return(CAM_REQ_CMP);
 1287 }
 1288 
 1289 static void
 1290 dastart(struct cam_periph *periph, union ccb *start_ccb)
 1291 {
 1292         struct da_softc *softc;
 1293 
 1294         softc = (struct da_softc *)periph->softc;
 1295 
 1296         switch (softc->state) {
 1297         case DA_STATE_NORMAL:
 1298         {
 1299                 /* Pull a buffer from the queue and get going on it */          
 1300                 struct bio *bp;
 1301 
 1302                 /*
 1303                  * See if there is a buf with work for us to do..
 1304                  */
 1305                 bp = bioq_first(&softc->bio_queue);
 1306                 if (periph->immediate_priority <= periph->pinfo.priority) {
 1307                         CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
 1308                                         ("queuing for immediate ccb\n"));
 1309                         start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
 1310                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
 1311                                           periph_links.sle);
 1312                         periph->immediate_priority = CAM_PRIORITY_NONE;
 1313                         wakeup(&periph->ccb_list);
 1314                 } else if (bp == NULL) {
 1315                         xpt_release_ccb(start_ccb);
 1316                 } else {
 1317                         u_int8_t tag_code;
 1318 
 1319                         bioq_remove(&softc->bio_queue, bp);
 1320 
 1321                         if ((bp->bio_flags & BIO_ORDERED) != 0
 1322                          || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
 1323                                 softc->flags &= ~DA_FLAG_NEED_OTAG;
 1324                                 softc->ordered_tag_count++;
 1325                                 tag_code = MSG_ORDERED_Q_TAG;
 1326                         } else {
 1327                                 tag_code = MSG_SIMPLE_Q_TAG;
 1328                         }
 1329                         switch (bp->bio_cmd) {
 1330                         case BIO_READ:
 1331                         case BIO_WRITE:
 1332                                 scsi_read_write(&start_ccb->csio,
 1333                                                 /*retries*/da_retry_count,
 1334                                                 /*cbfcnp*/dadone,
 1335                                                 /*tag_action*/tag_code,
 1336                                                 /*read_op*/bp->bio_cmd
 1337                                                         == BIO_READ,
 1338                                                 /*byte2*/0,
 1339                                                 softc->minimum_cmd_size,
 1340                                                 /*lba*/bp->bio_pblkno,
 1341                                                 /*block_count*/bp->bio_bcount /
 1342                                                 softc->params.secsize,
 1343                                                 /*data_ptr*/ bp->bio_data,
 1344                                                 /*dxfer_len*/ bp->bio_bcount,
 1345                                                 /*sense_len*/SSD_FULL_SIZE,
 1346                                                 da_default_timeout * 1000);
 1347                                 break;
 1348                         case BIO_FLUSH:
 1349                                 /*
 1350                                  * BIO_FLUSH doesn't currently communicate
 1351                                  * range data, so we synchronize the cache
 1352                                  * over the whole disk.  We also force
 1353                                  * ordered tag semantics the flush applies
 1354                                  * to all previously queued I/O.
 1355                                  */
 1356                                 scsi_synchronize_cache(&start_ccb->csio,
 1357                                                        /*retries*/1,
 1358                                                        /*cbfcnp*/dadone,
 1359                                                        MSG_ORDERED_Q_TAG,
 1360                                                        /*begin_lba*/0,
 1361                                                        /*lb_count*/0,
 1362                                                        SSD_FULL_SIZE,
 1363                                                        da_default_timeout*1000);
 1364                                 break;
 1365                         }
 1366                         start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
 1367 
 1368                         /*
 1369                          * Block out any asyncronous callbacks
 1370                          * while we touch the pending ccb list.
 1371                          */
 1372                         LIST_INSERT_HEAD(&softc->pending_ccbs,
 1373                                          &start_ccb->ccb_h, periph_links.le);
 1374                         softc->outstanding_cmds++;
 1375 
 1376                         /* We expect a unit attention from this device */
 1377                         if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
 1378                                 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
 1379                                 softc->flags &= ~DA_FLAG_RETRY_UA;
 1380                         }
 1381 
 1382                         start_ccb->ccb_h.ccb_bp = bp;
 1383                         bp = bioq_first(&softc->bio_queue);
 1384 
 1385                         xpt_action(start_ccb);
 1386                 }
 1387                 
 1388                 if (bp != NULL) {
 1389                         /* Have more work to do, so ensure we stay scheduled */
 1390                         xpt_schedule(periph, CAM_PRIORITY_NORMAL);
 1391                 }
 1392                 break;
 1393         }
 1394         case DA_STATE_PROBE:
 1395         {
 1396                 struct ccb_scsiio *csio;
 1397                 struct scsi_read_capacity_data *rcap;
 1398 
 1399                 rcap = (struct scsi_read_capacity_data *)
 1400                     malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
 1401                 if (rcap == NULL) {
 1402                         printf("dastart: Couldn't malloc read_capacity data\n");
 1403                         /* da_free_periph??? */
 1404                         break;
 1405                 }
 1406                 csio = &start_ccb->csio;
 1407                 scsi_read_capacity(csio,
 1408                                    /*retries*/4,
 1409                                    dadone,
 1410                                    MSG_SIMPLE_Q_TAG,
 1411                                    rcap,
 1412                                    SSD_FULL_SIZE,
 1413                                    /*timeout*/5000);
 1414                 start_ccb->ccb_h.ccb_bp = NULL;
 1415                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
 1416                 xpt_action(start_ccb);
 1417                 break;
 1418         }
 1419         case DA_STATE_PROBE2:
 1420         {
 1421                 struct ccb_scsiio *csio;
 1422                 struct scsi_read_capacity_data_long *rcaplong;
 1423 
 1424                 rcaplong = (struct scsi_read_capacity_data_long *)
 1425                         malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
 1426                 if (rcaplong == NULL) {
 1427                         printf("dastart: Couldn't malloc read_capacity data\n");
 1428                         /* da_free_periph??? */
 1429                         break;
 1430                 }
 1431                 csio = &start_ccb->csio;
 1432                 scsi_read_capacity_16(csio,
 1433                                       /*retries*/ 4,
 1434                                       /*cbfcnp*/ dadone,
 1435                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
 1436                                       /*lba*/ 0,
 1437                                       /*reladr*/ 0,
 1438                                       /*pmi*/ 0,
 1439                                       rcaplong,
 1440                                       /*sense_len*/ SSD_FULL_SIZE,
 1441                                       /*timeout*/ 60000);
 1442                 start_ccb->ccb_h.ccb_bp = NULL;
 1443                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
 1444                 xpt_action(start_ccb);  
 1445                 break;
 1446         }
 1447         }
 1448 }
 1449 
 1450 static int
 1451 cmd6workaround(union ccb *ccb)
 1452 {
 1453         struct scsi_rw_6 cmd6;
 1454         struct scsi_rw_10 *cmd10;
 1455         struct da_softc *softc;
 1456         u_int8_t *cdb;
 1457         int frozen;
 1458 
 1459         cdb = ccb->csio.cdb_io.cdb_bytes;
 1460 
 1461         /* Translation only possible if CDB is an array and cmd is R/W6 */
 1462         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
 1463             (*cdb != READ_6 && *cdb != WRITE_6))
 1464                 return 0;
 1465 
 1466         xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
 1467             "increasing minimum_cmd_size to 10.\n");
 1468         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
 1469         softc->minimum_cmd_size = 10;
 1470 
 1471         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
 1472         cmd10 = (struct scsi_rw_10 *)cdb;
 1473         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
 1474         cmd10->byte2 = 0;
 1475         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
 1476         cmd10->reserved = 0;
 1477         scsi_ulto2b(cmd6.length, cmd10->length);
 1478         cmd10->control = cmd6.control;
 1479         ccb->csio.cdb_len = sizeof(*cmd10);
 1480 
 1481         /* Requeue request, unfreezing queue if necessary */
 1482         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
 1483         ccb->ccb_h.status = CAM_REQUEUE_REQ;
 1484         xpt_action(ccb);
 1485         if (frozen) {
 1486                 cam_release_devq(ccb->ccb_h.path,
 1487                                  /*relsim_flags*/0,
 1488                                  /*reduction*/0,
 1489                                  /*timeout*/0,
 1490                                  /*getcount_only*/0);
 1491         }
 1492         return (ERESTART);
 1493 }
 1494 
 1495 static void
 1496 dadone(struct cam_periph *periph, union ccb *done_ccb)
 1497 {
 1498         struct da_softc *softc;
 1499         struct ccb_scsiio *csio;
 1500         u_int32_t  priority;
 1501 
 1502         softc = (struct da_softc *)periph->softc;
 1503         priority = done_ccb->ccb_h.pinfo.priority;
 1504         csio = &done_ccb->csio;
 1505         switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
 1506         case DA_CCB_BUFFER_IO:
 1507         {
 1508                 struct bio *bp;
 1509 
 1510                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
 1511                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 1512                         int error;
 1513                         int sf;
 1514                         
 1515                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
 1516                                 sf = SF_RETRY_UA;
 1517                         else
 1518                                 sf = 0;
 1519 
 1520                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
 1521                         if (error == ERESTART) {
 1522                                 /*
 1523                                  * A retry was scheuled, so
 1524                                  * just return.
 1525                                  */
 1526                                 return;
 1527                         }
 1528                         if (error != 0) {
 1529 
 1530                                 if (error == ENXIO) {
 1531                                         /*
 1532                                          * Catastrophic error.  Mark our pack as
 1533                                          * invalid.
 1534                                          */
 1535                                         /*
 1536                                          * XXX See if this is really a media
 1537                                          * XXX change first?
 1538                                          */
 1539                                         xpt_print(periph->path,
 1540                                             "Invalidating pack\n");
 1541                                         softc->flags |= DA_FLAG_PACK_INVALID;
 1542                                 }
 1543 
 1544                                 /*
 1545                                  * return all queued I/O with EIO, so that
 1546                                  * the client can retry these I/Os in the
 1547                                  * proper order should it attempt to recover.
 1548                                  */
 1549                                 bioq_flush(&softc->bio_queue, NULL, EIO);
 1550                                 bp->bio_error = error;
 1551                                 bp->bio_resid = bp->bio_bcount;
 1552                                 bp->bio_flags |= BIO_ERROR;
 1553                         } else {
 1554                                 bp->bio_resid = csio->resid;
 1555                                 bp->bio_error = 0;
 1556                                 if (bp->bio_resid != 0)
 1557                                         bp->bio_flags |= BIO_ERROR;
 1558                         }
 1559                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1560                                 cam_release_devq(done_ccb->ccb_h.path,
 1561                                                  /*relsim_flags*/0,
 1562                                                  /*reduction*/0,
 1563                                                  /*timeout*/0,
 1564                                                  /*getcount_only*/0);
 1565                 } else {
 1566                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1567                                 panic("REQ_CMP with QFRZN");
 1568                         bp->bio_resid = csio->resid;
 1569                         if (csio->resid > 0)
 1570                                 bp->bio_flags |= BIO_ERROR;
 1571                 }
 1572 
 1573                 /*
 1574                  * Block out any asyncronous callbacks
 1575                  * while we touch the pending ccb list.
 1576                  */
 1577                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
 1578                 softc->outstanding_cmds--;
 1579                 if (softc->outstanding_cmds == 0)
 1580                         softc->flags |= DA_FLAG_WENT_IDLE;
 1581 
 1582                 biodone(bp);
 1583                 break;
 1584         }
 1585         case DA_CCB_PROBE:
 1586         case DA_CCB_PROBE2:
 1587         {
 1588                 struct     scsi_read_capacity_data *rdcap;
 1589                 struct     scsi_read_capacity_data_long *rcaplong;
 1590                 char       announce_buf[80];
 1591 
 1592                 rdcap = NULL;
 1593                 rcaplong = NULL;
 1594                 if (softc->state == DA_STATE_PROBE)
 1595                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
 1596                 else
 1597                         rcaplong = (struct scsi_read_capacity_data_long *)
 1598                                 csio->data_ptr;
 1599 
 1600                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 1601                         struct disk_params *dp;
 1602                         uint32_t block_size;
 1603                         uint64_t maxsector;
 1604 
 1605                         if (softc->state == DA_STATE_PROBE) {
 1606                                 block_size = scsi_4btoul(rdcap->length);
 1607                                 maxsector = scsi_4btoul(rdcap->addr);
 1608 
 1609                                 /*
 1610                                  * According to SBC-2, if the standard 10
 1611                                  * byte READ CAPACITY command returns 2^32,
 1612                                  * we should issue the 16 byte version of
 1613                                  * the command, since the device in question
 1614                                  * has more sectors than can be represented
 1615                                  * with the short version of the command.
 1616                                  */
 1617                                 if (maxsector == 0xffffffff) {
 1618                                         softc->state = DA_STATE_PROBE2;
 1619                                         free(rdcap, M_SCSIDA);
 1620                                         xpt_release_ccb(done_ccb);
 1621                                         xpt_schedule(periph, priority);
 1622                                         return;
 1623                                 }
 1624                         } else {
 1625                                 block_size = scsi_4btoul(rcaplong->length);
 1626                                 maxsector = scsi_8btou64(rcaplong->addr);
 1627                         }
 1628 
 1629                         /*
 1630                          * Because GEOM code just will panic us if we
 1631                          * give them an 'illegal' value we'll avoid that
 1632                          * here.
 1633                          */
 1634                         if (block_size == 0 && maxsector == 0) {
 1635                                 snprintf(announce_buf, sizeof(announce_buf),
 1636                                         "0MB (no media?)");
 1637                         } else if (block_size >= MAXPHYS || block_size == 0) {
 1638                                 xpt_print(periph->path,
 1639                                     "unsupportable block size %ju\n",
 1640                                     (uintmax_t) block_size);
 1641                                 announce_buf[0] = '\0';
 1642                                 cam_periph_invalidate(periph);
 1643                         } else {
 1644                                 dasetgeom(periph, block_size, maxsector);
 1645                                 dp = &softc->params;
 1646                                 snprintf(announce_buf, sizeof(announce_buf),
 1647                                         "%juMB (%ju %u byte sectors: %dH %dS/T "
 1648                                         "%dC)", (uintmax_t)
 1649                                         (((uintmax_t)dp->secsize *
 1650                                         dp->sectors) / (1024*1024)),
 1651                                         (uintmax_t)dp->sectors,
 1652                                         dp->secsize, dp->heads,
 1653                                         dp->secs_per_track, dp->cylinders);
 1654                         }
 1655                 } else {
 1656                         int     error;
 1657 
 1658                         announce_buf[0] = '\0';
 1659 
 1660                         /*
 1661                          * Retry any UNIT ATTENTION type errors.  They
 1662                          * are expected at boot.
 1663                          */
 1664                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 1665                                         SF_RETRY_UA|SF_NO_PRINT);
 1666                         if (error == ERESTART) {
 1667                                 /*
 1668                                  * A retry was scheuled, so
 1669                                  * just return.
 1670                                  */
 1671                                 return;
 1672                         } else if (error != 0) {
 1673                                 struct scsi_sense_data *sense;
 1674                                 int asc, ascq;
 1675                                 int sense_key, error_code;
 1676                                 int have_sense;
 1677                                 cam_status status;
 1678                                 struct ccb_getdev cgd;
 1679 
 1680                                 /* Don't wedge this device's queue */
 1681                                 status = done_ccb->ccb_h.status;
 1682                                 if ((status & CAM_DEV_QFRZN) != 0)
 1683                                         cam_release_devq(done_ccb->ccb_h.path,
 1684                                                          /*relsim_flags*/0,
 1685                                                          /*reduction*/0,
 1686                                                          /*timeout*/0,
 1687                                                          /*getcount_only*/0);
 1688 
 1689 
 1690                                 xpt_setup_ccb(&cgd.ccb_h, 
 1691                                               done_ccb->ccb_h.path,
 1692                                               CAM_PRIORITY_NORMAL);
 1693                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
 1694                                 xpt_action((union ccb *)&cgd);
 1695 
 1696                                 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
 1697                                  || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
 1698                                  || ((status & CAM_AUTOSNS_VALID) == 0))
 1699                                         have_sense = FALSE;
 1700                                 else
 1701                                         have_sense = TRUE;
 1702 
 1703                                 if (have_sense) {
 1704                                         sense = &csio->sense_data;
 1705                                         scsi_extract_sense(sense, &error_code,
 1706                                                            &sense_key, 
 1707                                                            &asc, &ascq);
 1708                                 }
 1709                                 /*
 1710                                  * Attach to anything that claims to be a
 1711                                  * direct access or optical disk device,
 1712                                  * as long as it doesn't return a "Logical
 1713                                  * unit not supported" (0x25) error.
 1714                                  */
 1715                                 if ((have_sense) && (asc != 0x25)
 1716                                  && (error_code == SSD_CURRENT_ERROR)) {
 1717                                         const char *sense_key_desc;
 1718                                         const char *asc_desc;
 1719 
 1720                                         scsi_sense_desc(sense_key, asc, ascq,
 1721                                                         &cgd.inq_data,
 1722                                                         &sense_key_desc,
 1723                                                         &asc_desc);
 1724                                         snprintf(announce_buf,
 1725                                             sizeof(announce_buf),
 1726                                                 "Attempt to query device "
 1727                                                 "size failed: %s, %s",
 1728                                                 sense_key_desc,
 1729                                                 asc_desc);
 1730                                 } else { 
 1731                                         if (have_sense)
 1732                                                 scsi_sense_print(
 1733                                                         &done_ccb->csio);
 1734                                         else {
 1735                                                 xpt_print(periph->path,
 1736                                                     "got CAM status %#x\n",
 1737                                                     done_ccb->ccb_h.status);
 1738                                         }
 1739 
 1740                                         xpt_print(periph->path, "fatal error, "
 1741                                             "failed to attach to device\n");
 1742 
 1743                                         /*
 1744                                          * Free up resources.
 1745                                          */
 1746                                         cam_periph_invalidate(periph);
 1747                                 } 
 1748                         }
 1749                 }
 1750                 free(csio->data_ptr, M_SCSIDA);
 1751                 if (announce_buf[0] != '\0') {
 1752                         xpt_announce_periph(periph, announce_buf);
 1753                         /*
 1754                          * Create our sysctl variables, now that we know
 1755                          * we have successfully attached.
 1756                          */
 1757                         taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
 1758                 }
 1759                 softc->state = DA_STATE_NORMAL; 
 1760                 /*
 1761                  * Since our peripheral may be invalidated by an error
 1762                  * above or an external event, we must release our CCB
 1763                  * before releasing the probe lock on the peripheral.
 1764                  * The peripheral will only go away once the last lock
 1765                  * is removed, and we need it around for the CCB release
 1766                  * operation.
 1767                  */
 1768                 xpt_release_ccb(done_ccb);
 1769                 cam_periph_unhold(periph);
 1770                 return;
 1771         }
 1772         case DA_CCB_WAITING:
 1773         {
 1774                 /* Caller will release the CCB */
 1775                 wakeup(&done_ccb->ccb_h.cbfcnp);
 1776                 return;
 1777         }
 1778         case DA_CCB_DUMP:
 1779                 /* No-op.  We're polling */
 1780                 return;
 1781         default:
 1782                 break;
 1783         }
 1784         xpt_release_ccb(done_ccb);
 1785 }
 1786 
 1787 static int
 1788 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
 1789 {
 1790         struct da_softc   *softc;
 1791         struct cam_periph *periph;
 1792         int error;
 1793 
 1794         periph = xpt_path_periph(ccb->ccb_h.path);
 1795         softc = (struct da_softc *)periph->softc;
 1796 
 1797         /*
 1798          * Automatically detect devices that do not support
 1799          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
 1800          */
 1801         error = 0;
 1802         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
 1803                 error = cmd6workaround(ccb);
 1804         } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
 1805                    CAM_SCSI_STATUS_ERROR)
 1806          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
 1807          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
 1808          && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
 1809          && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
 1810                 int sense_key, error_code, asc, ascq;
 1811 
 1812                 scsi_extract_sense(&ccb->csio.sense_data,
 1813                                    &error_code, &sense_key, &asc, &ascq);
 1814                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
 1815                         error = cmd6workaround(ccb);
 1816         }
 1817         if (error == ERESTART)
 1818                 return (ERESTART);
 1819 
 1820         /*
 1821          * XXX
 1822          * Until we have a better way of doing pack validation,
 1823          * don't treat UAs as errors.
 1824          */
 1825         sense_flags |= SF_RETRY_UA;
 1826         return(cam_periph_error(ccb, cam_flags, sense_flags,
 1827                                 &softc->saved_ccb));
 1828 }
 1829 
 1830 static void
 1831 daprevent(struct cam_periph *periph, int action)
 1832 {
 1833         struct  da_softc *softc;
 1834         union   ccb *ccb;               
 1835         int     error;
 1836                 
 1837         softc = (struct da_softc *)periph->softc;
 1838 
 1839         if (((action == PR_ALLOW)
 1840           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
 1841          || ((action == PR_PREVENT)
 1842           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
 1843                 return;
 1844         }
 1845 
 1846         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
 1847 
 1848         scsi_prevent(&ccb->csio,
 1849                      /*retries*/1,
 1850                      /*cbcfp*/dadone,
 1851                      MSG_SIMPLE_Q_TAG,
 1852                      action,
 1853                      SSD_FULL_SIZE,
 1854                      5000);
 1855 
 1856         error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
 1857                                   SF_RETRY_UA, softc->disk->d_devstat);
 1858 
 1859         if (error == 0) {
 1860                 if (action == PR_ALLOW)
 1861                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
 1862                 else
 1863                         softc->flags |= DA_FLAG_PACK_LOCKED;
 1864         }
 1865 
 1866         xpt_release_ccb(ccb);
 1867 }
 1868 
 1869 static int
 1870 dagetcapacity(struct cam_periph *periph)
 1871 {
 1872         struct da_softc *softc;
 1873         union ccb *ccb;
 1874         struct scsi_read_capacity_data *rcap;
 1875         struct scsi_read_capacity_data_long *rcaplong;
 1876         uint32_t block_len;
 1877         uint64_t maxsector;
 1878         int error;
 1879         u_int32_t sense_flags;
 1880 
 1881         softc = (struct da_softc *)periph->softc;
 1882         block_len = 0;
 1883         maxsector = 0;
 1884         error = 0;
 1885         sense_flags = SF_RETRY_UA;
 1886         if (softc->flags & DA_FLAG_PACK_REMOVABLE)
 1887                 sense_flags |= SF_NO_PRINT;
 1888 
 1889         /* Do a read capacity */
 1890         rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
 1891                                                         M_SCSIDA,
 1892                                                         M_NOWAIT);
 1893         if (rcap == NULL)
 1894                 return (ENOMEM);
 1895                 
 1896         ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
 1897         scsi_read_capacity(&ccb->csio,
 1898                            /*retries*/4,
 1899                            /*cbfncp*/dadone,
 1900                            MSG_SIMPLE_Q_TAG,
 1901                            rcap,
 1902                            SSD_FULL_SIZE,
 1903                            /*timeout*/60000);
 1904         ccb->ccb_h.ccb_bp = NULL;
 1905 
 1906         error = cam_periph_runccb(ccb, daerror,
 1907                                   /*cam_flags*/CAM_RETRY_SELTO,
 1908                                   sense_flags,
 1909                                   softc->disk->d_devstat);
 1910 
 1911         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1912                 cam_release_devq(ccb->ccb_h.path,
 1913                                  /*relsim_flags*/0,
 1914                                  /*reduction*/0,
 1915                                  /*timeout*/0,
 1916                                  /*getcount_only*/0);
 1917 
 1918         if (error == 0) {
 1919                 block_len = scsi_4btoul(rcap->length);
 1920                 maxsector = scsi_4btoul(rcap->addr);
 1921 
 1922                 if (maxsector != 0xffffffff)
 1923                         goto done;
 1924         } else
 1925                 goto done;
 1926 
 1927         rcaplong = (struct scsi_read_capacity_data_long *)rcap;
 1928 
 1929         scsi_read_capacity_16(&ccb->csio,
 1930                               /*retries*/ 4,
 1931                               /*cbfcnp*/ dadone,
 1932                               /*tag_action*/ MSG_SIMPLE_Q_TAG,
 1933                               /*lba*/ 0,
 1934                               /*reladr*/ 0,
 1935                               /*pmi*/ 0,
 1936                               rcaplong,
 1937                               /*sense_len*/ SSD_FULL_SIZE,
 1938                               /*timeout*/ 60000);
 1939         ccb->ccb_h.ccb_bp = NULL;
 1940 
 1941         error = cam_periph_runccb(ccb, daerror,
 1942                                   /*cam_flags*/CAM_RETRY_SELTO,
 1943                                   sense_flags,
 1944                                   softc->disk->d_devstat);
 1945 
 1946         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1947                 cam_release_devq(ccb->ccb_h.path,
 1948                                  /*relsim_flags*/0,
 1949                                  /*reduction*/0,
 1950                                  /*timeout*/0,
 1951                                  /*getcount_only*/0);
 1952 
 1953         if (error == 0) {
 1954                 block_len = scsi_4btoul(rcaplong->length);
 1955                 maxsector = scsi_8btou64(rcaplong->addr);
 1956         }
 1957 
 1958 done:
 1959 
 1960         if (error == 0) {
 1961                 if (block_len >= MAXPHYS || block_len == 0) {
 1962                         xpt_print(periph->path,
 1963                             "unsupportable block size %ju\n",
 1964                             (uintmax_t) block_len);
 1965                         error = EINVAL;
 1966                 } else
 1967                         dasetgeom(periph, block_len, maxsector);
 1968         }
 1969 
 1970         xpt_release_ccb(ccb);
 1971 
 1972         free(rcap, M_SCSIDA);
 1973 
 1974         return (error);
 1975 }
 1976 
 1977 static void
 1978 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
 1979 {
 1980         struct ccb_calc_geometry ccg;
 1981         struct da_softc *softc;
 1982         struct disk_params *dp;
 1983 
 1984         softc = (struct da_softc *)periph->softc;
 1985 
 1986         dp = &softc->params;
 1987         dp->secsize = block_len;
 1988         dp->sectors = maxsector + 1;
 1989         /*
 1990          * Have the controller provide us with a geometry
 1991          * for this disk.  The only time the geometry
 1992          * matters is when we boot and the controller
 1993          * is the only one knowledgeable enough to come
 1994          * up with something that will make this a bootable
 1995          * device.
 1996          */
 1997         xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 1998         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
 1999         ccg.block_size = dp->secsize;
 2000         ccg.volume_size = dp->sectors;
 2001         ccg.heads = 0;
 2002         ccg.secs_per_track = 0;
 2003         ccg.cylinders = 0;
 2004         xpt_action((union ccb*)&ccg);
 2005         if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 2006                 /*
 2007                  * We don't know what went wrong here- but just pick
 2008                  * a geometry so we don't have nasty things like divide
 2009                  * by zero.
 2010                  */
 2011                 dp->heads = 255;
 2012                 dp->secs_per_track = 255;
 2013                 dp->cylinders = dp->sectors / (255 * 255);
 2014                 if (dp->cylinders == 0) {
 2015                         dp->cylinders = 1;
 2016                 }
 2017         } else {
 2018                 dp->heads = ccg.heads;
 2019                 dp->secs_per_track = ccg.secs_per_track;
 2020                 dp->cylinders = ccg.cylinders;
 2021         }
 2022 }
 2023 
 2024 static void
 2025 dasendorderedtag(void *arg)
 2026 {
 2027         struct da_softc *softc = arg;
 2028 
 2029         if (da_send_ordered) {
 2030                 if ((softc->ordered_tag_count == 0) 
 2031                  && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
 2032                         softc->flags |= DA_FLAG_NEED_OTAG;
 2033                 }
 2034                 if (softc->outstanding_cmds > 0)
 2035                         softc->flags &= ~DA_FLAG_WENT_IDLE;
 2036 
 2037                 softc->ordered_tag_count = 0;
 2038         }
 2039         /* Queue us up again */
 2040         callout_reset(&softc->sendordered_c,
 2041             (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL,
 2042             dasendorderedtag, softc);
 2043 }
 2044 
 2045 /*
 2046  * Step through all DA peripheral drivers, and if the device is still open,
 2047  * sync the disk cache to physical media.
 2048  */
 2049 static void
 2050 dashutdown(void * arg, int howto)
 2051 {
 2052         struct cam_periph *periph;
 2053         struct da_softc *softc;
 2054 
 2055         TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
 2056                 union ccb ccb;
 2057 
 2058                 cam_periph_lock(periph);
 2059                 softc = (struct da_softc *)periph->softc;
 2060 
 2061                 /*
 2062                  * We only sync the cache if the drive is still open, and
 2063                  * if the drive is capable of it..
 2064                  */
 2065                 if (((softc->flags & DA_FLAG_OPEN) == 0)
 2066                  || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
 2067                         cam_periph_unlock(periph);
 2068                         continue;
 2069                 }
 2070 
 2071                 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
 2072 
 2073                 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
 2074                 scsi_synchronize_cache(&ccb.csio,
 2075                                        /*retries*/1,
 2076                                        /*cbfcnp*/dadone,
 2077                                        MSG_SIMPLE_Q_TAG,
 2078                                        /*begin_lba*/0, /* whole disk */
 2079                                        /*lb_count*/0,
 2080                                        SSD_FULL_SIZE,
 2081                                        60 * 60 * 1000);
 2082 
 2083                 xpt_polled_action(&ccb);
 2084 
 2085                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 2086                         if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
 2087                              CAM_SCSI_STATUS_ERROR)
 2088                          && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
 2089                                 int error_code, sense_key, asc, ascq;
 2090 
 2091                                 scsi_extract_sense(&ccb.csio.sense_data,
 2092                                                    &error_code, &sense_key,
 2093                                                    &asc, &ascq);
 2094 
 2095                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
 2096                                         scsi_sense_print(&ccb.csio);
 2097                         } else {
 2098                                 xpt_print(periph->path, "Synchronize "
 2099                                     "cache failed, status == 0x%x, scsi status "
 2100                                     "== 0x%x\n", ccb.ccb_h.status,
 2101                                     ccb.csio.scsi_status);
 2102                         }
 2103                 }
 2104 
 2105                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
 2106                         cam_release_devq(ccb.ccb_h.path,
 2107                                          /*relsim_flags*/0,
 2108                                          /*reduction*/0,
 2109                                          /*timeout*/0,
 2110                                          /*getcount_only*/0);
 2111                 cam_periph_unlock(periph);
 2112         }
 2113 }
 2114 
 2115 #else /* !_KERNEL */
 2116 
 2117 /*
 2118  * XXX This is only left out of the kernel build to silence warnings.  If,
 2119  * for some reason this function is used in the kernel, the ifdefs should
 2120  * be moved so it is included both in the kernel and userland.
 2121  */
 2122 void
 2123 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
 2124                  void (*cbfcnp)(struct cam_periph *, union ccb *),
 2125                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
 2126                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
 2127                  u_int32_t timeout)
 2128 {
 2129         struct scsi_format_unit *scsi_cmd;
 2130 
 2131         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
 2132         scsi_cmd->opcode = FORMAT_UNIT;
 2133         scsi_cmd->byte2 = byte2;
 2134         scsi_ulto2b(ileave, scsi_cmd->interleave);
 2135 
 2136         cam_fill_csio(csio,
 2137                       retries,
 2138                       cbfcnp,
 2139                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
 2140                       tag_action,
 2141                       data_ptr,
 2142                       dxfer_len,
 2143                       sense_len,
 2144                       sizeof(*scsi_cmd),
 2145                       timeout);
 2146 }
 2147 
 2148 #endif /* _KERNEL */

Cache object: 7e5882064178df2422a342f39db7bf15


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