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/st.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: st.c,v 1.207.4.1 2008/11/20 02:50:27 snj Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1998, 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  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * Originally written by Julian Elischer (julian@tfs.com)
   34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
   35  *
   36  * TRW Financial Systems, in accordance with their agreement with Carnegie
   37  * Mellon University, makes this software available to CMU to distribute
   38  * or use in any manner that they see fit as long as this message is kept with
   39  * the software. For this reason TFS also grants any other persons or
   40  * organisations permission to use or modify this software.
   41  *
   42  * TFS supplies this software to be publicly redistributed
   43  * on the understanding that TFS is not responsible for the correct
   44  * functioning of this software in any circumstances.
   45  *
   46  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
   47  * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
   48  *
   49  * A lot of rewhacking done by mjacob (mjacob@nas.nasa.gov).
   50  */
   51 
   52 #include <sys/cdefs.h>
   53 __KERNEL_RCSID(0, "$NetBSD: st.c,v 1.207.4.1 2008/11/20 02:50:27 snj Exp $");
   54 
   55 #include "opt_scsi.h"
   56 
   57 #include <sys/param.h>
   58 #include <sys/systm.h>
   59 #include <sys/fcntl.h>
   60 #include <sys/errno.h>
   61 #include <sys/ioctl.h>
   62 #include <sys/malloc.h>
   63 #include <sys/buf.h>
   64 #include <sys/bufq.h>
   65 #include <sys/proc.h>
   66 #include <sys/user.h>
   67 #include <sys/mtio.h>
   68 #include <sys/device.h>
   69 #include <sys/conf.h>
   70 #include <sys/kernel.h>
   71 #include <sys/vnode.h>
   72 #include <sys/iostat.h>
   73 #include <sys/sysctl.h>
   74 
   75 #include <dev/scsipi/scsi_spc.h>
   76 #include <dev/scsipi/scsipi_all.h>
   77 #include <dev/scsipi/scsi_all.h>
   78 #include <dev/scsipi/scsi_tape.h>
   79 #include <dev/scsipi/scsipiconf.h>
   80 #include <dev/scsipi/scsipi_base.h>
   81 #include <dev/scsipi/stvar.h>
   82 
   83 /* Defines for device specific stuff */
   84 #define DEF_FIXED_BSIZE  512
   85 
   86 #define STMODE(z)       ( minor(z)       & 0x03)
   87 #define STDSTY(z)       ((minor(z) >> 2) & 0x03)
   88 #define STUNIT(z)       ((minor(z) >> 4)       )
   89 #define STNMINOR        16
   90 
   91 #define NORMAL_MODE     0
   92 #define NOREW_MODE      1
   93 #define EJECT_MODE      2
   94 #define CTRL_MODE       3
   95 
   96 #ifndef         ST_MOUNT_DELAY
   97 #define         ST_MOUNT_DELAY          0
   98 #endif
   99 
  100 static dev_type_open(stopen);
  101 static dev_type_close(stclose);
  102 static dev_type_read(stread);
  103 static dev_type_write(stwrite);
  104 static dev_type_ioctl(stioctl);
  105 static dev_type_strategy(ststrategy);
  106 static dev_type_dump(stdump);
  107 
  108 const struct bdevsw st_bdevsw = {
  109         stopen, stclose, ststrategy, stioctl, stdump, nosize, D_TAPE
  110 };
  111 
  112 const struct cdevsw st_cdevsw = {
  113         stopen, stclose, stread, stwrite, stioctl,
  114         nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
  115 };
  116 
  117 /*
  118  * Define various devices that we know mis-behave in some way,
  119  * and note how they are bad, so we can correct for them
  120  */
  121 
  122 static const struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
  123         {{T_SEQUENTIAL, T_REMOV,
  124          "        ", "                ", "    "}, {0, 0, {
  125                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  126                 {ST_Q_FORCE_BLKSIZE, 512, QIC_24},      /* minor 4-7 */
  127                 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600}, /* minor 8-11 */
  128                 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250}  /* minor 12-15 */
  129         }}},
  130         {{T_SEQUENTIAL, T_REMOV,
  131          "TANDBERG", " TDC 3600       ", ""},     {0, 12, {
  132                 {0, 0, 0},                              /* minor 0-3 */
  133                 {ST_Q_FORCE_BLKSIZE, 0, QIC_525},       /* minor 4-7 */
  134                 {0, 0, QIC_150},                        /* minor 8-11 */
  135                 {0, 0, QIC_120}                         /* minor 12-15 */
  136         }}},
  137         {{T_SEQUENTIAL, T_REMOV,
  138          "TANDBERG", " TDC 3800       ", ""},     {0, 0, {
  139                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  140                 {0, 0, QIC_525},                        /* minor 4-7 */
  141                 {0, 0, QIC_150},                        /* minor 8-11 */
  142                 {0, 0, QIC_120}                         /* minor 12-15 */
  143         }}},
  144         {{T_SEQUENTIAL, T_REMOV,
  145           "TANDBERG", " SLR5 4/8GB     ", ""},     {0, 0, {
  146                 {ST_Q_FORCE_BLKSIZE, 1024, 0},          /* minor 0-3 */
  147                 {0, 0, 0},                              /* minor 4-7 */
  148                 {0, 0, 0},                              /* minor 8-11 */
  149                 {0, 0, 0}                               /* minor 12-15 */
  150         }}},
  151         /*
  152          * lacking a manual for the 4200, it's not clear what the
  153          * specific density codes should be- the device is a 2.5GB
  154          * capable QIC drive, those density codes aren't readily
  155          * availabel. The 'default' will just have to do.
  156          */
  157         {{T_SEQUENTIAL, T_REMOV,
  158          "TANDBERG", " TDC 4200       ", ""},     {0, 0, {
  159                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  160                 {0, 0, QIC_525},                        /* minor 4-7 */
  161                 {0, 0, QIC_150},                        /* minor 8-11 */
  162                 {0, 0, QIC_120}                         /* minor 12-15 */
  163         }}},
  164         /*
  165          * At least -005 and -007 need this.  I'll assume they all do unless I
  166          * hear otherwise.  - mycroft, 31MAR1994
  167          */
  168         {{T_SEQUENTIAL, T_REMOV,
  169          "ARCHIVE ", "VIPER 2525 25462", ""},     {0, 0, {
  170                 {ST_Q_SENSE_HELP, 0, 0},                /* minor 0-3 */
  171                 {ST_Q_SENSE_HELP, 0, QIC_525},          /* minor 4-7 */
  172                 {0, 0, QIC_150},                        /* minor 8-11 */
  173                 {0, 0, QIC_120}                         /* minor 12-15 */
  174         }}},
  175         /*
  176          * One user reports that this works for his tape drive.  It probably
  177          * needs more work.  - mycroft, 09APR1994
  178          */
  179         {{T_SEQUENTIAL, T_REMOV,
  180          "SANKYO  ", "CP525           ", ""},    {0, 0, {
  181                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  182                 {ST_Q_FORCE_BLKSIZE, 512, QIC_525},     /* minor 4-7 */
  183                 {0, 0, QIC_150},                        /* minor 8-11 */
  184                 {0, 0, QIC_120}                         /* minor 12-15 */
  185         }}},
  186         {{T_SEQUENTIAL, T_REMOV,
  187          "ANRITSU ", "DMT780          ", ""},     {0, 0, {
  188                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  189                 {ST_Q_FORCE_BLKSIZE, 512, QIC_525},     /* minor 4-7 */
  190                 {0, 0, QIC_150},                        /* minor 8-11 */
  191                 {0, 0, QIC_120}                         /* minor 12-15 */
  192         }}},
  193         {{T_SEQUENTIAL, T_REMOV,
  194          "ARCHIVE ", "VIPER 150  21247", ""},     {ST_Q_ERASE_NOIMM, 12, {
  195                 {ST_Q_SENSE_HELP, 0, 0},                /* minor 0-3 */
  196                 {0, 0, QIC_150},                        /* minor 4-7 */
  197                 {0, 0, QIC_120},                        /* minor 8-11 */
  198                 {0, 0, QIC_24}                          /* minor 12-15 */
  199         }}},
  200         {{T_SEQUENTIAL, T_REMOV,
  201          "ARCHIVE ", "VIPER 150  21531", ""},     {ST_Q_ERASE_NOIMM, 12, {
  202                 {ST_Q_SENSE_HELP, 0, 0},                /* minor 0-3 */
  203                 {0, 0, QIC_150},                        /* minor 4-7 */
  204                 {0, 0, QIC_120},                        /* minor 8-11 */
  205                 {0, 0, QIC_24}                          /* minor 12-15 */
  206         }}},
  207         {{T_SEQUENTIAL, T_REMOV,
  208          "WANGTEK ", "5099ES SCSI", ""},          {0, 0, {
  209                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  210                 {0, 0, QIC_11},                         /* minor 4-7 */
  211                 {0, 0, QIC_24},                         /* minor 8-11 */
  212                 {0, 0, QIC_24}                          /* minor 12-15 */
  213         }}},
  214         {{T_SEQUENTIAL, T_REMOV,
  215          "WANGTEK ", "5150ES SCSI", ""},          {0, 0, {
  216                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  217                 {0, 0, QIC_24},                         /* minor 4-7 */
  218                 {0, 0, QIC_120},                        /* minor 8-11 */
  219                 {0, 0, QIC_150}                         /* minor 12-15 */
  220         }}},
  221         {{T_SEQUENTIAL, T_REMOV,
  222          "WANGTEK ", "5525ES SCSI REV7", ""},     {0, 0, {
  223                 {0, 0, 0},                              /* minor 0-3 */
  224                 {ST_Q_BLKSIZE, 0, QIC_525},             /* minor 4-7 */
  225                 {0, 0, QIC_150},                        /* minor 8-11 */
  226                 {0, 0, QIC_120}                         /* minor 12-15 */
  227         }}},
  228         {{T_SEQUENTIAL, T_REMOV,
  229          "WangDAT ", "Model 1300      ", ""},     {0, 0, {
  230                 {0, 0, 0},                              /* minor 0-3 */
  231                 {ST_Q_FORCE_BLKSIZE, 512, DDS},         /* minor 4-7 */
  232                 {ST_Q_FORCE_BLKSIZE, 1024, DDS},        /* minor 8-11 */
  233                 {ST_Q_FORCE_BLKSIZE, 0, DDS}            /* minor 12-15 */
  234         }}},
  235         {{T_SEQUENTIAL, T_REMOV,
  236          "EXABYTE ", "EXB-8200        ", "263H"}, {0, 5, {
  237                 {0, 0, 0},                              /* minor 0-3 */
  238                 {0, 0, 0},                              /* minor 4-7 */
  239                 {0, 0, 0},                              /* minor 8-11 */
  240                 {0, 0, 0}                               /* minor 12-15 */
  241         }}},
  242         {{T_SEQUENTIAL, T_REMOV,
  243          "STK",      "9490",             ""},
  244                                 {ST_Q_FORCE_BLKSIZE, 0, {
  245                 {0, 0, 0},                              /* minor 0-3 */
  246                 {0, 0, 0},                              /* minor 4-7 */
  247                 {0, 0, 0},                              /* minor 8-11 */
  248                 {0, 0, 0}                               /* minor 12-15 */
  249         }}},
  250         {{T_SEQUENTIAL, T_REMOV,
  251          "STK",      "SD-3",             ""},
  252                                 {ST_Q_FORCE_BLKSIZE, 0, {
  253                 {0, 0, 0},                              /* minor 0-3 */
  254                 {0, 0, 0},                              /* minor 4-7 */
  255                 {0, 0, 0},                              /* minor 8-11 */
  256                 {0, 0, 0}                               /* minor 12-15 */
  257         }}},
  258         {{T_SEQUENTIAL, T_REMOV,
  259          "IBM",      "03590",            ""},     {ST_Q_IGNORE_LOADS, 0, {
  260                 {0, 0, 0},                              /* minor 0-3 */
  261                 {0, 0, 0},                              /* minor 4-7 */
  262                 {0, 0, 0},                              /* minor 8-11 */
  263                 {0, 0, 0}                               /* minor 12-15 */
  264         }}},
  265         {{T_SEQUENTIAL, T_REMOV,
  266          "HP      ", "T4000s          ", ""},     {ST_Q_UNIMODAL, 0, {
  267                 {0, 0, QIC_3095},                       /* minor 0-3 */
  268                 {0, 0, QIC_3095},                       /* minor 4-7 */
  269                 {0, 0, QIC_3095},                       /* minor 8-11 */
  270                 {0, 0, QIC_3095},                       /* minor 12-15 */
  271         }}},
  272 #if 0
  273         {{T_SEQUENTIAL, T_REMOV,
  274          "EXABYTE ", "EXB-8200        ", ""},     {0, 12, {
  275                 {0, 0, 0},                              /* minor 0-3 */
  276                 {0, 0, 0},                              /* minor 4-7 */
  277                 {0, 0, 0},                              /* minor 8-11 */
  278                 {0, 0, 0}                               /* minor 12-15 */
  279         }}},
  280 #endif
  281         {{T_SEQUENTIAL, T_REMOV,
  282          "TEAC    ", "MT-2ST/N50      ", ""},     {ST_Q_IGNORE_LOADS, 0, {
  283                 {0, 0, 0},                              /* minor 0-3 */
  284                 {0, 0, 0},                              /* minor 4-7 */
  285                 {0, 0, 0},                              /* minor 8-11 */
  286                 {0, 0, 0}                               /* minor 12-15 */
  287         }}},
  288         {{T_SEQUENTIAL, T_REMOV,
  289          "OnStream", "ADR50 Drive", ""},          {ST_Q_UNIMODAL, 0, {
  290                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 0-3 */
  291                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 4-7 */
  292                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 8-11 */
  293                 {ST_Q_FORCE_BLKSIZE, 512, 0},           /* minor 12-15 */
  294         }}},
  295         {{T_SEQUENTIAL, T_REMOV,
  296          "OnStream DI-30",      "",   "1.0"},  {ST_Q_NOFILEMARKS, 0, {
  297                 {0, 0, 0},                              /* minor 0-3 */
  298                 {0, 0, 0},                              /* minor 4-7 */
  299                 {0, 0, 0},                              /* minor 8-11 */
  300                 {0, 0, 0}                               /* minor 12-15 */
  301         }}},
  302         {{T_SEQUENTIAL, T_REMOV,
  303          "NCR H621", "0-STD-03-46F880 ", ""},     {ST_Q_NOPREVENT, 0, {
  304                 {0, 0, 0},                             /* minor 0-3 */
  305                 {0, 0, 0},                             /* minor 4-7 */
  306                 {0, 0, 0},                             /* minor 8-11 */
  307                 {0, 0, 0}                              /* minor 12-15 */
  308         }}},
  309 };
  310 
  311 #define NOEJECT 0
  312 #define EJECT 1
  313 
  314 static void     st_identify_drive(struct st_softc *,
  315                     struct scsipi_inquiry_pattern *);
  316 static void     st_loadquirks(struct st_softc *);
  317 static int      st_mount_tape(dev_t, int);
  318 static void     st_unmount(struct st_softc *, boolean);
  319 static int      st_decide_mode(struct st_softc *, boolean);
  320 static void     ststart(struct scsipi_periph *);
  321 static void     strestart(void *);
  322 static void     stdone(struct scsipi_xfer *, int);
  323 static int      st_read(struct st_softc *, char *, int, int);
  324 static int      st_space(struct st_softc *, int, u_int, int);
  325 static int      st_write_filemarks(struct st_softc *, int, int);
  326 static int      st_check_eod(struct st_softc *, boolean, int *, int);
  327 static int      st_load(struct st_softc *, u_int, int);
  328 static int      st_rewind(struct st_softc *, u_int, int);
  329 static int      st_interpret_sense(struct scsipi_xfer *);
  330 static int      st_touch_tape(struct st_softc *);
  331 static int      st_erase(struct st_softc *, int full, int flags);
  332 static int      st_rdpos(struct st_softc *, int, u_int32_t *);
  333 static int      st_setpos(struct st_softc *, int, u_int32_t *);
  334 
  335 static const struct scsipi_periphsw st_switch = {
  336         st_interpret_sense,
  337         ststart,
  338         NULL,
  339         stdone
  340 };
  341 
  342 #if     defined(ST_ENABLE_EARLYWARN)
  343 #define ST_INIT_FLAGS   ST_EARLYWARN
  344 #else
  345 #define ST_INIT_FLAGS   0
  346 #endif
  347 
  348 /*
  349  * The routine called by the low level scsi routine when it discovers
  350  * A device suitable for this driver
  351  */
  352 void
  353 stattach(struct device *parent, struct st_softc *st, void *aux)
  354 {
  355         struct scsipibus_attach_args *sa = aux;
  356         struct scsipi_periph *periph = sa->sa_periph;
  357 
  358         SC_DEBUG(periph, SCSIPI_DB2, ("stattach: "));
  359 
  360         /*
  361          * Store information needed to contact our base driver
  362          */
  363         st->sc_periph = periph;
  364         periph->periph_dev = &st->sc_dev;
  365         periph->periph_switch = &st_switch;
  366 
  367         /*
  368          * Set initial flags
  369          */
  370 
  371         st->flags = ST_INIT_FLAGS;
  372 
  373         /*
  374          * Set up the buf queue for this device
  375          */
  376         bufq_alloc(&st->buf_queue, "fcfs", 0);
  377 
  378         callout_init(&st->sc_callout, 0);
  379 
  380         /*
  381          * Check if the drive is a known criminal and take
  382          * Any steps needed to bring it into line
  383          */
  384         st_identify_drive(st, &sa->sa_inqbuf);
  385         /*
  386          * Use the subdriver to request information regarding the drive.
  387          */
  388         printf("\n");
  389         printf("%s: %s", device_xname(&st->sc_dev), st->quirkdata ? "quirks apply, " : "");
  390         if (scsipi_test_unit_ready(periph,
  391             XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) ||
  392             st->ops(st, ST_OPS_MODESENSE,
  393             XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE))
  394                 printf("drive empty\n");
  395         else {
  396                 printf("density code %d, ", st->media_density);
  397                 if (st->media_blksize > 0)
  398                         printf("%d-byte", st->media_blksize);
  399                 else
  400                         printf("variable");
  401                 printf(" blocks, write-%s\n",
  402                     (st->flags & ST_READONLY) ? "protected" : "enabled");
  403         }
  404 
  405         st->stats = iostat_alloc(IOSTAT_TAPE, parent, device_xname(&st->sc_dev));
  406 
  407 #if NRND > 0
  408         rnd_attach_source(&st->rnd_source, device_xname(&st->sc_dev),
  409                           RND_TYPE_TAPE, 0);
  410 #endif
  411 }
  412 
  413 int
  414 stactivate(struct device *self, enum devact act)
  415 {
  416         int rv = 0;
  417 
  418         switch (act) {
  419         case DVACT_ACTIVATE:
  420                 rv = EOPNOTSUPP;
  421                 break;
  422 
  423         case DVACT_DEACTIVATE:
  424                 /*
  425                  * Nothing to do; we key off the device's DVF_ACTIVE.
  426                  */
  427                 break;
  428         }
  429         return (rv);
  430 }
  431 
  432 int
  433 stdetach(struct device *self, int flags)
  434 {
  435         struct st_softc *st = device_private(self);
  436         int s, bmaj, cmaj, mn;
  437 
  438         /* locate the major number */
  439         bmaj = bdevsw_lookup_major(&st_bdevsw);
  440         cmaj = cdevsw_lookup_major(&st_cdevsw);
  441 
  442         /* kill any pending restart */
  443         callout_stop(&st->sc_callout);
  444 
  445         s = splbio();
  446 
  447         /* Kill off any queued buffers. */
  448         bufq_drain(st->buf_queue);
  449 
  450         bufq_free(st->buf_queue);
  451 
  452         /* Kill off any pending commands. */
  453         scsipi_kill_pending(st->sc_periph);
  454 
  455         splx(s);
  456 
  457         /* Nuke the vnodes for any open instances */
  458         mn = STUNIT(device_unit(self));
  459         vdevgone(bmaj, mn, mn+STNMINOR-1, VBLK);
  460         vdevgone(cmaj, mn, mn+STNMINOR-1, VCHR);
  461 
  462         iostat_free(st->stats);
  463 
  464 #if NRND > 0
  465         /* Unhook the entropy source. */
  466         rnd_detach_source(&st->rnd_source);
  467 #endif
  468 
  469         return (0);
  470 }
  471 
  472 /*
  473  * Use the inquiry routine in 'scsi_base' to get drive info so we can
  474  * Further tailor our behaviour.
  475  */
  476 static void
  477 st_identify_drive(struct st_softc *st, struct scsipi_inquiry_pattern *inqbuf)
  478 {
  479         const struct st_quirk_inquiry_pattern *finger;
  480         int priority;
  481 
  482         finger = scsipi_inqmatch(inqbuf,
  483             st_quirk_patterns,
  484             sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
  485             sizeof(st_quirk_patterns[0]), &priority);
  486         if (priority != 0) {
  487                 st->quirkdata = &finger->quirkdata;
  488                 st->drive_quirks = finger->quirkdata.quirks;
  489                 st->quirks = finger->quirkdata.quirks;  /* start value */
  490                 st->page_0_size = finger->quirkdata.page_0_size;
  491                 st_loadquirks(st);
  492         }
  493 }
  494 
  495 /*
  496  * initialise the subdevices to the default (QUIRK) state.
  497  * this will remove any setting made by the system operator or previous
  498  * operations.
  499  */
  500 static void
  501 st_loadquirks(struct st_softc *st)
  502 {
  503         int i;
  504         const struct    modes *mode;
  505         struct  modes *mode2;
  506 
  507         mode = st->quirkdata->modes;
  508         mode2 = st->modes;
  509         for (i = 0; i < 4; i++) {
  510                 memset(mode2, 0, sizeof(struct modes));
  511                 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
  512                     DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
  513                     DENSITY_SET_BY_USER);
  514                 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
  515                         mode2->blksize = mode->blksize;
  516                         st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
  517                 }
  518                 if (mode->density) {
  519                         mode2->density = mode->density;
  520                         st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
  521                 }
  522                 mode2->quirks |= mode->quirks;
  523                 mode++;
  524                 mode2++;
  525         }
  526 }
  527 
  528 /*
  529  * open the device.
  530  */
  531 static int
  532 stopen(dev_t dev, int flags, int mode, struct lwp *l)
  533 {
  534         u_int stmode, dsty;
  535         int error, sflags, unit, tries, ntries;
  536         struct st_softc *st;
  537         struct scsipi_periph *periph;
  538         struct scsipi_adapter *adapt;
  539 
  540         unit = STUNIT(dev);
  541         st = device_lookup_private(&st_cd, unit);
  542         if (st == NULL)
  543                 return (ENXIO);
  544 
  545         stmode = STMODE(dev);
  546         dsty = STDSTY(dev);
  547 
  548         periph = st->sc_periph;
  549         adapt = periph->periph_channel->chan_adapter;
  550 
  551         SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
  552             unit, st_cd.cd_ndevs));
  553 
  554 
  555         /*
  556          * Only allow one at a time
  557          */
  558         if (periph->periph_flags & PERIPH_OPEN) {
  559                 aprint_error_dev(&st->sc_dev, "already open\n");
  560                 return (EBUSY);
  561         }
  562 
  563         if ((error = scsipi_adapter_addref(adapt)) != 0)
  564                 return (error);
  565 
  566         /*
  567          * clear any latched errors.
  568          */
  569         st->mt_resid = 0;
  570         st->mt_erreg = 0;
  571         st->asc = 0;
  572         st->ascq = 0;
  573 
  574         /*
  575          * Catch any unit attention errors. Be silent about this
  576          * unless we're already mounted. We ignore media change
  577          * if we're in control mode or not mounted yet.
  578          */
  579         if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
  580 #ifdef SCSIDEBUG
  581                 sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
  582 #else
  583                 sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
  584 #endif
  585         } else
  586                 sflags = 0;
  587 
  588         /*
  589          * If we're already mounted or we aren't configured for
  590          * a mount delay, only try a test unit ready once. Otherwise,
  591          * try up to ST_MOUNT_DELAY times with a rest interval of
  592          * one second between each try.
  593          */
  594 
  595         if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) {
  596                 ntries = 1;
  597         } else {
  598                 ntries = ST_MOUNT_DELAY;
  599         }
  600 
  601         for (error = tries = 0; tries < ntries; tries++) {
  602                 int slpintr, oflags;
  603 
  604                 /*
  605                  * If we had no error, or we're opening the control mode
  606                  * device, we jump out right away.
  607                  */
  608 
  609                 error = scsipi_test_unit_ready(periph, sflags);
  610                 if (error == 0 || stmode == CTRL_MODE) {
  611                         break;
  612                 }
  613 
  614                 /*
  615                  * We had an error.
  616                  *
  617                  * If we're already mounted or we aren't configured for
  618                  * a mount delay, or the error isn't a NOT READY error,
  619                  * skip to the error exit now.
  620                  */
  621                 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
  622                     (st->mt_key != SKEY_NOT_READY)) {
  623                         goto bad;
  624                 }
  625 
  626                 /*
  627                  * clear any latched errors.
  628                  */
  629                 st->mt_resid = 0;
  630                 st->mt_erreg = 0;
  631                 st->asc = 0;
  632                 st->ascq = 0;
  633 
  634                 /*
  635                  * Fake that we have the device open so
  636                  * we block other apps from getting in.
  637                  */
  638 
  639                 oflags = periph->periph_flags;
  640                 periph->periph_flags |= PERIPH_OPEN;
  641 
  642                 slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0);
  643 
  644                 periph->periph_flags = oflags;  /* restore flags */
  645                 if (slpintr) {
  646                         goto bad;
  647                 }
  648         }
  649 
  650 
  651         /*
  652          * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
  653          * been opened to set defaults and perform other, usually non-I/O
  654          * related, operations. In this case, do a quick check to see
  655          * whether the unit actually had a tape loaded (this will be known
  656          * as to whether or not we got a NOT READY for the above
  657          * unit attention). If a tape is there, go do a mount sequence.
  658          */
  659         if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) {
  660                 periph->periph_flags |= PERIPH_OPEN;
  661                 return (0);
  662         }
  663 
  664         /*
  665          * If we get this far and had an error set, that means we failed
  666          * to pass the 'test unit ready' test for the non-controlmode device,
  667          * so we bounce the open.
  668          */
  669 
  670         if (error)
  671                 return (error);
  672 
  673         /*
  674          * Else, we're now committed to saying we're open.
  675          */
  676 
  677         periph->periph_flags |= PERIPH_OPEN; /* unit attn are now errors */
  678 
  679         /*
  680          * If it's a different mode, or if the media has been
  681          * invalidated, unmount the tape from the previous
  682          * session but continue with open processing
  683          */
  684         if (st->last_dsty != dsty ||
  685             (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
  686                 st_unmount(st, NOEJECT);
  687 
  688         /*
  689          * If we are not mounted, then we should start a new
  690          * mount session.
  691          */
  692         if (!(st->flags & ST_MOUNTED)) {
  693                 if ((error = st_mount_tape(dev, flags)) != 0)
  694                         goto bad;
  695                 st->last_dsty = dsty;
  696         }
  697         if (!(st->quirks & ST_Q_NOPREVENT)) {
  698                 scsipi_prevent(periph, SPAMR_PREVENT_DT,
  699                     XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
  700         }
  701 
  702         SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
  703         return (0);
  704 
  705 bad:
  706         st_unmount(st, NOEJECT);
  707         scsipi_adapter_delref(adapt);
  708         periph->periph_flags &= ~PERIPH_OPEN;
  709         return (error);
  710 }
  711 
  712 /*
  713  * close the device.. only called if we are the LAST
  714  * occurence of an open device
  715  */
  716 static int
  717 stclose(dev_t dev, int flags, int mode, struct lwp *l)
  718 {
  719         int stxx, error = 0;
  720         struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
  721         struct scsipi_periph *periph = st->sc_periph;
  722         struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
  723 
  724         SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
  725 
  726         /*
  727          * Make sure that a tape opened in write-only mode will have
  728          * file marks written on it when closed, even if not written to.
  729          *
  730          * This is for SUN compatibility. Actually, the Sun way of
  731          * things is to:
  732          *
  733          *      only write filemarks if there are fmks to be written and
  734          *              - open for write (possibly read/write)
  735          *              - the last operation was a write
  736          *      or:
  737          *              - opened for wronly
  738          *              - no data was written (including filemarks)
  739          */
  740 
  741         stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
  742         if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
  743             ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
  744                 int nm;
  745                 error = st_check_eod(st, FALSE, &nm, 0);
  746         }
  747 
  748         /*
  749          * Allow robots to eject tape if needed.
  750          */
  751         scsipi_prevent(periph, SPAMR_ALLOW,
  752             XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
  753 
  754         switch (STMODE(dev)) {
  755         case NORMAL_MODE:
  756                 st_unmount(st, NOEJECT);
  757                 break;
  758         case NOREW_MODE:
  759         case CTRL_MODE:
  760                 /*
  761                  * Leave mounted unless media seems to have been removed.
  762                  *
  763                  * Otherwise, if we're to terminate a tape with more than one
  764                  * filemark [ and because we're not rewinding here ], backspace
  765                  * one filemark so that later appends will see an unbroken
  766                  * sequence of:
  767                  *
  768                  *      file - FMK - file - FMK ... file - FMK FMK (EOM)
  769                  */
  770                 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
  771                         st_unmount(st, NOEJECT);
  772                 } else if (error == 0) {
  773                         /*
  774                          * ST_WRITTEN was preserved from above.
  775                          *
  776                          * All we need to know here is:
  777                          *
  778                          *      Were we writing this tape and was the last
  779                          *      operation a write?
  780                          *
  781                          *      Are there supposed to be 2FM at EOD?
  782                          *
  783                          * If both statements are true, then we backspace
  784                          * one filemark.
  785                          */
  786                         stxx |= (st->flags & ST_2FM_AT_EOD);
  787                         if ((flags & FWRITE) != 0 &&
  788                             (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
  789                                 error = st_space(st, -1, SP_FILEMARKS, 0);
  790                         }
  791                 }
  792                 break;
  793         case EJECT_MODE:
  794                 st_unmount(st, EJECT);
  795                 break;
  796         }
  797 
  798         scsipi_wait_drain(periph);
  799 
  800         scsipi_adapter_delref(adapt);
  801         periph->periph_flags &= ~PERIPH_OPEN;
  802 
  803         return (error);
  804 }
  805 
  806 /*
  807  * Start a new mount session.
  808  * Copy in all the default parameters from the selected device mode.
  809  * and try guess any that seem to be defaulted.
  810  */
  811 static int
  812 st_mount_tape(dev_t dev, int flags)
  813 {
  814         int unit;
  815         u_int dsty;
  816         struct st_softc *st;
  817         struct scsipi_periph *periph;
  818         int error = 0;
  819 
  820         unit = STUNIT(dev);
  821         dsty = STDSTY(dev);
  822         st = device_lookup_private(&st_cd, unit);
  823         periph = st->sc_periph;
  824 
  825         if (st->flags & ST_MOUNTED)
  826                 return (0);
  827 
  828         SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
  829         st->flags |= ST_NEW_MOUNT;
  830         st->quirks = st->drive_quirks | st->modes[dsty].quirks;
  831         /*
  832          * If the media is new, then make sure we give it a chance to
  833          * to do a 'load' instruction.  (We assume it is new.)
  834          */
  835         if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
  836                 return (error);
  837         /*
  838          * Throw another dummy instruction to catch
  839          * 'Unit attention' errors. Many drives give
  840          * these after doing a Load instruction (with
  841          * the MEDIUM MAY HAVE CHANGED asc/ascq).
  842          */
  843         scsipi_test_unit_ready(periph, XS_CTL_SILENT);  /* XXX */
  844 
  845         /*
  846          * Some devices can't tell you much until they have been
  847          * asked to look at the media. This quirk does this.
  848          */
  849         if (st->quirks & ST_Q_SENSE_HELP)
  850                 if ((error = st_touch_tape(st)) != 0)
  851                         return (error);
  852         /*
  853          * Load the physical device parameters
  854          * loads: blkmin, blkmax
  855          */
  856         if ((error = st->ops(st, ST_OPS_RBL, 0)) != 0)
  857                 return (error);
  858         /*
  859          * Load the media dependent parameters
  860          * includes: media_blksize,media_density,numblks
  861          * As we have a tape in, it should be reflected here.
  862          * If not you may need the "quirk" above.
  863          */
  864         if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
  865                 return (error);
  866         /*
  867          * If we have gained a permanent density from somewhere,
  868          * then use it in preference to the one supplied by
  869          * default by the driver.
  870          */
  871         if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
  872                 st->density = st->modes[dsty].density;
  873         else
  874                 st->density = st->media_density;
  875         /*
  876          * If we have gained a permanent blocksize
  877          * then use it in preference to the one supplied by
  878          * default by the driver.
  879          */
  880         st->flags &= ~ST_FIXEDBLOCKS;
  881         if (st->modeflags[dsty] &
  882             (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
  883                 st->blksize = st->modes[dsty].blksize;
  884                 if (st->blksize)
  885                         st->flags |= ST_FIXEDBLOCKS;
  886         } else {
  887                 if ((error = st_decide_mode(st, FALSE)) != 0)
  888                         return (error);
  889         }
  890         if ((error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
  891                 /* ATAPI will return ENODEV for this, and this may be OK */
  892                 if (error != ENODEV) {
  893                         aprint_error_dev(&st->sc_dev, "cannot set selected mode\n");
  894                         return (error);
  895                 }
  896         }
  897         st->flags &= ~ST_NEW_MOUNT;
  898         st->flags |= ST_MOUNTED;
  899         periph->periph_flags |= PERIPH_MEDIA_LOADED;    /* move earlier? */
  900         st->blkno = st->fileno = (daddr_t) 0;
  901         return (0);
  902 }
  903 
  904 /*
  905  * End the present mount session.
  906  * Rewind, and optionally eject the tape.
  907  * Reset various flags to indicate that all new
  908  * operations require another mount operation
  909  */
  910 static void
  911 st_unmount(struct st_softc *st, boolean eject)
  912 {
  913         struct scsipi_periph *periph = st->sc_periph;
  914         int nmarks;
  915 
  916         if ((st->flags & ST_MOUNTED) == 0)
  917                 return;
  918         SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
  919         st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
  920         st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
  921 
  922         /*
  923          * Section 9.3.3 of the SCSI specs states that a device shall return
  924          * the density value specified in the last succesfull MODE SELECT
  925          * after an unload operation, in case it is not able to
  926          * automatically determine the density of the new medium.
  927          *
  928          * So we instruct the device to use the default density, which will
  929          * prevent the use of stale density values (in particular,
  930          * in st_touch_tape().
  931          */
  932         st->density = 0;
  933         if (st->ops(st, ST_OPS_MODESELECT, 0) != 0) {
  934                 aprint_error_dev(&st->sc_dev, "WARNING: cannot revert to default density\n");
  935         }
  936 
  937         if (eject) {
  938                 if (!(st->quirks & ST_Q_NOPREVENT)) {
  939                         scsipi_prevent(periph, SPAMR_ALLOW,
  940                             XS_CTL_IGNORE_ILLEGAL_REQUEST |
  941                             XS_CTL_IGNORE_NOT_READY);
  942                 }
  943                 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
  944                 st->blkno = st->fileno = (daddr_t) -1;
  945         } else {
  946                 st->blkno = st->fileno = (daddr_t) 0;
  947         }
  948         st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
  949         periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
  950 }
  951 
  952 /*
  953  * Given all we know about the device, media, mode, 'quirks' and
  954  * initial operation, make a decision as to how we should be set
  955  * to run (regarding blocking and EOD marks)
  956  */
  957 int
  958 st_decide_mode(struct st_softc *st, boolean first_read)
  959 {
  960 
  961         SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
  962 
  963         /*
  964          * If the drive can only handle fixed-length blocks and only at
  965          * one size, perhaps we should just do that.
  966          */
  967         if (st->blkmin && (st->blkmin == st->blkmax)) {
  968                 st->flags |= ST_FIXEDBLOCKS;
  969                 st->blksize = st->blkmin;
  970                 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
  971                     ("blkmin == blkmax of %d\n", st->blkmin));
  972                 goto done;
  973         }
  974         /*
  975          * If the tape density mandates (or even suggests) use of fixed
  976          * or variable-length blocks, comply.
  977          */
  978         switch (st->density) {
  979         case HALFINCH_800:
  980         case HALFINCH_1600:
  981         case HALFINCH_6250:
  982         case DDS:
  983                 st->flags &= ~ST_FIXEDBLOCKS;
  984                 st->blksize = 0;
  985                 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
  986                     ("density specified variable\n"));
  987                 goto done;
  988         case QIC_11:
  989         case QIC_24:
  990         case QIC_120:
  991         case QIC_150:
  992         case QIC_525:
  993         case QIC_1320:
  994         case QIC_3095:
  995         case QIC_3220:
  996                 st->flags |= ST_FIXEDBLOCKS;
  997                 if (st->media_blksize > 0)
  998                         st->blksize = st->media_blksize;
  999                 else
 1000                         st->blksize = DEF_FIXED_BSIZE;
 1001                 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
 1002                     ("density specified fixed\n"));
 1003                 goto done;
 1004         }
 1005         /*
 1006          * If we're about to read the tape, perhaps we should choose
 1007          * fixed or variable-length blocks and block size according to
 1008          * what the drive found on the tape.
 1009          */
 1010         if (first_read &&
 1011             (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
 1012             (st->media_blksize == DEF_FIXED_BSIZE) ||
 1013             (st->media_blksize == 1024))) {
 1014                 if (st->media_blksize > 0)
 1015                         st->flags |= ST_FIXEDBLOCKS;
 1016                 else
 1017                         st->flags &= ~ST_FIXEDBLOCKS;
 1018                 st->blksize = st->media_blksize;
 1019                 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
 1020                     ("Used media_blksize of %d\n", st->media_blksize));
 1021                 goto done;
 1022         }
 1023         /*
 1024          * We're getting no hints from any direction.  Choose variable-
 1025          * length blocks arbitrarily.
 1026          */
 1027         st->flags &= ~ST_FIXEDBLOCKS;
 1028         st->blksize = 0;
 1029         SC_DEBUG(st->sc_periph, SCSIPI_DB3,
 1030             ("Give up and default to variable mode\n"));
 1031 
 1032 done:
 1033         /*
 1034          * Decide whether or not to write two file marks to signify end-
 1035          * of-data.  Make the decision as a function of density.  If
 1036          * the decision is not to use a second file mark, the SCSI BLANK
 1037          * CHECK condition code will be recognized as end-of-data when
 1038          * first read.
 1039          * (I think this should be a by-product of fixed/variable..julian)
 1040          */
 1041         switch (st->density) {
 1042 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
 1043         case QIC_11:
 1044         case QIC_24:
 1045         case QIC_120:
 1046         case QIC_150:
 1047         case QIC_525:
 1048         case QIC_1320:
 1049         case QIC_3095:
 1050         case QIC_3220:
 1051                 st->flags &= ~ST_2FM_AT_EOD;
 1052                 break;
 1053         default:
 1054                 st->flags |= ST_2FM_AT_EOD;
 1055         }
 1056         return (0);
 1057 }
 1058 
 1059 /*
 1060  * Actually translate the requested transfer into
 1061  * one the physical driver can understand
 1062  * The transfer is described by a buf and will include
 1063  * only one physical transfer.
 1064  */
 1065 static void
 1066 ststrategy(struct buf *bp)
 1067 {
 1068         struct st_softc *st = device_lookup_private(&st_cd, STUNIT(bp->b_dev));
 1069         int s;
 1070 
 1071         SC_DEBUG(st->sc_periph, SCSIPI_DB1,
 1072             ("ststrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
 1073         /*
 1074          * If it's a null transfer, return immediately
 1075          */
 1076         if (bp->b_bcount == 0)
 1077                 goto abort;
 1078 
 1079         /* If offset is negative, error */
 1080         if (bp->b_blkno < 0) {
 1081                 bp->b_error = EINVAL;
 1082                 goto abort;
 1083         }
 1084 
 1085         /*
 1086          * Odd sized request on fixed drives are verboten
 1087          */
 1088         if (st->flags & ST_FIXEDBLOCKS) {
 1089                 if (bp->b_bcount % st->blksize) {
 1090                         aprint_error_dev(&st->sc_dev, "bad request, must be multiple of %d\n",
 1091                             st->blksize);
 1092                         bp->b_error = EIO;
 1093                         goto abort;
 1094                 }
 1095         }
 1096         /*
 1097          * as are out-of-range requests on variable drives.
 1098          */
 1099         else if (bp->b_bcount < st->blkmin ||
 1100             (st->blkmax && bp->b_bcount > st->blkmax)) {
 1101                 aprint_error_dev(&st->sc_dev, "bad request, must be between %d and %d\n",
 1102                     st->blkmin, st->blkmax);
 1103                 bp->b_error = EIO;
 1104                 goto abort;
 1105         }
 1106         s = splbio();
 1107 
 1108         /*
 1109          * Place it in the queue of activities for this tape
 1110          * at the end (a bit silly because we only have on user..
 1111          * (but it could fork()))
 1112          */
 1113         BUFQ_PUT(st->buf_queue, bp);
 1114 
 1115         /*
 1116          * Tell the device to get going on the transfer if it's
 1117          * not doing anything, otherwise just wait for completion
 1118          * (All a bit silly if we're only allowing 1 open but..)
 1119          */
 1120         ststart(st->sc_periph);
 1121 
 1122         splx(s);
 1123         return;
 1124 abort:
 1125         /*
 1126          * Reset the residue because we didn't do anything,
 1127          * and send the buffer back as done.
 1128          */
 1129         bp->b_resid = bp->b_bcount;
 1130         biodone(bp);
 1131         return;
 1132 }
 1133 
 1134 /*
 1135  * ststart looks to see if there is a buf waiting for the device
 1136  * and that the device is not already busy. If both are true,
 1137  * It dequeues the buf and creates a scsi command to perform the
 1138  * transfer required. The transfer request will call scsipi_done
 1139  * on completion, which will in turn call this routine again
 1140  * so that the next queued transfer is performed.
 1141  * The bufs are queued by the strategy routine (ststrategy)
 1142  *
 1143  * This routine is also called after other non-queued requests
 1144  * have been made of the scsi driver, to ensure that the queue
 1145  * continues to be drained.
 1146  * ststart() is called at splbio
 1147  */
 1148 static void
 1149 ststart(struct scsipi_periph *periph)
 1150 {
 1151         struct st_softc *st = (void *)periph->periph_dev;
 1152         struct buf *bp;
 1153         struct scsi_rw_tape cmd;
 1154         struct scsipi_xfer *xs;
 1155         int flags, error;
 1156 
 1157         SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
 1158         /*
 1159          * See if there is a buf to do and we are not already
 1160          * doing one
 1161          */
 1162         while (periph->periph_active < periph->periph_openings) {
 1163                 /* if a special awaits, let it proceed first */
 1164                 if (periph->periph_flags & PERIPH_WAITING) {
 1165                         periph->periph_flags &= ~PERIPH_WAITING;
 1166                         wakeup((void *)periph);
 1167                         return;
 1168                 }
 1169 
 1170                 /*
 1171                  * If the device has been unmounted by the user
 1172                  * then throw away all requests until done.
 1173                  */
 1174                 if (__predict_false((st->flags & ST_MOUNTED) == 0 ||
 1175                     (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
 1176                         if ((bp = BUFQ_GET(st->buf_queue)) != NULL) {
 1177                                 /* make sure that one implies the other.. */
 1178                                 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
 1179                                 bp->b_error = EIO;
 1180                                 bp->b_resid = bp->b_bcount;
 1181                                 biodone(bp);
 1182                                 continue;
 1183                         } else {
 1184                                 return;
 1185                         }
 1186                 }
 1187 
 1188                 if ((bp = BUFQ_PEEK(st->buf_queue)) == NULL)
 1189                         return;
 1190 
 1191                 iostat_busy(st->stats);
 1192 
 1193                 /*
 1194                  * only FIXEDBLOCK devices have pending I/O or space operations.
 1195                  */
 1196                 if (st->flags & ST_FIXEDBLOCKS) {
 1197                         /*
 1198                          * If we are at a filemark but have not reported it yet
 1199                          * then we should report it now
 1200                          */
 1201                         if (st->flags & ST_AT_FILEMARK) {
 1202                                 if ((bp->b_flags & B_READ) == B_WRITE) {
 1203                                         /*
 1204                                          * Handling of ST_AT_FILEMARK in
 1205                                          * st_space will fill in the right file
 1206                                          * mark count.
 1207                                          * Back up over filemark
 1208                                          */
 1209                                         if (st_space(st, 0, SP_FILEMARKS, 0)) {
 1210                                                 BUFQ_GET(st->buf_queue);
 1211                                                 bp->b_error = EIO;
 1212                                                 bp->b_resid = bp->b_bcount;
 1213                                                 biodone(bp);
 1214                                                 continue;
 1215                                         }
 1216                                 } else {
 1217                                         BUFQ_GET(st->buf_queue);
 1218                                         bp->b_resid = bp->b_bcount;
 1219                                         bp->b_error = 0;
 1220                                         st->flags &= ~ST_AT_FILEMARK;
 1221                                         biodone(bp);
 1222                                         continue;       /* seek more work */
 1223                                 }
 1224                         }
 1225                 }
 1226                 /*
 1227                  * If we are at EOM but have not reported it
 1228                  * yet then we should report it now.
 1229                  */
 1230                 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
 1231                         BUFQ_GET(st->buf_queue);
 1232                         bp->b_resid = bp->b_bcount;
 1233                         if (st->flags & ST_EIO_PENDING)
 1234                                 bp->b_error = EIO;
 1235                         st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
 1236                         biodone(bp);
 1237                         continue;       /* seek more work */
 1238                 }
 1239 
 1240                 /*
 1241                  * Fill out the scsi command
 1242                  */
 1243                 memset(&cmd, 0, sizeof(cmd));
 1244                 flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
 1245                 if ((bp->b_flags & B_READ) == B_WRITE) {
 1246                         cmd.opcode = WRITE;
 1247                         st->flags &= ~ST_FM_WRITTEN;
 1248                         flags |= XS_CTL_DATA_OUT;
 1249                 } else {
 1250                         cmd.opcode = READ;
 1251                         flags |= XS_CTL_DATA_IN;
 1252                 }
 1253 
 1254                 /*
 1255                  * Handle "fixed-block-mode" tape drives by using the
 1256                  * block count instead of the length.
 1257                  */
 1258                 if (st->flags & ST_FIXEDBLOCKS) {
 1259                         cmd.byte2 |= SRW_FIXED;
 1260                         _lto3b(bp->b_bcount / st->blksize, cmd.len);
 1261                 } else
 1262                         _lto3b(bp->b_bcount, cmd.len);
 1263 
 1264                 /*
 1265                  * Clear 'position updated' indicator
 1266                  */
 1267                 st->flags &= ~ST_POSUPDATED;
 1268 
 1269                 /*
 1270                  * go ask the adapter to do all this for us
 1271                  */
 1272                 xs = scsipi_make_xs(periph,
 1273                     (struct scsipi_generic *)&cmd, sizeof(cmd),
 1274                     (u_char *)bp->b_data, bp->b_bcount,
 1275                     0, ST_IO_TIME, bp, flags);
 1276                 if (__predict_false(xs == NULL)) {
 1277                         /*
 1278                          * out of memory. Keep this buffer in the queue, and
 1279                          * retry later.
 1280                          */
 1281                         callout_reset(&st->sc_callout, hz / 2, strestart,
 1282                             periph);
 1283                         return;
 1284                 }
 1285                 /*
 1286                  * need to dequeue the buffer before queuing the command,
 1287                  * because cdstart may be called recursively from the
 1288                  * HBA driver
 1289                  */
 1290 #ifdef DIAGNOSTIC
 1291                 if (BUFQ_GET(st->buf_queue) != bp)
 1292                         panic("ststart(): dequeued wrong buf");
 1293 #else
 1294                 BUFQ_GET(st->buf_queue);
 1295 #endif
 1296                 error = scsipi_execute_xs(xs);
 1297                 /* with a scsipi_xfer preallocated, scsipi_command can't fail */
 1298                 KASSERT(error == 0);
 1299         } /* go back and see if we can cram more work in.. */
 1300 }
 1301 
 1302 static void
 1303 strestart(void *v)
 1304 {
 1305         int s = splbio();
 1306         ststart((struct scsipi_periph *)v);
 1307         splx(s);
 1308 }
 1309 
 1310 
 1311 static void
 1312 stdone(struct scsipi_xfer *xs, int error)
 1313 {
 1314         struct st_softc *st = (void *)xs->xs_periph->periph_dev;
 1315         struct buf *bp = xs->bp;
 1316 
 1317         if (bp) {
 1318                 bp->b_error = error;
 1319                 bp->b_resid = xs->resid;
 1320                 /*
 1321                  * buggy device ? A SDLT320 can report an info
 1322                  * field of 0x3de8000 on a Media Error/Write Error
 1323                  * for this CBD: 0x0a 00 00 80 00 00
 1324                  */
 1325                 if (bp->b_resid > bp->b_bcount || bp->b_resid < 0)
 1326                         bp->b_resid = bp->b_bcount;
 1327 
 1328                 if ((bp->b_flags & B_READ) == B_WRITE)
 1329                         st->flags |= ST_WRITTEN;
 1330                 else
 1331                         st->flags &= ~ST_WRITTEN;
 1332 
 1333                 iostat_unbusy(st->stats, bp->b_bcount,
 1334                              ((bp->b_flags & B_READ) == B_READ));
 1335 
 1336 #if NRND > 0
 1337                 rnd_add_uint32(&st->rnd_source, bp->b_blkno);
 1338 #endif
 1339 
 1340                 if ((st->flags & ST_POSUPDATED) == 0) {
 1341                         if (error) {
 1342                                 st->fileno = st->blkno = -1;
 1343                         } else if (st->blkno != -1) {
 1344                                 if (st->flags & ST_FIXEDBLOCKS)
 1345                                         st->blkno +=
 1346                                             (bp->b_bcount / st->blksize);
 1347                                 else
 1348                                         st->blkno++;
 1349                         }
 1350                 }
 1351 
 1352                 biodone(bp);
 1353         }
 1354 }
 1355 
 1356 static int
 1357 stread(dev_t dev, struct uio *uio, int iomode)
 1358 {
 1359         struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
 1360 
 1361         return (physio(ststrategy, NULL, dev, B_READ,
 1362             st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
 1363 }
 1364 
 1365 static int
 1366 stwrite(dev_t dev, struct uio *uio, int iomode)
 1367 {
 1368         struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
 1369 
 1370         return (physio(ststrategy, NULL, dev, B_WRITE,
 1371             st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
 1372 }
 1373 
 1374 /*
 1375  * Perform special action on behalf of the user;
 1376  * knows about the internals of this device
 1377  */
 1378 static int
 1379 stioctl(dev_t dev, u_long cmd, void *arg, int flag, struct lwp *l)
 1380 {
 1381         int error = 0;
 1382         int unit;
 1383         int number, nmarks, dsty;
 1384         int flags;
 1385         struct st_softc *st;
 1386         int hold_blksize;
 1387         u_int8_t hold_density;
 1388         struct mtop *mt = (struct mtop *) arg;
 1389 
 1390         /*
 1391          * Find the device that the user is talking about
 1392          */
 1393         flags = 0;              /* give error messages, act on errors etc. */
 1394         unit = STUNIT(dev);
 1395         dsty = STDSTY(dev);
 1396         st = device_lookup_private(&st_cd, unit);
 1397         hold_blksize = st->blksize;
 1398         hold_density = st->density;
 1399 
 1400         switch ((u_int) cmd) {
 1401 
 1402         case MTIOCGET: {
 1403                 struct mtget *g = (struct mtget *) arg;
 1404                 /*
 1405                  * (to get the current state of READONLY)
 1406                  */
 1407                 error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
 1408                 if (error) {
 1409                         /*
 1410                          * Ignore the error if in control mode;
 1411                          * this is mandated by st(4).
 1412                          */
 1413                         if (STMODE(dev) != CTRL_MODE)
 1414                                 break;
 1415                         error = 0;
 1416                 }
 1417                 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
 1418                 memset(g, 0, sizeof(struct mtget));
 1419                 g->mt_type = 0x7;       /* Ultrix compat *//*? */
 1420                 g->mt_blksiz = st->blksize;
 1421                 g->mt_density = st->density;
 1422                 g->mt_mblksiz[0] = st->modes[0].blksize;
 1423                 g->mt_mblksiz[1] = st->modes[1].blksize;
 1424                 g->mt_mblksiz[2] = st->modes[2].blksize;
 1425                 g->mt_mblksiz[3] = st->modes[3].blksize;
 1426                 g->mt_mdensity[0] = st->modes[0].density;
 1427                 g->mt_mdensity[1] = st->modes[1].density;
 1428                 g->mt_mdensity[2] = st->modes[2].density;
 1429                 g->mt_mdensity[3] = st->modes[3].density;
 1430                 g->mt_fileno = st->fileno;
 1431                 g->mt_blkno = st->blkno;
 1432                 if (st->flags & ST_READONLY)
 1433                         g->mt_dsreg |= MT_DS_RDONLY;
 1434                 if (st->flags & ST_MOUNTED)
 1435                         g->mt_dsreg |= MT_DS_MOUNTED;
 1436                 g->mt_resid = st->mt_resid;
 1437                 g->mt_erreg = st->mt_erreg;
 1438                 /*
 1439                  * clear latched errors.
 1440                  */
 1441                 st->mt_resid = 0;
 1442                 st->mt_erreg = 0;
 1443                 st->asc = 0;
 1444                 st->ascq = 0;
 1445                 break;
 1446         }
 1447         case MTIOCTOP: {
 1448 
 1449                 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
 1450                     ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
 1451                         mt->mt_count));
 1452 
 1453                 /* compat: in U*x it is a short */
 1454                 number = mt->mt_count;
 1455                 switch ((short) (mt->mt_op)) {
 1456                 case MTWEOF:    /* write an end-of-file record */
 1457                         error = st_write_filemarks(st, number, flags);
 1458                         break;
 1459                 case MTBSF:     /* backward space file */
 1460                         number = -number;
 1461                 case MTFSF:     /* forward space file */
 1462                         error = st_check_eod(st, FALSE, &nmarks, flags);
 1463                         if (!error)
 1464                                 error = st_space(st, number - nmarks,
 1465                                     SP_FILEMARKS, flags);
 1466                         break;
 1467                 case MTBSR:     /* backward space record */
 1468                         number = -number;
 1469                 case MTFSR:     /* forward space record */
 1470                         error = st_check_eod(st, true, &nmarks, flags);
 1471                         if (!error)
 1472                                 error = st_space(st, number, SP_BLKS, flags);
 1473                         break;
 1474                 case MTREW:     /* rewind */
 1475                         error = st_rewind(st, 0, flags);
 1476                         break;
 1477                 case MTOFFL:    /* rewind and put the drive offline */
 1478                         st_unmount(st, EJECT);
 1479                         break;
 1480                 case MTNOP:     /* no operation, sets status only */
 1481                         break;
 1482                 case MTRETEN:   /* retension the tape */
 1483                         error = st_load(st, LD_RETENSION, flags);
 1484                         if (!error)
 1485                                 error = st_load(st, LD_LOAD, flags);
 1486                         break;
 1487                 case MTEOM:     /* forward space to end of media */
 1488                         error = st_check_eod(st, FALSE, &nmarks, flags);
 1489                         if (!error)
 1490                                 error = st_space(st, 1, SP_EOM, flags);
 1491                         break;
 1492                 case MTCACHE:   /* enable controller cache */
 1493                         st->flags &= ~ST_DONTBUFFER;
 1494                         goto try_new_value;
 1495                 case MTNOCACHE: /* disable controller cache */
 1496                         st->flags |= ST_DONTBUFFER;
 1497                         goto try_new_value;
 1498                 case MTERASE:   /* erase volume */
 1499                         error = st_erase(st, number, flags);
 1500                         break;
 1501                 case MTSETBSIZ: /* Set block size for device */
 1502 #ifdef  NOTYET
 1503                         if (!(st->flags & ST_NEW_MOUNT)) {
 1504                                 uprintf("re-mount tape before changing blocksize");
 1505                                 error = EINVAL;
 1506                                 break;
 1507                         }
 1508 #endif
 1509                         if (number == 0)
 1510                                 st->flags &= ~ST_FIXEDBLOCKS;
 1511                         else {
 1512                                 if ((st->blkmin || st->blkmax) &&
 1513                                     (number < st->blkmin ||
 1514                                     number > st->blkmax)) {
 1515                                         error = EINVAL;
 1516                                         break;
 1517                                 }
 1518                                 st->flags |= ST_FIXEDBLOCKS;
 1519                         }
 1520                         st->blksize = number;
 1521                         st->flags |= ST_BLOCK_SET;      /*XXX */
 1522                         goto try_new_value;
 1523 
 1524                 case MTSETDNSTY:        /* Set density for device and mode */
 1525                         /*
 1526                          * Any number >= 0 and <= 0xff is legal. Numbers
 1527                          * above 0x80 are 'vendor unique'.
 1528                          */
 1529                         if (number < 0 || number > 255) {
 1530                                 error = EINVAL;
 1531                                 break;
 1532                         } else
 1533                                 st->density = number;
 1534                         goto try_new_value;
 1535 
 1536                 case MTCMPRESS:
 1537                         error = st->ops(st, (number == 0) ?
 1538                             ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
 1539                             XS_CTL_SILENT);
 1540                         break;
 1541 
 1542                 case MTEWARN:
 1543                         if (number)
 1544                                 st->flags |= ST_EARLYWARN;
 1545                         else
 1546                                 st->flags &= ~ST_EARLYWARN;
 1547                         break;
 1548 
 1549                 default:
 1550                         error = EINVAL;
 1551                 }
 1552                 break;
 1553         }
 1554         case MTIOCIEOT:
 1555         case MTIOCEEOT:
 1556                 break;
 1557 
 1558         case MTIOCRDSPOS:
 1559                 error = st_rdpos(st, 0, (u_int32_t *) arg);
 1560                 break;
 1561 
 1562         case MTIOCRDHPOS:
 1563                 error = st_rdpos(st, 1, (u_int32_t *) arg);
 1564                 break;
 1565 
 1566         case MTIOCSLOCATE:
 1567                 error = st_setpos(st, 0, (u_int32_t *) arg);
 1568                 break;
 1569 
 1570         case MTIOCHLOCATE:
 1571                 error = st_setpos(st, 1, (u_int32_t *) arg);
 1572                 break;
 1573 
 1574 
 1575         default:
 1576                 error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
 1577                                         flag, l);
 1578                 break;
 1579         }
 1580         return (error);
 1581 /*-----------------------------*/
 1582 try_new_value:
 1583         /*
 1584          * Check that the mode being asked for is aggreeable to the
 1585          * drive. If not, put it back the way it was.
 1586          *
 1587          * If in control mode, we can make (persistent) mode changes
 1588          * even if no medium is loaded (see st(4)).
 1589          */
 1590         if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
 1591             (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
 1592                 /* put it back as it was */
 1593                 aprint_error_dev(&st->sc_dev, "cannot set selected mode\n");
 1594                 st->density = hold_density;
 1595                 st->blksize = hold_blksize;
 1596                 if (st->blksize)
 1597                         st->flags |= ST_FIXEDBLOCKS;
 1598                 else
 1599                         st->flags &= ~ST_FIXEDBLOCKS;
 1600                 return (error);
 1601         }
 1602         /*
 1603          * As the drive liked it, if we are setting a new default,
 1604          * set it into the structures as such.
 1605          *
 1606          * The means for deciding this are not finalised yet- but
 1607          * if the device was opened in Control Mode, the values
 1608          * are persistent now across mounts.
 1609          */
 1610         if (STMODE(dev) == CTRL_MODE) {
 1611                 switch ((short) (mt->mt_op)) {
 1612                 case MTSETBSIZ:
 1613                         st->modes[dsty].blksize = st->blksize;
 1614                         st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
 1615                         break;
 1616                 case MTSETDNSTY:
 1617                         st->modes[dsty].density = st->density;
 1618                         st->modeflags[dsty] |= DENSITY_SET_BY_USER;
 1619                         break;
 1620                 }
 1621         }
 1622         return (0);
 1623 }
 1624 
 1625 /*
 1626  * Do a synchronous read.
 1627  */
 1628 static int
 1629 st_read(struct st_softc *st, char *bf, int size, int flags)
 1630 {
 1631         struct scsi_rw_tape cmd;
 1632 
 1633         /*
 1634          * If it's a null transfer, return immediatly
 1635          */
 1636         if (size == 0)
 1637                 return (0);
 1638         memset(&cmd, 0, sizeof(cmd));
 1639         cmd.opcode = READ;
 1640         if (st->flags & ST_FIXEDBLOCKS) {
 1641                 cmd.byte2 |= SRW_FIXED;
 1642                 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
 1643                     cmd.len);
 1644         } else
 1645                 _lto3b(size, cmd.len);
 1646         return (scsipi_command(st->sc_periph,
 1647             (void *)&cmd, sizeof(cmd), (void *)bf, size, 0, ST_IO_TIME, NULL,
 1648             flags | XS_CTL_DATA_IN));
 1649 }
 1650 
 1651 /*
 1652  * issue an erase command
 1653  */
 1654 static int
 1655 st_erase(struct st_softc *st, int full, int flags)
 1656 {
 1657         int tmo;
 1658         struct scsi_erase cmd;
 1659 
 1660         /*
 1661          * Full erase means set LONG bit in erase command, which asks
 1662          * the drive to erase the entire unit.  Without this bit, we're
 1663          * asking the drive to write an erase gap.
 1664          */
 1665         memset(&cmd, 0, sizeof(cmd));
 1666         cmd.opcode = ERASE;
 1667         if (full) {
 1668                 cmd.byte2 = SE_LONG;
 1669                 tmo = ST_SPC_TIME;
 1670         } else {
 1671                 tmo = ST_IO_TIME;
 1672         }
 1673 
 1674         /*
 1675          * XXX We always do this asynchronously, for now, unless the device
 1676          * has the ST_Q_ERASE_NOIMM quirk.  How long should we wait if we
 1677          * want to (eventually) to it synchronously?
 1678          */
 1679         if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
 1680                 cmd.byte2 |= SE_IMMED;
 1681 
 1682         return (scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
 1683             ST_RETRIES, tmo, NULL, flags));
 1684 }
 1685 
 1686 /*
 1687  * skip N blocks/filemarks/seq filemarks/eom
 1688  */
 1689 static int
 1690 st_space(struct st_softc *st, int number, u_int what, int flags)
 1691 {
 1692         struct scsi_space cmd;
 1693         int error;
 1694 
 1695         switch (what) {
 1696         case SP_BLKS:
 1697                 if (st->flags & ST_PER_ACTION) {
 1698                         if (number > 0) {
 1699                                 st->flags &= ~ST_PER_ACTION;
 1700                                 return (EIO);
 1701                         } else if (number < 0) {
 1702                                 if (st->flags & ST_AT_FILEMARK) {
 1703                                         /*
 1704                                          * Handling of ST_AT_FILEMARK
 1705                                          * in st_space will fill in the
 1706                                          * right file mark count.
 1707                                          */
 1708                                         error = st_space(st, 0, SP_FILEMARKS,
 1709                                             flags);
 1710                                         if (error)
 1711                                                 return (error);
 1712                                 }
 1713                                 if (st->flags & ST_BLANK_READ) {
 1714                                         st->flags &= ~ST_BLANK_READ;
 1715                                         return (EIO);
 1716                                 }
 1717                                 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
 1718                         }
 1719                 }
 1720                 break;
 1721         case SP_FILEMARKS:
 1722                 if (st->flags & ST_EIO_PENDING) {
 1723                         if (number > 0) {
 1724                                 /* pretend we just discovered the error */
 1725                                 st->flags &= ~ST_EIO_PENDING;
 1726                                 return (EIO);
 1727                         } else if (number < 0) {
 1728                                 /* back away from the error */
 1729                                 st->flags &= ~ST_EIO_PENDING;
 1730                         }
 1731                 }
 1732                 if (st->flags & ST_AT_FILEMARK) {
 1733                         st->flags &= ~ST_AT_FILEMARK;
 1734                         number--;
 1735                 }
 1736                 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
 1737                         /* back away from unwritten tape */
 1738                         st->flags &= ~ST_BLANK_READ;
 1739                         number++;       /* XXX dubious */
 1740                 }
 1741                 break;
 1742         case SP_EOM:
 1743                 if (st->flags & ST_EOM_PENDING) {
 1744                         /* we're already there */
 1745                         st->flags &= ~ST_EOM_PENDING;
 1746                         return (0);
 1747                 }
 1748                 if (st->flags & ST_EIO_PENDING) {
 1749                         /* pretend we just discovered the error */
 1750                         st->flags &= ~ST_EIO_PENDING;
 1751                         return (EIO);
 1752                 }
 1753                 if (st->flags & ST_AT_FILEMARK)
 1754                         st->flags &= ~ST_AT_FILEMARK;
 1755                 break;
 1756         }
 1757         if (number == 0)
 1758                 return (0);
 1759 
 1760         memset(&cmd, 0, sizeof(cmd));
 1761         cmd.opcode = SPACE;
 1762         cmd.byte2 = what;
 1763         _lto3b(number, cmd.number);
 1764 
 1765         st->flags &= ~ST_POSUPDATED;
 1766         st->last_ctl_resid = 0;
 1767         error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
 1768             0, ST_SPC_TIME, NULL, flags);
 1769 
 1770         if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
 1771                 number = number - st->last_ctl_resid;
 1772                 if (what == SP_BLKS) {
 1773                         if (st->blkno != -1) {
 1774                                 st->blkno += number;
 1775                         }
 1776                 } else if (what == SP_FILEMARKS) {
 1777                         if (st->fileno != -1) {
 1778                                 st->fileno += number;
 1779                                 if (number > 0) {
 1780                                         st->blkno = 0;
 1781                                 } else if (number < 0) {
 1782                                         st->blkno = -1;
 1783                                 }
 1784                         }
 1785                 } else if (what == SP_EOM) {
 1786                         /*
 1787                          * This loses us relative position.
 1788                          */
 1789                         st->fileno = st->blkno = -1;
 1790                 }
 1791         }
 1792         return (error);
 1793 }
 1794 
 1795 /*
 1796  * write N filemarks
 1797  */
 1798 static int
 1799 st_write_filemarks(struct st_softc *st, int number, int flags)
 1800 {
 1801         int error;
 1802         struct scsi_write_filemarks cmd;
 1803 
 1804         /*
 1805          * It's hard to write a negative number of file marks.
 1806          * Don't try.
 1807          */
 1808         if (number < 0)
 1809                 return (EINVAL);
 1810         switch (number) {
 1811         case 0:         /* really a command to sync the drive's buffers */
 1812                 break;
 1813         case 1:
 1814                 if (st->flags & ST_FM_WRITTEN)  /* already have one down */
 1815                         st->flags &= ~ST_WRITTEN;
 1816                 else
 1817                         st->flags |= ST_FM_WRITTEN;
 1818                 st->flags &= ~ST_PER_ACTION;
 1819                 break;
 1820         default:
 1821                 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
 1822         }
 1823 
 1824         memset(&cmd, 0, sizeof(cmd));
 1825         cmd.opcode = WRITE_FILEMARKS;
 1826         if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
 1827                 cmd.byte2 = SR_IMMED;
 1828         /*
 1829          * The ATAPI Onstream DI-30 doesn't support writing filemarks, but
 1830          * WRITE_FILEMARKS is still used to flush the buffer
 1831          */
 1832         if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
 1833                 _lto3b(number, cmd.number);
 1834 
 1835         /* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */
 1836         error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
 1837             0, ST_IO_TIME * 4, NULL, flags);
 1838         if (error == 0 && st->fileno != -1) {
 1839                 st->fileno += number;
 1840         }
 1841         return (error);
 1842 }
 1843 
 1844 /*
 1845  * Make sure the right number of file marks is on tape if the
 1846  * tape has been written.  If the position argument is true,
 1847  * leave the tape positioned where it was originally.
 1848  *
 1849  * nmarks returns the number of marks to skip (or, if position
 1850  * true, which were skipped) to get back original position.
 1851  */
 1852 static int
 1853 st_check_eod(struct st_softc *st, boolean position, int *nmarks, int flags)
 1854 {
 1855         int error;
 1856 
 1857         switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
 1858         default:
 1859                 *nmarks = 0;
 1860                 return (0);
 1861         case ST_WRITTEN:
 1862         case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
 1863                 *nmarks = 1;
 1864                 break;
 1865         case ST_WRITTEN | ST_2FM_AT_EOD:
 1866                 *nmarks = 2;
 1867         }
 1868         error = st_write_filemarks(st, *nmarks, flags);
 1869         if (position && !error)
 1870                 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
 1871         return (error);
 1872 }
 1873 
 1874 /*
 1875  * load/unload/retension
 1876  */
 1877 static int
 1878 st_load(struct st_softc *st, u_int type, int flags)
 1879 {
 1880         int error;
 1881         struct scsi_load cmd;
 1882 
 1883         if (type != LD_LOAD) {
 1884                 int nmarks;
 1885 
 1886                 error = st_check_eod(st, FALSE, &nmarks, flags);
 1887                 if (error) {
 1888                         aprint_error_dev(&st->sc_dev, "failed to write closing filemarks at "
 1889                             "unload, errno=%d\n", error);
 1890                         return (error);
 1891                 }
 1892         }
 1893         if (st->quirks & ST_Q_IGNORE_LOADS) {
 1894                 if (type == LD_LOAD) {
 1895                         /*
 1896                          * If we ignore loads, at least we should try a rewind.
 1897                          */
 1898                         return st_rewind(st, 0, flags);
 1899                 }
 1900                 /* otherwise, we should do what's asked of us */
 1901         }
 1902 
 1903         memset(&cmd, 0, sizeof(cmd));
 1904         cmd.opcode = LOAD;
 1905         if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
 1906                 cmd.byte2 = SR_IMMED;
 1907         cmd.how = type;
 1908 
 1909         error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
 1910             ST_RETRIES, ST_SPC_TIME, NULL, flags);
 1911         if (error) {
 1912                 aprint_error_dev(&st->sc_dev, "error %d in st_load (op %d)\n",
 1913                     error, type);
 1914         }
 1915         return (error);
 1916 }
 1917 
 1918 /*
 1919  *  Rewind the device
 1920  */
 1921 static int
 1922 st_rewind(struct st_softc *st, u_int immediate, int flags)
 1923 {
 1924         struct scsi_rewind cmd;
 1925         int error;
 1926         int nmarks;
 1927         int timeout;
 1928 
 1929         error = st_check_eod(st, FALSE, &nmarks, flags);
 1930         if (error) {
 1931                 aprint_error_dev(&st->sc_dev, "failed to write closing filemarks at "
 1932                     "rewind, errno=%d\n", error);
 1933                 return (error);
 1934         }
 1935         st->flags &= ~ST_PER_ACTION;
 1936 
 1937         /* If requestor asked for immediate response, set a short timeout */
 1938         timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME;
 1939 
 1940         /*
 1941          * ATAPI tapes always need immediate to be set
 1942          */
 1943         if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
 1944                 immediate = SR_IMMED;
 1945 
 1946         memset(&cmd, 0, sizeof(cmd));
 1947         cmd.opcode = REWIND;
 1948         cmd.byte2 = immediate;
 1949 
 1950         error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
 1951             ST_RETRIES, timeout, NULL, flags);
 1952         if (error) {
 1953                 aprint_error_dev(&st->sc_dev, "error %d trying to rewind\n",
 1954                     error);
 1955                 /* lost position */
 1956                 st->fileno = st->blkno = -1;
 1957         } else {
 1958                 st->fileno = st->blkno = 0;
 1959         }
 1960         return (error);
 1961 }
 1962 
 1963 static int
 1964 st_rdpos(struct st_softc *st, int hard, u_int32_t *blkptr)
 1965 {
 1966         int error;
 1967         u_int8_t posdata[20];
 1968         struct scsi_tape_read_position cmd;
 1969 
 1970         /*
 1971          * We try and flush any buffered writes here if we were writing
 1972          * and we're trying to get hardware block position. It eats
 1973          * up performance substantially, but I'm wary of drive firmware.
 1974          *
 1975          * I think that *logical* block position is probably okay-
 1976          * but hardware block position might have to wait for data
 1977          * to hit media to be valid. Caveat Emptor.
 1978          */
 1979 
 1980         if (hard && (st->flags & ST_WRITTEN)) {
 1981                 /*
 1982                  * First flush any pending writes...
 1983                  */
 1984                 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
 1985                 /*
 1986                  * The latter case is for 'write protected' tapes
 1987                  * which are too stupid to recognize a zero count
 1988                  * for writing filemarks as a no-op.
 1989                  */
 1990                 if (error != 0 && error != EACCES && error != EROFS)
 1991                         return (error);
 1992         }
 1993 
 1994         memset(&cmd, 0, sizeof(cmd));
 1995         memset(&posdata, 0, sizeof(posdata));
 1996         cmd.opcode = READ_POSITION;
 1997         if (hard)
 1998                 cmd.byte1 = 1;
 1999 
 2000         error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd),
 2001             (void *)&posdata, sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
 2002             XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
 2003 
 2004         if (error == 0) {
 2005 #if     0
 2006                 printf("posdata:");
 2007                 for (hard = 0; hard < sizeof(posdata); hard++)
 2008                         printf("%02x ", posdata[hard] & 0xff);
 2009                 printf("\n");
 2010 #endif
 2011                 if (posdata[0] & 0x4)   /* Block Position Unknown */
 2012                         error = EINVAL;
 2013                 else
 2014                         *blkptr = _4btol(&posdata[4]);
 2015         }
 2016         return (error);
 2017 }
 2018 
 2019 static int
 2020 st_setpos(struct st_softc *st, int hard, u_int32_t *blkptr)
 2021 {
 2022         int error;
 2023         struct scsi_tape_locate cmd;
 2024 
 2025         /*
 2026          * We used to try and flush any buffered writes here.
 2027          * Now we push this onto user applications to either
 2028          * flush the pending writes themselves (via a zero count
 2029          * WRITE FILEMARKS command) or they can trust their tape
 2030          * drive to do this correctly for them.
 2031          *
 2032          * There are very ugly performance limitations otherwise.
 2033          */
 2034 
 2035         memset(&cmd, 0, sizeof(cmd));
 2036         cmd.opcode = LOCATE;
 2037         if (hard)
 2038                 cmd.byte2 = 1 << 2;
 2039         _lto4b(*blkptr, cmd.blkaddr);
 2040         error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
 2041             ST_RETRIES, ST_SPC_TIME, NULL, 0);
 2042         /*
 2043          * Note file && block number position now unknown (if
 2044          * these things ever start being maintained in this driver)
 2045          */
 2046         st->fileno = st->blkno = -1;
 2047         return (error);
 2048 }
 2049 
 2050 
 2051 /*
 2052  * Look at the returned sense and act on the error and determine
 2053  * the unix error number to pass back..., 0 (== report no error),
 2054  * -1 = retry the operation, -2 continue error processing.
 2055  */
 2056 static int
 2057 st_interpret_sense(struct scsipi_xfer *xs)
 2058 {
 2059         struct scsipi_periph *periph = xs->xs_periph;
 2060         struct scsi_sense_data *sense = &xs->sense.scsi_sense;
 2061         struct buf *bp = xs->bp;
 2062         struct st_softc *st = (void *)periph->periph_dev;
 2063         int retval = EJUSTRETURN;
 2064         int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
 2065         u_int8_t key;
 2066         int32_t info;
 2067 
 2068         /*
 2069          * If it isn't a extended or extended/deferred error, let
 2070          * the generic code handle it.
 2071          */
 2072         if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
 2073             SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
 2074                 return (retval);
 2075 
 2076         if (sense->response_code & SSD_RCODE_VALID)
 2077                 info = _4btol(sense->info);
 2078         else
 2079                 info = (st->flags & ST_FIXEDBLOCKS) ?
 2080                     xs->datalen / st->blksize : xs->datalen;
 2081         key = SSD_SENSE_KEY(sense->flags);
 2082         st->mt_erreg = key;
 2083         st->asc = sense->asc;
 2084         st->ascq = sense->ascq;
 2085         st->mt_resid = (short) info;
 2086 
 2087         if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
 2088                 /* Not Ready, Logical Unit Is in Process Of Becoming Ready */
 2089                 if (!callout_pending(&periph->periph_callout))
 2090                         scsipi_periph_freeze(periph, 1);
 2091                 callout_reset(&periph->periph_callout,
 2092                     hz, scsipi_periph_timed_thaw, periph);
 2093                 return (ERESTART);
 2094         }
 2095 
 2096         /*
 2097          * If the device is not open yet, let generic handle
 2098          */
 2099         if ((periph->periph_flags & PERIPH_OPEN) == 0) {
 2100                 return (retval);
 2101         }
 2102 
 2103         xs->resid = info;
 2104         if (st->flags & ST_FIXEDBLOCKS) {
 2105                 if (bp) {
 2106                         xs->resid *= st->blksize;
 2107                         st->last_io_resid = xs->resid;
 2108                 } else {
 2109                         st->last_ctl_resid = xs->resid;
 2110                 }
 2111                 if (key == SKEY_VOLUME_OVERFLOW) {
 2112                         st->flags |= ST_EIO_PENDING;
 2113                         if (bp)
 2114                                 bp->b_resid = xs->resid;
 2115                 } else if (sense->flags & SSD_EOM) {
 2116                         if ((st->flags & ST_EARLYWARN) == 0)
 2117                                 st->flags |= ST_EIO_PENDING;
 2118                         st->flags |= ST_EOM_PENDING;
 2119                         if (bp) {
 2120 #if 0
 2121                                 bp->b_resid = xs->resid;
 2122 #else
 2123                                 /*
 2124                                  * Grotesque as it seems, the few times
 2125                                  * I've actually seen a non-zero resid,
 2126                                  * the tape drive actually lied and had
 2127                                  * written all the data!
 2128                                  */
 2129                                 bp->b_resid = 0;
 2130 #endif
 2131                         }
 2132                 }
 2133                 if (sense->flags & SSD_FILEMARK) {
 2134                         st->flags |= ST_AT_FILEMARK;
 2135                         if (bp)
 2136                                 bp->b_resid = xs->resid;
 2137                         if (st->fileno != (daddr_t) -1) {
 2138                                 st->fileno++;
 2139                                 st->blkno = 0;
 2140                                 st->flags |= ST_POSUPDATED;
 2141                         }
 2142                 }
 2143                 if (sense->flags & SSD_ILI) {
 2144                         st->flags |= ST_EIO_PENDING;
 2145                         if (bp)
 2146                                 bp->b_resid = xs->resid;
 2147                         if (sense->response_code & SSD_RCODE_VALID &&
 2148                             (xs->xs_control & XS_CTL_SILENT) == 0)
 2149                                 aprint_error_dev(&st->sc_dev, "block wrong size, %d blocks "
 2150                                     "residual\n", info);
 2151 
 2152                         /*
 2153                          * This quirk code helps the drive read
 2154                          * the first tape block, regardless of
 2155                          * format.  That is required for these
 2156                          * drives to return proper MODE SENSE
 2157                          * information.
 2158                          */
 2159                         if ((st->quirks & ST_Q_SENSE_HELP) &&
 2160                             (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
 2161                                 st->blksize -= 512;
 2162                         else if ((st->flags & ST_POSUPDATED) == 0) {
 2163                                 if (st->blkno != (daddr_t) -1) {
 2164                                         st->blkno +=
 2165                                             (xs->datalen / st->blksize);
 2166                                         st->flags |= ST_POSUPDATED;
 2167                                 }
 2168                         }
 2169                 }
 2170                 /*
 2171                  * If data wanted and no data was transferred, do it immediately
 2172                  */
 2173                 if (xs->datalen && xs->resid >= xs->datalen) {
 2174                         if (st->flags & ST_EIO_PENDING)
 2175                                 return (EIO);
 2176                         if (st->flags & ST_AT_FILEMARK) {
 2177                                 if (bp)
 2178                                         bp->b_resid = xs->resid;
 2179                                 return (0);
 2180                         }
 2181                 }
 2182         } else {                /* must be variable mode */
 2183                 if (bp) {
 2184                         st->last_io_resid = xs->resid;
 2185                 } else {
 2186                         st->last_ctl_resid = xs->resid;
 2187                 }
 2188                 if (sense->flags & SSD_EOM) {
 2189                         /*
 2190                          * The current semantics of this
 2191                          * driver requires EOM detection
 2192                          * to return EIO unless early
 2193                          * warning detection is enabled
 2194                          * for variable mode (this is always
 2195                          * on for fixed block mode).
 2196                          */
 2197                         if (st->flags & ST_EARLYWARN) {
 2198                                 st->flags |= ST_EOM_PENDING;
 2199                                 retval = 0;
 2200                         } else {
 2201                                 retval = EIO;
 2202                                 /*
 2203                                  * If we return an error we can't claim to
 2204                                  * have transfered all data.
 2205                                  */
 2206                                 if (xs->resid == 0)
 2207                                         xs->resid = xs->datalen;
 2208                         }
 2209 
 2210                         /*
 2211                          * If it's an unadorned EOM detection,
 2212                          * suppress printing an error.
 2213                          */
 2214                         if (key == SKEY_NO_SENSE) {
 2215                                 doprint = 0;
 2216                         }
 2217                 } else if (sense->flags & SSD_FILEMARK) {
 2218                         retval = 0;
 2219                         if (st->fileno != (daddr_t) -1) {
 2220                                 st->fileno++;
 2221                                 st->blkno = 0;
 2222                                 st->flags |= ST_POSUPDATED;
 2223                         }
 2224                 } else if (sense->flags & SSD_ILI) {
 2225                         if (info < 0) {
 2226                                 /*
 2227                                  * The tape record was bigger than the read
 2228                                  * we issued.
 2229                                  */
 2230                                 if ((xs->xs_control & XS_CTL_SILENT) == 0) {
 2231                                         aprint_error_dev(&st->sc_dev,
 2232                                             "%d-byte tape record too big"
 2233                                             " for %d-byte user buffer\n",
 2234                                             xs->datalen - info, xs->datalen);
 2235                                 }
 2236                                 retval = EIO;
 2237                         } else {
 2238                                 retval = 0;
 2239                                 if (st->blkno != (daddr_t) -1) {
 2240                                         st->blkno++;
 2241                                         st->flags |= ST_POSUPDATED;
 2242                                 }
 2243                         }
 2244                 }
 2245                 if (bp)
 2246                         bp->b_resid = xs->resid;
 2247         }
 2248 
 2249 #ifndef SCSIPI_DEBUG
 2250         if (retval == 0 && key == SKEY_NO_SENSE)
 2251                 doprint = 0;
 2252 #endif
 2253         if (key == SKEY_BLANK_CHECK) {
 2254                 /*
 2255                  * This quirk code helps the drive read the
 2256                  * first tape block, regardless of format.  That
 2257                  * is required for these drives to return proper
 2258                  * MODE SENSE information.
 2259                  */
 2260                 if ((st->quirks & ST_Q_SENSE_HELP) &&
 2261                     (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
 2262                         /* still starting */
 2263                         st->blksize -= 512;
 2264                 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
 2265                         st->flags |= ST_BLANK_READ;
 2266                         xs->resid = xs->datalen;
 2267                         if (bp) {
 2268                                 bp->b_resid = xs->resid;
 2269                                 /* return an EOF */
 2270                         }
 2271                         retval = 0;
 2272                         /* lost position */
 2273                         st->fileno = st->blkno = -1;
 2274                 }
 2275         }
 2276 
 2277         /*
 2278          * If generic sense processing will continue, we should not
 2279          * print sense info here.
 2280          */
 2281         if (retval == EJUSTRETURN)
 2282                 doprint = 0;
 2283 
 2284         if (doprint) {
 2285 #ifdef  SCSIVERBOSE
 2286                 scsipi_print_sense(xs, 0);
 2287 #else
 2288                 scsipi_printaddr(periph);
 2289                 printf("Sense Key 0x%02x", key);
 2290                 if ((sense->response_code & SSD_RCODE_VALID) != 0) {
 2291                         switch (key) {
 2292                         case SKEY_NOT_READY:
 2293                         case SKEY_ILLEGAL_REQUEST:
 2294                         case SKEY_UNIT_ATTENTION:
 2295                         case SKEY_DATA_PROTECT:
 2296                                 break;
 2297                         case SKEY_VOLUME_OVERFLOW:
 2298                         case SKEY_BLANK_CHECK:
 2299                                 printf(", requested size: %d (decimal)", info);
 2300                                 break;
 2301                         case SKEY_ABORTED_COMMAND:
 2302                                 if (xs->xs_retries)
 2303                                         printf(", retrying");
 2304                                 printf(", cmd 0x%x, info 0x%x",
 2305                                     xs->cmd->opcode, info);
 2306                                 break;
 2307                         default:
 2308                                 printf(", info = %d (decimal)", info);
 2309                         }
 2310                 }
 2311                 if (sense->extra_len != 0) {
 2312                         int n;
 2313                         printf(", data =");
 2314                         for (n = 0; n < sense->extra_len; n++)
 2315                                 printf(" %02x", sense->csi[n]);
 2316                 }
 2317                 printf("\n");
 2318 #endif
 2319         }
 2320         return (retval);
 2321 }
 2322 
 2323 /*
 2324  * The quirk here is that the drive returns some value to st_mode_sense
 2325  * incorrectly until the tape has actually passed by the head.
 2326  *
 2327  * The method is to set the drive to large fixed-block state (user-specified
 2328  * density and 1024-byte blocks), then read and rewind to get it to sense the
 2329  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
 2330  * work, as a last resort, try variable- length blocks.  The result will be
 2331  * the ability to do an accurate st_mode_sense.
 2332  *
 2333  * We know we can do a rewind because we just did a load, which implies rewind.
 2334  * Rewind seems preferable to space backward if we have a virgin tape.
 2335  *
 2336  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
 2337  * error processing, both part of st_interpret_sense.
 2338  */
 2339 static int
 2340 st_touch_tape(struct st_softc *st)
 2341 {
 2342         char *bf;
 2343         int readsize;
 2344         int error;
 2345 
 2346         bf = malloc(1024, M_TEMP, M_NOWAIT);
 2347         if (bf == NULL)
 2348                 return (ENOMEM);
 2349 
 2350         if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
 2351                 goto bad;
 2352 
 2353         /*
 2354          * If the block size is already known from the
 2355          * sense data, use it. Else start probing at 1024.
 2356          */
 2357         if (st->media_blksize > 0)
 2358                 st->blksize = st->media_blksize;
 2359         else
 2360                 st->blksize = 1024;
 2361 
 2362         do {
 2363                 switch (st->blksize) {
 2364                 case 512:
 2365                 case 1024:
 2366                         readsize = st->blksize;
 2367                         st->flags |= ST_FIXEDBLOCKS;
 2368                         break;
 2369                 default:
 2370                         readsize = 1;
 2371                         st->flags &= ~ST_FIXEDBLOCKS;
 2372                 }
 2373                 if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
 2374                     != 0) {
 2375                         /*
 2376                          * The device did not agree with the proposed
 2377                          * block size. If we exhausted our options,
 2378                          * return failure, else try another.
 2379                          */
 2380                         if (readsize == 1)
 2381                                 goto bad;
 2382                         st->blksize -= 512;
 2383                         continue;
 2384                 }
 2385                 st_read(st, bf, readsize, XS_CTL_SILENT);       /* XXX */
 2386                 if ((error = st_rewind(st, 0, 0)) != 0) {
 2387 bad:                    free(bf, M_TEMP);
 2388                         return (error);
 2389                 }
 2390         } while (readsize != 1 && readsize > st->blksize);
 2391 
 2392         free(bf, M_TEMP);
 2393         return (0);
 2394 }
 2395 
 2396 static int
 2397 stdump(dev_t dev, daddr_t blkno, void *va,
 2398     size_t size)
 2399 {
 2400 
 2401         /* Not implemented. */
 2402         return (ENXIO);
 2403 }

Cache object: 364fe8dab9349fde36812812fa43d601


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