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

Cache object: 72c5b3c65403b980754fd33dd18cd884


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