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

Cache object: b8c6c194ed75524dfa95bb40e1d7e256


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