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/dev/scsipi/sd.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 /*      $NetBSD: sd.c,v 1.237.2.3 2005/12/29 15:21:00 riz Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Charles M. Hannum.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. All advertising materials mentioning features or use of this software
   19  *    must display the following acknowledgement:
   20  *        This product includes software developed by the NetBSD
   21  *        Foundation, Inc. and its contributors.
   22  * 4. Neither the name of The NetBSD Foundation nor the names of its
   23  *    contributors may be used to endorse or promote products derived
   24  *    from this software without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   36  * POSSIBILITY OF SUCH DAMAGE.
   37  */
   38 
   39 /*
   40  * Originally written by Julian Elischer (julian@dialix.oz.au)
   41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
   42  *
   43  * TRW Financial Systems, in accordance with their agreement with Carnegie
   44  * Mellon University, makes this software available to CMU to distribute
   45  * or use in any manner that they see fit as long as this message is kept with
   46  * the software. For this reason TFS also grants any other persons or
   47  * organisations permission to use or modify this software.
   48  *
   49  * TFS supplies this software to be publicly redistributed
   50  * on the understanding that TFS is not responsible for the correct
   51  * functioning of this software in any circumstances.
   52  *
   53  * Ported to run under 386BSD by Julian Elischer (julian@dialix.oz.au) Sept 1992
   54  */
   55 
   56 #include <sys/cdefs.h>
   57 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.237.2.3 2005/12/29 15:21:00 riz Exp $");
   58 
   59 #include "opt_scsi.h"
   60 #include "rnd.h"
   61 
   62 #include <sys/param.h>
   63 #include <sys/systm.h>
   64 #include <sys/kernel.h>
   65 #include <sys/file.h>
   66 #include <sys/stat.h>
   67 #include <sys/ioctl.h>
   68 #include <sys/scsiio.h>
   69 #include <sys/buf.h>
   70 #include <sys/bufq.h>
   71 #include <sys/uio.h>
   72 #include <sys/malloc.h>
   73 #include <sys/errno.h>
   74 #include <sys/device.h>
   75 #include <sys/disklabel.h>
   76 #include <sys/disk.h>
   77 #include <sys/proc.h>
   78 #include <sys/conf.h>
   79 #include <sys/vnode.h>
   80 #if NRND > 0
   81 #include <sys/rnd.h>
   82 #endif
   83 
   84 #include <dev/scsipi/scsi_spc.h>
   85 #include <dev/scsipi/scsipi_all.h>
   86 #include <dev/scsipi/scsi_all.h>
   87 #include <dev/scsipi/scsipi_disk.h>
   88 #include <dev/scsipi/scsi_disk.h>
   89 #include <dev/scsipi/scsiconf.h>
   90 #include <dev/scsipi/scsipi_base.h>
   91 #include <dev/scsipi/sdvar.h>
   92 
   93 #define SDUNIT(dev)                     DISKUNIT(dev)
   94 #define SDPART(dev)                     DISKPART(dev)
   95 #define SDMINOR(unit, part)             DISKMINOR(unit, part)
   96 #define MAKESDDEV(maj, unit, part)      MAKEDISKDEV(maj, unit, part)
   97 
   98 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
   99 
  100 static void     sdminphys(struct buf *);
  101 static void     sdgetdefaultlabel(struct sd_softc *, struct disklabel *);
  102 static void     sdgetdisklabel(struct sd_softc *);
  103 static void     sdstart(struct scsipi_periph *);
  104 static void     sdrestart(void *);
  105 static void     sddone(struct scsipi_xfer *, int);
  106 static void     sd_shutdown(void *);
  107 static int      sd_interpret_sense(struct scsipi_xfer *);
  108 
  109 static int      sd_mode_sense(struct sd_softc *, u_int8_t, void *, size_t, int,
  110                     int, int *);
  111 static int      sd_mode_select(struct sd_softc *, u_int8_t, void *, size_t, int,
  112                     int);
  113 static int      sd_get_simplifiedparms(struct sd_softc *, struct disk_parms *,
  114                     int);
  115 static int      sd_get_capacity(struct sd_softc *, struct disk_parms *, int);
  116 static int      sd_get_parms(struct sd_softc *, struct disk_parms *, int);
  117 static int      sd_get_parms_page4(struct sd_softc *, struct disk_parms *,
  118                     int);
  119 static int      sd_get_parms_page5(struct sd_softc *, struct disk_parms *,
  120                     int);
  121 
  122 static int      sd_flush(struct sd_softc *, int);
  123 static int      sd_getcache(struct sd_softc *, int *);
  124 static int      sd_setcache(struct sd_softc *, int);
  125 
  126 static int      sdmatch(struct device *, struct cfdata *, void *);
  127 static void     sdattach(struct device *, struct device *, void *);
  128 static int      sdactivate(struct device *, enum devact);
  129 static int      sddetach(struct device *, int);
  130 
  131 CFATTACH_DECL(sd, sizeof(struct sd_softc), sdmatch, sdattach, sddetach,
  132     sdactivate);
  133 
  134 extern struct cfdriver sd_cd;
  135 
  136 static const struct scsipi_inquiry_pattern sd_patterns[] = {
  137         {T_DIRECT, T_FIXED,
  138          "",         "",                 ""},
  139         {T_DIRECT, T_REMOV,
  140          "",         "",                 ""},
  141         {T_OPTICAL, T_FIXED,
  142          "",         "",                 ""},
  143         {T_OPTICAL, T_REMOV,
  144          "",         "",                 ""},
  145         {T_SIMPLE_DIRECT, T_FIXED,
  146          "",         "",                 ""},
  147         {T_SIMPLE_DIRECT, T_REMOV,
  148          "",         "",                 ""},
  149 };
  150 
  151 static dev_type_open(sdopen);
  152 static dev_type_close(sdclose);
  153 static dev_type_read(sdread);
  154 static dev_type_write(sdwrite);
  155 static dev_type_ioctl(sdioctl);
  156 static dev_type_strategy(sdstrategy);
  157 static dev_type_dump(sddump);
  158 static dev_type_size(sdsize);
  159 
  160 const struct bdevsw sd_bdevsw = {
  161         sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK
  162 };
  163 
  164 const struct cdevsw sd_cdevsw = {
  165         sdopen, sdclose, sdread, sdwrite, sdioctl,
  166         nostop, notty, nopoll, nommap, nokqfilter, D_DISK
  167 };
  168 
  169 static struct dkdriver sddkdriver = { sdstrategy, sdminphys };
  170 
  171 static const struct scsipi_periphsw sd_switch = {
  172         sd_interpret_sense,     /* check our error handler first */
  173         sdstart,                /* have a queue, served by this */
  174         NULL,                   /* have no async handler */
  175         sddone,                 /* deal with stats at interrupt time */
  176 };
  177 
  178 struct sd_mode_sense_data {
  179         /*
  180          * XXX
  181          * We are not going to parse this as-is -- it just has to be large
  182          * enough.
  183          */
  184         union {
  185                 struct scsi_mode_parameter_header_6 small;
  186                 struct scsi_mode_parameter_header_10 big;
  187         } header;
  188         struct scsi_general_block_descriptor blk_desc;
  189         union scsi_disk_pages pages;
  190 };
  191 
  192 /*
  193  * The routine called by the low level scsi routine when it discovers
  194  * A device suitable for this driver
  195  */
  196 static int
  197 sdmatch(struct device *parent, struct cfdata *match, void *aux)
  198 {
  199         struct scsipibus_attach_args *sa = aux;
  200         int priority;
  201 
  202         (void)scsipi_inqmatch(&sa->sa_inqbuf,
  203             (caddr_t)sd_patterns, sizeof(sd_patterns) / sizeof(sd_patterns[0]),
  204             sizeof(sd_patterns[0]), &priority);
  205 
  206         return (priority);
  207 }
  208 
  209 /*
  210  * Attach routine common to atapi & scsi.
  211  */
  212 static void
  213 sdattach(struct device *parent, struct device *self, void *aux)
  214 {
  215         struct sd_softc *sd = (void *)self;
  216         struct scsipibus_attach_args *sa = aux;
  217         struct scsipi_periph *periph = sa->sa_periph;
  218         int error, result;
  219         struct disk_parms *dp = &sd->params;
  220         char pbuf[9];
  221 
  222         SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
  223 
  224         sd->type = (sa->sa_inqbuf.type & SID_TYPE);
  225         if (sd->type == T_SIMPLE_DIRECT)
  226                 periph->periph_quirks |= PQUIRK_ONLYBIG | PQUIRK_NOBIGMODESENSE;
  227 
  228         if (scsipi_periph_bustype(sa->sa_periph) == SCSIPI_BUSTYPE_SCSI &&
  229             periph->periph_version == 0)
  230                 sd->flags |= SDF_ANCIENT;
  231 
  232         bufq_alloc(&sd->buf_queue,
  233             BUFQ_DISK_DEFAULT_STRAT()|BUFQ_SORT_RAWBLOCK);
  234 
  235         callout_init(&sd->sc_callout);
  236 
  237         /*
  238          * Store information needed to contact our base driver
  239          */
  240         sd->sc_periph = periph;
  241 
  242         periph->periph_dev = &sd->sc_dev;
  243         periph->periph_switch = &sd_switch;
  244 
  245         /*
  246          * Increase our openings to the maximum-per-periph
  247          * supported by the adapter.  This will either be
  248          * clamped down or grown by the adapter if necessary.
  249          */
  250         periph->periph_openings =
  251             SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
  252         periph->periph_flags |= PERIPH_GROW_OPENINGS;
  253 
  254         /*
  255          * Initialize and attach the disk structure.
  256          */
  257         sd->sc_dk.dk_driver = &sddkdriver;
  258         sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
  259         disk_attach(&sd->sc_dk);
  260 
  261         /*
  262          * Use the subdriver to request information regarding the drive.
  263          */
  264         aprint_naive("\n");
  265         aprint_normal("\n");
  266 
  267         error = scsipi_test_unit_ready(periph,
  268             XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
  269             XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
  270 
  271         if (error)
  272                 result = SDGP_RESULT_OFFLINE;
  273         else
  274                 result = sd_get_parms(sd, &sd->params, XS_CTL_DISCOVERY);
  275         aprint_normal("%s: ", sd->sc_dev.dv_xname);
  276         switch (result) {
  277         case SDGP_RESULT_OK:
  278                 format_bytes(pbuf, sizeof(pbuf),
  279                     (u_int64_t)dp->disksize * dp->blksize);
  280                 aprint_normal(
  281                 "%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %llu sectors",
  282                     pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
  283                     (unsigned long long)dp->disksize);
  284                 break;
  285 
  286         case SDGP_RESULT_OFFLINE:
  287                 aprint_normal("drive offline");
  288                 break;
  289 
  290         case SDGP_RESULT_UNFORMATTED:
  291                 aprint_normal("unformatted media");
  292                 break;
  293 
  294 #ifdef DIAGNOSTIC
  295         default:
  296                 panic("sdattach: unknown result from get_parms");
  297                 break;
  298 #endif
  299         }
  300         aprint_normal("\n");
  301 
  302         /*
  303          * Establish a shutdown hook so that we can ensure that
  304          * our data has actually made it onto the platter at
  305          * shutdown time.  Note that this relies on the fact
  306          * that the shutdown hook code puts us at the head of
  307          * the list (thus guaranteeing that our hook runs before
  308          * our ancestors').
  309          */
  310         if ((sd->sc_sdhook =
  311             shutdownhook_establish(sd_shutdown, sd)) == NULL)
  312                 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
  313                     sd->sc_dev.dv_xname);
  314 
  315 #if NRND > 0
  316         /*
  317          * attach the device into the random source list
  318          */
  319         rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname,
  320                           RND_TYPE_DISK, 0);
  321 #endif
  322 
  323         /* Discover wedges on this disk. */
  324         dkwedge_discover(&sd->sc_dk);
  325 }
  326 
  327 static int
  328 sdactivate(struct device *self, enum devact act)
  329 {
  330         int rv = 0;
  331 
  332         switch (act) {
  333         case DVACT_ACTIVATE:
  334                 rv = EOPNOTSUPP;
  335                 break;
  336 
  337         case DVACT_DEACTIVATE:
  338                 /*
  339                  * Nothing to do; we key off the device's DVF_ACTIVE.
  340                  */
  341                 break;
  342         }
  343         return (rv);
  344 }
  345 
  346 static int
  347 sddetach(struct device *self, int flags)
  348 {
  349         struct sd_softc *sd = (struct sd_softc *) self;
  350         int s, bmaj, cmaj, i, mn;
  351 
  352         /* locate the major number */
  353         bmaj = bdevsw_lookup_major(&sd_bdevsw);
  354         cmaj = cdevsw_lookup_major(&sd_cdevsw);
  355 
  356         /* Nuke the vnodes for any open instances */
  357         for (i = 0; i < MAXPARTITIONS; i++) {
  358                 mn = SDMINOR(self->dv_unit, i);
  359                 vdevgone(bmaj, mn, mn, VBLK);
  360                 vdevgone(cmaj, mn, mn, VCHR);
  361         }
  362 
  363         /* kill any pending restart */
  364         callout_stop(&sd->sc_callout);
  365 
  366         /* Delete all of our wedges. */
  367         dkwedge_delall(&sd->sc_dk);
  368 
  369         s = splbio();
  370 
  371         /* Kill off any queued buffers. */
  372         bufq_drain(&sd->buf_queue);
  373 
  374         bufq_free(&sd->buf_queue);
  375 
  376         /* Kill off any pending commands. */
  377         scsipi_kill_pending(sd->sc_periph);
  378 
  379         splx(s);
  380 
  381         /* Detach from the disk list. */
  382         disk_detach(&sd->sc_dk);
  383 
  384         /* Get rid of the shutdown hook. */
  385         shutdownhook_disestablish(sd->sc_sdhook);
  386 
  387 #if NRND > 0
  388         /* Unhook the entropy source. */
  389         rnd_detach_source(&sd->rnd_source);
  390 #endif
  391 
  392         return (0);
  393 }
  394 
  395 /*
  396  * open the device. Make sure the partition info is a up-to-date as can be.
  397  */
  398 static int
  399 sdopen(dev_t dev, int flag, int fmt, struct proc *p)
  400 {
  401         struct sd_softc *sd;
  402         struct scsipi_periph *periph;
  403         struct scsipi_adapter *adapt;
  404         int unit, part;
  405         int error;
  406 
  407         unit = SDUNIT(dev);
  408         if (unit >= sd_cd.cd_ndevs)
  409                 return (ENXIO);
  410         sd = sd_cd.cd_devs[unit];
  411         if (sd == NULL)
  412                 return (ENXIO);
  413 
  414         if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
  415                 return (ENODEV);
  416 
  417         part = SDPART(dev);
  418 
  419         if ((error = lockmgr(&sd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
  420                 return (error);
  421 
  422         /*
  423          * If there are wedges, and this is not RAW_PART, then we
  424          * need to fail.
  425          */
  426         if (sd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
  427                 error = EBUSY;
  428                 goto bad1;
  429         }
  430 
  431         periph = sd->sc_periph;
  432         adapt = periph->periph_channel->chan_adapter;
  433 
  434         SC_DEBUG(periph, SCSIPI_DB1,
  435             ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
  436             sd_cd.cd_ndevs, part));
  437 
  438         /*
  439          * If this is the first open of this device, add a reference
  440          * to the adapter.
  441          */
  442         if (sd->sc_dk.dk_openmask == 0 &&
  443             (error = scsipi_adapter_addref(adapt)) != 0)
  444                 goto bad1;
  445 
  446         if ((periph->periph_flags & PERIPH_OPEN) != 0) {
  447                 /*
  448                  * If any partition is open, but the disk has been invalidated,
  449                  * disallow further opens of non-raw partition
  450                  */
  451                 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
  452                     (part != RAW_PART || fmt != S_IFCHR)) {
  453                         error = EIO;
  454                         goto bad2;
  455                 }
  456         } else {
  457                 int silent;
  458 
  459                 if (part == RAW_PART && fmt == S_IFCHR)
  460                         silent = XS_CTL_SILENT;
  461                 else
  462                         silent = 0;
  463 
  464                 /* Check that it is still responding and ok. */
  465                 error = scsipi_test_unit_ready(periph,
  466                     XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
  467                     silent);
  468 
  469                 /*
  470                  * Start the pack spinning if necessary. Always allow the
  471                  * raw parition to be opened, for raw IOCTLs. Data transfers
  472                  * will check for SDEV_MEDIA_LOADED.
  473                  */
  474                 if (error == EIO) {
  475                         int error2;
  476 
  477                         error2 = scsipi_start(periph, SSS_START, silent);
  478                         switch (error2) {
  479                         case 0:
  480                                 error = 0;
  481                                 break;
  482                         case EIO:
  483                         case EINVAL:
  484                                 break;
  485                         default:
  486                                 error = error2;
  487                                 break;
  488                         }
  489                 }
  490                 if (error) {
  491                         if (silent)
  492                                 goto out;
  493                         goto bad2;
  494                 }
  495 
  496                 periph->periph_flags |= PERIPH_OPEN;
  497 
  498                 if (periph->periph_flags & PERIPH_REMOVABLE) {
  499                         /* Lock the pack in. */
  500                         error = scsipi_prevent(periph, SPAMR_PREVENT_DT,
  501                             XS_CTL_IGNORE_ILLEGAL_REQUEST |
  502                             XS_CTL_IGNORE_MEDIA_CHANGE);
  503                         if (error)
  504                                 goto bad3;
  505                 }
  506 
  507                 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
  508                         int param_error;
  509                         periph->periph_flags |= PERIPH_MEDIA_LOADED;
  510 
  511                         /*
  512                          * Load the physical device parameters.
  513                          *
  514                          * Note that if media is present but unformatted,
  515                          * we allow the open (so that it can be formatted!).
  516                          * The drive should refuse real I/O, if the media is
  517                          * unformatted.
  518                          */
  519                         if ((param_error = sd_get_parms(sd, &sd->params, 0))
  520                              == SDGP_RESULT_OFFLINE) {
  521                                 error = ENXIO;
  522                                 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
  523                                 goto bad3;
  524                         }
  525                         SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
  526 
  527                         /* Load the partition info if not already loaded. */
  528                         if (param_error == 0) {
  529                                 sdgetdisklabel(sd);
  530                                 SC_DEBUG(periph, SCSIPI_DB3,
  531                                      ("Disklabel loaded "));
  532                         }
  533                 }
  534         }
  535 
  536         /* Check that the partition exists. */
  537         if (part != RAW_PART &&
  538             (part >= sd->sc_dk.dk_label->d_npartitions ||
  539              sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
  540                 error = ENXIO;
  541                 goto bad3;
  542         }
  543 
  544  out:   /* Insure only one open at a time. */
  545         switch (fmt) {
  546         case S_IFCHR:
  547                 sd->sc_dk.dk_copenmask |= (1 << part);
  548                 break;
  549         case S_IFBLK:
  550                 sd->sc_dk.dk_bopenmask |= (1 << part);
  551                 break;
  552         }
  553         sd->sc_dk.dk_openmask =
  554             sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
  555 
  556         SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
  557         (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL);
  558         return (0);
  559 
  560  bad3:
  561         if (sd->sc_dk.dk_openmask == 0) {
  562                 if (periph->periph_flags & PERIPH_REMOVABLE)
  563                         scsipi_prevent(periph, SPAMR_ALLOW,
  564                             XS_CTL_IGNORE_ILLEGAL_REQUEST |
  565                             XS_CTL_IGNORE_MEDIA_CHANGE);
  566                 periph->periph_flags &= ~PERIPH_OPEN;
  567         }
  568 
  569  bad2:
  570         if (sd->sc_dk.dk_openmask == 0)
  571                 scsipi_adapter_delref(adapt);
  572 
  573  bad1:
  574         (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL);
  575         return (error);
  576 }
  577 
  578 /*
  579  * close the device.. only called if we are the LAST occurence of an open
  580  * device.  Convenient now but usually a pain.
  581  */
  582 static int
  583 sdclose(dev_t dev, int flag, int fmt, struct proc *p)
  584 {
  585         struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
  586         struct scsipi_periph *periph = sd->sc_periph;
  587         struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
  588         int part = SDPART(dev);
  589         int error;
  590 
  591         if ((error = lockmgr(&sd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
  592                 return (error);
  593 
  594         switch (fmt) {
  595         case S_IFCHR:
  596                 sd->sc_dk.dk_copenmask &= ~(1 << part);
  597                 break;
  598         case S_IFBLK:
  599                 sd->sc_dk.dk_bopenmask &= ~(1 << part);
  600                 break;
  601         }
  602         sd->sc_dk.dk_openmask =
  603             sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
  604 
  605         if (sd->sc_dk.dk_openmask == 0) {
  606                 /*
  607                  * If the disk cache needs flushing, and the disk supports
  608                  * it, do it now.
  609                  */
  610                 if ((sd->flags & SDF_DIRTY) != 0) {
  611                         if (sd_flush(sd, 0)) {
  612                                 printf("%s: cache synchronization failed\n",
  613                                     sd->sc_dev.dv_xname);
  614                                 sd->flags &= ~SDF_FLUSHING;
  615                         } else
  616                                 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
  617                 }
  618 
  619                 if (! (periph->periph_flags & PERIPH_KEEP_LABEL))
  620                         periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
  621 
  622                 scsipi_wait_drain(periph);
  623 
  624                 if (periph->periph_flags & PERIPH_REMOVABLE)
  625                         scsipi_prevent(periph, SPAMR_ALLOW,
  626                             XS_CTL_IGNORE_ILLEGAL_REQUEST |
  627                             XS_CTL_IGNORE_NOT_READY);
  628                 periph->periph_flags &= ~PERIPH_OPEN;
  629 
  630                 scsipi_wait_drain(periph);
  631 
  632                 scsipi_adapter_delref(adapt);
  633         }
  634 
  635         (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL);
  636         return (0);
  637 }
  638 
  639 /*
  640  * Actually translate the requested transfer into one the physical driver
  641  * can understand.  The transfer is described by a buf and will include
  642  * only one physical transfer.
  643  */
  644 static void
  645 sdstrategy(struct buf *bp)
  646 {
  647         struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
  648         struct scsipi_periph *periph = sd->sc_periph;
  649         struct disklabel *lp;
  650         daddr_t blkno;
  651         int s;
  652         boolean_t sector_aligned;
  653 
  654         SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
  655         SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
  656             ("%d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
  657         /*
  658          * If the device has been made invalid, error out
  659          */
  660         if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
  661             (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
  662                 if (periph->periph_flags & PERIPH_OPEN)
  663                         bp->b_error = EIO;
  664                 else
  665                         bp->b_error = ENODEV;
  666                 goto bad;
  667         }
  668 
  669         lp = sd->sc_dk.dk_label;
  670 
  671         /*
  672          * The transfer must be a whole number of blocks, offset must not be
  673          * negative.
  674          */
  675         if (lp->d_secsize == DEV_BSIZE) {
  676                 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
  677         } else {
  678                 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
  679         }
  680         if (!sector_aligned || bp->b_blkno < 0) {
  681                 bp->b_error = EINVAL;
  682                 goto bad;
  683         }
  684         /*
  685          * If it's a null transfer, return immediatly
  686          */
  687         if (bp->b_bcount == 0)
  688                 goto done;
  689 
  690         /*
  691          * Do bounds checking, adjust transfer. if error, process.
  692          * If end of partition, just return.
  693          */
  694         if (SDPART(bp->b_dev) == RAW_PART) {
  695                 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
  696                     sd->params.disksize512) <= 0)
  697                         goto done;
  698         } else {
  699                 if (bounds_check_with_label(&sd->sc_dk, bp,
  700                     (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
  701                         goto done;
  702         }
  703 
  704         /*
  705          * Now convert the block number to absolute and put it in
  706          * terms of the device's logical block size.
  707          */
  708         if (lp->d_secsize == DEV_BSIZE)
  709                 blkno = bp->b_blkno;
  710         else if (lp->d_secsize > DEV_BSIZE)
  711                 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
  712         else
  713                 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
  714 
  715         if (SDPART(bp->b_dev) != RAW_PART)
  716                 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
  717 
  718         bp->b_rawblkno = blkno;
  719 
  720         s = splbio();
  721 
  722         /*
  723          * Place it in the queue of disk activities for this disk.
  724          *
  725          * XXX Only do disksort() if the current operating mode does not
  726          * XXX include tagged queueing.
  727          */
  728         BUFQ_PUT(&sd->buf_queue, bp);
  729 
  730         /*
  731          * Tell the device to get going on the transfer if it's
  732          * not doing anything, otherwise just wait for completion
  733          */
  734         sdstart(sd->sc_periph);
  735 
  736         splx(s);
  737         return;
  738 
  739 bad:
  740         bp->b_flags |= B_ERROR;
  741 done:
  742         /*
  743          * Correctly set the buf to indicate a completed xfer
  744          */
  745         bp->b_resid = bp->b_bcount;
  746         biodone(bp);
  747 }
  748 
  749 /*
  750  * sdstart looks to see if there is a buf waiting for the device
  751  * and that the device is not already busy. If both are true,
  752  * It dequeues the buf and creates a scsi command to perform the
  753  * transfer in the buf. The transfer request will call scsipi_done
  754  * on completion, which will in turn call this routine again
  755  * so that the next queued transfer is performed.
  756  * The bufs are queued by the strategy routine (sdstrategy)
  757  *
  758  * This routine is also called after other non-queued requests
  759  * have been made of the scsi driver, to ensure that the queue
  760  * continues to be drained.
  761  *
  762  * must be called at the correct (highish) spl level
  763  * sdstart() is called at splbio from sdstrategy, sdrestart and scsipi_done
  764  */
  765 static void
  766 sdstart(struct scsipi_periph *periph)
  767 {
  768         struct sd_softc *sd = (void *)periph->periph_dev;
  769         struct disklabel *lp = sd->sc_dk.dk_label;
  770         struct buf *bp = 0;
  771         struct scsipi_rw_16 cmd16;
  772         struct scsipi_rw_10 cmd_big;
  773         struct scsi_rw_6 cmd_small;
  774         struct scsipi_generic *cmdp;
  775         struct scsipi_xfer *xs;
  776         int nblks, cmdlen, error, flags;
  777 
  778         SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
  779         /*
  780          * Check if the device has room for another command
  781          */
  782         while (periph->periph_active < periph->periph_openings) {
  783                 /*
  784                  * there is excess capacity, but a special waits
  785                  * It'll need the adapter as soon as we clear out of the
  786                  * way and let it run (user level wait).
  787                  */
  788                 if (periph->periph_flags & PERIPH_WAITING) {
  789                         periph->periph_flags &= ~PERIPH_WAITING;
  790                         wakeup((caddr_t)periph);
  791                         return;
  792                 }
  793 
  794                 /*
  795                  * If the device has become invalid, abort all the
  796                  * reads and writes until all files have been closed and
  797                  * re-opened
  798                  */
  799                 if (__predict_false(
  800                     (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
  801                         if ((bp = BUFQ_GET(&sd->buf_queue)) != NULL) {
  802                                 bp->b_error = EIO;
  803                                 bp->b_flags |= B_ERROR;
  804                                 bp->b_resid = bp->b_bcount;
  805                                 biodone(bp);
  806                                 continue;
  807                         } else {
  808                                 return;
  809                         }
  810                 }
  811 
  812                 /*
  813                  * See if there is a buf with work for us to do..
  814                  */
  815                 if ((bp = BUFQ_PEEK(&sd->buf_queue)) == NULL)
  816                         return;
  817 
  818                 /*
  819                  * We have a buf, now we should make a command.
  820                  */
  821 
  822                 if (lp->d_secsize == DEV_BSIZE)
  823                         nblks = bp->b_bcount >> DEV_BSHIFT;
  824                 else
  825                         nblks = howmany(bp->b_bcount, lp->d_secsize);
  826 
  827                 /*
  828                  * Fill out the scsi command.  Use the smallest CDB possible
  829                  * (6-byte, 10-byte, or 16-byte).
  830                  */
  831                 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
  832                     ((nblks & 0xff) == nblks) &&
  833                     !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
  834                         /* 6-byte CDB */
  835                         memset(&cmd_small, 0, sizeof(cmd_small));
  836                         cmd_small.opcode = (bp->b_flags & B_READ) ?
  837                             SCSI_READ_6_COMMAND : SCSI_WRITE_6_COMMAND;
  838                         _lto3b(bp->b_rawblkno, cmd_small.addr);
  839                         cmd_small.length = nblks & 0xff;
  840                         cmdlen = sizeof(cmd_small);
  841                         cmdp = (struct scsipi_generic *)&cmd_small;
  842                 } else if ((bp->b_rawblkno & 0xffffffff) == bp->b_rawblkno) {
  843                         /* 10-byte CDB */
  844                         memset(&cmd_big, 0, sizeof(cmd_big));
  845                         cmd_big.opcode = (bp->b_flags & B_READ) ?
  846                             READ_10 : WRITE_10;
  847                         _lto4b(bp->b_rawblkno, cmd_big.addr);
  848                         _lto2b(nblks, cmd_big.length);
  849                         cmdlen = sizeof(cmd_big);
  850                         cmdp = (struct scsipi_generic *)&cmd_big;
  851                 } else {
  852                         /* 16-byte CDB */
  853                         memset(&cmd16, 0, sizeof(cmd16));
  854                         cmd16.opcode = (bp->b_flags & B_READ) ?
  855                             READ_16 : WRITE_16;
  856                         _lto8b(bp->b_rawblkno, cmd16.addr);
  857                         _lto4b(nblks, cmd16.length);
  858                         cmdlen = sizeof(cmd16);
  859                         cmdp = (struct scsipi_generic *)&cmd16;
  860                 }
  861 
  862                 /* Instrumentation. */
  863                 disk_busy(&sd->sc_dk);
  864 
  865                 /*
  866                  * Mark the disk dirty so that the cache will be
  867                  * flushed on close.
  868                  */
  869                 if ((bp->b_flags & B_READ) == 0)
  870                         sd->flags |= SDF_DIRTY;
  871 
  872                 /*
  873                  * Figure out what flags to use.
  874                  */
  875                 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
  876                 if (bp->b_flags & B_READ)
  877                         flags |= XS_CTL_DATA_IN;
  878                 else
  879                         flags |= XS_CTL_DATA_OUT;
  880 
  881                 /*
  882                  * Call the routine that chats with the adapter.
  883                  * Note: we cannot sleep as we may be an interrupt
  884                  */
  885                 xs = scsipi_make_xs(periph, cmdp, cmdlen,
  886                     (u_char *)bp->b_data, bp->b_bcount,
  887                     SDRETRIES, SD_IO_TIMEOUT, bp, flags);
  888                 if (__predict_false(xs == NULL)) {
  889                         /*
  890                          * out of memory. Keep this buffer in the queue, and
  891                          * retry later.
  892                          */
  893                         callout_reset(&sd->sc_callout, hz / 2, sdrestart,
  894                             periph);
  895                         return;
  896                 }
  897                 /*
  898                  * need to dequeue the buffer before queuing the command,
  899                  * because cdstart may be called recursively from the
  900                  * HBA driver
  901                  */
  902 #ifdef DIAGNOSTIC
  903                 if (BUFQ_GET(&sd->buf_queue) != bp)
  904                         panic("sdstart(): dequeued wrong buf");
  905 #else
  906                 BUFQ_GET(&sd->buf_queue);
  907 #endif
  908                 error = scsipi_execute_xs(xs);
  909                 /* with a scsipi_xfer preallocated, scsipi_command can't fail */
  910                 KASSERT(error == 0);
  911         }
  912 }
  913 
  914 static void
  915 sdrestart(void *v)
  916 {
  917         int s = splbio();
  918         sdstart((struct scsipi_periph *)v);
  919         splx(s);
  920 }
  921 
  922 static void
  923 sddone(struct scsipi_xfer *xs, int error)
  924 {
  925         struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
  926         struct buf *bp = xs->bp;
  927 
  928         if (sd->flags & SDF_FLUSHING) {
  929                 /* Flush completed, no longer dirty. */
  930                 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
  931         }
  932 
  933         if (bp) {
  934                 bp->b_error = error;
  935                 bp->b_resid = xs->resid;
  936                 if (error)
  937                         bp->b_flags |= B_ERROR;
  938 
  939                 disk_unbusy(&sd->sc_dk, bp->b_bcount - bp->b_resid,
  940                     (bp->b_flags & B_READ));
  941 #if NRND > 0
  942                 rnd_add_uint32(&sd->rnd_source, bp->b_rawblkno);
  943 #endif
  944 
  945                 biodone(bp);
  946         }
  947 }
  948 
  949 static void
  950 sdminphys(struct buf *bp)
  951 {
  952         struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
  953         long max;
  954 
  955         /*
  956          * If the device is ancient, we want to make sure that
  957          * the transfer fits into a 6-byte cdb.
  958          *
  959          * XXX Note that the SCSI-I spec says that 256-block transfers
  960          * are allowed in a 6-byte read/write, and are specified
  961          * by settng the "length" to 0.  However, we're conservative
  962          * here, allowing only 255-block transfers in case an
  963          * ancient device gets confused by length == 0.  A length of 0
  964          * in a 10-byte read/write actually means 0 blocks.
  965          */
  966         if ((sd->flags & SDF_ANCIENT) &&
  967             ((sd->sc_periph->periph_flags &
  968             (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
  969                 max = sd->sc_dk.dk_label->d_secsize * 0xff;
  970 
  971                 if (bp->b_bcount > max)
  972                         bp->b_bcount = max;
  973         }
  974 
  975         scsipi_adapter_minphys(sd->sc_periph->periph_channel, bp);
  976 }
  977 
  978 static int
  979 sdread(dev_t dev, struct uio *uio, int ioflag)
  980 {
  981 
  982         return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
  983 }
  984 
  985 static int
  986 sdwrite(dev_t dev, struct uio *uio, int ioflag)
  987 {
  988 
  989         return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
  990 }
  991 
  992 /*
  993  * Perform special action on behalf of the user
  994  * Knows about the internals of this device
  995  */
  996 static int
  997 sdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  998 {
  999         struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
 1000         struct scsipi_periph *periph = sd->sc_periph;
 1001         int part = SDPART(dev);
 1002         int error = 0;
 1003 #ifdef __HAVE_OLD_DISKLABEL
 1004         struct disklabel *newlabel = NULL;
 1005 #endif
 1006 
 1007         SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
 1008 
 1009         /*
 1010          * If the device is not valid, some IOCTLs can still be
 1011          * handled on the raw partition. Check this here.
 1012          */
 1013         if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
 1014                 switch (cmd) {
 1015                 case DIOCKLABEL:
 1016                 case DIOCWLABEL:
 1017                 case DIOCLOCK:
 1018                 case DIOCEJECT:
 1019                 case ODIOCEJECT:
 1020                 case DIOCGCACHE:
 1021                 case DIOCSCACHE:
 1022                 case SCIOCIDENTIFY:
 1023                 case OSCIOCIDENTIFY:
 1024                 case SCIOCCOMMAND:
 1025                 case SCIOCDEBUG:
 1026                         if (part == RAW_PART)
 1027                                 break;
 1028                 /* FALLTHROUGH */
 1029                 default:
 1030                         if ((periph->periph_flags & PERIPH_OPEN) == 0)
 1031                                 return (ENODEV);
 1032                         else
 1033                                 return (EIO);
 1034                 }
 1035         }
 1036 
 1037         switch (cmd) {
 1038         case DIOCGDINFO:
 1039                 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
 1040                 return (0);
 1041 
 1042 #ifdef __HAVE_OLD_DISKLABEL
 1043         case ODIOCGDINFO:
 1044                 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
 1045                 if (newlabel == NULL)
 1046                         return EIO;
 1047                 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
 1048                 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
 1049                         memcpy(addr, newlabel, sizeof (struct olddisklabel));
 1050                 else
 1051                         error = ENOTTY;
 1052                 free(newlabel, M_TEMP);
 1053                 return error;
 1054 #endif
 1055 
 1056         case DIOCGPART:
 1057                 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
 1058                 ((struct partinfo *)addr)->part =
 1059                     &sd->sc_dk.dk_label->d_partitions[part];
 1060                 return (0);
 1061 
 1062         case DIOCWDINFO:
 1063         case DIOCSDINFO:
 1064 #ifdef __HAVE_OLD_DISKLABEL
 1065         case ODIOCWDINFO:
 1066         case ODIOCSDINFO:
 1067 #endif
 1068         {
 1069                 struct disklabel *lp;
 1070 
 1071                 if ((flag & FWRITE) == 0)
 1072                         return (EBADF);
 1073 
 1074 #ifdef __HAVE_OLD_DISKLABEL
 1075                 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
 1076                         newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
 1077                         if (newlabel == NULL)
 1078                                 return EIO;
 1079                         memset(newlabel, 0, sizeof newlabel);
 1080                         memcpy(newlabel, addr, sizeof (struct olddisklabel));
 1081                         lp = newlabel;
 1082                 } else
 1083 #endif
 1084                 lp = (struct disklabel *)addr;
 1085 
 1086                 if ((error = lockmgr(&sd->sc_dk.dk_openlock,
 1087                                      LK_EXCLUSIVE, NULL)) != 0)
 1088                         goto bad;
 1089                 sd->flags |= SDF_LABELLING;
 1090 
 1091                 error = setdisklabel(sd->sc_dk.dk_label,
 1092                     lp, /*sd->sc_dk.dk_openmask : */0,
 1093                     sd->sc_dk.dk_cpulabel);
 1094                 if (error == 0) {
 1095                         if (cmd == DIOCWDINFO
 1096 #ifdef __HAVE_OLD_DISKLABEL
 1097                             || cmd == ODIOCWDINFO
 1098 #endif
 1099                            )
 1100                                 error = writedisklabel(SDLABELDEV(dev),
 1101                                     sdstrategy, sd->sc_dk.dk_label,
 1102                                     sd->sc_dk.dk_cpulabel);
 1103                 }
 1104 
 1105                 sd->flags &= ~SDF_LABELLING;
 1106                 (void) lockmgr(&sd->sc_dk.dk_openlock, LK_RELEASE, NULL);
 1107 bad:
 1108 #ifdef __HAVE_OLD_DISKLABEL
 1109                 if (newlabel != NULL)
 1110                         free(newlabel, M_TEMP);
 1111 #endif
 1112                 return (error);
 1113         }
 1114 
 1115         case DIOCKLABEL:
 1116                 if (*(int *)addr)
 1117                         periph->periph_flags |= PERIPH_KEEP_LABEL;
 1118                 else
 1119                         periph->periph_flags &= ~PERIPH_KEEP_LABEL;
 1120                 return (0);
 1121 
 1122         case DIOCWLABEL:
 1123                 if ((flag & FWRITE) == 0)
 1124                         return (EBADF);
 1125                 if (*(int *)addr)
 1126                         sd->flags |= SDF_WLABEL;
 1127                 else
 1128                         sd->flags &= ~SDF_WLABEL;
 1129                 return (0);
 1130 
 1131         case DIOCLOCK:
 1132                 if (periph->periph_flags & PERIPH_REMOVABLE)
 1133                         return (scsipi_prevent(periph,
 1134                             (*(int *)addr) ?
 1135                             SPAMR_PREVENT_DT : SPAMR_ALLOW, 0));
 1136                 else
 1137                         return (ENOTTY);
 1138 
 1139         case DIOCEJECT:
 1140                 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
 1141                         return (ENOTTY);
 1142                 if (*(int *)addr == 0) {
 1143                         /*
 1144                          * Don't force eject: check that we are the only
 1145                          * partition open. If so, unlock it.
 1146                          */
 1147                         if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
 1148                             sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
 1149                             sd->sc_dk.dk_openmask) {
 1150                                 error = scsipi_prevent(periph, SPAMR_ALLOW,
 1151                                     XS_CTL_IGNORE_NOT_READY);
 1152                                 if (error)
 1153                                         return (error);
 1154                         } else {
 1155                                 return (EBUSY);
 1156                         }
 1157                 }
 1158                 /* FALLTHROUGH */
 1159         case ODIOCEJECT:
 1160                 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
 1161                     ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
 1162 
 1163         case DIOCGDEFLABEL:
 1164                 sdgetdefaultlabel(sd, (struct disklabel *)addr);
 1165                 return (0);
 1166 
 1167 #ifdef __HAVE_OLD_DISKLABEL
 1168         case ODIOCGDEFLABEL:
 1169                 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
 1170                 if (newlabel == NULL)
 1171                         return EIO;
 1172                 sdgetdefaultlabel(sd, newlabel);
 1173                 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
 1174                         memcpy(addr, newlabel, sizeof (struct olddisklabel));
 1175                 else
 1176                         error = ENOTTY;
 1177                 free(newlabel, M_TEMP);
 1178                 return error;
 1179 #endif
 1180 
 1181         case DIOCGCACHE:
 1182                 return (sd_getcache(sd, (int *) addr));
 1183 
 1184         case DIOCSCACHE:
 1185                 if ((flag & FWRITE) == 0)
 1186                         return (EBADF);
 1187                 return (sd_setcache(sd, *(int *) addr));
 1188 
 1189         case DIOCCACHESYNC:
 1190                 /*
 1191                  * XXX Do we really need to care about having a writable
 1192                  * file descriptor here?
 1193                  */
 1194                 if ((flag & FWRITE) == 0)
 1195                         return (EBADF);
 1196                 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0)) {
 1197                         error = sd_flush(sd, 0);
 1198                         if (error)
 1199                                 sd->flags &= ~SDF_FLUSHING;
 1200                         else
 1201                                 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
 1202                 } else
 1203                         error = 0;
 1204                 return (error);
 1205 
 1206         case DIOCAWEDGE:
 1207             {
 1208                 struct dkwedge_info *dkw = (void *) addr;
 1209 
 1210                 if ((flag & FWRITE) == 0)
 1211                         return (EBADF);
 1212 
 1213                 /* If the ioctl happens here, the parent is us. */
 1214                 strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname);
 1215                 return (dkwedge_add(dkw));
 1216             }
 1217 
 1218         case DIOCDWEDGE:
 1219             {
 1220                 struct dkwedge_info *dkw = (void *) addr;
 1221 
 1222                 if ((flag & FWRITE) == 0)
 1223                         return (EBADF);
 1224 
 1225                 /* If the ioctl happens here, the parent is us. */
 1226                 strcpy(dkw->dkw_parent, sd->sc_dev.dv_xname);
 1227                 return (dkwedge_del(dkw));
 1228             }
 1229 
 1230         case DIOCLWEDGES:
 1231             {
 1232                 struct dkwedge_list *dkwl = (void *) addr;
 1233 
 1234                 return (dkwedge_list(&sd->sc_dk, dkwl, p));
 1235             }
 1236 
 1237         default:
 1238                 if (part != RAW_PART)
 1239                         return (ENOTTY);
 1240                 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
 1241         }
 1242 
 1243 #ifdef DIAGNOSTIC
 1244         panic("sdioctl: impossible");
 1245 #endif
 1246 }
 1247 
 1248 static void
 1249 sdgetdefaultlabel(struct sd_softc *sd, struct disklabel *lp)
 1250 {
 1251 
 1252         memset(lp, 0, sizeof(struct disklabel));
 1253 
 1254         lp->d_secsize = sd->params.blksize;
 1255         lp->d_ntracks = sd->params.heads;
 1256         lp->d_nsectors = sd->params.sectors;
 1257         lp->d_ncylinders = sd->params.cyls;
 1258         lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
 1259 
 1260         switch (scsipi_periph_bustype(sd->sc_periph)) {
 1261         case SCSIPI_BUSTYPE_SCSI:
 1262                 lp->d_type = DTYPE_SCSI;
 1263                 break;
 1264         case SCSIPI_BUSTYPE_ATAPI:
 1265                 lp->d_type = DTYPE_ATAPI;
 1266                 break;
 1267         }
 1268         /*
 1269          * XXX
 1270          * We could probe the mode pages to figure out what kind of disc it is.
 1271          * Is this worthwhile?
 1272          */
 1273         strncpy(lp->d_typename, "mydisk", 16);
 1274         strncpy(lp->d_packname, "fictitious", 16);
 1275         lp->d_secperunit = sd->params.disksize;
 1276         lp->d_rpm = sd->params.rot_rate;
 1277         lp->d_interleave = 1;
 1278         lp->d_flags = sd->sc_periph->periph_flags & PERIPH_REMOVABLE ?
 1279             D_REMOVABLE : 0;
 1280 
 1281         lp->d_partitions[RAW_PART].p_offset = 0;
 1282         lp->d_partitions[RAW_PART].p_size =
 1283             lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
 1284         lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
 1285         lp->d_npartitions = RAW_PART + 1;
 1286 
 1287         lp->d_magic = DISKMAGIC;
 1288         lp->d_magic2 = DISKMAGIC;
 1289         lp->d_checksum = dkcksum(lp);
 1290 }
 1291 
 1292 
 1293 /*
 1294  * Load the label information on the named device
 1295  */
 1296 static void
 1297 sdgetdisklabel(struct sd_softc *sd)
 1298 {
 1299         struct disklabel *lp = sd->sc_dk.dk_label;
 1300         const char *errstring;
 1301 
 1302         memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
 1303 
 1304         sdgetdefaultlabel(sd, lp);
 1305 
 1306         if (lp->d_secpercyl == 0) {
 1307                 lp->d_secpercyl = 100;
 1308                 /* as long as it's not 0 - readdisklabel divides by it (?) */
 1309         }
 1310 
 1311         /*
 1312          * Call the generic disklabel extraction routine
 1313          */
 1314         errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
 1315             sdstrategy, lp, sd->sc_dk.dk_cpulabel);
 1316         if (errstring) {
 1317                 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
 1318                 return;
 1319         }
 1320 }
 1321 
 1322 static void
 1323 sd_shutdown(void *arg)
 1324 {
 1325         struct sd_softc *sd = arg;
 1326 
 1327         /*
 1328          * If the disk cache needs to be flushed, and the disk supports
 1329          * it, flush it.  We're cold at this point, so we poll for
 1330          * completion.
 1331          */
 1332         if ((sd->flags & SDF_DIRTY) != 0) {
 1333                 if (sd_flush(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
 1334                         printf("%s: cache synchronization failed\n",
 1335                             sd->sc_dev.dv_xname);
 1336                         sd->flags &= ~SDF_FLUSHING;
 1337                 } else
 1338                         sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
 1339         }
 1340 }
 1341 
 1342 /*
 1343  * Check Errors
 1344  */
 1345 static int
 1346 sd_interpret_sense(struct scsipi_xfer *xs)
 1347 {
 1348         struct scsipi_periph *periph = xs->xs_periph;
 1349         struct scsi_sense_data *sense = &xs->sense.scsi_sense;
 1350         struct sd_softc *sd = (void *)periph->periph_dev;
 1351         int s, error, retval = EJUSTRETURN;
 1352 
 1353         /*
 1354          * If the periph is already recovering, just do the normal
 1355          * error processing.
 1356          */
 1357         if (periph->periph_flags & PERIPH_RECOVERING)
 1358                 return (retval);
 1359 
 1360         /*
 1361          * Ignore errors from accessing illegal fields (e.g. trying to
 1362          * lock the door of a digicam, which doesn't have a door that
 1363          * can be locked) for the SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL command.
 1364          */
 1365         if (xs->cmd->opcode == SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL &&
 1366             SSD_SENSE_KEY(sense->flags) == SKEY_ILLEGAL_REQUEST &&
 1367             sense->asc == 0x24 &&
 1368             sense->ascq == 0x00) { /* Illegal field in CDB */
 1369                 scsipi_printaddr(periph);
 1370                 printf("no door lock\n");
 1371                 periph->periph_flags &= ~PERIPH_REMOVABLE;
 1372                 return 0;
 1373         }
 1374 
 1375 
 1376 
 1377         /*
 1378          * If the device is not open yet, let the generic code handle it.
 1379          */
 1380         if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
 1381                 return (retval);
 1382 
 1383         /*
 1384          * If it isn't a extended or extended/deferred error, let
 1385          * the generic code handle it.
 1386          */
 1387         if ((sense->response_code & SSD_RCODE_VALID) == 0 ||
 1388             (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
 1389              SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED))
 1390                 return (retval);
 1391 
 1392         if (SSD_SENSE_KEY(sense->flags) == SKEY_NOT_READY &&
 1393             sense->asc == 0x4) {
 1394                 if (sense->ascq == 0x01)        {
 1395                         /*
 1396                          * Unit In The Process Of Becoming Ready.
 1397                          */
 1398                         printf("%s: waiting for pack to spin up...\n",
 1399                             sd->sc_dev.dv_xname);
 1400                         if (!callout_pending(&periph->periph_callout))
 1401                                 scsipi_periph_freeze(periph, 1);
 1402                         callout_reset(&periph->periph_callout,
 1403                             5 * hz, scsipi_periph_timed_thaw, periph);
 1404                         retval = ERESTART;
 1405                 } else if (sense->ascq == 0x02) {
 1406                         printf("%s: pack is stopped, restarting...\n",
 1407                             sd->sc_dev.dv_xname);
 1408                         s = splbio();
 1409                         periph->periph_flags |= PERIPH_RECOVERING;
 1410                         splx(s);
 1411                         error = scsipi_start(periph, SSS_START,
 1412                             XS_CTL_URGENT|XS_CTL_HEAD_TAG|
 1413                             XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
 1414                         if (error) {
 1415                                 printf("%s: unable to restart pack\n",
 1416                                     sd->sc_dev.dv_xname);
 1417                                 retval = error;
 1418                         } else
 1419                                 retval = ERESTART;
 1420                         s = splbio();
 1421                         periph->periph_flags &= ~PERIPH_RECOVERING;
 1422                         splx(s);
 1423                 }
 1424         }
 1425         if (SSD_SENSE_KEY(sense->flags) == SKEY_MEDIUM_ERROR &&
 1426             sense->asc == 0x31 &&
 1427             sense->ascq == 0x00)        { /* maybe for any asq ? */
 1428                 /* Medium Format Corrupted */
 1429                 retval = EFTYPE;
 1430         }
 1431         return (retval);
 1432 }
 1433 
 1434 
 1435 static int
 1436 sdsize(dev_t dev)
 1437 {
 1438         struct sd_softc *sd;
 1439         int part, unit, omask;
 1440         int size;
 1441 
 1442         unit = SDUNIT(dev);
 1443         if (unit >= sd_cd.cd_ndevs)
 1444                 return (-1);
 1445         sd = sd_cd.cd_devs[unit];
 1446         if (sd == NULL)
 1447                 return (-1);
 1448 
 1449         if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
 1450                 return (-1);
 1451 
 1452         part = SDPART(dev);
 1453         omask = sd->sc_dk.dk_openmask & (1 << part);
 1454 
 1455         if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
 1456                 return (-1);
 1457         if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
 1458                 size = -1;
 1459         else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
 1460                 size = -1;
 1461         else
 1462                 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
 1463                     (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
 1464         if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
 1465                 return (-1);
 1466         return (size);
 1467 }
 1468 
 1469 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
 1470 static struct scsipi_xfer sx;
 1471 static int sddoingadump;
 1472 
 1473 /*
 1474  * dump all of physical memory into the partition specified, starting
 1475  * at offset 'dumplo' into the partition.
 1476  */
 1477 static int
 1478 sddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
 1479 {
 1480         struct sd_softc *sd;    /* disk unit to do the I/O */
 1481         struct disklabel *lp;   /* disk's disklabel */
 1482         int     unit, part;
 1483         int     sectorsize;     /* size of a disk sector */
 1484         int     nsects;         /* number of sectors in partition */
 1485         int     sectoff;        /* sector offset of partition */
 1486         int     totwrt;         /* total number of sectors left to write */
 1487         int     nwrt;           /* current number of sectors to write */
 1488         struct scsipi_rw_10 cmd;        /* write command */
 1489         struct scsipi_xfer *xs; /* ... convenience */
 1490         struct scsipi_periph *periph;
 1491         struct scsipi_channel *chan;
 1492 
 1493         /* Check if recursive dump; if so, punt. */
 1494         if (sddoingadump)
 1495                 return (EFAULT);
 1496 
 1497         /* Mark as active early. */
 1498         sddoingadump = 1;
 1499 
 1500         unit = SDUNIT(dev);     /* Decompose unit & partition. */
 1501         part = SDPART(dev);
 1502 
 1503         /* Check for acceptable drive number. */
 1504         if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
 1505                 return (ENXIO);
 1506 
 1507         if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
 1508                 return (ENODEV);
 1509 
 1510         periph = sd->sc_periph;
 1511         chan = periph->periph_channel;
 1512 
 1513         /* Make sure it was initialized. */
 1514         if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
 1515                 return (ENXIO);
 1516 
 1517         /* Convert to disk sectors.  Request must be a multiple of size. */
 1518         lp = sd->sc_dk.dk_label;
 1519         sectorsize = lp->d_secsize;
 1520         if ((size % sectorsize) != 0)
 1521                 return (EFAULT);
 1522         totwrt = size / sectorsize;
 1523         blkno = dbtob(blkno) / sectorsize;      /* blkno in DEV_BSIZE units */
 1524 
 1525         nsects = lp->d_partitions[part].p_size;
 1526         sectoff = lp->d_partitions[part].p_offset;
 1527 
 1528         /* Check transfer bounds against partition size. */
 1529         if ((blkno < 0) || ((blkno + totwrt) > nsects))
 1530                 return (EINVAL);
 1531 
 1532         /* Offset block number to start of partition. */
 1533         blkno += sectoff;
 1534 
 1535         xs = &sx;
 1536 
 1537         while (totwrt > 0) {
 1538                 nwrt = totwrt;          /* XXX */
 1539 #ifndef SD_DUMP_NOT_TRUSTED
 1540                 /*
 1541                  *  Fill out the scsi command
 1542                  */
 1543                 memset(&cmd, 0, sizeof(cmd));
 1544                 cmd.opcode = WRITE_10;
 1545                 _lto4b(blkno, cmd.addr);
 1546                 _lto2b(nwrt, cmd.length);
 1547                 /*
 1548                  * Fill out the scsipi_xfer structure
 1549                  *    Note: we cannot sleep as we may be an interrupt
 1550                  * don't use scsipi_command() as it may want to wait
 1551                  * for an xs.
 1552                  */
 1553                 memset(xs, 0, sizeof(sx));
 1554                 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
 1555                     XS_CTL_DATA_OUT;
 1556                 xs->xs_status = 0;
 1557                 xs->xs_periph = periph;
 1558                 xs->xs_retries = SDRETRIES;
 1559                 xs->timeout = 10000;    /* 10000 millisecs for a disk ! */
 1560                 xs->cmd = (struct scsipi_generic *)&cmd;
 1561                 xs->cmdlen = sizeof(cmd);
 1562                 xs->resid = nwrt * sectorsize;
 1563                 xs->error = XS_NOERROR;
 1564                 xs->bp = 0;
 1565                 xs->data = va;
 1566                 xs->datalen = nwrt * sectorsize;
 1567 
 1568                 /*
 1569                  * Pass all this info to the scsi driver.
 1570                  */
 1571                 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
 1572                 if ((xs->xs_status & XS_STS_DONE) == 0 ||
 1573                     xs->error != XS_NOERROR)
 1574                         return (EIO);
 1575 #else   /* SD_DUMP_NOT_TRUSTED */
 1576                 /* Let's just talk about this first... */
 1577                 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
 1578                 delay(500 * 1000);      /* half a second */
 1579 #endif  /* SD_DUMP_NOT_TRUSTED */
 1580 
 1581                 /* update block count */
 1582                 totwrt -= nwrt;
 1583                 blkno += nwrt;
 1584                 va += sectorsize * nwrt;
 1585         }
 1586         sddoingadump = 0;
 1587         return (0);
 1588 }
 1589 
 1590 static int
 1591 sd_mode_sense(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
 1592     int page, int flags, int *big)
 1593 {
 1594 
 1595         if ((sd->sc_periph->periph_quirks & PQUIRK_ONLYBIG) &&
 1596             !(sd->sc_periph->periph_quirks & PQUIRK_NOBIGMODESENSE)) {
 1597                 *big = 1;
 1598                 return scsipi_mode_sense_big(sd->sc_periph, byte2, page, sense,
 1599                     size + sizeof(struct scsi_mode_parameter_header_10),
 1600                     flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
 1601         } else {
 1602                 *big = 0;
 1603                 return scsipi_mode_sense(sd->sc_periph, byte2, page, sense,
 1604                     size + sizeof(struct scsi_mode_parameter_header_6),
 1605                     flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
 1606         }
 1607 }
 1608 
 1609 static int
 1610 sd_mode_select(struct sd_softc *sd, u_int8_t byte2, void *sense, size_t size,
 1611     int flags, int big)
 1612 {
 1613 
 1614         if (big) {
 1615                 struct scsi_mode_parameter_header_10 *header = sense;
 1616 
 1617                 _lto2b(0, header->data_length);
 1618                 return scsipi_mode_select_big(sd->sc_periph, byte2, sense,
 1619                     size + sizeof(struct scsi_mode_parameter_header_10),
 1620                     flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
 1621         } else {
 1622                 struct scsi_mode_parameter_header_6 *header = sense;
 1623 
 1624                 header->data_length = 0;
 1625                 return scsipi_mode_select(sd->sc_periph, byte2, sense,
 1626                     size + sizeof(struct scsi_mode_parameter_header_6),
 1627                     flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
 1628         }
 1629 }
 1630 
 1631 static int
 1632 sd_get_simplifiedparms(struct sd_softc *sd, struct disk_parms *dp, int flags)
 1633 {
 1634         struct {
 1635                 struct scsi_mode_parameter_header_6 header;
 1636                 /* no block descriptor */
 1637                 u_int8_t pg_code; /* page code (should be 6) */
 1638                 u_int8_t pg_length; /* page length (should be 11) */
 1639                 u_int8_t wcd; /* bit0: cache disable */
 1640                 u_int8_t lbs[2]; /* logical block size */
 1641                 u_int8_t size[5]; /* number of log. blocks */
 1642                 u_int8_t pp; /* power/performance */
 1643                 u_int8_t flags;
 1644                 u_int8_t resvd;
 1645         } scsipi_sense;
 1646         u_int64_t sectors;
 1647         int error;
 1648 
 1649         /*
 1650          * scsipi_size (ie "read capacity") and mode sense page 6
 1651          * give the same information. Do both for now, and check
 1652          * for consistency.
 1653          * XXX probably differs for removable media
 1654          */
 1655         dp->blksize = 512;
 1656         if ((sectors = scsipi_size(sd->sc_periph, flags)) == 0)
 1657                 return (SDGP_RESULT_OFFLINE);           /* XXX? */
 1658 
 1659         error = scsipi_mode_sense(sd->sc_periph, SMS_DBD, 6,
 1660             &scsipi_sense.header, sizeof(scsipi_sense),
 1661             flags | XS_CTL_DATA_ONSTACK, SDRETRIES, 6000);
 1662 
 1663         if (error != 0)
 1664                 return (SDGP_RESULT_OFFLINE);           /* XXX? */
 1665 
 1666         dp->blksize = _2btol(scsipi_sense.lbs);
 1667         if (dp->blksize == 0)
 1668                 dp->blksize = 512;
 1669 
 1670         /*
 1671          * Create a pseudo-geometry.
 1672          */
 1673         dp->heads = 64;
 1674         dp->sectors = 32;
 1675         dp->cyls = sectors / (dp->heads * dp->sectors);
 1676         dp->disksize = _5btol(scsipi_sense.size);
 1677         if (dp->disksize <= UINT32_MAX && dp->disksize != sectors) {
 1678                 printf("RBC size: mode sense=%llu, get cap=%llu\n",
 1679                        (unsigned long long)dp->disksize,
 1680                        (unsigned long long)sectors);
 1681                 dp->disksize = sectors;
 1682         }
 1683         dp->disksize512 = (dp->disksize * dp->blksize) / DEV_BSIZE;
 1684 
 1685         return (SDGP_RESULT_OK);
 1686 }
 1687 
 1688 /*
 1689  * Get the scsi driver to send a full inquiry to the * device and use the
 1690  * results to fill out the disk parameter structure.
 1691  */
 1692 static int
 1693 sd_get_capacity(struct sd_softc *sd, struct disk_parms *dp, int flags)
 1694 {
 1695         u_int64_t sectors;
 1696         int error;
 1697 #if 0
 1698         int i;
 1699         u_int8_t *p;
 1700 #endif
 1701 
 1702         dp->disksize = sectors = scsipi_size(sd->sc_periph, flags);
 1703         if (sectors == 0) {
 1704                 struct scsipi_read_format_capacities cmd;
 1705                 struct {
 1706                         struct scsipi_capacity_list_header header;
 1707                         struct scsipi_capacity_descriptor desc;
 1708                 } __attribute__((packed)) data;
 1709 
 1710                 memset(&cmd, 0, sizeof(cmd));
 1711                 memset(&data, 0, sizeof(data));
 1712                 cmd.opcode = READ_FORMAT_CAPACITIES;
 1713                 _lto2b(sizeof(data), cmd.length);
 1714 
 1715                 error = scsipi_command(sd->sc_periph,
 1716                     (void *)&cmd, sizeof(cmd), (void *)&data, sizeof(data),
 1717                     SDRETRIES, 20000, NULL,
 1718                     flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
 1719                 if (error == EFTYPE) {
 1720                         /* Medium Format Corrupted, handle as not formatted */
 1721                         return (SDGP_RESULT_UNFORMATTED);
 1722                 }
 1723                 if (error || data.header.length == 0)
 1724                         return (SDGP_RESULT_OFFLINE);
 1725 
 1726 #if 0
 1727 printf("rfc: length=%d\n", data.header.length);
 1728 printf("rfc result:"); for (i = sizeof(struct scsipi_capacity_list_header) + data.header.length, p = (void *)&data; i; i--, p++) printf(" %02x", *p); printf("\n");
 1729 #endif
 1730                 switch (data.desc.byte5 & SCSIPI_CAP_DESC_CODE_MASK) {
 1731                 case SCSIPI_CAP_DESC_CODE_RESERVED:
 1732                 case SCSIPI_CAP_DESC_CODE_FORMATTED:
 1733                         break;
 1734 
 1735                 case SCSIPI_CAP_DESC_CODE_UNFORMATTED:
 1736                         return (SDGP_RESULT_UNFORMATTED);
 1737 
 1738                 case SCSIPI_CAP_DESC_CODE_NONE:
 1739                         return (SDGP_RESULT_OFFLINE);
 1740                 }
 1741 
 1742                 dp->disksize = sectors = _4btol(data.desc.nblks);
 1743                 if (sectors == 0)
 1744                         return (SDGP_RESULT_OFFLINE);           /* XXX? */
 1745 
 1746                 dp->blksize = _3btol(data.desc.blklen);
 1747                 if (dp->blksize == 0)
 1748                         dp->blksize = 512;
 1749         } else {
 1750                 struct sd_mode_sense_data scsipi_sense;
 1751                 int big, bsize;
 1752                 struct scsi_general_block_descriptor *bdesc;
 1753 
 1754                 memset(&scsipi_sense, 0, sizeof(scsipi_sense));
 1755                 error = sd_mode_sense(sd, 0, &scsipi_sense,
 1756                     sizeof(scsipi_sense.blk_desc), 0, flags | XS_CTL_SILENT, &big);
 1757                 dp->blksize = 512;
 1758                 if (!error) {
 1759                         if (big) {
 1760                                 bdesc = (void *)(&scsipi_sense.header.big + 1);
 1761                                 bsize = _2btol(scsipi_sense.header.big.blk_desc_len);
 1762                         } else {
 1763                                 bdesc = (void *)(&scsipi_sense.header.small + 1);
 1764                                 bsize = scsipi_sense.header.small.blk_desc_len;
 1765                         }
 1766 
 1767 #if 0
 1768 printf("page 0 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
 1769 printf("page 0 bsize=%d\n", bsize);
 1770 printf("page 0 ok\n");
 1771 #endif
 1772 
 1773                         if (bsize >= 8) {
 1774                                 dp->blksize = _3btol(bdesc->blklen);
 1775                                 if (dp->blksize == 0)
 1776                                         dp->blksize = 512;
 1777                         }
 1778                 }
 1779         }
 1780 
 1781         dp->disksize512 = (sectors * dp->blksize) / DEV_BSIZE;
 1782         return (0);
 1783 }
 1784 
 1785 static int
 1786 sd_get_parms_page4(struct sd_softc *sd, struct disk_parms *dp, int flags)
 1787 {
 1788         struct sd_mode_sense_data scsipi_sense;
 1789         int error;
 1790         int big, poffset, byte2;
 1791         union scsi_disk_pages *pages;
 1792 #if 0
 1793         int i;
 1794         u_int8_t *p;
 1795 #endif
 1796 
 1797         byte2 = SMS_DBD;
 1798 again:
 1799         memset(&scsipi_sense, 0, sizeof(scsipi_sense));
 1800         error = sd_mode_sense(sd, byte2, &scsipi_sense,
 1801             (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
 1802             sizeof(scsipi_sense.pages.rigid_geometry), 4,
 1803             flags | XS_CTL_SILENT, &big);
 1804         if (error) {
 1805                 if (byte2 == SMS_DBD) {
 1806                         /* No result; try once more with DBD off */
 1807                         byte2 = 0;
 1808                         goto again;
 1809                 }
 1810                 return (error);
 1811         }
 1812 
 1813         if (big) {
 1814                 poffset = sizeof scsipi_sense.header.big;
 1815                 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
 1816         } else {
 1817                 poffset = sizeof scsipi_sense.header.small;
 1818                 poffset += scsipi_sense.header.small.blk_desc_len;
 1819         }
 1820 
 1821         pages = (void *)((u_long)&scsipi_sense + poffset);
 1822 #if 0
 1823 printf("page 4 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
 1824 printf("page 4 pg_code=%d sense=%p/%p\n", pages->rigid_geometry.pg_code, &scsipi_sense, pages);
 1825 #endif
 1826 
 1827         if ((pages->rigid_geometry.pg_code & PGCODE_MASK) != 4)
 1828                 return (ERESTART);
 1829 
 1830         SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
 1831             ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
 1832             _3btol(pages->rigid_geometry.ncyl),
 1833             pages->rigid_geometry.nheads,
 1834             _2btol(pages->rigid_geometry.st_cyl_wp),
 1835             _2btol(pages->rigid_geometry.st_cyl_rwc),
 1836             _2btol(pages->rigid_geometry.land_zone)));
 1837 
 1838         /*
 1839          * KLUDGE!! (for zone recorded disks)
 1840          * give a number of sectors so that sec * trks * cyls
 1841          * is <= disk_size
 1842          * can lead to wasted space! THINK ABOUT THIS !
 1843          */
 1844         dp->heads = pages->rigid_geometry.nheads;
 1845         dp->cyls = _3btol(pages->rigid_geometry.ncyl);
 1846         if (dp->heads == 0 || dp->cyls == 0)
 1847                 return (ERESTART);
 1848         dp->sectors = dp->disksize / (dp->heads * dp->cyls);    /* XXX */
 1849 
 1850         dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
 1851         if (dp->rot_rate == 0)
 1852                 dp->rot_rate = 3600;
 1853 
 1854 #if 0
 1855 printf("page 4 ok\n");
 1856 #endif
 1857         return (0);
 1858 }
 1859 
 1860 static int
 1861 sd_get_parms_page5(struct sd_softc *sd, struct disk_parms *dp, int flags)
 1862 {
 1863         struct sd_mode_sense_data scsipi_sense;
 1864         int error;
 1865         int big, poffset, byte2;
 1866         union scsi_disk_pages *pages;
 1867 #if 0
 1868         int i;
 1869         u_int8_t *p;
 1870 #endif
 1871 
 1872         byte2 = SMS_DBD;
 1873 again:
 1874         memset(&scsipi_sense, 0, sizeof(scsipi_sense));
 1875         error = sd_mode_sense(sd, 0, &scsipi_sense,
 1876             (byte2 ? 0 : sizeof(scsipi_sense.blk_desc)) +
 1877             sizeof(scsipi_sense.pages.flex_geometry), 5,
 1878             flags | XS_CTL_SILENT, &big);
 1879         if (error) {
 1880                 if (byte2 == SMS_DBD) {
 1881                         /* No result; try once more with DBD off */
 1882                         byte2 = 0;
 1883                         goto again;
 1884                 }
 1885                 return (error);
 1886         }
 1887 
 1888         if (big) {
 1889                 poffset = sizeof scsipi_sense.header.big;
 1890                 poffset += _2btol(scsipi_sense.header.big.blk_desc_len);
 1891         } else {
 1892                 poffset = sizeof scsipi_sense.header.small;
 1893                 poffset += scsipi_sense.header.small.blk_desc_len;
 1894         }
 1895 
 1896         pages = (void *)((u_long)&scsipi_sense + poffset);
 1897 #if 0
 1898 printf("page 5 sense:"); for (i = sizeof(scsipi_sense), p = (void *)&scsipi_sense; i; i--, p++) printf(" %02x", *p); printf("\n");
 1899 printf("page 5 pg_code=%d sense=%p/%p\n", pages->flex_geometry.pg_code, &scsipi_sense, pages);
 1900 #endif
 1901 
 1902         if ((pages->flex_geometry.pg_code & PGCODE_MASK) != 5)
 1903                 return (ERESTART);
 1904 
 1905         SC_DEBUG(sd->sc_periph, SCSIPI_DB3,
 1906             ("%d cyls, %d heads, %d sec, %d bytes/sec\n",
 1907             _3btol(pages->flex_geometry.ncyl),
 1908             pages->flex_geometry.nheads,
 1909             pages->flex_geometry.ph_sec_tr,
 1910             _2btol(pages->flex_geometry.bytes_s)));
 1911 
 1912         dp->heads = pages->flex_geometry.nheads;
 1913         dp->cyls = _2btol(pages->flex_geometry.ncyl);
 1914         dp->sectors = pages->flex_geometry.ph_sec_tr;
 1915         if (dp->heads == 0 || dp->cyls == 0 || dp->sectors == 0)
 1916                 return (ERESTART);
 1917 
 1918         dp->rot_rate = _2btol(pages->rigid_geometry.rpm);
 1919         if (dp->rot_rate == 0)
 1920                 dp->rot_rate = 3600;
 1921 
 1922 #if 0
 1923 printf("page 5 ok\n");
 1924 #endif
 1925         return (0);
 1926 }
 1927 
 1928 static int
 1929 sd_get_parms(struct sd_softc *sd, struct disk_parms *dp, int flags)
 1930 {
 1931         int error;
 1932 
 1933         /*
 1934          * If offline, the SDEV_MEDIA_LOADED flag will be
 1935          * cleared by the caller if necessary.
 1936          */
 1937         if (sd->type == T_SIMPLE_DIRECT)
 1938                 return (sd_get_simplifiedparms(sd, dp, flags));
 1939 
 1940         error = sd_get_capacity(sd, dp, flags);
 1941         if (error)
 1942                 return (error);
 1943 
 1944         if (sd->type == T_OPTICAL)
 1945                 goto page0;
 1946 
 1947         if (sd->sc_periph->periph_flags & PERIPH_REMOVABLE) {
 1948                 if (!sd_get_parms_page5(sd, dp, flags) ||
 1949                     !sd_get_parms_page4(sd, dp, flags))
 1950                         return (SDGP_RESULT_OK);
 1951         } else {
 1952                 if (!sd_get_parms_page4(sd, dp, flags) ||
 1953                     !sd_get_parms_page5(sd, dp, flags))
 1954                         return (SDGP_RESULT_OK);
 1955         }
 1956 
 1957 page0:
 1958         printf("%s: fabricating a geometry\n", sd->sc_dev.dv_xname);
 1959         /* Try calling driver's method for figuring out geometry. */
 1960         if (!sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom ||
 1961             !(*sd->sc_periph->periph_channel->chan_adapter->adapt_getgeom)
 1962                 (sd->sc_periph, dp, dp->disksize)) {
 1963                 /*
 1964                  * Use adaptec standard fictitious geometry
 1965                  * this depends on which controller (e.g. 1542C is
 1966                  * different. but we have to put SOMETHING here..)
 1967                  */
 1968                 dp->heads = 64;
 1969                 dp->sectors = 32;
 1970                 dp->cyls = dp->disksize / (64 * 32);
 1971         }
 1972         dp->rot_rate = 3600;
 1973         return (SDGP_RESULT_OK);
 1974 }
 1975 
 1976 static int
 1977 sd_flush(struct sd_softc *sd, int flags)
 1978 {
 1979         struct scsipi_periph *periph = sd->sc_periph;
 1980         struct scsi_synchronize_cache_10 cmd;
 1981 
 1982         /*
 1983          * If the device is SCSI-2, issue a SYNCHRONIZE CACHE.
 1984          * We issue with address 0 length 0, which should be
 1985          * interpreted by the device as "all remaining blocks
 1986          * starting at address 0".  We ignore ILLEGAL REQUEST
 1987          * in the event that the command is not supported by
 1988          * the device, and poll for completion so that we know
 1989          * that the cache has actually been flushed.
 1990          *
 1991          * Unless, that is, the device can't handle the SYNCHRONIZE CACHE
 1992          * command, as indicated by our quirks flags.
 1993          *
 1994          * XXX What about older devices?
 1995          */
 1996         if (periph->periph_version < 2 ||
 1997             (periph->periph_quirks & PQUIRK_NOSYNCCACHE))
 1998                 return (0);
 1999 
 2000         sd->flags |= SDF_FLUSHING;
 2001         memset(&cmd, 0, sizeof(cmd));
 2002         cmd.opcode = SCSI_SYNCHRONIZE_CACHE_10;
 2003 
 2004         return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
 2005             SDRETRIES, 100000, NULL, flags | XS_CTL_IGNORE_ILLEGAL_REQUEST));
 2006 }
 2007 
 2008 static int
 2009 sd_getcache(struct sd_softc *sd, int *bitsp)
 2010 {
 2011         struct scsipi_periph *periph = sd->sc_periph;
 2012         struct sd_mode_sense_data scsipi_sense;
 2013         int error, bits = 0;
 2014         int big;
 2015         union scsi_disk_pages *pages;
 2016 
 2017         if (periph->periph_version < 2)
 2018                 return (EOPNOTSUPP);
 2019 
 2020         memset(&scsipi_sense, 0, sizeof(scsipi_sense));
 2021         error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
 2022             sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
 2023         if (error)
 2024                 return (error);
 2025 
 2026         if (big)
 2027                 pages = (void *)(&scsipi_sense.header.big + 1);
 2028         else
 2029                 pages = (void *)(&scsipi_sense.header.small + 1);
 2030 
 2031         if ((pages->caching_params.flags & CACHING_RCD) == 0)
 2032                 bits |= DKCACHE_READ;
 2033         if (pages->caching_params.flags & CACHING_WCE)
 2034                 bits |= DKCACHE_WRITE;
 2035         if (pages->caching_params.pg_code & PGCODE_PS)
 2036                 bits |= DKCACHE_SAVE;
 2037 
 2038         memset(&scsipi_sense, 0, sizeof(scsipi_sense));
 2039         error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
 2040             sizeof(scsipi_sense.pages.caching_params),
 2041             SMS_PCTRL_CHANGEABLE|8, 0, &big);
 2042         if (error == 0) {
 2043                 if (big)
 2044                         pages = (void *)(&scsipi_sense.header.big + 1);
 2045                 else
 2046                         pages = (void *)(&scsipi_sense.header.small + 1);
 2047 
 2048                 if (pages->caching_params.flags & CACHING_RCD)
 2049                         bits |= DKCACHE_RCHANGE;
 2050                 if (pages->caching_params.flags & CACHING_WCE)
 2051                         bits |= DKCACHE_WCHANGE;
 2052         }
 2053 
 2054         *bitsp = bits;
 2055 
 2056         return (0);
 2057 }
 2058 
 2059 static int
 2060 sd_setcache(struct sd_softc *sd, int bits)
 2061 {
 2062         struct scsipi_periph *periph = sd->sc_periph;
 2063         struct sd_mode_sense_data scsipi_sense;
 2064         int error;
 2065         uint8_t oflags, byte2 = 0;
 2066         int big;
 2067         union scsi_disk_pages *pages;
 2068 
 2069         if (periph->periph_version < 2)
 2070                 return (EOPNOTSUPP);
 2071 
 2072         memset(&scsipi_sense, 0, sizeof(scsipi_sense));
 2073         error = sd_mode_sense(sd, SMS_DBD, &scsipi_sense,
 2074             sizeof(scsipi_sense.pages.caching_params), 8, 0, &big);
 2075         if (error)
 2076                 return (error);
 2077 
 2078         if (big)
 2079                 pages = (void *)(&scsipi_sense.header.big + 1);
 2080         else
 2081                 pages = (void *)(&scsipi_sense.header.small + 1);
 2082 
 2083         oflags = pages->caching_params.flags;
 2084 
 2085         if (bits & DKCACHE_READ)
 2086                 pages->caching_params.flags &= ~CACHING_RCD;
 2087         else
 2088                 pages->caching_params.flags |= CACHING_RCD;
 2089 
 2090         if (bits & DKCACHE_WRITE)
 2091                 pages->caching_params.flags |= CACHING_WCE;
 2092         else
 2093                 pages->caching_params.flags &= ~CACHING_WCE;
 2094 
 2095         if (oflags == pages->caching_params.flags)
 2096                 return (0);
 2097 
 2098         pages->caching_params.pg_code &= PGCODE_MASK;
 2099 
 2100         if (bits & DKCACHE_SAVE)
 2101                 byte2 |= SMS_SP;
 2102 
 2103         return (sd_mode_select(sd, byte2|SMS_PF, &scsipi_sense,
 2104             sizeof(struct scsi_mode_page_header) +
 2105             pages->caching_params.pg_length, 0, big));
 2106 }

Cache object: e226f4ef5f5c75742a42f4affced1a16


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