The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_da.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Implementation of SCSI Direct Access Peripheral driver for CAM.
    3  *
    4  * Copyright (c) 1997 Justin T. Gibbs.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions, and the following disclaimer,
   12  *    without modification, immediately at the beginning of the file.
   13  * 2. The name of the author may not be used to endorse or promote products
   14  *    derived from this software without specific prior written permission.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
   20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #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                 mtx_unlock(&Giant);
 1000                 return;
 1001         }
 1002 
 1003         /*
 1004          * Now register the sysctl handler, so the user can the value on
 1005          * the fly.
 1006          */
 1007         SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
 1008                 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
 1009                 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
 1010                 "Minimum CDB size");
 1011 
 1012         mtx_unlock(&Giant);
 1013 }
 1014 
 1015 static int
 1016 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
 1017 {
 1018         int error, value;
 1019 
 1020         value = *(int *)arg1;
 1021 
 1022         error = sysctl_handle_int(oidp, &value, 0, req);
 1023 
 1024         if ((error != 0)
 1025          || (req->newptr == NULL))
 1026                 return (error);
 1027 
 1028         /*
 1029          * Acceptable values here are 6, 10, 12 or 16.
 1030          */
 1031         if (value < 6)
 1032                 value = 6;
 1033         else if ((value > 6)
 1034               && (value <= 10))
 1035                 value = 10;
 1036         else if ((value > 10)
 1037               && (value <= 12))
 1038                 value = 12;
 1039         else if (value > 12)
 1040                 value = 16;
 1041 
 1042         *(int *)arg1 = value;
 1043 
 1044         return (0);
 1045 }
 1046 
 1047 static cam_status
 1048 daregister(struct cam_periph *periph, void *arg)
 1049 {
 1050         int s;
 1051         struct da_softc *softc;
 1052         struct ccb_setasync csa;
 1053         struct ccb_pathinq cpi;
 1054         struct ccb_getdev *cgd;
 1055         char tmpstr[80];
 1056         caddr_t match;
 1057 
 1058         cgd = (struct ccb_getdev *)arg;
 1059         if (periph == NULL) {
 1060                 printf("daregister: periph was NULL!!\n");
 1061                 return(CAM_REQ_CMP_ERR);
 1062         }
 1063 
 1064         if (cgd == NULL) {
 1065                 printf("daregister: no getdev CCB, can't register device\n");
 1066                 return(CAM_REQ_CMP_ERR);
 1067         }
 1068 
 1069         softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
 1070             M_NOWAIT|M_ZERO);
 1071 
 1072         if (softc == NULL) {
 1073                 printf("daregister: Unable to probe new device. "
 1074                        "Unable to allocate softc\n");                           
 1075                 return(CAM_REQ_CMP_ERR);
 1076         }
 1077 
 1078         LIST_INIT(&softc->pending_ccbs);
 1079         softc->state = DA_STATE_PROBE;
 1080         bioq_init(&softc->bio_queue);
 1081         if (SID_IS_REMOVABLE(&cgd->inq_data))
 1082                 softc->flags |= DA_FLAG_PACK_REMOVABLE;
 1083         if ((cgd->inq_data.flags & SID_CmdQue) != 0)
 1084                 softc->flags |= DA_FLAG_TAGGED_QUEUING;
 1085 
 1086         periph->softc = softc;
 1087 
 1088         /*
 1089          * See if this device has any quirks.
 1090          */
 1091         match = cam_quirkmatch((caddr_t)&cgd->inq_data,
 1092                                (caddr_t)da_quirk_table,
 1093                                sizeof(da_quirk_table)/sizeof(*da_quirk_table),
 1094                                sizeof(*da_quirk_table), scsi_inquiry_match);
 1095 
 1096         if (match != NULL)
 1097                 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
 1098         else
 1099                 softc->quirks = DA_Q_NONE;
 1100 
 1101         /* Check if the SIM does not want 6 byte commands */
 1102         xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
 1103         cpi.ccb_h.func_code = XPT_PATH_INQ;
 1104         xpt_action((union ccb *)&cpi);
 1105         if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
 1106                 softc->quirks |= DA_Q_NO_6_BYTE;
 1107 
 1108         TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
 1109 
 1110         /*
 1111          * RBC devices don't have to support READ(6), only READ(10).
 1112          */
 1113         if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
 1114                 softc->minimum_cmd_size = 10;
 1115         else
 1116                 softc->minimum_cmd_size = 6;
 1117 
 1118         /*
 1119          * Load the user's default, if any.
 1120          */
 1121         snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
 1122                  periph->unit_number);
 1123         TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
 1124 
 1125         /*
 1126          * 6, 10, 12 and 16 are the currently permissible values.
 1127          */
 1128         if (softc->minimum_cmd_size < 6)
 1129                 softc->minimum_cmd_size = 6;
 1130         else if ((softc->minimum_cmd_size > 6)
 1131               && (softc->minimum_cmd_size <= 10))
 1132                 softc->minimum_cmd_size = 10;
 1133         else if ((softc->minimum_cmd_size > 10)
 1134               && (softc->minimum_cmd_size <= 12))
 1135                 softc->minimum_cmd_size = 12;
 1136         else if (softc->minimum_cmd_size > 12)
 1137                 softc->minimum_cmd_size = 16;
 1138 
 1139         /*
 1140          * Block our timeout handler while we
 1141          * add this softc to the dev list.
 1142          */
 1143         s = splsoftclock();
 1144         SLIST_INSERT_HEAD(&softc_list, softc, links);
 1145         splx(s);
 1146 
 1147         /*
 1148          * Register this media as a disk
 1149          */
 1150 
 1151         softc->disk = disk_alloc();
 1152         softc->disk->d_open = daopen;
 1153         softc->disk->d_close = daclose;
 1154         softc->disk->d_strategy = dastrategy;
 1155         softc->disk->d_dump = dadump;
 1156         softc->disk->d_name = "da";
 1157         softc->disk->d_drv1 = periph;
 1158         softc->disk->d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
 1159         softc->disk->d_unit = periph->unit_number;
 1160         softc->disk->d_flags = DISKFLAG_NEEDSGIANT;
 1161         disk_create(softc->disk, DISK_VERSION);
 1162 
 1163         /*
 1164          * Add async callbacks for bus reset and
 1165          * bus device reset calls.  I don't bother
 1166          * checking if this fails as, in most cases,
 1167          * the system will function just fine without
 1168          * them and the only alternative would be to
 1169          * not attach the device on failure.
 1170          */
 1171         xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
 1172         csa.ccb_h.func_code = XPT_SASYNC_CB;
 1173         csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
 1174         csa.callback = daasync;
 1175         csa.callback_arg = periph;
 1176         xpt_action((union ccb *)&csa);
 1177         /*
 1178          * Lock this peripheral until we are setup.
 1179          * This first call can't block
 1180          */
 1181         (void)cam_periph_lock(periph, PRIBIO);
 1182         xpt_schedule(periph, /*priority*/5);
 1183 
 1184         return(CAM_REQ_CMP);
 1185 }
 1186 
 1187 static void
 1188 dastart(struct cam_periph *periph, union ccb *start_ccb)
 1189 {
 1190         struct da_softc *softc;
 1191 
 1192         softc = (struct da_softc *)periph->softc;
 1193 
 1194         
 1195         switch (softc->state) {
 1196         case DA_STATE_NORMAL:
 1197         {
 1198                 /* Pull a buffer from the queue and get going on it */          
 1199                 struct bio *bp;
 1200                 int s;
 1201 
 1202                 /*
 1203                  * See if there is a buf with work for us to do..
 1204                  */
 1205                 s = splbio();
 1206                 bp = bioq_first(&softc->bio_queue);
 1207                 if (periph->immediate_priority <= periph->pinfo.priority) {
 1208                         CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
 1209                                         ("queuing for immediate ccb\n"));
 1210                         start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
 1211                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
 1212                                           periph_links.sle);
 1213                         periph->immediate_priority = CAM_PRIORITY_NONE;
 1214                         splx(s);
 1215                         wakeup(&periph->ccb_list);
 1216                 } else if (bp == NULL) {
 1217                         splx(s);
 1218                         xpt_release_ccb(start_ccb);
 1219                 } else {
 1220                         int oldspl;
 1221                         u_int8_t tag_code;
 1222 
 1223                         bioq_remove(&softc->bio_queue, bp);
 1224 
 1225                         if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
 1226                                 softc->flags &= ~DA_FLAG_NEED_OTAG;
 1227                                 softc->ordered_tag_count++;
 1228                                 tag_code = MSG_ORDERED_Q_TAG;
 1229                         } else {
 1230                                 tag_code = MSG_SIMPLE_Q_TAG;
 1231                         }
 1232                         scsi_read_write(&start_ccb->csio,
 1233                                         /*retries*/da_retry_count,
 1234                                         /*cbfcnp*/dadone,
 1235                                         /*tag_action*/tag_code,
 1236                                         /*read_op*/bp->bio_cmd == BIO_READ,
 1237                                         /*byte2*/0,
 1238                                         softc->minimum_cmd_size,
 1239                                         /*lba*/bp->bio_pblkno,
 1240                                         /*block_count*/bp->bio_bcount /
 1241                                         softc->params.secsize,
 1242                                         /*data_ptr*/ bp->bio_data,
 1243                                         /*dxfer_len*/ bp->bio_bcount,
 1244                                         /*sense_len*/SSD_FULL_SIZE,
 1245                                         /*timeout*/da_default_timeout*1000);
 1246                         start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
 1247 
 1248                         /*
 1249                          * Block out any asyncronous callbacks
 1250                          * while we touch the pending ccb list.
 1251                          */
 1252                         oldspl = splcam();
 1253                         LIST_INSERT_HEAD(&softc->pending_ccbs,
 1254                                          &start_ccb->ccb_h, periph_links.le);
 1255                         softc->outstanding_cmds++;
 1256                         splx(oldspl);
 1257 
 1258                         /* We expect a unit attention from this device */
 1259                         if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
 1260                                 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
 1261                                 softc->flags &= ~DA_FLAG_RETRY_UA;
 1262                         }
 1263 
 1264                         start_ccb->ccb_h.ccb_bp = bp;
 1265                         bp = bioq_first(&softc->bio_queue);
 1266                         splx(s);
 1267 
 1268                         xpt_action(start_ccb);
 1269                 }
 1270                 
 1271                 if (bp != NULL) {
 1272                         /* Have more work to do, so ensure we stay scheduled */
 1273                         xpt_schedule(periph, /* XXX priority */1);
 1274                 }
 1275                 break;
 1276         }
 1277         case DA_STATE_PROBE:
 1278         {
 1279                 struct ccb_scsiio *csio;
 1280                 struct scsi_read_capacity_data *rcap;
 1281 
 1282                 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
 1283                                                                 M_TEMP,
 1284                                                                 M_NOWAIT);
 1285                 if (rcap == NULL) {
 1286                         printf("dastart: Couldn't malloc read_capacity data\n");
 1287                         /* da_free_periph??? */
 1288                         break;
 1289                 }
 1290                 csio = &start_ccb->csio;
 1291                 scsi_read_capacity(csio,
 1292                                    /*retries*/4,
 1293                                    dadone,
 1294                                    MSG_SIMPLE_Q_TAG,
 1295                                    rcap,
 1296                                    SSD_FULL_SIZE,
 1297                                    /*timeout*/5000);
 1298                 start_ccb->ccb_h.ccb_bp = NULL;
 1299                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
 1300                 xpt_action(start_ccb);
 1301                 break;
 1302         }
 1303         case DA_STATE_PROBE2:
 1304         {
 1305                 struct ccb_scsiio *csio;
 1306                 struct scsi_read_capacity_data_long *rcaplong;
 1307 
 1308                 rcaplong = (struct scsi_read_capacity_data_long *)
 1309                         malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT);
 1310                 if (rcaplong == NULL) {
 1311                         printf("dastart: Couldn't malloc read_capacity data\n");
 1312                         /* da_free_periph??? */
 1313                         break;
 1314                 }
 1315                 csio = &start_ccb->csio;
 1316                 scsi_read_capacity_16(csio,
 1317                                       /*retries*/ 4,
 1318                                       /*cbfcnp*/ dadone,
 1319                                       /*tag_action*/ MSG_SIMPLE_Q_TAG,
 1320                                       /*lba*/ 0,
 1321                                       /*reladr*/ 0,
 1322                                       /*pmi*/ 0,
 1323                                       rcaplong,
 1324                                       /*sense_len*/ SSD_FULL_SIZE,
 1325                                       /*timeout*/ 60000);
 1326                 start_ccb->ccb_h.ccb_bp = NULL;
 1327                 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
 1328                 xpt_action(start_ccb);  
 1329                 break;
 1330         }
 1331         }
 1332 }
 1333 
 1334 static int
 1335 cmd6workaround(union ccb *ccb)
 1336 {
 1337         struct scsi_rw_6 cmd6;
 1338         struct scsi_rw_10 *cmd10;
 1339         struct da_softc *softc;
 1340         u_int8_t *cdb;
 1341         int frozen;
 1342 
 1343         cdb = ccb->csio.cdb_io.cdb_bytes;
 1344 
 1345         /* Translation only possible if CDB is an array and cmd is R/W6 */
 1346         if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
 1347             (*cdb != READ_6 && *cdb != WRITE_6))
 1348                 return 0;
 1349 
 1350         xpt_print_path(ccb->ccb_h.path);
 1351         printf("READ(6)/WRITE(6) not supported, "
 1352                "increasing minimum_cmd_size to 10.\n");
 1353         softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
 1354         softc->minimum_cmd_size = 10;
 1355 
 1356         bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
 1357         cmd10 = (struct scsi_rw_10 *)cdb;
 1358         cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
 1359         cmd10->byte2 = 0;
 1360         scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
 1361         cmd10->reserved = 0;
 1362         scsi_ulto2b(cmd6.length, cmd10->length);
 1363         cmd10->control = cmd6.control;
 1364         ccb->csio.cdb_len = sizeof(*cmd10);
 1365 
 1366         /* Requeue request, unfreezing queue if necessary */
 1367         frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
 1368         ccb->ccb_h.status = CAM_REQUEUE_REQ;
 1369         xpt_action(ccb);
 1370         if (frozen) {
 1371                 cam_release_devq(ccb->ccb_h.path,
 1372                                  /*relsim_flags*/0,
 1373                                  /*reduction*/0,
 1374                                  /*timeout*/0,
 1375                                  /*getcount_only*/0);
 1376         }
 1377         return (ERESTART);
 1378 }
 1379 
 1380 static void
 1381 dadone(struct cam_periph *periph, union ccb *done_ccb)
 1382 {
 1383         struct da_softc *softc;
 1384         struct ccb_scsiio *csio;
 1385 
 1386         softc = (struct da_softc *)periph->softc;
 1387         csio = &done_ccb->csio;
 1388         switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
 1389         case DA_CCB_BUFFER_IO:
 1390         {
 1391                 struct bio *bp;
 1392                 int    oldspl;
 1393 
 1394                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
 1395                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 1396                         int error;
 1397                         int s;
 1398                         int sf;
 1399                         
 1400                         if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
 1401                                 sf = SF_RETRY_UA;
 1402                         else
 1403                                 sf = 0;
 1404 
 1405                         error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
 1406                         if (error == ERESTART) {
 1407                                 /*
 1408                                  * A retry was scheuled, so
 1409                                  * just return.
 1410                                  */
 1411                                 return;
 1412                         }
 1413                         if (error != 0) {
 1414 
 1415                                 s = splbio();
 1416 
 1417                                 if (error == ENXIO) {
 1418                                         /*
 1419                                          * Catastrophic error.  Mark our pack as
 1420                                          * invalid.
 1421                                          */
 1422                                         /* XXX See if this is really a media
 1423                                          *     change first.
 1424                                          */
 1425                                         xpt_print_path(periph->path);
 1426                                         printf("Invalidating pack\n");
 1427                                         softc->flags |= DA_FLAG_PACK_INVALID;
 1428                                 }
 1429 
 1430                                 /*
 1431                                  * return all queued I/O with EIO, so that
 1432                                  * the client can retry these I/Os in the
 1433                                  * proper order should it attempt to recover.
 1434                                  */
 1435                                 bioq_flush(&softc->bio_queue, NULL, EIO);
 1436                                 splx(s);
 1437                                 bp->bio_error = error;
 1438                                 bp->bio_resid = bp->bio_bcount;
 1439                                 bp->bio_flags |= BIO_ERROR;
 1440                         } else {
 1441                                 bp->bio_resid = csio->resid;
 1442                                 bp->bio_error = 0;
 1443                                 if (bp->bio_resid != 0)
 1444                                         bp->bio_flags |= BIO_ERROR;
 1445                         }
 1446                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1447                                 cam_release_devq(done_ccb->ccb_h.path,
 1448                                                  /*relsim_flags*/0,
 1449                                                  /*reduction*/0,
 1450                                                  /*timeout*/0,
 1451                                                  /*getcount_only*/0);
 1452                 } else {
 1453                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1454                                 panic("REQ_CMP with QFRZN");
 1455                         bp->bio_resid = csio->resid;
 1456                         if (csio->resid > 0)
 1457                                 bp->bio_flags |= BIO_ERROR;
 1458                 }
 1459 
 1460                 /*
 1461                  * Block out any asyncronous callbacks
 1462                  * while we touch the pending ccb list.
 1463                  */
 1464                 oldspl = splcam();
 1465                 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
 1466                 softc->outstanding_cmds--;
 1467                 if (softc->outstanding_cmds == 0)
 1468                         softc->flags |= DA_FLAG_WENT_IDLE;
 1469                 splx(oldspl);
 1470 
 1471                 biodone(bp);
 1472                 break;
 1473         }
 1474         case DA_CCB_PROBE:
 1475         case DA_CCB_PROBE2:
 1476         {
 1477                 struct     scsi_read_capacity_data *rdcap;
 1478                 struct     scsi_read_capacity_data_long *rcaplong;
 1479                 char       announce_buf[80];
 1480 
 1481                 rdcap = NULL;
 1482                 rcaplong = NULL;
 1483                 if (softc->state == DA_STATE_PROBE)
 1484                         rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
 1485                 else
 1486                         rcaplong = (struct scsi_read_capacity_data_long *)
 1487                                 csio->data_ptr;
 1488 
 1489                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
 1490                         struct disk_params *dp;
 1491                         uint32_t block_size;
 1492                         uint64_t maxsector;
 1493 
 1494                         if (softc->state == DA_STATE_PROBE) {
 1495                                 block_size = scsi_4btoul(rdcap->length);
 1496                                 maxsector = scsi_4btoul(rdcap->addr);
 1497 
 1498                                 /*
 1499                                  * According to SBC-2, if the standard 10
 1500                                  * byte READ CAPACITY command returns 2^32,
 1501                                  * we should issue the 16 byte version of
 1502                                  * the command, since the device in question
 1503                                  * has more sectors than can be represented
 1504                                  * with the short version of the command.
 1505                                  */
 1506                                 if (maxsector == 0xffffffff) {
 1507                                         softc->state = DA_STATE_PROBE2;
 1508                                         free(rdcap, M_TEMP);
 1509                                         xpt_release_ccb(done_ccb);
 1510                                         xpt_schedule(periph, /*priority*/5);
 1511                                         return;
 1512                                 }
 1513                         } else {
 1514                                 block_size = scsi_4btoul(rcaplong->length);
 1515                                 maxsector = scsi_8btou64(rcaplong->addr);
 1516                         }
 1517                         dasetgeom(periph, block_size, maxsector);
 1518                         dp = &softc->params;
 1519                         snprintf(announce_buf, sizeof(announce_buf),
 1520                                 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
 1521                                 (uintmax_t) (((uintmax_t)dp->secsize *
 1522                                 dp->sectors) / (1024*1024)),
 1523                                 (uintmax_t)dp->sectors,
 1524                                 dp->secsize, dp->heads, dp->secs_per_track,
 1525                                 dp->cylinders);
 1526                 } else {
 1527                         int     error;
 1528 
 1529                         announce_buf[0] = '\0';
 1530 
 1531                         /*
 1532                          * Retry any UNIT ATTENTION type errors.  They
 1533                          * are expected at boot.
 1534                          */
 1535                         error = daerror(done_ccb, CAM_RETRY_SELTO,
 1536                                         SF_RETRY_UA|SF_NO_PRINT);
 1537                         if (error == ERESTART) {
 1538                                 /*
 1539                                  * A retry was scheuled, so
 1540                                  * just return.
 1541                                  */
 1542                                 return;
 1543                         } else if (error != 0) {
 1544                                 struct scsi_sense_data *sense;
 1545                                 int asc, ascq;
 1546                                 int sense_key, error_code;
 1547                                 int have_sense;
 1548                                 cam_status status;
 1549                                 struct ccb_getdev cgd;
 1550 
 1551                                 /* Don't wedge this device's queue */
 1552                                 status = done_ccb->ccb_h.status;
 1553                                 if ((status & CAM_DEV_QFRZN) != 0)
 1554                                         cam_release_devq(done_ccb->ccb_h.path,
 1555                                                          /*relsim_flags*/0,
 1556                                                          /*reduction*/0,
 1557                                                          /*timeout*/0,
 1558                                                          /*getcount_only*/0);
 1559 
 1560 
 1561                                 xpt_setup_ccb(&cgd.ccb_h, 
 1562                                               done_ccb->ccb_h.path,
 1563                                               /* priority */ 1);
 1564                                 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
 1565                                 xpt_action((union ccb *)&cgd);
 1566 
 1567                                 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
 1568                                  || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
 1569                                  || ((status & CAM_AUTOSNS_VALID) == 0))
 1570                                         have_sense = FALSE;
 1571                                 else
 1572                                         have_sense = TRUE;
 1573 
 1574                                 if (have_sense) {
 1575                                         sense = &csio->sense_data;
 1576                                         scsi_extract_sense(sense, &error_code,
 1577                                                            &sense_key, 
 1578                                                            &asc, &ascq);
 1579                                 }
 1580                                 /*
 1581                                  * Attach to anything that claims to be a
 1582                                  * direct access or optical disk device,
 1583                                  * as long as it doesn't return a "Logical
 1584                                  * unit not supported" (0x25) error.
 1585                                  */
 1586                                 if ((have_sense) && (asc != 0x25)
 1587                                  && (error_code == SSD_CURRENT_ERROR)) {
 1588                                         const char *sense_key_desc;
 1589                                         const char *asc_desc;
 1590 
 1591                                         scsi_sense_desc(sense_key, asc, ascq,
 1592                                                         &cgd.inq_data,
 1593                                                         &sense_key_desc,
 1594                                                         &asc_desc);
 1595                                         snprintf(announce_buf,
 1596                                             sizeof(announce_buf),
 1597                                                 "Attempt to query device "
 1598                                                 "size failed: %s, %s",
 1599                                                 sense_key_desc,
 1600                                                 asc_desc);
 1601                                 } else { 
 1602                                         if (have_sense)
 1603                                                 scsi_sense_print(
 1604                                                         &done_ccb->csio);
 1605                                         else {
 1606                                                 xpt_print_path(periph->path);
 1607                                                 printf("got CAM status %#x\n",
 1608                                                        done_ccb->ccb_h.status);
 1609                                         }
 1610 
 1611                                         xpt_print_path(periph->path);
 1612                                         printf("fatal error, failed" 
 1613                                                " to attach to device\n");
 1614 
 1615                                         /*
 1616                                          * Free up resources.
 1617                                          */
 1618                                         cam_periph_invalidate(periph);
 1619                                 } 
 1620                         }
 1621                 }
 1622                 free(csio->data_ptr, M_TEMP);
 1623                 if (announce_buf[0] != '\0') {
 1624                         xpt_announce_periph(periph, announce_buf);
 1625                         /*
 1626                          * Create our sysctl variables, now that we know
 1627                          * we have successfully attached.
 1628                          */
 1629                         taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
 1630                 }
 1631                 softc->state = DA_STATE_NORMAL; 
 1632                 /*
 1633                  * Since our peripheral may be invalidated by an error
 1634                  * above or an external event, we must release our CCB
 1635                  * before releasing the probe lock on the peripheral.
 1636                  * The peripheral will only go away once the last lock
 1637                  * is removed, and we need it around for the CCB release
 1638                  * operation.
 1639                  */
 1640                 xpt_release_ccb(done_ccb);
 1641                 cam_periph_unlock(periph);
 1642                 return;
 1643         }
 1644         case DA_CCB_WAITING:
 1645         {
 1646                 /* Caller will release the CCB */
 1647                 wakeup(&done_ccb->ccb_h.cbfcnp);
 1648                 return;
 1649         }
 1650         case DA_CCB_DUMP:
 1651                 /* No-op.  We're polling */
 1652                 return;
 1653         default:
 1654                 break;
 1655         }
 1656         xpt_release_ccb(done_ccb);
 1657 }
 1658 
 1659 static int
 1660 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
 1661 {
 1662         struct da_softc   *softc;
 1663         struct cam_periph *periph;
 1664         int error;
 1665 
 1666         periph = xpt_path_periph(ccb->ccb_h.path);
 1667         softc = (struct da_softc *)periph->softc;
 1668 
 1669         /*
 1670          * Automatically detect devices that do not support
 1671          * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
 1672          */
 1673         error = 0;
 1674         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
 1675                 error = cmd6workaround(ccb);
 1676         } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
 1677                    CAM_SCSI_STATUS_ERROR)
 1678          && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
 1679          && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
 1680          && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
 1681          && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
 1682                 int sense_key, error_code, asc, ascq;
 1683 
 1684                 scsi_extract_sense(&ccb->csio.sense_data,
 1685                                    &error_code, &sense_key, &asc, &ascq);
 1686                 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
 1687                         error = cmd6workaround(ccb);
 1688         }
 1689         if (error == ERESTART)
 1690                 return (ERESTART);
 1691 
 1692         /*
 1693          * XXX
 1694          * Until we have a better way of doing pack validation,
 1695          * don't treat UAs as errors.
 1696          */
 1697         sense_flags |= SF_RETRY_UA;
 1698         return(cam_periph_error(ccb, cam_flags, sense_flags,
 1699                                 &softc->saved_ccb));
 1700 }
 1701 
 1702 static void
 1703 daprevent(struct cam_periph *periph, int action)
 1704 {
 1705         struct  da_softc *softc;
 1706         union   ccb *ccb;               
 1707         int     error;
 1708                 
 1709         softc = (struct da_softc *)periph->softc;
 1710 
 1711         if (((action == PR_ALLOW)
 1712           && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
 1713          || ((action == PR_PREVENT)
 1714           && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
 1715                 return;
 1716         }
 1717 
 1718         ccb = cam_periph_getccb(periph, /*priority*/1);
 1719 
 1720         scsi_prevent(&ccb->csio,
 1721                      /*retries*/1,
 1722                      /*cbcfp*/dadone,
 1723                      MSG_SIMPLE_Q_TAG,
 1724                      action,
 1725                      SSD_FULL_SIZE,
 1726                      5000);
 1727 
 1728         error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
 1729                                   SF_RETRY_UA, softc->disk->d_devstat);
 1730 
 1731         if (error == 0) {
 1732                 if (action == PR_ALLOW)
 1733                         softc->flags &= ~DA_FLAG_PACK_LOCKED;
 1734                 else
 1735                         softc->flags |= DA_FLAG_PACK_LOCKED;
 1736         }
 1737 
 1738         xpt_release_ccb(ccb);
 1739 }
 1740 
 1741 static int
 1742 dagetcapacity(struct cam_periph *periph)
 1743 {
 1744         struct da_softc *softc;
 1745         union ccb *ccb;
 1746         struct scsi_read_capacity_data *rcap;
 1747         struct scsi_read_capacity_data_long *rcaplong;
 1748         uint32_t block_len;
 1749         uint64_t maxsector;
 1750         int error;
 1751 
 1752         softc = (struct da_softc *)periph->softc;
 1753         block_len = 0;
 1754         maxsector = 0;
 1755         error = 0;
 1756 
 1757         /* Do a read capacity */
 1758         rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
 1759                                                         M_TEMP,
 1760                                                         M_WAITOK);
 1761                 
 1762         ccb = cam_periph_getccb(periph, /*priority*/1);
 1763         scsi_read_capacity(&ccb->csio,
 1764                            /*retries*/4,
 1765                            /*cbfncp*/dadone,
 1766                            MSG_SIMPLE_Q_TAG,
 1767                            rcap,
 1768                            SSD_FULL_SIZE,
 1769                            /*timeout*/60000);
 1770         ccb->ccb_h.ccb_bp = NULL;
 1771 
 1772         error = cam_periph_runccb(ccb, daerror,
 1773                                   /*cam_flags*/CAM_RETRY_SELTO,
 1774                                   /*sense_flags*/SF_RETRY_UA,
 1775                                   softc->disk->d_devstat);
 1776 
 1777         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1778                 cam_release_devq(ccb->ccb_h.path,
 1779                                  /*relsim_flags*/0,
 1780                                  /*reduction*/0,
 1781                                  /*timeout*/0,
 1782                                  /*getcount_only*/0);
 1783 
 1784         if (error == 0) {
 1785                 block_len = scsi_4btoul(rcap->length);
 1786                 maxsector = scsi_4btoul(rcap->addr);
 1787 
 1788                 if (maxsector != 0xffffffff)
 1789                         goto done;
 1790         } else
 1791                 goto done;
 1792 
 1793         rcaplong = (struct scsi_read_capacity_data_long *)rcap;
 1794 
 1795         scsi_read_capacity_16(&ccb->csio,
 1796                               /*retries*/ 4,
 1797                               /*cbfcnp*/ dadone,
 1798                               /*tag_action*/ MSG_SIMPLE_Q_TAG,
 1799                               /*lba*/ 0,
 1800                               /*reladr*/ 0,
 1801                               /*pmi*/ 0,
 1802                               rcaplong,
 1803                               /*sense_len*/ SSD_FULL_SIZE,
 1804                               /*timeout*/ 60000);
 1805         ccb->ccb_h.ccb_bp = NULL;
 1806 
 1807         error = cam_periph_runccb(ccb, daerror,
 1808                                   /*cam_flags*/CAM_RETRY_SELTO,
 1809                                   /*sense_flags*/SF_RETRY_UA,
 1810                                   softc->disk->d_devstat);
 1811 
 1812         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
 1813                 cam_release_devq(ccb->ccb_h.path,
 1814                                  /*relsim_flags*/0,
 1815                                  /*reduction*/0,
 1816                                  /*timeout*/0,
 1817                                  /*getcount_only*/0);
 1818 
 1819         if (error == 0) {
 1820                 block_len = scsi_4btoul(rcaplong->length);
 1821                 maxsector = scsi_8btou64(rcaplong->addr);
 1822         }
 1823 
 1824 done:
 1825 
 1826         if (error == 0)
 1827                 dasetgeom(periph, block_len, maxsector);
 1828 
 1829         xpt_release_ccb(ccb);
 1830 
 1831         free(rcap, M_TEMP);
 1832 
 1833         return (error);
 1834 }
 1835 
 1836 static void
 1837 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
 1838 {
 1839         struct ccb_calc_geometry ccg;
 1840         struct da_softc *softc;
 1841         struct disk_params *dp;
 1842 
 1843         softc = (struct da_softc *)periph->softc;
 1844 
 1845         dp = &softc->params;
 1846         dp->secsize = block_len;
 1847         dp->sectors = maxsector + 1;
 1848         /*
 1849          * Have the controller provide us with a geometry
 1850          * for this disk.  The only time the geometry
 1851          * matters is when we boot and the controller
 1852          * is the only one knowledgeable enough to come
 1853          * up with something that will make this a bootable
 1854          * device.
 1855          */
 1856         xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
 1857         ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
 1858         ccg.block_size = dp->secsize;
 1859         ccg.volume_size = dp->sectors;
 1860         ccg.heads = 0;
 1861         ccg.secs_per_track = 0;
 1862         ccg.cylinders = 0;
 1863         xpt_action((union ccb*)&ccg);
 1864         dp->heads = ccg.heads;
 1865         dp->secs_per_track = ccg.secs_per_track;
 1866         dp->cylinders = ccg.cylinders;
 1867 }
 1868 
 1869 static void
 1870 dasendorderedtag(void *arg)
 1871 {
 1872         struct da_softc *softc;
 1873         int s;
 1874 
 1875         for (softc = SLIST_FIRST(&softc_list);
 1876              softc != NULL;
 1877              softc = SLIST_NEXT(softc, links)) {
 1878                 s = splsoftcam();
 1879                 if ((softc->ordered_tag_count == 0) 
 1880                  && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
 1881                         softc->flags |= DA_FLAG_NEED_OTAG;
 1882                 }
 1883                 if (softc->outstanding_cmds > 0)
 1884                         softc->flags &= ~DA_FLAG_WENT_IDLE;
 1885 
 1886                 softc->ordered_tag_count = 0;
 1887                 splx(s);
 1888         }
 1889         /* Queue us up again */
 1890         timeout(dasendorderedtag, NULL,
 1891                 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
 1892 }
 1893 
 1894 /*
 1895  * Step through all DA peripheral drivers, and if the device is still open,
 1896  * sync the disk cache to physical media.
 1897  */
 1898 static void
 1899 dashutdown(void * arg, int howto)
 1900 {
 1901         struct cam_periph *periph;
 1902         struct da_softc *softc;
 1903 
 1904         TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
 1905                 union ccb ccb;
 1906                 softc = (struct da_softc *)periph->softc;
 1907 
 1908                 /*
 1909                  * We only sync the cache if the drive is still open, and
 1910                  * if the drive is capable of it..
 1911                  */
 1912                 if (((softc->flags & DA_FLAG_OPEN) == 0)
 1913                  || (softc->quirks & DA_Q_NO_SYNC_CACHE))
 1914                         continue;
 1915 
 1916                 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
 1917 
 1918                 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
 1919                 scsi_synchronize_cache(&ccb.csio,
 1920                                        /*retries*/1,
 1921                                        /*cbfcnp*/dadone,
 1922                                        MSG_SIMPLE_Q_TAG,
 1923                                        /*begin_lba*/0, /* whole disk */
 1924                                        /*lb_count*/0,
 1925                                        SSD_FULL_SIZE,
 1926                                        60 * 60 * 1000);
 1927 
 1928                 xpt_polled_action(&ccb);
 1929 
 1930                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
 1931                         if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
 1932                              CAM_SCSI_STATUS_ERROR)
 1933                          && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
 1934                                 int error_code, sense_key, asc, ascq;
 1935 
 1936                                 scsi_extract_sense(&ccb.csio.sense_data,
 1937                                                    &error_code, &sense_key,
 1938                                                    &asc, &ascq);
 1939 
 1940                                 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
 1941                                         scsi_sense_print(&ccb.csio);
 1942                         } else {
 1943                                 xpt_print_path(periph->path);
 1944                                 printf("Synchronize cache failed, status "
 1945                                        "== 0x%x, scsi status == 0x%x\n",
 1946                                        ccb.ccb_h.status, ccb.csio.scsi_status);
 1947                         }
 1948                 }
 1949 
 1950                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
 1951                         cam_release_devq(ccb.ccb_h.path,
 1952                                          /*relsim_flags*/0,
 1953                                          /*reduction*/0,
 1954                                          /*timeout*/0,
 1955                                          /*getcount_only*/0);
 1956 
 1957         }
 1958 }
 1959 
 1960 #else /* !_KERNEL */
 1961 
 1962 /*
 1963  * XXX This is only left out of the kernel build to silence warnings.  If,
 1964  * for some reason this function is used in the kernel, the ifdefs should
 1965  * be moved so it is included both in the kernel and userland.
 1966  */
 1967 void
 1968 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
 1969                  void (*cbfcnp)(struct cam_periph *, union ccb *),
 1970                  u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
 1971                  u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
 1972                  u_int32_t timeout)
 1973 {
 1974         struct scsi_format_unit *scsi_cmd;
 1975 
 1976         scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
 1977         scsi_cmd->opcode = FORMAT_UNIT;
 1978         scsi_cmd->byte2 = byte2;
 1979         scsi_ulto2b(ileave, scsi_cmd->interleave);
 1980 
 1981         cam_fill_csio(csio,
 1982                       retries,
 1983                       cbfcnp,
 1984                       /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
 1985                       tag_action,
 1986                       data_ptr,
 1987                       dxfer_len,
 1988                       sense_len,
 1989                       sizeof(*scsi_cmd),
 1990                       timeout);
 1991 }
 1992 
 1993 #endif /* _KERNEL */

Cache object: 92af077757eed2b9837c5378f27d1f5c


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