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

Cache object: 367d572272eace6f57bc9d47d1bccbc1


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