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/scsi/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 /*
    2  * Written by Julian Elischer (julian@tfs.com)(now julian@DIALix.oz.au)
    3  * for TRW Financial Systems for use under the MACH(2.5) operating system.
    4  *
    5  * TRW Financial Systems, in accordance with their agreement with Carnegie
    6  * Mellon University, makes this software available to CMU to distribute
    7  * or use in any manner that they see fit as long as this message is kept with
    8  * the software. For this reason TFS also grants any other persons or
    9  * organisations permission to use or modify this software.
   10  *
   11  * TFS supplies this software to be publicly redistributed
   12  * on the understanding that TFS is not responsible for the correct
   13  * functioning of this software in any circumstances.
   14  *
   15  * $FreeBSD: src/sys/scsi/st.c,v 1.73.2.4 1999/09/05 08:21:51 peter Exp $
   16  */
   17 
   18 /*
   19  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
   20  * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
   21  */
   22 
   23 /*
   24  * To do:
   25  * work out some better way of guessing what a good timeout is going
   26  * to be depending on whether we expect to retension or not.
   27  *
   28  */
   29 
   30 #include "opt_bounce.h"
   31 #include "opt_scsi.h"
   32 
   33 #include <sys/types.h>
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 
   38 #include <sys/fcntl.h>
   39 #include <sys/errno.h>
   40 #include <sys/ioctl.h>
   41 #include <sys/malloc.h>
   42 #include <sys/buf.h>
   43 #include <sys/proc.h>
   44 #include <sys/mtio.h>
   45 #include <sys/conf.h>
   46 #include <sys/kernel.h>
   47 #ifdef DEVFS
   48 #include <sys/devfsext.h>
   49 #endif /*DEVFS*/
   50 
   51 #include <scsi/scsi_all.h>
   52 #include <scsi/scsi_tape.h>
   53 #include <scsi/scsiconf.h>
   54 
   55 
   56 
   57 /* Defines for device specific stuff */
   58 #define         PAGE_0_SENSE_DATA_SIZE  12
   59 #define DEF_FIXED_BSIZE  512
   60 #define ST_RETRIES      4       /* only on non IO commands */
   61 
   62 #define STUNIT(DEV)      ((minor(DEV)&0xF0) >> 4)    /* 4 bit unit.  */
   63 #define STSETUNIT(DEV, U) makedev(major(DEV), ((U) << 4))
   64 
   65 #define MODE(z)         (  (minor(z)       & 0x03) )
   66 #define DSTY(z)         ( ((minor(z) >> 2) & 0x03) )
   67 #define CTLMODE 3
   68 
   69 #define IS_CTLMODE(DEV) (MODE(DEV) == CTLMODE)
   70 
   71 static errval   st_space __P((u_int32_t unit, int32_t number, u_int32_t what, u_int32_t flags));
   72 static errval   st_rewind __P((u_int32_t unit, boolean immed, u_int32_t flags));
   73 static errval   st_erase __P((u_int32_t unit, boolean immed, u_int32_t flags));
   74 static errval st_mode_sense __P((u_int32_t unit, u_int32_t flags, \
   75         struct tape_pages *page, u_int32_t pagelen, u_int32_t pagecode));
   76 static errval   st_decide_mode __P((u_int32_t unit, boolean first_read));
   77 static errval   st_read __P((u_int32_t unit, char *buf, u_int32_t size,
   78         u_int32_t flags));
   79 static errval   st_rd_blk_lim __P((u_int32_t unit, u_int32_t flags));
   80 static errval   st_touch_tape __P((u_int32_t unit));
   81 static errval   st_write_filemarks __P((u_int32_t unit, int32_t number, u_int32_t flags));
   82 static errval   st_load __P((u_int32_t unit, u_int32_t type, u_int32_t flags));
   83 static errval   st_mode_select __P((u_int32_t unit, u_int32_t flags, \
   84         struct tape_pages *page, u_int32_t pagelen, u_int32_t byte2));
   85 static errval   st_comp __P((u_int32_t unit, u_int32_t mode));
   86 static int32_t  st_chkeod __P((u_int32_t unit, boolean position, int32_t *nmarks,
   87         u_int32_t flags));
   88 static void     ststart(u_int32_t       unit, u_int32_t flags);
   89 static void     st_unmount __P((int unit, boolean eject));
   90 static errval   st_mount_tape __P((dev_t dev, u_int32_t flags));
   91 static void     st_loadquirks __P((struct scsi_link *sc_link));
   92 static errval   st_interpret_sense __P((struct scsi_xfer *xs));
   93 
   94 #define ESUCCESS 0
   95 #define NOEJECT 0
   96 #define EJECT 1
   97 
   98 struct scsi_data {
   99 /*--------------------present operating parameters, flags etc.----------------*/
  100         u_int32_t flags;                /* see below                          */
  101         u_int32_t blksiz;               /* blksiz we are using                */
  102         u_int32_t density;      /* present density                    */
  103         u_int32_t comp;         /* present compression mode           */
  104         u_int32_t quirks;               /* quirks for the open mode           */
  105         u_int32_t last_dsty;    /* last density openned               */
  106 /*--------------------parameters reported by the device ----------------------*/
  107         u_int32_t blkmin;               /* min blk size                       */
  108         u_int32_t blkmax;               /* max blk size                       */
  109 /*--------------------parameters reported by the device for this media--------*/
  110         u_int32_t numblks;      /* nominal blocks capacity            */
  111         u_int32_t media_blksiz; /* 0 if not ST_FIXEDBLOCKS            */
  112         u_int32_t media_density;        /* this is what it said when asked    */
  113 /*--------------------quirks for the whole drive------------------------------*/
  114         u_int32_t drive_quirks; /* quirks of this drive               */
  115 /*--------------------How we should set up when openning each minor device----*/
  116         st_modes modes; /* plus more for each mode            */
  117         u_int8_t modeflags[4];  /* flags for the modes                */
  118 #define DENSITY_SET_BY_USER     0x01
  119 #define DENSITY_SET_BY_QUIRK    0x02
  120 #define BLKSIZE_SET_BY_USER     0x04
  121 #define BLKSIZE_SET_BY_QUIRK    0x08
  122 #define COMPRES_SET_BY_USER     0x10
  123 #define COMPRES_SET_BY_QUIRK    0x20
  124 /*--------------------storage for sense data returned by the drive------------*/
  125         unsigned char saved_page0[PAGE_0_SENSE_DATA_SIZE];      /*
  126                                          * additional sense data needed
  127                                          * for mode sense/select.
  128                                          */
  129         struct buf_queue_head buf_queue;
  130         struct scsi_xfer scsi_xfer;     /* scsi xfer struct for this drive */
  131         u_int32_t xfer_block_wait;      /* is a process waiting? */
  132 #ifdef  DEVFS
  133         struct {
  134                 void    *rst;
  135                 void    *nrst;
  136                 void    *erst;
  137         /* end of aliases */
  138                 void    *rst_[4];
  139                 void    *nrst_[4];
  140                 void    *erst_[4];
  141                 void    *ctl_[4];
  142                 void    *ctl;
  143         } devfs_token;
  144 #endif
  145 };
  146 
  147 static int stunit(dev_t dev) { return STUNIT(dev); }
  148 static dev_t stsetunit(dev_t dev, int unit) { return STSETUNIT(dev, unit); }
  149 
  150 static errval st_open(dev_t dev, int flags, int fmt, struct proc *p,
  151                 struct scsi_link *sc_link);
  152 static errval st_ioctl(dev_t dev, int cmd, caddr_t addr, int flag,
  153                 struct proc *p, struct scsi_link *sc_link);
  154 static errval st_close(dev_t dev, int flag, int fmt, struct proc *p,
  155         struct scsi_link *sc_link);
  156 static void st_strategy(struct buf *bp, struct scsi_link *sc_link);
  157 
  158 static d_open_t         stopen;
  159 static d_close_t        stclose;
  160 static d_ioctl_t        stioctl;
  161 static d_strategy_t     ststrategy;
  162 
  163 #define CDEV_MAJOR 14
  164 #define BDEV_MAJOR 5
  165 static struct cdevsw st_cdevsw;
  166 static struct bdevsw st_bdevsw = 
  167         { stopen,       stclose,        ststrategy,     stioctl,        /*5*/
  168           nodump,       nopsize,        0,      "st",   &st_cdevsw,     -1 };
  169 
  170 SCSI_DEVICE_ENTRIES(st)
  171 
  172 static struct scsi_device st_switch =
  173 {
  174         st_interpret_sense,     /* check errors with us first */
  175         ststart,         /* we have a queue, and this is how we service it */
  176         NULL,
  177         NULL,                   /* use the default 'done' routine */
  178         "st",
  179         0,
  180         {0, 0},
  181         0,                              /* Link flags */
  182         stattach,
  183         "Sequential-Access",
  184         stopen,
  185         sizeof(struct scsi_data),
  186         T_SEQUENTIAL,
  187         stunit,
  188         stsetunit,
  189         st_open,
  190         st_ioctl,
  191         st_close,
  192         st_strategy,
  193 };
  194 
  195 #define ST_INITIALIZED  0x01
  196 #define ST_INFO_VALID   0x02
  197 #define ST_OPEN         0x04
  198 #define ST_BLOCK_SET    0x08    /* block size, mode set by ioctl      */
  199 #define ST_WRITTEN      0x10    /* data have been written, EOD needed */
  200 #define ST_FIXEDBLOCKS  0x20
  201 #define ST_AT_FILEMARK  0x40
  202 #define ST_EIO_PENDING  0x80    /* we couldn't report it then (had data) */
  203 #define ST_NEW_MOUNT    0x100   /* still need to decide mode              */
  204 #define ST_READONLY     0x200   /* st_mode_sense says write protected */
  205 #define ST_FM_WRITTEN   0x400   /*
  206                                  * EOF file mark written  -- used with
  207                                  * ~ST_WRITTEN to indicate that multiple file
  208                                  * marks have been written
  209                                  */
  210 #define ST_BLANK_READ   0x800   /* BLANK CHECK encountered already */
  211 #define ST_2FM_AT_EOD   0x1000  /* write 2 file marks at EOD */
  212 #define ST_MOUNTED      0x2000  /* Device is presently mounted */
  213 #define ST_SENSE_READ   0x4000  /* mode sense read from drive */
  214 
  215 #define ST_PER_ACTION   (ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
  216 #define ST_PER_MOUNT    (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
  217                         ST_FIXEDBLOCKS | ST_READONLY | \
  218                         ST_FM_WRITTEN | ST_2FM_AT_EOD | ST_PER_ACTION)
  219 
  220 /*
  221  * The routine called by the low level scsi routine when it discovers
  222  * a device suitable for this driver
  223  */
  224 
  225 static  errval
  226 stattach(struct scsi_link *sc_link)
  227 {
  228         u_int32_t unit;
  229 #ifdef DEVFS
  230         int ii;
  231 #endif /*DEVFS*/
  232 
  233         struct scsi_data *st = sc_link->sd;
  234 
  235         unit = sc_link->dev_unit;
  236 
  237         bufq_init(&st->buf_queue);
  238         /*
  239          * Check if the drive is a known criminal and take
  240          * Any steps needed to bring it into line
  241          */
  242         st_loadquirks(sc_link);
  243         /*
  244          * Use the subdriver to request information regarding
  245          * the drive. We cannot use interrupts yet, so the
  246          * request must specify this.
  247          */
  248         if (st_mode_sense(unit, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT,
  249                 NULL, 0, 0)) {
  250                 printf("drive offline");
  251         } else {
  252                 printf("density code 0x%lx, ", st->media_density);
  253                 if (!scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
  254                         if (st->media_blksiz) {
  255                                 printf("%ld-byte", st->media_blksiz);
  256                         } else {
  257                                 printf("variable");
  258                         }
  259                         printf(" blocks, write-%s",
  260                             (st->flags & ST_READONLY) ? "protected" : "enabled");
  261                 } else {
  262                         printf(" drive empty");
  263                 }
  264         }
  265         /*
  266          * Set up the buf queue for this device
  267          */
  268         st->flags |= ST_INITIALIZED;
  269 #ifdef  DEVFS
  270         for(ii=0; ii<4; ii++) {
  271                 st->devfs_token.rst_[ii] = 
  272                         devfs_add_devswf(&st_cdevsw, 
  273                                          (unit << 4 ) + (ii * 4), DV_CHR, 
  274                                          UID_ROOT, GID_OPERATOR, 0660, 
  275                                          "rst%ld.%d", unit, ii);
  276                 st->devfs_token.nrst_[ii] = 
  277                         devfs_add_devswf(&st_cdevsw, 
  278                                          (unit << 4 ) + ((ii * 4) + 1), DV_CHR, 
  279                                          UID_ROOT, GID_OPERATOR, 0660, 
  280                                          "nrst%ld.%d", unit, ii);
  281                 st->devfs_token.erst_[ii] = 
  282                         devfs_add_devswf(&st_cdevsw, 
  283                                          (unit << 4 ) + ((ii * 4) + 2), DV_CHR,
  284                                          UID_ROOT, GID_OPERATOR, 0660, 
  285                                          "erst%ld.%d", unit, ii);
  286                 st->devfs_token.ctl_[ii] = 
  287                         devfs_add_devswf(&st_cdevsw, 
  288                                          (unit << 4 ) + ((ii * 4) + 3), DV_CHR, 
  289                                          UID_ROOT, GID_OPERATOR, 0600, 
  290                                          "st%ldctl.%d", unit, ii);
  291         }
  292 
  293         st->devfs_token.ctl = 
  294                 devfs_add_devswf(&st_cdevsw, (unit << 4 ) | SCSI_CONTROL_MASK,
  295                                  DV_CHR, UID_ROOT, GID_WHEEL, 0600,
  296                                  "rst%d.ctl", unit);
  297         /** add links **/
  298         st->devfs_token.rst = 
  299                 devfs_link(st->devfs_token.rst_[0], "rst%ld", unit);
  300         st->devfs_token.nrst = 
  301                 devfs_link(st->devfs_token.nrst_[0], "nrst%ld", unit);
  302         st->devfs_token.erst = 
  303                 devfs_link(st->devfs_token.erst_[0], "erst%ld", unit);
  304 #endif
  305         return 0;
  306 }
  307 
  308 
  309 /*
  310  * initialise the subdevices to the default (QUIRK) state.
  311  * this will remove any setting made by the system operator or previous
  312  * operations.
  313  */
  314 static  void
  315 st_loadquirks(sc_link)
  316         struct scsi_link *sc_link;
  317 {
  318         struct scsi_data *st = sc_link->sd;
  319         int     i;
  320         struct  st_mode *mode;
  321         struct  st_mode *mode2;
  322 
  323         mode = (struct st_mode*) sc_link->devmodes;
  324         if (!mode)
  325                 return;
  326 
  327         st->quirks = st->drive_quirks = sc_link->quirks;
  328 
  329         mode2 = st->modes;
  330 
  331         for (i = 0; i < 4; i++) {
  332                 bzero(mode2, sizeof(*mode2));
  333                 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK
  334                     | DENSITY_SET_BY_QUIRK
  335                     | BLKSIZE_SET_BY_USER
  336                     | DENSITY_SET_BY_USER);
  337                 if (mode->blksiz && ((mode->quirks | st->drive_quirks)
  338                         & (ST_Q_FORCE_FIXED_MODE))) {
  339                         mode2->blksiz = mode->blksiz;
  340                         st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
  341                 } else {
  342                         if ((mode->quirks | st->drive_quirks)
  343                             & ST_Q_FORCE_VAR_MODE) {
  344                                 mode2->blksiz = 0;
  345                                 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
  346                         }
  347                 }
  348                 if (mode->density) {
  349                         mode2->density = mode->density;
  350                         st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
  351                 }
  352                 mode++;
  353                 mode2++;
  354         }
  355 }
  356 
  357 /*
  358  * open the device.
  359  */
  360 static  errval
  361 st_open(dev_t dev, int flags, int fmt, struct proc *p,
  362 struct scsi_link *sc_link)
  363 {
  364         u_int32_t unit, mode, dsty;
  365         errval  errno = 0;
  366         struct scsi_data *st;
  367 
  368         unit = STUNIT(dev);
  369         mode = MODE(dev);
  370         dsty = DSTY(dev);
  371 
  372         st = sc_link->sd;
  373         /*
  374          * Make sure the device has been initialised
  375          */
  376         if ((st == NULL) || (!(st->flags & ST_INITIALIZED)))
  377                 return (ENXIO);
  378 
  379         /*
  380          * Only allow one at a time
  381          */
  382         if (st->flags & ST_OPEN) {
  383                 return (EBUSY);
  384         }
  385         /*
  386          * Throw out a dummy instruction to catch 'Unit attention
  387          * errors (the error handling will invalidate all our
  388          * device info if we get one, but otherwise, ignore it)
  389          */
  390         scsi_test_unit_ready(sc_link, SCSI_SILENT);
  391 
  392         sc_link->flags |= SDEV_OPEN;    /* unit attn are now errors */
  393         /*
  394          * If the mode is 3 (e.g. minor = 3,7,11,15)
  395          * then the device has been openned to set defaults
  396          * This mode does NOT ALLOW I/O, only ioctls.
  397          * XXX: Where do we lock out I/O?
  398          */
  399         if (IS_CTLMODE(dev))
  400                 return 0;
  401 
  402         /*
  403          * Check that the device is ready to use (media loaded?)
  404          * This time take notice of the return result
  405          */
  406         if ( (errno = scsi_test_unit_ready(sc_link, 0)) ) {
  407                 uprintf("st%d: not ready\n", unit);
  408                 st_unmount(unit, NOEJECT);
  409                 return (errno);
  410         }
  411         /*
  412          * if it's a different mode, or if the media has been
  413          * invalidated, unmount the tape from the previous
  414          * session but continue with open processing
  415          */
  416         if ((st->last_dsty != dsty)
  417             || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
  418                 st_unmount(unit, NOEJECT);
  419         }
  420         /*
  421          * If we are not mounted, then we should start a new
  422          * mount session.
  423          */
  424         if (!(st->flags & ST_MOUNTED)) {
  425                 st_mount_tape(dev, flags);
  426                 st->last_dsty = dsty;
  427         }
  428         /*
  429          * Make sure that a tape opened in write-only mode will have
  430          * file marks written on it when closed, even if not written to.
  431          * This is for SUN compatibility
  432          */
  433         if ((flags & O_ACCMODE) == FWRITE)
  434                 st->flags |= ST_WRITTEN;
  435 
  436         /* PREVENT ALLOW MEDIUM REMOVAL is optional per the SCSI-2 specs */
  437         scsi_prevent(sc_link, PR_PREVENT, SCSI_SILENT | SCSI_ERR_OK);
  438 
  439         SC_DEBUG(sc_link, SDEV_DB2, ("Open complete\n"));
  440 
  441         st->flags |= ST_OPEN;
  442         return 0;
  443 }
  444 
  445 /*
  446  * close the device.. only called if we are the LAST
  447  * occurence of an open device
  448  */
  449 static  errval
  450 st_close(dev_t dev, int flag, int fmt, struct proc *p,
  451         struct scsi_link *sc_link)
  452 {
  453         u_int32_t unit, mode;
  454         struct scsi_data *st;
  455 
  456         unit = STUNIT(dev);
  457         mode = MODE(dev);
  458         st = sc_link->sd;
  459 
  460         if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
  461                 st_write_filemarks(unit, 1, 0);
  462 
  463         /*
  464          * Since the device has seen its last close, allow media removal. 
  465          * Do this before we attempt an eject.
  466          */
  467         scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
  468 
  469         switch (mode & 0x3) {
  470         case 0:
  471         case 3:         /* for now */
  472                 st_unmount(unit, NOEJECT);
  473                 break;
  474         case 1:         /*leave mounted unless media seems to have been removed */
  475                 if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
  476                         st_unmount(unit, NOEJECT);
  477                 }
  478                 break;
  479         case 2:
  480                 st_unmount(unit, EJECT);
  481                 break;
  482         }
  483 
  484         sc_link->flags &= ~SDEV_OPEN;
  485         st->flags &= ~ST_OPEN;
  486 
  487         return (0);
  488 }
  489 
  490 /*
  491  * Start a new mount session.
  492  * Copy in all the default parameters from the selected device mode.
  493  * and try guess any that seem to be defaulted.
  494  */
  495 static  errval
  496 st_mount_tape(dev, flags)
  497         dev_t   dev;
  498         u_int32_t flags;
  499 {
  500         u_int32_t unit, mode, dsty;
  501         struct scsi_data *st;
  502         struct scsi_link *sc_link;
  503         errval  errno = 0;
  504 
  505         unit = STUNIT(dev);
  506         mode = MODE(dev);
  507         dsty = DSTY(dev);
  508         sc_link = SCSI_LINK(&st_switch, unit);
  509         st = sc_link->sd;
  510 
  511         if (st->flags & ST_MOUNTED)
  512                 return 0;
  513 
  514         SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
  515         st->flags |= ST_NEW_MOUNT;
  516         st->quirks = st->drive_quirks | st->modes[dsty].quirks;
  517         /*
  518          * If the media is new, then make sure we give it a chance to
  519          * to do a 'load' instruction. ( We assume it is new)
  520          */
  521         if ( (errno = st_load(unit, LD_LOAD, 0)) ) {
  522                 return (errno);
  523         }
  524         /*
  525          * Throw another dummy instruction to catch
  526          * 'Unit attention' errors. Some drives appear to give
  527          * these after doing a Load instruction.
  528          * (noteably some DAT drives)
  529          */
  530         scsi_test_unit_ready(sc_link, SCSI_SILENT);
  531 
  532         /*
  533          * Some devices can't tell you much until they have been
  534          * asked to look at the media. This quirk does this.
  535          */
  536         if (st->quirks & ST_Q_SNS_HLP) {
  537                 if ( (errno = st_touch_tape(unit)) )
  538                         return errno;
  539         }
  540         /*
  541          * Load the physical device parameters
  542          * loads: blkmin, blkmax
  543          */
  544         if ( (errno = st_rd_blk_lim(unit, 0)) ) {
  545                 return errno;
  546         }
  547         /*
  548          * Load the media dependent parameters
  549          * includes: media_blksiz,media_density,numblks
  550          * As we have a tape in, it should be reflected here.
  551          * If not you may need the "quirk" above.
  552          */
  553         if ( (errno = st_mode_sense(unit, 0, NULL, 0, 0)) ) {
  554                 return errno;
  555         }
  556         /*
  557          * If we have gained a permanent density from somewhere,
  558          * then use it in preference to the one supplied by
  559          * default by the driver.
  560          */
  561         if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) {
  562                 st->density = st->modes[dsty].density;
  563         } else {
  564                 st->density = st->media_density;
  565         }
  566         /*
  567          * If we have gained a permanent blocksize
  568          * then use it in preference to the one supplied by
  569          * default by the driver.
  570          */
  571         st->flags &= ~ST_FIXEDBLOCKS;
  572         if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
  573                 st->blksiz = st->modes[dsty].blksiz;
  574                 if (st->blksiz) {
  575                         st->flags |= ST_FIXEDBLOCKS;
  576                 }
  577         } else {
  578                 if ( (errno = st_decide_mode(unit, FALSE)) ) {
  579                         return errno;
  580                 }
  581         }
  582         if ( (errno = st_mode_select(unit, 0, NULL, 0, 0)) ) {
  583                 printf("st%ld: Cannot set selected mode", unit);
  584                 return errno;
  585         }
  586         st->flags &= ~ST_NEW_MOUNT;
  587         st->flags |= ST_MOUNTED;
  588         sc_link->flags |= SDEV_MEDIA_LOADED;
  589 
  590         return 0;
  591 }
  592 
  593 /*
  594  * End the present mount session.
  595  * Rewind, and optionally eject the tape.
  596  * Reset various flags to indicate that all new
  597  * operations require another mount operation
  598  */
  599 void
  600 st_unmount(int unit, boolean eject)
  601 {
  602         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
  603         struct scsi_data *st = sc_link->sd;
  604         int32_t nmarks;
  605 
  606         if (!(st->flags & ST_MOUNTED))
  607                 return;
  608         SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
  609         st_chkeod(unit, FALSE, &nmarks, SCSI_SILENT);
  610         st_rewind(unit, FALSE, SCSI_SILENT);
  611         if (eject) {
  612                 st_load(unit, LD_UNLOAD, SCSI_SILENT);
  613         }
  614         st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
  615         sc_link->flags &= ~SDEV_MEDIA_LOADED;
  616 }
  617 
  618 /*
  619  * Given all we know about the device, media, mode, 'quirks' and
  620  * initial operation, make a decision as to how we should be set
  621  * to run (regarding blocking and EOD marks)
  622  */
  623 static  errval
  624 st_decide_mode(unit, first_read)
  625         u_int32_t       unit;
  626         boolean first_read;
  627 {
  628         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
  629         struct scsi_data *st = sc_link->sd;
  630 
  631         SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
  632 
  633         /*
  634          * If the user hasn't already specified fixed or variable-length
  635          * blocks and the block size (zero if variable-length), we'll
  636          * have to try to figure them out ourselves.
  637          *
  638          * Our first shot at a method is, "The quirks made me do it!"
  639          */
  640         switch ((int)(st->quirks & (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE))) {
  641         case (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE):
  642                 printf("st%ld: bad quirks\n", unit);
  643                 return (EINVAL);
  644         case ST_Q_FORCE_FIXED_MODE:     /*specified fixed, but not what size */
  645                 st->flags |= ST_FIXEDBLOCKS;
  646                 if (st->blkmin && (st->blkmin == st->blkmax))
  647                         st->blksiz = st->blkmin;
  648                 else if (st->media_blksiz > 0)
  649                         st->blksiz = st->media_blksiz;
  650                 else
  651                         st->blksiz = DEF_FIXED_BSIZE;
  652                 SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force fixed mode(%ld)\n",
  653                         st->blksiz));
  654                 goto done;
  655         case ST_Q_FORCE_VAR_MODE:
  656                 st->flags &= ~ST_FIXEDBLOCKS;
  657                 st->blksiz = 0;
  658                 SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force variable mode\n"));
  659                 goto done;
  660         }
  661                 /*
  662                  * If the drive can only handle fixed-length blocks and only at
  663                  * one size, perhaps we should just do that.
  664                  */
  665                 if (st->blkmin && (st->blkmin == st->blkmax)) {
  666                 st->flags |= ST_FIXEDBLOCKS;
  667                 st->blksiz = st->blkmin;
  668                 SC_DEBUG(sc_link, SDEV_DB3,
  669                     ("blkmin == blkmax of %ld\n", st->blkmin));
  670                 goto done;
  671         }
  672         /*
  673          * If the tape density mandates (or even suggests) use of fixed
  674          * or variable-length blocks, comply.
  675          */
  676         switch ((int)st->density) {
  677         case HALFINCH_800:
  678         case HALFINCH_1600:
  679         case HALFINCH_6250:
  680         case DDS:
  681         case QIC_525:
  682         case QIC_1320:
  683         case QIC_3080:
  684                 st->flags &= ~ST_FIXEDBLOCKS;
  685                 st->blksiz = 0;
  686                 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
  687                 goto done;
  688         case QIC_11:
  689         case QIC_24:
  690         case QIC_120:
  691         case QIC_150:
  692                 st->flags |= ST_FIXEDBLOCKS;
  693                 if (st->media_blksiz > 0) {
  694                         st->blksiz = st->media_blksiz;
  695                 } else {
  696                         st->blksiz = DEF_FIXED_BSIZE;
  697                 }
  698                 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
  699                 goto done;
  700         }
  701         /*
  702          * If we're about to read the tape, perhaps we should choose
  703          * fixed or variable-length blocks and block size according to
  704          * what the drive found on the tape.
  705          */
  706         if (first_read
  707             && (!(st->quirks & ST_Q_BLKSIZ)
  708                 || (st->media_blksiz == 0)
  709                 || (st->media_blksiz == DEF_FIXED_BSIZE)
  710                 || (st->media_blksiz == 1024))) {
  711                 if (st->media_blksiz == 0) {
  712                         st->flags &= ~ST_FIXEDBLOCKS;
  713                 } else {
  714                         st->flags |= ST_FIXEDBLOCKS;
  715                 }
  716                 st->blksiz = st->media_blksiz;
  717                 SC_DEBUG(sc_link, SDEV_DB3,
  718                     ("Used media_blksiz of %ld\n", st->media_blksiz));
  719                 goto done;
  720         }
  721         /*
  722          * We're getting no hints from any direction.  Choose variable-
  723          * length blocks arbitrarily.
  724          */
  725         st->flags &= ~ST_FIXEDBLOCKS;
  726         st->blksiz = 0;
  727         SC_DEBUG(sc_link, SDEV_DB3, ("Give up and default to variable mode\n"));
  728 done:
  729 
  730         /*
  731          * Decide whether or not to write two file marks to signify end-
  732          * of-data.  Make the decision as a function of density.  If
  733          * the decision is not to use a second file mark, the SCSI BLANK
  734          * CHECK condition code will be recognized as end-of-data when
  735          * first read.
  736          * (I think this should be a by-product of fixed/variable..julian)
  737          */
  738         switch ((int)st->density) {
  739 /*      case    8 mm:   What is the SCSI density code for 8 mm, anyway? */
  740         case QIC_11:
  741         case QIC_24:
  742         case QIC_120:
  743         case QIC_150:
  744         case QIC_525:
  745         case QIC_1320:
  746         case QIC_3080:
  747                 st->flags &= ~ST_2FM_AT_EOD;
  748                 break;
  749         default:
  750                 st->flags |= ST_2FM_AT_EOD;
  751         }
  752         return 0;
  753 }
  754 
  755 /*
  756  * Actually translate the requested transfer into
  757  * one the physical driver can understand
  758  * The transfer is described by a buf and will include
  759  * only one physical transfer.
  760  */
  761 static  void
  762 st_strategy(struct buf *bp, struct scsi_link *sc_link)
  763 {
  764         u_int32_t unit;
  765         u_int32_t opri;
  766         struct scsi_data *st;
  767         int     len;
  768 
  769         unit = STUNIT((bp->b_dev));
  770         st = sc_link->sd;
  771         /*
  772          * If it's a null transfer, return immediatly
  773          */
  774         if ((len = bp->b_bcount) == 0) {
  775                 goto done;
  776         }
  777         /*
  778          * Check the adapter can do it
  779          */
  780         scsi_minphys(bp,&st_switch);
  781         /*
  782          * Odd sized request on fixed drives are verboten
  783          */
  784         if (st->flags & ST_FIXEDBLOCKS) {
  785                 if (bp->b_bcount % st->blksiz) {
  786                         printf("st%ld: bad request, must be multiple of %ld\n",
  787                             unit, st->blksiz);
  788                         bp->b_error = EIO;
  789                         goto bad;
  790                 }
  791         }
  792         /*
  793          * as are out-of-range requests on variable drives.
  794          * (or if we got chopped by minphys)
  795          */
  796         else {
  797                 if ((bp->b_bcount < st->blkmin || bp->b_bcount > st->blkmax)) {
  798                         printf("st%ld: bad request, must be between %ld and %ld\n",
  799                                 unit, st->blkmin, st->blkmax);
  800                         bp->b_error = EIO;
  801                         goto bad;
  802                 }
  803                 if (len != bp->b_bcount) {
  804                         printf("st%ld: bad request, must be less than %ld bytes\n",
  805                                 unit, bp->b_bcount + 1);
  806                         bp->b_error = EIO;
  807                         goto bad;
  808                 }
  809         }
  810         opri = splbio();
  811 
  812         /*
  813          * Use a bounce buffer if necessary
  814          */
  815 #ifdef BOUNCE_BUFFERS
  816         if (sc_link->flags & SDEV_BOUNCE)
  817                 vm_bounce_alloc(bp);
  818 #endif
  819 
  820         /*
  821          * Place it in the queue of activities for this tape
  822          * at the end (a bit silly because we only have on user..
  823          * (but it could fork() ))
  824          */
  825         bufq_insert_tail(&st->buf_queue, bp);
  826 
  827         /*
  828          * Tell the device to get going on the transfer if it's
  829          * not doing anything, otherwise just wait for completion
  830          * (All a bit silly if we're only allowing 1 open but..)
  831          */
  832         ststart(unit, 0);
  833 
  834         splx(opri);
  835         return;
  836 bad:
  837         bp->b_flags |= B_ERROR;
  838 done:
  839         /*
  840          * Correctly set the buf to indicate a completed xfer
  841          */
  842         biodone(bp);
  843         return;
  844 }
  845 
  846 /*
  847  * ststart looks to see if there is a buf waiting for the device
  848  * and that the device is not already busy. If both are true,
  849  * It dequeues the buf and creates a scsi command to perform the
  850  * transfer required. The transfer request will call scsi_done
  851  * on completion, which will in turn call this routine again
  852  * so that the next queued transfer is performed.
  853  * The bufs are queued by the strategy routine (ststrategy)
  854  *
  855  * This routine is also called after other non-queued requests
  856  * have been made of the scsi driver, to ensure that the queue
  857  * continues to be drained.
  858  * ststart() is called at splbio
  859  */
  860 static  void
  861 ststart(unit, flags)
  862         u_int32_t       unit;
  863         u_int32_t flags;
  864 {
  865         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
  866         struct scsi_data *st = sc_link->sd;
  867         register struct buf *bp = 0;
  868         struct scsi_rw_tape cmd;
  869 
  870         SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
  871         /*
  872          * See if there is a buf to do and we are not already
  873          * doing one
  874          */
  875         while (sc_link->opennings != 0) {
  876 
  877                 /* if a special awaits, let it proceed first */
  878                 if (sc_link->flags & SDEV_WAITING) {
  879                         sc_link->flags &= ~SDEV_WAITING;
  880                         wakeup((caddr_t)sc_link);
  881                         return;
  882                 }
  883 
  884                 bp = bufq_first(&st->buf_queue);
  885                 if (bp == NULL) {       /* yes, an assign */
  886                         return;
  887                 }
  888                 bufq_remove(&st->buf_queue, bp);
  889 
  890                 /*
  891                  * if the device has been unmounted by the user
  892                  * then throw away all requests until done
  893                  */
  894                 if ((!(st->flags & ST_MOUNTED))
  895                     || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
  896                         /* make sure that one implies the other.. */
  897                         sc_link->flags &= ~SDEV_MEDIA_LOADED;
  898                         goto badnews;
  899                 }
  900                 /*
  901                  * only FIXEDBLOCK devices have pending operations
  902                  */
  903                 if (st->flags & ST_FIXEDBLOCKS) {
  904                         /*
  905                          * If we are at a filemark but have not reported it yet
  906                          * then we should report it now
  907                          */
  908                         if (st->flags & ST_AT_FILEMARK) {
  909                                 if ((bp->b_flags & B_READ) == B_WRITE) {
  910                                         /*
  911                                          * Handling of ST_AT_FILEMARK in
  912                                          * st_space will fill in the right file
  913                                          * mark count.
  914                                          * Back up over filemark
  915                                          */
  916                                         if (st_space(unit, 0, SP_FILEMARKS, 0) !=
  917                                             ESUCCESS)
  918                                                 goto badnews;
  919                                 } else {
  920                                         bp->b_resid = bp->b_bcount;
  921                                         bp->b_error = 0;
  922                                         bp->b_flags &= ~B_ERROR;
  923                                         st->flags &= ~ST_AT_FILEMARK;
  924                                         biodone(bp);
  925                                         continue;       /* seek more work */
  926                                 }
  927                         }
  928                         /*
  929                          * If we are at EIO (e.g. EOM) but have not reported it
  930                          * yet then we should report it now
  931                          */
  932                         if (st->flags & ST_EIO_PENDING) {
  933                                 bp->b_resid = bp->b_bcount;
  934                                 bp->b_error = EIO;
  935                                 bp->b_flags |= B_ERROR;
  936                                 st->flags &= ~ST_EIO_PENDING;
  937                                 biodone(bp);
  938                                 continue;       /* seek more work */
  939                         }
  940                 }
  941                 /*
  942                  *  Fill out the scsi command
  943                  */
  944                 bzero(&cmd, sizeof(cmd));
  945                 if ((bp->b_flags & B_READ) == B_WRITE) {
  946                         cmd.op_code = WRITE_COMMAND_TAPE;
  947                         st->flags &= ~ST_FM_WRITTEN;
  948                         st->flags |= ST_WRITTEN;
  949                         flags |= SCSI_DATA_OUT;
  950                 } else {
  951                         cmd.op_code = READ_COMMAND_TAPE;
  952                         flags |= SCSI_DATA_IN;
  953                 }
  954                 /*
  955                  * Handle "fixed-block-mode" tape drives by using the
  956                  * block count instead of the length.
  957                  */
  958                 if (st->flags & ST_FIXEDBLOCKS) {
  959                         cmd.byte2 |= SRWT_FIXED;
  960                         scsi_uto3b(bp->b_bcount / st->blksiz, cmd.len);
  961                 } else {
  962                         scsi_uto3b(bp->b_bcount, cmd.len);
  963                 }
  964                 /*
  965                  * go ask the adapter to do all this for us
  966                  */
  967                 if (scsi_scsi_cmd(sc_link,
  968                         (struct scsi_generic *) &cmd,
  969                         sizeof(cmd),
  970                         (u_char *) bp->b_un.b_addr,
  971                         bp->b_bcount,
  972                         0,      /* can't retry a read on a tape really */
  973                         100000,
  974                         bp,
  975                         flags) == SUCCESSFULLY_QUEUED) {
  976                 } else {
  977 badnews:
  978                         printf("st%ld: oops not queued\n", unit);
  979                         bp->b_flags |= B_ERROR;
  980                         bp->b_error = EIO;
  981                         biodone(bp);
  982                 }
  983         } /* go back and see if we can cram more work in.. */
  984 }
  985 
  986 /*
  987  * Perform special action on behalf of the user;
  988  * knows about the internals of this device
  989  */
  990 static  errval
  991 st_ioctl(dev_t dev, int cmd, caddr_t arg, int   flag,
  992 struct proc *p, struct scsi_link *sc_link)
  993 {
  994         errval  errcode = 0;
  995         u_int32_t unit;
  996         u_int32_t number, flags, dsty;
  997         struct scsi_data *st;
  998         u_int32_t hold_blksiz;
  999         u_int32_t hold_density;
 1000         int32_t   nmarks;
 1001         struct mtop *mt = (struct mtop *) arg;
 1002 
 1003         /*
 1004          * Find the device that the user is talking about
 1005          */
 1006         flags = 0;              /* give error messages, act on errors etc. */
 1007         unit = STUNIT(dev);
 1008         dsty = DSTY(dev);
 1009         st = sc_link->sd;
 1010         hold_blksiz = st->blksiz;
 1011         hold_density = st->density;
 1012 
 1013         switch (cmd) {
 1014 
 1015         case MTIOCGET:
 1016                 {
 1017                         struct mtget *g = (struct mtget *) arg;
 1018 
 1019                         SC_DEBUG(sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
 1020                         bzero(g, sizeof(struct mtget));
 1021                         g->mt_type = 0x7;       /* Ultrix compat *//*? */
 1022                         g->mt_density = st->density;
 1023                         g->mt_blksiz = st->blksiz;
 1024                         g->mt_comp = st->comp;
 1025                         g->mt_density0 = st->modes[0].density;
 1026                         g->mt_density1 = st->modes[1].density;
 1027                         g->mt_density2 = st->modes[2].density;
 1028                         g->mt_density3 = st->modes[3].density;
 1029                         g->mt_blksiz0 = st->modes[0].blksiz;
 1030                         g->mt_blksiz1 = st->modes[1].blksiz;
 1031                         g->mt_blksiz2 = st->modes[2].blksiz;
 1032                         g->mt_blksiz3 = st->modes[3].blksiz;
 1033                         g->mt_comp0 = 0;
 1034                         g->mt_comp1 = 0;
 1035                         g->mt_comp2 = 0;
 1036                         g->mt_comp3 = 0;
 1037                         break;
 1038                 }
 1039         case MTIOCTOP:
 1040                 {
 1041 
 1042                         SC_DEBUG(sc_link, SDEV_DB1, ("[ioctl: op=0x%x count=0x%lx]\n",
 1043                                 mt->mt_op, mt->mt_count));
 1044 
 1045                         /* compat: in U*x it is a short */
 1046                         number = mt->mt_count;
 1047                         switch ((short) (mt->mt_op)) {
 1048                         case MTWEOF:    /* write an end-of-file record */
 1049                                 errcode = st_write_filemarks(unit, number, flags);
 1050                                 break;
 1051                         case MTBSF:     /* backward space file */
 1052                                 number = -number;
 1053                         case MTFSF:     /* forward space file */
 1054                                 errcode = st_chkeod(unit, FALSE, &nmarks, flags);
 1055                                 if (errcode == ESUCCESS)
 1056                                         errcode = st_space(unit, number - nmarks,
 1057                                             SP_FILEMARKS, flags);
 1058                                 break;
 1059                         case MTBSR:     /* backward space record */
 1060                                 number = -number;
 1061                         case MTFSR:     /* forward space record */
 1062                                 errcode = st_chkeod(unit, TRUE, &nmarks, flags);
 1063                                 if (errcode == ESUCCESS)
 1064                                         errcode = st_space(unit, number, SP_BLKS, flags);
 1065                                 break;
 1066                         case MTEOD:     /* space to end of recorded medium */
 1067                                 errcode = st_chkeod(unit, FALSE, &nmarks, flags);
 1068                                 if (errcode == ESUCCESS)
 1069                                         errcode = st_space(unit, 0, SP_EOM,
 1070                                                            flags);
 1071                                 break;
 1072                         case MTREW:     /* rewind */
 1073                                 errcode = st_rewind(unit, FALSE, flags);
 1074                                 break;
 1075                         case MTERASE:   /* erase */
 1076                                 errcode = st_erase(unit, FALSE, flags);
 1077                                 break;
 1078                         case MTRETENS:  /* re-tension tape */
 1079                                 errcode = st_load(unit, LD_LOAD|LD_RETEN,
 1080                                                   flags);
 1081                                 break;
 1082                         case MTOFFL:    /* rewind and put the drive offline */
 1083                                 /*
 1084                                  * Be sure to allow media removal before
 1085                                  * attempting the eject.
 1086                                  */
 1087                                 scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
 1088                                 st_unmount(unit, EJECT);
 1089                                 break;
 1090                         case MTNOP:     /* no operation, sets status only */
 1091                         case MTCACHE:   /* enable controller cache */
 1092                         case MTNOCACHE: /* disable controller cache */
 1093                                 break;
 1094                         case MTSETBSIZ: /* Set block size for device */
 1095 #ifdef  NOTYET
 1096                                 if (!(st->flags & ST_NEW_MOUNT)) {
 1097                                         uprintf("re-mount tape before changing blocksize");
 1098                                         errcode = EINVAL;
 1099                                         break;
 1100                                 }
 1101 #endif
 1102                                 if (number == 0) {
 1103                                         st->flags &= ~ST_FIXEDBLOCKS;
 1104                                 } else {
 1105                                         if ((st->blkmin || st->blkmax)  /* they exist */
 1106                                             &&((number < st->blkmin
 1107                                                     || number > st->blkmax))) {
 1108                                                 errcode = EINVAL;
 1109                                                 break;
 1110                                         }
 1111                                         st->flags |= ST_FIXEDBLOCKS;
 1112                                 }
 1113                                 st->blksiz = number;
 1114                                 st->flags |= ST_BLOCK_SET;      /*XXX */
 1115                                 goto try_new_value;
 1116 
 1117                         case MTSETDNSTY:        /* Set density for device and mode */
 1118                                 if (number > (u_int8_t)0xff) {
 1119                                         /* Guard against overflows */
 1120                                         errcode = EINVAL;
 1121                                 } else {
 1122                                         st->density = number;
 1123                                         goto try_new_value;
 1124                                 }
 1125                                 break;
 1126                         case MTCOMP:    /* enable default compression */
 1127                                 errcode = st_comp(unit,number);
 1128                                 break;
 1129                         default:
 1130                                 errcode = EINVAL;
 1131                         }
 1132                         break;
 1133                 }
 1134         case MTIOCIEOT:
 1135         case MTIOCEEOT:
 1136                 break;
 1137         default:
 1138                 if(IS_CTLMODE(dev))
 1139                         errcode = scsi_do_ioctl(dev, cmd, arg, flag, p, sc_link);
 1140                 else
 1141                         errcode = ENOTTY;
 1142                 break;
 1143         }
 1144         return errcode;
 1145 /*-----------------------------*/
 1146 try_new_value:
 1147         /*
 1148          * Check that the mode being asked for is aggreeable to the
 1149          * drive. If not, put it back the way it was.
 1150          */
 1151         if ( (errcode = st_mode_select(unit, 0, NULL, 0, 0)) ) {        
 1152                 /*
 1153                  * put back as it was 
 1154                  */
 1155                 printf("st%ld: Cannot set selected mode", unit);
 1156                 st->density = hold_density;
 1157                 st->blksiz = hold_blksiz;
 1158                 if (st->blksiz) {
 1159                         st->flags |= ST_FIXEDBLOCKS;
 1160                 } else {
 1161                         st->flags &= ~ST_FIXEDBLOCKS;
 1162                 }
 1163                 return (errcode);
 1164         }
 1165         /*
 1166          * As the drive liked it, if we are setting a new default,
 1167          * set it into the structures as such.
 1168          *
 1169          * The means for deciding this are not finalised yet
 1170          */
 1171         if (IS_CTLMODE(dev)) {
 1172                 /* special mode */
 1173                 /* XXX */
 1174                 switch ((short) (mt->mt_op)) {
 1175                 case MTSETBSIZ:
 1176                         st->modes[dsty].blksiz = st->blksiz;
 1177                         st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
 1178                         break;
 1179                 case MTSETDNSTY:
 1180                         st->modes[dsty].density = st->density;
 1181                         st->modeflags[dsty] |= DENSITY_SET_BY_USER;
 1182                         break;
 1183                 }
 1184         }
 1185         return 0;
 1186 }
 1187 
 1188 /*
 1189  * Do a synchronous read.
 1190  */
 1191 static errval
 1192 st_read(unit, buf, size, flags)
 1193         u_int32_t unit, size, flags;
 1194         char   *buf;
 1195 {
 1196         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1197         struct scsi_data *st = sc_link->sd;
 1198         struct scsi_rw_tape scsi_cmd;
 1199 
 1200         /*
 1201          * If it's a null transfer, return immediatly
 1202          */
 1203         if (size == 0) {
 1204                 return (ESUCCESS);
 1205         }
 1206         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1207         scsi_cmd.op_code = READ_COMMAND_TAPE;
 1208         if (st->flags & ST_FIXEDBLOCKS) {
 1209                 scsi_cmd.byte2 |= SRWT_FIXED;
 1210                 scsi_uto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
 1211                     scsi_cmd.len);
 1212         } else {
 1213                 scsi_uto3b(size, scsi_cmd.len);
 1214         }
 1215         return (scsi_scsi_cmd(sc_link,
 1216                 (struct scsi_generic *) &scsi_cmd,
 1217                 sizeof(scsi_cmd),
 1218                 (u_char *) buf,
 1219                 size,
 1220                 0,              /* not on io commands */
 1221                 100000,
 1222                 NULL,
 1223                 flags | SCSI_DATA_IN));
 1224 }
 1225 #ifdef  __STDC__
 1226 #define b2tol(a)        (((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
 1227 #else
 1228 #define b2tol(a)        (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0 )
 1229 #endif
 1230 
 1231 /*
 1232  * Ask the drive what it's min and max blk sizes are.
 1233  */
 1234 static  errval
 1235 st_rd_blk_lim(unit, flags)
 1236         u_int32_t unit, flags;
 1237 {
 1238         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1239         struct scsi_data *st = sc_link->sd;
 1240         struct scsi_blk_limits scsi_cmd;
 1241         struct scsi_blk_limits_data scsi_blkl;
 1242         errval  errno;
 1243 
 1244         /*
 1245          * First check if we have it all loaded
 1246          */
 1247         if ((sc_link->flags & SDEV_MEDIA_LOADED))
 1248                 return 0;
 1249 
 1250         /*
 1251          * do a 'Read Block Limits'
 1252          */
 1253         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1254         scsi_cmd.op_code = READ_BLK_LIMITS;
 1255 
 1256         /*
 1257          * do the command, update the global values
 1258          */
 1259         if ( (errno = scsi_scsi_cmd(sc_link,
 1260                 (struct scsi_generic *) &scsi_cmd,
 1261                 sizeof(scsi_cmd),
 1262                 (u_char *) & scsi_blkl,
 1263                 sizeof(scsi_blkl),
 1264                 ST_RETRIES,
 1265                 5000,
 1266                 NULL,
 1267                 flags | SCSI_DATA_IN)) ) {
 1268                 return errno;
 1269         }
 1270         st->blkmin = b2tol(scsi_blkl.min_length);
 1271         st->blkmax = scsi_3btou(&scsi_blkl.max_length_2);
 1272 
 1273         SC_DEBUG(sc_link, SDEV_DB3,
 1274             ("(%ld <= blksiz <= %ld)\n", st->blkmin, st->blkmax));
 1275         return 0;
 1276 }
 1277 
 1278 /*
 1279  * Get the scsi driver to send a full inquiry to the
 1280  * device and use the results to fill out the global
 1281  * parameter structure.
 1282  *
 1283  * called from:
 1284  * attach
 1285  * open
 1286  * ioctl (to reset original blksize)
 1287  */
 1288 static errval
 1289 st_mode_sense(unit, flags, page, pagelen, pagecode)
 1290         u_int32_t unit, flags;
 1291         struct tape_pages *page;
 1292         u_int32_t pagelen,pagecode;
 1293 {
 1294         u_int32_t dat_len;
 1295         errval  errno;
 1296         struct scsi_mode_sense scsi_cmd;
 1297         struct {
 1298                 struct scsi_mode_header header;
 1299                 struct blk_desc blk_desc;
 1300                 struct tape_pages page;
 1301         } dat;
 1302 
 1303         /* Tandberg tape drives returns page 00
 1304          * with the sense data, whether or not
 1305          * you want it( ie the don't like you
 1306          * saying you want anything less!!!!!
 1307          * They also expect page 00
 1308          * back when you issue a mode select
 1309          */
 1310         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1311         struct scsi_data *st = sc_link->sd;
 1312 
 1313         st->flags &= ~ST_SENSE_READ;
 1314         /*
 1315          * Check if we need to use a default page..
 1316          */
 1317         if ((st->quirks & ST_Q_NEEDS_PAGE_0) && (!page)) {
 1318                 pagelen = PAGE_0_SENSE_DATA_SIZE;
 1319                 page = (struct tape_pages *) st->saved_page0;
 1320                 pagecode = 0;
 1321         }
 1322         /*
 1323          * Now work out the total dat size etc.
 1324          */
 1325         dat_len = sizeof(struct scsi_mode_header)
 1326                 + sizeof(struct blk_desc)
 1327                 + (page ? pagelen : 0);
 1328         /*
 1329          * Set up a mode sense
 1330          */
 1331         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1332         scsi_cmd.op_code = MODE_SENSE;
 1333         scsi_cmd.page = (u_char) pagecode;
 1334         scsi_cmd.length = dat_len;
 1335 
 1336         /*
 1337          * do the command,
 1338          * use the results to set blksiz, numblks and density
 1339          * or if we need it as a template for the mode select
 1340          * store it away.
 1341          */
 1342         if ( (errno = scsi_scsi_cmd(sc_link,
 1343                 (struct scsi_generic *) &scsi_cmd,
 1344                 sizeof(scsi_cmd),
 1345                 (u_char *) &dat,
 1346                 dat_len,
 1347                 ST_RETRIES,
 1348                 5000,
 1349                 NULL,
 1350                 flags | SCSI_DATA_IN)) ) {
 1351                 return errno;
 1352         }
 1353         st->numblks = scsi_3btou(dat.blk_desc.nblocks);
 1354         st->media_blksiz = scsi_3btou(dat.blk_desc.blklen);
 1355         st->media_density = dat.blk_desc.density;
 1356         if (dat.header.dev_spec & SMH_DSP_WRITE_PROT) {
 1357                 st->flags |= ST_READONLY;
 1358         }
 1359         SC_DEBUG(sc_link, SDEV_DB3,
 1360             ("density code 0x%lx, %ld-byte blocks, write-%s, ",
 1361                 st->media_density, st->media_blksiz,
 1362                 st->flags & ST_READONLY ? "protected" : "enabled"));
 1363         SC_DEBUG(sc_link, SDEV_DB3, ("%sbuffered\n",
 1364                 ((dat.header.dev_spec & SMH_DSP_BUFF_MODE) ? "" : "un")));
 1365         if (page) {
 1366                 bcopy(&dat.page, page, pagelen);
 1367         }
 1368         st->flags |= ST_SENSE_READ;
 1369         return 0;
 1370 }
 1371 
 1372 /*
 1373  * Send a filled out parameter structure to the drive to
 1374  * set it into the desire modes etc.
 1375  */
 1376 static  errval
 1377 st_mode_select(unit, flags, page, pagelen, byte2)
 1378         u_int32_t unit, flags;
 1379         struct tape_pages *page;
 1380         u_int32_t pagelen;
 1381         u_int32_t byte2;
 1382 {
 1383         u_int32_t dat_len;
 1384         struct scsi_mode_select scsi_cmd;
 1385         struct {
 1386                 struct scsi_mode_header header;
 1387                 struct blk_desc blk_desc;
 1388                 struct tape_pages page;
 1389         } dat;
 1390         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1391         struct scsi_data *st = sc_link->sd;
 1392 
 1393         /*
 1394          * Check if we need to use a default page..
 1395          * Gee, hope we saved one before now........
 1396          */
 1397         if ((st->quirks & ST_Q_NEEDS_PAGE_0) && (!page)) {
 1398                 pagelen = PAGE_0_SENSE_DATA_SIZE;
 1399                 page = (struct tape_pages *) st->saved_page0;
 1400         }
 1401         /*
 1402          * Now work out the total dat size etc.
 1403          */
 1404         dat_len = sizeof(struct scsi_mode_header)
 1405                 + sizeof(struct blk_desc)
 1406                 + (page ? pagelen : 0);
 1407         /*
 1408          * Set up for a mode select
 1409          */
 1410         bzero(&dat, dat_len);
 1411         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1412         scsi_cmd.op_code = MODE_SELECT;
 1413         scsi_cmd.length = dat_len;
 1414         scsi_cmd.byte2 = (u_char)byte2;
 1415         dat.header.blk_desc_len = sizeof(struct blk_desc);
 1416         dat.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
 1417         dat.blk_desc.density = st->density;
 1418         if (st->flags & ST_FIXEDBLOCKS) {
 1419                 scsi_uto3b(st->blksiz, dat.blk_desc.blklen);
 1420         }
 1421         if (page) {
 1422                 bcopy(page, &dat.page, pagelen);
 1423                 /* the Tandberg tapes need the block size to */
 1424                 /* be set on each mode sense/select. */
 1425         }
 1426         /*
 1427          * do the command
 1428          */
 1429         return (scsi_scsi_cmd(sc_link,
 1430                 (struct scsi_generic *) &scsi_cmd,
 1431                 sizeof(scsi_cmd),
 1432                 (u_char *) &dat,
 1433                 dat_len,
 1434                 ST_RETRIES,
 1435                 5000,
 1436                 NULL,
 1437                 flags | SCSI_DATA_OUT));
 1438 }
 1439 
 1440 static int noisy_st = 0;
 1441 /***************************************************************\
 1442 * Set the compression mode of the drive to on (1) or off (0)    *
 1443         still doesn't work! grrr!
 1444 \***************************************************************/
 1445 static  errval
 1446 st_comp(unit,mode)
 1447 u_int32_t unit,mode;
 1448 {
 1449         struct tape_pages       page;
 1450         int     pagesize;
 1451         int retval;
 1452         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1453         struct scsi_data *st = sc_link->sd;
 1454 
 1455         bzero(&page, sizeof(page));
 1456         pagesize = sizeof(page.pages.configuration) + PAGE_HEADERLEN;
 1457 
 1458         if ( (retval = st_mode_sense(unit, 0,
 1459                         &page, pagesize, ST_PAGE_CONFIGURATION)) )
 1460         {
 1461                 printf("sense returned an error of %d\n",retval);
 1462                 return retval;
 1463         }
 1464         if ( noisy_st)
 1465                 printf("drive reports value of %d, setting %ld\n",
 1466                         page.pages.configuration.data_compress_alg,mode);
 1467 
 1468         page.pg_code &= ST_P_CODE;
 1469         page.pg_length = sizeof(page.pages.configuration);
 1470 
 1471         switch(mode)
 1472         {
 1473         case 0:
 1474                 page.pages.configuration.data_compress_alg = 0;
 1475                 break;
 1476         case 1:
 1477                 page.pages.configuration.data_compress_alg = 1;
 1478                 break;
 1479         default:
 1480                 printf("st%ld: bad value for compression mode\n",unit);
 1481                 return EINVAL;
 1482         }
 1483         /*
 1484          * send ST_PAGE_CONFIGURATION page as SCSI-II command because it
 1485          * is a SCSI-II structure.  This requires the PF bit (0x10) to be
 1486          * set for byte2.
 1487          */
 1488         if ( (retval = st_mode_select(unit, 0, &page, pagesize, 0x10)) )
 1489         {
 1490                 printf("select returned an error of %d\n",retval);
 1491                 return retval;
 1492         }
 1493         st->comp = mode;
 1494         return 0;
 1495 }
 1496 /*
 1497  * skip N blocks/filemarks/seq filemarks/eom
 1498  */
 1499 static  errval
 1500 st_space(unit, number, what, flags)
 1501         u_int32_t unit, what, flags;
 1502         int32_t   number;
 1503 {
 1504         errval  error;
 1505         struct scsi_space scsi_cmd;
 1506         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1507         struct scsi_data *st = sc_link->sd;
 1508 
 1509         switch ((int)what) {
 1510         case SP_BLKS:
 1511                 if (st->flags & ST_PER_ACTION) {
 1512                         if (number > 0) {
 1513                                 st->flags &= ~ST_PER_ACTION;
 1514                                 return (EIO);
 1515                         } else if (number < 0) {
 1516                                 if (st->flags & ST_AT_FILEMARK) {
 1517                                         /*
 1518                                          * Handling of ST_AT_FILEMARK
 1519                                          * in st_space will fill in the
 1520                                          * right file mark count.
 1521                                          */
 1522                                         error = st_space(unit, 0, SP_FILEMARKS,
 1523                                             flags);
 1524                                         if (error)
 1525                                                 return (error);
 1526                                 }
 1527                                 if (st->flags & ST_BLANK_READ) {
 1528                                         st->flags &= ~ST_BLANK_READ;
 1529                                         return (EIO);
 1530                                 }
 1531                                 st->flags &= ~ST_EIO_PENDING;
 1532                         }
 1533                 }
 1534                 break;
 1535         case SP_FILEMARKS:
 1536                 if (st->flags & ST_EIO_PENDING) {
 1537                         if (number > 0) {       /* pretend we just discover the error */
 1538                                 st->flags &= ~ST_EIO_PENDING;
 1539                                 return (EIO);
 1540                         } else if (number < 0) {        /* back away from the error */
 1541                                 st->flags &= ~ST_EIO_PENDING;
 1542                         }
 1543                 }
 1544                 if (st->flags & ST_AT_FILEMARK) {
 1545                         st->flags &= ~ST_AT_FILEMARK;
 1546                         number--;
 1547                 }
 1548                 if ((st->flags & ST_BLANK_READ) && (number < 0)) {      /* back away from unwritten tape */
 1549                         st->flags &= ~ST_BLANK_READ;
 1550                         number++;       /* dubious */
 1551                 }
 1552                 break;
 1553         case    SP_EOM:
 1554                 if (st->flags & ST_EIO_PENDING)
 1555                 {
 1556                         /* we are already at EOM */
 1557                         st->flags &= ~ST_EIO_PENDING;
 1558                         return(ESUCCESS);
 1559                 }
 1560                 number = 1;     /* we have only one end-of-medium */
 1561                 break;
 1562         }
 1563         if (number == 0) {
 1564                 return (ESUCCESS);
 1565         }
 1566         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1567         scsi_cmd.op_code = SPACE;
 1568         scsi_cmd.byte2 = what & SS_CODE;
 1569         scsi_uto3b(number, scsi_cmd.number);
 1570         return (scsi_scsi_cmd(sc_link,
 1571                 (struct scsi_generic *) &scsi_cmd,
 1572                 sizeof(scsi_cmd),
 1573                 0,
 1574                 0,
 1575                 0,              /* no retries please , just fail */
 1576                 3600000,        /* wait up to an hour for completion */
 1577                 NULL,
 1578                 flags));
 1579 }
 1580 
 1581 /*
 1582  * write N filemarks
 1583  */
 1584 static  errval
 1585 st_write_filemarks(unit, number, flags)
 1586         u_int32_t unit, flags;
 1587         int32_t   number;
 1588 {
 1589         struct scsi_write_filemarks scsi_cmd;
 1590         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1591         struct scsi_data *st = sc_link->sd;
 1592 
 1593         /*
 1594          * It's hard to write a negative number of file marks.
 1595          * Don't try.
 1596          */
 1597         if (number < 0) {
 1598                 return EINVAL;
 1599         }
 1600         switch ((int)number) {
 1601         case 0:         /* really a command to sync the drive's buffers */
 1602                 break;
 1603         case 1:
 1604                 if (st->flags & ST_FM_WRITTEN) {        /* already have one down */
 1605                         st->flags &= ~ST_WRITTEN;
 1606                 } else {
 1607                         st->flags |= ST_FM_WRITTEN;
 1608                 }
 1609                 st->flags &= ~ST_PER_ACTION;
 1610                 break;
 1611         default:
 1612                 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
 1613         }
 1614         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1615         scsi_cmd.op_code = WRITE_FILEMARKS;
 1616         scsi_uto3b(number, scsi_cmd.number);
 1617         return scsi_scsi_cmd(sc_link,
 1618                 (struct scsi_generic *) &scsi_cmd,
 1619                 sizeof(scsi_cmd),
 1620                 0,
 1621                 0,
 1622                 0,              /* no retries, just fail */
 1623                 100000,         /* 10 secs.. (may need to repos head ) */
 1624                 NULL,
 1625                 flags);
 1626 }
 1627 
 1628 /*
 1629  * Make sure the right number of file marks is on tape if the
 1630  * tape has been written.  If the position argument is true,
 1631  * leave the tape positioned where it was originally.
 1632  *
 1633  * nmarks returns the number of marks to skip (or, if position
 1634  * true, which were skipped) to get back original position.
 1635  */
 1636 static  int32_t
 1637 st_chkeod(unit, position, nmarks, flags)
 1638         u_int32_t unit;
 1639         boolean position;
 1640         int32_t *nmarks;
 1641         u_int32_t flags;
 1642 {
 1643         errval  error;
 1644         struct scsi_data *st = SCSI_DATA(&st_switch, unit);
 1645 
 1646         switch ((int)(st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD))) {
 1647         default:
 1648                 *nmarks = 0;
 1649                 return (ESUCCESS);
 1650         case ST_WRITTEN:
 1651         case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
 1652                 *nmarks = 1;
 1653                 break;
 1654         case ST_WRITTEN | ST_2FM_AT_EOD:
 1655                 *nmarks = 2;
 1656         }
 1657         error = st_write_filemarks(unit, *nmarks, flags);
 1658         if (position && (error == ESUCCESS))
 1659                 error = st_space(unit, -*nmarks, SP_FILEMARKS, flags);
 1660         return (error);
 1661 }
 1662 
 1663 /*
 1664  * load/unload (with retension if true)
 1665  */
 1666 static  errval
 1667 st_load(unit, type, flags)
 1668         u_int32_t unit, type, flags;
 1669 {
 1670         struct scsi_load scsi_cmd;
 1671         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1672         struct scsi_data *st = sc_link->sd;
 1673 
 1674         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1675         if (type != LD_LOAD) {
 1676                 errval  error;
 1677                 int32_t nmarks;
 1678 
 1679                 error = st_chkeod(unit, FALSE, &nmarks, flags);
 1680                 if (error != ESUCCESS)
 1681                         return (error);
 1682                 sc_link->flags &= ~SDEV_MEDIA_LOADED;
 1683         }
 1684         if (st->quirks & ST_Q_IGNORE_LOADS)
 1685                 return (0);
 1686         scsi_cmd.op_code = LOAD_UNLOAD;
 1687         scsi_cmd.how |= type;
 1688         return (scsi_scsi_cmd(sc_link,
 1689                 (struct scsi_generic *) &scsi_cmd,
 1690                 sizeof(scsi_cmd),
 1691                 0,
 1692                 0,
 1693                 ST_RETRIES,
 1694                 900000,         /* 15 min */
 1695                 NULL,
 1696                 flags));
 1697 }
 1698 
 1699 /*
 1700  *  Rewind the device
 1701  */
 1702 static  errval
 1703 st_rewind(unit, immed, flags)
 1704         u_int32_t unit, flags;
 1705         boolean immed;
 1706 {
 1707         struct scsi_rewind scsi_cmd;
 1708         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1709         struct scsi_data *st = sc_link->sd;
 1710         errval  error;
 1711         int32_t nmarks;
 1712 
 1713         error = st_chkeod(unit, FALSE, &nmarks, flags);
 1714         if (error != ESUCCESS)
 1715                 return (error);
 1716         st->flags &= ~ST_PER_ACTION;
 1717         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1718         scsi_cmd.op_code = REWIND;
 1719         scsi_cmd.byte2 = immed ? SR_IMMED : 0;
 1720         return (scsi_scsi_cmd(sc_link,
 1721                 (struct scsi_generic *) &scsi_cmd,
 1722                 sizeof(scsi_cmd),
 1723                 0,
 1724                 0,
 1725                 ST_RETRIES,
 1726                 immed ? 5000 : 300000,  /* 5 sec or 5 min */
 1727                 NULL,
 1728                 flags));
 1729 }
 1730 
 1731 /*
 1732 **  Erase the device
 1733 */
 1734 static  errval
 1735 st_erase(unit, immed, flags)
 1736         u_int32_t unit, flags;
 1737         boolean immed;
 1738 {
 1739         struct scsi_erase scsi_cmd;
 1740         struct scsi_link *sc_link = SCSI_LINK(&st_switch, unit);
 1741         struct scsi_data *st = sc_link->sd;
 1742         errval  error;
 1743         int32_t nmarks;
 1744 
 1745         error = st_chkeod(unit, FALSE, &nmarks, flags);
 1746         if (error != ESUCCESS)
 1747                 return (error);
 1748         /*
 1749         **      Archive Viper 2525 technical manual 5.7 (ERASE 19h):
 1750         **      tape has to be positioned to BOT first before erase command
 1751         **      is issued or command is rejected. So we rewind the tape first
 1752         **      and exit with an error, if the tape can't be rewinded.
 1753         */
 1754         error = st_rewind(unit, FALSE, SCSI_SILENT);
 1755         if (error != ESUCCESS)
 1756                 return (error);
 1757         st->flags &= ~ST_PER_ACTION;
 1758         bzero(&scsi_cmd, sizeof(scsi_cmd));
 1759         scsi_cmd.op_code = ERASE;
 1760         scsi_cmd.byte2 = SE_LONG;               /* LONG_ERASE */
 1761         scsi_cmd.byte2 += immed ? SE_IMMED : 0; /* immed bit is here the 2nd! */
 1762         return (scsi_scsi_cmd(sc_link,
 1763                 (struct scsi_generic *) &scsi_cmd,
 1764                 sizeof(scsi_cmd),
 1765                 0,
 1766                 0,
 1767                 ST_RETRIES,
 1768                 immed ? 5000 : (200 * 60 * 1000),       /* 5 sec or 200 min */
 1769                 NULL,
 1770                 flags));
 1771 }
 1772 
 1773 /*
 1774  * Look at the returned sense and act on the error and detirmine
 1775  * The unix error number to pass back... (0 = report no error)
 1776  *                            (SCSIRET_CONTINUE = continue processing)
 1777  */
 1778 static  errval
 1779 st_interpret_sense(xs)
 1780         struct scsi_xfer *xs;
 1781 {
 1782         struct scsi_link *sc_link = xs->sc_link;
 1783         struct scsi_sense_data *sense = &(xs->sense);
 1784         boolean silent = xs->flags & SCSI_SILENT;
 1785         u_int32_t unit = sc_link->dev_unit;
 1786         struct scsi_data *st = SCSI_DATA(&st_switch, unit);
 1787         u_int32_t key;
 1788         int32_t   info;
 1789 
 1790         /*
 1791          * Get the sense fields and work out what code
 1792          */
 1793         if (sense->error_code & SSD_ERRCODE_VALID) {
 1794                 info = ntohl(*((int32_t *) sense->ext.extended.info));
 1795         } else {
 1796                 if (st->flags & ST_FIXEDBLOCKS) {
 1797                         info = xs->datalen / st->blksiz;
 1798                 } else {
 1799                         info = xs->datalen;
 1800                 }
 1801         }
 1802 
 1803         key = sense->ext.extended.flags & SSD_KEY;
 1804 
 1805         if (((sense->error_code & SSD_ERRCODE) != 0x70)
 1806           || (key == 0x7)) /* Media Write Protected */ {
 1807                 return SCSIRET_CONTINUE;/* let the generic code handle it */
 1808         }
 1809         if(sense->ext.extended.flags & (SSD_EOM|SSD_FILEMARK|SSD_ILI)) {
 1810                 if (st->flags & ST_FIXEDBLOCKS) {
 1811                         xs->resid = info * st->blksiz;
 1812                         xs->flags |= SCSI_RESID_VALID;
 1813                         if (sense->ext.extended.flags & SSD_EOM) {
 1814                                 st->flags |= ST_EIO_PENDING;
 1815                         }
 1816                         if (sense->ext.extended.flags & SSD_FILEMARK) {
 1817                                 st->flags |= ST_AT_FILEMARK;
 1818                         }
 1819                         if (sense->ext.extended.flags & SSD_ILI) {
 1820                                 st->flags |= ST_EIO_PENDING;
 1821                                 if (sense->error_code & SSD_ERRCODE_VALID &&
 1822                                 !silent)
 1823                                         printf("st%ld: block wrong size"
 1824                                         ", %ld blocks residual\n", unit
 1825                                         ,info);
 1826                                 /*XXX*/ /* is this how it works ? */
 1827                                 /* check def of ILI for fixed blk tapes */
 1828 
 1829                                 /*
 1830                                  * This quirk code helps the drive read
 1831                                  * the first tape block, regardless of
 1832                                  * format.  That is required for these
 1833                                  * drives to return proper MODE SENSE
 1834                                  * information.
 1835                                  */
 1836                                 if ((st->quirks & ST_Q_SNS_HLP) &&
 1837                                     !(st->flags & ST_SENSE_READ)) {
 1838                                         st->blksiz -= 512;
 1839                                 }
 1840                         }
 1841                         /*
 1842                          * If no data was tranfered, do it immediatly
 1843                          */
 1844                         if (xs->resid >= xs->datalen) {
 1845                                 xs->flags &= ~SCSI_RESID_VALID;
 1846                                 if (st->flags & ST_AT_FILEMARK) {
 1847                                         xs->flags |= SCSI_EOF;
 1848                                         st->flags &= ~ST_AT_FILEMARK;
 1849                                         return 0;
 1850                                 }
 1851                                 if (st->flags & ST_EIO_PENDING) {
 1852                                         st->flags &= ~ST_EIO_PENDING;
 1853                                         return EIO;
 1854                                 }
 1855                         }
 1856                         return 0;
 1857                 } else {                /* must be variable mode */
 1858                         xs->resid = xs->datalen;        /* to be sure */
 1859                         if (sense->ext.extended.flags & SSD_EOM) {
 1860                                 return (EIO);
 1861                         }
 1862                         if (sense->ext.extended.flags & SSD_FILEMARK) {
 1863                                 xs->flags |= SCSI_EOF;
 1864                         }
 1865                         if (sense->ext.extended.flags & SSD_ILI) {
 1866                                 if (info < 0) {
 1867                                         /*
 1868                                          * the record was bigger than the read
 1869                                          */
 1870                                         if (!silent)
 1871                                                 printf("st%ld: %ld-byte record "
 1872                                                 "too big\n", unit,
 1873                                                 xs->datalen - info);
 1874                                         return (EIO);
 1875                                 }
 1876                                 xs->resid = info;
 1877                                 xs->flags |= SCSI_RESID_VALID;
 1878                         }
 1879                 }
 1880                 return 0;
 1881         }
 1882 
 1883         if (key == 0x8) {
 1884                 xs->flags |= SCSI_EOF; /* some drives need this */
 1885                 /*
 1886                  * This quirk code helps the drive read the
 1887                  * first tape block, regardless of format.  That
 1888                  * is required for these drives to return proper
 1889                  * MODE SENSE information.
 1890                  */
 1891                 if ((st->quirks & ST_Q_SNS_HLP) &&
 1892                     !(st->flags & ST_SENSE_READ)) {
 1893                         /* still starting */
 1894                         st->blksiz -= 512;
 1895                 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
 1896                         st->flags |= ST_BLANK_READ;
 1897                         xs->flags |= SCSI_EOF;
 1898                         return (ESUCCESS);
 1899                 }
 1900         }
 1901         return SCSIRET_CONTINUE;  /* Use the the generic handler */
 1902 }
 1903 
 1904 /*
 1905  * The quirk here is that the drive returns some value to st_mode_sense
 1906  * incorrectly until the tape has actually passed by the head.
 1907  *
 1908  * The method is to set the drive to large fixed-block state (user-specified
 1909  * density and 1024-byte blocks), then read and rewind to get it to sense the
 1910  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
 1911  * work, as a last resort, try variable- length blocks.  The result will be
 1912  * the ability to do an accurate st_mode_sense.
 1913  *
 1914  * We know we can do a rewind because we just did a load, which implies rewind.
 1915  * Rewind seems preferable to space backward if we have a virgin tape.
 1916  *
 1917  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
 1918  * error processing, both part of st_interpret_sense.
 1919  */
 1920 static  errval
 1921 st_touch_tape(unit)
 1922         u_int32_t       unit;
 1923 {
 1924         struct scsi_data *st = SCSI_DATA(&st_switch, unit);
 1925         char   *buf;
 1926         u_int32_t readsiz;
 1927         errval  errno;
 1928 
 1929         buf = malloc(1024, M_TEMP, M_NOWAIT);
 1930         if (!buf)
 1931                 return (ENOMEM);
 1932 
 1933         if (( errno = st_mode_sense(unit, 0, NULL, 0, 0)) ) {
 1934                 goto bad;
 1935         }
 1936         st->blksiz = (st->quirks & ST_Q_NO_1024)? 512: 1024;
 1937         do {
 1938                 switch ((int)st->blksiz) {
 1939                 case 512:
 1940                 case 1024:
 1941                         readsiz = st->blksiz;
 1942                         st->flags |= ST_FIXEDBLOCKS;
 1943                         break;
 1944                 default:
 1945                         readsiz = 1;
 1946                         st->flags &= ~ST_FIXEDBLOCKS;
 1947                 } if ( (errno = st_mode_select(unit, 0, NULL, 0, 0)) ) {
 1948                         goto bad;
 1949                 }
 1950                 st_read(unit, buf, readsiz, SCSI_SILENT);
 1951                 if ( (errno = st_rewind(unit, FALSE, 0)) ) {
 1952 bad:                    free(buf, M_TEMP);
 1953                         return (errno);
 1954                 }
 1955         } while (readsiz != 1 && readsiz > st->blksiz);
 1956         free(buf, M_TEMP);
 1957         return 0;
 1958 }
 1959 
 1960 static st_devsw_installed = 0;
 1961 
 1962 static void 
 1963 st_drvinit(void *unused)
 1964 {
 1965 
 1966         if( ! st_devsw_installed ) {
 1967                 bdevsw_add_generic(BDEV_MAJOR, CDEV_MAJOR, &st_bdevsw);
 1968                 st_devsw_installed = 1;
 1969         }
 1970 }
 1971 
 1972 SYSINIT(stdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,st_drvinit,NULL)
 1973 
 1974 

Cache object: 18f4ef95e89177848e5cc75fecd7d863


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