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

Cache object: 2dfd0fa8a525a8c94697cf1e74adeabf


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