History log of /openbsd-current/sys/scsi/st.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.190 27-Apr-2023 robert

add support for I/O statistics so that tape speeds can be observed with
iostat(8)


Revision tags: OPENBSD_7_1_BASE OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.189 27-Feb-2022 krw

SC_DEBUG() requires an initialized scsi_link. Shuffle
code to make it so.

Pointed out by clang when prodded with SCSIDEBUG.


# 1.188 11-Jan-2022 jsg

spelling


# 1.187 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.186 12-Mar-2021 jsg

spelling


Revision tags: OPENBSD_6_8_BASE
# 1.185 22-Sep-2020 krw

Since dlg@'s 2009 scsi midlayer refactoring the 'struct scsi_generic *cmd'
member of 'struct scsi_xfer' has always been pointed at the 'struct scsi_generic
cmdstore' member of the same instance. So nuke 'cmdstore' and remove the '*'
from cmd. Take the address of cmd as required by the various casts.

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

Identification of 2009's last *cmd use and ok jmatthew@


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.189 27-Feb-2022 krw

SC_DEBUG() requires an initialized scsi_link. Shuffle
code to make it so.

Pointed out by clang when prodded with SCSIDEBUG.


# 1.188 11-Jan-2022 jsg

spelling


# 1.187 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.186 12-Mar-2021 jsg

spelling


Revision tags: OPENBSD_6_8_BASE
# 1.185 22-Sep-2020 krw

Since dlg@'s 2009 scsi midlayer refactoring the 'struct scsi_generic *cmd'
member of 'struct scsi_xfer' has always been pointed at the 'struct scsi_generic
cmdstore' member of the same instance. So nuke 'cmdstore' and remove the '*'
from cmd. Take the address of cmd as required by the various casts.

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

Identification of 2009's last *cmd use and ok jmatthew@


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.188 11-Jan-2022 jsg

spelling


# 1.187 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.186 12-Mar-2021 jsg

spelling


Revision tags: OPENBSD_6_8_BASE
# 1.185 22-Sep-2020 krw

Since dlg@'s 2009 scsi midlayer refactoring the 'struct scsi_generic *cmd'
member of 'struct scsi_xfer' has always been pointed at the 'struct scsi_generic
cmdstore' member of the same instance. So nuke 'cmdstore' and remove the '*'
from cmd. Take the address of cmd as required by the various casts.

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

Identification of 2009's last *cmd use and ok jmatthew@


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.187 24-Oct-2021 mpi

Constify struct cfattach.

ok visa@ a long time ago, ok krw@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.186 12-Mar-2021 jsg

spelling


Revision tags: OPENBSD_6_8_BASE
# 1.185 22-Sep-2020 krw

Since dlg@'s 2009 scsi midlayer refactoring the 'struct scsi_generic *cmd'
member of 'struct scsi_xfer' has always been pointed at the 'struct scsi_generic
cmdstore' member of the same instance. So nuke 'cmdstore' and remove the '*'
from cmd. Take the address of cmd as required by the various casts.

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

Identification of 2009's last *cmd use and ok jmatthew@


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.186 12-Mar-2021 jsg

spelling


Revision tags: OPENBSD_6_8_BASE
# 1.185 22-Sep-2020 krw

Since dlg@'s 2009 scsi midlayer refactoring the 'struct scsi_generic *cmd'
member of 'struct scsi_xfer' has always been pointed at the 'struct scsi_generic
cmdstore' member of the same instance. So nuke 'cmdstore' and remove the '*'
from cmd. Take the address of cmd as required by the various casts.

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

Identification of 2009's last *cmd use and ok jmatthew@


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.185 22-Sep-2020 krw

Since dlg@'s 2009 scsi midlayer refactoring the 'struct scsi_generic *cmd'
member of 'struct scsi_xfer' has always been pointed at the 'struct scsi_generic
cmdstore' member of the same instance. So nuke 'cmdstore' and remove the '*'
from cmd. Take the address of cmd as required by the various casts.

No intentional functional change.

luna88k test by aoyama@, sparc64 test by jmatthew@

Identification of 2009's last *cmd use and ok jmatthew@


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.184 22-Aug-2020 krw

The last timeout_add(sc_timeout) died in the great XS_NO_CCB purge of 2017. Nuke
pointless timeout_set(sc_timoeut) and timeout_del(sc_timeout) calls and the
sc_timeout fields themselves.


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.183 20-Aug-2020 krw

Revert DYING. At least some USB memory sticks get very upset.


# 1.182 19-Aug-2020 krw

Nuke SDF_DYING, CDF_DYING and ST_DYING flags. Use the scsi_link flag
SDEV_S_DYING instead. Makes it clear that the scsi_link and the [cd|sd|st]
device always agree on their state.


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.181 15-Aug-2020 krw

The last SET()s of CDF_WAITING, SDF_WAITING, and ST_WAITING died in the great
XS_NO_CCB purge of 2017.

Nuke pointless ISSET()/CLR() checks and the #define's.


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.180 11-Aug-2020 krw

Explicitly #include <scsi/scsi_debug.h> rather than assuming scsiconf.h will do
it.

Prepares for removal of #include <scsi/scsi_debug.h> from scsiconf.h as soon as
the alpha tester digs out from hurricane to compile last untested files.


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.179 16-Jul-2020 krw

Access scsibus_softc info (luns, adapter, adapter_target,
adapter_softc, adapter_buswidth) via link->bus rather than using
copies currently residing in the link.


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.178 30-Jun-2020 krw

Nuke unneeded 'sa_inqbuf' member of struct scsi_attach_args. It always
points to the inquiry data contained in the struct scsi_link pointed
to by the other member, sa_sc_link.


Revision tags: OPENBSD_6_7_BASE
# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.177 20-Feb-2020 krw

Relax the grip the dead hand of QBUS (a.k.a. MAXPHYS) has on the
throat of physio().

Allows individual devices to eventually be modified to support larger
physio() (a.k.a. 'raw') i/o's if they prove capable of them.

No immediate functional change.

Tested for many weeks by and ok robert@.


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.176 05-Feb-2020 krw

Nuke unnecessary abstraction 'scsi_minphys()' which just calls
'minphys()'. Just use & check for NULL instead, since 'minphys()' is
always called on the code path ([cd|sd|st]minphys) that calls
physio().


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.175 27-Jan-2020 krw

Make the commonalities of cdminphys, sdminphys and stminphys more
obvious by consistently using the variable names and idiom of
sdminphys.

No functional change.


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.174 26-Jan-2020 krw

Shuffle some names around to make reading the code less headache
inducing. Rename scsi_adapter member 'scsi_minphys' to 'dev_minphys'
to reflect what it is supposed to do. Use consistent naming convention
(<dev>_minphys) for the actual device functions.

No functional change.


# 1.173 25-Jan-2020 krw

Ensure scsi_minphys() is always called on the physio() path.

Will allow simplification of individual driver *minphys() functions.

ok jmatthew@ as part of larger diff


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.172 05-Dec-2019 krw

Shrink scsi_mode_do_sense() parameter list by eliminating the three
pointers returning possible block descriptor values for block size,
block count and density. Most calls were passing "NULL, NULL, NULL"
since they did not care.

Call scsi_parse_blkdesc() directly in those few cases where one or
more of the values is of interest.

No intentional functional change.


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.171 26-Nov-2019 krw

Don't zero existing values for density, block_count and block_size in
scsi_do_mode_sense(). Just update the values when valid data is
available. Initialize the variables passed to scsi_do_mode_sense() in
the one instance where they weren't already set.

Fixes issue with softraid crypto volumes on 4K-sector disks.

Found, fix tested, and ok tim@


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.170 23-Nov-2019 krw

Consistently use ISSET() to check for set flags.


Revision tags: OPENBSD_6_6_BASE
# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.169 29-Sep-2019 krw

Introduce SC_DEBUG_SENSE() and simplify code by eliminating a bunch
of three line SCSIDEBUG chunks.


# 1.168 29-Sep-2019 krw

Continue SCSIDEBUG cleanup by nuking pointles SC_DEBUGN() #define.


# 1.167 29-Sep-2019 krw

Tweak some whitespace to make autoindenter happy. Use consistent naming idiom for
the debug functions.


# 1.166 27-Sep-2019 krw

Add/tweak #endif comments to make spelunking via grep more rewarding.


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.165 10-Sep-2019 krw

Shuffle a couple of lines to make the device -> st_softc dances look
the same.


# 1.164 10-Sep-2019 krw

No need to check for ST_MOUNTED before calling st_mount_tape() AND in
st_mount_tape(). Just keep the latter.

st_mount_tape() is called from inside stopen() which has already found
the relevant st_softc(), incremented the device reference count, and
checked if it is dying. No need to repeast that in
st_mount_tape(). Just pass the st_softc.


# 1.163 10-Sep-2019 krw

return is not a funciton call. Avoid pointless parenthesis.


# 1.162 10-Sep-2019 krw

Explicitly check error results against 0.


# 1.161 10-Sep-2019 krw

Some stray bit twiddles that were using yet another idiom.


# 1.160 10-Sep-2019 krw

check pointer against NULL and error values against 0.


# 1.159 10-Sep-2019 krw

Last (?) small bit twiddling modernization.


# 1.158 10-Sep-2019 krw

!ISSET() for various flags.


# 1.157 10-Sep-2019 krw

SET/CLR/ISSET all at once for st->quirks, and a few other rare flags.


# 1.156 09-Sep-2019 krw

SET/CLR/ISSET all at once for the far less numerous link->flags.


# 1.155 09-Sep-2019 krw

Continue bit twiddling tweaks. Use SET() with st->flags.


# 1.154 09-Sep-2019 krw

Lots of st->flags bit clearing in a mix of old school and CLR() makes
code harder to read. Switch to CLR().

No change to executable.


# 1.153 09-Sep-2019 krw

Lots of st->flags checking in a mix of old school and ISSET() makes
code harder to read. Switch to ISSET() where obvious.

No change to executable.


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.152 09-Sep-2019 krw

Tweak some comments to be useful. Sort switch cases into
numerical order. Move bit of logic before calling
st_unmount() into st_unmount().


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.151 07-Sep-2019 krw

Fold st_identify_drive() into stattach().


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.150 07-Sep-2019 krw

Tweak some comments and code layout to make MTSETBSIZE and MTSETDENSITY
cases look the same.


# 1.149 07-Sep-2019 krw

ST_USER_BLKSIZE and ST_QUIRK_BLKSIZE are only tested as an or'd pair.
Ditto for ST_USER_DENSITY and ST_QUIRK_DENSITY. Thus the origin of the
value stored in the mode is irrelevant. Just use two flags
(ST_MODE_BLKSIZE and ST_MODE_DENSITY) instead of the existing four.


# 1.148 07-Sep-2019 krw

ST_Q_FORCE_BLKSIZE is superfluous since it is only used in combination with
a non-zero value of blksize in the quirkdata. So just check for non-zero
value of blksize, as is already done for density.


# 1.147 07-Sep-2019 krw

Nuke unused flag ST_BLOCK_SET.


# 1.146 07-Sep-2019 krw

Various struct/variables "modes" -> "mode" since there is only one.


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.145 05-Sep-2019 krw

Nuke some 25+year old comments that the oracles at delphi would have
found obscure. Repair a broken WangTEK 5150ES quirks entry that
apparently suffered some damage when imported in 1995. It never made
NetBSD/FreeBSD quirk lists at all.

Some whitespace tweaking to make everything line up.


# 1.144 05-Sep-2019 krw

Devices that have no quirks don't need to appear in the list of
devices with quirks.


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.143 05-Sep-2019 krw

Nuke unused st_softc field 'numblks'.


# 1.142 05-Sep-2019 krw

There can be only one -- mode.

So fold 'modeflags' field of st_softc into 'flags' field of same. Nuke
a bunch of dubious/obvious comments.


# 1.141 04-Sep-2019 krw

Shuffle sdstart() and sdminphys() declarations closer to their friends.

Nuke pointless 'quirkdata' pointer in st_softc. We use the data once at
attach time and don't need to remember where we got it from.


# 1.140 04-Sep-2019 krw

Hoist the quirks up another rung. drive_quirks is superfluous, just
store the discovered quirks in st_softc's 'quirks' field.


# 1.139 04-Sep-2019 krw

'quirks' field of struct modes is superfluous. There is only 1 mode. Just use
the 'quirks' field of struct quirksdata.


# 1.138 04-Sep-2019 krw

Various whitespace nits.

Reformat quirks array the way the autoindenter likes it to minimize
the effort needed to keep diffs short.


# 1.137 04-Sep-2019 krw

The great density mode purge of 2006 (r1.62) was never carried through
to a logical conclusion. Start cleaning up the left overs.

First, delete the unused quirk ST_Q_BLKSIZE and simplify the one
condition referencing it in light of the fact that !ST_Q_BLKSIZE would
always be true.


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.136 17-Aug-2019 krw

Nuke some unused variables, tweak some declarations and
variable names into a consistant idiom.


Revision tags: OPENBSD_6_5_BASE
# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.135 20-Jan-2019 krw

When retiring a SCSI request, sometimes the buf's b_error value is
forcibly set to a value. Make sure that in all those cases the B_ERROR
flag is cleared (if b_error is being set to 0) or set (if b_error is
being set to non-zero) appropriately.

ok dlg@ jmatthew@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_2_BASE
# 1.134 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


# 1.133 29-May-2017 krw

To prevent anyone else from stumbling on this (now) archaic bit of
history, nuke all mentions of XS_NO_CCB and the #define.

2006 - 2017. R.I.P.

ok kettenis@ inferred ok dlg@


Revision tags: OPENBSD_6_1_BASE
# 1.132 04-Sep-2016 naddy

Remove support for tape block devices. Nobody mount(8)s tapes any longer.
ok deraadt@ guenther@


Revision tags: OPENBSD_6_0_BASE
# 1.131 12-Mar-2016 krw

Standardize on calling local scsi_link variables 'link' instead of
'sc_link'. 'sc_link' is a field in the various device *_softc
structures, where the sc_ prefix was for 'softc'. Reduces potential
mental confusion. Newer code from down under was already using
'link'.

No functional change.

Prompted by bluhm@'s adventures in scsi. ok dlg@ jmatthew@


# 1.130 11-Mar-2016 krw

"if (_3btol(cmd->len) != 0)" is more likely to be a useful test than
"if (cmd->len) != 0)", as cmd->len is a 3 byte array.

Broken since media_blkno accounting was introduced in r1.72 (2007!).
Might fix some obscure tape issues around miscounting variable sized blocks.

Spotted by mmcc@ and his friend clang.


Revision tags: OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.129 07-Jun-2015 krw

More damned eye searing whitespace.


Revision tags: OPENBSD_5_7_BASE
# 1.128 16-Dec-2014 tedu

primary change: move uvm_vnode out of vnode, keeping only a pointer.
objective: vnode.h doesn't include uvm_extern.h anymore.
followup changes: include uvm_extern.h or lock.h where necessary.
ok and help from deraadt


# 1.127 14-Sep-2014 jsg

remove uneeded proc.h includes
ok mpi@ kspillner@


Revision tags: OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.126 01-Nov-2013 krw

Sprinkle (long long) casts where %lld is being used to print daddr_t
variables. Some random whitespace/knf repairs encountered on the way.

ok miod@ on inspection, feedback & more suggestions from millert@


# 1.125 03-Oct-2013 krw

Print daddr_t variables with %lld, u_int64_t variables with %llu.


Revision tags: OPENBSD_5_4_BASE
# 1.124 05-Jul-2013 krw

Tweak calculations of resid to avoid unsigned vs signed comparison and
make code clearer.

Pointed out by brad@ and his friend llvm.


# 1.123 11-Jun-2013 deraadt

final removal of daddr64_t. daddr_t has been 64 bit for a long enough
test period; i think 3 years ago the last bugs fell out.
ok otto beck others


# 1.122 06-Jun-2013 krw

Fix EOM handling. Makes Bacula much happier by allowing the use of
both 'Fast Forward Space File = yes' and 'Hardware End of Medium = yes'.

Mostly taken from FreeBSD.

Constant prodding by robert@, testing actual backup and restore by
ajacoutot@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.121 03-Jul-2011 matthew

Remove config_activate() and DVACT_ACTIVATE. PCMCIA's the only thing
that's ever used it, and it's long since been changed to use
DVACT_{QUIESCE,SUSPEND,RESUME} instead.

ok deraadt@, dlg@; miod@ also agreed with this idea when I brought it
up a few weeks ago


# 1.120 07-Jun-2011 matthew

Fix a device reference leak in st{read,write}() by making them work
like {cd,sd}{read,write}().

ok krw@, dlg@


# 1.119 31-Mar-2011 jasper

- use nitems(); no binary change.

ok krw@


# 1.118 18-Mar-2011 matthew

Fix stdetach() to call vdevgone() with the right device minor numbers.
Some stylistic tweaks after discussions with krw@ and deraadt@.

ok krw@; "Lovely" deraadt@


# 1.117 17-Mar-2011 deraadt

use dma_alloc/dma_free instead of malloc to allocate buffers which need
to be in the right address space.
help from matthew and krw


Revision tags: OPENBSD_4_9_BASE
# 1.116 24-Dec-2010 krw

Have sd(4) devices check for and respect read-only information the
way st(4) does. Have both decline to open read-only devices for
anything but read-only access.

Suggestion to fail opens rather than individual i/o's from deraadt@.

Problem USB device found and donated by chefren, who also tested
diffs. Thanks!

ok dlg@ marco@


# 1.115 13-Oct-2010 krw

No need to check ST_DYING flag twice in succession.

Noticed by Damien McGuckin. Thanks!


# 1.114 22-Sep-2010 matthew

All users of physio(9) now pass NULL as the buf pointer argument, so
no point in keeping it around.

"i like this" thib@ (a while back); ok krw@ and oga@; reminder to
update the man page and tweaks jmc@


# 1.113 20-Sep-2010 deraadt

bufq_drain() does not need to be done in deactivate; we only need
to deactivate transactions we are currently doing.
ok dlg


# 1.112 14-Sep-2010 dlg

dont leak an xs in ststart if its called while the device is dying.

pointed out by matthew@


# 1.111 14-Sep-2010 dlg

scsi_xsh_del the xs handler on DEACTIVATE.

same change as sd.c r1.212. matthew@ pointed out that cd(4) and
st(4) would need the same change.


# 1.110 14-Sep-2010 krw

Clean up sense mode handling to make residual value handling clearer
and more plausibly correct.

Tested by jsing@ and myself.


# 1.109 01-Sep-2010 krw

Don't give xs->resid an invalid value (xs->datalen * st->blksize)
inside st_interpret_sense() and then exit without making sure it's
set back to <= xs->datalen.

Fixes 'done < 0; strategy broken' panics when un-tar'ing /cvs from
my ahci DAT tape drive.

ok dlg@ more-or-less-ok matthew@


# 1.108 01-Sep-2010 dlg

make struct bufq a member of the softc for devices that use it,
rather than it being a pointer to something that needs to be allocated
at attach. since all these devices need a bufq to operate, it makes
sense to have it allocated as part of the softc and get bufq_init
to just initialise all its fields. it also gets rid of the possibility
that you wont be able to allocate the bufq struct during attach,
which is something you dont want to happen.

secondly, it consistently implements a split between wrapper functions
and the per discipline implementation of the bufq handlers. it
consistently does the locking in the wrappers rather than doing
half in the wrappers and the other half in the implementations.

it also consistently handles the outstanding bufq bq pointer in the
wrappers.

this hides most of the implementation inside kern_bufq.c. the only
stuff left in buf.h is for the bits each implementation needs to
put inside struct buf.

tested by thib@ krw@ and me
ok thib@ matthew@
no objection from krw@


# 1.107 30-Aug-2010 matthew

Avoid possible aliasing-related compiler issues by always writing to
cmd->opcode through the type casted command pointer rather than
through xs->cmd->opcode.

Requested by deraadt@; ok krw@.


Revision tags: OPENBSD_4_8_BASE
# 1.106 22-Jul-2010 matthew

In st_space() and st_write_filemarks(), if scsi_xs_get() returns NULL,
set st->media_fileno and st->media_blkno before returning. Not sure
if this is needed or even really correct, but it's an unintentional
behavior change due to removing scsi_scsi_cmd().

ok dlg@


# 1.105 22-Jul-2010 krw

Eliminate scsi_scsi_cmd() now that ss(4) is gone and with it the
last bastardized sync and buf call through scsi_scsi_cmd(). Flatten
code to call scsi_xs_sync() directly for all commands.

Airplane typos shaken out by various.

ok dlg@ matthew@ deraadt@


# 1.104 01-Jul-2010 krw

Die struct scsi_device! Die! Instead, save a pointer to the routine
to interpret sense errors. This is initialized to the basic
interpretation routine, and specific scsi drivers (sd/st/cd) can
replace this with their own. While here kill EJUSTRETURN dance and
make more specialized interpretation routines directly call the
basic routine if desired.

Fixes by matthew@ to my first diff. Most original work by dlg@.

ok matthew@ marco@ dlg@


# 1.103 01-Jul-2010 matthew

Restore setting xs->bp; fixes ncr(4) on VAX.

ok miod@, krw@


# 1.102 30-Jun-2010 matthew

Prune some dead assignments in st_interpret_sense(); st(4) already
always explicitly sets bp->b_resid before calling biodone(), so these
are unneeded.

ok krw@


# 1.101 29-Jun-2010 matthew

Change st(4) to use the FIFO buf sorting discipline rather than the
default disk-sorting one.

ok krw@


# 1.100 26-Jun-2010 guenther

Don't #include <sys/user.h> into files that don't need the stuff
it defines. In some cases, this means pulling in uvm.h or pcb.h
instead, but most of the inclusions were just noise. Tested on
alpha, amd64, armish, hppa, i386, macpcc, sgi, sparc64, and vax,
mostly by krw and naddy.
ok krw@


# 1.99 25-Jun-2010 dlg

move st over to using xshandlers for scheduling io. based on the
same changes in cd and sd.

tested by and ok krw@


# 1.98 16-Jun-2010 krw

Bring ststrategy() into line with sdstrategy() and cdstrategy(), making
sure buf's are correctly completed.

Feedback from Matthew Dempsky.

ok dlg@


# 1.97 15-Jun-2010 dlg

dont pass the dev_t from the scsi device drivers into the midlayer for
ioctl requests, and dont pass the proc pointers around for any ioctl
requests in scsi land at all. neither were used, so trim the fat.

ok krw@ marco@


# 1.96 11-Jun-2010 krw

Restore an unusual XS_SENSE semantic that inadvertantly got lost
in the great re-write. If the scsi device *_interpret_sense()
function returns 0 that means there was no error.

Fixes restore(8) problems seen on certain tape drives.

Found and fix tested by Percey Piper. Suggestions from Matthew
Dempsky. Thanks!

ok dlg@


# 1.95 01-Jun-2010 thib

move cd, st & ss over to bufq's again.

Tested by myself and krw.
ok krw@


# 1.94 12-Apr-2010 dlg

i snuck fine grained locking into the midlayer as part of all the
other stuff ive been doing in here. everything that needs protection
inside the midlayer and the scsi device drivers (sd, cd, etc) uses
mutexes now.

this pushes splbio out of the midlayer. splbio is only taken before
biodone is called now.

ok beck@ marco@ krw@ deraadt@. theyre all terrified, but they all
say if we're going to do then now is the right stage of the dev
cycle.


Revision tags: OPENBSD_4_7_BASE
# 1.93 15-Jan-2010 krw

branches: 1.93.2;
Abstract and merge the manual buf queue manipulating functions into
one place for easier debugging and maintenance. No intended functional
changes.

ok dlg@


# 1.92 15-Jan-2010 krw

Restore XS_BUSY delay behaviour for buf i/o. Same as for sync path.

ok dlg@


# 1.91 12-Jan-2010 dlg

bring sd.c r1.180 over to the other drivers now using the antisemaphore,
ie, dont clear the WAITING flag when restarting the xxstart loops.


# 1.90 11-Jan-2010 krw

Bring mutex protections to ststart, cdstart and ssstart, as already
done in sd. Make names consistant across all three.

ok dlg@ tested (cd) & ok beck@


# 1.89 09-Jan-2010 dlg

dont try to prevent multiple concurrent runs of a devices start routine
by setting flags around the loop. there is a race which can prevent
necessary work being completed by any of the currently running instances
of xxstart.

the caveat with the removal of this code is because multiple xxstarts can
be running at the same time they can cause io reordering, but that is less
of a problem than no io.

found by and fix tested by claudio@
debugged with krw@ claudio@ beck@ deraadt@


# 1.88 26-Dec-2009 dlg

move st over to the new midlayer.

much testing and debugging from krw@
his diff was a nice christmas present


# 1.87 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.86 16-Feb-2009 miod

Extend the scsi_adapter minphys() callback to take a struct scsi_link *
as additional argument. This will allow intermediate layers between
scsi devices such as sd and scsi host adapters to take appropriate
action if necessary.


# 1.85 22-Sep-2008 miod

Do not return an uninitialized value on success in stclose().

ok krw@


# 1.84 06-Sep-2008 krw

Return EIO when the residual is > xs->datalen, even if SCSI_SILENT is
set. SCSI_SILENT mediates printing error messages only.


Revision tags: OPENBSD_4_4_BASE
# 1.83 22-Jun-2008 krw

Replace MUSTEK_RETRIES, SCANJET_RETRIES and ST_RETRIES with SCSI_RETRIES,
all defined to 4. Replace some magic number 4's with SCSI_RETRIES. Fix a
few lines that were now too long.


# 1.82 12-May-2008 krw

Fix device reference counting. Now that we try to support detachable
tape drives it is nice not to crash if one is detached. Basically use
a consistant mechanism modelled on sd to lookup devices and do the
device reference increments and decrements. Problem reported (PR#5811)
and fix tested by Jozef Hatala. Still some corner cases Jozef is
looking for but we'll fix those as discovered.


# 1.81 09-May-2008 krw

Oops. When detaching tape drives look for devices using 'stopen' not
devices using 'sdopen'. PR#5810 from Jozef Hatala.

ok beck@


Revision tags: OPENBSD_4_3_BASE
# 1.80 27-Nov-2007 martynas

typos; ok jmc@
sys/dev/pci/pciide.c from naddy@


# 1.79 22-Nov-2007 krw

Ansi-fy.


# 1.78 22-Nov-2007 krw

Ooops. Don't de-reference st until after it has been pointed at the
correct st_softc. Fixes 'mt rewoffl' panics, as reported on misc@ by
Jeff Ross and reproduced on my tape machine.


# 1.77 16-Nov-2007 deraadt

convert code over from sd.c, to let tapes discollect. Lucas Stefanutti
of pr 5635 confirms this works for him. similar diff authored by krw
who was unaware i wrote this and mailed it to Lucas...


# 1.76 16-Sep-2007 krw

A couple of obvious bzero() -> M_ZERO changes I missed.


Revision tags: OPENBSD_4_2_BASE
# 1.75 06-Jun-2007 deraadt

now that all partition size/offsets are potentially 64-bit, change the
type of all variables to daddr64_t. this includes the APIs for XXsize()
and XXdump(), all range checks inside bio drivers, internal variables
for disklabel handling, and even uvm's swap offsets. re-read numerous
times by otto, miod, krw, thib to look for errors


# 1.74 01-Jun-2007 krw

No need for tape fields to be daddr_t. int will do.

ok beck@ deraadt@


# 1.73 10-Apr-2007 miod

``it's'' -> ``its'' when the grammar gods require this change.


Revision tags: OPENBSD_4_1_BASE
# 1.72 20-Jan-2007 krw

Add media_fileno, media_blkno into st_softc and report their values
back via MTIOCGET ioctl, rather than always reporting -1 (unknown).

Simple 'Set to 0 on rewind, increment on file marks and positioning,
and set back to -1 on any ambiguity' logic. No functional impact on
current operation.

Should allow things like Bacula to work much faster by keeping track
of the current media position.

Adapted from FreeBSD code.

ok beck@


# 1.71 05-Jan-2007 krw

As first brought to my attention by Russel Sutherland, our deletion of
the mt_fileno and mt_blkno fields from the mtget struct was too hasty.

Subsequent discussions with Kern Sibbold of the Bacula project
revealed these are widely used by tape backup software on many
unicies. The de facto standard is to return -1 if you can't determine
the value.

So add them back and always return -1 as their value. Should allow
Bacula and similar software to at least compile while we investigate
providing meaningful values.

ok beck@


# 1.70 21-Dec-2006 krw

Fix some comment typos. 'immediatly' -> 'immediately', 'tranfer' ->
'transfer', 'tranfered' -> 'transferred'.


# 1.69 12-Dec-2006 krw

Give the SCSI layer the ability to requeue i/o's rejected by a driver
using the new NO_CCB result. Currently a no-op since no driver produces
that result.

ok dlg@ marco@ deraadt@


# 1.68 28-Nov-2006 dlg

rename scsibus_attach_args to scsi_attach_args. this can help avoid
confusing when trying to attach scsibus to a hba, since it is really meant
for attaching scsi devices to scsibus.

ok deraadt@ marco@


# 1.67 27-Nov-2006 beck

more magic removal
ok miod@, deraadt@


# 1.66 27-Nov-2006 beck

rework asc/ascq handling to get rid of "magic" 0x values in the code, changes
asc/ascq handling to deal with the pair together rather than seperately, since
modern scsi has long since polluted the "categories" of asc enough that you
pretty much need to always look as ascq.

ok krw@ deraadt@, dlg@


# 1.65 07-Oct-2006 beck

make cd-roms retry forever while the device indicates that it is
"becoming ready" - this is done in the exact same way that it
was done for tape in st.c. This commit adds a cd specific interpret_sense
routine to cd.c that will catch the becoming ready case and handle it.
This also removes the need to use crazy timeouts to catch this case.

ok krw@


# 1.64 02-Oct-2006 dlg

get rid of a boolean typedef. this is c, we have ints, deal with it.

ok marco@ krw@


Revision tags: OPENBSD_4_0_BASE
# 1.63 11-Jun-2006 krw

Don't spew sense informaton from the test unit ready command issued by
stopen(). This change was misplaced in the st roto-tilling at c2k6.
Noticed by deraadt@.


# 1.62 01-Jun-2006 krw

We do not create st devices with different density modes. i.e. with device
minor numbers having bits 2 or 3 set.

Eliminate the quirks, etc. used to store info on these non-existant modes.
Also eliminate a couple of 'unimplemented' fields in the ioctl request
structure.

ok beck@ deraadt@


# 1.61 28-May-2006 krw

Miscellaneous fixes.

1) Check the return value from st_mount_tape().

2) Always call st_unmount() if the media goes missing.

3) Use the special handling for 'Unit in the process of becoming
ready' for the TEST UNIT READY command in stopen().

4) Don't unmount a tape during stopen() just because it doesn't have
the current default density. Eliminate now unused last_dsty member of
st_softc.

tested & ok beck@


# 1.60 28-May-2006 beck

This commit changes the interpret_sense routine for st to catch the
"Unit Becoming Ready" case, and just like ch, allow the driver to
continue waiting, as long as the unit continues to promise us that
the unit is becoming ready. This avoids the case of slow tape drives
taking forever to bring media online requiring a huge retry count
being set for all commands, and like ch, will wait indefinately
only in this case. All other cases are handled as before.

ok krw@


# 1.59 28-May-2006 krw

In the absence of a coherent concept of 'RAW' tape devices, pass all
unhandled ioctl requests to scsi_do_ioctl() like ch.c does. Previously
only the mythical 'control devices' with certain minor numbers would
pass the requests along.

ok beck@


# 1.58 28-May-2006 krw

Whack a 'control device' leftover. Actually save the new density, etc.
that was just accepted by the device, no matter what the minor is.

ok beck@


# 1.57 28-May-2006 krw

We don't have 'control devices' for st. Nor are we likely to get any
in the foreseeable future. Eliminate half-baked, semi-operational code
that tried to handle such devices. People who had stumbled across the
previous control-like behaviour of erstX devices should not be relying
on these semantics.

Suggested by deraadt@.

ok beck@


# 1.56 28-May-2006 krw

ST_NEW_MOUNT was set/reset in a number of places but only examined inside
a #if notyet/#endif block. It's been 'notyet' since 1995. Eliminate it.

'flense!' beck@


# 1.55 28-May-2006 krw

Use TEST_READY_RETRIES_TAPE instead of TEST_READY_RETRIES_DEFAULT,
thus allowing tape drives more time to become ready.

ok beck@


# 1.54 28-May-2006 krw

Use bit 23 of the device minor to indicate a control mode device. A
control mode device can be opened but does not permit any i/o.

Currently no such devices are created by MAKEDEV. However the previous
code used bits 0 and 1 of the minor being set to indicate a control
device. This conflicted with other uses of device minors to indicate
things like rewind and eject. This behaviour would result in an open
of a newly loaded tape drive failing until the tape was successfully
mounted as a side effect of another command like 'mt status'.

ok beck@


# 1.53 28-May-2006 krw

Always allow ioctl's to work, even if there is no media loaded.
Analogous to what sd does for the raw partition. Eliminates useless
error messages and premature command completion. e.g. 'mt status' will
wait for media to to finish loading.

ok beck@


# 1.52 28-May-2006 krw

Don't print tape media information during probe. This is transitory
information that doesn't have anything to do with the device. We don't
say 'cd drive empty or not ready."

ok beck@


# 1.51 11-May-2006 krw

Zap trailing whitespace.


Revision tags: OPENBSD_3_9_BASE
# 1.50 11-Sep-2005 krw

Simplify code by changing struct scsi_mode_sense_buf{ union {}
headers; } to just union scsi_mode_sense_buf {}. No functional change.

ok marco@


Revision tags: OPENBSD_3_8_BASE
# 1.49 23-Aug-2005 krw

Move uses of struct scsi_mode_sense_buf (255 bytes each) from the
stack into malloc'd memory.

ok deraadt@


# 1.48 24-Jun-2005 krw

Move st.c to new mode sense mechanism. Don't save page0 data (if any), just
ask for it when needed. Eliminate the page_0_size quirk.

Tested on various tape drives by myself, hshoexer@ and henning@.


# 1.47 05-Jun-2005 krw

Fix scsi_mode_select() and scsi_mode_select_big() to send just the
required number of bytes, rather than a full scsi_mode_sense_buf. Some
devices (e.g. my HP SureStore DAT/24) decline to accept such oversized
transfers. Instead, force callers to fill in the data_length field in
the header and use that information to set the size of the transfer.


# 1.46 14-May-2005 krw

Add scsi_mode_[sense|select]_big() and use it instead of
atapi_mode_[sense|select](). This will allow the removal of
atapi_base.c and atapi_all.h files, and sets the stage for better usb
mode sense handling amoung other things.

Remove cd_scsibus_[get|set]_mode() and
sd_scsibus_mode_sense() shims in favour of scsi_mode_[sense|select]().
Add zero'ing of reserved length field in scsi_mode_select(). Change
some uchar * type parameters to scsi_mode_header *.

No functional changes. Mostly from NetBSD.

ok dlg@.


# 1.45 06-Apr-2005 krw

De-register scsi.


# 1.44 05-Apr-2005 krw

Factor out scsi_mode_sense() and scsi_mode_select() like NetBSD does.
Eliminate some duplicate structures and defines along the way. SCSI CD
drives will now pay attention to the 'flags' parameter for MODE SENSE
and MODE SELECT commands. No other functional change.

ok tdeval@


Revision tags: OPENBSD_3_7_BASE
# 1.43 30-Nov-2004 krw

No longer require write permission for read-only SCSI commands
submitted via ioctl. Eliminate scsi_do_safeioctl(), thus permitting
ch, ss and st devices to submit SCSI commands. Return EPERM rather
than EBADF when permissions are inadequate.

Allows more programs to run with fewer permissions. Problem diagnosed
by Nikolay Sturm in mplayer port.

Tested by Nikolay, ok deraadt@.


# 1.42 16-Oct-2004 krw

Fix 'Tape block size (32754) not a multiple of dump block size
(1024).' errors in restore(1).

Check for SDEV_OPEN in sc_link->flags (where it lives) rather than
sense->flags (where it doesn't).

Problem noted by John Danks on misc@ and reinforced by Otto & Hugh.

ok marco@ hugh@.


Revision tags: OPENBSD_3_6_BASE
# 1.41 01-Aug-2004 marco

branches: 1.41.2;
Remove openings = 1 and inherit the value from the HBA instead. This keeps the tape device stuffed with data and therefore it rewinds less often to reposition.

ok krw@


# 1.40 22-Jun-2004 marco

Add additional retries in TUR to allow for tape devices to settle after a reset.
More informative failure message and silence "already open" message.

ok krw@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.39 28-May-2004 krw

Clean up sense error logic and printing a bit, partly inspired by
NetBSD. Try to limit special handling of sense errors in sd and st to
a minimum.

ok marco@


# 1.38 17-May-2004 krw

Elminate SCSIRET_* synonyms for EJUSTRETURN and ERESTART by using
EJUSTRETURN and ERESTART instead. Delete unused SCSIRET_RETRY define.
From NetBSD.

ok marco@


# 1.37 17-May-2004 aoyama

Add a quirk entry for TEAC compact cassette tape drive, taken from NetBSD.
ok miod@ tdeval@ marco@ krw@


# 1.36 09-May-2004 krw

Add missing <cr>'s to various SC_DEBUG() calls, along with a few other
SC_DEBUG() spacing tweaks.

ok deraadt@


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.35 18-May-2003 mickey

constify the quirck tables and fix the scsi_inqmatch() proto accordingly; krw@ ok


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 11-Mar-2003 krw

Fix incorrect restriction on valid scsi tape density code values, allowing all
currently valid values (0 - 0xff) to be used in mt(1). Add sanity
check to catch negative values.

Minor cleanup so mt_count values are stored in daddr_t variables and
not ints. Eliminate unnecessary cast of mt_op, since it is already a
shot. Leave possible move of mt_count to int32_t rather than daddr_t
to post-3.3.

Problem found, and original diff by Joseph Bender.

costa@ henning@ tdeval@ ok


# 1.33 30-Dec-2002 grange

Add new parameter to scsi_test_unit_ready(): retries number.
Use increased retries number and don't ignore SCSI_IGNORE_NOT_READY
when call scsi_test_unit_ready() for cd-rom, this makes system wait
if drive is loading media.
Tested by millert@ and fgsch@; some input and ok from krw@.
Problem reported by The lord of the CD-writers
Igor Grabin <violent@death.kiev.ua>.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.32 09-Jun-2002 art

More (the last round?) of splasserts and splbio around biodone.


# 1.31 04-Jun-2002 deraadt

spelling; raj@cerias.purdue.edu


Revision tags: OPENBSD_3_1_BASE
# 1.30 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.29 22-Jun-2001 deraadt

branches: 1.29.4;
KNF


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.28 08-Apr-2000 csapuntz

These days, attach can occur outside the tsleep-restricted world of
BSD autoconf.

Don't use POLL & NOSLEEP mode if attaching after autoconf


Revision tags: OPENBSD_2_6_BASE SMP_BASE kame_19991208
# 1.27 05-Sep-1999 niklas

branches: 1.27.4;
ATAPI tapes are always fixed blocksize


# 1.26 25-Jul-1999 csapuntz

Merge sd stuff from NetBSD-current. Helps with LS-120, ZIP

More SCSI logic from NetBSD-current

Some tape fixes. ATAPI tapes do not work yet for most operations.


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE
# 1.25 23-Jul-1998 deraadt

do delays based on command type -- this works a lot better, make "mt status"
show much more information; ross


Revision tags: OPENBSD_2_3_BASE
# 1.24 22-Feb-1998 niklas

Detect and report a weird error case (residual > request)


# 1.23 16-Feb-1998 deraadt

Add the "VIPER 150/21531" to quirk list (ST_Q_SENSE_HELP; pk


# 1.22 10-Jan-1998 deraadt

be silent for ILI errors ; giannici@neomedia.it


Revision tags: OPENBSD_2_2_BASE
# 1.21 05-Sep-1997 millert

Add SCSITERSE option which omits the (large) ASC/ASCQ table.
Old scsi error printing code is now gone.


# 1.20 03-Sep-1997 deraadt

be silent for SCSI_SILENT transactions


# 1.19 22-Aug-1997 millert

Add more detailed SCSI error printing by Matthew Jacob. Enabled by
default. Use "option SCSIVERBOSE=0" to turn off (only added 8Kb to my kernel).
From the NetBSD commit message:
Add in more detailed SCSI error printing, based off of final SCSI-2 spec.
This will also print out things like progress indicator errors, and will
also dump out additional sense bytes that haven't been decoded.


# 1.18 19-Aug-1997 dgregor

Back out part of revision 1.16 where the tape was unmounted in stclose on
a non-rewinding device. When the tape was re-opened again, st_mount_tape
called st_load, which causes the tape to be rewound.

The best way to fix this problem would be to not call st_load in
st_mount_tape if the tape is already in the drive and has already been
loaded. But, I haven't figured out a good, safe way to do that yet....


# 1.17 11-Jul-1997 tholo

Rename REWIND to DOREWIND to avoid colliding with the SCSI opcode of the
same name


# 1.16 11-Jul-1997 tholo

Allow media to be removed on last close even if the device used was the
no-rewind device. Ensure we re-detect media on first open even if media
was present at boot.


Revision tags: OPENBSD_2_1_BASE
# 1.15 14-Apr-1997 downsj

Merge in various pieces of current NetBSD scsi code, including but not limited
to:

* New changer driver.
* Better optical support.
* Different `done' semantics.
* New quirks for SCSI QIC tape driver, SCSI floppy drives.
* Better support for SCSI-I devices.

Everybody needs to test this.


# 1.14 24-Feb-1997 jkatz

This changes the timeout on the st device so that the new tape drives that
are recognized are usable :-) If this break things for other people,
please feel free to change it back. from port-vax@netbsd.org


# 1.13 11-Dec-1996 deraadt

b_resid cleanups, pointed out by minoura@kw.netlaputa.or.jp in netbsd pr#3007


Revision tags: OPENBSD_2_0_BASE
# 1.12 11-Aug-1996 deraadt

limit ioctl priviledges


# 1.11 10-May-1996 deraadt

SC_DEBUG cleanup


# 1.10 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.9 19-Apr-1996 niklas

NetBSD 960317 merge


# 1.8 20-Feb-1996 briggs

Several changes from NetBSD:
- scsi prototypes
- remove #ifdef notdef made unnecessary by previous changes (PR#1597)
put in missing "if (error)" that caused tape IO to always fail.
(closes PR#2086)
- Minor change.


# 1.7 09-Feb-1996 briggs

NetBSD PR 2048. Prototype st_erase.


# 1.6 16-Jan-1996 deraadt

from netbsd:
Honor cache request and add the SCSI tape device configuration page.
Fixes PRs 807, 1201, and 1705. From John Kohl <jtk@kolvir.blrc.ma.us>.


# 1.5 14-Dec-1995 deraadt

from netbsd; Add quirk for Exabyte 8200/rev. 263H (from Paul Goyette; PR#1797)


# 1.4 21-Nov-1995 deraadt

make this work


# 1.3 20-Nov-1995 deraadt

add erase support and another wangtek tape drive; from rhialto@polder.ubc.kun.nl; netbsd pr#1705


# 1.2 19-Nov-1995 deraadt

if you read with a smaller buffer than the block on the tape, b_resid and b_count would be incorrect; from jtk@kolvir.arlington.ma.us; netbsd pr#1597


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision