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