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

Cache object: cc08788b9ff27e4a508e6d2c8b732d23


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