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


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

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

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

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

Cache object: a31dd394c0f549a9e676823fa345c6bc


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